2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 #include <sys/socket.h>
32 #include <sys/param.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
50 #include "ethertype.h"
54 #include "ieee80211.h"
56 #include "sunatmpos.h"
60 #include "pcap/ipnet.h"
62 #include "diag-control.h"
67 #include <linux/types.h>
68 #include <linux/if_packet.h>
69 #include <linux/filter.h>
73 #define offsetof(s, e) ((size_t)&((s *)0)->e)
78 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
85 uint16_t u6_addr16
[8];
86 uint32_t u6_addr32
[4];
88 #define s6_addr in6_u.u6_addr8
89 #define s6_addr16 in6_u.u6_addr16
90 #define s6_addr32 in6_u.u6_addr32
91 #define s6_addr64 in6_u.u6_addr64
94 typedef unsigned short sa_family_t
;
96 #define __SOCKADDR_COMMON(sa_prefix) \
97 sa_family_t sa_prefix##family
99 /* Ditto, for IPv6. */
102 __SOCKADDR_COMMON (sin6_
);
103 uint16_t sin6_port
; /* Transport layer port # */
104 uint32_t sin6_flowinfo
; /* IPv6 flow information */
105 struct in6_addr sin6_addr
; /* IPv6 address */
108 #ifndef EAI_ADDRFAMILY
110 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
111 int ai_family
; /* PF_xxx */
112 int ai_socktype
; /* SOCK_xxx */
113 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
114 size_t ai_addrlen
; /* length of ai_addr */
115 char *ai_canonname
; /* canonical name for hostname */
116 struct sockaddr
*ai_addr
; /* binary address */
117 struct addrinfo
*ai_next
; /* next structure in linked list */
119 #endif /* EAI_ADDRFAMILY */
120 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
123 #include <netdb.h> /* for "struct addrinfo" */
125 #include <pcap/namedb.h>
127 #include "nametoaddr.h"
129 #define ETHERMTU 1500
131 #ifndef IPPROTO_HOPOPTS
132 #define IPPROTO_HOPOPTS 0
134 #ifndef IPPROTO_ROUTING
135 #define IPPROTO_ROUTING 43
137 #ifndef IPPROTO_FRAGMENT
138 #define IPPROTO_FRAGMENT 44
140 #ifndef IPPROTO_DSTOPTS
141 #define IPPROTO_DSTOPTS 60
144 #define IPPROTO_SCTP 132
147 #define GENEVE_PORT 6081
149 #ifdef HAVE_OS_PROTO_H
150 #include "os-proto.h"
153 #define JMP(c) ((c)|BPF_JMP|BPF_K)
156 * "Push" the current value of the link-layer header type and link-layer
157 * header offset onto a "stack", and set a new value. (It's not a
158 * full-blown stack; we keep only the top two items.)
160 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
162 (cs)->prevlinktype = (cs)->linktype; \
163 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
164 (cs)->linktype = (new_linktype); \
165 (cs)->off_linkhdr.is_variable = (new_is_variable); \
166 (cs)->off_linkhdr.constant_part = (new_constant_part); \
167 (cs)->off_linkhdr.reg = (new_reg); \
168 (cs)->is_geneve = 0; \
172 * Offset "not set" value.
174 #define OFFSET_NOT_SET 0xffffffffU
177 * Absolute offsets, which are offsets from the beginning of the raw
178 * packet data, are, in the general case, the sum of a variable value
179 * and a constant value; the variable value may be absent, in which
180 * case the offset is only the constant value, and the constant value
181 * may be zero, in which case the offset is only the variable value.
183 * bpf_abs_offset is a structure containing all that information:
185 * is_variable is 1 if there's a variable part.
187 * constant_part is the constant part of the value, possibly zero;
189 * if is_variable is 1, reg is the register number for a register
190 * containing the variable value if the register has been assigned,
200 * Value passed to gen_load_a() to indicate what the offset argument
201 * is relative to the beginning of.
204 OR_PACKET
, /* full packet data */
205 OR_LINKHDR
, /* link-layer header */
206 OR_PREVLINKHDR
, /* previous link-layer header */
207 OR_LLC
, /* 802.2 LLC header */
208 OR_PREVMPLSHDR
, /* previous MPLS header */
209 OR_LINKTYPE
, /* link-layer type */
210 OR_LINKPL
, /* link-layer payload */
211 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
212 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
213 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
217 * We divvy out chunks of memory rather than call malloc each time so
218 * we don't have to worry about leaking memory. It's probably
219 * not a big deal if all this memory was wasted but if this ever
220 * goes into a library that would probably not be a good idea.
222 * XXX - this *is* in a library....
225 #define CHUNK0SIZE 1024
232 * A chunk can store any of:
233 * - a string (guaranteed alignment 1 but present for completeness)
237 * For this simple allocator every allocated chunk gets rounded up to the
238 * alignment needed for any chunk.
249 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
251 /* Code generator state */
253 struct _compiler_state
{
264 int outermostlinktype
;
269 /* Hack for handling VLAN and MPLS stacks. */
270 u_int label_stack_depth
;
271 u_int vlan_stack_depth
;
277 * As errors are handled by a longjmp, anything allocated must
278 * be freed in the longjmp handler, so it must be reachable
281 * One thing that's allocated is the result of pcap_nametoaddrinfo();
282 * it must be freed with freeaddrinfo(). This variable points to
283 * any addrinfo structure that would need to be freed.
288 * Another thing that's allocated is the result of pcap_ether_aton();
289 * it must be freed with free(). This variable points to any
290 * address that would need to be freed.
295 * Various code constructs need to know the layout of the packet.
296 * These values give the necessary offsets from the beginning
297 * of the packet data.
301 * Absolute offset of the beginning of the link-layer header.
303 bpf_abs_offset off_linkhdr
;
306 * If we're checking a link-layer header for a packet encapsulated
307 * in another protocol layer, this is the equivalent information
308 * for the previous layers' link-layer header from the beginning
309 * of the raw packet data.
311 bpf_abs_offset off_prevlinkhdr
;
314 * This is the equivalent information for the outermost layers'
317 bpf_abs_offset off_outermostlinkhdr
;
320 * Absolute offset of the beginning of the link-layer payload.
322 bpf_abs_offset off_linkpl
;
325 * "off_linktype" is the offset to information in the link-layer
326 * header giving the packet type. This is an absolute offset
327 * from the beginning of the packet.
329 * For Ethernet, it's the offset of the Ethernet type field; this
330 * means that it must have a value that skips VLAN tags.
332 * For link-layer types that always use 802.2 headers, it's the
333 * offset of the LLC header; this means that it must have a value
334 * that skips VLAN tags.
336 * For PPP, it's the offset of the PPP type field.
338 * For Cisco HDLC, it's the offset of the CHDLC type field.
340 * For BSD loopback, it's the offset of the AF_ value.
342 * For Linux cooked sockets, it's the offset of the type field.
344 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
345 * encapsulation, in which case, IP is assumed.
347 bpf_abs_offset off_linktype
;
350 * TRUE if the link layer includes an ATM pseudo-header.
355 * TRUE if "geneve" appeared in the filter; it causes us to
356 * generate code that checks for a Geneve header and assume
357 * that later filters apply to the encapsulated payload.
362 * TRUE if we need variable length part of VLAN offset
364 int is_vlan_vloffset
;
367 * These are offsets for the ATM pseudo-header.
374 * These are offsets for the MTP2 fields.
380 * These are offsets for the MTP3 fields.
388 * This is the offset of the first byte after the ATM pseudo_header,
389 * or -1 if there is no ATM pseudo-header.
394 * These are offsets to the beginning of the network-layer header.
395 * They are relative to the beginning of the link-layer payload
396 * (i.e., they don't include off_linkhdr.constant_part or
397 * off_linkpl.constant_part).
399 * If the link layer never uses 802.2 LLC:
401 * "off_nl" and "off_nl_nosnap" are the same.
403 * If the link layer always uses 802.2 LLC:
405 * "off_nl" is the offset if there's a SNAP header following
408 * "off_nl_nosnap" is the offset if there's no SNAP header.
410 * If the link layer is Ethernet:
412 * "off_nl" is the offset if the packet is an Ethernet II packet
413 * (we assume no 802.3+802.2+SNAP);
415 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
416 * with an 802.2 header following it.
422 * Here we handle simple allocation of the scratch registers.
423 * If too many registers are alloc'd, the allocator punts.
425 int regused
[BPF_MEMWORDS
];
431 struct chunk chunks
[NCHUNKS
];
436 * For use by routines outside this file.
440 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
445 * If we've already set an error, don't override it.
446 * The lexical analyzer reports some errors by setting
447 * the error and then returning a LEX_ERROR token, which
448 * is not recognized by any grammar rule, and thus forces
449 * the parse to stop. We don't want the error reported
450 * by the lexical analyzer to be overwritten by the syntax
453 if (!cstate
->error_set
) {
455 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
458 cstate
->error_set
= 1;
463 * For use *ONLY* in routines in this file.
465 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
466 PCAP_PRINTFLIKE(2, 3);
469 static void PCAP_NORETURN
470 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
475 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
478 longjmp(cstate
->top_ctx
, 1);
485 static int init_linktype(compiler_state_t
*, pcap_t
*);
487 static void init_regs(compiler_state_t
*);
488 static int alloc_reg(compiler_state_t
*);
489 static void free_reg(compiler_state_t
*, int);
491 static void initchunks(compiler_state_t
*cstate
);
492 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
493 static void *newchunk(compiler_state_t
*cstate
, size_t);
494 static void freechunks(compiler_state_t
*cstate
);
495 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
496 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
497 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
498 static inline void syntax(compiler_state_t
*cstate
);
500 static void backpatch(struct block
*, struct block
*);
501 static void merge(struct block
*, struct block
*);
502 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
504 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
506 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
508 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
510 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
512 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
513 u_int
, bpf_u_int32
, bpf_u_int32
);
514 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
515 u_int
, const u_char
*);
516 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
517 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
518 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
520 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
522 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
523 static struct block
*gen_uncond(compiler_state_t
*, int);
524 static inline struct block
*gen_true(compiler_state_t
*);
525 static inline struct block
*gen_false(compiler_state_t
*);
526 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
527 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
528 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
529 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
530 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
531 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
532 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
533 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
534 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
535 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
537 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
538 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
539 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
540 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
541 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
542 int, bpf_u_int32
, u_int
, u_int
);
544 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
545 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
547 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
548 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
549 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
550 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
551 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
552 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
553 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
554 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
555 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
558 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
559 struct in6_addr
*, int, int, int);
562 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
563 struct addrinfo
*, int, int);
565 static struct block
*gen_ipfrag(compiler_state_t
*);
566 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
567 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
569 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
570 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
572 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
573 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
574 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
576 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
577 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
578 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
579 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
581 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
582 static int lookup_proto(compiler_state_t
*, const char *, int);
583 #if !defined(NO_PROTOCHAIN)
584 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
585 #endif /* !defined(NO_PROTOCHAIN) */
586 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
587 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
588 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
589 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
590 static struct block
*gen_len(compiler_state_t
*, int, int);
591 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
592 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
594 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
595 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
596 bpf_u_int32
, int, int);
597 static struct block
*gen_atmtype_llc(compiler_state_t
*);
598 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
601 initchunks(compiler_state_t
*cstate
)
605 for (i
= 0; i
< NCHUNKS
; i
++) {
606 cstate
->chunks
[i
].n_left
= 0;
607 cstate
->chunks
[i
].m
= NULL
;
609 cstate
->cur_chunk
= 0;
613 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
619 /* Round up to chunk alignment. */
620 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
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 pcapint_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 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
746 "not-yet-activated pcap_t passed to pcap_compile");
759 * If the device on which we're capturing need to be notified
760 * that a new filter is being compiled, do so.
762 * This allows them to save a copy of it, in case, for example,
763 * they're implementing a form of remote packet capture, and
764 * want the remote machine to filter out the packets in which
765 * it's sending the packets it's captured.
767 * XXX - the fact that we happen to be compiling a filter
768 * doesn't necessarily mean we'll be installing it as the
769 * filter for this pcap_t; we might be running it from userland
770 * on captured packets to do packet classification. We really
771 * need a better way of handling this, but this is all that
772 * the WinPcap remote capture code did.
774 if (p
->save_current_filter_op
!= NULL
)
775 (p
->save_current_filter_op
)(p
, buf
);
779 cstate
.no_optimize
= 0;
784 cstate
.ic
.root
= NULL
;
785 cstate
.ic
.cur_mark
= 0;
787 cstate
.error_set
= 0;
790 cstate
.netmask
= mask
;
792 cstate
.snaplen
= pcap_snapshot(p
);
793 if (cstate
.snaplen
== 0) {
794 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
795 "snaplen of 0 rejects all packets");
800 if (pcap_lex_init(&scanner
) != 0) {
801 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
802 errno
, "can't initialize scanner");
806 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
809 * Associate the compiler state with the lexical analyzer
812 pcap_set_extra(&cstate
, scanner
);
814 if (init_linktype(&cstate
, p
) == -1) {
818 if (pcap_parse(scanner
, &cstate
) != 0) {
820 if (cstate
.ai
!= NULL
)
821 freeaddrinfo(cstate
.ai
);
823 if (cstate
.e
!= NULL
)
829 if (cstate
.ic
.root
== NULL
) {
831 * Catch errors reported by gen_retblk().
833 if (setjmp(cstate
.top_ctx
)) {
837 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
840 if (optimize
&& !cstate
.no_optimize
) {
841 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
846 if (cstate
.ic
.root
== NULL
||
847 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
848 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
849 "expression rejects all packets");
854 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
855 cstate
.ic
.root
, &len
, p
->errbuf
);
856 if (program
->bf_insns
== NULL
) {
861 program
->bf_len
= len
;
863 rc
= 0; /* We're all okay */
867 * Clean up everything for the lexical analyzer.
869 if (in_buffer
!= NULL
)
870 pcap__delete_buffer(in_buffer
, scanner
);
872 pcap_lex_destroy(scanner
);
875 * Clean up our own allocated memory.
883 * entry point for using the compiler with no pcap open
884 * pass in all the stuff that is needed explicitly instead.
887 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
888 struct bpf_program
*program
,
889 const char *buf
, int optimize
, bpf_u_int32 mask
)
894 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
897 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
903 * Clean up a "struct bpf_program" by freeing all the memory allocated
907 pcap_freecode(struct bpf_program
*program
)
910 if (program
->bf_insns
!= NULL
) {
911 free((char *)program
->bf_insns
);
912 program
->bf_insns
= NULL
;
917 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
918 * which of the jt and jf fields has been resolved and which is a pointer
919 * back to another unresolved block (or nil). At least one of the fields
920 * in each block is already resolved.
923 backpatch(struct block
*list
, struct block
*target
)
940 * Merge the lists in b0 and b1, using the 'sense' field to indicate
941 * which of jt and jf is the link.
944 merge(struct block
*b0
, struct block
*b1
)
946 register struct block
**p
= &b0
;
948 /* Find end of list. */
950 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
952 /* Concatenate the lists. */
957 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
959 struct block
*ppi_dlt_check
;
962 * Catch errors reported by us and routines below us, and return -1
965 if (setjmp(cstate
->top_ctx
))
969 * Insert before the statements of the first (root) block any
970 * statements needed to load the lengths of any variable-length
971 * headers into registers.
973 * XXX - a fancier strategy would be to insert those before the
974 * statements of all blocks that use those lengths and that
975 * have no predecessors that use them, so that we only compute
976 * the lengths if we need them. There might be even better
977 * approaches than that.
979 * However, those strategies would be more complicated, and
980 * as we don't generate code to compute a length if the
981 * program has no tests that use the length, and as most
982 * tests will probably use those lengths, we would just
983 * postpone computing the lengths so that it's not done
984 * for tests that fail early, and it's not clear that's
987 insert_compute_vloffsets(cstate
, p
->head
);
990 * For DLT_PPI captures, generate a check of the per-packet
991 * DLT value to make sure it's DLT_IEEE802_11.
993 * XXX - TurboCap cards use DLT_PPI for Ethernet.
994 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
995 * with appropriate Ethernet information and use that rather
996 * than using something such as DLT_PPI where you don't know
997 * the link-layer header type until runtime, which, in the
998 * general case, would force us to generate both Ethernet *and*
999 * 802.11 code (*and* anything else for which PPI is used)
1000 * and choose between them early in the BPF program?
1002 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
1003 if (ppi_dlt_check
!= NULL
)
1004 gen_and(ppi_dlt_check
, p
);
1006 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
1007 p
->sense
= !p
->sense
;
1008 backpatch(p
, gen_retblk(cstate
, 0));
1009 cstate
->ic
.root
= p
->head
;
1014 gen_and(struct block
*b0
, struct block
*b1
)
1016 backpatch(b0
, b1
->head
);
1017 b0
->sense
= !b0
->sense
;
1018 b1
->sense
= !b1
->sense
;
1020 b1
->sense
= !b1
->sense
;
1021 b1
->head
= b0
->head
;
1025 gen_or(struct block
*b0
, struct block
*b1
)
1027 b0
->sense
= !b0
->sense
;
1028 backpatch(b0
, b1
->head
);
1029 b0
->sense
= !b0
->sense
;
1031 b1
->head
= b0
->head
;
1035 gen_not(struct block
*b
)
1037 b
->sense
= !b
->sense
;
1040 static struct block
*
1041 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1042 u_int size
, bpf_u_int32 v
)
1044 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1047 static struct block
*
1048 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1049 u_int size
, bpf_u_int32 v
)
1051 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1054 static struct block
*
1055 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1056 u_int size
, bpf_u_int32 v
)
1058 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1061 static struct block
*
1062 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1063 u_int size
, bpf_u_int32 v
)
1065 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1068 static struct block
*
1069 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1070 u_int size
, bpf_u_int32 v
)
1072 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1075 static struct block
*
1076 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1077 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1079 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1082 static struct block
*
1083 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1084 u_int size
, const u_char
*v
)
1086 register struct block
*b
, *tmp
;
1090 register const u_char
*p
= &v
[size
- 4];
1092 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1100 register const u_char
*p
= &v
[size
- 2];
1102 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1110 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1119 * AND the field of size "size" at offset "offset" relative to the header
1120 * specified by "offrel" with "mask", and compare it with the value "v"
1121 * with the test specified by "jtype"; if "reverse" is true, the test
1122 * should test the opposite of "jtype".
1124 static struct block
*
1125 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1126 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1129 struct slist
*s
, *s2
;
1132 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1134 if (mask
!= 0xffffffff) {
1135 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1140 b
= new_block(cstate
, JMP(jtype
));
1143 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1149 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1151 cstate
->pcap_fddipad
= p
->fddipad
;
1154 * We start out with only one link-layer header.
1156 cstate
->outermostlinktype
= pcap_datalink(p
);
1157 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1158 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1159 cstate
->off_outermostlinkhdr
.reg
= -1;
1161 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1162 cstate
->off_prevlinkhdr
.constant_part
= 0;
1163 cstate
->off_prevlinkhdr
.is_variable
= 0;
1164 cstate
->off_prevlinkhdr
.reg
= -1;
1166 cstate
->linktype
= cstate
->outermostlinktype
;
1167 cstate
->off_linkhdr
.constant_part
= 0;
1168 cstate
->off_linkhdr
.is_variable
= 0;
1169 cstate
->off_linkhdr
.reg
= -1;
1174 cstate
->off_linkpl
.constant_part
= 0;
1175 cstate
->off_linkpl
.is_variable
= 0;
1176 cstate
->off_linkpl
.reg
= -1;
1178 cstate
->off_linktype
.constant_part
= 0;
1179 cstate
->off_linktype
.is_variable
= 0;
1180 cstate
->off_linktype
.reg
= -1;
1183 * Assume it's not raw ATM with a pseudo-header, for now.
1186 cstate
->off_vpi
= OFFSET_NOT_SET
;
1187 cstate
->off_vci
= OFFSET_NOT_SET
;
1188 cstate
->off_proto
= OFFSET_NOT_SET
;
1189 cstate
->off_payload
= OFFSET_NOT_SET
;
1194 cstate
->is_geneve
= 0;
1197 * No variable length VLAN offset by default
1199 cstate
->is_vlan_vloffset
= 0;
1202 * And assume we're not doing SS7.
1204 cstate
->off_li
= OFFSET_NOT_SET
;
1205 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1206 cstate
->off_sio
= OFFSET_NOT_SET
;
1207 cstate
->off_opc
= OFFSET_NOT_SET
;
1208 cstate
->off_dpc
= OFFSET_NOT_SET
;
1209 cstate
->off_sls
= OFFSET_NOT_SET
;
1211 cstate
->label_stack_depth
= 0;
1212 cstate
->vlan_stack_depth
= 0;
1214 switch (cstate
->linktype
) {
1217 cstate
->off_linktype
.constant_part
= 2;
1218 cstate
->off_linkpl
.constant_part
= 6;
1219 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1220 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 case DLT_ARCNET_LINUX
:
1224 cstate
->off_linktype
.constant_part
= 4;
1225 cstate
->off_linkpl
.constant_part
= 8;
1226 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1227 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1231 cstate
->off_linktype
.constant_part
= 12;
1232 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1233 cstate
->off_nl
= 0; /* Ethernet II */
1234 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1239 * SLIP doesn't have a link level type. The 16 byte
1240 * header is hacked into our SLIP driver.
1242 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1243 cstate
->off_linkpl
.constant_part
= 16;
1245 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1248 case DLT_SLIP_BSDOS
:
1249 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1250 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1252 cstate
->off_linkpl
.constant_part
= 24;
1254 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1259 cstate
->off_linktype
.constant_part
= 0;
1260 cstate
->off_linkpl
.constant_part
= 4;
1262 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1266 cstate
->off_linktype
.constant_part
= 0;
1267 cstate
->off_linkpl
.constant_part
= 12;
1269 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1274 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1275 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1276 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1277 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1278 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1280 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1285 * This does no include the Ethernet header, and
1286 * only covers session state.
1288 cstate
->off_linktype
.constant_part
= 6;
1289 cstate
->off_linkpl
.constant_part
= 8;
1291 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1295 cstate
->off_linktype
.constant_part
= 5;
1296 cstate
->off_linkpl
.constant_part
= 24;
1298 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1303 * FDDI doesn't really have a link-level type field.
1304 * We set "off_linktype" to the offset of the LLC header.
1306 * To check for Ethernet types, we assume that SSAP = SNAP
1307 * is being used and pick out the encapsulated Ethernet type.
1308 * XXX - should we generate code to check for SNAP?
1310 cstate
->off_linktype
.constant_part
= 13;
1311 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1312 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1313 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1314 cstate
->off_nl
= 8; /* 802.2+SNAP */
1315 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1320 * Token Ring doesn't really have a link-level type field.
1321 * We set "off_linktype" to the offset of the LLC header.
1323 * To check for Ethernet types, we assume that SSAP = SNAP
1324 * is being used and pick out the encapsulated Ethernet type.
1325 * XXX - should we generate code to check for SNAP?
1327 * XXX - the header is actually variable-length.
1328 * Some various Linux patched versions gave 38
1329 * as "off_linktype" and 40 as "off_nl"; however,
1330 * if a token ring packet has *no* routing
1331 * information, i.e. is not source-routed, the correct
1332 * values are 20 and 22, as they are in the vanilla code.
1334 * A packet is source-routed iff the uppermost bit
1335 * of the first byte of the source address, at an
1336 * offset of 8, has the uppermost bit set. If the
1337 * packet is source-routed, the total number of bytes
1338 * of routing information is 2 plus bits 0x1F00 of
1339 * the 16-bit value at an offset of 14 (shifted right
1340 * 8 - figure out which byte that is).
1342 cstate
->off_linktype
.constant_part
= 14;
1343 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1344 cstate
->off_nl
= 8; /* 802.2+SNAP */
1345 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1348 case DLT_PRISM_HEADER
:
1349 case DLT_IEEE802_11_RADIO_AVS
:
1350 case DLT_IEEE802_11_RADIO
:
1351 cstate
->off_linkhdr
.is_variable
= 1;
1352 /* Fall through, 802.11 doesn't have a variable link
1353 * prefix but is otherwise the same. */
1356 case DLT_IEEE802_11
:
1358 * 802.11 doesn't really have a link-level type field.
1359 * We set "off_linktype.constant_part" to the offset of
1362 * To check for Ethernet types, we assume that SSAP = SNAP
1363 * is being used and pick out the encapsulated Ethernet type.
1364 * XXX - should we generate code to check for SNAP?
1366 * We also handle variable-length radio headers here.
1367 * The Prism header is in theory variable-length, but in
1368 * practice it's always 144 bytes long. However, some
1369 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1370 * sometimes or always supply an AVS header, so we
1371 * have to check whether the radio header is a Prism
1372 * header or an AVS header, so, in practice, it's
1375 cstate
->off_linktype
.constant_part
= 24;
1376 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1377 cstate
->off_linkpl
.is_variable
= 1;
1378 cstate
->off_nl
= 8; /* 802.2+SNAP */
1379 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1384 * At the moment we treat PPI the same way that we treat
1385 * normal Radiotap encoded packets. The difference is in
1386 * the function that generates the code at the beginning
1387 * to compute the header length. Since this code generator
1388 * of PPI supports bare 802.11 encapsulation only (i.e.
1389 * the encapsulated DLT should be DLT_IEEE802_11) we
1390 * generate code to check for this too.
1392 cstate
->off_linktype
.constant_part
= 24;
1393 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1394 cstate
->off_linkpl
.is_variable
= 1;
1395 cstate
->off_linkhdr
.is_variable
= 1;
1396 cstate
->off_nl
= 8; /* 802.2+SNAP */
1397 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1400 case DLT_ATM_RFC1483
:
1401 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1403 * assume routed, non-ISO PDUs
1404 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1406 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1407 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1408 * latter would presumably be treated the way PPPoE
1409 * should be, so you can do "pppoe and udp port 2049"
1410 * or "pppoa and tcp port 80" and have it check for
1411 * PPPo{A,E} and a PPP protocol of IP and....
1413 cstate
->off_linktype
.constant_part
= 0;
1414 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1415 cstate
->off_nl
= 8; /* 802.2+SNAP */
1416 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1421 * Full Frontal ATM; you get AALn PDUs with an ATM
1425 cstate
->off_vpi
= SUNATM_VPI_POS
;
1426 cstate
->off_vci
= SUNATM_VCI_POS
;
1427 cstate
->off_proto
= PROTO_POS
;
1428 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1429 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1430 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1431 cstate
->off_nl
= 8; /* 802.2+SNAP */
1432 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1438 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1439 cstate
->off_linkpl
.constant_part
= 0;
1441 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1444 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1445 cstate
->off_linktype
.constant_part
= 14;
1446 cstate
->off_linkpl
.constant_part
= 16;
1448 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1451 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1452 cstate
->off_linktype
.constant_part
= 0;
1453 cstate
->off_linkpl
.constant_part
= 20;
1455 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1460 * LocalTalk does have a 1-byte type field in the LLAP header,
1461 * but really it just indicates whether there is a "short" or
1462 * "long" DDP packet following.
1464 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1465 cstate
->off_linkpl
.constant_part
= 0;
1467 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1470 case DLT_IP_OVER_FC
:
1472 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1473 * link-level type field. We set "off_linktype" to the
1474 * offset of the LLC header.
1476 * To check for Ethernet types, we assume that SSAP = SNAP
1477 * is being used and pick out the encapsulated Ethernet type.
1478 * XXX - should we generate code to check for SNAP? RFC
1479 * 2625 says SNAP should be used.
1481 cstate
->off_linktype
.constant_part
= 16;
1482 cstate
->off_linkpl
.constant_part
= 16;
1483 cstate
->off_nl
= 8; /* 802.2+SNAP */
1484 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1489 * XXX - we should set this to handle SNAP-encapsulated
1490 * frames (NLPID of 0x80).
1492 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1493 cstate
->off_linkpl
.constant_part
= 0;
1495 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1499 * the only BPF-interesting FRF.16 frames are non-control frames;
1500 * Frame Relay has a variable length link-layer
1501 * so lets start with offset 4 for now and increments later on (FIXME);
1504 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1505 cstate
->off_linkpl
.constant_part
= 0;
1507 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1510 case DLT_APPLE_IP_OVER_IEEE1394
:
1511 cstate
->off_linktype
.constant_part
= 16;
1512 cstate
->off_linkpl
.constant_part
= 18;
1514 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1517 case DLT_SYMANTEC_FIREWALL
:
1518 cstate
->off_linktype
.constant_part
= 6;
1519 cstate
->off_linkpl
.constant_part
= 44;
1520 cstate
->off_nl
= 0; /* Ethernet II */
1521 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1525 cstate
->off_linktype
.constant_part
= 0;
1526 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1527 cstate
->off_linkpl
.is_variable
= 1;
1529 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1532 case DLT_JUNIPER_MFR
:
1533 case DLT_JUNIPER_MLFR
:
1534 case DLT_JUNIPER_MLPPP
:
1535 case DLT_JUNIPER_PPP
:
1536 case DLT_JUNIPER_CHDLC
:
1537 case DLT_JUNIPER_FRELAY
:
1538 cstate
->off_linktype
.constant_part
= 4;
1539 cstate
->off_linkpl
.constant_part
= 4;
1541 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1544 case DLT_JUNIPER_ATM1
:
1545 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1546 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1548 cstate
->off_nl_nosnap
= 10;
1551 case DLT_JUNIPER_ATM2
:
1552 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1553 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1555 cstate
->off_nl_nosnap
= 10;
1558 /* frames captured on a Juniper PPPoE service PIC
1559 * contain raw ethernet frames */
1560 case DLT_JUNIPER_PPPOE
:
1561 case DLT_JUNIPER_ETHER
:
1562 cstate
->off_linkpl
.constant_part
= 14;
1563 cstate
->off_linktype
.constant_part
= 16;
1564 cstate
->off_nl
= 18; /* Ethernet II */
1565 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1568 case DLT_JUNIPER_PPPOE_ATM
:
1569 cstate
->off_linktype
.constant_part
= 4;
1570 cstate
->off_linkpl
.constant_part
= 6;
1572 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1575 case DLT_JUNIPER_GGSN
:
1576 cstate
->off_linktype
.constant_part
= 6;
1577 cstate
->off_linkpl
.constant_part
= 12;
1579 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1582 case DLT_JUNIPER_ES
:
1583 cstate
->off_linktype
.constant_part
= 6;
1584 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1585 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1586 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1589 case DLT_JUNIPER_MONITOR
:
1590 cstate
->off_linktype
.constant_part
= 12;
1591 cstate
->off_linkpl
.constant_part
= 12;
1592 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1593 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1596 case DLT_BACNET_MS_TP
:
1597 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1598 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1599 cstate
->off_nl
= OFFSET_NOT_SET
;
1600 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1603 case DLT_JUNIPER_SERVICES
:
1604 cstate
->off_linktype
.constant_part
= 12;
1605 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1606 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1607 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1610 case DLT_JUNIPER_VP
:
1611 cstate
->off_linktype
.constant_part
= 18;
1612 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1613 cstate
->off_nl
= OFFSET_NOT_SET
;
1614 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1617 case DLT_JUNIPER_ST
:
1618 cstate
->off_linktype
.constant_part
= 18;
1619 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1620 cstate
->off_nl
= OFFSET_NOT_SET
;
1621 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1624 case DLT_JUNIPER_ISM
:
1625 cstate
->off_linktype
.constant_part
= 8;
1626 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1627 cstate
->off_nl
= OFFSET_NOT_SET
;
1628 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1631 case DLT_JUNIPER_VS
:
1632 case DLT_JUNIPER_SRX_E2E
:
1633 case DLT_JUNIPER_FIBRECHANNEL
:
1634 case DLT_JUNIPER_ATM_CEMIC
:
1635 cstate
->off_linktype
.constant_part
= 8;
1636 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1637 cstate
->off_nl
= OFFSET_NOT_SET
;
1638 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1643 cstate
->off_li_hsl
= 4;
1644 cstate
->off_sio
= 3;
1645 cstate
->off_opc
= 4;
1646 cstate
->off_dpc
= 4;
1647 cstate
->off_sls
= 7;
1648 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1649 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1650 cstate
->off_nl
= OFFSET_NOT_SET
;
1651 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1654 case DLT_MTP2_WITH_PHDR
:
1656 cstate
->off_li_hsl
= 8;
1657 cstate
->off_sio
= 7;
1658 cstate
->off_opc
= 8;
1659 cstate
->off_dpc
= 8;
1660 cstate
->off_sls
= 11;
1661 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1662 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1663 cstate
->off_nl
= OFFSET_NOT_SET
;
1664 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1668 cstate
->off_li
= 22;
1669 cstate
->off_li_hsl
= 24;
1670 cstate
->off_sio
= 23;
1671 cstate
->off_opc
= 24;
1672 cstate
->off_dpc
= 24;
1673 cstate
->off_sls
= 27;
1674 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1675 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1676 cstate
->off_nl
= OFFSET_NOT_SET
;
1677 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1681 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1682 cstate
->off_linkpl
.constant_part
= 4;
1684 cstate
->off_nl_nosnap
= 0;
1689 * Currently, only raw "link[N:M]" filtering is supported.
1691 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1692 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1693 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1694 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1698 cstate
->off_linktype
.constant_part
= 1;
1699 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1701 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1704 case DLT_NETANALYZER
:
1705 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1706 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1707 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1708 cstate
->off_nl
= 0; /* Ethernet II */
1709 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1712 case DLT_NETANALYZER_TRANSPARENT
:
1713 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1714 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1715 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1716 cstate
->off_nl
= 0; /* Ethernet II */
1717 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1722 * For values in the range in which we've assigned new
1723 * DLT_ values, only raw "link[N:M]" filtering is supported.
1725 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1726 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1727 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1728 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1729 cstate
->off_nl
= OFFSET_NOT_SET
;
1730 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1732 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1733 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1739 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1744 * Load a value relative to the specified absolute offset.
1746 static struct slist
*
1747 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1748 u_int offset
, u_int size
)
1750 struct slist
*s
, *s2
;
1752 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1755 * If "s" is non-null, it has code to arrange that the X register
1756 * contains the variable part of the absolute offset, so we
1757 * generate a load relative to that, with an offset of
1758 * abs_offset->constant_part + offset.
1760 * Otherwise, we can do an absolute load with an offset of
1761 * abs_offset->constant_part + offset.
1765 * "s" points to a list of statements that puts the
1766 * variable part of the absolute offset into the X register.
1767 * Do an indirect load, to use the X register as an offset.
1769 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1770 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1774 * There is no variable part of the absolute offset, so
1775 * just do an absolute load.
1777 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1778 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1784 * Load a value relative to the beginning of the specified header.
1786 static struct slist
*
1787 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1790 struct slist
*s
, *s2
;
1793 * Squelch warnings from compilers that *don't* assume that
1794 * offrel always has a valid enum value and therefore don't
1795 * assume that we'll always go through one of the case arms.
1797 * If we have a default case, compilers that *do* assume that
1798 * will then complain about the default case code being
1801 * Damned if you do, damned if you don't.
1808 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1813 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1816 case OR_PREVLINKHDR
:
1817 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1821 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1824 case OR_PREVMPLSHDR
:
1825 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1829 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1832 case OR_LINKPL_NOSNAP
:
1833 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1837 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1842 * Load the X register with the length of the IPv4 header
1843 * (plus the offset of the link-layer header, if it's
1844 * preceded by a variable-length header such as a radio
1845 * header), in bytes.
1847 s
= gen_loadx_iphdrlen(cstate
);
1850 * Load the item at {offset of the link-layer payload} +
1851 * {offset, relative to the start of the link-layer
1852 * payload, of the IPv4 header} + {length of the IPv4 header} +
1853 * {specified offset}.
1855 * If the offset of the link-layer payload is variable,
1856 * the variable part of that offset is included in the
1857 * value in the X register, and we include the constant
1858 * part in the offset of the load.
1860 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1861 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1866 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1873 * Generate code to load into the X register the sum of the length of
1874 * the IPv4 header and the variable part of the offset of the link-layer
1877 static struct slist
*
1878 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1880 struct slist
*s
, *s2
;
1882 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1885 * The offset of the link-layer payload has a variable
1886 * part. "s" points to a list of statements that put
1887 * the variable part of that offset into the X register.
1889 * The 4*([k]&0xf) addressing mode can't be used, as we
1890 * don't have a constant offset, so we have to load the
1891 * value in question into the A register and add to it
1892 * the value from the X register.
1894 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1895 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1897 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1900 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1905 * The A register now contains the length of the IP header.
1906 * We need to add to it the variable part of the offset of
1907 * the link-layer payload, which is still in the X
1908 * register, and move the result into the X register.
1910 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1911 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1914 * The offset of the link-layer payload is a constant,
1915 * so no code was generated to load the (nonexistent)
1916 * variable part of that offset.
1918 * This means we can use the 4*([k]&0xf) addressing
1919 * mode. Load the length of the IPv4 header, which
1920 * is at an offset of cstate->off_nl from the beginning of
1921 * the link-layer payload, and thus at an offset of
1922 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1923 * of the raw packet data, using that addressing mode.
1925 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1926 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1932 static struct block
*
1933 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1938 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1940 b
= new_block(cstate
, JMP(BPF_JEQ
));
1946 static inline struct block
*
1947 gen_true(compiler_state_t
*cstate
)
1949 return gen_uncond(cstate
, 1);
1952 static inline struct block
*
1953 gen_false(compiler_state_t
*cstate
)
1955 return gen_uncond(cstate
, 0);
1959 * Byte-swap a 32-bit number.
1960 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1961 * big-endian platforms.)
1963 #define SWAPLONG(y) \
1964 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1967 * Generate code to match a particular packet type.
1969 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1970 * value, if <= ETHERMTU. We use that to determine whether to
1971 * match the type/length field or to check the type/length field for
1972 * a value <= ETHERMTU to see whether it's a type field and then do
1973 * the appropriate test.
1975 static struct block
*
1976 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1978 struct block
*b0
, *b1
;
1984 case LLCSAP_NETBEUI
:
1986 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1987 * so we check the DSAP and SSAP.
1989 * LLCSAP_IP checks for IP-over-802.2, rather
1990 * than IP-over-Ethernet or IP-over-SNAP.
1992 * XXX - should we check both the DSAP and the
1993 * SSAP, like this, or should we check just the
1994 * DSAP, as we do for other types <= ETHERMTU
1995 * (i.e., other SAP values)?
1997 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1999 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2007 * Ethernet_II frames, which are Ethernet
2008 * frames with a frame type of ETHERTYPE_IPX;
2010 * Ethernet_802.3 frames, which are 802.3
2011 * frames (i.e., the type/length field is
2012 * a length field, <= ETHERMTU, rather than
2013 * a type field) with the first two bytes
2014 * after the Ethernet/802.3 header being
2017 * Ethernet_802.2 frames, which are 802.3
2018 * frames with an 802.2 LLC header and
2019 * with the IPX LSAP as the DSAP in the LLC
2022 * Ethernet_SNAP frames, which are 802.3
2023 * frames with an LLC header and a SNAP
2024 * header and with an OUI of 0x000000
2025 * (encapsulated Ethernet) and a protocol
2026 * ID of ETHERTYPE_IPX in the SNAP header.
2028 * XXX - should we generate the same code both
2029 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2033 * This generates code to check both for the
2034 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2036 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2037 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2041 * Now we add code to check for SNAP frames with
2042 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2044 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2048 * Now we generate code to check for 802.3
2049 * frames in general.
2051 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2055 * Now add the check for 802.3 frames before the
2056 * check for Ethernet_802.2 and Ethernet_802.3,
2057 * as those checks should only be done on 802.3
2058 * frames, not on Ethernet frames.
2063 * Now add the check for Ethernet_II frames, and
2064 * do that before checking for the other frame
2067 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2071 case ETHERTYPE_ATALK
:
2072 case ETHERTYPE_AARP
:
2074 * EtherTalk (AppleTalk protocols on Ethernet link
2075 * layer) may use 802.2 encapsulation.
2079 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2080 * we check for an Ethernet type field less than
2081 * 1500, which means it's an 802.3 length field.
2083 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2087 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2088 * SNAP packets with an organization code of
2089 * 0x080007 (Apple, for Appletalk) and a protocol
2090 * type of ETHERTYPE_ATALK (Appletalk).
2092 * 802.2-encapsulated ETHERTYPE_AARP packets are
2093 * SNAP packets with an organization code of
2094 * 0x000000 (encapsulated Ethernet) and a protocol
2095 * type of ETHERTYPE_AARP (Appletalk ARP).
2097 if (ll_proto
== ETHERTYPE_ATALK
)
2098 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2099 else /* ll_proto == ETHERTYPE_AARP */
2100 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2104 * Check for Ethernet encapsulation (Ethertalk
2105 * phase 1?); we just check for the Ethernet
2108 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2114 if (ll_proto
<= ETHERMTU
) {
2116 * This is an LLC SAP value, so the frames
2117 * that match would be 802.2 frames.
2118 * Check that the frame is an 802.2 frame
2119 * (i.e., that the length/type field is
2120 * a length field, <= ETHERMTU) and
2121 * then check the DSAP.
2123 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2125 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2130 * This is an Ethernet type, so compare
2131 * the length/type field with it (if
2132 * the frame is an 802.2 frame, the length
2133 * field will be <= ETHERMTU, and, as
2134 * "ll_proto" is > ETHERMTU, this test
2135 * will fail and the frame won't match,
2136 * which is what we want).
2138 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2143 static struct block
*
2144 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2147 * For DLT_NULL, the link-layer header is a 32-bit word
2148 * containing an AF_ value in *host* byte order, and for
2149 * DLT_ENC, the link-layer header begins with a 32-bit
2150 * word containing an AF_ value in host byte order.
2152 * In addition, if we're reading a saved capture file,
2153 * the host byte order in the capture may not be the
2154 * same as the host byte order on this machine.
2156 * For DLT_LOOP, the link-layer header is a 32-bit
2157 * word containing an AF_ value in *network* byte order.
2159 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2161 * The AF_ value is in host byte order, but the BPF
2162 * interpreter will convert it to network byte order.
2164 * If this is a save file, and it's from a machine
2165 * with the opposite byte order to ours, we byte-swap
2168 * Then we run it through "htonl()", and generate
2169 * code to compare against the result.
2171 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2172 ll_proto
= SWAPLONG(ll_proto
);
2173 ll_proto
= htonl(ll_proto
);
2175 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2179 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2180 * or IPv6 then we have an error.
2182 static struct block
*
2183 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2188 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2191 case ETHERTYPE_IPV6
:
2192 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2199 return gen_false(cstate
);
2203 * Generate code to match a particular packet type.
2205 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2206 * value, if <= ETHERMTU. We use that to determine whether to
2207 * match the type field or to check the type field for the special
2208 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2210 static struct block
*
2211 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2213 struct block
*b0
, *b1
;
2219 case LLCSAP_NETBEUI
:
2221 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2222 * so we check the DSAP and SSAP.
2224 * LLCSAP_IP checks for IP-over-802.2, rather
2225 * than IP-over-Ethernet or IP-over-SNAP.
2227 * XXX - should we check both the DSAP and the
2228 * SSAP, like this, or should we check just the
2229 * DSAP, as we do for other types <= ETHERMTU
2230 * (i.e., other SAP values)?
2232 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2233 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2239 * Ethernet_II frames, which are Ethernet
2240 * frames with a frame type of ETHERTYPE_IPX;
2242 * Ethernet_802.3 frames, which have a frame
2243 * type of LINUX_SLL_P_802_3;
2245 * Ethernet_802.2 frames, which are 802.3
2246 * frames with an 802.2 LLC header (i.e, have
2247 * a frame type of LINUX_SLL_P_802_2) and
2248 * with the IPX LSAP as the DSAP in the LLC
2251 * Ethernet_SNAP frames, which are 802.3
2252 * frames with an LLC header and a SNAP
2253 * header and with an OUI of 0x000000
2254 * (encapsulated Ethernet) and a protocol
2255 * ID of ETHERTYPE_IPX in the SNAP header.
2257 * First, do the checks on LINUX_SLL_P_802_2
2258 * frames; generate the check for either
2259 * Ethernet_802.2 or Ethernet_SNAP frames, and
2260 * then put a check for LINUX_SLL_P_802_2 frames
2263 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2264 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2266 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2270 * Now check for 802.3 frames and OR that with
2271 * the previous test.
2273 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2277 * Now add the check for Ethernet_II frames, and
2278 * do that before checking for the other frame
2281 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2285 case ETHERTYPE_ATALK
:
2286 case ETHERTYPE_AARP
:
2288 * EtherTalk (AppleTalk protocols on Ethernet link
2289 * layer) may use 802.2 encapsulation.
2293 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2294 * we check for the 802.2 protocol type in the
2295 * "Ethernet type" field.
2297 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2300 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2301 * SNAP packets with an organization code of
2302 * 0x080007 (Apple, for Appletalk) and a protocol
2303 * type of ETHERTYPE_ATALK (Appletalk).
2305 * 802.2-encapsulated ETHERTYPE_AARP packets are
2306 * SNAP packets with an organization code of
2307 * 0x000000 (encapsulated Ethernet) and a protocol
2308 * type of ETHERTYPE_AARP (Appletalk ARP).
2310 if (ll_proto
== ETHERTYPE_ATALK
)
2311 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2312 else /* ll_proto == ETHERTYPE_AARP */
2313 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2317 * Check for Ethernet encapsulation (Ethertalk
2318 * phase 1?); we just check for the Ethernet
2321 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2327 if (ll_proto
<= ETHERMTU
) {
2329 * This is an LLC SAP value, so the frames
2330 * that match would be 802.2 frames.
2331 * Check for the 802.2 protocol type
2332 * in the "Ethernet type" field, and
2333 * then check the DSAP.
2335 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2336 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2342 * This is an Ethernet type, so compare
2343 * the length/type field with it (if
2344 * the frame is an 802.2 frame, the length
2345 * field will be <= ETHERMTU, and, as
2346 * "ll_proto" is > ETHERMTU, this test
2347 * will fail and the frame won't match,
2348 * which is what we want).
2350 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2356 * Load a value relative to the beginning of the link-layer header after the
2359 static struct slist
*
2360 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2362 struct slist
*s1
, *s2
;
2365 * Generate code to load the length of the pflog header into
2366 * the register assigned to hold that length, if one has been
2367 * assigned. (If one hasn't been assigned, no code we've
2368 * generated uses that prefix, so we don't need to generate any
2371 if (cstate
->off_linkpl
.reg
!= -1) {
2373 * The length is in the first byte of the header.
2375 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2379 * Round it up to a multiple of 4.
2380 * Add 3, and clear the lower 2 bits.
2382 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2385 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2386 s2
->s
.k
= 0xfffffffc;
2390 * Now allocate a register to hold that value and store
2393 s2
= new_stmt(cstate
, BPF_ST
);
2394 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2398 * Now move it into the X register.
2400 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2408 static struct slist
*
2409 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2411 struct slist
*s1
, *s2
;
2412 struct slist
*sjeq_avs_cookie
;
2413 struct slist
*sjcommon
;
2416 * This code is not compatible with the optimizer, as
2417 * we are generating jmp instructions within a normal
2418 * slist of instructions
2420 cstate
->no_optimize
= 1;
2423 * Generate code to load the length of the radio header into
2424 * the register assigned to hold that length, if one has been
2425 * assigned. (If one hasn't been assigned, no code we've
2426 * generated uses that prefix, so we don't need to generate any
2429 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2430 * or always use the AVS header rather than the Prism header.
2431 * We load a 4-byte big-endian value at the beginning of the
2432 * raw packet data, and see whether, when masked with 0xFFFFF000,
2433 * it's equal to 0x80211000. If so, that indicates that it's
2434 * an AVS header (the masked-out bits are the version number).
2435 * Otherwise, it's a Prism header.
2437 * XXX - the Prism header is also, in theory, variable-length,
2438 * but no known software generates headers that aren't 144
2441 if (cstate
->off_linkhdr
.reg
!= -1) {
2445 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2449 * AND it with 0xFFFFF000.
2451 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2452 s2
->s
.k
= 0xFFFFF000;
2456 * Compare with 0x80211000.
2458 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2459 sjeq_avs_cookie
->s
.k
= 0x80211000;
2460 sappend(s1
, sjeq_avs_cookie
);
2465 * The 4 bytes at an offset of 4 from the beginning of
2466 * the AVS header are the length of the AVS header.
2467 * That field is big-endian.
2469 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2472 sjeq_avs_cookie
->s
.jt
= s2
;
2475 * Now jump to the code to allocate a register
2476 * into which to save the header length and
2477 * store the length there. (The "jump always"
2478 * instruction needs to have the k field set;
2479 * it's added to the PC, so, as we're jumping
2480 * over a single instruction, it should be 1.)
2482 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2484 sappend(s1
, sjcommon
);
2487 * Now for the code that handles the Prism header.
2488 * Just load the length of the Prism header (144)
2489 * into the A register. Have the test for an AVS
2490 * header branch here if we don't have an AVS header.
2492 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2495 sjeq_avs_cookie
->s
.jf
= s2
;
2498 * Now allocate a register to hold that value and store
2499 * it. The code for the AVS header will jump here after
2500 * loading the length of the AVS header.
2502 s2
= new_stmt(cstate
, BPF_ST
);
2503 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2505 sjcommon
->s
.jf
= s2
;
2508 * Now move it into the X register.
2510 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2518 static struct slist
*
2519 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2521 struct slist
*s1
, *s2
;
2524 * Generate code to load the length of the AVS header into
2525 * the register assigned to hold that length, if one has been
2526 * assigned. (If one hasn't been assigned, no code we've
2527 * generated uses that prefix, so we don't need to generate any
2530 if (cstate
->off_linkhdr
.reg
!= -1) {
2532 * The 4 bytes at an offset of 4 from the beginning of
2533 * the AVS header are the length of the AVS header.
2534 * That field is big-endian.
2536 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2540 * Now allocate a register to hold that value and store
2543 s2
= new_stmt(cstate
, BPF_ST
);
2544 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2548 * Now move it into the X register.
2550 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2558 static struct slist
*
2559 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2561 struct slist
*s1
, *s2
;
2564 * Generate code to load the length of the radiotap header into
2565 * the register assigned to hold that length, if one has been
2566 * assigned. (If one hasn't been assigned, no code we've
2567 * generated uses that prefix, so we don't need to generate any
2570 if (cstate
->off_linkhdr
.reg
!= -1) {
2572 * The 2 bytes at offsets of 2 and 3 from the beginning
2573 * of the radiotap header are the length of the radiotap
2574 * header; unfortunately, it's little-endian, so we have
2575 * to load it a byte at a time and construct the value.
2579 * Load the high-order byte, at an offset of 3, shift it
2580 * left a byte, and put the result in the X register.
2582 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2584 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2587 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2591 * Load the next byte, at an offset of 2, and OR the
2592 * value from the X register into it.
2594 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2597 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2601 * Now allocate a register to hold that value and store
2604 s2
= new_stmt(cstate
, BPF_ST
);
2605 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2609 * Now move it into the X register.
2611 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2620 * At the moment we treat PPI as normal Radiotap encoded
2621 * packets. The difference is in the function that generates
2622 * the code at the beginning to compute the header length.
2623 * Since this code generator of PPI supports bare 802.11
2624 * encapsulation only (i.e. the encapsulated DLT should be
2625 * DLT_IEEE802_11) we generate code to check for this too;
2626 * that's done in finish_parse().
2628 static struct slist
*
2629 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2631 struct slist
*s1
, *s2
;
2634 * Generate code to load the length of the radiotap header
2635 * into the register assigned to hold that length, if one has
2638 if (cstate
->off_linkhdr
.reg
!= -1) {
2640 * The 2 bytes at offsets of 2 and 3 from the beginning
2641 * of the radiotap header are the length of the radiotap
2642 * header; unfortunately, it's little-endian, so we have
2643 * to load it a byte at a time and construct the value.
2647 * Load the high-order byte, at an offset of 3, shift it
2648 * left a byte, and put the result in the X register.
2650 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2652 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2655 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2659 * Load the next byte, at an offset of 2, and OR the
2660 * value from the X register into it.
2662 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2665 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2669 * Now allocate a register to hold that value and store
2672 s2
= new_stmt(cstate
, BPF_ST
);
2673 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2677 * Now move it into the X register.
2679 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2688 * Load a value relative to the beginning of the link-layer header after the 802.11
2689 * header, i.e. LLC_SNAP.
2690 * The link-layer header doesn't necessarily begin at the beginning
2691 * of the packet data; there might be a variable-length prefix containing
2692 * radio information.
2694 static struct slist
*
2695 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2698 struct slist
*sjset_data_frame_1
;
2699 struct slist
*sjset_data_frame_2
;
2700 struct slist
*sjset_qos
;
2701 struct slist
*sjset_radiotap_flags_present
;
2702 struct slist
*sjset_radiotap_ext_present
;
2703 struct slist
*sjset_radiotap_tsft_present
;
2704 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2705 struct slist
*s_roundup
;
2707 if (cstate
->off_linkpl
.reg
== -1) {
2709 * No register has been assigned to the offset of
2710 * the link-layer payload, which means nobody needs
2711 * it; don't bother computing it - just return
2712 * what we already have.
2718 * This code is not compatible with the optimizer, as
2719 * we are generating jmp instructions within a normal
2720 * slist of instructions
2722 cstate
->no_optimize
= 1;
2725 * If "s" is non-null, it has code to arrange that the X register
2726 * contains the length of the prefix preceding the link-layer
2729 * Otherwise, the length of the prefix preceding the link-layer
2730 * header is "off_outermostlinkhdr.constant_part".
2734 * There is no variable-length header preceding the
2735 * link-layer header.
2737 * Load the length of the fixed-length prefix preceding
2738 * the link-layer header (if any) into the X register,
2739 * and store it in the cstate->off_linkpl.reg register.
2740 * That length is off_outermostlinkhdr.constant_part.
2742 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2743 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2747 * The X register contains the offset of the beginning of the
2748 * link-layer header; add 24, which is the minimum length
2749 * of the MAC header for a data frame, to that, and store it
2750 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2751 * which is at the offset in the X register, with an indexed load.
2753 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2755 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2758 s2
= new_stmt(cstate
, BPF_ST
);
2759 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2762 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2767 * Check the Frame Control field to see if this is a data frame;
2768 * a data frame has the 0x08 bit (b3) in that field set and the
2769 * 0x04 bit (b2) clear.
2771 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2772 sjset_data_frame_1
->s
.k
= 0x08;
2773 sappend(s
, sjset_data_frame_1
);
2776 * If b3 is set, test b2, otherwise go to the first statement of
2777 * the rest of the program.
2779 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2780 sjset_data_frame_2
->s
.k
= 0x04;
2781 sappend(s
, sjset_data_frame_2
);
2782 sjset_data_frame_1
->s
.jf
= snext
;
2785 * If b2 is not set, this is a data frame; test the QoS bit.
2786 * Otherwise, go to the first statement of the rest of the
2789 sjset_data_frame_2
->s
.jt
= snext
;
2790 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2791 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2792 sappend(s
, sjset_qos
);
2795 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2797 * Otherwise, go to the first statement of the rest of the
2800 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2801 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2803 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2806 s2
= new_stmt(cstate
, BPF_ST
);
2807 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2811 * If we have a radiotap header, look at it to see whether
2812 * there's Atheros padding between the MAC-layer header
2815 * Note: all of the fields in the radiotap header are
2816 * little-endian, so we byte-swap all of the values
2817 * we test against, as they will be loaded as big-endian
2820 * XXX - in the general case, we would have to scan through
2821 * *all* the presence bits, if there's more than one word of
2822 * presence bits. That would require a loop, meaning that
2823 * we wouldn't be able to run the filter in the kernel.
2825 * We assume here that the Atheros adapters that insert the
2826 * annoying padding don't have multiple antennae and therefore
2827 * do not generate radiotap headers with multiple presence words.
2829 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2831 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2832 * in the first presence flag word?
2834 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2838 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2839 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2840 sappend(s
, sjset_radiotap_flags_present
);
2843 * If not, skip all of this.
2845 sjset_radiotap_flags_present
->s
.jf
= snext
;
2848 * Otherwise, is the "extension" bit set in that word?
2850 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2851 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2852 sappend(s
, sjset_radiotap_ext_present
);
2853 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2856 * If so, skip all of this.
2858 sjset_radiotap_ext_present
->s
.jt
= snext
;
2861 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2863 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2864 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2865 sappend(s
, sjset_radiotap_tsft_present
);
2866 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2869 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2870 * at an offset of 16 from the beginning of the raw packet
2871 * data (8 bytes for the radiotap header and 8 bytes for
2874 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2877 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2880 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2882 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2883 sjset_tsft_datapad
->s
.k
= 0x20;
2884 sappend(s
, sjset_tsft_datapad
);
2887 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2888 * at an offset of 8 from the beginning of the raw packet
2889 * data (8 bytes for the radiotap header).
2891 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2894 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2897 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2899 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2900 sjset_notsft_datapad
->s
.k
= 0x20;
2901 sappend(s
, sjset_notsft_datapad
);
2904 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2905 * set, round the length of the 802.11 header to
2906 * a multiple of 4. Do that by adding 3 and then
2907 * dividing by and multiplying by 4, which we do by
2910 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2911 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2912 sappend(s
, s_roundup
);
2913 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2916 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2917 s2
->s
.k
= (bpf_u_int32
)~3;
2919 s2
= new_stmt(cstate
, BPF_ST
);
2920 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2923 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2924 sjset_tsft_datapad
->s
.jf
= snext
;
2925 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2926 sjset_notsft_datapad
->s
.jf
= snext
;
2928 sjset_qos
->s
.jf
= snext
;
2934 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2938 /* There is an implicit dependency between the link
2939 * payload and link header since the payload computation
2940 * includes the variable part of the header. Therefore,
2941 * if nobody else has allocated a register for the link
2942 * header and we need it, do it now. */
2943 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2944 cstate
->off_linkhdr
.reg
== -1)
2945 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2948 * For link-layer types that have a variable-length header
2949 * preceding the link-layer header, generate code to load
2950 * the offset of the link-layer header into the register
2951 * assigned to that offset, if any.
2953 * XXX - this, and the next switch statement, won't handle
2954 * encapsulation of 802.11 or 802.11+radio information in
2955 * some other protocol stack. That's significantly more
2958 switch (cstate
->outermostlinktype
) {
2960 case DLT_PRISM_HEADER
:
2961 s
= gen_load_prism_llprefixlen(cstate
);
2964 case DLT_IEEE802_11_RADIO_AVS
:
2965 s
= gen_load_avs_llprefixlen(cstate
);
2968 case DLT_IEEE802_11_RADIO
:
2969 s
= gen_load_radiotap_llprefixlen(cstate
);
2973 s
= gen_load_ppi_llprefixlen(cstate
);
2982 * For link-layer types that have a variable-length link-layer
2983 * header, generate code to load the offset of the link-layer
2984 * payload into the register assigned to that offset, if any.
2986 switch (cstate
->outermostlinktype
) {
2988 case DLT_IEEE802_11
:
2989 case DLT_PRISM_HEADER
:
2990 case DLT_IEEE802_11_RADIO_AVS
:
2991 case DLT_IEEE802_11_RADIO
:
2993 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2997 s
= gen_load_pflog_llprefixlen(cstate
);
3002 * If there is no initialization yet and we need variable
3003 * length offsets for VLAN, initialize them to zero
3005 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3008 if (cstate
->off_linkpl
.reg
== -1)
3009 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3010 if (cstate
->off_linktype
.reg
== -1)
3011 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3013 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3015 s2
= new_stmt(cstate
, BPF_ST
);
3016 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3018 s2
= new_stmt(cstate
, BPF_ST
);
3019 s2
->s
.k
= cstate
->off_linktype
.reg
;
3024 * If we have any offset-loading code, append all the
3025 * existing statements in the block to those statements,
3026 * and make the resulting list the list of statements
3030 sappend(s
, b
->stmts
);
3035 static struct block
*
3036 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3038 struct slist
*s_load_dlt
;
3041 if (cstate
->linktype
== DLT_PPI
)
3043 /* Create the statements that check for the DLT
3045 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3046 s_load_dlt
->s
.k
= 4;
3048 b
= new_block(cstate
, JMP(BPF_JEQ
));
3050 b
->stmts
= s_load_dlt
;
3051 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3062 * Take an absolute offset, and:
3064 * if it has no variable part, return NULL;
3066 * if it has a variable part, generate code to load the register
3067 * containing that variable part into the X register, returning
3068 * a pointer to that code - if no register for that offset has
3069 * been allocated, allocate it first.
3071 * (The code to set that register will be generated later, but will
3072 * be placed earlier in the code sequence.)
3074 static struct slist
*
3075 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3079 if (off
->is_variable
) {
3080 if (off
->reg
== -1) {
3082 * We haven't yet assigned a register for the
3083 * variable part of the offset of the link-layer
3084 * header; allocate one.
3086 off
->reg
= alloc_reg(cstate
);
3090 * Load the register containing the variable part of the
3091 * offset of the link-layer header into the X register.
3093 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3098 * That offset isn't variable, there's no variable part,
3099 * so we don't need to generate any code.
3106 * Map an Ethernet type to the equivalent PPP type.
3109 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3117 case ETHERTYPE_IPV6
:
3118 ll_proto
= PPP_IPV6
;
3122 ll_proto
= PPP_DECNET
;
3125 case ETHERTYPE_ATALK
:
3126 ll_proto
= PPP_APPLE
;
3139 * I'm assuming the "Bridging PDU"s that go
3140 * over PPP are Spanning Tree Protocol
3143 ll_proto
= PPP_BRPDU
;
3154 * Generate any tests that, for encapsulation of a link-layer packet
3155 * inside another protocol stack, need to be done to check for those
3156 * link-layer packets (and that haven't already been done by a check
3157 * for that encapsulation).
3159 static struct block
*
3160 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3164 if (cstate
->is_geneve
)
3165 return gen_geneve_ll_check(cstate
);
3167 switch (cstate
->prevlinktype
) {
3171 * This is LANE-encapsulated Ethernet; check that the LANE
3172 * packet doesn't begin with an LE Control marker, i.e.
3173 * that it's data, not a control message.
3175 * (We've already generated a test for LANE.)
3177 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3183 * No such tests are necessary.
3191 * The three different values we should check for when checking for an
3192 * IPv6 packet with DLT_NULL.
3194 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3195 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3196 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3199 * Generate code to match a particular packet type by matching the
3200 * link-layer type field or fields in the 802.2 LLC header.
3202 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3203 * value, if <= ETHERMTU.
3205 static struct block
*
3206 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3208 struct block
*b0
, *b1
, *b2
;
3209 const char *description
;
3211 /* are we checking MPLS-encapsulated packets? */
3212 if (cstate
->label_stack_depth
> 0)
3213 return gen_mpls_linktype(cstate
, ll_proto
);
3215 switch (cstate
->linktype
) {
3218 case DLT_NETANALYZER
:
3219 case DLT_NETANALYZER_TRANSPARENT
:
3220 /* Geneve has an EtherType regardless of whether there is an
3222 if (!cstate
->is_geneve
)
3223 b0
= gen_prevlinkhdr_check(cstate
);
3227 b1
= gen_ether_linktype(cstate
, ll_proto
);
3238 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3242 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3246 case DLT_IEEE802_11
:
3247 case DLT_PRISM_HEADER
:
3248 case DLT_IEEE802_11_RADIO_AVS
:
3249 case DLT_IEEE802_11_RADIO
:
3252 * Check that we have a data frame.
3254 b0
= gen_check_802_11_data_frame(cstate
);
3257 * Now check for the specified link-layer type.
3259 b1
= gen_llc_linktype(cstate
, ll_proto
);
3266 * XXX - check for LLC frames.
3268 return gen_llc_linktype(cstate
, ll_proto
);
3273 * XXX - check for LLC PDUs, as per IEEE 802.5.
3275 return gen_llc_linktype(cstate
, ll_proto
);
3278 case DLT_ATM_RFC1483
:
3280 case DLT_IP_OVER_FC
:
3281 return gen_llc_linktype(cstate
, ll_proto
);
3286 * Check for an LLC-encapsulated version of this protocol;
3287 * if we were checking for LANE, linktype would no longer
3290 * Check for LLC encapsulation and then check the protocol.
3292 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3293 b1
= gen_llc_linktype(cstate
, ll_proto
);
3299 return gen_linux_sll_linktype(cstate
, ll_proto
);
3303 case DLT_SLIP_BSDOS
:
3306 * These types don't provide any type field; packets
3307 * are always IPv4 or IPv6.
3309 * XXX - for IPv4, check for a version number of 4, and,
3310 * for IPv6, check for a version number of 6?
3315 /* Check for a version number of 4. */
3316 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3318 case ETHERTYPE_IPV6
:
3319 /* Check for a version number of 6. */
3320 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3323 return gen_false(cstate
); /* always false */
3329 * Raw IPv4, so no type field.
3331 if (ll_proto
== ETHERTYPE_IP
)
3332 return gen_true(cstate
); /* always true */
3334 /* Checking for something other than IPv4; always false */
3335 return gen_false(cstate
);
3340 * Raw IPv6, so no type field.
3342 if (ll_proto
== ETHERTYPE_IPV6
)
3343 return gen_true(cstate
); /* always true */
3345 /* Checking for something other than IPv6; always false */
3346 return gen_false(cstate
);
3351 case DLT_PPP_SERIAL
:
3354 * We use Ethernet protocol types inside libpcap;
3355 * map them to the corresponding PPP protocol types.
3357 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3358 ethertype_to_ppptype(ll_proto
));
3363 * We use Ethernet protocol types inside libpcap;
3364 * map them to the corresponding PPP protocol types.
3370 * Also check for Van Jacobson-compressed IP.
3371 * XXX - do this for other forms of PPP?
3373 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3374 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3376 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3381 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3382 ethertype_to_ppptype(ll_proto
));
3392 return (gen_loopback_linktype(cstate
, AF_INET
));
3394 case ETHERTYPE_IPV6
:
3396 * AF_ values may, unfortunately, be platform-
3397 * dependent; AF_INET isn't, because everybody
3398 * used 4.2BSD's value, but AF_INET6 is, because
3399 * 4.2BSD didn't have a value for it (given that
3400 * IPv6 didn't exist back in the early 1980's),
3401 * and they all picked their own values.
3403 * This means that, if we're reading from a
3404 * savefile, we need to check for all the
3407 * If we're doing a live capture, we only need
3408 * to check for this platform's value; however,
3409 * Npcap uses 24, which isn't Windows's AF_INET6
3410 * value. (Given the multiple different values,
3411 * programs that read pcap files shouldn't be
3412 * checking for their platform's AF_INET6 value
3413 * anyway, they should check for all of the
3414 * possible values. and they might as well do
3415 * that even for live captures.)
3417 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3419 * Savefile - check for all three
3420 * possible IPv6 values.
3422 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3423 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3425 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3430 * Live capture, so we only need to
3431 * check for the value used on this
3436 * Npcap doesn't use Windows's AF_INET6,
3437 * as that collides with AF_IPX on
3438 * some BSDs (both have the value 23).
3439 * Instead, it uses 24.
3441 return (gen_loopback_linktype(cstate
, 24));
3444 return (gen_loopback_linktype(cstate
, AF_INET6
));
3445 #else /* AF_INET6 */
3447 * I guess this platform doesn't support
3448 * IPv6, so we just reject all packets.
3450 return gen_false(cstate
);
3451 #endif /* AF_INET6 */
3457 * Not a type on which we support filtering.
3458 * XXX - support those that have AF_ values
3459 * #defined on this platform, at least?
3461 return gen_false(cstate
);
3466 * af field is host byte order in contrast to the rest of
3469 if (ll_proto
== ETHERTYPE_IP
)
3470 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3472 else if (ll_proto
== ETHERTYPE_IPV6
)
3473 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3476 return gen_false(cstate
);
3480 case DLT_ARCNET_LINUX
:
3482 * XXX should we check for first fragment if the protocol
3488 return gen_false(cstate
);
3490 case ETHERTYPE_IPV6
:
3491 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3495 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3497 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3503 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3505 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3510 case ETHERTYPE_REVARP
:
3511 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3514 case ETHERTYPE_ATALK
:
3515 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3522 case ETHERTYPE_ATALK
:
3523 return gen_true(cstate
);
3525 return gen_false(cstate
);
3531 * XXX - assumes a 2-byte Frame Relay header with
3532 * DLCI and flags. What if the address is longer?
3538 * Check for the special NLPID for IP.
3540 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3542 case ETHERTYPE_IPV6
:
3544 * Check for the special NLPID for IPv6.
3546 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3550 * Check for several OSI protocols.
3552 * Frame Relay packets typically have an OSI
3553 * NLPID at the beginning; we check for each
3556 * What we check for is the NLPID and a frame
3557 * control field of UI, i.e. 0x03 followed
3560 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3561 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3562 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3568 return gen_false(cstate
);
3573 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3575 case DLT_JUNIPER_MFR
:
3576 case DLT_JUNIPER_MLFR
:
3577 case DLT_JUNIPER_MLPPP
:
3578 case DLT_JUNIPER_ATM1
:
3579 case DLT_JUNIPER_ATM2
:
3580 case DLT_JUNIPER_PPPOE
:
3581 case DLT_JUNIPER_PPPOE_ATM
:
3582 case DLT_JUNIPER_GGSN
:
3583 case DLT_JUNIPER_ES
:
3584 case DLT_JUNIPER_MONITOR
:
3585 case DLT_JUNIPER_SERVICES
:
3586 case DLT_JUNIPER_ETHER
:
3587 case DLT_JUNIPER_PPP
:
3588 case DLT_JUNIPER_FRELAY
:
3589 case DLT_JUNIPER_CHDLC
:
3590 case DLT_JUNIPER_VP
:
3591 case DLT_JUNIPER_ST
:
3592 case DLT_JUNIPER_ISM
:
3593 case DLT_JUNIPER_VS
:
3594 case DLT_JUNIPER_SRX_E2E
:
3595 case DLT_JUNIPER_FIBRECHANNEL
:
3596 case DLT_JUNIPER_ATM_CEMIC
:
3598 /* just lets verify the magic number for now -
3599 * on ATM we may have up to 6 different encapsulations on the wire
3600 * and need a lot of heuristics to figure out that the payload
3603 * FIXME encapsulation specific BPF_ filters
3605 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3607 case DLT_BACNET_MS_TP
:
3608 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3611 return gen_ipnet_linktype(cstate
, ll_proto
);
3613 case DLT_LINUX_IRDA
:
3614 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3617 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3620 case DLT_MTP2_WITH_PHDR
:
3621 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3624 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3627 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3629 case DLT_LINUX_LAPD
:
3630 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3632 case DLT_USB_FREEBSD
:
3634 case DLT_USB_LINUX_MMAPPED
:
3636 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3638 case DLT_BLUETOOTH_HCI_H4
:
3639 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3640 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3643 case DLT_CAN_SOCKETCAN
:
3644 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3646 case DLT_IEEE802_15_4
:
3647 case DLT_IEEE802_15_4_LINUX
:
3648 case DLT_IEEE802_15_4_NONASK_PHY
:
3649 case DLT_IEEE802_15_4_NOFCS
:
3650 case DLT_IEEE802_15_4_TAP
:
3651 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3653 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3654 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3657 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3660 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3662 case DLT_IPMB_KONTRON
:
3663 case DLT_IPMB_LINUX
:
3664 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3667 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3670 /* Using the fixed-size NFLOG header it is possible to tell only
3671 * the address family of the packet, other meaningful data is
3672 * either missing or behind TLVs.
3674 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3678 * Does this link-layer header type have a field
3679 * indicating the type of the next protocol? If
3680 * so, off_linktype.constant_part will be the offset of that
3681 * field in the packet; if not, it will be OFFSET_NOT_SET.
3683 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3685 * Yes; assume it's an Ethernet type. (If
3686 * it's not, it needs to be handled specially
3689 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3693 * No; report an error.
3695 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3696 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3704 * Check for an LLC SNAP packet with a given organization code and
3705 * protocol type; we check the entire contents of the 802.2 LLC and
3706 * snap headers, checking for DSAP and SSAP of SNAP and a control
3707 * field of 0x03 in the LLC header, and for the specified organization
3708 * code and protocol type in the SNAP header.
3710 static struct block
*
3711 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3713 u_char snapblock
[8];
3715 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3716 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3717 snapblock
[2] = 0x03; /* control = UI */
3718 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3719 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3720 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3721 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3722 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3723 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3727 * Generate code to match frames with an LLC header.
3729 static struct block
*
3730 gen_llc_internal(compiler_state_t
*cstate
)
3732 struct block
*b0
, *b1
;
3734 switch (cstate
->linktype
) {
3738 * We check for an Ethernet type field less than
3739 * 1500, which means it's an 802.3 length field.
3741 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3745 * Now check for the purported DSAP and SSAP not being
3746 * 0xFF, to rule out NetWare-over-802.3.
3748 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3755 * We check for LLC traffic.
3757 b0
= gen_atmtype_llc(cstate
);
3760 case DLT_IEEE802
: /* Token Ring */
3762 * XXX - check for LLC frames.
3764 return gen_true(cstate
);
3768 * XXX - check for LLC frames.
3770 return gen_true(cstate
);
3772 case DLT_ATM_RFC1483
:
3774 * For LLC encapsulation, these are defined to have an
3777 * For VC encapsulation, they don't, but there's no
3778 * way to check for that; the protocol used on the VC
3779 * is negotiated out of band.
3781 return gen_true(cstate
);
3783 case DLT_IEEE802_11
:
3784 case DLT_PRISM_HEADER
:
3785 case DLT_IEEE802_11_RADIO
:
3786 case DLT_IEEE802_11_RADIO_AVS
:
3789 * Check that we have a data frame.
3791 b0
= gen_check_802_11_data_frame(cstate
);
3795 bpf_error(cstate
, "'llc' not supported for %s",
3796 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3802 gen_llc(compiler_state_t
*cstate
)
3805 * Catch errors reported by us and routines below us, and return NULL
3808 if (setjmp(cstate
->top_ctx
))
3811 return gen_llc_internal(cstate
);
3815 gen_llc_i(compiler_state_t
*cstate
)
3817 struct block
*b0
, *b1
;
3821 * Catch errors reported by us and routines below us, and return NULL
3824 if (setjmp(cstate
->top_ctx
))
3828 * Check whether this is an LLC frame.
3830 b0
= gen_llc_internal(cstate
);
3833 * Load the control byte and test the low-order bit; it must
3834 * be clear for I frames.
3836 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3837 b1
= new_block(cstate
, JMP(BPF_JSET
));
3846 gen_llc_s(compiler_state_t
*cstate
)
3848 struct block
*b0
, *b1
;
3851 * Catch errors reported by us and routines below us, and return NULL
3854 if (setjmp(cstate
->top_ctx
))
3858 * Check whether this is an LLC frame.
3860 b0
= gen_llc_internal(cstate
);
3863 * Now compare the low-order 2 bit of the control byte against
3864 * the appropriate value for S frames.
3866 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3872 gen_llc_u(compiler_state_t
*cstate
)
3874 struct block
*b0
, *b1
;
3877 * Catch errors reported by us and routines below us, and return NULL
3880 if (setjmp(cstate
->top_ctx
))
3884 * Check whether this is an LLC frame.
3886 b0
= gen_llc_internal(cstate
);
3889 * Now compare the low-order 2 bit of the control byte against
3890 * the appropriate value for U frames.
3892 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3898 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3900 struct block
*b0
, *b1
;
3903 * Catch errors reported by us and routines below us, and return NULL
3906 if (setjmp(cstate
->top_ctx
))
3910 * Check whether this is an LLC frame.
3912 b0
= gen_llc_internal(cstate
);
3915 * Now check for an S frame with the appropriate type.
3917 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3923 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3925 struct block
*b0
, *b1
;
3928 * Catch errors reported by us and routines below us, and return NULL
3931 if (setjmp(cstate
->top_ctx
))
3935 * Check whether this is an LLC frame.
3937 b0
= gen_llc_internal(cstate
);
3940 * Now check for a U frame with the appropriate type.
3942 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3948 * Generate code to match a particular packet type, for link-layer types
3949 * using 802.2 LLC headers.
3951 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3952 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3954 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3955 * value, if <= ETHERMTU. We use that to determine whether to
3956 * match the DSAP or both DSAP and LSAP or to check the OUI and
3957 * protocol ID in a SNAP header.
3959 static struct block
*
3960 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3963 * XXX - handle token-ring variable-length header.
3969 case LLCSAP_NETBEUI
:
3971 * XXX - should we check both the DSAP and the
3972 * SSAP, like this, or should we check just the
3973 * DSAP, as we do for other SAP values?
3975 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3976 ((ll_proto
<< 8) | ll_proto
));
3980 * XXX - are there ever SNAP frames for IPX on
3981 * non-Ethernet 802.x networks?
3983 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3985 case ETHERTYPE_ATALK
:
3987 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3988 * SNAP packets with an organization code of
3989 * 0x080007 (Apple, for Appletalk) and a protocol
3990 * type of ETHERTYPE_ATALK (Appletalk).
3992 * XXX - check for an organization code of
3993 * encapsulated Ethernet as well?
3995 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3999 * XXX - we don't have to check for IPX 802.3
4000 * here, but should we check for the IPX Ethertype?
4002 if (ll_proto
<= ETHERMTU
) {
4004 * This is an LLC SAP value, so check
4007 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4010 * This is an Ethernet type; we assume that it's
4011 * unlikely that it'll appear in the right place
4012 * at random, and therefore check only the
4013 * location that would hold the Ethernet type
4014 * in a SNAP frame with an organization code of
4015 * 0x000000 (encapsulated Ethernet).
4017 * XXX - if we were to check for the SNAP DSAP and
4018 * LSAP, as per XXX, and were also to check for an
4019 * organization code of 0x000000 (encapsulated
4020 * Ethernet), we'd do
4022 * return gen_snap(cstate, 0x000000, ll_proto);
4024 * here; for now, we don't, as per the above.
4025 * I don't know whether it's worth the extra CPU
4026 * time to do the right check or not.
4028 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4033 static struct block
*
4034 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4035 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4037 struct block
*b0
, *b1
;
4051 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4052 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4058 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4059 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4064 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4068 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4072 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4076 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4080 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4084 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4091 b0
= gen_linktype(cstate
, ll_proto
);
4092 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4098 static struct block
*
4099 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4100 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4103 struct block
*b0
, *b1
;
4106 * Code below needs to access four separate 32-bit parts of the 128-bit
4107 * IPv6 address and mask. In some OSes this is as simple as using the
4108 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4109 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4110 * far as libpcap sees it. Hence copy the data before use to avoid
4111 * potential unaligned memory access and the associated compiler
4112 * warnings (whether genuine or not).
4114 bpf_u_int32 a
[4], m
[4];
4127 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4128 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4134 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4135 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4140 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4144 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4148 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4152 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4156 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4160 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4167 /* this order is important */
4168 memcpy(a
, addr
, sizeof(a
));
4169 memcpy(m
, mask
, sizeof(m
));
4170 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4171 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4173 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4175 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4177 b0
= gen_linktype(cstate
, ll_proto
);
4183 static struct block
*
4184 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4186 register struct block
*b0
, *b1
;
4190 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4193 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4196 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4197 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4203 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4204 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4209 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4213 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4217 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4221 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4225 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4229 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4237 * Like gen_ehostop, but for DLT_FDDI
4239 static struct block
*
4240 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4242 struct block
*b0
, *b1
;
4246 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4249 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4252 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4253 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4259 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4260 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4265 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4269 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4273 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4277 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4281 bpf_error(cstate
, "'ra' is only supported on 802.11");
4285 bpf_error(cstate
, "'ta' is only supported on 802.11");
4293 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4295 static struct block
*
4296 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4298 register struct block
*b0
, *b1
;
4302 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4305 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4308 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4309 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4315 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4316 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4321 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4325 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4329 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4333 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4337 bpf_error(cstate
, "'ra' is only supported on 802.11");
4341 bpf_error(cstate
, "'ta' is only supported on 802.11");
4349 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4350 * various 802.11 + radio headers.
4352 static struct block
*
4353 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4355 register struct block
*b0
, *b1
, *b2
;
4356 register struct slist
*s
;
4358 #ifdef ENABLE_WLAN_FILTERING_PATCH
4361 * We need to disable the optimizer because the optimizer is buggy
4362 * and wipes out some LD instructions generated by the below
4363 * code to validate the Frame Control bits
4365 cstate
->no_optimize
= 1;
4366 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4373 * For control frames, there is no SA.
4375 * For management frames, SA is at an
4376 * offset of 10 from the beginning of
4379 * For data frames, SA is at an offset
4380 * of 10 from the beginning of the packet
4381 * if From DS is clear, at an offset of
4382 * 16 from the beginning of the packet
4383 * if From DS is set and To DS is clear,
4384 * and an offset of 24 from the beginning
4385 * of the packet if From DS is set and To DS
4390 * Generate the tests to be done for data frames
4393 * First, check for To DS set, i.e. check "link[1] & 0x01".
4395 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4396 b1
= new_block(cstate
, JMP(BPF_JSET
));
4397 b1
->s
.k
= 0x01; /* To DS */
4401 * If To DS is set, the SA is at 24.
4403 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4407 * Now, check for To DS not set, i.e. check
4408 * "!(link[1] & 0x01)".
4410 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4411 b2
= new_block(cstate
, JMP(BPF_JSET
));
4412 b2
->s
.k
= 0x01; /* To DS */
4417 * If To DS is not set, the SA is at 16.
4419 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4423 * Now OR together the last two checks. That gives
4424 * the complete set of checks for data frames with
4430 * Now check for From DS being set, and AND that with
4431 * the ORed-together checks.
4433 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4434 b1
= new_block(cstate
, JMP(BPF_JSET
));
4435 b1
->s
.k
= 0x02; /* From DS */
4440 * Now check for data frames with From DS not set.
4442 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4443 b2
= new_block(cstate
, JMP(BPF_JSET
));
4444 b2
->s
.k
= 0x02; /* From DS */
4449 * If From DS isn't set, the SA is at 10.
4451 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4455 * Now OR together the checks for data frames with
4456 * From DS not set and for data frames with From DS
4457 * set; that gives the checks done for data frames.
4462 * Now check for a data frame.
4463 * I.e, check "link[0] & 0x08".
4465 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4466 b1
= new_block(cstate
, JMP(BPF_JSET
));
4471 * AND that with the checks done for data frames.
4476 * If the high-order bit of the type value is 0, this
4477 * is a management frame.
4478 * I.e, check "!(link[0] & 0x08)".
4480 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4481 b2
= new_block(cstate
, JMP(BPF_JSET
));
4487 * For management frames, the SA is at 10.
4489 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4493 * OR that with the checks done for data frames.
4494 * That gives the checks done for management and
4500 * If the low-order bit of the type value is 1,
4501 * this is either a control frame or a frame
4502 * with a reserved type, and thus not a
4505 * I.e., check "!(link[0] & 0x04)".
4507 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4508 b1
= new_block(cstate
, JMP(BPF_JSET
));
4514 * AND that with the checks for data and management
4524 * For control frames, there is no DA.
4526 * For management frames, DA is at an
4527 * offset of 4 from the beginning of
4530 * For data frames, DA is at an offset
4531 * of 4 from the beginning of the packet
4532 * if To DS is clear and at an offset of
4533 * 16 from the beginning of the packet
4538 * Generate the tests to be done for data frames.
4540 * First, check for To DS set, i.e. "link[1] & 0x01".
4542 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4543 b1
= new_block(cstate
, JMP(BPF_JSET
));
4544 b1
->s
.k
= 0x01; /* To DS */
4548 * If To DS is set, the DA is at 16.
4550 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4554 * Now, check for To DS not set, i.e. check
4555 * "!(link[1] & 0x01)".
4557 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4558 b2
= new_block(cstate
, JMP(BPF_JSET
));
4559 b2
->s
.k
= 0x01; /* To DS */
4564 * If To DS is not set, the DA is at 4.
4566 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4570 * Now OR together the last two checks. That gives
4571 * the complete set of checks for data frames.
4576 * Now check for a data frame.
4577 * I.e, check "link[0] & 0x08".
4579 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4580 b1
= new_block(cstate
, JMP(BPF_JSET
));
4585 * AND that with the checks done for data frames.
4590 * If the high-order bit of the type value is 0, this
4591 * is a management frame.
4592 * I.e, check "!(link[0] & 0x08)".
4594 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4595 b2
= new_block(cstate
, JMP(BPF_JSET
));
4601 * For management frames, the DA is at 4.
4603 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4607 * OR that with the checks done for data frames.
4608 * That gives the checks done for management and
4614 * If the low-order bit of the type value is 1,
4615 * this is either a control frame or a frame
4616 * with a reserved type, and thus not a
4619 * I.e., check "!(link[0] & 0x04)".
4621 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4622 b1
= new_block(cstate
, JMP(BPF_JSET
));
4628 * AND that with the checks for data and management
4635 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4636 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4642 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4643 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4648 * XXX - add BSSID keyword?
4651 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4655 * Not present in CTS or ACK control frames.
4657 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4658 IEEE80211_FC0_TYPE_MASK
);
4660 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4661 IEEE80211_FC0_SUBTYPE_MASK
);
4663 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4664 IEEE80211_FC0_SUBTYPE_MASK
);
4668 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4674 * Not present in control frames.
4676 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4677 IEEE80211_FC0_TYPE_MASK
);
4679 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4685 * Present only if the direction mask has both "From DS"
4686 * and "To DS" set. Neither control frames nor management
4687 * frames should have both of those set, so we don't
4688 * check the frame type.
4690 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4691 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4692 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4698 * Not present in management frames; addr1 in other
4703 * If the high-order bit of the type value is 0, this
4704 * is a management frame.
4705 * I.e, check "(link[0] & 0x08)".
4707 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4708 b1
= new_block(cstate
, JMP(BPF_JSET
));
4715 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4718 * AND that with the check of addr1.
4725 * Not present in management frames; addr2, if present,
4730 * Not present in CTS or ACK control frames.
4732 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4733 IEEE80211_FC0_TYPE_MASK
);
4735 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4736 IEEE80211_FC0_SUBTYPE_MASK
);
4738 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4739 IEEE80211_FC0_SUBTYPE_MASK
);
4745 * If the high-order bit of the type value is 0, this
4746 * is a management frame.
4747 * I.e, check "(link[0] & 0x08)".
4749 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4750 b1
= new_block(cstate
, JMP(BPF_JSET
));
4755 * AND that with the check for frames other than
4756 * CTS and ACK frames.
4763 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4772 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4773 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4774 * as the RFC states.)
4776 static struct block
*
4777 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4779 register struct block
*b0
, *b1
;
4783 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4786 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4789 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4790 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4796 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4797 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4802 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4806 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4810 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4814 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4818 bpf_error(cstate
, "'ra' is only supported on 802.11");
4822 bpf_error(cstate
, "'ta' is only supported on 802.11");
4830 * This is quite tricky because there may be pad bytes in front of the
4831 * DECNET header, and then there are two possible data packet formats that
4832 * carry both src and dst addresses, plus 5 packet types in a format that
4833 * carries only the src node, plus 2 types that use a different format and
4834 * also carry just the src node.
4838 * Instead of doing those all right, we just look for data packets with
4839 * 0 or 1 bytes of padding. If you want to look at other packets, that
4840 * will require a lot more hacking.
4842 * To add support for filtering on DECNET "areas" (network numbers)
4843 * one would want to add a "mask" argument to this routine. That would
4844 * make the filter even more inefficient, although one could be clever
4845 * and not generate masking instructions if the mask is 0xFFFF.
4847 static struct block
*
4848 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4850 struct block
*b0
, *b1
, *b2
, *tmp
;
4851 u_int offset_lh
; /* offset if long header is received */
4852 u_int offset_sh
; /* offset if short header is received */
4857 offset_sh
= 1; /* follows flags */
4858 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4862 offset_sh
= 3; /* follows flags, dstnode */
4863 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4867 /* Inefficient because we do our Calvinball dance twice */
4868 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4869 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4875 /* Inefficient because we do our Calvinball dance twice */
4876 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4877 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4882 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4886 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4890 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4894 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4898 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4902 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4909 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4910 /* Check for pad = 1, long header case */
4911 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4912 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4913 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4914 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4916 /* Check for pad = 0, long header case */
4917 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4919 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4920 (bpf_u_int32
)ntohs((u_short
)addr
));
4923 /* Check for pad = 1, short header case */
4924 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4925 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4926 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4927 (bpf_u_int32
)ntohs((u_short
)addr
));
4930 /* Check for pad = 0, short header case */
4931 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4933 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4934 (bpf_u_int32
)ntohs((u_short
)addr
));
4938 /* Combine with test for cstate->linktype */
4944 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4945 * test the bottom-of-stack bit, and then check the version number
4946 * field in the IP header.
4948 static struct block
*
4949 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4951 struct block
*b0
, *b1
;
4956 /* match the bottom-of-stack bit */
4957 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4958 /* match the IPv4 version number */
4959 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4963 case ETHERTYPE_IPV6
:
4964 /* match the bottom-of-stack bit */
4965 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4966 /* match the IPv4 version number */
4967 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4972 /* FIXME add other L3 proto IDs */
4973 bpf_error(cstate
, "unsupported protocol over mpls");
4978 static struct block
*
4979 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4980 int proto
, int dir
, int type
)
4982 struct block
*b0
, *b1
;
4983 const char *typestr
;
4993 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4995 * Only check for non-IPv4 addresses if we're not
4996 * checking MPLS-encapsulated packets.
4998 if (cstate
->label_stack_depth
== 0) {
4999 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5001 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5007 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5010 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
5013 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
5016 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
5019 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5022 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5025 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5028 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5031 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5034 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5037 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5040 return gen_dnhostop(cstate
, addr
, dir
);
5043 bpf_error(cstate
, "LAT host filtering not implemented");
5046 bpf_error(cstate
, "SCA host filtering not implemented");
5049 bpf_error(cstate
, "MOPRC host filtering not implemented");
5052 bpf_error(cstate
, "MOPDL host filtering not implemented");
5055 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5058 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5061 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5064 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5067 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5070 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5073 bpf_error(cstate
, "AARP host filtering not implemented");
5076 bpf_error(cstate
, "ISO host filtering not implemented");
5079 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5082 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5085 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5088 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5091 bpf_error(cstate
, "IPX host filtering not implemented");
5094 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5097 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5100 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5103 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5106 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5109 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5112 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5115 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5118 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5121 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5130 static struct block
*
5131 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5132 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5134 const char *typestr
;
5144 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5147 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5150 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5153 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5156 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5159 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5162 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5165 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5168 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5171 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5174 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5177 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5180 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5183 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5186 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5189 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5192 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5195 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5198 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5201 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5204 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5207 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5210 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5213 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5216 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5219 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5222 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5225 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5228 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5231 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5234 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5237 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5240 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5243 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5246 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5249 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5252 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5255 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5258 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5261 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5271 static struct block
*
5272 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5273 struct addrinfo
*alist
, int proto
, int dir
)
5275 struct block
*b0
, *b1
, *tmp
;
5276 struct addrinfo
*ai
;
5277 struct sockaddr_in
*sin
;
5280 bpf_error(cstate
, "direction applied to 'gateway'");
5287 switch (cstate
->linktype
) {
5289 case DLT_NETANALYZER
:
5290 case DLT_NETANALYZER_TRANSPARENT
:
5291 b1
= gen_prevlinkhdr_check(cstate
);
5292 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5297 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5300 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5302 case DLT_IEEE802_11
:
5303 case DLT_PRISM_HEADER
:
5304 case DLT_IEEE802_11_RADIO_AVS
:
5305 case DLT_IEEE802_11_RADIO
:
5307 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5311 * This is LLC-multiplexed traffic; if it were
5312 * LANE, cstate->linktype would have been set to
5316 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5317 case DLT_IP_OVER_FC
:
5318 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5322 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5325 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5327 * Does it have an address?
5329 if (ai
->ai_addr
!= NULL
) {
5331 * Yes. Is it an IPv4 address?
5333 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5335 * Generate an entry for it.
5337 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5338 tmp
= gen_host(cstate
,
5339 ntohl(sin
->sin_addr
.s_addr
),
5340 0xffffffff, proto
, Q_OR
, Q_HOST
);
5342 * Is it the *first* IPv4 address?
5346 * Yes, so start with it.
5351 * No, so OR it into the
5363 * No IPv4 addresses found.
5371 bpf_error(cstate
, "illegal modifier of 'gateway'");
5376 static struct block
*
5377 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5385 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5389 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5393 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5397 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5400 #ifndef IPPROTO_IGMP
5401 #define IPPROTO_IGMP 2
5405 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5408 #ifndef IPPROTO_IGRP
5409 #define IPPROTO_IGRP 9
5412 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5416 #define IPPROTO_PIM 103
5420 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5423 #ifndef IPPROTO_VRRP
5424 #define IPPROTO_VRRP 112
5428 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5431 #ifndef IPPROTO_CARP
5432 #define IPPROTO_CARP 112
5436 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5440 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5444 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5448 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5452 bpf_error(cstate
, "link layer applied in wrong context");
5455 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5459 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5463 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5467 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5471 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5475 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5479 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5483 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5486 #ifndef IPPROTO_ICMPV6
5487 #define IPPROTO_ICMPV6 58
5490 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5494 #define IPPROTO_AH 51
5497 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5501 #define IPPROTO_ESP 50
5504 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5508 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5512 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5516 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5519 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5520 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5521 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5523 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5525 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5527 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5531 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5532 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5533 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5535 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5537 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5539 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5543 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5544 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5545 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5547 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5552 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5553 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5558 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5559 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5561 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5563 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5568 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5569 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5574 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5575 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5580 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5584 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5588 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5592 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5596 bpf_error(cstate
, "'radio' is not a valid protocol type");
5605 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5608 * Catch errors reported by us and routines below us, and return NULL
5611 if (setjmp(cstate
->top_ctx
))
5614 return gen_proto_abbrev_internal(cstate
, proto
);
5617 static struct block
*
5618 gen_ipfrag(compiler_state_t
*cstate
)
5623 /* not IPv4 frag other than the first frag */
5624 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5625 b
= new_block(cstate
, JMP(BPF_JSET
));
5634 * Generate a comparison to a port value in the transport-layer header
5635 * at the specified offset from the beginning of that header.
5637 * XXX - this handles a variable-length prefix preceding the link-layer
5638 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5639 * variable-length link-layer headers (such as Token Ring or 802.11
5642 static struct block
*
5643 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5645 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5648 static struct block
*
5649 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5651 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5654 static struct block
*
5655 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5657 struct block
*b0
, *b1
, *tmp
;
5659 /* ip proto 'proto' and not a fragment other than the first fragment */
5660 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5661 b0
= gen_ipfrag(cstate
);
5666 b1
= gen_portatom(cstate
, 0, port
);
5670 b1
= gen_portatom(cstate
, 2, port
);
5674 tmp
= gen_portatom(cstate
, 0, port
);
5675 b1
= gen_portatom(cstate
, 2, port
);
5681 tmp
= gen_portatom(cstate
, 0, port
);
5682 b1
= gen_portatom(cstate
, 2, port
);
5687 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5691 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5695 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5699 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5703 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5707 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5719 static struct block
*
5720 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5722 struct block
*b0
, *b1
, *tmp
;
5727 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5728 * not LLC encapsulation with LLCSAP_IP.
5730 * For IEEE 802 networks - which includes 802.5 token ring
5731 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5732 * says that SNAP encapsulation is used, not LLC encapsulation
5735 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5736 * RFC 2225 say that SNAP encapsulation is used, not LLC
5737 * encapsulation with LLCSAP_IP.
5739 * So we always check for ETHERTYPE_IP.
5741 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5747 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5751 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5752 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5754 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5766 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5768 struct block
*b0
, *b1
, *tmp
;
5770 /* ip6 proto 'proto' */
5771 /* XXX - catch the first fragment of a fragmented packet? */
5772 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5776 b1
= gen_portatom6(cstate
, 0, port
);
5780 b1
= gen_portatom6(cstate
, 2, port
);
5784 tmp
= gen_portatom6(cstate
, 0, port
);
5785 b1
= gen_portatom6(cstate
, 2, port
);
5791 tmp
= gen_portatom6(cstate
, 0, port
);
5792 b1
= gen_portatom6(cstate
, 2, port
);
5804 static struct block
*
5805 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5807 struct block
*b0
, *b1
, *tmp
;
5809 /* link proto ip6 */
5810 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5816 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5820 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5821 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5823 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5834 /* gen_portrange code */
5835 static struct block
*
5836 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5839 struct block
*b1
, *b2
;
5843 * Reverse the order of the ports, so v1 is the lower one.
5852 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5853 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5860 static struct block
*
5861 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5862 bpf_u_int32 proto
, int dir
)
5864 struct block
*b0
, *b1
, *tmp
;
5866 /* ip proto 'proto' and not a fragment other than the first fragment */
5867 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5868 b0
= gen_ipfrag(cstate
);
5873 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5877 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5881 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5882 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5888 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5889 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5894 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5898 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5902 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5906 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5910 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5914 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5926 static struct block
*
5927 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5930 struct block
*b0
, *b1
, *tmp
;
5933 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5939 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5944 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5945 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5947 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5958 static struct block
*
5959 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5962 struct block
*b1
, *b2
;
5966 * Reverse the order of the ports, so v1 is the lower one.
5975 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5976 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5983 static struct block
*
5984 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5985 bpf_u_int32 proto
, int dir
)
5987 struct block
*b0
, *b1
, *tmp
;
5989 /* ip6 proto 'proto' */
5990 /* XXX - catch the first fragment of a fragmented packet? */
5991 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5995 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5999 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6003 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6004 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6010 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6011 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6023 static struct block
*
6024 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6027 struct block
*b0
, *b1
, *tmp
;
6029 /* link proto ip6 */
6030 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6036 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6041 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6042 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6044 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6056 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6065 v
= pcap_nametoproto(name
);
6066 if (v
== PROTO_UNDEF
)
6067 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6071 /* XXX should look up h/w protocol type based on cstate->linktype */
6072 v
= pcap_nametoeproto(name
);
6073 if (v
== PROTO_UNDEF
) {
6074 v
= pcap_nametollc(name
);
6075 if (v
== PROTO_UNDEF
)
6076 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6081 if (strcmp(name
, "esis") == 0)
6083 else if (strcmp(name
, "isis") == 0)
6085 else if (strcmp(name
, "clnp") == 0)
6088 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6098 #if !defined(NO_PROTOCHAIN)
6099 static struct block
*
6100 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6102 struct block
*b0
, *b
;
6103 struct slist
*s
[100];
6104 int fix2
, fix3
, fix4
, fix5
;
6105 int ahcheck
, again
, end
;
6107 int reg2
= alloc_reg(cstate
);
6109 memset(s
, 0, sizeof(s
));
6110 fix3
= fix4
= fix5
= 0;
6117 b0
= gen_protochain(cstate
, v
, Q_IP
);
6118 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6122 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6127 * We don't handle variable-length prefixes before the link-layer
6128 * header, or variable-length link-layer headers, here yet.
6129 * We might want to add BPF instructions to do the protochain
6130 * work, to simplify that and, on platforms that have a BPF
6131 * interpreter with the new instructions, let the filtering
6132 * be done in the kernel. (We already require a modified BPF
6133 * engine to do the protochain stuff, to support backward
6134 * branches, and backward branch support is unlikely to appear
6135 * in kernel BPF engines.)
6137 if (cstate
->off_linkpl
.is_variable
)
6138 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6141 * To quote a comment in optimize.c:
6143 * "These data structures are used in a Cocke and Schwartz style
6144 * value numbering scheme. Since the flowgraph is acyclic,
6145 * exit values can be propagated from a node's predecessors
6146 * provided it is uniquely defined."
6148 * "Acyclic" means "no backward branches", which means "no
6149 * loops", so we have to turn the optimizer off.
6151 cstate
->no_optimize
= 1;
6154 * s[0] is a dummy entry to protect other BPF insn from damage
6155 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6156 * hard to find interdependency made by jump table fixup.
6159 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6164 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6167 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6168 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6170 /* X = ip->ip_hl << 2 */
6171 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6172 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6177 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6179 /* A = ip6->ip_nxt */
6180 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6181 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6183 /* X = sizeof(struct ip6_hdr) */
6184 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6190 bpf_error(cstate
, "unsupported proto to gen_protochain");
6194 /* again: if (A == v) goto end; else fall through; */
6196 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6198 s
[i
]->s
.jt
= NULL
; /*later*/
6199 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6203 #ifndef IPPROTO_NONE
6204 #define IPPROTO_NONE 59
6206 /* if (A == IPPROTO_NONE) goto end */
6207 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6208 s
[i
]->s
.jt
= NULL
; /*later*/
6209 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6210 s
[i
]->s
.k
= IPPROTO_NONE
;
6211 s
[fix5
]->s
.jf
= s
[i
];
6215 if (proto
== Q_IPV6
) {
6216 int v6start
, v6end
, v6advance
, j
;
6219 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6220 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6221 s
[i
]->s
.jt
= NULL
; /*later*/
6222 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6223 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6224 s
[fix2
]->s
.jf
= s
[i
];
6226 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6227 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6228 s
[i
]->s
.jt
= NULL
; /*later*/
6229 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6230 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6232 /* if (A == IPPROTO_ROUTING) goto v6advance */
6233 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6234 s
[i
]->s
.jt
= NULL
; /*later*/
6235 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6236 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6238 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6239 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6240 s
[i
]->s
.jt
= NULL
; /*later*/
6241 s
[i
]->s
.jf
= NULL
; /*later*/
6242 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6252 * A = P[X + packet head];
6253 * X = X + (P[X + packet head + 1] + 1) * 8;
6255 /* A = P[X + packet head] */
6256 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6257 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6260 s
[i
] = new_stmt(cstate
, BPF_ST
);
6263 /* A = P[X + packet head + 1]; */
6264 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6265 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6268 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6272 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6276 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6280 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6283 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6287 /* goto again; (must use BPF_JA for backward jump) */
6288 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6289 s
[i
]->s
.k
= again
- i
- 1;
6290 s
[i
- 1]->s
.jf
= s
[i
];
6294 for (j
= v6start
; j
<= v6end
; j
++)
6295 s
[j
]->s
.jt
= s
[v6advance
];
6298 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6300 s
[fix2
]->s
.jf
= s
[i
];
6306 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6307 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6308 s
[i
]->s
.jt
= NULL
; /*later*/
6309 s
[i
]->s
.jf
= NULL
; /*later*/
6310 s
[i
]->s
.k
= IPPROTO_AH
;
6312 s
[fix3
]->s
.jf
= s
[ahcheck
];
6319 * X = X + (P[X + 1] + 2) * 4;
6322 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6324 /* A = P[X + packet head]; */
6325 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6326 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6329 s
[i
] = new_stmt(cstate
, BPF_ST
);
6333 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6336 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6340 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6342 /* A = P[X + packet head] */
6343 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6344 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6347 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6351 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6355 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6358 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6362 /* goto again; (must use BPF_JA for backward jump) */
6363 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6364 s
[i
]->s
.k
= again
- i
- 1;
6369 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6371 s
[fix2
]->s
.jt
= s
[end
];
6372 s
[fix4
]->s
.jf
= s
[end
];
6373 s
[fix5
]->s
.jt
= s
[end
];
6380 for (i
= 0; i
< max
- 1; i
++)
6381 s
[i
]->next
= s
[i
+ 1];
6382 s
[max
- 1]->next
= NULL
;
6387 b
= new_block(cstate
, JMP(BPF_JEQ
));
6388 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6391 free_reg(cstate
, reg2
);
6396 #endif /* !defined(NO_PROTOCHAIN) */
6398 static struct block
*
6399 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6402 struct block
*b0
, *b1
;
6405 * A data frame has the 0x08 bit (b3) in the frame control field set
6406 * and the 0x04 bit (b2) clear.
6408 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6409 b0
= new_block(cstate
, JMP(BPF_JSET
));
6413 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6414 b1
= new_block(cstate
, JMP(BPF_JSET
));
6425 * Generate code that checks whether the packet is a packet for protocol
6426 * <proto> and whether the type field in that protocol's header has
6427 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6428 * IP packet and checks the protocol number in the IP header against <v>.
6430 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6431 * against Q_IP and Q_IPV6.
6433 static struct block
*
6434 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6436 struct block
*b0
, *b1
;
6439 if (dir
!= Q_DEFAULT
)
6440 bpf_error(cstate
, "direction applied to 'proto'");
6444 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6445 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6450 return gen_linktype(cstate
, v
);
6454 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6455 * not LLC encapsulation with LLCSAP_IP.
6457 * For IEEE 802 networks - which includes 802.5 token ring
6458 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6459 * says that SNAP encapsulation is used, not LLC encapsulation
6462 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6463 * RFC 2225 say that SNAP encapsulation is used, not LLC
6464 * encapsulation with LLCSAP_IP.
6466 * So we always check for ETHERTYPE_IP.
6468 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6469 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6474 bpf_error(cstate
, "arp does not encapsulate another protocol");
6478 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6482 bpf_error(cstate
, "'sctp proto' is bogus");
6486 bpf_error(cstate
, "'tcp proto' is bogus");
6490 bpf_error(cstate
, "'udp proto' is bogus");
6494 bpf_error(cstate
, "'icmp proto' is bogus");
6498 bpf_error(cstate
, "'igmp proto' is bogus");
6502 bpf_error(cstate
, "'igrp proto' is bogus");
6506 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6510 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6514 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6518 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6522 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6526 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6530 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6532 * Also check for a fragment header before the final
6535 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6536 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6538 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6544 bpf_error(cstate
, "'icmp6 proto' is bogus");
6548 bpf_error(cstate
, "'ah proto' is bogus");
6552 bpf_error(cstate
, "'esp proto' is bogus");
6556 bpf_error(cstate
, "'pim proto' is bogus");
6560 bpf_error(cstate
, "'vrrp proto' is bogus");
6564 bpf_error(cstate
, "'aarp proto' is bogus");
6568 switch (cstate
->linktype
) {
6572 * Frame Relay packets typically have an OSI
6573 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6574 * generates code to check for all the OSI
6575 * NLPIDs, so calling it and then adding a check
6576 * for the particular NLPID for which we're
6577 * looking is bogus, as we can just check for
6580 * What we check for is the NLPID and a frame
6581 * control field value of UI, i.e. 0x03 followed
6584 * XXX - assumes a 2-byte Frame Relay header with
6585 * DLCI and flags. What if the address is longer?
6587 * XXX - what about SNAP-encapsulated frames?
6589 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6595 * Cisco uses an Ethertype lookalike - for OSI,
6598 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6599 /* OSI in C-HDLC is stuffed with a fudge byte */
6600 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6605 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6606 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6612 bpf_error(cstate
, "'esis proto' is bogus");
6616 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6618 * 4 is the offset of the PDU type relative to the IS-IS
6621 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6626 bpf_error(cstate
, "'clnp proto' is not supported");
6630 bpf_error(cstate
, "'stp proto' is bogus");
6634 bpf_error(cstate
, "'ipx proto' is bogus");
6638 bpf_error(cstate
, "'netbeui proto' is bogus");
6642 bpf_error(cstate
, "'l1 proto' is bogus");
6646 bpf_error(cstate
, "'l2 proto' is bogus");
6650 bpf_error(cstate
, "'iih proto' is bogus");
6654 bpf_error(cstate
, "'snp proto' is bogus");
6658 bpf_error(cstate
, "'csnp proto' is bogus");
6662 bpf_error(cstate
, "'psnp proto' is bogus");
6666 bpf_error(cstate
, "'lsp proto' is bogus");
6670 bpf_error(cstate
, "'radio proto' is bogus");
6674 bpf_error(cstate
, "'carp proto' is bogus");
6685 * Convert a non-numeric name to a port number.
6688 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6690 struct addrinfo hints
, *res
, *ai
;
6692 struct sockaddr_in
*in4
;
6694 struct sockaddr_in6
*in6
;
6699 * We check for both TCP and UDP in case there are
6700 * ambiguous entries.
6702 memset(&hints
, 0, sizeof(hints
));
6703 hints
.ai_family
= PF_UNSPEC
;
6704 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6705 hints
.ai_protocol
= ipproto
;
6706 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6713 * No such port. Just return -1.
6720 * We don't use strerror() because it's not
6721 * guaranteed to be thread-safe on all platforms
6722 * (probably because it might use a non-thread-local
6723 * buffer into which to format an error message
6724 * if the error code isn't one for which it has
6725 * a canned string; three cheers for C string
6728 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6730 port
= -2; /* a real error */
6736 * This is a real error, not just "there's
6737 * no such service name".
6739 * We don't use gai_strerror() because it's not
6740 * guaranteed to be thread-safe on all platforms
6741 * (probably because it might use a non-thread-local
6742 * buffer into which to format an error message
6743 * if the error code isn't one for which it has
6744 * a canned string; three cheers for C string
6747 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6749 port
= -2; /* a real error */
6754 * OK, we found it. Did it find anything?
6756 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6758 * Does it have an address?
6760 if (ai
->ai_addr
!= NULL
) {
6762 * Yes. Get a port number; we're done.
6764 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6765 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6766 port
= ntohs(in4
->sin_port
);
6770 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6771 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6772 port
= ntohs(in6
->sin6_port
);
6784 * Convert a string to a port number.
6787 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6797 * See if it's a number.
6799 ret
= stoulen(string
, string_size
, &val
, cstate
);
6803 /* Unknown port type - it's just a number. */
6804 *proto
= PROTO_UNDEF
;
6807 case STOULEN_NOT_OCTAL_NUMBER
:
6808 case STOULEN_NOT_HEX_NUMBER
:
6809 case STOULEN_NOT_DECIMAL_NUMBER
:
6811 * Not a valid number; try looking it up as a port.
6813 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6814 memcpy(cpy
, string
, string_size
);
6815 cpy
[string_size
] = '\0';
6816 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6817 if (tcp_port
== -2) {
6819 * We got a hard error; the error string has
6823 longjmp(cstate
->top_ctx
, 1);
6826 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6827 if (udp_port
== -2) {
6829 * We got a hard error; the error string has
6833 longjmp(cstate
->top_ctx
, 1);
6838 * We need to check /etc/services for ambiguous entries.
6839 * If we find an ambiguous entry, and it has the
6840 * same port number, change the proto to PROTO_UNDEF
6841 * so both TCP and UDP will be checked.
6843 if (tcp_port
>= 0) {
6844 val
= (bpf_u_int32
)tcp_port
;
6845 *proto
= IPPROTO_TCP
;
6846 if (udp_port
>= 0) {
6847 if (udp_port
== tcp_port
)
6848 *proto
= PROTO_UNDEF
;
6851 /* Can't handle ambiguous names that refer
6852 to different port numbers. */
6853 warning("ambiguous port %s in /etc/services",
6860 if (udp_port
>= 0) {
6861 val
= (bpf_u_int32
)udp_port
;
6862 *proto
= IPPROTO_UDP
;
6866 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6868 longjmp(cstate
->top_ctx
, 1);
6872 /* Error already set. */
6873 longjmp(cstate
->top_ctx
, 1);
6877 /* Should not happen */
6878 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6879 longjmp(cstate
->top_ctx
, 1);
6886 * Convert a string in the form PPP-PPP, which correspond to ports, to
6887 * a starting and ending port in a port range.
6890 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6891 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6894 const char *first
, *second
;
6895 size_t first_size
, second_size
;
6898 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6899 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6902 * Make sure there are no other hyphens.
6904 * XXX - we support named ports, but there are some port names
6905 * in /etc/services that include hyphens, so this would rule
6908 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6909 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6913 * Get the length of the first port.
6916 first_size
= hyphen_off
- string
;
6917 if (first_size
== 0) {
6918 /* Range of "-port", which we don't support. */
6919 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6923 * Try to convert it to a port.
6925 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6926 save_proto
= *proto
;
6929 * Get the length of the second port.
6931 second
= hyphen_off
+ 1;
6932 second_size
= strlen(second
);
6933 if (second_size
== 0) {
6934 /* Range of "port-", which we don't support. */
6935 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6939 * Try to convert it to a port.
6941 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6942 if (*proto
!= save_proto
)
6943 *proto
= PROTO_UNDEF
;
6947 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6949 int proto
= q
.proto
;
6953 bpf_u_int32 mask
, addr
;
6954 struct addrinfo
*res
, *res0
;
6955 struct sockaddr_in
*sin4
;
6958 struct sockaddr_in6
*sin6
;
6959 struct in6_addr mask128
;
6961 struct block
*b
, *tmp
;
6962 int port
, real_proto
;
6963 bpf_u_int32 port1
, port2
;
6966 * Catch errors reported by us and routines below us, and return NULL
6969 if (setjmp(cstate
->top_ctx
))
6975 addr
= pcap_nametonetaddr(name
);
6977 bpf_error(cstate
, "unknown network '%s'", name
);
6978 /* Left justify network addr and calculate its network mask */
6980 while (addr
&& (addr
& 0xff000000) == 0) {
6984 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6988 if (proto
== Q_LINK
) {
6989 switch (cstate
->linktype
) {
6992 case DLT_NETANALYZER
:
6993 case DLT_NETANALYZER_TRANSPARENT
:
6994 eaddr
= pcap_ether_hostton(name
);
6997 "unknown ether host '%s'", name
);
6998 tmp
= gen_prevlinkhdr_check(cstate
);
6999 b
= gen_ehostop(cstate
, eaddr
, dir
);
7006 eaddr
= pcap_ether_hostton(name
);
7009 "unknown FDDI host '%s'", name
);
7010 b
= gen_fhostop(cstate
, eaddr
, dir
);
7015 eaddr
= pcap_ether_hostton(name
);
7018 "unknown token ring host '%s'", name
);
7019 b
= gen_thostop(cstate
, eaddr
, dir
);
7023 case DLT_IEEE802_11
:
7024 case DLT_PRISM_HEADER
:
7025 case DLT_IEEE802_11_RADIO_AVS
:
7026 case DLT_IEEE802_11_RADIO
:
7028 eaddr
= pcap_ether_hostton(name
);
7031 "unknown 802.11 host '%s'", name
);
7032 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7036 case DLT_IP_OVER_FC
:
7037 eaddr
= pcap_ether_hostton(name
);
7040 "unknown Fibre Channel host '%s'", name
);
7041 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7046 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7047 } else if (proto
== Q_DECNET
) {
7049 * A long time ago on Ultrix libpcap supported
7050 * translation of DECnet host names into DECnet
7051 * addresses, but this feature is history now.
7053 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7056 memset(&mask128
, 0xff, sizeof(mask128
));
7058 res0
= res
= pcap_nametoaddrinfo(name
);
7060 bpf_error(cstate
, "unknown host '%s'", name
);
7067 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7068 tproto
== Q_DEFAULT
) {
7074 for (res
= res0
; res
; res
= res
->ai_next
) {
7075 switch (res
->ai_family
) {
7078 if (tproto
== Q_IPV6
)
7082 sin4
= (struct sockaddr_in
*)
7084 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7085 0xffffffff, tproto
, dir
, q
.addr
);
7089 if (tproto6
== Q_IP
)
7092 sin6
= (struct sockaddr_in6
*)
7094 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7095 &mask128
, tproto6
, dir
, q
.addr
);
7108 bpf_error(cstate
, "unknown host '%s'%s", name
,
7109 (proto
== Q_DEFAULT
)
7111 : " for specified address family");
7117 if (proto
!= Q_DEFAULT
&&
7118 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7119 bpf_error(cstate
, "illegal qualifier of 'port'");
7120 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7121 bpf_error(cstate
, "unknown port '%s'", name
);
7122 if (proto
== Q_UDP
) {
7123 if (real_proto
== IPPROTO_TCP
)
7124 bpf_error(cstate
, "port '%s' is tcp", name
);
7125 else if (real_proto
== IPPROTO_SCTP
)
7126 bpf_error(cstate
, "port '%s' is sctp", name
);
7128 /* override PROTO_UNDEF */
7129 real_proto
= IPPROTO_UDP
;
7131 if (proto
== Q_TCP
) {
7132 if (real_proto
== IPPROTO_UDP
)
7133 bpf_error(cstate
, "port '%s' is udp", name
);
7135 else if (real_proto
== IPPROTO_SCTP
)
7136 bpf_error(cstate
, "port '%s' is sctp", name
);
7138 /* override PROTO_UNDEF */
7139 real_proto
= IPPROTO_TCP
;
7141 if (proto
== Q_SCTP
) {
7142 if (real_proto
== IPPROTO_UDP
)
7143 bpf_error(cstate
, "port '%s' is udp", name
);
7145 else if (real_proto
== IPPROTO_TCP
)
7146 bpf_error(cstate
, "port '%s' is tcp", name
);
7148 /* override PROTO_UNDEF */
7149 real_proto
= IPPROTO_SCTP
;
7152 bpf_error(cstate
, "illegal port number %d < 0", port
);
7154 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7155 b
= gen_port(cstate
, port
, real_proto
, dir
);
7156 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7160 if (proto
!= Q_DEFAULT
&&
7161 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7162 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7163 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7164 if (proto
== Q_UDP
) {
7165 if (real_proto
== IPPROTO_TCP
)
7166 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7167 else if (real_proto
== IPPROTO_SCTP
)
7168 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7170 /* override PROTO_UNDEF */
7171 real_proto
= IPPROTO_UDP
;
7173 if (proto
== Q_TCP
) {
7174 if (real_proto
== IPPROTO_UDP
)
7175 bpf_error(cstate
, "port in range '%s' is udp", name
);
7176 else if (real_proto
== IPPROTO_SCTP
)
7177 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7179 /* override PROTO_UNDEF */
7180 real_proto
= IPPROTO_TCP
;
7182 if (proto
== Q_SCTP
) {
7183 if (real_proto
== IPPROTO_UDP
)
7184 bpf_error(cstate
, "port in range '%s' is udp", name
);
7185 else if (real_proto
== IPPROTO_TCP
)
7186 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7188 /* override PROTO_UNDEF */
7189 real_proto
= IPPROTO_SCTP
;
7192 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7194 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7196 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7197 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7202 eaddr
= pcap_ether_hostton(name
);
7204 bpf_error(cstate
, "unknown ether host: %s", name
);
7206 res
= pcap_nametoaddrinfo(name
);
7209 bpf_error(cstate
, "unknown host '%s'", name
);
7210 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7214 bpf_error(cstate
, "unknown host '%s'", name
);
7217 bpf_error(cstate
, "'gateway' not supported in this configuration");
7221 real_proto
= lookup_proto(cstate
, name
, proto
);
7222 if (real_proto
>= 0)
7223 return gen_proto(cstate
, real_proto
, proto
, dir
);
7225 bpf_error(cstate
, "unknown protocol: %s", name
);
7227 #if !defined(NO_PROTOCHAIN)
7229 real_proto
= lookup_proto(cstate
, name
, proto
);
7230 if (real_proto
>= 0)
7231 return gen_protochain(cstate
, real_proto
, proto
);
7233 bpf_error(cstate
, "unknown protocol: %s", name
);
7234 #endif /* !defined(NO_PROTOCHAIN) */
7245 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7246 bpf_u_int32 masklen
, struct qual q
)
7248 register int nlen
, mlen
;
7252 * Catch errors reported by us and routines below us, and return NULL
7255 if (setjmp(cstate
->top_ctx
))
7258 nlen
= __pcap_atoin(s1
, &n
);
7260 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7261 /* Promote short ipaddr */
7265 mlen
= __pcap_atoin(s2
, &m
);
7267 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7268 /* Promote short ipaddr */
7271 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7274 /* Convert mask len to mask */
7276 bpf_error(cstate
, "mask length must be <= 32");
7279 * X << 32 is not guaranteed by C to be 0; it's
7284 m
= 0xffffffff << (32 - masklen
);
7286 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7293 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7296 bpf_error(cstate
, "Mask syntax for networks only");
7303 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7311 * Catch errors reported by us and routines below us, and return NULL
7314 if (setjmp(cstate
->top_ctx
))
7321 * v contains a 32-bit unsigned parsed from a string of the
7322 * form {N}, which could be decimal, hexadecimal or octal.
7323 * Although it would be possible to use the value as a raw
7324 * 16-bit DECnet address when the value fits into 16 bits, this
7325 * would be a questionable feature: DECnet address wire
7326 * encoding is little-endian, so this would not work as
7327 * intuitively as the same works for [big-endian] IPv4
7328 * addresses (0x01020304 means 1.2.3.4).
7330 if (proto
== Q_DECNET
)
7331 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7333 } else if (proto
== Q_DECNET
) {
7335 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7336 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7337 * for a valid DECnet address.
7339 vlen
= __pcap_atodn(s
, &v
);
7341 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7344 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7345 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7348 vlen
= __pcap_atoin(s
, &v
);
7350 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7358 if (proto
== Q_DECNET
)
7359 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7360 else if (proto
== Q_LINK
) {
7361 bpf_error(cstate
, "illegal link layer address");
7364 if (s
== NULL
&& q
.addr
== Q_NET
) {
7365 /* Promote short net number */
7366 while (v
&& (v
& 0xff000000) == 0) {
7371 /* Promote short ipaddr */
7373 mask
<<= 32 - vlen
;
7375 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7380 proto
= IPPROTO_UDP
;
7381 else if (proto
== Q_TCP
)
7382 proto
= IPPROTO_TCP
;
7383 else if (proto
== Q_SCTP
)
7384 proto
= IPPROTO_SCTP
;
7385 else if (proto
== Q_DEFAULT
)
7386 proto
= PROTO_UNDEF
;
7388 bpf_error(cstate
, "illegal qualifier of 'port'");
7391 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7395 b
= gen_port(cstate
, v
, proto
, dir
);
7396 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7402 proto
= IPPROTO_UDP
;
7403 else if (proto
== Q_TCP
)
7404 proto
= IPPROTO_TCP
;
7405 else if (proto
== Q_SCTP
)
7406 proto
= IPPROTO_SCTP
;
7407 else if (proto
== Q_DEFAULT
)
7408 proto
= PROTO_UNDEF
;
7410 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7413 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7417 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7418 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7423 bpf_error(cstate
, "'gateway' requires a name");
7427 return gen_proto(cstate
, v
, proto
, dir
);
7429 #if !defined(NO_PROTOCHAIN)
7431 return gen_protochain(cstate
, v
, proto
);
7447 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7450 struct addrinfo
*res
;
7451 struct in6_addr
*addr
;
7452 struct in6_addr mask
;
7454 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7457 * Catch errors reported by us and routines below us, and return NULL
7460 if (setjmp(cstate
->top_ctx
))
7463 res
= pcap_nametoaddrinfo(s
);
7465 bpf_error(cstate
, "invalid ip6 address %s", s
);
7468 bpf_error(cstate
, "%s resolved to multiple address", s
);
7469 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7471 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7472 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7473 memset(&mask
, 0, sizeof(mask
));
7474 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7476 mask
.s6_addr
[masklen
/ 8] =
7477 (0xff << (8 - masklen
% 8)) & 0xff;
7480 memcpy(a
, addr
, sizeof(a
));
7481 memcpy(m
, &mask
, sizeof(m
));
7482 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7483 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7484 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7492 bpf_error(cstate
, "Mask syntax for networks only");
7496 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7502 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7509 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7511 struct block
*b
, *tmp
;
7514 * Catch errors reported by us and routines below us, and return NULL
7517 if (setjmp(cstate
->top_ctx
))
7520 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7521 cstate
->e
= pcap_ether_aton(s
);
7522 if (cstate
->e
== NULL
)
7523 bpf_error(cstate
, "malloc");
7524 switch (cstate
->linktype
) {
7526 case DLT_NETANALYZER
:
7527 case DLT_NETANALYZER_TRANSPARENT
:
7528 tmp
= gen_prevlinkhdr_check(cstate
);
7529 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7534 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7537 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7539 case DLT_IEEE802_11
:
7540 case DLT_PRISM_HEADER
:
7541 case DLT_IEEE802_11_RADIO_AVS
:
7542 case DLT_IEEE802_11_RADIO
:
7544 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7546 case DLT_IP_OVER_FC
:
7547 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7552 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7559 bpf_error(cstate
, "ethernet address used in non-ether expression");
7564 sappend(struct slist
*s0
, struct slist
*s1
)
7567 * This is definitely not the best way to do this, but the
7568 * lists will rarely get long.
7575 static struct slist
*
7576 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7580 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7585 static struct slist
*
7586 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7590 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7596 * Modify "index" to use the value stored into its register as an
7597 * offset relative to the beginning of the header for the protocol
7598 * "proto", and allocate a register and put an item "size" bytes long
7599 * (1, 2, or 4) at that offset into that register, making it the register
7602 static struct arth
*
7603 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7607 struct slist
*s
, *tmp
;
7609 int regno
= alloc_reg(cstate
);
7611 free_reg(cstate
, inst
->regno
);
7615 bpf_error(cstate
, "data size must be 1, 2, or 4");
7632 bpf_error(cstate
, "unsupported index operation");
7636 * The offset is relative to the beginning of the packet
7637 * data, if we have a radio header. (If we don't, this
7640 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7641 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7642 cstate
->linktype
!= DLT_PRISM_HEADER
)
7643 bpf_error(cstate
, "radio information not present in capture");
7646 * Load into the X register the offset computed into the
7647 * register specified by "index".
7649 s
= xfer_to_x(cstate
, inst
);
7652 * Load the item at that offset.
7654 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7656 sappend(inst
->s
, s
);
7661 * The offset is relative to the beginning of
7662 * the link-layer header.
7664 * XXX - what about ATM LANE? Should the index be
7665 * relative to the beginning of the AAL5 frame, so
7666 * that 0 refers to the beginning of the LE Control
7667 * field, or relative to the beginning of the LAN
7668 * frame, so that 0 refers, for Ethernet LANE, to
7669 * the beginning of the destination address?
7671 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7674 * If "s" is non-null, it has code to arrange that the
7675 * X register contains the length of the prefix preceding
7676 * the link-layer header. Add to it the offset computed
7677 * into the register specified by "index", and move that
7678 * into the X register. Otherwise, just load into the X
7679 * register the offset computed into the register specified
7683 sappend(s
, xfer_to_a(cstate
, inst
));
7684 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7685 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7687 s
= xfer_to_x(cstate
, inst
);
7690 * Load the item at the sum of the offset we've put in the
7691 * X register and the offset of the start of the link
7692 * layer header (which is 0 if the radio header is
7693 * variable-length; that header length is what we put
7694 * into the X register and then added to the index).
7696 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7697 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7699 sappend(inst
->s
, s
);
7713 * The offset is relative to the beginning of
7714 * the network-layer header.
7715 * XXX - are there any cases where we want
7716 * cstate->off_nl_nosnap?
7718 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7721 * If "s" is non-null, it has code to arrange that the
7722 * X register contains the variable part of the offset
7723 * of the link-layer payload. Add to it the offset
7724 * computed into the register specified by "index",
7725 * and move that into the X register. Otherwise, just
7726 * load into the X register the offset computed into
7727 * the register specified by "index".
7730 sappend(s
, xfer_to_a(cstate
, inst
));
7731 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7732 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7734 s
= xfer_to_x(cstate
, inst
);
7737 * Load the item at the sum of the offset we've put in the
7738 * X register, the offset of the start of the network
7739 * layer header from the beginning of the link-layer
7740 * payload, and the constant part of the offset of the
7741 * start of the link-layer payload.
7743 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7744 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7746 sappend(inst
->s
, s
);
7749 * Do the computation only if the packet contains
7750 * the protocol in question.
7752 b
= gen_proto_abbrev_internal(cstate
, proto
);
7754 gen_and(inst
->b
, b
);
7768 * The offset is relative to the beginning of
7769 * the transport-layer header.
7771 * Load the X register with the length of the IPv4 header
7772 * (plus the offset of the link-layer header, if it's
7773 * a variable-length header), in bytes.
7775 * XXX - are there any cases where we want
7776 * cstate->off_nl_nosnap?
7777 * XXX - we should, if we're built with
7778 * IPv6 support, generate code to load either
7779 * IPv4, IPv6, or both, as appropriate.
7781 s
= gen_loadx_iphdrlen(cstate
);
7784 * The X register now contains the sum of the variable
7785 * part of the offset of the link-layer payload and the
7786 * length of the network-layer header.
7788 * Load into the A register the offset relative to
7789 * the beginning of the transport layer header,
7790 * add the X register to that, move that to the
7791 * X register, and load with an offset from the
7792 * X register equal to the sum of the constant part of
7793 * the offset of the link-layer payload and the offset,
7794 * relative to the beginning of the link-layer payload,
7795 * of the network-layer header.
7797 sappend(s
, xfer_to_a(cstate
, inst
));
7798 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7799 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7800 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7801 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7802 sappend(inst
->s
, s
);
7805 * Do the computation only if the packet contains
7806 * the protocol in question - which is true only
7807 * if this is an IP datagram and is the first or
7808 * only fragment of that datagram.
7810 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7812 gen_and(inst
->b
, b
);
7813 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7818 * Do the computation only if the packet contains
7819 * the protocol in question.
7821 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7823 gen_and(inst
->b
, b
);
7827 * Check if we have an icmp6 next header
7829 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7831 gen_and(inst
->b
, b
);
7834 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7836 * If "s" is non-null, it has code to arrange that the
7837 * X register contains the variable part of the offset
7838 * of the link-layer payload. Add to it the offset
7839 * computed into the register specified by "index",
7840 * and move that into the X register. Otherwise, just
7841 * load into the X register the offset computed into
7842 * the register specified by "index".
7845 sappend(s
, xfer_to_a(cstate
, inst
));
7846 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7847 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7849 s
= xfer_to_x(cstate
, inst
);
7852 * Load the item at the sum of the offset we've put in the
7853 * X register, the offset of the start of the network
7854 * layer header from the beginning of the link-layer
7855 * payload, and the constant part of the offset of the
7856 * start of the link-layer payload.
7858 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7859 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7862 sappend(inst
->s
, s
);
7866 inst
->regno
= regno
;
7867 s
= new_stmt(cstate
, BPF_ST
);
7869 sappend(inst
->s
, s
);
7875 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7879 * Catch errors reported by us and routines below us, and return NULL
7882 if (setjmp(cstate
->top_ctx
))
7885 return gen_load_internal(cstate
, proto
, inst
, size
);
7888 static struct block
*
7889 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7890 struct arth
*a1
, int reversed
)
7892 struct slist
*s0
, *s1
, *s2
;
7893 struct block
*b
, *tmp
;
7895 s0
= xfer_to_x(cstate
, a1
);
7896 s1
= xfer_to_a(cstate
, a0
);
7897 if (code
== BPF_JEQ
) {
7898 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7899 b
= new_block(cstate
, JMP(code
));
7903 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7909 sappend(a0
->s
, a1
->s
);
7913 free_reg(cstate
, a0
->regno
);
7914 free_reg(cstate
, a1
->regno
);
7916 /* 'and' together protocol checks */
7919 gen_and(a0
->b
, tmp
= a1
->b
);
7933 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7934 struct arth
*a1
, int reversed
)
7937 * Catch errors reported by us and routines below us, and return NULL
7940 if (setjmp(cstate
->top_ctx
))
7943 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7947 gen_loadlen(compiler_state_t
*cstate
)
7954 * Catch errors reported by us and routines below us, and return NULL
7957 if (setjmp(cstate
->top_ctx
))
7960 regno
= alloc_reg(cstate
);
7961 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7962 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7963 s
->next
= new_stmt(cstate
, BPF_ST
);
7964 s
->next
->s
.k
= regno
;
7971 static struct arth
*
7972 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7978 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7980 reg
= alloc_reg(cstate
);
7982 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7984 s
->next
= new_stmt(cstate
, BPF_ST
);
7993 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7996 * Catch errors reported by us and routines below us, and return NULL
7999 if (setjmp(cstate
->top_ctx
))
8002 return gen_loadi_internal(cstate
, val
);
8006 * The a_arg dance is to avoid annoying whining by compilers that
8007 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8008 * It's not *used* after setjmp returns.
8011 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8013 struct arth
*a
= a_arg
;
8017 * Catch errors reported by us and routines below us, and return NULL
8020 if (setjmp(cstate
->top_ctx
))
8023 s
= xfer_to_a(cstate
, a
);
8025 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8028 s
= new_stmt(cstate
, BPF_ST
);
8036 * The a0_arg dance is to avoid annoying whining by compilers that
8037 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8038 * It's not *used* after setjmp returns.
8041 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8044 struct arth
*a0
= a0_arg
;
8045 struct slist
*s0
, *s1
, *s2
;
8048 * Catch errors reported by us and routines below us, and return NULL
8051 if (setjmp(cstate
->top_ctx
))
8055 * Disallow division by, or modulus by, zero; we do this here
8056 * so that it gets done even if the optimizer is disabled.
8058 * Also disallow shifts by a value greater than 31; we do this
8059 * here, for the same reason.
8061 if (code
== BPF_DIV
) {
8062 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8063 bpf_error(cstate
, "division by zero");
8064 } else if (code
== BPF_MOD
) {
8065 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8066 bpf_error(cstate
, "modulus by zero");
8067 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8068 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8069 bpf_error(cstate
, "shift by more than 31 bits");
8071 s0
= xfer_to_x(cstate
, a1
);
8072 s1
= xfer_to_a(cstate
, a0
);
8073 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8078 sappend(a0
->s
, a1
->s
);
8080 free_reg(cstate
, a0
->regno
);
8081 free_reg(cstate
, a1
->regno
);
8083 s0
= new_stmt(cstate
, BPF_ST
);
8084 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8091 * Initialize the table of used registers and the current register.
8094 init_regs(compiler_state_t
*cstate
)
8097 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8101 * Return the next free register.
8104 alloc_reg(compiler_state_t
*cstate
)
8106 int n
= BPF_MEMWORDS
;
8109 if (cstate
->regused
[cstate
->curreg
])
8110 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8112 cstate
->regused
[cstate
->curreg
] = 1;
8113 return cstate
->curreg
;
8116 bpf_error(cstate
, "too many registers needed to evaluate expression");
8121 * Return a register to the table so it can
8125 free_reg(compiler_state_t
*cstate
, int n
)
8127 cstate
->regused
[n
] = 0;
8130 static struct block
*
8131 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8136 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8137 b
= new_block(cstate
, JMP(jmp
));
8145 gen_greater(compiler_state_t
*cstate
, int n
)
8148 * Catch errors reported by us and routines below us, and return NULL
8151 if (setjmp(cstate
->top_ctx
))
8154 return gen_len(cstate
, BPF_JGE
, n
);
8158 * Actually, this is less than or equal.
8161 gen_less(compiler_state_t
*cstate
, int n
)
8166 * Catch errors reported by us and routines below us, and return NULL
8169 if (setjmp(cstate
->top_ctx
))
8172 b
= gen_len(cstate
, BPF_JGT
, n
);
8179 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8180 * the beginning of the link-layer header.
8181 * XXX - that means you can't test values in the radiotap header, but
8182 * as that header is difficult if not impossible to parse generally
8183 * without a loop, that might not be a severe problem. A new keyword
8184 * "radio" could be added for that, although what you'd really want
8185 * would be a way of testing particular radio header values, which
8186 * would generate code appropriate to the radio header in question.
8189 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8195 * Catch errors reported by us and routines below us, and return NULL
8198 if (setjmp(cstate
->top_ctx
))
8206 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8209 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8213 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8217 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8221 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8225 b
= new_block(cstate
, JMP(BPF_JEQ
));
8232 static const u_char abroadcast
[] = { 0x0 };
8235 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8237 bpf_u_int32 hostmask
;
8238 struct block
*b0
, *b1
, *b2
;
8239 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8242 * Catch errors reported by us and routines below us, and return NULL
8245 if (setjmp(cstate
->top_ctx
))
8252 switch (cstate
->linktype
) {
8254 case DLT_ARCNET_LINUX
:
8255 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8257 case DLT_NETANALYZER
:
8258 case DLT_NETANALYZER_TRANSPARENT
:
8259 b1
= gen_prevlinkhdr_check(cstate
);
8260 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8265 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8267 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8268 case DLT_IEEE802_11
:
8269 case DLT_PRISM_HEADER
:
8270 case DLT_IEEE802_11_RADIO_AVS
:
8271 case DLT_IEEE802_11_RADIO
:
8273 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8274 case DLT_IP_OVER_FC
:
8275 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8277 bpf_error(cstate
, "not a broadcast link");
8283 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8284 * as an indication that we don't know the netmask, and fail
8287 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8288 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8289 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8290 hostmask
= ~cstate
->netmask
;
8291 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8292 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8293 ~0 & hostmask
, hostmask
);
8298 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8303 * Generate code to test the low-order bit of a MAC address (that's
8304 * the bottom bit of the *first* byte).
8306 static struct block
*
8307 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8309 register struct block
*b0
;
8310 register struct slist
*s
;
8312 /* link[offset] & 1 != 0 */
8313 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8314 b0
= new_block(cstate
, JMP(BPF_JSET
));
8321 gen_multicast(compiler_state_t
*cstate
, int proto
)
8323 register struct block
*b0
, *b1
, *b2
;
8324 register struct slist
*s
;
8327 * Catch errors reported by us and routines below us, and return NULL
8330 if (setjmp(cstate
->top_ctx
))
8337 switch (cstate
->linktype
) {
8339 case DLT_ARCNET_LINUX
:
8340 /* all ARCnet multicasts use the same address */
8341 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8343 case DLT_NETANALYZER
:
8344 case DLT_NETANALYZER_TRANSPARENT
:
8345 b1
= gen_prevlinkhdr_check(cstate
);
8346 /* ether[0] & 1 != 0 */
8347 b0
= gen_mac_multicast(cstate
, 0);
8353 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8355 * XXX - was that referring to bit-order issues?
8357 /* fddi[1] & 1 != 0 */
8358 return gen_mac_multicast(cstate
, 1);
8360 /* tr[2] & 1 != 0 */
8361 return gen_mac_multicast(cstate
, 2);
8362 case DLT_IEEE802_11
:
8363 case DLT_PRISM_HEADER
:
8364 case DLT_IEEE802_11_RADIO_AVS
:
8365 case DLT_IEEE802_11_RADIO
:
8370 * For control frames, there is no DA.
8372 * For management frames, DA is at an
8373 * offset of 4 from the beginning of
8376 * For data frames, DA is at an offset
8377 * of 4 from the beginning of the packet
8378 * if To DS is clear and at an offset of
8379 * 16 from the beginning of the packet
8384 * Generate the tests to be done for data frames.
8386 * First, check for To DS set, i.e. "link[1] & 0x01".
8388 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8389 b1
= new_block(cstate
, JMP(BPF_JSET
));
8390 b1
->s
.k
= 0x01; /* To DS */
8394 * If To DS is set, the DA is at 16.
8396 b0
= gen_mac_multicast(cstate
, 16);
8400 * Now, check for To DS not set, i.e. check
8401 * "!(link[1] & 0x01)".
8403 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8404 b2
= new_block(cstate
, JMP(BPF_JSET
));
8405 b2
->s
.k
= 0x01; /* To DS */
8410 * If To DS is not set, the DA is at 4.
8412 b1
= gen_mac_multicast(cstate
, 4);
8416 * Now OR together the last two checks. That gives
8417 * the complete set of checks for data frames.
8422 * Now check for a data frame.
8423 * I.e, check "link[0] & 0x08".
8425 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8426 b1
= new_block(cstate
, JMP(BPF_JSET
));
8431 * AND that with the checks done for data frames.
8436 * If the high-order bit of the type value is 0, this
8437 * is a management frame.
8438 * I.e, check "!(link[0] & 0x08)".
8440 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8441 b2
= new_block(cstate
, JMP(BPF_JSET
));
8447 * For management frames, the DA is at 4.
8449 b1
= gen_mac_multicast(cstate
, 4);
8453 * OR that with the checks done for data frames.
8454 * That gives the checks done for management and
8460 * If the low-order bit of the type value is 1,
8461 * this is either a control frame or a frame
8462 * with a reserved type, and thus not a
8465 * I.e., check "!(link[0] & 0x04)".
8467 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8468 b1
= new_block(cstate
, JMP(BPF_JSET
));
8474 * AND that with the checks for data and management
8479 case DLT_IP_OVER_FC
:
8480 b0
= gen_mac_multicast(cstate
, 2);
8485 /* Link not known to support multicasts */
8489 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8490 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8495 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8496 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8500 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8505 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8507 register struct block
*b0
;
8510 * Catch errors reported by us and routines below us, and return NULL
8513 if (setjmp(cstate
->top_ctx
))
8517 * Only some data link types support ifindex qualifiers.
8519 switch (cstate
->linktype
) {
8520 case DLT_LINUX_SLL2
:
8521 /* match packets on this interface */
8522 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8527 * This is Linux; we require PF_PACKET support.
8528 * If this is a *live* capture, we can look at
8529 * special meta-data in the filter expression;
8530 * if it's a savefile, we can't.
8532 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8533 /* We have a FILE *, so this is a savefile */
8534 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8535 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8539 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8541 #else /* defined(linux) */
8542 bpf_error(cstate
, "ifindex not supported on %s",
8543 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8545 #endif /* defined(linux) */
8551 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8552 * Outbound traffic is sent by this machine, while inbound traffic is
8553 * sent by a remote machine (and may include packets destined for a
8554 * unicast or multicast link-layer address we are not subscribing to).
8555 * These are the same definitions implemented by pcap_setdirection().
8556 * Capturing only unicast traffic destined for this host is probably
8557 * better accomplished using a higher-layer filter.
8560 gen_inbound(compiler_state_t
*cstate
, int dir
)
8562 register struct block
*b0
;
8565 * Catch errors reported by us and routines below us, and return NULL
8568 if (setjmp(cstate
->top_ctx
))
8572 * Only some data link types support inbound/outbound qualifiers.
8574 switch (cstate
->linktype
) {
8576 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8577 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8578 gen_loadi_internal(cstate
, 0),
8584 /* match outgoing packets */
8585 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8587 /* match incoming packets */
8588 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8593 /* match outgoing packets */
8594 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8596 /* to filter on inbound traffic, invert the match */
8601 case DLT_LINUX_SLL2
:
8602 /* match outgoing packets */
8603 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8605 /* to filter on inbound traffic, invert the match */
8611 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8612 ((dir
== 0) ? PF_IN
: PF_OUT
));
8617 /* match outgoing packets */
8618 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8620 /* match incoming packets */
8621 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8625 case DLT_JUNIPER_MFR
:
8626 case DLT_JUNIPER_MLFR
:
8627 case DLT_JUNIPER_MLPPP
:
8628 case DLT_JUNIPER_ATM1
:
8629 case DLT_JUNIPER_ATM2
:
8630 case DLT_JUNIPER_PPPOE
:
8631 case DLT_JUNIPER_PPPOE_ATM
:
8632 case DLT_JUNIPER_GGSN
:
8633 case DLT_JUNIPER_ES
:
8634 case DLT_JUNIPER_MONITOR
:
8635 case DLT_JUNIPER_SERVICES
:
8636 case DLT_JUNIPER_ETHER
:
8637 case DLT_JUNIPER_PPP
:
8638 case DLT_JUNIPER_FRELAY
:
8639 case DLT_JUNIPER_CHDLC
:
8640 case DLT_JUNIPER_VP
:
8641 case DLT_JUNIPER_ST
:
8642 case DLT_JUNIPER_ISM
:
8643 case DLT_JUNIPER_VS
:
8644 case DLT_JUNIPER_SRX_E2E
:
8645 case DLT_JUNIPER_FIBRECHANNEL
:
8646 case DLT_JUNIPER_ATM_CEMIC
:
8648 /* juniper flags (including direction) are stored
8649 * the byte after the 3-byte magic number */
8651 /* match outgoing packets */
8652 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8654 /* match incoming packets */
8655 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8661 * If we have packet meta-data indicating a direction,
8662 * and that metadata can be checked by BPF code, check
8663 * it. Otherwise, give up, as this link-layer type has
8664 * nothing in the packet data.
8666 * Currently, the only platform where a BPF filter can
8667 * check that metadata is Linux with the in-kernel
8668 * BPF interpreter. If other packet capture mechanisms
8669 * and BPF filters also supported this, it would be
8670 * nice. It would be even better if they made that
8671 * metadata available so that we could provide it
8672 * with newer capture APIs, allowing it to be saved
8677 * This is Linux; we require PF_PACKET support.
8678 * If this is a *live* capture, we can look at
8679 * special meta-data in the filter expression;
8680 * if it's a savefile, we can't.
8682 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8683 /* We have a FILE *, so this is a savefile */
8684 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8685 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8688 /* match outgoing packets */
8689 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8692 /* to filter on inbound traffic, invert the match */
8695 #else /* defined(linux) */
8696 bpf_error(cstate
, "inbound/outbound not supported on %s",
8697 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8699 #endif /* defined(linux) */
8704 /* PF firewall log matched interface */
8706 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8712 * Catch errors reported by us and routines below us, and return NULL
8715 if (setjmp(cstate
->top_ctx
))
8718 if (cstate
->linktype
!= DLT_PFLOG
) {
8719 bpf_error(cstate
, "ifname supported only on PF linktype");
8722 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8723 off
= offsetof(struct pfloghdr
, ifname
);
8724 if (strlen(ifname
) >= len
) {
8725 bpf_error(cstate
, "ifname interface names can only be %d characters",
8729 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8730 (const u_char
*)ifname
);
8734 /* PF firewall log ruleset name */
8736 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8741 * Catch errors reported by us and routines below us, and return NULL
8744 if (setjmp(cstate
->top_ctx
))
8747 if (cstate
->linktype
!= DLT_PFLOG
) {
8748 bpf_error(cstate
, "ruleset supported only on PF linktype");
8752 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8753 bpf_error(cstate
, "ruleset names can only be %ld characters",
8754 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8758 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8759 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8763 /* PF firewall log rule number */
8765 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8770 * Catch errors reported by us and routines below us, and return NULL
8773 if (setjmp(cstate
->top_ctx
))
8776 if (cstate
->linktype
!= DLT_PFLOG
) {
8777 bpf_error(cstate
, "rnr supported only on PF linktype");
8781 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8786 /* PF firewall log sub-rule number */
8788 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8793 * Catch errors reported by us and routines below us, and return NULL
8796 if (setjmp(cstate
->top_ctx
))
8799 if (cstate
->linktype
!= DLT_PFLOG
) {
8800 bpf_error(cstate
, "srnr supported only on PF linktype");
8804 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8809 /* PF firewall log reason code */
8811 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8816 * Catch errors reported by us and routines below us, and return NULL
8819 if (setjmp(cstate
->top_ctx
))
8822 if (cstate
->linktype
!= DLT_PFLOG
) {
8823 bpf_error(cstate
, "reason supported only on PF linktype");
8827 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8828 (bpf_u_int32
)reason
);
8832 /* PF firewall log action */
8834 gen_pf_action(compiler_state_t
*cstate
, int action
)
8839 * Catch errors reported by us and routines below us, and return NULL
8842 if (setjmp(cstate
->top_ctx
))
8845 if (cstate
->linktype
!= DLT_PFLOG
) {
8846 bpf_error(cstate
, "action supported only on PF linktype");
8850 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8851 (bpf_u_int32
)action
);
8855 /* IEEE 802.11 wireless header */
8857 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8862 * Catch errors reported by us and routines below us, and return NULL
8865 if (setjmp(cstate
->top_ctx
))
8868 switch (cstate
->linktype
) {
8870 case DLT_IEEE802_11
:
8871 case DLT_PRISM_HEADER
:
8872 case DLT_IEEE802_11_RADIO_AVS
:
8873 case DLT_IEEE802_11_RADIO
:
8874 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8878 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8886 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8891 * Catch errors reported by us and routines below us, and return NULL
8894 if (setjmp(cstate
->top_ctx
))
8897 switch (cstate
->linktype
) {
8899 case DLT_IEEE802_11
:
8900 case DLT_PRISM_HEADER
:
8901 case DLT_IEEE802_11_RADIO_AVS
:
8902 case DLT_IEEE802_11_RADIO
:
8906 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8910 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8911 IEEE80211_FC1_DIR_MASK
);
8917 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8922 * Catch errors reported by us and routines below us, and return NULL
8925 if (setjmp(cstate
->top_ctx
))
8928 switch (cstate
->linktype
) {
8931 case DLT_ARCNET_LINUX
:
8932 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8933 q
.proto
== Q_LINK
) {
8934 cstate
->e
= pcap_ether_aton(s
);
8935 if (cstate
->e
== NULL
)
8936 bpf_error(cstate
, "malloc");
8937 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8942 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8946 bpf_error(cstate
, "aid supported only on ARCnet");
8951 static struct block
*
8952 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8954 register struct block
*b0
, *b1
;
8957 /* src comes first, different from Ethernet */
8959 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8962 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8965 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8966 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8972 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8973 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8978 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8982 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8986 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8990 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8994 bpf_error(cstate
, "'ra' is only supported on 802.11");
8998 bpf_error(cstate
, "'ta' is only supported on 802.11");
9005 static struct block
*
9006 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9008 struct block
*b0
, *b1
;
9010 /* check for VLAN, including 802.1ad and QinQ */
9011 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9012 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9015 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9021 static struct block
*
9022 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9024 if (vlan_num
> 0x0fff) {
9025 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9028 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9031 static struct block
*
9032 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9035 struct block
*b0
, *b1
;
9037 b0
= gen_vlan_tpid_test(cstate
);
9040 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9046 * Both payload and link header type follow the VLAN tags so that
9047 * both need to be updated.
9049 cstate
->off_linkpl
.constant_part
+= 4;
9050 cstate
->off_linktype
.constant_part
+= 4;
9055 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9056 /* add v to variable part of off */
9058 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9059 bpf_u_int32 v
, struct slist
*s
)
9063 if (!off
->is_variable
)
9064 off
->is_variable
= 1;
9066 off
->reg
= alloc_reg(cstate
);
9068 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9071 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9074 s2
= new_stmt(cstate
, BPF_ST
);
9080 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9081 * and link type offsets first
9084 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9088 /* offset determined at run time, shift variable part */
9090 cstate
->is_vlan_vloffset
= 1;
9091 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9092 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9094 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9095 sappend(s
.next
, b_tpid
->head
->stmts
);
9096 b_tpid
->head
->stmts
= s
.next
;
9100 * patch block b_vid (VLAN id test) to load VID value either from packet
9101 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9104 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9106 struct slist
*s
, *s2
, *sjeq
;
9109 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9110 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9112 /* true -> next instructions, false -> beginning of b_vid */
9113 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9115 sjeq
->s
.jf
= b_vid
->stmts
;
9118 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9119 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
9123 /* Jump to the test in b_vid. We need to jump one instruction before
9124 * the end of the b_vid block so that we only skip loading the TCI
9125 * from packet data and not the 'and' instruction extracting VID.
9128 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9130 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9134 /* insert our statements at the beginning of b_vid */
9135 sappend(s
, b_vid
->stmts
);
9140 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9141 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9142 * tag can be either in metadata or in packet data; therefore if the
9143 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9144 * header for VLAN tag. As the decision is done at run time, we need
9145 * update variable part of the offsets
9147 static struct block
*
9148 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9151 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9154 /* generate new filter code based on extracting packet
9156 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9157 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9159 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9164 * This is tricky. We need to insert the statements updating variable
9165 * parts of offsets before the traditional TPID and VID tests so
9166 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9167 * we do not want this update to affect those checks. That's why we
9168 * generate both test blocks first and insert the statements updating
9169 * variable parts of both offsets after that. This wouldn't work if
9170 * there already were variable length link header when entering this
9171 * function but gen_vlan_bpf_extensions() isn't called in that case.
9173 b_tpid
= gen_vlan_tpid_test(cstate
);
9175 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9177 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9182 gen_vlan_patch_vid_test(cstate
, b_vid
);
9192 * support IEEE 802.1Q VLAN trunk over ethernet
9195 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9200 * Catch errors reported by us and routines below us, and return NULL
9203 if (setjmp(cstate
->top_ctx
))
9206 /* can't check for VLAN-encapsulated packets inside MPLS */
9207 if (cstate
->label_stack_depth
> 0)
9208 bpf_error(cstate
, "no VLAN match after MPLS");
9211 * Check for a VLAN packet, and then change the offsets to point
9212 * to the type and data fields within the VLAN packet. Just
9213 * increment the offsets, so that we can support a hierarchy, e.g.
9214 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
9217 * XXX - this is a bit of a kludge. If we were to split the
9218 * compiler into a parser that parses an expression and
9219 * generates an expression tree, and a code generator that
9220 * takes an expression tree (which could come from our
9221 * parser or from some other parser) and generates BPF code,
9222 * we could perhaps make the offsets parameters of routines
9223 * and, in the handler for an "AND" node, pass to subnodes
9224 * other than the VLAN node the adjusted offsets.
9226 * This would mean that "vlan" would, instead of changing the
9227 * behavior of *all* tests after it, change only the behavior
9228 * of tests ANDed with it. That would change the documented
9229 * semantics of "vlan", which might break some expressions.
9230 * However, it would mean that "(vlan and ip) or ip" would check
9231 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9232 * checking only for VLAN-encapsulated IP, so that could still
9233 * be considered worth doing; it wouldn't break expressions
9234 * that are of the form "vlan and ..." or "vlan N and ...",
9235 * which I suspect are the most common expressions involving
9236 * "vlan". "vlan or ..." doesn't necessarily do what the user
9237 * would really want, now, as all the "or ..." tests would
9238 * be done assuming a VLAN, even though the "or" could be viewed
9239 * as meaning "or, if this isn't a VLAN packet...".
9241 switch (cstate
->linktype
) {
9245 * Newer version of the Linux kernel pass around
9246 * packets in which the VLAN tag has been removed
9247 * from the packet data and put into metadata.
9249 * This requires special treatment.
9251 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9252 /* Verify that this is the outer part of the packet and
9253 * not encapsulated somehow. */
9254 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9255 cstate
->off_linkhdr
.constant_part
==
9256 cstate
->off_outermostlinkhdr
.constant_part
) {
9258 * Do we need special VLAN handling?
9260 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9261 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9264 b0
= gen_vlan_no_bpf_extensions(cstate
,
9265 vlan_num
, has_vlan_tag
);
9268 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9272 case DLT_NETANALYZER
:
9273 case DLT_NETANALYZER_TRANSPARENT
:
9274 case DLT_IEEE802_11
:
9275 case DLT_PRISM_HEADER
:
9276 case DLT_IEEE802_11_RADIO_AVS
:
9277 case DLT_IEEE802_11_RADIO
:
9279 * These are either Ethernet packets with an additional
9280 * metadata header (the NetAnalyzer types), or 802.11
9281 * packets, possibly with an additional metadata header.
9283 * For the first of those, the VLAN tag is in the normal
9284 * place, so the special-case handling above isn't
9287 * For the second of those, we don't do the special-case
9290 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9294 bpf_error(cstate
, "no VLAN support for %s",
9295 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9299 cstate
->vlan_stack_depth
++;
9307 * The label_num_arg dance is to avoid annoying whining by compilers that
9308 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9309 * It's not *used* after setjmp returns.
9312 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9315 volatile bpf_u_int32 label_num
= label_num_arg
;
9316 struct block
*b0
, *b1
;
9319 * Catch errors reported by us and routines below us, and return NULL
9322 if (setjmp(cstate
->top_ctx
))
9325 if (cstate
->label_stack_depth
> 0) {
9326 /* just match the bottom-of-stack bit clear */
9327 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9330 * We're not in an MPLS stack yet, so check the link-layer
9331 * type against MPLS.
9333 switch (cstate
->linktype
) {
9335 case DLT_C_HDLC
: /* fall through */
9338 case DLT_NETANALYZER
:
9339 case DLT_NETANALYZER_TRANSPARENT
:
9340 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9344 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9347 /* FIXME add other DLT_s ...
9348 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9349 * leave it for now */
9352 bpf_error(cstate
, "no MPLS support for %s",
9353 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9358 /* If a specific MPLS label is requested, check it */
9359 if (has_label_num
) {
9360 if (label_num
> 0xFFFFF) {
9361 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9362 label_num
, 0xFFFFF);
9364 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9365 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9366 0xfffff000); /* only compare the first 20 bits */
9372 * Change the offsets to point to the type and data fields within
9373 * the MPLS packet. Just increment the offsets, so that we
9374 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9375 * capture packets with an outer label of 100000 and an inner
9378 * Increment the MPLS stack depth as well; this indicates that
9379 * we're checking MPLS-encapsulated headers, to make sure higher
9380 * level code generators don't try to match against IP-related
9381 * protocols such as Q_ARP, Q_RARP etc.
9383 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9385 cstate
->off_nl_nosnap
+= 4;
9386 cstate
->off_nl
+= 4;
9387 cstate
->label_stack_depth
++;
9392 * Support PPPOE discovery and session.
9395 gen_pppoed(compiler_state_t
*cstate
)
9398 * Catch errors reported by us and routines below us, and return NULL
9401 if (setjmp(cstate
->top_ctx
))
9404 /* check for PPPoE discovery */
9405 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9409 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9411 struct block
*b0
, *b1
;
9414 * Catch errors reported by us and routines below us, and return NULL
9417 if (setjmp(cstate
->top_ctx
))
9421 * Test against the PPPoE session link-layer type.
9423 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9425 /* If a specific session is requested, check PPPoE session id */
9427 if (sess_num
> 0x0000ffff) {
9428 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9429 sess_num
, 0x0000ffff);
9431 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9437 * Change the offsets to point to the type and data fields within
9438 * the PPP packet, and note that this is PPPoE rather than
9441 * XXX - this is a bit of a kludge. See the comments in
9444 * The "network-layer" protocol is PPPoE, which has a 6-byte
9445 * PPPoE header, followed by a PPP packet.
9447 * There is no HDLC encapsulation for the PPP packet (it's
9448 * encapsulated in PPPoES instead), so the link-layer type
9449 * starts at the first byte of the PPP packet. For PPPoE,
9450 * that offset is relative to the beginning of the total
9451 * link-layer payload, including any 802.2 LLC header, so
9452 * it's 6 bytes past cstate->off_nl.
9454 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9455 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9456 cstate
->off_linkpl
.reg
);
9458 cstate
->off_linktype
= cstate
->off_linkhdr
;
9459 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9462 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9467 /* Check that this is Geneve and the VNI is correct if
9468 * specified. Parameterized to handle both IPv4 and IPv6. */
9469 static struct block
*
9470 gen_geneve_check(compiler_state_t
*cstate
,
9471 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9472 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9474 struct block
*b0
, *b1
;
9476 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9478 /* Check that we are operating on version 0. Otherwise, we
9479 * can't decode the rest of the fields. The version is 2 bits
9480 * in the first byte of the Geneve header. */
9481 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9486 if (vni
> 0xffffff) {
9487 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9490 vni
<<= 8; /* VNI is in the upper 3 bytes */
9491 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9499 /* The IPv4 and IPv6 Geneve checks need to do two things:
9500 * - Verify that this actually is Geneve with the right VNI.
9501 * - Place the IP header length (plus variable link prefix if
9502 * needed) into register A to be used later to compute
9503 * the inner packet offsets. */
9504 static struct block
*
9505 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9507 struct block
*b0
, *b1
;
9508 struct slist
*s
, *s1
;
9510 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9512 /* Load the IP header length into A. */
9513 s
= gen_loadx_iphdrlen(cstate
);
9515 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9518 /* Forcibly append these statements to the true condition
9519 * of the protocol check by creating a new block that is
9520 * always true and ANDing them. */
9521 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9530 static struct block
*
9531 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9533 struct block
*b0
, *b1
;
9534 struct slist
*s
, *s1
;
9536 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9538 /* Load the IP header length. We need to account for a
9539 * variable length link prefix if there is one. */
9540 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9542 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9546 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9550 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9554 /* Forcibly append these statements to the true condition
9555 * of the protocol check by creating a new block that is
9556 * always true and ANDing them. */
9557 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9560 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9569 /* We need to store three values based on the Geneve header::
9570 * - The offset of the linktype.
9571 * - The offset of the end of the Geneve header.
9572 * - The offset of the end of the encapsulated MAC header. */
9573 static struct slist
*
9574 gen_geneve_offsets(compiler_state_t
*cstate
)
9576 struct slist
*s
, *s1
, *s_proto
;
9578 /* First we need to calculate the offset of the Geneve header
9579 * itself. This is composed of the IP header previously calculated
9580 * (include any variable link prefix) and stored in A plus the
9581 * fixed sized headers (fixed link prefix, MAC length, and UDP
9583 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9584 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9586 /* Stash this in X since we'll need it later. */
9587 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9590 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9592 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9596 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9597 cstate
->off_linktype
.is_variable
= 1;
9598 cstate
->off_linktype
.constant_part
= 0;
9600 s1
= new_stmt(cstate
, BPF_ST
);
9601 s1
->s
.k
= cstate
->off_linktype
.reg
;
9604 /* Load the Geneve option length and mask and shift to get the
9605 * number of bytes. It is stored in the first byte of the Geneve
9607 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9611 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9615 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9619 /* Add in the rest of the Geneve base header. */
9620 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9624 /* Add the Geneve header length to its offset and store. */
9625 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9629 /* Set the encapsulated type as Ethernet. Even though we may
9630 * not actually have Ethernet inside there are two reasons this
9632 * - The linktype field is always in EtherType format regardless
9633 * of whether it is in Geneve or an inner Ethernet frame.
9634 * - The only link layer that we have specific support for is
9635 * Ethernet. We will confirm that the packet actually is
9636 * Ethernet at runtime before executing these checks. */
9637 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9639 s1
= new_stmt(cstate
, BPF_ST
);
9640 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9643 /* Calculate whether we have an Ethernet header or just raw IP/
9644 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9645 * and linktype by 14 bytes so that the network header can be found
9646 * seamlessly. Otherwise, keep what we've calculated already. */
9648 /* We have a bare jmp so we can't use the optimizer. */
9649 cstate
->no_optimize
= 1;
9651 /* Load the EtherType in the Geneve header, 2 bytes in. */
9652 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9656 /* Load X with the end of the Geneve header. */
9657 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9658 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9661 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9662 * end of this check, we should have the total length in X. In
9663 * the non-Ethernet case, it's already there. */
9664 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9665 s_proto
->s
.k
= ETHERTYPE_TEB
;
9666 sappend(s
, s_proto
);
9668 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9672 /* Since this is Ethernet, use the EtherType of the payload
9673 * directly as the linktype. Overwrite what we already have. */
9674 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9678 s1
= new_stmt(cstate
, BPF_ST
);
9679 s1
->s
.k
= cstate
->off_linktype
.reg
;
9682 /* Advance two bytes further to get the end of the Ethernet
9684 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9688 /* Move the result to X. */
9689 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9692 /* Store the final result of our linkpl calculation. */
9693 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9694 cstate
->off_linkpl
.is_variable
= 1;
9695 cstate
->off_linkpl
.constant_part
= 0;
9697 s1
= new_stmt(cstate
, BPF_STX
);
9698 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9707 /* Check to see if this is a Geneve packet. */
9709 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9711 struct block
*b0
, *b1
;
9715 * Catch errors reported by us and routines below us, and return NULL
9718 if (setjmp(cstate
->top_ctx
))
9721 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9722 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9727 /* Later filters should act on the payload of the Geneve frame,
9728 * update all of the header pointers. Attach this code so that
9729 * it gets executed in the event that the Geneve filter matches. */
9730 s
= gen_geneve_offsets(cstate
);
9732 b1
= gen_true(cstate
);
9733 sappend(s
, b1
->stmts
);
9738 cstate
->is_geneve
= 1;
9743 /* Check that the encapsulated frame has a link layer header
9744 * for Ethernet filters. */
9745 static struct block
*
9746 gen_geneve_ll_check(compiler_state_t
*cstate
)
9749 struct slist
*s
, *s1
;
9751 /* The easiest way to see if there is a link layer present
9752 * is to check if the link layer header and payload are not
9755 /* Geneve always generates pure variable offsets so we can
9756 * compare only the registers. */
9757 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9758 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9760 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9761 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9764 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9772 static struct block
*
9773 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9774 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9781 if (!cstate
->is_atm
)
9782 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9783 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9785 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9786 0xffffffffU
, jtype
, reverse
, jvalue
);
9790 if (!cstate
->is_atm
)
9791 bpf_error(cstate
, "'vci' supported only on raw ATM");
9792 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9794 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9795 0xffffffffU
, jtype
, reverse
, jvalue
);
9799 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9800 abort(); /* XXX - this isn't on FreeBSD */
9801 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9802 0x0fU
, jtype
, reverse
, jvalue
);
9806 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9808 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9809 0xffffffffU
, jtype
, reverse
, jvalue
);
9813 if (!cstate
->is_atm
)
9814 bpf_error(cstate
, "'callref' supported only on raw ATM");
9815 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9817 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9818 0xffffffffU
, jtype
, reverse
, jvalue
);
9827 static struct block
*
9828 gen_atmtype_metac(compiler_state_t
*cstate
)
9830 struct block
*b0
, *b1
;
9832 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9833 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9838 static struct block
*
9839 gen_atmtype_sc(compiler_state_t
*cstate
)
9841 struct block
*b0
, *b1
;
9843 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9844 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9849 static struct block
*
9850 gen_atmtype_llc(compiler_state_t
*cstate
)
9854 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9855 cstate
->linktype
= cstate
->prevlinktype
;
9860 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9861 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9864 * Catch errors reported by us and routines below us, and return NULL
9867 if (setjmp(cstate
->top_ctx
))
9870 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9875 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9877 struct block
*b0
, *b1
;
9880 * Catch errors reported by us and routines below us, and return NULL
9883 if (setjmp(cstate
->top_ctx
))
9889 /* Get all packets in Meta signalling Circuit */
9890 if (!cstate
->is_atm
)
9891 bpf_error(cstate
, "'metac' supported only on raw ATM");
9892 b1
= gen_atmtype_metac(cstate
);
9896 /* Get all packets in Broadcast Circuit*/
9897 if (!cstate
->is_atm
)
9898 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9899 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9900 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9905 /* Get all cells in Segment OAM F4 circuit*/
9906 if (!cstate
->is_atm
)
9907 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9908 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9909 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9914 /* Get all cells in End-to-End OAM F4 Circuit*/
9915 if (!cstate
->is_atm
)
9916 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9917 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9918 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9923 /* Get all packets in connection Signalling Circuit */
9924 if (!cstate
->is_atm
)
9925 bpf_error(cstate
, "'sc' supported only on raw ATM");
9926 b1
= gen_atmtype_sc(cstate
);
9930 /* Get all packets in ILMI Circuit */
9931 if (!cstate
->is_atm
)
9932 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9933 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9934 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9939 /* Get all LANE packets */
9940 if (!cstate
->is_atm
)
9941 bpf_error(cstate
, "'lane' supported only on raw ATM");
9942 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9945 * Arrange that all subsequent tests assume LANE
9946 * rather than LLC-encapsulated packets, and set
9947 * the offsets appropriately for LANE-encapsulated
9950 * We assume LANE means Ethernet, not Token Ring.
9952 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9953 cstate
->off_payload
+ 2, /* Ethernet header */
9955 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9956 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9957 cstate
->off_nl
= 0; /* Ethernet II */
9958 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9962 /* Get all LLC-encapsulated packets */
9963 if (!cstate
->is_atm
)
9964 bpf_error(cstate
, "'llc' supported only on raw ATM");
9965 b1
= gen_atmtype_llc(cstate
);
9975 * Filtering for MTP2 messages based on li value
9976 * FISU, length is null
9977 * LSSU, length is 1 or 2
9978 * MSU, length is 3 or more
9979 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9982 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9984 struct block
*b0
, *b1
;
9987 * Catch errors reported by us and routines below us, and return NULL
9990 if (setjmp(cstate
->top_ctx
))
9996 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9997 (cstate
->linktype
!= DLT_ERF
) &&
9998 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9999 bpf_error(cstate
, "'fisu' supported only on MTP2");
10000 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10001 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10002 0x3fU
, BPF_JEQ
, 0, 0U);
10006 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10007 (cstate
->linktype
!= DLT_ERF
) &&
10008 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10009 bpf_error(cstate
, "'lssu' supported only on MTP2");
10010 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10011 0x3fU
, BPF_JGT
, 1, 2U);
10012 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10013 0x3fU
, BPF_JGT
, 0, 0U);
10018 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10019 (cstate
->linktype
!= DLT_ERF
) &&
10020 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10021 bpf_error(cstate
, "'msu' supported only on MTP2");
10022 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10023 0x3fU
, BPF_JGT
, 0, 2U);
10027 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10028 (cstate
->linktype
!= DLT_ERF
) &&
10029 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10030 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10031 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10032 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10033 0xff80U
, BPF_JEQ
, 0, 0U);
10037 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10038 (cstate
->linktype
!= DLT_ERF
) &&
10039 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10040 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10041 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10042 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10043 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10044 0xff80U
, BPF_JGT
, 0, 0U);
10049 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10050 (cstate
->linktype
!= DLT_ERF
) &&
10051 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10052 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10053 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10054 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10064 * The jvalue_arg dance is to avoid annoying whining by compilers that
10065 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
10066 * It's not *used* after setjmp returns.
10069 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10070 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
10072 volatile bpf_u_int32 jvalue
= jvalue_arg
;
10074 bpf_u_int32 val1
, val2
, val3
;
10081 * Catch errors reported by us and routines below us, and return NULL
10084 if (setjmp(cstate
->top_ctx
))
10087 newoff_sio
= cstate
->off_sio
;
10088 newoff_opc
= cstate
->off_opc
;
10089 newoff_dpc
= cstate
->off_dpc
;
10090 newoff_sls
= cstate
->off_sls
;
10091 switch (mtp3field
) {
10094 newoff_sio
+= 3; /* offset for MTP2_HSL */
10098 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10099 bpf_error(cstate
, "'sio' supported only on SS7");
10100 /* sio coded on 1 byte so max value 255 */
10102 bpf_error(cstate
, "sio value %u too big; max value = 255",
10104 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
10105 jtype
, reverse
, jvalue
);
10113 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10114 bpf_error(cstate
, "'opc' supported only on SS7");
10115 /* opc coded on 14 bits so max value 16383 */
10116 if (jvalue
> 16383)
10117 bpf_error(cstate
, "opc value %u too big; max value = 16383",
10119 /* the following instructions are made to convert jvalue
10120 * to the form used to write opc in an ss7 message*/
10121 val1
= jvalue
& 0x00003c00;
10123 val2
= jvalue
& 0x000003fc;
10125 val3
= jvalue
& 0x00000003;
10127 jvalue
= val1
+ val2
+ val3
;
10128 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
10129 jtype
, reverse
, jvalue
);
10137 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10138 bpf_error(cstate
, "'dpc' supported only on SS7");
10139 /* dpc coded on 14 bits so max value 16383 */
10140 if (jvalue
> 16383)
10141 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
10143 /* the following instructions are made to convert jvalue
10144 * to the forme used to write dpc in an ss7 message*/
10145 val1
= jvalue
& 0x000000ff;
10147 val2
= jvalue
& 0x00003f00;
10149 jvalue
= val1
+ val2
;
10150 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
10151 jtype
, reverse
, jvalue
);
10159 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10160 bpf_error(cstate
, "'sls' supported only on SS7");
10161 /* sls coded on 4 bits so max value 15 */
10163 bpf_error(cstate
, "sls value %u too big; max value = 15",
10165 /* the following instruction is made to convert jvalue
10166 * to the forme used to write sls in an ss7 message*/
10167 jvalue
= jvalue
<< 4;
10168 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
10169 jtype
, reverse
, jvalue
);
10178 static struct block
*
10179 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10184 * Q.2931 signalling protocol messages for handling virtual circuits
10185 * establishment and teardown
10190 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10193 case A_CALLPROCEED
:
10194 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10198 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10202 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10206 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10209 case A_RELEASE_DONE
:
10210 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10220 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10222 struct block
*b0
, *b1
;
10225 * Catch errors reported by us and routines below us, and return NULL
10228 if (setjmp(cstate
->top_ctx
))
10234 if (!cstate
->is_atm
)
10235 bpf_error(cstate
, "'oam' supported only on raw ATM");
10237 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10238 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10240 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10245 if (!cstate
->is_atm
)
10246 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10248 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10249 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10251 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10257 * Get Q.2931 signalling messages for switched
10258 * virtual connection
10260 if (!cstate
->is_atm
)
10261 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10262 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10263 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10265 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10267 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10269 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10271 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10273 b0
= gen_atmtype_sc(cstate
);
10277 case A_METACONNECT
:
10278 if (!cstate
->is_atm
)
10279 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10280 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10281 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10283 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10285 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10287 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10289 b0
= gen_atmtype_metac(cstate
);