1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <pcap-types.h>
31 #include <sys/socket.h>
34 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
55 #include "ethertype.h"
59 #include "ieee80211.h"
61 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
70 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
96 uint16_t u6_addr16
[8];
97 uint32_t u6_addr32
[4];
99 #define s6_addr in6_u.u6_addr8
100 #define s6_addr16 in6_u.u6_addr16
101 #define s6_addr32 in6_u.u6_addr32
102 #define s6_addr64 in6_u.u6_addr64
105 typedef unsigned short sa_family_t
;
107 #define __SOCKADDR_COMMON(sa_prefix) \
108 sa_family_t sa_prefix##family
110 /* Ditto, for IPv6. */
113 __SOCKADDR_COMMON (sin6_
);
114 uint16_t sin6_port
; /* Transport layer port # */
115 uint32_t sin6_flowinfo
; /* IPv6 flow information */
116 struct in6_addr sin6_addr
; /* IPv6 address */
119 #ifndef EAI_ADDRFAMILY
121 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
122 int ai_family
; /* PF_xxx */
123 int ai_socktype
; /* SOCK_xxx */
124 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
125 size_t ai_addrlen
; /* length of ai_addr */
126 char *ai_canonname
; /* canonical name for hostname */
127 struct sockaddr
*ai_addr
; /* binary address */
128 struct addrinfo
*ai_next
; /* next structure in linked list */
130 #endif /* EAI_ADDRFAMILY */
131 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
134 #include <netdb.h> /* for "struct addrinfo" */
136 #include <pcap/namedb.h>
138 #include "nametoaddr.h"
140 #define ETHERMTU 1500
142 #ifndef ETHERTYPE_TEB
143 #define ETHERTYPE_TEB 0x6558
146 #ifndef IPPROTO_HOPOPTS
147 #define IPPROTO_HOPOPTS 0
149 #ifndef IPPROTO_ROUTING
150 #define IPPROTO_ROUTING 43
152 #ifndef IPPROTO_FRAGMENT
153 #define IPPROTO_FRAGMENT 44
155 #ifndef IPPROTO_DSTOPTS
156 #define IPPROTO_DSTOPTS 60
159 #define IPPROTO_SCTP 132
162 #define GENEVE_PORT 6081
164 #ifdef HAVE_OS_PROTO_H
165 #include "os-proto.h"
168 #define JMP(c) ((c)|BPF_JMP|BPF_K)
171 * "Push" the current value of the link-layer header type and link-layer
172 * header offset onto a "stack", and set a new value. (It's not a
173 * full-blown stack; we keep only the top two items.)
175 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
177 (cs)->prevlinktype = (cs)->linktype; \
178 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
179 (cs)->linktype = (new_linktype); \
180 (cs)->off_linkhdr.is_variable = (new_is_variable); \
181 (cs)->off_linkhdr.constant_part = (new_constant_part); \
182 (cs)->off_linkhdr.reg = (new_reg); \
183 (cs)->is_geneve = 0; \
187 * Offset "not set" value.
189 #define OFFSET_NOT_SET 0xffffffffU
192 * Absolute offsets, which are offsets from the beginning of the raw
193 * packet data, are, in the general case, the sum of a variable value
194 * and a constant value; the variable value may be absent, in which
195 * case the offset is only the constant value, and the constant value
196 * may be zero, in which case the offset is only the variable value.
198 * bpf_abs_offset is a structure containing all that information:
200 * is_variable is 1 if there's a variable part.
202 * constant_part is the constant part of the value, possibly zero;
204 * if is_variable is 1, reg is the register number for a register
205 * containing the variable value if the register has been assigned,
215 * Value passed to gen_load_a() to indicate what the offset argument
216 * is relative to the beginning of.
219 OR_PACKET
, /* full packet data */
220 OR_LINKHDR
, /* link-layer header */
221 OR_PREVLINKHDR
, /* previous link-layer header */
222 OR_LLC
, /* 802.2 LLC header */
223 OR_PREVMPLSHDR
, /* previous MPLS header */
224 OR_LINKTYPE
, /* link-layer type */
225 OR_LINKPL
, /* link-layer payload */
226 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
227 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
228 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
232 * We divy out chunks of memory rather than call malloc each time so
233 * we don't have to worry about leaking memory. It's probably
234 * not a big deal if all this memory was wasted but if this ever
235 * goes into a library that would probably not be a good idea.
237 * XXX - this *is* in a library....
240 #define CHUNK0SIZE 1024
246 /* Code generator state */
248 struct _compiler_state
{
258 int outermostlinktype
;
263 /* Hack for handling VLAN and MPLS stacks. */
264 u_int label_stack_depth
;
265 u_int vlan_stack_depth
;
271 * As errors are handled by a longjmp, anything allocated must
272 * be freed in the longjmp handler, so it must be reachable
275 * One thing that's allocated is the result of pcap_nametoaddrinfo();
276 * it must be freed with freeaddrinfo(). This variable points to
277 * any addrinfo structure that would need to be freed.
282 * Another thing that's allocated is the result of pcap_ether_aton();
283 * it must be freed with free(). This variable points to any
284 * address that would need to be freed.
289 * Various code constructs need to know the layout of the packet.
290 * These values give the necessary offsets from the beginning
291 * of the packet data.
295 * Absolute offset of the beginning of the link-layer header.
297 bpf_abs_offset off_linkhdr
;
300 * If we're checking a link-layer header for a packet encapsulated
301 * in another protocol layer, this is the equivalent information
302 * for the previous layers' link-layer header from the beginning
303 * of the raw packet data.
305 bpf_abs_offset off_prevlinkhdr
;
308 * This is the equivalent information for the outermost layers'
311 bpf_abs_offset off_outermostlinkhdr
;
314 * Absolute offset of the beginning of the link-layer payload.
316 bpf_abs_offset off_linkpl
;
319 * "off_linktype" is the offset to information in the link-layer
320 * header giving the packet type. This is an absolute offset
321 * from the beginning of the packet.
323 * For Ethernet, it's the offset of the Ethernet type field; this
324 * means that it must have a value that skips VLAN tags.
326 * For link-layer types that always use 802.2 headers, it's the
327 * offset of the LLC header; this means that it must have a value
328 * that skips VLAN tags.
330 * For PPP, it's the offset of the PPP type field.
332 * For Cisco HDLC, it's the offset of the CHDLC type field.
334 * For BSD loopback, it's the offset of the AF_ value.
336 * For Linux cooked sockets, it's the offset of the type field.
338 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
339 * encapsulation, in which case, IP is assumed.
341 bpf_abs_offset off_linktype
;
344 * TRUE if the link layer includes an ATM pseudo-header.
349 * TRUE if "geneve" appeared in the filter; it causes us to
350 * generate code that checks for a Geneve header and assume
351 * that later filters apply to the encapsulated payload.
356 * TRUE if we need variable length part of VLAN offset
358 int is_vlan_vloffset
;
361 * These are offsets for the ATM pseudo-header.
368 * These are offsets for the MTP2 fields.
374 * These are offsets for the MTP3 fields.
382 * This is the offset of the first byte after the ATM pseudo_header,
383 * or -1 if there is no ATM pseudo-header.
388 * These are offsets to the beginning of the network-layer header.
389 * They are relative to the beginning of the link-layer payload
390 * (i.e., they don't include off_linkhdr.constant_part or
391 * off_linkpl.constant_part).
393 * If the link layer never uses 802.2 LLC:
395 * "off_nl" and "off_nl_nosnap" are the same.
397 * If the link layer always uses 802.2 LLC:
399 * "off_nl" is the offset if there's a SNAP header following
402 * "off_nl_nosnap" is the offset if there's no SNAP header.
404 * If the link layer is Ethernet:
406 * "off_nl" is the offset if the packet is an Ethernet II packet
407 * (we assume no 802.3+802.2+SNAP);
409 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
410 * with an 802.2 header following it.
416 * Here we handle simple allocation of the scratch registers.
417 * If too many registers are alloc'd, the allocator punts.
419 int regused
[BPF_MEMWORDS
];
425 struct chunk chunks
[NCHUNKS
];
430 bpf_parser_error(compiler_state_t
*cstate
, const char *msg
)
432 bpf_error(cstate
, "can't parse filter expression: %s", msg
);
437 * For use by the optimizer, which needs to do its *own* cleanup before
438 * delivering a longjmp-based exception.
441 bpf_vset_error(compiler_state_t
*cstate
, const char *fmt
, va_list ap
)
443 if (cstate
->bpf_pcap
!= NULL
)
444 (void)pcap_vsnprintf(pcap_geterr(cstate
->bpf_pcap
),
445 PCAP_ERRBUF_SIZE
, fmt
, ap
);
449 bpf_abort_compilation(compiler_state_t
*cstate
)
451 longjmp(cstate
->top_ctx
, 1);
457 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
462 bpf_vset_error(cstate
, fmt
, ap
);
464 bpf_abort_compilation(cstate
);
468 static void init_linktype(compiler_state_t
*, pcap_t
*);
470 static void init_regs(compiler_state_t
*);
471 static int alloc_reg(compiler_state_t
*);
472 static void free_reg(compiler_state_t
*, int);
474 static void initchunks(compiler_state_t
*cstate
);
475 static void *newchunk(compiler_state_t
*cstate
, size_t);
476 static void freechunks(compiler_state_t
*cstate
);
477 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
478 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
479 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
480 static inline void syntax(compiler_state_t
*cstate
);
482 static void backpatch(struct block
*, struct block
*);
483 static void merge(struct block
*, struct block
*);
484 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
486 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
488 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
490 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
492 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
494 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
495 u_int
, bpf_int32
, bpf_u_int32
);
496 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
497 u_int
, const u_char
*);
498 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
499 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
500 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
502 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
504 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
505 static struct block
*gen_uncond(compiler_state_t
*, int);
506 static inline struct block
*gen_true(compiler_state_t
*);
507 static inline struct block
*gen_false(compiler_state_t
*);
508 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
509 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
510 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
511 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
512 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
513 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
514 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
515 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
516 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
518 static int ethertype_to_ppptype(int);
519 static struct block
*gen_linktype(compiler_state_t
*, int);
520 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
521 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
522 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
523 int, int, u_int
, u_int
);
525 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
526 struct in6_addr
*, int, int, u_int
, u_int
);
528 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
529 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
530 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
531 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
532 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
533 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
534 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
535 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
536 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
539 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
540 struct in6_addr
*, int, int, int);
543 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
544 struct addrinfo
*, int, int);
546 static struct block
*gen_ipfrag(compiler_state_t
*);
547 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
548 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
550 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
551 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
553 struct block
*gen_portop(compiler_state_t
*, int, int, int);
554 static struct block
*gen_port(compiler_state_t
*, int, int, int);
555 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
556 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
557 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
558 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
559 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
560 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
561 static int lookup_proto(compiler_state_t
*, const char *, int);
562 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
563 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
564 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
565 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
566 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
567 static struct block
*gen_len(compiler_state_t
*, int, int);
568 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
569 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
571 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
572 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
575 initchunks(compiler_state_t
*cstate
)
579 for (i
= 0; i
< NCHUNKS
; i
++) {
580 cstate
->chunks
[i
].n_left
= 0;
581 cstate
->chunks
[i
].m
= NULL
;
583 cstate
->cur_chunk
= 0;
587 newchunk(compiler_state_t
*cstate
, size_t n
)
594 /* XXX Round up to nearest long. */
595 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
597 /* XXX Round up to structure boundary. */
601 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
602 if (n
> cp
->n_left
) {
604 k
= ++cstate
->cur_chunk
;
606 bpf_error(cstate
, "out of memory");
607 size
= CHUNK0SIZE
<< k
;
608 cp
->m
= (void *)malloc(size
);
610 bpf_error(cstate
, "out of memory");
611 memset((char *)cp
->m
, 0, size
);
614 bpf_error(cstate
, "out of memory");
617 return (void *)((char *)cp
->m
+ cp
->n_left
);
621 freechunks(compiler_state_t
*cstate
)
625 for (i
= 0; i
< NCHUNKS
; ++i
)
626 if (cstate
->chunks
[i
].m
!= NULL
)
627 free(cstate
->chunks
[i
].m
);
631 * A strdup whose allocations are freed after code generation is over.
634 sdup(compiler_state_t
*cstate
, const char *s
)
636 size_t n
= strlen(s
) + 1;
637 char *cp
= newchunk(cstate
, n
);
639 pcap_strlcpy(cp
, s
, n
);
643 static inline struct block
*
644 new_block(compiler_state_t
*cstate
, int code
)
648 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
655 static inline struct slist
*
656 new_stmt(compiler_state_t
*cstate
, int code
)
660 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
666 static struct block
*
667 gen_retblk(compiler_state_t
*cstate
, int v
)
669 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
675 static inline PCAP_NORETURN_DEF
void
676 syntax(compiler_state_t
*cstate
)
678 bpf_error(cstate
, "syntax error in filter expression");
682 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
683 const char *buf
, int optimize
, bpf_u_int32 mask
)
688 compiler_state_t cstate
;
689 const char * volatile xbuf
= buf
;
690 yyscan_t scanner
= NULL
;
691 volatile YY_BUFFER_STATE in_buffer
= NULL
;
696 * If this pcap_t hasn't been activated, it doesn't have a
697 * link-layer type, so we can't use it.
700 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
701 "not-yet-activated pcap_t passed to pcap_compile");
713 * If the device on which we're capturing need to be notified
714 * that a new filter is being compiled, do so.
716 * This allows them to save a copy of it, in case, for example,
717 * they're implementing a form of remote packet capture, and
718 * want the remote machine to filter out the packets in which
719 * it's sending the packets it's captured.
721 * XXX - the fact that we happen to be compiling a filter
722 * doesn't necessarily mean we'll be installing it as the
723 * filter for this pcap_t; we might be running it from userland
724 * on captured packets to do packet classification. We really
725 * need a better way of handling this, but this is all that
726 * the WinPcap code did.
728 if (p
->save_current_filter_op
!= NULL
)
729 (p
->save_current_filter_op
)(p
, buf
);
733 cstate
.no_optimize
= 0;
738 cstate
.ic
.root
= NULL
;
739 cstate
.ic
.cur_mark
= 0;
743 if (setjmp(cstate
.top_ctx
)) {
745 if (cstate
.ai
!= NULL
)
746 freeaddrinfo(cstate
.ai
);
748 if (cstate
.e
!= NULL
)
754 cstate
.netmask
= mask
;
756 cstate
.snaplen
= pcap_snapshot(p
);
757 if (cstate
.snaplen
== 0) {
758 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
759 "snaplen of 0 rejects all packets");
764 if (pcap_lex_init(&scanner
) != 0)
765 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
766 errno
, "can't initialize scanner");
767 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
770 * Associate the compiler state with the lexical analyzer
773 pcap_set_extra(&cstate
, scanner
);
775 init_linktype(&cstate
, p
);
776 (void)pcap_parse(scanner
, &cstate
);
778 if (cstate
.ic
.root
== NULL
)
779 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
781 if (optimize
&& !cstate
.no_optimize
) {
782 bpf_optimize(&cstate
, &cstate
.ic
);
783 if (cstate
.ic
.root
== NULL
||
784 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
785 bpf_error(&cstate
, "expression rejects all packets");
787 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
788 program
->bf_len
= len
;
790 rc
= 0; /* We're all okay */
794 * Clean up everything for the lexical analyzer.
796 if (in_buffer
!= NULL
)
797 pcap__delete_buffer(in_buffer
, scanner
);
799 pcap_lex_destroy(scanner
);
802 * Clean up our own allocated memory.
810 * entry point for using the compiler with no pcap open
811 * pass in all the stuff that is needed explicitly instead.
814 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
815 struct bpf_program
*program
,
816 const char *buf
, int optimize
, bpf_u_int32 mask
)
821 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
824 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
830 * Clean up a "struct bpf_program" by freeing all the memory allocated
834 pcap_freecode(struct bpf_program
*program
)
837 if (program
->bf_insns
!= NULL
) {
838 free((char *)program
->bf_insns
);
839 program
->bf_insns
= NULL
;
844 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
845 * which of the jt and jf fields has been resolved and which is a pointer
846 * back to another unresolved block (or nil). At least one of the fields
847 * in each block is already resolved.
850 backpatch(struct block
*list
, struct block
*target
)
867 * Merge the lists in b0 and b1, using the 'sense' field to indicate
868 * which of jt and jf is the link.
871 merge(struct block
*b0
, struct block
*b1
)
873 register struct block
**p
= &b0
;
875 /* Find end of list. */
877 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
879 /* Concatenate the lists. */
884 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
886 struct block
*ppi_dlt_check
;
889 * Insert before the statements of the first (root) block any
890 * statements needed to load the lengths of any variable-length
891 * headers into registers.
893 * XXX - a fancier strategy would be to insert those before the
894 * statements of all blocks that use those lengths and that
895 * have no predecessors that use them, so that we only compute
896 * the lengths if we need them. There might be even better
897 * approaches than that.
899 * However, those strategies would be more complicated, and
900 * as we don't generate code to compute a length if the
901 * program has no tests that use the length, and as most
902 * tests will probably use those lengths, we would just
903 * postpone computing the lengths so that it's not done
904 * for tests that fail early, and it's not clear that's
907 insert_compute_vloffsets(cstate
, p
->head
);
910 * For DLT_PPI captures, generate a check of the per-packet
911 * DLT value to make sure it's DLT_IEEE802_11.
913 * XXX - TurboCap cards use DLT_PPI for Ethernet.
914 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
915 * with appropriate Ethernet information and use that rather
916 * than using something such as DLT_PPI where you don't know
917 * the link-layer header type until runtime, which, in the
918 * general case, would force us to generate both Ethernet *and*
919 * 802.11 code (*and* anything else for which PPI is used)
920 * and choose between them early in the BPF program?
922 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
923 if (ppi_dlt_check
!= NULL
)
924 gen_and(ppi_dlt_check
, p
);
926 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
927 p
->sense
= !p
->sense
;
928 backpatch(p
, gen_retblk(cstate
, 0));
929 cstate
->ic
.root
= p
->head
;
933 gen_and(struct block
*b0
, struct block
*b1
)
935 backpatch(b0
, b1
->head
);
936 b0
->sense
= !b0
->sense
;
937 b1
->sense
= !b1
->sense
;
939 b1
->sense
= !b1
->sense
;
944 gen_or(struct block
*b0
, struct block
*b1
)
946 b0
->sense
= !b0
->sense
;
947 backpatch(b0
, b1
->head
);
948 b0
->sense
= !b0
->sense
;
954 gen_not(struct block
*b
)
956 b
->sense
= !b
->sense
;
959 static struct block
*
960 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
961 u_int size
, bpf_int32 v
)
963 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
966 static struct block
*
967 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
968 u_int size
, bpf_int32 v
)
970 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
973 static struct block
*
974 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
975 u_int size
, bpf_int32 v
)
977 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
980 static struct block
*
981 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
982 u_int size
, bpf_int32 v
)
984 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
987 static struct block
*
988 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
989 u_int size
, bpf_int32 v
)
991 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
994 static struct block
*
995 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
996 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
998 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1001 static struct block
*
1002 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1003 u_int size
, const u_char
*v
)
1005 register struct block
*b
, *tmp
;
1008 * XXX - the actual *instructions* do unsigned comparisons on
1009 * most platforms, and the load instructions don't do sign
1010 * extension, so gen_cmp() should really take an unsigned
1013 * As the load instructons also don't do sign-extension, we
1014 * fetch the values from the byte array as unsigned. We don't
1015 * want to use the signed versions of the extract calls.
1019 register const u_char
*p
= &v
[size
- 4];
1021 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1022 (bpf_int32
)EXTRACT_BE_U_4(p
));
1029 register const u_char
*p
= &v
[size
- 2];
1031 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1032 (bpf_int32
)EXTRACT_BE_U_2(p
));
1039 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1048 * AND the field of size "size" at offset "offset" relative to the header
1049 * specified by "offrel" with "mask", and compare it with the value "v"
1050 * with the test specified by "jtype"; if "reverse" is true, the test
1051 * should test the opposite of "jtype".
1053 static struct block
*
1054 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1055 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1058 struct slist
*s
, *s2
;
1061 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1063 if (mask
!= 0xffffffff) {
1064 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1069 b
= new_block(cstate
, JMP(jtype
));
1072 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1078 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1080 cstate
->pcap_fddipad
= p
->fddipad
;
1083 * We start out with only one link-layer header.
1085 cstate
->outermostlinktype
= pcap_datalink(p
);
1086 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1087 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1088 cstate
->off_outermostlinkhdr
.reg
= -1;
1090 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1091 cstate
->off_prevlinkhdr
.constant_part
= 0;
1092 cstate
->off_prevlinkhdr
.is_variable
= 0;
1093 cstate
->off_prevlinkhdr
.reg
= -1;
1095 cstate
->linktype
= cstate
->outermostlinktype
;
1096 cstate
->off_linkhdr
.constant_part
= 0;
1097 cstate
->off_linkhdr
.is_variable
= 0;
1098 cstate
->off_linkhdr
.reg
= -1;
1103 cstate
->off_linkpl
.constant_part
= 0;
1104 cstate
->off_linkpl
.is_variable
= 0;
1105 cstate
->off_linkpl
.reg
= -1;
1107 cstate
->off_linktype
.constant_part
= 0;
1108 cstate
->off_linktype
.is_variable
= 0;
1109 cstate
->off_linktype
.reg
= -1;
1112 * Assume it's not raw ATM with a pseudo-header, for now.
1115 cstate
->off_vpi
= OFFSET_NOT_SET
;
1116 cstate
->off_vci
= OFFSET_NOT_SET
;
1117 cstate
->off_proto
= OFFSET_NOT_SET
;
1118 cstate
->off_payload
= OFFSET_NOT_SET
;
1123 cstate
->is_geneve
= 0;
1126 * No variable length VLAN offset by default
1128 cstate
->is_vlan_vloffset
= 0;
1131 * And assume we're not doing SS7.
1133 cstate
->off_li
= OFFSET_NOT_SET
;
1134 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1135 cstate
->off_sio
= OFFSET_NOT_SET
;
1136 cstate
->off_opc
= OFFSET_NOT_SET
;
1137 cstate
->off_dpc
= OFFSET_NOT_SET
;
1138 cstate
->off_sls
= OFFSET_NOT_SET
;
1140 cstate
->label_stack_depth
= 0;
1141 cstate
->vlan_stack_depth
= 0;
1143 switch (cstate
->linktype
) {
1146 cstate
->off_linktype
.constant_part
= 2;
1147 cstate
->off_linkpl
.constant_part
= 6;
1148 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1149 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1152 case DLT_ARCNET_LINUX
:
1153 cstate
->off_linktype
.constant_part
= 4;
1154 cstate
->off_linkpl
.constant_part
= 8;
1155 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1156 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1160 cstate
->off_linktype
.constant_part
= 12;
1161 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1162 cstate
->off_nl
= 0; /* Ethernet II */
1163 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1168 * SLIP doesn't have a link level type. The 16 byte
1169 * header is hacked into our SLIP driver.
1171 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1172 cstate
->off_linkpl
.constant_part
= 16;
1174 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1177 case DLT_SLIP_BSDOS
:
1178 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1179 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1181 cstate
->off_linkpl
.constant_part
= 24;
1183 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1188 cstate
->off_linktype
.constant_part
= 0;
1189 cstate
->off_linkpl
.constant_part
= 4;
1191 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1195 cstate
->off_linktype
.constant_part
= 0;
1196 cstate
->off_linkpl
.constant_part
= 12;
1198 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1203 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1204 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1205 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1206 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1208 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1213 * This does no include the Ethernet header, and
1214 * only covers session state.
1216 cstate
->off_linktype
.constant_part
= 6;
1217 cstate
->off_linkpl
.constant_part
= 8;
1219 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 cstate
->off_linktype
.constant_part
= 5;
1224 cstate
->off_linkpl
.constant_part
= 24;
1226 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1231 * FDDI doesn't really have a link-level type field.
1232 * We set "off_linktype" to the offset of the LLC header.
1234 * To check for Ethernet types, we assume that SSAP = SNAP
1235 * is being used and pick out the encapsulated Ethernet type.
1236 * XXX - should we generate code to check for SNAP?
1238 cstate
->off_linktype
.constant_part
= 13;
1239 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1240 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1241 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1242 cstate
->off_nl
= 8; /* 802.2+SNAP */
1243 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1248 * Token Ring doesn't really have a link-level type field.
1249 * We set "off_linktype" to the offset of the LLC header.
1251 * To check for Ethernet types, we assume that SSAP = SNAP
1252 * is being used and pick out the encapsulated Ethernet type.
1253 * XXX - should we generate code to check for SNAP?
1255 * XXX - the header is actually variable-length.
1256 * Some various Linux patched versions gave 38
1257 * as "off_linktype" and 40 as "off_nl"; however,
1258 * if a token ring packet has *no* routing
1259 * information, i.e. is not source-routed, the correct
1260 * values are 20 and 22, as they are in the vanilla code.
1262 * A packet is source-routed iff the uppermost bit
1263 * of the first byte of the source address, at an
1264 * offset of 8, has the uppermost bit set. If the
1265 * packet is source-routed, the total number of bytes
1266 * of routing information is 2 plus bits 0x1F00 of
1267 * the 16-bit value at an offset of 14 (shifted right
1268 * 8 - figure out which byte that is).
1270 cstate
->off_linktype
.constant_part
= 14;
1271 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1272 cstate
->off_nl
= 8; /* 802.2+SNAP */
1273 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1276 case DLT_PRISM_HEADER
:
1277 case DLT_IEEE802_11_RADIO_AVS
:
1278 case DLT_IEEE802_11_RADIO
:
1279 cstate
->off_linkhdr
.is_variable
= 1;
1280 /* Fall through, 802.11 doesn't have a variable link
1281 * prefix but is otherwise the same. */
1283 case DLT_IEEE802_11
:
1285 * 802.11 doesn't really have a link-level type field.
1286 * We set "off_linktype.constant_part" to the offset of
1289 * To check for Ethernet types, we assume that SSAP = SNAP
1290 * is being used and pick out the encapsulated Ethernet type.
1291 * XXX - should we generate code to check for SNAP?
1293 * We also handle variable-length radio headers here.
1294 * The Prism header is in theory variable-length, but in
1295 * practice it's always 144 bytes long. However, some
1296 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1297 * sometimes or always supply an AVS header, so we
1298 * have to check whether the radio header is a Prism
1299 * header or an AVS header, so, in practice, it's
1302 cstate
->off_linktype
.constant_part
= 24;
1303 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1304 cstate
->off_linkpl
.is_variable
= 1;
1305 cstate
->off_nl
= 8; /* 802.2+SNAP */
1306 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1311 * At the moment we treat PPI the same way that we treat
1312 * normal Radiotap encoded packets. The difference is in
1313 * the function that generates the code at the beginning
1314 * to compute the header length. Since this code generator
1315 * of PPI supports bare 802.11 encapsulation only (i.e.
1316 * the encapsulated DLT should be DLT_IEEE802_11) we
1317 * generate code to check for this too.
1319 cstate
->off_linktype
.constant_part
= 24;
1320 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1321 cstate
->off_linkpl
.is_variable
= 1;
1322 cstate
->off_linkhdr
.is_variable
= 1;
1323 cstate
->off_nl
= 8; /* 802.2+SNAP */
1324 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1327 case DLT_ATM_RFC1483
:
1328 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1330 * assume routed, non-ISO PDUs
1331 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1333 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1334 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1335 * latter would presumably be treated the way PPPoE
1336 * should be, so you can do "pppoe and udp port 2049"
1337 * or "pppoa and tcp port 80" and have it check for
1338 * PPPo{A,E} and a PPP protocol of IP and....
1340 cstate
->off_linktype
.constant_part
= 0;
1341 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1342 cstate
->off_nl
= 8; /* 802.2+SNAP */
1343 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1348 * Full Frontal ATM; you get AALn PDUs with an ATM
1352 cstate
->off_vpi
= SUNATM_VPI_POS
;
1353 cstate
->off_vci
= SUNATM_VCI_POS
;
1354 cstate
->off_proto
= PROTO_POS
;
1355 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1356 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1357 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1358 cstate
->off_nl
= 8; /* 802.2+SNAP */
1359 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1365 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1366 cstate
->off_linkpl
.constant_part
= 0;
1368 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1371 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1372 cstate
->off_linktype
.constant_part
= 14;
1373 cstate
->off_linkpl
.constant_part
= 16;
1375 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1378 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1379 cstate
->off_linktype
.constant_part
= 0;
1380 cstate
->off_linkpl
.constant_part
= 20;
1382 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1387 * LocalTalk does have a 1-byte type field in the LLAP header,
1388 * but really it just indicates whether there is a "short" or
1389 * "long" DDP packet following.
1391 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1392 cstate
->off_linkpl
.constant_part
= 0;
1394 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1397 case DLT_IP_OVER_FC
:
1399 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1400 * link-level type field. We set "off_linktype" to the
1401 * offset of the LLC header.
1403 * To check for Ethernet types, we assume that SSAP = SNAP
1404 * is being used and pick out the encapsulated Ethernet type.
1405 * XXX - should we generate code to check for SNAP? RFC
1406 * 2625 says SNAP should be used.
1408 cstate
->off_linktype
.constant_part
= 16;
1409 cstate
->off_linkpl
.constant_part
= 16;
1410 cstate
->off_nl
= 8; /* 802.2+SNAP */
1411 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1416 * XXX - we should set this to handle SNAP-encapsulated
1417 * frames (NLPID of 0x80).
1419 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1420 cstate
->off_linkpl
.constant_part
= 0;
1422 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1426 * the only BPF-interesting FRF.16 frames are non-control frames;
1427 * Frame Relay has a variable length link-layer
1428 * so lets start with offset 4 for now and increments later on (FIXME);
1431 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1432 cstate
->off_linkpl
.constant_part
= 0;
1434 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1437 case DLT_APPLE_IP_OVER_IEEE1394
:
1438 cstate
->off_linktype
.constant_part
= 16;
1439 cstate
->off_linkpl
.constant_part
= 18;
1441 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1444 case DLT_SYMANTEC_FIREWALL
:
1445 cstate
->off_linktype
.constant_part
= 6;
1446 cstate
->off_linkpl
.constant_part
= 44;
1447 cstate
->off_nl
= 0; /* Ethernet II */
1448 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1451 #ifdef HAVE_NET_PFVAR_H
1453 cstate
->off_linktype
.constant_part
= 0;
1454 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1456 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1460 case DLT_JUNIPER_MFR
:
1461 case DLT_JUNIPER_MLFR
:
1462 case DLT_JUNIPER_MLPPP
:
1463 case DLT_JUNIPER_PPP
:
1464 case DLT_JUNIPER_CHDLC
:
1465 case DLT_JUNIPER_FRELAY
:
1466 cstate
->off_linktype
.constant_part
= 4;
1467 cstate
->off_linkpl
.constant_part
= 4;
1469 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1472 case DLT_JUNIPER_ATM1
:
1473 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1474 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1476 cstate
->off_nl_nosnap
= 10;
1479 case DLT_JUNIPER_ATM2
:
1480 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1481 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1483 cstate
->off_nl_nosnap
= 10;
1486 /* frames captured on a Juniper PPPoE service PIC
1487 * contain raw ethernet frames */
1488 case DLT_JUNIPER_PPPOE
:
1489 case DLT_JUNIPER_ETHER
:
1490 cstate
->off_linkpl
.constant_part
= 14;
1491 cstate
->off_linktype
.constant_part
= 16;
1492 cstate
->off_nl
= 18; /* Ethernet II */
1493 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1496 case DLT_JUNIPER_PPPOE_ATM
:
1497 cstate
->off_linktype
.constant_part
= 4;
1498 cstate
->off_linkpl
.constant_part
= 6;
1500 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1503 case DLT_JUNIPER_GGSN
:
1504 cstate
->off_linktype
.constant_part
= 6;
1505 cstate
->off_linkpl
.constant_part
= 12;
1507 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1510 case DLT_JUNIPER_ES
:
1511 cstate
->off_linktype
.constant_part
= 6;
1512 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1513 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1514 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1517 case DLT_JUNIPER_MONITOR
:
1518 cstate
->off_linktype
.constant_part
= 12;
1519 cstate
->off_linkpl
.constant_part
= 12;
1520 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1521 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1524 case DLT_BACNET_MS_TP
:
1525 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1526 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1527 cstate
->off_nl
= OFFSET_NOT_SET
;
1528 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1531 case DLT_JUNIPER_SERVICES
:
1532 cstate
->off_linktype
.constant_part
= 12;
1533 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1534 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1535 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1538 case DLT_JUNIPER_VP
:
1539 cstate
->off_linktype
.constant_part
= 18;
1540 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1541 cstate
->off_nl
= OFFSET_NOT_SET
;
1542 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1545 case DLT_JUNIPER_ST
:
1546 cstate
->off_linktype
.constant_part
= 18;
1547 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1548 cstate
->off_nl
= OFFSET_NOT_SET
;
1549 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1552 case DLT_JUNIPER_ISM
:
1553 cstate
->off_linktype
.constant_part
= 8;
1554 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1555 cstate
->off_nl
= OFFSET_NOT_SET
;
1556 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1559 case DLT_JUNIPER_VS
:
1560 case DLT_JUNIPER_SRX_E2E
:
1561 case DLT_JUNIPER_FIBRECHANNEL
:
1562 case DLT_JUNIPER_ATM_CEMIC
:
1563 cstate
->off_linktype
.constant_part
= 8;
1564 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1565 cstate
->off_nl
= OFFSET_NOT_SET
;
1566 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1571 cstate
->off_li_hsl
= 4;
1572 cstate
->off_sio
= 3;
1573 cstate
->off_opc
= 4;
1574 cstate
->off_dpc
= 4;
1575 cstate
->off_sls
= 7;
1576 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1577 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1578 cstate
->off_nl
= OFFSET_NOT_SET
;
1579 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1582 case DLT_MTP2_WITH_PHDR
:
1584 cstate
->off_li_hsl
= 8;
1585 cstate
->off_sio
= 7;
1586 cstate
->off_opc
= 8;
1587 cstate
->off_dpc
= 8;
1588 cstate
->off_sls
= 11;
1589 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1590 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1591 cstate
->off_nl
= OFFSET_NOT_SET
;
1592 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1596 cstate
->off_li
= 22;
1597 cstate
->off_li_hsl
= 24;
1598 cstate
->off_sio
= 23;
1599 cstate
->off_opc
= 24;
1600 cstate
->off_dpc
= 24;
1601 cstate
->off_sls
= 27;
1602 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1603 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1604 cstate
->off_nl
= OFFSET_NOT_SET
;
1605 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1609 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1610 cstate
->off_linkpl
.constant_part
= 4;
1612 cstate
->off_nl_nosnap
= 0;
1617 * Currently, only raw "link[N:M]" filtering is supported.
1619 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1620 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1621 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1622 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1626 cstate
->off_linktype
.constant_part
= 1;
1627 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1629 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1632 case DLT_NETANALYZER
:
1633 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1634 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1635 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1636 cstate
->off_nl
= 0; /* Ethernet II */
1637 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1640 case DLT_NETANALYZER_TRANSPARENT
:
1641 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1642 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1643 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1644 cstate
->off_nl
= 0; /* Ethernet II */
1645 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1650 * For values in the range in which we've assigned new
1651 * DLT_ values, only raw "link[N:M]" filtering is supported.
1653 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1654 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1655 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1656 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1657 cstate
->off_nl
= OFFSET_NOT_SET
;
1658 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1660 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1665 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1669 * Load a value relative to the specified absolute offset.
1671 static struct slist
*
1672 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1673 u_int offset
, u_int size
)
1675 struct slist
*s
, *s2
;
1677 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1680 * If "s" is non-null, it has code to arrange that the X register
1681 * contains the variable part of the absolute offset, so we
1682 * generate a load relative to that, with an offset of
1683 * abs_offset->constant_part + offset.
1685 * Otherwise, we can do an absolute load with an offset of
1686 * abs_offset->constant_part + offset.
1690 * "s" points to a list of statements that puts the
1691 * variable part of the absolute offset into the X register.
1692 * Do an indirect load, to use the X register as an offset.
1694 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1695 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1699 * There is no variable part of the absolute offset, so
1700 * just do an absolute load.
1702 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1703 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1709 * Load a value relative to the beginning of the specified header.
1711 static struct slist
*
1712 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1715 struct slist
*s
, *s2
;
1720 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1725 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1728 case OR_PREVLINKHDR
:
1729 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1733 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1736 case OR_PREVMPLSHDR
:
1737 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1741 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1744 case OR_LINKPL_NOSNAP
:
1745 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1749 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1754 * Load the X register with the length of the IPv4 header
1755 * (plus the offset of the link-layer header, if it's
1756 * preceded by a variable-length header such as a radio
1757 * header), in bytes.
1759 s
= gen_loadx_iphdrlen(cstate
);
1762 * Load the item at {offset of the link-layer payload} +
1763 * {offset, relative to the start of the link-layer
1764 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1765 * {specified offset}.
1767 * If the offset of the link-layer payload is variable,
1768 * the variable part of that offset is included in the
1769 * value in the X register, and we include the constant
1770 * part in the offset of the load.
1772 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1773 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1778 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1789 * Generate code to load into the X register the sum of the length of
1790 * the IPv4 header and the variable part of the offset of the link-layer
1793 static struct slist
*
1794 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1796 struct slist
*s
, *s2
;
1798 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1801 * The offset of the link-layer payload has a variable
1802 * part. "s" points to a list of statements that put
1803 * the variable part of that offset into the X register.
1805 * The 4*([k]&0xf) addressing mode can't be used, as we
1806 * don't have a constant offset, so we have to load the
1807 * value in question into the A register and add to it
1808 * the value from the X register.
1810 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1811 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1813 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1816 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1821 * The A register now contains the length of the IP header.
1822 * We need to add to it the variable part of the offset of
1823 * the link-layer payload, which is still in the X
1824 * register, and move the result into the X register.
1826 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1827 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1830 * The offset of the link-layer payload is a constant,
1831 * so no code was generated to load the (non-existent)
1832 * variable part of that offset.
1834 * This means we can use the 4*([k]&0xf) addressing
1835 * mode. Load the length of the IPv4 header, which
1836 * is at an offset of cstate->off_nl from the beginning of
1837 * the link-layer payload, and thus at an offset of
1838 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1839 * of the raw packet data, using that addressing mode.
1841 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1842 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1848 static struct block
*
1849 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1854 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1856 b
= new_block(cstate
, JMP(BPF_JEQ
));
1862 static inline struct block
*
1863 gen_true(compiler_state_t
*cstate
)
1865 return gen_uncond(cstate
, 1);
1868 static inline struct block
*
1869 gen_false(compiler_state_t
*cstate
)
1871 return gen_uncond(cstate
, 0);
1875 * Byte-swap a 32-bit number.
1876 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1877 * big-endian platforms.)
1879 #define SWAPLONG(y) \
1880 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1883 * Generate code to match a particular packet type.
1885 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1886 * value, if <= ETHERMTU. We use that to determine whether to
1887 * match the type/length field or to check the type/length field for
1888 * a value <= ETHERMTU to see whether it's a type field and then do
1889 * the appropriate test.
1891 static struct block
*
1892 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1894 struct block
*b0
, *b1
;
1900 case LLCSAP_NETBEUI
:
1902 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1903 * so we check the DSAP and SSAP.
1905 * LLCSAP_IP checks for IP-over-802.2, rather
1906 * than IP-over-Ethernet or IP-over-SNAP.
1908 * XXX - should we check both the DSAP and the
1909 * SSAP, like this, or should we check just the
1910 * DSAP, as we do for other types <= ETHERMTU
1911 * (i.e., other SAP values)?
1913 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1915 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1916 ((proto
<< 8) | proto
));
1924 * Ethernet_II frames, which are Ethernet
1925 * frames with a frame type of ETHERTYPE_IPX;
1927 * Ethernet_802.3 frames, which are 802.3
1928 * frames (i.e., the type/length field is
1929 * a length field, <= ETHERMTU, rather than
1930 * a type field) with the first two bytes
1931 * after the Ethernet/802.3 header being
1934 * Ethernet_802.2 frames, which are 802.3
1935 * frames with an 802.2 LLC header and
1936 * with the IPX LSAP as the DSAP in the LLC
1939 * Ethernet_SNAP frames, which are 802.3
1940 * frames with an LLC header and a SNAP
1941 * header and with an OUI of 0x000000
1942 * (encapsulated Ethernet) and a protocol
1943 * ID of ETHERTYPE_IPX in the SNAP header.
1945 * XXX - should we generate the same code both
1946 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1950 * This generates code to check both for the
1951 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1953 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1954 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1958 * Now we add code to check for SNAP frames with
1959 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1961 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1965 * Now we generate code to check for 802.3
1966 * frames in general.
1968 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1972 * Now add the check for 802.3 frames before the
1973 * check for Ethernet_802.2 and Ethernet_802.3,
1974 * as those checks should only be done on 802.3
1975 * frames, not on Ethernet frames.
1980 * Now add the check for Ethernet_II frames, and
1981 * do that before checking for the other frame
1984 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1988 case ETHERTYPE_ATALK
:
1989 case ETHERTYPE_AARP
:
1991 * EtherTalk (AppleTalk protocols on Ethernet link
1992 * layer) may use 802.2 encapsulation.
1996 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1997 * we check for an Ethernet type field less than
1998 * 1500, which means it's an 802.3 length field.
2000 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2004 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2005 * SNAP packets with an organization code of
2006 * 0x080007 (Apple, for Appletalk) and a protocol
2007 * type of ETHERTYPE_ATALK (Appletalk).
2009 * 802.2-encapsulated ETHERTYPE_AARP packets are
2010 * SNAP packets with an organization code of
2011 * 0x000000 (encapsulated Ethernet) and a protocol
2012 * type of ETHERTYPE_AARP (Appletalk ARP).
2014 if (proto
== ETHERTYPE_ATALK
)
2015 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2016 else /* proto == ETHERTYPE_AARP */
2017 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2021 * Check for Ethernet encapsulation (Ethertalk
2022 * phase 1?); we just check for the Ethernet
2025 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2031 if (proto
<= ETHERMTU
) {
2033 * This is an LLC SAP value, so the frames
2034 * that match would be 802.2 frames.
2035 * Check that the frame is an 802.2 frame
2036 * (i.e., that the length/type field is
2037 * a length field, <= ETHERMTU) and
2038 * then check the DSAP.
2040 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2042 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2047 * This is an Ethernet type, so compare
2048 * the length/type field with it (if
2049 * the frame is an 802.2 frame, the length
2050 * field will be <= ETHERMTU, and, as
2051 * "proto" is > ETHERMTU, this test
2052 * will fail and the frame won't match,
2053 * which is what we want).
2055 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2061 static struct block
*
2062 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2065 * For DLT_NULL, the link-layer header is a 32-bit word
2066 * containing an AF_ value in *host* byte order, and for
2067 * DLT_ENC, the link-layer header begins with a 32-bit
2068 * word containing an AF_ value in host byte order.
2070 * In addition, if we're reading a saved capture file,
2071 * the host byte order in the capture may not be the
2072 * same as the host byte order on this machine.
2074 * For DLT_LOOP, the link-layer header is a 32-bit
2075 * word containing an AF_ value in *network* byte order.
2077 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2079 * The AF_ value is in host byte order, but the BPF
2080 * interpreter will convert it to network byte order.
2082 * If this is a save file, and it's from a machine
2083 * with the opposite byte order to ours, we byte-swap
2086 * Then we run it through "htonl()", and generate
2087 * code to compare against the result.
2089 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2090 proto
= SWAPLONG(proto
);
2091 proto
= htonl(proto
);
2093 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2097 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2098 * or IPv6 then we have an error.
2100 static struct block
*
2101 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2106 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2109 case ETHERTYPE_IPV6
:
2110 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2111 (bpf_int32
)IPH_AF_INET6
);
2118 return gen_false(cstate
);
2122 * Generate code to match a particular packet type.
2124 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2125 * value, if <= ETHERMTU. We use that to determine whether to
2126 * match the type field or to check the type field for the special
2127 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2129 static struct block
*
2130 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2132 struct block
*b0
, *b1
;
2138 case LLCSAP_NETBEUI
:
2140 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2141 * so we check the DSAP and SSAP.
2143 * LLCSAP_IP checks for IP-over-802.2, rather
2144 * than IP-over-Ethernet or IP-over-SNAP.
2146 * XXX - should we check both the DSAP and the
2147 * SSAP, like this, or should we check just the
2148 * DSAP, as we do for other types <= ETHERMTU
2149 * (i.e., other SAP values)?
2151 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2152 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2153 ((proto
<< 8) | proto
));
2159 * Ethernet_II frames, which are Ethernet
2160 * frames with a frame type of ETHERTYPE_IPX;
2162 * Ethernet_802.3 frames, which have a frame
2163 * type of LINUX_SLL_P_802_3;
2165 * Ethernet_802.2 frames, which are 802.3
2166 * frames with an 802.2 LLC header (i.e, have
2167 * a frame type of LINUX_SLL_P_802_2) and
2168 * with the IPX LSAP as the DSAP in the LLC
2171 * Ethernet_SNAP frames, which are 802.3
2172 * frames with an LLC header and a SNAP
2173 * header and with an OUI of 0x000000
2174 * (encapsulated Ethernet) and a protocol
2175 * ID of ETHERTYPE_IPX in the SNAP header.
2177 * First, do the checks on LINUX_SLL_P_802_2
2178 * frames; generate the check for either
2179 * Ethernet_802.2 or Ethernet_SNAP frames, and
2180 * then put a check for LINUX_SLL_P_802_2 frames
2183 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2184 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2186 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2190 * Now check for 802.3 frames and OR that with
2191 * the previous test.
2193 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2197 * Now add the check for Ethernet_II frames, and
2198 * do that before checking for the other frame
2201 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2205 case ETHERTYPE_ATALK
:
2206 case ETHERTYPE_AARP
:
2208 * EtherTalk (AppleTalk protocols on Ethernet link
2209 * layer) may use 802.2 encapsulation.
2213 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2214 * we check for the 802.2 protocol type in the
2215 * "Ethernet type" field.
2217 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2220 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2221 * SNAP packets with an organization code of
2222 * 0x080007 (Apple, for Appletalk) and a protocol
2223 * type of ETHERTYPE_ATALK (Appletalk).
2225 * 802.2-encapsulated ETHERTYPE_AARP packets are
2226 * SNAP packets with an organization code of
2227 * 0x000000 (encapsulated Ethernet) and a protocol
2228 * type of ETHERTYPE_AARP (Appletalk ARP).
2230 if (proto
== ETHERTYPE_ATALK
)
2231 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2232 else /* proto == ETHERTYPE_AARP */
2233 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2237 * Check for Ethernet encapsulation (Ethertalk
2238 * phase 1?); we just check for the Ethernet
2241 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2247 if (proto
<= ETHERMTU
) {
2249 * This is an LLC SAP value, so the frames
2250 * that match would be 802.2 frames.
2251 * Check for the 802.2 protocol type
2252 * in the "Ethernet type" field, and
2253 * then check the DSAP.
2255 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2256 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2262 * This is an Ethernet type, so compare
2263 * the length/type field with it (if
2264 * the frame is an 802.2 frame, the length
2265 * field will be <= ETHERMTU, and, as
2266 * "proto" is > ETHERMTU, this test
2267 * will fail and the frame won't match,
2268 * which is what we want).
2270 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2275 static struct slist
*
2276 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2278 struct slist
*s1
, *s2
;
2279 struct slist
*sjeq_avs_cookie
;
2280 struct slist
*sjcommon
;
2283 * This code is not compatible with the optimizer, as
2284 * we are generating jmp instructions within a normal
2285 * slist of instructions
2287 cstate
->no_optimize
= 1;
2290 * Generate code to load the length of the radio header into
2291 * the register assigned to hold that length, if one has been
2292 * assigned. (If one hasn't been assigned, no code we've
2293 * generated uses that prefix, so we don't need to generate any
2296 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2297 * or always use the AVS header rather than the Prism header.
2298 * We load a 4-byte big-endian value at the beginning of the
2299 * raw packet data, and see whether, when masked with 0xFFFFF000,
2300 * it's equal to 0x80211000. If so, that indicates that it's
2301 * an AVS header (the masked-out bits are the version number).
2302 * Otherwise, it's a Prism header.
2304 * XXX - the Prism header is also, in theory, variable-length,
2305 * but no known software generates headers that aren't 144
2308 if (cstate
->off_linkhdr
.reg
!= -1) {
2312 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2316 * AND it with 0xFFFFF000.
2318 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2319 s2
->s
.k
= 0xFFFFF000;
2323 * Compare with 0x80211000.
2325 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2326 sjeq_avs_cookie
->s
.k
= 0x80211000;
2327 sappend(s1
, sjeq_avs_cookie
);
2332 * The 4 bytes at an offset of 4 from the beginning of
2333 * the AVS header are the length of the AVS header.
2334 * That field is big-endian.
2336 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2339 sjeq_avs_cookie
->s
.jt
= s2
;
2342 * Now jump to the code to allocate a register
2343 * into which to save the header length and
2344 * store the length there. (The "jump always"
2345 * instruction needs to have the k field set;
2346 * it's added to the PC, so, as we're jumping
2347 * over a single instruction, it should be 1.)
2349 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2351 sappend(s1
, sjcommon
);
2354 * Now for the code that handles the Prism header.
2355 * Just load the length of the Prism header (144)
2356 * into the A register. Have the test for an AVS
2357 * header branch here if we don't have an AVS header.
2359 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2362 sjeq_avs_cookie
->s
.jf
= s2
;
2365 * Now allocate a register to hold that value and store
2366 * it. The code for the AVS header will jump here after
2367 * loading the length of the AVS header.
2369 s2
= new_stmt(cstate
, BPF_ST
);
2370 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2372 sjcommon
->s
.jf
= s2
;
2375 * Now move it into the X register.
2377 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2385 static struct slist
*
2386 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2388 struct slist
*s1
, *s2
;
2391 * Generate code to load the length of the AVS header into
2392 * the register assigned to hold that length, if one has been
2393 * assigned. (If one hasn't been assigned, no code we've
2394 * generated uses that prefix, so we don't need to generate any
2397 if (cstate
->off_linkhdr
.reg
!= -1) {
2399 * The 4 bytes at an offset of 4 from the beginning of
2400 * the AVS header are the length of the AVS header.
2401 * That field is big-endian.
2403 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2407 * Now allocate a register to hold that value and store
2410 s2
= new_stmt(cstate
, BPF_ST
);
2411 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2415 * Now move it into the X register.
2417 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2425 static struct slist
*
2426 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2428 struct slist
*s1
, *s2
;
2431 * Generate code to load the length of the radiotap header into
2432 * the register assigned to hold that length, if one has been
2433 * assigned. (If one hasn't been assigned, no code we've
2434 * generated uses that prefix, so we don't need to generate any
2437 if (cstate
->off_linkhdr
.reg
!= -1) {
2439 * The 2 bytes at offsets of 2 and 3 from the beginning
2440 * of the radiotap header are the length of the radiotap
2441 * header; unfortunately, it's little-endian, so we have
2442 * to load it a byte at a time and construct the value.
2446 * Load the high-order byte, at an offset of 3, shift it
2447 * left a byte, and put the result in the X register.
2449 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2451 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2454 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2458 * Load the next byte, at an offset of 2, and OR the
2459 * value from the X register into it.
2461 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2464 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2468 * Now allocate a register to hold that value and store
2471 s2
= new_stmt(cstate
, BPF_ST
);
2472 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2476 * Now move it into the X register.
2478 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2487 * At the moment we treat PPI as normal Radiotap encoded
2488 * packets. The difference is in the function that generates
2489 * the code at the beginning to compute the header length.
2490 * Since this code generator of PPI supports bare 802.11
2491 * encapsulation only (i.e. the encapsulated DLT should be
2492 * DLT_IEEE802_11) we generate code to check for this too;
2493 * that's done in finish_parse().
2495 static struct slist
*
2496 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2498 struct slist
*s1
, *s2
;
2501 * Generate code to load the length of the radiotap header
2502 * into the register assigned to hold that length, if one has
2505 if (cstate
->off_linkhdr
.reg
!= -1) {
2507 * The 2 bytes at offsets of 2 and 3 from the beginning
2508 * of the radiotap header are the length of the radiotap
2509 * header; unfortunately, it's little-endian, so we have
2510 * to load it a byte at a time and construct the value.
2514 * Load the high-order byte, at an offset of 3, shift it
2515 * left a byte, and put the result in the X register.
2517 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2519 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2522 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2526 * Load the next byte, at an offset of 2, and OR the
2527 * value from the X register into it.
2529 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2532 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2536 * Now allocate a register to hold that value and store
2539 s2
= new_stmt(cstate
, BPF_ST
);
2540 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2544 * Now move it into the X register.
2546 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2555 * Load a value relative to the beginning of the link-layer header after the 802.11
2556 * header, i.e. LLC_SNAP.
2557 * The link-layer header doesn't necessarily begin at the beginning
2558 * of the packet data; there might be a variable-length prefix containing
2559 * radio information.
2561 static struct slist
*
2562 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2565 struct slist
*sjset_data_frame_1
;
2566 struct slist
*sjset_data_frame_2
;
2567 struct slist
*sjset_qos
;
2568 struct slist
*sjset_radiotap_flags_present
;
2569 struct slist
*sjset_radiotap_ext_present
;
2570 struct slist
*sjset_radiotap_tsft_present
;
2571 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2572 struct slist
*s_roundup
;
2574 if (cstate
->off_linkpl
.reg
== -1) {
2576 * No register has been assigned to the offset of
2577 * the link-layer payload, which means nobody needs
2578 * it; don't bother computing it - just return
2579 * what we already have.
2585 * This code is not compatible with the optimizer, as
2586 * we are generating jmp instructions within a normal
2587 * slist of instructions
2589 cstate
->no_optimize
= 1;
2592 * If "s" is non-null, it has code to arrange that the X register
2593 * contains the length of the prefix preceding the link-layer
2596 * Otherwise, the length of the prefix preceding the link-layer
2597 * header is "off_outermostlinkhdr.constant_part".
2601 * There is no variable-length header preceding the
2602 * link-layer header.
2604 * Load the length of the fixed-length prefix preceding
2605 * the link-layer header (if any) into the X register,
2606 * and store it in the cstate->off_linkpl.reg register.
2607 * That length is off_outermostlinkhdr.constant_part.
2609 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2610 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2614 * The X register contains the offset of the beginning of the
2615 * link-layer header; add 24, which is the minimum length
2616 * of the MAC header for a data frame, to that, and store it
2617 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2618 * which is at the offset in the X register, with an indexed load.
2620 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2622 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2625 s2
= new_stmt(cstate
, BPF_ST
);
2626 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2629 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2634 * Check the Frame Control field to see if this is a data frame;
2635 * a data frame has the 0x08 bit (b3) in that field set and the
2636 * 0x04 bit (b2) clear.
2638 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2639 sjset_data_frame_1
->s
.k
= 0x08;
2640 sappend(s
, sjset_data_frame_1
);
2643 * If b3 is set, test b2, otherwise go to the first statement of
2644 * the rest of the program.
2646 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2647 sjset_data_frame_2
->s
.k
= 0x04;
2648 sappend(s
, sjset_data_frame_2
);
2649 sjset_data_frame_1
->s
.jf
= snext
;
2652 * If b2 is not set, this is a data frame; test the QoS bit.
2653 * Otherwise, go to the first statement of the rest of the
2656 sjset_data_frame_2
->s
.jt
= snext
;
2657 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2658 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2659 sappend(s
, sjset_qos
);
2662 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2664 * Otherwise, go to the first statement of the rest of the
2667 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2668 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2670 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2673 s2
= new_stmt(cstate
, BPF_ST
);
2674 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2678 * If we have a radiotap header, look at it to see whether
2679 * there's Atheros padding between the MAC-layer header
2682 * Note: all of the fields in the radiotap header are
2683 * little-endian, so we byte-swap all of the values
2684 * we test against, as they will be loaded as big-endian
2687 * XXX - in the general case, we would have to scan through
2688 * *all* the presence bits, if there's more than one word of
2689 * presence bits. That would require a loop, meaning that
2690 * we wouldn't be able to run the filter in the kernel.
2692 * We assume here that the Atheros adapters that insert the
2693 * annoying padding don't have multiple antennae and therefore
2694 * do not generate radiotap headers with multiple presence words.
2696 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2698 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2699 * in the first presence flag word?
2701 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2705 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2706 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2707 sappend(s
, sjset_radiotap_flags_present
);
2710 * If not, skip all of this.
2712 sjset_radiotap_flags_present
->s
.jf
= snext
;
2715 * Otherwise, is the "extension" bit set in that word?
2717 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2718 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2719 sappend(s
, sjset_radiotap_ext_present
);
2720 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2723 * If so, skip all of this.
2725 sjset_radiotap_ext_present
->s
.jt
= snext
;
2728 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2730 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2731 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2732 sappend(s
, sjset_radiotap_tsft_present
);
2733 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2736 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2737 * at an offset of 16 from the beginning of the raw packet
2738 * data (8 bytes for the radiotap header and 8 bytes for
2741 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2744 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2747 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2749 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2750 sjset_tsft_datapad
->s
.k
= 0x20;
2751 sappend(s
, sjset_tsft_datapad
);
2754 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2755 * at an offset of 8 from the beginning of the raw packet
2756 * data (8 bytes for the radiotap header).
2758 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2761 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2764 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2766 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2767 sjset_notsft_datapad
->s
.k
= 0x20;
2768 sappend(s
, sjset_notsft_datapad
);
2771 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2772 * set, round the length of the 802.11 header to
2773 * a multiple of 4. Do that by adding 3 and then
2774 * dividing by and multiplying by 4, which we do by
2777 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2778 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2779 sappend(s
, s_roundup
);
2780 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2783 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2786 s2
= new_stmt(cstate
, BPF_ST
);
2787 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2790 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2791 sjset_tsft_datapad
->s
.jf
= snext
;
2792 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2793 sjset_notsft_datapad
->s
.jf
= snext
;
2795 sjset_qos
->s
.jf
= snext
;
2801 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2805 /* There is an implicit dependency between the link
2806 * payload and link header since the payload computation
2807 * includes the variable part of the header. Therefore,
2808 * if nobody else has allocated a register for the link
2809 * header and we need it, do it now. */
2810 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2811 cstate
->off_linkhdr
.reg
== -1)
2812 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2815 * For link-layer types that have a variable-length header
2816 * preceding the link-layer header, generate code to load
2817 * the offset of the link-layer header into the register
2818 * assigned to that offset, if any.
2820 * XXX - this, and the next switch statement, won't handle
2821 * encapsulation of 802.11 or 802.11+radio information in
2822 * some other protocol stack. That's significantly more
2825 switch (cstate
->outermostlinktype
) {
2827 case DLT_PRISM_HEADER
:
2828 s
= gen_load_prism_llprefixlen(cstate
);
2831 case DLT_IEEE802_11_RADIO_AVS
:
2832 s
= gen_load_avs_llprefixlen(cstate
);
2835 case DLT_IEEE802_11_RADIO
:
2836 s
= gen_load_radiotap_llprefixlen(cstate
);
2840 s
= gen_load_ppi_llprefixlen(cstate
);
2849 * For link-layer types that have a variable-length link-layer
2850 * header, generate code to load the offset of the link-layer
2851 * payload into the register assigned to that offset, if any.
2853 switch (cstate
->outermostlinktype
) {
2855 case DLT_IEEE802_11
:
2856 case DLT_PRISM_HEADER
:
2857 case DLT_IEEE802_11_RADIO_AVS
:
2858 case DLT_IEEE802_11_RADIO
:
2860 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2865 * If there there is no initialization yet and we need variable
2866 * length offsets for VLAN, initialize them to zero
2868 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2871 if (cstate
->off_linkpl
.reg
== -1)
2872 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2873 if (cstate
->off_linktype
.reg
== -1)
2874 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2876 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2878 s2
= new_stmt(cstate
, BPF_ST
);
2879 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2881 s2
= new_stmt(cstate
, BPF_ST
);
2882 s2
->s
.k
= cstate
->off_linktype
.reg
;
2887 * If we have any offset-loading code, append all the
2888 * existing statements in the block to those statements,
2889 * and make the resulting list the list of statements
2893 sappend(s
, b
->stmts
);
2898 static struct block
*
2899 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2901 struct slist
*s_load_dlt
;
2904 if (cstate
->linktype
== DLT_PPI
)
2906 /* Create the statements that check for the DLT
2908 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2909 s_load_dlt
->s
.k
= 4;
2911 b
= new_block(cstate
, JMP(BPF_JEQ
));
2913 b
->stmts
= s_load_dlt
;
2914 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2925 * Take an absolute offset, and:
2927 * if it has no variable part, return NULL;
2929 * if it has a variable part, generate code to load the register
2930 * containing that variable part into the X register, returning
2931 * a pointer to that code - if no register for that offset has
2932 * been allocated, allocate it first.
2934 * (The code to set that register will be generated later, but will
2935 * be placed earlier in the code sequence.)
2937 static struct slist
*
2938 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2942 if (off
->is_variable
) {
2943 if (off
->reg
== -1) {
2945 * We haven't yet assigned a register for the
2946 * variable part of the offset of the link-layer
2947 * header; allocate one.
2949 off
->reg
= alloc_reg(cstate
);
2953 * Load the register containing the variable part of the
2954 * offset of the link-layer header into the X register.
2956 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2961 * That offset isn't variable, there's no variable part,
2962 * so we don't need to generate any code.
2969 * Map an Ethernet type to the equivalent PPP type.
2972 ethertype_to_ppptype(int proto
)
2980 case ETHERTYPE_IPV6
:
2988 case ETHERTYPE_ATALK
:
3002 * I'm assuming the "Bridging PDU"s that go
3003 * over PPP are Spanning Tree Protocol
3017 * Generate any tests that, for encapsulation of a link-layer packet
3018 * inside another protocol stack, need to be done to check for those
3019 * link-layer packets (and that haven't already been done by a check
3020 * for that encapsulation).
3022 static struct block
*
3023 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3027 if (cstate
->is_geneve
)
3028 return gen_geneve_ll_check(cstate
);
3030 switch (cstate
->prevlinktype
) {
3034 * This is LANE-encapsulated Ethernet; check that the LANE
3035 * packet doesn't begin with an LE Control marker, i.e.
3036 * that it's data, not a control message.
3038 * (We've already generated a test for LANE.)
3040 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3046 * No such tests are necessary.
3054 * The three different values we should check for when checking for an
3055 * IPv6 packet with DLT_NULL.
3057 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3058 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3059 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3062 * Generate code to match a particular packet type by matching the
3063 * link-layer type field or fields in the 802.2 LLC header.
3065 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3066 * value, if <= ETHERMTU.
3068 static struct block
*
3069 gen_linktype(compiler_state_t
*cstate
, int proto
)
3071 struct block
*b0
, *b1
, *b2
;
3072 const char *description
;
3074 /* are we checking MPLS-encapsulated packets? */
3075 if (cstate
->label_stack_depth
> 0) {
3079 /* FIXME add other L3 proto IDs */
3080 return gen_mpls_linktype(cstate
, Q_IP
);
3082 case ETHERTYPE_IPV6
:
3084 /* FIXME add other L3 proto IDs */
3085 return gen_mpls_linktype(cstate
, Q_IPV6
);
3088 bpf_error(cstate
, "unsupported protocol over mpls");
3093 switch (cstate
->linktype
) {
3096 case DLT_NETANALYZER
:
3097 case DLT_NETANALYZER_TRANSPARENT
:
3098 /* Geneve has an EtherType regardless of whether there is an
3100 if (!cstate
->is_geneve
)
3101 b0
= gen_prevlinkhdr_check(cstate
);
3105 b1
= gen_ether_linktype(cstate
, proto
);
3116 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3120 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3126 case DLT_IEEE802_11
:
3127 case DLT_PRISM_HEADER
:
3128 case DLT_IEEE802_11_RADIO_AVS
:
3129 case DLT_IEEE802_11_RADIO
:
3132 * Check that we have a data frame.
3134 b0
= gen_check_802_11_data_frame(cstate
);
3137 * Now check for the specified link-layer type.
3139 b1
= gen_llc_linktype(cstate
, proto
);
3147 * XXX - check for LLC frames.
3149 return gen_llc_linktype(cstate
, proto
);
3155 * XXX - check for LLC PDUs, as per IEEE 802.5.
3157 return gen_llc_linktype(cstate
, proto
);
3161 case DLT_ATM_RFC1483
:
3163 case DLT_IP_OVER_FC
:
3164 return gen_llc_linktype(cstate
, proto
);
3170 * Check for an LLC-encapsulated version of this protocol;
3171 * if we were checking for LANE, linktype would no longer
3174 * Check for LLC encapsulation and then check the protocol.
3176 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3177 b1
= gen_llc_linktype(cstate
, proto
);
3184 return gen_linux_sll_linktype(cstate
, proto
);
3189 case DLT_SLIP_BSDOS
:
3192 * These types don't provide any type field; packets
3193 * are always IPv4 or IPv6.
3195 * XXX - for IPv4, check for a version number of 4, and,
3196 * for IPv6, check for a version number of 6?
3201 /* Check for a version number of 4. */
3202 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3204 case ETHERTYPE_IPV6
:
3205 /* Check for a version number of 6. */
3206 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3209 return gen_false(cstate
); /* always false */
3216 * Raw IPv4, so no type field.
3218 if (proto
== ETHERTYPE_IP
)
3219 return gen_true(cstate
); /* always true */
3221 /* Checking for something other than IPv4; always false */
3222 return gen_false(cstate
);
3228 * Raw IPv6, so no type field.
3230 if (proto
== ETHERTYPE_IPV6
)
3231 return gen_true(cstate
); /* always true */
3233 /* Checking for something other than IPv6; always false */
3234 return gen_false(cstate
);
3240 case DLT_PPP_SERIAL
:
3243 * We use Ethernet protocol types inside libpcap;
3244 * map them to the corresponding PPP protocol types.
3246 proto
= ethertype_to_ppptype(proto
);
3247 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3253 * We use Ethernet protocol types inside libpcap;
3254 * map them to the corresponding PPP protocol types.
3260 * Also check for Van Jacobson-compressed IP.
3261 * XXX - do this for other forms of PPP?
3263 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3264 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3266 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3271 proto
= ethertype_to_ppptype(proto
);
3272 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3284 return (gen_loopback_linktype(cstate
, AF_INET
));
3286 case ETHERTYPE_IPV6
:
3288 * AF_ values may, unfortunately, be platform-
3289 * dependent; AF_INET isn't, because everybody
3290 * used 4.2BSD's value, but AF_INET6 is, because
3291 * 4.2BSD didn't have a value for it (given that
3292 * IPv6 didn't exist back in the early 1980's),
3293 * and they all picked their own values.
3295 * This means that, if we're reading from a
3296 * savefile, we need to check for all the
3299 * If we're doing a live capture, we only need
3300 * to check for this platform's value; however,
3301 * Npcap uses 24, which isn't Windows's AF_INET6
3302 * value. (Given the multiple different values,
3303 * programs that read pcap files shouldn't be
3304 * checking for their platform's AF_INET6 value
3305 * anyway, they should check for all of the
3306 * possible values. and they might as well do
3307 * that even for live captures.)
3309 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3311 * Savefile - check for all three
3312 * possible IPv6 values.
3314 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3315 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3317 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3322 * Live capture, so we only need to
3323 * check for the value used on this
3328 * Npcap doesn't use Windows's AF_INET6,
3329 * as that collides with AF_IPX on
3330 * some BSDs (both have the value 23).
3331 * Instead, it uses 24.
3333 return (gen_loopback_linktype(cstate
, 24));
3336 return (gen_loopback_linktype(cstate
, AF_INET6
));
3337 #else /* AF_INET6 */
3339 * I guess this platform doesn't support
3340 * IPv6, so we just reject all packets.
3342 return gen_false(cstate
);
3343 #endif /* AF_INET6 */
3349 * Not a type on which we support filtering.
3350 * XXX - support those that have AF_ values
3351 * #defined on this platform, at least?
3353 return gen_false(cstate
);
3356 #ifdef HAVE_NET_PFVAR_H
3359 * af field is host byte order in contrast to the rest of
3362 if (proto
== ETHERTYPE_IP
)
3363 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3364 BPF_B
, (bpf_int32
)AF_INET
));
3365 else if (proto
== ETHERTYPE_IPV6
)
3366 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3367 BPF_B
, (bpf_int32
)AF_INET6
));
3369 return gen_false(cstate
);
3372 #endif /* HAVE_NET_PFVAR_H */
3375 case DLT_ARCNET_LINUX
:
3377 * XXX should we check for first fragment if the protocol
3383 return gen_false(cstate
);
3385 case ETHERTYPE_IPV6
:
3386 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3387 (bpf_int32
)ARCTYPE_INET6
));
3390 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3391 (bpf_int32
)ARCTYPE_IP
);
3392 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3393 (bpf_int32
)ARCTYPE_IP_OLD
);
3398 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3399 (bpf_int32
)ARCTYPE_ARP
);
3400 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3401 (bpf_int32
)ARCTYPE_ARP_OLD
);
3405 case ETHERTYPE_REVARP
:
3406 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3407 (bpf_int32
)ARCTYPE_REVARP
));
3409 case ETHERTYPE_ATALK
:
3410 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3411 (bpf_int32
)ARCTYPE_ATALK
));
3418 case ETHERTYPE_ATALK
:
3419 return gen_true(cstate
);
3421 return gen_false(cstate
);
3428 * XXX - assumes a 2-byte Frame Relay header with
3429 * DLCI and flags. What if the address is longer?
3435 * Check for the special NLPID for IP.
3437 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3439 case ETHERTYPE_IPV6
:
3441 * Check for the special NLPID for IPv6.
3443 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3447 * Check for several OSI protocols.
3449 * Frame Relay packets typically have an OSI
3450 * NLPID at the beginning; we check for each
3453 * What we check for is the NLPID and a frame
3454 * control field of UI, i.e. 0x03 followed
3457 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3458 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3459 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3465 return gen_false(cstate
);
3471 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3473 case DLT_JUNIPER_MFR
:
3474 case DLT_JUNIPER_MLFR
:
3475 case DLT_JUNIPER_MLPPP
:
3476 case DLT_JUNIPER_ATM1
:
3477 case DLT_JUNIPER_ATM2
:
3478 case DLT_JUNIPER_PPPOE
:
3479 case DLT_JUNIPER_PPPOE_ATM
:
3480 case DLT_JUNIPER_GGSN
:
3481 case DLT_JUNIPER_ES
:
3482 case DLT_JUNIPER_MONITOR
:
3483 case DLT_JUNIPER_SERVICES
:
3484 case DLT_JUNIPER_ETHER
:
3485 case DLT_JUNIPER_PPP
:
3486 case DLT_JUNIPER_FRELAY
:
3487 case DLT_JUNIPER_CHDLC
:
3488 case DLT_JUNIPER_VP
:
3489 case DLT_JUNIPER_ST
:
3490 case DLT_JUNIPER_ISM
:
3491 case DLT_JUNIPER_VS
:
3492 case DLT_JUNIPER_SRX_E2E
:
3493 case DLT_JUNIPER_FIBRECHANNEL
:
3494 case DLT_JUNIPER_ATM_CEMIC
:
3496 /* just lets verify the magic number for now -
3497 * on ATM we may have up to 6 different encapsulations on the wire
3498 * and need a lot of heuristics to figure out that the payload
3501 * FIXME encapsulation specific BPF_ filters
3503 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3505 case DLT_BACNET_MS_TP
:
3506 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3509 return gen_ipnet_linktype(cstate
, proto
);
3511 case DLT_LINUX_IRDA
:
3512 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3515 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3518 case DLT_MTP2_WITH_PHDR
:
3519 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3522 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3525 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3527 case DLT_LINUX_LAPD
:
3528 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3530 case DLT_USB_FREEBSD
:
3532 case DLT_USB_LINUX_MMAPPED
:
3534 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3536 case DLT_BLUETOOTH_HCI_H4
:
3537 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3538 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3541 case DLT_CAN_SOCKETCAN
:
3542 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3544 case DLT_IEEE802_15_4
:
3545 case DLT_IEEE802_15_4_LINUX
:
3546 case DLT_IEEE802_15_4_NONASK_PHY
:
3547 case DLT_IEEE802_15_4_NOFCS
:
3548 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3550 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3551 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3554 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3557 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3560 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3563 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3566 /* Using the fixed-size NFLOG header it is possible to tell only
3567 * the address family of the packet, other meaningful data is
3568 * either missing or behind TLVs.
3570 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3574 * Does this link-layer header type have a field
3575 * indicating the type of the next protocol? If
3576 * so, off_linktype.constant_part will be the offset of that
3577 * field in the packet; if not, it will be OFFSET_NOT_SET.
3579 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3581 * Yes; assume it's an Ethernet type. (If
3582 * it's not, it needs to be handled specially
3585 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3588 * No; report an error.
3590 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3591 if (description
!= NULL
) {
3592 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3595 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3604 * Check for an LLC SNAP packet with a given organization code and
3605 * protocol type; we check the entire contents of the 802.2 LLC and
3606 * snap headers, checking for DSAP and SSAP of SNAP and a control
3607 * field of 0x03 in the LLC header, and for the specified organization
3608 * code and protocol type in the SNAP header.
3610 static struct block
*
3611 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3613 u_char snapblock
[8];
3615 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3616 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3617 snapblock
[2] = 0x03; /* control = UI */
3618 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3619 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3620 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3621 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3622 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3623 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3627 * Generate code to match frames with an LLC header.
3630 gen_llc(compiler_state_t
*cstate
)
3632 struct block
*b0
, *b1
;
3634 switch (cstate
->linktype
) {
3638 * We check for an Ethernet type field less than
3639 * 1500, which means it's an 802.3 length field.
3641 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3645 * Now check for the purported DSAP and SSAP not being
3646 * 0xFF, to rule out NetWare-over-802.3.
3648 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3655 * We check for LLC traffic.
3657 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3660 case DLT_IEEE802
: /* Token Ring */
3662 * XXX - check for LLC frames.
3664 return gen_true(cstate
);
3668 * XXX - check for LLC frames.
3670 return gen_true(cstate
);
3672 case DLT_ATM_RFC1483
:
3674 * For LLC encapsulation, these are defined to have an
3677 * For VC encapsulation, they don't, but there's no
3678 * way to check for that; the protocol used on the VC
3679 * is negotiated out of band.
3681 return gen_true(cstate
);
3683 case DLT_IEEE802_11
:
3684 case DLT_PRISM_HEADER
:
3685 case DLT_IEEE802_11_RADIO
:
3686 case DLT_IEEE802_11_RADIO_AVS
:
3689 * Check that we have a data frame.
3691 b0
= gen_check_802_11_data_frame(cstate
);
3695 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3701 gen_llc_i(compiler_state_t
*cstate
)
3703 struct block
*b0
, *b1
;
3707 * Check whether this is an LLC frame.
3709 b0
= gen_llc(cstate
);
3712 * Load the control byte and test the low-order bit; it must
3713 * be clear for I frames.
3715 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3716 b1
= new_block(cstate
, JMP(BPF_JSET
));
3725 gen_llc_s(compiler_state_t
*cstate
)
3727 struct block
*b0
, *b1
;
3730 * Check whether this is an LLC frame.
3732 b0
= gen_llc(cstate
);
3735 * Now compare the low-order 2 bit of the control byte against
3736 * the appropriate value for S frames.
3738 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3744 gen_llc_u(compiler_state_t
*cstate
)
3746 struct block
*b0
, *b1
;
3749 * Check whether this is an LLC frame.
3751 b0
= gen_llc(cstate
);
3754 * Now compare the low-order 2 bit of the control byte against
3755 * the appropriate value for U frames.
3757 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3763 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3765 struct block
*b0
, *b1
;
3768 * Check whether this is an LLC frame.
3770 b0
= gen_llc(cstate
);
3773 * Now check for an S frame with the appropriate type.
3775 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3781 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3783 struct block
*b0
, *b1
;
3786 * Check whether this is an LLC frame.
3788 b0
= gen_llc(cstate
);
3791 * Now check for a U frame with the appropriate type.
3793 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3799 * Generate code to match a particular packet type, for link-layer types
3800 * using 802.2 LLC headers.
3802 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3803 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3805 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3806 * value, if <= ETHERMTU. We use that to determine whether to
3807 * match the DSAP or both DSAP and LSAP or to check the OUI and
3808 * protocol ID in a SNAP header.
3810 static struct block
*
3811 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3814 * XXX - handle token-ring variable-length header.
3820 case LLCSAP_NETBEUI
:
3822 * XXX - should we check both the DSAP and the
3823 * SSAP, like this, or should we check just the
3824 * DSAP, as we do for other SAP values?
3826 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3827 ((proto
<< 8) | proto
));
3831 * XXX - are there ever SNAP frames for IPX on
3832 * non-Ethernet 802.x networks?
3834 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3835 (bpf_int32
)LLCSAP_IPX
);
3837 case ETHERTYPE_ATALK
:
3839 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3840 * SNAP packets with an organization code of
3841 * 0x080007 (Apple, for Appletalk) and a protocol
3842 * type of ETHERTYPE_ATALK (Appletalk).
3844 * XXX - check for an organization code of
3845 * encapsulated Ethernet as well?
3847 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3851 * XXX - we don't have to check for IPX 802.3
3852 * here, but should we check for the IPX Ethertype?
3854 if (proto
<= ETHERMTU
) {
3856 * This is an LLC SAP value, so check
3859 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3862 * This is an Ethernet type; we assume that it's
3863 * unlikely that it'll appear in the right place
3864 * at random, and therefore check only the
3865 * location that would hold the Ethernet type
3866 * in a SNAP frame with an organization code of
3867 * 0x000000 (encapsulated Ethernet).
3869 * XXX - if we were to check for the SNAP DSAP and
3870 * LSAP, as per XXX, and were also to check for an
3871 * organization code of 0x000000 (encapsulated
3872 * Ethernet), we'd do
3874 * return gen_snap(cstate, 0x000000, proto);
3876 * here; for now, we don't, as per the above.
3877 * I don't know whether it's worth the extra CPU
3878 * time to do the right check or not.
3880 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3885 static struct block
*
3886 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3887 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3889 struct block
*b0
, *b1
;
3903 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3904 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3910 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3911 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3916 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3920 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3924 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3928 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3932 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3936 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3942 b0
= gen_linktype(cstate
, proto
);
3943 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3949 static struct block
*
3950 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3951 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3953 struct block
*b0
, *b1
;
3968 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3969 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3975 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3976 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3981 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3985 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3989 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3993 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3997 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4001 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4007 /* this order is important */
4008 a
= (uint32_t *)addr
;
4009 m
= (uint32_t *)mask
;
4010 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4011 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4013 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4015 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4017 b0
= gen_linktype(cstate
, proto
);
4023 static struct block
*
4024 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4026 register struct block
*b0
, *b1
;
4030 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4033 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4036 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4037 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4043 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4044 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4049 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4053 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4057 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4061 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4065 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4069 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4077 * Like gen_ehostop, but for DLT_FDDI
4079 static struct block
*
4080 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4082 struct block
*b0
, *b1
;
4086 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4089 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4092 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4093 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4099 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4100 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4105 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4109 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4113 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4117 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4121 bpf_error(cstate
, "'ra' is only supported on 802.11");
4125 bpf_error(cstate
, "'ta' is only supported on 802.11");
4133 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4135 static struct block
*
4136 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4138 register struct block
*b0
, *b1
;
4142 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4145 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4148 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4149 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4155 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4156 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4161 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4165 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4169 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4173 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4177 bpf_error(cstate
, "'ra' is only supported on 802.11");
4181 bpf_error(cstate
, "'ta' is only supported on 802.11");
4189 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4190 * various 802.11 + radio headers.
4192 static struct block
*
4193 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4195 register struct block
*b0
, *b1
, *b2
;
4196 register struct slist
*s
;
4198 #ifdef ENABLE_WLAN_FILTERING_PATCH
4201 * We need to disable the optimizer because the optimizer is buggy
4202 * and wipes out some LD instructions generated by the below
4203 * code to validate the Frame Control bits
4205 cstate
->no_optimize
= 1;
4206 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4213 * For control frames, there is no SA.
4215 * For management frames, SA is at an
4216 * offset of 10 from the beginning of
4219 * For data frames, SA is at an offset
4220 * of 10 from the beginning of the packet
4221 * if From DS is clear, at an offset of
4222 * 16 from the beginning of the packet
4223 * if From DS is set and To DS is clear,
4224 * and an offset of 24 from the beginning
4225 * of the packet if From DS is set and To DS
4230 * Generate the tests to be done for data frames
4233 * First, check for To DS set, i.e. check "link[1] & 0x01".
4235 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4236 b1
= new_block(cstate
, JMP(BPF_JSET
));
4237 b1
->s
.k
= 0x01; /* To DS */
4241 * If To DS is set, the SA is at 24.
4243 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4247 * Now, check for To DS not set, i.e. check
4248 * "!(link[1] & 0x01)".
4250 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4251 b2
= new_block(cstate
, JMP(BPF_JSET
));
4252 b2
->s
.k
= 0x01; /* To DS */
4257 * If To DS is not set, the SA is at 16.
4259 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4263 * Now OR together the last two checks. That gives
4264 * the complete set of checks for data frames with
4270 * Now check for From DS being set, and AND that with
4271 * the ORed-together checks.
4273 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4274 b1
= new_block(cstate
, JMP(BPF_JSET
));
4275 b1
->s
.k
= 0x02; /* From DS */
4280 * Now check for data frames with From DS not set.
4282 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4283 b2
= new_block(cstate
, JMP(BPF_JSET
));
4284 b2
->s
.k
= 0x02; /* From DS */
4289 * If From DS isn't set, the SA is at 10.
4291 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4295 * Now OR together the checks for data frames with
4296 * From DS not set and for data frames with From DS
4297 * set; that gives the checks done for data frames.
4302 * Now check for a data frame.
4303 * I.e, check "link[0] & 0x08".
4305 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4306 b1
= new_block(cstate
, JMP(BPF_JSET
));
4311 * AND that with the checks done for data frames.
4316 * If the high-order bit of the type value is 0, this
4317 * is a management frame.
4318 * I.e, check "!(link[0] & 0x08)".
4320 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4321 b2
= new_block(cstate
, JMP(BPF_JSET
));
4327 * For management frames, the SA is at 10.
4329 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4333 * OR that with the checks done for data frames.
4334 * That gives the checks done for management and
4340 * If the low-order bit of the type value is 1,
4341 * this is either a control frame or a frame
4342 * with a reserved type, and thus not a
4345 * I.e., check "!(link[0] & 0x04)".
4347 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4348 b1
= new_block(cstate
, JMP(BPF_JSET
));
4354 * AND that with the checks for data and management
4364 * For control frames, there is no DA.
4366 * For management frames, DA is at an
4367 * offset of 4 from the beginning of
4370 * For data frames, DA is at an offset
4371 * of 4 from the beginning of the packet
4372 * if To DS is clear and at an offset of
4373 * 16 from the beginning of the packet
4378 * Generate the tests to be done for data frames.
4380 * First, check for To DS set, i.e. "link[1] & 0x01".
4382 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4383 b1
= new_block(cstate
, JMP(BPF_JSET
));
4384 b1
->s
.k
= 0x01; /* To DS */
4388 * If To DS is set, the DA is at 16.
4390 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4394 * Now, check for To DS not set, i.e. check
4395 * "!(link[1] & 0x01)".
4397 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4398 b2
= new_block(cstate
, JMP(BPF_JSET
));
4399 b2
->s
.k
= 0x01; /* To DS */
4404 * If To DS is not set, the DA is at 4.
4406 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4410 * Now OR together the last two checks. That gives
4411 * the complete set of checks for data frames.
4416 * Now check for a data frame.
4417 * I.e, check "link[0] & 0x08".
4419 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4420 b1
= new_block(cstate
, JMP(BPF_JSET
));
4425 * AND that with the checks done for data frames.
4430 * If the high-order bit of the type value is 0, this
4431 * is a management frame.
4432 * I.e, check "!(link[0] & 0x08)".
4434 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4435 b2
= new_block(cstate
, JMP(BPF_JSET
));
4441 * For management frames, the DA is at 4.
4443 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4447 * OR that with the checks done for data frames.
4448 * That gives the checks done for management and
4454 * If the low-order bit of the type value is 1,
4455 * this is either a control frame or a frame
4456 * with a reserved type, and thus not a
4459 * I.e., check "!(link[0] & 0x04)".
4461 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4462 b1
= new_block(cstate
, JMP(BPF_JSET
));
4468 * AND that with the checks for data and management
4475 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4476 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4482 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4483 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4488 * XXX - add BSSID keyword?
4491 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4495 * Not present in CTS or ACK control frames.
4497 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4498 IEEE80211_FC0_TYPE_MASK
);
4500 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4501 IEEE80211_FC0_SUBTYPE_MASK
);
4503 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4504 IEEE80211_FC0_SUBTYPE_MASK
);
4508 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4514 * Not present in control frames.
4516 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4517 IEEE80211_FC0_TYPE_MASK
);
4519 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4525 * Present only if the direction mask has both "From DS"
4526 * and "To DS" set. Neither control frames nor management
4527 * frames should have both of those set, so we don't
4528 * check the frame type.
4530 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4531 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4532 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4538 * Not present in management frames; addr1 in other
4543 * If the high-order bit of the type value is 0, this
4544 * is a management frame.
4545 * I.e, check "(link[0] & 0x08)".
4547 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4548 b1
= new_block(cstate
, JMP(BPF_JSET
));
4555 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4558 * AND that with the check of addr1.
4565 * Not present in management frames; addr2, if present,
4570 * Not present in CTS or ACK control frames.
4572 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4573 IEEE80211_FC0_TYPE_MASK
);
4575 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4576 IEEE80211_FC0_SUBTYPE_MASK
);
4578 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4579 IEEE80211_FC0_SUBTYPE_MASK
);
4585 * If the high-order bit of the type value is 0, this
4586 * is a management frame.
4587 * I.e, check "(link[0] & 0x08)".
4589 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4590 b1
= new_block(cstate
, JMP(BPF_JSET
));
4595 * AND that with the check for frames other than
4596 * CTS and ACK frames.
4603 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4612 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4613 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4614 * as the RFC states.)
4616 static struct block
*
4617 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4619 register struct block
*b0
, *b1
;
4623 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4626 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4629 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4630 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4636 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4637 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4642 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4646 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4650 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4654 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4658 bpf_error(cstate
, "'ra' is only supported on 802.11");
4662 bpf_error(cstate
, "'ta' is only supported on 802.11");
4670 * This is quite tricky because there may be pad bytes in front of the
4671 * DECNET header, and then there are two possible data packet formats that
4672 * carry both src and dst addresses, plus 5 packet types in a format that
4673 * carries only the src node, plus 2 types that use a different format and
4674 * also carry just the src node.
4678 * Instead of doing those all right, we just look for data packets with
4679 * 0 or 1 bytes of padding. If you want to look at other packets, that
4680 * will require a lot more hacking.
4682 * To add support for filtering on DECNET "areas" (network numbers)
4683 * one would want to add a "mask" argument to this routine. That would
4684 * make the filter even more inefficient, although one could be clever
4685 * and not generate masking instructions if the mask is 0xFFFF.
4687 static struct block
*
4688 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4690 struct block
*b0
, *b1
, *b2
, *tmp
;
4691 u_int offset_lh
; /* offset if long header is received */
4692 u_int offset_sh
; /* offset if short header is received */
4697 offset_sh
= 1; /* follows flags */
4698 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4702 offset_sh
= 3; /* follows flags, dstnode */
4703 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4707 /* Inefficient because we do our Calvinball dance twice */
4708 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4709 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4715 /* Inefficient because we do our Calvinball dance twice */
4716 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4717 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4722 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4726 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4730 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4734 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4738 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4742 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4748 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4749 /* Check for pad = 1, long header case */
4750 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4751 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4752 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4753 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4755 /* Check for pad = 0, long header case */
4756 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4757 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4760 /* Check for pad = 1, short header case */
4761 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4762 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4763 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4766 /* Check for pad = 0, short header case */
4767 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4768 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4772 /* Combine with test for cstate->linktype */
4778 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4779 * test the bottom-of-stack bit, and then check the version number
4780 * field in the IP header.
4782 static struct block
*
4783 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4785 struct block
*b0
, *b1
;
4790 /* match the bottom-of-stack bit */
4791 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4792 /* match the IPv4 version number */
4793 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4798 /* match the bottom-of-stack bit */
4799 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4800 /* match the IPv4 version number */
4801 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4810 static struct block
*
4811 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4812 int proto
, int dir
, int type
)
4814 struct block
*b0
, *b1
;
4815 const char *typestr
;
4825 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4827 * Only check for non-IPv4 addresses if we're not
4828 * checking MPLS-encapsulated packets.
4830 if (cstate
->label_stack_depth
== 0) {
4831 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4833 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4839 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4842 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4845 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4848 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4851 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4854 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4857 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4860 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4863 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4866 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4869 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4872 return gen_dnhostop(cstate
, addr
, dir
);
4875 bpf_error(cstate
, "LAT host filtering not implemented");
4878 bpf_error(cstate
, "SCA host filtering not implemented");
4881 bpf_error(cstate
, "MOPRC host filtering not implemented");
4884 bpf_error(cstate
, "MOPDL host filtering not implemented");
4887 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4890 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4893 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4896 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4899 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4902 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4905 bpf_error(cstate
, "AARP host filtering not implemented");
4908 bpf_error(cstate
, "ISO host filtering not implemented");
4911 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4914 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4917 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4920 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4923 bpf_error(cstate
, "IPX host filtering not implemented");
4926 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4929 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
4932 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
4935 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
4938 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
4941 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
4944 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
4947 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
4950 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4953 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4962 static struct block
*
4963 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4964 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4966 const char *typestr
;
4976 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4979 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4982 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4985 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4988 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4991 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
4994 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
4997 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5000 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5003 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5006 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5009 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5012 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5015 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5018 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5021 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5024 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5027 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5030 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5033 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5036 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5039 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5042 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5045 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5048 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5051 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5054 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5057 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5060 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5063 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5066 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5069 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5072 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5075 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5078 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5081 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5084 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5087 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5090 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5093 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5103 static struct block
*
5104 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5105 struct addrinfo
*alist
, int proto
, int dir
)
5107 struct block
*b0
, *b1
, *tmp
;
5108 struct addrinfo
*ai
;
5109 struct sockaddr_in
*sin
;
5112 bpf_error(cstate
, "direction applied to 'gateway'");
5119 switch (cstate
->linktype
) {
5121 case DLT_NETANALYZER
:
5122 case DLT_NETANALYZER_TRANSPARENT
:
5123 b1
= gen_prevlinkhdr_check(cstate
);
5124 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5129 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5132 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5134 case DLT_IEEE802_11
:
5135 case DLT_PRISM_HEADER
:
5136 case DLT_IEEE802_11_RADIO_AVS
:
5137 case DLT_IEEE802_11_RADIO
:
5139 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5143 * This is LLC-multiplexed traffic; if it were
5144 * LANE, cstate->linktype would have been set to
5148 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5150 case DLT_IP_OVER_FC
:
5151 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5155 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5158 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5160 * Does it have an address?
5162 if (ai
->ai_addr
!= NULL
) {
5164 * Yes. Is it an IPv4 address?
5166 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5168 * Generate an entry for it.
5170 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5171 tmp
= gen_host(cstate
,
5172 ntohl(sin
->sin_addr
.s_addr
),
5173 0xffffffff, proto
, Q_OR
, Q_HOST
);
5175 * Is it the *first* IPv4 address?
5179 * Yes, so start with it.
5184 * No, so OR it into the
5196 * No IPv4 addresses found.
5204 bpf_error(cstate
, "illegal modifier of 'gateway'");
5210 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5218 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5219 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5224 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5225 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5230 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5231 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5236 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5239 #ifndef IPPROTO_IGMP
5240 #define IPPROTO_IGMP 2
5244 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5247 #ifndef IPPROTO_IGRP
5248 #define IPPROTO_IGRP 9
5251 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5255 #define IPPROTO_PIM 103
5259 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5260 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5264 #ifndef IPPROTO_VRRP
5265 #define IPPROTO_VRRP 112
5269 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5272 #ifndef IPPROTO_CARP
5273 #define IPPROTO_CARP 112
5277 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5281 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5285 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5289 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5293 bpf_error(cstate
, "link layer applied in wrong context");
5296 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5300 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5304 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5308 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5312 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5316 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5320 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5324 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5327 #ifndef IPPROTO_ICMPV6
5328 #define IPPROTO_ICMPV6 58
5331 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5335 #define IPPROTO_AH 51
5338 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5339 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5344 #define IPPROTO_ESP 50
5347 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5348 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5353 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5357 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5361 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5364 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5365 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5366 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5368 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5370 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5372 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5376 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5377 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5378 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5380 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5382 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5384 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5388 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5389 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5390 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5392 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5397 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5398 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5403 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5404 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5406 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5408 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5413 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5414 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5419 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5420 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5425 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5429 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5433 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5437 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5441 bpf_error(cstate
, "'radio' is not a valid protocol type");
5449 static struct block
*
5450 gen_ipfrag(compiler_state_t
*cstate
)
5455 /* not IPv4 frag other than the first frag */
5456 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5457 b
= new_block(cstate
, JMP(BPF_JSET
));
5466 * Generate a comparison to a port value in the transport-layer header
5467 * at the specified offset from the beginning of that header.
5469 * XXX - this handles a variable-length prefix preceding the link-layer
5470 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5471 * variable-length link-layer headers (such as Token Ring or 802.11
5474 static struct block
*
5475 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5477 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5480 static struct block
*
5481 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5483 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5487 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5489 struct block
*b0
, *b1
, *tmp
;
5491 /* ip proto 'proto' and not a fragment other than the first fragment */
5492 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5493 b0
= gen_ipfrag(cstate
);
5498 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5502 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5506 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5507 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5513 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5514 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5519 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5523 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5527 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5531 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5535 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5539 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5550 static struct block
*
5551 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5553 struct block
*b0
, *b1
, *tmp
;
5558 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5559 * not LLC encapsulation with LLCSAP_IP.
5561 * For IEEE 802 networks - which includes 802.5 token ring
5562 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5563 * says that SNAP encapsulation is used, not LLC encapsulation
5566 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5567 * RFC 2225 say that SNAP encapsulation is used, not LLC
5568 * encapsulation with LLCSAP_IP.
5570 * So we always check for ETHERTYPE_IP.
5572 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5578 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5582 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5583 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5585 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5597 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5599 struct block
*b0
, *b1
, *tmp
;
5601 /* ip6 proto 'proto' */
5602 /* XXX - catch the first fragment of a fragmented packet? */
5603 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5607 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5611 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5615 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5616 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5622 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5623 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5635 static struct block
*
5636 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5638 struct block
*b0
, *b1
, *tmp
;
5640 /* link proto ip6 */
5641 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5647 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5651 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5652 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5654 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5665 /* gen_portrange code */
5666 static struct block
*
5667 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5670 struct block
*b1
, *b2
;
5674 * Reverse the order of the ports, so v1 is the lower one.
5683 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5684 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5692 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5695 struct block
*b0
, *b1
, *tmp
;
5697 /* ip proto 'proto' and not a fragment other than the first fragment */
5698 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5699 b0
= gen_ipfrag(cstate
);
5704 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5708 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5712 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5713 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5719 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5720 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5725 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5729 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5733 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5737 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5741 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5745 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5756 static struct block
*
5757 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5760 struct block
*b0
, *b1
, *tmp
;
5763 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5769 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5773 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5774 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5776 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5787 static struct block
*
5788 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5791 struct block
*b1
, *b2
;
5795 * Reverse the order of the ports, so v1 is the lower one.
5804 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5805 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5813 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5816 struct block
*b0
, *b1
, *tmp
;
5818 /* ip6 proto 'proto' */
5819 /* XXX - catch the first fragment of a fragmented packet? */
5820 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5824 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5828 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5832 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5833 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5839 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5840 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5852 static struct block
*
5853 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5856 struct block
*b0
, *b1
, *tmp
;
5858 /* link proto ip6 */
5859 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5865 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5869 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5870 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5872 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5884 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5893 v
= pcap_nametoproto(name
);
5894 if (v
== PROTO_UNDEF
)
5895 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5899 /* XXX should look up h/w protocol type based on cstate->linktype */
5900 v
= pcap_nametoeproto(name
);
5901 if (v
== PROTO_UNDEF
) {
5902 v
= pcap_nametollc(name
);
5903 if (v
== PROTO_UNDEF
)
5904 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5909 if (strcmp(name
, "esis") == 0)
5911 else if (strcmp(name
, "isis") == 0)
5913 else if (strcmp(name
, "clnp") == 0)
5916 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5928 gen_joinsp(struct stmt
**s
, int n
)
5934 static struct block
*
5935 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5937 #ifdef NO_PROTOCHAIN
5938 return gen_proto(cstate
, v
, proto
, dir
);
5940 struct block
*b0
, *b
;
5941 struct slist
*s
[100];
5942 int fix2
, fix3
, fix4
, fix5
;
5943 int ahcheck
, again
, end
;
5945 int reg2
= alloc_reg(cstate
);
5947 memset(s
, 0, sizeof(s
));
5948 fix3
= fix4
= fix5
= 0;
5955 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5956 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5960 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5965 * We don't handle variable-length prefixes before the link-layer
5966 * header, or variable-length link-layer headers, here yet.
5967 * We might want to add BPF instructions to do the protochain
5968 * work, to simplify that and, on platforms that have a BPF
5969 * interpreter with the new instructions, let the filtering
5970 * be done in the kernel. (We already require a modified BPF
5971 * engine to do the protochain stuff, to support backward
5972 * branches, and backward branch support is unlikely to appear
5973 * in kernel BPF engines.)
5975 if (cstate
->off_linkpl
.is_variable
)
5976 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5978 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
5981 * s[0] is a dummy entry to protect other BPF insn from damage
5982 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5983 * hard to find interdependency made by jump table fixup.
5986 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5991 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5994 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5995 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5997 /* X = ip->ip_hl << 2 */
5998 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5999 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6004 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6006 /* A = ip6->ip_nxt */
6007 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6008 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6010 /* X = sizeof(struct ip6_hdr) */
6011 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6017 bpf_error(cstate
, "unsupported proto to gen_protochain");
6021 /* again: if (A == v) goto end; else fall through; */
6023 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6025 s
[i
]->s
.jt
= NULL
; /*later*/
6026 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6030 #ifndef IPPROTO_NONE
6031 #define IPPROTO_NONE 59
6033 /* if (A == IPPROTO_NONE) goto end */
6034 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6035 s
[i
]->s
.jt
= NULL
; /*later*/
6036 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6037 s
[i
]->s
.k
= IPPROTO_NONE
;
6038 s
[fix5
]->s
.jf
= s
[i
];
6042 if (proto
== Q_IPV6
) {
6043 int v6start
, v6end
, v6advance
, j
;
6046 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6047 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6048 s
[i
]->s
.jt
= NULL
; /*later*/
6049 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6050 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6051 s
[fix2
]->s
.jf
= s
[i
];
6053 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6054 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6055 s
[i
]->s
.jt
= NULL
; /*later*/
6056 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6057 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6059 /* if (A == IPPROTO_ROUTING) goto v6advance */
6060 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6061 s
[i
]->s
.jt
= NULL
; /*later*/
6062 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6063 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6065 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6066 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6067 s
[i
]->s
.jt
= NULL
; /*later*/
6068 s
[i
]->s
.jf
= NULL
; /*later*/
6069 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6079 * A = P[X + packet head];
6080 * X = X + (P[X + packet head + 1] + 1) * 8;
6082 /* A = P[X + packet head] */
6083 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6084 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6087 s
[i
] = new_stmt(cstate
, BPF_ST
);
6090 /* A = P[X + packet head + 1]; */
6091 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6092 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6095 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6099 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6103 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6107 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6110 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6114 /* goto again; (must use BPF_JA for backward jump) */
6115 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6116 s
[i
]->s
.k
= again
- i
- 1;
6117 s
[i
- 1]->s
.jf
= s
[i
];
6121 for (j
= v6start
; j
<= v6end
; j
++)
6122 s
[j
]->s
.jt
= s
[v6advance
];
6125 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6127 s
[fix2
]->s
.jf
= s
[i
];
6133 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6134 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6135 s
[i
]->s
.jt
= NULL
; /*later*/
6136 s
[i
]->s
.jf
= NULL
; /*later*/
6137 s
[i
]->s
.k
= IPPROTO_AH
;
6139 s
[fix3
]->s
.jf
= s
[ahcheck
];
6146 * X = X + (P[X + 1] + 2) * 4;
6149 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6151 /* A = P[X + packet head]; */
6152 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6153 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6156 s
[i
] = new_stmt(cstate
, BPF_ST
);
6160 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6163 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6167 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6169 /* A = P[X + packet head] */
6170 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6171 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6174 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6178 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6182 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6185 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6189 /* goto again; (must use BPF_JA for backward jump) */
6190 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6191 s
[i
]->s
.k
= again
- i
- 1;
6196 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6198 s
[fix2
]->s
.jt
= s
[end
];
6199 s
[fix4
]->s
.jf
= s
[end
];
6200 s
[fix5
]->s
.jt
= s
[end
];
6207 for (i
= 0; i
< max
- 1; i
++)
6208 s
[i
]->next
= s
[i
+ 1];
6209 s
[max
- 1]->next
= NULL
;
6214 b
= new_block(cstate
, JMP(BPF_JEQ
));
6215 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6218 free_reg(cstate
, reg2
);
6225 static struct block
*
6226 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6229 struct block
*b0
, *b1
;
6232 * A data frame has the 0x08 bit (b3) in the frame control field set
6233 * and the 0x04 bit (b2) clear.
6235 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6236 b0
= new_block(cstate
, JMP(BPF_JSET
));
6240 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6241 b1
= new_block(cstate
, JMP(BPF_JSET
));
6252 * Generate code that checks whether the packet is a packet for protocol
6253 * <proto> and whether the type field in that protocol's header has
6254 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6255 * IP packet and checks the protocol number in the IP header against <v>.
6257 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6258 * against Q_IP and Q_IPV6.
6260 static struct block
*
6261 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6263 struct block
*b0
, *b1
;
6268 if (dir
!= Q_DEFAULT
)
6269 bpf_error(cstate
, "direction applied to 'proto'");
6273 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6274 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6280 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6281 * not LLC encapsulation with LLCSAP_IP.
6283 * For IEEE 802 networks - which includes 802.5 token ring
6284 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6285 * says that SNAP encapsulation is used, not LLC encapsulation
6288 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6289 * RFC 2225 say that SNAP encapsulation is used, not LLC
6290 * encapsulation with LLCSAP_IP.
6292 * So we always check for ETHERTYPE_IP.
6294 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6296 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6298 b1
= gen_protochain(cstate
, v
, Q_IP
);
6304 switch (cstate
->linktype
) {
6308 * Frame Relay packets typically have an OSI
6309 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6310 * generates code to check for all the OSI
6311 * NLPIDs, so calling it and then adding a check
6312 * for the particular NLPID for which we're
6313 * looking is bogus, as we can just check for
6316 * What we check for is the NLPID and a frame
6317 * control field value of UI, i.e. 0x03 followed
6320 * XXX - assumes a 2-byte Frame Relay header with
6321 * DLCI and flags. What if the address is longer?
6323 * XXX - what about SNAP-encapsulated frames?
6325 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6331 * Cisco uses an Ethertype lookalike - for OSI,
6334 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6335 /* OSI in C-HDLC is stuffed with a fudge byte */
6336 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (bpf_int32
)v
);
6341 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6342 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (bpf_int32
)v
);
6348 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6350 * 4 is the offset of the PDU type relative to the IS-IS
6353 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (bpf_int32
)v
);
6358 bpf_error(cstate
, "arp does not encapsulate another protocol");
6362 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6366 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6370 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6374 bpf_error(cstate
, "sca does not encapsulate another protocol");
6378 bpf_error(cstate
, "lat does not encapsulate another protocol");
6382 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6386 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6390 return gen_linktype(cstate
, v
);
6393 bpf_error(cstate
, "'udp proto' is bogus");
6397 bpf_error(cstate
, "'tcp proto' is bogus");
6401 bpf_error(cstate
, "'sctp proto' is bogus");
6405 bpf_error(cstate
, "'icmp proto' is bogus");
6409 bpf_error(cstate
, "'igmp proto' is bogus");
6413 bpf_error(cstate
, "'igrp proto' is bogus");
6417 bpf_error(cstate
, "'pim proto' is bogus");
6421 bpf_error(cstate
, "'vrrp proto' is bogus");
6425 bpf_error(cstate
, "'carp proto' is bogus");
6429 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6432 * Also check for a fragment header before the final
6435 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6436 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6438 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6441 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6447 bpf_error(cstate
, "'icmp6 proto' is bogus");
6450 bpf_error(cstate
, "'ah proto' is bogus");
6453 bpf_error(cstate
, "'ah proto' is bogus");
6456 bpf_error(cstate
, "'stp proto' is bogus");
6459 bpf_error(cstate
, "'ipx proto' is bogus");
6462 bpf_error(cstate
, "'netbeui proto' is bogus");
6465 bpf_error(cstate
, "'radio proto' is bogus");
6475 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6477 int proto
= q
.proto
;
6481 bpf_u_int32 mask
, addr
;
6482 struct addrinfo
*res
, *res0
;
6483 struct sockaddr_in
*sin4
;
6486 struct sockaddr_in6
*sin6
;
6487 struct in6_addr mask128
;
6489 struct block
*b
, *tmp
;
6490 int port
, real_proto
;
6496 addr
= pcap_nametonetaddr(name
);
6498 bpf_error(cstate
, "unknown network '%s'", name
);
6499 /* Left justify network addr and calculate its network mask */
6501 while (addr
&& (addr
& 0xff000000) == 0) {
6505 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6509 if (proto
== Q_LINK
) {
6510 switch (cstate
->linktype
) {
6513 case DLT_NETANALYZER
:
6514 case DLT_NETANALYZER_TRANSPARENT
:
6515 eaddr
= pcap_ether_hostton(name
);
6518 "unknown ether host '%s'", name
);
6519 tmp
= gen_prevlinkhdr_check(cstate
);
6520 b
= gen_ehostop(cstate
, eaddr
, dir
);
6527 eaddr
= pcap_ether_hostton(name
);
6530 "unknown FDDI host '%s'", name
);
6531 b
= gen_fhostop(cstate
, eaddr
, dir
);
6536 eaddr
= pcap_ether_hostton(name
);
6539 "unknown token ring host '%s'", name
);
6540 b
= gen_thostop(cstate
, eaddr
, dir
);
6544 case DLT_IEEE802_11
:
6545 case DLT_PRISM_HEADER
:
6546 case DLT_IEEE802_11_RADIO_AVS
:
6547 case DLT_IEEE802_11_RADIO
:
6549 eaddr
= pcap_ether_hostton(name
);
6552 "unknown 802.11 host '%s'", name
);
6553 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6557 case DLT_IP_OVER_FC
:
6558 eaddr
= pcap_ether_hostton(name
);
6561 "unknown Fibre Channel host '%s'", name
);
6562 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6567 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6568 } else if (proto
== Q_DECNET
) {
6569 unsigned short dn_addr
;
6571 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6573 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6575 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6580 * I don't think DECNET hosts can be multihomed, so
6581 * there is no need to build up a list of addresses
6583 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6586 memset(&mask128
, 0xff, sizeof(mask128
));
6588 res0
= res
= pcap_nametoaddrinfo(name
);
6590 bpf_error(cstate
, "unknown host '%s'", name
);
6597 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6598 tproto
== Q_DEFAULT
) {
6604 for (res
= res0
; res
; res
= res
->ai_next
) {
6605 switch (res
->ai_family
) {
6608 if (tproto
== Q_IPV6
)
6612 sin4
= (struct sockaddr_in
*)
6614 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6615 0xffffffff, tproto
, dir
, q
.addr
);
6619 if (tproto6
== Q_IP
)
6622 sin6
= (struct sockaddr_in6
*)
6624 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6625 &mask128
, tproto6
, dir
, q
.addr
);
6638 bpf_error(cstate
, "unknown host '%s'%s", name
,
6639 (proto
== Q_DEFAULT
)
6641 : " for specified address family");
6647 if (proto
!= Q_DEFAULT
&&
6648 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6649 bpf_error(cstate
, "illegal qualifier of 'port'");
6650 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6651 bpf_error(cstate
, "unknown port '%s'", name
);
6652 if (proto
== Q_UDP
) {
6653 if (real_proto
== IPPROTO_TCP
)
6654 bpf_error(cstate
, "port '%s' is tcp", name
);
6655 else if (real_proto
== IPPROTO_SCTP
)
6656 bpf_error(cstate
, "port '%s' is sctp", name
);
6658 /* override PROTO_UNDEF */
6659 real_proto
= IPPROTO_UDP
;
6661 if (proto
== Q_TCP
) {
6662 if (real_proto
== IPPROTO_UDP
)
6663 bpf_error(cstate
, "port '%s' is udp", name
);
6665 else if (real_proto
== IPPROTO_SCTP
)
6666 bpf_error(cstate
, "port '%s' is sctp", name
);
6668 /* override PROTO_UNDEF */
6669 real_proto
= IPPROTO_TCP
;
6671 if (proto
== Q_SCTP
) {
6672 if (real_proto
== IPPROTO_UDP
)
6673 bpf_error(cstate
, "port '%s' is udp", name
);
6675 else if (real_proto
== IPPROTO_TCP
)
6676 bpf_error(cstate
, "port '%s' is tcp", name
);
6678 /* override PROTO_UNDEF */
6679 real_proto
= IPPROTO_SCTP
;
6682 bpf_error(cstate
, "illegal port number %d < 0", port
);
6684 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6685 b
= gen_port(cstate
, port
, real_proto
, dir
);
6686 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6690 if (proto
!= Q_DEFAULT
&&
6691 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6692 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6693 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6694 bpf_error(cstate
, "unknown port in range '%s'", name
);
6695 if (proto
== Q_UDP
) {
6696 if (real_proto
== IPPROTO_TCP
)
6697 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6698 else if (real_proto
== IPPROTO_SCTP
)
6699 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6701 /* override PROTO_UNDEF */
6702 real_proto
= IPPROTO_UDP
;
6704 if (proto
== Q_TCP
) {
6705 if (real_proto
== IPPROTO_UDP
)
6706 bpf_error(cstate
, "port in range '%s' is udp", name
);
6707 else if (real_proto
== IPPROTO_SCTP
)
6708 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6710 /* override PROTO_UNDEF */
6711 real_proto
= IPPROTO_TCP
;
6713 if (proto
== Q_SCTP
) {
6714 if (real_proto
== IPPROTO_UDP
)
6715 bpf_error(cstate
, "port in range '%s' is udp", name
);
6716 else if (real_proto
== IPPROTO_TCP
)
6717 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6719 /* override PROTO_UNDEF */
6720 real_proto
= IPPROTO_SCTP
;
6723 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6725 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6727 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6729 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6731 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6732 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6737 eaddr
= pcap_ether_hostton(name
);
6739 bpf_error(cstate
, "unknown ether host: %s", name
);
6741 res
= pcap_nametoaddrinfo(name
);
6744 bpf_error(cstate
, "unknown host '%s'", name
);
6745 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6749 bpf_error(cstate
, "unknown host '%s'", name
);
6752 bpf_error(cstate
, "'gateway' not supported in this configuration");
6756 real_proto
= lookup_proto(cstate
, name
, proto
);
6757 if (real_proto
>= 0)
6758 return gen_proto(cstate
, real_proto
, proto
, dir
);
6760 bpf_error(cstate
, "unknown protocol: %s", name
);
6763 real_proto
= lookup_proto(cstate
, name
, proto
);
6764 if (real_proto
>= 0)
6765 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6767 bpf_error(cstate
, "unknown protocol: %s", name
);
6778 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6779 unsigned int masklen
, struct qual q
)
6781 register int nlen
, mlen
;
6784 nlen
= __pcap_atoin(s1
, &n
);
6785 /* Promote short ipaddr */
6789 mlen
= __pcap_atoin(s2
, &m
);
6790 /* Promote short ipaddr */
6793 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6796 /* Convert mask len to mask */
6798 bpf_error(cstate
, "mask length must be <= 32");
6801 * X << 32 is not guaranteed by C to be 0; it's
6806 m
= 0xffffffff << (32 - masklen
);
6808 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6815 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6818 bpf_error(cstate
, "Mask syntax for networks only");
6825 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6828 int proto
= q
.proto
;
6834 else if (q
.proto
== Q_DECNET
) {
6835 vlen
= __pcap_atodn(s
, &v
);
6837 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6839 vlen
= __pcap_atoin(s
, &v
);
6846 if (proto
== Q_DECNET
)
6847 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6848 else if (proto
== Q_LINK
) {
6849 bpf_error(cstate
, "illegal link layer address");
6852 if (s
== NULL
&& q
.addr
== Q_NET
) {
6853 /* Promote short net number */
6854 while (v
&& (v
& 0xff000000) == 0) {
6859 /* Promote short ipaddr */
6861 mask
<<= 32 - vlen
;
6863 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6868 proto
= IPPROTO_UDP
;
6869 else if (proto
== Q_TCP
)
6870 proto
= IPPROTO_TCP
;
6871 else if (proto
== Q_SCTP
)
6872 proto
= IPPROTO_SCTP
;
6873 else if (proto
== Q_DEFAULT
)
6874 proto
= PROTO_UNDEF
;
6876 bpf_error(cstate
, "illegal qualifier of 'port'");
6879 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6883 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6884 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6890 proto
= IPPROTO_UDP
;
6891 else if (proto
== Q_TCP
)
6892 proto
= IPPROTO_TCP
;
6893 else if (proto
== Q_SCTP
)
6894 proto
= IPPROTO_SCTP
;
6895 else if (proto
== Q_DEFAULT
)
6896 proto
= PROTO_UNDEF
;
6898 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6901 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6905 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6906 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6911 bpf_error(cstate
, "'gateway' requires a name");
6915 return gen_proto(cstate
, (int)v
, proto
, dir
);
6918 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6933 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6934 unsigned int masklen
, struct qual q
)
6936 struct addrinfo
*res
;
6937 struct in6_addr
*addr
;
6938 struct in6_addr mask
;
6943 bpf_error(cstate
, "no mask %s supported", s2
);
6945 res
= pcap_nametoaddrinfo(s1
);
6947 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6950 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6951 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6953 if (sizeof(mask
) * 8 < masklen
)
6954 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6955 memset(&mask
, 0, sizeof(mask
));
6956 memset(&mask
, 0xff, masklen
/ 8);
6958 mask
.s6_addr
[masklen
/ 8] =
6959 (0xff << (8 - masklen
% 8)) & 0xff;
6962 a
= (uint32_t *)addr
;
6963 m
= (uint32_t *)&mask
;
6964 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6965 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6966 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6974 bpf_error(cstate
, "Mask syntax for networks only");
6978 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6984 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6991 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
6993 struct block
*b
, *tmp
;
6995 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6996 cstate
->e
= pcap_ether_aton(s
);
6997 if (cstate
->e
== NULL
)
6998 bpf_error(cstate
, "malloc");
6999 switch (cstate
->linktype
) {
7001 case DLT_NETANALYZER
:
7002 case DLT_NETANALYZER_TRANSPARENT
:
7003 tmp
= gen_prevlinkhdr_check(cstate
);
7004 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7009 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7012 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7014 case DLT_IEEE802_11
:
7015 case DLT_PRISM_HEADER
:
7016 case DLT_IEEE802_11_RADIO_AVS
:
7017 case DLT_IEEE802_11_RADIO
:
7019 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7021 case DLT_IP_OVER_FC
:
7022 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7027 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7035 bpf_error(cstate
, "ethernet address used in non-ether expression");
7040 sappend(struct slist
*s0
, struct slist
*s1
)
7043 * This is definitely not the best way to do this, but the
7044 * lists will rarely get long.
7051 static struct slist
*
7052 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7056 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7061 static struct slist
*
7062 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7066 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7072 * Modify "index" to use the value stored into its register as an
7073 * offset relative to the beginning of the header for the protocol
7074 * "proto", and allocate a register and put an item "size" bytes long
7075 * (1, 2, or 4) at that offset into that register, making it the register
7079 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7081 struct slist
*s
, *tmp
;
7083 int regno
= alloc_reg(cstate
);
7085 free_reg(cstate
, inst
->regno
);
7089 bpf_error(cstate
, "data size must be 1, 2, or 4");
7105 bpf_error(cstate
, "unsupported index operation");
7109 * The offset is relative to the beginning of the packet
7110 * data, if we have a radio header. (If we don't, this
7113 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7114 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7115 cstate
->linktype
!= DLT_PRISM_HEADER
)
7116 bpf_error(cstate
, "radio information not present in capture");
7119 * Load into the X register the offset computed into the
7120 * register specified by "index".
7122 s
= xfer_to_x(cstate
, inst
);
7125 * Load the item at that offset.
7127 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7129 sappend(inst
->s
, s
);
7134 * The offset is relative to the beginning of
7135 * the link-layer header.
7137 * XXX - what about ATM LANE? Should the index be
7138 * relative to the beginning of the AAL5 frame, so
7139 * that 0 refers to the beginning of the LE Control
7140 * field, or relative to the beginning of the LAN
7141 * frame, so that 0 refers, for Ethernet LANE, to
7142 * the beginning of the destination address?
7144 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7147 * If "s" is non-null, it has code to arrange that the
7148 * X register contains the length of the prefix preceding
7149 * the link-layer header. Add to it the offset computed
7150 * into the register specified by "index", and move that
7151 * into the X register. Otherwise, just load into the X
7152 * register the offset computed into the register specified
7156 sappend(s
, xfer_to_a(cstate
, inst
));
7157 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7158 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7160 s
= xfer_to_x(cstate
, inst
);
7163 * Load the item at the sum of the offset we've put in the
7164 * X register and the offset of the start of the link
7165 * layer header (which is 0 if the radio header is
7166 * variable-length; that header length is what we put
7167 * into the X register and then added to the index).
7169 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7170 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7172 sappend(inst
->s
, s
);
7186 * The offset is relative to the beginning of
7187 * the network-layer header.
7188 * XXX - are there any cases where we want
7189 * cstate->off_nl_nosnap?
7191 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7194 * If "s" is non-null, it has code to arrange that the
7195 * X register contains the variable part of the offset
7196 * of the link-layer payload. Add to it the offset
7197 * computed into the register specified by "index",
7198 * and move that into the X register. Otherwise, just
7199 * load into the X register the offset computed into
7200 * the register specified by "index".
7203 sappend(s
, xfer_to_a(cstate
, inst
));
7204 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7205 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7207 s
= xfer_to_x(cstate
, inst
);
7210 * Load the item at the sum of the offset we've put in the
7211 * X register, the offset of the start of the network
7212 * layer header from the beginning of the link-layer
7213 * payload, and the constant part of the offset of the
7214 * start of the link-layer payload.
7216 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7217 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7219 sappend(inst
->s
, s
);
7222 * Do the computation only if the packet contains
7223 * the protocol in question.
7225 b
= gen_proto_abbrev(cstate
, proto
);
7227 gen_and(inst
->b
, b
);
7241 * The offset is relative to the beginning of
7242 * the transport-layer header.
7244 * Load the X register with the length of the IPv4 header
7245 * (plus the offset of the link-layer header, if it's
7246 * a variable-length header), in bytes.
7248 * XXX - are there any cases where we want
7249 * cstate->off_nl_nosnap?
7250 * XXX - we should, if we're built with
7251 * IPv6 support, generate code to load either
7252 * IPv4, IPv6, or both, as appropriate.
7254 s
= gen_loadx_iphdrlen(cstate
);
7257 * The X register now contains the sum of the variable
7258 * part of the offset of the link-layer payload and the
7259 * length of the network-layer header.
7261 * Load into the A register the offset relative to
7262 * the beginning of the transport layer header,
7263 * add the X register to that, move that to the
7264 * X register, and load with an offset from the
7265 * X register equal to the sum of the constant part of
7266 * the offset of the link-layer payload and the offset,
7267 * relative to the beginning of the link-layer payload,
7268 * of the network-layer header.
7270 sappend(s
, xfer_to_a(cstate
, inst
));
7271 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7272 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7273 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7274 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7275 sappend(inst
->s
, s
);
7278 * Do the computation only if the packet contains
7279 * the protocol in question - which is true only
7280 * if this is an IP datagram and is the first or
7281 * only fragment of that datagram.
7283 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7285 gen_and(inst
->b
, b
);
7286 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7291 * Do the computation only if the packet contains
7292 * the protocol in question.
7294 b
= gen_proto_abbrev(cstate
, Q_IPV6
);
7296 gen_and(inst
->b
, b
);
7301 * Check if we have an icmp6 next header
7303 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7305 gen_and(inst
->b
, b
);
7310 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7312 * If "s" is non-null, it has code to arrange that the
7313 * X register contains the variable part of the offset
7314 * of the link-layer payload. Add to it the offset
7315 * computed into the register specified by "index",
7316 * and move that into the X register. Otherwise, just
7317 * load into the X register the offset computed into
7318 * the register specified by "index".
7321 sappend(s
, xfer_to_a(cstate
, inst
));
7322 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7323 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7325 s
= xfer_to_x(cstate
, inst
);
7329 * Load the item at the sum of the offset we've put in the
7330 * X register, the offset of the start of the network
7331 * layer header from the beginning of the link-layer
7332 * payload, and the constant part of the offset of the
7333 * start of the link-layer payload.
7335 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7336 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7339 sappend(inst
->s
, s
);
7343 inst
->regno
= regno
;
7344 s
= new_stmt(cstate
, BPF_ST
);
7346 sappend(inst
->s
, s
);
7352 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7353 struct arth
*a1
, int reversed
)
7355 struct slist
*s0
, *s1
, *s2
;
7356 struct block
*b
, *tmp
;
7358 s0
= xfer_to_x(cstate
, a1
);
7359 s1
= xfer_to_a(cstate
, a0
);
7360 if (code
== BPF_JEQ
) {
7361 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7362 b
= new_block(cstate
, JMP(code
));
7366 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7372 sappend(a0
->s
, a1
->s
);
7376 free_reg(cstate
, a0
->regno
);
7377 free_reg(cstate
, a1
->regno
);
7379 /* 'and' together protocol checks */
7382 gen_and(a0
->b
, tmp
= a1
->b
);
7396 gen_loadlen(compiler_state_t
*cstate
)
7398 int regno
= alloc_reg(cstate
);
7399 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7402 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7403 s
->next
= new_stmt(cstate
, BPF_ST
);
7404 s
->next
->s
.k
= regno
;
7412 gen_loadi(compiler_state_t
*cstate
, int val
)
7418 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7420 reg
= alloc_reg(cstate
);
7422 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7424 s
->next
= new_stmt(cstate
, BPF_ST
);
7433 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7437 s
= xfer_to_a(cstate
, a
);
7439 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7442 s
= new_stmt(cstate
, BPF_ST
);
7450 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7453 struct slist
*s0
, *s1
, *s2
;
7456 * Disallow division by, or modulus by, zero; we do this here
7457 * so that it gets done even if the optimizer is disabled.
7459 * Also disallow shifts by a value greater than 31; we do this
7460 * here, for the same reason.
7462 if (code
== BPF_DIV
) {
7463 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7464 bpf_error(cstate
, "division by zero");
7465 } else if (code
== BPF_MOD
) {
7466 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7467 bpf_error(cstate
, "modulus by zero");
7468 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7469 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7470 bpf_error(cstate
, "shift by more than 31 bits");
7472 s0
= xfer_to_x(cstate
, a1
);
7473 s1
= xfer_to_a(cstate
, a0
);
7474 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7479 sappend(a0
->s
, a1
->s
);
7481 free_reg(cstate
, a0
->regno
);
7482 free_reg(cstate
, a1
->regno
);
7484 s0
= new_stmt(cstate
, BPF_ST
);
7485 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7492 * Initialize the table of used registers and the current register.
7495 init_regs(compiler_state_t
*cstate
)
7498 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7502 * Return the next free register.
7505 alloc_reg(compiler_state_t
*cstate
)
7507 int n
= BPF_MEMWORDS
;
7510 if (cstate
->regused
[cstate
->curreg
])
7511 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7513 cstate
->regused
[cstate
->curreg
] = 1;
7514 return cstate
->curreg
;
7517 bpf_error(cstate
, "too many registers needed to evaluate expression");
7522 * Return a register to the table so it can
7526 free_reg(compiler_state_t
*cstate
, int n
)
7528 cstate
->regused
[n
] = 0;
7531 static struct block
*
7532 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7537 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7538 b
= new_block(cstate
, JMP(jmp
));
7546 gen_greater(compiler_state_t
*cstate
, int n
)
7548 return gen_len(cstate
, BPF_JGE
, n
);
7552 * Actually, this is less than or equal.
7555 gen_less(compiler_state_t
*cstate
, int n
)
7559 b
= gen_len(cstate
, BPF_JGT
, n
);
7566 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7567 * the beginning of the link-layer header.
7568 * XXX - that means you can't test values in the radiotap header, but
7569 * as that header is difficult if not impossible to parse generally
7570 * without a loop, that might not be a severe problem. A new keyword
7571 * "radio" could be added for that, although what you'd really want
7572 * would be a way of testing particular radio header values, which
7573 * would generate code appropriate to the radio header in question.
7576 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7586 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7589 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7593 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7597 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7601 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7605 b
= new_block(cstate
, JMP(BPF_JEQ
));
7612 static const u_char abroadcast
[] = { 0x0 };
7615 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7617 bpf_u_int32 hostmask
;
7618 struct block
*b0
, *b1
, *b2
;
7619 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7625 switch (cstate
->linktype
) {
7627 case DLT_ARCNET_LINUX
:
7628 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7630 case DLT_NETANALYZER
:
7631 case DLT_NETANALYZER_TRANSPARENT
:
7632 b1
= gen_prevlinkhdr_check(cstate
);
7633 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7638 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7640 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7641 case DLT_IEEE802_11
:
7642 case DLT_PRISM_HEADER
:
7643 case DLT_IEEE802_11_RADIO_AVS
:
7644 case DLT_IEEE802_11_RADIO
:
7646 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7647 case DLT_IP_OVER_FC
:
7648 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7650 bpf_error(cstate
, "not a broadcast link");
7656 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7657 * as an indication that we don't know the netmask, and fail
7660 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7661 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7662 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7663 hostmask
= ~cstate
->netmask
;
7664 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7665 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7666 (bpf_int32
)(~0 & hostmask
), hostmask
);
7671 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7676 * Generate code to test the low-order bit of a MAC address (that's
7677 * the bottom bit of the *first* byte).
7679 static struct block
*
7680 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7682 register struct block
*b0
;
7683 register struct slist
*s
;
7685 /* link[offset] & 1 != 0 */
7686 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7687 b0
= new_block(cstate
, JMP(BPF_JSET
));
7694 gen_multicast(compiler_state_t
*cstate
, int proto
)
7696 register struct block
*b0
, *b1
, *b2
;
7697 register struct slist
*s
;
7703 switch (cstate
->linktype
) {
7705 case DLT_ARCNET_LINUX
:
7706 /* all ARCnet multicasts use the same address */
7707 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7709 case DLT_NETANALYZER
:
7710 case DLT_NETANALYZER_TRANSPARENT
:
7711 b1
= gen_prevlinkhdr_check(cstate
);
7712 /* ether[0] & 1 != 0 */
7713 b0
= gen_mac_multicast(cstate
, 0);
7719 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7721 * XXX - was that referring to bit-order issues?
7723 /* fddi[1] & 1 != 0 */
7724 return gen_mac_multicast(cstate
, 1);
7726 /* tr[2] & 1 != 0 */
7727 return gen_mac_multicast(cstate
, 2);
7728 case DLT_IEEE802_11
:
7729 case DLT_PRISM_HEADER
:
7730 case DLT_IEEE802_11_RADIO_AVS
:
7731 case DLT_IEEE802_11_RADIO
:
7736 * For control frames, there is no DA.
7738 * For management frames, DA is at an
7739 * offset of 4 from the beginning of
7742 * For data frames, DA is at an offset
7743 * of 4 from the beginning of the packet
7744 * if To DS is clear and at an offset of
7745 * 16 from the beginning of the packet
7750 * Generate the tests to be done for data frames.
7752 * First, check for To DS set, i.e. "link[1] & 0x01".
7754 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7755 b1
= new_block(cstate
, JMP(BPF_JSET
));
7756 b1
->s
.k
= 0x01; /* To DS */
7760 * If To DS is set, the DA is at 16.
7762 b0
= gen_mac_multicast(cstate
, 16);
7766 * Now, check for To DS not set, i.e. check
7767 * "!(link[1] & 0x01)".
7769 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7770 b2
= new_block(cstate
, JMP(BPF_JSET
));
7771 b2
->s
.k
= 0x01; /* To DS */
7776 * If To DS is not set, the DA is at 4.
7778 b1
= gen_mac_multicast(cstate
, 4);
7782 * Now OR together the last two checks. That gives
7783 * the complete set of checks for data frames.
7788 * Now check for a data frame.
7789 * I.e, check "link[0] & 0x08".
7791 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7792 b1
= new_block(cstate
, JMP(BPF_JSET
));
7797 * AND that with the checks done for data frames.
7802 * If the high-order bit of the type value is 0, this
7803 * is a management frame.
7804 * I.e, check "!(link[0] & 0x08)".
7806 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7807 b2
= new_block(cstate
, JMP(BPF_JSET
));
7813 * For management frames, the DA is at 4.
7815 b1
= gen_mac_multicast(cstate
, 4);
7819 * OR that with the checks done for data frames.
7820 * That gives the checks done for management and
7826 * If the low-order bit of the type value is 1,
7827 * this is either a control frame or a frame
7828 * with a reserved type, and thus not a
7831 * I.e., check "!(link[0] & 0x04)".
7833 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7834 b1
= new_block(cstate
, JMP(BPF_JSET
));
7840 * AND that with the checks for data and management
7845 case DLT_IP_OVER_FC
:
7846 b0
= gen_mac_multicast(cstate
, 2);
7851 /* Link not known to support multicasts */
7855 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7856 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7861 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7862 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7866 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7871 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7872 * Outbound traffic is sent by this machine, while inbound traffic is
7873 * sent by a remote machine (and may include packets destined for a
7874 * unicast or multicast link-layer address we are not subscribing to).
7875 * These are the same definitions implemented by pcap_setdirection().
7876 * Capturing only unicast traffic destined for this host is probably
7877 * better accomplished using a higher-layer filter.
7880 gen_inbound(compiler_state_t
*cstate
, int dir
)
7882 register struct block
*b0
;
7885 * Only some data link types support inbound/outbound qualifiers.
7887 switch (cstate
->linktype
) {
7889 b0
= gen_relation(cstate
, BPF_JEQ
,
7890 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7891 gen_loadi(cstate
, 0),
7897 /* match outgoing packets */
7898 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7900 /* match incoming packets */
7901 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7906 /* match outgoing packets */
7907 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7909 /* to filter on inbound traffic, invert the match */
7914 case DLT_LINUX_SLL2
:
7915 /* match outgoing packets */
7916 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
7918 /* to filter on inbound traffic, invert the match */
7923 #ifdef HAVE_NET_PFVAR_H
7925 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7926 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7932 /* match outgoing packets */
7933 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7935 /* match incoming packets */
7936 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7940 case DLT_JUNIPER_MFR
:
7941 case DLT_JUNIPER_MLFR
:
7942 case DLT_JUNIPER_MLPPP
:
7943 case DLT_JUNIPER_ATM1
:
7944 case DLT_JUNIPER_ATM2
:
7945 case DLT_JUNIPER_PPPOE
:
7946 case DLT_JUNIPER_PPPOE_ATM
:
7947 case DLT_JUNIPER_GGSN
:
7948 case DLT_JUNIPER_ES
:
7949 case DLT_JUNIPER_MONITOR
:
7950 case DLT_JUNIPER_SERVICES
:
7951 case DLT_JUNIPER_ETHER
:
7952 case DLT_JUNIPER_PPP
:
7953 case DLT_JUNIPER_FRELAY
:
7954 case DLT_JUNIPER_CHDLC
:
7955 case DLT_JUNIPER_VP
:
7956 case DLT_JUNIPER_ST
:
7957 case DLT_JUNIPER_ISM
:
7958 case DLT_JUNIPER_VS
:
7959 case DLT_JUNIPER_SRX_E2E
:
7960 case DLT_JUNIPER_FIBRECHANNEL
:
7961 case DLT_JUNIPER_ATM_CEMIC
:
7963 /* juniper flags (including direction) are stored
7964 * the byte after the 3-byte magic number */
7966 /* match outgoing packets */
7967 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7969 /* match incoming packets */
7970 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7976 * If we have packet meta-data indicating a direction,
7977 * and that metadata can be checked by BPF code, check
7978 * it. Otherwise, give up, as this link-layer type has
7979 * nothing in the packet data.
7981 * Currently, the only platform where a BPF filter can
7982 * check that metadata is Linux with the in-kernel
7983 * BPF interpreter. If other packet capture mechanisms
7984 * and BPF filters also supported this, it would be
7985 * nice. It would be even better if they made that
7986 * metadata available so that we could provide it
7987 * with newer capture APIs, allowing it to be saved
7990 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7992 * This is Linux with PF_PACKET support.
7993 * If this is a *live* capture, we can look at
7994 * special meta-data in the filter expression;
7995 * if it's a savefile, we can't.
7997 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7998 /* We have a FILE *, so this is a savefile */
7999 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
8004 /* match outgoing packets */
8005 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8008 /* to filter on inbound traffic, invert the match */
8011 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8012 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
8015 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8020 #ifdef HAVE_NET_PFVAR_H
8021 /* PF firewall log matched interface */
8023 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8028 if (cstate
->linktype
!= DLT_PFLOG
) {
8029 bpf_error(cstate
, "ifname supported only on PF linktype");
8032 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8033 off
= offsetof(struct pfloghdr
, ifname
);
8034 if (strlen(ifname
) >= len
) {
8035 bpf_error(cstate
, "ifname interface names can only be %d characters",
8039 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
8043 /* PF firewall log ruleset name */
8045 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8049 if (cstate
->linktype
!= DLT_PFLOG
) {
8050 bpf_error(cstate
, "ruleset supported only on PF linktype");
8054 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8055 bpf_error(cstate
, "ruleset names can only be %ld characters",
8056 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8060 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8061 strlen(ruleset
), (const u_char
*)ruleset
);
8065 /* PF firewall log rule number */
8067 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8071 if (cstate
->linktype
!= DLT_PFLOG
) {
8072 bpf_error(cstate
, "rnr supported only on PF linktype");
8076 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8081 /* PF firewall log sub-rule number */
8083 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8087 if (cstate
->linktype
!= DLT_PFLOG
) {
8088 bpf_error(cstate
, "srnr supported only on PF linktype");
8092 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8097 /* PF firewall log reason code */
8099 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8103 if (cstate
->linktype
!= DLT_PFLOG
) {
8104 bpf_error(cstate
, "reason supported only on PF linktype");
8108 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8113 /* PF firewall log action */
8115 gen_pf_action(compiler_state_t
*cstate
, int action
)
8119 if (cstate
->linktype
!= DLT_PFLOG
) {
8120 bpf_error(cstate
, "action supported only on PF linktype");
8124 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8128 #else /* !HAVE_NET_PFVAR_H */
8130 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8132 bpf_error(cstate
, "libpcap was compiled without pf support");
8137 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8139 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8144 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8146 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8151 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8153 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8158 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8160 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8165 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8167 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8170 #endif /* HAVE_NET_PFVAR_H */
8172 /* IEEE 802.11 wireless header */
8174 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
8178 switch (cstate
->linktype
) {
8180 case DLT_IEEE802_11
:
8181 case DLT_PRISM_HEADER
:
8182 case DLT_IEEE802_11_RADIO_AVS
:
8183 case DLT_IEEE802_11_RADIO
:
8184 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8189 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8197 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8201 switch (cstate
->linktype
) {
8203 case DLT_IEEE802_11
:
8204 case DLT_PRISM_HEADER
:
8205 case DLT_IEEE802_11_RADIO_AVS
:
8206 case DLT_IEEE802_11_RADIO
:
8210 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8214 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8215 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8221 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8225 switch (cstate
->linktype
) {
8228 case DLT_ARCNET_LINUX
:
8229 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8230 q
.proto
== Q_LINK
) {
8231 cstate
->e
= pcap_ether_aton(s
);
8232 if (cstate
->e
== NULL
)
8233 bpf_error(cstate
, "malloc");
8234 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8239 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8245 bpf_error(cstate
, "aid supported only on ARCnet");
8250 static struct block
*
8251 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8253 register struct block
*b0
, *b1
;
8256 /* src comes first, different from Ethernet */
8258 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8261 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8264 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8265 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8271 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8272 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8277 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8281 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8285 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8289 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8293 bpf_error(cstate
, "'ra' is only supported on 802.11");
8297 bpf_error(cstate
, "'ta' is only supported on 802.11");
8304 static struct block
*
8305 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8307 struct block
*b0
, *b1
;
8309 /* check for VLAN, including QinQ */
8310 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8311 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8314 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8320 static struct block
*
8321 gen_vlan_vid_test(compiler_state_t
*cstate
, int vlan_num
)
8323 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8326 static struct block
*
8327 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8329 struct block
*b0
, *b1
;
8331 b0
= gen_vlan_tpid_test(cstate
);
8333 if (vlan_num
>= 0) {
8334 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8340 * Both payload and link header type follow the VLAN tags so that
8341 * both need to be updated.
8343 cstate
->off_linkpl
.constant_part
+= 4;
8344 cstate
->off_linktype
.constant_part
+= 4;
8349 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8350 /* add v to variable part of off */
8352 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8356 if (!off
->is_variable
)
8357 off
->is_variable
= 1;
8359 off
->reg
= alloc_reg(cstate
);
8361 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8364 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8367 s2
= new_stmt(cstate
, BPF_ST
);
8373 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8374 * and link type offsets first
8377 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8381 /* offset determined at run time, shift variable part */
8383 cstate
->is_vlan_vloffset
= 1;
8384 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8385 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8387 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8388 sappend(s
.next
, b_tpid
->head
->stmts
);
8389 b_tpid
->head
->stmts
= s
.next
;
8393 * patch block b_vid (VLAN id test) to load VID value either from packet
8394 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8397 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8399 struct slist
*s
, *s2
, *sjeq
;
8402 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8403 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8405 /* true -> next instructions, false -> beginning of b_vid */
8406 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8408 sjeq
->s
.jf
= b_vid
->stmts
;
8411 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8412 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8416 /* Jump to the test in b_vid. We need to jump one instruction before
8417 * the end of the b_vid block so that we only skip loading the TCI
8418 * from packet data and not the 'and' instruction extractging VID.
8421 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8423 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8427 /* insert our statements at the beginning of b_vid */
8428 sappend(s
, b_vid
->stmts
);
8433 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8434 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8435 * tag can be either in metadata or in packet data; therefore if the
8436 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8437 * header for VLAN tag. As the decision is done at run time, we need
8438 * update variable part of the offsets
8440 static struct block
*
8441 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8443 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8446 /* generate new filter code based on extracting packet
8448 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8449 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8451 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8456 * This is tricky. We need to insert the statements updating variable
8457 * parts of offsets before the the traditional TPID and VID tests so
8458 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8459 * we do not want this update to affect those checks. That's why we
8460 * generate both test blocks first and insert the statements updating
8461 * variable parts of both offsets after that. This wouldn't work if
8462 * there already were variable length link header when entering this
8463 * function but gen_vlan_bpf_extensions() isn't called in that case.
8465 b_tpid
= gen_vlan_tpid_test(cstate
);
8467 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8469 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8473 if (vlan_num
>= 0) {
8474 gen_vlan_patch_vid_test(cstate
, b_vid
);
8484 * support IEEE 802.1Q VLAN trunk over ethernet
8487 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
8491 /* can't check for VLAN-encapsulated packets inside MPLS */
8492 if (cstate
->label_stack_depth
> 0)
8493 bpf_error(cstate
, "no VLAN match after MPLS");
8496 * Check for a VLAN packet, and then change the offsets to point
8497 * to the type and data fields within the VLAN packet. Just
8498 * increment the offsets, so that we can support a hierarchy, e.g.
8499 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8502 * XXX - this is a bit of a kludge. If we were to split the
8503 * compiler into a parser that parses an expression and
8504 * generates an expression tree, and a code generator that
8505 * takes an expression tree (which could come from our
8506 * parser or from some other parser) and generates BPF code,
8507 * we could perhaps make the offsets parameters of routines
8508 * and, in the handler for an "AND" node, pass to subnodes
8509 * other than the VLAN node the adjusted offsets.
8511 * This would mean that "vlan" would, instead of changing the
8512 * behavior of *all* tests after it, change only the behavior
8513 * of tests ANDed with it. That would change the documented
8514 * semantics of "vlan", which might break some expressions.
8515 * However, it would mean that "(vlan and ip) or ip" would check
8516 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8517 * checking only for VLAN-encapsulated IP, so that could still
8518 * be considered worth doing; it wouldn't break expressions
8519 * that are of the form "vlan and ..." or "vlan N and ...",
8520 * which I suspect are the most common expressions involving
8521 * "vlan". "vlan or ..." doesn't necessarily do what the user
8522 * would really want, now, as all the "or ..." tests would
8523 * be done assuming a VLAN, even though the "or" could be viewed
8524 * as meaning "or, if this isn't a VLAN packet...".
8526 switch (cstate
->linktype
) {
8529 case DLT_NETANALYZER
:
8530 case DLT_NETANALYZER_TRANSPARENT
:
8531 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8532 /* Verify that this is the outer part of the packet and
8533 * not encapsulated somehow. */
8534 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8535 cstate
->off_linkhdr
.constant_part
==
8536 cstate
->off_outermostlinkhdr
.constant_part
) {
8538 * Do we need special VLAN handling?
8540 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8541 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8543 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8546 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8549 case DLT_IEEE802_11
:
8550 case DLT_PRISM_HEADER
:
8551 case DLT_IEEE802_11_RADIO_AVS
:
8552 case DLT_IEEE802_11_RADIO
:
8553 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8557 bpf_error(cstate
, "no VLAN support for data link type %d",
8562 cstate
->vlan_stack_depth
++;
8571 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8573 struct block
*b0
, *b1
;
8575 if (cstate
->label_stack_depth
> 0) {
8576 /* just match the bottom-of-stack bit clear */
8577 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8580 * We're not in an MPLS stack yet, so check the link-layer
8581 * type against MPLS.
8583 switch (cstate
->linktype
) {
8585 case DLT_C_HDLC
: /* fall through */
8587 case DLT_NETANALYZER
:
8588 case DLT_NETANALYZER_TRANSPARENT
:
8589 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8593 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8596 /* FIXME add other DLT_s ...
8597 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8598 * leave it for now */
8601 bpf_error(cstate
, "no MPLS support for data link type %d",
8608 /* If a specific MPLS label is requested, check it */
8609 if (label_num
>= 0) {
8610 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8611 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8612 0xfffff000); /* only compare the first 20 bits */
8618 * Change the offsets to point to the type and data fields within
8619 * the MPLS packet. Just increment the offsets, so that we
8620 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8621 * capture packets with an outer label of 100000 and an inner
8624 * Increment the MPLS stack depth as well; this indicates that
8625 * we're checking MPLS-encapsulated headers, to make sure higher
8626 * level code generators don't try to match against IP-related
8627 * protocols such as Q_ARP, Q_RARP etc.
8629 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8631 cstate
->off_nl_nosnap
+= 4;
8632 cstate
->off_nl
+= 4;
8633 cstate
->label_stack_depth
++;
8638 * Support PPPOE discovery and session.
8641 gen_pppoed(compiler_state_t
*cstate
)
8643 /* check for PPPoE discovery */
8644 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8648 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8650 struct block
*b0
, *b1
;
8653 * Test against the PPPoE session link-layer type.
8655 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8657 /* If a specific session is requested, check PPPoE session id */
8658 if (sess_num
>= 0) {
8659 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8660 (bpf_int32
)sess_num
, 0x0000ffff);
8666 * Change the offsets to point to the type and data fields within
8667 * the PPP packet, and note that this is PPPoE rather than
8670 * XXX - this is a bit of a kludge. If we were to split the
8671 * compiler into a parser that parses an expression and
8672 * generates an expression tree, and a code generator that
8673 * takes an expression tree (which could come from our
8674 * parser or from some other parser) and generates BPF code,
8675 * we could perhaps make the offsets parameters of routines
8676 * and, in the handler for an "AND" node, pass to subnodes
8677 * other than the PPPoE node the adjusted offsets.
8679 * This would mean that "pppoes" would, instead of changing the
8680 * behavior of *all* tests after it, change only the behavior
8681 * of tests ANDed with it. That would change the documented
8682 * semantics of "pppoes", which might break some expressions.
8683 * However, it would mean that "(pppoes and ip) or ip" would check
8684 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8685 * checking only for VLAN-encapsulated IP, so that could still
8686 * be considered worth doing; it wouldn't break expressions
8687 * that are of the form "pppoes and ..." which I suspect are the
8688 * most common expressions involving "pppoes". "pppoes or ..."
8689 * doesn't necessarily do what the user would really want, now,
8690 * as all the "or ..." tests would be done assuming PPPoE, even
8691 * though the "or" could be viewed as meaning "or, if this isn't
8692 * a PPPoE packet...".
8694 * The "network-layer" protocol is PPPoE, which has a 6-byte
8695 * PPPoE header, followed by a PPP packet.
8697 * There is no HDLC encapsulation for the PPP packet (it's
8698 * encapsulated in PPPoES instead), so the link-layer type
8699 * starts at the first byte of the PPP packet. For PPPoE,
8700 * that offset is relative to the beginning of the total
8701 * link-layer payload, including any 802.2 LLC header, so
8702 * it's 6 bytes past cstate->off_nl.
8704 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8705 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8706 cstate
->off_linkpl
.reg
);
8708 cstate
->off_linktype
= cstate
->off_linkhdr
;
8709 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8712 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8717 /* Check that this is Geneve and the VNI is correct if
8718 * specified. Parameterized to handle both IPv4 and IPv6. */
8719 static struct block
*
8720 gen_geneve_check(compiler_state_t
*cstate
,
8721 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8722 enum e_offrel offrel
, int vni
)
8724 struct block
*b0
, *b1
;
8726 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8728 /* Check that we are operating on version 0. Otherwise, we
8729 * can't decode the rest of the fields. The version is 2 bits
8730 * in the first byte of the Geneve header. */
8731 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8736 vni
<<= 8; /* VNI is in the upper 3 bytes */
8737 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8746 /* The IPv4 and IPv6 Geneve checks need to do two things:
8747 * - Verify that this actually is Geneve with the right VNI.
8748 * - Place the IP header length (plus variable link prefix if
8749 * needed) into register A to be used later to compute
8750 * the inner packet offsets. */
8751 static struct block
*
8752 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8754 struct block
*b0
, *b1
;
8755 struct slist
*s
, *s1
;
8757 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8759 /* Load the IP header length into A. */
8760 s
= gen_loadx_iphdrlen(cstate
);
8762 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8765 /* Forcibly append these statements to the true condition
8766 * of the protocol check by creating a new block that is
8767 * always true and ANDing them. */
8768 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8777 static struct block
*
8778 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8780 struct block
*b0
, *b1
;
8781 struct slist
*s
, *s1
;
8783 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8785 /* Load the IP header length. We need to account for a
8786 * variable length link prefix if there is one. */
8787 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8789 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8793 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8797 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8801 /* Forcibly append these statements to the true condition
8802 * of the protocol check by creating a new block that is
8803 * always true and ANDing them. */
8804 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8807 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8816 /* We need to store three values based on the Geneve header::
8817 * - The offset of the linktype.
8818 * - The offset of the end of the Geneve header.
8819 * - The offset of the end of the encapsulated MAC header. */
8820 static struct slist
*
8821 gen_geneve_offsets(compiler_state_t
*cstate
)
8823 struct slist
*s
, *s1
, *s_proto
;
8825 /* First we need to calculate the offset of the Geneve header
8826 * itself. This is composed of the IP header previously calculated
8827 * (include any variable link prefix) and stored in A plus the
8828 * fixed sized headers (fixed link prefix, MAC length, and UDP
8830 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8831 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8833 /* Stash this in X since we'll need it later. */
8834 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8837 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8839 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8843 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8844 cstate
->off_linktype
.is_variable
= 1;
8845 cstate
->off_linktype
.constant_part
= 0;
8847 s1
= new_stmt(cstate
, BPF_ST
);
8848 s1
->s
.k
= cstate
->off_linktype
.reg
;
8851 /* Load the Geneve option length and mask and shift to get the
8852 * number of bytes. It is stored in the first byte of the Geneve
8854 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8858 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8862 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8866 /* Add in the rest of the Geneve base header. */
8867 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8871 /* Add the Geneve header length to its offset and store. */
8872 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8876 /* Set the encapsulated type as Ethernet. Even though we may
8877 * not actually have Ethernet inside there are two reasons this
8879 * - The linktype field is always in EtherType format regardless
8880 * of whether it is in Geneve or an inner Ethernet frame.
8881 * - The only link layer that we have specific support for is
8882 * Ethernet. We will confirm that the packet actually is
8883 * Ethernet at runtime before executing these checks. */
8884 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8886 s1
= new_stmt(cstate
, BPF_ST
);
8887 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8890 /* Calculate whether we have an Ethernet header or just raw IP/
8891 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8892 * and linktype by 14 bytes so that the network header can be found
8893 * seamlessly. Otherwise, keep what we've calculated already. */
8895 /* We have a bare jmp so we can't use the optimizer. */
8896 cstate
->no_optimize
= 1;
8898 /* Load the EtherType in the Geneve header, 2 bytes in. */
8899 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8903 /* Load X with the end of the Geneve header. */
8904 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8905 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8908 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8909 * end of this check, we should have the total length in X. In
8910 * the non-Ethernet case, it's already there. */
8911 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8912 s_proto
->s
.k
= ETHERTYPE_TEB
;
8913 sappend(s
, s_proto
);
8915 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8919 /* Since this is Ethernet, use the EtherType of the payload
8920 * directly as the linktype. Overwrite what we already have. */
8921 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8925 s1
= new_stmt(cstate
, BPF_ST
);
8926 s1
->s
.k
= cstate
->off_linktype
.reg
;
8929 /* Advance two bytes further to get the end of the Ethernet
8931 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8935 /* Move the result to X. */
8936 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8939 /* Store the final result of our linkpl calculation. */
8940 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8941 cstate
->off_linkpl
.is_variable
= 1;
8942 cstate
->off_linkpl
.constant_part
= 0;
8944 s1
= new_stmt(cstate
, BPF_STX
);
8945 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8954 /* Check to see if this is a Geneve packet. */
8956 gen_geneve(compiler_state_t
*cstate
, int vni
)
8958 struct block
*b0
, *b1
;
8961 b0
= gen_geneve4(cstate
, vni
);
8962 b1
= gen_geneve6(cstate
, vni
);
8967 /* Later filters should act on the payload of the Geneve frame,
8968 * update all of the header pointers. Attach this code so that
8969 * it gets executed in the event that the Geneve filter matches. */
8970 s
= gen_geneve_offsets(cstate
);
8972 b1
= gen_true(cstate
);
8973 sappend(s
, b1
->stmts
);
8978 cstate
->is_geneve
= 1;
8983 /* Check that the encapsulated frame has a link layer header
8984 * for Ethernet filters. */
8985 static struct block
*
8986 gen_geneve_ll_check(compiler_state_t
*cstate
)
8989 struct slist
*s
, *s1
;
8991 /* The easiest way to see if there is a link layer present
8992 * is to check if the link layer header and payload are not
8995 /* Geneve always generates pure variable offsets so we can
8996 * compare only the registers. */
8997 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8998 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9000 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9001 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9004 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9013 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
9014 bpf_u_int32 jtype
, int reverse
)
9021 if (!cstate
->is_atm
)
9022 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9023 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9025 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
9030 if (!cstate
->is_atm
)
9031 bpf_error(cstate
, "'vci' supported only on raw ATM");
9032 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9034 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
9039 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9040 abort(); /* XXX - this isn't on FreeBSD */
9041 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
9046 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9048 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9049 0xffffffff, jtype
, reverse
, jvalue
);
9053 if (!cstate
->is_atm
)
9054 bpf_error(cstate
, "'callref' supported only on raw ATM");
9055 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9057 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
9058 jtype
, reverse
, jvalue
);
9068 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9070 struct block
*b0
, *b1
;
9075 /* Get all packets in Meta signalling Circuit */
9076 if (!cstate
->is_atm
)
9077 bpf_error(cstate
, "'metac' supported only on raw ATM");
9078 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9079 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9084 /* Get all packets in Broadcast Circuit*/
9085 if (!cstate
->is_atm
)
9086 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9087 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9088 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9093 /* Get all cells in Segment OAM F4 circuit*/
9094 if (!cstate
->is_atm
)
9095 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9096 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9097 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9102 /* Get all cells in End-to-End OAM F4 Circuit*/
9103 if (!cstate
->is_atm
)
9104 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9105 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9106 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9111 /* Get all packets in connection Signalling Circuit */
9112 if (!cstate
->is_atm
)
9113 bpf_error(cstate
, "'sc' supported only on raw ATM");
9114 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9115 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9120 /* Get all packets in ILMI Circuit */
9121 if (!cstate
->is_atm
)
9122 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9123 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9124 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9129 /* Get all LANE packets */
9130 if (!cstate
->is_atm
)
9131 bpf_error(cstate
, "'lane' supported only on raw ATM");
9132 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9135 * Arrange that all subsequent tests assume LANE
9136 * rather than LLC-encapsulated packets, and set
9137 * the offsets appropriately for LANE-encapsulated
9140 * We assume LANE means Ethernet, not Token Ring.
9142 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9143 cstate
->off_payload
+ 2, /* Ethernet header */
9145 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9146 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9147 cstate
->off_nl
= 0; /* Ethernet II */
9148 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9152 /* Get all LLC-encapsulated packets */
9153 if (!cstate
->is_atm
)
9154 bpf_error(cstate
, "'llc' supported only on raw ATM");
9155 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9156 cstate
->linktype
= cstate
->prevlinktype
;
9166 * Filtering for MTP2 messages based on li value
9167 * FISU, length is null
9168 * LSSU, length is 1 or 2
9169 * MSU, length is 3 or more
9170 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9173 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9175 struct block
*b0
, *b1
;
9180 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9181 (cstate
->linktype
!= DLT_ERF
) &&
9182 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9183 bpf_error(cstate
, "'fisu' supported only on MTP2");
9184 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9185 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
9189 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9190 (cstate
->linktype
!= DLT_ERF
) &&
9191 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9192 bpf_error(cstate
, "'lssu' supported only on MTP2");
9193 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
9194 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9199 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9200 (cstate
->linktype
!= DLT_ERF
) &&
9201 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9202 bpf_error(cstate
, "'msu' supported only on MTP2");
9203 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9207 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9208 (cstate
->linktype
!= DLT_ERF
) &&
9209 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9210 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9211 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9212 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9216 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9217 (cstate
->linktype
!= DLT_ERF
) &&
9218 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9219 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9220 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9221 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9226 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9227 (cstate
->linktype
!= DLT_ERF
) &&
9228 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9229 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9230 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9240 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
9241 bpf_u_int32 jtype
, int reverse
)
9244 bpf_u_int32 val1
, val2
, val3
;
9245 u_int newoff_sio
= cstate
->off_sio
;
9246 u_int newoff_opc
= cstate
->off_opc
;
9247 u_int newoff_dpc
= cstate
->off_dpc
;
9248 u_int newoff_sls
= cstate
->off_sls
;
9250 switch (mtp3field
) {
9253 newoff_sio
+= 3; /* offset for MTP2_HSL */
9257 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9258 bpf_error(cstate
, "'sio' supported only on SS7");
9259 /* sio coded on 1 byte so max value 255 */
9261 bpf_error(cstate
, "sio value %u too big; max value = 255",
9263 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9264 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9270 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9271 bpf_error(cstate
, "'opc' supported only on SS7");
9272 /* opc coded on 14 bits so max value 16383 */
9274 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9276 /* the following instructions are made to convert jvalue
9277 * to the form used to write opc in an ss7 message*/
9278 val1
= jvalue
& 0x00003c00;
9280 val2
= jvalue
& 0x000003fc;
9282 val3
= jvalue
& 0x00000003;
9284 jvalue
= val1
+ val2
+ val3
;
9285 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9286 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9294 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9295 bpf_error(cstate
, "'dpc' supported only on SS7");
9296 /* dpc coded on 14 bits so max value 16383 */
9298 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9300 /* the following instructions are made to convert jvalue
9301 * to the forme used to write dpc in an ss7 message*/
9302 val1
= jvalue
& 0x000000ff;
9304 val2
= jvalue
& 0x00003f00;
9306 jvalue
= val1
+ val2
;
9307 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9308 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9314 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9315 bpf_error(cstate
, "'sls' supported only on SS7");
9316 /* sls coded on 4 bits so max value 15 */
9318 bpf_error(cstate
, "sls value %u too big; max value = 15",
9320 /* the following instruction is made to convert jvalue
9321 * to the forme used to write sls in an ss7 message*/
9322 jvalue
= jvalue
<< 4;
9323 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9324 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9333 static struct block
*
9334 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9339 * Q.2931 signalling protocol messages for handling virtual circuits
9340 * establishment and teardown
9345 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9349 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9353 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9357 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9361 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9364 case A_RELEASE_DONE
:
9365 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9375 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9377 struct block
*b0
, *b1
;
9382 if (!cstate
->is_atm
)
9383 bpf_error(cstate
, "'oam' supported only on raw ATM");
9384 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
9388 if (!cstate
->is_atm
)
9389 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9391 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9392 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9394 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9400 * Get Q.2931 signalling messages for switched
9401 * virtual connection
9403 if (!cstate
->is_atm
)
9404 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9405 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9406 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9408 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9410 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9412 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9414 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9416 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9421 if (!cstate
->is_atm
)
9422 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9423 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9424 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9426 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9428 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9430 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9432 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);