2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <netinet/in.h>
41 #include "ethertype.h"
44 #include "ieee80211.h"
48 #include "pcap/ipnet.h"
49 #include "diag-control.h"
53 #if defined(__linux__)
54 #include <linux/types.h>
55 #include <linux/if_packet.h>
56 #include <linux/filter.h>
60 #define offsetof(s, e) ((size_t)&((s *)0)->e)
65 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
72 uint16_t u6_addr16
[8];
73 uint32_t u6_addr32
[4];
75 #define s6_addr in6_u.u6_addr8
76 #define s6_addr16 in6_u.u6_addr16
77 #define s6_addr32 in6_u.u6_addr32
78 #define s6_addr64 in6_u.u6_addr64
81 typedef unsigned short sa_family_t
;
83 #define __SOCKADDR_COMMON(sa_prefix) \
84 sa_family_t sa_prefix##family
86 /* Ditto, for IPv6. */
89 __SOCKADDR_COMMON (sin6_
);
90 uint16_t sin6_port
; /* Transport layer port # */
91 uint32_t sin6_flowinfo
; /* IPv6 flow information */
92 struct in6_addr sin6_addr
; /* IPv6 address */
95 #ifndef EAI_ADDRFAMILY
97 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
98 int ai_family
; /* PF_xxx */
99 int ai_socktype
; /* SOCK_xxx */
100 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
101 size_t ai_addrlen
; /* length of ai_addr */
102 char *ai_canonname
; /* canonical name for hostname */
103 struct sockaddr
*ai_addr
; /* binary address */
104 struct addrinfo
*ai_next
; /* next structure in linked list */
106 #endif /* EAI_ADDRFAMILY */
107 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
110 #include <netdb.h> /* for "struct addrinfo" */
112 #include <pcap/namedb.h>
114 #include "nametoaddr.h"
116 #define ETHERMTU 1500
118 #ifndef IPPROTO_HOPOPTS
119 #define IPPROTO_HOPOPTS 0
121 #ifndef IPPROTO_ROUTING
122 #define IPPROTO_ROUTING 43
124 #ifndef IPPROTO_FRAGMENT
125 #define IPPROTO_FRAGMENT 44
127 #ifndef IPPROTO_DSTOPTS
128 #define IPPROTO_DSTOPTS 60
131 #define IPPROTO_SCTP 132
134 #define GENEVE_PORT 6081
135 #define VXLAN_PORT 4789
139 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
143 #define ARCTYPE_IP_OLD 240 /* IP protocol */
144 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
147 #define ARCTYPE_IP 212 /* IP protocol */
148 #define ARCTYPE_ARP 213 /* address resolution protocol */
149 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
151 #define ARCTYPE_ATALK 221 /* Appletalk */
152 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
153 #define ARCTYPE_IPX 250 /* Novell IPX */
155 #define ARCTYPE_INET6 0xc4 /* IPng */
156 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
159 /* Based on UNI3.1 standard by ATM Forum */
161 /* ATM traffic types based on VPI=0 and (the following VCI */
162 #define VCI_PPC 0x05 /* Point-to-point signal msg */
163 #define VCI_BCC 0x02 /* Broadcast signal msg */
164 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
165 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
166 #define VCI_METAC 0x01 /* Meta signal msg */
167 #define VCI_ILMIC 0x10 /* ILMI msg */
169 /* Q.2931 signalling messages */
170 #define CALL_PROCEED 0x02 /* call proceeding */
171 #define CONNECT 0x07 /* connect */
172 #define CONNECT_ACK 0x0f /* connect_ack */
173 #define SETUP 0x05 /* setup */
174 #define RELEASE 0x4d /* release */
175 #define RELEASE_DONE 0x5a /* release_done */
176 #define RESTART 0x46 /* restart */
177 #define RESTART_ACK 0x4e /* restart ack */
178 #define STATUS 0x7d /* status */
179 #define STATUS_ENQ 0x75 /* status ack */
180 #define ADD_PARTY 0x80 /* add party */
181 #define ADD_PARTY_ACK 0x81 /* add party ack */
182 #define ADD_PARTY_REJ 0x82 /* add party rej */
183 #define DROP_PARTY 0x83 /* drop party */
184 #define DROP_PARTY_ACK 0x84 /* drop party ack */
186 /* Information Element Parameters in the signalling messages */
187 #define CAUSE 0x08 /* cause */
188 #define ENDPT_REF 0x54 /* endpoint reference */
189 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
190 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
191 #define CONNECT_ID 0x5a /* connection identifier */
192 #define QOS_PARA 0x5c /* quality of service parameters */
193 #define B_HIGHER 0x5d /* broadband higher layer information */
194 #define B_BEARER 0x5e /* broadband bearer capability */
195 #define B_LOWER 0x5f /* broadband lower information */
196 #define CALLING_PARTY 0x6c /* calling party number */
197 #define CALLED_PARTY 0x70 /* called party number */
201 /* Q.2931 signalling general messages format */
202 #define PROTO_POS 0 /* offset of protocol discriminator */
203 #define CALL_REF_POS 2 /* offset of call reference value */
204 #define MSG_TYPE_POS 5 /* offset of message type */
205 #define MSG_LEN_POS 7 /* offset of message length */
206 #define IE_BEGIN_POS 9 /* offset of first information element */
208 /* format of signalling messages */
211 #define FIELD_BEGIN_POS 4
214 /* SunATM header for ATM packet */
215 #define SUNATM_DIR_POS 0
216 #define SUNATM_VPI_POS 1
217 #define SUNATM_VCI_POS 2
218 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
220 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
221 #define PT_LANE 0x01 /* LANE */
222 #define PT_LLC 0x02 /* LLC encapsulation */
223 #define PT_ILMI 0x05 /* ILMI */
224 #define PT_QSAAL 0x06 /* Q.SAAL */
227 /* Types missing from some systems */
230 * Network layer protocol identifiers
233 #define ISO8473_CLNP 0x81
236 #define ISO9542_ESIS 0x82
238 #ifndef ISO9542X25_ESIS
239 #define ISO9542X25_ESIS 0x8a
241 #ifndef ISO10589_ISIS
242 #define ISO10589_ISIS 0x83
245 #define ISIS_L1_LAN_IIH 15
246 #define ISIS_L2_LAN_IIH 16
247 #define ISIS_PTP_IIH 17
248 #define ISIS_L1_LSP 18
249 #define ISIS_L2_LSP 20
250 #define ISIS_L1_CSNP 24
251 #define ISIS_L2_CSNP 25
252 #define ISIS_L1_PSNP 26
253 #define ISIS_L2_PSNP 27
255 #ifndef ISO8878A_CONS
256 #define ISO8878A_CONS 0x84
258 #ifndef ISO10747_IDRP
259 #define ISO10747_IDRP 0x85
263 #ifdef HAVE_OS_PROTO_H
264 #include "os-proto.h"
267 #define JMP(c) ((c)|BPF_JMP|BPF_K)
270 * "Push" the current value of the link-layer header type and link-layer
271 * header offset onto a "stack", and set a new value. (It's not a
272 * full-blown stack; we keep only the top two items.)
274 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
276 (cs)->prevlinktype = (cs)->linktype; \
277 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
278 (cs)->linktype = (new_linktype); \
279 (cs)->off_linkhdr.is_variable = (new_is_variable); \
280 (cs)->off_linkhdr.constant_part = (new_constant_part); \
281 (cs)->off_linkhdr.reg = (new_reg); \
282 (cs)->is_encap = 0; \
286 * Offset "not set" value.
288 #define OFFSET_NOT_SET 0xffffffffU
291 * Absolute offsets, which are offsets from the beginning of the raw
292 * packet data, are, in the general case, the sum of a variable value
293 * and a constant value; the variable value may be absent, in which
294 * case the offset is only the constant value, and the constant value
295 * may be zero, in which case the offset is only the variable value.
297 * bpf_abs_offset is a structure containing all that information:
299 * is_variable is 1 if there's a variable part.
301 * constant_part is the constant part of the value, possibly zero;
303 * if is_variable is 1, reg is the register number for a register
304 * containing the variable value if the register has been assigned,
314 * Value passed to gen_load_a() to indicate what the offset argument
315 * is relative to the beginning of.
318 OR_PACKET
, /* full packet data */
319 OR_LINKHDR
, /* link-layer header */
320 OR_PREVLINKHDR
, /* previous link-layer header */
321 OR_LLC
, /* 802.2 LLC header */
322 OR_PREVMPLSHDR
, /* previous MPLS header */
323 OR_LINKTYPE
, /* link-layer type */
324 OR_LINKPL
, /* link-layer payload */
325 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
326 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
327 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
331 * We divvy out chunks of memory rather than call malloc each time so
332 * we don't have to worry about leaking memory. It's probably
333 * not a big deal if all this memory was wasted but if this ever
334 * goes into a library that would probably not be a good idea.
336 * XXX - this *is* in a library....
339 #define CHUNK0SIZE 1024
346 * A chunk can store any of:
347 * - a string (guaranteed alignment 1 but present for completeness)
351 * For this simple allocator every allocated chunk gets rounded up to the
352 * alignment needed for any chunk.
363 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
365 /* Code generator state */
367 struct _compiler_state
{
378 int outermostlinktype
;
383 /* Hack for handling VLAN and MPLS stacks. */
384 u_int label_stack_depth
;
385 u_int vlan_stack_depth
;
391 * As errors are handled by a longjmp, anything allocated must
392 * be freed in the longjmp handler, so it must be reachable
395 * One thing that's allocated is the result of pcap_nametoaddrinfo();
396 * it must be freed with freeaddrinfo(). This variable points to
397 * any addrinfo structure that would need to be freed.
402 * Another thing that's allocated is the result of pcap_ether_aton();
403 * it must be freed with free(). This variable points to any
404 * address that would need to be freed.
409 * Various code constructs need to know the layout of the packet.
410 * These values give the necessary offsets from the beginning
411 * of the packet data.
415 * Absolute offset of the beginning of the link-layer header.
417 bpf_abs_offset off_linkhdr
;
420 * If we're checking a link-layer header for a packet encapsulated
421 * in another protocol layer, this is the equivalent information
422 * for the previous layers' link-layer header from the beginning
423 * of the raw packet data.
425 bpf_abs_offset off_prevlinkhdr
;
428 * This is the equivalent information for the outermost layers'
431 bpf_abs_offset off_outermostlinkhdr
;
434 * Absolute offset of the beginning of the link-layer payload.
436 bpf_abs_offset off_linkpl
;
439 * "off_linktype" is the offset to information in the link-layer
440 * header giving the packet type. This is an absolute offset
441 * from the beginning of the packet.
443 * For Ethernet, it's the offset of the Ethernet type field; this
444 * means that it must have a value that skips VLAN tags.
446 * For link-layer types that always use 802.2 headers, it's the
447 * offset of the LLC header; this means that it must have a value
448 * that skips VLAN tags.
450 * For PPP, it's the offset of the PPP type field.
452 * For Cisco HDLC, it's the offset of the CHDLC type field.
454 * For BSD loopback, it's the offset of the AF_ value.
456 * For Linux cooked sockets, it's the offset of the type field.
458 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
459 * encapsulation, in which case, IP is assumed.
461 bpf_abs_offset off_linktype
;
464 * TRUE if the link layer includes an ATM pseudo-header.
468 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
469 * causes us to generate code that checks for a Geneve or
470 * VXLAN header respectively and assume that later filters
471 * apply to the encapsulated payload.
476 * TRUE if we need variable length part of VLAN offset
478 int is_vlan_vloffset
;
481 * These are offsets for the ATM pseudo-header.
488 * These are offsets for the MTP2 fields.
494 * These are offsets for the MTP3 fields.
502 * This is the offset of the first byte after the ATM pseudo_header,
503 * or -1 if there is no ATM pseudo-header.
508 * These are offsets to the beginning of the network-layer header.
509 * They are relative to the beginning of the link-layer payload
510 * (i.e., they don't include off_linkhdr.constant_part or
511 * off_linkpl.constant_part).
513 * If the link layer never uses 802.2 LLC:
515 * "off_nl" and "off_nl_nosnap" are the same.
517 * If the link layer always uses 802.2 LLC:
519 * "off_nl" is the offset if there's a SNAP header following
522 * "off_nl_nosnap" is the offset if there's no SNAP header.
524 * If the link layer is Ethernet:
526 * "off_nl" is the offset if the packet is an Ethernet II packet
527 * (we assume no 802.3+802.2+SNAP);
529 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
530 * with an 802.2 header following it.
536 * Here we handle simple allocation of the scratch registers.
537 * If too many registers are alloc'd, the allocator punts.
539 int regused
[BPF_MEMWORDS
];
545 struct chunk chunks
[NCHUNKS
];
550 * For use by routines outside this file.
554 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
559 * If we've already set an error, don't override it.
560 * The lexical analyzer reports some errors by setting
561 * the error and then returning a LEX_ERROR token, which
562 * is not recognized by any grammar rule, and thus forces
563 * the parse to stop. We don't want the error reported
564 * by the lexical analyzer to be overwritten by the syntax
567 if (!cstate
->error_set
) {
569 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
572 cstate
->error_set
= 1;
577 * For use *ONLY* in routines in this file.
579 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
580 PCAP_PRINTFLIKE(2, 3);
583 static void PCAP_NORETURN
584 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
589 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
592 longjmp(cstate
->top_ctx
, 1);
599 static int init_linktype(compiler_state_t
*, pcap_t
*);
601 static void init_regs(compiler_state_t
*);
602 static int alloc_reg(compiler_state_t
*);
603 static void free_reg(compiler_state_t
*, int);
605 static void initchunks(compiler_state_t
*cstate
);
606 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
607 static void *newchunk(compiler_state_t
*cstate
, size_t);
608 static void freechunks(compiler_state_t
*cstate
);
609 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
610 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
611 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
612 static inline void syntax(compiler_state_t
*cstate
);
614 static void backpatch(struct block
*, struct block
*);
615 static void merge(struct block
*, struct block
*);
616 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
618 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
620 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
622 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
624 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
626 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
627 u_int
, bpf_u_int32
, bpf_u_int32
);
628 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
629 u_int
, const u_char
*);
630 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
631 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
632 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
634 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
636 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
637 static struct block
*gen_uncond(compiler_state_t
*, int);
638 static inline struct block
*gen_true(compiler_state_t
*);
639 static inline struct block
*gen_false(compiler_state_t
*);
640 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
641 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
642 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
643 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
644 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
645 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
646 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
647 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
648 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
649 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
651 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
652 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
653 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
654 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
655 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
656 int, bpf_u_int32
, u_int
, u_int
);
658 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
659 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
661 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
662 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
663 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
664 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
665 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
666 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
667 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
668 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
669 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
672 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
673 struct in6_addr
*, int, int, int);
676 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
677 struct addrinfo
*, int, int);
679 static struct block
*gen_ipfrag(compiler_state_t
*);
680 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
681 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
683 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
684 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
686 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
687 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
688 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
690 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
691 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
692 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
693 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
695 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
696 static int lookup_proto(compiler_state_t
*, const char *, int);
697 #if !defined(NO_PROTOCHAIN)
698 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
699 #endif /* !defined(NO_PROTOCHAIN) */
700 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
701 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
702 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
703 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
704 static struct block
*gen_len(compiler_state_t
*, int, int);
705 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
706 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
708 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
709 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
710 bpf_u_int32
, int, int);
711 static struct block
*gen_atmtype_llc(compiler_state_t
*);
712 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
715 initchunks(compiler_state_t
*cstate
)
719 for (i
= 0; i
< NCHUNKS
; i
++) {
720 cstate
->chunks
[i
].n_left
= 0;
721 cstate
->chunks
[i
].m
= NULL
;
723 cstate
->cur_chunk
= 0;
727 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
733 /* Round up to chunk alignment. */
734 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
736 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
737 if (n
> cp
->n_left
) {
739 k
= ++cstate
->cur_chunk
;
741 bpf_set_error(cstate
, "out of memory");
744 size
= CHUNK0SIZE
<< k
;
745 cp
->m
= (void *)malloc(size
);
747 bpf_set_error(cstate
, "out of memory");
750 memset((char *)cp
->m
, 0, size
);
753 bpf_set_error(cstate
, "out of memory");
758 return (void *)((char *)cp
->m
+ cp
->n_left
);
762 newchunk(compiler_state_t
*cstate
, size_t n
)
766 p
= newchunk_nolongjmp(cstate
, n
);
768 longjmp(cstate
->top_ctx
, 1);
775 freechunks(compiler_state_t
*cstate
)
779 for (i
= 0; i
< NCHUNKS
; ++i
)
780 if (cstate
->chunks
[i
].m
!= NULL
)
781 free(cstate
->chunks
[i
].m
);
785 * A strdup whose allocations are freed after code generation is over.
786 * This is used by the lexical analyzer, so it can't longjmp; it just
787 * returns NULL on an allocation error, and the callers must check
791 sdup(compiler_state_t
*cstate
, const char *s
)
793 size_t n
= strlen(s
) + 1;
794 char *cp
= newchunk_nolongjmp(cstate
, n
);
798 pcapint_strlcpy(cp
, s
, n
);
802 static inline struct block
*
803 new_block(compiler_state_t
*cstate
, int code
)
807 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
814 static inline struct slist
*
815 new_stmt(compiler_state_t
*cstate
, int code
)
819 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
825 static struct block
*
826 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
828 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
834 static struct block
*
835 gen_retblk(compiler_state_t
*cstate
, int v
)
837 if (setjmp(cstate
->top_ctx
)) {
839 * gen_retblk() only fails because a memory
840 * allocation failed in newchunk(), meaning
841 * that it can't return a pointer.
847 return gen_retblk_internal(cstate
, v
);
850 static inline PCAP_NORETURN_DEF
void
851 syntax(compiler_state_t
*cstate
)
853 bpf_error(cstate
, "syntax error in filter expression");
857 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
858 const char *buf
, int optimize
, bpf_u_int32 mask
)
864 compiler_state_t cstate
;
865 yyscan_t scanner
= NULL
;
866 YY_BUFFER_STATE in_buffer
= NULL
;
871 * If this pcap_t hasn't been activated, it doesn't have a
872 * link-layer type, so we can't use it.
875 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
876 "not-yet-activated pcap_t passed to pcap_compile");
882 * Initialize Winsock, asking for the latest version (2.2),
883 * as we may be calling Winsock routines to translate
884 * host names to addresses.
886 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
888 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
889 err
, "Error calling WSAStartup()");
896 * If the device on which we're capturing need to be notified
897 * that a new filter is being compiled, do so.
899 * This allows them to save a copy of it, in case, for example,
900 * they're implementing a form of remote packet capture, and
901 * want the remote machine to filter out the packets in which
902 * it's sending the packets it's captured.
904 * XXX - the fact that we happen to be compiling a filter
905 * doesn't necessarily mean we'll be installing it as the
906 * filter for this pcap_t; we might be running it from userland
907 * on captured packets to do packet classification. We really
908 * need a better way of handling this, but this is all that
909 * the WinPcap remote capture code did.
911 if (p
->save_current_filter_op
!= NULL
)
912 (p
->save_current_filter_op
)(p
, buf
);
916 cstate
.no_optimize
= 0;
921 cstate
.ic
.root
= NULL
;
922 cstate
.ic
.cur_mark
= 0;
924 cstate
.error_set
= 0;
927 cstate
.netmask
= mask
;
929 cstate
.snaplen
= pcap_snapshot(p
);
930 if (cstate
.snaplen
== 0) {
931 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
932 "snaplen of 0 rejects all packets");
937 if (pcap_lex_init(&scanner
) != 0) {
938 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
939 errno
, "can't initialize scanner");
943 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
946 * Associate the compiler state with the lexical analyzer
949 pcap_set_extra(&cstate
, scanner
);
951 if (init_linktype(&cstate
, p
) == -1) {
955 if (pcap_parse(scanner
, &cstate
) != 0) {
957 if (cstate
.ai
!= NULL
)
958 freeaddrinfo(cstate
.ai
);
960 if (cstate
.e
!= NULL
)
966 if (cstate
.ic
.root
== NULL
) {
967 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
970 * Catch errors reported by gen_retblk().
972 if (cstate
.ic
.root
== NULL
) {
978 if (optimize
&& !cstate
.no_optimize
) {
979 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
984 if (cstate
.ic
.root
== NULL
||
985 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
986 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
987 "expression rejects all packets");
992 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
993 cstate
.ic
.root
, &len
, p
->errbuf
);
994 if (program
->bf_insns
== NULL
) {
999 program
->bf_len
= len
;
1001 rc
= 0; /* We're all okay */
1005 * Clean up everything for the lexical analyzer.
1007 if (in_buffer
!= NULL
)
1008 pcap__delete_buffer(in_buffer
, scanner
);
1009 if (scanner
!= NULL
)
1010 pcap_lex_destroy(scanner
);
1013 * Clean up our own allocated memory.
1015 freechunks(&cstate
);
1025 * entry point for using the compiler with no pcap open
1026 * pass in all the stuff that is needed explicitly instead.
1029 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1030 struct bpf_program
*program
,
1031 const char *buf
, int optimize
, bpf_u_int32 mask
)
1036 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1038 return (PCAP_ERROR
);
1039 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1045 * Clean up a "struct bpf_program" by freeing all the memory allocated
1049 pcap_freecode(struct bpf_program
*program
)
1051 program
->bf_len
= 0;
1052 if (program
->bf_insns
!= NULL
) {
1053 free((char *)program
->bf_insns
);
1054 program
->bf_insns
= NULL
;
1059 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1060 * which of the jt and jf fields has been resolved and which is a pointer
1061 * back to another unresolved block (or nil). At least one of the fields
1062 * in each block is already resolved.
1065 backpatch(struct block
*list
, struct block
*target
)
1082 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1083 * which of jt and jf is the link.
1086 merge(struct block
*b0
, struct block
*b1
)
1088 register struct block
**p
= &b0
;
1090 /* Find end of list. */
1092 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1094 /* Concatenate the lists. */
1099 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1101 struct block
*ppi_dlt_check
;
1104 * Catch errors reported by us and routines below us, and return -1
1107 if (setjmp(cstate
->top_ctx
))
1111 * Insert before the statements of the first (root) block any
1112 * statements needed to load the lengths of any variable-length
1113 * headers into registers.
1115 * XXX - a fancier strategy would be to insert those before the
1116 * statements of all blocks that use those lengths and that
1117 * have no predecessors that use them, so that we only compute
1118 * the lengths if we need them. There might be even better
1119 * approaches than that.
1121 * However, those strategies would be more complicated, and
1122 * as we don't generate code to compute a length if the
1123 * program has no tests that use the length, and as most
1124 * tests will probably use those lengths, we would just
1125 * postpone computing the lengths so that it's not done
1126 * for tests that fail early, and it's not clear that's
1129 insert_compute_vloffsets(cstate
, p
->head
);
1132 * For DLT_PPI captures, generate a check of the per-packet
1133 * DLT value to make sure it's DLT_IEEE802_11.
1135 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1136 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1137 * with appropriate Ethernet information and use that rather
1138 * than using something such as DLT_PPI where you don't know
1139 * the link-layer header type until runtime, which, in the
1140 * general case, would force us to generate both Ethernet *and*
1141 * 802.11 code (*and* anything else for which PPI is used)
1142 * and choose between them early in the BPF program?
1144 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
1145 if (ppi_dlt_check
!= NULL
)
1146 gen_and(ppi_dlt_check
, p
);
1148 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1149 p
->sense
= !p
->sense
;
1150 backpatch(p
, gen_retblk_internal(cstate
, 0));
1151 cstate
->ic
.root
= p
->head
;
1156 gen_and(struct block
*b0
, struct block
*b1
)
1158 backpatch(b0
, b1
->head
);
1159 b0
->sense
= !b0
->sense
;
1160 b1
->sense
= !b1
->sense
;
1162 b1
->sense
= !b1
->sense
;
1163 b1
->head
= b0
->head
;
1167 gen_or(struct block
*b0
, struct block
*b1
)
1169 b0
->sense
= !b0
->sense
;
1170 backpatch(b0
, b1
->head
);
1171 b0
->sense
= !b0
->sense
;
1173 b1
->head
= b0
->head
;
1177 gen_not(struct block
*b
)
1179 b
->sense
= !b
->sense
;
1182 static struct block
*
1183 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1184 u_int size
, bpf_u_int32 v
)
1186 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1189 static struct block
*
1190 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1191 u_int size
, bpf_u_int32 v
)
1193 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1196 static struct block
*
1197 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1198 u_int size
, bpf_u_int32 v
)
1200 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1203 static struct block
*
1204 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1205 u_int size
, bpf_u_int32 v
)
1207 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1210 static struct block
*
1211 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1212 u_int size
, bpf_u_int32 v
)
1214 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1217 static struct block
*
1218 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1219 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1221 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1224 static struct block
*
1225 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1226 u_int size
, const u_char
*v
)
1228 register struct block
*b
, *tmp
;
1232 register const u_char
*p
= &v
[size
- 4];
1234 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1242 register const u_char
*p
= &v
[size
- 2];
1244 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1252 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1261 * AND the field of size "size" at offset "offset" relative to the header
1262 * specified by "offrel" with "mask", and compare it with the value "v"
1263 * with the test specified by "jtype"; if "reverse" is true, the test
1264 * should test the opposite of "jtype".
1266 static struct block
*
1267 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1268 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1271 struct slist
*s
, *s2
;
1274 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1276 if (mask
!= 0xffffffff) {
1277 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1282 b
= new_block(cstate
, JMP(jtype
));
1285 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1291 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1293 cstate
->pcap_fddipad
= p
->fddipad
;
1296 * We start out with only one link-layer header.
1298 cstate
->outermostlinktype
= pcap_datalink(p
);
1299 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1300 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1301 cstate
->off_outermostlinkhdr
.reg
= -1;
1303 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1304 cstate
->off_prevlinkhdr
.constant_part
= 0;
1305 cstate
->off_prevlinkhdr
.is_variable
= 0;
1306 cstate
->off_prevlinkhdr
.reg
= -1;
1308 cstate
->linktype
= cstate
->outermostlinktype
;
1309 cstate
->off_linkhdr
.constant_part
= 0;
1310 cstate
->off_linkhdr
.is_variable
= 0;
1311 cstate
->off_linkhdr
.reg
= -1;
1316 cstate
->off_linkpl
.constant_part
= 0;
1317 cstate
->off_linkpl
.is_variable
= 0;
1318 cstate
->off_linkpl
.reg
= -1;
1320 cstate
->off_linktype
.constant_part
= 0;
1321 cstate
->off_linktype
.is_variable
= 0;
1322 cstate
->off_linktype
.reg
= -1;
1325 * Assume it's not raw ATM with a pseudo-header, for now.
1328 cstate
->off_vpi
= OFFSET_NOT_SET
;
1329 cstate
->off_vci
= OFFSET_NOT_SET
;
1330 cstate
->off_proto
= OFFSET_NOT_SET
;
1331 cstate
->off_payload
= OFFSET_NOT_SET
;
1334 * And not encapsulated with either Geneve or VXLAN.
1336 cstate
->is_encap
= 0;
1339 * No variable length VLAN offset by default
1341 cstate
->is_vlan_vloffset
= 0;
1344 * And assume we're not doing SS7.
1346 cstate
->off_li
= OFFSET_NOT_SET
;
1347 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1348 cstate
->off_sio
= OFFSET_NOT_SET
;
1349 cstate
->off_opc
= OFFSET_NOT_SET
;
1350 cstate
->off_dpc
= OFFSET_NOT_SET
;
1351 cstate
->off_sls
= OFFSET_NOT_SET
;
1353 cstate
->label_stack_depth
= 0;
1354 cstate
->vlan_stack_depth
= 0;
1356 switch (cstate
->linktype
) {
1359 cstate
->off_linktype
.constant_part
= 2;
1360 cstate
->off_linkpl
.constant_part
= 6;
1361 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1362 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1365 case DLT_ARCNET_LINUX
:
1366 cstate
->off_linktype
.constant_part
= 4;
1367 cstate
->off_linkpl
.constant_part
= 8;
1368 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1369 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1373 cstate
->off_linktype
.constant_part
= 12;
1374 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1375 cstate
->off_nl
= 0; /* Ethernet II */
1376 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1381 * SLIP doesn't have a link level type. The 16 byte
1382 * header is hacked into our SLIP driver.
1384 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1385 cstate
->off_linkpl
.constant_part
= 16;
1387 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1390 case DLT_SLIP_BSDOS
:
1391 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1392 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1394 cstate
->off_linkpl
.constant_part
= 24;
1396 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1401 cstate
->off_linktype
.constant_part
= 0;
1402 cstate
->off_linkpl
.constant_part
= 4;
1404 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1408 cstate
->off_linktype
.constant_part
= 0;
1409 cstate
->off_linkpl
.constant_part
= 12;
1411 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1416 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1417 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1418 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1419 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1420 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1422 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1427 * This does no include the Ethernet header, and
1428 * only covers session state.
1430 cstate
->off_linktype
.constant_part
= 6;
1431 cstate
->off_linkpl
.constant_part
= 8;
1433 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1437 cstate
->off_linktype
.constant_part
= 5;
1438 cstate
->off_linkpl
.constant_part
= 24;
1440 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1445 * FDDI doesn't really have a link-level type field.
1446 * We set "off_linktype" to the offset of the LLC header.
1448 * To check for Ethernet types, we assume that SSAP = SNAP
1449 * is being used and pick out the encapsulated Ethernet type.
1450 * XXX - should we generate code to check for SNAP?
1452 cstate
->off_linktype
.constant_part
= 13;
1453 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1454 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1455 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1456 cstate
->off_nl
= 8; /* 802.2+SNAP */
1457 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1462 * Token Ring doesn't really have a link-level type field.
1463 * We set "off_linktype" to the offset of the LLC header.
1465 * To check for Ethernet types, we assume that SSAP = SNAP
1466 * is being used and pick out the encapsulated Ethernet type.
1467 * XXX - should we generate code to check for SNAP?
1469 * XXX - the header is actually variable-length.
1470 * Some various Linux patched versions gave 38
1471 * as "off_linktype" and 40 as "off_nl"; however,
1472 * if a token ring packet has *no* routing
1473 * information, i.e. is not source-routed, the correct
1474 * values are 20 and 22, as they are in the vanilla code.
1476 * A packet is source-routed iff the uppermost bit
1477 * of the first byte of the source address, at an
1478 * offset of 8, has the uppermost bit set. If the
1479 * packet is source-routed, the total number of bytes
1480 * of routing information is 2 plus bits 0x1F00 of
1481 * the 16-bit value at an offset of 14 (shifted right
1482 * 8 - figure out which byte that is).
1484 cstate
->off_linktype
.constant_part
= 14;
1485 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1486 cstate
->off_nl
= 8; /* 802.2+SNAP */
1487 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1490 case DLT_PRISM_HEADER
:
1491 case DLT_IEEE802_11_RADIO_AVS
:
1492 case DLT_IEEE802_11_RADIO
:
1493 cstate
->off_linkhdr
.is_variable
= 1;
1494 /* Fall through, 802.11 doesn't have a variable link
1495 * prefix but is otherwise the same. */
1498 case DLT_IEEE802_11
:
1500 * 802.11 doesn't really have a link-level type field.
1501 * We set "off_linktype.constant_part" to the offset of
1504 * To check for Ethernet types, we assume that SSAP = SNAP
1505 * is being used and pick out the encapsulated Ethernet type.
1506 * XXX - should we generate code to check for SNAP?
1508 * We also handle variable-length radio headers here.
1509 * The Prism header is in theory variable-length, but in
1510 * practice it's always 144 bytes long. However, some
1511 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1512 * sometimes or always supply an AVS header, so we
1513 * have to check whether the radio header is a Prism
1514 * header or an AVS header, so, in practice, it's
1517 cstate
->off_linktype
.constant_part
= 24;
1518 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1519 cstate
->off_linkpl
.is_variable
= 1;
1520 cstate
->off_nl
= 8; /* 802.2+SNAP */
1521 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1526 * At the moment we treat PPI the same way that we treat
1527 * normal Radiotap encoded packets. The difference is in
1528 * the function that generates the code at the beginning
1529 * to compute the header length. Since this code generator
1530 * of PPI supports bare 802.11 encapsulation only (i.e.
1531 * the encapsulated DLT should be DLT_IEEE802_11) we
1532 * generate code to check for this too.
1534 cstate
->off_linktype
.constant_part
= 24;
1535 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1536 cstate
->off_linkpl
.is_variable
= 1;
1537 cstate
->off_linkhdr
.is_variable
= 1;
1538 cstate
->off_nl
= 8; /* 802.2+SNAP */
1539 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1542 case DLT_ATM_RFC1483
:
1543 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1545 * assume routed, non-ISO PDUs
1546 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1548 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1549 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1550 * latter would presumably be treated the way PPPoE
1551 * should be, so you can do "pppoe and udp port 2049"
1552 * or "pppoa and tcp port 80" and have it check for
1553 * PPPo{A,E} and a PPP protocol of IP and....
1555 cstate
->off_linktype
.constant_part
= 0;
1556 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1557 cstate
->off_nl
= 8; /* 802.2+SNAP */
1558 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1563 * Full Frontal ATM; you get AALn PDUs with an ATM
1567 cstate
->off_vpi
= SUNATM_VPI_POS
;
1568 cstate
->off_vci
= SUNATM_VCI_POS
;
1569 cstate
->off_proto
= PROTO_POS
;
1570 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1571 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1572 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1573 cstate
->off_nl
= 8; /* 802.2+SNAP */
1574 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1580 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1581 cstate
->off_linkpl
.constant_part
= 0;
1583 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1586 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1587 cstate
->off_linktype
.constant_part
= 14;
1588 cstate
->off_linkpl
.constant_part
= 16;
1590 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1593 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1594 cstate
->off_linktype
.constant_part
= 0;
1595 cstate
->off_linkpl
.constant_part
= 20;
1597 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1602 * LocalTalk does have a 1-byte type field in the LLAP header,
1603 * but really it just indicates whether there is a "short" or
1604 * "long" DDP packet following.
1606 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1607 cstate
->off_linkpl
.constant_part
= 0;
1609 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1612 case DLT_IP_OVER_FC
:
1614 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1615 * link-level type field. We set "off_linktype" to the
1616 * offset of the LLC header.
1618 * To check for Ethernet types, we assume that SSAP = SNAP
1619 * is being used and pick out the encapsulated Ethernet type.
1620 * XXX - should we generate code to check for SNAP? RFC
1621 * 2625 says SNAP should be used.
1623 cstate
->off_linktype
.constant_part
= 16;
1624 cstate
->off_linkpl
.constant_part
= 16;
1625 cstate
->off_nl
= 8; /* 802.2+SNAP */
1626 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1631 * XXX - we should set this to handle SNAP-encapsulated
1632 * frames (NLPID of 0x80).
1634 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1635 cstate
->off_linkpl
.constant_part
= 0;
1637 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1641 * the only BPF-interesting FRF.16 frames are non-control frames;
1642 * Frame Relay has a variable length link-layer
1643 * so lets start with offset 4 for now and increments later on (FIXME);
1646 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1647 cstate
->off_linkpl
.constant_part
= 0;
1649 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1652 case DLT_APPLE_IP_OVER_IEEE1394
:
1653 cstate
->off_linktype
.constant_part
= 16;
1654 cstate
->off_linkpl
.constant_part
= 18;
1656 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1659 case DLT_SYMANTEC_FIREWALL
:
1660 cstate
->off_linktype
.constant_part
= 6;
1661 cstate
->off_linkpl
.constant_part
= 44;
1662 cstate
->off_nl
= 0; /* Ethernet II */
1663 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1667 cstate
->off_linktype
.constant_part
= 0;
1668 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1669 cstate
->off_linkpl
.is_variable
= 1;
1671 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1674 case DLT_JUNIPER_MFR
:
1675 case DLT_JUNIPER_MLFR
:
1676 case DLT_JUNIPER_MLPPP
:
1677 case DLT_JUNIPER_PPP
:
1678 case DLT_JUNIPER_CHDLC
:
1679 case DLT_JUNIPER_FRELAY
:
1680 cstate
->off_linktype
.constant_part
= 4;
1681 cstate
->off_linkpl
.constant_part
= 4;
1683 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1686 case DLT_JUNIPER_ATM1
:
1687 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1688 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1690 cstate
->off_nl_nosnap
= 10;
1693 case DLT_JUNIPER_ATM2
:
1694 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1695 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1697 cstate
->off_nl_nosnap
= 10;
1700 /* frames captured on a Juniper PPPoE service PIC
1701 * contain raw ethernet frames */
1702 case DLT_JUNIPER_PPPOE
:
1703 case DLT_JUNIPER_ETHER
:
1704 cstate
->off_linkpl
.constant_part
= 14;
1705 cstate
->off_linktype
.constant_part
= 16;
1706 cstate
->off_nl
= 18; /* Ethernet II */
1707 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1710 case DLT_JUNIPER_PPPOE_ATM
:
1711 cstate
->off_linktype
.constant_part
= 4;
1712 cstate
->off_linkpl
.constant_part
= 6;
1714 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1717 case DLT_JUNIPER_GGSN
:
1718 cstate
->off_linktype
.constant_part
= 6;
1719 cstate
->off_linkpl
.constant_part
= 12;
1721 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1724 case DLT_JUNIPER_ES
:
1725 cstate
->off_linktype
.constant_part
= 6;
1726 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1727 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1728 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1731 case DLT_JUNIPER_MONITOR
:
1732 cstate
->off_linktype
.constant_part
= 12;
1733 cstate
->off_linkpl
.constant_part
= 12;
1734 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1735 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1738 case DLT_BACNET_MS_TP
:
1739 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1740 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1741 cstate
->off_nl
= OFFSET_NOT_SET
;
1742 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1745 case DLT_JUNIPER_SERVICES
:
1746 cstate
->off_linktype
.constant_part
= 12;
1747 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1748 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1749 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1752 case DLT_JUNIPER_VP
:
1753 cstate
->off_linktype
.constant_part
= 18;
1754 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1755 cstate
->off_nl
= OFFSET_NOT_SET
;
1756 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1759 case DLT_JUNIPER_ST
:
1760 cstate
->off_linktype
.constant_part
= 18;
1761 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1762 cstate
->off_nl
= OFFSET_NOT_SET
;
1763 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1766 case DLT_JUNIPER_ISM
:
1767 cstate
->off_linktype
.constant_part
= 8;
1768 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1769 cstate
->off_nl
= OFFSET_NOT_SET
;
1770 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1773 case DLT_JUNIPER_VS
:
1774 case DLT_JUNIPER_SRX_E2E
:
1775 case DLT_JUNIPER_FIBRECHANNEL
:
1776 case DLT_JUNIPER_ATM_CEMIC
:
1777 cstate
->off_linktype
.constant_part
= 8;
1778 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1779 cstate
->off_nl
= OFFSET_NOT_SET
;
1780 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1785 cstate
->off_li_hsl
= 4;
1786 cstate
->off_sio
= 3;
1787 cstate
->off_opc
= 4;
1788 cstate
->off_dpc
= 4;
1789 cstate
->off_sls
= 7;
1790 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1791 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1792 cstate
->off_nl
= OFFSET_NOT_SET
;
1793 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1796 case DLT_MTP2_WITH_PHDR
:
1798 cstate
->off_li_hsl
= 8;
1799 cstate
->off_sio
= 7;
1800 cstate
->off_opc
= 8;
1801 cstate
->off_dpc
= 8;
1802 cstate
->off_sls
= 11;
1803 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1804 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1805 cstate
->off_nl
= OFFSET_NOT_SET
;
1806 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1810 cstate
->off_li
= 22;
1811 cstate
->off_li_hsl
= 24;
1812 cstate
->off_sio
= 23;
1813 cstate
->off_opc
= 24;
1814 cstate
->off_dpc
= 24;
1815 cstate
->off_sls
= 27;
1816 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1817 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1818 cstate
->off_nl
= OFFSET_NOT_SET
;
1819 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1823 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1824 cstate
->off_linkpl
.constant_part
= 4;
1826 cstate
->off_nl_nosnap
= 0;
1831 * Currently, only raw "link[N:M]" filtering is supported.
1833 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1834 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1835 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1836 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1840 cstate
->off_linktype
.constant_part
= 1;
1841 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1843 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1846 case DLT_NETANALYZER
:
1847 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1848 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1849 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1850 cstate
->off_nl
= 0; /* Ethernet II */
1851 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1854 case DLT_NETANALYZER_TRANSPARENT
:
1855 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1856 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1857 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1858 cstate
->off_nl
= 0; /* Ethernet II */
1859 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1864 * For values in the range in which we've assigned new
1865 * DLT_ values, only raw "link[N:M]" filtering is supported.
1867 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1868 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1869 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1870 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1871 cstate
->off_nl
= OFFSET_NOT_SET
;
1872 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1874 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1875 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1881 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1886 * Load a value relative to the specified absolute offset.
1888 static struct slist
*
1889 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1890 u_int offset
, u_int size
)
1892 struct slist
*s
, *s2
;
1894 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1897 * If "s" is non-null, it has code to arrange that the X register
1898 * contains the variable part of the absolute offset, so we
1899 * generate a load relative to that, with an offset of
1900 * abs_offset->constant_part + offset.
1902 * Otherwise, we can do an absolute load with an offset of
1903 * abs_offset->constant_part + offset.
1907 * "s" points to a list of statements that puts the
1908 * variable part of the absolute offset into the X register.
1909 * Do an indirect load, to use the X register as an offset.
1911 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1912 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1916 * There is no variable part of the absolute offset, so
1917 * just do an absolute load.
1919 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1920 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1926 * Load a value relative to the beginning of the specified header.
1928 static struct slist
*
1929 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1932 struct slist
*s
, *s2
;
1935 * Squelch warnings from compilers that *don't* assume that
1936 * offrel always has a valid enum value and therefore don't
1937 * assume that we'll always go through one of the case arms.
1939 * If we have a default case, compilers that *do* assume that
1940 * will then complain about the default case code being
1943 * Damned if you do, damned if you don't.
1950 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1955 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1958 case OR_PREVLINKHDR
:
1959 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1963 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1966 case OR_PREVMPLSHDR
:
1967 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1971 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1974 case OR_LINKPL_NOSNAP
:
1975 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1979 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1984 * Load the X register with the length of the IPv4 header
1985 * (plus the offset of the link-layer header, if it's
1986 * preceded by a variable-length header such as a radio
1987 * header), in bytes.
1989 s
= gen_loadx_iphdrlen(cstate
);
1992 * Load the item at {offset of the link-layer payload} +
1993 * {offset, relative to the start of the link-layer
1994 * payload, of the IPv4 header} + {length of the IPv4 header} +
1995 * {specified offset}.
1997 * If the offset of the link-layer payload is variable,
1998 * the variable part of that offset is included in the
1999 * value in the X register, and we include the constant
2000 * part in the offset of the load.
2002 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2003 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2008 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2015 * Generate code to load into the X register the sum of the length of
2016 * the IPv4 header and the variable part of the offset of the link-layer
2019 static struct slist
*
2020 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2022 struct slist
*s
, *s2
;
2024 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2027 * The offset of the link-layer payload has a variable
2028 * part. "s" points to a list of statements that put
2029 * the variable part of that offset into the X register.
2031 * The 4*([k]&0xf) addressing mode can't be used, as we
2032 * don't have a constant offset, so we have to load the
2033 * value in question into the A register and add to it
2034 * the value from the X register.
2036 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2037 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2039 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2042 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2047 * The A register now contains the length of the IP header.
2048 * We need to add to it the variable part of the offset of
2049 * the link-layer payload, which is still in the X
2050 * register, and move the result into the X register.
2052 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2053 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2056 * The offset of the link-layer payload is a constant,
2057 * so no code was generated to load the (nonexistent)
2058 * variable part of that offset.
2060 * This means we can use the 4*([k]&0xf) addressing
2061 * mode. Load the length of the IPv4 header, which
2062 * is at an offset of cstate->off_nl from the beginning of
2063 * the link-layer payload, and thus at an offset of
2064 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2065 * of the raw packet data, using that addressing mode.
2067 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2068 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2074 static struct block
*
2075 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2080 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2082 b
= new_block(cstate
, JMP(BPF_JEQ
));
2088 static inline struct block
*
2089 gen_true(compiler_state_t
*cstate
)
2091 return gen_uncond(cstate
, 1);
2094 static inline struct block
*
2095 gen_false(compiler_state_t
*cstate
)
2097 return gen_uncond(cstate
, 0);
2101 * Byte-swap a 32-bit number.
2102 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
2103 * big-endian platforms.)
2105 #define SWAPLONG(y) \
2106 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
2109 * Generate code to match a particular packet type.
2111 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2112 * value, if <= ETHERMTU. We use that to determine whether to
2113 * match the type/length field or to check the type/length field for
2114 * a value <= ETHERMTU to see whether it's a type field and then do
2115 * the appropriate test.
2117 static struct block
*
2118 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2120 struct block
*b0
, *b1
;
2126 case LLCSAP_NETBEUI
:
2128 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2129 * so we check the DSAP and SSAP.
2131 * LLCSAP_IP checks for IP-over-802.2, rather
2132 * than IP-over-Ethernet or IP-over-SNAP.
2134 * XXX - should we check both the DSAP and the
2135 * SSAP, like this, or should we check just the
2136 * DSAP, as we do for other types <= ETHERMTU
2137 * (i.e., other SAP values)?
2139 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2141 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2149 * Ethernet_II frames, which are Ethernet
2150 * frames with a frame type of ETHERTYPE_IPX;
2152 * Ethernet_802.3 frames, which are 802.3
2153 * frames (i.e., the type/length field is
2154 * a length field, <= ETHERMTU, rather than
2155 * a type field) with the first two bytes
2156 * after the Ethernet/802.3 header being
2159 * Ethernet_802.2 frames, which are 802.3
2160 * frames with an 802.2 LLC header and
2161 * with the IPX LSAP as the DSAP in the LLC
2164 * Ethernet_SNAP frames, which are 802.3
2165 * frames with an LLC header and a SNAP
2166 * header and with an OUI of 0x000000
2167 * (encapsulated Ethernet) and a protocol
2168 * ID of ETHERTYPE_IPX in the SNAP header.
2170 * XXX - should we generate the same code both
2171 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2175 * This generates code to check both for the
2176 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2178 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2179 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2183 * Now we add code to check for SNAP frames with
2184 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2186 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2190 * Now we generate code to check for 802.3
2191 * frames in general.
2193 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2197 * Now add the check for 802.3 frames before the
2198 * check for Ethernet_802.2 and Ethernet_802.3,
2199 * as those checks should only be done on 802.3
2200 * frames, not on Ethernet frames.
2205 * Now add the check for Ethernet_II frames, and
2206 * do that before checking for the other frame
2209 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2213 case ETHERTYPE_ATALK
:
2214 case ETHERTYPE_AARP
:
2216 * EtherTalk (AppleTalk protocols on Ethernet link
2217 * layer) may use 802.2 encapsulation.
2221 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2222 * we check for an Ethernet type field less than
2223 * 1500, which means it's an 802.3 length field.
2225 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2229 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2230 * SNAP packets with an organization code of
2231 * 0x080007 (Apple, for Appletalk) and a protocol
2232 * type of ETHERTYPE_ATALK (Appletalk).
2234 * 802.2-encapsulated ETHERTYPE_AARP packets are
2235 * SNAP packets with an organization code of
2236 * 0x000000 (encapsulated Ethernet) and a protocol
2237 * type of ETHERTYPE_AARP (Appletalk ARP).
2239 if (ll_proto
== ETHERTYPE_ATALK
)
2240 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2241 else /* ll_proto == ETHERTYPE_AARP */
2242 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2246 * Check for Ethernet encapsulation (Ethertalk
2247 * phase 1?); we just check for the Ethernet
2250 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2256 if (ll_proto
<= ETHERMTU
) {
2258 * This is an LLC SAP value, so the frames
2259 * that match would be 802.2 frames.
2260 * Check that the frame is an 802.2 frame
2261 * (i.e., that the length/type field is
2262 * a length field, <= ETHERMTU) and
2263 * then check the DSAP.
2265 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2267 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2272 * This is an Ethernet type, so compare
2273 * the length/type field with it (if
2274 * the frame is an 802.2 frame, the length
2275 * field will be <= ETHERMTU, and, as
2276 * "ll_proto" is > ETHERMTU, this test
2277 * will fail and the frame won't match,
2278 * which is what we want).
2280 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2285 static struct block
*
2286 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2289 * For DLT_NULL, the link-layer header is a 32-bit word
2290 * containing an AF_ value in *host* byte order, and for
2291 * DLT_ENC, the link-layer header begins with a 32-bit
2292 * word containing an AF_ value in host byte order.
2294 * In addition, if we're reading a saved capture file,
2295 * the host byte order in the capture may not be the
2296 * same as the host byte order on this machine.
2298 * For DLT_LOOP, the link-layer header is a 32-bit
2299 * word containing an AF_ value in *network* byte order.
2301 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2303 * The AF_ value is in host byte order, but the BPF
2304 * interpreter will convert it to network byte order.
2306 * If this is a save file, and it's from a machine
2307 * with the opposite byte order to ours, we byte-swap
2310 * Then we run it through "htonl()", and generate
2311 * code to compare against the result.
2313 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2314 ll_proto
= SWAPLONG(ll_proto
);
2315 ll_proto
= htonl(ll_proto
);
2317 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2321 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2322 * or IPv6 then we have an error.
2324 static struct block
*
2325 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2330 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2333 case ETHERTYPE_IPV6
:
2334 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2341 return gen_false(cstate
);
2345 * Generate code to match a particular packet type.
2347 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2348 * value, if <= ETHERMTU. We use that to determine whether to
2349 * match the type field or to check the type field for the special
2350 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2352 static struct block
*
2353 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2355 struct block
*b0
, *b1
;
2361 case LLCSAP_NETBEUI
:
2363 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2364 * so we check the DSAP and SSAP.
2366 * LLCSAP_IP checks for IP-over-802.2, rather
2367 * than IP-over-Ethernet or IP-over-SNAP.
2369 * XXX - should we check both the DSAP and the
2370 * SSAP, like this, or should we check just the
2371 * DSAP, as we do for other types <= ETHERMTU
2372 * (i.e., other SAP values)?
2374 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2375 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2381 * Ethernet_II frames, which are Ethernet
2382 * frames with a frame type of ETHERTYPE_IPX;
2384 * Ethernet_802.3 frames, which have a frame
2385 * type of LINUX_SLL_P_802_3;
2387 * Ethernet_802.2 frames, which are 802.3
2388 * frames with an 802.2 LLC header (i.e, have
2389 * a frame type of LINUX_SLL_P_802_2) and
2390 * with the IPX LSAP as the DSAP in the LLC
2393 * Ethernet_SNAP frames, which are 802.3
2394 * frames with an LLC header and a SNAP
2395 * header and with an OUI of 0x000000
2396 * (encapsulated Ethernet) and a protocol
2397 * ID of ETHERTYPE_IPX in the SNAP header.
2399 * First, do the checks on LINUX_SLL_P_802_2
2400 * frames; generate the check for either
2401 * Ethernet_802.2 or Ethernet_SNAP frames, and
2402 * then put a check for LINUX_SLL_P_802_2 frames
2405 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2406 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2408 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2412 * Now check for 802.3 frames and OR that with
2413 * the previous test.
2415 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2419 * Now add the check for Ethernet_II frames, and
2420 * do that before checking for the other frame
2423 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2427 case ETHERTYPE_ATALK
:
2428 case ETHERTYPE_AARP
:
2430 * EtherTalk (AppleTalk protocols on Ethernet link
2431 * layer) may use 802.2 encapsulation.
2435 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2436 * we check for the 802.2 protocol type in the
2437 * "Ethernet type" field.
2439 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2442 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2443 * SNAP packets with an organization code of
2444 * 0x080007 (Apple, for Appletalk) and a protocol
2445 * type of ETHERTYPE_ATALK (Appletalk).
2447 * 802.2-encapsulated ETHERTYPE_AARP packets are
2448 * SNAP packets with an organization code of
2449 * 0x000000 (encapsulated Ethernet) and a protocol
2450 * type of ETHERTYPE_AARP (Appletalk ARP).
2452 if (ll_proto
== ETHERTYPE_ATALK
)
2453 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2454 else /* ll_proto == ETHERTYPE_AARP */
2455 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2459 * Check for Ethernet encapsulation (Ethertalk
2460 * phase 1?); we just check for the Ethernet
2463 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2469 if (ll_proto
<= ETHERMTU
) {
2471 * This is an LLC SAP value, so the frames
2472 * that match would be 802.2 frames.
2473 * Check for the 802.2 protocol type
2474 * in the "Ethernet type" field, and
2475 * then check the DSAP.
2477 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2478 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2484 * This is an Ethernet type, so compare
2485 * the length/type field with it (if
2486 * the frame is an 802.2 frame, the length
2487 * field will be <= ETHERMTU, and, as
2488 * "ll_proto" is > ETHERMTU, this test
2489 * will fail and the frame won't match,
2490 * which is what we want).
2492 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2498 * Load a value relative to the beginning of the link-layer header after the
2501 static struct slist
*
2502 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2504 struct slist
*s1
, *s2
;
2507 * Generate code to load the length of the pflog header into
2508 * the register assigned to hold that length, if one has been
2509 * assigned. (If one hasn't been assigned, no code we've
2510 * generated uses that prefix, so we don't need to generate any
2513 if (cstate
->off_linkpl
.reg
!= -1) {
2515 * The length is in the first byte of the header.
2517 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2521 * Round it up to a multiple of 4.
2522 * Add 3, and clear the lower 2 bits.
2524 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2527 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2528 s2
->s
.k
= 0xfffffffc;
2532 * Now allocate a register to hold that value and store
2535 s2
= new_stmt(cstate
, BPF_ST
);
2536 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2540 * Now move it into the X register.
2542 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2550 static struct slist
*
2551 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2553 struct slist
*s1
, *s2
;
2554 struct slist
*sjeq_avs_cookie
;
2555 struct slist
*sjcommon
;
2558 * This code is not compatible with the optimizer, as
2559 * we are generating jmp instructions within a normal
2560 * slist of instructions
2562 cstate
->no_optimize
= 1;
2565 * Generate code to load the length of the radio header into
2566 * the register assigned to hold that length, if one has been
2567 * assigned. (If one hasn't been assigned, no code we've
2568 * generated uses that prefix, so we don't need to generate any
2571 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2572 * or always use the AVS header rather than the Prism header.
2573 * We load a 4-byte big-endian value at the beginning of the
2574 * raw packet data, and see whether, when masked with 0xFFFFF000,
2575 * it's equal to 0x80211000. If so, that indicates that it's
2576 * an AVS header (the masked-out bits are the version number).
2577 * Otherwise, it's a Prism header.
2579 * XXX - the Prism header is also, in theory, variable-length,
2580 * but no known software generates headers that aren't 144
2583 if (cstate
->off_linkhdr
.reg
!= -1) {
2587 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2591 * AND it with 0xFFFFF000.
2593 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2594 s2
->s
.k
= 0xFFFFF000;
2598 * Compare with 0x80211000.
2600 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2601 sjeq_avs_cookie
->s
.k
= 0x80211000;
2602 sappend(s1
, sjeq_avs_cookie
);
2607 * The 4 bytes at an offset of 4 from the beginning of
2608 * the AVS header are the length of the AVS header.
2609 * That field is big-endian.
2611 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2614 sjeq_avs_cookie
->s
.jt
= s2
;
2617 * Now jump to the code to allocate a register
2618 * into which to save the header length and
2619 * store the length there. (The "jump always"
2620 * instruction needs to have the k field set;
2621 * it's added to the PC, so, as we're jumping
2622 * over a single instruction, it should be 1.)
2624 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2626 sappend(s1
, sjcommon
);
2629 * Now for the code that handles the Prism header.
2630 * Just load the length of the Prism header (144)
2631 * into the A register. Have the test for an AVS
2632 * header branch here if we don't have an AVS header.
2634 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2637 sjeq_avs_cookie
->s
.jf
= s2
;
2640 * Now allocate a register to hold that value and store
2641 * it. The code for the AVS header will jump here after
2642 * loading the length of the AVS header.
2644 s2
= new_stmt(cstate
, BPF_ST
);
2645 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2647 sjcommon
->s
.jf
= s2
;
2650 * Now move it into the X register.
2652 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2660 static struct slist
*
2661 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2663 struct slist
*s1
, *s2
;
2666 * Generate code to load the length of the AVS header into
2667 * the register assigned to hold that length, if one has been
2668 * assigned. (If one hasn't been assigned, no code we've
2669 * generated uses that prefix, so we don't need to generate any
2672 if (cstate
->off_linkhdr
.reg
!= -1) {
2674 * The 4 bytes at an offset of 4 from the beginning of
2675 * the AVS header are the length of the AVS header.
2676 * That field is big-endian.
2678 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2682 * Now allocate a register to hold that value and store
2685 s2
= new_stmt(cstate
, BPF_ST
);
2686 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2690 * Now move it into the X register.
2692 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2700 static struct slist
*
2701 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2703 struct slist
*s1
, *s2
;
2706 * Generate code to load the length of the radiotap header into
2707 * the register assigned to hold that length, if one has been
2708 * assigned. (If one hasn't been assigned, no code we've
2709 * generated uses that prefix, so we don't need to generate any
2712 if (cstate
->off_linkhdr
.reg
!= -1) {
2714 * The 2 bytes at offsets of 2 and 3 from the beginning
2715 * of the radiotap header are the length of the radiotap
2716 * header; unfortunately, it's little-endian, so we have
2717 * to load it a byte at a time and construct the value.
2721 * Load the high-order byte, at an offset of 3, shift it
2722 * left a byte, and put the result in the X register.
2724 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2726 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2729 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2733 * Load the next byte, at an offset of 2, and OR the
2734 * value from the X register into it.
2736 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2739 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2743 * Now allocate a register to hold that value and store
2746 s2
= new_stmt(cstate
, BPF_ST
);
2747 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2751 * Now move it into the X register.
2753 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2762 * At the moment we treat PPI as normal Radiotap encoded
2763 * packets. The difference is in the function that generates
2764 * the code at the beginning to compute the header length.
2765 * Since this code generator of PPI supports bare 802.11
2766 * encapsulation only (i.e. the encapsulated DLT should be
2767 * DLT_IEEE802_11) we generate code to check for this too;
2768 * that's done in finish_parse().
2770 static struct slist
*
2771 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2773 struct slist
*s1
, *s2
;
2776 * Generate code to load the length of the radiotap header
2777 * into the register assigned to hold that length, if one has
2780 if (cstate
->off_linkhdr
.reg
!= -1) {
2782 * The 2 bytes at offsets of 2 and 3 from the beginning
2783 * of the radiotap header are the length of the radiotap
2784 * header; unfortunately, it's little-endian, so we have
2785 * to load it a byte at a time and construct the value.
2789 * Load the high-order byte, at an offset of 3, shift it
2790 * left a byte, and put the result in the X register.
2792 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2794 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2797 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2801 * Load the next byte, at an offset of 2, and OR the
2802 * value from the X register into it.
2804 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2807 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2811 * Now allocate a register to hold that value and store
2814 s2
= new_stmt(cstate
, BPF_ST
);
2815 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2819 * Now move it into the X register.
2821 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2830 * Load a value relative to the beginning of the link-layer header after the 802.11
2831 * header, i.e. LLC_SNAP.
2832 * The link-layer header doesn't necessarily begin at the beginning
2833 * of the packet data; there might be a variable-length prefix containing
2834 * radio information.
2836 static struct slist
*
2837 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2840 struct slist
*sjset_data_frame_1
;
2841 struct slist
*sjset_data_frame_2
;
2842 struct slist
*sjset_qos
;
2843 struct slist
*sjset_radiotap_flags_present
;
2844 struct slist
*sjset_radiotap_ext_present
;
2845 struct slist
*sjset_radiotap_tsft_present
;
2846 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2847 struct slist
*s_roundup
;
2849 if (cstate
->off_linkpl
.reg
== -1) {
2851 * No register has been assigned to the offset of
2852 * the link-layer payload, which means nobody needs
2853 * it; don't bother computing it - just return
2854 * what we already have.
2860 * This code is not compatible with the optimizer, as
2861 * we are generating jmp instructions within a normal
2862 * slist of instructions
2864 cstate
->no_optimize
= 1;
2867 * If "s" is non-null, it has code to arrange that the X register
2868 * contains the length of the prefix preceding the link-layer
2871 * Otherwise, the length of the prefix preceding the link-layer
2872 * header is "off_outermostlinkhdr.constant_part".
2876 * There is no variable-length header preceding the
2877 * link-layer header.
2879 * Load the length of the fixed-length prefix preceding
2880 * the link-layer header (if any) into the X register,
2881 * and store it in the cstate->off_linkpl.reg register.
2882 * That length is off_outermostlinkhdr.constant_part.
2884 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2885 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2889 * The X register contains the offset of the beginning of the
2890 * link-layer header; add 24, which is the minimum length
2891 * of the MAC header for a data frame, to that, and store it
2892 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2893 * which is at the offset in the X register, with an indexed load.
2895 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2897 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2900 s2
= new_stmt(cstate
, BPF_ST
);
2901 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2904 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2909 * Check the Frame Control field to see if this is a data frame;
2910 * a data frame has the 0x08 bit (b3) in that field set and the
2911 * 0x04 bit (b2) clear.
2913 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2914 sjset_data_frame_1
->s
.k
= 0x08;
2915 sappend(s
, sjset_data_frame_1
);
2918 * If b3 is set, test b2, otherwise go to the first statement of
2919 * the rest of the program.
2921 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2922 sjset_data_frame_2
->s
.k
= 0x04;
2923 sappend(s
, sjset_data_frame_2
);
2924 sjset_data_frame_1
->s
.jf
= snext
;
2927 * If b2 is not set, this is a data frame; test the QoS bit.
2928 * Otherwise, go to the first statement of the rest of the
2931 sjset_data_frame_2
->s
.jt
= snext
;
2932 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2933 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2934 sappend(s
, sjset_qos
);
2937 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2939 * Otherwise, go to the first statement of the rest of the
2942 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2943 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2945 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2948 s2
= new_stmt(cstate
, BPF_ST
);
2949 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2953 * If we have a radiotap header, look at it to see whether
2954 * there's Atheros padding between the MAC-layer header
2957 * Note: all of the fields in the radiotap header are
2958 * little-endian, so we byte-swap all of the values
2959 * we test against, as they will be loaded as big-endian
2962 * XXX - in the general case, we would have to scan through
2963 * *all* the presence bits, if there's more than one word of
2964 * presence bits. That would require a loop, meaning that
2965 * we wouldn't be able to run the filter in the kernel.
2967 * We assume here that the Atheros adapters that insert the
2968 * annoying padding don't have multiple antennae and therefore
2969 * do not generate radiotap headers with multiple presence words.
2971 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2973 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2974 * in the first presence flag word?
2976 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2980 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2981 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2982 sappend(s
, sjset_radiotap_flags_present
);
2985 * If not, skip all of this.
2987 sjset_radiotap_flags_present
->s
.jf
= snext
;
2990 * Otherwise, is the "extension" bit set in that word?
2992 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2993 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2994 sappend(s
, sjset_radiotap_ext_present
);
2995 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2998 * If so, skip all of this.
3000 sjset_radiotap_ext_present
->s
.jt
= snext
;
3003 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3005 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3006 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3007 sappend(s
, sjset_radiotap_tsft_present
);
3008 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3011 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3012 * at an offset of 16 from the beginning of the raw packet
3013 * data (8 bytes for the radiotap header and 8 bytes for
3016 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3019 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3022 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3024 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3025 sjset_tsft_datapad
->s
.k
= 0x20;
3026 sappend(s
, sjset_tsft_datapad
);
3029 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3030 * at an offset of 8 from the beginning of the raw packet
3031 * data (8 bytes for the radiotap header).
3033 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3036 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3039 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3041 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3042 sjset_notsft_datapad
->s
.k
= 0x20;
3043 sappend(s
, sjset_notsft_datapad
);
3046 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3047 * set, round the length of the 802.11 header to
3048 * a multiple of 4. Do that by adding 3 and then
3049 * dividing by and multiplying by 4, which we do by
3052 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3053 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3054 sappend(s
, s_roundup
);
3055 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3058 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3059 s2
->s
.k
= (bpf_u_int32
)~3;
3061 s2
= new_stmt(cstate
, BPF_ST
);
3062 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3065 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3066 sjset_tsft_datapad
->s
.jf
= snext
;
3067 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3068 sjset_notsft_datapad
->s
.jf
= snext
;
3070 sjset_qos
->s
.jf
= snext
;
3076 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3080 /* There is an implicit dependency between the link
3081 * payload and link header since the payload computation
3082 * includes the variable part of the header. Therefore,
3083 * if nobody else has allocated a register for the link
3084 * header and we need it, do it now. */
3085 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3086 cstate
->off_linkhdr
.reg
== -1)
3087 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3090 * For link-layer types that have a variable-length header
3091 * preceding the link-layer header, generate code to load
3092 * the offset of the link-layer header into the register
3093 * assigned to that offset, if any.
3095 * XXX - this, and the next switch statement, won't handle
3096 * encapsulation of 802.11 or 802.11+radio information in
3097 * some other protocol stack. That's significantly more
3100 switch (cstate
->outermostlinktype
) {
3102 case DLT_PRISM_HEADER
:
3103 s
= gen_load_prism_llprefixlen(cstate
);
3106 case DLT_IEEE802_11_RADIO_AVS
:
3107 s
= gen_load_avs_llprefixlen(cstate
);
3110 case DLT_IEEE802_11_RADIO
:
3111 s
= gen_load_radiotap_llprefixlen(cstate
);
3115 s
= gen_load_ppi_llprefixlen(cstate
);
3124 * For link-layer types that have a variable-length link-layer
3125 * header, generate code to load the offset of the link-layer
3126 * payload into the register assigned to that offset, if any.
3128 switch (cstate
->outermostlinktype
) {
3130 case DLT_IEEE802_11
:
3131 case DLT_PRISM_HEADER
:
3132 case DLT_IEEE802_11_RADIO_AVS
:
3133 case DLT_IEEE802_11_RADIO
:
3135 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3139 s
= gen_load_pflog_llprefixlen(cstate
);
3144 * If there is no initialization yet and we need variable
3145 * length offsets for VLAN, initialize them to zero
3147 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3150 if (cstate
->off_linkpl
.reg
== -1)
3151 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3152 if (cstate
->off_linktype
.reg
== -1)
3153 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3155 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3157 s2
= new_stmt(cstate
, BPF_ST
);
3158 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3160 s2
= new_stmt(cstate
, BPF_ST
);
3161 s2
->s
.k
= cstate
->off_linktype
.reg
;
3166 * If we have any offset-loading code, append all the
3167 * existing statements in the block to those statements,
3168 * and make the resulting list the list of statements
3172 sappend(s
, b
->stmts
);
3177 static struct block
*
3178 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3180 struct slist
*s_load_dlt
;
3183 if (cstate
->linktype
== DLT_PPI
)
3185 /* Create the statements that check for the DLT
3187 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3188 s_load_dlt
->s
.k
= 4;
3190 b
= new_block(cstate
, JMP(BPF_JEQ
));
3192 b
->stmts
= s_load_dlt
;
3193 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3204 * Take an absolute offset, and:
3206 * if it has no variable part, return NULL;
3208 * if it has a variable part, generate code to load the register
3209 * containing that variable part into the X register, returning
3210 * a pointer to that code - if no register for that offset has
3211 * been allocated, allocate it first.
3213 * (The code to set that register will be generated later, but will
3214 * be placed earlier in the code sequence.)
3216 static struct slist
*
3217 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3221 if (off
->is_variable
) {
3222 if (off
->reg
== -1) {
3224 * We haven't yet assigned a register for the
3225 * variable part of the offset of the link-layer
3226 * header; allocate one.
3228 off
->reg
= alloc_reg(cstate
);
3232 * Load the register containing the variable part of the
3233 * offset of the link-layer header into the X register.
3235 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3240 * That offset isn't variable, there's no variable part,
3241 * so we don't need to generate any code.
3248 * Map an Ethernet type to the equivalent PPP type.
3251 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3259 case ETHERTYPE_IPV6
:
3260 ll_proto
= PPP_IPV6
;
3264 ll_proto
= PPP_DECNET
;
3267 case ETHERTYPE_ATALK
:
3268 ll_proto
= PPP_APPLE
;
3281 * I'm assuming the "Bridging PDU"s that go
3282 * over PPP are Spanning Tree Protocol
3285 ll_proto
= PPP_BRPDU
;
3296 * Generate any tests that, for encapsulation of a link-layer packet
3297 * inside another protocol stack, need to be done to check for those
3298 * link-layer packets (and that haven't already been done by a check
3299 * for that encapsulation).
3301 static struct block
*
3302 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3306 if (cstate
->is_encap
)
3307 return gen_encap_ll_check(cstate
);
3309 switch (cstate
->prevlinktype
) {
3313 * This is LANE-encapsulated Ethernet; check that the LANE
3314 * packet doesn't begin with an LE Control marker, i.e.
3315 * that it's data, not a control message.
3317 * (We've already generated a test for LANE.)
3319 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3325 * No such tests are necessary.
3333 * The three different values we should check for when checking for an
3334 * IPv6 packet with DLT_NULL.
3336 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3337 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3338 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3341 * Generate code to match a particular packet type by matching the
3342 * link-layer type field or fields in the 802.2 LLC header.
3344 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3345 * value, if <= ETHERMTU.
3347 static struct block
*
3348 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3350 struct block
*b0
, *b1
, *b2
;
3351 const char *description
;
3353 /* are we checking MPLS-encapsulated packets? */
3354 if (cstate
->label_stack_depth
> 0)
3355 return gen_mpls_linktype(cstate
, ll_proto
);
3357 switch (cstate
->linktype
) {
3360 case DLT_NETANALYZER
:
3361 case DLT_NETANALYZER_TRANSPARENT
:
3362 /* Geneve has an EtherType regardless of whether there is an
3363 * L2 header. VXLAN always has an EtherType. */
3364 if (!cstate
->is_encap
)
3365 b0
= gen_prevlinkhdr_check(cstate
);
3369 b1
= gen_ether_linktype(cstate
, ll_proto
);
3380 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3384 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3388 case DLT_IEEE802_11
:
3389 case DLT_PRISM_HEADER
:
3390 case DLT_IEEE802_11_RADIO_AVS
:
3391 case DLT_IEEE802_11_RADIO
:
3394 * Check that we have a data frame.
3396 b0
= gen_check_802_11_data_frame(cstate
);
3399 * Now check for the specified link-layer type.
3401 b1
= gen_llc_linktype(cstate
, ll_proto
);
3408 * XXX - check for LLC frames.
3410 return gen_llc_linktype(cstate
, ll_proto
);
3415 * XXX - check for LLC PDUs, as per IEEE 802.5.
3417 return gen_llc_linktype(cstate
, ll_proto
);
3420 case DLT_ATM_RFC1483
:
3422 case DLT_IP_OVER_FC
:
3423 return gen_llc_linktype(cstate
, ll_proto
);
3428 * Check for an LLC-encapsulated version of this protocol;
3429 * if we were checking for LANE, linktype would no longer
3432 * Check for LLC encapsulation and then check the protocol.
3434 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3435 b1
= gen_llc_linktype(cstate
, ll_proto
);
3441 return gen_linux_sll_linktype(cstate
, ll_proto
);
3445 case DLT_SLIP_BSDOS
:
3448 * These types don't provide any type field; packets
3449 * are always IPv4 or IPv6.
3451 * XXX - for IPv4, check for a version number of 4, and,
3452 * for IPv6, check for a version number of 6?
3457 /* Check for a version number of 4. */
3458 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3460 case ETHERTYPE_IPV6
:
3461 /* Check for a version number of 6. */
3462 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3465 return gen_false(cstate
); /* always false */
3471 * Raw IPv4, so no type field.
3473 if (ll_proto
== ETHERTYPE_IP
)
3474 return gen_true(cstate
); /* always true */
3476 /* Checking for something other than IPv4; always false */
3477 return gen_false(cstate
);
3482 * Raw IPv6, so no type field.
3484 if (ll_proto
== ETHERTYPE_IPV6
)
3485 return gen_true(cstate
); /* always true */
3487 /* Checking for something other than IPv6; always false */
3488 return gen_false(cstate
);
3493 case DLT_PPP_SERIAL
:
3496 * We use Ethernet protocol types inside libpcap;
3497 * map them to the corresponding PPP protocol types.
3499 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3500 ethertype_to_ppptype(ll_proto
));
3505 * We use Ethernet protocol types inside libpcap;
3506 * map them to the corresponding PPP protocol types.
3512 * Also check for Van Jacobson-compressed IP.
3513 * XXX - do this for other forms of PPP?
3515 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3516 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3518 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3523 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3524 ethertype_to_ppptype(ll_proto
));
3534 return (gen_loopback_linktype(cstate
, AF_INET
));
3536 case ETHERTYPE_IPV6
:
3538 * AF_ values may, unfortunately, be platform-
3539 * dependent; AF_INET isn't, because everybody
3540 * used 4.2BSD's value, but AF_INET6 is, because
3541 * 4.2BSD didn't have a value for it (given that
3542 * IPv6 didn't exist back in the early 1980's),
3543 * and they all picked their own values.
3545 * This means that, if we're reading from a
3546 * savefile, we need to check for all the
3549 * If we're doing a live capture, we only need
3550 * to check for this platform's value; however,
3551 * Npcap uses 24, which isn't Windows's AF_INET6
3552 * value. (Given the multiple different values,
3553 * programs that read pcap files shouldn't be
3554 * checking for their platform's AF_INET6 value
3555 * anyway, they should check for all of the
3556 * possible values. and they might as well do
3557 * that even for live captures.)
3559 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3561 * Savefile - check for all three
3562 * possible IPv6 values.
3564 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3565 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3567 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3572 * Live capture, so we only need to
3573 * check for the value used on this
3578 * Npcap doesn't use Windows's AF_INET6,
3579 * as that collides with AF_IPX on
3580 * some BSDs (both have the value 23).
3581 * Instead, it uses 24.
3583 return (gen_loopback_linktype(cstate
, 24));
3586 return (gen_loopback_linktype(cstate
, AF_INET6
));
3587 #else /* AF_INET6 */
3589 * I guess this platform doesn't support
3590 * IPv6, so we just reject all packets.
3592 return gen_false(cstate
);
3593 #endif /* AF_INET6 */
3599 * Not a type on which we support filtering.
3600 * XXX - support those that have AF_ values
3601 * #defined on this platform, at least?
3603 return gen_false(cstate
);
3608 * af field is host byte order in contrast to the rest of
3611 if (ll_proto
== ETHERTYPE_IP
)
3612 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3614 else if (ll_proto
== ETHERTYPE_IPV6
)
3615 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3618 return gen_false(cstate
);
3622 case DLT_ARCNET_LINUX
:
3624 * XXX should we check for first fragment if the protocol
3630 return gen_false(cstate
);
3632 case ETHERTYPE_IPV6
:
3633 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3637 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3639 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3645 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3647 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3652 case ETHERTYPE_REVARP
:
3653 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3656 case ETHERTYPE_ATALK
:
3657 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3664 case ETHERTYPE_ATALK
:
3665 return gen_true(cstate
);
3667 return gen_false(cstate
);
3673 * XXX - assumes a 2-byte Frame Relay header with
3674 * DLCI and flags. What if the address is longer?
3680 * Check for the special NLPID for IP.
3682 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3684 case ETHERTYPE_IPV6
:
3686 * Check for the special NLPID for IPv6.
3688 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3692 * Check for several OSI protocols.
3694 * Frame Relay packets typically have an OSI
3695 * NLPID at the beginning; we check for each
3698 * What we check for is the NLPID and a frame
3699 * control field of UI, i.e. 0x03 followed
3702 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3703 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3704 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3710 return gen_false(cstate
);
3715 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3717 case DLT_JUNIPER_MFR
:
3718 case DLT_JUNIPER_MLFR
:
3719 case DLT_JUNIPER_MLPPP
:
3720 case DLT_JUNIPER_ATM1
:
3721 case DLT_JUNIPER_ATM2
:
3722 case DLT_JUNIPER_PPPOE
:
3723 case DLT_JUNIPER_PPPOE_ATM
:
3724 case DLT_JUNIPER_GGSN
:
3725 case DLT_JUNIPER_ES
:
3726 case DLT_JUNIPER_MONITOR
:
3727 case DLT_JUNIPER_SERVICES
:
3728 case DLT_JUNIPER_ETHER
:
3729 case DLT_JUNIPER_PPP
:
3730 case DLT_JUNIPER_FRELAY
:
3731 case DLT_JUNIPER_CHDLC
:
3732 case DLT_JUNIPER_VP
:
3733 case DLT_JUNIPER_ST
:
3734 case DLT_JUNIPER_ISM
:
3735 case DLT_JUNIPER_VS
:
3736 case DLT_JUNIPER_SRX_E2E
:
3737 case DLT_JUNIPER_FIBRECHANNEL
:
3738 case DLT_JUNIPER_ATM_CEMIC
:
3740 /* just lets verify the magic number for now -
3741 * on ATM we may have up to 6 different encapsulations on the wire
3742 * and need a lot of heuristics to figure out that the payload
3745 * FIXME encapsulation specific BPF_ filters
3747 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3749 case DLT_BACNET_MS_TP
:
3750 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3753 return gen_ipnet_linktype(cstate
, ll_proto
);
3755 case DLT_LINUX_IRDA
:
3756 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3759 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3762 case DLT_MTP2_WITH_PHDR
:
3763 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3766 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3769 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3771 case DLT_LINUX_LAPD
:
3772 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3774 case DLT_USB_FREEBSD
:
3776 case DLT_USB_LINUX_MMAPPED
:
3778 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3780 case DLT_BLUETOOTH_HCI_H4
:
3781 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3782 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3785 case DLT_CAN_SOCKETCAN
:
3786 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3788 case DLT_IEEE802_15_4
:
3789 case DLT_IEEE802_15_4_LINUX
:
3790 case DLT_IEEE802_15_4_NONASK_PHY
:
3791 case DLT_IEEE802_15_4_NOFCS
:
3792 case DLT_IEEE802_15_4_TAP
:
3793 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3795 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3796 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3799 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3802 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3804 case DLT_IPMB_KONTRON
:
3805 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3808 bpf_error(cstate
, "I2C link-layer type filtering not implemented");
3811 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3814 /* Using the fixed-size NFLOG header it is possible to tell only
3815 * the address family of the packet, other meaningful data is
3816 * either missing or behind TLVs.
3818 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3822 * Does this link-layer header type have a field
3823 * indicating the type of the next protocol? If
3824 * so, off_linktype.constant_part will be the offset of that
3825 * field in the packet; if not, it will be OFFSET_NOT_SET.
3827 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3829 * Yes; assume it's an Ethernet type. (If
3830 * it's not, it needs to be handled specially
3833 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3837 * No; report an error.
3839 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3840 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3848 * Check for an LLC SNAP packet with a given organization code and
3849 * protocol type; we check the entire contents of the 802.2 LLC and
3850 * snap headers, checking for DSAP and SSAP of SNAP and a control
3851 * field of 0x03 in the LLC header, and for the specified organization
3852 * code and protocol type in the SNAP header.
3854 static struct block
*
3855 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3857 u_char snapblock
[8];
3859 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3860 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3861 snapblock
[2] = 0x03; /* control = UI */
3862 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3863 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3864 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3865 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3866 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3867 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3871 * Generate code to match frames with an LLC header.
3873 static struct block
*
3874 gen_llc_internal(compiler_state_t
*cstate
)
3876 struct block
*b0
, *b1
;
3878 switch (cstate
->linktype
) {
3882 * We check for an Ethernet type field less than
3883 * 1500, which means it's an 802.3 length field.
3885 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3889 * Now check for the purported DSAP and SSAP not being
3890 * 0xFF, to rule out NetWare-over-802.3.
3892 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3899 * We check for LLC traffic.
3901 b0
= gen_atmtype_llc(cstate
);
3904 case DLT_IEEE802
: /* Token Ring */
3906 * XXX - check for LLC frames.
3908 return gen_true(cstate
);
3912 * XXX - check for LLC frames.
3914 return gen_true(cstate
);
3916 case DLT_ATM_RFC1483
:
3918 * For LLC encapsulation, these are defined to have an
3921 * For VC encapsulation, they don't, but there's no
3922 * way to check for that; the protocol used on the VC
3923 * is negotiated out of band.
3925 return gen_true(cstate
);
3927 case DLT_IEEE802_11
:
3928 case DLT_PRISM_HEADER
:
3929 case DLT_IEEE802_11_RADIO
:
3930 case DLT_IEEE802_11_RADIO_AVS
:
3933 * Check that we have a data frame.
3935 b0
= gen_check_802_11_data_frame(cstate
);
3939 bpf_error(cstate
, "'llc' not supported for %s",
3940 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3946 gen_llc(compiler_state_t
*cstate
)
3949 * Catch errors reported by us and routines below us, and return NULL
3952 if (setjmp(cstate
->top_ctx
))
3955 return gen_llc_internal(cstate
);
3959 gen_llc_i(compiler_state_t
*cstate
)
3961 struct block
*b0
, *b1
;
3965 * Catch errors reported by us and routines below us, and return NULL
3968 if (setjmp(cstate
->top_ctx
))
3972 * Check whether this is an LLC frame.
3974 b0
= gen_llc_internal(cstate
);
3977 * Load the control byte and test the low-order bit; it must
3978 * be clear for I frames.
3980 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3981 b1
= new_block(cstate
, JMP(BPF_JSET
));
3990 gen_llc_s(compiler_state_t
*cstate
)
3992 struct block
*b0
, *b1
;
3995 * Catch errors reported by us and routines below us, and return NULL
3998 if (setjmp(cstate
->top_ctx
))
4002 * Check whether this is an LLC frame.
4004 b0
= gen_llc_internal(cstate
);
4007 * Now compare the low-order 2 bit of the control byte against
4008 * the appropriate value for S frames.
4010 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4016 gen_llc_u(compiler_state_t
*cstate
)
4018 struct block
*b0
, *b1
;
4021 * Catch errors reported by us and routines below us, and return NULL
4024 if (setjmp(cstate
->top_ctx
))
4028 * Check whether this is an LLC frame.
4030 b0
= gen_llc_internal(cstate
);
4033 * Now compare the low-order 2 bit of the control byte against
4034 * the appropriate value for U frames.
4036 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4042 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4044 struct block
*b0
, *b1
;
4047 * Catch errors reported by us and routines below us, and return NULL
4050 if (setjmp(cstate
->top_ctx
))
4054 * Check whether this is an LLC frame.
4056 b0
= gen_llc_internal(cstate
);
4059 * Now check for an S frame with the appropriate type.
4061 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4067 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4069 struct block
*b0
, *b1
;
4072 * Catch errors reported by us and routines below us, and return NULL
4075 if (setjmp(cstate
->top_ctx
))
4079 * Check whether this is an LLC frame.
4081 b0
= gen_llc_internal(cstate
);
4084 * Now check for a U frame with the appropriate type.
4086 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4092 * Generate code to match a particular packet type, for link-layer types
4093 * using 802.2 LLC headers.
4095 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4096 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4098 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4099 * value, if <= ETHERMTU. We use that to determine whether to
4100 * match the DSAP or both DSAP and LSAP or to check the OUI and
4101 * protocol ID in a SNAP header.
4103 static struct block
*
4104 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4107 * XXX - handle token-ring variable-length header.
4113 case LLCSAP_NETBEUI
:
4115 * XXX - should we check both the DSAP and the
4116 * SSAP, like this, or should we check just the
4117 * DSAP, as we do for other SAP values?
4119 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4120 ((ll_proto
<< 8) | ll_proto
));
4124 * XXX - are there ever SNAP frames for IPX on
4125 * non-Ethernet 802.x networks?
4127 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4129 case ETHERTYPE_ATALK
:
4131 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4132 * SNAP packets with an organization code of
4133 * 0x080007 (Apple, for Appletalk) and a protocol
4134 * type of ETHERTYPE_ATALK (Appletalk).
4136 * XXX - check for an organization code of
4137 * encapsulated Ethernet as well?
4139 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4143 * XXX - we don't have to check for IPX 802.3
4144 * here, but should we check for the IPX Ethertype?
4146 if (ll_proto
<= ETHERMTU
) {
4148 * This is an LLC SAP value, so check
4151 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4154 * This is an Ethernet type; we assume that it's
4155 * unlikely that it'll appear in the right place
4156 * at random, and therefore check only the
4157 * location that would hold the Ethernet type
4158 * in a SNAP frame with an organization code of
4159 * 0x000000 (encapsulated Ethernet).
4161 * XXX - if we were to check for the SNAP DSAP and
4162 * LSAP, as per XXX, and were also to check for an
4163 * organization code of 0x000000 (encapsulated
4164 * Ethernet), we'd do
4166 * return gen_snap(cstate, 0x000000, ll_proto);
4168 * here; for now, we don't, as per the above.
4169 * I don't know whether it's worth the extra CPU
4170 * time to do the right check or not.
4172 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4177 static struct block
*
4178 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4179 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4181 struct block
*b0
, *b1
;
4195 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4196 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4202 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4203 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4208 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4212 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4216 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4220 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4224 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4228 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4235 b0
= gen_linktype(cstate
, ll_proto
);
4236 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4242 static struct block
*
4243 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4244 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4247 struct block
*b0
, *b1
;
4250 * Code below needs to access four separate 32-bit parts of the 128-bit
4251 * IPv6 address and mask. In some OSes this is as simple as using the
4252 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4253 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4254 * far as libpcap sees it. Hence copy the data before use to avoid
4255 * potential unaligned memory access and the associated compiler
4256 * warnings (whether genuine or not).
4258 bpf_u_int32 a
[4], m
[4];
4271 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4272 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4278 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4279 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4284 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4288 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4292 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4296 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4300 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4304 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4311 /* this order is important */
4312 memcpy(a
, addr
, sizeof(a
));
4313 memcpy(m
, mask
, sizeof(m
));
4314 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4315 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4317 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4319 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4321 b0
= gen_linktype(cstate
, ll_proto
);
4327 static struct block
*
4328 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4330 register struct block
*b0
, *b1
;
4334 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4337 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4340 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4341 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4347 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4348 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4353 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4357 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4361 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4365 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4369 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4373 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4381 * Like gen_ehostop, but for DLT_FDDI
4383 static struct block
*
4384 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4386 struct block
*b0
, *b1
;
4390 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4393 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4396 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4397 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4403 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4404 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4409 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4413 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4417 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4421 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4425 bpf_error(cstate
, "'ra' is only supported on 802.11");
4429 bpf_error(cstate
, "'ta' is only supported on 802.11");
4437 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4439 static struct block
*
4440 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4442 register struct block
*b0
, *b1
;
4446 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4449 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4452 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4453 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4459 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4460 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4465 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4469 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4473 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4477 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4481 bpf_error(cstate
, "'ra' is only supported on 802.11");
4485 bpf_error(cstate
, "'ta' is only supported on 802.11");
4493 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4494 * various 802.11 + radio headers.
4496 static struct block
*
4497 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4499 register struct block
*b0
, *b1
, *b2
;
4500 register struct slist
*s
;
4502 #ifdef ENABLE_WLAN_FILTERING_PATCH
4505 * We need to disable the optimizer because the optimizer is buggy
4506 * and wipes out some LD instructions generated by the below
4507 * code to validate the Frame Control bits
4509 cstate
->no_optimize
= 1;
4510 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4517 * For control frames, there is no SA.
4519 * For management frames, SA is at an
4520 * offset of 10 from the beginning of
4523 * For data frames, SA is at an offset
4524 * of 10 from the beginning of the packet
4525 * if From DS is clear, at an offset of
4526 * 16 from the beginning of the packet
4527 * if From DS is set and To DS is clear,
4528 * and an offset of 24 from the beginning
4529 * of the packet if From DS is set and To DS
4534 * Generate the tests to be done for data frames
4537 * First, check for To DS set, i.e. check "link[1] & 0x01".
4539 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4540 b1
= new_block(cstate
, JMP(BPF_JSET
));
4541 b1
->s
.k
= 0x01; /* To DS */
4545 * If To DS is set, the SA is at 24.
4547 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4551 * Now, check for To DS not set, i.e. check
4552 * "!(link[1] & 0x01)".
4554 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4555 b2
= new_block(cstate
, JMP(BPF_JSET
));
4556 b2
->s
.k
= 0x01; /* To DS */
4561 * If To DS is not set, the SA is at 16.
4563 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4567 * Now OR together the last two checks. That gives
4568 * the complete set of checks for data frames with
4574 * Now check for From DS being set, and AND that with
4575 * the ORed-together checks.
4577 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4578 b1
= new_block(cstate
, JMP(BPF_JSET
));
4579 b1
->s
.k
= 0x02; /* From DS */
4584 * Now check for data frames with From DS not set.
4586 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4587 b2
= new_block(cstate
, JMP(BPF_JSET
));
4588 b2
->s
.k
= 0x02; /* From DS */
4593 * If From DS isn't set, the SA is at 10.
4595 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4599 * Now OR together the checks for data frames with
4600 * From DS not set and for data frames with From DS
4601 * set; that gives the checks done for data frames.
4606 * Now check for a data frame.
4607 * I.e, check "link[0] & 0x08".
4609 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4610 b1
= new_block(cstate
, JMP(BPF_JSET
));
4615 * AND that with the checks done for data frames.
4620 * If the high-order bit of the type value is 0, this
4621 * is a management frame.
4622 * I.e, check "!(link[0] & 0x08)".
4624 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4625 b2
= new_block(cstate
, JMP(BPF_JSET
));
4631 * For management frames, the SA is at 10.
4633 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4637 * OR that with the checks done for data frames.
4638 * That gives the checks done for management and
4644 * If the low-order bit of the type value is 1,
4645 * this is either a control frame or a frame
4646 * with a reserved type, and thus not a
4649 * I.e., check "!(link[0] & 0x04)".
4651 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4652 b1
= new_block(cstate
, JMP(BPF_JSET
));
4658 * AND that with the checks for data and management
4668 * For control frames, there is no DA.
4670 * For management frames, DA is at an
4671 * offset of 4 from the beginning of
4674 * For data frames, DA is at an offset
4675 * of 4 from the beginning of the packet
4676 * if To DS is clear and at an offset of
4677 * 16 from the beginning of the packet
4682 * Generate the tests to be done for data frames.
4684 * First, check for To DS set, i.e. "link[1] & 0x01".
4686 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4687 b1
= new_block(cstate
, JMP(BPF_JSET
));
4688 b1
->s
.k
= 0x01; /* To DS */
4692 * If To DS is set, the DA is at 16.
4694 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4698 * Now, check for To DS not set, i.e. check
4699 * "!(link[1] & 0x01)".
4701 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4702 b2
= new_block(cstate
, JMP(BPF_JSET
));
4703 b2
->s
.k
= 0x01; /* To DS */
4708 * If To DS is not set, the DA is at 4.
4710 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4714 * Now OR together the last two checks. That gives
4715 * the complete set of checks for data frames.
4720 * Now check for a data frame.
4721 * I.e, check "link[0] & 0x08".
4723 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4724 b1
= new_block(cstate
, JMP(BPF_JSET
));
4729 * AND that with the checks done for data frames.
4734 * If the high-order bit of the type value is 0, this
4735 * is a management frame.
4736 * I.e, check "!(link[0] & 0x08)".
4738 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4739 b2
= new_block(cstate
, JMP(BPF_JSET
));
4745 * For management frames, the DA is at 4.
4747 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4751 * OR that with the checks done for data frames.
4752 * That gives the checks done for management and
4758 * If the low-order bit of the type value is 1,
4759 * this is either a control frame or a frame
4760 * with a reserved type, and thus not a
4763 * I.e., check "!(link[0] & 0x04)".
4765 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4766 b1
= new_block(cstate
, JMP(BPF_JSET
));
4772 * AND that with the checks for data and management
4779 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4780 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4786 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4787 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4792 * XXX - add BSSID keyword?
4795 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4799 * Not present in CTS or ACK control frames.
4801 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4802 IEEE80211_FC0_TYPE_MASK
);
4804 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4805 IEEE80211_FC0_SUBTYPE_MASK
);
4807 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4808 IEEE80211_FC0_SUBTYPE_MASK
);
4812 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4818 * Not present in control frames.
4820 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4821 IEEE80211_FC0_TYPE_MASK
);
4823 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4829 * Present only if the direction mask has both "From DS"
4830 * and "To DS" set. Neither control frames nor management
4831 * frames should have both of those set, so we don't
4832 * check the frame type.
4834 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4835 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4836 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4842 * Not present in management frames; addr1 in other
4847 * If the high-order bit of the type value is 0, this
4848 * is a management frame.
4849 * I.e, check "(link[0] & 0x08)".
4851 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4852 b1
= new_block(cstate
, JMP(BPF_JSET
));
4859 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4862 * AND that with the check of addr1.
4869 * Not present in management frames; addr2, if present,
4874 * Not present in CTS or ACK control frames.
4876 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4877 IEEE80211_FC0_TYPE_MASK
);
4879 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4880 IEEE80211_FC0_SUBTYPE_MASK
);
4882 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4883 IEEE80211_FC0_SUBTYPE_MASK
);
4889 * If the high-order bit of the type value is 0, this
4890 * is a management frame.
4891 * I.e, check "(link[0] & 0x08)".
4893 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4894 b1
= new_block(cstate
, JMP(BPF_JSET
));
4899 * AND that with the check for frames other than
4900 * CTS and ACK frames.
4907 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4916 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4917 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4918 * as the RFC states.)
4920 static struct block
*
4921 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4923 register struct block
*b0
, *b1
;
4927 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4930 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4933 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4934 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4940 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4941 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4946 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4950 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4954 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4958 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4962 bpf_error(cstate
, "'ra' is only supported on 802.11");
4966 bpf_error(cstate
, "'ta' is only supported on 802.11");
4974 * This is quite tricky because there may be pad bytes in front of the
4975 * DECNET header, and then there are two possible data packet formats that
4976 * carry both src and dst addresses, plus 5 packet types in a format that
4977 * carries only the src node, plus 2 types that use a different format and
4978 * also carry just the src node.
4982 * Instead of doing those all right, we just look for data packets with
4983 * 0 or 1 bytes of padding. If you want to look at other packets, that
4984 * will require a lot more hacking.
4986 * To add support for filtering on DECNET "areas" (network numbers)
4987 * one would want to add a "mask" argument to this routine. That would
4988 * make the filter even more inefficient, although one could be clever
4989 * and not generate masking instructions if the mask is 0xFFFF.
4991 static struct block
*
4992 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4994 struct block
*b0
, *b1
, *b2
, *tmp
;
4995 u_int offset_lh
; /* offset if long header is received */
4996 u_int offset_sh
; /* offset if short header is received */
5001 offset_sh
= 1; /* follows flags */
5002 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5006 offset_sh
= 3; /* follows flags, dstnode */
5007 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5011 /* Inefficient because we do our Calvinball dance twice */
5012 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5013 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5019 /* Inefficient because we do our Calvinball dance twice */
5020 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5021 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5026 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5030 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5034 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5038 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5042 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5046 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5053 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5054 /* Check for pad = 1, long header case */
5055 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
5056 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
5057 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5058 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
5060 /* Check for pad = 0, long header case */
5061 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
5063 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5064 (bpf_u_int32
)ntohs((u_short
)addr
));
5067 /* Check for pad = 1, short header case */
5068 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
5069 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
5070 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5071 (bpf_u_int32
)ntohs((u_short
)addr
));
5074 /* Check for pad = 0, short header case */
5075 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
5077 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5078 (bpf_u_int32
)ntohs((u_short
)addr
));
5082 /* Combine with test for cstate->linktype */
5088 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5089 * test the bottom-of-stack bit, and then check the version number
5090 * field in the IP header.
5092 static struct block
*
5093 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5095 struct block
*b0
, *b1
;
5100 /* match the bottom-of-stack bit */
5101 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5102 /* match the IPv4 version number */
5103 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5107 case ETHERTYPE_IPV6
:
5108 /* match the bottom-of-stack bit */
5109 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5110 /* match the IPv4 version number */
5111 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5116 /* FIXME add other L3 proto IDs */
5117 bpf_error(cstate
, "unsupported protocol over mpls");
5122 static struct block
*
5123 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5124 int proto
, int dir
, int type
)
5126 struct block
*b0
, *b1
;
5127 const char *typestr
;
5137 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5139 * Only check for non-IPv4 addresses if we're not
5140 * checking MPLS-encapsulated packets.
5142 if (cstate
->label_stack_depth
== 0) {
5143 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5145 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5151 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5154 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
5157 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
5160 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
5163 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5166 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5169 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5172 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5175 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5178 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5181 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5184 return gen_dnhostop(cstate
, addr
, dir
);
5187 bpf_error(cstate
, "LAT host filtering not implemented");
5190 bpf_error(cstate
, "SCA host filtering not implemented");
5193 bpf_error(cstate
, "MOPRC host filtering not implemented");
5196 bpf_error(cstate
, "MOPDL host filtering not implemented");
5199 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5202 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5205 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5208 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5211 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5214 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5217 bpf_error(cstate
, "AARP host filtering not implemented");
5220 bpf_error(cstate
, "ISO host filtering not implemented");
5223 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5226 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5229 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5232 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5235 bpf_error(cstate
, "IPX host filtering not implemented");
5238 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5241 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5244 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5247 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5250 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5253 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5256 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5259 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5262 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5265 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5274 static struct block
*
5275 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5276 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5278 const char *typestr
;
5288 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5291 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5294 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5297 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5300 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5303 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5306 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5309 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5312 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5315 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5318 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5321 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5324 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5327 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5330 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5333 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5336 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5339 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5342 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5345 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5348 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5351 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5354 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5357 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5360 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5363 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5366 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5369 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5372 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5375 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5378 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5381 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5384 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5387 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5390 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5393 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5396 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5399 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5402 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5405 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5415 static struct block
*
5416 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5417 struct addrinfo
*alist
, int proto
, int dir
)
5419 struct block
*b0
, *b1
, *tmp
;
5420 struct addrinfo
*ai
;
5421 struct sockaddr_in
*sin
;
5424 bpf_error(cstate
, "direction applied to 'gateway'");
5431 switch (cstate
->linktype
) {
5433 case DLT_NETANALYZER
:
5434 case DLT_NETANALYZER_TRANSPARENT
:
5435 b1
= gen_prevlinkhdr_check(cstate
);
5436 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5441 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5444 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5446 case DLT_IEEE802_11
:
5447 case DLT_PRISM_HEADER
:
5448 case DLT_IEEE802_11_RADIO_AVS
:
5449 case DLT_IEEE802_11_RADIO
:
5451 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5455 * This is LLC-multiplexed traffic; if it were
5456 * LANE, cstate->linktype would have been set to
5460 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5461 case DLT_IP_OVER_FC
:
5462 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5466 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5469 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5471 * Does it have an address?
5473 if (ai
->ai_addr
!= NULL
) {
5475 * Yes. Is it an IPv4 address?
5477 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5479 * Generate an entry for it.
5481 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5482 tmp
= gen_host(cstate
,
5483 ntohl(sin
->sin_addr
.s_addr
),
5484 0xffffffff, proto
, Q_OR
, Q_HOST
);
5486 * Is it the *first* IPv4 address?
5490 * Yes, so start with it.
5495 * No, so OR it into the
5507 * No IPv4 addresses found.
5515 bpf_error(cstate
, "illegal modifier of 'gateway'");
5520 static struct block
*
5521 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5529 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5533 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5537 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5541 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5544 #ifndef IPPROTO_IGMP
5545 #define IPPROTO_IGMP 2
5549 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5552 #ifndef IPPROTO_IGRP
5553 #define IPPROTO_IGRP 9
5556 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5560 #define IPPROTO_PIM 103
5564 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5567 #ifndef IPPROTO_VRRP
5568 #define IPPROTO_VRRP 112
5572 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5575 #ifndef IPPROTO_CARP
5576 #define IPPROTO_CARP 112
5580 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5584 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5588 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5592 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5596 bpf_error(cstate
, "link layer applied in wrong context");
5599 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5603 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5607 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5611 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5615 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5619 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5623 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5627 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5630 #ifndef IPPROTO_ICMPV6
5631 #define IPPROTO_ICMPV6 58
5634 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5638 #define IPPROTO_AH 51
5641 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5645 #define IPPROTO_ESP 50
5648 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5652 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5656 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5660 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5663 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5664 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5665 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5667 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5669 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5671 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5675 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5676 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5677 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5679 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5681 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5683 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5687 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5688 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5689 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5691 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5696 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5697 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5702 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5703 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5705 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5707 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5712 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5713 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5718 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5719 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5724 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5728 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5732 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5736 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5740 bpf_error(cstate
, "'radio' is not a valid protocol type");
5749 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5752 * Catch errors reported by us and routines below us, and return NULL
5755 if (setjmp(cstate
->top_ctx
))
5758 return gen_proto_abbrev_internal(cstate
, proto
);
5761 static struct block
*
5762 gen_ipfrag(compiler_state_t
*cstate
)
5767 /* not IPv4 frag other than the first frag */
5768 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5769 b
= new_block(cstate
, JMP(BPF_JSET
));
5778 * Generate a comparison to a port value in the transport-layer header
5779 * at the specified offset from the beginning of that header.
5781 * XXX - this handles a variable-length prefix preceding the link-layer
5782 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5783 * variable-length link-layer headers (such as Token Ring or 802.11
5786 static struct block
*
5787 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5789 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5792 static struct block
*
5793 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5795 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5798 static struct block
*
5799 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5801 struct block
*b0
, *b1
, *tmp
;
5803 /* ip proto 'proto' and not a fragment other than the first fragment */
5804 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5805 b0
= gen_ipfrag(cstate
);
5810 b1
= gen_portatom(cstate
, 0, port
);
5814 b1
= gen_portatom(cstate
, 2, port
);
5818 tmp
= gen_portatom(cstate
, 0, port
);
5819 b1
= gen_portatom(cstate
, 2, port
);
5825 tmp
= gen_portatom(cstate
, 0, port
);
5826 b1
= gen_portatom(cstate
, 2, port
);
5831 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5835 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5839 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5843 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5847 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5851 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5863 static struct block
*
5864 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5866 struct block
*b0
, *b1
, *tmp
;
5871 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5872 * not LLC encapsulation with LLCSAP_IP.
5874 * For IEEE 802 networks - which includes 802.5 token ring
5875 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5876 * says that SNAP encapsulation is used, not LLC encapsulation
5879 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5880 * RFC 2225 say that SNAP encapsulation is used, not LLC
5881 * encapsulation with LLCSAP_IP.
5883 * So we always check for ETHERTYPE_IP.
5885 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5891 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5895 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5896 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5898 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5910 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5912 struct block
*b0
, *b1
, *tmp
;
5914 /* ip6 proto 'proto' */
5915 /* XXX - catch the first fragment of a fragmented packet? */
5916 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5920 b1
= gen_portatom6(cstate
, 0, port
);
5924 b1
= gen_portatom6(cstate
, 2, port
);
5928 tmp
= gen_portatom6(cstate
, 0, port
);
5929 b1
= gen_portatom6(cstate
, 2, port
);
5935 tmp
= gen_portatom6(cstate
, 0, port
);
5936 b1
= gen_portatom6(cstate
, 2, port
);
5948 static struct block
*
5949 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5951 struct block
*b0
, *b1
, *tmp
;
5953 /* link proto ip6 */
5954 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5960 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5964 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5965 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5967 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5978 /* gen_portrange code */
5979 static struct block
*
5980 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5983 struct block
*b1
, *b2
;
5987 * Reverse the order of the ports, so v1 is the lower one.
5996 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5997 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
6004 static struct block
*
6005 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6006 bpf_u_int32 proto
, int dir
)
6008 struct block
*b0
, *b1
, *tmp
;
6010 /* ip proto 'proto' and not a fragment other than the first fragment */
6011 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
6012 b0
= gen_ipfrag(cstate
);
6017 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6021 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6025 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6026 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6032 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6033 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6038 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6042 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6046 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6050 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6054 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
6058 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
6070 static struct block
*
6071 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6074 struct block
*b0
, *b1
, *tmp
;
6077 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6083 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6088 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6089 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6091 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6102 static struct block
*
6103 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6106 struct block
*b1
, *b2
;
6110 * Reverse the order of the ports, so v1 is the lower one.
6119 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6120 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6127 static struct block
*
6128 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6129 bpf_u_int32 proto
, int dir
)
6131 struct block
*b0
, *b1
, *tmp
;
6133 /* ip6 proto 'proto' */
6134 /* XXX - catch the first fragment of a fragmented packet? */
6135 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6139 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6143 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6147 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6148 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6154 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6155 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6167 static struct block
*
6168 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6171 struct block
*b0
, *b1
, *tmp
;
6173 /* link proto ip6 */
6174 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6180 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6185 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6186 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6188 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6200 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6209 v
= pcap_nametoproto(name
);
6210 if (v
== PROTO_UNDEF
)
6211 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6215 /* XXX should look up h/w protocol type based on cstate->linktype */
6216 v
= pcap_nametoeproto(name
);
6217 if (v
== PROTO_UNDEF
) {
6218 v
= pcap_nametollc(name
);
6219 if (v
== PROTO_UNDEF
)
6220 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6225 if (strcmp(name
, "esis") == 0)
6227 else if (strcmp(name
, "isis") == 0)
6229 else if (strcmp(name
, "clnp") == 0)
6232 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6242 #if !defined(NO_PROTOCHAIN)
6243 static struct block
*
6244 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6246 struct block
*b0
, *b
;
6247 struct slist
*s
[100];
6248 int fix2
, fix3
, fix4
, fix5
;
6249 int ahcheck
, again
, end
;
6251 int reg2
= alloc_reg(cstate
);
6253 memset(s
, 0, sizeof(s
));
6254 fix3
= fix4
= fix5
= 0;
6261 b0
= gen_protochain(cstate
, v
, Q_IP
);
6262 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6266 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6271 * We don't handle variable-length prefixes before the link-layer
6272 * header, or variable-length link-layer headers, here yet.
6273 * We might want to add BPF instructions to do the protochain
6274 * work, to simplify that and, on platforms that have a BPF
6275 * interpreter with the new instructions, let the filtering
6276 * be done in the kernel. (We already require a modified BPF
6277 * engine to do the protochain stuff, to support backward
6278 * branches, and backward branch support is unlikely to appear
6279 * in kernel BPF engines.)
6281 if (cstate
->off_linkpl
.is_variable
)
6282 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6285 * To quote a comment in optimize.c:
6287 * "These data structures are used in a Cocke and Schwartz style
6288 * value numbering scheme. Since the flowgraph is acyclic,
6289 * exit values can be propagated from a node's predecessors
6290 * provided it is uniquely defined."
6292 * "Acyclic" means "no backward branches", which means "no
6293 * loops", so we have to turn the optimizer off.
6295 cstate
->no_optimize
= 1;
6298 * s[0] is a dummy entry to protect other BPF insn from damage
6299 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6300 * hard to find interdependency made by jump table fixup.
6303 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6308 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6311 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6312 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6314 /* X = ip->ip_hl << 2 */
6315 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6316 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6321 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6323 /* A = ip6->ip_nxt */
6324 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6325 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6327 /* X = sizeof(struct ip6_hdr) */
6328 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6334 bpf_error(cstate
, "unsupported proto to gen_protochain");
6338 /* again: if (A == v) goto end; else fall through; */
6340 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6342 s
[i
]->s
.jt
= NULL
; /*later*/
6343 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6347 #ifndef IPPROTO_NONE
6348 #define IPPROTO_NONE 59
6350 /* if (A == IPPROTO_NONE) goto end */
6351 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6352 s
[i
]->s
.jt
= NULL
; /*later*/
6353 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6354 s
[i
]->s
.k
= IPPROTO_NONE
;
6355 s
[fix5
]->s
.jf
= s
[i
];
6359 if (proto
== Q_IPV6
) {
6360 int v6start
, v6end
, v6advance
, j
;
6363 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6364 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6365 s
[i
]->s
.jt
= NULL
; /*later*/
6366 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6367 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6368 s
[fix2
]->s
.jf
= s
[i
];
6370 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6371 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6372 s
[i
]->s
.jt
= NULL
; /*later*/
6373 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6374 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6376 /* if (A == IPPROTO_ROUTING) goto v6advance */
6377 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6378 s
[i
]->s
.jt
= NULL
; /*later*/
6379 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6380 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6382 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6383 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6384 s
[i
]->s
.jt
= NULL
; /*later*/
6385 s
[i
]->s
.jf
= NULL
; /*later*/
6386 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6396 * A = P[X + packet head];
6397 * X = X + (P[X + packet head + 1] + 1) * 8;
6399 /* A = P[X + packet head] */
6400 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6401 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6404 s
[i
] = new_stmt(cstate
, BPF_ST
);
6407 /* A = P[X + packet head + 1]; */
6408 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6409 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6412 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6416 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6420 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6424 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6427 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6431 /* goto again; (must use BPF_JA for backward jump) */
6432 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6433 s
[i
]->s
.k
= again
- i
- 1;
6434 s
[i
- 1]->s
.jf
= s
[i
];
6438 for (j
= v6start
; j
<= v6end
; j
++)
6439 s
[j
]->s
.jt
= s
[v6advance
];
6442 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6444 s
[fix2
]->s
.jf
= s
[i
];
6450 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6451 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6452 s
[i
]->s
.jt
= NULL
; /*later*/
6453 s
[i
]->s
.jf
= NULL
; /*later*/
6454 s
[i
]->s
.k
= IPPROTO_AH
;
6456 s
[fix3
]->s
.jf
= s
[ahcheck
];
6463 * X = X + (P[X + 1] + 2) * 4;
6466 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6468 /* A = P[X + packet head]; */
6469 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6470 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6473 s
[i
] = new_stmt(cstate
, BPF_ST
);
6477 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6480 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6484 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6486 /* A = P[X + packet head] */
6487 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6488 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6491 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6495 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6499 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6502 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6506 /* goto again; (must use BPF_JA for backward jump) */
6507 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6508 s
[i
]->s
.k
= again
- i
- 1;
6513 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6515 s
[fix2
]->s
.jt
= s
[end
];
6516 s
[fix4
]->s
.jf
= s
[end
];
6517 s
[fix5
]->s
.jt
= s
[end
];
6524 for (i
= 0; i
< max
- 1; i
++)
6525 s
[i
]->next
= s
[i
+ 1];
6526 s
[max
- 1]->next
= NULL
;
6531 b
= new_block(cstate
, JMP(BPF_JEQ
));
6532 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6535 free_reg(cstate
, reg2
);
6540 #endif /* !defined(NO_PROTOCHAIN) */
6542 static struct block
*
6543 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6546 struct block
*b0
, *b1
;
6549 * A data frame has the 0x08 bit (b3) in the frame control field set
6550 * and the 0x04 bit (b2) clear.
6552 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6553 b0
= new_block(cstate
, JMP(BPF_JSET
));
6557 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6558 b1
= new_block(cstate
, JMP(BPF_JSET
));
6569 * Generate code that checks whether the packet is a packet for protocol
6570 * <proto> and whether the type field in that protocol's header has
6571 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6572 * IP packet and checks the protocol number in the IP header against <v>.
6574 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6575 * against Q_IP and Q_IPV6.
6577 static struct block
*
6578 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6580 struct block
*b0
, *b1
;
6583 if (dir
!= Q_DEFAULT
)
6584 bpf_error(cstate
, "direction applied to 'proto'");
6588 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6589 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6594 return gen_linktype(cstate
, v
);
6598 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6599 * not LLC encapsulation with LLCSAP_IP.
6601 * For IEEE 802 networks - which includes 802.5 token ring
6602 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6603 * says that SNAP encapsulation is used, not LLC encapsulation
6606 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6607 * RFC 2225 say that SNAP encapsulation is used, not LLC
6608 * encapsulation with LLCSAP_IP.
6610 * So we always check for ETHERTYPE_IP.
6612 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6613 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6618 bpf_error(cstate
, "arp does not encapsulate another protocol");
6622 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6626 bpf_error(cstate
, "'sctp proto' is bogus");
6630 bpf_error(cstate
, "'tcp proto' is bogus");
6634 bpf_error(cstate
, "'udp proto' is bogus");
6638 bpf_error(cstate
, "'icmp proto' is bogus");
6642 bpf_error(cstate
, "'igmp proto' is bogus");
6646 bpf_error(cstate
, "'igrp proto' is bogus");
6650 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6654 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6658 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6662 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6666 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6670 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6674 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6676 * Also check for a fragment header before the final
6679 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6680 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6682 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6688 bpf_error(cstate
, "'icmp6 proto' is bogus");
6692 bpf_error(cstate
, "'ah proto' is bogus");
6696 bpf_error(cstate
, "'esp proto' is bogus");
6700 bpf_error(cstate
, "'pim proto' is bogus");
6704 bpf_error(cstate
, "'vrrp proto' is bogus");
6708 bpf_error(cstate
, "'aarp proto' is bogus");
6712 switch (cstate
->linktype
) {
6716 * Frame Relay packets typically have an OSI
6717 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6718 * generates code to check for all the OSI
6719 * NLPIDs, so calling it and then adding a check
6720 * for the particular NLPID for which we're
6721 * looking is bogus, as we can just check for
6724 * What we check for is the NLPID and a frame
6725 * control field value of UI, i.e. 0x03 followed
6728 * XXX - assumes a 2-byte Frame Relay header with
6729 * DLCI and flags. What if the address is longer?
6731 * XXX - what about SNAP-encapsulated frames?
6733 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6739 * Cisco uses an Ethertype lookalike - for OSI,
6742 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6743 /* OSI in C-HDLC is stuffed with a fudge byte */
6744 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6749 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6750 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6756 bpf_error(cstate
, "'esis proto' is bogus");
6760 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6762 * 4 is the offset of the PDU type relative to the IS-IS
6765 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6770 bpf_error(cstate
, "'clnp proto' is not supported");
6774 bpf_error(cstate
, "'stp proto' is bogus");
6778 bpf_error(cstate
, "'ipx proto' is bogus");
6782 bpf_error(cstate
, "'netbeui proto' is bogus");
6786 bpf_error(cstate
, "'l1 proto' is bogus");
6790 bpf_error(cstate
, "'l2 proto' is bogus");
6794 bpf_error(cstate
, "'iih proto' is bogus");
6798 bpf_error(cstate
, "'snp proto' is bogus");
6802 bpf_error(cstate
, "'csnp proto' is bogus");
6806 bpf_error(cstate
, "'psnp proto' is bogus");
6810 bpf_error(cstate
, "'lsp proto' is bogus");
6814 bpf_error(cstate
, "'radio proto' is bogus");
6818 bpf_error(cstate
, "'carp proto' is bogus");
6829 * Convert a non-numeric name to a port number.
6832 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6834 struct addrinfo hints
, *res
, *ai
;
6836 struct sockaddr_in
*in4
;
6838 struct sockaddr_in6
*in6
;
6843 * We check for both TCP and UDP in case there are
6844 * ambiguous entries.
6846 memset(&hints
, 0, sizeof(hints
));
6847 hints
.ai_family
= PF_UNSPEC
;
6848 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6849 hints
.ai_protocol
= ipproto
;
6850 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6857 * No such port. Just return -1.
6864 * We don't use strerror() because it's not
6865 * guaranteed to be thread-safe on all platforms
6866 * (probably because it might use a non-thread-local
6867 * buffer into which to format an error message
6868 * if the error code isn't one for which it has
6869 * a canned string; three cheers for C string
6872 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6874 port
= -2; /* a real error */
6880 * This is a real error, not just "there's
6881 * no such service name".
6883 * We don't use gai_strerror() because it's not
6884 * guaranteed to be thread-safe on all platforms
6885 * (probably because it might use a non-thread-local
6886 * buffer into which to format an error message
6887 * if the error code isn't one for which it has
6888 * a canned string; three cheers for C string
6891 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6893 port
= -2; /* a real error */
6898 * OK, we found it. Did it find anything?
6900 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6902 * Does it have an address?
6904 if (ai
->ai_addr
!= NULL
) {
6906 * Yes. Get a port number; we're done.
6908 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6909 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6910 port
= ntohs(in4
->sin_port
);
6914 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6915 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6916 port
= ntohs(in6
->sin6_port
);
6928 * Convert a string to a port number.
6931 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6941 * See if it's a number.
6943 ret
= stoulen(string
, string_size
, &val
, cstate
);
6947 /* Unknown port type - it's just a number. */
6948 *proto
= PROTO_UNDEF
;
6951 case STOULEN_NOT_OCTAL_NUMBER
:
6952 case STOULEN_NOT_HEX_NUMBER
:
6953 case STOULEN_NOT_DECIMAL_NUMBER
:
6955 * Not a valid number; try looking it up as a port.
6957 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6958 memcpy(cpy
, string
, string_size
);
6959 cpy
[string_size
] = '\0';
6960 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6961 if (tcp_port
== -2) {
6963 * We got a hard error; the error string has
6967 longjmp(cstate
->top_ctx
, 1);
6970 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6971 if (udp_port
== -2) {
6973 * We got a hard error; the error string has
6977 longjmp(cstate
->top_ctx
, 1);
6982 * We need to check /etc/services for ambiguous entries.
6983 * If we find an ambiguous entry, and it has the
6984 * same port number, change the proto to PROTO_UNDEF
6985 * so both TCP and UDP will be checked.
6987 if (tcp_port
>= 0) {
6988 val
= (bpf_u_int32
)tcp_port
;
6989 *proto
= IPPROTO_TCP
;
6990 if (udp_port
>= 0) {
6991 if (udp_port
== tcp_port
)
6992 *proto
= PROTO_UNDEF
;
6995 /* Can't handle ambiguous names that refer
6996 to different port numbers. */
6997 warning("ambiguous port %s in /etc/services",
7004 if (udp_port
>= 0) {
7005 val
= (bpf_u_int32
)udp_port
;
7006 *proto
= IPPROTO_UDP
;
7010 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
7012 longjmp(cstate
->top_ctx
, 1);
7019 /* Error already set. */
7020 longjmp(cstate
->top_ctx
, 1);
7027 /* Should not happen */
7028 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
7029 longjmp(cstate
->top_ctx
, 1);
7036 * Convert a string in the form PPP-PPP, which correspond to ports, to
7037 * a starting and ending port in a port range.
7040 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
7041 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
7044 const char *first
, *second
;
7045 size_t first_size
, second_size
;
7048 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
7049 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
7052 * Make sure there are no other hyphens.
7054 * XXX - we support named ports, but there are some port names
7055 * in /etc/services that include hyphens, so this would rule
7058 if (strchr(hyphen_off
+ 1, '-') != NULL
)
7059 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
7063 * Get the length of the first port.
7066 first_size
= hyphen_off
- string
;
7067 if (first_size
== 0) {
7068 /* Range of "-port", which we don't support. */
7069 bpf_error(cstate
, "port range '%s' has no starting port", string
);
7073 * Try to convert it to a port.
7075 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
7076 save_proto
= *proto
;
7079 * Get the length of the second port.
7081 second
= hyphen_off
+ 1;
7082 second_size
= strlen(second
);
7083 if (second_size
== 0) {
7084 /* Range of "port-", which we don't support. */
7085 bpf_error(cstate
, "port range '%s' has no ending port", string
);
7089 * Try to convert it to a port.
7091 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
7092 if (*proto
!= save_proto
)
7093 *proto
= PROTO_UNDEF
;
7097 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
7099 int proto
= q
.proto
;
7103 bpf_u_int32 mask
, addr
;
7104 struct addrinfo
*res
, *res0
;
7105 struct sockaddr_in
*sin4
;
7108 struct sockaddr_in6
*sin6
;
7109 struct in6_addr mask128
;
7111 struct block
*b
, *tmp
;
7112 int port
, real_proto
;
7113 bpf_u_int32 port1
, port2
;
7116 * Catch errors reported by us and routines below us, and return NULL
7119 if (setjmp(cstate
->top_ctx
))
7125 addr
= pcap_nametonetaddr(name
);
7127 bpf_error(cstate
, "unknown network '%s'", name
);
7128 /* Left justify network addr and calculate its network mask */
7130 while (addr
&& (addr
& 0xff000000) == 0) {
7134 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7138 if (proto
== Q_LINK
) {
7139 switch (cstate
->linktype
) {
7142 case DLT_NETANALYZER
:
7143 case DLT_NETANALYZER_TRANSPARENT
:
7144 eaddr
= pcap_ether_hostton(name
);
7147 "unknown ether host '%s'", name
);
7148 tmp
= gen_prevlinkhdr_check(cstate
);
7149 b
= gen_ehostop(cstate
, eaddr
, dir
);
7156 eaddr
= pcap_ether_hostton(name
);
7159 "unknown FDDI host '%s'", name
);
7160 b
= gen_fhostop(cstate
, eaddr
, dir
);
7165 eaddr
= pcap_ether_hostton(name
);
7168 "unknown token ring host '%s'", name
);
7169 b
= gen_thostop(cstate
, eaddr
, dir
);
7173 case DLT_IEEE802_11
:
7174 case DLT_PRISM_HEADER
:
7175 case DLT_IEEE802_11_RADIO_AVS
:
7176 case DLT_IEEE802_11_RADIO
:
7178 eaddr
= pcap_ether_hostton(name
);
7181 "unknown 802.11 host '%s'", name
);
7182 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7186 case DLT_IP_OVER_FC
:
7187 eaddr
= pcap_ether_hostton(name
);
7190 "unknown Fibre Channel host '%s'", name
);
7191 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7196 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7197 } else if (proto
== Q_DECNET
) {
7199 * A long time ago on Ultrix libpcap supported
7200 * translation of DECnet host names into DECnet
7201 * addresses, but this feature is history now.
7203 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7206 memset(&mask128
, 0xff, sizeof(mask128
));
7208 res0
= res
= pcap_nametoaddrinfo(name
);
7210 bpf_error(cstate
, "unknown host '%s'", name
);
7217 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7218 tproto
== Q_DEFAULT
) {
7224 for (res
= res0
; res
; res
= res
->ai_next
) {
7225 switch (res
->ai_family
) {
7228 if (tproto
== Q_IPV6
)
7232 sin4
= (struct sockaddr_in
*)
7234 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7235 0xffffffff, tproto
, dir
, q
.addr
);
7239 if (tproto6
== Q_IP
)
7242 sin6
= (struct sockaddr_in6
*)
7244 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7245 &mask128
, tproto6
, dir
, q
.addr
);
7258 bpf_error(cstate
, "unknown host '%s'%s", name
,
7259 (proto
== Q_DEFAULT
)
7261 : " for specified address family");
7267 if (proto
!= Q_DEFAULT
&&
7268 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7269 bpf_error(cstate
, "illegal qualifier of 'port'");
7270 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7271 bpf_error(cstate
, "unknown port '%s'", name
);
7272 if (proto
== Q_UDP
) {
7273 if (real_proto
== IPPROTO_TCP
)
7274 bpf_error(cstate
, "port '%s' is tcp", name
);
7275 else if (real_proto
== IPPROTO_SCTP
)
7276 bpf_error(cstate
, "port '%s' is sctp", name
);
7278 /* override PROTO_UNDEF */
7279 real_proto
= IPPROTO_UDP
;
7281 if (proto
== Q_TCP
) {
7282 if (real_proto
== IPPROTO_UDP
)
7283 bpf_error(cstate
, "port '%s' is udp", name
);
7285 else if (real_proto
== IPPROTO_SCTP
)
7286 bpf_error(cstate
, "port '%s' is sctp", name
);
7288 /* override PROTO_UNDEF */
7289 real_proto
= IPPROTO_TCP
;
7291 if (proto
== Q_SCTP
) {
7292 if (real_proto
== IPPROTO_UDP
)
7293 bpf_error(cstate
, "port '%s' is udp", name
);
7295 else if (real_proto
== IPPROTO_TCP
)
7296 bpf_error(cstate
, "port '%s' is tcp", name
);
7298 /* override PROTO_UNDEF */
7299 real_proto
= IPPROTO_SCTP
;
7302 bpf_error(cstate
, "illegal port number %d < 0", port
);
7304 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7305 b
= gen_port(cstate
, port
, real_proto
, dir
);
7306 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7310 if (proto
!= Q_DEFAULT
&&
7311 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7312 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7313 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7314 if (proto
== Q_UDP
) {
7315 if (real_proto
== IPPROTO_TCP
)
7316 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7317 else if (real_proto
== IPPROTO_SCTP
)
7318 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7320 /* override PROTO_UNDEF */
7321 real_proto
= IPPROTO_UDP
;
7323 if (proto
== Q_TCP
) {
7324 if (real_proto
== IPPROTO_UDP
)
7325 bpf_error(cstate
, "port in range '%s' is udp", name
);
7326 else if (real_proto
== IPPROTO_SCTP
)
7327 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7329 /* override PROTO_UNDEF */
7330 real_proto
= IPPROTO_TCP
;
7332 if (proto
== Q_SCTP
) {
7333 if (real_proto
== IPPROTO_UDP
)
7334 bpf_error(cstate
, "port in range '%s' is udp", name
);
7335 else if (real_proto
== IPPROTO_TCP
)
7336 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7338 /* override PROTO_UNDEF */
7339 real_proto
= IPPROTO_SCTP
;
7342 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7344 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7346 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7347 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7352 eaddr
= pcap_ether_hostton(name
);
7354 bpf_error(cstate
, "unknown ether host: %s", name
);
7356 res
= pcap_nametoaddrinfo(name
);
7359 bpf_error(cstate
, "unknown host '%s'", name
);
7360 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7364 bpf_error(cstate
, "unknown host '%s'", name
);
7367 bpf_error(cstate
, "'gateway' not supported in this configuration");
7371 real_proto
= lookup_proto(cstate
, name
, proto
);
7372 if (real_proto
>= 0)
7373 return gen_proto(cstate
, real_proto
, proto
, dir
);
7375 bpf_error(cstate
, "unknown protocol: %s", name
);
7377 #if !defined(NO_PROTOCHAIN)
7379 real_proto
= lookup_proto(cstate
, name
, proto
);
7380 if (real_proto
>= 0)
7381 return gen_protochain(cstate
, real_proto
, proto
);
7383 bpf_error(cstate
, "unknown protocol: %s", name
);
7384 #endif /* !defined(NO_PROTOCHAIN) */
7395 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7396 bpf_u_int32 masklen
, struct qual q
)
7398 register int nlen
, mlen
;
7402 * Catch errors reported by us and routines below us, and return NULL
7405 if (setjmp(cstate
->top_ctx
))
7408 nlen
= __pcap_atoin(s1
, &n
);
7410 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7411 /* Promote short ipaddr */
7415 mlen
= __pcap_atoin(s2
, &m
);
7417 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7418 /* Promote short ipaddr */
7421 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7424 /* Convert mask len to mask */
7426 bpf_error(cstate
, "mask length must be <= 32");
7429 * X << 32 is not guaranteed by C to be 0; it's
7434 m
= 0xffffffff << (32 - masklen
);
7436 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7443 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7446 bpf_error(cstate
, "Mask syntax for networks only");
7453 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7461 * Catch errors reported by us and routines below us, and return NULL
7464 if (setjmp(cstate
->top_ctx
))
7471 * v contains a 32-bit unsigned parsed from a string of the
7472 * form {N}, which could be decimal, hexadecimal or octal.
7473 * Although it would be possible to use the value as a raw
7474 * 16-bit DECnet address when the value fits into 16 bits, this
7475 * would be a questionable feature: DECnet address wire
7476 * encoding is little-endian, so this would not work as
7477 * intuitively as the same works for [big-endian] IPv4
7478 * addresses (0x01020304 means 1.2.3.4).
7480 if (proto
== Q_DECNET
)
7481 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7483 } else if (proto
== Q_DECNET
) {
7485 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7486 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7487 * for a valid DECnet address.
7489 vlen
= __pcap_atodn(s
, &v
);
7491 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7494 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7495 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7498 vlen
= __pcap_atoin(s
, &v
);
7500 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7508 if (proto
== Q_DECNET
)
7509 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7510 else if (proto
== Q_LINK
) {
7511 bpf_error(cstate
, "illegal link layer address");
7514 if (s
== NULL
&& q
.addr
== Q_NET
) {
7515 /* Promote short net number */
7516 while (v
&& (v
& 0xff000000) == 0) {
7521 /* Promote short ipaddr */
7523 mask
<<= 32 - vlen
;
7525 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7530 proto
= IPPROTO_UDP
;
7531 else if (proto
== Q_TCP
)
7532 proto
= IPPROTO_TCP
;
7533 else if (proto
== Q_SCTP
)
7534 proto
= IPPROTO_SCTP
;
7535 else if (proto
== Q_DEFAULT
)
7536 proto
= PROTO_UNDEF
;
7538 bpf_error(cstate
, "illegal qualifier of 'port'");
7541 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7545 b
= gen_port(cstate
, v
, proto
, dir
);
7546 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7552 proto
= IPPROTO_UDP
;
7553 else if (proto
== Q_TCP
)
7554 proto
= IPPROTO_TCP
;
7555 else if (proto
== Q_SCTP
)
7556 proto
= IPPROTO_SCTP
;
7557 else if (proto
== Q_DEFAULT
)
7558 proto
= PROTO_UNDEF
;
7560 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7563 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7567 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7568 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7573 bpf_error(cstate
, "'gateway' requires a name");
7577 return gen_proto(cstate
, v
, proto
, dir
);
7579 #if !defined(NO_PROTOCHAIN)
7581 return gen_protochain(cstate
, v
, proto
);
7597 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7600 struct addrinfo
*res
;
7601 struct in6_addr
*addr
;
7602 struct in6_addr mask
;
7604 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7607 * Catch errors reported by us and routines below us, and return NULL
7610 if (setjmp(cstate
->top_ctx
))
7613 res
= pcap_nametoaddrinfo(s
);
7615 bpf_error(cstate
, "invalid ip6 address %s", s
);
7618 bpf_error(cstate
, "%s resolved to multiple address", s
);
7619 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7621 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7622 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7623 memset(&mask
, 0, sizeof(mask
));
7624 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7626 mask
.s6_addr
[masklen
/ 8] =
7627 (0xff << (8 - masklen
% 8)) & 0xff;
7630 memcpy(a
, addr
, sizeof(a
));
7631 memcpy(m
, &mask
, sizeof(m
));
7632 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7633 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7634 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7642 bpf_error(cstate
, "Mask syntax for networks only");
7646 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7652 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7659 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7661 struct block
*b
, *tmp
;
7664 * Catch errors reported by us and routines below us, and return NULL
7667 if (setjmp(cstate
->top_ctx
))
7670 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7671 cstate
->e
= pcap_ether_aton(s
);
7672 if (cstate
->e
== NULL
)
7673 bpf_error(cstate
, "malloc");
7674 switch (cstate
->linktype
) {
7676 case DLT_NETANALYZER
:
7677 case DLT_NETANALYZER_TRANSPARENT
:
7678 tmp
= gen_prevlinkhdr_check(cstate
);
7679 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7684 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7687 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7689 case DLT_IEEE802_11
:
7690 case DLT_PRISM_HEADER
:
7691 case DLT_IEEE802_11_RADIO_AVS
:
7692 case DLT_IEEE802_11_RADIO
:
7694 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7696 case DLT_IP_OVER_FC
:
7697 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7702 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7709 bpf_error(cstate
, "ethernet address used in non-ether expression");
7714 sappend(struct slist
*s0
, struct slist
*s1
)
7717 * This is definitely not the best way to do this, but the
7718 * lists will rarely get long.
7725 static struct slist
*
7726 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7730 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7735 static struct slist
*
7736 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7740 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7746 * Modify "index" to use the value stored into its register as an
7747 * offset relative to the beginning of the header for the protocol
7748 * "proto", and allocate a register and put an item "size" bytes long
7749 * (1, 2, or 4) at that offset into that register, making it the register
7752 static struct arth
*
7753 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7757 struct slist
*s
, *tmp
;
7759 int regno
= alloc_reg(cstate
);
7761 free_reg(cstate
, inst
->regno
);
7765 bpf_error(cstate
, "data size must be 1, 2, or 4");
7782 bpf_error(cstate
, "unsupported index operation");
7786 * The offset is relative to the beginning of the packet
7787 * data, if we have a radio header. (If we don't, this
7790 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7791 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7792 cstate
->linktype
!= DLT_PRISM_HEADER
)
7793 bpf_error(cstate
, "radio information not present in capture");
7796 * Load into the X register the offset computed into the
7797 * register specified by "index".
7799 s
= xfer_to_x(cstate
, inst
);
7802 * Load the item at that offset.
7804 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7806 sappend(inst
->s
, s
);
7811 * The offset is relative to the beginning of
7812 * the link-layer header.
7814 * XXX - what about ATM LANE? Should the index be
7815 * relative to the beginning of the AAL5 frame, so
7816 * that 0 refers to the beginning of the LE Control
7817 * field, or relative to the beginning of the LAN
7818 * frame, so that 0 refers, for Ethernet LANE, to
7819 * the beginning of the destination address?
7821 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7824 * If "s" is non-null, it has code to arrange that the
7825 * X register contains the length of the prefix preceding
7826 * the link-layer header. Add to it the offset computed
7827 * into the register specified by "index", and move that
7828 * into the X register. Otherwise, just load into the X
7829 * register the offset computed into the register specified
7833 sappend(s
, xfer_to_a(cstate
, inst
));
7834 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7835 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7837 s
= xfer_to_x(cstate
, inst
);
7840 * Load the item at the sum of the offset we've put in the
7841 * X register and the offset of the start of the link
7842 * layer header (which is 0 if the radio header is
7843 * variable-length; that header length is what we put
7844 * into the X register and then added to the index).
7846 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7847 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7849 sappend(inst
->s
, s
);
7863 * The offset is relative to the beginning of
7864 * the network-layer header.
7865 * XXX - are there any cases where we want
7866 * cstate->off_nl_nosnap?
7868 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7871 * If "s" is non-null, it has code to arrange that the
7872 * X register contains the variable part of the offset
7873 * of the link-layer payload. Add to it the offset
7874 * computed into the register specified by "index",
7875 * and move that into the X register. Otherwise, just
7876 * load into the X register the offset computed into
7877 * the register specified by "index".
7880 sappend(s
, xfer_to_a(cstate
, inst
));
7881 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7882 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7884 s
= xfer_to_x(cstate
, inst
);
7887 * Load the item at the sum of the offset we've put in the
7888 * X register, the offset of the start of the network
7889 * layer header from the beginning of the link-layer
7890 * payload, and the constant part of the offset of the
7891 * start of the link-layer payload.
7893 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7894 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7896 sappend(inst
->s
, s
);
7899 * Do the computation only if the packet contains
7900 * the protocol in question.
7902 b
= gen_proto_abbrev_internal(cstate
, proto
);
7904 gen_and(inst
->b
, b
);
7918 * The offset is relative to the beginning of
7919 * the transport-layer header.
7921 * Load the X register with the length of the IPv4 header
7922 * (plus the offset of the link-layer header, if it's
7923 * a variable-length header), in bytes.
7925 * XXX - are there any cases where we want
7926 * cstate->off_nl_nosnap?
7927 * XXX - we should, if we're built with
7928 * IPv6 support, generate code to load either
7929 * IPv4, IPv6, or both, as appropriate.
7931 s
= gen_loadx_iphdrlen(cstate
);
7934 * The X register now contains the sum of the variable
7935 * part of the offset of the link-layer payload and the
7936 * length of the network-layer header.
7938 * Load into the A register the offset relative to
7939 * the beginning of the transport layer header,
7940 * add the X register to that, move that to the
7941 * X register, and load with an offset from the
7942 * X register equal to the sum of the constant part of
7943 * the offset of the link-layer payload and the offset,
7944 * relative to the beginning of the link-layer payload,
7945 * of the network-layer header.
7947 sappend(s
, xfer_to_a(cstate
, inst
));
7948 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7949 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7950 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7951 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7952 sappend(inst
->s
, s
);
7955 * Do the computation only if the packet contains
7956 * the protocol in question - which is true only
7957 * if this is an IP datagram and is the first or
7958 * only fragment of that datagram.
7960 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7962 gen_and(inst
->b
, b
);
7963 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7968 * Do the computation only if the packet contains
7969 * the protocol in question.
7971 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7973 gen_and(inst
->b
, b
);
7977 * Check if we have an icmp6 next header
7979 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7981 gen_and(inst
->b
, b
);
7984 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7986 * If "s" is non-null, it has code to arrange that the
7987 * X register contains the variable part of the offset
7988 * of the link-layer payload. Add to it the offset
7989 * computed into the register specified by "index",
7990 * and move that into the X register. Otherwise, just
7991 * load into the X register the offset computed into
7992 * the register specified by "index".
7995 sappend(s
, xfer_to_a(cstate
, inst
));
7996 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7997 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7999 s
= xfer_to_x(cstate
, inst
);
8002 * Load the item at the sum of the offset we've put in the
8003 * X register, the offset of the start of the network
8004 * layer header from the beginning of the link-layer
8005 * payload, and the constant part of the offset of the
8006 * start of the link-layer payload.
8008 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
8009 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
8012 sappend(inst
->s
, s
);
8016 inst
->regno
= regno
;
8017 s
= new_stmt(cstate
, BPF_ST
);
8019 sappend(inst
->s
, s
);
8025 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
8029 * Catch errors reported by us and routines below us, and return NULL
8032 if (setjmp(cstate
->top_ctx
))
8035 return gen_load_internal(cstate
, proto
, inst
, size
);
8038 static struct block
*
8039 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8040 struct arth
*a1
, int reversed
)
8042 struct slist
*s0
, *s1
, *s2
;
8043 struct block
*b
, *tmp
;
8045 s0
= xfer_to_x(cstate
, a1
);
8046 s1
= xfer_to_a(cstate
, a0
);
8047 if (code
== BPF_JEQ
) {
8048 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
8049 b
= new_block(cstate
, JMP(code
));
8053 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
8059 sappend(a0
->s
, a1
->s
);
8063 free_reg(cstate
, a0
->regno
);
8064 free_reg(cstate
, a1
->regno
);
8066 /* 'and' together protocol checks */
8069 gen_and(a0
->b
, tmp
= a1
->b
);
8083 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8084 struct arth
*a1
, int reversed
)
8087 * Catch errors reported by us and routines below us, and return NULL
8090 if (setjmp(cstate
->top_ctx
))
8093 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
8097 gen_loadlen(compiler_state_t
*cstate
)
8104 * Catch errors reported by us and routines below us, and return NULL
8107 if (setjmp(cstate
->top_ctx
))
8110 regno
= alloc_reg(cstate
);
8111 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8112 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8113 s
->next
= new_stmt(cstate
, BPF_ST
);
8114 s
->next
->s
.k
= regno
;
8121 static struct arth
*
8122 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8128 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8130 reg
= alloc_reg(cstate
);
8132 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8134 s
->next
= new_stmt(cstate
, BPF_ST
);
8143 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8146 * Catch errors reported by us and routines below us, and return NULL
8149 if (setjmp(cstate
->top_ctx
))
8152 return gen_loadi_internal(cstate
, val
);
8156 * The a_arg dance is to avoid annoying whining by compilers that
8157 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8158 * It's not *used* after setjmp returns.
8161 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8163 struct arth
*a
= a_arg
;
8167 * Catch errors reported by us and routines below us, and return NULL
8170 if (setjmp(cstate
->top_ctx
))
8173 s
= xfer_to_a(cstate
, a
);
8175 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8178 s
= new_stmt(cstate
, BPF_ST
);
8186 * The a0_arg dance is to avoid annoying whining by compilers that
8187 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8188 * It's not *used* after setjmp returns.
8191 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8194 struct arth
*a0
= a0_arg
;
8195 struct slist
*s0
, *s1
, *s2
;
8198 * Catch errors reported by us and routines below us, and return NULL
8201 if (setjmp(cstate
->top_ctx
))
8205 * Disallow division by, or modulus by, zero; we do this here
8206 * so that it gets done even if the optimizer is disabled.
8208 * Also disallow shifts by a value greater than 31; we do this
8209 * here, for the same reason.
8211 if (code
== BPF_DIV
) {
8212 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8213 bpf_error(cstate
, "division by zero");
8214 } else if (code
== BPF_MOD
) {
8215 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8216 bpf_error(cstate
, "modulus by zero");
8217 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8218 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8219 bpf_error(cstate
, "shift by more than 31 bits");
8221 s0
= xfer_to_x(cstate
, a1
);
8222 s1
= xfer_to_a(cstate
, a0
);
8223 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8228 sappend(a0
->s
, a1
->s
);
8230 free_reg(cstate
, a0
->regno
);
8231 free_reg(cstate
, a1
->regno
);
8233 s0
= new_stmt(cstate
, BPF_ST
);
8234 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8241 * Initialize the table of used registers and the current register.
8244 init_regs(compiler_state_t
*cstate
)
8247 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8251 * Return the next free register.
8254 alloc_reg(compiler_state_t
*cstate
)
8256 int n
= BPF_MEMWORDS
;
8259 if (cstate
->regused
[cstate
->curreg
])
8260 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8262 cstate
->regused
[cstate
->curreg
] = 1;
8263 return cstate
->curreg
;
8266 bpf_error(cstate
, "too many registers needed to evaluate expression");
8271 * Return a register to the table so it can
8275 free_reg(compiler_state_t
*cstate
, int n
)
8277 cstate
->regused
[n
] = 0;
8280 static struct block
*
8281 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8286 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8287 b
= new_block(cstate
, JMP(jmp
));
8295 gen_greater(compiler_state_t
*cstate
, int n
)
8298 * Catch errors reported by us and routines below us, and return NULL
8301 if (setjmp(cstate
->top_ctx
))
8304 return gen_len(cstate
, BPF_JGE
, n
);
8308 * Actually, this is less than or equal.
8311 gen_less(compiler_state_t
*cstate
, int n
)
8316 * Catch errors reported by us and routines below us, and return NULL
8319 if (setjmp(cstate
->top_ctx
))
8322 b
= gen_len(cstate
, BPF_JGT
, n
);
8329 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8330 * the beginning of the link-layer header.
8331 * XXX - that means you can't test values in the radiotap header, but
8332 * as that header is difficult if not impossible to parse generally
8333 * without a loop, that might not be a severe problem. A new keyword
8334 * "radio" could be added for that, although what you'd really want
8335 * would be a way of testing particular radio header values, which
8336 * would generate code appropriate to the radio header in question.
8339 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8345 * Catch errors reported by us and routines below us, and return NULL
8348 if (setjmp(cstate
->top_ctx
))
8356 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8359 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8363 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8367 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8371 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8375 b
= new_block(cstate
, JMP(BPF_JEQ
));
8382 static const u_char abroadcast
[] = { 0x0 };
8385 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8387 bpf_u_int32 hostmask
;
8388 struct block
*b0
, *b1
, *b2
;
8389 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8392 * Catch errors reported by us and routines below us, and return NULL
8395 if (setjmp(cstate
->top_ctx
))
8402 switch (cstate
->linktype
) {
8404 case DLT_ARCNET_LINUX
:
8405 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8407 case DLT_NETANALYZER
:
8408 case DLT_NETANALYZER_TRANSPARENT
:
8409 b1
= gen_prevlinkhdr_check(cstate
);
8410 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8415 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8417 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8418 case DLT_IEEE802_11
:
8419 case DLT_PRISM_HEADER
:
8420 case DLT_IEEE802_11_RADIO_AVS
:
8421 case DLT_IEEE802_11_RADIO
:
8423 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8424 case DLT_IP_OVER_FC
:
8425 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8427 bpf_error(cstate
, "not a broadcast link");
8433 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8434 * as an indication that we don't know the netmask, and fail
8437 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8438 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8439 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8440 hostmask
= ~cstate
->netmask
;
8441 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8442 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8443 ~0 & hostmask
, hostmask
);
8448 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8453 * Generate code to test the low-order bit of a MAC address (that's
8454 * the bottom bit of the *first* byte).
8456 static struct block
*
8457 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8459 register struct block
*b0
;
8460 register struct slist
*s
;
8462 /* link[offset] & 1 != 0 */
8463 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8464 b0
= new_block(cstate
, JMP(BPF_JSET
));
8471 gen_multicast(compiler_state_t
*cstate
, int proto
)
8473 register struct block
*b0
, *b1
, *b2
;
8474 register struct slist
*s
;
8477 * Catch errors reported by us and routines below us, and return NULL
8480 if (setjmp(cstate
->top_ctx
))
8487 switch (cstate
->linktype
) {
8489 case DLT_ARCNET_LINUX
:
8490 /* all ARCnet multicasts use the same address */
8491 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8493 case DLT_NETANALYZER
:
8494 case DLT_NETANALYZER_TRANSPARENT
:
8495 b1
= gen_prevlinkhdr_check(cstate
);
8496 /* ether[0] & 1 != 0 */
8497 b0
= gen_mac_multicast(cstate
, 0);
8503 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8505 * XXX - was that referring to bit-order issues?
8507 /* fddi[1] & 1 != 0 */
8508 return gen_mac_multicast(cstate
, 1);
8510 /* tr[2] & 1 != 0 */
8511 return gen_mac_multicast(cstate
, 2);
8512 case DLT_IEEE802_11
:
8513 case DLT_PRISM_HEADER
:
8514 case DLT_IEEE802_11_RADIO_AVS
:
8515 case DLT_IEEE802_11_RADIO
:
8520 * For control frames, there is no DA.
8522 * For management frames, DA is at an
8523 * offset of 4 from the beginning of
8526 * For data frames, DA is at an offset
8527 * of 4 from the beginning of the packet
8528 * if To DS is clear and at an offset of
8529 * 16 from the beginning of the packet
8534 * Generate the tests to be done for data frames.
8536 * First, check for To DS set, i.e. "link[1] & 0x01".
8538 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8539 b1
= new_block(cstate
, JMP(BPF_JSET
));
8540 b1
->s
.k
= 0x01; /* To DS */
8544 * If To DS is set, the DA is at 16.
8546 b0
= gen_mac_multicast(cstate
, 16);
8550 * Now, check for To DS not set, i.e. check
8551 * "!(link[1] & 0x01)".
8553 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8554 b2
= new_block(cstate
, JMP(BPF_JSET
));
8555 b2
->s
.k
= 0x01; /* To DS */
8560 * If To DS is not set, the DA is at 4.
8562 b1
= gen_mac_multicast(cstate
, 4);
8566 * Now OR together the last two checks. That gives
8567 * the complete set of checks for data frames.
8572 * Now check for a data frame.
8573 * I.e, check "link[0] & 0x08".
8575 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8576 b1
= new_block(cstate
, JMP(BPF_JSET
));
8581 * AND that with the checks done for data frames.
8586 * If the high-order bit of the type value is 0, this
8587 * is a management frame.
8588 * I.e, check "!(link[0] & 0x08)".
8590 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8591 b2
= new_block(cstate
, JMP(BPF_JSET
));
8597 * For management frames, the DA is at 4.
8599 b1
= gen_mac_multicast(cstate
, 4);
8603 * OR that with the checks done for data frames.
8604 * That gives the checks done for management and
8610 * If the low-order bit of the type value is 1,
8611 * this is either a control frame or a frame
8612 * with a reserved type, and thus not a
8615 * I.e., check "!(link[0] & 0x04)".
8617 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8618 b1
= new_block(cstate
, JMP(BPF_JSET
));
8624 * AND that with the checks for data and management
8629 case DLT_IP_OVER_FC
:
8630 b0
= gen_mac_multicast(cstate
, 2);
8635 /* Link not known to support multicasts */
8639 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8640 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8645 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8646 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8650 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8655 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8657 register struct block
*b0
;
8660 * Catch errors reported by us and routines below us, and return NULL
8663 if (setjmp(cstate
->top_ctx
))
8667 * Only some data link types support ifindex qualifiers.
8669 switch (cstate
->linktype
) {
8670 case DLT_LINUX_SLL2
:
8671 /* match packets on this interface */
8672 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8675 #if defined(__linux__)
8677 * This is Linux; we require PF_PACKET support.
8678 * If this is a *live* capture, we can look at
8679 * special meta-data in the filter expression;
8680 * if it's a savefile, we can't.
8682 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8683 /* We have a FILE *, so this is a savefile */
8684 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8685 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8689 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8691 #else /* defined(__linux__) */
8692 bpf_error(cstate
, "ifindex not supported on %s",
8693 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8695 #endif /* defined(__linux__) */
8701 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8702 * Outbound traffic is sent by this machine, while inbound traffic is
8703 * sent by a remote machine (and may include packets destined for a
8704 * unicast or multicast link-layer address we are not subscribing to).
8705 * These are the same definitions implemented by pcap_setdirection().
8706 * Capturing only unicast traffic destined for this host is probably
8707 * better accomplished using a higher-layer filter.
8710 gen_inbound(compiler_state_t
*cstate
, int dir
)
8712 register struct block
*b0
;
8715 * Catch errors reported by us and routines below us, and return NULL
8718 if (setjmp(cstate
->top_ctx
))
8722 * Only some data link types support inbound/outbound qualifiers.
8724 switch (cstate
->linktype
) {
8726 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8727 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8728 gen_loadi_internal(cstate
, 0),
8734 /* match outgoing packets */
8735 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8737 /* match incoming packets */
8738 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8743 /* match outgoing packets */
8744 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8746 /* to filter on inbound traffic, invert the match */
8751 case DLT_LINUX_SLL2
:
8752 /* match outgoing packets */
8753 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8755 /* to filter on inbound traffic, invert the match */
8761 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8762 ((dir
== 0) ? PF_IN
: PF_OUT
));
8767 /* match outgoing packets */
8768 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8770 /* match incoming packets */
8771 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8775 case DLT_JUNIPER_MFR
:
8776 case DLT_JUNIPER_MLFR
:
8777 case DLT_JUNIPER_MLPPP
:
8778 case DLT_JUNIPER_ATM1
:
8779 case DLT_JUNIPER_ATM2
:
8780 case DLT_JUNIPER_PPPOE
:
8781 case DLT_JUNIPER_PPPOE_ATM
:
8782 case DLT_JUNIPER_GGSN
:
8783 case DLT_JUNIPER_ES
:
8784 case DLT_JUNIPER_MONITOR
:
8785 case DLT_JUNIPER_SERVICES
:
8786 case DLT_JUNIPER_ETHER
:
8787 case DLT_JUNIPER_PPP
:
8788 case DLT_JUNIPER_FRELAY
:
8789 case DLT_JUNIPER_CHDLC
:
8790 case DLT_JUNIPER_VP
:
8791 case DLT_JUNIPER_ST
:
8792 case DLT_JUNIPER_ISM
:
8793 case DLT_JUNIPER_VS
:
8794 case DLT_JUNIPER_SRX_E2E
:
8795 case DLT_JUNIPER_FIBRECHANNEL
:
8796 case DLT_JUNIPER_ATM_CEMIC
:
8798 /* juniper flags (including direction) are stored
8799 * the byte after the 3-byte magic number */
8801 /* match outgoing packets */
8802 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8804 /* match incoming packets */
8805 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8811 * If we have packet meta-data indicating a direction,
8812 * and that metadata can be checked by BPF code, check
8813 * it. Otherwise, give up, as this link-layer type has
8814 * nothing in the packet data.
8816 * Currently, the only platform where a BPF filter can
8817 * check that metadata is Linux with the in-kernel
8818 * BPF interpreter. If other packet capture mechanisms
8819 * and BPF filters also supported this, it would be
8820 * nice. It would be even better if they made that
8821 * metadata available so that we could provide it
8822 * with newer capture APIs, allowing it to be saved
8825 #if defined(__linux__)
8827 * This is Linux; we require PF_PACKET support.
8828 * If this is a *live* capture, we can look at
8829 * special meta-data in the filter expression;
8830 * if it's a savefile, we can't.
8832 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8833 /* We have a FILE *, so this is a savefile */
8834 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8835 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8838 /* match outgoing packets */
8839 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8842 /* to filter on inbound traffic, invert the match */
8845 #else /* defined(__linux__) */
8846 bpf_error(cstate
, "inbound/outbound not supported on %s",
8847 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8849 #endif /* defined(__linux__) */
8854 /* PF firewall log matched interface */
8856 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8862 * Catch errors reported by us and routines below us, and return NULL
8865 if (setjmp(cstate
->top_ctx
))
8868 if (cstate
->linktype
!= DLT_PFLOG
) {
8869 bpf_error(cstate
, "ifname supported only on PF linktype");
8872 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8873 off
= offsetof(struct pfloghdr
, ifname
);
8874 if (strlen(ifname
) >= len
) {
8875 bpf_error(cstate
, "ifname interface names can only be %d characters",
8879 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8880 (const u_char
*)ifname
);
8884 /* PF firewall log ruleset name */
8886 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8891 * Catch errors reported by us and routines below us, and return NULL
8894 if (setjmp(cstate
->top_ctx
))
8897 if (cstate
->linktype
!= DLT_PFLOG
) {
8898 bpf_error(cstate
, "ruleset supported only on PF linktype");
8902 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8903 bpf_error(cstate
, "ruleset names can only be %ld characters",
8904 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8908 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8909 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8913 /* PF firewall log rule number */
8915 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8920 * Catch errors reported by us and routines below us, and return NULL
8923 if (setjmp(cstate
->top_ctx
))
8926 if (cstate
->linktype
!= DLT_PFLOG
) {
8927 bpf_error(cstate
, "rnr supported only on PF linktype");
8931 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8936 /* PF firewall log sub-rule number */
8938 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8943 * Catch errors reported by us and routines below us, and return NULL
8946 if (setjmp(cstate
->top_ctx
))
8949 if (cstate
->linktype
!= DLT_PFLOG
) {
8950 bpf_error(cstate
, "srnr supported only on PF linktype");
8954 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8959 /* PF firewall log reason code */
8961 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8966 * Catch errors reported by us and routines below us, and return NULL
8969 if (setjmp(cstate
->top_ctx
))
8972 if (cstate
->linktype
!= DLT_PFLOG
) {
8973 bpf_error(cstate
, "reason supported only on PF linktype");
8977 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8978 (bpf_u_int32
)reason
);
8982 /* PF firewall log action */
8984 gen_pf_action(compiler_state_t
*cstate
, int action
)
8989 * Catch errors reported by us and routines below us, and return NULL
8992 if (setjmp(cstate
->top_ctx
))
8995 if (cstate
->linktype
!= DLT_PFLOG
) {
8996 bpf_error(cstate
, "action supported only on PF linktype");
9000 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
9001 (bpf_u_int32
)action
);
9005 /* IEEE 802.11 wireless header */
9007 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
9012 * Catch errors reported by us and routines below us, and return NULL
9015 if (setjmp(cstate
->top_ctx
))
9018 switch (cstate
->linktype
) {
9020 case DLT_IEEE802_11
:
9021 case DLT_PRISM_HEADER
:
9022 case DLT_IEEE802_11_RADIO_AVS
:
9023 case DLT_IEEE802_11_RADIO
:
9024 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
9028 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
9036 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
9041 * Catch errors reported by us and routines below us, and return NULL
9044 if (setjmp(cstate
->top_ctx
))
9047 switch (cstate
->linktype
) {
9049 case DLT_IEEE802_11
:
9050 case DLT_PRISM_HEADER
:
9051 case DLT_IEEE802_11_RADIO_AVS
:
9052 case DLT_IEEE802_11_RADIO
:
9056 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
9060 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
9061 IEEE80211_FC1_DIR_MASK
);
9067 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
9072 * Catch errors reported by us and routines below us, and return NULL
9075 if (setjmp(cstate
->top_ctx
))
9078 switch (cstate
->linktype
) {
9081 case DLT_ARCNET_LINUX
:
9082 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
9083 q
.proto
== Q_LINK
) {
9084 cstate
->e
= pcap_ether_aton(s
);
9085 if (cstate
->e
== NULL
)
9086 bpf_error(cstate
, "malloc");
9087 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
9092 bpf_error(cstate
, "ARCnet address used in non-arc expression");
9096 bpf_error(cstate
, "aid supported only on ARCnet");
9101 static struct block
*
9102 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
9104 register struct block
*b0
, *b1
;
9107 /* src comes first, different from Ethernet */
9109 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
9112 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
9115 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9116 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9122 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9123 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9128 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9132 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9136 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9140 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9144 bpf_error(cstate
, "'ra' is only supported on 802.11");
9148 bpf_error(cstate
, "'ta' is only supported on 802.11");
9155 static struct block
*
9156 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9158 struct block
*b0
, *b1
;
9160 /* check for VLAN, including 802.1ad and QinQ */
9161 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9162 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9165 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9171 static struct block
*
9172 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9174 if (vlan_num
> 0x0fff) {
9175 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9178 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9181 static struct block
*
9182 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9185 struct block
*b0
, *b1
;
9187 b0
= gen_vlan_tpid_test(cstate
);
9190 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9196 * Both payload and link header type follow the VLAN tags so that
9197 * both need to be updated.
9199 cstate
->off_linkpl
.constant_part
+= 4;
9200 cstate
->off_linktype
.constant_part
+= 4;
9205 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9206 /* add v to variable part of off */
9208 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9209 bpf_u_int32 v
, struct slist
*s
)
9213 if (!off
->is_variable
)
9214 off
->is_variable
= 1;
9216 off
->reg
= alloc_reg(cstate
);
9218 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9221 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9224 s2
= new_stmt(cstate
, BPF_ST
);
9230 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9231 * and link type offsets first
9234 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9238 /* offset determined at run time, shift variable part */
9240 cstate
->is_vlan_vloffset
= 1;
9241 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9242 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9244 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9245 sappend(s
.next
, b_tpid
->head
->stmts
);
9246 b_tpid
->head
->stmts
= s
.next
;
9250 * patch block b_vid (VLAN id test) to load VID value either from packet
9251 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9254 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9256 struct slist
*s
, *s2
, *sjeq
;
9259 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9260 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9262 /* true -> next instructions, false -> beginning of b_vid */
9263 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9265 sjeq
->s
.jf
= b_vid
->stmts
;
9268 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9269 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
9273 /* Jump to the test in b_vid. We need to jump one instruction before
9274 * the end of the b_vid block so that we only skip loading the TCI
9275 * from packet data and not the 'and' instruction extracting VID.
9278 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9280 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9284 /* insert our statements at the beginning of b_vid */
9285 sappend(s
, b_vid
->stmts
);
9290 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9291 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9292 * tag can be either in metadata or in packet data; therefore if the
9293 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9294 * header for VLAN tag. As the decision is done at run time, we need
9295 * update variable part of the offsets
9297 static struct block
*
9298 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9301 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9304 /* generate new filter code based on extracting packet
9306 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9307 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9309 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9314 * This is tricky. We need to insert the statements updating variable
9315 * parts of offsets before the traditional TPID and VID tests so
9316 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9317 * we do not want this update to affect those checks. That's why we
9318 * generate both test blocks first and insert the statements updating
9319 * variable parts of both offsets after that. This wouldn't work if
9320 * there already were variable length link header when entering this
9321 * function but gen_vlan_bpf_extensions() isn't called in that case.
9323 b_tpid
= gen_vlan_tpid_test(cstate
);
9325 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9327 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9332 gen_vlan_patch_vid_test(cstate
, b_vid
);
9342 * support IEEE 802.1Q VLAN trunk over ethernet
9345 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9350 * Catch errors reported by us and routines below us, and return NULL
9353 if (setjmp(cstate
->top_ctx
))
9356 /* can't check for VLAN-encapsulated packets inside MPLS */
9357 if (cstate
->label_stack_depth
> 0)
9358 bpf_error(cstate
, "no VLAN match after MPLS");
9361 * Check for a VLAN packet, and then change the offsets to point
9362 * to the type and data fields within the VLAN packet. Just
9363 * increment the offsets, so that we can support a hierarchy, e.g.
9364 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
9367 * XXX - this is a bit of a kludge. If we were to split the
9368 * compiler into a parser that parses an expression and
9369 * generates an expression tree, and a code generator that
9370 * takes an expression tree (which could come from our
9371 * parser or from some other parser) and generates BPF code,
9372 * we could perhaps make the offsets parameters of routines
9373 * and, in the handler for an "AND" node, pass to subnodes
9374 * other than the VLAN node the adjusted offsets.
9376 * This would mean that "vlan" would, instead of changing the
9377 * behavior of *all* tests after it, change only the behavior
9378 * of tests ANDed with it. That would change the documented
9379 * semantics of "vlan", which might break some expressions.
9380 * However, it would mean that "(vlan and ip) or ip" would check
9381 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9382 * checking only for VLAN-encapsulated IP, so that could still
9383 * be considered worth doing; it wouldn't break expressions
9384 * that are of the form "vlan and ..." or "vlan N and ...",
9385 * which I suspect are the most common expressions involving
9386 * "vlan". "vlan or ..." doesn't necessarily do what the user
9387 * would really want, now, as all the "or ..." tests would
9388 * be done assuming a VLAN, even though the "or" could be viewed
9389 * as meaning "or, if this isn't a VLAN packet...".
9391 switch (cstate
->linktype
) {
9395 * Newer version of the Linux kernel pass around
9396 * packets in which the VLAN tag has been removed
9397 * from the packet data and put into metadata.
9399 * This requires special treatment.
9401 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9402 /* Verify that this is the outer part of the packet and
9403 * not encapsulated somehow. */
9404 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9405 cstate
->off_linkhdr
.constant_part
==
9406 cstate
->off_outermostlinkhdr
.constant_part
) {
9408 * Do we need special VLAN handling?
9410 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9411 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9414 b0
= gen_vlan_no_bpf_extensions(cstate
,
9415 vlan_num
, has_vlan_tag
);
9418 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9422 case DLT_NETANALYZER
:
9423 case DLT_NETANALYZER_TRANSPARENT
:
9424 case DLT_IEEE802_11
:
9425 case DLT_PRISM_HEADER
:
9426 case DLT_IEEE802_11_RADIO_AVS
:
9427 case DLT_IEEE802_11_RADIO
:
9429 * These are either Ethernet packets with an additional
9430 * metadata header (the NetAnalyzer types), or 802.11
9431 * packets, possibly with an additional metadata header.
9433 * For the first of those, the VLAN tag is in the normal
9434 * place, so the special-case handling above isn't
9437 * For the second of those, we don't do the special-case
9440 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9444 bpf_error(cstate
, "no VLAN support for %s",
9445 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9449 cstate
->vlan_stack_depth
++;
9457 * The label_num_arg dance is to avoid annoying whining by compilers that
9458 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9459 * It's not *used* after setjmp returns.
9461 static struct block
*
9462 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9465 struct block
*b0
, *b1
;
9467 if (cstate
->label_stack_depth
> 0) {
9468 /* just match the bottom-of-stack bit clear */
9469 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9472 * We're not in an MPLS stack yet, so check the link-layer
9473 * type against MPLS.
9475 switch (cstate
->linktype
) {
9477 case DLT_C_HDLC
: /* fall through */
9480 case DLT_NETANALYZER
:
9481 case DLT_NETANALYZER_TRANSPARENT
:
9482 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9486 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9489 /* FIXME add other DLT_s ...
9490 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9491 * leave it for now */
9494 bpf_error(cstate
, "no MPLS support for %s",
9495 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9500 /* If a specific MPLS label is requested, check it */
9501 if (has_label_num
) {
9502 if (label_num
> 0xFFFFF) {
9503 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9504 label_num
, 0xFFFFF);
9506 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9507 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9508 0xfffff000); /* only compare the first 20 bits */
9514 * Change the offsets to point to the type and data fields within
9515 * the MPLS packet. Just increment the offsets, so that we
9516 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9517 * capture packets with an outer label of 100000 and an inner
9520 * Increment the MPLS stack depth as well; this indicates that
9521 * we're checking MPLS-encapsulated headers, to make sure higher
9522 * level code generators don't try to match against IP-related
9523 * protocols such as Q_ARP, Q_RARP etc.
9525 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9527 cstate
->off_nl_nosnap
+= 4;
9528 cstate
->off_nl
+= 4;
9529 cstate
->label_stack_depth
++;
9534 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9537 * Catch errors reported by us and routines below us, and return NULL
9540 if (setjmp(cstate
->top_ctx
))
9543 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9547 * Support PPPOE discovery and session.
9550 gen_pppoed(compiler_state_t
*cstate
)
9553 * Catch errors reported by us and routines below us, and return NULL
9556 if (setjmp(cstate
->top_ctx
))
9559 /* check for PPPoE discovery */
9560 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9564 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9566 struct block
*b0
, *b1
;
9569 * Catch errors reported by us and routines below us, and return NULL
9572 if (setjmp(cstate
->top_ctx
))
9576 * Test against the PPPoE session link-layer type.
9578 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9580 /* If a specific session is requested, check PPPoE session id */
9582 if (sess_num
> 0x0000ffff) {
9583 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9584 sess_num
, 0x0000ffff);
9586 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9592 * Change the offsets to point to the type and data fields within
9593 * the PPP packet, and note that this is PPPoE rather than
9596 * XXX - this is a bit of a kludge. See the comments in
9599 * The "network-layer" protocol is PPPoE, which has a 6-byte
9600 * PPPoE header, followed by a PPP packet.
9602 * There is no HDLC encapsulation for the PPP packet (it's
9603 * encapsulated in PPPoES instead), so the link-layer type
9604 * starts at the first byte of the PPP packet. For PPPoE,
9605 * that offset is relative to the beginning of the total
9606 * link-layer payload, including any 802.2 LLC header, so
9607 * it's 6 bytes past cstate->off_nl.
9609 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9610 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9611 cstate
->off_linkpl
.reg
);
9613 cstate
->off_linktype
= cstate
->off_linkhdr
;
9614 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9617 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9622 /* Check that this is Geneve and the VNI is correct if
9623 * specified. Parameterized to handle both IPv4 and IPv6. */
9624 static struct block
*
9625 gen_geneve_check(compiler_state_t
*cstate
,
9626 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9627 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9629 struct block
*b0
, *b1
;
9631 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9633 /* Check that we are operating on version 0. Otherwise, we
9634 * can't decode the rest of the fields. The version is 2 bits
9635 * in the first byte of the Geneve header. */
9636 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9641 if (vni
> 0xffffff) {
9642 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9645 vni
<<= 8; /* VNI is in the upper 3 bytes */
9646 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9654 /* The IPv4 and IPv6 Geneve checks need to do two things:
9655 * - Verify that this actually is Geneve with the right VNI.
9656 * - Place the IP header length (plus variable link prefix if
9657 * needed) into register A to be used later to compute
9658 * the inner packet offsets. */
9659 static struct block
*
9660 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9662 struct block
*b0
, *b1
;
9663 struct slist
*s
, *s1
;
9665 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9667 /* Load the IP header length into A. */
9668 s
= gen_loadx_iphdrlen(cstate
);
9670 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9673 /* Forcibly append these statements to the true condition
9674 * of the protocol check by creating a new block that is
9675 * always true and ANDing them. */
9676 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9685 static struct block
*
9686 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9688 struct block
*b0
, *b1
;
9689 struct slist
*s
, *s1
;
9691 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9693 /* Load the IP header length. We need to account for a
9694 * variable length link prefix if there is one. */
9695 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9697 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9701 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9705 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9709 /* Forcibly append these statements to the true condition
9710 * of the protocol check by creating a new block that is
9711 * always true and ANDing them. */
9712 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9715 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9724 /* We need to store three values based on the Geneve header::
9725 * - The offset of the linktype.
9726 * - The offset of the end of the Geneve header.
9727 * - The offset of the end of the encapsulated MAC header. */
9728 static struct slist
*
9729 gen_geneve_offsets(compiler_state_t
*cstate
)
9731 struct slist
*s
, *s1
, *s_proto
;
9733 /* First we need to calculate the offset of the Geneve header
9734 * itself. This is composed of the IP header previously calculated
9735 * (include any variable link prefix) and stored in A plus the
9736 * fixed sized headers (fixed link prefix, MAC length, and UDP
9738 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9739 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9741 /* Stash this in X since we'll need it later. */
9742 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9745 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9747 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9751 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9752 cstate
->off_linktype
.is_variable
= 1;
9753 cstate
->off_linktype
.constant_part
= 0;
9755 s1
= new_stmt(cstate
, BPF_ST
);
9756 s1
->s
.k
= cstate
->off_linktype
.reg
;
9759 /* Load the Geneve option length and mask and shift to get the
9760 * number of bytes. It is stored in the first byte of the Geneve
9762 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9766 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9770 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9774 /* Add in the rest of the Geneve base header. */
9775 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9779 /* Add the Geneve header length to its offset and store. */
9780 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9784 /* Set the encapsulated type as Ethernet. Even though we may
9785 * not actually have Ethernet inside there are two reasons this
9787 * - The linktype field is always in EtherType format regardless
9788 * of whether it is in Geneve or an inner Ethernet frame.
9789 * - The only link layer that we have specific support for is
9790 * Ethernet. We will confirm that the packet actually is
9791 * Ethernet at runtime before executing these checks. */
9792 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9794 s1
= new_stmt(cstate
, BPF_ST
);
9795 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9798 /* Calculate whether we have an Ethernet header or just raw IP/
9799 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9800 * and linktype by 14 bytes so that the network header can be found
9801 * seamlessly. Otherwise, keep what we've calculated already. */
9803 /* We have a bare jmp so we can't use the optimizer. */
9804 cstate
->no_optimize
= 1;
9806 /* Load the EtherType in the Geneve header, 2 bytes in. */
9807 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9811 /* Load X with the end of the Geneve header. */
9812 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9813 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9816 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9817 * end of this check, we should have the total length in X. In
9818 * the non-Ethernet case, it's already there. */
9819 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9820 s_proto
->s
.k
= ETHERTYPE_TEB
;
9821 sappend(s
, s_proto
);
9823 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9827 /* Since this is Ethernet, use the EtherType of the payload
9828 * directly as the linktype. Overwrite what we already have. */
9829 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9833 s1
= new_stmt(cstate
, BPF_ST
);
9834 s1
->s
.k
= cstate
->off_linktype
.reg
;
9837 /* Advance two bytes further to get the end of the Ethernet
9839 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9843 /* Move the result to X. */
9844 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9847 /* Store the final result of our linkpl calculation. */
9848 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9849 cstate
->off_linkpl
.is_variable
= 1;
9850 cstate
->off_linkpl
.constant_part
= 0;
9852 s1
= new_stmt(cstate
, BPF_STX
);
9853 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9862 /* Check to see if this is a Geneve packet. */
9864 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9866 struct block
*b0
, *b1
;
9870 * Catch errors reported by us and routines below us, and return NULL
9873 if (setjmp(cstate
->top_ctx
))
9876 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9877 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9882 /* Later filters should act on the payload of the Geneve frame,
9883 * update all of the header pointers. Attach this code so that
9884 * it gets executed in the event that the Geneve filter matches. */
9885 s
= gen_geneve_offsets(cstate
);
9887 b1
= gen_true(cstate
);
9888 sappend(s
, b1
->stmts
);
9893 cstate
->is_encap
= 1;
9898 /* Check that this is VXLAN and the VNI is correct if
9899 * specified. Parameterized to handle both IPv4 and IPv6. */
9900 static struct block
*
9901 gen_vxlan_check(compiler_state_t
*cstate
,
9902 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9903 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9905 struct block
*b0
, *b1
;
9907 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9909 /* Check that the VXLAN header has the flag bits set
9911 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9916 if (vni
> 0xffffff) {
9917 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9920 vni
<<= 8; /* VNI is in the upper 3 bytes */
9921 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9929 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9930 * - Verify that this actually is VXLAN with the right VNI.
9931 * - Place the IP header length (plus variable link prefix if
9932 * needed) into register A to be used later to compute
9933 * the inner packet offsets. */
9934 static struct block
*
9935 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9937 struct block
*b0
, *b1
;
9938 struct slist
*s
, *s1
;
9940 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9942 /* Load the IP header length into A. */
9943 s
= gen_loadx_iphdrlen(cstate
);
9945 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9948 /* Forcibly append these statements to the true condition
9949 * of the protocol check by creating a new block that is
9950 * always true and ANDing them. */
9951 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9960 static struct block
*
9961 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9963 struct block
*b0
, *b1
;
9964 struct slist
*s
, *s1
;
9966 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9968 /* Load the IP header length. We need to account for a
9969 * variable length link prefix if there is one. */
9970 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9972 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9976 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9980 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9984 /* Forcibly append these statements to the true condition
9985 * of the protocol check by creating a new block that is
9986 * always true and ANDing them. */
9987 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9990 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9999 /* We need to store three values based on the VXLAN header:
10000 * - The offset of the linktype.
10001 * - The offset of the end of the VXLAN header.
10002 * - The offset of the end of the encapsulated MAC header. */
10003 static struct slist
*
10004 gen_vxlan_offsets(compiler_state_t
*cstate
)
10006 struct slist
*s
, *s1
;
10008 /* Calculate the offset of the VXLAN header itself. This
10009 * includes the IP header computed previously (including any
10010 * variable link prefix) and stored in A plus the fixed size
10011 * headers (fixed link prefix, MAC length, UDP header). */
10012 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10013 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
10015 /* Add the VXLAN header length to its offset and store */
10016 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10020 /* Push the link header. VXLAN packets always contain Ethernet
10022 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
10024 s1
= new_stmt(cstate
, BPF_ST
);
10025 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
10028 /* As the payload is an Ethernet packet, we can use the
10029 * EtherType of the payload directly as the linktype. */
10030 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10034 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
10035 cstate
->off_linktype
.is_variable
= 1;
10036 cstate
->off_linktype
.constant_part
= 0;
10038 s1
= new_stmt(cstate
, BPF_ST
);
10039 s1
->s
.k
= cstate
->off_linktype
.reg
;
10042 /* Two bytes further is the end of the Ethernet header and the
10043 * start of the payload. */
10044 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10048 /* Move the result to X. */
10049 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10052 /* Store the final result of our linkpl calculation. */
10053 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
10054 cstate
->off_linkpl
.is_variable
= 1;
10055 cstate
->off_linkpl
.constant_part
= 0;
10057 s1
= new_stmt(cstate
, BPF_STX
);
10058 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10061 cstate
->off_nl
= 0;
10066 /* Check to see if this is a VXLAN packet. */
10068 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
10070 struct block
*b0
, *b1
;
10074 * Catch errors reported by us and routines below us, and return NULL
10077 if (setjmp(cstate
->top_ctx
))
10080 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
10081 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
10086 /* Later filters should act on the payload of the VXLAN frame,
10087 * update all of the header pointers. Attach this code so that
10088 * it gets executed in the event that the VXLAN filter matches. */
10089 s
= gen_vxlan_offsets(cstate
);
10091 b1
= gen_true(cstate
);
10092 sappend(s
, b1
->stmts
);
10097 cstate
->is_encap
= 1;
10102 /* Check that the encapsulated frame has a link layer header
10103 * for Ethernet filters. */
10104 static struct block
*
10105 gen_encap_ll_check(compiler_state_t
*cstate
)
10108 struct slist
*s
, *s1
;
10110 /* The easiest way to see if there is a link layer present
10111 * is to check if the link layer header and payload are not
10114 /* Geneve always generates pure variable offsets so we can
10115 * compare only the registers. */
10116 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
10117 s
->s
.k
= cstate
->off_linkhdr
.reg
;
10119 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
10120 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10123 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10131 static struct block
*
10132 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
10133 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10137 switch (atmfield
) {
10140 if (!cstate
->is_atm
)
10141 bpf_error(cstate
, "'vpi' supported only on raw ATM");
10142 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10144 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10145 0xffffffffU
, jtype
, reverse
, jvalue
);
10149 if (!cstate
->is_atm
)
10150 bpf_error(cstate
, "'vci' supported only on raw ATM");
10151 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10153 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10154 0xffffffffU
, jtype
, reverse
, jvalue
);
10158 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10159 abort(); /* XXX - this isn't on FreeBSD */
10160 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10161 0x0fU
, jtype
, reverse
, jvalue
);
10165 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10167 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10168 0xffffffffU
, jtype
, reverse
, jvalue
);
10171 case A_CALLREFTYPE
:
10172 if (!cstate
->is_atm
)
10173 bpf_error(cstate
, "'callref' supported only on raw ATM");
10174 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10176 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10177 0xffffffffU
, jtype
, reverse
, jvalue
);
10186 static struct block
*
10187 gen_atmtype_metac(compiler_state_t
*cstate
)
10189 struct block
*b0
, *b1
;
10191 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10192 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10197 static struct block
*
10198 gen_atmtype_sc(compiler_state_t
*cstate
)
10200 struct block
*b0
, *b1
;
10202 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10203 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10208 static struct block
*
10209 gen_atmtype_llc(compiler_state_t
*cstate
)
10213 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10214 cstate
->linktype
= cstate
->prevlinktype
;
10219 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10220 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10223 * Catch errors reported by us and routines below us, and return NULL
10226 if (setjmp(cstate
->top_ctx
))
10229 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10234 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10236 struct block
*b0
, *b1
;
10239 * Catch errors reported by us and routines below us, and return NULL
10242 if (setjmp(cstate
->top_ctx
))
10248 /* Get all packets in Meta signalling Circuit */
10249 if (!cstate
->is_atm
)
10250 bpf_error(cstate
, "'metac' supported only on raw ATM");
10251 b1
= gen_atmtype_metac(cstate
);
10255 /* Get all packets in Broadcast Circuit*/
10256 if (!cstate
->is_atm
)
10257 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10258 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10259 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10264 /* Get all cells in Segment OAM F4 circuit*/
10265 if (!cstate
->is_atm
)
10266 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10267 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10268 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10273 /* Get all cells in End-to-End OAM F4 Circuit*/
10274 if (!cstate
->is_atm
)
10275 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10276 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10277 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10282 /* Get all packets in connection Signalling Circuit */
10283 if (!cstate
->is_atm
)
10284 bpf_error(cstate
, "'sc' supported only on raw ATM");
10285 b1
= gen_atmtype_sc(cstate
);
10289 /* Get all packets in ILMI Circuit */
10290 if (!cstate
->is_atm
)
10291 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10292 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10293 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10298 /* Get all LANE packets */
10299 if (!cstate
->is_atm
)
10300 bpf_error(cstate
, "'lane' supported only on raw ATM");
10301 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10304 * Arrange that all subsequent tests assume LANE
10305 * rather than LLC-encapsulated packets, and set
10306 * the offsets appropriately for LANE-encapsulated
10309 * We assume LANE means Ethernet, not Token Ring.
10311 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10312 cstate
->off_payload
+ 2, /* Ethernet header */
10314 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10315 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10316 cstate
->off_nl
= 0; /* Ethernet II */
10317 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10321 /* Get all LLC-encapsulated packets */
10322 if (!cstate
->is_atm
)
10323 bpf_error(cstate
, "'llc' supported only on raw ATM");
10324 b1
= gen_atmtype_llc(cstate
);
10334 * Filtering for MTP2 messages based on li value
10335 * FISU, length is null
10336 * LSSU, length is 1 or 2
10337 * MSU, length is 3 or more
10338 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10341 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10343 struct block
*b0
, *b1
;
10346 * Catch errors reported by us and routines below us, and return NULL
10349 if (setjmp(cstate
->top_ctx
))
10355 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10356 (cstate
->linktype
!= DLT_ERF
) &&
10357 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10358 bpf_error(cstate
, "'fisu' supported only on MTP2");
10359 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10360 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10361 0x3fU
, BPF_JEQ
, 0, 0U);
10365 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10366 (cstate
->linktype
!= DLT_ERF
) &&
10367 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10368 bpf_error(cstate
, "'lssu' supported only on MTP2");
10369 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10370 0x3fU
, BPF_JGT
, 1, 2U);
10371 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10372 0x3fU
, BPF_JGT
, 0, 0U);
10377 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10378 (cstate
->linktype
!= DLT_ERF
) &&
10379 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10380 bpf_error(cstate
, "'msu' supported only on MTP2");
10381 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10382 0x3fU
, BPF_JGT
, 0, 2U);
10386 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10387 (cstate
->linktype
!= DLT_ERF
) &&
10388 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10389 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10390 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10391 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10392 0xff80U
, BPF_JEQ
, 0, 0U);
10396 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10397 (cstate
->linktype
!= DLT_ERF
) &&
10398 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10399 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10400 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10401 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10402 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10403 0xff80U
, BPF_JGT
, 0, 0U);
10408 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10409 (cstate
->linktype
!= DLT_ERF
) &&
10410 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10411 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10412 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10413 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10422 static struct block
*
10423 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10424 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10427 bpf_u_int32 val1
, val2
, val3
;
10433 newoff_sio
= cstate
->off_sio
;
10434 newoff_opc
= cstate
->off_opc
;
10435 newoff_dpc
= cstate
->off_dpc
;
10436 newoff_sls
= cstate
->off_sls
;
10437 switch (mtp3field
) {
10440 newoff_sio
+= 3; /* offset for MTP2_HSL */
10444 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10445 bpf_error(cstate
, "'sio' supported only on SS7");
10446 /* sio coded on 1 byte so max value 255 */
10448 bpf_error(cstate
, "sio value %u too big; max value = 255",
10450 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
10451 jtype
, reverse
, jvalue
);
10459 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10460 bpf_error(cstate
, "'opc' supported only on SS7");
10461 /* opc coded on 14 bits so max value 16383 */
10462 if (jvalue
> 16383)
10463 bpf_error(cstate
, "opc value %u too big; max value = 16383",
10465 /* the following instructions are made to convert jvalue
10466 * to the form used to write opc in an ss7 message*/
10467 val1
= jvalue
& 0x00003c00;
10469 val2
= jvalue
& 0x000003fc;
10471 val3
= jvalue
& 0x00000003;
10473 jvalue
= val1
+ val2
+ val3
;
10474 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
10475 jtype
, reverse
, jvalue
);
10483 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10484 bpf_error(cstate
, "'dpc' supported only on SS7");
10485 /* dpc coded on 14 bits so max value 16383 */
10486 if (jvalue
> 16383)
10487 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
10489 /* the following instructions are made to convert jvalue
10490 * to the forme used to write dpc in an ss7 message*/
10491 val1
= jvalue
& 0x000000ff;
10493 val2
= jvalue
& 0x00003f00;
10495 jvalue
= val1
+ val2
;
10496 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
10497 jtype
, reverse
, jvalue
);
10505 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10506 bpf_error(cstate
, "'sls' supported only on SS7");
10507 /* sls coded on 4 bits so max value 15 */
10509 bpf_error(cstate
, "sls value %u too big; max value = 15",
10511 /* the following instruction is made to convert jvalue
10512 * to the forme used to write sls in an ss7 message*/
10513 jvalue
= jvalue
<< 4;
10514 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
10515 jtype
, reverse
, jvalue
);
10525 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10526 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10529 * Catch errors reported by us and routines below us, and return NULL
10532 if (setjmp(cstate
->top_ctx
))
10535 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10539 static struct block
*
10540 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10545 * Q.2931 signalling protocol messages for handling virtual circuits
10546 * establishment and teardown
10551 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10554 case A_CALLPROCEED
:
10555 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10559 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10563 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10567 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10570 case A_RELEASE_DONE
:
10571 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10581 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10583 struct block
*b0
, *b1
;
10586 * Catch errors reported by us and routines below us, and return NULL
10589 if (setjmp(cstate
->top_ctx
))
10595 if (!cstate
->is_atm
)
10596 bpf_error(cstate
, "'oam' supported only on raw ATM");
10598 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10599 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10601 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10606 if (!cstate
->is_atm
)
10607 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10609 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10610 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10612 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10618 * Get Q.2931 signalling messages for switched
10619 * virtual connection
10621 if (!cstate
->is_atm
)
10622 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10623 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10624 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10626 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10628 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10630 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10632 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10634 b0
= gen_atmtype_sc(cstate
);
10638 case A_METACONNECT
:
10639 if (!cstate
->is_atm
)
10640 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10641 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10642 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10644 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10646 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10648 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10650 b0
= gen_atmtype_metac(cstate
);