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_mac48hostop(compiler_state_t
*, const u_char
*,
683 const int, const u_int
, const u_int
);
684 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
685 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
686 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
687 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
688 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
689 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
690 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
691 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
694 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
695 struct in6_addr
*, int, int, int);
698 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
699 struct addrinfo
*, int);
701 static struct block
*gen_ip_proto(compiler_state_t
*, const uint8_t);
702 static struct block
*gen_ip6_proto(compiler_state_t
*, const uint8_t);
703 static struct block
*gen_ipfrag(compiler_state_t
*);
704 static struct block
*gen_portatom(compiler_state_t
*, int, uint16_t);
705 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, uint16_t,
707 static struct block
*gen_portatom6(compiler_state_t
*, int, uint16_t);
708 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, uint16_t,
710 static struct block
*gen_port(compiler_state_t
*, uint16_t, int, int);
711 static struct block
*gen_port_common(compiler_state_t
*, int, struct block
*);
712 static struct block
*gen_portrange(compiler_state_t
*, uint16_t, uint16_t,
714 static struct block
*gen_port6(compiler_state_t
*, uint16_t, int, int);
715 static struct block
*gen_port6_common(compiler_state_t
*, int, struct block
*);
716 static struct block
*gen_portrange6(compiler_state_t
*, uint16_t, uint16_t,
718 static int lookup_proto(compiler_state_t
*, const char *, int);
719 #if !defined(NO_PROTOCHAIN)
720 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
721 #endif /* !defined(NO_PROTOCHAIN) */
722 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int);
723 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
724 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
725 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
726 static struct block
*gen_len(compiler_state_t
*, int, int);
727 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
729 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
730 bpf_u_int32
, int, int);
731 static struct block
*gen_atmtype_llc(compiler_state_t
*);
732 static struct block
*gen_msg_abbrev(compiler_state_t
*, const uint8_t);
733 static struct block
*gen_atm_prototype(compiler_state_t
*, const uint8_t);
734 static struct block
*gen_atm_vpi(compiler_state_t
*, const uint8_t);
735 static struct block
*gen_atm_vci(compiler_state_t
*, const uint16_t);
738 initchunks(compiler_state_t
*cstate
)
742 for (i
= 0; i
< NCHUNKS
; i
++) {
743 cstate
->chunks
[i
].n_left
= 0;
744 cstate
->chunks
[i
].m
= NULL
;
746 cstate
->cur_chunk
= 0;
750 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
756 /* Round up to chunk alignment. */
757 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
759 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
760 if (n
> cp
->n_left
) {
762 k
= ++cstate
->cur_chunk
;
764 bpf_set_error(cstate
, "out of memory");
767 size
= CHUNK0SIZE
<< k
;
768 cp
->m
= (void *)malloc(size
);
770 bpf_set_error(cstate
, "out of memory");
773 memset((char *)cp
->m
, 0, size
);
776 bpf_set_error(cstate
, "out of memory");
781 return (void *)((char *)cp
->m
+ cp
->n_left
);
785 newchunk(compiler_state_t
*cstate
, size_t n
)
789 p
= newchunk_nolongjmp(cstate
, n
);
791 longjmp(cstate
->top_ctx
, 1);
798 freechunks(compiler_state_t
*cstate
)
802 for (i
= 0; i
< NCHUNKS
; ++i
)
803 if (cstate
->chunks
[i
].m
!= NULL
)
804 free(cstate
->chunks
[i
].m
);
808 * A strdup whose allocations are freed after code generation is over.
809 * This is used by the lexical analyzer, so it can't longjmp; it just
810 * returns NULL on an allocation error, and the callers must check
814 sdup(compiler_state_t
*cstate
, const char *s
)
816 size_t n
= strlen(s
) + 1;
817 char *cp
= newchunk_nolongjmp(cstate
, n
);
821 pcapint_strlcpy(cp
, s
, n
);
825 static inline struct block
*
826 new_block(compiler_state_t
*cstate
, int code
)
830 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
837 static inline struct slist
*
838 new_stmt(compiler_state_t
*cstate
, int code
)
842 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
848 static struct block
*
849 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
851 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
857 static struct block
*
858 gen_retblk(compiler_state_t
*cstate
, int v
)
860 if (setjmp(cstate
->top_ctx
)) {
862 * gen_retblk() only fails because a memory
863 * allocation failed in newchunk(), meaning
864 * that it can't return a pointer.
870 return gen_retblk_internal(cstate
, v
);
873 static inline PCAP_NORETURN_DEF
void
874 syntax(compiler_state_t
*cstate
)
876 bpf_error(cstate
, "syntax error in filter expression");
880 * For the given integer return a string with the keyword (or the nominal
881 * keyword if there is more than one). This is a simpler version of tok2str()
882 * in tcpdump because in this problem space a valid integer value is not
886 qual2kw(const char *kind
, const unsigned id
, const char *tokens
[],
889 static char buf
[4][64];
892 if (id
< size
&& tokens
[id
])
895 char *ret
= buf
[idx
];
896 idx
= (idx
+ 1) % (sizeof(buf
) / sizeof(buf
[0]));
897 ret
[0] = '\0'; // just in case
898 snprintf(ret
, sizeof(buf
[0]), "<invalid %s %u>", kind
, id
);
902 // protocol qualifier keywords
904 pqkw(const unsigned id
)
906 const char * tokens
[] = {
918 [Q_DECNET
] = "decnet",
924 [Q_ICMPV6
] = "icmp6",
936 [Q_NETBEUI
] = "netbeui",
939 [Q_ISIS_IIH
] = "iih",
940 [Q_ISIS_SNP
] = "snp",
941 [Q_ISIS_CSNP
] = "csnp",
942 [Q_ISIS_PSNP
] = "psnp",
943 [Q_ISIS_LSP
] = "lsp",
947 return qual2kw("proto", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
950 // direction qualifier keywords
952 dqkw(const unsigned id
)
954 const char * map
[] = {
957 [Q_OR
] = "src or dst",
958 [Q_AND
] = "src and dst",
966 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
971 atmkw(const unsigned id
)
973 const char * tokens
[] = {
976 [A_OAMF4SC
] = "oamf4sc",
977 [A_OAMF4EC
] = "oamf4ec",
985 [A_CONNECTMSG
] = "connectmsg",
986 [A_METACONNECT
] = "metaconnect",
988 return qual2kw("ATM keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
993 ss7kw(const unsigned id
)
995 const char * tokens
[] = {
1000 [MH_LSSU
] = "hlssu",
1011 return qual2kw("MTP keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
1014 static PCAP_NORETURN_DEF
void
1015 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
1017 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
1018 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
1022 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
1024 if (cstate
->linktype
!= DLT_PFLOG
)
1025 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
1029 assert_atm(compiler_state_t
*cstate
, const char *kw
)
1032 * Belt and braces: init_linktype() sets either all of these struct
1033 * members (for DLT_SUNATM) or none (otherwise).
1035 if (cstate
->linktype
!= DLT_SUNATM
||
1037 cstate
->off_vpi
== OFFSET_NOT_SET
||
1038 cstate
->off_vci
== OFFSET_NOT_SET
||
1039 cstate
->off_proto
== OFFSET_NOT_SET
||
1040 cstate
->off_payload
== OFFSET_NOT_SET
)
1041 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1045 assert_ss7(compiler_state_t
*cstate
, const char *kw
)
1047 switch (cstate
->linktype
) {
1050 case DLT_MTP2_WITH_PHDR
:
1051 // Belt and braces, same as in assert_atm().
1052 if (cstate
->off_sio
!= OFFSET_NOT_SET
&&
1053 cstate
->off_opc
!= OFFSET_NOT_SET
&&
1054 cstate
->off_dpc
!= OFFSET_NOT_SET
&&
1055 cstate
->off_sls
!= OFFSET_NOT_SET
)
1058 bpf_error(cstate
, "'%s' supported only on SS7", kw
);
1062 assert_maxval(compiler_state_t
*cstate
, const char *name
,
1063 const bpf_u_int32 val
, const bpf_u_int32 maxval
)
1066 bpf_error(cstate
, "%s %u greater than maximum %u",
1070 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1071 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1073 // Validate a port/portrange proto qualifier and map to an IP protocol number.
1075 port_pq_to_ipproto(compiler_state_t
*cstate
, const int proto
, const char *kw
)
1083 return IPPROTO_SCTP
;
1087 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), kw
);
1091 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1092 const char *buf
, int optimize
, bpf_u_int32 mask
)
1098 compiler_state_t cstate
;
1099 yyscan_t scanner
= NULL
;
1100 YY_BUFFER_STATE in_buffer
= NULL
;
1105 * If this pcap_t hasn't been activated, it doesn't have a
1106 * link-layer type, so we can't use it.
1108 if (!p
->activated
) {
1109 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1110 "not-yet-activated pcap_t passed to pcap_compile");
1111 return (PCAP_ERROR
);
1116 * Initialize Winsock, asking for the latest version (2.2),
1117 * as we may be calling Winsock routines to translate
1118 * host names to addresses.
1120 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1122 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1123 err
, "Error calling WSAStartup()");
1124 return (PCAP_ERROR
);
1128 #ifdef ENABLE_REMOTE
1130 * If the device on which we're capturing need to be notified
1131 * that a new filter is being compiled, do so.
1133 * This allows them to save a copy of it, in case, for example,
1134 * they're implementing a form of remote packet capture, and
1135 * want the remote machine to filter out the packets in which
1136 * it's sending the packets it's captured.
1138 * XXX - the fact that we happen to be compiling a filter
1139 * doesn't necessarily mean we'll be installing it as the
1140 * filter for this pcap_t; we might be running it from userland
1141 * on captured packets to do packet classification. We really
1142 * need a better way of handling this, but this is all that
1143 * the WinPcap remote capture code did.
1145 if (p
->save_current_filter_op
!= NULL
)
1146 (p
->save_current_filter_op
)(p
, buf
);
1149 initchunks(&cstate
);
1150 cstate
.no_optimize
= 0;
1155 cstate
.ic
.root
= NULL
;
1156 cstate
.ic
.cur_mark
= 0;
1157 cstate
.bpf_pcap
= p
;
1158 cstate
.error_set
= 0;
1161 cstate
.netmask
= mask
;
1163 cstate
.snaplen
= pcap_snapshot(p
);
1164 if (cstate
.snaplen
== 0) {
1165 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1166 "snaplen of 0 rejects all packets");
1171 if (pcap_lex_init(&scanner
) != 0) {
1172 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1173 errno
, "can't initialize scanner");
1177 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1180 * Associate the compiler state with the lexical analyzer
1183 pcap_set_extra(&cstate
, scanner
);
1185 if (init_linktype(&cstate
, p
) == -1) {
1189 if (pcap_parse(scanner
, &cstate
) != 0) {
1191 if (cstate
.ai
!= NULL
)
1192 freeaddrinfo(cstate
.ai
);
1194 if (cstate
.e
!= NULL
)
1200 if (cstate
.ic
.root
== NULL
) {
1201 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1204 * Catch errors reported by gen_retblk().
1206 if (cstate
.ic
.root
== NULL
) {
1212 if (optimize
&& !cstate
.no_optimize
) {
1213 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1218 if (cstate
.ic
.root
== NULL
||
1219 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1220 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1221 "expression rejects all packets");
1226 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1227 cstate
.ic
.root
, &len
, p
->errbuf
);
1228 if (program
->bf_insns
== NULL
) {
1233 program
->bf_len
= len
;
1235 rc
= 0; /* We're all okay */
1239 * Clean up everything for the lexical analyzer.
1241 if (in_buffer
!= NULL
)
1242 pcap__delete_buffer(in_buffer
, scanner
);
1243 if (scanner
!= NULL
)
1244 pcap_lex_destroy(scanner
);
1247 * Clean up our own allocated memory.
1249 freechunks(&cstate
);
1259 * entry point for using the compiler with no pcap open
1260 * pass in all the stuff that is needed explicitly instead.
1263 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1264 struct bpf_program
*program
,
1265 const char *buf
, int optimize
, bpf_u_int32 mask
)
1270 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1272 return (PCAP_ERROR
);
1273 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1279 * Clean up a "struct bpf_program" by freeing all the memory allocated
1283 pcap_freecode(struct bpf_program
*program
)
1285 program
->bf_len
= 0;
1286 if (program
->bf_insns
!= NULL
) {
1287 free((char *)program
->bf_insns
);
1288 program
->bf_insns
= NULL
;
1293 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1294 * which of the jt and jf fields has been resolved and which is a pointer
1295 * back to another unresolved block (or nil). At least one of the fields
1296 * in each block is already resolved.
1299 backpatch(struct block
*list
, struct block
*target
)
1316 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1317 * which of jt and jf is the link.
1320 merge(struct block
*b0
, struct block
*b1
)
1322 register struct block
**p
= &b0
;
1324 /* Find end of list. */
1326 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1328 /* Concatenate the lists. */
1333 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1336 * Catch errors reported by us and routines below us, and return -1
1339 if (setjmp(cstate
->top_ctx
))
1343 * Insert before the statements of the first (root) block any
1344 * statements needed to load the lengths of any variable-length
1345 * headers into registers.
1347 * XXX - a fancier strategy would be to insert those before the
1348 * statements of all blocks that use those lengths and that
1349 * have no predecessors that use them, so that we only compute
1350 * the lengths if we need them. There might be even better
1351 * approaches than that.
1353 * However, those strategies would be more complicated, and
1354 * as we don't generate code to compute a length if the
1355 * program has no tests that use the length, and as most
1356 * tests will probably use those lengths, we would just
1357 * postpone computing the lengths so that it's not done
1358 * for tests that fail early, and it's not clear that's
1361 insert_compute_vloffsets(cstate
, p
->head
);
1364 * For DLT_PPI captures, generate a check of the per-packet
1365 * DLT value to make sure it's DLT_IEEE802_11.
1367 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1368 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1369 * with appropriate Ethernet information and use that rather
1370 * than using something such as DLT_PPI where you don't know
1371 * the link-layer header type until runtime, which, in the
1372 * general case, would force us to generate both Ethernet *and*
1373 * 802.11 code (*and* anything else for which PPI is used)
1374 * and choose between them early in the BPF program?
1376 if (cstate
->linktype
== DLT_PPI
) {
1377 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1378 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1379 gen_and(ppi_dlt_check
, p
);
1382 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1383 p
->sense
= !p
->sense
;
1384 backpatch(p
, gen_retblk_internal(cstate
, 0));
1385 cstate
->ic
.root
= p
->head
;
1390 gen_and(struct block
*b0
, struct block
*b1
)
1392 backpatch(b0
, b1
->head
);
1393 b0
->sense
= !b0
->sense
;
1394 b1
->sense
= !b1
->sense
;
1396 b1
->sense
= !b1
->sense
;
1397 b1
->head
= b0
->head
;
1401 gen_or(struct block
*b0
, struct block
*b1
)
1403 b0
->sense
= !b0
->sense
;
1404 backpatch(b0
, b1
->head
);
1405 b0
->sense
= !b0
->sense
;
1407 b1
->head
= b0
->head
;
1411 gen_not(struct block
*b
)
1413 b
->sense
= !b
->sense
;
1416 static struct block
*
1417 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1418 u_int size
, bpf_u_int32 v
)
1420 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1423 static struct block
*
1424 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1425 u_int size
, bpf_u_int32 v
)
1427 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1430 static struct block
*
1431 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1432 u_int size
, bpf_u_int32 v
)
1434 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1437 static struct block
*
1438 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1439 u_int size
, bpf_u_int32 v
)
1441 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1444 static struct block
*
1445 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1446 u_int size
, bpf_u_int32 v
)
1448 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1451 static struct block
*
1452 gen_cmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1453 u_int size
, bpf_u_int32 v
)
1455 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 1, v
);
1458 static struct block
*
1459 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1460 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1463 * For any A: if mask == 0, it means A & mask == 0, so the result is
1464 * true iff v == 0. In this case ideally the caller should have
1465 * skipped this invocation and have fewer statement blocks to juggle.
1466 * If the caller could have skipped, but has not, produce a block with
1469 * This could be done in gen_ncmp() in a more generic way, but this
1470 * function is the only code path that can have mask == 0.
1473 return v
? gen_false(cstate
) : gen_true(cstate
);
1475 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1478 static struct block
*
1479 gen_mcmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1480 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1482 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 1, v
);
1485 static struct block
*
1486 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1487 u_int size
, const u_char
*v
)
1489 register struct block
*b
, *tmp
;
1493 register const u_char
*p
= &v
[size
- 4];
1495 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1503 register const u_char
*p
= &v
[size
- 2];
1505 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1513 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1521 static struct block
*
1522 gen_jmp(compiler_state_t
*cstate
, int jtype
, bpf_u_int32 v
, struct slist
*stmts
)
1524 struct block
*b
= new_block(cstate
, JMP(jtype
));
1530 static struct block
*
1531 gen_set(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1533 return gen_jmp(cstate
, BPF_JSET
, v
, stmts
);
1536 static struct block
*
1537 gen_unset(compiler_state_t
*cstate
, bpf_u_int32 v
, struct slist
*stmts
)
1539 struct block
*b
= gen_set(cstate
, v
, stmts
);
1545 * AND the field of size "size" at offset "offset" relative to the header
1546 * specified by "offrel" with "mask", and compare it with the value "v"
1547 * with the test specified by "jtype"; if "reverse" is true, the test
1548 * should test the opposite of "jtype".
1550 static struct block
*
1551 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1552 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1555 struct slist
*s
, *s2
;
1558 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1560 if (mask
!= 0xffffffff) {
1561 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1566 b
= gen_jmp(cstate
, jtype
, v
, s
);
1573 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1575 cstate
->pcap_fddipad
= p
->fddipad
;
1578 * We start out with only one link-layer header.
1580 cstate
->outermostlinktype
= pcap_datalink(p
);
1581 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1582 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1583 cstate
->off_outermostlinkhdr
.reg
= -1;
1585 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1586 cstate
->off_prevlinkhdr
.constant_part
= 0;
1587 cstate
->off_prevlinkhdr
.is_variable
= 0;
1588 cstate
->off_prevlinkhdr
.reg
= -1;
1590 cstate
->linktype
= cstate
->outermostlinktype
;
1591 cstate
->off_linkhdr
.constant_part
= 0;
1592 cstate
->off_linkhdr
.is_variable
= 0;
1593 cstate
->off_linkhdr
.reg
= -1;
1598 cstate
->off_linkpl
.constant_part
= 0;
1599 cstate
->off_linkpl
.is_variable
= 0;
1600 cstate
->off_linkpl
.reg
= -1;
1602 cstate
->off_linktype
.constant_part
= 0;
1603 cstate
->off_linktype
.is_variable
= 0;
1604 cstate
->off_linktype
.reg
= -1;
1607 * Assume it's not raw ATM with a pseudo-header, for now.
1610 cstate
->off_vpi
= OFFSET_NOT_SET
;
1611 cstate
->off_vci
= OFFSET_NOT_SET
;
1612 cstate
->off_proto
= OFFSET_NOT_SET
;
1613 cstate
->off_payload
= OFFSET_NOT_SET
;
1616 * And not encapsulated with either Geneve or VXLAN.
1618 cstate
->is_encap
= 0;
1621 * No variable length VLAN offset by default
1623 cstate
->is_vlan_vloffset
= 0;
1626 * And assume we're not doing SS7.
1628 cstate
->off_li
= OFFSET_NOT_SET
;
1629 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1630 cstate
->off_sio
= OFFSET_NOT_SET
;
1631 cstate
->off_opc
= OFFSET_NOT_SET
;
1632 cstate
->off_dpc
= OFFSET_NOT_SET
;
1633 cstate
->off_sls
= OFFSET_NOT_SET
;
1635 cstate
->label_stack_depth
= 0;
1636 cstate
->vlan_stack_depth
= 0;
1638 switch (cstate
->linktype
) {
1641 cstate
->off_linktype
.constant_part
= 2;
1642 cstate
->off_linkpl
.constant_part
= 6;
1643 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1644 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1647 case DLT_ARCNET_LINUX
:
1648 cstate
->off_linktype
.constant_part
= 4;
1649 cstate
->off_linkpl
.constant_part
= 8;
1650 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1651 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1655 cstate
->off_linktype
.constant_part
= 12;
1656 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1657 cstate
->off_nl
= 0; /* Ethernet II */
1658 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1663 * SLIP doesn't have a link level type. The 16 byte
1664 * header is hacked into our SLIP driver.
1666 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1667 cstate
->off_linkpl
.constant_part
= 16;
1669 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1672 case DLT_SLIP_BSDOS
:
1673 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1674 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1676 cstate
->off_linkpl
.constant_part
= 24;
1678 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1683 cstate
->off_linktype
.constant_part
= 0;
1684 cstate
->off_linkpl
.constant_part
= 4;
1686 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1690 cstate
->off_linktype
.constant_part
= 0;
1691 cstate
->off_linkpl
.constant_part
= 12;
1693 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1698 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1699 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1700 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1701 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1702 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1704 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1709 * This does not include the Ethernet header, and
1710 * only covers session state.
1712 cstate
->off_linktype
.constant_part
= 6;
1713 cstate
->off_linkpl
.constant_part
= 8;
1715 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1719 cstate
->off_linktype
.constant_part
= 5;
1720 cstate
->off_linkpl
.constant_part
= 24;
1722 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1727 * FDDI 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 cstate
->off_linktype
.constant_part
= 13;
1735 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1736 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1737 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1738 cstate
->off_nl
= 8; /* 802.2+SNAP */
1739 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1744 * Token Ring doesn't really have a link-level type field.
1745 * We set "off_linktype" to the offset of the LLC header.
1747 * To check for Ethernet types, we assume that SSAP = SNAP
1748 * is being used and pick out the encapsulated Ethernet type.
1749 * XXX - should we generate code to check for SNAP?
1751 * XXX - the header is actually variable-length.
1752 * Some various Linux patched versions gave 38
1753 * as "off_linktype" and 40 as "off_nl"; however,
1754 * if a token ring packet has *no* routing
1755 * information, i.e. is not source-routed, the correct
1756 * values are 20 and 22, as they are in the vanilla code.
1758 * A packet is source-routed iff the uppermost bit
1759 * of the first byte of the source address, at an
1760 * offset of 8, has the uppermost bit set. If the
1761 * packet is source-routed, the total number of bytes
1762 * of routing information is 2 plus bits 0x1F00 of
1763 * the 16-bit value at an offset of 14 (shifted right
1764 * 8 - figure out which byte that is).
1766 cstate
->off_linktype
.constant_part
= 14;
1767 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1768 cstate
->off_nl
= 8; /* 802.2+SNAP */
1769 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1772 case DLT_PRISM_HEADER
:
1773 case DLT_IEEE802_11_RADIO_AVS
:
1774 case DLT_IEEE802_11_RADIO
:
1775 cstate
->off_linkhdr
.is_variable
= 1;
1776 /* Fall through, 802.11 doesn't have a variable link
1777 * prefix but is otherwise the same. */
1780 case DLT_IEEE802_11
:
1782 * 802.11 doesn't really have a link-level type field.
1783 * We set "off_linktype.constant_part" to the offset of
1786 * To check for Ethernet types, we assume that SSAP = SNAP
1787 * is being used and pick out the encapsulated Ethernet type.
1788 * XXX - should we generate code to check for SNAP?
1790 * We also handle variable-length radio headers here.
1791 * The Prism header is in theory variable-length, but in
1792 * practice it's always 144 bytes long. However, some
1793 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1794 * sometimes or always supply an AVS header, so we
1795 * have to check whether the radio header is a Prism
1796 * header or an AVS header, so, in practice, it's
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_nl
= 8; /* 802.2+SNAP */
1803 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1808 * At the moment we treat PPI the same way that we treat
1809 * normal Radiotap encoded packets. The difference is in
1810 * the function that generates the code at the beginning
1811 * to compute the header length. Since this code generator
1812 * of PPI supports bare 802.11 encapsulation only (i.e.
1813 * the encapsulated DLT should be DLT_IEEE802_11) we
1814 * generate code to check for this too.
1816 cstate
->off_linktype
.constant_part
= 24;
1817 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1818 cstate
->off_linkpl
.is_variable
= 1;
1819 cstate
->off_linkhdr
.is_variable
= 1;
1820 cstate
->off_nl
= 8; /* 802.2+SNAP */
1821 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1824 case DLT_ATM_RFC1483
:
1825 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1827 * assume routed, non-ISO PDUs
1828 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1830 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1831 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1832 * latter would presumably be treated the way PPPoE
1833 * should be, so you can do "pppoe and udp port 2049"
1834 * or "pppoa and tcp port 80" and have it check for
1835 * PPPo{A,E} and a PPP protocol of IP and....
1837 cstate
->off_linktype
.constant_part
= 0;
1838 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1839 cstate
->off_nl
= 8; /* 802.2+SNAP */
1840 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1845 * Full Frontal ATM; you get AALn PDUs with an ATM
1849 cstate
->off_vpi
= SUNATM_VPI_POS
;
1850 cstate
->off_vci
= SUNATM_VCI_POS
;
1851 cstate
->off_proto
= PROTO_POS
;
1852 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1853 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1854 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1855 cstate
->off_nl
= 8; /* 802.2+SNAP */
1856 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1862 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1863 cstate
->off_linkpl
.constant_part
= 0;
1865 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1868 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1869 cstate
->off_linktype
.constant_part
= 14;
1870 cstate
->off_linkpl
.constant_part
= 16;
1872 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1875 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1876 cstate
->off_linktype
.constant_part
= 0;
1877 cstate
->off_linkpl
.constant_part
= 20;
1879 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1884 * LocalTalk does have a 1-byte type field in the LLAP header,
1885 * but really it just indicates whether there is a "short" or
1886 * "long" DDP packet following.
1888 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1889 cstate
->off_linkpl
.constant_part
= 0;
1891 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1894 case DLT_IP_OVER_FC
:
1896 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1897 * link-level type field. We set "off_linktype" to the
1898 * offset of the LLC header.
1900 * To check for Ethernet types, we assume that SSAP = SNAP
1901 * is being used and pick out the encapsulated Ethernet type.
1902 * XXX - should we generate code to check for SNAP? RFC
1903 * 2625 says SNAP should be used.
1905 cstate
->off_linktype
.constant_part
= 16;
1906 cstate
->off_linkpl
.constant_part
= 16;
1907 cstate
->off_nl
= 8; /* 802.2+SNAP */
1908 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1913 * XXX - we should set this to handle SNAP-encapsulated
1914 * frames (NLPID of 0x80).
1916 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1917 cstate
->off_linkpl
.constant_part
= 0;
1919 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1923 * the only BPF-interesting FRF.16 frames are non-control frames;
1924 * Frame Relay has a variable length link-layer
1925 * so lets start with offset 4 for now and increments later on (FIXME);
1928 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1929 cstate
->off_linkpl
.constant_part
= 0;
1931 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1934 case DLT_APPLE_IP_OVER_IEEE1394
:
1935 cstate
->off_linktype
.constant_part
= 16;
1936 cstate
->off_linkpl
.constant_part
= 18;
1938 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1941 case DLT_SYMANTEC_FIREWALL
:
1942 cstate
->off_linktype
.constant_part
= 6;
1943 cstate
->off_linkpl
.constant_part
= 44;
1944 cstate
->off_nl
= 0; /* Ethernet II */
1945 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1949 cstate
->off_linktype
.constant_part
= 0;
1950 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1951 cstate
->off_linkpl
.is_variable
= 1;
1953 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1956 case DLT_JUNIPER_MFR
:
1957 case DLT_JUNIPER_MLFR
:
1958 case DLT_JUNIPER_MLPPP
:
1959 case DLT_JUNIPER_PPP
:
1960 case DLT_JUNIPER_CHDLC
:
1961 case DLT_JUNIPER_FRELAY
:
1962 cstate
->off_linktype
.constant_part
= 4;
1963 cstate
->off_linkpl
.constant_part
= 4;
1965 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1968 case DLT_JUNIPER_ATM1
:
1969 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1970 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1972 cstate
->off_nl_nosnap
= 10;
1975 case DLT_JUNIPER_ATM2
:
1976 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1977 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1979 cstate
->off_nl_nosnap
= 10;
1982 /* frames captured on a Juniper PPPoE service PIC
1983 * contain raw ethernet frames */
1984 case DLT_JUNIPER_PPPOE
:
1985 case DLT_JUNIPER_ETHER
:
1986 cstate
->off_linkpl
.constant_part
= 14;
1987 cstate
->off_linktype
.constant_part
= 16;
1988 cstate
->off_nl
= 18; /* Ethernet II */
1989 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1992 case DLT_JUNIPER_PPPOE_ATM
:
1993 cstate
->off_linktype
.constant_part
= 4;
1994 cstate
->off_linkpl
.constant_part
= 6;
1996 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1999 case DLT_JUNIPER_GGSN
:
2000 cstate
->off_linktype
.constant_part
= 6;
2001 cstate
->off_linkpl
.constant_part
= 12;
2003 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2006 case DLT_JUNIPER_ES
:
2007 cstate
->off_linktype
.constant_part
= 6;
2008 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
2009 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
2010 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2013 case DLT_JUNIPER_MONITOR
:
2014 cstate
->off_linktype
.constant_part
= 12;
2015 cstate
->off_linkpl
.constant_part
= 12;
2016 cstate
->off_nl
= 0; /* raw IP/IP6 header */
2017 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2020 case DLT_BACNET_MS_TP
:
2021 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2022 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2023 cstate
->off_nl
= OFFSET_NOT_SET
;
2024 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2027 case DLT_JUNIPER_SERVICES
:
2028 cstate
->off_linktype
.constant_part
= 12;
2029 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2030 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
2031 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2034 case DLT_JUNIPER_VP
:
2035 cstate
->off_linktype
.constant_part
= 18;
2036 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2037 cstate
->off_nl
= OFFSET_NOT_SET
;
2038 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2041 case DLT_JUNIPER_ST
:
2042 cstate
->off_linktype
.constant_part
= 18;
2043 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2044 cstate
->off_nl
= OFFSET_NOT_SET
;
2045 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2048 case DLT_JUNIPER_ISM
:
2049 cstate
->off_linktype
.constant_part
= 8;
2050 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2051 cstate
->off_nl
= OFFSET_NOT_SET
;
2052 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2055 case DLT_JUNIPER_VS
:
2056 case DLT_JUNIPER_SRX_E2E
:
2057 case DLT_JUNIPER_FIBRECHANNEL
:
2058 case DLT_JUNIPER_ATM_CEMIC
:
2059 cstate
->off_linktype
.constant_part
= 8;
2060 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2061 cstate
->off_nl
= OFFSET_NOT_SET
;
2062 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2067 cstate
->off_li_hsl
= 4;
2068 cstate
->off_sio
= 3;
2069 cstate
->off_opc
= 4;
2070 cstate
->off_dpc
= 4;
2071 cstate
->off_sls
= 7;
2072 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2073 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2074 cstate
->off_nl
= OFFSET_NOT_SET
;
2075 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2078 case DLT_MTP2_WITH_PHDR
:
2080 cstate
->off_li_hsl
= 8;
2081 cstate
->off_sio
= 7;
2082 cstate
->off_opc
= 8;
2083 cstate
->off_dpc
= 8;
2084 cstate
->off_sls
= 11;
2085 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2086 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2087 cstate
->off_nl
= OFFSET_NOT_SET
;
2088 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2092 cstate
->off_li
= 22;
2093 cstate
->off_li_hsl
= 24;
2094 cstate
->off_sio
= 23;
2095 cstate
->off_opc
= 24;
2096 cstate
->off_dpc
= 24;
2097 cstate
->off_sls
= 27;
2098 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2099 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2100 cstate
->off_nl
= OFFSET_NOT_SET
;
2101 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2105 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2106 cstate
->off_linkpl
.constant_part
= 4;
2108 cstate
->off_nl_nosnap
= 0;
2113 * Currently, only raw "link[N:M]" filtering is supported.
2115 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
2116 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2117 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
2118 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2122 cstate
->off_linktype
.constant_part
= 1;
2123 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2125 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2128 case DLT_NETANALYZER
:
2129 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
2130 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2131 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
2132 cstate
->off_nl
= 0; /* Ethernet II */
2133 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2136 case DLT_NETANALYZER_TRANSPARENT
:
2137 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2138 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2139 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2140 cstate
->off_nl
= 0; /* Ethernet II */
2141 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2146 * For values in the range in which we've assigned new
2147 * DLT_ values, only raw "link[N:M]" filtering is supported.
2149 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2150 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2151 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2152 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2153 cstate
->off_nl
= OFFSET_NOT_SET
;
2154 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2156 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2157 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2163 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2168 * Load a value relative to the specified absolute offset.
2170 static struct slist
*
2171 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2172 u_int offset
, u_int size
)
2174 struct slist
*s
, *s2
;
2176 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2179 * If "s" is non-null, it has code to arrange that the X register
2180 * contains the variable part of the absolute offset, so we
2181 * generate a load relative to that, with an offset of
2182 * abs_offset->constant_part + offset.
2184 * Otherwise, we can do an absolute load with an offset of
2185 * abs_offset->constant_part + offset.
2189 * "s" points to a list of statements that puts the
2190 * variable part of the absolute offset into the X register.
2191 * Do an indirect load, to use the X register as an offset.
2193 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2194 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2198 * There is no variable part of the absolute offset, so
2199 * just do an absolute load.
2201 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2202 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2208 * Load a value relative to the beginning of the specified header.
2210 static struct slist
*
2211 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2214 struct slist
*s
, *s2
;
2217 * Squelch warnings from compilers that *don't* assume that
2218 * offrel always has a valid enum value and therefore don't
2219 * assume that we'll always go through one of the case arms.
2221 * If we have a default case, compilers that *do* assume that
2222 * will then complain about the default case code being
2225 * Damned if you do, damned if you don't.
2232 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2237 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2240 case OR_PREVLINKHDR
:
2241 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2245 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2248 case OR_PREVMPLSHDR
:
2249 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2253 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2256 case OR_LINKPL_NOSNAP
:
2257 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2261 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2266 * Load the X register with the length of the IPv4 header
2267 * (plus the offset of the link-layer header, if it's
2268 * preceded by a variable-length header such as a radio
2269 * header), in bytes.
2271 s
= gen_loadx_iphdrlen(cstate
);
2274 * Load the item at {offset of the link-layer payload} +
2275 * {offset, relative to the start of the link-layer
2276 * payload, of the IPv4 header} + {length of the IPv4 header} +
2277 * {specified offset}.
2279 * If the offset of the link-layer payload is variable,
2280 * the variable part of that offset is included in the
2281 * value in the X register, and we include the constant
2282 * part in the offset of the load.
2284 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2285 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2290 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2297 * Generate code to load into the X register the sum of the length of
2298 * the IPv4 header and the variable part of the offset of the link-layer
2301 static struct slist
*
2302 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2304 struct slist
*s
, *s2
;
2306 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2309 * The offset of the link-layer payload has a variable
2310 * part. "s" points to a list of statements that put
2311 * the variable part of that offset into the X register.
2313 * The 4*([k]&0xf) addressing mode can't be used, as we
2314 * don't have a constant offset, so we have to load the
2315 * value in question into the A register and add to it
2316 * the value from the X register.
2318 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2319 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2321 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2324 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2329 * The A register now contains the length of the IP header.
2330 * We need to add to it the variable part of the offset of
2331 * the link-layer payload, which is still in the X
2332 * register, and move the result into the X register.
2334 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2335 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2338 * The offset of the link-layer payload is a constant,
2339 * so no code was generated to load the (nonexistent)
2340 * variable part of that offset.
2342 * This means we can use the 4*([k]&0xf) addressing
2343 * mode. Load the length of the IPv4 header, which
2344 * is at an offset of cstate->off_nl from the beginning of
2345 * the link-layer payload, and thus at an offset of
2346 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2347 * of the raw packet data, using that addressing mode.
2349 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2350 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2356 static struct block
*
2357 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2361 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2363 return gen_jmp(cstate
, BPF_JEQ
, 0, s
);
2366 static inline struct block
*
2367 gen_true(compiler_state_t
*cstate
)
2369 return gen_uncond(cstate
, 1);
2372 static inline struct block
*
2373 gen_false(compiler_state_t
*cstate
)
2375 return gen_uncond(cstate
, 0);
2379 * Generate code to match a particular packet type.
2381 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2382 * value, if <= ETHERMTU. We use that to determine whether to
2383 * match the type/length field or to check the type/length field for
2384 * a value <= ETHERMTU to see whether it's a type field and then do
2385 * the appropriate test.
2387 static struct block
*
2388 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2390 struct block
*b0
, *b1
;
2396 case LLCSAP_NETBEUI
:
2398 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2399 * so we check the DSAP and SSAP.
2401 * LLCSAP_IP checks for IP-over-802.2, rather
2402 * than IP-over-Ethernet or IP-over-SNAP.
2404 * XXX - should we check both the DSAP and the
2405 * SSAP, like this, or should we check just the
2406 * DSAP, as we do for other types <= ETHERMTU
2407 * (i.e., other SAP values)?
2409 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2410 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2418 * Ethernet_II frames, which are Ethernet
2419 * frames with a frame type of ETHERTYPE_IPX;
2421 * Ethernet_802.3 frames, which are 802.3
2422 * frames (i.e., the type/length field is
2423 * a length field, <= ETHERMTU, rather than
2424 * a type field) with the first two bytes
2425 * after the Ethernet/802.3 header being
2428 * Ethernet_802.2 frames, which are 802.3
2429 * frames with an 802.2 LLC header and
2430 * with the IPX LSAP as the DSAP in the LLC
2433 * Ethernet_SNAP frames, which are 802.3
2434 * frames with an LLC header and a SNAP
2435 * header and with an OUI of 0x000000
2436 * (encapsulated Ethernet) and a protocol
2437 * ID of ETHERTYPE_IPX in the SNAP header.
2439 * XXX - should we generate the same code both
2440 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2444 * This generates code to check both for the
2445 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2447 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2448 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2452 * Now we add code to check for SNAP frames with
2453 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2455 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2459 * Now we generate code to check for 802.3
2460 * frames in general.
2462 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2465 * Now add the check for 802.3 frames before the
2466 * check for Ethernet_802.2 and Ethernet_802.3,
2467 * as those checks should only be done on 802.3
2468 * frames, not on Ethernet frames.
2473 * Now add the check for Ethernet_II frames, and
2474 * do that before checking for the other frame
2477 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2481 case ETHERTYPE_ATALK
:
2482 case ETHERTYPE_AARP
:
2484 * EtherTalk (AppleTalk protocols on Ethernet link
2485 * layer) may use 802.2 encapsulation.
2489 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2490 * we check for an Ethernet type field less or equal than
2491 * 1500, which means it's an 802.3 length field.
2493 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2496 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2497 * SNAP packets with an organization code of
2498 * 0x080007 (Apple, for Appletalk) and a protocol
2499 * type of ETHERTYPE_ATALK (Appletalk).
2501 * 802.2-encapsulated ETHERTYPE_AARP packets are
2502 * SNAP packets with an organization code of
2503 * 0x000000 (encapsulated Ethernet) and a protocol
2504 * type of ETHERTYPE_AARP (Appletalk ARP).
2506 if (ll_proto
== ETHERTYPE_ATALK
)
2507 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2508 else /* ll_proto == ETHERTYPE_AARP */
2509 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2513 * Check for Ethernet encapsulation (Ethertalk
2514 * phase 1?); we just check for the Ethernet
2517 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2523 if (ll_proto
<= ETHERMTU
) {
2524 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2526 * This is an LLC SAP value, so the frames
2527 * that match would be 802.2 frames.
2528 * Check that the frame is an 802.2 frame
2529 * (i.e., that the length/type field is
2530 * a length field, <= ETHERMTU) and
2531 * then check the DSAP.
2533 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2534 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2538 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2540 * This is an Ethernet type, so compare
2541 * the length/type field with it (if
2542 * the frame is an 802.2 frame, the length
2543 * field will be <= ETHERMTU, and, as
2544 * "ll_proto" is > ETHERMTU, this test
2545 * will fail and the frame won't match,
2546 * which is what we want).
2548 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2553 static struct block
*
2554 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2557 * For DLT_NULL, the link-layer header is a 32-bit word
2558 * containing an AF_ value in *host* byte order, and for
2559 * DLT_ENC, the link-layer header begins with a 32-bit
2560 * word containing an AF_ value in host byte order.
2562 * In addition, if we're reading a saved capture file,
2563 * the host byte order in the capture may not be the
2564 * same as the host byte order on this machine.
2566 * For DLT_LOOP, the link-layer header is a 32-bit
2567 * word containing an AF_ value in *network* byte order.
2569 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2571 * The AF_ value is in host byte order, but the BPF
2572 * interpreter will convert it to network byte order.
2574 * If this is a save file, and it's from a machine
2575 * with the opposite byte order to ours, we byte-swap
2578 * Then we run it through "htonl()", and generate
2579 * code to compare against the result.
2581 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2582 ll_proto
= SWAPLONG(ll_proto
);
2583 ll_proto
= htonl(ll_proto
);
2585 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2589 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2590 * or IPv6 then we have an error.
2592 static struct block
*
2593 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2598 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2601 case ETHERTYPE_IPV6
:
2602 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2609 return gen_false(cstate
);
2613 * Generate code to match a particular packet type.
2615 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2616 * value, if <= ETHERMTU. We use that to determine whether to
2617 * match the type field or to check the type field for the special
2618 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2620 static struct block
*
2621 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2623 struct block
*b0
, *b1
;
2629 case LLCSAP_NETBEUI
:
2631 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2632 * so we check the DSAP and SSAP.
2634 * LLCSAP_IP checks for IP-over-802.2, rather
2635 * than IP-over-Ethernet or IP-over-SNAP.
2637 * XXX - should we check both the DSAP and the
2638 * SSAP, like this, or should we check just the
2639 * DSAP, as we do for other types <= ETHERMTU
2640 * (i.e., other SAP values)?
2642 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2643 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2649 * Ethernet_II frames, which are Ethernet
2650 * frames with a frame type of ETHERTYPE_IPX;
2652 * Ethernet_802.3 frames, which have a frame
2653 * type of LINUX_SLL_P_802_3;
2655 * Ethernet_802.2 frames, which are 802.3
2656 * frames with an 802.2 LLC header (i.e, have
2657 * a frame type of LINUX_SLL_P_802_2) and
2658 * with the IPX LSAP as the DSAP in the LLC
2661 * Ethernet_SNAP frames, which are 802.3
2662 * frames with an LLC header and a SNAP
2663 * header and with an OUI of 0x000000
2664 * (encapsulated Ethernet) and a protocol
2665 * ID of ETHERTYPE_IPX in the SNAP header.
2667 * First, do the checks on LINUX_SLL_P_802_2
2668 * frames; generate the check for either
2669 * Ethernet_802.2 or Ethernet_SNAP frames, and
2670 * then put a check for LINUX_SLL_P_802_2 frames
2673 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2674 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2676 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2680 * Now check for 802.3 frames and OR that with
2681 * the previous test.
2683 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2687 * Now add the check for Ethernet_II frames, and
2688 * do that before checking for the other frame
2691 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2695 case ETHERTYPE_ATALK
:
2696 case ETHERTYPE_AARP
:
2698 * EtherTalk (AppleTalk protocols on Ethernet link
2699 * layer) may use 802.2 encapsulation.
2703 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2704 * we check for the 802.2 protocol type in the
2705 * "Ethernet type" field.
2707 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2710 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2711 * SNAP packets with an organization code of
2712 * 0x080007 (Apple, for Appletalk) and a protocol
2713 * type of ETHERTYPE_ATALK (Appletalk).
2715 * 802.2-encapsulated ETHERTYPE_AARP packets are
2716 * SNAP packets with an organization code of
2717 * 0x000000 (encapsulated Ethernet) and a protocol
2718 * type of ETHERTYPE_AARP (Appletalk ARP).
2720 if (ll_proto
== ETHERTYPE_ATALK
)
2721 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2722 else /* ll_proto == ETHERTYPE_AARP */
2723 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2727 * Check for Ethernet encapsulation (Ethertalk
2728 * phase 1?); we just check for the Ethernet
2731 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2737 if (ll_proto
<= ETHERMTU
) {
2738 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
2740 * This is an LLC SAP value, so the frames
2741 * that match would be 802.2 frames.
2742 * Check for the 802.2 protocol type
2743 * in the "Ethernet type" field, and
2744 * then check the DSAP.
2746 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2747 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2752 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
2754 * This is an Ethernet type, so compare
2755 * the length/type field with it (if
2756 * the frame is an 802.2 frame, the length
2757 * field will be <= ETHERMTU, and, as
2758 * "ll_proto" is > ETHERMTU, this test
2759 * will fail and the frame won't match,
2760 * which is what we want).
2762 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2768 * Load a value relative to the beginning of the link-layer header after the
2771 static struct slist
*
2772 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2774 struct slist
*s1
, *s2
;
2777 * Generate code to load the length of the pflog header into
2778 * the register assigned to hold that length, if one has been
2779 * assigned. (If one hasn't been assigned, no code we've
2780 * generated uses that prefix, so we don't need to generate any
2783 if (cstate
->off_linkpl
.reg
!= -1) {
2785 * The length is in the first byte of the header.
2787 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2791 * Round it up to a multiple of 4.
2792 * Add 3, and clear the lower 2 bits.
2794 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2797 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2798 s2
->s
.k
= 0xfffffffc;
2802 * Now allocate a register to hold that value and store
2805 s2
= new_stmt(cstate
, BPF_ST
);
2806 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2810 * Now move it into the X register.
2812 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2820 static struct slist
*
2821 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2823 struct slist
*s1
, *s2
;
2824 struct slist
*sjeq_avs_cookie
;
2825 struct slist
*sjcommon
;
2828 * This code is not compatible with the optimizer, as
2829 * we are generating jmp instructions within a normal
2830 * slist of instructions
2832 cstate
->no_optimize
= 1;
2835 * Generate code to load the length of the radio header into
2836 * the register assigned to hold that length, if one has been
2837 * assigned. (If one hasn't been assigned, no code we've
2838 * generated uses that prefix, so we don't need to generate any
2841 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2842 * or always use the AVS header rather than the Prism header.
2843 * We load a 4-byte big-endian value at the beginning of the
2844 * raw packet data, and see whether, when masked with 0xFFFFF000,
2845 * it's equal to 0x80211000. If so, that indicates that it's
2846 * an AVS header (the masked-out bits are the version number).
2847 * Otherwise, it's a Prism header.
2849 * XXX - the Prism header is also, in theory, variable-length,
2850 * but no known software generates headers that aren't 144
2853 if (cstate
->off_linkhdr
.reg
!= -1) {
2857 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2861 * AND it with 0xFFFFF000.
2863 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2864 s2
->s
.k
= 0xFFFFF000;
2868 * Compare with 0x80211000.
2870 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2871 sjeq_avs_cookie
->s
.k
= 0x80211000;
2872 sappend(s1
, sjeq_avs_cookie
);
2877 * The 4 bytes at an offset of 4 from the beginning of
2878 * the AVS header are the length of the AVS header.
2879 * That field is big-endian.
2881 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2884 sjeq_avs_cookie
->s
.jt
= s2
;
2887 * Now jump to the code to allocate a register
2888 * into which to save the header length and
2889 * store the length there. (The "jump always"
2890 * instruction needs to have the k field set;
2891 * it's added to the PC, so, as we're jumping
2892 * over a single instruction, it should be 1.)
2894 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2896 sappend(s1
, sjcommon
);
2899 * Now for the code that handles the Prism header.
2900 * Just load the length of the Prism header (144)
2901 * into the A register. Have the test for an AVS
2902 * header branch here if we don't have an AVS header.
2904 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2907 sjeq_avs_cookie
->s
.jf
= s2
;
2910 * Now allocate a register to hold that value and store
2911 * it. The code for the AVS header will jump here after
2912 * loading the length of the AVS header.
2914 s2
= new_stmt(cstate
, BPF_ST
);
2915 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2917 sjcommon
->s
.jf
= s2
;
2920 * Now move it into the X register.
2922 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2930 static struct slist
*
2931 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2933 struct slist
*s1
, *s2
;
2936 * Generate code to load the length of the AVS header into
2937 * the register assigned to hold that length, if one has been
2938 * assigned. (If one hasn't been assigned, no code we've
2939 * generated uses that prefix, so we don't need to generate any
2942 if (cstate
->off_linkhdr
.reg
!= -1) {
2944 * The 4 bytes at an offset of 4 from the beginning of
2945 * the AVS header are the length of the AVS header.
2946 * That field is big-endian.
2948 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2952 * Now allocate a register to hold that value and store
2955 s2
= new_stmt(cstate
, BPF_ST
);
2956 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2960 * Now move it into the X register.
2962 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2970 static struct slist
*
2971 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2973 struct slist
*s1
, *s2
;
2976 * Generate code to load the length of the radiotap header into
2977 * the register assigned to hold that length, if one has been
2978 * assigned. (If one hasn't been assigned, no code we've
2979 * generated uses that prefix, so we don't need to generate any
2982 if (cstate
->off_linkhdr
.reg
!= -1) {
2984 * The 2 bytes at offsets of 2 and 3 from the beginning
2985 * of the radiotap header are the length of the radiotap
2986 * header; unfortunately, it's little-endian, so we have
2987 * to load it a byte at a time and construct the value.
2991 * Load the high-order byte, at an offset of 3, shift it
2992 * left a byte, and put the result in the X register.
2994 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2996 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2999 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3003 * Load the next byte, at an offset of 2, and OR the
3004 * value from the X register into it.
3006 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3009 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3013 * Now allocate a register to hold that value and store
3016 s2
= new_stmt(cstate
, BPF_ST
);
3017 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3021 * Now move it into the X register.
3023 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3032 * At the moment we treat PPI as normal Radiotap encoded
3033 * packets. The difference is in the function that generates
3034 * the code at the beginning to compute the header length.
3035 * Since this code generator of PPI supports bare 802.11
3036 * encapsulation only (i.e. the encapsulated DLT should be
3037 * DLT_IEEE802_11) we generate code to check for this too;
3038 * that's done in finish_parse().
3040 static struct slist
*
3041 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
3043 struct slist
*s1
, *s2
;
3046 * Generate code to load the length of the radiotap header
3047 * into the register assigned to hold that length, if one has
3050 if (cstate
->off_linkhdr
.reg
!= -1) {
3052 * The 2 bytes at offsets of 2 and 3 from the beginning
3053 * of the radiotap header are the length of the radiotap
3054 * header; unfortunately, it's little-endian, so we have
3055 * to load it a byte at a time and construct the value.
3059 * Load the high-order byte, at an offset of 3, shift it
3060 * left a byte, and put the result in the X register.
3062 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3064 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
3067 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3071 * Load the next byte, at an offset of 2, and OR the
3072 * value from the X register into it.
3074 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3077 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3081 * Now allocate a register to hold that value and store
3084 s2
= new_stmt(cstate
, BPF_ST
);
3085 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3089 * Now move it into the X register.
3091 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3100 * Load a value relative to the beginning of the link-layer header after the 802.11
3101 * header, i.e. LLC_SNAP.
3102 * The link-layer header doesn't necessarily begin at the beginning
3103 * of the packet data; there might be a variable-length prefix containing
3104 * radio information.
3106 static struct slist
*
3107 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
3110 struct slist
*sjset_data_frame_1
;
3111 struct slist
*sjset_data_frame_2
;
3112 struct slist
*sjset_qos
;
3113 struct slist
*sjset_radiotap_flags_present
;
3114 struct slist
*sjset_radiotap_ext_present
;
3115 struct slist
*sjset_radiotap_tsft_present
;
3116 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
3117 struct slist
*s_roundup
;
3119 if (cstate
->off_linkpl
.reg
== -1) {
3121 * No register has been assigned to the offset of
3122 * the link-layer payload, which means nobody needs
3123 * it; don't bother computing it - just return
3124 * what we already have.
3130 * This code is not compatible with the optimizer, as
3131 * we are generating jmp instructions within a normal
3132 * slist of instructions
3134 cstate
->no_optimize
= 1;
3137 * If "s" is non-null, it has code to arrange that the X register
3138 * contains the length of the prefix preceding the link-layer
3141 * Otherwise, the length of the prefix preceding the link-layer
3142 * header is "off_outermostlinkhdr.constant_part".
3146 * There is no variable-length header preceding the
3147 * link-layer header.
3149 * Load the length of the fixed-length prefix preceding
3150 * the link-layer header (if any) into the X register,
3151 * and store it in the cstate->off_linkpl.reg register.
3152 * That length is off_outermostlinkhdr.constant_part.
3154 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3155 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3159 * The X register contains the offset of the beginning of the
3160 * link-layer header; add 24, which is the minimum length
3161 * of the MAC header for a data frame, to that, and store it
3162 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3163 * which is at the offset in the X register, with an indexed load.
3165 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3167 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3170 s2
= new_stmt(cstate
, BPF_ST
);
3171 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3174 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3179 * Check the Frame Control field to see if this is a data frame;
3180 * a data frame has the 0x08 bit (b3) in that field set and the
3181 * 0x04 bit (b2) clear.
3183 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3184 sjset_data_frame_1
->s
.k
= IEEE80211_FC0_TYPE_DATA
;
3185 sappend(s
, sjset_data_frame_1
);
3188 * If b3 is set, test b2, otherwise go to the first statement of
3189 * the rest of the program.
3191 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3192 sjset_data_frame_2
->s
.k
= IEEE80211_FC0_TYPE_CTL
;
3193 sappend(s
, sjset_data_frame_2
);
3194 sjset_data_frame_1
->s
.jf
= snext
;
3197 * If b2 is not set, this is a data frame; test the QoS bit.
3198 * Otherwise, go to the first statement of the rest of the
3201 sjset_data_frame_2
->s
.jt
= snext
;
3202 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3203 sjset_qos
->s
.k
= IEEE80211_FC0_SUBTYPE_QOS
;
3204 sappend(s
, sjset_qos
);
3207 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3209 * Otherwise, go to the first statement of the rest of the
3212 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3213 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3215 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3218 s2
= new_stmt(cstate
, BPF_ST
);
3219 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3223 * If we have a radiotap header, look at it to see whether
3224 * there's Atheros padding between the MAC-layer header
3227 * Note: all of the fields in the radiotap header are
3228 * little-endian, so we byte-swap all of the values
3229 * we test against, as they will be loaded as big-endian
3232 * XXX - in the general case, we would have to scan through
3233 * *all* the presence bits, if there's more than one word of
3234 * presence bits. That would require a loop, meaning that
3235 * we wouldn't be able to run the filter in the kernel.
3237 * We assume here that the Atheros adapters that insert the
3238 * annoying padding don't have multiple antennae and therefore
3239 * do not generate radiotap headers with multiple presence words.
3241 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3243 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3244 * in the first presence flag word?
3246 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3250 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3251 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3252 sappend(s
, sjset_radiotap_flags_present
);
3255 * If not, skip all of this.
3257 sjset_radiotap_flags_present
->s
.jf
= snext
;
3260 * Otherwise, is the "extension" bit set in that word?
3262 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3263 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3264 sappend(s
, sjset_radiotap_ext_present
);
3265 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3268 * If so, skip all of this.
3270 sjset_radiotap_ext_present
->s
.jt
= snext
;
3273 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3275 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3276 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3277 sappend(s
, sjset_radiotap_tsft_present
);
3278 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3281 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3282 * at an offset of 16 from the beginning of the raw packet
3283 * data (8 bytes for the radiotap header and 8 bytes for
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
.jt
= s2
;
3294 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3295 sjset_tsft_datapad
->s
.k
= 0x20;
3296 sappend(s
, sjset_tsft_datapad
);
3299 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3300 * at an offset of 8 from the beginning of the raw packet
3301 * data (8 bytes for the radiotap header).
3303 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3306 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3309 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3311 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3312 sjset_notsft_datapad
->s
.k
= 0x20;
3313 sappend(s
, sjset_notsft_datapad
);
3316 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3317 * set, round the length of the 802.11 header to
3318 * a multiple of 4. Do that by adding 3 and then
3319 * dividing by and multiplying by 4, which we do by
3322 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3323 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3324 sappend(s
, s_roundup
);
3325 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3328 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3329 s2
->s
.k
= (bpf_u_int32
)~3;
3331 s2
= new_stmt(cstate
, BPF_ST
);
3332 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3335 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3336 sjset_tsft_datapad
->s
.jf
= snext
;
3337 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3338 sjset_notsft_datapad
->s
.jf
= snext
;
3340 sjset_qos
->s
.jf
= snext
;
3346 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3350 /* There is an implicit dependency between the link
3351 * payload and link header since the payload computation
3352 * includes the variable part of the header. Therefore,
3353 * if nobody else has allocated a register for the link
3354 * header and we need it, do it now. */
3355 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3356 cstate
->off_linkhdr
.reg
== -1)
3357 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3360 * For link-layer types that have a variable-length header
3361 * preceding the link-layer header, generate code to load
3362 * the offset of the link-layer header into the register
3363 * assigned to that offset, if any.
3365 * XXX - this, and the next switch statement, won't handle
3366 * encapsulation of 802.11 or 802.11+radio information in
3367 * some other protocol stack. That's significantly more
3370 switch (cstate
->outermostlinktype
) {
3372 case DLT_PRISM_HEADER
:
3373 s
= gen_load_prism_llprefixlen(cstate
);
3376 case DLT_IEEE802_11_RADIO_AVS
:
3377 s
= gen_load_avs_llprefixlen(cstate
);
3380 case DLT_IEEE802_11_RADIO
:
3381 s
= gen_load_radiotap_llprefixlen(cstate
);
3385 s
= gen_load_ppi_llprefixlen(cstate
);
3394 * For link-layer types that have a variable-length link-layer
3395 * header, generate code to load the offset of the link-layer
3396 * payload into the register assigned to that offset, if any.
3398 switch (cstate
->outermostlinktype
) {
3400 case DLT_IEEE802_11
:
3401 case DLT_PRISM_HEADER
:
3402 case DLT_IEEE802_11_RADIO_AVS
:
3403 case DLT_IEEE802_11_RADIO
:
3405 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3409 s
= gen_load_pflog_llprefixlen(cstate
);
3414 * If there is no initialization yet and we need variable
3415 * length offsets for VLAN, initialize them to zero
3417 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3420 if (cstate
->off_linkpl
.reg
== -1)
3421 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3422 if (cstate
->off_linktype
.reg
== -1)
3423 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3425 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3427 s2
= new_stmt(cstate
, BPF_ST
);
3428 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3430 s2
= new_stmt(cstate
, BPF_ST
);
3431 s2
->s
.k
= cstate
->off_linktype
.reg
;
3436 * If we have any offset-loading code, append all the
3437 * existing statements in the block to those statements,
3438 * and make the resulting list the list of statements
3442 sappend(s
, b
->stmts
);
3448 * Take an absolute offset, and:
3450 * if it has no variable part, return NULL;
3452 * if it has a variable part, generate code to load the register
3453 * containing that variable part into the X register, returning
3454 * a pointer to that code - if no register for that offset has
3455 * been allocated, allocate it first.
3457 * (The code to set that register will be generated later, but will
3458 * be placed earlier in the code sequence.)
3460 static struct slist
*
3461 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3465 if (off
->is_variable
) {
3466 if (off
->reg
== -1) {
3468 * We haven't yet assigned a register for the
3469 * variable part of the offset of the link-layer
3470 * header; allocate one.
3472 off
->reg
= alloc_reg(cstate
);
3476 * Load the register containing the variable part of the
3477 * offset of the link-layer header into the X register.
3479 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3484 * That offset isn't variable, there's no variable part,
3485 * so we don't need to generate any code.
3492 * Map an Ethernet type to the equivalent PPP type.
3495 ethertype_to_ppptype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3502 case ETHERTYPE_IPV6
:
3508 case ETHERTYPE_ATALK
:
3519 * I'm assuming the "Bridging PDU"s that go
3520 * over PPP are Spanning Tree Protocol
3528 assert_maxval(cstate
, "PPP protocol", ll_proto
, UINT16_MAX
);
3529 return (uint16_t)ll_proto
;
3533 * Generate any tests that, for encapsulation of a link-layer packet
3534 * inside another protocol stack, need to be done to check for those
3535 * link-layer packets (and that haven't already been done by a check
3536 * for that encapsulation).
3538 static struct block
*
3539 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3541 if (cstate
->is_encap
)
3542 return gen_encap_ll_check(cstate
);
3544 switch (cstate
->prevlinktype
) {
3548 * This is LANE-encapsulated Ethernet; check that the LANE
3549 * packet doesn't begin with an LE Control marker, i.e.
3550 * that it's data, not a control message.
3552 * (We've already generated a test for LANE.)
3554 return gen_cmp_ne(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3558 * No such tests are necessary.
3566 * The three different values we should check for when checking for an
3567 * IPv6 packet with DLT_NULL.
3569 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3570 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3571 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3574 * Generate code to match a particular packet type by matching the
3575 * link-layer type field or fields in the 802.2 LLC header.
3577 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3578 * value, if <= ETHERMTU.
3580 static struct block
*
3581 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3583 struct block
*b0
, *b1
, *b2
;
3585 /* are we checking MPLS-encapsulated packets? */
3586 if (cstate
->label_stack_depth
> 0)
3587 return gen_mpls_linktype(cstate
, ll_proto
);
3589 switch (cstate
->linktype
) {
3592 case DLT_NETANALYZER
:
3593 case DLT_NETANALYZER_TRANSPARENT
:
3594 /* Geneve has an EtherType regardless of whether there is an
3595 * L2 header. VXLAN always has an EtherType. */
3596 if (!cstate
->is_encap
)
3597 b0
= gen_prevlinkhdr_check(cstate
);
3601 b1
= gen_ether_linktype(cstate
, ll_proto
);
3609 assert_maxval(cstate
, "HDLC protocol", ll_proto
, UINT16_MAX
);
3613 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3617 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3621 case DLT_IEEE802_11
:
3622 case DLT_PRISM_HEADER
:
3623 case DLT_IEEE802_11_RADIO_AVS
:
3624 case DLT_IEEE802_11_RADIO
:
3627 * Check that we have a data frame.
3629 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3630 IEEE80211_FC0_TYPE_DATA
,
3631 IEEE80211_FC0_TYPE_MASK
);
3634 * Now check for the specified link-layer type.
3636 b1
= gen_llc_linktype(cstate
, ll_proto
);
3643 * XXX - check for LLC frames.
3645 return gen_llc_linktype(cstate
, ll_proto
);
3650 * XXX - check for LLC PDUs, as per IEEE 802.5.
3652 return gen_llc_linktype(cstate
, ll_proto
);
3655 case DLT_ATM_RFC1483
:
3657 case DLT_IP_OVER_FC
:
3658 return gen_llc_linktype(cstate
, ll_proto
);
3663 * Check for an LLC-encapsulated version of this protocol;
3664 * if we were checking for LANE, linktype would no longer
3667 * Check for LLC encapsulation and then check the protocol.
3669 b0
= gen_atm_prototype(cstate
, PT_LLC
);
3670 b1
= gen_llc_linktype(cstate
, ll_proto
);
3676 return gen_linux_sll_linktype(cstate
, ll_proto
);
3680 case DLT_SLIP_BSDOS
:
3683 * These types don't provide any type field; packets
3684 * are always IPv4 or IPv6.
3686 * XXX - for IPv4, check for a version number of 4, and,
3687 * for IPv6, check for a version number of 6?
3692 /* Check for a version number of 4. */
3693 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3695 case ETHERTYPE_IPV6
:
3696 /* Check for a version number of 6. */
3697 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3700 return gen_false(cstate
); /* always false */
3706 * Raw IPv4, so no type field.
3708 if (ll_proto
== ETHERTYPE_IP
)
3709 return gen_true(cstate
); /* always true */
3711 /* Checking for something other than IPv4; always false */
3712 return gen_false(cstate
);
3717 * Raw IPv6, so no type field.
3719 if (ll_proto
== ETHERTYPE_IPV6
)
3720 return gen_true(cstate
); /* always true */
3722 /* Checking for something other than IPv6; always false */
3723 return gen_false(cstate
);
3728 case DLT_PPP_SERIAL
:
3731 * We use Ethernet protocol types inside libpcap;
3732 * map them to the corresponding PPP protocol types.
3734 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3735 ethertype_to_ppptype(cstate
, ll_proto
));
3740 * We use Ethernet protocol types inside libpcap;
3741 * map them to the corresponding PPP protocol types.
3747 * Also check for Van Jacobson-compressed IP.
3748 * XXX - do this for other forms of PPP?
3750 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3751 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3753 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3758 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3759 ethertype_to_ppptype(cstate
, ll_proto
));
3769 return (gen_loopback_linktype(cstate
, AF_INET
));
3771 case ETHERTYPE_IPV6
:
3773 * AF_ values may, unfortunately, be platform-
3774 * dependent; AF_INET isn't, because everybody
3775 * used 4.2BSD's value, but AF_INET6 is, because
3776 * 4.2BSD didn't have a value for it (given that
3777 * IPv6 didn't exist back in the early 1980's),
3778 * and they all picked their own values.
3780 * This means that, if we're reading from a
3781 * savefile, we need to check for all the
3784 * If we're doing a live capture, we only need
3785 * to check for this platform's value; however,
3786 * Npcap uses 24, which isn't Windows's AF_INET6
3787 * value. (Given the multiple different values,
3788 * programs that read pcap files shouldn't be
3789 * checking for their platform's AF_INET6 value
3790 * anyway, they should check for all of the
3791 * possible values. and they might as well do
3792 * that even for live captures.)
3794 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3796 * Savefile - check for all three
3797 * possible IPv6 values.
3799 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3800 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3802 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3807 * Live capture, so we only need to
3808 * check for the value used on this
3813 * Npcap doesn't use Windows's AF_INET6,
3814 * as that collides with AF_IPX on
3815 * some BSDs (both have the value 23).
3816 * Instead, it uses 24.
3818 return (gen_loopback_linktype(cstate
, 24));
3821 return (gen_loopback_linktype(cstate
, AF_INET6
));
3822 #else /* AF_INET6 */
3824 * I guess this platform doesn't support
3825 * IPv6, so we just reject all packets.
3827 return gen_false(cstate
);
3828 #endif /* AF_INET6 */
3834 * Not a type on which we support filtering.
3835 * XXX - support those that have AF_ values
3836 * #defined on this platform, at least?
3838 return gen_false(cstate
);
3843 * af field is host byte order in contrast to the rest of
3846 if (ll_proto
== ETHERTYPE_IP
)
3847 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3849 else if (ll_proto
== ETHERTYPE_IPV6
)
3850 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3853 return gen_false(cstate
);
3857 case DLT_ARCNET_LINUX
:
3859 * XXX should we check for first fragment if the protocol
3865 return gen_false(cstate
);
3867 case ETHERTYPE_IPV6
:
3868 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3872 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3874 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3880 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3882 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3887 case ETHERTYPE_REVARP
:
3888 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3891 case ETHERTYPE_ATALK
:
3892 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3899 case ETHERTYPE_ATALK
:
3900 return gen_true(cstate
);
3902 return gen_false(cstate
);
3908 * XXX - assumes a 2-byte Frame Relay header with
3909 * DLCI and flags. What if the address is longer?
3915 * Check for the special NLPID for IP.
3917 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3919 case ETHERTYPE_IPV6
:
3921 * Check for the special NLPID for IPv6.
3923 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3927 * Check for several OSI protocols.
3929 * Frame Relay packets typically have an OSI
3930 * NLPID at the beginning; we check for each
3933 * What we check for is the NLPID and a frame
3934 * control field of UI, i.e. 0x03 followed
3937 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3938 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3939 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3945 return gen_false(cstate
);
3950 break; // not implemented
3952 case DLT_JUNIPER_MFR
:
3953 case DLT_JUNIPER_MLFR
:
3954 case DLT_JUNIPER_MLPPP
:
3955 case DLT_JUNIPER_ATM1
:
3956 case DLT_JUNIPER_ATM2
:
3957 case DLT_JUNIPER_PPPOE
:
3958 case DLT_JUNIPER_PPPOE_ATM
:
3959 case DLT_JUNIPER_GGSN
:
3960 case DLT_JUNIPER_ES
:
3961 case DLT_JUNIPER_MONITOR
:
3962 case DLT_JUNIPER_SERVICES
:
3963 case DLT_JUNIPER_ETHER
:
3964 case DLT_JUNIPER_PPP
:
3965 case DLT_JUNIPER_FRELAY
:
3966 case DLT_JUNIPER_CHDLC
:
3967 case DLT_JUNIPER_VP
:
3968 case DLT_JUNIPER_ST
:
3969 case DLT_JUNIPER_ISM
:
3970 case DLT_JUNIPER_VS
:
3971 case DLT_JUNIPER_SRX_E2E
:
3972 case DLT_JUNIPER_FIBRECHANNEL
:
3973 case DLT_JUNIPER_ATM_CEMIC
:
3975 /* just lets verify the magic number for now -
3976 * on ATM we may have up to 6 different encapsulations on the wire
3977 * and need a lot of heuristics to figure out that the payload
3980 * FIXME encapsulation specific BPF_ filters
3982 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3984 case DLT_BACNET_MS_TP
:
3985 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3988 return gen_ipnet_linktype(cstate
, ll_proto
);
3990 case DLT_LINUX_IRDA
:
3993 case DLT_MTP2_WITH_PHDR
:
3996 case DLT_LINUX_LAPD
:
3997 case DLT_USB_FREEBSD
:
3999 case DLT_USB_LINUX_MMAPPED
:
4001 case DLT_BLUETOOTH_HCI_H4
:
4002 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
4004 case DLT_CAN_SOCKETCAN
:
4005 case DLT_IEEE802_15_4
:
4006 case DLT_IEEE802_15_4_LINUX
:
4007 case DLT_IEEE802_15_4_NONASK_PHY
:
4008 case DLT_IEEE802_15_4_NOFCS
:
4009 case DLT_IEEE802_15_4_TAP
:
4010 case DLT_IEEE802_16_MAC_CPS_RADIO
:
4013 case DLT_IPMB_KONTRON
:
4017 /* Using the fixed-size NFLOG header it is possible to tell only
4018 * the address family of the packet, other meaningful data is
4019 * either missing or behind TLVs.
4021 break; // not implemented
4025 * Does this link-layer header type have a field
4026 * indicating the type of the next protocol? If
4027 * so, off_linktype.constant_part will be the offset of that
4028 * field in the packet; if not, it will be OFFSET_NOT_SET.
4030 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
4032 * Yes; assume it's an Ethernet type. (If
4033 * it's not, it needs to be handled specially
4036 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4037 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
4041 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
4042 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
4046 * Check for an LLC SNAP packet with a given organization code and
4047 * protocol type; we check the entire contents of the 802.2 LLC and
4048 * snap headers, checking for DSAP and SSAP of SNAP and a control
4049 * field of 0x03 in the LLC header, and for the specified organization
4050 * code and protocol type in the SNAP header.
4052 static struct block
*
4053 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
4055 u_char snapblock
[8];
4057 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
4058 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
4059 snapblock
[2] = 0x03; /* control = UI */
4060 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
4061 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
4062 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
4063 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
4064 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
4065 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
4069 * Generate code to match frames with an LLC header.
4071 static struct block
*
4072 gen_llc_internal(compiler_state_t
*cstate
)
4074 struct block
*b0
, *b1
;
4076 switch (cstate
->linktype
) {
4080 * We check for an Ethernet type field less or equal than
4081 * 1500, which means it's an 802.3 length field.
4083 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
4086 * Now check for the purported DSAP and SSAP not being
4087 * 0xFF, to rule out NetWare-over-802.3.
4089 b1
= gen_cmp_ne(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
4095 * We check for LLC traffic.
4097 return gen_atmtype_llc(cstate
);
4099 case DLT_IEEE802
: /* Token Ring */
4101 * XXX - check for LLC frames.
4103 return gen_true(cstate
);
4107 * XXX - check for LLC frames.
4109 return gen_true(cstate
);
4111 case DLT_ATM_RFC1483
:
4113 * For LLC encapsulation, these are defined to have an
4116 * For VC encapsulation, they don't, but there's no
4117 * way to check for that; the protocol used on the VC
4118 * is negotiated out of band.
4120 return gen_true(cstate
);
4122 case DLT_IEEE802_11
:
4123 case DLT_PRISM_HEADER
:
4124 case DLT_IEEE802_11_RADIO
:
4125 case DLT_IEEE802_11_RADIO_AVS
:
4128 * Check that we have a data frame.
4130 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4131 IEEE80211_FC0_TYPE_DATA
,
4132 IEEE80211_FC0_TYPE_MASK
);
4135 fail_kw_on_dlt(cstate
, "llc");
4141 gen_llc(compiler_state_t
*cstate
)
4144 * Catch errors reported by us and routines below us, and return NULL
4147 if (setjmp(cstate
->top_ctx
))
4150 return gen_llc_internal(cstate
);
4154 gen_llc_i(compiler_state_t
*cstate
)
4156 struct block
*b0
, *b1
;
4160 * Catch errors reported by us and routines below us, and return NULL
4163 if (setjmp(cstate
->top_ctx
))
4167 * Check whether this is an LLC frame.
4169 b0
= gen_llc_internal(cstate
);
4172 * Load the control byte and test the low-order bit; it must
4173 * be clear for I frames.
4175 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4176 b1
= gen_unset(cstate
, 0x01, s
);
4183 gen_llc_s(compiler_state_t
*cstate
)
4185 struct block
*b0
, *b1
;
4188 * Catch errors reported by us and routines below us, and return NULL
4191 if (setjmp(cstate
->top_ctx
))
4195 * Check whether this is an LLC frame.
4197 b0
= gen_llc_internal(cstate
);
4200 * Now compare the low-order 2 bit of the control byte against
4201 * the appropriate value for S frames.
4203 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4209 gen_llc_u(compiler_state_t
*cstate
)
4211 struct block
*b0
, *b1
;
4214 * Catch errors reported by us and routines below us, and return NULL
4217 if (setjmp(cstate
->top_ctx
))
4221 * Check whether this is an LLC frame.
4223 b0
= gen_llc_internal(cstate
);
4226 * Now compare the low-order 2 bit of the control byte against
4227 * the appropriate value for U frames.
4229 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4235 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4237 struct block
*b0
, *b1
;
4240 * Catch errors reported by us and routines below us, and return NULL
4243 if (setjmp(cstate
->top_ctx
))
4247 * Check whether this is an LLC frame.
4249 b0
= gen_llc_internal(cstate
);
4252 * Now check for an S frame with the appropriate type.
4254 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4260 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4262 struct block
*b0
, *b1
;
4265 * Catch errors reported by us and routines below us, and return NULL
4268 if (setjmp(cstate
->top_ctx
))
4272 * Check whether this is an LLC frame.
4274 b0
= gen_llc_internal(cstate
);
4277 * Now check for a U frame with the appropriate type.
4279 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4285 * Generate code to match a particular packet type, for link-layer types
4286 * using 802.2 LLC headers.
4288 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4289 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4291 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4292 * value, if <= ETHERMTU. We use that to determine whether to
4293 * match the DSAP or both DSAP and LSAP or to check the OUI and
4294 * protocol ID in a SNAP header.
4296 static struct block
*
4297 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4300 * XXX - handle token-ring variable-length header.
4306 case LLCSAP_NETBEUI
:
4308 * XXX - should we check both the DSAP and the
4309 * SSAP, like this, or should we check just the
4310 * DSAP, as we do for other SAP values?
4312 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4313 ((ll_proto
<< 8) | ll_proto
));
4317 * XXX - are there ever SNAP frames for IPX on
4318 * non-Ethernet 802.x networks?
4320 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4322 case ETHERTYPE_ATALK
:
4324 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4325 * SNAP packets with an organization code of
4326 * 0x080007 (Apple, for Appletalk) and a protocol
4327 * type of ETHERTYPE_ATALK (Appletalk).
4329 * XXX - check for an organization code of
4330 * encapsulated Ethernet as well?
4332 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4336 * XXX - we don't have to check for IPX 802.3
4337 * here, but should we check for the IPX Ethertype?
4339 if (ll_proto
<= ETHERMTU
) {
4340 assert_maxval(cstate
, "LLC DSAP", ll_proto
, UINT8_MAX
);
4342 * This is an LLC SAP value, so check
4345 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4347 assert_maxval(cstate
, "EtherType", ll_proto
, UINT16_MAX
);
4349 * This is an Ethernet type; we assume that it's
4350 * unlikely that it'll appear in the right place
4351 * at random, and therefore check only the
4352 * location that would hold the Ethernet type
4353 * in a SNAP frame with an organization code of
4354 * 0x000000 (encapsulated Ethernet).
4356 * XXX - if we were to check for the SNAP DSAP and
4357 * LSAP, as per XXX, and were also to check for an
4358 * organization code of 0x000000 (encapsulated
4359 * Ethernet), we'd do
4361 * return gen_snap(cstate, 0x000000, ll_proto);
4363 * here; for now, we don't, as per the above.
4364 * I don't know whether it's worth the extra CPU
4365 * time to do the right check or not.
4367 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4372 static struct block
*
4373 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4374 int dir
, u_int src_off
, u_int dst_off
)
4376 struct block
*b0
, *b1
;
4390 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4391 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4397 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4398 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4408 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4415 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4419 static struct block
*
4420 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4421 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4423 struct block
*b0
, *b1
;
4426 * Code below needs to access four separate 32-bit parts of the 128-bit
4427 * IPv6 address and mask. In some OSes this is as simple as using the
4428 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4429 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4430 * far as libpcap sees it. Hence copy the data before use to avoid
4431 * potential unaligned memory access and the associated compiler
4432 * warnings (whether genuine or not).
4434 bpf_u_int32 a
[4], m
[4];
4447 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4448 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4454 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4455 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4465 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4472 /* this order is important */
4473 memcpy(a
, addr
, sizeof(a
));
4474 memcpy(m
, mask
, sizeof(m
));
4475 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4476 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4478 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4480 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4486 // MAC-48 address matching with the address offsets parametrised.
4487 static struct block
*
4488 gen_mac48hostop(compiler_state_t
*cstate
, const u_char
*addr
, const int dir
,
4489 const u_int src_off
, const u_int dst_off
)
4491 struct block
*b0
, *b1
;
4495 return gen_bcmp(cstate
, OR_LINKHDR
, src_off
, 6, addr
);
4498 return gen_bcmp(cstate
, OR_LINKHDR
, dst_off
, 6, addr
);
4501 b0
= gen_mac48hostop(cstate
, addr
, Q_SRC
, src_off
, dst_off
);
4502 b1
= gen_mac48hostop(cstate
, addr
, Q_DST
, src_off
, dst_off
);
4508 b0
= gen_mac48hostop(cstate
, addr
, Q_SRC
, src_off
, dst_off
);
4509 b1
= gen_mac48hostop(cstate
, addr
, Q_DST
, src_off
, dst_off
);
4519 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4526 static struct block
*
4527 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4529 return gen_mac48hostop(cstate
, eaddr
, dir
, 6, 0);
4533 * Like gen_ehostop, but for DLT_FDDI
4535 static struct block
*
4536 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4538 return gen_mac48hostop(cstate
, eaddr
, dir
,
4539 6 + 1 + cstate
->pcap_fddipad
,
4540 0 + 1 + cstate
->pcap_fddipad
);
4544 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4546 static struct block
*
4547 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4549 return gen_mac48hostop(cstate
, eaddr
, dir
, 8, 2);
4553 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4554 * various 802.11 + radio headers.
4556 static struct block
*
4557 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4559 register struct block
*b0
, *b1
, *b2
;
4560 register struct slist
*s
;
4562 #ifdef ENABLE_WLAN_FILTERING_PATCH
4565 * We need to disable the optimizer because the optimizer is buggy
4566 * and wipes out some LD instructions generated by the below
4567 * code to validate the Frame Control bits
4569 cstate
->no_optimize
= 1;
4570 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4577 * For control frames, there is no SA.
4579 * For management frames, SA is at an
4580 * offset of 10 from the beginning of
4583 * For data frames, SA is at an offset
4584 * of 10 from the beginning of the packet
4585 * if From DS is clear, at an offset of
4586 * 16 from the beginning of the packet
4587 * if From DS is set and To DS is clear,
4588 * and an offset of 24 from the beginning
4589 * of the packet if From DS is set and To DS
4594 * Generate the tests to be done for data frames
4597 * First, check for To DS set, i.e. check "link[1] & 0x01".
4599 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4600 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4603 * If To DS is set, the SA is at 24.
4605 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4609 * Now, check for To DS not set, i.e. check
4610 * "!(link[1] & 0x01)".
4612 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4613 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4616 * If To DS is not set, the SA is at 16.
4618 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4622 * Now OR together the last two checks. That gives
4623 * the complete set of checks for data frames with
4629 * Now check for From DS being set, and AND that with
4630 * the ORed-together checks.
4632 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4633 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4637 * Now check for data frames with From DS not set.
4639 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4640 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_FROMDS
, s
);
4643 * If From DS isn't set, the SA is at 10.
4645 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4649 * Now OR together the checks for data frames with
4650 * From DS not set and for data frames with From DS
4651 * set; that gives the checks done for data frames.
4656 * Now check for a data frame.
4657 * I.e, check "link[0] & 0x08".
4659 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4660 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4663 * AND that with the checks done for data frames.
4668 * If the high-order bit of the type value is 0, this
4669 * is a management frame.
4670 * I.e, check "!(link[0] & 0x08)".
4672 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4673 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4676 * For management frames, the SA is at 10.
4678 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4682 * OR that with the checks done for data frames.
4683 * That gives the checks done for management and
4689 * If the low-order bit of the type value is 1,
4690 * this is either a control frame or a frame
4691 * with a reserved type, and thus not a
4694 * I.e., check "!(link[0] & 0x04)".
4696 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4697 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4700 * AND that with the checks for data and management
4710 * For control frames, there is no DA.
4712 * For management frames, DA is at an
4713 * offset of 4 from the beginning of
4716 * For data frames, DA is at an offset
4717 * of 4 from the beginning of the packet
4718 * if To DS is clear and at an offset of
4719 * 16 from the beginning of the packet
4724 * Generate the tests to be done for data frames.
4726 * First, check for To DS set, i.e. "link[1] & 0x01".
4728 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4729 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4732 * If To DS is set, the DA is at 16.
4734 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4738 * Now, check for To DS not set, i.e. check
4739 * "!(link[1] & 0x01)".
4741 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4742 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
4745 * If To DS is not set, the DA is at 4.
4747 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4751 * Now OR together the last two checks. That gives
4752 * the complete set of checks for data frames.
4757 * Now check for a data frame.
4758 * I.e, check "link[0] & 0x08".
4760 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4761 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4764 * AND that with the checks done for data frames.
4769 * If the high-order bit of the type value is 0, this
4770 * is a management frame.
4771 * I.e, check "!(link[0] & 0x08)".
4773 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4774 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4777 * For management frames, the DA is at 4.
4779 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4783 * OR that with the checks done for data frames.
4784 * That gives the checks done for management and
4790 * If the low-order bit of the type value is 1,
4791 * this is either a control frame or a frame
4792 * with a reserved type, and thus not a
4795 * I.e., check "!(link[0] & 0x04)".
4797 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4798 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
4801 * AND that with the checks for data and management
4808 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4809 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4815 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4816 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4821 * XXX - add BSSID keyword?
4824 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4828 * Not present in CTS or ACK control frames.
4830 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4831 IEEE80211_FC0_TYPE_MASK
);
4832 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4833 IEEE80211_FC0_SUBTYPE_MASK
);
4834 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4835 IEEE80211_FC0_SUBTYPE_MASK
);
4838 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4844 * Not present in control frames.
4846 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4847 IEEE80211_FC0_TYPE_MASK
);
4848 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4854 * Present only if the direction mask has both "From DS"
4855 * and "To DS" set. Neither control frames nor management
4856 * frames should have both of those set, so we don't
4857 * check the frame type.
4859 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4860 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4861 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4867 * Not present in management frames; addr1 in other
4872 * If the high-order bit of the type value is 0, this
4873 * is a management frame.
4874 * I.e, check "(link[0] & 0x08)".
4876 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4877 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
4882 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4885 * AND that with the check of addr1.
4892 * Not present in management frames; addr2, if present,
4897 * Not present in CTS or ACK control frames.
4899 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4900 IEEE80211_FC0_TYPE_MASK
);
4901 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4902 IEEE80211_FC0_SUBTYPE_MASK
);
4903 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4904 IEEE80211_FC0_SUBTYPE_MASK
);
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
);
4917 * AND that with the check for frames other than
4918 * CTS and ACK frames.
4925 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4934 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4935 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4936 * as the RFC states.)
4938 static struct block
*
4939 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4941 return gen_mac48hostop(cstate
, eaddr
, dir
, 10, 2);
4945 * This is quite tricky because there may be pad bytes in front of the
4946 * DECNET header, and then there are two possible data packet formats that
4947 * carry both src and dst addresses, plus 5 packet types in a format that
4948 * carries only the src node, plus 2 types that use a different format and
4949 * also carry just the src node.
4953 * Instead of doing those all right, we just look for data packets with
4954 * 0 or 1 bytes of padding. If you want to look at other packets, that
4955 * will require a lot more hacking.
4957 * To add support for filtering on DECNET "areas" (network numbers)
4958 * one would want to add a "mask" argument to this routine. That would
4959 * make the filter even more inefficient, although one could be clever
4960 * and not generate masking instructions if the mask is 0xFFFF.
4962 static struct block
*
4963 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4965 struct block
*b0
, *b1
, *b2
, *tmp
;
4966 u_int offset_lh
; /* offset if long header is received */
4967 u_int offset_sh
; /* offset if short header is received */
4972 offset_sh
= 1; /* follows flags */
4973 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4977 offset_sh
= 3; /* follows flags, dstnode */
4978 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4982 /* Inefficient because we do our Calvinball dance twice */
4983 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4984 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4990 /* Inefficient because we do our Calvinball dance twice */
4991 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4992 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5002 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5010 * In a DECnet message inside an Ethernet frame the first two bytes
5011 * immediately after EtherType are the [litle-endian] DECnet message
5012 * length, which is irrelevant in this context.
5014 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5015 * 8-bit bitmap of the optional padding before the packet route header.
5016 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5017 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5018 * means there aren't any PAD bytes after the bitmap, so the header
5019 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5020 * is set to 0, thus the header begins at the third byte.
5022 * The header can be in several (as mentioned above) formats, all of
5023 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5024 * (PF, "pad field") set to 0 regardless of any padding present before
5025 * the header. "Short header" means bits 0-2 of the bitmap encode the
5026 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5028 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5029 * values and the masks, this maps to the required single bytes of
5030 * the message correctly on both big-endian and little-endian hosts.
5031 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5032 * because the wire encoding is little-endian and BPF multiple-byte
5033 * loads are big-endian. When the destination address is near enough
5034 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5037 /* Check for pad = 1, long header case */
5038 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5039 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5040 BPF_H
, SWAPSHORT(addr
));
5042 /* Check for pad = 0, long header case */
5043 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5044 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5048 /* Check for pad = 1, short header case */
5050 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5051 0x81020000U
| SWAPSHORT(addr
),
5054 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5055 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5060 /* Check for pad = 0, short header case */
5062 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5063 0x02000000U
| SWAPSHORT(addr
) << 8,
5066 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5067 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5077 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5078 * test the bottom-of-stack bit, and then check the version number
5079 * field in the IP header.
5081 static struct block
*
5082 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5084 struct block
*b0
, *b1
;
5089 /* match the bottom-of-stack bit */
5090 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5091 /* match the IPv4 version number */
5092 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5096 case ETHERTYPE_IPV6
:
5097 /* match the bottom-of-stack bit */
5098 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5099 /* match the IPv6 version number */
5100 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5105 /* FIXME add other L3 proto IDs */
5106 bpf_error(cstate
, "unsupported protocol over mpls");
5111 static struct block
*
5112 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5113 int proto
, int dir
, int type
)
5115 struct block
*b0
, *b1
;
5120 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5122 * Only check for non-IPv4 addresses if we're not
5123 * checking MPLS-encapsulated packets.
5125 if (cstate
->label_stack_depth
== 0) {
5126 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5128 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5134 // "link net NETNAME" and variations thereof
5135 break; // invalid qualifier
5138 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5139 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5144 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5145 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5150 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5151 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5162 break; // invalid qualifier
5165 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5166 b1
= gen_dnhostop(cstate
, addr
, dir
);
5197 break; // invalid qualifier
5202 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5203 type
== Q_NET
? "ip net" : "ip host");
5208 static struct block
*
5209 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5210 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5212 struct block
*b0
, *b1
;
5218 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5219 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5261 break; // invalid qualifier
5266 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5267 type
== Q_NET
? "ip6 net" : "ip6 host");
5274 * This primitive is non-directional by design, so the grammar does not allow
5275 * to qualify it with a direction.
5277 static struct block
*
5278 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5279 struct addrinfo
*alist
, int proto
)
5281 struct block
*b0
, *b1
, *tmp
;
5282 struct addrinfo
*ai
;
5283 struct sockaddr_in
*sin
;
5290 switch (cstate
->linktype
) {
5292 case DLT_NETANALYZER
:
5293 case DLT_NETANALYZER_TRANSPARENT
:
5294 b1
= gen_prevlinkhdr_check(cstate
);
5295 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5300 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5303 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5305 case DLT_IEEE802_11
:
5306 case DLT_PRISM_HEADER
:
5307 case DLT_IEEE802_11_RADIO_AVS
:
5308 case DLT_IEEE802_11_RADIO
:
5310 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5312 case DLT_IP_OVER_FC
:
5313 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5317 * This is LLC-multiplexed traffic; if it were
5318 * LANE, cstate->linktype would have been set to
5324 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5327 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5329 * Does it have an address?
5331 if (ai
->ai_addr
!= NULL
) {
5333 * Yes. Is it an IPv4 address?
5335 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5337 * Generate an entry for it.
5339 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5340 tmp
= gen_host(cstate
,
5341 ntohl(sin
->sin_addr
.s_addr
),
5342 0xffffffff, proto
, Q_OR
, Q_HOST
);
5344 * Is it the *first* IPv4 address?
5348 * Yes, so start with it.
5353 * No, so OR it into the
5365 * No IPv4 addresses found.
5373 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5378 static struct block
*
5379 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5387 return gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
);
5390 return gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
);
5393 return gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
);
5396 return gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
);
5398 #ifndef IPPROTO_IGMP
5399 #define IPPROTO_IGMP 2
5403 return gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
);
5405 #ifndef IPPROTO_IGRP
5406 #define IPPROTO_IGRP 9
5409 return gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
);
5412 #define IPPROTO_PIM 103
5416 return gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
);
5418 #ifndef IPPROTO_VRRP
5419 #define IPPROTO_VRRP 112
5423 return gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
);
5425 #ifndef IPPROTO_CARP
5426 #define IPPROTO_CARP 112
5430 return gen_proto(cstate
, IPPROTO_CARP
, Q_IP
);
5433 return gen_linktype(cstate
, ETHERTYPE_IP
);
5436 return gen_linktype(cstate
, ETHERTYPE_ARP
);
5439 return gen_linktype(cstate
, ETHERTYPE_REVARP
);
5442 break; // invalid syntax
5445 return gen_linktype(cstate
, ETHERTYPE_ATALK
);
5448 return gen_linktype(cstate
, ETHERTYPE_AARP
);
5451 return gen_linktype(cstate
, ETHERTYPE_DN
);
5454 return gen_linktype(cstate
, ETHERTYPE_SCA
);
5457 return gen_linktype(cstate
, ETHERTYPE_LAT
);
5460 return gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5463 return gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5466 return gen_linktype(cstate
, ETHERTYPE_IPV6
);
5468 #ifndef IPPROTO_ICMPV6
5469 #define IPPROTO_ICMPV6 58
5472 return gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
);
5475 #define IPPROTO_AH 51
5478 return gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
);
5481 #define IPPROTO_ESP 50
5484 return gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
);
5487 return gen_linktype(cstate
, LLCSAP_ISONS
);
5490 return gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
);
5493 return gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
5495 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5496 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5497 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5499 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5501 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5503 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5507 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5508 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5509 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5511 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5513 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5515 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5519 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5520 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5521 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5523 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
);
5528 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5529 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5534 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5535 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5537 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5539 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5544 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5545 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5550 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5551 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5556 return gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
);
5559 return gen_linktype(cstate
, LLCSAP_8021D
);
5562 return gen_linktype(cstate
, LLCSAP_IPX
);
5565 return gen_linktype(cstate
, LLCSAP_NETBEUI
);
5568 break; // invalid syntax
5573 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5577 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5580 * Catch errors reported by us and routines below us, and return NULL
5583 if (setjmp(cstate
->top_ctx
))
5586 return gen_proto_abbrev_internal(cstate
, proto
);
5589 static struct block
*
5590 gen_ip_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5592 return gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5595 static struct block
*
5596 gen_ip6_proto(compiler_state_t
*cstate
, const uint8_t proto
)
5598 return gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5601 static struct block
*
5602 gen_ipfrag(compiler_state_t
*cstate
)
5606 /* not IPv4 frag other than the first frag */
5607 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5608 return gen_unset(cstate
, 0x1fff, s
);
5612 * Generate a comparison to a port value in the transport-layer header
5613 * at the specified offset from the beginning of that header.
5615 * XXX - this handles a variable-length prefix preceding the link-layer
5616 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5617 * variable-length link-layer headers (such as Token Ring or 802.11
5620 static struct block
*
5621 gen_portatom(compiler_state_t
*cstate
, int off
, uint16_t v
)
5623 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5626 static struct block
*
5627 gen_portatom6(compiler_state_t
*cstate
, int off
, uint16_t v
)
5629 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5632 static struct block
*
5633 gen_port(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5635 struct block
*b1
, *tmp
;
5639 b1
= gen_portatom(cstate
, 0, port
);
5643 b1
= gen_portatom(cstate
, 2, port
);
5647 tmp
= gen_portatom(cstate
, 0, port
);
5648 b1
= gen_portatom(cstate
, 2, port
);
5654 tmp
= gen_portatom(cstate
, 0, port
);
5655 b1
= gen_portatom(cstate
, 2, port
);
5665 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5673 return gen_port_common(cstate
, proto
, b1
);
5676 static struct block
*
5677 gen_port_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5679 struct block
*b0
, *tmp
;
5684 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5685 * not LLC encapsulation with LLCSAP_IP.
5687 * For IEEE 802 networks - which includes 802.5 token ring
5688 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5689 * says that SNAP encapsulation is used, not LLC encapsulation
5692 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5693 * RFC 2225 say that SNAP encapsulation is used, not LLC
5694 * encapsulation with LLCSAP_IP.
5696 * So we always check for ETHERTYPE_IP.
5698 * At the time of this writing all three L4 protocols the "port" and
5699 * "portrange" primitives support (TCP, UDP and SCTP) have the source
5700 * and the destination ports identically encoded in the transport
5701 * protocol header. So without a proto qualifier the only difference
5702 * between the implemented cases is the protocol number and all other
5703 * checks need to be made exactly once.
5705 * If the expression syntax in future starts to support ports for
5706 * another L4 protocol that has unsigned integer ports encoded using a
5707 * different size and/or offset, this will require a different code.
5713 tmp
= gen_ip_proto(cstate
, (uint8_t)proto
);
5717 tmp
= gen_ip_proto(cstate
, IPPROTO_UDP
);
5718 gen_or(gen_ip_proto(cstate
, IPPROTO_TCP
), tmp
);
5719 gen_or(gen_ip_proto(cstate
, IPPROTO_SCTP
), tmp
);
5725 // Not a fragment other than the first fragment.
5726 b0
= gen_ipfrag(cstate
);
5730 gen_and(gen_linktype(cstate
, ETHERTYPE_IP
), b1
);
5734 static struct block
*
5735 gen_port6(compiler_state_t
*cstate
, uint16_t port
, int proto
, int dir
)
5737 struct block
*b1
, *tmp
;
5741 b1
= gen_portatom6(cstate
, 0, port
);
5745 b1
= gen_portatom6(cstate
, 2, port
);
5749 tmp
= gen_portatom6(cstate
, 0, port
);
5750 b1
= gen_portatom6(cstate
, 2, port
);
5756 tmp
= gen_portatom6(cstate
, 0, port
);
5757 b1
= gen_portatom6(cstate
, 2, port
);
5765 return gen_port6_common(cstate
, proto
, b1
);
5768 static struct block
*
5769 gen_port6_common(compiler_state_t
*cstate
, int proto
, struct block
*b1
)
5773 // "ip6 proto 'ip_proto'"
5778 tmp
= gen_ip6_proto(cstate
, (uint8_t)proto
);
5782 // Same as in gen_port_common().
5783 tmp
= gen_ip6_proto(cstate
, IPPROTO_UDP
);
5784 gen_or(gen_ip6_proto(cstate
, IPPROTO_TCP
), tmp
);
5785 gen_or(gen_ip6_proto(cstate
, IPPROTO_SCTP
), tmp
);
5791 // XXX - catch the first fragment of a fragmented packet?
5793 // "link proto \ip6"
5794 gen_and(gen_linktype(cstate
, ETHERTYPE_IPV6
), b1
);
5798 /* gen_portrange code */
5799 static struct block
*
5800 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5804 return gen_portatom(cstate
, off
, v1
);
5806 struct block
*b1
, *b2
;
5808 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, min(v1
, v2
));
5809 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, max(v1
, v2
));
5816 static struct block
*
5817 gen_portrange(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5820 struct block
*b1
, *tmp
;
5824 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5828 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5832 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5833 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5839 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5840 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5850 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5858 return gen_port_common(cstate
, proto
, b1
);
5861 static struct block
*
5862 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, uint16_t v1
,
5866 return gen_portatom6(cstate
, off
, v1
);
5868 struct block
*b1
, *b2
;
5870 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, min(v1
, v2
));
5871 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, max(v1
, v2
));
5878 static struct block
*
5879 gen_portrange6(compiler_state_t
*cstate
, uint16_t port1
, uint16_t port2
,
5882 struct block
*b1
, *tmp
;
5886 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5890 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5894 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5895 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5901 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5902 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5910 return gen_port6_common(cstate
, proto
, b1
);
5914 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5923 v
= pcap_nametoproto(name
);
5924 if (v
== PROTO_UNDEF
)
5925 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5929 /* XXX should look up h/w protocol type based on cstate->linktype */
5930 v
= pcap_nametoeproto(name
);
5931 if (v
== PROTO_UNDEF
) {
5932 v
= pcap_nametollc(name
);
5933 if (v
== PROTO_UNDEF
)
5934 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5939 if (strcmp(name
, "esis") == 0)
5941 else if (strcmp(name
, "isis") == 0)
5943 else if (strcmp(name
, "clnp") == 0)
5946 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5956 #if !defined(NO_PROTOCHAIN)
5958 * This primitive is non-directional by design, so the grammar does not allow
5959 * to qualify it with a direction.
5961 static struct block
*
5962 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
5964 struct block
*b0
, *b
;
5965 struct slist
*s
[100];
5966 int fix2
, fix3
, fix4
, fix5
;
5967 int ahcheck
, again
, end
;
5969 int reg2
= alloc_reg(cstate
);
5971 memset(s
, 0, sizeof(s
));
5972 fix3
= fix4
= fix5
= 0;
5979 b0
= gen_protochain(cstate
, v
, Q_IP
);
5980 b
= gen_protochain(cstate
, v
, Q_IPV6
);
5984 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
5989 * We don't handle variable-length prefixes before the link-layer
5990 * header, or variable-length link-layer headers, here yet.
5991 * We might want to add BPF instructions to do the protochain
5992 * work, to simplify that and, on platforms that have a BPF
5993 * interpreter with the new instructions, let the filtering
5994 * be done in the kernel. (We already require a modified BPF
5995 * engine to do the protochain stuff, to support backward
5996 * branches, and backward branch support is unlikely to appear
5997 * in kernel BPF engines.)
5999 if (cstate
->off_linkpl
.is_variable
)
6000 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6003 * To quote a comment in optimize.c:
6005 * "These data structures are used in a Cocke and Schwartz style
6006 * value numbering scheme. Since the flowgraph is acyclic,
6007 * exit values can be propagated from a node's predecessors
6008 * provided it is uniquely defined."
6010 * "Acyclic" means "no backward branches", which means "no
6011 * loops", so we have to turn the optimizer off.
6013 cstate
->no_optimize
= 1;
6016 * s[0] is a dummy entry to protect other BPF insn from damage
6017 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6018 * hard to find interdependency made by jump table fixup.
6021 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6026 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6029 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6030 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6032 /* X = ip->ip_hl << 2 */
6033 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6034 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6039 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6041 /* A = ip6->ip_nxt */
6042 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6043 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6045 /* X = sizeof(struct ip6_hdr) */
6046 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6052 bpf_error(cstate
, "unsupported proto to gen_protochain");
6056 /* again: if (A == v) goto end; else fall through; */
6058 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6060 s
[i
]->s
.jt
= NULL
; /*later*/
6061 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6065 #ifndef IPPROTO_NONE
6066 #define IPPROTO_NONE 59
6068 /* if (A == IPPROTO_NONE) goto end */
6069 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6070 s
[i
]->s
.jt
= NULL
; /*later*/
6071 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6072 s
[i
]->s
.k
= IPPROTO_NONE
;
6073 s
[fix5
]->s
.jf
= s
[i
];
6077 if (proto
== Q_IPV6
) {
6078 int v6start
, v6end
, v6advance
, j
;
6081 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6082 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6083 s
[i
]->s
.jt
= NULL
; /*later*/
6084 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6085 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6086 s
[fix2
]->s
.jf
= s
[i
];
6088 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6089 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6090 s
[i
]->s
.jt
= NULL
; /*later*/
6091 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6092 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6094 /* if (A == IPPROTO_ROUTING) goto v6advance */
6095 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6096 s
[i
]->s
.jt
= NULL
; /*later*/
6097 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6098 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6100 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6101 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6102 s
[i
]->s
.jt
= NULL
; /*later*/
6103 s
[i
]->s
.jf
= NULL
; /*later*/
6104 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6114 * A = P[X + packet head];
6115 * X = X + (P[X + packet head + 1] + 1) * 8;
6117 /* A = P[X + packet head] */
6118 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6119 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6122 s
[i
] = new_stmt(cstate
, BPF_ST
);
6125 /* A = P[X + packet head + 1]; */
6126 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6127 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6130 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6134 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6138 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6142 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6145 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6149 /* goto again; (must use BPF_JA for backward jump) */
6150 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6151 s
[i
]->s
.k
= again
- i
- 1;
6152 s
[i
- 1]->s
.jf
= s
[i
];
6156 for (j
= v6start
; j
<= v6end
; j
++)
6157 s
[j
]->s
.jt
= s
[v6advance
];
6160 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6162 s
[fix2
]->s
.jf
= s
[i
];
6168 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6169 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6170 s
[i
]->s
.jt
= NULL
; /*later*/
6171 s
[i
]->s
.jf
= NULL
; /*later*/
6172 s
[i
]->s
.k
= IPPROTO_AH
;
6174 s
[fix3
]->s
.jf
= s
[ahcheck
];
6181 * X = X + (P[X + 1] + 2) * 4;
6184 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
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
);
6195 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6198 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6202 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6204 /* A = P[X + packet head] */
6205 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6206 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6209 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6213 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6217 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6220 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6224 /* goto again; (must use BPF_JA for backward jump) */
6225 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6226 s
[i
]->s
.k
= again
- i
- 1;
6231 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6233 s
[fix2
]->s
.jt
= s
[end
];
6234 s
[fix4
]->s
.jf
= s
[end
];
6235 s
[fix5
]->s
.jt
= s
[end
];
6242 for (i
= 0; i
< max
- 1; i
++)
6243 s
[i
]->next
= s
[i
+ 1];
6244 s
[max
- 1]->next
= NULL
;
6248 * Remember, s[0] is dummy.
6250 b
= gen_jmp(cstate
, BPF_JEQ
, v
, s
[1]);
6252 free_reg(cstate
, reg2
);
6257 #endif /* !defined(NO_PROTOCHAIN) */
6260 * Generate code that checks whether the packet is a packet for protocol
6261 * <proto> and whether the type field in that protocol's header has
6262 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6263 * IP packet and checks the protocol number in the IP header against <v>.
6265 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6266 * against Q_IP and Q_IPV6.
6268 * This primitive is non-directional by design, so the grammar does not allow
6269 * to qualify it with a direction.
6271 static struct block
*
6272 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6274 struct block
*b0
, *b1
;
6279 b0
= gen_proto(cstate
, v
, Q_IP
);
6280 b1
= gen_proto(cstate
, v
, Q_IPV6
);
6285 return gen_linktype(cstate
, v
);
6288 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6290 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6291 * not LLC encapsulation with LLCSAP_IP.
6293 * For IEEE 802 networks - which includes 802.5 token ring
6294 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6295 * says that SNAP encapsulation is used, not LLC encapsulation
6298 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6299 * RFC 2225 say that SNAP encapsulation is used, not LLC
6300 * encapsulation with LLCSAP_IP.
6302 * So we always check for ETHERTYPE_IP.
6304 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6305 // 0 <= v <= UINT8_MAX
6306 b1
= gen_ip_proto(cstate
, (uint8_t)v
);
6324 break; // invalid qualifier
6327 assert_maxval(cstate
, "protocol number", v
, UINT8_MAX
);
6328 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6330 * Also check for a fragment header before the final
6333 b2
= gen_ip6_proto(cstate
, IPPROTO_FRAGMENT
);
6334 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6336 // 0 <= v <= UINT8_MAX
6337 b2
= gen_ip6_proto(cstate
, (uint8_t)v
);
6348 break; // invalid qualifier
6351 assert_maxval(cstate
, "ISO protocol", v
, UINT8_MAX
);
6352 switch (cstate
->linktype
) {
6356 * Frame Relay packets typically have an OSI
6357 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6358 * generates code to check for all the OSI
6359 * NLPIDs, so calling it and then adding a check
6360 * for the particular NLPID for which we're
6361 * looking is bogus, as we can just check for
6364 * What we check for is the NLPID and a frame
6365 * control field value of UI, i.e. 0x03 followed
6368 * XXX - assumes a 2-byte Frame Relay header with
6369 * DLCI and flags. What if the address is longer?
6371 * XXX - what about SNAP-encapsulated frames?
6373 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6379 * Cisco uses an Ethertype lookalike - for OSI,
6382 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6383 /* OSI in C-HDLC is stuffed with a fudge byte */
6384 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6389 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6390 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6396 break; // invalid qualifier
6399 assert_maxval(cstate
, "IS-IS PDU type", v
, ISIS_PDU_TYPE_MAX
);
6400 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
6402 * 4 is the offset of the PDU type relative to the IS-IS
6404 * Except when it is not, see above.
6406 unsigned pdu_type_offset
;
6407 switch (cstate
->linktype
) {
6410 pdu_type_offset
= 5;
6413 pdu_type_offset
= 4;
6415 b1
= gen_mcmp(cstate
, OR_LINKPL_NOSNAP
, pdu_type_offset
, BPF_B
,
6416 v
, ISIS_PDU_TYPE_MAX
);
6433 break; // invalid qualifier
6439 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6444 * Convert a non-numeric name to a port number.
6447 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6449 struct addrinfo hints
, *res
, *ai
;
6451 struct sockaddr_in
*in4
;
6453 struct sockaddr_in6
*in6
;
6458 * We check for both TCP and UDP in case there are
6459 * ambiguous entries.
6461 memset(&hints
, 0, sizeof(hints
));
6462 hints
.ai_family
= PF_UNSPEC
;
6463 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6464 hints
.ai_protocol
= ipproto
;
6465 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6472 * No such port. Just return -1.
6479 * We don't use strerror() because it's not
6480 * guaranteed to be thread-safe on all platforms
6481 * (probably because it might use a non-thread-local
6482 * buffer into which to format an error message
6483 * if the error code isn't one for which it has
6484 * a canned string; three cheers for C string
6487 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6489 port
= -2; /* a real error */
6495 * This is a real error, not just "there's
6496 * no such service name".
6498 * We don't use gai_strerror() because it's not
6499 * guaranteed to be thread-safe on all platforms
6500 * (probably because it might use a non-thread-local
6501 * buffer into which to format an error message
6502 * if the error code isn't one for which it has
6503 * a canned string; three cheers for C string
6506 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6508 port
= -2; /* a real error */
6513 * OK, we found it. Did it find anything?
6515 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6517 * Does it have an address?
6519 if (ai
->ai_addr
!= NULL
) {
6521 * Yes. Get a port number; we're done.
6523 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6524 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6525 port
= ntohs(in4
->sin_port
);
6529 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6530 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6531 port
= ntohs(in6
->sin6_port
);
6543 * Convert a string to a port number.
6546 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6556 * See if it's a number.
6558 ret
= stoulen(string
, string_size
, &val
, cstate
);
6562 /* Unknown port type - it's just a number. */
6563 *proto
= PROTO_UNDEF
;
6566 case STOULEN_NOT_OCTAL_NUMBER
:
6567 case STOULEN_NOT_HEX_NUMBER
:
6568 case STOULEN_NOT_DECIMAL_NUMBER
:
6570 * Not a valid number; try looking it up as a port.
6572 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6573 memcpy(cpy
, string
, string_size
);
6574 cpy
[string_size
] = '\0';
6575 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6576 if (tcp_port
== -2) {
6578 * We got a hard error; the error string has
6582 longjmp(cstate
->top_ctx
, 1);
6585 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6586 if (udp_port
== -2) {
6588 * We got a hard error; the error string has
6592 longjmp(cstate
->top_ctx
, 1);
6597 * We need to check /etc/services for ambiguous entries.
6598 * If we find an ambiguous entry, and it has the
6599 * same port number, change the proto to PROTO_UNDEF
6600 * so both TCP and UDP will be checked.
6602 if (tcp_port
>= 0) {
6603 val
= (bpf_u_int32
)tcp_port
;
6604 *proto
= IPPROTO_TCP
;
6605 if (udp_port
>= 0) {
6606 if (udp_port
== tcp_port
)
6607 *proto
= PROTO_UNDEF
;
6610 /* Can't handle ambiguous names that refer
6611 to different port numbers. */
6612 warning("ambiguous port %s in /etc/services",
6619 if (udp_port
>= 0) {
6620 val
= (bpf_u_int32
)udp_port
;
6621 *proto
= IPPROTO_UDP
;
6625 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6627 longjmp(cstate
->top_ctx
, 1);
6634 /* Error already set. */
6635 longjmp(cstate
->top_ctx
, 1);
6642 /* Should not happen */
6643 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6644 longjmp(cstate
->top_ctx
, 1);
6651 * Convert a string in the form PPP-PPP, which correspond to ports, to
6652 * a starting and ending port in a port range.
6655 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6656 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6659 const char *first
, *second
;
6660 size_t first_size
, second_size
;
6663 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6664 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6667 * Make sure there are no other hyphens.
6669 * XXX - we support named ports, but there are some port names
6670 * in /etc/services that include hyphens, so this would rule
6673 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6674 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6678 * Get the length of the first port.
6681 first_size
= hyphen_off
- string
;
6682 if (first_size
== 0) {
6683 /* Range of "-port", which we don't support. */
6684 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6688 * Try to convert it to a port.
6690 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6691 save_proto
= *proto
;
6694 * Get the length of the second port.
6696 second
= hyphen_off
+ 1;
6697 second_size
= strlen(second
);
6698 if (second_size
== 0) {
6699 /* Range of "port-", which we don't support. */
6700 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6704 * Try to convert it to a port.
6706 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6707 if (*proto
!= save_proto
)
6708 *proto
= PROTO_UNDEF
;
6712 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6714 int proto
= q
.proto
;
6718 bpf_u_int32 mask
, addr
;
6719 struct addrinfo
*res
, *res0
;
6720 struct sockaddr_in
*sin4
;
6723 struct sockaddr_in6
*sin6
;
6724 struct in6_addr mask128
;
6726 struct block
*b
, *tmp
;
6727 int port
, real_proto
;
6728 bpf_u_int32 port1
, port2
;
6731 * Catch errors reported by us and routines below us, and return NULL
6734 if (setjmp(cstate
->top_ctx
))
6740 addr
= pcap_nametonetaddr(name
);
6742 bpf_error(cstate
, "unknown network '%s'", name
);
6743 /* Left justify network addr and calculate its network mask */
6745 while (addr
&& (addr
& 0xff000000) == 0) {
6749 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6753 if (proto
== Q_LINK
) {
6754 switch (cstate
->linktype
) {
6757 case DLT_NETANALYZER
:
6758 case DLT_NETANALYZER_TRANSPARENT
:
6759 eaddr
= pcap_ether_hostton(name
);
6762 "unknown ether host '%s'", name
);
6763 tmp
= gen_prevlinkhdr_check(cstate
);
6764 b
= gen_ehostop(cstate
, eaddr
, dir
);
6771 eaddr
= pcap_ether_hostton(name
);
6774 "unknown FDDI host '%s'", name
);
6775 b
= gen_fhostop(cstate
, eaddr
, dir
);
6780 eaddr
= pcap_ether_hostton(name
);
6783 "unknown token ring host '%s'", name
);
6784 b
= gen_thostop(cstate
, eaddr
, dir
);
6788 case DLT_IEEE802_11
:
6789 case DLT_PRISM_HEADER
:
6790 case DLT_IEEE802_11_RADIO_AVS
:
6791 case DLT_IEEE802_11_RADIO
:
6793 eaddr
= pcap_ether_hostton(name
);
6796 "unknown 802.11 host '%s'", name
);
6797 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6801 case DLT_IP_OVER_FC
:
6802 eaddr
= pcap_ether_hostton(name
);
6805 "unknown Fibre Channel host '%s'", name
);
6806 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6811 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6812 } else if (proto
== Q_DECNET
) {
6814 * A long time ago on Ultrix libpcap supported
6815 * translation of DECnet host names into DECnet
6816 * addresses, but this feature is history now.
6818 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6821 memset(&mask128
, 0xff, sizeof(mask128
));
6823 res0
= res
= pcap_nametoaddrinfo(name
);
6825 bpf_error(cstate
, "unknown host '%s'", name
);
6832 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6833 tproto
== Q_DEFAULT
) {
6839 for (res
= res0
; res
; res
= res
->ai_next
) {
6840 switch (res
->ai_family
) {
6843 if (tproto
== Q_IPV6
)
6847 sin4
= (struct sockaddr_in
*)
6849 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6850 0xffffffff, tproto
, dir
, q
.addr
);
6854 if (tproto6
== Q_IP
)
6857 sin6
= (struct sockaddr_in6
*)
6859 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6860 &mask128
, tproto6
, dir
, q
.addr
);
6873 bpf_error(cstate
, "unknown host '%s'%s", name
,
6874 (proto
== Q_DEFAULT
)
6876 : " for specified address family");
6882 (void)port_pq_to_ipproto(cstate
, proto
, "port"); // validate only
6883 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6884 bpf_error(cstate
, "unknown port '%s'", name
);
6885 if (proto
== Q_UDP
) {
6886 if (real_proto
== IPPROTO_TCP
)
6887 bpf_error(cstate
, "port '%s' is tcp", name
);
6888 else if (real_proto
== IPPROTO_SCTP
)
6889 bpf_error(cstate
, "port '%s' is sctp", name
);
6891 /* override PROTO_UNDEF */
6892 real_proto
= IPPROTO_UDP
;
6894 if (proto
== Q_TCP
) {
6895 if (real_proto
== IPPROTO_UDP
)
6896 bpf_error(cstate
, "port '%s' is udp", name
);
6898 else if (real_proto
== IPPROTO_SCTP
)
6899 bpf_error(cstate
, "port '%s' is sctp", name
);
6901 /* override PROTO_UNDEF */
6902 real_proto
= IPPROTO_TCP
;
6904 if (proto
== Q_SCTP
) {
6905 if (real_proto
== IPPROTO_UDP
)
6906 bpf_error(cstate
, "port '%s' is udp", name
);
6908 else if (real_proto
== IPPROTO_TCP
)
6909 bpf_error(cstate
, "port '%s' is tcp", name
);
6911 /* override PROTO_UNDEF */
6912 real_proto
= IPPROTO_SCTP
;
6915 bpf_error(cstate
, "illegal port number %d < 0", port
);
6917 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6918 // real_proto can be PROTO_UNDEF
6919 b
= gen_port(cstate
, (uint16_t)port
, real_proto
, dir
);
6920 gen_or(gen_port6(cstate
, (uint16_t)port
, real_proto
, dir
), b
);
6924 (void)port_pq_to_ipproto(cstate
, proto
, "portrange"); // validate only
6925 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
6926 if (proto
== Q_UDP
) {
6927 if (real_proto
== IPPROTO_TCP
)
6928 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6929 else if (real_proto
== IPPROTO_SCTP
)
6930 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6932 /* override PROTO_UNDEF */
6933 real_proto
= IPPROTO_UDP
;
6935 if (proto
== Q_TCP
) {
6936 if (real_proto
== IPPROTO_UDP
)
6937 bpf_error(cstate
, "port in range '%s' is udp", name
);
6938 else if (real_proto
== IPPROTO_SCTP
)
6939 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6941 /* override PROTO_UNDEF */
6942 real_proto
= IPPROTO_TCP
;
6944 if (proto
== Q_SCTP
) {
6945 if (real_proto
== IPPROTO_UDP
)
6946 bpf_error(cstate
, "port in range '%s' is udp", name
);
6947 else if (real_proto
== IPPROTO_TCP
)
6948 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6950 /* override PROTO_UNDEF */
6951 real_proto
= IPPROTO_SCTP
;
6954 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6956 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6958 // real_proto can be PROTO_UNDEF
6959 b
= gen_portrange(cstate
, (uint16_t)port1
, (uint16_t)port2
,
6961 gen_or(gen_portrange6(cstate
, (uint16_t)port1
, (uint16_t)port2
,
6962 real_proto
, dir
), b
);
6967 eaddr
= pcap_ether_hostton(name
);
6969 bpf_error(cstate
, "unknown ether host: %s", name
);
6971 res
= pcap_nametoaddrinfo(name
);
6974 bpf_error(cstate
, "unknown host '%s'", name
);
6975 b
= gen_gateway(cstate
, eaddr
, res
, proto
);
6980 bpf_error(cstate
, "unknown host '%s'", name
);
6983 bpf_error(cstate
, "'gateway' not supported in this configuration");
6987 real_proto
= lookup_proto(cstate
, name
, proto
);
6988 if (real_proto
>= 0)
6989 return gen_proto(cstate
, real_proto
, proto
);
6991 bpf_error(cstate
, "unknown protocol: %s", name
);
6993 #if !defined(NO_PROTOCHAIN)
6995 real_proto
= lookup_proto(cstate
, name
, proto
);
6996 if (real_proto
>= 0)
6997 return gen_protochain(cstate
, real_proto
, proto
);
6999 bpf_error(cstate
, "unknown protocol: %s", name
);
7000 #endif /* !defined(NO_PROTOCHAIN) */
7011 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7012 bpf_u_int32 masklen
, struct qual q
)
7014 register int nlen
, mlen
;
7019 * Catch errors reported by us and routines below us, and return NULL
7022 if (setjmp(cstate
->top_ctx
))
7025 nlen
= pcapint_atoin(s1
, &n
);
7027 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7028 /* Promote short ipaddr */
7032 mlen
= pcapint_atoin(s2
, &m
);
7034 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7035 /* Promote short ipaddr */
7038 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7041 /* Convert mask len to mask */
7043 bpf_error(cstate
, "mask length must be <= 32");
7044 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7045 m
= (bpf_u_int32
)m64
;
7047 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7054 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7057 // Q_HOST and Q_GATEWAY only (see the grammar)
7058 bpf_error(cstate
, "Mask syntax for networks only");
7065 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7073 * Catch errors reported by us and routines below us, and return NULL
7076 if (setjmp(cstate
->top_ctx
))
7083 * v contains a 32-bit unsigned parsed from a string of the
7084 * form {N}, which could be decimal, hexadecimal or octal.
7085 * Although it would be possible to use the value as a raw
7086 * 16-bit DECnet address when the value fits into 16 bits, this
7087 * would be a questionable feature: DECnet address wire
7088 * encoding is little-endian, so this would not work as
7089 * intuitively as the same works for [big-endian] IPv4
7090 * addresses (0x01020304 means 1.2.3.4).
7092 if (proto
== Q_DECNET
)
7093 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7095 } else if (proto
== Q_DECNET
) {
7097 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7098 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7099 * for a valid DECnet address.
7101 vlen
= pcapint_atodn(s
, &v
);
7103 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7106 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7107 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7110 vlen
= pcapint_atoin(s
, &v
);
7112 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7120 if (proto
== Q_DECNET
)
7121 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7122 else if (proto
== Q_LINK
) {
7123 // "link (host|net) IPV4ADDR" and variations thereof
7124 bpf_error(cstate
, "illegal link layer address");
7127 if (s
== NULL
&& q
.addr
== Q_NET
) {
7128 /* Promote short net number */
7129 while (v
&& (v
& 0xff000000) == 0) {
7134 /* Promote short ipaddr */
7136 mask
<<= 32 - vlen
;
7138 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7142 proto
= port_pq_to_ipproto(cstate
, proto
, "port");
7145 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7147 // proto can be PROTO_UNDEF
7150 b
= gen_port(cstate
, (uint16_t)v
, proto
, dir
);
7151 gen_or(gen_port6(cstate
, (uint16_t)v
, proto
, dir
), b
);
7156 proto
= port_pq_to_ipproto(cstate
, proto
, "portrange");
7159 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7161 // proto can be PROTO_UNDEF
7164 b
= gen_portrange(cstate
, (uint16_t)v
, (uint16_t)v
,
7166 gen_or(gen_portrange6(cstate
, (uint16_t)v
, (uint16_t)v
,
7172 bpf_error(cstate
, "'gateway' requires a name");
7176 return gen_proto(cstate
, v
, proto
);
7178 #if !defined(NO_PROTOCHAIN)
7180 return gen_protochain(cstate
, v
, proto
);
7196 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7199 struct addrinfo
*res
;
7200 struct in6_addr
*addr
;
7201 struct in6_addr mask
;
7203 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7206 * Catch errors reported by us and routines below us, and return NULL
7209 if (setjmp(cstate
->top_ctx
))
7212 res
= pcap_nametoaddrinfo(s
);
7214 bpf_error(cstate
, "invalid ip6 address %s", s
);
7217 bpf_error(cstate
, "%s resolved to multiple address", s
);
7218 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7220 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7221 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7222 memset(&mask
, 0, sizeof(mask
));
7223 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7225 mask
.s6_addr
[masklen
/ 8] =
7226 (0xff << (8 - masklen
% 8)) & 0xff;
7229 memcpy(a
, addr
, sizeof(a
));
7230 memcpy(m
, &mask
, sizeof(m
));
7231 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7232 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7233 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7241 bpf_error(cstate
, "Mask syntax for networks only");
7245 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7251 // Q_GATEWAY only (see the grammar)
7252 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7259 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7261 struct block
*b
, *tmp
;
7264 * Catch errors reported by us and routines below us, and return NULL
7267 if (setjmp(cstate
->top_ctx
))
7270 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7271 cstate
->e
= pcap_ether_aton(s
);
7272 if (cstate
->e
== NULL
)
7273 bpf_error(cstate
, "malloc");
7274 switch (cstate
->linktype
) {
7276 case DLT_NETANALYZER
:
7277 case DLT_NETANALYZER_TRANSPARENT
:
7278 tmp
= gen_prevlinkhdr_check(cstate
);
7279 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7284 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7287 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7289 case DLT_IEEE802_11
:
7290 case DLT_PRISM_HEADER
:
7291 case DLT_IEEE802_11_RADIO_AVS
:
7292 case DLT_IEEE802_11_RADIO
:
7294 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7296 case DLT_IP_OVER_FC
:
7297 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7302 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7309 bpf_error(cstate
, "ethernet address used in non-ether expression");
7314 sappend(struct slist
*s0
, struct slist
*s1
)
7317 * This is definitely not the best way to do this, but the
7318 * lists will rarely get long.
7325 static struct slist
*
7326 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7330 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7335 static struct slist
*
7336 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7340 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7346 * Modify "index" to use the value stored into its register as an
7347 * offset relative to the beginning of the header for the protocol
7348 * "proto", and allocate a register and put an item "size" bytes long
7349 * (1, 2, or 4) at that offset into that register, making it the register
7352 static struct arth
*
7353 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7357 struct slist
*s
, *tmp
;
7359 int regno
= alloc_reg(cstate
);
7361 free_reg(cstate
, inst
->regno
);
7365 bpf_error(cstate
, "data size must be 1, 2, or 4");
7382 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7386 * The offset is relative to the beginning of the packet
7387 * data, if we have a radio header. (If we don't, this
7390 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7391 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7392 cstate
->linktype
!= DLT_PRISM_HEADER
)
7393 bpf_error(cstate
, "radio information not present in capture");
7396 * Load into the X register the offset computed into the
7397 * register specified by "index".
7399 s
= xfer_to_x(cstate
, inst
);
7402 * Load the item at that offset.
7404 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7406 sappend(inst
->s
, s
);
7411 * The offset is relative to the beginning of
7412 * the link-layer header.
7414 * XXX - what about ATM LANE? Should the index be
7415 * relative to the beginning of the AAL5 frame, so
7416 * that 0 refers to the beginning of the LE Control
7417 * field, or relative to the beginning of the LAN
7418 * frame, so that 0 refers, for Ethernet LANE, to
7419 * the beginning of the destination address?
7421 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7424 * If "s" is non-null, it has code to arrange that the
7425 * X register contains the length of the prefix preceding
7426 * the link-layer header. Add to it the offset computed
7427 * into the register specified by "index", and move that
7428 * into the X register. Otherwise, just load into the X
7429 * register the offset computed into the register specified
7433 sappend(s
, xfer_to_a(cstate
, inst
));
7434 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7435 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7437 s
= xfer_to_x(cstate
, inst
);
7440 * Load the item at the sum of the offset we've put in the
7441 * X register and the offset of the start of the link
7442 * layer header (which is 0 if the radio header is
7443 * variable-length; that header length is what we put
7444 * into the X register and then added to the index).
7446 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7447 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7449 sappend(inst
->s
, s
);
7463 * The offset is relative to the beginning of
7464 * the network-layer header.
7465 * XXX - are there any cases where we want
7466 * cstate->off_nl_nosnap?
7468 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7471 * If "s" is non-null, it has code to arrange that the
7472 * X register contains the variable part of the offset
7473 * of the link-layer payload. Add to it the offset
7474 * computed into the register specified by "index",
7475 * and move that into the X register. Otherwise, just
7476 * load into the X register the offset computed into
7477 * the register specified by "index".
7480 sappend(s
, xfer_to_a(cstate
, inst
));
7481 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7482 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7484 s
= xfer_to_x(cstate
, inst
);
7487 * Load the item at the sum of the offset we've put in the
7488 * X register, the offset of the start of the network
7489 * layer header from the beginning of the link-layer
7490 * payload, and the constant part of the offset of the
7491 * start of the link-layer payload.
7493 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7494 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7496 sappend(inst
->s
, s
);
7499 * Do the computation only if the packet contains
7500 * the protocol in question.
7502 b
= gen_proto_abbrev_internal(cstate
, proto
);
7504 gen_and(inst
->b
, b
);
7518 * The offset is relative to the beginning of
7519 * the transport-layer header.
7521 * Load the X register with the length of the IPv4 header
7522 * (plus the offset of the link-layer header, if it's
7523 * a variable-length header), in bytes.
7525 * XXX - are there any cases where we want
7526 * cstate->off_nl_nosnap?
7527 * XXX - we should, if we're built with
7528 * IPv6 support, generate code to load either
7529 * IPv4, IPv6, or both, as appropriate.
7531 s
= gen_loadx_iphdrlen(cstate
);
7534 * The X register now contains the sum of the variable
7535 * part of the offset of the link-layer payload and the
7536 * length of the network-layer header.
7538 * Load into the A register the offset relative to
7539 * the beginning of the transport layer header,
7540 * add the X register to that, move that to the
7541 * X register, and load with an offset from the
7542 * X register equal to the sum of the constant part of
7543 * the offset of the link-layer payload and the offset,
7544 * relative to the beginning of the link-layer payload,
7545 * of the network-layer header.
7547 sappend(s
, xfer_to_a(cstate
, inst
));
7548 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7549 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7550 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7551 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7552 sappend(inst
->s
, s
);
7555 * Do the computation only if the packet contains
7556 * the protocol in question - which is true only
7557 * if this is an IP datagram and is the first or
7558 * only fragment of that datagram.
7560 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7562 gen_and(inst
->b
, b
);
7563 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7568 * Do the computation only if the packet contains
7569 * the protocol in question.
7571 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7573 gen_and(inst
->b
, b
);
7577 * Check if we have an icmp6 next header
7579 b
= gen_ip6_proto(cstate
, 58);
7581 gen_and(inst
->b
, b
);
7584 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7586 * If "s" is non-null, it has code to arrange that the
7587 * X register contains the variable part of the offset
7588 * of the link-layer payload. Add to it the offset
7589 * computed into the register specified by "index",
7590 * and move that into the X register. Otherwise, just
7591 * load into the X register the offset computed into
7592 * the register specified by "index".
7595 sappend(s
, xfer_to_a(cstate
, inst
));
7596 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7597 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7599 s
= xfer_to_x(cstate
, inst
);
7602 * Load the item at the sum of the offset we've put in the
7603 * X register, the offset of the start of the network
7604 * layer header from the beginning of the link-layer
7605 * payload, and the constant part of the offset of the
7606 * start of the link-layer payload.
7608 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7609 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7612 sappend(inst
->s
, s
);
7616 inst
->regno
= regno
;
7617 s
= new_stmt(cstate
, BPF_ST
);
7619 sappend(inst
->s
, s
);
7625 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7629 * Catch errors reported by us and routines below us, and return NULL
7632 if (setjmp(cstate
->top_ctx
))
7635 return gen_load_internal(cstate
, proto
, inst
, size
);
7638 static struct block
*
7639 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7640 struct arth
*a1
, int reversed
)
7642 struct slist
*s0
, *s1
, *s2
;
7643 struct block
*b
, *tmp
;
7645 s0
= xfer_to_x(cstate
, a1
);
7646 s1
= xfer_to_a(cstate
, a0
);
7647 if (code
== BPF_JEQ
) {
7648 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7649 b
= new_block(cstate
, JMP(code
));
7653 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7659 sappend(a0
->s
, a1
->s
);
7663 free_reg(cstate
, a0
->regno
);
7664 free_reg(cstate
, a1
->regno
);
7666 /* 'and' together protocol checks */
7669 gen_and(a0
->b
, tmp
= a1
->b
);
7683 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7684 struct arth
*a1
, int reversed
)
7687 * Catch errors reported by us and routines below us, and return NULL
7690 if (setjmp(cstate
->top_ctx
))
7693 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7697 gen_loadlen(compiler_state_t
*cstate
)
7704 * Catch errors reported by us and routines below us, and return NULL
7707 if (setjmp(cstate
->top_ctx
))
7710 regno
= alloc_reg(cstate
);
7711 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7712 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7713 s
->next
= new_stmt(cstate
, BPF_ST
);
7714 s
->next
->s
.k
= regno
;
7721 static struct arth
*
7722 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7728 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7730 reg
= alloc_reg(cstate
);
7732 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7734 s
->next
= new_stmt(cstate
, BPF_ST
);
7743 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7746 * Catch errors reported by us and routines below us, and return NULL
7749 if (setjmp(cstate
->top_ctx
))
7752 return gen_loadi_internal(cstate
, val
);
7756 * The a_arg dance is to avoid annoying whining by compilers that
7757 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7758 * It's not *used* after setjmp returns.
7761 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7763 struct arth
*a
= a_arg
;
7767 * Catch errors reported by us and routines below us, and return NULL
7770 if (setjmp(cstate
->top_ctx
))
7773 s
= xfer_to_a(cstate
, a
);
7775 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7778 s
= new_stmt(cstate
, BPF_ST
);
7786 * The a0_arg dance is to avoid annoying whining by compilers that
7787 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7788 * It's not *used* after setjmp returns.
7791 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7794 struct arth
*a0
= a0_arg
;
7795 struct slist
*s0
, *s1
, *s2
;
7798 * Catch errors reported by us and routines below us, and return NULL
7801 if (setjmp(cstate
->top_ctx
))
7805 * Disallow division by, or modulus by, zero; we do this here
7806 * so that it gets done even if the optimizer is disabled.
7808 * Also disallow shifts by a value greater than 31; we do this
7809 * here, for the same reason.
7811 if (code
== BPF_DIV
) {
7812 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7813 bpf_error(cstate
, "division by zero");
7814 } else if (code
== BPF_MOD
) {
7815 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7816 bpf_error(cstate
, "modulus by zero");
7817 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7818 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7819 bpf_error(cstate
, "shift by more than 31 bits");
7821 s0
= xfer_to_x(cstate
, a1
);
7822 s1
= xfer_to_a(cstate
, a0
);
7823 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7828 sappend(a0
->s
, a1
->s
);
7830 free_reg(cstate
, a0
->regno
);
7831 free_reg(cstate
, a1
->regno
);
7833 s0
= new_stmt(cstate
, BPF_ST
);
7834 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7841 * Initialize the table of used registers and the current register.
7844 init_regs(compiler_state_t
*cstate
)
7847 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7851 * Return the next free register.
7854 alloc_reg(compiler_state_t
*cstate
)
7856 int n
= BPF_MEMWORDS
;
7859 if (cstate
->regused
[cstate
->curreg
])
7860 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7862 cstate
->regused
[cstate
->curreg
] = 1;
7863 return cstate
->curreg
;
7866 bpf_error(cstate
, "too many registers needed to evaluate expression");
7871 * Return a register to the table so it can
7875 free_reg(compiler_state_t
*cstate
, int n
)
7877 cstate
->regused
[n
] = 0;
7880 static struct block
*
7881 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7885 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7886 return gen_jmp(cstate
, jmp
, n
, s
);
7890 gen_greater(compiler_state_t
*cstate
, int n
)
7893 * Catch errors reported by us and routines below us, and return NULL
7896 if (setjmp(cstate
->top_ctx
))
7899 return gen_len(cstate
, BPF_JGE
, n
);
7903 * Actually, this is less than or equal.
7906 gen_less(compiler_state_t
*cstate
, int n
)
7911 * Catch errors reported by us and routines below us, and return NULL
7914 if (setjmp(cstate
->top_ctx
))
7917 b
= gen_len(cstate
, BPF_JGT
, n
);
7924 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7925 * the beginning of the link-layer header.
7926 * XXX - that means you can't test values in the radiotap header, but
7927 * as that header is difficult if not impossible to parse generally
7928 * without a loop, that might not be a severe problem. A new keyword
7929 * "radio" could be added for that, although what you'd really want
7930 * would be a way of testing particular radio header values, which
7931 * would generate code appropriate to the radio header in question.
7934 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7940 * Catch errors reported by us and routines below us, and return NULL
7943 if (setjmp(cstate
->top_ctx
))
7946 assert_maxval(cstate
, "byte argument", val
, UINT8_MAX
);
7953 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7956 return gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7959 return gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7962 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7966 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7970 // Load the required byte first.
7971 struct slist
*s0
= gen_load_a(cstate
, OR_LINKHDR
, idx
, BPF_B
);
7973 b
= gen_jmp(cstate
, BPF_JEQ
, 0, s0
);
7980 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7982 bpf_u_int32 hostmask
;
7983 struct block
*b0
, *b1
, *b2
;
7984 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7987 * Catch errors reported by us and routines below us, and return NULL
7990 if (setjmp(cstate
->top_ctx
))
7997 switch (cstate
->linktype
) {
7999 case DLT_ARCNET_LINUX
:
8000 // ARCnet broadcast is [8-bit] destination address 0.
8001 return gen_ahostop(cstate
, 0, Q_DST
);
8003 case DLT_NETANALYZER
:
8004 case DLT_NETANALYZER_TRANSPARENT
:
8005 b1
= gen_prevlinkhdr_check(cstate
);
8006 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8011 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8013 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8014 case DLT_IEEE802_11
:
8015 case DLT_PRISM_HEADER
:
8016 case DLT_IEEE802_11_RADIO_AVS
:
8017 case DLT_IEEE802_11_RADIO
:
8019 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8020 case DLT_IP_OVER_FC
:
8021 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8023 fail_kw_on_dlt(cstate
, "broadcast");
8028 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8029 * as an indication that we don't know the netmask, and fail
8032 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8033 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8034 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8035 hostmask
= ~cstate
->netmask
;
8036 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8037 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8042 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
8047 * Generate code to test the low-order bit of a MAC address (that's
8048 * the bottom bit of the *first* byte).
8050 static struct block
*
8051 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8053 register struct slist
*s
;
8055 /* link[offset] & 1 != 0 */
8056 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8057 return gen_set(cstate
, 1, s
);
8061 gen_multicast(compiler_state_t
*cstate
, int proto
)
8063 register struct block
*b0
, *b1
, *b2
;
8064 register struct slist
*s
;
8067 * Catch errors reported by us and routines below us, and return NULL
8070 if (setjmp(cstate
->top_ctx
))
8077 switch (cstate
->linktype
) {
8079 case DLT_ARCNET_LINUX
:
8080 // ARCnet multicast is the same as broadcast.
8081 return gen_ahostop(cstate
, 0, Q_DST
);
8083 case DLT_NETANALYZER
:
8084 case DLT_NETANALYZER_TRANSPARENT
:
8085 b1
= gen_prevlinkhdr_check(cstate
);
8086 /* ether[0] & 1 != 0 */
8087 b0
= gen_mac_multicast(cstate
, 0);
8093 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8095 * XXX - was that referring to bit-order issues?
8097 /* fddi[1] & 1 != 0 */
8098 return gen_mac_multicast(cstate
, 1);
8100 /* tr[2] & 1 != 0 */
8101 return gen_mac_multicast(cstate
, 2);
8102 case DLT_IEEE802_11
:
8103 case DLT_PRISM_HEADER
:
8104 case DLT_IEEE802_11_RADIO_AVS
:
8105 case DLT_IEEE802_11_RADIO
:
8110 * For control frames, there is no DA.
8112 * For management frames, DA is at an
8113 * offset of 4 from the beginning of
8116 * For data frames, DA is at an offset
8117 * of 4 from the beginning of the packet
8118 * if To DS is clear and at an offset of
8119 * 16 from the beginning of the packet
8124 * Generate the tests to be done for data frames.
8126 * First, check for To DS set, i.e. "link[1] & 0x01".
8128 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8129 b1
= gen_set(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8132 * If To DS is set, the DA is at 16.
8134 b0
= gen_mac_multicast(cstate
, 16);
8138 * Now, check for To DS not set, i.e. check
8139 * "!(link[1] & 0x01)".
8141 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8142 b2
= gen_unset(cstate
, IEEE80211_FC1_DIR_TODS
, s
);
8145 * If To DS is not set, the DA is at 4.
8147 b1
= gen_mac_multicast(cstate
, 4);
8151 * Now OR together the last two checks. That gives
8152 * the complete set of checks for data frames.
8157 * Now check for a data frame.
8158 * I.e, check "link[0] & 0x08".
8160 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8161 b1
= gen_set(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8164 * AND that with the checks done for data frames.
8169 * If the high-order bit of the type value is 0, this
8170 * is a management frame.
8171 * I.e, check "!(link[0] & 0x08)".
8173 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8174 b2
= gen_unset(cstate
, IEEE80211_FC0_TYPE_DATA
, s
);
8177 * For management frames, the DA is at 4.
8179 b1
= gen_mac_multicast(cstate
, 4);
8183 * OR that with the checks done for data frames.
8184 * That gives the checks done for management and
8190 * If the low-order bit of the type value is 1,
8191 * this is either a control frame or a frame
8192 * with a reserved type, and thus not a
8195 * I.e., check "!(link[0] & 0x04)".
8197 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8198 b1
= gen_unset(cstate
, IEEE80211_FC0_TYPE_CTL
, s
);
8201 * AND that with the checks for data and management
8206 case DLT_IP_OVER_FC
:
8207 return gen_mac_multicast(cstate
, 2);
8211 fail_kw_on_dlt(cstate
, "multicast");
8215 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8216 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8221 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8222 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8226 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8232 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8233 * we can look at special meta-data in the filter expression; otherwise we
8234 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8235 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8236 * pcap_activate() conditionally sets.
8239 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8241 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8243 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8245 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8250 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8253 * Catch errors reported by us and routines below us, and return NULL
8256 if (setjmp(cstate
->top_ctx
))
8260 * Only some data link types support ifindex qualifiers.
8262 switch (cstate
->linktype
) {
8263 case DLT_LINUX_SLL2
:
8264 /* match packets on this interface */
8265 return gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8267 #if defined(__linux__)
8268 require_basic_bpf_extensions(cstate
, "ifindex");
8270 return gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8272 #else /* defined(__linux__) */
8273 fail_kw_on_dlt(cstate
, "ifindex");
8275 #endif /* defined(__linux__) */
8280 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8281 * Outbound traffic is sent by this machine, while inbound traffic is
8282 * sent by a remote machine (and may include packets destined for a
8283 * unicast or multicast link-layer address we are not subscribing to).
8284 * These are the same definitions implemented by pcap_setdirection().
8285 * Capturing only unicast traffic destined for this host is probably
8286 * better accomplished using a higher-layer filter.
8289 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8291 register struct block
*b0
;
8294 * Catch errors reported by us and routines below us, and return NULL
8297 if (setjmp(cstate
->top_ctx
))
8301 * Only some data link types support inbound/outbound qualifiers.
8303 switch (cstate
->linktype
) {
8305 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8306 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8309 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8310 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8313 /* match outgoing packets */
8314 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8316 /* to filter on inbound traffic, invert the match */
8321 case DLT_LINUX_SLL2
:
8322 /* match outgoing packets */
8323 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8325 /* to filter on inbound traffic, invert the match */
8331 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8332 outbound
? PF_OUT
: PF_IN
);
8335 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8337 case DLT_JUNIPER_MFR
:
8338 case DLT_JUNIPER_MLFR
:
8339 case DLT_JUNIPER_MLPPP
:
8340 case DLT_JUNIPER_ATM1
:
8341 case DLT_JUNIPER_ATM2
:
8342 case DLT_JUNIPER_PPPOE
:
8343 case DLT_JUNIPER_PPPOE_ATM
:
8344 case DLT_JUNIPER_GGSN
:
8345 case DLT_JUNIPER_ES
:
8346 case DLT_JUNIPER_MONITOR
:
8347 case DLT_JUNIPER_SERVICES
:
8348 case DLT_JUNIPER_ETHER
:
8349 case DLT_JUNIPER_PPP
:
8350 case DLT_JUNIPER_FRELAY
:
8351 case DLT_JUNIPER_CHDLC
:
8352 case DLT_JUNIPER_VP
:
8353 case DLT_JUNIPER_ST
:
8354 case DLT_JUNIPER_ISM
:
8355 case DLT_JUNIPER_VS
:
8356 case DLT_JUNIPER_SRX_E2E
:
8357 case DLT_JUNIPER_FIBRECHANNEL
:
8358 case DLT_JUNIPER_ATM_CEMIC
:
8359 /* juniper flags (including direction) are stored
8360 * the byte after the 3-byte magic number */
8361 return gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8365 * If we have packet meta-data indicating a direction,
8366 * and that metadata can be checked by BPF code, check
8367 * it. Otherwise, give up, as this link-layer type has
8368 * nothing in the packet data.
8370 * Currently, the only platform where a BPF filter can
8371 * check that metadata is Linux with the in-kernel
8372 * BPF interpreter. If other packet capture mechanisms
8373 * and BPF filters also supported this, it would be
8374 * nice. It would be even better if they made that
8375 * metadata available so that we could provide it
8376 * with newer capture APIs, allowing it to be saved
8379 #if defined(__linux__)
8380 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8381 /* match outgoing packets */
8382 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8385 /* to filter on inbound traffic, invert the match */
8389 #else /* defined(__linux__) */
8390 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8392 #endif /* defined(__linux__) */
8396 /* PF firewall log matched interface */
8398 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8403 * Catch errors reported by us and routines below us, and return NULL
8406 if (setjmp(cstate
->top_ctx
))
8409 assert_pflog(cstate
, "ifname");
8411 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8412 off
= offsetof(struct pfloghdr
, ifname
);
8413 if (strlen(ifname
) >= len
) {
8414 bpf_error(cstate
, "ifname interface names can only be %d characters",
8418 return gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8419 (const u_char
*)ifname
);
8422 /* PF firewall log ruleset name */
8424 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8427 * Catch errors reported by us and routines below us, and return NULL
8430 if (setjmp(cstate
->top_ctx
))
8433 assert_pflog(cstate
, "ruleset");
8435 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8436 bpf_error(cstate
, "ruleset names can only be %ld characters",
8437 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8441 return gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8442 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8445 /* PF firewall log rule number */
8447 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8450 * Catch errors reported by us and routines below us, and return NULL
8453 if (setjmp(cstate
->top_ctx
))
8456 assert_pflog(cstate
, "rnr");
8458 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8462 /* PF firewall log sub-rule number */
8464 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8467 * Catch errors reported by us and routines below us, and return NULL
8470 if (setjmp(cstate
->top_ctx
))
8473 assert_pflog(cstate
, "srnr");
8475 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8479 /* PF firewall log reason code */
8481 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8484 * Catch errors reported by us and routines below us, and return NULL
8487 if (setjmp(cstate
->top_ctx
))
8490 assert_pflog(cstate
, "reason");
8492 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8493 (bpf_u_int32
)reason
);
8496 /* PF firewall log action */
8498 gen_pf_action(compiler_state_t
*cstate
, int action
)
8501 * Catch errors reported by us and routines below us, and return NULL
8504 if (setjmp(cstate
->top_ctx
))
8507 assert_pflog(cstate
, "action");
8509 return gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8510 (bpf_u_int32
)action
);
8513 /* IEEE 802.11 wireless header */
8515 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8518 * Catch errors reported by us and routines below us, and return NULL
8521 if (setjmp(cstate
->top_ctx
))
8524 switch (cstate
->linktype
) {
8526 case DLT_IEEE802_11
:
8527 case DLT_PRISM_HEADER
:
8528 case DLT_IEEE802_11_RADIO_AVS
:
8529 case DLT_IEEE802_11_RADIO
:
8531 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8534 fail_kw_on_dlt(cstate
, "type/subtype");
8540 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8543 * Catch errors reported by us and routines below us, and return NULL
8546 if (setjmp(cstate
->top_ctx
))
8549 switch (cstate
->linktype
) {
8551 case DLT_IEEE802_11
:
8552 case DLT_PRISM_HEADER
:
8553 case DLT_IEEE802_11_RADIO_AVS
:
8554 case DLT_IEEE802_11_RADIO
:
8556 return gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8557 IEEE80211_FC1_DIR_MASK
);
8560 fail_kw_on_dlt(cstate
, "dir");
8565 // Process an ARCnet host address string.
8567 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8570 * Catch errors reported by us and routines below us, and return NULL
8573 if (setjmp(cstate
->top_ctx
))
8576 switch (cstate
->linktype
) {
8579 case DLT_ARCNET_LINUX
:
8580 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8581 q
.proto
== Q_LINK
) {
8584 * The lexer currently defines the address format in a
8585 * way that makes this error condition never true.
8586 * Let's check it anyway in case this part of the lexer
8587 * changes in future.
8589 if (! pcapint_atoan(s
, &addr
))
8590 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8591 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8593 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8597 bpf_error(cstate
, "aid supported only on ARCnet");
8602 // Compare an ARCnet host address with the given value.
8603 static struct block
*
8604 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8606 register struct block
*b0
, *b1
;
8610 * ARCnet is different from Ethernet: the source address comes before
8611 * the destination address, each is one byte long. This holds for all
8612 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8613 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8614 * by Datapoint (document number 61610-01).
8617 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8620 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8623 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8624 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8630 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8631 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8641 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8648 static struct block
*
8649 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8651 struct block
*b0
, *b1
;
8653 /* check for VLAN, including 802.1ad and QinQ */
8654 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8655 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8658 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8664 static struct block
*
8665 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8667 assert_maxval(cstate
, "VLAN tag", vlan_num
, 0x0fff);
8668 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8671 static struct block
*
8672 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8675 struct block
*b0
, *b1
;
8677 b0
= gen_vlan_tpid_test(cstate
);
8680 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8686 * Both payload and link header type follow the VLAN tags so that
8687 * both need to be updated.
8689 cstate
->off_linkpl
.constant_part
+= 4;
8690 cstate
->off_linktype
.constant_part
+= 4;
8695 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8696 /* add v to variable part of off */
8698 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8699 bpf_u_int32 v
, struct slist
*s
)
8703 if (!off
->is_variable
)
8704 off
->is_variable
= 1;
8706 off
->reg
= alloc_reg(cstate
);
8708 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8711 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8714 s2
= new_stmt(cstate
, BPF_ST
);
8720 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8721 * and link type offsets first
8724 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8728 /* offset determined at run time, shift variable part */
8730 cstate
->is_vlan_vloffset
= 1;
8731 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8732 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8734 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8735 sappend(s
.next
, b_tpid
->head
->stmts
);
8736 b_tpid
->head
->stmts
= s
.next
;
8740 * patch block b_vid (VLAN id test) to load VID value either from packet
8741 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8744 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8746 struct slist
*s
, *s2
, *sjeq
;
8749 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8750 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8752 /* true -> next instructions, false -> beginning of b_vid */
8753 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8755 sjeq
->s
.jf
= b_vid
->stmts
;
8758 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8759 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8763 /* Jump to the test in b_vid. We need to jump one instruction before
8764 * the end of the b_vid block so that we only skip loading the TCI
8765 * from packet data and not the 'and' instruction extracting VID.
8768 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8770 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8774 /* insert our statements at the beginning of b_vid */
8775 sappend(s
, b_vid
->stmts
);
8780 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8781 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8782 * tag can be either in metadata or in packet data; therefore if the
8783 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8784 * header for VLAN tag. As the decision is done at run time, we need
8785 * update variable part of the offsets
8787 static struct block
*
8788 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8791 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8794 /* generate new filter code based on extracting packet
8796 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8797 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8799 b0
= gen_jmp(cstate
, BPF_JEQ
, 1, s
);
8802 * This is tricky. We need to insert the statements updating variable
8803 * parts of offsets before the traditional TPID and VID tests so
8804 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8805 * we do not want this update to affect those checks. That's why we
8806 * generate both test blocks first and insert the statements updating
8807 * variable parts of both offsets after that. This wouldn't work if
8808 * there already were variable length link header when entering this
8809 * function but gen_vlan_bpf_extensions() isn't called in that case.
8811 b_tpid
= gen_vlan_tpid_test(cstate
);
8813 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8815 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8820 gen_vlan_patch_vid_test(cstate
, b_vid
);
8830 * support IEEE 802.1Q VLAN trunk over ethernet
8833 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8838 * Catch errors reported by us and routines below us, and return NULL
8841 if (setjmp(cstate
->top_ctx
))
8844 /* can't check for VLAN-encapsulated packets inside MPLS */
8845 if (cstate
->label_stack_depth
> 0)
8846 bpf_error(cstate
, "no VLAN match after MPLS");
8849 * Check for a VLAN packet, and then change the offsets to point
8850 * to the type and data fields within the VLAN packet. Just
8851 * increment the offsets, so that we can support a hierarchy, e.g.
8852 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
8855 * XXX - this is a bit of a kludge. If we were to split the
8856 * compiler into a parser that parses an expression and
8857 * generates an expression tree, and a code generator that
8858 * takes an expression tree (which could come from our
8859 * parser or from some other parser) and generates BPF code,
8860 * we could perhaps make the offsets parameters of routines
8861 * and, in the handler for an "AND" node, pass to subnodes
8862 * other than the VLAN node the adjusted offsets.
8864 * This would mean that "vlan" would, instead of changing the
8865 * behavior of *all* tests after it, change only the behavior
8866 * of tests ANDed with it. That would change the documented
8867 * semantics of "vlan", which might break some expressions.
8868 * However, it would mean that "(vlan and ip) or ip" would check
8869 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8870 * checking only for VLAN-encapsulated IP, so that could still
8871 * be considered worth doing; it wouldn't break expressions
8872 * that are of the form "vlan and ..." or "vlan N and ...",
8873 * which I suspect are the most common expressions involving
8874 * "vlan". "vlan or ..." doesn't necessarily do what the user
8875 * would really want, now, as all the "or ..." tests would
8876 * be done assuming a VLAN, even though the "or" could be viewed
8877 * as meaning "or, if this isn't a VLAN packet...".
8879 switch (cstate
->linktype
) {
8883 * Newer version of the Linux kernel pass around
8884 * packets in which the VLAN tag has been removed
8885 * from the packet data and put into metadata.
8887 * This requires special treatment.
8889 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8890 /* Verify that this is the outer part of the packet and
8891 * not encapsulated somehow. */
8892 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8893 cstate
->off_linkhdr
.constant_part
==
8894 cstate
->off_outermostlinkhdr
.constant_part
) {
8896 * Do we need special VLAN handling?
8898 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8899 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8902 b0
= gen_vlan_no_bpf_extensions(cstate
,
8903 vlan_num
, has_vlan_tag
);
8906 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8910 case DLT_NETANALYZER
:
8911 case DLT_NETANALYZER_TRANSPARENT
:
8912 case DLT_IEEE802_11
:
8913 case DLT_PRISM_HEADER
:
8914 case DLT_IEEE802_11_RADIO_AVS
:
8915 case DLT_IEEE802_11_RADIO
:
8917 * These are either Ethernet packets with an additional
8918 * metadata header (the NetAnalyzer types), or 802.11
8919 * packets, possibly with an additional metadata header.
8921 * For the first of those, the VLAN tag is in the normal
8922 * place, so the special-case handling above isn't
8925 * For the second of those, we don't do the special-case
8928 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8932 bpf_error(cstate
, "no VLAN support for %s",
8933 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8937 cstate
->vlan_stack_depth
++;
8945 * The label_num_arg dance is to avoid annoying whining by compilers that
8946 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8947 * It's not *used* after setjmp returns.
8949 static struct block
*
8950 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
8953 struct block
*b0
, *b1
;
8955 if (cstate
->label_stack_depth
> 0) {
8956 /* just match the bottom-of-stack bit clear */
8957 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8960 * We're not in an MPLS stack yet, so check the link-layer
8961 * type against MPLS.
8963 switch (cstate
->linktype
) {
8965 case DLT_C_HDLC
: /* fall through */
8968 case DLT_NETANALYZER
:
8969 case DLT_NETANALYZER_TRANSPARENT
:
8970 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8974 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8977 /* FIXME add other DLT_s ...
8978 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8979 * leave it for now */
8982 bpf_error(cstate
, "no MPLS support for %s",
8983 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8988 /* If a specific MPLS label is requested, check it */
8989 if (has_label_num
) {
8990 assert_maxval(cstate
, "MPLS label", label_num
, 0xFFFFF);
8991 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8992 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
8993 0xfffff000); /* only compare the first 20 bits */
8999 * Change the offsets to point to the type and data fields within
9000 * the MPLS packet. Just increment the offsets, so that we
9001 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9002 * capture packets with an outer label of 100000 and an inner
9005 * Increment the MPLS stack depth as well; this indicates that
9006 * we're checking MPLS-encapsulated headers, to make sure higher
9007 * level code generators don't try to match against IP-related
9008 * protocols such as Q_ARP, Q_RARP etc.
9010 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9012 cstate
->off_nl_nosnap
+= 4;
9013 cstate
->off_nl
+= 4;
9014 cstate
->label_stack_depth
++;
9019 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9022 * Catch errors reported by us and routines below us, and return NULL
9025 if (setjmp(cstate
->top_ctx
))
9028 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9032 * Support PPPOE discovery and session.
9035 gen_pppoed(compiler_state_t
*cstate
)
9038 * Catch errors reported by us and routines below us, and return NULL
9041 if (setjmp(cstate
->top_ctx
))
9044 /* check for PPPoE discovery */
9045 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9049 * RFC 2516 Section 4:
9051 * The Ethernet payload for PPPoE is as follows:
9054 * 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
9055 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9056 * | VER | TYPE | CODE | SESSION_ID |
9057 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9058 * | LENGTH | payload ~
9059 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9062 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9064 struct block
*b0
, *b1
;
9067 * Catch errors reported by us and routines below us, and return NULL
9070 if (setjmp(cstate
->top_ctx
))
9074 * Test against the PPPoE session link-layer type.
9076 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9078 /* If a specific session is requested, check PPPoE session id */
9080 assert_maxval(cstate
, "PPPoE session number", sess_num
, UINT16_MAX
);
9081 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9087 * Change the offsets to point to the type and data fields within
9088 * the PPP packet, and note that this is PPPoE rather than
9091 * XXX - this is a bit of a kludge. See the comments in
9094 * The "network-layer" protocol is PPPoE, which has a 6-byte
9095 * PPPoE header, followed by a PPP packet.
9097 * There is no HDLC encapsulation for the PPP packet (it's
9098 * encapsulated in PPPoES instead), so the link-layer type
9099 * starts at the first byte of the PPP packet. For PPPoE,
9100 * that offset is relative to the beginning of the total
9101 * link-layer payload, including any 802.2 LLC header, so
9102 * it's 6 bytes past cstate->off_nl.
9104 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9105 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9106 cstate
->off_linkpl
.reg
);
9108 cstate
->off_linktype
= cstate
->off_linkhdr
;
9109 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9112 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9117 /* Check that this is Geneve and the VNI is correct if
9118 * specified. Parameterized to handle both IPv4 and IPv6. */
9119 static struct block
*
9120 gen_geneve_check(compiler_state_t
*cstate
,
9121 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
9122 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9124 struct block
*b0
, *b1
;
9126 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9128 /* Check that we are operating on version 0. Otherwise, we
9129 * can't decode the rest of the fields. The version is 2 bits
9130 * in the first byte of the Geneve header. */
9131 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9136 assert_maxval(cstate
, "Geneve VNI", vni
, 0xffffff);
9137 vni
<<= 8; /* VNI is in the upper 3 bytes */
9138 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9146 /* The IPv4 and IPv6 Geneve checks need to do two things:
9147 * - Verify that this actually is Geneve with the right VNI.
9148 * - Place the IP header length (plus variable link prefix if
9149 * needed) into register A to be used later to compute
9150 * the inner packet offsets. */
9151 static struct block
*
9152 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9154 struct block
*b0
, *b1
;
9155 struct slist
*s
, *s1
;
9157 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9159 /* Load the IP header length into A. */
9160 s
= gen_loadx_iphdrlen(cstate
);
9162 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9165 /* Forcibly append these statements to the true condition
9166 * of the protocol check by creating a new block that is
9167 * always true and ANDing them. */
9168 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9175 static struct block
*
9176 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9178 struct block
*b0
, *b1
;
9179 struct slist
*s
, *s1
;
9181 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9183 /* Load the IP header length. We need to account for a
9184 * variable length link prefix if there is one. */
9185 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9187 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9191 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9195 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9199 /* Forcibly append these statements to the true condition
9200 * of the protocol check by creating a new block that is
9201 * always true and ANDing them. */
9202 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9205 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9212 /* We need to store three values based on the Geneve header::
9213 * - The offset of the linktype.
9214 * - The offset of the end of the Geneve header.
9215 * - The offset of the end of the encapsulated MAC header. */
9216 static struct slist
*
9217 gen_geneve_offsets(compiler_state_t
*cstate
)
9219 struct slist
*s
, *s1
, *s_proto
;
9221 /* First we need to calculate the offset of the Geneve header
9222 * itself. This is composed of the IP header previously calculated
9223 * (include any variable link prefix) and stored in A plus the
9224 * fixed sized headers (fixed link prefix, MAC length, and UDP
9226 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9227 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9229 /* Stash this in X since we'll need it later. */
9230 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9233 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9235 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9239 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9240 cstate
->off_linktype
.is_variable
= 1;
9241 cstate
->off_linktype
.constant_part
= 0;
9243 s1
= new_stmt(cstate
, BPF_ST
);
9244 s1
->s
.k
= cstate
->off_linktype
.reg
;
9247 /* Load the Geneve option length and mask and shift to get the
9248 * number of bytes. It is stored in the first byte of the Geneve
9250 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9254 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9258 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9262 /* Add in the rest of the Geneve base header. */
9263 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9267 /* Add the Geneve header length to its offset and store. */
9268 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9272 /* Set the encapsulated type as Ethernet. Even though we may
9273 * not actually have Ethernet inside there are two reasons this
9275 * - The linktype field is always in EtherType format regardless
9276 * of whether it is in Geneve or an inner Ethernet frame.
9277 * - The only link layer that we have specific support for is
9278 * Ethernet. We will confirm that the packet actually is
9279 * Ethernet at runtime before executing these checks. */
9280 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9282 s1
= new_stmt(cstate
, BPF_ST
);
9283 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9286 /* Calculate whether we have an Ethernet header or just raw IP/
9287 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9288 * and linktype by 14 bytes so that the network header can be found
9289 * seamlessly. Otherwise, keep what we've calculated already. */
9291 /* We have a bare jmp so we can't use the optimizer. */
9292 cstate
->no_optimize
= 1;
9294 /* Load the EtherType in the Geneve header, 2 bytes in. */
9295 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9299 /* Load X with the end of the Geneve header. */
9300 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9301 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9304 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9305 * end of this check, we should have the total length in X. In
9306 * the non-Ethernet case, it's already there. */
9307 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9308 s_proto
->s
.k
= ETHERTYPE_TEB
;
9309 sappend(s
, s_proto
);
9311 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9315 /* Since this is Ethernet, use the EtherType of the payload
9316 * directly as the linktype. Overwrite what we already have. */
9317 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9321 s1
= new_stmt(cstate
, BPF_ST
);
9322 s1
->s
.k
= cstate
->off_linktype
.reg
;
9325 /* Advance two bytes further to get the end of the Ethernet
9327 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9331 /* Move the result to X. */
9332 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9335 /* Store the final result of our linkpl calculation. */
9336 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9337 cstate
->off_linkpl
.is_variable
= 1;
9338 cstate
->off_linkpl
.constant_part
= 0;
9340 s1
= new_stmt(cstate
, BPF_STX
);
9341 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9350 /* Check to see if this is a Geneve packet. */
9352 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9354 struct block
*b0
, *b1
;
9358 * Catch errors reported by us and routines below us, and return NULL
9361 if (setjmp(cstate
->top_ctx
))
9364 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9365 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9370 /* Later filters should act on the payload of the Geneve frame,
9371 * update all of the header pointers. Attach this code so that
9372 * it gets executed in the event that the Geneve filter matches. */
9373 s
= gen_geneve_offsets(cstate
);
9375 b1
= gen_true(cstate
);
9376 sappend(s
, b1
->stmts
);
9381 cstate
->is_encap
= 1;
9386 /* Check that this is VXLAN and the VNI is correct if
9387 * specified. Parameterized to handle both IPv4 and IPv6. */
9388 static struct block
*
9389 gen_vxlan_check(compiler_state_t
*cstate
,
9390 struct block
*(*gen_portfn
)(compiler_state_t
*, uint16_t, int, int),
9391 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9393 struct block
*b0
, *b1
;
9395 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9397 /* Check that the VXLAN header has the flag bits set
9399 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9404 assert_maxval(cstate
, "VXLAN VNI", vni
, 0xffffff);
9405 vni
<<= 8; /* VNI is in the upper 3 bytes */
9406 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9414 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9415 * - Verify that this actually is VXLAN with the right VNI.
9416 * - Place the IP header length (plus variable link prefix if
9417 * needed) into register A to be used later to compute
9418 * the inner packet offsets. */
9419 static struct block
*
9420 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9422 struct block
*b0
, *b1
;
9423 struct slist
*s
, *s1
;
9425 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9427 /* Load the IP header length into A. */
9428 s
= gen_loadx_iphdrlen(cstate
);
9430 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9433 /* Forcibly append these statements to the true condition
9434 * of the protocol check by creating a new block that is
9435 * always true and ANDing them. */
9436 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9443 static struct block
*
9444 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9446 struct block
*b0
, *b1
;
9447 struct slist
*s
, *s1
;
9449 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9451 /* Load the IP header length. We need to account for a
9452 * variable length link prefix if there is one. */
9453 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9455 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9459 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9463 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9467 /* Forcibly append these statements to the true condition
9468 * of the protocol check by creating a new block that is
9469 * always true and ANDing them. */
9470 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9473 b1
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9480 /* We need to store three values based on the VXLAN header:
9481 * - The offset of the linktype.
9482 * - The offset of the end of the VXLAN header.
9483 * - The offset of the end of the encapsulated MAC header. */
9484 static struct slist
*
9485 gen_vxlan_offsets(compiler_state_t
*cstate
)
9487 struct slist
*s
, *s1
;
9489 /* Calculate the offset of the VXLAN header itself. This
9490 * includes the IP header computed previously (including any
9491 * variable link prefix) and stored in A plus the fixed size
9492 * headers (fixed link prefix, MAC length, UDP header). */
9493 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9494 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9496 /* Add the VXLAN header length to its offset and store */
9497 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9501 /* Push the link header. VXLAN packets always contain Ethernet
9503 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9505 s1
= new_stmt(cstate
, BPF_ST
);
9506 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9509 /* As the payload is an Ethernet packet, we can use the
9510 * EtherType of the payload directly as the linktype. */
9511 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9515 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9516 cstate
->off_linktype
.is_variable
= 1;
9517 cstate
->off_linktype
.constant_part
= 0;
9519 s1
= new_stmt(cstate
, BPF_ST
);
9520 s1
->s
.k
= cstate
->off_linktype
.reg
;
9523 /* Two bytes further is the end of the Ethernet header and the
9524 * start of the payload. */
9525 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9529 /* Move the result to X. */
9530 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9533 /* Store the final result of our linkpl calculation. */
9534 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9535 cstate
->off_linkpl
.is_variable
= 1;
9536 cstate
->off_linkpl
.constant_part
= 0;
9538 s1
= new_stmt(cstate
, BPF_STX
);
9539 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9547 /* Check to see if this is a VXLAN packet. */
9549 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9551 struct block
*b0
, *b1
;
9555 * Catch errors reported by us and routines below us, and return NULL
9558 if (setjmp(cstate
->top_ctx
))
9561 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9562 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9567 /* Later filters should act on the payload of the VXLAN frame,
9568 * update all of the header pointers. Attach this code so that
9569 * it gets executed in the event that the VXLAN filter matches. */
9570 s
= gen_vxlan_offsets(cstate
);
9572 b1
= gen_true(cstate
);
9573 sappend(s
, b1
->stmts
);
9578 cstate
->is_encap
= 1;
9583 /* Check that the encapsulated frame has a link layer header
9584 * for Ethernet filters. */
9585 static struct block
*
9586 gen_encap_ll_check(compiler_state_t
*cstate
)
9589 struct slist
*s
, *s1
;
9591 /* The easiest way to see if there is a link layer present
9592 * is to check if the link layer header and payload are not
9595 /* Geneve always generates pure variable offsets so we can
9596 * compare only the registers. */
9597 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9598 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9600 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9601 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9604 b0
= gen_jmp(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
, 0, s
);
9610 static struct block
*
9611 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9612 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9614 assert_atm(cstate
, atmkw(atmfield
));
9619 assert_maxval(cstate
, "VPI", jvalue
, UINT8_MAX
);
9620 return gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9621 0xffffffffU
, jtype
, reverse
, jvalue
);
9624 assert_maxval(cstate
, "VCI", jvalue
, UINT16_MAX
);
9625 return gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9626 0xffffffffU
, jtype
, reverse
, jvalue
);
9633 static struct block
*
9634 gen_atm_vpi(compiler_state_t
*cstate
, const uint8_t v
)
9636 return gen_atmfield_code_internal(cstate
, A_VPI
, v
, BPF_JEQ
, 0);
9639 static struct block
*
9640 gen_atm_vci(compiler_state_t
*cstate
, const uint16_t v
)
9642 return gen_atmfield_code_internal(cstate
, A_VCI
, v
, BPF_JEQ
, 0);
9645 static struct block
*
9646 gen_atm_prototype(compiler_state_t
*cstate
, const uint8_t v
)
9648 return gen_mcmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, v
, 0x0fU
);
9651 static struct block
*
9652 gen_atmtype_llc(compiler_state_t
*cstate
)
9656 b0
= gen_atm_prototype(cstate
, PT_LLC
);
9657 cstate
->linktype
= cstate
->prevlinktype
;
9662 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9663 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9666 * Catch errors reported by us and routines below us, and return NULL
9669 if (setjmp(cstate
->top_ctx
))
9672 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9677 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9679 struct block
*b0
, *b1
;
9682 * Catch errors reported by us and routines below us, and return NULL
9685 if (setjmp(cstate
->top_ctx
))
9688 assert_atm(cstate
, atmkw(type
));
9693 /* Get all packets in Meta signalling Circuit */
9694 b0
= gen_atm_vpi(cstate
, 0);
9695 b1
= gen_atm_vci(cstate
, 1);
9700 /* Get all packets in Broadcast Circuit*/
9701 b0
= gen_atm_vpi(cstate
, 0);
9702 b1
= gen_atm_vci(cstate
, 2);
9707 /* Get all cells in Segment OAM F4 circuit*/
9708 b0
= gen_atm_vpi(cstate
, 0);
9709 b1
= gen_atm_vci(cstate
, 3);
9714 /* Get all cells in End-to-End OAM F4 Circuit*/
9715 b0
= gen_atm_vpi(cstate
, 0);
9716 b1
= gen_atm_vci(cstate
, 4);
9721 /* Get all packets in connection Signalling Circuit */
9722 b0
= gen_atm_vpi(cstate
, 0);
9723 b1
= gen_atm_vci(cstate
, 5);
9728 /* Get all packets in ILMI Circuit */
9729 b0
= gen_atm_vpi(cstate
, 0);
9730 b1
= gen_atm_vci(cstate
, 16);
9735 /* Get all LANE packets */
9736 b1
= gen_atm_prototype(cstate
, PT_LANE
);
9739 * Arrange that all subsequent tests assume LANE
9740 * rather than LLC-encapsulated packets, and set
9741 * the offsets appropriately for LANE-encapsulated
9744 * We assume LANE means Ethernet, not Token Ring.
9746 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9747 cstate
->off_payload
+ 2, /* Ethernet header */
9749 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9750 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9751 cstate
->off_nl
= 0; /* Ethernet II */
9752 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9761 * Filtering for MTP2 messages based on li value
9762 * FISU, length is null
9763 * LSSU, length is 1 or 2
9764 * MSU, length is 3 or more
9765 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9768 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9770 struct block
*b0
, *b1
;
9773 * Catch errors reported by us and routines below us, and return NULL
9776 if (setjmp(cstate
->top_ctx
))
9779 assert_ss7(cstate
, ss7kw(type
));
9784 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9785 0x3fU
, BPF_JEQ
, 0, 0U);
9788 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9789 0x3fU
, BPF_JGT
, 1, 2U);
9790 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9791 0x3fU
, BPF_JGT
, 0, 0U);
9796 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9797 0x3fU
, BPF_JGT
, 0, 2U);
9800 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9801 0xff80U
, BPF_JEQ
, 0, 0U);
9804 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9805 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9806 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9807 0xff80U
, BPF_JGT
, 0, 0U);
9812 return gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9813 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9821 * These maximum valid values are all-ones, so they double as the bitmasks
9822 * before any bitwise shifting.
9824 #define MTP2_SIO_MAXVAL UINT8_MAX
9825 #define MTP3_PC_MAXVAL 0x3fffU
9826 #define MTP3_SLS_MAXVAL 0xfU
9828 static struct block
*
9829 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
9830 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9837 newoff_sio
= cstate
->off_sio
;
9838 newoff_opc
= cstate
->off_opc
;
9839 newoff_dpc
= cstate
->off_dpc
;
9840 newoff_sls
= cstate
->off_sls
;
9842 assert_ss7(cstate
, ss7kw(mtp3field
));
9844 switch (mtp3field
) {
9847 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
9849 * SIO is the simplest field: the size is one byte and the offset is a
9850 * multiple of bytes, so the only detail to get right is the value of
9851 * the [right-to-left] field offset.
9854 newoff_sio
+= 3; /* offset for MTP2_HSL */
9858 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP2_SIO_MAXVAL
);
9859 // Here the bitmask means "do not apply a bitmask".
9860 return gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
9861 jtype
, reverse
, jvalue
);
9864 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
9866 * SLS, OPC and DPC are more complicated: none of these is sized in a
9867 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
9868 * diagrams are meant to be read right-to-left. This means in the
9869 * diagrams within individual fields and concatenations thereof
9870 * bitwise shifts and masks can be noted in the common left-to-right
9871 * manner until each final value is ready to be byte-swapped and
9872 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
9873 * similar problem in a similar way.
9875 * Offsets of fields within the packet header always have the
9876 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
9877 * DLTs the offset does not include the F (Flag) field at the
9878 * beginning of each message.
9880 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
9881 * 32-bit standard routing header has a 4 byte [RTL] offset and could
9882 * be tested entirely using a single BPF_W comparison. In this case
9883 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
9884 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
9885 * [LTR] bitmask would be (0xF << 28), all of which conveniently
9886 * correlates with the [RTL] packet diagram until the byte-swapping is
9889 * The code below uses this approach for OPC, which spans 3 bytes.
9890 * DPC and SLS use shorter loads, SLS also uses a different offset.
9897 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
9898 return gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
9899 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
9900 SWAPLONG(jvalue
<< 14));
9907 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
9908 return gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
9909 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
9917 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_SLS_MAXVAL
);
9918 return gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
9919 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
9928 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9929 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9932 * Catch errors reported by us and routines below us, and return NULL
9935 if (setjmp(cstate
->top_ctx
))
9938 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
9942 static struct block
*
9943 gen_msg_abbrev(compiler_state_t
*cstate
, const uint8_t type
)
9946 * Q.2931 signalling protocol messages for handling virtual circuits
9947 * establishment and teardown
9949 return gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
,
9954 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9956 struct block
*b0
, *b1
;
9959 * Catch errors reported by us and routines below us, and return NULL
9962 if (setjmp(cstate
->top_ctx
))
9965 assert_atm(cstate
, atmkw(type
));
9971 b0
= gen_atm_vci(cstate
, 3);
9972 b1
= gen_atm_vci(cstate
, 4);
9974 b0
= gen_atm_vpi(cstate
, 0);
9980 b0
= gen_atm_vci(cstate
, 3);
9981 b1
= gen_atm_vci(cstate
, 4);
9983 b0
= gen_atm_vpi(cstate
, 0);
9989 * Get Q.2931 signalling messages for switched
9990 * virtual connection
9992 b0
= gen_msg_abbrev(cstate
, SETUP
);
9993 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
9995 b0
= gen_msg_abbrev(cstate
, CONNECT
);
9997 b0
= gen_msg_abbrev(cstate
, CONNECT_ACK
);
9999 b0
= gen_msg_abbrev(cstate
, RELEASE
);
10001 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
10003 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
10007 case A_METACONNECT
:
10008 b0
= gen_msg_abbrev(cstate
, SETUP
);
10009 b1
= gen_msg_abbrev(cstate
, CALL_PROCEED
);
10011 b0
= gen_msg_abbrev(cstate
, CONNECT
);
10013 b0
= gen_msg_abbrev(cstate
, RELEASE
);
10015 b0
= gen_msg_abbrev(cstate
, RELEASE_DONE
);
10017 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);