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>
53 #include "ethertype.h"
57 #include "ieee80211.h"
59 #include "sunatmpos.h"
62 #include "pcap/ipnet.h"
68 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
69 #include <linux/types.h>
70 #include <linux/if_packet.h>
71 #include <linux/filter.h>
74 #ifdef HAVE_NET_PFVAR_H
75 #include <sys/socket.h>
77 #include <net/pfvar.h>
78 #include <net/if_pflog.h>
82 #define offsetof(s, e) ((size_t)&((s *)0)->e)
87 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
94 uint16_t u6_addr16
[8];
95 uint32_t u6_addr32
[4];
97 #define s6_addr in6_u.u6_addr8
98 #define s6_addr16 in6_u.u6_addr16
99 #define s6_addr32 in6_u.u6_addr32
100 #define s6_addr64 in6_u.u6_addr64
103 typedef unsigned short sa_family_t
;
105 #define __SOCKADDR_COMMON(sa_prefix) \
106 sa_family_t sa_prefix##family
108 /* Ditto, for IPv6. */
111 __SOCKADDR_COMMON (sin6_
);
112 uint16_t sin6_port
; /* Transport layer port # */
113 uint32_t sin6_flowinfo
; /* IPv6 flow information */
114 struct in6_addr sin6_addr
; /* IPv6 address */
117 #ifndef EAI_ADDRFAMILY
119 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
120 int ai_family
; /* PF_xxx */
121 int ai_socktype
; /* SOCK_xxx */
122 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
123 size_t ai_addrlen
; /* length of ai_addr */
124 char *ai_canonname
; /* canonical name for hostname */
125 struct sockaddr
*ai_addr
; /* binary address */
126 struct addrinfo
*ai_next
; /* next structure in linked list */
128 #endif /* EAI_ADDRFAMILY */
129 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
132 #include <netdb.h> /* for "struct addrinfo" */
134 #include <pcap/namedb.h>
136 #include "nametoaddr.h"
138 #define ETHERMTU 1500
140 #ifndef ETHERTYPE_TEB
141 #define ETHERTYPE_TEB 0x6558
144 #ifndef IPPROTO_HOPOPTS
145 #define IPPROTO_HOPOPTS 0
147 #ifndef IPPROTO_ROUTING
148 #define IPPROTO_ROUTING 43
150 #ifndef IPPROTO_FRAGMENT
151 #define IPPROTO_FRAGMENT 44
153 #ifndef IPPROTO_DSTOPTS
154 #define IPPROTO_DSTOPTS 60
157 #define IPPROTO_SCTP 132
160 #define GENEVE_PORT 6081
162 #ifdef HAVE_OS_PROTO_H
163 #include "os-proto.h"
166 #define JMP(c) ((c)|BPF_JMP|BPF_K)
169 * "Push" the current value of the link-layer header type and link-layer
170 * header offset onto a "stack", and set a new value. (It's not a
171 * full-blown stack; we keep only the top two items.)
173 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
175 (cs)->prevlinktype = (cs)->linktype; \
176 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
177 (cs)->linktype = (new_linktype); \
178 (cs)->off_linkhdr.is_variable = (new_is_variable); \
179 (cs)->off_linkhdr.constant_part = (new_constant_part); \
180 (cs)->off_linkhdr.reg = (new_reg); \
181 (cs)->is_geneve = 0; \
185 * Offset "not set" value.
187 #define OFFSET_NOT_SET 0xffffffffU
190 * Absolute offsets, which are offsets from the beginning of the raw
191 * packet data, are, in the general case, the sum of a variable value
192 * and a constant value; the variable value may be absent, in which
193 * case the offset is only the constant value, and the constant value
194 * may be zero, in which case the offset is only the variable value.
196 * bpf_abs_offset is a structure containing all that information:
198 * is_variable is 1 if there's a variable part.
200 * constant_part is the constant part of the value, possibly zero;
202 * if is_variable is 1, reg is the register number for a register
203 * containing the variable value if the register has been assigned,
213 * Value passed to gen_load_a() to indicate what the offset argument
214 * is relative to the beginning of.
217 OR_PACKET
, /* full packet data */
218 OR_LINKHDR
, /* link-layer header */
219 OR_PREVLINKHDR
, /* previous link-layer header */
220 OR_LLC
, /* 802.2 LLC header */
221 OR_PREVMPLSHDR
, /* previous MPLS header */
222 OR_LINKTYPE
, /* link-layer type */
223 OR_LINKPL
, /* link-layer payload */
224 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
225 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
226 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
230 * We divy out chunks of memory rather than call malloc each time so
231 * we don't have to worry about leaking memory. It's probably
232 * not a big deal if all this memory was wasted but if this ever
233 * goes into a library that would probably not be a good idea.
235 * XXX - this *is* in a library....
238 #define CHUNK0SIZE 1024
244 /* Code generator state */
246 struct _compiler_state
{
256 int outermostlinktype
;
261 /* Hack for handling VLAN and MPLS stacks. */
262 u_int label_stack_depth
;
263 u_int vlan_stack_depth
;
269 * As errors are handled by a longjmp, anything allocated must
270 * be freed in the longjmp handler, so it must be reachable
273 * One thing that's allocated is the result of pcap_nametoaddrinfo();
274 * it must be freed with freeaddrinfo(). This variable points to
275 * any addrinfo structure that would need to be freed.
280 * Various code constructs need to know the layout of the packet.
281 * These values give the necessary offsets from the beginning
282 * of the packet data.
286 * Absolute offset of the beginning of the link-layer header.
288 bpf_abs_offset off_linkhdr
;
291 * If we're checking a link-layer header for a packet encapsulated
292 * in another protocol layer, this is the equivalent information
293 * for the previous layers' link-layer header from the beginning
294 * of the raw packet data.
296 bpf_abs_offset off_prevlinkhdr
;
299 * This is the equivalent information for the outermost layers'
302 bpf_abs_offset off_outermostlinkhdr
;
305 * Absolute offset of the beginning of the link-layer payload.
307 bpf_abs_offset off_linkpl
;
310 * "off_linktype" is the offset to information in the link-layer
311 * header giving the packet type. This is an absolute offset
312 * from the beginning of the packet.
314 * For Ethernet, it's the offset of the Ethernet type field; this
315 * means that it must have a value that skips VLAN tags.
317 * For link-layer types that always use 802.2 headers, it's the
318 * offset of the LLC header; this means that it must have a value
319 * that skips VLAN tags.
321 * For PPP, it's the offset of the PPP type field.
323 * For Cisco HDLC, it's the offset of the CHDLC type field.
325 * For BSD loopback, it's the offset of the AF_ value.
327 * For Linux cooked sockets, it's the offset of the type field.
329 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
330 * encapsulation, in which case, IP is assumed.
332 bpf_abs_offset off_linktype
;
335 * TRUE if the link layer includes an ATM pseudo-header.
340 * TRUE if "geneve" appeared in the filter; it causes us to
341 * generate code that checks for a Geneve header and assume
342 * that later filters apply to the encapsulated payload.
347 * TRUE if we need variable length part of VLAN offset
349 int is_vlan_vloffset
;
352 * These are offsets for the ATM pseudo-header.
359 * These are offsets for the MTP2 fields.
365 * These are offsets for the MTP3 fields.
373 * This is the offset of the first byte after the ATM pseudo_header,
374 * or -1 if there is no ATM pseudo-header.
379 * These are offsets to the beginning of the network-layer header.
380 * They are relative to the beginning of the link-layer payload
381 * (i.e., they don't include off_linkhdr.constant_part or
382 * off_linkpl.constant_part).
384 * If the link layer never uses 802.2 LLC:
386 * "off_nl" and "off_nl_nosnap" are the same.
388 * If the link layer always uses 802.2 LLC:
390 * "off_nl" is the offset if there's a SNAP header following
393 * "off_nl_nosnap" is the offset if there's no SNAP header.
395 * If the link layer is Ethernet:
397 * "off_nl" is the offset if the packet is an Ethernet II packet
398 * (we assume no 802.3+802.2+SNAP);
400 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
401 * with an 802.2 header following it.
407 * Here we handle simple allocation of the scratch registers.
408 * If too many registers are alloc'd, the allocator punts.
410 int regused
[BPF_MEMWORDS
];
416 struct chunk chunks
[NCHUNKS
];
421 bpf_parser_error(compiler_state_t
*cstate
, const char *msg
)
423 bpf_error(cstate
, "can't parse filter expression: %s", msg
);
429 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
434 if (cstate
->bpf_pcap
!= NULL
)
435 (void)pcap_vsnprintf(pcap_geterr(cstate
->bpf_pcap
),
436 PCAP_ERRBUF_SIZE
, fmt
, ap
);
438 longjmp(cstate
->top_ctx
, 1);
442 static void init_linktype(compiler_state_t
*, pcap_t
*);
444 static void init_regs(compiler_state_t
*);
445 static int alloc_reg(compiler_state_t
*);
446 static void free_reg(compiler_state_t
*, int);
448 static void initchunks(compiler_state_t
*cstate
);
449 static void *newchunk(compiler_state_t
*cstate
, size_t);
450 static void freechunks(compiler_state_t
*cstate
);
451 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
452 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
453 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
454 static inline void syntax(compiler_state_t
*cstate
);
456 static void backpatch(struct block
*, struct block
*);
457 static void merge(struct block
*, struct block
*);
458 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
460 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
462 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
464 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
466 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
468 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
469 u_int
, bpf_int32
, bpf_u_int32
);
470 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
471 u_int
, const u_char
*);
472 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
473 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
474 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
476 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
478 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
479 static struct block
*gen_uncond(compiler_state_t
*, int);
480 static inline struct block
*gen_true(compiler_state_t
*);
481 static inline struct block
*gen_false(compiler_state_t
*);
482 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
483 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
484 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
485 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
486 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
487 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
488 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
489 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
490 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
492 static int ethertype_to_ppptype(int);
493 static struct block
*gen_linktype(compiler_state_t
*, int);
494 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
495 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
496 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
497 int, int, u_int
, u_int
);
499 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
500 struct in6_addr
*, int, int, u_int
, u_int
);
502 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
503 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
504 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
505 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
506 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
507 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
508 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
509 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
510 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
513 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
514 struct in6_addr
*, int, int, int);
517 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
518 struct addrinfo
*, int, int);
520 static struct block
*gen_ipfrag(compiler_state_t
*);
521 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
522 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
524 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
525 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
527 struct block
*gen_portop(compiler_state_t
*, int, int, int);
528 static struct block
*gen_port(compiler_state_t
*, int, int, int);
529 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
530 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
531 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
532 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
533 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
534 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
535 static int lookup_proto(compiler_state_t
*, const char *, int);
536 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
537 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
538 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
539 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
540 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
541 static struct block
*gen_len(compiler_state_t
*, int, int);
542 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
543 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
545 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
546 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
549 initchunks(compiler_state_t
*cstate
)
553 for (i
= 0; i
< NCHUNKS
; i
++) {
554 cstate
->chunks
[i
].n_left
= 0;
555 cstate
->chunks
[i
].m
= NULL
;
557 cstate
->cur_chunk
= 0;
561 newchunk(compiler_state_t
*cstate
, size_t n
)
568 /* XXX Round up to nearest long. */
569 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
571 /* XXX Round up to structure boundary. */
575 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
576 if (n
> cp
->n_left
) {
578 k
= ++cstate
->cur_chunk
;
580 bpf_error(cstate
, "out of memory");
581 size
= CHUNK0SIZE
<< k
;
582 cp
->m
= (void *)malloc(size
);
584 bpf_error(cstate
, "out of memory");
585 memset((char *)cp
->m
, 0, size
);
588 bpf_error(cstate
, "out of memory");
591 return (void *)((char *)cp
->m
+ cp
->n_left
);
595 freechunks(compiler_state_t
*cstate
)
599 for (i
= 0; i
< NCHUNKS
; ++i
)
600 if (cstate
->chunks
[i
].m
!= NULL
)
601 free(cstate
->chunks
[i
].m
);
605 * A strdup whose allocations are freed after code generation is over.
608 sdup(compiler_state_t
*cstate
, const char *s
)
610 size_t n
= strlen(s
) + 1;
611 char *cp
= newchunk(cstate
, n
);
617 static inline struct block
*
618 new_block(compiler_state_t
*cstate
, int code
)
622 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
629 static inline struct slist
*
630 new_stmt(compiler_state_t
*cstate
, int code
)
634 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
640 static struct block
*
641 gen_retblk(compiler_state_t
*cstate
, int v
)
643 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
649 static inline PCAP_NORETURN_DEF
void
650 syntax(compiler_state_t
*cstate
)
652 bpf_error(cstate
, "syntax error in filter expression");
656 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
657 const char *buf
, int optimize
, bpf_u_int32 mask
)
662 compiler_state_t cstate
;
663 const char * volatile xbuf
= buf
;
664 yyscan_t scanner
= NULL
;
665 YY_BUFFER_STATE in_buffer
= NULL
;
670 * If this pcap_t hasn't been activated, it doesn't have a
671 * link-layer type, so we can't use it.
674 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
675 "not-yet-activated pcap_t passed to pcap_compile");
687 * If the device on which we're capturing need to be notified
688 * that a new filter is being compiled, do so.
690 * This allows them to save a copy of it, in case, for example,
691 * they're implementing a form of remote packet capture, and
692 * want the remote machine to filter out the packets in which
693 * it's sending the packets it's captured.
695 * XXX - the fact that we happen to be compiling a filter
696 * doesn't necessarily mean we'll be installing it as the
697 * filter for this pcap_t; we might be running it from userland
698 * on captured packets to do packet classification. We really
699 * need a better way of handling this, but this is all that
700 * the WinPcap code did.
702 if (p
->save_current_filter_op
!= NULL
)
703 (p
->save_current_filter_op
)(p
, buf
);
707 cstate
.no_optimize
= 0;
711 cstate
.ic
.root
= NULL
;
712 cstate
.ic
.cur_mark
= 0;
716 if (setjmp(cstate
.top_ctx
)) {
718 if (cstate
.ai
!= NULL
)
719 freeaddrinfo(cstate
.ai
);
725 cstate
.netmask
= mask
;
727 cstate
.snaplen
= pcap_snapshot(p
);
728 if (cstate
.snaplen
== 0) {
729 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
730 "snaplen of 0 rejects all packets");
735 if (pcap_lex_init(&scanner
) != 0)
736 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
737 errno
, "can't initialize scanner");
738 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
741 * Associate the compiler state with the lexical analyzer
744 pcap_set_extra(&cstate
, scanner
);
746 init_linktype(&cstate
, p
);
747 (void)pcap_parse(scanner
, &cstate
);
749 if (cstate
.ic
.root
== NULL
)
750 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
752 if (optimize
&& !cstate
.no_optimize
) {
753 bpf_optimize(&cstate
, &cstate
.ic
);
754 if (cstate
.ic
.root
== NULL
||
755 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
756 bpf_error(&cstate
, "expression rejects all packets");
758 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
759 program
->bf_len
= len
;
761 rc
= 0; /* We're all okay */
765 * Clean up everything for the lexical analyzer.
767 if (in_buffer
!= NULL
)
768 pcap__delete_buffer(in_buffer
, scanner
);
770 pcap_lex_destroy(scanner
);
773 * Clean up our own allocated memory.
781 * entry point for using the compiler with no pcap open
782 * pass in all the stuff that is needed explicitly instead.
785 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
786 struct bpf_program
*program
,
787 const char *buf
, int optimize
, bpf_u_int32 mask
)
792 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
795 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
801 * Clean up a "struct bpf_program" by freeing all the memory allocated
805 pcap_freecode(struct bpf_program
*program
)
808 if (program
->bf_insns
!= NULL
) {
809 free((char *)program
->bf_insns
);
810 program
->bf_insns
= NULL
;
815 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
816 * which of the jt and jf fields has been resolved and which is a pointer
817 * back to another unresolved block (or nil). At least one of the fields
818 * in each block is already resolved.
821 backpatch(struct block
*list
, struct block
*target
)
838 * Merge the lists in b0 and b1, using the 'sense' field to indicate
839 * which of jt and jf is the link.
842 merge(struct block
*b0
, struct block
*b1
)
844 register struct block
**p
= &b0
;
846 /* Find end of list. */
848 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
850 /* Concatenate the lists. */
855 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
857 struct block
*ppi_dlt_check
;
860 * Insert before the statements of the first (root) block any
861 * statements needed to load the lengths of any variable-length
862 * headers into registers.
864 * XXX - a fancier strategy would be to insert those before the
865 * statements of all blocks that use those lengths and that
866 * have no predecessors that use them, so that we only compute
867 * the lengths if we need them. There might be even better
868 * approaches than that.
870 * However, those strategies would be more complicated, and
871 * as we don't generate code to compute a length if the
872 * program has no tests that use the length, and as most
873 * tests will probably use those lengths, we would just
874 * postpone computing the lengths so that it's not done
875 * for tests that fail early, and it's not clear that's
878 insert_compute_vloffsets(cstate
, p
->head
);
881 * For DLT_PPI captures, generate a check of the per-packet
882 * DLT value to make sure it's DLT_IEEE802_11.
884 * XXX - TurboCap cards use DLT_PPI for Ethernet.
885 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
886 * with appropriate Ethernet information and use that rather
887 * than using something such as DLT_PPI where you don't know
888 * the link-layer header type until runtime, which, in the
889 * general case, would force us to generate both Ethernet *and*
890 * 802.11 code (*and* anything else for which PPI is used)
891 * and choose between them early in the BPF program?
893 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
894 if (ppi_dlt_check
!= NULL
)
895 gen_and(ppi_dlt_check
, p
);
897 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
898 p
->sense
= !p
->sense
;
899 backpatch(p
, gen_retblk(cstate
, 0));
900 cstate
->ic
.root
= p
->head
;
904 gen_and(struct block
*b0
, struct block
*b1
)
906 backpatch(b0
, b1
->head
);
907 b0
->sense
= !b0
->sense
;
908 b1
->sense
= !b1
->sense
;
910 b1
->sense
= !b1
->sense
;
915 gen_or(struct block
*b0
, struct block
*b1
)
917 b0
->sense
= !b0
->sense
;
918 backpatch(b0
, b1
->head
);
919 b0
->sense
= !b0
->sense
;
925 gen_not(struct block
*b
)
927 b
->sense
= !b
->sense
;
930 static struct block
*
931 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
932 u_int size
, bpf_int32 v
)
934 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
937 static struct block
*
938 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
939 u_int size
, bpf_int32 v
)
941 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
944 static struct block
*
945 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
946 u_int size
, bpf_int32 v
)
948 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
951 static struct block
*
952 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
953 u_int size
, bpf_int32 v
)
955 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
958 static struct block
*
959 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
960 u_int size
, bpf_int32 v
)
962 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
965 static struct block
*
966 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
967 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
969 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
972 static struct block
*
973 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
974 u_int size
, const u_char
*v
)
976 register struct block
*b
, *tmp
;
980 register const u_char
*p
= &v
[size
- 4];
981 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
982 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
984 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
, w
);
991 register const u_char
*p
= &v
[size
- 2];
992 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
994 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
, w
);
1001 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1010 * AND the field of size "size" at offset "offset" relative to the header
1011 * specified by "offrel" with "mask", and compare it with the value "v"
1012 * with the test specified by "jtype"; if "reverse" is true, the test
1013 * should test the opposite of "jtype".
1015 static struct block
*
1016 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1017 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1020 struct slist
*s
, *s2
;
1023 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1025 if (mask
!= 0xffffffff) {
1026 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1031 b
= new_block(cstate
, JMP(jtype
));
1034 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1040 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1042 cstate
->pcap_fddipad
= p
->fddipad
;
1045 * We start out with only one link-layer header.
1047 cstate
->outermostlinktype
= pcap_datalink(p
);
1048 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1049 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1050 cstate
->off_outermostlinkhdr
.reg
= -1;
1052 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1053 cstate
->off_prevlinkhdr
.constant_part
= 0;
1054 cstate
->off_prevlinkhdr
.is_variable
= 0;
1055 cstate
->off_prevlinkhdr
.reg
= -1;
1057 cstate
->linktype
= cstate
->outermostlinktype
;
1058 cstate
->off_linkhdr
.constant_part
= 0;
1059 cstate
->off_linkhdr
.is_variable
= 0;
1060 cstate
->off_linkhdr
.reg
= -1;
1065 cstate
->off_linkpl
.constant_part
= 0;
1066 cstate
->off_linkpl
.is_variable
= 0;
1067 cstate
->off_linkpl
.reg
= -1;
1069 cstate
->off_linktype
.constant_part
= 0;
1070 cstate
->off_linktype
.is_variable
= 0;
1071 cstate
->off_linktype
.reg
= -1;
1074 * Assume it's not raw ATM with a pseudo-header, for now.
1077 cstate
->off_vpi
= OFFSET_NOT_SET
;
1078 cstate
->off_vci
= OFFSET_NOT_SET
;
1079 cstate
->off_proto
= OFFSET_NOT_SET
;
1080 cstate
->off_payload
= OFFSET_NOT_SET
;
1085 cstate
->is_geneve
= 0;
1088 * No variable length VLAN offset by default
1090 cstate
->is_vlan_vloffset
= 0;
1093 * And assume we're not doing SS7.
1095 cstate
->off_li
= OFFSET_NOT_SET
;
1096 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1097 cstate
->off_sio
= OFFSET_NOT_SET
;
1098 cstate
->off_opc
= OFFSET_NOT_SET
;
1099 cstate
->off_dpc
= OFFSET_NOT_SET
;
1100 cstate
->off_sls
= OFFSET_NOT_SET
;
1102 cstate
->label_stack_depth
= 0;
1103 cstate
->vlan_stack_depth
= 0;
1105 switch (cstate
->linktype
) {
1108 cstate
->off_linktype
.constant_part
= 2;
1109 cstate
->off_linkpl
.constant_part
= 6;
1110 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1111 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1114 case DLT_ARCNET_LINUX
:
1115 cstate
->off_linktype
.constant_part
= 4;
1116 cstate
->off_linkpl
.constant_part
= 8;
1117 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1118 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1122 cstate
->off_linktype
.constant_part
= 12;
1123 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1124 cstate
->off_nl
= 0; /* Ethernet II */
1125 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1130 * SLIP doesn't have a link level type. The 16 byte
1131 * header is hacked into our SLIP driver.
1133 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1134 cstate
->off_linkpl
.constant_part
= 16;
1136 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1139 case DLT_SLIP_BSDOS
:
1140 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1141 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1143 cstate
->off_linkpl
.constant_part
= 24;
1145 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1150 cstate
->off_linktype
.constant_part
= 0;
1151 cstate
->off_linkpl
.constant_part
= 4;
1153 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1157 cstate
->off_linktype
.constant_part
= 0;
1158 cstate
->off_linkpl
.constant_part
= 12;
1160 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1165 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1166 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1167 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1168 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1170 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1175 * This does no include the Ethernet header, and
1176 * only covers session state.
1178 cstate
->off_linktype
.constant_part
= 6;
1179 cstate
->off_linkpl
.constant_part
= 8;
1181 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1185 cstate
->off_linktype
.constant_part
= 5;
1186 cstate
->off_linkpl
.constant_part
= 24;
1188 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1193 * FDDI doesn't really have a link-level type field.
1194 * We set "off_linktype" to the offset of the LLC header.
1196 * To check for Ethernet types, we assume that SSAP = SNAP
1197 * is being used and pick out the encapsulated Ethernet type.
1198 * XXX - should we generate code to check for SNAP?
1200 cstate
->off_linktype
.constant_part
= 13;
1201 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1202 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1203 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1204 cstate
->off_nl
= 8; /* 802.2+SNAP */
1205 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1210 * Token Ring doesn't really have a link-level type field.
1211 * We set "off_linktype" to the offset of the LLC header.
1213 * To check for Ethernet types, we assume that SSAP = SNAP
1214 * is being used and pick out the encapsulated Ethernet type.
1215 * XXX - should we generate code to check for SNAP?
1217 * XXX - the header is actually variable-length.
1218 * Some various Linux patched versions gave 38
1219 * as "off_linktype" and 40 as "off_nl"; however,
1220 * if a token ring packet has *no* routing
1221 * information, i.e. is not source-routed, the correct
1222 * values are 20 and 22, as they are in the vanilla code.
1224 * A packet is source-routed iff the uppermost bit
1225 * of the first byte of the source address, at an
1226 * offset of 8, has the uppermost bit set. If the
1227 * packet is source-routed, the total number of bytes
1228 * of routing information is 2 plus bits 0x1F00 of
1229 * the 16-bit value at an offset of 14 (shifted right
1230 * 8 - figure out which byte that is).
1232 cstate
->off_linktype
.constant_part
= 14;
1233 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1234 cstate
->off_nl
= 8; /* 802.2+SNAP */
1235 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1238 case DLT_PRISM_HEADER
:
1239 case DLT_IEEE802_11_RADIO_AVS
:
1240 case DLT_IEEE802_11_RADIO
:
1241 cstate
->off_linkhdr
.is_variable
= 1;
1242 /* Fall through, 802.11 doesn't have a variable link
1243 * prefix but is otherwise the same. */
1245 case DLT_IEEE802_11
:
1247 * 802.11 doesn't really have a link-level type field.
1248 * We set "off_linktype.constant_part" to the offset of
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 * We also handle variable-length radio headers here.
1256 * The Prism header is in theory variable-length, but in
1257 * practice it's always 144 bytes long. However, some
1258 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1259 * sometimes or always supply an AVS header, so we
1260 * have to check whether the radio header is a Prism
1261 * header or an AVS header, so, in practice, it's
1264 cstate
->off_linktype
.constant_part
= 24;
1265 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1266 cstate
->off_linkpl
.is_variable
= 1;
1267 cstate
->off_nl
= 8; /* 802.2+SNAP */
1268 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1273 * At the moment we treat PPI the same way that we treat
1274 * normal Radiotap encoded packets. The difference is in
1275 * the function that generates the code at the beginning
1276 * to compute the header length. Since this code generator
1277 * of PPI supports bare 802.11 encapsulation only (i.e.
1278 * the encapsulated DLT should be DLT_IEEE802_11) we
1279 * generate code to check for this too.
1281 cstate
->off_linktype
.constant_part
= 24;
1282 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1283 cstate
->off_linkpl
.is_variable
= 1;
1284 cstate
->off_linkhdr
.is_variable
= 1;
1285 cstate
->off_nl
= 8; /* 802.2+SNAP */
1286 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1289 case DLT_ATM_RFC1483
:
1290 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1292 * assume routed, non-ISO PDUs
1293 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1295 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1296 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1297 * latter would presumably be treated the way PPPoE
1298 * should be, so you can do "pppoe and udp port 2049"
1299 * or "pppoa and tcp port 80" and have it check for
1300 * PPPo{A,E} and a PPP protocol of IP and....
1302 cstate
->off_linktype
.constant_part
= 0;
1303 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1304 cstate
->off_nl
= 8; /* 802.2+SNAP */
1305 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1310 * Full Frontal ATM; you get AALn PDUs with an ATM
1314 cstate
->off_vpi
= SUNATM_VPI_POS
;
1315 cstate
->off_vci
= SUNATM_VCI_POS
;
1316 cstate
->off_proto
= PROTO_POS
;
1317 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1318 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1319 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1320 cstate
->off_nl
= 8; /* 802.2+SNAP */
1321 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1327 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1328 cstate
->off_linkpl
.constant_part
= 0;
1330 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1333 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1334 cstate
->off_linktype
.constant_part
= 14;
1335 cstate
->off_linkpl
.constant_part
= 16;
1337 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1340 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1341 cstate
->off_linktype
.constant_part
= 0;
1342 cstate
->off_linkpl
.constant_part
= 20;
1344 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1349 * LocalTalk does have a 1-byte type field in the LLAP header,
1350 * but really it just indicates whether there is a "short" or
1351 * "long" DDP packet following.
1353 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1354 cstate
->off_linkpl
.constant_part
= 0;
1356 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1359 case DLT_IP_OVER_FC
:
1361 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1362 * link-level type field. We set "off_linktype" to the
1363 * offset of the LLC header.
1365 * To check for Ethernet types, we assume that SSAP = SNAP
1366 * is being used and pick out the encapsulated Ethernet type.
1367 * XXX - should we generate code to check for SNAP? RFC
1368 * 2625 says SNAP should be used.
1370 cstate
->off_linktype
.constant_part
= 16;
1371 cstate
->off_linkpl
.constant_part
= 16;
1372 cstate
->off_nl
= 8; /* 802.2+SNAP */
1373 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1378 * XXX - we should set this to handle SNAP-encapsulated
1379 * frames (NLPID of 0x80).
1381 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1382 cstate
->off_linkpl
.constant_part
= 0;
1384 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1388 * the only BPF-interesting FRF.16 frames are non-control frames;
1389 * Frame Relay has a variable length link-layer
1390 * so lets start with offset 4 for now and increments later on (FIXME);
1393 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1394 cstate
->off_linkpl
.constant_part
= 0;
1396 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1399 case DLT_APPLE_IP_OVER_IEEE1394
:
1400 cstate
->off_linktype
.constant_part
= 16;
1401 cstate
->off_linkpl
.constant_part
= 18;
1403 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1406 case DLT_SYMANTEC_FIREWALL
:
1407 cstate
->off_linktype
.constant_part
= 6;
1408 cstate
->off_linkpl
.constant_part
= 44;
1409 cstate
->off_nl
= 0; /* Ethernet II */
1410 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1413 #ifdef HAVE_NET_PFVAR_H
1415 cstate
->off_linktype
.constant_part
= 0;
1416 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1418 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1422 case DLT_JUNIPER_MFR
:
1423 case DLT_JUNIPER_MLFR
:
1424 case DLT_JUNIPER_MLPPP
:
1425 case DLT_JUNIPER_PPP
:
1426 case DLT_JUNIPER_CHDLC
:
1427 case DLT_JUNIPER_FRELAY
:
1428 cstate
->off_linktype
.constant_part
= 4;
1429 cstate
->off_linkpl
.constant_part
= 4;
1431 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1434 case DLT_JUNIPER_ATM1
:
1435 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1436 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1438 cstate
->off_nl_nosnap
= 10;
1441 case DLT_JUNIPER_ATM2
:
1442 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1443 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1445 cstate
->off_nl_nosnap
= 10;
1448 /* frames captured on a Juniper PPPoE service PIC
1449 * contain raw ethernet frames */
1450 case DLT_JUNIPER_PPPOE
:
1451 case DLT_JUNIPER_ETHER
:
1452 cstate
->off_linkpl
.constant_part
= 14;
1453 cstate
->off_linktype
.constant_part
= 16;
1454 cstate
->off_nl
= 18; /* Ethernet II */
1455 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1458 case DLT_JUNIPER_PPPOE_ATM
:
1459 cstate
->off_linktype
.constant_part
= 4;
1460 cstate
->off_linkpl
.constant_part
= 6;
1462 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1465 case DLT_JUNIPER_GGSN
:
1466 cstate
->off_linktype
.constant_part
= 6;
1467 cstate
->off_linkpl
.constant_part
= 12;
1469 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1472 case DLT_JUNIPER_ES
:
1473 cstate
->off_linktype
.constant_part
= 6;
1474 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1475 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1476 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1479 case DLT_JUNIPER_MONITOR
:
1480 cstate
->off_linktype
.constant_part
= 12;
1481 cstate
->off_linkpl
.constant_part
= 12;
1482 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1483 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1486 case DLT_BACNET_MS_TP
:
1487 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1488 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1489 cstate
->off_nl
= OFFSET_NOT_SET
;
1490 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1493 case DLT_JUNIPER_SERVICES
:
1494 cstate
->off_linktype
.constant_part
= 12;
1495 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1496 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1497 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1500 case DLT_JUNIPER_VP
:
1501 cstate
->off_linktype
.constant_part
= 18;
1502 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1503 cstate
->off_nl
= OFFSET_NOT_SET
;
1504 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1507 case DLT_JUNIPER_ST
:
1508 cstate
->off_linktype
.constant_part
= 18;
1509 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1510 cstate
->off_nl
= OFFSET_NOT_SET
;
1511 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1514 case DLT_JUNIPER_ISM
:
1515 cstate
->off_linktype
.constant_part
= 8;
1516 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1517 cstate
->off_nl
= OFFSET_NOT_SET
;
1518 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1521 case DLT_JUNIPER_VS
:
1522 case DLT_JUNIPER_SRX_E2E
:
1523 case DLT_JUNIPER_FIBRECHANNEL
:
1524 case DLT_JUNIPER_ATM_CEMIC
:
1525 cstate
->off_linktype
.constant_part
= 8;
1526 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1527 cstate
->off_nl
= OFFSET_NOT_SET
;
1528 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1533 cstate
->off_li_hsl
= 4;
1534 cstate
->off_sio
= 3;
1535 cstate
->off_opc
= 4;
1536 cstate
->off_dpc
= 4;
1537 cstate
->off_sls
= 7;
1538 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1539 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1540 cstate
->off_nl
= OFFSET_NOT_SET
;
1541 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1544 case DLT_MTP2_WITH_PHDR
:
1546 cstate
->off_li_hsl
= 8;
1547 cstate
->off_sio
= 7;
1548 cstate
->off_opc
= 8;
1549 cstate
->off_dpc
= 8;
1550 cstate
->off_sls
= 11;
1551 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1552 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1553 cstate
->off_nl
= OFFSET_NOT_SET
;
1554 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1558 cstate
->off_li
= 22;
1559 cstate
->off_li_hsl
= 24;
1560 cstate
->off_sio
= 23;
1561 cstate
->off_opc
= 24;
1562 cstate
->off_dpc
= 24;
1563 cstate
->off_sls
= 27;
1564 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1565 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1566 cstate
->off_nl
= OFFSET_NOT_SET
;
1567 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1571 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1572 cstate
->off_linkpl
.constant_part
= 4;
1574 cstate
->off_nl_nosnap
= 0;
1579 * Currently, only raw "link[N:M]" filtering is supported.
1581 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1582 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1583 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1584 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1588 cstate
->off_linktype
.constant_part
= 1;
1589 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1591 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1594 case DLT_NETANALYZER
:
1595 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1596 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1597 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1598 cstate
->off_nl
= 0; /* Ethernet II */
1599 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1602 case DLT_NETANALYZER_TRANSPARENT
:
1603 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1604 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1605 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1606 cstate
->off_nl
= 0; /* Ethernet II */
1607 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1612 * For values in the range in which we've assigned new
1613 * DLT_ values, only raw "link[N:M]" filtering is supported.
1615 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1616 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1617 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1618 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1619 cstate
->off_nl
= OFFSET_NOT_SET
;
1620 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1622 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1627 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1631 * Load a value relative to the specified absolute offset.
1633 static struct slist
*
1634 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1635 u_int offset
, u_int size
)
1637 struct slist
*s
, *s2
;
1639 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1642 * If "s" is non-null, it has code to arrange that the X register
1643 * contains the variable part of the absolute offset, so we
1644 * generate a load relative to that, with an offset of
1645 * abs_offset->constant_part + offset.
1647 * Otherwise, we can do an absolute load with an offset of
1648 * abs_offset->constant_part + offset.
1652 * "s" points to a list of statements that puts the
1653 * variable part of the absolute offset into the X register.
1654 * Do an indirect load, to use the X register as an offset.
1656 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1657 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1661 * There is no variable part of the absolute offset, so
1662 * just do an absolute load.
1664 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1665 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1671 * Load a value relative to the beginning of the specified header.
1673 static struct slist
*
1674 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1677 struct slist
*s
, *s2
;
1682 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1687 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1690 case OR_PREVLINKHDR
:
1691 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1695 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1698 case OR_PREVMPLSHDR
:
1699 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1703 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1706 case OR_LINKPL_NOSNAP
:
1707 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1711 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1716 * Load the X register with the length of the IPv4 header
1717 * (plus the offset of the link-layer header, if it's
1718 * preceded by a variable-length header such as a radio
1719 * header), in bytes.
1721 s
= gen_loadx_iphdrlen(cstate
);
1724 * Load the item at {offset of the link-layer payload} +
1725 * {offset, relative to the start of the link-layer
1726 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1727 * {specified offset}.
1729 * If the offset of the link-layer payload is variable,
1730 * the variable part of that offset is included in the
1731 * value in the X register, and we include the constant
1732 * part in the offset of the load.
1734 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1735 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1740 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1751 * Generate code to load into the X register the sum of the length of
1752 * the IPv4 header and the variable part of the offset of the link-layer
1755 static struct slist
*
1756 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1758 struct slist
*s
, *s2
;
1760 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1763 * The offset of the link-layer payload has a variable
1764 * part. "s" points to a list of statements that put
1765 * the variable part of that offset into the X register.
1767 * The 4*([k]&0xf) addressing mode can't be used, as we
1768 * don't have a constant offset, so we have to load the
1769 * value in question into the A register and add to it
1770 * the value from the X register.
1772 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1773 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1775 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1778 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1783 * The A register now contains the length of the IP header.
1784 * We need to add to it the variable part of the offset of
1785 * the link-layer payload, which is still in the X
1786 * register, and move the result into the X register.
1788 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1789 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1792 * The offset of the link-layer payload is a constant,
1793 * so no code was generated to load the (non-existent)
1794 * variable part of that offset.
1796 * This means we can use the 4*([k]&0xf) addressing
1797 * mode. Load the length of the IPv4 header, which
1798 * is at an offset of cstate->off_nl from the beginning of
1799 * the link-layer payload, and thus at an offset of
1800 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1801 * of the raw packet data, using that addressing mode.
1803 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1804 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1810 static struct block
*
1811 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1816 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1818 b
= new_block(cstate
, JMP(BPF_JEQ
));
1824 static inline struct block
*
1825 gen_true(compiler_state_t
*cstate
)
1827 return gen_uncond(cstate
, 1);
1830 static inline struct block
*
1831 gen_false(compiler_state_t
*cstate
)
1833 return gen_uncond(cstate
, 0);
1837 * Byte-swap a 32-bit number.
1838 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1839 * big-endian platforms.)
1841 #define SWAPLONG(y) \
1842 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1845 * Generate code to match a particular packet type.
1847 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1848 * value, if <= ETHERMTU. We use that to determine whether to
1849 * match the type/length field or to check the type/length field for
1850 * a value <= ETHERMTU to see whether it's a type field and then do
1851 * the appropriate test.
1853 static struct block
*
1854 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1856 struct block
*b0
, *b1
;
1862 case LLCSAP_NETBEUI
:
1864 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1865 * so we check the DSAP and SSAP.
1867 * LLCSAP_IP checks for IP-over-802.2, rather
1868 * than IP-over-Ethernet or IP-over-SNAP.
1870 * XXX - should we check both the DSAP and the
1871 * SSAP, like this, or should we check just the
1872 * DSAP, as we do for other types <= ETHERMTU
1873 * (i.e., other SAP values)?
1875 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1877 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1878 ((proto
<< 8) | proto
));
1886 * Ethernet_II frames, which are Ethernet
1887 * frames with a frame type of ETHERTYPE_IPX;
1889 * Ethernet_802.3 frames, which are 802.3
1890 * frames (i.e., the type/length field is
1891 * a length field, <= ETHERMTU, rather than
1892 * a type field) with the first two bytes
1893 * after the Ethernet/802.3 header being
1896 * Ethernet_802.2 frames, which are 802.3
1897 * frames with an 802.2 LLC header and
1898 * with the IPX LSAP as the DSAP in the LLC
1901 * Ethernet_SNAP frames, which are 802.3
1902 * frames with an LLC header and a SNAP
1903 * header and with an OUI of 0x000000
1904 * (encapsulated Ethernet) and a protocol
1905 * ID of ETHERTYPE_IPX in the SNAP header.
1907 * XXX - should we generate the same code both
1908 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1912 * This generates code to check both for the
1913 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1915 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1916 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1920 * Now we add code to check for SNAP frames with
1921 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1923 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1927 * Now we generate code to check for 802.3
1928 * frames in general.
1930 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1934 * Now add the check for 802.3 frames before the
1935 * check for Ethernet_802.2 and Ethernet_802.3,
1936 * as those checks should only be done on 802.3
1937 * frames, not on Ethernet frames.
1942 * Now add the check for Ethernet_II frames, and
1943 * do that before checking for the other frame
1946 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1950 case ETHERTYPE_ATALK
:
1951 case ETHERTYPE_AARP
:
1953 * EtherTalk (AppleTalk protocols on Ethernet link
1954 * layer) may use 802.2 encapsulation.
1958 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1959 * we check for an Ethernet type field less than
1960 * 1500, which means it's an 802.3 length field.
1962 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1966 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1967 * SNAP packets with an organization code of
1968 * 0x080007 (Apple, for Appletalk) and a protocol
1969 * type of ETHERTYPE_ATALK (Appletalk).
1971 * 802.2-encapsulated ETHERTYPE_AARP packets are
1972 * SNAP packets with an organization code of
1973 * 0x000000 (encapsulated Ethernet) and a protocol
1974 * type of ETHERTYPE_AARP (Appletalk ARP).
1976 if (proto
== ETHERTYPE_ATALK
)
1977 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
1978 else /* proto == ETHERTYPE_AARP */
1979 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
1983 * Check for Ethernet encapsulation (Ethertalk
1984 * phase 1?); we just check for the Ethernet
1987 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1993 if (proto
<= ETHERMTU
) {
1995 * This is an LLC SAP value, so the frames
1996 * that match would be 802.2 frames.
1997 * Check that the frame is an 802.2 frame
1998 * (i.e., that the length/type field is
1999 * a length field, <= ETHERMTU) and
2000 * then check the DSAP.
2002 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2004 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2009 * This is an Ethernet type, so compare
2010 * the length/type field with it (if
2011 * the frame is an 802.2 frame, the length
2012 * field will be <= ETHERMTU, and, as
2013 * "proto" is > ETHERMTU, this test
2014 * will fail and the frame won't match,
2015 * which is what we want).
2017 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2023 static struct block
*
2024 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2027 * For DLT_NULL, the link-layer header is a 32-bit word
2028 * containing an AF_ value in *host* byte order, and for
2029 * DLT_ENC, the link-layer header begins with a 32-bit
2030 * word containing an AF_ value in host byte order.
2032 * In addition, if we're reading a saved capture file,
2033 * the host byte order in the capture may not be the
2034 * same as the host byte order on this machine.
2036 * For DLT_LOOP, the link-layer header is a 32-bit
2037 * word containing an AF_ value in *network* byte order.
2039 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2041 * The AF_ value is in host byte order, but the BPF
2042 * interpreter will convert it to network byte order.
2044 * If this is a save file, and it's from a machine
2045 * with the opposite byte order to ours, we byte-swap
2048 * Then we run it through "htonl()", and generate
2049 * code to compare against the result.
2051 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2052 proto
= SWAPLONG(proto
);
2053 proto
= htonl(proto
);
2055 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2059 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2060 * or IPv6 then we have an error.
2062 static struct block
*
2063 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2068 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2071 case ETHERTYPE_IPV6
:
2072 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2073 (bpf_int32
)IPH_AF_INET6
);
2080 return gen_false(cstate
);
2084 * Generate code to match a particular packet type.
2086 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2087 * value, if <= ETHERMTU. We use that to determine whether to
2088 * match the type field or to check the type field for the special
2089 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2091 static struct block
*
2092 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2094 struct block
*b0
, *b1
;
2100 case LLCSAP_NETBEUI
:
2102 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2103 * so we check the DSAP and SSAP.
2105 * LLCSAP_IP checks for IP-over-802.2, rather
2106 * than IP-over-Ethernet or IP-over-SNAP.
2108 * XXX - should we check both the DSAP and the
2109 * SSAP, like this, or should we check just the
2110 * DSAP, as we do for other types <= ETHERMTU
2111 * (i.e., other SAP values)?
2113 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2114 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2115 ((proto
<< 8) | proto
));
2121 * Ethernet_II frames, which are Ethernet
2122 * frames with a frame type of ETHERTYPE_IPX;
2124 * Ethernet_802.3 frames, which have a frame
2125 * type of LINUX_SLL_P_802_3;
2127 * Ethernet_802.2 frames, which are 802.3
2128 * frames with an 802.2 LLC header (i.e, have
2129 * a frame type of LINUX_SLL_P_802_2) and
2130 * with the IPX LSAP as the DSAP in the LLC
2133 * Ethernet_SNAP frames, which are 802.3
2134 * frames with an LLC header and a SNAP
2135 * header and with an OUI of 0x000000
2136 * (encapsulated Ethernet) and a protocol
2137 * ID of ETHERTYPE_IPX in the SNAP header.
2139 * First, do the checks on LINUX_SLL_P_802_2
2140 * frames; generate the check for either
2141 * Ethernet_802.2 or Ethernet_SNAP frames, and
2142 * then put a check for LINUX_SLL_P_802_2 frames
2145 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2146 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2148 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2152 * Now check for 802.3 frames and OR that with
2153 * the previous test.
2155 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2159 * Now add the check for Ethernet_II frames, and
2160 * do that before checking for the other frame
2163 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2167 case ETHERTYPE_ATALK
:
2168 case ETHERTYPE_AARP
:
2170 * EtherTalk (AppleTalk protocols on Ethernet link
2171 * layer) may use 802.2 encapsulation.
2175 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2176 * we check for the 802.2 protocol type in the
2177 * "Ethernet type" field.
2179 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2182 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2183 * SNAP packets with an organization code of
2184 * 0x080007 (Apple, for Appletalk) and a protocol
2185 * type of ETHERTYPE_ATALK (Appletalk).
2187 * 802.2-encapsulated ETHERTYPE_AARP packets are
2188 * SNAP packets with an organization code of
2189 * 0x000000 (encapsulated Ethernet) and a protocol
2190 * type of ETHERTYPE_AARP (Appletalk ARP).
2192 if (proto
== ETHERTYPE_ATALK
)
2193 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2194 else /* proto == ETHERTYPE_AARP */
2195 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2199 * Check for Ethernet encapsulation (Ethertalk
2200 * phase 1?); we just check for the Ethernet
2203 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2209 if (proto
<= ETHERMTU
) {
2211 * This is an LLC SAP value, so the frames
2212 * that match would be 802.2 frames.
2213 * Check for the 802.2 protocol type
2214 * in the "Ethernet type" field, and
2215 * then check the DSAP.
2217 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2218 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2224 * This is an Ethernet type, so compare
2225 * the length/type field with it (if
2226 * the frame is an 802.2 frame, the length
2227 * field will be <= ETHERMTU, and, as
2228 * "proto" is > ETHERMTU, this test
2229 * will fail and the frame won't match,
2230 * which is what we want).
2232 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2237 static struct slist
*
2238 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2240 struct slist
*s1
, *s2
;
2241 struct slist
*sjeq_avs_cookie
;
2242 struct slist
*sjcommon
;
2245 * This code is not compatible with the optimizer, as
2246 * we are generating jmp instructions within a normal
2247 * slist of instructions
2249 cstate
->no_optimize
= 1;
2252 * Generate code to load the length of the radio header into
2253 * the register assigned to hold that length, if one has been
2254 * assigned. (If one hasn't been assigned, no code we've
2255 * generated uses that prefix, so we don't need to generate any
2258 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2259 * or always use the AVS header rather than the Prism header.
2260 * We load a 4-byte big-endian value at the beginning of the
2261 * raw packet data, and see whether, when masked with 0xFFFFF000,
2262 * it's equal to 0x80211000. If so, that indicates that it's
2263 * an AVS header (the masked-out bits are the version number).
2264 * Otherwise, it's a Prism header.
2266 * XXX - the Prism header is also, in theory, variable-length,
2267 * but no known software generates headers that aren't 144
2270 if (cstate
->off_linkhdr
.reg
!= -1) {
2274 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2278 * AND it with 0xFFFFF000.
2280 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2281 s2
->s
.k
= 0xFFFFF000;
2285 * Compare with 0x80211000.
2287 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2288 sjeq_avs_cookie
->s
.k
= 0x80211000;
2289 sappend(s1
, sjeq_avs_cookie
);
2294 * The 4 bytes at an offset of 4 from the beginning of
2295 * the AVS header are the length of the AVS header.
2296 * That field is big-endian.
2298 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2301 sjeq_avs_cookie
->s
.jt
= s2
;
2304 * Now jump to the code to allocate a register
2305 * into which to save the header length and
2306 * store the length there. (The "jump always"
2307 * instruction needs to have the k field set;
2308 * it's added to the PC, so, as we're jumping
2309 * over a single instruction, it should be 1.)
2311 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2313 sappend(s1
, sjcommon
);
2316 * Now for the code that handles the Prism header.
2317 * Just load the length of the Prism header (144)
2318 * into the A register. Have the test for an AVS
2319 * header branch here if we don't have an AVS header.
2321 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2324 sjeq_avs_cookie
->s
.jf
= s2
;
2327 * Now allocate a register to hold that value and store
2328 * it. The code for the AVS header will jump here after
2329 * loading the length of the AVS header.
2331 s2
= new_stmt(cstate
, BPF_ST
);
2332 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2334 sjcommon
->s
.jf
= s2
;
2337 * Now move it into the X register.
2339 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2347 static struct slist
*
2348 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2350 struct slist
*s1
, *s2
;
2353 * Generate code to load the length of the AVS header into
2354 * the register assigned to hold that length, if one has been
2355 * assigned. (If one hasn't been assigned, no code we've
2356 * generated uses that prefix, so we don't need to generate any
2359 if (cstate
->off_linkhdr
.reg
!= -1) {
2361 * The 4 bytes at an offset of 4 from the beginning of
2362 * the AVS header are the length of the AVS header.
2363 * That field is big-endian.
2365 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2369 * Now allocate a register to hold that value and store
2372 s2
= new_stmt(cstate
, BPF_ST
);
2373 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2377 * Now move it into the X register.
2379 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2387 static struct slist
*
2388 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2390 struct slist
*s1
, *s2
;
2393 * Generate code to load the length of the radiotap header into
2394 * the register assigned to hold that length, if one has been
2395 * assigned. (If one hasn't been assigned, no code we've
2396 * generated uses that prefix, so we don't need to generate any
2399 if (cstate
->off_linkhdr
.reg
!= -1) {
2401 * The 2 bytes at offsets of 2 and 3 from the beginning
2402 * of the radiotap header are the length of the radiotap
2403 * header; unfortunately, it's little-endian, so we have
2404 * to load it a byte at a time and construct the value.
2408 * Load the high-order byte, at an offset of 3, shift it
2409 * left a byte, and put the result in the X register.
2411 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2413 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2416 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2420 * Load the next byte, at an offset of 2, and OR the
2421 * value from the X register into it.
2423 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2426 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2430 * Now allocate a register to hold that value and store
2433 s2
= new_stmt(cstate
, BPF_ST
);
2434 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2438 * Now move it into the X register.
2440 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2449 * At the moment we treat PPI as normal Radiotap encoded
2450 * packets. The difference is in the function that generates
2451 * the code at the beginning to compute the header length.
2452 * Since this code generator of PPI supports bare 802.11
2453 * encapsulation only (i.e. the encapsulated DLT should be
2454 * DLT_IEEE802_11) we generate code to check for this too;
2455 * that's done in finish_parse().
2457 static struct slist
*
2458 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2460 struct slist
*s1
, *s2
;
2463 * Generate code to load the length of the radiotap header
2464 * into the register assigned to hold that length, if one has
2467 if (cstate
->off_linkhdr
.reg
!= -1) {
2469 * The 2 bytes at offsets of 2 and 3 from the beginning
2470 * of the radiotap header are the length of the radiotap
2471 * header; unfortunately, it's little-endian, so we have
2472 * to load it a byte at a time and construct the value.
2476 * Load the high-order byte, at an offset of 3, shift it
2477 * left a byte, and put the result in the X register.
2479 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2481 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2484 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2488 * Load the next byte, at an offset of 2, and OR the
2489 * value from the X register into it.
2491 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2494 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2498 * Now allocate a register to hold that value and store
2501 s2
= new_stmt(cstate
, BPF_ST
);
2502 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2506 * Now move it into the X register.
2508 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2517 * Load a value relative to the beginning of the link-layer header after the 802.11
2518 * header, i.e. LLC_SNAP.
2519 * The link-layer header doesn't necessarily begin at the beginning
2520 * of the packet data; there might be a variable-length prefix containing
2521 * radio information.
2523 static struct slist
*
2524 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2527 struct slist
*sjset_data_frame_1
;
2528 struct slist
*sjset_data_frame_2
;
2529 struct slist
*sjset_qos
;
2530 struct slist
*sjset_radiotap_flags_present
;
2531 struct slist
*sjset_radiotap_ext_present
;
2532 struct slist
*sjset_radiotap_tsft_present
;
2533 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2534 struct slist
*s_roundup
;
2536 if (cstate
->off_linkpl
.reg
== -1) {
2538 * No register has been assigned to the offset of
2539 * the link-layer payload, which means nobody needs
2540 * it; don't bother computing it - just return
2541 * what we already have.
2547 * This code is not compatible with the optimizer, as
2548 * we are generating jmp instructions within a normal
2549 * slist of instructions
2551 cstate
->no_optimize
= 1;
2554 * If "s" is non-null, it has code to arrange that the X register
2555 * contains the length of the prefix preceding the link-layer
2558 * Otherwise, the length of the prefix preceding the link-layer
2559 * header is "off_outermostlinkhdr.constant_part".
2563 * There is no variable-length header preceding the
2564 * link-layer header.
2566 * Load the length of the fixed-length prefix preceding
2567 * the link-layer header (if any) into the X register,
2568 * and store it in the cstate->off_linkpl.reg register.
2569 * That length is off_outermostlinkhdr.constant_part.
2571 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2572 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2576 * The X register contains the offset of the beginning of the
2577 * link-layer header; add 24, which is the minimum length
2578 * of the MAC header for a data frame, to that, and store it
2579 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2580 * which is at the offset in the X register, with an indexed load.
2582 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2584 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2587 s2
= new_stmt(cstate
, BPF_ST
);
2588 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2591 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2596 * Check the Frame Control field to see if this is a data frame;
2597 * a data frame has the 0x08 bit (b3) in that field set and the
2598 * 0x04 bit (b2) clear.
2600 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2601 sjset_data_frame_1
->s
.k
= 0x08;
2602 sappend(s
, sjset_data_frame_1
);
2605 * If b3 is set, test b2, otherwise go to the first statement of
2606 * the rest of the program.
2608 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2609 sjset_data_frame_2
->s
.k
= 0x04;
2610 sappend(s
, sjset_data_frame_2
);
2611 sjset_data_frame_1
->s
.jf
= snext
;
2614 * If b2 is not set, this is a data frame; test the QoS bit.
2615 * Otherwise, go to the first statement of the rest of the
2618 sjset_data_frame_2
->s
.jt
= snext
;
2619 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2620 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2621 sappend(s
, sjset_qos
);
2624 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2626 * Otherwise, go to the first statement of the rest of the
2629 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2630 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2632 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2635 s2
= new_stmt(cstate
, BPF_ST
);
2636 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2640 * If we have a radiotap header, look at it to see whether
2641 * there's Atheros padding between the MAC-layer header
2644 * Note: all of the fields in the radiotap header are
2645 * little-endian, so we byte-swap all of the values
2646 * we test against, as they will be loaded as big-endian
2649 * XXX - in the general case, we would have to scan through
2650 * *all* the presence bits, if there's more than one word of
2651 * presence bits. That would require a loop, meaning that
2652 * we wouldn't be able to run the filter in the kernel.
2654 * We assume here that the Atheros adapters that insert the
2655 * annoying padding don't have multiple antennae and therefore
2656 * do not generate radiotap headers with multiple presence words.
2658 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2660 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2661 * in the first presence flag word?
2663 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2667 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2668 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2669 sappend(s
, sjset_radiotap_flags_present
);
2672 * If not, skip all of this.
2674 sjset_radiotap_flags_present
->s
.jf
= snext
;
2677 * Otherwise, is the "extension" bit set in that word?
2679 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2680 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2681 sappend(s
, sjset_radiotap_ext_present
);
2682 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2685 * If so, skip all of this.
2687 sjset_radiotap_ext_present
->s
.jt
= snext
;
2690 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2692 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2693 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2694 sappend(s
, sjset_radiotap_tsft_present
);
2695 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2698 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2699 * at an offset of 16 from the beginning of the raw packet
2700 * data (8 bytes for the radiotap header and 8 bytes for
2703 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2706 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2709 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2711 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2712 sjset_tsft_datapad
->s
.k
= 0x20;
2713 sappend(s
, sjset_tsft_datapad
);
2716 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2717 * at an offset of 8 from the beginning of the raw packet
2718 * data (8 bytes for the radiotap header).
2720 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2723 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2726 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2728 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2729 sjset_notsft_datapad
->s
.k
= 0x20;
2730 sappend(s
, sjset_notsft_datapad
);
2733 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2734 * set, round the length of the 802.11 header to
2735 * a multiple of 4. Do that by adding 3 and then
2736 * dividing by and multiplying by 4, which we do by
2739 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2740 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2741 sappend(s
, s_roundup
);
2742 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2745 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2748 s2
= new_stmt(cstate
, BPF_ST
);
2749 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2752 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2753 sjset_tsft_datapad
->s
.jf
= snext
;
2754 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2755 sjset_notsft_datapad
->s
.jf
= snext
;
2757 sjset_qos
->s
.jf
= snext
;
2763 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2767 /* There is an implicit dependency between the link
2768 * payload and link header since the payload computation
2769 * includes the variable part of the header. Therefore,
2770 * if nobody else has allocated a register for the link
2771 * header and we need it, do it now. */
2772 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2773 cstate
->off_linkhdr
.reg
== -1)
2774 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2777 * For link-layer types that have a variable-length header
2778 * preceding the link-layer header, generate code to load
2779 * the offset of the link-layer header into the register
2780 * assigned to that offset, if any.
2782 * XXX - this, and the next switch statement, won't handle
2783 * encapsulation of 802.11 or 802.11+radio information in
2784 * some other protocol stack. That's significantly more
2787 switch (cstate
->outermostlinktype
) {
2789 case DLT_PRISM_HEADER
:
2790 s
= gen_load_prism_llprefixlen(cstate
);
2793 case DLT_IEEE802_11_RADIO_AVS
:
2794 s
= gen_load_avs_llprefixlen(cstate
);
2797 case DLT_IEEE802_11_RADIO
:
2798 s
= gen_load_radiotap_llprefixlen(cstate
);
2802 s
= gen_load_ppi_llprefixlen(cstate
);
2811 * For link-layer types that have a variable-length link-layer
2812 * header, generate code to load the offset of the link-layer
2813 * payload into the register assigned to that offset, if any.
2815 switch (cstate
->outermostlinktype
) {
2817 case DLT_IEEE802_11
:
2818 case DLT_PRISM_HEADER
:
2819 case DLT_IEEE802_11_RADIO_AVS
:
2820 case DLT_IEEE802_11_RADIO
:
2822 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2827 * If there there is no initialization yet and we need variable
2828 * length offsets for VLAN, initialize them to zero
2830 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2833 if (cstate
->off_linkpl
.reg
== -1)
2834 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2835 if (cstate
->off_linktype
.reg
== -1)
2836 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2838 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2840 s2
= new_stmt(cstate
, BPF_ST
);
2841 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2843 s2
= new_stmt(cstate
, BPF_ST
);
2844 s2
->s
.k
= cstate
->off_linktype
.reg
;
2849 * If we have any offset-loading code, append all the
2850 * existing statements in the block to those statements,
2851 * and make the resulting list the list of statements
2855 sappend(s
, b
->stmts
);
2860 static struct block
*
2861 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2863 struct slist
*s_load_dlt
;
2866 if (cstate
->linktype
== DLT_PPI
)
2868 /* Create the statements that check for the DLT
2870 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2871 s_load_dlt
->s
.k
= 4;
2873 b
= new_block(cstate
, JMP(BPF_JEQ
));
2875 b
->stmts
= s_load_dlt
;
2876 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2887 * Take an absolute offset, and:
2889 * if it has no variable part, return NULL;
2891 * if it has a variable part, generate code to load the register
2892 * containing that variable part into the X register, returning
2893 * a pointer to that code - if no register for that offset has
2894 * been allocated, allocate it first.
2896 * (The code to set that register will be generated later, but will
2897 * be placed earlier in the code sequence.)
2899 static struct slist
*
2900 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2904 if (off
->is_variable
) {
2905 if (off
->reg
== -1) {
2907 * We haven't yet assigned a register for the
2908 * variable part of the offset of the link-layer
2909 * header; allocate one.
2911 off
->reg
= alloc_reg(cstate
);
2915 * Load the register containing the variable part of the
2916 * offset of the link-layer header into the X register.
2918 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2923 * That offset isn't variable, there's no variable part,
2924 * so we don't need to generate any code.
2931 * Map an Ethernet type to the equivalent PPP type.
2934 ethertype_to_ppptype(int proto
)
2942 case ETHERTYPE_IPV6
:
2950 case ETHERTYPE_ATALK
:
2964 * I'm assuming the "Bridging PDU"s that go
2965 * over PPP are Spanning Tree Protocol
2979 * Generate any tests that, for encapsulation of a link-layer packet
2980 * inside another protocol stack, need to be done to check for those
2981 * link-layer packets (and that haven't already been done by a check
2982 * for that encapsulation).
2984 static struct block
*
2985 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
2989 if (cstate
->is_geneve
)
2990 return gen_geneve_ll_check(cstate
);
2992 switch (cstate
->prevlinktype
) {
2996 * This is LANE-encapsulated Ethernet; check that the LANE
2997 * packet doesn't begin with an LE Control marker, i.e.
2998 * that it's data, not a control message.
3000 * (We've already generated a test for LANE.)
3002 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3008 * No such tests are necessary.
3016 * The three different values we should check for when checking for an
3017 * IPv6 packet with DLT_NULL.
3019 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3020 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3021 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3024 * Generate code to match a particular packet type by matching the
3025 * link-layer type field or fields in the 802.2 LLC header.
3027 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3028 * value, if <= ETHERMTU.
3030 static struct block
*
3031 gen_linktype(compiler_state_t
*cstate
, int proto
)
3033 struct block
*b0
, *b1
, *b2
;
3034 const char *description
;
3036 /* are we checking MPLS-encapsulated packets? */
3037 if (cstate
->label_stack_depth
> 0) {
3041 /* FIXME add other L3 proto IDs */
3042 return gen_mpls_linktype(cstate
, Q_IP
);
3044 case ETHERTYPE_IPV6
:
3046 /* FIXME add other L3 proto IDs */
3047 return gen_mpls_linktype(cstate
, Q_IPV6
);
3050 bpf_error(cstate
, "unsupported protocol over mpls");
3055 switch (cstate
->linktype
) {
3058 case DLT_NETANALYZER
:
3059 case DLT_NETANALYZER_TRANSPARENT
:
3060 /* Geneve has an EtherType regardless of whether there is an
3062 if (!cstate
->is_geneve
)
3063 b0
= gen_prevlinkhdr_check(cstate
);
3067 b1
= gen_ether_linktype(cstate
, proto
);
3078 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3082 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3088 case DLT_IEEE802_11
:
3089 case DLT_PRISM_HEADER
:
3090 case DLT_IEEE802_11_RADIO_AVS
:
3091 case DLT_IEEE802_11_RADIO
:
3094 * Check that we have a data frame.
3096 b0
= gen_check_802_11_data_frame(cstate
);
3099 * Now check for the specified link-layer type.
3101 b1
= gen_llc_linktype(cstate
, proto
);
3109 * XXX - check for LLC frames.
3111 return gen_llc_linktype(cstate
, proto
);
3117 * XXX - check for LLC PDUs, as per IEEE 802.5.
3119 return gen_llc_linktype(cstate
, proto
);
3123 case DLT_ATM_RFC1483
:
3125 case DLT_IP_OVER_FC
:
3126 return gen_llc_linktype(cstate
, proto
);
3132 * Check for an LLC-encapsulated version of this protocol;
3133 * if we were checking for LANE, linktype would no longer
3136 * Check for LLC encapsulation and then check the protocol.
3138 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3139 b1
= gen_llc_linktype(cstate
, proto
);
3146 return gen_linux_sll_linktype(cstate
, proto
);
3151 case DLT_SLIP_BSDOS
:
3154 * These types don't provide any type field; packets
3155 * are always IPv4 or IPv6.
3157 * XXX - for IPv4, check for a version number of 4, and,
3158 * for IPv6, check for a version number of 6?
3163 /* Check for a version number of 4. */
3164 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3166 case ETHERTYPE_IPV6
:
3167 /* Check for a version number of 6. */
3168 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3171 return gen_false(cstate
); /* always false */
3178 * Raw IPv4, so no type field.
3180 if (proto
== ETHERTYPE_IP
)
3181 return gen_true(cstate
); /* always true */
3183 /* Checking for something other than IPv4; always false */
3184 return gen_false(cstate
);
3190 * Raw IPv6, so no type field.
3192 if (proto
== ETHERTYPE_IPV6
)
3193 return gen_true(cstate
); /* always true */
3195 /* Checking for something other than IPv6; always false */
3196 return gen_false(cstate
);
3202 case DLT_PPP_SERIAL
:
3205 * We use Ethernet protocol types inside libpcap;
3206 * map them to the corresponding PPP protocol types.
3208 proto
= ethertype_to_ppptype(proto
);
3209 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3215 * We use Ethernet protocol types inside libpcap;
3216 * map them to the corresponding PPP protocol types.
3222 * Also check for Van Jacobson-compressed IP.
3223 * XXX - do this for other forms of PPP?
3225 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3226 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3228 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3233 proto
= ethertype_to_ppptype(proto
);
3234 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3246 return (gen_loopback_linktype(cstate
, AF_INET
));
3248 case ETHERTYPE_IPV6
:
3250 * AF_ values may, unfortunately, be platform-
3251 * dependent; AF_INET isn't, because everybody
3252 * used 4.2BSD's value, but AF_INET6 is, because
3253 * 4.2BSD didn't have a value for it (given that
3254 * IPv6 didn't exist back in the early 1980's),
3255 * and they all picked their own values.
3257 * This means that, if we're reading from a
3258 * savefile, we need to check for all the
3261 * If we're doing a live capture, we only need
3262 * to check for this platform's value; however,
3263 * Npcap uses 24, which isn't Windows's AF_INET6
3264 * value. (Given the multiple different values,
3265 * programs that read pcap files shouldn't be
3266 * checking for their platform's AF_INET6 value
3267 * anyway, they should check for all of the
3268 * possible values. and they might as well do
3269 * that even for live captures.)
3271 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3273 * Savefile - check for all three
3274 * possible IPv6 values.
3276 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3277 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3279 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3284 * Live capture, so we only need to
3285 * check for the value used on this
3290 * Npcap doesn't use Windows's AF_INET6,
3291 * as that collides with AF_IPX on
3292 * some BSDs (both have the value 23).
3293 * Instead, it uses 24.
3295 return (gen_loopback_linktype(cstate
, 24));
3298 return (gen_loopback_linktype(cstate
, AF_INET6
));
3299 #else /* AF_INET6 */
3301 * I guess this platform doesn't support
3302 * IPv6, so we just reject all packets.
3304 return gen_false(cstate
);
3305 #endif /* AF_INET6 */
3311 * Not a type on which we support filtering.
3312 * XXX - support those that have AF_ values
3313 * #defined on this platform, at least?
3315 return gen_false(cstate
);
3318 #ifdef HAVE_NET_PFVAR_H
3321 * af field is host byte order in contrast to the rest of
3324 if (proto
== ETHERTYPE_IP
)
3325 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3326 BPF_B
, (bpf_int32
)AF_INET
));
3327 else if (proto
== ETHERTYPE_IPV6
)
3328 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3329 BPF_B
, (bpf_int32
)AF_INET6
));
3331 return gen_false(cstate
);
3334 #endif /* HAVE_NET_PFVAR_H */
3337 case DLT_ARCNET_LINUX
:
3339 * XXX should we check for first fragment if the protocol
3345 return gen_false(cstate
);
3347 case ETHERTYPE_IPV6
:
3348 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3349 (bpf_int32
)ARCTYPE_INET6
));
3352 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3353 (bpf_int32
)ARCTYPE_IP
);
3354 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3355 (bpf_int32
)ARCTYPE_IP_OLD
);
3360 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3361 (bpf_int32
)ARCTYPE_ARP
);
3362 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3363 (bpf_int32
)ARCTYPE_ARP_OLD
);
3367 case ETHERTYPE_REVARP
:
3368 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3369 (bpf_int32
)ARCTYPE_REVARP
));
3371 case ETHERTYPE_ATALK
:
3372 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3373 (bpf_int32
)ARCTYPE_ATALK
));
3380 case ETHERTYPE_ATALK
:
3381 return gen_true(cstate
);
3383 return gen_false(cstate
);
3390 * XXX - assumes a 2-byte Frame Relay header with
3391 * DLCI and flags. What if the address is longer?
3397 * Check for the special NLPID for IP.
3399 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3401 case ETHERTYPE_IPV6
:
3403 * Check for the special NLPID for IPv6.
3405 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3409 * Check for several OSI protocols.
3411 * Frame Relay packets typically have an OSI
3412 * NLPID at the beginning; we check for each
3415 * What we check for is the NLPID and a frame
3416 * control field of UI, i.e. 0x03 followed
3419 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3420 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3421 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3427 return gen_false(cstate
);
3433 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3435 case DLT_JUNIPER_MFR
:
3436 case DLT_JUNIPER_MLFR
:
3437 case DLT_JUNIPER_MLPPP
:
3438 case DLT_JUNIPER_ATM1
:
3439 case DLT_JUNIPER_ATM2
:
3440 case DLT_JUNIPER_PPPOE
:
3441 case DLT_JUNIPER_PPPOE_ATM
:
3442 case DLT_JUNIPER_GGSN
:
3443 case DLT_JUNIPER_ES
:
3444 case DLT_JUNIPER_MONITOR
:
3445 case DLT_JUNIPER_SERVICES
:
3446 case DLT_JUNIPER_ETHER
:
3447 case DLT_JUNIPER_PPP
:
3448 case DLT_JUNIPER_FRELAY
:
3449 case DLT_JUNIPER_CHDLC
:
3450 case DLT_JUNIPER_VP
:
3451 case DLT_JUNIPER_ST
:
3452 case DLT_JUNIPER_ISM
:
3453 case DLT_JUNIPER_VS
:
3454 case DLT_JUNIPER_SRX_E2E
:
3455 case DLT_JUNIPER_FIBRECHANNEL
:
3456 case DLT_JUNIPER_ATM_CEMIC
:
3458 /* just lets verify the magic number for now -
3459 * on ATM we may have up to 6 different encapsulations on the wire
3460 * and need a lot of heuristics to figure out that the payload
3463 * FIXME encapsulation specific BPF_ filters
3465 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3467 case DLT_BACNET_MS_TP
:
3468 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3471 return gen_ipnet_linktype(cstate
, proto
);
3473 case DLT_LINUX_IRDA
:
3474 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3477 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3480 case DLT_MTP2_WITH_PHDR
:
3481 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3484 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3487 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3489 case DLT_LINUX_LAPD
:
3490 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3492 case DLT_USB_FREEBSD
:
3494 case DLT_USB_LINUX_MMAPPED
:
3496 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3498 case DLT_BLUETOOTH_HCI_H4
:
3499 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3500 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3503 case DLT_CAN_SOCKETCAN
:
3504 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3506 case DLT_IEEE802_15_4
:
3507 case DLT_IEEE802_15_4_LINUX
:
3508 case DLT_IEEE802_15_4_NONASK_PHY
:
3509 case DLT_IEEE802_15_4_NOFCS
:
3510 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3512 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3513 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3516 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3519 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3522 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3525 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3528 /* Using the fixed-size NFLOG header it is possible to tell only
3529 * the address family of the packet, other meaningful data is
3530 * either missing or behind TLVs.
3532 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3536 * Does this link-layer header type have a field
3537 * indicating the type of the next protocol? If
3538 * so, off_linktype.constant_part will be the offset of that
3539 * field in the packet; if not, it will be OFFSET_NOT_SET.
3541 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3543 * Yes; assume it's an Ethernet type. (If
3544 * it's not, it needs to be handled specially
3547 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3550 * No; report an error.
3552 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3553 if (description
!= NULL
) {
3554 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3557 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3566 * Check for an LLC SNAP packet with a given organization code and
3567 * protocol type; we check the entire contents of the 802.2 LLC and
3568 * snap headers, checking for DSAP and SSAP of SNAP and a control
3569 * field of 0x03 in the LLC header, and for the specified organization
3570 * code and protocol type in the SNAP header.
3572 static struct block
*
3573 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3575 u_char snapblock
[8];
3577 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3578 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3579 snapblock
[2] = 0x03; /* control = UI */
3580 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3581 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3582 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3583 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3584 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3585 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3589 * Generate code to match frames with an LLC header.
3592 gen_llc(compiler_state_t
*cstate
)
3594 struct block
*b0
, *b1
;
3596 switch (cstate
->linktype
) {
3600 * We check for an Ethernet type field less than
3601 * 1500, which means it's an 802.3 length field.
3603 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3607 * Now check for the purported DSAP and SSAP not being
3608 * 0xFF, to rule out NetWare-over-802.3.
3610 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3617 * We check for LLC traffic.
3619 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3622 case DLT_IEEE802
: /* Token Ring */
3624 * XXX - check for LLC frames.
3626 return gen_true(cstate
);
3630 * XXX - check for LLC frames.
3632 return gen_true(cstate
);
3634 case DLT_ATM_RFC1483
:
3636 * For LLC encapsulation, these are defined to have an
3639 * For VC encapsulation, they don't, but there's no
3640 * way to check for that; the protocol used on the VC
3641 * is negotiated out of band.
3643 return gen_true(cstate
);
3645 case DLT_IEEE802_11
:
3646 case DLT_PRISM_HEADER
:
3647 case DLT_IEEE802_11_RADIO
:
3648 case DLT_IEEE802_11_RADIO_AVS
:
3651 * Check that we have a data frame.
3653 b0
= gen_check_802_11_data_frame(cstate
);
3657 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3663 gen_llc_i(compiler_state_t
*cstate
)
3665 struct block
*b0
, *b1
;
3669 * Check whether this is an LLC frame.
3671 b0
= gen_llc(cstate
);
3674 * Load the control byte and test the low-order bit; it must
3675 * be clear for I frames.
3677 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3678 b1
= new_block(cstate
, JMP(BPF_JSET
));
3687 gen_llc_s(compiler_state_t
*cstate
)
3689 struct block
*b0
, *b1
;
3692 * Check whether this is an LLC frame.
3694 b0
= gen_llc(cstate
);
3697 * Now compare the low-order 2 bit of the control byte against
3698 * the appropriate value for S frames.
3700 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3706 gen_llc_u(compiler_state_t
*cstate
)
3708 struct block
*b0
, *b1
;
3711 * Check whether this is an LLC frame.
3713 b0
= gen_llc(cstate
);
3716 * Now compare the low-order 2 bit of the control byte against
3717 * the appropriate value for U frames.
3719 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3725 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3727 struct block
*b0
, *b1
;
3730 * Check whether this is an LLC frame.
3732 b0
= gen_llc(cstate
);
3735 * Now check for an S frame with the appropriate type.
3737 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3743 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3745 struct block
*b0
, *b1
;
3748 * Check whether this is an LLC frame.
3750 b0
= gen_llc(cstate
);
3753 * Now check for a U frame with the appropriate type.
3755 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3761 * Generate code to match a particular packet type, for link-layer types
3762 * using 802.2 LLC headers.
3764 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3765 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3767 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3768 * value, if <= ETHERMTU. We use that to determine whether to
3769 * match the DSAP or both DSAP and LSAP or to check the OUI and
3770 * protocol ID in a SNAP header.
3772 static struct block
*
3773 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3776 * XXX - handle token-ring variable-length header.
3782 case LLCSAP_NETBEUI
:
3784 * XXX - should we check both the DSAP and the
3785 * SSAP, like this, or should we check just the
3786 * DSAP, as we do for other SAP values?
3788 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3789 ((proto
<< 8) | proto
));
3793 * XXX - are there ever SNAP frames for IPX on
3794 * non-Ethernet 802.x networks?
3796 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3797 (bpf_int32
)LLCSAP_IPX
);
3799 case ETHERTYPE_ATALK
:
3801 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3802 * SNAP packets with an organization code of
3803 * 0x080007 (Apple, for Appletalk) and a protocol
3804 * type of ETHERTYPE_ATALK (Appletalk).
3806 * XXX - check for an organization code of
3807 * encapsulated Ethernet as well?
3809 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3813 * XXX - we don't have to check for IPX 802.3
3814 * here, but should we check for the IPX Ethertype?
3816 if (proto
<= ETHERMTU
) {
3818 * This is an LLC SAP value, so check
3821 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3824 * This is an Ethernet type; we assume that it's
3825 * unlikely that it'll appear in the right place
3826 * at random, and therefore check only the
3827 * location that would hold the Ethernet type
3828 * in a SNAP frame with an organization code of
3829 * 0x000000 (encapsulated Ethernet).
3831 * XXX - if we were to check for the SNAP DSAP and
3832 * LSAP, as per XXX, and were also to check for an
3833 * organization code of 0x000000 (encapsulated
3834 * Ethernet), we'd do
3836 * return gen_snap(cstate, 0x000000, proto);
3838 * here; for now, we don't, as per the above.
3839 * I don't know whether it's worth the extra CPU
3840 * time to do the right check or not.
3842 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3847 static struct block
*
3848 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3849 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3851 struct block
*b0
, *b1
;
3865 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3866 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3872 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3873 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3878 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3882 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3886 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3890 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3894 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3898 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3904 b0
= gen_linktype(cstate
, proto
);
3905 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3911 static struct block
*
3912 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3913 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3915 struct block
*b0
, *b1
;
3930 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3931 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3937 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3938 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3943 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3947 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3951 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3955 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3959 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3963 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3969 /* this order is important */
3970 a
= (uint32_t *)addr
;
3971 m
= (uint32_t *)mask
;
3972 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3973 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3975 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3977 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3979 b0
= gen_linktype(cstate
, proto
);
3985 static struct block
*
3986 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3988 register struct block
*b0
, *b1
;
3992 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
3995 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
3998 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3999 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4005 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4006 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4011 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4015 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4019 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4023 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4027 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4031 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4039 * Like gen_ehostop, but for DLT_FDDI
4041 static struct block
*
4042 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4044 struct block
*b0
, *b1
;
4048 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4051 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4054 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4055 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4061 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4062 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4067 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4071 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4075 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4079 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4083 bpf_error(cstate
, "'ra' is only supported on 802.11");
4087 bpf_error(cstate
, "'ta' is only supported on 802.11");
4095 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4097 static struct block
*
4098 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4100 register struct block
*b0
, *b1
;
4104 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4107 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4110 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4111 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4117 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4118 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4123 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4127 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4131 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4135 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4139 bpf_error(cstate
, "'ra' is only supported on 802.11");
4143 bpf_error(cstate
, "'ta' is only supported on 802.11");
4151 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4152 * various 802.11 + radio headers.
4154 static struct block
*
4155 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4157 register struct block
*b0
, *b1
, *b2
;
4158 register struct slist
*s
;
4160 #ifdef ENABLE_WLAN_FILTERING_PATCH
4163 * We need to disable the optimizer because the optimizer is buggy
4164 * and wipes out some LD instructions generated by the below
4165 * code to validate the Frame Control bits
4167 cstate
->no_optimize
= 1;
4168 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4175 * For control frames, there is no SA.
4177 * For management frames, SA is at an
4178 * offset of 10 from the beginning of
4181 * For data frames, SA is at an offset
4182 * of 10 from the beginning of the packet
4183 * if From DS is clear, at an offset of
4184 * 16 from the beginning of the packet
4185 * if From DS is set and To DS is clear,
4186 * and an offset of 24 from the beginning
4187 * of the packet if From DS is set and To DS
4192 * Generate the tests to be done for data frames
4195 * First, check for To DS set, i.e. check "link[1] & 0x01".
4197 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4198 b1
= new_block(cstate
, JMP(BPF_JSET
));
4199 b1
->s
.k
= 0x01; /* To DS */
4203 * If To DS is set, the SA is at 24.
4205 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4209 * Now, check for To DS not set, i.e. check
4210 * "!(link[1] & 0x01)".
4212 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4213 b2
= new_block(cstate
, JMP(BPF_JSET
));
4214 b2
->s
.k
= 0x01; /* To DS */
4219 * If To DS is not set, the SA is at 16.
4221 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4225 * Now OR together the last two checks. That gives
4226 * the complete set of checks for data frames with
4232 * Now check for From DS being set, and AND that with
4233 * the ORed-together checks.
4235 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4236 b1
= new_block(cstate
, JMP(BPF_JSET
));
4237 b1
->s
.k
= 0x02; /* From DS */
4242 * Now check for data frames with From DS not set.
4244 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4245 b2
= new_block(cstate
, JMP(BPF_JSET
));
4246 b2
->s
.k
= 0x02; /* From DS */
4251 * If From DS isn't set, the SA is at 10.
4253 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4257 * Now OR together the checks for data frames with
4258 * From DS not set and for data frames with From DS
4259 * set; that gives the checks done for data frames.
4264 * Now check for a data frame.
4265 * I.e, check "link[0] & 0x08".
4267 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4268 b1
= new_block(cstate
, JMP(BPF_JSET
));
4273 * AND that with the checks done for data frames.
4278 * If the high-order bit of the type value is 0, this
4279 * is a management frame.
4280 * I.e, check "!(link[0] & 0x08)".
4282 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4283 b2
= new_block(cstate
, JMP(BPF_JSET
));
4289 * For management frames, the SA is at 10.
4291 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4295 * OR that with the checks done for data frames.
4296 * That gives the checks done for management and
4302 * If the low-order bit of the type value is 1,
4303 * this is either a control frame or a frame
4304 * with a reserved type, and thus not a
4307 * I.e., check "!(link[0] & 0x04)".
4309 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4310 b1
= new_block(cstate
, JMP(BPF_JSET
));
4316 * AND that with the checks for data and management
4326 * For control frames, there is no DA.
4328 * For management frames, DA is at an
4329 * offset of 4 from the beginning of
4332 * For data frames, DA is at an offset
4333 * of 4 from the beginning of the packet
4334 * if To DS is clear and at an offset of
4335 * 16 from the beginning of the packet
4340 * Generate the tests to be done for data frames.
4342 * First, check for To DS set, i.e. "link[1] & 0x01".
4344 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4345 b1
= new_block(cstate
, JMP(BPF_JSET
));
4346 b1
->s
.k
= 0x01; /* To DS */
4350 * If To DS is set, the DA is at 16.
4352 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4356 * Now, check for To DS not set, i.e. check
4357 * "!(link[1] & 0x01)".
4359 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4360 b2
= new_block(cstate
, JMP(BPF_JSET
));
4361 b2
->s
.k
= 0x01; /* To DS */
4366 * If To DS is not set, the DA is at 4.
4368 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4372 * Now OR together the last two checks. That gives
4373 * the complete set of checks for data frames.
4378 * Now check for a data frame.
4379 * I.e, check "link[0] & 0x08".
4381 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4382 b1
= new_block(cstate
, JMP(BPF_JSET
));
4387 * AND that with the checks done for data frames.
4392 * If the high-order bit of the type value is 0, this
4393 * is a management frame.
4394 * I.e, check "!(link[0] & 0x08)".
4396 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4397 b2
= new_block(cstate
, JMP(BPF_JSET
));
4403 * For management frames, the DA is at 4.
4405 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4409 * OR that with the checks done for data frames.
4410 * That gives the checks done for management and
4416 * If the low-order bit of the type value is 1,
4417 * this is either a control frame or a frame
4418 * with a reserved type, and thus not a
4421 * I.e., check "!(link[0] & 0x04)".
4423 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4424 b1
= new_block(cstate
, JMP(BPF_JSET
));
4430 * AND that with the checks for data and management
4438 * Not present in management frames; addr1 in other
4443 * If the high-order bit of the type value is 0, this
4444 * is a management frame.
4445 * I.e, check "(link[0] & 0x08)".
4447 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4448 b1
= new_block(cstate
, JMP(BPF_JSET
));
4455 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4458 * AND that with the check of addr1.
4465 * Not present in management frames; addr2, if present,
4470 * Not present in CTS or ACK control frames.
4472 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4473 IEEE80211_FC0_TYPE_MASK
);
4475 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4476 IEEE80211_FC0_SUBTYPE_MASK
);
4478 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4479 IEEE80211_FC0_SUBTYPE_MASK
);
4485 * If the high-order bit of the type value is 0, this
4486 * is a management frame.
4487 * I.e, check "(link[0] & 0x08)".
4489 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4490 b1
= new_block(cstate
, JMP(BPF_JSET
));
4495 * AND that with the check for frames other than
4496 * CTS and ACK frames.
4503 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4508 * XXX - add BSSID keyword?
4511 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4515 * Not present in CTS or ACK control frames.
4517 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4518 IEEE80211_FC0_TYPE_MASK
);
4520 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4521 IEEE80211_FC0_SUBTYPE_MASK
);
4523 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4524 IEEE80211_FC0_SUBTYPE_MASK
);
4528 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4534 * Not present in control frames.
4536 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4537 IEEE80211_FC0_TYPE_MASK
);
4539 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4545 * Present only if the direction mask has both "From DS"
4546 * and "To DS" set. Neither control frames nor management
4547 * frames should have both of those set, so we don't
4548 * check the frame type.
4550 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4551 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4552 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4557 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4558 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4564 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4565 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4574 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4575 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4576 * as the RFC states.)
4578 static struct block
*
4579 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4581 register struct block
*b0
, *b1
;
4585 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4588 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4591 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4592 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4598 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4599 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4604 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4608 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4612 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4616 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4620 bpf_error(cstate
, "'ra' is only supported on 802.11");
4624 bpf_error(cstate
, "'ta' is only supported on 802.11");
4632 * This is quite tricky because there may be pad bytes in front of the
4633 * DECNET header, and then there are two possible data packet formats that
4634 * carry both src and dst addresses, plus 5 packet types in a format that
4635 * carries only the src node, plus 2 types that use a different format and
4636 * also carry just the src node.
4640 * Instead of doing those all right, we just look for data packets with
4641 * 0 or 1 bytes of padding. If you want to look at other packets, that
4642 * will require a lot more hacking.
4644 * To add support for filtering on DECNET "areas" (network numbers)
4645 * one would want to add a "mask" argument to this routine. That would
4646 * make the filter even more inefficient, although one could be clever
4647 * and not generate masking instructions if the mask is 0xFFFF.
4649 static struct block
*
4650 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4652 struct block
*b0
, *b1
, *b2
, *tmp
;
4653 u_int offset_lh
; /* offset if long header is received */
4654 u_int offset_sh
; /* offset if short header is received */
4659 offset_sh
= 1; /* follows flags */
4660 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4664 offset_sh
= 3; /* follows flags, dstnode */
4665 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4669 /* Inefficient because we do our Calvinball dance twice */
4670 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4671 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4677 /* Inefficient because we do our Calvinball dance twice */
4678 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4679 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4684 bpf_error(cstate
, "ISO host filtering not implemented");
4689 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4690 /* Check for pad = 1, long header case */
4691 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4692 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4693 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4694 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4696 /* Check for pad = 0, long header case */
4697 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4698 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4701 /* Check for pad = 1, short header case */
4702 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4703 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4704 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4707 /* Check for pad = 0, short header case */
4708 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4709 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4713 /* Combine with test for cstate->linktype */
4719 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4720 * test the bottom-of-stack bit, and then check the version number
4721 * field in the IP header.
4723 static struct block
*
4724 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4726 struct block
*b0
, *b1
;
4731 /* match the bottom-of-stack bit */
4732 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4733 /* match the IPv4 version number */
4734 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4739 /* match the bottom-of-stack bit */
4740 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4741 /* match the IPv4 version number */
4742 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4751 static struct block
*
4752 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4753 int proto
, int dir
, int type
)
4755 struct block
*b0
, *b1
;
4756 const char *typestr
;
4766 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4768 * Only check for non-IPv4 addresses if we're not
4769 * checking MPLS-encapsulated packets.
4771 if (cstate
->label_stack_depth
== 0) {
4772 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4774 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4780 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4783 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4786 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4789 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4792 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4795 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4798 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4801 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4804 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4807 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4810 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4813 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4816 bpf_error(cstate
, "ATALK host filtering not implemented");
4819 bpf_error(cstate
, "AARP host filtering not implemented");
4822 return gen_dnhostop(cstate
, addr
, dir
);
4825 bpf_error(cstate
, "SCA host filtering not implemented");
4828 bpf_error(cstate
, "LAT host filtering not implemented");
4831 bpf_error(cstate
, "MOPDL host filtering not implemented");
4834 bpf_error(cstate
, "MOPRC host filtering not implemented");
4837 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4840 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4843 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4846 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4849 bpf_error(cstate
, "ISO host filtering not implemented");
4852 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4855 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4858 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4861 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4864 bpf_error(cstate
, "IPX host filtering not implemented");
4867 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4870 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4879 static struct block
*
4880 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4881 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4883 const char *typestr
;
4893 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4896 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4899 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4902 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4905 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4908 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4911 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4914 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4917 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4920 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4923 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4926 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4929 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4932 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4935 bpf_error(cstate
, "ATALK host filtering not implemented");
4938 bpf_error(cstate
, "AARP host filtering not implemented");
4941 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
4944 bpf_error(cstate
, "SCA host filtering not implemented");
4947 bpf_error(cstate
, "LAT host filtering not implemented");
4950 bpf_error(cstate
, "MOPDL host filtering not implemented");
4953 bpf_error(cstate
, "MOPRC host filtering not implemented");
4956 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4959 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4962 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4965 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4968 bpf_error(cstate
, "ISO host filtering not implemented");
4971 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4974 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4977 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4980 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4983 bpf_error(cstate
, "IPX host filtering not implemented");
4986 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4989 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4999 static struct block
*
5000 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5001 struct addrinfo
*alist
, int proto
, int dir
)
5003 struct block
*b0
, *b1
, *tmp
;
5004 struct addrinfo
*ai
;
5005 struct sockaddr_in
*sin
;
5008 bpf_error(cstate
, "direction applied to 'gateway'");
5015 switch (cstate
->linktype
) {
5017 case DLT_NETANALYZER
:
5018 case DLT_NETANALYZER_TRANSPARENT
:
5019 b1
= gen_prevlinkhdr_check(cstate
);
5020 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5025 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5028 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5030 case DLT_IEEE802_11
:
5031 case DLT_PRISM_HEADER
:
5032 case DLT_IEEE802_11_RADIO_AVS
:
5033 case DLT_IEEE802_11_RADIO
:
5035 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5039 * This is LLC-multiplexed traffic; if it were
5040 * LANE, cstate->linktype would have been set to
5044 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5046 case DLT_IP_OVER_FC
:
5047 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5051 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5054 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5056 * Does it have an address?
5058 if (ai
->ai_addr
!= NULL
) {
5060 * Yes. Is it an IPv4 address?
5062 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5064 * Generate an entry for it.
5066 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5067 tmp
= gen_host(cstate
,
5068 ntohl(sin
->sin_addr
.s_addr
),
5069 0xffffffff, proto
, Q_OR
, Q_HOST
);
5071 * Is it the *first* IPv4 address?
5075 * Yes, so start with it.
5080 * No, so OR it into the
5092 * No IPv4 addresses found.
5100 bpf_error(cstate
, "illegal modifier of 'gateway'");
5106 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5114 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5115 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5120 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5121 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5126 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5127 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5132 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5135 #ifndef IPPROTO_IGMP
5136 #define IPPROTO_IGMP 2
5140 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5143 #ifndef IPPROTO_IGRP
5144 #define IPPROTO_IGRP 9
5147 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5151 #define IPPROTO_PIM 103
5155 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5156 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5160 #ifndef IPPROTO_VRRP
5161 #define IPPROTO_VRRP 112
5165 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5168 #ifndef IPPROTO_CARP
5169 #define IPPROTO_CARP 112
5173 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5177 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5181 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5185 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5189 bpf_error(cstate
, "link layer applied in wrong context");
5192 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5196 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5200 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5204 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5208 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5212 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5216 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5220 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5223 #ifndef IPPROTO_ICMPV6
5224 #define IPPROTO_ICMPV6 58
5227 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5231 #define IPPROTO_AH 51
5234 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5235 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5240 #define IPPROTO_ESP 50
5243 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5244 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5249 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5253 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5257 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5260 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5261 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5262 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5264 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5266 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5268 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5272 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5273 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5274 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5276 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5278 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5280 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5284 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5285 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5286 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5288 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5293 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5294 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5299 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5300 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5302 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5304 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5309 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5310 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5315 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5316 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5321 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5325 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5329 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5333 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5337 bpf_error(cstate
, "'radio' is not a valid protocol type");
5345 static struct block
*
5346 gen_ipfrag(compiler_state_t
*cstate
)
5351 /* not IPv4 frag other than the first frag */
5352 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5353 b
= new_block(cstate
, JMP(BPF_JSET
));
5362 * Generate a comparison to a port value in the transport-layer header
5363 * at the specified offset from the beginning of that header.
5365 * XXX - this handles a variable-length prefix preceding the link-layer
5366 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5367 * variable-length link-layer headers (such as Token Ring or 802.11
5370 static struct block
*
5371 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5373 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5376 static struct block
*
5377 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5379 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5383 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5385 struct block
*b0
, *b1
, *tmp
;
5387 /* ip proto 'proto' and not a fragment other than the first fragment */
5388 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5389 b0
= gen_ipfrag(cstate
);
5394 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5398 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5403 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5404 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5409 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5410 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5422 static struct block
*
5423 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5425 struct block
*b0
, *b1
, *tmp
;
5430 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5431 * not LLC encapsulation with LLCSAP_IP.
5433 * For IEEE 802 networks - which includes 802.5 token ring
5434 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5435 * says that SNAP encapsulation is used, not LLC encapsulation
5438 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5439 * RFC 2225 say that SNAP encapsulation is used, not LLC
5440 * encapsulation with LLCSAP_IP.
5442 * So we always check for ETHERTYPE_IP.
5444 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5450 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5454 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5455 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5457 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5469 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5471 struct block
*b0
, *b1
, *tmp
;
5473 /* ip6 proto 'proto' */
5474 /* XXX - catch the first fragment of a fragmented packet? */
5475 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5479 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5483 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5488 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5489 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5494 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5495 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5507 static struct block
*
5508 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5510 struct block
*b0
, *b1
, *tmp
;
5512 /* link proto ip6 */
5513 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5519 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5523 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5524 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5526 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5537 /* gen_portrange code */
5538 static struct block
*
5539 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5542 struct block
*b1
, *b2
;
5546 * Reverse the order of the ports, so v1 is the lower one.
5555 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5556 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5564 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5567 struct block
*b0
, *b1
, *tmp
;
5569 /* ip proto 'proto' and not a fragment other than the first fragment */
5570 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5571 b0
= gen_ipfrag(cstate
);
5576 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5580 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5585 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5586 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5591 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5592 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5604 static struct block
*
5605 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5608 struct block
*b0
, *b1
, *tmp
;
5611 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5617 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5621 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5622 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5624 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5635 static struct block
*
5636 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5639 struct block
*b1
, *b2
;
5643 * Reverse the order of the ports, so v1 is the lower one.
5652 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5653 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5661 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5664 struct block
*b0
, *b1
, *tmp
;
5666 /* ip6 proto 'proto' */
5667 /* XXX - catch the first fragment of a fragmented packet? */
5668 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5672 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5676 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5681 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5682 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5687 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5688 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5700 static struct block
*
5701 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5704 struct block
*b0
, *b1
, *tmp
;
5706 /* link proto ip6 */
5707 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5713 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5717 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5718 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5720 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5732 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5741 v
= pcap_nametoproto(name
);
5742 if (v
== PROTO_UNDEF
)
5743 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5747 /* XXX should look up h/w protocol type based on cstate->linktype */
5748 v
= pcap_nametoeproto(name
);
5749 if (v
== PROTO_UNDEF
) {
5750 v
= pcap_nametollc(name
);
5751 if (v
== PROTO_UNDEF
)
5752 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5757 if (strcmp(name
, "esis") == 0)
5759 else if (strcmp(name
, "isis") == 0)
5761 else if (strcmp(name
, "clnp") == 0)
5764 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5776 gen_joinsp(struct stmt
**s
, int n
)
5782 static struct block
*
5783 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5785 #ifdef NO_PROTOCHAIN
5786 return gen_proto(cstate
, v
, proto
, dir
);
5788 struct block
*b0
, *b
;
5789 struct slist
*s
[100];
5790 int fix2
, fix3
, fix4
, fix5
;
5791 int ahcheck
, again
, end
;
5793 int reg2
= alloc_reg(cstate
);
5795 memset(s
, 0, sizeof(s
));
5796 fix3
= fix4
= fix5
= 0;
5803 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5804 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5808 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5813 * We don't handle variable-length prefixes before the link-layer
5814 * header, or variable-length link-layer headers, here yet.
5815 * We might want to add BPF instructions to do the protochain
5816 * work, to simplify that and, on platforms that have a BPF
5817 * interpreter with the new instructions, let the filtering
5818 * be done in the kernel. (We already require a modified BPF
5819 * engine to do the protochain stuff, to support backward
5820 * branches, and backward branch support is unlikely to appear
5821 * in kernel BPF engines.)
5823 if (cstate
->off_linkpl
.is_variable
)
5824 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5826 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
5829 * s[0] is a dummy entry to protect other BPF insn from damage
5830 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5831 * hard to find interdependency made by jump table fixup.
5834 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5839 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5842 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5843 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5845 /* X = ip->ip_hl << 2 */
5846 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5847 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5852 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5854 /* A = ip6->ip_nxt */
5855 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5856 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
5858 /* X = sizeof(struct ip6_hdr) */
5859 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
5865 bpf_error(cstate
, "unsupported proto to gen_protochain");
5869 /* again: if (A == v) goto end; else fall through; */
5871 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5873 s
[i
]->s
.jt
= NULL
; /*later*/
5874 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5878 #ifndef IPPROTO_NONE
5879 #define IPPROTO_NONE 59
5881 /* if (A == IPPROTO_NONE) goto end */
5882 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5883 s
[i
]->s
.jt
= NULL
; /*later*/
5884 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5885 s
[i
]->s
.k
= IPPROTO_NONE
;
5886 s
[fix5
]->s
.jf
= s
[i
];
5890 if (proto
== Q_IPV6
) {
5891 int v6start
, v6end
, v6advance
, j
;
5894 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5895 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5896 s
[i
]->s
.jt
= NULL
; /*later*/
5897 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5898 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5899 s
[fix2
]->s
.jf
= s
[i
];
5901 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5902 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5903 s
[i
]->s
.jt
= NULL
; /*later*/
5904 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5905 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5907 /* if (A == IPPROTO_ROUTING) goto v6advance */
5908 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5909 s
[i
]->s
.jt
= NULL
; /*later*/
5910 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5911 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5913 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5914 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5915 s
[i
]->s
.jt
= NULL
; /*later*/
5916 s
[i
]->s
.jf
= NULL
; /*later*/
5917 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5927 * A = P[X + packet head];
5928 * X = X + (P[X + packet head + 1] + 1) * 8;
5930 /* A = P[X + packet head] */
5931 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5932 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5935 s
[i
] = new_stmt(cstate
, BPF_ST
);
5938 /* A = P[X + packet head + 1]; */
5939 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5940 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
5943 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5947 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5951 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
5955 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5958 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5962 /* goto again; (must use BPF_JA for backward jump) */
5963 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5964 s
[i
]->s
.k
= again
- i
- 1;
5965 s
[i
- 1]->s
.jf
= s
[i
];
5969 for (j
= v6start
; j
<= v6end
; j
++)
5970 s
[j
]->s
.jt
= s
[v6advance
];
5973 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5975 s
[fix2
]->s
.jf
= s
[i
];
5981 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5982 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5983 s
[i
]->s
.jt
= NULL
; /*later*/
5984 s
[i
]->s
.jf
= NULL
; /*later*/
5985 s
[i
]->s
.k
= IPPROTO_AH
;
5987 s
[fix3
]->s
.jf
= s
[ahcheck
];
5994 * X = X + (P[X + 1] + 2) * 4;
5997 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5999 /* A = P[X + packet head]; */
6000 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6001 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6004 s
[i
] = new_stmt(cstate
, BPF_ST
);
6008 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6011 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6015 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6017 /* A = P[X + packet head] */
6018 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6019 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6022 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6026 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6030 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6033 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6037 /* goto again; (must use BPF_JA for backward jump) */
6038 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6039 s
[i
]->s
.k
= again
- i
- 1;
6044 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6046 s
[fix2
]->s
.jt
= s
[end
];
6047 s
[fix4
]->s
.jf
= s
[end
];
6048 s
[fix5
]->s
.jt
= s
[end
];
6055 for (i
= 0; i
< max
- 1; i
++)
6056 s
[i
]->next
= s
[i
+ 1];
6057 s
[max
- 1]->next
= NULL
;
6062 b
= new_block(cstate
, JMP(BPF_JEQ
));
6063 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6066 free_reg(cstate
, reg2
);
6073 static struct block
*
6074 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6077 struct block
*b0
, *b1
;
6080 * A data frame has the 0x08 bit (b3) in the frame control field set
6081 * and the 0x04 bit (b2) clear.
6083 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6084 b0
= new_block(cstate
, JMP(BPF_JSET
));
6088 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6089 b1
= new_block(cstate
, JMP(BPF_JSET
));
6100 * Generate code that checks whether the packet is a packet for protocol
6101 * <proto> and whether the type field in that protocol's header has
6102 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6103 * IP packet and checks the protocol number in the IP header against <v>.
6105 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6106 * against Q_IP and Q_IPV6.
6108 static struct block
*
6109 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6111 struct block
*b0
, *b1
;
6116 if (dir
!= Q_DEFAULT
)
6117 bpf_error(cstate
, "direction applied to 'proto'");
6121 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6122 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6128 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6129 * not LLC encapsulation with LLCSAP_IP.
6131 * For IEEE 802 networks - which includes 802.5 token ring
6132 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6133 * says that SNAP encapsulation is used, not LLC encapsulation
6136 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6137 * RFC 2225 say that SNAP encapsulation is used, not LLC
6138 * encapsulation with LLCSAP_IP.
6140 * So we always check for ETHERTYPE_IP.
6142 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6144 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6146 b1
= gen_protochain(cstate
, v
, Q_IP
);
6152 switch (cstate
->linktype
) {
6156 * Frame Relay packets typically have an OSI
6157 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6158 * generates code to check for all the OSI
6159 * NLPIDs, so calling it and then adding a check
6160 * for the particular NLPID for which we're
6161 * looking is bogus, as we can just check for
6164 * What we check for is the NLPID and a frame
6165 * control field value of UI, i.e. 0x03 followed
6168 * XXX - assumes a 2-byte Frame Relay header with
6169 * DLCI and flags. What if the address is longer?
6171 * XXX - what about SNAP-encapsulated frames?
6173 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6179 * Cisco uses an Ethertype lookalike - for OSI,
6182 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6183 /* OSI in C-HDLC is stuffed with a fudge byte */
6184 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (bpf_int32
)v
);
6189 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6190 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (bpf_int32
)v
);
6196 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6198 * 4 is the offset of the PDU type relative to the IS-IS
6201 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (bpf_int32
)v
);
6206 bpf_error(cstate
, "arp does not encapsulate another protocol");
6210 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6214 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6218 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6222 bpf_error(cstate
, "sca does not encapsulate another protocol");
6226 bpf_error(cstate
, "lat does not encapsulate another protocol");
6230 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6234 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6238 return gen_linktype(cstate
, v
);
6241 bpf_error(cstate
, "'udp proto' is bogus");
6245 bpf_error(cstate
, "'tcp proto' is bogus");
6249 bpf_error(cstate
, "'sctp proto' is bogus");
6253 bpf_error(cstate
, "'icmp proto' is bogus");
6257 bpf_error(cstate
, "'igmp proto' is bogus");
6261 bpf_error(cstate
, "'igrp proto' is bogus");
6265 bpf_error(cstate
, "'pim proto' is bogus");
6269 bpf_error(cstate
, "'vrrp proto' is bogus");
6273 bpf_error(cstate
, "'carp proto' is bogus");
6277 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6280 * Also check for a fragment header before the final
6283 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6284 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6286 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6289 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6295 bpf_error(cstate
, "'icmp6 proto' is bogus");
6298 bpf_error(cstate
, "'ah proto' is bogus");
6301 bpf_error(cstate
, "'ah proto' is bogus");
6304 bpf_error(cstate
, "'stp proto' is bogus");
6307 bpf_error(cstate
, "'ipx proto' is bogus");
6310 bpf_error(cstate
, "'netbeui proto' is bogus");
6313 bpf_error(cstate
, "'radio proto' is bogus");
6323 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6325 int proto
= q
.proto
;
6329 bpf_u_int32 mask
, addr
;
6330 struct addrinfo
*res
, *res0
;
6331 struct sockaddr_in
*sin4
;
6334 struct sockaddr_in6
*sin6
;
6335 struct in6_addr mask128
;
6337 struct block
*b
, *tmp
;
6338 int port
, real_proto
;
6344 addr
= pcap_nametonetaddr(name
);
6346 bpf_error(cstate
, "unknown network '%s'", name
);
6347 /* Left justify network addr and calculate its network mask */
6349 while (addr
&& (addr
& 0xff000000) == 0) {
6353 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6357 if (proto
== Q_LINK
) {
6358 switch (cstate
->linktype
) {
6361 case DLT_NETANALYZER
:
6362 case DLT_NETANALYZER_TRANSPARENT
:
6363 eaddr
= pcap_ether_hostton(name
);
6366 "unknown ether host '%s'", name
);
6367 tmp
= gen_prevlinkhdr_check(cstate
);
6368 b
= gen_ehostop(cstate
, eaddr
, dir
);
6375 eaddr
= pcap_ether_hostton(name
);
6378 "unknown FDDI host '%s'", name
);
6379 b
= gen_fhostop(cstate
, eaddr
, dir
);
6384 eaddr
= pcap_ether_hostton(name
);
6387 "unknown token ring host '%s'", name
);
6388 b
= gen_thostop(cstate
, eaddr
, dir
);
6392 case DLT_IEEE802_11
:
6393 case DLT_PRISM_HEADER
:
6394 case DLT_IEEE802_11_RADIO_AVS
:
6395 case DLT_IEEE802_11_RADIO
:
6397 eaddr
= pcap_ether_hostton(name
);
6400 "unknown 802.11 host '%s'", name
);
6401 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6405 case DLT_IP_OVER_FC
:
6406 eaddr
= pcap_ether_hostton(name
);
6409 "unknown Fibre Channel host '%s'", name
);
6410 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6415 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6416 } else if (proto
== Q_DECNET
) {
6417 unsigned short dn_addr
;
6419 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6421 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6423 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6428 * I don't think DECNET hosts can be multihomed, so
6429 * there is no need to build up a list of addresses
6431 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6434 memset(&mask128
, 0xff, sizeof(mask128
));
6436 res0
= res
= pcap_nametoaddrinfo(name
);
6438 bpf_error(cstate
, "unknown host '%s'", name
);
6445 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6446 tproto
== Q_DEFAULT
) {
6452 for (res
= res0
; res
; res
= res
->ai_next
) {
6453 switch (res
->ai_family
) {
6456 if (tproto
== Q_IPV6
)
6460 sin4
= (struct sockaddr_in
*)
6462 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6463 0xffffffff, tproto
, dir
, q
.addr
);
6467 if (tproto6
== Q_IP
)
6470 sin6
= (struct sockaddr_in6
*)
6472 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6473 &mask128
, tproto6
, dir
, q
.addr
);
6486 bpf_error(cstate
, "unknown host '%s'%s", name
,
6487 (proto
== Q_DEFAULT
)
6489 : " for specified address family");
6495 if (proto
!= Q_DEFAULT
&&
6496 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6497 bpf_error(cstate
, "illegal qualifier of 'port'");
6498 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6499 bpf_error(cstate
, "unknown port '%s'", name
);
6500 if (proto
== Q_UDP
) {
6501 if (real_proto
== IPPROTO_TCP
)
6502 bpf_error(cstate
, "port '%s' is tcp", name
);
6503 else if (real_proto
== IPPROTO_SCTP
)
6504 bpf_error(cstate
, "port '%s' is sctp", name
);
6506 /* override PROTO_UNDEF */
6507 real_proto
= IPPROTO_UDP
;
6509 if (proto
== Q_TCP
) {
6510 if (real_proto
== IPPROTO_UDP
)
6511 bpf_error(cstate
, "port '%s' is udp", name
);
6513 else if (real_proto
== IPPROTO_SCTP
)
6514 bpf_error(cstate
, "port '%s' is sctp", name
);
6516 /* override PROTO_UNDEF */
6517 real_proto
= IPPROTO_TCP
;
6519 if (proto
== Q_SCTP
) {
6520 if (real_proto
== IPPROTO_UDP
)
6521 bpf_error(cstate
, "port '%s' is udp", name
);
6523 else if (real_proto
== IPPROTO_TCP
)
6524 bpf_error(cstate
, "port '%s' is tcp", name
);
6526 /* override PROTO_UNDEF */
6527 real_proto
= IPPROTO_SCTP
;
6530 bpf_error(cstate
, "illegal port number %d < 0", port
);
6532 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6533 b
= gen_port(cstate
, port
, real_proto
, dir
);
6534 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6538 if (proto
!= Q_DEFAULT
&&
6539 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6540 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6541 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6542 bpf_error(cstate
, "unknown port in range '%s'", name
);
6543 if (proto
== Q_UDP
) {
6544 if (real_proto
== IPPROTO_TCP
)
6545 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6546 else if (real_proto
== IPPROTO_SCTP
)
6547 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6549 /* override PROTO_UNDEF */
6550 real_proto
= IPPROTO_UDP
;
6552 if (proto
== Q_TCP
) {
6553 if (real_proto
== IPPROTO_UDP
)
6554 bpf_error(cstate
, "port in range '%s' is udp", name
);
6555 else if (real_proto
== IPPROTO_SCTP
)
6556 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6558 /* override PROTO_UNDEF */
6559 real_proto
= IPPROTO_TCP
;
6561 if (proto
== Q_SCTP
) {
6562 if (real_proto
== IPPROTO_UDP
)
6563 bpf_error(cstate
, "port in range '%s' is udp", name
);
6564 else if (real_proto
== IPPROTO_TCP
)
6565 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6567 /* override PROTO_UNDEF */
6568 real_proto
= IPPROTO_SCTP
;
6571 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6573 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6575 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6577 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6579 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6580 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6585 eaddr
= pcap_ether_hostton(name
);
6587 bpf_error(cstate
, "unknown ether host: %s", name
);
6589 res
= pcap_nametoaddrinfo(name
);
6592 bpf_error(cstate
, "unknown host '%s'", name
);
6593 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6597 bpf_error(cstate
, "unknown host '%s'", name
);
6600 bpf_error(cstate
, "'gateway' not supported in this configuration");
6604 real_proto
= lookup_proto(cstate
, name
, proto
);
6605 if (real_proto
>= 0)
6606 return gen_proto(cstate
, real_proto
, proto
, dir
);
6608 bpf_error(cstate
, "unknown protocol: %s", name
);
6611 real_proto
= lookup_proto(cstate
, name
, proto
);
6612 if (real_proto
>= 0)
6613 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6615 bpf_error(cstate
, "unknown protocol: %s", name
);
6626 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6627 unsigned int masklen
, struct qual q
)
6629 register int nlen
, mlen
;
6632 nlen
= __pcap_atoin(s1
, &n
);
6633 /* Promote short ipaddr */
6637 mlen
= __pcap_atoin(s2
, &m
);
6638 /* Promote short ipaddr */
6641 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6644 /* Convert mask len to mask */
6646 bpf_error(cstate
, "mask length must be <= 32");
6649 * X << 32 is not guaranteed by C to be 0; it's
6654 m
= 0xffffffff << (32 - masklen
);
6656 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6663 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6666 bpf_error(cstate
, "Mask syntax for networks only");
6673 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6676 int proto
= q
.proto
;
6682 else if (q
.proto
== Q_DECNET
) {
6683 vlen
= __pcap_atodn(s
, &v
);
6685 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6687 vlen
= __pcap_atoin(s
, &v
);
6694 if (proto
== Q_DECNET
)
6695 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6696 else if (proto
== Q_LINK
) {
6697 bpf_error(cstate
, "illegal link layer address");
6700 if (s
== NULL
&& q
.addr
== Q_NET
) {
6701 /* Promote short net number */
6702 while (v
&& (v
& 0xff000000) == 0) {
6707 /* Promote short ipaddr */
6709 mask
<<= 32 - vlen
;
6711 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6716 proto
= IPPROTO_UDP
;
6717 else if (proto
== Q_TCP
)
6718 proto
= IPPROTO_TCP
;
6719 else if (proto
== Q_SCTP
)
6720 proto
= IPPROTO_SCTP
;
6721 else if (proto
== Q_DEFAULT
)
6722 proto
= PROTO_UNDEF
;
6724 bpf_error(cstate
, "illegal qualifier of 'port'");
6727 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6731 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6732 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6738 proto
= IPPROTO_UDP
;
6739 else if (proto
== Q_TCP
)
6740 proto
= IPPROTO_TCP
;
6741 else if (proto
== Q_SCTP
)
6742 proto
= IPPROTO_SCTP
;
6743 else if (proto
== Q_DEFAULT
)
6744 proto
= PROTO_UNDEF
;
6746 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6749 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6753 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6754 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6759 bpf_error(cstate
, "'gateway' requires a name");
6763 return gen_proto(cstate
, (int)v
, proto
, dir
);
6766 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6781 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6782 unsigned int masklen
, struct qual q
)
6784 struct addrinfo
*res
;
6785 struct in6_addr
*addr
;
6786 struct in6_addr mask
;
6791 bpf_error(cstate
, "no mask %s supported", s2
);
6793 res
= pcap_nametoaddrinfo(s1
);
6795 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6798 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6799 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6801 if (sizeof(mask
) * 8 < masklen
)
6802 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6803 memset(&mask
, 0, sizeof(mask
));
6804 memset(&mask
, 0xff, masklen
/ 8);
6806 mask
.s6_addr
[masklen
/ 8] =
6807 (0xff << (8 - masklen
% 8)) & 0xff;
6810 a
= (uint32_t *)addr
;
6811 m
= (uint32_t *)&mask
;
6812 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6813 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6814 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6822 bpf_error(cstate
, "Mask syntax for networks only");
6826 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6832 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6839 gen_ecode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
6841 struct block
*b
, *tmp
;
6843 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6844 switch (cstate
->linktype
) {
6846 case DLT_NETANALYZER
:
6847 case DLT_NETANALYZER_TRANSPARENT
:
6848 tmp
= gen_prevlinkhdr_check(cstate
);
6849 b
= gen_ehostop(cstate
, eaddr
, (int)q
.dir
);
6854 return gen_fhostop(cstate
, eaddr
, (int)q
.dir
);
6856 return gen_thostop(cstate
, eaddr
, (int)q
.dir
);
6857 case DLT_IEEE802_11
:
6858 case DLT_PRISM_HEADER
:
6859 case DLT_IEEE802_11_RADIO_AVS
:
6860 case DLT_IEEE802_11_RADIO
:
6862 return gen_wlanhostop(cstate
, eaddr
, (int)q
.dir
);
6863 case DLT_IP_OVER_FC
:
6864 return gen_ipfchostop(cstate
, eaddr
, (int)q
.dir
);
6866 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6870 bpf_error(cstate
, "ethernet address used in non-ether expression");
6875 sappend(struct slist
*s0
, struct slist
*s1
)
6878 * This is definitely not the best way to do this, but the
6879 * lists will rarely get long.
6886 static struct slist
*
6887 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
6891 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
6896 static struct slist
*
6897 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
6901 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6907 * Modify "index" to use the value stored into its register as an
6908 * offset relative to the beginning of the header for the protocol
6909 * "proto", and allocate a register and put an item "size" bytes long
6910 * (1, 2, or 4) at that offset into that register, making it the register
6914 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
6916 struct slist
*s
, *tmp
;
6918 int regno
= alloc_reg(cstate
);
6920 free_reg(cstate
, inst
->regno
);
6924 bpf_error(cstate
, "data size must be 1, 2, or 4");
6940 bpf_error(cstate
, "unsupported index operation");
6944 * The offset is relative to the beginning of the packet
6945 * data, if we have a radio header. (If we don't, this
6948 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6949 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
6950 cstate
->linktype
!= DLT_PRISM_HEADER
)
6951 bpf_error(cstate
, "radio information not present in capture");
6954 * Load into the X register the offset computed into the
6955 * register specified by "index".
6957 s
= xfer_to_x(cstate
, inst
);
6960 * Load the item at that offset.
6962 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6964 sappend(inst
->s
, s
);
6969 * The offset is relative to the beginning of
6970 * the link-layer header.
6972 * XXX - what about ATM LANE? Should the index be
6973 * relative to the beginning of the AAL5 frame, so
6974 * that 0 refers to the beginning of the LE Control
6975 * field, or relative to the beginning of the LAN
6976 * frame, so that 0 refers, for Ethernet LANE, to
6977 * the beginning of the destination address?
6979 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
6982 * If "s" is non-null, it has code to arrange that the
6983 * X register contains the length of the prefix preceding
6984 * the link-layer header. Add to it the offset computed
6985 * into the register specified by "index", and move that
6986 * into the X register. Otherwise, just load into the X
6987 * register the offset computed into the register specified
6991 sappend(s
, xfer_to_a(cstate
, inst
));
6992 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6993 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6995 s
= xfer_to_x(cstate
, inst
);
6998 * Load the item at the sum of the offset we've put in the
6999 * X register and the offset of the start of the link
7000 * layer header (which is 0 if the radio header is
7001 * variable-length; that header length is what we put
7002 * into the X register and then added to the index).
7004 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7005 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7007 sappend(inst
->s
, s
);
7021 * The offset is relative to the beginning of
7022 * the network-layer header.
7023 * XXX - are there any cases where we want
7024 * cstate->off_nl_nosnap?
7026 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7029 * If "s" is non-null, it has code to arrange that the
7030 * X register contains the variable part of the offset
7031 * of the link-layer payload. Add to it the offset
7032 * computed into the register specified by "index",
7033 * and move that into the X register. Otherwise, just
7034 * load into the X register the offset computed into
7035 * the register specified by "index".
7038 sappend(s
, xfer_to_a(cstate
, inst
));
7039 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7040 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7042 s
= xfer_to_x(cstate
, inst
);
7045 * Load the item at the sum of the offset we've put in the
7046 * X register, the offset of the start of the network
7047 * layer header from the beginning of the link-layer
7048 * payload, and the constant part of the offset of the
7049 * start of the link-layer payload.
7051 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7052 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7054 sappend(inst
->s
, s
);
7057 * Do the computation only if the packet contains
7058 * the protocol in question.
7060 b
= gen_proto_abbrev(cstate
, proto
);
7062 gen_and(inst
->b
, b
);
7076 * The offset is relative to the beginning of
7077 * the transport-layer header.
7079 * Load the X register with the length of the IPv4 header
7080 * (plus the offset of the link-layer header, if it's
7081 * a variable-length header), in bytes.
7083 * XXX - are there any cases where we want
7084 * cstate->off_nl_nosnap?
7085 * XXX - we should, if we're built with
7086 * IPv6 support, generate code to load either
7087 * IPv4, IPv6, or both, as appropriate.
7089 s
= gen_loadx_iphdrlen(cstate
);
7092 * The X register now contains the sum of the variable
7093 * part of the offset of the link-layer payload and the
7094 * length of the network-layer header.
7096 * Load into the A register the offset relative to
7097 * the beginning of the transport layer header,
7098 * add the X register to that, move that to the
7099 * X register, and load with an offset from the
7100 * X register equal to the sum of the constant part of
7101 * the offset of the link-layer payload and the offset,
7102 * relative to the beginning of the link-layer payload,
7103 * of the network-layer header.
7105 sappend(s
, xfer_to_a(cstate
, inst
));
7106 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7107 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7108 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7109 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7110 sappend(inst
->s
, s
);
7113 * Do the computation only if the packet contains
7114 * the protocol in question - which is true only
7115 * if this is an IP datagram and is the first or
7116 * only fragment of that datagram.
7118 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7120 gen_and(inst
->b
, b
);
7121 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7126 * Do the computation only if the packet contains
7127 * the protocol in question.
7129 b
= gen_proto_abbrev(cstate
, Q_IPV6
);
7131 gen_and(inst
->b
, b
);
7136 * Check if we have an icmp6 next header
7138 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7140 gen_and(inst
->b
, b
);
7145 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7147 * If "s" is non-null, it has code to arrange that the
7148 * X register contains the variable part of the offset
7149 * of the link-layer payload. Add to it the offset
7150 * computed into the register specified by "index",
7151 * and move that into the X register. Otherwise, just
7152 * load into the X register the offset computed into
7153 * the register specified by "index".
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
);
7164 * Load the item at the sum of the offset we've put in the
7165 * X register, the offset of the start of the network
7166 * layer header from the beginning of the link-layer
7167 * payload, and the constant part of the offset of the
7168 * start of the link-layer payload.
7170 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7171 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7174 sappend(inst
->s
, s
);
7178 inst
->regno
= regno
;
7179 s
= new_stmt(cstate
, BPF_ST
);
7181 sappend(inst
->s
, s
);
7187 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7188 struct arth
*a1
, int reversed
)
7190 struct slist
*s0
, *s1
, *s2
;
7191 struct block
*b
, *tmp
;
7193 s0
= xfer_to_x(cstate
, a1
);
7194 s1
= xfer_to_a(cstate
, a0
);
7195 if (code
== BPF_JEQ
) {
7196 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7197 b
= new_block(cstate
, JMP(code
));
7201 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7207 sappend(a0
->s
, a1
->s
);
7211 free_reg(cstate
, a0
->regno
);
7212 free_reg(cstate
, a1
->regno
);
7214 /* 'and' together protocol checks */
7217 gen_and(a0
->b
, tmp
= a1
->b
);
7231 gen_loadlen(compiler_state_t
*cstate
)
7233 int regno
= alloc_reg(cstate
);
7234 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7237 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7238 s
->next
= new_stmt(cstate
, BPF_ST
);
7239 s
->next
->s
.k
= regno
;
7247 gen_loadi(compiler_state_t
*cstate
, int val
)
7253 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7255 reg
= alloc_reg(cstate
);
7257 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7259 s
->next
= new_stmt(cstate
, BPF_ST
);
7268 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7272 s
= xfer_to_a(cstate
, a
);
7274 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7277 s
= new_stmt(cstate
, BPF_ST
);
7285 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7288 struct slist
*s0
, *s1
, *s2
;
7291 * Disallow division by, or modulus by, zero; we do this here
7292 * so that it gets done even if the optimizer is disabled.
7294 if (code
== BPF_DIV
) {
7295 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7296 bpf_error(cstate
, "division by zero");
7297 } else if (code
== BPF_MOD
) {
7298 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7299 bpf_error(cstate
, "modulus by zero");
7301 s0
= xfer_to_x(cstate
, a1
);
7302 s1
= xfer_to_a(cstate
, a0
);
7303 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7308 sappend(a0
->s
, a1
->s
);
7310 free_reg(cstate
, a0
->regno
);
7311 free_reg(cstate
, a1
->regno
);
7313 s0
= new_stmt(cstate
, BPF_ST
);
7314 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7321 * Initialize the table of used registers and the current register.
7324 init_regs(compiler_state_t
*cstate
)
7327 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7331 * Return the next free register.
7334 alloc_reg(compiler_state_t
*cstate
)
7336 int n
= BPF_MEMWORDS
;
7339 if (cstate
->regused
[cstate
->curreg
])
7340 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7342 cstate
->regused
[cstate
->curreg
] = 1;
7343 return cstate
->curreg
;
7346 bpf_error(cstate
, "too many registers needed to evaluate expression");
7351 * Return a register to the table so it can
7355 free_reg(compiler_state_t
*cstate
, int n
)
7357 cstate
->regused
[n
] = 0;
7360 static struct block
*
7361 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7366 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7367 b
= new_block(cstate
, JMP(jmp
));
7375 gen_greater(compiler_state_t
*cstate
, int n
)
7377 return gen_len(cstate
, BPF_JGE
, n
);
7381 * Actually, this is less than or equal.
7384 gen_less(compiler_state_t
*cstate
, int n
)
7388 b
= gen_len(cstate
, BPF_JGT
, n
);
7395 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7396 * the beginning of the link-layer header.
7397 * XXX - that means you can't test values in the radiotap header, but
7398 * as that header is difficult if not impossible to parse generally
7399 * without a loop, that might not be a severe problem. A new keyword
7400 * "radio" could be added for that, although what you'd really want
7401 * would be a way of testing particular radio header values, which
7402 * would generate code appropriate to the radio header in question.
7405 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7415 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7418 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7422 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7426 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7430 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7434 b
= new_block(cstate
, JMP(BPF_JEQ
));
7441 static const u_char abroadcast
[] = { 0x0 };
7444 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7446 bpf_u_int32 hostmask
;
7447 struct block
*b0
, *b1
, *b2
;
7448 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7454 switch (cstate
->linktype
) {
7456 case DLT_ARCNET_LINUX
:
7457 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7459 case DLT_NETANALYZER
:
7460 case DLT_NETANALYZER_TRANSPARENT
:
7461 b1
= gen_prevlinkhdr_check(cstate
);
7462 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7467 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7469 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7470 case DLT_IEEE802_11
:
7471 case DLT_PRISM_HEADER
:
7472 case DLT_IEEE802_11_RADIO_AVS
:
7473 case DLT_IEEE802_11_RADIO
:
7475 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7476 case DLT_IP_OVER_FC
:
7477 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7479 bpf_error(cstate
, "not a broadcast link");
7485 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7486 * as an indication that we don't know the netmask, and fail
7489 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7490 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7491 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7492 hostmask
= ~cstate
->netmask
;
7493 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7494 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7495 (bpf_int32
)(~0 & hostmask
), hostmask
);
7500 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7505 * Generate code to test the low-order bit of a MAC address (that's
7506 * the bottom bit of the *first* byte).
7508 static struct block
*
7509 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7511 register struct block
*b0
;
7512 register struct slist
*s
;
7514 /* link[offset] & 1 != 0 */
7515 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7516 b0
= new_block(cstate
, JMP(BPF_JSET
));
7523 gen_multicast(compiler_state_t
*cstate
, int proto
)
7525 register struct block
*b0
, *b1
, *b2
;
7526 register struct slist
*s
;
7532 switch (cstate
->linktype
) {
7534 case DLT_ARCNET_LINUX
:
7535 /* all ARCnet multicasts use the same address */
7536 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7538 case DLT_NETANALYZER
:
7539 case DLT_NETANALYZER_TRANSPARENT
:
7540 b1
= gen_prevlinkhdr_check(cstate
);
7541 /* ether[0] & 1 != 0 */
7542 b0
= gen_mac_multicast(cstate
, 0);
7548 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7550 * XXX - was that referring to bit-order issues?
7552 /* fddi[1] & 1 != 0 */
7553 return gen_mac_multicast(cstate
, 1);
7555 /* tr[2] & 1 != 0 */
7556 return gen_mac_multicast(cstate
, 2);
7557 case DLT_IEEE802_11
:
7558 case DLT_PRISM_HEADER
:
7559 case DLT_IEEE802_11_RADIO_AVS
:
7560 case DLT_IEEE802_11_RADIO
:
7565 * For control frames, there is no DA.
7567 * For management frames, DA is at an
7568 * offset of 4 from the beginning of
7571 * For data frames, DA is at an offset
7572 * of 4 from the beginning of the packet
7573 * if To DS is clear and at an offset of
7574 * 16 from the beginning of the packet
7579 * Generate the tests to be done for data frames.
7581 * First, check for To DS set, i.e. "link[1] & 0x01".
7583 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7584 b1
= new_block(cstate
, JMP(BPF_JSET
));
7585 b1
->s
.k
= 0x01; /* To DS */
7589 * If To DS is set, the DA is at 16.
7591 b0
= gen_mac_multicast(cstate
, 16);
7595 * Now, check for To DS not set, i.e. check
7596 * "!(link[1] & 0x01)".
7598 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7599 b2
= new_block(cstate
, JMP(BPF_JSET
));
7600 b2
->s
.k
= 0x01; /* To DS */
7605 * If To DS is not set, the DA is at 4.
7607 b1
= gen_mac_multicast(cstate
, 4);
7611 * Now OR together the last two checks. That gives
7612 * the complete set of checks for data frames.
7617 * Now check for a data frame.
7618 * I.e, check "link[0] & 0x08".
7620 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7621 b1
= new_block(cstate
, JMP(BPF_JSET
));
7626 * AND that with the checks done for data frames.
7631 * If the high-order bit of the type value is 0, this
7632 * is a management frame.
7633 * I.e, check "!(link[0] & 0x08)".
7635 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7636 b2
= new_block(cstate
, JMP(BPF_JSET
));
7642 * For management frames, the DA is at 4.
7644 b1
= gen_mac_multicast(cstate
, 4);
7648 * OR that with the checks done for data frames.
7649 * That gives the checks done for management and
7655 * If the low-order bit of the type value is 1,
7656 * this is either a control frame or a frame
7657 * with a reserved type, and thus not a
7660 * I.e., check "!(link[0] & 0x04)".
7662 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7663 b1
= new_block(cstate
, JMP(BPF_JSET
));
7669 * AND that with the checks for data and management
7674 case DLT_IP_OVER_FC
:
7675 b0
= gen_mac_multicast(cstate
, 2);
7680 /* Link not known to support multicasts */
7684 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7685 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7690 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7691 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7695 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7700 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7701 * Outbound traffic is sent by this machine, while inbound traffic is
7702 * sent by a remote machine (and may include packets destined for a
7703 * unicast or multicast link-layer address we are not subscribing to).
7704 * These are the same definitions implemented by pcap_setdirection().
7705 * Capturing only unicast traffic destined for this host is probably
7706 * better accomplished using a higher-layer filter.
7709 gen_inbound(compiler_state_t
*cstate
, int dir
)
7711 register struct block
*b0
;
7714 * Only some data link types support inbound/outbound qualifiers.
7716 switch (cstate
->linktype
) {
7718 b0
= gen_relation(cstate
, BPF_JEQ
,
7719 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7720 gen_loadi(cstate
, 0),
7726 /* match outgoing packets */
7727 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7729 /* match incoming packets */
7730 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7735 /* match outgoing packets */
7736 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7738 /* to filter on inbound traffic, invert the match */
7743 case DLT_LINUX_SLL2
:
7744 /* match outgoing packets */
7745 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
7747 /* to filter on inbound traffic, invert the match */
7752 #ifdef HAVE_NET_PFVAR_H
7754 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7755 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7761 /* match outgoing packets */
7762 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7764 /* match incoming packets */
7765 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7769 case DLT_JUNIPER_MFR
:
7770 case DLT_JUNIPER_MLFR
:
7771 case DLT_JUNIPER_MLPPP
:
7772 case DLT_JUNIPER_ATM1
:
7773 case DLT_JUNIPER_ATM2
:
7774 case DLT_JUNIPER_PPPOE
:
7775 case DLT_JUNIPER_PPPOE_ATM
:
7776 case DLT_JUNIPER_GGSN
:
7777 case DLT_JUNIPER_ES
:
7778 case DLT_JUNIPER_MONITOR
:
7779 case DLT_JUNIPER_SERVICES
:
7780 case DLT_JUNIPER_ETHER
:
7781 case DLT_JUNIPER_PPP
:
7782 case DLT_JUNIPER_FRELAY
:
7783 case DLT_JUNIPER_CHDLC
:
7784 case DLT_JUNIPER_VP
:
7785 case DLT_JUNIPER_ST
:
7786 case DLT_JUNIPER_ISM
:
7787 case DLT_JUNIPER_VS
:
7788 case DLT_JUNIPER_SRX_E2E
:
7789 case DLT_JUNIPER_FIBRECHANNEL
:
7790 case DLT_JUNIPER_ATM_CEMIC
:
7792 /* juniper flags (including direction) are stored
7793 * the byte after the 3-byte magic number */
7795 /* match outgoing packets */
7796 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7798 /* match incoming packets */
7799 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7805 * If we have packet meta-data indicating a direction,
7806 * and that metadata can be checked by BPF code, check
7807 * it. Otherwise, give up, as this link-layer type has
7808 * nothing in the packet data.
7810 * Currently, the only platform where a BPF filter can
7811 * check that metadata is Linux with the in-kernel
7812 * BPF interpreter. If other packet capture mechanisms
7813 * and BPF filters also supported this, it would be
7814 * nice. It would be even better if they made that
7815 * metadata available so that we could provide it
7816 * with newer capture APIs, allowing it to be saved
7819 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7821 * This is Linux with PF_PACKET support.
7822 * If this is a *live* capture, we can look at
7823 * special meta-data in the filter expression;
7824 * if it's a savefile, we can't.
7826 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7827 /* We have a FILE *, so this is a savefile */
7828 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
7833 /* match outgoing packets */
7834 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7837 /* to filter on inbound traffic, invert the match */
7840 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7841 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
7844 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7849 #ifdef HAVE_NET_PFVAR_H
7850 /* PF firewall log matched interface */
7852 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7857 if (cstate
->linktype
!= DLT_PFLOG
) {
7858 bpf_error(cstate
, "ifname supported only on PF linktype");
7861 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7862 off
= offsetof(struct pfloghdr
, ifname
);
7863 if (strlen(ifname
) >= len
) {
7864 bpf_error(cstate
, "ifname interface names can only be %d characters",
7868 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7872 /* PF firewall log ruleset name */
7874 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7878 if (cstate
->linktype
!= DLT_PFLOG
) {
7879 bpf_error(cstate
, "ruleset supported only on PF linktype");
7883 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7884 bpf_error(cstate
, "ruleset names can only be %ld characters",
7885 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7889 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7890 strlen(ruleset
), (const u_char
*)ruleset
);
7894 /* PF firewall log rule number */
7896 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7900 if (cstate
->linktype
!= DLT_PFLOG
) {
7901 bpf_error(cstate
, "rnr supported only on PF linktype");
7905 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7910 /* PF firewall log sub-rule number */
7912 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7916 if (cstate
->linktype
!= DLT_PFLOG
) {
7917 bpf_error(cstate
, "srnr supported only on PF linktype");
7921 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7926 /* PF firewall log reason code */
7928 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7932 if (cstate
->linktype
!= DLT_PFLOG
) {
7933 bpf_error(cstate
, "reason supported only on PF linktype");
7937 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7942 /* PF firewall log action */
7944 gen_pf_action(compiler_state_t
*cstate
, int action
)
7948 if (cstate
->linktype
!= DLT_PFLOG
) {
7949 bpf_error(cstate
, "action supported only on PF linktype");
7953 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7957 #else /* !HAVE_NET_PFVAR_H */
7959 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
7961 bpf_error(cstate
, "libpcap was compiled without pf support");
7966 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
7968 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7973 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
7975 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7980 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
7982 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7987 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
7989 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7994 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
7996 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7999 #endif /* HAVE_NET_PFVAR_H */
8001 /* IEEE 802.11 wireless header */
8003 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
8007 switch (cstate
->linktype
) {
8009 case DLT_IEEE802_11
:
8010 case DLT_PRISM_HEADER
:
8011 case DLT_IEEE802_11_RADIO_AVS
:
8012 case DLT_IEEE802_11_RADIO
:
8013 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8018 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8026 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8030 switch (cstate
->linktype
) {
8032 case DLT_IEEE802_11
:
8033 case DLT_PRISM_HEADER
:
8034 case DLT_IEEE802_11_RADIO_AVS
:
8035 case DLT_IEEE802_11_RADIO
:
8039 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8043 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8044 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8050 gen_acode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
8052 switch (cstate
->linktype
) {
8055 case DLT_ARCNET_LINUX
:
8056 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8058 return (gen_ahostop(cstate
, eaddr
, (int)q
.dir
));
8060 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8066 bpf_error(cstate
, "aid supported only on ARCnet");
8071 static struct block
*
8072 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8074 register struct block
*b0
, *b1
;
8077 /* src comes first, different from Ethernet */
8079 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8082 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8085 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8086 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8092 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8093 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8098 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8102 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8106 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8110 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8114 bpf_error(cstate
, "'ra' is only supported on 802.11");
8118 bpf_error(cstate
, "'ta' is only supported on 802.11");
8125 static struct block
*
8126 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8128 struct block
*b0
, *b1
;
8130 /* check for VLAN, including QinQ */
8131 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8132 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8135 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8141 static struct block
*
8142 gen_vlan_vid_test(compiler_state_t
*cstate
, int vlan_num
)
8144 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8147 static struct block
*
8148 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8150 struct block
*b0
, *b1
;
8152 b0
= gen_vlan_tpid_test(cstate
);
8154 if (vlan_num
>= 0) {
8155 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8161 * Both payload and link header type follow the VLAN tags so that
8162 * both need to be updated.
8164 cstate
->off_linkpl
.constant_part
+= 4;
8165 cstate
->off_linktype
.constant_part
+= 4;
8170 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8171 /* add v to variable part of off */
8173 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8177 if (!off
->is_variable
)
8178 off
->is_variable
= 1;
8180 off
->reg
= alloc_reg(cstate
);
8182 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8185 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8188 s2
= new_stmt(cstate
, BPF_ST
);
8194 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8195 * and link type offsets first
8198 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8202 /* offset determined at run time, shift variable part */
8204 cstate
->is_vlan_vloffset
= 1;
8205 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8206 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8208 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8209 sappend(s
.next
, b_tpid
->head
->stmts
);
8210 b_tpid
->head
->stmts
= s
.next
;
8214 * patch block b_vid (VLAN id test) to load VID value either from packet
8215 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8218 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8220 struct slist
*s
, *s2
, *sjeq
;
8223 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8224 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8226 /* true -> next instructions, false -> beginning of b_vid */
8227 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8229 sjeq
->s
.jf
= b_vid
->stmts
;
8232 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8233 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8237 /* jump to the test in b_vid (bypass loading VID from packet data) */
8239 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8241 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8245 /* insert our statements at the beginning of b_vid */
8246 sappend(s
, b_vid
->stmts
);
8251 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8252 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8253 * tag can be either in metadata or in packet data; therefore if the
8254 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8255 * header for VLAN tag. As the decision is done at run time, we need
8256 * update variable part of the offsets
8258 static struct block
*
8259 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8261 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8264 /* generate new filter code based on extracting packet
8266 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8267 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8269 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8274 * This is tricky. We need to insert the statements updating variable
8275 * parts of offsets before the the traditional TPID and VID tests so
8276 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8277 * we do not want this update to affect those checks. That's why we
8278 * generate both test blocks first and insert the statements updating
8279 * variable parts of both offsets after that. This wouldn't work if
8280 * there already were variable length link header when entering this
8281 * function but gen_vlan_bpf_extensions() isn't called in that case.
8283 b_tpid
= gen_vlan_tpid_test(cstate
);
8285 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8287 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8291 if (vlan_num
>= 0) {
8292 gen_vlan_patch_vid_test(cstate
, b_vid
);
8302 * support IEEE 802.1Q VLAN trunk over ethernet
8305 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
8309 /* can't check for VLAN-encapsulated packets inside MPLS */
8310 if (cstate
->label_stack_depth
> 0)
8311 bpf_error(cstate
, "no VLAN match after MPLS");
8314 * Check for a VLAN packet, and then change the offsets to point
8315 * to the type and data fields within the VLAN packet. Just
8316 * increment the offsets, so that we can support a hierarchy, e.g.
8317 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8320 * XXX - this is a bit of a kludge. If we were to split the
8321 * compiler into a parser that parses an expression and
8322 * generates an expression tree, and a code generator that
8323 * takes an expression tree (which could come from our
8324 * parser or from some other parser) and generates BPF code,
8325 * we could perhaps make the offsets parameters of routines
8326 * and, in the handler for an "AND" node, pass to subnodes
8327 * other than the VLAN node the adjusted offsets.
8329 * This would mean that "vlan" would, instead of changing the
8330 * behavior of *all* tests after it, change only the behavior
8331 * of tests ANDed with it. That would change the documented
8332 * semantics of "vlan", which might break some expressions.
8333 * However, it would mean that "(vlan and ip) or ip" would check
8334 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8335 * checking only for VLAN-encapsulated IP, so that could still
8336 * be considered worth doing; it wouldn't break expressions
8337 * that are of the form "vlan and ..." or "vlan N and ...",
8338 * which I suspect are the most common expressions involving
8339 * "vlan". "vlan or ..." doesn't necessarily do what the user
8340 * would really want, now, as all the "or ..." tests would
8341 * be done assuming a VLAN, even though the "or" could be viewed
8342 * as meaning "or, if this isn't a VLAN packet...".
8344 switch (cstate
->linktype
) {
8347 case DLT_NETANALYZER
:
8348 case DLT_NETANALYZER_TRANSPARENT
:
8349 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8350 /* Verify that this is the outer part of the packet and
8351 * not encapsulated somehow. */
8352 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8353 cstate
->off_linkhdr
.constant_part
==
8354 cstate
->off_outermostlinkhdr
.constant_part
) {
8356 * Do we need special VLAN handling?
8358 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8359 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8361 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8364 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8367 case DLT_IEEE802_11
:
8368 case DLT_PRISM_HEADER
:
8369 case DLT_IEEE802_11_RADIO_AVS
:
8370 case DLT_IEEE802_11_RADIO
:
8371 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8375 bpf_error(cstate
, "no VLAN support for data link type %d",
8380 cstate
->vlan_stack_depth
++;
8389 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8391 struct block
*b0
, *b1
;
8393 if (cstate
->label_stack_depth
> 0) {
8394 /* just match the bottom-of-stack bit clear */
8395 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8398 * We're not in an MPLS stack yet, so check the link-layer
8399 * type against MPLS.
8401 switch (cstate
->linktype
) {
8403 case DLT_C_HDLC
: /* fall through */
8405 case DLT_NETANALYZER
:
8406 case DLT_NETANALYZER_TRANSPARENT
:
8407 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8411 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8414 /* FIXME add other DLT_s ...
8415 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8416 * leave it for now */
8419 bpf_error(cstate
, "no MPLS support for data link type %d",
8426 /* If a specific MPLS label is requested, check it */
8427 if (label_num
>= 0) {
8428 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8429 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8430 0xfffff000); /* only compare the first 20 bits */
8436 * Change the offsets to point to the type and data fields within
8437 * the MPLS packet. Just increment the offsets, so that we
8438 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8439 * capture packets with an outer label of 100000 and an inner
8442 * Increment the MPLS stack depth as well; this indicates that
8443 * we're checking MPLS-encapsulated headers, to make sure higher
8444 * level code generators don't try to match against IP-related
8445 * protocols such as Q_ARP, Q_RARP etc.
8447 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8449 cstate
->off_nl_nosnap
+= 4;
8450 cstate
->off_nl
+= 4;
8451 cstate
->label_stack_depth
++;
8456 * Support PPPOE discovery and session.
8459 gen_pppoed(compiler_state_t
*cstate
)
8461 /* check for PPPoE discovery */
8462 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8466 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8468 struct block
*b0
, *b1
;
8471 * Test against the PPPoE session link-layer type.
8473 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8475 /* If a specific session is requested, check PPPoE session id */
8476 if (sess_num
>= 0) {
8477 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8478 (bpf_int32
)sess_num
, 0x0000ffff);
8484 * Change the offsets to point to the type and data fields within
8485 * the PPP packet, and note that this is PPPoE rather than
8488 * XXX - this is a bit of a kludge. If we were to split the
8489 * compiler into a parser that parses an expression and
8490 * generates an expression tree, and a code generator that
8491 * takes an expression tree (which could come from our
8492 * parser or from some other parser) and generates BPF code,
8493 * we could perhaps make the offsets parameters of routines
8494 * and, in the handler for an "AND" node, pass to subnodes
8495 * other than the PPPoE node the adjusted offsets.
8497 * This would mean that "pppoes" would, instead of changing the
8498 * behavior of *all* tests after it, change only the behavior
8499 * of tests ANDed with it. That would change the documented
8500 * semantics of "pppoes", which might break some expressions.
8501 * However, it would mean that "(pppoes and ip) or ip" would check
8502 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8503 * checking only for VLAN-encapsulated IP, so that could still
8504 * be considered worth doing; it wouldn't break expressions
8505 * that are of the form "pppoes and ..." which I suspect are the
8506 * most common expressions involving "pppoes". "pppoes or ..."
8507 * doesn't necessarily do what the user would really want, now,
8508 * as all the "or ..." tests would be done assuming PPPoE, even
8509 * though the "or" could be viewed as meaning "or, if this isn't
8510 * a PPPoE packet...".
8512 * The "network-layer" protocol is PPPoE, which has a 6-byte
8513 * PPPoE header, followed by a PPP packet.
8515 * There is no HDLC encapsulation for the PPP packet (it's
8516 * encapsulated in PPPoES instead), so the link-layer type
8517 * starts at the first byte of the PPP packet. For PPPoE,
8518 * that offset is relative to the beginning of the total
8519 * link-layer payload, including any 802.2 LLC header, so
8520 * it's 6 bytes past cstate->off_nl.
8522 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8523 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8524 cstate
->off_linkpl
.reg
);
8526 cstate
->off_linktype
= cstate
->off_linkhdr
;
8527 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8530 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8535 /* Check that this is Geneve and the VNI is correct if
8536 * specified. Parameterized to handle both IPv4 and IPv6. */
8537 static struct block
*
8538 gen_geneve_check(compiler_state_t
*cstate
,
8539 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8540 enum e_offrel offrel
, int vni
)
8542 struct block
*b0
, *b1
;
8544 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8546 /* Check that we are operating on version 0. Otherwise, we
8547 * can't decode the rest of the fields. The version is 2 bits
8548 * in the first byte of the Geneve header. */
8549 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8554 vni
<<= 8; /* VNI is in the upper 3 bytes */
8555 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8564 /* The IPv4 and IPv6 Geneve checks need to do two things:
8565 * - Verify that this actually is Geneve with the right VNI.
8566 * - Place the IP header length (plus variable link prefix if
8567 * needed) into register A to be used later to compute
8568 * the inner packet offsets. */
8569 static struct block
*
8570 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8572 struct block
*b0
, *b1
;
8573 struct slist
*s
, *s1
;
8575 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8577 /* Load the IP header length into A. */
8578 s
= gen_loadx_iphdrlen(cstate
);
8580 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8583 /* Forcibly append these statements to the true condition
8584 * of the protocol check by creating a new block that is
8585 * always true and ANDing them. */
8586 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8595 static struct block
*
8596 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8598 struct block
*b0
, *b1
;
8599 struct slist
*s
, *s1
;
8601 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8603 /* Load the IP header length. We need to account for a
8604 * variable length link prefix if there is one. */
8605 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8607 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8611 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8615 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8619 /* Forcibly append these statements to the true condition
8620 * of the protocol check by creating a new block that is
8621 * always true and ANDing them. */
8622 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8625 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8634 /* We need to store three values based on the Geneve header::
8635 * - The offset of the linktype.
8636 * - The offset of the end of the Geneve header.
8637 * - The offset of the end of the encapsulated MAC header. */
8638 static struct slist
*
8639 gen_geneve_offsets(compiler_state_t
*cstate
)
8641 struct slist
*s
, *s1
, *s_proto
;
8643 /* First we need to calculate the offset of the Geneve header
8644 * itself. This is composed of the IP header previously calculated
8645 * (include any variable link prefix) and stored in A plus the
8646 * fixed sized headers (fixed link prefix, MAC length, and UDP
8648 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8649 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8651 /* Stash this in X since we'll need it later. */
8652 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8655 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8657 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8661 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8662 cstate
->off_linktype
.is_variable
= 1;
8663 cstate
->off_linktype
.constant_part
= 0;
8665 s1
= new_stmt(cstate
, BPF_ST
);
8666 s1
->s
.k
= cstate
->off_linktype
.reg
;
8669 /* Load the Geneve option length and mask and shift to get the
8670 * number of bytes. It is stored in the first byte of the Geneve
8672 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8676 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8680 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8684 /* Add in the rest of the Geneve base header. */
8685 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8689 /* Add the Geneve header length to its offset and store. */
8690 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8694 /* Set the encapsulated type as Ethernet. Even though we may
8695 * not actually have Ethernet inside there are two reasons this
8697 * - The linktype field is always in EtherType format regardless
8698 * of whether it is in Geneve or an inner Ethernet frame.
8699 * - The only link layer that we have specific support for is
8700 * Ethernet. We will confirm that the packet actually is
8701 * Ethernet at runtime before executing these checks. */
8702 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8704 s1
= new_stmt(cstate
, BPF_ST
);
8705 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8708 /* Calculate whether we have an Ethernet header or just raw IP/
8709 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8710 * and linktype by 14 bytes so that the network header can be found
8711 * seamlessly. Otherwise, keep what we've calculated already. */
8713 /* We have a bare jmp so we can't use the optimizer. */
8714 cstate
->no_optimize
= 1;
8716 /* Load the EtherType in the Geneve header, 2 bytes in. */
8717 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8721 /* Load X with the end of the Geneve header. */
8722 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8723 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8726 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8727 * end of this check, we should have the total length in X. In
8728 * the non-Ethernet case, it's already there. */
8729 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8730 s_proto
->s
.k
= ETHERTYPE_TEB
;
8731 sappend(s
, s_proto
);
8733 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8737 /* Since this is Ethernet, use the EtherType of the payload
8738 * directly as the linktype. Overwrite what we already have. */
8739 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8743 s1
= new_stmt(cstate
, BPF_ST
);
8744 s1
->s
.k
= cstate
->off_linktype
.reg
;
8747 /* Advance two bytes further to get the end of the Ethernet
8749 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8753 /* Move the result to X. */
8754 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8757 /* Store the final result of our linkpl calculation. */
8758 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8759 cstate
->off_linkpl
.is_variable
= 1;
8760 cstate
->off_linkpl
.constant_part
= 0;
8762 s1
= new_stmt(cstate
, BPF_STX
);
8763 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8772 /* Check to see if this is a Geneve packet. */
8774 gen_geneve(compiler_state_t
*cstate
, int vni
)
8776 struct block
*b0
, *b1
;
8779 b0
= gen_geneve4(cstate
, vni
);
8780 b1
= gen_geneve6(cstate
, vni
);
8785 /* Later filters should act on the payload of the Geneve frame,
8786 * update all of the header pointers. Attach this code so that
8787 * it gets executed in the event that the Geneve filter matches. */
8788 s
= gen_geneve_offsets(cstate
);
8790 b1
= gen_true(cstate
);
8791 sappend(s
, b1
->stmts
);
8796 cstate
->is_geneve
= 1;
8801 /* Check that the encapsulated frame has a link layer header
8802 * for Ethernet filters. */
8803 static struct block
*
8804 gen_geneve_ll_check(compiler_state_t
*cstate
)
8807 struct slist
*s
, *s1
;
8809 /* The easiest way to see if there is a link layer present
8810 * is to check if the link layer header and payload are not
8813 /* Geneve always generates pure variable offsets so we can
8814 * compare only the registers. */
8815 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8816 s
->s
.k
= cstate
->off_linkhdr
.reg
;
8818 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8819 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8822 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8831 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
8832 bpf_u_int32 jtype
, int reverse
)
8839 if (!cstate
->is_atm
)
8840 bpf_error(cstate
, "'vpi' supported only on raw ATM");
8841 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
8843 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
8848 if (!cstate
->is_atm
)
8849 bpf_error(cstate
, "'vci' supported only on raw ATM");
8850 if (cstate
->off_vci
== OFFSET_NOT_SET
)
8852 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
8857 if (cstate
->off_proto
== OFFSET_NOT_SET
)
8858 abort(); /* XXX - this isn't on FreeBSD */
8859 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
8864 if (cstate
->off_payload
== OFFSET_NOT_SET
)
8866 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
8867 0xffffffff, jtype
, reverse
, jvalue
);
8871 if (!cstate
->is_atm
)
8872 bpf_error(cstate
, "'callref' supported only on raw ATM");
8873 if (cstate
->off_proto
== OFFSET_NOT_SET
)
8875 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
8876 jtype
, reverse
, jvalue
);
8886 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
8888 struct block
*b0
, *b1
;
8893 /* Get all packets in Meta signalling Circuit */
8894 if (!cstate
->is_atm
)
8895 bpf_error(cstate
, "'metac' supported only on raw ATM");
8896 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8897 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
8902 /* Get all packets in Broadcast Circuit*/
8903 if (!cstate
->is_atm
)
8904 bpf_error(cstate
, "'bcc' supported only on raw ATM");
8905 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8906 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
8911 /* Get all cells in Segment OAM F4 circuit*/
8912 if (!cstate
->is_atm
)
8913 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
8914 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8915 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8920 /* Get all cells in End-to-End OAM F4 Circuit*/
8921 if (!cstate
->is_atm
)
8922 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
8923 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8924 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8929 /* Get all packets in connection Signalling Circuit */
8930 if (!cstate
->is_atm
)
8931 bpf_error(cstate
, "'sc' supported only on raw ATM");
8932 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8933 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
8938 /* Get all packets in ILMI Circuit */
8939 if (!cstate
->is_atm
)
8940 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
8941 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8942 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
8947 /* Get all LANE packets */
8948 if (!cstate
->is_atm
)
8949 bpf_error(cstate
, "'lane' supported only on raw ATM");
8950 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8953 * Arrange that all subsequent tests assume LANE
8954 * rather than LLC-encapsulated packets, and set
8955 * the offsets appropriately for LANE-encapsulated
8958 * We assume LANE means Ethernet, not Token Ring.
8960 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
8961 cstate
->off_payload
+ 2, /* Ethernet header */
8963 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
8964 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
8965 cstate
->off_nl
= 0; /* Ethernet II */
8966 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
8970 /* Get all LLC-encapsulated packets */
8971 if (!cstate
->is_atm
)
8972 bpf_error(cstate
, "'llc' supported only on raw ATM");
8973 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8974 cstate
->linktype
= cstate
->prevlinktype
;
8984 * Filtering for MTP2 messages based on li value
8985 * FISU, length is null
8986 * LSSU, length is 1 or 2
8987 * MSU, length is 3 or more
8988 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8991 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
8993 struct block
*b0
, *b1
;
8998 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8999 (cstate
->linktype
!= DLT_ERF
) &&
9000 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9001 bpf_error(cstate
, "'fisu' supported only on MTP2");
9002 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9003 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
9007 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9008 (cstate
->linktype
!= DLT_ERF
) &&
9009 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9010 bpf_error(cstate
, "'lssu' supported only on MTP2");
9011 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
9012 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9017 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9018 (cstate
->linktype
!= DLT_ERF
) &&
9019 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9020 bpf_error(cstate
, "'msu' supported only on MTP2");
9021 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9025 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9026 (cstate
->linktype
!= DLT_ERF
) &&
9027 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9028 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9029 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9030 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9034 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9035 (cstate
->linktype
!= DLT_ERF
) &&
9036 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9037 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9038 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9039 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9044 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9045 (cstate
->linktype
!= DLT_ERF
) &&
9046 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9047 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9048 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9058 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
9059 bpf_u_int32 jtype
, int reverse
)
9062 bpf_u_int32 val1
, val2
, val3
;
9063 u_int newoff_sio
= cstate
->off_sio
;
9064 u_int newoff_opc
= cstate
->off_opc
;
9065 u_int newoff_dpc
= cstate
->off_dpc
;
9066 u_int newoff_sls
= cstate
->off_sls
;
9068 switch (mtp3field
) {
9071 newoff_sio
+= 3; /* offset for MTP2_HSL */
9075 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9076 bpf_error(cstate
, "'sio' supported only on SS7");
9077 /* sio coded on 1 byte so max value 255 */
9079 bpf_error(cstate
, "sio value %u too big; max value = 255",
9081 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9082 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9088 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9089 bpf_error(cstate
, "'opc' supported only on SS7");
9090 /* opc coded on 14 bits so max value 16383 */
9092 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9094 /* the following instructions are made to convert jvalue
9095 * to the form used to write opc in an ss7 message*/
9096 val1
= jvalue
& 0x00003c00;
9098 val2
= jvalue
& 0x000003fc;
9100 val3
= jvalue
& 0x00000003;
9102 jvalue
= val1
+ val2
+ val3
;
9103 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9104 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9112 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9113 bpf_error(cstate
, "'dpc' supported only on SS7");
9114 /* dpc coded on 14 bits so max value 16383 */
9116 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9118 /* the following instructions are made to convert jvalue
9119 * to the forme used to write dpc in an ss7 message*/
9120 val1
= jvalue
& 0x000000ff;
9122 val2
= jvalue
& 0x00003f00;
9124 jvalue
= val1
+ val2
;
9125 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9126 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9132 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9133 bpf_error(cstate
, "'sls' supported only on SS7");
9134 /* sls coded on 4 bits so max value 15 */
9136 bpf_error(cstate
, "sls value %u too big; max value = 15",
9138 /* the following instruction is made to convert jvalue
9139 * to the forme used to write sls in an ss7 message*/
9140 jvalue
= jvalue
<< 4;
9141 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9142 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9151 static struct block
*
9152 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9157 * Q.2931 signalling protocol messages for handling virtual circuits
9158 * establishment and teardown
9163 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9167 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9171 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9175 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9179 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9182 case A_RELEASE_DONE
:
9183 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9193 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9195 struct block
*b0
, *b1
;
9200 if (!cstate
->is_atm
)
9201 bpf_error(cstate
, "'oam' supported only on raw ATM");
9202 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
9206 if (!cstate
->is_atm
)
9207 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9209 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9210 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9212 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9218 * Get Q.2931 signalling messages for switched
9219 * virtual connection
9221 if (!cstate
->is_atm
)
9222 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9223 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9224 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9226 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9228 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9230 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9232 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9234 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9239 if (!cstate
->is_atm
)
9240 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9241 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9242 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9244 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9246 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9248 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9250 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);