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>
51 #ifdef HAVE_NET_PFVAR_H
53 * In NetBSD <net/if.h> includes <net/dlt.h>, which is an older version of
54 * "pcap/dlt.h" with a lower value of DLT_MATCHING_MAX. Include the headers
55 * below before "pcap-int.h", which eventually includes "pcap/dlt.h", which
56 * redefines DLT_MATCHING_MAX from what this version of NetBSD has to what
57 * this version of libpcap has.
59 #include <sys/socket.h>
61 #include <net/pfvar.h>
62 #include <net/if_pflog.h>
63 #endif /* HAVE_NET_PFVAR_H */
69 #include "ethertype.h"
73 #include "ieee80211.h"
75 #include "sunatmpos.h"
78 #include "pcap/ipnet.h"
80 #include "diag-control.h"
85 #include <linux/types.h>
86 #include <linux/if_packet.h>
87 #include <linux/filter.h>
91 #define offsetof(s, e) ((size_t)&((s *)0)->e)
96 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
102 uint8_t u6_addr8
[16];
103 uint16_t u6_addr16
[8];
104 uint32_t u6_addr32
[4];
106 #define s6_addr in6_u.u6_addr8
107 #define s6_addr16 in6_u.u6_addr16
108 #define s6_addr32 in6_u.u6_addr32
109 #define s6_addr64 in6_u.u6_addr64
112 typedef unsigned short sa_family_t
;
114 #define __SOCKADDR_COMMON(sa_prefix) \
115 sa_family_t sa_prefix##family
117 /* Ditto, for IPv6. */
120 __SOCKADDR_COMMON (sin6_
);
121 uint16_t sin6_port
; /* Transport layer port # */
122 uint32_t sin6_flowinfo
; /* IPv6 flow information */
123 struct in6_addr sin6_addr
; /* IPv6 address */
126 #ifndef EAI_ADDRFAMILY
128 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
129 int ai_family
; /* PF_xxx */
130 int ai_socktype
; /* SOCK_xxx */
131 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
132 size_t ai_addrlen
; /* length of ai_addr */
133 char *ai_canonname
; /* canonical name for hostname */
134 struct sockaddr
*ai_addr
; /* binary address */
135 struct addrinfo
*ai_next
; /* next structure in linked list */
137 #endif /* EAI_ADDRFAMILY */
138 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
141 #include <netdb.h> /* for "struct addrinfo" */
143 #include <pcap/namedb.h>
145 #include "nametoaddr.h"
147 #define ETHERMTU 1500
149 #ifndef IPPROTO_HOPOPTS
150 #define IPPROTO_HOPOPTS 0
152 #ifndef IPPROTO_ROUTING
153 #define IPPROTO_ROUTING 43
155 #ifndef IPPROTO_FRAGMENT
156 #define IPPROTO_FRAGMENT 44
158 #ifndef IPPROTO_DSTOPTS
159 #define IPPROTO_DSTOPTS 60
162 #define IPPROTO_SCTP 132
165 #define GENEVE_PORT 6081
167 #ifdef HAVE_OS_PROTO_H
168 #include "os-proto.h"
171 #define JMP(c) ((c)|BPF_JMP|BPF_K)
174 * "Push" the current value of the link-layer header type and link-layer
175 * header offset onto a "stack", and set a new value. (It's not a
176 * full-blown stack; we keep only the top two items.)
178 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
180 (cs)->prevlinktype = (cs)->linktype; \
181 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
182 (cs)->linktype = (new_linktype); \
183 (cs)->off_linkhdr.is_variable = (new_is_variable); \
184 (cs)->off_linkhdr.constant_part = (new_constant_part); \
185 (cs)->off_linkhdr.reg = (new_reg); \
186 (cs)->is_geneve = 0; \
190 * Offset "not set" value.
192 #define OFFSET_NOT_SET 0xffffffffU
195 * Absolute offsets, which are offsets from the beginning of the raw
196 * packet data, are, in the general case, the sum of a variable value
197 * and a constant value; the variable value may be absent, in which
198 * case the offset is only the constant value, and the constant value
199 * may be zero, in which case the offset is only the variable value.
201 * bpf_abs_offset is a structure containing all that information:
203 * is_variable is 1 if there's a variable part.
205 * constant_part is the constant part of the value, possibly zero;
207 * if is_variable is 1, reg is the register number for a register
208 * containing the variable value if the register has been assigned,
218 * Value passed to gen_load_a() to indicate what the offset argument
219 * is relative to the beginning of.
222 OR_PACKET
, /* full packet data */
223 OR_LINKHDR
, /* link-layer header */
224 OR_PREVLINKHDR
, /* previous link-layer header */
225 OR_LLC
, /* 802.2 LLC header */
226 OR_PREVMPLSHDR
, /* previous MPLS header */
227 OR_LINKTYPE
, /* link-layer type */
228 OR_LINKPL
, /* link-layer payload */
229 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
230 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
231 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
235 * We divy out chunks of memory rather than call malloc each time so
236 * we don't have to worry about leaking memory. It's probably
237 * not a big deal if all this memory was wasted but if this ever
238 * goes into a library that would probably not be a good idea.
240 * XXX - this *is* in a library....
243 #define CHUNK0SIZE 1024
249 /* Code generator state */
251 struct _compiler_state
{
262 int outermostlinktype
;
267 /* Hack for handling VLAN and MPLS stacks. */
268 u_int label_stack_depth
;
269 u_int vlan_stack_depth
;
275 * As errors are handled by a longjmp, anything allocated must
276 * be freed in the longjmp handler, so it must be reachable
279 * One thing that's allocated is the result of pcap_nametoaddrinfo();
280 * it must be freed with freeaddrinfo(). This variable points to
281 * any addrinfo structure that would need to be freed.
286 * Another thing that's allocated is the result of pcap_ether_aton();
287 * it must be freed with free(). This variable points to any
288 * address that would need to be freed.
293 * Various code constructs need to know the layout of the packet.
294 * These values give the necessary offsets from the beginning
295 * of the packet data.
299 * Absolute offset of the beginning of the link-layer header.
301 bpf_abs_offset off_linkhdr
;
304 * If we're checking a link-layer header for a packet encapsulated
305 * in another protocol layer, this is the equivalent information
306 * for the previous layers' link-layer header from the beginning
307 * of the raw packet data.
309 bpf_abs_offset off_prevlinkhdr
;
312 * This is the equivalent information for the outermost layers'
315 bpf_abs_offset off_outermostlinkhdr
;
318 * Absolute offset of the beginning of the link-layer payload.
320 bpf_abs_offset off_linkpl
;
323 * "off_linktype" is the offset to information in the link-layer
324 * header giving the packet type. This is an absolute offset
325 * from the beginning of the packet.
327 * For Ethernet, it's the offset of the Ethernet type field; this
328 * means that it must have a value that skips VLAN tags.
330 * For link-layer types that always use 802.2 headers, it's the
331 * offset of the LLC header; this means that it must have a value
332 * that skips VLAN tags.
334 * For PPP, it's the offset of the PPP type field.
336 * For Cisco HDLC, it's the offset of the CHDLC type field.
338 * For BSD loopback, it's the offset of the AF_ value.
340 * For Linux cooked sockets, it's the offset of the type field.
342 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
343 * encapsulation, in which case, IP is assumed.
345 bpf_abs_offset off_linktype
;
348 * TRUE if the link layer includes an ATM pseudo-header.
353 * TRUE if "geneve" appeared in the filter; it causes us to
354 * generate code that checks for a Geneve header and assume
355 * that later filters apply to the encapsulated payload.
360 * TRUE if we need variable length part of VLAN offset
362 int is_vlan_vloffset
;
365 * These are offsets for the ATM pseudo-header.
372 * These are offsets for the MTP2 fields.
378 * These are offsets for the MTP3 fields.
386 * This is the offset of the first byte after the ATM pseudo_header,
387 * or -1 if there is no ATM pseudo-header.
392 * These are offsets to the beginning of the network-layer header.
393 * They are relative to the beginning of the link-layer payload
394 * (i.e., they don't include off_linkhdr.constant_part or
395 * off_linkpl.constant_part).
397 * If the link layer never uses 802.2 LLC:
399 * "off_nl" and "off_nl_nosnap" are the same.
401 * If the link layer always uses 802.2 LLC:
403 * "off_nl" is the offset if there's a SNAP header following
406 * "off_nl_nosnap" is the offset if there's no SNAP header.
408 * If the link layer is Ethernet:
410 * "off_nl" is the offset if the packet is an Ethernet II packet
411 * (we assume no 802.3+802.2+SNAP);
413 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
414 * with an 802.2 header following it.
420 * Here we handle simple allocation of the scratch registers.
421 * If too many registers are alloc'd, the allocator punts.
423 int regused
[BPF_MEMWORDS
];
429 struct chunk chunks
[NCHUNKS
];
434 * For use by routines outside this file.
438 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
443 * If we've already set an error, don't override it.
444 * The lexical analyzer reports some errors by setting
445 * the error and then returning a LEX_ERROR token, which
446 * is not recognized by any grammar rule, and thus forces
447 * the parse to stop. We don't want the error reported
448 * by the lexical analyzer to be overwritten by the syntax
451 if (!cstate
->error_set
) {
453 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
456 cstate
->error_set
= 1;
461 * For use *ONLY* in routines in this file.
463 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
464 PCAP_PRINTFLIKE(2, 3);
467 static void PCAP_NORETURN
468 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
473 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
476 longjmp(cstate
->top_ctx
, 1);
483 static int init_linktype(compiler_state_t
*, pcap_t
*);
485 static void init_regs(compiler_state_t
*);
486 static int alloc_reg(compiler_state_t
*);
487 static void free_reg(compiler_state_t
*, int);
489 static void initchunks(compiler_state_t
*cstate
);
490 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
491 static void *newchunk(compiler_state_t
*cstate
, size_t);
492 static void freechunks(compiler_state_t
*cstate
);
493 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
494 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
495 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
496 static inline void syntax(compiler_state_t
*cstate
);
498 static void backpatch(struct block
*, struct block
*);
499 static void merge(struct block
*, struct block
*);
500 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
502 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
504 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
506 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
508 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
510 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
511 u_int
, bpf_u_int32
, bpf_u_int32
);
512 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
513 u_int
, const u_char
*);
514 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
515 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
516 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
518 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
520 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
521 static struct block
*gen_uncond(compiler_state_t
*, int);
522 static inline struct block
*gen_true(compiler_state_t
*);
523 static inline struct block
*gen_false(compiler_state_t
*);
524 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
525 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
526 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
527 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
528 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
529 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
530 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
531 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
532 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
534 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
535 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
536 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
537 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
538 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
539 int, bpf_u_int32
, u_int
, u_int
);
541 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
542 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
544 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
545 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
546 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
547 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
548 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
549 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
550 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
551 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
552 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
555 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
556 struct in6_addr
*, int, int, int);
559 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
560 struct addrinfo
*, int, int);
562 static struct block
*gen_ipfrag(compiler_state_t
*);
563 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
564 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
566 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
567 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
569 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
570 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
571 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
573 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
574 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
575 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
576 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
578 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
579 static int lookup_proto(compiler_state_t
*, const char *, int);
580 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
581 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
582 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
583 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
584 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
585 static struct block
*gen_len(compiler_state_t
*, int, int);
586 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
587 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
589 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
590 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
591 bpf_u_int32
, int, int);
592 static struct block
*gen_atmtype_llc(compiler_state_t
*);
593 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
596 initchunks(compiler_state_t
*cstate
)
600 for (i
= 0; i
< NCHUNKS
; i
++) {
601 cstate
->chunks
[i
].n_left
= 0;
602 cstate
->chunks
[i
].m
= NULL
;
604 cstate
->cur_chunk
= 0;
608 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
615 /* XXX Round up to nearest long. */
616 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
618 /* XXX Round up to structure boundary. */
622 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
623 if (n
> cp
->n_left
) {
625 k
= ++cstate
->cur_chunk
;
627 bpf_set_error(cstate
, "out of memory");
630 size
= CHUNK0SIZE
<< k
;
631 cp
->m
= (void *)malloc(size
);
633 bpf_set_error(cstate
, "out of memory");
636 memset((char *)cp
->m
, 0, size
);
639 bpf_set_error(cstate
, "out of memory");
644 return (void *)((char *)cp
->m
+ cp
->n_left
);
648 newchunk(compiler_state_t
*cstate
, size_t n
)
652 p
= newchunk_nolongjmp(cstate
, n
);
654 longjmp(cstate
->top_ctx
, 1);
661 freechunks(compiler_state_t
*cstate
)
665 for (i
= 0; i
< NCHUNKS
; ++i
)
666 if (cstate
->chunks
[i
].m
!= NULL
)
667 free(cstate
->chunks
[i
].m
);
671 * A strdup whose allocations are freed after code generation is over.
672 * This is used by the lexical analyzer, so it can't longjmp; it just
673 * returns NULL on an allocation error, and the callers must check
677 sdup(compiler_state_t
*cstate
, const char *s
)
679 size_t n
= strlen(s
) + 1;
680 char *cp
= newchunk_nolongjmp(cstate
, n
);
684 pcap_strlcpy(cp
, s
, n
);
688 static inline struct block
*
689 new_block(compiler_state_t
*cstate
, int code
)
693 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
700 static inline struct slist
*
701 new_stmt(compiler_state_t
*cstate
, int code
)
705 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
711 static struct block
*
712 gen_retblk(compiler_state_t
*cstate
, int v
)
714 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
720 static inline PCAP_NORETURN_DEF
void
721 syntax(compiler_state_t
*cstate
)
723 bpf_error(cstate
, "syntax error in filter expression");
727 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
728 const char *buf
, int optimize
, bpf_u_int32 mask
)
733 compiler_state_t cstate
;
734 const char * volatile xbuf
= buf
;
735 yyscan_t scanner
= NULL
;
736 volatile YY_BUFFER_STATE in_buffer
= NULL
;
741 * If this pcap_t hasn't been activated, it doesn't have a
742 * link-layer type, so we can't use it.
745 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
746 "not-yet-activated pcap_t passed to pcap_compile");
758 * If the device on which we're capturing need to be notified
759 * that a new filter is being compiled, do so.
761 * This allows them to save a copy of it, in case, for example,
762 * they're implementing a form of remote packet capture, and
763 * want the remote machine to filter out the packets in which
764 * it's sending the packets it's captured.
766 * XXX - the fact that we happen to be compiling a filter
767 * doesn't necessarily mean we'll be installing it as the
768 * filter for this pcap_t; we might be running it from userland
769 * on captured packets to do packet classification. We really
770 * need a better way of handling this, but this is all that
771 * the WinPcap remote capture code did.
773 if (p
->save_current_filter_op
!= NULL
)
774 (p
->save_current_filter_op
)(p
, buf
);
778 cstate
.no_optimize
= 0;
783 cstate
.ic
.root
= NULL
;
784 cstate
.ic
.cur_mark
= 0;
786 cstate
.error_set
= 0;
789 cstate
.netmask
= mask
;
791 cstate
.snaplen
= pcap_snapshot(p
);
792 if (cstate
.snaplen
== 0) {
793 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
794 "snaplen of 0 rejects all packets");
799 if (pcap_lex_init(&scanner
) != 0)
800 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
801 errno
, "can't initialize scanner");
802 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
805 * Associate the compiler state with the lexical analyzer
808 pcap_set_extra(&cstate
, scanner
);
810 if (init_linktype(&cstate
, p
) == -1) {
814 if (pcap_parse(scanner
, &cstate
) != 0) {
816 if (cstate
.ai
!= NULL
)
817 freeaddrinfo(cstate
.ai
);
819 if (cstate
.e
!= NULL
)
825 if (cstate
.ic
.root
== NULL
) {
827 * Catch errors reported by gen_retblk().
829 if (setjmp(cstate
.top_ctx
)) {
833 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
836 if (optimize
&& !cstate
.no_optimize
) {
837 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
842 if (cstate
.ic
.root
== NULL
||
843 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
844 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
845 "expression rejects all packets");
850 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
851 cstate
.ic
.root
, &len
, p
->errbuf
);
852 if (program
->bf_insns
== NULL
) {
857 program
->bf_len
= len
;
859 rc
= 0; /* We're all okay */
863 * Clean up everything for the lexical analyzer.
865 if (in_buffer
!= NULL
)
866 pcap__delete_buffer(in_buffer
, scanner
);
868 pcap_lex_destroy(scanner
);
871 * Clean up our own allocated memory.
879 * entry point for using the compiler with no pcap open
880 * pass in all the stuff that is needed explicitly instead.
883 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
884 struct bpf_program
*program
,
885 const char *buf
, int optimize
, bpf_u_int32 mask
)
890 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
893 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
899 * Clean up a "struct bpf_program" by freeing all the memory allocated
903 pcap_freecode(struct bpf_program
*program
)
906 if (program
->bf_insns
!= NULL
) {
907 free((char *)program
->bf_insns
);
908 program
->bf_insns
= NULL
;
913 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
914 * which of the jt and jf fields has been resolved and which is a pointer
915 * back to another unresolved block (or nil). At least one of the fields
916 * in each block is already resolved.
919 backpatch(struct block
*list
, struct block
*target
)
936 * Merge the lists in b0 and b1, using the 'sense' field to indicate
937 * which of jt and jf is the link.
940 merge(struct block
*b0
, struct block
*b1
)
942 register struct block
**p
= &b0
;
944 /* Find end of list. */
946 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
948 /* Concatenate the lists. */
953 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
955 struct block
*ppi_dlt_check
;
958 * Catch errors reported by us and routines below us, and return -1
961 if (setjmp(cstate
->top_ctx
))
965 * Insert before the statements of the first (root) block any
966 * statements needed to load the lengths of any variable-length
967 * headers into registers.
969 * XXX - a fancier strategy would be to insert those before the
970 * statements of all blocks that use those lengths and that
971 * have no predecessors that use them, so that we only compute
972 * the lengths if we need them. There might be even better
973 * approaches than that.
975 * However, those strategies would be more complicated, and
976 * as we don't generate code to compute a length if the
977 * program has no tests that use the length, and as most
978 * tests will probably use those lengths, we would just
979 * postpone computing the lengths so that it's not done
980 * for tests that fail early, and it's not clear that's
983 insert_compute_vloffsets(cstate
, p
->head
);
986 * For DLT_PPI captures, generate a check of the per-packet
987 * DLT value to make sure it's DLT_IEEE802_11.
989 * XXX - TurboCap cards use DLT_PPI for Ethernet.
990 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
991 * with appropriate Ethernet information and use that rather
992 * than using something such as DLT_PPI where you don't know
993 * the link-layer header type until runtime, which, in the
994 * general case, would force us to generate both Ethernet *and*
995 * 802.11 code (*and* anything else for which PPI is used)
996 * and choose between them early in the BPF program?
998 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
999 if (ppi_dlt_check
!= NULL
)
1000 gen_and(ppi_dlt_check
, p
);
1002 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
1003 p
->sense
= !p
->sense
;
1004 backpatch(p
, gen_retblk(cstate
, 0));
1005 cstate
->ic
.root
= p
->head
;
1010 gen_and(struct block
*b0
, struct block
*b1
)
1012 backpatch(b0
, b1
->head
);
1013 b0
->sense
= !b0
->sense
;
1014 b1
->sense
= !b1
->sense
;
1016 b1
->sense
= !b1
->sense
;
1017 b1
->head
= b0
->head
;
1021 gen_or(struct block
*b0
, struct block
*b1
)
1023 b0
->sense
= !b0
->sense
;
1024 backpatch(b0
, b1
->head
);
1025 b0
->sense
= !b0
->sense
;
1027 b1
->head
= b0
->head
;
1031 gen_not(struct block
*b
)
1033 b
->sense
= !b
->sense
;
1036 static struct block
*
1037 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1038 u_int size
, bpf_u_int32 v
)
1040 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1043 static struct block
*
1044 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1045 u_int size
, bpf_u_int32 v
)
1047 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1050 static struct block
*
1051 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1052 u_int size
, bpf_u_int32 v
)
1054 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1057 static struct block
*
1058 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1059 u_int size
, bpf_u_int32 v
)
1061 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1064 static struct block
*
1065 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1066 u_int size
, bpf_u_int32 v
)
1068 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1071 static struct block
*
1072 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1073 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1075 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1078 static struct block
*
1079 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1080 u_int size
, const u_char
*v
)
1082 register struct block
*b
, *tmp
;
1086 register const u_char
*p
= &v
[size
- 4];
1088 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1096 register const u_char
*p
= &v
[size
- 2];
1098 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1106 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1115 * AND the field of size "size" at offset "offset" relative to the header
1116 * specified by "offrel" with "mask", and compare it with the value "v"
1117 * with the test specified by "jtype"; if "reverse" is true, the test
1118 * should test the opposite of "jtype".
1120 static struct block
*
1121 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1122 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1125 struct slist
*s
, *s2
;
1128 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1130 if (mask
!= 0xffffffff) {
1131 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1136 b
= new_block(cstate
, JMP(jtype
));
1139 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1145 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1147 cstate
->pcap_fddipad
= p
->fddipad
;
1150 * We start out with only one link-layer header.
1152 cstate
->outermostlinktype
= pcap_datalink(p
);
1153 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1154 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1155 cstate
->off_outermostlinkhdr
.reg
= -1;
1157 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1158 cstate
->off_prevlinkhdr
.constant_part
= 0;
1159 cstate
->off_prevlinkhdr
.is_variable
= 0;
1160 cstate
->off_prevlinkhdr
.reg
= -1;
1162 cstate
->linktype
= cstate
->outermostlinktype
;
1163 cstate
->off_linkhdr
.constant_part
= 0;
1164 cstate
->off_linkhdr
.is_variable
= 0;
1165 cstate
->off_linkhdr
.reg
= -1;
1170 cstate
->off_linkpl
.constant_part
= 0;
1171 cstate
->off_linkpl
.is_variable
= 0;
1172 cstate
->off_linkpl
.reg
= -1;
1174 cstate
->off_linktype
.constant_part
= 0;
1175 cstate
->off_linktype
.is_variable
= 0;
1176 cstate
->off_linktype
.reg
= -1;
1179 * Assume it's not raw ATM with a pseudo-header, for now.
1182 cstate
->off_vpi
= OFFSET_NOT_SET
;
1183 cstate
->off_vci
= OFFSET_NOT_SET
;
1184 cstate
->off_proto
= OFFSET_NOT_SET
;
1185 cstate
->off_payload
= OFFSET_NOT_SET
;
1190 cstate
->is_geneve
= 0;
1193 * No variable length VLAN offset by default
1195 cstate
->is_vlan_vloffset
= 0;
1198 * And assume we're not doing SS7.
1200 cstate
->off_li
= OFFSET_NOT_SET
;
1201 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1202 cstate
->off_sio
= OFFSET_NOT_SET
;
1203 cstate
->off_opc
= OFFSET_NOT_SET
;
1204 cstate
->off_dpc
= OFFSET_NOT_SET
;
1205 cstate
->off_sls
= OFFSET_NOT_SET
;
1207 cstate
->label_stack_depth
= 0;
1208 cstate
->vlan_stack_depth
= 0;
1210 switch (cstate
->linktype
) {
1213 cstate
->off_linktype
.constant_part
= 2;
1214 cstate
->off_linkpl
.constant_part
= 6;
1215 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1216 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1219 case DLT_ARCNET_LINUX
:
1220 cstate
->off_linktype
.constant_part
= 4;
1221 cstate
->off_linkpl
.constant_part
= 8;
1222 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1223 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1227 cstate
->off_linktype
.constant_part
= 12;
1228 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1229 cstate
->off_nl
= 0; /* Ethernet II */
1230 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1235 * SLIP doesn't have a link level type. The 16 byte
1236 * header is hacked into our SLIP driver.
1238 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1239 cstate
->off_linkpl
.constant_part
= 16;
1241 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1244 case DLT_SLIP_BSDOS
:
1245 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1246 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1248 cstate
->off_linkpl
.constant_part
= 24;
1250 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1255 cstate
->off_linktype
.constant_part
= 0;
1256 cstate
->off_linkpl
.constant_part
= 4;
1258 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1262 cstate
->off_linktype
.constant_part
= 0;
1263 cstate
->off_linkpl
.constant_part
= 12;
1265 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1270 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1271 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1272 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1273 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1275 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1280 * This does no include the Ethernet header, and
1281 * only covers session state.
1283 cstate
->off_linktype
.constant_part
= 6;
1284 cstate
->off_linkpl
.constant_part
= 8;
1286 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1290 cstate
->off_linktype
.constant_part
= 5;
1291 cstate
->off_linkpl
.constant_part
= 24;
1293 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1298 * FDDI doesn't really have a link-level type field.
1299 * We set "off_linktype" to the offset of the LLC header.
1301 * To check for Ethernet types, we assume that SSAP = SNAP
1302 * is being used and pick out the encapsulated Ethernet type.
1303 * XXX - should we generate code to check for SNAP?
1305 cstate
->off_linktype
.constant_part
= 13;
1306 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1307 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1308 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1309 cstate
->off_nl
= 8; /* 802.2+SNAP */
1310 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1315 * Token Ring doesn't really have a link-level type field.
1316 * We set "off_linktype" to the offset of the LLC header.
1318 * To check for Ethernet types, we assume that SSAP = SNAP
1319 * is being used and pick out the encapsulated Ethernet type.
1320 * XXX - should we generate code to check for SNAP?
1322 * XXX - the header is actually variable-length.
1323 * Some various Linux patched versions gave 38
1324 * as "off_linktype" and 40 as "off_nl"; however,
1325 * if a token ring packet has *no* routing
1326 * information, i.e. is not source-routed, the correct
1327 * values are 20 and 22, as they are in the vanilla code.
1329 * A packet is source-routed iff the uppermost bit
1330 * of the first byte of the source address, at an
1331 * offset of 8, has the uppermost bit set. If the
1332 * packet is source-routed, the total number of bytes
1333 * of routing information is 2 plus bits 0x1F00 of
1334 * the 16-bit value at an offset of 14 (shifted right
1335 * 8 - figure out which byte that is).
1337 cstate
->off_linktype
.constant_part
= 14;
1338 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1339 cstate
->off_nl
= 8; /* 802.2+SNAP */
1340 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1343 case DLT_PRISM_HEADER
:
1344 case DLT_IEEE802_11_RADIO_AVS
:
1345 case DLT_IEEE802_11_RADIO
:
1346 cstate
->off_linkhdr
.is_variable
= 1;
1347 /* Fall through, 802.11 doesn't have a variable link
1348 * prefix but is otherwise the same. */
1351 case DLT_IEEE802_11
:
1353 * 802.11 doesn't really have a link-level type field.
1354 * We set "off_linktype.constant_part" to the offset of
1357 * To check for Ethernet types, we assume that SSAP = SNAP
1358 * is being used and pick out the encapsulated Ethernet type.
1359 * XXX - should we generate code to check for SNAP?
1361 * We also handle variable-length radio headers here.
1362 * The Prism header is in theory variable-length, but in
1363 * practice it's always 144 bytes long. However, some
1364 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1365 * sometimes or always supply an AVS header, so we
1366 * have to check whether the radio header is a Prism
1367 * header or an AVS header, so, in practice, it's
1370 cstate
->off_linktype
.constant_part
= 24;
1371 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1372 cstate
->off_linkpl
.is_variable
= 1;
1373 cstate
->off_nl
= 8; /* 802.2+SNAP */
1374 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1379 * At the moment we treat PPI the same way that we treat
1380 * normal Radiotap encoded packets. The difference is in
1381 * the function that generates the code at the beginning
1382 * to compute the header length. Since this code generator
1383 * of PPI supports bare 802.11 encapsulation only (i.e.
1384 * the encapsulated DLT should be DLT_IEEE802_11) we
1385 * generate code to check for this too.
1387 cstate
->off_linktype
.constant_part
= 24;
1388 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1389 cstate
->off_linkpl
.is_variable
= 1;
1390 cstate
->off_linkhdr
.is_variable
= 1;
1391 cstate
->off_nl
= 8; /* 802.2+SNAP */
1392 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1395 case DLT_ATM_RFC1483
:
1396 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1398 * assume routed, non-ISO PDUs
1399 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1401 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1402 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1403 * latter would presumably be treated the way PPPoE
1404 * should be, so you can do "pppoe and udp port 2049"
1405 * or "pppoa and tcp port 80" and have it check for
1406 * PPPo{A,E} and a PPP protocol of IP and....
1408 cstate
->off_linktype
.constant_part
= 0;
1409 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1410 cstate
->off_nl
= 8; /* 802.2+SNAP */
1411 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1416 * Full Frontal ATM; you get AALn PDUs with an ATM
1420 cstate
->off_vpi
= SUNATM_VPI_POS
;
1421 cstate
->off_vci
= SUNATM_VCI_POS
;
1422 cstate
->off_proto
= PROTO_POS
;
1423 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1424 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1425 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1426 cstate
->off_nl
= 8; /* 802.2+SNAP */
1427 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1433 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1434 cstate
->off_linkpl
.constant_part
= 0;
1436 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1439 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1440 cstate
->off_linktype
.constant_part
= 14;
1441 cstate
->off_linkpl
.constant_part
= 16;
1443 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1446 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1447 cstate
->off_linktype
.constant_part
= 0;
1448 cstate
->off_linkpl
.constant_part
= 20;
1450 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1455 * LocalTalk does have a 1-byte type field in the LLAP header,
1456 * but really it just indicates whether there is a "short" or
1457 * "long" DDP packet following.
1459 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1460 cstate
->off_linkpl
.constant_part
= 0;
1462 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1465 case DLT_IP_OVER_FC
:
1467 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1468 * link-level type field. We set "off_linktype" to the
1469 * offset of the LLC header.
1471 * To check for Ethernet types, we assume that SSAP = SNAP
1472 * is being used and pick out the encapsulated Ethernet type.
1473 * XXX - should we generate code to check for SNAP? RFC
1474 * 2625 says SNAP should be used.
1476 cstate
->off_linktype
.constant_part
= 16;
1477 cstate
->off_linkpl
.constant_part
= 16;
1478 cstate
->off_nl
= 8; /* 802.2+SNAP */
1479 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1484 * XXX - we should set this to handle SNAP-encapsulated
1485 * frames (NLPID of 0x80).
1487 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1488 cstate
->off_linkpl
.constant_part
= 0;
1490 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1494 * the only BPF-interesting FRF.16 frames are non-control frames;
1495 * Frame Relay has a variable length link-layer
1496 * so lets start with offset 4 for now and increments later on (FIXME);
1499 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1500 cstate
->off_linkpl
.constant_part
= 0;
1502 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1505 case DLT_APPLE_IP_OVER_IEEE1394
:
1506 cstate
->off_linktype
.constant_part
= 16;
1507 cstate
->off_linkpl
.constant_part
= 18;
1509 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1512 case DLT_SYMANTEC_FIREWALL
:
1513 cstate
->off_linktype
.constant_part
= 6;
1514 cstate
->off_linkpl
.constant_part
= 44;
1515 cstate
->off_nl
= 0; /* Ethernet II */
1516 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1519 #ifdef HAVE_NET_PFVAR_H
1521 cstate
->off_linktype
.constant_part
= 0;
1522 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1524 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1528 case DLT_JUNIPER_MFR
:
1529 case DLT_JUNIPER_MLFR
:
1530 case DLT_JUNIPER_MLPPP
:
1531 case DLT_JUNIPER_PPP
:
1532 case DLT_JUNIPER_CHDLC
:
1533 case DLT_JUNIPER_FRELAY
:
1534 cstate
->off_linktype
.constant_part
= 4;
1535 cstate
->off_linkpl
.constant_part
= 4;
1537 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1540 case DLT_JUNIPER_ATM1
:
1541 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1542 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1544 cstate
->off_nl_nosnap
= 10;
1547 case DLT_JUNIPER_ATM2
:
1548 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1549 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1551 cstate
->off_nl_nosnap
= 10;
1554 /* frames captured on a Juniper PPPoE service PIC
1555 * contain raw ethernet frames */
1556 case DLT_JUNIPER_PPPOE
:
1557 case DLT_JUNIPER_ETHER
:
1558 cstate
->off_linkpl
.constant_part
= 14;
1559 cstate
->off_linktype
.constant_part
= 16;
1560 cstate
->off_nl
= 18; /* Ethernet II */
1561 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1564 case DLT_JUNIPER_PPPOE_ATM
:
1565 cstate
->off_linktype
.constant_part
= 4;
1566 cstate
->off_linkpl
.constant_part
= 6;
1568 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1571 case DLT_JUNIPER_GGSN
:
1572 cstate
->off_linktype
.constant_part
= 6;
1573 cstate
->off_linkpl
.constant_part
= 12;
1575 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1578 case DLT_JUNIPER_ES
:
1579 cstate
->off_linktype
.constant_part
= 6;
1580 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1581 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1582 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1585 case DLT_JUNIPER_MONITOR
:
1586 cstate
->off_linktype
.constant_part
= 12;
1587 cstate
->off_linkpl
.constant_part
= 12;
1588 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1589 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1592 case DLT_BACNET_MS_TP
:
1593 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1594 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1595 cstate
->off_nl
= OFFSET_NOT_SET
;
1596 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1599 case DLT_JUNIPER_SERVICES
:
1600 cstate
->off_linktype
.constant_part
= 12;
1601 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1602 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1603 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1606 case DLT_JUNIPER_VP
:
1607 cstate
->off_linktype
.constant_part
= 18;
1608 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1609 cstate
->off_nl
= OFFSET_NOT_SET
;
1610 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1613 case DLT_JUNIPER_ST
:
1614 cstate
->off_linktype
.constant_part
= 18;
1615 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1616 cstate
->off_nl
= OFFSET_NOT_SET
;
1617 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1620 case DLT_JUNIPER_ISM
:
1621 cstate
->off_linktype
.constant_part
= 8;
1622 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1623 cstate
->off_nl
= OFFSET_NOT_SET
;
1624 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1627 case DLT_JUNIPER_VS
:
1628 case DLT_JUNIPER_SRX_E2E
:
1629 case DLT_JUNIPER_FIBRECHANNEL
:
1630 case DLT_JUNIPER_ATM_CEMIC
:
1631 cstate
->off_linktype
.constant_part
= 8;
1632 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1633 cstate
->off_nl
= OFFSET_NOT_SET
;
1634 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1639 cstate
->off_li_hsl
= 4;
1640 cstate
->off_sio
= 3;
1641 cstate
->off_opc
= 4;
1642 cstate
->off_dpc
= 4;
1643 cstate
->off_sls
= 7;
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
;
1650 case DLT_MTP2_WITH_PHDR
:
1652 cstate
->off_li_hsl
= 8;
1653 cstate
->off_sio
= 7;
1654 cstate
->off_opc
= 8;
1655 cstate
->off_dpc
= 8;
1656 cstate
->off_sls
= 11;
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_li
= 22;
1665 cstate
->off_li_hsl
= 24;
1666 cstate
->off_sio
= 23;
1667 cstate
->off_opc
= 24;
1668 cstate
->off_dpc
= 24;
1669 cstate
->off_sls
= 27;
1670 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1671 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1672 cstate
->off_nl
= OFFSET_NOT_SET
;
1673 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1677 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1678 cstate
->off_linkpl
.constant_part
= 4;
1680 cstate
->off_nl_nosnap
= 0;
1685 * Currently, only raw "link[N:M]" filtering is supported.
1687 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1688 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1689 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1690 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1694 cstate
->off_linktype
.constant_part
= 1;
1695 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1697 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1700 case DLT_NETANALYZER
:
1701 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1702 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1703 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1704 cstate
->off_nl
= 0; /* Ethernet II */
1705 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1708 case DLT_NETANALYZER_TRANSPARENT
:
1709 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1710 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1711 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1712 cstate
->off_nl
= 0; /* Ethernet II */
1713 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1718 * For values in the range in which we've assigned new
1719 * DLT_ values, only raw "link[N:M]" filtering is supported.
1721 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1722 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1723 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1724 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1725 cstate
->off_nl
= OFFSET_NOT_SET
;
1726 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1728 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1729 cstate
->linktype
, DLT_MATCHING_MIN
, DLT_MATCHING_MAX
);
1735 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1740 * Load a value relative to the specified absolute offset.
1742 static struct slist
*
1743 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1744 u_int offset
, u_int size
)
1746 struct slist
*s
, *s2
;
1748 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1751 * If "s" is non-null, it has code to arrange that the X register
1752 * contains the variable part of the absolute offset, so we
1753 * generate a load relative to that, with an offset of
1754 * abs_offset->constant_part + offset.
1756 * Otherwise, we can do an absolute load with an offset of
1757 * abs_offset->constant_part + offset.
1761 * "s" points to a list of statements that puts the
1762 * variable part of the absolute offset into the X register.
1763 * Do an indirect load, to use the X register as an offset.
1765 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1766 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1770 * There is no variable part of the absolute offset, so
1771 * just do an absolute load.
1773 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1774 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1780 * Load a value relative to the beginning of the specified header.
1782 static struct slist
*
1783 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1786 struct slist
*s
, *s2
;
1789 * Squelch warnings from compilers that *don't* assume that
1790 * offrel always has a valid enum value and therefore don't
1791 * assume that we'll always go through one of the case arms.
1793 * If we have a default case, compilers that *do* assume that
1794 * will then complain about the default case code being
1797 * Damned if you do, damned if you don't.
1804 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1809 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1812 case OR_PREVLINKHDR
:
1813 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1817 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1820 case OR_PREVMPLSHDR
:
1821 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1825 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1828 case OR_LINKPL_NOSNAP
:
1829 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1833 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1838 * Load the X register with the length of the IPv4 header
1839 * (plus the offset of the link-layer header, if it's
1840 * preceded by a variable-length header such as a radio
1841 * header), in bytes.
1843 s
= gen_loadx_iphdrlen(cstate
);
1846 * Load the item at {offset of the link-layer payload} +
1847 * {offset, relative to the start of the link-layer
1848 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1849 * {specified offset}.
1851 * If the offset of the link-layer payload is variable,
1852 * the variable part of that offset is included in the
1853 * value in the X register, and we include the constant
1854 * part in the offset of the load.
1856 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1857 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1862 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1869 * Generate code to load into the X register the sum of the length of
1870 * the IPv4 header and the variable part of the offset of the link-layer
1873 static struct slist
*
1874 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1876 struct slist
*s
, *s2
;
1878 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1881 * The offset of the link-layer payload has a variable
1882 * part. "s" points to a list of statements that put
1883 * the variable part of that offset into the X register.
1885 * The 4*([k]&0xf) addressing mode can't be used, as we
1886 * don't have a constant offset, so we have to load the
1887 * value in question into the A register and add to it
1888 * the value from the X register.
1890 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1891 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1893 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1896 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1901 * The A register now contains the length of the IP header.
1902 * We need to add to it the variable part of the offset of
1903 * the link-layer payload, which is still in the X
1904 * register, and move the result into the X register.
1906 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1907 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1910 * The offset of the link-layer payload is a constant,
1911 * so no code was generated to load the (non-existent)
1912 * variable part of that offset.
1914 * This means we can use the 4*([k]&0xf) addressing
1915 * mode. Load the length of the IPv4 header, which
1916 * is at an offset of cstate->off_nl from the beginning of
1917 * the link-layer payload, and thus at an offset of
1918 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1919 * of the raw packet data, using that addressing mode.
1921 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1922 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1928 static struct block
*
1929 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1934 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1936 b
= new_block(cstate
, JMP(BPF_JEQ
));
1942 static inline struct block
*
1943 gen_true(compiler_state_t
*cstate
)
1945 return gen_uncond(cstate
, 1);
1948 static inline struct block
*
1949 gen_false(compiler_state_t
*cstate
)
1951 return gen_uncond(cstate
, 0);
1955 * Byte-swap a 32-bit number.
1956 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1957 * big-endian platforms.)
1959 #define SWAPLONG(y) \
1960 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1963 * Generate code to match a particular packet type.
1965 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1966 * value, if <= ETHERMTU. We use that to determine whether to
1967 * match the type/length field or to check the type/length field for
1968 * a value <= ETHERMTU to see whether it's a type field and then do
1969 * the appropriate test.
1971 static struct block
*
1972 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1974 struct block
*b0
, *b1
;
1980 case LLCSAP_NETBEUI
:
1982 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1983 * so we check the DSAP and SSAP.
1985 * LLCSAP_IP checks for IP-over-802.2, rather
1986 * than IP-over-Ethernet or IP-over-SNAP.
1988 * XXX - should we check both the DSAP and the
1989 * SSAP, like this, or should we check just the
1990 * DSAP, as we do for other types <= ETHERMTU
1991 * (i.e., other SAP values)?
1993 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1995 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2003 * Ethernet_II frames, which are Ethernet
2004 * frames with a frame type of ETHERTYPE_IPX;
2006 * Ethernet_802.3 frames, which are 802.3
2007 * frames (i.e., the type/length field is
2008 * a length field, <= ETHERMTU, rather than
2009 * a type field) with the first two bytes
2010 * after the Ethernet/802.3 header being
2013 * Ethernet_802.2 frames, which are 802.3
2014 * frames with an 802.2 LLC header and
2015 * with the IPX LSAP as the DSAP in the LLC
2018 * Ethernet_SNAP frames, which are 802.3
2019 * frames with an LLC header and a SNAP
2020 * header and with an OUI of 0x000000
2021 * (encapsulated Ethernet) and a protocol
2022 * ID of ETHERTYPE_IPX in the SNAP header.
2024 * XXX - should we generate the same code both
2025 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2029 * This generates code to check both for the
2030 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2032 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2033 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2037 * Now we add code to check for SNAP frames with
2038 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2040 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2044 * Now we generate code to check for 802.3
2045 * frames in general.
2047 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2051 * Now add the check for 802.3 frames before the
2052 * check for Ethernet_802.2 and Ethernet_802.3,
2053 * as those checks should only be done on 802.3
2054 * frames, not on Ethernet frames.
2059 * Now add the check for Ethernet_II frames, and
2060 * do that before checking for the other frame
2063 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2067 case ETHERTYPE_ATALK
:
2068 case ETHERTYPE_AARP
:
2070 * EtherTalk (AppleTalk protocols on Ethernet link
2071 * layer) may use 802.2 encapsulation.
2075 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2076 * we check for an Ethernet type field less than
2077 * 1500, which means it's an 802.3 length field.
2079 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2083 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2084 * SNAP packets with an organization code of
2085 * 0x080007 (Apple, for Appletalk) and a protocol
2086 * type of ETHERTYPE_ATALK (Appletalk).
2088 * 802.2-encapsulated ETHERTYPE_AARP packets are
2089 * SNAP packets with an organization code of
2090 * 0x000000 (encapsulated Ethernet) and a protocol
2091 * type of ETHERTYPE_AARP (Appletalk ARP).
2093 if (ll_proto
== ETHERTYPE_ATALK
)
2094 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2095 else /* ll_proto == ETHERTYPE_AARP */
2096 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2100 * Check for Ethernet encapsulation (Ethertalk
2101 * phase 1?); we just check for the Ethernet
2104 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2110 if (ll_proto
<= ETHERMTU
) {
2112 * This is an LLC SAP value, so the frames
2113 * that match would be 802.2 frames.
2114 * Check that the frame is an 802.2 frame
2115 * (i.e., that the length/type field is
2116 * a length field, <= ETHERMTU) and
2117 * then check the DSAP.
2119 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2121 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2126 * This is an Ethernet type, so compare
2127 * the length/type field with it (if
2128 * the frame is an 802.2 frame, the length
2129 * field will be <= ETHERMTU, and, as
2130 * "ll_proto" is > ETHERMTU, this test
2131 * will fail and the frame won't match,
2132 * which is what we want).
2134 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2139 static struct block
*
2140 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2143 * For DLT_NULL, the link-layer header is a 32-bit word
2144 * containing an AF_ value in *host* byte order, and for
2145 * DLT_ENC, the link-layer header begins with a 32-bit
2146 * word containing an AF_ value in host byte order.
2148 * In addition, if we're reading a saved capture file,
2149 * the host byte order in the capture may not be the
2150 * same as the host byte order on this machine.
2152 * For DLT_LOOP, the link-layer header is a 32-bit
2153 * word containing an AF_ value in *network* byte order.
2155 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2157 * The AF_ value is in host byte order, but the BPF
2158 * interpreter will convert it to network byte order.
2160 * If this is a save file, and it's from a machine
2161 * with the opposite byte order to ours, we byte-swap
2164 * Then we run it through "htonl()", and generate
2165 * code to compare against the result.
2167 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2168 ll_proto
= SWAPLONG(ll_proto
);
2169 ll_proto
= htonl(ll_proto
);
2171 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2175 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2176 * or IPv6 then we have an error.
2178 static struct block
*
2179 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2184 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2187 case ETHERTYPE_IPV6
:
2188 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2195 return gen_false(cstate
);
2199 * Generate code to match a particular packet type.
2201 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2202 * value, if <= ETHERMTU. We use that to determine whether to
2203 * match the type field or to check the type field for the special
2204 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2206 static struct block
*
2207 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2209 struct block
*b0
, *b1
;
2215 case LLCSAP_NETBEUI
:
2217 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2218 * so we check the DSAP and SSAP.
2220 * LLCSAP_IP checks for IP-over-802.2, rather
2221 * than IP-over-Ethernet or IP-over-SNAP.
2223 * XXX - should we check both the DSAP and the
2224 * SSAP, like this, or should we check just the
2225 * DSAP, as we do for other types <= ETHERMTU
2226 * (i.e., other SAP values)?
2228 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2229 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2235 * Ethernet_II frames, which are Ethernet
2236 * frames with a frame type of ETHERTYPE_IPX;
2238 * Ethernet_802.3 frames, which have a frame
2239 * type of LINUX_SLL_P_802_3;
2241 * Ethernet_802.2 frames, which are 802.3
2242 * frames with an 802.2 LLC header (i.e, have
2243 * a frame type of LINUX_SLL_P_802_2) and
2244 * with the IPX LSAP as the DSAP in the LLC
2247 * Ethernet_SNAP frames, which are 802.3
2248 * frames with an LLC header and a SNAP
2249 * header and with an OUI of 0x000000
2250 * (encapsulated Ethernet) and a protocol
2251 * ID of ETHERTYPE_IPX in the SNAP header.
2253 * First, do the checks on LINUX_SLL_P_802_2
2254 * frames; generate the check for either
2255 * Ethernet_802.2 or Ethernet_SNAP frames, and
2256 * then put a check for LINUX_SLL_P_802_2 frames
2259 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2260 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2262 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2266 * Now check for 802.3 frames and OR that with
2267 * the previous test.
2269 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2273 * Now add the check for Ethernet_II frames, and
2274 * do that before checking for the other frame
2277 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2281 case ETHERTYPE_ATALK
:
2282 case ETHERTYPE_AARP
:
2284 * EtherTalk (AppleTalk protocols on Ethernet link
2285 * layer) may use 802.2 encapsulation.
2289 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2290 * we check for the 802.2 protocol type in the
2291 * "Ethernet type" field.
2293 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2296 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2297 * SNAP packets with an organization code of
2298 * 0x080007 (Apple, for Appletalk) and a protocol
2299 * type of ETHERTYPE_ATALK (Appletalk).
2301 * 802.2-encapsulated ETHERTYPE_AARP packets are
2302 * SNAP packets with an organization code of
2303 * 0x000000 (encapsulated Ethernet) and a protocol
2304 * type of ETHERTYPE_AARP (Appletalk ARP).
2306 if (ll_proto
== ETHERTYPE_ATALK
)
2307 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2308 else /* ll_proto == ETHERTYPE_AARP */
2309 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2313 * Check for Ethernet encapsulation (Ethertalk
2314 * phase 1?); we just check for the Ethernet
2317 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2323 if (ll_proto
<= ETHERMTU
) {
2325 * This is an LLC SAP value, so the frames
2326 * that match would be 802.2 frames.
2327 * Check for the 802.2 protocol type
2328 * in the "Ethernet type" field, and
2329 * then check the DSAP.
2331 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2332 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2338 * This is an Ethernet type, so compare
2339 * the length/type field with it (if
2340 * the frame is an 802.2 frame, the length
2341 * field will be <= ETHERMTU, and, as
2342 * "ll_proto" is > ETHERMTU, this test
2343 * will fail and the frame won't match,
2344 * which is what we want).
2346 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2351 static struct slist
*
2352 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2354 struct slist
*s1
, *s2
;
2355 struct slist
*sjeq_avs_cookie
;
2356 struct slist
*sjcommon
;
2359 * This code is not compatible with the optimizer, as
2360 * we are generating jmp instructions within a normal
2361 * slist of instructions
2363 cstate
->no_optimize
= 1;
2366 * Generate code to load the length of the radio header into
2367 * the register assigned to hold that length, if one has been
2368 * assigned. (If one hasn't been assigned, no code we've
2369 * generated uses that prefix, so we don't need to generate any
2372 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2373 * or always use the AVS header rather than the Prism header.
2374 * We load a 4-byte big-endian value at the beginning of the
2375 * raw packet data, and see whether, when masked with 0xFFFFF000,
2376 * it's equal to 0x80211000. If so, that indicates that it's
2377 * an AVS header (the masked-out bits are the version number).
2378 * Otherwise, it's a Prism header.
2380 * XXX - the Prism header is also, in theory, variable-length,
2381 * but no known software generates headers that aren't 144
2384 if (cstate
->off_linkhdr
.reg
!= -1) {
2388 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2392 * AND it with 0xFFFFF000.
2394 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2395 s2
->s
.k
= 0xFFFFF000;
2399 * Compare with 0x80211000.
2401 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2402 sjeq_avs_cookie
->s
.k
= 0x80211000;
2403 sappend(s1
, sjeq_avs_cookie
);
2408 * The 4 bytes at an offset of 4 from the beginning of
2409 * the AVS header are the length of the AVS header.
2410 * That field is big-endian.
2412 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2415 sjeq_avs_cookie
->s
.jt
= s2
;
2418 * Now jump to the code to allocate a register
2419 * into which to save the header length and
2420 * store the length there. (The "jump always"
2421 * instruction needs to have the k field set;
2422 * it's added to the PC, so, as we're jumping
2423 * over a single instruction, it should be 1.)
2425 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2427 sappend(s1
, sjcommon
);
2430 * Now for the code that handles the Prism header.
2431 * Just load the length of the Prism header (144)
2432 * into the A register. Have the test for an AVS
2433 * header branch here if we don't have an AVS header.
2435 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2438 sjeq_avs_cookie
->s
.jf
= s2
;
2441 * Now allocate a register to hold that value and store
2442 * it. The code for the AVS header will jump here after
2443 * loading the length of the AVS header.
2445 s2
= new_stmt(cstate
, BPF_ST
);
2446 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2448 sjcommon
->s
.jf
= s2
;
2451 * Now move it into the X register.
2453 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2461 static struct slist
*
2462 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2464 struct slist
*s1
, *s2
;
2467 * Generate code to load the length of the AVS header into
2468 * the register assigned to hold that length, if one has been
2469 * assigned. (If one hasn't been assigned, no code we've
2470 * generated uses that prefix, so we don't need to generate any
2473 if (cstate
->off_linkhdr
.reg
!= -1) {
2475 * The 4 bytes at an offset of 4 from the beginning of
2476 * the AVS header are the length of the AVS header.
2477 * That field is big-endian.
2479 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2483 * Now allocate a register to hold that value and store
2486 s2
= new_stmt(cstate
, BPF_ST
);
2487 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2491 * Now move it into the X register.
2493 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2501 static struct slist
*
2502 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2504 struct slist
*s1
, *s2
;
2507 * Generate code to load the length of the radiotap header into
2508 * the register assigned to hold that length, if one has been
2509 * assigned. (If one hasn't been assigned, no code we've
2510 * generated uses that prefix, so we don't need to generate any
2513 if (cstate
->off_linkhdr
.reg
!= -1) {
2515 * The 2 bytes at offsets of 2 and 3 from the beginning
2516 * of the radiotap header are the length of the radiotap
2517 * header; unfortunately, it's little-endian, so we have
2518 * to load it a byte at a time and construct the value.
2522 * Load the high-order byte, at an offset of 3, shift it
2523 * left a byte, and put the result in the X register.
2525 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2527 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2530 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2534 * Load the next byte, at an offset of 2, and OR the
2535 * value from the X register into it.
2537 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2540 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2544 * Now allocate a register to hold that value and store
2547 s2
= new_stmt(cstate
, BPF_ST
);
2548 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2552 * Now move it into the X register.
2554 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2563 * At the moment we treat PPI as normal Radiotap encoded
2564 * packets. The difference is in the function that generates
2565 * the code at the beginning to compute the header length.
2566 * Since this code generator of PPI supports bare 802.11
2567 * encapsulation only (i.e. the encapsulated DLT should be
2568 * DLT_IEEE802_11) we generate code to check for this too;
2569 * that's done in finish_parse().
2571 static struct slist
*
2572 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2574 struct slist
*s1
, *s2
;
2577 * Generate code to load the length of the radiotap header
2578 * into the register assigned to hold that length, if one has
2581 if (cstate
->off_linkhdr
.reg
!= -1) {
2583 * The 2 bytes at offsets of 2 and 3 from the beginning
2584 * of the radiotap header are the length of the radiotap
2585 * header; unfortunately, it's little-endian, so we have
2586 * to load it a byte at a time and construct the value.
2590 * Load the high-order byte, at an offset of 3, shift it
2591 * left a byte, and put the result in the X register.
2593 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2595 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2598 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2602 * Load the next byte, at an offset of 2, and OR the
2603 * value from the X register into it.
2605 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2608 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2612 * Now allocate a register to hold that value and store
2615 s2
= new_stmt(cstate
, BPF_ST
);
2616 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2620 * Now move it into the X register.
2622 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2631 * Load a value relative to the beginning of the link-layer header after the 802.11
2632 * header, i.e. LLC_SNAP.
2633 * The link-layer header doesn't necessarily begin at the beginning
2634 * of the packet data; there might be a variable-length prefix containing
2635 * radio information.
2637 static struct slist
*
2638 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2641 struct slist
*sjset_data_frame_1
;
2642 struct slist
*sjset_data_frame_2
;
2643 struct slist
*sjset_qos
;
2644 struct slist
*sjset_radiotap_flags_present
;
2645 struct slist
*sjset_radiotap_ext_present
;
2646 struct slist
*sjset_radiotap_tsft_present
;
2647 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2648 struct slist
*s_roundup
;
2650 if (cstate
->off_linkpl
.reg
== -1) {
2652 * No register has been assigned to the offset of
2653 * the link-layer payload, which means nobody needs
2654 * it; don't bother computing it - just return
2655 * what we already have.
2661 * This code is not compatible with the optimizer, as
2662 * we are generating jmp instructions within a normal
2663 * slist of instructions
2665 cstate
->no_optimize
= 1;
2668 * If "s" is non-null, it has code to arrange that the X register
2669 * contains the length of the prefix preceding the link-layer
2672 * Otherwise, the length of the prefix preceding the link-layer
2673 * header is "off_outermostlinkhdr.constant_part".
2677 * There is no variable-length header preceding the
2678 * link-layer header.
2680 * Load the length of the fixed-length prefix preceding
2681 * the link-layer header (if any) into the X register,
2682 * and store it in the cstate->off_linkpl.reg register.
2683 * That length is off_outermostlinkhdr.constant_part.
2685 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2686 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2690 * The X register contains the offset of the beginning of the
2691 * link-layer header; add 24, which is the minimum length
2692 * of the MAC header for a data frame, to that, and store it
2693 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2694 * which is at the offset in the X register, with an indexed load.
2696 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2698 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2701 s2
= new_stmt(cstate
, BPF_ST
);
2702 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2705 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2710 * Check the Frame Control field to see if this is a data frame;
2711 * a data frame has the 0x08 bit (b3) in that field set and the
2712 * 0x04 bit (b2) clear.
2714 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2715 sjset_data_frame_1
->s
.k
= 0x08;
2716 sappend(s
, sjset_data_frame_1
);
2719 * If b3 is set, test b2, otherwise go to the first statement of
2720 * the rest of the program.
2722 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2723 sjset_data_frame_2
->s
.k
= 0x04;
2724 sappend(s
, sjset_data_frame_2
);
2725 sjset_data_frame_1
->s
.jf
= snext
;
2728 * If b2 is not set, this is a data frame; test the QoS bit.
2729 * Otherwise, go to the first statement of the rest of the
2732 sjset_data_frame_2
->s
.jt
= snext
;
2733 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2734 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2735 sappend(s
, sjset_qos
);
2738 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2740 * Otherwise, go to the first statement of the rest of the
2743 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2744 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2746 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2749 s2
= new_stmt(cstate
, BPF_ST
);
2750 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2754 * If we have a radiotap header, look at it to see whether
2755 * there's Atheros padding between the MAC-layer header
2758 * Note: all of the fields in the radiotap header are
2759 * little-endian, so we byte-swap all of the values
2760 * we test against, as they will be loaded as big-endian
2763 * XXX - in the general case, we would have to scan through
2764 * *all* the presence bits, if there's more than one word of
2765 * presence bits. That would require a loop, meaning that
2766 * we wouldn't be able to run the filter in the kernel.
2768 * We assume here that the Atheros adapters that insert the
2769 * annoying padding don't have multiple antennae and therefore
2770 * do not generate radiotap headers with multiple presence words.
2772 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2774 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2775 * in the first presence flag word?
2777 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2781 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2782 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2783 sappend(s
, sjset_radiotap_flags_present
);
2786 * If not, skip all of this.
2788 sjset_radiotap_flags_present
->s
.jf
= snext
;
2791 * Otherwise, is the "extension" bit set in that word?
2793 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2794 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2795 sappend(s
, sjset_radiotap_ext_present
);
2796 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2799 * If so, skip all of this.
2801 sjset_radiotap_ext_present
->s
.jt
= snext
;
2804 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2806 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2807 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2808 sappend(s
, sjset_radiotap_tsft_present
);
2809 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2812 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2813 * at an offset of 16 from the beginning of the raw packet
2814 * data (8 bytes for the radiotap header and 8 bytes for
2817 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2820 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2823 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2825 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2826 sjset_tsft_datapad
->s
.k
= 0x20;
2827 sappend(s
, sjset_tsft_datapad
);
2830 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2831 * at an offset of 8 from the beginning of the raw packet
2832 * data (8 bytes for the radiotap header).
2834 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2837 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2840 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2842 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2843 sjset_notsft_datapad
->s
.k
= 0x20;
2844 sappend(s
, sjset_notsft_datapad
);
2847 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2848 * set, round the length of the 802.11 header to
2849 * a multiple of 4. Do that by adding 3 and then
2850 * dividing by and multiplying by 4, which we do by
2853 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2854 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2855 sappend(s
, s_roundup
);
2856 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2859 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2860 s2
->s
.k
= (bpf_u_int32
)~3;
2862 s2
= new_stmt(cstate
, BPF_ST
);
2863 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2866 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2867 sjset_tsft_datapad
->s
.jf
= snext
;
2868 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2869 sjset_notsft_datapad
->s
.jf
= snext
;
2871 sjset_qos
->s
.jf
= snext
;
2877 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2881 /* There is an implicit dependency between the link
2882 * payload and link header since the payload computation
2883 * includes the variable part of the header. Therefore,
2884 * if nobody else has allocated a register for the link
2885 * header and we need it, do it now. */
2886 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2887 cstate
->off_linkhdr
.reg
== -1)
2888 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2891 * For link-layer types that have a variable-length header
2892 * preceding the link-layer header, generate code to load
2893 * the offset of the link-layer header into the register
2894 * assigned to that offset, if any.
2896 * XXX - this, and the next switch statement, won't handle
2897 * encapsulation of 802.11 or 802.11+radio information in
2898 * some other protocol stack. That's significantly more
2901 switch (cstate
->outermostlinktype
) {
2903 case DLT_PRISM_HEADER
:
2904 s
= gen_load_prism_llprefixlen(cstate
);
2907 case DLT_IEEE802_11_RADIO_AVS
:
2908 s
= gen_load_avs_llprefixlen(cstate
);
2911 case DLT_IEEE802_11_RADIO
:
2912 s
= gen_load_radiotap_llprefixlen(cstate
);
2916 s
= gen_load_ppi_llprefixlen(cstate
);
2925 * For link-layer types that have a variable-length link-layer
2926 * header, generate code to load the offset of the link-layer
2927 * payload into the register assigned to that offset, if any.
2929 switch (cstate
->outermostlinktype
) {
2931 case DLT_IEEE802_11
:
2932 case DLT_PRISM_HEADER
:
2933 case DLT_IEEE802_11_RADIO_AVS
:
2934 case DLT_IEEE802_11_RADIO
:
2936 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2941 * If there is no initialization yet and we need variable
2942 * length offsets for VLAN, initialize them to zero
2944 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2947 if (cstate
->off_linkpl
.reg
== -1)
2948 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2949 if (cstate
->off_linktype
.reg
== -1)
2950 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2952 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2954 s2
= new_stmt(cstate
, BPF_ST
);
2955 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2957 s2
= new_stmt(cstate
, BPF_ST
);
2958 s2
->s
.k
= cstate
->off_linktype
.reg
;
2963 * If we have any offset-loading code, append all the
2964 * existing statements in the block to those statements,
2965 * and make the resulting list the list of statements
2969 sappend(s
, b
->stmts
);
2974 static struct block
*
2975 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2977 struct slist
*s_load_dlt
;
2980 if (cstate
->linktype
== DLT_PPI
)
2982 /* Create the statements that check for the DLT
2984 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2985 s_load_dlt
->s
.k
= 4;
2987 b
= new_block(cstate
, JMP(BPF_JEQ
));
2989 b
->stmts
= s_load_dlt
;
2990 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3001 * Take an absolute offset, and:
3003 * if it has no variable part, return NULL;
3005 * if it has a variable part, generate code to load the register
3006 * containing that variable part into the X register, returning
3007 * a pointer to that code - if no register for that offset has
3008 * been allocated, allocate it first.
3010 * (The code to set that register will be generated later, but will
3011 * be placed earlier in the code sequence.)
3013 static struct slist
*
3014 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3018 if (off
->is_variable
) {
3019 if (off
->reg
== -1) {
3021 * We haven't yet assigned a register for the
3022 * variable part of the offset of the link-layer
3023 * header; allocate one.
3025 off
->reg
= alloc_reg(cstate
);
3029 * Load the register containing the variable part of the
3030 * offset of the link-layer header into the X register.
3032 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3037 * That offset isn't variable, there's no variable part,
3038 * so we don't need to generate any code.
3045 * Map an Ethernet type to the equivalent PPP type.
3048 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3056 case ETHERTYPE_IPV6
:
3057 ll_proto
= PPP_IPV6
;
3061 ll_proto
= PPP_DECNET
;
3064 case ETHERTYPE_ATALK
:
3065 ll_proto
= PPP_APPLE
;
3078 * I'm assuming the "Bridging PDU"s that go
3079 * over PPP are Spanning Tree Protocol
3082 ll_proto
= PPP_BRPDU
;
3093 * Generate any tests that, for encapsulation of a link-layer packet
3094 * inside another protocol stack, need to be done to check for those
3095 * link-layer packets (and that haven't already been done by a check
3096 * for that encapsulation).
3098 static struct block
*
3099 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3103 if (cstate
->is_geneve
)
3104 return gen_geneve_ll_check(cstate
);
3106 switch (cstate
->prevlinktype
) {
3110 * This is LANE-encapsulated Ethernet; check that the LANE
3111 * packet doesn't begin with an LE Control marker, i.e.
3112 * that it's data, not a control message.
3114 * (We've already generated a test for LANE.)
3116 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3122 * No such tests are necessary.
3130 * The three different values we should check for when checking for an
3131 * IPv6 packet with DLT_NULL.
3133 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3134 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3135 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3138 * Generate code to match a particular packet type by matching the
3139 * link-layer type field or fields in the 802.2 LLC header.
3141 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3142 * value, if <= ETHERMTU.
3144 static struct block
*
3145 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3147 struct block
*b0
, *b1
, *b2
;
3148 const char *description
;
3150 /* are we checking MPLS-encapsulated packets? */
3151 if (cstate
->label_stack_depth
> 0)
3152 return gen_mpls_linktype(cstate
, ll_proto
);
3154 switch (cstate
->linktype
) {
3157 case DLT_NETANALYZER
:
3158 case DLT_NETANALYZER_TRANSPARENT
:
3159 /* Geneve has an EtherType regardless of whether there is an
3161 if (!cstate
->is_geneve
)
3162 b0
= gen_prevlinkhdr_check(cstate
);
3166 b1
= gen_ether_linktype(cstate
, ll_proto
);
3176 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3180 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3184 case DLT_IEEE802_11
:
3185 case DLT_PRISM_HEADER
:
3186 case DLT_IEEE802_11_RADIO_AVS
:
3187 case DLT_IEEE802_11_RADIO
:
3190 * Check that we have a data frame.
3192 b0
= gen_check_802_11_data_frame(cstate
);
3195 * Now check for the specified link-layer type.
3197 b1
= gen_llc_linktype(cstate
, ll_proto
);
3204 * XXX - check for LLC frames.
3206 return gen_llc_linktype(cstate
, ll_proto
);
3211 * XXX - check for LLC PDUs, as per IEEE 802.5.
3213 return gen_llc_linktype(cstate
, ll_proto
);
3216 case DLT_ATM_RFC1483
:
3218 case DLT_IP_OVER_FC
:
3219 return gen_llc_linktype(cstate
, ll_proto
);
3224 * Check for an LLC-encapsulated version of this protocol;
3225 * if we were checking for LANE, linktype would no longer
3228 * Check for LLC encapsulation and then check the protocol.
3230 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3231 b1
= gen_llc_linktype(cstate
, ll_proto
);
3237 return gen_linux_sll_linktype(cstate
, ll_proto
);
3241 case DLT_SLIP_BSDOS
:
3244 * These types don't provide any type field; packets
3245 * are always IPv4 or IPv6.
3247 * XXX - for IPv4, check for a version number of 4, and,
3248 * for IPv6, check for a version number of 6?
3253 /* Check for a version number of 4. */
3254 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3256 case ETHERTYPE_IPV6
:
3257 /* Check for a version number of 6. */
3258 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3261 return gen_false(cstate
); /* always false */
3267 * Raw IPv4, so no type field.
3269 if (ll_proto
== ETHERTYPE_IP
)
3270 return gen_true(cstate
); /* always true */
3272 /* Checking for something other than IPv4; always false */
3273 return gen_false(cstate
);
3278 * Raw IPv6, so no type field.
3280 if (ll_proto
== ETHERTYPE_IPV6
)
3281 return gen_true(cstate
); /* always true */
3283 /* Checking for something other than IPv6; always false */
3284 return gen_false(cstate
);
3289 case DLT_PPP_SERIAL
:
3292 * We use Ethernet protocol types inside libpcap;
3293 * map them to the corresponding PPP protocol types.
3295 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3296 ethertype_to_ppptype(ll_proto
));
3301 * We use Ethernet protocol types inside libpcap;
3302 * map them to the corresponding PPP protocol types.
3308 * Also check for Van Jacobson-compressed IP.
3309 * XXX - do this for other forms of PPP?
3311 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3312 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3314 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3319 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3320 ethertype_to_ppptype(ll_proto
));
3330 return (gen_loopback_linktype(cstate
, AF_INET
));
3332 case ETHERTYPE_IPV6
:
3334 * AF_ values may, unfortunately, be platform-
3335 * dependent; AF_INET isn't, because everybody
3336 * used 4.2BSD's value, but AF_INET6 is, because
3337 * 4.2BSD didn't have a value for it (given that
3338 * IPv6 didn't exist back in the early 1980's),
3339 * and they all picked their own values.
3341 * This means that, if we're reading from a
3342 * savefile, we need to check for all the
3345 * If we're doing a live capture, we only need
3346 * to check for this platform's value; however,
3347 * Npcap uses 24, which isn't Windows's AF_INET6
3348 * value. (Given the multiple different values,
3349 * programs that read pcap files shouldn't be
3350 * checking for their platform's AF_INET6 value
3351 * anyway, they should check for all of the
3352 * possible values. and they might as well do
3353 * that even for live captures.)
3355 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3357 * Savefile - check for all three
3358 * possible IPv6 values.
3360 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3361 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3363 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3368 * Live capture, so we only need to
3369 * check for the value used on this
3374 * Npcap doesn't use Windows's AF_INET6,
3375 * as that collides with AF_IPX on
3376 * some BSDs (both have the value 23).
3377 * Instead, it uses 24.
3379 return (gen_loopback_linktype(cstate
, 24));
3382 return (gen_loopback_linktype(cstate
, AF_INET6
));
3383 #else /* AF_INET6 */
3385 * I guess this platform doesn't support
3386 * IPv6, so we just reject all packets.
3388 return gen_false(cstate
);
3389 #endif /* AF_INET6 */
3395 * Not a type on which we support filtering.
3396 * XXX - support those that have AF_ values
3397 * #defined on this platform, at least?
3399 return gen_false(cstate
);
3402 #ifdef HAVE_NET_PFVAR_H
3405 * af field is host byte order in contrast to the rest of
3408 if (ll_proto
== ETHERTYPE_IP
)
3409 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3411 else if (ll_proto
== ETHERTYPE_IPV6
)
3412 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3415 return gen_false(cstate
);
3417 #endif /* HAVE_NET_PFVAR_H */
3420 case DLT_ARCNET_LINUX
:
3422 * XXX should we check for first fragment if the protocol
3428 return gen_false(cstate
);
3430 case ETHERTYPE_IPV6
:
3431 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3435 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3437 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3443 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3445 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3450 case ETHERTYPE_REVARP
:
3451 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3454 case ETHERTYPE_ATALK
:
3455 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3462 case ETHERTYPE_ATALK
:
3463 return gen_true(cstate
);
3465 return gen_false(cstate
);
3471 * XXX - assumes a 2-byte Frame Relay header with
3472 * DLCI and flags. What if the address is longer?
3478 * Check for the special NLPID for IP.
3480 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3482 case ETHERTYPE_IPV6
:
3484 * Check for the special NLPID for IPv6.
3486 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3490 * Check for several OSI protocols.
3492 * Frame Relay packets typically have an OSI
3493 * NLPID at the beginning; we check for each
3496 * What we check for is the NLPID and a frame
3497 * control field of UI, i.e. 0x03 followed
3500 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3501 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3502 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3508 return gen_false(cstate
);
3513 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3515 case DLT_JUNIPER_MFR
:
3516 case DLT_JUNIPER_MLFR
:
3517 case DLT_JUNIPER_MLPPP
:
3518 case DLT_JUNIPER_ATM1
:
3519 case DLT_JUNIPER_ATM2
:
3520 case DLT_JUNIPER_PPPOE
:
3521 case DLT_JUNIPER_PPPOE_ATM
:
3522 case DLT_JUNIPER_GGSN
:
3523 case DLT_JUNIPER_ES
:
3524 case DLT_JUNIPER_MONITOR
:
3525 case DLT_JUNIPER_SERVICES
:
3526 case DLT_JUNIPER_ETHER
:
3527 case DLT_JUNIPER_PPP
:
3528 case DLT_JUNIPER_FRELAY
:
3529 case DLT_JUNIPER_CHDLC
:
3530 case DLT_JUNIPER_VP
:
3531 case DLT_JUNIPER_ST
:
3532 case DLT_JUNIPER_ISM
:
3533 case DLT_JUNIPER_VS
:
3534 case DLT_JUNIPER_SRX_E2E
:
3535 case DLT_JUNIPER_FIBRECHANNEL
:
3536 case DLT_JUNIPER_ATM_CEMIC
:
3538 /* just lets verify the magic number for now -
3539 * on ATM we may have up to 6 different encapsulations on the wire
3540 * and need a lot of heuristics to figure out that the payload
3543 * FIXME encapsulation specific BPF_ filters
3545 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3547 case DLT_BACNET_MS_TP
:
3548 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3551 return gen_ipnet_linktype(cstate
, ll_proto
);
3553 case DLT_LINUX_IRDA
:
3554 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3557 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3560 case DLT_MTP2_WITH_PHDR
:
3561 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3564 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3567 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3569 case DLT_LINUX_LAPD
:
3570 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3572 case DLT_USB_FREEBSD
:
3574 case DLT_USB_LINUX_MMAPPED
:
3576 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3578 case DLT_BLUETOOTH_HCI_H4
:
3579 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3580 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3583 case DLT_CAN_SOCKETCAN
:
3584 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3586 case DLT_IEEE802_15_4
:
3587 case DLT_IEEE802_15_4_LINUX
:
3588 case DLT_IEEE802_15_4_NONASK_PHY
:
3589 case DLT_IEEE802_15_4_NOFCS
:
3590 case DLT_IEEE802_15_4_TAP
:
3591 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3593 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3594 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3597 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3600 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3602 case DLT_IPMB_KONTRON
:
3603 case DLT_IPMB_LINUX
:
3604 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3607 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3610 /* Using the fixed-size NFLOG header it is possible to tell only
3611 * the address family of the packet, other meaningful data is
3612 * either missing or behind TLVs.
3614 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3618 * Does this link-layer header type have a field
3619 * indicating the type of the next protocol? If
3620 * so, off_linktype.constant_part will be the offset of that
3621 * field in the packet; if not, it will be OFFSET_NOT_SET.
3623 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3625 * Yes; assume it's an Ethernet type. (If
3626 * it's not, it needs to be handled specially
3629 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3633 * No; report an error.
3635 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3636 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3644 * Check for an LLC SNAP packet with a given organization code and
3645 * protocol type; we check the entire contents of the 802.2 LLC and
3646 * snap headers, checking for DSAP and SSAP of SNAP and a control
3647 * field of 0x03 in the LLC header, and for the specified organization
3648 * code and protocol type in the SNAP header.
3650 static struct block
*
3651 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3653 u_char snapblock
[8];
3655 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3656 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3657 snapblock
[2] = 0x03; /* control = UI */
3658 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3659 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3660 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3661 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3662 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3663 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3667 * Generate code to match frames with an LLC header.
3669 static struct block
*
3670 gen_llc_internal(compiler_state_t
*cstate
)
3672 struct block
*b0
, *b1
;
3674 switch (cstate
->linktype
) {
3678 * We check for an Ethernet type field less than
3679 * 1500, which means it's an 802.3 length field.
3681 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3685 * Now check for the purported DSAP and SSAP not being
3686 * 0xFF, to rule out NetWare-over-802.3.
3688 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3695 * We check for LLC traffic.
3697 b0
= gen_atmtype_llc(cstate
);
3700 case DLT_IEEE802
: /* Token Ring */
3702 * XXX - check for LLC frames.
3704 return gen_true(cstate
);
3708 * XXX - check for LLC frames.
3710 return gen_true(cstate
);
3712 case DLT_ATM_RFC1483
:
3714 * For LLC encapsulation, these are defined to have an
3717 * For VC encapsulation, they don't, but there's no
3718 * way to check for that; the protocol used on the VC
3719 * is negotiated out of band.
3721 return gen_true(cstate
);
3723 case DLT_IEEE802_11
:
3724 case DLT_PRISM_HEADER
:
3725 case DLT_IEEE802_11_RADIO
:
3726 case DLT_IEEE802_11_RADIO_AVS
:
3729 * Check that we have a data frame.
3731 b0
= gen_check_802_11_data_frame(cstate
);
3735 bpf_error(cstate
, "'llc' not supported for %s",
3736 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3742 gen_llc(compiler_state_t
*cstate
)
3745 * Catch errors reported by us and routines below us, and return NULL
3748 if (setjmp(cstate
->top_ctx
))
3751 return gen_llc_internal(cstate
);
3755 gen_llc_i(compiler_state_t
*cstate
)
3757 struct block
*b0
, *b1
;
3761 * Catch errors reported by us and routines below us, and return NULL
3764 if (setjmp(cstate
->top_ctx
))
3768 * Check whether this is an LLC frame.
3770 b0
= gen_llc_internal(cstate
);
3773 * Load the control byte and test the low-order bit; it must
3774 * be clear for I frames.
3776 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3777 b1
= new_block(cstate
, JMP(BPF_JSET
));
3786 gen_llc_s(compiler_state_t
*cstate
)
3788 struct block
*b0
, *b1
;
3791 * Catch errors reported by us and routines below us, and return NULL
3794 if (setjmp(cstate
->top_ctx
))
3798 * Check whether this is an LLC frame.
3800 b0
= gen_llc_internal(cstate
);
3803 * Now compare the low-order 2 bit of the control byte against
3804 * the appropriate value for S frames.
3806 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3812 gen_llc_u(compiler_state_t
*cstate
)
3814 struct block
*b0
, *b1
;
3817 * Catch errors reported by us and routines below us, and return NULL
3820 if (setjmp(cstate
->top_ctx
))
3824 * Check whether this is an LLC frame.
3826 b0
= gen_llc_internal(cstate
);
3829 * Now compare the low-order 2 bit of the control byte against
3830 * the appropriate value for U frames.
3832 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3838 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3840 struct block
*b0
, *b1
;
3843 * Catch errors reported by us and routines below us, and return NULL
3846 if (setjmp(cstate
->top_ctx
))
3850 * Check whether this is an LLC frame.
3852 b0
= gen_llc_internal(cstate
);
3855 * Now check for an S frame with the appropriate type.
3857 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3863 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3865 struct block
*b0
, *b1
;
3868 * Catch errors reported by us and routines below us, and return NULL
3871 if (setjmp(cstate
->top_ctx
))
3875 * Check whether this is an LLC frame.
3877 b0
= gen_llc_internal(cstate
);
3880 * Now check for a U frame with the appropriate type.
3882 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3888 * Generate code to match a particular packet type, for link-layer types
3889 * using 802.2 LLC headers.
3891 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3892 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3894 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3895 * value, if <= ETHERMTU. We use that to determine whether to
3896 * match the DSAP or both DSAP and LSAP or to check the OUI and
3897 * protocol ID in a SNAP header.
3899 static struct block
*
3900 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3903 * XXX - handle token-ring variable-length header.
3909 case LLCSAP_NETBEUI
:
3911 * XXX - should we check both the DSAP and the
3912 * SSAP, like this, or should we check just the
3913 * DSAP, as we do for other SAP values?
3915 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3916 ((ll_proto
<< 8) | ll_proto
));
3920 * XXX - are there ever SNAP frames for IPX on
3921 * non-Ethernet 802.x networks?
3923 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3925 case ETHERTYPE_ATALK
:
3927 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3928 * SNAP packets with an organization code of
3929 * 0x080007 (Apple, for Appletalk) and a protocol
3930 * type of ETHERTYPE_ATALK (Appletalk).
3932 * XXX - check for an organization code of
3933 * encapsulated Ethernet as well?
3935 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3939 * XXX - we don't have to check for IPX 802.3
3940 * here, but should we check for the IPX Ethertype?
3942 if (ll_proto
<= ETHERMTU
) {
3944 * This is an LLC SAP value, so check
3947 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
3950 * This is an Ethernet type; we assume that it's
3951 * unlikely that it'll appear in the right place
3952 * at random, and therefore check only the
3953 * location that would hold the Ethernet type
3954 * in a SNAP frame with an organization code of
3955 * 0x000000 (encapsulated Ethernet).
3957 * XXX - if we were to check for the SNAP DSAP and
3958 * LSAP, as per XXX, and were also to check for an
3959 * organization code of 0x000000 (encapsulated
3960 * Ethernet), we'd do
3962 * return gen_snap(cstate, 0x000000, ll_proto);
3964 * here; for now, we don't, as per the above.
3965 * I don't know whether it's worth the extra CPU
3966 * time to do the right check or not.
3968 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
3973 static struct block
*
3974 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3975 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
3977 struct block
*b0
, *b1
;
3991 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
3992 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
3998 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
3999 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4004 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4008 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4012 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4016 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4020 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4024 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4031 b0
= gen_linktype(cstate
, ll_proto
);
4032 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4038 static struct block
*
4039 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4040 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4043 struct block
*b0
, *b1
;
4058 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4059 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4065 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4066 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4071 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4075 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4079 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4083 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4087 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4091 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4098 /* this order is important */
4099 a
= (uint32_t *)addr
;
4100 m
= (uint32_t *)mask
;
4101 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4102 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4104 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4106 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4108 b0
= gen_linktype(cstate
, ll_proto
);
4114 static struct block
*
4115 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4117 register struct block
*b0
, *b1
;
4121 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4124 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4127 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4128 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4134 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4135 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4140 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4144 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4148 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4152 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4156 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4160 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4168 * Like gen_ehostop, but for DLT_FDDI
4170 static struct block
*
4171 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4173 struct block
*b0
, *b1
;
4177 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4180 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4183 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4184 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4190 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4191 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4196 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4200 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4204 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4208 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4212 bpf_error(cstate
, "'ra' is only supported on 802.11");
4216 bpf_error(cstate
, "'ta' is only supported on 802.11");
4224 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4226 static struct block
*
4227 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4229 register struct block
*b0
, *b1
;
4233 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4236 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4239 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4240 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4246 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4247 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4252 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4256 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4260 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4264 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4268 bpf_error(cstate
, "'ra' is only supported on 802.11");
4272 bpf_error(cstate
, "'ta' is only supported on 802.11");
4280 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4281 * various 802.11 + radio headers.
4283 static struct block
*
4284 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4286 register struct block
*b0
, *b1
, *b2
;
4287 register struct slist
*s
;
4289 #ifdef ENABLE_WLAN_FILTERING_PATCH
4292 * We need to disable the optimizer because the optimizer is buggy
4293 * and wipes out some LD instructions generated by the below
4294 * code to validate the Frame Control bits
4296 cstate
->no_optimize
= 1;
4297 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4304 * For control frames, there is no SA.
4306 * For management frames, SA is at an
4307 * offset of 10 from the beginning of
4310 * For data frames, SA is at an offset
4311 * of 10 from the beginning of the packet
4312 * if From DS is clear, at an offset of
4313 * 16 from the beginning of the packet
4314 * if From DS is set and To DS is clear,
4315 * and an offset of 24 from the beginning
4316 * of the packet if From DS is set and To DS
4321 * Generate the tests to be done for data frames
4324 * First, check for To DS set, i.e. check "link[1] & 0x01".
4326 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4327 b1
= new_block(cstate
, JMP(BPF_JSET
));
4328 b1
->s
.k
= 0x01; /* To DS */
4332 * If To DS is set, the SA is at 24.
4334 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4338 * Now, check for To DS not set, i.e. check
4339 * "!(link[1] & 0x01)".
4341 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4342 b2
= new_block(cstate
, JMP(BPF_JSET
));
4343 b2
->s
.k
= 0x01; /* To DS */
4348 * If To DS is not set, the SA is at 16.
4350 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4354 * Now OR together the last two checks. That gives
4355 * the complete set of checks for data frames with
4361 * Now check for From DS being set, and AND that with
4362 * the ORed-together checks.
4364 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4365 b1
= new_block(cstate
, JMP(BPF_JSET
));
4366 b1
->s
.k
= 0x02; /* From DS */
4371 * Now check for data frames with From DS not set.
4373 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4374 b2
= new_block(cstate
, JMP(BPF_JSET
));
4375 b2
->s
.k
= 0x02; /* From DS */
4380 * If From DS isn't set, the SA is at 10.
4382 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4386 * Now OR together the checks for data frames with
4387 * From DS not set and for data frames with From DS
4388 * set; that gives the checks done for data frames.
4393 * Now check for a data frame.
4394 * I.e, check "link[0] & 0x08".
4396 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4397 b1
= new_block(cstate
, JMP(BPF_JSET
));
4402 * AND that with the checks done for data frames.
4407 * If the high-order bit of the type value is 0, this
4408 * is a management frame.
4409 * I.e, check "!(link[0] & 0x08)".
4411 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4412 b2
= new_block(cstate
, JMP(BPF_JSET
));
4418 * For management frames, the SA is at 10.
4420 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4424 * OR that with the checks done for data frames.
4425 * That gives the checks done for management and
4431 * If the low-order bit of the type value is 1,
4432 * this is either a control frame or a frame
4433 * with a reserved type, and thus not a
4436 * I.e., check "!(link[0] & 0x04)".
4438 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4439 b1
= new_block(cstate
, JMP(BPF_JSET
));
4445 * AND that with the checks for data and management
4455 * For control frames, there is no DA.
4457 * For management frames, DA is at an
4458 * offset of 4 from the beginning of
4461 * For data frames, DA is at an offset
4462 * of 4 from the beginning of the packet
4463 * if To DS is clear and at an offset of
4464 * 16 from the beginning of the packet
4469 * Generate the tests to be done for data frames.
4471 * First, check for To DS set, i.e. "link[1] & 0x01".
4473 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4474 b1
= new_block(cstate
, JMP(BPF_JSET
));
4475 b1
->s
.k
= 0x01; /* To DS */
4479 * If To DS is set, the DA is at 16.
4481 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4485 * Now, check for To DS not set, i.e. check
4486 * "!(link[1] & 0x01)".
4488 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4489 b2
= new_block(cstate
, JMP(BPF_JSET
));
4490 b2
->s
.k
= 0x01; /* To DS */
4495 * If To DS is not set, the DA is at 4.
4497 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4501 * Now OR together the last two checks. That gives
4502 * the complete set of checks for data frames.
4507 * Now check for a data frame.
4508 * I.e, check "link[0] & 0x08".
4510 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4511 b1
= new_block(cstate
, JMP(BPF_JSET
));
4516 * AND that with the checks done for data frames.
4521 * If the high-order bit of the type value is 0, this
4522 * is a management frame.
4523 * I.e, check "!(link[0] & 0x08)".
4525 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4526 b2
= new_block(cstate
, JMP(BPF_JSET
));
4532 * For management frames, the DA is at 4.
4534 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4538 * OR that with the checks done for data frames.
4539 * That gives the checks done for management and
4545 * If the low-order bit of the type value is 1,
4546 * this is either a control frame or a frame
4547 * with a reserved type, and thus not a
4550 * I.e., check "!(link[0] & 0x04)".
4552 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4553 b1
= new_block(cstate
, JMP(BPF_JSET
));
4559 * AND that with the checks for data and management
4566 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4567 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4573 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4574 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4579 * XXX - add BSSID keyword?
4582 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4586 * Not present in CTS or ACK control frames.
4588 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4589 IEEE80211_FC0_TYPE_MASK
);
4591 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4592 IEEE80211_FC0_SUBTYPE_MASK
);
4594 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4595 IEEE80211_FC0_SUBTYPE_MASK
);
4599 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4605 * Not present in control frames.
4607 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4608 IEEE80211_FC0_TYPE_MASK
);
4610 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4616 * Present only if the direction mask has both "From DS"
4617 * and "To DS" set. Neither control frames nor management
4618 * frames should have both of those set, so we don't
4619 * check the frame type.
4621 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4622 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4623 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4629 * Not present in management frames; addr1 in other
4634 * If the high-order bit of the type value is 0, this
4635 * is a management frame.
4636 * I.e, check "(link[0] & 0x08)".
4638 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4639 b1
= new_block(cstate
, JMP(BPF_JSET
));
4646 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4649 * AND that with the check of addr1.
4656 * Not present in management frames; addr2, if present,
4661 * Not present in CTS or ACK control frames.
4663 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4664 IEEE80211_FC0_TYPE_MASK
);
4666 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4667 IEEE80211_FC0_SUBTYPE_MASK
);
4669 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4670 IEEE80211_FC0_SUBTYPE_MASK
);
4676 * If the high-order bit of the type value is 0, this
4677 * is a management frame.
4678 * I.e, check "(link[0] & 0x08)".
4680 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4681 b1
= new_block(cstate
, JMP(BPF_JSET
));
4686 * AND that with the check for frames other than
4687 * CTS and ACK frames.
4694 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4703 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4704 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4705 * as the RFC states.)
4707 static struct block
*
4708 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4710 register struct block
*b0
, *b1
;
4714 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4717 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4720 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4721 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4727 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4728 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4733 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4737 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4741 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4745 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4749 bpf_error(cstate
, "'ra' is only supported on 802.11");
4753 bpf_error(cstate
, "'ta' is only supported on 802.11");
4761 * This is quite tricky because there may be pad bytes in front of the
4762 * DECNET header, and then there are two possible data packet formats that
4763 * carry both src and dst addresses, plus 5 packet types in a format that
4764 * carries only the src node, plus 2 types that use a different format and
4765 * also carry just the src node.
4769 * Instead of doing those all right, we just look for data packets with
4770 * 0 or 1 bytes of padding. If you want to look at other packets, that
4771 * will require a lot more hacking.
4773 * To add support for filtering on DECNET "areas" (network numbers)
4774 * one would want to add a "mask" argument to this routine. That would
4775 * make the filter even more inefficient, although one could be clever
4776 * and not generate masking instructions if the mask is 0xFFFF.
4778 static struct block
*
4779 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4781 struct block
*b0
, *b1
, *b2
, *tmp
;
4782 u_int offset_lh
; /* offset if long header is received */
4783 u_int offset_sh
; /* offset if short header is received */
4788 offset_sh
= 1; /* follows flags */
4789 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4793 offset_sh
= 3; /* follows flags, dstnode */
4794 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4798 /* Inefficient because we do our Calvinball dance twice */
4799 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4800 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4806 /* Inefficient because we do our Calvinball dance twice */
4807 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4808 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4813 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4817 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4821 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4825 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4829 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4833 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4840 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4841 /* Check for pad = 1, long header case */
4842 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4843 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4844 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4845 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4847 /* Check for pad = 0, long header case */
4848 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4850 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4851 (bpf_u_int32
)ntohs((u_short
)addr
));
4854 /* Check for pad = 1, short header case */
4855 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4856 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4857 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4858 (bpf_u_int32
)ntohs((u_short
)addr
));
4861 /* Check for pad = 0, short header case */
4862 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4864 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4865 (bpf_u_int32
)ntohs((u_short
)addr
));
4869 /* Combine with test for cstate->linktype */
4875 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4876 * test the bottom-of-stack bit, and then check the version number
4877 * field in the IP header.
4879 static struct block
*
4880 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4882 struct block
*b0
, *b1
;
4887 /* match the bottom-of-stack bit */
4888 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4889 /* match the IPv4 version number */
4890 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4894 case ETHERTYPE_IPV6
:
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
, 0x60, 0xf0);
4903 /* FIXME add other L3 proto IDs */
4904 bpf_error(cstate
, "unsupported protocol over mpls");
4909 static struct block
*
4910 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4911 int proto
, int dir
, int type
)
4913 struct block
*b0
, *b1
;
4914 const char *typestr
;
4924 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4926 * Only check for non-IPv4 addresses if we're not
4927 * checking MPLS-encapsulated packets.
4929 if (cstate
->label_stack_depth
== 0) {
4930 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4932 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4938 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4941 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4944 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4947 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4950 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4953 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4956 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4959 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4962 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4965 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4968 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4971 return gen_dnhostop(cstate
, addr
, dir
);
4974 bpf_error(cstate
, "LAT host filtering not implemented");
4977 bpf_error(cstate
, "SCA host filtering not implemented");
4980 bpf_error(cstate
, "MOPRC host filtering not implemented");
4983 bpf_error(cstate
, "MOPDL host filtering not implemented");
4986 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4989 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4992 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4995 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4998 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5001 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5004 bpf_error(cstate
, "AARP host filtering not implemented");
5007 bpf_error(cstate
, "ISO host filtering not implemented");
5010 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5013 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5016 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5019 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5022 bpf_error(cstate
, "IPX host filtering not implemented");
5025 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5028 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5031 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5034 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5037 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5040 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5043 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5046 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5049 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5052 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5061 static struct block
*
5062 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5063 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5065 const char *typestr
;
5075 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5078 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5081 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5084 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5087 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5090 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5093 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5096 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5099 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5102 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5105 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5108 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5111 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5114 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5117 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5120 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5123 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5126 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5129 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5132 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5135 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5138 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5141 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5144 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5147 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5150 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5153 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5156 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5159 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5162 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5165 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5168 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5171 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5174 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5177 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5180 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5183 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5186 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5189 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5192 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5202 static struct block
*
5203 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5204 struct addrinfo
*alist
, int proto
, int dir
)
5206 struct block
*b0
, *b1
, *tmp
;
5207 struct addrinfo
*ai
;
5208 struct sockaddr_in
*sin
;
5211 bpf_error(cstate
, "direction applied to 'gateway'");
5218 switch (cstate
->linktype
) {
5220 case DLT_NETANALYZER
:
5221 case DLT_NETANALYZER_TRANSPARENT
:
5222 b1
= gen_prevlinkhdr_check(cstate
);
5223 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5228 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5231 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5233 case DLT_IEEE802_11
:
5234 case DLT_PRISM_HEADER
:
5235 case DLT_IEEE802_11_RADIO_AVS
:
5236 case DLT_IEEE802_11_RADIO
:
5238 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5242 * This is LLC-multiplexed traffic; if it were
5243 * LANE, cstate->linktype would have been set to
5247 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5249 case DLT_IP_OVER_FC
:
5250 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5254 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5257 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5259 * Does it have an address?
5261 if (ai
->ai_addr
!= NULL
) {
5263 * Yes. Is it an IPv4 address?
5265 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5267 * Generate an entry for it.
5269 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5270 tmp
= gen_host(cstate
,
5271 ntohl(sin
->sin_addr
.s_addr
),
5272 0xffffffff, proto
, Q_OR
, Q_HOST
);
5274 * Is it the *first* IPv4 address?
5278 * Yes, so start with it.
5283 * No, so OR it into the
5295 * No IPv4 addresses found.
5303 bpf_error(cstate
, "illegal modifier of 'gateway'");
5308 static struct block
*
5309 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5317 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5318 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5323 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5324 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5329 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5330 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5335 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5338 #ifndef IPPROTO_IGMP
5339 #define IPPROTO_IGMP 2
5343 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5346 #ifndef IPPROTO_IGRP
5347 #define IPPROTO_IGRP 9
5350 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5354 #define IPPROTO_PIM 103
5358 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5359 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5363 #ifndef IPPROTO_VRRP
5364 #define IPPROTO_VRRP 112
5368 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5371 #ifndef IPPROTO_CARP
5372 #define IPPROTO_CARP 112
5376 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5380 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5384 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5388 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5392 bpf_error(cstate
, "link layer applied in wrong context");
5395 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5399 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5403 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5407 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5411 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5415 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5419 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5423 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5426 #ifndef IPPROTO_ICMPV6
5427 #define IPPROTO_ICMPV6 58
5430 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5434 #define IPPROTO_AH 51
5437 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5438 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5443 #define IPPROTO_ESP 50
5446 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5447 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5452 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5456 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5460 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5463 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5464 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5465 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5467 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5469 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5471 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5475 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5476 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5477 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5479 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5481 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5483 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5487 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5488 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5489 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5491 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5496 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5497 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5502 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5503 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5505 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5507 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5512 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5513 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5518 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5519 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5524 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5528 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5532 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5536 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5540 bpf_error(cstate
, "'radio' is not a valid protocol type");
5549 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5552 * Catch errors reported by us and routines below us, and return NULL
5555 if (setjmp(cstate
->top_ctx
))
5558 return gen_proto_abbrev_internal(cstate
, proto
);
5561 static struct block
*
5562 gen_ipfrag(compiler_state_t
*cstate
)
5567 /* not IPv4 frag other than the first frag */
5568 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5569 b
= new_block(cstate
, JMP(BPF_JSET
));
5578 * Generate a comparison to a port value in the transport-layer header
5579 * at the specified offset from the beginning of that header.
5581 * XXX - this handles a variable-length prefix preceding the link-layer
5582 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5583 * variable-length link-layer headers (such as Token Ring or 802.11
5586 static struct block
*
5587 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5589 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5592 static struct block
*
5593 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5595 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5598 static struct block
*
5599 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5601 struct block
*b0
, *b1
, *tmp
;
5603 /* ip proto 'proto' and not a fragment other than the first fragment */
5604 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5605 b0
= gen_ipfrag(cstate
);
5610 b1
= gen_portatom(cstate
, 0, port
);
5614 b1
= gen_portatom(cstate
, 2, port
);
5618 tmp
= gen_portatom(cstate
, 0, port
);
5619 b1
= gen_portatom(cstate
, 2, port
);
5625 tmp
= gen_portatom(cstate
, 0, port
);
5626 b1
= gen_portatom(cstate
, 2, port
);
5631 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5635 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5639 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5643 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5647 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5651 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5663 static struct block
*
5664 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5666 struct block
*b0
, *b1
, *tmp
;
5671 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5672 * not LLC encapsulation with LLCSAP_IP.
5674 * For IEEE 802 networks - which includes 802.5 token ring
5675 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5676 * says that SNAP encapsulation is used, not LLC encapsulation
5679 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5680 * RFC 2225 say that SNAP encapsulation is used, not LLC
5681 * encapsulation with LLCSAP_IP.
5683 * So we always check for ETHERTYPE_IP.
5685 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5691 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5695 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5696 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5698 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5710 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5712 struct block
*b0
, *b1
, *tmp
;
5714 /* ip6 proto 'proto' */
5715 /* XXX - catch the first fragment of a fragmented packet? */
5716 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5720 b1
= gen_portatom6(cstate
, 0, port
);
5724 b1
= gen_portatom6(cstate
, 2, port
);
5728 tmp
= gen_portatom6(cstate
, 0, port
);
5729 b1
= gen_portatom6(cstate
, 2, port
);
5735 tmp
= gen_portatom6(cstate
, 0, port
);
5736 b1
= gen_portatom6(cstate
, 2, port
);
5748 static struct block
*
5749 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5751 struct block
*b0
, *b1
, *tmp
;
5753 /* link proto ip6 */
5754 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5760 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5764 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5765 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5767 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5778 /* gen_portrange code */
5779 static struct block
*
5780 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5783 struct block
*b1
, *b2
;
5787 * Reverse the order of the ports, so v1 is the lower one.
5796 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5797 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5804 static struct block
*
5805 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5806 bpf_u_int32 proto
, int dir
)
5808 struct block
*b0
, *b1
, *tmp
;
5810 /* ip proto 'proto' and not a fragment other than the first fragment */
5811 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5812 b0
= gen_ipfrag(cstate
);
5817 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5821 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5825 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5826 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5832 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5833 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5838 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5842 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5846 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5850 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5854 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5858 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5870 static struct block
*
5871 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5874 struct block
*b0
, *b1
, *tmp
;
5877 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5883 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5888 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5889 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5891 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5902 static struct block
*
5903 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5906 struct block
*b1
, *b2
;
5910 * Reverse the order of the ports, so v1 is the lower one.
5919 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5920 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5927 static struct block
*
5928 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5929 bpf_u_int32 proto
, int dir
)
5931 struct block
*b0
, *b1
, *tmp
;
5933 /* ip6 proto 'proto' */
5934 /* XXX - catch the first fragment of a fragmented packet? */
5935 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5939 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5943 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5947 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5948 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5954 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5955 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5967 static struct block
*
5968 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5971 struct block
*b0
, *b1
, *tmp
;
5973 /* link proto ip6 */
5974 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5980 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5985 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5986 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5988 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6000 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6009 v
= pcap_nametoproto(name
);
6010 if (v
== PROTO_UNDEF
)
6011 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6015 /* XXX should look up h/w protocol type based on cstate->linktype */
6016 v
= pcap_nametoeproto(name
);
6017 if (v
== PROTO_UNDEF
) {
6018 v
= pcap_nametollc(name
);
6019 if (v
== PROTO_UNDEF
)
6020 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6025 if (strcmp(name
, "esis") == 0)
6027 else if (strcmp(name
, "isis") == 0)
6029 else if (strcmp(name
, "clnp") == 0)
6032 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6044 gen_joinsp(struct stmt
**s
, int n
)
6050 static struct block
*
6051 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6053 #ifdef NO_PROTOCHAIN
6054 return gen_proto(cstate
, v
, proto
);
6056 struct block
*b0
, *b
;
6057 struct slist
*s
[100];
6058 int fix2
, fix3
, fix4
, fix5
;
6059 int ahcheck
, again
, end
;
6061 int reg2
= alloc_reg(cstate
);
6063 memset(s
, 0, sizeof(s
));
6064 fix3
= fix4
= fix5
= 0;
6071 b0
= gen_protochain(cstate
, v
, Q_IP
);
6072 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6076 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6081 * We don't handle variable-length prefixes before the link-layer
6082 * header, or variable-length link-layer headers, here yet.
6083 * We might want to add BPF instructions to do the protochain
6084 * work, to simplify that and, on platforms that have a BPF
6085 * interpreter with the new instructions, let the filtering
6086 * be done in the kernel. (We already require a modified BPF
6087 * engine to do the protochain stuff, to support backward
6088 * branches, and backward branch support is unlikely to appear
6089 * in kernel BPF engines.)
6091 if (cstate
->off_linkpl
.is_variable
)
6092 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6095 * To quote a comment in optimize.c:
6097 * "These data structures are used in a Cocke and Shwarz style
6098 * value numbering scheme. Since the flowgraph is acyclic,
6099 * exit values can be propagated from a node's predecessors
6100 * provided it is uniquely defined."
6102 * "Acyclic" means "no backward branches", which means "no
6103 * loops", so we have to turn the optimizer off.
6105 cstate
->no_optimize
= 1;
6108 * s[0] is a dummy entry to protect other BPF insn from damage
6109 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6110 * hard to find interdependency made by jump table fixup.
6113 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6118 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6121 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6122 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6124 /* X = ip->ip_hl << 2 */
6125 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6126 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6131 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6133 /* A = ip6->ip_nxt */
6134 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6135 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6137 /* X = sizeof(struct ip6_hdr) */
6138 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6144 bpf_error(cstate
, "unsupported proto to gen_protochain");
6148 /* again: if (A == v) goto end; else fall through; */
6150 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6152 s
[i
]->s
.jt
= NULL
; /*later*/
6153 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6157 #ifndef IPPROTO_NONE
6158 #define IPPROTO_NONE 59
6160 /* if (A == IPPROTO_NONE) goto end */
6161 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6162 s
[i
]->s
.jt
= NULL
; /*later*/
6163 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6164 s
[i
]->s
.k
= IPPROTO_NONE
;
6165 s
[fix5
]->s
.jf
= s
[i
];
6169 if (proto
== Q_IPV6
) {
6170 int v6start
, v6end
, v6advance
, j
;
6173 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6174 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6175 s
[i
]->s
.jt
= NULL
; /*later*/
6176 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6177 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6178 s
[fix2
]->s
.jf
= s
[i
];
6180 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6181 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6182 s
[i
]->s
.jt
= NULL
; /*later*/
6183 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6184 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6186 /* if (A == IPPROTO_ROUTING) goto v6advance */
6187 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6188 s
[i
]->s
.jt
= NULL
; /*later*/
6189 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6190 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6192 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6193 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6194 s
[i
]->s
.jt
= NULL
; /*later*/
6195 s
[i
]->s
.jf
= NULL
; /*later*/
6196 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6206 * A = P[X + packet head];
6207 * X = X + (P[X + packet head + 1] + 1) * 8;
6209 /* A = P[X + packet head] */
6210 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6211 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6214 s
[i
] = new_stmt(cstate
, BPF_ST
);
6217 /* A = P[X + packet head + 1]; */
6218 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6219 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6222 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6226 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6230 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6234 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6237 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6241 /* goto again; (must use BPF_JA for backward jump) */
6242 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6243 s
[i
]->s
.k
= again
- i
- 1;
6244 s
[i
- 1]->s
.jf
= s
[i
];
6248 for (j
= v6start
; j
<= v6end
; j
++)
6249 s
[j
]->s
.jt
= s
[v6advance
];
6252 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6254 s
[fix2
]->s
.jf
= s
[i
];
6260 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6261 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6262 s
[i
]->s
.jt
= NULL
; /*later*/
6263 s
[i
]->s
.jf
= NULL
; /*later*/
6264 s
[i
]->s
.k
= IPPROTO_AH
;
6266 s
[fix3
]->s
.jf
= s
[ahcheck
];
6273 * X = X + (P[X + 1] + 2) * 4;
6276 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6278 /* A = P[X + packet head]; */
6279 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6280 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6283 s
[i
] = new_stmt(cstate
, BPF_ST
);
6287 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6290 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6294 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6296 /* A = P[X + packet head] */
6297 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6298 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6301 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6305 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6309 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6312 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6316 /* goto again; (must use BPF_JA for backward jump) */
6317 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6318 s
[i
]->s
.k
= again
- i
- 1;
6323 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6325 s
[fix2
]->s
.jt
= s
[end
];
6326 s
[fix4
]->s
.jf
= s
[end
];
6327 s
[fix5
]->s
.jt
= s
[end
];
6334 for (i
= 0; i
< max
- 1; i
++)
6335 s
[i
]->next
= s
[i
+ 1];
6336 s
[max
- 1]->next
= NULL
;
6341 b
= new_block(cstate
, JMP(BPF_JEQ
));
6342 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6345 free_reg(cstate
, reg2
);
6352 static struct block
*
6353 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6356 struct block
*b0
, *b1
;
6359 * A data frame has the 0x08 bit (b3) in the frame control field set
6360 * and the 0x04 bit (b2) clear.
6362 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6363 b0
= new_block(cstate
, JMP(BPF_JSET
));
6367 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6368 b1
= new_block(cstate
, JMP(BPF_JSET
));
6379 * Generate code that checks whether the packet is a packet for protocol
6380 * <proto> and whether the type field in that protocol's header has
6381 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6382 * IP packet and checks the protocol number in the IP header against <v>.
6384 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6385 * against Q_IP and Q_IPV6.
6387 static struct block
*
6388 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6390 struct block
*b0
, *b1
;
6395 if (dir
!= Q_DEFAULT
)
6396 bpf_error(cstate
, "direction applied to 'proto'");
6400 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6401 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6406 return gen_linktype(cstate
, v
);
6410 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6411 * not LLC encapsulation with LLCSAP_IP.
6413 * For IEEE 802 networks - which includes 802.5 token ring
6414 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6415 * says that SNAP encapsulation is used, not LLC encapsulation
6418 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6419 * RFC 2225 say that SNAP encapsulation is used, not LLC
6420 * encapsulation with LLCSAP_IP.
6422 * So we always check for ETHERTYPE_IP.
6424 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6426 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6428 b1
= gen_protochain(cstate
, v
, Q_IP
);
6434 bpf_error(cstate
, "arp does not encapsulate another protocol");
6438 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6442 bpf_error(cstate
, "'sctp proto' is bogus");
6446 bpf_error(cstate
, "'tcp proto' is bogus");
6450 bpf_error(cstate
, "'udp proto' is bogus");
6454 bpf_error(cstate
, "'icmp proto' is bogus");
6458 bpf_error(cstate
, "'igmp proto' is bogus");
6462 bpf_error(cstate
, "'igrp proto' is bogus");
6466 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6470 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6474 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6478 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6482 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6486 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6490 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6493 * Also check for a fragment header before the final
6496 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6497 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6499 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6502 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6508 bpf_error(cstate
, "'icmp6 proto' is bogus");
6512 bpf_error(cstate
, "'ah proto' is bogus");
6516 bpf_error(cstate
, "'esp proto' is bogus");
6520 bpf_error(cstate
, "'pim proto' is bogus");
6524 bpf_error(cstate
, "'vrrp proto' is bogus");
6528 bpf_error(cstate
, "'aarp proto' is bogus");
6532 switch (cstate
->linktype
) {
6536 * Frame Relay packets typically have an OSI
6537 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6538 * generates code to check for all the OSI
6539 * NLPIDs, so calling it and then adding a check
6540 * for the particular NLPID for which we're
6541 * looking is bogus, as we can just check for
6544 * What we check for is the NLPID and a frame
6545 * control field value of UI, i.e. 0x03 followed
6548 * XXX - assumes a 2-byte Frame Relay header with
6549 * DLCI and flags. What if the address is longer?
6551 * XXX - what about SNAP-encapsulated frames?
6553 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6558 * Cisco uses an Ethertype lookalike - for OSI,
6561 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6562 /* OSI in C-HDLC is stuffed with a fudge byte */
6563 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6568 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6569 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6575 bpf_error(cstate
, "'esis proto' is bogus");
6579 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6581 * 4 is the offset of the PDU type relative to the IS-IS
6584 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6589 bpf_error(cstate
, "'clnp proto' is not supported");
6593 bpf_error(cstate
, "'stp proto' is bogus");
6597 bpf_error(cstate
, "'ipx proto' is bogus");
6601 bpf_error(cstate
, "'netbeui proto' is bogus");
6605 bpf_error(cstate
, "'l1 proto' is bogus");
6609 bpf_error(cstate
, "'l2 proto' is bogus");
6613 bpf_error(cstate
, "'iih proto' is bogus");
6617 bpf_error(cstate
, "'snp proto' is bogus");
6621 bpf_error(cstate
, "'csnp proto' is bogus");
6625 bpf_error(cstate
, "'psnp proto' is bogus");
6629 bpf_error(cstate
, "'lsp proto' is bogus");
6633 bpf_error(cstate
, "'radio proto' is bogus");
6637 bpf_error(cstate
, "'carp proto' is bogus");
6648 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6650 int proto
= q
.proto
;
6654 bpf_u_int32 mask
, addr
;
6655 struct addrinfo
*res
, *res0
;
6656 struct sockaddr_in
*sin4
;
6659 struct sockaddr_in6
*sin6
;
6660 struct in6_addr mask128
;
6662 struct block
*b
, *tmp
;
6663 int port
, real_proto
;
6667 * Catch errors reported by us and routines below us, and return NULL
6670 if (setjmp(cstate
->top_ctx
))
6676 addr
= pcap_nametonetaddr(name
);
6678 bpf_error(cstate
, "unknown network '%s'", name
);
6679 /* Left justify network addr and calculate its network mask */
6681 while (addr
&& (addr
& 0xff000000) == 0) {
6685 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6689 if (proto
== Q_LINK
) {
6690 switch (cstate
->linktype
) {
6693 case DLT_NETANALYZER
:
6694 case DLT_NETANALYZER_TRANSPARENT
:
6695 eaddr
= pcap_ether_hostton(name
);
6698 "unknown ether host '%s'", name
);
6699 tmp
= gen_prevlinkhdr_check(cstate
);
6700 b
= gen_ehostop(cstate
, eaddr
, dir
);
6707 eaddr
= pcap_ether_hostton(name
);
6710 "unknown FDDI host '%s'", name
);
6711 b
= gen_fhostop(cstate
, eaddr
, dir
);
6716 eaddr
= pcap_ether_hostton(name
);
6719 "unknown token ring host '%s'", name
);
6720 b
= gen_thostop(cstate
, eaddr
, dir
);
6724 case DLT_IEEE802_11
:
6725 case DLT_PRISM_HEADER
:
6726 case DLT_IEEE802_11_RADIO_AVS
:
6727 case DLT_IEEE802_11_RADIO
:
6729 eaddr
= pcap_ether_hostton(name
);
6732 "unknown 802.11 host '%s'", name
);
6733 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6737 case DLT_IP_OVER_FC
:
6738 eaddr
= pcap_ether_hostton(name
);
6741 "unknown Fibre Channel host '%s'", name
);
6742 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6747 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6748 } else if (proto
== Q_DECNET
) {
6749 unsigned short dn_addr
;
6751 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6753 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6755 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6760 * I don't think DECNET hosts can be multihomed, so
6761 * there is no need to build up a list of addresses
6763 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6766 memset(&mask128
, 0xff, sizeof(mask128
));
6768 res0
= res
= pcap_nametoaddrinfo(name
);
6770 bpf_error(cstate
, "unknown host '%s'", name
);
6777 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6778 tproto
== Q_DEFAULT
) {
6784 for (res
= res0
; res
; res
= res
->ai_next
) {
6785 switch (res
->ai_family
) {
6788 if (tproto
== Q_IPV6
)
6792 sin4
= (struct sockaddr_in
*)
6794 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6795 0xffffffff, tproto
, dir
, q
.addr
);
6799 if (tproto6
== Q_IP
)
6802 sin6
= (struct sockaddr_in6
*)
6804 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6805 &mask128
, tproto6
, dir
, q
.addr
);
6818 bpf_error(cstate
, "unknown host '%s'%s", name
,
6819 (proto
== Q_DEFAULT
)
6821 : " for specified address family");
6827 if (proto
!= Q_DEFAULT
&&
6828 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6829 bpf_error(cstate
, "illegal qualifier of 'port'");
6830 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6831 bpf_error(cstate
, "unknown port '%s'", name
);
6832 if (proto
== Q_UDP
) {
6833 if (real_proto
== IPPROTO_TCP
)
6834 bpf_error(cstate
, "port '%s' is tcp", name
);
6835 else if (real_proto
== IPPROTO_SCTP
)
6836 bpf_error(cstate
, "port '%s' is sctp", name
);
6838 /* override PROTO_UNDEF */
6839 real_proto
= IPPROTO_UDP
;
6841 if (proto
== Q_TCP
) {
6842 if (real_proto
== IPPROTO_UDP
)
6843 bpf_error(cstate
, "port '%s' is udp", name
);
6845 else if (real_proto
== IPPROTO_SCTP
)
6846 bpf_error(cstate
, "port '%s' is sctp", name
);
6848 /* override PROTO_UNDEF */
6849 real_proto
= IPPROTO_TCP
;
6851 if (proto
== Q_SCTP
) {
6852 if (real_proto
== IPPROTO_UDP
)
6853 bpf_error(cstate
, "port '%s' is udp", name
);
6855 else if (real_proto
== IPPROTO_TCP
)
6856 bpf_error(cstate
, "port '%s' is tcp", name
);
6858 /* override PROTO_UNDEF */
6859 real_proto
= IPPROTO_SCTP
;
6862 bpf_error(cstate
, "illegal port number %d < 0", port
);
6864 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6865 b
= gen_port(cstate
, port
, real_proto
, dir
);
6866 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6870 if (proto
!= Q_DEFAULT
&&
6871 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6872 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6873 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6874 bpf_error(cstate
, "unknown port in range '%s'", name
);
6875 if (proto
== Q_UDP
) {
6876 if (real_proto
== IPPROTO_TCP
)
6877 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6878 else if (real_proto
== IPPROTO_SCTP
)
6879 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6881 /* override PROTO_UNDEF */
6882 real_proto
= IPPROTO_UDP
;
6884 if (proto
== Q_TCP
) {
6885 if (real_proto
== IPPROTO_UDP
)
6886 bpf_error(cstate
, "port in range '%s' is udp", name
);
6887 else if (real_proto
== IPPROTO_SCTP
)
6888 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6890 /* override PROTO_UNDEF */
6891 real_proto
= IPPROTO_TCP
;
6893 if (proto
== Q_SCTP
) {
6894 if (real_proto
== IPPROTO_UDP
)
6895 bpf_error(cstate
, "port in range '%s' is udp", name
);
6896 else if (real_proto
== IPPROTO_TCP
)
6897 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6899 /* override PROTO_UNDEF */
6900 real_proto
= IPPROTO_SCTP
;
6903 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6905 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6907 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6909 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6911 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6912 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6917 eaddr
= pcap_ether_hostton(name
);
6919 bpf_error(cstate
, "unknown ether host: %s", name
);
6921 res
= pcap_nametoaddrinfo(name
);
6924 bpf_error(cstate
, "unknown host '%s'", name
);
6925 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6929 bpf_error(cstate
, "unknown host '%s'", name
);
6932 bpf_error(cstate
, "'gateway' not supported in this configuration");
6936 real_proto
= lookup_proto(cstate
, name
, proto
);
6937 if (real_proto
>= 0)
6938 return gen_proto(cstate
, real_proto
, proto
, dir
);
6940 bpf_error(cstate
, "unknown protocol: %s", name
);
6943 real_proto
= lookup_proto(cstate
, name
, proto
);
6944 if (real_proto
>= 0)
6945 return gen_protochain(cstate
, real_proto
, proto
);
6947 bpf_error(cstate
, "unknown protocol: %s", name
);
6958 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6959 bpf_u_int32 masklen
, struct qual q
)
6961 register int nlen
, mlen
;
6965 * Catch errors reported by us and routines below us, and return NULL
6968 if (setjmp(cstate
->top_ctx
))
6971 nlen
= __pcap_atoin(s1
, &n
);
6973 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
6974 /* Promote short ipaddr */
6978 mlen
= __pcap_atoin(s2
, &m
);
6980 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
6981 /* Promote short ipaddr */
6984 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6987 /* Convert mask len to mask */
6989 bpf_error(cstate
, "mask length must be <= 32");
6992 * X << 32 is not guaranteed by C to be 0; it's
6997 m
= 0xffffffff << (32 - masklen
);
6999 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7006 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7009 bpf_error(cstate
, "Mask syntax for networks only");
7016 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7024 * Catch errors reported by us and routines below us, and return NULL
7027 if (setjmp(cstate
->top_ctx
))
7034 else if (q
.proto
== Q_DECNET
) {
7035 vlen
= __pcap_atodn(s
, &v
);
7037 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7039 vlen
= __pcap_atoin(s
, &v
);
7041 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7049 if (proto
== Q_DECNET
)
7050 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7051 else if (proto
== Q_LINK
) {
7052 bpf_error(cstate
, "illegal link layer address");
7055 if (s
== NULL
&& q
.addr
== Q_NET
) {
7056 /* Promote short net number */
7057 while (v
&& (v
& 0xff000000) == 0) {
7062 /* Promote short ipaddr */
7064 mask
<<= 32 - vlen
;
7066 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7071 proto
= IPPROTO_UDP
;
7072 else if (proto
== Q_TCP
)
7073 proto
= IPPROTO_TCP
;
7074 else if (proto
== Q_SCTP
)
7075 proto
= IPPROTO_SCTP
;
7076 else if (proto
== Q_DEFAULT
)
7077 proto
= PROTO_UNDEF
;
7079 bpf_error(cstate
, "illegal qualifier of 'port'");
7082 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7086 b
= gen_port(cstate
, v
, proto
, dir
);
7087 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7093 proto
= IPPROTO_UDP
;
7094 else if (proto
== Q_TCP
)
7095 proto
= IPPROTO_TCP
;
7096 else if (proto
== Q_SCTP
)
7097 proto
= IPPROTO_SCTP
;
7098 else if (proto
== Q_DEFAULT
)
7099 proto
= PROTO_UNDEF
;
7101 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7104 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7108 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7109 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7114 bpf_error(cstate
, "'gateway' requires a name");
7118 return gen_proto(cstate
, v
, proto
, dir
);
7121 return gen_protochain(cstate
, v
, proto
);
7136 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7137 bpf_u_int32 masklen
, struct qual q
)
7139 struct addrinfo
*res
;
7140 struct in6_addr
*addr
;
7141 struct in6_addr mask
;
7146 * Catch errors reported by us and routines below us, and return NULL
7149 if (setjmp(cstate
->top_ctx
))
7153 bpf_error(cstate
, "no mask %s supported", s2
);
7155 res
= pcap_nametoaddrinfo(s1
);
7157 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7160 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7161 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7163 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7164 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
.s6_addr
) * 8));
7165 memset(&mask
, 0, sizeof(mask
));
7166 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7168 mask
.s6_addr
[masklen
/ 8] =
7169 (0xff << (8 - masklen
% 8)) & 0xff;
7172 a
= (uint32_t *)addr
;
7173 m
= (uint32_t *)&mask
;
7174 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7175 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7176 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7184 bpf_error(cstate
, "Mask syntax for networks only");
7188 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7194 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7201 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7203 struct block
*b
, *tmp
;
7206 * Catch errors reported by us and routines below us, and return NULL
7209 if (setjmp(cstate
->top_ctx
))
7212 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7213 cstate
->e
= pcap_ether_aton(s
);
7214 if (cstate
->e
== NULL
)
7215 bpf_error(cstate
, "malloc");
7216 switch (cstate
->linktype
) {
7218 case DLT_NETANALYZER
:
7219 case DLT_NETANALYZER_TRANSPARENT
:
7220 tmp
= gen_prevlinkhdr_check(cstate
);
7221 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7226 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7229 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7231 case DLT_IEEE802_11
:
7232 case DLT_PRISM_HEADER
:
7233 case DLT_IEEE802_11_RADIO_AVS
:
7234 case DLT_IEEE802_11_RADIO
:
7236 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7238 case DLT_IP_OVER_FC
:
7239 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7244 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7251 bpf_error(cstate
, "ethernet address used in non-ether expression");
7256 sappend(struct slist
*s0
, struct slist
*s1
)
7259 * This is definitely not the best way to do this, but the
7260 * lists will rarely get long.
7267 static struct slist
*
7268 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7272 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7277 static struct slist
*
7278 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7282 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7288 * Modify "index" to use the value stored into its register as an
7289 * offset relative to the beginning of the header for the protocol
7290 * "proto", and allocate a register and put an item "size" bytes long
7291 * (1, 2, or 4) at that offset into that register, making it the register
7294 static struct arth
*
7295 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7299 struct slist
*s
, *tmp
;
7301 int regno
= alloc_reg(cstate
);
7303 free_reg(cstate
, inst
->regno
);
7307 bpf_error(cstate
, "data size must be 1, 2, or 4");
7324 bpf_error(cstate
, "unsupported index operation");
7328 * The offset is relative to the beginning of the packet
7329 * data, if we have a radio header. (If we don't, this
7332 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7333 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7334 cstate
->linktype
!= DLT_PRISM_HEADER
)
7335 bpf_error(cstate
, "radio information not present in capture");
7338 * Load into the X register the offset computed into the
7339 * register specified by "index".
7341 s
= xfer_to_x(cstate
, inst
);
7344 * Load the item at that offset.
7346 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7348 sappend(inst
->s
, s
);
7353 * The offset is relative to the beginning of
7354 * the link-layer header.
7356 * XXX - what about ATM LANE? Should the index be
7357 * relative to the beginning of the AAL5 frame, so
7358 * that 0 refers to the beginning of the LE Control
7359 * field, or relative to the beginning of the LAN
7360 * frame, so that 0 refers, for Ethernet LANE, to
7361 * the beginning of the destination address?
7363 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7366 * If "s" is non-null, it has code to arrange that the
7367 * X register contains the length of the prefix preceding
7368 * the link-layer header. Add to it the offset computed
7369 * into the register specified by "index", and move that
7370 * into the X register. Otherwise, just load into the X
7371 * register the offset computed into the register specified
7375 sappend(s
, xfer_to_a(cstate
, inst
));
7376 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7377 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7379 s
= xfer_to_x(cstate
, inst
);
7382 * Load the item at the sum of the offset we've put in the
7383 * X register and the offset of the start of the link
7384 * layer header (which is 0 if the radio header is
7385 * variable-length; that header length is what we put
7386 * into the X register and then added to the index).
7388 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7389 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7391 sappend(inst
->s
, s
);
7405 * The offset is relative to the beginning of
7406 * the network-layer header.
7407 * XXX - are there any cases where we want
7408 * cstate->off_nl_nosnap?
7410 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7413 * If "s" is non-null, it has code to arrange that the
7414 * X register contains the variable part of the offset
7415 * of the link-layer payload. Add to it the offset
7416 * computed into the register specified by "index",
7417 * and move that into the X register. Otherwise, just
7418 * load into the X register the offset computed into
7419 * the register specified by "index".
7422 sappend(s
, xfer_to_a(cstate
, inst
));
7423 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7424 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7426 s
= xfer_to_x(cstate
, inst
);
7429 * Load the item at the sum of the offset we've put in the
7430 * X register, the offset of the start of the network
7431 * layer header from the beginning of the link-layer
7432 * payload, and the constant part of the offset of the
7433 * start of the link-layer payload.
7435 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7436 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7438 sappend(inst
->s
, s
);
7441 * Do the computation only if the packet contains
7442 * the protocol in question.
7444 b
= gen_proto_abbrev_internal(cstate
, proto
);
7446 gen_and(inst
->b
, b
);
7460 * The offset is relative to the beginning of
7461 * the transport-layer header.
7463 * Load the X register with the length of the IPv4 header
7464 * (plus the offset of the link-layer header, if it's
7465 * a variable-length header), in bytes.
7467 * XXX - are there any cases where we want
7468 * cstate->off_nl_nosnap?
7469 * XXX - we should, if we're built with
7470 * IPv6 support, generate code to load either
7471 * IPv4, IPv6, or both, as appropriate.
7473 s
= gen_loadx_iphdrlen(cstate
);
7476 * The X register now contains the sum of the variable
7477 * part of the offset of the link-layer payload and the
7478 * length of the network-layer header.
7480 * Load into the A register the offset relative to
7481 * the beginning of the transport layer header,
7482 * add the X register to that, move that to the
7483 * X register, and load with an offset from the
7484 * X register equal to the sum of the constant part of
7485 * the offset of the link-layer payload and the offset,
7486 * relative to the beginning of the link-layer payload,
7487 * of the network-layer header.
7489 sappend(s
, xfer_to_a(cstate
, inst
));
7490 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7491 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7492 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7493 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7494 sappend(inst
->s
, s
);
7497 * Do the computation only if the packet contains
7498 * the protocol in question - which is true only
7499 * if this is an IP datagram and is the first or
7500 * only fragment of that datagram.
7502 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7504 gen_and(inst
->b
, b
);
7505 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7510 * Do the computation only if the packet contains
7511 * the protocol in question.
7513 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7515 gen_and(inst
->b
, b
);
7520 * Check if we have an icmp6 next header
7522 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7524 gen_and(inst
->b
, b
);
7529 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7531 * If "s" is non-null, it has code to arrange that the
7532 * X register contains the variable part of the offset
7533 * of the link-layer payload. Add to it the offset
7534 * computed into the register specified by "index",
7535 * and move that into the X register. Otherwise, just
7536 * load into the X register the offset computed into
7537 * the register specified by "index".
7540 sappend(s
, xfer_to_a(cstate
, inst
));
7541 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7542 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7544 s
= xfer_to_x(cstate
, inst
);
7548 * Load the item at the sum of the offset we've put in the
7549 * X register, the offset of the start of the network
7550 * layer header from the beginning of the link-layer
7551 * payload, and the constant part of the offset of the
7552 * start of the link-layer payload.
7554 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7555 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7558 sappend(inst
->s
, s
);
7562 inst
->regno
= regno
;
7563 s
= new_stmt(cstate
, BPF_ST
);
7565 sappend(inst
->s
, s
);
7571 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7575 * Catch errors reported by us and routines below us, and return NULL
7578 if (setjmp(cstate
->top_ctx
))
7581 return gen_load_internal(cstate
, proto
, inst
, size
);
7584 static struct block
*
7585 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7586 struct arth
*a1
, int reversed
)
7588 struct slist
*s0
, *s1
, *s2
;
7589 struct block
*b
, *tmp
;
7591 s0
= xfer_to_x(cstate
, a1
);
7592 s1
= xfer_to_a(cstate
, a0
);
7593 if (code
== BPF_JEQ
) {
7594 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7595 b
= new_block(cstate
, JMP(code
));
7599 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7605 sappend(a0
->s
, a1
->s
);
7609 free_reg(cstate
, a0
->regno
);
7610 free_reg(cstate
, a1
->regno
);
7612 /* 'and' together protocol checks */
7615 gen_and(a0
->b
, tmp
= a1
->b
);
7629 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7630 struct arth
*a1
, int reversed
)
7633 * Catch errors reported by us and routines below us, and return NULL
7636 if (setjmp(cstate
->top_ctx
))
7639 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7643 gen_loadlen(compiler_state_t
*cstate
)
7650 * Catch errors reported by us and routines below us, and return NULL
7653 if (setjmp(cstate
->top_ctx
))
7656 regno
= alloc_reg(cstate
);
7657 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7658 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7659 s
->next
= new_stmt(cstate
, BPF_ST
);
7660 s
->next
->s
.k
= regno
;
7667 static struct arth
*
7668 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7674 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7676 reg
= alloc_reg(cstate
);
7678 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7680 s
->next
= new_stmt(cstate
, BPF_ST
);
7689 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7692 * Catch errors reported by us and routines below us, and return NULL
7695 if (setjmp(cstate
->top_ctx
))
7698 return gen_loadi_internal(cstate
, val
);
7702 * The a_arg dance is to avoid annoying whining by compilers that
7703 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7704 * It's not *used* after setjmp returns.
7707 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7709 struct arth
*a
= a_arg
;
7713 * Catch errors reported by us and routines below us, and return NULL
7716 if (setjmp(cstate
->top_ctx
))
7719 s
= xfer_to_a(cstate
, a
);
7721 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7724 s
= new_stmt(cstate
, BPF_ST
);
7732 * The a0_arg dance is to avoid annoying whining by compilers that
7733 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7734 * It's not *used* after setjmp returns.
7737 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7740 struct arth
*a0
= a0_arg
;
7741 struct slist
*s0
, *s1
, *s2
;
7744 * Catch errors reported by us and routines below us, and return NULL
7747 if (setjmp(cstate
->top_ctx
))
7751 * Disallow division by, or modulus by, zero; we do this here
7752 * so that it gets done even if the optimizer is disabled.
7754 * Also disallow shifts by a value greater than 31; we do this
7755 * here, for the same reason.
7757 if (code
== BPF_DIV
) {
7758 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7759 bpf_error(cstate
, "division by zero");
7760 } else if (code
== BPF_MOD
) {
7761 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7762 bpf_error(cstate
, "modulus by zero");
7763 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7764 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7765 bpf_error(cstate
, "shift by more than 31 bits");
7767 s0
= xfer_to_x(cstate
, a1
);
7768 s1
= xfer_to_a(cstate
, a0
);
7769 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7774 sappend(a0
->s
, a1
->s
);
7776 free_reg(cstate
, a0
->regno
);
7777 free_reg(cstate
, a1
->regno
);
7779 s0
= new_stmt(cstate
, BPF_ST
);
7780 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7787 * Initialize the table of used registers and the current register.
7790 init_regs(compiler_state_t
*cstate
)
7793 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7797 * Return the next free register.
7800 alloc_reg(compiler_state_t
*cstate
)
7802 int n
= BPF_MEMWORDS
;
7805 if (cstate
->regused
[cstate
->curreg
])
7806 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7808 cstate
->regused
[cstate
->curreg
] = 1;
7809 return cstate
->curreg
;
7812 bpf_error(cstate
, "too many registers needed to evaluate expression");
7817 * Return a register to the table so it can
7821 free_reg(compiler_state_t
*cstate
, int n
)
7823 cstate
->regused
[n
] = 0;
7826 static struct block
*
7827 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7832 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7833 b
= new_block(cstate
, JMP(jmp
));
7841 gen_greater(compiler_state_t
*cstate
, int n
)
7844 * Catch errors reported by us and routines below us, and return NULL
7847 if (setjmp(cstate
->top_ctx
))
7850 return gen_len(cstate
, BPF_JGE
, n
);
7854 * Actually, this is less than or equal.
7857 gen_less(compiler_state_t
*cstate
, int n
)
7862 * Catch errors reported by us and routines below us, and return NULL
7865 if (setjmp(cstate
->top_ctx
))
7868 b
= gen_len(cstate
, BPF_JGT
, n
);
7875 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7876 * the beginning of the link-layer header.
7877 * XXX - that means you can't test values in the radiotap header, but
7878 * as that header is difficult if not impossible to parse generally
7879 * without a loop, that might not be a severe problem. A new keyword
7880 * "radio" could be added for that, although what you'd really want
7881 * would be a way of testing particular radio header values, which
7882 * would generate code appropriate to the radio header in question.
7885 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7891 * Catch errors reported by us and routines below us, and return NULL
7894 if (setjmp(cstate
->top_ctx
))
7902 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7905 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7909 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7913 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7917 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7921 b
= new_block(cstate
, JMP(BPF_JEQ
));
7928 static const u_char abroadcast
[] = { 0x0 };
7931 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7933 bpf_u_int32 hostmask
;
7934 struct block
*b0
, *b1
, *b2
;
7935 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7938 * Catch errors reported by us and routines below us, and return NULL
7941 if (setjmp(cstate
->top_ctx
))
7948 switch (cstate
->linktype
) {
7950 case DLT_ARCNET_LINUX
:
7951 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7953 case DLT_NETANALYZER
:
7954 case DLT_NETANALYZER_TRANSPARENT
:
7955 b1
= gen_prevlinkhdr_check(cstate
);
7956 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7961 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7963 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7964 case DLT_IEEE802_11
:
7965 case DLT_PRISM_HEADER
:
7966 case DLT_IEEE802_11_RADIO_AVS
:
7967 case DLT_IEEE802_11_RADIO
:
7969 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7970 case DLT_IP_OVER_FC
:
7971 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7973 bpf_error(cstate
, "not a broadcast link");
7979 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7980 * as an indication that we don't know the netmask, and fail
7983 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7984 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7985 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7986 hostmask
= ~cstate
->netmask
;
7987 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
7988 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7989 ~0 & hostmask
, hostmask
);
7994 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7999 * Generate code to test the low-order bit of a MAC address (that's
8000 * the bottom bit of the *first* byte).
8002 static struct block
*
8003 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8005 register struct block
*b0
;
8006 register struct slist
*s
;
8008 /* link[offset] & 1 != 0 */
8009 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8010 b0
= new_block(cstate
, JMP(BPF_JSET
));
8017 gen_multicast(compiler_state_t
*cstate
, int proto
)
8019 register struct block
*b0
, *b1
, *b2
;
8020 register struct slist
*s
;
8023 * Catch errors reported by us and routines below us, and return NULL
8026 if (setjmp(cstate
->top_ctx
))
8033 switch (cstate
->linktype
) {
8035 case DLT_ARCNET_LINUX
:
8036 /* all ARCnet multicasts use the same address */
8037 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8039 case DLT_NETANALYZER
:
8040 case DLT_NETANALYZER_TRANSPARENT
:
8041 b1
= gen_prevlinkhdr_check(cstate
);
8042 /* ether[0] & 1 != 0 */
8043 b0
= gen_mac_multicast(cstate
, 0);
8049 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8051 * XXX - was that referring to bit-order issues?
8053 /* fddi[1] & 1 != 0 */
8054 return gen_mac_multicast(cstate
, 1);
8056 /* tr[2] & 1 != 0 */
8057 return gen_mac_multicast(cstate
, 2);
8058 case DLT_IEEE802_11
:
8059 case DLT_PRISM_HEADER
:
8060 case DLT_IEEE802_11_RADIO_AVS
:
8061 case DLT_IEEE802_11_RADIO
:
8066 * For control frames, there is no DA.
8068 * For management frames, DA is at an
8069 * offset of 4 from the beginning of
8072 * For data frames, DA is at an offset
8073 * of 4 from the beginning of the packet
8074 * if To DS is clear and at an offset of
8075 * 16 from the beginning of the packet
8080 * Generate the tests to be done for data frames.
8082 * First, check for To DS set, i.e. "link[1] & 0x01".
8084 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8085 b1
= new_block(cstate
, JMP(BPF_JSET
));
8086 b1
->s
.k
= 0x01; /* To DS */
8090 * If To DS is set, the DA is at 16.
8092 b0
= gen_mac_multicast(cstate
, 16);
8096 * Now, check for To DS not set, i.e. check
8097 * "!(link[1] & 0x01)".
8099 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8100 b2
= new_block(cstate
, JMP(BPF_JSET
));
8101 b2
->s
.k
= 0x01; /* To DS */
8106 * If To DS is not set, the DA is at 4.
8108 b1
= gen_mac_multicast(cstate
, 4);
8112 * Now OR together the last two checks. That gives
8113 * the complete set of checks for data frames.
8118 * Now check for a data frame.
8119 * I.e, check "link[0] & 0x08".
8121 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8122 b1
= new_block(cstate
, JMP(BPF_JSET
));
8127 * AND that with the checks done for data frames.
8132 * If the high-order bit of the type value is 0, this
8133 * is a management frame.
8134 * I.e, check "!(link[0] & 0x08)".
8136 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8137 b2
= new_block(cstate
, JMP(BPF_JSET
));
8143 * For management frames, the DA is at 4.
8145 b1
= gen_mac_multicast(cstate
, 4);
8149 * OR that with the checks done for data frames.
8150 * That gives the checks done for management and
8156 * If the low-order bit of the type value is 1,
8157 * this is either a control frame or a frame
8158 * with a reserved type, and thus not a
8161 * I.e., check "!(link[0] & 0x04)".
8163 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8164 b1
= new_block(cstate
, JMP(BPF_JSET
));
8170 * AND that with the checks for data and management
8175 case DLT_IP_OVER_FC
:
8176 b0
= gen_mac_multicast(cstate
, 2);
8181 /* Link not known to support multicasts */
8185 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8186 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8191 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8192 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8196 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8201 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8203 register struct block
*b0
;
8206 * Catch errors reported by us and routines below us, and return NULL
8209 if (setjmp(cstate
->top_ctx
))
8213 * Only some data link types support ifindex qualifiers.
8215 switch (cstate
->linktype
) {
8216 case DLT_LINUX_SLL2
:
8217 /* match packets on this interface */
8218 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8223 * This is Linux; we require PF_PACKET support.
8224 * If this is a *live* capture, we can look at
8225 * special meta-data in the filter expression;
8226 * if it's a savefile, we can't.
8228 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8229 /* We have a FILE *, so this is a savefile */
8230 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8231 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8236 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8238 #else /* defined(linux) */
8239 bpf_error(cstate
, "ifindex not supported on %s",
8240 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8242 #endif /* defined(linux) */
8248 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8249 * Outbound traffic is sent by this machine, while inbound traffic is
8250 * sent by a remote machine (and may include packets destined for a
8251 * unicast or multicast link-layer address we are not subscribing to).
8252 * These are the same definitions implemented by pcap_setdirection().
8253 * Capturing only unicast traffic destined for this host is probably
8254 * better accomplished using a higher-layer filter.
8257 gen_inbound(compiler_state_t
*cstate
, int dir
)
8259 register struct block
*b0
;
8262 * Catch errors reported by us and routines below us, and return NULL
8265 if (setjmp(cstate
->top_ctx
))
8269 * Only some data link types support inbound/outbound qualifiers.
8271 switch (cstate
->linktype
) {
8273 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8274 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8275 gen_loadi_internal(cstate
, 0),
8281 /* match outgoing packets */
8282 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8284 /* match incoming packets */
8285 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8290 /* match outgoing packets */
8291 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8293 /* to filter on inbound traffic, invert the match */
8298 case DLT_LINUX_SLL2
:
8299 /* match outgoing packets */
8300 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8302 /* to filter on inbound traffic, invert the match */
8307 #ifdef HAVE_NET_PFVAR_H
8309 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8310 ((dir
== 0) ? PF_IN
: PF_OUT
));
8316 /* match outgoing packets */
8317 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8319 /* match incoming packets */
8320 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8324 case DLT_JUNIPER_MFR
:
8325 case DLT_JUNIPER_MLFR
:
8326 case DLT_JUNIPER_MLPPP
:
8327 case DLT_JUNIPER_ATM1
:
8328 case DLT_JUNIPER_ATM2
:
8329 case DLT_JUNIPER_PPPOE
:
8330 case DLT_JUNIPER_PPPOE_ATM
:
8331 case DLT_JUNIPER_GGSN
:
8332 case DLT_JUNIPER_ES
:
8333 case DLT_JUNIPER_MONITOR
:
8334 case DLT_JUNIPER_SERVICES
:
8335 case DLT_JUNIPER_ETHER
:
8336 case DLT_JUNIPER_PPP
:
8337 case DLT_JUNIPER_FRELAY
:
8338 case DLT_JUNIPER_CHDLC
:
8339 case DLT_JUNIPER_VP
:
8340 case DLT_JUNIPER_ST
:
8341 case DLT_JUNIPER_ISM
:
8342 case DLT_JUNIPER_VS
:
8343 case DLT_JUNIPER_SRX_E2E
:
8344 case DLT_JUNIPER_FIBRECHANNEL
:
8345 case DLT_JUNIPER_ATM_CEMIC
:
8347 /* juniper flags (including direction) are stored
8348 * the byte after the 3-byte magic number */
8350 /* match outgoing packets */
8351 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8353 /* match incoming packets */
8354 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8360 * If we have packet meta-data indicating a direction,
8361 * and that metadata can be checked by BPF code, check
8362 * it. Otherwise, give up, as this link-layer type has
8363 * nothing in the packet data.
8365 * Currently, the only platform where a BPF filter can
8366 * check that metadata is Linux with the in-kernel
8367 * BPF interpreter. If other packet capture mechanisms
8368 * and BPF filters also supported this, it would be
8369 * nice. It would be even better if they made that
8370 * metadata available so that we could provide it
8371 * with newer capture APIs, allowing it to be saved
8376 * This is Linux; we require PF_PACKET support.
8377 * If this is a *live* capture, we can look at
8378 * special meta-data in the filter expression;
8379 * if it's a savefile, we can't.
8381 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8382 /* We have a FILE *, so this is a savefile */
8383 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8384 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8387 /* match outgoing packets */
8388 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8391 /* to filter on inbound traffic, invert the match */
8394 #else /* defined(linux) */
8395 bpf_error(cstate
, "inbound/outbound not supported on %s",
8396 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8398 #endif /* defined(linux) */
8403 #ifdef HAVE_NET_PFVAR_H
8404 /* PF firewall log matched interface */
8406 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8412 * Catch errors reported by us and routines below us, and return NULL
8415 if (setjmp(cstate
->top_ctx
))
8418 if (cstate
->linktype
!= DLT_PFLOG
) {
8419 bpf_error(cstate
, "ifname supported only on PF linktype");
8422 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8423 off
= offsetof(struct pfloghdr
, ifname
);
8424 if (strlen(ifname
) >= len
) {
8425 bpf_error(cstate
, "ifname interface names can only be %d characters",
8429 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8430 (const u_char
*)ifname
);
8434 /* PF firewall log ruleset name */
8436 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8441 * Catch errors reported by us and routines below us, and return NULL
8444 if (setjmp(cstate
->top_ctx
))
8447 if (cstate
->linktype
!= DLT_PFLOG
) {
8448 bpf_error(cstate
, "ruleset supported only on PF linktype");
8452 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8453 bpf_error(cstate
, "ruleset names can only be %ld characters",
8454 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8458 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8459 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8463 /* PF firewall log rule number */
8465 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8470 * Catch errors reported by us and routines below us, and return NULL
8473 if (setjmp(cstate
->top_ctx
))
8476 if (cstate
->linktype
!= DLT_PFLOG
) {
8477 bpf_error(cstate
, "rnr supported only on PF linktype");
8481 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8486 /* PF firewall log sub-rule number */
8488 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8493 * Catch errors reported by us and routines below us, and return NULL
8496 if (setjmp(cstate
->top_ctx
))
8499 if (cstate
->linktype
!= DLT_PFLOG
) {
8500 bpf_error(cstate
, "srnr supported only on PF linktype");
8504 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8509 /* PF firewall log reason code */
8511 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8516 * Catch errors reported by us and routines below us, and return NULL
8519 if (setjmp(cstate
->top_ctx
))
8522 if (cstate
->linktype
!= DLT_PFLOG
) {
8523 bpf_error(cstate
, "reason supported only on PF linktype");
8527 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8528 (bpf_u_int32
)reason
);
8532 /* PF firewall log action */
8534 gen_pf_action(compiler_state_t
*cstate
, int action
)
8539 * Catch errors reported by us and routines below us, and return NULL
8542 if (setjmp(cstate
->top_ctx
))
8545 if (cstate
->linktype
!= DLT_PFLOG
) {
8546 bpf_error(cstate
, "action supported only on PF linktype");
8550 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8551 (bpf_u_int32
)action
);
8554 #else /* !HAVE_NET_PFVAR_H */
8556 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8559 * Catch errors reported by us and routines below us, and return NULL
8562 if (setjmp(cstate
->top_ctx
))
8565 bpf_error(cstate
, "libpcap was compiled without pf support");
8570 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8573 * Catch errors reported by us and routines below us, and return NULL
8576 if (setjmp(cstate
->top_ctx
))
8579 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8584 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8587 * Catch errors reported by us and routines below us, and return NULL
8590 if (setjmp(cstate
->top_ctx
))
8593 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8598 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8601 * Catch errors reported by us and routines below us, and return NULL
8604 if (setjmp(cstate
->top_ctx
))
8607 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8612 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8615 * Catch errors reported by us and routines below us, and return NULL
8618 if (setjmp(cstate
->top_ctx
))
8621 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8626 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8629 * Catch errors reported by us and routines below us, and return NULL
8632 if (setjmp(cstate
->top_ctx
))
8635 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8638 #endif /* HAVE_NET_PFVAR_H */
8640 /* IEEE 802.11 wireless header */
8642 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8647 * Catch errors reported by us and routines below us, and return NULL
8650 if (setjmp(cstate
->top_ctx
))
8653 switch (cstate
->linktype
) {
8655 case DLT_IEEE802_11
:
8656 case DLT_PRISM_HEADER
:
8657 case DLT_IEEE802_11_RADIO_AVS
:
8658 case DLT_IEEE802_11_RADIO
:
8659 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8663 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8671 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8676 * Catch errors reported by us and routines below us, and return NULL
8679 if (setjmp(cstate
->top_ctx
))
8682 switch (cstate
->linktype
) {
8684 case DLT_IEEE802_11
:
8685 case DLT_PRISM_HEADER
:
8686 case DLT_IEEE802_11_RADIO_AVS
:
8687 case DLT_IEEE802_11_RADIO
:
8691 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8695 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8696 IEEE80211_FC1_DIR_MASK
);
8702 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8707 * Catch errors reported by us and routines below us, and return NULL
8710 if (setjmp(cstate
->top_ctx
))
8713 switch (cstate
->linktype
) {
8716 case DLT_ARCNET_LINUX
:
8717 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8718 q
.proto
== Q_LINK
) {
8719 cstate
->e
= pcap_ether_aton(s
);
8720 if (cstate
->e
== NULL
)
8721 bpf_error(cstate
, "malloc");
8722 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8727 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8731 bpf_error(cstate
, "aid supported only on ARCnet");
8736 static struct block
*
8737 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8739 register struct block
*b0
, *b1
;
8742 /* src comes first, different from Ethernet */
8744 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8747 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8750 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8751 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8757 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8758 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8763 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8767 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8771 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8775 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8779 bpf_error(cstate
, "'ra' is only supported on 802.11");
8783 bpf_error(cstate
, "'ta' is only supported on 802.11");
8790 static struct block
*
8791 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8793 struct block
*b0
, *b1
;
8795 /* check for VLAN, including QinQ */
8796 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8797 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8800 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8806 static struct block
*
8807 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8809 if (vlan_num
> 0x0fff) {
8810 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8813 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8816 static struct block
*
8817 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8820 struct block
*b0
, *b1
;
8822 b0
= gen_vlan_tpid_test(cstate
);
8825 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8831 * Both payload and link header type follow the VLAN tags so that
8832 * both need to be updated.
8834 cstate
->off_linkpl
.constant_part
+= 4;
8835 cstate
->off_linktype
.constant_part
+= 4;
8840 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8841 /* add v to variable part of off */
8843 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8844 bpf_u_int32 v
, struct slist
*s
)
8848 if (!off
->is_variable
)
8849 off
->is_variable
= 1;
8851 off
->reg
= alloc_reg(cstate
);
8853 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8856 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8859 s2
= new_stmt(cstate
, BPF_ST
);
8865 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8866 * and link type offsets first
8869 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8873 /* offset determined at run time, shift variable part */
8875 cstate
->is_vlan_vloffset
= 1;
8876 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8877 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8879 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8880 sappend(s
.next
, b_tpid
->head
->stmts
);
8881 b_tpid
->head
->stmts
= s
.next
;
8885 * patch block b_vid (VLAN id test) to load VID value either from packet
8886 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8889 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8891 struct slist
*s
, *s2
, *sjeq
;
8894 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8895 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8897 /* true -> next instructions, false -> beginning of b_vid */
8898 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8900 sjeq
->s
.jf
= b_vid
->stmts
;
8903 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8904 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8908 /* Jump to the test in b_vid. We need to jump one instruction before
8909 * the end of the b_vid block so that we only skip loading the TCI
8910 * from packet data and not the 'and' instruction extractging VID.
8913 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8915 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8919 /* insert our statements at the beginning of b_vid */
8920 sappend(s
, b_vid
->stmts
);
8925 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8926 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8927 * tag can be either in metadata or in packet data; therefore if the
8928 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8929 * header for VLAN tag. As the decision is done at run time, we need
8930 * update variable part of the offsets
8932 static struct block
*
8933 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8936 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8939 /* generate new filter code based on extracting packet
8941 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8942 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8944 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8949 * This is tricky. We need to insert the statements updating variable
8950 * parts of offsets before the traditional TPID and VID tests so
8951 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8952 * we do not want this update to affect those checks. That's why we
8953 * generate both test blocks first and insert the statements updating
8954 * variable parts of both offsets after that. This wouldn't work if
8955 * there already were variable length link header when entering this
8956 * function but gen_vlan_bpf_extensions() isn't called in that case.
8958 b_tpid
= gen_vlan_tpid_test(cstate
);
8960 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8962 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8967 gen_vlan_patch_vid_test(cstate
, b_vid
);
8977 * support IEEE 802.1Q VLAN trunk over ethernet
8980 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8985 * Catch errors reported by us and routines below us, and return NULL
8988 if (setjmp(cstate
->top_ctx
))
8991 /* can't check for VLAN-encapsulated packets inside MPLS */
8992 if (cstate
->label_stack_depth
> 0)
8993 bpf_error(cstate
, "no VLAN match after MPLS");
8996 * Check for a VLAN packet, and then change the offsets to point
8997 * to the type and data fields within the VLAN packet. Just
8998 * increment the offsets, so that we can support a hierarchy, e.g.
8999 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
9002 * XXX - this is a bit of a kludge. If we were to split the
9003 * compiler into a parser that parses an expression and
9004 * generates an expression tree, and a code generator that
9005 * takes an expression tree (which could come from our
9006 * parser or from some other parser) and generates BPF code,
9007 * we could perhaps make the offsets parameters of routines
9008 * and, in the handler for an "AND" node, pass to subnodes
9009 * other than the VLAN node the adjusted offsets.
9011 * This would mean that "vlan" would, instead of changing the
9012 * behavior of *all* tests after it, change only the behavior
9013 * of tests ANDed with it. That would change the documented
9014 * semantics of "vlan", which might break some expressions.
9015 * However, it would mean that "(vlan and ip) or ip" would check
9016 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9017 * checking only for VLAN-encapsulated IP, so that could still
9018 * be considered worth doing; it wouldn't break expressions
9019 * that are of the form "vlan and ..." or "vlan N and ...",
9020 * which I suspect are the most common expressions involving
9021 * "vlan". "vlan or ..." doesn't necessarily do what the user
9022 * would really want, now, as all the "or ..." tests would
9023 * be done assuming a VLAN, even though the "or" could be viewed
9024 * as meaning "or, if this isn't a VLAN packet...".
9026 switch (cstate
->linktype
) {
9029 case DLT_NETANALYZER
:
9030 case DLT_NETANALYZER_TRANSPARENT
:
9031 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9032 /* Verify that this is the outer part of the packet and
9033 * not encapsulated somehow. */
9034 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9035 cstate
->off_linkhdr
.constant_part
==
9036 cstate
->off_outermostlinkhdr
.constant_part
) {
9038 * Do we need special VLAN handling?
9040 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9041 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9044 b0
= gen_vlan_no_bpf_extensions(cstate
,
9045 vlan_num
, has_vlan_tag
);
9048 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9052 case DLT_IEEE802_11
:
9053 case DLT_PRISM_HEADER
:
9054 case DLT_IEEE802_11_RADIO_AVS
:
9055 case DLT_IEEE802_11_RADIO
:
9056 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9060 bpf_error(cstate
, "no VLAN support for %s",
9061 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9065 cstate
->vlan_stack_depth
++;
9073 * The label_num_arg dance is to avoid annoying whining by compilers that
9074 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9075 * It's not *used* after setjmp returns.
9078 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9081 volatile bpf_u_int32 label_num
= label_num_arg
;
9082 struct block
*b0
, *b1
;
9085 * Catch errors reported by us and routines below us, and return NULL
9088 if (setjmp(cstate
->top_ctx
))
9091 if (cstate
->label_stack_depth
> 0) {
9092 /* just match the bottom-of-stack bit clear */
9093 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9096 * We're not in an MPLS stack yet, so check the link-layer
9097 * type against MPLS.
9099 switch (cstate
->linktype
) {
9101 case DLT_C_HDLC
: /* fall through */
9103 case DLT_NETANALYZER
:
9104 case DLT_NETANALYZER_TRANSPARENT
:
9105 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9109 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9112 /* FIXME add other DLT_s ...
9113 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9114 * leave it for now */
9117 bpf_error(cstate
, "no MPLS support for %s",
9118 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9123 /* If a specific MPLS label is requested, check it */
9124 if (has_label_num
) {
9125 if (label_num
> 0xFFFFF) {
9126 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9127 label_num
, 0xFFFFF);
9129 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9130 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9131 0xfffff000); /* only compare the first 20 bits */
9137 * Change the offsets to point to the type and data fields within
9138 * the MPLS packet. Just increment the offsets, so that we
9139 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9140 * capture packets with an outer label of 100000 and an inner
9143 * Increment the MPLS stack depth as well; this indicates that
9144 * we're checking MPLS-encapsulated headers, to make sure higher
9145 * level code generators don't try to match against IP-related
9146 * protocols such as Q_ARP, Q_RARP etc.
9148 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9150 cstate
->off_nl_nosnap
+= 4;
9151 cstate
->off_nl
+= 4;
9152 cstate
->label_stack_depth
++;
9157 * Support PPPOE discovery and session.
9160 gen_pppoed(compiler_state_t
*cstate
)
9163 * Catch errors reported by us and routines below us, and return NULL
9166 if (setjmp(cstate
->top_ctx
))
9169 /* check for PPPoE discovery */
9170 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9174 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9176 struct block
*b0
, *b1
;
9179 * Catch errors reported by us and routines below us, and return NULL
9182 if (setjmp(cstate
->top_ctx
))
9186 * Test against the PPPoE session link-layer type.
9188 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9190 /* If a specific session is requested, check PPPoE session id */
9192 if (sess_num
> 0x0000ffff) {
9193 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9194 sess_num
, 0x0000ffff);
9196 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9202 * Change the offsets to point to the type and data fields within
9203 * the PPP packet, and note that this is PPPoE rather than
9206 * XXX - this is a bit of a kludge. See the comments in
9209 * The "network-layer" protocol is PPPoE, which has a 6-byte
9210 * PPPoE header, followed by a PPP packet.
9212 * There is no HDLC encapsulation for the PPP packet (it's
9213 * encapsulated in PPPoES instead), so the link-layer type
9214 * starts at the first byte of the PPP packet. For PPPoE,
9215 * that offset is relative to the beginning of the total
9216 * link-layer payload, including any 802.2 LLC header, so
9217 * it's 6 bytes past cstate->off_nl.
9219 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9220 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9221 cstate
->off_linkpl
.reg
);
9223 cstate
->off_linktype
= cstate
->off_linkhdr
;
9224 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9227 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9232 /* Check that this is Geneve and the VNI is correct if
9233 * specified. Parameterized to handle both IPv4 and IPv6. */
9234 static struct block
*
9235 gen_geneve_check(compiler_state_t
*cstate
,
9236 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9237 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9239 struct block
*b0
, *b1
;
9241 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9243 /* Check that we are operating on version 0. Otherwise, we
9244 * can't decode the rest of the fields. The version is 2 bits
9245 * in the first byte of the Geneve header. */
9246 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9251 if (vni
> 0xffffff) {
9252 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9255 vni
<<= 8; /* VNI is in the upper 3 bytes */
9256 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9264 /* The IPv4 and IPv6 Geneve checks need to do two things:
9265 * - Verify that this actually is Geneve with the right VNI.
9266 * - Place the IP header length (plus variable link prefix if
9267 * needed) into register A to be used later to compute
9268 * the inner packet offsets. */
9269 static struct block
*
9270 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9272 struct block
*b0
, *b1
;
9273 struct slist
*s
, *s1
;
9275 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9277 /* Load the IP header length into A. */
9278 s
= gen_loadx_iphdrlen(cstate
);
9280 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9283 /* Forcibly append these statements to the true condition
9284 * of the protocol check by creating a new block that is
9285 * always true and ANDing them. */
9286 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9295 static struct block
*
9296 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9298 struct block
*b0
, *b1
;
9299 struct slist
*s
, *s1
;
9301 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9303 /* Load the IP header length. We need to account for a
9304 * variable length link prefix if there is one. */
9305 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9307 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9311 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9315 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9319 /* Forcibly append these statements to the true condition
9320 * of the protocol check by creating a new block that is
9321 * always true and ANDing them. */
9322 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9325 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9334 /* We need to store three values based on the Geneve header::
9335 * - The offset of the linktype.
9336 * - The offset of the end of the Geneve header.
9337 * - The offset of the end of the encapsulated MAC header. */
9338 static struct slist
*
9339 gen_geneve_offsets(compiler_state_t
*cstate
)
9341 struct slist
*s
, *s1
, *s_proto
;
9343 /* First we need to calculate the offset of the Geneve header
9344 * itself. This is composed of the IP header previously calculated
9345 * (include any variable link prefix) and stored in A plus the
9346 * fixed sized headers (fixed link prefix, MAC length, and UDP
9348 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9349 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9351 /* Stash this in X since we'll need it later. */
9352 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9355 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9357 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9361 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9362 cstate
->off_linktype
.is_variable
= 1;
9363 cstate
->off_linktype
.constant_part
= 0;
9365 s1
= new_stmt(cstate
, BPF_ST
);
9366 s1
->s
.k
= cstate
->off_linktype
.reg
;
9369 /* Load the Geneve option length and mask and shift to get the
9370 * number of bytes. It is stored in the first byte of the Geneve
9372 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9376 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9380 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9384 /* Add in the rest of the Geneve base header. */
9385 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9389 /* Add the Geneve header length to its offset and store. */
9390 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9394 /* Set the encapsulated type as Ethernet. Even though we may
9395 * not actually have Ethernet inside there are two reasons this
9397 * - The linktype field is always in EtherType format regardless
9398 * of whether it is in Geneve or an inner Ethernet frame.
9399 * - The only link layer that we have specific support for is
9400 * Ethernet. We will confirm that the packet actually is
9401 * Ethernet at runtime before executing these checks. */
9402 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9404 s1
= new_stmt(cstate
, BPF_ST
);
9405 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9408 /* Calculate whether we have an Ethernet header or just raw IP/
9409 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9410 * and linktype by 14 bytes so that the network header can be found
9411 * seamlessly. Otherwise, keep what we've calculated already. */
9413 /* We have a bare jmp so we can't use the optimizer. */
9414 cstate
->no_optimize
= 1;
9416 /* Load the EtherType in the Geneve header, 2 bytes in. */
9417 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9421 /* Load X with the end of the Geneve header. */
9422 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9423 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9426 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9427 * end of this check, we should have the total length in X. In
9428 * the non-Ethernet case, it's already there. */
9429 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9430 s_proto
->s
.k
= ETHERTYPE_TEB
;
9431 sappend(s
, s_proto
);
9433 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9437 /* Since this is Ethernet, use the EtherType of the payload
9438 * directly as the linktype. Overwrite what we already have. */
9439 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9443 s1
= new_stmt(cstate
, BPF_ST
);
9444 s1
->s
.k
= cstate
->off_linktype
.reg
;
9447 /* Advance two bytes further to get the end of the Ethernet
9449 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9453 /* Move the result to X. */
9454 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9457 /* Store the final result of our linkpl calculation. */
9458 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9459 cstate
->off_linkpl
.is_variable
= 1;
9460 cstate
->off_linkpl
.constant_part
= 0;
9462 s1
= new_stmt(cstate
, BPF_STX
);
9463 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9472 /* Check to see if this is a Geneve packet. */
9474 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9476 struct block
*b0
, *b1
;
9480 * Catch errors reported by us and routines below us, and return NULL
9483 if (setjmp(cstate
->top_ctx
))
9486 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9487 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9492 /* Later filters should act on the payload of the Geneve frame,
9493 * update all of the header pointers. Attach this code so that
9494 * it gets executed in the event that the Geneve filter matches. */
9495 s
= gen_geneve_offsets(cstate
);
9497 b1
= gen_true(cstate
);
9498 sappend(s
, b1
->stmts
);
9503 cstate
->is_geneve
= 1;
9508 /* Check that the encapsulated frame has a link layer header
9509 * for Ethernet filters. */
9510 static struct block
*
9511 gen_geneve_ll_check(compiler_state_t
*cstate
)
9514 struct slist
*s
, *s1
;
9516 /* The easiest way to see if there is a link layer present
9517 * is to check if the link layer header and payload are not
9520 /* Geneve always generates pure variable offsets so we can
9521 * compare only the registers. */
9522 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9523 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9525 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9526 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9529 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9537 static struct block
*
9538 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9539 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9546 if (!cstate
->is_atm
)
9547 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9548 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9550 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9551 0xffffffffU
, jtype
, reverse
, jvalue
);
9555 if (!cstate
->is_atm
)
9556 bpf_error(cstate
, "'vci' supported only on raw ATM");
9557 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9559 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9560 0xffffffffU
, jtype
, reverse
, jvalue
);
9564 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9565 abort(); /* XXX - this isn't on FreeBSD */
9566 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9567 0x0fU
, jtype
, reverse
, jvalue
);
9571 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9573 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9574 0xffffffffU
, jtype
, reverse
, jvalue
);
9578 if (!cstate
->is_atm
)
9579 bpf_error(cstate
, "'callref' supported only on raw ATM");
9580 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9582 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9583 0xffffffffU
, jtype
, reverse
, jvalue
);
9592 static struct block
*
9593 gen_atmtype_metac(compiler_state_t
*cstate
)
9595 struct block
*b0
, *b1
;
9597 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9598 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9603 static struct block
*
9604 gen_atmtype_sc(compiler_state_t
*cstate
)
9606 struct block
*b0
, *b1
;
9608 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9609 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9614 static struct block
*
9615 gen_atmtype_llc(compiler_state_t
*cstate
)
9619 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9620 cstate
->linktype
= cstate
->prevlinktype
;
9625 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9626 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9629 * Catch errors reported by us and routines below us, and return NULL
9632 if (setjmp(cstate
->top_ctx
))
9635 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9640 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9642 struct block
*b0
, *b1
;
9645 * Catch errors reported by us and routines below us, and return NULL
9648 if (setjmp(cstate
->top_ctx
))
9654 /* Get all packets in Meta signalling Circuit */
9655 if (!cstate
->is_atm
)
9656 bpf_error(cstate
, "'metac' supported only on raw ATM");
9657 b1
= gen_atmtype_metac(cstate
);
9661 /* Get all packets in Broadcast Circuit*/
9662 if (!cstate
->is_atm
)
9663 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9664 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9665 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9670 /* Get all cells in Segment OAM F4 circuit*/
9671 if (!cstate
->is_atm
)
9672 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9673 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9674 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9679 /* Get all cells in End-to-End OAM F4 Circuit*/
9680 if (!cstate
->is_atm
)
9681 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9682 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9683 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9688 /* Get all packets in connection Signalling Circuit */
9689 if (!cstate
->is_atm
)
9690 bpf_error(cstate
, "'sc' supported only on raw ATM");
9691 b1
= gen_atmtype_sc(cstate
);
9695 /* Get all packets in ILMI Circuit */
9696 if (!cstate
->is_atm
)
9697 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9698 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9699 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9704 /* Get all LANE packets */
9705 if (!cstate
->is_atm
)
9706 bpf_error(cstate
, "'lane' supported only on raw ATM");
9707 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9710 * Arrange that all subsequent tests assume LANE
9711 * rather than LLC-encapsulated packets, and set
9712 * the offsets appropriately for LANE-encapsulated
9715 * We assume LANE means Ethernet, not Token Ring.
9717 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9718 cstate
->off_payload
+ 2, /* Ethernet header */
9720 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9721 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9722 cstate
->off_nl
= 0; /* Ethernet II */
9723 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9727 /* Get all LLC-encapsulated packets */
9728 if (!cstate
->is_atm
)
9729 bpf_error(cstate
, "'llc' supported only on raw ATM");
9730 b1
= gen_atmtype_llc(cstate
);
9740 * Filtering for MTP2 messages based on li value
9741 * FISU, length is null
9742 * LSSU, length is 1 or 2
9743 * MSU, length is 3 or more
9744 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9747 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9749 struct block
*b0
, *b1
;
9752 * Catch errors reported by us and routines below us, and return NULL
9755 if (setjmp(cstate
->top_ctx
))
9761 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9762 (cstate
->linktype
!= DLT_ERF
) &&
9763 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9764 bpf_error(cstate
, "'fisu' supported only on MTP2");
9765 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9766 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9767 0x3fU
, BPF_JEQ
, 0, 0U);
9771 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9772 (cstate
->linktype
!= DLT_ERF
) &&
9773 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9774 bpf_error(cstate
, "'lssu' supported only on MTP2");
9775 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9776 0x3fU
, BPF_JGT
, 1, 2U);
9777 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9778 0x3fU
, BPF_JGT
, 0, 0U);
9783 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9784 (cstate
->linktype
!= DLT_ERF
) &&
9785 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9786 bpf_error(cstate
, "'msu' supported only on MTP2");
9787 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9788 0x3fU
, BPF_JGT
, 0, 2U);
9792 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9793 (cstate
->linktype
!= DLT_ERF
) &&
9794 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9795 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9796 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9797 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9798 0xff80U
, BPF_JEQ
, 0, 0U);
9802 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9803 (cstate
->linktype
!= DLT_ERF
) &&
9804 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9805 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9806 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9807 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9808 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9809 0xff80U
, BPF_JGT
, 0, 0U);
9814 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9815 (cstate
->linktype
!= DLT_ERF
) &&
9816 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9817 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9818 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9819 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9829 * The jvalue_arg dance is to avoid annoying whining by compilers that
9830 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9831 * It's not *used* after setjmp returns.
9834 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9835 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
9837 volatile bpf_u_int32 jvalue
= jvalue_arg
;
9839 bpf_u_int32 val1
, val2
, val3
;
9846 * Catch errors reported by us and routines below us, and return NULL
9849 if (setjmp(cstate
->top_ctx
))
9852 newoff_sio
= cstate
->off_sio
;
9853 newoff_opc
= cstate
->off_opc
;
9854 newoff_dpc
= cstate
->off_dpc
;
9855 newoff_sls
= cstate
->off_sls
;
9856 switch (mtp3field
) {
9859 newoff_sio
+= 3; /* offset for MTP2_HSL */
9863 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9864 bpf_error(cstate
, "'sio' supported only on SS7");
9865 /* sio coded on 1 byte so max value 255 */
9867 bpf_error(cstate
, "sio value %u too big; max value = 255",
9869 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
9870 jtype
, reverse
, jvalue
);
9878 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9879 bpf_error(cstate
, "'opc' supported only on SS7");
9880 /* opc coded on 14 bits so max value 16383 */
9882 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9884 /* the following instructions are made to convert jvalue
9885 * to the form used to write opc in an ss7 message*/
9886 val1
= jvalue
& 0x00003c00;
9888 val2
= jvalue
& 0x000003fc;
9890 val3
= jvalue
& 0x00000003;
9892 jvalue
= val1
+ val2
+ val3
;
9893 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
9894 jtype
, reverse
, jvalue
);
9902 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9903 bpf_error(cstate
, "'dpc' supported only on SS7");
9904 /* dpc coded on 14 bits so max value 16383 */
9906 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9908 /* the following instructions are made to convert jvalue
9909 * to the forme used to write dpc in an ss7 message*/
9910 val1
= jvalue
& 0x000000ff;
9912 val2
= jvalue
& 0x00003f00;
9914 jvalue
= val1
+ val2
;
9915 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
9916 jtype
, reverse
, jvalue
);
9924 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9925 bpf_error(cstate
, "'sls' supported only on SS7");
9926 /* sls coded on 4 bits so max value 15 */
9928 bpf_error(cstate
, "sls value %u too big; max value = 15",
9930 /* the following instruction is made to convert jvalue
9931 * to the forme used to write sls in an ss7 message*/
9932 jvalue
= jvalue
<< 4;
9933 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
9934 jtype
, reverse
, jvalue
);
9943 static struct block
*
9944 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9949 * Q.2931 signalling protocol messages for handling virtual circuits
9950 * establishment and teardown
9955 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9959 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9963 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9967 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9971 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9974 case A_RELEASE_DONE
:
9975 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9985 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9987 struct block
*b0
, *b1
;
9990 * Catch errors reported by us and routines below us, and return NULL
9993 if (setjmp(cstate
->top_ctx
))
9999 if (!cstate
->is_atm
)
10000 bpf_error(cstate
, "'oam' supported only on raw ATM");
10002 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10003 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10005 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10010 if (!cstate
->is_atm
)
10011 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10013 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10014 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10016 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10022 * Get Q.2931 signalling messages for switched
10023 * virtual connection
10025 if (!cstate
->is_atm
)
10026 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10027 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10028 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10030 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10032 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10034 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10036 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10038 b0
= gen_atmtype_sc(cstate
);
10042 case A_METACONNECT
:
10043 if (!cstate
->is_atm
)
10044 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10045 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10046 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10048 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10050 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10052 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10054 b0
= gen_atmtype_metac(cstate
);