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>
42 #include "ethertype.h"
45 #include "ieee80211.h"
49 #include "pcap/ipnet.h"
50 #include "diag-control.h"
51 #include "pcap-util.h"
55 #if defined(__linux__)
56 #include <linux/types.h>
57 #include <linux/if_packet.h>
58 #include <linux/filter.h>
62 #define offsetof(s, e) ((size_t)&((s *)0)->e)
67 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
74 uint16_t u6_addr16
[8];
75 uint32_t u6_addr32
[4];
77 #define s6_addr in6_u.u6_addr8
78 #define s6_addr16 in6_u.u6_addr16
79 #define s6_addr32 in6_u.u6_addr32
80 #define s6_addr64 in6_u.u6_addr64
83 typedef unsigned short sa_family_t
;
85 #define __SOCKADDR_COMMON(sa_prefix) \
86 sa_family_t sa_prefix##family
88 /* Ditto, for IPv6. */
91 __SOCKADDR_COMMON (sin6_
);
92 uint16_t sin6_port
; /* Transport layer port # */
93 uint32_t sin6_flowinfo
; /* IPv6 flow information */
94 struct in6_addr sin6_addr
; /* IPv6 address */
97 #ifndef EAI_ADDRFAMILY
99 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
100 int ai_family
; /* PF_xxx */
101 int ai_socktype
; /* SOCK_xxx */
102 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
103 size_t ai_addrlen
; /* length of ai_addr */
104 char *ai_canonname
; /* canonical name for hostname */
105 struct sockaddr
*ai_addr
; /* binary address */
106 struct addrinfo
*ai_next
; /* next structure in linked list */
108 #endif /* EAI_ADDRFAMILY */
109 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
112 #include <netdb.h> /* for "struct addrinfo" */
114 #include <pcap/namedb.h>
116 #include "nametoaddr.h"
118 #define ETHERMTU 1500
120 #ifndef IPPROTO_HOPOPTS
121 #define IPPROTO_HOPOPTS 0
123 #ifndef IPPROTO_ROUTING
124 #define IPPROTO_ROUTING 43
126 #ifndef IPPROTO_FRAGMENT
127 #define IPPROTO_FRAGMENT 44
129 #ifndef IPPROTO_DSTOPTS
130 #define IPPROTO_DSTOPTS 60
133 #define IPPROTO_SCTP 132
136 #define GENEVE_PORT 6081
137 #define VXLAN_PORT 4789
141 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
145 #define ARCTYPE_IP_OLD 240 /* IP protocol */
146 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
149 #define ARCTYPE_IP 212 /* IP protocol */
150 #define ARCTYPE_ARP 213 /* address resolution protocol */
151 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
153 #define ARCTYPE_ATALK 221 /* Appletalk */
154 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
155 #define ARCTYPE_IPX 250 /* Novell IPX */
157 #define ARCTYPE_INET6 0xc4 /* IPng */
158 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
161 /* Based on UNI3.1 standard by ATM Forum */
163 /* ATM traffic types based on VPI=0 and (the following VCI */
164 #define VCI_PPC 0x05 /* Point-to-point signal msg */
165 #define VCI_BCC 0x02 /* Broadcast signal msg */
166 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
167 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
168 #define VCI_METAC 0x01 /* Meta signal msg */
169 #define VCI_ILMIC 0x10 /* ILMI msg */
171 /* Q.2931 signalling messages */
172 #define CALL_PROCEED 0x02 /* call proceeding */
173 #define CONNECT 0x07 /* connect */
174 #define CONNECT_ACK 0x0f /* connect_ack */
175 #define SETUP 0x05 /* setup */
176 #define RELEASE 0x4d /* release */
177 #define RELEASE_DONE 0x5a /* release_done */
178 #define RESTART 0x46 /* restart */
179 #define RESTART_ACK 0x4e /* restart ack */
180 #define STATUS 0x7d /* status */
181 #define STATUS_ENQ 0x75 /* status ack */
182 #define ADD_PARTY 0x80 /* add party */
183 #define ADD_PARTY_ACK 0x81 /* add party ack */
184 #define ADD_PARTY_REJ 0x82 /* add party rej */
185 #define DROP_PARTY 0x83 /* drop party */
186 #define DROP_PARTY_ACK 0x84 /* drop party ack */
188 /* Information Element Parameters in the signalling messages */
189 #define CAUSE 0x08 /* cause */
190 #define ENDPT_REF 0x54 /* endpoint reference */
191 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
192 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
193 #define CONNECT_ID 0x5a /* connection identifier */
194 #define QOS_PARA 0x5c /* quality of service parameters */
195 #define B_HIGHER 0x5d /* broadband higher layer information */
196 #define B_BEARER 0x5e /* broadband bearer capability */
197 #define B_LOWER 0x5f /* broadband lower information */
198 #define CALLING_PARTY 0x6c /* calling party number */
199 #define CALLED_PARTY 0x70 /* called party number */
203 /* Q.2931 signalling general messages format */
204 #define PROTO_POS 0 /* offset of protocol discriminator */
205 #define CALL_REF_POS 2 /* offset of call reference value */
206 #define MSG_TYPE_POS 5 /* offset of message type */
207 #define MSG_LEN_POS 7 /* offset of message length */
208 #define IE_BEGIN_POS 9 /* offset of first information element */
210 /* format of signalling messages */
213 #define FIELD_BEGIN_POS 4
216 /* SunATM header for ATM packet */
217 #define SUNATM_DIR_POS 0
218 #define SUNATM_VPI_POS 1
219 #define SUNATM_VCI_POS 2
220 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
222 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
223 #define PT_LANE 0x01 /* LANE */
224 #define PT_LLC 0x02 /* LLC encapsulation */
225 #define PT_ILMI 0x05 /* ILMI */
226 #define PT_QSAAL 0x06 /* Q.SAAL */
229 /* Types missing from some systems */
232 * Network layer protocol identifiers
235 #define ISO8473_CLNP 0x81
238 #define ISO9542_ESIS 0x82
240 #ifndef ISO9542X25_ESIS
241 #define ISO9542X25_ESIS 0x8a
243 #ifndef ISO10589_ISIS
244 #define ISO10589_ISIS 0x83
247 #define ISIS_L1_LAN_IIH 15
248 #define ISIS_L2_LAN_IIH 16
249 #define ISIS_PTP_IIH 17
250 #define ISIS_L1_LSP 18
251 #define ISIS_L2_LSP 20
252 #define ISIS_L1_CSNP 24
253 #define ISIS_L2_CSNP 25
254 #define ISIS_L1_PSNP 26
255 #define ISIS_L2_PSNP 27
257 #ifndef ISO8878A_CONS
258 #define ISO8878A_CONS 0x84
260 #ifndef ISO10747_IDRP
261 #define ISO10747_IDRP 0x85
265 #ifdef HAVE_OS_PROTO_H
266 #include "os-proto.h"
269 #define JMP(c) ((c)|BPF_JMP|BPF_K)
272 * "Push" the current value of the link-layer header type and link-layer
273 * header offset onto a "stack", and set a new value. (It's not a
274 * full-blown stack; we keep only the top two items.)
276 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
278 (cs)->prevlinktype = (cs)->linktype; \
279 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
280 (cs)->linktype = (new_linktype); \
281 (cs)->off_linkhdr.is_variable = (new_is_variable); \
282 (cs)->off_linkhdr.constant_part = (new_constant_part); \
283 (cs)->off_linkhdr.reg = (new_reg); \
284 (cs)->is_encap = 0; \
288 * Offset "not set" value.
290 #define OFFSET_NOT_SET 0xffffffffU
293 * Absolute offsets, which are offsets from the beginning of the raw
294 * packet data, are, in the general case, the sum of a variable value
295 * and a constant value; the variable value may be absent, in which
296 * case the offset is only the constant value, and the constant value
297 * may be zero, in which case the offset is only the variable value.
299 * bpf_abs_offset is a structure containing all that information:
301 * is_variable is 1 if there's a variable part.
303 * constant_part is the constant part of the value, possibly zero;
305 * if is_variable is 1, reg is the register number for a register
306 * containing the variable value if the register has been assigned,
316 * Value passed to gen_load_a() to indicate what the offset argument
317 * is relative to the beginning of.
320 OR_PACKET
, /* full packet data */
321 OR_LINKHDR
, /* link-layer header */
322 OR_PREVLINKHDR
, /* previous link-layer header */
323 OR_LLC
, /* 802.2 LLC header */
324 OR_PREVMPLSHDR
, /* previous MPLS header */
325 OR_LINKTYPE
, /* link-layer type */
326 OR_LINKPL
, /* link-layer payload */
327 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
328 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
329 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
333 * We divvy out chunks of memory rather than call malloc each time so
334 * we don't have to worry about leaking memory. It's probably
335 * not a big deal if all this memory was wasted but if this ever
336 * goes into a library that would probably not be a good idea.
338 * XXX - this *is* in a library....
341 #define CHUNK0SIZE 1024
348 * A chunk can store any of:
349 * - a string (guaranteed alignment 1 but present for completeness)
353 * For this simple allocator every allocated chunk gets rounded up to the
354 * alignment needed for any chunk.
365 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
367 /* Code generator state */
369 struct _compiler_state
{
380 int outermostlinktype
;
385 /* Hack for handling VLAN and MPLS stacks. */
386 u_int label_stack_depth
;
387 u_int vlan_stack_depth
;
393 * As errors are handled by a longjmp, anything allocated must
394 * be freed in the longjmp handler, so it must be reachable
397 * One thing that's allocated is the result of pcap_nametoaddrinfo();
398 * it must be freed with freeaddrinfo(). This variable points to
399 * any addrinfo structure that would need to be freed.
404 * Another thing that's allocated is the result of pcap_ether_aton();
405 * it must be freed with free(). This variable points to any
406 * address that would need to be freed.
411 * Various code constructs need to know the layout of the packet.
412 * These values give the necessary offsets from the beginning
413 * of the packet data.
417 * Absolute offset of the beginning of the link-layer header.
419 bpf_abs_offset off_linkhdr
;
422 * If we're checking a link-layer header for a packet encapsulated
423 * in another protocol layer, this is the equivalent information
424 * for the previous layers' link-layer header from the beginning
425 * of the raw packet data.
427 bpf_abs_offset off_prevlinkhdr
;
430 * This is the equivalent information for the outermost layers'
433 bpf_abs_offset off_outermostlinkhdr
;
436 * Absolute offset of the beginning of the link-layer payload.
438 bpf_abs_offset off_linkpl
;
441 * "off_linktype" is the offset to information in the link-layer
442 * header giving the packet type. This is an absolute offset
443 * from the beginning of the packet.
445 * For Ethernet, it's the offset of the Ethernet type field; this
446 * means that it must have a value that skips VLAN tags.
448 * For link-layer types that always use 802.2 headers, it's the
449 * offset of the LLC header; this means that it must have a value
450 * that skips VLAN tags.
452 * For PPP, it's the offset of the PPP type field.
454 * For Cisco HDLC, it's the offset of the CHDLC type field.
456 * For BSD loopback, it's the offset of the AF_ value.
458 * For Linux cooked sockets, it's the offset of the type field.
460 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
461 * encapsulation, in which case, IP is assumed.
463 bpf_abs_offset off_linktype
;
466 * TRUE if the link layer includes an ATM pseudo-header.
470 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
471 * causes us to generate code that checks for a Geneve or
472 * VXLAN header respectively and assume that later filters
473 * apply to the encapsulated payload.
478 * TRUE if we need variable length part of VLAN offset
480 int is_vlan_vloffset
;
483 * These are offsets for the ATM pseudo-header.
490 * These are offsets for the MTP2 fields.
496 * These are offsets for the MTP3 fields.
504 * This is the offset of the first byte after the ATM pseudo_header,
505 * or -1 if there is no ATM pseudo-header.
510 * These are offsets to the beginning of the network-layer header.
511 * They are relative to the beginning of the link-layer payload
512 * (i.e., they don't include off_linkhdr.constant_part or
513 * off_linkpl.constant_part).
515 * If the link layer never uses 802.2 LLC:
517 * "off_nl" and "off_nl_nosnap" are the same.
519 * If the link layer always uses 802.2 LLC:
521 * "off_nl" is the offset if there's a SNAP header following
524 * "off_nl_nosnap" is the offset if there's no SNAP header.
526 * If the link layer is Ethernet:
528 * "off_nl" is the offset if the packet is an Ethernet II packet
529 * (we assume no 802.3+802.2+SNAP);
531 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
532 * with an 802.2 header following it.
538 * Here we handle simple allocation of the scratch registers.
539 * If too many registers are alloc'd, the allocator punts.
541 int regused
[BPF_MEMWORDS
];
547 struct chunk chunks
[NCHUNKS
];
552 * For use by routines outside this file.
556 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
561 * If we've already set an error, don't override it.
562 * The lexical analyzer reports some errors by setting
563 * the error and then returning a LEX_ERROR token, which
564 * is not recognized by any grammar rule, and thus forces
565 * the parse to stop. We don't want the error reported
566 * by the lexical analyzer to be overwritten by the syntax
569 if (!cstate
->error_set
) {
571 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
574 cstate
->error_set
= 1;
579 * For use *ONLY* in routines in this file.
581 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
582 PCAP_PRINTFLIKE(2, 3);
585 static void PCAP_NORETURN
586 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
591 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
594 longjmp(cstate
->top_ctx
, 1);
601 static int init_linktype(compiler_state_t
*, pcap_t
*);
603 static void init_regs(compiler_state_t
*);
604 static int alloc_reg(compiler_state_t
*);
605 static void free_reg(compiler_state_t
*, int);
607 static void initchunks(compiler_state_t
*cstate
);
608 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
609 static void *newchunk(compiler_state_t
*cstate
, size_t);
610 static void freechunks(compiler_state_t
*cstate
);
611 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
612 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
613 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
614 static inline void syntax(compiler_state_t
*cstate
);
616 static void backpatch(struct block
*, struct block
*);
617 static void merge(struct block
*, struct block
*);
618 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
620 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
622 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
624 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
626 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
628 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
629 u_int
, bpf_u_int32
, bpf_u_int32
);
630 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
631 u_int
, const u_char
*);
632 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
633 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
634 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
636 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
638 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
639 static struct block
*gen_uncond(compiler_state_t
*, int);
640 static inline struct block
*gen_true(compiler_state_t
*);
641 static inline struct block
*gen_false(compiler_state_t
*);
642 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
643 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
644 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
645 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
646 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
647 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
648 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
649 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
650 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
651 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
653 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
654 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
655 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
656 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
657 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
658 int, bpf_u_int32
, u_int
, u_int
);
660 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
661 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
663 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
664 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
665 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
666 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
667 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
668 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
669 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
670 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
671 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
674 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
675 struct in6_addr
*, int, int, int);
678 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
679 struct addrinfo
*, int, int);
681 static struct block
*gen_ipfrag(compiler_state_t
*);
682 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
683 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
685 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
686 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
688 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
689 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
690 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
692 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
693 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
694 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
695 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
697 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
698 static int lookup_proto(compiler_state_t
*, const char *, int);
699 #if !defined(NO_PROTOCHAIN)
700 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
701 #endif /* !defined(NO_PROTOCHAIN) */
702 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
703 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
704 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
705 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
706 static struct block
*gen_len(compiler_state_t
*, int, int);
707 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
708 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
710 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
711 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
712 bpf_u_int32
, int, int);
713 static struct block
*gen_atmtype_llc(compiler_state_t
*);
714 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
717 initchunks(compiler_state_t
*cstate
)
721 for (i
= 0; i
< NCHUNKS
; i
++) {
722 cstate
->chunks
[i
].n_left
= 0;
723 cstate
->chunks
[i
].m
= NULL
;
725 cstate
->cur_chunk
= 0;
729 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
735 /* Round up to chunk alignment. */
736 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
738 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
739 if (n
> cp
->n_left
) {
741 k
= ++cstate
->cur_chunk
;
743 bpf_set_error(cstate
, "out of memory");
746 size
= CHUNK0SIZE
<< k
;
747 cp
->m
= (void *)malloc(size
);
749 bpf_set_error(cstate
, "out of memory");
752 memset((char *)cp
->m
, 0, size
);
755 bpf_set_error(cstate
, "out of memory");
760 return (void *)((char *)cp
->m
+ cp
->n_left
);
764 newchunk(compiler_state_t
*cstate
, size_t n
)
768 p
= newchunk_nolongjmp(cstate
, n
);
770 longjmp(cstate
->top_ctx
, 1);
777 freechunks(compiler_state_t
*cstate
)
781 for (i
= 0; i
< NCHUNKS
; ++i
)
782 if (cstate
->chunks
[i
].m
!= NULL
)
783 free(cstate
->chunks
[i
].m
);
787 * A strdup whose allocations are freed after code generation is over.
788 * This is used by the lexical analyzer, so it can't longjmp; it just
789 * returns NULL on an allocation error, and the callers must check
793 sdup(compiler_state_t
*cstate
, const char *s
)
795 size_t n
= strlen(s
) + 1;
796 char *cp
= newchunk_nolongjmp(cstate
, n
);
800 pcapint_strlcpy(cp
, s
, n
);
804 static inline struct block
*
805 new_block(compiler_state_t
*cstate
, int code
)
809 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
816 static inline struct slist
*
817 new_stmt(compiler_state_t
*cstate
, int code
)
821 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
827 static struct block
*
828 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
830 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
836 static struct block
*
837 gen_retblk(compiler_state_t
*cstate
, int v
)
839 if (setjmp(cstate
->top_ctx
)) {
841 * gen_retblk() only fails because a memory
842 * allocation failed in newchunk(), meaning
843 * that it can't return a pointer.
849 return gen_retblk_internal(cstate
, v
);
852 static inline PCAP_NORETURN_DEF
void
853 syntax(compiler_state_t
*cstate
)
855 bpf_error(cstate
, "syntax error in filter expression");
859 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
860 const char *buf
, int optimize
, bpf_u_int32 mask
)
866 compiler_state_t cstate
;
867 yyscan_t scanner
= NULL
;
868 YY_BUFFER_STATE in_buffer
= NULL
;
873 * If this pcap_t hasn't been activated, it doesn't have a
874 * link-layer type, so we can't use it.
877 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
878 "not-yet-activated pcap_t passed to pcap_compile");
884 * Initialize Winsock, asking for the latest version (2.2),
885 * as we may be calling Winsock routines to translate
886 * host names to addresses.
888 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
890 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
891 err
, "Error calling WSAStartup()");
898 * If the device on which we're capturing need to be notified
899 * that a new filter is being compiled, do so.
901 * This allows them to save a copy of it, in case, for example,
902 * they're implementing a form of remote packet capture, and
903 * want the remote machine to filter out the packets in which
904 * it's sending the packets it's captured.
906 * XXX - the fact that we happen to be compiling a filter
907 * doesn't necessarily mean we'll be installing it as the
908 * filter for this pcap_t; we might be running it from userland
909 * on captured packets to do packet classification. We really
910 * need a better way of handling this, but this is all that
911 * the WinPcap remote capture code did.
913 if (p
->save_current_filter_op
!= NULL
)
914 (p
->save_current_filter_op
)(p
, buf
);
918 cstate
.no_optimize
= 0;
923 cstate
.ic
.root
= NULL
;
924 cstate
.ic
.cur_mark
= 0;
926 cstate
.error_set
= 0;
929 cstate
.netmask
= mask
;
931 cstate
.snaplen
= pcap_snapshot(p
);
932 if (cstate
.snaplen
== 0) {
933 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
934 "snaplen of 0 rejects all packets");
939 if (pcap_lex_init(&scanner
) != 0) {
940 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
941 errno
, "can't initialize scanner");
945 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
948 * Associate the compiler state with the lexical analyzer
951 pcap_set_extra(&cstate
, scanner
);
953 if (init_linktype(&cstate
, p
) == -1) {
957 if (pcap_parse(scanner
, &cstate
) != 0) {
959 if (cstate
.ai
!= NULL
)
960 freeaddrinfo(cstate
.ai
);
962 if (cstate
.e
!= NULL
)
968 if (cstate
.ic
.root
== NULL
) {
969 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
972 * Catch errors reported by gen_retblk().
974 if (cstate
.ic
.root
== NULL
) {
980 if (optimize
&& !cstate
.no_optimize
) {
981 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
986 if (cstate
.ic
.root
== NULL
||
987 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
988 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
989 "expression rejects all packets");
994 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
995 cstate
.ic
.root
, &len
, p
->errbuf
);
996 if (program
->bf_insns
== NULL
) {
1001 program
->bf_len
= len
;
1003 rc
= 0; /* We're all okay */
1007 * Clean up everything for the lexical analyzer.
1009 if (in_buffer
!= NULL
)
1010 pcap__delete_buffer(in_buffer
, scanner
);
1011 if (scanner
!= NULL
)
1012 pcap_lex_destroy(scanner
);
1015 * Clean up our own allocated memory.
1017 freechunks(&cstate
);
1027 * entry point for using the compiler with no pcap open
1028 * pass in all the stuff that is needed explicitly instead.
1031 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1032 struct bpf_program
*program
,
1033 const char *buf
, int optimize
, bpf_u_int32 mask
)
1038 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1040 return (PCAP_ERROR
);
1041 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1047 * Clean up a "struct bpf_program" by freeing all the memory allocated
1051 pcap_freecode(struct bpf_program
*program
)
1053 program
->bf_len
= 0;
1054 if (program
->bf_insns
!= NULL
) {
1055 free((char *)program
->bf_insns
);
1056 program
->bf_insns
= NULL
;
1061 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1062 * which of the jt and jf fields has been resolved and which is a pointer
1063 * back to another unresolved block (or nil). At least one of the fields
1064 * in each block is already resolved.
1067 backpatch(struct block
*list
, struct block
*target
)
1084 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1085 * which of jt and jf is the link.
1088 merge(struct block
*b0
, struct block
*b1
)
1090 register struct block
**p
= &b0
;
1092 /* Find end of list. */
1094 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1096 /* Concatenate the lists. */
1101 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1103 struct block
*ppi_dlt_check
;
1106 * Catch errors reported by us and routines below us, and return -1
1109 if (setjmp(cstate
->top_ctx
))
1113 * Insert before the statements of the first (root) block any
1114 * statements needed to load the lengths of any variable-length
1115 * headers into registers.
1117 * XXX - a fancier strategy would be to insert those before the
1118 * statements of all blocks that use those lengths and that
1119 * have no predecessors that use them, so that we only compute
1120 * the lengths if we need them. There might be even better
1121 * approaches than that.
1123 * However, those strategies would be more complicated, and
1124 * as we don't generate code to compute a length if the
1125 * program has no tests that use the length, and as most
1126 * tests will probably use those lengths, we would just
1127 * postpone computing the lengths so that it's not done
1128 * for tests that fail early, and it's not clear that's
1131 insert_compute_vloffsets(cstate
, p
->head
);
1134 * For DLT_PPI captures, generate a check of the per-packet
1135 * DLT value to make sure it's DLT_IEEE802_11.
1137 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1138 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1139 * with appropriate Ethernet information and use that rather
1140 * than using something such as DLT_PPI where you don't know
1141 * the link-layer header type until runtime, which, in the
1142 * general case, would force us to generate both Ethernet *and*
1143 * 802.11 code (*and* anything else for which PPI is used)
1144 * and choose between them early in the BPF program?
1146 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
1147 if (ppi_dlt_check
!= NULL
)
1148 gen_and(ppi_dlt_check
, p
);
1150 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1151 p
->sense
= !p
->sense
;
1152 backpatch(p
, gen_retblk_internal(cstate
, 0));
1153 cstate
->ic
.root
= p
->head
;
1158 gen_and(struct block
*b0
, struct block
*b1
)
1160 backpatch(b0
, b1
->head
);
1161 b0
->sense
= !b0
->sense
;
1162 b1
->sense
= !b1
->sense
;
1164 b1
->sense
= !b1
->sense
;
1165 b1
->head
= b0
->head
;
1169 gen_or(struct block
*b0
, struct block
*b1
)
1171 b0
->sense
= !b0
->sense
;
1172 backpatch(b0
, b1
->head
);
1173 b0
->sense
= !b0
->sense
;
1175 b1
->head
= b0
->head
;
1179 gen_not(struct block
*b
)
1181 b
->sense
= !b
->sense
;
1184 static struct block
*
1185 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1186 u_int size
, bpf_u_int32 v
)
1188 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1191 static struct block
*
1192 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1193 u_int size
, bpf_u_int32 v
)
1195 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1198 static struct block
*
1199 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1200 u_int size
, bpf_u_int32 v
)
1202 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1205 static struct block
*
1206 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1207 u_int size
, bpf_u_int32 v
)
1209 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1212 static struct block
*
1213 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1214 u_int size
, bpf_u_int32 v
)
1216 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1219 static struct block
*
1220 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1221 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1223 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1226 static struct block
*
1227 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1228 u_int size
, const u_char
*v
)
1230 register struct block
*b
, *tmp
;
1234 register const u_char
*p
= &v
[size
- 4];
1236 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1244 register const u_char
*p
= &v
[size
- 2];
1246 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1254 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1263 * AND the field of size "size" at offset "offset" relative to the header
1264 * specified by "offrel" with "mask", and compare it with the value "v"
1265 * with the test specified by "jtype"; if "reverse" is true, the test
1266 * should test the opposite of "jtype".
1268 static struct block
*
1269 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1270 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1273 struct slist
*s
, *s2
;
1276 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1278 if (mask
!= 0xffffffff) {
1279 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1284 b
= new_block(cstate
, JMP(jtype
));
1293 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1295 cstate
->pcap_fddipad
= p
->fddipad
;
1298 * We start out with only one link-layer header.
1300 cstate
->outermostlinktype
= pcap_datalink(p
);
1301 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1302 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1303 cstate
->off_outermostlinkhdr
.reg
= -1;
1305 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1306 cstate
->off_prevlinkhdr
.constant_part
= 0;
1307 cstate
->off_prevlinkhdr
.is_variable
= 0;
1308 cstate
->off_prevlinkhdr
.reg
= -1;
1310 cstate
->linktype
= cstate
->outermostlinktype
;
1311 cstate
->off_linkhdr
.constant_part
= 0;
1312 cstate
->off_linkhdr
.is_variable
= 0;
1313 cstate
->off_linkhdr
.reg
= -1;
1318 cstate
->off_linkpl
.constant_part
= 0;
1319 cstate
->off_linkpl
.is_variable
= 0;
1320 cstate
->off_linkpl
.reg
= -1;
1322 cstate
->off_linktype
.constant_part
= 0;
1323 cstate
->off_linktype
.is_variable
= 0;
1324 cstate
->off_linktype
.reg
= -1;
1327 * Assume it's not raw ATM with a pseudo-header, for now.
1330 cstate
->off_vpi
= OFFSET_NOT_SET
;
1331 cstate
->off_vci
= OFFSET_NOT_SET
;
1332 cstate
->off_proto
= OFFSET_NOT_SET
;
1333 cstate
->off_payload
= OFFSET_NOT_SET
;
1336 * And not encapsulated with either Geneve or VXLAN.
1338 cstate
->is_encap
= 0;
1341 * No variable length VLAN offset by default
1343 cstate
->is_vlan_vloffset
= 0;
1346 * And assume we're not doing SS7.
1348 cstate
->off_li
= OFFSET_NOT_SET
;
1349 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1350 cstate
->off_sio
= OFFSET_NOT_SET
;
1351 cstate
->off_opc
= OFFSET_NOT_SET
;
1352 cstate
->off_dpc
= OFFSET_NOT_SET
;
1353 cstate
->off_sls
= OFFSET_NOT_SET
;
1355 cstate
->label_stack_depth
= 0;
1356 cstate
->vlan_stack_depth
= 0;
1358 switch (cstate
->linktype
) {
1361 cstate
->off_linktype
.constant_part
= 2;
1362 cstate
->off_linkpl
.constant_part
= 6;
1363 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1364 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1367 case DLT_ARCNET_LINUX
:
1368 cstate
->off_linktype
.constant_part
= 4;
1369 cstate
->off_linkpl
.constant_part
= 8;
1370 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1371 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1375 cstate
->off_linktype
.constant_part
= 12;
1376 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1377 cstate
->off_nl
= 0; /* Ethernet II */
1378 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1383 * SLIP doesn't have a link level type. The 16 byte
1384 * header is hacked into our SLIP driver.
1386 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1387 cstate
->off_linkpl
.constant_part
= 16;
1389 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1392 case DLT_SLIP_BSDOS
:
1393 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1394 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1396 cstate
->off_linkpl
.constant_part
= 24;
1398 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1403 cstate
->off_linktype
.constant_part
= 0;
1404 cstate
->off_linkpl
.constant_part
= 4;
1406 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1410 cstate
->off_linktype
.constant_part
= 0;
1411 cstate
->off_linkpl
.constant_part
= 12;
1413 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1418 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1419 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1420 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1421 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1422 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1424 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1429 * This does no include the Ethernet header, and
1430 * only covers session state.
1432 cstate
->off_linktype
.constant_part
= 6;
1433 cstate
->off_linkpl
.constant_part
= 8;
1435 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1439 cstate
->off_linktype
.constant_part
= 5;
1440 cstate
->off_linkpl
.constant_part
= 24;
1442 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1447 * FDDI doesn't really have a link-level type field.
1448 * We set "off_linktype" to the offset of the LLC header.
1450 * To check for Ethernet types, we assume that SSAP = SNAP
1451 * is being used and pick out the encapsulated Ethernet type.
1452 * XXX - should we generate code to check for SNAP?
1454 cstate
->off_linktype
.constant_part
= 13;
1455 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1456 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1457 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1458 cstate
->off_nl
= 8; /* 802.2+SNAP */
1459 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1464 * Token Ring doesn't really have a link-level type field.
1465 * We set "off_linktype" to the offset of the LLC header.
1467 * To check for Ethernet types, we assume that SSAP = SNAP
1468 * is being used and pick out the encapsulated Ethernet type.
1469 * XXX - should we generate code to check for SNAP?
1471 * XXX - the header is actually variable-length.
1472 * Some various Linux patched versions gave 38
1473 * as "off_linktype" and 40 as "off_nl"; however,
1474 * if a token ring packet has *no* routing
1475 * information, i.e. is not source-routed, the correct
1476 * values are 20 and 22, as they are in the vanilla code.
1478 * A packet is source-routed iff the uppermost bit
1479 * of the first byte of the source address, at an
1480 * offset of 8, has the uppermost bit set. If the
1481 * packet is source-routed, the total number of bytes
1482 * of routing information is 2 plus bits 0x1F00 of
1483 * the 16-bit value at an offset of 14 (shifted right
1484 * 8 - figure out which byte that is).
1486 cstate
->off_linktype
.constant_part
= 14;
1487 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1488 cstate
->off_nl
= 8; /* 802.2+SNAP */
1489 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1492 case DLT_PRISM_HEADER
:
1493 case DLT_IEEE802_11_RADIO_AVS
:
1494 case DLT_IEEE802_11_RADIO
:
1495 cstate
->off_linkhdr
.is_variable
= 1;
1496 /* Fall through, 802.11 doesn't have a variable link
1497 * prefix but is otherwise the same. */
1500 case DLT_IEEE802_11
:
1502 * 802.11 doesn't really have a link-level type field.
1503 * We set "off_linktype.constant_part" to the offset of
1506 * To check for Ethernet types, we assume that SSAP = SNAP
1507 * is being used and pick out the encapsulated Ethernet type.
1508 * XXX - should we generate code to check for SNAP?
1510 * We also handle variable-length radio headers here.
1511 * The Prism header is in theory variable-length, but in
1512 * practice it's always 144 bytes long. However, some
1513 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1514 * sometimes or always supply an AVS header, so we
1515 * have to check whether the radio header is a Prism
1516 * header or an AVS header, so, in practice, it's
1519 cstate
->off_linktype
.constant_part
= 24;
1520 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1521 cstate
->off_linkpl
.is_variable
= 1;
1522 cstate
->off_nl
= 8; /* 802.2+SNAP */
1523 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1528 * At the moment we treat PPI the same way that we treat
1529 * normal Radiotap encoded packets. The difference is in
1530 * the function that generates the code at the beginning
1531 * to compute the header length. Since this code generator
1532 * of PPI supports bare 802.11 encapsulation only (i.e.
1533 * the encapsulated DLT should be DLT_IEEE802_11) we
1534 * generate code to check for this too.
1536 cstate
->off_linktype
.constant_part
= 24;
1537 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1538 cstate
->off_linkpl
.is_variable
= 1;
1539 cstate
->off_linkhdr
.is_variable
= 1;
1540 cstate
->off_nl
= 8; /* 802.2+SNAP */
1541 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1544 case DLT_ATM_RFC1483
:
1545 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1547 * assume routed, non-ISO PDUs
1548 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1550 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1551 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1552 * latter would presumably be treated the way PPPoE
1553 * should be, so you can do "pppoe and udp port 2049"
1554 * or "pppoa and tcp port 80" and have it check for
1555 * PPPo{A,E} and a PPP protocol of IP and....
1557 cstate
->off_linktype
.constant_part
= 0;
1558 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1559 cstate
->off_nl
= 8; /* 802.2+SNAP */
1560 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1565 * Full Frontal ATM; you get AALn PDUs with an ATM
1569 cstate
->off_vpi
= SUNATM_VPI_POS
;
1570 cstate
->off_vci
= SUNATM_VCI_POS
;
1571 cstate
->off_proto
= PROTO_POS
;
1572 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1573 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1574 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1575 cstate
->off_nl
= 8; /* 802.2+SNAP */
1576 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1582 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1583 cstate
->off_linkpl
.constant_part
= 0;
1585 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1588 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1589 cstate
->off_linktype
.constant_part
= 14;
1590 cstate
->off_linkpl
.constant_part
= 16;
1592 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1595 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1596 cstate
->off_linktype
.constant_part
= 0;
1597 cstate
->off_linkpl
.constant_part
= 20;
1599 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1604 * LocalTalk does have a 1-byte type field in the LLAP header,
1605 * but really it just indicates whether there is a "short" or
1606 * "long" DDP packet following.
1608 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1609 cstate
->off_linkpl
.constant_part
= 0;
1611 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1614 case DLT_IP_OVER_FC
:
1616 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1617 * link-level type field. We set "off_linktype" to the
1618 * offset of the LLC header.
1620 * To check for Ethernet types, we assume that SSAP = SNAP
1621 * is being used and pick out the encapsulated Ethernet type.
1622 * XXX - should we generate code to check for SNAP? RFC
1623 * 2625 says SNAP should be used.
1625 cstate
->off_linktype
.constant_part
= 16;
1626 cstate
->off_linkpl
.constant_part
= 16;
1627 cstate
->off_nl
= 8; /* 802.2+SNAP */
1628 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1633 * XXX - we should set this to handle SNAP-encapsulated
1634 * frames (NLPID of 0x80).
1636 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1637 cstate
->off_linkpl
.constant_part
= 0;
1639 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1643 * the only BPF-interesting FRF.16 frames are non-control frames;
1644 * Frame Relay has a variable length link-layer
1645 * so lets start with offset 4 for now and increments later on (FIXME);
1648 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1649 cstate
->off_linkpl
.constant_part
= 0;
1651 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1654 case DLT_APPLE_IP_OVER_IEEE1394
:
1655 cstate
->off_linktype
.constant_part
= 16;
1656 cstate
->off_linkpl
.constant_part
= 18;
1658 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1661 case DLT_SYMANTEC_FIREWALL
:
1662 cstate
->off_linktype
.constant_part
= 6;
1663 cstate
->off_linkpl
.constant_part
= 44;
1664 cstate
->off_nl
= 0; /* Ethernet II */
1665 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1669 cstate
->off_linktype
.constant_part
= 0;
1670 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1671 cstate
->off_linkpl
.is_variable
= 1;
1673 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1676 case DLT_JUNIPER_MFR
:
1677 case DLT_JUNIPER_MLFR
:
1678 case DLT_JUNIPER_MLPPP
:
1679 case DLT_JUNIPER_PPP
:
1680 case DLT_JUNIPER_CHDLC
:
1681 case DLT_JUNIPER_FRELAY
:
1682 cstate
->off_linktype
.constant_part
= 4;
1683 cstate
->off_linkpl
.constant_part
= 4;
1685 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1688 case DLT_JUNIPER_ATM1
:
1689 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1690 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1692 cstate
->off_nl_nosnap
= 10;
1695 case DLT_JUNIPER_ATM2
:
1696 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1697 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1699 cstate
->off_nl_nosnap
= 10;
1702 /* frames captured on a Juniper PPPoE service PIC
1703 * contain raw ethernet frames */
1704 case DLT_JUNIPER_PPPOE
:
1705 case DLT_JUNIPER_ETHER
:
1706 cstate
->off_linkpl
.constant_part
= 14;
1707 cstate
->off_linktype
.constant_part
= 16;
1708 cstate
->off_nl
= 18; /* Ethernet II */
1709 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1712 case DLT_JUNIPER_PPPOE_ATM
:
1713 cstate
->off_linktype
.constant_part
= 4;
1714 cstate
->off_linkpl
.constant_part
= 6;
1716 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1719 case DLT_JUNIPER_GGSN
:
1720 cstate
->off_linktype
.constant_part
= 6;
1721 cstate
->off_linkpl
.constant_part
= 12;
1723 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1726 case DLT_JUNIPER_ES
:
1727 cstate
->off_linktype
.constant_part
= 6;
1728 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1729 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1730 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1733 case DLT_JUNIPER_MONITOR
:
1734 cstate
->off_linktype
.constant_part
= 12;
1735 cstate
->off_linkpl
.constant_part
= 12;
1736 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1737 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1740 case DLT_BACNET_MS_TP
:
1741 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1742 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1743 cstate
->off_nl
= OFFSET_NOT_SET
;
1744 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1747 case DLT_JUNIPER_SERVICES
:
1748 cstate
->off_linktype
.constant_part
= 12;
1749 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1750 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1751 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1754 case DLT_JUNIPER_VP
:
1755 cstate
->off_linktype
.constant_part
= 18;
1756 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1757 cstate
->off_nl
= OFFSET_NOT_SET
;
1758 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1761 case DLT_JUNIPER_ST
:
1762 cstate
->off_linktype
.constant_part
= 18;
1763 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1764 cstate
->off_nl
= OFFSET_NOT_SET
;
1765 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1768 case DLT_JUNIPER_ISM
:
1769 cstate
->off_linktype
.constant_part
= 8;
1770 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1771 cstate
->off_nl
= OFFSET_NOT_SET
;
1772 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1775 case DLT_JUNIPER_VS
:
1776 case DLT_JUNIPER_SRX_E2E
:
1777 case DLT_JUNIPER_FIBRECHANNEL
:
1778 case DLT_JUNIPER_ATM_CEMIC
:
1779 cstate
->off_linktype
.constant_part
= 8;
1780 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1781 cstate
->off_nl
= OFFSET_NOT_SET
;
1782 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1787 cstate
->off_li_hsl
= 4;
1788 cstate
->off_sio
= 3;
1789 cstate
->off_opc
= 4;
1790 cstate
->off_dpc
= 4;
1791 cstate
->off_sls
= 7;
1792 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1793 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1794 cstate
->off_nl
= OFFSET_NOT_SET
;
1795 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1798 case DLT_MTP2_WITH_PHDR
:
1800 cstate
->off_li_hsl
= 8;
1801 cstate
->off_sio
= 7;
1802 cstate
->off_opc
= 8;
1803 cstate
->off_dpc
= 8;
1804 cstate
->off_sls
= 11;
1805 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1806 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1807 cstate
->off_nl
= OFFSET_NOT_SET
;
1808 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1812 cstate
->off_li
= 22;
1813 cstate
->off_li_hsl
= 24;
1814 cstate
->off_sio
= 23;
1815 cstate
->off_opc
= 24;
1816 cstate
->off_dpc
= 24;
1817 cstate
->off_sls
= 27;
1818 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1819 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1820 cstate
->off_nl
= OFFSET_NOT_SET
;
1821 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1825 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1826 cstate
->off_linkpl
.constant_part
= 4;
1828 cstate
->off_nl_nosnap
= 0;
1833 * Currently, only raw "link[N:M]" filtering is supported.
1835 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1836 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1837 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1838 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1842 cstate
->off_linktype
.constant_part
= 1;
1843 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1845 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1848 case DLT_NETANALYZER
:
1849 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1850 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1851 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1852 cstate
->off_nl
= 0; /* Ethernet II */
1853 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1856 case DLT_NETANALYZER_TRANSPARENT
:
1857 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1858 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1859 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1860 cstate
->off_nl
= 0; /* Ethernet II */
1861 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1866 * For values in the range in which we've assigned new
1867 * DLT_ values, only raw "link[N:M]" filtering is supported.
1869 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1870 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1871 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1872 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1873 cstate
->off_nl
= OFFSET_NOT_SET
;
1874 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1876 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1877 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1883 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1888 * Load a value relative to the specified absolute offset.
1890 static struct slist
*
1891 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1892 u_int offset
, u_int size
)
1894 struct slist
*s
, *s2
;
1896 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1899 * If "s" is non-null, it has code to arrange that the X register
1900 * contains the variable part of the absolute offset, so we
1901 * generate a load relative to that, with an offset of
1902 * abs_offset->constant_part + offset.
1904 * Otherwise, we can do an absolute load with an offset of
1905 * abs_offset->constant_part + offset.
1909 * "s" points to a list of statements that puts the
1910 * variable part of the absolute offset into the X register.
1911 * Do an indirect load, to use the X register as an offset.
1913 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1914 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1918 * There is no variable part of the absolute offset, so
1919 * just do an absolute load.
1921 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1922 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1928 * Load a value relative to the beginning of the specified header.
1930 static struct slist
*
1931 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1934 struct slist
*s
, *s2
;
1937 * Squelch warnings from compilers that *don't* assume that
1938 * offrel always has a valid enum value and therefore don't
1939 * assume that we'll always go through one of the case arms.
1941 * If we have a default case, compilers that *do* assume that
1942 * will then complain about the default case code being
1945 * Damned if you do, damned if you don't.
1952 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1957 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1960 case OR_PREVLINKHDR
:
1961 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1965 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1968 case OR_PREVMPLSHDR
:
1969 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1973 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1976 case OR_LINKPL_NOSNAP
:
1977 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1981 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1986 * Load the X register with the length of the IPv4 header
1987 * (plus the offset of the link-layer header, if it's
1988 * preceded by a variable-length header such as a radio
1989 * header), in bytes.
1991 s
= gen_loadx_iphdrlen(cstate
);
1994 * Load the item at {offset of the link-layer payload} +
1995 * {offset, relative to the start of the link-layer
1996 * payload, of the IPv4 header} + {length of the IPv4 header} +
1997 * {specified offset}.
1999 * If the offset of the link-layer payload is variable,
2000 * the variable part of that offset is included in the
2001 * value in the X register, and we include the constant
2002 * part in the offset of the load.
2004 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2005 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2010 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2017 * Generate code to load into the X register the sum of the length of
2018 * the IPv4 header and the variable part of the offset of the link-layer
2021 static struct slist
*
2022 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2024 struct slist
*s
, *s2
;
2026 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2029 * The offset of the link-layer payload has a variable
2030 * part. "s" points to a list of statements that put
2031 * the variable part of that offset into the X register.
2033 * The 4*([k]&0xf) addressing mode can't be used, as we
2034 * don't have a constant offset, so we have to load the
2035 * value in question into the A register and add to it
2036 * the value from the X register.
2038 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2039 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2041 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2044 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2049 * The A register now contains the length of the IP header.
2050 * We need to add to it the variable part of the offset of
2051 * the link-layer payload, which is still in the X
2052 * register, and move the result into the X register.
2054 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2055 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2058 * The offset of the link-layer payload is a constant,
2059 * so no code was generated to load the (nonexistent)
2060 * variable part of that offset.
2062 * This means we can use the 4*([k]&0xf) addressing
2063 * mode. Load the length of the IPv4 header, which
2064 * is at an offset of cstate->off_nl from the beginning of
2065 * the link-layer payload, and thus at an offset of
2066 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2067 * of the raw packet data, using that addressing mode.
2069 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2070 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2076 static struct block
*
2077 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2082 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2084 b
= new_block(cstate
, JMP(BPF_JEQ
));
2090 static inline struct block
*
2091 gen_true(compiler_state_t
*cstate
)
2093 return gen_uncond(cstate
, 1);
2096 static inline struct block
*
2097 gen_false(compiler_state_t
*cstate
)
2099 return gen_uncond(cstate
, 0);
2103 * Generate code to match a particular packet type.
2105 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2106 * value, if <= ETHERMTU. We use that to determine whether to
2107 * match the type/length field or to check the type/length field for
2108 * a value <= ETHERMTU to see whether it's a type field and then do
2109 * the appropriate test.
2111 static struct block
*
2112 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2114 struct block
*b0
, *b1
;
2120 case LLCSAP_NETBEUI
:
2122 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2123 * so we check the DSAP and SSAP.
2125 * LLCSAP_IP checks for IP-over-802.2, rather
2126 * than IP-over-Ethernet or IP-over-SNAP.
2128 * XXX - should we check both the DSAP and the
2129 * SSAP, like this, or should we check just the
2130 * DSAP, as we do for other types <= ETHERMTU
2131 * (i.e., other SAP values)?
2133 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2135 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2143 * Ethernet_II frames, which are Ethernet
2144 * frames with a frame type of ETHERTYPE_IPX;
2146 * Ethernet_802.3 frames, which are 802.3
2147 * frames (i.e., the type/length field is
2148 * a length field, <= ETHERMTU, rather than
2149 * a type field) with the first two bytes
2150 * after the Ethernet/802.3 header being
2153 * Ethernet_802.2 frames, which are 802.3
2154 * frames with an 802.2 LLC header and
2155 * with the IPX LSAP as the DSAP in the LLC
2158 * Ethernet_SNAP frames, which are 802.3
2159 * frames with an LLC header and a SNAP
2160 * header and with an OUI of 0x000000
2161 * (encapsulated Ethernet) and a protocol
2162 * ID of ETHERTYPE_IPX in the SNAP header.
2164 * XXX - should we generate the same code both
2165 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2169 * This generates code to check both for the
2170 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2172 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2173 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2177 * Now we add code to check for SNAP frames with
2178 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2180 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2184 * Now we generate code to check for 802.3
2185 * frames in general.
2187 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2191 * Now add the check for 802.3 frames before the
2192 * check for Ethernet_802.2 and Ethernet_802.3,
2193 * as those checks should only be done on 802.3
2194 * frames, not on Ethernet frames.
2199 * Now add the check for Ethernet_II frames, and
2200 * do that before checking for the other frame
2203 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2207 case ETHERTYPE_ATALK
:
2208 case ETHERTYPE_AARP
:
2210 * EtherTalk (AppleTalk protocols on Ethernet link
2211 * layer) may use 802.2 encapsulation.
2215 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2216 * we check for an Ethernet type field less than
2217 * 1500, which means it's an 802.3 length field.
2219 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2223 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2224 * SNAP packets with an organization code of
2225 * 0x080007 (Apple, for Appletalk) and a protocol
2226 * type of ETHERTYPE_ATALK (Appletalk).
2228 * 802.2-encapsulated ETHERTYPE_AARP packets are
2229 * SNAP packets with an organization code of
2230 * 0x000000 (encapsulated Ethernet) and a protocol
2231 * type of ETHERTYPE_AARP (Appletalk ARP).
2233 if (ll_proto
== ETHERTYPE_ATALK
)
2234 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2235 else /* ll_proto == ETHERTYPE_AARP */
2236 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2240 * Check for Ethernet encapsulation (Ethertalk
2241 * phase 1?); we just check for the Ethernet
2244 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2250 if (ll_proto
<= ETHERMTU
) {
2252 * This is an LLC SAP value, so the frames
2253 * that match would be 802.2 frames.
2254 * Check that the frame is an 802.2 frame
2255 * (i.e., that the length/type field is
2256 * a length field, <= ETHERMTU) and
2257 * then check the DSAP.
2259 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2261 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2266 * This is an Ethernet type, so compare
2267 * the length/type field with it (if
2268 * the frame is an 802.2 frame, the length
2269 * field will be <= ETHERMTU, and, as
2270 * "ll_proto" is > ETHERMTU, this test
2271 * will fail and the frame won't match,
2272 * which is what we want).
2274 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2279 static struct block
*
2280 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2283 * For DLT_NULL, the link-layer header is a 32-bit word
2284 * containing an AF_ value in *host* byte order, and for
2285 * DLT_ENC, the link-layer header begins with a 32-bit
2286 * word containing an AF_ value in host byte order.
2288 * In addition, if we're reading a saved capture file,
2289 * the host byte order in the capture may not be the
2290 * same as the host byte order on this machine.
2292 * For DLT_LOOP, the link-layer header is a 32-bit
2293 * word containing an AF_ value in *network* byte order.
2295 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2297 * The AF_ value is in host byte order, but the BPF
2298 * interpreter will convert it to network byte order.
2300 * If this is a save file, and it's from a machine
2301 * with the opposite byte order to ours, we byte-swap
2304 * Then we run it through "htonl()", and generate
2305 * code to compare against the result.
2307 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2308 ll_proto
= SWAPLONG(ll_proto
);
2309 ll_proto
= htonl(ll_proto
);
2311 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2315 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2316 * or IPv6 then we have an error.
2318 static struct block
*
2319 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2324 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2327 case ETHERTYPE_IPV6
:
2328 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2335 return gen_false(cstate
);
2339 * Generate code to match a particular packet type.
2341 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2342 * value, if <= ETHERMTU. We use that to determine whether to
2343 * match the type field or to check the type field for the special
2344 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2346 static struct block
*
2347 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2349 struct block
*b0
, *b1
;
2355 case LLCSAP_NETBEUI
:
2357 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2358 * so we check the DSAP and SSAP.
2360 * LLCSAP_IP checks for IP-over-802.2, rather
2361 * than IP-over-Ethernet or IP-over-SNAP.
2363 * XXX - should we check both the DSAP and the
2364 * SSAP, like this, or should we check just the
2365 * DSAP, as we do for other types <= ETHERMTU
2366 * (i.e., other SAP values)?
2368 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2369 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2375 * Ethernet_II frames, which are Ethernet
2376 * frames with a frame type of ETHERTYPE_IPX;
2378 * Ethernet_802.3 frames, which have a frame
2379 * type of LINUX_SLL_P_802_3;
2381 * Ethernet_802.2 frames, which are 802.3
2382 * frames with an 802.2 LLC header (i.e, have
2383 * a frame type of LINUX_SLL_P_802_2) and
2384 * with the IPX LSAP as the DSAP in the LLC
2387 * Ethernet_SNAP frames, which are 802.3
2388 * frames with an LLC header and a SNAP
2389 * header and with an OUI of 0x000000
2390 * (encapsulated Ethernet) and a protocol
2391 * ID of ETHERTYPE_IPX in the SNAP header.
2393 * First, do the checks on LINUX_SLL_P_802_2
2394 * frames; generate the check for either
2395 * Ethernet_802.2 or Ethernet_SNAP frames, and
2396 * then put a check for LINUX_SLL_P_802_2 frames
2399 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2400 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2402 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2406 * Now check for 802.3 frames and OR that with
2407 * the previous test.
2409 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2413 * Now add the check for Ethernet_II frames, and
2414 * do that before checking for the other frame
2417 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2421 case ETHERTYPE_ATALK
:
2422 case ETHERTYPE_AARP
:
2424 * EtherTalk (AppleTalk protocols on Ethernet link
2425 * layer) may use 802.2 encapsulation.
2429 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2430 * we check for the 802.2 protocol type in the
2431 * "Ethernet type" field.
2433 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2436 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2437 * SNAP packets with an organization code of
2438 * 0x080007 (Apple, for Appletalk) and a protocol
2439 * type of ETHERTYPE_ATALK (Appletalk).
2441 * 802.2-encapsulated ETHERTYPE_AARP packets are
2442 * SNAP packets with an organization code of
2443 * 0x000000 (encapsulated Ethernet) and a protocol
2444 * type of ETHERTYPE_AARP (Appletalk ARP).
2446 if (ll_proto
== ETHERTYPE_ATALK
)
2447 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2448 else /* ll_proto == ETHERTYPE_AARP */
2449 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2453 * Check for Ethernet encapsulation (Ethertalk
2454 * phase 1?); we just check for the Ethernet
2457 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2463 if (ll_proto
<= ETHERMTU
) {
2465 * This is an LLC SAP value, so the frames
2466 * that match would be 802.2 frames.
2467 * Check for the 802.2 protocol type
2468 * in the "Ethernet type" field, and
2469 * then check the DSAP.
2471 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2472 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2478 * This is an Ethernet type, so compare
2479 * the length/type field with it (if
2480 * the frame is an 802.2 frame, the length
2481 * field will be <= ETHERMTU, and, as
2482 * "ll_proto" is > ETHERMTU, this test
2483 * will fail and the frame won't match,
2484 * which is what we want).
2486 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2492 * Load a value relative to the beginning of the link-layer header after the
2495 static struct slist
*
2496 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2498 struct slist
*s1
, *s2
;
2501 * Generate code to load the length of the pflog header into
2502 * the register assigned to hold that length, if one has been
2503 * assigned. (If one hasn't been assigned, no code we've
2504 * generated uses that prefix, so we don't need to generate any
2507 if (cstate
->off_linkpl
.reg
!= -1) {
2509 * The length is in the first byte of the header.
2511 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2515 * Round it up to a multiple of 4.
2516 * Add 3, and clear the lower 2 bits.
2518 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2521 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2522 s2
->s
.k
= 0xfffffffc;
2526 * Now allocate a register to hold that value and store
2529 s2
= new_stmt(cstate
, BPF_ST
);
2530 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2534 * Now move it into the X register.
2536 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2544 static struct slist
*
2545 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2547 struct slist
*s1
, *s2
;
2548 struct slist
*sjeq_avs_cookie
;
2549 struct slist
*sjcommon
;
2552 * This code is not compatible with the optimizer, as
2553 * we are generating jmp instructions within a normal
2554 * slist of instructions
2556 cstate
->no_optimize
= 1;
2559 * Generate code to load the length of the radio header into
2560 * the register assigned to hold that length, if one has been
2561 * assigned. (If one hasn't been assigned, no code we've
2562 * generated uses that prefix, so we don't need to generate any
2565 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2566 * or always use the AVS header rather than the Prism header.
2567 * We load a 4-byte big-endian value at the beginning of the
2568 * raw packet data, and see whether, when masked with 0xFFFFF000,
2569 * it's equal to 0x80211000. If so, that indicates that it's
2570 * an AVS header (the masked-out bits are the version number).
2571 * Otherwise, it's a Prism header.
2573 * XXX - the Prism header is also, in theory, variable-length,
2574 * but no known software generates headers that aren't 144
2577 if (cstate
->off_linkhdr
.reg
!= -1) {
2581 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2585 * AND it with 0xFFFFF000.
2587 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2588 s2
->s
.k
= 0xFFFFF000;
2592 * Compare with 0x80211000.
2594 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2595 sjeq_avs_cookie
->s
.k
= 0x80211000;
2596 sappend(s1
, sjeq_avs_cookie
);
2601 * The 4 bytes at an offset of 4 from the beginning of
2602 * the AVS header are the length of the AVS header.
2603 * That field is big-endian.
2605 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2608 sjeq_avs_cookie
->s
.jt
= s2
;
2611 * Now jump to the code to allocate a register
2612 * into which to save the header length and
2613 * store the length there. (The "jump always"
2614 * instruction needs to have the k field set;
2615 * it's added to the PC, so, as we're jumping
2616 * over a single instruction, it should be 1.)
2618 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2620 sappend(s1
, sjcommon
);
2623 * Now for the code that handles the Prism header.
2624 * Just load the length of the Prism header (144)
2625 * into the A register. Have the test for an AVS
2626 * header branch here if we don't have an AVS header.
2628 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2631 sjeq_avs_cookie
->s
.jf
= s2
;
2634 * Now allocate a register to hold that value and store
2635 * it. The code for the AVS header will jump here after
2636 * loading the length of the AVS header.
2638 s2
= new_stmt(cstate
, BPF_ST
);
2639 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2641 sjcommon
->s
.jf
= s2
;
2644 * Now move it into the X register.
2646 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2654 static struct slist
*
2655 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2657 struct slist
*s1
, *s2
;
2660 * Generate code to load the length of the AVS header into
2661 * the register assigned to hold that length, if one has been
2662 * assigned. (If one hasn't been assigned, no code we've
2663 * generated uses that prefix, so we don't need to generate any
2666 if (cstate
->off_linkhdr
.reg
!= -1) {
2668 * The 4 bytes at an offset of 4 from the beginning of
2669 * the AVS header are the length of the AVS header.
2670 * That field is big-endian.
2672 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2676 * Now allocate a register to hold that value and store
2679 s2
= new_stmt(cstate
, BPF_ST
);
2680 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2684 * Now move it into the X register.
2686 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2694 static struct slist
*
2695 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2697 struct slist
*s1
, *s2
;
2700 * Generate code to load the length of the radiotap header into
2701 * the register assigned to hold that length, if one has been
2702 * assigned. (If one hasn't been assigned, no code we've
2703 * generated uses that prefix, so we don't need to generate any
2706 if (cstate
->off_linkhdr
.reg
!= -1) {
2708 * The 2 bytes at offsets of 2 and 3 from the beginning
2709 * of the radiotap header are the length of the radiotap
2710 * header; unfortunately, it's little-endian, so we have
2711 * to load it a byte at a time and construct the value.
2715 * Load the high-order byte, at an offset of 3, shift it
2716 * left a byte, and put the result in the X register.
2718 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2720 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2723 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2727 * Load the next byte, at an offset of 2, and OR the
2728 * value from the X register into it.
2730 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2733 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2737 * Now allocate a register to hold that value and store
2740 s2
= new_stmt(cstate
, BPF_ST
);
2741 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2745 * Now move it into the X register.
2747 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2756 * At the moment we treat PPI as normal Radiotap encoded
2757 * packets. The difference is in the function that generates
2758 * the code at the beginning to compute the header length.
2759 * Since this code generator of PPI supports bare 802.11
2760 * encapsulation only (i.e. the encapsulated DLT should be
2761 * DLT_IEEE802_11) we generate code to check for this too;
2762 * that's done in finish_parse().
2764 static struct slist
*
2765 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2767 struct slist
*s1
, *s2
;
2770 * Generate code to load the length of the radiotap header
2771 * into the register assigned to hold that length, if one has
2774 if (cstate
->off_linkhdr
.reg
!= -1) {
2776 * The 2 bytes at offsets of 2 and 3 from the beginning
2777 * of the radiotap header are the length of the radiotap
2778 * header; unfortunately, it's little-endian, so we have
2779 * to load it a byte at a time and construct the value.
2783 * Load the high-order byte, at an offset of 3, shift it
2784 * left a byte, and put the result in the X register.
2786 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2788 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2791 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2795 * Load the next byte, at an offset of 2, and OR the
2796 * value from the X register into it.
2798 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2801 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2805 * Now allocate a register to hold that value and store
2808 s2
= new_stmt(cstate
, BPF_ST
);
2809 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2813 * Now move it into the X register.
2815 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2824 * Load a value relative to the beginning of the link-layer header after the 802.11
2825 * header, i.e. LLC_SNAP.
2826 * The link-layer header doesn't necessarily begin at the beginning
2827 * of the packet data; there might be a variable-length prefix containing
2828 * radio information.
2830 static struct slist
*
2831 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2834 struct slist
*sjset_data_frame_1
;
2835 struct slist
*sjset_data_frame_2
;
2836 struct slist
*sjset_qos
;
2837 struct slist
*sjset_radiotap_flags_present
;
2838 struct slist
*sjset_radiotap_ext_present
;
2839 struct slist
*sjset_radiotap_tsft_present
;
2840 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2841 struct slist
*s_roundup
;
2843 if (cstate
->off_linkpl
.reg
== -1) {
2845 * No register has been assigned to the offset of
2846 * the link-layer payload, which means nobody needs
2847 * it; don't bother computing it - just return
2848 * what we already have.
2854 * This code is not compatible with the optimizer, as
2855 * we are generating jmp instructions within a normal
2856 * slist of instructions
2858 cstate
->no_optimize
= 1;
2861 * If "s" is non-null, it has code to arrange that the X register
2862 * contains the length of the prefix preceding the link-layer
2865 * Otherwise, the length of the prefix preceding the link-layer
2866 * header is "off_outermostlinkhdr.constant_part".
2870 * There is no variable-length header preceding the
2871 * link-layer header.
2873 * Load the length of the fixed-length prefix preceding
2874 * the link-layer header (if any) into the X register,
2875 * and store it in the cstate->off_linkpl.reg register.
2876 * That length is off_outermostlinkhdr.constant_part.
2878 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2879 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2883 * The X register contains the offset of the beginning of the
2884 * link-layer header; add 24, which is the minimum length
2885 * of the MAC header for a data frame, to that, and store it
2886 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2887 * which is at the offset in the X register, with an indexed load.
2889 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2891 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2894 s2
= new_stmt(cstate
, BPF_ST
);
2895 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2898 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2903 * Check the Frame Control field to see if this is a data frame;
2904 * a data frame has the 0x08 bit (b3) in that field set and the
2905 * 0x04 bit (b2) clear.
2907 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2908 sjset_data_frame_1
->s
.k
= 0x08;
2909 sappend(s
, sjset_data_frame_1
);
2912 * If b3 is set, test b2, otherwise go to the first statement of
2913 * the rest of the program.
2915 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2916 sjset_data_frame_2
->s
.k
= 0x04;
2917 sappend(s
, sjset_data_frame_2
);
2918 sjset_data_frame_1
->s
.jf
= snext
;
2921 * If b2 is not set, this is a data frame; test the QoS bit.
2922 * Otherwise, go to the first statement of the rest of the
2925 sjset_data_frame_2
->s
.jt
= snext
;
2926 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2927 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2928 sappend(s
, sjset_qos
);
2931 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2933 * Otherwise, go to the first statement of the rest of the
2936 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2937 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2939 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2942 s2
= new_stmt(cstate
, BPF_ST
);
2943 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2947 * If we have a radiotap header, look at it to see whether
2948 * there's Atheros padding between the MAC-layer header
2951 * Note: all of the fields in the radiotap header are
2952 * little-endian, so we byte-swap all of the values
2953 * we test against, as they will be loaded as big-endian
2956 * XXX - in the general case, we would have to scan through
2957 * *all* the presence bits, if there's more than one word of
2958 * presence bits. That would require a loop, meaning that
2959 * we wouldn't be able to run the filter in the kernel.
2961 * We assume here that the Atheros adapters that insert the
2962 * annoying padding don't have multiple antennae and therefore
2963 * do not generate radiotap headers with multiple presence words.
2965 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2967 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2968 * in the first presence flag word?
2970 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2974 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2975 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2976 sappend(s
, sjset_radiotap_flags_present
);
2979 * If not, skip all of this.
2981 sjset_radiotap_flags_present
->s
.jf
= snext
;
2984 * Otherwise, is the "extension" bit set in that word?
2986 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2987 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2988 sappend(s
, sjset_radiotap_ext_present
);
2989 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2992 * If so, skip all of this.
2994 sjset_radiotap_ext_present
->s
.jt
= snext
;
2997 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2999 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3000 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3001 sappend(s
, sjset_radiotap_tsft_present
);
3002 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3005 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3006 * at an offset of 16 from the beginning of the raw packet
3007 * data (8 bytes for the radiotap header and 8 bytes for
3010 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3013 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3016 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3018 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3019 sjset_tsft_datapad
->s
.k
= 0x20;
3020 sappend(s
, sjset_tsft_datapad
);
3023 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3024 * at an offset of 8 from the beginning of the raw packet
3025 * data (8 bytes for the radiotap header).
3027 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3030 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3033 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3035 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3036 sjset_notsft_datapad
->s
.k
= 0x20;
3037 sappend(s
, sjset_notsft_datapad
);
3040 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3041 * set, round the length of the 802.11 header to
3042 * a multiple of 4. Do that by adding 3 and then
3043 * dividing by and multiplying by 4, which we do by
3046 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3047 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3048 sappend(s
, s_roundup
);
3049 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3052 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3053 s2
->s
.k
= (bpf_u_int32
)~3;
3055 s2
= new_stmt(cstate
, BPF_ST
);
3056 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3059 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3060 sjset_tsft_datapad
->s
.jf
= snext
;
3061 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3062 sjset_notsft_datapad
->s
.jf
= snext
;
3064 sjset_qos
->s
.jf
= snext
;
3070 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3074 /* There is an implicit dependency between the link
3075 * payload and link header since the payload computation
3076 * includes the variable part of the header. Therefore,
3077 * if nobody else has allocated a register for the link
3078 * header and we need it, do it now. */
3079 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3080 cstate
->off_linkhdr
.reg
== -1)
3081 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3084 * For link-layer types that have a variable-length header
3085 * preceding the link-layer header, generate code to load
3086 * the offset of the link-layer header into the register
3087 * assigned to that offset, if any.
3089 * XXX - this, and the next switch statement, won't handle
3090 * encapsulation of 802.11 or 802.11+radio information in
3091 * some other protocol stack. That's significantly more
3094 switch (cstate
->outermostlinktype
) {
3096 case DLT_PRISM_HEADER
:
3097 s
= gen_load_prism_llprefixlen(cstate
);
3100 case DLT_IEEE802_11_RADIO_AVS
:
3101 s
= gen_load_avs_llprefixlen(cstate
);
3104 case DLT_IEEE802_11_RADIO
:
3105 s
= gen_load_radiotap_llprefixlen(cstate
);
3109 s
= gen_load_ppi_llprefixlen(cstate
);
3118 * For link-layer types that have a variable-length link-layer
3119 * header, generate code to load the offset of the link-layer
3120 * payload into the register assigned to that offset, if any.
3122 switch (cstate
->outermostlinktype
) {
3124 case DLT_IEEE802_11
:
3125 case DLT_PRISM_HEADER
:
3126 case DLT_IEEE802_11_RADIO_AVS
:
3127 case DLT_IEEE802_11_RADIO
:
3129 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3133 s
= gen_load_pflog_llprefixlen(cstate
);
3138 * If there is no initialization yet and we need variable
3139 * length offsets for VLAN, initialize them to zero
3141 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3144 if (cstate
->off_linkpl
.reg
== -1)
3145 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3146 if (cstate
->off_linktype
.reg
== -1)
3147 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3149 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3151 s2
= new_stmt(cstate
, BPF_ST
);
3152 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3154 s2
= new_stmt(cstate
, BPF_ST
);
3155 s2
->s
.k
= cstate
->off_linktype
.reg
;
3160 * If we have any offset-loading code, append all the
3161 * existing statements in the block to those statements,
3162 * and make the resulting list the list of statements
3166 sappend(s
, b
->stmts
);
3171 static struct block
*
3172 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3174 struct slist
*s_load_dlt
;
3177 if (cstate
->linktype
== DLT_PPI
)
3179 /* Create the statements that check for the DLT
3181 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3182 s_load_dlt
->s
.k
= 4;
3184 b
= new_block(cstate
, JMP(BPF_JEQ
));
3186 b
->stmts
= s_load_dlt
;
3187 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3198 * Take an absolute offset, and:
3200 * if it has no variable part, return NULL;
3202 * if it has a variable part, generate code to load the register
3203 * containing that variable part into the X register, returning
3204 * a pointer to that code - if no register for that offset has
3205 * been allocated, allocate it first.
3207 * (The code to set that register will be generated later, but will
3208 * be placed earlier in the code sequence.)
3210 static struct slist
*
3211 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3215 if (off
->is_variable
) {
3216 if (off
->reg
== -1) {
3218 * We haven't yet assigned a register for the
3219 * variable part of the offset of the link-layer
3220 * header; allocate one.
3222 off
->reg
= alloc_reg(cstate
);
3226 * Load the register containing the variable part of the
3227 * offset of the link-layer header into the X register.
3229 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3234 * That offset isn't variable, there's no variable part,
3235 * so we don't need to generate any code.
3242 * Map an Ethernet type to the equivalent PPP type.
3245 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3253 case ETHERTYPE_IPV6
:
3254 ll_proto
= PPP_IPV6
;
3258 ll_proto
= PPP_DECNET
;
3261 case ETHERTYPE_ATALK
:
3262 ll_proto
= PPP_APPLE
;
3275 * I'm assuming the "Bridging PDU"s that go
3276 * over PPP are Spanning Tree Protocol
3279 ll_proto
= PPP_BRPDU
;
3290 * Generate any tests that, for encapsulation of a link-layer packet
3291 * inside another protocol stack, need to be done to check for those
3292 * link-layer packets (and that haven't already been done by a check
3293 * for that encapsulation).
3295 static struct block
*
3296 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3300 if (cstate
->is_encap
)
3301 return gen_encap_ll_check(cstate
);
3303 switch (cstate
->prevlinktype
) {
3307 * This is LANE-encapsulated Ethernet; check that the LANE
3308 * packet doesn't begin with an LE Control marker, i.e.
3309 * that it's data, not a control message.
3311 * (We've already generated a test for LANE.)
3313 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3319 * No such tests are necessary.
3327 * The three different values we should check for when checking for an
3328 * IPv6 packet with DLT_NULL.
3330 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3331 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3332 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3335 * Generate code to match a particular packet type by matching the
3336 * link-layer type field or fields in the 802.2 LLC header.
3338 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3339 * value, if <= ETHERMTU.
3341 static struct block
*
3342 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3344 struct block
*b0
, *b1
, *b2
;
3345 const char *description
;
3347 /* are we checking MPLS-encapsulated packets? */
3348 if (cstate
->label_stack_depth
> 0)
3349 return gen_mpls_linktype(cstate
, ll_proto
);
3351 switch (cstate
->linktype
) {
3354 case DLT_NETANALYZER
:
3355 case DLT_NETANALYZER_TRANSPARENT
:
3356 /* Geneve has an EtherType regardless of whether there is an
3357 * L2 header. VXLAN always has an EtherType. */
3358 if (!cstate
->is_encap
)
3359 b0
= gen_prevlinkhdr_check(cstate
);
3363 b1
= gen_ether_linktype(cstate
, ll_proto
);
3374 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3378 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3382 case DLT_IEEE802_11
:
3383 case DLT_PRISM_HEADER
:
3384 case DLT_IEEE802_11_RADIO_AVS
:
3385 case DLT_IEEE802_11_RADIO
:
3388 * Check that we have a data frame.
3390 b0
= gen_check_802_11_data_frame(cstate
);
3393 * Now check for the specified link-layer type.
3395 b1
= gen_llc_linktype(cstate
, ll_proto
);
3402 * XXX - check for LLC frames.
3404 return gen_llc_linktype(cstate
, ll_proto
);
3409 * XXX - check for LLC PDUs, as per IEEE 802.5.
3411 return gen_llc_linktype(cstate
, ll_proto
);
3414 case DLT_ATM_RFC1483
:
3416 case DLT_IP_OVER_FC
:
3417 return gen_llc_linktype(cstate
, ll_proto
);
3422 * Check for an LLC-encapsulated version of this protocol;
3423 * if we were checking for LANE, linktype would no longer
3426 * Check for LLC encapsulation and then check the protocol.
3428 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3429 b1
= gen_llc_linktype(cstate
, ll_proto
);
3435 return gen_linux_sll_linktype(cstate
, ll_proto
);
3439 case DLT_SLIP_BSDOS
:
3442 * These types don't provide any type field; packets
3443 * are always IPv4 or IPv6.
3445 * XXX - for IPv4, check for a version number of 4, and,
3446 * for IPv6, check for a version number of 6?
3451 /* Check for a version number of 4. */
3452 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3454 case ETHERTYPE_IPV6
:
3455 /* Check for a version number of 6. */
3456 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3459 return gen_false(cstate
); /* always false */
3465 * Raw IPv4, so no type field.
3467 if (ll_proto
== ETHERTYPE_IP
)
3468 return gen_true(cstate
); /* always true */
3470 /* Checking for something other than IPv4; always false */
3471 return gen_false(cstate
);
3476 * Raw IPv6, so no type field.
3478 if (ll_proto
== ETHERTYPE_IPV6
)
3479 return gen_true(cstate
); /* always true */
3481 /* Checking for something other than IPv6; always false */
3482 return gen_false(cstate
);
3487 case DLT_PPP_SERIAL
:
3490 * We use Ethernet protocol types inside libpcap;
3491 * map them to the corresponding PPP protocol types.
3493 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3494 ethertype_to_ppptype(ll_proto
));
3499 * We use Ethernet protocol types inside libpcap;
3500 * map them to the corresponding PPP protocol types.
3506 * Also check for Van Jacobson-compressed IP.
3507 * XXX - do this for other forms of PPP?
3509 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3510 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3512 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3517 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3518 ethertype_to_ppptype(ll_proto
));
3528 return (gen_loopback_linktype(cstate
, AF_INET
));
3530 case ETHERTYPE_IPV6
:
3532 * AF_ values may, unfortunately, be platform-
3533 * dependent; AF_INET isn't, because everybody
3534 * used 4.2BSD's value, but AF_INET6 is, because
3535 * 4.2BSD didn't have a value for it (given that
3536 * IPv6 didn't exist back in the early 1980's),
3537 * and they all picked their own values.
3539 * This means that, if we're reading from a
3540 * savefile, we need to check for all the
3543 * If we're doing a live capture, we only need
3544 * to check for this platform's value; however,
3545 * Npcap uses 24, which isn't Windows's AF_INET6
3546 * value. (Given the multiple different values,
3547 * programs that read pcap files shouldn't be
3548 * checking for their platform's AF_INET6 value
3549 * anyway, they should check for all of the
3550 * possible values. and they might as well do
3551 * that even for live captures.)
3553 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3555 * Savefile - check for all three
3556 * possible IPv6 values.
3558 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3559 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3561 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3566 * Live capture, so we only need to
3567 * check for the value used on this
3572 * Npcap doesn't use Windows's AF_INET6,
3573 * as that collides with AF_IPX on
3574 * some BSDs (both have the value 23).
3575 * Instead, it uses 24.
3577 return (gen_loopback_linktype(cstate
, 24));
3580 return (gen_loopback_linktype(cstate
, AF_INET6
));
3581 #else /* AF_INET6 */
3583 * I guess this platform doesn't support
3584 * IPv6, so we just reject all packets.
3586 return gen_false(cstate
);
3587 #endif /* AF_INET6 */
3593 * Not a type on which we support filtering.
3594 * XXX - support those that have AF_ values
3595 * #defined on this platform, at least?
3597 return gen_false(cstate
);
3602 * af field is host byte order in contrast to the rest of
3605 if (ll_proto
== ETHERTYPE_IP
)
3606 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3608 else if (ll_proto
== ETHERTYPE_IPV6
)
3609 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3612 return gen_false(cstate
);
3616 case DLT_ARCNET_LINUX
:
3618 * XXX should we check for first fragment if the protocol
3624 return gen_false(cstate
);
3626 case ETHERTYPE_IPV6
:
3627 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3631 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3633 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3639 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3641 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3646 case ETHERTYPE_REVARP
:
3647 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3650 case ETHERTYPE_ATALK
:
3651 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3658 case ETHERTYPE_ATALK
:
3659 return gen_true(cstate
);
3661 return gen_false(cstate
);
3667 * XXX - assumes a 2-byte Frame Relay header with
3668 * DLCI and flags. What if the address is longer?
3674 * Check for the special NLPID for IP.
3676 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3678 case ETHERTYPE_IPV6
:
3680 * Check for the special NLPID for IPv6.
3682 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3686 * Check for several OSI protocols.
3688 * Frame Relay packets typically have an OSI
3689 * NLPID at the beginning; we check for each
3692 * What we check for is the NLPID and a frame
3693 * control field of UI, i.e. 0x03 followed
3696 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3697 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3698 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3704 return gen_false(cstate
);
3709 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3711 case DLT_JUNIPER_MFR
:
3712 case DLT_JUNIPER_MLFR
:
3713 case DLT_JUNIPER_MLPPP
:
3714 case DLT_JUNIPER_ATM1
:
3715 case DLT_JUNIPER_ATM2
:
3716 case DLT_JUNIPER_PPPOE
:
3717 case DLT_JUNIPER_PPPOE_ATM
:
3718 case DLT_JUNIPER_GGSN
:
3719 case DLT_JUNIPER_ES
:
3720 case DLT_JUNIPER_MONITOR
:
3721 case DLT_JUNIPER_SERVICES
:
3722 case DLT_JUNIPER_ETHER
:
3723 case DLT_JUNIPER_PPP
:
3724 case DLT_JUNIPER_FRELAY
:
3725 case DLT_JUNIPER_CHDLC
:
3726 case DLT_JUNIPER_VP
:
3727 case DLT_JUNIPER_ST
:
3728 case DLT_JUNIPER_ISM
:
3729 case DLT_JUNIPER_VS
:
3730 case DLT_JUNIPER_SRX_E2E
:
3731 case DLT_JUNIPER_FIBRECHANNEL
:
3732 case DLT_JUNIPER_ATM_CEMIC
:
3734 /* just lets verify the magic number for now -
3735 * on ATM we may have up to 6 different encapsulations on the wire
3736 * and need a lot of heuristics to figure out that the payload
3739 * FIXME encapsulation specific BPF_ filters
3741 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3743 case DLT_BACNET_MS_TP
:
3744 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3747 return gen_ipnet_linktype(cstate
, ll_proto
);
3749 case DLT_LINUX_IRDA
:
3750 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3753 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3756 case DLT_MTP2_WITH_PHDR
:
3757 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3760 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3763 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3765 case DLT_LINUX_LAPD
:
3766 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3768 case DLT_USB_FREEBSD
:
3770 case DLT_USB_LINUX_MMAPPED
:
3772 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3774 case DLT_BLUETOOTH_HCI_H4
:
3775 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3776 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3779 case DLT_CAN_SOCKETCAN
:
3780 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3782 case DLT_IEEE802_15_4
:
3783 case DLT_IEEE802_15_4_LINUX
:
3784 case DLT_IEEE802_15_4_NONASK_PHY
:
3785 case DLT_IEEE802_15_4_NOFCS
:
3786 case DLT_IEEE802_15_4_TAP
:
3787 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3789 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3790 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3793 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3796 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3798 case DLT_IPMB_KONTRON
:
3799 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3802 bpf_error(cstate
, "I2C link-layer type filtering not implemented");
3805 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3808 /* Using the fixed-size NFLOG header it is possible to tell only
3809 * the address family of the packet, other meaningful data is
3810 * either missing or behind TLVs.
3812 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3816 * Does this link-layer header type have a field
3817 * indicating the type of the next protocol? If
3818 * so, off_linktype.constant_part will be the offset of that
3819 * field in the packet; if not, it will be OFFSET_NOT_SET.
3821 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3823 * Yes; assume it's an Ethernet type. (If
3824 * it's not, it needs to be handled specially
3827 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3831 * No; report an error.
3833 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3834 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3842 * Check for an LLC SNAP packet with a given organization code and
3843 * protocol type; we check the entire contents of the 802.2 LLC and
3844 * snap headers, checking for DSAP and SSAP of SNAP and a control
3845 * field of 0x03 in the LLC header, and for the specified organization
3846 * code and protocol type in the SNAP header.
3848 static struct block
*
3849 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3851 u_char snapblock
[8];
3853 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3854 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3855 snapblock
[2] = 0x03; /* control = UI */
3856 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3857 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3858 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3859 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3860 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3861 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3865 * Generate code to match frames with an LLC header.
3867 static struct block
*
3868 gen_llc_internal(compiler_state_t
*cstate
)
3870 struct block
*b0
, *b1
;
3872 switch (cstate
->linktype
) {
3876 * We check for an Ethernet type field less than
3877 * 1500, which means it's an 802.3 length field.
3879 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3883 * Now check for the purported DSAP and SSAP not being
3884 * 0xFF, to rule out NetWare-over-802.3.
3886 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3893 * We check for LLC traffic.
3895 b0
= gen_atmtype_llc(cstate
);
3898 case DLT_IEEE802
: /* Token Ring */
3900 * XXX - check for LLC frames.
3902 return gen_true(cstate
);
3906 * XXX - check for LLC frames.
3908 return gen_true(cstate
);
3910 case DLT_ATM_RFC1483
:
3912 * For LLC encapsulation, these are defined to have an
3915 * For VC encapsulation, they don't, but there's no
3916 * way to check for that; the protocol used on the VC
3917 * is negotiated out of band.
3919 return gen_true(cstate
);
3921 case DLT_IEEE802_11
:
3922 case DLT_PRISM_HEADER
:
3923 case DLT_IEEE802_11_RADIO
:
3924 case DLT_IEEE802_11_RADIO_AVS
:
3927 * Check that we have a data frame.
3929 b0
= gen_check_802_11_data_frame(cstate
);
3933 bpf_error(cstate
, "'llc' not supported for %s",
3934 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3940 gen_llc(compiler_state_t
*cstate
)
3943 * Catch errors reported by us and routines below us, and return NULL
3946 if (setjmp(cstate
->top_ctx
))
3949 return gen_llc_internal(cstate
);
3953 gen_llc_i(compiler_state_t
*cstate
)
3955 struct block
*b0
, *b1
;
3959 * Catch errors reported by us and routines below us, and return NULL
3962 if (setjmp(cstate
->top_ctx
))
3966 * Check whether this is an LLC frame.
3968 b0
= gen_llc_internal(cstate
);
3971 * Load the control byte and test the low-order bit; it must
3972 * be clear for I frames.
3974 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3975 b1
= new_block(cstate
, JMP(BPF_JSET
));
3984 gen_llc_s(compiler_state_t
*cstate
)
3986 struct block
*b0
, *b1
;
3989 * Catch errors reported by us and routines below us, and return NULL
3992 if (setjmp(cstate
->top_ctx
))
3996 * Check whether this is an LLC frame.
3998 b0
= gen_llc_internal(cstate
);
4001 * Now compare the low-order 2 bit of the control byte against
4002 * the appropriate value for S frames.
4004 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4010 gen_llc_u(compiler_state_t
*cstate
)
4012 struct block
*b0
, *b1
;
4015 * Catch errors reported by us and routines below us, and return NULL
4018 if (setjmp(cstate
->top_ctx
))
4022 * Check whether this is an LLC frame.
4024 b0
= gen_llc_internal(cstate
);
4027 * Now compare the low-order 2 bit of the control byte against
4028 * the appropriate value for U frames.
4030 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4036 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4038 struct block
*b0
, *b1
;
4041 * Catch errors reported by us and routines below us, and return NULL
4044 if (setjmp(cstate
->top_ctx
))
4048 * Check whether this is an LLC frame.
4050 b0
= gen_llc_internal(cstate
);
4053 * Now check for an S frame with the appropriate type.
4055 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4061 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4063 struct block
*b0
, *b1
;
4066 * Catch errors reported by us and routines below us, and return NULL
4069 if (setjmp(cstate
->top_ctx
))
4073 * Check whether this is an LLC frame.
4075 b0
= gen_llc_internal(cstate
);
4078 * Now check for a U frame with the appropriate type.
4080 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4086 * Generate code to match a particular packet type, for link-layer types
4087 * using 802.2 LLC headers.
4089 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4090 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4092 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4093 * value, if <= ETHERMTU. We use that to determine whether to
4094 * match the DSAP or both DSAP and LSAP or to check the OUI and
4095 * protocol ID in a SNAP header.
4097 static struct block
*
4098 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4101 * XXX - handle token-ring variable-length header.
4107 case LLCSAP_NETBEUI
:
4109 * XXX - should we check both the DSAP and the
4110 * SSAP, like this, or should we check just the
4111 * DSAP, as we do for other SAP values?
4113 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4114 ((ll_proto
<< 8) | ll_proto
));
4118 * XXX - are there ever SNAP frames for IPX on
4119 * non-Ethernet 802.x networks?
4121 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4123 case ETHERTYPE_ATALK
:
4125 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4126 * SNAP packets with an organization code of
4127 * 0x080007 (Apple, for Appletalk) and a protocol
4128 * type of ETHERTYPE_ATALK (Appletalk).
4130 * XXX - check for an organization code of
4131 * encapsulated Ethernet as well?
4133 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4137 * XXX - we don't have to check for IPX 802.3
4138 * here, but should we check for the IPX Ethertype?
4140 if (ll_proto
<= ETHERMTU
) {
4142 * This is an LLC SAP value, so check
4145 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4148 * This is an Ethernet type; we assume that it's
4149 * unlikely that it'll appear in the right place
4150 * at random, and therefore check only the
4151 * location that would hold the Ethernet type
4152 * in a SNAP frame with an organization code of
4153 * 0x000000 (encapsulated Ethernet).
4155 * XXX - if we were to check for the SNAP DSAP and
4156 * LSAP, as per XXX, and were also to check for an
4157 * organization code of 0x000000 (encapsulated
4158 * Ethernet), we'd do
4160 * return gen_snap(cstate, 0x000000, ll_proto);
4162 * here; for now, we don't, as per the above.
4163 * I don't know whether it's worth the extra CPU
4164 * time to do the right check or not.
4166 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4171 static struct block
*
4172 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4173 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4175 struct block
*b0
, *b1
;
4189 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4190 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4196 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4197 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4202 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4206 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4210 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4214 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4218 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4222 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4229 b0
= gen_linktype(cstate
, ll_proto
);
4230 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4236 static struct block
*
4237 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4238 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4241 struct block
*b0
, *b1
;
4244 * Code below needs to access four separate 32-bit parts of the 128-bit
4245 * IPv6 address and mask. In some OSes this is as simple as using the
4246 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4247 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4248 * far as libpcap sees it. Hence copy the data before use to avoid
4249 * potential unaligned memory access and the associated compiler
4250 * warnings (whether genuine or not).
4252 bpf_u_int32 a
[4], m
[4];
4265 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4266 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4272 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4273 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4278 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4282 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4286 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4290 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4294 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4298 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4305 /* this order is important */
4306 memcpy(a
, addr
, sizeof(a
));
4307 memcpy(m
, mask
, sizeof(m
));
4308 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4309 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4311 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4313 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4315 b0
= gen_linktype(cstate
, ll_proto
);
4321 static struct block
*
4322 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4324 register struct block
*b0
, *b1
;
4328 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4331 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4334 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4335 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4341 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4342 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4347 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4351 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4355 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4359 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4363 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4367 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4375 * Like gen_ehostop, but for DLT_FDDI
4377 static struct block
*
4378 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4380 struct block
*b0
, *b1
;
4384 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4387 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4390 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4391 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4397 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4398 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4403 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4407 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4411 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4415 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4419 bpf_error(cstate
, "'ra' is only supported on 802.11");
4423 bpf_error(cstate
, "'ta' is only supported on 802.11");
4431 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4433 static struct block
*
4434 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4436 register struct block
*b0
, *b1
;
4440 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4443 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4446 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4447 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4453 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4454 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4459 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4463 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4467 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4471 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4475 bpf_error(cstate
, "'ra' is only supported on 802.11");
4479 bpf_error(cstate
, "'ta' is only supported on 802.11");
4487 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4488 * various 802.11 + radio headers.
4490 static struct block
*
4491 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4493 register struct block
*b0
, *b1
, *b2
;
4494 register struct slist
*s
;
4496 #ifdef ENABLE_WLAN_FILTERING_PATCH
4499 * We need to disable the optimizer because the optimizer is buggy
4500 * and wipes out some LD instructions generated by the below
4501 * code to validate the Frame Control bits
4503 cstate
->no_optimize
= 1;
4504 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4511 * For control frames, there is no SA.
4513 * For management frames, SA is at an
4514 * offset of 10 from the beginning of
4517 * For data frames, SA is at an offset
4518 * of 10 from the beginning of the packet
4519 * if From DS is clear, at an offset of
4520 * 16 from the beginning of the packet
4521 * if From DS is set and To DS is clear,
4522 * and an offset of 24 from the beginning
4523 * of the packet if From DS is set and To DS
4528 * Generate the tests to be done for data frames
4531 * First, check for To DS set, i.e. check "link[1] & 0x01".
4533 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4534 b1
= new_block(cstate
, JMP(BPF_JSET
));
4535 b1
->s
.k
= 0x01; /* To DS */
4539 * If To DS is set, the SA is at 24.
4541 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4545 * Now, check for To DS not set, i.e. check
4546 * "!(link[1] & 0x01)".
4548 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4549 b2
= new_block(cstate
, JMP(BPF_JSET
));
4550 b2
->s
.k
= 0x01; /* To DS */
4555 * If To DS is not set, the SA is at 16.
4557 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4561 * Now OR together the last two checks. That gives
4562 * the complete set of checks for data frames with
4568 * Now check for From DS being set, and AND that with
4569 * the ORed-together checks.
4571 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4572 b1
= new_block(cstate
, JMP(BPF_JSET
));
4573 b1
->s
.k
= 0x02; /* From DS */
4578 * Now check for data frames with From DS not set.
4580 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4581 b2
= new_block(cstate
, JMP(BPF_JSET
));
4582 b2
->s
.k
= 0x02; /* From DS */
4587 * If From DS isn't set, the SA is at 10.
4589 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4593 * Now OR together the checks for data frames with
4594 * From DS not set and for data frames with From DS
4595 * set; that gives the checks done for data frames.
4600 * Now check for a data frame.
4601 * I.e, check "link[0] & 0x08".
4603 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4604 b1
= new_block(cstate
, JMP(BPF_JSET
));
4609 * AND that with the checks done for data frames.
4614 * If the high-order bit of the type value is 0, this
4615 * is a management frame.
4616 * I.e, check "!(link[0] & 0x08)".
4618 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4619 b2
= new_block(cstate
, JMP(BPF_JSET
));
4625 * For management frames, the SA is at 10.
4627 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4631 * OR that with the checks done for data frames.
4632 * That gives the checks done for management and
4638 * If the low-order bit of the type value is 1,
4639 * this is either a control frame or a frame
4640 * with a reserved type, and thus not a
4643 * I.e., check "!(link[0] & 0x04)".
4645 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4646 b1
= new_block(cstate
, JMP(BPF_JSET
));
4652 * AND that with the checks for data and management
4662 * For control frames, there is no DA.
4664 * For management frames, DA is at an
4665 * offset of 4 from the beginning of
4668 * For data frames, DA is at an offset
4669 * of 4 from the beginning of the packet
4670 * if To DS is clear and at an offset of
4671 * 16 from the beginning of the packet
4676 * Generate the tests to be done for data frames.
4678 * First, check for To DS set, i.e. "link[1] & 0x01".
4680 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4681 b1
= new_block(cstate
, JMP(BPF_JSET
));
4682 b1
->s
.k
= 0x01; /* To DS */
4686 * If To DS is set, the DA is at 16.
4688 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4692 * Now, check for To DS not set, i.e. check
4693 * "!(link[1] & 0x01)".
4695 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4696 b2
= new_block(cstate
, JMP(BPF_JSET
));
4697 b2
->s
.k
= 0x01; /* To DS */
4702 * If To DS is not set, the DA is at 4.
4704 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4708 * Now OR together the last two checks. That gives
4709 * the complete set of checks for data frames.
4714 * Now check for a data frame.
4715 * I.e, check "link[0] & 0x08".
4717 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4718 b1
= new_block(cstate
, JMP(BPF_JSET
));
4723 * AND that with the checks done for data frames.
4728 * If the high-order bit of the type value is 0, this
4729 * is a management frame.
4730 * I.e, check "!(link[0] & 0x08)".
4732 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4733 b2
= new_block(cstate
, JMP(BPF_JSET
));
4739 * For management frames, the DA is at 4.
4741 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4745 * OR that with the checks done for data frames.
4746 * That gives the checks done for management and
4752 * If the low-order bit of the type value is 1,
4753 * this is either a control frame or a frame
4754 * with a reserved type, and thus not a
4757 * I.e., check "!(link[0] & 0x04)".
4759 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4760 b1
= new_block(cstate
, JMP(BPF_JSET
));
4766 * AND that with the checks for data and management
4773 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4774 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4780 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4781 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4786 * XXX - add BSSID keyword?
4789 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4793 * Not present in CTS or ACK control frames.
4795 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4796 IEEE80211_FC0_TYPE_MASK
);
4798 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4799 IEEE80211_FC0_SUBTYPE_MASK
);
4801 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4802 IEEE80211_FC0_SUBTYPE_MASK
);
4806 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4812 * Not present in control frames.
4814 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4815 IEEE80211_FC0_TYPE_MASK
);
4817 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4823 * Present only if the direction mask has both "From DS"
4824 * and "To DS" set. Neither control frames nor management
4825 * frames should have both of those set, so we don't
4826 * check the frame type.
4828 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4829 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4830 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4836 * Not present in management frames; addr1 in other
4841 * If the high-order bit of the type value is 0, this
4842 * is a management frame.
4843 * I.e, check "(link[0] & 0x08)".
4845 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4846 b1
= new_block(cstate
, JMP(BPF_JSET
));
4853 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4856 * AND that with the check of addr1.
4863 * Not present in management frames; addr2, if present,
4868 * Not present in CTS or ACK control frames.
4870 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4871 IEEE80211_FC0_TYPE_MASK
);
4873 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4874 IEEE80211_FC0_SUBTYPE_MASK
);
4876 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4877 IEEE80211_FC0_SUBTYPE_MASK
);
4883 * If the high-order bit of the type value is 0, this
4884 * is a management frame.
4885 * I.e, check "(link[0] & 0x08)".
4887 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4888 b1
= new_block(cstate
, JMP(BPF_JSET
));
4893 * AND that with the check for frames other than
4894 * CTS and ACK frames.
4901 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4910 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4911 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4912 * as the RFC states.)
4914 static struct block
*
4915 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4917 register struct block
*b0
, *b1
;
4921 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4924 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4927 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4928 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4934 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4935 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4940 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4944 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4948 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4952 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4956 bpf_error(cstate
, "'ra' is only supported on 802.11");
4960 bpf_error(cstate
, "'ta' is only supported on 802.11");
4968 * This is quite tricky because there may be pad bytes in front of the
4969 * DECNET header, and then there are two possible data packet formats that
4970 * carry both src and dst addresses, plus 5 packet types in a format that
4971 * carries only the src node, plus 2 types that use a different format and
4972 * also carry just the src node.
4976 * Instead of doing those all right, we just look for data packets with
4977 * 0 or 1 bytes of padding. If you want to look at other packets, that
4978 * will require a lot more hacking.
4980 * To add support for filtering on DECNET "areas" (network numbers)
4981 * one would want to add a "mask" argument to this routine. That would
4982 * make the filter even more inefficient, although one could be clever
4983 * and not generate masking instructions if the mask is 0xFFFF.
4985 static struct block
*
4986 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4988 struct block
*b0
, *b1
, *b2
, *tmp
;
4989 u_int offset_lh
; /* offset if long header is received */
4990 u_int offset_sh
; /* offset if short header is received */
4995 offset_sh
= 1; /* follows flags */
4996 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5000 offset_sh
= 3; /* follows flags, dstnode */
5001 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5005 /* Inefficient because we do our Calvinball dance twice */
5006 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5007 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5013 /* Inefficient because we do our Calvinball dance twice */
5014 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5015 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5020 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5024 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5028 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5032 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5036 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5040 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5048 * In a DECnet message inside an Ethernet frame the first two bytes
5049 * immediately after EtherType are the [litle-endian] DECnet message
5050 * length, which is irrelevant in this context.
5052 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5053 * 8-bit bitmap of the optional padding before the packet route header.
5054 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5055 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5056 * means there aren't any PAD bytes after the bitmap, so the header
5057 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5058 * is set to 0, thus the header begins at the third byte.
5060 * The header can be in several (as mentioned above) formats, all of
5061 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5062 * (PF, "pad field") set to 0 regardless of any padding present before
5063 * the header. "Short header" means bits 0-2 of the bitmap encode the
5064 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5066 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5067 * because the wire encoding is little-endian and this function always
5068 * receives a big-endian address value.
5070 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5071 /* Check for pad = 1, long header case */
5072 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5073 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5074 BPF_H
, SWAPSHORT(addr
));
5076 /* Check for pad = 0, long header case */
5077 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5078 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5082 /* Check for pad = 1, short header case */
5083 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5084 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5088 /* Check for pad = 0, short header case */
5089 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5090 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5095 /* Combine with test for cstate->linktype */
5101 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5102 * test the bottom-of-stack bit, and then check the version number
5103 * field in the IP header.
5105 static struct block
*
5106 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5108 struct block
*b0
, *b1
;
5113 /* match the bottom-of-stack bit */
5114 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5115 /* match the IPv4 version number */
5116 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5120 case ETHERTYPE_IPV6
:
5121 /* match the bottom-of-stack bit */
5122 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5123 /* match the IPv4 version number */
5124 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5129 /* FIXME add other L3 proto IDs */
5130 bpf_error(cstate
, "unsupported protocol over mpls");
5135 static struct block
*
5136 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5137 int proto
, int dir
, int type
)
5139 struct block
*b0
, *b1
;
5140 const char *typestr
;
5150 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5152 * Only check for non-IPv4 addresses if we're not
5153 * checking MPLS-encapsulated packets.
5155 if (cstate
->label_stack_depth
== 0) {
5156 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5158 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5164 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5167 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
5170 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
5173 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
5176 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5179 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5182 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5185 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5188 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5191 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5194 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5197 return gen_dnhostop(cstate
, addr
, dir
);
5200 bpf_error(cstate
, "LAT host filtering not implemented");
5203 bpf_error(cstate
, "SCA host filtering not implemented");
5206 bpf_error(cstate
, "MOPRC host filtering not implemented");
5209 bpf_error(cstate
, "MOPDL host filtering not implemented");
5212 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5215 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5218 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5221 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5224 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5227 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5230 bpf_error(cstate
, "AARP host filtering not implemented");
5233 bpf_error(cstate
, "ISO host filtering not implemented");
5236 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5239 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5242 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5245 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5248 bpf_error(cstate
, "IPX host filtering not implemented");
5251 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5254 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5257 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5260 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5263 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5266 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5269 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5272 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5275 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5278 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5287 static struct block
*
5288 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5289 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5291 const char *typestr
;
5301 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5304 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5307 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5310 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5313 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5316 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5319 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5322 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5325 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5328 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5331 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5334 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5337 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5340 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5343 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5346 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5349 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5352 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5355 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5358 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5361 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5364 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5367 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5370 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5373 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5376 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5379 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5382 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5385 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5388 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5391 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5394 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5397 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5400 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5403 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5406 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5409 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5412 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5415 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5418 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5428 static struct block
*
5429 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5430 struct addrinfo
*alist
, int proto
, int dir
)
5432 struct block
*b0
, *b1
, *tmp
;
5433 struct addrinfo
*ai
;
5434 struct sockaddr_in
*sin
;
5437 bpf_error(cstate
, "direction applied to 'gateway'");
5444 switch (cstate
->linktype
) {
5446 case DLT_NETANALYZER
:
5447 case DLT_NETANALYZER_TRANSPARENT
:
5448 b1
= gen_prevlinkhdr_check(cstate
);
5449 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5454 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5457 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5459 case DLT_IEEE802_11
:
5460 case DLT_PRISM_HEADER
:
5461 case DLT_IEEE802_11_RADIO_AVS
:
5462 case DLT_IEEE802_11_RADIO
:
5464 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5468 * This is LLC-multiplexed traffic; if it were
5469 * LANE, cstate->linktype would have been set to
5473 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5474 case DLT_IP_OVER_FC
:
5475 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5479 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5482 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5484 * Does it have an address?
5486 if (ai
->ai_addr
!= NULL
) {
5488 * Yes. Is it an IPv4 address?
5490 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5492 * Generate an entry for it.
5494 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5495 tmp
= gen_host(cstate
,
5496 ntohl(sin
->sin_addr
.s_addr
),
5497 0xffffffff, proto
, Q_OR
, Q_HOST
);
5499 * Is it the *first* IPv4 address?
5503 * Yes, so start with it.
5508 * No, so OR it into the
5520 * No IPv4 addresses found.
5528 bpf_error(cstate
, "illegal modifier of 'gateway'");
5533 static struct block
*
5534 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5542 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5546 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5550 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5554 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5557 #ifndef IPPROTO_IGMP
5558 #define IPPROTO_IGMP 2
5562 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5565 #ifndef IPPROTO_IGRP
5566 #define IPPROTO_IGRP 9
5569 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5573 #define IPPROTO_PIM 103
5577 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5580 #ifndef IPPROTO_VRRP
5581 #define IPPROTO_VRRP 112
5585 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5588 #ifndef IPPROTO_CARP
5589 #define IPPROTO_CARP 112
5593 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5597 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5601 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5605 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5609 bpf_error(cstate
, "link layer applied in wrong context");
5612 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5616 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5620 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5624 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5628 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5632 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5636 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5640 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5643 #ifndef IPPROTO_ICMPV6
5644 #define IPPROTO_ICMPV6 58
5647 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5651 #define IPPROTO_AH 51
5654 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5658 #define IPPROTO_ESP 50
5661 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5665 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5669 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5673 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5676 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5677 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5678 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5680 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5682 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5684 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5688 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5689 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5690 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5692 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5694 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5696 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5700 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5701 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5702 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5704 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5709 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5710 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5715 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5716 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5718 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5720 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5725 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5726 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5731 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5732 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5737 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5741 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5745 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5749 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5753 bpf_error(cstate
, "'radio' is not a valid protocol type");
5762 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5765 * Catch errors reported by us and routines below us, and return NULL
5768 if (setjmp(cstate
->top_ctx
))
5771 return gen_proto_abbrev_internal(cstate
, proto
);
5774 static struct block
*
5775 gen_ipfrag(compiler_state_t
*cstate
)
5780 /* not IPv4 frag other than the first frag */
5781 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5782 b
= new_block(cstate
, JMP(BPF_JSET
));
5791 * Generate a comparison to a port value in the transport-layer header
5792 * at the specified offset from the beginning of that header.
5794 * XXX - this handles a variable-length prefix preceding the link-layer
5795 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5796 * variable-length link-layer headers (such as Token Ring or 802.11
5799 static struct block
*
5800 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5802 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5805 static struct block
*
5806 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5808 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5811 static struct block
*
5812 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5814 struct block
*b0
, *b1
, *tmp
;
5816 /* ip proto 'proto' and not a fragment other than the first fragment */
5817 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5818 b0
= gen_ipfrag(cstate
);
5823 b1
= gen_portatom(cstate
, 0, port
);
5827 b1
= gen_portatom(cstate
, 2, port
);
5831 tmp
= gen_portatom(cstate
, 0, port
);
5832 b1
= gen_portatom(cstate
, 2, port
);
5838 tmp
= gen_portatom(cstate
, 0, port
);
5839 b1
= gen_portatom(cstate
, 2, port
);
5844 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5848 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5852 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5856 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5860 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5864 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5876 static struct block
*
5877 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5879 struct block
*b0
, *b1
, *tmp
;
5884 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5885 * not LLC encapsulation with LLCSAP_IP.
5887 * For IEEE 802 networks - which includes 802.5 token ring
5888 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5889 * says that SNAP encapsulation is used, not LLC encapsulation
5892 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5893 * RFC 2225 say that SNAP encapsulation is used, not LLC
5894 * encapsulation with LLCSAP_IP.
5896 * So we always check for ETHERTYPE_IP.
5898 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5904 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5908 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5909 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5911 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5923 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5925 struct block
*b0
, *b1
, *tmp
;
5927 /* ip6 proto 'proto' */
5928 /* XXX - catch the first fragment of a fragmented packet? */
5929 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5933 b1
= gen_portatom6(cstate
, 0, port
);
5937 b1
= gen_portatom6(cstate
, 2, port
);
5941 tmp
= gen_portatom6(cstate
, 0, port
);
5942 b1
= gen_portatom6(cstate
, 2, port
);
5948 tmp
= gen_portatom6(cstate
, 0, port
);
5949 b1
= gen_portatom6(cstate
, 2, port
);
5961 static struct block
*
5962 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5964 struct block
*b0
, *b1
, *tmp
;
5966 /* link proto ip6 */
5967 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5973 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5977 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5978 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5980 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5991 /* gen_portrange code */
5992 static struct block
*
5993 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5996 struct block
*b1
, *b2
;
6000 * Reverse the order of the ports, so v1 is the lower one.
6009 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
6010 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
6017 static struct block
*
6018 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6019 bpf_u_int32 proto
, int dir
)
6021 struct block
*b0
, *b1
, *tmp
;
6023 /* ip proto 'proto' and not a fragment other than the first fragment */
6024 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
6025 b0
= gen_ipfrag(cstate
);
6030 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6034 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6038 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6039 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6045 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6046 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6051 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6055 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6059 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6063 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6067 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
6071 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
6083 static struct block
*
6084 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6087 struct block
*b0
, *b1
, *tmp
;
6090 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6096 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6101 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6102 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6104 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6115 static struct block
*
6116 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6119 struct block
*b1
, *b2
;
6123 * Reverse the order of the ports, so v1 is the lower one.
6132 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6133 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6140 static struct block
*
6141 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6142 bpf_u_int32 proto
, int dir
)
6144 struct block
*b0
, *b1
, *tmp
;
6146 /* ip6 proto 'proto' */
6147 /* XXX - catch the first fragment of a fragmented packet? */
6148 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6152 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6156 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6160 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6161 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6167 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6168 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6180 static struct block
*
6181 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6184 struct block
*b0
, *b1
, *tmp
;
6186 /* link proto ip6 */
6187 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6193 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6198 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6199 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6201 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6213 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6222 v
= pcap_nametoproto(name
);
6223 if (v
== PROTO_UNDEF
)
6224 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6228 /* XXX should look up h/w protocol type based on cstate->linktype */
6229 v
= pcap_nametoeproto(name
);
6230 if (v
== PROTO_UNDEF
) {
6231 v
= pcap_nametollc(name
);
6232 if (v
== PROTO_UNDEF
)
6233 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6238 if (strcmp(name
, "esis") == 0)
6240 else if (strcmp(name
, "isis") == 0)
6242 else if (strcmp(name
, "clnp") == 0)
6245 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6255 #if !defined(NO_PROTOCHAIN)
6256 static struct block
*
6257 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6259 struct block
*b0
, *b
;
6260 struct slist
*s
[100];
6261 int fix2
, fix3
, fix4
, fix5
;
6262 int ahcheck
, again
, end
;
6264 int reg2
= alloc_reg(cstate
);
6266 memset(s
, 0, sizeof(s
));
6267 fix3
= fix4
= fix5
= 0;
6274 b0
= gen_protochain(cstate
, v
, Q_IP
);
6275 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6279 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6284 * We don't handle variable-length prefixes before the link-layer
6285 * header, or variable-length link-layer headers, here yet.
6286 * We might want to add BPF instructions to do the protochain
6287 * work, to simplify that and, on platforms that have a BPF
6288 * interpreter with the new instructions, let the filtering
6289 * be done in the kernel. (We already require a modified BPF
6290 * engine to do the protochain stuff, to support backward
6291 * branches, and backward branch support is unlikely to appear
6292 * in kernel BPF engines.)
6294 if (cstate
->off_linkpl
.is_variable
)
6295 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6298 * To quote a comment in optimize.c:
6300 * "These data structures are used in a Cocke and Schwartz style
6301 * value numbering scheme. Since the flowgraph is acyclic,
6302 * exit values can be propagated from a node's predecessors
6303 * provided it is uniquely defined."
6305 * "Acyclic" means "no backward branches", which means "no
6306 * loops", so we have to turn the optimizer off.
6308 cstate
->no_optimize
= 1;
6311 * s[0] is a dummy entry to protect other BPF insn from damage
6312 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6313 * hard to find interdependency made by jump table fixup.
6316 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6321 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
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
+ 9;
6327 /* X = ip->ip_hl << 2 */
6328 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6329 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6334 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6336 /* A = ip6->ip_nxt */
6337 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6338 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6340 /* X = sizeof(struct ip6_hdr) */
6341 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6347 bpf_error(cstate
, "unsupported proto to gen_protochain");
6351 /* again: if (A == v) goto end; else fall through; */
6353 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6355 s
[i
]->s
.jt
= NULL
; /*later*/
6356 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6360 #ifndef IPPROTO_NONE
6361 #define IPPROTO_NONE 59
6363 /* if (A == IPPROTO_NONE) goto end */
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_NONE
;
6368 s
[fix5
]->s
.jf
= s
[i
];
6372 if (proto
== Q_IPV6
) {
6373 int v6start
, v6end
, v6advance
, j
;
6376 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6377 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_HOPOPTS
;
6381 s
[fix2
]->s
.jf
= s
[i
];
6383 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6384 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6385 s
[i
]->s
.jt
= NULL
; /*later*/
6386 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6387 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6389 /* if (A == IPPROTO_ROUTING) goto v6advance */
6390 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6391 s
[i
]->s
.jt
= NULL
; /*later*/
6392 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6393 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6395 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6396 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6397 s
[i
]->s
.jt
= NULL
; /*later*/
6398 s
[i
]->s
.jf
= NULL
; /*later*/
6399 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6409 * A = P[X + packet head];
6410 * X = X + (P[X + packet head + 1] + 1) * 8;
6412 /* A = P[X + packet head] */
6413 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6414 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6417 s
[i
] = new_stmt(cstate
, BPF_ST
);
6420 /* A = P[X + packet head + 1]; */
6421 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6422 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6425 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6429 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6433 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6437 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6440 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6444 /* goto again; (must use BPF_JA for backward jump) */
6445 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6446 s
[i
]->s
.k
= again
- i
- 1;
6447 s
[i
- 1]->s
.jf
= s
[i
];
6451 for (j
= v6start
; j
<= v6end
; j
++)
6452 s
[j
]->s
.jt
= s
[v6advance
];
6455 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6457 s
[fix2
]->s
.jf
= s
[i
];
6463 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6464 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6465 s
[i
]->s
.jt
= NULL
; /*later*/
6466 s
[i
]->s
.jf
= NULL
; /*later*/
6467 s
[i
]->s
.k
= IPPROTO_AH
;
6469 s
[fix3
]->s
.jf
= s
[ahcheck
];
6476 * X = X + (P[X + 1] + 2) * 4;
6479 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6481 /* A = P[X + packet head]; */
6482 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6483 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6486 s
[i
] = new_stmt(cstate
, BPF_ST
);
6490 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6493 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6497 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6499 /* A = P[X + packet head] */
6500 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6501 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6504 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6508 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6512 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6515 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6519 /* goto again; (must use BPF_JA for backward jump) */
6520 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6521 s
[i
]->s
.k
= again
- i
- 1;
6526 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6528 s
[fix2
]->s
.jt
= s
[end
];
6529 s
[fix4
]->s
.jf
= s
[end
];
6530 s
[fix5
]->s
.jt
= s
[end
];
6537 for (i
= 0; i
< max
- 1; i
++)
6538 s
[i
]->next
= s
[i
+ 1];
6539 s
[max
- 1]->next
= NULL
;
6544 b
= new_block(cstate
, JMP(BPF_JEQ
));
6545 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6548 free_reg(cstate
, reg2
);
6553 #endif /* !defined(NO_PROTOCHAIN) */
6555 static struct block
*
6556 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6559 struct block
*b0
, *b1
;
6562 * A data frame has the 0x08 bit (b3) in the frame control field set
6563 * and the 0x04 bit (b2) clear.
6565 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6566 b0
= new_block(cstate
, JMP(BPF_JSET
));
6570 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6571 b1
= new_block(cstate
, JMP(BPF_JSET
));
6582 * Generate code that checks whether the packet is a packet for protocol
6583 * <proto> and whether the type field in that protocol's header has
6584 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6585 * IP packet and checks the protocol number in the IP header against <v>.
6587 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6588 * against Q_IP and Q_IPV6.
6590 static struct block
*
6591 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6593 struct block
*b0
, *b1
;
6596 if (dir
!= Q_DEFAULT
)
6597 bpf_error(cstate
, "direction applied to 'proto'");
6601 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6602 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6607 return gen_linktype(cstate
, v
);
6611 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6612 * not LLC encapsulation with LLCSAP_IP.
6614 * For IEEE 802 networks - which includes 802.5 token ring
6615 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6616 * says that SNAP encapsulation is used, not LLC encapsulation
6619 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6620 * RFC 2225 say that SNAP encapsulation is used, not LLC
6621 * encapsulation with LLCSAP_IP.
6623 * So we always check for ETHERTYPE_IP.
6625 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6626 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6631 bpf_error(cstate
, "arp does not encapsulate another protocol");
6635 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6639 bpf_error(cstate
, "'sctp proto' is bogus");
6643 bpf_error(cstate
, "'tcp proto' is bogus");
6647 bpf_error(cstate
, "'udp proto' is bogus");
6651 bpf_error(cstate
, "'icmp proto' is bogus");
6655 bpf_error(cstate
, "'igmp proto' is bogus");
6659 bpf_error(cstate
, "'igrp proto' is bogus");
6663 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6667 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6671 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6675 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6679 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6683 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6687 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6689 * Also check for a fragment header before the final
6692 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6693 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6695 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6701 bpf_error(cstate
, "'icmp6 proto' is bogus");
6705 bpf_error(cstate
, "'ah proto' is bogus");
6709 bpf_error(cstate
, "'esp proto' is bogus");
6713 bpf_error(cstate
, "'pim proto' is bogus");
6717 bpf_error(cstate
, "'vrrp proto' is bogus");
6721 bpf_error(cstate
, "'aarp proto' is bogus");
6725 switch (cstate
->linktype
) {
6729 * Frame Relay packets typically have an OSI
6730 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6731 * generates code to check for all the OSI
6732 * NLPIDs, so calling it and then adding a check
6733 * for the particular NLPID for which we're
6734 * looking is bogus, as we can just check for
6737 * What we check for is the NLPID and a frame
6738 * control field value of UI, i.e. 0x03 followed
6741 * XXX - assumes a 2-byte Frame Relay header with
6742 * DLCI and flags. What if the address is longer?
6744 * XXX - what about SNAP-encapsulated frames?
6746 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6752 * Cisco uses an Ethertype lookalike - for OSI,
6755 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6756 /* OSI in C-HDLC is stuffed with a fudge byte */
6757 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6762 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6763 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6769 bpf_error(cstate
, "'esis proto' is bogus");
6773 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6775 * 4 is the offset of the PDU type relative to the IS-IS
6778 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6783 bpf_error(cstate
, "'clnp proto' is not supported");
6787 bpf_error(cstate
, "'stp proto' is bogus");
6791 bpf_error(cstate
, "'ipx proto' is bogus");
6795 bpf_error(cstate
, "'netbeui proto' is bogus");
6799 bpf_error(cstate
, "'l1 proto' is bogus");
6803 bpf_error(cstate
, "'l2 proto' is bogus");
6807 bpf_error(cstate
, "'iih proto' is bogus");
6811 bpf_error(cstate
, "'snp proto' is bogus");
6815 bpf_error(cstate
, "'csnp proto' is bogus");
6819 bpf_error(cstate
, "'psnp proto' is bogus");
6823 bpf_error(cstate
, "'lsp proto' is bogus");
6827 bpf_error(cstate
, "'radio proto' is bogus");
6831 bpf_error(cstate
, "'carp proto' is bogus");
6842 * Convert a non-numeric name to a port number.
6845 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6847 struct addrinfo hints
, *res
, *ai
;
6849 struct sockaddr_in
*in4
;
6851 struct sockaddr_in6
*in6
;
6856 * We check for both TCP and UDP in case there are
6857 * ambiguous entries.
6859 memset(&hints
, 0, sizeof(hints
));
6860 hints
.ai_family
= PF_UNSPEC
;
6861 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6862 hints
.ai_protocol
= ipproto
;
6863 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6870 * No such port. Just return -1.
6877 * We don't use strerror() because it's not
6878 * guaranteed to be thread-safe on all platforms
6879 * (probably because it might use a non-thread-local
6880 * buffer into which to format an error message
6881 * if the error code isn't one for which it has
6882 * a canned string; three cheers for C string
6885 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6887 port
= -2; /* a real error */
6893 * This is a real error, not just "there's
6894 * no such service name".
6896 * We don't use gai_strerror() because it's not
6897 * guaranteed to be thread-safe on all platforms
6898 * (probably because it might use a non-thread-local
6899 * buffer into which to format an error message
6900 * if the error code isn't one for which it has
6901 * a canned string; three cheers for C string
6904 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6906 port
= -2; /* a real error */
6911 * OK, we found it. Did it find anything?
6913 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6915 * Does it have an address?
6917 if (ai
->ai_addr
!= NULL
) {
6919 * Yes. Get a port number; we're done.
6921 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6922 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6923 port
= ntohs(in4
->sin_port
);
6927 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6928 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6929 port
= ntohs(in6
->sin6_port
);
6941 * Convert a string to a port number.
6944 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6954 * See if it's a number.
6956 ret
= stoulen(string
, string_size
, &val
, cstate
);
6960 /* Unknown port type - it's just a number. */
6961 *proto
= PROTO_UNDEF
;
6964 case STOULEN_NOT_OCTAL_NUMBER
:
6965 case STOULEN_NOT_HEX_NUMBER
:
6966 case STOULEN_NOT_DECIMAL_NUMBER
:
6968 * Not a valid number; try looking it up as a port.
6970 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6971 memcpy(cpy
, string
, string_size
);
6972 cpy
[string_size
] = '\0';
6973 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6974 if (tcp_port
== -2) {
6976 * We got a hard error; the error string has
6980 longjmp(cstate
->top_ctx
, 1);
6983 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6984 if (udp_port
== -2) {
6986 * We got a hard error; the error string has
6990 longjmp(cstate
->top_ctx
, 1);
6995 * We need to check /etc/services for ambiguous entries.
6996 * If we find an ambiguous entry, and it has the
6997 * same port number, change the proto to PROTO_UNDEF
6998 * so both TCP and UDP will be checked.
7000 if (tcp_port
>= 0) {
7001 val
= (bpf_u_int32
)tcp_port
;
7002 *proto
= IPPROTO_TCP
;
7003 if (udp_port
>= 0) {
7004 if (udp_port
== tcp_port
)
7005 *proto
= PROTO_UNDEF
;
7008 /* Can't handle ambiguous names that refer
7009 to different port numbers. */
7010 warning("ambiguous port %s in /etc/services",
7017 if (udp_port
>= 0) {
7018 val
= (bpf_u_int32
)udp_port
;
7019 *proto
= IPPROTO_UDP
;
7023 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
7025 longjmp(cstate
->top_ctx
, 1);
7032 /* Error already set. */
7033 longjmp(cstate
->top_ctx
, 1);
7040 /* Should not happen */
7041 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
7042 longjmp(cstate
->top_ctx
, 1);
7049 * Convert a string in the form PPP-PPP, which correspond to ports, to
7050 * a starting and ending port in a port range.
7053 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
7054 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
7057 const char *first
, *second
;
7058 size_t first_size
, second_size
;
7061 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
7062 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
7065 * Make sure there are no other hyphens.
7067 * XXX - we support named ports, but there are some port names
7068 * in /etc/services that include hyphens, so this would rule
7071 if (strchr(hyphen_off
+ 1, '-') != NULL
)
7072 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
7076 * Get the length of the first port.
7079 first_size
= hyphen_off
- string
;
7080 if (first_size
== 0) {
7081 /* Range of "-port", which we don't support. */
7082 bpf_error(cstate
, "port range '%s' has no starting port", string
);
7086 * Try to convert it to a port.
7088 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
7089 save_proto
= *proto
;
7092 * Get the length of the second port.
7094 second
= hyphen_off
+ 1;
7095 second_size
= strlen(second
);
7096 if (second_size
== 0) {
7097 /* Range of "port-", which we don't support. */
7098 bpf_error(cstate
, "port range '%s' has no ending port", string
);
7102 * Try to convert it to a port.
7104 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
7105 if (*proto
!= save_proto
)
7106 *proto
= PROTO_UNDEF
;
7110 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
7112 int proto
= q
.proto
;
7116 bpf_u_int32 mask
, addr
;
7117 struct addrinfo
*res
, *res0
;
7118 struct sockaddr_in
*sin4
;
7121 struct sockaddr_in6
*sin6
;
7122 struct in6_addr mask128
;
7124 struct block
*b
, *tmp
;
7125 int port
, real_proto
;
7126 bpf_u_int32 port1
, port2
;
7129 * Catch errors reported by us and routines below us, and return NULL
7132 if (setjmp(cstate
->top_ctx
))
7138 addr
= pcap_nametonetaddr(name
);
7140 bpf_error(cstate
, "unknown network '%s'", name
);
7141 /* Left justify network addr and calculate its network mask */
7143 while (addr
&& (addr
& 0xff000000) == 0) {
7147 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7151 if (proto
== Q_LINK
) {
7152 switch (cstate
->linktype
) {
7155 case DLT_NETANALYZER
:
7156 case DLT_NETANALYZER_TRANSPARENT
:
7157 eaddr
= pcap_ether_hostton(name
);
7160 "unknown ether host '%s'", name
);
7161 tmp
= gen_prevlinkhdr_check(cstate
);
7162 b
= gen_ehostop(cstate
, eaddr
, dir
);
7169 eaddr
= pcap_ether_hostton(name
);
7172 "unknown FDDI host '%s'", name
);
7173 b
= gen_fhostop(cstate
, eaddr
, dir
);
7178 eaddr
= pcap_ether_hostton(name
);
7181 "unknown token ring host '%s'", name
);
7182 b
= gen_thostop(cstate
, eaddr
, dir
);
7186 case DLT_IEEE802_11
:
7187 case DLT_PRISM_HEADER
:
7188 case DLT_IEEE802_11_RADIO_AVS
:
7189 case DLT_IEEE802_11_RADIO
:
7191 eaddr
= pcap_ether_hostton(name
);
7194 "unknown 802.11 host '%s'", name
);
7195 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7199 case DLT_IP_OVER_FC
:
7200 eaddr
= pcap_ether_hostton(name
);
7203 "unknown Fibre Channel host '%s'", name
);
7204 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7209 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7210 } else if (proto
== Q_DECNET
) {
7212 * A long time ago on Ultrix libpcap supported
7213 * translation of DECnet host names into DECnet
7214 * addresses, but this feature is history now.
7216 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7219 memset(&mask128
, 0xff, sizeof(mask128
));
7221 res0
= res
= pcap_nametoaddrinfo(name
);
7223 bpf_error(cstate
, "unknown host '%s'", name
);
7230 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7231 tproto
== Q_DEFAULT
) {
7237 for (res
= res0
; res
; res
= res
->ai_next
) {
7238 switch (res
->ai_family
) {
7241 if (tproto
== Q_IPV6
)
7245 sin4
= (struct sockaddr_in
*)
7247 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7248 0xffffffff, tproto
, dir
, q
.addr
);
7252 if (tproto6
== Q_IP
)
7255 sin6
= (struct sockaddr_in6
*)
7257 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7258 &mask128
, tproto6
, dir
, q
.addr
);
7271 bpf_error(cstate
, "unknown host '%s'%s", name
,
7272 (proto
== Q_DEFAULT
)
7274 : " for specified address family");
7280 if (proto
!= Q_DEFAULT
&&
7281 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7282 bpf_error(cstate
, "illegal qualifier of 'port'");
7283 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7284 bpf_error(cstate
, "unknown port '%s'", name
);
7285 if (proto
== Q_UDP
) {
7286 if (real_proto
== IPPROTO_TCP
)
7287 bpf_error(cstate
, "port '%s' is tcp", name
);
7288 else if (real_proto
== IPPROTO_SCTP
)
7289 bpf_error(cstate
, "port '%s' is sctp", name
);
7291 /* override PROTO_UNDEF */
7292 real_proto
= IPPROTO_UDP
;
7294 if (proto
== Q_TCP
) {
7295 if (real_proto
== IPPROTO_UDP
)
7296 bpf_error(cstate
, "port '%s' is udp", name
);
7298 else if (real_proto
== IPPROTO_SCTP
)
7299 bpf_error(cstate
, "port '%s' is sctp", name
);
7301 /* override PROTO_UNDEF */
7302 real_proto
= IPPROTO_TCP
;
7304 if (proto
== Q_SCTP
) {
7305 if (real_proto
== IPPROTO_UDP
)
7306 bpf_error(cstate
, "port '%s' is udp", name
);
7308 else if (real_proto
== IPPROTO_TCP
)
7309 bpf_error(cstate
, "port '%s' is tcp", name
);
7311 /* override PROTO_UNDEF */
7312 real_proto
= IPPROTO_SCTP
;
7315 bpf_error(cstate
, "illegal port number %d < 0", port
);
7317 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7318 b
= gen_port(cstate
, port
, real_proto
, dir
);
7319 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7323 if (proto
!= Q_DEFAULT
&&
7324 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7325 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7326 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7327 if (proto
== Q_UDP
) {
7328 if (real_proto
== IPPROTO_TCP
)
7329 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7330 else if (real_proto
== IPPROTO_SCTP
)
7331 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7333 /* override PROTO_UNDEF */
7334 real_proto
= IPPROTO_UDP
;
7336 if (proto
== Q_TCP
) {
7337 if (real_proto
== IPPROTO_UDP
)
7338 bpf_error(cstate
, "port in range '%s' is udp", name
);
7339 else if (real_proto
== IPPROTO_SCTP
)
7340 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7342 /* override PROTO_UNDEF */
7343 real_proto
= IPPROTO_TCP
;
7345 if (proto
== Q_SCTP
) {
7346 if (real_proto
== IPPROTO_UDP
)
7347 bpf_error(cstate
, "port in range '%s' is udp", name
);
7348 else if (real_proto
== IPPROTO_TCP
)
7349 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7351 /* override PROTO_UNDEF */
7352 real_proto
= IPPROTO_SCTP
;
7355 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7357 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7359 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7360 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7365 eaddr
= pcap_ether_hostton(name
);
7367 bpf_error(cstate
, "unknown ether host: %s", name
);
7369 res
= pcap_nametoaddrinfo(name
);
7372 bpf_error(cstate
, "unknown host '%s'", name
);
7373 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7377 bpf_error(cstate
, "unknown host '%s'", name
);
7380 bpf_error(cstate
, "'gateway' not supported in this configuration");
7384 real_proto
= lookup_proto(cstate
, name
, proto
);
7385 if (real_proto
>= 0)
7386 return gen_proto(cstate
, real_proto
, proto
, dir
);
7388 bpf_error(cstate
, "unknown protocol: %s", name
);
7390 #if !defined(NO_PROTOCHAIN)
7392 real_proto
= lookup_proto(cstate
, name
, proto
);
7393 if (real_proto
>= 0)
7394 return gen_protochain(cstate
, real_proto
, proto
);
7396 bpf_error(cstate
, "unknown protocol: %s", name
);
7397 #endif /* !defined(NO_PROTOCHAIN) */
7408 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7409 bpf_u_int32 masklen
, struct qual q
)
7411 register int nlen
, mlen
;
7416 * Catch errors reported by us and routines below us, and return NULL
7419 if (setjmp(cstate
->top_ctx
))
7422 nlen
= __pcap_atoin(s1
, &n
);
7424 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7425 /* Promote short ipaddr */
7429 mlen
= __pcap_atoin(s2
, &m
);
7431 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7432 /* Promote short ipaddr */
7435 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7438 /* Convert mask len to mask */
7440 bpf_error(cstate
, "mask length must be <= 32");
7441 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7442 m
= (bpf_u_int32
)m64
;
7444 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7451 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7454 bpf_error(cstate
, "Mask syntax for networks only");
7461 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7469 * Catch errors reported by us and routines below us, and return NULL
7472 if (setjmp(cstate
->top_ctx
))
7479 * v contains a 32-bit unsigned parsed from a string of the
7480 * form {N}, which could be decimal, hexadecimal or octal.
7481 * Although it would be possible to use the value as a raw
7482 * 16-bit DECnet address when the value fits into 16 bits, this
7483 * would be a questionable feature: DECnet address wire
7484 * encoding is little-endian, so this would not work as
7485 * intuitively as the same works for [big-endian] IPv4
7486 * addresses (0x01020304 means 1.2.3.4).
7488 if (proto
== Q_DECNET
)
7489 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7491 } else if (proto
== Q_DECNET
) {
7493 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7494 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7495 * for a valid DECnet address.
7497 vlen
= __pcap_atodn(s
, &v
);
7499 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7502 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7503 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7506 vlen
= __pcap_atoin(s
, &v
);
7508 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7516 if (proto
== Q_DECNET
)
7517 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7518 else if (proto
== Q_LINK
) {
7519 bpf_error(cstate
, "illegal link layer address");
7522 if (s
== NULL
&& q
.addr
== Q_NET
) {
7523 /* Promote short net number */
7524 while (v
&& (v
& 0xff000000) == 0) {
7529 /* Promote short ipaddr */
7531 mask
<<= 32 - vlen
;
7533 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7538 proto
= IPPROTO_UDP
;
7539 else if (proto
== Q_TCP
)
7540 proto
= IPPROTO_TCP
;
7541 else if (proto
== Q_SCTP
)
7542 proto
= IPPROTO_SCTP
;
7543 else if (proto
== Q_DEFAULT
)
7544 proto
= PROTO_UNDEF
;
7546 bpf_error(cstate
, "illegal qualifier of 'port'");
7549 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7553 b
= gen_port(cstate
, v
, proto
, dir
);
7554 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7560 proto
= IPPROTO_UDP
;
7561 else if (proto
== Q_TCP
)
7562 proto
= IPPROTO_TCP
;
7563 else if (proto
== Q_SCTP
)
7564 proto
= IPPROTO_SCTP
;
7565 else if (proto
== Q_DEFAULT
)
7566 proto
= PROTO_UNDEF
;
7568 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7571 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7575 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7576 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7581 bpf_error(cstate
, "'gateway' requires a name");
7585 return gen_proto(cstate
, v
, proto
, dir
);
7587 #if !defined(NO_PROTOCHAIN)
7589 return gen_protochain(cstate
, v
, proto
);
7605 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7608 struct addrinfo
*res
;
7609 struct in6_addr
*addr
;
7610 struct in6_addr mask
;
7612 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7615 * Catch errors reported by us and routines below us, and return NULL
7618 if (setjmp(cstate
->top_ctx
))
7621 res
= pcap_nametoaddrinfo(s
);
7623 bpf_error(cstate
, "invalid ip6 address %s", s
);
7626 bpf_error(cstate
, "%s resolved to multiple address", s
);
7627 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7629 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7630 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7631 memset(&mask
, 0, sizeof(mask
));
7632 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7634 mask
.s6_addr
[masklen
/ 8] =
7635 (0xff << (8 - masklen
% 8)) & 0xff;
7638 memcpy(a
, addr
, sizeof(a
));
7639 memcpy(m
, &mask
, sizeof(m
));
7640 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7641 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7642 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7650 bpf_error(cstate
, "Mask syntax for networks only");
7654 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7660 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7667 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7669 struct block
*b
, *tmp
;
7672 * Catch errors reported by us and routines below us, and return NULL
7675 if (setjmp(cstate
->top_ctx
))
7678 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7679 cstate
->e
= pcap_ether_aton(s
);
7680 if (cstate
->e
== NULL
)
7681 bpf_error(cstate
, "malloc");
7682 switch (cstate
->linktype
) {
7684 case DLT_NETANALYZER
:
7685 case DLT_NETANALYZER_TRANSPARENT
:
7686 tmp
= gen_prevlinkhdr_check(cstate
);
7687 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7692 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7695 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7697 case DLT_IEEE802_11
:
7698 case DLT_PRISM_HEADER
:
7699 case DLT_IEEE802_11_RADIO_AVS
:
7700 case DLT_IEEE802_11_RADIO
:
7702 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7704 case DLT_IP_OVER_FC
:
7705 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7710 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7717 bpf_error(cstate
, "ethernet address used in non-ether expression");
7722 sappend(struct slist
*s0
, struct slist
*s1
)
7725 * This is definitely not the best way to do this, but the
7726 * lists will rarely get long.
7733 static struct slist
*
7734 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7738 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7743 static struct slist
*
7744 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7748 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7754 * Modify "index" to use the value stored into its register as an
7755 * offset relative to the beginning of the header for the protocol
7756 * "proto", and allocate a register and put an item "size" bytes long
7757 * (1, 2, or 4) at that offset into that register, making it the register
7760 static struct arth
*
7761 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7765 struct slist
*s
, *tmp
;
7767 int regno
= alloc_reg(cstate
);
7769 free_reg(cstate
, inst
->regno
);
7773 bpf_error(cstate
, "data size must be 1, 2, or 4");
7790 bpf_error(cstate
, "unsupported index operation");
7794 * The offset is relative to the beginning of the packet
7795 * data, if we have a radio header. (If we don't, this
7798 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7799 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7800 cstate
->linktype
!= DLT_PRISM_HEADER
)
7801 bpf_error(cstate
, "radio information not present in capture");
7804 * Load into the X register the offset computed into the
7805 * register specified by "index".
7807 s
= xfer_to_x(cstate
, inst
);
7810 * Load the item at that offset.
7812 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7814 sappend(inst
->s
, s
);
7819 * The offset is relative to the beginning of
7820 * the link-layer header.
7822 * XXX - what about ATM LANE? Should the index be
7823 * relative to the beginning of the AAL5 frame, so
7824 * that 0 refers to the beginning of the LE Control
7825 * field, or relative to the beginning of the LAN
7826 * frame, so that 0 refers, for Ethernet LANE, to
7827 * the beginning of the destination address?
7829 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7832 * If "s" is non-null, it has code to arrange that the
7833 * X register contains the length of the prefix preceding
7834 * the link-layer header. Add to it the offset computed
7835 * into the register specified by "index", and move that
7836 * into the X register. Otherwise, just load into the X
7837 * register the offset computed into the register specified
7841 sappend(s
, xfer_to_a(cstate
, inst
));
7842 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7843 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7845 s
= xfer_to_x(cstate
, inst
);
7848 * Load the item at the sum of the offset we've put in the
7849 * X register and the offset of the start of the link
7850 * layer header (which is 0 if the radio header is
7851 * variable-length; that header length is what we put
7852 * into the X register and then added to the index).
7854 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7855 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7857 sappend(inst
->s
, s
);
7871 * The offset is relative to the beginning of
7872 * the network-layer header.
7873 * XXX - are there any cases where we want
7874 * cstate->off_nl_nosnap?
7876 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7879 * If "s" is non-null, it has code to arrange that the
7880 * X register contains the variable part of the offset
7881 * of the link-layer payload. Add to it the offset
7882 * computed into the register specified by "index",
7883 * and move that into the X register. Otherwise, just
7884 * load into the X register the offset computed into
7885 * the register specified by "index".
7888 sappend(s
, xfer_to_a(cstate
, inst
));
7889 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7890 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7892 s
= xfer_to_x(cstate
, inst
);
7895 * Load the item at the sum of the offset we've put in the
7896 * X register, the offset of the start of the network
7897 * layer header from the beginning of the link-layer
7898 * payload, and the constant part of the offset of the
7899 * start of the link-layer payload.
7901 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7902 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7904 sappend(inst
->s
, s
);
7907 * Do the computation only if the packet contains
7908 * the protocol in question.
7910 b
= gen_proto_abbrev_internal(cstate
, proto
);
7912 gen_and(inst
->b
, b
);
7926 * The offset is relative to the beginning of
7927 * the transport-layer header.
7929 * Load the X register with the length of the IPv4 header
7930 * (plus the offset of the link-layer header, if it's
7931 * a variable-length header), in bytes.
7933 * XXX - are there any cases where we want
7934 * cstate->off_nl_nosnap?
7935 * XXX - we should, if we're built with
7936 * IPv6 support, generate code to load either
7937 * IPv4, IPv6, or both, as appropriate.
7939 s
= gen_loadx_iphdrlen(cstate
);
7942 * The X register now contains the sum of the variable
7943 * part of the offset of the link-layer payload and the
7944 * length of the network-layer header.
7946 * Load into the A register the offset relative to
7947 * the beginning of the transport layer header,
7948 * add the X register to that, move that to the
7949 * X register, and load with an offset from the
7950 * X register equal to the sum of the constant part of
7951 * the offset of the link-layer payload and the offset,
7952 * relative to the beginning of the link-layer payload,
7953 * of the network-layer header.
7955 sappend(s
, xfer_to_a(cstate
, inst
));
7956 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7957 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7958 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7959 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7960 sappend(inst
->s
, s
);
7963 * Do the computation only if the packet contains
7964 * the protocol in question - which is true only
7965 * if this is an IP datagram and is the first or
7966 * only fragment of that datagram.
7968 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7970 gen_and(inst
->b
, b
);
7971 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7976 * Do the computation only if the packet contains
7977 * the protocol in question.
7979 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7981 gen_and(inst
->b
, b
);
7985 * Check if we have an icmp6 next header
7987 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7989 gen_and(inst
->b
, b
);
7992 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7994 * If "s" is non-null, it has code to arrange that the
7995 * X register contains the variable part of the offset
7996 * of the link-layer payload. Add to it the offset
7997 * computed into the register specified by "index",
7998 * and move that into the X register. Otherwise, just
7999 * load into the X register the offset computed into
8000 * the register specified by "index".
8003 sappend(s
, xfer_to_a(cstate
, inst
));
8004 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
8005 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
8007 s
= xfer_to_x(cstate
, inst
);
8010 * Load the item at the sum of the offset we've put in the
8011 * X register, the offset of the start of the network
8012 * layer header from the beginning of the link-layer
8013 * payload, and the constant part of the offset of the
8014 * start of the link-layer payload.
8016 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
8017 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
8020 sappend(inst
->s
, s
);
8024 inst
->regno
= regno
;
8025 s
= new_stmt(cstate
, BPF_ST
);
8027 sappend(inst
->s
, s
);
8033 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
8037 * Catch errors reported by us and routines below us, and return NULL
8040 if (setjmp(cstate
->top_ctx
))
8043 return gen_load_internal(cstate
, proto
, inst
, size
);
8046 static struct block
*
8047 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8048 struct arth
*a1
, int reversed
)
8050 struct slist
*s0
, *s1
, *s2
;
8051 struct block
*b
, *tmp
;
8053 s0
= xfer_to_x(cstate
, a1
);
8054 s1
= xfer_to_a(cstate
, a0
);
8055 if (code
== BPF_JEQ
) {
8056 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
8057 b
= new_block(cstate
, JMP(code
));
8061 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
8067 sappend(a0
->s
, a1
->s
);
8071 free_reg(cstate
, a0
->regno
);
8072 free_reg(cstate
, a1
->regno
);
8074 /* 'and' together protocol checks */
8077 gen_and(a0
->b
, tmp
= a1
->b
);
8091 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8092 struct arth
*a1
, int reversed
)
8095 * Catch errors reported by us and routines below us, and return NULL
8098 if (setjmp(cstate
->top_ctx
))
8101 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
8105 gen_loadlen(compiler_state_t
*cstate
)
8112 * Catch errors reported by us and routines below us, and return NULL
8115 if (setjmp(cstate
->top_ctx
))
8118 regno
= alloc_reg(cstate
);
8119 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8120 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8121 s
->next
= new_stmt(cstate
, BPF_ST
);
8122 s
->next
->s
.k
= regno
;
8129 static struct arth
*
8130 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8136 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8138 reg
= alloc_reg(cstate
);
8140 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8142 s
->next
= new_stmt(cstate
, BPF_ST
);
8151 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8154 * Catch errors reported by us and routines below us, and return NULL
8157 if (setjmp(cstate
->top_ctx
))
8160 return gen_loadi_internal(cstate
, val
);
8164 * The a_arg dance is to avoid annoying whining by compilers that
8165 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8166 * It's not *used* after setjmp returns.
8169 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8171 struct arth
*a
= a_arg
;
8175 * Catch errors reported by us and routines below us, and return NULL
8178 if (setjmp(cstate
->top_ctx
))
8181 s
= xfer_to_a(cstate
, a
);
8183 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8186 s
= new_stmt(cstate
, BPF_ST
);
8194 * The a0_arg dance is to avoid annoying whining by compilers that
8195 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8196 * It's not *used* after setjmp returns.
8199 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8202 struct arth
*a0
= a0_arg
;
8203 struct slist
*s0
, *s1
, *s2
;
8206 * Catch errors reported by us and routines below us, and return NULL
8209 if (setjmp(cstate
->top_ctx
))
8213 * Disallow division by, or modulus by, zero; we do this here
8214 * so that it gets done even if the optimizer is disabled.
8216 * Also disallow shifts by a value greater than 31; we do this
8217 * here, for the same reason.
8219 if (code
== BPF_DIV
) {
8220 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8221 bpf_error(cstate
, "division by zero");
8222 } else if (code
== BPF_MOD
) {
8223 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8224 bpf_error(cstate
, "modulus by zero");
8225 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8226 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8227 bpf_error(cstate
, "shift by more than 31 bits");
8229 s0
= xfer_to_x(cstate
, a1
);
8230 s1
= xfer_to_a(cstate
, a0
);
8231 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8236 sappend(a0
->s
, a1
->s
);
8238 free_reg(cstate
, a0
->regno
);
8239 free_reg(cstate
, a1
->regno
);
8241 s0
= new_stmt(cstate
, BPF_ST
);
8242 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8249 * Initialize the table of used registers and the current register.
8252 init_regs(compiler_state_t
*cstate
)
8255 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8259 * Return the next free register.
8262 alloc_reg(compiler_state_t
*cstate
)
8264 int n
= BPF_MEMWORDS
;
8267 if (cstate
->regused
[cstate
->curreg
])
8268 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8270 cstate
->regused
[cstate
->curreg
] = 1;
8271 return cstate
->curreg
;
8274 bpf_error(cstate
, "too many registers needed to evaluate expression");
8279 * Return a register to the table so it can
8283 free_reg(compiler_state_t
*cstate
, int n
)
8285 cstate
->regused
[n
] = 0;
8288 static struct block
*
8289 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8294 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8295 b
= new_block(cstate
, JMP(jmp
));
8303 gen_greater(compiler_state_t
*cstate
, int n
)
8306 * Catch errors reported by us and routines below us, and return NULL
8309 if (setjmp(cstate
->top_ctx
))
8312 return gen_len(cstate
, BPF_JGE
, n
);
8316 * Actually, this is less than or equal.
8319 gen_less(compiler_state_t
*cstate
, int n
)
8324 * Catch errors reported by us and routines below us, and return NULL
8327 if (setjmp(cstate
->top_ctx
))
8330 b
= gen_len(cstate
, BPF_JGT
, n
);
8337 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8338 * the beginning of the link-layer header.
8339 * XXX - that means you can't test values in the radiotap header, but
8340 * as that header is difficult if not impossible to parse generally
8341 * without a loop, that might not be a severe problem. A new keyword
8342 * "radio" could be added for that, although what you'd really want
8343 * would be a way of testing particular radio header values, which
8344 * would generate code appropriate to the radio header in question.
8347 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8353 * Catch errors reported by us and routines below us, and return NULL
8356 if (setjmp(cstate
->top_ctx
))
8364 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8367 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8371 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8375 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8379 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8383 b
= new_block(cstate
, JMP(BPF_JEQ
));
8390 static const u_char abroadcast
[] = { 0x0 };
8393 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8395 bpf_u_int32 hostmask
;
8396 struct block
*b0
, *b1
, *b2
;
8397 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8400 * Catch errors reported by us and routines below us, and return NULL
8403 if (setjmp(cstate
->top_ctx
))
8410 switch (cstate
->linktype
) {
8412 case DLT_ARCNET_LINUX
:
8413 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8415 case DLT_NETANALYZER
:
8416 case DLT_NETANALYZER_TRANSPARENT
:
8417 b1
= gen_prevlinkhdr_check(cstate
);
8418 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8423 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8425 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8426 case DLT_IEEE802_11
:
8427 case DLT_PRISM_HEADER
:
8428 case DLT_IEEE802_11_RADIO_AVS
:
8429 case DLT_IEEE802_11_RADIO
:
8431 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8432 case DLT_IP_OVER_FC
:
8433 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8435 bpf_error(cstate
, "not a broadcast link");
8441 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8442 * as an indication that we don't know the netmask, and fail
8445 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8446 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8447 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8448 hostmask
= ~cstate
->netmask
;
8449 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8450 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8451 ~0 & hostmask
, hostmask
);
8456 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8461 * Generate code to test the low-order bit of a MAC address (that's
8462 * the bottom bit of the *first* byte).
8464 static struct block
*
8465 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8467 register struct block
*b0
;
8468 register struct slist
*s
;
8470 /* link[offset] & 1 != 0 */
8471 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8472 b0
= new_block(cstate
, JMP(BPF_JSET
));
8479 gen_multicast(compiler_state_t
*cstate
, int proto
)
8481 register struct block
*b0
, *b1
, *b2
;
8482 register struct slist
*s
;
8485 * Catch errors reported by us and routines below us, and return NULL
8488 if (setjmp(cstate
->top_ctx
))
8495 switch (cstate
->linktype
) {
8497 case DLT_ARCNET_LINUX
:
8498 /* all ARCnet multicasts use the same address */
8499 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8501 case DLT_NETANALYZER
:
8502 case DLT_NETANALYZER_TRANSPARENT
:
8503 b1
= gen_prevlinkhdr_check(cstate
);
8504 /* ether[0] & 1 != 0 */
8505 b0
= gen_mac_multicast(cstate
, 0);
8511 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8513 * XXX - was that referring to bit-order issues?
8515 /* fddi[1] & 1 != 0 */
8516 return gen_mac_multicast(cstate
, 1);
8518 /* tr[2] & 1 != 0 */
8519 return gen_mac_multicast(cstate
, 2);
8520 case DLT_IEEE802_11
:
8521 case DLT_PRISM_HEADER
:
8522 case DLT_IEEE802_11_RADIO_AVS
:
8523 case DLT_IEEE802_11_RADIO
:
8528 * For control frames, there is no DA.
8530 * For management frames, DA is at an
8531 * offset of 4 from the beginning of
8534 * For data frames, DA is at an offset
8535 * of 4 from the beginning of the packet
8536 * if To DS is clear and at an offset of
8537 * 16 from the beginning of the packet
8542 * Generate the tests to be done for data frames.
8544 * First, check for To DS set, i.e. "link[1] & 0x01".
8546 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8547 b1
= new_block(cstate
, JMP(BPF_JSET
));
8548 b1
->s
.k
= 0x01; /* To DS */
8552 * If To DS is set, the DA is at 16.
8554 b0
= gen_mac_multicast(cstate
, 16);
8558 * Now, check for To DS not set, i.e. check
8559 * "!(link[1] & 0x01)".
8561 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8562 b2
= new_block(cstate
, JMP(BPF_JSET
));
8563 b2
->s
.k
= 0x01; /* To DS */
8568 * If To DS is not set, the DA is at 4.
8570 b1
= gen_mac_multicast(cstate
, 4);
8574 * Now OR together the last two checks. That gives
8575 * the complete set of checks for data frames.
8580 * Now check for a data frame.
8581 * I.e, check "link[0] & 0x08".
8583 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8584 b1
= new_block(cstate
, JMP(BPF_JSET
));
8589 * AND that with the checks done for data frames.
8594 * If the high-order bit of the type value is 0, this
8595 * is a management frame.
8596 * I.e, check "!(link[0] & 0x08)".
8598 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8599 b2
= new_block(cstate
, JMP(BPF_JSET
));
8605 * For management frames, the DA is at 4.
8607 b1
= gen_mac_multicast(cstate
, 4);
8611 * OR that with the checks done for data frames.
8612 * That gives the checks done for management and
8618 * If the low-order bit of the type value is 1,
8619 * this is either a control frame or a frame
8620 * with a reserved type, and thus not a
8623 * I.e., check "!(link[0] & 0x04)".
8625 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8626 b1
= new_block(cstate
, JMP(BPF_JSET
));
8632 * AND that with the checks for data and management
8637 case DLT_IP_OVER_FC
:
8638 b0
= gen_mac_multicast(cstate
, 2);
8643 /* Link not known to support multicasts */
8647 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8648 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8653 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8654 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8658 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8663 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8665 register struct block
*b0
;
8668 * Catch errors reported by us and routines below us, and return NULL
8671 if (setjmp(cstate
->top_ctx
))
8675 * Only some data link types support ifindex qualifiers.
8677 switch (cstate
->linktype
) {
8678 case DLT_LINUX_SLL2
:
8679 /* match packets on this interface */
8680 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8683 #if defined(__linux__)
8685 * This is Linux; we require PF_PACKET support.
8686 * If this is a *live* capture, we can look at
8687 * special meta-data in the filter expression;
8688 * if it's a savefile, we can't.
8690 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8691 /* We have a FILE *, so this is a savefile */
8692 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8693 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8697 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8699 #else /* defined(__linux__) */
8700 bpf_error(cstate
, "ifindex not supported on %s",
8701 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8703 #endif /* defined(__linux__) */
8709 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8710 * Outbound traffic is sent by this machine, while inbound traffic is
8711 * sent by a remote machine (and may include packets destined for a
8712 * unicast or multicast link-layer address we are not subscribing to).
8713 * These are the same definitions implemented by pcap_setdirection().
8714 * Capturing only unicast traffic destined for this host is probably
8715 * better accomplished using a higher-layer filter.
8718 gen_inbound(compiler_state_t
*cstate
, int dir
)
8720 register struct block
*b0
;
8723 * Catch errors reported by us and routines below us, and return NULL
8726 if (setjmp(cstate
->top_ctx
))
8730 * Only some data link types support inbound/outbound qualifiers.
8732 switch (cstate
->linktype
) {
8734 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8735 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8736 gen_loadi_internal(cstate
, 0),
8742 /* match outgoing packets */
8743 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8745 /* match incoming packets */
8746 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8751 /* match outgoing packets */
8752 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8754 /* to filter on inbound traffic, invert the match */
8759 case DLT_LINUX_SLL2
:
8760 /* match outgoing packets */
8761 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8763 /* to filter on inbound traffic, invert the match */
8769 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8770 ((dir
== 0) ? PF_IN
: PF_OUT
));
8775 /* match outgoing packets */
8776 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8778 /* match incoming packets */
8779 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8783 case DLT_JUNIPER_MFR
:
8784 case DLT_JUNIPER_MLFR
:
8785 case DLT_JUNIPER_MLPPP
:
8786 case DLT_JUNIPER_ATM1
:
8787 case DLT_JUNIPER_ATM2
:
8788 case DLT_JUNIPER_PPPOE
:
8789 case DLT_JUNIPER_PPPOE_ATM
:
8790 case DLT_JUNIPER_GGSN
:
8791 case DLT_JUNIPER_ES
:
8792 case DLT_JUNIPER_MONITOR
:
8793 case DLT_JUNIPER_SERVICES
:
8794 case DLT_JUNIPER_ETHER
:
8795 case DLT_JUNIPER_PPP
:
8796 case DLT_JUNIPER_FRELAY
:
8797 case DLT_JUNIPER_CHDLC
:
8798 case DLT_JUNIPER_VP
:
8799 case DLT_JUNIPER_ST
:
8800 case DLT_JUNIPER_ISM
:
8801 case DLT_JUNIPER_VS
:
8802 case DLT_JUNIPER_SRX_E2E
:
8803 case DLT_JUNIPER_FIBRECHANNEL
:
8804 case DLT_JUNIPER_ATM_CEMIC
:
8806 /* juniper flags (including direction) are stored
8807 * the byte after the 3-byte magic number */
8809 /* match outgoing packets */
8810 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8812 /* match incoming packets */
8813 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8819 * If we have packet meta-data indicating a direction,
8820 * and that metadata can be checked by BPF code, check
8821 * it. Otherwise, give up, as this link-layer type has
8822 * nothing in the packet data.
8824 * Currently, the only platform where a BPF filter can
8825 * check that metadata is Linux with the in-kernel
8826 * BPF interpreter. If other packet capture mechanisms
8827 * and BPF filters also supported this, it would be
8828 * nice. It would be even better if they made that
8829 * metadata available so that we could provide it
8830 * with newer capture APIs, allowing it to be saved
8833 #if defined(__linux__)
8835 * This is Linux; we require PF_PACKET support.
8836 * If this is a *live* capture, we can look at
8837 * special meta-data in the filter expression;
8838 * if it's a savefile, we can't.
8840 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8841 /* We have a FILE *, so this is a savefile */
8842 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8843 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8846 /* match outgoing packets */
8847 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8850 /* to filter on inbound traffic, invert the match */
8853 #else /* defined(__linux__) */
8854 bpf_error(cstate
, "inbound/outbound not supported on %s",
8855 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8857 #endif /* defined(__linux__) */
8862 /* PF firewall log matched interface */
8864 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8870 * Catch errors reported by us and routines below us, and return NULL
8873 if (setjmp(cstate
->top_ctx
))
8876 if (cstate
->linktype
!= DLT_PFLOG
) {
8877 bpf_error(cstate
, "ifname supported only on PF linktype");
8880 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8881 off
= offsetof(struct pfloghdr
, ifname
);
8882 if (strlen(ifname
) >= len
) {
8883 bpf_error(cstate
, "ifname interface names can only be %d characters",
8887 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8888 (const u_char
*)ifname
);
8892 /* PF firewall log ruleset name */
8894 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8899 * Catch errors reported by us and routines below us, and return NULL
8902 if (setjmp(cstate
->top_ctx
))
8905 if (cstate
->linktype
!= DLT_PFLOG
) {
8906 bpf_error(cstate
, "ruleset supported only on PF linktype");
8910 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8911 bpf_error(cstate
, "ruleset names can only be %ld characters",
8912 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8916 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8917 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8921 /* PF firewall log rule number */
8923 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8928 * Catch errors reported by us and routines below us, and return NULL
8931 if (setjmp(cstate
->top_ctx
))
8934 if (cstate
->linktype
!= DLT_PFLOG
) {
8935 bpf_error(cstate
, "rnr supported only on PF linktype");
8939 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8944 /* PF firewall log sub-rule number */
8946 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8951 * Catch errors reported by us and routines below us, and return NULL
8954 if (setjmp(cstate
->top_ctx
))
8957 if (cstate
->linktype
!= DLT_PFLOG
) {
8958 bpf_error(cstate
, "srnr supported only on PF linktype");
8962 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8967 /* PF firewall log reason code */
8969 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8974 * Catch errors reported by us and routines below us, and return NULL
8977 if (setjmp(cstate
->top_ctx
))
8980 if (cstate
->linktype
!= DLT_PFLOG
) {
8981 bpf_error(cstate
, "reason supported only on PF linktype");
8985 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8986 (bpf_u_int32
)reason
);
8990 /* PF firewall log action */
8992 gen_pf_action(compiler_state_t
*cstate
, int action
)
8997 * Catch errors reported by us and routines below us, and return NULL
9000 if (setjmp(cstate
->top_ctx
))
9003 if (cstate
->linktype
!= DLT_PFLOG
) {
9004 bpf_error(cstate
, "action supported only on PF linktype");
9008 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
9009 (bpf_u_int32
)action
);
9013 /* IEEE 802.11 wireless header */
9015 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
9020 * Catch errors reported by us and routines below us, and return NULL
9023 if (setjmp(cstate
->top_ctx
))
9026 switch (cstate
->linktype
) {
9028 case DLT_IEEE802_11
:
9029 case DLT_PRISM_HEADER
:
9030 case DLT_IEEE802_11_RADIO_AVS
:
9031 case DLT_IEEE802_11_RADIO
:
9032 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
9036 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
9044 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
9049 * Catch errors reported by us and routines below us, and return NULL
9052 if (setjmp(cstate
->top_ctx
))
9055 switch (cstate
->linktype
) {
9057 case DLT_IEEE802_11
:
9058 case DLT_PRISM_HEADER
:
9059 case DLT_IEEE802_11_RADIO_AVS
:
9060 case DLT_IEEE802_11_RADIO
:
9064 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
9068 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
9069 IEEE80211_FC1_DIR_MASK
);
9075 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
9080 * Catch errors reported by us and routines below us, and return NULL
9083 if (setjmp(cstate
->top_ctx
))
9086 switch (cstate
->linktype
) {
9089 case DLT_ARCNET_LINUX
:
9090 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
9091 q
.proto
== Q_LINK
) {
9092 cstate
->e
= pcap_ether_aton(s
);
9093 if (cstate
->e
== NULL
)
9094 bpf_error(cstate
, "malloc");
9095 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
9100 bpf_error(cstate
, "ARCnet address used in non-arc expression");
9104 bpf_error(cstate
, "aid supported only on ARCnet");
9109 static struct block
*
9110 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
9112 register struct block
*b0
, *b1
;
9115 /* src comes first, different from Ethernet */
9117 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
9120 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
9123 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9124 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9130 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9131 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9136 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9140 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9144 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9148 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9152 bpf_error(cstate
, "'ra' is only supported on 802.11");
9156 bpf_error(cstate
, "'ta' is only supported on 802.11");
9163 static struct block
*
9164 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9166 struct block
*b0
, *b1
;
9168 /* check for VLAN, including 802.1ad and QinQ */
9169 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9170 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9173 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9179 static struct block
*
9180 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9182 if (vlan_num
> 0x0fff) {
9183 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9186 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9189 static struct block
*
9190 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9193 struct block
*b0
, *b1
;
9195 b0
= gen_vlan_tpid_test(cstate
);
9198 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9204 * Both payload and link header type follow the VLAN tags so that
9205 * both need to be updated.
9207 cstate
->off_linkpl
.constant_part
+= 4;
9208 cstate
->off_linktype
.constant_part
+= 4;
9213 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9214 /* add v to variable part of off */
9216 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9217 bpf_u_int32 v
, struct slist
*s
)
9221 if (!off
->is_variable
)
9222 off
->is_variable
= 1;
9224 off
->reg
= alloc_reg(cstate
);
9226 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9229 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9232 s2
= new_stmt(cstate
, BPF_ST
);
9238 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9239 * and link type offsets first
9242 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9246 /* offset determined at run time, shift variable part */
9248 cstate
->is_vlan_vloffset
= 1;
9249 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9250 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9252 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9253 sappend(s
.next
, b_tpid
->head
->stmts
);
9254 b_tpid
->head
->stmts
= s
.next
;
9258 * patch block b_vid (VLAN id test) to load VID value either from packet
9259 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9262 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9264 struct slist
*s
, *s2
, *sjeq
;
9267 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9268 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9270 /* true -> next instructions, false -> beginning of b_vid */
9271 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9273 sjeq
->s
.jf
= b_vid
->stmts
;
9276 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9277 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
9281 /* Jump to the test in b_vid. We need to jump one instruction before
9282 * the end of the b_vid block so that we only skip loading the TCI
9283 * from packet data and not the 'and' instruction extracting VID.
9286 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9288 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9292 /* insert our statements at the beginning of b_vid */
9293 sappend(s
, b_vid
->stmts
);
9298 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9299 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9300 * tag can be either in metadata or in packet data; therefore if the
9301 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9302 * header for VLAN tag. As the decision is done at run time, we need
9303 * update variable part of the offsets
9305 static struct block
*
9306 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9309 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9312 /* generate new filter code based on extracting packet
9314 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9315 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9317 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9322 * This is tricky. We need to insert the statements updating variable
9323 * parts of offsets before the traditional TPID and VID tests so
9324 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9325 * we do not want this update to affect those checks. That's why we
9326 * generate both test blocks first and insert the statements updating
9327 * variable parts of both offsets after that. This wouldn't work if
9328 * there already were variable length link header when entering this
9329 * function but gen_vlan_bpf_extensions() isn't called in that case.
9331 b_tpid
= gen_vlan_tpid_test(cstate
);
9333 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9335 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9340 gen_vlan_patch_vid_test(cstate
, b_vid
);
9350 * support IEEE 802.1Q VLAN trunk over ethernet
9353 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9358 * Catch errors reported by us and routines below us, and return NULL
9361 if (setjmp(cstate
->top_ctx
))
9364 /* can't check for VLAN-encapsulated packets inside MPLS */
9365 if (cstate
->label_stack_depth
> 0)
9366 bpf_error(cstate
, "no VLAN match after MPLS");
9369 * Check for a VLAN packet, and then change the offsets to point
9370 * to the type and data fields within the VLAN packet. Just
9371 * increment the offsets, so that we can support a hierarchy, e.g.
9372 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9375 * XXX - this is a bit of a kludge. If we were to split the
9376 * compiler into a parser that parses an expression and
9377 * generates an expression tree, and a code generator that
9378 * takes an expression tree (which could come from our
9379 * parser or from some other parser) and generates BPF code,
9380 * we could perhaps make the offsets parameters of routines
9381 * and, in the handler for an "AND" node, pass to subnodes
9382 * other than the VLAN node the adjusted offsets.
9384 * This would mean that "vlan" would, instead of changing the
9385 * behavior of *all* tests after it, change only the behavior
9386 * of tests ANDed with it. That would change the documented
9387 * semantics of "vlan", which might break some expressions.
9388 * However, it would mean that "(vlan and ip) or ip" would check
9389 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9390 * checking only for VLAN-encapsulated IP, so that could still
9391 * be considered worth doing; it wouldn't break expressions
9392 * that are of the form "vlan and ..." or "vlan N and ...",
9393 * which I suspect are the most common expressions involving
9394 * "vlan". "vlan or ..." doesn't necessarily do what the user
9395 * would really want, now, as all the "or ..." tests would
9396 * be done assuming a VLAN, even though the "or" could be viewed
9397 * as meaning "or, if this isn't a VLAN packet...".
9399 switch (cstate
->linktype
) {
9403 * Newer version of the Linux kernel pass around
9404 * packets in which the VLAN tag has been removed
9405 * from the packet data and put into metadata.
9407 * This requires special treatment.
9409 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9410 /* Verify that this is the outer part of the packet and
9411 * not encapsulated somehow. */
9412 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9413 cstate
->off_linkhdr
.constant_part
==
9414 cstate
->off_outermostlinkhdr
.constant_part
) {
9416 * Do we need special VLAN handling?
9418 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9419 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9422 b0
= gen_vlan_no_bpf_extensions(cstate
,
9423 vlan_num
, has_vlan_tag
);
9426 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9430 case DLT_NETANALYZER
:
9431 case DLT_NETANALYZER_TRANSPARENT
:
9432 case DLT_IEEE802_11
:
9433 case DLT_PRISM_HEADER
:
9434 case DLT_IEEE802_11_RADIO_AVS
:
9435 case DLT_IEEE802_11_RADIO
:
9437 * These are either Ethernet packets with an additional
9438 * metadata header (the NetAnalyzer types), or 802.11
9439 * packets, possibly with an additional metadata header.
9441 * For the first of those, the VLAN tag is in the normal
9442 * place, so the special-case handling above isn't
9445 * For the second of those, we don't do the special-case
9448 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9452 bpf_error(cstate
, "no VLAN support for %s",
9453 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9457 cstate
->vlan_stack_depth
++;
9465 * The label_num_arg dance is to avoid annoying whining by compilers that
9466 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9467 * It's not *used* after setjmp returns.
9469 static struct block
*
9470 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9473 struct block
*b0
, *b1
;
9475 if (cstate
->label_stack_depth
> 0) {
9476 /* just match the bottom-of-stack bit clear */
9477 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9480 * We're not in an MPLS stack yet, so check the link-layer
9481 * type against MPLS.
9483 switch (cstate
->linktype
) {
9485 case DLT_C_HDLC
: /* fall through */
9488 case DLT_NETANALYZER
:
9489 case DLT_NETANALYZER_TRANSPARENT
:
9490 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9494 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9497 /* FIXME add other DLT_s ...
9498 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9499 * leave it for now */
9502 bpf_error(cstate
, "no MPLS support for %s",
9503 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9508 /* If a specific MPLS label is requested, check it */
9509 if (has_label_num
) {
9510 if (label_num
> 0xFFFFF) {
9511 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9512 label_num
, 0xFFFFF);
9514 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9515 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9516 0xfffff000); /* only compare the first 20 bits */
9522 * Change the offsets to point to the type and data fields within
9523 * the MPLS packet. Just increment the offsets, so that we
9524 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9525 * capture packets with an outer label of 100000 and an inner
9528 * Increment the MPLS stack depth as well; this indicates that
9529 * we're checking MPLS-encapsulated headers, to make sure higher
9530 * level code generators don't try to match against IP-related
9531 * protocols such as Q_ARP, Q_RARP etc.
9533 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9535 cstate
->off_nl_nosnap
+= 4;
9536 cstate
->off_nl
+= 4;
9537 cstate
->label_stack_depth
++;
9542 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9545 * Catch errors reported by us and routines below us, and return NULL
9548 if (setjmp(cstate
->top_ctx
))
9551 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9555 * Support PPPOE discovery and session.
9558 gen_pppoed(compiler_state_t
*cstate
)
9561 * Catch errors reported by us and routines below us, and return NULL
9564 if (setjmp(cstate
->top_ctx
))
9567 /* check for PPPoE discovery */
9568 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9572 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9574 struct block
*b0
, *b1
;
9577 * Catch errors reported by us and routines below us, and return NULL
9580 if (setjmp(cstate
->top_ctx
))
9584 * Test against the PPPoE session link-layer type.
9586 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9588 /* If a specific session is requested, check PPPoE session id */
9590 if (sess_num
> 0x0000ffff) {
9591 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9592 sess_num
, 0x0000ffff);
9594 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9600 * Change the offsets to point to the type and data fields within
9601 * the PPP packet, and note that this is PPPoE rather than
9604 * XXX - this is a bit of a kludge. See the comments in
9607 * The "network-layer" protocol is PPPoE, which has a 6-byte
9608 * PPPoE header, followed by a PPP packet.
9610 * There is no HDLC encapsulation for the PPP packet (it's
9611 * encapsulated in PPPoES instead), so the link-layer type
9612 * starts at the first byte of the PPP packet. For PPPoE,
9613 * that offset is relative to the beginning of the total
9614 * link-layer payload, including any 802.2 LLC header, so
9615 * it's 6 bytes past cstate->off_nl.
9617 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9618 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9619 cstate
->off_linkpl
.reg
);
9621 cstate
->off_linktype
= cstate
->off_linkhdr
;
9622 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9625 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9630 /* Check that this is Geneve and the VNI is correct if
9631 * specified. Parameterized to handle both IPv4 and IPv6. */
9632 static struct block
*
9633 gen_geneve_check(compiler_state_t
*cstate
,
9634 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9635 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9637 struct block
*b0
, *b1
;
9639 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9641 /* Check that we are operating on version 0. Otherwise, we
9642 * can't decode the rest of the fields. The version is 2 bits
9643 * in the first byte of the Geneve header. */
9644 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9649 if (vni
> 0xffffff) {
9650 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9653 vni
<<= 8; /* VNI is in the upper 3 bytes */
9654 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9662 /* The IPv4 and IPv6 Geneve checks need to do two things:
9663 * - Verify that this actually is Geneve with the right VNI.
9664 * - Place the IP header length (plus variable link prefix if
9665 * needed) into register A to be used later to compute
9666 * the inner packet offsets. */
9667 static struct block
*
9668 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9670 struct block
*b0
, *b1
;
9671 struct slist
*s
, *s1
;
9673 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9675 /* Load the IP header length into A. */
9676 s
= gen_loadx_iphdrlen(cstate
);
9678 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9681 /* Forcibly append these statements to the true condition
9682 * of the protocol check by creating a new block that is
9683 * always true and ANDing them. */
9684 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9693 static struct block
*
9694 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9696 struct block
*b0
, *b1
;
9697 struct slist
*s
, *s1
;
9699 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9701 /* Load the IP header length. We need to account for a
9702 * variable length link prefix if there is one. */
9703 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9705 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9709 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9713 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9717 /* Forcibly append these statements to the true condition
9718 * of the protocol check by creating a new block that is
9719 * always true and ANDing them. */
9720 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9723 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9732 /* We need to store three values based on the Geneve header::
9733 * - The offset of the linktype.
9734 * - The offset of the end of the Geneve header.
9735 * - The offset of the end of the encapsulated MAC header. */
9736 static struct slist
*
9737 gen_geneve_offsets(compiler_state_t
*cstate
)
9739 struct slist
*s
, *s1
, *s_proto
;
9741 /* First we need to calculate the offset of the Geneve header
9742 * itself. This is composed of the IP header previously calculated
9743 * (include any variable link prefix) and stored in A plus the
9744 * fixed sized headers (fixed link prefix, MAC length, and UDP
9746 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9747 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9749 /* Stash this in X since we'll need it later. */
9750 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9753 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9755 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9759 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9760 cstate
->off_linktype
.is_variable
= 1;
9761 cstate
->off_linktype
.constant_part
= 0;
9763 s1
= new_stmt(cstate
, BPF_ST
);
9764 s1
->s
.k
= cstate
->off_linktype
.reg
;
9767 /* Load the Geneve option length and mask and shift to get the
9768 * number of bytes. It is stored in the first byte of the Geneve
9770 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9774 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9778 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9782 /* Add in the rest of the Geneve base header. */
9783 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9787 /* Add the Geneve header length to its offset and store. */
9788 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9792 /* Set the encapsulated type as Ethernet. Even though we may
9793 * not actually have Ethernet inside there are two reasons this
9795 * - The linktype field is always in EtherType format regardless
9796 * of whether it is in Geneve or an inner Ethernet frame.
9797 * - The only link layer that we have specific support for is
9798 * Ethernet. We will confirm that the packet actually is
9799 * Ethernet at runtime before executing these checks. */
9800 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9802 s1
= new_stmt(cstate
, BPF_ST
);
9803 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9806 /* Calculate whether we have an Ethernet header or just raw IP/
9807 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9808 * and linktype by 14 bytes so that the network header can be found
9809 * seamlessly. Otherwise, keep what we've calculated already. */
9811 /* We have a bare jmp so we can't use the optimizer. */
9812 cstate
->no_optimize
= 1;
9814 /* Load the EtherType in the Geneve header, 2 bytes in. */
9815 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9819 /* Load X with the end of the Geneve header. */
9820 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9821 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9824 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9825 * end of this check, we should have the total length in X. In
9826 * the non-Ethernet case, it's already there. */
9827 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9828 s_proto
->s
.k
= ETHERTYPE_TEB
;
9829 sappend(s
, s_proto
);
9831 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9835 /* Since this is Ethernet, use the EtherType of the payload
9836 * directly as the linktype. Overwrite what we already have. */
9837 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9841 s1
= new_stmt(cstate
, BPF_ST
);
9842 s1
->s
.k
= cstate
->off_linktype
.reg
;
9845 /* Advance two bytes further to get the end of the Ethernet
9847 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9851 /* Move the result to X. */
9852 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9855 /* Store the final result of our linkpl calculation. */
9856 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9857 cstate
->off_linkpl
.is_variable
= 1;
9858 cstate
->off_linkpl
.constant_part
= 0;
9860 s1
= new_stmt(cstate
, BPF_STX
);
9861 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9870 /* Check to see if this is a Geneve packet. */
9872 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9874 struct block
*b0
, *b1
;
9878 * Catch errors reported by us and routines below us, and return NULL
9881 if (setjmp(cstate
->top_ctx
))
9884 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9885 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9890 /* Later filters should act on the payload of the Geneve frame,
9891 * update all of the header pointers. Attach this code so that
9892 * it gets executed in the event that the Geneve filter matches. */
9893 s
= gen_geneve_offsets(cstate
);
9895 b1
= gen_true(cstate
);
9896 sappend(s
, b1
->stmts
);
9901 cstate
->is_encap
= 1;
9906 /* Check that this is VXLAN and the VNI is correct if
9907 * specified. Parameterized to handle both IPv4 and IPv6. */
9908 static struct block
*
9909 gen_vxlan_check(compiler_state_t
*cstate
,
9910 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9911 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9913 struct block
*b0
, *b1
;
9915 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9917 /* Check that the VXLAN header has the flag bits set
9919 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9924 if (vni
> 0xffffff) {
9925 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9928 vni
<<= 8; /* VNI is in the upper 3 bytes */
9929 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9937 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9938 * - Verify that this actually is VXLAN with the right VNI.
9939 * - Place the IP header length (plus variable link prefix if
9940 * needed) into register A to be used later to compute
9941 * the inner packet offsets. */
9942 static struct block
*
9943 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9945 struct block
*b0
, *b1
;
9946 struct slist
*s
, *s1
;
9948 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9950 /* Load the IP header length into A. */
9951 s
= gen_loadx_iphdrlen(cstate
);
9953 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9956 /* Forcibly append these statements to the true condition
9957 * of the protocol check by creating a new block that is
9958 * always true and ANDing them. */
9959 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9968 static struct block
*
9969 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9971 struct block
*b0
, *b1
;
9972 struct slist
*s
, *s1
;
9974 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9976 /* Load the IP header length. We need to account for a
9977 * variable length link prefix if there is one. */
9978 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9980 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9984 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9988 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9992 /* Forcibly append these statements to the true condition
9993 * of the protocol check by creating a new block that is
9994 * always true and ANDing them. */
9995 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9998 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10007 /* We need to store three values based on the VXLAN header:
10008 * - The offset of the linktype.
10009 * - The offset of the end of the VXLAN header.
10010 * - The offset of the end of the encapsulated MAC header. */
10011 static struct slist
*
10012 gen_vxlan_offsets(compiler_state_t
*cstate
)
10014 struct slist
*s
, *s1
;
10016 /* Calculate the offset of the VXLAN header itself. This
10017 * includes the IP header computed previously (including any
10018 * variable link prefix) and stored in A plus the fixed size
10019 * headers (fixed link prefix, MAC length, UDP header). */
10020 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10021 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
10023 /* Add the VXLAN header length to its offset and store */
10024 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10028 /* Push the link header. VXLAN packets always contain Ethernet
10030 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
10032 s1
= new_stmt(cstate
, BPF_ST
);
10033 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
10036 /* As the payload is an Ethernet packet, we can use the
10037 * EtherType of the payload directly as the linktype. */
10038 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10042 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
10043 cstate
->off_linktype
.is_variable
= 1;
10044 cstate
->off_linktype
.constant_part
= 0;
10046 s1
= new_stmt(cstate
, BPF_ST
);
10047 s1
->s
.k
= cstate
->off_linktype
.reg
;
10050 /* Two bytes further is the end of the Ethernet header and the
10051 * start of the payload. */
10052 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10056 /* Move the result to X. */
10057 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10060 /* Store the final result of our linkpl calculation. */
10061 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
10062 cstate
->off_linkpl
.is_variable
= 1;
10063 cstate
->off_linkpl
.constant_part
= 0;
10065 s1
= new_stmt(cstate
, BPF_STX
);
10066 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10069 cstate
->off_nl
= 0;
10074 /* Check to see if this is a VXLAN packet. */
10076 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
10078 struct block
*b0
, *b1
;
10082 * Catch errors reported by us and routines below us, and return NULL
10085 if (setjmp(cstate
->top_ctx
))
10088 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
10089 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
10094 /* Later filters should act on the payload of the VXLAN frame,
10095 * update all of the header pointers. Attach this code so that
10096 * it gets executed in the event that the VXLAN filter matches. */
10097 s
= gen_vxlan_offsets(cstate
);
10099 b1
= gen_true(cstate
);
10100 sappend(s
, b1
->stmts
);
10105 cstate
->is_encap
= 1;
10110 /* Check that the encapsulated frame has a link layer header
10111 * for Ethernet filters. */
10112 static struct block
*
10113 gen_encap_ll_check(compiler_state_t
*cstate
)
10116 struct slist
*s
, *s1
;
10118 /* The easiest way to see if there is a link layer present
10119 * is to check if the link layer header and payload are not
10122 /* Geneve always generates pure variable offsets so we can
10123 * compare only the registers. */
10124 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
10125 s
->s
.k
= cstate
->off_linkhdr
.reg
;
10127 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
10128 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10131 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10139 static struct block
*
10140 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
10141 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10145 switch (atmfield
) {
10148 if (!cstate
->is_atm
)
10149 bpf_error(cstate
, "'vpi' supported only on raw ATM");
10150 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10152 if (jvalue
> UINT8_MAX
)
10153 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
10154 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10155 0xffffffffU
, jtype
, reverse
, jvalue
);
10159 if (!cstate
->is_atm
)
10160 bpf_error(cstate
, "'vci' supported only on raw ATM");
10161 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10163 if (jvalue
> UINT16_MAX
)
10164 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
10165 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10166 0xffffffffU
, jtype
, reverse
, jvalue
);
10170 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10171 abort(); /* XXX - this isn't on FreeBSD */
10172 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10173 0x0fU
, jtype
, reverse
, jvalue
);
10177 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10179 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10180 0xffffffffU
, jtype
, reverse
, jvalue
);
10183 case A_CALLREFTYPE
:
10184 if (!cstate
->is_atm
)
10185 bpf_error(cstate
, "'callref' supported only on raw ATM");
10186 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10188 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10189 0xffffffffU
, jtype
, reverse
, jvalue
);
10198 static struct block
*
10199 gen_atmtype_metac(compiler_state_t
*cstate
)
10201 struct block
*b0
, *b1
;
10203 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10204 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10209 static struct block
*
10210 gen_atmtype_sc(compiler_state_t
*cstate
)
10212 struct block
*b0
, *b1
;
10214 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10215 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10220 static struct block
*
10221 gen_atmtype_llc(compiler_state_t
*cstate
)
10225 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10226 cstate
->linktype
= cstate
->prevlinktype
;
10231 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10232 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10235 * Catch errors reported by us and routines below us, and return NULL
10238 if (setjmp(cstate
->top_ctx
))
10241 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10246 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10248 struct block
*b0
, *b1
;
10251 * Catch errors reported by us and routines below us, and return NULL
10254 if (setjmp(cstate
->top_ctx
))
10260 /* Get all packets in Meta signalling Circuit */
10261 if (!cstate
->is_atm
)
10262 bpf_error(cstate
, "'metac' supported only on raw ATM");
10263 b1
= gen_atmtype_metac(cstate
);
10267 /* Get all packets in Broadcast Circuit*/
10268 if (!cstate
->is_atm
)
10269 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10270 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10271 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10276 /* Get all cells in Segment OAM F4 circuit*/
10277 if (!cstate
->is_atm
)
10278 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10279 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10280 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10285 /* Get all cells in End-to-End OAM F4 Circuit*/
10286 if (!cstate
->is_atm
)
10287 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10288 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10289 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10294 /* Get all packets in connection Signalling Circuit */
10295 if (!cstate
->is_atm
)
10296 bpf_error(cstate
, "'sc' supported only on raw ATM");
10297 b1
= gen_atmtype_sc(cstate
);
10301 /* Get all packets in ILMI Circuit */
10302 if (!cstate
->is_atm
)
10303 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10304 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10305 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10310 /* Get all LANE packets */
10311 if (!cstate
->is_atm
)
10312 bpf_error(cstate
, "'lane' supported only on raw ATM");
10313 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10316 * Arrange that all subsequent tests assume LANE
10317 * rather than LLC-encapsulated packets, and set
10318 * the offsets appropriately for LANE-encapsulated
10321 * We assume LANE means Ethernet, not Token Ring.
10323 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10324 cstate
->off_payload
+ 2, /* Ethernet header */
10326 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10327 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10328 cstate
->off_nl
= 0; /* Ethernet II */
10329 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10333 /* Get all LLC-encapsulated packets */
10334 if (!cstate
->is_atm
)
10335 bpf_error(cstate
, "'llc' supported only on raw ATM");
10336 b1
= gen_atmtype_llc(cstate
);
10346 * Filtering for MTP2 messages based on li value
10347 * FISU, length is null
10348 * LSSU, length is 1 or 2
10349 * MSU, length is 3 or more
10350 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10353 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10355 struct block
*b0
, *b1
;
10358 * Catch errors reported by us and routines below us, and return NULL
10361 if (setjmp(cstate
->top_ctx
))
10367 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10368 (cstate
->linktype
!= DLT_ERF
) &&
10369 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10370 bpf_error(cstate
, "'fisu' supported only on MTP2");
10371 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10372 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10373 0x3fU
, BPF_JEQ
, 0, 0U);
10377 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10378 (cstate
->linktype
!= DLT_ERF
) &&
10379 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10380 bpf_error(cstate
, "'lssu' supported only on MTP2");
10381 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10382 0x3fU
, BPF_JGT
, 1, 2U);
10383 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10384 0x3fU
, BPF_JGT
, 0, 0U);
10389 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10390 (cstate
->linktype
!= DLT_ERF
) &&
10391 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10392 bpf_error(cstate
, "'msu' supported only on MTP2");
10393 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10394 0x3fU
, BPF_JGT
, 0, 2U);
10398 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10399 (cstate
->linktype
!= DLT_ERF
) &&
10400 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10401 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10402 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10403 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10404 0xff80U
, BPF_JEQ
, 0, 0U);
10408 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10409 (cstate
->linktype
!= DLT_ERF
) &&
10410 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10411 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10412 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10413 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10414 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10415 0xff80U
, BPF_JGT
, 0, 0U);
10420 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10421 (cstate
->linktype
!= DLT_ERF
) &&
10422 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10423 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10424 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10425 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10434 static struct block
*
10435 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10436 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10439 bpf_u_int32 val1
, val2
, val3
;
10445 newoff_sio
= cstate
->off_sio
;
10446 newoff_opc
= cstate
->off_opc
;
10447 newoff_dpc
= cstate
->off_dpc
;
10448 newoff_sls
= cstate
->off_sls
;
10449 switch (mtp3field
) {
10452 newoff_sio
+= 3; /* offset for MTP2_HSL */
10456 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10457 bpf_error(cstate
, "'sio' supported only on SS7");
10458 /* sio coded on 1 byte so max value 255 */
10460 bpf_error(cstate
, "sio value %u too big; max value = 255",
10462 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
10463 jtype
, reverse
, jvalue
);
10471 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10472 bpf_error(cstate
, "'opc' supported only on SS7");
10473 /* opc coded on 14 bits so max value 16383 */
10474 if (jvalue
> 16383)
10475 bpf_error(cstate
, "opc value %u too big; max value = 16383",
10477 /* the following instructions are made to convert jvalue
10478 * to the form used to write opc in an ss7 message*/
10479 val1
= jvalue
& 0x00003c00;
10481 val2
= jvalue
& 0x000003fc;
10483 val3
= jvalue
& 0x00000003;
10485 jvalue
= val1
+ val2
+ val3
;
10486 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
10487 jtype
, reverse
, jvalue
);
10495 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10496 bpf_error(cstate
, "'dpc' supported only on SS7");
10497 /* dpc coded on 14 bits so max value 16383 */
10498 if (jvalue
> 16383)
10499 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
10501 /* the following instructions are made to convert jvalue
10502 * to the forme used to write dpc in an ss7 message*/
10503 val1
= jvalue
& 0x000000ff;
10505 val2
= jvalue
& 0x00003f00;
10507 jvalue
= val1
+ val2
;
10508 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
10509 jtype
, reverse
, jvalue
);
10517 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10518 bpf_error(cstate
, "'sls' supported only on SS7");
10519 /* sls coded on 4 bits so max value 15 */
10521 bpf_error(cstate
, "sls value %u too big; max value = 15",
10523 /* the following instruction is made to convert jvalue
10524 * to the forme used to write sls in an ss7 message*/
10525 jvalue
= jvalue
<< 4;
10526 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
10527 jtype
, reverse
, jvalue
);
10537 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10538 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10541 * Catch errors reported by us and routines below us, and return NULL
10544 if (setjmp(cstate
->top_ctx
))
10547 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10551 static struct block
*
10552 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10557 * Q.2931 signalling protocol messages for handling virtual circuits
10558 * establishment and teardown
10563 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10566 case A_CALLPROCEED
:
10567 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10571 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10575 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10579 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10582 case A_RELEASE_DONE
:
10583 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10593 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10595 struct block
*b0
, *b1
;
10598 * Catch errors reported by us and routines below us, and return NULL
10601 if (setjmp(cstate
->top_ctx
))
10607 if (!cstate
->is_atm
)
10608 bpf_error(cstate
, "'oam' supported only on raw ATM");
10610 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10611 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10613 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10618 if (!cstate
->is_atm
)
10619 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10621 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10622 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10624 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10630 * Get Q.2931 signalling messages for switched
10631 * virtual connection
10633 if (!cstate
->is_atm
)
10634 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10635 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10636 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10638 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10640 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10642 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10644 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10646 b0
= gen_atmtype_sc(cstate
);
10650 case A_METACONNECT
:
10651 if (!cstate
->is_atm
)
10652 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10653 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10654 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10656 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10658 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10660 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10662 b0
= gen_atmtype_metac(cstate
);