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_ehostop(compiler_state_t
*, const u_char
*, int);
683 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
684 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
685 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
686 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
687 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
688 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
689 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
692 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
693 struct in6_addr
*, int, int, int);
696 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
697 struct addrinfo
*, int);
699 static struct block
*gen_ip_proto(compiler_state_t
*, const uint8_t);
700 static struct block
*gen_ip6_proto(compiler_state_t
*, const uint8_t);
701 static struct block
*gen_ipfrag(compiler_state_t
*);
702 static struct block
*gen_portatom(compiler_state_t
*, int, uint16_t);
703 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, uint16_t,
705 static struct block
*gen_portatom6(compiler_state_t
*, int, uint16_t);
706 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, uint16_t,
708 static struct block
*gen_port(compiler_state_t
*, uint16_t, int, int);
709 static struct block
*gen_port_common(compiler_state_t
*, int, struct block
*);
710 static struct block
*gen_portrange(compiler_state_t
*, uint16_t, uint16_t,
712 static struct block
*gen_port6(compiler_state_t
*, uint16_t, int, int);
713 static struct block
*gen_port6_common(compiler_state_t
*, int, struct block
*);
714 static struct block
*gen_portrange6(compiler_state_t
*, uint16_t, uint16_t,
716 static int lookup_proto(compiler_state_t
*, const char *, int);
717 #if !defined(NO_PROTOCHAIN)
718 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
719 #endif /* !defined(NO_PROTOCHAIN) */
720 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int);
721 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
722 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
723 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
724 static struct block
*gen_len(compiler_state_t
*, int, int);
725 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
727 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
728 bpf_u_int32
, int, int);
729 static struct block
*gen_atmtype_llc(compiler_state_t
*);
730 static struct block
*gen_msg_abbrev(compiler_state_t
*, const uint8_t);
731 static struct block
*gen_atm_prototype(compiler_state_t
*, const uint8_t);
734 initchunks(compiler_state_t
*cstate
)
738 for (i
= 0; i
< NCHUNKS
; i
++) {
739 cstate
->chunks
[i
].n_left
= 0;
740 cstate
->chunks
[i
].m
= NULL
;
742 cstate
->cur_chunk
= 0;
746 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
752 /* Round up to chunk alignment. */
753 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
755 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
756 if (n
> cp
->n_left
) {
758 k
= ++cstate
->cur_chunk
;
760 bpf_set_error(cstate
, "out of memory");
763 size
= CHUNK0SIZE
<< k
;
764 cp
->m
= (void *)malloc(size
);
766 bpf_set_error(cstate
, "out of memory");
769 memset((char *)cp
->m
, 0, size
);
772 bpf_set_error(cstate
, "out of memory");
777 return (void *)((char *)cp
->m
+ cp
->n_left
);
781 newchunk(compiler_state_t
*cstate
, size_t n
)
785 p
= newchunk_nolongjmp(cstate
, n
);
787 longjmp(cstate
->top_ctx
, 1);
794 freechunks(compiler_state_t
*cstate
)
798 for (i
= 0; i
< NCHUNKS
; ++i
)
799 if (cstate
->chunks
[i
].m
!= NULL
)
800 free(cstate
->chunks
[i
].m
);
804 * A strdup whose allocations are freed after code generation is over.
805 * This is used by the lexical analyzer, so it can't longjmp; it just
806 * returns NULL on an allocation error, and the callers must check
810 sdup(compiler_state_t
*cstate
, const char *s
)
812 size_t n
= strlen(s
) + 1;
813 char *cp
= newchunk_nolongjmp(cstate
, n
);
817 pcapint_strlcpy(cp
, s
, n
);
821 static inline struct block
*
822 new_block(compiler_state_t
*cstate
, int code
)
826 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
833 static inline struct slist
*
834 new_stmt(compiler_state_t
*cstate
, int code
)
838 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
844 static struct block
*
845 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
847 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
853 static struct block
*
854 gen_retblk(compiler_state_t
*cstate
, int v
)
856 if (setjmp(cstate
->top_ctx
)) {
858 * gen_retblk() only fails because a memory
859 * allocation failed in newchunk(), meaning
860 * that it can't return a pointer.
866 return gen_retblk_internal(cstate
, v
);
869 static inline PCAP_NORETURN_DEF
void
870 syntax(compiler_state_t
*cstate
)
872 bpf_error(cstate
, "syntax error in filter expression");
876 * For the given integer return a string with the keyword (or the nominal
877 * keyword if there is more than one). This is a simpler version of tok2str()
878 * in tcpdump because in this problem space a valid integer value is not
882 qual2kw(const char *kind
, const unsigned id
, const char *tokens
[],
885 static char buf
[4][64];
888 if (id
< size
&& tokens
[id
])
891 char *ret
= buf
[idx
];
892 idx
= (idx
+ 1) % (sizeof(buf
) / sizeof(buf
[0]));
893 ret
[0] = '\0'; // just in case
894 snprintf(ret
, sizeof(buf
[0]), "<invalid %s %u>", kind
, id
);
898 // protocol qualifier keywords
900 pqkw(const unsigned id
)
902 const char * tokens
[] = {
914 [Q_DECNET
] = "decnet",
920 [Q_ICMPV6
] = "icmp6",
932 [Q_NETBEUI
] = "netbeui",
935 [Q_ISIS_IIH
] = "iih",
936 [Q_ISIS_SNP
] = "snp",
937 [Q_ISIS_CSNP
] = "csnp",
938 [Q_ISIS_PSNP
] = "psnp",
939 [Q_ISIS_LSP
] = "lsp",
943 return qual2kw("proto", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
946 // direction qualifier keywords
948 dqkw(const unsigned id
)
950 const char * map
[] = {
953 [Q_OR
] = "src or dst",
954 [Q_AND
] = "src and dst",
962 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
967 atmkw(const unsigned id
)
969 const char * tokens
[] = {
972 [A_OAMF4SC
] = "oamf4sc",
973 [A_OAMF4EC
] = "oamf4ec",
981 [A_CONNECTMSG
] = "connectmsg",
982 [A_METACONNECT
] = "metaconnect",
984 return qual2kw("ATM keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
989 ss7kw(const unsigned id
)
991 const char * tokens
[] = {
1007 return qual2kw("MTP keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
1010 static PCAP_NORETURN_DEF
void
1011 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
1013 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
1014 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
1018 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
1020 if (cstate
->linktype
!= DLT_PFLOG
)
1021 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
1025 assert_atm(compiler_state_t
*cstate
, const char *kw
)
1028 * Belt and braces: init_linktype() sets either all of these struct
1029 * members (for DLT_SUNATM) or none (otherwise).
1031 if (cstate
->linktype
!= DLT_SUNATM
||
1033 cstate
->off_vpi
== OFFSET_NOT_SET
||
1034 cstate
->off_vci
== OFFSET_NOT_SET
||
1035 cstate
->off_proto
== OFFSET_NOT_SET
||
1036 cstate
->off_payload
== OFFSET_NOT_SET
)
1037 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1041 assert_ss7(compiler_state_t
*cstate
, const char *kw
)
1043 switch (cstate
->linktype
) {
1046 case DLT_MTP2_WITH_PHDR
:
1047 // Belt and braces, same as in assert_atm().
1048 if (cstate
->off_sio
!= OFFSET_NOT_SET
&&
1049 cstate
->off_opc
!= OFFSET_NOT_SET
&&
1050 cstate
->off_dpc
!= OFFSET_NOT_SET
&&
1051 cstate
->off_sls
!= OFFSET_NOT_SET
)
1054 bpf_error(cstate
, "'%s' supported only on SS7", kw
);
1058 assert_maxval(compiler_state_t
*cstate
, const char *name
,
1059 const bpf_u_int32 val
, const bpf_u_int32 maxval
)
1062 bpf_error(cstate
, "%s %u greater than maximum %u",
1066 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1067 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1069 // Validate a port/portrange proto qualifier and map to an IP protocol number.
1071 port_pq_to_ipproto(compiler_state_t
*cstate
, const int proto
, const char *kw
)
1079 return IPPROTO_SCTP
;
1083 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), kw
);
1087 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1088 const char *buf
, int optimize
, bpf_u_int32 mask
)
1094 compiler_state_t cstate
;
1095 yyscan_t scanner
= NULL
;
1096 YY_BUFFER_STATE in_buffer
= NULL
;
1101 * If this pcap_t hasn't been activated, it doesn't have a
1102 * link-layer type, so we can't use it.
1104 if (!p
->activated
) {
1105 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1106 "not-yet-activated pcap_t passed to pcap_compile");
1107 return (PCAP_ERROR
);
1112 * Initialize Winsock, asking for the latest version (2.2),
1113 * as we may be calling Winsock routines to translate
1114 * host names to addresses.
1116 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1118 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1119 err
, "Error calling WSAStartup()");
1120 return (PCAP_ERROR
);
1124 #ifdef ENABLE_REMOTE
1126 * If the device on which we're capturing need to be notified
1127 * that a new filter is being compiled, do so.
1129 * This allows them to save a copy of it, in case, for example,
1130 * they're implementing a form of remote packet capture, and
1131 * want the remote machine to filter out the packets in which
1132 * it's sending the packets it's captured.
1134 * XXX - the fact that we happen to be compiling a filter
1135 * doesn't necessarily mean we'll be installing it as the
1136 * filter for this pcap_t; we might be running it from userland
1137 * on captured packets to do packet classification. We really
1138 * need a better way of handling this, but this is all that
1139 * the WinPcap remote capture code did.
1141 if (p
->save_current_filter_op
!= NULL
)
1142 (p
->save_current_filter_op
)(p
, buf
);
1145 initchunks(&cstate
);
1146 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) {
1187 if (cstate
.ai
!= NULL
)
1188 freeaddrinfo(cstate
.ai
);
1190 if (cstate
.e
!= NULL
)
1196 if (cstate
.ic
.root
== NULL
) {
1197 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1200 * Catch errors reported by gen_retblk().
1202 if (cstate
.ic
.root
== NULL
) {
1208 if (optimize
&& !cstate
.no_optimize
) {
1209 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1214 if (cstate
.ic
.root
== NULL
||
1215 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1216 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1217 "expression rejects all packets");
1222 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1223 cstate
.ic
.root
, &len
, p
->errbuf
);
1224 if (program
->bf_insns
== NULL
) {
1229 program
->bf_len
= len
;
1231 rc
= 0; /* We're all okay */
1235 * Clean up everything for the lexical analyzer.
1237 if (in_buffer
!= NULL
)
1238 pcap__delete_buffer(in_buffer
, scanner
);
1239 if (scanner
!= NULL
)
1240 pcap_lex_destroy(scanner
);
1243 * Clean up our own allocated memory.
1245 freechunks(&cstate
);
1255 * entry point for using the compiler with no pcap open
1256 * pass in all the stuff that is needed explicitly instead.
1259 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1260 struct bpf_program
*program
,
1261 const char *buf
, int optimize
, bpf_u_int32 mask
)
1266 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1268 return (PCAP_ERROR
);
1269 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1275 * Clean up a "struct bpf_program" by freeing all the memory allocated
1279 pcap_freecode(struct bpf_program
*program
)
1281 program
->bf_len
= 0;
1282 if (program
->bf_insns
!= NULL
) {
1283 free((char *)program
->bf_insns
);
1284 program
->bf_insns
= NULL
;
1289 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1290 * which of the jt and jf fields has been resolved and which is a pointer
1291 * back to another unresolved block (or nil). At least one of the fields
1292 * in each block is already resolved.
1295 backpatch(struct block
*list
, struct block
*target
)
1312 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1313 * which of jt and jf is the link.
1316 merge(struct block
*b0
, struct block
*b1
)
1318 register struct block
**p
= &b0
;
1320 /* Find end of list. */
1322 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1324 /* Concatenate the lists. */
1329 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1332 * Catch errors reported by us and routines below us, and return -1
1335 if (setjmp(cstate
->top_ctx
))
1339 * Insert before the statements of the first (root) block any
1340 * statements needed to load the lengths of any variable-length
1341 * headers into registers.
1343 * XXX - a fancier strategy would be to insert those before the
1344 * statements of all blocks that use those lengths and that
1345 * have no predecessors that use them, so that we only compute
1346 * the lengths if we need them. There might be even better
1347 * approaches than that.
1349 * However, those strategies would be more complicated, and
1350 * as we don't generate code to compute a length if the
1351 * program has no tests that use the length, and as most
1352 * tests will probably use those lengths, we would just
1353 * postpone computing the lengths so that it's not done
1354 * for tests that fail early, and it's not clear that's
1357 insert_compute_vloffsets(cstate
, p
->head
);
1360 * For DLT_PPI captures, generate a check of the per-packet
1361 * DLT value to make sure it's DLT_IEEE802_11.
1363 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1364 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1365 * with appropriate Ethernet information and use that rather
1366 * than using something such as DLT_PPI where you don't know
1367 * the link-layer header type until runtime, which, in the
1368 * general case, would force us to generate both Ethernet *and*
1369 * 802.11 code (*and* anything else for which PPI is used)
1370 * and choose between them early in the BPF program?
1372 if (cstate
->linktype
== DLT_PPI
) {
1373 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1374 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1375 gen_and(ppi_dlt_check
, p
);
1378 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1379 p
->sense
= !p
->sense
;
1380 backpatch(p
, gen_retblk_internal(cstate
, 0));
1381 cstate
->ic
.root
= p
->head
;
1386 gen_and(struct block
*b0
, struct block
*b1
)
1388 backpatch(b0
, b1
->head
);
1389 b0
->sense
= !b0
->sense
;
1390 b1
->sense
= !b1
->sense
;
1392 b1
->sense
= !b1
->sense
;
1393 b1
->head
= b0
->head
;
1397 gen_or(struct block
*b0
, struct block
*b1
)
1399 b0
->sense
= !b0
->sense
;
1400 backpatch(b0
, b1
->head
);
1401 b0
->sense
= !b0
->sense
;
1403 b1
->head
= b0
->head
;
1407 gen_not(struct block
*b
)
1409 b
->sense
= !b
->sense
;
1412 static struct block
*
1413 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1414 u_int size
, bpf_u_int32 v
)
1416 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1419 static struct block
*
1420 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1421 u_int size
, bpf_u_int32 v
)
1423 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1426 static struct block
*
1427 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1428 u_int size
, bpf_u_int32 v
)
1430 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1433 static struct block
*
1434 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1435 u_int size
, bpf_u_int32 v
)
1437 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1440 static struct block
*
1441 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1442 u_int size
, bpf_u_int32 v
)
1444 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1447 static struct block
*
1448 gen_cmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1449 u_int size
, bpf_u_int32 v
)
1451 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 1, v
);
1454 static struct block
*
1455 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1456 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1458 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1461 static struct block
*
1462 gen_mcmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1463 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1465 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 1, v
);
1468 static struct block
*
1469 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1470 u_int size
, const u_char
*v
)
1472 register struct block
*b
, *tmp
;
1476 register const u_char
*p
= &v
[size
- 4];
1478 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1486 register const u_char
*p
= &v
[size
- 2];
1488 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1496 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1504 static struct block
*
1505 gen_jmp(compiler_state_t
*cstate
, int jtype
, bpf_u_int32 v
, struct slist
*stmts
)
1507 struct block
*b
= new_block(cstate
, JMP(jtype
));
1513 static struct block
*
1514 gen_set(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1516 return gen_jmp(cstate
, BPF_JSET
, v
, stmts
);
1519 static struct block
*
1520 gen_unset(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1522 struct block
*b
= gen_set(cstate
, v
, stmts
);
1528 * AND the field of size "size" at offset "offset" relative to the header
1529 * specified by "offrel" with "mask", and compare it with the value "v"
1530 * with the test specified by "jtype"; if "reverse" is true, the test
1531 * should test the opposite of "jtype".
1533 static struct block
*
1534 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1535 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1538 struct slist
*s
, *s2
;
1541 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1543 if (mask
!= 0xffffffff) {
1544 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1549 b
= gen_jmp(cstate
, jtype
, v
, s
);
1556 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1558 cstate
->pcap_fddipad
= p
->fddipad
;
1561 * We start out with only one link-layer header.
1563 cstate
->outermostlinktype
= pcap_datalink(p
);
1564 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1565 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1566 cstate
->off_outermostlinkhdr
.reg
= -1;
1568 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1569 cstate
->off_prevlinkhdr
.constant_part
= 0;
1570 cstate
->off_prevlinkhdr
.is_variable
= 0;
1571 cstate
->off_prevlinkhdr
.reg
= -1;
1573 cstate
->linktype
= cstate
->outermostlinktype
;
1574 cstate
->off_linkhdr
.constant_part
= 0;
1575 cstate
->off_linkhdr
.is_variable
= 0;
1576 cstate
->off_linkhdr
.reg
= -1;
1581 cstate
->off_linkpl
.constant_part
= 0;
1582 cstate
->off_linkpl
.is_variable
= 0;
1583 cstate
->off_linkpl
.reg
= -1;
1585 cstate
->off_linktype
.constant_part
= 0;
1586 cstate
->off_linktype
.is_variable
= 0;
1587 cstate
->off_linktype
.reg
= -1;
1590 * Assume it's not raw ATM with a pseudo-header, for now.
1593 cstate
->off_vpi
= OFFSET_NOT_SET
;
1594 cstate
->off_vci
= OFFSET_NOT_SET
;
1595 cstate
->off_proto
= OFFSET_NOT_SET
;
1596 cstate
->off_payload
= OFFSET_NOT_SET
;
1599 * And not encapsulated with either Geneve or VXLAN.
1601 cstate
->is_encap
= 0;
1604 * No variable length VLAN offset by default
1606 cstate
->is_vlan_vloffset
= 0;
1609 * And assume we're not doing SS7.
1611 cstate
->off_li
= OFFSET_NOT_SET
;
1612 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1613 cstate
->off_sio
= OFFSET_NOT_SET
;
1614 cstate
->off_opc
= OFFSET_NOT_SET
;
1615 cstate
->off_dpc
= OFFSET_NOT_SET
;
1616 cstate
->off_sls
= OFFSET_NOT_SET
;
1618 cstate
->label_stack_depth
= 0;
1619 cstate
->vlan_stack_depth
= 0;
1621 switch (cstate
->linktype
) {
1624 cstate
->off_linktype
.constant_part
= 2;
1625 cstate
->off_linkpl
.constant_part
= 6;
1626 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1627 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1630 case DLT_ARCNET_LINUX
:
1631 cstate
->off_linktype
.constant_part
= 4;
1632 cstate
->off_linkpl
.constant_part
= 8;
1633 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1634 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1638 cstate
->off_linktype
.constant_part
= 12;
1639 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1640 cstate
->off_nl
= 0; /* Ethernet II */
1641 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1646 * SLIP doesn't have a link level type. The 16 byte
1647 * header is hacked into our SLIP driver.
1649 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1650 cstate
->off_linkpl
.constant_part
= 16;
1652 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1655 case DLT_SLIP_BSDOS
:
1656 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1657 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1659 cstate
->off_linkpl
.constant_part
= 24;
1661 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1666 cstate
->off_linktype
.constant_part
= 0;
1667 cstate
->off_linkpl
.constant_part
= 4;
1669 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1673 cstate
->off_linktype
.constant_part
= 0;
1674 cstate
->off_linkpl
.constant_part
= 12;
1676 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1681 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1682 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1683 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1684 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1685 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1687 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1692 * This does not include the Ethernet header, and
1693 * only covers session state.
1695 cstate
->off_linktype
.constant_part
= 6;
1696 cstate
->off_linkpl
.constant_part
= 8;
1698 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1702 cstate
->off_linktype
.constant_part
= 5;
1703 cstate
->off_linkpl
.constant_part
= 24;
1705 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1710 * FDDI doesn't really have a link-level type field.
1711 * We set "off_linktype" to the offset of the LLC header.
1713 * To check for Ethernet types, we assume that SSAP = SNAP
1714 * is being used and pick out the encapsulated Ethernet type.
1715 * XXX - should we generate code to check for SNAP?
1717 cstate
->off_linktype
.constant_part
= 13;
1718 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1719 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1720 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1721 cstate
->off_nl
= 8; /* 802.2+SNAP */
1722 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1727 * Token Ring doesn't really have a link-level type field.
1728 * We set "off_linktype" to the offset of the LLC header.
1730 * To check for Ethernet types, we assume that SSAP = SNAP
1731 * is being used and pick out the encapsulated Ethernet type.
1732 * XXX - should we generate code to check for SNAP?
1734 * XXX - the header is actually variable-length.
1735 * Some various Linux patched versions gave 38
1736 * as "off_linktype" and 40 as "off_nl"; however,
1737 * if a token ring packet has *no* routing
1738 * information, i.e. is not source-routed, the correct
1739 * values are 20 and 22, as they are in the vanilla code.
1741 * A packet is source-routed iff the uppermost bit
1742 * of the first byte of the source address, at an
1743 * offset of 8, has the uppermost bit set. If the
1744 * packet is source-routed, the total number of bytes
1745 * of routing information is 2 plus bits 0x1F00 of
1746 * the 16-bit value at an offset of 14 (shifted right
1747 * 8 - figure out which byte that is).
1749 cstate
->off_linktype
.constant_part
= 14;
1750 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1751 cstate
->off_nl
= 8; /* 802.2+SNAP */
1752 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1755 case DLT_PRISM_HEADER
:
1756 case DLT_IEEE802_11_RADIO_AVS
:
1757 case DLT_IEEE802_11_RADIO
:
1758 cstate
->off_linkhdr
.is_variable
= 1;
1759 /* Fall through, 802.11 doesn't have a variable link
1760 * prefix but is otherwise the same. */
1763 case DLT_IEEE802_11
:
1765 * 802.11 doesn't really have a link-level type field.
1766 * We set "off_linktype.constant_part" to the offset of
1769 * To check for Ethernet types, we assume that SSAP = SNAP
1770 * is being used and pick out the encapsulated Ethernet type.
1771 * XXX - should we generate code to check for SNAP?
1773 * We also handle variable-length radio headers here.
1774 * The Prism header is in theory variable-length, but in
1775 * practice it's always 144 bytes long. However, some
1776 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1777 * sometimes or always supply an AVS header, so we
1778 * have to check whether the radio header is a Prism
1779 * header or an AVS header, so, in practice, it's
1782 cstate
->off_linktype
.constant_part
= 24;
1783 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1784 cstate
->off_linkpl
.is_variable
= 1;
1785 cstate
->off_nl
= 8; /* 802.2+SNAP */
1786 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1791 * At the moment we treat PPI the same way that we treat
1792 * normal Radiotap encoded packets. The difference is in
1793 * the function that generates the code at the beginning
1794 * to compute the header length. Since this code generator
1795 * of PPI supports bare 802.11 encapsulation only (i.e.
1796 * the encapsulated DLT should be DLT_IEEE802_11) we
1797 * generate code to check for this too.
1799 cstate
->off_linktype
.constant_part
= 24;
1800 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1801 cstate
->off_linkpl
.is_variable
= 1;
1802 cstate
->off_linkhdr
.is_variable
= 1;
1803 cstate
->off_nl
= 8; /* 802.2+SNAP */
1804 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1807 case DLT_ATM_RFC1483
:
1808 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1810 * assume routed, non-ISO PDUs
1811 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1813 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1814 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1815 * latter would presumably be treated the way PPPoE
1816 * should be, so you can do "pppoe and udp port 2049"
1817 * or "pppoa and tcp port 80" and have it check for
1818 * PPPo{A,E} and a PPP protocol of IP and....
1820 cstate
->off_linktype
.constant_part
= 0;
1821 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1822 cstate
->off_nl
= 8; /* 802.2+SNAP */
1823 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1828 * Full Frontal ATM; you get AALn PDUs with an ATM
1832 cstate
->off_vpi
= SUNATM_VPI_POS
;
1833 cstate
->off_vci
= SUNATM_VCI_POS
;
1834 cstate
->off_proto
= PROTO_POS
;
1835 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1836 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1837 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1838 cstate
->off_nl
= 8; /* 802.2+SNAP */
1839 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1845 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1846 cstate
->off_linkpl
.constant_part
= 0;
1848 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1851 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1852 cstate
->off_linktype
.constant_part
= 14;
1853 cstate
->off_linkpl
.constant_part
= 16;
1855 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1858 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1859 cstate
->off_linktype
.constant_part
= 0;
1860 cstate
->off_linkpl
.constant_part
= 20;
1862 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1867 * LocalTalk does have a 1-byte type field in the LLAP header,
1868 * but really it just indicates whether there is a "short" or
1869 * "long" DDP packet following.
1871 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1872 cstate
->off_linkpl
.constant_part
= 0;
1874 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1877 case DLT_IP_OVER_FC
:
1879 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1880 * link-level type field. We set "off_linktype" to the
1881 * offset of the LLC header.
1883 * To check for Ethernet types, we assume that SSAP = SNAP
1884 * is being used and pick out the encapsulated Ethernet type.
1885 * XXX - should we generate code to check for SNAP? RFC
1886 * 2625 says SNAP should be used.
1888 cstate
->off_linktype
.constant_part
= 16;
1889 cstate
->off_linkpl
.constant_part
= 16;
1890 cstate
->off_nl
= 8; /* 802.2+SNAP */
1891 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1896 * XXX - we should set this to handle SNAP-encapsulated
1897 * frames (NLPID of 0x80).
1899 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1900 cstate
->off_linkpl
.constant_part
= 0;
1902 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1906 * the only BPF-interesting FRF.16 frames are non-control frames;
1907 * Frame Relay has a variable length link-layer
1908 * so lets start with offset 4 for now and increments later on (FIXME);
1911 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1912 cstate
->off_linkpl
.constant_part
= 0;
1914 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1917 case DLT_APPLE_IP_OVER_IEEE1394
:
1918 cstate
->off_linktype
.constant_part
= 16;
1919 cstate
->off_linkpl
.constant_part
= 18;
1921 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1924 case DLT_SYMANTEC_FIREWALL
:
1925 cstate
->off_linktype
.constant_part
= 6;
1926 cstate
->off_linkpl
.constant_part
= 44;
1927 cstate
->off_nl
= 0; /* Ethernet II */
1928 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1932 cstate
->off_linktype
.constant_part
= 0;
1933 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1934 cstate
->off_linkpl
.is_variable
= 1;
1936 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1939 case DLT_JUNIPER_MFR
:
1940 case DLT_JUNIPER_MLFR
:
1941 case DLT_JUNIPER_MLPPP
:
1942 case DLT_JUNIPER_PPP
:
1943 case DLT_JUNIPER_CHDLC
:
1944 case DLT_JUNIPER_FRELAY
:
1945 cstate
->off_linktype
.constant_part
= 4;
1946 cstate
->off_linkpl
.constant_part
= 4;
1948 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1951 case DLT_JUNIPER_ATM1
:
1952 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1953 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1955 cstate
->off_nl_nosnap
= 10;
1958 case DLT_JUNIPER_ATM2
:
1959 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1960 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1962 cstate
->off_nl_nosnap
= 10;
1965 /* frames captured on a Juniper PPPoE service PIC
1966 * contain raw ethernet frames */
1967 case DLT_JUNIPER_PPPOE
:
1968 case DLT_JUNIPER_ETHER
:
1969 cstate
->off_linkpl
.constant_part
= 14;
1970 cstate
->off_linktype
.constant_part
= 16;
1971 cstate
->off_nl
= 18; /* Ethernet II */
1972 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1975 case DLT_JUNIPER_PPPOE_ATM
:
1976 cstate
->off_linktype
.constant_part
= 4;
1977 cstate
->off_linkpl
.constant_part
= 6;
1979 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1982 case DLT_JUNIPER_GGSN
:
1983 cstate
->off_linktype
.constant_part
= 6;
1984 cstate
->off_linkpl
.constant_part
= 12;
1986 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1989 case DLT_JUNIPER_ES
:
1990 cstate
->off_linktype
.constant_part
= 6;
1991 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1992 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1993 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1996 case DLT_JUNIPER_MONITOR
:
1997 cstate
->off_linktype
.constant_part
= 12;
1998 cstate
->off_linkpl
.constant_part
= 12;
1999 cstate
->off_nl
= 0; /* raw IP/IP6 header */
2000 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2003 case DLT_BACNET_MS_TP
:
2004 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2005 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2006 cstate
->off_nl
= OFFSET_NOT_SET
;
2007 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2010 case DLT_JUNIPER_SERVICES
:
2011 cstate
->off_linktype
.constant_part
= 12;
2012 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2013 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2014 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2017 case DLT_JUNIPER_VP
:
2018 cstate
->off_linktype
.constant_part
= 18;
2019 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2020 cstate
->off_nl
= OFFSET_NOT_SET
;
2021 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2024 case DLT_JUNIPER_ST
:
2025 cstate
->off_linktype
.constant_part
= 18;
2026 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2027 cstate
->off_nl
= OFFSET_NOT_SET
;
2028 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2031 case DLT_JUNIPER_ISM
:
2032 cstate
->off_linktype
.constant_part
= 8;
2033 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2034 cstate
->off_nl
= OFFSET_NOT_SET
;
2035 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2038 case DLT_JUNIPER_VS
:
2039 case DLT_JUNIPER_SRX_E2E
:
2040 case DLT_JUNIPER_FIBRECHANNEL
:
2041 case DLT_JUNIPER_ATM_CEMIC
:
2042 cstate
->off_linktype
.constant_part
= 8;
2043 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2044 cstate
->off_nl
= OFFSET_NOT_SET
;
2045 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2050 cstate
->off_li_hsl
= 4;
2051 cstate
->off_sio
= 3;
2052 cstate
->off_opc
= 4;
2053 cstate
->off_dpc
= 4;
2054 cstate
->off_sls
= 7;
2055 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2056 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2057 cstate
->off_nl
= OFFSET_NOT_SET
;
2058 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2061 case DLT_MTP2_WITH_PHDR
:
2063 cstate
->off_li_hsl
= 8;
2064 cstate
->off_sio
= 7;
2065 cstate
->off_opc
= 8;
2066 cstate
->off_dpc
= 8;
2067 cstate
->off_sls
= 11;
2068 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2069 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2070 cstate
->off_nl
= OFFSET_NOT_SET
;
2071 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2075 cstate
->off_li
= 22;
2076 cstate
->off_li_hsl
= 24;
2077 cstate
->off_sio
= 23;
2078 cstate
->off_opc
= 24;
2079 cstate
->off_dpc
= 24;
2080 cstate
->off_sls
= 27;
2081 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2082 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2083 cstate
->off_nl
= OFFSET_NOT_SET
;
2084 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2088 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2089 cstate
->off_linkpl
.constant_part
= 4;
2091 cstate
->off_nl_nosnap
= 0;
2096 * Currently, only raw "link[N:M]" filtering is supported.
2098 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
2099 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2100 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
2101 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2105 cstate
->off_linktype
.constant_part
= 1;
2106 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2108 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2111 case DLT_NETANALYZER
:
2112 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
2113 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2114 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
2115 cstate
->off_nl
= 0; /* Ethernet II */
2116 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2119 case DLT_NETANALYZER_TRANSPARENT
:
2120 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2121 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2122 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2123 cstate
->off_nl
= 0; /* Ethernet II */
2124 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2129 * For values in the range in which we've assigned new
2130 * DLT_ values, only raw "link[N:M]" filtering is supported.
2132 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2133 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2134 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2135 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2136 cstate
->off_nl
= OFFSET_NOT_SET
;
2137 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2139 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2140 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2146 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2151 * Load a value relative to the specified absolute offset.
2153 static struct slist
*
2154 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2155 u_int offset
, u_int size
)
2157 struct slist
*s
, *s2
;
2159 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2162 * If "s" is non-null, it has code to arrange that the X register
2163 * contains the variable part of the absolute offset, so we
2164 * generate a load relative to that, with an offset of
2165 * abs_offset->constant_part + offset.
2167 * Otherwise, we can do an absolute load with an offset of
2168 * abs_offset->constant_part + offset.
2172 * "s" points to a list of statements that puts the
2173 * variable part of the absolute offset into the X register.
2174 * Do an indirect load, to use the X register as an offset.
2176 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2177 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2181 * There is no variable part of the absolute offset, so
2182 * just do an absolute load.
2184 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2185 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2191 * Load a value relative to the beginning of the specified header.
2193 static struct slist
*
2194 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2197 struct slist
*s
, *s2
;
2200 * Squelch warnings from compilers that *don't* assume that
2201 * offrel always has a valid enum value and therefore don't
2202 * assume that we'll always go through one of the case arms.
2204 * If we have a default case, compilers that *do* assume that
2205 * will then complain about the default case code being
2208 * Damned if you do, damned if you don't.
2215 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2220 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2223 case OR_PREVLINKHDR
:
2224 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2228 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2231 case OR_PREVMPLSHDR
:
2232 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2236 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2239 case OR_LINKPL_NOSNAP
:
2240 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2244 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2249 * Load the X register with the length of the IPv4 header
2250 * (plus the offset of the link-layer header, if it's
2251 * preceded by a variable-length header such as a radio
2252 * header), in bytes.
2254 s
= gen_loadx_iphdrlen(cstate
);
2257 * Load the item at {offset of the link-layer payload} +
2258 * {offset, relative to the start of the link-layer
2259 * payload, of the IPv4 header} + {length of the IPv4 header} +
2260 * {specified offset}.
2262 * If the offset of the link-layer payload is variable,
2263 * the variable part of that offset is included in the
2264 * value in the X register, and we include the constant
2265 * part in the offset of the load.
2267 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2268 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2273 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2280 * Generate code to load into the X register the sum of the length of
2281 * the IPv4 header and the variable part of the offset of the link-layer
2284 static struct slist
*
2285 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2287 struct slist
*s
, *s2
;
2289 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2292 * The offset of the link-layer payload has a variable
2293 * part. "s" points to a list of statements that put
2294 * the variable part of that offset into the X register.
2296 * The 4*([k]&0xf) addressing mode can't be used, as we
2297 * don't have a constant offset, so we have to load the
2298 * value in question into the A register and add to it
2299 * the value from the X register.
2301 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2302 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2304 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2307 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2312 * The A register now contains the length of the IP header.
2313 * We need to add to it the variable part of the offset of
2314 * the link-layer payload, which is still in the X
2315 * register, and move the result into the X register.
2317 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2318 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2321 * The offset of the link-layer payload is a constant,
2322 * so no code was generated to load the (nonexistent)
2323 * variable part of that offset.
2325 * This means we can use the 4*([k]&0xf) addressing
2326 * mode. Load the length of the IPv4 header, which
2327 * is at an offset of cstate->off_nl from the beginning of
2328 * the link-layer payload, and thus at an offset of
2329 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2330 * of the raw packet data, using that addressing mode.
2332 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2333 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2339 static struct block
*
2340 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2344 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2346 return gen_jmp(cstate
, BPF_JEQ
, 0, s
);
2349 static inline struct block
*
2350 gen_true(compiler_state_t
*cstate
)
2352 return gen_uncond(cstate
, 1);
2355 static inline struct block
*
2356 gen_false(compiler_state_t
*cstate
)
2358 return gen_uncond(cstate
, 0);
2362 * Generate code to match a particular packet type.
2364 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2365 * value, if <= ETHERMTU. We use that to determine whether to
2366 * match the type/length field or to check the type/length field for
2367 * a value <= ETHERMTU to see whether it's a type field and then do
2368 * the appropriate test.
2370 static struct block
*
2371 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2373 struct block
*b0
, *b1
;
2379 case LLCSAP_NETBEUI
:
2381 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2382 * so we check the DSAP and SSAP.
2384 * LLCSAP_IP checks for IP-over-802.2, rather
2385 * than IP-over-Ethernet or IP-over-SNAP.
2387 * XXX - should we check both the DSAP and the
2388 * SSAP, like this, or should we check just the
2389 * DSAP, as we do for other types <= ETHERMTU
2390 * (i.e., other SAP values)?
2392 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2393 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2401 * Ethernet_II frames, which are Ethernet
2402 * frames with a frame type of ETHERTYPE_IPX;
2404 * Ethernet_802.3 frames, which are 802.3
2405 * frames (i.e., the type/length field is
2406 * a length field, <= ETHERMTU, rather than
2407 * a type field) with the first two bytes
2408 * after the Ethernet/802.3 header being
2411 * Ethernet_802.2 frames, which are 802.3
2412 * frames with an 802.2 LLC header and
2413 * with the IPX LSAP as the DSAP in the LLC
2416 * Ethernet_SNAP frames, which are 802.3
2417 * frames with an LLC header and a SNAP
2418 * header and with an OUI of 0x000000
2419 * (encapsulated Ethernet) and a protocol
2420 * ID of ETHERTYPE_IPX in the SNAP header.
2422 * XXX - should we generate the same code both
2423 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2427 * This generates code to check both for the
2428 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2430 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2431 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2435 * Now we add code to check for SNAP frames with
2436 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2438 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2442 * Now we generate code to check for 802.3
2443 * frames in general.
2445 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2448 * Now add the check for 802.3 frames before the
2449 * check for Ethernet_802.2 and Ethernet_802.3,
2450 * as those checks should only be done on 802.3
2451 * frames, not on Ethernet frames.
2456 * Now add the check for Ethernet_II frames, and
2457 * do that before checking for the other frame
2460 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2464 case ETHERTYPE_ATALK
:
2465 case ETHERTYPE_AARP
:
2467 * EtherTalk (AppleTalk protocols on Ethernet link
2468 * layer) may use 802.2 encapsulation.
2472 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2473 * we check for an Ethernet type field less or equal than
2474 * 1500, which means it's an 802.3 length field.
2476 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2479 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2480 * SNAP packets with an organization code of
2481 * 0x080007 (Apple, for Appletalk) and a protocol
2482 * type of ETHERTYPE_ATALK (Appletalk).
2484 * 802.2-encapsulated ETHERTYPE_AARP packets are
2485 * SNAP packets with an organization code of
2486 * 0x000000 (encapsulated Ethernet) and a protocol
2487 * type of ETHERTYPE_AARP (Appletalk ARP).
2489 if (ll_proto
== ETHERTYPE_ATALK
)
2490 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2491 else /* ll_proto == ETHERTYPE_AARP */
2492 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2496 * Check for Ethernet encapsulation (Ethertalk
2497 * phase 1?); we just check for the Ethernet
2500 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2506 if (ll_proto
<= ETHERMTU
) {
2507 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2509 * This is an LLC SAP value, so the frames
2510 * that match would be 802.2 frames.
2511 * Check that the frame is an 802.2 frame
2512 * (i.e., that the length/type field is
2513 * a length field, <= ETHERMTU) and
2514 * then check the DSAP.
2516 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2517 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2521 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2523 * This is an Ethernet type, so compare
2524 * the length/type field with it (if
2525 * the frame is an 802.2 frame, the length
2526 * field will be <= ETHERMTU, and, as
2527 * "ll_proto" is > ETHERMTU, this test
2528 * will fail and the frame won't match,
2529 * which is what we want).
2531 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2536 static struct block
*
2537 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2540 * For DLT_NULL, the link-layer header is a 32-bit word
2541 * containing an AF_ value in *host* byte order, and for
2542 * DLT_ENC, the link-layer header begins with a 32-bit
2543 * word containing an AF_ value in host byte order.
2545 * In addition, if we're reading a saved capture file,
2546 * the host byte order in the capture may not be the
2547 * same as the host byte order on this machine.
2549 * For DLT_LOOP, the link-layer header is a 32-bit
2550 * word containing an AF_ value in *network* byte order.
2552 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2554 * The AF_ value is in host byte order, but the BPF
2555 * interpreter will convert it to network byte order.
2557 * If this is a save file, and it's from a machine
2558 * with the opposite byte order to ours, we byte-swap
2561 * Then we run it through "htonl()", and generate
2562 * code to compare against the result.
2564 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2565 ll_proto
= SWAPLONG(ll_proto
);
2566 ll_proto
= htonl(ll_proto
);
2568 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2572 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2573 * or IPv6 then we have an error.
2575 static struct block
*
2576 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2581 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2584 case ETHERTYPE_IPV6
:
2585 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2592 return gen_false(cstate
);
2596 * Generate code to match a particular packet type.
2598 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2599 * value, if <= ETHERMTU. We use that to determine whether to
2600 * match the type field or to check the type field for the special
2601 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2603 static struct block
*
2604 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2606 struct block
*b0
, *b1
;
2612 case LLCSAP_NETBEUI
:
2614 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2615 * so we check the DSAP and SSAP.
2617 * LLCSAP_IP checks for IP-over-802.2, rather
2618 * than IP-over-Ethernet or IP-over-SNAP.
2620 * XXX - should we check both the DSAP and the
2621 * SSAP, like this, or should we check just the
2622 * DSAP, as we do for other types <= ETHERMTU
2623 * (i.e., other SAP values)?
2625 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2626 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2632 * Ethernet_II frames, which are Ethernet
2633 * frames with a frame type of ETHERTYPE_IPX;
2635 * Ethernet_802.3 frames, which have a frame
2636 * type of LINUX_SLL_P_802_3;
2638 * Ethernet_802.2 frames, which are 802.3
2639 * frames with an 802.2 LLC header (i.e, have
2640 * a frame type of LINUX_SLL_P_802_2) and
2641 * with the IPX LSAP as the DSAP in the LLC
2644 * Ethernet_SNAP frames, which are 802.3
2645 * frames with an LLC header and a SNAP
2646 * header and with an OUI of 0x000000
2647 * (encapsulated Ethernet) and a protocol
2648 * ID of ETHERTYPE_IPX in the SNAP header.
2650 * First, do the checks on LINUX_SLL_P_802_2
2651 * frames; generate the check for either
2652 * Ethernet_802.2 or Ethernet_SNAP frames, and
2653 * then put a check for LINUX_SLL_P_802_2 frames
2656 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2657 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2659 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2663 * Now check for 802.3 frames and OR that with
2664 * the previous test.
2666 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2670 * Now add the check for Ethernet_II frames, and
2671 * do that before checking for the other frame
2674 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2678 case ETHERTYPE_ATALK
:
2679 case ETHERTYPE_AARP
:
2681 * EtherTalk (AppleTalk protocols on Ethernet link
2682 * layer) may use 802.2 encapsulation.
2686 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2687 * we check for the 802.2 protocol type in the
2688 * "Ethernet type" field.
2690 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2693 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2694 * SNAP packets with an organization code of
2695 * 0x080007 (Apple, for Appletalk) and a protocol
2696 * type of ETHERTYPE_ATALK (Appletalk).
2698 * 802.2-encapsulated ETHERTYPE_AARP packets are
2699 * SNAP packets with an organization code of
2700 * 0x000000 (encapsulated Ethernet) and a protocol
2701 * type of ETHERTYPE_AARP (Appletalk ARP).
2703 if (ll_proto
== ETHERTYPE_ATALK
)
2704 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2705 else /* ll_proto == ETHERTYPE_AARP */
2706 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2710 * Check for Ethernet encapsulation (Ethertalk
2711 * phase 1?); we just check for the Ethernet
2714 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2720 if (ll_proto
<= ETHERMTU
) {
2721 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2723 * This is an LLC SAP value, so the frames
2724 * that match would be 802.2 frames.
2725 * Check for the 802.2 protocol type
2726 * in the "Ethernet type" field, and
2727 * then check the DSAP.
2729 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2730 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2735 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2737 * This is an Ethernet type, so compare
2738 * the length/type field with it (if
2739 * the frame is an 802.2 frame, the length
2740 * field will be <= ETHERMTU, and, as
2741 * "ll_proto" is > ETHERMTU, this test
2742 * will fail and the frame won't match,
2743 * which is what we want).
2745 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2751 * Load a value relative to the beginning of the link-layer header after the
2754 static struct slist
*
2755 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2757 struct slist
*s1
, *s2
;
2760 * Generate code to load the length of the pflog header into
2761 * the register assigned to hold that length, if one has been
2762 * assigned. (If one hasn't been assigned, no code we've
2763 * generated uses that prefix, so we don't need to generate any
2766 if (cstate
->off_linkpl
.reg
!= -1) {
2768 * The length is in the first byte of the header.
2770 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2774 * Round it up to a multiple of 4.
2775 * Add 3, and clear the lower 2 bits.
2777 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2780 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2781 s2
->s
.k
= 0xfffffffc;
2785 * Now allocate a register to hold that value and store
2788 s2
= new_stmt(cstate
, BPF_ST
);
2789 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2793 * Now move it into the X register.
2795 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2803 static struct slist
*
2804 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2806 struct slist
*s1
, *s2
;
2807 struct slist
*sjeq_avs_cookie
;
2808 struct slist
*sjcommon
;
2811 * This code is not compatible with the optimizer, as
2812 * we are generating jmp instructions within a normal
2813 * slist of instructions
2815 cstate
->no_optimize
= 1;
2818 * Generate code to load the length of the radio header into
2819 * the register assigned to hold that length, if one has been
2820 * assigned. (If one hasn't been assigned, no code we've
2821 * generated uses that prefix, so we don't need to generate any
2824 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2825 * or always use the AVS header rather than the Prism header.
2826 * We load a 4-byte big-endian value at the beginning of the
2827 * raw packet data, and see whether, when masked with 0xFFFFF000,
2828 * it's equal to 0x80211000. If so, that indicates that it's
2829 * an AVS header (the masked-out bits are the version number).
2830 * Otherwise, it's a Prism header.
2832 * XXX - the Prism header is also, in theory, variable-length,
2833 * but no known software generates headers that aren't 144
2836 if (cstate
->off_linkhdr
.reg
!= -1) {
2840 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2844 * AND it with 0xFFFFF000.
2846 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2847 s2
->s
.k
= 0xFFFFF000;
2851 * Compare with 0x80211000.
2853 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2854 sjeq_avs_cookie
->s
.k
= 0x80211000;
2855 sappend(s1
, sjeq_avs_cookie
);
2860 * The 4 bytes at an offset of 4 from the beginning of
2861 * the AVS header are the length of the AVS header.
2862 * That field is big-endian.
2864 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2867 sjeq_avs_cookie
->s
.jt
= s2
;
2870 * Now jump to the code to allocate a register
2871 * into which to save the header length and
2872 * store the length there. (The "jump always"
2873 * instruction needs to have the k field set;
2874 * it's added to the PC, so, as we're jumping
2875 * over a single instruction, it should be 1.)
2877 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2879 sappend(s1
, sjcommon
);
2882 * Now for the code that handles the Prism header.
2883 * Just load the length of the Prism header (144)
2884 * into the A register. Have the test for an AVS
2885 * header branch here if we don't have an AVS header.
2887 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2890 sjeq_avs_cookie
->s
.jf
= s2
;
2893 * Now allocate a register to hold that value and store
2894 * it. The code for the AVS header will jump here after
2895 * loading the length of the AVS header.
2897 s2
= new_stmt(cstate
, BPF_ST
);
2898 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2900 sjcommon
->s
.jf
= s2
;
2903 * Now move it into the X register.
2905 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2913 static struct slist
*
2914 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2916 struct slist
*s1
, *s2
;
2919 * Generate code to load the length of the AVS header into
2920 * the register assigned to hold that length, if one has been
2921 * assigned. (If one hasn't been assigned, no code we've
2922 * generated uses that prefix, so we don't need to generate any
2925 if (cstate
->off_linkhdr
.reg
!= -1) {
2927 * The 4 bytes at an offset of 4 from the beginning of
2928 * the AVS header are the length of the AVS header.
2929 * That field is big-endian.
2931 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2935 * Now allocate a register to hold that value and store
2938 s2
= new_stmt(cstate
, BPF_ST
);
2939 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2943 * Now move it into the X register.
2945 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2953 static struct slist
*
2954 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2956 struct slist
*s1
, *s2
;
2959 * Generate code to load the length of the radiotap header into
2960 * the register assigned to hold that length, if one has been
2961 * assigned. (If one hasn't been assigned, no code we've
2962 * generated uses that prefix, so we don't need to generate any
2965 if (cstate
->off_linkhdr
.reg
!= -1) {
2967 * The 2 bytes at offsets of 2 and 3 from the beginning
2968 * of the radiotap header are the length of the radiotap
2969 * header; unfortunately, it's little-endian, so we have
2970 * to load it a byte at a time and construct the value.
2974 * Load the high-order byte, at an offset of 3, shift it
2975 * left a byte, and put the result in the X register.
2977 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2979 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2982 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2986 * Load the next byte, at an offset of 2, and OR the
2987 * value from the X register into it.
2989 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2992 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2996 * Now allocate a register to hold that value and store
2999 s2
= new_stmt(cstate
, BPF_ST
);
3000 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3004 * Now move it into the X register.
3006 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3015 * At the moment we treat PPI as normal Radiotap encoded
3016 * packets. The difference is in the function that generates
3017 * the code at the beginning to compute the header length.
3018 * Since this code generator of PPI supports bare 802.11
3019 * encapsulation only (i.e. the encapsulated DLT should be
3020 * DLT_IEEE802_11) we generate code to check for this too;
3021 * that's done in finish_parse().
3023 static struct slist
*
3024 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
3026 struct slist
*s1
, *s2
;
3029 * Generate code to load the length of the radiotap header
3030 * into the register assigned to hold that length, if one has
3033 if (cstate
->off_linkhdr
.reg
!= -1) {
3035 * The 2 bytes at offsets of 2 and 3 from the beginning
3036 * of the radiotap header are the length of the radiotap
3037 * header; unfortunately, it's little-endian, so we have
3038 * to load it a byte at a time and construct the value.
3042 * Load the high-order byte, at an offset of 3, shift it
3043 * left a byte, and put the result in the X register.
3045 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3047 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
3050 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3054 * Load the next byte, at an offset of 2, and OR the
3055 * value from the X register into it.
3057 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3060 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3064 * Now allocate a register to hold that value and store
3067 s2
= new_stmt(cstate
, BPF_ST
);
3068 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3072 * Now move it into the X register.
3074 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3083 * Load a value relative to the beginning of the link-layer header after the 802.11
3084 * header, i.e. LLC_SNAP.
3085 * The link-layer header doesn't necessarily begin at the beginning
3086 * of the packet data; there might be a variable-length prefix containing
3087 * radio information.
3089 static struct slist
*
3090 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
3093 struct slist
*sjset_data_frame_1
;
3094 struct slist
*sjset_data_frame_2
;
3095 struct slist
*sjset_qos
;
3096 struct slist
*sjset_radiotap_flags_present
;
3097 struct slist
*sjset_radiotap_ext_present
;
3098 struct slist
*sjset_radiotap_tsft_present
;
3099 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
3100 struct slist
*s_roundup
;
3102 if (cstate
->off_linkpl
.reg
== -1) {
3104 * No register has been assigned to the offset of
3105 * the link-layer payload, which means nobody needs
3106 * it; don't bother computing it - just return
3107 * what we already have.
3113 * This code is not compatible with the optimizer, as
3114 * we are generating jmp instructions within a normal
3115 * slist of instructions
3117 cstate
->no_optimize
= 1;
3120 * If "s" is non-null, it has code to arrange that the X register
3121 * contains the length of the prefix preceding the link-layer
3124 * Otherwise, the length of the prefix preceding the link-layer
3125 * header is "off_outermostlinkhdr.constant_part".
3129 * There is no variable-length header preceding the
3130 * link-layer header.
3132 * Load the length of the fixed-length prefix preceding
3133 * the link-layer header (if any) into the X register,
3134 * and store it in the cstate->off_linkpl.reg register.
3135 * That length is off_outermostlinkhdr.constant_part.
3137 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3138 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3142 * The X register contains the offset of the beginning of the
3143 * link-layer header; add 24, which is the minimum length
3144 * of the MAC header for a data frame, to that, and store it
3145 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3146 * which is at the offset in the X register, with an indexed load.
3148 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3150 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3153 s2
= new_stmt(cstate
, BPF_ST
);
3154 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3157 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3162 * Check the Frame Control field to see if this is a data frame;
3163 * a data frame has the 0x08 bit (b3) in that field set and the
3164 * 0x04 bit (b2) clear.
3166 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3167 sjset_data_frame_1
->s
.k
= IEEE80211_FC0_TYPE_DATA
;
3168 sappend(s
, sjset_data_frame_1
);
3171 * If b3 is set, test b2, otherwise go to the first statement of
3172 * the rest of the program.
3174 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3175 sjset_data_frame_2
->s
.k
= IEEE80211_FC0_TYPE_CTL
;
3176 sappend(s
, sjset_data_frame_2
);
3177 sjset_data_frame_1
->s
.jf
= snext
;
3180 * If b2 is not set, this is a data frame; test the QoS bit.
3181 * Otherwise, go to the first statement of the rest of the
3184 sjset_data_frame_2
->s
.jt
= snext
;
3185 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3186 sjset_qos
->s
.k
= IEEE80211_FC0_SUBTYPE_QOS
;
3187 sappend(s
, sjset_qos
);
3190 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3192 * Otherwise, go to the first statement of the rest of the
3195 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3196 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3198 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3201 s2
= new_stmt(cstate
, BPF_ST
);
3202 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3206 * If we have a radiotap header, look at it to see whether
3207 * there's Atheros padding between the MAC-layer header
3210 * Note: all of the fields in the radiotap header are
3211 * little-endian, so we byte-swap all of the values
3212 * we test against, as they will be loaded as big-endian
3215 * XXX - in the general case, we would have to scan through
3216 * *all* the presence bits, if there's more than one word of
3217 * presence bits. That would require a loop, meaning that
3218 * we wouldn't be able to run the filter in the kernel.
3220 * We assume here that the Atheros adapters that insert the
3221 * annoying padding don't have multiple antennae and therefore
3222 * do not generate radiotap headers with multiple presence words.
3224 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3226 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3227 * in the first presence flag word?
3229 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3233 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3234 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3235 sappend(s
, sjset_radiotap_flags_present
);
3238 * If not, skip all of this.
3240 sjset_radiotap_flags_present
->s
.jf
= snext
;
3243 * Otherwise, is the "extension" bit set in that word?
3245 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3246 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3247 sappend(s
, sjset_radiotap_ext_present
);
3248 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3251 * If so, skip all of this.
3253 sjset_radiotap_ext_present
->s
.jt
= snext
;
3256 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3258 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3259 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3260 sappend(s
, sjset_radiotap_tsft_present
);
3261 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3264 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3265 * at an offset of 16 from the beginning of the raw packet
3266 * data (8 bytes for the radiotap header and 8 bytes for
3269 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3272 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3275 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3277 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3278 sjset_tsft_datapad
->s
.k
= 0x20;
3279 sappend(s
, sjset_tsft_datapad
);
3282 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3283 * at an offset of 8 from the beginning of the raw packet
3284 * data (8 bytes for the radiotap header).
3286 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3289 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3292 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3294 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3295 sjset_notsft_datapad
->s
.k
= 0x20;
3296 sappend(s
, sjset_notsft_datapad
);
3299 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3300 * set, round the length of the 802.11 header to
3301 * a multiple of 4. Do that by adding 3 and then
3302 * dividing by and multiplying by 4, which we do by
3305 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3306 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3307 sappend(s
, s_roundup
);
3308 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3311 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3312 s2
->s
.k
= (bpf_u_int32
)~3;
3314 s2
= new_stmt(cstate
, BPF_ST
);
3315 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3318 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3319 sjset_tsft_datapad
->s
.jf
= snext
;
3320 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3321 sjset_notsft_datapad
->s
.jf
= snext
;
3323 sjset_qos
->s
.jf
= snext
;
3329 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3333 /* There is an implicit dependency between the link
3334 * payload and link header since the payload computation
3335 * includes the variable part of the header. Therefore,
3336 * if nobody else has allocated a register for the link
3337 * header and we need it, do it now. */
3338 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3339 cstate
->off_linkhdr
.reg
== -1)
3340 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3343 * For link-layer types that have a variable-length header
3344 * preceding the link-layer header, generate code to load
3345 * the offset of the link-layer header into the register
3346 * assigned to that offset, if any.
3348 * XXX - this, and the next switch statement, won't handle
3349 * encapsulation of 802.11 or 802.11+radio information in
3350 * some other protocol stack. That's significantly more
3353 switch (cstate
->outermostlinktype
) {
3355 case DLT_PRISM_HEADER
:
3356 s
= gen_load_prism_llprefixlen(cstate
);
3359 case DLT_IEEE802_11_RADIO_AVS
:
3360 s
= gen_load_avs_llprefixlen(cstate
);
3363 case DLT_IEEE802_11_RADIO
:
3364 s
= gen_load_radiotap_llprefixlen(cstate
);
3368 s
= gen_load_ppi_llprefixlen(cstate
);
3377 * For link-layer types that have a variable-length link-layer
3378 * header, generate code to load the offset of the link-layer
3379 * payload into the register assigned to that offset, if any.
3381 switch (cstate
->outermostlinktype
) {
3383 case DLT_IEEE802_11
:
3384 case DLT_PRISM_HEADER
:
3385 case DLT_IEEE802_11_RADIO_AVS
:
3386 case DLT_IEEE802_11_RADIO
:
3388 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3392 s
= gen_load_pflog_llprefixlen(cstate
);
3397 * If there is no initialization yet and we need variable
3398 * length offsets for VLAN, initialize them to zero
3400 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3403 if (cstate
->off_linkpl
.reg
== -1)
3404 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3405 if (cstate
->off_linktype
.reg
== -1)
3406 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3408 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3410 s2
= new_stmt(cstate
, BPF_ST
);
3411 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3413 s2
= new_stmt(cstate
, BPF_ST
);
3414 s2
->s
.k
= cstate
->off_linktype
.reg
;
3419 * If we have any offset-loading code, append all the
3420 * existing statements in the block to those statements,
3421 * and make the resulting list the list of statements
3425 sappend(s
, b
->stmts
);
3431 * Take an absolute offset, and:
3433 * if it has no variable part, return NULL;
3435 * if it has a variable part, generate code to load the register
3436 * containing that variable part into the X register, returning
3437 * a pointer to that code - if no register for that offset has
3438 * been allocated, allocate it first.
3440 * (The code to set that register will be generated later, but will
3441 * be placed earlier in the code sequence.)
3443 static struct slist
*
3444 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3448 if (off
->is_variable
) {
3449 if (off
->reg
== -1) {
3451 * We haven't yet assigned a register for the
3452 * variable part of the offset of the link-layer
3453 * header; allocate one.
3455 off
->reg
= alloc_reg(cstate
);
3459 * Load the register containing the variable part of the
3460 * offset of the link-layer header into the X register.
3462 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3467 * That offset isn't variable, there's no variable part,
3468 * so we don't need to generate any code.
3475 * Map an Ethernet type to the equivalent PPP type.
3478 ethertype_to_ppptype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3485 case ETHERTYPE_IPV6
:
3491 case ETHERTYPE_ATALK
:
3502 * I'm assuming the "Bridging PDU"s that go
3503 * over PPP are Spanning Tree Protocol
3511 assert_maxval(cstate
, "PPP protocol", ll_proto
, UINT16_MAX
);
3512 return (uint16_t)ll_proto
;
3516 * Generate any tests that, for encapsulation of a link-layer packet
3517 * inside another protocol stack, need to be done to check for those
3518 * link-layer packets (and that haven't already been done by a check
3519 * for that encapsulation).
3521 static struct block
*
3522 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3524 if (cstate
->is_encap
)
3525 return gen_encap_ll_check(cstate
);
3527 switch (cstate
->prevlinktype
) {
3531 * This is LANE-encapsulated Ethernet; check that the LANE
3532 * packet doesn't begin with an LE Control marker, i.e.
3533 * that it's data, not a control message.
3535 * (We've already generated a test for LANE.)
3537 return gen_cmp_ne(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3541 * No such tests are necessary.
3549 * The three different values we should check for when checking for an
3550 * IPv6 packet with DLT_NULL.
3552 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3553 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3554 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3557 * Generate code to match a particular packet type by matching the
3558 * link-layer type field or fields in the 802.2 LLC header.
3560 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3561 * value, if <= ETHERMTU.
3563 static struct block
*
3564 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3566 struct block
*b0
, *b1
, *b2
;
3568 /* are we checking MPLS-encapsulated packets? */
3569 if (cstate
->label_stack_depth
> 0)
3570 return gen_mpls_linktype(cstate
, ll_proto
);
3572 switch (cstate
->linktype
) {
3575 case DLT_NETANALYZER
:
3576 case DLT_NETANALYZER_TRANSPARENT
:
3577 /* Geneve has an EtherType regardless of whether there is an
3578 * L2 header. VXLAN always has an EtherType. */
3579 if (!cstate
->is_encap
)
3580 b0
= gen_prevlinkhdr_check(cstate
);
3584 b1
= gen_ether_linktype(cstate
, ll_proto
);
3592 assert_maxval(cstate
, "HDLC protocol", ll_proto
, UINT16_MAX
);
3596 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3600 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3604 case DLT_IEEE802_11
:
3605 case DLT_PRISM_HEADER
:
3606 case DLT_IEEE802_11_RADIO_AVS
:
3607 case DLT_IEEE802_11_RADIO
:
3610 * Check that we have a data frame.
3612 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3613 IEEE80211_FC0_TYPE_DATA
,
3614 IEEE80211_FC0_TYPE_MASK
);
3617 * Now check for the specified link-layer type.
3619 b1
= gen_llc_linktype(cstate
, ll_proto
);
3626 * XXX - check for LLC frames.
3628 return gen_llc_linktype(cstate
, ll_proto
);
3633 * XXX - check for LLC PDUs, as per IEEE 802.5.
3635 return gen_llc_linktype(cstate
, ll_proto
);
3638 case DLT_ATM_RFC1483
:
3640 case DLT_IP_OVER_FC
:
3641 return gen_llc_linktype(cstate
, ll_proto
);
3646 * Check for an LLC-encapsulated version of this protocol;
3647 * if we were checking for LANE, linktype would no longer
3650 * Check for LLC encapsulation and then check the protocol.
3652 b0
= gen_atm_prototype(cstate
, PT_LLC
);
3653 b1
= gen_llc_linktype(cstate
, ll_proto
);
3659 return gen_linux_sll_linktype(cstate
, ll_proto
);
3663 case DLT_SLIP_BSDOS
:
3666 * These types don't provide any type field; packets
3667 * are always IPv4 or IPv6.
3669 * XXX - for IPv4, check for a version number of 4, and,
3670 * for IPv6, check for a version number of 6?
3675 /* Check for a version number of 4. */
3676 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3678 case ETHERTYPE_IPV6
:
3679 /* Check for a version number of 6. */
3680 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3683 return gen_false(cstate
); /* always false */
3689 * Raw IPv4, so no type field.
3691 if (ll_proto
== ETHERTYPE_IP
)
3692 return gen_true(cstate
); /* always true */
3694 /* Checking for something other than IPv4; always false */
3695 return gen_false(cstate
);
3700 * Raw IPv6, so no type field.
3702 if (ll_proto
== ETHERTYPE_IPV6
)
3703 return gen_true(cstate
); /* always true */
3705 /* Checking for something other than IPv6; always false */
3706 return gen_false(cstate
);
3711 case DLT_PPP_SERIAL
:
3714 * We use Ethernet protocol types inside libpcap;
3715 * map them to the corresponding PPP protocol types.
3717 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3718 ethertype_to_ppptype(cstate
, ll_proto
));
3723 * We use Ethernet protocol types inside libpcap;
3724 * map them to the corresponding PPP protocol types.
3730 * Also check for Van Jacobson-compressed IP.
3731 * XXX - do this for other forms of PPP?
3733 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3734 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3736 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3741 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3742 ethertype_to_ppptype(cstate
, ll_proto
));
3752 return (gen_loopback_linktype(cstate
, AF_INET
));
3754 case ETHERTYPE_IPV6
:
3756 * AF_ values may, unfortunately, be platform-
3757 * dependent; AF_INET isn't, because everybody
3758 * used 4.2BSD's value, but AF_INET6 is, because
3759 * 4.2BSD didn't have a value for it (given that
3760 * IPv6 didn't exist back in the early 1980's),
3761 * and they all picked their own values.
3763 * This means that, if we're reading from a
3764 * savefile, we need to check for all the
3767 * If we're doing a live capture, we only need
3768 * to check for this platform's value; however,
3769 * Npcap uses 24, which isn't Windows's AF_INET6
3770 * value. (Given the multiple different values,
3771 * programs that read pcap files shouldn't be
3772 * checking for their platform's AF_INET6 value
3773 * anyway, they should check for all of the
3774 * possible values. and they might as well do
3775 * that even for live captures.)
3777 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3779 * Savefile - check for all three
3780 * possible IPv6 values.
3782 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3783 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3785 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3790 * Live capture, so we only need to
3791 * check for the value used on this
3796 * Npcap doesn't use Windows's AF_INET6,
3797 * as that collides with AF_IPX on
3798 * some BSDs (both have the value 23).
3799 * Instead, it uses 24.
3801 return (gen_loopback_linktype(cstate
, 24));
3804 return (gen_loopback_linktype(cstate
, AF_INET6
));
3805 #else /* AF_INET6 */
3807 * I guess this platform doesn't support
3808 * IPv6, so we just reject all packets.
3810 return gen_false(cstate
);
3811 #endif /* AF_INET6 */
3817 * Not a type on which we support filtering.
3818 * XXX - support those that have AF_ values
3819 * #defined on this platform, at least?
3821 return gen_false(cstate
);
3826 * af field is host byte order in contrast to the rest of
3829 if (ll_proto
== ETHERTYPE_IP
)
3830 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3832 else if (ll_proto
== ETHERTYPE_IPV6
)
3833 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3836 return gen_false(cstate
);
3840 case DLT_ARCNET_LINUX
:
3842 * XXX should we check for first fragment if the protocol
3848 return gen_false(cstate
);
3850 case ETHERTYPE_IPV6
:
3851 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3855 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3857 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3863 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3865 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3870 case ETHERTYPE_REVARP
:
3871 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3874 case ETHERTYPE_ATALK
:
3875 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3882 case ETHERTYPE_ATALK
:
3883 return gen_true(cstate
);
3885 return gen_false(cstate
);
3891 * XXX - assumes a 2-byte Frame Relay header with
3892 * DLCI and flags. What if the address is longer?
3898 * Check for the special NLPID for IP.
3900 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3902 case ETHERTYPE_IPV6
:
3904 * Check for the special NLPID for IPv6.
3906 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3910 * Check for several OSI protocols.
3912 * Frame Relay packets typically have an OSI
3913 * NLPID at the beginning; we check for each
3916 * What we check for is the NLPID and a frame
3917 * control field of UI, i.e. 0x03 followed
3920 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3921 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3922 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3928 return gen_false(cstate
);
3933 break; // not implemented
3935 case DLT_JUNIPER_MFR
:
3936 case DLT_JUNIPER_MLFR
:
3937 case DLT_JUNIPER_MLPPP
:
3938 case DLT_JUNIPER_ATM1
:
3939 case DLT_JUNIPER_ATM2
:
3940 case DLT_JUNIPER_PPPOE
:
3941 case DLT_JUNIPER_PPPOE_ATM
:
3942 case DLT_JUNIPER_GGSN
:
3943 case DLT_JUNIPER_ES
:
3944 case DLT_JUNIPER_MONITOR
:
3945 case DLT_JUNIPER_SERVICES
:
3946 case DLT_JUNIPER_ETHER
:
3947 case DLT_JUNIPER_PPP
:
3948 case DLT_JUNIPER_FRELAY
:
3949 case DLT_JUNIPER_CHDLC
:
3950 case DLT_JUNIPER_VP
:
3951 case DLT_JUNIPER_ST
:
3952 case DLT_JUNIPER_ISM
:
3953 case DLT_JUNIPER_VS
:
3954 case DLT_JUNIPER_SRX_E2E
:
3955 case DLT_JUNIPER_FIBRECHANNEL
:
3956 case DLT_JUNIPER_ATM_CEMIC
:
3958 /* just lets verify the magic number for now -
3959 * on ATM we may have up to 6 different encapsulations on the wire
3960 * and need a lot of heuristics to figure out that the payload
3963 * FIXME encapsulation specific BPF_ filters
3965 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3967 case DLT_BACNET_MS_TP
:
3968 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3971 return gen_ipnet_linktype(cstate
, ll_proto
);
3973 case DLT_LINUX_IRDA
:
3976 case DLT_MTP2_WITH_PHDR
:
3979 case DLT_LINUX_LAPD
:
3980 case DLT_USB_FREEBSD
:
3982 case DLT_USB_LINUX_MMAPPED
:
3984 case DLT_BLUETOOTH_HCI_H4
:
3985 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3987 case DLT_CAN_SOCKETCAN
:
3988 case DLT_IEEE802_15_4
:
3989 case DLT_IEEE802_15_4_LINUX
:
3990 case DLT_IEEE802_15_4_NONASK_PHY
:
3991 case DLT_IEEE802_15_4_NOFCS
:
3992 case DLT_IEEE802_15_4_TAP
:
3993 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3996 case DLT_IPMB_KONTRON
:
4000 /* Using the fixed-size NFLOG header it is possible to tell only
4001 * the address family of the packet, other meaningful data is
4002 * either missing or behind TLVs.
4004 break; // not implemented
4008 * Does this link-layer header type have a field
4009 * indicating the type of the next protocol? If
4010 * so, off_linktype.constant_part will be the offset of that
4011 * field in the packet; if not, it will be OFFSET_NOT_SET.
4013 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
4015 * Yes; assume it's an Ethernet type. (If
4016 * it's not, it needs to be handled specially
4019 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4020 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
4024 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
4025 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
4029 * Check for an LLC SNAP packet with a given organization code and
4030 * protocol type; we check the entire contents of the 802.2 LLC and
4031 * snap headers, checking for DSAP and SSAP of SNAP and a control
4032 * field of 0x03 in the LLC header, and for the specified organization
4033 * code and protocol type in the SNAP header.
4035 static struct block
*
4036 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
4038 u_char snapblock
[8];
4040 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
4041 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
4042 snapblock
[2] = 0x03; /* control = UI */
4043 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
4044 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
4045 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
4046 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
4047 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
4048 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
4052 * Generate code to match frames with an LLC header.
4054 static struct block
*
4055 gen_llc_internal(compiler_state_t
*cstate
)
4057 struct block
*b0
, *b1
;
4059 switch (cstate
->linktype
) {
4063 * We check for an Ethernet type field less or equal than
4064 * 1500, which means it's an 802.3 length field.
4066 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
4069 * Now check for the purported DSAP and SSAP not being
4070 * 0xFF, to rule out NetWare-over-802.3.
4072 b1
= gen_cmp_ne(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
4078 * We check for LLC traffic.
4080 return gen_atmtype_llc(cstate
);
4082 case DLT_IEEE802
: /* Token Ring */
4084 * XXX - check for LLC frames.
4086 return gen_true(cstate
);
4090 * XXX - check for LLC frames.
4092 return gen_true(cstate
);
4094 case DLT_ATM_RFC1483
:
4096 * For LLC encapsulation, these are defined to have an
4099 * For VC encapsulation, they don't, but there's no
4100 * way to check for that; the protocol used on the VC
4101 * is negotiated out of band.
4103 return gen_true(cstate
);
4105 case DLT_IEEE802_11
:
4106 case DLT_PRISM_HEADER
:
4107 case DLT_IEEE802_11_RADIO
:
4108 case DLT_IEEE802_11_RADIO_AVS
:
4111 * Check that we have a data frame.
4113 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4114 IEEE80211_FC0_TYPE_DATA
,
4115 IEEE80211_FC0_TYPE_MASK
);
4118 fail_kw_on_dlt(cstate
, "llc");
4124 gen_llc(compiler_state_t
*cstate
)
4127 * Catch errors reported by us and routines below us, and return NULL
4130 if (setjmp(cstate
->top_ctx
))
4133 return gen_llc_internal(cstate
);
4137 gen_llc_i(compiler_state_t
*cstate
)
4139 struct block
*b0
, *b1
;
4143 * Catch errors reported by us and routines below us, and return NULL
4146 if (setjmp(cstate
->top_ctx
))
4150 * Check whether this is an LLC frame.
4152 b0
= gen_llc_internal(cstate
);
4155 * Load the control byte and test the low-order bit; it must
4156 * be clear for I frames.
4158 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4159 b1
= gen_unset(cstate
, 0x01, s
);
4166 gen_llc_s(compiler_state_t
*cstate
)
4168 struct block
*b0
, *b1
;
4171 * Catch errors reported by us and routines below us, and return NULL
4174 if (setjmp(cstate
->top_ctx
))
4178 * Check whether this is an LLC frame.
4180 b0
= gen_llc_internal(cstate
);
4183 * Now compare the low-order 2 bit of the control byte against
4184 * the appropriate value for S frames.
4186 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4192 gen_llc_u(compiler_state_t
*cstate
)
4194 struct block
*b0
, *b1
;
4197 * Catch errors reported by us and routines below us, and return NULL
4200 if (setjmp(cstate
->top_ctx
))
4204 * Check whether this is an LLC frame.
4206 b0
= gen_llc_internal(cstate
);
4209 * Now compare the low-order 2 bit of the control byte against
4210 * the appropriate value for U frames.
4212 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4218 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4220 struct block
*b0
, *b1
;
4223 * Catch errors reported by us and routines below us, and return NULL
4226 if (setjmp(cstate
->top_ctx
))
4230 * Check whether this is an LLC frame.
4232 b0
= gen_llc_internal(cstate
);
4235 * Now check for an S frame with the appropriate type.
4237 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4243 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4245 struct block
*b0
, *b1
;
4248 * Catch errors reported by us and routines below us, and return NULL
4251 if (setjmp(cstate
->top_ctx
))
4255 * Check whether this is an LLC frame.
4257 b0
= gen_llc_internal(cstate
);
4260 * Now check for a U frame with the appropriate type.
4262 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4268 * Generate code to match a particular packet type, for link-layer types
4269 * using 802.2 LLC headers.
4271 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4272 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4274 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4275 * value, if <= ETHERMTU. We use that to determine whether to
4276 * match the DSAP or both DSAP and LSAP or to check the OUI and
4277 * protocol ID in a SNAP header.
4279 static struct block
*
4280 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4283 * XXX - handle token-ring variable-length header.
4289 case LLCSAP_NETBEUI
:
4291 * XXX - should we check both the DSAP and the
4292 * SSAP, like this, or should we check just the
4293 * DSAP, as we do for other SAP values?
4295 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4296 ((ll_proto
<< 8) | ll_proto
));
4300 * XXX - are there ever SNAP frames for IPX on
4301 * non-Ethernet 802.x networks?
4303 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4305 case ETHERTYPE_ATALK
:
4307 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4308 * SNAP packets with an organization code of
4309 * 0x080007 (Apple, for Appletalk) and a protocol
4310 * type of ETHERTYPE_ATALK (Appletalk).
4312 * XXX - check for an organization code of
4313 * encapsulated Ethernet as well?
4315 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4319 * XXX - we don't have to check for IPX 802.3
4320 * here, but should we check for the IPX Ethertype?
4322 if (ll_proto
<= ETHERMTU
) {
4323 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
4325 * This is an LLC SAP value, so check
4328 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4330 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4332 * This is an Ethernet type; we assume that it's
4333 * unlikely that it'll appear in the right place
4334 * at random, and therefore check only the
4335 * location that would hold the Ethernet type
4336 * in a SNAP frame with an organization code of
4337 * 0x000000 (encapsulated Ethernet).
4339 * XXX - if we were to check for the SNAP DSAP and
4340 * LSAP, as per XXX, and were also to check for an
4341 * organization code of 0x000000 (encapsulated
4342 * Ethernet), we'd do
4344 * return gen_snap(cstate, 0x000000, ll_proto);
4346 * here; for now, we don't, as per the above.
4347 * I don't know whether it's worth the extra CPU
4348 * time to do the right check or not.
4350 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4355 static struct block
*
4356 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4357 int dir
, u_int src_off
, u_int dst_off
)
4359 struct block
*b0
, *b1
;
4373 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4374 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4380 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4381 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4391 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4398 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4402 static struct block
*
4403 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4404 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4406 struct block
*b0
, *b1
;
4409 * Code below needs to access four separate 32-bit parts of the 128-bit
4410 * IPv6 address and mask. In some OSes this is as simple as using the
4411 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4412 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4413 * far as libpcap sees it. Hence copy the data before use to avoid
4414 * potential unaligned memory access and the associated compiler
4415 * warnings (whether genuine or not).
4417 bpf_u_int32 a
[4], m
[4];
4430 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4431 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4437 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4438 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4448 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4455 /* this order is important */
4456 memcpy(a
, addr
, sizeof(a
));
4457 memcpy(m
, mask
, sizeof(m
));
4458 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4459 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4461 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4463 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4469 static struct block
*
4470 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4472 register struct block
*b0
, *b1
;
4476 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4479 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4482 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4483 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4489 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4490 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4500 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4508 * Like gen_ehostop, but for DLT_FDDI
4510 static struct block
*
4511 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4513 struct block
*b0
, *b1
;
4517 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4520 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4523 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4524 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4530 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4531 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4541 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4549 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4551 static struct block
*
4552 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4554 register struct block
*b0
, *b1
;
4558 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4561 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4564 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4565 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4571 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4572 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4582 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4590 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4591 * various 802.11 + radio headers.
4593 static struct block
*
4594 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4596 register struct block
*b0
, *b1
, *b2
;
4597 register struct slist
*s
;
4599 #ifdef ENABLE_WLAN_FILTERING_PATCH
4602 * We need to disable the optimizer because the optimizer is buggy
4603 * and wipes out some LD instructions generated by the below
4604 * code to validate the Frame Control bits
4606 cstate
->no_optimize
= 1;
4607 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4614 * For control frames, there is no SA.
4616 * For management frames, SA is at an
4617 * offset of 10 from the beginning of
4620 * For data frames, SA is at an offset
4621 * of 10 from the beginning of the packet
4622 * if From DS is clear, at an offset of
4623 * 16 from the beginning of the packet
4624 * if From DS is set and To DS is clear,
4625 * and an offset of 24 from the beginning
4626 * of the packet if From DS is set and To DS
4631 * Generate the tests to be done for data frames
4634 * First, check for To DS set, i.e. check "link[1] & 0x01".
4636 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4637 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4640 * If To DS is set, the SA is at 24.
4642 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4646 * Now, check for To DS not set, i.e. check
4647 * "!(link[1] & 0x01)".
4649 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4650 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4653 * If To DS is not set, the SA is at 16.
4655 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4659 * Now OR together the last two checks. That gives
4660 * the complete set of checks for data frames with
4666 * Now check for From DS being set, and AND that with
4667 * the ORed-together checks.
4669 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4670 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4674 * Now check for data frames with From DS not set.
4676 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4677 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4680 * If From DS isn't set, the SA is at 10.
4682 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4686 * Now OR together the checks for data frames with
4687 * From DS not set and for data frames with From DS
4688 * set; that gives the checks done for data frames.
4693 * Now check for a data frame.
4694 * I.e, check "link[0] & 0x08".
4696 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4697 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4700 * AND that with the checks done for data frames.
4705 * If the high-order bit of the type value is 0, this
4706 * is a management frame.
4707 * I.e, check "!(link[0] & 0x08)".
4709 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4710 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4713 * For management frames, the SA is at 10.
4715 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4719 * OR that with the checks done for data frames.
4720 * That gives the checks done for management and
4726 * If the low-order bit of the type value is 1,
4727 * this is either a control frame or a frame
4728 * with a reserved type, and thus not a
4731 * I.e., check "!(link[0] & 0x04)".
4733 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4734 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4737 * AND that with the checks for data and management
4747 * For control frames, there is no DA.
4749 * For management frames, DA is at an
4750 * offset of 4 from the beginning of
4753 * For data frames, DA is at an offset
4754 * of 4 from the beginning of the packet
4755 * if To DS is clear and at an offset of
4756 * 16 from the beginning of the packet
4761 * Generate the tests to be done for data frames.
4763 * First, check for To DS set, i.e. "link[1] & 0x01".
4765 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4766 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4769 * If To DS is set, the DA is at 16.
4771 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4775 * Now, check for To DS not set, i.e. check
4776 * "!(link[1] & 0x01)".
4778 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4779 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4782 * If To DS is not set, the DA is at 4.
4784 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4788 * Now OR together the last two checks. That gives
4789 * the complete set of checks for data frames.
4794 * Now check for a data frame.
4795 * I.e, check "link[0] & 0x08".
4797 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4798 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4801 * AND that with the checks done for data frames.
4806 * If the high-order bit of the type value is 0, this
4807 * is a management frame.
4808 * I.e, check "!(link[0] & 0x08)".
4810 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4811 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4814 * For management frames, the DA is at 4.
4816 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4820 * OR that with the checks done for data frames.
4821 * That gives the checks done for management and
4827 * If the low-order bit of the type value is 1,
4828 * this is either a control frame or a frame
4829 * with a reserved type, and thus not a
4832 * I.e., check "!(link[0] & 0x04)".
4834 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4835 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4838 * AND that with the checks for data and management
4845 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4846 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4852 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4853 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4858 * XXX - add BSSID keyword?
4861 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4865 * Not present in CTS or ACK control frames.
4867 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4868 IEEE80211_FC0_TYPE_MASK
);
4869 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4870 IEEE80211_FC0_SUBTYPE_MASK
);
4871 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4872 IEEE80211_FC0_SUBTYPE_MASK
);
4875 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4881 * Not present in control frames.
4883 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4884 IEEE80211_FC0_TYPE_MASK
);
4885 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4891 * Present only if the direction mask has both "From DS"
4892 * and "To DS" set. Neither control frames nor management
4893 * frames should have both of those set, so we don't
4894 * check the frame type.
4896 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4897 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4898 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4904 * Not present in management frames; addr1 in other
4909 * If the high-order bit of the type value is 0, this
4910 * is a management frame.
4911 * I.e, check "(link[0] & 0x08)".
4913 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4914 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4919 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4922 * AND that with the check of addr1.
4929 * Not present in management frames; addr2, if present,
4934 * Not present in CTS or ACK control frames.
4936 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4937 IEEE80211_FC0_TYPE_MASK
);
4938 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4939 IEEE80211_FC0_SUBTYPE_MASK
);
4940 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4941 IEEE80211_FC0_SUBTYPE_MASK
);
4946 * If the high-order bit of the type value is 0, this
4947 * is a management frame.
4948 * I.e, check "(link[0] & 0x08)".
4950 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4951 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4954 * AND that with the check for frames other than
4955 * CTS and ACK frames.
4962 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4971 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4972 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4973 * as the RFC states.)
4975 static struct block
*
4976 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4978 register struct block
*b0
, *b1
;
4982 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4985 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4988 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4989 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4995 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4996 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
5006 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5014 * This is quite tricky because there may be pad bytes in front of the
5015 * DECNET header, and then there are two possible data packet formats that
5016 * carry both src and dst addresses, plus 5 packet types in a format that
5017 * carries only the src node, plus 2 types that use a different format and
5018 * also carry just the src node.
5022 * Instead of doing those all right, we just look for data packets with
5023 * 0 or 1 bytes of padding. If you want to look at other packets, that
5024 * will require a lot more hacking.
5026 * To add support for filtering on DECNET "areas" (network numbers)
5027 * one would want to add a "mask" argument to this routine. That would
5028 * make the filter even more inefficient, although one could be clever
5029 * and not generate masking instructions if the mask is 0xFFFF.
5031 static struct block
*
5032 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
5034 struct block
*b0
, *b1
, *b2
, *tmp
;
5035 u_int offset_lh
; /* offset if long header is received */
5036 u_int offset_sh
; /* offset if short header is received */
5041 offset_sh
= 1; /* follows flags */
5042 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5046 offset_sh
= 3; /* follows flags, dstnode */
5047 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5051 /* Inefficient because we do our Calvinball dance twice */
5052 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5053 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5059 /* Inefficient because we do our Calvinball dance twice */
5060 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5061 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5071 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5079 * In a DECnet message inside an Ethernet frame the first two bytes
5080 * immediately after EtherType are the [litle-endian] DECnet message
5081 * length, which is irrelevant in this context.
5083 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5084 * 8-bit bitmap of the optional padding before the packet route header.
5085 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5086 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5087 * means there aren't any PAD bytes after the bitmap, so the header
5088 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5089 * is set to 0, thus the header begins at the third byte.
5091 * The header can be in several (as mentioned above) formats, all of
5092 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5093 * (PF, "pad field") set to 0 regardless of any padding present before
5094 * the header. "Short header" means bits 0-2 of the bitmap encode the
5095 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5097 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5098 * values and the masks, this maps to the required single bytes of
5099 * the message correctly on both big-endian and little-endian hosts.
5100 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5101 * because the wire encoding is little-endian and BPF multiple-byte
5102 * loads are big-endian. When the destination address is near enough
5103 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5106 /* Check for pad = 1, long header case */
5107 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5108 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5109 BPF_H
, SWAPSHORT(addr
));
5111 /* Check for pad = 0, long header case */
5112 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5113 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5117 /* Check for pad = 1, short header case */
5119 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5120 0x81020000U
| SWAPSHORT(addr
),
5123 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5124 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5129 /* Check for pad = 0, short header case */
5131 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5132 0x02000000U
| SWAPSHORT(addr
) << 8,
5135 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5136 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5146 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5147 * test the bottom-of-stack bit, and then check the version number
5148 * field in the IP header.
5150 static struct block
*
5151 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5153 struct block
*b0
, *b1
;
5158 /* match the bottom-of-stack bit */
5159 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5160 /* match the IPv4 version number */
5161 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5165 case ETHERTYPE_IPV6
:
5166 /* match the bottom-of-stack bit */
5167 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5168 /* match the IPv6 version number */
5169 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5174 /* FIXME add other L3 proto IDs */
5175 bpf_error(cstate
, "unsupported protocol over mpls");
5180 static struct block
*
5181 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5182 int proto
, int dir
, int type
)
5184 struct block
*b0
, *b1
;
5189 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5191 * Only check for non-IPv4 addresses if we're not
5192 * checking MPLS-encapsulated packets.
5194 if (cstate
->label_stack_depth
== 0) {
5195 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5197 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5203 // "link net NETNAME" and variations thereof
5204 break; // invalid qualifier
5207 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5208 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5213 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5214 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5219 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5220 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5231 break; // invalid qualifier
5234 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5235 b1
= gen_dnhostop(cstate
, addr
, dir
);
5266 break; // invalid qualifier
5271 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5272 type
== Q_NET
? "ip net" : "ip host");
5277 static struct block
*
5278 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5279 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5281 struct block
*b0
, *b1
;
5287 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5288 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5330 break; // invalid qualifier
5335 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5336 type
== Q_NET
? "ip6 net" : "ip6 host");
5343 * This primitive is non-directional by design, so the grammar does not allow
5344 * to qualify it with a direction.
5346 static struct block
*
5347 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5348 struct addrinfo
*alist
, int proto
)
5350 struct block
*b0
, *b1
, *tmp
;
5351 struct addrinfo
*ai
;
5352 struct sockaddr_in
*sin
;
5359 switch (cstate
->linktype
) {
5361 case DLT_NETANALYZER
:
5362 case DLT_NETANALYZER_TRANSPARENT
:
5363 b1
= gen_prevlinkhdr_check(cstate
);
5364 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5369 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5372 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5374 case DLT_IEEE802_11
:
5375 case DLT_PRISM_HEADER
:
5376 case DLT_IEEE802_11_RADIO_AVS
:
5377 case DLT_IEEE802_11_RADIO
:
5379 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5381 case DLT_IP_OVER_FC
:
5382 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5386 * This is LLC-multiplexed traffic; if it were
5387 * LANE, cstate->linktype would have been set to
5393 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5396 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5398 * Does it have an address?
5400 if (ai
->ai_addr
!= NULL
) {
5402 * Yes. Is it an IPv4 address?
5404 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5406 * Generate an entry for it.
5408 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5409 tmp
= gen_host(cstate
,
5410 ntohl(sin
->sin_addr
.s_addr
),
5411 0xffffffff, proto
, Q_OR
, Q_HOST
);
5413 * Is it the *first* IPv4 address?
5417 * Yes, so start with it.
5422 * No, so OR it into the
5434 * No IPv4 addresses found.
5442 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5447 static struct block
*
5448 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5456 return gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
);
5459 return gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
);
5462 return gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
);
5465 return gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
);
5467 #ifndef IPPROTO_IGMP
5468 #define IPPROTO_IGMP 2
5472 return gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
);
5474 #ifndef IPPROTO_IGRP
5475 #define IPPROTO_IGRP 9
5478 return gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
);
5481 #define IPPROTO_PIM 103
5485 return gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
);
5487 #ifndef IPPROTO_VRRP
5488 #define IPPROTO_VRRP 112
5492 return gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
);
5494 #ifndef IPPROTO_CARP
5495 #define IPPROTO_CARP 112
5499 return gen_proto(cstate
, IPPROTO_CARP
, Q_IP
);
5502 return gen_linktype(cstate
, ETHERTYPE_IP
);
5505 return gen_linktype(cstate
, ETHERTYPE_ARP
);
5508 return gen_linktype(cstate
, ETHERTYPE_REVARP
);
5511 break; // invalid syntax
5514 return gen_linktype(cstate
, ETHERTYPE_ATALK
);
5517 return gen_linktype(cstate
, ETHERTYPE_AARP
);
5520 return gen_linktype(cstate
, ETHERTYPE_DN
);
5523 return gen_linktype(cstate
, ETHERTYPE_SCA
);
5526 return gen_linktype(cstate
, ETHERTYPE_LAT
);
5529 return gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5532 return gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5535 return gen_linktype(cstate
, ETHERTYPE_IPV6
);
5537 #ifndef IPPROTO_ICMPV6
5538 #define IPPROTO_ICMPV6 58
5541 return gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
);
5544 #define IPPROTO_AH 51
5547 return gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
);
5550 #define IPPROTO_ESP 50
5553 return gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
);
5556 return gen_linktype(cstate
, LLCSAP_ISONS
);
5559 return gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
);
5562 return gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
5564 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5565 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5566 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5568 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5570 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5572 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5576 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5577 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5578 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5580 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5582 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5584 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5588 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5589 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5590 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5592 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
);
5597 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5598 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5603 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5604 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5606 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5608 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5613 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5614 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5619 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5620 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5625 return gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
);
5628 return gen_linktype(cstate
, LLCSAP_8021D
);
5631 return gen_linktype(cstate
, LLCSAP_IPX
);
5634 return gen_linktype(cstate
, LLCSAP_NETBEUI
);
5637 break; // invalid syntax
5642 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5646 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5649 * Catch errors reported by us and routines below us, and return NULL
5652 if (setjmp(cstate
->top_ctx
))
5655 return gen_proto_abbrev_internal(cstate
, proto
);
5658 static struct block
*
5659 gen_ip_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5661 return gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5664 static struct block
*
5665 gen_ip6_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5667 return gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5670 static struct block
*
5671 gen_ipfrag(compiler_state_t
*cstate
)
5675 /* not IPv4 frag other than the first frag */
5676 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5677 return gen_unset(cstate
, 0x1fff, s
);
5681 * Generate a comparison to a port value in the transport-layer header
5682 * at the specified offset from the beginning of that header.
5684 * XXX - this handles a variable-length prefix preceding the link-layer
5685 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5686 * variable-length link-layer headers (such as Token Ring or 802.11
5689 static struct block
*
5690 gen_portatom(compiler_state_t
*cstate
, int off
, uint16_t v
)
5692 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5695 static struct block
*
5696 gen_portatom6(compiler_state_t
*cstate
, int off
, uint16_t v
)
5698 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5701 static struct block
*
5702 gen_port(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5704 struct block
*b1
, *tmp
;
5708 b1
= gen_portatom(cstate
, 0, port
);
5712 b1
= gen_portatom(cstate
, 2, port
);
5716 tmp
= gen_portatom(cstate
, 0, port
);
5717 b1
= gen_portatom(cstate
, 2, port
);
5723 tmp
= gen_portatom(cstate
, 0, port
);
5724 b1
= gen_portatom(cstate
, 2, port
);
5734 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5742 return gen_port_common(cstate
, proto
, b1
);
5745 static struct block
*
5746 gen_port_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5748 struct block
*b0
, *tmp
;
5753 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5754 * not LLC encapsulation with LLCSAP_IP.
5756 * For IEEE 802 networks - which includes 802.5 token ring
5757 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5758 * says that SNAP encapsulation is used, not LLC encapsulation
5761 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5762 * RFC 2225 say that SNAP encapsulation is used, not LLC
5763 * encapsulation with LLCSAP_IP.
5765 * So we always check for ETHERTYPE_IP.
5767 * At the time of this writing all three L4 protocols the "port" and
5768 * "portrange" primitives support (TCP, UDP and SCTP) have the source
5769 * and the destination ports identically encoded in the transport
5770 * protocol header. So without a proto qualifier the only difference
5771 * between the implemented cases is the protocol number and all other
5772 * checks need to be made exactly once.
5774 * If the expression syntax in future starts to support ports for
5775 * another L4 protocol that has unsigned integer ports encoded using a
5776 * different size and/or offset, this will require a different code.
5782 tmp
= gen_ip_proto(cstate
, (uint8_t)proto
);
5786 tmp
= gen_ip_proto(cstate
, IPPROTO_UDP
);
5787 gen_or(gen_ip_proto(cstate
, IPPROTO_TCP
), tmp
);
5788 gen_or(gen_ip_proto(cstate
, IPPROTO_SCTP
), tmp
);
5794 // Not a fragment other than the first fragment.
5795 b0
= gen_ipfrag(cstate
);
5799 gen_and(gen_linktype(cstate
, ETHERTYPE_IP
), b1
);
5803 static struct block
*
5804 gen_port6(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5806 struct block
*b1
, *tmp
;
5810 b1
= gen_portatom6(cstate
, 0, port
);
5814 b1
= gen_portatom6(cstate
, 2, port
);
5818 tmp
= gen_portatom6(cstate
, 0, port
);
5819 b1
= gen_portatom6(cstate
, 2, port
);
5825 tmp
= gen_portatom6(cstate
, 0, port
);
5826 b1
= gen_portatom6(cstate
, 2, port
);
5834 return gen_port6_common(cstate
, proto
, b1
);
5837 static struct block
*
5838 gen_port6_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5842 // "ip6 proto 'ip_proto'"
5847 tmp
= gen_ip6_proto(cstate
, (uint8_t)proto
);
5851 // Same as in gen_port_common().
5852 tmp
= gen_ip6_proto(cstate
, IPPROTO_UDP
);
5853 gen_or(gen_ip6_proto(cstate
, IPPROTO_TCP
), tmp
);
5854 gen_or(gen_ip6_proto(cstate
, IPPROTO_SCTP
), tmp
);
5860 // XXX - catch the first fragment of a fragmented packet?
5862 // "link proto \ip6"
5863 gen_and(gen_linktype(cstate
, ETHERTYPE_IPV6
), b1
);
5867 /* gen_portrange code */
5868 static struct block
*
5869 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5873 return gen_portatom(cstate
, off
, v1
);
5875 struct block
*b1
, *b2
;
5877 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, min(v1
, v2
));
5878 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, max(v1
, v2
));
5885 static struct block
*
5886 gen_portrange(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5889 struct block
*b1
, *tmp
;
5893 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5897 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5901 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5902 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5908 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5909 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5919 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5927 return gen_port_common(cstate
, proto
, b1
);
5930 static struct block
*
5931 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5935 return gen_portatom6(cstate
, off
, v1
);
5937 struct block
*b1
, *b2
;
5939 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, min(v1
, v2
));
5940 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, max(v1
, v2
));
5947 static struct block
*
5948 gen_portrange6(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5951 struct block
*b1
, *tmp
;
5955 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5959 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5963 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5964 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5970 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5971 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5979 return gen_port6_common(cstate
, proto
, b1
);
5983 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5992 v
= pcap_nametoproto(name
);
5993 if (v
== PROTO_UNDEF
)
5994 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5998 /* XXX should look up h/w protocol type based on cstate->linktype */
5999 v
= pcap_nametoeproto(name
);
6000 if (v
== PROTO_UNDEF
) {
6001 v
= pcap_nametollc(name
);
6002 if (v
== PROTO_UNDEF
)
6003 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6008 if (strcmp(name
, "esis") == 0)
6010 else if (strcmp(name
, "isis") == 0)
6012 else if (strcmp(name
, "clnp") == 0)
6015 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6025 #if !defined(NO_PROTOCHAIN)
6027 * This primitive is non-directional by design, so the grammar does not allow
6028 * to qualify it with a direction.
6030 static struct block
*
6031 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6033 struct block
*b0
, *b
;
6034 struct slist
*s
[100];
6035 int fix2
, fix3
, fix4
, fix5
;
6036 int ahcheck
, again
, end
;
6038 int reg2
= alloc_reg(cstate
);
6040 memset(s
, 0, sizeof(s
));
6041 fix3
= fix4
= fix5
= 0;
6048 b0
= gen_protochain(cstate
, v
, Q_IP
);
6049 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6053 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
6058 * We don't handle variable-length prefixes before the link-layer
6059 * header, or variable-length link-layer headers, here yet.
6060 * We might want to add BPF instructions to do the protochain
6061 * work, to simplify that and, on platforms that have a BPF
6062 * interpreter with the new instructions, let the filtering
6063 * be done in the kernel. (We already require a modified BPF
6064 * engine to do the protochain stuff, to support backward
6065 * branches, and backward branch support is unlikely to appear
6066 * in kernel BPF engines.)
6068 if (cstate
->off_linkpl
.is_variable
)
6069 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6072 * To quote a comment in optimize.c:
6074 * "These data structures are used in a Cocke and Schwartz style
6075 * value numbering scheme. Since the flowgraph is acyclic,
6076 * exit values can be propagated from a node's predecessors
6077 * provided it is uniquely defined."
6079 * "Acyclic" means "no backward branches", which means "no
6080 * loops", so we have to turn the optimizer off.
6082 cstate
->no_optimize
= 1;
6085 * s[0] is a dummy entry to protect other BPF insn from damage
6086 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6087 * hard to find interdependency made by jump table fixup.
6090 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6095 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6098 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6099 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6101 /* X = ip->ip_hl << 2 */
6102 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6103 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6108 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6110 /* A = ip6->ip_nxt */
6111 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6112 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6114 /* X = sizeof(struct ip6_hdr) */
6115 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6121 bpf_error(cstate
, "unsupported proto to gen_protochain");
6125 /* again: if (A == v) goto end; else fall through; */
6127 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6129 s
[i
]->s
.jt
= NULL
; /*later*/
6130 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6134 #ifndef IPPROTO_NONE
6135 #define IPPROTO_NONE 59
6137 /* if (A == IPPROTO_NONE) goto end */
6138 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6139 s
[i
]->s
.jt
= NULL
; /*later*/
6140 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6141 s
[i
]->s
.k
= IPPROTO_NONE
;
6142 s
[fix5
]->s
.jf
= s
[i
];
6146 if (proto
== Q_IPV6
) {
6147 int v6start
, v6end
, v6advance
, j
;
6150 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6151 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6152 s
[i
]->s
.jt
= NULL
; /*later*/
6153 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6154 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6155 s
[fix2
]->s
.jf
= s
[i
];
6157 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6158 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6159 s
[i
]->s
.jt
= NULL
; /*later*/
6160 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6161 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6163 /* if (A == IPPROTO_ROUTING) goto v6advance */
6164 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6165 s
[i
]->s
.jt
= NULL
; /*later*/
6166 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6167 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6169 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6170 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6171 s
[i
]->s
.jt
= NULL
; /*later*/
6172 s
[i
]->s
.jf
= NULL
; /*later*/
6173 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6183 * A = P[X + packet head];
6184 * X = X + (P[X + packet head + 1] + 1) * 8;
6186 /* A = P[X + packet head] */
6187 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6188 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6191 s
[i
] = new_stmt(cstate
, BPF_ST
);
6194 /* A = P[X + packet head + 1]; */
6195 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6196 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6199 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6203 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6207 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6211 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6214 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6218 /* goto again; (must use BPF_JA for backward jump) */
6219 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6220 s
[i
]->s
.k
= again
- i
- 1;
6221 s
[i
- 1]->s
.jf
= s
[i
];
6225 for (j
= v6start
; j
<= v6end
; j
++)
6226 s
[j
]->s
.jt
= s
[v6advance
];
6229 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6231 s
[fix2
]->s
.jf
= s
[i
];
6237 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6238 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6239 s
[i
]->s
.jt
= NULL
; /*later*/
6240 s
[i
]->s
.jf
= NULL
; /*later*/
6241 s
[i
]->s
.k
= IPPROTO_AH
;
6243 s
[fix3
]->s
.jf
= s
[ahcheck
];
6250 * X = X + (P[X + 1] + 2) * 4;
6253 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6255 /* A = P[X + packet head]; */
6256 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6257 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6260 s
[i
] = new_stmt(cstate
, BPF_ST
);
6264 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6267 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6271 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6273 /* A = P[X + packet head] */
6274 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6275 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6278 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6282 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6286 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6289 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6293 /* goto again; (must use BPF_JA for backward jump) */
6294 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6295 s
[i
]->s
.k
= again
- i
- 1;
6300 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6302 s
[fix2
]->s
.jt
= s
[end
];
6303 s
[fix4
]->s
.jf
= s
[end
];
6304 s
[fix5
]->s
.jt
= s
[end
];
6311 for (i
= 0; i
< max
- 1; i
++)
6312 s
[i
]->next
= s
[i
+ 1];
6313 s
[max
- 1]->next
= NULL
;
6317 * Remember, s[0] is dummy.
6319 b
= gen_jmp(cstate
, BPF_JEQ
, v
, s
[1]);
6321 free_reg(cstate
, reg2
);
6326 #endif /* !defined(NO_PROTOCHAIN) */
6329 * Generate code that checks whether the packet is a packet for protocol
6330 * <proto> and whether the type field in that protocol's header has
6331 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6332 * IP packet and checks the protocol number in the IP header against <v>.
6334 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6335 * against Q_IP and Q_IPV6.
6337 * This primitive is non-directional by design, so the grammar does not allow
6338 * to qualify it with a direction.
6340 static struct block
*
6341 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6343 struct block
*b0
, *b1
;
6348 b0
= gen_proto(cstate
, v
, Q_IP
);
6349 b1
= gen_proto(cstate
, v
, Q_IPV6
);
6354 return gen_linktype(cstate
, v
);
6357 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6359 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6360 * not LLC encapsulation with LLCSAP_IP.
6362 * For IEEE 802 networks - which includes 802.5 token ring
6363 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6364 * says that SNAP encapsulation is used, not LLC encapsulation
6367 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6368 * RFC 2225 say that SNAP encapsulation is used, not LLC
6369 * encapsulation with LLCSAP_IP.
6371 * So we always check for ETHERTYPE_IP.
6373 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6374 // 0 <= v <= UINT8_MAX
6375 b1
= gen_ip_proto(cstate
, (uint8_t)v
);
6393 break; // invalid qualifier
6396 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6397 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6399 * Also check for a fragment header before the final
6402 b2
= gen_ip6_proto(cstate
, IPPROTO_FRAGMENT
);
6403 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6405 // 0 <= v <= UINT8_MAX
6406 b2
= gen_ip6_proto(cstate
, (uint8_t)v
);
6417 break; // invalid qualifier
6420 assert_maxval(cstate
, "ISO protocol", v
, UINT8_MAX
);
6421 switch (cstate
->linktype
) {
6425 * Frame Relay packets typically have an OSI
6426 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6427 * generates code to check for all the OSI
6428 * NLPIDs, so calling it and then adding a check
6429 * for the particular NLPID for which we're
6430 * looking is bogus, as we can just check for
6433 * What we check for is the NLPID and a frame
6434 * control field value of UI, i.e. 0x03 followed
6437 * XXX - assumes a 2-byte Frame Relay header with
6438 * DLCI and flags. What if the address is longer?
6440 * XXX - what about SNAP-encapsulated frames?
6442 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6448 * Cisco uses an Ethertype lookalike - for OSI,
6451 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6452 /* OSI in C-HDLC is stuffed with a fudge byte */
6453 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6458 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6459 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6465 break; // invalid qualifier
6468 assert_maxval(cstate
, "IS-IS PDU type", v
, ISIS_PDU_TYPE_MAX
);
6469 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
6471 * 4 is the offset of the PDU type relative to the IS-IS
6473 * Except when it is not, see above.
6475 unsigned pdu_type_offset
;
6476 switch (cstate
->linktype
) {
6479 pdu_type_offset
= 5;
6482 pdu_type_offset
= 4;
6484 b1
= gen_mcmp(cstate
, OR_LINKPL_NOSNAP
, pdu_type_offset
, BPF_B
,
6485 v
, ISIS_PDU_TYPE_MAX
);
6502 break; // invalid qualifier
6508 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6513 * Convert a non-numeric name to a port number.
6516 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6518 struct addrinfo hints
, *res
, *ai
;
6520 struct sockaddr_in
*in4
;
6522 struct sockaddr_in6
*in6
;
6527 * We check for both TCP and UDP in case there are
6528 * ambiguous entries.
6530 memset(&hints
, 0, sizeof(hints
));
6531 hints
.ai_family
= PF_UNSPEC
;
6532 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6533 hints
.ai_protocol
= ipproto
;
6534 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6541 * No such port. Just return -1.
6548 * We don't use strerror() because it's not
6549 * guaranteed to be thread-safe on all platforms
6550 * (probably because it might use a non-thread-local
6551 * buffer into which to format an error message
6552 * if the error code isn't one for which it has
6553 * a canned string; three cheers for C string
6556 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6558 port
= -2; /* a real error */
6564 * This is a real error, not just "there's
6565 * no such service name".
6567 * We don't use gai_strerror() because it's not
6568 * guaranteed to be thread-safe on all platforms
6569 * (probably because it might use a non-thread-local
6570 * buffer into which to format an error message
6571 * if the error code isn't one for which it has
6572 * a canned string; three cheers for C string
6575 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6577 port
= -2; /* a real error */
6582 * OK, we found it. Did it find anything?
6584 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6586 * Does it have an address?
6588 if (ai
->ai_addr
!= NULL
) {
6590 * Yes. Get a port number; we're done.
6592 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6593 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6594 port
= ntohs(in4
->sin_port
);
6598 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6599 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6600 port
= ntohs(in6
->sin6_port
);
6612 * Convert a string to a port number.
6615 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6625 * See if it's a number.
6627 ret
= stoulen(string
, string_size
, &val
, cstate
);
6631 /* Unknown port type - it's just a number. */
6632 *proto
= PROTO_UNDEF
;
6635 case STOULEN_NOT_OCTAL_NUMBER
:
6636 case STOULEN_NOT_HEX_NUMBER
:
6637 case STOULEN_NOT_DECIMAL_NUMBER
:
6639 * Not a valid number; try looking it up as a port.
6641 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6642 memcpy(cpy
, string
, string_size
);
6643 cpy
[string_size
] = '\0';
6644 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6645 if (tcp_port
== -2) {
6647 * We got a hard error; the error string has
6651 longjmp(cstate
->top_ctx
, 1);
6654 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6655 if (udp_port
== -2) {
6657 * We got a hard error; the error string has
6661 longjmp(cstate
->top_ctx
, 1);
6666 * We need to check /etc/services for ambiguous entries.
6667 * If we find an ambiguous entry, and it has the
6668 * same port number, change the proto to PROTO_UNDEF
6669 * so both TCP and UDP will be checked.
6671 if (tcp_port
>= 0) {
6672 val
= (bpf_u_int32
)tcp_port
;
6673 *proto
= IPPROTO_TCP
;
6674 if (udp_port
>= 0) {
6675 if (udp_port
== tcp_port
)
6676 *proto
= PROTO_UNDEF
;
6679 /* Can't handle ambiguous names that refer
6680 to different port numbers. */
6681 warning("ambiguous port %s in /etc/services",
6688 if (udp_port
>= 0) {
6689 val
= (bpf_u_int32
)udp_port
;
6690 *proto
= IPPROTO_UDP
;
6694 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6696 longjmp(cstate
->top_ctx
, 1);
6703 /* Error already set. */
6704 longjmp(cstate
->top_ctx
, 1);
6711 /* Should not happen */
6712 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6713 longjmp(cstate
->top_ctx
, 1);
6720 * Convert a string in the form PPP-PPP, which correspond to ports, to
6721 * a starting and ending port in a port range.
6724 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6725 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6728 const char *first
, *second
;
6729 size_t first_size
, second_size
;
6732 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6733 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6736 * Make sure there are no other hyphens.
6738 * XXX - we support named ports, but there are some port names
6739 * in /etc/services that include hyphens, so this would rule
6742 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6743 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6747 * Get the length of the first port.
6750 first_size
= hyphen_off
- string
;
6751 if (first_size
== 0) {
6752 /* Range of "-port", which we don't support. */
6753 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6757 * Try to convert it to a port.
6759 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6760 save_proto
= *proto
;
6763 * Get the length of the second port.
6765 second
= hyphen_off
+ 1;
6766 second_size
= strlen(second
);
6767 if (second_size
== 0) {
6768 /* Range of "port-", which we don't support. */
6769 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6773 * Try to convert it to a port.
6775 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6776 if (*proto
!= save_proto
)
6777 *proto
= PROTO_UNDEF
;
6781 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6783 int proto
= q
.proto
;
6787 bpf_u_int32 mask
, addr
;
6788 struct addrinfo
*res
, *res0
;
6789 struct sockaddr_in
*sin4
;
6792 struct sockaddr_in6
*sin6
;
6793 struct in6_addr mask128
;
6795 struct block
*b
, *tmp
;
6796 int port
, real_proto
;
6797 bpf_u_int32 port1
, port2
;
6800 * Catch errors reported by us and routines below us, and return NULL
6803 if (setjmp(cstate
->top_ctx
))
6809 addr
= pcap_nametonetaddr(name
);
6811 bpf_error(cstate
, "unknown network '%s'", name
);
6812 /* Left justify network addr and calculate its network mask */
6814 while (addr
&& (addr
& 0xff000000) == 0) {
6818 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6822 if (proto
== Q_LINK
) {
6823 switch (cstate
->linktype
) {
6826 case DLT_NETANALYZER
:
6827 case DLT_NETANALYZER_TRANSPARENT
:
6828 eaddr
= pcap_ether_hostton(name
);
6831 "unknown ether host '%s'", name
);
6832 tmp
= gen_prevlinkhdr_check(cstate
);
6833 b
= gen_ehostop(cstate
, eaddr
, dir
);
6840 eaddr
= pcap_ether_hostton(name
);
6843 "unknown FDDI host '%s'", name
);
6844 b
= gen_fhostop(cstate
, eaddr
, dir
);
6849 eaddr
= pcap_ether_hostton(name
);
6852 "unknown token ring host '%s'", name
);
6853 b
= gen_thostop(cstate
, eaddr
, dir
);
6857 case DLT_IEEE802_11
:
6858 case DLT_PRISM_HEADER
:
6859 case DLT_IEEE802_11_RADIO_AVS
:
6860 case DLT_IEEE802_11_RADIO
:
6862 eaddr
= pcap_ether_hostton(name
);
6865 "unknown 802.11 host '%s'", name
);
6866 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6870 case DLT_IP_OVER_FC
:
6871 eaddr
= pcap_ether_hostton(name
);
6874 "unknown Fibre Channel host '%s'", name
);
6875 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6880 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6881 } else if (proto
== Q_DECNET
) {
6883 * A long time ago on Ultrix libpcap supported
6884 * translation of DECnet host names into DECnet
6885 * addresses, but this feature is history now.
6887 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6890 memset(&mask128
, 0xff, sizeof(mask128
));
6892 res0
= res
= pcap_nametoaddrinfo(name
);
6894 bpf_error(cstate
, "unknown host '%s'", name
);
6901 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6902 tproto
== Q_DEFAULT
) {
6908 for (res
= res0
; res
; res
= res
->ai_next
) {
6909 switch (res
->ai_family
) {
6912 if (tproto
== Q_IPV6
)
6916 sin4
= (struct sockaddr_in
*)
6918 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6919 0xffffffff, tproto
, dir
, q
.addr
);
6923 if (tproto6
== Q_IP
)
6926 sin6
= (struct sockaddr_in6
*)
6928 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6929 &mask128
, tproto6
, dir
, q
.addr
);
6942 bpf_error(cstate
, "unknown host '%s'%s", name
,
6943 (proto
== Q_DEFAULT
)
6945 : " for specified address family");
6951 (void)port_pq_to_ipproto(cstate
, proto
, "port"); // validate only
6952 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6953 bpf_error(cstate
, "unknown port '%s'", name
);
6954 if (proto
== Q_UDP
) {
6955 if (real_proto
== IPPROTO_TCP
)
6956 bpf_error(cstate
, "port '%s' is tcp", name
);
6957 else if (real_proto
== IPPROTO_SCTP
)
6958 bpf_error(cstate
, "port '%s' is sctp", name
);
6960 /* override PROTO_UNDEF */
6961 real_proto
= IPPROTO_UDP
;
6963 if (proto
== Q_TCP
) {
6964 if (real_proto
== IPPROTO_UDP
)
6965 bpf_error(cstate
, "port '%s' is udp", name
);
6967 else if (real_proto
== IPPROTO_SCTP
)
6968 bpf_error(cstate
, "port '%s' is sctp", name
);
6970 /* override PROTO_UNDEF */
6971 real_proto
= IPPROTO_TCP
;
6973 if (proto
== Q_SCTP
) {
6974 if (real_proto
== IPPROTO_UDP
)
6975 bpf_error(cstate
, "port '%s' is udp", name
);
6977 else if (real_proto
== IPPROTO_TCP
)
6978 bpf_error(cstate
, "port '%s' is tcp", name
);
6980 /* override PROTO_UNDEF */
6981 real_proto
= IPPROTO_SCTP
;
6984 bpf_error(cstate
, "illegal port number %d < 0", port
);
6986 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6987 // real_proto can be PROTO_UNDEF
6988 b
= gen_port(cstate
, (uint16_t)port
, real_proto
, dir
);
6989 gen_or(gen_port6(cstate
, (uint16_t)port
, real_proto
, dir
), b
);
6993 (void)port_pq_to_ipproto(cstate
, proto
, "portrange"); // validate only
6994 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
6995 if (proto
== Q_UDP
) {
6996 if (real_proto
== IPPROTO_TCP
)
6997 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6998 else if (real_proto
== IPPROTO_SCTP
)
6999 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7001 /* override PROTO_UNDEF */
7002 real_proto
= IPPROTO_UDP
;
7004 if (proto
== Q_TCP
) {
7005 if (real_proto
== IPPROTO_UDP
)
7006 bpf_error(cstate
, "port in range '%s' is udp", name
);
7007 else if (real_proto
== IPPROTO_SCTP
)
7008 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7010 /* override PROTO_UNDEF */
7011 real_proto
= IPPROTO_TCP
;
7013 if (proto
== Q_SCTP
) {
7014 if (real_proto
== IPPROTO_UDP
)
7015 bpf_error(cstate
, "port in range '%s' is udp", name
);
7016 else if (real_proto
== IPPROTO_TCP
)
7017 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7019 /* override PROTO_UNDEF */
7020 real_proto
= IPPROTO_SCTP
;
7023 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7025 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7027 // real_proto can be PROTO_UNDEF
7028 b
= gen_portrange(cstate
, (uint16_t)port1
, (uint16_t)port2
,
7030 gen_or(gen_portrange6(cstate
, (uint16_t)port1
, (uint16_t)port2
,
7031 real_proto
, dir
), b
);
7036 eaddr
= pcap_ether_hostton(name
);
7038 bpf_error(cstate
, "unknown ether host: %s", name
);
7040 res
= pcap_nametoaddrinfo(name
);
7043 bpf_error(cstate
, "unknown host '%s'", name
);
7044 b
= gen_gateway(cstate
, eaddr
, res
, proto
);
7049 bpf_error(cstate
, "unknown host '%s'", name
);
7052 bpf_error(cstate
, "'gateway' not supported in this configuration");
7056 real_proto
= lookup_proto(cstate
, name
, proto
);
7057 if (real_proto
>= 0)
7058 return gen_proto(cstate
, real_proto
, proto
);
7060 bpf_error(cstate
, "unknown protocol: %s", name
);
7062 #if !defined(NO_PROTOCHAIN)
7064 real_proto
= lookup_proto(cstate
, name
, proto
);
7065 if (real_proto
>= 0)
7066 return gen_protochain(cstate
, real_proto
, proto
);
7068 bpf_error(cstate
, "unknown protocol: %s", name
);
7069 #endif /* !defined(NO_PROTOCHAIN) */
7080 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7081 bpf_u_int32 masklen
, struct qual q
)
7083 register int nlen
, mlen
;
7088 * Catch errors reported by us and routines below us, and return NULL
7091 if (setjmp(cstate
->top_ctx
))
7094 nlen
= pcapint_atoin(s1
, &n
);
7096 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7097 /* Promote short ipaddr */
7101 mlen
= pcapint_atoin(s2
, &m
);
7103 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7104 /* Promote short ipaddr */
7107 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7110 /* Convert mask len to mask */
7112 bpf_error(cstate
, "mask length must be <= 32");
7113 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7114 m
= (bpf_u_int32
)m64
;
7116 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7123 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7126 // Q_HOST and Q_GATEWAY only (see the grammar)
7127 bpf_error(cstate
, "Mask syntax for networks only");
7134 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7142 * Catch errors reported by us and routines below us, and return NULL
7145 if (setjmp(cstate
->top_ctx
))
7152 * v contains a 32-bit unsigned parsed from a string of the
7153 * form {N}, which could be decimal, hexadecimal or octal.
7154 * Although it would be possible to use the value as a raw
7155 * 16-bit DECnet address when the value fits into 16 bits, this
7156 * would be a questionable feature: DECnet address wire
7157 * encoding is little-endian, so this would not work as
7158 * intuitively as the same works for [big-endian] IPv4
7159 * addresses (0x01020304 means 1.2.3.4).
7161 if (proto
== Q_DECNET
)
7162 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7164 } else if (proto
== Q_DECNET
) {
7166 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7167 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7168 * for a valid DECnet address.
7170 vlen
= pcapint_atodn(s
, &v
);
7172 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7175 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7176 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7179 vlen
= pcapint_atoin(s
, &v
);
7181 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7189 if (proto
== Q_DECNET
)
7190 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7191 else if (proto
== Q_LINK
) {
7192 // "link (host|net) IPV4ADDR" and variations thereof
7193 bpf_error(cstate
, "illegal link layer address");
7196 if (s
== NULL
&& q
.addr
== Q_NET
) {
7197 /* Promote short net number */
7198 while (v
&& (v
& 0xff000000) == 0) {
7203 /* Promote short ipaddr */
7205 mask
<<= 32 - vlen
;
7207 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7211 proto
= port_pq_to_ipproto(cstate
, proto
, "port");
7214 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7216 // proto can be PROTO_UNDEF
7219 b
= gen_port(cstate
, (uint16_t)v
, proto
, dir
);
7220 gen_or(gen_port6(cstate
, (uint16_t)v
, proto
, dir
), b
);
7225 proto
= port_pq_to_ipproto(cstate
, proto
, "portrange");
7228 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7230 // proto can be PROTO_UNDEF
7233 b
= gen_portrange(cstate
, (uint16_t)v
, (uint16_t)v
,
7235 gen_or(gen_portrange6(cstate
, (uint16_t)v
, (uint16_t)v
,
7241 bpf_error(cstate
, "'gateway' requires a name");
7245 return gen_proto(cstate
, v
, proto
);
7247 #if !defined(NO_PROTOCHAIN)
7249 return gen_protochain(cstate
, v
, proto
);
7265 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7268 struct addrinfo
*res
;
7269 struct in6_addr
*addr
;
7270 struct in6_addr mask
;
7272 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7275 * Catch errors reported by us and routines below us, and return NULL
7278 if (setjmp(cstate
->top_ctx
))
7281 res
= pcap_nametoaddrinfo(s
);
7283 bpf_error(cstate
, "invalid ip6 address %s", s
);
7286 bpf_error(cstate
, "%s resolved to multiple address", s
);
7287 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7289 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7290 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7291 memset(&mask
, 0, sizeof(mask
));
7292 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7294 mask
.s6_addr
[masklen
/ 8] =
7295 (0xff << (8 - masklen
% 8)) & 0xff;
7298 memcpy(a
, addr
, sizeof(a
));
7299 memcpy(m
, &mask
, sizeof(m
));
7300 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7301 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7302 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7310 bpf_error(cstate
, "Mask syntax for networks only");
7314 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7320 // Q_GATEWAY only (see the grammar)
7321 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7328 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7330 struct block
*b
, *tmp
;
7333 * Catch errors reported by us and routines below us, and return NULL
7336 if (setjmp(cstate
->top_ctx
))
7339 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7340 cstate
->e
= pcap_ether_aton(s
);
7341 if (cstate
->e
== NULL
)
7342 bpf_error(cstate
, "malloc");
7343 switch (cstate
->linktype
) {
7345 case DLT_NETANALYZER
:
7346 case DLT_NETANALYZER_TRANSPARENT
:
7347 tmp
= gen_prevlinkhdr_check(cstate
);
7348 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7353 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7356 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7358 case DLT_IEEE802_11
:
7359 case DLT_PRISM_HEADER
:
7360 case DLT_IEEE802_11_RADIO_AVS
:
7361 case DLT_IEEE802_11_RADIO
:
7363 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7365 case DLT_IP_OVER_FC
:
7366 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7371 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7378 bpf_error(cstate
, "ethernet address used in non-ether expression");
7383 sappend(struct slist
*s0
, struct slist
*s1
)
7386 * This is definitely not the best way to do this, but the
7387 * lists will rarely get long.
7394 static struct slist
*
7395 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7399 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7404 static struct slist
*
7405 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7409 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7415 * Modify "index" to use the value stored into its register as an
7416 * offset relative to the beginning of the header for the protocol
7417 * "proto", and allocate a register and put an item "size" bytes long
7418 * (1, 2, or 4) at that offset into that register, making it the register
7421 static struct arth
*
7422 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7426 struct slist
*s
, *tmp
;
7428 int regno
= alloc_reg(cstate
);
7430 free_reg(cstate
, inst
->regno
);
7434 bpf_error(cstate
, "data size must be 1, 2, or 4");
7451 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7455 * The offset is relative to the beginning of the packet
7456 * data, if we have a radio header. (If we don't, this
7459 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7460 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7461 cstate
->linktype
!= DLT_PRISM_HEADER
)
7462 bpf_error(cstate
, "radio information not present in capture");
7465 * Load into the X register the offset computed into the
7466 * register specified by "index".
7468 s
= xfer_to_x(cstate
, inst
);
7471 * Load the item at that offset.
7473 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7475 sappend(inst
->s
, s
);
7480 * The offset is relative to the beginning of
7481 * the link-layer header.
7483 * XXX - what about ATM LANE? Should the index be
7484 * relative to the beginning of the AAL5 frame, so
7485 * that 0 refers to the beginning of the LE Control
7486 * field, or relative to the beginning of the LAN
7487 * frame, so that 0 refers, for Ethernet LANE, to
7488 * the beginning of the destination address?
7490 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7493 * If "s" is non-null, it has code to arrange that the
7494 * X register contains the length of the prefix preceding
7495 * the link-layer header. Add to it the offset computed
7496 * into the register specified by "index", and move that
7497 * into the X register. Otherwise, just load into the X
7498 * register the offset computed into the register specified
7502 sappend(s
, xfer_to_a(cstate
, inst
));
7503 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7504 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7506 s
= xfer_to_x(cstate
, inst
);
7509 * Load the item at the sum of the offset we've put in the
7510 * X register and the offset of the start of the link
7511 * layer header (which is 0 if the radio header is
7512 * variable-length; that header length is what we put
7513 * into the X register and then added to the index).
7515 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7516 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7518 sappend(inst
->s
, s
);
7532 * The offset is relative to the beginning of
7533 * the network-layer header.
7534 * XXX - are there any cases where we want
7535 * cstate->off_nl_nosnap?
7537 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7540 * If "s" is non-null, it has code to arrange that the
7541 * X register contains the variable part of the offset
7542 * of the link-layer payload. Add to it the offset
7543 * computed into the register specified by "index",
7544 * and move that into the X register. Otherwise, just
7545 * load into the X register the offset computed into
7546 * the register specified by "index".
7549 sappend(s
, xfer_to_a(cstate
, inst
));
7550 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7551 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7553 s
= xfer_to_x(cstate
, inst
);
7556 * Load the item at the sum of the offset we've put in the
7557 * X register, the offset of the start of the network
7558 * layer header from the beginning of the link-layer
7559 * payload, and the constant part of the offset of the
7560 * start of the link-layer payload.
7562 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7563 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7565 sappend(inst
->s
, s
);
7568 * Do the computation only if the packet contains
7569 * the protocol in question.
7571 b
= gen_proto_abbrev_internal(cstate
, proto
);
7573 gen_and(inst
->b
, b
);
7587 * The offset is relative to the beginning of
7588 * the transport-layer header.
7590 * Load the X register with the length of the IPv4 header
7591 * (plus the offset of the link-layer header, if it's
7592 * a variable-length header), in bytes.
7594 * XXX - are there any cases where we want
7595 * cstate->off_nl_nosnap?
7596 * XXX - we should, if we're built with
7597 * IPv6 support, generate code to load either
7598 * IPv4, IPv6, or both, as appropriate.
7600 s
= gen_loadx_iphdrlen(cstate
);
7603 * The X register now contains the sum of the variable
7604 * part of the offset of the link-layer payload and the
7605 * length of the network-layer header.
7607 * Load into the A register the offset relative to
7608 * the beginning of the transport layer header,
7609 * add the X register to that, move that to the
7610 * X register, and load with an offset from the
7611 * X register equal to the sum of the constant part of
7612 * the offset of the link-layer payload and the offset,
7613 * relative to the beginning of the link-layer payload,
7614 * of the network-layer header.
7616 sappend(s
, xfer_to_a(cstate
, inst
));
7617 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7618 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7619 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7620 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7621 sappend(inst
->s
, s
);
7624 * Do the computation only if the packet contains
7625 * the protocol in question - which is true only
7626 * if this is an IP datagram and is the first or
7627 * only fragment of that datagram.
7629 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7631 gen_and(inst
->b
, b
);
7632 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7637 * Do the computation only if the packet contains
7638 * the protocol in question.
7640 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7642 gen_and(inst
->b
, b
);
7646 * Check if we have an icmp6 next header
7648 b
= gen_ip6_proto(cstate
, 58);
7650 gen_and(inst
->b
, b
);
7653 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7655 * If "s" is non-null, it has code to arrange that the
7656 * X register contains the variable part of the offset
7657 * of the link-layer payload. Add to it the offset
7658 * computed into the register specified by "index",
7659 * and move that into the X register. Otherwise, just
7660 * load into the X register the offset computed into
7661 * the register specified by "index".
7664 sappend(s
, xfer_to_a(cstate
, inst
));
7665 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7666 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7668 s
= xfer_to_x(cstate
, inst
);
7671 * Load the item at the sum of the offset we've put in the
7672 * X register, the offset of the start of the network
7673 * layer header from the beginning of the link-layer
7674 * payload, and the constant part of the offset of the
7675 * start of the link-layer payload.
7677 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7678 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7681 sappend(inst
->s
, s
);
7685 inst
->regno
= regno
;
7686 s
= new_stmt(cstate
, BPF_ST
);
7688 sappend(inst
->s
, s
);
7694 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7698 * Catch errors reported by us and routines below us, and return NULL
7701 if (setjmp(cstate
->top_ctx
))
7704 return gen_load_internal(cstate
, proto
, inst
, size
);
7707 static struct block
*
7708 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7709 struct arth
*a1
, int reversed
)
7711 struct slist
*s0
, *s1
, *s2
;
7712 struct block
*b
, *tmp
;
7714 s0
= xfer_to_x(cstate
, a1
);
7715 s1
= xfer_to_a(cstate
, a0
);
7716 if (code
== BPF_JEQ
) {
7717 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7718 b
= new_block(cstate
, JMP(code
));
7722 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7728 sappend(a0
->s
, a1
->s
);
7732 free_reg(cstate
, a0
->regno
);
7733 free_reg(cstate
, a1
->regno
);
7735 /* 'and' together protocol checks */
7738 gen_and(a0
->b
, tmp
= a1
->b
);
7752 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7753 struct arth
*a1
, int reversed
)
7756 * Catch errors reported by us and routines below us, and return NULL
7759 if (setjmp(cstate
->top_ctx
))
7762 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7766 gen_loadlen(compiler_state_t
*cstate
)
7773 * Catch errors reported by us and routines below us, and return NULL
7776 if (setjmp(cstate
->top_ctx
))
7779 regno
= alloc_reg(cstate
);
7780 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7781 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7782 s
->next
= new_stmt(cstate
, BPF_ST
);
7783 s
->next
->s
.k
= regno
;
7790 static struct arth
*
7791 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7797 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7799 reg
= alloc_reg(cstate
);
7801 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7803 s
->next
= new_stmt(cstate
, BPF_ST
);
7812 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7815 * Catch errors reported by us and routines below us, and return NULL
7818 if (setjmp(cstate
->top_ctx
))
7821 return gen_loadi_internal(cstate
, val
);
7825 * The a_arg dance is to avoid annoying whining by compilers that
7826 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7827 * It's not *used* after setjmp returns.
7830 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7832 struct arth
*a
= a_arg
;
7836 * Catch errors reported by us and routines below us, and return NULL
7839 if (setjmp(cstate
->top_ctx
))
7842 s
= xfer_to_a(cstate
, a
);
7844 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7847 s
= new_stmt(cstate
, BPF_ST
);
7855 * The a0_arg dance is to avoid annoying whining by compilers that
7856 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7857 * It's not *used* after setjmp returns.
7860 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7863 struct arth
*a0
= a0_arg
;
7864 struct slist
*s0
, *s1
, *s2
;
7867 * Catch errors reported by us and routines below us, and return NULL
7870 if (setjmp(cstate
->top_ctx
))
7874 * Disallow division by, or modulus by, zero; we do this here
7875 * so that it gets done even if the optimizer is disabled.
7877 * Also disallow shifts by a value greater than 31; we do this
7878 * here, for the same reason.
7880 if (code
== BPF_DIV
) {
7881 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7882 bpf_error(cstate
, "division by zero");
7883 } else if (code
== BPF_MOD
) {
7884 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7885 bpf_error(cstate
, "modulus by zero");
7886 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7887 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7888 bpf_error(cstate
, "shift by more than 31 bits");
7890 s0
= xfer_to_x(cstate
, a1
);
7891 s1
= xfer_to_a(cstate
, a0
);
7892 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7897 sappend(a0
->s
, a1
->s
);
7899 free_reg(cstate
, a0
->regno
);
7900 free_reg(cstate
, a1
->regno
);
7902 s0
= new_stmt(cstate
, BPF_ST
);
7903 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7910 * Initialize the table of used registers and the current register.
7913 init_regs(compiler_state_t
*cstate
)
7916 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7920 * Return the next free register.
7923 alloc_reg(compiler_state_t
*cstate
)
7925 int n
= BPF_MEMWORDS
;
7928 if (cstate
->regused
[cstate
->curreg
])
7929 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7931 cstate
->regused
[cstate
->curreg
] = 1;
7932 return cstate
->curreg
;
7935 bpf_error(cstate
, "too many registers needed to evaluate expression");
7940 * Return a register to the table so it can
7944 free_reg(compiler_state_t
*cstate
, int n
)
7946 cstate
->regused
[n
] = 0;
7949 static struct block
*
7950 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7954 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7955 return gen_jmp(cstate
, jmp
, n
, s
);
7959 gen_greater(compiler_state_t
*cstate
, int n
)
7962 * Catch errors reported by us and routines below us, and return NULL
7965 if (setjmp(cstate
->top_ctx
))
7968 return gen_len(cstate
, BPF_JGE
, n
);
7972 * Actually, this is less than or equal.
7975 gen_less(compiler_state_t
*cstate
, int n
)
7980 * Catch errors reported by us and routines below us, and return NULL
7983 if (setjmp(cstate
->top_ctx
))
7986 b
= gen_len(cstate
, BPF_JGT
, n
);
7993 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7994 * the beginning of the link-layer header.
7995 * XXX - that means you can't test values in the radiotap header, but
7996 * as that header is difficult if not impossible to parse generally
7997 * without a loop, that might not be a severe problem. A new keyword
7998 * "radio" could be added for that, although what you'd really want
7999 * would be a way of testing particular radio header values, which
8000 * would generate code appropriate to the radio header in question.
8003 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8009 * Catch errors reported by us and routines below us, and return NULL
8012 if (setjmp(cstate
->top_ctx
))
8015 assert_maxval(cstate
, "byte argument", val
, UINT8_MAX
);
8022 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8025 return gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8028 return gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8031 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8035 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8039 // Load the required byte first.
8040 struct slist
*s0
= gen_load_a(cstate
, OR_LINKHDR
, idx
, BPF_B
);
8042 b
= gen_jmp(cstate
, BPF_JEQ
, 0, s0
);
8049 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8051 bpf_u_int32 hostmask
;
8052 struct block
*b0
, *b1
, *b2
;
8053 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8056 * Catch errors reported by us and routines below us, and return NULL
8059 if (setjmp(cstate
->top_ctx
))
8066 switch (cstate
->linktype
) {
8068 case DLT_ARCNET_LINUX
:
8069 // ARCnet broadcast is [8-bit] destination address 0.
8070 return gen_ahostop(cstate
, 0, Q_DST
);
8072 case DLT_NETANALYZER
:
8073 case DLT_NETANALYZER_TRANSPARENT
:
8074 b1
= gen_prevlinkhdr_check(cstate
);
8075 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8080 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8082 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8083 case DLT_IEEE802_11
:
8084 case DLT_PRISM_HEADER
:
8085 case DLT_IEEE802_11_RADIO_AVS
:
8086 case DLT_IEEE802_11_RADIO
:
8088 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8089 case DLT_IP_OVER_FC
:
8090 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8092 fail_kw_on_dlt(cstate
, "broadcast");
8097 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8098 * as an indication that we don't know the netmask, and fail
8101 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8102 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8103 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8104 hostmask
= ~cstate
->netmask
;
8105 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8106 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8111 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
8116 * Generate code to test the low-order bit of a MAC address (that's
8117 * the bottom bit of the *first* byte).
8119 static struct block
*
8120 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8122 register struct slist
*s
;
8124 /* link[offset] & 1 != 0 */
8125 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8126 return gen_set(cstate
, 1, s
);
8130 gen_multicast(compiler_state_t
*cstate
, int proto
)
8132 register struct block
*b0
, *b1
, *b2
;
8133 register struct slist
*s
;
8136 * Catch errors reported by us and routines below us, and return NULL
8139 if (setjmp(cstate
->top_ctx
))
8146 switch (cstate
->linktype
) {
8148 case DLT_ARCNET_LINUX
:
8149 // ARCnet multicast is the same as broadcast.
8150 return gen_ahostop(cstate
, 0, Q_DST
);
8152 case DLT_NETANALYZER
:
8153 case DLT_NETANALYZER_TRANSPARENT
:
8154 b1
= gen_prevlinkhdr_check(cstate
);
8155 /* ether[0] & 1 != 0 */
8156 b0
= gen_mac_multicast(cstate
, 0);
8162 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8164 * XXX - was that referring to bit-order issues?
8166 /* fddi[1] & 1 != 0 */
8167 return gen_mac_multicast(cstate
, 1);
8169 /* tr[2] & 1 != 0 */
8170 return gen_mac_multicast(cstate
, 2);
8171 case DLT_IEEE802_11
:
8172 case DLT_PRISM_HEADER
:
8173 case DLT_IEEE802_11_RADIO_AVS
:
8174 case DLT_IEEE802_11_RADIO
:
8179 * For control frames, there is no DA.
8181 * For management frames, DA is at an
8182 * offset of 4 from the beginning of
8185 * For data frames, DA is at an offset
8186 * of 4 from the beginning of the packet
8187 * if To DS is clear and at an offset of
8188 * 16 from the beginning of the packet
8193 * Generate the tests to be done for data frames.
8195 * First, check for To DS set, i.e. "link[1] & 0x01".
8197 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8198 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8201 * If To DS is set, the DA is at 16.
8203 b0
= gen_mac_multicast(cstate
, 16);
8207 * Now, check for To DS not set, i.e. check
8208 * "!(link[1] & 0x01)".
8210 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8211 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8214 * If To DS is not set, the DA is at 4.
8216 b1
= gen_mac_multicast(cstate
, 4);
8220 * Now OR together the last two checks. That gives
8221 * the complete set of checks for data frames.
8226 * Now check for a data frame.
8227 * I.e, check "link[0] & 0x08".
8229 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8230 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8233 * AND that with the checks done for data frames.
8238 * If the high-order bit of the type value is 0, this
8239 * is a management frame.
8240 * I.e, check "!(link[0] & 0x08)".
8242 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8243 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8246 * For management frames, the DA is at 4.
8248 b1
= gen_mac_multicast(cstate
, 4);
8252 * OR that with the checks done for data frames.
8253 * That gives the checks done for management and
8259 * If the low-order bit of the type value is 1,
8260 * this is either a control frame or a frame
8261 * with a reserved type, and thus not a
8264 * I.e., check "!(link[0] & 0x04)".
8266 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8267 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
8270 * AND that with the checks for data and management
8275 case DLT_IP_OVER_FC
:
8276 return gen_mac_multicast(cstate
, 2);
8280 fail_kw_on_dlt(cstate
, "multicast");
8284 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8285 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8290 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8291 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8295 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8301 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8302 * we can look at special meta-data in the filter expression; otherwise we
8303 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8304 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8305 * pcap_activate() conditionally sets.
8308 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8310 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8312 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8314 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8319 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8321 register struct block
*b0
;
8324 * Catch errors reported by us and routines below us, and return NULL
8327 if (setjmp(cstate
->top_ctx
))
8331 * Only some data link types support ifindex qualifiers.
8333 switch (cstate
->linktype
) {
8334 case DLT_LINUX_SLL2
:
8335 /* match packets on this interface */
8336 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8339 #if defined(__linux__)
8340 require_basic_bpf_extensions(cstate
, "ifindex");
8342 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8344 #else /* defined(__linux__) */
8345 fail_kw_on_dlt(cstate
, "ifindex");
8347 #endif /* defined(__linux__) */
8353 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8354 * Outbound traffic is sent by this machine, while inbound traffic is
8355 * sent by a remote machine (and may include packets destined for a
8356 * unicast or multicast link-layer address we are not subscribing to).
8357 * These are the same definitions implemented by pcap_setdirection().
8358 * Capturing only unicast traffic destined for this host is probably
8359 * better accomplished using a higher-layer filter.
8362 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8364 register struct block
*b0
;
8367 * Catch errors reported by us and routines below us, and return NULL
8370 if (setjmp(cstate
->top_ctx
))
8374 * Only some data link types support inbound/outbound qualifiers.
8376 switch (cstate
->linktype
) {
8378 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8379 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8383 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8384 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8388 /* match outgoing packets */
8389 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8391 /* to filter on inbound traffic, invert the match */
8396 case DLT_LINUX_SLL2
:
8397 /* match outgoing packets */
8398 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8400 /* to filter on inbound traffic, invert the match */
8406 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8407 outbound
? PF_OUT
: PF_IN
);
8411 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8414 case DLT_JUNIPER_MFR
:
8415 case DLT_JUNIPER_MLFR
:
8416 case DLT_JUNIPER_MLPPP
:
8417 case DLT_JUNIPER_ATM1
:
8418 case DLT_JUNIPER_ATM2
:
8419 case DLT_JUNIPER_PPPOE
:
8420 case DLT_JUNIPER_PPPOE_ATM
:
8421 case DLT_JUNIPER_GGSN
:
8422 case DLT_JUNIPER_ES
:
8423 case DLT_JUNIPER_MONITOR
:
8424 case DLT_JUNIPER_SERVICES
:
8425 case DLT_JUNIPER_ETHER
:
8426 case DLT_JUNIPER_PPP
:
8427 case DLT_JUNIPER_FRELAY
:
8428 case DLT_JUNIPER_CHDLC
:
8429 case DLT_JUNIPER_VP
:
8430 case DLT_JUNIPER_ST
:
8431 case DLT_JUNIPER_ISM
:
8432 case DLT_JUNIPER_VS
:
8433 case DLT_JUNIPER_SRX_E2E
:
8434 case DLT_JUNIPER_FIBRECHANNEL
:
8435 case DLT_JUNIPER_ATM_CEMIC
:
8436 /* juniper flags (including direction) are stored
8437 * the byte after the 3-byte magic number */
8438 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8443 * If we have packet meta-data indicating a direction,
8444 * and that metadata can be checked by BPF code, check
8445 * it. Otherwise, give up, as this link-layer type has
8446 * nothing in the packet data.
8448 * Currently, the only platform where a BPF filter can
8449 * check that metadata is Linux with the in-kernel
8450 * BPF interpreter. If other packet capture mechanisms
8451 * and BPF filters also supported this, it would be
8452 * nice. It would be even better if they made that
8453 * metadata available so that we could provide it
8454 * with newer capture APIs, allowing it to be saved
8457 #if defined(__linux__)
8458 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8459 /* match outgoing packets */
8460 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8463 /* to filter on inbound traffic, invert the match */
8466 #else /* defined(__linux__) */
8467 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8469 #endif /* defined(__linux__) */
8474 /* PF firewall log matched interface */
8476 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8482 * Catch errors reported by us and routines below us, and return NULL
8485 if (setjmp(cstate
->top_ctx
))
8488 assert_pflog(cstate
, "ifname");
8490 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8491 off
= offsetof(struct pfloghdr
, ifname
);
8492 if (strlen(ifname
) >= len
) {
8493 bpf_error(cstate
, "ifname interface names can only be %d characters",
8497 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8498 (const u_char
*)ifname
);
8502 /* PF firewall log ruleset name */
8504 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8509 * Catch errors reported by us and routines below us, and return NULL
8512 if (setjmp(cstate
->top_ctx
))
8515 assert_pflog(cstate
, "ruleset");
8517 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8518 bpf_error(cstate
, "ruleset names can only be %ld characters",
8519 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8523 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8524 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8528 /* PF firewall log rule number */
8530 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8535 * Catch errors reported by us and routines below us, and return NULL
8538 if (setjmp(cstate
->top_ctx
))
8541 assert_pflog(cstate
, "rnr");
8543 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8548 /* PF firewall log sub-rule number */
8550 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8555 * Catch errors reported by us and routines below us, and return NULL
8558 if (setjmp(cstate
->top_ctx
))
8561 assert_pflog(cstate
, "srnr");
8563 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8568 /* PF firewall log reason code */
8570 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8575 * Catch errors reported by us and routines below us, and return NULL
8578 if (setjmp(cstate
->top_ctx
))
8581 assert_pflog(cstate
, "reason");
8583 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8584 (bpf_u_int32
)reason
);
8588 /* PF firewall log action */
8590 gen_pf_action(compiler_state_t
*cstate
, int action
)
8595 * Catch errors reported by us and routines below us, and return NULL
8598 if (setjmp(cstate
->top_ctx
))
8601 assert_pflog(cstate
, "action");
8603 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8604 (bpf_u_int32
)action
);
8608 /* IEEE 802.11 wireless header */
8610 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8615 * Catch errors reported by us and routines below us, and return NULL
8618 if (setjmp(cstate
->top_ctx
))
8621 switch (cstate
->linktype
) {
8623 case DLT_IEEE802_11
:
8624 case DLT_PRISM_HEADER
:
8625 case DLT_IEEE802_11_RADIO_AVS
:
8626 case DLT_IEEE802_11_RADIO
:
8628 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8632 fail_kw_on_dlt(cstate
, "type/subtype");
8640 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8645 * Catch errors reported by us and routines below us, and return NULL
8648 if (setjmp(cstate
->top_ctx
))
8651 switch (cstate
->linktype
) {
8653 case DLT_IEEE802_11
:
8654 case DLT_PRISM_HEADER
:
8655 case DLT_IEEE802_11_RADIO_AVS
:
8656 case DLT_IEEE802_11_RADIO
:
8661 fail_kw_on_dlt(cstate
, "dir");
8665 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8666 IEEE80211_FC1_DIR_MASK
);
8671 // Process an ARCnet host address string.
8673 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8676 * Catch errors reported by us and routines below us, and return NULL
8679 if (setjmp(cstate
->top_ctx
))
8682 switch (cstate
->linktype
) {
8685 case DLT_ARCNET_LINUX
:
8686 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8687 q
.proto
== Q_LINK
) {
8690 * The lexer currently defines the address format in a
8691 * way that makes this error condition never true.
8692 * Let's check it anyway in case this part of the lexer
8693 * changes in future.
8695 if (! pcapint_atoan(s
, &addr
))
8696 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8697 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8699 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8703 bpf_error(cstate
, "aid supported only on ARCnet");
8708 // Compare an ARCnet host address with the given value.
8709 static struct block
*
8710 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8712 register struct block
*b0
, *b1
;
8716 * ARCnet is different from Ethernet: the source address comes before
8717 * the destination address, each is one byte long. This holds for all
8718 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8719 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8720 * by Datapoint (document number 61610-01).
8723 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8726 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8729 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8730 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8736 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8737 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8747 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8754 static struct block
*
8755 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8757 struct block
*b0
, *b1
;
8759 /* check for VLAN, including 802.1ad and QinQ */
8760 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8761 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8764 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8770 static struct block
*
8771 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8773 assert_maxval(cstate
, "VLAN tag", vlan_num
, 0x0fff);
8774 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8777 static struct block
*
8778 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8781 struct block
*b0
, *b1
;
8783 b0
= gen_vlan_tpid_test(cstate
);
8786 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8792 * Both payload and link header type follow the VLAN tags so that
8793 * both need to be updated.
8795 cstate
->off_linkpl
.constant_part
+= 4;
8796 cstate
->off_linktype
.constant_part
+= 4;
8801 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8802 /* add v to variable part of off */
8804 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8805 bpf_u_int32 v
, struct slist
*s
)
8809 if (!off
->is_variable
)
8810 off
->is_variable
= 1;
8812 off
->reg
= alloc_reg(cstate
);
8814 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8817 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8820 s2
= new_stmt(cstate
, BPF_ST
);
8826 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8827 * and link type offsets first
8830 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8834 /* offset determined at run time, shift variable part */
8836 cstate
->is_vlan_vloffset
= 1;
8837 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8838 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8840 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8841 sappend(s
.next
, b_tpid
->head
->stmts
);
8842 b_tpid
->head
->stmts
= s
.next
;
8846 * patch block b_vid (VLAN id test) to load VID value either from packet
8847 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8850 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8852 struct slist
*s
, *s2
, *sjeq
;
8855 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8856 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8858 /* true -> next instructions, false -> beginning of b_vid */
8859 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8861 sjeq
->s
.jf
= b_vid
->stmts
;
8864 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8865 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8869 /* Jump to the test in b_vid. We need to jump one instruction before
8870 * the end of the b_vid block so that we only skip loading the TCI
8871 * from packet data and not the 'and' instruction extracting VID.
8874 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8876 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8880 /* insert our statements at the beginning of b_vid */
8881 sappend(s
, b_vid
->stmts
);
8886 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8887 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8888 * tag can be either in metadata or in packet data; therefore if the
8889 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8890 * header for VLAN tag. As the decision is done at run time, we need
8891 * update variable part of the offsets
8893 static struct block
*
8894 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8897 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8900 /* generate new filter code based on extracting packet
8902 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8903 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8905 b0
= gen_jmp(cstate
, BPF_JEQ
, 1, s
);
8908 * This is tricky. We need to insert the statements updating variable
8909 * parts of offsets before the traditional TPID and VID tests so
8910 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8911 * we do not want this update to affect those checks. That's why we
8912 * generate both test blocks first and insert the statements updating
8913 * variable parts of both offsets after that. This wouldn't work if
8914 * there already were variable length link header when entering this
8915 * function but gen_vlan_bpf_extensions() isn't called in that case.
8917 b_tpid
= gen_vlan_tpid_test(cstate
);
8919 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8921 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8926 gen_vlan_patch_vid_test(cstate
, b_vid
);
8936 * support IEEE 802.1Q VLAN trunk over ethernet
8939 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8944 * Catch errors reported by us and routines below us, and return NULL
8947 if (setjmp(cstate
->top_ctx
))
8950 /* can't check for VLAN-encapsulated packets inside MPLS */
8951 if (cstate
->label_stack_depth
> 0)
8952 bpf_error(cstate
, "no VLAN match after MPLS");
8955 * Check for a VLAN packet, and then change the offsets to point
8956 * to the type and data fields within the VLAN packet. Just
8957 * increment the offsets, so that we can support a hierarchy, e.g.
8958 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
8961 * XXX - this is a bit of a kludge. If we were to split the
8962 * compiler into a parser that parses an expression and
8963 * generates an expression tree, and a code generator that
8964 * takes an expression tree (which could come from our
8965 * parser or from some other parser) and generates BPF code,
8966 * we could perhaps make the offsets parameters of routines
8967 * and, in the handler for an "AND" node, pass to subnodes
8968 * other than the VLAN node the adjusted offsets.
8970 * This would mean that "vlan" would, instead of changing the
8971 * behavior of *all* tests after it, change only the behavior
8972 * of tests ANDed with it. That would change the documented
8973 * semantics of "vlan", which might break some expressions.
8974 * However, it would mean that "(vlan and ip) or ip" would check
8975 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8976 * checking only for VLAN-encapsulated IP, so that could still
8977 * be considered worth doing; it wouldn't break expressions
8978 * that are of the form "vlan and ..." or "vlan N and ...",
8979 * which I suspect are the most common expressions involving
8980 * "vlan". "vlan or ..." doesn't necessarily do what the user
8981 * would really want, now, as all the "or ..." tests would
8982 * be done assuming a VLAN, even though the "or" could be viewed
8983 * as meaning "or, if this isn't a VLAN packet...".
8985 switch (cstate
->linktype
) {
8989 * Newer version of the Linux kernel pass around
8990 * packets in which the VLAN tag has been removed
8991 * from the packet data and put into metadata.
8993 * This requires special treatment.
8995 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8996 /* Verify that this is the outer part of the packet and
8997 * not encapsulated somehow. */
8998 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8999 cstate
->off_linkhdr
.constant_part
==
9000 cstate
->off_outermostlinkhdr
.constant_part
) {
9002 * Do we need special VLAN handling?
9004 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9005 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9008 b0
= gen_vlan_no_bpf_extensions(cstate
,
9009 vlan_num
, has_vlan_tag
);
9012 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9016 case DLT_NETANALYZER
:
9017 case DLT_NETANALYZER_TRANSPARENT
:
9018 case DLT_IEEE802_11
:
9019 case DLT_PRISM_HEADER
:
9020 case DLT_IEEE802_11_RADIO_AVS
:
9021 case DLT_IEEE802_11_RADIO
:
9023 * These are either Ethernet packets with an additional
9024 * metadata header (the NetAnalyzer types), or 802.11
9025 * packets, possibly with an additional metadata header.
9027 * For the first of those, the VLAN tag is in the normal
9028 * place, so the special-case handling above isn't
9031 * For the second of those, we don't do the special-case
9034 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9038 bpf_error(cstate
, "no VLAN support for %s",
9039 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9043 cstate
->vlan_stack_depth
++;
9051 * The label_num_arg dance is to avoid annoying whining by compilers that
9052 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9053 * It's not *used* after setjmp returns.
9055 static struct block
*
9056 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9059 struct block
*b0
, *b1
;
9061 if (cstate
->label_stack_depth
> 0) {
9062 /* just match the bottom-of-stack bit clear */
9063 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9066 * We're not in an MPLS stack yet, so check the link-layer
9067 * type against MPLS.
9069 switch (cstate
->linktype
) {
9071 case DLT_C_HDLC
: /* fall through */
9074 case DLT_NETANALYZER
:
9075 case DLT_NETANALYZER_TRANSPARENT
:
9076 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9080 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9083 /* FIXME add other DLT_s ...
9084 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9085 * leave it for now */
9088 bpf_error(cstate
, "no MPLS support for %s",
9089 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9094 /* If a specific MPLS label is requested, check it */
9095 if (has_label_num
) {
9096 assert_maxval(cstate
, "MPLS label", label_num
, 0xFFFFF);
9097 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9098 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9099 0xfffff000); /* only compare the first 20 bits */
9105 * Change the offsets to point to the type and data fields within
9106 * the MPLS packet. Just increment the offsets, so that we
9107 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9108 * capture packets with an outer label of 100000 and an inner
9111 * Increment the MPLS stack depth as well; this indicates that
9112 * we're checking MPLS-encapsulated headers, to make sure higher
9113 * level code generators don't try to match against IP-related
9114 * protocols such as Q_ARP, Q_RARP etc.
9116 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9118 cstate
->off_nl_nosnap
+= 4;
9119 cstate
->off_nl
+= 4;
9120 cstate
->label_stack_depth
++;
9125 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9128 * Catch errors reported by us and routines below us, and return NULL
9131 if (setjmp(cstate
->top_ctx
))
9134 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9138 * Support PPPOE discovery and session.
9141 gen_pppoed(compiler_state_t
*cstate
)
9144 * Catch errors reported by us and routines below us, and return NULL
9147 if (setjmp(cstate
->top_ctx
))
9150 /* check for PPPoE discovery */
9151 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9155 * RFC 2516 Section 4:
9157 * The Ethernet payload for PPPoE is as follows:
9160 * 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
9161 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9162 * | VER | TYPE | CODE | SESSION_ID |
9163 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9164 * | LENGTH | payload ~
9165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9168 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9170 struct block
*b0
, *b1
;
9173 * Catch errors reported by us and routines below us, and return NULL
9176 if (setjmp(cstate
->top_ctx
))
9180 * Test against the PPPoE session link-layer type.
9182 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9184 /* If a specific session is requested, check PPPoE session id */
9186 assert_maxval(cstate
, "PPPoE session number", sess_num
, UINT16_MAX
);
9187 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9193 * Change the offsets to point to the type and data fields within
9194 * the PPP packet, and note that this is PPPoE rather than
9197 * XXX - this is a bit of a kludge. See the comments in
9200 * The "network-layer" protocol is PPPoE, which has a 6-byte
9201 * PPPoE header, followed by a PPP packet.
9203 * There is no HDLC encapsulation for the PPP packet (it's
9204 * encapsulated in PPPoES instead), so the link-layer type
9205 * starts at the first byte of the PPP packet. For PPPoE,
9206 * that offset is relative to the beginning of the total
9207 * link-layer payload, including any 802.2 LLC header, so
9208 * it's 6 bytes past cstate->off_nl.
9210 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9211 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9212 cstate
->off_linkpl
.reg
);
9214 cstate
->off_linktype
= cstate
->off_linkhdr
;
9215 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9218 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9223 /* Check that this is Geneve and the VNI is correct if
9224 * specified. Parameterized to handle both IPv4 and IPv6. */
9225 static struct block
*
9226 gen_geneve_check(compiler_state_t
*cstate
,
9227 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
9228 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9230 struct block
*b0
, *b1
;
9232 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9234 /* Check that we are operating on version 0. Otherwise, we
9235 * can't decode the rest of the fields. The version is 2 bits
9236 * in the first byte of the Geneve header. */
9237 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9242 assert_maxval(cstate
, "Geneve VNI", vni
, 0xffffff);
9243 vni
<<= 8; /* VNI is in the upper 3 bytes */
9244 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9252 /* The IPv4 and IPv6 Geneve checks need to do two things:
9253 * - Verify that this actually is Geneve with the right VNI.
9254 * - Place the IP header length (plus variable link prefix if
9255 * needed) into register A to be used later to compute
9256 * the inner packet offsets. */
9257 static struct block
*
9258 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9260 struct block
*b0
, *b1
;
9261 struct slist
*s
, *s1
;
9263 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9265 /* Load the IP header length into A. */
9266 s
= gen_loadx_iphdrlen(cstate
);
9268 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9271 /* Forcibly append these statements to the true condition
9272 * of the protocol check by creating a new block that is
9273 * always true and ANDing them. */
9274 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9281 static struct block
*
9282 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9284 struct block
*b0
, *b1
;
9285 struct slist
*s
, *s1
;
9287 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9289 /* Load the IP header length. We need to account for a
9290 * variable length link prefix if there is one. */
9291 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9293 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9297 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9301 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9305 /* Forcibly append these statements to the true condition
9306 * of the protocol check by creating a new block that is
9307 * always true and ANDing them. */
9308 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9311 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9318 /* We need to store three values based on the Geneve header::
9319 * - The offset of the linktype.
9320 * - The offset of the end of the Geneve header.
9321 * - The offset of the end of the encapsulated MAC header. */
9322 static struct slist
*
9323 gen_geneve_offsets(compiler_state_t
*cstate
)
9325 struct slist
*s
, *s1
, *s_proto
;
9327 /* First we need to calculate the offset of the Geneve header
9328 * itself. This is composed of the IP header previously calculated
9329 * (include any variable link prefix) and stored in A plus the
9330 * fixed sized headers (fixed link prefix, MAC length, and UDP
9332 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9333 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9335 /* Stash this in X since we'll need it later. */
9336 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9339 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9341 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9345 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9346 cstate
->off_linktype
.is_variable
= 1;
9347 cstate
->off_linktype
.constant_part
= 0;
9349 s1
= new_stmt(cstate
, BPF_ST
);
9350 s1
->s
.k
= cstate
->off_linktype
.reg
;
9353 /* Load the Geneve option length and mask and shift to get the
9354 * number of bytes. It is stored in the first byte of the Geneve
9356 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9360 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9364 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9368 /* Add in the rest of the Geneve base header. */
9369 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9373 /* Add the Geneve header length to its offset and store. */
9374 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9378 /* Set the encapsulated type as Ethernet. Even though we may
9379 * not actually have Ethernet inside there are two reasons this
9381 * - The linktype field is always in EtherType format regardless
9382 * of whether it is in Geneve or an inner Ethernet frame.
9383 * - The only link layer that we have specific support for is
9384 * Ethernet. We will confirm that the packet actually is
9385 * Ethernet at runtime before executing these checks. */
9386 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9388 s1
= new_stmt(cstate
, BPF_ST
);
9389 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9392 /* Calculate whether we have an Ethernet header or just raw IP/
9393 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9394 * and linktype by 14 bytes so that the network header can be found
9395 * seamlessly. Otherwise, keep what we've calculated already. */
9397 /* We have a bare jmp so we can't use the optimizer. */
9398 cstate
->no_optimize
= 1;
9400 /* Load the EtherType in the Geneve header, 2 bytes in. */
9401 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9405 /* Load X with the end of the Geneve header. */
9406 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9407 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9410 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9411 * end of this check, we should have the total length in X. In
9412 * the non-Ethernet case, it's already there. */
9413 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9414 s_proto
->s
.k
= ETHERTYPE_TEB
;
9415 sappend(s
, s_proto
);
9417 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9421 /* Since this is Ethernet, use the EtherType of the payload
9422 * directly as the linktype. Overwrite what we already have. */
9423 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9427 s1
= new_stmt(cstate
, BPF_ST
);
9428 s1
->s
.k
= cstate
->off_linktype
.reg
;
9431 /* Advance two bytes further to get the end of the Ethernet
9433 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9437 /* Move the result to X. */
9438 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9441 /* Store the final result of our linkpl calculation. */
9442 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9443 cstate
->off_linkpl
.is_variable
= 1;
9444 cstate
->off_linkpl
.constant_part
= 0;
9446 s1
= new_stmt(cstate
, BPF_STX
);
9447 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9456 /* Check to see if this is a Geneve packet. */
9458 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9460 struct block
*b0
, *b1
;
9464 * Catch errors reported by us and routines below us, and return NULL
9467 if (setjmp(cstate
->top_ctx
))
9470 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9471 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9476 /* Later filters should act on the payload of the Geneve frame,
9477 * update all of the header pointers. Attach this code so that
9478 * it gets executed in the event that the Geneve filter matches. */
9479 s
= gen_geneve_offsets(cstate
);
9481 b1
= gen_true(cstate
);
9482 sappend(s
, b1
->stmts
);
9487 cstate
->is_encap
= 1;
9492 /* Check that this is VXLAN and the VNI is correct if
9493 * specified. Parameterized to handle both IPv4 and IPv6. */
9494 static struct block
*
9495 gen_vxlan_check(compiler_state_t
*cstate
,
9496 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
9497 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9499 struct block
*b0
, *b1
;
9501 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9503 /* Check that the VXLAN header has the flag bits set
9505 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9510 assert_maxval(cstate
, "VXLAN VNI", vni
, 0xffffff);
9511 vni
<<= 8; /* VNI is in the upper 3 bytes */
9512 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9520 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9521 * - Verify that this actually is VXLAN with the right VNI.
9522 * - Place the IP header length (plus variable link prefix if
9523 * needed) into register A to be used later to compute
9524 * the inner packet offsets. */
9525 static struct block
*
9526 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9528 struct block
*b0
, *b1
;
9529 struct slist
*s
, *s1
;
9531 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9533 /* Load the IP header length into A. */
9534 s
= gen_loadx_iphdrlen(cstate
);
9536 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9539 /* Forcibly append these statements to the true condition
9540 * of the protocol check by creating a new block that is
9541 * always true and ANDing them. */
9542 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9549 static struct block
*
9550 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9552 struct block
*b0
, *b1
;
9553 struct slist
*s
, *s1
;
9555 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9557 /* Load the IP header length. We need to account for a
9558 * variable length link prefix if there is one. */
9559 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9561 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9565 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9569 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9573 /* Forcibly append these statements to the true condition
9574 * of the protocol check by creating a new block that is
9575 * always true and ANDing them. */
9576 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9579 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9586 /* We need to store three values based on the VXLAN header:
9587 * - The offset of the linktype.
9588 * - The offset of the end of the VXLAN header.
9589 * - The offset of the end of the encapsulated MAC header. */
9590 static struct slist
*
9591 gen_vxlan_offsets(compiler_state_t
*cstate
)
9593 struct slist
*s
, *s1
;
9595 /* Calculate the offset of the VXLAN header itself. This
9596 * includes the IP header computed previously (including any
9597 * variable link prefix) and stored in A plus the fixed size
9598 * headers (fixed link prefix, MAC length, UDP header). */
9599 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9600 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9602 /* Add the VXLAN header length to its offset and store */
9603 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9607 /* Push the link header. VXLAN packets always contain Ethernet
9609 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9611 s1
= new_stmt(cstate
, BPF_ST
);
9612 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9615 /* As the payload is an Ethernet packet, we can use the
9616 * EtherType of the payload directly as the linktype. */
9617 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9621 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9622 cstate
->off_linktype
.is_variable
= 1;
9623 cstate
->off_linktype
.constant_part
= 0;
9625 s1
= new_stmt(cstate
, BPF_ST
);
9626 s1
->s
.k
= cstate
->off_linktype
.reg
;
9629 /* Two bytes further is the end of the Ethernet header and the
9630 * start of the payload. */
9631 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9635 /* Move the result to X. */
9636 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9639 /* Store the final result of our linkpl calculation. */
9640 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9641 cstate
->off_linkpl
.is_variable
= 1;
9642 cstate
->off_linkpl
.constant_part
= 0;
9644 s1
= new_stmt(cstate
, BPF_STX
);
9645 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9653 /* Check to see if this is a VXLAN packet. */
9655 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9657 struct block
*b0
, *b1
;
9661 * Catch errors reported by us and routines below us, and return NULL
9664 if (setjmp(cstate
->top_ctx
))
9667 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9668 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9673 /* Later filters should act on the payload of the VXLAN frame,
9674 * update all of the header pointers. Attach this code so that
9675 * it gets executed in the event that the VXLAN filter matches. */
9676 s
= gen_vxlan_offsets(cstate
);
9678 b1
= gen_true(cstate
);
9679 sappend(s
, b1
->stmts
);
9684 cstate
->is_encap
= 1;
9689 /* Check that the encapsulated frame has a link layer header
9690 * for Ethernet filters. */
9691 static struct block
*
9692 gen_encap_ll_check(compiler_state_t
*cstate
)
9695 struct slist
*s
, *s1
;
9697 /* The easiest way to see if there is a link layer present
9698 * is to check if the link layer header and payload are not
9701 /* Geneve always generates pure variable offsets so we can
9702 * compare only the registers. */
9703 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9704 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9706 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9707 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9710 b0
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9716 static struct block
*
9717 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9718 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9722 assert_atm(cstate
, atmkw(atmfield
));
9727 assert_maxval(cstate
, "VPI", jvalue
, UINT8_MAX
);
9728 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9729 0xffffffffU
, jtype
, reverse
, jvalue
);
9733 assert_maxval(cstate
, "VCI", jvalue
, UINT16_MAX
);
9734 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9735 0xffffffffU
, jtype
, reverse
, jvalue
);
9744 static struct block
*
9745 gen_atmtype_metac(compiler_state_t
*cstate
)
9747 struct block
*b0
, *b1
;
9749 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9750 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9755 static struct block
*
9756 gen_atmtype_sc(compiler_state_t
*cstate
)
9758 struct block
*b0
, *b1
;
9760 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9761 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9766 static struct block
*
9767 gen_atm_prototype(compiler_state_t
*cstate
, const uint8_t v
)
9769 return gen_mcmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, v
, 0x0fU
);
9772 static struct block
*
9773 gen_atmtype_llc(compiler_state_t
*cstate
)
9777 b0
= gen_atm_prototype(cstate
, PT_LLC
);
9778 cstate
->linktype
= cstate
->prevlinktype
;
9783 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9784 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9787 * Catch errors reported by us and routines below us, and return NULL
9790 if (setjmp(cstate
->top_ctx
))
9793 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9798 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9800 struct block
*b0
, *b1
;
9803 * Catch errors reported by us and routines below us, and return NULL
9806 if (setjmp(cstate
->top_ctx
))
9809 assert_atm(cstate
, atmkw(type
));
9814 /* Get all packets in Meta signalling Circuit */
9815 b1
= gen_atmtype_metac(cstate
);
9819 /* Get all packets in Broadcast Circuit*/
9820 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9821 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9826 /* Get all cells in Segment OAM F4 circuit*/
9827 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9828 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9833 /* Get all cells in End-to-End OAM F4 Circuit*/
9834 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9835 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9840 /* Get all packets in connection Signalling Circuit */
9841 b1
= gen_atmtype_sc(cstate
);
9845 /* Get all packets in ILMI Circuit */
9846 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9847 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9852 /* Get all LANE packets */
9853 b1
= gen_atm_prototype(cstate
, PT_LANE
);
9856 * Arrange that all subsequent tests assume LANE
9857 * rather than LLC-encapsulated packets, and set
9858 * the offsets appropriately for LANE-encapsulated
9861 * We assume LANE means Ethernet, not Token Ring.
9863 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9864 cstate
->off_payload
+ 2, /* Ethernet header */
9866 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9867 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9868 cstate
->off_nl
= 0; /* Ethernet II */
9869 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9879 * Filtering for MTP2 messages based on li value
9880 * FISU, length is null
9881 * LSSU, length is 1 or 2
9882 * MSU, length is 3 or more
9883 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9886 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9888 struct block
*b0
, *b1
;
9891 * Catch errors reported by us and routines below us, and return NULL
9894 if (setjmp(cstate
->top_ctx
))
9897 assert_ss7(cstate
, ss7kw(type
));
9902 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9903 0x3fU
, BPF_JEQ
, 0, 0U);
9907 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9908 0x3fU
, BPF_JGT
, 1, 2U);
9909 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9910 0x3fU
, BPF_JGT
, 0, 0U);
9915 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9916 0x3fU
, BPF_JGT
, 0, 2U);
9920 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9921 0xff80U
, BPF_JEQ
, 0, 0U);
9925 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9926 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9927 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9928 0xff80U
, BPF_JGT
, 0, 0U);
9933 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9934 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9944 * These maximum valid values are all-ones, so they double as the bitmasks
9945 * before any bitwise shifting.
9947 #define MTP2_SIO_MAXVAL UINT8_MAX
9948 #define MTP3_PC_MAXVAL 0x3fffU
9949 #define MTP3_SLS_MAXVAL 0xfU
9951 static struct block
*
9952 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
9953 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9961 newoff_sio
= cstate
->off_sio
;
9962 newoff_opc
= cstate
->off_opc
;
9963 newoff_dpc
= cstate
->off_dpc
;
9964 newoff_sls
= cstate
->off_sls
;
9966 assert_ss7(cstate
, ss7kw(mtp3field
));
9968 switch (mtp3field
) {
9971 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
9973 * SIO is the simplest field: the size is one byte and the offset is a
9974 * multiple of bytes, so the only detail to get right is the value of
9975 * the [right-to-left] field offset.
9978 newoff_sio
+= 3; /* offset for MTP2_HSL */
9982 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP2_SIO_MAXVAL
);
9983 // Here the bitmask means "do not apply a bitmask".
9984 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
9985 jtype
, reverse
, jvalue
);
9989 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
9991 * SLS, OPC and DPC are more complicated: none of these is sized in a
9992 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
9993 * diagrams are meant to be read right-to-left. This means in the
9994 * diagrams within individual fields and concatenations thereof
9995 * bitwise shifts and masks can be noted in the common left-to-right
9996 * manner until each final value is ready to be byte-swapped and
9997 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
9998 * similar problem in a similar way.
10000 * Offsets of fields within the packet header always have the
10001 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10002 * DLTs the offset does not include the F (Flag) field at the
10003 * beginning of each message.
10005 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10006 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10007 * be tested entirely using a single BPF_W comparison. In this case
10008 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10009 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10010 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10011 * correlates with the [RTL] packet diagram until the byte-swapping is
10014 * The code below uses this approach for OPC, which spans 3 bytes.
10015 * DPC and SLS use shorter loads, SLS also uses a different offset.
10022 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
10023 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10024 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10025 SWAPLONG(jvalue
<< 14));
10033 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
10034 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10035 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10036 SWAPSHORT(jvalue
));
10044 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_SLS_MAXVAL
);
10045 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10046 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10057 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10058 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10061 * Catch errors reported by us and routines below us, and return NULL
10064 if (setjmp(cstate
->top_ctx
))
10067 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10071 static struct block
*
10072 gen_msg_abbrev(compiler_state_t
*cstate
, const uint8_t type
)
10075 * Q.2931 signalling protocol messages for handling virtual circuits
10076 * establishment and teardown
10078 return gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
,
10083 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10085 struct block
*b0
, *b1
;
10088 * Catch errors reported by us and routines below us, and return NULL
10091 if (setjmp(cstate
->top_ctx
))
10094 assert_atm(cstate
, atmkw(type
));
10100 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10101 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10103 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10109 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10110 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10112 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10118 * Get Q.2931 signalling messages for switched
10119 * virtual connection
10121 b0
= gen_msg_abbrev(cstate
, SETUP
);
10122 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
10124 b0
= gen_msg_abbrev(cstate
, CONNECT
);
10126 b0
= gen_msg_abbrev(cstate
, CONNECT_ACK
);
10128 b0
= gen_msg_abbrev(cstate
, RELEASE
);
10130 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
10132 b0
= gen_atmtype_sc(cstate
);
10136 case A_METACONNECT
:
10137 b0
= gen_msg_abbrev(cstate
, SETUP
);
10138 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
10140 b0
= gen_msg_abbrev(cstate
, CONNECT
);
10142 b0
= gen_msg_abbrev(cstate
, RELEASE
);
10144 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
10146 b0
= gen_atmtype_metac(cstate
);