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 #ifndef ISO8878A_CONS
259 #define ISO8878A_CONS 0x84
261 #ifndef ISO10747_IDRP
262 #define ISO10747_IDRP 0x85
265 // Same as in tcpdump/print-sl.c.
267 #define SLIPDIR_OUT 1
269 #ifdef HAVE_OS_PROTO_H
270 #include "os-proto.h"
273 #define JMP(c) ((c)|BPF_JMP|BPF_K)
276 * "Push" the current value of the link-layer header type and link-layer
277 * header offset onto a "stack", and set a new value. (It's not a
278 * full-blown stack; we keep only the top two items.)
280 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
282 (cs)->prevlinktype = (cs)->linktype; \
283 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
284 (cs)->linktype = (new_linktype); \
285 (cs)->off_linkhdr.is_variable = (new_is_variable); \
286 (cs)->off_linkhdr.constant_part = (new_constant_part); \
287 (cs)->off_linkhdr.reg = (new_reg); \
288 (cs)->is_encap = 0; \
292 * Offset "not set" value.
294 #define OFFSET_NOT_SET 0xffffffffU
297 * Absolute offsets, which are offsets from the beginning of the raw
298 * packet data, are, in the general case, the sum of a variable value
299 * and a constant value; the variable value may be absent, in which
300 * case the offset is only the constant value, and the constant value
301 * may be zero, in which case the offset is only the variable value.
303 * bpf_abs_offset is a structure containing all that information:
305 * is_variable is 1 if there's a variable part.
307 * constant_part is the constant part of the value, possibly zero;
309 * if is_variable is 1, reg is the register number for a register
310 * containing the variable value if the register has been assigned,
320 * Value passed to gen_load_a() to indicate what the offset argument
321 * is relative to the beginning of.
324 OR_PACKET
, /* full packet data */
325 OR_LINKHDR
, /* link-layer header */
326 OR_PREVLINKHDR
, /* previous link-layer header */
327 OR_LLC
, /* 802.2 LLC header */
328 OR_PREVMPLSHDR
, /* previous MPLS header */
329 OR_LINKTYPE
, /* link-layer type */
330 OR_LINKPL
, /* link-layer payload */
331 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
332 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
333 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
337 * We divvy out chunks of memory rather than call malloc each time so
338 * we don't have to worry about leaking memory. It's probably
339 * not a big deal if all this memory was wasted but if this ever
340 * goes into a library that would probably not be a good idea.
342 * XXX - this *is* in a library....
345 #define CHUNK0SIZE 1024
352 * A chunk can store any of:
353 * - a string (guaranteed alignment 1 but present for completeness)
357 * For this simple allocator every allocated chunk gets rounded up to the
358 * alignment needed for any chunk.
369 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
371 /* Code generator state */
373 struct _compiler_state
{
384 int outermostlinktype
;
389 /* Hack for handling VLAN and MPLS stacks. */
390 u_int label_stack_depth
;
391 u_int vlan_stack_depth
;
397 * As errors are handled by a longjmp, anything allocated must
398 * be freed in the longjmp handler, so it must be reachable
401 * One thing that's allocated is the result of pcap_nametoaddrinfo();
402 * it must be freed with freeaddrinfo(). This variable points to
403 * any addrinfo structure that would need to be freed.
408 * Another thing that's allocated is the result of pcap_ether_aton();
409 * it must be freed with free(). This variable points to any
410 * address that would need to be freed.
415 * Various code constructs need to know the layout of the packet.
416 * These values give the necessary offsets from the beginning
417 * of the packet data.
421 * Absolute offset of the beginning of the link-layer header.
423 bpf_abs_offset off_linkhdr
;
426 * If we're checking a link-layer header for a packet encapsulated
427 * in another protocol layer, this is the equivalent information
428 * for the previous layers' link-layer header from the beginning
429 * of the raw packet data.
431 bpf_abs_offset off_prevlinkhdr
;
434 * This is the equivalent information for the outermost layers'
437 bpf_abs_offset off_outermostlinkhdr
;
440 * Absolute offset of the beginning of the link-layer payload.
442 bpf_abs_offset off_linkpl
;
445 * "off_linktype" is the offset to information in the link-layer
446 * header giving the packet type. This is an absolute offset
447 * from the beginning of the packet.
449 * For Ethernet, it's the offset of the Ethernet type field; this
450 * means that it must have a value that skips VLAN tags.
452 * For link-layer types that always use 802.2 headers, it's the
453 * offset of the LLC header; this means that it must have a value
454 * that skips VLAN tags.
456 * For PPP, it's the offset of the PPP type field.
458 * For Cisco HDLC, it's the offset of the CHDLC type field.
460 * For BSD loopback, it's the offset of the AF_ value.
462 * For Linux cooked sockets, it's the offset of the type field.
464 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
465 * encapsulation, in which case, IP is assumed.
467 bpf_abs_offset off_linktype
;
470 * TRUE if the link layer includes an ATM pseudo-header.
474 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
475 * causes us to generate code that checks for a Geneve or
476 * VXLAN header respectively and assume that later filters
477 * apply to the encapsulated payload.
482 * TRUE if we need variable length part of VLAN offset
484 int is_vlan_vloffset
;
487 * These are offsets for the ATM pseudo-header.
494 * These are offsets for the MTP2 fields.
500 * These are offsets for the MTP3 fields.
508 * This is the offset of the first byte after the ATM pseudo_header,
509 * or -1 if there is no ATM pseudo-header.
514 * These are offsets to the beginning of the network-layer header.
515 * They are relative to the beginning of the link-layer payload
516 * (i.e., they don't include off_linkhdr.constant_part or
517 * off_linkpl.constant_part).
519 * If the link layer never uses 802.2 LLC:
521 * "off_nl" and "off_nl_nosnap" are the same.
523 * If the link layer always uses 802.2 LLC:
525 * "off_nl" is the offset if there's a SNAP header following
528 * "off_nl_nosnap" is the offset if there's no SNAP header.
530 * If the link layer is Ethernet:
532 * "off_nl" is the offset if the packet is an Ethernet II packet
533 * (we assume no 802.3+802.2+SNAP);
535 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
536 * with an 802.2 header following it.
542 * Here we handle simple allocation of the scratch registers.
543 * If too many registers are alloc'd, the allocator punts.
545 int regused
[BPF_MEMWORDS
];
551 struct chunk chunks
[NCHUNKS
];
556 * For use by routines outside this file.
560 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
565 * If we've already set an error, don't override it.
566 * The lexical analyzer reports some errors by setting
567 * the error and then returning a LEX_ERROR token, which
568 * is not recognized by any grammar rule, and thus forces
569 * the parse to stop. We don't want the error reported
570 * by the lexical analyzer to be overwritten by the syntax
573 if (!cstate
->error_set
) {
575 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
578 cstate
->error_set
= 1;
583 * For use *ONLY* in routines in this file.
585 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
586 PCAP_PRINTFLIKE(2, 3);
589 static void PCAP_NORETURN
590 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
595 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
598 longjmp(cstate
->top_ctx
, 1);
605 static int init_linktype(compiler_state_t
*, pcap_t
*);
607 static void init_regs(compiler_state_t
*);
608 static int alloc_reg(compiler_state_t
*);
609 static void free_reg(compiler_state_t
*, int);
611 static void initchunks(compiler_state_t
*cstate
);
612 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
613 static void *newchunk(compiler_state_t
*cstate
, size_t);
614 static void freechunks(compiler_state_t
*cstate
);
615 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
616 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
617 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
618 static inline void syntax(compiler_state_t
*cstate
);
620 static void backpatch(struct block
*, struct block
*);
621 static void merge(struct block
*, struct block
*);
622 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
624 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
626 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
628 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
630 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
632 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
633 u_int
, bpf_u_int32
, bpf_u_int32
);
634 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
635 u_int
, const u_char
*);
636 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
637 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
638 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
640 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
642 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
643 static struct block
*gen_uncond(compiler_state_t
*, int);
644 static inline struct block
*gen_true(compiler_state_t
*);
645 static inline struct block
*gen_false(compiler_state_t
*);
646 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
647 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
648 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
649 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
650 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
651 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
652 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
653 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
654 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
655 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
657 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
658 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
659 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
660 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
661 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
664 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
665 struct in6_addr
*, int, u_int
, u_int
);
667 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
668 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
669 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
670 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
671 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
672 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
673 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
674 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
675 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
678 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
679 struct in6_addr
*, int, int, int);
682 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
683 struct addrinfo
*, int, int);
685 static struct block
*gen_ipfrag(compiler_state_t
*);
686 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
687 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
689 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
690 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
692 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
693 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
694 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
696 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
697 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
698 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
699 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
701 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
702 static int lookup_proto(compiler_state_t
*, const char *, int);
703 #if !defined(NO_PROTOCHAIN)
704 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
705 #endif /* !defined(NO_PROTOCHAIN) */
706 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
707 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
708 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
709 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
710 static struct block
*gen_len(compiler_state_t
*, int, int);
711 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
713 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
714 bpf_u_int32
, int, int);
715 static struct block
*gen_atmtype_llc(compiler_state_t
*);
716 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
719 initchunks(compiler_state_t
*cstate
)
723 for (i
= 0; i
< NCHUNKS
; i
++) {
724 cstate
->chunks
[i
].n_left
= 0;
725 cstate
->chunks
[i
].m
= NULL
;
727 cstate
->cur_chunk
= 0;
731 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
737 /* Round up to chunk alignment. */
738 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
740 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
741 if (n
> cp
->n_left
) {
743 k
= ++cstate
->cur_chunk
;
745 bpf_set_error(cstate
, "out of memory");
748 size
= CHUNK0SIZE
<< k
;
749 cp
->m
= (void *)malloc(size
);
751 bpf_set_error(cstate
, "out of memory");
754 memset((char *)cp
->m
, 0, size
);
757 bpf_set_error(cstate
, "out of memory");
762 return (void *)((char *)cp
->m
+ cp
->n_left
);
766 newchunk(compiler_state_t
*cstate
, size_t n
)
770 p
= newchunk_nolongjmp(cstate
, n
);
772 longjmp(cstate
->top_ctx
, 1);
779 freechunks(compiler_state_t
*cstate
)
783 for (i
= 0; i
< NCHUNKS
; ++i
)
784 if (cstate
->chunks
[i
].m
!= NULL
)
785 free(cstate
->chunks
[i
].m
);
789 * A strdup whose allocations are freed after code generation is over.
790 * This is used by the lexical analyzer, so it can't longjmp; it just
791 * returns NULL on an allocation error, and the callers must check
795 sdup(compiler_state_t
*cstate
, const char *s
)
797 size_t n
= strlen(s
) + 1;
798 char *cp
= newchunk_nolongjmp(cstate
, n
);
802 pcapint_strlcpy(cp
, s
, n
);
806 static inline struct block
*
807 new_block(compiler_state_t
*cstate
, int code
)
811 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
818 static inline struct slist
*
819 new_stmt(compiler_state_t
*cstate
, int code
)
823 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
829 static struct block
*
830 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
832 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
838 static struct block
*
839 gen_retblk(compiler_state_t
*cstate
, int v
)
841 if (setjmp(cstate
->top_ctx
)) {
843 * gen_retblk() only fails because a memory
844 * allocation failed in newchunk(), meaning
845 * that it can't return a pointer.
851 return gen_retblk_internal(cstate
, v
);
854 static inline PCAP_NORETURN_DEF
void
855 syntax(compiler_state_t
*cstate
)
857 bpf_error(cstate
, "syntax error in filter expression");
861 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
862 const char *buf
, int optimize
, bpf_u_int32 mask
)
868 compiler_state_t cstate
;
869 yyscan_t scanner
= NULL
;
870 YY_BUFFER_STATE in_buffer
= NULL
;
875 * If this pcap_t hasn't been activated, it doesn't have a
876 * link-layer type, so we can't use it.
879 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
880 "not-yet-activated pcap_t passed to pcap_compile");
886 * Initialize Winsock, asking for the latest version (2.2),
887 * as we may be calling Winsock routines to translate
888 * host names to addresses.
890 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
892 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
893 err
, "Error calling WSAStartup()");
900 * If the device on which we're capturing need to be notified
901 * that a new filter is being compiled, do so.
903 * This allows them to save a copy of it, in case, for example,
904 * they're implementing a form of remote packet capture, and
905 * want the remote machine to filter out the packets in which
906 * it's sending the packets it's captured.
908 * XXX - the fact that we happen to be compiling a filter
909 * doesn't necessarily mean we'll be installing it as the
910 * filter for this pcap_t; we might be running it from userland
911 * on captured packets to do packet classification. We really
912 * need a better way of handling this, but this is all that
913 * the WinPcap remote capture code did.
915 if (p
->save_current_filter_op
!= NULL
)
916 (p
->save_current_filter_op
)(p
, buf
);
920 cstate
.no_optimize
= 0;
925 cstate
.ic
.root
= NULL
;
926 cstate
.ic
.cur_mark
= 0;
928 cstate
.error_set
= 0;
931 cstate
.netmask
= mask
;
933 cstate
.snaplen
= pcap_snapshot(p
);
934 if (cstate
.snaplen
== 0) {
935 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
936 "snaplen of 0 rejects all packets");
941 if (pcap_lex_init(&scanner
) != 0) {
942 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
943 errno
, "can't initialize scanner");
947 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
950 * Associate the compiler state with the lexical analyzer
953 pcap_set_extra(&cstate
, scanner
);
955 if (init_linktype(&cstate
, p
) == -1) {
959 if (pcap_parse(scanner
, &cstate
) != 0) {
961 if (cstate
.ai
!= NULL
)
962 freeaddrinfo(cstate
.ai
);
964 if (cstate
.e
!= NULL
)
970 if (cstate
.ic
.root
== NULL
) {
971 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
974 * Catch errors reported by gen_retblk().
976 if (cstate
.ic
.root
== NULL
) {
982 if (optimize
&& !cstate
.no_optimize
) {
983 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
988 if (cstate
.ic
.root
== NULL
||
989 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
990 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
991 "expression rejects all packets");
996 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
997 cstate
.ic
.root
, &len
, p
->errbuf
);
998 if (program
->bf_insns
== NULL
) {
1003 program
->bf_len
= len
;
1005 rc
= 0; /* We're all okay */
1009 * Clean up everything for the lexical analyzer.
1011 if (in_buffer
!= NULL
)
1012 pcap__delete_buffer(in_buffer
, scanner
);
1013 if (scanner
!= NULL
)
1014 pcap_lex_destroy(scanner
);
1017 * Clean up our own allocated memory.
1019 freechunks(&cstate
);
1029 * entry point for using the compiler with no pcap open
1030 * pass in all the stuff that is needed explicitly instead.
1033 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1034 struct bpf_program
*program
,
1035 const char *buf
, int optimize
, bpf_u_int32 mask
)
1040 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1042 return (PCAP_ERROR
);
1043 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1049 * Clean up a "struct bpf_program" by freeing all the memory allocated
1053 pcap_freecode(struct bpf_program
*program
)
1055 program
->bf_len
= 0;
1056 if (program
->bf_insns
!= NULL
) {
1057 free((char *)program
->bf_insns
);
1058 program
->bf_insns
= NULL
;
1063 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1064 * which of the jt and jf fields has been resolved and which is a pointer
1065 * back to another unresolved block (or nil). At least one of the fields
1066 * in each block is already resolved.
1069 backpatch(struct block
*list
, struct block
*target
)
1086 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1087 * which of jt and jf is the link.
1090 merge(struct block
*b0
, struct block
*b1
)
1092 register struct block
**p
= &b0
;
1094 /* Find end of list. */
1096 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1098 /* Concatenate the lists. */
1103 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1106 * Catch errors reported by us and routines below us, and return -1
1109 if (setjmp(cstate
->top_ctx
))
1113 * Insert before the statements of the first (root) block any
1114 * statements needed to load the lengths of any variable-length
1115 * headers into registers.
1117 * XXX - a fancier strategy would be to insert those before the
1118 * statements of all blocks that use those lengths and that
1119 * have no predecessors that use them, so that we only compute
1120 * the lengths if we need them. There might be even better
1121 * approaches than that.
1123 * However, those strategies would be more complicated, and
1124 * as we don't generate code to compute a length if the
1125 * program has no tests that use the length, and as most
1126 * tests will probably use those lengths, we would just
1127 * postpone computing the lengths so that it's not done
1128 * for tests that fail early, and it's not clear that's
1131 insert_compute_vloffsets(cstate
, p
->head
);
1134 * For DLT_PPI captures, generate a check of the per-packet
1135 * DLT value to make sure it's DLT_IEEE802_11.
1137 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1138 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1139 * with appropriate Ethernet information and use that rather
1140 * than using something such as DLT_PPI where you don't know
1141 * the link-layer header type until runtime, which, in the
1142 * general case, would force us to generate both Ethernet *and*
1143 * 802.11 code (*and* anything else for which PPI is used)
1144 * and choose between them early in the BPF program?
1146 if (cstate
->linktype
== DLT_PPI
) {
1147 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1148 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1149 gen_and(ppi_dlt_check
, p
);
1152 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1153 p
->sense
= !p
->sense
;
1154 backpatch(p
, gen_retblk_internal(cstate
, 0));
1155 cstate
->ic
.root
= p
->head
;
1160 gen_and(struct block
*b0
, struct block
*b1
)
1162 backpatch(b0
, b1
->head
);
1163 b0
->sense
= !b0
->sense
;
1164 b1
->sense
= !b1
->sense
;
1166 b1
->sense
= !b1
->sense
;
1167 b1
->head
= b0
->head
;
1171 gen_or(struct block
*b0
, struct block
*b1
)
1173 b0
->sense
= !b0
->sense
;
1174 backpatch(b0
, b1
->head
);
1175 b0
->sense
= !b0
->sense
;
1177 b1
->head
= b0
->head
;
1181 gen_not(struct block
*b
)
1183 b
->sense
= !b
->sense
;
1186 static struct block
*
1187 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1188 u_int size
, bpf_u_int32 v
)
1190 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1193 static struct block
*
1194 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1195 u_int size
, bpf_u_int32 v
)
1197 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1200 static struct block
*
1201 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1202 u_int size
, bpf_u_int32 v
)
1204 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1207 static struct block
*
1208 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1209 u_int size
, bpf_u_int32 v
)
1211 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1214 static struct block
*
1215 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1216 u_int size
, bpf_u_int32 v
)
1218 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1221 static struct block
*
1222 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1223 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1225 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1228 static struct block
*
1229 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1230 u_int size
, const u_char
*v
)
1232 register struct block
*b
, *tmp
;
1236 register const u_char
*p
= &v
[size
- 4];
1238 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1246 register const u_char
*p
= &v
[size
- 2];
1248 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1256 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1265 * AND the field of size "size" at offset "offset" relative to the header
1266 * specified by "offrel" with "mask", and compare it with the value "v"
1267 * with the test specified by "jtype"; if "reverse" is true, the test
1268 * should test the opposite of "jtype".
1270 static struct block
*
1271 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1272 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1275 struct slist
*s
, *s2
;
1278 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1280 if (mask
!= 0xffffffff) {
1281 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1286 b
= new_block(cstate
, JMP(jtype
));
1295 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1297 cstate
->pcap_fddipad
= p
->fddipad
;
1300 * We start out with only one link-layer header.
1302 cstate
->outermostlinktype
= pcap_datalink(p
);
1303 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1304 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1305 cstate
->off_outermostlinkhdr
.reg
= -1;
1307 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1308 cstate
->off_prevlinkhdr
.constant_part
= 0;
1309 cstate
->off_prevlinkhdr
.is_variable
= 0;
1310 cstate
->off_prevlinkhdr
.reg
= -1;
1312 cstate
->linktype
= cstate
->outermostlinktype
;
1313 cstate
->off_linkhdr
.constant_part
= 0;
1314 cstate
->off_linkhdr
.is_variable
= 0;
1315 cstate
->off_linkhdr
.reg
= -1;
1320 cstate
->off_linkpl
.constant_part
= 0;
1321 cstate
->off_linkpl
.is_variable
= 0;
1322 cstate
->off_linkpl
.reg
= -1;
1324 cstate
->off_linktype
.constant_part
= 0;
1325 cstate
->off_linktype
.is_variable
= 0;
1326 cstate
->off_linktype
.reg
= -1;
1329 * Assume it's not raw ATM with a pseudo-header, for now.
1332 cstate
->off_vpi
= OFFSET_NOT_SET
;
1333 cstate
->off_vci
= OFFSET_NOT_SET
;
1334 cstate
->off_proto
= OFFSET_NOT_SET
;
1335 cstate
->off_payload
= OFFSET_NOT_SET
;
1338 * And not encapsulated with either Geneve or VXLAN.
1340 cstate
->is_encap
= 0;
1343 * No variable length VLAN offset by default
1345 cstate
->is_vlan_vloffset
= 0;
1348 * And assume we're not doing SS7.
1350 cstate
->off_li
= OFFSET_NOT_SET
;
1351 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1352 cstate
->off_sio
= OFFSET_NOT_SET
;
1353 cstate
->off_opc
= OFFSET_NOT_SET
;
1354 cstate
->off_dpc
= OFFSET_NOT_SET
;
1355 cstate
->off_sls
= OFFSET_NOT_SET
;
1357 cstate
->label_stack_depth
= 0;
1358 cstate
->vlan_stack_depth
= 0;
1360 switch (cstate
->linktype
) {
1363 cstate
->off_linktype
.constant_part
= 2;
1364 cstate
->off_linkpl
.constant_part
= 6;
1365 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1366 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1369 case DLT_ARCNET_LINUX
:
1370 cstate
->off_linktype
.constant_part
= 4;
1371 cstate
->off_linkpl
.constant_part
= 8;
1372 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1373 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1377 cstate
->off_linktype
.constant_part
= 12;
1378 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1379 cstate
->off_nl
= 0; /* Ethernet II */
1380 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1385 * SLIP doesn't have a link level type. The 16 byte
1386 * header is hacked into our SLIP driver.
1388 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1389 cstate
->off_linkpl
.constant_part
= 16;
1391 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1394 case DLT_SLIP_BSDOS
:
1395 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1396 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1398 cstate
->off_linkpl
.constant_part
= 24;
1400 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1405 cstate
->off_linktype
.constant_part
= 0;
1406 cstate
->off_linkpl
.constant_part
= 4;
1408 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1412 cstate
->off_linktype
.constant_part
= 0;
1413 cstate
->off_linkpl
.constant_part
= 12;
1415 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1420 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1421 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1422 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1423 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1424 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1426 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1431 * This does not include the Ethernet header, and
1432 * only covers session state.
1434 cstate
->off_linktype
.constant_part
= 6;
1435 cstate
->off_linkpl
.constant_part
= 8;
1437 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1441 cstate
->off_linktype
.constant_part
= 5;
1442 cstate
->off_linkpl
.constant_part
= 24;
1444 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1449 * FDDI doesn't really have a link-level type field.
1450 * We set "off_linktype" to the offset of the LLC header.
1452 * To check for Ethernet types, we assume that SSAP = SNAP
1453 * is being used and pick out the encapsulated Ethernet type.
1454 * XXX - should we generate code to check for SNAP?
1456 cstate
->off_linktype
.constant_part
= 13;
1457 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1458 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1459 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1460 cstate
->off_nl
= 8; /* 802.2+SNAP */
1461 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1466 * Token Ring doesn't really have a link-level type field.
1467 * We set "off_linktype" to the offset of the LLC header.
1469 * To check for Ethernet types, we assume that SSAP = SNAP
1470 * is being used and pick out the encapsulated Ethernet type.
1471 * XXX - should we generate code to check for SNAP?
1473 * XXX - the header is actually variable-length.
1474 * Some various Linux patched versions gave 38
1475 * as "off_linktype" and 40 as "off_nl"; however,
1476 * if a token ring packet has *no* routing
1477 * information, i.e. is not source-routed, the correct
1478 * values are 20 and 22, as they are in the vanilla code.
1480 * A packet is source-routed iff the uppermost bit
1481 * of the first byte of the source address, at an
1482 * offset of 8, has the uppermost bit set. If the
1483 * packet is source-routed, the total number of bytes
1484 * of routing information is 2 plus bits 0x1F00 of
1485 * the 16-bit value at an offset of 14 (shifted right
1486 * 8 - figure out which byte that is).
1488 cstate
->off_linktype
.constant_part
= 14;
1489 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1490 cstate
->off_nl
= 8; /* 802.2+SNAP */
1491 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1494 case DLT_PRISM_HEADER
:
1495 case DLT_IEEE802_11_RADIO_AVS
:
1496 case DLT_IEEE802_11_RADIO
:
1497 cstate
->off_linkhdr
.is_variable
= 1;
1498 /* Fall through, 802.11 doesn't have a variable link
1499 * prefix but is otherwise the same. */
1502 case DLT_IEEE802_11
:
1504 * 802.11 doesn't really have a link-level type field.
1505 * We set "off_linktype.constant_part" to the offset of
1508 * To check for Ethernet types, we assume that SSAP = SNAP
1509 * is being used and pick out the encapsulated Ethernet type.
1510 * XXX - should we generate code to check for SNAP?
1512 * We also handle variable-length radio headers here.
1513 * The Prism header is in theory variable-length, but in
1514 * practice it's always 144 bytes long. However, some
1515 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1516 * sometimes or always supply an AVS header, so we
1517 * have to check whether the radio header is a Prism
1518 * header or an AVS header, so, in practice, it's
1521 cstate
->off_linktype
.constant_part
= 24;
1522 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1523 cstate
->off_linkpl
.is_variable
= 1;
1524 cstate
->off_nl
= 8; /* 802.2+SNAP */
1525 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1530 * At the moment we treat PPI the same way that we treat
1531 * normal Radiotap encoded packets. The difference is in
1532 * the function that generates the code at the beginning
1533 * to compute the header length. Since this code generator
1534 * of PPI supports bare 802.11 encapsulation only (i.e.
1535 * the encapsulated DLT should be DLT_IEEE802_11) we
1536 * generate code to check for this too.
1538 cstate
->off_linktype
.constant_part
= 24;
1539 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1540 cstate
->off_linkpl
.is_variable
= 1;
1541 cstate
->off_linkhdr
.is_variable
= 1;
1542 cstate
->off_nl
= 8; /* 802.2+SNAP */
1543 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1546 case DLT_ATM_RFC1483
:
1547 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1549 * assume routed, non-ISO PDUs
1550 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1552 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1553 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1554 * latter would presumably be treated the way PPPoE
1555 * should be, so you can do "pppoe and udp port 2049"
1556 * or "pppoa and tcp port 80" and have it check for
1557 * PPPo{A,E} and a PPP protocol of IP and....
1559 cstate
->off_linktype
.constant_part
= 0;
1560 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1561 cstate
->off_nl
= 8; /* 802.2+SNAP */
1562 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1567 * Full Frontal ATM; you get AALn PDUs with an ATM
1571 cstate
->off_vpi
= SUNATM_VPI_POS
;
1572 cstate
->off_vci
= SUNATM_VCI_POS
;
1573 cstate
->off_proto
= PROTO_POS
;
1574 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1575 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1576 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1577 cstate
->off_nl
= 8; /* 802.2+SNAP */
1578 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1584 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1585 cstate
->off_linkpl
.constant_part
= 0;
1587 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1590 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1591 cstate
->off_linktype
.constant_part
= 14;
1592 cstate
->off_linkpl
.constant_part
= 16;
1594 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1597 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1598 cstate
->off_linktype
.constant_part
= 0;
1599 cstate
->off_linkpl
.constant_part
= 20;
1601 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1606 * LocalTalk does have a 1-byte type field in the LLAP header,
1607 * but really it just indicates whether there is a "short" or
1608 * "long" DDP packet following.
1610 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1611 cstate
->off_linkpl
.constant_part
= 0;
1613 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1616 case DLT_IP_OVER_FC
:
1618 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1619 * link-level type field. We set "off_linktype" to the
1620 * offset of the LLC header.
1622 * To check for Ethernet types, we assume that SSAP = SNAP
1623 * is being used and pick out the encapsulated Ethernet type.
1624 * XXX - should we generate code to check for SNAP? RFC
1625 * 2625 says SNAP should be used.
1627 cstate
->off_linktype
.constant_part
= 16;
1628 cstate
->off_linkpl
.constant_part
= 16;
1629 cstate
->off_nl
= 8; /* 802.2+SNAP */
1630 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1635 * XXX - we should set this to handle SNAP-encapsulated
1636 * frames (NLPID of 0x80).
1638 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1639 cstate
->off_linkpl
.constant_part
= 0;
1641 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1645 * the only BPF-interesting FRF.16 frames are non-control frames;
1646 * Frame Relay has a variable length link-layer
1647 * so lets start with offset 4 for now and increments later on (FIXME);
1650 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1651 cstate
->off_linkpl
.constant_part
= 0;
1653 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1656 case DLT_APPLE_IP_OVER_IEEE1394
:
1657 cstate
->off_linktype
.constant_part
= 16;
1658 cstate
->off_linkpl
.constant_part
= 18;
1660 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1663 case DLT_SYMANTEC_FIREWALL
:
1664 cstate
->off_linktype
.constant_part
= 6;
1665 cstate
->off_linkpl
.constant_part
= 44;
1666 cstate
->off_nl
= 0; /* Ethernet II */
1667 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1671 cstate
->off_linktype
.constant_part
= 0;
1672 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1673 cstate
->off_linkpl
.is_variable
= 1;
1675 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1678 case DLT_JUNIPER_MFR
:
1679 case DLT_JUNIPER_MLFR
:
1680 case DLT_JUNIPER_MLPPP
:
1681 case DLT_JUNIPER_PPP
:
1682 case DLT_JUNIPER_CHDLC
:
1683 case DLT_JUNIPER_FRELAY
:
1684 cstate
->off_linktype
.constant_part
= 4;
1685 cstate
->off_linkpl
.constant_part
= 4;
1687 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1690 case DLT_JUNIPER_ATM1
:
1691 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1692 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1694 cstate
->off_nl_nosnap
= 10;
1697 case DLT_JUNIPER_ATM2
:
1698 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1699 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1701 cstate
->off_nl_nosnap
= 10;
1704 /* frames captured on a Juniper PPPoE service PIC
1705 * contain raw ethernet frames */
1706 case DLT_JUNIPER_PPPOE
:
1707 case DLT_JUNIPER_ETHER
:
1708 cstate
->off_linkpl
.constant_part
= 14;
1709 cstate
->off_linktype
.constant_part
= 16;
1710 cstate
->off_nl
= 18; /* Ethernet II */
1711 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1714 case DLT_JUNIPER_PPPOE_ATM
:
1715 cstate
->off_linktype
.constant_part
= 4;
1716 cstate
->off_linkpl
.constant_part
= 6;
1718 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1721 case DLT_JUNIPER_GGSN
:
1722 cstate
->off_linktype
.constant_part
= 6;
1723 cstate
->off_linkpl
.constant_part
= 12;
1725 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1728 case DLT_JUNIPER_ES
:
1729 cstate
->off_linktype
.constant_part
= 6;
1730 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1731 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1732 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1735 case DLT_JUNIPER_MONITOR
:
1736 cstate
->off_linktype
.constant_part
= 12;
1737 cstate
->off_linkpl
.constant_part
= 12;
1738 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1739 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1742 case DLT_BACNET_MS_TP
:
1743 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1744 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1745 cstate
->off_nl
= OFFSET_NOT_SET
;
1746 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1749 case DLT_JUNIPER_SERVICES
:
1750 cstate
->off_linktype
.constant_part
= 12;
1751 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1752 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1753 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1756 case DLT_JUNIPER_VP
:
1757 cstate
->off_linktype
.constant_part
= 18;
1758 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1759 cstate
->off_nl
= OFFSET_NOT_SET
;
1760 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1763 case DLT_JUNIPER_ST
:
1764 cstate
->off_linktype
.constant_part
= 18;
1765 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1766 cstate
->off_nl
= OFFSET_NOT_SET
;
1767 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1770 case DLT_JUNIPER_ISM
:
1771 cstate
->off_linktype
.constant_part
= 8;
1772 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1773 cstate
->off_nl
= OFFSET_NOT_SET
;
1774 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1777 case DLT_JUNIPER_VS
:
1778 case DLT_JUNIPER_SRX_E2E
:
1779 case DLT_JUNIPER_FIBRECHANNEL
:
1780 case DLT_JUNIPER_ATM_CEMIC
:
1781 cstate
->off_linktype
.constant_part
= 8;
1782 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1783 cstate
->off_nl
= OFFSET_NOT_SET
;
1784 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1789 cstate
->off_li_hsl
= 4;
1790 cstate
->off_sio
= 3;
1791 cstate
->off_opc
= 4;
1792 cstate
->off_dpc
= 4;
1793 cstate
->off_sls
= 7;
1794 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1795 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1796 cstate
->off_nl
= OFFSET_NOT_SET
;
1797 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1800 case DLT_MTP2_WITH_PHDR
:
1802 cstate
->off_li_hsl
= 8;
1803 cstate
->off_sio
= 7;
1804 cstate
->off_opc
= 8;
1805 cstate
->off_dpc
= 8;
1806 cstate
->off_sls
= 11;
1807 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1808 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1809 cstate
->off_nl
= OFFSET_NOT_SET
;
1810 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1814 cstate
->off_li
= 22;
1815 cstate
->off_li_hsl
= 24;
1816 cstate
->off_sio
= 23;
1817 cstate
->off_opc
= 24;
1818 cstate
->off_dpc
= 24;
1819 cstate
->off_sls
= 27;
1820 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1821 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1822 cstate
->off_nl
= OFFSET_NOT_SET
;
1823 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1827 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1828 cstate
->off_linkpl
.constant_part
= 4;
1830 cstate
->off_nl_nosnap
= 0;
1835 * Currently, only raw "link[N:M]" filtering is supported.
1837 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1838 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1839 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1840 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1844 cstate
->off_linktype
.constant_part
= 1;
1845 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1847 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1850 case DLT_NETANALYZER
:
1851 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1852 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1853 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1854 cstate
->off_nl
= 0; /* Ethernet II */
1855 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1858 case DLT_NETANALYZER_TRANSPARENT
:
1859 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1860 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1861 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1862 cstate
->off_nl
= 0; /* Ethernet II */
1863 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1868 * For values in the range in which we've assigned new
1869 * DLT_ values, only raw "link[N:M]" filtering is supported.
1871 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1872 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1873 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1874 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1875 cstate
->off_nl
= OFFSET_NOT_SET
;
1876 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1878 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1879 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1885 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1890 * Load a value relative to the specified absolute offset.
1892 static struct slist
*
1893 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1894 u_int offset
, u_int size
)
1896 struct slist
*s
, *s2
;
1898 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1901 * If "s" is non-null, it has code to arrange that the X register
1902 * contains the variable part of the absolute offset, so we
1903 * generate a load relative to that, with an offset of
1904 * abs_offset->constant_part + offset.
1906 * Otherwise, we can do an absolute load with an offset of
1907 * abs_offset->constant_part + offset.
1911 * "s" points to a list of statements that puts the
1912 * variable part of the absolute offset into the X register.
1913 * Do an indirect load, to use the X register as an offset.
1915 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1916 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1920 * There is no variable part of the absolute offset, so
1921 * just do an absolute load.
1923 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1924 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1930 * Load a value relative to the beginning of the specified header.
1932 static struct slist
*
1933 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1936 struct slist
*s
, *s2
;
1939 * Squelch warnings from compilers that *don't* assume that
1940 * offrel always has a valid enum value and therefore don't
1941 * assume that we'll always go through one of the case arms.
1943 * If we have a default case, compilers that *do* assume that
1944 * will then complain about the default case code being
1947 * Damned if you do, damned if you don't.
1954 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1959 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1962 case OR_PREVLINKHDR
:
1963 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1967 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1970 case OR_PREVMPLSHDR
:
1971 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1975 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1978 case OR_LINKPL_NOSNAP
:
1979 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1983 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1988 * Load the X register with the length of the IPv4 header
1989 * (plus the offset of the link-layer header, if it's
1990 * preceded by a variable-length header such as a radio
1991 * header), in bytes.
1993 s
= gen_loadx_iphdrlen(cstate
);
1996 * Load the item at {offset of the link-layer payload} +
1997 * {offset, relative to the start of the link-layer
1998 * payload, of the IPv4 header} + {length of the IPv4 header} +
1999 * {specified offset}.
2001 * If the offset of the link-layer payload is variable,
2002 * the variable part of that offset is included in the
2003 * value in the X register, and we include the constant
2004 * part in the offset of the load.
2006 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2007 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2012 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2019 * Generate code to load into the X register the sum of the length of
2020 * the IPv4 header and the variable part of the offset of the link-layer
2023 static struct slist
*
2024 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2026 struct slist
*s
, *s2
;
2028 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2031 * The offset of the link-layer payload has a variable
2032 * part. "s" points to a list of statements that put
2033 * the variable part of that offset into the X register.
2035 * The 4*([k]&0xf) addressing mode can't be used, as we
2036 * don't have a constant offset, so we have to load the
2037 * value in question into the A register and add to it
2038 * the value from the X register.
2040 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2041 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2043 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2046 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2051 * The A register now contains the length of the IP header.
2052 * We need to add to it the variable part of the offset of
2053 * the link-layer payload, which is still in the X
2054 * register, and move the result into the X register.
2056 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2057 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2060 * The offset of the link-layer payload is a constant,
2061 * so no code was generated to load the (nonexistent)
2062 * variable part of that offset.
2064 * This means we can use the 4*([k]&0xf) addressing
2065 * mode. Load the length of the IPv4 header, which
2066 * is at an offset of cstate->off_nl from the beginning of
2067 * the link-layer payload, and thus at an offset of
2068 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2069 * of the raw packet data, using that addressing mode.
2071 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2072 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2078 static struct block
*
2079 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2084 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2086 b
= new_block(cstate
, JMP(BPF_JEQ
));
2092 static inline struct block
*
2093 gen_true(compiler_state_t
*cstate
)
2095 return gen_uncond(cstate
, 1);
2098 static inline struct block
*
2099 gen_false(compiler_state_t
*cstate
)
2101 return gen_uncond(cstate
, 0);
2105 * Generate code to match a particular packet type.
2107 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2108 * value, if <= ETHERMTU. We use that to determine whether to
2109 * match the type/length field or to check the type/length field for
2110 * a value <= ETHERMTU to see whether it's a type field and then do
2111 * the appropriate test.
2113 static struct block
*
2114 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2116 struct block
*b0
, *b1
;
2122 case LLCSAP_NETBEUI
:
2124 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2125 * so we check the DSAP and SSAP.
2127 * LLCSAP_IP checks for IP-over-802.2, rather
2128 * than IP-over-Ethernet or IP-over-SNAP.
2130 * XXX - should we check both the DSAP and the
2131 * SSAP, like this, or should we check just the
2132 * DSAP, as we do for other types <= ETHERMTU
2133 * (i.e., other SAP values)?
2135 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2136 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2144 * Ethernet_II frames, which are Ethernet
2145 * frames with a frame type of ETHERTYPE_IPX;
2147 * Ethernet_802.3 frames, which are 802.3
2148 * frames (i.e., the type/length field is
2149 * a length field, <= ETHERMTU, rather than
2150 * a type field) with the first two bytes
2151 * after the Ethernet/802.3 header being
2154 * Ethernet_802.2 frames, which are 802.3
2155 * frames with an 802.2 LLC header and
2156 * with the IPX LSAP as the DSAP in the LLC
2159 * Ethernet_SNAP frames, which are 802.3
2160 * frames with an LLC header and a SNAP
2161 * header and with an OUI of 0x000000
2162 * (encapsulated Ethernet) and a protocol
2163 * ID of ETHERTYPE_IPX in the SNAP header.
2165 * XXX - should we generate the same code both
2166 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2170 * This generates code to check both for the
2171 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2173 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2174 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2178 * Now we add code to check for SNAP frames with
2179 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2181 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2185 * Now we generate code to check for 802.3
2186 * frames in general.
2188 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2191 * Now add the check for 802.3 frames before the
2192 * check for Ethernet_802.2 and Ethernet_802.3,
2193 * as those checks should only be done on 802.3
2194 * frames, not on Ethernet frames.
2199 * Now add the check for Ethernet_II frames, and
2200 * do that before checking for the other frame
2203 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2207 case ETHERTYPE_ATALK
:
2208 case ETHERTYPE_AARP
:
2210 * EtherTalk (AppleTalk protocols on Ethernet link
2211 * layer) may use 802.2 encapsulation.
2215 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2216 * we check for an Ethernet type field less or equal than
2217 * 1500, which means it's an 802.3 length field.
2219 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2222 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2223 * SNAP packets with an organization code of
2224 * 0x080007 (Apple, for Appletalk) and a protocol
2225 * type of ETHERTYPE_ATALK (Appletalk).
2227 * 802.2-encapsulated ETHERTYPE_AARP packets are
2228 * SNAP packets with an organization code of
2229 * 0x000000 (encapsulated Ethernet) and a protocol
2230 * type of ETHERTYPE_AARP (Appletalk ARP).
2232 if (ll_proto
== ETHERTYPE_ATALK
)
2233 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2234 else /* ll_proto == ETHERTYPE_AARP */
2235 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2239 * Check for Ethernet encapsulation (Ethertalk
2240 * phase 1?); we just check for the Ethernet
2243 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2249 if (ll_proto
<= ETHERMTU
) {
2251 * This is an LLC SAP value, so the frames
2252 * that match would be 802.2 frames.
2253 * Check that the frame is an 802.2 frame
2254 * (i.e., that the length/type field is
2255 * a length field, <= ETHERMTU) and
2256 * then check the DSAP.
2258 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2259 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2264 * This is an Ethernet type, so compare
2265 * the length/type field with it (if
2266 * the frame is an 802.2 frame, the length
2267 * field will be <= ETHERMTU, and, as
2268 * "ll_proto" is > ETHERMTU, this test
2269 * will fail and the frame won't match,
2270 * which is what we want).
2272 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2277 static struct block
*
2278 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2281 * For DLT_NULL, the link-layer header is a 32-bit word
2282 * containing an AF_ value in *host* byte order, and for
2283 * DLT_ENC, the link-layer header begins with a 32-bit
2284 * word containing an AF_ value in host byte order.
2286 * In addition, if we're reading a saved capture file,
2287 * the host byte order in the capture may not be the
2288 * same as the host byte order on this machine.
2290 * For DLT_LOOP, the link-layer header is a 32-bit
2291 * word containing an AF_ value in *network* byte order.
2293 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2295 * The AF_ value is in host byte order, but the BPF
2296 * interpreter will convert it to network byte order.
2298 * If this is a save file, and it's from a machine
2299 * with the opposite byte order to ours, we byte-swap
2302 * Then we run it through "htonl()", and generate
2303 * code to compare against the result.
2305 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2306 ll_proto
= SWAPLONG(ll_proto
);
2307 ll_proto
= htonl(ll_proto
);
2309 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2313 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2314 * or IPv6 then we have an error.
2316 static struct block
*
2317 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2322 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2325 case ETHERTYPE_IPV6
:
2326 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2333 return gen_false(cstate
);
2337 * Generate code to match a particular packet type.
2339 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2340 * value, if <= ETHERMTU. We use that to determine whether to
2341 * match the type field or to check the type field for the special
2342 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2344 static struct block
*
2345 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2347 struct block
*b0
, *b1
;
2353 case LLCSAP_NETBEUI
:
2355 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2356 * so we check the DSAP and SSAP.
2358 * LLCSAP_IP checks for IP-over-802.2, rather
2359 * than IP-over-Ethernet or IP-over-SNAP.
2361 * XXX - should we check both the DSAP and the
2362 * SSAP, like this, or should we check just the
2363 * DSAP, as we do for other types <= ETHERMTU
2364 * (i.e., other SAP values)?
2366 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2367 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2373 * Ethernet_II frames, which are Ethernet
2374 * frames with a frame type of ETHERTYPE_IPX;
2376 * Ethernet_802.3 frames, which have a frame
2377 * type of LINUX_SLL_P_802_3;
2379 * Ethernet_802.2 frames, which are 802.3
2380 * frames with an 802.2 LLC header (i.e, have
2381 * a frame type of LINUX_SLL_P_802_2) and
2382 * with the IPX LSAP as the DSAP in the LLC
2385 * Ethernet_SNAP frames, which are 802.3
2386 * frames with an LLC header and a SNAP
2387 * header and with an OUI of 0x000000
2388 * (encapsulated Ethernet) and a protocol
2389 * ID of ETHERTYPE_IPX in the SNAP header.
2391 * First, do the checks on LINUX_SLL_P_802_2
2392 * frames; generate the check for either
2393 * Ethernet_802.2 or Ethernet_SNAP frames, and
2394 * then put a check for LINUX_SLL_P_802_2 frames
2397 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2398 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2400 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2404 * Now check for 802.3 frames and OR that with
2405 * the previous test.
2407 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2411 * Now add the check for Ethernet_II frames, and
2412 * do that before checking for the other frame
2415 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2419 case ETHERTYPE_ATALK
:
2420 case ETHERTYPE_AARP
:
2422 * EtherTalk (AppleTalk protocols on Ethernet link
2423 * layer) may use 802.2 encapsulation.
2427 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2428 * we check for the 802.2 protocol type in the
2429 * "Ethernet type" field.
2431 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2434 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2435 * SNAP packets with an organization code of
2436 * 0x080007 (Apple, for Appletalk) and a protocol
2437 * type of ETHERTYPE_ATALK (Appletalk).
2439 * 802.2-encapsulated ETHERTYPE_AARP packets are
2440 * SNAP packets with an organization code of
2441 * 0x000000 (encapsulated Ethernet) and a protocol
2442 * type of ETHERTYPE_AARP (Appletalk ARP).
2444 if (ll_proto
== ETHERTYPE_ATALK
)
2445 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2446 else /* ll_proto == ETHERTYPE_AARP */
2447 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2451 * Check for Ethernet encapsulation (Ethertalk
2452 * phase 1?); we just check for the Ethernet
2455 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2461 if (ll_proto
<= ETHERMTU
) {
2463 * This is an LLC SAP value, so the frames
2464 * that match would be 802.2 frames.
2465 * Check for the 802.2 protocol type
2466 * in the "Ethernet type" field, and
2467 * then check the DSAP.
2469 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2470 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2476 * This is an Ethernet type, so compare
2477 * the length/type field with it (if
2478 * the frame is an 802.2 frame, the length
2479 * field will be <= ETHERMTU, and, as
2480 * "ll_proto" is > ETHERMTU, this test
2481 * will fail and the frame won't match,
2482 * which is what we want).
2484 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2490 * Load a value relative to the beginning of the link-layer header after the
2493 static struct slist
*
2494 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2496 struct slist
*s1
, *s2
;
2499 * Generate code to load the length of the pflog header into
2500 * the register assigned to hold that length, if one has been
2501 * assigned. (If one hasn't been assigned, no code we've
2502 * generated uses that prefix, so we don't need to generate any
2505 if (cstate
->off_linkpl
.reg
!= -1) {
2507 * The length is in the first byte of the header.
2509 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2513 * Round it up to a multiple of 4.
2514 * Add 3, and clear the lower 2 bits.
2516 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2519 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2520 s2
->s
.k
= 0xfffffffc;
2524 * Now allocate a register to hold that value and store
2527 s2
= new_stmt(cstate
, BPF_ST
);
2528 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2532 * Now move it into the X register.
2534 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2542 static struct slist
*
2543 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2545 struct slist
*s1
, *s2
;
2546 struct slist
*sjeq_avs_cookie
;
2547 struct slist
*sjcommon
;
2550 * This code is not compatible with the optimizer, as
2551 * we are generating jmp instructions within a normal
2552 * slist of instructions
2554 cstate
->no_optimize
= 1;
2557 * Generate code to load the length of the radio header into
2558 * the register assigned to hold that length, if one has been
2559 * assigned. (If one hasn't been assigned, no code we've
2560 * generated uses that prefix, so we don't need to generate any
2563 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2564 * or always use the AVS header rather than the Prism header.
2565 * We load a 4-byte big-endian value at the beginning of the
2566 * raw packet data, and see whether, when masked with 0xFFFFF000,
2567 * it's equal to 0x80211000. If so, that indicates that it's
2568 * an AVS header (the masked-out bits are the version number).
2569 * Otherwise, it's a Prism header.
2571 * XXX - the Prism header is also, in theory, variable-length,
2572 * but no known software generates headers that aren't 144
2575 if (cstate
->off_linkhdr
.reg
!= -1) {
2579 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2583 * AND it with 0xFFFFF000.
2585 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2586 s2
->s
.k
= 0xFFFFF000;
2590 * Compare with 0x80211000.
2592 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2593 sjeq_avs_cookie
->s
.k
= 0x80211000;
2594 sappend(s1
, sjeq_avs_cookie
);
2599 * The 4 bytes at an offset of 4 from the beginning of
2600 * the AVS header are the length of the AVS header.
2601 * That field is big-endian.
2603 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2606 sjeq_avs_cookie
->s
.jt
= s2
;
2609 * Now jump to the code to allocate a register
2610 * into which to save the header length and
2611 * store the length there. (The "jump always"
2612 * instruction needs to have the k field set;
2613 * it's added to the PC, so, as we're jumping
2614 * over a single instruction, it should be 1.)
2616 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2618 sappend(s1
, sjcommon
);
2621 * Now for the code that handles the Prism header.
2622 * Just load the length of the Prism header (144)
2623 * into the A register. Have the test for an AVS
2624 * header branch here if we don't have an AVS header.
2626 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2629 sjeq_avs_cookie
->s
.jf
= s2
;
2632 * Now allocate a register to hold that value and store
2633 * it. The code for the AVS header will jump here after
2634 * loading the length of the AVS header.
2636 s2
= new_stmt(cstate
, BPF_ST
);
2637 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2639 sjcommon
->s
.jf
= s2
;
2642 * Now move it into the X register.
2644 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2652 static struct slist
*
2653 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2655 struct slist
*s1
, *s2
;
2658 * Generate code to load the length of the AVS header into
2659 * the register assigned to hold that length, if one has been
2660 * assigned. (If one hasn't been assigned, no code we've
2661 * generated uses that prefix, so we don't need to generate any
2664 if (cstate
->off_linkhdr
.reg
!= -1) {
2666 * The 4 bytes at an offset of 4 from the beginning of
2667 * the AVS header are the length of the AVS header.
2668 * That field is big-endian.
2670 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2674 * Now allocate a register to hold that value and store
2677 s2
= new_stmt(cstate
, BPF_ST
);
2678 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2682 * Now move it into the X register.
2684 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2692 static struct slist
*
2693 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2695 struct slist
*s1
, *s2
;
2698 * Generate code to load the length of the radiotap header into
2699 * the register assigned to hold that length, if one has been
2700 * assigned. (If one hasn't been assigned, no code we've
2701 * generated uses that prefix, so we don't need to generate any
2704 if (cstate
->off_linkhdr
.reg
!= -1) {
2706 * The 2 bytes at offsets of 2 and 3 from the beginning
2707 * of the radiotap header are the length of the radiotap
2708 * header; unfortunately, it's little-endian, so we have
2709 * to load it a byte at a time and construct the value.
2713 * Load the high-order byte, at an offset of 3, shift it
2714 * left a byte, and put the result in the X register.
2716 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2718 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2721 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2725 * Load the next byte, at an offset of 2, and OR the
2726 * value from the X register into it.
2728 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2731 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2735 * Now allocate a register to hold that value and store
2738 s2
= new_stmt(cstate
, BPF_ST
);
2739 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2743 * Now move it into the X register.
2745 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2754 * At the moment we treat PPI as normal Radiotap encoded
2755 * packets. The difference is in the function that generates
2756 * the code at the beginning to compute the header length.
2757 * Since this code generator of PPI supports bare 802.11
2758 * encapsulation only (i.e. the encapsulated DLT should be
2759 * DLT_IEEE802_11) we generate code to check for this too;
2760 * that's done in finish_parse().
2762 static struct slist
*
2763 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2765 struct slist
*s1
, *s2
;
2768 * Generate code to load the length of the radiotap header
2769 * into the register assigned to hold that length, if one has
2772 if (cstate
->off_linkhdr
.reg
!= -1) {
2774 * The 2 bytes at offsets of 2 and 3 from the beginning
2775 * of the radiotap header are the length of the radiotap
2776 * header; unfortunately, it's little-endian, so we have
2777 * to load it a byte at a time and construct the value.
2781 * Load the high-order byte, at an offset of 3, shift it
2782 * left a byte, and put the result in the X register.
2784 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2786 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2789 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2793 * Load the next byte, at an offset of 2, and OR the
2794 * value from the X register into it.
2796 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2799 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2803 * Now allocate a register to hold that value and store
2806 s2
= new_stmt(cstate
, BPF_ST
);
2807 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2811 * Now move it into the X register.
2813 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2822 * Load a value relative to the beginning of the link-layer header after the 802.11
2823 * header, i.e. LLC_SNAP.
2824 * The link-layer header doesn't necessarily begin at the beginning
2825 * of the packet data; there might be a variable-length prefix containing
2826 * radio information.
2828 static struct slist
*
2829 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2832 struct slist
*sjset_data_frame_1
;
2833 struct slist
*sjset_data_frame_2
;
2834 struct slist
*sjset_qos
;
2835 struct slist
*sjset_radiotap_flags_present
;
2836 struct slist
*sjset_radiotap_ext_present
;
2837 struct slist
*sjset_radiotap_tsft_present
;
2838 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2839 struct slist
*s_roundup
;
2841 if (cstate
->off_linkpl
.reg
== -1) {
2843 * No register has been assigned to the offset of
2844 * the link-layer payload, which means nobody needs
2845 * it; don't bother computing it - just return
2846 * what we already have.
2852 * This code is not compatible with the optimizer, as
2853 * we are generating jmp instructions within a normal
2854 * slist of instructions
2856 cstate
->no_optimize
= 1;
2859 * If "s" is non-null, it has code to arrange that the X register
2860 * contains the length of the prefix preceding the link-layer
2863 * Otherwise, the length of the prefix preceding the link-layer
2864 * header is "off_outermostlinkhdr.constant_part".
2868 * There is no variable-length header preceding the
2869 * link-layer header.
2871 * Load the length of the fixed-length prefix preceding
2872 * the link-layer header (if any) into the X register,
2873 * and store it in the cstate->off_linkpl.reg register.
2874 * That length is off_outermostlinkhdr.constant_part.
2876 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2877 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2881 * The X register contains the offset of the beginning of the
2882 * link-layer header; add 24, which is the minimum length
2883 * of the MAC header for a data frame, to that, and store it
2884 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2885 * which is at the offset in the X register, with an indexed load.
2887 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2889 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2892 s2
= new_stmt(cstate
, BPF_ST
);
2893 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2896 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2901 * Check the Frame Control field to see if this is a data frame;
2902 * a data frame has the 0x08 bit (b3) in that field set and the
2903 * 0x04 bit (b2) clear.
2905 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2906 sjset_data_frame_1
->s
.k
= 0x08;
2907 sappend(s
, sjset_data_frame_1
);
2910 * If b3 is set, test b2, otherwise go to the first statement of
2911 * the rest of the program.
2913 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2914 sjset_data_frame_2
->s
.k
= 0x04;
2915 sappend(s
, sjset_data_frame_2
);
2916 sjset_data_frame_1
->s
.jf
= snext
;
2919 * If b2 is not set, this is a data frame; test the QoS bit.
2920 * Otherwise, go to the first statement of the rest of the
2923 sjset_data_frame_2
->s
.jt
= snext
;
2924 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2925 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2926 sappend(s
, sjset_qos
);
2929 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2931 * Otherwise, go to the first statement of the rest of the
2934 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2935 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2937 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2940 s2
= new_stmt(cstate
, BPF_ST
);
2941 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2945 * If we have a radiotap header, look at it to see whether
2946 * there's Atheros padding between the MAC-layer header
2949 * Note: all of the fields in the radiotap header are
2950 * little-endian, so we byte-swap all of the values
2951 * we test against, as they will be loaded as big-endian
2954 * XXX - in the general case, we would have to scan through
2955 * *all* the presence bits, if there's more than one word of
2956 * presence bits. That would require a loop, meaning that
2957 * we wouldn't be able to run the filter in the kernel.
2959 * We assume here that the Atheros adapters that insert the
2960 * annoying padding don't have multiple antennae and therefore
2961 * do not generate radiotap headers with multiple presence words.
2963 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2965 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2966 * in the first presence flag word?
2968 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2972 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2973 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2974 sappend(s
, sjset_radiotap_flags_present
);
2977 * If not, skip all of this.
2979 sjset_radiotap_flags_present
->s
.jf
= snext
;
2982 * Otherwise, is the "extension" bit set in that word?
2984 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2985 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2986 sappend(s
, sjset_radiotap_ext_present
);
2987 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2990 * If so, skip all of this.
2992 sjset_radiotap_ext_present
->s
.jt
= snext
;
2995 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2997 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2998 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2999 sappend(s
, sjset_radiotap_tsft_present
);
3000 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3003 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3004 * at an offset of 16 from the beginning of the raw packet
3005 * data (8 bytes for the radiotap header and 8 bytes for
3008 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3011 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3014 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3016 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3017 sjset_tsft_datapad
->s
.k
= 0x20;
3018 sappend(s
, sjset_tsft_datapad
);
3021 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3022 * at an offset of 8 from the beginning of the raw packet
3023 * data (8 bytes for the radiotap header).
3025 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3028 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3031 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3033 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3034 sjset_notsft_datapad
->s
.k
= 0x20;
3035 sappend(s
, sjset_notsft_datapad
);
3038 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3039 * set, round the length of the 802.11 header to
3040 * a multiple of 4. Do that by adding 3 and then
3041 * dividing by and multiplying by 4, which we do by
3044 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3045 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3046 sappend(s
, s_roundup
);
3047 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3050 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3051 s2
->s
.k
= (bpf_u_int32
)~3;
3053 s2
= new_stmt(cstate
, BPF_ST
);
3054 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3057 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3058 sjset_tsft_datapad
->s
.jf
= snext
;
3059 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3060 sjset_notsft_datapad
->s
.jf
= snext
;
3062 sjset_qos
->s
.jf
= snext
;
3068 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3072 /* There is an implicit dependency between the link
3073 * payload and link header since the payload computation
3074 * includes the variable part of the header. Therefore,
3075 * if nobody else has allocated a register for the link
3076 * header and we need it, do it now. */
3077 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3078 cstate
->off_linkhdr
.reg
== -1)
3079 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3082 * For link-layer types that have a variable-length header
3083 * preceding the link-layer header, generate code to load
3084 * the offset of the link-layer header into the register
3085 * assigned to that offset, if any.
3087 * XXX - this, and the next switch statement, won't handle
3088 * encapsulation of 802.11 or 802.11+radio information in
3089 * some other protocol stack. That's significantly more
3092 switch (cstate
->outermostlinktype
) {
3094 case DLT_PRISM_HEADER
:
3095 s
= gen_load_prism_llprefixlen(cstate
);
3098 case DLT_IEEE802_11_RADIO_AVS
:
3099 s
= gen_load_avs_llprefixlen(cstate
);
3102 case DLT_IEEE802_11_RADIO
:
3103 s
= gen_load_radiotap_llprefixlen(cstate
);
3107 s
= gen_load_ppi_llprefixlen(cstate
);
3116 * For link-layer types that have a variable-length link-layer
3117 * header, generate code to load the offset of the link-layer
3118 * payload into the register assigned to that offset, if any.
3120 switch (cstate
->outermostlinktype
) {
3122 case DLT_IEEE802_11
:
3123 case DLT_PRISM_HEADER
:
3124 case DLT_IEEE802_11_RADIO_AVS
:
3125 case DLT_IEEE802_11_RADIO
:
3127 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3131 s
= gen_load_pflog_llprefixlen(cstate
);
3136 * If there is no initialization yet and we need variable
3137 * length offsets for VLAN, initialize them to zero
3139 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3142 if (cstate
->off_linkpl
.reg
== -1)
3143 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3144 if (cstate
->off_linktype
.reg
== -1)
3145 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3147 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3149 s2
= new_stmt(cstate
, BPF_ST
);
3150 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3152 s2
= new_stmt(cstate
, BPF_ST
);
3153 s2
->s
.k
= cstate
->off_linktype
.reg
;
3158 * If we have any offset-loading code, append all the
3159 * existing statements in the block to those statements,
3160 * and make the resulting list the list of statements
3164 sappend(s
, b
->stmts
);
3170 * Take an absolute offset, and:
3172 * if it has no variable part, return NULL;
3174 * if it has a variable part, generate code to load the register
3175 * containing that variable part into the X register, returning
3176 * a pointer to that code - if no register for that offset has
3177 * been allocated, allocate it first.
3179 * (The code to set that register will be generated later, but will
3180 * be placed earlier in the code sequence.)
3182 static struct slist
*
3183 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3187 if (off
->is_variable
) {
3188 if (off
->reg
== -1) {
3190 * We haven't yet assigned a register for the
3191 * variable part of the offset of the link-layer
3192 * header; allocate one.
3194 off
->reg
= alloc_reg(cstate
);
3198 * Load the register containing the variable part of the
3199 * offset of the link-layer header into the X register.
3201 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3206 * That offset isn't variable, there's no variable part,
3207 * so we don't need to generate any code.
3214 * Map an Ethernet type to the equivalent PPP type.
3217 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3225 case ETHERTYPE_IPV6
:
3226 ll_proto
= PPP_IPV6
;
3230 ll_proto
= PPP_DECNET
;
3233 case ETHERTYPE_ATALK
:
3234 ll_proto
= PPP_APPLE
;
3247 * I'm assuming the "Bridging PDU"s that go
3248 * over PPP are Spanning Tree Protocol
3251 ll_proto
= PPP_BRPDU
;
3262 * Generate any tests that, for encapsulation of a link-layer packet
3263 * inside another protocol stack, need to be done to check for those
3264 * link-layer packets (and that haven't already been done by a check
3265 * for that encapsulation).
3267 static struct block
*
3268 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3272 if (cstate
->is_encap
)
3273 return gen_encap_ll_check(cstate
);
3275 switch (cstate
->prevlinktype
) {
3279 * This is LANE-encapsulated Ethernet; check that the LANE
3280 * packet doesn't begin with an LE Control marker, i.e.
3281 * that it's data, not a control message.
3283 * (We've already generated a test for LANE.)
3285 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3291 * No such tests are necessary.
3299 * The three different values we should check for when checking for an
3300 * IPv6 packet with DLT_NULL.
3302 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3303 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3304 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3307 * Generate code to match a particular packet type by matching the
3308 * link-layer type field or fields in the 802.2 LLC header.
3310 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3311 * value, if <= ETHERMTU.
3313 static struct block
*
3314 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3316 struct block
*b0
, *b1
, *b2
;
3317 const char *description
;
3319 /* are we checking MPLS-encapsulated packets? */
3320 if (cstate
->label_stack_depth
> 0)
3321 return gen_mpls_linktype(cstate
, ll_proto
);
3323 switch (cstate
->linktype
) {
3326 case DLT_NETANALYZER
:
3327 case DLT_NETANALYZER_TRANSPARENT
:
3328 /* Geneve has an EtherType regardless of whether there is an
3329 * L2 header. VXLAN always has an EtherType. */
3330 if (!cstate
->is_encap
)
3331 b0
= gen_prevlinkhdr_check(cstate
);
3335 b1
= gen_ether_linktype(cstate
, ll_proto
);
3346 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3350 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3354 case DLT_IEEE802_11
:
3355 case DLT_PRISM_HEADER
:
3356 case DLT_IEEE802_11_RADIO_AVS
:
3357 case DLT_IEEE802_11_RADIO
:
3360 * Check that we have a data frame.
3362 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3363 IEEE80211_FC0_TYPE_DATA
,
3364 IEEE80211_FC0_TYPE_MASK
);
3367 * Now check for the specified link-layer type.
3369 b1
= gen_llc_linktype(cstate
, ll_proto
);
3376 * XXX - check for LLC frames.
3378 return gen_llc_linktype(cstate
, ll_proto
);
3383 * XXX - check for LLC PDUs, as per IEEE 802.5.
3385 return gen_llc_linktype(cstate
, ll_proto
);
3388 case DLT_ATM_RFC1483
:
3390 case DLT_IP_OVER_FC
:
3391 return gen_llc_linktype(cstate
, ll_proto
);
3396 * Check for an LLC-encapsulated version of this protocol;
3397 * if we were checking for LANE, linktype would no longer
3400 * Check for LLC encapsulation and then check the protocol.
3402 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3403 b1
= gen_llc_linktype(cstate
, ll_proto
);
3409 return gen_linux_sll_linktype(cstate
, ll_proto
);
3413 case DLT_SLIP_BSDOS
:
3416 * These types don't provide any type field; packets
3417 * are always IPv4 or IPv6.
3419 * XXX - for IPv4, check for a version number of 4, and,
3420 * for IPv6, check for a version number of 6?
3425 /* Check for a version number of 4. */
3426 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3428 case ETHERTYPE_IPV6
:
3429 /* Check for a version number of 6. */
3430 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3433 return gen_false(cstate
); /* always false */
3439 * Raw IPv4, so no type field.
3441 if (ll_proto
== ETHERTYPE_IP
)
3442 return gen_true(cstate
); /* always true */
3444 /* Checking for something other than IPv4; always false */
3445 return gen_false(cstate
);
3450 * Raw IPv6, so no type field.
3452 if (ll_proto
== ETHERTYPE_IPV6
)
3453 return gen_true(cstate
); /* always true */
3455 /* Checking for something other than IPv6; always false */
3456 return gen_false(cstate
);
3461 case DLT_PPP_SERIAL
:
3464 * We use Ethernet protocol types inside libpcap;
3465 * map them to the corresponding PPP protocol types.
3467 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3468 ethertype_to_ppptype(ll_proto
));
3473 * We use Ethernet protocol types inside libpcap;
3474 * map them to the corresponding PPP protocol types.
3480 * Also check for Van Jacobson-compressed IP.
3481 * XXX - do this for other forms of PPP?
3483 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3484 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3486 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3491 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3492 ethertype_to_ppptype(ll_proto
));
3502 return (gen_loopback_linktype(cstate
, AF_INET
));
3504 case ETHERTYPE_IPV6
:
3506 * AF_ values may, unfortunately, be platform-
3507 * dependent; AF_INET isn't, because everybody
3508 * used 4.2BSD's value, but AF_INET6 is, because
3509 * 4.2BSD didn't have a value for it (given that
3510 * IPv6 didn't exist back in the early 1980's),
3511 * and they all picked their own values.
3513 * This means that, if we're reading from a
3514 * savefile, we need to check for all the
3517 * If we're doing a live capture, we only need
3518 * to check for this platform's value; however,
3519 * Npcap uses 24, which isn't Windows's AF_INET6
3520 * value. (Given the multiple different values,
3521 * programs that read pcap files shouldn't be
3522 * checking for their platform's AF_INET6 value
3523 * anyway, they should check for all of the
3524 * possible values. and they might as well do
3525 * that even for live captures.)
3527 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3529 * Savefile - check for all three
3530 * possible IPv6 values.
3532 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3533 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3535 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3540 * Live capture, so we only need to
3541 * check for the value used on this
3546 * Npcap doesn't use Windows's AF_INET6,
3547 * as that collides with AF_IPX on
3548 * some BSDs (both have the value 23).
3549 * Instead, it uses 24.
3551 return (gen_loopback_linktype(cstate
, 24));
3554 return (gen_loopback_linktype(cstate
, AF_INET6
));
3555 #else /* AF_INET6 */
3557 * I guess this platform doesn't support
3558 * IPv6, so we just reject all packets.
3560 return gen_false(cstate
);
3561 #endif /* AF_INET6 */
3567 * Not a type on which we support filtering.
3568 * XXX - support those that have AF_ values
3569 * #defined on this platform, at least?
3571 return gen_false(cstate
);
3576 * af field is host byte order in contrast to the rest of
3579 if (ll_proto
== ETHERTYPE_IP
)
3580 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3582 else if (ll_proto
== ETHERTYPE_IPV6
)
3583 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3586 return gen_false(cstate
);
3590 case DLT_ARCNET_LINUX
:
3592 * XXX should we check for first fragment if the protocol
3598 return gen_false(cstate
);
3600 case ETHERTYPE_IPV6
:
3601 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3605 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3607 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3613 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3615 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3620 case ETHERTYPE_REVARP
:
3621 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3624 case ETHERTYPE_ATALK
:
3625 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3632 case ETHERTYPE_ATALK
:
3633 return gen_true(cstate
);
3635 return gen_false(cstate
);
3641 * XXX - assumes a 2-byte Frame Relay header with
3642 * DLCI and flags. What if the address is longer?
3648 * Check for the special NLPID for IP.
3650 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3652 case ETHERTYPE_IPV6
:
3654 * Check for the special NLPID for IPv6.
3656 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3660 * Check for several OSI protocols.
3662 * Frame Relay packets typically have an OSI
3663 * NLPID at the beginning; we check for each
3666 * What we check for is the NLPID and a frame
3667 * control field of UI, i.e. 0x03 followed
3670 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3671 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3672 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3678 return gen_false(cstate
);
3683 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3685 case DLT_JUNIPER_MFR
:
3686 case DLT_JUNIPER_MLFR
:
3687 case DLT_JUNIPER_MLPPP
:
3688 case DLT_JUNIPER_ATM1
:
3689 case DLT_JUNIPER_ATM2
:
3690 case DLT_JUNIPER_PPPOE
:
3691 case DLT_JUNIPER_PPPOE_ATM
:
3692 case DLT_JUNIPER_GGSN
:
3693 case DLT_JUNIPER_ES
:
3694 case DLT_JUNIPER_MONITOR
:
3695 case DLT_JUNIPER_SERVICES
:
3696 case DLT_JUNIPER_ETHER
:
3697 case DLT_JUNIPER_PPP
:
3698 case DLT_JUNIPER_FRELAY
:
3699 case DLT_JUNIPER_CHDLC
:
3700 case DLT_JUNIPER_VP
:
3701 case DLT_JUNIPER_ST
:
3702 case DLT_JUNIPER_ISM
:
3703 case DLT_JUNIPER_VS
:
3704 case DLT_JUNIPER_SRX_E2E
:
3705 case DLT_JUNIPER_FIBRECHANNEL
:
3706 case DLT_JUNIPER_ATM_CEMIC
:
3708 /* just lets verify the magic number for now -
3709 * on ATM we may have up to 6 different encapsulations on the wire
3710 * and need a lot of heuristics to figure out that the payload
3713 * FIXME encapsulation specific BPF_ filters
3715 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3717 case DLT_BACNET_MS_TP
:
3718 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3721 return gen_ipnet_linktype(cstate
, ll_proto
);
3723 case DLT_LINUX_IRDA
:
3724 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3727 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3730 case DLT_MTP2_WITH_PHDR
:
3731 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3734 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3737 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3739 case DLT_LINUX_LAPD
:
3740 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3742 case DLT_USB_FREEBSD
:
3744 case DLT_USB_LINUX_MMAPPED
:
3746 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3748 case DLT_BLUETOOTH_HCI_H4
:
3749 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3750 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3753 case DLT_CAN_SOCKETCAN
:
3754 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3756 case DLT_IEEE802_15_4
:
3757 case DLT_IEEE802_15_4_LINUX
:
3758 case DLT_IEEE802_15_4_NONASK_PHY
:
3759 case DLT_IEEE802_15_4_NOFCS
:
3760 case DLT_IEEE802_15_4_TAP
:
3761 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3763 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3764 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3767 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3770 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3772 case DLT_IPMB_KONTRON
:
3773 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3776 bpf_error(cstate
, "I2C link-layer type filtering not implemented");
3779 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3782 /* Using the fixed-size NFLOG header it is possible to tell only
3783 * the address family of the packet, other meaningful data is
3784 * either missing or behind TLVs.
3786 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3790 * Does this link-layer header type have a field
3791 * indicating the type of the next protocol? If
3792 * so, off_linktype.constant_part will be the offset of that
3793 * field in the packet; if not, it will be OFFSET_NOT_SET.
3795 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3797 * Yes; assume it's an Ethernet type. (If
3798 * it's not, it needs to be handled specially
3801 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3805 * No; report an error.
3807 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3808 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3816 * Check for an LLC SNAP packet with a given organization code and
3817 * protocol type; we check the entire contents of the 802.2 LLC and
3818 * snap headers, checking for DSAP and SSAP of SNAP and a control
3819 * field of 0x03 in the LLC header, and for the specified organization
3820 * code and protocol type in the SNAP header.
3822 static struct block
*
3823 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3825 u_char snapblock
[8];
3827 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3828 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3829 snapblock
[2] = 0x03; /* control = UI */
3830 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3831 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3832 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3833 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3834 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3835 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3839 * Generate code to match frames with an LLC header.
3841 static struct block
*
3842 gen_llc_internal(compiler_state_t
*cstate
)
3844 struct block
*b0
, *b1
;
3846 switch (cstate
->linktype
) {
3850 * We check for an Ethernet type field less or equal than
3851 * 1500, which means it's an 802.3 length field.
3853 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3856 * Now check for the purported DSAP and SSAP not being
3857 * 0xFF, to rule out NetWare-over-802.3.
3859 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3866 * We check for LLC traffic.
3868 b0
= gen_atmtype_llc(cstate
);
3871 case DLT_IEEE802
: /* Token Ring */
3873 * XXX - check for LLC frames.
3875 return gen_true(cstate
);
3879 * XXX - check for LLC frames.
3881 return gen_true(cstate
);
3883 case DLT_ATM_RFC1483
:
3885 * For LLC encapsulation, these are defined to have an
3888 * For VC encapsulation, they don't, but there's no
3889 * way to check for that; the protocol used on the VC
3890 * is negotiated out of band.
3892 return gen_true(cstate
);
3894 case DLT_IEEE802_11
:
3895 case DLT_PRISM_HEADER
:
3896 case DLT_IEEE802_11_RADIO
:
3897 case DLT_IEEE802_11_RADIO_AVS
:
3900 * Check that we have a data frame.
3902 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3903 IEEE80211_FC0_TYPE_DATA
,
3904 IEEE80211_FC0_TYPE_MASK
);
3907 bpf_error(cstate
, "'llc' not supported for %s",
3908 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3914 gen_llc(compiler_state_t
*cstate
)
3917 * Catch errors reported by us and routines below us, and return NULL
3920 if (setjmp(cstate
->top_ctx
))
3923 return gen_llc_internal(cstate
);
3927 gen_llc_i(compiler_state_t
*cstate
)
3929 struct block
*b0
, *b1
;
3933 * Catch errors reported by us and routines below us, and return NULL
3936 if (setjmp(cstate
->top_ctx
))
3940 * Check whether this is an LLC frame.
3942 b0
= gen_llc_internal(cstate
);
3945 * Load the control byte and test the low-order bit; it must
3946 * be clear for I frames.
3948 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3949 b1
= new_block(cstate
, JMP(BPF_JSET
));
3958 gen_llc_s(compiler_state_t
*cstate
)
3960 struct block
*b0
, *b1
;
3963 * Catch errors reported by us and routines below us, and return NULL
3966 if (setjmp(cstate
->top_ctx
))
3970 * Check whether this is an LLC frame.
3972 b0
= gen_llc_internal(cstate
);
3975 * Now compare the low-order 2 bit of the control byte against
3976 * the appropriate value for S frames.
3978 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3984 gen_llc_u(compiler_state_t
*cstate
)
3986 struct block
*b0
, *b1
;
3989 * Catch errors reported by us and routines below us, and return NULL
3992 if (setjmp(cstate
->top_ctx
))
3996 * Check whether this is an LLC frame.
3998 b0
= gen_llc_internal(cstate
);
4001 * Now compare the low-order 2 bit of the control byte against
4002 * the appropriate value for U frames.
4004 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4010 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4012 struct block
*b0
, *b1
;
4015 * Catch errors reported by us and routines below us, and return NULL
4018 if (setjmp(cstate
->top_ctx
))
4022 * Check whether this is an LLC frame.
4024 b0
= gen_llc_internal(cstate
);
4027 * Now check for an S frame with the appropriate type.
4029 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4035 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4037 struct block
*b0
, *b1
;
4040 * Catch errors reported by us and routines below us, and return NULL
4043 if (setjmp(cstate
->top_ctx
))
4047 * Check whether this is an LLC frame.
4049 b0
= gen_llc_internal(cstate
);
4052 * Now check for a U frame with the appropriate type.
4054 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4060 * Generate code to match a particular packet type, for link-layer types
4061 * using 802.2 LLC headers.
4063 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4064 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4066 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4067 * value, if <= ETHERMTU. We use that to determine whether to
4068 * match the DSAP or both DSAP and LSAP or to check the OUI and
4069 * protocol ID in a SNAP header.
4071 static struct block
*
4072 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4075 * XXX - handle token-ring variable-length header.
4081 case LLCSAP_NETBEUI
:
4083 * XXX - should we check both the DSAP and the
4084 * SSAP, like this, or should we check just the
4085 * DSAP, as we do for other SAP values?
4087 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4088 ((ll_proto
<< 8) | ll_proto
));
4092 * XXX - are there ever SNAP frames for IPX on
4093 * non-Ethernet 802.x networks?
4095 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4097 case ETHERTYPE_ATALK
:
4099 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4100 * SNAP packets with an organization code of
4101 * 0x080007 (Apple, for Appletalk) and a protocol
4102 * type of ETHERTYPE_ATALK (Appletalk).
4104 * XXX - check for an organization code of
4105 * encapsulated Ethernet as well?
4107 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4111 * XXX - we don't have to check for IPX 802.3
4112 * here, but should we check for the IPX Ethertype?
4114 if (ll_proto
<= ETHERMTU
) {
4116 * This is an LLC SAP value, so check
4119 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4122 * This is an Ethernet type; we assume that it's
4123 * unlikely that it'll appear in the right place
4124 * at random, and therefore check only the
4125 * location that would hold the Ethernet type
4126 * in a SNAP frame with an organization code of
4127 * 0x000000 (encapsulated Ethernet).
4129 * XXX - if we were to check for the SNAP DSAP and
4130 * LSAP, as per XXX, and were also to check for an
4131 * organization code of 0x000000 (encapsulated
4132 * Ethernet), we'd do
4134 * return gen_snap(cstate, 0x000000, ll_proto);
4136 * here; for now, we don't, as per the above.
4137 * I don't know whether it's worth the extra CPU
4138 * time to do the right check or not.
4140 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4145 static struct block
*
4146 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4147 int dir
, u_int src_off
, u_int dst_off
)
4149 struct block
*b0
, *b1
;
4163 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4164 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4170 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4171 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4176 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4180 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4184 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4188 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4192 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4196 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4203 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4207 static struct block
*
4208 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4209 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4211 struct block
*b0
, *b1
;
4214 * Code below needs to access four separate 32-bit parts of the 128-bit
4215 * IPv6 address and mask. In some OSes this is as simple as using the
4216 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4217 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4218 * far as libpcap sees it. Hence copy the data before use to avoid
4219 * potential unaligned memory access and the associated compiler
4220 * warnings (whether genuine or not).
4222 bpf_u_int32 a
[4], m
[4];
4235 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4236 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4242 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4243 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4248 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4252 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4256 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4260 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4264 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4268 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4275 /* this order is important */
4276 memcpy(a
, addr
, sizeof(a
));
4277 memcpy(m
, mask
, sizeof(m
));
4278 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4279 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4281 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4283 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4289 static struct block
*
4290 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4292 register struct block
*b0
, *b1
;
4296 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4299 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4302 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4303 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4309 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4310 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4315 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4319 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4323 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4327 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4331 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4335 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4343 * Like gen_ehostop, but for DLT_FDDI
4345 static struct block
*
4346 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4348 struct block
*b0
, *b1
;
4352 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4355 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4358 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4359 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4365 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4366 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4371 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4375 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4379 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4383 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4387 bpf_error(cstate
, "'ra' is only supported on 802.11");
4391 bpf_error(cstate
, "'ta' is only supported on 802.11");
4399 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4401 static struct block
*
4402 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4404 register struct block
*b0
, *b1
;
4408 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4411 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4414 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4415 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4421 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4422 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4427 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4431 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4435 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4439 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4443 bpf_error(cstate
, "'ra' is only supported on 802.11");
4447 bpf_error(cstate
, "'ta' is only supported on 802.11");
4455 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4456 * various 802.11 + radio headers.
4458 static struct block
*
4459 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4461 register struct block
*b0
, *b1
, *b2
;
4462 register struct slist
*s
;
4464 #ifdef ENABLE_WLAN_FILTERING_PATCH
4467 * We need to disable the optimizer because the optimizer is buggy
4468 * and wipes out some LD instructions generated by the below
4469 * code to validate the Frame Control bits
4471 cstate
->no_optimize
= 1;
4472 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4479 * For control frames, there is no SA.
4481 * For management frames, SA is at an
4482 * offset of 10 from the beginning of
4485 * For data frames, SA is at an offset
4486 * of 10 from the beginning of the packet
4487 * if From DS is clear, at an offset of
4488 * 16 from the beginning of the packet
4489 * if From DS is set and To DS is clear,
4490 * and an offset of 24 from the beginning
4491 * of the packet if From DS is set and To DS
4496 * Generate the tests to be done for data frames
4499 * First, check for To DS set, i.e. check "link[1] & 0x01".
4501 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4502 b1
= new_block(cstate
, JMP(BPF_JSET
));
4503 b1
->s
.k
= 0x01; /* To DS */
4507 * If To DS is set, the SA is at 24.
4509 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4513 * Now, check for To DS not set, i.e. check
4514 * "!(link[1] & 0x01)".
4516 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4517 b2
= new_block(cstate
, JMP(BPF_JSET
));
4518 b2
->s
.k
= 0x01; /* To DS */
4523 * If To DS is not set, the SA is at 16.
4525 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4529 * Now OR together the last two checks. That gives
4530 * the complete set of checks for data frames with
4536 * Now check for From DS being set, and AND that with
4537 * the ORed-together checks.
4539 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4540 b1
= new_block(cstate
, JMP(BPF_JSET
));
4541 b1
->s
.k
= 0x02; /* From DS */
4546 * Now check for data frames with From DS not set.
4548 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4549 b2
= new_block(cstate
, JMP(BPF_JSET
));
4550 b2
->s
.k
= 0x02; /* From DS */
4555 * If From DS isn't set, the SA is at 10.
4557 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4561 * Now OR together the checks for data frames with
4562 * From DS not set and for data frames with From DS
4563 * set; that gives the checks done for data frames.
4568 * Now check for a data frame.
4569 * I.e, check "link[0] & 0x08".
4571 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4572 b1
= new_block(cstate
, JMP(BPF_JSET
));
4577 * AND that with the checks done for data frames.
4582 * If the high-order bit of the type value is 0, this
4583 * is a management frame.
4584 * I.e, check "!(link[0] & 0x08)".
4586 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4587 b2
= new_block(cstate
, JMP(BPF_JSET
));
4593 * For management frames, the SA is at 10.
4595 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4599 * OR that with the checks done for data frames.
4600 * That gives the checks done for management and
4606 * If the low-order bit of the type value is 1,
4607 * this is either a control frame or a frame
4608 * with a reserved type, and thus not a
4611 * I.e., check "!(link[0] & 0x04)".
4613 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4614 b1
= new_block(cstate
, JMP(BPF_JSET
));
4620 * AND that with the checks for data and management
4630 * For control frames, there is no DA.
4632 * For management frames, DA is at an
4633 * offset of 4 from the beginning of
4636 * For data frames, DA is at an offset
4637 * of 4 from the beginning of the packet
4638 * if To DS is clear and at an offset of
4639 * 16 from the beginning of the packet
4644 * Generate the tests to be done for data frames.
4646 * First, check for To DS set, i.e. "link[1] & 0x01".
4648 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4649 b1
= new_block(cstate
, JMP(BPF_JSET
));
4650 b1
->s
.k
= 0x01; /* To DS */
4654 * If To DS is set, the DA is at 16.
4656 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4660 * Now, check for To DS not set, i.e. check
4661 * "!(link[1] & 0x01)".
4663 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4664 b2
= new_block(cstate
, JMP(BPF_JSET
));
4665 b2
->s
.k
= 0x01; /* To DS */
4670 * If To DS is not set, the DA is at 4.
4672 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4676 * Now OR together the last two checks. That gives
4677 * the complete set of checks for data frames.
4682 * Now check for a data frame.
4683 * I.e, check "link[0] & 0x08".
4685 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4686 b1
= new_block(cstate
, JMP(BPF_JSET
));
4691 * AND that with the checks done for data frames.
4696 * If the high-order bit of the type value is 0, this
4697 * is a management frame.
4698 * I.e, check "!(link[0] & 0x08)".
4700 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4701 b2
= new_block(cstate
, JMP(BPF_JSET
));
4707 * For management frames, the DA is at 4.
4709 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4713 * OR that with the checks done for data frames.
4714 * That gives the checks done for management and
4720 * If the low-order bit of the type value is 1,
4721 * this is either a control frame or a frame
4722 * with a reserved type, and thus not a
4725 * I.e., check "!(link[0] & 0x04)".
4727 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4728 b1
= new_block(cstate
, JMP(BPF_JSET
));
4734 * AND that with the checks for data and management
4741 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4742 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4748 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4749 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4754 * XXX - add BSSID keyword?
4757 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4761 * Not present in CTS or ACK control frames.
4763 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4764 IEEE80211_FC0_TYPE_MASK
);
4766 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4767 IEEE80211_FC0_SUBTYPE_MASK
);
4769 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4770 IEEE80211_FC0_SUBTYPE_MASK
);
4774 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4780 * Not present in control frames.
4782 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4783 IEEE80211_FC0_TYPE_MASK
);
4785 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4791 * Present only if the direction mask has both "From DS"
4792 * and "To DS" set. Neither control frames nor management
4793 * frames should have both of those set, so we don't
4794 * check the frame type.
4796 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4797 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4798 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4804 * Not present in management frames; addr1 in other
4809 * If the high-order bit of the type value is 0, this
4810 * is a management frame.
4811 * I.e, check "(link[0] & 0x08)".
4813 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4814 b1
= new_block(cstate
, JMP(BPF_JSET
));
4821 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4824 * AND that with the check of addr1.
4831 * Not present in management frames; addr2, if present,
4836 * Not present in CTS or ACK control frames.
4838 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4839 IEEE80211_FC0_TYPE_MASK
);
4841 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4842 IEEE80211_FC0_SUBTYPE_MASK
);
4844 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4845 IEEE80211_FC0_SUBTYPE_MASK
);
4851 * If the high-order bit of the type value is 0, this
4852 * is a management frame.
4853 * I.e, check "(link[0] & 0x08)".
4855 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4856 b1
= new_block(cstate
, JMP(BPF_JSET
));
4861 * AND that with the check for frames other than
4862 * CTS and ACK frames.
4869 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4878 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4879 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4880 * as the RFC states.)
4882 static struct block
*
4883 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4885 register struct block
*b0
, *b1
;
4889 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4892 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4895 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4896 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4902 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4903 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4908 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4912 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4916 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4920 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4924 bpf_error(cstate
, "'ra' is only supported on 802.11");
4928 bpf_error(cstate
, "'ta' is only supported on 802.11");
4936 * This is quite tricky because there may be pad bytes in front of the
4937 * DECNET header, and then there are two possible data packet formats that
4938 * carry both src and dst addresses, plus 5 packet types in a format that
4939 * carries only the src node, plus 2 types that use a different format and
4940 * also carry just the src node.
4944 * Instead of doing those all right, we just look for data packets with
4945 * 0 or 1 bytes of padding. If you want to look at other packets, that
4946 * will require a lot more hacking.
4948 * To add support for filtering on DECNET "areas" (network numbers)
4949 * one would want to add a "mask" argument to this routine. That would
4950 * make the filter even more inefficient, although one could be clever
4951 * and not generate masking instructions if the mask is 0xFFFF.
4953 static struct block
*
4954 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4956 struct block
*b0
, *b1
, *b2
, *tmp
;
4957 u_int offset_lh
; /* offset if long header is received */
4958 u_int offset_sh
; /* offset if short header is received */
4963 offset_sh
= 1; /* follows flags */
4964 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4968 offset_sh
= 3; /* follows flags, dstnode */
4969 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4973 /* Inefficient because we do our Calvinball dance twice */
4974 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4975 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4981 /* Inefficient because we do our Calvinball dance twice */
4982 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4983 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4988 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4992 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4996 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5000 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5004 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5008 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5016 * In a DECnet message inside an Ethernet frame the first two bytes
5017 * immediately after EtherType are the [litle-endian] DECnet message
5018 * length, which is irrelevant in this context.
5020 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5021 * 8-bit bitmap of the optional padding before the packet route header.
5022 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5023 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5024 * means there aren't any PAD bytes after the bitmap, so the header
5025 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5026 * is set to 0, thus the header begins at the third byte.
5028 * The header can be in several (as mentioned above) formats, all of
5029 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5030 * (PF, "pad field") set to 0 regardless of any padding present before
5031 * the header. "Short header" means bits 0-2 of the bitmap encode the
5032 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5034 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5035 * values and the masks, this maps to the required single bytes of
5036 * the message correctly on both big-endian and little-endian hosts.
5037 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5038 * because the wire encoding is little-endian and BPF multiple-byte
5039 * loads are big-endian. When the destination address is near enough
5040 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5043 /* Check for pad = 1, long header case */
5044 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5045 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5046 BPF_H
, SWAPSHORT(addr
));
5048 /* Check for pad = 0, long header case */
5049 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5050 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5054 /* Check for pad = 1, short header case */
5056 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5057 0x81020000U
| SWAPSHORT(addr
),
5060 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5061 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5066 /* Check for pad = 0, short header case */
5068 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5069 0x02000000U
| SWAPSHORT(addr
) << 8,
5072 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5073 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5083 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5084 * test the bottom-of-stack bit, and then check the version number
5085 * field in the IP header.
5087 static struct block
*
5088 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5090 struct block
*b0
, *b1
;
5095 /* match the bottom-of-stack bit */
5096 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5097 /* match the IPv4 version number */
5098 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5102 case ETHERTYPE_IPV6
:
5103 /* match the bottom-of-stack bit */
5104 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5105 /* match the IPv4 version number */
5106 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5111 /* FIXME add other L3 proto IDs */
5112 bpf_error(cstate
, "unsupported protocol over mpls");
5117 static struct block
*
5118 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5119 int proto
, int dir
, int type
)
5121 struct block
*b0
, *b1
;
5122 const char *typestr
;
5132 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5134 * Only check for non-IPv4 addresses if we're not
5135 * checking MPLS-encapsulated packets.
5137 if (cstate
->label_stack_depth
== 0) {
5138 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5140 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5146 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5149 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5150 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5155 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5156 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5161 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5162 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5167 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5170 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5173 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5176 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5179 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5182 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5185 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5188 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5189 b1
= gen_dnhostop(cstate
, addr
, dir
);
5194 bpf_error(cstate
, "LAT host filtering not implemented");
5197 bpf_error(cstate
, "SCA host filtering not implemented");
5200 bpf_error(cstate
, "MOPRC host filtering not implemented");
5203 bpf_error(cstate
, "MOPDL host filtering not implemented");
5206 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5209 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5212 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5215 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5218 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5221 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5224 bpf_error(cstate
, "AARP host filtering not implemented");
5227 bpf_error(cstate
, "ISO host filtering not implemented");
5230 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5233 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5236 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5239 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5242 bpf_error(cstate
, "IPX host filtering not implemented");
5245 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5248 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5251 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5254 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5257 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5260 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5263 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5266 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5269 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5272 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5281 static struct block
*
5282 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5283 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5285 struct block
*b0
, *b1
;
5286 const char *typestr
;
5297 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5298 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5303 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5306 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5309 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5312 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5315 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5318 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5321 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5324 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5327 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5330 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5333 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5336 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5339 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5342 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5345 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5348 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5351 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5354 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5357 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5360 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5363 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5366 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5369 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5372 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5375 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5378 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5381 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5384 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5387 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5390 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5393 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5396 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5399 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5402 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5405 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5408 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5411 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5414 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5424 static struct block
*
5425 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5426 struct addrinfo
*alist
, int proto
, int dir
)
5428 struct block
*b0
, *b1
, *tmp
;
5429 struct addrinfo
*ai
;
5430 struct sockaddr_in
*sin
;
5433 bpf_error(cstate
, "direction applied to 'gateway'");
5440 switch (cstate
->linktype
) {
5442 case DLT_NETANALYZER
:
5443 case DLT_NETANALYZER_TRANSPARENT
:
5444 b1
= gen_prevlinkhdr_check(cstate
);
5445 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5450 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5453 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5455 case DLT_IEEE802_11
:
5456 case DLT_PRISM_HEADER
:
5457 case DLT_IEEE802_11_RADIO_AVS
:
5458 case DLT_IEEE802_11_RADIO
:
5460 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5462 case DLT_IP_OVER_FC
:
5463 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5467 * This is LLC-multiplexed traffic; if it were
5468 * LANE, cstate->linktype would have been set to
5474 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5477 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5479 * Does it have an address?
5481 if (ai
->ai_addr
!= NULL
) {
5483 * Yes. Is it an IPv4 address?
5485 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5487 * Generate an entry for it.
5489 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5490 tmp
= gen_host(cstate
,
5491 ntohl(sin
->sin_addr
.s_addr
),
5492 0xffffffff, proto
, Q_OR
, Q_HOST
);
5494 * Is it the *first* IPv4 address?
5498 * Yes, so start with it.
5503 * No, so OR it into the
5515 * No IPv4 addresses found.
5523 bpf_error(cstate
, "illegal modifier of 'gateway'");
5528 static struct block
*
5529 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5537 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5541 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5545 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5549 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5552 #ifndef IPPROTO_IGMP
5553 #define IPPROTO_IGMP 2
5557 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5560 #ifndef IPPROTO_IGRP
5561 #define IPPROTO_IGRP 9
5564 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5568 #define IPPROTO_PIM 103
5572 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5575 #ifndef IPPROTO_VRRP
5576 #define IPPROTO_VRRP 112
5580 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5583 #ifndef IPPROTO_CARP
5584 #define IPPROTO_CARP 112
5588 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5592 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5596 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5600 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5604 bpf_error(cstate
, "link layer applied in wrong context");
5607 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5611 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5615 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5619 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5623 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5627 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5631 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5635 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5638 #ifndef IPPROTO_ICMPV6
5639 #define IPPROTO_ICMPV6 58
5642 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5646 #define IPPROTO_AH 51
5649 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5653 #define IPPROTO_ESP 50
5656 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5660 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5664 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5668 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5671 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5672 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5673 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5675 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5677 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5679 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5683 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5684 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5685 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5687 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5689 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5691 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5695 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5696 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5697 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5699 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5704 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5705 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5710 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5711 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5713 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5715 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5720 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5721 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5726 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5727 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5732 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5736 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5740 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5744 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5748 bpf_error(cstate
, "'radio' is not a valid protocol type");
5757 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5760 * Catch errors reported by us and routines below us, and return NULL
5763 if (setjmp(cstate
->top_ctx
))
5766 return gen_proto_abbrev_internal(cstate
, proto
);
5769 static struct block
*
5770 gen_ipfrag(compiler_state_t
*cstate
)
5775 /* not IPv4 frag other than the first frag */
5776 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5777 b
= new_block(cstate
, JMP(BPF_JSET
));
5786 * Generate a comparison to a port value in the transport-layer header
5787 * at the specified offset from the beginning of that header.
5789 * XXX - this handles a variable-length prefix preceding the link-layer
5790 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5791 * variable-length link-layer headers (such as Token Ring or 802.11
5794 static struct block
*
5795 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5797 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5800 static struct block
*
5801 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5803 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5806 static struct block
*
5807 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5809 struct block
*b0
, *b1
, *tmp
;
5811 /* ip proto 'proto' and not a fragment other than the first fragment */
5812 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5813 b0
= gen_ipfrag(cstate
);
5818 b1
= gen_portatom(cstate
, 0, port
);
5822 b1
= gen_portatom(cstate
, 2, port
);
5826 tmp
= gen_portatom(cstate
, 0, port
);
5827 b1
= gen_portatom(cstate
, 2, port
);
5833 tmp
= gen_portatom(cstate
, 0, port
);
5834 b1
= gen_portatom(cstate
, 2, port
);
5839 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5843 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5847 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5851 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5855 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5859 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5871 static struct block
*
5872 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5874 struct block
*b0
, *b1
, *tmp
;
5879 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5880 * not LLC encapsulation with LLCSAP_IP.
5882 * For IEEE 802 networks - which includes 802.5 token ring
5883 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5884 * says that SNAP encapsulation is used, not LLC encapsulation
5887 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5888 * RFC 2225 say that SNAP encapsulation is used, not LLC
5889 * encapsulation with LLCSAP_IP.
5891 * So we always check for ETHERTYPE_IP.
5893 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5899 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5903 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5904 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5906 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5918 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5920 struct block
*b0
, *b1
, *tmp
;
5922 /* ip6 proto 'proto' */
5923 /* XXX - catch the first fragment of a fragmented packet? */
5924 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5928 b1
= gen_portatom6(cstate
, 0, port
);
5932 b1
= gen_portatom6(cstate
, 2, port
);
5936 tmp
= gen_portatom6(cstate
, 0, port
);
5937 b1
= gen_portatom6(cstate
, 2, port
);
5943 tmp
= gen_portatom6(cstate
, 0, port
);
5944 b1
= gen_portatom6(cstate
, 2, port
);
5956 static struct block
*
5957 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5959 struct block
*b0
, *b1
, *tmp
;
5961 /* link proto ip6 */
5962 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5968 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5972 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5973 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5975 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5986 /* gen_portrange code */
5987 static struct block
*
5988 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5991 struct block
*b1
, *b2
;
5995 * Reverse the order of the ports, so v1 is the lower one.
6004 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
6005 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
6012 static struct block
*
6013 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6014 bpf_u_int32 proto
, int dir
)
6016 struct block
*b0
, *b1
, *tmp
;
6018 /* ip proto 'proto' and not a fragment other than the first fragment */
6019 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
6020 b0
= gen_ipfrag(cstate
);
6025 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6029 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6033 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6034 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6040 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6041 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6046 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6050 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6054 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6058 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6062 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
6066 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
6078 static struct block
*
6079 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6082 struct block
*b0
, *b1
, *tmp
;
6085 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6091 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6096 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6097 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6099 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6110 static struct block
*
6111 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6114 struct block
*b1
, *b2
;
6118 * Reverse the order of the ports, so v1 is the lower one.
6127 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6128 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6135 static struct block
*
6136 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6137 bpf_u_int32 proto
, int dir
)
6139 struct block
*b0
, *b1
, *tmp
;
6141 /* ip6 proto 'proto' */
6142 /* XXX - catch the first fragment of a fragmented packet? */
6143 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6147 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6151 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6155 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6156 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6162 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6163 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6175 static struct block
*
6176 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6179 struct block
*b0
, *b1
, *tmp
;
6181 /* link proto ip6 */
6182 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6188 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6193 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6194 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6196 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6208 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6217 v
= pcap_nametoproto(name
);
6218 if (v
== PROTO_UNDEF
)
6219 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6223 /* XXX should look up h/w protocol type based on cstate->linktype */
6224 v
= pcap_nametoeproto(name
);
6225 if (v
== PROTO_UNDEF
) {
6226 v
= pcap_nametollc(name
);
6227 if (v
== PROTO_UNDEF
)
6228 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6233 if (strcmp(name
, "esis") == 0)
6235 else if (strcmp(name
, "isis") == 0)
6237 else if (strcmp(name
, "clnp") == 0)
6240 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6250 #if !defined(NO_PROTOCHAIN)
6251 static struct block
*
6252 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6254 struct block
*b0
, *b
;
6255 struct slist
*s
[100];
6256 int fix2
, fix3
, fix4
, fix5
;
6257 int ahcheck
, again
, end
;
6259 int reg2
= alloc_reg(cstate
);
6261 memset(s
, 0, sizeof(s
));
6262 fix3
= fix4
= fix5
= 0;
6269 b0
= gen_protochain(cstate
, v
, Q_IP
);
6270 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6274 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6279 * We don't handle variable-length prefixes before the link-layer
6280 * header, or variable-length link-layer headers, here yet.
6281 * We might want to add BPF instructions to do the protochain
6282 * work, to simplify that and, on platforms that have a BPF
6283 * interpreter with the new instructions, let the filtering
6284 * be done in the kernel. (We already require a modified BPF
6285 * engine to do the protochain stuff, to support backward
6286 * branches, and backward branch support is unlikely to appear
6287 * in kernel BPF engines.)
6289 if (cstate
->off_linkpl
.is_variable
)
6290 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6293 * To quote a comment in optimize.c:
6295 * "These data structures are used in a Cocke and Schwartz style
6296 * value numbering scheme. Since the flowgraph is acyclic,
6297 * exit values can be propagated from a node's predecessors
6298 * provided it is uniquely defined."
6300 * "Acyclic" means "no backward branches", which means "no
6301 * loops", so we have to turn the optimizer off.
6303 cstate
->no_optimize
= 1;
6306 * s[0] is a dummy entry to protect other BPF insn from damage
6307 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6308 * hard to find interdependency made by jump table fixup.
6311 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6316 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6319 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6320 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6322 /* X = ip->ip_hl << 2 */
6323 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6324 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6329 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6331 /* A = ip6->ip_nxt */
6332 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6333 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6335 /* X = sizeof(struct ip6_hdr) */
6336 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6342 bpf_error(cstate
, "unsupported proto to gen_protochain");
6346 /* again: if (A == v) goto end; else fall through; */
6348 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6350 s
[i
]->s
.jt
= NULL
; /*later*/
6351 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6355 #ifndef IPPROTO_NONE
6356 #define IPPROTO_NONE 59
6358 /* if (A == IPPROTO_NONE) goto end */
6359 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6360 s
[i
]->s
.jt
= NULL
; /*later*/
6361 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6362 s
[i
]->s
.k
= IPPROTO_NONE
;
6363 s
[fix5
]->s
.jf
= s
[i
];
6367 if (proto
== Q_IPV6
) {
6368 int v6start
, v6end
, v6advance
, j
;
6371 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6372 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6373 s
[i
]->s
.jt
= NULL
; /*later*/
6374 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6375 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6376 s
[fix2
]->s
.jf
= s
[i
];
6378 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6379 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6380 s
[i
]->s
.jt
= NULL
; /*later*/
6381 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6382 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6384 /* if (A == IPPROTO_ROUTING) goto v6advance */
6385 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6386 s
[i
]->s
.jt
= NULL
; /*later*/
6387 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6388 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6390 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6391 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6392 s
[i
]->s
.jt
= NULL
; /*later*/
6393 s
[i
]->s
.jf
= NULL
; /*later*/
6394 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6404 * A = P[X + packet head];
6405 * X = X + (P[X + packet head + 1] + 1) * 8;
6407 /* A = P[X + packet head] */
6408 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6409 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6412 s
[i
] = new_stmt(cstate
, BPF_ST
);
6415 /* A = P[X + packet head + 1]; */
6416 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6417 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6420 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6424 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6428 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6432 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6435 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6439 /* goto again; (must use BPF_JA for backward jump) */
6440 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6441 s
[i
]->s
.k
= again
- i
- 1;
6442 s
[i
- 1]->s
.jf
= s
[i
];
6446 for (j
= v6start
; j
<= v6end
; j
++)
6447 s
[j
]->s
.jt
= s
[v6advance
];
6450 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6452 s
[fix2
]->s
.jf
= s
[i
];
6458 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6459 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6460 s
[i
]->s
.jt
= NULL
; /*later*/
6461 s
[i
]->s
.jf
= NULL
; /*later*/
6462 s
[i
]->s
.k
= IPPROTO_AH
;
6464 s
[fix3
]->s
.jf
= s
[ahcheck
];
6471 * X = X + (P[X + 1] + 2) * 4;
6474 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6476 /* A = P[X + packet head]; */
6477 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6478 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6481 s
[i
] = new_stmt(cstate
, BPF_ST
);
6485 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6488 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6492 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6494 /* A = P[X + packet head] */
6495 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6496 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6499 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6503 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6507 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6510 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6514 /* goto again; (must use BPF_JA for backward jump) */
6515 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6516 s
[i
]->s
.k
= again
- i
- 1;
6521 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6523 s
[fix2
]->s
.jt
= s
[end
];
6524 s
[fix4
]->s
.jf
= s
[end
];
6525 s
[fix5
]->s
.jt
= s
[end
];
6532 for (i
= 0; i
< max
- 1; i
++)
6533 s
[i
]->next
= s
[i
+ 1];
6534 s
[max
- 1]->next
= NULL
;
6539 b
= new_block(cstate
, JMP(BPF_JEQ
));
6540 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6543 free_reg(cstate
, reg2
);
6548 #endif /* !defined(NO_PROTOCHAIN) */
6551 * Generate code that checks whether the packet is a packet for protocol
6552 * <proto> and whether the type field in that protocol's header has
6553 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6554 * IP packet and checks the protocol number in the IP header against <v>.
6556 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6557 * against Q_IP and Q_IPV6.
6559 static struct block
*
6560 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6562 struct block
*b0
, *b1
;
6565 if (dir
!= Q_DEFAULT
)
6566 bpf_error(cstate
, "direction applied to 'proto'");
6570 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6571 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6576 return gen_linktype(cstate
, v
);
6580 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6581 * not LLC encapsulation with LLCSAP_IP.
6583 * For IEEE 802 networks - which includes 802.5 token ring
6584 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6585 * says that SNAP encapsulation is used, not LLC encapsulation
6588 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6589 * RFC 2225 say that SNAP encapsulation is used, not LLC
6590 * encapsulation with LLCSAP_IP.
6592 * So we always check for ETHERTYPE_IP.
6594 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6595 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6600 bpf_error(cstate
, "arp does not encapsulate another protocol");
6604 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6608 bpf_error(cstate
, "'sctp proto' is bogus");
6612 bpf_error(cstate
, "'tcp proto' is bogus");
6616 bpf_error(cstate
, "'udp proto' is bogus");
6620 bpf_error(cstate
, "'icmp proto' is bogus");
6624 bpf_error(cstate
, "'igmp proto' is bogus");
6628 bpf_error(cstate
, "'igrp proto' is bogus");
6632 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6636 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6640 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6644 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6648 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6652 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6656 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6658 * Also check for a fragment header before the final
6661 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6662 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6664 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6670 bpf_error(cstate
, "'icmp6 proto' is bogus");
6674 bpf_error(cstate
, "'ah proto' is bogus");
6678 bpf_error(cstate
, "'esp proto' is bogus");
6682 bpf_error(cstate
, "'pim proto' is bogus");
6686 bpf_error(cstate
, "'vrrp proto' is bogus");
6690 bpf_error(cstate
, "'aarp proto' is bogus");
6694 switch (cstate
->linktype
) {
6698 * Frame Relay packets typically have an OSI
6699 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6700 * generates code to check for all the OSI
6701 * NLPIDs, so calling it and then adding a check
6702 * for the particular NLPID for which we're
6703 * looking is bogus, as we can just check for
6706 * What we check for is the NLPID and a frame
6707 * control field value of UI, i.e. 0x03 followed
6710 * XXX - assumes a 2-byte Frame Relay header with
6711 * DLCI and flags. What if the address is longer?
6713 * XXX - what about SNAP-encapsulated frames?
6715 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6721 * Cisco uses an Ethertype lookalike - for OSI,
6724 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6725 /* OSI in C-HDLC is stuffed with a fudge byte */
6726 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6731 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6732 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6738 bpf_error(cstate
, "'esis proto' is bogus");
6742 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6744 * 4 is the offset of the PDU type relative to the IS-IS
6747 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6752 bpf_error(cstate
, "'clnp proto' is not supported");
6756 bpf_error(cstate
, "'stp proto' is bogus");
6760 bpf_error(cstate
, "'ipx proto' is bogus");
6764 bpf_error(cstate
, "'netbeui proto' is bogus");
6768 bpf_error(cstate
, "'l1 proto' is bogus");
6772 bpf_error(cstate
, "'l2 proto' is bogus");
6776 bpf_error(cstate
, "'iih proto' is bogus");
6780 bpf_error(cstate
, "'snp proto' is bogus");
6784 bpf_error(cstate
, "'csnp proto' is bogus");
6788 bpf_error(cstate
, "'psnp proto' is bogus");
6792 bpf_error(cstate
, "'lsp proto' is bogus");
6796 bpf_error(cstate
, "'radio proto' is bogus");
6800 bpf_error(cstate
, "'carp proto' is bogus");
6811 * Convert a non-numeric name to a port number.
6814 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6816 struct addrinfo hints
, *res
, *ai
;
6818 struct sockaddr_in
*in4
;
6820 struct sockaddr_in6
*in6
;
6825 * We check for both TCP and UDP in case there are
6826 * ambiguous entries.
6828 memset(&hints
, 0, sizeof(hints
));
6829 hints
.ai_family
= PF_UNSPEC
;
6830 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6831 hints
.ai_protocol
= ipproto
;
6832 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6839 * No such port. Just return -1.
6846 * We don't use strerror() because it's not
6847 * guaranteed to be thread-safe on all platforms
6848 * (probably because it might use a non-thread-local
6849 * buffer into which to format an error message
6850 * if the error code isn't one for which it has
6851 * a canned string; three cheers for C string
6854 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6856 port
= -2; /* a real error */
6862 * This is a real error, not just "there's
6863 * no such service name".
6865 * We don't use gai_strerror() because it's not
6866 * guaranteed to be thread-safe on all platforms
6867 * (probably because it might use a non-thread-local
6868 * buffer into which to format an error message
6869 * if the error code isn't one for which it has
6870 * a canned string; three cheers for C string
6873 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6875 port
= -2; /* a real error */
6880 * OK, we found it. Did it find anything?
6882 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6884 * Does it have an address?
6886 if (ai
->ai_addr
!= NULL
) {
6888 * Yes. Get a port number; we're done.
6890 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6891 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6892 port
= ntohs(in4
->sin_port
);
6896 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6897 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6898 port
= ntohs(in6
->sin6_port
);
6910 * Convert a string to a port number.
6913 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6923 * See if it's a number.
6925 ret
= stoulen(string
, string_size
, &val
, cstate
);
6929 /* Unknown port type - it's just a number. */
6930 *proto
= PROTO_UNDEF
;
6933 case STOULEN_NOT_OCTAL_NUMBER
:
6934 case STOULEN_NOT_HEX_NUMBER
:
6935 case STOULEN_NOT_DECIMAL_NUMBER
:
6937 * Not a valid number; try looking it up as a port.
6939 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6940 memcpy(cpy
, string
, string_size
);
6941 cpy
[string_size
] = '\0';
6942 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6943 if (tcp_port
== -2) {
6945 * We got a hard error; the error string has
6949 longjmp(cstate
->top_ctx
, 1);
6952 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6953 if (udp_port
== -2) {
6955 * We got a hard error; the error string has
6959 longjmp(cstate
->top_ctx
, 1);
6964 * We need to check /etc/services for ambiguous entries.
6965 * If we find an ambiguous entry, and it has the
6966 * same port number, change the proto to PROTO_UNDEF
6967 * so both TCP and UDP will be checked.
6969 if (tcp_port
>= 0) {
6970 val
= (bpf_u_int32
)tcp_port
;
6971 *proto
= IPPROTO_TCP
;
6972 if (udp_port
>= 0) {
6973 if (udp_port
== tcp_port
)
6974 *proto
= PROTO_UNDEF
;
6977 /* Can't handle ambiguous names that refer
6978 to different port numbers. */
6979 warning("ambiguous port %s in /etc/services",
6986 if (udp_port
>= 0) {
6987 val
= (bpf_u_int32
)udp_port
;
6988 *proto
= IPPROTO_UDP
;
6992 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6994 longjmp(cstate
->top_ctx
, 1);
7001 /* Error already set. */
7002 longjmp(cstate
->top_ctx
, 1);
7009 /* Should not happen */
7010 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
7011 longjmp(cstate
->top_ctx
, 1);
7018 * Convert a string in the form PPP-PPP, which correspond to ports, to
7019 * a starting and ending port in a port range.
7022 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
7023 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
7026 const char *first
, *second
;
7027 size_t first_size
, second_size
;
7030 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
7031 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
7034 * Make sure there are no other hyphens.
7036 * XXX - we support named ports, but there are some port names
7037 * in /etc/services that include hyphens, so this would rule
7040 if (strchr(hyphen_off
+ 1, '-') != NULL
)
7041 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
7045 * Get the length of the first port.
7048 first_size
= hyphen_off
- string
;
7049 if (first_size
== 0) {
7050 /* Range of "-port", which we don't support. */
7051 bpf_error(cstate
, "port range '%s' has no starting port", string
);
7055 * Try to convert it to a port.
7057 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
7058 save_proto
= *proto
;
7061 * Get the length of the second port.
7063 second
= hyphen_off
+ 1;
7064 second_size
= strlen(second
);
7065 if (second_size
== 0) {
7066 /* Range of "port-", which we don't support. */
7067 bpf_error(cstate
, "port range '%s' has no ending port", string
);
7071 * Try to convert it to a port.
7073 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
7074 if (*proto
!= save_proto
)
7075 *proto
= PROTO_UNDEF
;
7079 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
7081 int proto
= q
.proto
;
7085 bpf_u_int32 mask
, addr
;
7086 struct addrinfo
*res
, *res0
;
7087 struct sockaddr_in
*sin4
;
7090 struct sockaddr_in6
*sin6
;
7091 struct in6_addr mask128
;
7093 struct block
*b
, *tmp
;
7094 int port
, real_proto
;
7095 bpf_u_int32 port1
, port2
;
7098 * Catch errors reported by us and routines below us, and return NULL
7101 if (setjmp(cstate
->top_ctx
))
7107 addr
= pcap_nametonetaddr(name
);
7109 bpf_error(cstate
, "unknown network '%s'", name
);
7110 /* Left justify network addr and calculate its network mask */
7112 while (addr
&& (addr
& 0xff000000) == 0) {
7116 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7120 if (proto
== Q_LINK
) {
7121 switch (cstate
->linktype
) {
7124 case DLT_NETANALYZER
:
7125 case DLT_NETANALYZER_TRANSPARENT
:
7126 eaddr
= pcap_ether_hostton(name
);
7129 "unknown ether host '%s'", name
);
7130 tmp
= gen_prevlinkhdr_check(cstate
);
7131 b
= gen_ehostop(cstate
, eaddr
, dir
);
7138 eaddr
= pcap_ether_hostton(name
);
7141 "unknown FDDI host '%s'", name
);
7142 b
= gen_fhostop(cstate
, eaddr
, dir
);
7147 eaddr
= pcap_ether_hostton(name
);
7150 "unknown token ring host '%s'", name
);
7151 b
= gen_thostop(cstate
, eaddr
, dir
);
7155 case DLT_IEEE802_11
:
7156 case DLT_PRISM_HEADER
:
7157 case DLT_IEEE802_11_RADIO_AVS
:
7158 case DLT_IEEE802_11_RADIO
:
7160 eaddr
= pcap_ether_hostton(name
);
7163 "unknown 802.11 host '%s'", name
);
7164 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7168 case DLT_IP_OVER_FC
:
7169 eaddr
= pcap_ether_hostton(name
);
7172 "unknown Fibre Channel host '%s'", name
);
7173 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7178 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7179 } else if (proto
== Q_DECNET
) {
7181 * A long time ago on Ultrix libpcap supported
7182 * translation of DECnet host names into DECnet
7183 * addresses, but this feature is history now.
7185 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7188 memset(&mask128
, 0xff, sizeof(mask128
));
7190 res0
= res
= pcap_nametoaddrinfo(name
);
7192 bpf_error(cstate
, "unknown host '%s'", name
);
7199 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7200 tproto
== Q_DEFAULT
) {
7206 for (res
= res0
; res
; res
= res
->ai_next
) {
7207 switch (res
->ai_family
) {
7210 if (tproto
== Q_IPV6
)
7214 sin4
= (struct sockaddr_in
*)
7216 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7217 0xffffffff, tproto
, dir
, q
.addr
);
7221 if (tproto6
== Q_IP
)
7224 sin6
= (struct sockaddr_in6
*)
7226 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7227 &mask128
, tproto6
, dir
, q
.addr
);
7240 bpf_error(cstate
, "unknown host '%s'%s", name
,
7241 (proto
== Q_DEFAULT
)
7243 : " for specified address family");
7249 if (proto
!= Q_DEFAULT
&&
7250 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7251 bpf_error(cstate
, "illegal qualifier of 'port'");
7252 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7253 bpf_error(cstate
, "unknown port '%s'", name
);
7254 if (proto
== Q_UDP
) {
7255 if (real_proto
== IPPROTO_TCP
)
7256 bpf_error(cstate
, "port '%s' is tcp", name
);
7257 else if (real_proto
== IPPROTO_SCTP
)
7258 bpf_error(cstate
, "port '%s' is sctp", name
);
7260 /* override PROTO_UNDEF */
7261 real_proto
= IPPROTO_UDP
;
7263 if (proto
== Q_TCP
) {
7264 if (real_proto
== IPPROTO_UDP
)
7265 bpf_error(cstate
, "port '%s' is udp", name
);
7267 else if (real_proto
== IPPROTO_SCTP
)
7268 bpf_error(cstate
, "port '%s' is sctp", name
);
7270 /* override PROTO_UNDEF */
7271 real_proto
= IPPROTO_TCP
;
7273 if (proto
== Q_SCTP
) {
7274 if (real_proto
== IPPROTO_UDP
)
7275 bpf_error(cstate
, "port '%s' is udp", name
);
7277 else if (real_proto
== IPPROTO_TCP
)
7278 bpf_error(cstate
, "port '%s' is tcp", name
);
7280 /* override PROTO_UNDEF */
7281 real_proto
= IPPROTO_SCTP
;
7284 bpf_error(cstate
, "illegal port number %d < 0", port
);
7286 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7287 b
= gen_port(cstate
, port
, real_proto
, dir
);
7288 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7292 if (proto
!= Q_DEFAULT
&&
7293 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7294 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7295 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7296 if (proto
== Q_UDP
) {
7297 if (real_proto
== IPPROTO_TCP
)
7298 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7299 else if (real_proto
== IPPROTO_SCTP
)
7300 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7302 /* override PROTO_UNDEF */
7303 real_proto
= IPPROTO_UDP
;
7305 if (proto
== Q_TCP
) {
7306 if (real_proto
== IPPROTO_UDP
)
7307 bpf_error(cstate
, "port in range '%s' is udp", name
);
7308 else if (real_proto
== IPPROTO_SCTP
)
7309 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7311 /* override PROTO_UNDEF */
7312 real_proto
= IPPROTO_TCP
;
7314 if (proto
== Q_SCTP
) {
7315 if (real_proto
== IPPROTO_UDP
)
7316 bpf_error(cstate
, "port in range '%s' is udp", name
);
7317 else if (real_proto
== IPPROTO_TCP
)
7318 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7320 /* override PROTO_UNDEF */
7321 real_proto
= IPPROTO_SCTP
;
7324 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7326 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7328 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7329 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7334 eaddr
= pcap_ether_hostton(name
);
7336 bpf_error(cstate
, "unknown ether host: %s", name
);
7338 res
= pcap_nametoaddrinfo(name
);
7341 bpf_error(cstate
, "unknown host '%s'", name
);
7342 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7347 bpf_error(cstate
, "unknown host '%s'", name
);
7350 bpf_error(cstate
, "'gateway' not supported in this configuration");
7354 real_proto
= lookup_proto(cstate
, name
, proto
);
7355 if (real_proto
>= 0)
7356 return gen_proto(cstate
, real_proto
, proto
, dir
);
7358 bpf_error(cstate
, "unknown protocol: %s", name
);
7360 #if !defined(NO_PROTOCHAIN)
7362 real_proto
= lookup_proto(cstate
, name
, proto
);
7363 if (real_proto
>= 0)
7364 return gen_protochain(cstate
, real_proto
, proto
);
7366 bpf_error(cstate
, "unknown protocol: %s", name
);
7367 #endif /* !defined(NO_PROTOCHAIN) */
7378 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7379 bpf_u_int32 masklen
, struct qual q
)
7381 register int nlen
, mlen
;
7386 * Catch errors reported by us and routines below us, and return NULL
7389 if (setjmp(cstate
->top_ctx
))
7392 nlen
= pcapint_atoin(s1
, &n
);
7394 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7395 /* Promote short ipaddr */
7399 mlen
= pcapint_atoin(s2
, &m
);
7401 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7402 /* Promote short ipaddr */
7405 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7408 /* Convert mask len to mask */
7410 bpf_error(cstate
, "mask length must be <= 32");
7411 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7412 m
= (bpf_u_int32
)m64
;
7414 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7421 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7424 bpf_error(cstate
, "Mask syntax for networks only");
7431 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7439 * Catch errors reported by us and routines below us, and return NULL
7442 if (setjmp(cstate
->top_ctx
))
7449 * v contains a 32-bit unsigned parsed from a string of the
7450 * form {N}, which could be decimal, hexadecimal or octal.
7451 * Although it would be possible to use the value as a raw
7452 * 16-bit DECnet address when the value fits into 16 bits, this
7453 * would be a questionable feature: DECnet address wire
7454 * encoding is little-endian, so this would not work as
7455 * intuitively as the same works for [big-endian] IPv4
7456 * addresses (0x01020304 means 1.2.3.4).
7458 if (proto
== Q_DECNET
)
7459 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7461 } else if (proto
== Q_DECNET
) {
7463 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7464 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7465 * for a valid DECnet address.
7467 vlen
= pcapint_atodn(s
, &v
);
7469 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7472 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7473 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7476 vlen
= pcapint_atoin(s
, &v
);
7478 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7486 if (proto
== Q_DECNET
)
7487 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7488 else if (proto
== Q_LINK
) {
7489 bpf_error(cstate
, "illegal link layer address");
7492 if (s
== NULL
&& q
.addr
== Q_NET
) {
7493 /* Promote short net number */
7494 while (v
&& (v
& 0xff000000) == 0) {
7499 /* Promote short ipaddr */
7501 mask
<<= 32 - vlen
;
7503 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7508 proto
= IPPROTO_UDP
;
7509 else if (proto
== Q_TCP
)
7510 proto
= IPPROTO_TCP
;
7511 else if (proto
== Q_SCTP
)
7512 proto
= IPPROTO_SCTP
;
7513 else if (proto
== Q_DEFAULT
)
7514 proto
= PROTO_UNDEF
;
7516 bpf_error(cstate
, "illegal qualifier of 'port'");
7519 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7523 b
= gen_port(cstate
, v
, proto
, dir
);
7524 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7530 proto
= IPPROTO_UDP
;
7531 else if (proto
== Q_TCP
)
7532 proto
= IPPROTO_TCP
;
7533 else if (proto
== Q_SCTP
)
7534 proto
= IPPROTO_SCTP
;
7535 else if (proto
== Q_DEFAULT
)
7536 proto
= PROTO_UNDEF
;
7538 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7541 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7545 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7546 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7551 bpf_error(cstate
, "'gateway' requires a name");
7555 return gen_proto(cstate
, v
, proto
, dir
);
7557 #if !defined(NO_PROTOCHAIN)
7559 return gen_protochain(cstate
, v
, proto
);
7575 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7578 struct addrinfo
*res
;
7579 struct in6_addr
*addr
;
7580 struct in6_addr mask
;
7582 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7585 * Catch errors reported by us and routines below us, and return NULL
7588 if (setjmp(cstate
->top_ctx
))
7591 res
= pcap_nametoaddrinfo(s
);
7593 bpf_error(cstate
, "invalid ip6 address %s", s
);
7596 bpf_error(cstate
, "%s resolved to multiple address", s
);
7597 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7599 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7600 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7601 memset(&mask
, 0, sizeof(mask
));
7602 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7604 mask
.s6_addr
[masklen
/ 8] =
7605 (0xff << (8 - masklen
% 8)) & 0xff;
7608 memcpy(a
, addr
, sizeof(a
));
7609 memcpy(m
, &mask
, sizeof(m
));
7610 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7611 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7612 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7620 bpf_error(cstate
, "Mask syntax for networks only");
7624 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7630 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7637 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7639 struct block
*b
, *tmp
;
7642 * Catch errors reported by us and routines below us, and return NULL
7645 if (setjmp(cstate
->top_ctx
))
7648 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7649 cstate
->e
= pcap_ether_aton(s
);
7650 if (cstate
->e
== NULL
)
7651 bpf_error(cstate
, "malloc");
7652 switch (cstate
->linktype
) {
7654 case DLT_NETANALYZER
:
7655 case DLT_NETANALYZER_TRANSPARENT
:
7656 tmp
= gen_prevlinkhdr_check(cstate
);
7657 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7662 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7665 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7667 case DLT_IEEE802_11
:
7668 case DLT_PRISM_HEADER
:
7669 case DLT_IEEE802_11_RADIO_AVS
:
7670 case DLT_IEEE802_11_RADIO
:
7672 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7674 case DLT_IP_OVER_FC
:
7675 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7680 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7687 bpf_error(cstate
, "ethernet address used in non-ether expression");
7692 sappend(struct slist
*s0
, struct slist
*s1
)
7695 * This is definitely not the best way to do this, but the
7696 * lists will rarely get long.
7703 static struct slist
*
7704 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7708 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7713 static struct slist
*
7714 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7718 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7724 * Modify "index" to use the value stored into its register as an
7725 * offset relative to the beginning of the header for the protocol
7726 * "proto", and allocate a register and put an item "size" bytes long
7727 * (1, 2, or 4) at that offset into that register, making it the register
7730 static struct arth
*
7731 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7735 struct slist
*s
, *tmp
;
7737 int regno
= alloc_reg(cstate
);
7739 free_reg(cstate
, inst
->regno
);
7743 bpf_error(cstate
, "data size must be 1, 2, or 4");
7760 bpf_error(cstate
, "unsupported index operation");
7764 * The offset is relative to the beginning of the packet
7765 * data, if we have a radio header. (If we don't, this
7768 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7769 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7770 cstate
->linktype
!= DLT_PRISM_HEADER
)
7771 bpf_error(cstate
, "radio information not present in capture");
7774 * Load into the X register the offset computed into the
7775 * register specified by "index".
7777 s
= xfer_to_x(cstate
, inst
);
7780 * Load the item at that offset.
7782 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7784 sappend(inst
->s
, s
);
7789 * The offset is relative to the beginning of
7790 * the link-layer header.
7792 * XXX - what about ATM LANE? Should the index be
7793 * relative to the beginning of the AAL5 frame, so
7794 * that 0 refers to the beginning of the LE Control
7795 * field, or relative to the beginning of the LAN
7796 * frame, so that 0 refers, for Ethernet LANE, to
7797 * the beginning of the destination address?
7799 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7802 * If "s" is non-null, it has code to arrange that the
7803 * X register contains the length of the prefix preceding
7804 * the link-layer header. Add to it the offset computed
7805 * into the register specified by "index", and move that
7806 * into the X register. Otherwise, just load into the X
7807 * register the offset computed into the register specified
7811 sappend(s
, xfer_to_a(cstate
, inst
));
7812 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7813 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7815 s
= xfer_to_x(cstate
, inst
);
7818 * Load the item at the sum of the offset we've put in the
7819 * X register and the offset of the start of the link
7820 * layer header (which is 0 if the radio header is
7821 * variable-length; that header length is what we put
7822 * into the X register and then added to the index).
7824 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7825 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7827 sappend(inst
->s
, s
);
7841 * The offset is relative to the beginning of
7842 * the network-layer header.
7843 * XXX - are there any cases where we want
7844 * cstate->off_nl_nosnap?
7846 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7849 * If "s" is non-null, it has code to arrange that the
7850 * X register contains the variable part of the offset
7851 * of the link-layer payload. Add to it the offset
7852 * computed into the register specified by "index",
7853 * and move that into the X register. Otherwise, just
7854 * load into the X register the offset computed into
7855 * the register specified by "index".
7858 sappend(s
, xfer_to_a(cstate
, inst
));
7859 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7860 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7862 s
= xfer_to_x(cstate
, inst
);
7865 * Load the item at the sum of the offset we've put in the
7866 * X register, the offset of the start of the network
7867 * layer header from the beginning of the link-layer
7868 * payload, and the constant part of the offset of the
7869 * start of the link-layer payload.
7871 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7872 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7874 sappend(inst
->s
, s
);
7877 * Do the computation only if the packet contains
7878 * the protocol in question.
7880 b
= gen_proto_abbrev_internal(cstate
, proto
);
7882 gen_and(inst
->b
, b
);
7896 * The offset is relative to the beginning of
7897 * the transport-layer header.
7899 * Load the X register with the length of the IPv4 header
7900 * (plus the offset of the link-layer header, if it's
7901 * a variable-length header), in bytes.
7903 * XXX - are there any cases where we want
7904 * cstate->off_nl_nosnap?
7905 * XXX - we should, if we're built with
7906 * IPv6 support, generate code to load either
7907 * IPv4, IPv6, or both, as appropriate.
7909 s
= gen_loadx_iphdrlen(cstate
);
7912 * The X register now contains the sum of the variable
7913 * part of the offset of the link-layer payload and the
7914 * length of the network-layer header.
7916 * Load into the A register the offset relative to
7917 * the beginning of the transport layer header,
7918 * add the X register to that, move that to the
7919 * X register, and load with an offset from the
7920 * X register equal to the sum of the constant part of
7921 * the offset of the link-layer payload and the offset,
7922 * relative to the beginning of the link-layer payload,
7923 * of the network-layer header.
7925 sappend(s
, xfer_to_a(cstate
, inst
));
7926 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7927 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7928 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7929 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7930 sappend(inst
->s
, s
);
7933 * Do the computation only if the packet contains
7934 * the protocol in question - which is true only
7935 * if this is an IP datagram and is the first or
7936 * only fragment of that datagram.
7938 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7940 gen_and(inst
->b
, b
);
7941 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7946 * Do the computation only if the packet contains
7947 * the protocol in question.
7949 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7951 gen_and(inst
->b
, b
);
7955 * Check if we have an icmp6 next header
7957 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7959 gen_and(inst
->b
, b
);
7962 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7964 * If "s" is non-null, it has code to arrange that the
7965 * X register contains the variable part of the offset
7966 * of the link-layer payload. Add to it the offset
7967 * computed into the register specified by "index",
7968 * and move that into the X register. Otherwise, just
7969 * load into the X register the offset computed into
7970 * the register specified by "index".
7973 sappend(s
, xfer_to_a(cstate
, inst
));
7974 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7975 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7977 s
= xfer_to_x(cstate
, inst
);
7980 * Load the item at the sum of the offset we've put in the
7981 * X register, the offset of the start of the network
7982 * layer header from the beginning of the link-layer
7983 * payload, and the constant part of the offset of the
7984 * start of the link-layer payload.
7986 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7987 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7990 sappend(inst
->s
, s
);
7994 inst
->regno
= regno
;
7995 s
= new_stmt(cstate
, BPF_ST
);
7997 sappend(inst
->s
, s
);
8003 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
8007 * Catch errors reported by us and routines below us, and return NULL
8010 if (setjmp(cstate
->top_ctx
))
8013 return gen_load_internal(cstate
, proto
, inst
, size
);
8016 static struct block
*
8017 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8018 struct arth
*a1
, int reversed
)
8020 struct slist
*s0
, *s1
, *s2
;
8021 struct block
*b
, *tmp
;
8023 s0
= xfer_to_x(cstate
, a1
);
8024 s1
= xfer_to_a(cstate
, a0
);
8025 if (code
== BPF_JEQ
) {
8026 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
8027 b
= new_block(cstate
, JMP(code
));
8031 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
8037 sappend(a0
->s
, a1
->s
);
8041 free_reg(cstate
, a0
->regno
);
8042 free_reg(cstate
, a1
->regno
);
8044 /* 'and' together protocol checks */
8047 gen_and(a0
->b
, tmp
= a1
->b
);
8061 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8062 struct arth
*a1
, int reversed
)
8065 * Catch errors reported by us and routines below us, and return NULL
8068 if (setjmp(cstate
->top_ctx
))
8071 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
8075 gen_loadlen(compiler_state_t
*cstate
)
8082 * Catch errors reported by us and routines below us, and return NULL
8085 if (setjmp(cstate
->top_ctx
))
8088 regno
= alloc_reg(cstate
);
8089 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8090 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8091 s
->next
= new_stmt(cstate
, BPF_ST
);
8092 s
->next
->s
.k
= regno
;
8099 static struct arth
*
8100 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8106 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8108 reg
= alloc_reg(cstate
);
8110 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8112 s
->next
= new_stmt(cstate
, BPF_ST
);
8121 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8124 * Catch errors reported by us and routines below us, and return NULL
8127 if (setjmp(cstate
->top_ctx
))
8130 return gen_loadi_internal(cstate
, val
);
8134 * The a_arg dance is to avoid annoying whining by compilers that
8135 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8136 * It's not *used* after setjmp returns.
8139 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8141 struct arth
*a
= a_arg
;
8145 * Catch errors reported by us and routines below us, and return NULL
8148 if (setjmp(cstate
->top_ctx
))
8151 s
= xfer_to_a(cstate
, a
);
8153 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8156 s
= new_stmt(cstate
, BPF_ST
);
8164 * The a0_arg dance is to avoid annoying whining by compilers that
8165 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8166 * It's not *used* after setjmp returns.
8169 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8172 struct arth
*a0
= a0_arg
;
8173 struct slist
*s0
, *s1
, *s2
;
8176 * Catch errors reported by us and routines below us, and return NULL
8179 if (setjmp(cstate
->top_ctx
))
8183 * Disallow division by, or modulus by, zero; we do this here
8184 * so that it gets done even if the optimizer is disabled.
8186 * Also disallow shifts by a value greater than 31; we do this
8187 * here, for the same reason.
8189 if (code
== BPF_DIV
) {
8190 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8191 bpf_error(cstate
, "division by zero");
8192 } else if (code
== BPF_MOD
) {
8193 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8194 bpf_error(cstate
, "modulus by zero");
8195 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8196 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8197 bpf_error(cstate
, "shift by more than 31 bits");
8199 s0
= xfer_to_x(cstate
, a1
);
8200 s1
= xfer_to_a(cstate
, a0
);
8201 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8206 sappend(a0
->s
, a1
->s
);
8208 free_reg(cstate
, a0
->regno
);
8209 free_reg(cstate
, a1
->regno
);
8211 s0
= new_stmt(cstate
, BPF_ST
);
8212 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8219 * Initialize the table of used registers and the current register.
8222 init_regs(compiler_state_t
*cstate
)
8225 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8229 * Return the next free register.
8232 alloc_reg(compiler_state_t
*cstate
)
8234 int n
= BPF_MEMWORDS
;
8237 if (cstate
->regused
[cstate
->curreg
])
8238 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8240 cstate
->regused
[cstate
->curreg
] = 1;
8241 return cstate
->curreg
;
8244 bpf_error(cstate
, "too many registers needed to evaluate expression");
8249 * Return a register to the table so it can
8253 free_reg(compiler_state_t
*cstate
, int n
)
8255 cstate
->regused
[n
] = 0;
8258 static struct block
*
8259 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8264 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8265 b
= new_block(cstate
, JMP(jmp
));
8273 gen_greater(compiler_state_t
*cstate
, int n
)
8276 * Catch errors reported by us and routines below us, and return NULL
8279 if (setjmp(cstate
->top_ctx
))
8282 return gen_len(cstate
, BPF_JGE
, n
);
8286 * Actually, this is less than or equal.
8289 gen_less(compiler_state_t
*cstate
, int n
)
8294 * Catch errors reported by us and routines below us, and return NULL
8297 if (setjmp(cstate
->top_ctx
))
8300 b
= gen_len(cstate
, BPF_JGT
, n
);
8307 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8308 * the beginning of the link-layer header.
8309 * XXX - that means you can't test values in the radiotap header, but
8310 * as that header is difficult if not impossible to parse generally
8311 * without a loop, that might not be a severe problem. A new keyword
8312 * "radio" could be added for that, although what you'd really want
8313 * would be a way of testing particular radio header values, which
8314 * would generate code appropriate to the radio header in question.
8317 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8323 * Catch errors reported by us and routines below us, and return NULL
8326 if (setjmp(cstate
->top_ctx
))
8334 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8337 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8341 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8345 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8349 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8353 b
= new_block(cstate
, JMP(BPF_JEQ
));
8361 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8363 bpf_u_int32 hostmask
;
8364 struct block
*b0
, *b1
, *b2
;
8365 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8368 * Catch errors reported by us and routines below us, and return NULL
8371 if (setjmp(cstate
->top_ctx
))
8378 switch (cstate
->linktype
) {
8380 case DLT_ARCNET_LINUX
:
8381 // ARCnet broadcast is [8-bit] destination address 0.
8382 return gen_ahostop(cstate
, 0, Q_DST
);
8384 case DLT_NETANALYZER
:
8385 case DLT_NETANALYZER_TRANSPARENT
:
8386 b1
= gen_prevlinkhdr_check(cstate
);
8387 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8392 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8394 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8395 case DLT_IEEE802_11
:
8396 case DLT_PRISM_HEADER
:
8397 case DLT_IEEE802_11_RADIO_AVS
:
8398 case DLT_IEEE802_11_RADIO
:
8400 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8401 case DLT_IP_OVER_FC
:
8402 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8404 bpf_error(cstate
, "not a broadcast link");
8410 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8411 * as an indication that we don't know the netmask, and fail
8414 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8415 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8416 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8417 hostmask
= ~cstate
->netmask
;
8418 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8419 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8424 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8429 * Generate code to test the low-order bit of a MAC address (that's
8430 * the bottom bit of the *first* byte).
8432 static struct block
*
8433 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8435 register struct block
*b0
;
8436 register struct slist
*s
;
8438 /* link[offset] & 1 != 0 */
8439 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8440 b0
= new_block(cstate
, JMP(BPF_JSET
));
8447 gen_multicast(compiler_state_t
*cstate
, int proto
)
8449 register struct block
*b0
, *b1
, *b2
;
8450 register struct slist
*s
;
8453 * Catch errors reported by us and routines below us, and return NULL
8456 if (setjmp(cstate
->top_ctx
))
8463 switch (cstate
->linktype
) {
8465 case DLT_ARCNET_LINUX
:
8466 // ARCnet multicast is the same as broadcast.
8467 return gen_ahostop(cstate
, 0, Q_DST
);
8469 case DLT_NETANALYZER
:
8470 case DLT_NETANALYZER_TRANSPARENT
:
8471 b1
= gen_prevlinkhdr_check(cstate
);
8472 /* ether[0] & 1 != 0 */
8473 b0
= gen_mac_multicast(cstate
, 0);
8479 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8481 * XXX - was that referring to bit-order issues?
8483 /* fddi[1] & 1 != 0 */
8484 return gen_mac_multicast(cstate
, 1);
8486 /* tr[2] & 1 != 0 */
8487 return gen_mac_multicast(cstate
, 2);
8488 case DLT_IEEE802_11
:
8489 case DLT_PRISM_HEADER
:
8490 case DLT_IEEE802_11_RADIO_AVS
:
8491 case DLT_IEEE802_11_RADIO
:
8496 * For control frames, there is no DA.
8498 * For management frames, DA is at an
8499 * offset of 4 from the beginning of
8502 * For data frames, DA is at an offset
8503 * of 4 from the beginning of the packet
8504 * if To DS is clear and at an offset of
8505 * 16 from the beginning of the packet
8510 * Generate the tests to be done for data frames.
8512 * First, check for To DS set, i.e. "link[1] & 0x01".
8514 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8515 b1
= new_block(cstate
, JMP(BPF_JSET
));
8516 b1
->s
.k
= 0x01; /* To DS */
8520 * If To DS is set, the DA is at 16.
8522 b0
= gen_mac_multicast(cstate
, 16);
8526 * Now, check for To DS not set, i.e. check
8527 * "!(link[1] & 0x01)".
8529 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8530 b2
= new_block(cstate
, JMP(BPF_JSET
));
8531 b2
->s
.k
= 0x01; /* To DS */
8536 * If To DS is not set, the DA is at 4.
8538 b1
= gen_mac_multicast(cstate
, 4);
8542 * Now OR together the last two checks. That gives
8543 * the complete set of checks for data frames.
8548 * Now check for a data frame.
8549 * I.e, check "link[0] & 0x08".
8551 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8552 b1
= new_block(cstate
, JMP(BPF_JSET
));
8557 * AND that with the checks done for data frames.
8562 * If the high-order bit of the type value is 0, this
8563 * is a management frame.
8564 * I.e, check "!(link[0] & 0x08)".
8566 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8567 b2
= new_block(cstate
, JMP(BPF_JSET
));
8573 * For management frames, the DA is at 4.
8575 b1
= gen_mac_multicast(cstate
, 4);
8579 * OR that with the checks done for data frames.
8580 * That gives the checks done for management and
8586 * If the low-order bit of the type value is 1,
8587 * this is either a control frame or a frame
8588 * with a reserved type, and thus not a
8591 * I.e., check "!(link[0] & 0x04)".
8593 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8594 b1
= new_block(cstate
, JMP(BPF_JSET
));
8600 * AND that with the checks for data and management
8605 case DLT_IP_OVER_FC
:
8606 b0
= gen_mac_multicast(cstate
, 2);
8611 /* Link not known to support multicasts */
8615 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8616 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8621 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8622 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8626 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8632 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8633 * we can look at special meta-data in the filter expression; otherwise we
8634 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8635 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8636 * pcap_activate() conditionally sets.
8639 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8641 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8643 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8645 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8650 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8652 register struct block
*b0
;
8655 * Catch errors reported by us and routines below us, and return NULL
8658 if (setjmp(cstate
->top_ctx
))
8662 * Only some data link types support ifindex qualifiers.
8664 switch (cstate
->linktype
) {
8665 case DLT_LINUX_SLL2
:
8666 /* match packets on this interface */
8667 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8670 #if defined(__linux__)
8671 require_basic_bpf_extensions(cstate
, "ifindex");
8673 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8675 #else /* defined(__linux__) */
8676 bpf_error(cstate
, "ifindex not supported on %s",
8677 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8679 #endif /* defined(__linux__) */
8685 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8686 * Outbound traffic is sent by this machine, while inbound traffic is
8687 * sent by a remote machine (and may include packets destined for a
8688 * unicast or multicast link-layer address we are not subscribing to).
8689 * These are the same definitions implemented by pcap_setdirection().
8690 * Capturing only unicast traffic destined for this host is probably
8691 * better accomplished using a higher-layer filter.
8694 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8696 register struct block
*b0
;
8699 * Catch errors reported by us and routines below us, and return NULL
8702 if (setjmp(cstate
->top_ctx
))
8706 * Only some data link types support inbound/outbound qualifiers.
8708 switch (cstate
->linktype
) {
8710 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8711 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8715 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8716 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8720 /* match outgoing packets */
8721 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8723 /* to filter on inbound traffic, invert the match */
8728 case DLT_LINUX_SLL2
:
8729 /* match outgoing packets */
8730 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8732 /* to filter on inbound traffic, invert the match */
8738 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8739 outbound
? PF_OUT
: PF_IN
);
8743 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8746 case DLT_JUNIPER_MFR
:
8747 case DLT_JUNIPER_MLFR
:
8748 case DLT_JUNIPER_MLPPP
:
8749 case DLT_JUNIPER_ATM1
:
8750 case DLT_JUNIPER_ATM2
:
8751 case DLT_JUNIPER_PPPOE
:
8752 case DLT_JUNIPER_PPPOE_ATM
:
8753 case DLT_JUNIPER_GGSN
:
8754 case DLT_JUNIPER_ES
:
8755 case DLT_JUNIPER_MONITOR
:
8756 case DLT_JUNIPER_SERVICES
:
8757 case DLT_JUNIPER_ETHER
:
8758 case DLT_JUNIPER_PPP
:
8759 case DLT_JUNIPER_FRELAY
:
8760 case DLT_JUNIPER_CHDLC
:
8761 case DLT_JUNIPER_VP
:
8762 case DLT_JUNIPER_ST
:
8763 case DLT_JUNIPER_ISM
:
8764 case DLT_JUNIPER_VS
:
8765 case DLT_JUNIPER_SRX_E2E
:
8766 case DLT_JUNIPER_FIBRECHANNEL
:
8767 case DLT_JUNIPER_ATM_CEMIC
:
8768 /* juniper flags (including direction) are stored
8769 * the byte after the 3-byte magic number */
8770 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8775 * If we have packet meta-data indicating a direction,
8776 * and that metadata can be checked by BPF code, check
8777 * it. Otherwise, give up, as this link-layer type has
8778 * nothing in the packet data.
8780 * Currently, the only platform where a BPF filter can
8781 * check that metadata is Linux with the in-kernel
8782 * BPF interpreter. If other packet capture mechanisms
8783 * and BPF filters also supported this, it would be
8784 * nice. It would be even better if they made that
8785 * metadata available so that we could provide it
8786 * with newer capture APIs, allowing it to be saved
8789 #if defined(__linux__)
8790 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8791 /* match outgoing packets */
8792 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8795 /* to filter on inbound traffic, invert the match */
8798 #else /* defined(__linux__) */
8799 bpf_error(cstate
, "inbound/outbound not supported on %s",
8800 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8802 #endif /* defined(__linux__) */
8807 /* PF firewall log matched interface */
8809 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8815 * Catch errors reported by us and routines below us, and return NULL
8818 if (setjmp(cstate
->top_ctx
))
8821 if (cstate
->linktype
!= DLT_PFLOG
) {
8822 bpf_error(cstate
, "ifname supported only on PF linktype");
8825 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8826 off
= offsetof(struct pfloghdr
, ifname
);
8827 if (strlen(ifname
) >= len
) {
8828 bpf_error(cstate
, "ifname interface names can only be %d characters",
8832 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8833 (const u_char
*)ifname
);
8837 /* PF firewall log ruleset name */
8839 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8844 * Catch errors reported by us and routines below us, and return NULL
8847 if (setjmp(cstate
->top_ctx
))
8850 if (cstate
->linktype
!= DLT_PFLOG
) {
8851 bpf_error(cstate
, "ruleset supported only on PF linktype");
8855 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8856 bpf_error(cstate
, "ruleset names can only be %ld characters",
8857 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8861 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8862 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8866 /* PF firewall log rule number */
8868 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8873 * Catch errors reported by us and routines below us, and return NULL
8876 if (setjmp(cstate
->top_ctx
))
8879 if (cstate
->linktype
!= DLT_PFLOG
) {
8880 bpf_error(cstate
, "rnr supported only on PF linktype");
8884 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8889 /* PF firewall log sub-rule number */
8891 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8896 * Catch errors reported by us and routines below us, and return NULL
8899 if (setjmp(cstate
->top_ctx
))
8902 if (cstate
->linktype
!= DLT_PFLOG
) {
8903 bpf_error(cstate
, "srnr supported only on PF linktype");
8907 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8912 /* PF firewall log reason code */
8914 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8919 * Catch errors reported by us and routines below us, and return NULL
8922 if (setjmp(cstate
->top_ctx
))
8925 if (cstate
->linktype
!= DLT_PFLOG
) {
8926 bpf_error(cstate
, "reason supported only on PF linktype");
8930 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8931 (bpf_u_int32
)reason
);
8935 /* PF firewall log action */
8937 gen_pf_action(compiler_state_t
*cstate
, int action
)
8942 * Catch errors reported by us and routines below us, and return NULL
8945 if (setjmp(cstate
->top_ctx
))
8948 if (cstate
->linktype
!= DLT_PFLOG
) {
8949 bpf_error(cstate
, "action supported only on PF linktype");
8953 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8954 (bpf_u_int32
)action
);
8958 /* IEEE 802.11 wireless header */
8960 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8965 * Catch errors reported by us and routines below us, and return NULL
8968 if (setjmp(cstate
->top_ctx
))
8971 switch (cstate
->linktype
) {
8973 case DLT_IEEE802_11
:
8974 case DLT_PRISM_HEADER
:
8975 case DLT_IEEE802_11_RADIO_AVS
:
8976 case DLT_IEEE802_11_RADIO
:
8978 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8982 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8990 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8995 * Catch errors reported by us and routines below us, and return NULL
8998 if (setjmp(cstate
->top_ctx
))
9001 switch (cstate
->linktype
) {
9003 case DLT_IEEE802_11
:
9004 case DLT_PRISM_HEADER
:
9005 case DLT_IEEE802_11_RADIO_AVS
:
9006 case DLT_IEEE802_11_RADIO
:
9011 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
9015 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
9016 IEEE80211_FC1_DIR_MASK
);
9021 // Process an ARCnet host address string.
9023 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
9026 * Catch errors reported by us and routines below us, and return NULL
9029 if (setjmp(cstate
->top_ctx
))
9032 switch (cstate
->linktype
) {
9035 case DLT_ARCNET_LINUX
:
9036 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
9037 q
.proto
== Q_LINK
) {
9040 * The lexer currently defines the address format in a
9041 * way that makes this error condition never true.
9042 * Let's check it anyway in case this part of the lexer
9043 * changes in future.
9045 if (! pcapint_atoan(s
, &addr
))
9046 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
9047 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
9049 bpf_error(cstate
, "ARCnet address used in non-arc expression");
9053 bpf_error(cstate
, "aid supported only on ARCnet");
9058 // Compare an ARCnet host address with the given value.
9059 static struct block
*
9060 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
9062 register struct block
*b0
, *b1
;
9066 * ARCnet is different from Ethernet: the source address comes before
9067 * the destination address, each is one byte long. This holds for all
9068 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
9069 * in the 1983 edition of the "ARCNET Designer's Handbook" published
9070 * by Datapoint (document number 61610-01).
9073 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
9076 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
9079 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9080 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9086 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9087 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9092 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9096 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9100 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9104 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9108 bpf_error(cstate
, "'ra' is only supported on 802.11");
9112 bpf_error(cstate
, "'ta' is only supported on 802.11");
9119 static struct block
*
9120 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9122 struct block
*b0
, *b1
;
9124 /* check for VLAN, including 802.1ad and QinQ */
9125 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9126 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9129 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9135 static struct block
*
9136 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9138 if (vlan_num
> 0x0fff) {
9139 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9142 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9145 static struct block
*
9146 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9149 struct block
*b0
, *b1
;
9151 b0
= gen_vlan_tpid_test(cstate
);
9154 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9160 * Both payload and link header type follow the VLAN tags so that
9161 * both need to be updated.
9163 cstate
->off_linkpl
.constant_part
+= 4;
9164 cstate
->off_linktype
.constant_part
+= 4;
9169 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9170 /* add v to variable part of off */
9172 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9173 bpf_u_int32 v
, struct slist
*s
)
9177 if (!off
->is_variable
)
9178 off
->is_variable
= 1;
9180 off
->reg
= alloc_reg(cstate
);
9182 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9185 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9188 s2
= new_stmt(cstate
, BPF_ST
);
9194 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9195 * and link type offsets first
9198 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9202 /* offset determined at run time, shift variable part */
9204 cstate
->is_vlan_vloffset
= 1;
9205 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9206 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9208 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9209 sappend(s
.next
, b_tpid
->head
->stmts
);
9210 b_tpid
->head
->stmts
= s
.next
;
9214 * patch block b_vid (VLAN id test) to load VID value either from packet
9215 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9218 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9220 struct slist
*s
, *s2
, *sjeq
;
9223 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9224 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9226 /* true -> next instructions, false -> beginning of b_vid */
9227 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9229 sjeq
->s
.jf
= b_vid
->stmts
;
9232 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
9233 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
9237 /* Jump to the test in b_vid. We need to jump one instruction before
9238 * the end of the b_vid block so that we only skip loading the TCI
9239 * from packet data and not the 'and' instruction extracting VID.
9242 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9244 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9248 /* insert our statements at the beginning of b_vid */
9249 sappend(s
, b_vid
->stmts
);
9254 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9255 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9256 * tag can be either in metadata or in packet data; therefore if the
9257 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9258 * header for VLAN tag. As the decision is done at run time, we need
9259 * update variable part of the offsets
9261 static struct block
*
9262 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9265 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9268 /* generate new filter code based on extracting packet
9270 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9271 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9273 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9278 * This is tricky. We need to insert the statements updating variable
9279 * parts of offsets before the traditional TPID and VID tests so
9280 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9281 * we do not want this update to affect those checks. That's why we
9282 * generate both test blocks first and insert the statements updating
9283 * variable parts of both offsets after that. This wouldn't work if
9284 * there already were variable length link header when entering this
9285 * function but gen_vlan_bpf_extensions() isn't called in that case.
9287 b_tpid
= gen_vlan_tpid_test(cstate
);
9289 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9291 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9296 gen_vlan_patch_vid_test(cstate
, b_vid
);
9306 * support IEEE 802.1Q VLAN trunk over ethernet
9309 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9314 * Catch errors reported by us and routines below us, and return NULL
9317 if (setjmp(cstate
->top_ctx
))
9320 /* can't check for VLAN-encapsulated packets inside MPLS */
9321 if (cstate
->label_stack_depth
> 0)
9322 bpf_error(cstate
, "no VLAN match after MPLS");
9325 * Check for a VLAN packet, and then change the offsets to point
9326 * to the type and data fields within the VLAN packet. Just
9327 * increment the offsets, so that we can support a hierarchy, e.g.
9328 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9331 * XXX - this is a bit of a kludge. If we were to split the
9332 * compiler into a parser that parses an expression and
9333 * generates an expression tree, and a code generator that
9334 * takes an expression tree (which could come from our
9335 * parser or from some other parser) and generates BPF code,
9336 * we could perhaps make the offsets parameters of routines
9337 * and, in the handler for an "AND" node, pass to subnodes
9338 * other than the VLAN node the adjusted offsets.
9340 * This would mean that "vlan" would, instead of changing the
9341 * behavior of *all* tests after it, change only the behavior
9342 * of tests ANDed with it. That would change the documented
9343 * semantics of "vlan", which might break some expressions.
9344 * However, it would mean that "(vlan and ip) or ip" would check
9345 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9346 * checking only for VLAN-encapsulated IP, so that could still
9347 * be considered worth doing; it wouldn't break expressions
9348 * that are of the form "vlan and ..." or "vlan N and ...",
9349 * which I suspect are the most common expressions involving
9350 * "vlan". "vlan or ..." doesn't necessarily do what the user
9351 * would really want, now, as all the "or ..." tests would
9352 * be done assuming a VLAN, even though the "or" could be viewed
9353 * as meaning "or, if this isn't a VLAN packet...".
9355 switch (cstate
->linktype
) {
9359 * Newer version of the Linux kernel pass around
9360 * packets in which the VLAN tag has been removed
9361 * from the packet data and put into metadata.
9363 * This requires special treatment.
9365 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9366 /* Verify that this is the outer part of the packet and
9367 * not encapsulated somehow. */
9368 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9369 cstate
->off_linkhdr
.constant_part
==
9370 cstate
->off_outermostlinkhdr
.constant_part
) {
9372 * Do we need special VLAN handling?
9374 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9375 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9378 b0
= gen_vlan_no_bpf_extensions(cstate
,
9379 vlan_num
, has_vlan_tag
);
9382 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9386 case DLT_NETANALYZER
:
9387 case DLT_NETANALYZER_TRANSPARENT
:
9388 case DLT_IEEE802_11
:
9389 case DLT_PRISM_HEADER
:
9390 case DLT_IEEE802_11_RADIO_AVS
:
9391 case DLT_IEEE802_11_RADIO
:
9393 * These are either Ethernet packets with an additional
9394 * metadata header (the NetAnalyzer types), or 802.11
9395 * packets, possibly with an additional metadata header.
9397 * For the first of those, the VLAN tag is in the normal
9398 * place, so the special-case handling above isn't
9401 * For the second of those, we don't do the special-case
9404 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9408 bpf_error(cstate
, "no VLAN support for %s",
9409 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9413 cstate
->vlan_stack_depth
++;
9421 * The label_num_arg dance is to avoid annoying whining by compilers that
9422 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9423 * It's not *used* after setjmp returns.
9425 static struct block
*
9426 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9429 struct block
*b0
, *b1
;
9431 if (cstate
->label_stack_depth
> 0) {
9432 /* just match the bottom-of-stack bit clear */
9433 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9436 * We're not in an MPLS stack yet, so check the link-layer
9437 * type against MPLS.
9439 switch (cstate
->linktype
) {
9441 case DLT_C_HDLC
: /* fall through */
9444 case DLT_NETANALYZER
:
9445 case DLT_NETANALYZER_TRANSPARENT
:
9446 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9450 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9453 /* FIXME add other DLT_s ...
9454 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9455 * leave it for now */
9458 bpf_error(cstate
, "no MPLS support for %s",
9459 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9464 /* If a specific MPLS label is requested, check it */
9465 if (has_label_num
) {
9466 if (label_num
> 0xFFFFF) {
9467 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9468 label_num
, 0xFFFFF);
9470 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9471 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9472 0xfffff000); /* only compare the first 20 bits */
9478 * Change the offsets to point to the type and data fields within
9479 * the MPLS packet. Just increment the offsets, so that we
9480 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9481 * capture packets with an outer label of 100000 and an inner
9484 * Increment the MPLS stack depth as well; this indicates that
9485 * we're checking MPLS-encapsulated headers, to make sure higher
9486 * level code generators don't try to match against IP-related
9487 * protocols such as Q_ARP, Q_RARP etc.
9489 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9491 cstate
->off_nl_nosnap
+= 4;
9492 cstate
->off_nl
+= 4;
9493 cstate
->label_stack_depth
++;
9498 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9501 * Catch errors reported by us and routines below us, and return NULL
9504 if (setjmp(cstate
->top_ctx
))
9507 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9511 * Support PPPOE discovery and session.
9514 gen_pppoed(compiler_state_t
*cstate
)
9517 * Catch errors reported by us and routines below us, and return NULL
9520 if (setjmp(cstate
->top_ctx
))
9523 /* check for PPPoE discovery */
9524 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9528 * RFC 2516 Section 4:
9530 * The Ethernet payload for PPPoE is as follows:
9533 * 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
9534 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9535 * | VER | TYPE | CODE | SESSION_ID |
9536 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9537 * | LENGTH | payload ~
9538 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9541 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9543 struct block
*b0
, *b1
;
9546 * Catch errors reported by us and routines below us, and return NULL
9549 if (setjmp(cstate
->top_ctx
))
9553 * Test against the PPPoE session link-layer type.
9555 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9557 /* If a specific session is requested, check PPPoE session id */
9559 if (sess_num
> UINT16_MAX
) {
9560 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9561 sess_num
, UINT16_MAX
);
9563 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9569 * Change the offsets to point to the type and data fields within
9570 * the PPP packet, and note that this is PPPoE rather than
9573 * XXX - this is a bit of a kludge. See the comments in
9576 * The "network-layer" protocol is PPPoE, which has a 6-byte
9577 * PPPoE header, followed by a PPP packet.
9579 * There is no HDLC encapsulation for the PPP packet (it's
9580 * encapsulated in PPPoES instead), so the link-layer type
9581 * starts at the first byte of the PPP packet. For PPPoE,
9582 * that offset is relative to the beginning of the total
9583 * link-layer payload, including any 802.2 LLC header, so
9584 * it's 6 bytes past cstate->off_nl.
9586 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9587 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9588 cstate
->off_linkpl
.reg
);
9590 cstate
->off_linktype
= cstate
->off_linkhdr
;
9591 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9594 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9599 /* Check that this is Geneve and the VNI is correct if
9600 * specified. Parameterized to handle both IPv4 and IPv6. */
9601 static struct block
*
9602 gen_geneve_check(compiler_state_t
*cstate
,
9603 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9604 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9606 struct block
*b0
, *b1
;
9608 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9610 /* Check that we are operating on version 0. Otherwise, we
9611 * can't decode the rest of the fields. The version is 2 bits
9612 * in the first byte of the Geneve header. */
9613 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9618 if (vni
> 0xffffff) {
9619 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9622 vni
<<= 8; /* VNI is in the upper 3 bytes */
9623 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9631 /* The IPv4 and IPv6 Geneve checks need to do two things:
9632 * - Verify that this actually is Geneve with the right VNI.
9633 * - Place the IP header length (plus variable link prefix if
9634 * needed) into register A to be used later to compute
9635 * the inner packet offsets. */
9636 static struct block
*
9637 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9639 struct block
*b0
, *b1
;
9640 struct slist
*s
, *s1
;
9642 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9644 /* Load the IP header length into A. */
9645 s
= gen_loadx_iphdrlen(cstate
);
9647 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9650 /* Forcibly append these statements to the true condition
9651 * of the protocol check by creating a new block that is
9652 * always true and ANDing them. */
9653 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9662 static struct block
*
9663 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9665 struct block
*b0
, *b1
;
9666 struct slist
*s
, *s1
;
9668 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9670 /* Load the IP header length. We need to account for a
9671 * variable length link prefix if there is one. */
9672 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9674 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9678 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9682 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9686 /* Forcibly append these statements to the true condition
9687 * of the protocol check by creating a new block that is
9688 * always true and ANDing them. */
9689 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9692 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9701 /* We need to store three values based on the Geneve header::
9702 * - The offset of the linktype.
9703 * - The offset of the end of the Geneve header.
9704 * - The offset of the end of the encapsulated MAC header. */
9705 static struct slist
*
9706 gen_geneve_offsets(compiler_state_t
*cstate
)
9708 struct slist
*s
, *s1
, *s_proto
;
9710 /* First we need to calculate the offset of the Geneve header
9711 * itself. This is composed of the IP header previously calculated
9712 * (include any variable link prefix) and stored in A plus the
9713 * fixed sized headers (fixed link prefix, MAC length, and UDP
9715 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9716 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9718 /* Stash this in X since we'll need it later. */
9719 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9722 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9724 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9728 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9729 cstate
->off_linktype
.is_variable
= 1;
9730 cstate
->off_linktype
.constant_part
= 0;
9732 s1
= new_stmt(cstate
, BPF_ST
);
9733 s1
->s
.k
= cstate
->off_linktype
.reg
;
9736 /* Load the Geneve option length and mask and shift to get the
9737 * number of bytes. It is stored in the first byte of the Geneve
9739 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9743 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9747 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9751 /* Add in the rest of the Geneve base header. */
9752 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9756 /* Add the Geneve header length to its offset and store. */
9757 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9761 /* Set the encapsulated type as Ethernet. Even though we may
9762 * not actually have Ethernet inside there are two reasons this
9764 * - The linktype field is always in EtherType format regardless
9765 * of whether it is in Geneve or an inner Ethernet frame.
9766 * - The only link layer that we have specific support for is
9767 * Ethernet. We will confirm that the packet actually is
9768 * Ethernet at runtime before executing these checks. */
9769 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9771 s1
= new_stmt(cstate
, BPF_ST
);
9772 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9775 /* Calculate whether we have an Ethernet header or just raw IP/
9776 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9777 * and linktype by 14 bytes so that the network header can be found
9778 * seamlessly. Otherwise, keep what we've calculated already. */
9780 /* We have a bare jmp so we can't use the optimizer. */
9781 cstate
->no_optimize
= 1;
9783 /* Load the EtherType in the Geneve header, 2 bytes in. */
9784 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9788 /* Load X with the end of the Geneve header. */
9789 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9790 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9793 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9794 * end of this check, we should have the total length in X. In
9795 * the non-Ethernet case, it's already there. */
9796 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9797 s_proto
->s
.k
= ETHERTYPE_TEB
;
9798 sappend(s
, s_proto
);
9800 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9804 /* Since this is Ethernet, use the EtherType of the payload
9805 * directly as the linktype. Overwrite what we already have. */
9806 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9810 s1
= new_stmt(cstate
, BPF_ST
);
9811 s1
->s
.k
= cstate
->off_linktype
.reg
;
9814 /* Advance two bytes further to get the end of the Ethernet
9816 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9820 /* Move the result to X. */
9821 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9824 /* Store the final result of our linkpl calculation. */
9825 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9826 cstate
->off_linkpl
.is_variable
= 1;
9827 cstate
->off_linkpl
.constant_part
= 0;
9829 s1
= new_stmt(cstate
, BPF_STX
);
9830 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9839 /* Check to see if this is a Geneve packet. */
9841 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9843 struct block
*b0
, *b1
;
9847 * Catch errors reported by us and routines below us, and return NULL
9850 if (setjmp(cstate
->top_ctx
))
9853 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9854 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9859 /* Later filters should act on the payload of the Geneve frame,
9860 * update all of the header pointers. Attach this code so that
9861 * it gets executed in the event that the Geneve filter matches. */
9862 s
= gen_geneve_offsets(cstate
);
9864 b1
= gen_true(cstate
);
9865 sappend(s
, b1
->stmts
);
9870 cstate
->is_encap
= 1;
9875 /* Check that this is VXLAN and the VNI is correct if
9876 * specified. Parameterized to handle both IPv4 and IPv6. */
9877 static struct block
*
9878 gen_vxlan_check(compiler_state_t
*cstate
,
9879 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9880 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9882 struct block
*b0
, *b1
;
9884 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9886 /* Check that the VXLAN header has the flag bits set
9888 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9893 if (vni
> 0xffffff) {
9894 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9897 vni
<<= 8; /* VNI is in the upper 3 bytes */
9898 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9906 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9907 * - Verify that this actually is VXLAN with the right VNI.
9908 * - Place the IP header length (plus variable link prefix if
9909 * needed) into register A to be used later to compute
9910 * the inner packet offsets. */
9911 static struct block
*
9912 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9914 struct block
*b0
, *b1
;
9915 struct slist
*s
, *s1
;
9917 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9919 /* Load the IP header length into A. */
9920 s
= gen_loadx_iphdrlen(cstate
);
9922 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9925 /* Forcibly append these statements to the true condition
9926 * of the protocol check by creating a new block that is
9927 * always true and ANDing them. */
9928 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9937 static struct block
*
9938 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9940 struct block
*b0
, *b1
;
9941 struct slist
*s
, *s1
;
9943 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9945 /* Load the IP header length. We need to account for a
9946 * variable length link prefix if there is one. */
9947 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9949 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9953 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9957 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9961 /* Forcibly append these statements to the true condition
9962 * of the protocol check by creating a new block that is
9963 * always true and ANDing them. */
9964 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9967 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9976 /* We need to store three values based on the VXLAN header:
9977 * - The offset of the linktype.
9978 * - The offset of the end of the VXLAN header.
9979 * - The offset of the end of the encapsulated MAC header. */
9980 static struct slist
*
9981 gen_vxlan_offsets(compiler_state_t
*cstate
)
9983 struct slist
*s
, *s1
;
9985 /* Calculate the offset of the VXLAN header itself. This
9986 * includes the IP header computed previously (including any
9987 * variable link prefix) and stored in A plus the fixed size
9988 * headers (fixed link prefix, MAC length, UDP header). */
9989 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9990 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9992 /* Add the VXLAN header length to its offset and store */
9993 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9997 /* Push the link header. VXLAN packets always contain Ethernet
9999 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
10001 s1
= new_stmt(cstate
, BPF_ST
);
10002 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
10005 /* As the payload is an Ethernet packet, we can use the
10006 * EtherType of the payload directly as the linktype. */
10007 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10011 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
10012 cstate
->off_linktype
.is_variable
= 1;
10013 cstate
->off_linktype
.constant_part
= 0;
10015 s1
= new_stmt(cstate
, BPF_ST
);
10016 s1
->s
.k
= cstate
->off_linktype
.reg
;
10019 /* Two bytes further is the end of the Ethernet header and the
10020 * start of the payload. */
10021 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10025 /* Move the result to X. */
10026 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10029 /* Store the final result of our linkpl calculation. */
10030 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
10031 cstate
->off_linkpl
.is_variable
= 1;
10032 cstate
->off_linkpl
.constant_part
= 0;
10034 s1
= new_stmt(cstate
, BPF_STX
);
10035 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10038 cstate
->off_nl
= 0;
10043 /* Check to see if this is a VXLAN packet. */
10045 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
10047 struct block
*b0
, *b1
;
10051 * Catch errors reported by us and routines below us, and return NULL
10054 if (setjmp(cstate
->top_ctx
))
10057 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
10058 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
10063 /* Later filters should act on the payload of the VXLAN frame,
10064 * update all of the header pointers. Attach this code so that
10065 * it gets executed in the event that the VXLAN filter matches. */
10066 s
= gen_vxlan_offsets(cstate
);
10068 b1
= gen_true(cstate
);
10069 sappend(s
, b1
->stmts
);
10074 cstate
->is_encap
= 1;
10079 /* Check that the encapsulated frame has a link layer header
10080 * for Ethernet filters. */
10081 static struct block
*
10082 gen_encap_ll_check(compiler_state_t
*cstate
)
10085 struct slist
*s
, *s1
;
10087 /* The easiest way to see if there is a link layer present
10088 * is to check if the link layer header and payload are not
10091 /* Geneve always generates pure variable offsets so we can
10092 * compare only the registers. */
10093 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
10094 s
->s
.k
= cstate
->off_linkhdr
.reg
;
10096 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
10097 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10100 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10108 static struct block
*
10109 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
10110 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10114 switch (atmfield
) {
10117 if (!cstate
->is_atm
)
10118 bpf_error(cstate
, "'vpi' supported only on raw ATM");
10119 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10121 if (jvalue
> UINT8_MAX
)
10122 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
10123 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10124 0xffffffffU
, jtype
, reverse
, jvalue
);
10128 if (!cstate
->is_atm
)
10129 bpf_error(cstate
, "'vci' supported only on raw ATM");
10130 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10132 if (jvalue
> UINT16_MAX
)
10133 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
10134 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10135 0xffffffffU
, jtype
, reverse
, jvalue
);
10139 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10140 abort(); /* XXX - this isn't on FreeBSD */
10141 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10142 0x0fU
, jtype
, reverse
, jvalue
);
10146 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10148 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10149 0xffffffffU
, jtype
, reverse
, jvalue
);
10158 static struct block
*
10159 gen_atmtype_metac(compiler_state_t
*cstate
)
10161 struct block
*b0
, *b1
;
10163 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10164 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10169 static struct block
*
10170 gen_atmtype_sc(compiler_state_t
*cstate
)
10172 struct block
*b0
, *b1
;
10174 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10175 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10180 static struct block
*
10181 gen_atmtype_llc(compiler_state_t
*cstate
)
10185 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10186 cstate
->linktype
= cstate
->prevlinktype
;
10191 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10192 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10195 * Catch errors reported by us and routines below us, and return NULL
10198 if (setjmp(cstate
->top_ctx
))
10201 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10206 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10208 struct block
*b0
, *b1
;
10211 * Catch errors reported by us and routines below us, and return NULL
10214 if (setjmp(cstate
->top_ctx
))
10220 /* Get all packets in Meta signalling Circuit */
10221 if (!cstate
->is_atm
)
10222 bpf_error(cstate
, "'metac' supported only on raw ATM");
10223 b1
= gen_atmtype_metac(cstate
);
10227 /* Get all packets in Broadcast Circuit*/
10228 if (!cstate
->is_atm
)
10229 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10230 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10231 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10236 /* Get all cells in Segment OAM F4 circuit*/
10237 if (!cstate
->is_atm
)
10238 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10239 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10240 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10245 /* Get all cells in End-to-End OAM F4 Circuit*/
10246 if (!cstate
->is_atm
)
10247 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10248 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10249 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10254 /* Get all packets in connection Signalling Circuit */
10255 if (!cstate
->is_atm
)
10256 bpf_error(cstate
, "'sc' supported only on raw ATM");
10257 b1
= gen_atmtype_sc(cstate
);
10261 /* Get all packets in ILMI Circuit */
10262 if (!cstate
->is_atm
)
10263 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10264 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10265 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10270 /* Get all LANE packets */
10271 if (!cstate
->is_atm
)
10272 bpf_error(cstate
, "'lane' supported only on raw ATM");
10273 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10276 * Arrange that all subsequent tests assume LANE
10277 * rather than LLC-encapsulated packets, and set
10278 * the offsets appropriately for LANE-encapsulated
10281 * We assume LANE means Ethernet, not Token Ring.
10283 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10284 cstate
->off_payload
+ 2, /* Ethernet header */
10286 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10287 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10288 cstate
->off_nl
= 0; /* Ethernet II */
10289 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10299 * Filtering for MTP2 messages based on li value
10300 * FISU, length is null
10301 * LSSU, length is 1 or 2
10302 * MSU, length is 3 or more
10303 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10306 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10308 struct block
*b0
, *b1
;
10311 * Catch errors reported by us and routines below us, and return NULL
10314 if (setjmp(cstate
->top_ctx
))
10320 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10321 (cstate
->linktype
!= DLT_ERF
) &&
10322 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10323 bpf_error(cstate
, "'fisu' supported only on MTP2");
10324 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10325 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10326 0x3fU
, BPF_JEQ
, 0, 0U);
10330 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10331 (cstate
->linktype
!= DLT_ERF
) &&
10332 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10333 bpf_error(cstate
, "'lssu' supported only on MTP2");
10334 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10335 0x3fU
, BPF_JGT
, 1, 2U);
10336 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10337 0x3fU
, BPF_JGT
, 0, 0U);
10342 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10343 (cstate
->linktype
!= DLT_ERF
) &&
10344 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10345 bpf_error(cstate
, "'msu' supported only on MTP2");
10346 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10347 0x3fU
, BPF_JGT
, 0, 2U);
10351 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10352 (cstate
->linktype
!= DLT_ERF
) &&
10353 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10354 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10355 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10356 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10357 0xff80U
, BPF_JEQ
, 0, 0U);
10361 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10362 (cstate
->linktype
!= DLT_ERF
) &&
10363 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10364 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10365 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10366 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10367 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10368 0xff80U
, BPF_JGT
, 0, 0U);
10373 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10374 (cstate
->linktype
!= DLT_ERF
) &&
10375 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10376 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10377 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10378 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10388 * These maximum valid values are all-ones, so they double as the bitmasks
10389 * before any bitwise shifting.
10391 #define MTP2_SIO_MAXVAL UINT8_MAX
10392 #define MTP3_PC_MAXVAL 0x3fffU
10393 #define MTP3_SLS_MAXVAL 0xfU
10395 static struct block
*
10396 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10397 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10405 newoff_sio
= cstate
->off_sio
;
10406 newoff_opc
= cstate
->off_opc
;
10407 newoff_dpc
= cstate
->off_dpc
;
10408 newoff_sls
= cstate
->off_sls
;
10409 switch (mtp3field
) {
10412 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10414 * SIO is the simplest field: the size is one byte and the offset is a
10415 * multiple of bytes, so the only detail to get right is the value of
10416 * the [right-to-left] field offset.
10419 newoff_sio
+= 3; /* offset for MTP2_HSL */
10423 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10424 bpf_error(cstate
, "'sio' supported only on SS7");
10425 if(jvalue
> MTP2_SIO_MAXVAL
)
10426 bpf_error(cstate
, "sio value %u too big; max value = %u",
10427 jvalue
, MTP2_SIO_MAXVAL
);
10428 // Here the bitmask means "do not apply a bitmask".
10429 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10430 jtype
, reverse
, jvalue
);
10434 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10436 * SLS, OPC and DPC are more complicated: none of these is sized in a
10437 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10438 * diagrams are meant to be read right-to-left. This means in the
10439 * diagrams within individual fields and concatenations thereof
10440 * bitwise shifts and masks can be noted in the common left-to-right
10441 * manner until each final value is ready to be byte-swapped and
10442 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10443 * similar problem in a similar way.
10445 * Offsets of fields within the packet header always have the
10446 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10447 * DLTs the offset does not include the F (Flag) field at the
10448 * beginning of each message.
10450 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10451 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10452 * be tested entirely using a single BPF_W comparison. In this case
10453 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10454 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10455 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10456 * correlates with the [RTL] packet diagram until the byte-swapping is
10459 * The code below uses this approach for OPC, which spans 3 bytes.
10460 * DPC and SLS use shorter loads, SLS also uses a different offset.
10467 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10468 bpf_error(cstate
, "'opc' supported only on SS7");
10469 if (jvalue
> MTP3_PC_MAXVAL
)
10470 bpf_error(cstate
, "opc value %u too big; max value = %u",
10471 jvalue
, MTP3_PC_MAXVAL
);
10472 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10473 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10474 SWAPLONG(jvalue
<< 14));
10482 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10483 bpf_error(cstate
, "'dpc' supported only on SS7");
10484 if (jvalue
> MTP3_PC_MAXVAL
)
10485 bpf_error(cstate
, "dpc value %u too big; max value = %u",
10486 jvalue
, MTP3_PC_MAXVAL
);
10487 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10488 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10489 SWAPSHORT(jvalue
));
10497 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10498 bpf_error(cstate
, "'sls' supported only on SS7");
10499 if (jvalue
> MTP3_SLS_MAXVAL
)
10500 bpf_error(cstate
, "sls value %u too big; max value = %u",
10501 jvalue
, MTP3_SLS_MAXVAL
);
10502 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10503 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10514 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10515 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10518 * Catch errors reported by us and routines below us, and return NULL
10521 if (setjmp(cstate
->top_ctx
))
10524 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10528 static struct block
*
10529 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10534 * Q.2931 signalling protocol messages for handling virtual circuits
10535 * establishment and teardown
10540 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10543 case A_CALLPROCEED
:
10544 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10548 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10552 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10556 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10559 case A_RELEASE_DONE
:
10560 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10570 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10572 struct block
*b0
, *b1
;
10575 * Catch errors reported by us and routines below us, and return NULL
10578 if (setjmp(cstate
->top_ctx
))
10584 if (!cstate
->is_atm
)
10585 bpf_error(cstate
, "'oam' supported only on raw ATM");
10587 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10588 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10590 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10595 if (!cstate
->is_atm
)
10596 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10598 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10599 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10601 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10607 * Get Q.2931 signalling messages for switched
10608 * virtual connection
10610 if (!cstate
->is_atm
)
10611 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10612 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10613 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10615 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10617 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10619 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10621 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10623 b0
= gen_atmtype_sc(cstate
);
10627 case A_METACONNECT
:
10628 if (!cstate
->is_atm
)
10629 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10630 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10631 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10633 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10635 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10637 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10639 b0
= gen_atmtype_metac(cstate
);