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 * Another thing that's allocated is the result of pcap_ether_aton();
413 * it must be freed with free(). This variable points to any
414 * address that would need to be freed.
419 * Various code constructs need to know the layout of the packet.
420 * These values give the necessary offsets from the beginning
421 * of the packet data.
425 * Absolute offset of the beginning of the link-layer header.
427 bpf_abs_offset off_linkhdr
;
430 * If we're checking a link-layer header for a packet encapsulated
431 * in another protocol layer, this is the equivalent information
432 * for the previous layers' link-layer header from the beginning
433 * of the raw packet data.
435 bpf_abs_offset off_prevlinkhdr
;
438 * This is the equivalent information for the outermost layers'
441 bpf_abs_offset off_outermostlinkhdr
;
444 * Absolute offset of the beginning of the link-layer payload.
446 bpf_abs_offset off_linkpl
;
449 * "off_linktype" is the offset to information in the link-layer
450 * header giving the packet type. This is an absolute offset
451 * from the beginning of the packet.
453 * For Ethernet, it's the offset of the Ethernet type field; this
454 * means that it must have a value that skips VLAN tags.
456 * For link-layer types that always use 802.2 headers, it's the
457 * offset of the LLC header; this means that it must have a value
458 * that skips VLAN tags.
460 * For PPP, it's the offset of the PPP type field.
462 * For Cisco HDLC, it's the offset of the CHDLC type field.
464 * For BSD loopback, it's the offset of the AF_ value.
466 * For Linux cooked sockets, it's the offset of the type field.
468 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
469 * encapsulation, in which case, IP is assumed.
471 bpf_abs_offset off_linktype
;
474 * TRUE if the link layer includes an ATM pseudo-header.
478 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
479 * causes us to generate code that checks for a Geneve or
480 * VXLAN header respectively and assume that later filters
481 * apply to the encapsulated payload.
486 * TRUE if we need variable length part of VLAN offset
488 int is_vlan_vloffset
;
491 * These are offsets for the ATM pseudo-header.
498 * These are offsets for the MTP2 fields.
504 * These are offsets for the MTP3 fields.
512 * This is the offset of the first byte after the ATM pseudo_header,
513 * or -1 if there is no ATM pseudo-header.
518 * These are offsets to the beginning of the network-layer header.
519 * They are relative to the beginning of the link-layer payload
520 * (i.e., they don't include off_linkhdr.constant_part or
521 * off_linkpl.constant_part).
523 * If the link layer never uses 802.2 LLC:
525 * "off_nl" and "off_nl_nosnap" are the same.
527 * If the link layer always uses 802.2 LLC:
529 * "off_nl" is the offset if there's a SNAP header following
532 * "off_nl_nosnap" is the offset if there's no SNAP header.
534 * If the link layer is Ethernet:
536 * "off_nl" is the offset if the packet is an Ethernet II packet
537 * (we assume no 802.3+802.2+SNAP);
539 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
540 * with an 802.2 header following it.
546 * Here we handle simple allocation of the scratch registers.
547 * If too many registers are alloc'd, the allocator punts.
549 int regused
[BPF_MEMWORDS
];
555 struct chunk chunks
[NCHUNKS
];
560 * For use by routines outside this file.
564 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
569 * If we've already set an error, don't override it.
570 * The lexical analyzer reports some errors by setting
571 * the error and then returning a LEX_ERROR token, which
572 * is not recognized by any grammar rule, and thus forces
573 * the parse to stop. We don't want the error reported
574 * by the lexical analyzer to be overwritten by the syntax
577 if (!cstate
->error_set
) {
579 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
582 cstate
->error_set
= 1;
587 * For use *ONLY* in routines in this file.
589 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
590 PCAP_PRINTFLIKE(2, 3);
593 static void PCAP_NORETURN
594 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
599 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
602 longjmp(cstate
->top_ctx
, 1);
609 static int init_linktype(compiler_state_t
*, pcap_t
*);
611 static void init_regs(compiler_state_t
*);
612 static int alloc_reg(compiler_state_t
*);
613 static void free_reg(compiler_state_t
*, int);
615 static void initchunks(compiler_state_t
*cstate
);
616 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
617 static void *newchunk(compiler_state_t
*cstate
, size_t);
618 static void freechunks(compiler_state_t
*cstate
);
619 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
620 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
621 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
622 static inline void syntax(compiler_state_t
*cstate
);
624 static void backpatch(struct block
*, struct block
*);
625 static void merge(struct block
*, struct block
*);
626 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
628 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
630 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
632 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
634 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
636 static struct block
*gen_cmp_ne(compiler_state_t
*, enum e_offrel
, u_int
,
637 u_int size
, bpf_u_int32
);
638 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
639 u_int
, bpf_u_int32
, bpf_u_int32
);
640 static struct block
*gen_mcmp_ne(compiler_state_t
*, enum e_offrel
, u_int
,
641 u_int
, bpf_u_int32
, bpf_u_int32
);
642 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
643 u_int
, const u_char
*);
644 static struct block
*gen_jmp(compiler_state_t
*, int, bpf_u_int32
,
646 static struct block
*gen_set(compiler_state_t
*, bpf_u_int32
, struct slist
*);
647 static struct block
*gen_unset(compiler_state_t
*, bpf_u_int32
, struct slist
*);
648 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
649 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
650 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
652 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
654 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
655 static struct block
*gen_uncond(compiler_state_t
*, int);
656 static inline struct block
*gen_true(compiler_state_t
*);
657 static inline struct block
*gen_false(compiler_state_t
*);
658 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
659 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
660 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
661 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
662 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
663 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
664 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
665 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
666 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
667 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
669 static uint16_t ethertype_to_ppptype(compiler_state_t
*, bpf_u_int32
);
670 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
671 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
672 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
673 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
675 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
676 struct in6_addr
*, int, u_int
, u_int
);
677 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
678 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
679 static unsigned char is_mac48_linktype(const int);
680 static struct block
*gen_mac48host(compiler_state_t
*, const u_char
*,
681 const u_char
, const char *);
682 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
683 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
684 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
686 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
687 struct in6_addr
*, int, int, int);
688 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
689 struct addrinfo
*, int);
690 static struct block
*gen_ip_proto(compiler_state_t
*, const uint8_t);
691 static struct block
*gen_ip6_proto(compiler_state_t
*, const uint8_t);
692 static struct block
*gen_ipfrag(compiler_state_t
*);
693 static struct block
*gen_portatom(compiler_state_t
*, int, uint16_t);
694 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, uint16_t,
696 static struct block
*gen_portatom6(compiler_state_t
*, int, uint16_t);
697 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, uint16_t,
699 static struct block
*gen_port(compiler_state_t
*, uint16_t, int, int);
700 static struct block
*gen_port_common(compiler_state_t
*, int, struct block
*);
701 static struct block
*gen_portrange(compiler_state_t
*, uint16_t, uint16_t,
703 static struct block
*gen_port6(compiler_state_t
*, uint16_t, int, int);
704 static struct block
*gen_port6_common(compiler_state_t
*, int, struct block
*);
705 static struct block
*gen_portrange6(compiler_state_t
*, uint16_t, uint16_t,
707 static int lookup_proto(compiler_state_t
*, const char *, int);
708 #if !defined(NO_PROTOCHAIN)
709 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
710 #endif /* !defined(NO_PROTOCHAIN) */
711 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int);
712 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
713 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
714 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
715 static struct block
*gen_len(compiler_state_t
*, int, int);
716 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
718 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
719 bpf_u_int32
, int, int);
720 static struct block
*gen_atmtype_llc(compiler_state_t
*);
721 static struct block
*gen_msg_abbrev(compiler_state_t
*, const uint8_t);
722 static struct block
*gen_atm_prototype(compiler_state_t
*, const uint8_t);
723 static struct block
*gen_atm_vpi(compiler_state_t
*, const uint8_t);
724 static struct block
*gen_atm_vci(compiler_state_t
*, const uint16_t);
727 initchunks(compiler_state_t
*cstate
)
731 for (i
= 0; i
< NCHUNKS
; i
++) {
732 cstate
->chunks
[i
].n_left
= 0;
733 cstate
->chunks
[i
].m
= NULL
;
735 cstate
->cur_chunk
= 0;
739 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
745 /* Round up to chunk alignment. */
746 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
748 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
749 if (n
> cp
->n_left
) {
751 k
= ++cstate
->cur_chunk
;
753 bpf_set_error(cstate
, "out of memory");
756 size
= CHUNK0SIZE
<< k
;
757 cp
->m
= (void *)malloc(size
);
759 bpf_set_error(cstate
, "out of memory");
762 memset((char *)cp
->m
, 0, size
);
765 bpf_set_error(cstate
, "out of memory");
770 return (void *)((char *)cp
->m
+ cp
->n_left
);
774 newchunk(compiler_state_t
*cstate
, size_t n
)
778 p
= newchunk_nolongjmp(cstate
, n
);
780 longjmp(cstate
->top_ctx
, 1);
787 freechunks(compiler_state_t
*cstate
)
791 for (i
= 0; i
< NCHUNKS
; ++i
)
792 if (cstate
->chunks
[i
].m
!= NULL
)
793 free(cstate
->chunks
[i
].m
);
797 * A strdup whose allocations are freed after code generation is over.
798 * This is used by the lexical analyzer, so it can't longjmp; it just
799 * returns NULL on an allocation error, and the callers must check
803 sdup(compiler_state_t
*cstate
, const char *s
)
805 size_t n
= strlen(s
) + 1;
806 char *cp
= newchunk_nolongjmp(cstate
, n
);
810 pcapint_strlcpy(cp
, s
, n
);
814 static inline struct block
*
815 new_block(compiler_state_t
*cstate
, int code
)
819 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
826 static inline struct slist
*
827 new_stmt(compiler_state_t
*cstate
, int code
)
831 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
837 static struct block
*
838 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
840 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
846 static struct block
*
847 gen_retblk(compiler_state_t
*cstate
, int v
)
849 if (setjmp(cstate
->top_ctx
)) {
851 * gen_retblk() only fails because a memory
852 * allocation failed in newchunk(), meaning
853 * that it can't return a pointer.
859 return gen_retblk_internal(cstate
, v
);
862 static inline PCAP_NORETURN_DEF
void
863 syntax(compiler_state_t
*cstate
)
865 bpf_error(cstate
, "syntax error in filter expression");
869 * For the given integer return a string with the keyword (or the nominal
870 * keyword if there is more than one). This is a simpler version of tok2str()
871 * in tcpdump because in this problem space a valid integer value is not
875 qual2kw(const char *kind
, const unsigned id
, const char *tokens
[],
878 static char buf
[4][64];
881 if (id
< size
&& tokens
[id
])
884 char *ret
= buf
[idx
];
885 idx
= (idx
+ 1) % (sizeof(buf
) / sizeof(buf
[0]));
886 ret
[0] = '\0'; // just in case
887 snprintf(ret
, sizeof(buf
[0]), "<invalid %s %u>", kind
, id
);
891 // protocol qualifier keywords
893 pqkw(const unsigned id
)
895 const char * tokens
[] = {
907 [Q_DECNET
] = "decnet",
913 [Q_ICMPV6
] = "icmp6",
925 [Q_NETBEUI
] = "netbeui",
928 [Q_ISIS_IIH
] = "iih",
929 [Q_ISIS_SNP
] = "snp",
930 [Q_ISIS_CSNP
] = "csnp",
931 [Q_ISIS_PSNP
] = "psnp",
932 [Q_ISIS_LSP
] = "lsp",
936 return qual2kw("proto", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
939 // direction qualifier keywords
941 dqkw(const unsigned id
)
943 const char * map
[] = {
946 [Q_OR
] = "src or dst",
947 [Q_AND
] = "src and dst",
955 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
960 atmkw(const unsigned id
)
962 const char * tokens
[] = {
965 [A_OAMF4SC
] = "oamf4sc",
966 [A_OAMF4EC
] = "oamf4ec",
974 [A_CONNECTMSG
] = "connectmsg",
975 [A_METACONNECT
] = "metaconnect",
977 return qual2kw("ATM keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
982 ss7kw(const unsigned id
)
984 const char * tokens
[] = {
1000 return qual2kw("MTP keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
1003 static PCAP_NORETURN_DEF
void
1004 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
1006 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
1007 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
1011 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
1013 if (cstate
->linktype
!= DLT_PFLOG
)
1014 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
1018 assert_atm(compiler_state_t
*cstate
, const char *kw
)
1021 * Belt and braces: init_linktype() sets either all of these struct
1022 * members (for DLT_SUNATM) or none (otherwise).
1024 if (cstate
->linktype
!= DLT_SUNATM
||
1026 cstate
->off_vpi
== OFFSET_NOT_SET
||
1027 cstate
->off_vci
== OFFSET_NOT_SET
||
1028 cstate
->off_proto
== OFFSET_NOT_SET
||
1029 cstate
->off_payload
== OFFSET_NOT_SET
)
1030 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1034 assert_ss7(compiler_state_t
*cstate
, const char *kw
)
1036 switch (cstate
->linktype
) {
1039 case DLT_MTP2_WITH_PHDR
:
1040 // Belt and braces, same as in assert_atm().
1041 if (cstate
->off_sio
!= OFFSET_NOT_SET
&&
1042 cstate
->off_opc
!= OFFSET_NOT_SET
&&
1043 cstate
->off_dpc
!= OFFSET_NOT_SET
&&
1044 cstate
->off_sls
!= OFFSET_NOT_SET
)
1047 bpf_error(cstate
, "'%s' supported only on SS7", kw
);
1051 assert_maxval(compiler_state_t
*cstate
, const char *name
,
1052 const bpf_u_int32 val
, const bpf_u_int32 maxval
)
1055 bpf_error(cstate
, "%s %u greater than maximum %u",
1059 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1060 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1061 #define ERRSTR_UNKNOWN_MAC48HOST "unknown Ethernet-like host '%s'"
1063 // Validate a port/portrange proto qualifier and map to an IP protocol number.
1065 port_pq_to_ipproto(compiler_state_t
*cstate
, const int proto
, const char *kw
)
1073 return IPPROTO_SCTP
;
1077 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), kw
);
1081 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1082 const char *buf
, int optimize
, bpf_u_int32 mask
)
1088 compiler_state_t cstate
;
1089 yyscan_t scanner
= NULL
;
1090 YY_BUFFER_STATE in_buffer
= NULL
;
1095 * If this pcap_t hasn't been activated, it doesn't have a
1096 * link-layer type, so we can't use it.
1098 if (!p
->activated
) {
1099 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1100 "not-yet-activated pcap_t passed to pcap_compile");
1101 return (PCAP_ERROR
);
1106 * Initialize Winsock, asking for the latest version (2.2),
1107 * as we may be calling Winsock routines to translate
1108 * host names to addresses.
1110 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1112 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1113 err
, "Error calling WSAStartup()");
1114 return (PCAP_ERROR
);
1118 #ifdef ENABLE_REMOTE
1120 * If the device on which we're capturing need to be notified
1121 * that a new filter is being compiled, do so.
1123 * This allows them to save a copy of it, in case, for example,
1124 * they're implementing a form of remote packet capture, and
1125 * want the remote machine to filter out the packets in which
1126 * it's sending the packets it's captured.
1128 * XXX - the fact that we happen to be compiling a filter
1129 * doesn't necessarily mean we'll be installing it as the
1130 * filter for this pcap_t; we might be running it from userland
1131 * on captured packets to do packet classification. We really
1132 * need a better way of handling this, but this is all that
1133 * the WinPcap remote capture code did.
1135 if (p
->save_current_filter_op
!= NULL
)
1136 (p
->save_current_filter_op
)(p
, buf
);
1139 initchunks(&cstate
);
1140 cstate
.no_optimize
= 0;
1143 cstate
.ic
.root
= NULL
;
1144 cstate
.ic
.cur_mark
= 0;
1145 cstate
.bpf_pcap
= p
;
1146 cstate
.error_set
= 0;
1149 cstate
.netmask
= mask
;
1151 cstate
.snaplen
= pcap_snapshot(p
);
1152 if (cstate
.snaplen
== 0) {
1153 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1154 "snaplen of 0 rejects all packets");
1159 if (pcap_lex_init(&scanner
) != 0) {
1160 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1161 errno
, "can't initialize scanner");
1165 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1168 * Associate the compiler state with the lexical analyzer
1171 pcap_set_extra(&cstate
, scanner
);
1173 if (init_linktype(&cstate
, p
) == -1) {
1177 if (pcap_parse(scanner
, &cstate
) != 0) {
1178 if (cstate
.ai
!= NULL
)
1179 freeaddrinfo(cstate
.ai
);
1180 if (cstate
.e
!= NULL
)
1186 if (cstate
.ic
.root
== NULL
) {
1187 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1190 * Catch errors reported by gen_retblk().
1192 if (cstate
.ic
.root
== NULL
) {
1198 if (optimize
&& !cstate
.no_optimize
) {
1199 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1204 if (cstate
.ic
.root
== NULL
||
1205 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1206 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1207 "expression rejects all packets");
1212 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1213 cstate
.ic
.root
, &len
, p
->errbuf
);
1214 if (program
->bf_insns
== NULL
) {
1219 program
->bf_len
= len
;
1221 rc
= 0; /* We're all okay */
1225 * Clean up everything for the lexical analyzer.
1227 if (in_buffer
!= NULL
)
1228 pcap__delete_buffer(in_buffer
, scanner
);
1229 if (scanner
!= NULL
)
1230 pcap_lex_destroy(scanner
);
1233 * Clean up our own allocated memory.
1235 freechunks(&cstate
);
1245 * entry point for using the compiler with no pcap open
1246 * pass in all the stuff that is needed explicitly instead.
1249 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1250 struct bpf_program
*program
,
1251 const char *buf
, int optimize
, bpf_u_int32 mask
)
1256 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1258 return (PCAP_ERROR
);
1259 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1265 * Clean up a "struct bpf_program" by freeing all the memory allocated
1269 pcap_freecode(struct bpf_program
*program
)
1271 program
->bf_len
= 0;
1272 if (program
->bf_insns
!= NULL
) {
1273 free((char *)program
->bf_insns
);
1274 program
->bf_insns
= NULL
;
1279 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1280 * which of the jt and jf fields has been resolved and which is a pointer
1281 * back to another unresolved block (or nil). At least one of the fields
1282 * in each block is already resolved.
1285 backpatch(struct block
*list
, struct block
*target
)
1302 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1303 * which of jt and jf is the link.
1306 merge(struct block
*b0
, struct block
*b1
)
1308 register struct block
**p
= &b0
;
1310 /* Find end of list. */
1312 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1314 /* Concatenate the lists. */
1319 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1322 * Catch errors reported by us and routines below us, and return -1
1325 if (setjmp(cstate
->top_ctx
))
1329 * Insert before the statements of the first (root) block any
1330 * statements needed to load the lengths of any variable-length
1331 * headers into registers.
1333 * XXX - a fancier strategy would be to insert those before the
1334 * statements of all blocks that use those lengths and that
1335 * have no predecessors that use them, so that we only compute
1336 * the lengths if we need them. There might be even better
1337 * approaches than that.
1339 * However, those strategies would be more complicated, and
1340 * as we don't generate code to compute a length if the
1341 * program has no tests that use the length, and as most
1342 * tests will probably use those lengths, we would just
1343 * postpone computing the lengths so that it's not done
1344 * for tests that fail early, and it's not clear that's
1347 insert_compute_vloffsets(cstate
, p
->head
);
1350 * For DLT_PPI captures, generate a check of the per-packet
1351 * DLT value to make sure it's DLT_IEEE802_11.
1353 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1354 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1355 * with appropriate Ethernet information and use that rather
1356 * than using something such as DLT_PPI where you don't know
1357 * the link-layer header type until runtime, which, in the
1358 * general case, would force us to generate both Ethernet *and*
1359 * 802.11 code (*and* anything else for which PPI is used)
1360 * and choose between them early in the BPF program?
1362 if (cstate
->linktype
== DLT_PPI
) {
1363 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1364 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1365 gen_and(ppi_dlt_check
, p
);
1368 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1369 p
->sense
= !p
->sense
;
1370 backpatch(p
, gen_retblk_internal(cstate
, 0));
1371 cstate
->ic
.root
= p
->head
;
1376 gen_and(struct block
*b0
, struct block
*b1
)
1378 backpatch(b0
, b1
->head
);
1379 b0
->sense
= !b0
->sense
;
1380 b1
->sense
= !b1
->sense
;
1382 b1
->sense
= !b1
->sense
;
1383 b1
->head
= b0
->head
;
1387 gen_or(struct block
*b0
, struct block
*b1
)
1389 b0
->sense
= !b0
->sense
;
1390 backpatch(b0
, b1
->head
);
1391 b0
->sense
= !b0
->sense
;
1393 b1
->head
= b0
->head
;
1397 gen_not(struct block
*b
)
1399 b
->sense
= !b
->sense
;
1402 static struct block
*
1403 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1404 u_int size
, bpf_u_int32 v
)
1406 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1409 static struct block
*
1410 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1411 u_int size
, bpf_u_int32 v
)
1413 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1416 static struct block
*
1417 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1418 u_int size
, bpf_u_int32 v
)
1420 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1423 static struct block
*
1424 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1425 u_int size
, bpf_u_int32 v
)
1427 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1430 static struct block
*
1431 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1432 u_int size
, bpf_u_int32 v
)
1434 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1437 static struct block
*
1438 gen_cmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1439 u_int size
, bpf_u_int32 v
)
1441 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 1, v
);
1444 static struct block
*
1445 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1446 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1449 * For any A: if mask == 0, it means A & mask == 0, so the result is
1450 * true iff v == 0. In this case ideally the caller should have
1451 * skipped this invocation and have fewer statement blocks to juggle.
1452 * If the caller could have skipped, but has not, produce a block with
1455 * This could be done in gen_ncmp() in a more generic way, but this
1456 * function is the only code path that can have mask == 0.
1459 return v
? gen_false(cstate
) : gen_true(cstate
);
1461 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1464 static struct block
*
1465 gen_mcmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1466 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1468 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 1, v
);
1471 static struct block
*
1472 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1473 u_int size
, const u_char
*v
)
1475 register struct block
*b
, *tmp
;
1479 register const u_char
*p
= &v
[size
- 4];
1481 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1489 register const u_char
*p
= &v
[size
- 2];
1491 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1499 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1507 static struct block
*
1508 gen_jmp(compiler_state_t
*cstate
, int jtype
, bpf_u_int32 v
, struct slist
*stmts
)
1510 struct block
*b
= new_block(cstate
, JMP(jtype
));
1516 static struct block
*
1517 gen_set(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1519 return gen_jmp(cstate
, BPF_JSET
, v
, stmts
);
1522 static struct block
*
1523 gen_unset(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1525 struct block
*b
= gen_set(cstate
, v
, stmts
);
1531 * AND the field of size "size" at offset "offset" relative to the header
1532 * specified by "offrel" with "mask", and compare it with the value "v"
1533 * with the test specified by "jtype"; if "reverse" is true, the test
1534 * should test the opposite of "jtype".
1536 static struct block
*
1537 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1538 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1541 struct slist
*s
, *s2
;
1544 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1546 if (mask
!= 0xffffffff) {
1547 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1552 b
= gen_jmp(cstate
, jtype
, v
, s
);
1559 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1561 cstate
->pcap_fddipad
= p
->fddipad
;
1564 * We start out with only one link-layer header.
1566 cstate
->outermostlinktype
= pcap_datalink(p
);
1567 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1568 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1569 cstate
->off_outermostlinkhdr
.reg
= -1;
1571 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1572 cstate
->off_prevlinkhdr
.constant_part
= 0;
1573 cstate
->off_prevlinkhdr
.is_variable
= 0;
1574 cstate
->off_prevlinkhdr
.reg
= -1;
1576 cstate
->linktype
= cstate
->outermostlinktype
;
1577 cstate
->off_linkhdr
.constant_part
= 0;
1578 cstate
->off_linkhdr
.is_variable
= 0;
1579 cstate
->off_linkhdr
.reg
= -1;
1584 cstate
->off_linkpl
.constant_part
= 0;
1585 cstate
->off_linkpl
.is_variable
= 0;
1586 cstate
->off_linkpl
.reg
= -1;
1588 cstate
->off_linktype
.constant_part
= 0;
1589 cstate
->off_linktype
.is_variable
= 0;
1590 cstate
->off_linktype
.reg
= -1;
1593 * Assume it's not raw ATM with a pseudo-header, for now.
1596 cstate
->off_vpi
= OFFSET_NOT_SET
;
1597 cstate
->off_vci
= OFFSET_NOT_SET
;
1598 cstate
->off_proto
= OFFSET_NOT_SET
;
1599 cstate
->off_payload
= OFFSET_NOT_SET
;
1602 * And not encapsulated with either Geneve or VXLAN.
1604 cstate
->is_encap
= 0;
1607 * No variable length VLAN offset by default
1609 cstate
->is_vlan_vloffset
= 0;
1612 * And assume we're not doing SS7.
1614 cstate
->off_li
= OFFSET_NOT_SET
;
1615 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1616 cstate
->off_sio
= OFFSET_NOT_SET
;
1617 cstate
->off_opc
= OFFSET_NOT_SET
;
1618 cstate
->off_dpc
= OFFSET_NOT_SET
;
1619 cstate
->off_sls
= OFFSET_NOT_SET
;
1621 cstate
->label_stack_depth
= 0;
1622 cstate
->vlan_stack_depth
= 0;
1624 switch (cstate
->linktype
) {
1627 cstate
->off_linktype
.constant_part
= 2;
1628 cstate
->off_linkpl
.constant_part
= 6;
1629 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1630 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1633 case DLT_ARCNET_LINUX
:
1634 cstate
->off_linktype
.constant_part
= 4;
1635 cstate
->off_linkpl
.constant_part
= 8;
1636 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1637 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1641 cstate
->off_linktype
.constant_part
= 12;
1642 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1643 cstate
->off_nl
= 0; /* Ethernet II */
1644 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1649 * SLIP doesn't have a link level type. The 16 byte
1650 * header is hacked into our SLIP driver.
1652 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1653 cstate
->off_linkpl
.constant_part
= 16;
1655 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1658 case DLT_SLIP_BSDOS
:
1659 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1660 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1662 cstate
->off_linkpl
.constant_part
= 24;
1664 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1669 cstate
->off_linktype
.constant_part
= 0;
1670 cstate
->off_linkpl
.constant_part
= 4;
1672 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1676 cstate
->off_linktype
.constant_part
= 0;
1677 cstate
->off_linkpl
.constant_part
= 12;
1679 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1684 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1685 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1686 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1687 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1688 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1690 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1695 * This does not include the Ethernet header, and
1696 * only covers session state.
1698 cstate
->off_linktype
.constant_part
= 6;
1699 cstate
->off_linkpl
.constant_part
= 8;
1701 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1705 cstate
->off_linktype
.constant_part
= 5;
1706 cstate
->off_linkpl
.constant_part
= 24;
1708 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1713 * FDDI doesn't really have a link-level type field.
1714 * We set "off_linktype" to the offset of the LLC header.
1716 * To check for Ethernet types, we assume that SSAP = SNAP
1717 * is being used and pick out the encapsulated Ethernet type.
1718 * XXX - should we generate code to check for SNAP?
1720 cstate
->off_linktype
.constant_part
= 13;
1721 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1722 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1723 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1724 cstate
->off_nl
= 8; /* 802.2+SNAP */
1725 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1730 * Token Ring doesn't really have a link-level type field.
1731 * We set "off_linktype" to the offset of the LLC header.
1733 * To check for Ethernet types, we assume that SSAP = SNAP
1734 * is being used and pick out the encapsulated Ethernet type.
1735 * XXX - should we generate code to check for SNAP?
1737 * XXX - the header is actually variable-length.
1738 * Some various Linux patched versions gave 38
1739 * as "off_linktype" and 40 as "off_nl"; however,
1740 * if a token ring packet has *no* routing
1741 * information, i.e. is not source-routed, the correct
1742 * values are 20 and 22, as they are in the vanilla code.
1744 * A packet is source-routed iff the uppermost bit
1745 * of the first byte of the source address, at an
1746 * offset of 8, has the uppermost bit set. If the
1747 * packet is source-routed, the total number of bytes
1748 * of routing information is 2 plus bits 0x1F00 of
1749 * the 16-bit value at an offset of 14 (shifted right
1750 * 8 - figure out which byte that is).
1752 cstate
->off_linktype
.constant_part
= 14;
1753 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1754 cstate
->off_nl
= 8; /* 802.2+SNAP */
1755 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1758 case DLT_PRISM_HEADER
:
1759 case DLT_IEEE802_11_RADIO_AVS
:
1760 case DLT_IEEE802_11_RADIO
:
1761 cstate
->off_linkhdr
.is_variable
= 1;
1762 /* Fall through, 802.11 doesn't have a variable link
1763 * prefix but is otherwise the same. */
1766 case DLT_IEEE802_11
:
1768 * 802.11 doesn't really have a link-level type field.
1769 * We set "off_linktype.constant_part" to the offset of
1772 * To check for Ethernet types, we assume that SSAP = SNAP
1773 * is being used and pick out the encapsulated Ethernet type.
1774 * XXX - should we generate code to check for SNAP?
1776 * We also handle variable-length radio headers here.
1777 * The Prism header is in theory variable-length, but in
1778 * practice it's always 144 bytes long. However, some
1779 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1780 * sometimes or always supply an AVS header, so we
1781 * have to check whether the radio header is a Prism
1782 * header or an AVS header, so, in practice, it's
1785 cstate
->off_linktype
.constant_part
= 24;
1786 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1787 cstate
->off_linkpl
.is_variable
= 1;
1788 cstate
->off_nl
= 8; /* 802.2+SNAP */
1789 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1794 * At the moment we treat PPI the same way that we treat
1795 * normal Radiotap encoded packets. The difference is in
1796 * the function that generates the code at the beginning
1797 * to compute the header length. Since this code generator
1798 * of PPI supports bare 802.11 encapsulation only (i.e.
1799 * the encapsulated DLT should be DLT_IEEE802_11) we
1800 * generate code to check for this too.
1802 cstate
->off_linktype
.constant_part
= 24;
1803 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1804 cstate
->off_linkpl
.is_variable
= 1;
1805 cstate
->off_linkhdr
.is_variable
= 1;
1806 cstate
->off_nl
= 8; /* 802.2+SNAP */
1807 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1810 case DLT_ATM_RFC1483
:
1811 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1813 * assume routed, non-ISO PDUs
1814 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1816 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1817 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1818 * latter would presumably be treated the way PPPoE
1819 * should be, so you can do "pppoe and udp port 2049"
1820 * or "pppoa and tcp port 80" and have it check for
1821 * PPPo{A,E} and a PPP protocol of IP and....
1823 cstate
->off_linktype
.constant_part
= 0;
1824 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1825 cstate
->off_nl
= 8; /* 802.2+SNAP */
1826 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1831 * Full Frontal ATM; you get AALn PDUs with an ATM
1835 cstate
->off_vpi
= SUNATM_VPI_POS
;
1836 cstate
->off_vci
= SUNATM_VCI_POS
;
1837 cstate
->off_proto
= PROTO_POS
;
1838 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1839 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1840 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1841 cstate
->off_nl
= 8; /* 802.2+SNAP */
1842 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1848 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1849 cstate
->off_linkpl
.constant_part
= 0;
1851 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1854 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1855 cstate
->off_linktype
.constant_part
= 14;
1856 cstate
->off_linkpl
.constant_part
= 16;
1858 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1861 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1862 cstate
->off_linktype
.constant_part
= 0;
1863 cstate
->off_linkpl
.constant_part
= 20;
1865 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1870 * LocalTalk does have a 1-byte type field in the LLAP header,
1871 * but really it just indicates whether there is a "short" or
1872 * "long" DDP packet following.
1874 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1875 cstate
->off_linkpl
.constant_part
= 0;
1877 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1880 case DLT_IP_OVER_FC
:
1882 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1883 * link-level type field. We set "off_linktype" to the
1884 * offset of the LLC header.
1886 * To check for Ethernet types, we assume that SSAP = SNAP
1887 * is being used and pick out the encapsulated Ethernet type.
1888 * XXX - should we generate code to check for SNAP? RFC
1889 * 2625 says SNAP should be used.
1891 cstate
->off_linktype
.constant_part
= 16;
1892 cstate
->off_linkpl
.constant_part
= 16;
1893 cstate
->off_nl
= 8; /* 802.2+SNAP */
1894 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1899 * XXX - we should set this to handle SNAP-encapsulated
1900 * frames (NLPID of 0x80).
1902 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1903 cstate
->off_linkpl
.constant_part
= 0;
1905 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1909 * the only BPF-interesting FRF.16 frames are non-control frames;
1910 * Frame Relay has a variable length link-layer
1911 * so lets start with offset 4 for now and increments later on (FIXME);
1914 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1915 cstate
->off_linkpl
.constant_part
= 0;
1917 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1920 case DLT_APPLE_IP_OVER_IEEE1394
:
1921 cstate
->off_linktype
.constant_part
= 16;
1922 cstate
->off_linkpl
.constant_part
= 18;
1924 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1927 case DLT_SYMANTEC_FIREWALL
:
1928 cstate
->off_linktype
.constant_part
= 6;
1929 cstate
->off_linkpl
.constant_part
= 44;
1930 cstate
->off_nl
= 0; /* Ethernet II */
1931 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1935 cstate
->off_linktype
.constant_part
= 0;
1936 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1937 cstate
->off_linkpl
.is_variable
= 1;
1939 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1942 case DLT_JUNIPER_MFR
:
1943 case DLT_JUNIPER_MLFR
:
1944 case DLT_JUNIPER_MLPPP
:
1945 case DLT_JUNIPER_PPP
:
1946 case DLT_JUNIPER_CHDLC
:
1947 case DLT_JUNIPER_FRELAY
:
1948 cstate
->off_linktype
.constant_part
= 4;
1949 cstate
->off_linkpl
.constant_part
= 4;
1951 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1954 case DLT_JUNIPER_ATM1
:
1955 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1956 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1958 cstate
->off_nl_nosnap
= 10;
1961 case DLT_JUNIPER_ATM2
:
1962 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1963 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1965 cstate
->off_nl_nosnap
= 10;
1968 /* frames captured on a Juniper PPPoE service PIC
1969 * contain raw ethernet frames */
1970 case DLT_JUNIPER_PPPOE
:
1971 case DLT_JUNIPER_ETHER
:
1972 cstate
->off_linkpl
.constant_part
= 14;
1973 cstate
->off_linktype
.constant_part
= 16;
1974 cstate
->off_nl
= 18; /* Ethernet II */
1975 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1978 case DLT_JUNIPER_PPPOE_ATM
:
1979 cstate
->off_linktype
.constant_part
= 4;
1980 cstate
->off_linkpl
.constant_part
= 6;
1982 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1985 case DLT_JUNIPER_GGSN
:
1986 cstate
->off_linktype
.constant_part
= 6;
1987 cstate
->off_linkpl
.constant_part
= 12;
1989 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1992 case DLT_JUNIPER_ES
:
1993 cstate
->off_linktype
.constant_part
= 6;
1994 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1995 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1996 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1999 case DLT_JUNIPER_MONITOR
:
2000 cstate
->off_linktype
.constant_part
= 12;
2001 cstate
->off_linkpl
.constant_part
= 12;
2002 cstate
->off_nl
= 0; /* raw IP/IP6 header */
2003 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2006 case DLT_BACNET_MS_TP
:
2007 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2008 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2009 cstate
->off_nl
= OFFSET_NOT_SET
;
2010 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2013 case DLT_JUNIPER_SERVICES
:
2014 cstate
->off_linktype
.constant_part
= 12;
2015 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2016 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2017 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2020 case DLT_JUNIPER_VP
:
2021 cstate
->off_linktype
.constant_part
= 18;
2022 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2023 cstate
->off_nl
= OFFSET_NOT_SET
;
2024 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2027 case DLT_JUNIPER_ST
:
2028 cstate
->off_linktype
.constant_part
= 18;
2029 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2030 cstate
->off_nl
= OFFSET_NOT_SET
;
2031 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2034 case DLT_JUNIPER_ISM
:
2035 cstate
->off_linktype
.constant_part
= 8;
2036 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2037 cstate
->off_nl
= OFFSET_NOT_SET
;
2038 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2041 case DLT_JUNIPER_VS
:
2042 case DLT_JUNIPER_SRX_E2E
:
2043 case DLT_JUNIPER_FIBRECHANNEL
:
2044 case DLT_JUNIPER_ATM_CEMIC
:
2045 cstate
->off_linktype
.constant_part
= 8;
2046 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2047 cstate
->off_nl
= OFFSET_NOT_SET
;
2048 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2053 cstate
->off_li_hsl
= 4;
2054 cstate
->off_sio
= 3;
2055 cstate
->off_opc
= 4;
2056 cstate
->off_dpc
= 4;
2057 cstate
->off_sls
= 7;
2058 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2059 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2060 cstate
->off_nl
= OFFSET_NOT_SET
;
2061 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2064 case DLT_MTP2_WITH_PHDR
:
2066 cstate
->off_li_hsl
= 8;
2067 cstate
->off_sio
= 7;
2068 cstate
->off_opc
= 8;
2069 cstate
->off_dpc
= 8;
2070 cstate
->off_sls
= 11;
2071 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2072 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2073 cstate
->off_nl
= OFFSET_NOT_SET
;
2074 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2078 cstate
->off_li
= 22;
2079 cstate
->off_li_hsl
= 24;
2080 cstate
->off_sio
= 23;
2081 cstate
->off_opc
= 24;
2082 cstate
->off_dpc
= 24;
2083 cstate
->off_sls
= 27;
2084 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2085 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2086 cstate
->off_nl
= OFFSET_NOT_SET
;
2087 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2091 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2092 cstate
->off_linkpl
.constant_part
= 4;
2094 cstate
->off_nl_nosnap
= 0;
2099 * Currently, only raw "link[N:M]" filtering is supported.
2101 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
2102 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2103 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
2104 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2108 cstate
->off_linktype
.constant_part
= 1;
2109 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2111 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2114 case DLT_NETANALYZER
:
2115 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
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+Ethernet header length */
2118 cstate
->off_nl
= 0; /* Ethernet II */
2119 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2122 case DLT_NETANALYZER_TRANSPARENT
:
2123 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2124 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2125 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2126 cstate
->off_nl
= 0; /* Ethernet II */
2127 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2132 * For values in the range in which we've assigned new
2133 * DLT_ values, only raw "link[N:M]" filtering is supported.
2135 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2136 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2137 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2138 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2139 cstate
->off_nl
= OFFSET_NOT_SET
;
2140 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2142 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2143 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2149 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2154 * Load a value relative to the specified absolute offset.
2156 static struct slist
*
2157 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2158 u_int offset
, u_int size
)
2160 struct slist
*s
, *s2
;
2162 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2165 * If "s" is non-null, it has code to arrange that the X register
2166 * contains the variable part of the absolute offset, so we
2167 * generate a load relative to that, with an offset of
2168 * abs_offset->constant_part + offset.
2170 * Otherwise, we can do an absolute load with an offset of
2171 * abs_offset->constant_part + offset.
2175 * "s" points to a list of statements that puts the
2176 * variable part of the absolute offset into the X register.
2177 * Do an indirect load, to use the X register as an offset.
2179 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2180 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2184 * There is no variable part of the absolute offset, so
2185 * just do an absolute load.
2187 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2188 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2194 * Load a value relative to the beginning of the specified header.
2196 static struct slist
*
2197 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2200 struct slist
*s
, *s2
;
2203 * Squelch warnings from compilers that *don't* assume that
2204 * offrel always has a valid enum value and therefore don't
2205 * assume that we'll always go through one of the case arms.
2207 * If we have a default case, compilers that *do* assume that
2208 * will then complain about the default case code being
2211 * Damned if you do, damned if you don't.
2218 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2223 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2226 case OR_PREVLINKHDR
:
2227 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2231 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2234 case OR_PREVMPLSHDR
:
2235 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2239 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2242 case OR_LINKPL_NOSNAP
:
2243 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2247 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2252 * Load the X register with the length of the IPv4 header
2253 * (plus the offset of the link-layer header, if it's
2254 * preceded by a variable-length header such as a radio
2255 * header), in bytes.
2257 s
= gen_loadx_iphdrlen(cstate
);
2260 * Load the item at {offset of the link-layer payload} +
2261 * {offset, relative to the start of the link-layer
2262 * payload, of the IPv4 header} + {length of the IPv4 header} +
2263 * {specified offset}.
2265 * If the offset of the link-layer payload is variable,
2266 * the variable part of that offset is included in the
2267 * value in the X register, and we include the constant
2268 * part in the offset of the load.
2270 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2271 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2276 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2283 * Generate code to load into the X register the sum of the length of
2284 * the IPv4 header and the variable part of the offset of the link-layer
2287 static struct slist
*
2288 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2290 struct slist
*s
, *s2
;
2292 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2295 * The offset of the link-layer payload has a variable
2296 * part. "s" points to a list of statements that put
2297 * the variable part of that offset into the X register.
2299 * The 4*([k]&0xf) addressing mode can't be used, as we
2300 * don't have a constant offset, so we have to load the
2301 * value in question into the A register and add to it
2302 * the value from the X register.
2304 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2305 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2307 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2310 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2315 * The A register now contains the length of the IP header.
2316 * We need to add to it the variable part of the offset of
2317 * the link-layer payload, which is still in the X
2318 * register, and move the result into the X register.
2320 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2321 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2324 * The offset of the link-layer payload is a constant,
2325 * so no code was generated to load the (nonexistent)
2326 * variable part of that offset.
2328 * This means we can use the 4*([k]&0xf) addressing
2329 * mode. Load the length of the IPv4 header, which
2330 * is at an offset of cstate->off_nl from the beginning of
2331 * the link-layer payload, and thus at an offset of
2332 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2333 * of the raw packet data, using that addressing mode.
2335 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2336 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2342 static struct block
*
2343 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2347 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2349 return gen_jmp(cstate
, BPF_JEQ
, 0, s
);
2352 static inline struct block
*
2353 gen_true(compiler_state_t
*cstate
)
2355 return gen_uncond(cstate
, 1);
2358 static inline struct block
*
2359 gen_false(compiler_state_t
*cstate
)
2361 return gen_uncond(cstate
, 0);
2365 * Generate code to match a particular packet type.
2367 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2368 * value, if <= ETHERMTU. We use that to determine whether to
2369 * match the type/length field or to check the type/length field for
2370 * a value <= ETHERMTU to see whether it's a type field and then do
2371 * the appropriate test.
2373 static struct block
*
2374 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2376 struct block
*b0
, *b1
;
2382 case LLCSAP_NETBEUI
:
2384 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2385 * so we check the DSAP and SSAP.
2387 * LLCSAP_IP checks for IP-over-802.2, rather
2388 * than IP-over-Ethernet or IP-over-SNAP.
2390 * XXX - should we check both the DSAP and the
2391 * SSAP, like this, or should we check just the
2392 * DSAP, as we do for other types <= ETHERMTU
2393 * (i.e., other SAP values)?
2395 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2396 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2404 * Ethernet_II frames, which are Ethernet
2405 * frames with a frame type of ETHERTYPE_IPX;
2407 * Ethernet_802.3 frames, which are 802.3
2408 * frames (i.e., the type/length field is
2409 * a length field, <= ETHERMTU, rather than
2410 * a type field) with the first two bytes
2411 * after the Ethernet/802.3 header being
2414 * Ethernet_802.2 frames, which are 802.3
2415 * frames with an 802.2 LLC header and
2416 * with the IPX LSAP as the DSAP in the LLC
2419 * Ethernet_SNAP frames, which are 802.3
2420 * frames with an LLC header and a SNAP
2421 * header and with an OUI of 0x000000
2422 * (encapsulated Ethernet) and a protocol
2423 * ID of ETHERTYPE_IPX in the SNAP header.
2425 * XXX - should we generate the same code both
2426 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2430 * This generates code to check both for the
2431 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2433 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2434 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2438 * Now we add code to check for SNAP frames with
2439 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2441 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2445 * Now we generate code to check for 802.3
2446 * frames in general.
2448 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2451 * Now add the check for 802.3 frames before the
2452 * check for Ethernet_802.2 and Ethernet_802.3,
2453 * as those checks should only be done on 802.3
2454 * frames, not on Ethernet frames.
2459 * Now add the check for Ethernet_II frames, and
2460 * do that before checking for the other frame
2463 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2467 case ETHERTYPE_ATALK
:
2468 case ETHERTYPE_AARP
:
2470 * EtherTalk (AppleTalk protocols on Ethernet link
2471 * layer) may use 802.2 encapsulation.
2475 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2476 * we check for an Ethernet type field less or equal than
2477 * 1500, which means it's an 802.3 length field.
2479 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2482 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2483 * SNAP packets with an organization code of
2484 * 0x080007 (Apple, for Appletalk) and a protocol
2485 * type of ETHERTYPE_ATALK (Appletalk).
2487 * 802.2-encapsulated ETHERTYPE_AARP packets are
2488 * SNAP packets with an organization code of
2489 * 0x000000 (encapsulated Ethernet) and a protocol
2490 * type of ETHERTYPE_AARP (Appletalk ARP).
2492 if (ll_proto
== ETHERTYPE_ATALK
)
2493 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2494 else /* ll_proto == ETHERTYPE_AARP */
2495 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2499 * Check for Ethernet encapsulation (Ethertalk
2500 * phase 1?); we just check for the Ethernet
2503 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2509 if (ll_proto
<= ETHERMTU
) {
2510 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2512 * This is an LLC SAP value, so the frames
2513 * that match would be 802.2 frames.
2514 * Check that the frame is an 802.2 frame
2515 * (i.e., that the length/type field is
2516 * a length field, <= ETHERMTU) and
2517 * then check the DSAP.
2519 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2520 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2524 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2526 * This is an Ethernet type, so compare
2527 * the length/type field with it (if
2528 * the frame is an 802.2 frame, the length
2529 * field will be <= ETHERMTU, and, as
2530 * "ll_proto" is > ETHERMTU, this test
2531 * will fail and the frame won't match,
2532 * which is what we want).
2534 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2539 static struct block
*
2540 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2543 * For DLT_NULL, the link-layer header is a 32-bit word
2544 * containing an AF_ value in *host* byte order, and for
2545 * DLT_ENC, the link-layer header begins with a 32-bit
2546 * word containing an AF_ value in host byte order.
2548 * In addition, if we're reading a saved capture file,
2549 * the host byte order in the capture may not be the
2550 * same as the host byte order on this machine.
2552 * For DLT_LOOP, the link-layer header is a 32-bit
2553 * word containing an AF_ value in *network* byte order.
2555 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2557 * The AF_ value is in host byte order, but the BPF
2558 * interpreter will convert it to network byte order.
2560 * If this is a save file, and it's from a machine
2561 * with the opposite byte order to ours, we byte-swap
2564 * Then we run it through "htonl()", and generate
2565 * code to compare against the result.
2567 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2568 ll_proto
= SWAPLONG(ll_proto
);
2569 ll_proto
= htonl(ll_proto
);
2571 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2575 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2576 * or IPv6 then we have an error.
2578 static struct block
*
2579 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2584 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2587 case ETHERTYPE_IPV6
:
2588 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2595 return gen_false(cstate
);
2599 * Generate code to match a particular packet type.
2601 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2602 * value, if <= ETHERMTU. We use that to determine whether to
2603 * match the type field or to check the type field for the special
2604 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2606 static struct block
*
2607 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2609 struct block
*b0
, *b1
;
2615 case LLCSAP_NETBEUI
:
2617 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2618 * so we check the DSAP and SSAP.
2620 * LLCSAP_IP checks for IP-over-802.2, rather
2621 * than IP-over-Ethernet or IP-over-SNAP.
2623 * XXX - should we check both the DSAP and the
2624 * SSAP, like this, or should we check just the
2625 * DSAP, as we do for other types <= ETHERMTU
2626 * (i.e., other SAP values)?
2628 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2629 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2635 * Ethernet_II frames, which are Ethernet
2636 * frames with a frame type of ETHERTYPE_IPX;
2638 * Ethernet_802.3 frames, which have a frame
2639 * type of LINUX_SLL_P_802_3;
2641 * Ethernet_802.2 frames, which are 802.3
2642 * frames with an 802.2 LLC header (i.e, have
2643 * a frame type of LINUX_SLL_P_802_2) and
2644 * with the IPX LSAP as the DSAP in the LLC
2647 * Ethernet_SNAP frames, which are 802.3
2648 * frames with an LLC header and a SNAP
2649 * header and with an OUI of 0x000000
2650 * (encapsulated Ethernet) and a protocol
2651 * ID of ETHERTYPE_IPX in the SNAP header.
2653 * First, do the checks on LINUX_SLL_P_802_2
2654 * frames; generate the check for either
2655 * Ethernet_802.2 or Ethernet_SNAP frames, and
2656 * then put a check for LINUX_SLL_P_802_2 frames
2659 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2660 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2662 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2666 * Now check for 802.3 frames and OR that with
2667 * the previous test.
2669 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2673 * Now add the check for Ethernet_II frames, and
2674 * do that before checking for the other frame
2677 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2681 case ETHERTYPE_ATALK
:
2682 case ETHERTYPE_AARP
:
2684 * EtherTalk (AppleTalk protocols on Ethernet link
2685 * layer) may use 802.2 encapsulation.
2689 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2690 * we check for the 802.2 protocol type in the
2691 * "Ethernet type" field.
2693 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2696 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2697 * SNAP packets with an organization code of
2698 * 0x080007 (Apple, for Appletalk) and a protocol
2699 * type of ETHERTYPE_ATALK (Appletalk).
2701 * 802.2-encapsulated ETHERTYPE_AARP packets are
2702 * SNAP packets with an organization code of
2703 * 0x000000 (encapsulated Ethernet) and a protocol
2704 * type of ETHERTYPE_AARP (Appletalk ARP).
2706 if (ll_proto
== ETHERTYPE_ATALK
)
2707 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2708 else /* ll_proto == ETHERTYPE_AARP */
2709 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2713 * Check for Ethernet encapsulation (Ethertalk
2714 * phase 1?); we just check for the Ethernet
2717 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2723 if (ll_proto
<= ETHERMTU
) {
2724 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2726 * This is an LLC SAP value, so the frames
2727 * that match would be 802.2 frames.
2728 * Check for the 802.2 protocol type
2729 * in the "Ethernet type" field, and
2730 * then check the DSAP.
2732 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2733 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2738 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2740 * This is an Ethernet type, so compare
2741 * the length/type field with it (if
2742 * the frame is an 802.2 frame, the length
2743 * field will be <= ETHERMTU, and, as
2744 * "ll_proto" is > ETHERMTU, this test
2745 * will fail and the frame won't match,
2746 * which is what we want).
2748 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2754 * Load a value relative to the beginning of the link-layer header after the
2757 static struct slist
*
2758 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2760 struct slist
*s1
, *s2
;
2763 * Generate code to load the length of the pflog header into
2764 * the register assigned to hold that length, if one has been
2765 * assigned. (If one hasn't been assigned, no code we've
2766 * generated uses that prefix, so we don't need to generate any
2769 if (cstate
->off_linkpl
.reg
!= -1) {
2771 * The length is in the first byte of the header.
2773 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2777 * Round it up to a multiple of 4.
2778 * Add 3, and clear the lower 2 bits.
2780 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2783 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2784 s2
->s
.k
= 0xfffffffc;
2788 * Now allocate a register to hold that value and store
2791 s2
= new_stmt(cstate
, BPF_ST
);
2792 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2796 * Now move it into the X register.
2798 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2806 static struct slist
*
2807 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2809 struct slist
*s1
, *s2
;
2810 struct slist
*sjeq_avs_cookie
;
2811 struct slist
*sjcommon
;
2814 * This code is not compatible with the optimizer, as
2815 * we are generating jmp instructions within a normal
2816 * slist of instructions
2818 cstate
->no_optimize
= 1;
2821 * Generate code to load the length of the radio header into
2822 * the register assigned to hold that length, if one has been
2823 * assigned. (If one hasn't been assigned, no code we've
2824 * generated uses that prefix, so we don't need to generate any
2827 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2828 * or always use the AVS header rather than the Prism header.
2829 * We load a 4-byte big-endian value at the beginning of the
2830 * raw packet data, and see whether, when masked with 0xFFFFF000,
2831 * it's equal to 0x80211000. If so, that indicates that it's
2832 * an AVS header (the masked-out bits are the version number).
2833 * Otherwise, it's a Prism header.
2835 * XXX - the Prism header is also, in theory, variable-length,
2836 * but no known software generates headers that aren't 144
2839 if (cstate
->off_linkhdr
.reg
!= -1) {
2843 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2847 * AND it with 0xFFFFF000.
2849 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2850 s2
->s
.k
= 0xFFFFF000;
2854 * Compare with 0x80211000.
2856 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2857 sjeq_avs_cookie
->s
.k
= 0x80211000;
2858 sappend(s1
, sjeq_avs_cookie
);
2863 * The 4 bytes at an offset of 4 from the beginning of
2864 * the AVS header are the length of the AVS header.
2865 * That field is big-endian.
2867 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2870 sjeq_avs_cookie
->s
.jt
= s2
;
2873 * Now jump to the code to allocate a register
2874 * into which to save the header length and
2875 * store the length there. (The "jump always"
2876 * instruction needs to have the k field set;
2877 * it's added to the PC, so, as we're jumping
2878 * over a single instruction, it should be 1.)
2880 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2882 sappend(s1
, sjcommon
);
2885 * Now for the code that handles the Prism header.
2886 * Just load the length of the Prism header (144)
2887 * into the A register. Have the test for an AVS
2888 * header branch here if we don't have an AVS header.
2890 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2893 sjeq_avs_cookie
->s
.jf
= s2
;
2896 * Now allocate a register to hold that value and store
2897 * it. The code for the AVS header will jump here after
2898 * loading the length of the AVS header.
2900 s2
= new_stmt(cstate
, BPF_ST
);
2901 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2903 sjcommon
->s
.jf
= s2
;
2906 * Now move it into the X register.
2908 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2916 static struct slist
*
2917 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2919 struct slist
*s1
, *s2
;
2922 * Generate code to load the length of the AVS header into
2923 * the register assigned to hold that length, if one has been
2924 * assigned. (If one hasn't been assigned, no code we've
2925 * generated uses that prefix, so we don't need to generate any
2928 if (cstate
->off_linkhdr
.reg
!= -1) {
2930 * The 4 bytes at an offset of 4 from the beginning of
2931 * the AVS header are the length of the AVS header.
2932 * That field is big-endian.
2934 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2938 * Now allocate a register to hold that value and store
2941 s2
= new_stmt(cstate
, BPF_ST
);
2942 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2946 * Now move it into the X register.
2948 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2956 static struct slist
*
2957 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2959 struct slist
*s1
, *s2
;
2962 * Generate code to load the length of the radiotap header into
2963 * the register assigned to hold that length, if one has been
2964 * assigned. (If one hasn't been assigned, no code we've
2965 * generated uses that prefix, so we don't need to generate any
2968 if (cstate
->off_linkhdr
.reg
!= -1) {
2970 * The 2 bytes at offsets of 2 and 3 from the beginning
2971 * of the radiotap header are the length of the radiotap
2972 * header; unfortunately, it's little-endian, so we have
2973 * to load it a byte at a time and construct the value.
2977 * Load the high-order byte, at an offset of 3, shift it
2978 * left a byte, and put the result in the X register.
2980 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2982 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2985 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2989 * Load the next byte, at an offset of 2, and OR the
2990 * value from the X register into it.
2992 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2995 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2999 * Now allocate a register to hold that value and store
3002 s2
= new_stmt(cstate
, BPF_ST
);
3003 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3007 * Now move it into the X register.
3009 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3018 * At the moment we treat PPI as normal Radiotap encoded
3019 * packets. The difference is in the function that generates
3020 * the code at the beginning to compute the header length.
3021 * Since this code generator of PPI supports bare 802.11
3022 * encapsulation only (i.e. the encapsulated DLT should be
3023 * DLT_IEEE802_11) we generate code to check for this too;
3024 * that's done in finish_parse().
3026 static struct slist
*
3027 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
3029 struct slist
*s1
, *s2
;
3032 * Generate code to load the length of the radiotap header
3033 * into the register assigned to hold that length, if one has
3036 if (cstate
->off_linkhdr
.reg
!= -1) {
3038 * The 2 bytes at offsets of 2 and 3 from the beginning
3039 * of the radiotap header are the length of the radiotap
3040 * header; unfortunately, it's little-endian, so we have
3041 * to load it a byte at a time and construct the value.
3045 * Load the high-order byte, at an offset of 3, shift it
3046 * left a byte, and put the result in the X register.
3048 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3050 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
3053 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3057 * Load the next byte, at an offset of 2, and OR the
3058 * value from the X register into it.
3060 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3063 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3067 * Now allocate a register to hold that value and store
3070 s2
= new_stmt(cstate
, BPF_ST
);
3071 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3075 * Now move it into the X register.
3077 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3086 * Load a value relative to the beginning of the link-layer header after the 802.11
3087 * header, i.e. LLC_SNAP.
3088 * The link-layer header doesn't necessarily begin at the beginning
3089 * of the packet data; there might be a variable-length prefix containing
3090 * radio information.
3092 static struct slist
*
3093 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
3096 struct slist
*sjset_data_frame_1
;
3097 struct slist
*sjset_data_frame_2
;
3098 struct slist
*sjset_qos
;
3099 struct slist
*sjset_radiotap_flags_present
;
3100 struct slist
*sjset_radiotap_ext_present
;
3101 struct slist
*sjset_radiotap_tsft_present
;
3102 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
3103 struct slist
*s_roundup
;
3105 if (cstate
->off_linkpl
.reg
== -1) {
3107 * No register has been assigned to the offset of
3108 * the link-layer payload, which means nobody needs
3109 * it; don't bother computing it - just return
3110 * what we already have.
3116 * This code is not compatible with the optimizer, as
3117 * we are generating jmp instructions within a normal
3118 * slist of instructions
3120 cstate
->no_optimize
= 1;
3123 * If "s" is non-null, it has code to arrange that the X register
3124 * contains the length of the prefix preceding the link-layer
3127 * Otherwise, the length of the prefix preceding the link-layer
3128 * header is "off_outermostlinkhdr.constant_part".
3132 * There is no variable-length header preceding the
3133 * link-layer header.
3135 * Load the length of the fixed-length prefix preceding
3136 * the link-layer header (if any) into the X register,
3137 * and store it in the cstate->off_linkpl.reg register.
3138 * That length is off_outermostlinkhdr.constant_part.
3140 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3141 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3145 * The X register contains the offset of the beginning of the
3146 * link-layer header; add 24, which is the minimum length
3147 * of the MAC header for a data frame, to that, and store it
3148 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3149 * which is at the offset in the X register, with an indexed load.
3151 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3153 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3156 s2
= new_stmt(cstate
, BPF_ST
);
3157 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3160 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3165 * Check the Frame Control field to see if this is a data frame;
3166 * a data frame has the 0x08 bit (b3) in that field set and the
3167 * 0x04 bit (b2) clear.
3169 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3170 sjset_data_frame_1
->s
.k
= IEEE80211_FC0_TYPE_DATA
;
3171 sappend(s
, sjset_data_frame_1
);
3174 * If b3 is set, test b2, otherwise go to the first statement of
3175 * the rest of the program.
3177 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3178 sjset_data_frame_2
->s
.k
= IEEE80211_FC0_TYPE_CTL
;
3179 sappend(s
, sjset_data_frame_2
);
3180 sjset_data_frame_1
->s
.jf
= snext
;
3183 * If b2 is not set, this is a data frame; test the QoS bit.
3184 * Otherwise, go to the first statement of the rest of the
3187 sjset_data_frame_2
->s
.jt
= snext
;
3188 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3189 sjset_qos
->s
.k
= IEEE80211_FC0_SUBTYPE_QOS
;
3190 sappend(s
, sjset_qos
);
3193 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3195 * Otherwise, go to the first statement of the rest of the
3198 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3199 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3201 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3204 s2
= new_stmt(cstate
, BPF_ST
);
3205 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3209 * If we have a radiotap header, look at it to see whether
3210 * there's Atheros padding between the MAC-layer header
3213 * Note: all of the fields in the radiotap header are
3214 * little-endian, so we byte-swap all of the values
3215 * we test against, as they will be loaded as big-endian
3218 * XXX - in the general case, we would have to scan through
3219 * *all* the presence bits, if there's more than one word of
3220 * presence bits. That would require a loop, meaning that
3221 * we wouldn't be able to run the filter in the kernel.
3223 * We assume here that the Atheros adapters that insert the
3224 * annoying padding don't have multiple antennae and therefore
3225 * do not generate radiotap headers with multiple presence words.
3227 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3229 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3230 * in the first presence flag word?
3232 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3236 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3237 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3238 sappend(s
, sjset_radiotap_flags_present
);
3241 * If not, skip all of this.
3243 sjset_radiotap_flags_present
->s
.jf
= snext
;
3246 * Otherwise, is the "extension" bit set in that word?
3248 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3249 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3250 sappend(s
, sjset_radiotap_ext_present
);
3251 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3254 * If so, skip all of this.
3256 sjset_radiotap_ext_present
->s
.jt
= snext
;
3259 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3261 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3262 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3263 sappend(s
, sjset_radiotap_tsft_present
);
3264 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3267 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3268 * at an offset of 16 from the beginning of the raw packet
3269 * data (8 bytes for the radiotap header and 8 bytes for
3272 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3275 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3278 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3280 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3281 sjset_tsft_datapad
->s
.k
= 0x20;
3282 sappend(s
, sjset_tsft_datapad
);
3285 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3286 * at an offset of 8 from the beginning of the raw packet
3287 * data (8 bytes for the radiotap header).
3289 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3292 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3295 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3297 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3298 sjset_notsft_datapad
->s
.k
= 0x20;
3299 sappend(s
, sjset_notsft_datapad
);
3302 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3303 * set, round the length of the 802.11 header to
3304 * a multiple of 4. Do that by adding 3 and then
3305 * dividing by and multiplying by 4, which we do by
3308 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3309 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3310 sappend(s
, s_roundup
);
3311 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3314 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3315 s2
->s
.k
= (bpf_u_int32
)~3;
3317 s2
= new_stmt(cstate
, BPF_ST
);
3318 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3321 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3322 sjset_tsft_datapad
->s
.jf
= snext
;
3323 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3324 sjset_notsft_datapad
->s
.jf
= snext
;
3326 sjset_qos
->s
.jf
= snext
;
3332 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3336 /* There is an implicit dependency between the link
3337 * payload and link header since the payload computation
3338 * includes the variable part of the header. Therefore,
3339 * if nobody else has allocated a register for the link
3340 * header and we need it, do it now. */
3341 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3342 cstate
->off_linkhdr
.reg
== -1)
3343 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3346 * For link-layer types that have a variable-length header
3347 * preceding the link-layer header, generate code to load
3348 * the offset of the link-layer header into the register
3349 * assigned to that offset, if any.
3351 * XXX - this, and the next switch statement, won't handle
3352 * encapsulation of 802.11 or 802.11+radio information in
3353 * some other protocol stack. That's significantly more
3356 switch (cstate
->outermostlinktype
) {
3358 case DLT_PRISM_HEADER
:
3359 s
= gen_load_prism_llprefixlen(cstate
);
3362 case DLT_IEEE802_11_RADIO_AVS
:
3363 s
= gen_load_avs_llprefixlen(cstate
);
3366 case DLT_IEEE802_11_RADIO
:
3367 s
= gen_load_radiotap_llprefixlen(cstate
);
3371 s
= gen_load_ppi_llprefixlen(cstate
);
3380 * For link-layer types that have a variable-length link-layer
3381 * header, generate code to load the offset of the link-layer
3382 * payload into the register assigned to that offset, if any.
3384 switch (cstate
->outermostlinktype
) {
3386 case DLT_IEEE802_11
:
3387 case DLT_PRISM_HEADER
:
3388 case DLT_IEEE802_11_RADIO_AVS
:
3389 case DLT_IEEE802_11_RADIO
:
3391 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3395 s
= gen_load_pflog_llprefixlen(cstate
);
3400 * If there is no initialization yet and we need variable
3401 * length offsets for VLAN, initialize them to zero
3403 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3406 if (cstate
->off_linkpl
.reg
== -1)
3407 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3408 if (cstate
->off_linktype
.reg
== -1)
3409 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3411 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3413 s2
= new_stmt(cstate
, BPF_ST
);
3414 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3416 s2
= new_stmt(cstate
, BPF_ST
);
3417 s2
->s
.k
= cstate
->off_linktype
.reg
;
3422 * If we have any offset-loading code, append all the
3423 * existing statements in the block to those statements,
3424 * and make the resulting list the list of statements
3428 sappend(s
, b
->stmts
);
3434 * Take an absolute offset, and:
3436 * if it has no variable part, return NULL;
3438 * if it has a variable part, generate code to load the register
3439 * containing that variable part into the X register, returning
3440 * a pointer to that code - if no register for that offset has
3441 * been allocated, allocate it first.
3443 * (The code to set that register will be generated later, but will
3444 * be placed earlier in the code sequence.)
3446 static struct slist
*
3447 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3451 if (off
->is_variable
) {
3452 if (off
->reg
== -1) {
3454 * We haven't yet assigned a register for the
3455 * variable part of the offset of the link-layer
3456 * header; allocate one.
3458 off
->reg
= alloc_reg(cstate
);
3462 * Load the register containing the variable part of the
3463 * offset of the link-layer header into the X register.
3465 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3470 * That offset isn't variable, there's no variable part,
3471 * so we don't need to generate any code.
3478 * Map an Ethernet type to the equivalent PPP type.
3481 ethertype_to_ppptype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3488 case ETHERTYPE_IPV6
:
3494 case ETHERTYPE_ATALK
:
3505 * I'm assuming the "Bridging PDU"s that go
3506 * over PPP are Spanning Tree Protocol
3514 assert_maxval(cstate
, "PPP protocol", ll_proto
, UINT16_MAX
);
3515 return (uint16_t)ll_proto
;
3519 * Generate any tests that, for encapsulation of a link-layer packet
3520 * inside another protocol stack, need to be done to check for those
3521 * link-layer packets (and that haven't already been done by a check
3522 * for that encapsulation).
3524 static struct block
*
3525 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3527 if (cstate
->is_encap
)
3528 return gen_encap_ll_check(cstate
);
3530 switch (cstate
->prevlinktype
) {
3534 * This is LANE-encapsulated Ethernet; check that the LANE
3535 * packet doesn't begin with an LE Control marker, i.e.
3536 * that it's data, not a control message.
3538 * (We've already generated a test for LANE.)
3540 return gen_cmp_ne(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3544 * No such tests are necessary.
3552 * The three different values we should check for when checking for an
3553 * IPv6 packet with DLT_NULL.
3555 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3556 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3557 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3560 * Generate code to match a particular packet type by matching the
3561 * link-layer type field or fields in the 802.2 LLC header.
3563 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3564 * value, if <= ETHERMTU.
3566 static struct block
*
3567 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3569 struct block
*b0
, *b1
, *b2
;
3571 /* are we checking MPLS-encapsulated packets? */
3572 if (cstate
->label_stack_depth
> 0)
3573 return gen_mpls_linktype(cstate
, ll_proto
);
3575 switch (cstate
->linktype
) {
3578 case DLT_NETANALYZER
:
3579 case DLT_NETANALYZER_TRANSPARENT
:
3580 /* Geneve has an EtherType regardless of whether there is an
3581 * L2 header. VXLAN always has an EtherType. */
3582 if (!cstate
->is_encap
)
3583 b0
= gen_prevlinkhdr_check(cstate
);
3587 b1
= gen_ether_linktype(cstate
, ll_proto
);
3595 assert_maxval(cstate
, "HDLC protocol", ll_proto
, UINT16_MAX
);
3599 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3603 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3607 case DLT_IEEE802_11
:
3608 case DLT_PRISM_HEADER
:
3609 case DLT_IEEE802_11_RADIO_AVS
:
3610 case DLT_IEEE802_11_RADIO
:
3613 * Check that we have a data frame.
3615 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3616 IEEE80211_FC0_TYPE_DATA
,
3617 IEEE80211_FC0_TYPE_MASK
);
3620 * Now check for the specified link-layer type.
3622 b1
= gen_llc_linktype(cstate
, ll_proto
);
3629 * XXX - check for LLC frames.
3631 return gen_llc_linktype(cstate
, ll_proto
);
3636 * XXX - check for LLC PDUs, as per IEEE 802.5.
3638 return gen_llc_linktype(cstate
, ll_proto
);
3641 case DLT_ATM_RFC1483
:
3643 case DLT_IP_OVER_FC
:
3644 return gen_llc_linktype(cstate
, ll_proto
);
3649 * Check for an LLC-encapsulated version of this protocol;
3650 * if we were checking for LANE, linktype would no longer
3653 * Check for LLC encapsulation and then check the protocol.
3655 b0
= gen_atm_prototype(cstate
, PT_LLC
);
3656 b1
= gen_llc_linktype(cstate
, ll_proto
);
3662 return gen_linux_sll_linktype(cstate
, ll_proto
);
3666 case DLT_SLIP_BSDOS
:
3669 * These types don't provide any type field; packets
3670 * are always IPv4 or IPv6.
3672 * XXX - for IPv4, check for a version number of 4, and,
3673 * for IPv6, check for a version number of 6?
3678 /* Check for a version number of 4. */
3679 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3681 case ETHERTYPE_IPV6
:
3682 /* Check for a version number of 6. */
3683 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3686 return gen_false(cstate
); /* always false */
3692 * Raw IPv4, so no type field.
3694 if (ll_proto
== ETHERTYPE_IP
)
3695 return gen_true(cstate
); /* always true */
3697 /* Checking for something other than IPv4; always false */
3698 return gen_false(cstate
);
3703 * Raw IPv6, so no type field.
3705 if (ll_proto
== ETHERTYPE_IPV6
)
3706 return gen_true(cstate
); /* always true */
3708 /* Checking for something other than IPv6; always false */
3709 return gen_false(cstate
);
3714 case DLT_PPP_SERIAL
:
3717 * We use Ethernet protocol types inside libpcap;
3718 * map them to the corresponding PPP protocol types.
3720 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3721 ethertype_to_ppptype(cstate
, ll_proto
));
3726 * We use Ethernet protocol types inside libpcap;
3727 * map them to the corresponding PPP protocol types.
3733 * Also check for Van Jacobson-compressed IP.
3734 * XXX - do this for other forms of PPP?
3736 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3737 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3739 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3744 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3745 ethertype_to_ppptype(cstate
, ll_proto
));
3755 return (gen_loopback_linktype(cstate
, AF_INET
));
3757 case ETHERTYPE_IPV6
:
3759 * AF_ values may, unfortunately, be platform-
3760 * dependent; AF_INET isn't, because everybody
3761 * used 4.2BSD's value, but AF_INET6 is, because
3762 * 4.2BSD didn't have a value for it (given that
3763 * IPv6 didn't exist back in the early 1980's),
3764 * and they all picked their own values.
3766 * This means that, if we're reading from a
3767 * savefile, we need to check for all the
3770 * If we're doing a live capture, we only need
3771 * to check for this platform's value; however,
3772 * Npcap uses 24, which isn't Windows's AF_INET6
3773 * value. (Given the multiple different values,
3774 * programs that read pcap files shouldn't be
3775 * checking for their platform's AF_INET6 value
3776 * anyway, they should check for all of the
3777 * possible values. and they might as well do
3778 * that even for live captures.)
3780 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3782 * Savefile - check for all three
3783 * possible IPv6 values.
3785 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3786 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3788 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3793 * Live capture, so we only need to
3794 * check for the value used on this
3799 * Npcap doesn't use Windows's AF_INET6,
3800 * as that collides with AF_IPX on
3801 * some BSDs (both have the value 23).
3802 * Instead, it uses 24.
3804 return (gen_loopback_linktype(cstate
, 24));
3807 return (gen_loopback_linktype(cstate
, AF_INET6
));
3808 #else /* AF_INET6 */
3810 * I guess this platform doesn't support
3811 * IPv6, so we just reject all packets.
3813 return gen_false(cstate
);
3814 #endif /* AF_INET6 */
3820 * Not a type on which we support filtering.
3821 * XXX - support those that have AF_ values
3822 * #defined on this platform, at least?
3824 return gen_false(cstate
);
3829 * af field is host byte order in contrast to the rest of
3832 if (ll_proto
== ETHERTYPE_IP
)
3833 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3835 else if (ll_proto
== ETHERTYPE_IPV6
)
3836 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3839 return gen_false(cstate
);
3843 case DLT_ARCNET_LINUX
:
3845 * XXX should we check for first fragment if the protocol
3851 return gen_false(cstate
);
3853 case ETHERTYPE_IPV6
:
3854 return (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
,
3866 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3868 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3873 case ETHERTYPE_REVARP
:
3874 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3877 case ETHERTYPE_ATALK
:
3878 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3885 case ETHERTYPE_ATALK
:
3886 return gen_true(cstate
);
3888 return gen_false(cstate
);
3894 * XXX - assumes a 2-byte Frame Relay header with
3895 * DLCI and flags. What if the address is longer?
3901 * Check for the special NLPID for IP.
3903 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3905 case ETHERTYPE_IPV6
:
3907 * Check for the special NLPID for IPv6.
3909 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3913 * Check for several OSI protocols.
3915 * Frame Relay packets typically have an OSI
3916 * NLPID at the beginning; we check for each
3919 * What we check for is the NLPID and a frame
3920 * control field of UI, i.e. 0x03 followed
3923 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3924 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3925 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3931 return gen_false(cstate
);
3936 break; // not implemented
3938 case DLT_JUNIPER_MFR
:
3939 case DLT_JUNIPER_MLFR
:
3940 case DLT_JUNIPER_MLPPP
:
3941 case DLT_JUNIPER_ATM1
:
3942 case DLT_JUNIPER_ATM2
:
3943 case DLT_JUNIPER_PPPOE
:
3944 case DLT_JUNIPER_PPPOE_ATM
:
3945 case DLT_JUNIPER_GGSN
:
3946 case DLT_JUNIPER_ES
:
3947 case DLT_JUNIPER_MONITOR
:
3948 case DLT_JUNIPER_SERVICES
:
3949 case DLT_JUNIPER_ETHER
:
3950 case DLT_JUNIPER_PPP
:
3951 case DLT_JUNIPER_FRELAY
:
3952 case DLT_JUNIPER_CHDLC
:
3953 case DLT_JUNIPER_VP
:
3954 case DLT_JUNIPER_ST
:
3955 case DLT_JUNIPER_ISM
:
3956 case DLT_JUNIPER_VS
:
3957 case DLT_JUNIPER_SRX_E2E
:
3958 case DLT_JUNIPER_FIBRECHANNEL
:
3959 case DLT_JUNIPER_ATM_CEMIC
:
3961 /* just lets verify the magic number for now -
3962 * on ATM we may have up to 6 different encapsulations on the wire
3963 * and need a lot of heuristics to figure out that the payload
3966 * FIXME encapsulation specific BPF_ filters
3968 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3970 case DLT_BACNET_MS_TP
:
3971 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3974 return gen_ipnet_linktype(cstate
, ll_proto
);
3976 case DLT_LINUX_IRDA
:
3979 case DLT_MTP2_WITH_PHDR
:
3982 case DLT_LINUX_LAPD
:
3983 case DLT_USB_FREEBSD
:
3985 case DLT_USB_LINUX_MMAPPED
:
3987 case DLT_BLUETOOTH_HCI_H4
:
3988 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3990 case DLT_CAN_SOCKETCAN
:
3991 case DLT_IEEE802_15_4
:
3992 case DLT_IEEE802_15_4_LINUX
:
3993 case DLT_IEEE802_15_4_NONASK_PHY
:
3994 case DLT_IEEE802_15_4_NOFCS
:
3995 case DLT_IEEE802_15_4_TAP
:
3996 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3999 case DLT_IPMB_KONTRON
:
4003 /* Using the fixed-size NFLOG header it is possible to tell only
4004 * the address family of the packet, other meaningful data is
4005 * either missing or behind TLVs.
4007 break; // not implemented
4011 * Does this link-layer header type have a field
4012 * indicating the type of the next protocol? If
4013 * so, off_linktype.constant_part will be the offset of that
4014 * field in the packet; if not, it will be OFFSET_NOT_SET.
4016 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
4018 * Yes; assume it's an Ethernet type. (If
4019 * it's not, it needs to be handled specially
4022 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4023 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
4027 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
4028 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
4032 * Check for an LLC SNAP packet with a given organization code and
4033 * protocol type; we check the entire contents of the 802.2 LLC and
4034 * snap headers, checking for DSAP and SSAP of SNAP and a control
4035 * field of 0x03 in the LLC header, and for the specified organization
4036 * code and protocol type in the SNAP header.
4038 static struct block
*
4039 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
4041 u_char snapblock
[8];
4043 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
4044 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
4045 snapblock
[2] = 0x03; /* control = UI */
4046 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
4047 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
4048 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
4049 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
4050 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
4051 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
4055 * Generate code to match frames with an LLC header.
4057 static struct block
*
4058 gen_llc_internal(compiler_state_t
*cstate
)
4060 struct block
*b0
, *b1
;
4062 switch (cstate
->linktype
) {
4066 * We check for an Ethernet type field less or equal than
4067 * 1500, which means it's an 802.3 length field.
4069 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
4072 * Now check for the purported DSAP and SSAP not being
4073 * 0xFF, to rule out NetWare-over-802.3.
4075 b1
= gen_cmp_ne(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
4081 * We check for LLC traffic.
4083 return gen_atmtype_llc(cstate
);
4085 case DLT_IEEE802
: /* Token Ring */
4087 * XXX - check for LLC frames.
4089 return gen_true(cstate
);
4093 * XXX - check for LLC frames.
4095 return gen_true(cstate
);
4097 case DLT_ATM_RFC1483
:
4099 * For LLC encapsulation, these are defined to have an
4102 * For VC encapsulation, they don't, but there's no
4103 * way to check for that; the protocol used on the VC
4104 * is negotiated out of band.
4106 return gen_true(cstate
);
4108 case DLT_IEEE802_11
:
4109 case DLT_PRISM_HEADER
:
4110 case DLT_IEEE802_11_RADIO
:
4111 case DLT_IEEE802_11_RADIO_AVS
:
4114 * Check that we have a data frame.
4116 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4117 IEEE80211_FC0_TYPE_DATA
,
4118 IEEE80211_FC0_TYPE_MASK
);
4121 fail_kw_on_dlt(cstate
, "llc");
4127 gen_llc(compiler_state_t
*cstate
)
4130 * Catch errors reported by us and routines below us, and return NULL
4133 if (setjmp(cstate
->top_ctx
))
4136 return gen_llc_internal(cstate
);
4140 gen_llc_i(compiler_state_t
*cstate
)
4142 struct block
*b0
, *b1
;
4146 * Catch errors reported by us and routines below us, and return NULL
4149 if (setjmp(cstate
->top_ctx
))
4153 * Check whether this is an LLC frame.
4155 b0
= gen_llc_internal(cstate
);
4158 * Load the control byte and test the low-order bit; it must
4159 * be clear for I frames.
4161 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4162 b1
= gen_unset(cstate
, 0x01, s
);
4169 gen_llc_s(compiler_state_t
*cstate
)
4171 struct block
*b0
, *b1
;
4174 * Catch errors reported by us and routines below us, and return NULL
4177 if (setjmp(cstate
->top_ctx
))
4181 * Check whether this is an LLC frame.
4183 b0
= gen_llc_internal(cstate
);
4186 * Now compare the low-order 2 bit of the control byte against
4187 * the appropriate value for S frames.
4189 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4195 gen_llc_u(compiler_state_t
*cstate
)
4197 struct block
*b0
, *b1
;
4200 * Catch errors reported by us and routines below us, and return NULL
4203 if (setjmp(cstate
->top_ctx
))
4207 * Check whether this is an LLC frame.
4209 b0
= gen_llc_internal(cstate
);
4212 * Now compare the low-order 2 bit of the control byte against
4213 * the appropriate value for U frames.
4215 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4221 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4223 struct block
*b0
, *b1
;
4226 * Catch errors reported by us and routines below us, and return NULL
4229 if (setjmp(cstate
->top_ctx
))
4233 * Check whether this is an LLC frame.
4235 b0
= gen_llc_internal(cstate
);
4238 * Now check for an S frame with the appropriate type.
4240 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4246 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4248 struct block
*b0
, *b1
;
4251 * Catch errors reported by us and routines below us, and return NULL
4254 if (setjmp(cstate
->top_ctx
))
4258 * Check whether this is an LLC frame.
4260 b0
= gen_llc_internal(cstate
);
4263 * Now check for a U frame with the appropriate type.
4265 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4271 * Generate code to match a particular packet type, for link-layer types
4272 * using 802.2 LLC headers.
4274 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4275 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4277 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4278 * value, if <= ETHERMTU. We use that to determine whether to
4279 * match the DSAP or both DSAP and LSAP or to check the OUI and
4280 * protocol ID in a SNAP header.
4282 static struct block
*
4283 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4286 * XXX - handle token-ring variable-length header.
4292 case LLCSAP_NETBEUI
:
4294 * XXX - should we check both the DSAP and the
4295 * SSAP, like this, or should we check just the
4296 * DSAP, as we do for other SAP values?
4298 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4299 ((ll_proto
<< 8) | ll_proto
));
4303 * XXX - are there ever SNAP frames for IPX on
4304 * non-Ethernet 802.x networks?
4306 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4308 case ETHERTYPE_ATALK
:
4310 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4311 * SNAP packets with an organization code of
4312 * 0x080007 (Apple, for Appletalk) and a protocol
4313 * type of ETHERTYPE_ATALK (Appletalk).
4315 * XXX - check for an organization code of
4316 * encapsulated Ethernet as well?
4318 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4322 * XXX - we don't have to check for IPX 802.3
4323 * here, but should we check for the IPX Ethertype?
4325 if (ll_proto
<= ETHERMTU
) {
4326 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
4328 * This is an LLC SAP value, so check
4331 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4333 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4335 * This is an Ethernet type; we assume that it's
4336 * unlikely that it'll appear in the right place
4337 * at random, and therefore check only the
4338 * location that would hold the Ethernet type
4339 * in a SNAP frame with an organization code of
4340 * 0x000000 (encapsulated Ethernet).
4342 * XXX - if we were to check for the SNAP DSAP and
4343 * LSAP, as per XXX, and were also to check for an
4344 * organization code of 0x000000 (encapsulated
4345 * Ethernet), we'd do
4347 * return gen_snap(cstate, 0x000000, ll_proto);
4349 * here; for now, we don't, as per the above.
4350 * I don't know whether it's worth the extra CPU
4351 * time to do the right check or not.
4353 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4358 static struct block
*
4359 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4360 int dir
, u_int src_off
, u_int dst_off
)
4362 struct block
*b0
, *b1
;
4376 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4377 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4383 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4384 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4394 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4401 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4404 static struct block
*
4405 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4406 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4408 struct block
*b0
, *b1
;
4411 * Code below needs to access four separate 32-bit parts of the 128-bit
4412 * IPv6 address and mask. In some OSes this is as simple as using the
4413 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4414 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4415 * far as libpcap sees it. Hence copy the data before use to avoid
4416 * potential unaligned memory access and the associated compiler
4417 * warnings (whether genuine or not).
4419 bpf_u_int32 a
[4], m
[4];
4432 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4433 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4439 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4440 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4450 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4457 /* this order is important */
4458 memcpy(a
, addr
, sizeof(a
));
4459 memcpy(m
, mask
, sizeof(m
));
4461 for (int i
= 3; i
>= 0; i
--) {
4462 // Same as the Q_IP case in gen_host().
4463 if (m
[i
] == 0 && a
[i
] == 0)
4465 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4 * i
, BPF_W
,
4466 ntohl(a
[i
]), ntohl(m
[i
]));
4472 return b1
? b1
: gen_true(cstate
);
4476 * Like gen_mac48host(), but for DLT_IEEE802_11 (802.11 wireless LAN) and
4477 * various 802.11 + radio headers.
4479 static struct block
*
4480 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4482 register struct block
*b0
, *b1
, *b2
;
4483 register struct slist
*s
;
4485 #ifdef ENABLE_WLAN_FILTERING_PATCH
4488 * We need to disable the optimizer because the optimizer is buggy
4489 * and wipes out some LD instructions generated by the below
4490 * code to validate the Frame Control bits
4492 cstate
->no_optimize
= 1;
4493 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4500 * For control frames, there is no SA.
4502 * For management frames, SA is at an
4503 * offset of 10 from the beginning of
4506 * For data frames, SA is at an offset
4507 * of 10 from the beginning of the packet
4508 * if From DS is clear, at an offset of
4509 * 16 from the beginning of the packet
4510 * if From DS is set and To DS is clear,
4511 * and an offset of 24 from the beginning
4512 * of the packet if From DS is set and To DS
4517 * Generate the tests to be done for data frames
4520 * First, check for To DS set, i.e. check "link[1] & 0x01".
4522 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4523 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4526 * If To DS is set, the SA is at 24.
4528 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4532 * Now, check for To DS not set, i.e. check
4533 * "!(link[1] & 0x01)".
4535 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4536 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4539 * If To DS is not set, the SA is at 16.
4541 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4545 * Now OR together the last two checks. That gives
4546 * the complete set of checks for data frames with
4552 * Now check for From DS being set, and AND that with
4553 * the ORed-together checks.
4555 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4556 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4560 * Now check for data frames with From DS not set.
4562 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4563 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4566 * If From DS isn't set, the SA is at 10.
4568 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4572 * Now OR together the checks for data frames with
4573 * From DS not set and for data frames with From DS
4574 * set; that gives the checks done for data frames.
4579 * Now check for a data frame.
4580 * I.e, check "link[0] & 0x08".
4582 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4583 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4586 * AND that with the checks done for data frames.
4591 * If the high-order bit of the type value is 0, this
4592 * is a management frame.
4593 * I.e, check "!(link[0] & 0x08)".
4595 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4596 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4599 * For management frames, the SA is at 10.
4601 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4605 * OR that with the checks done for data frames.
4606 * That gives the checks done for management and
4612 * If the low-order bit of the type value is 1,
4613 * this is either a control frame or a frame
4614 * with a reserved type, and thus not a
4617 * I.e., check "!(link[0] & 0x04)".
4619 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4620 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4623 * AND that with the checks for data and management
4633 * For control frames, there is no DA.
4635 * For management frames, DA is at an
4636 * offset of 4 from the beginning of
4639 * For data frames, DA is at an offset
4640 * of 4 from the beginning of the packet
4641 * if To DS is clear and at an offset of
4642 * 16 from the beginning of the packet
4647 * Generate the tests to be done for data frames.
4649 * First, check for To DS set, i.e. "link[1] & 0x01".
4651 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4652 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4655 * If To DS is set, the DA is at 16.
4657 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4661 * Now, check for To DS not set, i.e. check
4662 * "!(link[1] & 0x01)".
4664 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4665 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4668 * If To DS is not set, the DA is at 4.
4670 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4674 * Now OR together the last two checks. That gives
4675 * the complete set of checks for data frames.
4680 * Now check for a data frame.
4681 * I.e, check "link[0] & 0x08".
4683 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4684 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4687 * AND that with the checks done for data frames.
4692 * If the high-order bit of the type value is 0, this
4693 * is a management frame.
4694 * I.e, check "!(link[0] & 0x08)".
4696 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4697 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4700 * For management frames, the DA is at 4.
4702 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4706 * OR that with the checks done for data frames.
4707 * That gives the checks done for management and
4713 * If the low-order bit of the type value is 1,
4714 * this is either a control frame or a frame
4715 * with a reserved type, and thus not a
4718 * I.e., check "!(link[0] & 0x04)".
4720 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4721 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4724 * AND that with the checks for data and management
4731 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4732 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4738 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4739 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4744 * XXX - add BSSID keyword?
4747 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4751 * Not present in CTS or ACK control frames.
4753 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4754 IEEE80211_FC0_TYPE_MASK
);
4755 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4756 IEEE80211_FC0_SUBTYPE_MASK
);
4757 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4758 IEEE80211_FC0_SUBTYPE_MASK
);
4761 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4767 * Not present in control frames.
4769 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4770 IEEE80211_FC0_TYPE_MASK
);
4771 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4777 * Present only if the direction mask has both "From DS"
4778 * and "To DS" set. Neither control frames nor management
4779 * frames should have both of those set, so we don't
4780 * check the frame type.
4782 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4783 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4784 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4790 * Not present in management frames; addr1 in other
4795 * If the high-order bit of the type value is 0, this
4796 * is a management frame.
4797 * I.e, check "(link[0] & 0x08)".
4799 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4800 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4805 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4808 * AND that with the check of addr1.
4815 * Not present in management frames; addr2, if present,
4820 * Not present in CTS or ACK control frames.
4822 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4823 IEEE80211_FC0_TYPE_MASK
);
4824 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4825 IEEE80211_FC0_SUBTYPE_MASK
);
4826 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4827 IEEE80211_FC0_SUBTYPE_MASK
);
4832 * If the high-order bit of the type value is 0, this
4833 * is a management frame.
4834 * I.e, check "(link[0] & 0x08)".
4836 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4837 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4840 * AND that with the check for frames other than
4841 * CTS and ACK frames.
4848 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4857 * This is quite tricky because there may be pad bytes in front of the
4858 * DECNET header, and then there are two possible data packet formats that
4859 * carry both src and dst addresses, plus 5 packet types in a format that
4860 * carries only the src node, plus 2 types that use a different format and
4861 * also carry just the src node.
4865 * Instead of doing those all right, we just look for data packets with
4866 * 0 or 1 bytes of padding. If you want to look at other packets, that
4867 * will require a lot more hacking.
4869 * To add support for filtering on DECNET "areas" (network numbers)
4870 * one would want to add a "mask" argument to this routine. That would
4871 * make the filter even more inefficient, although one could be clever
4872 * and not generate masking instructions if the mask is 0xFFFF.
4874 static struct block
*
4875 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4877 struct block
*b0
, *b1
, *b2
, *tmp
;
4878 u_int offset_lh
; /* offset if long header is received */
4879 u_int offset_sh
; /* offset if short header is received */
4884 offset_sh
= 1; /* follows flags */
4885 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4889 offset_sh
= 3; /* follows flags, dstnode */
4890 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
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
);
4902 /* Inefficient because we do our Calvinball dance twice */
4903 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4904 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4914 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4922 * In a DECnet message inside an Ethernet frame the first two bytes
4923 * immediately after EtherType are the [litle-endian] DECnet message
4924 * length, which is irrelevant in this context.
4926 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
4927 * 8-bit bitmap of the optional padding before the packet route header.
4928 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
4929 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
4930 * means there aren't any PAD bytes after the bitmap, so the header
4931 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
4932 * is set to 0, thus the header begins at the third byte.
4934 * The header can be in several (as mentioned above) formats, all of
4935 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
4936 * (PF, "pad field") set to 0 regardless of any padding present before
4937 * the header. "Short header" means bits 0-2 of the bitmap encode the
4938 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
4940 * To test PLENGTH and FLAGS, use multiple-byte constants with the
4941 * values and the masks, this maps to the required single bytes of
4942 * the message correctly on both big-endian and little-endian hosts.
4943 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
4944 * because the wire encoding is little-endian and BPF multiple-byte
4945 * loads are big-endian. When the destination address is near enough
4946 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
4949 /* Check for pad = 1, long header case */
4950 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
4951 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4952 BPF_H
, SWAPSHORT(addr
));
4954 /* Check for pad = 0, long header case */
4955 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
4956 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4960 /* Check for pad = 1, short header case */
4962 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
4963 0x81020000U
| SWAPSHORT(addr
),
4966 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
4967 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4972 /* Check for pad = 0, short header case */
4974 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
4975 0x02000000U
| SWAPSHORT(addr
) << 8,
4978 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
4979 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4989 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4990 * test the bottom-of-stack bit, and then check the version number
4991 * field in the IP header.
4993 static struct block
*
4994 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4996 struct block
*b0
, *b1
;
5001 /* match the bottom-of-stack bit */
5002 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5003 /* match the IPv4 version number */
5004 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5008 case ETHERTYPE_IPV6
:
5009 /* match the bottom-of-stack bit */
5010 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5011 /* match the IPv6 version number */
5012 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5017 /* FIXME add other L3 proto IDs */
5018 bpf_error(cstate
, "unsupported protocol over mpls");
5023 static struct block
*
5024 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5025 int proto
, int dir
, int type
)
5027 struct block
*b0
, *b1
;
5032 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5034 * Only check for non-IPv4 addresses if we're not
5035 * checking MPLS-encapsulated packets.
5037 if (cstate
->label_stack_depth
== 0) {
5038 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5040 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5046 // "link net NETNAME" and variations thereof
5047 break; // invalid qualifier
5050 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5052 * Belt and braces: if other code works correctly, any host
5053 * bits are clear and mask == 0 means addr == 0. In this case
5054 * the call to gen_hostop() would produce an "always true"
5055 * instruction block and ANDing it with the link type check
5058 if (mask
== 0 && addr
== 0)
5060 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5065 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5066 // Same as for Q_IP above.
5067 if (mask
== 0 && addr
== 0)
5069 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5074 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5075 // Same as for Q_IP above.
5076 if (mask
== 0 && addr
== 0)
5078 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5089 break; // invalid qualifier
5092 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5093 b1
= gen_dnhostop(cstate
, addr
, dir
);
5124 break; // invalid qualifier
5129 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5130 type
== Q_NET
? "ip net" : "ip host");
5134 static struct block
*
5135 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5136 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5138 struct block
*b0
, *b1
;
5144 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5145 // Same as the Q_IP case in gen_host().
5147 ! memcmp(mask
, &in6addr_any
, sizeof(struct in6_addr
)) &&
5148 ! memcmp(addr
, &in6addr_any
, sizeof(struct in6_addr
))
5151 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5193 break; // invalid qualifier
5198 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5199 type
== Q_NET
? "ip6 net" : "ip6 host");
5203 static unsigned char
5204 is_mac48_linktype(const int linktype
)
5210 case DLT_IEEE802_11
:
5211 case DLT_IEEE802_11_RADIO
:
5212 case DLT_IEEE802_11_RADIO_AVS
:
5213 case DLT_IP_OVER_FC
:
5214 case DLT_NETANALYZER
:
5215 case DLT_NETANALYZER_TRANSPARENT
:
5217 case DLT_PRISM_HEADER
:
5224 static struct block
*
5225 gen_mac48host(compiler_state_t
*cstate
, const u_char
*eaddr
, const u_char dir
,
5226 const char *keyword
)
5228 struct block
*b1
= NULL
;
5229 u_int src_off
, dst_off
;
5231 switch (cstate
->linktype
) {
5233 case DLT_NETANALYZER
:
5234 case DLT_NETANALYZER_TRANSPARENT
:
5235 b1
= gen_prevlinkhdr_check(cstate
);
5240 src_off
= 6 + 1 + cstate
->pcap_fddipad
;
5241 dst_off
= 0 + 1 + cstate
->pcap_fddipad
;
5247 case DLT_IEEE802_11
:
5248 case DLT_PRISM_HEADER
:
5249 case DLT_IEEE802_11_RADIO_AVS
:
5250 case DLT_IEEE802_11_RADIO
:
5252 return gen_wlanhostop(cstate
, eaddr
, dir
);
5253 case DLT_IP_OVER_FC
:
5255 * Assume that the addresses are IEEE 48-bit MAC addresses,
5256 * as RFC 2625 states.
5263 * This is LLC-multiplexed traffic; if it were
5264 * LANE, cstate->linktype would have been set to
5269 fail_kw_on_dlt(cstate
, keyword
);
5272 struct block
*b0
, *tmp
;
5276 b0
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5279 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5282 tmp
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5283 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5288 tmp
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5289 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5293 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5302 * This primitive is non-directional by design, so the grammar does not allow
5303 * to qualify it with a direction.
5305 static struct block
*
5306 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5307 struct addrinfo
*alist
, int proto
)
5309 struct block
*b0
, *b1
, *tmp
;
5310 struct addrinfo
*ai
;
5311 struct sockaddr_in
*sin
;
5318 b0
= gen_mac48host(cstate
, eaddr
, Q_OR
, "gateway");
5320 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5322 * Does it have an address?
5324 if (ai
->ai_addr
!= NULL
) {
5326 * Yes. Is it an IPv4 address?
5328 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5330 * Generate an entry for it.
5332 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5333 tmp
= gen_host(cstate
,
5334 ntohl(sin
->sin_addr
.s_addr
),
5335 0xffffffff, proto
, Q_OR
, Q_HOST
);
5337 * Is it the *first* IPv4 address?
5341 * Yes, so start with it.
5346 * No, so OR it into the
5358 * No IPv4 addresses found.
5366 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5370 static struct block
*
5371 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5379 return gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
);
5382 return gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
);
5385 return gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
);
5388 return gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
);
5390 #ifndef IPPROTO_IGMP
5391 #define IPPROTO_IGMP 2
5395 return gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
);
5397 #ifndef IPPROTO_IGRP
5398 #define IPPROTO_IGRP 9
5401 return gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
);
5404 #define IPPROTO_PIM 103
5408 return gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
);
5410 #ifndef IPPROTO_VRRP
5411 #define IPPROTO_VRRP 112
5415 return gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
);
5417 #ifndef IPPROTO_CARP
5418 #define IPPROTO_CARP 112
5422 return gen_proto(cstate
, IPPROTO_CARP
, Q_IP
);
5425 return gen_linktype(cstate
, ETHERTYPE_IP
);
5428 return gen_linktype(cstate
, ETHERTYPE_ARP
);
5431 return gen_linktype(cstate
, ETHERTYPE_REVARP
);
5434 break; // invalid syntax
5437 return gen_linktype(cstate
, ETHERTYPE_ATALK
);
5440 return gen_linktype(cstate
, ETHERTYPE_AARP
);
5443 return gen_linktype(cstate
, ETHERTYPE_DN
);
5446 return gen_linktype(cstate
, ETHERTYPE_SCA
);
5449 return gen_linktype(cstate
, ETHERTYPE_LAT
);
5452 return gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5455 return gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5458 return gen_linktype(cstate
, ETHERTYPE_IPV6
);
5460 #ifndef IPPROTO_ICMPV6
5461 #define IPPROTO_ICMPV6 58
5464 return gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
);
5467 #define IPPROTO_AH 51
5470 return gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
);
5473 #define IPPROTO_ESP 50
5476 return gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
);
5479 return gen_linktype(cstate
, LLCSAP_ISONS
);
5482 return gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
);
5485 return gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
5487 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5488 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5489 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5491 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5493 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5495 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5499 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5500 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5501 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5503 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5505 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5507 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5511 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5512 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5513 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5515 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
);
5520 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5521 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5526 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5527 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5529 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5531 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5536 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5537 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5542 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5543 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5548 return gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
);
5551 return gen_linktype(cstate
, LLCSAP_8021D
);
5554 return gen_linktype(cstate
, LLCSAP_IPX
);
5557 return gen_linktype(cstate
, LLCSAP_NETBEUI
);
5560 break; // invalid syntax
5565 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5569 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5572 * Catch errors reported by us and routines below us, and return NULL
5575 if (setjmp(cstate
->top_ctx
))
5578 return gen_proto_abbrev_internal(cstate
, proto
);
5581 static struct block
*
5582 gen_ip_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5584 return gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5587 static struct block
*
5588 gen_ip6_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5590 return gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5593 static struct block
*
5594 gen_ipfrag(compiler_state_t
*cstate
)
5598 /* not IPv4 frag other than the first frag */
5599 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5600 return gen_unset(cstate
, 0x1fff, s
);
5604 * Generate a comparison to a port value in the transport-layer header
5605 * at the specified offset from the beginning of that header.
5607 * XXX - this handles a variable-length prefix preceding the link-layer
5608 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5609 * variable-length link-layer headers (such as Token Ring or 802.11
5612 static struct block
*
5613 gen_portatom(compiler_state_t
*cstate
, int off
, uint16_t v
)
5615 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5618 static struct block
*
5619 gen_portatom6(compiler_state_t
*cstate
, int off
, uint16_t v
)
5621 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5624 static struct block
*
5625 gen_port(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5627 struct block
*b1
, *tmp
;
5631 b1
= gen_portatom(cstate
, 0, port
);
5635 b1
= gen_portatom(cstate
, 2, port
);
5639 tmp
= gen_portatom(cstate
, 0, port
);
5640 b1
= gen_portatom(cstate
, 2, port
);
5646 tmp
= gen_portatom(cstate
, 0, port
);
5647 b1
= gen_portatom(cstate
, 2, port
);
5657 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5665 return gen_port_common(cstate
, proto
, b1
);
5668 static struct block
*
5669 gen_port_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5671 struct block
*b0
, *tmp
;
5676 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5677 * not LLC encapsulation with LLCSAP_IP.
5679 * For IEEE 802 networks - which includes 802.5 token ring
5680 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5681 * says that SNAP encapsulation is used, not LLC encapsulation
5684 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5685 * RFC 2225 say that SNAP encapsulation is used, not LLC
5686 * encapsulation with LLCSAP_IP.
5688 * So we always check for ETHERTYPE_IP.
5690 * At the time of this writing all three L4 protocols the "port" and
5691 * "portrange" primitives support (TCP, UDP and SCTP) have the source
5692 * and the destination ports identically encoded in the transport
5693 * protocol header. So without a proto qualifier the only difference
5694 * between the implemented cases is the protocol number and all other
5695 * checks need to be made exactly once.
5697 * If the expression syntax in future starts to support ports for
5698 * another L4 protocol that has unsigned integer ports encoded using a
5699 * different size and/or offset, this will require a different code.
5705 tmp
= gen_ip_proto(cstate
, (uint8_t)proto
);
5709 tmp
= gen_ip_proto(cstate
, IPPROTO_UDP
);
5710 gen_or(gen_ip_proto(cstate
, IPPROTO_TCP
), tmp
);
5711 gen_or(gen_ip_proto(cstate
, IPPROTO_SCTP
), tmp
);
5717 // Not a fragment other than the first fragment.
5718 b0
= gen_ipfrag(cstate
);
5722 gen_and(gen_linktype(cstate
, ETHERTYPE_IP
), b1
);
5726 static struct block
*
5727 gen_port6(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5729 struct block
*b1
, *tmp
;
5733 b1
= gen_portatom6(cstate
, 0, port
);
5737 b1
= gen_portatom6(cstate
, 2, port
);
5741 tmp
= gen_portatom6(cstate
, 0, port
);
5742 b1
= gen_portatom6(cstate
, 2, port
);
5748 tmp
= gen_portatom6(cstate
, 0, port
);
5749 b1
= gen_portatom6(cstate
, 2, port
);
5757 return gen_port6_common(cstate
, proto
, b1
);
5760 static struct block
*
5761 gen_port6_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5765 // "ip6 proto 'ip_proto'"
5770 tmp
= gen_ip6_proto(cstate
, (uint8_t)proto
);
5774 // Same as in gen_port_common().
5775 tmp
= gen_ip6_proto(cstate
, IPPROTO_UDP
);
5776 gen_or(gen_ip6_proto(cstate
, IPPROTO_TCP
), tmp
);
5777 gen_or(gen_ip6_proto(cstate
, IPPROTO_SCTP
), tmp
);
5783 // XXX - catch the first fragment of a fragmented packet?
5785 // "link proto \ip6"
5786 gen_and(gen_linktype(cstate
, ETHERTYPE_IPV6
), b1
);
5790 /* gen_portrange code */
5791 static struct block
*
5792 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5796 return gen_portatom(cstate
, off
, v1
);
5798 struct block
*b1
, *b2
;
5800 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, min(v1
, v2
));
5801 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, max(v1
, v2
));
5808 static struct block
*
5809 gen_portrange(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5812 struct block
*b1
, *tmp
;
5816 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5820 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5824 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5825 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5831 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5832 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5842 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5850 return gen_port_common(cstate
, proto
, b1
);
5853 static struct block
*
5854 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5858 return gen_portatom6(cstate
, off
, v1
);
5860 struct block
*b1
, *b2
;
5862 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, min(v1
, v2
));
5863 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, max(v1
, v2
));
5870 static struct block
*
5871 gen_portrange6(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5874 struct block
*b1
, *tmp
;
5878 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5882 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5886 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5887 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5893 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5894 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5902 return gen_port6_common(cstate
, proto
, b1
);
5906 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5915 v
= pcap_nametoproto(name
);
5916 if (v
== PROTO_UNDEF
)
5917 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5921 /* XXX should look up h/w protocol type based on cstate->linktype */
5922 v
= pcap_nametoeproto(name
);
5923 if (v
== PROTO_UNDEF
) {
5924 v
= pcap_nametollc(name
);
5925 if (v
== PROTO_UNDEF
)
5926 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5931 if (strcmp(name
, "esis") == 0)
5933 else if (strcmp(name
, "isis") == 0)
5935 else if (strcmp(name
, "clnp") == 0)
5938 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5948 #if !defined(NO_PROTOCHAIN)
5950 * This primitive is non-directional by design, so the grammar does not allow
5951 * to qualify it with a direction.
5953 static struct block
*
5954 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
5956 struct block
*b0
, *b
;
5957 struct slist
*s
[100];
5958 int fix2
, fix3
, fix4
, fix5
;
5959 int ahcheck
, again
, end
;
5961 int reg2
= alloc_reg(cstate
);
5963 memset(s
, 0, sizeof(s
));
5964 fix3
= fix4
= fix5
= 0;
5969 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
5972 b0
= gen_protochain(cstate
, v
, Q_IP
);
5973 b
= gen_protochain(cstate
, v
, Q_IPV6
);
5977 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
5982 * We don't handle variable-length prefixes before the link-layer
5983 * header, or variable-length link-layer headers, here yet.
5984 * We might want to add BPF instructions to do the protochain
5985 * work, to simplify that and, on platforms that have a BPF
5986 * interpreter with the new instructions, let the filtering
5987 * be done in the kernel. (We already require a modified BPF
5988 * engine to do the protochain stuff, to support backward
5989 * branches, and backward branch support is unlikely to appear
5990 * in kernel BPF engines.)
5992 if (cstate
->off_linkpl
.is_variable
)
5993 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5996 * To quote a comment in optimize.c:
5998 * "These data structures are used in a Cocke and Schwartz style
5999 * value numbering scheme. Since the flowgraph is acyclic,
6000 * exit values can be propagated from a node's predecessors
6001 * provided it is uniquely defined."
6003 * "Acyclic" means "no backward branches", which means "no
6004 * loops", so we have to turn the optimizer off.
6006 cstate
->no_optimize
= 1;
6009 * s[0] is a dummy entry to protect other BPF insn from damage
6010 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6011 * hard to find interdependency made by jump table fixup.
6014 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6019 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6022 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6023 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6025 /* X = ip->ip_hl << 2 */
6026 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6027 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6032 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6034 /* A = ip6->ip_nxt */
6035 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6036 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6038 /* X = sizeof(struct ip6_hdr) */
6039 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6045 bpf_error(cstate
, "unsupported proto to gen_protochain");
6049 /* again: if (A == v) goto end; else fall through; */
6051 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6053 s
[i
]->s
.jt
= NULL
; /*later*/
6054 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6058 #ifndef IPPROTO_NONE
6059 #define IPPROTO_NONE 59
6061 /* if (A == IPPROTO_NONE) goto end */
6062 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6063 s
[i
]->s
.jt
= NULL
; /*later*/
6064 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6065 s
[i
]->s
.k
= IPPROTO_NONE
;
6066 s
[fix5
]->s
.jf
= s
[i
];
6070 if (proto
== Q_IPV6
) {
6071 int v6start
, v6end
, v6advance
, j
;
6074 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6075 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6076 s
[i
]->s
.jt
= NULL
; /*later*/
6077 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6078 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6079 s
[fix2
]->s
.jf
= s
[i
];
6081 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6082 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6083 s
[i
]->s
.jt
= NULL
; /*later*/
6084 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6085 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6087 /* if (A == IPPROTO_ROUTING) goto v6advance */
6088 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6089 s
[i
]->s
.jt
= NULL
; /*later*/
6090 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6091 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6093 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6094 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6095 s
[i
]->s
.jt
= NULL
; /*later*/
6096 s
[i
]->s
.jf
= NULL
; /*later*/
6097 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6107 * A = P[X + packet head];
6108 * X = X + (P[X + packet head + 1] + 1) * 8;
6110 /* A = P[X + packet head] */
6111 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6112 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6115 s
[i
] = new_stmt(cstate
, BPF_ST
);
6118 /* A = P[X + packet head + 1]; */
6119 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6120 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6123 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6127 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6131 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6135 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6138 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6142 /* goto again; (must use BPF_JA for backward jump) */
6143 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6144 s
[i
]->s
.k
= again
- i
- 1;
6145 s
[i
- 1]->s
.jf
= s
[i
];
6149 for (j
= v6start
; j
<= v6end
; j
++)
6150 s
[j
]->s
.jt
= s
[v6advance
];
6153 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6155 s
[fix2
]->s
.jf
= s
[i
];
6161 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6162 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6163 s
[i
]->s
.jt
= NULL
; /*later*/
6164 s
[i
]->s
.jf
= NULL
; /*later*/
6165 s
[i
]->s
.k
= IPPROTO_AH
;
6167 s
[fix3
]->s
.jf
= s
[ahcheck
];
6174 * X = X + (P[X + 1] + 2) * 4;
6176 /* A = P[X + packet head]; */
6177 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6178 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6179 s
[i
- 1]->s
.jt
= s
[i
];
6182 s
[i
] = new_stmt(cstate
, BPF_ST
);
6186 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6189 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6193 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6195 /* A = P[X + packet head] */
6196 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6197 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6200 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6204 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6208 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6211 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6215 /* goto again; (must use BPF_JA for backward jump) */
6216 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6217 s
[i
]->s
.k
= again
- i
- 1;
6222 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6224 s
[fix2
]->s
.jt
= s
[end
];
6225 s
[fix4
]->s
.jf
= s
[end
];
6226 s
[fix5
]->s
.jt
= s
[end
];
6233 for (i
= 0; i
< max
- 1; i
++)
6234 s
[i
]->next
= s
[i
+ 1];
6235 s
[max
- 1]->next
= NULL
;
6239 * Remember, s[0] is dummy.
6241 b
= gen_jmp(cstate
, BPF_JEQ
, v
, s
[1]);
6243 free_reg(cstate
, reg2
);
6248 #endif /* !defined(NO_PROTOCHAIN) */
6251 * Generate code that checks whether the packet is a packet for protocol
6252 * <proto> and whether the type field in that protocol's header has
6253 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6254 * IP packet and checks the protocol number in the IP header against <v>.
6256 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6257 * against Q_IP and Q_IPV6.
6259 * This primitive is non-directional by design, so the grammar does not allow
6260 * to qualify it with a direction.
6262 static struct block
*
6263 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6265 struct block
*b0
, *b1
;
6270 b0
= gen_proto(cstate
, v
, Q_IP
);
6271 b1
= gen_proto(cstate
, v
, Q_IPV6
);
6276 return gen_linktype(cstate
, v
);
6279 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6281 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6282 * not LLC encapsulation with LLCSAP_IP.
6284 * For IEEE 802 networks - which includes 802.5 token ring
6285 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6286 * says that SNAP encapsulation is used, not LLC encapsulation
6289 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6290 * RFC 2225 say that SNAP encapsulation is used, not LLC
6291 * encapsulation with LLCSAP_IP.
6293 * So we always check for ETHERTYPE_IP.
6295 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6296 // 0 <= v <= UINT8_MAX
6297 b1
= gen_ip_proto(cstate
, (uint8_t)v
);
6315 break; // invalid qualifier
6318 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6319 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6321 * Also check for a fragment header before the final
6324 b2
= gen_ip6_proto(cstate
, IPPROTO_FRAGMENT
);
6325 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6327 // 0 <= v <= UINT8_MAX
6328 b2
= gen_ip6_proto(cstate
, (uint8_t)v
);
6339 break; // invalid qualifier
6342 assert_maxval(cstate
, "ISO protocol", v
, UINT8_MAX
);
6343 switch (cstate
->linktype
) {
6347 * Frame Relay packets typically have an OSI
6348 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6349 * generates code to check for all the OSI
6350 * NLPIDs, so calling it and then adding a check
6351 * for the particular NLPID for which we're
6352 * looking is bogus, as we can just check for
6355 * What we check for is the NLPID and a frame
6356 * control field value of UI, i.e. 0x03 followed
6359 * XXX - assumes a 2-byte Frame Relay header with
6360 * DLCI and flags. What if the address is longer?
6362 * XXX - what about SNAP-encapsulated frames?
6364 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6370 * Cisco uses an Ethertype lookalike - for OSI,
6373 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6374 /* OSI in C-HDLC is stuffed with a fudge byte */
6375 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6380 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6381 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6387 break; // invalid qualifier
6390 assert_maxval(cstate
, "IS-IS PDU type", v
, ISIS_PDU_TYPE_MAX
);
6391 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
6393 * 4 is the offset of the PDU type relative to the IS-IS
6395 * Except when it is not, see above.
6397 unsigned pdu_type_offset
;
6398 switch (cstate
->linktype
) {
6401 pdu_type_offset
= 5;
6404 pdu_type_offset
= 4;
6406 b1
= gen_mcmp(cstate
, OR_LINKPL_NOSNAP
, pdu_type_offset
, BPF_B
,
6407 v
, ISIS_PDU_TYPE_MAX
);
6424 break; // invalid qualifier
6430 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6435 * Convert a non-numeric name to a port number.
6438 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6440 struct addrinfo hints
, *res
, *ai
;
6442 struct sockaddr_in
*in4
;
6443 struct sockaddr_in6
*in6
;
6447 * We check for both TCP and UDP in case there are
6448 * ambiguous entries.
6450 memset(&hints
, 0, sizeof(hints
));
6451 hints
.ai_family
= PF_UNSPEC
;
6452 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6453 hints
.ai_protocol
= ipproto
;
6454 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6461 * No such port. Just return -1.
6468 * We don't use strerror() because it's not
6469 * guaranteed to be thread-safe on all platforms
6470 * (probably because it might use a non-thread-local
6471 * buffer into which to format an error message
6472 * if the error code isn't one for which it has
6473 * a canned string; three cheers for C string
6476 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6478 port
= -2; /* a real error */
6484 * This is a real error, not just "there's
6485 * no such service name".
6487 * We don't use gai_strerror() because it's not
6488 * guaranteed to be thread-safe on all platforms
6489 * (probably because it might use a non-thread-local
6490 * buffer into which to format an error message
6491 * if the error code isn't one for which it has
6492 * a canned string; three cheers for C string
6495 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6497 port
= -2; /* a real error */
6502 * OK, we found it. Did it find anything?
6504 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6506 * Does it have an address?
6508 if (ai
->ai_addr
!= NULL
) {
6510 * Yes. Get a port number; we're done.
6512 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6513 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6514 port
= ntohs(in4
->sin_port
);
6517 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6518 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6519 port
= ntohs(in6
->sin6_port
);
6530 * Convert a string to a port number.
6533 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6543 * See if it's a number.
6545 ret
= stoulen(string
, string_size
, &val
, cstate
);
6549 /* Unknown port type - it's just a number. */
6550 *proto
= PROTO_UNDEF
;
6553 case STOULEN_NOT_OCTAL_NUMBER
:
6554 case STOULEN_NOT_HEX_NUMBER
:
6555 case STOULEN_NOT_DECIMAL_NUMBER
:
6557 * Not a valid number; try looking it up as a port.
6559 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6560 memcpy(cpy
, string
, string_size
);
6561 cpy
[string_size
] = '\0';
6562 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6563 if (tcp_port
== -2) {
6565 * We got a hard error; the error string has
6569 longjmp(cstate
->top_ctx
, 1);
6572 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6573 if (udp_port
== -2) {
6575 * We got a hard error; the error string has
6579 longjmp(cstate
->top_ctx
, 1);
6584 * We need to check /etc/services for ambiguous entries.
6585 * If we find an ambiguous entry, and it has the
6586 * same port number, change the proto to PROTO_UNDEF
6587 * so both TCP and UDP will be checked.
6589 if (tcp_port
>= 0) {
6590 val
= (bpf_u_int32
)tcp_port
;
6591 *proto
= IPPROTO_TCP
;
6592 if (udp_port
>= 0) {
6593 if (udp_port
== tcp_port
)
6594 *proto
= PROTO_UNDEF
;
6597 /* Can't handle ambiguous names that refer
6598 to different port numbers. */
6599 warning("ambiguous port %s in /etc/services",
6606 if (udp_port
>= 0) {
6607 val
= (bpf_u_int32
)udp_port
;
6608 *proto
= IPPROTO_UDP
;
6612 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6614 longjmp(cstate
->top_ctx
, 1);
6621 /* Error already set. */
6622 longjmp(cstate
->top_ctx
, 1);
6629 /* Should not happen */
6630 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6631 longjmp(cstate
->top_ctx
, 1);
6638 * Convert a string in the form PPP-PPP, which correspond to ports, to
6639 * a starting and ending port in a port range.
6642 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6643 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6646 const char *first
, *second
;
6647 size_t first_size
, second_size
;
6650 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6651 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6654 * Make sure there are no other hyphens.
6656 * XXX - we support named ports, but there are some port names
6657 * in /etc/services that include hyphens, so this would rule
6660 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6661 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6665 * Get the length of the first port.
6668 first_size
= hyphen_off
- string
;
6669 if (first_size
== 0) {
6670 /* Range of "-port", which we don't support. */
6671 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6675 * Try to convert it to a port.
6677 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6678 save_proto
= *proto
;
6681 * Get the length of the second port.
6683 second
= hyphen_off
+ 1;
6684 second_size
= strlen(second
);
6685 if (second_size
== 0) {
6686 /* Range of "port-", which we don't support. */
6687 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6691 * Try to convert it to a port.
6693 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6694 if (*proto
!= save_proto
)
6695 *proto
= PROTO_UNDEF
;
6699 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6701 int proto
= q
.proto
;
6706 bpf_u_int32 mask
, addr
;
6707 struct addrinfo
*res
, *res0
;
6708 struct sockaddr_in
*sin4
;
6710 struct sockaddr_in6
*sin6
;
6711 struct in6_addr mask128
;
6712 struct block
*b
, *tmp
;
6713 int port
, real_proto
;
6714 bpf_u_int32 port1
, port2
;
6717 * Catch errors reported by us and routines below us, and return NULL
6720 if (setjmp(cstate
->top_ctx
))
6726 addr
= pcap_nametonetaddr(name
);
6728 bpf_error(cstate
, "unknown network '%s'", name
);
6729 /* Left justify network addr and calculate its network mask */
6731 while (addr
&& (addr
& 0xff000000) == 0) {
6735 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6739 if (proto
== Q_LINK
) {
6740 const char *context
= "link host NAME";
6741 if (! is_mac48_linktype(cstate
->linktype
))
6742 fail_kw_on_dlt(cstate
, context
);
6743 eaddrp
= pcap_ether_hostton(name
);
6745 bpf_error(cstate
, ERRSTR_UNKNOWN_MAC48HOST
, name
);
6746 memcpy(eaddr
, eaddrp
, sizeof(eaddr
));
6748 return gen_mac48host(cstate
, eaddr
, q
.dir
, context
);
6749 } else if (proto
== Q_DECNET
) {
6751 * A long time ago on Ultrix libpcap supported
6752 * translation of DECnet host names into DECnet
6753 * addresses, but this feature is history now.
6755 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6757 memset(&mask128
, 0xff, sizeof(mask128
));
6758 res0
= res
= pcap_nametoaddrinfo(name
);
6760 bpf_error(cstate
, "unknown host '%s'", name
);
6765 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6766 tproto
== Q_DEFAULT
) {
6770 for (res
= res0
; res
; res
= res
->ai_next
) {
6771 switch (res
->ai_family
) {
6773 if (tproto
== Q_IPV6
)
6776 sin4
= (struct sockaddr_in
*)
6778 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6779 0xffffffff, tproto
, dir
, q
.addr
);
6782 if (tproto6
== Q_IP
)
6785 sin6
= (struct sockaddr_in6
*)
6787 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6788 &mask128
, tproto6
, dir
, q
.addr
);
6800 bpf_error(cstate
, "unknown host '%s'%s", name
,
6801 (proto
== Q_DEFAULT
)
6803 : " for specified address family");
6809 (void)port_pq_to_ipproto(cstate
, proto
, "port"); // validate only
6810 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6811 bpf_error(cstate
, "unknown port '%s'", name
);
6812 if (proto
== Q_UDP
) {
6813 if (real_proto
== IPPROTO_TCP
)
6814 bpf_error(cstate
, "port '%s' is tcp", name
);
6815 else if (real_proto
== IPPROTO_SCTP
)
6816 bpf_error(cstate
, "port '%s' is sctp", name
);
6818 /* override PROTO_UNDEF */
6819 real_proto
= IPPROTO_UDP
;
6821 if (proto
== Q_TCP
) {
6822 if (real_proto
== IPPROTO_UDP
)
6823 bpf_error(cstate
, "port '%s' is udp", name
);
6825 else if (real_proto
== IPPROTO_SCTP
)
6826 bpf_error(cstate
, "port '%s' is sctp", name
);
6828 /* override PROTO_UNDEF */
6829 real_proto
= IPPROTO_TCP
;
6831 if (proto
== Q_SCTP
) {
6832 if (real_proto
== IPPROTO_UDP
)
6833 bpf_error(cstate
, "port '%s' is udp", name
);
6835 else if (real_proto
== IPPROTO_TCP
)
6836 bpf_error(cstate
, "port '%s' is tcp", name
);
6838 /* override PROTO_UNDEF */
6839 real_proto
= IPPROTO_SCTP
;
6842 bpf_error(cstate
, "illegal port number %d < 0", port
);
6844 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6845 // real_proto can be PROTO_UNDEF
6846 b
= gen_port(cstate
, (uint16_t)port
, real_proto
, dir
);
6847 gen_or(gen_port6(cstate
, (uint16_t)port
, real_proto
, dir
), b
);
6851 (void)port_pq_to_ipproto(cstate
, proto
, "portrange"); // validate only
6852 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
6853 if (proto
== Q_UDP
) {
6854 if (real_proto
== IPPROTO_TCP
)
6855 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6856 else if (real_proto
== IPPROTO_SCTP
)
6857 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6859 /* override PROTO_UNDEF */
6860 real_proto
= IPPROTO_UDP
;
6862 if (proto
== Q_TCP
) {
6863 if (real_proto
== IPPROTO_UDP
)
6864 bpf_error(cstate
, "port in range '%s' is udp", name
);
6865 else if (real_proto
== IPPROTO_SCTP
)
6866 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6868 /* override PROTO_UNDEF */
6869 real_proto
= IPPROTO_TCP
;
6871 if (proto
== Q_SCTP
) {
6872 if (real_proto
== IPPROTO_UDP
)
6873 bpf_error(cstate
, "port in range '%s' is udp", name
);
6874 else if (real_proto
== IPPROTO_TCP
)
6875 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6877 /* override PROTO_UNDEF */
6878 real_proto
= IPPROTO_SCTP
;
6881 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6883 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6885 // real_proto can be PROTO_UNDEF
6886 b
= gen_portrange(cstate
, (uint16_t)port1
, (uint16_t)port2
,
6888 gen_or(gen_portrange6(cstate
, (uint16_t)port1
, (uint16_t)port2
,
6889 real_proto
, dir
), b
);
6893 if (! is_mac48_linktype(cstate
->linktype
))
6894 fail_kw_on_dlt(cstate
, "gateway");
6895 eaddrp
= pcap_ether_hostton(name
);
6897 bpf_error(cstate
, ERRSTR_UNKNOWN_MAC48HOST
, name
);
6898 memcpy(eaddr
, eaddrp
, sizeof(eaddr
));
6901 res
= pcap_nametoaddrinfo(name
);
6904 bpf_error(cstate
, "unknown host '%s'", name
);
6905 b
= gen_gateway(cstate
, eaddr
, res
, proto
);
6909 bpf_error(cstate
, "unknown host '%s'", name
);
6913 real_proto
= lookup_proto(cstate
, name
, proto
);
6914 if (real_proto
>= 0)
6915 return gen_proto(cstate
, real_proto
, proto
);
6917 bpf_error(cstate
, "unknown protocol: %s", name
);
6919 #if !defined(NO_PROTOCHAIN)
6921 real_proto
= lookup_proto(cstate
, name
, proto
);
6922 if (real_proto
>= 0)
6923 return gen_protochain(cstate
, real_proto
, proto
);
6925 bpf_error(cstate
, "unknown protocol: %s", name
);
6926 #endif /* !defined(NO_PROTOCHAIN) */
6937 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6938 bpf_u_int32 masklen
, struct qual q
)
6940 register int nlen
, mlen
;
6945 * Catch errors reported by us and routines below us, and return NULL
6948 if (setjmp(cstate
->top_ctx
))
6951 nlen
= pcapint_atoin(s1
, &n
);
6953 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
6954 /* Promote short ipaddr */
6958 mlen
= pcapint_atoin(s2
, &m
);
6960 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
6961 /* Promote short ipaddr */
6964 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6967 /* Convert mask len to mask */
6969 bpf_error(cstate
, "mask length must be <= 32");
6970 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
6971 m
= (bpf_u_int32
)m64
;
6973 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6980 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6983 // Q_HOST and Q_GATEWAY only (see the grammar)
6984 bpf_error(cstate
, "Mask syntax for networks only");
6991 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6999 * Catch errors reported by us and routines below us, and return NULL
7002 if (setjmp(cstate
->top_ctx
))
7009 * v contains a 32-bit unsigned parsed from a string of the
7010 * form {N}, which could be decimal, hexadecimal or octal.
7011 * Although it would be possible to use the value as a raw
7012 * 16-bit DECnet address when the value fits into 16 bits, this
7013 * would be a questionable feature: DECnet address wire
7014 * encoding is little-endian, so this would not work as
7015 * intuitively as the same works for [big-endian] IPv4
7016 * addresses (0x01020304 means 1.2.3.4).
7018 if (proto
== Q_DECNET
)
7019 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7021 } else if (proto
== Q_DECNET
) {
7023 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7024 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7025 * for a valid DECnet address.
7027 vlen
= pcapint_atodn(s
, &v
);
7029 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7032 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7033 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7036 vlen
= pcapint_atoin(s
, &v
);
7038 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7046 if (proto
== Q_DECNET
)
7047 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7048 else if (proto
== Q_LINK
) {
7049 // "link (host|net) IPV4ADDR" and variations thereof
7050 bpf_error(cstate
, "illegal link layer address");
7053 if (s
== NULL
&& q
.addr
== Q_NET
) {
7054 /* Promote short net number */
7055 while (v
&& (v
& 0xff000000) == 0) {
7060 /* Promote short ipaddr */
7062 mask
<<= 32 - vlen
;
7064 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7068 proto
= port_pq_to_ipproto(cstate
, proto
, "port");
7071 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7073 // proto can be PROTO_UNDEF
7076 b
= gen_port(cstate
, (uint16_t)v
, proto
, dir
);
7077 gen_or(gen_port6(cstate
, (uint16_t)v
, proto
, dir
), b
);
7082 proto
= port_pq_to_ipproto(cstate
, proto
, "portrange");
7085 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7087 // proto can be PROTO_UNDEF
7090 b
= gen_portrange(cstate
, (uint16_t)v
, (uint16_t)v
,
7092 gen_or(gen_portrange6(cstate
, (uint16_t)v
, (uint16_t)v
,
7098 bpf_error(cstate
, "'gateway' requires a name");
7102 return gen_proto(cstate
, v
, proto
);
7104 #if !defined(NO_PROTOCHAIN)
7106 return gen_protochain(cstate
, v
, proto
);
7121 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7124 struct addrinfo
*res
;
7125 struct in6_addr
*addr
;
7126 struct in6_addr mask
;
7128 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7131 * Catch errors reported by us and routines below us, and return NULL
7134 if (setjmp(cstate
->top_ctx
))
7137 res
= pcap_nametoaddrinfo(s
);
7139 bpf_error(cstate
, "invalid ip6 address %s", s
);
7142 bpf_error(cstate
, "%s resolved to multiple address", s
);
7143 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7145 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7146 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7147 memset(&mask
, 0, sizeof(mask
));
7148 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7150 mask
.s6_addr
[masklen
/ 8] =
7151 (0xff << (8 - masklen
% 8)) & 0xff;
7154 memcpy(a
, addr
, sizeof(a
));
7155 memcpy(m
, &mask
, sizeof(m
));
7156 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7157 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7158 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7166 bpf_error(cstate
, "Mask syntax for networks only");
7170 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7176 // Q_GATEWAY only (see the grammar)
7177 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7183 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7186 * Catch errors reported by us and routines below us, and return NULL
7189 if (setjmp(cstate
->top_ctx
))
7192 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7193 const char *context
= "link host XX:XX:XX:XX:XX:XX";
7194 if (! is_mac48_linktype(cstate
->linktype
))
7195 fail_kw_on_dlt(cstate
, context
);
7196 cstate
->e
= pcap_ether_aton(s
);
7197 if (cstate
->e
== NULL
)
7198 bpf_error(cstate
, "malloc");
7199 struct block
*b
= gen_mac48host(cstate
, cstate
->e
, q
.dir
, context
);
7204 bpf_error(cstate
, "ethernet address used in non-ether expression");
7209 sappend(struct slist
*s0
, struct slist
*s1
)
7212 * This is definitely not the best way to do this, but the
7213 * lists will rarely get long.
7220 static struct slist
*
7221 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7225 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7230 static struct slist
*
7231 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7235 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7241 * Modify "index" to use the value stored into its register as an
7242 * offset relative to the beginning of the header for the protocol
7243 * "proto", and allocate a register and put an item "size" bytes long
7244 * (1, 2, or 4) at that offset into that register, making it the register
7247 static struct arth
*
7248 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7252 struct slist
*s
, *tmp
;
7254 int regno
= alloc_reg(cstate
);
7256 free_reg(cstate
, inst
->regno
);
7260 bpf_error(cstate
, "data size must be 1, 2, or 4");
7277 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7281 * The offset is relative to the beginning of the packet
7282 * data, if we have a radio header. (If we don't, this
7285 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7286 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7287 cstate
->linktype
!= DLT_PRISM_HEADER
)
7288 bpf_error(cstate
, "radio information not present in capture");
7291 * Load into the X register the offset computed into the
7292 * register specified by "index".
7294 s
= xfer_to_x(cstate
, inst
);
7297 * Load the item at that offset.
7299 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7301 sappend(inst
->s
, s
);
7306 * The offset is relative to the beginning of
7307 * the link-layer header.
7309 * XXX - what about ATM LANE? Should the index be
7310 * relative to the beginning of the AAL5 frame, so
7311 * that 0 refers to the beginning of the LE Control
7312 * field, or relative to the beginning of the LAN
7313 * frame, so that 0 refers, for Ethernet LANE, to
7314 * the beginning of the destination address?
7316 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7319 * If "s" is non-null, it has code to arrange that the
7320 * X register contains the length of the prefix preceding
7321 * the link-layer header. Add to it the offset computed
7322 * into the register specified by "index", and move that
7323 * into the X register. Otherwise, just load into the X
7324 * register the offset computed into the register specified
7328 sappend(s
, xfer_to_a(cstate
, inst
));
7329 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7330 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7332 s
= xfer_to_x(cstate
, inst
);
7335 * Load the item at the sum of the offset we've put in the
7336 * X register and the offset of the start of the link
7337 * layer header (which is 0 if the radio header is
7338 * variable-length; that header length is what we put
7339 * into the X register and then added to the index).
7341 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7342 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7344 sappend(inst
->s
, s
);
7358 * The offset is relative to the beginning of
7359 * the network-layer header.
7360 * XXX - are there any cases where we want
7361 * cstate->off_nl_nosnap?
7363 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7366 * If "s" is non-null, it has code to arrange that the
7367 * X register contains the variable part of the offset
7368 * of the link-layer payload. Add to it the offset
7369 * computed into the register specified by "index",
7370 * and move that into the X register. Otherwise, just
7371 * load into the X register the offset computed into
7372 * the register specified by "index".
7375 sappend(s
, xfer_to_a(cstate
, inst
));
7376 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7377 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7379 s
= xfer_to_x(cstate
, inst
);
7382 * Load the item at the sum of the offset we've put in the
7383 * X register, the offset of the start of the network
7384 * layer header from the beginning of the link-layer
7385 * payload, and the constant part of the offset of the
7386 * start of the link-layer payload.
7388 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7389 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7391 sappend(inst
->s
, s
);
7394 * Do the computation only if the packet contains
7395 * the protocol in question.
7397 b
= gen_proto_abbrev_internal(cstate
, proto
);
7399 gen_and(inst
->b
, b
);
7413 * The offset is relative to the beginning of
7414 * the transport-layer header.
7416 * Load the X register with the length of the IPv4 header
7417 * (plus the offset of the link-layer header, if it's
7418 * a variable-length header), in bytes.
7420 * XXX - are there any cases where we want
7421 * cstate->off_nl_nosnap?
7422 * XXX - we should, if we're built with
7423 * IPv6 support, generate code to load either
7424 * IPv4, IPv6, or both, as appropriate.
7426 s
= gen_loadx_iphdrlen(cstate
);
7429 * The X register now contains the sum of the variable
7430 * part of the offset of the link-layer payload and the
7431 * length of the network-layer header.
7433 * Load into the A register the offset relative to
7434 * the beginning of the transport layer header,
7435 * add the X register to that, move that to the
7436 * X register, and load with an offset from the
7437 * X register equal to the sum of the constant part of
7438 * the offset of the link-layer payload and the offset,
7439 * relative to the beginning of the link-layer payload,
7440 * of the network-layer header.
7442 sappend(s
, xfer_to_a(cstate
, inst
));
7443 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7444 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7445 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7446 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7447 sappend(inst
->s
, s
);
7450 * Do the computation only if the packet contains
7451 * the protocol in question - which is true only
7452 * if this is an IP datagram and is the first or
7453 * only fragment of that datagram.
7455 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7457 gen_and(inst
->b
, b
);
7458 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7463 * Do the computation only if the packet contains
7464 * the protocol in question.
7466 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7468 gen_and(inst
->b
, b
);
7472 * Check if we have an icmp6 next header
7474 b
= gen_ip6_proto(cstate
, 58);
7476 gen_and(inst
->b
, b
);
7479 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7481 * If "s" is non-null, it has code to arrange that the
7482 * X register contains the variable part of the offset
7483 * of the link-layer payload. Add to it the offset
7484 * computed into the register specified by "index",
7485 * and move that into the X register. Otherwise, just
7486 * load into the X register the offset computed into
7487 * the register specified by "index".
7490 sappend(s
, xfer_to_a(cstate
, inst
));
7491 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7492 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7494 s
= xfer_to_x(cstate
, inst
);
7497 * Load the item at the sum of the offset we've put in the
7498 * X register, the offset of the start of the network
7499 * layer header from the beginning of the link-layer
7500 * payload, and the constant part of the offset of the
7501 * start of the link-layer payload.
7503 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7504 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7507 sappend(inst
->s
, s
);
7511 inst
->regno
= regno
;
7512 s
= new_stmt(cstate
, BPF_ST
);
7514 sappend(inst
->s
, s
);
7520 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7524 * Catch errors reported by us and routines below us, and return NULL
7527 if (setjmp(cstate
->top_ctx
))
7530 return gen_load_internal(cstate
, proto
, inst
, size
);
7533 static struct block
*
7534 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7535 struct arth
*a1
, int reversed
)
7537 struct slist
*s0
, *s1
, *s2
;
7538 struct block
*b
, *tmp
;
7540 s0
= xfer_to_x(cstate
, a1
);
7541 s1
= xfer_to_a(cstate
, a0
);
7542 if (code
== BPF_JEQ
) {
7543 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7544 b
= new_block(cstate
, JMP(code
));
7548 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7554 sappend(a0
->s
, a1
->s
);
7558 free_reg(cstate
, a0
->regno
);
7559 free_reg(cstate
, a1
->regno
);
7561 /* 'and' together protocol checks */
7564 gen_and(a0
->b
, tmp
= a1
->b
);
7578 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7579 struct arth
*a1
, int reversed
)
7582 * Catch errors reported by us and routines below us, and return NULL
7585 if (setjmp(cstate
->top_ctx
))
7588 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7592 gen_loadlen(compiler_state_t
*cstate
)
7599 * Catch errors reported by us and routines below us, and return NULL
7602 if (setjmp(cstate
->top_ctx
))
7605 regno
= alloc_reg(cstate
);
7606 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7607 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7608 s
->next
= new_stmt(cstate
, BPF_ST
);
7609 s
->next
->s
.k
= regno
;
7616 static struct arth
*
7617 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7623 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7625 reg
= alloc_reg(cstate
);
7627 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7629 s
->next
= new_stmt(cstate
, BPF_ST
);
7638 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7641 * Catch errors reported by us and routines below us, and return NULL
7644 if (setjmp(cstate
->top_ctx
))
7647 return gen_loadi_internal(cstate
, val
);
7651 * The a_arg dance is to avoid annoying whining by compilers that
7652 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7653 * It's not *used* after setjmp returns.
7656 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7658 struct arth
*a
= a_arg
;
7662 * Catch errors reported by us and routines below us, and return NULL
7665 if (setjmp(cstate
->top_ctx
))
7668 s
= xfer_to_a(cstate
, a
);
7670 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7673 s
= new_stmt(cstate
, BPF_ST
);
7681 * The a0_arg dance is to avoid annoying whining by compilers that
7682 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7683 * It's not *used* after setjmp returns.
7686 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7689 struct arth
*a0
= a0_arg
;
7690 struct slist
*s0
, *s1
, *s2
;
7693 * Catch errors reported by us and routines below us, and return NULL
7696 if (setjmp(cstate
->top_ctx
))
7700 * Disallow division by, or modulus by, zero; we do this here
7701 * so that it gets done even if the optimizer is disabled.
7703 * Also disallow shifts by a value greater than 31; we do this
7704 * here, for the same reason.
7706 if (code
== BPF_DIV
) {
7707 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7708 bpf_error(cstate
, "division by zero");
7709 } else if (code
== BPF_MOD
) {
7710 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7711 bpf_error(cstate
, "modulus by zero");
7712 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7713 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7714 bpf_error(cstate
, "shift by more than 31 bits");
7716 s0
= xfer_to_x(cstate
, a1
);
7717 s1
= xfer_to_a(cstate
, a0
);
7718 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7723 sappend(a0
->s
, a1
->s
);
7725 free_reg(cstate
, a0
->regno
);
7726 free_reg(cstate
, a1
->regno
);
7728 s0
= new_stmt(cstate
, BPF_ST
);
7729 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7736 * Initialize the table of used registers and the current register.
7739 init_regs(compiler_state_t
*cstate
)
7742 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7746 * Return the next free register.
7749 alloc_reg(compiler_state_t
*cstate
)
7751 int n
= BPF_MEMWORDS
;
7754 if (cstate
->regused
[cstate
->curreg
])
7755 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7757 cstate
->regused
[cstate
->curreg
] = 1;
7758 return cstate
->curreg
;
7761 bpf_error(cstate
, "too many registers needed to evaluate expression");
7766 * Return a register to the table so it can
7770 free_reg(compiler_state_t
*cstate
, int n
)
7772 cstate
->regused
[n
] = 0;
7775 static struct block
*
7776 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7780 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7781 return gen_jmp(cstate
, jmp
, n
, s
);
7785 gen_greater(compiler_state_t
*cstate
, int n
)
7788 * Catch errors reported by us and routines below us, and return NULL
7791 if (setjmp(cstate
->top_ctx
))
7794 return gen_len(cstate
, BPF_JGE
, n
);
7798 * Actually, this is less than or equal.
7801 gen_less(compiler_state_t
*cstate
, int n
)
7806 * Catch errors reported by us and routines below us, and return NULL
7809 if (setjmp(cstate
->top_ctx
))
7812 b
= gen_len(cstate
, BPF_JGT
, n
);
7819 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7820 * the beginning of the link-layer header.
7821 * XXX - that means you can't test values in the radiotap header, but
7822 * as that header is difficult if not impossible to parse generally
7823 * without a loop, that might not be a severe problem. A new keyword
7824 * "radio" could be added for that, although what you'd really want
7825 * would be a way of testing particular radio header values, which
7826 * would generate code appropriate to the radio header in question.
7829 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7835 * Catch errors reported by us and routines below us, and return NULL
7838 if (setjmp(cstate
->top_ctx
))
7841 assert_maxval(cstate
, "byte argument", val
, UINT8_MAX
);
7848 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7851 return gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7854 return gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7857 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7861 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7865 // Load the required byte first.
7866 struct slist
*s0
= gen_load_a(cstate
, OR_LINKHDR
, idx
, BPF_B
);
7868 b
= gen_jmp(cstate
, BPF_JEQ
, 0, s0
);
7875 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7877 bpf_u_int32 hostmask
;
7878 struct block
*b0
, *b1
, *b2
;
7879 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7882 * Catch errors reported by us and routines below us, and return NULL
7885 if (setjmp(cstate
->top_ctx
))
7892 switch (cstate
->linktype
) {
7894 case DLT_ARCNET_LINUX
:
7895 // ARCnet broadcast is [8-bit] destination address 0.
7896 return gen_ahostop(cstate
, 0, Q_DST
);
7898 return gen_mac48host(cstate
, ebroadcast
, Q_DST
, "broadcast");
7903 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7904 * as an indication that we don't know the netmask, and fail
7907 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7908 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7909 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7910 hostmask
= ~cstate
->netmask
;
7911 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
7912 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
7917 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
7922 * Generate code to test the low-order bit of a MAC address (that's
7923 * the bottom bit of the *first* byte).
7925 static struct block
*
7926 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7928 register struct slist
*s
;
7930 /* link[offset] & 1 != 0 */
7931 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7932 return gen_set(cstate
, 1, s
);
7936 gen_multicast(compiler_state_t
*cstate
, int proto
)
7938 register struct block
*b0
, *b1
, *b2
;
7939 register struct slist
*s
;
7942 * Catch errors reported by us and routines below us, and return NULL
7945 if (setjmp(cstate
->top_ctx
))
7952 switch (cstate
->linktype
) {
7954 case DLT_ARCNET_LINUX
:
7955 // ARCnet multicast is the same as broadcast.
7956 return gen_ahostop(cstate
, 0, Q_DST
);
7958 case DLT_NETANALYZER
:
7959 case DLT_NETANALYZER_TRANSPARENT
:
7960 b1
= gen_prevlinkhdr_check(cstate
);
7961 /* ether[0] & 1 != 0 */
7962 b0
= gen_mac_multicast(cstate
, 0);
7968 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7970 * XXX - was that referring to bit-order issues?
7972 /* fddi[1] & 1 != 0 */
7973 return gen_mac_multicast(cstate
, 1);
7975 /* tr[2] & 1 != 0 */
7976 return gen_mac_multicast(cstate
, 2);
7977 case DLT_IEEE802_11
:
7978 case DLT_PRISM_HEADER
:
7979 case DLT_IEEE802_11_RADIO_AVS
:
7980 case DLT_IEEE802_11_RADIO
:
7985 * For control frames, there is no DA.
7987 * For management frames, DA is at an
7988 * offset of 4 from the beginning of
7991 * For data frames, DA is at an offset
7992 * of 4 from the beginning of the packet
7993 * if To DS is clear and at an offset of
7994 * 16 from the beginning of the packet
7999 * Generate the tests to be done for data frames.
8001 * First, check for To DS set, i.e. "link[1] & 0x01".
8003 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8004 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8007 * If To DS is set, the DA is at 16.
8009 b0
= gen_mac_multicast(cstate
, 16);
8013 * Now, check for To DS not set, i.e. check
8014 * "!(link[1] & 0x01)".
8016 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8017 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8020 * If To DS is not set, the DA is at 4.
8022 b1
= gen_mac_multicast(cstate
, 4);
8026 * Now OR together the last two checks. That gives
8027 * the complete set of checks for data frames.
8032 * Now check for a data frame.
8033 * I.e, check "link[0] & 0x08".
8035 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8036 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8039 * AND that with the checks done for data frames.
8044 * If the high-order bit of the type value is 0, this
8045 * is a management frame.
8046 * I.e, check "!(link[0] & 0x08)".
8048 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8049 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8052 * For management frames, the DA is at 4.
8054 b1
= gen_mac_multicast(cstate
, 4);
8058 * OR that with the checks done for data frames.
8059 * That gives the checks done for management and
8065 * If the low-order bit of the type value is 1,
8066 * this is either a control frame or a frame
8067 * with a reserved type, and thus not a
8070 * I.e., check "!(link[0] & 0x04)".
8072 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8073 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
8076 * AND that with the checks for data and management
8081 case DLT_IP_OVER_FC
:
8082 return gen_mac_multicast(cstate
, 2);
8086 fail_kw_on_dlt(cstate
, "multicast");
8090 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8091 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8096 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8097 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8101 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8107 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8108 * we can look at special meta-data in the filter expression; otherwise we
8109 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8110 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8111 * pcap_activate() conditionally sets.
8114 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8116 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8118 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8120 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8125 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8128 * Catch errors reported by us and routines below us, and return NULL
8131 if (setjmp(cstate
->top_ctx
))
8135 * Only some data link types support ifindex qualifiers.
8137 switch (cstate
->linktype
) {
8138 case DLT_LINUX_SLL2
:
8139 /* match packets on this interface */
8140 return gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8142 #if defined(__linux__)
8143 require_basic_bpf_extensions(cstate
, "ifindex");
8145 return gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8147 #else /* defined(__linux__) */
8148 fail_kw_on_dlt(cstate
, "ifindex");
8150 #endif /* defined(__linux__) */
8155 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8156 * Outbound traffic is sent by this machine, while inbound traffic is
8157 * sent by a remote machine (and may include packets destined for a
8158 * unicast or multicast link-layer address we are not subscribing to).
8159 * These are the same definitions implemented by pcap_setdirection().
8160 * Capturing only unicast traffic destined for this host is probably
8161 * better accomplished using a higher-layer filter.
8164 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8166 register struct block
*b0
;
8169 * Catch errors reported by us and routines below us, and return NULL
8172 if (setjmp(cstate
->top_ctx
))
8176 * Only some data link types support inbound/outbound qualifiers.
8178 switch (cstate
->linktype
) {
8180 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8181 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8184 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8185 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8188 /* match outgoing packets */
8189 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8191 /* to filter on inbound traffic, invert the match */
8196 case DLT_LINUX_SLL2
:
8197 /* match outgoing packets */
8198 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8200 /* to filter on inbound traffic, invert the match */
8206 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8207 outbound
? PF_OUT
: PF_IN
);
8210 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8212 case DLT_JUNIPER_MFR
:
8213 case DLT_JUNIPER_MLFR
:
8214 case DLT_JUNIPER_MLPPP
:
8215 case DLT_JUNIPER_ATM1
:
8216 case DLT_JUNIPER_ATM2
:
8217 case DLT_JUNIPER_PPPOE
:
8218 case DLT_JUNIPER_PPPOE_ATM
:
8219 case DLT_JUNIPER_GGSN
:
8220 case DLT_JUNIPER_ES
:
8221 case DLT_JUNIPER_MONITOR
:
8222 case DLT_JUNIPER_SERVICES
:
8223 case DLT_JUNIPER_ETHER
:
8224 case DLT_JUNIPER_PPP
:
8225 case DLT_JUNIPER_FRELAY
:
8226 case DLT_JUNIPER_CHDLC
:
8227 case DLT_JUNIPER_VP
:
8228 case DLT_JUNIPER_ST
:
8229 case DLT_JUNIPER_ISM
:
8230 case DLT_JUNIPER_VS
:
8231 case DLT_JUNIPER_SRX_E2E
:
8232 case DLT_JUNIPER_FIBRECHANNEL
:
8233 case DLT_JUNIPER_ATM_CEMIC
:
8234 /* juniper flags (including direction) are stored
8235 * the byte after the 3-byte magic number */
8236 return gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8240 * If we have packet meta-data indicating a direction,
8241 * and that metadata can be checked by BPF code, check
8242 * it. Otherwise, give up, as this link-layer type has
8243 * nothing in the packet data.
8245 * Currently, the only platform where a BPF filter can
8246 * check that metadata is Linux with the in-kernel
8247 * BPF interpreter. If other packet capture mechanisms
8248 * and BPF filters also supported this, it would be
8249 * nice. It would be even better if they made that
8250 * metadata available so that we could provide it
8251 * with newer capture APIs, allowing it to be saved
8254 #if defined(__linux__)
8255 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8256 /* match outgoing packets */
8257 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8260 /* to filter on inbound traffic, invert the match */
8264 #else /* defined(__linux__) */
8265 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8267 #endif /* defined(__linux__) */
8271 /* PF firewall log matched interface */
8273 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8278 * Catch errors reported by us and routines below us, and return NULL
8281 if (setjmp(cstate
->top_ctx
))
8284 assert_pflog(cstate
, "ifname");
8286 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8287 off
= offsetof(struct pfloghdr
, ifname
);
8288 if (strlen(ifname
) >= len
) {
8289 bpf_error(cstate
, "ifname interface names can only be %d characters",
8293 return gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8294 (const u_char
*)ifname
);
8297 /* PF firewall log ruleset name */
8299 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8302 * Catch errors reported by us and routines below us, and return NULL
8305 if (setjmp(cstate
->top_ctx
))
8308 assert_pflog(cstate
, "ruleset");
8310 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8311 bpf_error(cstate
, "ruleset names can only be %ld characters",
8312 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8316 return gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8317 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8320 /* PF firewall log rule number */
8322 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8325 * Catch errors reported by us and routines below us, and return NULL
8328 if (setjmp(cstate
->top_ctx
))
8331 assert_pflog(cstate
, "rnr");
8333 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8337 /* PF firewall log sub-rule number */
8339 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8342 * Catch errors reported by us and routines below us, and return NULL
8345 if (setjmp(cstate
->top_ctx
))
8348 assert_pflog(cstate
, "srnr");
8350 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8354 /* PF firewall log reason code */
8356 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8359 * Catch errors reported by us and routines below us, and return NULL
8362 if (setjmp(cstate
->top_ctx
))
8365 assert_pflog(cstate
, "reason");
8367 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8368 (bpf_u_int32
)reason
);
8371 /* PF firewall log action */
8373 gen_pf_action(compiler_state_t
*cstate
, int action
)
8376 * Catch errors reported by us and routines below us, and return NULL
8379 if (setjmp(cstate
->top_ctx
))
8382 assert_pflog(cstate
, "action");
8384 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8385 (bpf_u_int32
)action
);
8388 /* IEEE 802.11 wireless header */
8390 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8393 * Catch errors reported by us and routines below us, and return NULL
8396 if (setjmp(cstate
->top_ctx
))
8399 switch (cstate
->linktype
) {
8401 case DLT_IEEE802_11
:
8402 case DLT_PRISM_HEADER
:
8403 case DLT_IEEE802_11_RADIO_AVS
:
8404 case DLT_IEEE802_11_RADIO
:
8406 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8409 fail_kw_on_dlt(cstate
, "type/subtype");
8415 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8418 * Catch errors reported by us and routines below us, and return NULL
8421 if (setjmp(cstate
->top_ctx
))
8424 switch (cstate
->linktype
) {
8426 case DLT_IEEE802_11
:
8427 case DLT_PRISM_HEADER
:
8428 case DLT_IEEE802_11_RADIO_AVS
:
8429 case DLT_IEEE802_11_RADIO
:
8431 return gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8432 IEEE80211_FC1_DIR_MASK
);
8435 fail_kw_on_dlt(cstate
, "dir");
8440 // Process an ARCnet host address string.
8442 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8445 * Catch errors reported by us and routines below us, and return NULL
8448 if (setjmp(cstate
->top_ctx
))
8451 switch (cstate
->linktype
) {
8454 case DLT_ARCNET_LINUX
:
8455 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8456 q
.proto
== Q_LINK
) {
8459 * The lexer currently defines the address format in a
8460 * way that makes this error condition never true.
8461 * Let's check it anyway in case this part of the lexer
8462 * changes in future.
8464 if (! pcapint_atoan(s
, &addr
))
8465 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8466 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8468 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8472 bpf_error(cstate
, "aid supported only on ARCnet");
8477 // Compare an ARCnet host address with the given value.
8478 static struct block
*
8479 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8481 register struct block
*b0
, *b1
;
8485 * ARCnet is different from Ethernet: the source address comes before
8486 * the destination address, each is one byte long. This holds for all
8487 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8488 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8489 * by Datapoint (document number 61610-01).
8492 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8495 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8498 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8499 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8505 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8506 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8516 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8523 static struct block
*
8524 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8526 struct block
*b0
, *b1
;
8528 /* check for VLAN, including 802.1ad and QinQ */
8529 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8530 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8533 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8539 static struct block
*
8540 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8542 assert_maxval(cstate
, "VLAN tag", vlan_num
, 0x0fff);
8543 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8546 static struct block
*
8547 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8550 struct block
*b0
, *b1
;
8552 b0
= gen_vlan_tpid_test(cstate
);
8555 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8561 * Both payload and link header type follow the VLAN tags so that
8562 * both need to be updated.
8564 cstate
->off_linkpl
.constant_part
+= 4;
8565 cstate
->off_linktype
.constant_part
+= 4;
8570 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8571 /* add v to variable part of off */
8573 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8574 bpf_u_int32 v
, struct slist
*s
)
8578 if (!off
->is_variable
)
8579 off
->is_variable
= 1;
8581 off
->reg
= alloc_reg(cstate
);
8583 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8586 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8589 s2
= new_stmt(cstate
, BPF_ST
);
8595 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8596 * and link type offsets first
8599 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8603 /* offset determined at run time, shift variable part */
8605 cstate
->is_vlan_vloffset
= 1;
8606 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8607 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8609 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8610 sappend(s
.next
, b_tpid
->head
->stmts
);
8611 b_tpid
->head
->stmts
= s
.next
;
8615 * patch block b_vid (VLAN id test) to load VID value either from packet
8616 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8619 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8621 struct slist
*s
, *s2
, *sjeq
;
8624 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8625 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8627 /* true -> next instructions, false -> beginning of b_vid */
8628 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8630 sjeq
->s
.jf
= b_vid
->stmts
;
8633 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8634 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8638 /* Jump to the test in b_vid. We need to jump one instruction before
8639 * the end of the b_vid block so that we only skip loading the TCI
8640 * from packet data and not the 'and' instruction extracting VID.
8643 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8645 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8649 /* insert our statements at the beginning of b_vid */
8650 sappend(s
, b_vid
->stmts
);
8655 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8656 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8657 * tag can be either in metadata or in packet data; therefore if the
8658 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8659 * header for VLAN tag. As the decision is done at run time, we need
8660 * update variable part of the offsets
8662 static struct block
*
8663 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8666 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8669 /* generate new filter code based on extracting packet
8671 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8672 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8674 b0
= gen_jmp(cstate
, BPF_JEQ
, 1, s
);
8677 * This is tricky. We need to insert the statements updating variable
8678 * parts of offsets before the traditional TPID and VID tests so
8679 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8680 * we do not want this update to affect those checks. That's why we
8681 * generate both test blocks first and insert the statements updating
8682 * variable parts of both offsets after that. This wouldn't work if
8683 * there already were variable length link header when entering this
8684 * function but gen_vlan_bpf_extensions() isn't called in that case.
8686 b_tpid
= gen_vlan_tpid_test(cstate
);
8688 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8690 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8695 gen_vlan_patch_vid_test(cstate
, b_vid
);
8705 * support IEEE 802.1Q VLAN trunk over ethernet
8708 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8713 * Catch errors reported by us and routines below us, and return NULL
8716 if (setjmp(cstate
->top_ctx
))
8719 /* can't check for VLAN-encapsulated packets inside MPLS */
8720 if (cstate
->label_stack_depth
> 0)
8721 bpf_error(cstate
, "no VLAN match after MPLS");
8724 * Check for a VLAN packet, and then change the offsets to point
8725 * to the type and data fields within the VLAN packet. Just
8726 * increment the offsets, so that we can support a hierarchy, e.g.
8727 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
8730 * XXX - this is a bit of a kludge. If we were to split the
8731 * compiler into a parser that parses an expression and
8732 * generates an expression tree, and a code generator that
8733 * takes an expression tree (which could come from our
8734 * parser or from some other parser) and generates BPF code,
8735 * we could perhaps make the offsets parameters of routines
8736 * and, in the handler for an "AND" node, pass to subnodes
8737 * other than the VLAN node the adjusted offsets.
8739 * This would mean that "vlan" would, instead of changing the
8740 * behavior of *all* tests after it, change only the behavior
8741 * of tests ANDed with it. That would change the documented
8742 * semantics of "vlan", which might break some expressions.
8743 * However, it would mean that "(vlan and ip) or ip" would check
8744 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8745 * checking only for VLAN-encapsulated IP, so that could still
8746 * be considered worth doing; it wouldn't break expressions
8747 * that are of the form "vlan and ..." or "vlan N and ...",
8748 * which I suspect are the most common expressions involving
8749 * "vlan". "vlan or ..." doesn't necessarily do what the user
8750 * would really want, now, as all the "or ..." tests would
8751 * be done assuming a VLAN, even though the "or" could be viewed
8752 * as meaning "or, if this isn't a VLAN packet...".
8754 switch (cstate
->linktype
) {
8758 * Newer version of the Linux kernel pass around
8759 * packets in which the VLAN tag has been removed
8760 * from the packet data and put into metadata.
8762 * This requires special treatment.
8764 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8765 /* Verify that this is the outer part of the packet and
8766 * not encapsulated somehow. */
8767 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8768 cstate
->off_linkhdr
.constant_part
==
8769 cstate
->off_outermostlinkhdr
.constant_part
) {
8771 * Do we need special VLAN handling?
8773 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8774 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8777 b0
= gen_vlan_no_bpf_extensions(cstate
,
8778 vlan_num
, has_vlan_tag
);
8781 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8785 case DLT_NETANALYZER
:
8786 case DLT_NETANALYZER_TRANSPARENT
:
8787 case DLT_IEEE802_11
:
8788 case DLT_PRISM_HEADER
:
8789 case DLT_IEEE802_11_RADIO_AVS
:
8790 case DLT_IEEE802_11_RADIO
:
8792 * These are either Ethernet packets with an additional
8793 * metadata header (the NetAnalyzer types), or 802.11
8794 * packets, possibly with an additional metadata header.
8796 * For the first of those, the VLAN tag is in the normal
8797 * place, so the special-case handling above isn't
8800 * For the second of those, we don't do the special-case
8803 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8807 bpf_error(cstate
, "no VLAN support for %s",
8808 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8812 cstate
->vlan_stack_depth
++;
8820 * The label_num_arg dance is to avoid annoying whining by compilers that
8821 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8822 * It's not *used* after setjmp returns.
8824 static struct block
*
8825 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
8828 struct block
*b0
, *b1
;
8830 if (cstate
->label_stack_depth
> 0) {
8831 /* just match the bottom-of-stack bit clear */
8832 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8835 * We're not in an MPLS stack yet, so check the link-layer
8836 * type against MPLS.
8838 switch (cstate
->linktype
) {
8840 case DLT_C_HDLC
: /* fall through */
8843 case DLT_NETANALYZER
:
8844 case DLT_NETANALYZER_TRANSPARENT
:
8845 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8849 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8852 /* FIXME add other DLT_s ...
8853 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8854 * leave it for now */
8857 bpf_error(cstate
, "no MPLS support for %s",
8858 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8863 /* If a specific MPLS label is requested, check it */
8864 if (has_label_num
) {
8865 assert_maxval(cstate
, "MPLS label", label_num
, 0xFFFFF);
8866 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8867 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
8868 0xfffff000); /* only compare the first 20 bits */
8874 * Change the offsets to point to the type and data fields within
8875 * the MPLS packet. Just increment the offsets, so that we
8876 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8877 * capture packets with an outer label of 100000 and an inner
8880 * Increment the MPLS stack depth as well; this indicates that
8881 * we're checking MPLS-encapsulated headers, to make sure higher
8882 * level code generators don't try to match against IP-related
8883 * protocols such as Q_ARP, Q_RARP etc.
8885 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8887 cstate
->off_nl_nosnap
+= 4;
8888 cstate
->off_nl
+= 4;
8889 cstate
->label_stack_depth
++;
8894 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
8897 * Catch errors reported by us and routines below us, and return NULL
8900 if (setjmp(cstate
->top_ctx
))
8903 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
8907 * Support PPPOE discovery and session.
8910 gen_pppoed(compiler_state_t
*cstate
)
8913 * Catch errors reported by us and routines below us, and return NULL
8916 if (setjmp(cstate
->top_ctx
))
8919 /* check for PPPoE discovery */
8920 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
8924 * RFC 2516 Section 4:
8926 * The Ethernet payload for PPPoE is as follows:
8929 * 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
8930 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8931 * | VER | TYPE | CODE | SESSION_ID |
8932 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8933 * | LENGTH | payload ~
8934 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8937 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
8939 struct block
*b0
, *b1
;
8942 * Catch errors reported by us and routines below us, and return NULL
8945 if (setjmp(cstate
->top_ctx
))
8949 * Test against the PPPoE session link-layer type.
8951 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
8953 /* If a specific session is requested, check PPPoE session id */
8955 assert_maxval(cstate
, "PPPoE session number", sess_num
, UINT16_MAX
);
8956 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
8962 * Change the offsets to point to the type and data fields within
8963 * the PPP packet, and note that this is PPPoE rather than
8966 * XXX - this is a bit of a kludge. See the comments in
8969 * The "network-layer" protocol is PPPoE, which has a 6-byte
8970 * PPPoE header, followed by a PPP packet.
8972 * There is no HDLC encapsulation for the PPP packet (it's
8973 * encapsulated in PPPoES instead), so the link-layer type
8974 * starts at the first byte of the PPP packet. For PPPoE,
8975 * that offset is relative to the beginning of the total
8976 * link-layer payload, including any 802.2 LLC header, so
8977 * it's 6 bytes past cstate->off_nl.
8979 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8980 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8981 cstate
->off_linkpl
.reg
);
8983 cstate
->off_linktype
= cstate
->off_linkhdr
;
8984 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8987 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8992 /* Check that this is Geneve and the VNI is correct if
8993 * specified. Parameterized to handle both IPv4 and IPv6. */
8994 static struct block
*
8995 gen_geneve_check(compiler_state_t
*cstate
,
8996 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
8997 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
8999 struct block
*b0
, *b1
;
9001 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9003 /* Check that we are operating on version 0. Otherwise, we
9004 * can't decode the rest of the fields. The version is 2 bits
9005 * in the first byte of the Geneve header. */
9006 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9011 assert_maxval(cstate
, "Geneve VNI", vni
, 0xffffff);
9012 vni
<<= 8; /* VNI is in the upper 3 bytes */
9013 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9021 /* The IPv4 and IPv6 Geneve checks need to do two things:
9022 * - Verify that this actually is Geneve with the right VNI.
9023 * - Place the IP header length (plus variable link prefix if
9024 * needed) into register A to be used later to compute
9025 * the inner packet offsets. */
9026 static struct block
*
9027 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9029 struct block
*b0
, *b1
;
9030 struct slist
*s
, *s1
;
9032 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9034 /* Load the IP header length into A. */
9035 s
= gen_loadx_iphdrlen(cstate
);
9037 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9040 /* Forcibly append these statements to the true condition
9041 * of the protocol check by creating a new block that is
9042 * always true and ANDing them. */
9043 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9050 static struct block
*
9051 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9053 struct block
*b0
, *b1
;
9054 struct slist
*s
, *s1
;
9056 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9058 /* Load the IP header length. We need to account for a
9059 * variable length link prefix if there is one. */
9060 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9062 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9066 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9070 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9074 /* Forcibly append these statements to the true condition
9075 * of the protocol check by creating a new block that is
9076 * always true and ANDing them. */
9077 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9080 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9087 /* We need to store three values based on the Geneve header::
9088 * - The offset of the linktype.
9089 * - The offset of the end of the Geneve header.
9090 * - The offset of the end of the encapsulated MAC header. */
9091 static struct slist
*
9092 gen_geneve_offsets(compiler_state_t
*cstate
)
9094 struct slist
*s
, *s1
, *s_proto
;
9096 /* First we need to calculate the offset of the Geneve header
9097 * itself. This is composed of the IP header previously calculated
9098 * (include any variable link prefix) and stored in A plus the
9099 * fixed sized headers (fixed link prefix, MAC length, and UDP
9101 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9102 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9104 /* Stash this in X since we'll need it later. */
9105 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9108 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9110 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9114 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9115 cstate
->off_linktype
.is_variable
= 1;
9116 cstate
->off_linktype
.constant_part
= 0;
9118 s1
= new_stmt(cstate
, BPF_ST
);
9119 s1
->s
.k
= cstate
->off_linktype
.reg
;
9122 /* Load the Geneve option length and mask and shift to get the
9123 * number of bytes. It is stored in the first byte of the Geneve
9125 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9129 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9133 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9137 /* Add in the rest of the Geneve base header. */
9138 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9142 /* Add the Geneve header length to its offset and store. */
9143 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9147 /* Set the encapsulated type as Ethernet. Even though we may
9148 * not actually have Ethernet inside there are two reasons this
9150 * - The linktype field is always in EtherType format regardless
9151 * of whether it is in Geneve or an inner Ethernet frame.
9152 * - The only link layer that we have specific support for is
9153 * Ethernet. We will confirm that the packet actually is
9154 * Ethernet at runtime before executing these checks. */
9155 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9157 s1
= new_stmt(cstate
, BPF_ST
);
9158 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9161 /* Calculate whether we have an Ethernet header or just raw IP/
9162 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9163 * and linktype by 14 bytes so that the network header can be found
9164 * seamlessly. Otherwise, keep what we've calculated already. */
9166 /* We have a bare jmp so we can't use the optimizer. */
9167 cstate
->no_optimize
= 1;
9169 /* Load the EtherType in the Geneve header, 2 bytes in. */
9170 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9174 /* Load X with the end of the Geneve header. */
9175 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9176 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9179 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9180 * end of this check, we should have the total length in X. In
9181 * the non-Ethernet case, it's already there. */
9182 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9183 s_proto
->s
.k
= ETHERTYPE_TEB
;
9184 sappend(s
, s_proto
);
9186 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9190 /* Since this is Ethernet, use the EtherType of the payload
9191 * directly as the linktype. Overwrite what we already have. */
9192 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9196 s1
= new_stmt(cstate
, BPF_ST
);
9197 s1
->s
.k
= cstate
->off_linktype
.reg
;
9200 /* Advance two bytes further to get the end of the Ethernet
9202 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9206 /* Move the result to X. */
9207 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9210 /* Store the final result of our linkpl calculation. */
9211 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9212 cstate
->off_linkpl
.is_variable
= 1;
9213 cstate
->off_linkpl
.constant_part
= 0;
9215 s1
= new_stmt(cstate
, BPF_STX
);
9216 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9225 /* Check to see if this is a Geneve packet. */
9227 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9229 struct block
*b0
, *b1
;
9233 * Catch errors reported by us and routines below us, and return NULL
9236 if (setjmp(cstate
->top_ctx
))
9239 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9240 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9245 /* Later filters should act on the payload of the Geneve frame,
9246 * update all of the header pointers. Attach this code so that
9247 * it gets executed in the event that the Geneve filter matches. */
9248 s
= gen_geneve_offsets(cstate
);
9250 b1
= gen_true(cstate
);
9251 sappend(s
, b1
->stmts
);
9256 cstate
->is_encap
= 1;
9261 /* Check that this is VXLAN and the VNI is correct if
9262 * specified. Parameterized to handle both IPv4 and IPv6. */
9263 static struct block
*
9264 gen_vxlan_check(compiler_state_t
*cstate
,
9265 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
9266 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9268 struct block
*b0
, *b1
;
9270 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9272 /* Check that the VXLAN header has the flag bits set
9274 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9279 assert_maxval(cstate
, "VXLAN VNI", vni
, 0xffffff);
9280 vni
<<= 8; /* VNI is in the upper 3 bytes */
9281 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9289 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9290 * - Verify that this actually is VXLAN with the right VNI.
9291 * - Place the IP header length (plus variable link prefix if
9292 * needed) into register A to be used later to compute
9293 * the inner packet offsets. */
9294 static struct block
*
9295 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9297 struct block
*b0
, *b1
;
9298 struct slist
*s
, *s1
;
9300 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9302 /* Load the IP header length into A. */
9303 s
= gen_loadx_iphdrlen(cstate
);
9305 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9308 /* Forcibly append these statements to the true condition
9309 * of the protocol check by creating a new block that is
9310 * always true and ANDing them. */
9311 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9318 static struct block
*
9319 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9321 struct block
*b0
, *b1
;
9322 struct slist
*s
, *s1
;
9324 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9326 /* Load the IP header length. We need to account for a
9327 * variable length link prefix if there is one. */
9328 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9330 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9334 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9338 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9342 /* Forcibly append these statements to the true condition
9343 * of the protocol check by creating a new block that is
9344 * always true and ANDing them. */
9345 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9348 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9355 /* We need to store three values based on the VXLAN header:
9356 * - The offset of the linktype.
9357 * - The offset of the end of the VXLAN header.
9358 * - The offset of the end of the encapsulated MAC header. */
9359 static struct slist
*
9360 gen_vxlan_offsets(compiler_state_t
*cstate
)
9362 struct slist
*s
, *s1
;
9364 /* Calculate the offset of the VXLAN header itself. This
9365 * includes the IP header computed previously (including any
9366 * variable link prefix) and stored in A plus the fixed size
9367 * headers (fixed link prefix, MAC length, UDP header). */
9368 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9369 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9371 /* Add the VXLAN header length to its offset and store */
9372 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9376 /* Push the link header. VXLAN packets always contain Ethernet
9378 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9380 s1
= new_stmt(cstate
, BPF_ST
);
9381 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9384 /* As the payload is an Ethernet packet, we can use the
9385 * EtherType of the payload directly as the linktype. */
9386 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9390 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9391 cstate
->off_linktype
.is_variable
= 1;
9392 cstate
->off_linktype
.constant_part
= 0;
9394 s1
= new_stmt(cstate
, BPF_ST
);
9395 s1
->s
.k
= cstate
->off_linktype
.reg
;
9398 /* Two bytes further is the end of the Ethernet header and the
9399 * start of the payload. */
9400 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9404 /* Move the result to X. */
9405 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9408 /* Store the final result of our linkpl calculation. */
9409 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9410 cstate
->off_linkpl
.is_variable
= 1;
9411 cstate
->off_linkpl
.constant_part
= 0;
9413 s1
= new_stmt(cstate
, BPF_STX
);
9414 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9422 /* Check to see if this is a VXLAN packet. */
9424 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9426 struct block
*b0
, *b1
;
9430 * Catch errors reported by us and routines below us, and return NULL
9433 if (setjmp(cstate
->top_ctx
))
9436 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9437 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9442 /* Later filters should act on the payload of the VXLAN frame,
9443 * update all of the header pointers. Attach this code so that
9444 * it gets executed in the event that the VXLAN filter matches. */
9445 s
= gen_vxlan_offsets(cstate
);
9447 b1
= gen_true(cstate
);
9448 sappend(s
, b1
->stmts
);
9453 cstate
->is_encap
= 1;
9458 /* Check that the encapsulated frame has a link layer header
9459 * for Ethernet filters. */
9460 static struct block
*
9461 gen_encap_ll_check(compiler_state_t
*cstate
)
9464 struct slist
*s
, *s1
;
9466 /* The easiest way to see if there is a link layer present
9467 * is to check if the link layer header and payload are not
9470 /* Geneve always generates pure variable offsets so we can
9471 * compare only the registers. */
9472 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9473 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9475 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9476 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9479 b0
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9485 static struct block
*
9486 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9487 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9489 assert_atm(cstate
, atmkw(atmfield
));
9494 assert_maxval(cstate
, "VPI", jvalue
, UINT8_MAX
);
9495 return gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9496 0xffffffffU
, jtype
, reverse
, jvalue
);
9499 assert_maxval(cstate
, "VCI", jvalue
, UINT16_MAX
);
9500 return gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9501 0xffffffffU
, jtype
, reverse
, jvalue
);
9508 static struct block
*
9509 gen_atm_vpi(compiler_state_t
*cstate
, const uint8_t v
)
9511 return gen_atmfield_code_internal(cstate
, A_VPI
, v
, BPF_JEQ
, 0);
9514 static struct block
*
9515 gen_atm_vci(compiler_state_t
*cstate
, const uint16_t v
)
9517 return gen_atmfield_code_internal(cstate
, A_VCI
, v
, BPF_JEQ
, 0);
9520 static struct block
*
9521 gen_atm_prototype(compiler_state_t
*cstate
, const uint8_t v
)
9523 return gen_mcmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, v
, 0x0fU
);
9526 static struct block
*
9527 gen_atmtype_llc(compiler_state_t
*cstate
)
9531 b0
= gen_atm_prototype(cstate
, PT_LLC
);
9532 cstate
->linktype
= cstate
->prevlinktype
;
9537 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9538 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9541 * Catch errors reported by us and routines below us, and return NULL
9544 if (setjmp(cstate
->top_ctx
))
9547 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9552 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9554 struct block
*b0
, *b1
;
9557 * Catch errors reported by us and routines below us, and return NULL
9560 if (setjmp(cstate
->top_ctx
))
9563 assert_atm(cstate
, atmkw(type
));
9568 /* Get all packets in Meta signalling Circuit */
9569 b0
= gen_atm_vpi(cstate
, 0);
9570 b1
= gen_atm_vci(cstate
, 1);
9575 /* Get all packets in Broadcast Circuit*/
9576 b0
= gen_atm_vpi(cstate
, 0);
9577 b1
= gen_atm_vci(cstate
, 2);
9582 /* Get all cells in Segment OAM F4 circuit*/
9583 b0
= gen_atm_vpi(cstate
, 0);
9584 b1
= gen_atm_vci(cstate
, 3);
9589 /* Get all cells in End-to-End OAM F4 Circuit*/
9590 b0
= gen_atm_vpi(cstate
, 0);
9591 b1
= gen_atm_vci(cstate
, 4);
9596 /* Get all packets in connection Signalling Circuit */
9597 b0
= gen_atm_vpi(cstate
, 0);
9598 b1
= gen_atm_vci(cstate
, 5);
9603 /* Get all packets in ILMI Circuit */
9604 b0
= gen_atm_vpi(cstate
, 0);
9605 b1
= gen_atm_vci(cstate
, 16);
9610 /* Get all LANE packets */
9611 b1
= gen_atm_prototype(cstate
, PT_LANE
);
9614 * Arrange that all subsequent tests assume LANE
9615 * rather than LLC-encapsulated packets, and set
9616 * the offsets appropriately for LANE-encapsulated
9619 * We assume LANE means Ethernet, not Token Ring.
9621 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9622 cstate
->off_payload
+ 2, /* Ethernet header */
9624 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9625 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9626 cstate
->off_nl
= 0; /* Ethernet II */
9627 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9636 * Filtering for MTP2 messages based on li value
9637 * FISU, length is null
9638 * LSSU, length is 1 or 2
9639 * MSU, length is 3 or more
9640 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9643 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9645 struct block
*b0
, *b1
;
9648 * Catch errors reported by us and routines below us, and return NULL
9651 if (setjmp(cstate
->top_ctx
))
9654 assert_ss7(cstate
, ss7kw(type
));
9659 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9660 0x3fU
, BPF_JEQ
, 0, 0U);
9663 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9664 0x3fU
, BPF_JGT
, 1, 2U);
9665 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9666 0x3fU
, BPF_JGT
, 0, 0U);
9671 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9672 0x3fU
, BPF_JGT
, 0, 2U);
9675 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9676 0xff80U
, BPF_JEQ
, 0, 0U);
9679 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9680 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9681 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9682 0xff80U
, BPF_JGT
, 0, 0U);
9687 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9688 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9696 * These maximum valid values are all-ones, so they double as the bitmasks
9697 * before any bitwise shifting.
9699 #define MTP2_SIO_MAXVAL UINT8_MAX
9700 #define MTP3_PC_MAXVAL 0x3fffU
9701 #define MTP3_SLS_MAXVAL 0xfU
9703 static struct block
*
9704 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
9705 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9712 newoff_sio
= cstate
->off_sio
;
9713 newoff_opc
= cstate
->off_opc
;
9714 newoff_dpc
= cstate
->off_dpc
;
9715 newoff_sls
= cstate
->off_sls
;
9717 assert_ss7(cstate
, ss7kw(mtp3field
));
9719 switch (mtp3field
) {
9722 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
9724 * SIO is the simplest field: the size is one byte and the offset is a
9725 * multiple of bytes, so the only detail to get right is the value of
9726 * the [right-to-left] field offset.
9729 newoff_sio
+= 3; /* offset for MTP2_HSL */
9733 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP2_SIO_MAXVAL
);
9734 // Here the bitmask means "do not apply a bitmask".
9735 return gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
9736 jtype
, reverse
, jvalue
);
9739 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
9741 * SLS, OPC and DPC are more complicated: none of these is sized in a
9742 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
9743 * diagrams are meant to be read right-to-left. This means in the
9744 * diagrams within individual fields and concatenations thereof
9745 * bitwise shifts and masks can be noted in the common left-to-right
9746 * manner until each final value is ready to be byte-swapped and
9747 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
9748 * similar problem in a similar way.
9750 * Offsets of fields within the packet header always have the
9751 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
9752 * DLTs the offset does not include the F (Flag) field at the
9753 * beginning of each message.
9755 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
9756 * 32-bit standard routing header has a 4 byte [RTL] offset and could
9757 * be tested entirely using a single BPF_W comparison. In this case
9758 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
9759 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
9760 * [LTR] bitmask would be (0xF << 28), all of which conveniently
9761 * correlates with the [RTL] packet diagram until the byte-swapping is
9764 * The code below uses this approach for OPC, which spans 3 bytes.
9765 * DPC and SLS use shorter loads, SLS also uses a different offset.
9772 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
9773 return gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
9774 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
9775 SWAPLONG(jvalue
<< 14));
9782 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
9783 return gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
9784 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
9792 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_SLS_MAXVAL
);
9793 return gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
9794 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
9803 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9804 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9807 * Catch errors reported by us and routines below us, and return NULL
9810 if (setjmp(cstate
->top_ctx
))
9813 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
9817 static struct block
*
9818 gen_msg_abbrev(compiler_state_t
*cstate
, const uint8_t type
)
9821 * Q.2931 signalling protocol messages for handling virtual circuits
9822 * establishment and teardown
9824 return gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
,
9829 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9831 struct block
*b0
, *b1
;
9834 * Catch errors reported by us and routines below us, and return NULL
9837 if (setjmp(cstate
->top_ctx
))
9840 assert_atm(cstate
, atmkw(type
));
9846 b0
= gen_atm_vci(cstate
, 3);
9847 b1
= gen_atm_vci(cstate
, 4);
9849 b0
= gen_atm_vpi(cstate
, 0);
9855 b0
= gen_atm_vci(cstate
, 3);
9856 b1
= gen_atm_vci(cstate
, 4);
9858 b0
= gen_atm_vpi(cstate
, 0);
9864 * Get Q.2931 signalling messages for switched
9865 * virtual connection
9867 b0
= gen_msg_abbrev(cstate
, SETUP
);
9868 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
9870 b0
= gen_msg_abbrev(cstate
, CONNECT
);
9872 b0
= gen_msg_abbrev(cstate
, CONNECT_ACK
);
9874 b0
= gen_msg_abbrev(cstate
, RELEASE
);
9876 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
9878 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9883 b0
= gen_msg_abbrev(cstate
, SETUP
);
9884 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
9886 b0
= gen_msg_abbrev(cstate
, CONNECT
);
9888 b0
= gen_msg_abbrev(cstate
, RELEASE
);
9890 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
9892 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);