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>
54 #include "ethertype.h"
58 #include "ieee80211.h"
60 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
66 #include "diag-control.h"
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
77 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
89 uint16_t u6_addr16
[8];
90 uint32_t u6_addr32
[4];
92 #define s6_addr in6_u.u6_addr8
93 #define s6_addr16 in6_u.u6_addr16
94 #define s6_addr32 in6_u.u6_addr32
95 #define s6_addr64 in6_u.u6_addr64
98 typedef unsigned short sa_family_t
;
100 #define __SOCKADDR_COMMON(sa_prefix) \
101 sa_family_t sa_prefix##family
103 /* Ditto, for IPv6. */
106 __SOCKADDR_COMMON (sin6_
);
107 uint16_t sin6_port
; /* Transport layer port # */
108 uint32_t sin6_flowinfo
; /* IPv6 flow information */
109 struct in6_addr sin6_addr
; /* IPv6 address */
112 #ifndef EAI_ADDRFAMILY
114 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
115 int ai_family
; /* PF_xxx */
116 int ai_socktype
; /* SOCK_xxx */
117 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
118 size_t ai_addrlen
; /* length of ai_addr */
119 char *ai_canonname
; /* canonical name for hostname */
120 struct sockaddr
*ai_addr
; /* binary address */
121 struct addrinfo
*ai_next
; /* next structure in linked list */
123 #endif /* EAI_ADDRFAMILY */
124 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
127 #include <netdb.h> /* for "struct addrinfo" */
129 #include <pcap/namedb.h>
131 #include "nametoaddr.h"
133 #define ETHERMTU 1500
135 #ifndef IPPROTO_HOPOPTS
136 #define IPPROTO_HOPOPTS 0
138 #ifndef IPPROTO_ROUTING
139 #define IPPROTO_ROUTING 43
141 #ifndef IPPROTO_FRAGMENT
142 #define IPPROTO_FRAGMENT 44
144 #ifndef IPPROTO_DSTOPTS
145 #define IPPROTO_DSTOPTS 60
148 #define IPPROTO_SCTP 132
151 #define GENEVE_PORT 6081
153 #ifdef HAVE_OS_PROTO_H
154 #include "os-proto.h"
157 #define JMP(c) ((c)|BPF_JMP|BPF_K)
160 * "Push" the current value of the link-layer header type and link-layer
161 * header offset onto a "stack", and set a new value. (It's not a
162 * full-blown stack; we keep only the top two items.)
164 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
166 (cs)->prevlinktype = (cs)->linktype; \
167 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
168 (cs)->linktype = (new_linktype); \
169 (cs)->off_linkhdr.is_variable = (new_is_variable); \
170 (cs)->off_linkhdr.constant_part = (new_constant_part); \
171 (cs)->off_linkhdr.reg = (new_reg); \
172 (cs)->is_geneve = 0; \
176 * Offset "not set" value.
178 #define OFFSET_NOT_SET 0xffffffffU
181 * Absolute offsets, which are offsets from the beginning of the raw
182 * packet data, are, in the general case, the sum of a variable value
183 * and a constant value; the variable value may be absent, in which
184 * case the offset is only the constant value, and the constant value
185 * may be zero, in which case the offset is only the variable value.
187 * bpf_abs_offset is a structure containing all that information:
189 * is_variable is 1 if there's a variable part.
191 * constant_part is the constant part of the value, possibly zero;
193 * if is_variable is 1, reg is the register number for a register
194 * containing the variable value if the register has been assigned,
204 * Value passed to gen_load_a() to indicate what the offset argument
205 * is relative to the beginning of.
208 OR_PACKET
, /* full packet data */
209 OR_LINKHDR
, /* link-layer header */
210 OR_PREVLINKHDR
, /* previous link-layer header */
211 OR_LLC
, /* 802.2 LLC header */
212 OR_PREVMPLSHDR
, /* previous MPLS header */
213 OR_LINKTYPE
, /* link-layer type */
214 OR_LINKPL
, /* link-layer payload */
215 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
216 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
217 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
221 * We divvy out chunks of memory rather than call malloc each time so
222 * we don't have to worry about leaking memory. It's probably
223 * not a big deal if all this memory was wasted but if this ever
224 * goes into a library that would probably not be a good idea.
226 * XXX - this *is* in a library....
229 #define CHUNK0SIZE 1024
236 * A chunk can store any of:
237 * - a string (guaranteed alignment 1 but present for completeness)
241 * For this simple allocator every allocated chunk gets rounded up to the
242 * alignment needed for any chunk.
253 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
255 /* Code generator state */
257 struct _compiler_state
{
268 int outermostlinktype
;
273 /* Hack for handling VLAN and MPLS stacks. */
274 u_int label_stack_depth
;
275 u_int vlan_stack_depth
;
281 * As errors are handled by a longjmp, anything allocated must
282 * be freed in the longjmp handler, so it must be reachable
285 * One thing that's allocated is the result of pcap_nametoaddrinfo();
286 * it must be freed with freeaddrinfo(). This variable points to
287 * any addrinfo structure that would need to be freed.
292 * Another thing that's allocated is the result of pcap_ether_aton();
293 * it must be freed with free(). This variable points to any
294 * address that would need to be freed.
299 * Various code constructs need to know the layout of the packet.
300 * These values give the necessary offsets from the beginning
301 * of the packet data.
305 * Absolute offset of the beginning of the link-layer header.
307 bpf_abs_offset off_linkhdr
;
310 * If we're checking a link-layer header for a packet encapsulated
311 * in another protocol layer, this is the equivalent information
312 * for the previous layers' link-layer header from the beginning
313 * of the raw packet data.
315 bpf_abs_offset off_prevlinkhdr
;
318 * This is the equivalent information for the outermost layers'
321 bpf_abs_offset off_outermostlinkhdr
;
324 * Absolute offset of the beginning of the link-layer payload.
326 bpf_abs_offset off_linkpl
;
329 * "off_linktype" is the offset to information in the link-layer
330 * header giving the packet type. This is an absolute offset
331 * from the beginning of the packet.
333 * For Ethernet, it's the offset of the Ethernet type field; this
334 * means that it must have a value that skips VLAN tags.
336 * For link-layer types that always use 802.2 headers, it's the
337 * offset of the LLC header; this means that it must have a value
338 * that skips VLAN tags.
340 * For PPP, it's the offset of the PPP type field.
342 * For Cisco HDLC, it's the offset of the CHDLC type field.
344 * For BSD loopback, it's the offset of the AF_ value.
346 * For Linux cooked sockets, it's the offset of the type field.
348 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
349 * encapsulation, in which case, IP is assumed.
351 bpf_abs_offset off_linktype
;
354 * TRUE if the link layer includes an ATM pseudo-header.
359 * TRUE if "geneve" appeared in the filter; it causes us to
360 * generate code that checks for a Geneve header and assume
361 * that later filters apply to the encapsulated payload.
366 * TRUE if we need variable length part of VLAN offset
368 int is_vlan_vloffset
;
371 * These are offsets for the ATM pseudo-header.
378 * These are offsets for the MTP2 fields.
384 * These are offsets for the MTP3 fields.
392 * This is the offset of the first byte after the ATM pseudo_header,
393 * or -1 if there is no ATM pseudo-header.
398 * These are offsets to the beginning of the network-layer header.
399 * They are relative to the beginning of the link-layer payload
400 * (i.e., they don't include off_linkhdr.constant_part or
401 * off_linkpl.constant_part).
403 * If the link layer never uses 802.2 LLC:
405 * "off_nl" and "off_nl_nosnap" are the same.
407 * If the link layer always uses 802.2 LLC:
409 * "off_nl" is the offset if there's a SNAP header following
412 * "off_nl_nosnap" is the offset if there's no SNAP header.
414 * If the link layer is Ethernet:
416 * "off_nl" is the offset if the packet is an Ethernet II packet
417 * (we assume no 802.3+802.2+SNAP);
419 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
420 * with an 802.2 header following it.
426 * Here we handle simple allocation of the scratch registers.
427 * If too many registers are alloc'd, the allocator punts.
429 int regused
[BPF_MEMWORDS
];
435 struct chunk chunks
[NCHUNKS
];
440 * For use by routines outside this file.
444 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
449 * If we've already set an error, don't override it.
450 * The lexical analyzer reports some errors by setting
451 * the error and then returning a LEX_ERROR token, which
452 * is not recognized by any grammar rule, and thus forces
453 * the parse to stop. We don't want the error reported
454 * by the lexical analyzer to be overwritten by the syntax
457 if (!cstate
->error_set
) {
459 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
462 cstate
->error_set
= 1;
467 * For use *ONLY* in routines in this file.
469 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
470 PCAP_PRINTFLIKE(2, 3);
473 static void PCAP_NORETURN
474 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
479 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
482 longjmp(cstate
->top_ctx
, 1);
489 static int init_linktype(compiler_state_t
*, pcap_t
*);
491 static void init_regs(compiler_state_t
*);
492 static int alloc_reg(compiler_state_t
*);
493 static void free_reg(compiler_state_t
*, int);
495 static void initchunks(compiler_state_t
*cstate
);
496 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
497 static void *newchunk(compiler_state_t
*cstate
, size_t);
498 static void freechunks(compiler_state_t
*cstate
);
499 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
500 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
501 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
502 static inline void syntax(compiler_state_t
*cstate
);
504 static void backpatch(struct block
*, struct block
*);
505 static void merge(struct block
*, struct block
*);
506 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
508 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
510 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
512 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
514 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
516 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
517 u_int
, bpf_u_int32
, bpf_u_int32
);
518 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
519 u_int
, const u_char
*);
520 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
521 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
522 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
524 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
526 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
527 static struct block
*gen_uncond(compiler_state_t
*, int);
528 static inline struct block
*gen_true(compiler_state_t
*);
529 static inline struct block
*gen_false(compiler_state_t
*);
530 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
531 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
532 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
533 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
534 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
535 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
536 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
537 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
538 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
539 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
541 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
542 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
543 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
544 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
545 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
546 int, bpf_u_int32
, u_int
, u_int
);
548 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
549 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
551 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
552 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
553 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
554 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
555 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
556 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
557 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
558 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
559 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
562 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
563 struct in6_addr
*, int, int, int);
566 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
567 struct addrinfo
*, int, int);
569 static struct block
*gen_ipfrag(compiler_state_t
*);
570 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
571 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
573 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
574 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
576 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
577 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
578 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
580 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
581 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
582 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
583 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
585 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
586 static int lookup_proto(compiler_state_t
*, const char *, int);
587 #if !defined(NO_PROTOCHAIN)
588 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
589 #endif /* !defined(NO_PROTOCHAIN) */
590 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
591 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
592 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
593 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
594 static struct block
*gen_len(compiler_state_t
*, int, int);
595 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
596 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
598 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
599 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
600 bpf_u_int32
, int, int);
601 static struct block
*gen_atmtype_llc(compiler_state_t
*);
602 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
605 initchunks(compiler_state_t
*cstate
)
609 for (i
= 0; i
< NCHUNKS
; i
++) {
610 cstate
->chunks
[i
].n_left
= 0;
611 cstate
->chunks
[i
].m
= NULL
;
613 cstate
->cur_chunk
= 0;
617 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
623 /* Round up to chunk alignment. */
624 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
626 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
627 if (n
> cp
->n_left
) {
629 k
= ++cstate
->cur_chunk
;
631 bpf_set_error(cstate
, "out of memory");
634 size
= CHUNK0SIZE
<< k
;
635 cp
->m
= (void *)malloc(size
);
637 bpf_set_error(cstate
, "out of memory");
640 memset((char *)cp
->m
, 0, size
);
643 bpf_set_error(cstate
, "out of memory");
648 return (void *)((char *)cp
->m
+ cp
->n_left
);
652 newchunk(compiler_state_t
*cstate
, size_t n
)
656 p
= newchunk_nolongjmp(cstate
, n
);
658 longjmp(cstate
->top_ctx
, 1);
665 freechunks(compiler_state_t
*cstate
)
669 for (i
= 0; i
< NCHUNKS
; ++i
)
670 if (cstate
->chunks
[i
].m
!= NULL
)
671 free(cstate
->chunks
[i
].m
);
675 * A strdup whose allocations are freed after code generation is over.
676 * This is used by the lexical analyzer, so it can't longjmp; it just
677 * returns NULL on an allocation error, and the callers must check
681 sdup(compiler_state_t
*cstate
, const char *s
)
683 size_t n
= strlen(s
) + 1;
684 char *cp
= newchunk_nolongjmp(cstate
, n
);
688 pcap_strlcpy(cp
, s
, n
);
692 static inline struct block
*
693 new_block(compiler_state_t
*cstate
, int code
)
697 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
704 static inline struct slist
*
705 new_stmt(compiler_state_t
*cstate
, int code
)
709 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
715 static struct block
*
716 gen_retblk(compiler_state_t
*cstate
, int v
)
718 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
724 static inline PCAP_NORETURN_DEF
void
725 syntax(compiler_state_t
*cstate
)
727 bpf_error(cstate
, "syntax error in filter expression");
731 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
732 const char *buf
, int optimize
, bpf_u_int32 mask
)
737 compiler_state_t cstate
;
738 const char * volatile xbuf
= buf
;
739 yyscan_t scanner
= NULL
;
740 volatile YY_BUFFER_STATE in_buffer
= NULL
;
745 * If this pcap_t hasn't been activated, it doesn't have a
746 * link-layer type, so we can't use it.
749 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
750 "not-yet-activated pcap_t passed to pcap_compile");
763 * If the device on which we're capturing need to be notified
764 * that a new filter is being compiled, do so.
766 * This allows them to save a copy of it, in case, for example,
767 * they're implementing a form of remote packet capture, and
768 * want the remote machine to filter out the packets in which
769 * it's sending the packets it's captured.
771 * XXX - the fact that we happen to be compiling a filter
772 * doesn't necessarily mean we'll be installing it as the
773 * filter for this pcap_t; we might be running it from userland
774 * on captured packets to do packet classification. We really
775 * need a better way of handling this, but this is all that
776 * the WinPcap remote capture code did.
778 if (p
->save_current_filter_op
!= NULL
)
779 (p
->save_current_filter_op
)(p
, buf
);
783 cstate
.no_optimize
= 0;
788 cstate
.ic
.root
= NULL
;
789 cstate
.ic
.cur_mark
= 0;
791 cstate
.error_set
= 0;
794 cstate
.netmask
= mask
;
796 cstate
.snaplen
= pcap_snapshot(p
);
797 if (cstate
.snaplen
== 0) {
798 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
799 "snaplen of 0 rejects all packets");
804 if (pcap_lex_init(&scanner
) != 0) {
805 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
806 errno
, "can't initialize scanner");
810 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
813 * Associate the compiler state with the lexical analyzer
816 pcap_set_extra(&cstate
, scanner
);
818 if (init_linktype(&cstate
, p
) == -1) {
822 if (pcap_parse(scanner
, &cstate
) != 0) {
824 if (cstate
.ai
!= NULL
)
825 freeaddrinfo(cstate
.ai
);
827 if (cstate
.e
!= NULL
)
833 if (cstate
.ic
.root
== NULL
) {
835 * Catch errors reported by gen_retblk().
837 if (setjmp(cstate
.top_ctx
)) {
841 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
844 if (optimize
&& !cstate
.no_optimize
) {
845 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
850 if (cstate
.ic
.root
== NULL
||
851 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
852 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
853 "expression rejects all packets");
858 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
859 cstate
.ic
.root
, &len
, p
->errbuf
);
860 if (program
->bf_insns
== NULL
) {
865 program
->bf_len
= len
;
867 rc
= 0; /* We're all okay */
871 * Clean up everything for the lexical analyzer.
873 if (in_buffer
!= NULL
)
874 pcap__delete_buffer(in_buffer
, scanner
);
876 pcap_lex_destroy(scanner
);
879 * Clean up our own allocated memory.
887 * entry point for using the compiler with no pcap open
888 * pass in all the stuff that is needed explicitly instead.
891 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
892 struct bpf_program
*program
,
893 const char *buf
, int optimize
, bpf_u_int32 mask
)
898 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
901 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
907 * Clean up a "struct bpf_program" by freeing all the memory allocated
911 pcap_freecode(struct bpf_program
*program
)
914 if (program
->bf_insns
!= NULL
) {
915 free((char *)program
->bf_insns
);
916 program
->bf_insns
= NULL
;
921 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
922 * which of the jt and jf fields has been resolved and which is a pointer
923 * back to another unresolved block (or nil). At least one of the fields
924 * in each block is already resolved.
927 backpatch(struct block
*list
, struct block
*target
)
944 * Merge the lists in b0 and b1, using the 'sense' field to indicate
945 * which of jt and jf is the link.
948 merge(struct block
*b0
, struct block
*b1
)
950 register struct block
**p
= &b0
;
952 /* Find end of list. */
954 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
956 /* Concatenate the lists. */
961 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
963 struct block
*ppi_dlt_check
;
966 * Catch errors reported by us and routines below us, and return -1
969 if (setjmp(cstate
->top_ctx
))
973 * Insert before the statements of the first (root) block any
974 * statements needed to load the lengths of any variable-length
975 * headers into registers.
977 * XXX - a fancier strategy would be to insert those before the
978 * statements of all blocks that use those lengths and that
979 * have no predecessors that use them, so that we only compute
980 * the lengths if we need them. There might be even better
981 * approaches than that.
983 * However, those strategies would be more complicated, and
984 * as we don't generate code to compute a length if the
985 * program has no tests that use the length, and as most
986 * tests will probably use those lengths, we would just
987 * postpone computing the lengths so that it's not done
988 * for tests that fail early, and it's not clear that's
991 insert_compute_vloffsets(cstate
, p
->head
);
994 * For DLT_PPI captures, generate a check of the per-packet
995 * DLT value to make sure it's DLT_IEEE802_11.
997 * XXX - TurboCap cards use DLT_PPI for Ethernet.
998 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
999 * with appropriate Ethernet information and use that rather
1000 * than using something such as DLT_PPI where you don't know
1001 * the link-layer header type until runtime, which, in the
1002 * general case, would force us to generate both Ethernet *and*
1003 * 802.11 code (*and* anything else for which PPI is used)
1004 * and choose between them early in the BPF program?
1006 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
1007 if (ppi_dlt_check
!= NULL
)
1008 gen_and(ppi_dlt_check
, p
);
1010 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
1011 p
->sense
= !p
->sense
;
1012 backpatch(p
, gen_retblk(cstate
, 0));
1013 cstate
->ic
.root
= p
->head
;
1018 gen_and(struct block
*b0
, struct block
*b1
)
1020 backpatch(b0
, b1
->head
);
1021 b0
->sense
= !b0
->sense
;
1022 b1
->sense
= !b1
->sense
;
1024 b1
->sense
= !b1
->sense
;
1025 b1
->head
= b0
->head
;
1029 gen_or(struct block
*b0
, struct block
*b1
)
1031 b0
->sense
= !b0
->sense
;
1032 backpatch(b0
, b1
->head
);
1033 b0
->sense
= !b0
->sense
;
1035 b1
->head
= b0
->head
;
1039 gen_not(struct block
*b
)
1041 b
->sense
= !b
->sense
;
1044 static struct block
*
1045 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1046 u_int size
, bpf_u_int32 v
)
1048 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1051 static struct block
*
1052 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1053 u_int size
, bpf_u_int32 v
)
1055 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1058 static struct block
*
1059 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1060 u_int size
, bpf_u_int32 v
)
1062 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1065 static struct block
*
1066 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1067 u_int size
, bpf_u_int32 v
)
1069 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1072 static struct block
*
1073 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1074 u_int size
, bpf_u_int32 v
)
1076 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1079 static struct block
*
1080 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1081 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1083 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1086 static struct block
*
1087 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1088 u_int size
, const u_char
*v
)
1090 register struct block
*b
, *tmp
;
1094 register const u_char
*p
= &v
[size
- 4];
1096 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1104 register const u_char
*p
= &v
[size
- 2];
1106 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1114 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1123 * AND the field of size "size" at offset "offset" relative to the header
1124 * specified by "offrel" with "mask", and compare it with the value "v"
1125 * with the test specified by "jtype"; if "reverse" is true, the test
1126 * should test the opposite of "jtype".
1128 static struct block
*
1129 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1130 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1133 struct slist
*s
, *s2
;
1136 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1138 if (mask
!= 0xffffffff) {
1139 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1144 b
= new_block(cstate
, JMP(jtype
));
1147 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1153 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1155 cstate
->pcap_fddipad
= p
->fddipad
;
1158 * We start out with only one link-layer header.
1160 cstate
->outermostlinktype
= pcap_datalink(p
);
1161 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1162 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1163 cstate
->off_outermostlinkhdr
.reg
= -1;
1165 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1166 cstate
->off_prevlinkhdr
.constant_part
= 0;
1167 cstate
->off_prevlinkhdr
.is_variable
= 0;
1168 cstate
->off_prevlinkhdr
.reg
= -1;
1170 cstate
->linktype
= cstate
->outermostlinktype
;
1171 cstate
->off_linkhdr
.constant_part
= 0;
1172 cstate
->off_linkhdr
.is_variable
= 0;
1173 cstate
->off_linkhdr
.reg
= -1;
1178 cstate
->off_linkpl
.constant_part
= 0;
1179 cstate
->off_linkpl
.is_variable
= 0;
1180 cstate
->off_linkpl
.reg
= -1;
1182 cstate
->off_linktype
.constant_part
= 0;
1183 cstate
->off_linktype
.is_variable
= 0;
1184 cstate
->off_linktype
.reg
= -1;
1187 * Assume it's not raw ATM with a pseudo-header, for now.
1190 cstate
->off_vpi
= OFFSET_NOT_SET
;
1191 cstate
->off_vci
= OFFSET_NOT_SET
;
1192 cstate
->off_proto
= OFFSET_NOT_SET
;
1193 cstate
->off_payload
= OFFSET_NOT_SET
;
1198 cstate
->is_geneve
= 0;
1201 * No variable length VLAN offset by default
1203 cstate
->is_vlan_vloffset
= 0;
1206 * And assume we're not doing SS7.
1208 cstate
->off_li
= OFFSET_NOT_SET
;
1209 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1210 cstate
->off_sio
= OFFSET_NOT_SET
;
1211 cstate
->off_opc
= OFFSET_NOT_SET
;
1212 cstate
->off_dpc
= OFFSET_NOT_SET
;
1213 cstate
->off_sls
= OFFSET_NOT_SET
;
1215 cstate
->label_stack_depth
= 0;
1216 cstate
->vlan_stack_depth
= 0;
1218 switch (cstate
->linktype
) {
1221 cstate
->off_linktype
.constant_part
= 2;
1222 cstate
->off_linkpl
.constant_part
= 6;
1223 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1224 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1227 case DLT_ARCNET_LINUX
:
1228 cstate
->off_linktype
.constant_part
= 4;
1229 cstate
->off_linkpl
.constant_part
= 8;
1230 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1231 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1235 cstate
->off_linktype
.constant_part
= 12;
1236 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1237 cstate
->off_nl
= 0; /* Ethernet II */
1238 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1243 * SLIP doesn't have a link level type. The 16 byte
1244 * header is hacked into our SLIP driver.
1246 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1247 cstate
->off_linkpl
.constant_part
= 16;
1249 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1252 case DLT_SLIP_BSDOS
:
1253 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1254 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1256 cstate
->off_linkpl
.constant_part
= 24;
1258 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1263 cstate
->off_linktype
.constant_part
= 0;
1264 cstate
->off_linkpl
.constant_part
= 4;
1266 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1270 cstate
->off_linktype
.constant_part
= 0;
1271 cstate
->off_linkpl
.constant_part
= 12;
1273 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1278 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1279 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1280 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1281 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1282 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1284 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1289 * This does no include the Ethernet header, and
1290 * only covers session state.
1292 cstate
->off_linktype
.constant_part
= 6;
1293 cstate
->off_linkpl
.constant_part
= 8;
1295 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1299 cstate
->off_linktype
.constant_part
= 5;
1300 cstate
->off_linkpl
.constant_part
= 24;
1302 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1307 * FDDI doesn't really have a link-level type field.
1308 * We set "off_linktype" to the offset of the LLC header.
1310 * To check for Ethernet types, we assume that SSAP = SNAP
1311 * is being used and pick out the encapsulated Ethernet type.
1312 * XXX - should we generate code to check for SNAP?
1314 cstate
->off_linktype
.constant_part
= 13;
1315 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1316 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1317 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1318 cstate
->off_nl
= 8; /* 802.2+SNAP */
1319 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1324 * Token Ring doesn't really have a link-level type field.
1325 * We set "off_linktype" to the offset of the LLC header.
1327 * To check for Ethernet types, we assume that SSAP = SNAP
1328 * is being used and pick out the encapsulated Ethernet type.
1329 * XXX - should we generate code to check for SNAP?
1331 * XXX - the header is actually variable-length.
1332 * Some various Linux patched versions gave 38
1333 * as "off_linktype" and 40 as "off_nl"; however,
1334 * if a token ring packet has *no* routing
1335 * information, i.e. is not source-routed, the correct
1336 * values are 20 and 22, as they are in the vanilla code.
1338 * A packet is source-routed iff the uppermost bit
1339 * of the first byte of the source address, at an
1340 * offset of 8, has the uppermost bit set. If the
1341 * packet is source-routed, the total number of bytes
1342 * of routing information is 2 plus bits 0x1F00 of
1343 * the 16-bit value at an offset of 14 (shifted right
1344 * 8 - figure out which byte that is).
1346 cstate
->off_linktype
.constant_part
= 14;
1347 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1348 cstate
->off_nl
= 8; /* 802.2+SNAP */
1349 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1352 case DLT_PRISM_HEADER
:
1353 case DLT_IEEE802_11_RADIO_AVS
:
1354 case DLT_IEEE802_11_RADIO
:
1355 cstate
->off_linkhdr
.is_variable
= 1;
1356 /* Fall through, 802.11 doesn't have a variable link
1357 * prefix but is otherwise the same. */
1360 case DLT_IEEE802_11
:
1362 * 802.11 doesn't really have a link-level type field.
1363 * We set "off_linktype.constant_part" to the offset of
1366 * To check for Ethernet types, we assume that SSAP = SNAP
1367 * is being used and pick out the encapsulated Ethernet type.
1368 * XXX - should we generate code to check for SNAP?
1370 * We also handle variable-length radio headers here.
1371 * The Prism header is in theory variable-length, but in
1372 * practice it's always 144 bytes long. However, some
1373 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1374 * sometimes or always supply an AVS header, so we
1375 * have to check whether the radio header is a Prism
1376 * header or an AVS header, so, in practice, it's
1379 cstate
->off_linktype
.constant_part
= 24;
1380 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1381 cstate
->off_linkpl
.is_variable
= 1;
1382 cstate
->off_nl
= 8; /* 802.2+SNAP */
1383 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1388 * At the moment we treat PPI the same way that we treat
1389 * normal Radiotap encoded packets. The difference is in
1390 * the function that generates the code at the beginning
1391 * to compute the header length. Since this code generator
1392 * of PPI supports bare 802.11 encapsulation only (i.e.
1393 * the encapsulated DLT should be DLT_IEEE802_11) we
1394 * generate code to check for this too.
1396 cstate
->off_linktype
.constant_part
= 24;
1397 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1398 cstate
->off_linkpl
.is_variable
= 1;
1399 cstate
->off_linkhdr
.is_variable
= 1;
1400 cstate
->off_nl
= 8; /* 802.2+SNAP */
1401 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1404 case DLT_ATM_RFC1483
:
1405 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1407 * assume routed, non-ISO PDUs
1408 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1410 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1411 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1412 * latter would presumably be treated the way PPPoE
1413 * should be, so you can do "pppoe and udp port 2049"
1414 * or "pppoa and tcp port 80" and have it check for
1415 * PPPo{A,E} and a PPP protocol of IP and....
1417 cstate
->off_linktype
.constant_part
= 0;
1418 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1419 cstate
->off_nl
= 8; /* 802.2+SNAP */
1420 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1425 * Full Frontal ATM; you get AALn PDUs with an ATM
1429 cstate
->off_vpi
= SUNATM_VPI_POS
;
1430 cstate
->off_vci
= SUNATM_VCI_POS
;
1431 cstate
->off_proto
= PROTO_POS
;
1432 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1433 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1434 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1435 cstate
->off_nl
= 8; /* 802.2+SNAP */
1436 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1442 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1443 cstate
->off_linkpl
.constant_part
= 0;
1445 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1448 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1449 cstate
->off_linktype
.constant_part
= 14;
1450 cstate
->off_linkpl
.constant_part
= 16;
1452 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1455 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1456 cstate
->off_linktype
.constant_part
= 0;
1457 cstate
->off_linkpl
.constant_part
= 20;
1459 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1464 * LocalTalk does have a 1-byte type field in the LLAP header,
1465 * but really it just indicates whether there is a "short" or
1466 * "long" DDP packet following.
1468 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1469 cstate
->off_linkpl
.constant_part
= 0;
1471 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1474 case DLT_IP_OVER_FC
:
1476 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1477 * link-level type field. We set "off_linktype" to the
1478 * offset of the LLC header.
1480 * To check for Ethernet types, we assume that SSAP = SNAP
1481 * is being used and pick out the encapsulated Ethernet type.
1482 * XXX - should we generate code to check for SNAP? RFC
1483 * 2625 says SNAP should be used.
1485 cstate
->off_linktype
.constant_part
= 16;
1486 cstate
->off_linkpl
.constant_part
= 16;
1487 cstate
->off_nl
= 8; /* 802.2+SNAP */
1488 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1493 * XXX - we should set this to handle SNAP-encapsulated
1494 * frames (NLPID of 0x80).
1496 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1497 cstate
->off_linkpl
.constant_part
= 0;
1499 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1503 * the only BPF-interesting FRF.16 frames are non-control frames;
1504 * Frame Relay has a variable length link-layer
1505 * so lets start with offset 4 for now and increments later on (FIXME);
1508 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1509 cstate
->off_linkpl
.constant_part
= 0;
1511 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1514 case DLT_APPLE_IP_OVER_IEEE1394
:
1515 cstate
->off_linktype
.constant_part
= 16;
1516 cstate
->off_linkpl
.constant_part
= 18;
1518 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1521 case DLT_SYMANTEC_FIREWALL
:
1522 cstate
->off_linktype
.constant_part
= 6;
1523 cstate
->off_linkpl
.constant_part
= 44;
1524 cstate
->off_nl
= 0; /* Ethernet II */
1525 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1529 cstate
->off_linktype
.constant_part
= 0;
1530 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1531 cstate
->off_linkpl
.is_variable
= 1;
1533 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1536 case DLT_JUNIPER_MFR
:
1537 case DLT_JUNIPER_MLFR
:
1538 case DLT_JUNIPER_MLPPP
:
1539 case DLT_JUNIPER_PPP
:
1540 case DLT_JUNIPER_CHDLC
:
1541 case DLT_JUNIPER_FRELAY
:
1542 cstate
->off_linktype
.constant_part
= 4;
1543 cstate
->off_linkpl
.constant_part
= 4;
1545 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1548 case DLT_JUNIPER_ATM1
:
1549 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1550 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1552 cstate
->off_nl_nosnap
= 10;
1555 case DLT_JUNIPER_ATM2
:
1556 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1557 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1559 cstate
->off_nl_nosnap
= 10;
1562 /* frames captured on a Juniper PPPoE service PIC
1563 * contain raw ethernet frames */
1564 case DLT_JUNIPER_PPPOE
:
1565 case DLT_JUNIPER_ETHER
:
1566 cstate
->off_linkpl
.constant_part
= 14;
1567 cstate
->off_linktype
.constant_part
= 16;
1568 cstate
->off_nl
= 18; /* Ethernet II */
1569 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1572 case DLT_JUNIPER_PPPOE_ATM
:
1573 cstate
->off_linktype
.constant_part
= 4;
1574 cstate
->off_linkpl
.constant_part
= 6;
1576 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1579 case DLT_JUNIPER_GGSN
:
1580 cstate
->off_linktype
.constant_part
= 6;
1581 cstate
->off_linkpl
.constant_part
= 12;
1583 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1586 case DLT_JUNIPER_ES
:
1587 cstate
->off_linktype
.constant_part
= 6;
1588 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1589 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1590 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1593 case DLT_JUNIPER_MONITOR
:
1594 cstate
->off_linktype
.constant_part
= 12;
1595 cstate
->off_linkpl
.constant_part
= 12;
1596 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1597 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1600 case DLT_BACNET_MS_TP
:
1601 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1602 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1603 cstate
->off_nl
= OFFSET_NOT_SET
;
1604 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1607 case DLT_JUNIPER_SERVICES
:
1608 cstate
->off_linktype
.constant_part
= 12;
1609 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1610 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1611 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1614 case DLT_JUNIPER_VP
:
1615 cstate
->off_linktype
.constant_part
= 18;
1616 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1617 cstate
->off_nl
= OFFSET_NOT_SET
;
1618 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1621 case DLT_JUNIPER_ST
:
1622 cstate
->off_linktype
.constant_part
= 18;
1623 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1624 cstate
->off_nl
= OFFSET_NOT_SET
;
1625 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1628 case DLT_JUNIPER_ISM
:
1629 cstate
->off_linktype
.constant_part
= 8;
1630 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1631 cstate
->off_nl
= OFFSET_NOT_SET
;
1632 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1635 case DLT_JUNIPER_VS
:
1636 case DLT_JUNIPER_SRX_E2E
:
1637 case DLT_JUNIPER_FIBRECHANNEL
:
1638 case DLT_JUNIPER_ATM_CEMIC
:
1639 cstate
->off_linktype
.constant_part
= 8;
1640 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1641 cstate
->off_nl
= OFFSET_NOT_SET
;
1642 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1647 cstate
->off_li_hsl
= 4;
1648 cstate
->off_sio
= 3;
1649 cstate
->off_opc
= 4;
1650 cstate
->off_dpc
= 4;
1651 cstate
->off_sls
= 7;
1652 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1653 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1654 cstate
->off_nl
= OFFSET_NOT_SET
;
1655 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1658 case DLT_MTP2_WITH_PHDR
:
1660 cstate
->off_li_hsl
= 8;
1661 cstate
->off_sio
= 7;
1662 cstate
->off_opc
= 8;
1663 cstate
->off_dpc
= 8;
1664 cstate
->off_sls
= 11;
1665 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1666 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1667 cstate
->off_nl
= OFFSET_NOT_SET
;
1668 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1672 cstate
->off_li
= 22;
1673 cstate
->off_li_hsl
= 24;
1674 cstate
->off_sio
= 23;
1675 cstate
->off_opc
= 24;
1676 cstate
->off_dpc
= 24;
1677 cstate
->off_sls
= 27;
1678 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1679 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1680 cstate
->off_nl
= OFFSET_NOT_SET
;
1681 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1685 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1686 cstate
->off_linkpl
.constant_part
= 4;
1688 cstate
->off_nl_nosnap
= 0;
1693 * Currently, only raw "link[N:M]" filtering is supported.
1695 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1696 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1697 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1698 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1702 cstate
->off_linktype
.constant_part
= 1;
1703 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1705 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1708 case DLT_NETANALYZER
:
1709 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1710 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1711 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1712 cstate
->off_nl
= 0; /* Ethernet II */
1713 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1716 case DLT_NETANALYZER_TRANSPARENT
:
1717 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1718 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1719 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1720 cstate
->off_nl
= 0; /* Ethernet II */
1721 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1726 * For values in the range in which we've assigned new
1727 * DLT_ values, only raw "link[N:M]" filtering is supported.
1729 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1730 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1731 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1732 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1733 cstate
->off_nl
= OFFSET_NOT_SET
;
1734 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1736 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1737 cstate
->linktype
, DLT_MATCHING_MIN
, DLT_MATCHING_MAX
);
1743 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1748 * Load a value relative to the specified absolute offset.
1750 static struct slist
*
1751 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1752 u_int offset
, u_int size
)
1754 struct slist
*s
, *s2
;
1756 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1759 * If "s" is non-null, it has code to arrange that the X register
1760 * contains the variable part of the absolute offset, so we
1761 * generate a load relative to that, with an offset of
1762 * abs_offset->constant_part + offset.
1764 * Otherwise, we can do an absolute load with an offset of
1765 * abs_offset->constant_part + offset.
1769 * "s" points to a list of statements that puts the
1770 * variable part of the absolute offset into the X register.
1771 * Do an indirect load, to use the X register as an offset.
1773 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1774 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1778 * There is no variable part of the absolute offset, so
1779 * just do an absolute load.
1781 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1782 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1788 * Load a value relative to the beginning of the specified header.
1790 static struct slist
*
1791 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1794 struct slist
*s
, *s2
;
1797 * Squelch warnings from compilers that *don't* assume that
1798 * offrel always has a valid enum value and therefore don't
1799 * assume that we'll always go through one of the case arms.
1801 * If we have a default case, compilers that *do* assume that
1802 * will then complain about the default case code being
1805 * Damned if you do, damned if you don't.
1812 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1817 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1820 case OR_PREVLINKHDR
:
1821 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1825 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1828 case OR_PREVMPLSHDR
:
1829 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1833 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1836 case OR_LINKPL_NOSNAP
:
1837 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1841 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1846 * Load the X register with the length of the IPv4 header
1847 * (plus the offset of the link-layer header, if it's
1848 * preceded by a variable-length header such as a radio
1849 * header), in bytes.
1851 s
= gen_loadx_iphdrlen(cstate
);
1854 * Load the item at {offset of the link-layer payload} +
1855 * {offset, relative to the start of the link-layer
1856 * payload, of the IPv4 header} + {length of the IPv4 header} +
1857 * {specified offset}.
1859 * If the offset of the link-layer payload is variable,
1860 * the variable part of that offset is included in the
1861 * value in the X register, and we include the constant
1862 * part in the offset of the load.
1864 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1865 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1870 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1877 * Generate code to load into the X register the sum of the length of
1878 * the IPv4 header and the variable part of the offset of the link-layer
1881 static struct slist
*
1882 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1884 struct slist
*s
, *s2
;
1886 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1889 * The offset of the link-layer payload has a variable
1890 * part. "s" points to a list of statements that put
1891 * the variable part of that offset into the X register.
1893 * The 4*([k]&0xf) addressing mode can't be used, as we
1894 * don't have a constant offset, so we have to load the
1895 * value in question into the A register and add to it
1896 * the value from the X register.
1898 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1899 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1901 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1904 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1909 * The A register now contains the length of the IP header.
1910 * We need to add to it the variable part of the offset of
1911 * the link-layer payload, which is still in the X
1912 * register, and move the result into the X register.
1914 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1915 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1918 * The offset of the link-layer payload is a constant,
1919 * so no code was generated to load the (nonexistent)
1920 * variable part of that offset.
1922 * This means we can use the 4*([k]&0xf) addressing
1923 * mode. Load the length of the IPv4 header, which
1924 * is at an offset of cstate->off_nl from the beginning of
1925 * the link-layer payload, and thus at an offset of
1926 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1927 * of the raw packet data, using that addressing mode.
1929 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1930 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1936 static struct block
*
1937 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1942 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1944 b
= new_block(cstate
, JMP(BPF_JEQ
));
1950 static inline struct block
*
1951 gen_true(compiler_state_t
*cstate
)
1953 return gen_uncond(cstate
, 1);
1956 static inline struct block
*
1957 gen_false(compiler_state_t
*cstate
)
1959 return gen_uncond(cstate
, 0);
1963 * Byte-swap a 32-bit number.
1964 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1965 * big-endian platforms.)
1967 #define SWAPLONG(y) \
1968 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1971 * Generate code to match a particular packet type.
1973 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1974 * value, if <= ETHERMTU. We use that to determine whether to
1975 * match the type/length field or to check the type/length field for
1976 * a value <= ETHERMTU to see whether it's a type field and then do
1977 * the appropriate test.
1979 static struct block
*
1980 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1982 struct block
*b0
, *b1
;
1988 case LLCSAP_NETBEUI
:
1990 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1991 * so we check the DSAP and SSAP.
1993 * LLCSAP_IP checks for IP-over-802.2, rather
1994 * than IP-over-Ethernet or IP-over-SNAP.
1996 * XXX - should we check both the DSAP and the
1997 * SSAP, like this, or should we check just the
1998 * DSAP, as we do for other types <= ETHERMTU
1999 * (i.e., other SAP values)?
2001 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2003 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2011 * Ethernet_II frames, which are Ethernet
2012 * frames with a frame type of ETHERTYPE_IPX;
2014 * Ethernet_802.3 frames, which are 802.3
2015 * frames (i.e., the type/length field is
2016 * a length field, <= ETHERMTU, rather than
2017 * a type field) with the first two bytes
2018 * after the Ethernet/802.3 header being
2021 * Ethernet_802.2 frames, which are 802.3
2022 * frames with an 802.2 LLC header and
2023 * with the IPX LSAP as the DSAP in the LLC
2026 * Ethernet_SNAP frames, which are 802.3
2027 * frames with an LLC header and a SNAP
2028 * header and with an OUI of 0x000000
2029 * (encapsulated Ethernet) and a protocol
2030 * ID of ETHERTYPE_IPX in the SNAP header.
2032 * XXX - should we generate the same code both
2033 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2037 * This generates code to check both for the
2038 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2040 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2041 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2045 * Now we add code to check for SNAP frames with
2046 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2048 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2052 * Now we generate code to check for 802.3
2053 * frames in general.
2055 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2059 * Now add the check for 802.3 frames before the
2060 * check for Ethernet_802.2 and Ethernet_802.3,
2061 * as those checks should only be done on 802.3
2062 * frames, not on Ethernet frames.
2067 * Now add the check for Ethernet_II frames, and
2068 * do that before checking for the other frame
2071 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2075 case ETHERTYPE_ATALK
:
2076 case ETHERTYPE_AARP
:
2078 * EtherTalk (AppleTalk protocols on Ethernet link
2079 * layer) may use 802.2 encapsulation.
2083 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2084 * we check for an Ethernet type field less than
2085 * 1500, which means it's an 802.3 length field.
2087 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2091 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2092 * SNAP packets with an organization code of
2093 * 0x080007 (Apple, for Appletalk) and a protocol
2094 * type of ETHERTYPE_ATALK (Appletalk).
2096 * 802.2-encapsulated ETHERTYPE_AARP packets are
2097 * SNAP packets with an organization code of
2098 * 0x000000 (encapsulated Ethernet) and a protocol
2099 * type of ETHERTYPE_AARP (Appletalk ARP).
2101 if (ll_proto
== ETHERTYPE_ATALK
)
2102 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2103 else /* ll_proto == ETHERTYPE_AARP */
2104 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2108 * Check for Ethernet encapsulation (Ethertalk
2109 * phase 1?); we just check for the Ethernet
2112 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2118 if (ll_proto
<= ETHERMTU
) {
2120 * This is an LLC SAP value, so the frames
2121 * that match would be 802.2 frames.
2122 * Check that the frame is an 802.2 frame
2123 * (i.e., that the length/type field is
2124 * a length field, <= ETHERMTU) and
2125 * then check the DSAP.
2127 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2129 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2134 * This is an Ethernet type, so compare
2135 * the length/type field with it (if
2136 * the frame is an 802.2 frame, the length
2137 * field will be <= ETHERMTU, and, as
2138 * "ll_proto" is > ETHERMTU, this test
2139 * will fail and the frame won't match,
2140 * which is what we want).
2142 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2147 static struct block
*
2148 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2151 * For DLT_NULL, the link-layer header is a 32-bit word
2152 * containing an AF_ value in *host* byte order, and for
2153 * DLT_ENC, the link-layer header begins with a 32-bit
2154 * word containing an AF_ value in host byte order.
2156 * In addition, if we're reading a saved capture file,
2157 * the host byte order in the capture may not be the
2158 * same as the host byte order on this machine.
2160 * For DLT_LOOP, the link-layer header is a 32-bit
2161 * word containing an AF_ value in *network* byte order.
2163 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2165 * The AF_ value is in host byte order, but the BPF
2166 * interpreter will convert it to network byte order.
2168 * If this is a save file, and it's from a machine
2169 * with the opposite byte order to ours, we byte-swap
2172 * Then we run it through "htonl()", and generate
2173 * code to compare against the result.
2175 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2176 ll_proto
= SWAPLONG(ll_proto
);
2177 ll_proto
= htonl(ll_proto
);
2179 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2183 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2184 * or IPv6 then we have an error.
2186 static struct block
*
2187 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2192 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2195 case ETHERTYPE_IPV6
:
2196 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2203 return gen_false(cstate
);
2207 * Generate code to match a particular packet type.
2209 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2210 * value, if <= ETHERMTU. We use that to determine whether to
2211 * match the type field or to check the type field for the special
2212 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2214 static struct block
*
2215 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2217 struct block
*b0
, *b1
;
2223 case LLCSAP_NETBEUI
:
2225 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2226 * so we check the DSAP and SSAP.
2228 * LLCSAP_IP checks for IP-over-802.2, rather
2229 * than IP-over-Ethernet or IP-over-SNAP.
2231 * XXX - should we check both the DSAP and the
2232 * SSAP, like this, or should we check just the
2233 * DSAP, as we do for other types <= ETHERMTU
2234 * (i.e., other SAP values)?
2236 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2237 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2243 * Ethernet_II frames, which are Ethernet
2244 * frames with a frame type of ETHERTYPE_IPX;
2246 * Ethernet_802.3 frames, which have a frame
2247 * type of LINUX_SLL_P_802_3;
2249 * Ethernet_802.2 frames, which are 802.3
2250 * frames with an 802.2 LLC header (i.e, have
2251 * a frame type of LINUX_SLL_P_802_2) and
2252 * with the IPX LSAP as the DSAP in the LLC
2255 * Ethernet_SNAP frames, which are 802.3
2256 * frames with an LLC header and a SNAP
2257 * header and with an OUI of 0x000000
2258 * (encapsulated Ethernet) and a protocol
2259 * ID of ETHERTYPE_IPX in the SNAP header.
2261 * First, do the checks on LINUX_SLL_P_802_2
2262 * frames; generate the check for either
2263 * Ethernet_802.2 or Ethernet_SNAP frames, and
2264 * then put a check for LINUX_SLL_P_802_2 frames
2267 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2268 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2270 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2274 * Now check for 802.3 frames and OR that with
2275 * the previous test.
2277 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2281 * Now add the check for Ethernet_II frames, and
2282 * do that before checking for the other frame
2285 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2289 case ETHERTYPE_ATALK
:
2290 case ETHERTYPE_AARP
:
2292 * EtherTalk (AppleTalk protocols on Ethernet link
2293 * layer) may use 802.2 encapsulation.
2297 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2298 * we check for the 802.2 protocol type in the
2299 * "Ethernet type" field.
2301 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2304 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2305 * SNAP packets with an organization code of
2306 * 0x080007 (Apple, for Appletalk) and a protocol
2307 * type of ETHERTYPE_ATALK (Appletalk).
2309 * 802.2-encapsulated ETHERTYPE_AARP packets are
2310 * SNAP packets with an organization code of
2311 * 0x000000 (encapsulated Ethernet) and a protocol
2312 * type of ETHERTYPE_AARP (Appletalk ARP).
2314 if (ll_proto
== ETHERTYPE_ATALK
)
2315 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2316 else /* ll_proto == ETHERTYPE_AARP */
2317 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2321 * Check for Ethernet encapsulation (Ethertalk
2322 * phase 1?); we just check for the Ethernet
2325 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2331 if (ll_proto
<= ETHERMTU
) {
2333 * This is an LLC SAP value, so the frames
2334 * that match would be 802.2 frames.
2335 * Check for the 802.2 protocol type
2336 * in the "Ethernet type" field, and
2337 * then check the DSAP.
2339 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2340 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2346 * This is an Ethernet type, so compare
2347 * the length/type field with it (if
2348 * the frame is an 802.2 frame, the length
2349 * field will be <= ETHERMTU, and, as
2350 * "ll_proto" is > ETHERMTU, this test
2351 * will fail and the frame won't match,
2352 * which is what we want).
2354 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2360 * Load a value relative to the beginning of the link-layer header after the
2363 static struct slist
*
2364 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2366 struct slist
*s1
, *s2
;
2369 * Generate code to load the length of the pflog header into
2370 * the register assigned to hold that length, if one has been
2371 * assigned. (If one hasn't been assigned, no code we've
2372 * generated uses that prefix, so we don't need to generate any
2375 if (cstate
->off_linkpl
.reg
!= -1) {
2377 * The length is in the first byte of the header.
2379 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2383 * Round it up to a multiple of 4.
2384 * Add 3, and clear the lower 2 bits.
2386 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2389 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2390 s2
->s
.k
= 0xfffffffc;
2394 * Now allocate a register to hold that value and store
2397 s2
= new_stmt(cstate
, BPF_ST
);
2398 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2402 * Now move it into the X register.
2404 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2412 static struct slist
*
2413 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2415 struct slist
*s1
, *s2
;
2416 struct slist
*sjeq_avs_cookie
;
2417 struct slist
*sjcommon
;
2420 * This code is not compatible with the optimizer, as
2421 * we are generating jmp instructions within a normal
2422 * slist of instructions
2424 cstate
->no_optimize
= 1;
2427 * Generate code to load the length of the radio header into
2428 * the register assigned to hold that length, if one has been
2429 * assigned. (If one hasn't been assigned, no code we've
2430 * generated uses that prefix, so we don't need to generate any
2433 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2434 * or always use the AVS header rather than the Prism header.
2435 * We load a 4-byte big-endian value at the beginning of the
2436 * raw packet data, and see whether, when masked with 0xFFFFF000,
2437 * it's equal to 0x80211000. If so, that indicates that it's
2438 * an AVS header (the masked-out bits are the version number).
2439 * Otherwise, it's a Prism header.
2441 * XXX - the Prism header is also, in theory, variable-length,
2442 * but no known software generates headers that aren't 144
2445 if (cstate
->off_linkhdr
.reg
!= -1) {
2449 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2453 * AND it with 0xFFFFF000.
2455 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2456 s2
->s
.k
= 0xFFFFF000;
2460 * Compare with 0x80211000.
2462 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2463 sjeq_avs_cookie
->s
.k
= 0x80211000;
2464 sappend(s1
, sjeq_avs_cookie
);
2469 * The 4 bytes at an offset of 4 from the beginning of
2470 * the AVS header are the length of the AVS header.
2471 * That field is big-endian.
2473 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2476 sjeq_avs_cookie
->s
.jt
= s2
;
2479 * Now jump to the code to allocate a register
2480 * into which to save the header length and
2481 * store the length there. (The "jump always"
2482 * instruction needs to have the k field set;
2483 * it's added to the PC, so, as we're jumping
2484 * over a single instruction, it should be 1.)
2486 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2488 sappend(s1
, sjcommon
);
2491 * Now for the code that handles the Prism header.
2492 * Just load the length of the Prism header (144)
2493 * into the A register. Have the test for an AVS
2494 * header branch here if we don't have an AVS header.
2496 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2499 sjeq_avs_cookie
->s
.jf
= s2
;
2502 * Now allocate a register to hold that value and store
2503 * it. The code for the AVS header will jump here after
2504 * loading the length of the AVS header.
2506 s2
= new_stmt(cstate
, BPF_ST
);
2507 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2509 sjcommon
->s
.jf
= s2
;
2512 * Now move it into the X register.
2514 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2522 static struct slist
*
2523 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2525 struct slist
*s1
, *s2
;
2528 * Generate code to load the length of the AVS header into
2529 * the register assigned to hold that length, if one has been
2530 * assigned. (If one hasn't been assigned, no code we've
2531 * generated uses that prefix, so we don't need to generate any
2534 if (cstate
->off_linkhdr
.reg
!= -1) {
2536 * The 4 bytes at an offset of 4 from the beginning of
2537 * the AVS header are the length of the AVS header.
2538 * That field is big-endian.
2540 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2544 * Now allocate a register to hold that value and store
2547 s2
= new_stmt(cstate
, BPF_ST
);
2548 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2552 * Now move it into the X register.
2554 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2562 static struct slist
*
2563 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2565 struct slist
*s1
, *s2
;
2568 * Generate code to load the length of the radiotap header into
2569 * the register assigned to hold that length, if one has been
2570 * assigned. (If one hasn't been assigned, no code we've
2571 * generated uses that prefix, so we don't need to generate any
2574 if (cstate
->off_linkhdr
.reg
!= -1) {
2576 * The 2 bytes at offsets of 2 and 3 from the beginning
2577 * of the radiotap header are the length of the radiotap
2578 * header; unfortunately, it's little-endian, so we have
2579 * to load it a byte at a time and construct the value.
2583 * Load the high-order byte, at an offset of 3, shift it
2584 * left a byte, and put the result in the X register.
2586 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2588 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2591 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2595 * Load the next byte, at an offset of 2, and OR the
2596 * value from the X register into it.
2598 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2601 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2605 * Now allocate a register to hold that value and store
2608 s2
= new_stmt(cstate
, BPF_ST
);
2609 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2613 * Now move it into the X register.
2615 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2624 * At the moment we treat PPI as normal Radiotap encoded
2625 * packets. The difference is in the function that generates
2626 * the code at the beginning to compute the header length.
2627 * Since this code generator of PPI supports bare 802.11
2628 * encapsulation only (i.e. the encapsulated DLT should be
2629 * DLT_IEEE802_11) we generate code to check for this too;
2630 * that's done in finish_parse().
2632 static struct slist
*
2633 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2635 struct slist
*s1
, *s2
;
2638 * Generate code to load the length of the radiotap header
2639 * into the register assigned to hold that length, if one has
2642 if (cstate
->off_linkhdr
.reg
!= -1) {
2644 * The 2 bytes at offsets of 2 and 3 from the beginning
2645 * of the radiotap header are the length of the radiotap
2646 * header; unfortunately, it's little-endian, so we have
2647 * to load it a byte at a time and construct the value.
2651 * Load the high-order byte, at an offset of 3, shift it
2652 * left a byte, and put the result in the X register.
2654 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2656 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2659 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2663 * Load the next byte, at an offset of 2, and OR the
2664 * value from the X register into it.
2666 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2669 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2673 * Now allocate a register to hold that value and store
2676 s2
= new_stmt(cstate
, BPF_ST
);
2677 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2681 * Now move it into the X register.
2683 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2692 * Load a value relative to the beginning of the link-layer header after the 802.11
2693 * header, i.e. LLC_SNAP.
2694 * The link-layer header doesn't necessarily begin at the beginning
2695 * of the packet data; there might be a variable-length prefix containing
2696 * radio information.
2698 static struct slist
*
2699 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2702 struct slist
*sjset_data_frame_1
;
2703 struct slist
*sjset_data_frame_2
;
2704 struct slist
*sjset_qos
;
2705 struct slist
*sjset_radiotap_flags_present
;
2706 struct slist
*sjset_radiotap_ext_present
;
2707 struct slist
*sjset_radiotap_tsft_present
;
2708 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2709 struct slist
*s_roundup
;
2711 if (cstate
->off_linkpl
.reg
== -1) {
2713 * No register has been assigned to the offset of
2714 * the link-layer payload, which means nobody needs
2715 * it; don't bother computing it - just return
2716 * what we already have.
2722 * This code is not compatible with the optimizer, as
2723 * we are generating jmp instructions within a normal
2724 * slist of instructions
2726 cstate
->no_optimize
= 1;
2729 * If "s" is non-null, it has code to arrange that the X register
2730 * contains the length of the prefix preceding the link-layer
2733 * Otherwise, the length of the prefix preceding the link-layer
2734 * header is "off_outermostlinkhdr.constant_part".
2738 * There is no variable-length header preceding the
2739 * link-layer header.
2741 * Load the length of the fixed-length prefix preceding
2742 * the link-layer header (if any) into the X register,
2743 * and store it in the cstate->off_linkpl.reg register.
2744 * That length is off_outermostlinkhdr.constant_part.
2746 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2747 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2751 * The X register contains the offset of the beginning of the
2752 * link-layer header; add 24, which is the minimum length
2753 * of the MAC header for a data frame, to that, and store it
2754 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2755 * which is at the offset in the X register, with an indexed load.
2757 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2759 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2762 s2
= new_stmt(cstate
, BPF_ST
);
2763 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2766 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2771 * Check the Frame Control field to see if this is a data frame;
2772 * a data frame has the 0x08 bit (b3) in that field set and the
2773 * 0x04 bit (b2) clear.
2775 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2776 sjset_data_frame_1
->s
.k
= 0x08;
2777 sappend(s
, sjset_data_frame_1
);
2780 * If b3 is set, test b2, otherwise go to the first statement of
2781 * the rest of the program.
2783 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2784 sjset_data_frame_2
->s
.k
= 0x04;
2785 sappend(s
, sjset_data_frame_2
);
2786 sjset_data_frame_1
->s
.jf
= snext
;
2789 * If b2 is not set, this is a data frame; test the QoS bit.
2790 * Otherwise, go to the first statement of the rest of the
2793 sjset_data_frame_2
->s
.jt
= snext
;
2794 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2795 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2796 sappend(s
, sjset_qos
);
2799 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2801 * Otherwise, go to the first statement of the rest of the
2804 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2805 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2807 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2810 s2
= new_stmt(cstate
, BPF_ST
);
2811 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2815 * If we have a radiotap header, look at it to see whether
2816 * there's Atheros padding between the MAC-layer header
2819 * Note: all of the fields in the radiotap header are
2820 * little-endian, so we byte-swap all of the values
2821 * we test against, as they will be loaded as big-endian
2824 * XXX - in the general case, we would have to scan through
2825 * *all* the presence bits, if there's more than one word of
2826 * presence bits. That would require a loop, meaning that
2827 * we wouldn't be able to run the filter in the kernel.
2829 * We assume here that the Atheros adapters that insert the
2830 * annoying padding don't have multiple antennae and therefore
2831 * do not generate radiotap headers with multiple presence words.
2833 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2835 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2836 * in the first presence flag word?
2838 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2842 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2843 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2844 sappend(s
, sjset_radiotap_flags_present
);
2847 * If not, skip all of this.
2849 sjset_radiotap_flags_present
->s
.jf
= snext
;
2852 * Otherwise, is the "extension" bit set in that word?
2854 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2855 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2856 sappend(s
, sjset_radiotap_ext_present
);
2857 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2860 * If so, skip all of this.
2862 sjset_radiotap_ext_present
->s
.jt
= snext
;
2865 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2867 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2868 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2869 sappend(s
, sjset_radiotap_tsft_present
);
2870 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2873 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2874 * at an offset of 16 from the beginning of the raw packet
2875 * data (8 bytes for the radiotap header and 8 bytes for
2878 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2881 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2884 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2886 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2887 sjset_tsft_datapad
->s
.k
= 0x20;
2888 sappend(s
, sjset_tsft_datapad
);
2891 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2892 * at an offset of 8 from the beginning of the raw packet
2893 * data (8 bytes for the radiotap header).
2895 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2898 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2901 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2903 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2904 sjset_notsft_datapad
->s
.k
= 0x20;
2905 sappend(s
, sjset_notsft_datapad
);
2908 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2909 * set, round the length of the 802.11 header to
2910 * a multiple of 4. Do that by adding 3 and then
2911 * dividing by and multiplying by 4, which we do by
2914 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2915 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2916 sappend(s
, s_roundup
);
2917 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2920 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2921 s2
->s
.k
= (bpf_u_int32
)~3;
2923 s2
= new_stmt(cstate
, BPF_ST
);
2924 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2927 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2928 sjset_tsft_datapad
->s
.jf
= snext
;
2929 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2930 sjset_notsft_datapad
->s
.jf
= snext
;
2932 sjset_qos
->s
.jf
= snext
;
2938 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2942 /* There is an implicit dependency between the link
2943 * payload and link header since the payload computation
2944 * includes the variable part of the header. Therefore,
2945 * if nobody else has allocated a register for the link
2946 * header and we need it, do it now. */
2947 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2948 cstate
->off_linkhdr
.reg
== -1)
2949 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2952 * For link-layer types that have a variable-length header
2953 * preceding the link-layer header, generate code to load
2954 * the offset of the link-layer header into the register
2955 * assigned to that offset, if any.
2957 * XXX - this, and the next switch statement, won't handle
2958 * encapsulation of 802.11 or 802.11+radio information in
2959 * some other protocol stack. That's significantly more
2962 switch (cstate
->outermostlinktype
) {
2964 case DLT_PRISM_HEADER
:
2965 s
= gen_load_prism_llprefixlen(cstate
);
2968 case DLT_IEEE802_11_RADIO_AVS
:
2969 s
= gen_load_avs_llprefixlen(cstate
);
2972 case DLT_IEEE802_11_RADIO
:
2973 s
= gen_load_radiotap_llprefixlen(cstate
);
2977 s
= gen_load_ppi_llprefixlen(cstate
);
2986 * For link-layer types that have a variable-length link-layer
2987 * header, generate code to load the offset of the link-layer
2988 * payload into the register assigned to that offset, if any.
2990 switch (cstate
->outermostlinktype
) {
2992 case DLT_IEEE802_11
:
2993 case DLT_PRISM_HEADER
:
2994 case DLT_IEEE802_11_RADIO_AVS
:
2995 case DLT_IEEE802_11_RADIO
:
2997 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3001 s
= gen_load_pflog_llprefixlen(cstate
);
3006 * If there is no initialization yet and we need variable
3007 * length offsets for VLAN, initialize them to zero
3009 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3012 if (cstate
->off_linkpl
.reg
== -1)
3013 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3014 if (cstate
->off_linktype
.reg
== -1)
3015 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3017 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3019 s2
= new_stmt(cstate
, BPF_ST
);
3020 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3022 s2
= new_stmt(cstate
, BPF_ST
);
3023 s2
->s
.k
= cstate
->off_linktype
.reg
;
3028 * If we have any offset-loading code, append all the
3029 * existing statements in the block to those statements,
3030 * and make the resulting list the list of statements
3034 sappend(s
, b
->stmts
);
3039 static struct block
*
3040 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3042 struct slist
*s_load_dlt
;
3045 if (cstate
->linktype
== DLT_PPI
)
3047 /* Create the statements that check for the DLT
3049 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3050 s_load_dlt
->s
.k
= 4;
3052 b
= new_block(cstate
, JMP(BPF_JEQ
));
3054 b
->stmts
= s_load_dlt
;
3055 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3066 * Take an absolute offset, and:
3068 * if it has no variable part, return NULL;
3070 * if it has a variable part, generate code to load the register
3071 * containing that variable part into the X register, returning
3072 * a pointer to that code - if no register for that offset has
3073 * been allocated, allocate it first.
3075 * (The code to set that register will be generated later, but will
3076 * be placed earlier in the code sequence.)
3078 static struct slist
*
3079 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3083 if (off
->is_variable
) {
3084 if (off
->reg
== -1) {
3086 * We haven't yet assigned a register for the
3087 * variable part of the offset of the link-layer
3088 * header; allocate one.
3090 off
->reg
= alloc_reg(cstate
);
3094 * Load the register containing the variable part of the
3095 * offset of the link-layer header into the X register.
3097 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3102 * That offset isn't variable, there's no variable part,
3103 * so we don't need to generate any code.
3110 * Map an Ethernet type to the equivalent PPP type.
3113 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3121 case ETHERTYPE_IPV6
:
3122 ll_proto
= PPP_IPV6
;
3126 ll_proto
= PPP_DECNET
;
3129 case ETHERTYPE_ATALK
:
3130 ll_proto
= PPP_APPLE
;
3143 * I'm assuming the "Bridging PDU"s that go
3144 * over PPP are Spanning Tree Protocol
3147 ll_proto
= PPP_BRPDU
;
3158 * Generate any tests that, for encapsulation of a link-layer packet
3159 * inside another protocol stack, need to be done to check for those
3160 * link-layer packets (and that haven't already been done by a check
3161 * for that encapsulation).
3163 static struct block
*
3164 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3168 if (cstate
->is_geneve
)
3169 return gen_geneve_ll_check(cstate
);
3171 switch (cstate
->prevlinktype
) {
3175 * This is LANE-encapsulated Ethernet; check that the LANE
3176 * packet doesn't begin with an LE Control marker, i.e.
3177 * that it's data, not a control message.
3179 * (We've already generated a test for LANE.)
3181 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3187 * No such tests are necessary.
3195 * The three different values we should check for when checking for an
3196 * IPv6 packet with DLT_NULL.
3198 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3199 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3200 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3203 * Generate code to match a particular packet type by matching the
3204 * link-layer type field or fields in the 802.2 LLC header.
3206 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3207 * value, if <= ETHERMTU.
3209 static struct block
*
3210 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3212 struct block
*b0
, *b1
, *b2
;
3213 const char *description
;
3215 /* are we checking MPLS-encapsulated packets? */
3216 if (cstate
->label_stack_depth
> 0)
3217 return gen_mpls_linktype(cstate
, ll_proto
);
3219 switch (cstate
->linktype
) {
3222 case DLT_NETANALYZER
:
3223 case DLT_NETANALYZER_TRANSPARENT
:
3224 /* Geneve has an EtherType regardless of whether there is an
3226 if (!cstate
->is_geneve
)
3227 b0
= gen_prevlinkhdr_check(cstate
);
3231 b1
= gen_ether_linktype(cstate
, ll_proto
);
3242 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3246 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3250 case DLT_IEEE802_11
:
3251 case DLT_PRISM_HEADER
:
3252 case DLT_IEEE802_11_RADIO_AVS
:
3253 case DLT_IEEE802_11_RADIO
:
3256 * Check that we have a data frame.
3258 b0
= gen_check_802_11_data_frame(cstate
);
3261 * Now check for the specified link-layer type.
3263 b1
= gen_llc_linktype(cstate
, ll_proto
);
3270 * XXX - check for LLC frames.
3272 return gen_llc_linktype(cstate
, ll_proto
);
3277 * XXX - check for LLC PDUs, as per IEEE 802.5.
3279 return gen_llc_linktype(cstate
, ll_proto
);
3282 case DLT_ATM_RFC1483
:
3284 case DLT_IP_OVER_FC
:
3285 return gen_llc_linktype(cstate
, ll_proto
);
3290 * Check for an LLC-encapsulated version of this protocol;
3291 * if we were checking for LANE, linktype would no longer
3294 * Check for LLC encapsulation and then check the protocol.
3296 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3297 b1
= gen_llc_linktype(cstate
, ll_proto
);
3303 return gen_linux_sll_linktype(cstate
, ll_proto
);
3307 case DLT_SLIP_BSDOS
:
3310 * These types don't provide any type field; packets
3311 * are always IPv4 or IPv6.
3313 * XXX - for IPv4, check for a version number of 4, and,
3314 * for IPv6, check for a version number of 6?
3319 /* Check for a version number of 4. */
3320 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3322 case ETHERTYPE_IPV6
:
3323 /* Check for a version number of 6. */
3324 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3327 return gen_false(cstate
); /* always false */
3333 * Raw IPv4, so no type field.
3335 if (ll_proto
== ETHERTYPE_IP
)
3336 return gen_true(cstate
); /* always true */
3338 /* Checking for something other than IPv4; always false */
3339 return gen_false(cstate
);
3344 * Raw IPv6, so no type field.
3346 if (ll_proto
== ETHERTYPE_IPV6
)
3347 return gen_true(cstate
); /* always true */
3349 /* Checking for something other than IPv6; always false */
3350 return gen_false(cstate
);
3355 case DLT_PPP_SERIAL
:
3358 * We use Ethernet protocol types inside libpcap;
3359 * map them to the corresponding PPP protocol types.
3361 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3362 ethertype_to_ppptype(ll_proto
));
3367 * We use Ethernet protocol types inside libpcap;
3368 * map them to the corresponding PPP protocol types.
3374 * Also check for Van Jacobson-compressed IP.
3375 * XXX - do this for other forms of PPP?
3377 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3378 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3380 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3385 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3386 ethertype_to_ppptype(ll_proto
));
3396 return (gen_loopback_linktype(cstate
, AF_INET
));
3398 case ETHERTYPE_IPV6
:
3400 * AF_ values may, unfortunately, be platform-
3401 * dependent; AF_INET isn't, because everybody
3402 * used 4.2BSD's value, but AF_INET6 is, because
3403 * 4.2BSD didn't have a value for it (given that
3404 * IPv6 didn't exist back in the early 1980's),
3405 * and they all picked their own values.
3407 * This means that, if we're reading from a
3408 * savefile, we need to check for all the
3411 * If we're doing a live capture, we only need
3412 * to check for this platform's value; however,
3413 * Npcap uses 24, which isn't Windows's AF_INET6
3414 * value. (Given the multiple different values,
3415 * programs that read pcap files shouldn't be
3416 * checking for their platform's AF_INET6 value
3417 * anyway, they should check for all of the
3418 * possible values. and they might as well do
3419 * that even for live captures.)
3421 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3423 * Savefile - check for all three
3424 * possible IPv6 values.
3426 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3427 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3429 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3434 * Live capture, so we only need to
3435 * check for the value used on this
3440 * Npcap doesn't use Windows's AF_INET6,
3441 * as that collides with AF_IPX on
3442 * some BSDs (both have the value 23).
3443 * Instead, it uses 24.
3445 return (gen_loopback_linktype(cstate
, 24));
3448 return (gen_loopback_linktype(cstate
, AF_INET6
));
3449 #else /* AF_INET6 */
3451 * I guess this platform doesn't support
3452 * IPv6, so we just reject all packets.
3454 return gen_false(cstate
);
3455 #endif /* AF_INET6 */
3461 * Not a type on which we support filtering.
3462 * XXX - support those that have AF_ values
3463 * #defined on this platform, at least?
3465 return gen_false(cstate
);
3470 * af field is host byte order in contrast to the rest of
3473 if (ll_proto
== ETHERTYPE_IP
)
3474 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3476 else if (ll_proto
== ETHERTYPE_IPV6
)
3477 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3480 return gen_false(cstate
);
3484 case DLT_ARCNET_LINUX
:
3486 * XXX should we check for first fragment if the protocol
3492 return gen_false(cstate
);
3494 case ETHERTYPE_IPV6
:
3495 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3499 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3501 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3507 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3509 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3514 case ETHERTYPE_REVARP
:
3515 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3518 case ETHERTYPE_ATALK
:
3519 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3526 case ETHERTYPE_ATALK
:
3527 return gen_true(cstate
);
3529 return gen_false(cstate
);
3535 * XXX - assumes a 2-byte Frame Relay header with
3536 * DLCI and flags. What if the address is longer?
3542 * Check for the special NLPID for IP.
3544 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3546 case ETHERTYPE_IPV6
:
3548 * Check for the special NLPID for IPv6.
3550 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3554 * Check for several OSI protocols.
3556 * Frame Relay packets typically have an OSI
3557 * NLPID at the beginning; we check for each
3560 * What we check for is the NLPID and a frame
3561 * control field of UI, i.e. 0x03 followed
3564 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3565 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3566 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3572 return gen_false(cstate
);
3577 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3579 case DLT_JUNIPER_MFR
:
3580 case DLT_JUNIPER_MLFR
:
3581 case DLT_JUNIPER_MLPPP
:
3582 case DLT_JUNIPER_ATM1
:
3583 case DLT_JUNIPER_ATM2
:
3584 case DLT_JUNIPER_PPPOE
:
3585 case DLT_JUNIPER_PPPOE_ATM
:
3586 case DLT_JUNIPER_GGSN
:
3587 case DLT_JUNIPER_ES
:
3588 case DLT_JUNIPER_MONITOR
:
3589 case DLT_JUNIPER_SERVICES
:
3590 case DLT_JUNIPER_ETHER
:
3591 case DLT_JUNIPER_PPP
:
3592 case DLT_JUNIPER_FRELAY
:
3593 case DLT_JUNIPER_CHDLC
:
3594 case DLT_JUNIPER_VP
:
3595 case DLT_JUNIPER_ST
:
3596 case DLT_JUNIPER_ISM
:
3597 case DLT_JUNIPER_VS
:
3598 case DLT_JUNIPER_SRX_E2E
:
3599 case DLT_JUNIPER_FIBRECHANNEL
:
3600 case DLT_JUNIPER_ATM_CEMIC
:
3602 /* just lets verify the magic number for now -
3603 * on ATM we may have up to 6 different encapsulations on the wire
3604 * and need a lot of heuristics to figure out that the payload
3607 * FIXME encapsulation specific BPF_ filters
3609 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3611 case DLT_BACNET_MS_TP
:
3612 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3615 return gen_ipnet_linktype(cstate
, ll_proto
);
3617 case DLT_LINUX_IRDA
:
3618 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3621 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3624 case DLT_MTP2_WITH_PHDR
:
3625 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3628 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3631 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3633 case DLT_LINUX_LAPD
:
3634 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3636 case DLT_USB_FREEBSD
:
3638 case DLT_USB_LINUX_MMAPPED
:
3640 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3642 case DLT_BLUETOOTH_HCI_H4
:
3643 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3644 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3647 case DLT_CAN_SOCKETCAN
:
3648 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3650 case DLT_IEEE802_15_4
:
3651 case DLT_IEEE802_15_4_LINUX
:
3652 case DLT_IEEE802_15_4_NONASK_PHY
:
3653 case DLT_IEEE802_15_4_NOFCS
:
3654 case DLT_IEEE802_15_4_TAP
:
3655 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3657 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3658 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3661 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3664 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3666 case DLT_IPMB_KONTRON
:
3667 case DLT_IPMB_LINUX
:
3668 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3671 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3674 /* Using the fixed-size NFLOG header it is possible to tell only
3675 * the address family of the packet, other meaningful data is
3676 * either missing or behind TLVs.
3678 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3682 * Does this link-layer header type have a field
3683 * indicating the type of the next protocol? If
3684 * so, off_linktype.constant_part will be the offset of that
3685 * field in the packet; if not, it will be OFFSET_NOT_SET.
3687 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3689 * Yes; assume it's an Ethernet type. (If
3690 * it's not, it needs to be handled specially
3693 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3697 * No; report an error.
3699 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3700 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3708 * Check for an LLC SNAP packet with a given organization code and
3709 * protocol type; we check the entire contents of the 802.2 LLC and
3710 * snap headers, checking for DSAP and SSAP of SNAP and a control
3711 * field of 0x03 in the LLC header, and for the specified organization
3712 * code and protocol type in the SNAP header.
3714 static struct block
*
3715 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3717 u_char snapblock
[8];
3719 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3720 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3721 snapblock
[2] = 0x03; /* control = UI */
3722 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3723 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3724 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3725 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3726 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3727 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3731 * Generate code to match frames with an LLC header.
3733 static struct block
*
3734 gen_llc_internal(compiler_state_t
*cstate
)
3736 struct block
*b0
, *b1
;
3738 switch (cstate
->linktype
) {
3742 * We check for an Ethernet type field less than
3743 * 1500, which means it's an 802.3 length field.
3745 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3749 * Now check for the purported DSAP and SSAP not being
3750 * 0xFF, to rule out NetWare-over-802.3.
3752 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3759 * We check for LLC traffic.
3761 b0
= gen_atmtype_llc(cstate
);
3764 case DLT_IEEE802
: /* Token Ring */
3766 * XXX - check for LLC frames.
3768 return gen_true(cstate
);
3772 * XXX - check for LLC frames.
3774 return gen_true(cstate
);
3776 case DLT_ATM_RFC1483
:
3778 * For LLC encapsulation, these are defined to have an
3781 * For VC encapsulation, they don't, but there's no
3782 * way to check for that; the protocol used on the VC
3783 * is negotiated out of band.
3785 return gen_true(cstate
);
3787 case DLT_IEEE802_11
:
3788 case DLT_PRISM_HEADER
:
3789 case DLT_IEEE802_11_RADIO
:
3790 case DLT_IEEE802_11_RADIO_AVS
:
3793 * Check that we have a data frame.
3795 b0
= gen_check_802_11_data_frame(cstate
);
3799 bpf_error(cstate
, "'llc' not supported for %s",
3800 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3806 gen_llc(compiler_state_t
*cstate
)
3809 * Catch errors reported by us and routines below us, and return NULL
3812 if (setjmp(cstate
->top_ctx
))
3815 return gen_llc_internal(cstate
);
3819 gen_llc_i(compiler_state_t
*cstate
)
3821 struct block
*b0
, *b1
;
3825 * Catch errors reported by us and routines below us, and return NULL
3828 if (setjmp(cstate
->top_ctx
))
3832 * Check whether this is an LLC frame.
3834 b0
= gen_llc_internal(cstate
);
3837 * Load the control byte and test the low-order bit; it must
3838 * be clear for I frames.
3840 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3841 b1
= new_block(cstate
, JMP(BPF_JSET
));
3850 gen_llc_s(compiler_state_t
*cstate
)
3852 struct block
*b0
, *b1
;
3855 * Catch errors reported by us and routines below us, and return NULL
3858 if (setjmp(cstate
->top_ctx
))
3862 * Check whether this is an LLC frame.
3864 b0
= gen_llc_internal(cstate
);
3867 * Now compare the low-order 2 bit of the control byte against
3868 * the appropriate value for S frames.
3870 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3876 gen_llc_u(compiler_state_t
*cstate
)
3878 struct block
*b0
, *b1
;
3881 * Catch errors reported by us and routines below us, and return NULL
3884 if (setjmp(cstate
->top_ctx
))
3888 * Check whether this is an LLC frame.
3890 b0
= gen_llc_internal(cstate
);
3893 * Now compare the low-order 2 bit of the control byte against
3894 * the appropriate value for U frames.
3896 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3902 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3904 struct block
*b0
, *b1
;
3907 * Catch errors reported by us and routines below us, and return NULL
3910 if (setjmp(cstate
->top_ctx
))
3914 * Check whether this is an LLC frame.
3916 b0
= gen_llc_internal(cstate
);
3919 * Now check for an S frame with the appropriate type.
3921 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3927 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3929 struct block
*b0
, *b1
;
3932 * Catch errors reported by us and routines below us, and return NULL
3935 if (setjmp(cstate
->top_ctx
))
3939 * Check whether this is an LLC frame.
3941 b0
= gen_llc_internal(cstate
);
3944 * Now check for a U frame with the appropriate type.
3946 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3952 * Generate code to match a particular packet type, for link-layer types
3953 * using 802.2 LLC headers.
3955 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3956 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3958 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3959 * value, if <= ETHERMTU. We use that to determine whether to
3960 * match the DSAP or both DSAP and LSAP or to check the OUI and
3961 * protocol ID in a SNAP header.
3963 static struct block
*
3964 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3967 * XXX - handle token-ring variable-length header.
3973 case LLCSAP_NETBEUI
:
3975 * XXX - should we check both the DSAP and the
3976 * SSAP, like this, or should we check just the
3977 * DSAP, as we do for other SAP values?
3979 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3980 ((ll_proto
<< 8) | ll_proto
));
3984 * XXX - are there ever SNAP frames for IPX on
3985 * non-Ethernet 802.x networks?
3987 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3989 case ETHERTYPE_ATALK
:
3991 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3992 * SNAP packets with an organization code of
3993 * 0x080007 (Apple, for Appletalk) and a protocol
3994 * type of ETHERTYPE_ATALK (Appletalk).
3996 * XXX - check for an organization code of
3997 * encapsulated Ethernet as well?
3999 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4003 * XXX - we don't have to check for IPX 802.3
4004 * here, but should we check for the IPX Ethertype?
4006 if (ll_proto
<= ETHERMTU
) {
4008 * This is an LLC SAP value, so check
4011 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4014 * This is an Ethernet type; we assume that it's
4015 * unlikely that it'll appear in the right place
4016 * at random, and therefore check only the
4017 * location that would hold the Ethernet type
4018 * in a SNAP frame with an organization code of
4019 * 0x000000 (encapsulated Ethernet).
4021 * XXX - if we were to check for the SNAP DSAP and
4022 * LSAP, as per XXX, and were also to check for an
4023 * organization code of 0x000000 (encapsulated
4024 * Ethernet), we'd do
4026 * return gen_snap(cstate, 0x000000, ll_proto);
4028 * here; for now, we don't, as per the above.
4029 * I don't know whether it's worth the extra CPU
4030 * time to do the right check or not.
4032 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4037 static struct block
*
4038 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4039 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4041 struct block
*b0
, *b1
;
4055 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4056 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4062 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4063 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4068 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4072 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4076 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4080 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4084 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4088 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4095 b0
= gen_linktype(cstate
, ll_proto
);
4096 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4102 static struct block
*
4103 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4104 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4107 struct block
*b0
, *b1
;
4110 * Code below needs to access four separate 32-bit parts of the 128-bit
4111 * IPv6 address and mask. In some OSes this is as simple as using the
4112 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4113 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4114 * far as libpcap sees it. Hence copy the data before use to avoid
4115 * potential unaligned memory access and the associated compiler
4116 * warnings (whether genuine or not).
4118 bpf_u_int32 a
[4], m
[4];
4131 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4132 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4138 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4139 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4144 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4148 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4152 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4156 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4160 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4164 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4171 /* this order is important */
4172 memcpy(a
, addr
, sizeof(a
));
4173 memcpy(m
, mask
, sizeof(m
));
4174 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4175 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4177 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4179 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4181 b0
= gen_linktype(cstate
, ll_proto
);
4187 static struct block
*
4188 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4190 register struct block
*b0
, *b1
;
4194 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4197 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4200 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4201 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4207 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4208 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4213 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4217 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4221 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4225 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4229 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4233 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4241 * Like gen_ehostop, but for DLT_FDDI
4243 static struct block
*
4244 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4246 struct block
*b0
, *b1
;
4250 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4253 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4256 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4257 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4263 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4264 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4269 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4273 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4277 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4281 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4285 bpf_error(cstate
, "'ra' is only supported on 802.11");
4289 bpf_error(cstate
, "'ta' is only supported on 802.11");
4297 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4299 static struct block
*
4300 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4302 register struct block
*b0
, *b1
;
4306 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4309 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4312 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4313 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4319 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4320 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4325 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4329 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4333 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4337 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4341 bpf_error(cstate
, "'ra' is only supported on 802.11");
4345 bpf_error(cstate
, "'ta' is only supported on 802.11");
4353 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4354 * various 802.11 + radio headers.
4356 static struct block
*
4357 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4359 register struct block
*b0
, *b1
, *b2
;
4360 register struct slist
*s
;
4362 #ifdef ENABLE_WLAN_FILTERING_PATCH
4365 * We need to disable the optimizer because the optimizer is buggy
4366 * and wipes out some LD instructions generated by the below
4367 * code to validate the Frame Control bits
4369 cstate
->no_optimize
= 1;
4370 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4377 * For control frames, there is no SA.
4379 * For management frames, SA is at an
4380 * offset of 10 from the beginning of
4383 * For data frames, SA is at an offset
4384 * of 10 from the beginning of the packet
4385 * if From DS is clear, at an offset of
4386 * 16 from the beginning of the packet
4387 * if From DS is set and To DS is clear,
4388 * and an offset of 24 from the beginning
4389 * of the packet if From DS is set and To DS
4394 * Generate the tests to be done for data frames
4397 * First, check for To DS set, i.e. check "link[1] & 0x01".
4399 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4400 b1
= new_block(cstate
, JMP(BPF_JSET
));
4401 b1
->s
.k
= 0x01; /* To DS */
4405 * If To DS is set, the SA is at 24.
4407 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4411 * Now, check for To DS not set, i.e. check
4412 * "!(link[1] & 0x01)".
4414 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4415 b2
= new_block(cstate
, JMP(BPF_JSET
));
4416 b2
->s
.k
= 0x01; /* To DS */
4421 * If To DS is not set, the SA is at 16.
4423 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4427 * Now OR together the last two checks. That gives
4428 * the complete set of checks for data frames with
4434 * Now check for From DS being set, and AND that with
4435 * the ORed-together checks.
4437 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4438 b1
= new_block(cstate
, JMP(BPF_JSET
));
4439 b1
->s
.k
= 0x02; /* From DS */
4444 * Now check for data frames with From DS not set.
4446 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4447 b2
= new_block(cstate
, JMP(BPF_JSET
));
4448 b2
->s
.k
= 0x02; /* From DS */
4453 * If From DS isn't set, the SA is at 10.
4455 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4459 * Now OR together the checks for data frames with
4460 * From DS not set and for data frames with From DS
4461 * set; that gives the checks done for data frames.
4466 * Now check for a data frame.
4467 * I.e, check "link[0] & 0x08".
4469 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4470 b1
= new_block(cstate
, JMP(BPF_JSET
));
4475 * AND that with the checks done for data frames.
4480 * If the high-order bit of the type value is 0, this
4481 * is a management frame.
4482 * I.e, check "!(link[0] & 0x08)".
4484 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4485 b2
= new_block(cstate
, JMP(BPF_JSET
));
4491 * For management frames, the SA is at 10.
4493 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4497 * OR that with the checks done for data frames.
4498 * That gives the checks done for management and
4504 * If the low-order bit of the type value is 1,
4505 * this is either a control frame or a frame
4506 * with a reserved type, and thus not a
4509 * I.e., check "!(link[0] & 0x04)".
4511 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4512 b1
= new_block(cstate
, JMP(BPF_JSET
));
4518 * AND that with the checks for data and management
4528 * For control frames, there is no DA.
4530 * For management frames, DA is at an
4531 * offset of 4 from the beginning of
4534 * For data frames, DA is at an offset
4535 * of 4 from the beginning of the packet
4536 * if To DS is clear and at an offset of
4537 * 16 from the beginning of the packet
4542 * Generate the tests to be done for data frames.
4544 * First, check for To DS set, i.e. "link[1] & 0x01".
4546 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4547 b1
= new_block(cstate
, JMP(BPF_JSET
));
4548 b1
->s
.k
= 0x01; /* To DS */
4552 * If To DS is set, the DA is at 16.
4554 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4558 * Now, check for To DS not set, i.e. check
4559 * "!(link[1] & 0x01)".
4561 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4562 b2
= new_block(cstate
, JMP(BPF_JSET
));
4563 b2
->s
.k
= 0x01; /* To DS */
4568 * If To DS is not set, the DA is at 4.
4570 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4574 * Now OR together the last two checks. That gives
4575 * the complete set of checks for data frames.
4580 * Now check for a data frame.
4581 * I.e, check "link[0] & 0x08".
4583 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4584 b1
= new_block(cstate
, JMP(BPF_JSET
));
4589 * AND that with the checks done for data frames.
4594 * If the high-order bit of the type value is 0, this
4595 * is a management frame.
4596 * I.e, check "!(link[0] & 0x08)".
4598 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4599 b2
= new_block(cstate
, JMP(BPF_JSET
));
4605 * For management frames, the DA is at 4.
4607 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4611 * OR that with the checks done for data frames.
4612 * That gives the checks done for management and
4618 * If the low-order bit of the type value is 1,
4619 * this is either a control frame or a frame
4620 * with a reserved type, and thus not a
4623 * I.e., check "!(link[0] & 0x04)".
4625 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4626 b1
= new_block(cstate
, JMP(BPF_JSET
));
4632 * AND that with the checks for data and management
4639 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4640 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4646 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4647 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4652 * XXX - add BSSID keyword?
4655 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4659 * Not present in CTS or ACK control frames.
4661 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4662 IEEE80211_FC0_TYPE_MASK
);
4664 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4665 IEEE80211_FC0_SUBTYPE_MASK
);
4667 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4668 IEEE80211_FC0_SUBTYPE_MASK
);
4672 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4678 * Not present in control frames.
4680 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4681 IEEE80211_FC0_TYPE_MASK
);
4683 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4689 * Present only if the direction mask has both "From DS"
4690 * and "To DS" set. Neither control frames nor management
4691 * frames should have both of those set, so we don't
4692 * check the frame type.
4694 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4695 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4696 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4702 * Not present in management frames; addr1 in other
4707 * If the high-order bit of the type value is 0, this
4708 * is a management frame.
4709 * I.e, check "(link[0] & 0x08)".
4711 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4712 b1
= new_block(cstate
, JMP(BPF_JSET
));
4719 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4722 * AND that with the check of addr1.
4729 * Not present in management frames; addr2, if present,
4734 * Not present in CTS or ACK control frames.
4736 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4737 IEEE80211_FC0_TYPE_MASK
);
4739 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4740 IEEE80211_FC0_SUBTYPE_MASK
);
4742 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4743 IEEE80211_FC0_SUBTYPE_MASK
);
4749 * If the high-order bit of the type value is 0, this
4750 * is a management frame.
4751 * I.e, check "(link[0] & 0x08)".
4753 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4754 b1
= new_block(cstate
, JMP(BPF_JSET
));
4759 * AND that with the check for frames other than
4760 * CTS and ACK frames.
4767 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4776 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4777 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4778 * as the RFC states.)
4780 static struct block
*
4781 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4783 register struct block
*b0
, *b1
;
4787 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4790 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4793 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4794 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4800 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4801 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4806 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4810 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4814 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4818 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4822 bpf_error(cstate
, "'ra' is only supported on 802.11");
4826 bpf_error(cstate
, "'ta' is only supported on 802.11");
4834 * This is quite tricky because there may be pad bytes in front of the
4835 * DECNET header, and then there are two possible data packet formats that
4836 * carry both src and dst addresses, plus 5 packet types in a format that
4837 * carries only the src node, plus 2 types that use a different format and
4838 * also carry just the src node.
4842 * Instead of doing those all right, we just look for data packets with
4843 * 0 or 1 bytes of padding. If you want to look at other packets, that
4844 * will require a lot more hacking.
4846 * To add support for filtering on DECNET "areas" (network numbers)
4847 * one would want to add a "mask" argument to this routine. That would
4848 * make the filter even more inefficient, although one could be clever
4849 * and not generate masking instructions if the mask is 0xFFFF.
4851 static struct block
*
4852 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4854 struct block
*b0
, *b1
, *b2
, *tmp
;
4855 u_int offset_lh
; /* offset if long header is received */
4856 u_int offset_sh
; /* offset if short header is received */
4861 offset_sh
= 1; /* follows flags */
4862 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4866 offset_sh
= 3; /* follows flags, dstnode */
4867 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4871 /* Inefficient because we do our Calvinball dance twice */
4872 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4873 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4879 /* Inefficient because we do our Calvinball dance twice */
4880 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4881 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4886 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4890 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4894 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4898 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4902 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4906 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4913 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4914 /* Check for pad = 1, long header case */
4915 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4916 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4917 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4918 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4920 /* Check for pad = 0, long header case */
4921 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4923 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4924 (bpf_u_int32
)ntohs((u_short
)addr
));
4927 /* Check for pad = 1, short header case */
4928 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4929 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4930 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4931 (bpf_u_int32
)ntohs((u_short
)addr
));
4934 /* Check for pad = 0, short header case */
4935 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4937 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4938 (bpf_u_int32
)ntohs((u_short
)addr
));
4942 /* Combine with test for cstate->linktype */
4948 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4949 * test the bottom-of-stack bit, and then check the version number
4950 * field in the IP header.
4952 static struct block
*
4953 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4955 struct block
*b0
, *b1
;
4960 /* match the bottom-of-stack bit */
4961 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4962 /* match the IPv4 version number */
4963 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4967 case ETHERTYPE_IPV6
:
4968 /* match the bottom-of-stack bit */
4969 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4970 /* match the IPv4 version number */
4971 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4976 /* FIXME add other L3 proto IDs */
4977 bpf_error(cstate
, "unsupported protocol over mpls");
4982 static struct block
*
4983 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4984 int proto
, int dir
, int type
)
4986 struct block
*b0
, *b1
;
4987 const char *typestr
;
4997 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4999 * Only check for non-IPv4 addresses if we're not
5000 * checking MPLS-encapsulated packets.
5002 if (cstate
->label_stack_depth
== 0) {
5003 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5005 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5011 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5014 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
5017 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
5020 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
5023 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5026 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5029 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5032 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5035 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5038 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5041 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5044 return gen_dnhostop(cstate
, addr
, dir
);
5047 bpf_error(cstate
, "LAT host filtering not implemented");
5050 bpf_error(cstate
, "SCA host filtering not implemented");
5053 bpf_error(cstate
, "MOPRC host filtering not implemented");
5056 bpf_error(cstate
, "MOPDL host filtering not implemented");
5059 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5062 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5065 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5068 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5071 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5074 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5077 bpf_error(cstate
, "AARP host filtering not implemented");
5080 bpf_error(cstate
, "ISO host filtering not implemented");
5083 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5086 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5089 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5092 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5095 bpf_error(cstate
, "IPX host filtering not implemented");
5098 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5101 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5104 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5107 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5110 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5113 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5116 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5119 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5122 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5125 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5134 static struct block
*
5135 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5136 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5138 const char *typestr
;
5148 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5151 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5154 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5157 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5160 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5163 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5166 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5169 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5172 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5175 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5178 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5181 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5184 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5187 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5190 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5193 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5196 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5199 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5202 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5205 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5208 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5211 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5214 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5217 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5220 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5223 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5226 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5229 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5232 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5235 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5238 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5241 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5244 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5247 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5250 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5253 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5256 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5259 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5262 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5265 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5275 static struct block
*
5276 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5277 struct addrinfo
*alist
, int proto
, int dir
)
5279 struct block
*b0
, *b1
, *tmp
;
5280 struct addrinfo
*ai
;
5281 struct sockaddr_in
*sin
;
5284 bpf_error(cstate
, "direction applied to 'gateway'");
5291 switch (cstate
->linktype
) {
5293 case DLT_NETANALYZER
:
5294 case DLT_NETANALYZER_TRANSPARENT
:
5295 b1
= gen_prevlinkhdr_check(cstate
);
5296 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5301 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5304 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5306 case DLT_IEEE802_11
:
5307 case DLT_PRISM_HEADER
:
5308 case DLT_IEEE802_11_RADIO_AVS
:
5309 case DLT_IEEE802_11_RADIO
:
5311 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5315 * This is LLC-multiplexed traffic; if it were
5316 * LANE, cstate->linktype would have been set to
5320 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5322 case DLT_IP_OVER_FC
:
5323 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5327 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5330 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5332 * Does it have an address?
5334 if (ai
->ai_addr
!= NULL
) {
5336 * Yes. Is it an IPv4 address?
5338 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5340 * Generate an entry for it.
5342 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5343 tmp
= gen_host(cstate
,
5344 ntohl(sin
->sin_addr
.s_addr
),
5345 0xffffffff, proto
, Q_OR
, Q_HOST
);
5347 * Is it the *first* IPv4 address?
5351 * Yes, so start with it.
5356 * No, so OR it into the
5368 * No IPv4 addresses found.
5376 bpf_error(cstate
, "illegal modifier of 'gateway'");
5381 static struct block
*
5382 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5390 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5394 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5398 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5402 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5405 #ifndef IPPROTO_IGMP
5406 #define IPPROTO_IGMP 2
5410 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5413 #ifndef IPPROTO_IGRP
5414 #define IPPROTO_IGRP 9
5417 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5421 #define IPPROTO_PIM 103
5425 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5428 #ifndef IPPROTO_VRRP
5429 #define IPPROTO_VRRP 112
5433 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5436 #ifndef IPPROTO_CARP
5437 #define IPPROTO_CARP 112
5441 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5445 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5449 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5453 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5457 bpf_error(cstate
, "link layer applied in wrong context");
5460 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5464 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5468 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5472 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5476 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5480 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5484 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5488 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5491 #ifndef IPPROTO_ICMPV6
5492 #define IPPROTO_ICMPV6 58
5495 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5499 #define IPPROTO_AH 51
5502 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5506 #define IPPROTO_ESP 50
5509 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5513 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5517 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5521 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5524 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5525 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5526 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5528 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5530 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5532 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5536 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5537 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5538 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5540 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5542 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5544 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5548 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5549 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5550 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5552 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5557 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5558 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5563 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5564 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5566 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5568 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5573 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5574 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5579 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5580 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5585 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5589 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5593 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5597 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5601 bpf_error(cstate
, "'radio' is not a valid protocol type");
5610 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5613 * Catch errors reported by us and routines below us, and return NULL
5616 if (setjmp(cstate
->top_ctx
))
5619 return gen_proto_abbrev_internal(cstate
, proto
);
5622 static struct block
*
5623 gen_ipfrag(compiler_state_t
*cstate
)
5628 /* not IPv4 frag other than the first frag */
5629 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5630 b
= new_block(cstate
, JMP(BPF_JSET
));
5639 * Generate a comparison to a port value in the transport-layer header
5640 * at the specified offset from the beginning of that header.
5642 * XXX - this handles a variable-length prefix preceding the link-layer
5643 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5644 * variable-length link-layer headers (such as Token Ring or 802.11
5647 static struct block
*
5648 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5650 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5653 static struct block
*
5654 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5656 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5659 static struct block
*
5660 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5662 struct block
*b0
, *b1
, *tmp
;
5664 /* ip proto 'proto' and not a fragment other than the first fragment */
5665 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5666 b0
= gen_ipfrag(cstate
);
5671 b1
= gen_portatom(cstate
, 0, port
);
5675 b1
= gen_portatom(cstate
, 2, port
);
5679 tmp
= gen_portatom(cstate
, 0, port
);
5680 b1
= gen_portatom(cstate
, 2, port
);
5686 tmp
= gen_portatom(cstate
, 0, port
);
5687 b1
= gen_portatom(cstate
, 2, port
);
5692 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5696 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5700 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5704 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5708 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5712 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5724 static struct block
*
5725 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5727 struct block
*b0
, *b1
, *tmp
;
5732 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5733 * not LLC encapsulation with LLCSAP_IP.
5735 * For IEEE 802 networks - which includes 802.5 token ring
5736 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5737 * says that SNAP encapsulation is used, not LLC encapsulation
5740 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5741 * RFC 2225 say that SNAP encapsulation is used, not LLC
5742 * encapsulation with LLCSAP_IP.
5744 * So we always check for ETHERTYPE_IP.
5746 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5752 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5756 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5757 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5759 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5771 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5773 struct block
*b0
, *b1
, *tmp
;
5775 /* ip6 proto 'proto' */
5776 /* XXX - catch the first fragment of a fragmented packet? */
5777 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5781 b1
= gen_portatom6(cstate
, 0, port
);
5785 b1
= gen_portatom6(cstate
, 2, port
);
5789 tmp
= gen_portatom6(cstate
, 0, port
);
5790 b1
= gen_portatom6(cstate
, 2, port
);
5796 tmp
= gen_portatom6(cstate
, 0, port
);
5797 b1
= gen_portatom6(cstate
, 2, port
);
5809 static struct block
*
5810 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5812 struct block
*b0
, *b1
, *tmp
;
5814 /* link proto ip6 */
5815 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5821 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5825 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5826 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5828 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5839 /* gen_portrange code */
5840 static struct block
*
5841 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5844 struct block
*b1
, *b2
;
5848 * Reverse the order of the ports, so v1 is the lower one.
5857 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5858 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5865 static struct block
*
5866 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5867 bpf_u_int32 proto
, int dir
)
5869 struct block
*b0
, *b1
, *tmp
;
5871 /* ip proto 'proto' and not a fragment other than the first fragment */
5872 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5873 b0
= gen_ipfrag(cstate
);
5878 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5882 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5886 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5887 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5893 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5894 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5899 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5903 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5907 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5911 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5915 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5919 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5931 static struct block
*
5932 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5935 struct block
*b0
, *b1
, *tmp
;
5938 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5944 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5949 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5950 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5952 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5963 static struct block
*
5964 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5967 struct block
*b1
, *b2
;
5971 * Reverse the order of the ports, so v1 is the lower one.
5980 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5981 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5988 static struct block
*
5989 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5990 bpf_u_int32 proto
, int dir
)
5992 struct block
*b0
, *b1
, *tmp
;
5994 /* ip6 proto 'proto' */
5995 /* XXX - catch the first fragment of a fragmented packet? */
5996 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6000 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6004 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6008 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6009 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6015 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6016 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6028 static struct block
*
6029 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6032 struct block
*b0
, *b1
, *tmp
;
6034 /* link proto ip6 */
6035 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6041 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6046 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6047 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6049 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6061 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6070 v
= pcap_nametoproto(name
);
6071 if (v
== PROTO_UNDEF
)
6072 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6076 /* XXX should look up h/w protocol type based on cstate->linktype */
6077 v
= pcap_nametoeproto(name
);
6078 if (v
== PROTO_UNDEF
) {
6079 v
= pcap_nametollc(name
);
6080 if (v
== PROTO_UNDEF
)
6081 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6086 if (strcmp(name
, "esis") == 0)
6088 else if (strcmp(name
, "isis") == 0)
6090 else if (strcmp(name
, "clnp") == 0)
6093 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6103 #if !defined(NO_PROTOCHAIN)
6104 static struct block
*
6105 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6107 struct block
*b0
, *b
;
6108 struct slist
*s
[100];
6109 int fix2
, fix3
, fix4
, fix5
;
6110 int ahcheck
, again
, end
;
6112 int reg2
= alloc_reg(cstate
);
6114 memset(s
, 0, sizeof(s
));
6115 fix3
= fix4
= fix5
= 0;
6122 b0
= gen_protochain(cstate
, v
, Q_IP
);
6123 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6127 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6132 * We don't handle variable-length prefixes before the link-layer
6133 * header, or variable-length link-layer headers, here yet.
6134 * We might want to add BPF instructions to do the protochain
6135 * work, to simplify that and, on platforms that have a BPF
6136 * interpreter with the new instructions, let the filtering
6137 * be done in the kernel. (We already require a modified BPF
6138 * engine to do the protochain stuff, to support backward
6139 * branches, and backward branch support is unlikely to appear
6140 * in kernel BPF engines.)
6142 if (cstate
->off_linkpl
.is_variable
)
6143 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6146 * To quote a comment in optimize.c:
6148 * "These data structures are used in a Cocke and Schwartz style
6149 * value numbering scheme. Since the flowgraph is acyclic,
6150 * exit values can be propagated from a node's predecessors
6151 * provided it is uniquely defined."
6153 * "Acyclic" means "no backward branches", which means "no
6154 * loops", so we have to turn the optimizer off.
6156 cstate
->no_optimize
= 1;
6159 * s[0] is a dummy entry to protect other BPF insn from damage
6160 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6161 * hard to find interdependency made by jump table fixup.
6164 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6169 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6172 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6173 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6175 /* X = ip->ip_hl << 2 */
6176 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6177 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6182 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6184 /* A = ip6->ip_nxt */
6185 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6186 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6188 /* X = sizeof(struct ip6_hdr) */
6189 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6195 bpf_error(cstate
, "unsupported proto to gen_protochain");
6199 /* again: if (A == v) goto end; else fall through; */
6201 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6203 s
[i
]->s
.jt
= NULL
; /*later*/
6204 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6208 #ifndef IPPROTO_NONE
6209 #define IPPROTO_NONE 59
6211 /* if (A == IPPROTO_NONE) goto end */
6212 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6213 s
[i
]->s
.jt
= NULL
; /*later*/
6214 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6215 s
[i
]->s
.k
= IPPROTO_NONE
;
6216 s
[fix5
]->s
.jf
= s
[i
];
6220 if (proto
== Q_IPV6
) {
6221 int v6start
, v6end
, v6advance
, j
;
6224 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6225 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6226 s
[i
]->s
.jt
= NULL
; /*later*/
6227 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6228 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6229 s
[fix2
]->s
.jf
= s
[i
];
6231 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6232 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6233 s
[i
]->s
.jt
= NULL
; /*later*/
6234 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6235 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6237 /* if (A == IPPROTO_ROUTING) goto v6advance */
6238 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6239 s
[i
]->s
.jt
= NULL
; /*later*/
6240 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6241 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6243 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6244 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6245 s
[i
]->s
.jt
= NULL
; /*later*/
6246 s
[i
]->s
.jf
= NULL
; /*later*/
6247 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6257 * A = P[X + packet head];
6258 * X = X + (P[X + packet head + 1] + 1) * 8;
6260 /* A = P[X + packet head] */
6261 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6262 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6265 s
[i
] = new_stmt(cstate
, BPF_ST
);
6268 /* A = P[X + packet head + 1]; */
6269 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6270 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6273 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6277 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6281 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6285 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6288 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6292 /* goto again; (must use BPF_JA for backward jump) */
6293 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6294 s
[i
]->s
.k
= again
- i
- 1;
6295 s
[i
- 1]->s
.jf
= s
[i
];
6299 for (j
= v6start
; j
<= v6end
; j
++)
6300 s
[j
]->s
.jt
= s
[v6advance
];
6303 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6305 s
[fix2
]->s
.jf
= s
[i
];
6311 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6312 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6313 s
[i
]->s
.jt
= NULL
; /*later*/
6314 s
[i
]->s
.jf
= NULL
; /*later*/
6315 s
[i
]->s
.k
= IPPROTO_AH
;
6317 s
[fix3
]->s
.jf
= s
[ahcheck
];
6324 * X = X + (P[X + 1] + 2) * 4;
6327 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6329 /* A = P[X + packet head]; */
6330 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6331 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6334 s
[i
] = new_stmt(cstate
, BPF_ST
);
6338 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6341 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6345 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6347 /* A = P[X + packet head] */
6348 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6349 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6352 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6356 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6360 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6363 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6367 /* goto again; (must use BPF_JA for backward jump) */
6368 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6369 s
[i
]->s
.k
= again
- i
- 1;
6374 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6376 s
[fix2
]->s
.jt
= s
[end
];
6377 s
[fix4
]->s
.jf
= s
[end
];
6378 s
[fix5
]->s
.jt
= s
[end
];
6385 for (i
= 0; i
< max
- 1; i
++)
6386 s
[i
]->next
= s
[i
+ 1];
6387 s
[max
- 1]->next
= NULL
;
6392 b
= new_block(cstate
, JMP(BPF_JEQ
));
6393 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6396 free_reg(cstate
, reg2
);
6401 #endif /* !defined(NO_PROTOCHAIN) */
6403 static struct block
*
6404 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6407 struct block
*b0
, *b1
;
6410 * A data frame has the 0x08 bit (b3) in the frame control field set
6411 * and the 0x04 bit (b2) clear.
6413 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6414 b0
= new_block(cstate
, JMP(BPF_JSET
));
6418 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6419 b1
= new_block(cstate
, JMP(BPF_JSET
));
6430 * Generate code that checks whether the packet is a packet for protocol
6431 * <proto> and whether the type field in that protocol's header has
6432 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6433 * IP packet and checks the protocol number in the IP header against <v>.
6435 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6436 * against Q_IP and Q_IPV6.
6438 static struct block
*
6439 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6441 struct block
*b0
, *b1
;
6444 if (dir
!= Q_DEFAULT
)
6445 bpf_error(cstate
, "direction applied to 'proto'");
6449 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6450 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6455 return gen_linktype(cstate
, v
);
6459 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6460 * not LLC encapsulation with LLCSAP_IP.
6462 * For IEEE 802 networks - which includes 802.5 token ring
6463 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6464 * says that SNAP encapsulation is used, not LLC encapsulation
6467 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6468 * RFC 2225 say that SNAP encapsulation is used, not LLC
6469 * encapsulation with LLCSAP_IP.
6471 * So we always check for ETHERTYPE_IP.
6473 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6474 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6479 bpf_error(cstate
, "arp does not encapsulate another protocol");
6483 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6487 bpf_error(cstate
, "'sctp proto' is bogus");
6491 bpf_error(cstate
, "'tcp proto' is bogus");
6495 bpf_error(cstate
, "'udp proto' is bogus");
6499 bpf_error(cstate
, "'icmp proto' is bogus");
6503 bpf_error(cstate
, "'igmp proto' is bogus");
6507 bpf_error(cstate
, "'igrp proto' is bogus");
6511 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6515 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6519 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6523 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6527 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6531 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6535 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6537 * Also check for a fragment header before the final
6540 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6541 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6543 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6549 bpf_error(cstate
, "'icmp6 proto' is bogus");
6553 bpf_error(cstate
, "'ah proto' is bogus");
6557 bpf_error(cstate
, "'esp proto' is bogus");
6561 bpf_error(cstate
, "'pim proto' is bogus");
6565 bpf_error(cstate
, "'vrrp proto' is bogus");
6569 bpf_error(cstate
, "'aarp proto' is bogus");
6573 switch (cstate
->linktype
) {
6577 * Frame Relay packets typically have an OSI
6578 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6579 * generates code to check for all the OSI
6580 * NLPIDs, so calling it and then adding a check
6581 * for the particular NLPID for which we're
6582 * looking is bogus, as we can just check for
6585 * What we check for is the NLPID and a frame
6586 * control field value of UI, i.e. 0x03 followed
6589 * XXX - assumes a 2-byte Frame Relay header with
6590 * DLCI and flags. What if the address is longer?
6592 * XXX - what about SNAP-encapsulated frames?
6594 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6600 * Cisco uses an Ethertype lookalike - for OSI,
6603 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6604 /* OSI in C-HDLC is stuffed with a fudge byte */
6605 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6610 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6611 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6617 bpf_error(cstate
, "'esis proto' is bogus");
6621 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6623 * 4 is the offset of the PDU type relative to the IS-IS
6626 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6631 bpf_error(cstate
, "'clnp proto' is not supported");
6635 bpf_error(cstate
, "'stp proto' is bogus");
6639 bpf_error(cstate
, "'ipx proto' is bogus");
6643 bpf_error(cstate
, "'netbeui proto' is bogus");
6647 bpf_error(cstate
, "'l1 proto' is bogus");
6651 bpf_error(cstate
, "'l2 proto' is bogus");
6655 bpf_error(cstate
, "'iih proto' is bogus");
6659 bpf_error(cstate
, "'snp proto' is bogus");
6663 bpf_error(cstate
, "'csnp proto' is bogus");
6667 bpf_error(cstate
, "'psnp proto' is bogus");
6671 bpf_error(cstate
, "'lsp proto' is bogus");
6675 bpf_error(cstate
, "'radio proto' is bogus");
6679 bpf_error(cstate
, "'carp proto' is bogus");
6690 * Convert a non-numeric name to a port number.
6693 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6695 struct addrinfo hints
, *res
, *ai
;
6697 struct sockaddr_in
*in4
;
6699 struct sockaddr_in6
*in6
;
6704 * We check for both TCP and UDP in case there are
6705 * ambiguous entries.
6707 memset(&hints
, 0, sizeof(hints
));
6708 hints
.ai_family
= PF_UNSPEC
;
6709 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6710 hints
.ai_protocol
= ipproto
;
6711 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6718 * No such port. Just return -1.
6725 * We don't use strerror() because it's not
6726 * guaranteed to be thread-safe on all platforms
6727 * (probably because it might use a non-thread-local
6728 * buffer into which to format an error message
6729 * if the error code isn't one for which it has
6730 * a canned string; three cheers for C string
6733 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6735 port
= -2; /* a real error */
6741 * This is a real error, not just "there's
6742 * no such service name".
6744 * We don't use gai_strerror() because it's not
6745 * guaranteed to be thread-safe on all platforms
6746 * (probably because it might use a non-thread-local
6747 * buffer into which to format an error message
6748 * if the error code isn't one for which it has
6749 * a canned string; three cheers for C string
6752 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6754 port
= -2; /* a real error */
6759 * OK, we found it. Did it find anything?
6761 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6763 * Does it have an address?
6765 if (ai
->ai_addr
!= NULL
) {
6767 * Yes. Get a port number; we're done.
6769 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6770 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6771 port
= ntohs(in4
->sin_port
);
6775 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6776 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6777 port
= ntohs(in6
->sin6_port
);
6789 * Convert a string to a port number.
6792 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6802 * See if it's a number.
6804 ret
= stoulen(string
, string_size
, &val
, cstate
);
6808 /* Unknown port type - it's just a number. */
6809 *proto
= PROTO_UNDEF
;
6812 case STOULEN_NOT_OCTAL_NUMBER
:
6813 case STOULEN_NOT_HEX_NUMBER
:
6814 case STOULEN_NOT_DECIMAL_NUMBER
:
6816 * Not a valid number; try looking it up as a port.
6818 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6819 memcpy(cpy
, string
, string_size
);
6820 cpy
[string_size
] = '\0';
6821 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6822 if (tcp_port
== -2) {
6824 * We got a hard error; the error string has
6828 longjmp(cstate
->top_ctx
, 1);
6831 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6832 if (udp_port
== -2) {
6834 * We got a hard error; the error string has
6838 longjmp(cstate
->top_ctx
, 1);
6843 * We need to check /etc/services for ambiguous entries.
6844 * If we find an ambiguous entry, and it has the
6845 * same port number, change the proto to PROTO_UNDEF
6846 * so both TCP and UDP will be checked.
6848 if (tcp_port
>= 0) {
6849 val
= (bpf_u_int32
)tcp_port
;
6850 *proto
= IPPROTO_TCP
;
6851 if (udp_port
>= 0) {
6852 if (udp_port
== tcp_port
)
6853 *proto
= PROTO_UNDEF
;
6856 /* Can't handle ambiguous names that refer
6857 to different port numbers. */
6858 warning("ambiguous port %s in /etc/services",
6865 if (udp_port
>= 0) {
6866 val
= (bpf_u_int32
)udp_port
;
6867 *proto
= IPPROTO_UDP
;
6871 #if defined(ultrix) || defined(__osf__)
6872 /* Special hack in case NFS isn't in /etc/services */
6873 if (strcmp(cpy
, "nfs") == 0) {
6875 *proto
= PROTO_UNDEF
;
6880 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6882 longjmp(cstate
->top_ctx
, 1);
6886 /* Error already set. */
6887 longjmp(cstate
->top_ctx
, 1);
6891 /* Should not happen */
6892 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6893 longjmp(cstate
->top_ctx
, 1);
6900 * Convert a string in the form PPP-PPP, which correspond to ports, to
6901 * a starting and ending port in a port range.
6904 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6905 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6908 const char *first
, *second
;
6909 size_t first_size
, second_size
;
6912 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6913 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6916 * Make sure there are no other hyphens.
6918 * XXX - we support named ports, but there are some port names
6919 * in /etc/services that include hyphens, so this would rule
6922 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6923 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6927 * Get the length of the first port.
6930 first_size
= hyphen_off
- string
;
6931 if (first_size
== 0) {
6932 /* Range of "-port", which we don't support. */
6933 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6937 * Try to convert it to a port.
6939 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6940 save_proto
= *proto
;
6943 * Get the length of the second port.
6945 second
= hyphen_off
+ 1;
6946 second_size
= strlen(second
);
6947 if (second_size
== 0) {
6948 /* Range of "port-", which we don't support. */
6949 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6953 * Try to convert it to a port.
6955 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6956 if (*proto
!= save_proto
)
6957 *proto
= PROTO_UNDEF
;
6961 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6963 int proto
= q
.proto
;
6967 bpf_u_int32 mask
, addr
;
6968 struct addrinfo
*res
, *res0
;
6969 struct sockaddr_in
*sin4
;
6972 struct sockaddr_in6
*sin6
;
6973 struct in6_addr mask128
;
6975 struct block
*b
, *tmp
;
6976 int port
, real_proto
;
6977 bpf_u_int32 port1
, port2
;
6980 * Catch errors reported by us and routines below us, and return NULL
6983 if (setjmp(cstate
->top_ctx
))
6989 addr
= pcap_nametonetaddr(name
);
6991 bpf_error(cstate
, "unknown network '%s'", name
);
6992 /* Left justify network addr and calculate its network mask */
6994 while (addr
&& (addr
& 0xff000000) == 0) {
6998 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7002 if (proto
== Q_LINK
) {
7003 switch (cstate
->linktype
) {
7006 case DLT_NETANALYZER
:
7007 case DLT_NETANALYZER_TRANSPARENT
:
7008 eaddr
= pcap_ether_hostton(name
);
7011 "unknown ether host '%s'", name
);
7012 tmp
= gen_prevlinkhdr_check(cstate
);
7013 b
= gen_ehostop(cstate
, eaddr
, dir
);
7020 eaddr
= pcap_ether_hostton(name
);
7023 "unknown FDDI host '%s'", name
);
7024 b
= gen_fhostop(cstate
, eaddr
, dir
);
7029 eaddr
= pcap_ether_hostton(name
);
7032 "unknown token ring host '%s'", name
);
7033 b
= gen_thostop(cstate
, eaddr
, dir
);
7037 case DLT_IEEE802_11
:
7038 case DLT_PRISM_HEADER
:
7039 case DLT_IEEE802_11_RADIO_AVS
:
7040 case DLT_IEEE802_11_RADIO
:
7042 eaddr
= pcap_ether_hostton(name
);
7045 "unknown 802.11 host '%s'", name
);
7046 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7050 case DLT_IP_OVER_FC
:
7051 eaddr
= pcap_ether_hostton(name
);
7054 "unknown Fibre Channel host '%s'", name
);
7055 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7060 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7061 } else if (proto
== Q_DECNET
) {
7063 * A long time ago on Ultrix libpcap supported
7064 * translation of DECnet host names into DECnet
7065 * addresses, but this feature is history now.
7067 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7070 memset(&mask128
, 0xff, sizeof(mask128
));
7072 res0
= res
= pcap_nametoaddrinfo(name
);
7074 bpf_error(cstate
, "unknown host '%s'", name
);
7081 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7082 tproto
== Q_DEFAULT
) {
7088 for (res
= res0
; res
; res
= res
->ai_next
) {
7089 switch (res
->ai_family
) {
7092 if (tproto
== Q_IPV6
)
7096 sin4
= (struct sockaddr_in
*)
7098 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7099 0xffffffff, tproto
, dir
, q
.addr
);
7103 if (tproto6
== Q_IP
)
7106 sin6
= (struct sockaddr_in6
*)
7108 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7109 &mask128
, tproto6
, dir
, q
.addr
);
7122 bpf_error(cstate
, "unknown host '%s'%s", name
,
7123 (proto
== Q_DEFAULT
)
7125 : " for specified address family");
7131 if (proto
!= Q_DEFAULT
&&
7132 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7133 bpf_error(cstate
, "illegal qualifier of 'port'");
7134 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7135 bpf_error(cstate
, "unknown port '%s'", name
);
7136 if (proto
== Q_UDP
) {
7137 if (real_proto
== IPPROTO_TCP
)
7138 bpf_error(cstate
, "port '%s' is tcp", name
);
7139 else if (real_proto
== IPPROTO_SCTP
)
7140 bpf_error(cstate
, "port '%s' is sctp", name
);
7142 /* override PROTO_UNDEF */
7143 real_proto
= IPPROTO_UDP
;
7145 if (proto
== Q_TCP
) {
7146 if (real_proto
== IPPROTO_UDP
)
7147 bpf_error(cstate
, "port '%s' is udp", name
);
7149 else if (real_proto
== IPPROTO_SCTP
)
7150 bpf_error(cstate
, "port '%s' is sctp", name
);
7152 /* override PROTO_UNDEF */
7153 real_proto
= IPPROTO_TCP
;
7155 if (proto
== Q_SCTP
) {
7156 if (real_proto
== IPPROTO_UDP
)
7157 bpf_error(cstate
, "port '%s' is udp", name
);
7159 else if (real_proto
== IPPROTO_TCP
)
7160 bpf_error(cstate
, "port '%s' is tcp", name
);
7162 /* override PROTO_UNDEF */
7163 real_proto
= IPPROTO_SCTP
;
7166 bpf_error(cstate
, "illegal port number %d < 0", port
);
7168 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7169 b
= gen_port(cstate
, port
, real_proto
, dir
);
7170 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7174 if (proto
!= Q_DEFAULT
&&
7175 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7176 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7177 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7178 if (proto
== Q_UDP
) {
7179 if (real_proto
== IPPROTO_TCP
)
7180 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7181 else if (real_proto
== IPPROTO_SCTP
)
7182 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7184 /* override PROTO_UNDEF */
7185 real_proto
= IPPROTO_UDP
;
7187 if (proto
== Q_TCP
) {
7188 if (real_proto
== IPPROTO_UDP
)
7189 bpf_error(cstate
, "port in range '%s' is udp", name
);
7190 else if (real_proto
== IPPROTO_SCTP
)
7191 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7193 /* override PROTO_UNDEF */
7194 real_proto
= IPPROTO_TCP
;
7196 if (proto
== Q_SCTP
) {
7197 if (real_proto
== IPPROTO_UDP
)
7198 bpf_error(cstate
, "port in range '%s' is udp", name
);
7199 else if (real_proto
== IPPROTO_TCP
)
7200 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7202 /* override PROTO_UNDEF */
7203 real_proto
= IPPROTO_SCTP
;
7206 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7208 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7210 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7211 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7216 eaddr
= pcap_ether_hostton(name
);
7218 bpf_error(cstate
, "unknown ether host: %s", name
);
7220 res
= pcap_nametoaddrinfo(name
);
7223 bpf_error(cstate
, "unknown host '%s'", name
);
7224 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7228 bpf_error(cstate
, "unknown host '%s'", name
);
7231 bpf_error(cstate
, "'gateway' not supported in this configuration");
7235 real_proto
= lookup_proto(cstate
, name
, proto
);
7236 if (real_proto
>= 0)
7237 return gen_proto(cstate
, real_proto
, proto
, dir
);
7239 bpf_error(cstate
, "unknown protocol: %s", name
);
7241 #if !defined(NO_PROTOCHAIN)
7243 real_proto
= lookup_proto(cstate
, name
, proto
);
7244 if (real_proto
>= 0)
7245 return gen_protochain(cstate
, real_proto
, proto
);
7247 bpf_error(cstate
, "unknown protocol: %s", name
);
7248 #endif /* !defined(NO_PROTOCHAIN) */
7259 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7260 bpf_u_int32 masklen
, struct qual q
)
7262 register int nlen
, mlen
;
7266 * Catch errors reported by us and routines below us, and return NULL
7269 if (setjmp(cstate
->top_ctx
))
7272 nlen
= __pcap_atoin(s1
, &n
);
7274 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7275 /* Promote short ipaddr */
7279 mlen
= __pcap_atoin(s2
, &m
);
7281 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7282 /* Promote short ipaddr */
7285 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7288 /* Convert mask len to mask */
7290 bpf_error(cstate
, "mask length must be <= 32");
7293 * X << 32 is not guaranteed by C to be 0; it's
7298 m
= 0xffffffff << (32 - masklen
);
7300 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7307 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7310 bpf_error(cstate
, "Mask syntax for networks only");
7317 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7325 * Catch errors reported by us and routines below us, and return NULL
7328 if (setjmp(cstate
->top_ctx
))
7335 * v contains a 32-bit unsigned parsed from a string of the
7336 * form {N}, which could be decimal, hexadecimal or octal.
7337 * Although it would be possible to use the value as a raw
7338 * 16-bit DECnet address when the value fits into 16 bits, this
7339 * would be a questionable feature: DECnet address wire
7340 * encoding is little-endian, so this would not work as
7341 * intuitively as the same works for [big-endian] IPv4
7342 * addresses (0x01020304 means 1.2.3.4).
7344 if (proto
== Q_DECNET
)
7345 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7347 } else if (proto
== Q_DECNET
) {
7349 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7350 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7351 * for a valid DECnet address.
7353 vlen
= __pcap_atodn(s
, &v
);
7355 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7358 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7359 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7362 vlen
= __pcap_atoin(s
, &v
);
7364 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7372 if (proto
== Q_DECNET
)
7373 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7374 else if (proto
== Q_LINK
) {
7375 bpf_error(cstate
, "illegal link layer address");
7378 if (s
== NULL
&& q
.addr
== Q_NET
) {
7379 /* Promote short net number */
7380 while (v
&& (v
& 0xff000000) == 0) {
7385 /* Promote short ipaddr */
7387 mask
<<= 32 - vlen
;
7389 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7394 proto
= IPPROTO_UDP
;
7395 else if (proto
== Q_TCP
)
7396 proto
= IPPROTO_TCP
;
7397 else if (proto
== Q_SCTP
)
7398 proto
= IPPROTO_SCTP
;
7399 else if (proto
== Q_DEFAULT
)
7400 proto
= PROTO_UNDEF
;
7402 bpf_error(cstate
, "illegal qualifier of 'port'");
7405 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7409 b
= gen_port(cstate
, v
, proto
, dir
);
7410 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7416 proto
= IPPROTO_UDP
;
7417 else if (proto
== Q_TCP
)
7418 proto
= IPPROTO_TCP
;
7419 else if (proto
== Q_SCTP
)
7420 proto
= IPPROTO_SCTP
;
7421 else if (proto
== Q_DEFAULT
)
7422 proto
= PROTO_UNDEF
;
7424 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7427 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7431 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7432 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7437 bpf_error(cstate
, "'gateway' requires a name");
7441 return gen_proto(cstate
, v
, proto
, dir
);
7443 #if !defined(NO_PROTOCHAIN)
7445 return gen_protochain(cstate
, v
, proto
);
7461 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7464 struct addrinfo
*res
;
7465 struct in6_addr
*addr
;
7466 struct in6_addr mask
;
7468 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7471 * Catch errors reported by us and routines below us, and return NULL
7474 if (setjmp(cstate
->top_ctx
))
7477 res
= pcap_nametoaddrinfo(s
);
7479 bpf_error(cstate
, "invalid ip6 address %s", s
);
7482 bpf_error(cstate
, "%s resolved to multiple address", s
);
7483 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7485 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7486 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7487 memset(&mask
, 0, sizeof(mask
));
7488 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7490 mask
.s6_addr
[masklen
/ 8] =
7491 (0xff << (8 - masklen
% 8)) & 0xff;
7494 memcpy(a
, addr
, sizeof(a
));
7495 memcpy(m
, &mask
, sizeof(m
));
7496 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7497 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7498 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7506 bpf_error(cstate
, "Mask syntax for networks only");
7510 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7516 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7523 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7525 struct block
*b
, *tmp
;
7528 * Catch errors reported by us and routines below us, and return NULL
7531 if (setjmp(cstate
->top_ctx
))
7534 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7535 cstate
->e
= pcap_ether_aton(s
);
7536 if (cstate
->e
== NULL
)
7537 bpf_error(cstate
, "malloc");
7538 switch (cstate
->linktype
) {
7540 case DLT_NETANALYZER
:
7541 case DLT_NETANALYZER_TRANSPARENT
:
7542 tmp
= gen_prevlinkhdr_check(cstate
);
7543 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7548 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7551 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7553 case DLT_IEEE802_11
:
7554 case DLT_PRISM_HEADER
:
7555 case DLT_IEEE802_11_RADIO_AVS
:
7556 case DLT_IEEE802_11_RADIO
:
7558 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7560 case DLT_IP_OVER_FC
:
7561 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7566 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7573 bpf_error(cstate
, "ethernet address used in non-ether expression");
7578 sappend(struct slist
*s0
, struct slist
*s1
)
7581 * This is definitely not the best way to do this, but the
7582 * lists will rarely get long.
7589 static struct slist
*
7590 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7594 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7599 static struct slist
*
7600 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7604 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7610 * Modify "index" to use the value stored into its register as an
7611 * offset relative to the beginning of the header for the protocol
7612 * "proto", and allocate a register and put an item "size" bytes long
7613 * (1, 2, or 4) at that offset into that register, making it the register
7616 static struct arth
*
7617 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7621 struct slist
*s
, *tmp
;
7623 int regno
= alloc_reg(cstate
);
7625 free_reg(cstate
, inst
->regno
);
7629 bpf_error(cstate
, "data size must be 1, 2, or 4");
7646 bpf_error(cstate
, "unsupported index operation");
7650 * The offset is relative to the beginning of the packet
7651 * data, if we have a radio header. (If we don't, this
7654 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7655 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7656 cstate
->linktype
!= DLT_PRISM_HEADER
)
7657 bpf_error(cstate
, "radio information not present in capture");
7660 * Load into the X register the offset computed into the
7661 * register specified by "index".
7663 s
= xfer_to_x(cstate
, inst
);
7666 * Load the item at that offset.
7668 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7670 sappend(inst
->s
, s
);
7675 * The offset is relative to the beginning of
7676 * the link-layer header.
7678 * XXX - what about ATM LANE? Should the index be
7679 * relative to the beginning of the AAL5 frame, so
7680 * that 0 refers to the beginning of the LE Control
7681 * field, or relative to the beginning of the LAN
7682 * frame, so that 0 refers, for Ethernet LANE, to
7683 * the beginning of the destination address?
7685 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7688 * If "s" is non-null, it has code to arrange that the
7689 * X register contains the length of the prefix preceding
7690 * the link-layer header. Add to it the offset computed
7691 * into the register specified by "index", and move that
7692 * into the X register. Otherwise, just load into the X
7693 * register the offset computed into the register specified
7697 sappend(s
, xfer_to_a(cstate
, inst
));
7698 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7699 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7701 s
= xfer_to_x(cstate
, inst
);
7704 * Load the item at the sum of the offset we've put in the
7705 * X register and the offset of the start of the link
7706 * layer header (which is 0 if the radio header is
7707 * variable-length; that header length is what we put
7708 * into the X register and then added to the index).
7710 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7711 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7713 sappend(inst
->s
, s
);
7727 * The offset is relative to the beginning of
7728 * the network-layer header.
7729 * XXX - are there any cases where we want
7730 * cstate->off_nl_nosnap?
7732 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7735 * If "s" is non-null, it has code to arrange that the
7736 * X register contains the variable part of the offset
7737 * of the link-layer payload. Add to it the offset
7738 * computed into the register specified by "index",
7739 * and move that into the X register. Otherwise, just
7740 * load into the X register the offset computed into
7741 * the register specified by "index".
7744 sappend(s
, xfer_to_a(cstate
, inst
));
7745 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7746 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7748 s
= xfer_to_x(cstate
, inst
);
7751 * Load the item at the sum of the offset we've put in the
7752 * X register, the offset of the start of the network
7753 * layer header from the beginning of the link-layer
7754 * payload, and the constant part of the offset of the
7755 * start of the link-layer payload.
7757 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7758 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7760 sappend(inst
->s
, s
);
7763 * Do the computation only if the packet contains
7764 * the protocol in question.
7766 b
= gen_proto_abbrev_internal(cstate
, proto
);
7768 gen_and(inst
->b
, b
);
7782 * The offset is relative to the beginning of
7783 * the transport-layer header.
7785 * Load the X register with the length of the IPv4 header
7786 * (plus the offset of the link-layer header, if it's
7787 * a variable-length header), in bytes.
7789 * XXX - are there any cases where we want
7790 * cstate->off_nl_nosnap?
7791 * XXX - we should, if we're built with
7792 * IPv6 support, generate code to load either
7793 * IPv4, IPv6, or both, as appropriate.
7795 s
= gen_loadx_iphdrlen(cstate
);
7798 * The X register now contains the sum of the variable
7799 * part of the offset of the link-layer payload and the
7800 * length of the network-layer header.
7802 * Load into the A register the offset relative to
7803 * the beginning of the transport layer header,
7804 * add the X register to that, move that to the
7805 * X register, and load with an offset from the
7806 * X register equal to the sum of the constant part of
7807 * the offset of the link-layer payload and the offset,
7808 * relative to the beginning of the link-layer payload,
7809 * of the network-layer header.
7811 sappend(s
, xfer_to_a(cstate
, inst
));
7812 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7813 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7814 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7815 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7816 sappend(inst
->s
, s
);
7819 * Do the computation only if the packet contains
7820 * the protocol in question - which is true only
7821 * if this is an IP datagram and is the first or
7822 * only fragment of that datagram.
7824 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7826 gen_and(inst
->b
, b
);
7827 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7832 * Do the computation only if the packet contains
7833 * the protocol in question.
7835 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7837 gen_and(inst
->b
, b
);
7842 * Check if we have an icmp6 next header
7844 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7846 gen_and(inst
->b
, b
);
7851 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7853 * If "s" is non-null, it has code to arrange that the
7854 * X register contains the variable part of the offset
7855 * of the link-layer payload. Add to it the offset
7856 * computed into the register specified by "index",
7857 * and move that into the X register. Otherwise, just
7858 * load into the X register the offset computed into
7859 * the register specified by "index".
7862 sappend(s
, xfer_to_a(cstate
, inst
));
7863 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7864 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7866 s
= xfer_to_x(cstate
, inst
);
7870 * Load the item at the sum of the offset we've put in the
7871 * X register, the offset of the start of the network
7872 * layer header from the beginning of the link-layer
7873 * payload, and the constant part of the offset of the
7874 * start of the link-layer payload.
7876 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7877 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7880 sappend(inst
->s
, s
);
7884 inst
->regno
= regno
;
7885 s
= new_stmt(cstate
, BPF_ST
);
7887 sappend(inst
->s
, s
);
7893 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7897 * Catch errors reported by us and routines below us, and return NULL
7900 if (setjmp(cstate
->top_ctx
))
7903 return gen_load_internal(cstate
, proto
, inst
, size
);
7906 static struct block
*
7907 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7908 struct arth
*a1
, int reversed
)
7910 struct slist
*s0
, *s1
, *s2
;
7911 struct block
*b
, *tmp
;
7913 s0
= xfer_to_x(cstate
, a1
);
7914 s1
= xfer_to_a(cstate
, a0
);
7915 if (code
== BPF_JEQ
) {
7916 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7917 b
= new_block(cstate
, JMP(code
));
7921 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7927 sappend(a0
->s
, a1
->s
);
7931 free_reg(cstate
, a0
->regno
);
7932 free_reg(cstate
, a1
->regno
);
7934 /* 'and' together protocol checks */
7937 gen_and(a0
->b
, tmp
= a1
->b
);
7951 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7952 struct arth
*a1
, int reversed
)
7955 * Catch errors reported by us and routines below us, and return NULL
7958 if (setjmp(cstate
->top_ctx
))
7961 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7965 gen_loadlen(compiler_state_t
*cstate
)
7972 * Catch errors reported by us and routines below us, and return NULL
7975 if (setjmp(cstate
->top_ctx
))
7978 regno
= alloc_reg(cstate
);
7979 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7980 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7981 s
->next
= new_stmt(cstate
, BPF_ST
);
7982 s
->next
->s
.k
= regno
;
7989 static struct arth
*
7990 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7996 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7998 reg
= alloc_reg(cstate
);
8000 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8002 s
->next
= new_stmt(cstate
, BPF_ST
);
8011 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8014 * Catch errors reported by us and routines below us, and return NULL
8017 if (setjmp(cstate
->top_ctx
))
8020 return gen_loadi_internal(cstate
, val
);
8024 * The a_arg dance is to avoid annoying whining by compilers that
8025 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8026 * It's not *used* after setjmp returns.
8029 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8031 struct arth
*a
= a_arg
;
8035 * Catch errors reported by us and routines below us, and return NULL
8038 if (setjmp(cstate
->top_ctx
))
8041 s
= xfer_to_a(cstate
, a
);
8043 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8046 s
= new_stmt(cstate
, BPF_ST
);
8054 * The a0_arg dance is to avoid annoying whining by compilers that
8055 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8056 * It's not *used* after setjmp returns.
8059 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8062 struct arth
*a0
= a0_arg
;
8063 struct slist
*s0
, *s1
, *s2
;
8066 * Catch errors reported by us and routines below us, and return NULL
8069 if (setjmp(cstate
->top_ctx
))
8073 * Disallow division by, or modulus by, zero; we do this here
8074 * so that it gets done even if the optimizer is disabled.
8076 * Also disallow shifts by a value greater than 31; we do this
8077 * here, for the same reason.
8079 if (code
== BPF_DIV
) {
8080 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8081 bpf_error(cstate
, "division by zero");
8082 } else if (code
== BPF_MOD
) {
8083 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8084 bpf_error(cstate
, "modulus by zero");
8085 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8086 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8087 bpf_error(cstate
, "shift by more than 31 bits");
8089 s0
= xfer_to_x(cstate
, a1
);
8090 s1
= xfer_to_a(cstate
, a0
);
8091 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8096 sappend(a0
->s
, a1
->s
);
8098 free_reg(cstate
, a0
->regno
);
8099 free_reg(cstate
, a1
->regno
);
8101 s0
= new_stmt(cstate
, BPF_ST
);
8102 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8109 * Initialize the table of used registers and the current register.
8112 init_regs(compiler_state_t
*cstate
)
8115 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8119 * Return the next free register.
8122 alloc_reg(compiler_state_t
*cstate
)
8124 int n
= BPF_MEMWORDS
;
8127 if (cstate
->regused
[cstate
->curreg
])
8128 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8130 cstate
->regused
[cstate
->curreg
] = 1;
8131 return cstate
->curreg
;
8134 bpf_error(cstate
, "too many registers needed to evaluate expression");
8139 * Return a register to the table so it can
8143 free_reg(compiler_state_t
*cstate
, int n
)
8145 cstate
->regused
[n
] = 0;
8148 static struct block
*
8149 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8154 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8155 b
= new_block(cstate
, JMP(jmp
));
8163 gen_greater(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 return gen_len(cstate
, BPF_JGE
, n
);
8176 * Actually, this is less than or equal.
8179 gen_less(compiler_state_t
*cstate
, int n
)
8184 * Catch errors reported by us and routines below us, and return NULL
8187 if (setjmp(cstate
->top_ctx
))
8190 b
= gen_len(cstate
, BPF_JGT
, n
);
8197 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8198 * the beginning of the link-layer header.
8199 * XXX - that means you can't test values in the radiotap header, but
8200 * as that header is difficult if not impossible to parse generally
8201 * without a loop, that might not be a severe problem. A new keyword
8202 * "radio" could be added for that, although what you'd really want
8203 * would be a way of testing particular radio header values, which
8204 * would generate code appropriate to the radio header in question.
8207 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8213 * Catch errors reported by us and routines below us, and return NULL
8216 if (setjmp(cstate
->top_ctx
))
8224 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8227 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8231 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8235 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8239 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8243 b
= new_block(cstate
, JMP(BPF_JEQ
));
8250 static const u_char abroadcast
[] = { 0x0 };
8253 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8255 bpf_u_int32 hostmask
;
8256 struct block
*b0
, *b1
, *b2
;
8257 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8260 * Catch errors reported by us and routines below us, and return NULL
8263 if (setjmp(cstate
->top_ctx
))
8270 switch (cstate
->linktype
) {
8272 case DLT_ARCNET_LINUX
:
8273 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8275 case DLT_NETANALYZER
:
8276 case DLT_NETANALYZER_TRANSPARENT
:
8277 b1
= gen_prevlinkhdr_check(cstate
);
8278 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8283 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8285 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8286 case DLT_IEEE802_11
:
8287 case DLT_PRISM_HEADER
:
8288 case DLT_IEEE802_11_RADIO_AVS
:
8289 case DLT_IEEE802_11_RADIO
:
8291 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8292 case DLT_IP_OVER_FC
:
8293 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8295 bpf_error(cstate
, "not a broadcast link");
8301 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8302 * as an indication that we don't know the netmask, and fail
8305 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8306 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8307 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8308 hostmask
= ~cstate
->netmask
;
8309 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8310 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8311 ~0 & hostmask
, hostmask
);
8316 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8321 * Generate code to test the low-order bit of a MAC address (that's
8322 * the bottom bit of the *first* byte).
8324 static struct block
*
8325 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8327 register struct block
*b0
;
8328 register struct slist
*s
;
8330 /* link[offset] & 1 != 0 */
8331 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8332 b0
= new_block(cstate
, JMP(BPF_JSET
));
8339 gen_multicast(compiler_state_t
*cstate
, int proto
)
8341 register struct block
*b0
, *b1
, *b2
;
8342 register struct slist
*s
;
8345 * Catch errors reported by us and routines below us, and return NULL
8348 if (setjmp(cstate
->top_ctx
))
8355 switch (cstate
->linktype
) {
8357 case DLT_ARCNET_LINUX
:
8358 /* all ARCnet multicasts use the same address */
8359 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8361 case DLT_NETANALYZER
:
8362 case DLT_NETANALYZER_TRANSPARENT
:
8363 b1
= gen_prevlinkhdr_check(cstate
);
8364 /* ether[0] & 1 != 0 */
8365 b0
= gen_mac_multicast(cstate
, 0);
8371 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8373 * XXX - was that referring to bit-order issues?
8375 /* fddi[1] & 1 != 0 */
8376 return gen_mac_multicast(cstate
, 1);
8378 /* tr[2] & 1 != 0 */
8379 return gen_mac_multicast(cstate
, 2);
8380 case DLT_IEEE802_11
:
8381 case DLT_PRISM_HEADER
:
8382 case DLT_IEEE802_11_RADIO_AVS
:
8383 case DLT_IEEE802_11_RADIO
:
8388 * For control frames, there is no DA.
8390 * For management frames, DA is at an
8391 * offset of 4 from the beginning of
8394 * For data frames, DA is at an offset
8395 * of 4 from the beginning of the packet
8396 * if To DS is clear and at an offset of
8397 * 16 from the beginning of the packet
8402 * Generate the tests to be done for data frames.
8404 * First, check for To DS set, i.e. "link[1] & 0x01".
8406 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8407 b1
= new_block(cstate
, JMP(BPF_JSET
));
8408 b1
->s
.k
= 0x01; /* To DS */
8412 * If To DS is set, the DA is at 16.
8414 b0
= gen_mac_multicast(cstate
, 16);
8418 * Now, check for To DS not set, i.e. check
8419 * "!(link[1] & 0x01)".
8421 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8422 b2
= new_block(cstate
, JMP(BPF_JSET
));
8423 b2
->s
.k
= 0x01; /* To DS */
8428 * If To DS is not set, the DA is at 4.
8430 b1
= gen_mac_multicast(cstate
, 4);
8434 * Now OR together the last two checks. That gives
8435 * the complete set of checks for data frames.
8440 * Now check for a data frame.
8441 * I.e, check "link[0] & 0x08".
8443 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8444 b1
= new_block(cstate
, JMP(BPF_JSET
));
8449 * AND that with the checks done for data frames.
8454 * If the high-order bit of the type value is 0, this
8455 * is a management frame.
8456 * I.e, check "!(link[0] & 0x08)".
8458 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8459 b2
= new_block(cstate
, JMP(BPF_JSET
));
8465 * For management frames, the DA is at 4.
8467 b1
= gen_mac_multicast(cstate
, 4);
8471 * OR that with the checks done for data frames.
8472 * That gives the checks done for management and
8478 * If the low-order bit of the type value is 1,
8479 * this is either a control frame or a frame
8480 * with a reserved type, and thus not a
8483 * I.e., check "!(link[0] & 0x04)".
8485 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8486 b1
= new_block(cstate
, JMP(BPF_JSET
));
8492 * AND that with the checks for data and management
8497 case DLT_IP_OVER_FC
:
8498 b0
= gen_mac_multicast(cstate
, 2);
8503 /* Link not known to support multicasts */
8507 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8508 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8513 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8514 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8518 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8523 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8525 register struct block
*b0
;
8528 * Catch errors reported by us and routines below us, and return NULL
8531 if (setjmp(cstate
->top_ctx
))
8535 * Only some data link types support ifindex qualifiers.
8537 switch (cstate
->linktype
) {
8538 case DLT_LINUX_SLL2
:
8539 /* match packets on this interface */
8540 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8545 * This is Linux; we require PF_PACKET support.
8546 * If this is a *live* capture, we can look at
8547 * special meta-data in the filter expression;
8548 * if it's a savefile, we can't.
8550 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8551 /* We have a FILE *, so this is a savefile */
8552 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8553 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8557 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8559 #else /* defined(linux) */
8560 bpf_error(cstate
, "ifindex not supported on %s",
8561 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8563 #endif /* defined(linux) */
8569 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8570 * Outbound traffic is sent by this machine, while inbound traffic is
8571 * sent by a remote machine (and may include packets destined for a
8572 * unicast or multicast link-layer address we are not subscribing to).
8573 * These are the same definitions implemented by pcap_setdirection().
8574 * Capturing only unicast traffic destined for this host is probably
8575 * better accomplished using a higher-layer filter.
8578 gen_inbound(compiler_state_t
*cstate
, int dir
)
8580 register struct block
*b0
;
8583 * Catch errors reported by us and routines below us, and return NULL
8586 if (setjmp(cstate
->top_ctx
))
8590 * Only some data link types support inbound/outbound qualifiers.
8592 switch (cstate
->linktype
) {
8594 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8595 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8596 gen_loadi_internal(cstate
, 0),
8602 /* match outgoing packets */
8603 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8605 /* match incoming packets */
8606 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8611 /* match outgoing packets */
8612 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8614 /* to filter on inbound traffic, invert the match */
8619 case DLT_LINUX_SLL2
:
8620 /* match outgoing packets */
8621 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8623 /* to filter on inbound traffic, invert the match */
8629 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8630 ((dir
== 0) ? PF_IN
: PF_OUT
));
8635 /* match outgoing packets */
8636 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8638 /* match incoming packets */
8639 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8643 case DLT_JUNIPER_MFR
:
8644 case DLT_JUNIPER_MLFR
:
8645 case DLT_JUNIPER_MLPPP
:
8646 case DLT_JUNIPER_ATM1
:
8647 case DLT_JUNIPER_ATM2
:
8648 case DLT_JUNIPER_PPPOE
:
8649 case DLT_JUNIPER_PPPOE_ATM
:
8650 case DLT_JUNIPER_GGSN
:
8651 case DLT_JUNIPER_ES
:
8652 case DLT_JUNIPER_MONITOR
:
8653 case DLT_JUNIPER_SERVICES
:
8654 case DLT_JUNIPER_ETHER
:
8655 case DLT_JUNIPER_PPP
:
8656 case DLT_JUNIPER_FRELAY
:
8657 case DLT_JUNIPER_CHDLC
:
8658 case DLT_JUNIPER_VP
:
8659 case DLT_JUNIPER_ST
:
8660 case DLT_JUNIPER_ISM
:
8661 case DLT_JUNIPER_VS
:
8662 case DLT_JUNIPER_SRX_E2E
:
8663 case DLT_JUNIPER_FIBRECHANNEL
:
8664 case DLT_JUNIPER_ATM_CEMIC
:
8666 /* juniper flags (including direction) are stored
8667 * the byte after the 3-byte magic number */
8669 /* match outgoing packets */
8670 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8672 /* match incoming packets */
8673 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8679 * If we have packet meta-data indicating a direction,
8680 * and that metadata can be checked by BPF code, check
8681 * it. Otherwise, give up, as this link-layer type has
8682 * nothing in the packet data.
8684 * Currently, the only platform where a BPF filter can
8685 * check that metadata is Linux with the in-kernel
8686 * BPF interpreter. If other packet capture mechanisms
8687 * and BPF filters also supported this, it would be
8688 * nice. It would be even better if they made that
8689 * metadata available so that we could provide it
8690 * with newer capture APIs, allowing it to be saved
8695 * This is Linux; we require PF_PACKET support.
8696 * If this is a *live* capture, we can look at
8697 * special meta-data in the filter expression;
8698 * if it's a savefile, we can't.
8700 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8701 /* We have a FILE *, so this is a savefile */
8702 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8703 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8706 /* match outgoing packets */
8707 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8710 /* to filter on inbound traffic, invert the match */
8713 #else /* defined(linux) */
8714 bpf_error(cstate
, "inbound/outbound not supported on %s",
8715 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8717 #endif /* defined(linux) */
8722 /* PF firewall log matched interface */
8724 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8730 * Catch errors reported by us and routines below us, and return NULL
8733 if (setjmp(cstate
->top_ctx
))
8736 if (cstate
->linktype
!= DLT_PFLOG
) {
8737 bpf_error(cstate
, "ifname supported only on PF linktype");
8740 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8741 off
= offsetof(struct pfloghdr
, ifname
);
8742 if (strlen(ifname
) >= len
) {
8743 bpf_error(cstate
, "ifname interface names can only be %d characters",
8747 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8748 (const u_char
*)ifname
);
8752 /* PF firewall log ruleset name */
8754 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8759 * Catch errors reported by us and routines below us, and return NULL
8762 if (setjmp(cstate
->top_ctx
))
8765 if (cstate
->linktype
!= DLT_PFLOG
) {
8766 bpf_error(cstate
, "ruleset supported only on PF linktype");
8770 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8771 bpf_error(cstate
, "ruleset names can only be %ld characters",
8772 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8776 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8777 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8781 /* PF firewall log rule number */
8783 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8788 * Catch errors reported by us and routines below us, and return NULL
8791 if (setjmp(cstate
->top_ctx
))
8794 if (cstate
->linktype
!= DLT_PFLOG
) {
8795 bpf_error(cstate
, "rnr supported only on PF linktype");
8799 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8804 /* PF firewall log sub-rule number */
8806 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8811 * Catch errors reported by us and routines below us, and return NULL
8814 if (setjmp(cstate
->top_ctx
))
8817 if (cstate
->linktype
!= DLT_PFLOG
) {
8818 bpf_error(cstate
, "srnr supported only on PF linktype");
8822 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8827 /* PF firewall log reason code */
8829 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8834 * Catch errors reported by us and routines below us, and return NULL
8837 if (setjmp(cstate
->top_ctx
))
8840 if (cstate
->linktype
!= DLT_PFLOG
) {
8841 bpf_error(cstate
, "reason supported only on PF linktype");
8845 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8846 (bpf_u_int32
)reason
);
8850 /* PF firewall log action */
8852 gen_pf_action(compiler_state_t
*cstate
, int action
)
8857 * Catch errors reported by us and routines below us, and return NULL
8860 if (setjmp(cstate
->top_ctx
))
8863 if (cstate
->linktype
!= DLT_PFLOG
) {
8864 bpf_error(cstate
, "action supported only on PF linktype");
8868 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8869 (bpf_u_int32
)action
);
8873 /* IEEE 802.11 wireless header */
8875 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8880 * Catch errors reported by us and routines below us, and return NULL
8883 if (setjmp(cstate
->top_ctx
))
8886 switch (cstate
->linktype
) {
8888 case DLT_IEEE802_11
:
8889 case DLT_PRISM_HEADER
:
8890 case DLT_IEEE802_11_RADIO_AVS
:
8891 case DLT_IEEE802_11_RADIO
:
8892 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8896 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8904 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8909 * Catch errors reported by us and routines below us, and return NULL
8912 if (setjmp(cstate
->top_ctx
))
8915 switch (cstate
->linktype
) {
8917 case DLT_IEEE802_11
:
8918 case DLT_PRISM_HEADER
:
8919 case DLT_IEEE802_11_RADIO_AVS
:
8920 case DLT_IEEE802_11_RADIO
:
8924 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8928 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8929 IEEE80211_FC1_DIR_MASK
);
8935 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8940 * Catch errors reported by us and routines below us, and return NULL
8943 if (setjmp(cstate
->top_ctx
))
8946 switch (cstate
->linktype
) {
8949 case DLT_ARCNET_LINUX
:
8950 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8951 q
.proto
== Q_LINK
) {
8952 cstate
->e
= pcap_ether_aton(s
);
8953 if (cstate
->e
== NULL
)
8954 bpf_error(cstate
, "malloc");
8955 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8960 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8964 bpf_error(cstate
, "aid supported only on ARCnet");
8969 static struct block
*
8970 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8972 register struct block
*b0
, *b1
;
8975 /* src comes first, different from Ethernet */
8977 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8980 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8983 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8984 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8990 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8991 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8996 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9000 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9004 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9008 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9012 bpf_error(cstate
, "'ra' is only supported on 802.11");
9016 bpf_error(cstate
, "'ta' is only supported on 802.11");
9023 static struct block
*
9024 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9026 struct block
*b0
, *b1
;
9028 /* check for VLAN, including 802.1ad and QinQ */
9029 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9030 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9033 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9039 static struct block
*
9040 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9042 if (vlan_num
> 0x0fff) {
9043 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9046 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9049 static struct block
*
9050 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9053 struct block
*b0
, *b1
;
9055 b0
= gen_vlan_tpid_test(cstate
);
9058 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9064 * Both payload and link header type follow the VLAN tags so that
9065 * both need to be updated.
9067 cstate
->off_linkpl
.constant_part
+= 4;
9068 cstate
->off_linktype
.constant_part
+= 4;
9073 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9074 /* add v to variable part of off */
9076 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9077 bpf_u_int32 v
, struct slist
*s
)
9081 if (!off
->is_variable
)
9082 off
->is_variable
= 1;
9084 off
->reg
= alloc_reg(cstate
);
9086 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9089 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9092 s2
= new_stmt(cstate
, BPF_ST
);
9098 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9099 * and link type offsets first
9102 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9106 /* offset determined at run time, shift variable part */
9108 cstate
->is_vlan_vloffset
= 1;
9109 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9110 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9112 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9113 sappend(s
.next
, b_tpid
->head
->stmts
);
9114 b_tpid
->head
->stmts
= s
.next
;
9118 * patch block b_vid (VLAN id test) to load VID value either from packet
9119 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9122 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9124 struct slist
*s
, *s2
, *sjeq
;
9127 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9128 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9130 /* true -> next instructions, false -> beginning of b_vid */
9131 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9133 sjeq
->s
.jf
= b_vid
->stmts
;
9136 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9137 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
9141 /* Jump to the test in b_vid. We need to jump one instruction before
9142 * the end of the b_vid block so that we only skip loading the TCI
9143 * from packet data and not the 'and' instruction extracting VID.
9146 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9148 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9152 /* insert our statements at the beginning of b_vid */
9153 sappend(s
, b_vid
->stmts
);
9158 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9159 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9160 * tag can be either in metadata or in packet data; therefore if the
9161 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9162 * header for VLAN tag. As the decision is done at run time, we need
9163 * update variable part of the offsets
9165 static struct block
*
9166 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9169 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9172 /* generate new filter code based on extracting packet
9174 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9175 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9177 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9182 * This is tricky. We need to insert the statements updating variable
9183 * parts of offsets before the traditional TPID and VID tests so
9184 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9185 * we do not want this update to affect those checks. That's why we
9186 * generate both test blocks first and insert the statements updating
9187 * variable parts of both offsets after that. This wouldn't work if
9188 * there already were variable length link header when entering this
9189 * function but gen_vlan_bpf_extensions() isn't called in that case.
9191 b_tpid
= gen_vlan_tpid_test(cstate
);
9193 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9195 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9200 gen_vlan_patch_vid_test(cstate
, b_vid
);
9210 * support IEEE 802.1Q VLAN trunk over ethernet
9213 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9218 * Catch errors reported by us and routines below us, and return NULL
9221 if (setjmp(cstate
->top_ctx
))
9224 /* can't check for VLAN-encapsulated packets inside MPLS */
9225 if (cstate
->label_stack_depth
> 0)
9226 bpf_error(cstate
, "no VLAN match after MPLS");
9229 * Check for a VLAN packet, and then change the offsets to point
9230 * to the type and data fields within the VLAN packet. Just
9231 * increment the offsets, so that we can support a hierarchy, e.g.
9232 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
9235 * XXX - this is a bit of a kludge. If we were to split the
9236 * compiler into a parser that parses an expression and
9237 * generates an expression tree, and a code generator that
9238 * takes an expression tree (which could come from our
9239 * parser or from some other parser) and generates BPF code,
9240 * we could perhaps make the offsets parameters of routines
9241 * and, in the handler for an "AND" node, pass to subnodes
9242 * other than the VLAN node the adjusted offsets.
9244 * This would mean that "vlan" would, instead of changing the
9245 * behavior of *all* tests after it, change only the behavior
9246 * of tests ANDed with it. That would change the documented
9247 * semantics of "vlan", which might break some expressions.
9248 * However, it would mean that "(vlan and ip) or ip" would check
9249 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9250 * checking only for VLAN-encapsulated IP, so that could still
9251 * be considered worth doing; it wouldn't break expressions
9252 * that are of the form "vlan and ..." or "vlan N and ...",
9253 * which I suspect are the most common expressions involving
9254 * "vlan". "vlan or ..." doesn't necessarily do what the user
9255 * would really want, now, as all the "or ..." tests would
9256 * be done assuming a VLAN, even though the "or" could be viewed
9257 * as meaning "or, if this isn't a VLAN packet...".
9259 switch (cstate
->linktype
) {
9262 case DLT_NETANALYZER
:
9263 case DLT_NETANALYZER_TRANSPARENT
:
9264 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9265 /* Verify that this is the outer part of the packet and
9266 * not encapsulated somehow. */
9267 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9268 cstate
->off_linkhdr
.constant_part
==
9269 cstate
->off_outermostlinkhdr
.constant_part
) {
9271 * Do we need special VLAN handling?
9273 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9274 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9277 b0
= gen_vlan_no_bpf_extensions(cstate
,
9278 vlan_num
, has_vlan_tag
);
9281 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9285 case DLT_IEEE802_11
:
9286 case DLT_PRISM_HEADER
:
9287 case DLT_IEEE802_11_RADIO_AVS
:
9288 case DLT_IEEE802_11_RADIO
:
9289 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9293 bpf_error(cstate
, "no VLAN support for %s",
9294 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9298 cstate
->vlan_stack_depth
++;
9306 * The label_num_arg dance is to avoid annoying whining by compilers that
9307 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9308 * It's not *used* after setjmp returns.
9311 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9314 volatile bpf_u_int32 label_num
= label_num_arg
;
9315 struct block
*b0
, *b1
;
9318 * Catch errors reported by us and routines below us, and return NULL
9321 if (setjmp(cstate
->top_ctx
))
9324 if (cstate
->label_stack_depth
> 0) {
9325 /* just match the bottom-of-stack bit clear */
9326 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9329 * We're not in an MPLS stack yet, so check the link-layer
9330 * type against MPLS.
9332 switch (cstate
->linktype
) {
9334 case DLT_C_HDLC
: /* fall through */
9337 case DLT_NETANALYZER
:
9338 case DLT_NETANALYZER_TRANSPARENT
:
9339 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9343 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9346 /* FIXME add other DLT_s ...
9347 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9348 * leave it for now */
9351 bpf_error(cstate
, "no MPLS support for %s",
9352 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9357 /* If a specific MPLS label is requested, check it */
9358 if (has_label_num
) {
9359 if (label_num
> 0xFFFFF) {
9360 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9361 label_num
, 0xFFFFF);
9363 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9364 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9365 0xfffff000); /* only compare the first 20 bits */
9371 * Change the offsets to point to the type and data fields within
9372 * the MPLS packet. Just increment the offsets, so that we
9373 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9374 * capture packets with an outer label of 100000 and an inner
9377 * Increment the MPLS stack depth as well; this indicates that
9378 * we're checking MPLS-encapsulated headers, to make sure higher
9379 * level code generators don't try to match against IP-related
9380 * protocols such as Q_ARP, Q_RARP etc.
9382 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9384 cstate
->off_nl_nosnap
+= 4;
9385 cstate
->off_nl
+= 4;
9386 cstate
->label_stack_depth
++;
9391 * Support PPPOE discovery and session.
9394 gen_pppoed(compiler_state_t
*cstate
)
9397 * Catch errors reported by us and routines below us, and return NULL
9400 if (setjmp(cstate
->top_ctx
))
9403 /* check for PPPoE discovery */
9404 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9408 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9410 struct block
*b0
, *b1
;
9413 * Catch errors reported by us and routines below us, and return NULL
9416 if (setjmp(cstate
->top_ctx
))
9420 * Test against the PPPoE session link-layer type.
9422 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9424 /* If a specific session is requested, check PPPoE session id */
9426 if (sess_num
> 0x0000ffff) {
9427 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9428 sess_num
, 0x0000ffff);
9430 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9436 * Change the offsets to point to the type and data fields within
9437 * the PPP packet, and note that this is PPPoE rather than
9440 * XXX - this is a bit of a kludge. See the comments in
9443 * The "network-layer" protocol is PPPoE, which has a 6-byte
9444 * PPPoE header, followed by a PPP packet.
9446 * There is no HDLC encapsulation for the PPP packet (it's
9447 * encapsulated in PPPoES instead), so the link-layer type
9448 * starts at the first byte of the PPP packet. For PPPoE,
9449 * that offset is relative to the beginning of the total
9450 * link-layer payload, including any 802.2 LLC header, so
9451 * it's 6 bytes past cstate->off_nl.
9453 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9454 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9455 cstate
->off_linkpl
.reg
);
9457 cstate
->off_linktype
= cstate
->off_linkhdr
;
9458 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9461 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9466 /* Check that this is Geneve and the VNI is correct if
9467 * specified. Parameterized to handle both IPv4 and IPv6. */
9468 static struct block
*
9469 gen_geneve_check(compiler_state_t
*cstate
,
9470 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9471 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9473 struct block
*b0
, *b1
;
9475 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9477 /* Check that we are operating on version 0. Otherwise, we
9478 * can't decode the rest of the fields. The version is 2 bits
9479 * in the first byte of the Geneve header. */
9480 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9485 if (vni
> 0xffffff) {
9486 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9489 vni
<<= 8; /* VNI is in the upper 3 bytes */
9490 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9498 /* The IPv4 and IPv6 Geneve checks need to do two things:
9499 * - Verify that this actually is Geneve with the right VNI.
9500 * - Place the IP header length (plus variable link prefix if
9501 * needed) into register A to be used later to compute
9502 * the inner packet offsets. */
9503 static struct block
*
9504 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9506 struct block
*b0
, *b1
;
9507 struct slist
*s
, *s1
;
9509 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9511 /* Load the IP header length into A. */
9512 s
= gen_loadx_iphdrlen(cstate
);
9514 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9517 /* Forcibly append these statements to the true condition
9518 * of the protocol check by creating a new block that is
9519 * always true and ANDing them. */
9520 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9529 static struct block
*
9530 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9532 struct block
*b0
, *b1
;
9533 struct slist
*s
, *s1
;
9535 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9537 /* Load the IP header length. We need to account for a
9538 * variable length link prefix if there is one. */
9539 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9541 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9545 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9549 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9553 /* Forcibly append these statements to the true condition
9554 * of the protocol check by creating a new block that is
9555 * always true and ANDing them. */
9556 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9559 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9568 /* We need to store three values based on the Geneve header::
9569 * - The offset of the linktype.
9570 * - The offset of the end of the Geneve header.
9571 * - The offset of the end of the encapsulated MAC header. */
9572 static struct slist
*
9573 gen_geneve_offsets(compiler_state_t
*cstate
)
9575 struct slist
*s
, *s1
, *s_proto
;
9577 /* First we need to calculate the offset of the Geneve header
9578 * itself. This is composed of the IP header previously calculated
9579 * (include any variable link prefix) and stored in A plus the
9580 * fixed sized headers (fixed link prefix, MAC length, and UDP
9582 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9583 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9585 /* Stash this in X since we'll need it later. */
9586 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9589 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9591 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9595 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9596 cstate
->off_linktype
.is_variable
= 1;
9597 cstate
->off_linktype
.constant_part
= 0;
9599 s1
= new_stmt(cstate
, BPF_ST
);
9600 s1
->s
.k
= cstate
->off_linktype
.reg
;
9603 /* Load the Geneve option length and mask and shift to get the
9604 * number of bytes. It is stored in the first byte of the Geneve
9606 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9610 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9614 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9618 /* Add in the rest of the Geneve base header. */
9619 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9623 /* Add the Geneve header length to its offset and store. */
9624 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9628 /* Set the encapsulated type as Ethernet. Even though we may
9629 * not actually have Ethernet inside there are two reasons this
9631 * - The linktype field is always in EtherType format regardless
9632 * of whether it is in Geneve or an inner Ethernet frame.
9633 * - The only link layer that we have specific support for is
9634 * Ethernet. We will confirm that the packet actually is
9635 * Ethernet at runtime before executing these checks. */
9636 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9638 s1
= new_stmt(cstate
, BPF_ST
);
9639 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9642 /* Calculate whether we have an Ethernet header or just raw IP/
9643 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9644 * and linktype by 14 bytes so that the network header can be found
9645 * seamlessly. Otherwise, keep what we've calculated already. */
9647 /* We have a bare jmp so we can't use the optimizer. */
9648 cstate
->no_optimize
= 1;
9650 /* Load the EtherType in the Geneve header, 2 bytes in. */
9651 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9655 /* Load X with the end of the Geneve header. */
9656 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9657 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9660 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9661 * end of this check, we should have the total length in X. In
9662 * the non-Ethernet case, it's already there. */
9663 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9664 s_proto
->s
.k
= ETHERTYPE_TEB
;
9665 sappend(s
, s_proto
);
9667 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9671 /* Since this is Ethernet, use the EtherType of the payload
9672 * directly as the linktype. Overwrite what we already have. */
9673 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9677 s1
= new_stmt(cstate
, BPF_ST
);
9678 s1
->s
.k
= cstate
->off_linktype
.reg
;
9681 /* Advance two bytes further to get the end of the Ethernet
9683 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9687 /* Move the result to X. */
9688 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9691 /* Store the final result of our linkpl calculation. */
9692 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9693 cstate
->off_linkpl
.is_variable
= 1;
9694 cstate
->off_linkpl
.constant_part
= 0;
9696 s1
= new_stmt(cstate
, BPF_STX
);
9697 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9706 /* Check to see if this is a Geneve packet. */
9708 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9710 struct block
*b0
, *b1
;
9714 * Catch errors reported by us and routines below us, and return NULL
9717 if (setjmp(cstate
->top_ctx
))
9720 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9721 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9726 /* Later filters should act on the payload of the Geneve frame,
9727 * update all of the header pointers. Attach this code so that
9728 * it gets executed in the event that the Geneve filter matches. */
9729 s
= gen_geneve_offsets(cstate
);
9731 b1
= gen_true(cstate
);
9732 sappend(s
, b1
->stmts
);
9737 cstate
->is_geneve
= 1;
9742 /* Check that the encapsulated frame has a link layer header
9743 * for Ethernet filters. */
9744 static struct block
*
9745 gen_geneve_ll_check(compiler_state_t
*cstate
)
9748 struct slist
*s
, *s1
;
9750 /* The easiest way to see if there is a link layer present
9751 * is to check if the link layer header and payload are not
9754 /* Geneve always generates pure variable offsets so we can
9755 * compare only the registers. */
9756 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9757 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9759 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9760 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9763 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9771 static struct block
*
9772 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9773 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9780 if (!cstate
->is_atm
)
9781 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9782 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9784 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9785 0xffffffffU
, jtype
, reverse
, jvalue
);
9789 if (!cstate
->is_atm
)
9790 bpf_error(cstate
, "'vci' supported only on raw ATM");
9791 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9793 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9794 0xffffffffU
, jtype
, reverse
, jvalue
);
9798 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9799 abort(); /* XXX - this isn't on FreeBSD */
9800 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9801 0x0fU
, jtype
, reverse
, jvalue
);
9805 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9807 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9808 0xffffffffU
, jtype
, reverse
, jvalue
);
9812 if (!cstate
->is_atm
)
9813 bpf_error(cstate
, "'callref' supported only on raw ATM");
9814 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9816 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9817 0xffffffffU
, jtype
, reverse
, jvalue
);
9826 static struct block
*
9827 gen_atmtype_metac(compiler_state_t
*cstate
)
9829 struct block
*b0
, *b1
;
9831 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9832 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9837 static struct block
*
9838 gen_atmtype_sc(compiler_state_t
*cstate
)
9840 struct block
*b0
, *b1
;
9842 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9843 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9848 static struct block
*
9849 gen_atmtype_llc(compiler_state_t
*cstate
)
9853 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9854 cstate
->linktype
= cstate
->prevlinktype
;
9859 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9860 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9863 * Catch errors reported by us and routines below us, and return NULL
9866 if (setjmp(cstate
->top_ctx
))
9869 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9874 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9876 struct block
*b0
, *b1
;
9879 * Catch errors reported by us and routines below us, and return NULL
9882 if (setjmp(cstate
->top_ctx
))
9888 /* Get all packets in Meta signalling Circuit */
9889 if (!cstate
->is_atm
)
9890 bpf_error(cstate
, "'metac' supported only on raw ATM");
9891 b1
= gen_atmtype_metac(cstate
);
9895 /* Get all packets in Broadcast Circuit*/
9896 if (!cstate
->is_atm
)
9897 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9898 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9899 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9904 /* Get all cells in Segment OAM F4 circuit*/
9905 if (!cstate
->is_atm
)
9906 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9907 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9908 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9913 /* Get all cells in End-to-End OAM F4 Circuit*/
9914 if (!cstate
->is_atm
)
9915 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9916 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9917 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9922 /* Get all packets in connection Signalling Circuit */
9923 if (!cstate
->is_atm
)
9924 bpf_error(cstate
, "'sc' supported only on raw ATM");
9925 b1
= gen_atmtype_sc(cstate
);
9929 /* Get all packets in ILMI Circuit */
9930 if (!cstate
->is_atm
)
9931 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9932 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9933 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9938 /* Get all LANE packets */
9939 if (!cstate
->is_atm
)
9940 bpf_error(cstate
, "'lane' supported only on raw ATM");
9941 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9944 * Arrange that all subsequent tests assume LANE
9945 * rather than LLC-encapsulated packets, and set
9946 * the offsets appropriately for LANE-encapsulated
9949 * We assume LANE means Ethernet, not Token Ring.
9951 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9952 cstate
->off_payload
+ 2, /* Ethernet header */
9954 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9955 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9956 cstate
->off_nl
= 0; /* Ethernet II */
9957 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9961 /* Get all LLC-encapsulated packets */
9962 if (!cstate
->is_atm
)
9963 bpf_error(cstate
, "'llc' supported only on raw ATM");
9964 b1
= gen_atmtype_llc(cstate
);
9974 * Filtering for MTP2 messages based on li value
9975 * FISU, length is null
9976 * LSSU, length is 1 or 2
9977 * MSU, length is 3 or more
9978 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9981 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9983 struct block
*b0
, *b1
;
9986 * Catch errors reported by us and routines below us, and return NULL
9989 if (setjmp(cstate
->top_ctx
))
9995 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9996 (cstate
->linktype
!= DLT_ERF
) &&
9997 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9998 bpf_error(cstate
, "'fisu' supported only on MTP2");
9999 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10000 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10001 0x3fU
, BPF_JEQ
, 0, 0U);
10005 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10006 (cstate
->linktype
!= DLT_ERF
) &&
10007 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10008 bpf_error(cstate
, "'lssu' supported only on MTP2");
10009 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10010 0x3fU
, BPF_JGT
, 1, 2U);
10011 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10012 0x3fU
, BPF_JGT
, 0, 0U);
10017 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10018 (cstate
->linktype
!= DLT_ERF
) &&
10019 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10020 bpf_error(cstate
, "'msu' supported only on MTP2");
10021 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10022 0x3fU
, BPF_JGT
, 0, 2U);
10026 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10027 (cstate
->linktype
!= DLT_ERF
) &&
10028 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10029 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10030 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10031 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10032 0xff80U
, BPF_JEQ
, 0, 0U);
10036 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10037 (cstate
->linktype
!= DLT_ERF
) &&
10038 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10039 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10040 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10041 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10042 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10043 0xff80U
, BPF_JGT
, 0, 0U);
10048 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10049 (cstate
->linktype
!= DLT_ERF
) &&
10050 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10051 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10052 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10053 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10063 * The jvalue_arg dance is to avoid annoying whining by compilers that
10064 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
10065 * It's not *used* after setjmp returns.
10068 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10069 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
10071 volatile bpf_u_int32 jvalue
= jvalue_arg
;
10073 bpf_u_int32 val1
, val2
, val3
;
10080 * Catch errors reported by us and routines below us, and return NULL
10083 if (setjmp(cstate
->top_ctx
))
10086 newoff_sio
= cstate
->off_sio
;
10087 newoff_opc
= cstate
->off_opc
;
10088 newoff_dpc
= cstate
->off_dpc
;
10089 newoff_sls
= cstate
->off_sls
;
10090 switch (mtp3field
) {
10093 newoff_sio
+= 3; /* offset for MTP2_HSL */
10097 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10098 bpf_error(cstate
, "'sio' supported only on SS7");
10099 /* sio coded on 1 byte so max value 255 */
10101 bpf_error(cstate
, "sio value %u too big; max value = 255",
10103 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
10104 jtype
, reverse
, jvalue
);
10112 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10113 bpf_error(cstate
, "'opc' supported only on SS7");
10114 /* opc coded on 14 bits so max value 16383 */
10115 if (jvalue
> 16383)
10116 bpf_error(cstate
, "opc value %u too big; max value = 16383",
10118 /* the following instructions are made to convert jvalue
10119 * to the form used to write opc in an ss7 message*/
10120 val1
= jvalue
& 0x00003c00;
10122 val2
= jvalue
& 0x000003fc;
10124 val3
= jvalue
& 0x00000003;
10126 jvalue
= val1
+ val2
+ val3
;
10127 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
10128 jtype
, reverse
, jvalue
);
10136 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10137 bpf_error(cstate
, "'dpc' supported only on SS7");
10138 /* dpc coded on 14 bits so max value 16383 */
10139 if (jvalue
> 16383)
10140 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
10142 /* the following instructions are made to convert jvalue
10143 * to the forme used to write dpc in an ss7 message*/
10144 val1
= jvalue
& 0x000000ff;
10146 val2
= jvalue
& 0x00003f00;
10148 jvalue
= val1
+ val2
;
10149 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
10150 jtype
, reverse
, jvalue
);
10158 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10159 bpf_error(cstate
, "'sls' supported only on SS7");
10160 /* sls coded on 4 bits so max value 15 */
10162 bpf_error(cstate
, "sls value %u too big; max value = 15",
10164 /* the following instruction is made to convert jvalue
10165 * to the forme used to write sls in an ss7 message*/
10166 jvalue
= jvalue
<< 4;
10167 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
10168 jtype
, reverse
, jvalue
);
10177 static struct block
*
10178 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10183 * Q.2931 signalling protocol messages for handling virtual circuits
10184 * establishment and teardown
10189 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10192 case A_CALLPROCEED
:
10193 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10197 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10201 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10205 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10208 case A_RELEASE_DONE
:
10209 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10219 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10221 struct block
*b0
, *b1
;
10224 * Catch errors reported by us and routines below us, and return NULL
10227 if (setjmp(cstate
->top_ctx
))
10233 if (!cstate
->is_atm
)
10234 bpf_error(cstate
, "'oam' supported only on raw ATM");
10236 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10237 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10239 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10244 if (!cstate
->is_atm
)
10245 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10247 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10248 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10250 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10256 * Get Q.2931 signalling messages for switched
10257 * virtual connection
10259 if (!cstate
->is_atm
)
10260 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10261 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10262 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10264 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10266 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10268 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10270 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10272 b0
= gen_atmtype_sc(cstate
);
10276 case A_METACONNECT
:
10277 if (!cstate
->is_atm
)
10278 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10279 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10280 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10282 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10284 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10286 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10288 b0
= gen_atmtype_metac(cstate
);