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_HDLC
: /* NetBSD (Cisco) HDLC */
1272 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1273 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1274 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1276 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1281 * This does no include the Ethernet header, and
1282 * only covers session state.
1284 cstate
->off_linktype
.constant_part
= 6;
1285 cstate
->off_linkpl
.constant_part
= 8;
1287 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1291 cstate
->off_linktype
.constant_part
= 5;
1292 cstate
->off_linkpl
.constant_part
= 24;
1294 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1299 * FDDI doesn't really have a link-level type field.
1300 * We set "off_linktype" to the offset of the LLC header.
1302 * To check for Ethernet types, we assume that SSAP = SNAP
1303 * is being used and pick out the encapsulated Ethernet type.
1304 * XXX - should we generate code to check for SNAP?
1306 cstate
->off_linktype
.constant_part
= 13;
1307 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1308 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1309 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1310 cstate
->off_nl
= 8; /* 802.2+SNAP */
1311 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1316 * Token Ring doesn't really have a link-level type field.
1317 * We set "off_linktype" to the offset of the LLC header.
1319 * To check for Ethernet types, we assume that SSAP = SNAP
1320 * is being used and pick out the encapsulated Ethernet type.
1321 * XXX - should we generate code to check for SNAP?
1323 * XXX - the header is actually variable-length.
1324 * Some various Linux patched versions gave 38
1325 * as "off_linktype" and 40 as "off_nl"; however,
1326 * if a token ring packet has *no* routing
1327 * information, i.e. is not source-routed, the correct
1328 * values are 20 and 22, as they are in the vanilla code.
1330 * A packet is source-routed iff the uppermost bit
1331 * of the first byte of the source address, at an
1332 * offset of 8, has the uppermost bit set. If the
1333 * packet is source-routed, the total number of bytes
1334 * of routing information is 2 plus bits 0x1F00 of
1335 * the 16-bit value at an offset of 14 (shifted right
1336 * 8 - figure out which byte that is).
1338 cstate
->off_linktype
.constant_part
= 14;
1339 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1340 cstate
->off_nl
= 8; /* 802.2+SNAP */
1341 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1344 case DLT_PRISM_HEADER
:
1345 case DLT_IEEE802_11_RADIO_AVS
:
1346 case DLT_IEEE802_11_RADIO
:
1347 cstate
->off_linkhdr
.is_variable
= 1;
1348 /* Fall through, 802.11 doesn't have a variable link
1349 * prefix but is otherwise the same. */
1352 case DLT_IEEE802_11
:
1354 * 802.11 doesn't really have a link-level type field.
1355 * We set "off_linktype.constant_part" to the offset of
1358 * To check for Ethernet types, we assume that SSAP = SNAP
1359 * is being used and pick out the encapsulated Ethernet type.
1360 * XXX - should we generate code to check for SNAP?
1362 * We also handle variable-length radio headers here.
1363 * The Prism header is in theory variable-length, but in
1364 * practice it's always 144 bytes long. However, some
1365 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1366 * sometimes or always supply an AVS header, so we
1367 * have to check whether the radio header is a Prism
1368 * header or an AVS header, so, in practice, it's
1371 cstate
->off_linktype
.constant_part
= 24;
1372 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1373 cstate
->off_linkpl
.is_variable
= 1;
1374 cstate
->off_nl
= 8; /* 802.2+SNAP */
1375 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1380 * At the moment we treat PPI the same way that we treat
1381 * normal Radiotap encoded packets. The difference is in
1382 * the function that generates the code at the beginning
1383 * to compute the header length. Since this code generator
1384 * of PPI supports bare 802.11 encapsulation only (i.e.
1385 * the encapsulated DLT should be DLT_IEEE802_11) we
1386 * generate code to check for this too.
1388 cstate
->off_linktype
.constant_part
= 24;
1389 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1390 cstate
->off_linkpl
.is_variable
= 1;
1391 cstate
->off_linkhdr
.is_variable
= 1;
1392 cstate
->off_nl
= 8; /* 802.2+SNAP */
1393 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1396 case DLT_ATM_RFC1483
:
1397 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1399 * assume routed, non-ISO PDUs
1400 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1402 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1403 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1404 * latter would presumably be treated the way PPPoE
1405 * should be, so you can do "pppoe and udp port 2049"
1406 * or "pppoa and tcp port 80" and have it check for
1407 * PPPo{A,E} and a PPP protocol of IP and....
1409 cstate
->off_linktype
.constant_part
= 0;
1410 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1411 cstate
->off_nl
= 8; /* 802.2+SNAP */
1412 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1417 * Full Frontal ATM; you get AALn PDUs with an ATM
1421 cstate
->off_vpi
= SUNATM_VPI_POS
;
1422 cstate
->off_vci
= SUNATM_VCI_POS
;
1423 cstate
->off_proto
= PROTO_POS
;
1424 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1425 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1426 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1427 cstate
->off_nl
= 8; /* 802.2+SNAP */
1428 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1434 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1435 cstate
->off_linkpl
.constant_part
= 0;
1437 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1440 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1441 cstate
->off_linktype
.constant_part
= 14;
1442 cstate
->off_linkpl
.constant_part
= 16;
1444 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1447 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1448 cstate
->off_linktype
.constant_part
= 0;
1449 cstate
->off_linkpl
.constant_part
= 20;
1451 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1456 * LocalTalk does have a 1-byte type field in the LLAP header,
1457 * but really it just indicates whether there is a "short" or
1458 * "long" DDP packet following.
1460 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1461 cstate
->off_linkpl
.constant_part
= 0;
1463 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1466 case DLT_IP_OVER_FC
:
1468 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1469 * link-level type field. We set "off_linktype" to the
1470 * offset of the LLC header.
1472 * To check for Ethernet types, we assume that SSAP = SNAP
1473 * is being used and pick out the encapsulated Ethernet type.
1474 * XXX - should we generate code to check for SNAP? RFC
1475 * 2625 says SNAP should be used.
1477 cstate
->off_linktype
.constant_part
= 16;
1478 cstate
->off_linkpl
.constant_part
= 16;
1479 cstate
->off_nl
= 8; /* 802.2+SNAP */
1480 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1485 * XXX - we should set this to handle SNAP-encapsulated
1486 * frames (NLPID of 0x80).
1488 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1489 cstate
->off_linkpl
.constant_part
= 0;
1491 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1495 * the only BPF-interesting FRF.16 frames are non-control frames;
1496 * Frame Relay has a variable length link-layer
1497 * so lets start with offset 4 for now and increments later on (FIXME);
1500 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1501 cstate
->off_linkpl
.constant_part
= 0;
1503 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1506 case DLT_APPLE_IP_OVER_IEEE1394
:
1507 cstate
->off_linktype
.constant_part
= 16;
1508 cstate
->off_linkpl
.constant_part
= 18;
1510 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1513 case DLT_SYMANTEC_FIREWALL
:
1514 cstate
->off_linktype
.constant_part
= 6;
1515 cstate
->off_linkpl
.constant_part
= 44;
1516 cstate
->off_nl
= 0; /* Ethernet II */
1517 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1520 #ifdef HAVE_NET_PFVAR_H
1522 cstate
->off_linktype
.constant_part
= 0;
1523 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1525 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1529 case DLT_JUNIPER_MFR
:
1530 case DLT_JUNIPER_MLFR
:
1531 case DLT_JUNIPER_MLPPP
:
1532 case DLT_JUNIPER_PPP
:
1533 case DLT_JUNIPER_CHDLC
:
1534 case DLT_JUNIPER_FRELAY
:
1535 cstate
->off_linktype
.constant_part
= 4;
1536 cstate
->off_linkpl
.constant_part
= 4;
1538 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1541 case DLT_JUNIPER_ATM1
:
1542 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1543 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1545 cstate
->off_nl_nosnap
= 10;
1548 case DLT_JUNIPER_ATM2
:
1549 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1550 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1552 cstate
->off_nl_nosnap
= 10;
1555 /* frames captured on a Juniper PPPoE service PIC
1556 * contain raw ethernet frames */
1557 case DLT_JUNIPER_PPPOE
:
1558 case DLT_JUNIPER_ETHER
:
1559 cstate
->off_linkpl
.constant_part
= 14;
1560 cstate
->off_linktype
.constant_part
= 16;
1561 cstate
->off_nl
= 18; /* Ethernet II */
1562 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1565 case DLT_JUNIPER_PPPOE_ATM
:
1566 cstate
->off_linktype
.constant_part
= 4;
1567 cstate
->off_linkpl
.constant_part
= 6;
1569 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1572 case DLT_JUNIPER_GGSN
:
1573 cstate
->off_linktype
.constant_part
= 6;
1574 cstate
->off_linkpl
.constant_part
= 12;
1576 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1579 case DLT_JUNIPER_ES
:
1580 cstate
->off_linktype
.constant_part
= 6;
1581 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1582 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1583 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1586 case DLT_JUNIPER_MONITOR
:
1587 cstate
->off_linktype
.constant_part
= 12;
1588 cstate
->off_linkpl
.constant_part
= 12;
1589 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1590 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1593 case DLT_BACNET_MS_TP
:
1594 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1595 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1596 cstate
->off_nl
= OFFSET_NOT_SET
;
1597 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1600 case DLT_JUNIPER_SERVICES
:
1601 cstate
->off_linktype
.constant_part
= 12;
1602 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1603 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1604 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1607 case DLT_JUNIPER_VP
:
1608 cstate
->off_linktype
.constant_part
= 18;
1609 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1610 cstate
->off_nl
= OFFSET_NOT_SET
;
1611 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1614 case DLT_JUNIPER_ST
:
1615 cstate
->off_linktype
.constant_part
= 18;
1616 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1617 cstate
->off_nl
= OFFSET_NOT_SET
;
1618 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1621 case DLT_JUNIPER_ISM
:
1622 cstate
->off_linktype
.constant_part
= 8;
1623 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1624 cstate
->off_nl
= OFFSET_NOT_SET
;
1625 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1628 case DLT_JUNIPER_VS
:
1629 case DLT_JUNIPER_SRX_E2E
:
1630 case DLT_JUNIPER_FIBRECHANNEL
:
1631 case DLT_JUNIPER_ATM_CEMIC
:
1632 cstate
->off_linktype
.constant_part
= 8;
1633 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1634 cstate
->off_nl
= OFFSET_NOT_SET
;
1635 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1640 cstate
->off_li_hsl
= 4;
1641 cstate
->off_sio
= 3;
1642 cstate
->off_opc
= 4;
1643 cstate
->off_dpc
= 4;
1644 cstate
->off_sls
= 7;
1645 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1646 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1647 cstate
->off_nl
= OFFSET_NOT_SET
;
1648 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1651 case DLT_MTP2_WITH_PHDR
:
1653 cstate
->off_li_hsl
= 8;
1654 cstate
->off_sio
= 7;
1655 cstate
->off_opc
= 8;
1656 cstate
->off_dpc
= 8;
1657 cstate
->off_sls
= 11;
1658 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1659 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1660 cstate
->off_nl
= OFFSET_NOT_SET
;
1661 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1665 cstate
->off_li
= 22;
1666 cstate
->off_li_hsl
= 24;
1667 cstate
->off_sio
= 23;
1668 cstate
->off_opc
= 24;
1669 cstate
->off_dpc
= 24;
1670 cstate
->off_sls
= 27;
1671 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1672 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1673 cstate
->off_nl
= OFFSET_NOT_SET
;
1674 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1678 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1679 cstate
->off_linkpl
.constant_part
= 4;
1681 cstate
->off_nl_nosnap
= 0;
1686 * Currently, only raw "link[N:M]" filtering is supported.
1688 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1689 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1690 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1691 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1695 cstate
->off_linktype
.constant_part
= 1;
1696 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1698 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1701 case DLT_NETANALYZER
:
1702 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1703 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1704 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1705 cstate
->off_nl
= 0; /* Ethernet II */
1706 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1709 case DLT_NETANALYZER_TRANSPARENT
:
1710 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1711 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1712 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1713 cstate
->off_nl
= 0; /* Ethernet II */
1714 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1719 * For values in the range in which we've assigned new
1720 * DLT_ values, only raw "link[N:M]" filtering is supported.
1722 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1723 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1724 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1725 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1726 cstate
->off_nl
= OFFSET_NOT_SET
;
1727 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1729 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1730 cstate
->linktype
, DLT_MATCHING_MIN
, DLT_MATCHING_MAX
);
1736 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1741 * Load a value relative to the specified absolute offset.
1743 static struct slist
*
1744 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1745 u_int offset
, u_int size
)
1747 struct slist
*s
, *s2
;
1749 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1752 * If "s" is non-null, it has code to arrange that the X register
1753 * contains the variable part of the absolute offset, so we
1754 * generate a load relative to that, with an offset of
1755 * abs_offset->constant_part + offset.
1757 * Otherwise, we can do an absolute load with an offset of
1758 * abs_offset->constant_part + offset.
1762 * "s" points to a list of statements that puts the
1763 * variable part of the absolute offset into the X register.
1764 * Do an indirect load, to use the X register as an offset.
1766 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1767 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1771 * There is no variable part of the absolute offset, so
1772 * just do an absolute load.
1774 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1775 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1781 * Load a value relative to the beginning of the specified header.
1783 static struct slist
*
1784 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1787 struct slist
*s
, *s2
;
1790 * Squelch warnings from compilers that *don't* assume that
1791 * offrel always has a valid enum value and therefore don't
1792 * assume that we'll always go through one of the case arms.
1794 * If we have a default case, compilers that *do* assume that
1795 * will then complain about the default case code being
1798 * Damned if you do, damned if you don't.
1805 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1810 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1813 case OR_PREVLINKHDR
:
1814 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1818 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1821 case OR_PREVMPLSHDR
:
1822 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1826 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1829 case OR_LINKPL_NOSNAP
:
1830 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1834 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1839 * Load the X register with the length of the IPv4 header
1840 * (plus the offset of the link-layer header, if it's
1841 * preceded by a variable-length header such as a radio
1842 * header), in bytes.
1844 s
= gen_loadx_iphdrlen(cstate
);
1847 * Load the item at {offset of the link-layer payload} +
1848 * {offset, relative to the start of the link-layer
1849 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1850 * {specified offset}.
1852 * If the offset of the link-layer payload is variable,
1853 * the variable part of that offset is included in the
1854 * value in the X register, and we include the constant
1855 * part in the offset of the load.
1857 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1858 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1863 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1870 * Generate code to load into the X register the sum of the length of
1871 * the IPv4 header and the variable part of the offset of the link-layer
1874 static struct slist
*
1875 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1877 struct slist
*s
, *s2
;
1879 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1882 * The offset of the link-layer payload has a variable
1883 * part. "s" points to a list of statements that put
1884 * the variable part of that offset into the X register.
1886 * The 4*([k]&0xf) addressing mode can't be used, as we
1887 * don't have a constant offset, so we have to load the
1888 * value in question into the A register and add to it
1889 * the value from the X register.
1891 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1892 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1894 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1897 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1902 * The A register now contains the length of the IP header.
1903 * We need to add to it the variable part of the offset of
1904 * the link-layer payload, which is still in the X
1905 * register, and move the result into the X register.
1907 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1908 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1911 * The offset of the link-layer payload is a constant,
1912 * so no code was generated to load the (non-existent)
1913 * variable part of that offset.
1915 * This means we can use the 4*([k]&0xf) addressing
1916 * mode. Load the length of the IPv4 header, which
1917 * is at an offset of cstate->off_nl from the beginning of
1918 * the link-layer payload, and thus at an offset of
1919 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1920 * of the raw packet data, using that addressing mode.
1922 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1923 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1929 static struct block
*
1930 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1935 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1937 b
= new_block(cstate
, JMP(BPF_JEQ
));
1943 static inline struct block
*
1944 gen_true(compiler_state_t
*cstate
)
1946 return gen_uncond(cstate
, 1);
1949 static inline struct block
*
1950 gen_false(compiler_state_t
*cstate
)
1952 return gen_uncond(cstate
, 0);
1956 * Byte-swap a 32-bit number.
1957 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1958 * big-endian platforms.)
1960 #define SWAPLONG(y) \
1961 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1964 * Generate code to match a particular packet type.
1966 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1967 * value, if <= ETHERMTU. We use that to determine whether to
1968 * match the type/length field or to check the type/length field for
1969 * a value <= ETHERMTU to see whether it's a type field and then do
1970 * the appropriate test.
1972 static struct block
*
1973 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1975 struct block
*b0
, *b1
;
1981 case LLCSAP_NETBEUI
:
1983 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1984 * so we check the DSAP and SSAP.
1986 * LLCSAP_IP checks for IP-over-802.2, rather
1987 * than IP-over-Ethernet or IP-over-SNAP.
1989 * XXX - should we check both the DSAP and the
1990 * SSAP, like this, or should we check just the
1991 * DSAP, as we do for other types <= ETHERMTU
1992 * (i.e., other SAP values)?
1994 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1996 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2004 * Ethernet_II frames, which are Ethernet
2005 * frames with a frame type of ETHERTYPE_IPX;
2007 * Ethernet_802.3 frames, which are 802.3
2008 * frames (i.e., the type/length field is
2009 * a length field, <= ETHERMTU, rather than
2010 * a type field) with the first two bytes
2011 * after the Ethernet/802.3 header being
2014 * Ethernet_802.2 frames, which are 802.3
2015 * frames with an 802.2 LLC header and
2016 * with the IPX LSAP as the DSAP in the LLC
2019 * Ethernet_SNAP frames, which are 802.3
2020 * frames with an LLC header and a SNAP
2021 * header and with an OUI of 0x000000
2022 * (encapsulated Ethernet) and a protocol
2023 * ID of ETHERTYPE_IPX in the SNAP header.
2025 * XXX - should we generate the same code both
2026 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2030 * This generates code to check both for the
2031 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2033 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2034 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2038 * Now we add code to check for SNAP frames with
2039 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2041 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2045 * Now we generate code to check for 802.3
2046 * frames in general.
2048 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2052 * Now add the check for 802.3 frames before the
2053 * check for Ethernet_802.2 and Ethernet_802.3,
2054 * as those checks should only be done on 802.3
2055 * frames, not on Ethernet frames.
2060 * Now add the check for Ethernet_II frames, and
2061 * do that before checking for the other frame
2064 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2068 case ETHERTYPE_ATALK
:
2069 case ETHERTYPE_AARP
:
2071 * EtherTalk (AppleTalk protocols on Ethernet link
2072 * layer) may use 802.2 encapsulation.
2076 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2077 * we check for an Ethernet type field less than
2078 * 1500, which means it's an 802.3 length field.
2080 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2084 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2085 * SNAP packets with an organization code of
2086 * 0x080007 (Apple, for Appletalk) and a protocol
2087 * type of ETHERTYPE_ATALK (Appletalk).
2089 * 802.2-encapsulated ETHERTYPE_AARP packets are
2090 * SNAP packets with an organization code of
2091 * 0x000000 (encapsulated Ethernet) and a protocol
2092 * type of ETHERTYPE_AARP (Appletalk ARP).
2094 if (ll_proto
== ETHERTYPE_ATALK
)
2095 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2096 else /* ll_proto == ETHERTYPE_AARP */
2097 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2101 * Check for Ethernet encapsulation (Ethertalk
2102 * phase 1?); we just check for the Ethernet
2105 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2111 if (ll_proto
<= ETHERMTU
) {
2113 * This is an LLC SAP value, so the frames
2114 * that match would be 802.2 frames.
2115 * Check that the frame is an 802.2 frame
2116 * (i.e., that the length/type field is
2117 * a length field, <= ETHERMTU) and
2118 * then check the DSAP.
2120 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2122 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2127 * This is an Ethernet type, so compare
2128 * the length/type field with it (if
2129 * the frame is an 802.2 frame, the length
2130 * field will be <= ETHERMTU, and, as
2131 * "ll_proto" is > ETHERMTU, this test
2132 * will fail and the frame won't match,
2133 * which is what we want).
2135 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2140 static struct block
*
2141 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2144 * For DLT_NULL, the link-layer header is a 32-bit word
2145 * containing an AF_ value in *host* byte order, and for
2146 * DLT_ENC, the link-layer header begins with a 32-bit
2147 * word containing an AF_ value in host byte order.
2149 * In addition, if we're reading a saved capture file,
2150 * the host byte order in the capture may not be the
2151 * same as the host byte order on this machine.
2153 * For DLT_LOOP, the link-layer header is a 32-bit
2154 * word containing an AF_ value in *network* byte order.
2156 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2158 * The AF_ value is in host byte order, but the BPF
2159 * interpreter will convert it to network byte order.
2161 * If this is a save file, and it's from a machine
2162 * with the opposite byte order to ours, we byte-swap
2165 * Then we run it through "htonl()", and generate
2166 * code to compare against the result.
2168 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2169 ll_proto
= SWAPLONG(ll_proto
);
2170 ll_proto
= htonl(ll_proto
);
2172 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2176 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2177 * or IPv6 then we have an error.
2179 static struct block
*
2180 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2185 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2188 case ETHERTYPE_IPV6
:
2189 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2196 return gen_false(cstate
);
2200 * Generate code to match a particular packet type.
2202 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2203 * value, if <= ETHERMTU. We use that to determine whether to
2204 * match the type field or to check the type field for the special
2205 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2207 static struct block
*
2208 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2210 struct block
*b0
, *b1
;
2216 case LLCSAP_NETBEUI
:
2218 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2219 * so we check the DSAP and SSAP.
2221 * LLCSAP_IP checks for IP-over-802.2, rather
2222 * than IP-over-Ethernet or IP-over-SNAP.
2224 * XXX - should we check both the DSAP and the
2225 * SSAP, like this, or should we check just the
2226 * DSAP, as we do for other types <= ETHERMTU
2227 * (i.e., other SAP values)?
2229 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2230 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2236 * Ethernet_II frames, which are Ethernet
2237 * frames with a frame type of ETHERTYPE_IPX;
2239 * Ethernet_802.3 frames, which have a frame
2240 * type of LINUX_SLL_P_802_3;
2242 * Ethernet_802.2 frames, which are 802.3
2243 * frames with an 802.2 LLC header (i.e, have
2244 * a frame type of LINUX_SLL_P_802_2) and
2245 * with the IPX LSAP as the DSAP in the LLC
2248 * Ethernet_SNAP frames, which are 802.3
2249 * frames with an LLC header and a SNAP
2250 * header and with an OUI of 0x000000
2251 * (encapsulated Ethernet) and a protocol
2252 * ID of ETHERTYPE_IPX in the SNAP header.
2254 * First, do the checks on LINUX_SLL_P_802_2
2255 * frames; generate the check for either
2256 * Ethernet_802.2 or Ethernet_SNAP frames, and
2257 * then put a check for LINUX_SLL_P_802_2 frames
2260 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2261 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2263 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2267 * Now check for 802.3 frames and OR that with
2268 * the previous test.
2270 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2274 * Now add the check for Ethernet_II frames, and
2275 * do that before checking for the other frame
2278 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2282 case ETHERTYPE_ATALK
:
2283 case ETHERTYPE_AARP
:
2285 * EtherTalk (AppleTalk protocols on Ethernet link
2286 * layer) may use 802.2 encapsulation.
2290 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2291 * we check for the 802.2 protocol type in the
2292 * "Ethernet type" field.
2294 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2297 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2298 * SNAP packets with an organization code of
2299 * 0x080007 (Apple, for Appletalk) and a protocol
2300 * type of ETHERTYPE_ATALK (Appletalk).
2302 * 802.2-encapsulated ETHERTYPE_AARP packets are
2303 * SNAP packets with an organization code of
2304 * 0x000000 (encapsulated Ethernet) and a protocol
2305 * type of ETHERTYPE_AARP (Appletalk ARP).
2307 if (ll_proto
== ETHERTYPE_ATALK
)
2308 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2309 else /* ll_proto == ETHERTYPE_AARP */
2310 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2314 * Check for Ethernet encapsulation (Ethertalk
2315 * phase 1?); we just check for the Ethernet
2318 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2324 if (ll_proto
<= ETHERMTU
) {
2326 * This is an LLC SAP value, so the frames
2327 * that match would be 802.2 frames.
2328 * Check for the 802.2 protocol type
2329 * in the "Ethernet type" field, and
2330 * then check the DSAP.
2332 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2333 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2339 * This is an Ethernet type, so compare
2340 * the length/type field with it (if
2341 * the frame is an 802.2 frame, the length
2342 * field will be <= ETHERMTU, and, as
2343 * "ll_proto" is > ETHERMTU, this test
2344 * will fail and the frame won't match,
2345 * which is what we want).
2347 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2352 static struct slist
*
2353 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2355 struct slist
*s1
, *s2
;
2356 struct slist
*sjeq_avs_cookie
;
2357 struct slist
*sjcommon
;
2360 * This code is not compatible with the optimizer, as
2361 * we are generating jmp instructions within a normal
2362 * slist of instructions
2364 cstate
->no_optimize
= 1;
2367 * Generate code to load the length of the radio header into
2368 * the register assigned to hold that length, if one has been
2369 * assigned. (If one hasn't been assigned, no code we've
2370 * generated uses that prefix, so we don't need to generate any
2373 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2374 * or always use the AVS header rather than the Prism header.
2375 * We load a 4-byte big-endian value at the beginning of the
2376 * raw packet data, and see whether, when masked with 0xFFFFF000,
2377 * it's equal to 0x80211000. If so, that indicates that it's
2378 * an AVS header (the masked-out bits are the version number).
2379 * Otherwise, it's a Prism header.
2381 * XXX - the Prism header is also, in theory, variable-length,
2382 * but no known software generates headers that aren't 144
2385 if (cstate
->off_linkhdr
.reg
!= -1) {
2389 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2393 * AND it with 0xFFFFF000.
2395 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2396 s2
->s
.k
= 0xFFFFF000;
2400 * Compare with 0x80211000.
2402 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2403 sjeq_avs_cookie
->s
.k
= 0x80211000;
2404 sappend(s1
, sjeq_avs_cookie
);
2409 * The 4 bytes at an offset of 4 from the beginning of
2410 * the AVS header are the length of the AVS header.
2411 * That field is big-endian.
2413 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2416 sjeq_avs_cookie
->s
.jt
= s2
;
2419 * Now jump to the code to allocate a register
2420 * into which to save the header length and
2421 * store the length there. (The "jump always"
2422 * instruction needs to have the k field set;
2423 * it's added to the PC, so, as we're jumping
2424 * over a single instruction, it should be 1.)
2426 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2428 sappend(s1
, sjcommon
);
2431 * Now for the code that handles the Prism header.
2432 * Just load the length of the Prism header (144)
2433 * into the A register. Have the test for an AVS
2434 * header branch here if we don't have an AVS header.
2436 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2439 sjeq_avs_cookie
->s
.jf
= s2
;
2442 * Now allocate a register to hold that value and store
2443 * it. The code for the AVS header will jump here after
2444 * loading the length of the AVS header.
2446 s2
= new_stmt(cstate
, BPF_ST
);
2447 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2449 sjcommon
->s
.jf
= s2
;
2452 * Now move it into the X register.
2454 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2462 static struct slist
*
2463 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2465 struct slist
*s1
, *s2
;
2468 * Generate code to load the length of the AVS header into
2469 * the register assigned to hold that length, if one has been
2470 * assigned. (If one hasn't been assigned, no code we've
2471 * generated uses that prefix, so we don't need to generate any
2474 if (cstate
->off_linkhdr
.reg
!= -1) {
2476 * The 4 bytes at an offset of 4 from the beginning of
2477 * the AVS header are the length of the AVS header.
2478 * That field is big-endian.
2480 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2484 * Now allocate a register to hold that value and store
2487 s2
= new_stmt(cstate
, BPF_ST
);
2488 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2492 * Now move it into the X register.
2494 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2502 static struct slist
*
2503 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2505 struct slist
*s1
, *s2
;
2508 * Generate code to load the length of the radiotap header into
2509 * the register assigned to hold that length, if one has been
2510 * assigned. (If one hasn't been assigned, no code we've
2511 * generated uses that prefix, so we don't need to generate any
2514 if (cstate
->off_linkhdr
.reg
!= -1) {
2516 * The 2 bytes at offsets of 2 and 3 from the beginning
2517 * of the radiotap header are the length of the radiotap
2518 * header; unfortunately, it's little-endian, so we have
2519 * to load it a byte at a time and construct the value.
2523 * Load the high-order byte, at an offset of 3, shift it
2524 * left a byte, and put the result in the X register.
2526 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2528 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2531 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2535 * Load the next byte, at an offset of 2, and OR the
2536 * value from the X register into it.
2538 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2541 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2545 * Now allocate a register to hold that value and store
2548 s2
= new_stmt(cstate
, BPF_ST
);
2549 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2553 * Now move it into the X register.
2555 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2564 * At the moment we treat PPI as normal Radiotap encoded
2565 * packets. The difference is in the function that generates
2566 * the code at the beginning to compute the header length.
2567 * Since this code generator of PPI supports bare 802.11
2568 * encapsulation only (i.e. the encapsulated DLT should be
2569 * DLT_IEEE802_11) we generate code to check for this too;
2570 * that's done in finish_parse().
2572 static struct slist
*
2573 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2575 struct slist
*s1
, *s2
;
2578 * Generate code to load the length of the radiotap header
2579 * into the register assigned to hold that length, if one has
2582 if (cstate
->off_linkhdr
.reg
!= -1) {
2584 * The 2 bytes at offsets of 2 and 3 from the beginning
2585 * of the radiotap header are the length of the radiotap
2586 * header; unfortunately, it's little-endian, so we have
2587 * to load it a byte at a time and construct the value.
2591 * Load the high-order byte, at an offset of 3, shift it
2592 * left a byte, and put the result in the X register.
2594 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2596 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2599 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2603 * Load the next byte, at an offset of 2, and OR the
2604 * value from the X register into it.
2606 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2609 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2613 * Now allocate a register to hold that value and store
2616 s2
= new_stmt(cstate
, BPF_ST
);
2617 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2621 * Now move it into the X register.
2623 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2632 * Load a value relative to the beginning of the link-layer header after the 802.11
2633 * header, i.e. LLC_SNAP.
2634 * The link-layer header doesn't necessarily begin at the beginning
2635 * of the packet data; there might be a variable-length prefix containing
2636 * radio information.
2638 static struct slist
*
2639 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2642 struct slist
*sjset_data_frame_1
;
2643 struct slist
*sjset_data_frame_2
;
2644 struct slist
*sjset_qos
;
2645 struct slist
*sjset_radiotap_flags_present
;
2646 struct slist
*sjset_radiotap_ext_present
;
2647 struct slist
*sjset_radiotap_tsft_present
;
2648 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2649 struct slist
*s_roundup
;
2651 if (cstate
->off_linkpl
.reg
== -1) {
2653 * No register has been assigned to the offset of
2654 * the link-layer payload, which means nobody needs
2655 * it; don't bother computing it - just return
2656 * what we already have.
2662 * This code is not compatible with the optimizer, as
2663 * we are generating jmp instructions within a normal
2664 * slist of instructions
2666 cstate
->no_optimize
= 1;
2669 * If "s" is non-null, it has code to arrange that the X register
2670 * contains the length of the prefix preceding the link-layer
2673 * Otherwise, the length of the prefix preceding the link-layer
2674 * header is "off_outermostlinkhdr.constant_part".
2678 * There is no variable-length header preceding the
2679 * link-layer header.
2681 * Load the length of the fixed-length prefix preceding
2682 * the link-layer header (if any) into the X register,
2683 * and store it in the cstate->off_linkpl.reg register.
2684 * That length is off_outermostlinkhdr.constant_part.
2686 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2687 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2691 * The X register contains the offset of the beginning of the
2692 * link-layer header; add 24, which is the minimum length
2693 * of the MAC header for a data frame, to that, and store it
2694 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2695 * which is at the offset in the X register, with an indexed load.
2697 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2699 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2702 s2
= new_stmt(cstate
, BPF_ST
);
2703 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2706 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2711 * Check the Frame Control field to see if this is a data frame;
2712 * a data frame has the 0x08 bit (b3) in that field set and the
2713 * 0x04 bit (b2) clear.
2715 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2716 sjset_data_frame_1
->s
.k
= 0x08;
2717 sappend(s
, sjset_data_frame_1
);
2720 * If b3 is set, test b2, otherwise go to the first statement of
2721 * the rest of the program.
2723 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2724 sjset_data_frame_2
->s
.k
= 0x04;
2725 sappend(s
, sjset_data_frame_2
);
2726 sjset_data_frame_1
->s
.jf
= snext
;
2729 * If b2 is not set, this is a data frame; test the QoS bit.
2730 * Otherwise, go to the first statement of the rest of the
2733 sjset_data_frame_2
->s
.jt
= snext
;
2734 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2735 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2736 sappend(s
, sjset_qos
);
2739 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2741 * Otherwise, go to the first statement of the rest of the
2744 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2745 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2747 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2750 s2
= new_stmt(cstate
, BPF_ST
);
2751 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2755 * If we have a radiotap header, look at it to see whether
2756 * there's Atheros padding between the MAC-layer header
2759 * Note: all of the fields in the radiotap header are
2760 * little-endian, so we byte-swap all of the values
2761 * we test against, as they will be loaded as big-endian
2764 * XXX - in the general case, we would have to scan through
2765 * *all* the presence bits, if there's more than one word of
2766 * presence bits. That would require a loop, meaning that
2767 * we wouldn't be able to run the filter in the kernel.
2769 * We assume here that the Atheros adapters that insert the
2770 * annoying padding don't have multiple antennae and therefore
2771 * do not generate radiotap headers with multiple presence words.
2773 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2775 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2776 * in the first presence flag word?
2778 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2782 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2783 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2784 sappend(s
, sjset_radiotap_flags_present
);
2787 * If not, skip all of this.
2789 sjset_radiotap_flags_present
->s
.jf
= snext
;
2792 * Otherwise, is the "extension" bit set in that word?
2794 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2795 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2796 sappend(s
, sjset_radiotap_ext_present
);
2797 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2800 * If so, skip all of this.
2802 sjset_radiotap_ext_present
->s
.jt
= snext
;
2805 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2807 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2808 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2809 sappend(s
, sjset_radiotap_tsft_present
);
2810 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2813 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2814 * at an offset of 16 from the beginning of the raw packet
2815 * data (8 bytes for the radiotap header and 8 bytes for
2818 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2821 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2824 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2826 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2827 sjset_tsft_datapad
->s
.k
= 0x20;
2828 sappend(s
, sjset_tsft_datapad
);
2831 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2832 * at an offset of 8 from the beginning of the raw packet
2833 * data (8 bytes for the radiotap header).
2835 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2838 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2841 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2843 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2844 sjset_notsft_datapad
->s
.k
= 0x20;
2845 sappend(s
, sjset_notsft_datapad
);
2848 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2849 * set, round the length of the 802.11 header to
2850 * a multiple of 4. Do that by adding 3 and then
2851 * dividing by and multiplying by 4, which we do by
2854 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2855 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2856 sappend(s
, s_roundup
);
2857 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2860 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2861 s2
->s
.k
= (bpf_u_int32
)~3;
2863 s2
= new_stmt(cstate
, BPF_ST
);
2864 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2867 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2868 sjset_tsft_datapad
->s
.jf
= snext
;
2869 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2870 sjset_notsft_datapad
->s
.jf
= snext
;
2872 sjset_qos
->s
.jf
= snext
;
2878 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2882 /* There is an implicit dependency between the link
2883 * payload and link header since the payload computation
2884 * includes the variable part of the header. Therefore,
2885 * if nobody else has allocated a register for the link
2886 * header and we need it, do it now. */
2887 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2888 cstate
->off_linkhdr
.reg
== -1)
2889 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2892 * For link-layer types that have a variable-length header
2893 * preceding the link-layer header, generate code to load
2894 * the offset of the link-layer header into the register
2895 * assigned to that offset, if any.
2897 * XXX - this, and the next switch statement, won't handle
2898 * encapsulation of 802.11 or 802.11+radio information in
2899 * some other protocol stack. That's significantly more
2902 switch (cstate
->outermostlinktype
) {
2904 case DLT_PRISM_HEADER
:
2905 s
= gen_load_prism_llprefixlen(cstate
);
2908 case DLT_IEEE802_11_RADIO_AVS
:
2909 s
= gen_load_avs_llprefixlen(cstate
);
2912 case DLT_IEEE802_11_RADIO
:
2913 s
= gen_load_radiotap_llprefixlen(cstate
);
2917 s
= gen_load_ppi_llprefixlen(cstate
);
2926 * For link-layer types that have a variable-length link-layer
2927 * header, generate code to load the offset of the link-layer
2928 * payload into the register assigned to that offset, if any.
2930 switch (cstate
->outermostlinktype
) {
2932 case DLT_IEEE802_11
:
2933 case DLT_PRISM_HEADER
:
2934 case DLT_IEEE802_11_RADIO_AVS
:
2935 case DLT_IEEE802_11_RADIO
:
2937 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2942 * If there is no initialization yet and we need variable
2943 * length offsets for VLAN, initialize them to zero
2945 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2948 if (cstate
->off_linkpl
.reg
== -1)
2949 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2950 if (cstate
->off_linktype
.reg
== -1)
2951 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2953 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2955 s2
= new_stmt(cstate
, BPF_ST
);
2956 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2958 s2
= new_stmt(cstate
, BPF_ST
);
2959 s2
->s
.k
= cstate
->off_linktype
.reg
;
2964 * If we have any offset-loading code, append all the
2965 * existing statements in the block to those statements,
2966 * and make the resulting list the list of statements
2970 sappend(s
, b
->stmts
);
2975 static struct block
*
2976 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2978 struct slist
*s_load_dlt
;
2981 if (cstate
->linktype
== DLT_PPI
)
2983 /* Create the statements that check for the DLT
2985 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2986 s_load_dlt
->s
.k
= 4;
2988 b
= new_block(cstate
, JMP(BPF_JEQ
));
2990 b
->stmts
= s_load_dlt
;
2991 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3002 * Take an absolute offset, and:
3004 * if it has no variable part, return NULL;
3006 * if it has a variable part, generate code to load the register
3007 * containing that variable part into the X register, returning
3008 * a pointer to that code - if no register for that offset has
3009 * been allocated, allocate it first.
3011 * (The code to set that register will be generated later, but will
3012 * be placed earlier in the code sequence.)
3014 static struct slist
*
3015 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3019 if (off
->is_variable
) {
3020 if (off
->reg
== -1) {
3022 * We haven't yet assigned a register for the
3023 * variable part of the offset of the link-layer
3024 * header; allocate one.
3026 off
->reg
= alloc_reg(cstate
);
3030 * Load the register containing the variable part of the
3031 * offset of the link-layer header into the X register.
3033 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3038 * That offset isn't variable, there's no variable part,
3039 * so we don't need to generate any code.
3046 * Map an Ethernet type to the equivalent PPP type.
3049 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3057 case ETHERTYPE_IPV6
:
3058 ll_proto
= PPP_IPV6
;
3062 ll_proto
= PPP_DECNET
;
3065 case ETHERTYPE_ATALK
:
3066 ll_proto
= PPP_APPLE
;
3079 * I'm assuming the "Bridging PDU"s that go
3080 * over PPP are Spanning Tree Protocol
3083 ll_proto
= PPP_BRPDU
;
3094 * Generate any tests that, for encapsulation of a link-layer packet
3095 * inside another protocol stack, need to be done to check for those
3096 * link-layer packets (and that haven't already been done by a check
3097 * for that encapsulation).
3099 static struct block
*
3100 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3104 if (cstate
->is_geneve
)
3105 return gen_geneve_ll_check(cstate
);
3107 switch (cstate
->prevlinktype
) {
3111 * This is LANE-encapsulated Ethernet; check that the LANE
3112 * packet doesn't begin with an LE Control marker, i.e.
3113 * that it's data, not a control message.
3115 * (We've already generated a test for LANE.)
3117 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3123 * No such tests are necessary.
3131 * The three different values we should check for when checking for an
3132 * IPv6 packet with DLT_NULL.
3134 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3135 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3136 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3139 * Generate code to match a particular packet type by matching the
3140 * link-layer type field or fields in the 802.2 LLC header.
3142 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3143 * value, if <= ETHERMTU.
3145 static struct block
*
3146 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3148 struct block
*b0
, *b1
, *b2
;
3149 const char *description
;
3151 /* are we checking MPLS-encapsulated packets? */
3152 if (cstate
->label_stack_depth
> 0)
3153 return gen_mpls_linktype(cstate
, ll_proto
);
3155 switch (cstate
->linktype
) {
3158 case DLT_NETANALYZER
:
3159 case DLT_NETANALYZER_TRANSPARENT
:
3160 /* Geneve has an EtherType regardless of whether there is an
3162 if (!cstate
->is_geneve
)
3163 b0
= gen_prevlinkhdr_check(cstate
);
3167 b1
= gen_ether_linktype(cstate
, ll_proto
);
3178 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3182 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3186 case DLT_IEEE802_11
:
3187 case DLT_PRISM_HEADER
:
3188 case DLT_IEEE802_11_RADIO_AVS
:
3189 case DLT_IEEE802_11_RADIO
:
3192 * Check that we have a data frame.
3194 b0
= gen_check_802_11_data_frame(cstate
);
3197 * Now check for the specified link-layer type.
3199 b1
= gen_llc_linktype(cstate
, ll_proto
);
3206 * XXX - check for LLC frames.
3208 return gen_llc_linktype(cstate
, ll_proto
);
3213 * XXX - check for LLC PDUs, as per IEEE 802.5.
3215 return gen_llc_linktype(cstate
, ll_proto
);
3218 case DLT_ATM_RFC1483
:
3220 case DLT_IP_OVER_FC
:
3221 return gen_llc_linktype(cstate
, ll_proto
);
3226 * Check for an LLC-encapsulated version of this protocol;
3227 * if we were checking for LANE, linktype would no longer
3230 * Check for LLC encapsulation and then check the protocol.
3232 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3233 b1
= gen_llc_linktype(cstate
, ll_proto
);
3239 return gen_linux_sll_linktype(cstate
, ll_proto
);
3243 case DLT_SLIP_BSDOS
:
3246 * These types don't provide any type field; packets
3247 * are always IPv4 or IPv6.
3249 * XXX - for IPv4, check for a version number of 4, and,
3250 * for IPv6, check for a version number of 6?
3255 /* Check for a version number of 4. */
3256 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3258 case ETHERTYPE_IPV6
:
3259 /* Check for a version number of 6. */
3260 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3263 return gen_false(cstate
); /* always false */
3269 * Raw IPv4, so no type field.
3271 if (ll_proto
== ETHERTYPE_IP
)
3272 return gen_true(cstate
); /* always true */
3274 /* Checking for something other than IPv4; always false */
3275 return gen_false(cstate
);
3280 * Raw IPv6, so no type field.
3282 if (ll_proto
== ETHERTYPE_IPV6
)
3283 return gen_true(cstate
); /* always true */
3285 /* Checking for something other than IPv6; always false */
3286 return gen_false(cstate
);
3291 case DLT_PPP_SERIAL
:
3294 * We use Ethernet protocol types inside libpcap;
3295 * map them to the corresponding PPP protocol types.
3297 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3298 ethertype_to_ppptype(ll_proto
));
3303 * We use Ethernet protocol types inside libpcap;
3304 * map them to the corresponding PPP protocol types.
3310 * Also check for Van Jacobson-compressed IP.
3311 * XXX - do this for other forms of PPP?
3313 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3314 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3316 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3321 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3322 ethertype_to_ppptype(ll_proto
));
3332 return (gen_loopback_linktype(cstate
, AF_INET
));
3334 case ETHERTYPE_IPV6
:
3336 * AF_ values may, unfortunately, be platform-
3337 * dependent; AF_INET isn't, because everybody
3338 * used 4.2BSD's value, but AF_INET6 is, because
3339 * 4.2BSD didn't have a value for it (given that
3340 * IPv6 didn't exist back in the early 1980's),
3341 * and they all picked their own values.
3343 * This means that, if we're reading from a
3344 * savefile, we need to check for all the
3347 * If we're doing a live capture, we only need
3348 * to check for this platform's value; however,
3349 * Npcap uses 24, which isn't Windows's AF_INET6
3350 * value. (Given the multiple different values,
3351 * programs that read pcap files shouldn't be
3352 * checking for their platform's AF_INET6 value
3353 * anyway, they should check for all of the
3354 * possible values. and they might as well do
3355 * that even for live captures.)
3357 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3359 * Savefile - check for all three
3360 * possible IPv6 values.
3362 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3363 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3365 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3370 * Live capture, so we only need to
3371 * check for the value used on this
3376 * Npcap doesn't use Windows's AF_INET6,
3377 * as that collides with AF_IPX on
3378 * some BSDs (both have the value 23).
3379 * Instead, it uses 24.
3381 return (gen_loopback_linktype(cstate
, 24));
3384 return (gen_loopback_linktype(cstate
, AF_INET6
));
3385 #else /* AF_INET6 */
3387 * I guess this platform doesn't support
3388 * IPv6, so we just reject all packets.
3390 return gen_false(cstate
);
3391 #endif /* AF_INET6 */
3397 * Not a type on which we support filtering.
3398 * XXX - support those that have AF_ values
3399 * #defined on this platform, at least?
3401 return gen_false(cstate
);
3404 #ifdef HAVE_NET_PFVAR_H
3407 * af field is host byte order in contrast to the rest of
3410 if (ll_proto
== ETHERTYPE_IP
)
3411 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3413 else if (ll_proto
== ETHERTYPE_IPV6
)
3414 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3417 return gen_false(cstate
);
3419 #endif /* HAVE_NET_PFVAR_H */
3422 case DLT_ARCNET_LINUX
:
3424 * XXX should we check for first fragment if the protocol
3430 return gen_false(cstate
);
3432 case ETHERTYPE_IPV6
:
3433 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3437 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3439 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3445 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3447 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3452 case ETHERTYPE_REVARP
:
3453 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3456 case ETHERTYPE_ATALK
:
3457 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3464 case ETHERTYPE_ATALK
:
3465 return gen_true(cstate
);
3467 return gen_false(cstate
);
3473 * XXX - assumes a 2-byte Frame Relay header with
3474 * DLCI and flags. What if the address is longer?
3480 * Check for the special NLPID for IP.
3482 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3484 case ETHERTYPE_IPV6
:
3486 * Check for the special NLPID for IPv6.
3488 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3492 * Check for several OSI protocols.
3494 * Frame Relay packets typically have an OSI
3495 * NLPID at the beginning; we check for each
3498 * What we check for is the NLPID and a frame
3499 * control field of UI, i.e. 0x03 followed
3502 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3503 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3504 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3510 return gen_false(cstate
);
3515 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3517 case DLT_JUNIPER_MFR
:
3518 case DLT_JUNIPER_MLFR
:
3519 case DLT_JUNIPER_MLPPP
:
3520 case DLT_JUNIPER_ATM1
:
3521 case DLT_JUNIPER_ATM2
:
3522 case DLT_JUNIPER_PPPOE
:
3523 case DLT_JUNIPER_PPPOE_ATM
:
3524 case DLT_JUNIPER_GGSN
:
3525 case DLT_JUNIPER_ES
:
3526 case DLT_JUNIPER_MONITOR
:
3527 case DLT_JUNIPER_SERVICES
:
3528 case DLT_JUNIPER_ETHER
:
3529 case DLT_JUNIPER_PPP
:
3530 case DLT_JUNIPER_FRELAY
:
3531 case DLT_JUNIPER_CHDLC
:
3532 case DLT_JUNIPER_VP
:
3533 case DLT_JUNIPER_ST
:
3534 case DLT_JUNIPER_ISM
:
3535 case DLT_JUNIPER_VS
:
3536 case DLT_JUNIPER_SRX_E2E
:
3537 case DLT_JUNIPER_FIBRECHANNEL
:
3538 case DLT_JUNIPER_ATM_CEMIC
:
3540 /* just lets verify the magic number for now -
3541 * on ATM we may have up to 6 different encapsulations on the wire
3542 * and need a lot of heuristics to figure out that the payload
3545 * FIXME encapsulation specific BPF_ filters
3547 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3549 case DLT_BACNET_MS_TP
:
3550 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3553 return gen_ipnet_linktype(cstate
, ll_proto
);
3555 case DLT_LINUX_IRDA
:
3556 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3559 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3562 case DLT_MTP2_WITH_PHDR
:
3563 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3566 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3569 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3571 case DLT_LINUX_LAPD
:
3572 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3574 case DLT_USB_FREEBSD
:
3576 case DLT_USB_LINUX_MMAPPED
:
3578 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3580 case DLT_BLUETOOTH_HCI_H4
:
3581 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3582 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3585 case DLT_CAN_SOCKETCAN
:
3586 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3588 case DLT_IEEE802_15_4
:
3589 case DLT_IEEE802_15_4_LINUX
:
3590 case DLT_IEEE802_15_4_NONASK_PHY
:
3591 case DLT_IEEE802_15_4_NOFCS
:
3592 case DLT_IEEE802_15_4_TAP
:
3593 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3595 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3596 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3599 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3602 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3604 case DLT_IPMB_KONTRON
:
3605 case DLT_IPMB_LINUX
:
3606 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3609 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3612 /* Using the fixed-size NFLOG header it is possible to tell only
3613 * the address family of the packet, other meaningful data is
3614 * either missing or behind TLVs.
3616 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3620 * Does this link-layer header type have a field
3621 * indicating the type of the next protocol? If
3622 * so, off_linktype.constant_part will be the offset of that
3623 * field in the packet; if not, it will be OFFSET_NOT_SET.
3625 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3627 * Yes; assume it's an Ethernet type. (If
3628 * it's not, it needs to be handled specially
3631 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3635 * No; report an error.
3637 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3638 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3646 * Check for an LLC SNAP packet with a given organization code and
3647 * protocol type; we check the entire contents of the 802.2 LLC and
3648 * snap headers, checking for DSAP and SSAP of SNAP and a control
3649 * field of 0x03 in the LLC header, and for the specified organization
3650 * code and protocol type in the SNAP header.
3652 static struct block
*
3653 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3655 u_char snapblock
[8];
3657 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3658 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3659 snapblock
[2] = 0x03; /* control = UI */
3660 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3661 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3662 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3663 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3664 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3665 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3669 * Generate code to match frames with an LLC header.
3671 static struct block
*
3672 gen_llc_internal(compiler_state_t
*cstate
)
3674 struct block
*b0
, *b1
;
3676 switch (cstate
->linktype
) {
3680 * We check for an Ethernet type field less than
3681 * 1500, which means it's an 802.3 length field.
3683 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3687 * Now check for the purported DSAP and SSAP not being
3688 * 0xFF, to rule out NetWare-over-802.3.
3690 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3697 * We check for LLC traffic.
3699 b0
= gen_atmtype_llc(cstate
);
3702 case DLT_IEEE802
: /* Token Ring */
3704 * XXX - check for LLC frames.
3706 return gen_true(cstate
);
3710 * XXX - check for LLC frames.
3712 return gen_true(cstate
);
3714 case DLT_ATM_RFC1483
:
3716 * For LLC encapsulation, these are defined to have an
3719 * For VC encapsulation, they don't, but there's no
3720 * way to check for that; the protocol used on the VC
3721 * is negotiated out of band.
3723 return gen_true(cstate
);
3725 case DLT_IEEE802_11
:
3726 case DLT_PRISM_HEADER
:
3727 case DLT_IEEE802_11_RADIO
:
3728 case DLT_IEEE802_11_RADIO_AVS
:
3731 * Check that we have a data frame.
3733 b0
= gen_check_802_11_data_frame(cstate
);
3737 bpf_error(cstate
, "'llc' not supported for %s",
3738 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3744 gen_llc(compiler_state_t
*cstate
)
3747 * Catch errors reported by us and routines below us, and return NULL
3750 if (setjmp(cstate
->top_ctx
))
3753 return gen_llc_internal(cstate
);
3757 gen_llc_i(compiler_state_t
*cstate
)
3759 struct block
*b0
, *b1
;
3763 * Catch errors reported by us and routines below us, and return NULL
3766 if (setjmp(cstate
->top_ctx
))
3770 * Check whether this is an LLC frame.
3772 b0
= gen_llc_internal(cstate
);
3775 * Load the control byte and test the low-order bit; it must
3776 * be clear for I frames.
3778 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3779 b1
= new_block(cstate
, JMP(BPF_JSET
));
3788 gen_llc_s(compiler_state_t
*cstate
)
3790 struct block
*b0
, *b1
;
3793 * Catch errors reported by us and routines below us, and return NULL
3796 if (setjmp(cstate
->top_ctx
))
3800 * Check whether this is an LLC frame.
3802 b0
= gen_llc_internal(cstate
);
3805 * Now compare the low-order 2 bit of the control byte against
3806 * the appropriate value for S frames.
3808 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3814 gen_llc_u(compiler_state_t
*cstate
)
3816 struct block
*b0
, *b1
;
3819 * Catch errors reported by us and routines below us, and return NULL
3822 if (setjmp(cstate
->top_ctx
))
3826 * Check whether this is an LLC frame.
3828 b0
= gen_llc_internal(cstate
);
3831 * Now compare the low-order 2 bit of the control byte against
3832 * the appropriate value for U frames.
3834 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3840 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3842 struct block
*b0
, *b1
;
3845 * Catch errors reported by us and routines below us, and return NULL
3848 if (setjmp(cstate
->top_ctx
))
3852 * Check whether this is an LLC frame.
3854 b0
= gen_llc_internal(cstate
);
3857 * Now check for an S frame with the appropriate type.
3859 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3865 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3867 struct block
*b0
, *b1
;
3870 * Catch errors reported by us and routines below us, and return NULL
3873 if (setjmp(cstate
->top_ctx
))
3877 * Check whether this is an LLC frame.
3879 b0
= gen_llc_internal(cstate
);
3882 * Now check for a U frame with the appropriate type.
3884 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3890 * Generate code to match a particular packet type, for link-layer types
3891 * using 802.2 LLC headers.
3893 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3894 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3896 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3897 * value, if <= ETHERMTU. We use that to determine whether to
3898 * match the DSAP or both DSAP and LSAP or to check the OUI and
3899 * protocol ID in a SNAP header.
3901 static struct block
*
3902 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3905 * XXX - handle token-ring variable-length header.
3911 case LLCSAP_NETBEUI
:
3913 * XXX - should we check both the DSAP and the
3914 * SSAP, like this, or should we check just the
3915 * DSAP, as we do for other SAP values?
3917 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3918 ((ll_proto
<< 8) | ll_proto
));
3922 * XXX - are there ever SNAP frames for IPX on
3923 * non-Ethernet 802.x networks?
3925 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3927 case ETHERTYPE_ATALK
:
3929 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3930 * SNAP packets with an organization code of
3931 * 0x080007 (Apple, for Appletalk) and a protocol
3932 * type of ETHERTYPE_ATALK (Appletalk).
3934 * XXX - check for an organization code of
3935 * encapsulated Ethernet as well?
3937 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3941 * XXX - we don't have to check for IPX 802.3
3942 * here, but should we check for the IPX Ethertype?
3944 if (ll_proto
<= ETHERMTU
) {
3946 * This is an LLC SAP value, so check
3949 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
3952 * This is an Ethernet type; we assume that it's
3953 * unlikely that it'll appear in the right place
3954 * at random, and therefore check only the
3955 * location that would hold the Ethernet type
3956 * in a SNAP frame with an organization code of
3957 * 0x000000 (encapsulated Ethernet).
3959 * XXX - if we were to check for the SNAP DSAP and
3960 * LSAP, as per XXX, and were also to check for an
3961 * organization code of 0x000000 (encapsulated
3962 * Ethernet), we'd do
3964 * return gen_snap(cstate, 0x000000, ll_proto);
3966 * here; for now, we don't, as per the above.
3967 * I don't know whether it's worth the extra CPU
3968 * time to do the right check or not.
3970 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
3975 static struct block
*
3976 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3977 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
3979 struct block
*b0
, *b1
;
3993 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
3994 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4000 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4001 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4006 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4010 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4014 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4018 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4022 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4026 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4033 b0
= gen_linktype(cstate
, ll_proto
);
4034 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4040 static struct block
*
4041 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4042 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4045 struct block
*b0
, *b1
;
4060 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4061 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4067 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4068 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4073 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4077 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4081 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4085 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4089 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4093 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4100 /* this order is important */
4101 a
= (uint32_t *)addr
;
4102 m
= (uint32_t *)mask
;
4103 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4104 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4106 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4108 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4110 b0
= gen_linktype(cstate
, ll_proto
);
4116 static struct block
*
4117 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4119 register struct block
*b0
, *b1
;
4123 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4126 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4129 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4130 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4136 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4137 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4142 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4146 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4150 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4154 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4158 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4162 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4170 * Like gen_ehostop, but for DLT_FDDI
4172 static struct block
*
4173 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4175 struct block
*b0
, *b1
;
4179 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4182 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4185 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4186 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4192 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4193 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4198 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4202 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4206 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4210 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4214 bpf_error(cstate
, "'ra' is only supported on 802.11");
4218 bpf_error(cstate
, "'ta' is only supported on 802.11");
4226 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4228 static struct block
*
4229 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4231 register struct block
*b0
, *b1
;
4235 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4238 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4241 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4242 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4248 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4249 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4254 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4258 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4262 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4266 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4270 bpf_error(cstate
, "'ra' is only supported on 802.11");
4274 bpf_error(cstate
, "'ta' is only supported on 802.11");
4282 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4283 * various 802.11 + radio headers.
4285 static struct block
*
4286 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4288 register struct block
*b0
, *b1
, *b2
;
4289 register struct slist
*s
;
4291 #ifdef ENABLE_WLAN_FILTERING_PATCH
4294 * We need to disable the optimizer because the optimizer is buggy
4295 * and wipes out some LD instructions generated by the below
4296 * code to validate the Frame Control bits
4298 cstate
->no_optimize
= 1;
4299 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4306 * For control frames, there is no SA.
4308 * For management frames, SA is at an
4309 * offset of 10 from the beginning of
4312 * For data frames, SA is at an offset
4313 * of 10 from the beginning of the packet
4314 * if From DS is clear, at an offset of
4315 * 16 from the beginning of the packet
4316 * if From DS is set and To DS is clear,
4317 * and an offset of 24 from the beginning
4318 * of the packet if From DS is set and To DS
4323 * Generate the tests to be done for data frames
4326 * First, check for To DS set, i.e. check "link[1] & 0x01".
4328 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4329 b1
= new_block(cstate
, JMP(BPF_JSET
));
4330 b1
->s
.k
= 0x01; /* To DS */
4334 * If To DS is set, the SA is at 24.
4336 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4340 * Now, check for To DS not set, i.e. check
4341 * "!(link[1] & 0x01)".
4343 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4344 b2
= new_block(cstate
, JMP(BPF_JSET
));
4345 b2
->s
.k
= 0x01; /* To DS */
4350 * If To DS is not set, the SA is at 16.
4352 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4356 * Now OR together the last two checks. That gives
4357 * the complete set of checks for data frames with
4363 * Now check for From DS being set, and AND that with
4364 * the ORed-together checks.
4366 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4367 b1
= new_block(cstate
, JMP(BPF_JSET
));
4368 b1
->s
.k
= 0x02; /* From DS */
4373 * Now check for data frames with From DS not set.
4375 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4376 b2
= new_block(cstate
, JMP(BPF_JSET
));
4377 b2
->s
.k
= 0x02; /* From DS */
4382 * If From DS isn't set, the SA is at 10.
4384 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4388 * Now OR together the checks for data frames with
4389 * From DS not set and for data frames with From DS
4390 * set; that gives the checks done for data frames.
4395 * Now check for a data frame.
4396 * I.e, check "link[0] & 0x08".
4398 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4399 b1
= new_block(cstate
, JMP(BPF_JSET
));
4404 * AND that with the checks done for data frames.
4409 * If the high-order bit of the type value is 0, this
4410 * is a management frame.
4411 * I.e, check "!(link[0] & 0x08)".
4413 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4414 b2
= new_block(cstate
, JMP(BPF_JSET
));
4420 * For management frames, the SA is at 10.
4422 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4426 * OR that with the checks done for data frames.
4427 * That gives the checks done for management and
4433 * If the low-order bit of the type value is 1,
4434 * this is either a control frame or a frame
4435 * with a reserved type, and thus not a
4438 * I.e., check "!(link[0] & 0x04)".
4440 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4441 b1
= new_block(cstate
, JMP(BPF_JSET
));
4447 * AND that with the checks for data and management
4457 * For control frames, there is no DA.
4459 * For management frames, DA is at an
4460 * offset of 4 from the beginning of
4463 * For data frames, DA is at an offset
4464 * of 4 from the beginning of the packet
4465 * if To DS is clear and at an offset of
4466 * 16 from the beginning of the packet
4471 * Generate the tests to be done for data frames.
4473 * First, check for To DS set, i.e. "link[1] & 0x01".
4475 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4476 b1
= new_block(cstate
, JMP(BPF_JSET
));
4477 b1
->s
.k
= 0x01; /* To DS */
4481 * If To DS is set, the DA is at 16.
4483 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4487 * Now, check for To DS not set, i.e. check
4488 * "!(link[1] & 0x01)".
4490 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4491 b2
= new_block(cstate
, JMP(BPF_JSET
));
4492 b2
->s
.k
= 0x01; /* To DS */
4497 * If To DS is not set, the DA is at 4.
4499 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4503 * Now OR together the last two checks. That gives
4504 * the complete set of checks for data frames.
4509 * Now check for a data frame.
4510 * I.e, check "link[0] & 0x08".
4512 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4513 b1
= new_block(cstate
, JMP(BPF_JSET
));
4518 * AND that with the checks done for data frames.
4523 * If the high-order bit of the type value is 0, this
4524 * is a management frame.
4525 * I.e, check "!(link[0] & 0x08)".
4527 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4528 b2
= new_block(cstate
, JMP(BPF_JSET
));
4534 * For management frames, the DA is at 4.
4536 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4540 * OR that with the checks done for data frames.
4541 * That gives the checks done for management and
4547 * If the low-order bit of the type value is 1,
4548 * this is either a control frame or a frame
4549 * with a reserved type, and thus not a
4552 * I.e., check "!(link[0] & 0x04)".
4554 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4555 b1
= new_block(cstate
, JMP(BPF_JSET
));
4561 * AND that with the checks for data and management
4568 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4569 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4575 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4576 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4581 * XXX - add BSSID keyword?
4584 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4588 * Not present in CTS or ACK control frames.
4590 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4591 IEEE80211_FC0_TYPE_MASK
);
4593 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4594 IEEE80211_FC0_SUBTYPE_MASK
);
4596 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4597 IEEE80211_FC0_SUBTYPE_MASK
);
4601 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4607 * Not present in control frames.
4609 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4610 IEEE80211_FC0_TYPE_MASK
);
4612 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4618 * Present only if the direction mask has both "From DS"
4619 * and "To DS" set. Neither control frames nor management
4620 * frames should have both of those set, so we don't
4621 * check the frame type.
4623 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4624 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4625 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4631 * Not present in management frames; addr1 in other
4636 * If the high-order bit of the type value is 0, this
4637 * is a management frame.
4638 * I.e, check "(link[0] & 0x08)".
4640 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4641 b1
= new_block(cstate
, JMP(BPF_JSET
));
4648 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4651 * AND that with the check of addr1.
4658 * Not present in management frames; addr2, if present,
4663 * Not present in CTS or ACK control frames.
4665 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4666 IEEE80211_FC0_TYPE_MASK
);
4668 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4669 IEEE80211_FC0_SUBTYPE_MASK
);
4671 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4672 IEEE80211_FC0_SUBTYPE_MASK
);
4678 * If the high-order bit of the type value is 0, this
4679 * is a management frame.
4680 * I.e, check "(link[0] & 0x08)".
4682 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4683 b1
= new_block(cstate
, JMP(BPF_JSET
));
4688 * AND that with the check for frames other than
4689 * CTS and ACK frames.
4696 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4705 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4706 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4707 * as the RFC states.)
4709 static struct block
*
4710 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4712 register struct block
*b0
, *b1
;
4716 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4719 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4722 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4723 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4729 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4730 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4735 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4739 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4743 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4747 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4751 bpf_error(cstate
, "'ra' is only supported on 802.11");
4755 bpf_error(cstate
, "'ta' is only supported on 802.11");
4763 * This is quite tricky because there may be pad bytes in front of the
4764 * DECNET header, and then there are two possible data packet formats that
4765 * carry both src and dst addresses, plus 5 packet types in a format that
4766 * carries only the src node, plus 2 types that use a different format and
4767 * also carry just the src node.
4771 * Instead of doing those all right, we just look for data packets with
4772 * 0 or 1 bytes of padding. If you want to look at other packets, that
4773 * will require a lot more hacking.
4775 * To add support for filtering on DECNET "areas" (network numbers)
4776 * one would want to add a "mask" argument to this routine. That would
4777 * make the filter even more inefficient, although one could be clever
4778 * and not generate masking instructions if the mask is 0xFFFF.
4780 static struct block
*
4781 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4783 struct block
*b0
, *b1
, *b2
, *tmp
;
4784 u_int offset_lh
; /* offset if long header is received */
4785 u_int offset_sh
; /* offset if short header is received */
4790 offset_sh
= 1; /* follows flags */
4791 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4795 offset_sh
= 3; /* follows flags, dstnode */
4796 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4800 /* Inefficient because we do our Calvinball dance twice */
4801 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4802 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4808 /* Inefficient because we do our Calvinball dance twice */
4809 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4810 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4815 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4819 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4823 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4827 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4831 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4835 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4842 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4843 /* Check for pad = 1, long header case */
4844 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4845 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4846 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4847 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4849 /* Check for pad = 0, long header case */
4850 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4852 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4853 (bpf_u_int32
)ntohs((u_short
)addr
));
4856 /* Check for pad = 1, short header case */
4857 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4858 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4859 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4860 (bpf_u_int32
)ntohs((u_short
)addr
));
4863 /* Check for pad = 0, short header case */
4864 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4866 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4867 (bpf_u_int32
)ntohs((u_short
)addr
));
4871 /* Combine with test for cstate->linktype */
4877 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4878 * test the bottom-of-stack bit, and then check the version number
4879 * field in the IP header.
4881 static struct block
*
4882 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4884 struct block
*b0
, *b1
;
4889 /* match the bottom-of-stack bit */
4890 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4891 /* match the IPv4 version number */
4892 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4896 case ETHERTYPE_IPV6
:
4897 /* match the bottom-of-stack bit */
4898 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4899 /* match the IPv4 version number */
4900 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4905 /* FIXME add other L3 proto IDs */
4906 bpf_error(cstate
, "unsupported protocol over mpls");
4911 static struct block
*
4912 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4913 int proto
, int dir
, int type
)
4915 struct block
*b0
, *b1
;
4916 const char *typestr
;
4926 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4928 * Only check for non-IPv4 addresses if we're not
4929 * checking MPLS-encapsulated packets.
4931 if (cstate
->label_stack_depth
== 0) {
4932 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4934 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4940 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4943 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4946 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4949 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4952 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4955 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4958 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4961 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4964 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4967 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4970 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4973 return gen_dnhostop(cstate
, addr
, dir
);
4976 bpf_error(cstate
, "LAT host filtering not implemented");
4979 bpf_error(cstate
, "SCA host filtering not implemented");
4982 bpf_error(cstate
, "MOPRC host filtering not implemented");
4985 bpf_error(cstate
, "MOPDL host filtering not implemented");
4988 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4991 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4994 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4997 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5000 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5003 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5006 bpf_error(cstate
, "AARP host filtering not implemented");
5009 bpf_error(cstate
, "ISO host filtering not implemented");
5012 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5015 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5018 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5021 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5024 bpf_error(cstate
, "IPX host filtering not implemented");
5027 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5030 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5033 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5036 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5039 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5042 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5045 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5048 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5051 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5054 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5063 static struct block
*
5064 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5065 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5067 const char *typestr
;
5077 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5080 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5083 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5086 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5089 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5092 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5095 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5098 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5101 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5104 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5107 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5110 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5113 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5116 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5119 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5122 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5125 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5128 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5131 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5134 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5137 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5140 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5143 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5146 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5149 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5152 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5155 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5158 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5161 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5164 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5167 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5170 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5173 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5176 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5179 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5182 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5185 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5188 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5191 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5194 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5204 static struct block
*
5205 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5206 struct addrinfo
*alist
, int proto
, int dir
)
5208 struct block
*b0
, *b1
, *tmp
;
5209 struct addrinfo
*ai
;
5210 struct sockaddr_in
*sin
;
5213 bpf_error(cstate
, "direction applied to 'gateway'");
5220 switch (cstate
->linktype
) {
5222 case DLT_NETANALYZER
:
5223 case DLT_NETANALYZER_TRANSPARENT
:
5224 b1
= gen_prevlinkhdr_check(cstate
);
5225 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5230 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5233 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5235 case DLT_IEEE802_11
:
5236 case DLT_PRISM_HEADER
:
5237 case DLT_IEEE802_11_RADIO_AVS
:
5238 case DLT_IEEE802_11_RADIO
:
5240 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5244 * This is LLC-multiplexed traffic; if it were
5245 * LANE, cstate->linktype would have been set to
5249 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5251 case DLT_IP_OVER_FC
:
5252 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5256 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5259 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5261 * Does it have an address?
5263 if (ai
->ai_addr
!= NULL
) {
5265 * Yes. Is it an IPv4 address?
5267 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5269 * Generate an entry for it.
5271 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5272 tmp
= gen_host(cstate
,
5273 ntohl(sin
->sin_addr
.s_addr
),
5274 0xffffffff, proto
, Q_OR
, Q_HOST
);
5276 * Is it the *first* IPv4 address?
5280 * Yes, so start with it.
5285 * No, so OR it into the
5297 * No IPv4 addresses found.
5305 bpf_error(cstate
, "illegal modifier of 'gateway'");
5310 static struct block
*
5311 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5319 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5320 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5325 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5326 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5331 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5332 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5337 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5340 #ifndef IPPROTO_IGMP
5341 #define IPPROTO_IGMP 2
5345 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5348 #ifndef IPPROTO_IGRP
5349 #define IPPROTO_IGRP 9
5352 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5356 #define IPPROTO_PIM 103
5360 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5361 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5365 #ifndef IPPROTO_VRRP
5366 #define IPPROTO_VRRP 112
5370 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5373 #ifndef IPPROTO_CARP
5374 #define IPPROTO_CARP 112
5378 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5382 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5386 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5390 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5394 bpf_error(cstate
, "link layer applied in wrong context");
5397 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5401 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5405 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5409 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5413 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5417 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5421 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5425 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5428 #ifndef IPPROTO_ICMPV6
5429 #define IPPROTO_ICMPV6 58
5432 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5436 #define IPPROTO_AH 51
5439 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5440 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5445 #define IPPROTO_ESP 50
5448 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5449 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5454 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5458 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5462 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5465 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5466 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5467 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5469 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5471 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5473 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5477 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5478 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5479 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5481 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5483 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5485 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5489 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5490 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5491 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5493 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5498 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5499 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5504 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5505 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5507 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5509 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5514 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5515 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5520 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5521 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5526 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5530 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5534 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5538 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5542 bpf_error(cstate
, "'radio' is not a valid protocol type");
5551 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5554 * Catch errors reported by us and routines below us, and return NULL
5557 if (setjmp(cstate
->top_ctx
))
5560 return gen_proto_abbrev_internal(cstate
, proto
);
5563 static struct block
*
5564 gen_ipfrag(compiler_state_t
*cstate
)
5569 /* not IPv4 frag other than the first frag */
5570 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5571 b
= new_block(cstate
, JMP(BPF_JSET
));
5580 * Generate a comparison to a port value in the transport-layer header
5581 * at the specified offset from the beginning of that header.
5583 * XXX - this handles a variable-length prefix preceding the link-layer
5584 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5585 * variable-length link-layer headers (such as Token Ring or 802.11
5588 static struct block
*
5589 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5591 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5594 static struct block
*
5595 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5597 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5600 static struct block
*
5601 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5603 struct block
*b0
, *b1
, *tmp
;
5605 /* ip proto 'proto' and not a fragment other than the first fragment */
5606 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5607 b0
= gen_ipfrag(cstate
);
5612 b1
= gen_portatom(cstate
, 0, port
);
5616 b1
= gen_portatom(cstate
, 2, port
);
5620 tmp
= gen_portatom(cstate
, 0, port
);
5621 b1
= gen_portatom(cstate
, 2, port
);
5627 tmp
= gen_portatom(cstate
, 0, port
);
5628 b1
= gen_portatom(cstate
, 2, port
);
5633 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5637 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5641 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5645 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5649 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5653 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5665 static struct block
*
5666 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5668 struct block
*b0
, *b1
, *tmp
;
5673 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5674 * not LLC encapsulation with LLCSAP_IP.
5676 * For IEEE 802 networks - which includes 802.5 token ring
5677 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5678 * says that SNAP encapsulation is used, not LLC encapsulation
5681 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5682 * RFC 2225 say that SNAP encapsulation is used, not LLC
5683 * encapsulation with LLCSAP_IP.
5685 * So we always check for ETHERTYPE_IP.
5687 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5693 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5697 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5698 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5700 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5712 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5714 struct block
*b0
, *b1
, *tmp
;
5716 /* ip6 proto 'proto' */
5717 /* XXX - catch the first fragment of a fragmented packet? */
5718 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5722 b1
= gen_portatom6(cstate
, 0, port
);
5726 b1
= gen_portatom6(cstate
, 2, port
);
5730 tmp
= gen_portatom6(cstate
, 0, port
);
5731 b1
= gen_portatom6(cstate
, 2, port
);
5737 tmp
= gen_portatom6(cstate
, 0, port
);
5738 b1
= gen_portatom6(cstate
, 2, port
);
5750 static struct block
*
5751 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5753 struct block
*b0
, *b1
, *tmp
;
5755 /* link proto ip6 */
5756 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5762 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5766 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5767 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5769 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5780 /* gen_portrange code */
5781 static struct block
*
5782 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5785 struct block
*b1
, *b2
;
5789 * Reverse the order of the ports, so v1 is the lower one.
5798 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5799 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5806 static struct block
*
5807 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5808 bpf_u_int32 proto
, int dir
)
5810 struct block
*b0
, *b1
, *tmp
;
5812 /* ip proto 'proto' and not a fragment other than the first fragment */
5813 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5814 b0
= gen_ipfrag(cstate
);
5819 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5823 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5827 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5828 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5834 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5835 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5840 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5844 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5848 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5852 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5856 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5860 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5872 static struct block
*
5873 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5876 struct block
*b0
, *b1
, *tmp
;
5879 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5885 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5890 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5891 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5893 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5904 static struct block
*
5905 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5908 struct block
*b1
, *b2
;
5912 * Reverse the order of the ports, so v1 is the lower one.
5921 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5922 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5929 static struct block
*
5930 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5931 bpf_u_int32 proto
, int dir
)
5933 struct block
*b0
, *b1
, *tmp
;
5935 /* ip6 proto 'proto' */
5936 /* XXX - catch the first fragment of a fragmented packet? */
5937 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5941 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5945 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5949 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5950 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5956 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5957 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5969 static struct block
*
5970 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5973 struct block
*b0
, *b1
, *tmp
;
5975 /* link proto ip6 */
5976 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5982 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5987 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5988 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5990 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6002 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6011 v
= pcap_nametoproto(name
);
6012 if (v
== PROTO_UNDEF
)
6013 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6017 /* XXX should look up h/w protocol type based on cstate->linktype */
6018 v
= pcap_nametoeproto(name
);
6019 if (v
== PROTO_UNDEF
) {
6020 v
= pcap_nametollc(name
);
6021 if (v
== PROTO_UNDEF
)
6022 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6027 if (strcmp(name
, "esis") == 0)
6029 else if (strcmp(name
, "isis") == 0)
6031 else if (strcmp(name
, "clnp") == 0)
6034 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6046 gen_joinsp(struct stmt
**s
, int n
)
6052 static struct block
*
6053 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6055 #ifdef NO_PROTOCHAIN
6056 return gen_proto(cstate
, v
, proto
);
6058 struct block
*b0
, *b
;
6059 struct slist
*s
[100];
6060 int fix2
, fix3
, fix4
, fix5
;
6061 int ahcheck
, again
, end
;
6063 int reg2
= alloc_reg(cstate
);
6065 memset(s
, 0, sizeof(s
));
6066 fix3
= fix4
= fix5
= 0;
6073 b0
= gen_protochain(cstate
, v
, Q_IP
);
6074 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6078 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6083 * We don't handle variable-length prefixes before the link-layer
6084 * header, or variable-length link-layer headers, here yet.
6085 * We might want to add BPF instructions to do the protochain
6086 * work, to simplify that and, on platforms that have a BPF
6087 * interpreter with the new instructions, let the filtering
6088 * be done in the kernel. (We already require a modified BPF
6089 * engine to do the protochain stuff, to support backward
6090 * branches, and backward branch support is unlikely to appear
6091 * in kernel BPF engines.)
6093 if (cstate
->off_linkpl
.is_variable
)
6094 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6097 * To quote a comment in optimize.c:
6099 * "These data structures are used in a Cocke and Shwarz style
6100 * value numbering scheme. Since the flowgraph is acyclic,
6101 * exit values can be propagated from a node's predecessors
6102 * provided it is uniquely defined."
6104 * "Acyclic" means "no backward branches", which means "no
6105 * loops", so we have to turn the optimizer off.
6107 cstate
->no_optimize
= 1;
6110 * s[0] is a dummy entry to protect other BPF insn from damage
6111 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6112 * hard to find interdependency made by jump table fixup.
6115 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6120 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6123 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6124 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6126 /* X = ip->ip_hl << 2 */
6127 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6128 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6133 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6135 /* A = ip6->ip_nxt */
6136 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6137 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6139 /* X = sizeof(struct ip6_hdr) */
6140 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6146 bpf_error(cstate
, "unsupported proto to gen_protochain");
6150 /* again: if (A == v) goto end; else fall through; */
6152 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6154 s
[i
]->s
.jt
= NULL
; /*later*/
6155 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6159 #ifndef IPPROTO_NONE
6160 #define IPPROTO_NONE 59
6162 /* if (A == IPPROTO_NONE) goto end */
6163 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6164 s
[i
]->s
.jt
= NULL
; /*later*/
6165 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6166 s
[i
]->s
.k
= IPPROTO_NONE
;
6167 s
[fix5
]->s
.jf
= s
[i
];
6171 if (proto
== Q_IPV6
) {
6172 int v6start
, v6end
, v6advance
, j
;
6175 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6176 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6177 s
[i
]->s
.jt
= NULL
; /*later*/
6178 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6179 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6180 s
[fix2
]->s
.jf
= s
[i
];
6182 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6183 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6184 s
[i
]->s
.jt
= NULL
; /*later*/
6185 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6186 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6188 /* if (A == IPPROTO_ROUTING) goto v6advance */
6189 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6190 s
[i
]->s
.jt
= NULL
; /*later*/
6191 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6192 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6194 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6195 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6196 s
[i
]->s
.jt
= NULL
; /*later*/
6197 s
[i
]->s
.jf
= NULL
; /*later*/
6198 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6208 * A = P[X + packet head];
6209 * X = X + (P[X + packet head + 1] + 1) * 8;
6211 /* A = P[X + packet head] */
6212 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6213 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6216 s
[i
] = new_stmt(cstate
, BPF_ST
);
6219 /* A = P[X + packet head + 1]; */
6220 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6221 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6224 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6228 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6232 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6236 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6239 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6243 /* goto again; (must use BPF_JA for backward jump) */
6244 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6245 s
[i
]->s
.k
= again
- i
- 1;
6246 s
[i
- 1]->s
.jf
= s
[i
];
6250 for (j
= v6start
; j
<= v6end
; j
++)
6251 s
[j
]->s
.jt
= s
[v6advance
];
6254 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6256 s
[fix2
]->s
.jf
= s
[i
];
6262 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6263 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6264 s
[i
]->s
.jt
= NULL
; /*later*/
6265 s
[i
]->s
.jf
= NULL
; /*later*/
6266 s
[i
]->s
.k
= IPPROTO_AH
;
6268 s
[fix3
]->s
.jf
= s
[ahcheck
];
6275 * X = X + (P[X + 1] + 2) * 4;
6278 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6280 /* A = P[X + packet head]; */
6281 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6282 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6285 s
[i
] = new_stmt(cstate
, BPF_ST
);
6289 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6292 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6296 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6298 /* A = P[X + packet head] */
6299 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6300 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6303 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6307 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6311 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6314 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6318 /* goto again; (must use BPF_JA for backward jump) */
6319 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6320 s
[i
]->s
.k
= again
- i
- 1;
6325 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6327 s
[fix2
]->s
.jt
= s
[end
];
6328 s
[fix4
]->s
.jf
= s
[end
];
6329 s
[fix5
]->s
.jt
= s
[end
];
6336 for (i
= 0; i
< max
- 1; i
++)
6337 s
[i
]->next
= s
[i
+ 1];
6338 s
[max
- 1]->next
= NULL
;
6343 b
= new_block(cstate
, JMP(BPF_JEQ
));
6344 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6347 free_reg(cstate
, reg2
);
6354 static struct block
*
6355 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6358 struct block
*b0
, *b1
;
6361 * A data frame has the 0x08 bit (b3) in the frame control field set
6362 * and the 0x04 bit (b2) clear.
6364 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6365 b0
= new_block(cstate
, JMP(BPF_JSET
));
6369 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6370 b1
= new_block(cstate
, JMP(BPF_JSET
));
6381 * Generate code that checks whether the packet is a packet for protocol
6382 * <proto> and whether the type field in that protocol's header has
6383 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6384 * IP packet and checks the protocol number in the IP header against <v>.
6386 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6387 * against Q_IP and Q_IPV6.
6389 static struct block
*
6390 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6392 struct block
*b0
, *b1
;
6397 if (dir
!= Q_DEFAULT
)
6398 bpf_error(cstate
, "direction applied to 'proto'");
6402 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6403 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6408 return gen_linktype(cstate
, v
);
6412 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6413 * not LLC encapsulation with LLCSAP_IP.
6415 * For IEEE 802 networks - which includes 802.5 token ring
6416 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6417 * says that SNAP encapsulation is used, not LLC encapsulation
6420 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6421 * RFC 2225 say that SNAP encapsulation is used, not LLC
6422 * encapsulation with LLCSAP_IP.
6424 * So we always check for ETHERTYPE_IP.
6426 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6428 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6430 b1
= gen_protochain(cstate
, v
, Q_IP
);
6436 bpf_error(cstate
, "arp does not encapsulate another protocol");
6440 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6444 bpf_error(cstate
, "'sctp proto' is bogus");
6448 bpf_error(cstate
, "'tcp proto' is bogus");
6452 bpf_error(cstate
, "'udp proto' is bogus");
6456 bpf_error(cstate
, "'icmp proto' is bogus");
6460 bpf_error(cstate
, "'igmp proto' is bogus");
6464 bpf_error(cstate
, "'igrp proto' is bogus");
6468 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6472 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6476 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6480 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6484 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6488 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6492 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6495 * Also check for a fragment header before the final
6498 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6499 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6501 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6504 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6510 bpf_error(cstate
, "'icmp6 proto' is bogus");
6514 bpf_error(cstate
, "'ah proto' is bogus");
6518 bpf_error(cstate
, "'esp proto' is bogus");
6522 bpf_error(cstate
, "'pim proto' is bogus");
6526 bpf_error(cstate
, "'vrrp proto' is bogus");
6530 bpf_error(cstate
, "'aarp proto' is bogus");
6534 switch (cstate
->linktype
) {
6538 * Frame Relay packets typically have an OSI
6539 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6540 * generates code to check for all the OSI
6541 * NLPIDs, so calling it and then adding a check
6542 * for the particular NLPID for which we're
6543 * looking is bogus, as we can just check for
6546 * What we check for is the NLPID and a frame
6547 * control field value of UI, i.e. 0x03 followed
6550 * XXX - assumes a 2-byte Frame Relay header with
6551 * DLCI and flags. What if the address is longer?
6553 * XXX - what about SNAP-encapsulated frames?
6555 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6561 * Cisco uses an Ethertype lookalike - for OSI,
6564 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6565 /* OSI in C-HDLC is stuffed with a fudge byte */
6566 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6571 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6572 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6578 bpf_error(cstate
, "'esis proto' is bogus");
6582 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6584 * 4 is the offset of the PDU type relative to the IS-IS
6587 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6592 bpf_error(cstate
, "'clnp proto' is not supported");
6596 bpf_error(cstate
, "'stp proto' is bogus");
6600 bpf_error(cstate
, "'ipx proto' is bogus");
6604 bpf_error(cstate
, "'netbeui proto' is bogus");
6608 bpf_error(cstate
, "'l1 proto' is bogus");
6612 bpf_error(cstate
, "'l2 proto' is bogus");
6616 bpf_error(cstate
, "'iih proto' is bogus");
6620 bpf_error(cstate
, "'snp proto' is bogus");
6624 bpf_error(cstate
, "'csnp proto' is bogus");
6628 bpf_error(cstate
, "'psnp proto' is bogus");
6632 bpf_error(cstate
, "'lsp proto' is bogus");
6636 bpf_error(cstate
, "'radio proto' is bogus");
6640 bpf_error(cstate
, "'carp proto' is bogus");
6651 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6653 int proto
= q
.proto
;
6657 bpf_u_int32 mask
, addr
;
6658 struct addrinfo
*res
, *res0
;
6659 struct sockaddr_in
*sin4
;
6662 struct sockaddr_in6
*sin6
;
6663 struct in6_addr mask128
;
6665 struct block
*b
, *tmp
;
6666 int port
, real_proto
;
6670 * Catch errors reported by us and routines below us, and return NULL
6673 if (setjmp(cstate
->top_ctx
))
6679 addr
= pcap_nametonetaddr(name
);
6681 bpf_error(cstate
, "unknown network '%s'", name
);
6682 /* Left justify network addr and calculate its network mask */
6684 while (addr
&& (addr
& 0xff000000) == 0) {
6688 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6692 if (proto
== Q_LINK
) {
6693 switch (cstate
->linktype
) {
6696 case DLT_NETANALYZER
:
6697 case DLT_NETANALYZER_TRANSPARENT
:
6698 eaddr
= pcap_ether_hostton(name
);
6701 "unknown ether host '%s'", name
);
6702 tmp
= gen_prevlinkhdr_check(cstate
);
6703 b
= gen_ehostop(cstate
, eaddr
, dir
);
6710 eaddr
= pcap_ether_hostton(name
);
6713 "unknown FDDI host '%s'", name
);
6714 b
= gen_fhostop(cstate
, eaddr
, dir
);
6719 eaddr
= pcap_ether_hostton(name
);
6722 "unknown token ring host '%s'", name
);
6723 b
= gen_thostop(cstate
, eaddr
, dir
);
6727 case DLT_IEEE802_11
:
6728 case DLT_PRISM_HEADER
:
6729 case DLT_IEEE802_11_RADIO_AVS
:
6730 case DLT_IEEE802_11_RADIO
:
6732 eaddr
= pcap_ether_hostton(name
);
6735 "unknown 802.11 host '%s'", name
);
6736 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6740 case DLT_IP_OVER_FC
:
6741 eaddr
= pcap_ether_hostton(name
);
6744 "unknown Fibre Channel host '%s'", name
);
6745 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6750 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6751 } else if (proto
== Q_DECNET
) {
6752 unsigned short dn_addr
;
6754 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6756 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6758 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6763 * I don't think DECNET hosts can be multihomed, so
6764 * there is no need to build up a list of addresses
6766 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6769 memset(&mask128
, 0xff, sizeof(mask128
));
6771 res0
= res
= pcap_nametoaddrinfo(name
);
6773 bpf_error(cstate
, "unknown host '%s'", name
);
6780 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6781 tproto
== Q_DEFAULT
) {
6787 for (res
= res0
; res
; res
= res
->ai_next
) {
6788 switch (res
->ai_family
) {
6791 if (tproto
== Q_IPV6
)
6795 sin4
= (struct sockaddr_in
*)
6797 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6798 0xffffffff, tproto
, dir
, q
.addr
);
6802 if (tproto6
== Q_IP
)
6805 sin6
= (struct sockaddr_in6
*)
6807 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6808 &mask128
, tproto6
, dir
, q
.addr
);
6821 bpf_error(cstate
, "unknown host '%s'%s", name
,
6822 (proto
== Q_DEFAULT
)
6824 : " for specified address family");
6830 if (proto
!= Q_DEFAULT
&&
6831 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6832 bpf_error(cstate
, "illegal qualifier of 'port'");
6833 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6834 bpf_error(cstate
, "unknown port '%s'", name
);
6835 if (proto
== Q_UDP
) {
6836 if (real_proto
== IPPROTO_TCP
)
6837 bpf_error(cstate
, "port '%s' is tcp", name
);
6838 else if (real_proto
== IPPROTO_SCTP
)
6839 bpf_error(cstate
, "port '%s' is sctp", name
);
6841 /* override PROTO_UNDEF */
6842 real_proto
= IPPROTO_UDP
;
6844 if (proto
== Q_TCP
) {
6845 if (real_proto
== IPPROTO_UDP
)
6846 bpf_error(cstate
, "port '%s' is udp", name
);
6848 else if (real_proto
== IPPROTO_SCTP
)
6849 bpf_error(cstate
, "port '%s' is sctp", name
);
6851 /* override PROTO_UNDEF */
6852 real_proto
= IPPROTO_TCP
;
6854 if (proto
== Q_SCTP
) {
6855 if (real_proto
== IPPROTO_UDP
)
6856 bpf_error(cstate
, "port '%s' is udp", name
);
6858 else if (real_proto
== IPPROTO_TCP
)
6859 bpf_error(cstate
, "port '%s' is tcp", name
);
6861 /* override PROTO_UNDEF */
6862 real_proto
= IPPROTO_SCTP
;
6865 bpf_error(cstate
, "illegal port number %d < 0", port
);
6867 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6868 b
= gen_port(cstate
, port
, real_proto
, dir
);
6869 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6873 if (proto
!= Q_DEFAULT
&&
6874 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6875 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6876 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6877 bpf_error(cstate
, "unknown port in range '%s'", name
);
6878 if (proto
== Q_UDP
) {
6879 if (real_proto
== IPPROTO_TCP
)
6880 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6881 else if (real_proto
== IPPROTO_SCTP
)
6882 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6884 /* override PROTO_UNDEF */
6885 real_proto
= IPPROTO_UDP
;
6887 if (proto
== Q_TCP
) {
6888 if (real_proto
== IPPROTO_UDP
)
6889 bpf_error(cstate
, "port in range '%s' is udp", name
);
6890 else if (real_proto
== IPPROTO_SCTP
)
6891 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6893 /* override PROTO_UNDEF */
6894 real_proto
= IPPROTO_TCP
;
6896 if (proto
== Q_SCTP
) {
6897 if (real_proto
== IPPROTO_UDP
)
6898 bpf_error(cstate
, "port in range '%s' is udp", name
);
6899 else if (real_proto
== IPPROTO_TCP
)
6900 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6902 /* override PROTO_UNDEF */
6903 real_proto
= IPPROTO_SCTP
;
6906 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6908 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6910 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6912 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6914 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6915 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6920 eaddr
= pcap_ether_hostton(name
);
6922 bpf_error(cstate
, "unknown ether host: %s", name
);
6924 res
= pcap_nametoaddrinfo(name
);
6927 bpf_error(cstate
, "unknown host '%s'", name
);
6928 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6932 bpf_error(cstate
, "unknown host '%s'", name
);
6935 bpf_error(cstate
, "'gateway' not supported in this configuration");
6939 real_proto
= lookup_proto(cstate
, name
, proto
);
6940 if (real_proto
>= 0)
6941 return gen_proto(cstate
, real_proto
, proto
, dir
);
6943 bpf_error(cstate
, "unknown protocol: %s", name
);
6946 real_proto
= lookup_proto(cstate
, name
, proto
);
6947 if (real_proto
>= 0)
6948 return gen_protochain(cstate
, real_proto
, proto
);
6950 bpf_error(cstate
, "unknown protocol: %s", name
);
6961 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6962 bpf_u_int32 masklen
, struct qual q
)
6964 register int nlen
, mlen
;
6968 * Catch errors reported by us and routines below us, and return NULL
6971 if (setjmp(cstate
->top_ctx
))
6974 nlen
= __pcap_atoin(s1
, &n
);
6976 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
6977 /* Promote short ipaddr */
6981 mlen
= __pcap_atoin(s2
, &m
);
6983 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
6984 /* Promote short ipaddr */
6987 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6990 /* Convert mask len to mask */
6992 bpf_error(cstate
, "mask length must be <= 32");
6995 * X << 32 is not guaranteed by C to be 0; it's
7000 m
= 0xffffffff << (32 - masklen
);
7002 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7009 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7012 bpf_error(cstate
, "Mask syntax for networks only");
7019 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7027 * Catch errors reported by us and routines below us, and return NULL
7030 if (setjmp(cstate
->top_ctx
))
7037 else if (q
.proto
== Q_DECNET
) {
7038 vlen
= __pcap_atodn(s
, &v
);
7040 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7042 vlen
= __pcap_atoin(s
, &v
);
7044 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7052 if (proto
== Q_DECNET
)
7053 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7054 else if (proto
== Q_LINK
) {
7055 bpf_error(cstate
, "illegal link layer address");
7058 if (s
== NULL
&& q
.addr
== Q_NET
) {
7059 /* Promote short net number */
7060 while (v
&& (v
& 0xff000000) == 0) {
7065 /* Promote short ipaddr */
7067 mask
<<= 32 - vlen
;
7069 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7074 proto
= IPPROTO_UDP
;
7075 else if (proto
== Q_TCP
)
7076 proto
= IPPROTO_TCP
;
7077 else if (proto
== Q_SCTP
)
7078 proto
= IPPROTO_SCTP
;
7079 else if (proto
== Q_DEFAULT
)
7080 proto
= PROTO_UNDEF
;
7082 bpf_error(cstate
, "illegal qualifier of 'port'");
7085 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7089 b
= gen_port(cstate
, v
, proto
, dir
);
7090 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7096 proto
= IPPROTO_UDP
;
7097 else if (proto
== Q_TCP
)
7098 proto
= IPPROTO_TCP
;
7099 else if (proto
== Q_SCTP
)
7100 proto
= IPPROTO_SCTP
;
7101 else if (proto
== Q_DEFAULT
)
7102 proto
= PROTO_UNDEF
;
7104 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7107 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7111 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7112 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7117 bpf_error(cstate
, "'gateway' requires a name");
7121 return gen_proto(cstate
, v
, proto
, dir
);
7124 return gen_protochain(cstate
, v
, proto
);
7139 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7140 bpf_u_int32 masklen
, struct qual q
)
7142 struct addrinfo
*res
;
7143 struct in6_addr
*addr
;
7144 struct in6_addr mask
;
7149 * Catch errors reported by us and routines below us, and return NULL
7152 if (setjmp(cstate
->top_ctx
))
7156 bpf_error(cstate
, "no mask %s supported", s2
);
7158 res
= pcap_nametoaddrinfo(s1
);
7160 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7163 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7164 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7166 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7167 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
.s6_addr
) * 8));
7168 memset(&mask
, 0, sizeof(mask
));
7169 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7171 mask
.s6_addr
[masklen
/ 8] =
7172 (0xff << (8 - masklen
% 8)) & 0xff;
7175 a
= (uint32_t *)addr
;
7176 m
= (uint32_t *)&mask
;
7177 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7178 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7179 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7187 bpf_error(cstate
, "Mask syntax for networks only");
7191 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7197 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7204 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7206 struct block
*b
, *tmp
;
7209 * Catch errors reported by us and routines below us, and return NULL
7212 if (setjmp(cstate
->top_ctx
))
7215 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7216 cstate
->e
= pcap_ether_aton(s
);
7217 if (cstate
->e
== NULL
)
7218 bpf_error(cstate
, "malloc");
7219 switch (cstate
->linktype
) {
7221 case DLT_NETANALYZER
:
7222 case DLT_NETANALYZER_TRANSPARENT
:
7223 tmp
= gen_prevlinkhdr_check(cstate
);
7224 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7229 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7232 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7234 case DLT_IEEE802_11
:
7235 case DLT_PRISM_HEADER
:
7236 case DLT_IEEE802_11_RADIO_AVS
:
7237 case DLT_IEEE802_11_RADIO
:
7239 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7241 case DLT_IP_OVER_FC
:
7242 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7247 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7254 bpf_error(cstate
, "ethernet address used in non-ether expression");
7259 sappend(struct slist
*s0
, struct slist
*s1
)
7262 * This is definitely not the best way to do this, but the
7263 * lists will rarely get long.
7270 static struct slist
*
7271 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7275 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7280 static struct slist
*
7281 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7285 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7291 * Modify "index" to use the value stored into its register as an
7292 * offset relative to the beginning of the header for the protocol
7293 * "proto", and allocate a register and put an item "size" bytes long
7294 * (1, 2, or 4) at that offset into that register, making it the register
7297 static struct arth
*
7298 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7302 struct slist
*s
, *tmp
;
7304 int regno
= alloc_reg(cstate
);
7306 free_reg(cstate
, inst
->regno
);
7310 bpf_error(cstate
, "data size must be 1, 2, or 4");
7327 bpf_error(cstate
, "unsupported index operation");
7331 * The offset is relative to the beginning of the packet
7332 * data, if we have a radio header. (If we don't, this
7335 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7336 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7337 cstate
->linktype
!= DLT_PRISM_HEADER
)
7338 bpf_error(cstate
, "radio information not present in capture");
7341 * Load into the X register the offset computed into the
7342 * register specified by "index".
7344 s
= xfer_to_x(cstate
, inst
);
7347 * Load the item at that offset.
7349 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7351 sappend(inst
->s
, s
);
7356 * The offset is relative to the beginning of
7357 * the link-layer header.
7359 * XXX - what about ATM LANE? Should the index be
7360 * relative to the beginning of the AAL5 frame, so
7361 * that 0 refers to the beginning of the LE Control
7362 * field, or relative to the beginning of the LAN
7363 * frame, so that 0 refers, for Ethernet LANE, to
7364 * the beginning of the destination address?
7366 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7369 * If "s" is non-null, it has code to arrange that the
7370 * X register contains the length of the prefix preceding
7371 * the link-layer header. Add to it the offset computed
7372 * into the register specified by "index", and move that
7373 * into the X register. Otherwise, just load into the X
7374 * register the offset computed into the register specified
7378 sappend(s
, xfer_to_a(cstate
, inst
));
7379 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7380 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7382 s
= xfer_to_x(cstate
, inst
);
7385 * Load the item at the sum of the offset we've put in the
7386 * X register and the offset of the start of the link
7387 * layer header (which is 0 if the radio header is
7388 * variable-length; that header length is what we put
7389 * into the X register and then added to the index).
7391 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7392 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7394 sappend(inst
->s
, s
);
7408 * The offset is relative to the beginning of
7409 * the network-layer header.
7410 * XXX - are there any cases where we want
7411 * cstate->off_nl_nosnap?
7413 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7416 * If "s" is non-null, it has code to arrange that the
7417 * X register contains the variable part of the offset
7418 * of the link-layer payload. Add to it the offset
7419 * computed into the register specified by "index",
7420 * and move that into the X register. Otherwise, just
7421 * load into the X register the offset computed into
7422 * the register specified by "index".
7425 sappend(s
, xfer_to_a(cstate
, inst
));
7426 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7427 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7429 s
= xfer_to_x(cstate
, inst
);
7432 * Load the item at the sum of the offset we've put in the
7433 * X register, the offset of the start of the network
7434 * layer header from the beginning of the link-layer
7435 * payload, and the constant part of the offset of the
7436 * start of the link-layer payload.
7438 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7439 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7441 sappend(inst
->s
, s
);
7444 * Do the computation only if the packet contains
7445 * the protocol in question.
7447 b
= gen_proto_abbrev_internal(cstate
, proto
);
7449 gen_and(inst
->b
, b
);
7463 * The offset is relative to the beginning of
7464 * the transport-layer header.
7466 * Load the X register with the length of the IPv4 header
7467 * (plus the offset of the link-layer header, if it's
7468 * a variable-length header), in bytes.
7470 * XXX - are there any cases where we want
7471 * cstate->off_nl_nosnap?
7472 * XXX - we should, if we're built with
7473 * IPv6 support, generate code to load either
7474 * IPv4, IPv6, or both, as appropriate.
7476 s
= gen_loadx_iphdrlen(cstate
);
7479 * The X register now contains the sum of the variable
7480 * part of the offset of the link-layer payload and the
7481 * length of the network-layer header.
7483 * Load into the A register the offset relative to
7484 * the beginning of the transport layer header,
7485 * add the X register to that, move that to the
7486 * X register, and load with an offset from the
7487 * X register equal to the sum of the constant part of
7488 * the offset of the link-layer payload and the offset,
7489 * relative to the beginning of the link-layer payload,
7490 * of the network-layer header.
7492 sappend(s
, xfer_to_a(cstate
, inst
));
7493 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7494 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7495 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7496 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7497 sappend(inst
->s
, s
);
7500 * Do the computation only if the packet contains
7501 * the protocol in question - which is true only
7502 * if this is an IP datagram and is the first or
7503 * only fragment of that datagram.
7505 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7507 gen_and(inst
->b
, b
);
7508 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7513 * Do the computation only if the packet contains
7514 * the protocol in question.
7516 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7518 gen_and(inst
->b
, b
);
7523 * Check if we have an icmp6 next header
7525 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7527 gen_and(inst
->b
, b
);
7532 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7534 * If "s" is non-null, it has code to arrange that the
7535 * X register contains the variable part of the offset
7536 * of the link-layer payload. Add to it the offset
7537 * computed into the register specified by "index",
7538 * and move that into the X register. Otherwise, just
7539 * load into the X register the offset computed into
7540 * the register specified by "index".
7543 sappend(s
, xfer_to_a(cstate
, inst
));
7544 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7545 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7547 s
= xfer_to_x(cstate
, inst
);
7551 * Load the item at the sum of the offset we've put in the
7552 * X register, the offset of the start of the network
7553 * layer header from the beginning of the link-layer
7554 * payload, and the constant part of the offset of the
7555 * start of the link-layer payload.
7557 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7558 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7561 sappend(inst
->s
, s
);
7565 inst
->regno
= regno
;
7566 s
= new_stmt(cstate
, BPF_ST
);
7568 sappend(inst
->s
, s
);
7574 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7578 * Catch errors reported by us and routines below us, and return NULL
7581 if (setjmp(cstate
->top_ctx
))
7584 return gen_load_internal(cstate
, proto
, inst
, size
);
7587 static struct block
*
7588 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7589 struct arth
*a1
, int reversed
)
7591 struct slist
*s0
, *s1
, *s2
;
7592 struct block
*b
, *tmp
;
7594 s0
= xfer_to_x(cstate
, a1
);
7595 s1
= xfer_to_a(cstate
, a0
);
7596 if (code
== BPF_JEQ
) {
7597 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7598 b
= new_block(cstate
, JMP(code
));
7602 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7608 sappend(a0
->s
, a1
->s
);
7612 free_reg(cstate
, a0
->regno
);
7613 free_reg(cstate
, a1
->regno
);
7615 /* 'and' together protocol checks */
7618 gen_and(a0
->b
, tmp
= a1
->b
);
7632 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7633 struct arth
*a1
, int reversed
)
7636 * Catch errors reported by us and routines below us, and return NULL
7639 if (setjmp(cstate
->top_ctx
))
7642 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7646 gen_loadlen(compiler_state_t
*cstate
)
7653 * Catch errors reported by us and routines below us, and return NULL
7656 if (setjmp(cstate
->top_ctx
))
7659 regno
= alloc_reg(cstate
);
7660 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7661 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7662 s
->next
= new_stmt(cstate
, BPF_ST
);
7663 s
->next
->s
.k
= regno
;
7670 static struct arth
*
7671 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7677 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7679 reg
= alloc_reg(cstate
);
7681 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7683 s
->next
= new_stmt(cstate
, BPF_ST
);
7692 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7695 * Catch errors reported by us and routines below us, and return NULL
7698 if (setjmp(cstate
->top_ctx
))
7701 return gen_loadi_internal(cstate
, val
);
7705 * The a_arg dance is to avoid annoying whining by compilers that
7706 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7707 * It's not *used* after setjmp returns.
7710 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7712 struct arth
*a
= a_arg
;
7716 * Catch errors reported by us and routines below us, and return NULL
7719 if (setjmp(cstate
->top_ctx
))
7722 s
= xfer_to_a(cstate
, a
);
7724 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7727 s
= new_stmt(cstate
, BPF_ST
);
7735 * The a0_arg dance is to avoid annoying whining by compilers that
7736 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7737 * It's not *used* after setjmp returns.
7740 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7743 struct arth
*a0
= a0_arg
;
7744 struct slist
*s0
, *s1
, *s2
;
7747 * Catch errors reported by us and routines below us, and return NULL
7750 if (setjmp(cstate
->top_ctx
))
7754 * Disallow division by, or modulus by, zero; we do this here
7755 * so that it gets done even if the optimizer is disabled.
7757 * Also disallow shifts by a value greater than 31; we do this
7758 * here, for the same reason.
7760 if (code
== BPF_DIV
) {
7761 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7762 bpf_error(cstate
, "division by zero");
7763 } else if (code
== BPF_MOD
) {
7764 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7765 bpf_error(cstate
, "modulus by zero");
7766 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7767 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7768 bpf_error(cstate
, "shift by more than 31 bits");
7770 s0
= xfer_to_x(cstate
, a1
);
7771 s1
= xfer_to_a(cstate
, a0
);
7772 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7777 sappend(a0
->s
, a1
->s
);
7779 free_reg(cstate
, a0
->regno
);
7780 free_reg(cstate
, a1
->regno
);
7782 s0
= new_stmt(cstate
, BPF_ST
);
7783 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7790 * Initialize the table of used registers and the current register.
7793 init_regs(compiler_state_t
*cstate
)
7796 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7800 * Return the next free register.
7803 alloc_reg(compiler_state_t
*cstate
)
7805 int n
= BPF_MEMWORDS
;
7808 if (cstate
->regused
[cstate
->curreg
])
7809 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7811 cstate
->regused
[cstate
->curreg
] = 1;
7812 return cstate
->curreg
;
7815 bpf_error(cstate
, "too many registers needed to evaluate expression");
7820 * Return a register to the table so it can
7824 free_reg(compiler_state_t
*cstate
, int n
)
7826 cstate
->regused
[n
] = 0;
7829 static struct block
*
7830 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7835 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7836 b
= new_block(cstate
, JMP(jmp
));
7844 gen_greater(compiler_state_t
*cstate
, int n
)
7847 * Catch errors reported by us and routines below us, and return NULL
7850 if (setjmp(cstate
->top_ctx
))
7853 return gen_len(cstate
, BPF_JGE
, n
);
7857 * Actually, this is less than or equal.
7860 gen_less(compiler_state_t
*cstate
, int n
)
7865 * Catch errors reported by us and routines below us, and return NULL
7868 if (setjmp(cstate
->top_ctx
))
7871 b
= gen_len(cstate
, BPF_JGT
, n
);
7878 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7879 * the beginning of the link-layer header.
7880 * XXX - that means you can't test values in the radiotap header, but
7881 * as that header is difficult if not impossible to parse generally
7882 * without a loop, that might not be a severe problem. A new keyword
7883 * "radio" could be added for that, although what you'd really want
7884 * would be a way of testing particular radio header values, which
7885 * would generate code appropriate to the radio header in question.
7888 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7894 * Catch errors reported by us and routines below us, and return NULL
7897 if (setjmp(cstate
->top_ctx
))
7905 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7908 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7912 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7916 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7920 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7924 b
= new_block(cstate
, JMP(BPF_JEQ
));
7931 static const u_char abroadcast
[] = { 0x0 };
7934 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7936 bpf_u_int32 hostmask
;
7937 struct block
*b0
, *b1
, *b2
;
7938 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7941 * Catch errors reported by us and routines below us, and return NULL
7944 if (setjmp(cstate
->top_ctx
))
7951 switch (cstate
->linktype
) {
7953 case DLT_ARCNET_LINUX
:
7954 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7956 case DLT_NETANALYZER
:
7957 case DLT_NETANALYZER_TRANSPARENT
:
7958 b1
= gen_prevlinkhdr_check(cstate
);
7959 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7964 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7966 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7967 case DLT_IEEE802_11
:
7968 case DLT_PRISM_HEADER
:
7969 case DLT_IEEE802_11_RADIO_AVS
:
7970 case DLT_IEEE802_11_RADIO
:
7972 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7973 case DLT_IP_OVER_FC
:
7974 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7976 bpf_error(cstate
, "not a broadcast link");
7982 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7983 * as an indication that we don't know the netmask, and fail
7986 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7987 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7988 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7989 hostmask
= ~cstate
->netmask
;
7990 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
7991 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7992 ~0 & hostmask
, hostmask
);
7997 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8002 * Generate code to test the low-order bit of a MAC address (that's
8003 * the bottom bit of the *first* byte).
8005 static struct block
*
8006 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8008 register struct block
*b0
;
8009 register struct slist
*s
;
8011 /* link[offset] & 1 != 0 */
8012 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8013 b0
= new_block(cstate
, JMP(BPF_JSET
));
8020 gen_multicast(compiler_state_t
*cstate
, int proto
)
8022 register struct block
*b0
, *b1
, *b2
;
8023 register struct slist
*s
;
8026 * Catch errors reported by us and routines below us, and return NULL
8029 if (setjmp(cstate
->top_ctx
))
8036 switch (cstate
->linktype
) {
8038 case DLT_ARCNET_LINUX
:
8039 /* all ARCnet multicasts use the same address */
8040 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8042 case DLT_NETANALYZER
:
8043 case DLT_NETANALYZER_TRANSPARENT
:
8044 b1
= gen_prevlinkhdr_check(cstate
);
8045 /* ether[0] & 1 != 0 */
8046 b0
= gen_mac_multicast(cstate
, 0);
8052 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8054 * XXX - was that referring to bit-order issues?
8056 /* fddi[1] & 1 != 0 */
8057 return gen_mac_multicast(cstate
, 1);
8059 /* tr[2] & 1 != 0 */
8060 return gen_mac_multicast(cstate
, 2);
8061 case DLT_IEEE802_11
:
8062 case DLT_PRISM_HEADER
:
8063 case DLT_IEEE802_11_RADIO_AVS
:
8064 case DLT_IEEE802_11_RADIO
:
8069 * For control frames, there is no DA.
8071 * For management frames, DA is at an
8072 * offset of 4 from the beginning of
8075 * For data frames, DA is at an offset
8076 * of 4 from the beginning of the packet
8077 * if To DS is clear and at an offset of
8078 * 16 from the beginning of the packet
8083 * Generate the tests to be done for data frames.
8085 * First, check for To DS set, i.e. "link[1] & 0x01".
8087 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8088 b1
= new_block(cstate
, JMP(BPF_JSET
));
8089 b1
->s
.k
= 0x01; /* To DS */
8093 * If To DS is set, the DA is at 16.
8095 b0
= gen_mac_multicast(cstate
, 16);
8099 * Now, check for To DS not set, i.e. check
8100 * "!(link[1] & 0x01)".
8102 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8103 b2
= new_block(cstate
, JMP(BPF_JSET
));
8104 b2
->s
.k
= 0x01; /* To DS */
8109 * If To DS is not set, the DA is at 4.
8111 b1
= gen_mac_multicast(cstate
, 4);
8115 * Now OR together the last two checks. That gives
8116 * the complete set of checks for data frames.
8121 * Now check for a data frame.
8122 * I.e, check "link[0] & 0x08".
8124 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8125 b1
= new_block(cstate
, JMP(BPF_JSET
));
8130 * AND that with the checks done for data frames.
8135 * If the high-order bit of the type value is 0, this
8136 * is a management frame.
8137 * I.e, check "!(link[0] & 0x08)".
8139 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8140 b2
= new_block(cstate
, JMP(BPF_JSET
));
8146 * For management frames, the DA is at 4.
8148 b1
= gen_mac_multicast(cstate
, 4);
8152 * OR that with the checks done for data frames.
8153 * That gives the checks done for management and
8159 * If the low-order bit of the type value is 1,
8160 * this is either a control frame or a frame
8161 * with a reserved type, and thus not a
8164 * I.e., check "!(link[0] & 0x04)".
8166 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8167 b1
= new_block(cstate
, JMP(BPF_JSET
));
8173 * AND that with the checks for data and management
8178 case DLT_IP_OVER_FC
:
8179 b0
= gen_mac_multicast(cstate
, 2);
8184 /* Link not known to support multicasts */
8188 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8189 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8194 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8195 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8199 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8204 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8206 register struct block
*b0
;
8209 * Catch errors reported by us and routines below us, and return NULL
8212 if (setjmp(cstate
->top_ctx
))
8216 * Only some data link types support ifindex qualifiers.
8218 switch (cstate
->linktype
) {
8219 case DLT_LINUX_SLL2
:
8220 /* match packets on this interface */
8221 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8226 * This is Linux; we require PF_PACKET support.
8227 * If this is a *live* capture, we can look at
8228 * special meta-data in the filter expression;
8229 * if it's a savefile, we can't.
8231 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8232 /* We have a FILE *, so this is a savefile */
8233 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8234 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8239 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8241 #else /* defined(linux) */
8242 bpf_error(cstate
, "ifindex not supported on %s",
8243 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8245 #endif /* defined(linux) */
8251 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8252 * Outbound traffic is sent by this machine, while inbound traffic is
8253 * sent by a remote machine (and may include packets destined for a
8254 * unicast or multicast link-layer address we are not subscribing to).
8255 * These are the same definitions implemented by pcap_setdirection().
8256 * Capturing only unicast traffic destined for this host is probably
8257 * better accomplished using a higher-layer filter.
8260 gen_inbound(compiler_state_t
*cstate
, int dir
)
8262 register struct block
*b0
;
8265 * Catch errors reported by us and routines below us, and return NULL
8268 if (setjmp(cstate
->top_ctx
))
8272 * Only some data link types support inbound/outbound qualifiers.
8274 switch (cstate
->linktype
) {
8276 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8277 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8278 gen_loadi_internal(cstate
, 0),
8284 /* match outgoing packets */
8285 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8287 /* match incoming packets */
8288 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8293 /* match outgoing packets */
8294 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8296 /* to filter on inbound traffic, invert the match */
8301 case DLT_LINUX_SLL2
:
8302 /* match outgoing packets */
8303 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8305 /* to filter on inbound traffic, invert the match */
8310 #ifdef HAVE_NET_PFVAR_H
8312 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8313 ((dir
== 0) ? PF_IN
: PF_OUT
));
8319 /* match outgoing packets */
8320 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8322 /* match incoming packets */
8323 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8327 case DLT_JUNIPER_MFR
:
8328 case DLT_JUNIPER_MLFR
:
8329 case DLT_JUNIPER_MLPPP
:
8330 case DLT_JUNIPER_ATM1
:
8331 case DLT_JUNIPER_ATM2
:
8332 case DLT_JUNIPER_PPPOE
:
8333 case DLT_JUNIPER_PPPOE_ATM
:
8334 case DLT_JUNIPER_GGSN
:
8335 case DLT_JUNIPER_ES
:
8336 case DLT_JUNIPER_MONITOR
:
8337 case DLT_JUNIPER_SERVICES
:
8338 case DLT_JUNIPER_ETHER
:
8339 case DLT_JUNIPER_PPP
:
8340 case DLT_JUNIPER_FRELAY
:
8341 case DLT_JUNIPER_CHDLC
:
8342 case DLT_JUNIPER_VP
:
8343 case DLT_JUNIPER_ST
:
8344 case DLT_JUNIPER_ISM
:
8345 case DLT_JUNIPER_VS
:
8346 case DLT_JUNIPER_SRX_E2E
:
8347 case DLT_JUNIPER_FIBRECHANNEL
:
8348 case DLT_JUNIPER_ATM_CEMIC
:
8350 /* juniper flags (including direction) are stored
8351 * the byte after the 3-byte magic number */
8353 /* match outgoing packets */
8354 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8356 /* match incoming packets */
8357 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8363 * If we have packet meta-data indicating a direction,
8364 * and that metadata can be checked by BPF code, check
8365 * it. Otherwise, give up, as this link-layer type has
8366 * nothing in the packet data.
8368 * Currently, the only platform where a BPF filter can
8369 * check that metadata is Linux with the in-kernel
8370 * BPF interpreter. If other packet capture mechanisms
8371 * and BPF filters also supported this, it would be
8372 * nice. It would be even better if they made that
8373 * metadata available so that we could provide it
8374 * with newer capture APIs, allowing it to be saved
8379 * This is Linux; we require PF_PACKET support.
8380 * If this is a *live* capture, we can look at
8381 * special meta-data in the filter expression;
8382 * if it's a savefile, we can't.
8384 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8385 /* We have a FILE *, so this is a savefile */
8386 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8387 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8390 /* match outgoing packets */
8391 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8394 /* to filter on inbound traffic, invert the match */
8397 #else /* defined(linux) */
8398 bpf_error(cstate
, "inbound/outbound not supported on %s",
8399 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8401 #endif /* defined(linux) */
8406 #ifdef HAVE_NET_PFVAR_H
8407 /* PF firewall log matched interface */
8409 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8415 * Catch errors reported by us and routines below us, and return NULL
8418 if (setjmp(cstate
->top_ctx
))
8421 if (cstate
->linktype
!= DLT_PFLOG
) {
8422 bpf_error(cstate
, "ifname supported only on PF linktype");
8425 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8426 off
= offsetof(struct pfloghdr
, ifname
);
8427 if (strlen(ifname
) >= len
) {
8428 bpf_error(cstate
, "ifname interface names can only be %d characters",
8432 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8433 (const u_char
*)ifname
);
8437 /* PF firewall log ruleset name */
8439 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8444 * Catch errors reported by us and routines below us, and return NULL
8447 if (setjmp(cstate
->top_ctx
))
8450 if (cstate
->linktype
!= DLT_PFLOG
) {
8451 bpf_error(cstate
, "ruleset supported only on PF linktype");
8455 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8456 bpf_error(cstate
, "ruleset names can only be %ld characters",
8457 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8461 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8462 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8466 /* PF firewall log rule number */
8468 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8473 * Catch errors reported by us and routines below us, and return NULL
8476 if (setjmp(cstate
->top_ctx
))
8479 if (cstate
->linktype
!= DLT_PFLOG
) {
8480 bpf_error(cstate
, "rnr supported only on PF linktype");
8484 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8489 /* PF firewall log sub-rule number */
8491 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8496 * Catch errors reported by us and routines below us, and return NULL
8499 if (setjmp(cstate
->top_ctx
))
8502 if (cstate
->linktype
!= DLT_PFLOG
) {
8503 bpf_error(cstate
, "srnr supported only on PF linktype");
8507 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8512 /* PF firewall log reason code */
8514 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8519 * Catch errors reported by us and routines below us, and return NULL
8522 if (setjmp(cstate
->top_ctx
))
8525 if (cstate
->linktype
!= DLT_PFLOG
) {
8526 bpf_error(cstate
, "reason supported only on PF linktype");
8530 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8531 (bpf_u_int32
)reason
);
8535 /* PF firewall log action */
8537 gen_pf_action(compiler_state_t
*cstate
, int action
)
8542 * Catch errors reported by us and routines below us, and return NULL
8545 if (setjmp(cstate
->top_ctx
))
8548 if (cstate
->linktype
!= DLT_PFLOG
) {
8549 bpf_error(cstate
, "action supported only on PF linktype");
8553 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8554 (bpf_u_int32
)action
);
8557 #else /* !HAVE_NET_PFVAR_H */
8559 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8562 * Catch errors reported by us and routines below us, and return NULL
8565 if (setjmp(cstate
->top_ctx
))
8568 bpf_error(cstate
, "libpcap was compiled without pf support");
8573 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8576 * Catch errors reported by us and routines below us, and return NULL
8579 if (setjmp(cstate
->top_ctx
))
8582 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8587 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8590 * Catch errors reported by us and routines below us, and return NULL
8593 if (setjmp(cstate
->top_ctx
))
8596 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8601 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8604 * Catch errors reported by us and routines below us, and return NULL
8607 if (setjmp(cstate
->top_ctx
))
8610 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8615 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8618 * Catch errors reported by us and routines below us, and return NULL
8621 if (setjmp(cstate
->top_ctx
))
8624 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8629 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8632 * Catch errors reported by us and routines below us, and return NULL
8635 if (setjmp(cstate
->top_ctx
))
8638 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8641 #endif /* HAVE_NET_PFVAR_H */
8643 /* IEEE 802.11 wireless header */
8645 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8650 * Catch errors reported by us and routines below us, and return NULL
8653 if (setjmp(cstate
->top_ctx
))
8656 switch (cstate
->linktype
) {
8658 case DLT_IEEE802_11
:
8659 case DLT_PRISM_HEADER
:
8660 case DLT_IEEE802_11_RADIO_AVS
:
8661 case DLT_IEEE802_11_RADIO
:
8662 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8666 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8674 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8679 * Catch errors reported by us and routines below us, and return NULL
8682 if (setjmp(cstate
->top_ctx
))
8685 switch (cstate
->linktype
) {
8687 case DLT_IEEE802_11
:
8688 case DLT_PRISM_HEADER
:
8689 case DLT_IEEE802_11_RADIO_AVS
:
8690 case DLT_IEEE802_11_RADIO
:
8694 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8698 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8699 IEEE80211_FC1_DIR_MASK
);
8705 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8710 * Catch errors reported by us and routines below us, and return NULL
8713 if (setjmp(cstate
->top_ctx
))
8716 switch (cstate
->linktype
) {
8719 case DLT_ARCNET_LINUX
:
8720 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8721 q
.proto
== Q_LINK
) {
8722 cstate
->e
= pcap_ether_aton(s
);
8723 if (cstate
->e
== NULL
)
8724 bpf_error(cstate
, "malloc");
8725 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8730 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8734 bpf_error(cstate
, "aid supported only on ARCnet");
8739 static struct block
*
8740 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8742 register struct block
*b0
, *b1
;
8745 /* src comes first, different from Ethernet */
8747 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8750 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8753 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8754 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8760 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8761 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8766 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8770 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8774 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8778 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8782 bpf_error(cstate
, "'ra' is only supported on 802.11");
8786 bpf_error(cstate
, "'ta' is only supported on 802.11");
8793 static struct block
*
8794 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8796 struct block
*b0
, *b1
;
8798 /* check for VLAN, including QinQ */
8799 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8800 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8803 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8809 static struct block
*
8810 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8812 if (vlan_num
> 0x0fff) {
8813 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8816 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8819 static struct block
*
8820 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8823 struct block
*b0
, *b1
;
8825 b0
= gen_vlan_tpid_test(cstate
);
8828 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8834 * Both payload and link header type follow the VLAN tags so that
8835 * both need to be updated.
8837 cstate
->off_linkpl
.constant_part
+= 4;
8838 cstate
->off_linktype
.constant_part
+= 4;
8843 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8844 /* add v to variable part of off */
8846 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8847 bpf_u_int32 v
, struct slist
*s
)
8851 if (!off
->is_variable
)
8852 off
->is_variable
= 1;
8854 off
->reg
= alloc_reg(cstate
);
8856 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8859 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8862 s2
= new_stmt(cstate
, BPF_ST
);
8868 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8869 * and link type offsets first
8872 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8876 /* offset determined at run time, shift variable part */
8878 cstate
->is_vlan_vloffset
= 1;
8879 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8880 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8882 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8883 sappend(s
.next
, b_tpid
->head
->stmts
);
8884 b_tpid
->head
->stmts
= s
.next
;
8888 * patch block b_vid (VLAN id test) to load VID value either from packet
8889 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8892 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8894 struct slist
*s
, *s2
, *sjeq
;
8897 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8898 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8900 /* true -> next instructions, false -> beginning of b_vid */
8901 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8903 sjeq
->s
.jf
= b_vid
->stmts
;
8906 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8907 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8911 /* Jump to the test in b_vid. We need to jump one instruction before
8912 * the end of the b_vid block so that we only skip loading the TCI
8913 * from packet data and not the 'and' instruction extractging VID.
8916 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8918 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8922 /* insert our statements at the beginning of b_vid */
8923 sappend(s
, b_vid
->stmts
);
8928 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8929 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8930 * tag can be either in metadata or in packet data; therefore if the
8931 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8932 * header for VLAN tag. As the decision is done at run time, we need
8933 * update variable part of the offsets
8935 static struct block
*
8936 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8939 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8942 /* generate new filter code based on extracting packet
8944 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8945 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8947 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8952 * This is tricky. We need to insert the statements updating variable
8953 * parts of offsets before the traditional TPID and VID tests so
8954 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8955 * we do not want this update to affect those checks. That's why we
8956 * generate both test blocks first and insert the statements updating
8957 * variable parts of both offsets after that. This wouldn't work if
8958 * there already were variable length link header when entering this
8959 * function but gen_vlan_bpf_extensions() isn't called in that case.
8961 b_tpid
= gen_vlan_tpid_test(cstate
);
8963 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8965 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8970 gen_vlan_patch_vid_test(cstate
, b_vid
);
8980 * support IEEE 802.1Q VLAN trunk over ethernet
8983 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8988 * Catch errors reported by us and routines below us, and return NULL
8991 if (setjmp(cstate
->top_ctx
))
8994 /* can't check for VLAN-encapsulated packets inside MPLS */
8995 if (cstate
->label_stack_depth
> 0)
8996 bpf_error(cstate
, "no VLAN match after MPLS");
8999 * Check for a VLAN packet, and then change the offsets to point
9000 * to the type and data fields within the VLAN packet. Just
9001 * increment the offsets, so that we can support a hierarchy, e.g.
9002 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
9005 * XXX - this is a bit of a kludge. If we were to split the
9006 * compiler into a parser that parses an expression and
9007 * generates an expression tree, and a code generator that
9008 * takes an expression tree (which could come from our
9009 * parser or from some other parser) and generates BPF code,
9010 * we could perhaps make the offsets parameters of routines
9011 * and, in the handler for an "AND" node, pass to subnodes
9012 * other than the VLAN node the adjusted offsets.
9014 * This would mean that "vlan" would, instead of changing the
9015 * behavior of *all* tests after it, change only the behavior
9016 * of tests ANDed with it. That would change the documented
9017 * semantics of "vlan", which might break some expressions.
9018 * However, it would mean that "(vlan and ip) or ip" would check
9019 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9020 * checking only for VLAN-encapsulated IP, so that could still
9021 * be considered worth doing; it wouldn't break expressions
9022 * that are of the form "vlan and ..." or "vlan N and ...",
9023 * which I suspect are the most common expressions involving
9024 * "vlan". "vlan or ..." doesn't necessarily do what the user
9025 * would really want, now, as all the "or ..." tests would
9026 * be done assuming a VLAN, even though the "or" could be viewed
9027 * as meaning "or, if this isn't a VLAN packet...".
9029 switch (cstate
->linktype
) {
9032 case DLT_NETANALYZER
:
9033 case DLT_NETANALYZER_TRANSPARENT
:
9034 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9035 /* Verify that this is the outer part of the packet and
9036 * not encapsulated somehow. */
9037 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9038 cstate
->off_linkhdr
.constant_part
==
9039 cstate
->off_outermostlinkhdr
.constant_part
) {
9041 * Do we need special VLAN handling?
9043 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9044 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9047 b0
= gen_vlan_no_bpf_extensions(cstate
,
9048 vlan_num
, has_vlan_tag
);
9051 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9055 case DLT_IEEE802_11
:
9056 case DLT_PRISM_HEADER
:
9057 case DLT_IEEE802_11_RADIO_AVS
:
9058 case DLT_IEEE802_11_RADIO
:
9059 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9063 bpf_error(cstate
, "no VLAN support for %s",
9064 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9068 cstate
->vlan_stack_depth
++;
9076 * The label_num_arg dance is to avoid annoying whining by compilers that
9077 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9078 * It's not *used* after setjmp returns.
9081 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9084 volatile bpf_u_int32 label_num
= label_num_arg
;
9085 struct block
*b0
, *b1
;
9088 * Catch errors reported by us and routines below us, and return NULL
9091 if (setjmp(cstate
->top_ctx
))
9094 if (cstate
->label_stack_depth
> 0) {
9095 /* just match the bottom-of-stack bit clear */
9096 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9099 * We're not in an MPLS stack yet, so check the link-layer
9100 * type against MPLS.
9102 switch (cstate
->linktype
) {
9104 case DLT_C_HDLC
: /* fall through */
9107 case DLT_NETANALYZER
:
9108 case DLT_NETANALYZER_TRANSPARENT
:
9109 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9113 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9116 /* FIXME add other DLT_s ...
9117 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9118 * leave it for now */
9121 bpf_error(cstate
, "no MPLS support for %s",
9122 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9127 /* If a specific MPLS label is requested, check it */
9128 if (has_label_num
) {
9129 if (label_num
> 0xFFFFF) {
9130 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9131 label_num
, 0xFFFFF);
9133 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9134 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9135 0xfffff000); /* only compare the first 20 bits */
9141 * Change the offsets to point to the type and data fields within
9142 * the MPLS packet. Just increment the offsets, so that we
9143 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9144 * capture packets with an outer label of 100000 and an inner
9147 * Increment the MPLS stack depth as well; this indicates that
9148 * we're checking MPLS-encapsulated headers, to make sure higher
9149 * level code generators don't try to match against IP-related
9150 * protocols such as Q_ARP, Q_RARP etc.
9152 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9154 cstate
->off_nl_nosnap
+= 4;
9155 cstate
->off_nl
+= 4;
9156 cstate
->label_stack_depth
++;
9161 * Support PPPOE discovery and session.
9164 gen_pppoed(compiler_state_t
*cstate
)
9167 * Catch errors reported by us and routines below us, and return NULL
9170 if (setjmp(cstate
->top_ctx
))
9173 /* check for PPPoE discovery */
9174 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9178 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9180 struct block
*b0
, *b1
;
9183 * Catch errors reported by us and routines below us, and return NULL
9186 if (setjmp(cstate
->top_ctx
))
9190 * Test against the PPPoE session link-layer type.
9192 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9194 /* If a specific session is requested, check PPPoE session id */
9196 if (sess_num
> 0x0000ffff) {
9197 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9198 sess_num
, 0x0000ffff);
9200 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9206 * Change the offsets to point to the type and data fields within
9207 * the PPP packet, and note that this is PPPoE rather than
9210 * XXX - this is a bit of a kludge. See the comments in
9213 * The "network-layer" protocol is PPPoE, which has a 6-byte
9214 * PPPoE header, followed by a PPP packet.
9216 * There is no HDLC encapsulation for the PPP packet (it's
9217 * encapsulated in PPPoES instead), so the link-layer type
9218 * starts at the first byte of the PPP packet. For PPPoE,
9219 * that offset is relative to the beginning of the total
9220 * link-layer payload, including any 802.2 LLC header, so
9221 * it's 6 bytes past cstate->off_nl.
9223 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9224 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9225 cstate
->off_linkpl
.reg
);
9227 cstate
->off_linktype
= cstate
->off_linkhdr
;
9228 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9231 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9236 /* Check that this is Geneve and the VNI is correct if
9237 * specified. Parameterized to handle both IPv4 and IPv6. */
9238 static struct block
*
9239 gen_geneve_check(compiler_state_t
*cstate
,
9240 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9241 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9243 struct block
*b0
, *b1
;
9245 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9247 /* Check that we are operating on version 0. Otherwise, we
9248 * can't decode the rest of the fields. The version is 2 bits
9249 * in the first byte of the Geneve header. */
9250 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9255 if (vni
> 0xffffff) {
9256 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9259 vni
<<= 8; /* VNI is in the upper 3 bytes */
9260 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9268 /* The IPv4 and IPv6 Geneve checks need to do two things:
9269 * - Verify that this actually is Geneve with the right VNI.
9270 * - Place the IP header length (plus variable link prefix if
9271 * needed) into register A to be used later to compute
9272 * the inner packet offsets. */
9273 static struct block
*
9274 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9276 struct block
*b0
, *b1
;
9277 struct slist
*s
, *s1
;
9279 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9281 /* Load the IP header length into A. */
9282 s
= gen_loadx_iphdrlen(cstate
);
9284 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9287 /* Forcibly append these statements to the true condition
9288 * of the protocol check by creating a new block that is
9289 * always true and ANDing them. */
9290 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9299 static struct block
*
9300 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9302 struct block
*b0
, *b1
;
9303 struct slist
*s
, *s1
;
9305 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9307 /* Load the IP header length. We need to account for a
9308 * variable length link prefix if there is one. */
9309 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9311 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9315 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9319 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9323 /* Forcibly append these statements to the true condition
9324 * of the protocol check by creating a new block that is
9325 * always true and ANDing them. */
9326 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9329 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9338 /* We need to store three values based on the Geneve header::
9339 * - The offset of the linktype.
9340 * - The offset of the end of the Geneve header.
9341 * - The offset of the end of the encapsulated MAC header. */
9342 static struct slist
*
9343 gen_geneve_offsets(compiler_state_t
*cstate
)
9345 struct slist
*s
, *s1
, *s_proto
;
9347 /* First we need to calculate the offset of the Geneve header
9348 * itself. This is composed of the IP header previously calculated
9349 * (include any variable link prefix) and stored in A plus the
9350 * fixed sized headers (fixed link prefix, MAC length, and UDP
9352 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9353 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9355 /* Stash this in X since we'll need it later. */
9356 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9359 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9361 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9365 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9366 cstate
->off_linktype
.is_variable
= 1;
9367 cstate
->off_linktype
.constant_part
= 0;
9369 s1
= new_stmt(cstate
, BPF_ST
);
9370 s1
->s
.k
= cstate
->off_linktype
.reg
;
9373 /* Load the Geneve option length and mask and shift to get the
9374 * number of bytes. It is stored in the first byte of the Geneve
9376 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9380 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9384 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9388 /* Add in the rest of the Geneve base header. */
9389 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9393 /* Add the Geneve header length to its offset and store. */
9394 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9398 /* Set the encapsulated type as Ethernet. Even though we may
9399 * not actually have Ethernet inside there are two reasons this
9401 * - The linktype field is always in EtherType format regardless
9402 * of whether it is in Geneve or an inner Ethernet frame.
9403 * - The only link layer that we have specific support for is
9404 * Ethernet. We will confirm that the packet actually is
9405 * Ethernet at runtime before executing these checks. */
9406 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9408 s1
= new_stmt(cstate
, BPF_ST
);
9409 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9412 /* Calculate whether we have an Ethernet header or just raw IP/
9413 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9414 * and linktype by 14 bytes so that the network header can be found
9415 * seamlessly. Otherwise, keep what we've calculated already. */
9417 /* We have a bare jmp so we can't use the optimizer. */
9418 cstate
->no_optimize
= 1;
9420 /* Load the EtherType in the Geneve header, 2 bytes in. */
9421 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9425 /* Load X with the end of the Geneve header. */
9426 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9427 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9430 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9431 * end of this check, we should have the total length in X. In
9432 * the non-Ethernet case, it's already there. */
9433 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9434 s_proto
->s
.k
= ETHERTYPE_TEB
;
9435 sappend(s
, s_proto
);
9437 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9441 /* Since this is Ethernet, use the EtherType of the payload
9442 * directly as the linktype. Overwrite what we already have. */
9443 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9447 s1
= new_stmt(cstate
, BPF_ST
);
9448 s1
->s
.k
= cstate
->off_linktype
.reg
;
9451 /* Advance two bytes further to get the end of the Ethernet
9453 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9457 /* Move the result to X. */
9458 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9461 /* Store the final result of our linkpl calculation. */
9462 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9463 cstate
->off_linkpl
.is_variable
= 1;
9464 cstate
->off_linkpl
.constant_part
= 0;
9466 s1
= new_stmt(cstate
, BPF_STX
);
9467 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9476 /* Check to see if this is a Geneve packet. */
9478 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9480 struct block
*b0
, *b1
;
9484 * Catch errors reported by us and routines below us, and return NULL
9487 if (setjmp(cstate
->top_ctx
))
9490 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9491 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9496 /* Later filters should act on the payload of the Geneve frame,
9497 * update all of the header pointers. Attach this code so that
9498 * it gets executed in the event that the Geneve filter matches. */
9499 s
= gen_geneve_offsets(cstate
);
9501 b1
= gen_true(cstate
);
9502 sappend(s
, b1
->stmts
);
9507 cstate
->is_geneve
= 1;
9512 /* Check that the encapsulated frame has a link layer header
9513 * for Ethernet filters. */
9514 static struct block
*
9515 gen_geneve_ll_check(compiler_state_t
*cstate
)
9518 struct slist
*s
, *s1
;
9520 /* The easiest way to see if there is a link layer present
9521 * is to check if the link layer header and payload are not
9524 /* Geneve always generates pure variable offsets so we can
9525 * compare only the registers. */
9526 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9527 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9529 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9530 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9533 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9541 static struct block
*
9542 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9543 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9550 if (!cstate
->is_atm
)
9551 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9552 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9554 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9555 0xffffffffU
, jtype
, reverse
, jvalue
);
9559 if (!cstate
->is_atm
)
9560 bpf_error(cstate
, "'vci' supported only on raw ATM");
9561 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9563 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9564 0xffffffffU
, jtype
, reverse
, jvalue
);
9568 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9569 abort(); /* XXX - this isn't on FreeBSD */
9570 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9571 0x0fU
, jtype
, reverse
, jvalue
);
9575 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9577 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9578 0xffffffffU
, jtype
, reverse
, jvalue
);
9582 if (!cstate
->is_atm
)
9583 bpf_error(cstate
, "'callref' supported only on raw ATM");
9584 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9586 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9587 0xffffffffU
, jtype
, reverse
, jvalue
);
9596 static struct block
*
9597 gen_atmtype_metac(compiler_state_t
*cstate
)
9599 struct block
*b0
, *b1
;
9601 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9602 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9607 static struct block
*
9608 gen_atmtype_sc(compiler_state_t
*cstate
)
9610 struct block
*b0
, *b1
;
9612 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9613 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9618 static struct block
*
9619 gen_atmtype_llc(compiler_state_t
*cstate
)
9623 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9624 cstate
->linktype
= cstate
->prevlinktype
;
9629 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9630 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9633 * Catch errors reported by us and routines below us, and return NULL
9636 if (setjmp(cstate
->top_ctx
))
9639 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9644 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9646 struct block
*b0
, *b1
;
9649 * Catch errors reported by us and routines below us, and return NULL
9652 if (setjmp(cstate
->top_ctx
))
9658 /* Get all packets in Meta signalling Circuit */
9659 if (!cstate
->is_atm
)
9660 bpf_error(cstate
, "'metac' supported only on raw ATM");
9661 b1
= gen_atmtype_metac(cstate
);
9665 /* Get all packets in Broadcast Circuit*/
9666 if (!cstate
->is_atm
)
9667 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9668 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9669 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9674 /* Get all cells in Segment OAM F4 circuit*/
9675 if (!cstate
->is_atm
)
9676 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9677 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9678 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9683 /* Get all cells in End-to-End OAM F4 Circuit*/
9684 if (!cstate
->is_atm
)
9685 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9686 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9687 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9692 /* Get all packets in connection Signalling Circuit */
9693 if (!cstate
->is_atm
)
9694 bpf_error(cstate
, "'sc' supported only on raw ATM");
9695 b1
= gen_atmtype_sc(cstate
);
9699 /* Get all packets in ILMI Circuit */
9700 if (!cstate
->is_atm
)
9701 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9702 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9703 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9708 /* Get all LANE packets */
9709 if (!cstate
->is_atm
)
9710 bpf_error(cstate
, "'lane' supported only on raw ATM");
9711 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9714 * Arrange that all subsequent tests assume LANE
9715 * rather than LLC-encapsulated packets, and set
9716 * the offsets appropriately for LANE-encapsulated
9719 * We assume LANE means Ethernet, not Token Ring.
9721 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9722 cstate
->off_payload
+ 2, /* Ethernet header */
9724 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9725 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9726 cstate
->off_nl
= 0; /* Ethernet II */
9727 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9731 /* Get all LLC-encapsulated packets */
9732 if (!cstate
->is_atm
)
9733 bpf_error(cstate
, "'llc' supported only on raw ATM");
9734 b1
= gen_atmtype_llc(cstate
);
9744 * Filtering for MTP2 messages based on li value
9745 * FISU, length is null
9746 * LSSU, length is 1 or 2
9747 * MSU, length is 3 or more
9748 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9751 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9753 struct block
*b0
, *b1
;
9756 * Catch errors reported by us and routines below us, and return NULL
9759 if (setjmp(cstate
->top_ctx
))
9765 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9766 (cstate
->linktype
!= DLT_ERF
) &&
9767 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9768 bpf_error(cstate
, "'fisu' supported only on MTP2");
9769 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9770 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9771 0x3fU
, BPF_JEQ
, 0, 0U);
9775 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9776 (cstate
->linktype
!= DLT_ERF
) &&
9777 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9778 bpf_error(cstate
, "'lssu' supported only on MTP2");
9779 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9780 0x3fU
, BPF_JGT
, 1, 2U);
9781 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9782 0x3fU
, BPF_JGT
, 0, 0U);
9787 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9788 (cstate
->linktype
!= DLT_ERF
) &&
9789 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9790 bpf_error(cstate
, "'msu' supported only on MTP2");
9791 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9792 0x3fU
, BPF_JGT
, 0, 2U);
9796 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9797 (cstate
->linktype
!= DLT_ERF
) &&
9798 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9799 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9800 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9801 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9802 0xff80U
, BPF_JEQ
, 0, 0U);
9806 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9807 (cstate
->linktype
!= DLT_ERF
) &&
9808 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9809 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9810 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9811 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9812 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9813 0xff80U
, BPF_JGT
, 0, 0U);
9818 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9819 (cstate
->linktype
!= DLT_ERF
) &&
9820 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9821 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9822 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9823 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9833 * The jvalue_arg dance is to avoid annoying whining by compilers that
9834 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9835 * It's not *used* after setjmp returns.
9838 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9839 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
9841 volatile bpf_u_int32 jvalue
= jvalue_arg
;
9843 bpf_u_int32 val1
, val2
, val3
;
9850 * Catch errors reported by us and routines below us, and return NULL
9853 if (setjmp(cstate
->top_ctx
))
9856 newoff_sio
= cstate
->off_sio
;
9857 newoff_opc
= cstate
->off_opc
;
9858 newoff_dpc
= cstate
->off_dpc
;
9859 newoff_sls
= cstate
->off_sls
;
9860 switch (mtp3field
) {
9863 newoff_sio
+= 3; /* offset for MTP2_HSL */
9867 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9868 bpf_error(cstate
, "'sio' supported only on SS7");
9869 /* sio coded on 1 byte so max value 255 */
9871 bpf_error(cstate
, "sio value %u too big; max value = 255",
9873 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
9874 jtype
, reverse
, jvalue
);
9882 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9883 bpf_error(cstate
, "'opc' supported only on SS7");
9884 /* opc coded on 14 bits so max value 16383 */
9886 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9888 /* the following instructions are made to convert jvalue
9889 * to the form used to write opc in an ss7 message*/
9890 val1
= jvalue
& 0x00003c00;
9892 val2
= jvalue
& 0x000003fc;
9894 val3
= jvalue
& 0x00000003;
9896 jvalue
= val1
+ val2
+ val3
;
9897 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
9898 jtype
, reverse
, jvalue
);
9906 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9907 bpf_error(cstate
, "'dpc' supported only on SS7");
9908 /* dpc coded on 14 bits so max value 16383 */
9910 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9912 /* the following instructions are made to convert jvalue
9913 * to the forme used to write dpc in an ss7 message*/
9914 val1
= jvalue
& 0x000000ff;
9916 val2
= jvalue
& 0x00003f00;
9918 jvalue
= val1
+ val2
;
9919 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
9920 jtype
, reverse
, jvalue
);
9928 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9929 bpf_error(cstate
, "'sls' supported only on SS7");
9930 /* sls coded on 4 bits so max value 15 */
9932 bpf_error(cstate
, "sls value %u too big; max value = 15",
9934 /* the following instruction is made to convert jvalue
9935 * to the forme used to write sls in an ss7 message*/
9936 jvalue
= jvalue
<< 4;
9937 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
9938 jtype
, reverse
, jvalue
);
9947 static struct block
*
9948 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9953 * Q.2931 signalling protocol messages for handling virtual circuits
9954 * establishment and teardown
9959 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9963 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9967 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9971 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9975 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9978 case A_RELEASE_DONE
:
9979 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9989 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9991 struct block
*b0
, *b1
;
9994 * Catch errors reported by us and routines below us, and return NULL
9997 if (setjmp(cstate
->top_ctx
))
10003 if (!cstate
->is_atm
)
10004 bpf_error(cstate
, "'oam' supported only on raw ATM");
10006 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10007 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10009 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10014 if (!cstate
->is_atm
)
10015 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10017 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10018 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10020 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10026 * Get Q.2931 signalling messages for switched
10027 * virtual connection
10029 if (!cstate
->is_atm
)
10030 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10031 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10032 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10034 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10036 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10038 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10040 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10042 b0
= gen_atmtype_sc(cstate
);
10046 case A_METACONNECT
:
10047 if (!cstate
->is_atm
)
10048 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10049 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10050 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10052 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10054 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10056 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10058 b0
= gen_atmtype_metac(cstate
);