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_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
647 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
648 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
650 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
652 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
653 static struct block
*gen_uncond(compiler_state_t
*, int);
654 static inline struct block
*gen_true(compiler_state_t
*);
655 static inline struct block
*gen_false(compiler_state_t
*);
656 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
657 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
658 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
659 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
660 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
661 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
662 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
663 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
664 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
665 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
667 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
668 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
669 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
670 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
671 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
674 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
675 struct in6_addr
*, int, u_int
, u_int
);
677 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
678 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
679 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
680 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
681 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
682 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
683 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
684 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
685 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
688 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
689 struct in6_addr
*, int, int, int);
692 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
693 struct addrinfo
*, int);
695 static struct block
*gen_ipfrag(compiler_state_t
*);
696 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
697 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
699 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
700 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
702 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
703 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
704 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
706 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
707 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
708 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
709 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
711 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
712 static int lookup_proto(compiler_state_t
*, const char *, int);
713 #if !defined(NO_PROTOCHAIN)
714 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
715 #endif /* !defined(NO_PROTOCHAIN) */
716 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int);
717 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
718 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
719 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
720 static struct block
*gen_len(compiler_state_t
*, int, int);
721 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
723 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
724 bpf_u_int32
, int, int);
725 static struct block
*gen_atmtype_llc(compiler_state_t
*);
726 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
729 initchunks(compiler_state_t
*cstate
)
733 for (i
= 0; i
< NCHUNKS
; i
++) {
734 cstate
->chunks
[i
].n_left
= 0;
735 cstate
->chunks
[i
].m
= NULL
;
737 cstate
->cur_chunk
= 0;
741 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
747 /* Round up to chunk alignment. */
748 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
750 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
751 if (n
> cp
->n_left
) {
753 k
= ++cstate
->cur_chunk
;
755 bpf_set_error(cstate
, "out of memory");
758 size
= CHUNK0SIZE
<< k
;
759 cp
->m
= (void *)malloc(size
);
761 bpf_set_error(cstate
, "out of memory");
764 memset((char *)cp
->m
, 0, size
);
767 bpf_set_error(cstate
, "out of memory");
772 return (void *)((char *)cp
->m
+ cp
->n_left
);
776 newchunk(compiler_state_t
*cstate
, size_t n
)
780 p
= newchunk_nolongjmp(cstate
, n
);
782 longjmp(cstate
->top_ctx
, 1);
789 freechunks(compiler_state_t
*cstate
)
793 for (i
= 0; i
< NCHUNKS
; ++i
)
794 if (cstate
->chunks
[i
].m
!= NULL
)
795 free(cstate
->chunks
[i
].m
);
799 * A strdup whose allocations are freed after code generation is over.
800 * This is used by the lexical analyzer, so it can't longjmp; it just
801 * returns NULL on an allocation error, and the callers must check
805 sdup(compiler_state_t
*cstate
, const char *s
)
807 size_t n
= strlen(s
) + 1;
808 char *cp
= newchunk_nolongjmp(cstate
, n
);
812 pcapint_strlcpy(cp
, s
, n
);
816 static inline struct block
*
817 new_block(compiler_state_t
*cstate
, int code
)
821 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
828 static inline struct slist
*
829 new_stmt(compiler_state_t
*cstate
, int code
)
833 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
839 static struct block
*
840 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
842 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
848 static struct block
*
849 gen_retblk(compiler_state_t
*cstate
, int v
)
851 if (setjmp(cstate
->top_ctx
)) {
853 * gen_retblk() only fails because a memory
854 * allocation failed in newchunk(), meaning
855 * that it can't return a pointer.
861 return gen_retblk_internal(cstate
, v
);
864 static inline PCAP_NORETURN_DEF
void
865 syntax(compiler_state_t
*cstate
)
867 bpf_error(cstate
, "syntax error in filter expression");
871 * For the given integer return a string with the keyword (or the nominal
872 * keyword if there is more than one). This is a simpler version of tok2str()
873 * in tcpdump because in this problem space a valid integer value is not
877 qual2kw(const char *kind
, const unsigned id
, const char *tokens
[],
880 static char buf
[4][64];
883 if (id
< size
&& tokens
[id
])
886 char *ret
= buf
[idx
];
887 idx
= (idx
+ 1) % (sizeof(buf
) / sizeof(buf
[0]));
888 ret
[0] = '\0'; // just in case
889 snprintf(ret
, sizeof(buf
[0]), "<invalid %s %u>", kind
, id
);
893 // protocol qualifier keywords
895 pqkw(const unsigned id
)
897 const char * tokens
[] = {
909 [Q_DECNET
] = "decnet",
915 [Q_ICMPV6
] = "icmp6",
927 [Q_NETBEUI
] = "netbeui",
930 [Q_ISIS_IIH
] = "iih",
931 [Q_ISIS_SNP
] = "snp",
932 [Q_ISIS_CSNP
] = "csnp",
933 [Q_ISIS_PSNP
] = "psnp",
934 [Q_ISIS_LSP
] = "lsp",
938 return qual2kw("proto", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
941 // direction qualifier keywords
943 dqkw(const unsigned id
)
945 const char * map
[] = {
948 [Q_OR
] = "src or dst",
949 [Q_AND
] = "src and dst",
957 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
962 atmkw(const unsigned id
)
964 const char * tokens
[] = {
967 [A_OAMF4SC
] = "oamf4sc",
968 [A_OAMF4EC
] = "oamf4ec",
974 // no keyword for A_SETUP
975 // no keyword for A_CALLPROCEED
976 // no keyword for A_CONNECT
977 // no keyword for A_CONNECTACK
978 // no keyword for A_RELEASE
979 // no keyword for A_RELEASE_DONE
982 // no keyword for A_PROTOTYPE
983 // no keyword for A_MSGTYPE
984 [A_CONNECTMSG
] = "connectmsg",
985 [A_METACONNECT
] = "metaconnect",
987 return qual2kw("ATM keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
992 ss7kw(const unsigned id
)
994 const char * tokens
[] = {
1010 return qual2kw("MTP keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
1013 static PCAP_NORETURN_DEF
void
1014 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
1016 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
1017 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
1021 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
1023 if (cstate
->linktype
!= DLT_PFLOG
)
1024 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
1028 assert_atm(compiler_state_t
*cstate
, const char *kw
)
1031 * Belt and braces: init_linktype() sets either all of these struct
1032 * members (for DLT_SUNATM) or none (otherwise).
1034 if (cstate
->linktype
!= DLT_SUNATM
||
1036 cstate
->off_vpi
== OFFSET_NOT_SET
||
1037 cstate
->off_vci
== OFFSET_NOT_SET
||
1038 cstate
->off_proto
== OFFSET_NOT_SET
||
1039 cstate
->off_payload
== OFFSET_NOT_SET
)
1040 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1044 assert_ss7(compiler_state_t
*cstate
, const char *kw
)
1046 switch (cstate
->linktype
) {
1049 case DLT_MTP2_WITH_PHDR
:
1050 // Belt and braces, same as in assert_atm().
1051 if (cstate
->off_sio
!= OFFSET_NOT_SET
&&
1052 cstate
->off_opc
!= OFFSET_NOT_SET
&&
1053 cstate
->off_dpc
!= OFFSET_NOT_SET
&&
1054 cstate
->off_sls
!= OFFSET_NOT_SET
)
1057 bpf_error(cstate
, "'%s' supported only on SS7", kw
);
1061 assert_maxval(compiler_state_t
*cstate
, const char *name
,
1062 const bpf_u_int32 val
, const bpf_u_int32 maxval
)
1065 bpf_error(cstate
, "%s %u greater than maximum %u",
1069 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1070 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1072 // Validate a port/portrange proto qualifier and map to an IP protocol number.
1074 port_pq_to_ipproto(compiler_state_t
*cstate
, const int proto
, const char *kw
)
1082 return IPPROTO_SCTP
;
1086 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), kw
);
1090 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1091 const char *buf
, int optimize
, bpf_u_int32 mask
)
1097 compiler_state_t cstate
;
1098 yyscan_t scanner
= NULL
;
1099 YY_BUFFER_STATE in_buffer
= NULL
;
1104 * If this pcap_t hasn't been activated, it doesn't have a
1105 * link-layer type, so we can't use it.
1107 if (!p
->activated
) {
1108 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1109 "not-yet-activated pcap_t passed to pcap_compile");
1110 return (PCAP_ERROR
);
1115 * Initialize Winsock, asking for the latest version (2.2),
1116 * as we may be calling Winsock routines to translate
1117 * host names to addresses.
1119 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1121 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1122 err
, "Error calling WSAStartup()");
1123 return (PCAP_ERROR
);
1127 #ifdef ENABLE_REMOTE
1129 * If the device on which we're capturing need to be notified
1130 * that a new filter is being compiled, do so.
1132 * This allows them to save a copy of it, in case, for example,
1133 * they're implementing a form of remote packet capture, and
1134 * want the remote machine to filter out the packets in which
1135 * it's sending the packets it's captured.
1137 * XXX - the fact that we happen to be compiling a filter
1138 * doesn't necessarily mean we'll be installing it as the
1139 * filter for this pcap_t; we might be running it from userland
1140 * on captured packets to do packet classification. We really
1141 * need a better way of handling this, but this is all that
1142 * the WinPcap remote capture code did.
1144 if (p
->save_current_filter_op
!= NULL
)
1145 (p
->save_current_filter_op
)(p
, buf
);
1148 initchunks(&cstate
);
1149 cstate
.no_optimize
= 0;
1154 cstate
.ic
.root
= NULL
;
1155 cstate
.ic
.cur_mark
= 0;
1156 cstate
.bpf_pcap
= p
;
1157 cstate
.error_set
= 0;
1160 cstate
.netmask
= mask
;
1162 cstate
.snaplen
= pcap_snapshot(p
);
1163 if (cstate
.snaplen
== 0) {
1164 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1165 "snaplen of 0 rejects all packets");
1170 if (pcap_lex_init(&scanner
) != 0) {
1171 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1172 errno
, "can't initialize scanner");
1176 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1179 * Associate the compiler state with the lexical analyzer
1182 pcap_set_extra(&cstate
, scanner
);
1184 if (init_linktype(&cstate
, p
) == -1) {
1188 if (pcap_parse(scanner
, &cstate
) != 0) {
1190 if (cstate
.ai
!= NULL
)
1191 freeaddrinfo(cstate
.ai
);
1193 if (cstate
.e
!= NULL
)
1199 if (cstate
.ic
.root
== NULL
) {
1200 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1203 * Catch errors reported by gen_retblk().
1205 if (cstate
.ic
.root
== NULL
) {
1211 if (optimize
&& !cstate
.no_optimize
) {
1212 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1217 if (cstate
.ic
.root
== NULL
||
1218 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1219 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1220 "expression rejects all packets");
1225 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1226 cstate
.ic
.root
, &len
, p
->errbuf
);
1227 if (program
->bf_insns
== NULL
) {
1232 program
->bf_len
= len
;
1234 rc
= 0; /* We're all okay */
1238 * Clean up everything for the lexical analyzer.
1240 if (in_buffer
!= NULL
)
1241 pcap__delete_buffer(in_buffer
, scanner
);
1242 if (scanner
!= NULL
)
1243 pcap_lex_destroy(scanner
);
1246 * Clean up our own allocated memory.
1248 freechunks(&cstate
);
1258 * entry point for using the compiler with no pcap open
1259 * pass in all the stuff that is needed explicitly instead.
1262 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1263 struct bpf_program
*program
,
1264 const char *buf
, int optimize
, bpf_u_int32 mask
)
1269 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1271 return (PCAP_ERROR
);
1272 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1278 * Clean up a "struct bpf_program" by freeing all the memory allocated
1282 pcap_freecode(struct bpf_program
*program
)
1284 program
->bf_len
= 0;
1285 if (program
->bf_insns
!= NULL
) {
1286 free((char *)program
->bf_insns
);
1287 program
->bf_insns
= NULL
;
1292 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1293 * which of the jt and jf fields has been resolved and which is a pointer
1294 * back to another unresolved block (or nil). At least one of the fields
1295 * in each block is already resolved.
1298 backpatch(struct block
*list
, struct block
*target
)
1315 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1316 * which of jt and jf is the link.
1319 merge(struct block
*b0
, struct block
*b1
)
1321 register struct block
**p
= &b0
;
1323 /* Find end of list. */
1325 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1327 /* Concatenate the lists. */
1332 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1335 * Catch errors reported by us and routines below us, and return -1
1338 if (setjmp(cstate
->top_ctx
))
1342 * Insert before the statements of the first (root) block any
1343 * statements needed to load the lengths of any variable-length
1344 * headers into registers.
1346 * XXX - a fancier strategy would be to insert those before the
1347 * statements of all blocks that use those lengths and that
1348 * have no predecessors that use them, so that we only compute
1349 * the lengths if we need them. There might be even better
1350 * approaches than that.
1352 * However, those strategies would be more complicated, and
1353 * as we don't generate code to compute a length if the
1354 * program has no tests that use the length, and as most
1355 * tests will probably use those lengths, we would just
1356 * postpone computing the lengths so that it's not done
1357 * for tests that fail early, and it's not clear that's
1360 insert_compute_vloffsets(cstate
, p
->head
);
1363 * For DLT_PPI captures, generate a check of the per-packet
1364 * DLT value to make sure it's DLT_IEEE802_11.
1366 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1367 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1368 * with appropriate Ethernet information and use that rather
1369 * than using something such as DLT_PPI where you don't know
1370 * the link-layer header type until runtime, which, in the
1371 * general case, would force us to generate both Ethernet *and*
1372 * 802.11 code (*and* anything else for which PPI is used)
1373 * and choose between them early in the BPF program?
1375 if (cstate
->linktype
== DLT_PPI
) {
1376 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1377 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1378 gen_and(ppi_dlt_check
, p
);
1381 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1382 p
->sense
= !p
->sense
;
1383 backpatch(p
, gen_retblk_internal(cstate
, 0));
1384 cstate
->ic
.root
= p
->head
;
1389 gen_and(struct block
*b0
, struct block
*b1
)
1391 backpatch(b0
, b1
->head
);
1392 b0
->sense
= !b0
->sense
;
1393 b1
->sense
= !b1
->sense
;
1395 b1
->sense
= !b1
->sense
;
1396 b1
->head
= b0
->head
;
1400 gen_or(struct block
*b0
, struct block
*b1
)
1402 b0
->sense
= !b0
->sense
;
1403 backpatch(b0
, b1
->head
);
1404 b0
->sense
= !b0
->sense
;
1406 b1
->head
= b0
->head
;
1410 gen_not(struct block
*b
)
1412 b
->sense
= !b
->sense
;
1415 static struct block
*
1416 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1417 u_int size
, bpf_u_int32 v
)
1419 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1422 static struct block
*
1423 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1424 u_int size
, bpf_u_int32 v
)
1426 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1429 static struct block
*
1430 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1431 u_int size
, bpf_u_int32 v
)
1433 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1436 static struct block
*
1437 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1438 u_int size
, bpf_u_int32 v
)
1440 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1443 static struct block
*
1444 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1445 u_int size
, bpf_u_int32 v
)
1447 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1450 static struct block
*
1451 gen_cmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1452 u_int size
, bpf_u_int32 v
)
1454 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 1, v
);
1457 static struct block
*
1458 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1459 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1461 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1464 static struct block
*
1465 gen_mcmp_ne(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1466 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1468 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 1, v
);
1471 static struct block
*
1472 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1473 u_int size
, const u_char
*v
)
1475 register struct block
*b
, *tmp
;
1479 register const u_char
*p
= &v
[size
- 4];
1481 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1489 register const u_char
*p
= &v
[size
- 2];
1491 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1499 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1508 * AND the field of size "size" at offset "offset" relative to the header
1509 * specified by "offrel" with "mask", and compare it with the value "v"
1510 * with the test specified by "jtype"; if "reverse" is true, the test
1511 * should test the opposite of "jtype".
1513 static struct block
*
1514 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1515 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1518 struct slist
*s
, *s2
;
1521 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1523 if (mask
!= 0xffffffff) {
1524 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1529 b
= new_block(cstate
, JMP(jtype
));
1538 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1540 cstate
->pcap_fddipad
= p
->fddipad
;
1543 * We start out with only one link-layer header.
1545 cstate
->outermostlinktype
= pcap_datalink(p
);
1546 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1547 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1548 cstate
->off_outermostlinkhdr
.reg
= -1;
1550 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1551 cstate
->off_prevlinkhdr
.constant_part
= 0;
1552 cstate
->off_prevlinkhdr
.is_variable
= 0;
1553 cstate
->off_prevlinkhdr
.reg
= -1;
1555 cstate
->linktype
= cstate
->outermostlinktype
;
1556 cstate
->off_linkhdr
.constant_part
= 0;
1557 cstate
->off_linkhdr
.is_variable
= 0;
1558 cstate
->off_linkhdr
.reg
= -1;
1563 cstate
->off_linkpl
.constant_part
= 0;
1564 cstate
->off_linkpl
.is_variable
= 0;
1565 cstate
->off_linkpl
.reg
= -1;
1567 cstate
->off_linktype
.constant_part
= 0;
1568 cstate
->off_linktype
.is_variable
= 0;
1569 cstate
->off_linktype
.reg
= -1;
1572 * Assume it's not raw ATM with a pseudo-header, for now.
1575 cstate
->off_vpi
= OFFSET_NOT_SET
;
1576 cstate
->off_vci
= OFFSET_NOT_SET
;
1577 cstate
->off_proto
= OFFSET_NOT_SET
;
1578 cstate
->off_payload
= OFFSET_NOT_SET
;
1581 * And not encapsulated with either Geneve or VXLAN.
1583 cstate
->is_encap
= 0;
1586 * No variable length VLAN offset by default
1588 cstate
->is_vlan_vloffset
= 0;
1591 * And assume we're not doing SS7.
1593 cstate
->off_li
= OFFSET_NOT_SET
;
1594 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1595 cstate
->off_sio
= OFFSET_NOT_SET
;
1596 cstate
->off_opc
= OFFSET_NOT_SET
;
1597 cstate
->off_dpc
= OFFSET_NOT_SET
;
1598 cstate
->off_sls
= OFFSET_NOT_SET
;
1600 cstate
->label_stack_depth
= 0;
1601 cstate
->vlan_stack_depth
= 0;
1603 switch (cstate
->linktype
) {
1606 cstate
->off_linktype
.constant_part
= 2;
1607 cstate
->off_linkpl
.constant_part
= 6;
1608 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1609 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1612 case DLT_ARCNET_LINUX
:
1613 cstate
->off_linktype
.constant_part
= 4;
1614 cstate
->off_linkpl
.constant_part
= 8;
1615 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1616 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1620 cstate
->off_linktype
.constant_part
= 12;
1621 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1622 cstate
->off_nl
= 0; /* Ethernet II */
1623 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1628 * SLIP doesn't have a link level type. The 16 byte
1629 * header is hacked into our SLIP driver.
1631 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1632 cstate
->off_linkpl
.constant_part
= 16;
1634 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1637 case DLT_SLIP_BSDOS
:
1638 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1639 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1641 cstate
->off_linkpl
.constant_part
= 24;
1643 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1648 cstate
->off_linktype
.constant_part
= 0;
1649 cstate
->off_linkpl
.constant_part
= 4;
1651 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1655 cstate
->off_linktype
.constant_part
= 0;
1656 cstate
->off_linkpl
.constant_part
= 12;
1658 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1663 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1664 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1665 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1666 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1667 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1669 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1674 * This does not include the Ethernet header, and
1675 * only covers session state.
1677 cstate
->off_linktype
.constant_part
= 6;
1678 cstate
->off_linkpl
.constant_part
= 8;
1680 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1684 cstate
->off_linktype
.constant_part
= 5;
1685 cstate
->off_linkpl
.constant_part
= 24;
1687 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1692 * FDDI doesn't really have a link-level type field.
1693 * We set "off_linktype" to the offset of the LLC header.
1695 * To check for Ethernet types, we assume that SSAP = SNAP
1696 * is being used and pick out the encapsulated Ethernet type.
1697 * XXX - should we generate code to check for SNAP?
1699 cstate
->off_linktype
.constant_part
= 13;
1700 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1701 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1702 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1703 cstate
->off_nl
= 8; /* 802.2+SNAP */
1704 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1709 * Token Ring doesn't really have a link-level type field.
1710 * We set "off_linktype" to the offset of the LLC header.
1712 * To check for Ethernet types, we assume that SSAP = SNAP
1713 * is being used and pick out the encapsulated Ethernet type.
1714 * XXX - should we generate code to check for SNAP?
1716 * XXX - the header is actually variable-length.
1717 * Some various Linux patched versions gave 38
1718 * as "off_linktype" and 40 as "off_nl"; however,
1719 * if a token ring packet has *no* routing
1720 * information, i.e. is not source-routed, the correct
1721 * values are 20 and 22, as they are in the vanilla code.
1723 * A packet is source-routed iff the uppermost bit
1724 * of the first byte of the source address, at an
1725 * offset of 8, has the uppermost bit set. If the
1726 * packet is source-routed, the total number of bytes
1727 * of routing information is 2 plus bits 0x1F00 of
1728 * the 16-bit value at an offset of 14 (shifted right
1729 * 8 - figure out which byte that is).
1731 cstate
->off_linktype
.constant_part
= 14;
1732 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1733 cstate
->off_nl
= 8; /* 802.2+SNAP */
1734 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1737 case DLT_PRISM_HEADER
:
1738 case DLT_IEEE802_11_RADIO_AVS
:
1739 case DLT_IEEE802_11_RADIO
:
1740 cstate
->off_linkhdr
.is_variable
= 1;
1741 /* Fall through, 802.11 doesn't have a variable link
1742 * prefix but is otherwise the same. */
1745 case DLT_IEEE802_11
:
1747 * 802.11 doesn't really have a link-level type field.
1748 * We set "off_linktype.constant_part" to the offset of
1751 * To check for Ethernet types, we assume that SSAP = SNAP
1752 * is being used and pick out the encapsulated Ethernet type.
1753 * XXX - should we generate code to check for SNAP?
1755 * We also handle variable-length radio headers here.
1756 * The Prism header is in theory variable-length, but in
1757 * practice it's always 144 bytes long. However, some
1758 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1759 * sometimes or always supply an AVS header, so we
1760 * have to check whether the radio header is a Prism
1761 * header or an AVS header, so, in practice, it's
1764 cstate
->off_linktype
.constant_part
= 24;
1765 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1766 cstate
->off_linkpl
.is_variable
= 1;
1767 cstate
->off_nl
= 8; /* 802.2+SNAP */
1768 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1773 * At the moment we treat PPI the same way that we treat
1774 * normal Radiotap encoded packets. The difference is in
1775 * the function that generates the code at the beginning
1776 * to compute the header length. Since this code generator
1777 * of PPI supports bare 802.11 encapsulation only (i.e.
1778 * the encapsulated DLT should be DLT_IEEE802_11) we
1779 * generate code to check for this too.
1781 cstate
->off_linktype
.constant_part
= 24;
1782 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1783 cstate
->off_linkpl
.is_variable
= 1;
1784 cstate
->off_linkhdr
.is_variable
= 1;
1785 cstate
->off_nl
= 8; /* 802.2+SNAP */
1786 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1789 case DLT_ATM_RFC1483
:
1790 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1792 * assume routed, non-ISO PDUs
1793 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1795 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1796 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1797 * latter would presumably be treated the way PPPoE
1798 * should be, so you can do "pppoe and udp port 2049"
1799 * or "pppoa and tcp port 80" and have it check for
1800 * PPPo{A,E} and a PPP protocol of IP and....
1802 cstate
->off_linktype
.constant_part
= 0;
1803 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1804 cstate
->off_nl
= 8; /* 802.2+SNAP */
1805 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1810 * Full Frontal ATM; you get AALn PDUs with an ATM
1814 cstate
->off_vpi
= SUNATM_VPI_POS
;
1815 cstate
->off_vci
= SUNATM_VCI_POS
;
1816 cstate
->off_proto
= PROTO_POS
;
1817 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1818 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1819 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1820 cstate
->off_nl
= 8; /* 802.2+SNAP */
1821 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1827 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1828 cstate
->off_linkpl
.constant_part
= 0;
1830 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1833 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1834 cstate
->off_linktype
.constant_part
= 14;
1835 cstate
->off_linkpl
.constant_part
= 16;
1837 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1840 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1841 cstate
->off_linktype
.constant_part
= 0;
1842 cstate
->off_linkpl
.constant_part
= 20;
1844 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1849 * LocalTalk does have a 1-byte type field in the LLAP header,
1850 * but really it just indicates whether there is a "short" or
1851 * "long" DDP packet following.
1853 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1854 cstate
->off_linkpl
.constant_part
= 0;
1856 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1859 case DLT_IP_OVER_FC
:
1861 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1862 * link-level type field. We set "off_linktype" to the
1863 * offset of the LLC header.
1865 * To check for Ethernet types, we assume that SSAP = SNAP
1866 * is being used and pick out the encapsulated Ethernet type.
1867 * XXX - should we generate code to check for SNAP? RFC
1868 * 2625 says SNAP should be used.
1870 cstate
->off_linktype
.constant_part
= 16;
1871 cstate
->off_linkpl
.constant_part
= 16;
1872 cstate
->off_nl
= 8; /* 802.2+SNAP */
1873 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1878 * XXX - we should set this to handle SNAP-encapsulated
1879 * frames (NLPID of 0x80).
1881 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1882 cstate
->off_linkpl
.constant_part
= 0;
1884 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1888 * the only BPF-interesting FRF.16 frames are non-control frames;
1889 * Frame Relay has a variable length link-layer
1890 * so lets start with offset 4 for now and increments later on (FIXME);
1893 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1894 cstate
->off_linkpl
.constant_part
= 0;
1896 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1899 case DLT_APPLE_IP_OVER_IEEE1394
:
1900 cstate
->off_linktype
.constant_part
= 16;
1901 cstate
->off_linkpl
.constant_part
= 18;
1903 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1906 case DLT_SYMANTEC_FIREWALL
:
1907 cstate
->off_linktype
.constant_part
= 6;
1908 cstate
->off_linkpl
.constant_part
= 44;
1909 cstate
->off_nl
= 0; /* Ethernet II */
1910 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1914 cstate
->off_linktype
.constant_part
= 0;
1915 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1916 cstate
->off_linkpl
.is_variable
= 1;
1918 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1921 case DLT_JUNIPER_MFR
:
1922 case DLT_JUNIPER_MLFR
:
1923 case DLT_JUNIPER_MLPPP
:
1924 case DLT_JUNIPER_PPP
:
1925 case DLT_JUNIPER_CHDLC
:
1926 case DLT_JUNIPER_FRELAY
:
1927 cstate
->off_linktype
.constant_part
= 4;
1928 cstate
->off_linkpl
.constant_part
= 4;
1930 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1933 case DLT_JUNIPER_ATM1
:
1934 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1935 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1937 cstate
->off_nl_nosnap
= 10;
1940 case DLT_JUNIPER_ATM2
:
1941 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1942 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1944 cstate
->off_nl_nosnap
= 10;
1947 /* frames captured on a Juniper PPPoE service PIC
1948 * contain raw ethernet frames */
1949 case DLT_JUNIPER_PPPOE
:
1950 case DLT_JUNIPER_ETHER
:
1951 cstate
->off_linkpl
.constant_part
= 14;
1952 cstate
->off_linktype
.constant_part
= 16;
1953 cstate
->off_nl
= 18; /* Ethernet II */
1954 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1957 case DLT_JUNIPER_PPPOE_ATM
:
1958 cstate
->off_linktype
.constant_part
= 4;
1959 cstate
->off_linkpl
.constant_part
= 6;
1961 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1964 case DLT_JUNIPER_GGSN
:
1965 cstate
->off_linktype
.constant_part
= 6;
1966 cstate
->off_linkpl
.constant_part
= 12;
1968 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1971 case DLT_JUNIPER_ES
:
1972 cstate
->off_linktype
.constant_part
= 6;
1973 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1974 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1975 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1978 case DLT_JUNIPER_MONITOR
:
1979 cstate
->off_linktype
.constant_part
= 12;
1980 cstate
->off_linkpl
.constant_part
= 12;
1981 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1982 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1985 case DLT_BACNET_MS_TP
:
1986 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1987 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1988 cstate
->off_nl
= OFFSET_NOT_SET
;
1989 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1992 case DLT_JUNIPER_SERVICES
:
1993 cstate
->off_linktype
.constant_part
= 12;
1994 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1995 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1996 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1999 case DLT_JUNIPER_VP
:
2000 cstate
->off_linktype
.constant_part
= 18;
2001 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2002 cstate
->off_nl
= OFFSET_NOT_SET
;
2003 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2006 case DLT_JUNIPER_ST
:
2007 cstate
->off_linktype
.constant_part
= 18;
2008 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2009 cstate
->off_nl
= OFFSET_NOT_SET
;
2010 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2013 case DLT_JUNIPER_ISM
:
2014 cstate
->off_linktype
.constant_part
= 8;
2015 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2016 cstate
->off_nl
= OFFSET_NOT_SET
;
2017 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2020 case DLT_JUNIPER_VS
:
2021 case DLT_JUNIPER_SRX_E2E
:
2022 case DLT_JUNIPER_FIBRECHANNEL
:
2023 case DLT_JUNIPER_ATM_CEMIC
:
2024 cstate
->off_linktype
.constant_part
= 8;
2025 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2026 cstate
->off_nl
= OFFSET_NOT_SET
;
2027 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2032 cstate
->off_li_hsl
= 4;
2033 cstate
->off_sio
= 3;
2034 cstate
->off_opc
= 4;
2035 cstate
->off_dpc
= 4;
2036 cstate
->off_sls
= 7;
2037 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2038 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2039 cstate
->off_nl
= OFFSET_NOT_SET
;
2040 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2043 case DLT_MTP2_WITH_PHDR
:
2045 cstate
->off_li_hsl
= 8;
2046 cstate
->off_sio
= 7;
2047 cstate
->off_opc
= 8;
2048 cstate
->off_dpc
= 8;
2049 cstate
->off_sls
= 11;
2050 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2051 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2052 cstate
->off_nl
= OFFSET_NOT_SET
;
2053 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2057 cstate
->off_li
= 22;
2058 cstate
->off_li_hsl
= 24;
2059 cstate
->off_sio
= 23;
2060 cstate
->off_opc
= 24;
2061 cstate
->off_dpc
= 24;
2062 cstate
->off_sls
= 27;
2063 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2064 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2065 cstate
->off_nl
= OFFSET_NOT_SET
;
2066 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2070 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2071 cstate
->off_linkpl
.constant_part
= 4;
2073 cstate
->off_nl_nosnap
= 0;
2078 * Currently, only raw "link[N:M]" filtering is supported.
2080 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
2081 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2082 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
2083 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2087 cstate
->off_linktype
.constant_part
= 1;
2088 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2090 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2093 case DLT_NETANALYZER
:
2094 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
2095 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2096 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
2097 cstate
->off_nl
= 0; /* Ethernet II */
2098 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2101 case DLT_NETANALYZER_TRANSPARENT
:
2102 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2103 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2104 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2105 cstate
->off_nl
= 0; /* Ethernet II */
2106 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2111 * For values in the range in which we've assigned new
2112 * DLT_ values, only raw "link[N:M]" filtering is supported.
2114 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2115 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2116 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2117 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2118 cstate
->off_nl
= OFFSET_NOT_SET
;
2119 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2121 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2122 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2128 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2133 * Load a value relative to the specified absolute offset.
2135 static struct slist
*
2136 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2137 u_int offset
, u_int size
)
2139 struct slist
*s
, *s2
;
2141 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2144 * If "s" is non-null, it has code to arrange that the X register
2145 * contains the variable part of the absolute offset, so we
2146 * generate a load relative to that, with an offset of
2147 * abs_offset->constant_part + offset.
2149 * Otherwise, we can do an absolute load with an offset of
2150 * abs_offset->constant_part + offset.
2154 * "s" points to a list of statements that puts the
2155 * variable part of the absolute offset into the X register.
2156 * Do an indirect load, to use the X register as an offset.
2158 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2159 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2163 * There is no variable part of the absolute offset, so
2164 * just do an absolute load.
2166 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2167 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2173 * Load a value relative to the beginning of the specified header.
2175 static struct slist
*
2176 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2179 struct slist
*s
, *s2
;
2182 * Squelch warnings from compilers that *don't* assume that
2183 * offrel always has a valid enum value and therefore don't
2184 * assume that we'll always go through one of the case arms.
2186 * If we have a default case, compilers that *do* assume that
2187 * will then complain about the default case code being
2190 * Damned if you do, damned if you don't.
2197 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2202 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2205 case OR_PREVLINKHDR
:
2206 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2210 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2213 case OR_PREVMPLSHDR
:
2214 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2218 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2221 case OR_LINKPL_NOSNAP
:
2222 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2226 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2231 * Load the X register with the length of the IPv4 header
2232 * (plus the offset of the link-layer header, if it's
2233 * preceded by a variable-length header such as a radio
2234 * header), in bytes.
2236 s
= gen_loadx_iphdrlen(cstate
);
2239 * Load the item at {offset of the link-layer payload} +
2240 * {offset, relative to the start of the link-layer
2241 * payload, of the IPv4 header} + {length of the IPv4 header} +
2242 * {specified offset}.
2244 * If the offset of the link-layer payload is variable,
2245 * the variable part of that offset is included in the
2246 * value in the X register, and we include the constant
2247 * part in the offset of the load.
2249 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2250 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2255 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2262 * Generate code to load into the X register the sum of the length of
2263 * the IPv4 header and the variable part of the offset of the link-layer
2266 static struct slist
*
2267 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2269 struct slist
*s
, *s2
;
2271 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2274 * The offset of the link-layer payload has a variable
2275 * part. "s" points to a list of statements that put
2276 * the variable part of that offset into the X register.
2278 * The 4*([k]&0xf) addressing mode can't be used, as we
2279 * don't have a constant offset, so we have to load the
2280 * value in question into the A register and add to it
2281 * the value from the X register.
2283 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2284 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2286 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2289 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2294 * The A register now contains the length of the IP header.
2295 * We need to add to it the variable part of the offset of
2296 * the link-layer payload, which is still in the X
2297 * register, and move the result into the X register.
2299 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2300 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2303 * The offset of the link-layer payload is a constant,
2304 * so no code was generated to load the (nonexistent)
2305 * variable part of that offset.
2307 * This means we can use the 4*([k]&0xf) addressing
2308 * mode. Load the length of the IPv4 header, which
2309 * is at an offset of cstate->off_nl from the beginning of
2310 * the link-layer payload, and thus at an offset of
2311 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2312 * of the raw packet data, using that addressing mode.
2314 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2315 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2321 static struct block
*
2322 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2327 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2329 b
= new_block(cstate
, JMP(BPF_JEQ
));
2335 static inline struct block
*
2336 gen_true(compiler_state_t
*cstate
)
2338 return gen_uncond(cstate
, 1);
2341 static inline struct block
*
2342 gen_false(compiler_state_t
*cstate
)
2344 return gen_uncond(cstate
, 0);
2348 * Generate code to match a particular packet type.
2350 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2351 * value, if <= ETHERMTU. We use that to determine whether to
2352 * match the type/length field or to check the type/length field for
2353 * a value <= ETHERMTU to see whether it's a type field and then do
2354 * the appropriate test.
2356 static struct block
*
2357 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2359 struct block
*b0
, *b1
;
2365 case LLCSAP_NETBEUI
:
2367 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2368 * so we check the DSAP and SSAP.
2370 * LLCSAP_IP checks for IP-over-802.2, rather
2371 * than IP-over-Ethernet or IP-over-SNAP.
2373 * XXX - should we check both the DSAP and the
2374 * SSAP, like this, or should we check just the
2375 * DSAP, as we do for other types <= ETHERMTU
2376 * (i.e., other SAP values)?
2378 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2379 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2387 * Ethernet_II frames, which are Ethernet
2388 * frames with a frame type of ETHERTYPE_IPX;
2390 * Ethernet_802.3 frames, which are 802.3
2391 * frames (i.e., the type/length field is
2392 * a length field, <= ETHERMTU, rather than
2393 * a type field) with the first two bytes
2394 * after the Ethernet/802.3 header being
2397 * Ethernet_802.2 frames, which are 802.3
2398 * frames with an 802.2 LLC header and
2399 * with the IPX LSAP as the DSAP in the LLC
2402 * Ethernet_SNAP frames, which are 802.3
2403 * frames with an LLC header and a SNAP
2404 * header and with an OUI of 0x000000
2405 * (encapsulated Ethernet) and a protocol
2406 * ID of ETHERTYPE_IPX in the SNAP header.
2408 * XXX - should we generate the same code both
2409 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2413 * This generates code to check both for the
2414 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2416 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2417 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2421 * Now we add code to check for SNAP frames with
2422 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2424 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2428 * Now we generate code to check for 802.3
2429 * frames in general.
2431 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2434 * Now add the check for 802.3 frames before the
2435 * check for Ethernet_802.2 and Ethernet_802.3,
2436 * as those checks should only be done on 802.3
2437 * frames, not on Ethernet frames.
2442 * Now add the check for Ethernet_II frames, and
2443 * do that before checking for the other frame
2446 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2450 case ETHERTYPE_ATALK
:
2451 case ETHERTYPE_AARP
:
2453 * EtherTalk (AppleTalk protocols on Ethernet link
2454 * layer) may use 802.2 encapsulation.
2458 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2459 * we check for an Ethernet type field less or equal than
2460 * 1500, which means it's an 802.3 length field.
2462 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2465 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2466 * SNAP packets with an organization code of
2467 * 0x080007 (Apple, for Appletalk) and a protocol
2468 * type of ETHERTYPE_ATALK (Appletalk).
2470 * 802.2-encapsulated ETHERTYPE_AARP packets are
2471 * SNAP packets with an organization code of
2472 * 0x000000 (encapsulated Ethernet) and a protocol
2473 * type of ETHERTYPE_AARP (Appletalk ARP).
2475 if (ll_proto
== ETHERTYPE_ATALK
)
2476 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2477 else /* ll_proto == ETHERTYPE_AARP */
2478 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2482 * Check for Ethernet encapsulation (Ethertalk
2483 * phase 1?); we just check for the Ethernet
2486 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2492 if (ll_proto
<= ETHERMTU
) {
2494 * This is an LLC SAP value, so the frames
2495 * that match would be 802.2 frames.
2496 * Check that the frame is an 802.2 frame
2497 * (i.e., that the length/type field is
2498 * a length field, <= ETHERMTU) and
2499 * then check the DSAP.
2501 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2502 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2507 * This is an Ethernet type, so compare
2508 * the length/type field with it (if
2509 * the frame is an 802.2 frame, the length
2510 * field will be <= ETHERMTU, and, as
2511 * "ll_proto" is > ETHERMTU, this test
2512 * will fail and the frame won't match,
2513 * which is what we want).
2515 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2520 static struct block
*
2521 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2524 * For DLT_NULL, the link-layer header is a 32-bit word
2525 * containing an AF_ value in *host* byte order, and for
2526 * DLT_ENC, the link-layer header begins with a 32-bit
2527 * word containing an AF_ value in host byte order.
2529 * In addition, if we're reading a saved capture file,
2530 * the host byte order in the capture may not be the
2531 * same as the host byte order on this machine.
2533 * For DLT_LOOP, the link-layer header is a 32-bit
2534 * word containing an AF_ value in *network* byte order.
2536 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2538 * The AF_ value is in host byte order, but the BPF
2539 * interpreter will convert it to network byte order.
2541 * If this is a save file, and it's from a machine
2542 * with the opposite byte order to ours, we byte-swap
2545 * Then we run it through "htonl()", and generate
2546 * code to compare against the result.
2548 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2549 ll_proto
= SWAPLONG(ll_proto
);
2550 ll_proto
= htonl(ll_proto
);
2552 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2556 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2557 * or IPv6 then we have an error.
2559 static struct block
*
2560 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2565 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2568 case ETHERTYPE_IPV6
:
2569 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2576 return gen_false(cstate
);
2580 * Generate code to match a particular packet type.
2582 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2583 * value, if <= ETHERMTU. We use that to determine whether to
2584 * match the type field or to check the type field for the special
2585 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2587 static struct block
*
2588 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2590 struct block
*b0
, *b1
;
2596 case LLCSAP_NETBEUI
:
2598 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2599 * so we check the DSAP and SSAP.
2601 * LLCSAP_IP checks for IP-over-802.2, rather
2602 * than IP-over-Ethernet or IP-over-SNAP.
2604 * XXX - should we check both the DSAP and the
2605 * SSAP, like this, or should we check just the
2606 * DSAP, as we do for other types <= ETHERMTU
2607 * (i.e., other SAP values)?
2609 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2610 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2616 * Ethernet_II frames, which are Ethernet
2617 * frames with a frame type of ETHERTYPE_IPX;
2619 * Ethernet_802.3 frames, which have a frame
2620 * type of LINUX_SLL_P_802_3;
2622 * Ethernet_802.2 frames, which are 802.3
2623 * frames with an 802.2 LLC header (i.e, have
2624 * a frame type of LINUX_SLL_P_802_2) and
2625 * with the IPX LSAP as the DSAP in the LLC
2628 * Ethernet_SNAP frames, which are 802.3
2629 * frames with an LLC header and a SNAP
2630 * header and with an OUI of 0x000000
2631 * (encapsulated Ethernet) and a protocol
2632 * ID of ETHERTYPE_IPX in the SNAP header.
2634 * First, do the checks on LINUX_SLL_P_802_2
2635 * frames; generate the check for either
2636 * Ethernet_802.2 or Ethernet_SNAP frames, and
2637 * then put a check for LINUX_SLL_P_802_2 frames
2640 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2641 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2643 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2647 * Now check for 802.3 frames and OR that with
2648 * the previous test.
2650 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2654 * Now add the check for Ethernet_II frames, and
2655 * do that before checking for the other frame
2658 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2662 case ETHERTYPE_ATALK
:
2663 case ETHERTYPE_AARP
:
2665 * EtherTalk (AppleTalk protocols on Ethernet link
2666 * layer) may use 802.2 encapsulation.
2670 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2671 * we check for the 802.2 protocol type in the
2672 * "Ethernet type" field.
2674 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2677 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2678 * SNAP packets with an organization code of
2679 * 0x080007 (Apple, for Appletalk) and a protocol
2680 * type of ETHERTYPE_ATALK (Appletalk).
2682 * 802.2-encapsulated ETHERTYPE_AARP packets are
2683 * SNAP packets with an organization code of
2684 * 0x000000 (encapsulated Ethernet) and a protocol
2685 * type of ETHERTYPE_AARP (Appletalk ARP).
2687 if (ll_proto
== ETHERTYPE_ATALK
)
2688 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2689 else /* ll_proto == ETHERTYPE_AARP */
2690 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2694 * Check for Ethernet encapsulation (Ethertalk
2695 * phase 1?); we just check for the Ethernet
2698 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2704 if (ll_proto
<= ETHERMTU
) {
2706 * This is an LLC SAP value, so the frames
2707 * that match would be 802.2 frames.
2708 * Check for the 802.2 protocol type
2709 * in the "Ethernet type" field, and
2710 * then check the DSAP.
2712 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2713 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2719 * This is an Ethernet type, so compare
2720 * the length/type field with it (if
2721 * the frame is an 802.2 frame, the length
2722 * field will be <= ETHERMTU, and, as
2723 * "ll_proto" is > ETHERMTU, this test
2724 * will fail and the frame won't match,
2725 * which is what we want).
2727 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2733 * Load a value relative to the beginning of the link-layer header after the
2736 static struct slist
*
2737 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2739 struct slist
*s1
, *s2
;
2742 * Generate code to load the length of the pflog header into
2743 * the register assigned to hold that length, if one has been
2744 * assigned. (If one hasn't been assigned, no code we've
2745 * generated uses that prefix, so we don't need to generate any
2748 if (cstate
->off_linkpl
.reg
!= -1) {
2750 * The length is in the first byte of the header.
2752 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2756 * Round it up to a multiple of 4.
2757 * Add 3, and clear the lower 2 bits.
2759 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2762 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2763 s2
->s
.k
= 0xfffffffc;
2767 * Now allocate a register to hold that value and store
2770 s2
= new_stmt(cstate
, BPF_ST
);
2771 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2775 * Now move it into the X register.
2777 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2785 static struct slist
*
2786 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2788 struct slist
*s1
, *s2
;
2789 struct slist
*sjeq_avs_cookie
;
2790 struct slist
*sjcommon
;
2793 * This code is not compatible with the optimizer, as
2794 * we are generating jmp instructions within a normal
2795 * slist of instructions
2797 cstate
->no_optimize
= 1;
2800 * Generate code to load the length of the radio header into
2801 * the register assigned to hold that length, if one has been
2802 * assigned. (If one hasn't been assigned, no code we've
2803 * generated uses that prefix, so we don't need to generate any
2806 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2807 * or always use the AVS header rather than the Prism header.
2808 * We load a 4-byte big-endian value at the beginning of the
2809 * raw packet data, and see whether, when masked with 0xFFFFF000,
2810 * it's equal to 0x80211000. If so, that indicates that it's
2811 * an AVS header (the masked-out bits are the version number).
2812 * Otherwise, it's a Prism header.
2814 * XXX - the Prism header is also, in theory, variable-length,
2815 * but no known software generates headers that aren't 144
2818 if (cstate
->off_linkhdr
.reg
!= -1) {
2822 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2826 * AND it with 0xFFFFF000.
2828 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2829 s2
->s
.k
= 0xFFFFF000;
2833 * Compare with 0x80211000.
2835 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2836 sjeq_avs_cookie
->s
.k
= 0x80211000;
2837 sappend(s1
, sjeq_avs_cookie
);
2842 * The 4 bytes at an offset of 4 from the beginning of
2843 * the AVS header are the length of the AVS header.
2844 * That field is big-endian.
2846 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2849 sjeq_avs_cookie
->s
.jt
= s2
;
2852 * Now jump to the code to allocate a register
2853 * into which to save the header length and
2854 * store the length there. (The "jump always"
2855 * instruction needs to have the k field set;
2856 * it's added to the PC, so, as we're jumping
2857 * over a single instruction, it should be 1.)
2859 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2861 sappend(s1
, sjcommon
);
2864 * Now for the code that handles the Prism header.
2865 * Just load the length of the Prism header (144)
2866 * into the A register. Have the test for an AVS
2867 * header branch here if we don't have an AVS header.
2869 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2872 sjeq_avs_cookie
->s
.jf
= s2
;
2875 * Now allocate a register to hold that value and store
2876 * it. The code for the AVS header will jump here after
2877 * loading the length of the AVS header.
2879 s2
= new_stmt(cstate
, BPF_ST
);
2880 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2882 sjcommon
->s
.jf
= s2
;
2885 * Now move it into the X register.
2887 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2895 static struct slist
*
2896 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2898 struct slist
*s1
, *s2
;
2901 * Generate code to load the length of the AVS header into
2902 * the register assigned to hold that length, if one has been
2903 * assigned. (If one hasn't been assigned, no code we've
2904 * generated uses that prefix, so we don't need to generate any
2907 if (cstate
->off_linkhdr
.reg
!= -1) {
2909 * The 4 bytes at an offset of 4 from the beginning of
2910 * the AVS header are the length of the AVS header.
2911 * That field is big-endian.
2913 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2917 * Now allocate a register to hold that value and store
2920 s2
= new_stmt(cstate
, BPF_ST
);
2921 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2925 * Now move it into the X register.
2927 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2935 static struct slist
*
2936 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2938 struct slist
*s1
, *s2
;
2941 * Generate code to load the length of the radiotap header into
2942 * the register assigned to hold that length, if one has been
2943 * assigned. (If one hasn't been assigned, no code we've
2944 * generated uses that prefix, so we don't need to generate any
2947 if (cstate
->off_linkhdr
.reg
!= -1) {
2949 * The 2 bytes at offsets of 2 and 3 from the beginning
2950 * of the radiotap header are the length of the radiotap
2951 * header; unfortunately, it's little-endian, so we have
2952 * to load it a byte at a time and construct the value.
2956 * Load the high-order byte, at an offset of 3, shift it
2957 * left a byte, and put the result in the X register.
2959 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2961 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2964 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2968 * Load the next byte, at an offset of 2, and OR the
2969 * value from the X register into it.
2971 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2974 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2978 * Now allocate a register to hold that value and store
2981 s2
= new_stmt(cstate
, BPF_ST
);
2982 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2986 * Now move it into the X register.
2988 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2997 * At the moment we treat PPI as normal Radiotap encoded
2998 * packets. The difference is in the function that generates
2999 * the code at the beginning to compute the header length.
3000 * Since this code generator of PPI supports bare 802.11
3001 * encapsulation only (i.e. the encapsulated DLT should be
3002 * DLT_IEEE802_11) we generate code to check for this too;
3003 * that's done in finish_parse().
3005 static struct slist
*
3006 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
3008 struct slist
*s1
, *s2
;
3011 * Generate code to load the length of the radiotap header
3012 * into the register assigned to hold that length, if one has
3015 if (cstate
->off_linkhdr
.reg
!= -1) {
3017 * The 2 bytes at offsets of 2 and 3 from the beginning
3018 * of the radiotap header are the length of the radiotap
3019 * header; unfortunately, it's little-endian, so we have
3020 * to load it a byte at a time and construct the value.
3024 * Load the high-order byte, at an offset of 3, shift it
3025 * left a byte, and put the result in the X register.
3027 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3029 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
3032 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3036 * Load the next byte, at an offset of 2, and OR the
3037 * value from the X register into it.
3039 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3042 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3046 * Now allocate a register to hold that value and store
3049 s2
= new_stmt(cstate
, BPF_ST
);
3050 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3054 * Now move it into the X register.
3056 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3065 * Load a value relative to the beginning of the link-layer header after the 802.11
3066 * header, i.e. LLC_SNAP.
3067 * The link-layer header doesn't necessarily begin at the beginning
3068 * of the packet data; there might be a variable-length prefix containing
3069 * radio information.
3071 static struct slist
*
3072 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
3075 struct slist
*sjset_data_frame_1
;
3076 struct slist
*sjset_data_frame_2
;
3077 struct slist
*sjset_qos
;
3078 struct slist
*sjset_radiotap_flags_present
;
3079 struct slist
*sjset_radiotap_ext_present
;
3080 struct slist
*sjset_radiotap_tsft_present
;
3081 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
3082 struct slist
*s_roundup
;
3084 if (cstate
->off_linkpl
.reg
== -1) {
3086 * No register has been assigned to the offset of
3087 * the link-layer payload, which means nobody needs
3088 * it; don't bother computing it - just return
3089 * what we already have.
3095 * This code is not compatible with the optimizer, as
3096 * we are generating jmp instructions within a normal
3097 * slist of instructions
3099 cstate
->no_optimize
= 1;
3102 * If "s" is non-null, it has code to arrange that the X register
3103 * contains the length of the prefix preceding the link-layer
3106 * Otherwise, the length of the prefix preceding the link-layer
3107 * header is "off_outermostlinkhdr.constant_part".
3111 * There is no variable-length header preceding the
3112 * link-layer header.
3114 * Load the length of the fixed-length prefix preceding
3115 * the link-layer header (if any) into the X register,
3116 * and store it in the cstate->off_linkpl.reg register.
3117 * That length is off_outermostlinkhdr.constant_part.
3119 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3120 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3124 * The X register contains the offset of the beginning of the
3125 * link-layer header; add 24, which is the minimum length
3126 * of the MAC header for a data frame, to that, and store it
3127 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3128 * which is at the offset in the X register, with an indexed load.
3130 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3132 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3135 s2
= new_stmt(cstate
, BPF_ST
);
3136 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3139 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3144 * Check the Frame Control field to see if this is a data frame;
3145 * a data frame has the 0x08 bit (b3) in that field set and the
3146 * 0x04 bit (b2) clear.
3148 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3149 sjset_data_frame_1
->s
.k
= IEEE80211_FC0_TYPE_DATA
;
3150 sappend(s
, sjset_data_frame_1
);
3153 * If b3 is set, test b2, otherwise go to the first statement of
3154 * the rest of the program.
3156 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3157 sjset_data_frame_2
->s
.k
= IEEE80211_FC0_TYPE_CTL
;
3158 sappend(s
, sjset_data_frame_2
);
3159 sjset_data_frame_1
->s
.jf
= snext
;
3162 * If b2 is not set, this is a data frame; test the QoS bit.
3163 * Otherwise, go to the first statement of the rest of the
3166 sjset_data_frame_2
->s
.jt
= snext
;
3167 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3168 sjset_qos
->s
.k
= IEEE80211_FC0_SUBTYPE_QOS
;
3169 sappend(s
, sjset_qos
);
3172 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3174 * Otherwise, go to the first statement of the rest of the
3177 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3178 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3180 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3183 s2
= new_stmt(cstate
, BPF_ST
);
3184 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3188 * If we have a radiotap header, look at it to see whether
3189 * there's Atheros padding between the MAC-layer header
3192 * Note: all of the fields in the radiotap header are
3193 * little-endian, so we byte-swap all of the values
3194 * we test against, as they will be loaded as big-endian
3197 * XXX - in the general case, we would have to scan through
3198 * *all* the presence bits, if there's more than one word of
3199 * presence bits. That would require a loop, meaning that
3200 * we wouldn't be able to run the filter in the kernel.
3202 * We assume here that the Atheros adapters that insert the
3203 * annoying padding don't have multiple antennae and therefore
3204 * do not generate radiotap headers with multiple presence words.
3206 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3208 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3209 * in the first presence flag word?
3211 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3215 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3216 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3217 sappend(s
, sjset_radiotap_flags_present
);
3220 * If not, skip all of this.
3222 sjset_radiotap_flags_present
->s
.jf
= snext
;
3225 * Otherwise, is the "extension" bit set in that word?
3227 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3228 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3229 sappend(s
, sjset_radiotap_ext_present
);
3230 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3233 * If so, skip all of this.
3235 sjset_radiotap_ext_present
->s
.jt
= snext
;
3238 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3240 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3241 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3242 sappend(s
, sjset_radiotap_tsft_present
);
3243 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3246 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3247 * at an offset of 16 from the beginning of the raw packet
3248 * data (8 bytes for the radiotap header and 8 bytes for
3251 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3254 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3257 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3259 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3260 sjset_tsft_datapad
->s
.k
= 0x20;
3261 sappend(s
, sjset_tsft_datapad
);
3264 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3265 * at an offset of 8 from the beginning of the raw packet
3266 * data (8 bytes for the radiotap header).
3268 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3271 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3274 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3276 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3277 sjset_notsft_datapad
->s
.k
= 0x20;
3278 sappend(s
, sjset_notsft_datapad
);
3281 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3282 * set, round the length of the 802.11 header to
3283 * a multiple of 4. Do that by adding 3 and then
3284 * dividing by and multiplying by 4, which we do by
3287 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3288 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3289 sappend(s
, s_roundup
);
3290 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3293 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3294 s2
->s
.k
= (bpf_u_int32
)~3;
3296 s2
= new_stmt(cstate
, BPF_ST
);
3297 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3300 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3301 sjset_tsft_datapad
->s
.jf
= snext
;
3302 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3303 sjset_notsft_datapad
->s
.jf
= snext
;
3305 sjset_qos
->s
.jf
= snext
;
3311 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3315 /* There is an implicit dependency between the link
3316 * payload and link header since the payload computation
3317 * includes the variable part of the header. Therefore,
3318 * if nobody else has allocated a register for the link
3319 * header and we need it, do it now. */
3320 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3321 cstate
->off_linkhdr
.reg
== -1)
3322 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3325 * For link-layer types that have a variable-length header
3326 * preceding the link-layer header, generate code to load
3327 * the offset of the link-layer header into the register
3328 * assigned to that offset, if any.
3330 * XXX - this, and the next switch statement, won't handle
3331 * encapsulation of 802.11 or 802.11+radio information in
3332 * some other protocol stack. That's significantly more
3335 switch (cstate
->outermostlinktype
) {
3337 case DLT_PRISM_HEADER
:
3338 s
= gen_load_prism_llprefixlen(cstate
);
3341 case DLT_IEEE802_11_RADIO_AVS
:
3342 s
= gen_load_avs_llprefixlen(cstate
);
3345 case DLT_IEEE802_11_RADIO
:
3346 s
= gen_load_radiotap_llprefixlen(cstate
);
3350 s
= gen_load_ppi_llprefixlen(cstate
);
3359 * For link-layer types that have a variable-length link-layer
3360 * header, generate code to load the offset of the link-layer
3361 * payload into the register assigned to that offset, if any.
3363 switch (cstate
->outermostlinktype
) {
3365 case DLT_IEEE802_11
:
3366 case DLT_PRISM_HEADER
:
3367 case DLT_IEEE802_11_RADIO_AVS
:
3368 case DLT_IEEE802_11_RADIO
:
3370 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3374 s
= gen_load_pflog_llprefixlen(cstate
);
3379 * If there is no initialization yet and we need variable
3380 * length offsets for VLAN, initialize them to zero
3382 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3385 if (cstate
->off_linkpl
.reg
== -1)
3386 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3387 if (cstate
->off_linktype
.reg
== -1)
3388 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3390 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3392 s2
= new_stmt(cstate
, BPF_ST
);
3393 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3395 s2
= new_stmt(cstate
, BPF_ST
);
3396 s2
->s
.k
= cstate
->off_linktype
.reg
;
3401 * If we have any offset-loading code, append all the
3402 * existing statements in the block to those statements,
3403 * and make the resulting list the list of statements
3407 sappend(s
, b
->stmts
);
3413 * Take an absolute offset, and:
3415 * if it has no variable part, return NULL;
3417 * if it has a variable part, generate code to load the register
3418 * containing that variable part into the X register, returning
3419 * a pointer to that code - if no register for that offset has
3420 * been allocated, allocate it first.
3422 * (The code to set that register will be generated later, but will
3423 * be placed earlier in the code sequence.)
3425 static struct slist
*
3426 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3430 if (off
->is_variable
) {
3431 if (off
->reg
== -1) {
3433 * We haven't yet assigned a register for the
3434 * variable part of the offset of the link-layer
3435 * header; allocate one.
3437 off
->reg
= alloc_reg(cstate
);
3441 * Load the register containing the variable part of the
3442 * offset of the link-layer header into the X register.
3444 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3449 * That offset isn't variable, there's no variable part,
3450 * so we don't need to generate any code.
3457 * Map an Ethernet type to the equivalent PPP type.
3460 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3468 case ETHERTYPE_IPV6
:
3469 ll_proto
= PPP_IPV6
;
3473 ll_proto
= PPP_DECNET
;
3476 case ETHERTYPE_ATALK
:
3477 ll_proto
= PPP_APPLE
;
3490 * I'm assuming the "Bridging PDU"s that go
3491 * over PPP are Spanning Tree Protocol
3494 ll_proto
= PPP_BRPDU
;
3505 * Generate any tests that, for encapsulation of a link-layer packet
3506 * inside another protocol stack, need to be done to check for those
3507 * link-layer packets (and that haven't already been done by a check
3508 * for that encapsulation).
3510 static struct block
*
3511 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3513 if (cstate
->is_encap
)
3514 return gen_encap_ll_check(cstate
);
3516 switch (cstate
->prevlinktype
) {
3520 * This is LANE-encapsulated Ethernet; check that the LANE
3521 * packet doesn't begin with an LE Control marker, i.e.
3522 * that it's data, not a control message.
3524 * (We've already generated a test for LANE.)
3526 return gen_cmp_ne(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3530 * No such tests are necessary.
3538 * The three different values we should check for when checking for an
3539 * IPv6 packet with DLT_NULL.
3541 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3542 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3543 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3546 * Generate code to match a particular packet type by matching the
3547 * link-layer type field or fields in the 802.2 LLC header.
3549 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3550 * value, if <= ETHERMTU.
3552 static struct block
*
3553 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3555 struct block
*b0
, *b1
, *b2
;
3557 /* are we checking MPLS-encapsulated packets? */
3558 if (cstate
->label_stack_depth
> 0)
3559 return gen_mpls_linktype(cstate
, ll_proto
);
3561 switch (cstate
->linktype
) {
3564 case DLT_NETANALYZER
:
3565 case DLT_NETANALYZER_TRANSPARENT
:
3566 /* Geneve has an EtherType regardless of whether there is an
3567 * L2 header. VXLAN always has an EtherType. */
3568 if (!cstate
->is_encap
)
3569 b0
= gen_prevlinkhdr_check(cstate
);
3573 b1
= gen_ether_linktype(cstate
, ll_proto
);
3584 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3588 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3592 case DLT_IEEE802_11
:
3593 case DLT_PRISM_HEADER
:
3594 case DLT_IEEE802_11_RADIO_AVS
:
3595 case DLT_IEEE802_11_RADIO
:
3598 * Check that we have a data frame.
3600 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3601 IEEE80211_FC0_TYPE_DATA
,
3602 IEEE80211_FC0_TYPE_MASK
);
3605 * Now check for the specified link-layer type.
3607 b1
= gen_llc_linktype(cstate
, ll_proto
);
3614 * XXX - check for LLC frames.
3616 return gen_llc_linktype(cstate
, ll_proto
);
3621 * XXX - check for LLC PDUs, as per IEEE 802.5.
3623 return gen_llc_linktype(cstate
, ll_proto
);
3626 case DLT_ATM_RFC1483
:
3628 case DLT_IP_OVER_FC
:
3629 return gen_llc_linktype(cstate
, ll_proto
);
3634 * Check for an LLC-encapsulated version of this protocol;
3635 * if we were checking for LANE, linktype would no longer
3638 * Check for LLC encapsulation and then check the protocol.
3640 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3641 b1
= gen_llc_linktype(cstate
, ll_proto
);
3647 return gen_linux_sll_linktype(cstate
, ll_proto
);
3651 case DLT_SLIP_BSDOS
:
3654 * These types don't provide any type field; packets
3655 * are always IPv4 or IPv6.
3657 * XXX - for IPv4, check for a version number of 4, and,
3658 * for IPv6, check for a version number of 6?
3663 /* Check for a version number of 4. */
3664 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3666 case ETHERTYPE_IPV6
:
3667 /* Check for a version number of 6. */
3668 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3671 return gen_false(cstate
); /* always false */
3677 * Raw IPv4, so no type field.
3679 if (ll_proto
== ETHERTYPE_IP
)
3680 return gen_true(cstate
); /* always true */
3682 /* Checking for something other than IPv4; always false */
3683 return gen_false(cstate
);
3688 * Raw IPv6, so no type field.
3690 if (ll_proto
== ETHERTYPE_IPV6
)
3691 return gen_true(cstate
); /* always true */
3693 /* Checking for something other than IPv6; always false */
3694 return gen_false(cstate
);
3699 case DLT_PPP_SERIAL
:
3702 * We use Ethernet protocol types inside libpcap;
3703 * map them to the corresponding PPP protocol types.
3705 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3706 ethertype_to_ppptype(ll_proto
));
3711 * We use Ethernet protocol types inside libpcap;
3712 * map them to the corresponding PPP protocol types.
3718 * Also check for Van Jacobson-compressed IP.
3719 * XXX - do this for other forms of PPP?
3721 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3722 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3724 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3729 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3730 ethertype_to_ppptype(ll_proto
));
3740 return (gen_loopback_linktype(cstate
, AF_INET
));
3742 case ETHERTYPE_IPV6
:
3744 * AF_ values may, unfortunately, be platform-
3745 * dependent; AF_INET isn't, because everybody
3746 * used 4.2BSD's value, but AF_INET6 is, because
3747 * 4.2BSD didn't have a value for it (given that
3748 * IPv6 didn't exist back in the early 1980's),
3749 * and they all picked their own values.
3751 * This means that, if we're reading from a
3752 * savefile, we need to check for all the
3755 * If we're doing a live capture, we only need
3756 * to check for this platform's value; however,
3757 * Npcap uses 24, which isn't Windows's AF_INET6
3758 * value. (Given the multiple different values,
3759 * programs that read pcap files shouldn't be
3760 * checking for their platform's AF_INET6 value
3761 * anyway, they should check for all of the
3762 * possible values. and they might as well do
3763 * that even for live captures.)
3765 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3767 * Savefile - check for all three
3768 * possible IPv6 values.
3770 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3771 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3773 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3778 * Live capture, so we only need to
3779 * check for the value used on this
3784 * Npcap doesn't use Windows's AF_INET6,
3785 * as that collides with AF_IPX on
3786 * some BSDs (both have the value 23).
3787 * Instead, it uses 24.
3789 return (gen_loopback_linktype(cstate
, 24));
3792 return (gen_loopback_linktype(cstate
, AF_INET6
));
3793 #else /* AF_INET6 */
3795 * I guess this platform doesn't support
3796 * IPv6, so we just reject all packets.
3798 return gen_false(cstate
);
3799 #endif /* AF_INET6 */
3805 * Not a type on which we support filtering.
3806 * XXX - support those that have AF_ values
3807 * #defined on this platform, at least?
3809 return gen_false(cstate
);
3814 * af field is host byte order in contrast to the rest of
3817 if (ll_proto
== ETHERTYPE_IP
)
3818 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3820 else if (ll_proto
== ETHERTYPE_IPV6
)
3821 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3824 return gen_false(cstate
);
3828 case DLT_ARCNET_LINUX
:
3830 * XXX should we check for first fragment if the protocol
3836 return gen_false(cstate
);
3838 case ETHERTYPE_IPV6
:
3839 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3843 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3845 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3851 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3853 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3858 case ETHERTYPE_REVARP
:
3859 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3862 case ETHERTYPE_ATALK
:
3863 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3870 case ETHERTYPE_ATALK
:
3871 return gen_true(cstate
);
3873 return gen_false(cstate
);
3879 * XXX - assumes a 2-byte Frame Relay header with
3880 * DLCI and flags. What if the address is longer?
3886 * Check for the special NLPID for IP.
3888 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3890 case ETHERTYPE_IPV6
:
3892 * Check for the special NLPID for IPv6.
3894 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3898 * Check for several OSI protocols.
3900 * Frame Relay packets typically have an OSI
3901 * NLPID at the beginning; we check for each
3904 * What we check for is the NLPID and a frame
3905 * control field of UI, i.e. 0x03 followed
3908 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3909 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3910 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3916 return gen_false(cstate
);
3921 break; // not implemented
3923 case DLT_JUNIPER_MFR
:
3924 case DLT_JUNIPER_MLFR
:
3925 case DLT_JUNIPER_MLPPP
:
3926 case DLT_JUNIPER_ATM1
:
3927 case DLT_JUNIPER_ATM2
:
3928 case DLT_JUNIPER_PPPOE
:
3929 case DLT_JUNIPER_PPPOE_ATM
:
3930 case DLT_JUNIPER_GGSN
:
3931 case DLT_JUNIPER_ES
:
3932 case DLT_JUNIPER_MONITOR
:
3933 case DLT_JUNIPER_SERVICES
:
3934 case DLT_JUNIPER_ETHER
:
3935 case DLT_JUNIPER_PPP
:
3936 case DLT_JUNIPER_FRELAY
:
3937 case DLT_JUNIPER_CHDLC
:
3938 case DLT_JUNIPER_VP
:
3939 case DLT_JUNIPER_ST
:
3940 case DLT_JUNIPER_ISM
:
3941 case DLT_JUNIPER_VS
:
3942 case DLT_JUNIPER_SRX_E2E
:
3943 case DLT_JUNIPER_FIBRECHANNEL
:
3944 case DLT_JUNIPER_ATM_CEMIC
:
3946 /* just lets verify the magic number for now -
3947 * on ATM we may have up to 6 different encapsulations on the wire
3948 * and need a lot of heuristics to figure out that the payload
3951 * FIXME encapsulation specific BPF_ filters
3953 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3955 case DLT_BACNET_MS_TP
:
3956 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3959 return gen_ipnet_linktype(cstate
, ll_proto
);
3961 case DLT_LINUX_IRDA
:
3964 case DLT_MTP2_WITH_PHDR
:
3967 case DLT_LINUX_LAPD
:
3968 case DLT_USB_FREEBSD
:
3970 case DLT_USB_LINUX_MMAPPED
:
3972 case DLT_BLUETOOTH_HCI_H4
:
3973 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3975 case DLT_CAN_SOCKETCAN
:
3976 case DLT_IEEE802_15_4
:
3977 case DLT_IEEE802_15_4_LINUX
:
3978 case DLT_IEEE802_15_4_NONASK_PHY
:
3979 case DLT_IEEE802_15_4_NOFCS
:
3980 case DLT_IEEE802_15_4_TAP
:
3981 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3984 case DLT_IPMB_KONTRON
:
3988 /* Using the fixed-size NFLOG header it is possible to tell only
3989 * the address family of the packet, other meaningful data is
3990 * either missing or behind TLVs.
3992 break; // not implemented
3996 * Does this link-layer header type have a field
3997 * indicating the type of the next protocol? If
3998 * so, off_linktype.constant_part will be the offset of that
3999 * field in the packet; if not, it will be OFFSET_NOT_SET.
4001 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
4003 * Yes; assume it's an Ethernet type. (If
4004 * it's not, it needs to be handled specially
4007 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
4011 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
4012 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
4016 * Check for an LLC SNAP packet with a given organization code and
4017 * protocol type; we check the entire contents of the 802.2 LLC and
4018 * snap headers, checking for DSAP and SSAP of SNAP and a control
4019 * field of 0x03 in the LLC header, and for the specified organization
4020 * code and protocol type in the SNAP header.
4022 static struct block
*
4023 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
4025 u_char snapblock
[8];
4027 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
4028 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
4029 snapblock
[2] = 0x03; /* control = UI */
4030 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
4031 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
4032 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
4033 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
4034 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
4035 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
4039 * Generate code to match frames with an LLC header.
4041 static struct block
*
4042 gen_llc_internal(compiler_state_t
*cstate
)
4044 struct block
*b0
, *b1
;
4046 switch (cstate
->linktype
) {
4050 * We check for an Ethernet type field less or equal than
4051 * 1500, which means it's an 802.3 length field.
4053 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
4056 * Now check for the purported DSAP and SSAP not being
4057 * 0xFF, to rule out NetWare-over-802.3.
4059 b1
= gen_cmp_ne(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
4065 * We check for LLC traffic.
4067 b0
= gen_atmtype_llc(cstate
);
4070 case DLT_IEEE802
: /* Token Ring */
4072 * XXX - check for LLC frames.
4074 return gen_true(cstate
);
4078 * XXX - check for LLC frames.
4080 return gen_true(cstate
);
4082 case DLT_ATM_RFC1483
:
4084 * For LLC encapsulation, these are defined to have an
4087 * For VC encapsulation, they don't, but there's no
4088 * way to check for that; the protocol used on the VC
4089 * is negotiated out of band.
4091 return gen_true(cstate
);
4093 case DLT_IEEE802_11
:
4094 case DLT_PRISM_HEADER
:
4095 case DLT_IEEE802_11_RADIO
:
4096 case DLT_IEEE802_11_RADIO_AVS
:
4099 * Check that we have a data frame.
4101 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4102 IEEE80211_FC0_TYPE_DATA
,
4103 IEEE80211_FC0_TYPE_MASK
);
4106 fail_kw_on_dlt(cstate
, "llc");
4112 gen_llc(compiler_state_t
*cstate
)
4115 * Catch errors reported by us and routines below us, and return NULL
4118 if (setjmp(cstate
->top_ctx
))
4121 return gen_llc_internal(cstate
);
4125 gen_llc_i(compiler_state_t
*cstate
)
4127 struct block
*b0
, *b1
;
4131 * Catch errors reported by us and routines below us, and return NULL
4134 if (setjmp(cstate
->top_ctx
))
4138 * Check whether this is an LLC frame.
4140 b0
= gen_llc_internal(cstate
);
4143 * Load the control byte and test the low-order bit; it must
4144 * be clear for I frames.
4146 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4147 b1
= new_block(cstate
, JMP(BPF_JSET
));
4156 gen_llc_s(compiler_state_t
*cstate
)
4158 struct block
*b0
, *b1
;
4161 * Catch errors reported by us and routines below us, and return NULL
4164 if (setjmp(cstate
->top_ctx
))
4168 * Check whether this is an LLC frame.
4170 b0
= gen_llc_internal(cstate
);
4173 * Now compare the low-order 2 bit of the control byte against
4174 * the appropriate value for S frames.
4176 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4182 gen_llc_u(compiler_state_t
*cstate
)
4184 struct block
*b0
, *b1
;
4187 * Catch errors reported by us and routines below us, and return NULL
4190 if (setjmp(cstate
->top_ctx
))
4194 * Check whether this is an LLC frame.
4196 b0
= gen_llc_internal(cstate
);
4199 * Now compare the low-order 2 bit of the control byte against
4200 * the appropriate value for U frames.
4202 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4208 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4210 struct block
*b0
, *b1
;
4213 * Catch errors reported by us and routines below us, and return NULL
4216 if (setjmp(cstate
->top_ctx
))
4220 * Check whether this is an LLC frame.
4222 b0
= gen_llc_internal(cstate
);
4225 * Now check for an S frame with the appropriate type.
4227 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4233 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4235 struct block
*b0
, *b1
;
4238 * Catch errors reported by us and routines below us, and return NULL
4241 if (setjmp(cstate
->top_ctx
))
4245 * Check whether this is an LLC frame.
4247 b0
= gen_llc_internal(cstate
);
4250 * Now check for a U frame with the appropriate type.
4252 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4258 * Generate code to match a particular packet type, for link-layer types
4259 * using 802.2 LLC headers.
4261 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4262 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4264 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4265 * value, if <= ETHERMTU. We use that to determine whether to
4266 * match the DSAP or both DSAP and LSAP or to check the OUI and
4267 * protocol ID in a SNAP header.
4269 static struct block
*
4270 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4273 * XXX - handle token-ring variable-length header.
4279 case LLCSAP_NETBEUI
:
4281 * XXX - should we check both the DSAP and the
4282 * SSAP, like this, or should we check just the
4283 * DSAP, as we do for other SAP values?
4285 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4286 ((ll_proto
<< 8) | ll_proto
));
4290 * XXX - are there ever SNAP frames for IPX on
4291 * non-Ethernet 802.x networks?
4293 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4295 case ETHERTYPE_ATALK
:
4297 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4298 * SNAP packets with an organization code of
4299 * 0x080007 (Apple, for Appletalk) and a protocol
4300 * type of ETHERTYPE_ATALK (Appletalk).
4302 * XXX - check for an organization code of
4303 * encapsulated Ethernet as well?
4305 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4309 * XXX - we don't have to check for IPX 802.3
4310 * here, but should we check for the IPX Ethertype?
4312 if (ll_proto
<= ETHERMTU
) {
4314 * This is an LLC SAP value, so check
4317 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4320 * This is an Ethernet type; we assume that it's
4321 * unlikely that it'll appear in the right place
4322 * at random, and therefore check only the
4323 * location that would hold the Ethernet type
4324 * in a SNAP frame with an organization code of
4325 * 0x000000 (encapsulated Ethernet).
4327 * XXX - if we were to check for the SNAP DSAP and
4328 * LSAP, as per XXX, and were also to check for an
4329 * organization code of 0x000000 (encapsulated
4330 * Ethernet), we'd do
4332 * return gen_snap(cstate, 0x000000, ll_proto);
4334 * here; for now, we don't, as per the above.
4335 * I don't know whether it's worth the extra CPU
4336 * time to do the right check or not.
4338 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4343 static struct block
*
4344 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4345 int dir
, u_int src_off
, u_int dst_off
)
4347 struct block
*b0
, *b1
;
4361 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4362 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4368 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4369 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4379 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4386 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4390 static struct block
*
4391 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4392 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4394 struct block
*b0
, *b1
;
4397 * Code below needs to access four separate 32-bit parts of the 128-bit
4398 * IPv6 address and mask. In some OSes this is as simple as using the
4399 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4400 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4401 * far as libpcap sees it. Hence copy the data before use to avoid
4402 * potential unaligned memory access and the associated compiler
4403 * warnings (whether genuine or not).
4405 bpf_u_int32 a
[4], m
[4];
4418 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4419 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4425 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4426 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4436 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4443 /* this order is important */
4444 memcpy(a
, addr
, sizeof(a
));
4445 memcpy(m
, mask
, sizeof(m
));
4446 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4447 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4449 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4451 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4457 static struct block
*
4458 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4460 register struct block
*b0
, *b1
;
4464 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4467 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4470 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4471 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4477 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4478 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4488 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4496 * Like gen_ehostop, but for DLT_FDDI
4498 static struct block
*
4499 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4501 struct block
*b0
, *b1
;
4505 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4508 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4511 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4512 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4518 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4519 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4529 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4537 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4539 static struct block
*
4540 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4542 register struct block
*b0
, *b1
;
4546 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4549 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4552 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4553 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4559 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4560 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4570 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4578 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4579 * various 802.11 + radio headers.
4581 static struct block
*
4582 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4584 register struct block
*b0
, *b1
, *b2
;
4585 register struct slist
*s
;
4587 #ifdef ENABLE_WLAN_FILTERING_PATCH
4590 * We need to disable the optimizer because the optimizer is buggy
4591 * and wipes out some LD instructions generated by the below
4592 * code to validate the Frame Control bits
4594 cstate
->no_optimize
= 1;
4595 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4602 * For control frames, there is no SA.
4604 * For management frames, SA is at an
4605 * offset of 10 from the beginning of
4608 * For data frames, SA is at an offset
4609 * of 10 from the beginning of the packet
4610 * if From DS is clear, at an offset of
4611 * 16 from the beginning of the packet
4612 * if From DS is set and To DS is clear,
4613 * and an offset of 24 from the beginning
4614 * of the packet if From DS is set and To DS
4619 * Generate the tests to be done for data frames
4622 * First, check for To DS set, i.e. check "link[1] & 0x01".
4624 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4625 b1
= new_block(cstate
, JMP(BPF_JSET
));
4626 b1
->s
.k
= 0x01; /* To DS */
4630 * If To DS is set, the SA is at 24.
4632 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4636 * Now, check for To DS not set, i.e. check
4637 * "!(link[1] & 0x01)".
4639 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4640 b2
= new_block(cstate
, JMP(BPF_JSET
));
4641 b2
->s
.k
= 0x01; /* To DS */
4646 * If To DS is not set, the SA is at 16.
4648 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4652 * Now OR together the last two checks. That gives
4653 * the complete set of checks for data frames with
4659 * Now check for From DS being set, and AND that with
4660 * the ORed-together checks.
4662 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4663 b1
= new_block(cstate
, JMP(BPF_JSET
));
4664 b1
->s
.k
= 0x02; /* From DS */
4669 * Now check for data frames with From DS not set.
4671 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4672 b2
= new_block(cstate
, JMP(BPF_JSET
));
4673 b2
->s
.k
= 0x02; /* From DS */
4678 * If From DS isn't set, the SA is at 10.
4680 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4684 * Now OR together the checks for data frames with
4685 * From DS not set and for data frames with From DS
4686 * set; that gives the checks done for data frames.
4691 * Now check for a data frame.
4692 * I.e, check "link[0] & 0x08".
4694 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4695 b1
= new_block(cstate
, JMP(BPF_JSET
));
4700 * AND that with the checks done for data frames.
4705 * If the high-order bit of the type value is 0, this
4706 * is a management frame.
4707 * I.e, check "!(link[0] & 0x08)".
4709 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4710 b2
= new_block(cstate
, JMP(BPF_JSET
));
4716 * For management frames, the SA is at 10.
4718 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4722 * OR that with the checks done for data frames.
4723 * That gives the checks done for management and
4729 * If the low-order bit of the type value is 1,
4730 * this is either a control frame or a frame
4731 * with a reserved type, and thus not a
4734 * I.e., check "!(link[0] & 0x04)".
4736 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4737 b1
= new_block(cstate
, JMP(BPF_JSET
));
4743 * AND that with the checks for data and management
4753 * For control frames, there is no DA.
4755 * For management frames, DA is at an
4756 * offset of 4 from the beginning of
4759 * For data frames, DA is at an offset
4760 * of 4 from the beginning of the packet
4761 * if To DS is clear and at an offset of
4762 * 16 from the beginning of the packet
4767 * Generate the tests to be done for data frames.
4769 * First, check for To DS set, i.e. "link[1] & 0x01".
4771 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4772 b1
= new_block(cstate
, JMP(BPF_JSET
));
4773 b1
->s
.k
= 0x01; /* To DS */
4777 * If To DS is set, the DA is at 16.
4779 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4783 * Now, check for To DS not set, i.e. check
4784 * "!(link[1] & 0x01)".
4786 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4787 b2
= new_block(cstate
, JMP(BPF_JSET
));
4788 b2
->s
.k
= 0x01; /* To DS */
4793 * If To DS is not set, the DA is at 4.
4795 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4799 * Now OR together the last two checks. That gives
4800 * the complete set of checks for data frames.
4805 * Now check for a data frame.
4806 * I.e, check "link[0] & 0x08".
4808 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4809 b1
= new_block(cstate
, JMP(BPF_JSET
));
4814 * AND that with the checks done for data frames.
4819 * If the high-order bit of the type value is 0, this
4820 * is a management frame.
4821 * I.e, check "!(link[0] & 0x08)".
4823 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4824 b2
= new_block(cstate
, JMP(BPF_JSET
));
4830 * For management frames, the DA is at 4.
4832 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4836 * OR that with the checks done for data frames.
4837 * That gives the checks done for management and
4843 * If the low-order bit of the type value is 1,
4844 * this is either a control frame or a frame
4845 * with a reserved type, and thus not a
4848 * I.e., check "!(link[0] & 0x04)".
4850 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4851 b1
= new_block(cstate
, JMP(BPF_JSET
));
4857 * AND that with the checks for data and management
4864 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4865 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4871 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4872 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4877 * XXX - add BSSID keyword?
4880 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4884 * Not present in CTS or ACK control frames.
4886 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4887 IEEE80211_FC0_TYPE_MASK
);
4888 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4889 IEEE80211_FC0_SUBTYPE_MASK
);
4890 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4891 IEEE80211_FC0_SUBTYPE_MASK
);
4894 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4900 * Not present in control frames.
4902 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4903 IEEE80211_FC0_TYPE_MASK
);
4904 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4910 * Present only if the direction mask has both "From DS"
4911 * and "To DS" set. Neither control frames nor management
4912 * frames should have both of those set, so we don't
4913 * check the frame type.
4915 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4916 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4917 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4923 * Not present in management frames; addr1 in other
4928 * If the high-order bit of the type value is 0, this
4929 * is a management frame.
4930 * I.e, check "(link[0] & 0x08)".
4932 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4933 b1
= new_block(cstate
, JMP(BPF_JSET
));
4940 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4943 * AND that with the check of addr1.
4950 * Not present in management frames; addr2, if present,
4955 * Not present in CTS or ACK control frames.
4957 b0
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4958 IEEE80211_FC0_TYPE_MASK
);
4959 b1
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4960 IEEE80211_FC0_SUBTYPE_MASK
);
4961 b2
= gen_mcmp_ne(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4962 IEEE80211_FC0_SUBTYPE_MASK
);
4967 * If the high-order bit of the type value is 0, this
4968 * is a management frame.
4969 * I.e, check "(link[0] & 0x08)".
4971 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4972 b1
= new_block(cstate
, JMP(BPF_JSET
));
4977 * AND that with the check for frames other than
4978 * CTS and ACK frames.
4985 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4994 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4995 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4996 * as the RFC states.)
4998 static struct block
*
4999 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
5001 register struct block
*b0
, *b1
;
5005 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
5008 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
5011 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
5012 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
5018 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
5019 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
5029 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5037 * This is quite tricky because there may be pad bytes in front of the
5038 * DECNET header, and then there are two possible data packet formats that
5039 * carry both src and dst addresses, plus 5 packet types in a format that
5040 * carries only the src node, plus 2 types that use a different format and
5041 * also carry just the src node.
5045 * Instead of doing those all right, we just look for data packets with
5046 * 0 or 1 bytes of padding. If you want to look at other packets, that
5047 * will require a lot more hacking.
5049 * To add support for filtering on DECNET "areas" (network numbers)
5050 * one would want to add a "mask" argument to this routine. That would
5051 * make the filter even more inefficient, although one could be clever
5052 * and not generate masking instructions if the mask is 0xFFFF.
5054 static struct block
*
5055 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
5057 struct block
*b0
, *b1
, *b2
, *tmp
;
5058 u_int offset_lh
; /* offset if long header is received */
5059 u_int offset_sh
; /* offset if short header is received */
5064 offset_sh
= 1; /* follows flags */
5065 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5069 offset_sh
= 3; /* follows flags, dstnode */
5070 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5074 /* Inefficient because we do our Calvinball dance twice */
5075 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5076 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5082 /* Inefficient because we do our Calvinball dance twice */
5083 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5084 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5094 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5102 * In a DECnet message inside an Ethernet frame the first two bytes
5103 * immediately after EtherType are the [litle-endian] DECnet message
5104 * length, which is irrelevant in this context.
5106 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5107 * 8-bit bitmap of the optional padding before the packet route header.
5108 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5109 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5110 * means there aren't any PAD bytes after the bitmap, so the header
5111 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5112 * is set to 0, thus the header begins at the third byte.
5114 * The header can be in several (as mentioned above) formats, all of
5115 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5116 * (PF, "pad field") set to 0 regardless of any padding present before
5117 * the header. "Short header" means bits 0-2 of the bitmap encode the
5118 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5120 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5121 * values and the masks, this maps to the required single bytes of
5122 * the message correctly on both big-endian and little-endian hosts.
5123 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5124 * because the wire encoding is little-endian and BPF multiple-byte
5125 * loads are big-endian. When the destination address is near enough
5126 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5129 /* Check for pad = 1, long header case */
5130 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5131 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5132 BPF_H
, SWAPSHORT(addr
));
5134 /* Check for pad = 0, long header case */
5135 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5136 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5140 /* Check for pad = 1, short header case */
5142 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5143 0x81020000U
| SWAPSHORT(addr
),
5146 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5147 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5152 /* Check for pad = 0, short header case */
5154 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5155 0x02000000U
| SWAPSHORT(addr
) << 8,
5158 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5159 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5169 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5170 * test the bottom-of-stack bit, and then check the version number
5171 * field in the IP header.
5173 static struct block
*
5174 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5176 struct block
*b0
, *b1
;
5181 /* match the bottom-of-stack bit */
5182 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5183 /* match the IPv4 version number */
5184 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5188 case ETHERTYPE_IPV6
:
5189 /* match the bottom-of-stack bit */
5190 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5191 /* match the IPv4 version number */
5192 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5197 /* FIXME add other L3 proto IDs */
5198 bpf_error(cstate
, "unsupported protocol over mpls");
5203 static struct block
*
5204 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5205 int proto
, int dir
, int type
)
5207 struct block
*b0
, *b1
;
5212 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5214 * Only check for non-IPv4 addresses if we're not
5215 * checking MPLS-encapsulated packets.
5217 if (cstate
->label_stack_depth
== 0) {
5218 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5220 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5226 // "link net NETNAME" and variations thereof
5227 break; // invalid qualifier
5230 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5231 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5236 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5237 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5242 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5243 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5254 break; // invalid qualifier
5257 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5258 b1
= gen_dnhostop(cstate
, addr
, dir
);
5289 break; // invalid qualifier
5294 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5295 type
== Q_NET
? "ip net" : "ip host");
5300 static struct block
*
5301 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5302 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5304 struct block
*b0
, *b1
;
5310 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5311 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5353 break; // invalid qualifier
5358 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5359 type
== Q_NET
? "ip6 net" : "ip6 host");
5366 * This primitive is non-directional by design, so the grammar does not allow
5367 * to qualify it with a direction.
5369 static struct block
*
5370 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5371 struct addrinfo
*alist
, int proto
)
5373 struct block
*b0
, *b1
, *tmp
;
5374 struct addrinfo
*ai
;
5375 struct sockaddr_in
*sin
;
5382 switch (cstate
->linktype
) {
5384 case DLT_NETANALYZER
:
5385 case DLT_NETANALYZER_TRANSPARENT
:
5386 b1
= gen_prevlinkhdr_check(cstate
);
5387 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5392 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5395 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5397 case DLT_IEEE802_11
:
5398 case DLT_PRISM_HEADER
:
5399 case DLT_IEEE802_11_RADIO_AVS
:
5400 case DLT_IEEE802_11_RADIO
:
5402 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5404 case DLT_IP_OVER_FC
:
5405 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5409 * This is LLC-multiplexed traffic; if it were
5410 * LANE, cstate->linktype would have been set to
5416 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5419 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5421 * Does it have an address?
5423 if (ai
->ai_addr
!= NULL
) {
5425 * Yes. Is it an IPv4 address?
5427 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5429 * Generate an entry for it.
5431 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5432 tmp
= gen_host(cstate
,
5433 ntohl(sin
->sin_addr
.s_addr
),
5434 0xffffffff, proto
, Q_OR
, Q_HOST
);
5436 * Is it the *first* IPv4 address?
5440 * Yes, so start with it.
5445 * No, so OR it into the
5457 * No IPv4 addresses found.
5465 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5470 static struct block
*
5471 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5474 struct block
*b1
= NULL
;
5479 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
);
5483 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
);
5487 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
);
5491 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
);
5494 #ifndef IPPROTO_IGMP
5495 #define IPPROTO_IGMP 2
5499 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
);
5502 #ifndef IPPROTO_IGRP
5503 #define IPPROTO_IGRP 9
5506 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
);
5510 #define IPPROTO_PIM 103
5514 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
);
5517 #ifndef IPPROTO_VRRP
5518 #define IPPROTO_VRRP 112
5522 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
);
5525 #ifndef IPPROTO_CARP
5526 #define IPPROTO_CARP 112
5530 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
);
5534 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5538 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5542 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5546 break; // invalid syntax
5549 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5553 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5557 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5561 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5565 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5569 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5573 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5577 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5580 #ifndef IPPROTO_ICMPV6
5581 #define IPPROTO_ICMPV6 58
5584 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
);
5588 #define IPPROTO_AH 51
5591 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
);
5595 #define IPPROTO_ESP 50
5598 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
);
5602 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5606 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
);
5610 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
5613 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5614 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5615 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5617 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5619 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5621 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5625 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5626 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5627 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
); /* FIXME extract the circuit-type bits */
5629 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5631 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5633 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5637 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5638 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
);
5639 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
);
5641 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
);
5646 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
);
5647 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
);
5652 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5653 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5655 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5657 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5662 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
);
5663 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
);
5668 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
);
5669 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
);
5674 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
);
5678 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5682 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5686 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5690 break; // invalid syntax
5697 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5701 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5704 * Catch errors reported by us and routines below us, and return NULL
5707 if (setjmp(cstate
->top_ctx
))
5710 return gen_proto_abbrev_internal(cstate
, proto
);
5713 static struct block
*
5714 gen_ipfrag(compiler_state_t
*cstate
)
5719 /* not IPv4 frag other than the first frag */
5720 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5721 b
= new_block(cstate
, JMP(BPF_JSET
));
5730 * Generate a comparison to a port value in the transport-layer header
5731 * at the specified offset from the beginning of that header.
5733 * XXX - this handles a variable-length prefix preceding the link-layer
5734 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5735 * variable-length link-layer headers (such as Token Ring or 802.11
5738 static struct block
*
5739 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5741 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5744 static struct block
*
5745 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5747 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5750 static struct block
*
5751 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5753 struct block
*b0
, *b1
, *tmp
;
5755 /* ip proto 'proto' and not a fragment other than the first fragment */
5756 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5757 b0
= gen_ipfrag(cstate
);
5762 b1
= gen_portatom(cstate
, 0, port
);
5766 b1
= gen_portatom(cstate
, 2, port
);
5770 tmp
= gen_portatom(cstate
, 0, port
);
5771 b1
= gen_portatom(cstate
, 2, port
);
5777 tmp
= gen_portatom(cstate
, 0, port
);
5778 b1
= gen_portatom(cstate
, 2, port
);
5788 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5800 static struct block
*
5801 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5803 struct block
*b0
, *b1
, *tmp
;
5808 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5809 * not LLC encapsulation with LLCSAP_IP.
5811 * For IEEE 802 networks - which includes 802.5 token ring
5812 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5813 * says that SNAP encapsulation is used, not LLC encapsulation
5816 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5817 * RFC 2225 say that SNAP encapsulation is used, not LLC
5818 * encapsulation with LLCSAP_IP.
5820 * So we always check for ETHERTYPE_IP.
5822 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5828 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5832 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5833 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5835 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5847 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5849 struct block
*b0
, *b1
, *tmp
;
5851 /* ip6 proto 'proto' */
5852 /* XXX - catch the first fragment of a fragmented packet? */
5853 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5857 b1
= gen_portatom6(cstate
, 0, port
);
5861 b1
= gen_portatom6(cstate
, 2, port
);
5865 tmp
= gen_portatom6(cstate
, 0, port
);
5866 b1
= gen_portatom6(cstate
, 2, port
);
5872 tmp
= gen_portatom6(cstate
, 0, port
);
5873 b1
= gen_portatom6(cstate
, 2, port
);
5885 static struct block
*
5886 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5888 struct block
*b0
, *b1
, *tmp
;
5890 /* link proto ip6 */
5891 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5897 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5901 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5902 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5904 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5915 /* gen_portrange code */
5916 static struct block
*
5917 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5921 return gen_portatom(cstate
, off
, v1
);
5923 struct block
*b1
, *b2
;
5925 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, min(v1
, v2
));
5926 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, max(v1
, v2
));
5933 static struct block
*
5934 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5935 bpf_u_int32 proto
, int dir
)
5937 struct block
*b0
, *b1
, *tmp
;
5939 /* ip proto 'proto' and not a fragment other than the first fragment */
5940 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5941 b0
= gen_ipfrag(cstate
);
5946 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5950 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5954 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5955 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5961 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5962 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5972 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5984 static struct block
*
5985 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5988 struct block
*b0
, *b1
, *tmp
;
5991 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5997 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6002 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6003 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6005 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6016 static struct block
*
6017 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6021 return gen_portatom6(cstate
, off
, v1
);
6023 struct block
*b1
, *b2
;
6025 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, min(v1
, v2
));
6026 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, max(v1
, v2
));
6033 static struct block
*
6034 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6035 bpf_u_int32 proto
, int dir
)
6037 struct block
*b0
, *b1
, *tmp
;
6039 /* ip6 proto 'proto' */
6040 /* XXX - catch the first fragment of a fragmented packet? */
6041 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6045 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6049 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6053 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6054 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6060 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6061 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6073 static struct block
*
6074 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6077 struct block
*b0
, *b1
, *tmp
;
6079 /* link proto ip6 */
6080 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6086 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6091 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6092 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6094 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6106 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6115 v
= pcap_nametoproto(name
);
6116 if (v
== PROTO_UNDEF
)
6117 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6121 /* XXX should look up h/w protocol type based on cstate->linktype */
6122 v
= pcap_nametoeproto(name
);
6123 if (v
== PROTO_UNDEF
) {
6124 v
= pcap_nametollc(name
);
6125 if (v
== PROTO_UNDEF
)
6126 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6131 if (strcmp(name
, "esis") == 0)
6133 else if (strcmp(name
, "isis") == 0)
6135 else if (strcmp(name
, "clnp") == 0)
6138 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6148 #if !defined(NO_PROTOCHAIN)
6150 * This primitive is non-directional by design, so the grammar does not allow
6151 * to qualify it with a direction.
6153 static struct block
*
6154 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6156 struct block
*b0
, *b
;
6157 struct slist
*s
[100];
6158 int fix2
, fix3
, fix4
, fix5
;
6159 int ahcheck
, again
, end
;
6161 int reg2
= alloc_reg(cstate
);
6163 memset(s
, 0, sizeof(s
));
6164 fix3
= fix4
= fix5
= 0;
6171 b0
= gen_protochain(cstate
, v
, Q_IP
);
6172 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6176 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
6181 * We don't handle variable-length prefixes before the link-layer
6182 * header, or variable-length link-layer headers, here yet.
6183 * We might want to add BPF instructions to do the protochain
6184 * work, to simplify that and, on platforms that have a BPF
6185 * interpreter with the new instructions, let the filtering
6186 * be done in the kernel. (We already require a modified BPF
6187 * engine to do the protochain stuff, to support backward
6188 * branches, and backward branch support is unlikely to appear
6189 * in kernel BPF engines.)
6191 if (cstate
->off_linkpl
.is_variable
)
6192 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6195 * To quote a comment in optimize.c:
6197 * "These data structures are used in a Cocke and Schwartz style
6198 * value numbering scheme. Since the flowgraph is acyclic,
6199 * exit values can be propagated from a node's predecessors
6200 * provided it is uniquely defined."
6202 * "Acyclic" means "no backward branches", which means "no
6203 * loops", so we have to turn the optimizer off.
6205 cstate
->no_optimize
= 1;
6208 * s[0] is a dummy entry to protect other BPF insn from damage
6209 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6210 * hard to find interdependency made by jump table fixup.
6213 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6218 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6221 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6222 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6224 /* X = ip->ip_hl << 2 */
6225 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6226 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6231 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6233 /* A = ip6->ip_nxt */
6234 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6235 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6237 /* X = sizeof(struct ip6_hdr) */
6238 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6244 bpf_error(cstate
, "unsupported proto to gen_protochain");
6248 /* again: if (A == v) goto end; else fall through; */
6250 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6252 s
[i
]->s
.jt
= NULL
; /*later*/
6253 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6257 #ifndef IPPROTO_NONE
6258 #define IPPROTO_NONE 59
6260 /* if (A == IPPROTO_NONE) goto end */
6261 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6262 s
[i
]->s
.jt
= NULL
; /*later*/
6263 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6264 s
[i
]->s
.k
= IPPROTO_NONE
;
6265 s
[fix5
]->s
.jf
= s
[i
];
6269 if (proto
== Q_IPV6
) {
6270 int v6start
, v6end
, v6advance
, j
;
6273 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6274 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6275 s
[i
]->s
.jt
= NULL
; /*later*/
6276 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6277 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6278 s
[fix2
]->s
.jf
= s
[i
];
6280 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6281 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6282 s
[i
]->s
.jt
= NULL
; /*later*/
6283 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6284 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6286 /* if (A == IPPROTO_ROUTING) goto v6advance */
6287 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6288 s
[i
]->s
.jt
= NULL
; /*later*/
6289 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6290 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6292 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6293 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6294 s
[i
]->s
.jt
= NULL
; /*later*/
6295 s
[i
]->s
.jf
= NULL
; /*later*/
6296 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6306 * A = P[X + packet head];
6307 * X = X + (P[X + packet head + 1] + 1) * 8;
6309 /* A = P[X + packet head] */
6310 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6311 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6314 s
[i
] = new_stmt(cstate
, BPF_ST
);
6317 /* A = P[X + packet head + 1]; */
6318 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6319 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6322 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6326 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6330 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6334 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6337 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6341 /* goto again; (must use BPF_JA for backward jump) */
6342 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6343 s
[i
]->s
.k
= again
- i
- 1;
6344 s
[i
- 1]->s
.jf
= s
[i
];
6348 for (j
= v6start
; j
<= v6end
; j
++)
6349 s
[j
]->s
.jt
= s
[v6advance
];
6352 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6354 s
[fix2
]->s
.jf
= s
[i
];
6360 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6361 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6362 s
[i
]->s
.jt
= NULL
; /*later*/
6363 s
[i
]->s
.jf
= NULL
; /*later*/
6364 s
[i
]->s
.k
= IPPROTO_AH
;
6366 s
[fix3
]->s
.jf
= s
[ahcheck
];
6373 * X = X + (P[X + 1] + 2) * 4;
6376 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6378 /* A = P[X + packet head]; */
6379 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6380 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6383 s
[i
] = new_stmt(cstate
, BPF_ST
);
6387 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6390 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6394 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6396 /* A = P[X + packet head] */
6397 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6398 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6401 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6405 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6409 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6412 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6416 /* goto again; (must use BPF_JA for backward jump) */
6417 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6418 s
[i
]->s
.k
= again
- i
- 1;
6423 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6425 s
[fix2
]->s
.jt
= s
[end
];
6426 s
[fix4
]->s
.jf
= s
[end
];
6427 s
[fix5
]->s
.jt
= s
[end
];
6434 for (i
= 0; i
< max
- 1; i
++)
6435 s
[i
]->next
= s
[i
+ 1];
6436 s
[max
- 1]->next
= NULL
;
6441 b
= new_block(cstate
, JMP(BPF_JEQ
));
6442 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6445 free_reg(cstate
, reg2
);
6450 #endif /* !defined(NO_PROTOCHAIN) */
6453 * Generate code that checks whether the packet is a packet for protocol
6454 * <proto> and whether the type field in that protocol's header has
6455 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6456 * IP packet and checks the protocol number in the IP header against <v>.
6458 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6459 * against Q_IP and Q_IPV6.
6461 * This primitive is non-directional by design, so the grammar does not allow
6462 * to qualify it with a direction.
6464 static struct block
*
6465 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6467 struct block
*b0
, *b1
;
6472 b0
= gen_proto(cstate
, v
, Q_IP
);
6473 b1
= gen_proto(cstate
, v
, Q_IPV6
);
6478 return gen_linktype(cstate
, v
);
6482 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6483 * not LLC encapsulation with LLCSAP_IP.
6485 * For IEEE 802 networks - which includes 802.5 token ring
6486 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6487 * says that SNAP encapsulation is used, not LLC encapsulation
6490 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6491 * RFC 2225 say that SNAP encapsulation is used, not LLC
6492 * encapsulation with LLCSAP_IP.
6494 * So we always check for ETHERTYPE_IP.
6496 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6497 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6515 break; // invalid qualifier
6518 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6520 * Also check for a fragment header before the final
6523 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6524 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6526 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6537 break; // invalid qualifier
6540 assert_maxval(cstate
, "ISO protocol", v
, UINT8_MAX
);
6541 switch (cstate
->linktype
) {
6545 * Frame Relay packets typically have an OSI
6546 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6547 * generates code to check for all the OSI
6548 * NLPIDs, so calling it and then adding a check
6549 * for the particular NLPID for which we're
6550 * looking is bogus, as we can just check for
6553 * What we check for is the NLPID and a frame
6554 * control field value of UI, i.e. 0x03 followed
6557 * XXX - assumes a 2-byte Frame Relay header with
6558 * DLCI and flags. What if the address is longer?
6560 * XXX - what about SNAP-encapsulated frames?
6562 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6568 * Cisco uses an Ethertype lookalike - for OSI,
6571 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6572 /* OSI in C-HDLC is stuffed with a fudge byte */
6573 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6578 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6579 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6585 break; // invalid qualifier
6588 assert_maxval(cstate
, "IS-IS PDU type", v
, ISIS_PDU_TYPE_MAX
);
6589 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
);
6591 * 4 is the offset of the PDU type relative to the IS-IS
6593 * Except when it is not, see above.
6595 unsigned pdu_type_offset
;
6596 switch (cstate
->linktype
) {
6599 pdu_type_offset
= 5;
6602 pdu_type_offset
= 4;
6604 b1
= gen_mcmp(cstate
, OR_LINKPL_NOSNAP
, pdu_type_offset
, BPF_B
,
6605 v
, ISIS_PDU_TYPE_MAX
);
6622 break; // invalid qualifier
6628 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6633 * Convert a non-numeric name to a port number.
6636 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6638 struct addrinfo hints
, *res
, *ai
;
6640 struct sockaddr_in
*in4
;
6642 struct sockaddr_in6
*in6
;
6647 * We check for both TCP and UDP in case there are
6648 * ambiguous entries.
6650 memset(&hints
, 0, sizeof(hints
));
6651 hints
.ai_family
= PF_UNSPEC
;
6652 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6653 hints
.ai_protocol
= ipproto
;
6654 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6661 * No such port. Just return -1.
6668 * We don't use strerror() because it's not
6669 * guaranteed to be thread-safe on all platforms
6670 * (probably because it might use a non-thread-local
6671 * buffer into which to format an error message
6672 * if the error code isn't one for which it has
6673 * a canned string; three cheers for C string
6676 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6678 port
= -2; /* a real error */
6684 * This is a real error, not just "there's
6685 * no such service name".
6687 * We don't use gai_strerror() because it's not
6688 * guaranteed to be thread-safe on all platforms
6689 * (probably because it might use a non-thread-local
6690 * buffer into which to format an error message
6691 * if the error code isn't one for which it has
6692 * a canned string; three cheers for C string
6695 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6697 port
= -2; /* a real error */
6702 * OK, we found it. Did it find anything?
6704 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6706 * Does it have an address?
6708 if (ai
->ai_addr
!= NULL
) {
6710 * Yes. Get a port number; we're done.
6712 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6713 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6714 port
= ntohs(in4
->sin_port
);
6718 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6719 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6720 port
= ntohs(in6
->sin6_port
);
6732 * Convert a string to a port number.
6735 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6745 * See if it's a number.
6747 ret
= stoulen(string
, string_size
, &val
, cstate
);
6751 /* Unknown port type - it's just a number. */
6752 *proto
= PROTO_UNDEF
;
6755 case STOULEN_NOT_OCTAL_NUMBER
:
6756 case STOULEN_NOT_HEX_NUMBER
:
6757 case STOULEN_NOT_DECIMAL_NUMBER
:
6759 * Not a valid number; try looking it up as a port.
6761 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6762 memcpy(cpy
, string
, string_size
);
6763 cpy
[string_size
] = '\0';
6764 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6765 if (tcp_port
== -2) {
6767 * We got a hard error; the error string has
6771 longjmp(cstate
->top_ctx
, 1);
6774 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6775 if (udp_port
== -2) {
6777 * We got a hard error; the error string has
6781 longjmp(cstate
->top_ctx
, 1);
6786 * We need to check /etc/services for ambiguous entries.
6787 * If we find an ambiguous entry, and it has the
6788 * same port number, change the proto to PROTO_UNDEF
6789 * so both TCP and UDP will be checked.
6791 if (tcp_port
>= 0) {
6792 val
= (bpf_u_int32
)tcp_port
;
6793 *proto
= IPPROTO_TCP
;
6794 if (udp_port
>= 0) {
6795 if (udp_port
== tcp_port
)
6796 *proto
= PROTO_UNDEF
;
6799 /* Can't handle ambiguous names that refer
6800 to different port numbers. */
6801 warning("ambiguous port %s in /etc/services",
6808 if (udp_port
>= 0) {
6809 val
= (bpf_u_int32
)udp_port
;
6810 *proto
= IPPROTO_UDP
;
6814 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6816 longjmp(cstate
->top_ctx
, 1);
6823 /* Error already set. */
6824 longjmp(cstate
->top_ctx
, 1);
6831 /* Should not happen */
6832 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6833 longjmp(cstate
->top_ctx
, 1);
6840 * Convert a string in the form PPP-PPP, which correspond to ports, to
6841 * a starting and ending port in a port range.
6844 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6845 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6848 const char *first
, *second
;
6849 size_t first_size
, second_size
;
6852 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6853 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6856 * Make sure there are no other hyphens.
6858 * XXX - we support named ports, but there are some port names
6859 * in /etc/services that include hyphens, so this would rule
6862 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6863 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6867 * Get the length of the first port.
6870 first_size
= hyphen_off
- string
;
6871 if (first_size
== 0) {
6872 /* Range of "-port", which we don't support. */
6873 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6877 * Try to convert it to a port.
6879 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6880 save_proto
= *proto
;
6883 * Get the length of the second port.
6885 second
= hyphen_off
+ 1;
6886 second_size
= strlen(second
);
6887 if (second_size
== 0) {
6888 /* Range of "port-", which we don't support. */
6889 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6893 * Try to convert it to a port.
6895 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6896 if (*proto
!= save_proto
)
6897 *proto
= PROTO_UNDEF
;
6901 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6903 int proto
= q
.proto
;
6907 bpf_u_int32 mask
, addr
;
6908 struct addrinfo
*res
, *res0
;
6909 struct sockaddr_in
*sin4
;
6912 struct sockaddr_in6
*sin6
;
6913 struct in6_addr mask128
;
6915 struct block
*b
, *tmp
;
6916 int port
, real_proto
;
6917 bpf_u_int32 port1
, port2
;
6920 * Catch errors reported by us and routines below us, and return NULL
6923 if (setjmp(cstate
->top_ctx
))
6929 addr
= pcap_nametonetaddr(name
);
6931 bpf_error(cstate
, "unknown network '%s'", name
);
6932 /* Left justify network addr and calculate its network mask */
6934 while (addr
&& (addr
& 0xff000000) == 0) {
6938 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6942 if (proto
== Q_LINK
) {
6943 switch (cstate
->linktype
) {
6946 case DLT_NETANALYZER
:
6947 case DLT_NETANALYZER_TRANSPARENT
:
6948 eaddr
= pcap_ether_hostton(name
);
6951 "unknown ether host '%s'", name
);
6952 tmp
= gen_prevlinkhdr_check(cstate
);
6953 b
= gen_ehostop(cstate
, eaddr
, dir
);
6960 eaddr
= pcap_ether_hostton(name
);
6963 "unknown FDDI host '%s'", name
);
6964 b
= gen_fhostop(cstate
, eaddr
, dir
);
6969 eaddr
= pcap_ether_hostton(name
);
6972 "unknown token ring host '%s'", name
);
6973 b
= gen_thostop(cstate
, eaddr
, dir
);
6977 case DLT_IEEE802_11
:
6978 case DLT_PRISM_HEADER
:
6979 case DLT_IEEE802_11_RADIO_AVS
:
6980 case DLT_IEEE802_11_RADIO
:
6982 eaddr
= pcap_ether_hostton(name
);
6985 "unknown 802.11 host '%s'", name
);
6986 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6990 case DLT_IP_OVER_FC
:
6991 eaddr
= pcap_ether_hostton(name
);
6994 "unknown Fibre Channel host '%s'", name
);
6995 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7000 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7001 } else if (proto
== Q_DECNET
) {
7003 * A long time ago on Ultrix libpcap supported
7004 * translation of DECnet host names into DECnet
7005 * addresses, but this feature is history now.
7007 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7010 memset(&mask128
, 0xff, sizeof(mask128
));
7012 res0
= res
= pcap_nametoaddrinfo(name
);
7014 bpf_error(cstate
, "unknown host '%s'", name
);
7021 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7022 tproto
== Q_DEFAULT
) {
7028 for (res
= res0
; res
; res
= res
->ai_next
) {
7029 switch (res
->ai_family
) {
7032 if (tproto
== Q_IPV6
)
7036 sin4
= (struct sockaddr_in
*)
7038 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7039 0xffffffff, tproto
, dir
, q
.addr
);
7043 if (tproto6
== Q_IP
)
7046 sin6
= (struct sockaddr_in6
*)
7048 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7049 &mask128
, tproto6
, dir
, q
.addr
);
7062 bpf_error(cstate
, "unknown host '%s'%s", name
,
7063 (proto
== Q_DEFAULT
)
7065 : " for specified address family");
7071 (void)port_pq_to_ipproto(cstate
, proto
, "port"); // validate only
7072 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7073 bpf_error(cstate
, "unknown port '%s'", name
);
7074 if (proto
== Q_UDP
) {
7075 if (real_proto
== IPPROTO_TCP
)
7076 bpf_error(cstate
, "port '%s' is tcp", name
);
7077 else if (real_proto
== IPPROTO_SCTP
)
7078 bpf_error(cstate
, "port '%s' is sctp", name
);
7080 /* override PROTO_UNDEF */
7081 real_proto
= IPPROTO_UDP
;
7083 if (proto
== Q_TCP
) {
7084 if (real_proto
== IPPROTO_UDP
)
7085 bpf_error(cstate
, "port '%s' is udp", name
);
7087 else if (real_proto
== IPPROTO_SCTP
)
7088 bpf_error(cstate
, "port '%s' is sctp", name
);
7090 /* override PROTO_UNDEF */
7091 real_proto
= IPPROTO_TCP
;
7093 if (proto
== Q_SCTP
) {
7094 if (real_proto
== IPPROTO_UDP
)
7095 bpf_error(cstate
, "port '%s' is udp", name
);
7097 else if (real_proto
== IPPROTO_TCP
)
7098 bpf_error(cstate
, "port '%s' is tcp", name
);
7100 /* override PROTO_UNDEF */
7101 real_proto
= IPPROTO_SCTP
;
7104 bpf_error(cstate
, "illegal port number %d < 0", port
);
7106 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7107 b
= gen_port(cstate
, port
, real_proto
, dir
);
7108 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7112 (void)port_pq_to_ipproto(cstate
, proto
, "portrange"); // validate only
7113 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7114 if (proto
== Q_UDP
) {
7115 if (real_proto
== IPPROTO_TCP
)
7116 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7117 else if (real_proto
== IPPROTO_SCTP
)
7118 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7120 /* override PROTO_UNDEF */
7121 real_proto
= IPPROTO_UDP
;
7123 if (proto
== Q_TCP
) {
7124 if (real_proto
== IPPROTO_UDP
)
7125 bpf_error(cstate
, "port in range '%s' is udp", name
);
7126 else if (real_proto
== IPPROTO_SCTP
)
7127 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7129 /* override PROTO_UNDEF */
7130 real_proto
= IPPROTO_TCP
;
7132 if (proto
== Q_SCTP
) {
7133 if (real_proto
== IPPROTO_UDP
)
7134 bpf_error(cstate
, "port in range '%s' is udp", name
);
7135 else if (real_proto
== IPPROTO_TCP
)
7136 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7138 /* override PROTO_UNDEF */
7139 real_proto
= IPPROTO_SCTP
;
7142 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7144 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7146 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7147 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7152 eaddr
= pcap_ether_hostton(name
);
7154 bpf_error(cstate
, "unknown ether host: %s", name
);
7156 res
= pcap_nametoaddrinfo(name
);
7159 bpf_error(cstate
, "unknown host '%s'", name
);
7160 b
= gen_gateway(cstate
, eaddr
, res
, proto
);
7165 bpf_error(cstate
, "unknown host '%s'", name
);
7168 bpf_error(cstate
, "'gateway' not supported in this configuration");
7172 real_proto
= lookup_proto(cstate
, name
, proto
);
7173 if (real_proto
>= 0)
7174 return gen_proto(cstate
, real_proto
, proto
);
7176 bpf_error(cstate
, "unknown protocol: %s", name
);
7178 #if !defined(NO_PROTOCHAIN)
7180 real_proto
= lookup_proto(cstate
, name
, proto
);
7181 if (real_proto
>= 0)
7182 return gen_protochain(cstate
, real_proto
, proto
);
7184 bpf_error(cstate
, "unknown protocol: %s", name
);
7185 #endif /* !defined(NO_PROTOCHAIN) */
7196 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7197 bpf_u_int32 masklen
, struct qual q
)
7199 register int nlen
, mlen
;
7204 * Catch errors reported by us and routines below us, and return NULL
7207 if (setjmp(cstate
->top_ctx
))
7210 nlen
= pcapint_atoin(s1
, &n
);
7212 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7213 /* Promote short ipaddr */
7217 mlen
= pcapint_atoin(s2
, &m
);
7219 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7220 /* Promote short ipaddr */
7223 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7226 /* Convert mask len to mask */
7228 bpf_error(cstate
, "mask length must be <= 32");
7229 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7230 m
= (bpf_u_int32
)m64
;
7232 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7239 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7242 // Q_HOST and Q_GATEWAY only (see the grammar)
7243 bpf_error(cstate
, "Mask syntax for networks only");
7250 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7258 * Catch errors reported by us and routines below us, and return NULL
7261 if (setjmp(cstate
->top_ctx
))
7268 * v contains a 32-bit unsigned parsed from a string of the
7269 * form {N}, which could be decimal, hexadecimal or octal.
7270 * Although it would be possible to use the value as a raw
7271 * 16-bit DECnet address when the value fits into 16 bits, this
7272 * would be a questionable feature: DECnet address wire
7273 * encoding is little-endian, so this would not work as
7274 * intuitively as the same works for [big-endian] IPv4
7275 * addresses (0x01020304 means 1.2.3.4).
7277 if (proto
== Q_DECNET
)
7278 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7280 } else if (proto
== Q_DECNET
) {
7282 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7283 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7284 * for a valid DECnet address.
7286 vlen
= pcapint_atodn(s
, &v
);
7288 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7291 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7292 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7295 vlen
= pcapint_atoin(s
, &v
);
7297 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7305 if (proto
== Q_DECNET
)
7306 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7307 else if (proto
== Q_LINK
) {
7308 // "link (host|net) IPV4ADDR" and variations thereof
7309 bpf_error(cstate
, "illegal link layer address");
7312 if (s
== NULL
&& q
.addr
== Q_NET
) {
7313 /* Promote short net number */
7314 while (v
&& (v
& 0xff000000) == 0) {
7319 /* Promote short ipaddr */
7321 mask
<<= 32 - vlen
;
7323 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7327 proto
= port_pq_to_ipproto(cstate
, proto
, "port");
7330 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7334 b
= gen_port(cstate
, v
, proto
, dir
);
7335 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7340 proto
= port_pq_to_ipproto(cstate
, proto
, "portrange");
7343 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7347 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7348 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7353 bpf_error(cstate
, "'gateway' requires a name");
7357 return gen_proto(cstate
, v
, proto
);
7359 #if !defined(NO_PROTOCHAIN)
7361 return gen_protochain(cstate
, v
, proto
);
7377 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7380 struct addrinfo
*res
;
7381 struct in6_addr
*addr
;
7382 struct in6_addr mask
;
7384 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7387 * Catch errors reported by us and routines below us, and return NULL
7390 if (setjmp(cstate
->top_ctx
))
7393 res
= pcap_nametoaddrinfo(s
);
7395 bpf_error(cstate
, "invalid ip6 address %s", s
);
7398 bpf_error(cstate
, "%s resolved to multiple address", s
);
7399 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7401 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7402 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7403 memset(&mask
, 0, sizeof(mask
));
7404 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7406 mask
.s6_addr
[masklen
/ 8] =
7407 (0xff << (8 - masklen
% 8)) & 0xff;
7410 memcpy(a
, addr
, sizeof(a
));
7411 memcpy(m
, &mask
, sizeof(m
));
7412 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7413 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7414 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7422 bpf_error(cstate
, "Mask syntax for networks only");
7426 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7432 // Q_GATEWAY only (see the grammar)
7433 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7440 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7442 struct block
*b
, *tmp
;
7445 * Catch errors reported by us and routines below us, and return NULL
7448 if (setjmp(cstate
->top_ctx
))
7451 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7452 cstate
->e
= pcap_ether_aton(s
);
7453 if (cstate
->e
== NULL
)
7454 bpf_error(cstate
, "malloc");
7455 switch (cstate
->linktype
) {
7457 case DLT_NETANALYZER
:
7458 case DLT_NETANALYZER_TRANSPARENT
:
7459 tmp
= gen_prevlinkhdr_check(cstate
);
7460 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7465 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7468 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7470 case DLT_IEEE802_11
:
7471 case DLT_PRISM_HEADER
:
7472 case DLT_IEEE802_11_RADIO_AVS
:
7473 case DLT_IEEE802_11_RADIO
:
7475 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7477 case DLT_IP_OVER_FC
:
7478 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7483 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7490 bpf_error(cstate
, "ethernet address used in non-ether expression");
7495 sappend(struct slist
*s0
, struct slist
*s1
)
7498 * This is definitely not the best way to do this, but the
7499 * lists will rarely get long.
7506 static struct slist
*
7507 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7511 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7516 static struct slist
*
7517 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7521 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7527 * Modify "index" to use the value stored into its register as an
7528 * offset relative to the beginning of the header for the protocol
7529 * "proto", and allocate a register and put an item "size" bytes long
7530 * (1, 2, or 4) at that offset into that register, making it the register
7533 static struct arth
*
7534 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7538 struct slist
*s
, *tmp
;
7540 int regno
= alloc_reg(cstate
);
7542 free_reg(cstate
, inst
->regno
);
7546 bpf_error(cstate
, "data size must be 1, 2, or 4");
7563 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7567 * The offset is relative to the beginning of the packet
7568 * data, if we have a radio header. (If we don't, this
7571 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7572 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7573 cstate
->linktype
!= DLT_PRISM_HEADER
)
7574 bpf_error(cstate
, "radio information not present in capture");
7577 * Load into the X register the offset computed into the
7578 * register specified by "index".
7580 s
= xfer_to_x(cstate
, inst
);
7583 * Load the item at that offset.
7585 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7587 sappend(inst
->s
, s
);
7592 * The offset is relative to the beginning of
7593 * the link-layer header.
7595 * XXX - what about ATM LANE? Should the index be
7596 * relative to the beginning of the AAL5 frame, so
7597 * that 0 refers to the beginning of the LE Control
7598 * field, or relative to the beginning of the LAN
7599 * frame, so that 0 refers, for Ethernet LANE, to
7600 * the beginning of the destination address?
7602 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7605 * If "s" is non-null, it has code to arrange that the
7606 * X register contains the length of the prefix preceding
7607 * the link-layer header. Add to it the offset computed
7608 * into the register specified by "index", and move that
7609 * into the X register. Otherwise, just load into the X
7610 * register the offset computed into the register specified
7614 sappend(s
, xfer_to_a(cstate
, inst
));
7615 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7616 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7618 s
= xfer_to_x(cstate
, inst
);
7621 * Load the item at the sum of the offset we've put in the
7622 * X register and the offset of the start of the link
7623 * layer header (which is 0 if the radio header is
7624 * variable-length; that header length is what we put
7625 * into the X register and then added to the index).
7627 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7628 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7630 sappend(inst
->s
, s
);
7644 * The offset is relative to the beginning of
7645 * the network-layer header.
7646 * XXX - are there any cases where we want
7647 * cstate->off_nl_nosnap?
7649 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7652 * If "s" is non-null, it has code to arrange that the
7653 * X register contains the variable part of the offset
7654 * of the link-layer payload. Add to it the offset
7655 * computed into the register specified by "index",
7656 * and move that into the X register. Otherwise, just
7657 * load into the X register the offset computed into
7658 * the register specified by "index".
7661 sappend(s
, xfer_to_a(cstate
, inst
));
7662 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7663 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7665 s
= xfer_to_x(cstate
, inst
);
7668 * Load the item at the sum of the offset we've put in the
7669 * X register, the offset of the start of the network
7670 * layer header from the beginning of the link-layer
7671 * payload, and the constant part of the offset of the
7672 * start of the link-layer payload.
7674 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7675 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7677 sappend(inst
->s
, s
);
7680 * Do the computation only if the packet contains
7681 * the protocol in question.
7683 b
= gen_proto_abbrev_internal(cstate
, proto
);
7685 gen_and(inst
->b
, b
);
7699 * The offset is relative to the beginning of
7700 * the transport-layer header.
7702 * Load the X register with the length of the IPv4 header
7703 * (plus the offset of the link-layer header, if it's
7704 * a variable-length header), in bytes.
7706 * XXX - are there any cases where we want
7707 * cstate->off_nl_nosnap?
7708 * XXX - we should, if we're built with
7709 * IPv6 support, generate code to load either
7710 * IPv4, IPv6, or both, as appropriate.
7712 s
= gen_loadx_iphdrlen(cstate
);
7715 * The X register now contains the sum of the variable
7716 * part of the offset of the link-layer payload and the
7717 * length of the network-layer header.
7719 * Load into the A register the offset relative to
7720 * the beginning of the transport layer header,
7721 * add the X register to that, move that to the
7722 * X register, and load with an offset from the
7723 * X register equal to the sum of the constant part of
7724 * the offset of the link-layer payload and the offset,
7725 * relative to the beginning of the link-layer payload,
7726 * of the network-layer header.
7728 sappend(s
, xfer_to_a(cstate
, inst
));
7729 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7730 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7731 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7732 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7733 sappend(inst
->s
, s
);
7736 * Do the computation only if the packet contains
7737 * the protocol in question - which is true only
7738 * if this is an IP datagram and is the first or
7739 * only fragment of that datagram.
7741 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7743 gen_and(inst
->b
, b
);
7744 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7749 * Do the computation only if the packet contains
7750 * the protocol in question.
7752 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7754 gen_and(inst
->b
, b
);
7758 * Check if we have an icmp6 next header
7760 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7762 gen_and(inst
->b
, b
);
7765 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7767 * If "s" is non-null, it has code to arrange that the
7768 * X register contains the variable part of the offset
7769 * of the link-layer payload. Add to it the offset
7770 * computed into the register specified by "index",
7771 * and move that into the X register. Otherwise, just
7772 * load into the X register the offset computed into
7773 * the register specified by "index".
7776 sappend(s
, xfer_to_a(cstate
, inst
));
7777 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7778 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7780 s
= xfer_to_x(cstate
, inst
);
7783 * Load the item at the sum of the offset we've put in the
7784 * X register, the offset of the start of the network
7785 * layer header from the beginning of the link-layer
7786 * payload, and the constant part of the offset of the
7787 * start of the link-layer payload.
7789 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7790 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7793 sappend(inst
->s
, s
);
7797 inst
->regno
= regno
;
7798 s
= new_stmt(cstate
, BPF_ST
);
7800 sappend(inst
->s
, s
);
7806 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7810 * Catch errors reported by us and routines below us, and return NULL
7813 if (setjmp(cstate
->top_ctx
))
7816 return gen_load_internal(cstate
, proto
, inst
, size
);
7819 static struct block
*
7820 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7821 struct arth
*a1
, int reversed
)
7823 struct slist
*s0
, *s1
, *s2
;
7824 struct block
*b
, *tmp
;
7826 s0
= xfer_to_x(cstate
, a1
);
7827 s1
= xfer_to_a(cstate
, a0
);
7828 if (code
== BPF_JEQ
) {
7829 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7830 b
= new_block(cstate
, JMP(code
));
7834 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7840 sappend(a0
->s
, a1
->s
);
7844 free_reg(cstate
, a0
->regno
);
7845 free_reg(cstate
, a1
->regno
);
7847 /* 'and' together protocol checks */
7850 gen_and(a0
->b
, tmp
= a1
->b
);
7864 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7865 struct arth
*a1
, int reversed
)
7868 * Catch errors reported by us and routines below us, and return NULL
7871 if (setjmp(cstate
->top_ctx
))
7874 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7878 gen_loadlen(compiler_state_t
*cstate
)
7885 * Catch errors reported by us and routines below us, and return NULL
7888 if (setjmp(cstate
->top_ctx
))
7891 regno
= alloc_reg(cstate
);
7892 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7893 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7894 s
->next
= new_stmt(cstate
, BPF_ST
);
7895 s
->next
->s
.k
= regno
;
7902 static struct arth
*
7903 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7909 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7911 reg
= alloc_reg(cstate
);
7913 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7915 s
->next
= new_stmt(cstate
, BPF_ST
);
7924 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7927 * Catch errors reported by us and routines below us, and return NULL
7930 if (setjmp(cstate
->top_ctx
))
7933 return gen_loadi_internal(cstate
, val
);
7937 * The a_arg dance is to avoid annoying whining by compilers that
7938 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7939 * It's not *used* after setjmp returns.
7942 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7944 struct arth
*a
= a_arg
;
7948 * Catch errors reported by us and routines below us, and return NULL
7951 if (setjmp(cstate
->top_ctx
))
7954 s
= xfer_to_a(cstate
, a
);
7956 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7959 s
= new_stmt(cstate
, BPF_ST
);
7967 * The a0_arg dance is to avoid annoying whining by compilers that
7968 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7969 * It's not *used* after setjmp returns.
7972 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7975 struct arth
*a0
= a0_arg
;
7976 struct slist
*s0
, *s1
, *s2
;
7979 * Catch errors reported by us and routines below us, and return NULL
7982 if (setjmp(cstate
->top_ctx
))
7986 * Disallow division by, or modulus by, zero; we do this here
7987 * so that it gets done even if the optimizer is disabled.
7989 * Also disallow shifts by a value greater than 31; we do this
7990 * here, for the same reason.
7992 if (code
== BPF_DIV
) {
7993 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7994 bpf_error(cstate
, "division by zero");
7995 } else if (code
== BPF_MOD
) {
7996 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7997 bpf_error(cstate
, "modulus by zero");
7998 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7999 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8000 bpf_error(cstate
, "shift by more than 31 bits");
8002 s0
= xfer_to_x(cstate
, a1
);
8003 s1
= xfer_to_a(cstate
, a0
);
8004 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8009 sappend(a0
->s
, a1
->s
);
8011 free_reg(cstate
, a0
->regno
);
8012 free_reg(cstate
, a1
->regno
);
8014 s0
= new_stmt(cstate
, BPF_ST
);
8015 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8022 * Initialize the table of used registers and the current register.
8025 init_regs(compiler_state_t
*cstate
)
8028 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8032 * Return the next free register.
8035 alloc_reg(compiler_state_t
*cstate
)
8037 int n
= BPF_MEMWORDS
;
8040 if (cstate
->regused
[cstate
->curreg
])
8041 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8043 cstate
->regused
[cstate
->curreg
] = 1;
8044 return cstate
->curreg
;
8047 bpf_error(cstate
, "too many registers needed to evaluate expression");
8052 * Return a register to the table so it can
8056 free_reg(compiler_state_t
*cstate
, int n
)
8058 cstate
->regused
[n
] = 0;
8061 static struct block
*
8062 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8067 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8068 b
= new_block(cstate
, JMP(jmp
));
8076 gen_greater(compiler_state_t
*cstate
, int n
)
8079 * Catch errors reported by us and routines below us, and return NULL
8082 if (setjmp(cstate
->top_ctx
))
8085 return gen_len(cstate
, BPF_JGE
, n
);
8089 * Actually, this is less than or equal.
8092 gen_less(compiler_state_t
*cstate
, int n
)
8097 * Catch errors reported by us and routines below us, and return NULL
8100 if (setjmp(cstate
->top_ctx
))
8103 b
= gen_len(cstate
, BPF_JGT
, n
);
8110 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8111 * the beginning of the link-layer header.
8112 * XXX - that means you can't test values in the radiotap header, but
8113 * as that header is difficult if not impossible to parse generally
8114 * without a loop, that might not be a severe problem. A new keyword
8115 * "radio" could be added for that, although what you'd really want
8116 * would be a way of testing particular radio header values, which
8117 * would generate code appropriate to the radio header in question.
8120 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8126 * Catch errors reported by us and routines below us, and return NULL
8129 if (setjmp(cstate
->top_ctx
))
8132 assert_maxval(cstate
, "byte argument", val
, UINT8_MAX
);
8139 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8142 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8146 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8150 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8154 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8158 // Load the required byte first.
8159 struct slist
*s0
= gen_load_a(cstate
, OR_LINKHDR
, idx
, BPF_B
);
8161 b
= new_block(cstate
, JMP(BPF_JEQ
));
8169 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8171 bpf_u_int32 hostmask
;
8172 struct block
*b0
, *b1
, *b2
;
8173 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8176 * Catch errors reported by us and routines below us, and return NULL
8179 if (setjmp(cstate
->top_ctx
))
8186 switch (cstate
->linktype
) {
8188 case DLT_ARCNET_LINUX
:
8189 // ARCnet broadcast is [8-bit] destination address 0.
8190 return gen_ahostop(cstate
, 0, Q_DST
);
8192 case DLT_NETANALYZER
:
8193 case DLT_NETANALYZER_TRANSPARENT
:
8194 b1
= gen_prevlinkhdr_check(cstate
);
8195 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8200 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8202 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8203 case DLT_IEEE802_11
:
8204 case DLT_PRISM_HEADER
:
8205 case DLT_IEEE802_11_RADIO_AVS
:
8206 case DLT_IEEE802_11_RADIO
:
8208 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8209 case DLT_IP_OVER_FC
:
8210 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8212 fail_kw_on_dlt(cstate
, "broadcast");
8217 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8218 * as an indication that we don't know the netmask, and fail
8221 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8222 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8223 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8224 hostmask
= ~cstate
->netmask
;
8225 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8226 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8231 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
8236 * Generate code to test the low-order bit of a MAC address (that's
8237 * the bottom bit of the *first* byte).
8239 static struct block
*
8240 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8242 register struct block
*b0
;
8243 register struct slist
*s
;
8245 /* link[offset] & 1 != 0 */
8246 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8247 b0
= new_block(cstate
, JMP(BPF_JSET
));
8254 gen_multicast(compiler_state_t
*cstate
, int proto
)
8256 register struct block
*b0
, *b1
, *b2
;
8257 register struct slist
*s
;
8260 * Catch errors reported by us and routines below us, and return NULL
8263 if (setjmp(cstate
->top_ctx
))
8270 switch (cstate
->linktype
) {
8272 case DLT_ARCNET_LINUX
:
8273 // ARCnet multicast is the same as broadcast.
8274 return gen_ahostop(cstate
, 0, Q_DST
);
8276 case DLT_NETANALYZER
:
8277 case DLT_NETANALYZER_TRANSPARENT
:
8278 b1
= gen_prevlinkhdr_check(cstate
);
8279 /* ether[0] & 1 != 0 */
8280 b0
= gen_mac_multicast(cstate
, 0);
8286 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8288 * XXX - was that referring to bit-order issues?
8290 /* fddi[1] & 1 != 0 */
8291 return gen_mac_multicast(cstate
, 1);
8293 /* tr[2] & 1 != 0 */
8294 return gen_mac_multicast(cstate
, 2);
8295 case DLT_IEEE802_11
:
8296 case DLT_PRISM_HEADER
:
8297 case DLT_IEEE802_11_RADIO_AVS
:
8298 case DLT_IEEE802_11_RADIO
:
8303 * For control frames, there is no DA.
8305 * For management frames, DA is at an
8306 * offset of 4 from the beginning of
8309 * For data frames, DA is at an offset
8310 * of 4 from the beginning of the packet
8311 * if To DS is clear and at an offset of
8312 * 16 from the beginning of the packet
8317 * Generate the tests to be done for data frames.
8319 * First, check for To DS set, i.e. "link[1] & 0x01".
8321 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8322 b1
= new_block(cstate
, JMP(BPF_JSET
));
8323 b1
->s
.k
= 0x01; /* To DS */
8327 * If To DS is set, the DA is at 16.
8329 b0
= gen_mac_multicast(cstate
, 16);
8333 * Now, check for To DS not set, i.e. check
8334 * "!(link[1] & 0x01)".
8336 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8337 b2
= new_block(cstate
, JMP(BPF_JSET
));
8338 b2
->s
.k
= 0x01; /* To DS */
8343 * If To DS is not set, the DA is at 4.
8345 b1
= gen_mac_multicast(cstate
, 4);
8349 * Now OR together the last two checks. That gives
8350 * the complete set of checks for data frames.
8355 * Now check for a data frame.
8356 * I.e, check "link[0] & 0x08".
8358 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8359 b1
= new_block(cstate
, JMP(BPF_JSET
));
8364 * AND that with the checks done for data frames.
8369 * If the high-order bit of the type value is 0, this
8370 * is a management frame.
8371 * I.e, check "!(link[0] & 0x08)".
8373 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8374 b2
= new_block(cstate
, JMP(BPF_JSET
));
8380 * For management frames, the DA is at 4.
8382 b1
= gen_mac_multicast(cstate
, 4);
8386 * OR that with the checks done for data frames.
8387 * That gives the checks done for management and
8393 * If the low-order bit of the type value is 1,
8394 * this is either a control frame or a frame
8395 * with a reserved type, and thus not a
8398 * I.e., check "!(link[0] & 0x04)".
8400 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8401 b1
= new_block(cstate
, JMP(BPF_JSET
));
8407 * AND that with the checks for data and management
8412 case DLT_IP_OVER_FC
:
8413 b0
= gen_mac_multicast(cstate
, 2);
8418 fail_kw_on_dlt(cstate
, "multicast");
8422 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8423 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8428 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8429 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8433 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8439 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8440 * we can look at special meta-data in the filter expression; otherwise we
8441 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8442 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8443 * pcap_activate() conditionally sets.
8446 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8448 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8450 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8452 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8457 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8459 register struct block
*b0
;
8462 * Catch errors reported by us and routines below us, and return NULL
8465 if (setjmp(cstate
->top_ctx
))
8469 * Only some data link types support ifindex qualifiers.
8471 switch (cstate
->linktype
) {
8472 case DLT_LINUX_SLL2
:
8473 /* match packets on this interface */
8474 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8477 #if defined(__linux__)
8478 require_basic_bpf_extensions(cstate
, "ifindex");
8480 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8482 #else /* defined(__linux__) */
8483 fail_kw_on_dlt(cstate
, "ifindex");
8485 #endif /* defined(__linux__) */
8491 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8492 * Outbound traffic is sent by this machine, while inbound traffic is
8493 * sent by a remote machine (and may include packets destined for a
8494 * unicast or multicast link-layer address we are not subscribing to).
8495 * These are the same definitions implemented by pcap_setdirection().
8496 * Capturing only unicast traffic destined for this host is probably
8497 * better accomplished using a higher-layer filter.
8500 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8502 register struct block
*b0
;
8505 * Catch errors reported by us and routines below us, and return NULL
8508 if (setjmp(cstate
->top_ctx
))
8512 * Only some data link types support inbound/outbound qualifiers.
8514 switch (cstate
->linktype
) {
8516 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8517 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8521 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8522 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8526 /* match outgoing packets */
8527 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8529 /* to filter on inbound traffic, invert the match */
8534 case DLT_LINUX_SLL2
:
8535 /* match outgoing packets */
8536 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8538 /* to filter on inbound traffic, invert the match */
8544 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8545 outbound
? PF_OUT
: PF_IN
);
8549 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8552 case DLT_JUNIPER_MFR
:
8553 case DLT_JUNIPER_MLFR
:
8554 case DLT_JUNIPER_MLPPP
:
8555 case DLT_JUNIPER_ATM1
:
8556 case DLT_JUNIPER_ATM2
:
8557 case DLT_JUNIPER_PPPOE
:
8558 case DLT_JUNIPER_PPPOE_ATM
:
8559 case DLT_JUNIPER_GGSN
:
8560 case DLT_JUNIPER_ES
:
8561 case DLT_JUNIPER_MONITOR
:
8562 case DLT_JUNIPER_SERVICES
:
8563 case DLT_JUNIPER_ETHER
:
8564 case DLT_JUNIPER_PPP
:
8565 case DLT_JUNIPER_FRELAY
:
8566 case DLT_JUNIPER_CHDLC
:
8567 case DLT_JUNIPER_VP
:
8568 case DLT_JUNIPER_ST
:
8569 case DLT_JUNIPER_ISM
:
8570 case DLT_JUNIPER_VS
:
8571 case DLT_JUNIPER_SRX_E2E
:
8572 case DLT_JUNIPER_FIBRECHANNEL
:
8573 case DLT_JUNIPER_ATM_CEMIC
:
8574 /* juniper flags (including direction) are stored
8575 * the byte after the 3-byte magic number */
8576 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8581 * If we have packet meta-data indicating a direction,
8582 * and that metadata can be checked by BPF code, check
8583 * it. Otherwise, give up, as this link-layer type has
8584 * nothing in the packet data.
8586 * Currently, the only platform where a BPF filter can
8587 * check that metadata is Linux with the in-kernel
8588 * BPF interpreter. If other packet capture mechanisms
8589 * and BPF filters also supported this, it would be
8590 * nice. It would be even better if they made that
8591 * metadata available so that we could provide it
8592 * with newer capture APIs, allowing it to be saved
8595 #if defined(__linux__)
8596 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8597 /* match outgoing packets */
8598 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8601 /* to filter on inbound traffic, invert the match */
8604 #else /* defined(__linux__) */
8605 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8607 #endif /* defined(__linux__) */
8612 /* PF firewall log matched interface */
8614 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8620 * Catch errors reported by us and routines below us, and return NULL
8623 if (setjmp(cstate
->top_ctx
))
8626 assert_pflog(cstate
, "ifname");
8628 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8629 off
= offsetof(struct pfloghdr
, ifname
);
8630 if (strlen(ifname
) >= len
) {
8631 bpf_error(cstate
, "ifname interface names can only be %d characters",
8635 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8636 (const u_char
*)ifname
);
8640 /* PF firewall log ruleset name */
8642 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8647 * Catch errors reported by us and routines below us, and return NULL
8650 if (setjmp(cstate
->top_ctx
))
8653 assert_pflog(cstate
, "ruleset");
8655 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8656 bpf_error(cstate
, "ruleset names can only be %ld characters",
8657 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8661 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8662 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8666 /* PF firewall log rule number */
8668 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8673 * Catch errors reported by us and routines below us, and return NULL
8676 if (setjmp(cstate
->top_ctx
))
8679 assert_pflog(cstate
, "rnr");
8681 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8686 /* PF firewall log sub-rule number */
8688 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8693 * Catch errors reported by us and routines below us, and return NULL
8696 if (setjmp(cstate
->top_ctx
))
8699 assert_pflog(cstate
, "srnr");
8701 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8706 /* PF firewall log reason code */
8708 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8713 * Catch errors reported by us and routines below us, and return NULL
8716 if (setjmp(cstate
->top_ctx
))
8719 assert_pflog(cstate
, "reason");
8721 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8722 (bpf_u_int32
)reason
);
8726 /* PF firewall log action */
8728 gen_pf_action(compiler_state_t
*cstate
, int action
)
8733 * Catch errors reported by us and routines below us, and return NULL
8736 if (setjmp(cstate
->top_ctx
))
8739 assert_pflog(cstate
, "action");
8741 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8742 (bpf_u_int32
)action
);
8746 /* IEEE 802.11 wireless header */
8748 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8753 * Catch errors reported by us and routines below us, and return NULL
8756 if (setjmp(cstate
->top_ctx
))
8759 switch (cstate
->linktype
) {
8761 case DLT_IEEE802_11
:
8762 case DLT_PRISM_HEADER
:
8763 case DLT_IEEE802_11_RADIO_AVS
:
8764 case DLT_IEEE802_11_RADIO
:
8766 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8770 fail_kw_on_dlt(cstate
, "type/subtype");
8778 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8783 * Catch errors reported by us and routines below us, and return NULL
8786 if (setjmp(cstate
->top_ctx
))
8789 switch (cstate
->linktype
) {
8791 case DLT_IEEE802_11
:
8792 case DLT_PRISM_HEADER
:
8793 case DLT_IEEE802_11_RADIO_AVS
:
8794 case DLT_IEEE802_11_RADIO
:
8799 fail_kw_on_dlt(cstate
, "dir");
8803 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8804 IEEE80211_FC1_DIR_MASK
);
8809 // Process an ARCnet host address string.
8811 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8814 * Catch errors reported by us and routines below us, and return NULL
8817 if (setjmp(cstate
->top_ctx
))
8820 switch (cstate
->linktype
) {
8823 case DLT_ARCNET_LINUX
:
8824 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8825 q
.proto
== Q_LINK
) {
8828 * The lexer currently defines the address format in a
8829 * way that makes this error condition never true.
8830 * Let's check it anyway in case this part of the lexer
8831 * changes in future.
8833 if (! pcapint_atoan(s
, &addr
))
8834 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8835 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8837 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8841 bpf_error(cstate
, "aid supported only on ARCnet");
8846 // Compare an ARCnet host address with the given value.
8847 static struct block
*
8848 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8850 register struct block
*b0
, *b1
;
8854 * ARCnet is different from Ethernet: the source address comes before
8855 * the destination address, each is one byte long. This holds for all
8856 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8857 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8858 * by Datapoint (document number 61610-01).
8861 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8864 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8867 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8868 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8874 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8875 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8885 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8892 static struct block
*
8893 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8895 struct block
*b0
, *b1
;
8897 /* check for VLAN, including 802.1ad and QinQ */
8898 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8899 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8902 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8908 static struct block
*
8909 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8911 assert_maxval(cstate
, "VLAN tag", vlan_num
, 0x0fff);
8912 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8915 static struct block
*
8916 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8919 struct block
*b0
, *b1
;
8921 b0
= gen_vlan_tpid_test(cstate
);
8924 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8930 * Both payload and link header type follow the VLAN tags so that
8931 * both need to be updated.
8933 cstate
->off_linkpl
.constant_part
+= 4;
8934 cstate
->off_linktype
.constant_part
+= 4;
8939 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8940 /* add v to variable part of off */
8942 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8943 bpf_u_int32 v
, struct slist
*s
)
8947 if (!off
->is_variable
)
8948 off
->is_variable
= 1;
8950 off
->reg
= alloc_reg(cstate
);
8952 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8955 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8958 s2
= new_stmt(cstate
, BPF_ST
);
8964 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8965 * and link type offsets first
8968 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8972 /* offset determined at run time, shift variable part */
8974 cstate
->is_vlan_vloffset
= 1;
8975 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8976 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8978 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8979 sappend(s
.next
, b_tpid
->head
->stmts
);
8980 b_tpid
->head
->stmts
= s
.next
;
8984 * patch block b_vid (VLAN id test) to load VID value either from packet
8985 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8988 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8990 struct slist
*s
, *s2
, *sjeq
;
8993 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8994 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8996 /* true -> next instructions, false -> beginning of b_vid */
8997 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8999 sjeq
->s
.jf
= b_vid
->stmts
;
9002 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
9003 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
9007 /* Jump to the test in b_vid. We need to jump one instruction before
9008 * the end of the b_vid block so that we only skip loading the TCI
9009 * from packet data and not the 'and' instruction extracting VID.
9012 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9014 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9018 /* insert our statements at the beginning of b_vid */
9019 sappend(s
, b_vid
->stmts
);
9024 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9025 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9026 * tag can be either in metadata or in packet data; therefore if the
9027 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9028 * header for VLAN tag. As the decision is done at run time, we need
9029 * update variable part of the offsets
9031 static struct block
*
9032 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9035 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9038 /* generate new filter code based on extracting packet
9040 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9041 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9043 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9048 * This is tricky. We need to insert the statements updating variable
9049 * parts of offsets before the traditional TPID and VID tests so
9050 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9051 * we do not want this update to affect those checks. That's why we
9052 * generate both test blocks first and insert the statements updating
9053 * variable parts of both offsets after that. This wouldn't work if
9054 * there already were variable length link header when entering this
9055 * function but gen_vlan_bpf_extensions() isn't called in that case.
9057 b_tpid
= gen_vlan_tpid_test(cstate
);
9059 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9061 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9066 gen_vlan_patch_vid_test(cstate
, b_vid
);
9076 * support IEEE 802.1Q VLAN trunk over ethernet
9079 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9084 * Catch errors reported by us and routines below us, and return NULL
9087 if (setjmp(cstate
->top_ctx
))
9090 /* can't check for VLAN-encapsulated packets inside MPLS */
9091 if (cstate
->label_stack_depth
> 0)
9092 bpf_error(cstate
, "no VLAN match after MPLS");
9095 * Check for a VLAN packet, and then change the offsets to point
9096 * to the type and data fields within the VLAN packet. Just
9097 * increment the offsets, so that we can support a hierarchy, e.g.
9098 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9101 * XXX - this is a bit of a kludge. If we were to split the
9102 * compiler into a parser that parses an expression and
9103 * generates an expression tree, and a code generator that
9104 * takes an expression tree (which could come from our
9105 * parser or from some other parser) and generates BPF code,
9106 * we could perhaps make the offsets parameters of routines
9107 * and, in the handler for an "AND" node, pass to subnodes
9108 * other than the VLAN node the adjusted offsets.
9110 * This would mean that "vlan" would, instead of changing the
9111 * behavior of *all* tests after it, change only the behavior
9112 * of tests ANDed with it. That would change the documented
9113 * semantics of "vlan", which might break some expressions.
9114 * However, it would mean that "(vlan and ip) or ip" would check
9115 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9116 * checking only for VLAN-encapsulated IP, so that could still
9117 * be considered worth doing; it wouldn't break expressions
9118 * that are of the form "vlan and ..." or "vlan N and ...",
9119 * which I suspect are the most common expressions involving
9120 * "vlan". "vlan or ..." doesn't necessarily do what the user
9121 * would really want, now, as all the "or ..." tests would
9122 * be done assuming a VLAN, even though the "or" could be viewed
9123 * as meaning "or, if this isn't a VLAN packet...".
9125 switch (cstate
->linktype
) {
9129 * Newer version of the Linux kernel pass around
9130 * packets in which the VLAN tag has been removed
9131 * from the packet data and put into metadata.
9133 * This requires special treatment.
9135 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9136 /* Verify that this is the outer part of the packet and
9137 * not encapsulated somehow. */
9138 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9139 cstate
->off_linkhdr
.constant_part
==
9140 cstate
->off_outermostlinkhdr
.constant_part
) {
9142 * Do we need special VLAN handling?
9144 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9145 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9148 b0
= gen_vlan_no_bpf_extensions(cstate
,
9149 vlan_num
, has_vlan_tag
);
9152 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9156 case DLT_NETANALYZER
:
9157 case DLT_NETANALYZER_TRANSPARENT
:
9158 case DLT_IEEE802_11
:
9159 case DLT_PRISM_HEADER
:
9160 case DLT_IEEE802_11_RADIO_AVS
:
9161 case DLT_IEEE802_11_RADIO
:
9163 * These are either Ethernet packets with an additional
9164 * metadata header (the NetAnalyzer types), or 802.11
9165 * packets, possibly with an additional metadata header.
9167 * For the first of those, the VLAN tag is in the normal
9168 * place, so the special-case handling above isn't
9171 * For the second of those, we don't do the special-case
9174 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9178 bpf_error(cstate
, "no VLAN support for %s",
9179 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9183 cstate
->vlan_stack_depth
++;
9191 * The label_num_arg dance is to avoid annoying whining by compilers that
9192 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9193 * It's not *used* after setjmp returns.
9195 static struct block
*
9196 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9199 struct block
*b0
, *b1
;
9201 if (cstate
->label_stack_depth
> 0) {
9202 /* just match the bottom-of-stack bit clear */
9203 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9206 * We're not in an MPLS stack yet, so check the link-layer
9207 * type against MPLS.
9209 switch (cstate
->linktype
) {
9211 case DLT_C_HDLC
: /* fall through */
9214 case DLT_NETANALYZER
:
9215 case DLT_NETANALYZER_TRANSPARENT
:
9216 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9220 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9223 /* FIXME add other DLT_s ...
9224 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9225 * leave it for now */
9228 bpf_error(cstate
, "no MPLS support for %s",
9229 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9234 /* If a specific MPLS label is requested, check it */
9235 if (has_label_num
) {
9236 assert_maxval(cstate
, "MPLS label", label_num
, 0xFFFFF);
9237 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9238 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9239 0xfffff000); /* only compare the first 20 bits */
9245 * Change the offsets to point to the type and data fields within
9246 * the MPLS packet. Just increment the offsets, so that we
9247 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9248 * capture packets with an outer label of 100000 and an inner
9251 * Increment the MPLS stack depth as well; this indicates that
9252 * we're checking MPLS-encapsulated headers, to make sure higher
9253 * level code generators don't try to match against IP-related
9254 * protocols such as Q_ARP, Q_RARP etc.
9256 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9258 cstate
->off_nl_nosnap
+= 4;
9259 cstate
->off_nl
+= 4;
9260 cstate
->label_stack_depth
++;
9265 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9268 * Catch errors reported by us and routines below us, and return NULL
9271 if (setjmp(cstate
->top_ctx
))
9274 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9278 * Support PPPOE discovery and session.
9281 gen_pppoed(compiler_state_t
*cstate
)
9284 * Catch errors reported by us and routines below us, and return NULL
9287 if (setjmp(cstate
->top_ctx
))
9290 /* check for PPPoE discovery */
9291 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9295 * RFC 2516 Section 4:
9297 * The Ethernet payload for PPPoE is as follows:
9300 * 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
9301 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9302 * | VER | TYPE | CODE | SESSION_ID |
9303 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9304 * | LENGTH | payload ~
9305 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9308 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9310 struct block
*b0
, *b1
;
9313 * Catch errors reported by us and routines below us, and return NULL
9316 if (setjmp(cstate
->top_ctx
))
9320 * Test against the PPPoE session link-layer type.
9322 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9324 /* If a specific session is requested, check PPPoE session id */
9326 assert_maxval(cstate
, "PPPoE session number", sess_num
, UINT16_MAX
);
9327 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9333 * Change the offsets to point to the type and data fields within
9334 * the PPP packet, and note that this is PPPoE rather than
9337 * XXX - this is a bit of a kludge. See the comments in
9340 * The "network-layer" protocol is PPPoE, which has a 6-byte
9341 * PPPoE header, followed by a PPP packet.
9343 * There is no HDLC encapsulation for the PPP packet (it's
9344 * encapsulated in PPPoES instead), so the link-layer type
9345 * starts at the first byte of the PPP packet. For PPPoE,
9346 * that offset is relative to the beginning of the total
9347 * link-layer payload, including any 802.2 LLC header, so
9348 * it's 6 bytes past cstate->off_nl.
9350 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9351 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9352 cstate
->off_linkpl
.reg
);
9354 cstate
->off_linktype
= cstate
->off_linkhdr
;
9355 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9358 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9363 /* Check that this is Geneve and the VNI is correct if
9364 * specified. Parameterized to handle both IPv4 and IPv6. */
9365 static struct block
*
9366 gen_geneve_check(compiler_state_t
*cstate
,
9367 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9368 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9370 struct block
*b0
, *b1
;
9372 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9374 /* Check that we are operating on version 0. Otherwise, we
9375 * can't decode the rest of the fields. The version is 2 bits
9376 * in the first byte of the Geneve header. */
9377 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9382 assert_maxval(cstate
, "Geneve VNI", vni
, 0xffffff);
9383 vni
<<= 8; /* VNI is in the upper 3 bytes */
9384 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9392 /* The IPv4 and IPv6 Geneve checks need to do two things:
9393 * - Verify that this actually is Geneve with the right VNI.
9394 * - Place the IP header length (plus variable link prefix if
9395 * needed) into register A to be used later to compute
9396 * the inner packet offsets. */
9397 static struct block
*
9398 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9400 struct block
*b0
, *b1
;
9401 struct slist
*s
, *s1
;
9403 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9405 /* Load the IP header length into A. */
9406 s
= gen_loadx_iphdrlen(cstate
);
9408 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9411 /* Forcibly append these statements to the true condition
9412 * of the protocol check by creating a new block that is
9413 * always true and ANDing them. */
9414 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9423 static struct block
*
9424 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9426 struct block
*b0
, *b1
;
9427 struct slist
*s
, *s1
;
9429 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9431 /* Load the IP header length. We need to account for a
9432 * variable length link prefix if there is one. */
9433 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9435 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9439 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9443 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9447 /* Forcibly append these statements to the true condition
9448 * of the protocol check by creating a new block that is
9449 * always true and ANDing them. */
9450 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9453 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9462 /* We need to store three values based on the Geneve header::
9463 * - The offset of the linktype.
9464 * - The offset of the end of the Geneve header.
9465 * - The offset of the end of the encapsulated MAC header. */
9466 static struct slist
*
9467 gen_geneve_offsets(compiler_state_t
*cstate
)
9469 struct slist
*s
, *s1
, *s_proto
;
9471 /* First we need to calculate the offset of the Geneve header
9472 * itself. This is composed of the IP header previously calculated
9473 * (include any variable link prefix) and stored in A plus the
9474 * fixed sized headers (fixed link prefix, MAC length, and UDP
9476 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9477 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9479 /* Stash this in X since we'll need it later. */
9480 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9483 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9485 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9489 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9490 cstate
->off_linktype
.is_variable
= 1;
9491 cstate
->off_linktype
.constant_part
= 0;
9493 s1
= new_stmt(cstate
, BPF_ST
);
9494 s1
->s
.k
= cstate
->off_linktype
.reg
;
9497 /* Load the Geneve option length and mask and shift to get the
9498 * number of bytes. It is stored in the first byte of the Geneve
9500 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9504 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9508 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9512 /* Add in the rest of the Geneve base header. */
9513 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9517 /* Add the Geneve header length to its offset and store. */
9518 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9522 /* Set the encapsulated type as Ethernet. Even though we may
9523 * not actually have Ethernet inside there are two reasons this
9525 * - The linktype field is always in EtherType format regardless
9526 * of whether it is in Geneve or an inner Ethernet frame.
9527 * - The only link layer that we have specific support for is
9528 * Ethernet. We will confirm that the packet actually is
9529 * Ethernet at runtime before executing these checks. */
9530 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9532 s1
= new_stmt(cstate
, BPF_ST
);
9533 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9536 /* Calculate whether we have an Ethernet header or just raw IP/
9537 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9538 * and linktype by 14 bytes so that the network header can be found
9539 * seamlessly. Otherwise, keep what we've calculated already. */
9541 /* We have a bare jmp so we can't use the optimizer. */
9542 cstate
->no_optimize
= 1;
9544 /* Load the EtherType in the Geneve header, 2 bytes in. */
9545 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9549 /* Load X with the end of the Geneve header. */
9550 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9551 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9554 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9555 * end of this check, we should have the total length in X. In
9556 * the non-Ethernet case, it's already there. */
9557 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9558 s_proto
->s
.k
= ETHERTYPE_TEB
;
9559 sappend(s
, s_proto
);
9561 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9565 /* Since this is Ethernet, use the EtherType of the payload
9566 * directly as the linktype. Overwrite what we already have. */
9567 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9571 s1
= new_stmt(cstate
, BPF_ST
);
9572 s1
->s
.k
= cstate
->off_linktype
.reg
;
9575 /* Advance two bytes further to get the end of the Ethernet
9577 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9581 /* Move the result to X. */
9582 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9585 /* Store the final result of our linkpl calculation. */
9586 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9587 cstate
->off_linkpl
.is_variable
= 1;
9588 cstate
->off_linkpl
.constant_part
= 0;
9590 s1
= new_stmt(cstate
, BPF_STX
);
9591 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9600 /* Check to see if this is a Geneve packet. */
9602 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9604 struct block
*b0
, *b1
;
9608 * Catch errors reported by us and routines below us, and return NULL
9611 if (setjmp(cstate
->top_ctx
))
9614 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9615 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9620 /* Later filters should act on the payload of the Geneve frame,
9621 * update all of the header pointers. Attach this code so that
9622 * it gets executed in the event that the Geneve filter matches. */
9623 s
= gen_geneve_offsets(cstate
);
9625 b1
= gen_true(cstate
);
9626 sappend(s
, b1
->stmts
);
9631 cstate
->is_encap
= 1;
9636 /* Check that this is VXLAN and the VNI is correct if
9637 * specified. Parameterized to handle both IPv4 and IPv6. */
9638 static struct block
*
9639 gen_vxlan_check(compiler_state_t
*cstate
,
9640 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9641 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9643 struct block
*b0
, *b1
;
9645 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9647 /* Check that the VXLAN header has the flag bits set
9649 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9654 assert_maxval(cstate
, "VXLAN VNI", vni
, 0xffffff);
9655 vni
<<= 8; /* VNI is in the upper 3 bytes */
9656 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9664 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9665 * - Verify that this actually is VXLAN with the right VNI.
9666 * - Place the IP header length (plus variable link prefix if
9667 * needed) into register A to be used later to compute
9668 * the inner packet offsets. */
9669 static struct block
*
9670 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9672 struct block
*b0
, *b1
;
9673 struct slist
*s
, *s1
;
9675 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9677 /* Load the IP header length into A. */
9678 s
= gen_loadx_iphdrlen(cstate
);
9680 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9683 /* Forcibly append these statements to the true condition
9684 * of the protocol check by creating a new block that is
9685 * always true and ANDing them. */
9686 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9695 static struct block
*
9696 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9698 struct block
*b0
, *b1
;
9699 struct slist
*s
, *s1
;
9701 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9703 /* Load the IP header length. We need to account for a
9704 * variable length link prefix if there is one. */
9705 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9707 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9711 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9715 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9719 /* Forcibly append these statements to the true condition
9720 * of the protocol check by creating a new block that is
9721 * always true and ANDing them. */
9722 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9725 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9734 /* We need to store three values based on the VXLAN header:
9735 * - The offset of the linktype.
9736 * - The offset of the end of the VXLAN header.
9737 * - The offset of the end of the encapsulated MAC header. */
9738 static struct slist
*
9739 gen_vxlan_offsets(compiler_state_t
*cstate
)
9741 struct slist
*s
, *s1
;
9743 /* Calculate the offset of the VXLAN header itself. This
9744 * includes the IP header computed previously (including any
9745 * variable link prefix) and stored in A plus the fixed size
9746 * headers (fixed link prefix, MAC length, UDP header). */
9747 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9748 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9750 /* Add the VXLAN header length to its offset and store */
9751 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9755 /* Push the link header. VXLAN packets always contain Ethernet
9757 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9759 s1
= new_stmt(cstate
, BPF_ST
);
9760 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9763 /* As the payload is an Ethernet packet, we can use the
9764 * EtherType of the payload directly as the linktype. */
9765 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9769 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9770 cstate
->off_linktype
.is_variable
= 1;
9771 cstate
->off_linktype
.constant_part
= 0;
9773 s1
= new_stmt(cstate
, BPF_ST
);
9774 s1
->s
.k
= cstate
->off_linktype
.reg
;
9777 /* Two bytes further is the end of the Ethernet header and the
9778 * start of the payload. */
9779 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9783 /* Move the result to X. */
9784 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9787 /* Store the final result of our linkpl calculation. */
9788 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9789 cstate
->off_linkpl
.is_variable
= 1;
9790 cstate
->off_linkpl
.constant_part
= 0;
9792 s1
= new_stmt(cstate
, BPF_STX
);
9793 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9801 /* Check to see if this is a VXLAN packet. */
9803 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9805 struct block
*b0
, *b1
;
9809 * Catch errors reported by us and routines below us, and return NULL
9812 if (setjmp(cstate
->top_ctx
))
9815 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9816 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9821 /* Later filters should act on the payload of the VXLAN frame,
9822 * update all of the header pointers. Attach this code so that
9823 * it gets executed in the event that the VXLAN filter matches. */
9824 s
= gen_vxlan_offsets(cstate
);
9826 b1
= gen_true(cstate
);
9827 sappend(s
, b1
->stmts
);
9832 cstate
->is_encap
= 1;
9837 /* Check that the encapsulated frame has a link layer header
9838 * for Ethernet filters. */
9839 static struct block
*
9840 gen_encap_ll_check(compiler_state_t
*cstate
)
9843 struct slist
*s
, *s1
;
9845 /* The easiest way to see if there is a link layer present
9846 * is to check if the link layer header and payload are not
9849 /* Geneve always generates pure variable offsets so we can
9850 * compare only the registers. */
9851 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9852 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9854 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9855 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9858 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9866 static struct block
*
9867 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9868 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9873 * This check is a no-op for A_MSGTYPE so long as the only incoming
9874 * code path is from gen_atmmulti_abbrev(), which makes the same
9875 * check first; also for A_PROTOTYPE so long as the only incoming code
9876 * paths are from gen_atmtype_abbrev(), which makes the same check
9877 * first, or from gen_llc_internal() or gen_linktype(), which restrict
9880 assert_atm(cstate
, atmkw(atmfield
));
9885 assert_maxval(cstate
, "VPI", jvalue
, UINT8_MAX
);
9886 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9887 0xffffffffU
, jtype
, reverse
, jvalue
);
9891 assert_maxval(cstate
, "VCI", jvalue
, UINT16_MAX
);
9892 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9893 0xffffffffU
, jtype
, reverse
, jvalue
);
9897 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9898 0x0fU
, jtype
, reverse
, jvalue
);
9902 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9903 0xffffffffU
, jtype
, reverse
, jvalue
);
9912 static struct block
*
9913 gen_atmtype_metac(compiler_state_t
*cstate
)
9915 struct block
*b0
, *b1
;
9917 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9918 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9923 static struct block
*
9924 gen_atmtype_sc(compiler_state_t
*cstate
)
9926 struct block
*b0
, *b1
;
9928 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9929 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9934 static struct block
*
9935 gen_atmtype_llc(compiler_state_t
*cstate
)
9939 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9940 cstate
->linktype
= cstate
->prevlinktype
;
9945 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9946 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9949 * Catch errors reported by us and routines below us, and return NULL
9952 if (setjmp(cstate
->top_ctx
))
9955 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9960 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9962 struct block
*b0
, *b1
;
9965 * Catch errors reported by us and routines below us, and return NULL
9968 if (setjmp(cstate
->top_ctx
))
9971 assert_atm(cstate
, atmkw(type
));
9976 /* Get all packets in Meta signalling Circuit */
9977 b1
= gen_atmtype_metac(cstate
);
9981 /* Get all packets in Broadcast Circuit*/
9982 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9983 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9988 /* Get all cells in Segment OAM F4 circuit*/
9989 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9990 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9995 /* Get all cells in End-to-End OAM F4 Circuit*/
9996 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9997 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10002 /* Get all packets in connection Signalling Circuit */
10003 b1
= gen_atmtype_sc(cstate
);
10007 /* Get all packets in ILMI Circuit */
10008 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10009 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10014 /* Get all LANE packets */
10015 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10018 * Arrange that all subsequent tests assume LANE
10019 * rather than LLC-encapsulated packets, and set
10020 * the offsets appropriately for LANE-encapsulated
10023 * We assume LANE means Ethernet, not Token Ring.
10025 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10026 cstate
->off_payload
+ 2, /* Ethernet header */
10028 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10029 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10030 cstate
->off_nl
= 0; /* Ethernet II */
10031 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10041 * Filtering for MTP2 messages based on li value
10042 * FISU, length is null
10043 * LSSU, length is 1 or 2
10044 * MSU, length is 3 or more
10045 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10048 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10050 struct block
*b0
, *b1
;
10053 * Catch errors reported by us and routines below us, and return NULL
10056 if (setjmp(cstate
->top_ctx
))
10059 assert_ss7(cstate
, ss7kw(type
));
10064 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10065 0x3fU
, BPF_JEQ
, 0, 0U);
10069 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10070 0x3fU
, BPF_JGT
, 1, 2U);
10071 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10072 0x3fU
, BPF_JGT
, 0, 0U);
10077 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10078 0x3fU
, BPF_JGT
, 0, 2U);
10082 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10083 0xff80U
, BPF_JEQ
, 0, 0U);
10087 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10088 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10089 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10090 0xff80U
, BPF_JGT
, 0, 0U);
10095 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10096 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10106 * These maximum valid values are all-ones, so they double as the bitmasks
10107 * before any bitwise shifting.
10109 #define MTP2_SIO_MAXVAL UINT8_MAX
10110 #define MTP3_PC_MAXVAL 0x3fffU
10111 #define MTP3_SLS_MAXVAL 0xfU
10113 static struct block
*
10114 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10115 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10123 newoff_sio
= cstate
->off_sio
;
10124 newoff_opc
= cstate
->off_opc
;
10125 newoff_dpc
= cstate
->off_dpc
;
10126 newoff_sls
= cstate
->off_sls
;
10128 assert_ss7(cstate
, ss7kw(mtp3field
));
10130 switch (mtp3field
) {
10133 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10135 * SIO is the simplest field: the size is one byte and the offset is a
10136 * multiple of bytes, so the only detail to get right is the value of
10137 * the [right-to-left] field offset.
10140 newoff_sio
+= 3; /* offset for MTP2_HSL */
10144 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP2_SIO_MAXVAL
);
10145 // Here the bitmask means "do not apply a bitmask".
10146 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10147 jtype
, reverse
, jvalue
);
10151 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10153 * SLS, OPC and DPC are more complicated: none of these is sized in a
10154 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10155 * diagrams are meant to be read right-to-left. This means in the
10156 * diagrams within individual fields and concatenations thereof
10157 * bitwise shifts and masks can be noted in the common left-to-right
10158 * manner until each final value is ready to be byte-swapped and
10159 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10160 * similar problem in a similar way.
10162 * Offsets of fields within the packet header always have the
10163 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10164 * DLTs the offset does not include the F (Flag) field at the
10165 * beginning of each message.
10167 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10168 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10169 * be tested entirely using a single BPF_W comparison. In this case
10170 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10171 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10172 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10173 * correlates with the [RTL] packet diagram until the byte-swapping is
10176 * The code below uses this approach for OPC, which spans 3 bytes.
10177 * DPC and SLS use shorter loads, SLS also uses a different offset.
10184 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
10185 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10186 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10187 SWAPLONG(jvalue
<< 14));
10195 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
10196 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10197 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10198 SWAPSHORT(jvalue
));
10206 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_SLS_MAXVAL
);
10207 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10208 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10219 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10220 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10223 * Catch errors reported by us and routines below us, and return NULL
10226 if (setjmp(cstate
->top_ctx
))
10229 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10233 static struct block
*
10234 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10239 * Q.2931 signalling protocol messages for handling virtual circuits
10240 * establishment and teardown
10245 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10248 case A_CALLPROCEED
:
10249 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10253 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10257 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10261 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10264 case A_RELEASE_DONE
:
10265 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10275 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10277 struct block
*b0
, *b1
;
10280 * Catch errors reported by us and routines below us, and return NULL
10283 if (setjmp(cstate
->top_ctx
))
10286 assert_atm(cstate
, atmkw(type
));
10292 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10293 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10295 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10301 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10302 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10304 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10310 * Get Q.2931 signalling messages for switched
10311 * virtual connection
10313 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10314 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10316 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10318 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10320 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10322 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10324 b0
= gen_atmtype_sc(cstate
);
10328 case A_METACONNECT
:
10329 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10330 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10332 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10334 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10336 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10338 b0
= gen_atmtype_metac(cstate
);