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>
43 #include "ethertype.h"
46 #include "ieee80211.h"
50 #include "pcap/ipnet.h"
51 #include "diag-control.h"
52 #include "pcap-util.h"
56 #if defined(__linux__)
57 #include <linux/types.h>
58 #include <linux/if_packet.h>
59 #include <linux/filter.h>
63 #ifdef HAVE_NPCAP_BPF_H
64 /* Defines BPF extensions for Npcap */
65 #include <npcap-bpf.h>
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) */
111 #include <netdb.h> /* for "struct addrinfo" */
113 #include <pcap/namedb.h>
115 #include "nametoaddr.h"
117 #define ETHERMTU 1500
119 #ifndef IPPROTO_HOPOPTS
120 #define IPPROTO_HOPOPTS 0
122 #ifndef IPPROTO_ROUTING
123 #define IPPROTO_ROUTING 43
125 #ifndef IPPROTO_FRAGMENT
126 #define IPPROTO_FRAGMENT 44
128 #ifndef IPPROTO_DSTOPTS
129 #define IPPROTO_DSTOPTS 60
132 #define IPPROTO_SCTP 132
135 #define GENEVE_PORT 6081
136 #define VXLAN_PORT 4789
140 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
144 #define ARCTYPE_IP_OLD 240 /* IP protocol */
145 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
148 #define ARCTYPE_IP 212 /* IP protocol */
149 #define ARCTYPE_ARP 213 /* address resolution protocol */
150 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
152 #define ARCTYPE_ATALK 221 /* Appletalk */
153 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
154 #define ARCTYPE_IPX 250 /* Novell IPX */
156 #define ARCTYPE_INET6 0xc4 /* IPng */
157 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
160 /* Based on UNI3.1 standard by ATM Forum */
162 /* ATM traffic types based on VPI=0 and (the following VCI */
163 #define VCI_PPC 0x05 /* Point-to-point signal msg */
164 #define VCI_BCC 0x02 /* Broadcast signal msg */
165 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
166 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
167 #define VCI_METAC 0x01 /* Meta signal msg */
168 #define VCI_ILMIC 0x10 /* ILMI msg */
170 /* Q.2931 signalling messages */
171 #define CALL_PROCEED 0x02 /* call proceeding */
172 #define CONNECT 0x07 /* connect */
173 #define CONNECT_ACK 0x0f /* connect_ack */
174 #define SETUP 0x05 /* setup */
175 #define RELEASE 0x4d /* release */
176 #define RELEASE_DONE 0x5a /* release_done */
177 #define RESTART 0x46 /* restart */
178 #define RESTART_ACK 0x4e /* restart ack */
179 #define STATUS 0x7d /* status */
180 #define STATUS_ENQ 0x75 /* status ack */
181 #define ADD_PARTY 0x80 /* add party */
182 #define ADD_PARTY_ACK 0x81 /* add party ack */
183 #define ADD_PARTY_REJ 0x82 /* add party rej */
184 #define DROP_PARTY 0x83 /* drop party */
185 #define DROP_PARTY_ACK 0x84 /* drop party ack */
187 /* Information Element Parameters in the signalling messages */
188 #define CAUSE 0x08 /* cause */
189 #define ENDPT_REF 0x54 /* endpoint reference */
190 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
191 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
192 #define CONNECT_ID 0x5a /* connection identifier */
193 #define QOS_PARA 0x5c /* quality of service parameters */
194 #define B_HIGHER 0x5d /* broadband higher layer information */
195 #define B_BEARER 0x5e /* broadband bearer capability */
196 #define B_LOWER 0x5f /* broadband lower information */
197 #define CALLING_PARTY 0x6c /* calling party number */
198 #define CALLED_PARTY 0x70 /* called party number */
202 /* Q.2931 signalling general messages format */
203 #define PROTO_POS 0 /* offset of protocol discriminator */
204 #define CALL_REF_POS 2 /* offset of call reference value */
205 #define MSG_TYPE_POS 5 /* offset of message type */
206 #define MSG_LEN_POS 7 /* offset of message length */
207 #define IE_BEGIN_POS 9 /* offset of first information element */
209 /* format of signalling messages */
212 #define FIELD_BEGIN_POS 4
215 /* SunATM header for ATM packet */
216 #define SUNATM_DIR_POS 0
217 #define SUNATM_VPI_POS 1
218 #define SUNATM_VCI_POS 2
219 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
221 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
222 #define PT_LANE 0x01 /* LANE */
223 #define PT_LLC 0x02 /* LLC encapsulation */
224 #define PT_ILMI 0x05 /* ILMI */
225 #define PT_QSAAL 0x06 /* Q.SAAL */
228 /* Types missing from some systems */
231 * Network layer protocol identifiers
234 #define ISO8473_CLNP 0x81
237 #define ISO9542_ESIS 0x82
239 #ifndef ISO9542X25_ESIS
240 #define ISO9542X25_ESIS 0x8a
242 #ifndef ISO10589_ISIS
243 #define ISO10589_ISIS 0x83
246 #define ISIS_L1_LAN_IIH 15
247 #define ISIS_L2_LAN_IIH 16
248 #define ISIS_PTP_IIH 17
249 #define ISIS_L1_LSP 18
250 #define ISIS_L2_LSP 20
251 #define ISIS_L1_CSNP 24
252 #define ISIS_L2_CSNP 25
253 #define ISIS_L1_PSNP 26
254 #define ISIS_L2_PSNP 27
256 * The maximum possible value can also be used as a bit mask because the
257 * "PDU Type" field comprises the least significant 5 bits of a particular
258 * octet, see sections 9.5~9.13 of ISO/IEC 10589:2002(E).
260 #define ISIS_PDU_TYPE_MAX 0x1FU
262 #ifndef ISO8878A_CONS
263 #define ISO8878A_CONS 0x84
265 #ifndef ISO10747_IDRP
266 #define ISO10747_IDRP 0x85
269 // Same as in tcpdump/print-sl.c.
271 #define SLIPDIR_OUT 1
273 #ifdef HAVE_OS_PROTO_H
274 #include "os-proto.h"
277 #define JMP(c) ((c)|BPF_JMP|BPF_K)
280 * "Push" the current value of the link-layer header type and link-layer
281 * header offset onto a "stack", and set a new value. (It's not a
282 * full-blown stack; we keep only the top two items.)
284 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
286 (cs)->prevlinktype = (cs)->linktype; \
287 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
288 (cs)->linktype = (new_linktype); \
289 (cs)->off_linkhdr.is_variable = (new_is_variable); \
290 (cs)->off_linkhdr.constant_part = (new_constant_part); \
291 (cs)->off_linkhdr.reg = (new_reg); \
292 (cs)->is_encap = 0; \
296 * Offset "not set" value.
298 #define OFFSET_NOT_SET 0xffffffffU
301 * Absolute offsets, which are offsets from the beginning of the raw
302 * packet data, are, in the general case, the sum of a variable value
303 * and a constant value; the variable value may be absent, in which
304 * case the offset is only the constant value, and the constant value
305 * may be zero, in which case the offset is only the variable value.
307 * bpf_abs_offset is a structure containing all that information:
309 * is_variable is 1 if there's a variable part.
311 * constant_part is the constant part of the value, possibly zero;
313 * if is_variable is 1, reg is the register number for a register
314 * containing the variable value if the register has been assigned,
324 * Value passed to gen_load_a() to indicate what the offset argument
325 * is relative to the beginning of.
328 OR_PACKET
, /* full packet data */
329 OR_LINKHDR
, /* link-layer header */
330 OR_PREVLINKHDR
, /* previous link-layer header */
331 OR_LLC
, /* 802.2 LLC header */
332 OR_PREVMPLSHDR
, /* previous MPLS header */
333 OR_LINKTYPE
, /* link-layer type */
334 OR_LINKPL
, /* link-layer payload */
335 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
336 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
337 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
341 * We divvy out chunks of memory rather than call malloc each time so
342 * we don't have to worry about leaking memory. It's probably
343 * not a big deal if all this memory was wasted but if this ever
344 * goes into a library that would probably not be a good idea.
346 * XXX - this *is* in a library....
349 #define CHUNK0SIZE 1024
356 * A chunk can store any of:
357 * - a string (guaranteed alignment 1 but present for completeness)
361 * For this simple allocator every allocated chunk gets rounded up to the
362 * alignment needed for any chunk.
373 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
375 /* Code generator state */
377 struct _compiler_state
{
388 int outermostlinktype
;
393 /* Hack for handling VLAN and MPLS stacks. */
394 u_int label_stack_depth
;
395 u_int vlan_stack_depth
;
401 * As errors are handled by a longjmp, anything allocated must
402 * be freed in the longjmp handler, so it must be reachable
405 * One thing that's allocated is the result of pcap_nametoaddrinfo();
406 * it must be freed with freeaddrinfo(). This variable points to
407 * any addrinfo structure that would need to be freed.
412 * Various code constructs need to know the layout of the packet.
413 * These values give the necessary offsets from the beginning
414 * of the packet data.
418 * Absolute offset of the beginning of the link-layer header.
420 bpf_abs_offset off_linkhdr
;
423 * If we're checking a link-layer header for a packet encapsulated
424 * in another protocol layer, this is the equivalent information
425 * for the previous layers' link-layer header from the beginning
426 * of the raw packet data.
428 bpf_abs_offset off_prevlinkhdr
;
431 * This is the equivalent information for the outermost layers'
434 bpf_abs_offset off_outermostlinkhdr
;
437 * Absolute offset of the beginning of the link-layer payload.
439 bpf_abs_offset off_linkpl
;
442 * "off_linktype" is the offset to information in the link-layer
443 * header giving the packet type. This is an absolute offset
444 * from the beginning of the packet.
446 * For Ethernet, it's the offset of the Ethernet type field; this
447 * means that it must have a value that skips VLAN tags.
449 * For link-layer types that always use 802.2 headers, it's the
450 * offset of the LLC header; this means that it must have a value
451 * that skips VLAN tags.
453 * For PPP, it's the offset of the PPP type field.
455 * For Cisco HDLC, it's the offset of the CHDLC type field.
457 * For BSD loopback, it's the offset of the AF_ value.
459 * For Linux cooked sockets, it's the offset of the type field.
461 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
462 * encapsulation, in which case, IP is assumed.
464 bpf_abs_offset off_linktype
;
467 * TRUE if the link layer includes an ATM pseudo-header.
471 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
472 * causes us to generate code that checks for a Geneve or
473 * VXLAN header respectively and assume that later filters
474 * apply to the encapsulated payload.
479 * TRUE if we need variable length part of VLAN offset
481 int is_vlan_vloffset
;
484 * These are offsets for the ATM pseudo-header.
491 * These are offsets for the MTP2 fields.
497 * These are offsets for the MTP3 fields.
505 * This is the offset of the first byte after the ATM pseudo_header,
506 * or -1 if there is no ATM pseudo-header.
511 * These are offsets to the beginning of the network-layer header.
512 * They are relative to the beginning of the link-layer payload
513 * (i.e., they don't include off_linkhdr.constant_part or
514 * off_linkpl.constant_part).
516 * If the link layer never uses 802.2 LLC:
518 * "off_nl" and "off_nl_nosnap" are the same.
520 * If the link layer always uses 802.2 LLC:
522 * "off_nl" is the offset if there's a SNAP header following
525 * "off_nl_nosnap" is the offset if there's no SNAP header.
527 * If the link layer is Ethernet:
529 * "off_nl" is the offset if the packet is an Ethernet II packet
530 * (we assume no 802.3+802.2+SNAP);
532 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
533 * with an 802.2 header following it.
539 * Here we handle simple allocation of the scratch registers.
540 * If too many registers are alloc'd, the allocator punts.
542 int regused
[BPF_MEMWORDS
];
548 struct chunk chunks
[NCHUNKS
];
553 * For use by routines outside this file.
557 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
562 * If we've already set an error, don't override it.
563 * The lexical analyzer reports some errors by setting
564 * the error and then returning a LEX_ERROR token, which
565 * is not recognized by any grammar rule, and thus forces
566 * the parse to stop. We don't want the error reported
567 * by the lexical analyzer to be overwritten by the syntax
570 if (!cstate
->error_set
) {
572 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
575 cstate
->error_set
= 1;
580 * For use *ONLY* in routines in this file.
582 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
583 PCAP_PRINTFLIKE(2, 3);
586 static void PCAP_NORETURN
587 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
592 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
595 longjmp(cstate
->top_ctx
, 1);
602 static int init_linktype(compiler_state_t
*, pcap_t
*);
604 static void init_regs(compiler_state_t
*);
605 static int alloc_reg(compiler_state_t
*);
606 static void free_reg(compiler_state_t
*, int);
608 static void initchunks(compiler_state_t
*cstate
);
609 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
610 static void *newchunk(compiler_state_t
*cstate
, size_t);
611 static void freechunks(compiler_state_t
*cstate
);
612 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
613 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
614 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
615 static inline void syntax(compiler_state_t
*cstate
);
617 static void backpatch(struct block
*, struct block
*);
618 static void merge(struct block
*, struct block
*);
619 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
621 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
623 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
625 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
627 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
629 static struct block
*gen_cmp_ne(compiler_state_t
*, enum e_offrel
, u_int
,
630 u_int size
, bpf_u_int32
);
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_mcmp_ne(compiler_state_t
*, enum e_offrel
, u_int
,
634 u_int
, bpf_u_int32
, bpf_u_int32
);
635 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
636 u_int
, const u_char
*);
637 static struct block
*gen_jmp(compiler_state_t
*, int, bpf_u_int32
,
639 static struct block
*gen_set(compiler_state_t
*, bpf_u_int32
, struct slist
*);
640 static struct block
*gen_unset(compiler_state_t
*, bpf_u_int32
, struct slist
*);
641 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
642 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
643 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
645 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
647 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
648 static struct block
*gen_uncond(compiler_state_t
*, int);
649 static inline struct block
*gen_true(compiler_state_t
*);
650 static inline struct block
*gen_false(compiler_state_t
*);
651 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
652 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
653 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
654 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
655 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
656 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
657 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
658 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
659 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
660 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
662 static uint16_t ethertype_to_ppptype(compiler_state_t
*, bpf_u_int32
);
663 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
664 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
665 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
666 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
668 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
669 struct in6_addr
*, int, u_int
, u_int
);
670 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
671 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
672 static unsigned char is_mac48_linktype(const int);
673 static struct block
*gen_mac48host(compiler_state_t
*, const u_char
*,
674 const u_char
, const char *);
675 static struct block
*gen_mac48host_byname(compiler_state_t
*, const char *,
676 const u_char
, const char *);
677 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
678 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
679 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
681 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
682 struct in6_addr
*, int, int, int);
683 static struct block
*gen_gateway(compiler_state_t
*, const char *,
684 struct addrinfo
*, int);
685 static struct block
*gen_ip_proto(compiler_state_t
*, const uint8_t);
686 static struct block
*gen_ip6_proto(compiler_state_t
*, const uint8_t);
687 static struct block
*gen_ipfrag(compiler_state_t
*);
688 static struct block
*gen_portatom(compiler_state_t
*, int, uint16_t);
689 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, uint16_t,
691 static struct block
*gen_portatom6(compiler_state_t
*, int, uint16_t);
692 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, uint16_t,
694 static struct block
*gen_port(compiler_state_t
*, uint16_t, int, int);
695 static struct block
*gen_port_common(compiler_state_t
*, int, struct block
*);
696 static struct block
*gen_portrange(compiler_state_t
*, uint16_t, uint16_t,
698 static struct block
*gen_port6(compiler_state_t
*, uint16_t, int, int);
699 static struct block
*gen_port6_common(compiler_state_t
*, int, struct block
*);
700 static struct block
*gen_portrange6(compiler_state_t
*, uint16_t, uint16_t,
702 static int lookup_proto(compiler_state_t
*, const char *, int);
703 #if !defined(NO_PROTOCHAIN)
704 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
705 #endif /* !defined(NO_PROTOCHAIN) */
706 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int);
707 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
708 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
709 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
710 static struct block
*gen_len(compiler_state_t
*, int, int);
711 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
713 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
714 bpf_u_int32
, int, int);
715 static struct block
*gen_atmtype_llc(compiler_state_t
*);
716 static struct block
*gen_msg_abbrev(compiler_state_t
*, const uint8_t);
717 static struct block
*gen_atm_prototype(compiler_state_t
*, const uint8_t);
718 static struct block
*gen_atm_vpi(compiler_state_t
*, const uint8_t);
719 static struct block
*gen_atm_vci(compiler_state_t
*, const uint16_t);
722 initchunks(compiler_state_t
*cstate
)
726 for (i
= 0; i
< NCHUNKS
; i
++) {
727 cstate
->chunks
[i
].n_left
= 0;
728 cstate
->chunks
[i
].m
= NULL
;
730 cstate
->cur_chunk
= 0;
734 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
740 /* Round up to chunk alignment. */
741 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
743 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
744 if (n
> cp
->n_left
) {
746 k
= ++cstate
->cur_chunk
;
748 bpf_set_error(cstate
, "out of memory");
751 size
= CHUNK0SIZE
<< k
;
752 cp
->m
= (void *)malloc(size
);
754 bpf_set_error(cstate
, "out of memory");
757 memset((char *)cp
->m
, 0, size
);
760 bpf_set_error(cstate
, "out of memory");
765 return (void *)((char *)cp
->m
+ cp
->n_left
);
769 newchunk(compiler_state_t
*cstate
, size_t n
)
773 p
= newchunk_nolongjmp(cstate
, n
);
775 longjmp(cstate
->top_ctx
, 1);
782 freechunks(compiler_state_t
*cstate
)
786 for (i
= 0; i
< NCHUNKS
; ++i
)
787 if (cstate
->chunks
[i
].m
!= NULL
)
788 free(cstate
->chunks
[i
].m
);
792 * A strdup whose allocations are freed after code generation is over.
793 * This is used by the lexical analyzer, so it can't longjmp; it just
794 * returns NULL on an allocation error, and the callers must check
798 sdup(compiler_state_t
*cstate
, const char *s
)
800 size_t n
= strlen(s
) + 1;
801 char *cp
= newchunk_nolongjmp(cstate
, n
);
805 pcapint_strlcpy(cp
, s
, n
);
809 static inline struct block
*
810 new_block(compiler_state_t
*cstate
, int code
)
814 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
821 static inline struct slist
*
822 new_stmt(compiler_state_t
*cstate
, int code
)
826 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
832 static struct block
*
833 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
835 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
841 static struct block
*
842 gen_retblk(compiler_state_t
*cstate
, int v
)
844 if (setjmp(cstate
->top_ctx
)) {
846 * gen_retblk() only fails because a memory
847 * allocation failed in newchunk(), meaning
848 * that it can't return a pointer.
854 return gen_retblk_internal(cstate
, v
);
857 static inline PCAP_NORETURN_DEF
void
858 syntax(compiler_state_t
*cstate
)
860 bpf_error(cstate
, "syntax error in filter expression");
864 * For the given integer return a string with the keyword (or the nominal
865 * keyword if there is more than one). This is a simpler version of tok2str()
866 * in tcpdump because in this problem space a valid integer value is not
870 qual2kw(const char *kind
, const unsigned id
, const char *tokens
[],
873 static char buf
[4][64];
876 if (id
< size
&& tokens
[id
])
879 char *ret
= buf
[idx
];
880 idx
= (idx
+ 1) % (sizeof(buf
) / sizeof(buf
[0]));
881 ret
[0] = '\0'; // just in case
882 snprintf(ret
, sizeof(buf
[0]), "<invalid %s %u>", kind
, id
);
886 // protocol qualifier keywords
888 pqkw(const unsigned id
)
890 const char * tokens
[] = {
902 [Q_DECNET
] = "decnet",
908 [Q_ICMPV6
] = "icmp6",
920 [Q_NETBEUI
] = "netbeui",
923 [Q_ISIS_IIH
] = "iih",
924 [Q_ISIS_SNP
] = "snp",
925 [Q_ISIS_CSNP
] = "csnp",
926 [Q_ISIS_PSNP
] = "psnp",
927 [Q_ISIS_LSP
] = "lsp",
931 return qual2kw("proto", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
934 // direction qualifier keywords
936 dqkw(const unsigned id
)
938 const char * map
[] = {
941 [Q_OR
] = "src or dst",
942 [Q_AND
] = "src and dst",
950 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
955 atmkw(const unsigned id
)
957 const char * tokens
[] = {
960 [A_OAMF4SC
] = "oamf4sc",
961 [A_OAMF4EC
] = "oamf4ec",
969 [A_CONNECTMSG
] = "connectmsg",
970 [A_METACONNECT
] = "metaconnect",
972 return qual2kw("ATM keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
977 ss7kw(const unsigned id
)
979 const char * tokens
[] = {
995 return qual2kw("MTP keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
998 static PCAP_NORETURN_DEF
void
999 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
1001 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
1002 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
1006 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
1008 if (cstate
->linktype
!= DLT_PFLOG
)
1009 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
1013 assert_atm(compiler_state_t
*cstate
, const char *kw
)
1016 * Belt and braces: init_linktype() sets either all of these struct
1017 * members (for DLT_SUNATM) or none (otherwise).
1019 if (cstate
->linktype
!= DLT_SUNATM
||
1021 cstate
->off_vpi
== OFFSET_NOT_SET
||
1022 cstate
->off_vci
== OFFSET_NOT_SET
||
1023 cstate
->off_proto
== OFFSET_NOT_SET
||
1024 cstate
->off_payload
== OFFSET_NOT_SET
)
1025 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1029 assert_ss7(compiler_state_t
*cstate
, const char *kw
)
1031 switch (cstate
->linktype
) {
1034 case DLT_MTP2_WITH_PHDR
:
1035 // Belt and braces, same as in assert_atm().
1036 if (cstate
->off_sio
!= OFFSET_NOT_SET
&&
1037 cstate
->off_opc
!= OFFSET_NOT_SET
&&
1038 cstate
->off_dpc
!= OFFSET_NOT_SET
&&
1039 cstate
->off_sls
!= OFFSET_NOT_SET
)
1042 bpf_error(cstate
, "'%s' supported only on SS7", kw
);
1046 assert_maxval(compiler_state_t
*cstate
, const char *name
,
1047 const bpf_u_int32 val
, const bpf_u_int32 maxval
)
1050 bpf_error(cstate
, "%s %u greater than maximum %u",
1054 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1055 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1056 #define ERRSTR_UNKNOWN_MAC48HOST "unknown Ethernet-like host '%s'"
1058 // Validate a port/portrange proto qualifier and map to an IP protocol number.
1060 port_pq_to_ipproto(compiler_state_t
*cstate
, const int proto
, const char *kw
)
1068 return IPPROTO_SCTP
;
1072 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), kw
);
1076 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1077 const char *buf
, int optimize
, bpf_u_int32 mask
)
1083 compiler_state_t cstate
;
1084 yyscan_t scanner
= NULL
;
1085 YY_BUFFER_STATE in_buffer
= NULL
;
1090 * If this pcap_t hasn't been activated, it doesn't have a
1091 * link-layer type, so we can't use it.
1093 if (!p
->activated
) {
1094 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1095 "not-yet-activated pcap_t passed to pcap_compile");
1096 return (PCAP_ERROR
);
1101 * Initialize Winsock, asking for the latest version (2.2),
1102 * as we may be calling Winsock routines to translate
1103 * host names to addresses.
1105 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1107 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1108 err
, "Error calling WSAStartup()");
1109 return (PCAP_ERROR
);
1113 #ifdef ENABLE_REMOTE
1115 * If the device on which we're capturing need to be notified
1116 * that a new filter is being compiled, do so.
1118 * This allows them to save a copy of it, in case, for example,
1119 * they're implementing a form of remote packet capture, and
1120 * want the remote machine to filter out the packets in which
1121 * it's sending the packets it's captured.
1123 * XXX - the fact that we happen to be compiling a filter
1124 * doesn't necessarily mean we'll be installing it as the
1125 * filter for this pcap_t; we might be running it from userland
1126 * on captured packets to do packet classification. We really
1127 * need a better way of handling this, but this is all that
1128 * the WinPcap remote capture code did.
1130 if (p
->save_current_filter_op
!= NULL
)
1131 (p
->save_current_filter_op
)(p
, buf
);
1134 initchunks(&cstate
);
1135 cstate
.no_optimize
= 0;
1137 cstate
.ic
.root
= NULL
;
1138 cstate
.ic
.cur_mark
= 0;
1139 cstate
.bpf_pcap
= p
;
1140 cstate
.error_set
= 0;
1143 cstate
.netmask
= mask
;
1145 cstate
.snaplen
= pcap_snapshot(p
);
1146 if (cstate
.snaplen
== 0) {
1147 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1148 "snaplen of 0 rejects all packets");
1153 if (pcap_lex_init(&scanner
) != 0) {
1154 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1155 errno
, "can't initialize scanner");
1159 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1162 * Associate the compiler state with the lexical analyzer
1165 pcap_set_extra(&cstate
, scanner
);
1167 if (init_linktype(&cstate
, p
) == -1) {
1171 if (pcap_parse(scanner
, &cstate
) != 0) {
1172 if (cstate
.ai
!= NULL
)
1173 freeaddrinfo(cstate
.ai
);
1178 if (cstate
.ic
.root
== NULL
) {
1179 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1182 * Catch errors reported by gen_retblk().
1184 if (cstate
.ic
.root
== NULL
) {
1190 if (optimize
&& !cstate
.no_optimize
) {
1191 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1196 if (cstate
.ic
.root
== NULL
||
1197 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1198 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1199 "expression rejects all packets");
1204 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1205 cstate
.ic
.root
, &len
, p
->errbuf
);
1206 if (program
->bf_insns
== NULL
) {
1211 program
->bf_len
= len
;
1213 rc
= 0; /* We're all okay */
1217 * Clean up everything for the lexical analyzer.
1219 if (in_buffer
!= NULL
)
1220 pcap__delete_buffer(in_buffer
, scanner
);
1221 if (scanner
!= NULL
)
1222 pcap_lex_destroy(scanner
);
1225 * Clean up our own allocated memory.
1227 freechunks(&cstate
);
1237 * entry point for using the compiler with no pcap open
1238 * pass in all the stuff that is needed explicitly instead.
1241 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1242 struct bpf_program
*program
,
1243 const char *buf
, int optimize
, bpf_u_int32 mask
)
1248 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1250 return (PCAP_ERROR
);
1251 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1257 * Clean up a "struct bpf_program" by freeing all the memory allocated
1261 pcap_freecode(struct bpf_program
*program
)
1263 program
->bf_len
= 0;
1264 if (program
->bf_insns
!= NULL
) {
1265 free((char *)program
->bf_insns
);
1266 program
->bf_insns
= NULL
;
1271 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1272 * which of the jt and jf fields has been resolved and which is a pointer
1273 * back to another unresolved block (or nil). At least one of the fields
1274 * in each block is already resolved.
1277 backpatch(struct block
*list
, struct block
*target
)
1294 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1295 * which of jt and jf is the link.
1298 merge(struct block
*b0
, struct block
*b1
)
1300 register struct block
**p
= &b0
;
1302 /* Find end of list. */
1304 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1306 /* Concatenate the lists. */
1311 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1314 * Catch errors reported by us and routines below us, and return -1
1317 if (setjmp(cstate
->top_ctx
))
1321 * Insert before the statements of the first (root) block any
1322 * statements needed to load the lengths of any variable-length
1323 * headers into registers.
1325 * XXX - a fancier strategy would be to insert those before the
1326 * statements of all blocks that use those lengths and that
1327 * have no predecessors that use them, so that we only compute
1328 * the lengths if we need them. There might be even better
1329 * approaches than that.
1331 * However, those strategies would be more complicated, and
1332 * as we don't generate code to compute a length if the
1333 * program has no tests that use the length, and as most
1334 * tests will probably use those lengths, we would just
1335 * postpone computing the lengths so that it's not done
1336 * for tests that fail early, and it's not clear that's
1339 insert_compute_vloffsets(cstate
, p
->head
);
1342 * For DLT_PPI captures, generate a check of the per-packet
1343 * DLT value to make sure it's DLT_IEEE802_11.
1345 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1346 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1347 * with appropriate Ethernet information and use that rather
1348 * than using something such as DLT_PPI where you don't know
1349 * the link-layer header type until runtime, which, in the
1350 * general case, would force us to generate both Ethernet *and*
1351 * 802.11 code (*and* anything else for which PPI is used)
1352 * and choose between them early in the BPF program?
1354 if (cstate
->linktype
== DLT_PPI
) {
1355 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1356 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1357 gen_and(ppi_dlt_check
, p
);
1360 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1361 p
->sense
= !p
->sense
;
1362 backpatch(p
, gen_retblk_internal(cstate
, 0));
1363 cstate
->ic
.root
= p
->head
;
1368 gen_and(struct block
*b0
, struct block
*b1
)
1370 backpatch(b0
, b1
->head
);
1371 b0
->sense
= !b0
->sense
;
1372 b1
->sense
= !b1
->sense
;
1374 b1
->sense
= !b1
->sense
;
1375 b1
->head
= b0
->head
;
1379 gen_or(struct block
*b0
, struct block
*b1
)
1381 b0
->sense
= !b0
->sense
;
1382 backpatch(b0
, b1
->head
);
1383 b0
->sense
= !b0
->sense
;
1385 b1
->head
= b0
->head
;
1389 gen_not(struct block
*b
)
1391 b
->sense
= !b
->sense
;
1394 static struct block
*
1395 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1396 u_int size
, bpf_u_int32 v
)
1398 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1401 static struct block
*
1402 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1403 u_int size
, bpf_u_int32 v
)
1405 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1408 static struct block
*
1409 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1410 u_int size
, bpf_u_int32 v
)
1412 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1415 static struct block
*
1416 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1417 u_int size
, bpf_u_int32 v
)
1419 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1422 static struct block
*
1423 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1424 u_int size
, bpf_u_int32 v
)
1426 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1429 static struct block
*
1430 gen_cmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1431 u_int size
, bpf_u_int32 v
)
1433 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 1, v
);
1436 static struct block
*
1437 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1438 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1441 * For any A: if mask == 0, it means A & mask == 0, so the result is
1442 * true iff v == 0. In this case ideally the caller should have
1443 * skipped this invocation and have fewer statement blocks to juggle.
1444 * If the caller could have skipped, but has not, produce a block with
1447 * This could be done in gen_ncmp() in a more generic way, but this
1448 * function is the only code path that can have mask == 0.
1451 return v
? gen_false(cstate
) : gen_true(cstate
);
1453 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1456 static struct block
*
1457 gen_mcmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1458 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1460 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 1, v
);
1463 static struct block
*
1464 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1465 u_int size
, const u_char
*v
)
1467 register struct block
*b
, *tmp
;
1471 register const u_char
*p
= &v
[size
- 4];
1473 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1481 register const u_char
*p
= &v
[size
- 2];
1483 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1491 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1499 static struct block
*
1500 gen_jmp(compiler_state_t
*cstate
, int jtype
, bpf_u_int32 v
, struct slist
*stmts
)
1502 struct block
*b
= new_block(cstate
, JMP(jtype
));
1508 static struct block
*
1509 gen_set(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1511 return gen_jmp(cstate
, BPF_JSET
, v
, stmts
);
1514 static struct block
*
1515 gen_unset(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1517 struct block
*b
= gen_set(cstate
, v
, stmts
);
1523 * AND the field of size "size" at offset "offset" relative to the header
1524 * specified by "offrel" with "mask", and compare it with the value "v"
1525 * with the test specified by "jtype"; if "reverse" is true, the test
1526 * should test the opposite of "jtype".
1528 static struct block
*
1529 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1530 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1533 struct slist
*s
, *s2
;
1536 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1538 if (mask
!= 0xffffffff) {
1539 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1544 b
= gen_jmp(cstate
, jtype
, v
, s
);
1551 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1553 cstate
->pcap_fddipad
= p
->fddipad
;
1556 * We start out with only one link-layer header.
1558 cstate
->outermostlinktype
= pcap_datalink(p
);
1559 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1560 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1561 cstate
->off_outermostlinkhdr
.reg
= -1;
1563 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1564 cstate
->off_prevlinkhdr
.constant_part
= 0;
1565 cstate
->off_prevlinkhdr
.is_variable
= 0;
1566 cstate
->off_prevlinkhdr
.reg
= -1;
1568 cstate
->linktype
= cstate
->outermostlinktype
;
1569 cstate
->off_linkhdr
.constant_part
= 0;
1570 cstate
->off_linkhdr
.is_variable
= 0;
1571 cstate
->off_linkhdr
.reg
= -1;
1576 cstate
->off_linkpl
.constant_part
= 0;
1577 cstate
->off_linkpl
.is_variable
= 0;
1578 cstate
->off_linkpl
.reg
= -1;
1580 cstate
->off_linktype
.constant_part
= 0;
1581 cstate
->off_linktype
.is_variable
= 0;
1582 cstate
->off_linktype
.reg
= -1;
1585 * Assume it's not raw ATM with a pseudo-header, for now.
1588 cstate
->off_vpi
= OFFSET_NOT_SET
;
1589 cstate
->off_vci
= OFFSET_NOT_SET
;
1590 cstate
->off_proto
= OFFSET_NOT_SET
;
1591 cstate
->off_payload
= OFFSET_NOT_SET
;
1594 * And not encapsulated with either Geneve or VXLAN.
1596 cstate
->is_encap
= 0;
1599 * No variable length VLAN offset by default
1601 cstate
->is_vlan_vloffset
= 0;
1604 * And assume we're not doing SS7.
1606 cstate
->off_li
= OFFSET_NOT_SET
;
1607 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1608 cstate
->off_sio
= OFFSET_NOT_SET
;
1609 cstate
->off_opc
= OFFSET_NOT_SET
;
1610 cstate
->off_dpc
= OFFSET_NOT_SET
;
1611 cstate
->off_sls
= OFFSET_NOT_SET
;
1613 cstate
->label_stack_depth
= 0;
1614 cstate
->vlan_stack_depth
= 0;
1616 switch (cstate
->linktype
) {
1619 cstate
->off_linktype
.constant_part
= 2;
1620 cstate
->off_linkpl
.constant_part
= 6;
1621 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1622 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1625 case DLT_ARCNET_LINUX
:
1626 cstate
->off_linktype
.constant_part
= 4;
1627 cstate
->off_linkpl
.constant_part
= 8;
1628 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1629 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1633 cstate
->off_linktype
.constant_part
= 12;
1634 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1635 cstate
->off_nl
= 0; /* Ethernet II */
1636 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1641 * SLIP doesn't have a link level type. The 16 byte
1642 * header is hacked into our SLIP driver.
1644 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1645 cstate
->off_linkpl
.constant_part
= 16;
1647 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1650 case DLT_SLIP_BSDOS
:
1651 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1652 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1654 cstate
->off_linkpl
.constant_part
= 24;
1656 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1661 cstate
->off_linktype
.constant_part
= 0;
1662 cstate
->off_linkpl
.constant_part
= 4;
1664 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1668 cstate
->off_linktype
.constant_part
= 0;
1669 cstate
->off_linkpl
.constant_part
= 12;
1671 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1676 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1677 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1678 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1679 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1680 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1682 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1687 * This does not include the Ethernet header, and
1688 * only covers session state.
1690 cstate
->off_linktype
.constant_part
= 6;
1691 cstate
->off_linkpl
.constant_part
= 8;
1693 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1697 cstate
->off_linktype
.constant_part
= 5;
1698 cstate
->off_linkpl
.constant_part
= 24;
1700 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1705 * FDDI doesn't really have a link-level type field.
1706 * We set "off_linktype" to the offset of the LLC header.
1708 * To check for Ethernet types, we assume that SSAP = SNAP
1709 * is being used and pick out the encapsulated Ethernet type.
1710 * XXX - should we generate code to check for SNAP?
1712 cstate
->off_linktype
.constant_part
= 13;
1713 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1714 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1715 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1716 cstate
->off_nl
= 8; /* 802.2+SNAP */
1717 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1722 * Token Ring doesn't really have a link-level type field.
1723 * We set "off_linktype" to the offset of the LLC header.
1725 * To check for Ethernet types, we assume that SSAP = SNAP
1726 * is being used and pick out the encapsulated Ethernet type.
1727 * XXX - should we generate code to check for SNAP?
1729 * XXX - the header is actually variable-length.
1730 * Some various Linux patched versions gave 38
1731 * as "off_linktype" and 40 as "off_nl"; however,
1732 * if a token ring packet has *no* routing
1733 * information, i.e. is not source-routed, the correct
1734 * values are 20 and 22, as they are in the vanilla code.
1736 * A packet is source-routed iff the uppermost bit
1737 * of the first byte of the source address, at an
1738 * offset of 8, has the uppermost bit set. If the
1739 * packet is source-routed, the total number of bytes
1740 * of routing information is 2 plus bits 0x1F00 of
1741 * the 16-bit value at an offset of 14 (shifted right
1742 * 8 - figure out which byte that is).
1744 cstate
->off_linktype
.constant_part
= 14;
1745 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1746 cstate
->off_nl
= 8; /* 802.2+SNAP */
1747 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1750 case DLT_PRISM_HEADER
:
1751 case DLT_IEEE802_11_RADIO_AVS
:
1752 case DLT_IEEE802_11_RADIO
:
1753 cstate
->off_linkhdr
.is_variable
= 1;
1754 /* Fall through, 802.11 doesn't have a variable link
1755 * prefix but is otherwise the same. */
1758 case DLT_IEEE802_11
:
1760 * 802.11 doesn't really have a link-level type field.
1761 * We set "off_linktype.constant_part" to the offset of
1764 * To check for Ethernet types, we assume that SSAP = SNAP
1765 * is being used and pick out the encapsulated Ethernet type.
1766 * XXX - should we generate code to check for SNAP?
1768 * We also handle variable-length radio headers here.
1769 * The Prism header is in theory variable-length, but in
1770 * practice it's always 144 bytes long. However, some
1771 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1772 * sometimes or always supply an AVS header, so we
1773 * have to check whether the radio header is a Prism
1774 * header or an AVS header, so, in practice, it's
1777 cstate
->off_linktype
.constant_part
= 24;
1778 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1779 cstate
->off_linkpl
.is_variable
= 1;
1780 cstate
->off_nl
= 8; /* 802.2+SNAP */
1781 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1786 * At the moment we treat PPI the same way that we treat
1787 * normal Radiotap encoded packets. The difference is in
1788 * the function that generates the code at the beginning
1789 * to compute the header length. Since this code generator
1790 * of PPI supports bare 802.11 encapsulation only (i.e.
1791 * the encapsulated DLT should be DLT_IEEE802_11) we
1792 * generate code to check for this too.
1794 cstate
->off_linktype
.constant_part
= 24;
1795 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1796 cstate
->off_linkpl
.is_variable
= 1;
1797 cstate
->off_linkhdr
.is_variable
= 1;
1798 cstate
->off_nl
= 8; /* 802.2+SNAP */
1799 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1802 case DLT_ATM_RFC1483
:
1803 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1805 * assume routed, non-ISO PDUs
1806 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1808 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1809 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1810 * latter would presumably be treated the way PPPoE
1811 * should be, so you can do "pppoe and udp port 2049"
1812 * or "pppoa and tcp port 80" and have it check for
1813 * PPPo{A,E} and a PPP protocol of IP and....
1815 cstate
->off_linktype
.constant_part
= 0;
1816 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1817 cstate
->off_nl
= 8; /* 802.2+SNAP */
1818 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1823 * Full Frontal ATM; you get AALn PDUs with an ATM
1827 cstate
->off_vpi
= SUNATM_VPI_POS
;
1828 cstate
->off_vci
= SUNATM_VCI_POS
;
1829 cstate
->off_proto
= PROTO_POS
;
1830 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1831 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1832 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1833 cstate
->off_nl
= 8; /* 802.2+SNAP */
1834 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1840 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1841 cstate
->off_linkpl
.constant_part
= 0;
1843 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1846 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1847 cstate
->off_linktype
.constant_part
= 14;
1848 cstate
->off_linkpl
.constant_part
= 16;
1850 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1853 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1854 cstate
->off_linktype
.constant_part
= 0;
1855 cstate
->off_linkpl
.constant_part
= 20;
1857 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1862 * LocalTalk does have a 1-byte type field in the LLAP header,
1863 * but really it just indicates whether there is a "short" or
1864 * "long" DDP packet following.
1866 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1867 cstate
->off_linkpl
.constant_part
= 0;
1869 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1872 case DLT_IP_OVER_FC
:
1874 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1875 * link-level type field. We set "off_linktype" to the
1876 * offset of the LLC header.
1878 * To check for Ethernet types, we assume that SSAP = SNAP
1879 * is being used and pick out the encapsulated Ethernet type.
1880 * XXX - should we generate code to check for SNAP? RFC
1881 * 2625 says SNAP should be used.
1883 cstate
->off_linktype
.constant_part
= 16;
1884 cstate
->off_linkpl
.constant_part
= 16;
1885 cstate
->off_nl
= 8; /* 802.2+SNAP */
1886 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1891 * XXX - we should set this to handle SNAP-encapsulated
1892 * frames (NLPID of 0x80).
1894 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1895 cstate
->off_linkpl
.constant_part
= 0;
1897 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1901 * the only BPF-interesting FRF.16 frames are non-control frames;
1902 * Frame Relay has a variable length link-layer
1903 * so lets start with offset 4 for now and increments later on (FIXME);
1906 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1907 cstate
->off_linkpl
.constant_part
= 0;
1909 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1912 case DLT_APPLE_IP_OVER_IEEE1394
:
1913 cstate
->off_linktype
.constant_part
= 16;
1914 cstate
->off_linkpl
.constant_part
= 18;
1916 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1919 case DLT_SYMANTEC_FIREWALL
:
1920 cstate
->off_linktype
.constant_part
= 6;
1921 cstate
->off_linkpl
.constant_part
= 44;
1922 cstate
->off_nl
= 0; /* Ethernet II */
1923 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1927 cstate
->off_linktype
.constant_part
= 0;
1928 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1929 cstate
->off_linkpl
.is_variable
= 1;
1931 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1934 case DLT_JUNIPER_MFR
:
1935 case DLT_JUNIPER_MLFR
:
1936 case DLT_JUNIPER_MLPPP
:
1937 case DLT_JUNIPER_PPP
:
1938 case DLT_JUNIPER_CHDLC
:
1939 case DLT_JUNIPER_FRELAY
:
1940 cstate
->off_linktype
.constant_part
= 4;
1941 cstate
->off_linkpl
.constant_part
= 4;
1943 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1946 case DLT_JUNIPER_ATM1
:
1947 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1948 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1950 cstate
->off_nl_nosnap
= 10;
1953 case DLT_JUNIPER_ATM2
:
1954 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1955 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1957 cstate
->off_nl_nosnap
= 10;
1960 /* frames captured on a Juniper PPPoE service PIC
1961 * contain raw ethernet frames */
1962 case DLT_JUNIPER_PPPOE
:
1963 case DLT_JUNIPER_ETHER
:
1964 cstate
->off_linkpl
.constant_part
= 14;
1965 cstate
->off_linktype
.constant_part
= 16;
1966 cstate
->off_nl
= 18; /* Ethernet II */
1967 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1970 case DLT_JUNIPER_PPPOE_ATM
:
1971 cstate
->off_linktype
.constant_part
= 4;
1972 cstate
->off_linkpl
.constant_part
= 6;
1974 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1977 case DLT_JUNIPER_GGSN
:
1978 cstate
->off_linktype
.constant_part
= 6;
1979 cstate
->off_linkpl
.constant_part
= 12;
1981 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1984 case DLT_JUNIPER_ES
:
1985 cstate
->off_linktype
.constant_part
= 6;
1986 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1987 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1988 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1991 case DLT_JUNIPER_MONITOR
:
1992 cstate
->off_linktype
.constant_part
= 12;
1993 cstate
->off_linkpl
.constant_part
= 12;
1994 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1995 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1998 case DLT_BACNET_MS_TP
:
1999 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2000 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2001 cstate
->off_nl
= OFFSET_NOT_SET
;
2002 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2005 case DLT_JUNIPER_SERVICES
:
2006 cstate
->off_linktype
.constant_part
= 12;
2007 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2008 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2009 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2012 case DLT_JUNIPER_VP
:
2013 cstate
->off_linktype
.constant_part
= 18;
2014 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2015 cstate
->off_nl
= OFFSET_NOT_SET
;
2016 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2019 case DLT_JUNIPER_ST
:
2020 cstate
->off_linktype
.constant_part
= 18;
2021 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2022 cstate
->off_nl
= OFFSET_NOT_SET
;
2023 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2026 case DLT_JUNIPER_ISM
:
2027 cstate
->off_linktype
.constant_part
= 8;
2028 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2029 cstate
->off_nl
= OFFSET_NOT_SET
;
2030 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2033 case DLT_JUNIPER_VS
:
2034 case DLT_JUNIPER_SRX_E2E
:
2035 case DLT_JUNIPER_FIBRECHANNEL
:
2036 case DLT_JUNIPER_ATM_CEMIC
:
2037 cstate
->off_linktype
.constant_part
= 8;
2038 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2039 cstate
->off_nl
= OFFSET_NOT_SET
;
2040 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2045 cstate
->off_li_hsl
= 4;
2046 cstate
->off_sio
= 3;
2047 cstate
->off_opc
= 4;
2048 cstate
->off_dpc
= 4;
2049 cstate
->off_sls
= 7;
2050 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2051 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2052 cstate
->off_nl
= OFFSET_NOT_SET
;
2053 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2056 case DLT_MTP2_WITH_PHDR
:
2058 cstate
->off_li_hsl
= 8;
2059 cstate
->off_sio
= 7;
2060 cstate
->off_opc
= 8;
2061 cstate
->off_dpc
= 8;
2062 cstate
->off_sls
= 11;
2063 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2064 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2065 cstate
->off_nl
= OFFSET_NOT_SET
;
2066 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2070 cstate
->off_li
= 22;
2071 cstate
->off_li_hsl
= 24;
2072 cstate
->off_sio
= 23;
2073 cstate
->off_opc
= 24;
2074 cstate
->off_dpc
= 24;
2075 cstate
->off_sls
= 27;
2076 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2077 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2078 cstate
->off_nl
= OFFSET_NOT_SET
;
2079 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2083 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2084 cstate
->off_linkpl
.constant_part
= 4;
2086 cstate
->off_nl_nosnap
= 0;
2091 * Currently, only raw "link[N:M]" filtering is supported.
2093 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
2094 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2095 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
2096 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2100 cstate
->off_linktype
.constant_part
= 1;
2101 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2103 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2106 case DLT_NETANALYZER
:
2107 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
2108 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2109 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
2110 cstate
->off_nl
= 0; /* Ethernet II */
2111 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2114 case DLT_NETANALYZER_TRANSPARENT
:
2115 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2116 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2117 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2118 cstate
->off_nl
= 0; /* Ethernet II */
2119 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2124 * For values in the range in which we've assigned new
2125 * DLT_ values, only raw "link[N:M]" filtering is supported.
2127 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2128 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2129 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2130 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2131 cstate
->off_nl
= OFFSET_NOT_SET
;
2132 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2134 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2135 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2141 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2146 * Load a value relative to the specified absolute offset.
2148 static struct slist
*
2149 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2150 u_int offset
, u_int size
)
2152 struct slist
*s
, *s2
;
2154 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2157 * If "s" is non-null, it has code to arrange that the X register
2158 * contains the variable part of the absolute offset, so we
2159 * generate a load relative to that, with an offset of
2160 * abs_offset->constant_part + offset.
2162 * Otherwise, we can do an absolute load with an offset of
2163 * abs_offset->constant_part + offset.
2167 * "s" points to a list of statements that puts the
2168 * variable part of the absolute offset into the X register.
2169 * Do an indirect load, to use the X register as an offset.
2171 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2172 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2176 * There is no variable part of the absolute offset, so
2177 * just do an absolute load.
2179 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2180 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2186 * Load a value relative to the beginning of the specified header.
2188 static struct slist
*
2189 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2192 struct slist
*s
, *s2
;
2195 * Squelch warnings from compilers that *don't* assume that
2196 * offrel always has a valid enum value and therefore don't
2197 * assume that we'll always go through one of the case arms.
2199 * If we have a default case, compilers that *do* assume that
2200 * will then complain about the default case code being
2203 * Damned if you do, damned if you don't.
2210 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2215 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2218 case OR_PREVLINKHDR
:
2219 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2223 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2226 case OR_PREVMPLSHDR
:
2227 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2231 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2234 case OR_LINKPL_NOSNAP
:
2235 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2239 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2244 * Load the X register with the length of the IPv4 header
2245 * (plus the offset of the link-layer header, if it's
2246 * preceded by a variable-length header such as a radio
2247 * header), in bytes.
2249 s
= gen_loadx_iphdrlen(cstate
);
2252 * Load the item at {offset of the link-layer payload} +
2253 * {offset, relative to the start of the link-layer
2254 * payload, of the IPv4 header} + {length of the IPv4 header} +
2255 * {specified offset}.
2257 * If the offset of the link-layer payload is variable,
2258 * the variable part of that offset is included in the
2259 * value in the X register, and we include the constant
2260 * part in the offset of the load.
2262 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2263 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2268 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2275 * Generate code to load into the X register the sum of the length of
2276 * the IPv4 header and the variable part of the offset of the link-layer
2279 static struct slist
*
2280 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2282 struct slist
*s
, *s2
;
2284 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2287 * The offset of the link-layer payload has a variable
2288 * part. "s" points to a list of statements that put
2289 * the variable part of that offset into the X register.
2291 * The 4*([k]&0xf) addressing mode can't be used, as we
2292 * don't have a constant offset, so we have to load the
2293 * value in question into the A register and add to it
2294 * the value from the X register.
2296 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2297 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2299 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2302 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2307 * The A register now contains the length of the IP header.
2308 * We need to add to it the variable part of the offset of
2309 * the link-layer payload, which is still in the X
2310 * register, and move the result into the X register.
2312 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2313 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2316 * The offset of the link-layer payload is a constant,
2317 * so no code was generated to load the (nonexistent)
2318 * variable part of that offset.
2320 * This means we can use the 4*([k]&0xf) addressing
2321 * mode. Load the length of the IPv4 header, which
2322 * is at an offset of cstate->off_nl from the beginning of
2323 * the link-layer payload, and thus at an offset of
2324 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2325 * of the raw packet data, using that addressing mode.
2327 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2328 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2334 static struct block
*
2335 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2339 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2341 return gen_jmp(cstate
, BPF_JEQ
, 0, s
);
2344 static inline struct block
*
2345 gen_true(compiler_state_t
*cstate
)
2347 return gen_uncond(cstate
, 1);
2350 static inline struct block
*
2351 gen_false(compiler_state_t
*cstate
)
2353 return gen_uncond(cstate
, 0);
2357 * Generate code to match a particular packet type.
2359 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2360 * value, if <= ETHERMTU. We use that to determine whether to
2361 * match the type/length field or to check the type/length field for
2362 * a value <= ETHERMTU to see whether it's a type field and then do
2363 * the appropriate test.
2365 static struct block
*
2366 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2368 struct block
*b0
, *b1
;
2374 case LLCSAP_NETBEUI
:
2376 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2377 * so we check the DSAP and SSAP.
2379 * LLCSAP_IP checks for IP-over-802.2, rather
2380 * than IP-over-Ethernet or IP-over-SNAP.
2382 * XXX - should we check both the DSAP and the
2383 * SSAP, like this, or should we check just the
2384 * DSAP, as we do for other types <= ETHERMTU
2385 * (i.e., other SAP values)?
2387 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2388 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2396 * Ethernet_II frames, which are Ethernet
2397 * frames with a frame type of ETHERTYPE_IPX;
2399 * Ethernet_802.3 frames, which are 802.3
2400 * frames (i.e., the type/length field is
2401 * a length field, <= ETHERMTU, rather than
2402 * a type field) with the first two bytes
2403 * after the Ethernet/802.3 header being
2406 * Ethernet_802.2 frames, which are 802.3
2407 * frames with an 802.2 LLC header and
2408 * with the IPX LSAP as the DSAP in the LLC
2411 * Ethernet_SNAP frames, which are 802.3
2412 * frames with an LLC header and a SNAP
2413 * header and with an OUI of 0x000000
2414 * (encapsulated Ethernet) and a protocol
2415 * ID of ETHERTYPE_IPX in the SNAP header.
2417 * XXX - should we generate the same code both
2418 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2422 * This generates code to check both for the
2423 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2425 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2426 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2430 * Now we add code to check for SNAP frames with
2431 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2433 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2437 * Now we generate code to check for 802.3
2438 * frames in general.
2440 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2443 * Now add the check for 802.3 frames before the
2444 * check for Ethernet_802.2 and Ethernet_802.3,
2445 * as those checks should only be done on 802.3
2446 * frames, not on Ethernet frames.
2451 * Now add the check for Ethernet_II frames, and
2452 * do that before checking for the other frame
2455 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2459 case ETHERTYPE_ATALK
:
2460 case ETHERTYPE_AARP
:
2462 * EtherTalk (AppleTalk protocols on Ethernet link
2463 * layer) may use 802.2 encapsulation.
2467 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2468 * we check for an Ethernet type field less or equal than
2469 * 1500, which means it's an 802.3 length field.
2471 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2474 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2475 * SNAP packets with an organization code of
2476 * 0x080007 (Apple, for Appletalk) and a protocol
2477 * type of ETHERTYPE_ATALK (Appletalk).
2479 * 802.2-encapsulated ETHERTYPE_AARP packets are
2480 * SNAP packets with an organization code of
2481 * 0x000000 (encapsulated Ethernet) and a protocol
2482 * type of ETHERTYPE_AARP (Appletalk ARP).
2484 if (ll_proto
== ETHERTYPE_ATALK
)
2485 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2486 else /* ll_proto == ETHERTYPE_AARP */
2487 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2491 * Check for Ethernet encapsulation (Ethertalk
2492 * phase 1?); we just check for the Ethernet
2495 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2501 if (ll_proto
<= ETHERMTU
) {
2502 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2504 * This is an LLC SAP value, so the frames
2505 * that match would be 802.2 frames.
2506 * Check that the frame is an 802.2 frame
2507 * (i.e., that the length/type field is
2508 * a length field, <= ETHERMTU) and
2509 * then check the DSAP.
2511 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2512 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2516 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2518 * This is an Ethernet type, so compare
2519 * the length/type field with it (if
2520 * the frame is an 802.2 frame, the length
2521 * field will be <= ETHERMTU, and, as
2522 * "ll_proto" is > ETHERMTU, this test
2523 * will fail and the frame won't match,
2524 * which is what we want).
2526 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2531 static struct block
*
2532 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2535 * For DLT_NULL, the link-layer header is a 32-bit word
2536 * containing an AF_ value in *host* byte order, and for
2537 * DLT_ENC, the link-layer header begins with a 32-bit
2538 * word containing an AF_ value in host byte order.
2540 * In addition, if we're reading a saved capture file,
2541 * the host byte order in the capture may not be the
2542 * same as the host byte order on this machine.
2544 * For DLT_LOOP, the link-layer header is a 32-bit
2545 * word containing an AF_ value in *network* byte order.
2547 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2549 * The AF_ value is in host byte order, but the BPF
2550 * interpreter will convert it to network byte order.
2552 * If this is a save file, and it's from a machine
2553 * with the opposite byte order to ours, we byte-swap
2556 * Then we run it through "htonl()", and generate
2557 * code to compare against the result.
2559 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2560 ll_proto
= SWAPLONG(ll_proto
);
2561 ll_proto
= htonl(ll_proto
);
2563 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2567 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2568 * or IPv6 then we have an error.
2570 static struct block
*
2571 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2576 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2579 case ETHERTYPE_IPV6
:
2580 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2587 return gen_false(cstate
);
2591 * Generate code to match a particular packet type.
2593 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2594 * value, if <= ETHERMTU. We use that to determine whether to
2595 * match the type field or to check the type field for the special
2596 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2598 static struct block
*
2599 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2601 struct block
*b0
, *b1
;
2607 case LLCSAP_NETBEUI
:
2609 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2610 * so we check the DSAP and SSAP.
2612 * LLCSAP_IP checks for IP-over-802.2, rather
2613 * than IP-over-Ethernet or IP-over-SNAP.
2615 * XXX - should we check both the DSAP and the
2616 * SSAP, like this, or should we check just the
2617 * DSAP, as we do for other types <= ETHERMTU
2618 * (i.e., other SAP values)?
2620 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2621 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2627 * Ethernet_II frames, which are Ethernet
2628 * frames with a frame type of ETHERTYPE_IPX;
2630 * Ethernet_802.3 frames, which have a frame
2631 * type of LINUX_SLL_P_802_3;
2633 * Ethernet_802.2 frames, which are 802.3
2634 * frames with an 802.2 LLC header (i.e, have
2635 * a frame type of LINUX_SLL_P_802_2) and
2636 * with the IPX LSAP as the DSAP in the LLC
2639 * Ethernet_SNAP frames, which are 802.3
2640 * frames with an LLC header and a SNAP
2641 * header and with an OUI of 0x000000
2642 * (encapsulated Ethernet) and a protocol
2643 * ID of ETHERTYPE_IPX in the SNAP header.
2645 * First, do the checks on LINUX_SLL_P_802_2
2646 * frames; generate the check for either
2647 * Ethernet_802.2 or Ethernet_SNAP frames, and
2648 * then put a check for LINUX_SLL_P_802_2 frames
2651 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2652 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2654 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2658 * Now check for 802.3 frames and OR that with
2659 * the previous test.
2661 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2665 * Now add the check for Ethernet_II frames, and
2666 * do that before checking for the other frame
2669 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2673 case ETHERTYPE_ATALK
:
2674 case ETHERTYPE_AARP
:
2676 * EtherTalk (AppleTalk protocols on Ethernet link
2677 * layer) may use 802.2 encapsulation.
2681 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2682 * we check for the 802.2 protocol type in the
2683 * "Ethernet type" field.
2685 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2688 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2689 * SNAP packets with an organization code of
2690 * 0x080007 (Apple, for Appletalk) and a protocol
2691 * type of ETHERTYPE_ATALK (Appletalk).
2693 * 802.2-encapsulated ETHERTYPE_AARP packets are
2694 * SNAP packets with an organization code of
2695 * 0x000000 (encapsulated Ethernet) and a protocol
2696 * type of ETHERTYPE_AARP (Appletalk ARP).
2698 if (ll_proto
== ETHERTYPE_ATALK
)
2699 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2700 else /* ll_proto == ETHERTYPE_AARP */
2701 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2705 * Check for Ethernet encapsulation (Ethertalk
2706 * phase 1?); we just check for the Ethernet
2709 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2715 if (ll_proto
<= ETHERMTU
) {
2716 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2718 * This is an LLC SAP value, so the frames
2719 * that match would be 802.2 frames.
2720 * Check for the 802.2 protocol type
2721 * in the "Ethernet type" field, and
2722 * then check the DSAP.
2724 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2725 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2730 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2732 * This is an Ethernet type, so compare
2733 * the length/type field with it (if
2734 * the frame is an 802.2 frame, the length
2735 * field will be <= ETHERMTU, and, as
2736 * "ll_proto" is > ETHERMTU, this test
2737 * will fail and the frame won't match,
2738 * which is what we want).
2740 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2746 * Load a value relative to the beginning of the link-layer header after the
2749 static struct slist
*
2750 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2752 struct slist
*s1
, *s2
;
2755 * Generate code to load the length of the pflog header into
2756 * the register assigned to hold that length, if one has been
2757 * assigned. (If one hasn't been assigned, no code we've
2758 * generated uses that prefix, so we don't need to generate any
2761 if (cstate
->off_linkpl
.reg
!= -1) {
2763 * The length is in the first byte of the header.
2765 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2769 * Round it up to a multiple of 4.
2770 * Add 3, and clear the lower 2 bits.
2772 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2775 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2776 s2
->s
.k
= 0xfffffffc;
2780 * Now allocate a register to hold that value and store
2783 s2
= new_stmt(cstate
, BPF_ST
);
2784 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2788 * Now move it into the X register.
2790 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2798 static struct slist
*
2799 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2801 struct slist
*s1
, *s2
;
2802 struct slist
*sjeq_avs_cookie
;
2803 struct slist
*sjcommon
;
2806 * This code is not compatible with the optimizer, as
2807 * we are generating jmp instructions within a normal
2808 * slist of instructions
2810 cstate
->no_optimize
= 1;
2813 * Generate code to load the length of the radio header into
2814 * the register assigned to hold that length, if one has been
2815 * assigned. (If one hasn't been assigned, no code we've
2816 * generated uses that prefix, so we don't need to generate any
2819 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2820 * or always use the AVS header rather than the Prism header.
2821 * We load a 4-byte big-endian value at the beginning of the
2822 * raw packet data, and see whether, when masked with 0xFFFFF000,
2823 * it's equal to 0x80211000. If so, that indicates that it's
2824 * an AVS header (the masked-out bits are the version number).
2825 * Otherwise, it's a Prism header.
2827 * XXX - the Prism header is also, in theory, variable-length,
2828 * but no known software generates headers that aren't 144
2831 if (cstate
->off_linkhdr
.reg
!= -1) {
2835 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2839 * AND it with 0xFFFFF000.
2841 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2842 s2
->s
.k
= 0xFFFFF000;
2846 * Compare with 0x80211000.
2848 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2849 sjeq_avs_cookie
->s
.k
= 0x80211000;
2850 sappend(s1
, sjeq_avs_cookie
);
2855 * The 4 bytes at an offset of 4 from the beginning of
2856 * the AVS header are the length of the AVS header.
2857 * That field is big-endian.
2859 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2862 sjeq_avs_cookie
->s
.jt
= s2
;
2865 * Now jump to the code to allocate a register
2866 * into which to save the header length and
2867 * store the length there. (The "jump always"
2868 * instruction needs to have the k field set;
2869 * it's added to the PC, so, as we're jumping
2870 * over a single instruction, it should be 1.)
2872 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2874 sappend(s1
, sjcommon
);
2877 * Now for the code that handles the Prism header.
2878 * Just load the length of the Prism header (144)
2879 * into the A register. Have the test for an AVS
2880 * header branch here if we don't have an AVS header.
2882 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2885 sjeq_avs_cookie
->s
.jf
= s2
;
2888 * Now allocate a register to hold that value and store
2889 * it. The code for the AVS header will jump here after
2890 * loading the length of the AVS header.
2892 s2
= new_stmt(cstate
, BPF_ST
);
2893 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2895 sjcommon
->s
.jf
= s2
;
2898 * Now move it into the X register.
2900 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2908 static struct slist
*
2909 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2911 struct slist
*s1
, *s2
;
2914 * Generate code to load the length of the AVS header into
2915 * the register assigned to hold that length, if one has been
2916 * assigned. (If one hasn't been assigned, no code we've
2917 * generated uses that prefix, so we don't need to generate any
2920 if (cstate
->off_linkhdr
.reg
!= -1) {
2922 * The 4 bytes at an offset of 4 from the beginning of
2923 * the AVS header are the length of the AVS header.
2924 * That field is big-endian.
2926 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2930 * Now allocate a register to hold that value and store
2933 s2
= new_stmt(cstate
, BPF_ST
);
2934 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2938 * Now move it into the X register.
2940 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2948 static struct slist
*
2949 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2951 struct slist
*s1
, *s2
;
2954 * Generate code to load the length of the radiotap header into
2955 * the register assigned to hold that length, if one has been
2956 * assigned. (If one hasn't been assigned, no code we've
2957 * generated uses that prefix, so we don't need to generate any
2960 if (cstate
->off_linkhdr
.reg
!= -1) {
2962 * The 2 bytes at offsets of 2 and 3 from the beginning
2963 * of the radiotap header are the length of the radiotap
2964 * header; unfortunately, it's little-endian, so we have
2965 * to load it a byte at a time and construct the value.
2969 * Load the high-order byte, at an offset of 3, shift it
2970 * left a byte, and put the result in the X register.
2972 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2974 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2977 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2981 * Load the next byte, at an offset of 2, and OR the
2982 * value from the X register into it.
2984 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2987 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2991 * Now allocate a register to hold that value and store
2994 s2
= new_stmt(cstate
, BPF_ST
);
2995 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2999 * Now move it into the X register.
3001 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3010 * At the moment we treat PPI as normal Radiotap encoded
3011 * packets. The difference is in the function that generates
3012 * the code at the beginning to compute the header length.
3013 * Since this code generator of PPI supports bare 802.11
3014 * encapsulation only (i.e. the encapsulated DLT should be
3015 * DLT_IEEE802_11) we generate code to check for this too;
3016 * that's done in finish_parse().
3018 static struct slist
*
3019 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
3021 struct slist
*s1
, *s2
;
3024 * Generate code to load the length of the radiotap header
3025 * into the register assigned to hold that length, if one has
3028 if (cstate
->off_linkhdr
.reg
!= -1) {
3030 * The 2 bytes at offsets of 2 and 3 from the beginning
3031 * of the radiotap header are the length of the radiotap
3032 * header; unfortunately, it's little-endian, so we have
3033 * to load it a byte at a time and construct the value.
3037 * Load the high-order byte, at an offset of 3, shift it
3038 * left a byte, and put the result in the X register.
3040 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3042 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
3045 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3049 * Load the next byte, at an offset of 2, and OR the
3050 * value from the X register into it.
3052 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3055 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3059 * Now allocate a register to hold that value and store
3062 s2
= new_stmt(cstate
, BPF_ST
);
3063 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3067 * Now move it into the X register.
3069 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3078 * Load a value relative to the beginning of the link-layer header after the 802.11
3079 * header, i.e. LLC_SNAP.
3080 * The link-layer header doesn't necessarily begin at the beginning
3081 * of the packet data; there might be a variable-length prefix containing
3082 * radio information.
3084 static struct slist
*
3085 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
3088 struct slist
*sjset_data_frame_1
;
3089 struct slist
*sjset_data_frame_2
;
3090 struct slist
*sjset_qos
;
3091 struct slist
*sjset_radiotap_flags_present
;
3092 struct slist
*sjset_radiotap_ext_present
;
3093 struct slist
*sjset_radiotap_tsft_present
;
3094 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
3095 struct slist
*s_roundup
;
3097 if (cstate
->off_linkpl
.reg
== -1) {
3099 * No register has been assigned to the offset of
3100 * the link-layer payload, which means nobody needs
3101 * it; don't bother computing it - just return
3102 * what we already have.
3108 * This code is not compatible with the optimizer, as
3109 * we are generating jmp instructions within a normal
3110 * slist of instructions
3112 cstate
->no_optimize
= 1;
3115 * If "s" is non-null, it has code to arrange that the X register
3116 * contains the length of the prefix preceding the link-layer
3119 * Otherwise, the length of the prefix preceding the link-layer
3120 * header is "off_outermostlinkhdr.constant_part".
3124 * There is no variable-length header preceding the
3125 * link-layer header.
3127 * Load the length of the fixed-length prefix preceding
3128 * the link-layer header (if any) into the X register,
3129 * and store it in the cstate->off_linkpl.reg register.
3130 * That length is off_outermostlinkhdr.constant_part.
3132 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3133 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3137 * The X register contains the offset of the beginning of the
3138 * link-layer header; add 24, which is the minimum length
3139 * of the MAC header for a data frame, to that, and store it
3140 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3141 * which is at the offset in the X register, with an indexed load.
3143 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3145 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3148 s2
= new_stmt(cstate
, BPF_ST
);
3149 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3152 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3157 * Check the Frame Control field to see if this is a data frame;
3158 * a data frame has the 0x08 bit (b3) in that field set and the
3159 * 0x04 bit (b2) clear.
3161 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3162 sjset_data_frame_1
->s
.k
= IEEE80211_FC0_TYPE_DATA
;
3163 sappend(s
, sjset_data_frame_1
);
3166 * If b3 is set, test b2, otherwise go to the first statement of
3167 * the rest of the program.
3169 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3170 sjset_data_frame_2
->s
.k
= IEEE80211_FC0_TYPE_CTL
;
3171 sappend(s
, sjset_data_frame_2
);
3172 sjset_data_frame_1
->s
.jf
= snext
;
3175 * If b2 is not set, this is a data frame; test the QoS bit.
3176 * Otherwise, go to the first statement of the rest of the
3179 sjset_data_frame_2
->s
.jt
= snext
;
3180 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3181 sjset_qos
->s
.k
= IEEE80211_FC0_SUBTYPE_QOS
;
3182 sappend(s
, sjset_qos
);
3185 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3187 * Otherwise, go to the first statement of the rest of the
3190 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3191 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3193 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3196 s2
= new_stmt(cstate
, BPF_ST
);
3197 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3201 * If we have a radiotap header, look at it to see whether
3202 * there's Atheros padding between the MAC-layer header
3205 * Note: all of the fields in the radiotap header are
3206 * little-endian, so we byte-swap all of the values
3207 * we test against, as they will be loaded as big-endian
3210 * XXX - in the general case, we would have to scan through
3211 * *all* the presence bits, if there's more than one word of
3212 * presence bits. That would require a loop, meaning that
3213 * we wouldn't be able to run the filter in the kernel.
3215 * We assume here that the Atheros adapters that insert the
3216 * annoying padding don't have multiple antennae and therefore
3217 * do not generate radiotap headers with multiple presence words.
3219 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3221 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3222 * in the first presence flag word?
3224 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3228 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3229 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3230 sappend(s
, sjset_radiotap_flags_present
);
3233 * If not, skip all of this.
3235 sjset_radiotap_flags_present
->s
.jf
= snext
;
3238 * Otherwise, is the "extension" bit set in that word?
3240 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3241 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3242 sappend(s
, sjset_radiotap_ext_present
);
3243 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3246 * If so, skip all of this.
3248 sjset_radiotap_ext_present
->s
.jt
= snext
;
3251 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3253 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3254 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3255 sappend(s
, sjset_radiotap_tsft_present
);
3256 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3259 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3260 * at an offset of 16 from the beginning of the raw packet
3261 * data (8 bytes for the radiotap header and 8 bytes for
3264 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3267 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3270 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3272 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3273 sjset_tsft_datapad
->s
.k
= 0x20;
3274 sappend(s
, sjset_tsft_datapad
);
3277 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3278 * at an offset of 8 from the beginning of the raw packet
3279 * data (8 bytes for the radiotap header).
3281 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3284 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3287 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3289 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3290 sjset_notsft_datapad
->s
.k
= 0x20;
3291 sappend(s
, sjset_notsft_datapad
);
3294 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3295 * set, round the length of the 802.11 header to
3296 * a multiple of 4. Do that by adding 3 and then
3297 * dividing by and multiplying by 4, which we do by
3300 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3301 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3302 sappend(s
, s_roundup
);
3303 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3306 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3307 s2
->s
.k
= (bpf_u_int32
)~3;
3309 s2
= new_stmt(cstate
, BPF_ST
);
3310 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3313 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3314 sjset_tsft_datapad
->s
.jf
= snext
;
3315 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3316 sjset_notsft_datapad
->s
.jf
= snext
;
3318 sjset_qos
->s
.jf
= snext
;
3324 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3328 /* There is an implicit dependency between the link
3329 * payload and link header since the payload computation
3330 * includes the variable part of the header. Therefore,
3331 * if nobody else has allocated a register for the link
3332 * header and we need it, do it now. */
3333 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3334 cstate
->off_linkhdr
.reg
== -1)
3335 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3338 * For link-layer types that have a variable-length header
3339 * preceding the link-layer header, generate code to load
3340 * the offset of the link-layer header into the register
3341 * assigned to that offset, if any.
3343 * XXX - this, and the next switch statement, won't handle
3344 * encapsulation of 802.11 or 802.11+radio information in
3345 * some other protocol stack. That's significantly more
3348 switch (cstate
->outermostlinktype
) {
3350 case DLT_PRISM_HEADER
:
3351 s
= gen_load_prism_llprefixlen(cstate
);
3354 case DLT_IEEE802_11_RADIO_AVS
:
3355 s
= gen_load_avs_llprefixlen(cstate
);
3358 case DLT_IEEE802_11_RADIO
:
3359 s
= gen_load_radiotap_llprefixlen(cstate
);
3363 s
= gen_load_ppi_llprefixlen(cstate
);
3372 * For link-layer types that have a variable-length link-layer
3373 * header, generate code to load the offset of the link-layer
3374 * payload into the register assigned to that offset, if any.
3376 switch (cstate
->outermostlinktype
) {
3378 case DLT_IEEE802_11
:
3379 case DLT_PRISM_HEADER
:
3380 case DLT_IEEE802_11_RADIO_AVS
:
3381 case DLT_IEEE802_11_RADIO
:
3383 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3387 s
= gen_load_pflog_llprefixlen(cstate
);
3392 * If there is no initialization yet and we need variable
3393 * length offsets for VLAN, initialize them to zero
3395 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3398 if (cstate
->off_linkpl
.reg
== -1)
3399 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3400 if (cstate
->off_linktype
.reg
== -1)
3401 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3403 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3405 s2
= new_stmt(cstate
, BPF_ST
);
3406 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3408 s2
= new_stmt(cstate
, BPF_ST
);
3409 s2
->s
.k
= cstate
->off_linktype
.reg
;
3414 * If we have any offset-loading code, append all the
3415 * existing statements in the block to those statements,
3416 * and make the resulting list the list of statements
3420 sappend(s
, b
->stmts
);
3426 * Take an absolute offset, and:
3428 * if it has no variable part, return NULL;
3430 * if it has a variable part, generate code to load the register
3431 * containing that variable part into the X register, returning
3432 * a pointer to that code - if no register for that offset has
3433 * been allocated, allocate it first.
3435 * (The code to set that register will be generated later, but will
3436 * be placed earlier in the code sequence.)
3438 static struct slist
*
3439 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3443 if (off
->is_variable
) {
3444 if (off
->reg
== -1) {
3446 * We haven't yet assigned a register for the
3447 * variable part of the offset of the link-layer
3448 * header; allocate one.
3450 off
->reg
= alloc_reg(cstate
);
3454 * Load the register containing the variable part of the
3455 * offset of the link-layer header into the X register.
3457 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3462 * That offset isn't variable, there's no variable part,
3463 * so we don't need to generate any code.
3470 * Map an Ethernet type to the equivalent PPP type.
3473 ethertype_to_ppptype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3480 case ETHERTYPE_IPV6
:
3486 case ETHERTYPE_ATALK
:
3497 * I'm assuming the "Bridging PDU"s that go
3498 * over PPP are Spanning Tree Protocol
3506 assert_maxval(cstate
, "PPP protocol", ll_proto
, UINT16_MAX
);
3507 return (uint16_t)ll_proto
;
3511 * Generate any tests that, for encapsulation of a link-layer packet
3512 * inside another protocol stack, need to be done to check for those
3513 * link-layer packets (and that haven't already been done by a check
3514 * for that encapsulation).
3516 static struct block
*
3517 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3519 if (cstate
->is_encap
)
3520 return gen_encap_ll_check(cstate
);
3522 switch (cstate
->prevlinktype
) {
3526 * This is LANE-encapsulated Ethernet; check that the LANE
3527 * packet doesn't begin with an LE Control marker, i.e.
3528 * that it's data, not a control message.
3530 * (We've already generated a test for LANE.)
3532 return gen_cmp_ne(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3536 * No such tests are necessary.
3544 * The three different values we should check for when checking for an
3545 * IPv6 packet with DLT_NULL.
3547 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3548 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3549 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3552 * Generate code to match a particular packet type by matching the
3553 * link-layer type field or fields in the 802.2 LLC header.
3555 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3556 * value, if <= ETHERMTU.
3558 static struct block
*
3559 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3561 struct block
*b0
, *b1
, *b2
;
3563 /* are we checking MPLS-encapsulated packets? */
3564 if (cstate
->label_stack_depth
> 0)
3565 return gen_mpls_linktype(cstate
, ll_proto
);
3567 switch (cstate
->linktype
) {
3570 case DLT_NETANALYZER
:
3571 case DLT_NETANALYZER_TRANSPARENT
:
3572 /* Geneve has an EtherType regardless of whether there is an
3573 * L2 header. VXLAN always has an EtherType. */
3574 if (!cstate
->is_encap
)
3575 b0
= gen_prevlinkhdr_check(cstate
);
3579 b1
= gen_ether_linktype(cstate
, ll_proto
);
3587 assert_maxval(cstate
, "HDLC protocol", ll_proto
, UINT16_MAX
);
3591 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3595 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3599 case DLT_IEEE802_11
:
3600 case DLT_PRISM_HEADER
:
3601 case DLT_IEEE802_11_RADIO_AVS
:
3602 case DLT_IEEE802_11_RADIO
:
3605 * Check that we have a data frame.
3607 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3608 IEEE80211_FC0_TYPE_DATA
,
3609 IEEE80211_FC0_TYPE_MASK
);
3612 * Now check for the specified link-layer type.
3614 b1
= gen_llc_linktype(cstate
, ll_proto
);
3621 * XXX - check for LLC frames.
3623 return gen_llc_linktype(cstate
, ll_proto
);
3628 * XXX - check for LLC PDUs, as per IEEE 802.5.
3630 return gen_llc_linktype(cstate
, ll_proto
);
3633 case DLT_ATM_RFC1483
:
3635 case DLT_IP_OVER_FC
:
3636 return gen_llc_linktype(cstate
, ll_proto
);
3641 * Check for an LLC-encapsulated version of this protocol;
3642 * if we were checking for LANE, linktype would no longer
3645 * Check for LLC encapsulation and then check the protocol.
3647 b0
= gen_atm_prototype(cstate
, PT_LLC
);
3648 b1
= gen_llc_linktype(cstate
, ll_proto
);
3654 return gen_linux_sll_linktype(cstate
, ll_proto
);
3658 case DLT_SLIP_BSDOS
:
3661 * These types don't provide any type field; packets
3662 * are always IPv4 or IPv6.
3664 * XXX - for IPv4, check for a version number of 4, and,
3665 * for IPv6, check for a version number of 6?
3670 /* Check for a version number of 4. */
3671 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3673 case ETHERTYPE_IPV6
:
3674 /* Check for a version number of 6. */
3675 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3678 return gen_false(cstate
); /* always false */
3684 * Raw IPv4, so no type field.
3686 if (ll_proto
== ETHERTYPE_IP
)
3687 return gen_true(cstate
); /* always true */
3689 /* Checking for something other than IPv4; always false */
3690 return gen_false(cstate
);
3695 * Raw IPv6, so no type field.
3697 if (ll_proto
== ETHERTYPE_IPV6
)
3698 return gen_true(cstate
); /* always true */
3700 /* Checking for something other than IPv6; always false */
3701 return gen_false(cstate
);
3706 case DLT_PPP_SERIAL
:
3709 * We use Ethernet protocol types inside libpcap;
3710 * map them to the corresponding PPP protocol types.
3712 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3713 ethertype_to_ppptype(cstate
, ll_proto
));
3718 * We use Ethernet protocol types inside libpcap;
3719 * map them to the corresponding PPP protocol types.
3725 * Also check for Van Jacobson-compressed IP.
3726 * XXX - do this for other forms of PPP?
3728 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3729 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3731 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3736 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3737 ethertype_to_ppptype(cstate
, ll_proto
));
3747 return (gen_loopback_linktype(cstate
, AF_INET
));
3749 case ETHERTYPE_IPV6
:
3751 * AF_ values may, unfortunately, be platform-
3752 * dependent; AF_INET isn't, because everybody
3753 * used 4.2BSD's value, but AF_INET6 is, because
3754 * 4.2BSD didn't have a value for it (given that
3755 * IPv6 didn't exist back in the early 1980's),
3756 * and they all picked their own values.
3758 * This means that, if we're reading from a
3759 * savefile, we need to check for all the
3762 * If we're doing a live capture, we only need
3763 * to check for this platform's value; however,
3764 * Npcap uses 24, which isn't Windows's AF_INET6
3765 * value. (Given the multiple different values,
3766 * programs that read pcap files shouldn't be
3767 * checking for their platform's AF_INET6 value
3768 * anyway, they should check for all of the
3769 * possible values. and they might as well do
3770 * that even for live captures.)
3772 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3774 * Savefile - check for all three
3775 * possible IPv6 values.
3777 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3778 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3780 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3785 * Live capture, so we only need to
3786 * check for the value used on this
3791 * Npcap doesn't use Windows's AF_INET6,
3792 * as that collides with AF_IPX on
3793 * some BSDs (both have the value 23).
3794 * Instead, it uses 24.
3796 return (gen_loopback_linktype(cstate
, 24));
3799 return (gen_loopback_linktype(cstate
, AF_INET6
));
3800 #else /* AF_INET6 */
3802 * I guess this platform doesn't support
3803 * IPv6, so we just reject all packets.
3805 return gen_false(cstate
);
3806 #endif /* AF_INET6 */
3812 * Not a type on which we support filtering.
3813 * XXX - support those that have AF_ values
3814 * #defined on this platform, at least?
3816 return gen_false(cstate
);
3821 * af field is host byte order in contrast to the rest of
3824 if (ll_proto
== ETHERTYPE_IP
)
3825 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3827 else if (ll_proto
== ETHERTYPE_IPV6
)
3828 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3831 return gen_false(cstate
);
3835 case DLT_ARCNET_LINUX
:
3837 * XXX should we check for first fragment if the protocol
3843 return gen_false(cstate
);
3845 case ETHERTYPE_IPV6
:
3846 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3850 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3852 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3858 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3860 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3865 case ETHERTYPE_REVARP
:
3866 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3869 case ETHERTYPE_ATALK
:
3870 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3877 case ETHERTYPE_ATALK
:
3878 return gen_true(cstate
);
3880 return gen_false(cstate
);
3886 * XXX - assumes a 2-byte Frame Relay header with
3887 * DLCI and flags. What if the address is longer?
3893 * Check for the special NLPID for IP.
3895 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3897 case ETHERTYPE_IPV6
:
3899 * Check for the special NLPID for IPv6.
3901 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3905 * Check for several OSI protocols.
3907 * Frame Relay packets typically have an OSI
3908 * NLPID at the beginning; we check for each
3911 * What we check for is the NLPID and a frame
3912 * control field of UI, i.e. 0x03 followed
3915 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3916 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3917 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3923 return gen_false(cstate
);
3928 break; // not implemented
3930 case DLT_JUNIPER_MFR
:
3931 case DLT_JUNIPER_MLFR
:
3932 case DLT_JUNIPER_MLPPP
:
3933 case DLT_JUNIPER_ATM1
:
3934 case DLT_JUNIPER_ATM2
:
3935 case DLT_JUNIPER_PPPOE
:
3936 case DLT_JUNIPER_PPPOE_ATM
:
3937 case DLT_JUNIPER_GGSN
:
3938 case DLT_JUNIPER_ES
:
3939 case DLT_JUNIPER_MONITOR
:
3940 case DLT_JUNIPER_SERVICES
:
3941 case DLT_JUNIPER_ETHER
:
3942 case DLT_JUNIPER_PPP
:
3943 case DLT_JUNIPER_FRELAY
:
3944 case DLT_JUNIPER_CHDLC
:
3945 case DLT_JUNIPER_VP
:
3946 case DLT_JUNIPER_ST
:
3947 case DLT_JUNIPER_ISM
:
3948 case DLT_JUNIPER_VS
:
3949 case DLT_JUNIPER_SRX_E2E
:
3950 case DLT_JUNIPER_FIBRECHANNEL
:
3951 case DLT_JUNIPER_ATM_CEMIC
:
3953 /* just lets verify the magic number for now -
3954 * on ATM we may have up to 6 different encapsulations on the wire
3955 * and need a lot of heuristics to figure out that the payload
3958 * FIXME encapsulation specific BPF_ filters
3960 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3962 case DLT_BACNET_MS_TP
:
3963 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3966 return gen_ipnet_linktype(cstate
, ll_proto
);
3968 case DLT_LINUX_IRDA
:
3971 case DLT_MTP2_WITH_PHDR
:
3974 case DLT_LINUX_LAPD
:
3975 case DLT_USB_FREEBSD
:
3977 case DLT_USB_LINUX_MMAPPED
:
3979 case DLT_BLUETOOTH_HCI_H4
:
3980 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3982 case DLT_CAN_SOCKETCAN
:
3983 case DLT_IEEE802_15_4
:
3984 case DLT_IEEE802_15_4_LINUX
:
3985 case DLT_IEEE802_15_4_NONASK_PHY
:
3986 case DLT_IEEE802_15_4_NOFCS
:
3987 case DLT_IEEE802_15_4_TAP
:
3988 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3991 case DLT_IPMB_KONTRON
:
3995 /* Using the fixed-size NFLOG header it is possible to tell only
3996 * the address family of the packet, other meaningful data is
3997 * either missing or behind TLVs.
3999 break; // not implemented
4003 * Does this link-layer header type have a field
4004 * indicating the type of the next protocol? If
4005 * so, off_linktype.constant_part will be the offset of that
4006 * field in the packet; if not, it will be OFFSET_NOT_SET.
4008 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
4010 * Yes; assume it's an Ethernet type. (If
4011 * it's not, it needs to be handled specially
4014 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4015 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
4019 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
4020 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
4024 * Check for an LLC SNAP packet with a given organization code and
4025 * protocol type; we check the entire contents of the 802.2 LLC and
4026 * snap headers, checking for DSAP and SSAP of SNAP and a control
4027 * field of 0x03 in the LLC header, and for the specified organization
4028 * code and protocol type in the SNAP header.
4030 static struct block
*
4031 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
4033 u_char snapblock
[8];
4035 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
4036 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
4037 snapblock
[2] = 0x03; /* control = UI */
4038 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
4039 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
4040 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
4041 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
4042 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
4043 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
4047 * Generate code to match frames with an LLC header.
4049 static struct block
*
4050 gen_llc_internal(compiler_state_t
*cstate
)
4052 struct block
*b0
, *b1
;
4054 switch (cstate
->linktype
) {
4058 * We check for an Ethernet type field less or equal than
4059 * 1500, which means it's an 802.3 length field.
4061 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
4064 * Now check for the purported DSAP and SSAP not being
4065 * 0xFF, to rule out NetWare-over-802.3.
4067 b1
= gen_cmp_ne(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
4073 * We check for LLC traffic.
4075 return gen_atmtype_llc(cstate
);
4077 case DLT_IEEE802
: /* Token Ring */
4079 * XXX - check for LLC frames.
4081 return gen_true(cstate
);
4085 * XXX - check for LLC frames.
4087 return gen_true(cstate
);
4089 case DLT_ATM_RFC1483
:
4091 * For LLC encapsulation, these are defined to have an
4094 * For VC encapsulation, they don't, but there's no
4095 * way to check for that; the protocol used on the VC
4096 * is negotiated out of band.
4098 return gen_true(cstate
);
4100 case DLT_IEEE802_11
:
4101 case DLT_PRISM_HEADER
:
4102 case DLT_IEEE802_11_RADIO
:
4103 case DLT_IEEE802_11_RADIO_AVS
:
4106 * Check that we have a data frame.
4108 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4109 IEEE80211_FC0_TYPE_DATA
,
4110 IEEE80211_FC0_TYPE_MASK
);
4113 fail_kw_on_dlt(cstate
, "llc");
4119 gen_llc(compiler_state_t
*cstate
)
4122 * Catch errors reported by us and routines below us, and return NULL
4125 if (setjmp(cstate
->top_ctx
))
4128 return gen_llc_internal(cstate
);
4132 gen_llc_i(compiler_state_t
*cstate
)
4134 struct block
*b0
, *b1
;
4138 * Catch errors reported by us and routines below us, and return NULL
4141 if (setjmp(cstate
->top_ctx
))
4145 * Check whether this is an LLC frame.
4147 b0
= gen_llc_internal(cstate
);
4150 * Load the control byte and test the low-order bit; it must
4151 * be clear for I frames.
4153 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4154 b1
= gen_unset(cstate
, 0x01, s
);
4161 gen_llc_s(compiler_state_t
*cstate
)
4163 struct block
*b0
, *b1
;
4166 * Catch errors reported by us and routines below us, and return NULL
4169 if (setjmp(cstate
->top_ctx
))
4173 * Check whether this is an LLC frame.
4175 b0
= gen_llc_internal(cstate
);
4178 * Now compare the low-order 2 bit of the control byte against
4179 * the appropriate value for S frames.
4181 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4187 gen_llc_u(compiler_state_t
*cstate
)
4189 struct block
*b0
, *b1
;
4192 * Catch errors reported by us and routines below us, and return NULL
4195 if (setjmp(cstate
->top_ctx
))
4199 * Check whether this is an LLC frame.
4201 b0
= gen_llc_internal(cstate
);
4204 * Now compare the low-order 2 bit of the control byte against
4205 * the appropriate value for U frames.
4207 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4213 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4215 struct block
*b0
, *b1
;
4218 * Catch errors reported by us and routines below us, and return NULL
4221 if (setjmp(cstate
->top_ctx
))
4225 * Check whether this is an LLC frame.
4227 b0
= gen_llc_internal(cstate
);
4230 * Now check for an S frame with the appropriate type.
4232 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4238 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4240 struct block
*b0
, *b1
;
4243 * Catch errors reported by us and routines below us, and return NULL
4246 if (setjmp(cstate
->top_ctx
))
4250 * Check whether this is an LLC frame.
4252 b0
= gen_llc_internal(cstate
);
4255 * Now check for a U frame with the appropriate type.
4257 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4263 * Generate code to match a particular packet type, for link-layer types
4264 * using 802.2 LLC headers.
4266 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4267 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4269 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4270 * value, if <= ETHERMTU. We use that to determine whether to
4271 * match the DSAP or both DSAP and LSAP or to check the OUI and
4272 * protocol ID in a SNAP header.
4274 static struct block
*
4275 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4278 * XXX - handle token-ring variable-length header.
4284 case LLCSAP_NETBEUI
:
4286 * XXX - should we check both the DSAP and the
4287 * SSAP, like this, or should we check just the
4288 * DSAP, as we do for other SAP values?
4290 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4291 ((ll_proto
<< 8) | ll_proto
));
4295 * XXX - are there ever SNAP frames for IPX on
4296 * non-Ethernet 802.x networks?
4298 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4300 case ETHERTYPE_ATALK
:
4302 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4303 * SNAP packets with an organization code of
4304 * 0x080007 (Apple, for Appletalk) and a protocol
4305 * type of ETHERTYPE_ATALK (Appletalk).
4307 * XXX - check for an organization code of
4308 * encapsulated Ethernet as well?
4310 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4314 * XXX - we don't have to check for IPX 802.3
4315 * here, but should we check for the IPX Ethertype?
4317 if (ll_proto
<= ETHERMTU
) {
4318 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
4320 * This is an LLC SAP value, so check
4323 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4325 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4327 * This is an Ethernet type; we assume that it's
4328 * unlikely that it'll appear in the right place
4329 * at random, and therefore check only the
4330 * location that would hold the Ethernet type
4331 * in a SNAP frame with an organization code of
4332 * 0x000000 (encapsulated Ethernet).
4334 * XXX - if we were to check for the SNAP DSAP and
4335 * LSAP, as per XXX, and were also to check for an
4336 * organization code of 0x000000 (encapsulated
4337 * Ethernet), we'd do
4339 * return gen_snap(cstate, 0x000000, ll_proto);
4341 * here; for now, we don't, as per the above.
4342 * I don't know whether it's worth the extra CPU
4343 * time to do the right check or not.
4345 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4350 static struct block
*
4351 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4352 int dir
, u_int src_off
, u_int dst_off
)
4354 struct block
*b0
, *b1
;
4368 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4369 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4375 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4376 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4386 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4393 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4396 static struct block
*
4397 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4398 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4400 struct block
*b0
, *b1
;
4403 * Code below needs to access four separate 32-bit parts of the 128-bit
4404 * IPv6 address and mask. In some OSes this is as simple as using the
4405 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4406 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4407 * far as libpcap sees it. Hence copy the data before use to avoid
4408 * potential unaligned memory access and the associated compiler
4409 * warnings (whether genuine or not).
4411 bpf_u_int32 a
[4], m
[4];
4424 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4425 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4431 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4432 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4442 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4449 /* this order is important */
4450 memcpy(a
, addr
, sizeof(a
));
4451 memcpy(m
, mask
, sizeof(m
));
4453 for (int i
= 3; i
>= 0; i
--) {
4454 // Same as the Q_IP case in gen_host().
4455 if (m
[i
] == 0 && a
[i
] == 0)
4457 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4 * i
, BPF_W
,
4458 ntohl(a
[i
]), ntohl(m
[i
]));
4464 return b1
? b1
: gen_true(cstate
);
4468 * Like gen_mac48host(), but for DLT_IEEE802_11 (802.11 wireless LAN) and
4469 * various 802.11 + radio headers.
4471 static struct block
*
4472 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4474 register struct block
*b0
, *b1
, *b2
;
4475 register struct slist
*s
;
4477 #ifdef ENABLE_WLAN_FILTERING_PATCH
4480 * We need to disable the optimizer because the optimizer is buggy
4481 * and wipes out some LD instructions generated by the below
4482 * code to validate the Frame Control bits
4484 cstate
->no_optimize
= 1;
4485 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4492 * For control frames, there is no SA.
4494 * For management frames, SA is at an
4495 * offset of 10 from the beginning of
4498 * For data frames, SA is at an offset
4499 * of 10 from the beginning of the packet
4500 * if From DS is clear, at an offset of
4501 * 16 from the beginning of the packet
4502 * if From DS is set and To DS is clear,
4503 * and an offset of 24 from the beginning
4504 * of the packet if From DS is set and To DS
4509 * Generate the tests to be done for data frames
4512 * First, check for To DS set, i.e. check "link[1] & 0x01".
4514 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4515 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4518 * If To DS is set, the SA is at 24.
4520 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4524 * Now, check for To DS not set, i.e. check
4525 * "!(link[1] & 0x01)".
4527 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4528 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4531 * If To DS is not set, the SA is at 16.
4533 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4537 * Now OR together the last two checks. That gives
4538 * the complete set of checks for data frames with
4544 * Now check for From DS being set, and AND that with
4545 * the ORed-together checks.
4547 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4548 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4552 * Now check for data frames with From DS not set.
4554 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4555 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4558 * If From DS isn't set, the SA is at 10.
4560 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4564 * Now OR together the checks for data frames with
4565 * From DS not set and for data frames with From DS
4566 * set; that gives the checks done for data frames.
4571 * Now check for a data frame.
4572 * I.e, check "link[0] & 0x08".
4574 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4575 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4578 * AND that with the checks done for data frames.
4583 * If the high-order bit of the type value is 0, this
4584 * is a management frame.
4585 * I.e, check "!(link[0] & 0x08)".
4587 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4588 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4591 * For management frames, the SA is at 10.
4593 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4597 * OR that with the checks done for data frames.
4598 * That gives the checks done for management and
4604 * If the low-order bit of the type value is 1,
4605 * this is either a control frame or a frame
4606 * with a reserved type, and thus not a
4609 * I.e., check "!(link[0] & 0x04)".
4611 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4612 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4615 * AND that with the checks for data and management
4625 * For control frames, there is no DA.
4627 * For management frames, DA is at an
4628 * offset of 4 from the beginning of
4631 * For data frames, DA is at an offset
4632 * of 4 from the beginning of the packet
4633 * if To DS is clear and at an offset of
4634 * 16 from the beginning of the packet
4639 * Generate the tests to be done for data frames.
4641 * First, check for To DS set, i.e. "link[1] & 0x01".
4643 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4644 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4647 * If To DS is set, the DA is at 16.
4649 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4653 * Now, check for To DS not set, i.e. check
4654 * "!(link[1] & 0x01)".
4656 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4657 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4660 * If To DS is not set, the DA is at 4.
4662 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4666 * Now OR together the last two checks. That gives
4667 * the complete set of checks for data frames.
4672 * Now check for a data frame.
4673 * I.e, check "link[0] & 0x08".
4675 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4676 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4679 * AND that with the checks done for data frames.
4684 * If the high-order bit of the type value is 0, this
4685 * is a management frame.
4686 * I.e, check "!(link[0] & 0x08)".
4688 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4689 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4692 * For management frames, the DA is at 4.
4694 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4698 * OR that with the checks done for data frames.
4699 * That gives the checks done for management and
4705 * If the low-order bit of the type value is 1,
4706 * this is either a control frame or a frame
4707 * with a reserved type, and thus not a
4710 * I.e., check "!(link[0] & 0x04)".
4712 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4713 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4716 * AND that with the checks for data and management
4723 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4724 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4730 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4731 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4736 * XXX - add BSSID keyword?
4739 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4743 * Not present in CTS or ACK control frames.
4745 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4746 IEEE80211_FC0_TYPE_MASK
);
4747 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4748 IEEE80211_FC0_SUBTYPE_MASK
);
4749 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4750 IEEE80211_FC0_SUBTYPE_MASK
);
4753 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4759 * Not present in control frames.
4761 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4762 IEEE80211_FC0_TYPE_MASK
);
4763 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4769 * Present only if the direction mask has both "From DS"
4770 * and "To DS" set. Neither control frames nor management
4771 * frames should have both of those set, so we don't
4772 * check the frame type.
4774 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4775 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4776 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4782 * Not present in management frames; addr1 in other
4787 * If the high-order bit of the type value is 0, this
4788 * is a management frame.
4789 * I.e, check "(link[0] & 0x08)".
4791 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4792 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4797 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4800 * AND that with the check of addr1.
4807 * Not present in management frames; addr2, if present,
4812 * Not present in CTS or ACK control frames.
4814 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4815 IEEE80211_FC0_TYPE_MASK
);
4816 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4817 IEEE80211_FC0_SUBTYPE_MASK
);
4818 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4819 IEEE80211_FC0_SUBTYPE_MASK
);
4824 * If the high-order bit of the type value is 0, this
4825 * is a management frame.
4826 * I.e, check "(link[0] & 0x08)".
4828 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4829 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4832 * AND that with the check for frames other than
4833 * CTS and ACK frames.
4840 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4849 * This is quite tricky because there may be pad bytes in front of the
4850 * DECNET header, and then there are two possible data packet formats that
4851 * carry both src and dst addresses, plus 5 packet types in a format that
4852 * carries only the src node, plus 2 types that use a different format and
4853 * also carry just the src node.
4857 * Instead of doing those all right, we just look for data packets with
4858 * 0 or 1 bytes of padding. If you want to look at other packets, that
4859 * will require a lot more hacking.
4861 * To add support for filtering on DECNET "areas" (network numbers)
4862 * one would want to add a "mask" argument to this routine. That would
4863 * make the filter even more inefficient, although one could be clever
4864 * and not generate masking instructions if the mask is 0xFFFF.
4866 static struct block
*
4867 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4869 struct block
*b0
, *b1
, *b2
, *tmp
;
4870 u_int offset_lh
; /* offset if long header is received */
4871 u_int offset_sh
; /* offset if short header is received */
4876 offset_sh
= 1; /* follows flags */
4877 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4881 offset_sh
= 3; /* follows flags, dstnode */
4882 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4886 /* Inefficient because we do our Calvinball dance twice */
4887 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4888 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4894 /* Inefficient because we do our Calvinball dance twice */
4895 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4896 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4906 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4914 * In a DECnet message inside an Ethernet frame the first two bytes
4915 * immediately after EtherType are the [litle-endian] DECnet message
4916 * length, which is irrelevant in this context.
4918 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
4919 * 8-bit bitmap of the optional padding before the packet route header.
4920 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
4921 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
4922 * means there aren't any PAD bytes after the bitmap, so the header
4923 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
4924 * is set to 0, thus the header begins at the third byte.
4926 * The header can be in several (as mentioned above) formats, all of
4927 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
4928 * (PF, "pad field") set to 0 regardless of any padding present before
4929 * the header. "Short header" means bits 0-2 of the bitmap encode the
4930 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
4932 * To test PLENGTH and FLAGS, use multiple-byte constants with the
4933 * values and the masks, this maps to the required single bytes of
4934 * the message correctly on both big-endian and little-endian hosts.
4935 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
4936 * because the wire encoding is little-endian and BPF multiple-byte
4937 * loads are big-endian. When the destination address is near enough
4938 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
4941 /* Check for pad = 1, long header case */
4942 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
4943 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4944 BPF_H
, SWAPSHORT(addr
));
4946 /* Check for pad = 0, long header case */
4947 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
4948 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4952 /* Check for pad = 1, short header case */
4954 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
4955 0x81020000U
| SWAPSHORT(addr
),
4958 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
4959 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4964 /* Check for pad = 0, short header case */
4966 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
4967 0x02000000U
| SWAPSHORT(addr
) << 8,
4970 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
4971 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4981 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4982 * test the bottom-of-stack bit, and then check the version number
4983 * field in the IP header.
4985 static struct block
*
4986 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4988 struct block
*b0
, *b1
;
4993 /* match the bottom-of-stack bit */
4994 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4995 /* match the IPv4 version number */
4996 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5000 case ETHERTYPE_IPV6
:
5001 /* match the bottom-of-stack bit */
5002 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5003 /* match the IPv6 version number */
5004 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5009 /* FIXME add other L3 proto IDs */
5010 bpf_error(cstate
, "unsupported protocol over mpls");
5015 static struct block
*
5016 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5017 int proto
, int dir
, int type
)
5019 struct block
*b0
, *b1
;
5024 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5026 * Only check for non-IPv4 addresses if we're not
5027 * checking MPLS-encapsulated packets.
5029 if (cstate
->label_stack_depth
== 0) {
5030 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5032 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5038 // "link net NETNAME" and variations thereof
5039 break; // invalid qualifier
5042 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5044 * Belt and braces: if other code works correctly, any host
5045 * bits are clear and mask == 0 means addr == 0. In this case
5046 * the call to gen_hostop() would produce an "always true"
5047 * instruction block and ANDing it with the link type check
5050 if (mask
== 0 && addr
== 0)
5052 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5057 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5058 // Same as for Q_IP above.
5059 if (mask
== 0 && addr
== 0)
5061 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5066 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5067 // Same as for Q_IP above.
5068 if (mask
== 0 && addr
== 0)
5070 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5081 break; // invalid qualifier
5084 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5085 b1
= gen_dnhostop(cstate
, addr
, dir
);
5116 break; // invalid qualifier
5121 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5122 type
== Q_NET
? "ip net" : "ip host");
5126 static struct block
*
5127 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5128 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5130 struct block
*b0
, *b1
;
5136 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5137 // Same as the Q_IP case in gen_host().
5139 ! memcmp(mask
, &in6addr_any
, sizeof(struct in6_addr
)) &&
5140 ! memcmp(addr
, &in6addr_any
, sizeof(struct in6_addr
))
5143 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5185 break; // invalid qualifier
5190 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5191 type
== Q_NET
? "ip6 net" : "ip6 host");
5195 static unsigned char
5196 is_mac48_linktype(const int linktype
)
5202 case DLT_IEEE802_11
:
5203 case DLT_IEEE802_11_RADIO
:
5204 case DLT_IEEE802_11_RADIO_AVS
:
5205 case DLT_IP_OVER_FC
:
5206 case DLT_NETANALYZER
:
5207 case DLT_NETANALYZER_TRANSPARENT
:
5209 case DLT_PRISM_HEADER
:
5216 static struct block
*
5217 gen_mac48host(compiler_state_t
*cstate
, const u_char
*eaddr
, const u_char dir
,
5218 const char *keyword
)
5220 struct block
*b1
= NULL
;
5221 u_int src_off
, dst_off
;
5223 switch (cstate
->linktype
) {
5225 case DLT_NETANALYZER
:
5226 case DLT_NETANALYZER_TRANSPARENT
:
5227 b1
= gen_prevlinkhdr_check(cstate
);
5232 src_off
= 6 + 1 + cstate
->pcap_fddipad
;
5233 dst_off
= 0 + 1 + cstate
->pcap_fddipad
;
5239 case DLT_IEEE802_11
:
5240 case DLT_PRISM_HEADER
:
5241 case DLT_IEEE802_11_RADIO_AVS
:
5242 case DLT_IEEE802_11_RADIO
:
5244 return gen_wlanhostop(cstate
, eaddr
, dir
);
5245 case DLT_IP_OVER_FC
:
5247 * Assume that the addresses are IEEE 48-bit MAC addresses,
5248 * as RFC 2625 states.
5255 * This is LLC-multiplexed traffic; if it were
5256 * LANE, cstate->linktype would have been set to
5261 fail_kw_on_dlt(cstate
, keyword
);
5264 struct block
*b0
, *tmp
;
5268 b0
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5271 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5274 tmp
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5275 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5280 tmp
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5281 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5285 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5293 static struct block
*
5294 gen_mac48host_byname(compiler_state_t
*cstate
, const char *name
,
5295 const u_char dir
, const char *context
)
5297 if (! is_mac48_linktype(cstate
->linktype
))
5298 fail_kw_on_dlt(cstate
, context
);
5300 u_char
*eaddrp
= pcap_ether_hostton(name
);
5302 bpf_error(cstate
, ERRSTR_UNKNOWN_MAC48HOST
, name
);
5304 memcpy(eaddr
, eaddrp
, sizeof(eaddr
));
5307 return gen_mac48host(cstate
, eaddr
, dir
, context
);
5311 * This primitive is non-directional by design, so the grammar does not allow
5312 * to qualify it with a direction.
5314 static struct block
*
5315 gen_gateway(compiler_state_t
*cstate
, const char *name
,
5316 struct addrinfo
*alist
, int proto
)
5318 struct block
*b0
, *b1
, *tmp
;
5319 struct addrinfo
*ai
;
5320 struct sockaddr_in
*sin
;
5327 b0
= gen_mac48host_byname(cstate
, name
, Q_OR
, "gateway");
5329 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5331 * Does it have an address?
5333 if (ai
->ai_addr
!= NULL
) {
5335 * Yes. Is it an IPv4 address?
5337 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5339 * Generate an entry for it.
5341 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5342 tmp
= gen_host(cstate
,
5343 ntohl(sin
->sin_addr
.s_addr
),
5344 0xffffffff, proto
, Q_OR
, Q_HOST
);
5346 * Is it the *first* IPv4 address?
5350 * Yes, so start with it.
5355 * No, so OR it into the
5367 * No IPv4 addresses found.
5375 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5379 static struct block
*
5380 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5388 return gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
);
5391 return gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
);
5394 return gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
);
5397 return gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
);
5399 #ifndef IPPROTO_IGMP
5400 #define IPPROTO_IGMP 2
5404 return gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
);
5406 #ifndef IPPROTO_IGRP
5407 #define IPPROTO_IGRP 9
5410 return gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
);
5413 #define IPPROTO_PIM 103
5417 return gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
);
5419 #ifndef IPPROTO_VRRP
5420 #define IPPROTO_VRRP 112
5424 return gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
);
5426 #ifndef IPPROTO_CARP
5427 #define IPPROTO_CARP 112
5431 return gen_proto(cstate
, IPPROTO_CARP
, Q_IP
);
5434 return gen_linktype(cstate
, ETHERTYPE_IP
);
5437 return gen_linktype(cstate
, ETHERTYPE_ARP
);
5440 return gen_linktype(cstate
, ETHERTYPE_REVARP
);
5443 break; // invalid syntax
5446 return gen_linktype(cstate
, ETHERTYPE_ATALK
);
5449 return gen_linktype(cstate
, ETHERTYPE_AARP
);
5452 return gen_linktype(cstate
, ETHERTYPE_DN
);
5455 return gen_linktype(cstate
, ETHERTYPE_SCA
);
5458 return gen_linktype(cstate
, ETHERTYPE_LAT
);
5461 return gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5464 return gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5467 return gen_linktype(cstate
, ETHERTYPE_IPV6
);
5469 #ifndef IPPROTO_ICMPV6
5470 #define IPPROTO_ICMPV6 58
5473 return gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
);
5476 #define IPPROTO_AH 51
5479 return gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
);
5482 #define IPPROTO_ESP 50
5485 return gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
);
5488 return gen_linktype(cstate
, LLCSAP_ISONS
);
5491 return gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
);
5494 return gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
5496 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5497 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5498 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5500 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5502 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5504 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5508 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5509 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5510 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5512 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5514 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5516 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5520 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5521 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5522 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5524 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
);
5529 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5530 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5535 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5536 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5538 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5540 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5545 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5546 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5551 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5552 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5557 return gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
);
5560 return gen_linktype(cstate
, LLCSAP_8021D
);
5563 return gen_linktype(cstate
, LLCSAP_IPX
);
5566 return gen_linktype(cstate
, LLCSAP_NETBEUI
);
5569 break; // invalid syntax
5574 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5578 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5581 * Catch errors reported by us and routines below us, and return NULL
5584 if (setjmp(cstate
->top_ctx
))
5587 return gen_proto_abbrev_internal(cstate
, proto
);
5590 static struct block
*
5591 gen_ip_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5593 return gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5596 static struct block
*
5597 gen_ip6_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5599 return gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5602 static struct block
*
5603 gen_ipfrag(compiler_state_t
*cstate
)
5607 /* not IPv4 frag other than the first frag */
5608 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5609 return gen_unset(cstate
, 0x1fff, s
);
5613 * Generate a comparison to a port value in the transport-layer header
5614 * at the specified offset from the beginning of that header.
5616 * XXX - this handles a variable-length prefix preceding the link-layer
5617 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5618 * variable-length link-layer headers (such as Token Ring or 802.11
5621 static struct block
*
5622 gen_portatom(compiler_state_t
*cstate
, int off
, uint16_t v
)
5624 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5627 static struct block
*
5628 gen_portatom6(compiler_state_t
*cstate
, int off
, uint16_t v
)
5630 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5633 static struct block
*
5634 gen_port(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5636 struct block
*b1
, *tmp
;
5640 b1
= gen_portatom(cstate
, 0, port
);
5644 b1
= gen_portatom(cstate
, 2, port
);
5648 tmp
= gen_portatom(cstate
, 0, port
);
5649 b1
= gen_portatom(cstate
, 2, port
);
5655 tmp
= gen_portatom(cstate
, 0, port
);
5656 b1
= gen_portatom(cstate
, 2, port
);
5666 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5674 return gen_port_common(cstate
, proto
, b1
);
5677 static struct block
*
5678 gen_port_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5680 struct block
*b0
, *tmp
;
5685 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5686 * not LLC encapsulation with LLCSAP_IP.
5688 * For IEEE 802 networks - which includes 802.5 token ring
5689 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5690 * says that SNAP encapsulation is used, not LLC encapsulation
5693 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5694 * RFC 2225 say that SNAP encapsulation is used, not LLC
5695 * encapsulation with LLCSAP_IP.
5697 * So we always check for ETHERTYPE_IP.
5699 * At the time of this writing all three L4 protocols the "port" and
5700 * "portrange" primitives support (TCP, UDP and SCTP) have the source
5701 * and the destination ports identically encoded in the transport
5702 * protocol header. So without a proto qualifier the only difference
5703 * between the implemented cases is the protocol number and all other
5704 * checks need to be made exactly once.
5706 * If the expression syntax in future starts to support ports for
5707 * another L4 protocol that has unsigned integer ports encoded using a
5708 * different size and/or offset, this will require a different code.
5714 tmp
= gen_ip_proto(cstate
, (uint8_t)proto
);
5718 tmp
= gen_ip_proto(cstate
, IPPROTO_UDP
);
5719 gen_or(gen_ip_proto(cstate
, IPPROTO_TCP
), tmp
);
5720 gen_or(gen_ip_proto(cstate
, IPPROTO_SCTP
), tmp
);
5726 // Not a fragment other than the first fragment.
5727 b0
= gen_ipfrag(cstate
);
5731 gen_and(gen_linktype(cstate
, ETHERTYPE_IP
), b1
);
5735 static struct block
*
5736 gen_port6(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5738 struct block
*b1
, *tmp
;
5742 b1
= gen_portatom6(cstate
, 0, port
);
5746 b1
= gen_portatom6(cstate
, 2, port
);
5750 tmp
= gen_portatom6(cstate
, 0, port
);
5751 b1
= gen_portatom6(cstate
, 2, port
);
5757 tmp
= gen_portatom6(cstate
, 0, port
);
5758 b1
= gen_portatom6(cstate
, 2, port
);
5766 return gen_port6_common(cstate
, proto
, b1
);
5769 static struct block
*
5770 gen_port6_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5774 // "ip6 proto 'ip_proto'"
5779 tmp
= gen_ip6_proto(cstate
, (uint8_t)proto
);
5783 // Same as in gen_port_common().
5784 tmp
= gen_ip6_proto(cstate
, IPPROTO_UDP
);
5785 gen_or(gen_ip6_proto(cstate
, IPPROTO_TCP
), tmp
);
5786 gen_or(gen_ip6_proto(cstate
, IPPROTO_SCTP
), tmp
);
5792 // XXX - catch the first fragment of a fragmented packet?
5794 // "link proto \ip6"
5795 gen_and(gen_linktype(cstate
, ETHERTYPE_IPV6
), b1
);
5799 /* gen_portrange code */
5800 static struct block
*
5801 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5805 return gen_portatom(cstate
, off
, v1
);
5807 struct block
*b1
, *b2
;
5809 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, min(v1
, v2
));
5810 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, max(v1
, v2
));
5817 static struct block
*
5818 gen_portrange(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5821 struct block
*b1
, *tmp
;
5825 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5829 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5833 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5834 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5840 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5841 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5851 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5859 return gen_port_common(cstate
, proto
, b1
);
5862 static struct block
*
5863 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5867 return gen_portatom6(cstate
, off
, v1
);
5869 struct block
*b1
, *b2
;
5871 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, min(v1
, v2
));
5872 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, max(v1
, v2
));
5879 static struct block
*
5880 gen_portrange6(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5883 struct block
*b1
, *tmp
;
5887 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5891 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5895 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5896 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5902 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5903 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5911 return gen_port6_common(cstate
, proto
, b1
);
5915 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5924 v
= pcap_nametoproto(name
);
5925 if (v
== PROTO_UNDEF
)
5926 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5930 /* XXX should look up h/w protocol type based on cstate->linktype */
5931 v
= pcap_nametoeproto(name
);
5932 if (v
== PROTO_UNDEF
) {
5933 v
= pcap_nametollc(name
);
5934 if (v
== PROTO_UNDEF
)
5935 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5940 if (strcmp(name
, "esis") == 0)
5942 else if (strcmp(name
, "isis") == 0)
5944 else if (strcmp(name
, "clnp") == 0)
5947 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5957 #if !defined(NO_PROTOCHAIN)
5959 * This primitive is non-directional by design, so the grammar does not allow
5960 * to qualify it with a direction.
5962 static struct block
*
5963 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
5965 struct block
*b0
, *b
;
5966 struct slist
*s
[100];
5967 int fix2
, fix3
, fix4
, fix5
;
5968 int ahcheck
, again
, end
;
5970 int reg2
= alloc_reg(cstate
);
5972 memset(s
, 0, sizeof(s
));
5973 fix3
= fix4
= fix5
= 0;
5978 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
5981 b0
= gen_protochain(cstate
, v
, Q_IP
);
5982 b
= gen_protochain(cstate
, v
, Q_IPV6
);
5986 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
5991 * We don't handle variable-length prefixes before the link-layer
5992 * header, or variable-length link-layer headers, here yet.
5993 * We might want to add BPF instructions to do the protochain
5994 * work, to simplify that and, on platforms that have a BPF
5995 * interpreter with the new instructions, let the filtering
5996 * be done in the kernel. (We already require a modified BPF
5997 * engine to do the protochain stuff, to support backward
5998 * branches, and backward branch support is unlikely to appear
5999 * in kernel BPF engines.)
6001 if (cstate
->off_linkpl
.is_variable
)
6002 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6005 * To quote a comment in optimize.c:
6007 * "These data structures are used in a Cocke and Schwartz style
6008 * value numbering scheme. Since the flowgraph is acyclic,
6009 * exit values can be propagated from a node's predecessors
6010 * provided it is uniquely defined."
6012 * "Acyclic" means "no backward branches", which means "no
6013 * loops", so we have to turn the optimizer off.
6015 cstate
->no_optimize
= 1;
6018 * s[0] is a dummy entry to protect other BPF insn from damage
6019 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6020 * hard to find interdependency made by jump table fixup.
6023 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6028 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6031 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6032 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6034 /* X = ip->ip_hl << 2 */
6035 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6036 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6041 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6043 /* A = ip6->ip_nxt */
6044 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6045 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6047 /* X = sizeof(struct ip6_hdr) */
6048 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6054 bpf_error(cstate
, "unsupported proto to gen_protochain");
6058 /* again: if (A == v) goto end; else fall through; */
6060 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6062 s
[i
]->s
.jt
= NULL
; /*later*/
6063 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6067 #ifndef IPPROTO_NONE
6068 #define IPPROTO_NONE 59
6070 /* if (A == IPPROTO_NONE) goto end */
6071 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6072 s
[i
]->s
.jt
= NULL
; /*later*/
6073 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6074 s
[i
]->s
.k
= IPPROTO_NONE
;
6075 s
[fix5
]->s
.jf
= s
[i
];
6079 if (proto
== Q_IPV6
) {
6080 int v6start
, v6end
, v6advance
, j
;
6083 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6084 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6085 s
[i
]->s
.jt
= NULL
; /*later*/
6086 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6087 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6088 s
[fix2
]->s
.jf
= s
[i
];
6090 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6091 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6092 s
[i
]->s
.jt
= NULL
; /*later*/
6093 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6094 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6096 /* if (A == IPPROTO_ROUTING) goto v6advance */
6097 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6098 s
[i
]->s
.jt
= NULL
; /*later*/
6099 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6100 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6102 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6103 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6104 s
[i
]->s
.jt
= NULL
; /*later*/
6105 s
[i
]->s
.jf
= NULL
; /*later*/
6106 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6116 * A = P[X + packet head];
6117 * X = X + (P[X + packet head + 1] + 1) * 8;
6119 /* A = P[X + packet head] */
6120 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6121 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6124 s
[i
] = new_stmt(cstate
, BPF_ST
);
6127 /* A = P[X + packet head + 1]; */
6128 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6129 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6132 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6136 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6140 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6144 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6147 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6151 /* goto again; (must use BPF_JA for backward jump) */
6152 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6153 s
[i
]->s
.k
= again
- i
- 1;
6154 s
[i
- 1]->s
.jf
= s
[i
];
6158 for (j
= v6start
; j
<= v6end
; j
++)
6159 s
[j
]->s
.jt
= s
[v6advance
];
6162 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6164 s
[fix2
]->s
.jf
= s
[i
];
6170 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6171 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6172 s
[i
]->s
.jt
= NULL
; /*later*/
6173 s
[i
]->s
.jf
= NULL
; /*later*/
6174 s
[i
]->s
.k
= IPPROTO_AH
;
6176 s
[fix3
]->s
.jf
= s
[ahcheck
];
6183 * X = X + (P[X + 1] + 2) * 4;
6185 /* A = P[X + packet head]; */
6186 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6187 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6188 s
[i
- 1]->s
.jt
= s
[i
];
6191 s
[i
] = new_stmt(cstate
, BPF_ST
);
6195 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6198 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6202 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6204 /* A = P[X + packet head] */
6205 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6206 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6209 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6213 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6217 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6220 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6224 /* goto again; (must use BPF_JA for backward jump) */
6225 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6226 s
[i
]->s
.k
= again
- i
- 1;
6231 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6233 s
[fix2
]->s
.jt
= s
[end
];
6234 s
[fix4
]->s
.jf
= s
[end
];
6235 s
[fix5
]->s
.jt
= s
[end
];
6242 for (i
= 0; i
< max
- 1; i
++)
6243 s
[i
]->next
= s
[i
+ 1];
6244 s
[max
- 1]->next
= NULL
;
6248 * Remember, s[0] is dummy.
6250 b
= gen_jmp(cstate
, BPF_JEQ
, v
, s
[1]);
6252 free_reg(cstate
, reg2
);
6257 #endif /* !defined(NO_PROTOCHAIN) */
6260 * Generate code that checks whether the packet is a packet for protocol
6261 * <proto> and whether the type field in that protocol's header has
6262 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6263 * IP packet and checks the protocol number in the IP header against <v>.
6265 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6266 * against Q_IP and Q_IPV6.
6268 * This primitive is non-directional by design, so the grammar does not allow
6269 * to qualify it with a direction.
6271 static struct block
*
6272 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6274 struct block
*b0
, *b1
;
6279 b0
= gen_proto(cstate
, v
, Q_IP
);
6280 b1
= gen_proto(cstate
, v
, Q_IPV6
);
6285 return gen_linktype(cstate
, v
);
6288 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6290 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6291 * not LLC encapsulation with LLCSAP_IP.
6293 * For IEEE 802 networks - which includes 802.5 token ring
6294 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6295 * says that SNAP encapsulation is used, not LLC encapsulation
6298 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6299 * RFC 2225 say that SNAP encapsulation is used, not LLC
6300 * encapsulation with LLCSAP_IP.
6302 * So we always check for ETHERTYPE_IP.
6304 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6305 // 0 <= v <= UINT8_MAX
6306 b1
= gen_ip_proto(cstate
, (uint8_t)v
);
6324 break; // invalid qualifier
6327 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6328 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6330 * Also check for a fragment header before the final
6333 b2
= gen_ip6_proto(cstate
, IPPROTO_FRAGMENT
);
6334 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6336 // 0 <= v <= UINT8_MAX
6337 b2
= gen_ip6_proto(cstate
, (uint8_t)v
);
6348 break; // invalid qualifier
6351 assert_maxval(cstate
, "ISO protocol", v
, UINT8_MAX
);
6352 switch (cstate
->linktype
) {
6356 * Frame Relay packets typically have an OSI
6357 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6358 * generates code to check for all the OSI
6359 * NLPIDs, so calling it and then adding a check
6360 * for the particular NLPID for which we're
6361 * looking is bogus, as we can just check for
6364 * What we check for is the NLPID and a frame
6365 * control field value of UI, i.e. 0x03 followed
6368 * XXX - assumes a 2-byte Frame Relay header with
6369 * DLCI and flags. What if the address is longer?
6371 * XXX - what about SNAP-encapsulated frames?
6373 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6379 * Cisco uses an Ethertype lookalike - for OSI,
6382 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6383 /* OSI in C-HDLC is stuffed with a fudge byte */
6384 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6389 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6390 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6396 break; // invalid qualifier
6399 assert_maxval(cstate
, "IS-IS PDU type", v
, ISIS_PDU_TYPE_MAX
);
6400 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
6402 * 4 is the offset of the PDU type relative to the IS-IS
6404 * Except when it is not, see above.
6406 unsigned pdu_type_offset
;
6407 switch (cstate
->linktype
) {
6410 pdu_type_offset
= 5;
6413 pdu_type_offset
= 4;
6415 b1
= gen_mcmp(cstate
, OR_LINKPL_NOSNAP
, pdu_type_offset
, BPF_B
,
6416 v
, ISIS_PDU_TYPE_MAX
);
6433 break; // invalid qualifier
6439 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6444 * Convert a non-numeric name to a port number.
6447 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6449 struct addrinfo hints
, *res
, *ai
;
6451 struct sockaddr_in
*in4
;
6452 struct sockaddr_in6
*in6
;
6456 * We check for both TCP and UDP in case there are
6457 * ambiguous entries.
6459 memset(&hints
, 0, sizeof(hints
));
6460 hints
.ai_family
= PF_UNSPEC
;
6461 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6462 hints
.ai_protocol
= ipproto
;
6463 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6470 * No such port. Just return -1.
6477 * We don't use strerror() because it's not
6478 * guaranteed to be thread-safe on all platforms
6479 * (probably because it might use a non-thread-local
6480 * buffer into which to format an error message
6481 * if the error code isn't one for which it has
6482 * a canned string; three cheers for C string
6485 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6487 port
= -2; /* a real error */
6493 * This is a real error, not just "there's
6494 * no such service name".
6496 * We don't use gai_strerror() because it's not
6497 * guaranteed to be thread-safe on all platforms
6498 * (probably because it might use a non-thread-local
6499 * buffer into which to format an error message
6500 * if the error code isn't one for which it has
6501 * a canned string; three cheers for C string
6504 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6506 port
= -2; /* a real error */
6511 * OK, we found it. Did it find anything?
6513 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6515 * Does it have an address?
6517 if (ai
->ai_addr
!= NULL
) {
6519 * Yes. Get a port number; we're done.
6521 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6522 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6523 port
= ntohs(in4
->sin_port
);
6526 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6527 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6528 port
= ntohs(in6
->sin6_port
);
6539 * Convert a string to a port number.
6542 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6552 * See if it's a number.
6554 ret
= stoulen(string
, string_size
, &val
, cstate
);
6558 /* Unknown port type - it's just a number. */
6559 *proto
= PROTO_UNDEF
;
6562 case STOULEN_NOT_OCTAL_NUMBER
:
6563 case STOULEN_NOT_HEX_NUMBER
:
6564 case STOULEN_NOT_DECIMAL_NUMBER
:
6566 * Not a valid number; try looking it up as a port.
6568 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6569 memcpy(cpy
, string
, string_size
);
6570 cpy
[string_size
] = '\0';
6571 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6572 if (tcp_port
== -2) {
6574 * We got a hard error; the error string has
6578 longjmp(cstate
->top_ctx
, 1);
6581 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6582 if (udp_port
== -2) {
6584 * We got a hard error; the error string has
6588 longjmp(cstate
->top_ctx
, 1);
6593 * We need to check /etc/services for ambiguous entries.
6594 * If we find an ambiguous entry, and it has the
6595 * same port number, change the proto to PROTO_UNDEF
6596 * so both TCP and UDP will be checked.
6598 if (tcp_port
>= 0) {
6599 val
= (bpf_u_int32
)tcp_port
;
6600 *proto
= IPPROTO_TCP
;
6601 if (udp_port
>= 0) {
6602 if (udp_port
== tcp_port
)
6603 *proto
= PROTO_UNDEF
;
6606 /* Can't handle ambiguous names that refer
6607 to different port numbers. */
6608 warning("ambiguous port %s in /etc/services",
6615 if (udp_port
>= 0) {
6616 val
= (bpf_u_int32
)udp_port
;
6617 *proto
= IPPROTO_UDP
;
6621 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6623 longjmp(cstate
->top_ctx
, 1);
6630 /* Error already set. */
6631 longjmp(cstate
->top_ctx
, 1);
6638 /* Should not happen */
6639 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6640 longjmp(cstate
->top_ctx
, 1);
6647 * Convert a string in the form PPP-PPP, which correspond to ports, to
6648 * a starting and ending port in a port range.
6651 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6652 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6655 const char *first
, *second
;
6656 size_t first_size
, second_size
;
6659 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6660 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6663 * Make sure there are no other hyphens.
6665 * XXX - we support named ports, but there are some port names
6666 * in /etc/services that include hyphens, so this would rule
6669 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6670 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6674 * Get the length of the first port.
6677 first_size
= hyphen_off
- string
;
6678 if (first_size
== 0) {
6679 /* Range of "-port", which we don't support. */
6680 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6684 * Try to convert it to a port.
6686 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6687 save_proto
= *proto
;
6690 * Get the length of the second port.
6692 second
= hyphen_off
+ 1;
6693 second_size
= strlen(second
);
6694 if (second_size
== 0) {
6695 /* Range of "port-", which we don't support. */
6696 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6700 * Try to convert it to a port.
6702 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6703 if (*proto
!= save_proto
)
6704 *proto
= PROTO_UNDEF
;
6708 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6710 int proto
= q
.proto
;
6713 bpf_u_int32 mask
, addr
;
6714 struct addrinfo
*res
, *res0
;
6715 struct sockaddr_in
*sin4
;
6717 struct sockaddr_in6
*sin6
;
6718 struct in6_addr mask128
;
6719 struct block
*b
, *tmp
;
6720 int port
, real_proto
;
6721 bpf_u_int32 port1
, port2
;
6724 * Catch errors reported by us and routines below us, and return NULL
6727 if (setjmp(cstate
->top_ctx
))
6733 addr
= pcap_nametonetaddr(name
);
6735 bpf_error(cstate
, "unknown network '%s'", name
);
6736 /* Left justify network addr and calculate its network mask */
6738 while (addr
&& (addr
& 0xff000000) == 0) {
6742 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6746 if (proto
== Q_LINK
) {
6747 return gen_mac48host_byname(cstate
, name
, q
.dir
, "link host NAME");
6748 } else if (proto
== Q_DECNET
) {
6750 * A long time ago on Ultrix libpcap supported
6751 * translation of DECnet host names into DECnet
6752 * addresses, but this feature is history now.
6754 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6756 memset(&mask128
, 0xff, sizeof(mask128
));
6757 res0
= res
= pcap_nametoaddrinfo(name
);
6759 bpf_error(cstate
, "unknown host '%s'", name
);
6764 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6765 tproto
== Q_DEFAULT
) {
6769 for (res
= res0
; res
; res
= res
->ai_next
) {
6770 switch (res
->ai_family
) {
6772 if (tproto
== Q_IPV6
)
6775 sin4
= (struct sockaddr_in
*)
6777 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6778 0xffffffff, tproto
, dir
, q
.addr
);
6781 if (tproto6
== Q_IP
)
6784 sin6
= (struct sockaddr_in6
*)
6786 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6787 &mask128
, tproto6
, dir
, q
.addr
);
6799 bpf_error(cstate
, "unknown host '%s'%s", name
,
6800 (proto
== Q_DEFAULT
)
6802 : " for specified address family");
6808 (void)port_pq_to_ipproto(cstate
, proto
, "port"); // validate only
6809 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6810 bpf_error(cstate
, "unknown port '%s'", name
);
6811 if (proto
== Q_UDP
) {
6812 if (real_proto
== IPPROTO_TCP
)
6813 bpf_error(cstate
, "port '%s' is tcp", name
);
6814 else if (real_proto
== IPPROTO_SCTP
)
6815 bpf_error(cstate
, "port '%s' is sctp", name
);
6817 /* override PROTO_UNDEF */
6818 real_proto
= IPPROTO_UDP
;
6820 if (proto
== Q_TCP
) {
6821 if (real_proto
== IPPROTO_UDP
)
6822 bpf_error(cstate
, "port '%s' is udp", name
);
6824 else if (real_proto
== IPPROTO_SCTP
)
6825 bpf_error(cstate
, "port '%s' is sctp", name
);
6827 /* override PROTO_UNDEF */
6828 real_proto
= IPPROTO_TCP
;
6830 if (proto
== Q_SCTP
) {
6831 if (real_proto
== IPPROTO_UDP
)
6832 bpf_error(cstate
, "port '%s' is udp", name
);
6834 else if (real_proto
== IPPROTO_TCP
)
6835 bpf_error(cstate
, "port '%s' is tcp", name
);
6837 /* override PROTO_UNDEF */
6838 real_proto
= IPPROTO_SCTP
;
6841 bpf_error(cstate
, "illegal port number %d < 0", port
);
6843 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6844 // real_proto can be PROTO_UNDEF
6845 b
= gen_port(cstate
, (uint16_t)port
, real_proto
, dir
);
6846 gen_or(gen_port6(cstate
, (uint16_t)port
, real_proto
, dir
), b
);
6850 (void)port_pq_to_ipproto(cstate
, proto
, "portrange"); // validate only
6851 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
6852 if (proto
== Q_UDP
) {
6853 if (real_proto
== IPPROTO_TCP
)
6854 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6855 else if (real_proto
== IPPROTO_SCTP
)
6856 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6858 /* override PROTO_UNDEF */
6859 real_proto
= IPPROTO_UDP
;
6861 if (proto
== Q_TCP
) {
6862 if (real_proto
== IPPROTO_UDP
)
6863 bpf_error(cstate
, "port in range '%s' is udp", name
);
6864 else if (real_proto
== IPPROTO_SCTP
)
6865 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6867 /* override PROTO_UNDEF */
6868 real_proto
= IPPROTO_TCP
;
6870 if (proto
== Q_SCTP
) {
6871 if (real_proto
== IPPROTO_UDP
)
6872 bpf_error(cstate
, "port in range '%s' is udp", name
);
6873 else if (real_proto
== IPPROTO_TCP
)
6874 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6876 /* override PROTO_UNDEF */
6877 real_proto
= IPPROTO_SCTP
;
6880 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6882 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6884 // real_proto can be PROTO_UNDEF
6885 b
= gen_portrange(cstate
, (uint16_t)port1
, (uint16_t)port2
,
6887 gen_or(gen_portrange6(cstate
, (uint16_t)port1
, (uint16_t)port2
,
6888 real_proto
, dir
), b
);
6892 res
= pcap_nametoaddrinfo(name
);
6895 bpf_error(cstate
, "unknown host '%s'", name
);
6896 b
= gen_gateway(cstate
, name
, res
, proto
);
6900 bpf_error(cstate
, "unknown host '%s'", name
);
6904 real_proto
= lookup_proto(cstate
, name
, proto
);
6905 if (real_proto
>= 0)
6906 return gen_proto(cstate
, real_proto
, proto
);
6908 bpf_error(cstate
, "unknown protocol: %s", name
);
6910 #if !defined(NO_PROTOCHAIN)
6912 real_proto
= lookup_proto(cstate
, name
, proto
);
6913 if (real_proto
>= 0)
6914 return gen_protochain(cstate
, real_proto
, proto
);
6916 bpf_error(cstate
, "unknown protocol: %s", name
);
6917 #endif /* !defined(NO_PROTOCHAIN) */
6928 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6929 bpf_u_int32 masklen
, struct qual q
)
6931 register int nlen
, mlen
;
6936 * Catch errors reported by us and routines below us, and return NULL
6939 if (setjmp(cstate
->top_ctx
))
6942 nlen
= pcapint_atoin(s1
, &n
);
6944 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
6945 /* Promote short ipaddr */
6949 mlen
= pcapint_atoin(s2
, &m
);
6951 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
6952 /* Promote short ipaddr */
6955 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6958 /* Convert mask len to mask */
6960 bpf_error(cstate
, "mask length must be <= 32");
6961 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
6962 m
= (bpf_u_int32
)m64
;
6964 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6971 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6974 // Q_HOST and Q_GATEWAY only (see the grammar)
6975 bpf_error(cstate
, "Mask syntax for networks only");
6982 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6990 * Catch errors reported by us and routines below us, and return NULL
6993 if (setjmp(cstate
->top_ctx
))
7000 * v contains a 32-bit unsigned parsed from a string of the
7001 * form {N}, which could be decimal, hexadecimal or octal.
7002 * Although it would be possible to use the value as a raw
7003 * 16-bit DECnet address when the value fits into 16 bits, this
7004 * would be a questionable feature: DECnet address wire
7005 * encoding is little-endian, so this would not work as
7006 * intuitively as the same works for [big-endian] IPv4
7007 * addresses (0x01020304 means 1.2.3.4).
7009 if (proto
== Q_DECNET
)
7010 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7012 } else if (proto
== Q_DECNET
) {
7014 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7015 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7016 * for a valid DECnet address.
7018 vlen
= pcapint_atodn(s
, &v
);
7020 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7023 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7024 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7027 vlen
= pcapint_atoin(s
, &v
);
7029 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7037 if (proto
== Q_DECNET
)
7038 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7039 else if (proto
== Q_LINK
) {
7040 // "link (host|net) IPV4ADDR" and variations thereof
7041 bpf_error(cstate
, "illegal link layer address");
7044 if (s
== NULL
&& q
.addr
== Q_NET
) {
7045 /* Promote short net number */
7046 while (v
&& (v
& 0xff000000) == 0) {
7051 /* Promote short ipaddr */
7053 mask
<<= 32 - vlen
;
7055 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7059 proto
= port_pq_to_ipproto(cstate
, proto
, "port");
7062 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7064 // proto can be PROTO_UNDEF
7067 b
= gen_port(cstate
, (uint16_t)v
, proto
, dir
);
7068 gen_or(gen_port6(cstate
, (uint16_t)v
, proto
, dir
), b
);
7073 proto
= port_pq_to_ipproto(cstate
, proto
, "portrange");
7076 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7078 // proto can be PROTO_UNDEF
7081 b
= gen_portrange(cstate
, (uint16_t)v
, (uint16_t)v
,
7083 gen_or(gen_portrange6(cstate
, (uint16_t)v
, (uint16_t)v
,
7089 bpf_error(cstate
, "'gateway' requires a name");
7093 return gen_proto(cstate
, v
, proto
);
7095 #if !defined(NO_PROTOCHAIN)
7097 return gen_protochain(cstate
, v
, proto
);
7112 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7115 struct addrinfo
*res
;
7116 struct in6_addr
*addr
;
7117 struct in6_addr mask
;
7119 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7122 * Catch errors reported by us and routines below us, and return NULL
7125 if (setjmp(cstate
->top_ctx
))
7128 res
= pcap_nametoaddrinfo(s
);
7130 bpf_error(cstate
, "invalid ip6 address %s", s
);
7133 bpf_error(cstate
, "%s resolved to multiple address", s
);
7134 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7136 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7137 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7138 memset(&mask
, 0, sizeof(mask
));
7139 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7141 mask
.s6_addr
[masklen
/ 8] =
7142 (0xff << (8 - masklen
% 8)) & 0xff;
7145 memcpy(a
, addr
, sizeof(a
));
7146 memcpy(m
, &mask
, sizeof(m
));
7147 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7148 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7149 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7157 bpf_error(cstate
, "Mask syntax for networks only");
7161 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7167 // Q_GATEWAY only (see the grammar)
7168 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7174 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7177 * Catch errors reported by us and routines below us, and return NULL
7180 if (setjmp(cstate
->top_ctx
))
7183 const char *context
= "link host XX:XX:XX:XX:XX:XX";
7185 if (! ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
))
7186 bpf_error(cstate
, "ethernet address used in non-ether expression");
7187 if (! is_mac48_linktype(cstate
->linktype
))
7188 fail_kw_on_dlt(cstate
, context
);
7190 u_char
*eaddrp
= pcap_ether_aton(s
);
7192 bpf_error(cstate
, "malloc");
7194 memcpy(eaddr
, eaddrp
, sizeof(eaddr
));
7197 return gen_mac48host(cstate
, eaddr
, q
.dir
, context
);
7201 sappend(struct slist
*s0
, struct slist
*s1
)
7204 * This is definitely not the best way to do this, but the
7205 * lists will rarely get long.
7212 static struct slist
*
7213 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7217 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7222 static struct slist
*
7223 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7227 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7233 * Modify "index" to use the value stored into its register as an
7234 * offset relative to the beginning of the header for the protocol
7235 * "proto", and allocate a register and put an item "size" bytes long
7236 * (1, 2, or 4) at that offset into that register, making it the register
7239 static struct arth
*
7240 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7244 struct slist
*s
, *tmp
;
7246 int regno
= alloc_reg(cstate
);
7248 free_reg(cstate
, inst
->regno
);
7252 bpf_error(cstate
, "data size must be 1, 2, or 4");
7269 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7273 * The offset is relative to the beginning of the packet
7274 * data, if we have a radio header. (If we don't, this
7277 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7278 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7279 cstate
->linktype
!= DLT_PRISM_HEADER
)
7280 bpf_error(cstate
, "radio information not present in capture");
7283 * Load into the X register the offset computed into the
7284 * register specified by "index".
7286 s
= xfer_to_x(cstate
, inst
);
7289 * Load the item at that offset.
7291 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7293 sappend(inst
->s
, s
);
7298 * The offset is relative to the beginning of
7299 * the link-layer header.
7301 * XXX - what about ATM LANE? Should the index be
7302 * relative to the beginning of the AAL5 frame, so
7303 * that 0 refers to the beginning of the LE Control
7304 * field, or relative to the beginning of the LAN
7305 * frame, so that 0 refers, for Ethernet LANE, to
7306 * the beginning of the destination address?
7308 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7311 * If "s" is non-null, it has code to arrange that the
7312 * X register contains the length of the prefix preceding
7313 * the link-layer header. Add to it the offset computed
7314 * into the register specified by "index", and move that
7315 * into the X register. Otherwise, just load into the X
7316 * register the offset computed into the register specified
7320 sappend(s
, xfer_to_a(cstate
, inst
));
7321 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7322 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7324 s
= xfer_to_x(cstate
, inst
);
7327 * Load the item at the sum of the offset we've put in the
7328 * X register and the offset of the start of the link
7329 * layer header (which is 0 if the radio header is
7330 * variable-length; that header length is what we put
7331 * into the X register and then added to the index).
7333 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7334 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7336 sappend(inst
->s
, s
);
7350 * The offset is relative to the beginning of
7351 * the network-layer header.
7352 * XXX - are there any cases where we want
7353 * cstate->off_nl_nosnap?
7355 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7358 * If "s" is non-null, it has code to arrange that the
7359 * X register contains the variable part of the offset
7360 * of the link-layer payload. Add to it the offset
7361 * computed into the register specified by "index",
7362 * and move that into the X register. Otherwise, just
7363 * load into the X register the offset computed into
7364 * the register specified by "index".
7367 sappend(s
, xfer_to_a(cstate
, inst
));
7368 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7369 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7371 s
= xfer_to_x(cstate
, inst
);
7374 * Load the item at the sum of the offset we've put in the
7375 * X register, the offset of the start of the network
7376 * layer header from the beginning of the link-layer
7377 * payload, and the constant part of the offset of the
7378 * start of the link-layer payload.
7380 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7381 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7383 sappend(inst
->s
, s
);
7386 * Do the computation only if the packet contains
7387 * the protocol in question.
7389 b
= gen_proto_abbrev_internal(cstate
, proto
);
7391 gen_and(inst
->b
, b
);
7405 * The offset is relative to the beginning of
7406 * the transport-layer header.
7408 * Load the X register with the length of the IPv4 header
7409 * (plus the offset of the link-layer header, if it's
7410 * a variable-length header), in bytes.
7412 * XXX - are there any cases where we want
7413 * cstate->off_nl_nosnap?
7414 * XXX - we should, if we're built with
7415 * IPv6 support, generate code to load either
7416 * IPv4, IPv6, or both, as appropriate.
7418 s
= gen_loadx_iphdrlen(cstate
);
7421 * The X register now contains the sum of the variable
7422 * part of the offset of the link-layer payload and the
7423 * length of the network-layer header.
7425 * Load into the A register the offset relative to
7426 * the beginning of the transport layer header,
7427 * add the X register to that, move that to the
7428 * X register, and load with an offset from the
7429 * X register equal to the sum of the constant part of
7430 * the offset of the link-layer payload and the offset,
7431 * relative to the beginning of the link-layer payload,
7432 * of the network-layer header.
7434 sappend(s
, xfer_to_a(cstate
, inst
));
7435 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7436 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7437 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7438 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7439 sappend(inst
->s
, s
);
7442 * Do the computation only if the packet contains
7443 * the protocol in question - which is true only
7444 * if this is an IP datagram and is the first or
7445 * only fragment of that datagram.
7447 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7449 gen_and(inst
->b
, b
);
7450 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7455 * Do the computation only if the packet contains
7456 * the protocol in question.
7458 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7460 gen_and(inst
->b
, b
);
7464 * Check if we have an icmp6 next header
7466 b
= gen_ip6_proto(cstate
, 58);
7468 gen_and(inst
->b
, b
);
7471 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7473 * If "s" is non-null, it has code to arrange that the
7474 * X register contains the variable part of the offset
7475 * of the link-layer payload. Add to it the offset
7476 * computed into the register specified by "index",
7477 * and move that into the X register. Otherwise, just
7478 * load into the X register the offset computed into
7479 * the register specified by "index".
7482 sappend(s
, xfer_to_a(cstate
, inst
));
7483 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7484 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7486 s
= xfer_to_x(cstate
, inst
);
7489 * Load the item at the sum of the offset we've put in the
7490 * X register, the offset of the start of the network
7491 * layer header from the beginning of the link-layer
7492 * payload, and the constant part of the offset of the
7493 * start of the link-layer payload.
7495 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7496 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7499 sappend(inst
->s
, s
);
7503 inst
->regno
= regno
;
7504 s
= new_stmt(cstate
, BPF_ST
);
7506 sappend(inst
->s
, s
);
7512 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7516 * Catch errors reported by us and routines below us, and return NULL
7519 if (setjmp(cstate
->top_ctx
))
7522 return gen_load_internal(cstate
, proto
, inst
, size
);
7525 static struct block
*
7526 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7527 struct arth
*a1
, int reversed
)
7529 struct slist
*s0
, *s1
, *s2
;
7530 struct block
*b
, *tmp
;
7532 s0
= xfer_to_x(cstate
, a1
);
7533 s1
= xfer_to_a(cstate
, a0
);
7534 if (code
== BPF_JEQ
) {
7535 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7536 b
= new_block(cstate
, JMP(code
));
7540 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7546 sappend(a0
->s
, a1
->s
);
7550 free_reg(cstate
, a0
->regno
);
7551 free_reg(cstate
, a1
->regno
);
7553 /* 'and' together protocol checks */
7556 gen_and(a0
->b
, tmp
= a1
->b
);
7570 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7571 struct arth
*a1
, int reversed
)
7574 * Catch errors reported by us and routines below us, and return NULL
7577 if (setjmp(cstate
->top_ctx
))
7580 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7584 gen_loadlen(compiler_state_t
*cstate
)
7591 * Catch errors reported by us and routines below us, and return NULL
7594 if (setjmp(cstate
->top_ctx
))
7597 regno
= alloc_reg(cstate
);
7598 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7599 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7600 s
->next
= new_stmt(cstate
, BPF_ST
);
7601 s
->next
->s
.k
= regno
;
7608 static struct arth
*
7609 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7615 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7617 reg
= alloc_reg(cstate
);
7619 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7621 s
->next
= new_stmt(cstate
, BPF_ST
);
7630 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7633 * Catch errors reported by us and routines below us, and return NULL
7636 if (setjmp(cstate
->top_ctx
))
7639 return gen_loadi_internal(cstate
, val
);
7643 * The a_arg dance is to avoid annoying whining by compilers that
7644 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7645 * It's not *used* after setjmp returns.
7648 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7650 struct arth
*a
= a_arg
;
7654 * Catch errors reported by us and routines below us, and return NULL
7657 if (setjmp(cstate
->top_ctx
))
7660 s
= xfer_to_a(cstate
, a
);
7662 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7665 s
= new_stmt(cstate
, BPF_ST
);
7673 * The a0_arg dance is to avoid annoying whining by compilers that
7674 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7675 * It's not *used* after setjmp returns.
7678 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7681 struct arth
*a0
= a0_arg
;
7682 struct slist
*s0
, *s1
, *s2
;
7685 * Catch errors reported by us and routines below us, and return NULL
7688 if (setjmp(cstate
->top_ctx
))
7692 * Disallow division by, or modulus by, zero; we do this here
7693 * so that it gets done even if the optimizer is disabled.
7695 * Also disallow shifts by a value greater than 31; we do this
7696 * here, for the same reason.
7698 if (code
== BPF_DIV
) {
7699 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7700 bpf_error(cstate
, "division by zero");
7701 } else if (code
== BPF_MOD
) {
7702 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7703 bpf_error(cstate
, "modulus by zero");
7704 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7705 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7706 bpf_error(cstate
, "shift by more than 31 bits");
7708 s0
= xfer_to_x(cstate
, a1
);
7709 s1
= xfer_to_a(cstate
, a0
);
7710 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7715 sappend(a0
->s
, a1
->s
);
7717 free_reg(cstate
, a0
->regno
);
7718 free_reg(cstate
, a1
->regno
);
7720 s0
= new_stmt(cstate
, BPF_ST
);
7721 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7728 * Initialize the table of used registers and the current register.
7731 init_regs(compiler_state_t
*cstate
)
7734 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7738 * Return the next free register.
7741 alloc_reg(compiler_state_t
*cstate
)
7743 int n
= BPF_MEMWORDS
;
7746 if (cstate
->regused
[cstate
->curreg
])
7747 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7749 cstate
->regused
[cstate
->curreg
] = 1;
7750 return cstate
->curreg
;
7753 bpf_error(cstate
, "too many registers needed to evaluate expression");
7758 * Return a register to the table so it can
7762 free_reg(compiler_state_t
*cstate
, int n
)
7764 cstate
->regused
[n
] = 0;
7767 static struct block
*
7768 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7772 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7773 return gen_jmp(cstate
, jmp
, n
, s
);
7777 gen_greater(compiler_state_t
*cstate
, int n
)
7780 * Catch errors reported by us and routines below us, and return NULL
7783 if (setjmp(cstate
->top_ctx
))
7786 return gen_len(cstate
, BPF_JGE
, n
);
7790 * Actually, this is less than or equal.
7793 gen_less(compiler_state_t
*cstate
, int n
)
7798 * Catch errors reported by us and routines below us, and return NULL
7801 if (setjmp(cstate
->top_ctx
))
7804 b
= gen_len(cstate
, BPF_JGT
, n
);
7811 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7812 * the beginning of the link-layer header.
7813 * XXX - that means you can't test values in the radiotap header, but
7814 * as that header is difficult if not impossible to parse generally
7815 * without a loop, that might not be a severe problem. A new keyword
7816 * "radio" could be added for that, although what you'd really want
7817 * would be a way of testing particular radio header values, which
7818 * would generate code appropriate to the radio header in question.
7821 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7827 * Catch errors reported by us and routines below us, and return NULL
7830 if (setjmp(cstate
->top_ctx
))
7833 assert_maxval(cstate
, "byte argument", val
, UINT8_MAX
);
7840 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7843 return gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7846 return gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7849 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7853 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7857 // Load the required byte first.
7858 struct slist
*s0
= gen_load_a(cstate
, OR_LINKHDR
, idx
, BPF_B
);
7860 b
= gen_jmp(cstate
, BPF_JEQ
, 0, s0
);
7867 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7869 bpf_u_int32 hostmask
;
7870 struct block
*b0
, *b1
, *b2
;
7871 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7874 * Catch errors reported by us and routines below us, and return NULL
7877 if (setjmp(cstate
->top_ctx
))
7884 switch (cstate
->linktype
) {
7886 case DLT_ARCNET_LINUX
:
7887 // ARCnet broadcast is [8-bit] destination address 0.
7888 return gen_ahostop(cstate
, 0, Q_DST
);
7890 return gen_mac48host(cstate
, ebroadcast
, Q_DST
, "broadcast");
7895 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7896 * as an indication that we don't know the netmask, and fail
7899 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7900 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7901 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7902 hostmask
= ~cstate
->netmask
;
7903 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
7904 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
7909 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
7914 * Generate code to test the low-order bit of a MAC address (that's
7915 * the bottom bit of the *first* byte).
7917 static struct block
*
7918 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7920 register struct slist
*s
;
7922 /* link[offset] & 1 != 0 */
7923 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7924 return gen_set(cstate
, 1, s
);
7928 gen_multicast(compiler_state_t
*cstate
, int proto
)
7930 register struct block
*b0
, *b1
, *b2
;
7931 register struct slist
*s
;
7934 * Catch errors reported by us and routines below us, and return NULL
7937 if (setjmp(cstate
->top_ctx
))
7944 switch (cstate
->linktype
) {
7946 case DLT_ARCNET_LINUX
:
7947 // ARCnet multicast is the same as broadcast.
7948 return gen_ahostop(cstate
, 0, Q_DST
);
7950 case DLT_NETANALYZER
:
7951 case DLT_NETANALYZER_TRANSPARENT
:
7952 b1
= gen_prevlinkhdr_check(cstate
);
7953 /* ether[0] & 1 != 0 */
7954 b0
= gen_mac_multicast(cstate
, 0);
7960 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7962 * XXX - was that referring to bit-order issues?
7964 /* fddi[1] & 1 != 0 */
7965 return gen_mac_multicast(cstate
, 1);
7967 /* tr[2] & 1 != 0 */
7968 return gen_mac_multicast(cstate
, 2);
7969 case DLT_IEEE802_11
:
7970 case DLT_PRISM_HEADER
:
7971 case DLT_IEEE802_11_RADIO_AVS
:
7972 case DLT_IEEE802_11_RADIO
:
7977 * For control frames, there is no DA.
7979 * For management frames, DA is at an
7980 * offset of 4 from the beginning of
7983 * For data frames, DA is at an offset
7984 * of 4 from the beginning of the packet
7985 * if To DS is clear and at an offset of
7986 * 16 from the beginning of the packet
7991 * Generate the tests to be done for data frames.
7993 * First, check for To DS set, i.e. "link[1] & 0x01".
7995 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7996 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
7999 * If To DS is set, the DA is at 16.
8001 b0
= gen_mac_multicast(cstate
, 16);
8005 * Now, check for To DS not set, i.e. check
8006 * "!(link[1] & 0x01)".
8008 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8009 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8012 * If To DS is not set, the DA is at 4.
8014 b1
= gen_mac_multicast(cstate
, 4);
8018 * Now OR together the last two checks. That gives
8019 * the complete set of checks for data frames.
8024 * Now check for a data frame.
8025 * I.e, check "link[0] & 0x08".
8027 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8028 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8031 * AND that with the checks done for data frames.
8036 * If the high-order bit of the type value is 0, this
8037 * is a management frame.
8038 * I.e, check "!(link[0] & 0x08)".
8040 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8041 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8044 * For management frames, the DA is at 4.
8046 b1
= gen_mac_multicast(cstate
, 4);
8050 * OR that with the checks done for data frames.
8051 * That gives the checks done for management and
8057 * If the low-order bit of the type value is 1,
8058 * this is either a control frame or a frame
8059 * with a reserved type, and thus not a
8062 * I.e., check "!(link[0] & 0x04)".
8064 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8065 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
8068 * AND that with the checks for data and management
8073 case DLT_IP_OVER_FC
:
8074 return gen_mac_multicast(cstate
, 2);
8078 fail_kw_on_dlt(cstate
, "multicast");
8082 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8083 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8088 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8089 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8093 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8099 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8100 * we can look at special meta-data in the filter expression; otherwise we
8101 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8102 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8103 * pcap_activate() conditionally sets.
8106 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8108 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8110 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8112 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8117 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8120 * Catch errors reported by us and routines below us, and return NULL
8123 if (setjmp(cstate
->top_ctx
))
8127 * Only some data link types support ifindex qualifiers.
8129 switch (cstate
->linktype
) {
8130 case DLT_LINUX_SLL2
:
8131 /* match packets on this interface */
8132 return gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8134 #if defined(__linux__)
8135 require_basic_bpf_extensions(cstate
, "ifindex");
8137 return gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8139 #else /* defined(__linux__) */
8140 fail_kw_on_dlt(cstate
, "ifindex");
8142 #endif /* defined(__linux__) */
8147 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8148 * Outbound traffic is sent by this machine, while inbound traffic is
8149 * sent by a remote machine (and may include packets destined for a
8150 * unicast or multicast link-layer address we are not subscribing to).
8151 * These are the same definitions implemented by pcap_setdirection().
8152 * Capturing only unicast traffic destined for this host is probably
8153 * better accomplished using a higher-layer filter.
8156 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8158 register struct block
*b0
;
8161 * Catch errors reported by us and routines below us, and return NULL
8164 if (setjmp(cstate
->top_ctx
))
8168 * Only some data link types support inbound/outbound qualifiers.
8170 switch (cstate
->linktype
) {
8172 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8173 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8176 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8177 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8180 /* match outgoing packets */
8181 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8183 /* to filter on inbound traffic, invert the match */
8188 case DLT_LINUX_SLL2
:
8189 /* match outgoing packets */
8190 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8192 /* to filter on inbound traffic, invert the match */
8198 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8199 outbound
? PF_OUT
: PF_IN
);
8202 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8204 case DLT_JUNIPER_MFR
:
8205 case DLT_JUNIPER_MLFR
:
8206 case DLT_JUNIPER_MLPPP
:
8207 case DLT_JUNIPER_ATM1
:
8208 case DLT_JUNIPER_ATM2
:
8209 case DLT_JUNIPER_PPPOE
:
8210 case DLT_JUNIPER_PPPOE_ATM
:
8211 case DLT_JUNIPER_GGSN
:
8212 case DLT_JUNIPER_ES
:
8213 case DLT_JUNIPER_MONITOR
:
8214 case DLT_JUNIPER_SERVICES
:
8215 case DLT_JUNIPER_ETHER
:
8216 case DLT_JUNIPER_PPP
:
8217 case DLT_JUNIPER_FRELAY
:
8218 case DLT_JUNIPER_CHDLC
:
8219 case DLT_JUNIPER_VP
:
8220 case DLT_JUNIPER_ST
:
8221 case DLT_JUNIPER_ISM
:
8222 case DLT_JUNIPER_VS
:
8223 case DLT_JUNIPER_SRX_E2E
:
8224 case DLT_JUNIPER_FIBRECHANNEL
:
8225 case DLT_JUNIPER_ATM_CEMIC
:
8226 /* juniper flags (including direction) are stored
8227 * the byte after the 3-byte magic number */
8228 return gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8232 * If we have packet meta-data indicating a direction,
8233 * and that metadata can be checked by BPF code, check
8234 * it. Otherwise, give up, as this link-layer type has
8235 * nothing in the packet data.
8237 * Currently, the only platform where a BPF filter can
8238 * check that metadata is Linux with the in-kernel
8239 * BPF interpreter. If other packet capture mechanisms
8240 * and BPF filters also supported this, it would be
8241 * nice. It would be even better if they made that
8242 * metadata available so that we could provide it
8243 * with newer capture APIs, allowing it to be saved
8246 #if defined(__linux__)
8247 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8248 /* match outgoing packets */
8249 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8252 /* to filter on inbound traffic, invert the match */
8256 #else /* defined(__linux__) */
8257 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8259 #endif /* defined(__linux__) */
8263 /* PF firewall log matched interface */
8265 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8270 * Catch errors reported by us and routines below us, and return NULL
8273 if (setjmp(cstate
->top_ctx
))
8276 assert_pflog(cstate
, "ifname");
8278 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8279 off
= offsetof(struct pfloghdr
, ifname
);
8280 if (strlen(ifname
) >= len
) {
8281 bpf_error(cstate
, "ifname interface names can only be %d characters",
8285 return gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8286 (const u_char
*)ifname
);
8289 /* PF firewall log ruleset name */
8291 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8294 * Catch errors reported by us and routines below us, and return NULL
8297 if (setjmp(cstate
->top_ctx
))
8300 assert_pflog(cstate
, "ruleset");
8302 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8303 bpf_error(cstate
, "ruleset names can only be %ld characters",
8304 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8308 return gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8309 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8312 /* PF firewall log rule number */
8314 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8317 * Catch errors reported by us and routines below us, and return NULL
8320 if (setjmp(cstate
->top_ctx
))
8323 assert_pflog(cstate
, "rnr");
8325 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8329 /* PF firewall log sub-rule number */
8331 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8334 * Catch errors reported by us and routines below us, and return NULL
8337 if (setjmp(cstate
->top_ctx
))
8340 assert_pflog(cstate
, "srnr");
8342 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8346 /* PF firewall log reason code */
8348 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8351 * Catch errors reported by us and routines below us, and return NULL
8354 if (setjmp(cstate
->top_ctx
))
8357 assert_pflog(cstate
, "reason");
8359 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8360 (bpf_u_int32
)reason
);
8363 /* PF firewall log action */
8365 gen_pf_action(compiler_state_t
*cstate
, int action
)
8368 * Catch errors reported by us and routines below us, and return NULL
8371 if (setjmp(cstate
->top_ctx
))
8374 assert_pflog(cstate
, "action");
8376 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8377 (bpf_u_int32
)action
);
8380 /* IEEE 802.11 wireless header */
8382 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8385 * Catch errors reported by us and routines below us, and return NULL
8388 if (setjmp(cstate
->top_ctx
))
8391 switch (cstate
->linktype
) {
8393 case DLT_IEEE802_11
:
8394 case DLT_PRISM_HEADER
:
8395 case DLT_IEEE802_11_RADIO_AVS
:
8396 case DLT_IEEE802_11_RADIO
:
8398 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8401 fail_kw_on_dlt(cstate
, "type/subtype");
8407 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8410 * Catch errors reported by us and routines below us, and return NULL
8413 if (setjmp(cstate
->top_ctx
))
8416 switch (cstate
->linktype
) {
8418 case DLT_IEEE802_11
:
8419 case DLT_PRISM_HEADER
:
8420 case DLT_IEEE802_11_RADIO_AVS
:
8421 case DLT_IEEE802_11_RADIO
:
8423 return gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8424 IEEE80211_FC1_DIR_MASK
);
8427 fail_kw_on_dlt(cstate
, "dir");
8432 // Process an ARCnet host address string.
8434 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8437 * Catch errors reported by us and routines below us, and return NULL
8440 if (setjmp(cstate
->top_ctx
))
8443 switch (cstate
->linktype
) {
8446 case DLT_ARCNET_LINUX
:
8447 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8448 q
.proto
== Q_LINK
) {
8451 * The lexer currently defines the address format in a
8452 * way that makes this error condition never true.
8453 * Let's check it anyway in case this part of the lexer
8454 * changes in future.
8456 if (! pcapint_atoan(s
, &addr
))
8457 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8458 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8460 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8464 bpf_error(cstate
, "aid supported only on ARCnet");
8469 // Compare an ARCnet host address with the given value.
8470 static struct block
*
8471 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8473 register struct block
*b0
, *b1
;
8477 * ARCnet is different from Ethernet: the source address comes before
8478 * the destination address, each is one byte long. This holds for all
8479 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8480 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8481 * by Datapoint (document number 61610-01).
8484 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8487 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8490 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8491 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8497 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8498 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8508 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8515 static struct block
*
8516 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8518 struct block
*b0
, *b1
;
8520 /* check for VLAN, including 802.1ad and QinQ */
8521 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8522 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8525 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8531 static struct block
*
8532 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8534 assert_maxval(cstate
, "VLAN tag", vlan_num
, 0x0fff);
8535 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8538 static struct block
*
8539 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8542 struct block
*b0
, *b1
;
8544 b0
= gen_vlan_tpid_test(cstate
);
8547 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8553 * Both payload and link header type follow the VLAN tags so that
8554 * both need to be updated.
8556 cstate
->off_linkpl
.constant_part
+= 4;
8557 cstate
->off_linktype
.constant_part
+= 4;
8562 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8563 /* add v to variable part of off */
8565 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8566 bpf_u_int32 v
, struct slist
*s
)
8570 if (!off
->is_variable
)
8571 off
->is_variable
= 1;
8573 off
->reg
= alloc_reg(cstate
);
8575 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8578 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8581 s2
= new_stmt(cstate
, BPF_ST
);
8587 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8588 * and link type offsets first
8591 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8595 /* offset determined at run time, shift variable part */
8597 cstate
->is_vlan_vloffset
= 1;
8598 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8599 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8601 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8602 sappend(s
.next
, b_tpid
->head
->stmts
);
8603 b_tpid
->head
->stmts
= s
.next
;
8607 * patch block b_vid (VLAN id test) to load VID value either from packet
8608 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8611 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8613 struct slist
*s
, *s2
, *sjeq
;
8616 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8617 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8619 /* true -> next instructions, false -> beginning of b_vid */
8620 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8622 sjeq
->s
.jf
= b_vid
->stmts
;
8625 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8626 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8630 /* Jump to the test in b_vid. We need to jump one instruction before
8631 * the end of the b_vid block so that we only skip loading the TCI
8632 * from packet data and not the 'and' instruction extracting VID.
8635 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8637 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8641 /* insert our statements at the beginning of b_vid */
8642 sappend(s
, b_vid
->stmts
);
8647 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8648 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8649 * tag can be either in metadata or in packet data; therefore if the
8650 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8651 * header for VLAN tag. As the decision is done at run time, we need
8652 * update variable part of the offsets
8654 static struct block
*
8655 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8658 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8661 /* generate new filter code based on extracting packet
8663 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8664 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8666 b0
= gen_jmp(cstate
, BPF_JEQ
, 1, s
);
8669 * This is tricky. We need to insert the statements updating variable
8670 * parts of offsets before the traditional TPID and VID tests so
8671 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8672 * we do not want this update to affect those checks. That's why we
8673 * generate both test blocks first and insert the statements updating
8674 * variable parts of both offsets after that. This wouldn't work if
8675 * there already were variable length link header when entering this
8676 * function but gen_vlan_bpf_extensions() isn't called in that case.
8678 b_tpid
= gen_vlan_tpid_test(cstate
);
8680 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8682 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8687 gen_vlan_patch_vid_test(cstate
, b_vid
);
8697 * support IEEE 802.1Q VLAN trunk over ethernet
8700 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8705 * Catch errors reported by us and routines below us, and return NULL
8708 if (setjmp(cstate
->top_ctx
))
8711 /* can't check for VLAN-encapsulated packets inside MPLS */
8712 if (cstate
->label_stack_depth
> 0)
8713 bpf_error(cstate
, "no VLAN match after MPLS");
8716 * Check for a VLAN packet, and then change the offsets to point
8717 * to the type and data fields within the VLAN packet. Just
8718 * increment the offsets, so that we can support a hierarchy, e.g.
8719 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
8722 * XXX - this is a bit of a kludge. If we were to split the
8723 * compiler into a parser that parses an expression and
8724 * generates an expression tree, and a code generator that
8725 * takes an expression tree (which could come from our
8726 * parser or from some other parser) and generates BPF code,
8727 * we could perhaps make the offsets parameters of routines
8728 * and, in the handler for an "AND" node, pass to subnodes
8729 * other than the VLAN node the adjusted offsets.
8731 * This would mean that "vlan" would, instead of changing the
8732 * behavior of *all* tests after it, change only the behavior
8733 * of tests ANDed with it. That would change the documented
8734 * semantics of "vlan", which might break some expressions.
8735 * However, it would mean that "(vlan and ip) or ip" would check
8736 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8737 * checking only for VLAN-encapsulated IP, so that could still
8738 * be considered worth doing; it wouldn't break expressions
8739 * that are of the form "vlan and ..." or "vlan N and ...",
8740 * which I suspect are the most common expressions involving
8741 * "vlan". "vlan or ..." doesn't necessarily do what the user
8742 * would really want, now, as all the "or ..." tests would
8743 * be done assuming a VLAN, even though the "or" could be viewed
8744 * as meaning "or, if this isn't a VLAN packet...".
8746 switch (cstate
->linktype
) {
8750 * Newer version of the Linux kernel pass around
8751 * packets in which the VLAN tag has been removed
8752 * from the packet data and put into metadata.
8754 * This requires special treatment.
8756 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8757 /* Verify that this is the outer part of the packet and
8758 * not encapsulated somehow. */
8759 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8760 cstate
->off_linkhdr
.constant_part
==
8761 cstate
->off_outermostlinkhdr
.constant_part
) {
8763 * Do we need special VLAN handling?
8765 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8766 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8769 b0
= gen_vlan_no_bpf_extensions(cstate
,
8770 vlan_num
, has_vlan_tag
);
8773 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8777 case DLT_NETANALYZER
:
8778 case DLT_NETANALYZER_TRANSPARENT
:
8779 case DLT_IEEE802_11
:
8780 case DLT_PRISM_HEADER
:
8781 case DLT_IEEE802_11_RADIO_AVS
:
8782 case DLT_IEEE802_11_RADIO
:
8784 * These are either Ethernet packets with an additional
8785 * metadata header (the NetAnalyzer types), or 802.11
8786 * packets, possibly with an additional metadata header.
8788 * For the first of those, the VLAN tag is in the normal
8789 * place, so the special-case handling above isn't
8792 * For the second of those, we don't do the special-case
8795 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8799 bpf_error(cstate
, "no VLAN support for %s",
8800 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8804 cstate
->vlan_stack_depth
++;
8812 * The label_num_arg dance is to avoid annoying whining by compilers that
8813 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8814 * It's not *used* after setjmp returns.
8816 static struct block
*
8817 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
8820 struct block
*b0
, *b1
;
8822 if (cstate
->label_stack_depth
> 0) {
8823 /* just match the bottom-of-stack bit clear */
8824 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8827 * We're not in an MPLS stack yet, so check the link-layer
8828 * type against MPLS.
8830 switch (cstate
->linktype
) {
8832 case DLT_C_HDLC
: /* fall through */
8835 case DLT_NETANALYZER
:
8836 case DLT_NETANALYZER_TRANSPARENT
:
8837 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8841 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8844 /* FIXME add other DLT_s ...
8845 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8846 * leave it for now */
8849 bpf_error(cstate
, "no MPLS support for %s",
8850 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8855 /* If a specific MPLS label is requested, check it */
8856 if (has_label_num
) {
8857 assert_maxval(cstate
, "MPLS label", label_num
, 0xFFFFF);
8858 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8859 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
8860 0xfffff000); /* only compare the first 20 bits */
8866 * Change the offsets to point to the type and data fields within
8867 * the MPLS packet. Just increment the offsets, so that we
8868 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8869 * capture packets with an outer label of 100000 and an inner
8872 * Increment the MPLS stack depth as well; this indicates that
8873 * we're checking MPLS-encapsulated headers, to make sure higher
8874 * level code generators don't try to match against IP-related
8875 * protocols such as Q_ARP, Q_RARP etc.
8877 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8879 cstate
->off_nl_nosnap
+= 4;
8880 cstate
->off_nl
+= 4;
8881 cstate
->label_stack_depth
++;
8886 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
8889 * Catch errors reported by us and routines below us, and return NULL
8892 if (setjmp(cstate
->top_ctx
))
8895 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
8899 * Support PPPOE discovery and session.
8902 gen_pppoed(compiler_state_t
*cstate
)
8905 * Catch errors reported by us and routines below us, and return NULL
8908 if (setjmp(cstate
->top_ctx
))
8911 /* check for PPPoE discovery */
8912 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
8916 * RFC 2516 Section 4:
8918 * The Ethernet payload for PPPoE is as follows:
8921 * 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
8922 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8923 * | VER | TYPE | CODE | SESSION_ID |
8924 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8925 * | LENGTH | payload ~
8926 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8929 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
8931 struct block
*b0
, *b1
;
8934 * Catch errors reported by us and routines below us, and return NULL
8937 if (setjmp(cstate
->top_ctx
))
8941 * Test against the PPPoE session link-layer type.
8943 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
8945 /* If a specific session is requested, check PPPoE session id */
8947 assert_maxval(cstate
, "PPPoE session number", sess_num
, UINT16_MAX
);
8948 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
8954 * Change the offsets to point to the type and data fields within
8955 * the PPP packet, and note that this is PPPoE rather than
8958 * XXX - this is a bit of a kludge. See the comments in
8961 * The "network-layer" protocol is PPPoE, which has a 6-byte
8962 * PPPoE header, followed by a PPP packet.
8964 * There is no HDLC encapsulation for the PPP packet (it's
8965 * encapsulated in PPPoES instead), so the link-layer type
8966 * starts at the first byte of the PPP packet. For PPPoE,
8967 * that offset is relative to the beginning of the total
8968 * link-layer payload, including any 802.2 LLC header, so
8969 * it's 6 bytes past cstate->off_nl.
8971 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8972 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8973 cstate
->off_linkpl
.reg
);
8975 cstate
->off_linktype
= cstate
->off_linkhdr
;
8976 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8979 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8984 /* Check that this is Geneve and the VNI is correct if
8985 * specified. Parameterized to handle both IPv4 and IPv6. */
8986 static struct block
*
8987 gen_geneve_check(compiler_state_t
*cstate
,
8988 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
8989 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
8991 struct block
*b0
, *b1
;
8993 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8995 /* Check that we are operating on version 0. Otherwise, we
8996 * can't decode the rest of the fields. The version is 2 bits
8997 * in the first byte of the Geneve header. */
8998 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9003 assert_maxval(cstate
, "Geneve VNI", vni
, 0xffffff);
9004 vni
<<= 8; /* VNI is in the upper 3 bytes */
9005 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9013 /* The IPv4 and IPv6 Geneve checks need to do two things:
9014 * - Verify that this actually is Geneve with the right VNI.
9015 * - Place the IP header length (plus variable link prefix if
9016 * needed) into register A to be used later to compute
9017 * the inner packet offsets. */
9018 static struct block
*
9019 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9021 struct block
*b0
, *b1
;
9022 struct slist
*s
, *s1
;
9024 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9026 /* Load the IP header length into A. */
9027 s
= gen_loadx_iphdrlen(cstate
);
9029 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9032 /* Forcibly append these statements to the true condition
9033 * of the protocol check by creating a new block that is
9034 * always true and ANDing them. */
9035 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9042 static struct block
*
9043 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9045 struct block
*b0
, *b1
;
9046 struct slist
*s
, *s1
;
9048 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9050 /* Load the IP header length. We need to account for a
9051 * variable length link prefix if there is one. */
9052 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9054 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9058 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9062 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9066 /* Forcibly append these statements to the true condition
9067 * of the protocol check by creating a new block that is
9068 * always true and ANDing them. */
9069 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9072 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9079 /* We need to store three values based on the Geneve header::
9080 * - The offset of the linktype.
9081 * - The offset of the end of the Geneve header.
9082 * - The offset of the end of the encapsulated MAC header. */
9083 static struct slist
*
9084 gen_geneve_offsets(compiler_state_t
*cstate
)
9086 struct slist
*s
, *s1
, *s_proto
;
9088 /* First we need to calculate the offset of the Geneve header
9089 * itself. This is composed of the IP header previously calculated
9090 * (include any variable link prefix) and stored in A plus the
9091 * fixed sized headers (fixed link prefix, MAC length, and UDP
9093 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9094 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9096 /* Stash this in X since we'll need it later. */
9097 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9100 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9102 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9106 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9107 cstate
->off_linktype
.is_variable
= 1;
9108 cstate
->off_linktype
.constant_part
= 0;
9110 s1
= new_stmt(cstate
, BPF_ST
);
9111 s1
->s
.k
= cstate
->off_linktype
.reg
;
9114 /* Load the Geneve option length and mask and shift to get the
9115 * number of bytes. It is stored in the first byte of the Geneve
9117 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9121 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9125 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9129 /* Add in the rest of the Geneve base header. */
9130 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9134 /* Add the Geneve header length to its offset and store. */
9135 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9139 /* Set the encapsulated type as Ethernet. Even though we may
9140 * not actually have Ethernet inside there are two reasons this
9142 * - The linktype field is always in EtherType format regardless
9143 * of whether it is in Geneve or an inner Ethernet frame.
9144 * - The only link layer that we have specific support for is
9145 * Ethernet. We will confirm that the packet actually is
9146 * Ethernet at runtime before executing these checks. */
9147 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9149 s1
= new_stmt(cstate
, BPF_ST
);
9150 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9153 /* Calculate whether we have an Ethernet header or just raw IP/
9154 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9155 * and linktype by 14 bytes so that the network header can be found
9156 * seamlessly. Otherwise, keep what we've calculated already. */
9158 /* We have a bare jmp so we can't use the optimizer. */
9159 cstate
->no_optimize
= 1;
9161 /* Load the EtherType in the Geneve header, 2 bytes in. */
9162 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9166 /* Load X with the end of the Geneve header. */
9167 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9168 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9171 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9172 * end of this check, we should have the total length in X. In
9173 * the non-Ethernet case, it's already there. */
9174 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9175 s_proto
->s
.k
= ETHERTYPE_TEB
;
9176 sappend(s
, s_proto
);
9178 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9182 /* Since this is Ethernet, use the EtherType of the payload
9183 * directly as the linktype. Overwrite what we already have. */
9184 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9188 s1
= new_stmt(cstate
, BPF_ST
);
9189 s1
->s
.k
= cstate
->off_linktype
.reg
;
9192 /* Advance two bytes further to get the end of the Ethernet
9194 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9198 /* Move the result to X. */
9199 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9202 /* Store the final result of our linkpl calculation. */
9203 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9204 cstate
->off_linkpl
.is_variable
= 1;
9205 cstate
->off_linkpl
.constant_part
= 0;
9207 s1
= new_stmt(cstate
, BPF_STX
);
9208 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9217 /* Check to see if this is a Geneve packet. */
9219 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9221 struct block
*b0
, *b1
;
9225 * Catch errors reported by us and routines below us, and return NULL
9228 if (setjmp(cstate
->top_ctx
))
9231 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9232 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9237 /* Later filters should act on the payload of the Geneve frame,
9238 * update all of the header pointers. Attach this code so that
9239 * it gets executed in the event that the Geneve filter matches. */
9240 s
= gen_geneve_offsets(cstate
);
9242 b1
= gen_true(cstate
);
9243 sappend(s
, b1
->stmts
);
9248 cstate
->is_encap
= 1;
9253 /* Check that this is VXLAN and the VNI is correct if
9254 * specified. Parameterized to handle both IPv4 and IPv6. */
9255 static struct block
*
9256 gen_vxlan_check(compiler_state_t
*cstate
,
9257 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
9258 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9260 struct block
*b0
, *b1
;
9262 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9264 /* Check that the VXLAN header has the flag bits set
9266 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9271 assert_maxval(cstate
, "VXLAN VNI", vni
, 0xffffff);
9272 vni
<<= 8; /* VNI is in the upper 3 bytes */
9273 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9281 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9282 * - Verify that this actually is VXLAN with the right VNI.
9283 * - Place the IP header length (plus variable link prefix if
9284 * needed) into register A to be used later to compute
9285 * the inner packet offsets. */
9286 static struct block
*
9287 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9289 struct block
*b0
, *b1
;
9290 struct slist
*s
, *s1
;
9292 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9294 /* Load the IP header length into A. */
9295 s
= gen_loadx_iphdrlen(cstate
);
9297 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9300 /* Forcibly append these statements to the true condition
9301 * of the protocol check by creating a new block that is
9302 * always true and ANDing them. */
9303 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9310 static struct block
*
9311 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9313 struct block
*b0
, *b1
;
9314 struct slist
*s
, *s1
;
9316 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9318 /* Load the IP header length. We need to account for a
9319 * variable length link prefix if there is one. */
9320 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9322 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9326 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9330 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9334 /* Forcibly append these statements to the true condition
9335 * of the protocol check by creating a new block that is
9336 * always true and ANDing them. */
9337 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9340 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9347 /* We need to store three values based on the VXLAN header:
9348 * - The offset of the linktype.
9349 * - The offset of the end of the VXLAN header.
9350 * - The offset of the end of the encapsulated MAC header. */
9351 static struct slist
*
9352 gen_vxlan_offsets(compiler_state_t
*cstate
)
9354 struct slist
*s
, *s1
;
9356 /* Calculate the offset of the VXLAN header itself. This
9357 * includes the IP header computed previously (including any
9358 * variable link prefix) and stored in A plus the fixed size
9359 * headers (fixed link prefix, MAC length, UDP header). */
9360 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9361 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9363 /* Add the VXLAN header length to its offset and store */
9364 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9368 /* Push the link header. VXLAN packets always contain Ethernet
9370 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9372 s1
= new_stmt(cstate
, BPF_ST
);
9373 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9376 /* As the payload is an Ethernet packet, we can use the
9377 * EtherType of the payload directly as the linktype. */
9378 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9382 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9383 cstate
->off_linktype
.is_variable
= 1;
9384 cstate
->off_linktype
.constant_part
= 0;
9386 s1
= new_stmt(cstate
, BPF_ST
);
9387 s1
->s
.k
= cstate
->off_linktype
.reg
;
9390 /* Two bytes further is the end of the Ethernet header and the
9391 * start of the payload. */
9392 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9396 /* Move the result to X. */
9397 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9400 /* Store the final result of our linkpl calculation. */
9401 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9402 cstate
->off_linkpl
.is_variable
= 1;
9403 cstate
->off_linkpl
.constant_part
= 0;
9405 s1
= new_stmt(cstate
, BPF_STX
);
9406 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9414 /* Check to see if this is a VXLAN packet. */
9416 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9418 struct block
*b0
, *b1
;
9422 * Catch errors reported by us and routines below us, and return NULL
9425 if (setjmp(cstate
->top_ctx
))
9428 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9429 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9434 /* Later filters should act on the payload of the VXLAN frame,
9435 * update all of the header pointers. Attach this code so that
9436 * it gets executed in the event that the VXLAN filter matches. */
9437 s
= gen_vxlan_offsets(cstate
);
9439 b1
= gen_true(cstate
);
9440 sappend(s
, b1
->stmts
);
9445 cstate
->is_encap
= 1;
9450 /* Check that the encapsulated frame has a link layer header
9451 * for Ethernet filters. */
9452 static struct block
*
9453 gen_encap_ll_check(compiler_state_t
*cstate
)
9456 struct slist
*s
, *s1
;
9458 /* The easiest way to see if there is a link layer present
9459 * is to check if the link layer header and payload are not
9462 /* Geneve always generates pure variable offsets so we can
9463 * compare only the registers. */
9464 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9465 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9467 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9468 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9471 b0
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9477 static struct block
*
9478 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9479 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9481 assert_atm(cstate
, atmkw(atmfield
));
9486 assert_maxval(cstate
, "VPI", jvalue
, UINT8_MAX
);
9487 return gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9488 0xffffffffU
, jtype
, reverse
, jvalue
);
9491 assert_maxval(cstate
, "VCI", jvalue
, UINT16_MAX
);
9492 return gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9493 0xffffffffU
, jtype
, reverse
, jvalue
);
9500 static struct block
*
9501 gen_atm_vpi(compiler_state_t
*cstate
, const uint8_t v
)
9503 return gen_atmfield_code_internal(cstate
, A_VPI
, v
, BPF_JEQ
, 0);
9506 static struct block
*
9507 gen_atm_vci(compiler_state_t
*cstate
, const uint16_t v
)
9509 return gen_atmfield_code_internal(cstate
, A_VCI
, v
, BPF_JEQ
, 0);
9512 static struct block
*
9513 gen_atm_prototype(compiler_state_t
*cstate
, const uint8_t v
)
9515 return gen_mcmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, v
, 0x0fU
);
9518 static struct block
*
9519 gen_atmtype_llc(compiler_state_t
*cstate
)
9523 b0
= gen_atm_prototype(cstate
, PT_LLC
);
9524 cstate
->linktype
= cstate
->prevlinktype
;
9529 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9530 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9533 * Catch errors reported by us and routines below us, and return NULL
9536 if (setjmp(cstate
->top_ctx
))
9539 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9544 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9546 struct block
*b0
, *b1
;
9549 * Catch errors reported by us and routines below us, and return NULL
9552 if (setjmp(cstate
->top_ctx
))
9555 assert_atm(cstate
, atmkw(type
));
9560 /* Get all packets in Meta signalling Circuit */
9561 b0
= gen_atm_vpi(cstate
, 0);
9562 b1
= gen_atm_vci(cstate
, 1);
9567 /* Get all packets in Broadcast Circuit*/
9568 b0
= gen_atm_vpi(cstate
, 0);
9569 b1
= gen_atm_vci(cstate
, 2);
9574 /* Get all cells in Segment OAM F4 circuit*/
9575 b0
= gen_atm_vpi(cstate
, 0);
9576 b1
= gen_atm_vci(cstate
, 3);
9581 /* Get all cells in End-to-End OAM F4 Circuit*/
9582 b0
= gen_atm_vpi(cstate
, 0);
9583 b1
= gen_atm_vci(cstate
, 4);
9588 /* Get all packets in connection Signalling Circuit */
9589 b0
= gen_atm_vpi(cstate
, 0);
9590 b1
= gen_atm_vci(cstate
, 5);
9595 /* Get all packets in ILMI Circuit */
9596 b0
= gen_atm_vpi(cstate
, 0);
9597 b1
= gen_atm_vci(cstate
, 16);
9602 /* Get all LANE packets */
9603 b1
= gen_atm_prototype(cstate
, PT_LANE
);
9606 * Arrange that all subsequent tests assume LANE
9607 * rather than LLC-encapsulated packets, and set
9608 * the offsets appropriately for LANE-encapsulated
9611 * We assume LANE means Ethernet, not Token Ring.
9613 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9614 cstate
->off_payload
+ 2, /* Ethernet header */
9616 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9617 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9618 cstate
->off_nl
= 0; /* Ethernet II */
9619 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9628 * Filtering for MTP2 messages based on li value
9629 * FISU, length is null
9630 * LSSU, length is 1 or 2
9631 * MSU, length is 3 or more
9632 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9635 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9637 struct block
*b0
, *b1
;
9640 * Catch errors reported by us and routines below us, and return NULL
9643 if (setjmp(cstate
->top_ctx
))
9646 assert_ss7(cstate
, ss7kw(type
));
9651 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9652 0x3fU
, BPF_JEQ
, 0, 0U);
9655 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9656 0x3fU
, BPF_JGT
, 1, 2U);
9657 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9658 0x3fU
, BPF_JGT
, 0, 0U);
9663 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9664 0x3fU
, BPF_JGT
, 0, 2U);
9667 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9668 0xff80U
, BPF_JEQ
, 0, 0U);
9671 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9672 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9673 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9674 0xff80U
, BPF_JGT
, 0, 0U);
9679 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9680 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9688 * These maximum valid values are all-ones, so they double as the bitmasks
9689 * before any bitwise shifting.
9691 #define MTP2_SIO_MAXVAL UINT8_MAX
9692 #define MTP3_PC_MAXVAL 0x3fffU
9693 #define MTP3_SLS_MAXVAL 0xfU
9695 static struct block
*
9696 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
9697 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9704 newoff_sio
= cstate
->off_sio
;
9705 newoff_opc
= cstate
->off_opc
;
9706 newoff_dpc
= cstate
->off_dpc
;
9707 newoff_sls
= cstate
->off_sls
;
9709 assert_ss7(cstate
, ss7kw(mtp3field
));
9711 switch (mtp3field
) {
9714 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
9716 * SIO is the simplest field: the size is one byte and the offset is a
9717 * multiple of bytes, so the only detail to get right is the value of
9718 * the [right-to-left] field offset.
9721 newoff_sio
+= 3; /* offset for MTP2_HSL */
9725 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP2_SIO_MAXVAL
);
9726 // Here the bitmask means "do not apply a bitmask".
9727 return gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
9728 jtype
, reverse
, jvalue
);
9731 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
9733 * SLS, OPC and DPC are more complicated: none of these is sized in a
9734 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
9735 * diagrams are meant to be read right-to-left. This means in the
9736 * diagrams within individual fields and concatenations thereof
9737 * bitwise shifts and masks can be noted in the common left-to-right
9738 * manner until each final value is ready to be byte-swapped and
9739 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
9740 * similar problem in a similar way.
9742 * Offsets of fields within the packet header always have the
9743 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
9744 * DLTs the offset does not include the F (Flag) field at the
9745 * beginning of each message.
9747 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
9748 * 32-bit standard routing header has a 4 byte [RTL] offset and could
9749 * be tested entirely using a single BPF_W comparison. In this case
9750 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
9751 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
9752 * [LTR] bitmask would be (0xF << 28), all of which conveniently
9753 * correlates with the [RTL] packet diagram until the byte-swapping is
9756 * The code below uses this approach for OPC, which spans 3 bytes.
9757 * DPC and SLS use shorter loads, SLS also uses a different offset.
9764 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
9765 return gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
9766 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
9767 SWAPLONG(jvalue
<< 14));
9774 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
9775 return gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
9776 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
9784 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_SLS_MAXVAL
);
9785 return gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
9786 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
9795 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9796 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9799 * Catch errors reported by us and routines below us, and return NULL
9802 if (setjmp(cstate
->top_ctx
))
9805 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
9809 static struct block
*
9810 gen_msg_abbrev(compiler_state_t
*cstate
, const uint8_t type
)
9813 * Q.2931 signalling protocol messages for handling virtual circuits
9814 * establishment and teardown
9816 return gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
,
9821 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9823 struct block
*b0
, *b1
;
9826 * Catch errors reported by us and routines below us, and return NULL
9829 if (setjmp(cstate
->top_ctx
))
9832 assert_atm(cstate
, atmkw(type
));
9838 b0
= gen_atm_vci(cstate
, 3);
9839 b1
= gen_atm_vci(cstate
, 4);
9841 b0
= gen_atm_vpi(cstate
, 0);
9847 b0
= gen_atm_vci(cstate
, 3);
9848 b1
= gen_atm_vci(cstate
, 4);
9850 b0
= gen_atm_vpi(cstate
, 0);
9856 * Get Q.2931 signalling messages for switched
9857 * virtual connection
9859 b0
= gen_msg_abbrev(cstate
, SETUP
);
9860 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
9862 b0
= gen_msg_abbrev(cstate
, CONNECT
);
9864 b0
= gen_msg_abbrev(cstate
, CONNECT_ACK
);
9866 b0
= gen_msg_abbrev(cstate
, RELEASE
);
9868 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
9870 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9875 b0
= gen_msg_abbrev(cstate
, SETUP
);
9876 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
9878 b0
= gen_msg_abbrev(cstate
, CONNECT
);
9880 b0
= gen_msg_abbrev(cstate
, RELEASE
);
9882 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
9884 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);