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
264 // Same as in tcpdump/print-sl.c.
266 #define SLIPDIR_OUT 1
268 #ifdef HAVE_OS_PROTO_H
269 #include "os-proto.h"
272 #define JMP(c) ((c)|BPF_JMP|BPF_K)
275 * "Push" the current value of the link-layer header type and link-layer
276 * header offset onto a "stack", and set a new value. (It's not a
277 * full-blown stack; we keep only the top two items.)
279 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
281 (cs)->prevlinktype = (cs)->linktype; \
282 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
283 (cs)->linktype = (new_linktype); \
284 (cs)->off_linkhdr.is_variable = (new_is_variable); \
285 (cs)->off_linkhdr.constant_part = (new_constant_part); \
286 (cs)->off_linkhdr.reg = (new_reg); \
287 (cs)->is_encap = 0; \
291 * Offset "not set" value.
293 #define OFFSET_NOT_SET 0xffffffffU
296 * Absolute offsets, which are offsets from the beginning of the raw
297 * packet data, are, in the general case, the sum of a variable value
298 * and a constant value; the variable value may be absent, in which
299 * case the offset is only the constant value, and the constant value
300 * may be zero, in which case the offset is only the variable value.
302 * bpf_abs_offset is a structure containing all that information:
304 * is_variable is 1 if there's a variable part.
306 * constant_part is the constant part of the value, possibly zero;
308 * if is_variable is 1, reg is the register number for a register
309 * containing the variable value if the register has been assigned,
319 * Value passed to gen_load_a() to indicate what the offset argument
320 * is relative to the beginning of.
323 OR_PACKET
, /* full packet data */
324 OR_LINKHDR
, /* link-layer header */
325 OR_PREVLINKHDR
, /* previous link-layer header */
326 OR_LLC
, /* 802.2 LLC header */
327 OR_PREVMPLSHDR
, /* previous MPLS header */
328 OR_LINKTYPE
, /* link-layer type */
329 OR_LINKPL
, /* link-layer payload */
330 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
331 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
332 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
336 * We divvy out chunks of memory rather than call malloc each time so
337 * we don't have to worry about leaking memory. It's probably
338 * not a big deal if all this memory was wasted but if this ever
339 * goes into a library that would probably not be a good idea.
341 * XXX - this *is* in a library....
344 #define CHUNK0SIZE 1024
351 * A chunk can store any of:
352 * - a string (guaranteed alignment 1 but present for completeness)
356 * For this simple allocator every allocated chunk gets rounded up to the
357 * alignment needed for any chunk.
368 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
370 /* Code generator state */
372 struct _compiler_state
{
383 int outermostlinktype
;
388 /* Hack for handling VLAN and MPLS stacks. */
389 u_int label_stack_depth
;
390 u_int vlan_stack_depth
;
396 * As errors are handled by a longjmp, anything allocated must
397 * be freed in the longjmp handler, so it must be reachable
400 * One thing that's allocated is the result of pcap_nametoaddrinfo();
401 * it must be freed with freeaddrinfo(). This variable points to
402 * any addrinfo structure that would need to be freed.
407 * Another thing that's allocated is the result of pcap_ether_aton();
408 * it must be freed with free(). This variable points to any
409 * address that would need to be freed.
414 * Various code constructs need to know the layout of the packet.
415 * These values give the necessary offsets from the beginning
416 * of the packet data.
420 * Absolute offset of the beginning of the link-layer header.
422 bpf_abs_offset off_linkhdr
;
425 * If we're checking a link-layer header for a packet encapsulated
426 * in another protocol layer, this is the equivalent information
427 * for the previous layers' link-layer header from the beginning
428 * of the raw packet data.
430 bpf_abs_offset off_prevlinkhdr
;
433 * This is the equivalent information for the outermost layers'
436 bpf_abs_offset off_outermostlinkhdr
;
439 * Absolute offset of the beginning of the link-layer payload.
441 bpf_abs_offset off_linkpl
;
444 * "off_linktype" is the offset to information in the link-layer
445 * header giving the packet type. This is an absolute offset
446 * from the beginning of the packet.
448 * For Ethernet, it's the offset of the Ethernet type field; this
449 * means that it must have a value that skips VLAN tags.
451 * For link-layer types that always use 802.2 headers, it's the
452 * offset of the LLC header; this means that it must have a value
453 * that skips VLAN tags.
455 * For PPP, it's the offset of the PPP type field.
457 * For Cisco HDLC, it's the offset of the CHDLC type field.
459 * For BSD loopback, it's the offset of the AF_ value.
461 * For Linux cooked sockets, it's the offset of the type field.
463 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
464 * encapsulation, in which case, IP is assumed.
466 bpf_abs_offset off_linktype
;
469 * TRUE if the link layer includes an ATM pseudo-header.
473 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
474 * causes us to generate code that checks for a Geneve or
475 * VXLAN header respectively and assume that later filters
476 * apply to the encapsulated payload.
481 * TRUE if we need variable length part of VLAN offset
483 int is_vlan_vloffset
;
486 * These are offsets for the ATM pseudo-header.
493 * These are offsets for the MTP2 fields.
499 * These are offsets for the MTP3 fields.
507 * This is the offset of the first byte after the ATM pseudo_header,
508 * or -1 if there is no ATM pseudo-header.
513 * These are offsets to the beginning of the network-layer header.
514 * They are relative to the beginning of the link-layer payload
515 * (i.e., they don't include off_linkhdr.constant_part or
516 * off_linkpl.constant_part).
518 * If the link layer never uses 802.2 LLC:
520 * "off_nl" and "off_nl_nosnap" are the same.
522 * If the link layer always uses 802.2 LLC:
524 * "off_nl" is the offset if there's a SNAP header following
527 * "off_nl_nosnap" is the offset if there's no SNAP header.
529 * If the link layer is Ethernet:
531 * "off_nl" is the offset if the packet is an Ethernet II packet
532 * (we assume no 802.3+802.2+SNAP);
534 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
535 * with an 802.2 header following it.
541 * Here we handle simple allocation of the scratch registers.
542 * If too many registers are alloc'd, the allocator punts.
544 int regused
[BPF_MEMWORDS
];
550 struct chunk chunks
[NCHUNKS
];
555 * For use by routines outside this file.
559 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
564 * If we've already set an error, don't override it.
565 * The lexical analyzer reports some errors by setting
566 * the error and then returning a LEX_ERROR token, which
567 * is not recognized by any grammar rule, and thus forces
568 * the parse to stop. We don't want the error reported
569 * by the lexical analyzer to be overwritten by the syntax
572 if (!cstate
->error_set
) {
574 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
577 cstate
->error_set
= 1;
582 * For use *ONLY* in routines in this file.
584 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
585 PCAP_PRINTFLIKE(2, 3);
588 static void PCAP_NORETURN
589 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
594 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
597 longjmp(cstate
->top_ctx
, 1);
604 static int init_linktype(compiler_state_t
*, pcap_t
*);
606 static void init_regs(compiler_state_t
*);
607 static int alloc_reg(compiler_state_t
*);
608 static void free_reg(compiler_state_t
*, int);
610 static void initchunks(compiler_state_t
*cstate
);
611 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
612 static void *newchunk(compiler_state_t
*cstate
, size_t);
613 static void freechunks(compiler_state_t
*cstate
);
614 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
615 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
616 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
617 static inline void syntax(compiler_state_t
*cstate
);
619 static void backpatch(struct block
*, struct block
*);
620 static void merge(struct block
*, struct block
*);
621 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
623 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
625 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
627 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
629 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
631 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
632 u_int
, bpf_u_int32
, bpf_u_int32
);
633 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
634 u_int
, const u_char
*);
635 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
636 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
637 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
639 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
641 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
642 static struct block
*gen_uncond(compiler_state_t
*, int);
643 static inline struct block
*gen_true(compiler_state_t
*);
644 static inline struct block
*gen_false(compiler_state_t
*);
645 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
646 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
647 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
648 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
649 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
650 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
651 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
652 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
653 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
654 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
656 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
657 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
658 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
659 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
660 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
661 int, bpf_u_int32
, u_int
, u_int
);
663 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
664 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
666 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
667 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
668 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
669 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
670 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
671 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
672 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
673 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
674 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
677 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
678 struct in6_addr
*, int, int, int);
681 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
682 struct addrinfo
*, int, int);
684 static struct block
*gen_ipfrag(compiler_state_t
*);
685 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
686 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
688 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
689 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
691 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
692 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
693 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
695 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
696 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
697 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
698 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
700 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
701 static int lookup_proto(compiler_state_t
*, const char *, int);
702 #if !defined(NO_PROTOCHAIN)
703 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
704 #endif /* !defined(NO_PROTOCHAIN) */
705 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
706 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
707 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
708 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
709 static struct block
*gen_len(compiler_state_t
*, int, int);
710 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
711 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
713 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
714 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
715 bpf_u_int32
, int, int);
716 static struct block
*gen_atmtype_llc(compiler_state_t
*);
717 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
720 initchunks(compiler_state_t
*cstate
)
724 for (i
= 0; i
< NCHUNKS
; i
++) {
725 cstate
->chunks
[i
].n_left
= 0;
726 cstate
->chunks
[i
].m
= NULL
;
728 cstate
->cur_chunk
= 0;
732 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
738 /* Round up to chunk alignment. */
739 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
741 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
742 if (n
> cp
->n_left
) {
744 k
= ++cstate
->cur_chunk
;
746 bpf_set_error(cstate
, "out of memory");
749 size
= CHUNK0SIZE
<< k
;
750 cp
->m
= (void *)malloc(size
);
752 bpf_set_error(cstate
, "out of memory");
755 memset((char *)cp
->m
, 0, size
);
758 bpf_set_error(cstate
, "out of memory");
763 return (void *)((char *)cp
->m
+ cp
->n_left
);
767 newchunk(compiler_state_t
*cstate
, size_t n
)
771 p
= newchunk_nolongjmp(cstate
, n
);
773 longjmp(cstate
->top_ctx
, 1);
780 freechunks(compiler_state_t
*cstate
)
784 for (i
= 0; i
< NCHUNKS
; ++i
)
785 if (cstate
->chunks
[i
].m
!= NULL
)
786 free(cstate
->chunks
[i
].m
);
790 * A strdup whose allocations are freed after code generation is over.
791 * This is used by the lexical analyzer, so it can't longjmp; it just
792 * returns NULL on an allocation error, and the callers must check
796 sdup(compiler_state_t
*cstate
, const char *s
)
798 size_t n
= strlen(s
) + 1;
799 char *cp
= newchunk_nolongjmp(cstate
, n
);
803 pcapint_strlcpy(cp
, s
, n
);
807 static inline struct block
*
808 new_block(compiler_state_t
*cstate
, int code
)
812 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
819 static inline struct slist
*
820 new_stmt(compiler_state_t
*cstate
, int code
)
824 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
830 static struct block
*
831 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
833 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
839 static struct block
*
840 gen_retblk(compiler_state_t
*cstate
, int v
)
842 if (setjmp(cstate
->top_ctx
)) {
844 * gen_retblk() only fails because a memory
845 * allocation failed in newchunk(), meaning
846 * that it can't return a pointer.
852 return gen_retblk_internal(cstate
, v
);
855 static inline PCAP_NORETURN_DEF
void
856 syntax(compiler_state_t
*cstate
)
858 bpf_error(cstate
, "syntax error in filter expression");
862 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
863 const char *buf
, int optimize
, bpf_u_int32 mask
)
869 compiler_state_t cstate
;
870 yyscan_t scanner
= NULL
;
871 YY_BUFFER_STATE in_buffer
= NULL
;
876 * If this pcap_t hasn't been activated, it doesn't have a
877 * link-layer type, so we can't use it.
880 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
881 "not-yet-activated pcap_t passed to pcap_compile");
887 * Initialize Winsock, asking for the latest version (2.2),
888 * as we may be calling Winsock routines to translate
889 * host names to addresses.
891 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
893 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
894 err
, "Error calling WSAStartup()");
901 * If the device on which we're capturing need to be notified
902 * that a new filter is being compiled, do so.
904 * This allows them to save a copy of it, in case, for example,
905 * they're implementing a form of remote packet capture, and
906 * want the remote machine to filter out the packets in which
907 * it's sending the packets it's captured.
909 * XXX - the fact that we happen to be compiling a filter
910 * doesn't necessarily mean we'll be installing it as the
911 * filter for this pcap_t; we might be running it from userland
912 * on captured packets to do packet classification. We really
913 * need a better way of handling this, but this is all that
914 * the WinPcap remote capture code did.
916 if (p
->save_current_filter_op
!= NULL
)
917 (p
->save_current_filter_op
)(p
, buf
);
921 cstate
.no_optimize
= 0;
926 cstate
.ic
.root
= NULL
;
927 cstate
.ic
.cur_mark
= 0;
929 cstate
.error_set
= 0;
932 cstate
.netmask
= mask
;
934 cstate
.snaplen
= pcap_snapshot(p
);
935 if (cstate
.snaplen
== 0) {
936 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
937 "snaplen of 0 rejects all packets");
942 if (pcap_lex_init(&scanner
) != 0) {
943 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
944 errno
, "can't initialize scanner");
948 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
951 * Associate the compiler state with the lexical analyzer
954 pcap_set_extra(&cstate
, scanner
);
956 if (init_linktype(&cstate
, p
) == -1) {
960 if (pcap_parse(scanner
, &cstate
) != 0) {
962 if (cstate
.ai
!= NULL
)
963 freeaddrinfo(cstate
.ai
);
965 if (cstate
.e
!= NULL
)
971 if (cstate
.ic
.root
== NULL
) {
972 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
975 * Catch errors reported by gen_retblk().
977 if (cstate
.ic
.root
== NULL
) {
983 if (optimize
&& !cstate
.no_optimize
) {
984 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
989 if (cstate
.ic
.root
== NULL
||
990 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
991 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
992 "expression rejects all packets");
997 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
998 cstate
.ic
.root
, &len
, p
->errbuf
);
999 if (program
->bf_insns
== NULL
) {
1004 program
->bf_len
= len
;
1006 rc
= 0; /* We're all okay */
1010 * Clean up everything for the lexical analyzer.
1012 if (in_buffer
!= NULL
)
1013 pcap__delete_buffer(in_buffer
, scanner
);
1014 if (scanner
!= NULL
)
1015 pcap_lex_destroy(scanner
);
1018 * Clean up our own allocated memory.
1020 freechunks(&cstate
);
1030 * entry point for using the compiler with no pcap open
1031 * pass in all the stuff that is needed explicitly instead.
1034 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1035 struct bpf_program
*program
,
1036 const char *buf
, int optimize
, bpf_u_int32 mask
)
1041 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1043 return (PCAP_ERROR
);
1044 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1050 * Clean up a "struct bpf_program" by freeing all the memory allocated
1054 pcap_freecode(struct bpf_program
*program
)
1056 program
->bf_len
= 0;
1057 if (program
->bf_insns
!= NULL
) {
1058 free((char *)program
->bf_insns
);
1059 program
->bf_insns
= NULL
;
1064 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1065 * which of the jt and jf fields has been resolved and which is a pointer
1066 * back to another unresolved block (or nil). At least one of the fields
1067 * in each block is already resolved.
1070 backpatch(struct block
*list
, struct block
*target
)
1087 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1088 * which of jt and jf is the link.
1091 merge(struct block
*b0
, struct block
*b1
)
1093 register struct block
**p
= &b0
;
1095 /* Find end of list. */
1097 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1099 /* Concatenate the lists. */
1104 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1106 struct block
*ppi_dlt_check
;
1109 * Catch errors reported by us and routines below us, and return -1
1112 if (setjmp(cstate
->top_ctx
))
1116 * Insert before the statements of the first (root) block any
1117 * statements needed to load the lengths of any variable-length
1118 * headers into registers.
1120 * XXX - a fancier strategy would be to insert those before the
1121 * statements of all blocks that use those lengths and that
1122 * have no predecessors that use them, so that we only compute
1123 * the lengths if we need them. There might be even better
1124 * approaches than that.
1126 * However, those strategies would be more complicated, and
1127 * as we don't generate code to compute a length if the
1128 * program has no tests that use the length, and as most
1129 * tests will probably use those lengths, we would just
1130 * postpone computing the lengths so that it's not done
1131 * for tests that fail early, and it's not clear that's
1134 insert_compute_vloffsets(cstate
, p
->head
);
1137 * For DLT_PPI captures, generate a check of the per-packet
1138 * DLT value to make sure it's DLT_IEEE802_11.
1140 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1141 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1142 * with appropriate Ethernet information and use that rather
1143 * than using something such as DLT_PPI where you don't know
1144 * the link-layer header type until runtime, which, in the
1145 * general case, would force us to generate both Ethernet *and*
1146 * 802.11 code (*and* anything else for which PPI is used)
1147 * and choose between them early in the BPF program?
1149 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
1150 if (ppi_dlt_check
!= NULL
)
1151 gen_and(ppi_dlt_check
, p
);
1153 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1154 p
->sense
= !p
->sense
;
1155 backpatch(p
, gen_retblk_internal(cstate
, 0));
1156 cstate
->ic
.root
= p
->head
;
1161 gen_and(struct block
*b0
, struct block
*b1
)
1163 backpatch(b0
, b1
->head
);
1164 b0
->sense
= !b0
->sense
;
1165 b1
->sense
= !b1
->sense
;
1167 b1
->sense
= !b1
->sense
;
1168 b1
->head
= b0
->head
;
1172 gen_or(struct block
*b0
, struct block
*b1
)
1174 b0
->sense
= !b0
->sense
;
1175 backpatch(b0
, b1
->head
);
1176 b0
->sense
= !b0
->sense
;
1178 b1
->head
= b0
->head
;
1182 gen_not(struct block
*b
)
1184 b
->sense
= !b
->sense
;
1187 static struct block
*
1188 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1189 u_int size
, bpf_u_int32 v
)
1191 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1194 static struct block
*
1195 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1196 u_int size
, bpf_u_int32 v
)
1198 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1201 static struct block
*
1202 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1203 u_int size
, bpf_u_int32 v
)
1205 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1208 static struct block
*
1209 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1210 u_int size
, bpf_u_int32 v
)
1212 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1215 static struct block
*
1216 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1217 u_int size
, bpf_u_int32 v
)
1219 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1222 static struct block
*
1223 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1224 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1226 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1229 static struct block
*
1230 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1231 u_int size
, const u_char
*v
)
1233 register struct block
*b
, *tmp
;
1237 register const u_char
*p
= &v
[size
- 4];
1239 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1247 register const u_char
*p
= &v
[size
- 2];
1249 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1257 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1266 * AND the field of size "size" at offset "offset" relative to the header
1267 * specified by "offrel" with "mask", and compare it with the value "v"
1268 * with the test specified by "jtype"; if "reverse" is true, the test
1269 * should test the opposite of "jtype".
1271 static struct block
*
1272 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1273 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1276 struct slist
*s
, *s2
;
1279 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1281 if (mask
!= 0xffffffff) {
1282 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1287 b
= new_block(cstate
, JMP(jtype
));
1296 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1298 cstate
->pcap_fddipad
= p
->fddipad
;
1301 * We start out with only one link-layer header.
1303 cstate
->outermostlinktype
= pcap_datalink(p
);
1304 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1305 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1306 cstate
->off_outermostlinkhdr
.reg
= -1;
1308 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1309 cstate
->off_prevlinkhdr
.constant_part
= 0;
1310 cstate
->off_prevlinkhdr
.is_variable
= 0;
1311 cstate
->off_prevlinkhdr
.reg
= -1;
1313 cstate
->linktype
= cstate
->outermostlinktype
;
1314 cstate
->off_linkhdr
.constant_part
= 0;
1315 cstate
->off_linkhdr
.is_variable
= 0;
1316 cstate
->off_linkhdr
.reg
= -1;
1321 cstate
->off_linkpl
.constant_part
= 0;
1322 cstate
->off_linkpl
.is_variable
= 0;
1323 cstate
->off_linkpl
.reg
= -1;
1325 cstate
->off_linktype
.constant_part
= 0;
1326 cstate
->off_linktype
.is_variable
= 0;
1327 cstate
->off_linktype
.reg
= -1;
1330 * Assume it's not raw ATM with a pseudo-header, for now.
1333 cstate
->off_vpi
= OFFSET_NOT_SET
;
1334 cstate
->off_vci
= OFFSET_NOT_SET
;
1335 cstate
->off_proto
= OFFSET_NOT_SET
;
1336 cstate
->off_payload
= OFFSET_NOT_SET
;
1339 * And not encapsulated with either Geneve or VXLAN.
1341 cstate
->is_encap
= 0;
1344 * No variable length VLAN offset by default
1346 cstate
->is_vlan_vloffset
= 0;
1349 * And assume we're not doing SS7.
1351 cstate
->off_li
= OFFSET_NOT_SET
;
1352 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1353 cstate
->off_sio
= OFFSET_NOT_SET
;
1354 cstate
->off_opc
= OFFSET_NOT_SET
;
1355 cstate
->off_dpc
= OFFSET_NOT_SET
;
1356 cstate
->off_sls
= OFFSET_NOT_SET
;
1358 cstate
->label_stack_depth
= 0;
1359 cstate
->vlan_stack_depth
= 0;
1361 switch (cstate
->linktype
) {
1364 cstate
->off_linktype
.constant_part
= 2;
1365 cstate
->off_linkpl
.constant_part
= 6;
1366 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1367 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1370 case DLT_ARCNET_LINUX
:
1371 cstate
->off_linktype
.constant_part
= 4;
1372 cstate
->off_linkpl
.constant_part
= 8;
1373 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1374 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1378 cstate
->off_linktype
.constant_part
= 12;
1379 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1380 cstate
->off_nl
= 0; /* Ethernet II */
1381 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1386 * SLIP doesn't have a link level type. The 16 byte
1387 * header is hacked into our SLIP driver.
1389 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1390 cstate
->off_linkpl
.constant_part
= 16;
1392 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1395 case DLT_SLIP_BSDOS
:
1396 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1397 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1399 cstate
->off_linkpl
.constant_part
= 24;
1401 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1406 cstate
->off_linktype
.constant_part
= 0;
1407 cstate
->off_linkpl
.constant_part
= 4;
1409 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1413 cstate
->off_linktype
.constant_part
= 0;
1414 cstate
->off_linkpl
.constant_part
= 12;
1416 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1421 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1422 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1423 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1424 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1425 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1427 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1432 * This does no include the Ethernet header, and
1433 * only covers session state.
1435 cstate
->off_linktype
.constant_part
= 6;
1436 cstate
->off_linkpl
.constant_part
= 8;
1438 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1442 cstate
->off_linktype
.constant_part
= 5;
1443 cstate
->off_linkpl
.constant_part
= 24;
1445 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1450 * FDDI doesn't really have a link-level type field.
1451 * We set "off_linktype" to the offset of the LLC header.
1453 * To check for Ethernet types, we assume that SSAP = SNAP
1454 * is being used and pick out the encapsulated Ethernet type.
1455 * XXX - should we generate code to check for SNAP?
1457 cstate
->off_linktype
.constant_part
= 13;
1458 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1459 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1460 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1461 cstate
->off_nl
= 8; /* 802.2+SNAP */
1462 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1467 * Token Ring doesn't really have a link-level type field.
1468 * We set "off_linktype" to the offset of the LLC header.
1470 * To check for Ethernet types, we assume that SSAP = SNAP
1471 * is being used and pick out the encapsulated Ethernet type.
1472 * XXX - should we generate code to check for SNAP?
1474 * XXX - the header is actually variable-length.
1475 * Some various Linux patched versions gave 38
1476 * as "off_linktype" and 40 as "off_nl"; however,
1477 * if a token ring packet has *no* routing
1478 * information, i.e. is not source-routed, the correct
1479 * values are 20 and 22, as they are in the vanilla code.
1481 * A packet is source-routed iff the uppermost bit
1482 * of the first byte of the source address, at an
1483 * offset of 8, has the uppermost bit set. If the
1484 * packet is source-routed, the total number of bytes
1485 * of routing information is 2 plus bits 0x1F00 of
1486 * the 16-bit value at an offset of 14 (shifted right
1487 * 8 - figure out which byte that is).
1489 cstate
->off_linktype
.constant_part
= 14;
1490 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1491 cstate
->off_nl
= 8; /* 802.2+SNAP */
1492 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1495 case DLT_PRISM_HEADER
:
1496 case DLT_IEEE802_11_RADIO_AVS
:
1497 case DLT_IEEE802_11_RADIO
:
1498 cstate
->off_linkhdr
.is_variable
= 1;
1499 /* Fall through, 802.11 doesn't have a variable link
1500 * prefix but is otherwise the same. */
1503 case DLT_IEEE802_11
:
1505 * 802.11 doesn't really have a link-level type field.
1506 * We set "off_linktype.constant_part" to the offset of
1509 * To check for Ethernet types, we assume that SSAP = SNAP
1510 * is being used and pick out the encapsulated Ethernet type.
1511 * XXX - should we generate code to check for SNAP?
1513 * We also handle variable-length radio headers here.
1514 * The Prism header is in theory variable-length, but in
1515 * practice it's always 144 bytes long. However, some
1516 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1517 * sometimes or always supply an AVS header, so we
1518 * have to check whether the radio header is a Prism
1519 * header or an AVS header, so, in practice, it's
1522 cstate
->off_linktype
.constant_part
= 24;
1523 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1524 cstate
->off_linkpl
.is_variable
= 1;
1525 cstate
->off_nl
= 8; /* 802.2+SNAP */
1526 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1531 * At the moment we treat PPI the same way that we treat
1532 * normal Radiotap encoded packets. The difference is in
1533 * the function that generates the code at the beginning
1534 * to compute the header length. Since this code generator
1535 * of PPI supports bare 802.11 encapsulation only (i.e.
1536 * the encapsulated DLT should be DLT_IEEE802_11) we
1537 * generate code to check for this too.
1539 cstate
->off_linktype
.constant_part
= 24;
1540 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1541 cstate
->off_linkpl
.is_variable
= 1;
1542 cstate
->off_linkhdr
.is_variable
= 1;
1543 cstate
->off_nl
= 8; /* 802.2+SNAP */
1544 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1547 case DLT_ATM_RFC1483
:
1548 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1550 * assume routed, non-ISO PDUs
1551 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1553 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1554 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1555 * latter would presumably be treated the way PPPoE
1556 * should be, so you can do "pppoe and udp port 2049"
1557 * or "pppoa and tcp port 80" and have it check for
1558 * PPPo{A,E} and a PPP protocol of IP and....
1560 cstate
->off_linktype
.constant_part
= 0;
1561 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1562 cstate
->off_nl
= 8; /* 802.2+SNAP */
1563 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1568 * Full Frontal ATM; you get AALn PDUs with an ATM
1572 cstate
->off_vpi
= SUNATM_VPI_POS
;
1573 cstate
->off_vci
= SUNATM_VCI_POS
;
1574 cstate
->off_proto
= PROTO_POS
;
1575 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1576 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1577 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1578 cstate
->off_nl
= 8; /* 802.2+SNAP */
1579 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1585 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1586 cstate
->off_linkpl
.constant_part
= 0;
1588 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1591 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1592 cstate
->off_linktype
.constant_part
= 14;
1593 cstate
->off_linkpl
.constant_part
= 16;
1595 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1598 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1599 cstate
->off_linktype
.constant_part
= 0;
1600 cstate
->off_linkpl
.constant_part
= 20;
1602 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1607 * LocalTalk does have a 1-byte type field in the LLAP header,
1608 * but really it just indicates whether there is a "short" or
1609 * "long" DDP packet following.
1611 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1612 cstate
->off_linkpl
.constant_part
= 0;
1614 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1617 case DLT_IP_OVER_FC
:
1619 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1620 * link-level type field. We set "off_linktype" to the
1621 * offset of the LLC header.
1623 * To check for Ethernet types, we assume that SSAP = SNAP
1624 * is being used and pick out the encapsulated Ethernet type.
1625 * XXX - should we generate code to check for SNAP? RFC
1626 * 2625 says SNAP should be used.
1628 cstate
->off_linktype
.constant_part
= 16;
1629 cstate
->off_linkpl
.constant_part
= 16;
1630 cstate
->off_nl
= 8; /* 802.2+SNAP */
1631 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1636 * XXX - we should set this to handle SNAP-encapsulated
1637 * frames (NLPID of 0x80).
1639 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1640 cstate
->off_linkpl
.constant_part
= 0;
1642 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1646 * the only BPF-interesting FRF.16 frames are non-control frames;
1647 * Frame Relay has a variable length link-layer
1648 * so lets start with offset 4 for now and increments later on (FIXME);
1651 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1652 cstate
->off_linkpl
.constant_part
= 0;
1654 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1657 case DLT_APPLE_IP_OVER_IEEE1394
:
1658 cstate
->off_linktype
.constant_part
= 16;
1659 cstate
->off_linkpl
.constant_part
= 18;
1661 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1664 case DLT_SYMANTEC_FIREWALL
:
1665 cstate
->off_linktype
.constant_part
= 6;
1666 cstate
->off_linkpl
.constant_part
= 44;
1667 cstate
->off_nl
= 0; /* Ethernet II */
1668 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1672 cstate
->off_linktype
.constant_part
= 0;
1673 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1674 cstate
->off_linkpl
.is_variable
= 1;
1676 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1679 case DLT_JUNIPER_MFR
:
1680 case DLT_JUNIPER_MLFR
:
1681 case DLT_JUNIPER_MLPPP
:
1682 case DLT_JUNIPER_PPP
:
1683 case DLT_JUNIPER_CHDLC
:
1684 case DLT_JUNIPER_FRELAY
:
1685 cstate
->off_linktype
.constant_part
= 4;
1686 cstate
->off_linkpl
.constant_part
= 4;
1688 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1691 case DLT_JUNIPER_ATM1
:
1692 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1693 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1695 cstate
->off_nl_nosnap
= 10;
1698 case DLT_JUNIPER_ATM2
:
1699 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1700 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1702 cstate
->off_nl_nosnap
= 10;
1705 /* frames captured on a Juniper PPPoE service PIC
1706 * contain raw ethernet frames */
1707 case DLT_JUNIPER_PPPOE
:
1708 case DLT_JUNIPER_ETHER
:
1709 cstate
->off_linkpl
.constant_part
= 14;
1710 cstate
->off_linktype
.constant_part
= 16;
1711 cstate
->off_nl
= 18; /* Ethernet II */
1712 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1715 case DLT_JUNIPER_PPPOE_ATM
:
1716 cstate
->off_linktype
.constant_part
= 4;
1717 cstate
->off_linkpl
.constant_part
= 6;
1719 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1722 case DLT_JUNIPER_GGSN
:
1723 cstate
->off_linktype
.constant_part
= 6;
1724 cstate
->off_linkpl
.constant_part
= 12;
1726 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1729 case DLT_JUNIPER_ES
:
1730 cstate
->off_linktype
.constant_part
= 6;
1731 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1732 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1733 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1736 case DLT_JUNIPER_MONITOR
:
1737 cstate
->off_linktype
.constant_part
= 12;
1738 cstate
->off_linkpl
.constant_part
= 12;
1739 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1740 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1743 case DLT_BACNET_MS_TP
:
1744 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1745 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1746 cstate
->off_nl
= OFFSET_NOT_SET
;
1747 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1750 case DLT_JUNIPER_SERVICES
:
1751 cstate
->off_linktype
.constant_part
= 12;
1752 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1753 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1754 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1757 case DLT_JUNIPER_VP
:
1758 cstate
->off_linktype
.constant_part
= 18;
1759 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1760 cstate
->off_nl
= OFFSET_NOT_SET
;
1761 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1764 case DLT_JUNIPER_ST
:
1765 cstate
->off_linktype
.constant_part
= 18;
1766 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1767 cstate
->off_nl
= OFFSET_NOT_SET
;
1768 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1771 case DLT_JUNIPER_ISM
:
1772 cstate
->off_linktype
.constant_part
= 8;
1773 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1774 cstate
->off_nl
= OFFSET_NOT_SET
;
1775 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1778 case DLT_JUNIPER_VS
:
1779 case DLT_JUNIPER_SRX_E2E
:
1780 case DLT_JUNIPER_FIBRECHANNEL
:
1781 case DLT_JUNIPER_ATM_CEMIC
:
1782 cstate
->off_linktype
.constant_part
= 8;
1783 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1784 cstate
->off_nl
= OFFSET_NOT_SET
;
1785 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1790 cstate
->off_li_hsl
= 4;
1791 cstate
->off_sio
= 3;
1792 cstate
->off_opc
= 4;
1793 cstate
->off_dpc
= 4;
1794 cstate
->off_sls
= 7;
1795 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1796 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1797 cstate
->off_nl
= OFFSET_NOT_SET
;
1798 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1801 case DLT_MTP2_WITH_PHDR
:
1803 cstate
->off_li_hsl
= 8;
1804 cstate
->off_sio
= 7;
1805 cstate
->off_opc
= 8;
1806 cstate
->off_dpc
= 8;
1807 cstate
->off_sls
= 11;
1808 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1809 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1810 cstate
->off_nl
= OFFSET_NOT_SET
;
1811 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1815 cstate
->off_li
= 22;
1816 cstate
->off_li_hsl
= 24;
1817 cstate
->off_sio
= 23;
1818 cstate
->off_opc
= 24;
1819 cstate
->off_dpc
= 24;
1820 cstate
->off_sls
= 27;
1821 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1822 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1823 cstate
->off_nl
= OFFSET_NOT_SET
;
1824 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1828 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1829 cstate
->off_linkpl
.constant_part
= 4;
1831 cstate
->off_nl_nosnap
= 0;
1836 * Currently, only raw "link[N:M]" filtering is supported.
1838 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1839 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1840 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1841 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1845 cstate
->off_linktype
.constant_part
= 1;
1846 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1848 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1851 case DLT_NETANALYZER
:
1852 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1853 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1854 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1855 cstate
->off_nl
= 0; /* Ethernet II */
1856 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1859 case DLT_NETANALYZER_TRANSPARENT
:
1860 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1861 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1862 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1863 cstate
->off_nl
= 0; /* Ethernet II */
1864 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1869 * For values in the range in which we've assigned new
1870 * DLT_ values, only raw "link[N:M]" filtering is supported.
1872 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1873 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1874 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1875 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1876 cstate
->off_nl
= OFFSET_NOT_SET
;
1877 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1879 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1880 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1886 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1891 * Load a value relative to the specified absolute offset.
1893 static struct slist
*
1894 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1895 u_int offset
, u_int size
)
1897 struct slist
*s
, *s2
;
1899 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1902 * If "s" is non-null, it has code to arrange that the X register
1903 * contains the variable part of the absolute offset, so we
1904 * generate a load relative to that, with an offset of
1905 * abs_offset->constant_part + offset.
1907 * Otherwise, we can do an absolute load with an offset of
1908 * abs_offset->constant_part + offset.
1912 * "s" points to a list of statements that puts the
1913 * variable part of the absolute offset into the X register.
1914 * Do an indirect load, to use the X register as an offset.
1916 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1917 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1921 * There is no variable part of the absolute offset, so
1922 * just do an absolute load.
1924 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1925 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1931 * Load a value relative to the beginning of the specified header.
1933 static struct slist
*
1934 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1937 struct slist
*s
, *s2
;
1940 * Squelch warnings from compilers that *don't* assume that
1941 * offrel always has a valid enum value and therefore don't
1942 * assume that we'll always go through one of the case arms.
1944 * If we have a default case, compilers that *do* assume that
1945 * will then complain about the default case code being
1948 * Damned if you do, damned if you don't.
1955 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1960 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1963 case OR_PREVLINKHDR
:
1964 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1968 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1971 case OR_PREVMPLSHDR
:
1972 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1976 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1979 case OR_LINKPL_NOSNAP
:
1980 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1984 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1989 * Load the X register with the length of the IPv4 header
1990 * (plus the offset of the link-layer header, if it's
1991 * preceded by a variable-length header such as a radio
1992 * header), in bytes.
1994 s
= gen_loadx_iphdrlen(cstate
);
1997 * Load the item at {offset of the link-layer payload} +
1998 * {offset, relative to the start of the link-layer
1999 * payload, of the IPv4 header} + {length of the IPv4 header} +
2000 * {specified offset}.
2002 * If the offset of the link-layer payload is variable,
2003 * the variable part of that offset is included in the
2004 * value in the X register, and we include the constant
2005 * part in the offset of the load.
2007 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2008 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2013 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2020 * Generate code to load into the X register the sum of the length of
2021 * the IPv4 header and the variable part of the offset of the link-layer
2024 static struct slist
*
2025 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2027 struct slist
*s
, *s2
;
2029 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2032 * The offset of the link-layer payload has a variable
2033 * part. "s" points to a list of statements that put
2034 * the variable part of that offset into the X register.
2036 * The 4*([k]&0xf) addressing mode can't be used, as we
2037 * don't have a constant offset, so we have to load the
2038 * value in question into the A register and add to it
2039 * the value from the X register.
2041 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2042 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2044 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2047 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2052 * The A register now contains the length of the IP header.
2053 * We need to add to it the variable part of the offset of
2054 * the link-layer payload, which is still in the X
2055 * register, and move the result into the X register.
2057 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2058 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2061 * The offset of the link-layer payload is a constant,
2062 * so no code was generated to load the (nonexistent)
2063 * variable part of that offset.
2065 * This means we can use the 4*([k]&0xf) addressing
2066 * mode. Load the length of the IPv4 header, which
2067 * is at an offset of cstate->off_nl from the beginning of
2068 * the link-layer payload, and thus at an offset of
2069 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2070 * of the raw packet data, using that addressing mode.
2072 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2073 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2079 static struct block
*
2080 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2085 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2087 b
= new_block(cstate
, JMP(BPF_JEQ
));
2093 static inline struct block
*
2094 gen_true(compiler_state_t
*cstate
)
2096 return gen_uncond(cstate
, 1);
2099 static inline struct block
*
2100 gen_false(compiler_state_t
*cstate
)
2102 return gen_uncond(cstate
, 0);
2106 * Generate code to match a particular packet type.
2108 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2109 * value, if <= ETHERMTU. We use that to determine whether to
2110 * match the type/length field or to check the type/length field for
2111 * a value <= ETHERMTU to see whether it's a type field and then do
2112 * the appropriate test.
2114 static struct block
*
2115 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2117 struct block
*b0
, *b1
;
2123 case LLCSAP_NETBEUI
:
2125 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2126 * so we check the DSAP and SSAP.
2128 * LLCSAP_IP checks for IP-over-802.2, rather
2129 * than IP-over-Ethernet or IP-over-SNAP.
2131 * XXX - should we check both the DSAP and the
2132 * SSAP, like this, or should we check just the
2133 * DSAP, as we do for other types <= ETHERMTU
2134 * (i.e., other SAP values)?
2136 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2138 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2146 * Ethernet_II frames, which are Ethernet
2147 * frames with a frame type of ETHERTYPE_IPX;
2149 * Ethernet_802.3 frames, which are 802.3
2150 * frames (i.e., the type/length field is
2151 * a length field, <= ETHERMTU, rather than
2152 * a type field) with the first two bytes
2153 * after the Ethernet/802.3 header being
2156 * Ethernet_802.2 frames, which are 802.3
2157 * frames with an 802.2 LLC header and
2158 * with the IPX LSAP as the DSAP in the LLC
2161 * Ethernet_SNAP frames, which are 802.3
2162 * frames with an LLC header and a SNAP
2163 * header and with an OUI of 0x000000
2164 * (encapsulated Ethernet) and a protocol
2165 * ID of ETHERTYPE_IPX in the SNAP header.
2167 * XXX - should we generate the same code both
2168 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2172 * This generates code to check both for the
2173 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2175 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2176 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2180 * Now we add code to check for SNAP frames with
2181 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2183 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2187 * Now we generate code to check for 802.3
2188 * frames in general.
2190 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2194 * Now add the check for 802.3 frames before the
2195 * check for Ethernet_802.2 and Ethernet_802.3,
2196 * as those checks should only be done on 802.3
2197 * frames, not on Ethernet frames.
2202 * Now add the check for Ethernet_II frames, and
2203 * do that before checking for the other frame
2206 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2210 case ETHERTYPE_ATALK
:
2211 case ETHERTYPE_AARP
:
2213 * EtherTalk (AppleTalk protocols on Ethernet link
2214 * layer) may use 802.2 encapsulation.
2218 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2219 * we check for an Ethernet type field less than
2220 * 1500, which means it's an 802.3 length field.
2222 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2226 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2227 * SNAP packets with an organization code of
2228 * 0x080007 (Apple, for Appletalk) and a protocol
2229 * type of ETHERTYPE_ATALK (Appletalk).
2231 * 802.2-encapsulated ETHERTYPE_AARP packets are
2232 * SNAP packets with an organization code of
2233 * 0x000000 (encapsulated Ethernet) and a protocol
2234 * type of ETHERTYPE_AARP (Appletalk ARP).
2236 if (ll_proto
== ETHERTYPE_ATALK
)
2237 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2238 else /* ll_proto == ETHERTYPE_AARP */
2239 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2243 * Check for Ethernet encapsulation (Ethertalk
2244 * phase 1?); we just check for the Ethernet
2247 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2253 if (ll_proto
<= ETHERMTU
) {
2255 * This is an LLC SAP value, so the frames
2256 * that match would be 802.2 frames.
2257 * Check that the frame is an 802.2 frame
2258 * (i.e., that the length/type field is
2259 * a length field, <= ETHERMTU) and
2260 * then check the DSAP.
2262 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2264 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2269 * This is an Ethernet type, so compare
2270 * the length/type field with it (if
2271 * the frame is an 802.2 frame, the length
2272 * field will be <= ETHERMTU, and, as
2273 * "ll_proto" is > ETHERMTU, this test
2274 * will fail and the frame won't match,
2275 * which is what we want).
2277 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2282 static struct block
*
2283 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2286 * For DLT_NULL, the link-layer header is a 32-bit word
2287 * containing an AF_ value in *host* byte order, and for
2288 * DLT_ENC, the link-layer header begins with a 32-bit
2289 * word containing an AF_ value in host byte order.
2291 * In addition, if we're reading a saved capture file,
2292 * the host byte order in the capture may not be the
2293 * same as the host byte order on this machine.
2295 * For DLT_LOOP, the link-layer header is a 32-bit
2296 * word containing an AF_ value in *network* byte order.
2298 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2300 * The AF_ value is in host byte order, but the BPF
2301 * interpreter will convert it to network byte order.
2303 * If this is a save file, and it's from a machine
2304 * with the opposite byte order to ours, we byte-swap
2307 * Then we run it through "htonl()", and generate
2308 * code to compare against the result.
2310 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2311 ll_proto
= SWAPLONG(ll_proto
);
2312 ll_proto
= htonl(ll_proto
);
2314 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2318 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2319 * or IPv6 then we have an error.
2321 static struct block
*
2322 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2327 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2330 case ETHERTYPE_IPV6
:
2331 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2338 return gen_false(cstate
);
2342 * Generate code to match a particular packet type.
2344 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2345 * value, if <= ETHERMTU. We use that to determine whether to
2346 * match the type field or to check the type field for the special
2347 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2349 static struct block
*
2350 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2352 struct block
*b0
, *b1
;
2358 case LLCSAP_NETBEUI
:
2360 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2361 * so we check the DSAP and SSAP.
2363 * LLCSAP_IP checks for IP-over-802.2, rather
2364 * than IP-over-Ethernet or IP-over-SNAP.
2366 * XXX - should we check both the DSAP and the
2367 * SSAP, like this, or should we check just the
2368 * DSAP, as we do for other types <= ETHERMTU
2369 * (i.e., other SAP values)?
2371 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2372 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2378 * Ethernet_II frames, which are Ethernet
2379 * frames with a frame type of ETHERTYPE_IPX;
2381 * Ethernet_802.3 frames, which have a frame
2382 * type of LINUX_SLL_P_802_3;
2384 * Ethernet_802.2 frames, which are 802.3
2385 * frames with an 802.2 LLC header (i.e, have
2386 * a frame type of LINUX_SLL_P_802_2) and
2387 * with the IPX LSAP as the DSAP in the LLC
2390 * Ethernet_SNAP frames, which are 802.3
2391 * frames with an LLC header and a SNAP
2392 * header and with an OUI of 0x000000
2393 * (encapsulated Ethernet) and a protocol
2394 * ID of ETHERTYPE_IPX in the SNAP header.
2396 * First, do the checks on LINUX_SLL_P_802_2
2397 * frames; generate the check for either
2398 * Ethernet_802.2 or Ethernet_SNAP frames, and
2399 * then put a check for LINUX_SLL_P_802_2 frames
2402 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2403 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2405 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2409 * Now check for 802.3 frames and OR that with
2410 * the previous test.
2412 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2416 * Now add the check for Ethernet_II frames, and
2417 * do that before checking for the other frame
2420 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2424 case ETHERTYPE_ATALK
:
2425 case ETHERTYPE_AARP
:
2427 * EtherTalk (AppleTalk protocols on Ethernet link
2428 * layer) may use 802.2 encapsulation.
2432 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2433 * we check for the 802.2 protocol type in the
2434 * "Ethernet type" field.
2436 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2439 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2440 * SNAP packets with an organization code of
2441 * 0x080007 (Apple, for Appletalk) and a protocol
2442 * type of ETHERTYPE_ATALK (Appletalk).
2444 * 802.2-encapsulated ETHERTYPE_AARP packets are
2445 * SNAP packets with an organization code of
2446 * 0x000000 (encapsulated Ethernet) and a protocol
2447 * type of ETHERTYPE_AARP (Appletalk ARP).
2449 if (ll_proto
== ETHERTYPE_ATALK
)
2450 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2451 else /* ll_proto == ETHERTYPE_AARP */
2452 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2456 * Check for Ethernet encapsulation (Ethertalk
2457 * phase 1?); we just check for the Ethernet
2460 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2466 if (ll_proto
<= ETHERMTU
) {
2468 * This is an LLC SAP value, so the frames
2469 * that match would be 802.2 frames.
2470 * Check for the 802.2 protocol type
2471 * in the "Ethernet type" field, and
2472 * then check the DSAP.
2474 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2475 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2481 * This is an Ethernet type, so compare
2482 * the length/type field with it (if
2483 * the frame is an 802.2 frame, the length
2484 * field will be <= ETHERMTU, and, as
2485 * "ll_proto" is > ETHERMTU, this test
2486 * will fail and the frame won't match,
2487 * which is what we want).
2489 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2495 * Load a value relative to the beginning of the link-layer header after the
2498 static struct slist
*
2499 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2501 struct slist
*s1
, *s2
;
2504 * Generate code to load the length of the pflog header into
2505 * the register assigned to hold that length, if one has been
2506 * assigned. (If one hasn't been assigned, no code we've
2507 * generated uses that prefix, so we don't need to generate any
2510 if (cstate
->off_linkpl
.reg
!= -1) {
2512 * The length is in the first byte of the header.
2514 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2518 * Round it up to a multiple of 4.
2519 * Add 3, and clear the lower 2 bits.
2521 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2524 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2525 s2
->s
.k
= 0xfffffffc;
2529 * Now allocate a register to hold that value and store
2532 s2
= new_stmt(cstate
, BPF_ST
);
2533 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2537 * Now move it into the X register.
2539 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2547 static struct slist
*
2548 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2550 struct slist
*s1
, *s2
;
2551 struct slist
*sjeq_avs_cookie
;
2552 struct slist
*sjcommon
;
2555 * This code is not compatible with the optimizer, as
2556 * we are generating jmp instructions within a normal
2557 * slist of instructions
2559 cstate
->no_optimize
= 1;
2562 * Generate code to load the length of the radio header into
2563 * the register assigned to hold that length, if one has been
2564 * assigned. (If one hasn't been assigned, no code we've
2565 * generated uses that prefix, so we don't need to generate any
2568 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2569 * or always use the AVS header rather than the Prism header.
2570 * We load a 4-byte big-endian value at the beginning of the
2571 * raw packet data, and see whether, when masked with 0xFFFFF000,
2572 * it's equal to 0x80211000. If so, that indicates that it's
2573 * an AVS header (the masked-out bits are the version number).
2574 * Otherwise, it's a Prism header.
2576 * XXX - the Prism header is also, in theory, variable-length,
2577 * but no known software generates headers that aren't 144
2580 if (cstate
->off_linkhdr
.reg
!= -1) {
2584 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2588 * AND it with 0xFFFFF000.
2590 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2591 s2
->s
.k
= 0xFFFFF000;
2595 * Compare with 0x80211000.
2597 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2598 sjeq_avs_cookie
->s
.k
= 0x80211000;
2599 sappend(s1
, sjeq_avs_cookie
);
2604 * The 4 bytes at an offset of 4 from the beginning of
2605 * the AVS header are the length of the AVS header.
2606 * That field is big-endian.
2608 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2611 sjeq_avs_cookie
->s
.jt
= s2
;
2614 * Now jump to the code to allocate a register
2615 * into which to save the header length and
2616 * store the length there. (The "jump always"
2617 * instruction needs to have the k field set;
2618 * it's added to the PC, so, as we're jumping
2619 * over a single instruction, it should be 1.)
2621 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2623 sappend(s1
, sjcommon
);
2626 * Now for the code that handles the Prism header.
2627 * Just load the length of the Prism header (144)
2628 * into the A register. Have the test for an AVS
2629 * header branch here if we don't have an AVS header.
2631 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2634 sjeq_avs_cookie
->s
.jf
= s2
;
2637 * Now allocate a register to hold that value and store
2638 * it. The code for the AVS header will jump here after
2639 * loading the length of the AVS header.
2641 s2
= new_stmt(cstate
, BPF_ST
);
2642 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2644 sjcommon
->s
.jf
= s2
;
2647 * Now move it into the X register.
2649 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2657 static struct slist
*
2658 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2660 struct slist
*s1
, *s2
;
2663 * Generate code to load the length of the AVS header into
2664 * the register assigned to hold that length, if one has been
2665 * assigned. (If one hasn't been assigned, no code we've
2666 * generated uses that prefix, so we don't need to generate any
2669 if (cstate
->off_linkhdr
.reg
!= -1) {
2671 * The 4 bytes at an offset of 4 from the beginning of
2672 * the AVS header are the length of the AVS header.
2673 * That field is big-endian.
2675 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2679 * Now allocate a register to hold that value and store
2682 s2
= new_stmt(cstate
, BPF_ST
);
2683 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2687 * Now move it into the X register.
2689 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2697 static struct slist
*
2698 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2700 struct slist
*s1
, *s2
;
2703 * Generate code to load the length of the radiotap header into
2704 * the register assigned to hold that length, if one has been
2705 * assigned. (If one hasn't been assigned, no code we've
2706 * generated uses that prefix, so we don't need to generate any
2709 if (cstate
->off_linkhdr
.reg
!= -1) {
2711 * The 2 bytes at offsets of 2 and 3 from the beginning
2712 * of the radiotap header are the length of the radiotap
2713 * header; unfortunately, it's little-endian, so we have
2714 * to load it a byte at a time and construct the value.
2718 * Load the high-order byte, at an offset of 3, shift it
2719 * left a byte, and put the result in the X register.
2721 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2723 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2726 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2730 * Load the next byte, at an offset of 2, and OR the
2731 * value from the X register into it.
2733 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2736 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2740 * Now allocate a register to hold that value and store
2743 s2
= new_stmt(cstate
, BPF_ST
);
2744 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2748 * Now move it into the X register.
2750 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2759 * At the moment we treat PPI as normal Radiotap encoded
2760 * packets. The difference is in the function that generates
2761 * the code at the beginning to compute the header length.
2762 * Since this code generator of PPI supports bare 802.11
2763 * encapsulation only (i.e. the encapsulated DLT should be
2764 * DLT_IEEE802_11) we generate code to check for this too;
2765 * that's done in finish_parse().
2767 static struct slist
*
2768 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2770 struct slist
*s1
, *s2
;
2773 * Generate code to load the length of the radiotap header
2774 * into the register assigned to hold that length, if one has
2777 if (cstate
->off_linkhdr
.reg
!= -1) {
2779 * The 2 bytes at offsets of 2 and 3 from the beginning
2780 * of the radiotap header are the length of the radiotap
2781 * header; unfortunately, it's little-endian, so we have
2782 * to load it a byte at a time and construct the value.
2786 * Load the high-order byte, at an offset of 3, shift it
2787 * left a byte, and put the result in the X register.
2789 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2791 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2794 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2798 * Load the next byte, at an offset of 2, and OR the
2799 * value from the X register into it.
2801 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2804 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2808 * Now allocate a register to hold that value and store
2811 s2
= new_stmt(cstate
, BPF_ST
);
2812 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2816 * Now move it into the X register.
2818 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2827 * Load a value relative to the beginning of the link-layer header after the 802.11
2828 * header, i.e. LLC_SNAP.
2829 * The link-layer header doesn't necessarily begin at the beginning
2830 * of the packet data; there might be a variable-length prefix containing
2831 * radio information.
2833 static struct slist
*
2834 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2837 struct slist
*sjset_data_frame_1
;
2838 struct slist
*sjset_data_frame_2
;
2839 struct slist
*sjset_qos
;
2840 struct slist
*sjset_radiotap_flags_present
;
2841 struct slist
*sjset_radiotap_ext_present
;
2842 struct slist
*sjset_radiotap_tsft_present
;
2843 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2844 struct slist
*s_roundup
;
2846 if (cstate
->off_linkpl
.reg
== -1) {
2848 * No register has been assigned to the offset of
2849 * the link-layer payload, which means nobody needs
2850 * it; don't bother computing it - just return
2851 * what we already have.
2857 * This code is not compatible with the optimizer, as
2858 * we are generating jmp instructions within a normal
2859 * slist of instructions
2861 cstate
->no_optimize
= 1;
2864 * If "s" is non-null, it has code to arrange that the X register
2865 * contains the length of the prefix preceding the link-layer
2868 * Otherwise, the length of the prefix preceding the link-layer
2869 * header is "off_outermostlinkhdr.constant_part".
2873 * There is no variable-length header preceding the
2874 * link-layer header.
2876 * Load the length of the fixed-length prefix preceding
2877 * the link-layer header (if any) into the X register,
2878 * and store it in the cstate->off_linkpl.reg register.
2879 * That length is off_outermostlinkhdr.constant_part.
2881 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2882 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2886 * The X register contains the offset of the beginning of the
2887 * link-layer header; add 24, which is the minimum length
2888 * of the MAC header for a data frame, to that, and store it
2889 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2890 * which is at the offset in the X register, with an indexed load.
2892 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2894 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2897 s2
= new_stmt(cstate
, BPF_ST
);
2898 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2901 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2906 * Check the Frame Control field to see if this is a data frame;
2907 * a data frame has the 0x08 bit (b3) in that field set and the
2908 * 0x04 bit (b2) clear.
2910 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2911 sjset_data_frame_1
->s
.k
= 0x08;
2912 sappend(s
, sjset_data_frame_1
);
2915 * If b3 is set, test b2, otherwise go to the first statement of
2916 * the rest of the program.
2918 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2919 sjset_data_frame_2
->s
.k
= 0x04;
2920 sappend(s
, sjset_data_frame_2
);
2921 sjset_data_frame_1
->s
.jf
= snext
;
2924 * If b2 is not set, this is a data frame; test the QoS bit.
2925 * Otherwise, go to the first statement of the rest of the
2928 sjset_data_frame_2
->s
.jt
= snext
;
2929 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2930 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2931 sappend(s
, sjset_qos
);
2934 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2936 * Otherwise, go to the first statement of the rest of the
2939 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2940 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2942 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2945 s2
= new_stmt(cstate
, BPF_ST
);
2946 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2950 * If we have a radiotap header, look at it to see whether
2951 * there's Atheros padding between the MAC-layer header
2954 * Note: all of the fields in the radiotap header are
2955 * little-endian, so we byte-swap all of the values
2956 * we test against, as they will be loaded as big-endian
2959 * XXX - in the general case, we would have to scan through
2960 * *all* the presence bits, if there's more than one word of
2961 * presence bits. That would require a loop, meaning that
2962 * we wouldn't be able to run the filter in the kernel.
2964 * We assume here that the Atheros adapters that insert the
2965 * annoying padding don't have multiple antennae and therefore
2966 * do not generate radiotap headers with multiple presence words.
2968 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2970 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2971 * in the first presence flag word?
2973 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2977 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2978 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2979 sappend(s
, sjset_radiotap_flags_present
);
2982 * If not, skip all of this.
2984 sjset_radiotap_flags_present
->s
.jf
= snext
;
2987 * Otherwise, is the "extension" bit set in that word?
2989 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2990 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2991 sappend(s
, sjset_radiotap_ext_present
);
2992 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2995 * If so, skip all of this.
2997 sjset_radiotap_ext_present
->s
.jt
= snext
;
3000 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3002 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3003 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3004 sappend(s
, sjset_radiotap_tsft_present
);
3005 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3008 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3009 * at an offset of 16 from the beginning of the raw packet
3010 * data (8 bytes for the radiotap header and 8 bytes for
3013 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3016 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3019 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3021 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3022 sjset_tsft_datapad
->s
.k
= 0x20;
3023 sappend(s
, sjset_tsft_datapad
);
3026 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3027 * at an offset of 8 from the beginning of the raw packet
3028 * data (8 bytes for the radiotap header).
3030 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3033 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3036 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3038 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3039 sjset_notsft_datapad
->s
.k
= 0x20;
3040 sappend(s
, sjset_notsft_datapad
);
3043 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3044 * set, round the length of the 802.11 header to
3045 * a multiple of 4. Do that by adding 3 and then
3046 * dividing by and multiplying by 4, which we do by
3049 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3050 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3051 sappend(s
, s_roundup
);
3052 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3055 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3056 s2
->s
.k
= (bpf_u_int32
)~3;
3058 s2
= new_stmt(cstate
, BPF_ST
);
3059 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3062 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3063 sjset_tsft_datapad
->s
.jf
= snext
;
3064 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3065 sjset_notsft_datapad
->s
.jf
= snext
;
3067 sjset_qos
->s
.jf
= snext
;
3073 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3077 /* There is an implicit dependency between the link
3078 * payload and link header since the payload computation
3079 * includes the variable part of the header. Therefore,
3080 * if nobody else has allocated a register for the link
3081 * header and we need it, do it now. */
3082 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3083 cstate
->off_linkhdr
.reg
== -1)
3084 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3087 * For link-layer types that have a variable-length header
3088 * preceding the link-layer header, generate code to load
3089 * the offset of the link-layer header into the register
3090 * assigned to that offset, if any.
3092 * XXX - this, and the next switch statement, won't handle
3093 * encapsulation of 802.11 or 802.11+radio information in
3094 * some other protocol stack. That's significantly more
3097 switch (cstate
->outermostlinktype
) {
3099 case DLT_PRISM_HEADER
:
3100 s
= gen_load_prism_llprefixlen(cstate
);
3103 case DLT_IEEE802_11_RADIO_AVS
:
3104 s
= gen_load_avs_llprefixlen(cstate
);
3107 case DLT_IEEE802_11_RADIO
:
3108 s
= gen_load_radiotap_llprefixlen(cstate
);
3112 s
= gen_load_ppi_llprefixlen(cstate
);
3121 * For link-layer types that have a variable-length link-layer
3122 * header, generate code to load the offset of the link-layer
3123 * payload into the register assigned to that offset, if any.
3125 switch (cstate
->outermostlinktype
) {
3127 case DLT_IEEE802_11
:
3128 case DLT_PRISM_HEADER
:
3129 case DLT_IEEE802_11_RADIO_AVS
:
3130 case DLT_IEEE802_11_RADIO
:
3132 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3136 s
= gen_load_pflog_llprefixlen(cstate
);
3141 * If there is no initialization yet and we need variable
3142 * length offsets for VLAN, initialize them to zero
3144 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3147 if (cstate
->off_linkpl
.reg
== -1)
3148 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3149 if (cstate
->off_linktype
.reg
== -1)
3150 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3152 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3154 s2
= new_stmt(cstate
, BPF_ST
);
3155 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3157 s2
= new_stmt(cstate
, BPF_ST
);
3158 s2
->s
.k
= cstate
->off_linktype
.reg
;
3163 * If we have any offset-loading code, append all the
3164 * existing statements in the block to those statements,
3165 * and make the resulting list the list of statements
3169 sappend(s
, b
->stmts
);
3174 static struct block
*
3175 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3177 struct slist
*s_load_dlt
;
3180 if (cstate
->linktype
== DLT_PPI
)
3182 /* Create the statements that check for the DLT
3184 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3185 s_load_dlt
->s
.k
= 4;
3187 b
= new_block(cstate
, JMP(BPF_JEQ
));
3189 b
->stmts
= s_load_dlt
;
3190 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3201 * Take an absolute offset, and:
3203 * if it has no variable part, return NULL;
3205 * if it has a variable part, generate code to load the register
3206 * containing that variable part into the X register, returning
3207 * a pointer to that code - if no register for that offset has
3208 * been allocated, allocate it first.
3210 * (The code to set that register will be generated later, but will
3211 * be placed earlier in the code sequence.)
3213 static struct slist
*
3214 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3218 if (off
->is_variable
) {
3219 if (off
->reg
== -1) {
3221 * We haven't yet assigned a register for the
3222 * variable part of the offset of the link-layer
3223 * header; allocate one.
3225 off
->reg
= alloc_reg(cstate
);
3229 * Load the register containing the variable part of the
3230 * offset of the link-layer header into the X register.
3232 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3237 * That offset isn't variable, there's no variable part,
3238 * so we don't need to generate any code.
3245 * Map an Ethernet type to the equivalent PPP type.
3248 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3256 case ETHERTYPE_IPV6
:
3257 ll_proto
= PPP_IPV6
;
3261 ll_proto
= PPP_DECNET
;
3264 case ETHERTYPE_ATALK
:
3265 ll_proto
= PPP_APPLE
;
3278 * I'm assuming the "Bridging PDU"s that go
3279 * over PPP are Spanning Tree Protocol
3282 ll_proto
= PPP_BRPDU
;
3293 * Generate any tests that, for encapsulation of a link-layer packet
3294 * inside another protocol stack, need to be done to check for those
3295 * link-layer packets (and that haven't already been done by a check
3296 * for that encapsulation).
3298 static struct block
*
3299 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3303 if (cstate
->is_encap
)
3304 return gen_encap_ll_check(cstate
);
3306 switch (cstate
->prevlinktype
) {
3310 * This is LANE-encapsulated Ethernet; check that the LANE
3311 * packet doesn't begin with an LE Control marker, i.e.
3312 * that it's data, not a control message.
3314 * (We've already generated a test for LANE.)
3316 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3322 * No such tests are necessary.
3330 * The three different values we should check for when checking for an
3331 * IPv6 packet with DLT_NULL.
3333 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3334 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3335 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3338 * Generate code to match a particular packet type by matching the
3339 * link-layer type field or fields in the 802.2 LLC header.
3341 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3342 * value, if <= ETHERMTU.
3344 static struct block
*
3345 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3347 struct block
*b0
, *b1
, *b2
;
3348 const char *description
;
3350 /* are we checking MPLS-encapsulated packets? */
3351 if (cstate
->label_stack_depth
> 0)
3352 return gen_mpls_linktype(cstate
, ll_proto
);
3354 switch (cstate
->linktype
) {
3357 case DLT_NETANALYZER
:
3358 case DLT_NETANALYZER_TRANSPARENT
:
3359 /* Geneve has an EtherType regardless of whether there is an
3360 * L2 header. VXLAN always has an EtherType. */
3361 if (!cstate
->is_encap
)
3362 b0
= gen_prevlinkhdr_check(cstate
);
3366 b1
= gen_ether_linktype(cstate
, ll_proto
);
3377 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3381 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3385 case DLT_IEEE802_11
:
3386 case DLT_PRISM_HEADER
:
3387 case DLT_IEEE802_11_RADIO_AVS
:
3388 case DLT_IEEE802_11_RADIO
:
3391 * Check that we have a data frame.
3393 b0
= gen_check_802_11_data_frame(cstate
);
3396 * Now check for the specified link-layer type.
3398 b1
= gen_llc_linktype(cstate
, ll_proto
);
3405 * XXX - check for LLC frames.
3407 return gen_llc_linktype(cstate
, ll_proto
);
3412 * XXX - check for LLC PDUs, as per IEEE 802.5.
3414 return gen_llc_linktype(cstate
, ll_proto
);
3417 case DLT_ATM_RFC1483
:
3419 case DLT_IP_OVER_FC
:
3420 return gen_llc_linktype(cstate
, ll_proto
);
3425 * Check for an LLC-encapsulated version of this protocol;
3426 * if we were checking for LANE, linktype would no longer
3429 * Check for LLC encapsulation and then check the protocol.
3431 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3432 b1
= gen_llc_linktype(cstate
, ll_proto
);
3438 return gen_linux_sll_linktype(cstate
, ll_proto
);
3442 case DLT_SLIP_BSDOS
:
3445 * These types don't provide any type field; packets
3446 * are always IPv4 or IPv6.
3448 * XXX - for IPv4, check for a version number of 4, and,
3449 * for IPv6, check for a version number of 6?
3454 /* Check for a version number of 4. */
3455 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3457 case ETHERTYPE_IPV6
:
3458 /* Check for a version number of 6. */
3459 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3462 return gen_false(cstate
); /* always false */
3468 * Raw IPv4, so no type field.
3470 if (ll_proto
== ETHERTYPE_IP
)
3471 return gen_true(cstate
); /* always true */
3473 /* Checking for something other than IPv4; always false */
3474 return gen_false(cstate
);
3479 * Raw IPv6, so no type field.
3481 if (ll_proto
== ETHERTYPE_IPV6
)
3482 return gen_true(cstate
); /* always true */
3484 /* Checking for something other than IPv6; always false */
3485 return gen_false(cstate
);
3490 case DLT_PPP_SERIAL
:
3493 * We use Ethernet protocol types inside libpcap;
3494 * map them to the corresponding PPP protocol types.
3496 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3497 ethertype_to_ppptype(ll_proto
));
3502 * We use Ethernet protocol types inside libpcap;
3503 * map them to the corresponding PPP protocol types.
3509 * Also check for Van Jacobson-compressed IP.
3510 * XXX - do this for other forms of PPP?
3512 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3513 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3515 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3520 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3521 ethertype_to_ppptype(ll_proto
));
3531 return (gen_loopback_linktype(cstate
, AF_INET
));
3533 case ETHERTYPE_IPV6
:
3535 * AF_ values may, unfortunately, be platform-
3536 * dependent; AF_INET isn't, because everybody
3537 * used 4.2BSD's value, but AF_INET6 is, because
3538 * 4.2BSD didn't have a value for it (given that
3539 * IPv6 didn't exist back in the early 1980's),
3540 * and they all picked their own values.
3542 * This means that, if we're reading from a
3543 * savefile, we need to check for all the
3546 * If we're doing a live capture, we only need
3547 * to check for this platform's value; however,
3548 * Npcap uses 24, which isn't Windows's AF_INET6
3549 * value. (Given the multiple different values,
3550 * programs that read pcap files shouldn't be
3551 * checking for their platform's AF_INET6 value
3552 * anyway, they should check for all of the
3553 * possible values. and they might as well do
3554 * that even for live captures.)
3556 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3558 * Savefile - check for all three
3559 * possible IPv6 values.
3561 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3562 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3564 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3569 * Live capture, so we only need to
3570 * check for the value used on this
3575 * Npcap doesn't use Windows's AF_INET6,
3576 * as that collides with AF_IPX on
3577 * some BSDs (both have the value 23).
3578 * Instead, it uses 24.
3580 return (gen_loopback_linktype(cstate
, 24));
3583 return (gen_loopback_linktype(cstate
, AF_INET6
));
3584 #else /* AF_INET6 */
3586 * I guess this platform doesn't support
3587 * IPv6, so we just reject all packets.
3589 return gen_false(cstate
);
3590 #endif /* AF_INET6 */
3596 * Not a type on which we support filtering.
3597 * XXX - support those that have AF_ values
3598 * #defined on this platform, at least?
3600 return gen_false(cstate
);
3605 * af field is host byte order in contrast to the rest of
3608 if (ll_proto
== ETHERTYPE_IP
)
3609 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3611 else if (ll_proto
== ETHERTYPE_IPV6
)
3612 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3615 return gen_false(cstate
);
3619 case DLT_ARCNET_LINUX
:
3621 * XXX should we check for first fragment if the protocol
3627 return gen_false(cstate
);
3629 case ETHERTYPE_IPV6
:
3630 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3634 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3636 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3642 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3644 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3649 case ETHERTYPE_REVARP
:
3650 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3653 case ETHERTYPE_ATALK
:
3654 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3661 case ETHERTYPE_ATALK
:
3662 return gen_true(cstate
);
3664 return gen_false(cstate
);
3670 * XXX - assumes a 2-byte Frame Relay header with
3671 * DLCI and flags. What if the address is longer?
3677 * Check for the special NLPID for IP.
3679 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3681 case ETHERTYPE_IPV6
:
3683 * Check for the special NLPID for IPv6.
3685 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3689 * Check for several OSI protocols.
3691 * Frame Relay packets typically have an OSI
3692 * NLPID at the beginning; we check for each
3695 * What we check for is the NLPID and a frame
3696 * control field of UI, i.e. 0x03 followed
3699 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3700 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3701 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3707 return gen_false(cstate
);
3712 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3714 case DLT_JUNIPER_MFR
:
3715 case DLT_JUNIPER_MLFR
:
3716 case DLT_JUNIPER_MLPPP
:
3717 case DLT_JUNIPER_ATM1
:
3718 case DLT_JUNIPER_ATM2
:
3719 case DLT_JUNIPER_PPPOE
:
3720 case DLT_JUNIPER_PPPOE_ATM
:
3721 case DLT_JUNIPER_GGSN
:
3722 case DLT_JUNIPER_ES
:
3723 case DLT_JUNIPER_MONITOR
:
3724 case DLT_JUNIPER_SERVICES
:
3725 case DLT_JUNIPER_ETHER
:
3726 case DLT_JUNIPER_PPP
:
3727 case DLT_JUNIPER_FRELAY
:
3728 case DLT_JUNIPER_CHDLC
:
3729 case DLT_JUNIPER_VP
:
3730 case DLT_JUNIPER_ST
:
3731 case DLT_JUNIPER_ISM
:
3732 case DLT_JUNIPER_VS
:
3733 case DLT_JUNIPER_SRX_E2E
:
3734 case DLT_JUNIPER_FIBRECHANNEL
:
3735 case DLT_JUNIPER_ATM_CEMIC
:
3737 /* just lets verify the magic number for now -
3738 * on ATM we may have up to 6 different encapsulations on the wire
3739 * and need a lot of heuristics to figure out that the payload
3742 * FIXME encapsulation specific BPF_ filters
3744 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3746 case DLT_BACNET_MS_TP
:
3747 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3750 return gen_ipnet_linktype(cstate
, ll_proto
);
3752 case DLT_LINUX_IRDA
:
3753 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3756 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3759 case DLT_MTP2_WITH_PHDR
:
3760 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3763 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3766 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3768 case DLT_LINUX_LAPD
:
3769 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3771 case DLT_USB_FREEBSD
:
3773 case DLT_USB_LINUX_MMAPPED
:
3775 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3777 case DLT_BLUETOOTH_HCI_H4
:
3778 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3779 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3782 case DLT_CAN_SOCKETCAN
:
3783 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3785 case DLT_IEEE802_15_4
:
3786 case DLT_IEEE802_15_4_LINUX
:
3787 case DLT_IEEE802_15_4_NONASK_PHY
:
3788 case DLT_IEEE802_15_4_NOFCS
:
3789 case DLT_IEEE802_15_4_TAP
:
3790 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3792 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3793 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3796 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3799 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3801 case DLT_IPMB_KONTRON
:
3802 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3805 bpf_error(cstate
, "I2C link-layer type filtering not implemented");
3808 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3811 /* Using the fixed-size NFLOG header it is possible to tell only
3812 * the address family of the packet, other meaningful data is
3813 * either missing or behind TLVs.
3815 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3819 * Does this link-layer header type have a field
3820 * indicating the type of the next protocol? If
3821 * so, off_linktype.constant_part will be the offset of that
3822 * field in the packet; if not, it will be OFFSET_NOT_SET.
3824 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3826 * Yes; assume it's an Ethernet type. (If
3827 * it's not, it needs to be handled specially
3830 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3834 * No; report an error.
3836 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3837 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3845 * Check for an LLC SNAP packet with a given organization code and
3846 * protocol type; we check the entire contents of the 802.2 LLC and
3847 * snap headers, checking for DSAP and SSAP of SNAP and a control
3848 * field of 0x03 in the LLC header, and for the specified organization
3849 * code and protocol type in the SNAP header.
3851 static struct block
*
3852 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3854 u_char snapblock
[8];
3856 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3857 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3858 snapblock
[2] = 0x03; /* control = UI */
3859 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3860 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3861 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3862 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3863 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3864 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3868 * Generate code to match frames with an LLC header.
3870 static struct block
*
3871 gen_llc_internal(compiler_state_t
*cstate
)
3873 struct block
*b0
, *b1
;
3875 switch (cstate
->linktype
) {
3879 * We check for an Ethernet type field less than
3880 * 1500, which means it's an 802.3 length field.
3882 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3886 * Now check for the purported DSAP and SSAP not being
3887 * 0xFF, to rule out NetWare-over-802.3.
3889 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3896 * We check for LLC traffic.
3898 b0
= gen_atmtype_llc(cstate
);
3901 case DLT_IEEE802
: /* Token Ring */
3903 * XXX - check for LLC frames.
3905 return gen_true(cstate
);
3909 * XXX - check for LLC frames.
3911 return gen_true(cstate
);
3913 case DLT_ATM_RFC1483
:
3915 * For LLC encapsulation, these are defined to have an
3918 * For VC encapsulation, they don't, but there's no
3919 * way to check for that; the protocol used on the VC
3920 * is negotiated out of band.
3922 return gen_true(cstate
);
3924 case DLT_IEEE802_11
:
3925 case DLT_PRISM_HEADER
:
3926 case DLT_IEEE802_11_RADIO
:
3927 case DLT_IEEE802_11_RADIO_AVS
:
3930 * Check that we have a data frame.
3932 b0
= gen_check_802_11_data_frame(cstate
);
3936 bpf_error(cstate
, "'llc' not supported for %s",
3937 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3943 gen_llc(compiler_state_t
*cstate
)
3946 * Catch errors reported by us and routines below us, and return NULL
3949 if (setjmp(cstate
->top_ctx
))
3952 return gen_llc_internal(cstate
);
3956 gen_llc_i(compiler_state_t
*cstate
)
3958 struct block
*b0
, *b1
;
3962 * Catch errors reported by us and routines below us, and return NULL
3965 if (setjmp(cstate
->top_ctx
))
3969 * Check whether this is an LLC frame.
3971 b0
= gen_llc_internal(cstate
);
3974 * Load the control byte and test the low-order bit; it must
3975 * be clear for I frames.
3977 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3978 b1
= new_block(cstate
, JMP(BPF_JSET
));
3987 gen_llc_s(compiler_state_t
*cstate
)
3989 struct block
*b0
, *b1
;
3992 * Catch errors reported by us and routines below us, and return NULL
3995 if (setjmp(cstate
->top_ctx
))
3999 * Check whether this is an LLC frame.
4001 b0
= gen_llc_internal(cstate
);
4004 * Now compare the low-order 2 bit of the control byte against
4005 * the appropriate value for S frames.
4007 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4013 gen_llc_u(compiler_state_t
*cstate
)
4015 struct block
*b0
, *b1
;
4018 * Catch errors reported by us and routines below us, and return NULL
4021 if (setjmp(cstate
->top_ctx
))
4025 * Check whether this is an LLC frame.
4027 b0
= gen_llc_internal(cstate
);
4030 * Now compare the low-order 2 bit of the control byte against
4031 * the appropriate value for U frames.
4033 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4039 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4041 struct block
*b0
, *b1
;
4044 * Catch errors reported by us and routines below us, and return NULL
4047 if (setjmp(cstate
->top_ctx
))
4051 * Check whether this is an LLC frame.
4053 b0
= gen_llc_internal(cstate
);
4056 * Now check for an S frame with the appropriate type.
4058 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4064 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4066 struct block
*b0
, *b1
;
4069 * Catch errors reported by us and routines below us, and return NULL
4072 if (setjmp(cstate
->top_ctx
))
4076 * Check whether this is an LLC frame.
4078 b0
= gen_llc_internal(cstate
);
4081 * Now check for a U frame with the appropriate type.
4083 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4089 * Generate code to match a particular packet type, for link-layer types
4090 * using 802.2 LLC headers.
4092 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4093 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4095 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4096 * value, if <= ETHERMTU. We use that to determine whether to
4097 * match the DSAP or both DSAP and LSAP or to check the OUI and
4098 * protocol ID in a SNAP header.
4100 static struct block
*
4101 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4104 * XXX - handle token-ring variable-length header.
4110 case LLCSAP_NETBEUI
:
4112 * XXX - should we check both the DSAP and the
4113 * SSAP, like this, or should we check just the
4114 * DSAP, as we do for other SAP values?
4116 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4117 ((ll_proto
<< 8) | ll_proto
));
4121 * XXX - are there ever SNAP frames for IPX on
4122 * non-Ethernet 802.x networks?
4124 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4126 case ETHERTYPE_ATALK
:
4128 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4129 * SNAP packets with an organization code of
4130 * 0x080007 (Apple, for Appletalk) and a protocol
4131 * type of ETHERTYPE_ATALK (Appletalk).
4133 * XXX - check for an organization code of
4134 * encapsulated Ethernet as well?
4136 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4140 * XXX - we don't have to check for IPX 802.3
4141 * here, but should we check for the IPX Ethertype?
4143 if (ll_proto
<= ETHERMTU
) {
4145 * This is an LLC SAP value, so check
4148 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4151 * This is an Ethernet type; we assume that it's
4152 * unlikely that it'll appear in the right place
4153 * at random, and therefore check only the
4154 * location that would hold the Ethernet type
4155 * in a SNAP frame with an organization code of
4156 * 0x000000 (encapsulated Ethernet).
4158 * XXX - if we were to check for the SNAP DSAP and
4159 * LSAP, as per XXX, and were also to check for an
4160 * organization code of 0x000000 (encapsulated
4161 * Ethernet), we'd do
4163 * return gen_snap(cstate, 0x000000, ll_proto);
4165 * here; for now, we don't, as per the above.
4166 * I don't know whether it's worth the extra CPU
4167 * time to do the right check or not.
4169 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4174 static struct block
*
4175 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4176 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4178 struct block
*b0
, *b1
;
4192 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4193 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4199 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4200 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4205 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4209 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4213 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4217 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4221 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4225 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4232 b0
= gen_linktype(cstate
, ll_proto
);
4233 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4239 static struct block
*
4240 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4241 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4244 struct block
*b0
, *b1
;
4247 * Code below needs to access four separate 32-bit parts of the 128-bit
4248 * IPv6 address and mask. In some OSes this is as simple as using the
4249 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4250 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4251 * far as libpcap sees it. Hence copy the data before use to avoid
4252 * potential unaligned memory access and the associated compiler
4253 * warnings (whether genuine or not).
4255 bpf_u_int32 a
[4], m
[4];
4268 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4269 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4275 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4276 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4281 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4285 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4289 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4293 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4297 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4301 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4308 /* this order is important */
4309 memcpy(a
, addr
, sizeof(a
));
4310 memcpy(m
, mask
, sizeof(m
));
4311 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4312 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4314 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4316 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4318 b0
= gen_linktype(cstate
, ll_proto
);
4324 static struct block
*
4325 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4327 register struct block
*b0
, *b1
;
4331 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4334 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4337 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4338 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4344 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4345 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4350 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4354 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4358 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4362 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4366 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4370 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4378 * Like gen_ehostop, but for DLT_FDDI
4380 static struct block
*
4381 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4383 struct block
*b0
, *b1
;
4387 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4390 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4393 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4394 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4400 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4401 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4406 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4410 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4414 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4418 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4422 bpf_error(cstate
, "'ra' is only supported on 802.11");
4426 bpf_error(cstate
, "'ta' is only supported on 802.11");
4434 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4436 static struct block
*
4437 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4439 register struct block
*b0
, *b1
;
4443 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4446 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4449 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4450 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4456 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4457 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4462 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4466 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4470 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4474 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4478 bpf_error(cstate
, "'ra' is only supported on 802.11");
4482 bpf_error(cstate
, "'ta' is only supported on 802.11");
4490 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4491 * various 802.11 + radio headers.
4493 static struct block
*
4494 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4496 register struct block
*b0
, *b1
, *b2
;
4497 register struct slist
*s
;
4499 #ifdef ENABLE_WLAN_FILTERING_PATCH
4502 * We need to disable the optimizer because the optimizer is buggy
4503 * and wipes out some LD instructions generated by the below
4504 * code to validate the Frame Control bits
4506 cstate
->no_optimize
= 1;
4507 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4514 * For control frames, there is no SA.
4516 * For management frames, SA is at an
4517 * offset of 10 from the beginning of
4520 * For data frames, SA is at an offset
4521 * of 10 from the beginning of the packet
4522 * if From DS is clear, at an offset of
4523 * 16 from the beginning of the packet
4524 * if From DS is set and To DS is clear,
4525 * and an offset of 24 from the beginning
4526 * of the packet if From DS is set and To DS
4531 * Generate the tests to be done for data frames
4534 * First, check for To DS set, i.e. check "link[1] & 0x01".
4536 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4537 b1
= new_block(cstate
, JMP(BPF_JSET
));
4538 b1
->s
.k
= 0x01; /* To DS */
4542 * If To DS is set, the SA is at 24.
4544 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4548 * Now, check for To DS not set, i.e. check
4549 * "!(link[1] & 0x01)".
4551 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4552 b2
= new_block(cstate
, JMP(BPF_JSET
));
4553 b2
->s
.k
= 0x01; /* To DS */
4558 * If To DS is not set, the SA is at 16.
4560 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4564 * Now OR together the last two checks. That gives
4565 * the complete set of checks for data frames with
4571 * Now check for From DS being set, and AND that with
4572 * the ORed-together checks.
4574 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4575 b1
= new_block(cstate
, JMP(BPF_JSET
));
4576 b1
->s
.k
= 0x02; /* From DS */
4581 * Now check for data frames with From DS not set.
4583 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4584 b2
= new_block(cstate
, JMP(BPF_JSET
));
4585 b2
->s
.k
= 0x02; /* From DS */
4590 * If From DS isn't set, the SA is at 10.
4592 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4596 * Now OR together the checks for data frames with
4597 * From DS not set and for data frames with From DS
4598 * set; that gives the checks done for data frames.
4603 * Now check for a data frame.
4604 * I.e, check "link[0] & 0x08".
4606 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4607 b1
= new_block(cstate
, JMP(BPF_JSET
));
4612 * AND that with the checks done for data frames.
4617 * If the high-order bit of the type value is 0, this
4618 * is a management frame.
4619 * I.e, check "!(link[0] & 0x08)".
4621 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4622 b2
= new_block(cstate
, JMP(BPF_JSET
));
4628 * For management frames, the SA is at 10.
4630 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4634 * OR that with the checks done for data frames.
4635 * That gives the checks done for management and
4641 * If the low-order bit of the type value is 1,
4642 * this is either a control frame or a frame
4643 * with a reserved type, and thus not a
4646 * I.e., check "!(link[0] & 0x04)".
4648 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4649 b1
= new_block(cstate
, JMP(BPF_JSET
));
4655 * AND that with the checks for data and management
4665 * For control frames, there is no DA.
4667 * For management frames, DA is at an
4668 * offset of 4 from the beginning of
4671 * For data frames, DA is at an offset
4672 * of 4 from the beginning of the packet
4673 * if To DS is clear and at an offset of
4674 * 16 from the beginning of the packet
4679 * Generate the tests to be done for data frames.
4681 * First, check for To DS set, i.e. "link[1] & 0x01".
4683 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4684 b1
= new_block(cstate
, JMP(BPF_JSET
));
4685 b1
->s
.k
= 0x01; /* To DS */
4689 * If To DS is set, the DA is at 16.
4691 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4695 * Now, check for To DS not set, i.e. check
4696 * "!(link[1] & 0x01)".
4698 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4699 b2
= new_block(cstate
, JMP(BPF_JSET
));
4700 b2
->s
.k
= 0x01; /* To DS */
4705 * If To DS is not set, the DA is at 4.
4707 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4711 * Now OR together the last two checks. That gives
4712 * the complete set of checks for data frames.
4717 * Now check for a data frame.
4718 * I.e, check "link[0] & 0x08".
4720 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4721 b1
= new_block(cstate
, JMP(BPF_JSET
));
4726 * AND that with the checks done for data frames.
4731 * If the high-order bit of the type value is 0, this
4732 * is a management frame.
4733 * I.e, check "!(link[0] & 0x08)".
4735 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4736 b2
= new_block(cstate
, JMP(BPF_JSET
));
4742 * For management frames, the DA is at 4.
4744 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4748 * OR that with the checks done for data frames.
4749 * That gives the checks done for management and
4755 * If the low-order bit of the type value is 1,
4756 * this is either a control frame or a frame
4757 * with a reserved type, and thus not a
4760 * I.e., check "!(link[0] & 0x04)".
4762 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4763 b1
= new_block(cstate
, JMP(BPF_JSET
));
4769 * AND that with the checks for data and management
4776 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4777 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4783 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4784 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4789 * XXX - add BSSID keyword?
4792 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4796 * Not present in CTS or ACK control frames.
4798 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4799 IEEE80211_FC0_TYPE_MASK
);
4801 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4802 IEEE80211_FC0_SUBTYPE_MASK
);
4804 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4805 IEEE80211_FC0_SUBTYPE_MASK
);
4809 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4815 * Not present in control frames.
4817 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4818 IEEE80211_FC0_TYPE_MASK
);
4820 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4826 * Present only if the direction mask has both "From DS"
4827 * and "To DS" set. Neither control frames nor management
4828 * frames should have both of those set, so we don't
4829 * check the frame type.
4831 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4832 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4833 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4839 * Not present in management frames; addr1 in other
4844 * If the high-order bit of the type value is 0, this
4845 * is a management frame.
4846 * I.e, check "(link[0] & 0x08)".
4848 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4849 b1
= new_block(cstate
, JMP(BPF_JSET
));
4856 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4859 * AND that with the check of addr1.
4866 * Not present in management frames; addr2, if present,
4871 * Not present in CTS or ACK control frames.
4873 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4874 IEEE80211_FC0_TYPE_MASK
);
4876 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4877 IEEE80211_FC0_SUBTYPE_MASK
);
4879 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4880 IEEE80211_FC0_SUBTYPE_MASK
);
4886 * If the high-order bit of the type value is 0, this
4887 * is a management frame.
4888 * I.e, check "(link[0] & 0x08)".
4890 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4891 b1
= new_block(cstate
, JMP(BPF_JSET
));
4896 * AND that with the check for frames other than
4897 * CTS and ACK frames.
4904 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4913 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4914 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4915 * as the RFC states.)
4917 static struct block
*
4918 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4920 register struct block
*b0
, *b1
;
4924 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4927 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4930 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4931 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4937 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4938 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4943 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4947 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4951 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4955 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4959 bpf_error(cstate
, "'ra' is only supported on 802.11");
4963 bpf_error(cstate
, "'ta' is only supported on 802.11");
4971 * This is quite tricky because there may be pad bytes in front of the
4972 * DECNET header, and then there are two possible data packet formats that
4973 * carry both src and dst addresses, plus 5 packet types in a format that
4974 * carries only the src node, plus 2 types that use a different format and
4975 * also carry just the src node.
4979 * Instead of doing those all right, we just look for data packets with
4980 * 0 or 1 bytes of padding. If you want to look at other packets, that
4981 * will require a lot more hacking.
4983 * To add support for filtering on DECNET "areas" (network numbers)
4984 * one would want to add a "mask" argument to this routine. That would
4985 * make the filter even more inefficient, although one could be clever
4986 * and not generate masking instructions if the mask is 0xFFFF.
4988 static struct block
*
4989 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4991 struct block
*b0
, *b1
, *b2
, *tmp
;
4992 u_int offset_lh
; /* offset if long header is received */
4993 u_int offset_sh
; /* offset if short header is received */
4998 offset_sh
= 1; /* follows flags */
4999 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5003 offset_sh
= 3; /* follows flags, dstnode */
5004 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5008 /* Inefficient because we do our Calvinball dance twice */
5009 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5010 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5016 /* Inefficient because we do our Calvinball dance twice */
5017 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5018 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5023 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5027 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5031 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5035 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5039 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5043 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5051 * In a DECnet message inside an Ethernet frame the first two bytes
5052 * immediately after EtherType are the [litle-endian] DECnet message
5053 * length, which is irrelevant in this context.
5055 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5056 * 8-bit bitmap of the optional padding before the packet route header.
5057 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5058 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5059 * means there aren't any PAD bytes after the bitmap, so the header
5060 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5061 * is set to 0, thus the header begins at the third byte.
5063 * The header can be in several (as mentioned above) formats, all of
5064 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5065 * (PF, "pad field") set to 0 regardless of any padding present before
5066 * the header. "Short header" means bits 0-2 of the bitmap encode the
5067 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5069 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5070 * because the wire encoding is little-endian and this function always
5071 * receives a big-endian address value.
5073 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5074 /* Check for pad = 1, long header case */
5075 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5076 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5077 BPF_H
, SWAPSHORT(addr
));
5079 /* Check for pad = 0, long header case */
5080 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5081 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5085 /* Check for pad = 1, short header case */
5086 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5087 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5091 /* Check for pad = 0, short header case */
5092 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5093 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5098 /* Combine with test for cstate->linktype */
5104 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5105 * test the bottom-of-stack bit, and then check the version number
5106 * field in the IP header.
5108 static struct block
*
5109 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5111 struct block
*b0
, *b1
;
5116 /* match the bottom-of-stack bit */
5117 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5118 /* match the IPv4 version number */
5119 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5123 case ETHERTYPE_IPV6
:
5124 /* match the bottom-of-stack bit */
5125 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5126 /* match the IPv4 version number */
5127 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5132 /* FIXME add other L3 proto IDs */
5133 bpf_error(cstate
, "unsupported protocol over mpls");
5138 static struct block
*
5139 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5140 int proto
, int dir
, int type
)
5142 struct block
*b0
, *b1
;
5143 const char *typestr
;
5153 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5155 * Only check for non-IPv4 addresses if we're not
5156 * checking MPLS-encapsulated packets.
5158 if (cstate
->label_stack_depth
== 0) {
5159 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5161 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5167 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5170 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
5173 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
5176 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
5179 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5182 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5185 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5188 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5191 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5194 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5197 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5200 return gen_dnhostop(cstate
, addr
, dir
);
5203 bpf_error(cstate
, "LAT host filtering not implemented");
5206 bpf_error(cstate
, "SCA host filtering not implemented");
5209 bpf_error(cstate
, "MOPRC host filtering not implemented");
5212 bpf_error(cstate
, "MOPDL host filtering not implemented");
5215 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5218 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5221 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5224 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5227 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5230 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5233 bpf_error(cstate
, "AARP host filtering not implemented");
5236 bpf_error(cstate
, "ISO host filtering not implemented");
5239 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5242 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5245 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5248 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5251 bpf_error(cstate
, "IPX host filtering not implemented");
5254 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5257 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5260 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5263 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5266 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5269 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5272 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5275 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5278 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5281 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5290 static struct block
*
5291 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5292 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5294 const char *typestr
;
5304 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5307 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5310 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5313 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5316 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5319 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5322 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5325 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5328 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5331 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5334 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5337 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5340 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5343 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5346 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5349 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5352 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5355 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5358 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5361 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5364 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5367 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5370 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5373 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5376 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5379 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5382 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5385 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5388 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5391 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5394 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5397 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5400 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5403 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5406 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5409 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5412 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5415 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5418 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5421 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5431 static struct block
*
5432 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5433 struct addrinfo
*alist
, int proto
, int dir
)
5435 struct block
*b0
, *b1
, *tmp
;
5436 struct addrinfo
*ai
;
5437 struct sockaddr_in
*sin
;
5440 bpf_error(cstate
, "direction applied to 'gateway'");
5447 switch (cstate
->linktype
) {
5449 case DLT_NETANALYZER
:
5450 case DLT_NETANALYZER_TRANSPARENT
:
5451 b1
= gen_prevlinkhdr_check(cstate
);
5452 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5457 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5460 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5462 case DLT_IEEE802_11
:
5463 case DLT_PRISM_HEADER
:
5464 case DLT_IEEE802_11_RADIO_AVS
:
5465 case DLT_IEEE802_11_RADIO
:
5467 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5471 * This is LLC-multiplexed traffic; if it were
5472 * LANE, cstate->linktype would have been set to
5476 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5477 case DLT_IP_OVER_FC
:
5478 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5482 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5485 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5487 * Does it have an address?
5489 if (ai
->ai_addr
!= NULL
) {
5491 * Yes. Is it an IPv4 address?
5493 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5495 * Generate an entry for it.
5497 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5498 tmp
= gen_host(cstate
,
5499 ntohl(sin
->sin_addr
.s_addr
),
5500 0xffffffff, proto
, Q_OR
, Q_HOST
);
5502 * Is it the *first* IPv4 address?
5506 * Yes, so start with it.
5511 * No, so OR it into the
5523 * No IPv4 addresses found.
5531 bpf_error(cstate
, "illegal modifier of 'gateway'");
5536 static struct block
*
5537 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5545 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5549 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5553 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5557 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5560 #ifndef IPPROTO_IGMP
5561 #define IPPROTO_IGMP 2
5565 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5568 #ifndef IPPROTO_IGRP
5569 #define IPPROTO_IGRP 9
5572 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5576 #define IPPROTO_PIM 103
5580 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5583 #ifndef IPPROTO_VRRP
5584 #define IPPROTO_VRRP 112
5588 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5591 #ifndef IPPROTO_CARP
5592 #define IPPROTO_CARP 112
5596 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5600 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5604 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5608 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5612 bpf_error(cstate
, "link layer applied in wrong context");
5615 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5619 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5623 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5627 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5631 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5635 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5639 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5643 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5646 #ifndef IPPROTO_ICMPV6
5647 #define IPPROTO_ICMPV6 58
5650 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5654 #define IPPROTO_AH 51
5657 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5661 #define IPPROTO_ESP 50
5664 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5668 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5672 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5676 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5679 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5680 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5681 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5683 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5685 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5687 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5691 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5692 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5693 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5695 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5697 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5699 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5703 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5704 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5705 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5707 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5712 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5713 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5718 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5719 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5721 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5723 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5728 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5729 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5734 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5735 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5740 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5744 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5748 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5752 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5756 bpf_error(cstate
, "'radio' is not a valid protocol type");
5765 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5768 * Catch errors reported by us and routines below us, and return NULL
5771 if (setjmp(cstate
->top_ctx
))
5774 return gen_proto_abbrev_internal(cstate
, proto
);
5777 static struct block
*
5778 gen_ipfrag(compiler_state_t
*cstate
)
5783 /* not IPv4 frag other than the first frag */
5784 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5785 b
= new_block(cstate
, JMP(BPF_JSET
));
5794 * Generate a comparison to a port value in the transport-layer header
5795 * at the specified offset from the beginning of that header.
5797 * XXX - this handles a variable-length prefix preceding the link-layer
5798 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5799 * variable-length link-layer headers (such as Token Ring or 802.11
5802 static struct block
*
5803 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5805 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5808 static struct block
*
5809 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5811 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5814 static struct block
*
5815 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5817 struct block
*b0
, *b1
, *tmp
;
5819 /* ip proto 'proto' and not a fragment other than the first fragment */
5820 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5821 b0
= gen_ipfrag(cstate
);
5826 b1
= gen_portatom(cstate
, 0, port
);
5830 b1
= gen_portatom(cstate
, 2, port
);
5834 tmp
= gen_portatom(cstate
, 0, port
);
5835 b1
= gen_portatom(cstate
, 2, port
);
5841 tmp
= gen_portatom(cstate
, 0, port
);
5842 b1
= gen_portatom(cstate
, 2, port
);
5847 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5851 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5855 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5859 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5863 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5867 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5879 static struct block
*
5880 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5882 struct block
*b0
, *b1
, *tmp
;
5887 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5888 * not LLC encapsulation with LLCSAP_IP.
5890 * For IEEE 802 networks - which includes 802.5 token ring
5891 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5892 * says that SNAP encapsulation is used, not LLC encapsulation
5895 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5896 * RFC 2225 say that SNAP encapsulation is used, not LLC
5897 * encapsulation with LLCSAP_IP.
5899 * So we always check for ETHERTYPE_IP.
5901 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5907 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5911 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5912 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5914 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5926 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5928 struct block
*b0
, *b1
, *tmp
;
5930 /* ip6 proto 'proto' */
5931 /* XXX - catch the first fragment of a fragmented packet? */
5932 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5936 b1
= gen_portatom6(cstate
, 0, port
);
5940 b1
= gen_portatom6(cstate
, 2, port
);
5944 tmp
= gen_portatom6(cstate
, 0, port
);
5945 b1
= gen_portatom6(cstate
, 2, port
);
5951 tmp
= gen_portatom6(cstate
, 0, port
);
5952 b1
= gen_portatom6(cstate
, 2, port
);
5964 static struct block
*
5965 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5967 struct block
*b0
, *b1
, *tmp
;
5969 /* link proto ip6 */
5970 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5976 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5980 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5981 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5983 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5994 /* gen_portrange code */
5995 static struct block
*
5996 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5999 struct block
*b1
, *b2
;
6003 * Reverse the order of the ports, so v1 is the lower one.
6012 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
6013 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
6020 static struct block
*
6021 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6022 bpf_u_int32 proto
, int dir
)
6024 struct block
*b0
, *b1
, *tmp
;
6026 /* ip proto 'proto' and not a fragment other than the first fragment */
6027 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
6028 b0
= gen_ipfrag(cstate
);
6033 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6037 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6041 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6042 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6048 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6049 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6054 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6058 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6062 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6066 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6070 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
6074 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
6086 static struct block
*
6087 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6090 struct block
*b0
, *b1
, *tmp
;
6093 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6099 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6104 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6105 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6107 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6118 static struct block
*
6119 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6122 struct block
*b1
, *b2
;
6126 * Reverse the order of the ports, so v1 is the lower one.
6135 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6136 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6143 static struct block
*
6144 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6145 bpf_u_int32 proto
, int dir
)
6147 struct block
*b0
, *b1
, *tmp
;
6149 /* ip6 proto 'proto' */
6150 /* XXX - catch the first fragment of a fragmented packet? */
6151 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6155 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6159 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6163 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6164 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6170 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6171 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6183 static struct block
*
6184 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6187 struct block
*b0
, *b1
, *tmp
;
6189 /* link proto ip6 */
6190 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6196 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6201 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6202 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6204 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6216 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6225 v
= pcap_nametoproto(name
);
6226 if (v
== PROTO_UNDEF
)
6227 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6231 /* XXX should look up h/w protocol type based on cstate->linktype */
6232 v
= pcap_nametoeproto(name
);
6233 if (v
== PROTO_UNDEF
) {
6234 v
= pcap_nametollc(name
);
6235 if (v
== PROTO_UNDEF
)
6236 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6241 if (strcmp(name
, "esis") == 0)
6243 else if (strcmp(name
, "isis") == 0)
6245 else if (strcmp(name
, "clnp") == 0)
6248 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6258 #if !defined(NO_PROTOCHAIN)
6259 static struct block
*
6260 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6262 struct block
*b0
, *b
;
6263 struct slist
*s
[100];
6264 int fix2
, fix3
, fix4
, fix5
;
6265 int ahcheck
, again
, end
;
6267 int reg2
= alloc_reg(cstate
);
6269 memset(s
, 0, sizeof(s
));
6270 fix3
= fix4
= fix5
= 0;
6277 b0
= gen_protochain(cstate
, v
, Q_IP
);
6278 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6282 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6287 * We don't handle variable-length prefixes before the link-layer
6288 * header, or variable-length link-layer headers, here yet.
6289 * We might want to add BPF instructions to do the protochain
6290 * work, to simplify that and, on platforms that have a BPF
6291 * interpreter with the new instructions, let the filtering
6292 * be done in the kernel. (We already require a modified BPF
6293 * engine to do the protochain stuff, to support backward
6294 * branches, and backward branch support is unlikely to appear
6295 * in kernel BPF engines.)
6297 if (cstate
->off_linkpl
.is_variable
)
6298 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6301 * To quote a comment in optimize.c:
6303 * "These data structures are used in a Cocke and Schwartz style
6304 * value numbering scheme. Since the flowgraph is acyclic,
6305 * exit values can be propagated from a node's predecessors
6306 * provided it is uniquely defined."
6308 * "Acyclic" means "no backward branches", which means "no
6309 * loops", so we have to turn the optimizer off.
6311 cstate
->no_optimize
= 1;
6314 * s[0] is a dummy entry to protect other BPF insn from damage
6315 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6316 * hard to find interdependency made by jump table fixup.
6319 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6324 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6327 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6328 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6330 /* X = ip->ip_hl << 2 */
6331 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6332 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6337 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6339 /* A = ip6->ip_nxt */
6340 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6341 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6343 /* X = sizeof(struct ip6_hdr) */
6344 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6350 bpf_error(cstate
, "unsupported proto to gen_protochain");
6354 /* again: if (A == v) goto end; else fall through; */
6356 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6358 s
[i
]->s
.jt
= NULL
; /*later*/
6359 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6363 #ifndef IPPROTO_NONE
6364 #define IPPROTO_NONE 59
6366 /* if (A == IPPROTO_NONE) goto end */
6367 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6368 s
[i
]->s
.jt
= NULL
; /*later*/
6369 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6370 s
[i
]->s
.k
= IPPROTO_NONE
;
6371 s
[fix5
]->s
.jf
= s
[i
];
6375 if (proto
== Q_IPV6
) {
6376 int v6start
, v6end
, v6advance
, j
;
6379 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6380 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6381 s
[i
]->s
.jt
= NULL
; /*later*/
6382 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6383 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6384 s
[fix2
]->s
.jf
= s
[i
];
6386 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6387 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6388 s
[i
]->s
.jt
= NULL
; /*later*/
6389 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6390 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6392 /* if (A == IPPROTO_ROUTING) goto v6advance */
6393 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6394 s
[i
]->s
.jt
= NULL
; /*later*/
6395 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6396 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6398 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6399 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6400 s
[i
]->s
.jt
= NULL
; /*later*/
6401 s
[i
]->s
.jf
= NULL
; /*later*/
6402 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6412 * A = P[X + packet head];
6413 * X = X + (P[X + packet head + 1] + 1) * 8;
6415 /* A = P[X + packet head] */
6416 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6417 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6420 s
[i
] = new_stmt(cstate
, BPF_ST
);
6423 /* A = P[X + packet head + 1]; */
6424 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6425 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6428 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6432 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6436 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6440 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6443 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6447 /* goto again; (must use BPF_JA for backward jump) */
6448 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6449 s
[i
]->s
.k
= again
- i
- 1;
6450 s
[i
- 1]->s
.jf
= s
[i
];
6454 for (j
= v6start
; j
<= v6end
; j
++)
6455 s
[j
]->s
.jt
= s
[v6advance
];
6458 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6460 s
[fix2
]->s
.jf
= s
[i
];
6466 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6467 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6468 s
[i
]->s
.jt
= NULL
; /*later*/
6469 s
[i
]->s
.jf
= NULL
; /*later*/
6470 s
[i
]->s
.k
= IPPROTO_AH
;
6472 s
[fix3
]->s
.jf
= s
[ahcheck
];
6479 * X = X + (P[X + 1] + 2) * 4;
6482 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6484 /* A = P[X + packet head]; */
6485 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6486 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6489 s
[i
] = new_stmt(cstate
, BPF_ST
);
6493 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6496 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6500 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6502 /* A = P[X + packet head] */
6503 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6504 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6507 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6511 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6515 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6518 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6522 /* goto again; (must use BPF_JA for backward jump) */
6523 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6524 s
[i
]->s
.k
= again
- i
- 1;
6529 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6531 s
[fix2
]->s
.jt
= s
[end
];
6532 s
[fix4
]->s
.jf
= s
[end
];
6533 s
[fix5
]->s
.jt
= s
[end
];
6540 for (i
= 0; i
< max
- 1; i
++)
6541 s
[i
]->next
= s
[i
+ 1];
6542 s
[max
- 1]->next
= NULL
;
6547 b
= new_block(cstate
, JMP(BPF_JEQ
));
6548 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6551 free_reg(cstate
, reg2
);
6556 #endif /* !defined(NO_PROTOCHAIN) */
6558 static struct block
*
6559 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6562 struct block
*b0
, *b1
;
6565 * A data frame has the 0x08 bit (b3) in the frame control field set
6566 * and the 0x04 bit (b2) clear.
6568 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6569 b0
= new_block(cstate
, JMP(BPF_JSET
));
6573 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6574 b1
= new_block(cstate
, JMP(BPF_JSET
));
6585 * Generate code that checks whether the packet is a packet for protocol
6586 * <proto> and whether the type field in that protocol's header has
6587 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6588 * IP packet and checks the protocol number in the IP header against <v>.
6590 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6591 * against Q_IP and Q_IPV6.
6593 static struct block
*
6594 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6596 struct block
*b0
, *b1
;
6599 if (dir
!= Q_DEFAULT
)
6600 bpf_error(cstate
, "direction applied to 'proto'");
6604 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6605 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6610 return gen_linktype(cstate
, v
);
6614 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6615 * not LLC encapsulation with LLCSAP_IP.
6617 * For IEEE 802 networks - which includes 802.5 token ring
6618 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6619 * says that SNAP encapsulation is used, not LLC encapsulation
6622 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6623 * RFC 2225 say that SNAP encapsulation is used, not LLC
6624 * encapsulation with LLCSAP_IP.
6626 * So we always check for ETHERTYPE_IP.
6628 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6629 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6634 bpf_error(cstate
, "arp does not encapsulate another protocol");
6638 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6642 bpf_error(cstate
, "'sctp proto' is bogus");
6646 bpf_error(cstate
, "'tcp proto' is bogus");
6650 bpf_error(cstate
, "'udp proto' is bogus");
6654 bpf_error(cstate
, "'icmp proto' is bogus");
6658 bpf_error(cstate
, "'igmp proto' is bogus");
6662 bpf_error(cstate
, "'igrp proto' is bogus");
6666 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6670 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6674 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6678 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6682 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6686 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6690 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6692 * Also check for a fragment header before the final
6695 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6696 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6698 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6704 bpf_error(cstate
, "'icmp6 proto' is bogus");
6708 bpf_error(cstate
, "'ah proto' is bogus");
6712 bpf_error(cstate
, "'esp proto' is bogus");
6716 bpf_error(cstate
, "'pim proto' is bogus");
6720 bpf_error(cstate
, "'vrrp proto' is bogus");
6724 bpf_error(cstate
, "'aarp proto' is bogus");
6728 switch (cstate
->linktype
) {
6732 * Frame Relay packets typically have an OSI
6733 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6734 * generates code to check for all the OSI
6735 * NLPIDs, so calling it and then adding a check
6736 * for the particular NLPID for which we're
6737 * looking is bogus, as we can just check for
6740 * What we check for is the NLPID and a frame
6741 * control field value of UI, i.e. 0x03 followed
6744 * XXX - assumes a 2-byte Frame Relay header with
6745 * DLCI and flags. What if the address is longer?
6747 * XXX - what about SNAP-encapsulated frames?
6749 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6755 * Cisco uses an Ethertype lookalike - for OSI,
6758 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6759 /* OSI in C-HDLC is stuffed with a fudge byte */
6760 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6765 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6766 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6772 bpf_error(cstate
, "'esis proto' is bogus");
6776 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6778 * 4 is the offset of the PDU type relative to the IS-IS
6781 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6786 bpf_error(cstate
, "'clnp proto' is not supported");
6790 bpf_error(cstate
, "'stp proto' is bogus");
6794 bpf_error(cstate
, "'ipx proto' is bogus");
6798 bpf_error(cstate
, "'netbeui proto' is bogus");
6802 bpf_error(cstate
, "'l1 proto' is bogus");
6806 bpf_error(cstate
, "'l2 proto' is bogus");
6810 bpf_error(cstate
, "'iih proto' is bogus");
6814 bpf_error(cstate
, "'snp proto' is bogus");
6818 bpf_error(cstate
, "'csnp proto' is bogus");
6822 bpf_error(cstate
, "'psnp proto' is bogus");
6826 bpf_error(cstate
, "'lsp proto' is bogus");
6830 bpf_error(cstate
, "'radio proto' is bogus");
6834 bpf_error(cstate
, "'carp proto' is bogus");
6845 * Convert a non-numeric name to a port number.
6848 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6850 struct addrinfo hints
, *res
, *ai
;
6852 struct sockaddr_in
*in4
;
6854 struct sockaddr_in6
*in6
;
6859 * We check for both TCP and UDP in case there are
6860 * ambiguous entries.
6862 memset(&hints
, 0, sizeof(hints
));
6863 hints
.ai_family
= PF_UNSPEC
;
6864 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6865 hints
.ai_protocol
= ipproto
;
6866 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6873 * No such port. Just return -1.
6880 * We don't use strerror() because it's not
6881 * guaranteed to be thread-safe on all platforms
6882 * (probably because it might use a non-thread-local
6883 * buffer into which to format an error message
6884 * if the error code isn't one for which it has
6885 * a canned string; three cheers for C string
6888 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6890 port
= -2; /* a real error */
6896 * This is a real error, not just "there's
6897 * no such service name".
6899 * We don't use gai_strerror() because it's not
6900 * guaranteed to be thread-safe on all platforms
6901 * (probably because it might use a non-thread-local
6902 * buffer into which to format an error message
6903 * if the error code isn't one for which it has
6904 * a canned string; three cheers for C string
6907 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6909 port
= -2; /* a real error */
6914 * OK, we found it. Did it find anything?
6916 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6918 * Does it have an address?
6920 if (ai
->ai_addr
!= NULL
) {
6922 * Yes. Get a port number; we're done.
6924 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6925 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6926 port
= ntohs(in4
->sin_port
);
6930 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6931 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6932 port
= ntohs(in6
->sin6_port
);
6944 * Convert a string to a port number.
6947 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6957 * See if it's a number.
6959 ret
= stoulen(string
, string_size
, &val
, cstate
);
6963 /* Unknown port type - it's just a number. */
6964 *proto
= PROTO_UNDEF
;
6967 case STOULEN_NOT_OCTAL_NUMBER
:
6968 case STOULEN_NOT_HEX_NUMBER
:
6969 case STOULEN_NOT_DECIMAL_NUMBER
:
6971 * Not a valid number; try looking it up as a port.
6973 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6974 memcpy(cpy
, string
, string_size
);
6975 cpy
[string_size
] = '\0';
6976 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6977 if (tcp_port
== -2) {
6979 * We got a hard error; the error string has
6983 longjmp(cstate
->top_ctx
, 1);
6986 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6987 if (udp_port
== -2) {
6989 * We got a hard error; the error string has
6993 longjmp(cstate
->top_ctx
, 1);
6998 * We need to check /etc/services for ambiguous entries.
6999 * If we find an ambiguous entry, and it has the
7000 * same port number, change the proto to PROTO_UNDEF
7001 * so both TCP and UDP will be checked.
7003 if (tcp_port
>= 0) {
7004 val
= (bpf_u_int32
)tcp_port
;
7005 *proto
= IPPROTO_TCP
;
7006 if (udp_port
>= 0) {
7007 if (udp_port
== tcp_port
)
7008 *proto
= PROTO_UNDEF
;
7011 /* Can't handle ambiguous names that refer
7012 to different port numbers. */
7013 warning("ambiguous port %s in /etc/services",
7020 if (udp_port
>= 0) {
7021 val
= (bpf_u_int32
)udp_port
;
7022 *proto
= IPPROTO_UDP
;
7026 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
7028 longjmp(cstate
->top_ctx
, 1);
7035 /* Error already set. */
7036 longjmp(cstate
->top_ctx
, 1);
7043 /* Should not happen */
7044 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
7045 longjmp(cstate
->top_ctx
, 1);
7052 * Convert a string in the form PPP-PPP, which correspond to ports, to
7053 * a starting and ending port in a port range.
7056 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
7057 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
7060 const char *first
, *second
;
7061 size_t first_size
, second_size
;
7064 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
7065 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
7068 * Make sure there are no other hyphens.
7070 * XXX - we support named ports, but there are some port names
7071 * in /etc/services that include hyphens, so this would rule
7074 if (strchr(hyphen_off
+ 1, '-') != NULL
)
7075 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
7079 * Get the length of the first port.
7082 first_size
= hyphen_off
- string
;
7083 if (first_size
== 0) {
7084 /* Range of "-port", which we don't support. */
7085 bpf_error(cstate
, "port range '%s' has no starting port", string
);
7089 * Try to convert it to a port.
7091 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
7092 save_proto
= *proto
;
7095 * Get the length of the second port.
7097 second
= hyphen_off
+ 1;
7098 second_size
= strlen(second
);
7099 if (second_size
== 0) {
7100 /* Range of "port-", which we don't support. */
7101 bpf_error(cstate
, "port range '%s' has no ending port", string
);
7105 * Try to convert it to a port.
7107 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
7108 if (*proto
!= save_proto
)
7109 *proto
= PROTO_UNDEF
;
7113 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
7115 int proto
= q
.proto
;
7119 bpf_u_int32 mask
, addr
;
7120 struct addrinfo
*res
, *res0
;
7121 struct sockaddr_in
*sin4
;
7124 struct sockaddr_in6
*sin6
;
7125 struct in6_addr mask128
;
7127 struct block
*b
, *tmp
;
7128 int port
, real_proto
;
7129 bpf_u_int32 port1
, port2
;
7132 * Catch errors reported by us and routines below us, and return NULL
7135 if (setjmp(cstate
->top_ctx
))
7141 addr
= pcap_nametonetaddr(name
);
7143 bpf_error(cstate
, "unknown network '%s'", name
);
7144 /* Left justify network addr and calculate its network mask */
7146 while (addr
&& (addr
& 0xff000000) == 0) {
7150 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7154 if (proto
== Q_LINK
) {
7155 switch (cstate
->linktype
) {
7158 case DLT_NETANALYZER
:
7159 case DLT_NETANALYZER_TRANSPARENT
:
7160 eaddr
= pcap_ether_hostton(name
);
7163 "unknown ether host '%s'", name
);
7164 tmp
= gen_prevlinkhdr_check(cstate
);
7165 b
= gen_ehostop(cstate
, eaddr
, dir
);
7172 eaddr
= pcap_ether_hostton(name
);
7175 "unknown FDDI host '%s'", name
);
7176 b
= gen_fhostop(cstate
, eaddr
, dir
);
7181 eaddr
= pcap_ether_hostton(name
);
7184 "unknown token ring host '%s'", name
);
7185 b
= gen_thostop(cstate
, eaddr
, dir
);
7189 case DLT_IEEE802_11
:
7190 case DLT_PRISM_HEADER
:
7191 case DLT_IEEE802_11_RADIO_AVS
:
7192 case DLT_IEEE802_11_RADIO
:
7194 eaddr
= pcap_ether_hostton(name
);
7197 "unknown 802.11 host '%s'", name
);
7198 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7202 case DLT_IP_OVER_FC
:
7203 eaddr
= pcap_ether_hostton(name
);
7206 "unknown Fibre Channel host '%s'", name
);
7207 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7212 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7213 } else if (proto
== Q_DECNET
) {
7215 * A long time ago on Ultrix libpcap supported
7216 * translation of DECnet host names into DECnet
7217 * addresses, but this feature is history now.
7219 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7222 memset(&mask128
, 0xff, sizeof(mask128
));
7224 res0
= res
= pcap_nametoaddrinfo(name
);
7226 bpf_error(cstate
, "unknown host '%s'", name
);
7233 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7234 tproto
== Q_DEFAULT
) {
7240 for (res
= res0
; res
; res
= res
->ai_next
) {
7241 switch (res
->ai_family
) {
7244 if (tproto
== Q_IPV6
)
7248 sin4
= (struct sockaddr_in
*)
7250 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7251 0xffffffff, tproto
, dir
, q
.addr
);
7255 if (tproto6
== Q_IP
)
7258 sin6
= (struct sockaddr_in6
*)
7260 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7261 &mask128
, tproto6
, dir
, q
.addr
);
7274 bpf_error(cstate
, "unknown host '%s'%s", name
,
7275 (proto
== Q_DEFAULT
)
7277 : " for specified address family");
7283 if (proto
!= Q_DEFAULT
&&
7284 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7285 bpf_error(cstate
, "illegal qualifier of 'port'");
7286 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7287 bpf_error(cstate
, "unknown port '%s'", name
);
7288 if (proto
== Q_UDP
) {
7289 if (real_proto
== IPPROTO_TCP
)
7290 bpf_error(cstate
, "port '%s' is tcp", name
);
7291 else if (real_proto
== IPPROTO_SCTP
)
7292 bpf_error(cstate
, "port '%s' is sctp", name
);
7294 /* override PROTO_UNDEF */
7295 real_proto
= IPPROTO_UDP
;
7297 if (proto
== Q_TCP
) {
7298 if (real_proto
== IPPROTO_UDP
)
7299 bpf_error(cstate
, "port '%s' is udp", name
);
7301 else if (real_proto
== IPPROTO_SCTP
)
7302 bpf_error(cstate
, "port '%s' is sctp", name
);
7304 /* override PROTO_UNDEF */
7305 real_proto
= IPPROTO_TCP
;
7307 if (proto
== Q_SCTP
) {
7308 if (real_proto
== IPPROTO_UDP
)
7309 bpf_error(cstate
, "port '%s' is udp", name
);
7311 else if (real_proto
== IPPROTO_TCP
)
7312 bpf_error(cstate
, "port '%s' is tcp", name
);
7314 /* override PROTO_UNDEF */
7315 real_proto
= IPPROTO_SCTP
;
7318 bpf_error(cstate
, "illegal port number %d < 0", port
);
7320 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7321 b
= gen_port(cstate
, port
, real_proto
, dir
);
7322 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7326 if (proto
!= Q_DEFAULT
&&
7327 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7328 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7329 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7330 if (proto
== Q_UDP
) {
7331 if (real_proto
== IPPROTO_TCP
)
7332 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7333 else if (real_proto
== IPPROTO_SCTP
)
7334 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7336 /* override PROTO_UNDEF */
7337 real_proto
= IPPROTO_UDP
;
7339 if (proto
== Q_TCP
) {
7340 if (real_proto
== IPPROTO_UDP
)
7341 bpf_error(cstate
, "port in range '%s' is udp", name
);
7342 else if (real_proto
== IPPROTO_SCTP
)
7343 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7345 /* override PROTO_UNDEF */
7346 real_proto
= IPPROTO_TCP
;
7348 if (proto
== Q_SCTP
) {
7349 if (real_proto
== IPPROTO_UDP
)
7350 bpf_error(cstate
, "port in range '%s' is udp", name
);
7351 else if (real_proto
== IPPROTO_TCP
)
7352 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7354 /* override PROTO_UNDEF */
7355 real_proto
= IPPROTO_SCTP
;
7358 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7360 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7362 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7363 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7368 eaddr
= pcap_ether_hostton(name
);
7370 bpf_error(cstate
, "unknown ether host: %s", name
);
7372 res
= pcap_nametoaddrinfo(name
);
7375 bpf_error(cstate
, "unknown host '%s'", name
);
7376 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7380 bpf_error(cstate
, "unknown host '%s'", name
);
7383 bpf_error(cstate
, "'gateway' not supported in this configuration");
7387 real_proto
= lookup_proto(cstate
, name
, proto
);
7388 if (real_proto
>= 0)
7389 return gen_proto(cstate
, real_proto
, proto
, dir
);
7391 bpf_error(cstate
, "unknown protocol: %s", name
);
7393 #if !defined(NO_PROTOCHAIN)
7395 real_proto
= lookup_proto(cstate
, name
, proto
);
7396 if (real_proto
>= 0)
7397 return gen_protochain(cstate
, real_proto
, proto
);
7399 bpf_error(cstate
, "unknown protocol: %s", name
);
7400 #endif /* !defined(NO_PROTOCHAIN) */
7411 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7412 bpf_u_int32 masklen
, struct qual q
)
7414 register int nlen
, mlen
;
7419 * Catch errors reported by us and routines below us, and return NULL
7422 if (setjmp(cstate
->top_ctx
))
7425 nlen
= pcapint_atoin(s1
, &n
);
7427 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7428 /* Promote short ipaddr */
7432 mlen
= pcapint_atoin(s2
, &m
);
7434 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7435 /* Promote short ipaddr */
7438 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7441 /* Convert mask len to mask */
7443 bpf_error(cstate
, "mask length must be <= 32");
7444 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7445 m
= (bpf_u_int32
)m64
;
7447 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7454 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7457 bpf_error(cstate
, "Mask syntax for networks only");
7464 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7472 * Catch errors reported by us and routines below us, and return NULL
7475 if (setjmp(cstate
->top_ctx
))
7482 * v contains a 32-bit unsigned parsed from a string of the
7483 * form {N}, which could be decimal, hexadecimal or octal.
7484 * Although it would be possible to use the value as a raw
7485 * 16-bit DECnet address when the value fits into 16 bits, this
7486 * would be a questionable feature: DECnet address wire
7487 * encoding is little-endian, so this would not work as
7488 * intuitively as the same works for [big-endian] IPv4
7489 * addresses (0x01020304 means 1.2.3.4).
7491 if (proto
== Q_DECNET
)
7492 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7494 } else if (proto
== Q_DECNET
) {
7496 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7497 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7498 * for a valid DECnet address.
7500 vlen
= pcapint_atodn(s
, &v
);
7502 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7505 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7506 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7509 vlen
= pcapint_atoin(s
, &v
);
7511 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7519 if (proto
== Q_DECNET
)
7520 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7521 else if (proto
== Q_LINK
) {
7522 bpf_error(cstate
, "illegal link layer address");
7525 if (s
== NULL
&& q
.addr
== Q_NET
) {
7526 /* Promote short net number */
7527 while (v
&& (v
& 0xff000000) == 0) {
7532 /* Promote short ipaddr */
7534 mask
<<= 32 - vlen
;
7536 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7541 proto
= IPPROTO_UDP
;
7542 else if (proto
== Q_TCP
)
7543 proto
= IPPROTO_TCP
;
7544 else if (proto
== Q_SCTP
)
7545 proto
= IPPROTO_SCTP
;
7546 else if (proto
== Q_DEFAULT
)
7547 proto
= PROTO_UNDEF
;
7549 bpf_error(cstate
, "illegal qualifier of 'port'");
7552 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7556 b
= gen_port(cstate
, v
, proto
, dir
);
7557 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7563 proto
= IPPROTO_UDP
;
7564 else if (proto
== Q_TCP
)
7565 proto
= IPPROTO_TCP
;
7566 else if (proto
== Q_SCTP
)
7567 proto
= IPPROTO_SCTP
;
7568 else if (proto
== Q_DEFAULT
)
7569 proto
= PROTO_UNDEF
;
7571 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7574 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7578 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7579 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7584 bpf_error(cstate
, "'gateway' requires a name");
7588 return gen_proto(cstate
, v
, proto
, dir
);
7590 #if !defined(NO_PROTOCHAIN)
7592 return gen_protochain(cstate
, v
, proto
);
7608 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7611 struct addrinfo
*res
;
7612 struct in6_addr
*addr
;
7613 struct in6_addr mask
;
7615 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7618 * Catch errors reported by us and routines below us, and return NULL
7621 if (setjmp(cstate
->top_ctx
))
7624 res
= pcap_nametoaddrinfo(s
);
7626 bpf_error(cstate
, "invalid ip6 address %s", s
);
7629 bpf_error(cstate
, "%s resolved to multiple address", s
);
7630 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7632 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7633 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7634 memset(&mask
, 0, sizeof(mask
));
7635 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7637 mask
.s6_addr
[masklen
/ 8] =
7638 (0xff << (8 - masklen
% 8)) & 0xff;
7641 memcpy(a
, addr
, sizeof(a
));
7642 memcpy(m
, &mask
, sizeof(m
));
7643 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7644 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7645 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7653 bpf_error(cstate
, "Mask syntax for networks only");
7657 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7663 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7670 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7672 struct block
*b
, *tmp
;
7675 * Catch errors reported by us and routines below us, and return NULL
7678 if (setjmp(cstate
->top_ctx
))
7681 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7682 cstate
->e
= pcap_ether_aton(s
);
7683 if (cstate
->e
== NULL
)
7684 bpf_error(cstate
, "malloc");
7685 switch (cstate
->linktype
) {
7687 case DLT_NETANALYZER
:
7688 case DLT_NETANALYZER_TRANSPARENT
:
7689 tmp
= gen_prevlinkhdr_check(cstate
);
7690 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7695 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7698 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7700 case DLT_IEEE802_11
:
7701 case DLT_PRISM_HEADER
:
7702 case DLT_IEEE802_11_RADIO_AVS
:
7703 case DLT_IEEE802_11_RADIO
:
7705 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7707 case DLT_IP_OVER_FC
:
7708 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7713 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7720 bpf_error(cstate
, "ethernet address used in non-ether expression");
7725 sappend(struct slist
*s0
, struct slist
*s1
)
7728 * This is definitely not the best way to do this, but the
7729 * lists will rarely get long.
7736 static struct slist
*
7737 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7741 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7746 static struct slist
*
7747 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7751 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7757 * Modify "index" to use the value stored into its register as an
7758 * offset relative to the beginning of the header for the protocol
7759 * "proto", and allocate a register and put an item "size" bytes long
7760 * (1, 2, or 4) at that offset into that register, making it the register
7763 static struct arth
*
7764 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7768 struct slist
*s
, *tmp
;
7770 int regno
= alloc_reg(cstate
);
7772 free_reg(cstate
, inst
->regno
);
7776 bpf_error(cstate
, "data size must be 1, 2, or 4");
7793 bpf_error(cstate
, "unsupported index operation");
7797 * The offset is relative to the beginning of the packet
7798 * data, if we have a radio header. (If we don't, this
7801 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7802 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7803 cstate
->linktype
!= DLT_PRISM_HEADER
)
7804 bpf_error(cstate
, "radio information not present in capture");
7807 * Load into the X register the offset computed into the
7808 * register specified by "index".
7810 s
= xfer_to_x(cstate
, inst
);
7813 * Load the item at that offset.
7815 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7817 sappend(inst
->s
, s
);
7822 * The offset is relative to the beginning of
7823 * the link-layer header.
7825 * XXX - what about ATM LANE? Should the index be
7826 * relative to the beginning of the AAL5 frame, so
7827 * that 0 refers to the beginning of the LE Control
7828 * field, or relative to the beginning of the LAN
7829 * frame, so that 0 refers, for Ethernet LANE, to
7830 * the beginning of the destination address?
7832 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7835 * If "s" is non-null, it has code to arrange that the
7836 * X register contains the length of the prefix preceding
7837 * the link-layer header. Add to it the offset computed
7838 * into the register specified by "index", and move that
7839 * into the X register. Otherwise, just load into the X
7840 * register the offset computed into the register specified
7844 sappend(s
, xfer_to_a(cstate
, inst
));
7845 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7846 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7848 s
= xfer_to_x(cstate
, inst
);
7851 * Load the item at the sum of the offset we've put in the
7852 * X register and the offset of the start of the link
7853 * layer header (which is 0 if the radio header is
7854 * variable-length; that header length is what we put
7855 * into the X register and then added to the index).
7857 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7858 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7860 sappend(inst
->s
, s
);
7874 * The offset is relative to the beginning of
7875 * the network-layer header.
7876 * XXX - are there any cases where we want
7877 * cstate->off_nl_nosnap?
7879 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7882 * If "s" is non-null, it has code to arrange that the
7883 * X register contains the variable part of the offset
7884 * of the link-layer payload. Add to it the offset
7885 * computed into the register specified by "index",
7886 * and move that into the X register. Otherwise, just
7887 * load into the X register the offset computed into
7888 * the register specified by "index".
7891 sappend(s
, xfer_to_a(cstate
, inst
));
7892 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7893 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7895 s
= xfer_to_x(cstate
, inst
);
7898 * Load the item at the sum of the offset we've put in the
7899 * X register, the offset of the start of the network
7900 * layer header from the beginning of the link-layer
7901 * payload, and the constant part of the offset of the
7902 * start of the link-layer payload.
7904 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7905 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7907 sappend(inst
->s
, s
);
7910 * Do the computation only if the packet contains
7911 * the protocol in question.
7913 b
= gen_proto_abbrev_internal(cstate
, proto
);
7915 gen_and(inst
->b
, b
);
7929 * The offset is relative to the beginning of
7930 * the transport-layer header.
7932 * Load the X register with the length of the IPv4 header
7933 * (plus the offset of the link-layer header, if it's
7934 * a variable-length header), in bytes.
7936 * XXX - are there any cases where we want
7937 * cstate->off_nl_nosnap?
7938 * XXX - we should, if we're built with
7939 * IPv6 support, generate code to load either
7940 * IPv4, IPv6, or both, as appropriate.
7942 s
= gen_loadx_iphdrlen(cstate
);
7945 * The X register now contains the sum of the variable
7946 * part of the offset of the link-layer payload and the
7947 * length of the network-layer header.
7949 * Load into the A register the offset relative to
7950 * the beginning of the transport layer header,
7951 * add the X register to that, move that to the
7952 * X register, and load with an offset from the
7953 * X register equal to the sum of the constant part of
7954 * the offset of the link-layer payload and the offset,
7955 * relative to the beginning of the link-layer payload,
7956 * of the network-layer header.
7958 sappend(s
, xfer_to_a(cstate
, inst
));
7959 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7960 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7961 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7962 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7963 sappend(inst
->s
, s
);
7966 * Do the computation only if the packet contains
7967 * the protocol in question - which is true only
7968 * if this is an IP datagram and is the first or
7969 * only fragment of that datagram.
7971 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7973 gen_and(inst
->b
, b
);
7974 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7979 * Do the computation only if the packet contains
7980 * the protocol in question.
7982 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7984 gen_and(inst
->b
, b
);
7988 * Check if we have an icmp6 next header
7990 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7992 gen_and(inst
->b
, b
);
7995 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7997 * If "s" is non-null, it has code to arrange that the
7998 * X register contains the variable part of the offset
7999 * of the link-layer payload. Add to it the offset
8000 * computed into the register specified by "index",
8001 * and move that into the X register. Otherwise, just
8002 * load into the X register the offset computed into
8003 * the register specified by "index".
8006 sappend(s
, xfer_to_a(cstate
, inst
));
8007 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
8008 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
8010 s
= xfer_to_x(cstate
, inst
);
8013 * Load the item at the sum of the offset we've put in the
8014 * X register, the offset of the start of the network
8015 * layer header from the beginning of the link-layer
8016 * payload, and the constant part of the offset of the
8017 * start of the link-layer payload.
8019 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
8020 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
8023 sappend(inst
->s
, s
);
8027 inst
->regno
= regno
;
8028 s
= new_stmt(cstate
, BPF_ST
);
8030 sappend(inst
->s
, s
);
8036 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
8040 * Catch errors reported by us and routines below us, and return NULL
8043 if (setjmp(cstate
->top_ctx
))
8046 return gen_load_internal(cstate
, proto
, inst
, size
);
8049 static struct block
*
8050 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8051 struct arth
*a1
, int reversed
)
8053 struct slist
*s0
, *s1
, *s2
;
8054 struct block
*b
, *tmp
;
8056 s0
= xfer_to_x(cstate
, a1
);
8057 s1
= xfer_to_a(cstate
, a0
);
8058 if (code
== BPF_JEQ
) {
8059 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
8060 b
= new_block(cstate
, JMP(code
));
8064 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
8070 sappend(a0
->s
, a1
->s
);
8074 free_reg(cstate
, a0
->regno
);
8075 free_reg(cstate
, a1
->regno
);
8077 /* 'and' together protocol checks */
8080 gen_and(a0
->b
, tmp
= a1
->b
);
8094 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8095 struct arth
*a1
, int reversed
)
8098 * Catch errors reported by us and routines below us, and return NULL
8101 if (setjmp(cstate
->top_ctx
))
8104 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
8108 gen_loadlen(compiler_state_t
*cstate
)
8115 * Catch errors reported by us and routines below us, and return NULL
8118 if (setjmp(cstate
->top_ctx
))
8121 regno
= alloc_reg(cstate
);
8122 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8123 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8124 s
->next
= new_stmt(cstate
, BPF_ST
);
8125 s
->next
->s
.k
= regno
;
8132 static struct arth
*
8133 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8139 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8141 reg
= alloc_reg(cstate
);
8143 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8145 s
->next
= new_stmt(cstate
, BPF_ST
);
8154 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8157 * Catch errors reported by us and routines below us, and return NULL
8160 if (setjmp(cstate
->top_ctx
))
8163 return gen_loadi_internal(cstate
, val
);
8167 * The a_arg dance is to avoid annoying whining by compilers that
8168 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8169 * It's not *used* after setjmp returns.
8172 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8174 struct arth
*a
= a_arg
;
8178 * Catch errors reported by us and routines below us, and return NULL
8181 if (setjmp(cstate
->top_ctx
))
8184 s
= xfer_to_a(cstate
, a
);
8186 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8189 s
= new_stmt(cstate
, BPF_ST
);
8197 * The a0_arg dance is to avoid annoying whining by compilers that
8198 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8199 * It's not *used* after setjmp returns.
8202 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8205 struct arth
*a0
= a0_arg
;
8206 struct slist
*s0
, *s1
, *s2
;
8209 * Catch errors reported by us and routines below us, and return NULL
8212 if (setjmp(cstate
->top_ctx
))
8216 * Disallow division by, or modulus by, zero; we do this here
8217 * so that it gets done even if the optimizer is disabled.
8219 * Also disallow shifts by a value greater than 31; we do this
8220 * here, for the same reason.
8222 if (code
== BPF_DIV
) {
8223 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8224 bpf_error(cstate
, "division by zero");
8225 } else if (code
== BPF_MOD
) {
8226 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8227 bpf_error(cstate
, "modulus by zero");
8228 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8229 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8230 bpf_error(cstate
, "shift by more than 31 bits");
8232 s0
= xfer_to_x(cstate
, a1
);
8233 s1
= xfer_to_a(cstate
, a0
);
8234 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8239 sappend(a0
->s
, a1
->s
);
8241 free_reg(cstate
, a0
->regno
);
8242 free_reg(cstate
, a1
->regno
);
8244 s0
= new_stmt(cstate
, BPF_ST
);
8245 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8252 * Initialize the table of used registers and the current register.
8255 init_regs(compiler_state_t
*cstate
)
8258 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8262 * Return the next free register.
8265 alloc_reg(compiler_state_t
*cstate
)
8267 int n
= BPF_MEMWORDS
;
8270 if (cstate
->regused
[cstate
->curreg
])
8271 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8273 cstate
->regused
[cstate
->curreg
] = 1;
8274 return cstate
->curreg
;
8277 bpf_error(cstate
, "too many registers needed to evaluate expression");
8282 * Return a register to the table so it can
8286 free_reg(compiler_state_t
*cstate
, int n
)
8288 cstate
->regused
[n
] = 0;
8291 static struct block
*
8292 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8297 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8298 b
= new_block(cstate
, JMP(jmp
));
8306 gen_greater(compiler_state_t
*cstate
, int n
)
8309 * Catch errors reported by us and routines below us, and return NULL
8312 if (setjmp(cstate
->top_ctx
))
8315 return gen_len(cstate
, BPF_JGE
, n
);
8319 * Actually, this is less than or equal.
8322 gen_less(compiler_state_t
*cstate
, int n
)
8327 * Catch errors reported by us and routines below us, and return NULL
8330 if (setjmp(cstate
->top_ctx
))
8333 b
= gen_len(cstate
, BPF_JGT
, n
);
8340 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8341 * the beginning of the link-layer header.
8342 * XXX - that means you can't test values in the radiotap header, but
8343 * as that header is difficult if not impossible to parse generally
8344 * without a loop, that might not be a severe problem. A new keyword
8345 * "radio" could be added for that, although what you'd really want
8346 * would be a way of testing particular radio header values, which
8347 * would generate code appropriate to the radio header in question.
8350 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8356 * Catch errors reported by us and routines below us, and return NULL
8359 if (setjmp(cstate
->top_ctx
))
8367 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8370 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8374 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8378 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8382 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8386 b
= new_block(cstate
, JMP(BPF_JEQ
));
8394 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8396 bpf_u_int32 hostmask
;
8397 struct block
*b0
, *b1
, *b2
;
8398 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8401 * Catch errors reported by us and routines below us, and return NULL
8404 if (setjmp(cstate
->top_ctx
))
8411 switch (cstate
->linktype
) {
8413 case DLT_ARCNET_LINUX
:
8414 // ARCnet broadcast is [8-bit] destination address 0.
8415 return gen_ahostop(cstate
, 0, Q_DST
);
8417 case DLT_NETANALYZER
:
8418 case DLT_NETANALYZER_TRANSPARENT
:
8419 b1
= gen_prevlinkhdr_check(cstate
);
8420 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8425 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8427 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8428 case DLT_IEEE802_11
:
8429 case DLT_PRISM_HEADER
:
8430 case DLT_IEEE802_11_RADIO_AVS
:
8431 case DLT_IEEE802_11_RADIO
:
8433 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8434 case DLT_IP_OVER_FC
:
8435 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8437 bpf_error(cstate
, "not a broadcast link");
8443 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8444 * as an indication that we don't know the netmask, and fail
8447 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8448 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8449 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8450 hostmask
= ~cstate
->netmask
;
8451 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8452 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8453 ~0 & hostmask
, hostmask
);
8458 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8463 * Generate code to test the low-order bit of a MAC address (that's
8464 * the bottom bit of the *first* byte).
8466 static struct block
*
8467 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8469 register struct block
*b0
;
8470 register struct slist
*s
;
8472 /* link[offset] & 1 != 0 */
8473 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8474 b0
= new_block(cstate
, JMP(BPF_JSET
));
8481 gen_multicast(compiler_state_t
*cstate
, int proto
)
8483 register struct block
*b0
, *b1
, *b2
;
8484 register struct slist
*s
;
8487 * Catch errors reported by us and routines below us, and return NULL
8490 if (setjmp(cstate
->top_ctx
))
8497 switch (cstate
->linktype
) {
8499 case DLT_ARCNET_LINUX
:
8500 // ARCnet multicast is the same as broadcast.
8501 return gen_ahostop(cstate
, 0, Q_DST
);
8503 case DLT_NETANALYZER
:
8504 case DLT_NETANALYZER_TRANSPARENT
:
8505 b1
= gen_prevlinkhdr_check(cstate
);
8506 /* ether[0] & 1 != 0 */
8507 b0
= gen_mac_multicast(cstate
, 0);
8513 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8515 * XXX - was that referring to bit-order issues?
8517 /* fddi[1] & 1 != 0 */
8518 return gen_mac_multicast(cstate
, 1);
8520 /* tr[2] & 1 != 0 */
8521 return gen_mac_multicast(cstate
, 2);
8522 case DLT_IEEE802_11
:
8523 case DLT_PRISM_HEADER
:
8524 case DLT_IEEE802_11_RADIO_AVS
:
8525 case DLT_IEEE802_11_RADIO
:
8530 * For control frames, there is no DA.
8532 * For management frames, DA is at an
8533 * offset of 4 from the beginning of
8536 * For data frames, DA is at an offset
8537 * of 4 from the beginning of the packet
8538 * if To DS is clear and at an offset of
8539 * 16 from the beginning of the packet
8544 * Generate the tests to be done for data frames.
8546 * First, check for To DS set, i.e. "link[1] & 0x01".
8548 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8549 b1
= new_block(cstate
, JMP(BPF_JSET
));
8550 b1
->s
.k
= 0x01; /* To DS */
8554 * If To DS is set, the DA is at 16.
8556 b0
= gen_mac_multicast(cstate
, 16);
8560 * Now, check for To DS not set, i.e. check
8561 * "!(link[1] & 0x01)".
8563 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8564 b2
= new_block(cstate
, JMP(BPF_JSET
));
8565 b2
->s
.k
= 0x01; /* To DS */
8570 * If To DS is not set, the DA is at 4.
8572 b1
= gen_mac_multicast(cstate
, 4);
8576 * Now OR together the last two checks. That gives
8577 * the complete set of checks for data frames.
8582 * Now check for a data frame.
8583 * I.e, check "link[0] & 0x08".
8585 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8586 b1
= new_block(cstate
, JMP(BPF_JSET
));
8591 * AND that with the checks done for data frames.
8596 * If the high-order bit of the type value is 0, this
8597 * is a management frame.
8598 * I.e, check "!(link[0] & 0x08)".
8600 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8601 b2
= new_block(cstate
, JMP(BPF_JSET
));
8607 * For management frames, the DA is at 4.
8609 b1
= gen_mac_multicast(cstate
, 4);
8613 * OR that with the checks done for data frames.
8614 * That gives the checks done for management and
8620 * If the low-order bit of the type value is 1,
8621 * this is either a control frame or a frame
8622 * with a reserved type, and thus not a
8625 * I.e., check "!(link[0] & 0x04)".
8627 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8628 b1
= new_block(cstate
, JMP(BPF_JSET
));
8634 * AND that with the checks for data and management
8639 case DLT_IP_OVER_FC
:
8640 b0
= gen_mac_multicast(cstate
, 2);
8645 /* Link not known to support multicasts */
8649 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8650 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8655 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8656 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8660 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8666 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8667 * we can look at special meta-data in the filter expression; otherwise we
8668 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8669 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8670 * pcap_activate() conditionally sets.
8673 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8675 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8677 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8679 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8684 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8686 register struct block
*b0
;
8689 * Catch errors reported by us and routines below us, and return NULL
8692 if (setjmp(cstate
->top_ctx
))
8696 * Only some data link types support ifindex qualifiers.
8698 switch (cstate
->linktype
) {
8699 case DLT_LINUX_SLL2
:
8700 /* match packets on this interface */
8701 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8704 #if defined(__linux__)
8705 require_basic_bpf_extensions(cstate
, "ifindex");
8707 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8709 #else /* defined(__linux__) */
8710 bpf_error(cstate
, "ifindex not supported on %s",
8711 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8713 #endif /* defined(__linux__) */
8719 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8720 * Outbound traffic is sent by this machine, while inbound traffic is
8721 * sent by a remote machine (and may include packets destined for a
8722 * unicast or multicast link-layer address we are not subscribing to).
8723 * These are the same definitions implemented by pcap_setdirection().
8724 * Capturing only unicast traffic destined for this host is probably
8725 * better accomplished using a higher-layer filter.
8728 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8730 register struct block
*b0
;
8733 * Catch errors reported by us and routines below us, and return NULL
8736 if (setjmp(cstate
->top_ctx
))
8740 * Only some data link types support inbound/outbound qualifiers.
8742 switch (cstate
->linktype
) {
8744 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8745 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8746 gen_loadi_internal(cstate
, 0),
8747 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8751 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8752 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8756 /* match outgoing packets */
8757 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8759 /* to filter on inbound traffic, invert the match */
8764 case DLT_LINUX_SLL2
:
8765 /* match outgoing packets */
8766 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8768 /* to filter on inbound traffic, invert the match */
8774 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8775 outbound
? PF_OUT
: PF_IN
);
8779 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8782 case DLT_JUNIPER_MFR
:
8783 case DLT_JUNIPER_MLFR
:
8784 case DLT_JUNIPER_MLPPP
:
8785 case DLT_JUNIPER_ATM1
:
8786 case DLT_JUNIPER_ATM2
:
8787 case DLT_JUNIPER_PPPOE
:
8788 case DLT_JUNIPER_PPPOE_ATM
:
8789 case DLT_JUNIPER_GGSN
:
8790 case DLT_JUNIPER_ES
:
8791 case DLT_JUNIPER_MONITOR
:
8792 case DLT_JUNIPER_SERVICES
:
8793 case DLT_JUNIPER_ETHER
:
8794 case DLT_JUNIPER_PPP
:
8795 case DLT_JUNIPER_FRELAY
:
8796 case DLT_JUNIPER_CHDLC
:
8797 case DLT_JUNIPER_VP
:
8798 case DLT_JUNIPER_ST
:
8799 case DLT_JUNIPER_ISM
:
8800 case DLT_JUNIPER_VS
:
8801 case DLT_JUNIPER_SRX_E2E
:
8802 case DLT_JUNIPER_FIBRECHANNEL
:
8803 case DLT_JUNIPER_ATM_CEMIC
:
8804 /* juniper flags (including direction) are stored
8805 * the byte after the 3-byte magic number */
8806 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8811 * If we have packet meta-data indicating a direction,
8812 * and that metadata can be checked by BPF code, check
8813 * it. Otherwise, give up, as this link-layer type has
8814 * nothing in the packet data.
8816 * Currently, the only platform where a BPF filter can
8817 * check that metadata is Linux with the in-kernel
8818 * BPF interpreter. If other packet capture mechanisms
8819 * and BPF filters also supported this, it would be
8820 * nice. It would be even better if they made that
8821 * metadata available so that we could provide it
8822 * with newer capture APIs, allowing it to be saved
8825 #if defined(__linux__)
8826 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8827 /* match outgoing packets */
8828 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8831 /* to filter on inbound traffic, invert the match */
8834 #else /* defined(__linux__) */
8835 bpf_error(cstate
, "inbound/outbound not supported on %s",
8836 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8838 #endif /* defined(__linux__) */
8843 /* PF firewall log matched interface */
8845 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8851 * Catch errors reported by us and routines below us, and return NULL
8854 if (setjmp(cstate
->top_ctx
))
8857 if (cstate
->linktype
!= DLT_PFLOG
) {
8858 bpf_error(cstate
, "ifname supported only on PF linktype");
8861 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8862 off
= offsetof(struct pfloghdr
, ifname
);
8863 if (strlen(ifname
) >= len
) {
8864 bpf_error(cstate
, "ifname interface names can only be %d characters",
8868 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8869 (const u_char
*)ifname
);
8873 /* PF firewall log ruleset name */
8875 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8880 * Catch errors reported by us and routines below us, and return NULL
8883 if (setjmp(cstate
->top_ctx
))
8886 if (cstate
->linktype
!= DLT_PFLOG
) {
8887 bpf_error(cstate
, "ruleset supported only on PF linktype");
8891 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8892 bpf_error(cstate
, "ruleset names can only be %ld characters",
8893 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8897 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8898 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8902 /* PF firewall log rule number */
8904 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8909 * Catch errors reported by us and routines below us, and return NULL
8912 if (setjmp(cstate
->top_ctx
))
8915 if (cstate
->linktype
!= DLT_PFLOG
) {
8916 bpf_error(cstate
, "rnr supported only on PF linktype");
8920 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8925 /* PF firewall log sub-rule number */
8927 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8932 * Catch errors reported by us and routines below us, and return NULL
8935 if (setjmp(cstate
->top_ctx
))
8938 if (cstate
->linktype
!= DLT_PFLOG
) {
8939 bpf_error(cstate
, "srnr supported only on PF linktype");
8943 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8948 /* PF firewall log reason code */
8950 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8955 * Catch errors reported by us and routines below us, and return NULL
8958 if (setjmp(cstate
->top_ctx
))
8961 if (cstate
->linktype
!= DLT_PFLOG
) {
8962 bpf_error(cstate
, "reason supported only on PF linktype");
8966 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8967 (bpf_u_int32
)reason
);
8971 /* PF firewall log action */
8973 gen_pf_action(compiler_state_t
*cstate
, int action
)
8978 * Catch errors reported by us and routines below us, and return NULL
8981 if (setjmp(cstate
->top_ctx
))
8984 if (cstate
->linktype
!= DLT_PFLOG
) {
8985 bpf_error(cstate
, "action supported only on PF linktype");
8989 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8990 (bpf_u_int32
)action
);
8994 /* IEEE 802.11 wireless header */
8996 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
9001 * Catch errors reported by us and routines below us, and return NULL
9004 if (setjmp(cstate
->top_ctx
))
9007 switch (cstate
->linktype
) {
9009 case DLT_IEEE802_11
:
9010 case DLT_PRISM_HEADER
:
9011 case DLT_IEEE802_11_RADIO_AVS
:
9012 case DLT_IEEE802_11_RADIO
:
9013 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
9017 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
9025 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
9030 * Catch errors reported by us and routines below us, and return NULL
9033 if (setjmp(cstate
->top_ctx
))
9036 switch (cstate
->linktype
) {
9038 case DLT_IEEE802_11
:
9039 case DLT_PRISM_HEADER
:
9040 case DLT_IEEE802_11_RADIO_AVS
:
9041 case DLT_IEEE802_11_RADIO
:
9045 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
9049 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
9050 IEEE80211_FC1_DIR_MASK
);
9055 // Process an ARCnet host address string.
9057 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
9060 * Catch errors reported by us and routines below us, and return NULL
9063 if (setjmp(cstate
->top_ctx
))
9066 switch (cstate
->linktype
) {
9069 case DLT_ARCNET_LINUX
:
9070 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
9071 q
.proto
== Q_LINK
) {
9074 * The lexer currently defines the address format in a
9075 * way that makes this error condition never true.
9076 * Let's check it anyway in case this part of the lexer
9077 * changes in future.
9079 if (! pcapint_atoan(s
, &addr
))
9080 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
9081 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
9083 bpf_error(cstate
, "ARCnet address used in non-arc expression");
9087 bpf_error(cstate
, "aid supported only on ARCnet");
9092 // Compare an ARCnet host address with the given value.
9093 static struct block
*
9094 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
9096 register struct block
*b0
, *b1
;
9100 * ARCnet is different from Ethernet: the source address comes before
9101 * the destination address, each is one byte long. This holds for all
9102 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
9103 * in the 1983 edition of the "ARCNET Designer's Handbook" published
9104 * by Datapoint (document number 61610-01).
9107 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
9110 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
9113 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9114 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9120 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9121 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9126 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9130 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9134 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9138 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9142 bpf_error(cstate
, "'ra' is only supported on 802.11");
9146 bpf_error(cstate
, "'ta' is only supported on 802.11");
9153 static struct block
*
9154 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9156 struct block
*b0
, *b1
;
9158 /* check for VLAN, including 802.1ad and QinQ */
9159 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9160 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9163 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9169 static struct block
*
9170 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9172 if (vlan_num
> 0x0fff) {
9173 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9176 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9179 static struct block
*
9180 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9183 struct block
*b0
, *b1
;
9185 b0
= gen_vlan_tpid_test(cstate
);
9188 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9194 * Both payload and link header type follow the VLAN tags so that
9195 * both need to be updated.
9197 cstate
->off_linkpl
.constant_part
+= 4;
9198 cstate
->off_linktype
.constant_part
+= 4;
9203 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9204 /* add v to variable part of off */
9206 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9207 bpf_u_int32 v
, struct slist
*s
)
9211 if (!off
->is_variable
)
9212 off
->is_variable
= 1;
9214 off
->reg
= alloc_reg(cstate
);
9216 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9219 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9222 s2
= new_stmt(cstate
, BPF_ST
);
9228 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9229 * and link type offsets first
9232 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9236 /* offset determined at run time, shift variable part */
9238 cstate
->is_vlan_vloffset
= 1;
9239 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9240 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9242 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9243 sappend(s
.next
, b_tpid
->head
->stmts
);
9244 b_tpid
->head
->stmts
= s
.next
;
9248 * patch block b_vid (VLAN id test) to load VID value either from packet
9249 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9252 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9254 struct slist
*s
, *s2
, *sjeq
;
9257 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9258 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9260 /* true -> next instructions, false -> beginning of b_vid */
9261 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9263 sjeq
->s
.jf
= b_vid
->stmts
;
9266 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
9267 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
9271 /* Jump to the test in b_vid. We need to jump one instruction before
9272 * the end of the b_vid block so that we only skip loading the TCI
9273 * from packet data and not the 'and' instruction extracting VID.
9276 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9278 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9282 /* insert our statements at the beginning of b_vid */
9283 sappend(s
, b_vid
->stmts
);
9288 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9289 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9290 * tag can be either in metadata or in packet data; therefore if the
9291 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9292 * header for VLAN tag. As the decision is done at run time, we need
9293 * update variable part of the offsets
9295 static struct block
*
9296 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9299 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9302 /* generate new filter code based on extracting packet
9304 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9305 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9307 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9312 * This is tricky. We need to insert the statements updating variable
9313 * parts of offsets before the traditional TPID and VID tests so
9314 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9315 * we do not want this update to affect those checks. That's why we
9316 * generate both test blocks first and insert the statements updating
9317 * variable parts of both offsets after that. This wouldn't work if
9318 * there already were variable length link header when entering this
9319 * function but gen_vlan_bpf_extensions() isn't called in that case.
9321 b_tpid
= gen_vlan_tpid_test(cstate
);
9323 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9325 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9330 gen_vlan_patch_vid_test(cstate
, b_vid
);
9340 * support IEEE 802.1Q VLAN trunk over ethernet
9343 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9348 * Catch errors reported by us and routines below us, and return NULL
9351 if (setjmp(cstate
->top_ctx
))
9354 /* can't check for VLAN-encapsulated packets inside MPLS */
9355 if (cstate
->label_stack_depth
> 0)
9356 bpf_error(cstate
, "no VLAN match after MPLS");
9359 * Check for a VLAN packet, and then change the offsets to point
9360 * to the type and data fields within the VLAN packet. Just
9361 * increment the offsets, so that we can support a hierarchy, e.g.
9362 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9365 * XXX - this is a bit of a kludge. If we were to split the
9366 * compiler into a parser that parses an expression and
9367 * generates an expression tree, and a code generator that
9368 * takes an expression tree (which could come from our
9369 * parser or from some other parser) and generates BPF code,
9370 * we could perhaps make the offsets parameters of routines
9371 * and, in the handler for an "AND" node, pass to subnodes
9372 * other than the VLAN node the adjusted offsets.
9374 * This would mean that "vlan" would, instead of changing the
9375 * behavior of *all* tests after it, change only the behavior
9376 * of tests ANDed with it. That would change the documented
9377 * semantics of "vlan", which might break some expressions.
9378 * However, it would mean that "(vlan and ip) or ip" would check
9379 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9380 * checking only for VLAN-encapsulated IP, so that could still
9381 * be considered worth doing; it wouldn't break expressions
9382 * that are of the form "vlan and ..." or "vlan N and ...",
9383 * which I suspect are the most common expressions involving
9384 * "vlan". "vlan or ..." doesn't necessarily do what the user
9385 * would really want, now, as all the "or ..." tests would
9386 * be done assuming a VLAN, even though the "or" could be viewed
9387 * as meaning "or, if this isn't a VLAN packet...".
9389 switch (cstate
->linktype
) {
9393 * Newer version of the Linux kernel pass around
9394 * packets in which the VLAN tag has been removed
9395 * from the packet data and put into metadata.
9397 * This requires special treatment.
9399 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9400 /* Verify that this is the outer part of the packet and
9401 * not encapsulated somehow. */
9402 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9403 cstate
->off_linkhdr
.constant_part
==
9404 cstate
->off_outermostlinkhdr
.constant_part
) {
9406 * Do we need special VLAN handling?
9408 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9409 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9412 b0
= gen_vlan_no_bpf_extensions(cstate
,
9413 vlan_num
, has_vlan_tag
);
9416 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9420 case DLT_NETANALYZER
:
9421 case DLT_NETANALYZER_TRANSPARENT
:
9422 case DLT_IEEE802_11
:
9423 case DLT_PRISM_HEADER
:
9424 case DLT_IEEE802_11_RADIO_AVS
:
9425 case DLT_IEEE802_11_RADIO
:
9427 * These are either Ethernet packets with an additional
9428 * metadata header (the NetAnalyzer types), or 802.11
9429 * packets, possibly with an additional metadata header.
9431 * For the first of those, the VLAN tag is in the normal
9432 * place, so the special-case handling above isn't
9435 * For the second of those, we don't do the special-case
9438 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9442 bpf_error(cstate
, "no VLAN support for %s",
9443 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9447 cstate
->vlan_stack_depth
++;
9455 * The label_num_arg dance is to avoid annoying whining by compilers that
9456 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9457 * It's not *used* after setjmp returns.
9459 static struct block
*
9460 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9463 struct block
*b0
, *b1
;
9465 if (cstate
->label_stack_depth
> 0) {
9466 /* just match the bottom-of-stack bit clear */
9467 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9470 * We're not in an MPLS stack yet, so check the link-layer
9471 * type against MPLS.
9473 switch (cstate
->linktype
) {
9475 case DLT_C_HDLC
: /* fall through */
9478 case DLT_NETANALYZER
:
9479 case DLT_NETANALYZER_TRANSPARENT
:
9480 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9484 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9487 /* FIXME add other DLT_s ...
9488 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9489 * leave it for now */
9492 bpf_error(cstate
, "no MPLS support for %s",
9493 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9498 /* If a specific MPLS label is requested, check it */
9499 if (has_label_num
) {
9500 if (label_num
> 0xFFFFF) {
9501 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9502 label_num
, 0xFFFFF);
9504 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9505 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9506 0xfffff000); /* only compare the first 20 bits */
9512 * Change the offsets to point to the type and data fields within
9513 * the MPLS packet. Just increment the offsets, so that we
9514 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9515 * capture packets with an outer label of 100000 and an inner
9518 * Increment the MPLS stack depth as well; this indicates that
9519 * we're checking MPLS-encapsulated headers, to make sure higher
9520 * level code generators don't try to match against IP-related
9521 * protocols such as Q_ARP, Q_RARP etc.
9523 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9525 cstate
->off_nl_nosnap
+= 4;
9526 cstate
->off_nl
+= 4;
9527 cstate
->label_stack_depth
++;
9532 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9535 * Catch errors reported by us and routines below us, and return NULL
9538 if (setjmp(cstate
->top_ctx
))
9541 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9545 * Support PPPOE discovery and session.
9548 gen_pppoed(compiler_state_t
*cstate
)
9551 * Catch errors reported by us and routines below us, and return NULL
9554 if (setjmp(cstate
->top_ctx
))
9557 /* check for PPPoE discovery */
9558 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9562 * RFC 2516 Section 4:
9564 * The Ethernet payload for PPPoE is as follows:
9567 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
9568 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9569 * | VER | TYPE | CODE | SESSION_ID |
9570 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9571 * | LENGTH | payload ~
9572 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9575 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9577 struct block
*b0
, *b1
;
9580 * Catch errors reported by us and routines below us, and return NULL
9583 if (setjmp(cstate
->top_ctx
))
9587 * Test against the PPPoE session link-layer type.
9589 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9591 /* If a specific session is requested, check PPPoE session id */
9593 if (sess_num
> UINT16_MAX
) {
9594 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9595 sess_num
, UINT16_MAX
);
9597 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9603 * Change the offsets to point to the type and data fields within
9604 * the PPP packet, and note that this is PPPoE rather than
9607 * XXX - this is a bit of a kludge. See the comments in
9610 * The "network-layer" protocol is PPPoE, which has a 6-byte
9611 * PPPoE header, followed by a PPP packet.
9613 * There is no HDLC encapsulation for the PPP packet (it's
9614 * encapsulated in PPPoES instead), so the link-layer type
9615 * starts at the first byte of the PPP packet. For PPPoE,
9616 * that offset is relative to the beginning of the total
9617 * link-layer payload, including any 802.2 LLC header, so
9618 * it's 6 bytes past cstate->off_nl.
9620 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9621 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9622 cstate
->off_linkpl
.reg
);
9624 cstate
->off_linktype
= cstate
->off_linkhdr
;
9625 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9628 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9633 /* Check that this is Geneve and the VNI is correct if
9634 * specified. Parameterized to handle both IPv4 and IPv6. */
9635 static struct block
*
9636 gen_geneve_check(compiler_state_t
*cstate
,
9637 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9638 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9640 struct block
*b0
, *b1
;
9642 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9644 /* Check that we are operating on version 0. Otherwise, we
9645 * can't decode the rest of the fields. The version is 2 bits
9646 * in the first byte of the Geneve header. */
9647 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9652 if (vni
> 0xffffff) {
9653 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9656 vni
<<= 8; /* VNI is in the upper 3 bytes */
9657 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9665 /* The IPv4 and IPv6 Geneve checks need to do two things:
9666 * - Verify that this actually is Geneve with the right VNI.
9667 * - Place the IP header length (plus variable link prefix if
9668 * needed) into register A to be used later to compute
9669 * the inner packet offsets. */
9670 static struct block
*
9671 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9673 struct block
*b0
, *b1
;
9674 struct slist
*s
, *s1
;
9676 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9678 /* Load the IP header length into A. */
9679 s
= gen_loadx_iphdrlen(cstate
);
9681 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9684 /* Forcibly append these statements to the true condition
9685 * of the protocol check by creating a new block that is
9686 * always true and ANDing them. */
9687 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9696 static struct block
*
9697 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9699 struct block
*b0
, *b1
;
9700 struct slist
*s
, *s1
;
9702 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9704 /* Load the IP header length. We need to account for a
9705 * variable length link prefix if there is one. */
9706 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9708 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9712 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9716 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9720 /* Forcibly append these statements to the true condition
9721 * of the protocol check by creating a new block that is
9722 * always true and ANDing them. */
9723 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9726 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9735 /* We need to store three values based on the Geneve header::
9736 * - The offset of the linktype.
9737 * - The offset of the end of the Geneve header.
9738 * - The offset of the end of the encapsulated MAC header. */
9739 static struct slist
*
9740 gen_geneve_offsets(compiler_state_t
*cstate
)
9742 struct slist
*s
, *s1
, *s_proto
;
9744 /* First we need to calculate the offset of the Geneve header
9745 * itself. This is composed of the IP header previously calculated
9746 * (include any variable link prefix) and stored in A plus the
9747 * fixed sized headers (fixed link prefix, MAC length, and UDP
9749 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9750 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9752 /* Stash this in X since we'll need it later. */
9753 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9756 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9758 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9762 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9763 cstate
->off_linktype
.is_variable
= 1;
9764 cstate
->off_linktype
.constant_part
= 0;
9766 s1
= new_stmt(cstate
, BPF_ST
);
9767 s1
->s
.k
= cstate
->off_linktype
.reg
;
9770 /* Load the Geneve option length and mask and shift to get the
9771 * number of bytes. It is stored in the first byte of the Geneve
9773 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9777 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9781 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9785 /* Add in the rest of the Geneve base header. */
9786 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9790 /* Add the Geneve header length to its offset and store. */
9791 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9795 /* Set the encapsulated type as Ethernet. Even though we may
9796 * not actually have Ethernet inside there are two reasons this
9798 * - The linktype field is always in EtherType format regardless
9799 * of whether it is in Geneve or an inner Ethernet frame.
9800 * - The only link layer that we have specific support for is
9801 * Ethernet. We will confirm that the packet actually is
9802 * Ethernet at runtime before executing these checks. */
9803 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9805 s1
= new_stmt(cstate
, BPF_ST
);
9806 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9809 /* Calculate whether we have an Ethernet header or just raw IP/
9810 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9811 * and linktype by 14 bytes so that the network header can be found
9812 * seamlessly. Otherwise, keep what we've calculated already. */
9814 /* We have a bare jmp so we can't use the optimizer. */
9815 cstate
->no_optimize
= 1;
9817 /* Load the EtherType in the Geneve header, 2 bytes in. */
9818 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9822 /* Load X with the end of the Geneve header. */
9823 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9824 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9827 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9828 * end of this check, we should have the total length in X. In
9829 * the non-Ethernet case, it's already there. */
9830 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9831 s_proto
->s
.k
= ETHERTYPE_TEB
;
9832 sappend(s
, s_proto
);
9834 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9838 /* Since this is Ethernet, use the EtherType of the payload
9839 * directly as the linktype. Overwrite what we already have. */
9840 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9844 s1
= new_stmt(cstate
, BPF_ST
);
9845 s1
->s
.k
= cstate
->off_linktype
.reg
;
9848 /* Advance two bytes further to get the end of the Ethernet
9850 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9854 /* Move the result to X. */
9855 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9858 /* Store the final result of our linkpl calculation. */
9859 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9860 cstate
->off_linkpl
.is_variable
= 1;
9861 cstate
->off_linkpl
.constant_part
= 0;
9863 s1
= new_stmt(cstate
, BPF_STX
);
9864 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9873 /* Check to see if this is a Geneve packet. */
9875 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9877 struct block
*b0
, *b1
;
9881 * Catch errors reported by us and routines below us, and return NULL
9884 if (setjmp(cstate
->top_ctx
))
9887 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9888 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9893 /* Later filters should act on the payload of the Geneve frame,
9894 * update all of the header pointers. Attach this code so that
9895 * it gets executed in the event that the Geneve filter matches. */
9896 s
= gen_geneve_offsets(cstate
);
9898 b1
= gen_true(cstate
);
9899 sappend(s
, b1
->stmts
);
9904 cstate
->is_encap
= 1;
9909 /* Check that this is VXLAN and the VNI is correct if
9910 * specified. Parameterized to handle both IPv4 and IPv6. */
9911 static struct block
*
9912 gen_vxlan_check(compiler_state_t
*cstate
,
9913 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9914 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9916 struct block
*b0
, *b1
;
9918 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9920 /* Check that the VXLAN header has the flag bits set
9922 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9927 if (vni
> 0xffffff) {
9928 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9931 vni
<<= 8; /* VNI is in the upper 3 bytes */
9932 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9940 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9941 * - Verify that this actually is VXLAN with the right VNI.
9942 * - Place the IP header length (plus variable link prefix if
9943 * needed) into register A to be used later to compute
9944 * the inner packet offsets. */
9945 static struct block
*
9946 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9948 struct block
*b0
, *b1
;
9949 struct slist
*s
, *s1
;
9951 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9953 /* Load the IP header length into A. */
9954 s
= gen_loadx_iphdrlen(cstate
);
9956 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9959 /* Forcibly append these statements to the true condition
9960 * of the protocol check by creating a new block that is
9961 * always true and ANDing them. */
9962 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9971 static struct block
*
9972 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9974 struct block
*b0
, *b1
;
9975 struct slist
*s
, *s1
;
9977 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9979 /* Load the IP header length. We need to account for a
9980 * variable length link prefix if there is one. */
9981 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9983 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9987 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9991 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9995 /* Forcibly append these statements to the true condition
9996 * of the protocol check by creating a new block that is
9997 * always true and ANDing them. */
9998 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10001 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10010 /* We need to store three values based on the VXLAN header:
10011 * - The offset of the linktype.
10012 * - The offset of the end of the VXLAN header.
10013 * - The offset of the end of the encapsulated MAC header. */
10014 static struct slist
*
10015 gen_vxlan_offsets(compiler_state_t
*cstate
)
10017 struct slist
*s
, *s1
;
10019 /* Calculate the offset of the VXLAN header itself. This
10020 * includes the IP header computed previously (including any
10021 * variable link prefix) and stored in A plus the fixed size
10022 * headers (fixed link prefix, MAC length, UDP header). */
10023 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10024 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
10026 /* Add the VXLAN header length to its offset and store */
10027 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10031 /* Push the link header. VXLAN packets always contain Ethernet
10033 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
10035 s1
= new_stmt(cstate
, BPF_ST
);
10036 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
10039 /* As the payload is an Ethernet packet, we can use the
10040 * EtherType of the payload directly as the linktype. */
10041 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10045 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
10046 cstate
->off_linktype
.is_variable
= 1;
10047 cstate
->off_linktype
.constant_part
= 0;
10049 s1
= new_stmt(cstate
, BPF_ST
);
10050 s1
->s
.k
= cstate
->off_linktype
.reg
;
10053 /* Two bytes further is the end of the Ethernet header and the
10054 * start of the payload. */
10055 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10059 /* Move the result to X. */
10060 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10063 /* Store the final result of our linkpl calculation. */
10064 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
10065 cstate
->off_linkpl
.is_variable
= 1;
10066 cstate
->off_linkpl
.constant_part
= 0;
10068 s1
= new_stmt(cstate
, BPF_STX
);
10069 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10072 cstate
->off_nl
= 0;
10077 /* Check to see if this is a VXLAN packet. */
10079 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
10081 struct block
*b0
, *b1
;
10085 * Catch errors reported by us and routines below us, and return NULL
10088 if (setjmp(cstate
->top_ctx
))
10091 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
10092 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
10097 /* Later filters should act on the payload of the VXLAN frame,
10098 * update all of the header pointers. Attach this code so that
10099 * it gets executed in the event that the VXLAN filter matches. */
10100 s
= gen_vxlan_offsets(cstate
);
10102 b1
= gen_true(cstate
);
10103 sappend(s
, b1
->stmts
);
10108 cstate
->is_encap
= 1;
10113 /* Check that the encapsulated frame has a link layer header
10114 * for Ethernet filters. */
10115 static struct block
*
10116 gen_encap_ll_check(compiler_state_t
*cstate
)
10119 struct slist
*s
, *s1
;
10121 /* The easiest way to see if there is a link layer present
10122 * is to check if the link layer header and payload are not
10125 /* Geneve always generates pure variable offsets so we can
10126 * compare only the registers. */
10127 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
10128 s
->s
.k
= cstate
->off_linkhdr
.reg
;
10130 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
10131 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10134 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10142 static struct block
*
10143 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
10144 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10148 switch (atmfield
) {
10151 if (!cstate
->is_atm
)
10152 bpf_error(cstate
, "'vpi' supported only on raw ATM");
10153 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10155 if (jvalue
> UINT8_MAX
)
10156 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
10157 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10158 0xffffffffU
, jtype
, reverse
, jvalue
);
10162 if (!cstate
->is_atm
)
10163 bpf_error(cstate
, "'vci' supported only on raw ATM");
10164 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10166 if (jvalue
> UINT16_MAX
)
10167 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
10168 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10169 0xffffffffU
, jtype
, reverse
, jvalue
);
10173 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10174 abort(); /* XXX - this isn't on FreeBSD */
10175 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10176 0x0fU
, jtype
, reverse
, jvalue
);
10180 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10182 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10183 0xffffffffU
, jtype
, reverse
, jvalue
);
10186 case A_CALLREFTYPE
:
10187 if (!cstate
->is_atm
)
10188 bpf_error(cstate
, "'callref' supported only on raw ATM");
10189 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10191 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10192 0xffffffffU
, jtype
, reverse
, jvalue
);
10201 static struct block
*
10202 gen_atmtype_metac(compiler_state_t
*cstate
)
10204 struct block
*b0
, *b1
;
10206 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10207 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10212 static struct block
*
10213 gen_atmtype_sc(compiler_state_t
*cstate
)
10215 struct block
*b0
, *b1
;
10217 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10218 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10223 static struct block
*
10224 gen_atmtype_llc(compiler_state_t
*cstate
)
10228 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10229 cstate
->linktype
= cstate
->prevlinktype
;
10234 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10235 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10238 * Catch errors reported by us and routines below us, and return NULL
10241 if (setjmp(cstate
->top_ctx
))
10244 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10249 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10251 struct block
*b0
, *b1
;
10254 * Catch errors reported by us and routines below us, and return NULL
10257 if (setjmp(cstate
->top_ctx
))
10263 /* Get all packets in Meta signalling Circuit */
10264 if (!cstate
->is_atm
)
10265 bpf_error(cstate
, "'metac' supported only on raw ATM");
10266 b1
= gen_atmtype_metac(cstate
);
10270 /* Get all packets in Broadcast Circuit*/
10271 if (!cstate
->is_atm
)
10272 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10273 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10274 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10279 /* Get all cells in Segment OAM F4 circuit*/
10280 if (!cstate
->is_atm
)
10281 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10282 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10283 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10288 /* Get all cells in End-to-End OAM F4 Circuit*/
10289 if (!cstate
->is_atm
)
10290 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10291 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10292 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10297 /* Get all packets in connection Signalling Circuit */
10298 if (!cstate
->is_atm
)
10299 bpf_error(cstate
, "'sc' supported only on raw ATM");
10300 b1
= gen_atmtype_sc(cstate
);
10304 /* Get all packets in ILMI Circuit */
10305 if (!cstate
->is_atm
)
10306 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10307 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10308 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10313 /* Get all LANE packets */
10314 if (!cstate
->is_atm
)
10315 bpf_error(cstate
, "'lane' supported only on raw ATM");
10316 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10319 * Arrange that all subsequent tests assume LANE
10320 * rather than LLC-encapsulated packets, and set
10321 * the offsets appropriately for LANE-encapsulated
10324 * We assume LANE means Ethernet, not Token Ring.
10326 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10327 cstate
->off_payload
+ 2, /* Ethernet header */
10329 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10330 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10331 cstate
->off_nl
= 0; /* Ethernet II */
10332 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10336 /* Get all LLC-encapsulated packets */
10337 if (!cstate
->is_atm
)
10338 bpf_error(cstate
, "'llc' supported only on raw ATM");
10339 b1
= gen_atmtype_llc(cstate
);
10349 * Filtering for MTP2 messages based on li value
10350 * FISU, length is null
10351 * LSSU, length is 1 or 2
10352 * MSU, length is 3 or more
10353 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10356 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10358 struct block
*b0
, *b1
;
10361 * Catch errors reported by us and routines below us, and return NULL
10364 if (setjmp(cstate
->top_ctx
))
10370 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10371 (cstate
->linktype
!= DLT_ERF
) &&
10372 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10373 bpf_error(cstate
, "'fisu' supported only on MTP2");
10374 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10375 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10376 0x3fU
, BPF_JEQ
, 0, 0U);
10380 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10381 (cstate
->linktype
!= DLT_ERF
) &&
10382 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10383 bpf_error(cstate
, "'lssu' supported only on MTP2");
10384 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10385 0x3fU
, BPF_JGT
, 1, 2U);
10386 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10387 0x3fU
, BPF_JGT
, 0, 0U);
10392 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10393 (cstate
->linktype
!= DLT_ERF
) &&
10394 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10395 bpf_error(cstate
, "'msu' supported only on MTP2");
10396 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10397 0x3fU
, BPF_JGT
, 0, 2U);
10401 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10402 (cstate
->linktype
!= DLT_ERF
) &&
10403 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10404 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10405 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10406 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10407 0xff80U
, BPF_JEQ
, 0, 0U);
10411 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10412 (cstate
->linktype
!= DLT_ERF
) &&
10413 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10414 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10415 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10416 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10417 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10418 0xff80U
, BPF_JGT
, 0, 0U);
10423 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10424 (cstate
->linktype
!= DLT_ERF
) &&
10425 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10426 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10427 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10428 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10437 static struct block
*
10438 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10439 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10442 bpf_u_int32 val1
, val2
, val3
;
10448 newoff_sio
= cstate
->off_sio
;
10449 newoff_opc
= cstate
->off_opc
;
10450 newoff_dpc
= cstate
->off_dpc
;
10451 newoff_sls
= cstate
->off_sls
;
10452 switch (mtp3field
) {
10455 newoff_sio
+= 3; /* offset for MTP2_HSL */
10459 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10460 bpf_error(cstate
, "'sio' supported only on SS7");
10461 /* sio coded on 1 byte so max value 255 */
10463 bpf_error(cstate
, "sio value %u too big; max value = 255",
10465 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
10466 jtype
, reverse
, jvalue
);
10474 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10475 bpf_error(cstate
, "'opc' supported only on SS7");
10476 /* opc coded on 14 bits so max value 16383 */
10477 if (jvalue
> 16383)
10478 bpf_error(cstate
, "opc value %u too big; max value = 16383",
10480 /* the following instructions are made to convert jvalue
10481 * to the form used to write opc in an ss7 message*/
10482 val1
= jvalue
& 0x00003c00;
10484 val2
= jvalue
& 0x000003fc;
10486 val3
= jvalue
& 0x00000003;
10488 jvalue
= val1
+ val2
+ val3
;
10489 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
10490 jtype
, reverse
, jvalue
);
10498 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10499 bpf_error(cstate
, "'dpc' supported only on SS7");
10500 /* dpc coded on 14 bits so max value 16383 */
10501 if (jvalue
> 16383)
10502 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
10504 /* the following instructions are made to convert jvalue
10505 * to the forme used to write dpc in an ss7 message*/
10506 val1
= jvalue
& 0x000000ff;
10508 val2
= jvalue
& 0x00003f00;
10510 jvalue
= val1
+ val2
;
10511 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
10512 jtype
, reverse
, jvalue
);
10520 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10521 bpf_error(cstate
, "'sls' supported only on SS7");
10522 /* sls coded on 4 bits so max value 15 */
10524 bpf_error(cstate
, "sls value %u too big; max value = 15",
10526 /* the following instruction is made to convert jvalue
10527 * to the forme used to write sls in an ss7 message*/
10528 jvalue
= jvalue
<< 4;
10529 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
10530 jtype
, reverse
, jvalue
);
10540 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10541 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10544 * Catch errors reported by us and routines below us, and return NULL
10547 if (setjmp(cstate
->top_ctx
))
10550 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10554 static struct block
*
10555 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10560 * Q.2931 signalling protocol messages for handling virtual circuits
10561 * establishment and teardown
10566 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10569 case A_CALLPROCEED
:
10570 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10574 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10578 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10582 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10585 case A_RELEASE_DONE
:
10586 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10596 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10598 struct block
*b0
, *b1
;
10601 * Catch errors reported by us and routines below us, and return NULL
10604 if (setjmp(cstate
->top_ctx
))
10610 if (!cstate
->is_atm
)
10611 bpf_error(cstate
, "'oam' supported only on raw ATM");
10613 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10614 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10616 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10621 if (!cstate
->is_atm
)
10622 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10624 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10625 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10627 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10633 * Get Q.2931 signalling messages for switched
10634 * virtual connection
10636 if (!cstate
->is_atm
)
10637 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10638 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10639 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10641 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10643 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10645 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10647 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10649 b0
= gen_atmtype_sc(cstate
);
10653 case A_METACONNECT
:
10654 if (!cstate
->is_atm
)
10655 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10656 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10657 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10659 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10661 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10663 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10665 b0
= gen_atmtype_metac(cstate
);