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>
68 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
75 uint16_t u6_addr16
[8];
76 uint32_t u6_addr32
[4];
78 #define s6_addr in6_u.u6_addr8
79 #define s6_addr16 in6_u.u6_addr16
80 #define s6_addr32 in6_u.u6_addr32
81 #define s6_addr64 in6_u.u6_addr64
84 typedef unsigned short sa_family_t
;
86 #define __SOCKADDR_COMMON(sa_prefix) \
87 sa_family_t sa_prefix##family
89 /* Ditto, for IPv6. */
92 __SOCKADDR_COMMON (sin6_
);
93 uint16_t sin6_port
; /* Transport layer port # */
94 uint32_t sin6_flowinfo
; /* IPv6 flow information */
95 struct in6_addr sin6_addr
; /* IPv6 address */
98 #ifndef EAI_ADDRFAMILY
100 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
101 int ai_family
; /* PF_xxx */
102 int ai_socktype
; /* SOCK_xxx */
103 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
104 size_t ai_addrlen
; /* length of ai_addr */
105 char *ai_canonname
; /* canonical name for hostname */
106 struct sockaddr
*ai_addr
; /* binary address */
107 struct addrinfo
*ai_next
; /* next structure in linked list */
109 #endif /* EAI_ADDRFAMILY */
110 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
113 #include <netdb.h> /* for "struct addrinfo" */
115 #include <pcap/namedb.h>
117 #include "nametoaddr.h"
119 #define ETHERMTU 1500
121 #ifndef IPPROTO_HOPOPTS
122 #define IPPROTO_HOPOPTS 0
124 #ifndef IPPROTO_ROUTING
125 #define IPPROTO_ROUTING 43
127 #ifndef IPPROTO_FRAGMENT
128 #define IPPROTO_FRAGMENT 44
130 #ifndef IPPROTO_DSTOPTS
131 #define IPPROTO_DSTOPTS 60
134 #define IPPROTO_SCTP 132
137 #define GENEVE_PORT 6081
138 #define VXLAN_PORT 4789
142 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
146 #define ARCTYPE_IP_OLD 240 /* IP protocol */
147 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
150 #define ARCTYPE_IP 212 /* IP protocol */
151 #define ARCTYPE_ARP 213 /* address resolution protocol */
152 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
154 #define ARCTYPE_ATALK 221 /* Appletalk */
155 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
156 #define ARCTYPE_IPX 250 /* Novell IPX */
158 #define ARCTYPE_INET6 0xc4 /* IPng */
159 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
162 /* Based on UNI3.1 standard by ATM Forum */
164 /* ATM traffic types based on VPI=0 and (the following VCI */
165 #define VCI_PPC 0x05 /* Point-to-point signal msg */
166 #define VCI_BCC 0x02 /* Broadcast signal msg */
167 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
168 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
169 #define VCI_METAC 0x01 /* Meta signal msg */
170 #define VCI_ILMIC 0x10 /* ILMI msg */
172 /* Q.2931 signalling messages */
173 #define CALL_PROCEED 0x02 /* call proceeding */
174 #define CONNECT 0x07 /* connect */
175 #define CONNECT_ACK 0x0f /* connect_ack */
176 #define SETUP 0x05 /* setup */
177 #define RELEASE 0x4d /* release */
178 #define RELEASE_DONE 0x5a /* release_done */
179 #define RESTART 0x46 /* restart */
180 #define RESTART_ACK 0x4e /* restart ack */
181 #define STATUS 0x7d /* status */
182 #define STATUS_ENQ 0x75 /* status ack */
183 #define ADD_PARTY 0x80 /* add party */
184 #define ADD_PARTY_ACK 0x81 /* add party ack */
185 #define ADD_PARTY_REJ 0x82 /* add party rej */
186 #define DROP_PARTY 0x83 /* drop party */
187 #define DROP_PARTY_ACK 0x84 /* drop party ack */
189 /* Information Element Parameters in the signalling messages */
190 #define CAUSE 0x08 /* cause */
191 #define ENDPT_REF 0x54 /* endpoint reference */
192 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
193 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
194 #define CONNECT_ID 0x5a /* connection identifier */
195 #define QOS_PARA 0x5c /* quality of service parameters */
196 #define B_HIGHER 0x5d /* broadband higher layer information */
197 #define B_BEARER 0x5e /* broadband bearer capability */
198 #define B_LOWER 0x5f /* broadband lower information */
199 #define CALLING_PARTY 0x6c /* calling party number */
200 #define CALLED_PARTY 0x70 /* called party number */
204 /* Q.2931 signalling general messages format */
205 #define PROTO_POS 0 /* offset of protocol discriminator */
206 #define CALL_REF_POS 2 /* offset of call reference value */
207 #define MSG_TYPE_POS 5 /* offset of message type */
208 #define MSG_LEN_POS 7 /* offset of message length */
209 #define IE_BEGIN_POS 9 /* offset of first information element */
211 /* format of signalling messages */
214 #define FIELD_BEGIN_POS 4
217 /* SunATM header for ATM packet */
218 #define SUNATM_DIR_POS 0
219 #define SUNATM_VPI_POS 1
220 #define SUNATM_VCI_POS 2
221 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
223 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
224 #define PT_LANE 0x01 /* LANE */
225 #define PT_LLC 0x02 /* LLC encapsulation */
226 #define PT_ILMI 0x05 /* ILMI */
227 #define PT_QSAAL 0x06 /* Q.SAAL */
230 /* Types missing from some systems */
233 * Network layer protocol identifiers
236 #define ISO8473_CLNP 0x81
239 #define ISO9542_ESIS 0x82
241 #ifndef ISO9542X25_ESIS
242 #define ISO9542X25_ESIS 0x8a
244 #ifndef ISO10589_ISIS
245 #define ISO10589_ISIS 0x83
248 #define ISIS_L1_LAN_IIH 15
249 #define ISIS_L2_LAN_IIH 16
250 #define ISIS_PTP_IIH 17
251 #define ISIS_L1_LSP 18
252 #define ISIS_L2_LSP 20
253 #define ISIS_L1_CSNP 24
254 #define ISIS_L2_CSNP 25
255 #define ISIS_L1_PSNP 26
256 #define ISIS_L2_PSNP 27
258 * The maximum possible value can also be used as a bit mask because the
259 * "PDU Type" field comprises the least significant 5 bits of a particular
260 * octet, see sections 9.5~9.13 of ISO/IEC 10589:2002(E).
262 #define ISIS_PDU_TYPE_MAX 0x1FU
264 #ifndef ISO8878A_CONS
265 #define ISO8878A_CONS 0x84
267 #ifndef ISO10747_IDRP
268 #define ISO10747_IDRP 0x85
271 // Same as in tcpdump/print-sl.c.
273 #define SLIPDIR_OUT 1
275 #ifdef HAVE_OS_PROTO_H
276 #include "os-proto.h"
279 #define JMP(c) ((c)|BPF_JMP|BPF_K)
282 * "Push" the current value of the link-layer header type and link-layer
283 * header offset onto a "stack", and set a new value. (It's not a
284 * full-blown stack; we keep only the top two items.)
286 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
288 (cs)->prevlinktype = (cs)->linktype; \
289 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
290 (cs)->linktype = (new_linktype); \
291 (cs)->off_linkhdr.is_variable = (new_is_variable); \
292 (cs)->off_linkhdr.constant_part = (new_constant_part); \
293 (cs)->off_linkhdr.reg = (new_reg); \
294 (cs)->is_encap = 0; \
298 * Offset "not set" value.
300 #define OFFSET_NOT_SET 0xffffffffU
303 * Absolute offsets, which are offsets from the beginning of the raw
304 * packet data, are, in the general case, the sum of a variable value
305 * and a constant value; the variable value may be absent, in which
306 * case the offset is only the constant value, and the constant value
307 * may be zero, in which case the offset is only the variable value.
309 * bpf_abs_offset is a structure containing all that information:
311 * is_variable is 1 if there's a variable part.
313 * constant_part is the constant part of the value, possibly zero;
315 * if is_variable is 1, reg is the register number for a register
316 * containing the variable value if the register has been assigned,
326 * Value passed to gen_load_a() to indicate what the offset argument
327 * is relative to the beginning of.
330 OR_PACKET
, /* full packet data */
331 OR_LINKHDR
, /* link-layer header */
332 OR_PREVLINKHDR
, /* previous link-layer header */
333 OR_LLC
, /* 802.2 LLC header */
334 OR_PREVMPLSHDR
, /* previous MPLS header */
335 OR_LINKTYPE
, /* link-layer type */
336 OR_LINKPL
, /* link-layer payload */
337 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
338 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
339 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
343 * We divvy out chunks of memory rather than call malloc each time so
344 * we don't have to worry about leaking memory. It's probably
345 * not a big deal if all this memory was wasted but if this ever
346 * goes into a library that would probably not be a good idea.
348 * XXX - this *is* in a library....
351 #define CHUNK0SIZE 1024
358 * A chunk can store any of:
359 * - a string (guaranteed alignment 1 but present for completeness)
363 * For this simple allocator every allocated chunk gets rounded up to the
364 * alignment needed for any chunk.
375 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
377 /* Code generator state */
379 struct _compiler_state
{
390 int outermostlinktype
;
395 /* Hack for handling VLAN and MPLS stacks. */
396 u_int label_stack_depth
;
397 u_int vlan_stack_depth
;
403 * As errors are handled by a longjmp, anything allocated must
404 * be freed in the longjmp handler, so it must be reachable
407 * One thing that's allocated is the result of pcap_nametoaddrinfo();
408 * it must be freed with freeaddrinfo(). This variable points to
409 * any addrinfo structure that would need to be freed.
414 * Another thing that's allocated is the result of pcap_ether_aton();
415 * it must be freed with free(). This variable points to any
416 * address that would need to be freed.
421 * Various code constructs need to know the layout of the packet.
422 * These values give the necessary offsets from the beginning
423 * of the packet data.
427 * Absolute offset of the beginning of the link-layer header.
429 bpf_abs_offset off_linkhdr
;
432 * If we're checking a link-layer header for a packet encapsulated
433 * in another protocol layer, this is the equivalent information
434 * for the previous layers' link-layer header from the beginning
435 * of the raw packet data.
437 bpf_abs_offset off_prevlinkhdr
;
440 * This is the equivalent information for the outermost layers'
443 bpf_abs_offset off_outermostlinkhdr
;
446 * Absolute offset of the beginning of the link-layer payload.
448 bpf_abs_offset off_linkpl
;
451 * "off_linktype" is the offset to information in the link-layer
452 * header giving the packet type. This is an absolute offset
453 * from the beginning of the packet.
455 * For Ethernet, it's the offset of the Ethernet type field; this
456 * means that it must have a value that skips VLAN tags.
458 * For link-layer types that always use 802.2 headers, it's the
459 * offset of the LLC header; this means that it must have a value
460 * that skips VLAN tags.
462 * For PPP, it's the offset of the PPP type field.
464 * For Cisco HDLC, it's the offset of the CHDLC type field.
466 * For BSD loopback, it's the offset of the AF_ value.
468 * For Linux cooked sockets, it's the offset of the type field.
470 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
471 * encapsulation, in which case, IP is assumed.
473 bpf_abs_offset off_linktype
;
476 * TRUE if the link layer includes an ATM pseudo-header.
480 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
481 * causes us to generate code that checks for a Geneve or
482 * VXLAN header respectively and assume that later filters
483 * apply to the encapsulated payload.
488 * TRUE if we need variable length part of VLAN offset
490 int is_vlan_vloffset
;
493 * These are offsets for the ATM pseudo-header.
500 * These are offsets for the MTP2 fields.
506 * These are offsets for the MTP3 fields.
514 * This is the offset of the first byte after the ATM pseudo_header,
515 * or -1 if there is no ATM pseudo-header.
520 * These are offsets to the beginning of the network-layer header.
521 * They are relative to the beginning of the link-layer payload
522 * (i.e., they don't include off_linkhdr.constant_part or
523 * off_linkpl.constant_part).
525 * If the link layer never uses 802.2 LLC:
527 * "off_nl" and "off_nl_nosnap" are the same.
529 * If the link layer always uses 802.2 LLC:
531 * "off_nl" is the offset if there's a SNAP header following
534 * "off_nl_nosnap" is the offset if there's no SNAP header.
536 * If the link layer is Ethernet:
538 * "off_nl" is the offset if the packet is an Ethernet II packet
539 * (we assume no 802.3+802.2+SNAP);
541 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
542 * with an 802.2 header following it.
548 * Here we handle simple allocation of the scratch registers.
549 * If too many registers are alloc'd, the allocator punts.
551 int regused
[BPF_MEMWORDS
];
557 struct chunk chunks
[NCHUNKS
];
562 * For use by routines outside this file.
566 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
571 * If we've already set an error, don't override it.
572 * The lexical analyzer reports some errors by setting
573 * the error and then returning a LEX_ERROR token, which
574 * is not recognized by any grammar rule, and thus forces
575 * the parse to stop. We don't want the error reported
576 * by the lexical analyzer to be overwritten by the syntax
579 if (!cstate
->error_set
) {
581 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
584 cstate
->error_set
= 1;
589 * For use *ONLY* in routines in this file.
591 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
592 PCAP_PRINTFLIKE(2, 3);
595 static void PCAP_NORETURN
596 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
601 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
604 longjmp(cstate
->top_ctx
, 1);
611 static int init_linktype(compiler_state_t
*, pcap_t
*);
613 static void init_regs(compiler_state_t
*);
614 static int alloc_reg(compiler_state_t
*);
615 static void free_reg(compiler_state_t
*, int);
617 static void initchunks(compiler_state_t
*cstate
);
618 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
619 static void *newchunk(compiler_state_t
*cstate
, size_t);
620 static void freechunks(compiler_state_t
*cstate
);
621 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
622 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
623 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
624 static inline void syntax(compiler_state_t
*cstate
);
626 static void backpatch(struct block
*, struct block
*);
627 static void merge(struct block
*, struct block
*);
628 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
630 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
632 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
634 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
636 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
638 static struct block
*gen_cmp_ne(compiler_state_t
*, enum e_offrel
, u_int
,
639 u_int size
, bpf_u_int32
);
640 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
641 u_int
, bpf_u_int32
, bpf_u_int32
);
642 static struct block
*gen_mcmp_ne(compiler_state_t
*, enum e_offrel
, u_int
,
643 u_int
, bpf_u_int32
, bpf_u_int32
);
644 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
645 u_int
, const u_char
*);
646 static struct block
*gen_jmp(compiler_state_t
*, int, bpf_u_int32
,
648 static struct block
*gen_set(compiler_state_t
*, bpf_u_int32
, struct slist
*);
649 static struct block
*gen_unset(compiler_state_t
*, bpf_u_int32
, struct slist
*);
650 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
651 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
652 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
654 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
656 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
657 static struct block
*gen_uncond(compiler_state_t
*, int);
658 static inline struct block
*gen_true(compiler_state_t
*);
659 static inline struct block
*gen_false(compiler_state_t
*);
660 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
661 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
662 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
663 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
664 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
665 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
666 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
667 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
668 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
669 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
671 static uint16_t ethertype_to_ppptype(compiler_state_t
*, bpf_u_int32
);
672 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
673 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
674 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
675 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
678 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
679 struct in6_addr
*, int, u_int
, u_int
);
681 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
682 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
683 static unsigned char is_mac48_linktype(const int);
684 static struct block
*gen_mac48host(compiler_state_t
*, const u_char
*,
685 const u_char
, const char *);
686 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
687 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
688 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
691 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
692 struct in6_addr
*, int, int, int);
695 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
696 struct addrinfo
*, int);
698 static struct block
*gen_ip_proto(compiler_state_t
*, const uint8_t);
699 static struct block
*gen_ip6_proto(compiler_state_t
*, const uint8_t);
700 static struct block
*gen_ipfrag(compiler_state_t
*);
701 static struct block
*gen_portatom(compiler_state_t
*, int, uint16_t);
702 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, uint16_t,
704 static struct block
*gen_portatom6(compiler_state_t
*, int, uint16_t);
705 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, uint16_t,
707 static struct block
*gen_port(compiler_state_t
*, uint16_t, int, int);
708 static struct block
*gen_port_common(compiler_state_t
*, int, struct block
*);
709 static struct block
*gen_portrange(compiler_state_t
*, uint16_t, uint16_t,
711 static struct block
*gen_port6(compiler_state_t
*, uint16_t, int, int);
712 static struct block
*gen_port6_common(compiler_state_t
*, int, struct block
*);
713 static struct block
*gen_portrange6(compiler_state_t
*, uint16_t, uint16_t,
715 static int lookup_proto(compiler_state_t
*, const char *, int);
716 #if !defined(NO_PROTOCHAIN)
717 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
718 #endif /* !defined(NO_PROTOCHAIN) */
719 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int);
720 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
721 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
722 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
723 static struct block
*gen_len(compiler_state_t
*, int, int);
724 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
726 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
727 bpf_u_int32
, int, int);
728 static struct block
*gen_atmtype_llc(compiler_state_t
*);
729 static struct block
*gen_msg_abbrev(compiler_state_t
*, const uint8_t);
730 static struct block
*gen_atm_prototype(compiler_state_t
*, const uint8_t);
731 static struct block
*gen_atm_vpi(compiler_state_t
*, const uint8_t);
732 static struct block
*gen_atm_vci(compiler_state_t
*, const uint16_t);
735 initchunks(compiler_state_t
*cstate
)
739 for (i
= 0; i
< NCHUNKS
; i
++) {
740 cstate
->chunks
[i
].n_left
= 0;
741 cstate
->chunks
[i
].m
= NULL
;
743 cstate
->cur_chunk
= 0;
747 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
753 /* Round up to chunk alignment. */
754 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
756 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
757 if (n
> cp
->n_left
) {
759 k
= ++cstate
->cur_chunk
;
761 bpf_set_error(cstate
, "out of memory");
764 size
= CHUNK0SIZE
<< k
;
765 cp
->m
= (void *)malloc(size
);
767 bpf_set_error(cstate
, "out of memory");
770 memset((char *)cp
->m
, 0, size
);
773 bpf_set_error(cstate
, "out of memory");
778 return (void *)((char *)cp
->m
+ cp
->n_left
);
782 newchunk(compiler_state_t
*cstate
, size_t n
)
786 p
= newchunk_nolongjmp(cstate
, n
);
788 longjmp(cstate
->top_ctx
, 1);
795 freechunks(compiler_state_t
*cstate
)
799 for (i
= 0; i
< NCHUNKS
; ++i
)
800 if (cstate
->chunks
[i
].m
!= NULL
)
801 free(cstate
->chunks
[i
].m
);
805 * A strdup whose allocations are freed after code generation is over.
806 * This is used by the lexical analyzer, so it can't longjmp; it just
807 * returns NULL on an allocation error, and the callers must check
811 sdup(compiler_state_t
*cstate
, const char *s
)
813 size_t n
= strlen(s
) + 1;
814 char *cp
= newchunk_nolongjmp(cstate
, n
);
818 pcapint_strlcpy(cp
, s
, n
);
822 static inline struct block
*
823 new_block(compiler_state_t
*cstate
, int code
)
827 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
834 static inline struct slist
*
835 new_stmt(compiler_state_t
*cstate
, int code
)
839 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
845 static struct block
*
846 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
848 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
854 static struct block
*
855 gen_retblk(compiler_state_t
*cstate
, int v
)
857 if (setjmp(cstate
->top_ctx
)) {
859 * gen_retblk() only fails because a memory
860 * allocation failed in newchunk(), meaning
861 * that it can't return a pointer.
867 return gen_retblk_internal(cstate
, v
);
870 static inline PCAP_NORETURN_DEF
void
871 syntax(compiler_state_t
*cstate
)
873 bpf_error(cstate
, "syntax error in filter expression");
877 * For the given integer return a string with the keyword (or the nominal
878 * keyword if there is more than one). This is a simpler version of tok2str()
879 * in tcpdump because in this problem space a valid integer value is not
883 qual2kw(const char *kind
, const unsigned id
, const char *tokens
[],
886 static char buf
[4][64];
889 if (id
< size
&& tokens
[id
])
892 char *ret
= buf
[idx
];
893 idx
= (idx
+ 1) % (sizeof(buf
) / sizeof(buf
[0]));
894 ret
[0] = '\0'; // just in case
895 snprintf(ret
, sizeof(buf
[0]), "<invalid %s %u>", kind
, id
);
899 // protocol qualifier keywords
901 pqkw(const unsigned id
)
903 const char * tokens
[] = {
915 [Q_DECNET
] = "decnet",
921 [Q_ICMPV6
] = "icmp6",
933 [Q_NETBEUI
] = "netbeui",
936 [Q_ISIS_IIH
] = "iih",
937 [Q_ISIS_SNP
] = "snp",
938 [Q_ISIS_CSNP
] = "csnp",
939 [Q_ISIS_PSNP
] = "psnp",
940 [Q_ISIS_LSP
] = "lsp",
944 return qual2kw("proto", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
947 // direction qualifier keywords
949 dqkw(const unsigned id
)
951 const char * map
[] = {
954 [Q_OR
] = "src or dst",
955 [Q_AND
] = "src and dst",
963 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
968 atmkw(const unsigned id
)
970 const char * tokens
[] = {
973 [A_OAMF4SC
] = "oamf4sc",
974 [A_OAMF4EC
] = "oamf4ec",
982 [A_CONNECTMSG
] = "connectmsg",
983 [A_METACONNECT
] = "metaconnect",
985 return qual2kw("ATM keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
990 ss7kw(const unsigned id
)
992 const char * tokens
[] = {
1008 return qual2kw("MTP keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
1011 static PCAP_NORETURN_DEF
void
1012 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
1014 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
1015 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
1019 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
1021 if (cstate
->linktype
!= DLT_PFLOG
)
1022 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
1026 assert_atm(compiler_state_t
*cstate
, const char *kw
)
1029 * Belt and braces: init_linktype() sets either all of these struct
1030 * members (for DLT_SUNATM) or none (otherwise).
1032 if (cstate
->linktype
!= DLT_SUNATM
||
1034 cstate
->off_vpi
== OFFSET_NOT_SET
||
1035 cstate
->off_vci
== OFFSET_NOT_SET
||
1036 cstate
->off_proto
== OFFSET_NOT_SET
||
1037 cstate
->off_payload
== OFFSET_NOT_SET
)
1038 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1042 assert_ss7(compiler_state_t
*cstate
, const char *kw
)
1044 switch (cstate
->linktype
) {
1047 case DLT_MTP2_WITH_PHDR
:
1048 // Belt and braces, same as in assert_atm().
1049 if (cstate
->off_sio
!= OFFSET_NOT_SET
&&
1050 cstate
->off_opc
!= OFFSET_NOT_SET
&&
1051 cstate
->off_dpc
!= OFFSET_NOT_SET
&&
1052 cstate
->off_sls
!= OFFSET_NOT_SET
)
1055 bpf_error(cstate
, "'%s' supported only on SS7", kw
);
1059 assert_maxval(compiler_state_t
*cstate
, const char *name
,
1060 const bpf_u_int32 val
, const bpf_u_int32 maxval
)
1063 bpf_error(cstate
, "%s %u greater than maximum %u",
1067 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1068 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1069 #define ERRSTR_UNKNOWN_MAC48HOST "unknown Ethernet-like host '%s'"
1071 // Validate a port/portrange proto qualifier and map to an IP protocol number.
1073 port_pq_to_ipproto(compiler_state_t
*cstate
, const int proto
, const char *kw
)
1081 return IPPROTO_SCTP
;
1085 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), kw
);
1089 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1090 const char *buf
, int optimize
, bpf_u_int32 mask
)
1096 compiler_state_t cstate
;
1097 yyscan_t scanner
= NULL
;
1098 YY_BUFFER_STATE in_buffer
= NULL
;
1103 * If this pcap_t hasn't been activated, it doesn't have a
1104 * link-layer type, so we can't use it.
1106 if (!p
->activated
) {
1107 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1108 "not-yet-activated pcap_t passed to pcap_compile");
1109 return (PCAP_ERROR
);
1114 * Initialize Winsock, asking for the latest version (2.2),
1115 * as we may be calling Winsock routines to translate
1116 * host names to addresses.
1118 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1120 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1121 err
, "Error calling WSAStartup()");
1122 return (PCAP_ERROR
);
1126 #ifdef ENABLE_REMOTE
1128 * If the device on which we're capturing need to be notified
1129 * that a new filter is being compiled, do so.
1131 * This allows them to save a copy of it, in case, for example,
1132 * they're implementing a form of remote packet capture, and
1133 * want the remote machine to filter out the packets in which
1134 * it's sending the packets it's captured.
1136 * XXX - the fact that we happen to be compiling a filter
1137 * doesn't necessarily mean we'll be installing it as the
1138 * filter for this pcap_t; we might be running it from userland
1139 * on captured packets to do packet classification. We really
1140 * need a better way of handling this, but this is all that
1141 * the WinPcap remote capture code did.
1143 if (p
->save_current_filter_op
!= NULL
)
1144 (p
->save_current_filter_op
)(p
, buf
);
1147 initchunks(&cstate
);
1148 cstate
.no_optimize
= 0;
1151 cstate
.ic
.root
= NULL
;
1152 cstate
.ic
.cur_mark
= 0;
1153 cstate
.bpf_pcap
= p
;
1154 cstate
.error_set
= 0;
1157 cstate
.netmask
= mask
;
1159 cstate
.snaplen
= pcap_snapshot(p
);
1160 if (cstate
.snaplen
== 0) {
1161 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1162 "snaplen of 0 rejects all packets");
1167 if (pcap_lex_init(&scanner
) != 0) {
1168 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1169 errno
, "can't initialize scanner");
1173 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1176 * Associate the compiler state with the lexical analyzer
1179 pcap_set_extra(&cstate
, scanner
);
1181 if (init_linktype(&cstate
, p
) == -1) {
1185 if (pcap_parse(scanner
, &cstate
) != 0) {
1186 if (cstate
.ai
!= NULL
)
1187 freeaddrinfo(cstate
.ai
);
1188 if (cstate
.e
!= NULL
)
1194 if (cstate
.ic
.root
== NULL
) {
1195 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1198 * Catch errors reported by gen_retblk().
1200 if (cstate
.ic
.root
== NULL
) {
1206 if (optimize
&& !cstate
.no_optimize
) {
1207 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1212 if (cstate
.ic
.root
== NULL
||
1213 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1214 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1215 "expression rejects all packets");
1220 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1221 cstate
.ic
.root
, &len
, p
->errbuf
);
1222 if (program
->bf_insns
== NULL
) {
1227 program
->bf_len
= len
;
1229 rc
= 0; /* We're all okay */
1233 * Clean up everything for the lexical analyzer.
1235 if (in_buffer
!= NULL
)
1236 pcap__delete_buffer(in_buffer
, scanner
);
1237 if (scanner
!= NULL
)
1238 pcap_lex_destroy(scanner
);
1241 * Clean up our own allocated memory.
1243 freechunks(&cstate
);
1253 * entry point for using the compiler with no pcap open
1254 * pass in all the stuff that is needed explicitly instead.
1257 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1258 struct bpf_program
*program
,
1259 const char *buf
, int optimize
, bpf_u_int32 mask
)
1264 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1266 return (PCAP_ERROR
);
1267 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1273 * Clean up a "struct bpf_program" by freeing all the memory allocated
1277 pcap_freecode(struct bpf_program
*program
)
1279 program
->bf_len
= 0;
1280 if (program
->bf_insns
!= NULL
) {
1281 free((char *)program
->bf_insns
);
1282 program
->bf_insns
= NULL
;
1287 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1288 * which of the jt and jf fields has been resolved and which is a pointer
1289 * back to another unresolved block (or nil). At least one of the fields
1290 * in each block is already resolved.
1293 backpatch(struct block
*list
, struct block
*target
)
1310 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1311 * which of jt and jf is the link.
1314 merge(struct block
*b0
, struct block
*b1
)
1316 register struct block
**p
= &b0
;
1318 /* Find end of list. */
1320 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1322 /* Concatenate the lists. */
1327 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1330 * Catch errors reported by us and routines below us, and return -1
1333 if (setjmp(cstate
->top_ctx
))
1337 * Insert before the statements of the first (root) block any
1338 * statements needed to load the lengths of any variable-length
1339 * headers into registers.
1341 * XXX - a fancier strategy would be to insert those before the
1342 * statements of all blocks that use those lengths and that
1343 * have no predecessors that use them, so that we only compute
1344 * the lengths if we need them. There might be even better
1345 * approaches than that.
1347 * However, those strategies would be more complicated, and
1348 * as we don't generate code to compute a length if the
1349 * program has no tests that use the length, and as most
1350 * tests will probably use those lengths, we would just
1351 * postpone computing the lengths so that it's not done
1352 * for tests that fail early, and it's not clear that's
1355 insert_compute_vloffsets(cstate
, p
->head
);
1358 * For DLT_PPI captures, generate a check of the per-packet
1359 * DLT value to make sure it's DLT_IEEE802_11.
1361 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1362 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1363 * with appropriate Ethernet information and use that rather
1364 * than using something such as DLT_PPI where you don't know
1365 * the link-layer header type until runtime, which, in the
1366 * general case, would force us to generate both Ethernet *and*
1367 * 802.11 code (*and* anything else for which PPI is used)
1368 * and choose between them early in the BPF program?
1370 if (cstate
->linktype
== DLT_PPI
) {
1371 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1372 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1373 gen_and(ppi_dlt_check
, p
);
1376 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1377 p
->sense
= !p
->sense
;
1378 backpatch(p
, gen_retblk_internal(cstate
, 0));
1379 cstate
->ic
.root
= p
->head
;
1384 gen_and(struct block
*b0
, struct block
*b1
)
1386 backpatch(b0
, b1
->head
);
1387 b0
->sense
= !b0
->sense
;
1388 b1
->sense
= !b1
->sense
;
1390 b1
->sense
= !b1
->sense
;
1391 b1
->head
= b0
->head
;
1395 gen_or(struct block
*b0
, struct block
*b1
)
1397 b0
->sense
= !b0
->sense
;
1398 backpatch(b0
, b1
->head
);
1399 b0
->sense
= !b0
->sense
;
1401 b1
->head
= b0
->head
;
1405 gen_not(struct block
*b
)
1407 b
->sense
= !b
->sense
;
1410 static struct block
*
1411 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1412 u_int size
, bpf_u_int32 v
)
1414 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1417 static struct block
*
1418 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1419 u_int size
, bpf_u_int32 v
)
1421 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1424 static struct block
*
1425 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1426 u_int size
, bpf_u_int32 v
)
1428 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1431 static struct block
*
1432 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1433 u_int size
, bpf_u_int32 v
)
1435 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1438 static struct block
*
1439 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1440 u_int size
, bpf_u_int32 v
)
1442 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1445 static struct block
*
1446 gen_cmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1447 u_int size
, bpf_u_int32 v
)
1449 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 1, v
);
1452 static struct block
*
1453 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1454 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1457 * For any A: if mask == 0, it means A & mask == 0, so the result is
1458 * true iff v == 0. In this case ideally the caller should have
1459 * skipped this invocation and have fewer statement blocks to juggle.
1460 * If the caller could have skipped, but has not, produce a block with
1463 * This could be done in gen_ncmp() in a more generic way, but this
1464 * function is the only code path that can have mask == 0.
1467 return v
? gen_false(cstate
) : gen_true(cstate
);
1469 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1472 static struct block
*
1473 gen_mcmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1474 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1476 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 1, v
);
1479 static struct block
*
1480 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1481 u_int size
, const u_char
*v
)
1483 register struct block
*b
, *tmp
;
1487 register const u_char
*p
= &v
[size
- 4];
1489 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1497 register const u_char
*p
= &v
[size
- 2];
1499 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1507 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1515 static struct block
*
1516 gen_jmp(compiler_state_t
*cstate
, int jtype
, bpf_u_int32 v
, struct slist
*stmts
)
1518 struct block
*b
= new_block(cstate
, JMP(jtype
));
1524 static struct block
*
1525 gen_set(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1527 return gen_jmp(cstate
, BPF_JSET
, v
, stmts
);
1530 static struct block
*
1531 gen_unset(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1533 struct block
*b
= gen_set(cstate
, v
, stmts
);
1539 * AND the field of size "size" at offset "offset" relative to the header
1540 * specified by "offrel" with "mask", and compare it with the value "v"
1541 * with the test specified by "jtype"; if "reverse" is true, the test
1542 * should test the opposite of "jtype".
1544 static struct block
*
1545 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1546 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1549 struct slist
*s
, *s2
;
1552 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1554 if (mask
!= 0xffffffff) {
1555 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1560 b
= gen_jmp(cstate
, jtype
, v
, s
);
1567 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1569 cstate
->pcap_fddipad
= p
->fddipad
;
1572 * We start out with only one link-layer header.
1574 cstate
->outermostlinktype
= pcap_datalink(p
);
1575 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1576 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1577 cstate
->off_outermostlinkhdr
.reg
= -1;
1579 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1580 cstate
->off_prevlinkhdr
.constant_part
= 0;
1581 cstate
->off_prevlinkhdr
.is_variable
= 0;
1582 cstate
->off_prevlinkhdr
.reg
= -1;
1584 cstate
->linktype
= cstate
->outermostlinktype
;
1585 cstate
->off_linkhdr
.constant_part
= 0;
1586 cstate
->off_linkhdr
.is_variable
= 0;
1587 cstate
->off_linkhdr
.reg
= -1;
1592 cstate
->off_linkpl
.constant_part
= 0;
1593 cstate
->off_linkpl
.is_variable
= 0;
1594 cstate
->off_linkpl
.reg
= -1;
1596 cstate
->off_linktype
.constant_part
= 0;
1597 cstate
->off_linktype
.is_variable
= 0;
1598 cstate
->off_linktype
.reg
= -1;
1601 * Assume it's not raw ATM with a pseudo-header, for now.
1604 cstate
->off_vpi
= OFFSET_NOT_SET
;
1605 cstate
->off_vci
= OFFSET_NOT_SET
;
1606 cstate
->off_proto
= OFFSET_NOT_SET
;
1607 cstate
->off_payload
= OFFSET_NOT_SET
;
1610 * And not encapsulated with either Geneve or VXLAN.
1612 cstate
->is_encap
= 0;
1615 * No variable length VLAN offset by default
1617 cstate
->is_vlan_vloffset
= 0;
1620 * And assume we're not doing SS7.
1622 cstate
->off_li
= OFFSET_NOT_SET
;
1623 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1624 cstate
->off_sio
= OFFSET_NOT_SET
;
1625 cstate
->off_opc
= OFFSET_NOT_SET
;
1626 cstate
->off_dpc
= OFFSET_NOT_SET
;
1627 cstate
->off_sls
= OFFSET_NOT_SET
;
1629 cstate
->label_stack_depth
= 0;
1630 cstate
->vlan_stack_depth
= 0;
1632 switch (cstate
->linktype
) {
1635 cstate
->off_linktype
.constant_part
= 2;
1636 cstate
->off_linkpl
.constant_part
= 6;
1637 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1638 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1641 case DLT_ARCNET_LINUX
:
1642 cstate
->off_linktype
.constant_part
= 4;
1643 cstate
->off_linkpl
.constant_part
= 8;
1644 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1645 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1649 cstate
->off_linktype
.constant_part
= 12;
1650 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1651 cstate
->off_nl
= 0; /* Ethernet II */
1652 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1657 * SLIP doesn't have a link level type. The 16 byte
1658 * header is hacked into our SLIP driver.
1660 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1661 cstate
->off_linkpl
.constant_part
= 16;
1663 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1666 case DLT_SLIP_BSDOS
:
1667 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1668 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1670 cstate
->off_linkpl
.constant_part
= 24;
1672 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1677 cstate
->off_linktype
.constant_part
= 0;
1678 cstate
->off_linkpl
.constant_part
= 4;
1680 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1684 cstate
->off_linktype
.constant_part
= 0;
1685 cstate
->off_linkpl
.constant_part
= 12;
1687 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1692 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1693 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1694 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1695 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1696 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1698 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1703 * This does not include the Ethernet header, and
1704 * only covers session state.
1706 cstate
->off_linktype
.constant_part
= 6;
1707 cstate
->off_linkpl
.constant_part
= 8;
1709 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1713 cstate
->off_linktype
.constant_part
= 5;
1714 cstate
->off_linkpl
.constant_part
= 24;
1716 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1721 * FDDI doesn't really have a link-level type field.
1722 * We set "off_linktype" to the offset of the LLC header.
1724 * To check for Ethernet types, we assume that SSAP = SNAP
1725 * is being used and pick out the encapsulated Ethernet type.
1726 * XXX - should we generate code to check for SNAP?
1728 cstate
->off_linktype
.constant_part
= 13;
1729 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1730 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1731 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1732 cstate
->off_nl
= 8; /* 802.2+SNAP */
1733 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1738 * Token Ring doesn't really have a link-level type field.
1739 * We set "off_linktype" to the offset of the LLC header.
1741 * To check for Ethernet types, we assume that SSAP = SNAP
1742 * is being used and pick out the encapsulated Ethernet type.
1743 * XXX - should we generate code to check for SNAP?
1745 * XXX - the header is actually variable-length.
1746 * Some various Linux patched versions gave 38
1747 * as "off_linktype" and 40 as "off_nl"; however,
1748 * if a token ring packet has *no* routing
1749 * information, i.e. is not source-routed, the correct
1750 * values are 20 and 22, as they are in the vanilla code.
1752 * A packet is source-routed iff the uppermost bit
1753 * of the first byte of the source address, at an
1754 * offset of 8, has the uppermost bit set. If the
1755 * packet is source-routed, the total number of bytes
1756 * of routing information is 2 plus bits 0x1F00 of
1757 * the 16-bit value at an offset of 14 (shifted right
1758 * 8 - figure out which byte that is).
1760 cstate
->off_linktype
.constant_part
= 14;
1761 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1762 cstate
->off_nl
= 8; /* 802.2+SNAP */
1763 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1766 case DLT_PRISM_HEADER
:
1767 case DLT_IEEE802_11_RADIO_AVS
:
1768 case DLT_IEEE802_11_RADIO
:
1769 cstate
->off_linkhdr
.is_variable
= 1;
1770 /* Fall through, 802.11 doesn't have a variable link
1771 * prefix but is otherwise the same. */
1774 case DLT_IEEE802_11
:
1776 * 802.11 doesn't really have a link-level type field.
1777 * We set "off_linktype.constant_part" to the offset of
1780 * To check for Ethernet types, we assume that SSAP = SNAP
1781 * is being used and pick out the encapsulated Ethernet type.
1782 * XXX - should we generate code to check for SNAP?
1784 * We also handle variable-length radio headers here.
1785 * The Prism header is in theory variable-length, but in
1786 * practice it's always 144 bytes long. However, some
1787 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1788 * sometimes or always supply an AVS header, so we
1789 * have to check whether the radio header is a Prism
1790 * header or an AVS header, so, in practice, it's
1793 cstate
->off_linktype
.constant_part
= 24;
1794 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1795 cstate
->off_linkpl
.is_variable
= 1;
1796 cstate
->off_nl
= 8; /* 802.2+SNAP */
1797 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1802 * At the moment we treat PPI the same way that we treat
1803 * normal Radiotap encoded packets. The difference is in
1804 * the function that generates the code at the beginning
1805 * to compute the header length. Since this code generator
1806 * of PPI supports bare 802.11 encapsulation only (i.e.
1807 * the encapsulated DLT should be DLT_IEEE802_11) we
1808 * generate code to check for this too.
1810 cstate
->off_linktype
.constant_part
= 24;
1811 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1812 cstate
->off_linkpl
.is_variable
= 1;
1813 cstate
->off_linkhdr
.is_variable
= 1;
1814 cstate
->off_nl
= 8; /* 802.2+SNAP */
1815 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1818 case DLT_ATM_RFC1483
:
1819 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1821 * assume routed, non-ISO PDUs
1822 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1824 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1825 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1826 * latter would presumably be treated the way PPPoE
1827 * should be, so you can do "pppoe and udp port 2049"
1828 * or "pppoa and tcp port 80" and have it check for
1829 * PPPo{A,E} and a PPP protocol of IP and....
1831 cstate
->off_linktype
.constant_part
= 0;
1832 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1833 cstate
->off_nl
= 8; /* 802.2+SNAP */
1834 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1839 * Full Frontal ATM; you get AALn PDUs with an ATM
1843 cstate
->off_vpi
= SUNATM_VPI_POS
;
1844 cstate
->off_vci
= SUNATM_VCI_POS
;
1845 cstate
->off_proto
= PROTO_POS
;
1846 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1847 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1848 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1849 cstate
->off_nl
= 8; /* 802.2+SNAP */
1850 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1856 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1857 cstate
->off_linkpl
.constant_part
= 0;
1859 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1862 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1863 cstate
->off_linktype
.constant_part
= 14;
1864 cstate
->off_linkpl
.constant_part
= 16;
1866 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1869 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1870 cstate
->off_linktype
.constant_part
= 0;
1871 cstate
->off_linkpl
.constant_part
= 20;
1873 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1878 * LocalTalk does have a 1-byte type field in the LLAP header,
1879 * but really it just indicates whether there is a "short" or
1880 * "long" DDP packet following.
1882 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1883 cstate
->off_linkpl
.constant_part
= 0;
1885 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1888 case DLT_IP_OVER_FC
:
1890 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1891 * link-level type field. We set "off_linktype" to the
1892 * offset of the LLC header.
1894 * To check for Ethernet types, we assume that SSAP = SNAP
1895 * is being used and pick out the encapsulated Ethernet type.
1896 * XXX - should we generate code to check for SNAP? RFC
1897 * 2625 says SNAP should be used.
1899 cstate
->off_linktype
.constant_part
= 16;
1900 cstate
->off_linkpl
.constant_part
= 16;
1901 cstate
->off_nl
= 8; /* 802.2+SNAP */
1902 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1907 * XXX - we should set this to handle SNAP-encapsulated
1908 * frames (NLPID of 0x80).
1910 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1911 cstate
->off_linkpl
.constant_part
= 0;
1913 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1917 * the only BPF-interesting FRF.16 frames are non-control frames;
1918 * Frame Relay has a variable length link-layer
1919 * so lets start with offset 4 for now and increments later on (FIXME);
1922 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1923 cstate
->off_linkpl
.constant_part
= 0;
1925 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1928 case DLT_APPLE_IP_OVER_IEEE1394
:
1929 cstate
->off_linktype
.constant_part
= 16;
1930 cstate
->off_linkpl
.constant_part
= 18;
1932 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1935 case DLT_SYMANTEC_FIREWALL
:
1936 cstate
->off_linktype
.constant_part
= 6;
1937 cstate
->off_linkpl
.constant_part
= 44;
1938 cstate
->off_nl
= 0; /* Ethernet II */
1939 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1943 cstate
->off_linktype
.constant_part
= 0;
1944 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1945 cstate
->off_linkpl
.is_variable
= 1;
1947 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1950 case DLT_JUNIPER_MFR
:
1951 case DLT_JUNIPER_MLFR
:
1952 case DLT_JUNIPER_MLPPP
:
1953 case DLT_JUNIPER_PPP
:
1954 case DLT_JUNIPER_CHDLC
:
1955 case DLT_JUNIPER_FRELAY
:
1956 cstate
->off_linktype
.constant_part
= 4;
1957 cstate
->off_linkpl
.constant_part
= 4;
1959 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1962 case DLT_JUNIPER_ATM1
:
1963 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1964 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1966 cstate
->off_nl_nosnap
= 10;
1969 case DLT_JUNIPER_ATM2
:
1970 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1971 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1973 cstate
->off_nl_nosnap
= 10;
1976 /* frames captured on a Juniper PPPoE service PIC
1977 * contain raw ethernet frames */
1978 case DLT_JUNIPER_PPPOE
:
1979 case DLT_JUNIPER_ETHER
:
1980 cstate
->off_linkpl
.constant_part
= 14;
1981 cstate
->off_linktype
.constant_part
= 16;
1982 cstate
->off_nl
= 18; /* Ethernet II */
1983 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1986 case DLT_JUNIPER_PPPOE_ATM
:
1987 cstate
->off_linktype
.constant_part
= 4;
1988 cstate
->off_linkpl
.constant_part
= 6;
1990 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1993 case DLT_JUNIPER_GGSN
:
1994 cstate
->off_linktype
.constant_part
= 6;
1995 cstate
->off_linkpl
.constant_part
= 12;
1997 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2000 case DLT_JUNIPER_ES
:
2001 cstate
->off_linktype
.constant_part
= 6;
2002 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
2003 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
2004 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2007 case DLT_JUNIPER_MONITOR
:
2008 cstate
->off_linktype
.constant_part
= 12;
2009 cstate
->off_linkpl
.constant_part
= 12;
2010 cstate
->off_nl
= 0; /* raw IP/IP6 header */
2011 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2014 case DLT_BACNET_MS_TP
:
2015 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2016 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2017 cstate
->off_nl
= OFFSET_NOT_SET
;
2018 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2021 case DLT_JUNIPER_SERVICES
:
2022 cstate
->off_linktype
.constant_part
= 12;
2023 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2024 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2025 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2028 case DLT_JUNIPER_VP
:
2029 cstate
->off_linktype
.constant_part
= 18;
2030 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2031 cstate
->off_nl
= OFFSET_NOT_SET
;
2032 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2035 case DLT_JUNIPER_ST
:
2036 cstate
->off_linktype
.constant_part
= 18;
2037 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2038 cstate
->off_nl
= OFFSET_NOT_SET
;
2039 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2042 case DLT_JUNIPER_ISM
:
2043 cstate
->off_linktype
.constant_part
= 8;
2044 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2045 cstate
->off_nl
= OFFSET_NOT_SET
;
2046 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2049 case DLT_JUNIPER_VS
:
2050 case DLT_JUNIPER_SRX_E2E
:
2051 case DLT_JUNIPER_FIBRECHANNEL
:
2052 case DLT_JUNIPER_ATM_CEMIC
:
2053 cstate
->off_linktype
.constant_part
= 8;
2054 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2055 cstate
->off_nl
= OFFSET_NOT_SET
;
2056 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2061 cstate
->off_li_hsl
= 4;
2062 cstate
->off_sio
= 3;
2063 cstate
->off_opc
= 4;
2064 cstate
->off_dpc
= 4;
2065 cstate
->off_sls
= 7;
2066 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2067 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2068 cstate
->off_nl
= OFFSET_NOT_SET
;
2069 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2072 case DLT_MTP2_WITH_PHDR
:
2074 cstate
->off_li_hsl
= 8;
2075 cstate
->off_sio
= 7;
2076 cstate
->off_opc
= 8;
2077 cstate
->off_dpc
= 8;
2078 cstate
->off_sls
= 11;
2079 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2080 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2081 cstate
->off_nl
= OFFSET_NOT_SET
;
2082 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2086 cstate
->off_li
= 22;
2087 cstate
->off_li_hsl
= 24;
2088 cstate
->off_sio
= 23;
2089 cstate
->off_opc
= 24;
2090 cstate
->off_dpc
= 24;
2091 cstate
->off_sls
= 27;
2092 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2093 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2094 cstate
->off_nl
= OFFSET_NOT_SET
;
2095 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2099 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2100 cstate
->off_linkpl
.constant_part
= 4;
2102 cstate
->off_nl_nosnap
= 0;
2107 * Currently, only raw "link[N:M]" filtering is supported.
2109 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
2110 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2111 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
2112 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2116 cstate
->off_linktype
.constant_part
= 1;
2117 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2119 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2122 case DLT_NETANALYZER
:
2123 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
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+Ethernet header length */
2126 cstate
->off_nl
= 0; /* Ethernet II */
2127 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2130 case DLT_NETANALYZER_TRANSPARENT
:
2131 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2132 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2133 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2134 cstate
->off_nl
= 0; /* Ethernet II */
2135 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2140 * For values in the range in which we've assigned new
2141 * DLT_ values, only raw "link[N:M]" filtering is supported.
2143 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2144 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2145 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2146 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2147 cstate
->off_nl
= OFFSET_NOT_SET
;
2148 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2150 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2151 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2157 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2162 * Load a value relative to the specified absolute offset.
2164 static struct slist
*
2165 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2166 u_int offset
, u_int size
)
2168 struct slist
*s
, *s2
;
2170 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2173 * If "s" is non-null, it has code to arrange that the X register
2174 * contains the variable part of the absolute offset, so we
2175 * generate a load relative to that, with an offset of
2176 * abs_offset->constant_part + offset.
2178 * Otherwise, we can do an absolute load with an offset of
2179 * abs_offset->constant_part + offset.
2183 * "s" points to a list of statements that puts the
2184 * variable part of the absolute offset into the X register.
2185 * Do an indirect load, to use the X register as an offset.
2187 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2188 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2192 * There is no variable part of the absolute offset, so
2193 * just do an absolute load.
2195 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2196 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2202 * Load a value relative to the beginning of the specified header.
2204 static struct slist
*
2205 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2208 struct slist
*s
, *s2
;
2211 * Squelch warnings from compilers that *don't* assume that
2212 * offrel always has a valid enum value and therefore don't
2213 * assume that we'll always go through one of the case arms.
2215 * If we have a default case, compilers that *do* assume that
2216 * will then complain about the default case code being
2219 * Damned if you do, damned if you don't.
2226 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2231 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2234 case OR_PREVLINKHDR
:
2235 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2239 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2242 case OR_PREVMPLSHDR
:
2243 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2247 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2250 case OR_LINKPL_NOSNAP
:
2251 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2255 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2260 * Load the X register with the length of the IPv4 header
2261 * (plus the offset of the link-layer header, if it's
2262 * preceded by a variable-length header such as a radio
2263 * header), in bytes.
2265 s
= gen_loadx_iphdrlen(cstate
);
2268 * Load the item at {offset of the link-layer payload} +
2269 * {offset, relative to the start of the link-layer
2270 * payload, of the IPv4 header} + {length of the IPv4 header} +
2271 * {specified offset}.
2273 * If the offset of the link-layer payload is variable,
2274 * the variable part of that offset is included in the
2275 * value in the X register, and we include the constant
2276 * part in the offset of the load.
2278 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2279 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2284 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2291 * Generate code to load into the X register the sum of the length of
2292 * the IPv4 header and the variable part of the offset of the link-layer
2295 static struct slist
*
2296 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2298 struct slist
*s
, *s2
;
2300 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2303 * The offset of the link-layer payload has a variable
2304 * part. "s" points to a list of statements that put
2305 * the variable part of that offset into the X register.
2307 * The 4*([k]&0xf) addressing mode can't be used, as we
2308 * don't have a constant offset, so we have to load the
2309 * value in question into the A register and add to it
2310 * the value from the X register.
2312 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2313 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2315 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2318 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2323 * The A register now contains the length of the IP header.
2324 * We need to add to it the variable part of the offset of
2325 * the link-layer payload, which is still in the X
2326 * register, and move the result into the X register.
2328 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2329 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2332 * The offset of the link-layer payload is a constant,
2333 * so no code was generated to load the (nonexistent)
2334 * variable part of that offset.
2336 * This means we can use the 4*([k]&0xf) addressing
2337 * mode. Load the length of the IPv4 header, which
2338 * is at an offset of cstate->off_nl from the beginning of
2339 * the link-layer payload, and thus at an offset of
2340 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2341 * of the raw packet data, using that addressing mode.
2343 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2344 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2350 static struct block
*
2351 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2355 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2357 return gen_jmp(cstate
, BPF_JEQ
, 0, s
);
2360 static inline struct block
*
2361 gen_true(compiler_state_t
*cstate
)
2363 return gen_uncond(cstate
, 1);
2366 static inline struct block
*
2367 gen_false(compiler_state_t
*cstate
)
2369 return gen_uncond(cstate
, 0);
2373 * Generate code to match a particular packet type.
2375 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2376 * value, if <= ETHERMTU. We use that to determine whether to
2377 * match the type/length field or to check the type/length field for
2378 * a value <= ETHERMTU to see whether it's a type field and then do
2379 * the appropriate test.
2381 static struct block
*
2382 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2384 struct block
*b0
, *b1
;
2390 case LLCSAP_NETBEUI
:
2392 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2393 * so we check the DSAP and SSAP.
2395 * LLCSAP_IP checks for IP-over-802.2, rather
2396 * than IP-over-Ethernet or IP-over-SNAP.
2398 * XXX - should we check both the DSAP and the
2399 * SSAP, like this, or should we check just the
2400 * DSAP, as we do for other types <= ETHERMTU
2401 * (i.e., other SAP values)?
2403 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2404 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2412 * Ethernet_II frames, which are Ethernet
2413 * frames with a frame type of ETHERTYPE_IPX;
2415 * Ethernet_802.3 frames, which are 802.3
2416 * frames (i.e., the type/length field is
2417 * a length field, <= ETHERMTU, rather than
2418 * a type field) with the first two bytes
2419 * after the Ethernet/802.3 header being
2422 * Ethernet_802.2 frames, which are 802.3
2423 * frames with an 802.2 LLC header and
2424 * with the IPX LSAP as the DSAP in the LLC
2427 * Ethernet_SNAP frames, which are 802.3
2428 * frames with an LLC header and a SNAP
2429 * header and with an OUI of 0x000000
2430 * (encapsulated Ethernet) and a protocol
2431 * ID of ETHERTYPE_IPX in the SNAP header.
2433 * XXX - should we generate the same code both
2434 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2438 * This generates code to check both for the
2439 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2441 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2442 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2446 * Now we add code to check for SNAP frames with
2447 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2449 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2453 * Now we generate code to check for 802.3
2454 * frames in general.
2456 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2459 * Now add the check for 802.3 frames before the
2460 * check for Ethernet_802.2 and Ethernet_802.3,
2461 * as those checks should only be done on 802.3
2462 * frames, not on Ethernet frames.
2467 * Now add the check for Ethernet_II frames, and
2468 * do that before checking for the other frame
2471 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2475 case ETHERTYPE_ATALK
:
2476 case ETHERTYPE_AARP
:
2478 * EtherTalk (AppleTalk protocols on Ethernet link
2479 * layer) may use 802.2 encapsulation.
2483 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2484 * we check for an Ethernet type field less or equal than
2485 * 1500, which means it's an 802.3 length field.
2487 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2490 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2491 * SNAP packets with an organization code of
2492 * 0x080007 (Apple, for Appletalk) and a protocol
2493 * type of ETHERTYPE_ATALK (Appletalk).
2495 * 802.2-encapsulated ETHERTYPE_AARP packets are
2496 * SNAP packets with an organization code of
2497 * 0x000000 (encapsulated Ethernet) and a protocol
2498 * type of ETHERTYPE_AARP (Appletalk ARP).
2500 if (ll_proto
== ETHERTYPE_ATALK
)
2501 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2502 else /* ll_proto == ETHERTYPE_AARP */
2503 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2507 * Check for Ethernet encapsulation (Ethertalk
2508 * phase 1?); we just check for the Ethernet
2511 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2517 if (ll_proto
<= ETHERMTU
) {
2518 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2520 * This is an LLC SAP value, so the frames
2521 * that match would be 802.2 frames.
2522 * Check that the frame is an 802.2 frame
2523 * (i.e., that the length/type field is
2524 * a length field, <= ETHERMTU) and
2525 * then check the DSAP.
2527 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2528 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2532 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2534 * This is an Ethernet type, so compare
2535 * the length/type field with it (if
2536 * the frame is an 802.2 frame, the length
2537 * field will be <= ETHERMTU, and, as
2538 * "ll_proto" is > ETHERMTU, this test
2539 * will fail and the frame won't match,
2540 * which is what we want).
2542 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2547 static struct block
*
2548 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2551 * For DLT_NULL, the link-layer header is a 32-bit word
2552 * containing an AF_ value in *host* byte order, and for
2553 * DLT_ENC, the link-layer header begins with a 32-bit
2554 * word containing an AF_ value in host byte order.
2556 * In addition, if we're reading a saved capture file,
2557 * the host byte order in the capture may not be the
2558 * same as the host byte order on this machine.
2560 * For DLT_LOOP, the link-layer header is a 32-bit
2561 * word containing an AF_ value in *network* byte order.
2563 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2565 * The AF_ value is in host byte order, but the BPF
2566 * interpreter will convert it to network byte order.
2568 * If this is a save file, and it's from a machine
2569 * with the opposite byte order to ours, we byte-swap
2572 * Then we run it through "htonl()", and generate
2573 * code to compare against the result.
2575 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2576 ll_proto
= SWAPLONG(ll_proto
);
2577 ll_proto
= htonl(ll_proto
);
2579 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2583 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2584 * or IPv6 then we have an error.
2586 static struct block
*
2587 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2592 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2595 case ETHERTYPE_IPV6
:
2596 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2603 return gen_false(cstate
);
2607 * Generate code to match a particular packet type.
2609 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2610 * value, if <= ETHERMTU. We use that to determine whether to
2611 * match the type field or to check the type field for the special
2612 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2614 static struct block
*
2615 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2617 struct block
*b0
, *b1
;
2623 case LLCSAP_NETBEUI
:
2625 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2626 * so we check the DSAP and SSAP.
2628 * LLCSAP_IP checks for IP-over-802.2, rather
2629 * than IP-over-Ethernet or IP-over-SNAP.
2631 * XXX - should we check both the DSAP and the
2632 * SSAP, like this, or should we check just the
2633 * DSAP, as we do for other types <= ETHERMTU
2634 * (i.e., other SAP values)?
2636 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2637 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2643 * Ethernet_II frames, which are Ethernet
2644 * frames with a frame type of ETHERTYPE_IPX;
2646 * Ethernet_802.3 frames, which have a frame
2647 * type of LINUX_SLL_P_802_3;
2649 * Ethernet_802.2 frames, which are 802.3
2650 * frames with an 802.2 LLC header (i.e, have
2651 * a frame type of LINUX_SLL_P_802_2) and
2652 * with the IPX LSAP as the DSAP in the LLC
2655 * Ethernet_SNAP frames, which are 802.3
2656 * frames with an LLC header and a SNAP
2657 * header and with an OUI of 0x000000
2658 * (encapsulated Ethernet) and a protocol
2659 * ID of ETHERTYPE_IPX in the SNAP header.
2661 * First, do the checks on LINUX_SLL_P_802_2
2662 * frames; generate the check for either
2663 * Ethernet_802.2 or Ethernet_SNAP frames, and
2664 * then put a check for LINUX_SLL_P_802_2 frames
2667 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2668 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2670 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2674 * Now check for 802.3 frames and OR that with
2675 * the previous test.
2677 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2681 * Now add the check for Ethernet_II frames, and
2682 * do that before checking for the other frame
2685 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2689 case ETHERTYPE_ATALK
:
2690 case ETHERTYPE_AARP
:
2692 * EtherTalk (AppleTalk protocols on Ethernet link
2693 * layer) may use 802.2 encapsulation.
2697 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2698 * we check for the 802.2 protocol type in the
2699 * "Ethernet type" field.
2701 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2704 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2705 * SNAP packets with an organization code of
2706 * 0x080007 (Apple, for Appletalk) and a protocol
2707 * type of ETHERTYPE_ATALK (Appletalk).
2709 * 802.2-encapsulated ETHERTYPE_AARP packets are
2710 * SNAP packets with an organization code of
2711 * 0x000000 (encapsulated Ethernet) and a protocol
2712 * type of ETHERTYPE_AARP (Appletalk ARP).
2714 if (ll_proto
== ETHERTYPE_ATALK
)
2715 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2716 else /* ll_proto == ETHERTYPE_AARP */
2717 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2721 * Check for Ethernet encapsulation (Ethertalk
2722 * phase 1?); we just check for the Ethernet
2725 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2731 if (ll_proto
<= ETHERMTU
) {
2732 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2734 * This is an LLC SAP value, so the frames
2735 * that match would be 802.2 frames.
2736 * Check for the 802.2 protocol type
2737 * in the "Ethernet type" field, and
2738 * then check the DSAP.
2740 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2741 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2746 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2748 * This is an Ethernet type, so compare
2749 * the length/type field with it (if
2750 * the frame is an 802.2 frame, the length
2751 * field will be <= ETHERMTU, and, as
2752 * "ll_proto" is > ETHERMTU, this test
2753 * will fail and the frame won't match,
2754 * which is what we want).
2756 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2762 * Load a value relative to the beginning of the link-layer header after the
2765 static struct slist
*
2766 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2768 struct slist
*s1
, *s2
;
2771 * Generate code to load the length of the pflog header into
2772 * the register assigned to hold that length, if one has been
2773 * assigned. (If one hasn't been assigned, no code we've
2774 * generated uses that prefix, so we don't need to generate any
2777 if (cstate
->off_linkpl
.reg
!= -1) {
2779 * The length is in the first byte of the header.
2781 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2785 * Round it up to a multiple of 4.
2786 * Add 3, and clear the lower 2 bits.
2788 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2791 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2792 s2
->s
.k
= 0xfffffffc;
2796 * Now allocate a register to hold that value and store
2799 s2
= new_stmt(cstate
, BPF_ST
);
2800 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2804 * Now move it into the X register.
2806 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2814 static struct slist
*
2815 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2817 struct slist
*s1
, *s2
;
2818 struct slist
*sjeq_avs_cookie
;
2819 struct slist
*sjcommon
;
2822 * This code is not compatible with the optimizer, as
2823 * we are generating jmp instructions within a normal
2824 * slist of instructions
2826 cstate
->no_optimize
= 1;
2829 * Generate code to load the length of the radio header into
2830 * the register assigned to hold that length, if one has been
2831 * assigned. (If one hasn't been assigned, no code we've
2832 * generated uses that prefix, so we don't need to generate any
2835 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2836 * or always use the AVS header rather than the Prism header.
2837 * We load a 4-byte big-endian value at the beginning of the
2838 * raw packet data, and see whether, when masked with 0xFFFFF000,
2839 * it's equal to 0x80211000. If so, that indicates that it's
2840 * an AVS header (the masked-out bits are the version number).
2841 * Otherwise, it's a Prism header.
2843 * XXX - the Prism header is also, in theory, variable-length,
2844 * but no known software generates headers that aren't 144
2847 if (cstate
->off_linkhdr
.reg
!= -1) {
2851 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2855 * AND it with 0xFFFFF000.
2857 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2858 s2
->s
.k
= 0xFFFFF000;
2862 * Compare with 0x80211000.
2864 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2865 sjeq_avs_cookie
->s
.k
= 0x80211000;
2866 sappend(s1
, sjeq_avs_cookie
);
2871 * The 4 bytes at an offset of 4 from the beginning of
2872 * the AVS header are the length of the AVS header.
2873 * That field is big-endian.
2875 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2878 sjeq_avs_cookie
->s
.jt
= s2
;
2881 * Now jump to the code to allocate a register
2882 * into which to save the header length and
2883 * store the length there. (The "jump always"
2884 * instruction needs to have the k field set;
2885 * it's added to the PC, so, as we're jumping
2886 * over a single instruction, it should be 1.)
2888 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2890 sappend(s1
, sjcommon
);
2893 * Now for the code that handles the Prism header.
2894 * Just load the length of the Prism header (144)
2895 * into the A register. Have the test for an AVS
2896 * header branch here if we don't have an AVS header.
2898 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2901 sjeq_avs_cookie
->s
.jf
= s2
;
2904 * Now allocate a register to hold that value and store
2905 * it. The code for the AVS header will jump here after
2906 * loading the length of the AVS header.
2908 s2
= new_stmt(cstate
, BPF_ST
);
2909 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2911 sjcommon
->s
.jf
= s2
;
2914 * Now move it into the X register.
2916 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2924 static struct slist
*
2925 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2927 struct slist
*s1
, *s2
;
2930 * Generate code to load the length of the AVS header into
2931 * the register assigned to hold that length, if one has been
2932 * assigned. (If one hasn't been assigned, no code we've
2933 * generated uses that prefix, so we don't need to generate any
2936 if (cstate
->off_linkhdr
.reg
!= -1) {
2938 * The 4 bytes at an offset of 4 from the beginning of
2939 * the AVS header are the length of the AVS header.
2940 * That field is big-endian.
2942 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2946 * Now allocate a register to hold that value and store
2949 s2
= new_stmt(cstate
, BPF_ST
);
2950 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2954 * Now move it into the X register.
2956 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2964 static struct slist
*
2965 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2967 struct slist
*s1
, *s2
;
2970 * Generate code to load the length of the radiotap header into
2971 * the register assigned to hold that length, if one has been
2972 * assigned. (If one hasn't been assigned, no code we've
2973 * generated uses that prefix, so we don't need to generate any
2976 if (cstate
->off_linkhdr
.reg
!= -1) {
2978 * The 2 bytes at offsets of 2 and 3 from the beginning
2979 * of the radiotap header are the length of the radiotap
2980 * header; unfortunately, it's little-endian, so we have
2981 * to load it a byte at a time and construct the value.
2985 * Load the high-order byte, at an offset of 3, shift it
2986 * left a byte, and put the result in the X register.
2988 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2990 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2993 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2997 * Load the next byte, at an offset of 2, and OR the
2998 * value from the X register into it.
3000 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3003 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3007 * Now allocate a register to hold that value and store
3010 s2
= new_stmt(cstate
, BPF_ST
);
3011 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3015 * Now move it into the X register.
3017 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3026 * At the moment we treat PPI as normal Radiotap encoded
3027 * packets. The difference is in the function that generates
3028 * the code at the beginning to compute the header length.
3029 * Since this code generator of PPI supports bare 802.11
3030 * encapsulation only (i.e. the encapsulated DLT should be
3031 * DLT_IEEE802_11) we generate code to check for this too;
3032 * that's done in finish_parse().
3034 static struct slist
*
3035 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
3037 struct slist
*s1
, *s2
;
3040 * Generate code to load the length of the radiotap header
3041 * into the register assigned to hold that length, if one has
3044 if (cstate
->off_linkhdr
.reg
!= -1) {
3046 * The 2 bytes at offsets of 2 and 3 from the beginning
3047 * of the radiotap header are the length of the radiotap
3048 * header; unfortunately, it's little-endian, so we have
3049 * to load it a byte at a time and construct the value.
3053 * Load the high-order byte, at an offset of 3, shift it
3054 * left a byte, and put the result in the X register.
3056 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3058 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
3061 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3065 * Load the next byte, at an offset of 2, and OR the
3066 * value from the X register into it.
3068 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3071 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3075 * Now allocate a register to hold that value and store
3078 s2
= new_stmt(cstate
, BPF_ST
);
3079 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3083 * Now move it into the X register.
3085 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3094 * Load a value relative to the beginning of the link-layer header after the 802.11
3095 * header, i.e. LLC_SNAP.
3096 * The link-layer header doesn't necessarily begin at the beginning
3097 * of the packet data; there might be a variable-length prefix containing
3098 * radio information.
3100 static struct slist
*
3101 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
3104 struct slist
*sjset_data_frame_1
;
3105 struct slist
*sjset_data_frame_2
;
3106 struct slist
*sjset_qos
;
3107 struct slist
*sjset_radiotap_flags_present
;
3108 struct slist
*sjset_radiotap_ext_present
;
3109 struct slist
*sjset_radiotap_tsft_present
;
3110 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
3111 struct slist
*s_roundup
;
3113 if (cstate
->off_linkpl
.reg
== -1) {
3115 * No register has been assigned to the offset of
3116 * the link-layer payload, which means nobody needs
3117 * it; don't bother computing it - just return
3118 * what we already have.
3124 * This code is not compatible with the optimizer, as
3125 * we are generating jmp instructions within a normal
3126 * slist of instructions
3128 cstate
->no_optimize
= 1;
3131 * If "s" is non-null, it has code to arrange that the X register
3132 * contains the length of the prefix preceding the link-layer
3135 * Otherwise, the length of the prefix preceding the link-layer
3136 * header is "off_outermostlinkhdr.constant_part".
3140 * There is no variable-length header preceding the
3141 * link-layer header.
3143 * Load the length of the fixed-length prefix preceding
3144 * the link-layer header (if any) into the X register,
3145 * and store it in the cstate->off_linkpl.reg register.
3146 * That length is off_outermostlinkhdr.constant_part.
3148 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3149 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3153 * The X register contains the offset of the beginning of the
3154 * link-layer header; add 24, which is the minimum length
3155 * of the MAC header for a data frame, to that, and store it
3156 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3157 * which is at the offset in the X register, with an indexed load.
3159 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3161 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3164 s2
= new_stmt(cstate
, BPF_ST
);
3165 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3168 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3173 * Check the Frame Control field to see if this is a data frame;
3174 * a data frame has the 0x08 bit (b3) in that field set and the
3175 * 0x04 bit (b2) clear.
3177 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3178 sjset_data_frame_1
->s
.k
= IEEE80211_FC0_TYPE_DATA
;
3179 sappend(s
, sjset_data_frame_1
);
3182 * If b3 is set, test b2, otherwise go to the first statement of
3183 * the rest of the program.
3185 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3186 sjset_data_frame_2
->s
.k
= IEEE80211_FC0_TYPE_CTL
;
3187 sappend(s
, sjset_data_frame_2
);
3188 sjset_data_frame_1
->s
.jf
= snext
;
3191 * If b2 is not set, this is a data frame; test the QoS bit.
3192 * Otherwise, go to the first statement of the rest of the
3195 sjset_data_frame_2
->s
.jt
= snext
;
3196 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3197 sjset_qos
->s
.k
= IEEE80211_FC0_SUBTYPE_QOS
;
3198 sappend(s
, sjset_qos
);
3201 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3203 * Otherwise, go to the first statement of the rest of the
3206 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3207 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3209 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3212 s2
= new_stmt(cstate
, BPF_ST
);
3213 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3217 * If we have a radiotap header, look at it to see whether
3218 * there's Atheros padding between the MAC-layer header
3221 * Note: all of the fields in the radiotap header are
3222 * little-endian, so we byte-swap all of the values
3223 * we test against, as they will be loaded as big-endian
3226 * XXX - in the general case, we would have to scan through
3227 * *all* the presence bits, if there's more than one word of
3228 * presence bits. That would require a loop, meaning that
3229 * we wouldn't be able to run the filter in the kernel.
3231 * We assume here that the Atheros adapters that insert the
3232 * annoying padding don't have multiple antennae and therefore
3233 * do not generate radiotap headers with multiple presence words.
3235 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3237 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3238 * in the first presence flag word?
3240 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3244 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3245 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3246 sappend(s
, sjset_radiotap_flags_present
);
3249 * If not, skip all of this.
3251 sjset_radiotap_flags_present
->s
.jf
= snext
;
3254 * Otherwise, is the "extension" bit set in that word?
3256 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3257 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3258 sappend(s
, sjset_radiotap_ext_present
);
3259 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3262 * If so, skip all of this.
3264 sjset_radiotap_ext_present
->s
.jt
= snext
;
3267 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3269 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3270 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3271 sappend(s
, sjset_radiotap_tsft_present
);
3272 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3275 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3276 * at an offset of 16 from the beginning of the raw packet
3277 * data (8 bytes for the radiotap header and 8 bytes for
3280 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3283 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3286 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3288 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3289 sjset_tsft_datapad
->s
.k
= 0x20;
3290 sappend(s
, sjset_tsft_datapad
);
3293 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3294 * at an offset of 8 from the beginning of the raw packet
3295 * data (8 bytes for the radiotap header).
3297 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3300 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3303 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3305 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3306 sjset_notsft_datapad
->s
.k
= 0x20;
3307 sappend(s
, sjset_notsft_datapad
);
3310 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3311 * set, round the length of the 802.11 header to
3312 * a multiple of 4. Do that by adding 3 and then
3313 * dividing by and multiplying by 4, which we do by
3316 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3317 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3318 sappend(s
, s_roundup
);
3319 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3322 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3323 s2
->s
.k
= (bpf_u_int32
)~3;
3325 s2
= new_stmt(cstate
, BPF_ST
);
3326 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3329 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3330 sjset_tsft_datapad
->s
.jf
= snext
;
3331 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3332 sjset_notsft_datapad
->s
.jf
= snext
;
3334 sjset_qos
->s
.jf
= snext
;
3340 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3344 /* There is an implicit dependency between the link
3345 * payload and link header since the payload computation
3346 * includes the variable part of the header. Therefore,
3347 * if nobody else has allocated a register for the link
3348 * header and we need it, do it now. */
3349 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3350 cstate
->off_linkhdr
.reg
== -1)
3351 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3354 * For link-layer types that have a variable-length header
3355 * preceding the link-layer header, generate code to load
3356 * the offset of the link-layer header into the register
3357 * assigned to that offset, if any.
3359 * XXX - this, and the next switch statement, won't handle
3360 * encapsulation of 802.11 or 802.11+radio information in
3361 * some other protocol stack. That's significantly more
3364 switch (cstate
->outermostlinktype
) {
3366 case DLT_PRISM_HEADER
:
3367 s
= gen_load_prism_llprefixlen(cstate
);
3370 case DLT_IEEE802_11_RADIO_AVS
:
3371 s
= gen_load_avs_llprefixlen(cstate
);
3374 case DLT_IEEE802_11_RADIO
:
3375 s
= gen_load_radiotap_llprefixlen(cstate
);
3379 s
= gen_load_ppi_llprefixlen(cstate
);
3388 * For link-layer types that have a variable-length link-layer
3389 * header, generate code to load the offset of the link-layer
3390 * payload into the register assigned to that offset, if any.
3392 switch (cstate
->outermostlinktype
) {
3394 case DLT_IEEE802_11
:
3395 case DLT_PRISM_HEADER
:
3396 case DLT_IEEE802_11_RADIO_AVS
:
3397 case DLT_IEEE802_11_RADIO
:
3399 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3403 s
= gen_load_pflog_llprefixlen(cstate
);
3408 * If there is no initialization yet and we need variable
3409 * length offsets for VLAN, initialize them to zero
3411 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3414 if (cstate
->off_linkpl
.reg
== -1)
3415 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3416 if (cstate
->off_linktype
.reg
== -1)
3417 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3419 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3421 s2
= new_stmt(cstate
, BPF_ST
);
3422 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3424 s2
= new_stmt(cstate
, BPF_ST
);
3425 s2
->s
.k
= cstate
->off_linktype
.reg
;
3430 * If we have any offset-loading code, append all the
3431 * existing statements in the block to those statements,
3432 * and make the resulting list the list of statements
3436 sappend(s
, b
->stmts
);
3442 * Take an absolute offset, and:
3444 * if it has no variable part, return NULL;
3446 * if it has a variable part, generate code to load the register
3447 * containing that variable part into the X register, returning
3448 * a pointer to that code - if no register for that offset has
3449 * been allocated, allocate it first.
3451 * (The code to set that register will be generated later, but will
3452 * be placed earlier in the code sequence.)
3454 static struct slist
*
3455 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3459 if (off
->is_variable
) {
3460 if (off
->reg
== -1) {
3462 * We haven't yet assigned a register for the
3463 * variable part of the offset of the link-layer
3464 * header; allocate one.
3466 off
->reg
= alloc_reg(cstate
);
3470 * Load the register containing the variable part of the
3471 * offset of the link-layer header into the X register.
3473 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3478 * That offset isn't variable, there's no variable part,
3479 * so we don't need to generate any code.
3486 * Map an Ethernet type to the equivalent PPP type.
3489 ethertype_to_ppptype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3496 case ETHERTYPE_IPV6
:
3502 case ETHERTYPE_ATALK
:
3513 * I'm assuming the "Bridging PDU"s that go
3514 * over PPP are Spanning Tree Protocol
3522 assert_maxval(cstate
, "PPP protocol", ll_proto
, UINT16_MAX
);
3523 return (uint16_t)ll_proto
;
3527 * Generate any tests that, for encapsulation of a link-layer packet
3528 * inside another protocol stack, need to be done to check for those
3529 * link-layer packets (and that haven't already been done by a check
3530 * for that encapsulation).
3532 static struct block
*
3533 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3535 if (cstate
->is_encap
)
3536 return gen_encap_ll_check(cstate
);
3538 switch (cstate
->prevlinktype
) {
3542 * This is LANE-encapsulated Ethernet; check that the LANE
3543 * packet doesn't begin with an LE Control marker, i.e.
3544 * that it's data, not a control message.
3546 * (We've already generated a test for LANE.)
3548 return gen_cmp_ne(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3552 * No such tests are necessary.
3560 * The three different values we should check for when checking for an
3561 * IPv6 packet with DLT_NULL.
3563 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3564 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3565 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3568 * Generate code to match a particular packet type by matching the
3569 * link-layer type field or fields in the 802.2 LLC header.
3571 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3572 * value, if <= ETHERMTU.
3574 static struct block
*
3575 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3577 struct block
*b0
, *b1
, *b2
;
3579 /* are we checking MPLS-encapsulated packets? */
3580 if (cstate
->label_stack_depth
> 0)
3581 return gen_mpls_linktype(cstate
, ll_proto
);
3583 switch (cstate
->linktype
) {
3586 case DLT_NETANALYZER
:
3587 case DLT_NETANALYZER_TRANSPARENT
:
3588 /* Geneve has an EtherType regardless of whether there is an
3589 * L2 header. VXLAN always has an EtherType. */
3590 if (!cstate
->is_encap
)
3591 b0
= gen_prevlinkhdr_check(cstate
);
3595 b1
= gen_ether_linktype(cstate
, ll_proto
);
3603 assert_maxval(cstate
, "HDLC protocol", ll_proto
, UINT16_MAX
);
3607 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3611 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3615 case DLT_IEEE802_11
:
3616 case DLT_PRISM_HEADER
:
3617 case DLT_IEEE802_11_RADIO_AVS
:
3618 case DLT_IEEE802_11_RADIO
:
3621 * Check that we have a data frame.
3623 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3624 IEEE80211_FC0_TYPE_DATA
,
3625 IEEE80211_FC0_TYPE_MASK
);
3628 * Now check for the specified link-layer type.
3630 b1
= gen_llc_linktype(cstate
, ll_proto
);
3637 * XXX - check for LLC frames.
3639 return gen_llc_linktype(cstate
, ll_proto
);
3644 * XXX - check for LLC PDUs, as per IEEE 802.5.
3646 return gen_llc_linktype(cstate
, ll_proto
);
3649 case DLT_ATM_RFC1483
:
3651 case DLT_IP_OVER_FC
:
3652 return gen_llc_linktype(cstate
, ll_proto
);
3657 * Check for an LLC-encapsulated version of this protocol;
3658 * if we were checking for LANE, linktype would no longer
3661 * Check for LLC encapsulation and then check the protocol.
3663 b0
= gen_atm_prototype(cstate
, PT_LLC
);
3664 b1
= gen_llc_linktype(cstate
, ll_proto
);
3670 return gen_linux_sll_linktype(cstate
, ll_proto
);
3674 case DLT_SLIP_BSDOS
:
3677 * These types don't provide any type field; packets
3678 * are always IPv4 or IPv6.
3680 * XXX - for IPv4, check for a version number of 4, and,
3681 * for IPv6, check for a version number of 6?
3686 /* Check for a version number of 4. */
3687 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3689 case ETHERTYPE_IPV6
:
3690 /* Check for a version number of 6. */
3691 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3694 return gen_false(cstate
); /* always false */
3700 * Raw IPv4, so no type field.
3702 if (ll_proto
== ETHERTYPE_IP
)
3703 return gen_true(cstate
); /* always true */
3705 /* Checking for something other than IPv4; always false */
3706 return gen_false(cstate
);
3711 * Raw IPv6, so no type field.
3713 if (ll_proto
== ETHERTYPE_IPV6
)
3714 return gen_true(cstate
); /* always true */
3716 /* Checking for something other than IPv6; always false */
3717 return gen_false(cstate
);
3722 case DLT_PPP_SERIAL
:
3725 * We use Ethernet protocol types inside libpcap;
3726 * map them to the corresponding PPP protocol types.
3728 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3729 ethertype_to_ppptype(cstate
, ll_proto
));
3734 * We use Ethernet protocol types inside libpcap;
3735 * map them to the corresponding PPP protocol types.
3741 * Also check for Van Jacobson-compressed IP.
3742 * XXX - do this for other forms of PPP?
3744 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3745 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3747 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3752 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3753 ethertype_to_ppptype(cstate
, ll_proto
));
3763 return (gen_loopback_linktype(cstate
, AF_INET
));
3765 case ETHERTYPE_IPV6
:
3767 * AF_ values may, unfortunately, be platform-
3768 * dependent; AF_INET isn't, because everybody
3769 * used 4.2BSD's value, but AF_INET6 is, because
3770 * 4.2BSD didn't have a value for it (given that
3771 * IPv6 didn't exist back in the early 1980's),
3772 * and they all picked their own values.
3774 * This means that, if we're reading from a
3775 * savefile, we need to check for all the
3778 * If we're doing a live capture, we only need
3779 * to check for this platform's value; however,
3780 * Npcap uses 24, which isn't Windows's AF_INET6
3781 * value. (Given the multiple different values,
3782 * programs that read pcap files shouldn't be
3783 * checking for their platform's AF_INET6 value
3784 * anyway, they should check for all of the
3785 * possible values. and they might as well do
3786 * that even for live captures.)
3788 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3790 * Savefile - check for all three
3791 * possible IPv6 values.
3793 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3794 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3796 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3801 * Live capture, so we only need to
3802 * check for the value used on this
3807 * Npcap doesn't use Windows's AF_INET6,
3808 * as that collides with AF_IPX on
3809 * some BSDs (both have the value 23).
3810 * Instead, it uses 24.
3812 return (gen_loopback_linktype(cstate
, 24));
3815 return (gen_loopback_linktype(cstate
, AF_INET6
));
3816 #else /* AF_INET6 */
3818 * I guess this platform doesn't support
3819 * IPv6, so we just reject all packets.
3821 return gen_false(cstate
);
3822 #endif /* AF_INET6 */
3828 * Not a type on which we support filtering.
3829 * XXX - support those that have AF_ values
3830 * #defined on this platform, at least?
3832 return gen_false(cstate
);
3837 * af field is host byte order in contrast to the rest of
3840 if (ll_proto
== ETHERTYPE_IP
)
3841 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3843 else if (ll_proto
== ETHERTYPE_IPV6
)
3844 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3847 return gen_false(cstate
);
3851 case DLT_ARCNET_LINUX
:
3853 * XXX should we check for first fragment if the protocol
3859 return gen_false(cstate
);
3861 case ETHERTYPE_IPV6
:
3862 return (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
,
3874 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3876 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3881 case ETHERTYPE_REVARP
:
3882 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3885 case ETHERTYPE_ATALK
:
3886 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3893 case ETHERTYPE_ATALK
:
3894 return gen_true(cstate
);
3896 return gen_false(cstate
);
3902 * XXX - assumes a 2-byte Frame Relay header with
3903 * DLCI and flags. What if the address is longer?
3909 * Check for the special NLPID for IP.
3911 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3913 case ETHERTYPE_IPV6
:
3915 * Check for the special NLPID for IPv6.
3917 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3921 * Check for several OSI protocols.
3923 * Frame Relay packets typically have an OSI
3924 * NLPID at the beginning; we check for each
3927 * What we check for is the NLPID and a frame
3928 * control field of UI, i.e. 0x03 followed
3931 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3932 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3933 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3939 return gen_false(cstate
);
3944 break; // not implemented
3946 case DLT_JUNIPER_MFR
:
3947 case DLT_JUNIPER_MLFR
:
3948 case DLT_JUNIPER_MLPPP
:
3949 case DLT_JUNIPER_ATM1
:
3950 case DLT_JUNIPER_ATM2
:
3951 case DLT_JUNIPER_PPPOE
:
3952 case DLT_JUNIPER_PPPOE_ATM
:
3953 case DLT_JUNIPER_GGSN
:
3954 case DLT_JUNIPER_ES
:
3955 case DLT_JUNIPER_MONITOR
:
3956 case DLT_JUNIPER_SERVICES
:
3957 case DLT_JUNIPER_ETHER
:
3958 case DLT_JUNIPER_PPP
:
3959 case DLT_JUNIPER_FRELAY
:
3960 case DLT_JUNIPER_CHDLC
:
3961 case DLT_JUNIPER_VP
:
3962 case DLT_JUNIPER_ST
:
3963 case DLT_JUNIPER_ISM
:
3964 case DLT_JUNIPER_VS
:
3965 case DLT_JUNIPER_SRX_E2E
:
3966 case DLT_JUNIPER_FIBRECHANNEL
:
3967 case DLT_JUNIPER_ATM_CEMIC
:
3969 /* just lets verify the magic number for now -
3970 * on ATM we may have up to 6 different encapsulations on the wire
3971 * and need a lot of heuristics to figure out that the payload
3974 * FIXME encapsulation specific BPF_ filters
3976 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3978 case DLT_BACNET_MS_TP
:
3979 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3982 return gen_ipnet_linktype(cstate
, ll_proto
);
3984 case DLT_LINUX_IRDA
:
3987 case DLT_MTP2_WITH_PHDR
:
3990 case DLT_LINUX_LAPD
:
3991 case DLT_USB_FREEBSD
:
3993 case DLT_USB_LINUX_MMAPPED
:
3995 case DLT_BLUETOOTH_HCI_H4
:
3996 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3998 case DLT_CAN_SOCKETCAN
:
3999 case DLT_IEEE802_15_4
:
4000 case DLT_IEEE802_15_4_LINUX
:
4001 case DLT_IEEE802_15_4_NONASK_PHY
:
4002 case DLT_IEEE802_15_4_NOFCS
:
4003 case DLT_IEEE802_15_4_TAP
:
4004 case DLT_IEEE802_16_MAC_CPS_RADIO
:
4007 case DLT_IPMB_KONTRON
:
4011 /* Using the fixed-size NFLOG header it is possible to tell only
4012 * the address family of the packet, other meaningful data is
4013 * either missing or behind TLVs.
4015 break; // not implemented
4019 * Does this link-layer header type have a field
4020 * indicating the type of the next protocol? If
4021 * so, off_linktype.constant_part will be the offset of that
4022 * field in the packet; if not, it will be OFFSET_NOT_SET.
4024 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
4026 * Yes; assume it's an Ethernet type. (If
4027 * it's not, it needs to be handled specially
4030 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4031 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
4035 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
4036 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
4040 * Check for an LLC SNAP packet with a given organization code and
4041 * protocol type; we check the entire contents of the 802.2 LLC and
4042 * snap headers, checking for DSAP and SSAP of SNAP and a control
4043 * field of 0x03 in the LLC header, and for the specified organization
4044 * code and protocol type in the SNAP header.
4046 static struct block
*
4047 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
4049 u_char snapblock
[8];
4051 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
4052 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
4053 snapblock
[2] = 0x03; /* control = UI */
4054 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
4055 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
4056 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
4057 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
4058 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
4059 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
4063 * Generate code to match frames with an LLC header.
4065 static struct block
*
4066 gen_llc_internal(compiler_state_t
*cstate
)
4068 struct block
*b0
, *b1
;
4070 switch (cstate
->linktype
) {
4074 * We check for an Ethernet type field less or equal than
4075 * 1500, which means it's an 802.3 length field.
4077 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
4080 * Now check for the purported DSAP and SSAP not being
4081 * 0xFF, to rule out NetWare-over-802.3.
4083 b1
= gen_cmp_ne(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
4089 * We check for LLC traffic.
4091 return gen_atmtype_llc(cstate
);
4093 case DLT_IEEE802
: /* Token Ring */
4095 * XXX - check for LLC frames.
4097 return gen_true(cstate
);
4101 * XXX - check for LLC frames.
4103 return gen_true(cstate
);
4105 case DLT_ATM_RFC1483
:
4107 * For LLC encapsulation, these are defined to have an
4110 * For VC encapsulation, they don't, but there's no
4111 * way to check for that; the protocol used on the VC
4112 * is negotiated out of band.
4114 return gen_true(cstate
);
4116 case DLT_IEEE802_11
:
4117 case DLT_PRISM_HEADER
:
4118 case DLT_IEEE802_11_RADIO
:
4119 case DLT_IEEE802_11_RADIO_AVS
:
4122 * Check that we have a data frame.
4124 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4125 IEEE80211_FC0_TYPE_DATA
,
4126 IEEE80211_FC0_TYPE_MASK
);
4129 fail_kw_on_dlt(cstate
, "llc");
4135 gen_llc(compiler_state_t
*cstate
)
4138 * Catch errors reported by us and routines below us, and return NULL
4141 if (setjmp(cstate
->top_ctx
))
4144 return gen_llc_internal(cstate
);
4148 gen_llc_i(compiler_state_t
*cstate
)
4150 struct block
*b0
, *b1
;
4154 * Catch errors reported by us and routines below us, and return NULL
4157 if (setjmp(cstate
->top_ctx
))
4161 * Check whether this is an LLC frame.
4163 b0
= gen_llc_internal(cstate
);
4166 * Load the control byte and test the low-order bit; it must
4167 * be clear for I frames.
4169 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4170 b1
= gen_unset(cstate
, 0x01, s
);
4177 gen_llc_s(compiler_state_t
*cstate
)
4179 struct block
*b0
, *b1
;
4182 * Catch errors reported by us and routines below us, and return NULL
4185 if (setjmp(cstate
->top_ctx
))
4189 * Check whether this is an LLC frame.
4191 b0
= gen_llc_internal(cstate
);
4194 * Now compare the low-order 2 bit of the control byte against
4195 * the appropriate value for S frames.
4197 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4203 gen_llc_u(compiler_state_t
*cstate
)
4205 struct block
*b0
, *b1
;
4208 * Catch errors reported by us and routines below us, and return NULL
4211 if (setjmp(cstate
->top_ctx
))
4215 * Check whether this is an LLC frame.
4217 b0
= gen_llc_internal(cstate
);
4220 * Now compare the low-order 2 bit of the control byte against
4221 * the appropriate value for U frames.
4223 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4229 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4231 struct block
*b0
, *b1
;
4234 * Catch errors reported by us and routines below us, and return NULL
4237 if (setjmp(cstate
->top_ctx
))
4241 * Check whether this is an LLC frame.
4243 b0
= gen_llc_internal(cstate
);
4246 * Now check for an S frame with the appropriate type.
4248 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4254 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4256 struct block
*b0
, *b1
;
4259 * Catch errors reported by us and routines below us, and return NULL
4262 if (setjmp(cstate
->top_ctx
))
4266 * Check whether this is an LLC frame.
4268 b0
= gen_llc_internal(cstate
);
4271 * Now check for a U frame with the appropriate type.
4273 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4279 * Generate code to match a particular packet type, for link-layer types
4280 * using 802.2 LLC headers.
4282 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4283 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4285 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4286 * value, if <= ETHERMTU. We use that to determine whether to
4287 * match the DSAP or both DSAP and LSAP or to check the OUI and
4288 * protocol ID in a SNAP header.
4290 static struct block
*
4291 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4294 * XXX - handle token-ring variable-length header.
4300 case LLCSAP_NETBEUI
:
4302 * XXX - should we check both the DSAP and the
4303 * SSAP, like this, or should we check just the
4304 * DSAP, as we do for other SAP values?
4306 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4307 ((ll_proto
<< 8) | ll_proto
));
4311 * XXX - are there ever SNAP frames for IPX on
4312 * non-Ethernet 802.x networks?
4314 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4316 case ETHERTYPE_ATALK
:
4318 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4319 * SNAP packets with an organization code of
4320 * 0x080007 (Apple, for Appletalk) and a protocol
4321 * type of ETHERTYPE_ATALK (Appletalk).
4323 * XXX - check for an organization code of
4324 * encapsulated Ethernet as well?
4326 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4330 * XXX - we don't have to check for IPX 802.3
4331 * here, but should we check for the IPX Ethertype?
4333 if (ll_proto
<= ETHERMTU
) {
4334 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
4336 * This is an LLC SAP value, so check
4339 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4341 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4343 * This is an Ethernet type; we assume that it's
4344 * unlikely that it'll appear in the right place
4345 * at random, and therefore check only the
4346 * location that would hold the Ethernet type
4347 * in a SNAP frame with an organization code of
4348 * 0x000000 (encapsulated Ethernet).
4350 * XXX - if we were to check for the SNAP DSAP and
4351 * LSAP, as per XXX, and were also to check for an
4352 * organization code of 0x000000 (encapsulated
4353 * Ethernet), we'd do
4355 * return gen_snap(cstate, 0x000000, ll_proto);
4357 * here; for now, we don't, as per the above.
4358 * I don't know whether it's worth the extra CPU
4359 * time to do the right check or not.
4361 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4366 static struct block
*
4367 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4368 int dir
, u_int src_off
, u_int dst_off
)
4370 struct block
*b0
, *b1
;
4384 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4385 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4391 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4392 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4402 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4409 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4413 static struct block
*
4414 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4415 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4417 struct block
*b0
, *b1
;
4420 * Code below needs to access four separate 32-bit parts of the 128-bit
4421 * IPv6 address and mask. In some OSes this is as simple as using the
4422 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4423 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4424 * far as libpcap sees it. Hence copy the data before use to avoid
4425 * potential unaligned memory access and the associated compiler
4426 * warnings (whether genuine or not).
4428 bpf_u_int32 a
[4], m
[4];
4441 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4442 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4448 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4449 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4459 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4466 /* this order is important */
4467 memcpy(a
, addr
, sizeof(a
));
4468 memcpy(m
, mask
, sizeof(m
));
4470 for (int i
= 3; i
>= 0; i
--) {
4471 // Same as the Q_IP case in gen_host().
4472 if (m
[i
] == 0 && a
[i
] == 0)
4474 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4 * i
, BPF_W
,
4475 ntohl(a
[i
]), ntohl(m
[i
]));
4481 return b1
? b1
: gen_true(cstate
);
4486 * Like gen_mac48host(), but for DLT_IEEE802_11 (802.11 wireless LAN) and
4487 * various 802.11 + radio headers.
4489 static struct block
*
4490 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4492 register struct block
*b0
, *b1
, *b2
;
4493 register struct slist
*s
;
4495 #ifdef ENABLE_WLAN_FILTERING_PATCH
4498 * We need to disable the optimizer because the optimizer is buggy
4499 * and wipes out some LD instructions generated by the below
4500 * code to validate the Frame Control bits
4502 cstate
->no_optimize
= 1;
4503 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4510 * For control frames, there is no SA.
4512 * For management frames, SA is at an
4513 * offset of 10 from the beginning of
4516 * For data frames, SA is at an offset
4517 * of 10 from the beginning of the packet
4518 * if From DS is clear, at an offset of
4519 * 16 from the beginning of the packet
4520 * if From DS is set and To DS is clear,
4521 * and an offset of 24 from the beginning
4522 * of the packet if From DS is set and To DS
4527 * Generate the tests to be done for data frames
4530 * First, check for To DS set, i.e. check "link[1] & 0x01".
4532 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4533 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4536 * If To DS is set, the SA is at 24.
4538 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4542 * Now, check for To DS not set, i.e. check
4543 * "!(link[1] & 0x01)".
4545 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4546 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4549 * If To DS is not set, the SA is at 16.
4551 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4555 * Now OR together the last two checks. That gives
4556 * the complete set of checks for data frames with
4562 * Now check for From DS being set, and AND that with
4563 * the ORed-together checks.
4565 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4566 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4570 * Now check for data frames with From DS not set.
4572 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4573 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4576 * If From DS isn't set, the SA is at 10.
4578 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4582 * Now OR together the checks for data frames with
4583 * From DS not set and for data frames with From DS
4584 * set; that gives the checks done for data frames.
4589 * Now check for a data frame.
4590 * I.e, check "link[0] & 0x08".
4592 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4593 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4596 * AND that with the checks done for data frames.
4601 * If the high-order bit of the type value is 0, this
4602 * is a management frame.
4603 * I.e, check "!(link[0] & 0x08)".
4605 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4606 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4609 * For management frames, the SA is at 10.
4611 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4615 * OR that with the checks done for data frames.
4616 * That gives the checks done for management and
4622 * If the low-order bit of the type value is 1,
4623 * this is either a control frame or a frame
4624 * with a reserved type, and thus not a
4627 * I.e., check "!(link[0] & 0x04)".
4629 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4630 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4633 * AND that with the checks for data and management
4643 * For control frames, there is no DA.
4645 * For management frames, DA is at an
4646 * offset of 4 from the beginning of
4649 * For data frames, DA is at an offset
4650 * of 4 from the beginning of the packet
4651 * if To DS is clear and at an offset of
4652 * 16 from the beginning of the packet
4657 * Generate the tests to be done for data frames.
4659 * First, check for To DS set, i.e. "link[1] & 0x01".
4661 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4662 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4665 * If To DS is set, the DA is at 16.
4667 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4671 * Now, check for To DS not set, i.e. check
4672 * "!(link[1] & 0x01)".
4674 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4675 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4678 * If To DS is not set, the DA is at 4.
4680 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4684 * Now OR together the last two checks. That gives
4685 * the complete set of checks for data frames.
4690 * Now check for a data frame.
4691 * I.e, check "link[0] & 0x08".
4693 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4694 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4697 * AND that with the checks done for data frames.
4702 * If the high-order bit of the type value is 0, this
4703 * is a management frame.
4704 * I.e, check "!(link[0] & 0x08)".
4706 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4707 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4710 * For management frames, the DA is at 4.
4712 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4716 * OR that with the checks done for data frames.
4717 * That gives the checks done for management and
4723 * If the low-order bit of the type value is 1,
4724 * this is either a control frame or a frame
4725 * with a reserved type, and thus not a
4728 * I.e., check "!(link[0] & 0x04)".
4730 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4731 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4734 * AND that with the checks for data and management
4741 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4742 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4748 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4749 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4754 * XXX - add BSSID keyword?
4757 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4761 * Not present in CTS or ACK control frames.
4763 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4764 IEEE80211_FC0_TYPE_MASK
);
4765 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4766 IEEE80211_FC0_SUBTYPE_MASK
);
4767 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4768 IEEE80211_FC0_SUBTYPE_MASK
);
4771 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4777 * Not present in control frames.
4779 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4780 IEEE80211_FC0_TYPE_MASK
);
4781 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4787 * Present only if the direction mask has both "From DS"
4788 * and "To DS" set. Neither control frames nor management
4789 * frames should have both of those set, so we don't
4790 * check the frame type.
4792 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4793 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4794 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4800 * Not present in management frames; addr1 in other
4805 * If the high-order bit of the type value is 0, this
4806 * is a management frame.
4807 * I.e, check "(link[0] & 0x08)".
4809 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4810 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4815 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4818 * AND that with the check of addr1.
4825 * Not present in management frames; addr2, if present,
4830 * Not present in CTS or ACK control frames.
4832 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4833 IEEE80211_FC0_TYPE_MASK
);
4834 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4835 IEEE80211_FC0_SUBTYPE_MASK
);
4836 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4837 IEEE80211_FC0_SUBTYPE_MASK
);
4842 * If the high-order bit of the type value is 0, this
4843 * is a management frame.
4844 * I.e, check "(link[0] & 0x08)".
4846 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4847 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4850 * AND that with the check for frames other than
4851 * CTS and ACK frames.
4858 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4867 * This is quite tricky because there may be pad bytes in front of the
4868 * DECNET header, and then there are two possible data packet formats that
4869 * carry both src and dst addresses, plus 5 packet types in a format that
4870 * carries only the src node, plus 2 types that use a different format and
4871 * also carry just the src node.
4875 * Instead of doing those all right, we just look for data packets with
4876 * 0 or 1 bytes of padding. If you want to look at other packets, that
4877 * will require a lot more hacking.
4879 * To add support for filtering on DECNET "areas" (network numbers)
4880 * one would want to add a "mask" argument to this routine. That would
4881 * make the filter even more inefficient, although one could be clever
4882 * and not generate masking instructions if the mask is 0xFFFF.
4884 static struct block
*
4885 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4887 struct block
*b0
, *b1
, *b2
, *tmp
;
4888 u_int offset_lh
; /* offset if long header is received */
4889 u_int offset_sh
; /* offset if short header is received */
4894 offset_sh
= 1; /* follows flags */
4895 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4899 offset_sh
= 3; /* follows flags, dstnode */
4900 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4904 /* Inefficient because we do our Calvinball dance twice */
4905 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4906 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4912 /* Inefficient because we do our Calvinball dance twice */
4913 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4914 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4924 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4932 * In a DECnet message inside an Ethernet frame the first two bytes
4933 * immediately after EtherType are the [litle-endian] DECnet message
4934 * length, which is irrelevant in this context.
4936 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
4937 * 8-bit bitmap of the optional padding before the packet route header.
4938 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
4939 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
4940 * means there aren't any PAD bytes after the bitmap, so the header
4941 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
4942 * is set to 0, thus the header begins at the third byte.
4944 * The header can be in several (as mentioned above) formats, all of
4945 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
4946 * (PF, "pad field") set to 0 regardless of any padding present before
4947 * the header. "Short header" means bits 0-2 of the bitmap encode the
4948 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
4950 * To test PLENGTH and FLAGS, use multiple-byte constants with the
4951 * values and the masks, this maps to the required single bytes of
4952 * the message correctly on both big-endian and little-endian hosts.
4953 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
4954 * because the wire encoding is little-endian and BPF multiple-byte
4955 * loads are big-endian. When the destination address is near enough
4956 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
4959 /* Check for pad = 1, long header case */
4960 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
4961 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4962 BPF_H
, SWAPSHORT(addr
));
4964 /* Check for pad = 0, long header case */
4965 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
4966 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4970 /* Check for pad = 1, short header case */
4972 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
4973 0x81020000U
| SWAPSHORT(addr
),
4976 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
4977 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4982 /* Check for pad = 0, short header case */
4984 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
4985 0x02000000U
| SWAPSHORT(addr
) << 8,
4988 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
4989 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4999 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5000 * test the bottom-of-stack bit, and then check the version number
5001 * field in the IP header.
5003 static struct block
*
5004 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5006 struct block
*b0
, *b1
;
5011 /* match the bottom-of-stack bit */
5012 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5013 /* match the IPv4 version number */
5014 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5018 case ETHERTYPE_IPV6
:
5019 /* match the bottom-of-stack bit */
5020 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5021 /* match the IPv6 version number */
5022 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5027 /* FIXME add other L3 proto IDs */
5028 bpf_error(cstate
, "unsupported protocol over mpls");
5033 static struct block
*
5034 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5035 int proto
, int dir
, int type
)
5037 struct block
*b0
, *b1
;
5042 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5044 * Only check for non-IPv4 addresses if we're not
5045 * checking MPLS-encapsulated packets.
5047 if (cstate
->label_stack_depth
== 0) {
5048 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5050 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5056 // "link net NETNAME" and variations thereof
5057 break; // invalid qualifier
5060 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5062 * Belt and braces: if other code works correctly, any host
5063 * bits are clear and mask == 0 means addr == 0. In this case
5064 * the call to gen_hostop() would produce an "always true"
5065 * instruction block and ANDing it with the link type check
5068 if (mask
== 0 && addr
== 0)
5070 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5075 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5076 // Same as for Q_IP above.
5077 if (mask
== 0 && addr
== 0)
5079 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5084 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5085 // Same as for Q_IP above.
5086 if (mask
== 0 && addr
== 0)
5088 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5099 break; // invalid qualifier
5102 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5103 b1
= gen_dnhostop(cstate
, addr
, dir
);
5134 break; // invalid qualifier
5139 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5140 type
== Q_NET
? "ip net" : "ip host");
5145 static struct block
*
5146 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5147 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5149 struct block
*b0
, *b1
;
5155 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5156 // Same as the Q_IP case in gen_host().
5158 ! memcmp(mask
, &in6addr_any
, sizeof(struct in6_addr
)) &&
5159 ! memcmp(addr
, &in6addr_any
, sizeof(struct in6_addr
))
5162 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5204 break; // invalid qualifier
5209 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5210 type
== Q_NET
? "ip6 net" : "ip6 host");
5215 static unsigned char
5216 is_mac48_linktype(const int linktype
)
5222 case DLT_IEEE802_11
:
5223 case DLT_IEEE802_11_RADIO
:
5224 case DLT_IEEE802_11_RADIO_AVS
:
5225 case DLT_IP_OVER_FC
:
5226 case DLT_NETANALYZER
:
5227 case DLT_NETANALYZER_TRANSPARENT
:
5229 case DLT_PRISM_HEADER
:
5236 static struct block
*
5237 gen_mac48host(compiler_state_t
*cstate
, const u_char
*eaddr
, const u_char dir
,
5238 const char *keyword
)
5240 struct block
*b1
= NULL
;
5241 u_int src_off
, dst_off
;
5243 switch (cstate
->linktype
) {
5245 case DLT_NETANALYZER
:
5246 case DLT_NETANALYZER_TRANSPARENT
:
5247 b1
= gen_prevlinkhdr_check(cstate
);
5252 src_off
= 6 + 1 + cstate
->pcap_fddipad
;
5253 dst_off
= 0 + 1 + cstate
->pcap_fddipad
;
5259 case DLT_IEEE802_11
:
5260 case DLT_PRISM_HEADER
:
5261 case DLT_IEEE802_11_RADIO_AVS
:
5262 case DLT_IEEE802_11_RADIO
:
5264 return gen_wlanhostop(cstate
, eaddr
, dir
);
5265 case DLT_IP_OVER_FC
:
5267 * Assume that the addresses are IEEE 48-bit MAC addresses,
5268 * as RFC 2625 states.
5275 * This is LLC-multiplexed traffic; if it were
5276 * LANE, cstate->linktype would have been set to
5281 fail_kw_on_dlt(cstate
, keyword
);
5284 struct block
*b0
, *tmp
;
5288 b0
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5291 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5294 tmp
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5295 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5300 tmp
= gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, eaddr
);
5301 b0
= gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, eaddr
);
5305 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5315 * This primitive is non-directional by design, so the grammar does not allow
5316 * to qualify it with a direction.
5318 static struct block
*
5319 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5320 struct addrinfo
*alist
, int proto
)
5322 struct block
*b0
, *b1
, *tmp
;
5323 struct addrinfo
*ai
;
5324 struct sockaddr_in
*sin
;
5331 b0
= gen_mac48host(cstate
, eaddr
, Q_OR
, "gateway");
5333 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5335 * Does it have an address?
5337 if (ai
->ai_addr
!= NULL
) {
5339 * Yes. Is it an IPv4 address?
5341 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5343 * Generate an entry for it.
5345 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5346 tmp
= gen_host(cstate
,
5347 ntohl(sin
->sin_addr
.s_addr
),
5348 0xffffffff, proto
, Q_OR
, Q_HOST
);
5350 * Is it the *first* IPv4 address?
5354 * Yes, so start with it.
5359 * No, so OR it into the
5371 * No IPv4 addresses found.
5379 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5384 static struct block
*
5385 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5393 return gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
);
5396 return gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
);
5399 return gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
);
5402 return gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
);
5404 #ifndef IPPROTO_IGMP
5405 #define IPPROTO_IGMP 2
5409 return gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
);
5411 #ifndef IPPROTO_IGRP
5412 #define IPPROTO_IGRP 9
5415 return gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
);
5418 #define IPPROTO_PIM 103
5422 return gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
);
5424 #ifndef IPPROTO_VRRP
5425 #define IPPROTO_VRRP 112
5429 return gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
);
5431 #ifndef IPPROTO_CARP
5432 #define IPPROTO_CARP 112
5436 return gen_proto(cstate
, IPPROTO_CARP
, Q_IP
);
5439 return gen_linktype(cstate
, ETHERTYPE_IP
);
5442 return gen_linktype(cstate
, ETHERTYPE_ARP
);
5445 return gen_linktype(cstate
, ETHERTYPE_REVARP
);
5448 break; // invalid syntax
5451 return gen_linktype(cstate
, ETHERTYPE_ATALK
);
5454 return gen_linktype(cstate
, ETHERTYPE_AARP
);
5457 return gen_linktype(cstate
, ETHERTYPE_DN
);
5460 return gen_linktype(cstate
, ETHERTYPE_SCA
);
5463 return gen_linktype(cstate
, ETHERTYPE_LAT
);
5466 return gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5469 return gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5472 return gen_linktype(cstate
, ETHERTYPE_IPV6
);
5474 #ifndef IPPROTO_ICMPV6
5475 #define IPPROTO_ICMPV6 58
5478 return gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
);
5481 #define IPPROTO_AH 51
5484 return gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
);
5487 #define IPPROTO_ESP 50
5490 return gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
);
5493 return gen_linktype(cstate
, LLCSAP_ISONS
);
5496 return gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
);
5499 return gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
5501 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5502 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5503 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5505 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5507 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5509 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5513 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5514 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5515 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5517 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5519 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5521 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5525 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5526 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5527 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5529 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
);
5534 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5535 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5540 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5541 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5543 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5545 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5550 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5551 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5556 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5557 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5562 return gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
);
5565 return gen_linktype(cstate
, LLCSAP_8021D
);
5568 return gen_linktype(cstate
, LLCSAP_IPX
);
5571 return gen_linktype(cstate
, LLCSAP_NETBEUI
);
5574 break; // invalid syntax
5579 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5583 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5586 * Catch errors reported by us and routines below us, and return NULL
5589 if (setjmp(cstate
->top_ctx
))
5592 return gen_proto_abbrev_internal(cstate
, proto
);
5595 static struct block
*
5596 gen_ip_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5598 return gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5601 static struct block
*
5602 gen_ip6_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5604 return gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5607 static struct block
*
5608 gen_ipfrag(compiler_state_t
*cstate
)
5612 /* not IPv4 frag other than the first frag */
5613 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5614 return gen_unset(cstate
, 0x1fff, s
);
5618 * Generate a comparison to a port value in the transport-layer header
5619 * at the specified offset from the beginning of that header.
5621 * XXX - this handles a variable-length prefix preceding the link-layer
5622 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5623 * variable-length link-layer headers (such as Token Ring or 802.11
5626 static struct block
*
5627 gen_portatom(compiler_state_t
*cstate
, int off
, uint16_t v
)
5629 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5632 static struct block
*
5633 gen_portatom6(compiler_state_t
*cstate
, int off
, uint16_t v
)
5635 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5638 static struct block
*
5639 gen_port(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5641 struct block
*b1
, *tmp
;
5645 b1
= gen_portatom(cstate
, 0, port
);
5649 b1
= gen_portatom(cstate
, 2, port
);
5653 tmp
= gen_portatom(cstate
, 0, port
);
5654 b1
= gen_portatom(cstate
, 2, port
);
5660 tmp
= gen_portatom(cstate
, 0, port
);
5661 b1
= gen_portatom(cstate
, 2, port
);
5671 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5679 return gen_port_common(cstate
, proto
, b1
);
5682 static struct block
*
5683 gen_port_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5685 struct block
*b0
, *tmp
;
5690 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5691 * not LLC encapsulation with LLCSAP_IP.
5693 * For IEEE 802 networks - which includes 802.5 token ring
5694 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5695 * says that SNAP encapsulation is used, not LLC encapsulation
5698 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5699 * RFC 2225 say that SNAP encapsulation is used, not LLC
5700 * encapsulation with LLCSAP_IP.
5702 * So we always check for ETHERTYPE_IP.
5704 * At the time of this writing all three L4 protocols the "port" and
5705 * "portrange" primitives support (TCP, UDP and SCTP) have the source
5706 * and the destination ports identically encoded in the transport
5707 * protocol header. So without a proto qualifier the only difference
5708 * between the implemented cases is the protocol number and all other
5709 * checks need to be made exactly once.
5711 * If the expression syntax in future starts to support ports for
5712 * another L4 protocol that has unsigned integer ports encoded using a
5713 * different size and/or offset, this will require a different code.
5719 tmp
= gen_ip_proto(cstate
, (uint8_t)proto
);
5723 tmp
= gen_ip_proto(cstate
, IPPROTO_UDP
);
5724 gen_or(gen_ip_proto(cstate
, IPPROTO_TCP
), tmp
);
5725 gen_or(gen_ip_proto(cstate
, IPPROTO_SCTP
), tmp
);
5731 // Not a fragment other than the first fragment.
5732 b0
= gen_ipfrag(cstate
);
5736 gen_and(gen_linktype(cstate
, ETHERTYPE_IP
), b1
);
5740 static struct block
*
5741 gen_port6(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5743 struct block
*b1
, *tmp
;
5747 b1
= gen_portatom6(cstate
, 0, port
);
5751 b1
= gen_portatom6(cstate
, 2, port
);
5755 tmp
= gen_portatom6(cstate
, 0, port
);
5756 b1
= gen_portatom6(cstate
, 2, port
);
5762 tmp
= gen_portatom6(cstate
, 0, port
);
5763 b1
= gen_portatom6(cstate
, 2, port
);
5771 return gen_port6_common(cstate
, proto
, b1
);
5774 static struct block
*
5775 gen_port6_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5779 // "ip6 proto 'ip_proto'"
5784 tmp
= gen_ip6_proto(cstate
, (uint8_t)proto
);
5788 // Same as in gen_port_common().
5789 tmp
= gen_ip6_proto(cstate
, IPPROTO_UDP
);
5790 gen_or(gen_ip6_proto(cstate
, IPPROTO_TCP
), tmp
);
5791 gen_or(gen_ip6_proto(cstate
, IPPROTO_SCTP
), tmp
);
5797 // XXX - catch the first fragment of a fragmented packet?
5799 // "link proto \ip6"
5800 gen_and(gen_linktype(cstate
, ETHERTYPE_IPV6
), b1
);
5804 /* gen_portrange code */
5805 static struct block
*
5806 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5810 return gen_portatom(cstate
, off
, v1
);
5812 struct block
*b1
, *b2
;
5814 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, min(v1
, v2
));
5815 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, max(v1
, v2
));
5822 static struct block
*
5823 gen_portrange(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5826 struct block
*b1
, *tmp
;
5830 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5834 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5838 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5839 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5845 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5846 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5856 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5864 return gen_port_common(cstate
, proto
, b1
);
5867 static struct block
*
5868 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5872 return gen_portatom6(cstate
, off
, v1
);
5874 struct block
*b1
, *b2
;
5876 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, min(v1
, v2
));
5877 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, max(v1
, v2
));
5884 static struct block
*
5885 gen_portrange6(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5888 struct block
*b1
, *tmp
;
5892 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5896 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5900 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5901 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5907 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5908 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5916 return gen_port6_common(cstate
, proto
, b1
);
5920 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5929 v
= pcap_nametoproto(name
);
5930 if (v
== PROTO_UNDEF
)
5931 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5935 /* XXX should look up h/w protocol type based on cstate->linktype */
5936 v
= pcap_nametoeproto(name
);
5937 if (v
== PROTO_UNDEF
) {
5938 v
= pcap_nametollc(name
);
5939 if (v
== PROTO_UNDEF
)
5940 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5945 if (strcmp(name
, "esis") == 0)
5947 else if (strcmp(name
, "isis") == 0)
5949 else if (strcmp(name
, "clnp") == 0)
5952 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5962 #if !defined(NO_PROTOCHAIN)
5964 * This primitive is non-directional by design, so the grammar does not allow
5965 * to qualify it with a direction.
5967 static struct block
*
5968 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
5970 struct block
*b0
, *b
;
5971 struct slist
*s
[100];
5972 int fix2
, fix3
, fix4
, fix5
;
5973 int ahcheck
, again
, end
;
5975 int reg2
= alloc_reg(cstate
);
5977 memset(s
, 0, sizeof(s
));
5978 fix3
= fix4
= fix5
= 0;
5983 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
5986 b0
= gen_protochain(cstate
, v
, Q_IP
);
5987 b
= gen_protochain(cstate
, v
, Q_IPV6
);
5991 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
5996 * We don't handle variable-length prefixes before the link-layer
5997 * header, or variable-length link-layer headers, here yet.
5998 * We might want to add BPF instructions to do the protochain
5999 * work, to simplify that and, on platforms that have a BPF
6000 * interpreter with the new instructions, let the filtering
6001 * be done in the kernel. (We already require a modified BPF
6002 * engine to do the protochain stuff, to support backward
6003 * branches, and backward branch support is unlikely to appear
6004 * in kernel BPF engines.)
6006 if (cstate
->off_linkpl
.is_variable
)
6007 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6010 * To quote a comment in optimize.c:
6012 * "These data structures are used in a Cocke and Schwartz style
6013 * value numbering scheme. Since the flowgraph is acyclic,
6014 * exit values can be propagated from a node's predecessors
6015 * provided it is uniquely defined."
6017 * "Acyclic" means "no backward branches", which means "no
6018 * loops", so we have to turn the optimizer off.
6020 cstate
->no_optimize
= 1;
6023 * s[0] is a dummy entry to protect other BPF insn from damage
6024 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6025 * hard to find interdependency made by jump table fixup.
6028 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6033 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6036 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6037 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6039 /* X = ip->ip_hl << 2 */
6040 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6041 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6046 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6048 /* A = ip6->ip_nxt */
6049 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6050 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6052 /* X = sizeof(struct ip6_hdr) */
6053 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6059 bpf_error(cstate
, "unsupported proto to gen_protochain");
6063 /* again: if (A == v) goto end; else fall through; */
6065 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6067 s
[i
]->s
.jt
= NULL
; /*later*/
6068 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6072 #ifndef IPPROTO_NONE
6073 #define IPPROTO_NONE 59
6075 /* if (A == IPPROTO_NONE) goto end */
6076 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6077 s
[i
]->s
.jt
= NULL
; /*later*/
6078 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6079 s
[i
]->s
.k
= IPPROTO_NONE
;
6080 s
[fix5
]->s
.jf
= s
[i
];
6084 if (proto
== Q_IPV6
) {
6085 int v6start
, v6end
, v6advance
, j
;
6088 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6089 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6090 s
[i
]->s
.jt
= NULL
; /*later*/
6091 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6092 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6093 s
[fix2
]->s
.jf
= s
[i
];
6095 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6096 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6097 s
[i
]->s
.jt
= NULL
; /*later*/
6098 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6099 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6101 /* if (A == IPPROTO_ROUTING) goto v6advance */
6102 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6103 s
[i
]->s
.jt
= NULL
; /*later*/
6104 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6105 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6107 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6108 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6109 s
[i
]->s
.jt
= NULL
; /*later*/
6110 s
[i
]->s
.jf
= NULL
; /*later*/
6111 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6121 * A = P[X + packet head];
6122 * X = X + (P[X + packet head + 1] + 1) * 8;
6124 /* A = P[X + packet head] */
6125 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6126 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6129 s
[i
] = new_stmt(cstate
, BPF_ST
);
6132 /* A = P[X + packet head + 1]; */
6133 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6134 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6137 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6141 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6145 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6149 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6152 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6156 /* goto again; (must use BPF_JA for backward jump) */
6157 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6158 s
[i
]->s
.k
= again
- i
- 1;
6159 s
[i
- 1]->s
.jf
= s
[i
];
6163 for (j
= v6start
; j
<= v6end
; j
++)
6164 s
[j
]->s
.jt
= s
[v6advance
];
6167 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6169 s
[fix2
]->s
.jf
= s
[i
];
6175 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6176 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6177 s
[i
]->s
.jt
= NULL
; /*later*/
6178 s
[i
]->s
.jf
= NULL
; /*later*/
6179 s
[i
]->s
.k
= IPPROTO_AH
;
6181 s
[fix3
]->s
.jf
= s
[ahcheck
];
6188 * X = X + (P[X + 1] + 2) * 4;
6190 /* A = P[X + packet head]; */
6191 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6192 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6193 s
[i
- 1]->s
.jt
= s
[i
];
6196 s
[i
] = new_stmt(cstate
, BPF_ST
);
6200 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6203 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6207 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6209 /* A = P[X + packet head] */
6210 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6211 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6214 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6218 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6222 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6225 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6229 /* goto again; (must use BPF_JA for backward jump) */
6230 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6231 s
[i
]->s
.k
= again
- i
- 1;
6236 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6238 s
[fix2
]->s
.jt
= s
[end
];
6239 s
[fix4
]->s
.jf
= s
[end
];
6240 s
[fix5
]->s
.jt
= s
[end
];
6247 for (i
= 0; i
< max
- 1; i
++)
6248 s
[i
]->next
= s
[i
+ 1];
6249 s
[max
- 1]->next
= NULL
;
6253 * Remember, s[0] is dummy.
6255 b
= gen_jmp(cstate
, BPF_JEQ
, v
, s
[1]);
6257 free_reg(cstate
, reg2
);
6262 #endif /* !defined(NO_PROTOCHAIN) */
6265 * Generate code that checks whether the packet is a packet for protocol
6266 * <proto> and whether the type field in that protocol's header has
6267 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6268 * IP packet and checks the protocol number in the IP header against <v>.
6270 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6271 * against Q_IP and Q_IPV6.
6273 * This primitive is non-directional by design, so the grammar does not allow
6274 * to qualify it with a direction.
6276 static struct block
*
6277 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6279 struct block
*b0
, *b1
;
6284 b0
= gen_proto(cstate
, v
, Q_IP
);
6285 b1
= gen_proto(cstate
, v
, Q_IPV6
);
6290 return gen_linktype(cstate
, v
);
6293 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6295 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6296 * not LLC encapsulation with LLCSAP_IP.
6298 * For IEEE 802 networks - which includes 802.5 token ring
6299 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6300 * says that SNAP encapsulation is used, not LLC encapsulation
6303 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6304 * RFC 2225 say that SNAP encapsulation is used, not LLC
6305 * encapsulation with LLCSAP_IP.
6307 * So we always check for ETHERTYPE_IP.
6309 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6310 // 0 <= v <= UINT8_MAX
6311 b1
= gen_ip_proto(cstate
, (uint8_t)v
);
6329 break; // invalid qualifier
6332 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6333 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6335 * Also check for a fragment header before the final
6338 b2
= gen_ip6_proto(cstate
, IPPROTO_FRAGMENT
);
6339 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6341 // 0 <= v <= UINT8_MAX
6342 b2
= gen_ip6_proto(cstate
, (uint8_t)v
);
6353 break; // invalid qualifier
6356 assert_maxval(cstate
, "ISO protocol", v
, UINT8_MAX
);
6357 switch (cstate
->linktype
) {
6361 * Frame Relay packets typically have an OSI
6362 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6363 * generates code to check for all the OSI
6364 * NLPIDs, so calling it and then adding a check
6365 * for the particular NLPID for which we're
6366 * looking is bogus, as we can just check for
6369 * What we check for is the NLPID and a frame
6370 * control field value of UI, i.e. 0x03 followed
6373 * XXX - assumes a 2-byte Frame Relay header with
6374 * DLCI and flags. What if the address is longer?
6376 * XXX - what about SNAP-encapsulated frames?
6378 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6384 * Cisco uses an Ethertype lookalike - for OSI,
6387 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6388 /* OSI in C-HDLC is stuffed with a fudge byte */
6389 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6394 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6395 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6401 break; // invalid qualifier
6404 assert_maxval(cstate
, "IS-IS PDU type", v
, ISIS_PDU_TYPE_MAX
);
6405 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
6407 * 4 is the offset of the PDU type relative to the IS-IS
6409 * Except when it is not, see above.
6411 unsigned pdu_type_offset
;
6412 switch (cstate
->linktype
) {
6415 pdu_type_offset
= 5;
6418 pdu_type_offset
= 4;
6420 b1
= gen_mcmp(cstate
, OR_LINKPL_NOSNAP
, pdu_type_offset
, BPF_B
,
6421 v
, ISIS_PDU_TYPE_MAX
);
6438 break; // invalid qualifier
6444 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6449 * Convert a non-numeric name to a port number.
6452 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6454 struct addrinfo hints
, *res
, *ai
;
6456 struct sockaddr_in
*in4
;
6458 struct sockaddr_in6
*in6
;
6463 * We check for both TCP and UDP in case there are
6464 * ambiguous entries.
6466 memset(&hints
, 0, sizeof(hints
));
6467 hints
.ai_family
= PF_UNSPEC
;
6468 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6469 hints
.ai_protocol
= ipproto
;
6470 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6477 * No such port. Just return -1.
6484 * We don't use strerror() because it's not
6485 * guaranteed to be thread-safe on all platforms
6486 * (probably because it might use a non-thread-local
6487 * buffer into which to format an error message
6488 * if the error code isn't one for which it has
6489 * a canned string; three cheers for C string
6492 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6494 port
= -2; /* a real error */
6500 * This is a real error, not just "there's
6501 * no such service name".
6503 * We don't use gai_strerror() because it's not
6504 * guaranteed to be thread-safe on all platforms
6505 * (probably because it might use a non-thread-local
6506 * buffer into which to format an error message
6507 * if the error code isn't one for which it has
6508 * a canned string; three cheers for C string
6511 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6513 port
= -2; /* a real error */
6518 * OK, we found it. Did it find anything?
6520 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6522 * Does it have an address?
6524 if (ai
->ai_addr
!= NULL
) {
6526 * Yes. Get a port number; we're done.
6528 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6529 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6530 port
= ntohs(in4
->sin_port
);
6534 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6535 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6536 port
= ntohs(in6
->sin6_port
);
6548 * Convert a string to a port number.
6551 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6561 * See if it's a number.
6563 ret
= stoulen(string
, string_size
, &val
, cstate
);
6567 /* Unknown port type - it's just a number. */
6568 *proto
= PROTO_UNDEF
;
6571 case STOULEN_NOT_OCTAL_NUMBER
:
6572 case STOULEN_NOT_HEX_NUMBER
:
6573 case STOULEN_NOT_DECIMAL_NUMBER
:
6575 * Not a valid number; try looking it up as a port.
6577 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6578 memcpy(cpy
, string
, string_size
);
6579 cpy
[string_size
] = '\0';
6580 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6581 if (tcp_port
== -2) {
6583 * We got a hard error; the error string has
6587 longjmp(cstate
->top_ctx
, 1);
6590 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6591 if (udp_port
== -2) {
6593 * We got a hard error; the error string has
6597 longjmp(cstate
->top_ctx
, 1);
6602 * We need to check /etc/services for ambiguous entries.
6603 * If we find an ambiguous entry, and it has the
6604 * same port number, change the proto to PROTO_UNDEF
6605 * so both TCP and UDP will be checked.
6607 if (tcp_port
>= 0) {
6608 val
= (bpf_u_int32
)tcp_port
;
6609 *proto
= IPPROTO_TCP
;
6610 if (udp_port
>= 0) {
6611 if (udp_port
== tcp_port
)
6612 *proto
= PROTO_UNDEF
;
6615 /* Can't handle ambiguous names that refer
6616 to different port numbers. */
6617 warning("ambiguous port %s in /etc/services",
6624 if (udp_port
>= 0) {
6625 val
= (bpf_u_int32
)udp_port
;
6626 *proto
= IPPROTO_UDP
;
6630 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6632 longjmp(cstate
->top_ctx
, 1);
6639 /* Error already set. */
6640 longjmp(cstate
->top_ctx
, 1);
6647 /* Should not happen */
6648 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6649 longjmp(cstate
->top_ctx
, 1);
6656 * Convert a string in the form PPP-PPP, which correspond to ports, to
6657 * a starting and ending port in a port range.
6660 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6661 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6664 const char *first
, *second
;
6665 size_t first_size
, second_size
;
6668 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6669 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6672 * Make sure there are no other hyphens.
6674 * XXX - we support named ports, but there are some port names
6675 * in /etc/services that include hyphens, so this would rule
6678 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6679 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6683 * Get the length of the first port.
6686 first_size
= hyphen_off
- string
;
6687 if (first_size
== 0) {
6688 /* Range of "-port", which we don't support. */
6689 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6693 * Try to convert it to a port.
6695 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6696 save_proto
= *proto
;
6699 * Get the length of the second port.
6701 second
= hyphen_off
+ 1;
6702 second_size
= strlen(second
);
6703 if (second_size
== 0) {
6704 /* Range of "port-", which we don't support. */
6705 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6709 * Try to convert it to a port.
6711 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6712 if (*proto
!= save_proto
)
6713 *proto
= PROTO_UNDEF
;
6717 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6719 int proto
= q
.proto
;
6724 bpf_u_int32 mask
, addr
;
6725 struct addrinfo
*res
, *res0
;
6726 struct sockaddr_in
*sin4
;
6729 struct sockaddr_in6
*sin6
;
6730 struct in6_addr mask128
;
6732 struct block
*b
, *tmp
;
6733 int port
, real_proto
;
6734 bpf_u_int32 port1
, port2
;
6737 * Catch errors reported by us and routines below us, and return NULL
6740 if (setjmp(cstate
->top_ctx
))
6746 addr
= pcap_nametonetaddr(name
);
6748 bpf_error(cstate
, "unknown network '%s'", name
);
6749 /* Left justify network addr and calculate its network mask */
6751 while (addr
&& (addr
& 0xff000000) == 0) {
6755 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6759 if (proto
== Q_LINK
) {
6760 const char *context
= "link host NAME";
6761 if (! is_mac48_linktype(cstate
->linktype
))
6762 fail_kw_on_dlt(cstate
, context
);
6763 eaddrp
= pcap_ether_hostton(name
);
6765 bpf_error(cstate
, ERRSTR_UNKNOWN_MAC48HOST
, name
);
6766 memcpy(eaddr
, eaddrp
, sizeof(eaddr
));
6768 return gen_mac48host(cstate
, eaddr
, q
.dir
, context
);
6769 } else if (proto
== Q_DECNET
) {
6771 * A long time ago on Ultrix libpcap supported
6772 * translation of DECnet host names into DECnet
6773 * addresses, but this feature is history now.
6775 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6778 memset(&mask128
, 0xff, sizeof(mask128
));
6780 res0
= res
= pcap_nametoaddrinfo(name
);
6782 bpf_error(cstate
, "unknown host '%s'", name
);
6789 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6790 tproto
== Q_DEFAULT
) {
6796 for (res
= res0
; res
; res
= res
->ai_next
) {
6797 switch (res
->ai_family
) {
6800 if (tproto
== Q_IPV6
)
6804 sin4
= (struct sockaddr_in
*)
6806 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6807 0xffffffff, tproto
, dir
, q
.addr
);
6811 if (tproto6
== Q_IP
)
6814 sin6
= (struct sockaddr_in6
*)
6816 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6817 &mask128
, tproto6
, dir
, q
.addr
);
6830 bpf_error(cstate
, "unknown host '%s'%s", name
,
6831 (proto
== Q_DEFAULT
)
6833 : " for specified address family");
6839 (void)port_pq_to_ipproto(cstate
, proto
, "port"); // validate only
6840 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6841 bpf_error(cstate
, "unknown port '%s'", name
);
6842 if (proto
== Q_UDP
) {
6843 if (real_proto
== IPPROTO_TCP
)
6844 bpf_error(cstate
, "port '%s' is tcp", name
);
6845 else if (real_proto
== IPPROTO_SCTP
)
6846 bpf_error(cstate
, "port '%s' is sctp", name
);
6848 /* override PROTO_UNDEF */
6849 real_proto
= IPPROTO_UDP
;
6851 if (proto
== Q_TCP
) {
6852 if (real_proto
== IPPROTO_UDP
)
6853 bpf_error(cstate
, "port '%s' is udp", name
);
6855 else if (real_proto
== IPPROTO_SCTP
)
6856 bpf_error(cstate
, "port '%s' is sctp", name
);
6858 /* override PROTO_UNDEF */
6859 real_proto
= IPPROTO_TCP
;
6861 if (proto
== Q_SCTP
) {
6862 if (real_proto
== IPPROTO_UDP
)
6863 bpf_error(cstate
, "port '%s' is udp", name
);
6865 else if (real_proto
== IPPROTO_TCP
)
6866 bpf_error(cstate
, "port '%s' is tcp", name
);
6868 /* override PROTO_UNDEF */
6869 real_proto
= IPPROTO_SCTP
;
6872 bpf_error(cstate
, "illegal port number %d < 0", port
);
6874 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6875 // real_proto can be PROTO_UNDEF
6876 b
= gen_port(cstate
, (uint16_t)port
, real_proto
, dir
);
6877 gen_or(gen_port6(cstate
, (uint16_t)port
, real_proto
, dir
), b
);
6881 (void)port_pq_to_ipproto(cstate
, proto
, "portrange"); // validate only
6882 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
6883 if (proto
== Q_UDP
) {
6884 if (real_proto
== IPPROTO_TCP
)
6885 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6886 else if (real_proto
== IPPROTO_SCTP
)
6887 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6889 /* override PROTO_UNDEF */
6890 real_proto
= IPPROTO_UDP
;
6892 if (proto
== Q_TCP
) {
6893 if (real_proto
== IPPROTO_UDP
)
6894 bpf_error(cstate
, "port in range '%s' is udp", name
);
6895 else if (real_proto
== IPPROTO_SCTP
)
6896 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6898 /* override PROTO_UNDEF */
6899 real_proto
= IPPROTO_TCP
;
6901 if (proto
== Q_SCTP
) {
6902 if (real_proto
== IPPROTO_UDP
)
6903 bpf_error(cstate
, "port in range '%s' is udp", name
);
6904 else if (real_proto
== IPPROTO_TCP
)
6905 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6907 /* override PROTO_UNDEF */
6908 real_proto
= IPPROTO_SCTP
;
6911 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6913 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6915 // real_proto can be PROTO_UNDEF
6916 b
= gen_portrange(cstate
, (uint16_t)port1
, (uint16_t)port2
,
6918 gen_or(gen_portrange6(cstate
, (uint16_t)port1
, (uint16_t)port2
,
6919 real_proto
, dir
), b
);
6924 if (! is_mac48_linktype(cstate
->linktype
))
6925 fail_kw_on_dlt(cstate
, "gateway");
6926 eaddrp
= pcap_ether_hostton(name
);
6928 bpf_error(cstate
, ERRSTR_UNKNOWN_MAC48HOST
, name
);
6929 memcpy(eaddr
, eaddrp
, sizeof(eaddr
));
6932 res
= pcap_nametoaddrinfo(name
);
6935 bpf_error(cstate
, "unknown host '%s'", name
);
6936 b
= gen_gateway(cstate
, eaddr
, res
, proto
);
6940 bpf_error(cstate
, "unknown host '%s'", name
);
6943 bpf_error(cstate
, "'gateway' not supported in this configuration");
6947 real_proto
= lookup_proto(cstate
, name
, proto
);
6948 if (real_proto
>= 0)
6949 return gen_proto(cstate
, real_proto
, proto
);
6951 bpf_error(cstate
, "unknown protocol: %s", name
);
6953 #if !defined(NO_PROTOCHAIN)
6955 real_proto
= lookup_proto(cstate
, name
, proto
);
6956 if (real_proto
>= 0)
6957 return gen_protochain(cstate
, real_proto
, proto
);
6959 bpf_error(cstate
, "unknown protocol: %s", name
);
6960 #endif /* !defined(NO_PROTOCHAIN) */
6971 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6972 bpf_u_int32 masklen
, struct qual q
)
6974 register int nlen
, mlen
;
6979 * Catch errors reported by us and routines below us, and return NULL
6982 if (setjmp(cstate
->top_ctx
))
6985 nlen
= pcapint_atoin(s1
, &n
);
6987 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
6988 /* Promote short ipaddr */
6992 mlen
= pcapint_atoin(s2
, &m
);
6994 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
6995 /* Promote short ipaddr */
6998 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7001 /* Convert mask len to mask */
7003 bpf_error(cstate
, "mask length must be <= 32");
7004 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7005 m
= (bpf_u_int32
)m64
;
7007 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7014 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7017 // Q_HOST and Q_GATEWAY only (see the grammar)
7018 bpf_error(cstate
, "Mask syntax for networks only");
7025 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7033 * Catch errors reported by us and routines below us, and return NULL
7036 if (setjmp(cstate
->top_ctx
))
7043 * v contains a 32-bit unsigned parsed from a string of the
7044 * form {N}, which could be decimal, hexadecimal or octal.
7045 * Although it would be possible to use the value as a raw
7046 * 16-bit DECnet address when the value fits into 16 bits, this
7047 * would be a questionable feature: DECnet address wire
7048 * encoding is little-endian, so this would not work as
7049 * intuitively as the same works for [big-endian] IPv4
7050 * addresses (0x01020304 means 1.2.3.4).
7052 if (proto
== Q_DECNET
)
7053 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7055 } else if (proto
== Q_DECNET
) {
7057 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7058 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7059 * for a valid DECnet address.
7061 vlen
= pcapint_atodn(s
, &v
);
7063 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7066 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7067 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7070 vlen
= pcapint_atoin(s
, &v
);
7072 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7080 if (proto
== Q_DECNET
)
7081 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7082 else if (proto
== Q_LINK
) {
7083 // "link (host|net) IPV4ADDR" and variations thereof
7084 bpf_error(cstate
, "illegal link layer address");
7087 if (s
== NULL
&& q
.addr
== Q_NET
) {
7088 /* Promote short net number */
7089 while (v
&& (v
& 0xff000000) == 0) {
7094 /* Promote short ipaddr */
7096 mask
<<= 32 - vlen
;
7098 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7102 proto
= port_pq_to_ipproto(cstate
, proto
, "port");
7105 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7107 // proto can be PROTO_UNDEF
7110 b
= gen_port(cstate
, (uint16_t)v
, proto
, dir
);
7111 gen_or(gen_port6(cstate
, (uint16_t)v
, proto
, dir
), b
);
7116 proto
= port_pq_to_ipproto(cstate
, proto
, "portrange");
7119 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7121 // proto can be PROTO_UNDEF
7124 b
= gen_portrange(cstate
, (uint16_t)v
, (uint16_t)v
,
7126 gen_or(gen_portrange6(cstate
, (uint16_t)v
, (uint16_t)v
,
7132 bpf_error(cstate
, "'gateway' requires a name");
7136 return gen_proto(cstate
, v
, proto
);
7138 #if !defined(NO_PROTOCHAIN)
7140 return gen_protochain(cstate
, v
, proto
);
7156 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7159 struct addrinfo
*res
;
7160 struct in6_addr
*addr
;
7161 struct in6_addr mask
;
7163 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7166 * Catch errors reported by us and routines below us, and return NULL
7169 if (setjmp(cstate
->top_ctx
))
7172 res
= pcap_nametoaddrinfo(s
);
7174 bpf_error(cstate
, "invalid ip6 address %s", s
);
7177 bpf_error(cstate
, "%s resolved to multiple address", s
);
7178 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7180 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7181 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7182 memset(&mask
, 0, sizeof(mask
));
7183 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7185 mask
.s6_addr
[masklen
/ 8] =
7186 (0xff << (8 - masklen
% 8)) & 0xff;
7189 memcpy(a
, addr
, sizeof(a
));
7190 memcpy(m
, &mask
, sizeof(m
));
7191 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7192 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7193 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7201 bpf_error(cstate
, "Mask syntax for networks only");
7205 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7211 // Q_GATEWAY only (see the grammar)
7212 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7219 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7222 * Catch errors reported by us and routines below us, and return NULL
7225 if (setjmp(cstate
->top_ctx
))
7228 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7229 const char *context
= "link host XX:XX:XX:XX:XX:XX";
7230 if (! is_mac48_linktype(cstate
->linktype
))
7231 fail_kw_on_dlt(cstate
, context
);
7232 cstate
->e
= pcap_ether_aton(s
);
7233 if (cstate
->e
== NULL
)
7234 bpf_error(cstate
, "malloc");
7235 struct block
*b
= gen_mac48host(cstate
, cstate
->e
, q
.dir
, context
);
7240 bpf_error(cstate
, "ethernet address used in non-ether expression");
7245 sappend(struct slist
*s0
, struct slist
*s1
)
7248 * This is definitely not the best way to do this, but the
7249 * lists will rarely get long.
7256 static struct slist
*
7257 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7261 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7266 static struct slist
*
7267 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7271 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7277 * Modify "index" to use the value stored into its register as an
7278 * offset relative to the beginning of the header for the protocol
7279 * "proto", and allocate a register and put an item "size" bytes long
7280 * (1, 2, or 4) at that offset into that register, making it the register
7283 static struct arth
*
7284 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7288 struct slist
*s
, *tmp
;
7290 int regno
= alloc_reg(cstate
);
7292 free_reg(cstate
, inst
->regno
);
7296 bpf_error(cstate
, "data size must be 1, 2, or 4");
7313 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7317 * The offset is relative to the beginning of the packet
7318 * data, if we have a radio header. (If we don't, this
7321 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7322 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7323 cstate
->linktype
!= DLT_PRISM_HEADER
)
7324 bpf_error(cstate
, "radio information not present in capture");
7327 * Load into the X register the offset computed into the
7328 * register specified by "index".
7330 s
= xfer_to_x(cstate
, inst
);
7333 * Load the item at that offset.
7335 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7337 sappend(inst
->s
, s
);
7342 * The offset is relative to the beginning of
7343 * the link-layer header.
7345 * XXX - what about ATM LANE? Should the index be
7346 * relative to the beginning of the AAL5 frame, so
7347 * that 0 refers to the beginning of the LE Control
7348 * field, or relative to the beginning of the LAN
7349 * frame, so that 0 refers, for Ethernet LANE, to
7350 * the beginning of the destination address?
7352 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7355 * If "s" is non-null, it has code to arrange that the
7356 * X register contains the length of the prefix preceding
7357 * the link-layer header. Add to it the offset computed
7358 * into the register specified by "index", and move that
7359 * into the X register. Otherwise, just load into the X
7360 * register the offset computed into the register specified
7364 sappend(s
, xfer_to_a(cstate
, inst
));
7365 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7366 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7368 s
= xfer_to_x(cstate
, inst
);
7371 * Load the item at the sum of the offset we've put in the
7372 * X register and the offset of the start of the link
7373 * layer header (which is 0 if the radio header is
7374 * variable-length; that header length is what we put
7375 * into the X register and then added to the index).
7377 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7378 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7380 sappend(inst
->s
, s
);
7394 * The offset is relative to the beginning of
7395 * the network-layer header.
7396 * XXX - are there any cases where we want
7397 * cstate->off_nl_nosnap?
7399 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7402 * If "s" is non-null, it has code to arrange that the
7403 * X register contains the variable part of the offset
7404 * of the link-layer payload. Add to it the offset
7405 * computed into the register specified by "index",
7406 * and move that into the X register. Otherwise, just
7407 * load into the X register the offset computed into
7408 * the register specified by "index".
7411 sappend(s
, xfer_to_a(cstate
, inst
));
7412 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7413 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7415 s
= xfer_to_x(cstate
, inst
);
7418 * Load the item at the sum of the offset we've put in the
7419 * X register, the offset of the start of the network
7420 * layer header from the beginning of the link-layer
7421 * payload, and the constant part of the offset of the
7422 * start of the link-layer payload.
7424 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7425 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7427 sappend(inst
->s
, s
);
7430 * Do the computation only if the packet contains
7431 * the protocol in question.
7433 b
= gen_proto_abbrev_internal(cstate
, proto
);
7435 gen_and(inst
->b
, b
);
7449 * The offset is relative to the beginning of
7450 * the transport-layer header.
7452 * Load the X register with the length of the IPv4 header
7453 * (plus the offset of the link-layer header, if it's
7454 * a variable-length header), in bytes.
7456 * XXX - are there any cases where we want
7457 * cstate->off_nl_nosnap?
7458 * XXX - we should, if we're built with
7459 * IPv6 support, generate code to load either
7460 * IPv4, IPv6, or both, as appropriate.
7462 s
= gen_loadx_iphdrlen(cstate
);
7465 * The X register now contains the sum of the variable
7466 * part of the offset of the link-layer payload and the
7467 * length of the network-layer header.
7469 * Load into the A register the offset relative to
7470 * the beginning of the transport layer header,
7471 * add the X register to that, move that to the
7472 * X register, and load with an offset from the
7473 * X register equal to the sum of the constant part of
7474 * the offset of the link-layer payload and the offset,
7475 * relative to the beginning of the link-layer payload,
7476 * of the network-layer header.
7478 sappend(s
, xfer_to_a(cstate
, inst
));
7479 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7480 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7481 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7482 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7483 sappend(inst
->s
, s
);
7486 * Do the computation only if the packet contains
7487 * the protocol in question - which is true only
7488 * if this is an IP datagram and is the first or
7489 * only fragment of that datagram.
7491 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7493 gen_and(inst
->b
, b
);
7494 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7499 * Do the computation only if the packet contains
7500 * the protocol in question.
7502 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7504 gen_and(inst
->b
, b
);
7508 * Check if we have an icmp6 next header
7510 b
= gen_ip6_proto(cstate
, 58);
7512 gen_and(inst
->b
, b
);
7515 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7517 * If "s" is non-null, it has code to arrange that the
7518 * X register contains the variable part of the offset
7519 * of the link-layer payload. Add to it the offset
7520 * computed into the register specified by "index",
7521 * and move that into the X register. Otherwise, just
7522 * load into the X register the offset computed into
7523 * the register specified by "index".
7526 sappend(s
, xfer_to_a(cstate
, inst
));
7527 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7528 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7530 s
= xfer_to_x(cstate
, inst
);
7533 * Load the item at the sum of the offset we've put in the
7534 * X register, the offset of the start of the network
7535 * layer header from the beginning of the link-layer
7536 * payload, and the constant part of the offset of the
7537 * start of the link-layer payload.
7539 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7540 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7543 sappend(inst
->s
, s
);
7547 inst
->regno
= regno
;
7548 s
= new_stmt(cstate
, BPF_ST
);
7550 sappend(inst
->s
, s
);
7556 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7560 * Catch errors reported by us and routines below us, and return NULL
7563 if (setjmp(cstate
->top_ctx
))
7566 return gen_load_internal(cstate
, proto
, inst
, size
);
7569 static struct block
*
7570 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7571 struct arth
*a1
, int reversed
)
7573 struct slist
*s0
, *s1
, *s2
;
7574 struct block
*b
, *tmp
;
7576 s0
= xfer_to_x(cstate
, a1
);
7577 s1
= xfer_to_a(cstate
, a0
);
7578 if (code
== BPF_JEQ
) {
7579 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7580 b
= new_block(cstate
, JMP(code
));
7584 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7590 sappend(a0
->s
, a1
->s
);
7594 free_reg(cstate
, a0
->regno
);
7595 free_reg(cstate
, a1
->regno
);
7597 /* 'and' together protocol checks */
7600 gen_and(a0
->b
, tmp
= a1
->b
);
7614 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7615 struct arth
*a1
, int reversed
)
7618 * Catch errors reported by us and routines below us, and return NULL
7621 if (setjmp(cstate
->top_ctx
))
7624 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7628 gen_loadlen(compiler_state_t
*cstate
)
7635 * Catch errors reported by us and routines below us, and return NULL
7638 if (setjmp(cstate
->top_ctx
))
7641 regno
= alloc_reg(cstate
);
7642 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7643 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7644 s
->next
= new_stmt(cstate
, BPF_ST
);
7645 s
->next
->s
.k
= regno
;
7652 static struct arth
*
7653 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7659 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7661 reg
= alloc_reg(cstate
);
7663 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7665 s
->next
= new_stmt(cstate
, BPF_ST
);
7674 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7677 * Catch errors reported by us and routines below us, and return NULL
7680 if (setjmp(cstate
->top_ctx
))
7683 return gen_loadi_internal(cstate
, val
);
7687 * The a_arg dance is to avoid annoying whining by compilers that
7688 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7689 * It's not *used* after setjmp returns.
7692 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7694 struct arth
*a
= a_arg
;
7698 * Catch errors reported by us and routines below us, and return NULL
7701 if (setjmp(cstate
->top_ctx
))
7704 s
= xfer_to_a(cstate
, a
);
7706 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7709 s
= new_stmt(cstate
, BPF_ST
);
7717 * The a0_arg dance is to avoid annoying whining by compilers that
7718 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7719 * It's not *used* after setjmp returns.
7722 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7725 struct arth
*a0
= a0_arg
;
7726 struct slist
*s0
, *s1
, *s2
;
7729 * Catch errors reported by us and routines below us, and return NULL
7732 if (setjmp(cstate
->top_ctx
))
7736 * Disallow division by, or modulus by, zero; we do this here
7737 * so that it gets done even if the optimizer is disabled.
7739 * Also disallow shifts by a value greater than 31; we do this
7740 * here, for the same reason.
7742 if (code
== BPF_DIV
) {
7743 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7744 bpf_error(cstate
, "division by zero");
7745 } else if (code
== BPF_MOD
) {
7746 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7747 bpf_error(cstate
, "modulus by zero");
7748 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7749 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7750 bpf_error(cstate
, "shift by more than 31 bits");
7752 s0
= xfer_to_x(cstate
, a1
);
7753 s1
= xfer_to_a(cstate
, a0
);
7754 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7759 sappend(a0
->s
, a1
->s
);
7761 free_reg(cstate
, a0
->regno
);
7762 free_reg(cstate
, a1
->regno
);
7764 s0
= new_stmt(cstate
, BPF_ST
);
7765 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7772 * Initialize the table of used registers and the current register.
7775 init_regs(compiler_state_t
*cstate
)
7778 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7782 * Return the next free register.
7785 alloc_reg(compiler_state_t
*cstate
)
7787 int n
= BPF_MEMWORDS
;
7790 if (cstate
->regused
[cstate
->curreg
])
7791 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7793 cstate
->regused
[cstate
->curreg
] = 1;
7794 return cstate
->curreg
;
7797 bpf_error(cstate
, "too many registers needed to evaluate expression");
7802 * Return a register to the table so it can
7806 free_reg(compiler_state_t
*cstate
, int n
)
7808 cstate
->regused
[n
] = 0;
7811 static struct block
*
7812 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7816 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7817 return gen_jmp(cstate
, jmp
, n
, s
);
7821 gen_greater(compiler_state_t
*cstate
, int n
)
7824 * Catch errors reported by us and routines below us, and return NULL
7827 if (setjmp(cstate
->top_ctx
))
7830 return gen_len(cstate
, BPF_JGE
, n
);
7834 * Actually, this is less than or equal.
7837 gen_less(compiler_state_t
*cstate
, int n
)
7842 * Catch errors reported by us and routines below us, and return NULL
7845 if (setjmp(cstate
->top_ctx
))
7848 b
= gen_len(cstate
, BPF_JGT
, n
);
7855 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7856 * the beginning of the link-layer header.
7857 * XXX - that means you can't test values in the radiotap header, but
7858 * as that header is difficult if not impossible to parse generally
7859 * without a loop, that might not be a severe problem. A new keyword
7860 * "radio" could be added for that, although what you'd really want
7861 * would be a way of testing particular radio header values, which
7862 * would generate code appropriate to the radio header in question.
7865 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7871 * Catch errors reported by us and routines below us, and return NULL
7874 if (setjmp(cstate
->top_ctx
))
7877 assert_maxval(cstate
, "byte argument", val
, UINT8_MAX
);
7884 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7887 return gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7890 return gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7893 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7897 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7901 // Load the required byte first.
7902 struct slist
*s0
= gen_load_a(cstate
, OR_LINKHDR
, idx
, BPF_B
);
7904 b
= gen_jmp(cstate
, BPF_JEQ
, 0, s0
);
7911 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7913 bpf_u_int32 hostmask
;
7914 struct block
*b0
, *b1
, *b2
;
7915 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7918 * Catch errors reported by us and routines below us, and return NULL
7921 if (setjmp(cstate
->top_ctx
))
7928 switch (cstate
->linktype
) {
7930 case DLT_ARCNET_LINUX
:
7931 // ARCnet broadcast is [8-bit] destination address 0.
7932 return gen_ahostop(cstate
, 0, Q_DST
);
7934 return gen_mac48host(cstate
, ebroadcast
, Q_DST
, "broadcast");
7939 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7940 * as an indication that we don't know the netmask, and fail
7943 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7944 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7945 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7946 hostmask
= ~cstate
->netmask
;
7947 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
7948 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
7953 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
7958 * Generate code to test the low-order bit of a MAC address (that's
7959 * the bottom bit of the *first* byte).
7961 static struct block
*
7962 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7964 register struct slist
*s
;
7966 /* link[offset] & 1 != 0 */
7967 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7968 return gen_set(cstate
, 1, s
);
7972 gen_multicast(compiler_state_t
*cstate
, int proto
)
7974 register struct block
*b0
, *b1
, *b2
;
7975 register struct slist
*s
;
7978 * Catch errors reported by us and routines below us, and return NULL
7981 if (setjmp(cstate
->top_ctx
))
7988 switch (cstate
->linktype
) {
7990 case DLT_ARCNET_LINUX
:
7991 // ARCnet multicast is the same as broadcast.
7992 return gen_ahostop(cstate
, 0, Q_DST
);
7994 case DLT_NETANALYZER
:
7995 case DLT_NETANALYZER_TRANSPARENT
:
7996 b1
= gen_prevlinkhdr_check(cstate
);
7997 /* ether[0] & 1 != 0 */
7998 b0
= gen_mac_multicast(cstate
, 0);
8004 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8006 * XXX - was that referring to bit-order issues?
8008 /* fddi[1] & 1 != 0 */
8009 return gen_mac_multicast(cstate
, 1);
8011 /* tr[2] & 1 != 0 */
8012 return gen_mac_multicast(cstate
, 2);
8013 case DLT_IEEE802_11
:
8014 case DLT_PRISM_HEADER
:
8015 case DLT_IEEE802_11_RADIO_AVS
:
8016 case DLT_IEEE802_11_RADIO
:
8021 * For control frames, there is no DA.
8023 * For management frames, DA is at an
8024 * offset of 4 from the beginning of
8027 * For data frames, DA is at an offset
8028 * of 4 from the beginning of the packet
8029 * if To DS is clear and at an offset of
8030 * 16 from the beginning of the packet
8035 * Generate the tests to be done for data frames.
8037 * First, check for To DS set, i.e. "link[1] & 0x01".
8039 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8040 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8043 * If To DS is set, the DA is at 16.
8045 b0
= gen_mac_multicast(cstate
, 16);
8049 * Now, check for To DS not set, i.e. check
8050 * "!(link[1] & 0x01)".
8052 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8053 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8056 * If To DS is not set, the DA is at 4.
8058 b1
= gen_mac_multicast(cstate
, 4);
8062 * Now OR together the last two checks. That gives
8063 * the complete set of checks for data frames.
8068 * Now check for a data frame.
8069 * I.e, check "link[0] & 0x08".
8071 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8072 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8075 * AND that with the checks done for data frames.
8080 * If the high-order bit of the type value is 0, this
8081 * is a management frame.
8082 * I.e, check "!(link[0] & 0x08)".
8084 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8085 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8088 * For management frames, the DA is at 4.
8090 b1
= gen_mac_multicast(cstate
, 4);
8094 * OR that with the checks done for data frames.
8095 * That gives the checks done for management and
8101 * If the low-order bit of the type value is 1,
8102 * this is either a control frame or a frame
8103 * with a reserved type, and thus not a
8106 * I.e., check "!(link[0] & 0x04)".
8108 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8109 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
8112 * AND that with the checks for data and management
8117 case DLT_IP_OVER_FC
:
8118 return gen_mac_multicast(cstate
, 2);
8122 fail_kw_on_dlt(cstate
, "multicast");
8126 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8127 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8132 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8133 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8137 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8143 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8144 * we can look at special meta-data in the filter expression; otherwise we
8145 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8146 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8147 * pcap_activate() conditionally sets.
8150 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8152 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8154 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8156 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8161 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8164 * Catch errors reported by us and routines below us, and return NULL
8167 if (setjmp(cstate
->top_ctx
))
8171 * Only some data link types support ifindex qualifiers.
8173 switch (cstate
->linktype
) {
8174 case DLT_LINUX_SLL2
:
8175 /* match packets on this interface */
8176 return gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8178 #if defined(__linux__)
8179 require_basic_bpf_extensions(cstate
, "ifindex");
8181 return gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8183 #else /* defined(__linux__) */
8184 fail_kw_on_dlt(cstate
, "ifindex");
8186 #endif /* defined(__linux__) */
8191 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8192 * Outbound traffic is sent by this machine, while inbound traffic is
8193 * sent by a remote machine (and may include packets destined for a
8194 * unicast or multicast link-layer address we are not subscribing to).
8195 * These are the same definitions implemented by pcap_setdirection().
8196 * Capturing only unicast traffic destined for this host is probably
8197 * better accomplished using a higher-layer filter.
8200 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8202 register struct block
*b0
;
8205 * Catch errors reported by us and routines below us, and return NULL
8208 if (setjmp(cstate
->top_ctx
))
8212 * Only some data link types support inbound/outbound qualifiers.
8214 switch (cstate
->linktype
) {
8216 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8217 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8220 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8221 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8224 /* match outgoing packets */
8225 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8227 /* to filter on inbound traffic, invert the match */
8232 case DLT_LINUX_SLL2
:
8233 /* match outgoing packets */
8234 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8236 /* to filter on inbound traffic, invert the match */
8242 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8243 outbound
? PF_OUT
: PF_IN
);
8246 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8248 case DLT_JUNIPER_MFR
:
8249 case DLT_JUNIPER_MLFR
:
8250 case DLT_JUNIPER_MLPPP
:
8251 case DLT_JUNIPER_ATM1
:
8252 case DLT_JUNIPER_ATM2
:
8253 case DLT_JUNIPER_PPPOE
:
8254 case DLT_JUNIPER_PPPOE_ATM
:
8255 case DLT_JUNIPER_GGSN
:
8256 case DLT_JUNIPER_ES
:
8257 case DLT_JUNIPER_MONITOR
:
8258 case DLT_JUNIPER_SERVICES
:
8259 case DLT_JUNIPER_ETHER
:
8260 case DLT_JUNIPER_PPP
:
8261 case DLT_JUNIPER_FRELAY
:
8262 case DLT_JUNIPER_CHDLC
:
8263 case DLT_JUNIPER_VP
:
8264 case DLT_JUNIPER_ST
:
8265 case DLT_JUNIPER_ISM
:
8266 case DLT_JUNIPER_VS
:
8267 case DLT_JUNIPER_SRX_E2E
:
8268 case DLT_JUNIPER_FIBRECHANNEL
:
8269 case DLT_JUNIPER_ATM_CEMIC
:
8270 /* juniper flags (including direction) are stored
8271 * the byte after the 3-byte magic number */
8272 return gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8276 * If we have packet meta-data indicating a direction,
8277 * and that metadata can be checked by BPF code, check
8278 * it. Otherwise, give up, as this link-layer type has
8279 * nothing in the packet data.
8281 * Currently, the only platform where a BPF filter can
8282 * check that metadata is Linux with the in-kernel
8283 * BPF interpreter. If other packet capture mechanisms
8284 * and BPF filters also supported this, it would be
8285 * nice. It would be even better if they made that
8286 * metadata available so that we could provide it
8287 * with newer capture APIs, allowing it to be saved
8290 #if defined(__linux__)
8291 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8292 /* match outgoing packets */
8293 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8296 /* to filter on inbound traffic, invert the match */
8300 #else /* defined(__linux__) */
8301 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8303 #endif /* defined(__linux__) */
8307 /* PF firewall log matched interface */
8309 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8314 * Catch errors reported by us and routines below us, and return NULL
8317 if (setjmp(cstate
->top_ctx
))
8320 assert_pflog(cstate
, "ifname");
8322 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8323 off
= offsetof(struct pfloghdr
, ifname
);
8324 if (strlen(ifname
) >= len
) {
8325 bpf_error(cstate
, "ifname interface names can only be %d characters",
8329 return gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8330 (const u_char
*)ifname
);
8333 /* PF firewall log ruleset name */
8335 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8338 * Catch errors reported by us and routines below us, and return NULL
8341 if (setjmp(cstate
->top_ctx
))
8344 assert_pflog(cstate
, "ruleset");
8346 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8347 bpf_error(cstate
, "ruleset names can only be %ld characters",
8348 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8352 return gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8353 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8356 /* PF firewall log rule number */
8358 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8361 * Catch errors reported by us and routines below us, and return NULL
8364 if (setjmp(cstate
->top_ctx
))
8367 assert_pflog(cstate
, "rnr");
8369 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8373 /* PF firewall log sub-rule number */
8375 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8378 * Catch errors reported by us and routines below us, and return NULL
8381 if (setjmp(cstate
->top_ctx
))
8384 assert_pflog(cstate
, "srnr");
8386 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8390 /* PF firewall log reason code */
8392 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8395 * Catch errors reported by us and routines below us, and return NULL
8398 if (setjmp(cstate
->top_ctx
))
8401 assert_pflog(cstate
, "reason");
8403 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8404 (bpf_u_int32
)reason
);
8407 /* PF firewall log action */
8409 gen_pf_action(compiler_state_t
*cstate
, int action
)
8412 * Catch errors reported by us and routines below us, and return NULL
8415 if (setjmp(cstate
->top_ctx
))
8418 assert_pflog(cstate
, "action");
8420 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8421 (bpf_u_int32
)action
);
8424 /* IEEE 802.11 wireless header */
8426 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8429 * Catch errors reported by us and routines below us, and return NULL
8432 if (setjmp(cstate
->top_ctx
))
8435 switch (cstate
->linktype
) {
8437 case DLT_IEEE802_11
:
8438 case DLT_PRISM_HEADER
:
8439 case DLT_IEEE802_11_RADIO_AVS
:
8440 case DLT_IEEE802_11_RADIO
:
8442 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8445 fail_kw_on_dlt(cstate
, "type/subtype");
8451 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8454 * Catch errors reported by us and routines below us, and return NULL
8457 if (setjmp(cstate
->top_ctx
))
8460 switch (cstate
->linktype
) {
8462 case DLT_IEEE802_11
:
8463 case DLT_PRISM_HEADER
:
8464 case DLT_IEEE802_11_RADIO_AVS
:
8465 case DLT_IEEE802_11_RADIO
:
8467 return gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8468 IEEE80211_FC1_DIR_MASK
);
8471 fail_kw_on_dlt(cstate
, "dir");
8476 // Process an ARCnet host address string.
8478 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8481 * Catch errors reported by us and routines below us, and return NULL
8484 if (setjmp(cstate
->top_ctx
))
8487 switch (cstate
->linktype
) {
8490 case DLT_ARCNET_LINUX
:
8491 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8492 q
.proto
== Q_LINK
) {
8495 * The lexer currently defines the address format in a
8496 * way that makes this error condition never true.
8497 * Let's check it anyway in case this part of the lexer
8498 * changes in future.
8500 if (! pcapint_atoan(s
, &addr
))
8501 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8502 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8504 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8508 bpf_error(cstate
, "aid supported only on ARCnet");
8513 // Compare an ARCnet host address with the given value.
8514 static struct block
*
8515 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8517 register struct block
*b0
, *b1
;
8521 * ARCnet is different from Ethernet: the source address comes before
8522 * the destination address, each is one byte long. This holds for all
8523 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8524 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8525 * by Datapoint (document number 61610-01).
8528 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8531 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8534 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8535 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8541 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8542 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8552 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8559 static struct block
*
8560 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8562 struct block
*b0
, *b1
;
8564 /* check for VLAN, including 802.1ad and QinQ */
8565 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8566 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8569 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8575 static struct block
*
8576 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8578 assert_maxval(cstate
, "VLAN tag", vlan_num
, 0x0fff);
8579 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8582 static struct block
*
8583 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8586 struct block
*b0
, *b1
;
8588 b0
= gen_vlan_tpid_test(cstate
);
8591 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8597 * Both payload and link header type follow the VLAN tags so that
8598 * both need to be updated.
8600 cstate
->off_linkpl
.constant_part
+= 4;
8601 cstate
->off_linktype
.constant_part
+= 4;
8606 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8607 /* add v to variable part of off */
8609 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8610 bpf_u_int32 v
, struct slist
*s
)
8614 if (!off
->is_variable
)
8615 off
->is_variable
= 1;
8617 off
->reg
= alloc_reg(cstate
);
8619 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8622 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8625 s2
= new_stmt(cstate
, BPF_ST
);
8631 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8632 * and link type offsets first
8635 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8639 /* offset determined at run time, shift variable part */
8641 cstate
->is_vlan_vloffset
= 1;
8642 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8643 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8645 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8646 sappend(s
.next
, b_tpid
->head
->stmts
);
8647 b_tpid
->head
->stmts
= s
.next
;
8651 * patch block b_vid (VLAN id test) to load VID value either from packet
8652 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8655 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8657 struct slist
*s
, *s2
, *sjeq
;
8660 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8661 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8663 /* true -> next instructions, false -> beginning of b_vid */
8664 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8666 sjeq
->s
.jf
= b_vid
->stmts
;
8669 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8670 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8674 /* Jump to the test in b_vid. We need to jump one instruction before
8675 * the end of the b_vid block so that we only skip loading the TCI
8676 * from packet data and not the 'and' instruction extracting VID.
8679 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8681 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8685 /* insert our statements at the beginning of b_vid */
8686 sappend(s
, b_vid
->stmts
);
8691 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8692 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8693 * tag can be either in metadata or in packet data; therefore if the
8694 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8695 * header for VLAN tag. As the decision is done at run time, we need
8696 * update variable part of the offsets
8698 static struct block
*
8699 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8702 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8705 /* generate new filter code based on extracting packet
8707 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8708 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8710 b0
= gen_jmp(cstate
, BPF_JEQ
, 1, s
);
8713 * This is tricky. We need to insert the statements updating variable
8714 * parts of offsets before the traditional TPID and VID tests so
8715 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8716 * we do not want this update to affect those checks. That's why we
8717 * generate both test blocks first and insert the statements updating
8718 * variable parts of both offsets after that. This wouldn't work if
8719 * there already were variable length link header when entering this
8720 * function but gen_vlan_bpf_extensions() isn't called in that case.
8722 b_tpid
= gen_vlan_tpid_test(cstate
);
8724 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8726 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8731 gen_vlan_patch_vid_test(cstate
, b_vid
);
8741 * support IEEE 802.1Q VLAN trunk over ethernet
8744 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8749 * Catch errors reported by us and routines below us, and return NULL
8752 if (setjmp(cstate
->top_ctx
))
8755 /* can't check for VLAN-encapsulated packets inside MPLS */
8756 if (cstate
->label_stack_depth
> 0)
8757 bpf_error(cstate
, "no VLAN match after MPLS");
8760 * Check for a VLAN packet, and then change the offsets to point
8761 * to the type and data fields within the VLAN packet. Just
8762 * increment the offsets, so that we can support a hierarchy, e.g.
8763 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
8766 * XXX - this is a bit of a kludge. If we were to split the
8767 * compiler into a parser that parses an expression and
8768 * generates an expression tree, and a code generator that
8769 * takes an expression tree (which could come from our
8770 * parser or from some other parser) and generates BPF code,
8771 * we could perhaps make the offsets parameters of routines
8772 * and, in the handler for an "AND" node, pass to subnodes
8773 * other than the VLAN node the adjusted offsets.
8775 * This would mean that "vlan" would, instead of changing the
8776 * behavior of *all* tests after it, change only the behavior
8777 * of tests ANDed with it. That would change the documented
8778 * semantics of "vlan", which might break some expressions.
8779 * However, it would mean that "(vlan and ip) or ip" would check
8780 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8781 * checking only for VLAN-encapsulated IP, so that could still
8782 * be considered worth doing; it wouldn't break expressions
8783 * that are of the form "vlan and ..." or "vlan N and ...",
8784 * which I suspect are the most common expressions involving
8785 * "vlan". "vlan or ..." doesn't necessarily do what the user
8786 * would really want, now, as all the "or ..." tests would
8787 * be done assuming a VLAN, even though the "or" could be viewed
8788 * as meaning "or, if this isn't a VLAN packet...".
8790 switch (cstate
->linktype
) {
8794 * Newer version of the Linux kernel pass around
8795 * packets in which the VLAN tag has been removed
8796 * from the packet data and put into metadata.
8798 * This requires special treatment.
8800 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8801 /* Verify that this is the outer part of the packet and
8802 * not encapsulated somehow. */
8803 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8804 cstate
->off_linkhdr
.constant_part
==
8805 cstate
->off_outermostlinkhdr
.constant_part
) {
8807 * Do we need special VLAN handling?
8809 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8810 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8813 b0
= gen_vlan_no_bpf_extensions(cstate
,
8814 vlan_num
, has_vlan_tag
);
8817 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8821 case DLT_NETANALYZER
:
8822 case DLT_NETANALYZER_TRANSPARENT
:
8823 case DLT_IEEE802_11
:
8824 case DLT_PRISM_HEADER
:
8825 case DLT_IEEE802_11_RADIO_AVS
:
8826 case DLT_IEEE802_11_RADIO
:
8828 * These are either Ethernet packets with an additional
8829 * metadata header (the NetAnalyzer types), or 802.11
8830 * packets, possibly with an additional metadata header.
8832 * For the first of those, the VLAN tag is in the normal
8833 * place, so the special-case handling above isn't
8836 * For the second of those, we don't do the special-case
8839 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8843 bpf_error(cstate
, "no VLAN support for %s",
8844 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8848 cstate
->vlan_stack_depth
++;
8856 * The label_num_arg dance is to avoid annoying whining by compilers that
8857 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8858 * It's not *used* after setjmp returns.
8860 static struct block
*
8861 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
8864 struct block
*b0
, *b1
;
8866 if (cstate
->label_stack_depth
> 0) {
8867 /* just match the bottom-of-stack bit clear */
8868 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8871 * We're not in an MPLS stack yet, so check the link-layer
8872 * type against MPLS.
8874 switch (cstate
->linktype
) {
8876 case DLT_C_HDLC
: /* fall through */
8879 case DLT_NETANALYZER
:
8880 case DLT_NETANALYZER_TRANSPARENT
:
8881 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8885 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8888 /* FIXME add other DLT_s ...
8889 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8890 * leave it for now */
8893 bpf_error(cstate
, "no MPLS support for %s",
8894 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8899 /* If a specific MPLS label is requested, check it */
8900 if (has_label_num
) {
8901 assert_maxval(cstate
, "MPLS label", label_num
, 0xFFFFF);
8902 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8903 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
8904 0xfffff000); /* only compare the first 20 bits */
8910 * Change the offsets to point to the type and data fields within
8911 * the MPLS packet. Just increment the offsets, so that we
8912 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8913 * capture packets with an outer label of 100000 and an inner
8916 * Increment the MPLS stack depth as well; this indicates that
8917 * we're checking MPLS-encapsulated headers, to make sure higher
8918 * level code generators don't try to match against IP-related
8919 * protocols such as Q_ARP, Q_RARP etc.
8921 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8923 cstate
->off_nl_nosnap
+= 4;
8924 cstate
->off_nl
+= 4;
8925 cstate
->label_stack_depth
++;
8930 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
8933 * Catch errors reported by us and routines below us, and return NULL
8936 if (setjmp(cstate
->top_ctx
))
8939 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
8943 * Support PPPOE discovery and session.
8946 gen_pppoed(compiler_state_t
*cstate
)
8949 * Catch errors reported by us and routines below us, and return NULL
8952 if (setjmp(cstate
->top_ctx
))
8955 /* check for PPPoE discovery */
8956 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
8960 * RFC 2516 Section 4:
8962 * The Ethernet payload for PPPoE is as follows:
8965 * 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
8966 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8967 * | VER | TYPE | CODE | SESSION_ID |
8968 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8969 * | LENGTH | payload ~
8970 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8973 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
8975 struct block
*b0
, *b1
;
8978 * Catch errors reported by us and routines below us, and return NULL
8981 if (setjmp(cstate
->top_ctx
))
8985 * Test against the PPPoE session link-layer type.
8987 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
8989 /* If a specific session is requested, check PPPoE session id */
8991 assert_maxval(cstate
, "PPPoE session number", sess_num
, UINT16_MAX
);
8992 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
8998 * Change the offsets to point to the type and data fields within
8999 * the PPP packet, and note that this is PPPoE rather than
9002 * XXX - this is a bit of a kludge. See the comments in
9005 * The "network-layer" protocol is PPPoE, which has a 6-byte
9006 * PPPoE header, followed by a PPP packet.
9008 * There is no HDLC encapsulation for the PPP packet (it's
9009 * encapsulated in PPPoES instead), so the link-layer type
9010 * starts at the first byte of the PPP packet. For PPPoE,
9011 * that offset is relative to the beginning of the total
9012 * link-layer payload, including any 802.2 LLC header, so
9013 * it's 6 bytes past cstate->off_nl.
9015 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9016 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9017 cstate
->off_linkpl
.reg
);
9019 cstate
->off_linktype
= cstate
->off_linkhdr
;
9020 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9023 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9028 /* Check that this is Geneve and the VNI is correct if
9029 * specified. Parameterized to handle both IPv4 and IPv6. */
9030 static struct block
*
9031 gen_geneve_check(compiler_state_t
*cstate
,
9032 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
9033 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9035 struct block
*b0
, *b1
;
9037 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9039 /* Check that we are operating on version 0. Otherwise, we
9040 * can't decode the rest of the fields. The version is 2 bits
9041 * in the first byte of the Geneve header. */
9042 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9047 assert_maxval(cstate
, "Geneve VNI", vni
, 0xffffff);
9048 vni
<<= 8; /* VNI is in the upper 3 bytes */
9049 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9057 /* The IPv4 and IPv6 Geneve checks need to do two things:
9058 * - Verify that this actually is Geneve with the right VNI.
9059 * - Place the IP header length (plus variable link prefix if
9060 * needed) into register A to be used later to compute
9061 * the inner packet offsets. */
9062 static struct block
*
9063 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9065 struct block
*b0
, *b1
;
9066 struct slist
*s
, *s1
;
9068 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9070 /* Load the IP header length into A. */
9071 s
= gen_loadx_iphdrlen(cstate
);
9073 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9076 /* Forcibly append these statements to the true condition
9077 * of the protocol check by creating a new block that is
9078 * always true and ANDing them. */
9079 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9086 static struct block
*
9087 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9089 struct block
*b0
, *b1
;
9090 struct slist
*s
, *s1
;
9092 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9094 /* Load the IP header length. We need to account for a
9095 * variable length link prefix if there is one. */
9096 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9098 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9102 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9106 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9110 /* Forcibly append these statements to the true condition
9111 * of the protocol check by creating a new block that is
9112 * always true and ANDing them. */
9113 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9116 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9123 /* We need to store three values based on the Geneve header::
9124 * - The offset of the linktype.
9125 * - The offset of the end of the Geneve header.
9126 * - The offset of the end of the encapsulated MAC header. */
9127 static struct slist
*
9128 gen_geneve_offsets(compiler_state_t
*cstate
)
9130 struct slist
*s
, *s1
, *s_proto
;
9132 /* First we need to calculate the offset of the Geneve header
9133 * itself. This is composed of the IP header previously calculated
9134 * (include any variable link prefix) and stored in A plus the
9135 * fixed sized headers (fixed link prefix, MAC length, and UDP
9137 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9138 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9140 /* Stash this in X since we'll need it later. */
9141 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9144 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9146 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9150 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9151 cstate
->off_linktype
.is_variable
= 1;
9152 cstate
->off_linktype
.constant_part
= 0;
9154 s1
= new_stmt(cstate
, BPF_ST
);
9155 s1
->s
.k
= cstate
->off_linktype
.reg
;
9158 /* Load the Geneve option length and mask and shift to get the
9159 * number of bytes. It is stored in the first byte of the Geneve
9161 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9165 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9169 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9173 /* Add in the rest of the Geneve base header. */
9174 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9178 /* Add the Geneve header length to its offset and store. */
9179 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9183 /* Set the encapsulated type as Ethernet. Even though we may
9184 * not actually have Ethernet inside there are two reasons this
9186 * - The linktype field is always in EtherType format regardless
9187 * of whether it is in Geneve or an inner Ethernet frame.
9188 * - The only link layer that we have specific support for is
9189 * Ethernet. We will confirm that the packet actually is
9190 * Ethernet at runtime before executing these checks. */
9191 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9193 s1
= new_stmt(cstate
, BPF_ST
);
9194 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9197 /* Calculate whether we have an Ethernet header or just raw IP/
9198 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9199 * and linktype by 14 bytes so that the network header can be found
9200 * seamlessly. Otherwise, keep what we've calculated already. */
9202 /* We have a bare jmp so we can't use the optimizer. */
9203 cstate
->no_optimize
= 1;
9205 /* Load the EtherType in the Geneve header, 2 bytes in. */
9206 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9210 /* Load X with the end of the Geneve header. */
9211 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9212 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9215 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9216 * end of this check, we should have the total length in X. In
9217 * the non-Ethernet case, it's already there. */
9218 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9219 s_proto
->s
.k
= ETHERTYPE_TEB
;
9220 sappend(s
, s_proto
);
9222 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9226 /* Since this is Ethernet, use the EtherType of the payload
9227 * directly as the linktype. Overwrite what we already have. */
9228 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9232 s1
= new_stmt(cstate
, BPF_ST
);
9233 s1
->s
.k
= cstate
->off_linktype
.reg
;
9236 /* Advance two bytes further to get the end of the Ethernet
9238 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9242 /* Move the result to X. */
9243 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9246 /* Store the final result of our linkpl calculation. */
9247 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9248 cstate
->off_linkpl
.is_variable
= 1;
9249 cstate
->off_linkpl
.constant_part
= 0;
9251 s1
= new_stmt(cstate
, BPF_STX
);
9252 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9261 /* Check to see if this is a Geneve packet. */
9263 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9265 struct block
*b0
, *b1
;
9269 * Catch errors reported by us and routines below us, and return NULL
9272 if (setjmp(cstate
->top_ctx
))
9275 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9276 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9281 /* Later filters should act on the payload of the Geneve frame,
9282 * update all of the header pointers. Attach this code so that
9283 * it gets executed in the event that the Geneve filter matches. */
9284 s
= gen_geneve_offsets(cstate
);
9286 b1
= gen_true(cstate
);
9287 sappend(s
, b1
->stmts
);
9292 cstate
->is_encap
= 1;
9297 /* Check that this is VXLAN and the VNI is correct if
9298 * specified. Parameterized to handle both IPv4 and IPv6. */
9299 static struct block
*
9300 gen_vxlan_check(compiler_state_t
*cstate
,
9301 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
9302 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9304 struct block
*b0
, *b1
;
9306 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9308 /* Check that the VXLAN header has the flag bits set
9310 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9315 assert_maxval(cstate
, "VXLAN VNI", vni
, 0xffffff);
9316 vni
<<= 8; /* VNI is in the upper 3 bytes */
9317 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9325 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9326 * - Verify that this actually is VXLAN with the right VNI.
9327 * - Place the IP header length (plus variable link prefix if
9328 * needed) into register A to be used later to compute
9329 * the inner packet offsets. */
9330 static struct block
*
9331 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9333 struct block
*b0
, *b1
;
9334 struct slist
*s
, *s1
;
9336 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9338 /* Load the IP header length into A. */
9339 s
= gen_loadx_iphdrlen(cstate
);
9341 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9344 /* Forcibly append these statements to the true condition
9345 * of the protocol check by creating a new block that is
9346 * always true and ANDing them. */
9347 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9354 static struct block
*
9355 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9357 struct block
*b0
, *b1
;
9358 struct slist
*s
, *s1
;
9360 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9362 /* Load the IP header length. We need to account for a
9363 * variable length link prefix if there is one. */
9364 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9366 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9370 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9374 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9378 /* Forcibly append these statements to the true condition
9379 * of the protocol check by creating a new block that is
9380 * always true and ANDing them. */
9381 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9384 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9391 /* We need to store three values based on the VXLAN header:
9392 * - The offset of the linktype.
9393 * - The offset of the end of the VXLAN header.
9394 * - The offset of the end of the encapsulated MAC header. */
9395 static struct slist
*
9396 gen_vxlan_offsets(compiler_state_t
*cstate
)
9398 struct slist
*s
, *s1
;
9400 /* Calculate the offset of the VXLAN header itself. This
9401 * includes the IP header computed previously (including any
9402 * variable link prefix) and stored in A plus the fixed size
9403 * headers (fixed link prefix, MAC length, UDP header). */
9404 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9405 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9407 /* Add the VXLAN header length to its offset and store */
9408 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9412 /* Push the link header. VXLAN packets always contain Ethernet
9414 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9416 s1
= new_stmt(cstate
, BPF_ST
);
9417 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9420 /* As the payload is an Ethernet packet, we can use the
9421 * EtherType of the payload directly as the linktype. */
9422 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9426 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9427 cstate
->off_linktype
.is_variable
= 1;
9428 cstate
->off_linktype
.constant_part
= 0;
9430 s1
= new_stmt(cstate
, BPF_ST
);
9431 s1
->s
.k
= cstate
->off_linktype
.reg
;
9434 /* Two bytes further is the end of the Ethernet header and the
9435 * start of the payload. */
9436 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9440 /* Move the result to X. */
9441 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9444 /* Store the final result of our linkpl calculation. */
9445 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9446 cstate
->off_linkpl
.is_variable
= 1;
9447 cstate
->off_linkpl
.constant_part
= 0;
9449 s1
= new_stmt(cstate
, BPF_STX
);
9450 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9458 /* Check to see if this is a VXLAN packet. */
9460 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9462 struct block
*b0
, *b1
;
9466 * Catch errors reported by us and routines below us, and return NULL
9469 if (setjmp(cstate
->top_ctx
))
9472 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9473 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9478 /* Later filters should act on the payload of the VXLAN frame,
9479 * update all of the header pointers. Attach this code so that
9480 * it gets executed in the event that the VXLAN filter matches. */
9481 s
= gen_vxlan_offsets(cstate
);
9483 b1
= gen_true(cstate
);
9484 sappend(s
, b1
->stmts
);
9489 cstate
->is_encap
= 1;
9494 /* Check that the encapsulated frame has a link layer header
9495 * for Ethernet filters. */
9496 static struct block
*
9497 gen_encap_ll_check(compiler_state_t
*cstate
)
9500 struct slist
*s
, *s1
;
9502 /* The easiest way to see if there is a link layer present
9503 * is to check if the link layer header and payload are not
9506 /* Geneve always generates pure variable offsets so we can
9507 * compare only the registers. */
9508 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9509 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9511 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9512 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9515 b0
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9521 static struct block
*
9522 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9523 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9525 assert_atm(cstate
, atmkw(atmfield
));
9530 assert_maxval(cstate
, "VPI", jvalue
, UINT8_MAX
);
9531 return gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9532 0xffffffffU
, jtype
, reverse
, jvalue
);
9535 assert_maxval(cstate
, "VCI", jvalue
, UINT16_MAX
);
9536 return gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9537 0xffffffffU
, jtype
, reverse
, jvalue
);
9544 static struct block
*
9545 gen_atm_vpi(compiler_state_t
*cstate
, const uint8_t v
)
9547 return gen_atmfield_code_internal(cstate
, A_VPI
, v
, BPF_JEQ
, 0);
9550 static struct block
*
9551 gen_atm_vci(compiler_state_t
*cstate
, const uint16_t v
)
9553 return gen_atmfield_code_internal(cstate
, A_VCI
, v
, BPF_JEQ
, 0);
9556 static struct block
*
9557 gen_atm_prototype(compiler_state_t
*cstate
, const uint8_t v
)
9559 return gen_mcmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, v
, 0x0fU
);
9562 static struct block
*
9563 gen_atmtype_llc(compiler_state_t
*cstate
)
9567 b0
= gen_atm_prototype(cstate
, PT_LLC
);
9568 cstate
->linktype
= cstate
->prevlinktype
;
9573 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9574 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9577 * Catch errors reported by us and routines below us, and return NULL
9580 if (setjmp(cstate
->top_ctx
))
9583 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9588 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9590 struct block
*b0
, *b1
;
9593 * Catch errors reported by us and routines below us, and return NULL
9596 if (setjmp(cstate
->top_ctx
))
9599 assert_atm(cstate
, atmkw(type
));
9604 /* Get all packets in Meta signalling Circuit */
9605 b0
= gen_atm_vpi(cstate
, 0);
9606 b1
= gen_atm_vci(cstate
, 1);
9611 /* Get all packets in Broadcast Circuit*/
9612 b0
= gen_atm_vpi(cstate
, 0);
9613 b1
= gen_atm_vci(cstate
, 2);
9618 /* Get all cells in Segment OAM F4 circuit*/
9619 b0
= gen_atm_vpi(cstate
, 0);
9620 b1
= gen_atm_vci(cstate
, 3);
9625 /* Get all cells in End-to-End OAM F4 Circuit*/
9626 b0
= gen_atm_vpi(cstate
, 0);
9627 b1
= gen_atm_vci(cstate
, 4);
9632 /* Get all packets in connection Signalling Circuit */
9633 b0
= gen_atm_vpi(cstate
, 0);
9634 b1
= gen_atm_vci(cstate
, 5);
9639 /* Get all packets in ILMI Circuit */
9640 b0
= gen_atm_vpi(cstate
, 0);
9641 b1
= gen_atm_vci(cstate
, 16);
9646 /* Get all LANE packets */
9647 b1
= gen_atm_prototype(cstate
, PT_LANE
);
9650 * Arrange that all subsequent tests assume LANE
9651 * rather than LLC-encapsulated packets, and set
9652 * the offsets appropriately for LANE-encapsulated
9655 * We assume LANE means Ethernet, not Token Ring.
9657 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9658 cstate
->off_payload
+ 2, /* Ethernet header */
9660 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9661 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9662 cstate
->off_nl
= 0; /* Ethernet II */
9663 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9672 * Filtering for MTP2 messages based on li value
9673 * FISU, length is null
9674 * LSSU, length is 1 or 2
9675 * MSU, length is 3 or more
9676 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9679 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9681 struct block
*b0
, *b1
;
9684 * Catch errors reported by us and routines below us, and return NULL
9687 if (setjmp(cstate
->top_ctx
))
9690 assert_ss7(cstate
, ss7kw(type
));
9695 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9696 0x3fU
, BPF_JEQ
, 0, 0U);
9699 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9700 0x3fU
, BPF_JGT
, 1, 2U);
9701 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9702 0x3fU
, BPF_JGT
, 0, 0U);
9707 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9708 0x3fU
, BPF_JGT
, 0, 2U);
9711 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9712 0xff80U
, BPF_JEQ
, 0, 0U);
9715 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9716 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9717 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9718 0xff80U
, BPF_JGT
, 0, 0U);
9723 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9724 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9732 * These maximum valid values are all-ones, so they double as the bitmasks
9733 * before any bitwise shifting.
9735 #define MTP2_SIO_MAXVAL UINT8_MAX
9736 #define MTP3_PC_MAXVAL 0x3fffU
9737 #define MTP3_SLS_MAXVAL 0xfU
9739 static struct block
*
9740 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
9741 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9748 newoff_sio
= cstate
->off_sio
;
9749 newoff_opc
= cstate
->off_opc
;
9750 newoff_dpc
= cstate
->off_dpc
;
9751 newoff_sls
= cstate
->off_sls
;
9753 assert_ss7(cstate
, ss7kw(mtp3field
));
9755 switch (mtp3field
) {
9758 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
9760 * SIO is the simplest field: the size is one byte and the offset is a
9761 * multiple of bytes, so the only detail to get right is the value of
9762 * the [right-to-left] field offset.
9765 newoff_sio
+= 3; /* offset for MTP2_HSL */
9769 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP2_SIO_MAXVAL
);
9770 // Here the bitmask means "do not apply a bitmask".
9771 return gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
9772 jtype
, reverse
, jvalue
);
9775 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
9777 * SLS, OPC and DPC are more complicated: none of these is sized in a
9778 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
9779 * diagrams are meant to be read right-to-left. This means in the
9780 * diagrams within individual fields and concatenations thereof
9781 * bitwise shifts and masks can be noted in the common left-to-right
9782 * manner until each final value is ready to be byte-swapped and
9783 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
9784 * similar problem in a similar way.
9786 * Offsets of fields within the packet header always have the
9787 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
9788 * DLTs the offset does not include the F (Flag) field at the
9789 * beginning of each message.
9791 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
9792 * 32-bit standard routing header has a 4 byte [RTL] offset and could
9793 * be tested entirely using a single BPF_W comparison. In this case
9794 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
9795 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
9796 * [LTR] bitmask would be (0xF << 28), all of which conveniently
9797 * correlates with the [RTL] packet diagram until the byte-swapping is
9800 * The code below uses this approach for OPC, which spans 3 bytes.
9801 * DPC and SLS use shorter loads, SLS also uses a different offset.
9808 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
9809 return gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
9810 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
9811 SWAPLONG(jvalue
<< 14));
9818 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
9819 return gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
9820 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
9828 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_SLS_MAXVAL
);
9829 return gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
9830 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
9839 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9840 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9843 * Catch errors reported by us and routines below us, and return NULL
9846 if (setjmp(cstate
->top_ctx
))
9849 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
9853 static struct block
*
9854 gen_msg_abbrev(compiler_state_t
*cstate
, const uint8_t type
)
9857 * Q.2931 signalling protocol messages for handling virtual circuits
9858 * establishment and teardown
9860 return gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
,
9865 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9867 struct block
*b0
, *b1
;
9870 * Catch errors reported by us and routines below us, and return NULL
9873 if (setjmp(cstate
->top_ctx
))
9876 assert_atm(cstate
, atmkw(type
));
9882 b0
= gen_atm_vci(cstate
, 3);
9883 b1
= gen_atm_vci(cstate
, 4);
9885 b0
= gen_atm_vpi(cstate
, 0);
9891 b0
= gen_atm_vci(cstate
, 3);
9892 b1
= gen_atm_vci(cstate
, 4);
9894 b0
= gen_atm_vpi(cstate
, 0);
9900 * Get Q.2931 signalling messages for switched
9901 * virtual connection
9903 b0
= gen_msg_abbrev(cstate
, SETUP
);
9904 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
9906 b0
= gen_msg_abbrev(cstate
, CONNECT
);
9908 b0
= gen_msg_abbrev(cstate
, CONNECT_ACK
);
9910 b0
= gen_msg_abbrev(cstate
, RELEASE
);
9912 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
9914 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9919 b0
= gen_msg_abbrev(cstate
, SETUP
);
9920 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
9922 b0
= gen_msg_abbrev(cstate
, CONNECT
);
9924 b0
= gen_msg_abbrev(cstate
, RELEASE
);
9926 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
9928 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);