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
,
662 int, bpf_u_int32
, u_int
, u_int
);
664 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
665 struct in6_addr
*, int, bpf_u_int32
, 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_check_802_11_data_frame(compiler_state_t
*);
712 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
714 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
715 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
716 bpf_u_int32
, int, int);
717 static struct block
*gen_atmtype_llc(compiler_state_t
*);
718 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
721 initchunks(compiler_state_t
*cstate
)
725 for (i
= 0; i
< NCHUNKS
; i
++) {
726 cstate
->chunks
[i
].n_left
= 0;
727 cstate
->chunks
[i
].m
= NULL
;
729 cstate
->cur_chunk
= 0;
733 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
739 /* Round up to chunk alignment. */
740 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
742 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
743 if (n
> cp
->n_left
) {
745 k
= ++cstate
->cur_chunk
;
747 bpf_set_error(cstate
, "out of memory");
750 size
= CHUNK0SIZE
<< k
;
751 cp
->m
= (void *)malloc(size
);
753 bpf_set_error(cstate
, "out of memory");
756 memset((char *)cp
->m
, 0, size
);
759 bpf_set_error(cstate
, "out of memory");
764 return (void *)((char *)cp
->m
+ cp
->n_left
);
768 newchunk(compiler_state_t
*cstate
, size_t n
)
772 p
= newchunk_nolongjmp(cstate
, n
);
774 longjmp(cstate
->top_ctx
, 1);
781 freechunks(compiler_state_t
*cstate
)
785 for (i
= 0; i
< NCHUNKS
; ++i
)
786 if (cstate
->chunks
[i
].m
!= NULL
)
787 free(cstate
->chunks
[i
].m
);
791 * A strdup whose allocations are freed after code generation is over.
792 * This is used by the lexical analyzer, so it can't longjmp; it just
793 * returns NULL on an allocation error, and the callers must check
797 sdup(compiler_state_t
*cstate
, const char *s
)
799 size_t n
= strlen(s
) + 1;
800 char *cp
= newchunk_nolongjmp(cstate
, n
);
804 pcapint_strlcpy(cp
, s
, n
);
808 static inline struct block
*
809 new_block(compiler_state_t
*cstate
, int code
)
813 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
820 static inline struct slist
*
821 new_stmt(compiler_state_t
*cstate
, int code
)
825 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
831 static struct block
*
832 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
834 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
840 static struct block
*
841 gen_retblk(compiler_state_t
*cstate
, int v
)
843 if (setjmp(cstate
->top_ctx
)) {
845 * gen_retblk() only fails because a memory
846 * allocation failed in newchunk(), meaning
847 * that it can't return a pointer.
853 return gen_retblk_internal(cstate
, v
);
856 static inline PCAP_NORETURN_DEF
void
857 syntax(compiler_state_t
*cstate
)
859 bpf_error(cstate
, "syntax error in filter expression");
863 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
864 const char *buf
, int optimize
, bpf_u_int32 mask
)
870 compiler_state_t cstate
;
871 yyscan_t scanner
= NULL
;
872 YY_BUFFER_STATE in_buffer
= NULL
;
877 * If this pcap_t hasn't been activated, it doesn't have a
878 * link-layer type, so we can't use it.
881 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
882 "not-yet-activated pcap_t passed to pcap_compile");
888 * Initialize Winsock, asking for the latest version (2.2),
889 * as we may be calling Winsock routines to translate
890 * host names to addresses.
892 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
894 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
895 err
, "Error calling WSAStartup()");
902 * If the device on which we're capturing need to be notified
903 * that a new filter is being compiled, do so.
905 * This allows them to save a copy of it, in case, for example,
906 * they're implementing a form of remote packet capture, and
907 * want the remote machine to filter out the packets in which
908 * it's sending the packets it's captured.
910 * XXX - the fact that we happen to be compiling a filter
911 * doesn't necessarily mean we'll be installing it as the
912 * filter for this pcap_t; we might be running it from userland
913 * on captured packets to do packet classification. We really
914 * need a better way of handling this, but this is all that
915 * the WinPcap remote capture code did.
917 if (p
->save_current_filter_op
!= NULL
)
918 (p
->save_current_filter_op
)(p
, buf
);
922 cstate
.no_optimize
= 0;
927 cstate
.ic
.root
= NULL
;
928 cstate
.ic
.cur_mark
= 0;
930 cstate
.error_set
= 0;
933 cstate
.netmask
= mask
;
935 cstate
.snaplen
= pcap_snapshot(p
);
936 if (cstate
.snaplen
== 0) {
937 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
938 "snaplen of 0 rejects all packets");
943 if (pcap_lex_init(&scanner
) != 0) {
944 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
945 errno
, "can't initialize scanner");
949 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
952 * Associate the compiler state with the lexical analyzer
955 pcap_set_extra(&cstate
, scanner
);
957 if (init_linktype(&cstate
, p
) == -1) {
961 if (pcap_parse(scanner
, &cstate
) != 0) {
963 if (cstate
.ai
!= NULL
)
964 freeaddrinfo(cstate
.ai
);
966 if (cstate
.e
!= NULL
)
972 if (cstate
.ic
.root
== NULL
) {
973 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
976 * Catch errors reported by gen_retblk().
978 if (cstate
.ic
.root
== NULL
) {
984 if (optimize
&& !cstate
.no_optimize
) {
985 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
990 if (cstate
.ic
.root
== NULL
||
991 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
992 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
993 "expression rejects all packets");
998 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
999 cstate
.ic
.root
, &len
, p
->errbuf
);
1000 if (program
->bf_insns
== NULL
) {
1005 program
->bf_len
= len
;
1007 rc
= 0; /* We're all okay */
1011 * Clean up everything for the lexical analyzer.
1013 if (in_buffer
!= NULL
)
1014 pcap__delete_buffer(in_buffer
, scanner
);
1015 if (scanner
!= NULL
)
1016 pcap_lex_destroy(scanner
);
1019 * Clean up our own allocated memory.
1021 freechunks(&cstate
);
1031 * entry point for using the compiler with no pcap open
1032 * pass in all the stuff that is needed explicitly instead.
1035 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1036 struct bpf_program
*program
,
1037 const char *buf
, int optimize
, bpf_u_int32 mask
)
1042 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1044 return (PCAP_ERROR
);
1045 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1051 * Clean up a "struct bpf_program" by freeing all the memory allocated
1055 pcap_freecode(struct bpf_program
*program
)
1057 program
->bf_len
= 0;
1058 if (program
->bf_insns
!= NULL
) {
1059 free((char *)program
->bf_insns
);
1060 program
->bf_insns
= NULL
;
1065 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1066 * which of the jt and jf fields has been resolved and which is a pointer
1067 * back to another unresolved block (or nil). At least one of the fields
1068 * in each block is already resolved.
1071 backpatch(struct block
*list
, struct block
*target
)
1088 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1089 * which of jt and jf is the link.
1092 merge(struct block
*b0
, struct block
*b1
)
1094 register struct block
**p
= &b0
;
1096 /* Find end of list. */
1098 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1100 /* Concatenate the lists. */
1105 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1107 struct block
*ppi_dlt_check
;
1110 * Catch errors reported by us and routines below us, and return -1
1113 if (setjmp(cstate
->top_ctx
))
1117 * Insert before the statements of the first (root) block any
1118 * statements needed to load the lengths of any variable-length
1119 * headers into registers.
1121 * XXX - a fancier strategy would be to insert those before the
1122 * statements of all blocks that use those lengths and that
1123 * have no predecessors that use them, so that we only compute
1124 * the lengths if we need them. There might be even better
1125 * approaches than that.
1127 * However, those strategies would be more complicated, and
1128 * as we don't generate code to compute a length if the
1129 * program has no tests that use the length, and as most
1130 * tests will probably use those lengths, we would just
1131 * postpone computing the lengths so that it's not done
1132 * for tests that fail early, and it's not clear that's
1135 insert_compute_vloffsets(cstate
, p
->head
);
1138 * For DLT_PPI captures, generate a check of the per-packet
1139 * DLT value to make sure it's DLT_IEEE802_11.
1141 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1142 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1143 * with appropriate Ethernet information and use that rather
1144 * than using something such as DLT_PPI where you don't know
1145 * the link-layer header type until runtime, which, in the
1146 * general case, would force us to generate both Ethernet *and*
1147 * 802.11 code (*and* anything else for which PPI is used)
1148 * and choose between them early in the BPF program?
1150 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
1151 if (ppi_dlt_check
!= NULL
)
1152 gen_and(ppi_dlt_check
, p
);
1154 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1155 p
->sense
= !p
->sense
;
1156 backpatch(p
, gen_retblk_internal(cstate
, 0));
1157 cstate
->ic
.root
= p
->head
;
1162 gen_and(struct block
*b0
, struct block
*b1
)
1164 backpatch(b0
, b1
->head
);
1165 b0
->sense
= !b0
->sense
;
1166 b1
->sense
= !b1
->sense
;
1168 b1
->sense
= !b1
->sense
;
1169 b1
->head
= b0
->head
;
1173 gen_or(struct block
*b0
, struct block
*b1
)
1175 b0
->sense
= !b0
->sense
;
1176 backpatch(b0
, b1
->head
);
1177 b0
->sense
= !b0
->sense
;
1179 b1
->head
= b0
->head
;
1183 gen_not(struct block
*b
)
1185 b
->sense
= !b
->sense
;
1188 static struct block
*
1189 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1190 u_int size
, bpf_u_int32 v
)
1192 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1195 static struct block
*
1196 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1197 u_int size
, bpf_u_int32 v
)
1199 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1202 static struct block
*
1203 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1204 u_int size
, bpf_u_int32 v
)
1206 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1209 static struct block
*
1210 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1211 u_int size
, bpf_u_int32 v
)
1213 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1216 static struct block
*
1217 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1218 u_int size
, bpf_u_int32 v
)
1220 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1223 static struct block
*
1224 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1225 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1227 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1230 static struct block
*
1231 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1232 u_int size
, const u_char
*v
)
1234 register struct block
*b
, *tmp
;
1238 register const u_char
*p
= &v
[size
- 4];
1240 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1248 register const u_char
*p
= &v
[size
- 2];
1250 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1258 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1267 * AND the field of size "size" at offset "offset" relative to the header
1268 * specified by "offrel" with "mask", and compare it with the value "v"
1269 * with the test specified by "jtype"; if "reverse" is true, the test
1270 * should test the opposite of "jtype".
1272 static struct block
*
1273 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1274 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1277 struct slist
*s
, *s2
;
1280 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1282 if (mask
!= 0xffffffff) {
1283 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1288 b
= new_block(cstate
, JMP(jtype
));
1297 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1299 cstate
->pcap_fddipad
= p
->fddipad
;
1302 * We start out with only one link-layer header.
1304 cstate
->outermostlinktype
= pcap_datalink(p
);
1305 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1306 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1307 cstate
->off_outermostlinkhdr
.reg
= -1;
1309 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1310 cstate
->off_prevlinkhdr
.constant_part
= 0;
1311 cstate
->off_prevlinkhdr
.is_variable
= 0;
1312 cstate
->off_prevlinkhdr
.reg
= -1;
1314 cstate
->linktype
= cstate
->outermostlinktype
;
1315 cstate
->off_linkhdr
.constant_part
= 0;
1316 cstate
->off_linkhdr
.is_variable
= 0;
1317 cstate
->off_linkhdr
.reg
= -1;
1322 cstate
->off_linkpl
.constant_part
= 0;
1323 cstate
->off_linkpl
.is_variable
= 0;
1324 cstate
->off_linkpl
.reg
= -1;
1326 cstate
->off_linktype
.constant_part
= 0;
1327 cstate
->off_linktype
.is_variable
= 0;
1328 cstate
->off_linktype
.reg
= -1;
1331 * Assume it's not raw ATM with a pseudo-header, for now.
1334 cstate
->off_vpi
= OFFSET_NOT_SET
;
1335 cstate
->off_vci
= OFFSET_NOT_SET
;
1336 cstate
->off_proto
= OFFSET_NOT_SET
;
1337 cstate
->off_payload
= OFFSET_NOT_SET
;
1340 * And not encapsulated with either Geneve or VXLAN.
1342 cstate
->is_encap
= 0;
1345 * No variable length VLAN offset by default
1347 cstate
->is_vlan_vloffset
= 0;
1350 * And assume we're not doing SS7.
1352 cstate
->off_li
= OFFSET_NOT_SET
;
1353 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1354 cstate
->off_sio
= OFFSET_NOT_SET
;
1355 cstate
->off_opc
= OFFSET_NOT_SET
;
1356 cstate
->off_dpc
= OFFSET_NOT_SET
;
1357 cstate
->off_sls
= OFFSET_NOT_SET
;
1359 cstate
->label_stack_depth
= 0;
1360 cstate
->vlan_stack_depth
= 0;
1362 switch (cstate
->linktype
) {
1365 cstate
->off_linktype
.constant_part
= 2;
1366 cstate
->off_linkpl
.constant_part
= 6;
1367 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1368 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1371 case DLT_ARCNET_LINUX
:
1372 cstate
->off_linktype
.constant_part
= 4;
1373 cstate
->off_linkpl
.constant_part
= 8;
1374 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1375 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1379 cstate
->off_linktype
.constant_part
= 12;
1380 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1381 cstate
->off_nl
= 0; /* Ethernet II */
1382 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1387 * SLIP doesn't have a link level type. The 16 byte
1388 * header is hacked into our SLIP driver.
1390 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1391 cstate
->off_linkpl
.constant_part
= 16;
1393 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1396 case DLT_SLIP_BSDOS
:
1397 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1398 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1400 cstate
->off_linkpl
.constant_part
= 24;
1402 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1407 cstate
->off_linktype
.constant_part
= 0;
1408 cstate
->off_linkpl
.constant_part
= 4;
1410 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1414 cstate
->off_linktype
.constant_part
= 0;
1415 cstate
->off_linkpl
.constant_part
= 12;
1417 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1422 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1423 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1424 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1425 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1426 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1428 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1433 * This does no include the Ethernet header, and
1434 * only covers session state.
1436 cstate
->off_linktype
.constant_part
= 6;
1437 cstate
->off_linkpl
.constant_part
= 8;
1439 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1443 cstate
->off_linktype
.constant_part
= 5;
1444 cstate
->off_linkpl
.constant_part
= 24;
1446 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1451 * FDDI doesn't really have a link-level type field.
1452 * We set "off_linktype" to the offset of the LLC header.
1454 * To check for Ethernet types, we assume that SSAP = SNAP
1455 * is being used and pick out the encapsulated Ethernet type.
1456 * XXX - should we generate code to check for SNAP?
1458 cstate
->off_linktype
.constant_part
= 13;
1459 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1460 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1461 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1462 cstate
->off_nl
= 8; /* 802.2+SNAP */
1463 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1468 * Token Ring doesn't really have a link-level type field.
1469 * We set "off_linktype" to the offset of the LLC header.
1471 * To check for Ethernet types, we assume that SSAP = SNAP
1472 * is being used and pick out the encapsulated Ethernet type.
1473 * XXX - should we generate code to check for SNAP?
1475 * XXX - the header is actually variable-length.
1476 * Some various Linux patched versions gave 38
1477 * as "off_linktype" and 40 as "off_nl"; however,
1478 * if a token ring packet has *no* routing
1479 * information, i.e. is not source-routed, the correct
1480 * values are 20 and 22, as they are in the vanilla code.
1482 * A packet is source-routed iff the uppermost bit
1483 * of the first byte of the source address, at an
1484 * offset of 8, has the uppermost bit set. If the
1485 * packet is source-routed, the total number of bytes
1486 * of routing information is 2 plus bits 0x1F00 of
1487 * the 16-bit value at an offset of 14 (shifted right
1488 * 8 - figure out which byte that is).
1490 cstate
->off_linktype
.constant_part
= 14;
1491 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1492 cstate
->off_nl
= 8; /* 802.2+SNAP */
1493 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1496 case DLT_PRISM_HEADER
:
1497 case DLT_IEEE802_11_RADIO_AVS
:
1498 case DLT_IEEE802_11_RADIO
:
1499 cstate
->off_linkhdr
.is_variable
= 1;
1500 /* Fall through, 802.11 doesn't have a variable link
1501 * prefix but is otherwise the same. */
1504 case DLT_IEEE802_11
:
1506 * 802.11 doesn't really have a link-level type field.
1507 * We set "off_linktype.constant_part" to the offset of
1510 * To check for Ethernet types, we assume that SSAP = SNAP
1511 * is being used and pick out the encapsulated Ethernet type.
1512 * XXX - should we generate code to check for SNAP?
1514 * We also handle variable-length radio headers here.
1515 * The Prism header is in theory variable-length, but in
1516 * practice it's always 144 bytes long. However, some
1517 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1518 * sometimes or always supply an AVS header, so we
1519 * have to check whether the radio header is a Prism
1520 * header or an AVS header, so, in practice, it's
1523 cstate
->off_linktype
.constant_part
= 24;
1524 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1525 cstate
->off_linkpl
.is_variable
= 1;
1526 cstate
->off_nl
= 8; /* 802.2+SNAP */
1527 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1532 * At the moment we treat PPI the same way that we treat
1533 * normal Radiotap encoded packets. The difference is in
1534 * the function that generates the code at the beginning
1535 * to compute the header length. Since this code generator
1536 * of PPI supports bare 802.11 encapsulation only (i.e.
1537 * the encapsulated DLT should be DLT_IEEE802_11) we
1538 * generate code to check for this too.
1540 cstate
->off_linktype
.constant_part
= 24;
1541 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1542 cstate
->off_linkpl
.is_variable
= 1;
1543 cstate
->off_linkhdr
.is_variable
= 1;
1544 cstate
->off_nl
= 8; /* 802.2+SNAP */
1545 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1548 case DLT_ATM_RFC1483
:
1549 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1551 * assume routed, non-ISO PDUs
1552 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1554 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1555 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1556 * latter would presumably be treated the way PPPoE
1557 * should be, so you can do "pppoe and udp port 2049"
1558 * or "pppoa and tcp port 80" and have it check for
1559 * PPPo{A,E} and a PPP protocol of IP and....
1561 cstate
->off_linktype
.constant_part
= 0;
1562 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1563 cstate
->off_nl
= 8; /* 802.2+SNAP */
1564 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1569 * Full Frontal ATM; you get AALn PDUs with an ATM
1573 cstate
->off_vpi
= SUNATM_VPI_POS
;
1574 cstate
->off_vci
= SUNATM_VCI_POS
;
1575 cstate
->off_proto
= PROTO_POS
;
1576 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1577 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1578 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1579 cstate
->off_nl
= 8; /* 802.2+SNAP */
1580 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1586 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1587 cstate
->off_linkpl
.constant_part
= 0;
1589 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1592 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1593 cstate
->off_linktype
.constant_part
= 14;
1594 cstate
->off_linkpl
.constant_part
= 16;
1596 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1599 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1600 cstate
->off_linktype
.constant_part
= 0;
1601 cstate
->off_linkpl
.constant_part
= 20;
1603 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1608 * LocalTalk does have a 1-byte type field in the LLAP header,
1609 * but really it just indicates whether there is a "short" or
1610 * "long" DDP packet following.
1612 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1613 cstate
->off_linkpl
.constant_part
= 0;
1615 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1618 case DLT_IP_OVER_FC
:
1620 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1621 * link-level type field. We set "off_linktype" to the
1622 * offset of the LLC header.
1624 * To check for Ethernet types, we assume that SSAP = SNAP
1625 * is being used and pick out the encapsulated Ethernet type.
1626 * XXX - should we generate code to check for SNAP? RFC
1627 * 2625 says SNAP should be used.
1629 cstate
->off_linktype
.constant_part
= 16;
1630 cstate
->off_linkpl
.constant_part
= 16;
1631 cstate
->off_nl
= 8; /* 802.2+SNAP */
1632 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1637 * XXX - we should set this to handle SNAP-encapsulated
1638 * frames (NLPID of 0x80).
1640 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1641 cstate
->off_linkpl
.constant_part
= 0;
1643 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1647 * the only BPF-interesting FRF.16 frames are non-control frames;
1648 * Frame Relay has a variable length link-layer
1649 * so lets start with offset 4 for now and increments later on (FIXME);
1652 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1653 cstate
->off_linkpl
.constant_part
= 0;
1655 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1658 case DLT_APPLE_IP_OVER_IEEE1394
:
1659 cstate
->off_linktype
.constant_part
= 16;
1660 cstate
->off_linkpl
.constant_part
= 18;
1662 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1665 case DLT_SYMANTEC_FIREWALL
:
1666 cstate
->off_linktype
.constant_part
= 6;
1667 cstate
->off_linkpl
.constant_part
= 44;
1668 cstate
->off_nl
= 0; /* Ethernet II */
1669 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1673 cstate
->off_linktype
.constant_part
= 0;
1674 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1675 cstate
->off_linkpl
.is_variable
= 1;
1677 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1680 case DLT_JUNIPER_MFR
:
1681 case DLT_JUNIPER_MLFR
:
1682 case DLT_JUNIPER_MLPPP
:
1683 case DLT_JUNIPER_PPP
:
1684 case DLT_JUNIPER_CHDLC
:
1685 case DLT_JUNIPER_FRELAY
:
1686 cstate
->off_linktype
.constant_part
= 4;
1687 cstate
->off_linkpl
.constant_part
= 4;
1689 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1692 case DLT_JUNIPER_ATM1
:
1693 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1694 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1696 cstate
->off_nl_nosnap
= 10;
1699 case DLT_JUNIPER_ATM2
:
1700 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1701 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1703 cstate
->off_nl_nosnap
= 10;
1706 /* frames captured on a Juniper PPPoE service PIC
1707 * contain raw ethernet frames */
1708 case DLT_JUNIPER_PPPOE
:
1709 case DLT_JUNIPER_ETHER
:
1710 cstate
->off_linkpl
.constant_part
= 14;
1711 cstate
->off_linktype
.constant_part
= 16;
1712 cstate
->off_nl
= 18; /* Ethernet II */
1713 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1716 case DLT_JUNIPER_PPPOE_ATM
:
1717 cstate
->off_linktype
.constant_part
= 4;
1718 cstate
->off_linkpl
.constant_part
= 6;
1720 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1723 case DLT_JUNIPER_GGSN
:
1724 cstate
->off_linktype
.constant_part
= 6;
1725 cstate
->off_linkpl
.constant_part
= 12;
1727 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1730 case DLT_JUNIPER_ES
:
1731 cstate
->off_linktype
.constant_part
= 6;
1732 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1733 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1734 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1737 case DLT_JUNIPER_MONITOR
:
1738 cstate
->off_linktype
.constant_part
= 12;
1739 cstate
->off_linkpl
.constant_part
= 12;
1740 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1741 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1744 case DLT_BACNET_MS_TP
:
1745 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1746 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1747 cstate
->off_nl
= OFFSET_NOT_SET
;
1748 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1751 case DLT_JUNIPER_SERVICES
:
1752 cstate
->off_linktype
.constant_part
= 12;
1753 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1754 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1755 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1758 case DLT_JUNIPER_VP
:
1759 cstate
->off_linktype
.constant_part
= 18;
1760 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1761 cstate
->off_nl
= OFFSET_NOT_SET
;
1762 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1765 case DLT_JUNIPER_ST
:
1766 cstate
->off_linktype
.constant_part
= 18;
1767 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1768 cstate
->off_nl
= OFFSET_NOT_SET
;
1769 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1772 case DLT_JUNIPER_ISM
:
1773 cstate
->off_linktype
.constant_part
= 8;
1774 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1775 cstate
->off_nl
= OFFSET_NOT_SET
;
1776 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1779 case DLT_JUNIPER_VS
:
1780 case DLT_JUNIPER_SRX_E2E
:
1781 case DLT_JUNIPER_FIBRECHANNEL
:
1782 case DLT_JUNIPER_ATM_CEMIC
:
1783 cstate
->off_linktype
.constant_part
= 8;
1784 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1785 cstate
->off_nl
= OFFSET_NOT_SET
;
1786 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1791 cstate
->off_li_hsl
= 4;
1792 cstate
->off_sio
= 3;
1793 cstate
->off_opc
= 4;
1794 cstate
->off_dpc
= 4;
1795 cstate
->off_sls
= 7;
1796 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1797 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1798 cstate
->off_nl
= OFFSET_NOT_SET
;
1799 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1802 case DLT_MTP2_WITH_PHDR
:
1804 cstate
->off_li_hsl
= 8;
1805 cstate
->off_sio
= 7;
1806 cstate
->off_opc
= 8;
1807 cstate
->off_dpc
= 8;
1808 cstate
->off_sls
= 11;
1809 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1810 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1811 cstate
->off_nl
= OFFSET_NOT_SET
;
1812 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1816 cstate
->off_li
= 22;
1817 cstate
->off_li_hsl
= 24;
1818 cstate
->off_sio
= 23;
1819 cstate
->off_opc
= 24;
1820 cstate
->off_dpc
= 24;
1821 cstate
->off_sls
= 27;
1822 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1823 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1824 cstate
->off_nl
= OFFSET_NOT_SET
;
1825 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1829 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1830 cstate
->off_linkpl
.constant_part
= 4;
1832 cstate
->off_nl_nosnap
= 0;
1837 * Currently, only raw "link[N:M]" filtering is supported.
1839 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1840 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1841 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1842 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1846 cstate
->off_linktype
.constant_part
= 1;
1847 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1849 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1852 case DLT_NETANALYZER
:
1853 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1854 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1855 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1856 cstate
->off_nl
= 0; /* Ethernet II */
1857 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1860 case DLT_NETANALYZER_TRANSPARENT
:
1861 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1862 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1863 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1864 cstate
->off_nl
= 0; /* Ethernet II */
1865 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1870 * For values in the range in which we've assigned new
1871 * DLT_ values, only raw "link[N:M]" filtering is supported.
1873 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1874 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1875 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1876 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1877 cstate
->off_nl
= OFFSET_NOT_SET
;
1878 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1880 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1881 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1887 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1892 * Load a value relative to the specified absolute offset.
1894 static struct slist
*
1895 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1896 u_int offset
, u_int size
)
1898 struct slist
*s
, *s2
;
1900 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1903 * If "s" is non-null, it has code to arrange that the X register
1904 * contains the variable part of the absolute offset, so we
1905 * generate a load relative to that, with an offset of
1906 * abs_offset->constant_part + offset.
1908 * Otherwise, we can do an absolute load with an offset of
1909 * abs_offset->constant_part + offset.
1913 * "s" points to a list of statements that puts the
1914 * variable part of the absolute offset into the X register.
1915 * Do an indirect load, to use the X register as an offset.
1917 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1918 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1922 * There is no variable part of the absolute offset, so
1923 * just do an absolute load.
1925 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1926 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1932 * Load a value relative to the beginning of the specified header.
1934 static struct slist
*
1935 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1938 struct slist
*s
, *s2
;
1941 * Squelch warnings from compilers that *don't* assume that
1942 * offrel always has a valid enum value and therefore don't
1943 * assume that we'll always go through one of the case arms.
1945 * If we have a default case, compilers that *do* assume that
1946 * will then complain about the default case code being
1949 * Damned if you do, damned if you don't.
1956 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1961 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1964 case OR_PREVLINKHDR
:
1965 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1969 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1972 case OR_PREVMPLSHDR
:
1973 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1977 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1980 case OR_LINKPL_NOSNAP
:
1981 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1985 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1990 * Load the X register with the length of the IPv4 header
1991 * (plus the offset of the link-layer header, if it's
1992 * preceded by a variable-length header such as a radio
1993 * header), in bytes.
1995 s
= gen_loadx_iphdrlen(cstate
);
1998 * Load the item at {offset of the link-layer payload} +
1999 * {offset, relative to the start of the link-layer
2000 * payload, of the IPv4 header} + {length of the IPv4 header} +
2001 * {specified offset}.
2003 * If the offset of the link-layer payload is variable,
2004 * the variable part of that offset is included in the
2005 * value in the X register, and we include the constant
2006 * part in the offset of the load.
2008 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2009 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2014 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2021 * Generate code to load into the X register the sum of the length of
2022 * the IPv4 header and the variable part of the offset of the link-layer
2025 static struct slist
*
2026 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2028 struct slist
*s
, *s2
;
2030 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2033 * The offset of the link-layer payload has a variable
2034 * part. "s" points to a list of statements that put
2035 * the variable part of that offset into the X register.
2037 * The 4*([k]&0xf) addressing mode can't be used, as we
2038 * don't have a constant offset, so we have to load the
2039 * value in question into the A register and add to it
2040 * the value from the X register.
2042 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2043 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2045 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2048 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2053 * The A register now contains the length of the IP header.
2054 * We need to add to it the variable part of the offset of
2055 * the link-layer payload, which is still in the X
2056 * register, and move the result into the X register.
2058 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2059 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2062 * The offset of the link-layer payload is a constant,
2063 * so no code was generated to load the (nonexistent)
2064 * variable part of that offset.
2066 * This means we can use the 4*([k]&0xf) addressing
2067 * mode. Load the length of the IPv4 header, which
2068 * is at an offset of cstate->off_nl from the beginning of
2069 * the link-layer payload, and thus at an offset of
2070 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2071 * of the raw packet data, using that addressing mode.
2073 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2074 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2080 static struct block
*
2081 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2086 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2088 b
= new_block(cstate
, JMP(BPF_JEQ
));
2094 static inline struct block
*
2095 gen_true(compiler_state_t
*cstate
)
2097 return gen_uncond(cstate
, 1);
2100 static inline struct block
*
2101 gen_false(compiler_state_t
*cstate
)
2103 return gen_uncond(cstate
, 0);
2107 * Generate code to match a particular packet type.
2109 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2110 * value, if <= ETHERMTU. We use that to determine whether to
2111 * match the type/length field or to check the type/length field for
2112 * a value <= ETHERMTU to see whether it's a type field and then do
2113 * the appropriate test.
2115 static struct block
*
2116 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2118 struct block
*b0
, *b1
;
2124 case LLCSAP_NETBEUI
:
2126 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2127 * so we check the DSAP and SSAP.
2129 * LLCSAP_IP checks for IP-over-802.2, rather
2130 * than IP-over-Ethernet or IP-over-SNAP.
2132 * XXX - should we check both the DSAP and the
2133 * SSAP, like this, or should we check just the
2134 * DSAP, as we do for other types <= ETHERMTU
2135 * (i.e., other SAP values)?
2137 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2139 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2147 * Ethernet_II frames, which are Ethernet
2148 * frames with a frame type of ETHERTYPE_IPX;
2150 * Ethernet_802.3 frames, which are 802.3
2151 * frames (i.e., the type/length field is
2152 * a length field, <= ETHERMTU, rather than
2153 * a type field) with the first two bytes
2154 * after the Ethernet/802.3 header being
2157 * Ethernet_802.2 frames, which are 802.3
2158 * frames with an 802.2 LLC header and
2159 * with the IPX LSAP as the DSAP in the LLC
2162 * Ethernet_SNAP frames, which are 802.3
2163 * frames with an LLC header and a SNAP
2164 * header and with an OUI of 0x000000
2165 * (encapsulated Ethernet) and a protocol
2166 * ID of ETHERTYPE_IPX in the SNAP header.
2168 * XXX - should we generate the same code both
2169 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2173 * This generates code to check both for the
2174 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2176 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2177 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2181 * Now we add code to check for SNAP frames with
2182 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2184 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2188 * Now we generate code to check for 802.3
2189 * frames in general.
2191 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2195 * Now add the check for 802.3 frames before the
2196 * check for Ethernet_802.2 and Ethernet_802.3,
2197 * as those checks should only be done on 802.3
2198 * frames, not on Ethernet frames.
2203 * Now add the check for Ethernet_II frames, and
2204 * do that before checking for the other frame
2207 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2211 case ETHERTYPE_ATALK
:
2212 case ETHERTYPE_AARP
:
2214 * EtherTalk (AppleTalk protocols on Ethernet link
2215 * layer) may use 802.2 encapsulation.
2219 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2220 * we check for an Ethernet type field less than
2221 * 1500, which means it's an 802.3 length field.
2223 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2227 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2228 * SNAP packets with an organization code of
2229 * 0x080007 (Apple, for Appletalk) and a protocol
2230 * type of ETHERTYPE_ATALK (Appletalk).
2232 * 802.2-encapsulated ETHERTYPE_AARP packets are
2233 * SNAP packets with an organization code of
2234 * 0x000000 (encapsulated Ethernet) and a protocol
2235 * type of ETHERTYPE_AARP (Appletalk ARP).
2237 if (ll_proto
== ETHERTYPE_ATALK
)
2238 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2239 else /* ll_proto == ETHERTYPE_AARP */
2240 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2244 * Check for Ethernet encapsulation (Ethertalk
2245 * phase 1?); we just check for the Ethernet
2248 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2254 if (ll_proto
<= ETHERMTU
) {
2256 * This is an LLC SAP value, so the frames
2257 * that match would be 802.2 frames.
2258 * Check that the frame is an 802.2 frame
2259 * (i.e., that the length/type field is
2260 * a length field, <= ETHERMTU) and
2261 * then check the DSAP.
2263 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2265 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2270 * This is an Ethernet type, so compare
2271 * the length/type field with it (if
2272 * the frame is an 802.2 frame, the length
2273 * field will be <= ETHERMTU, and, as
2274 * "ll_proto" is > ETHERMTU, this test
2275 * will fail and the frame won't match,
2276 * which is what we want).
2278 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2283 static struct block
*
2284 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2287 * For DLT_NULL, the link-layer header is a 32-bit word
2288 * containing an AF_ value in *host* byte order, and for
2289 * DLT_ENC, the link-layer header begins with a 32-bit
2290 * word containing an AF_ value in host byte order.
2292 * In addition, if we're reading a saved capture file,
2293 * the host byte order in the capture may not be the
2294 * same as the host byte order on this machine.
2296 * For DLT_LOOP, the link-layer header is a 32-bit
2297 * word containing an AF_ value in *network* byte order.
2299 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2301 * The AF_ value is in host byte order, but the BPF
2302 * interpreter will convert it to network byte order.
2304 * If this is a save file, and it's from a machine
2305 * with the opposite byte order to ours, we byte-swap
2308 * Then we run it through "htonl()", and generate
2309 * code to compare against the result.
2311 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2312 ll_proto
= SWAPLONG(ll_proto
);
2313 ll_proto
= htonl(ll_proto
);
2315 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2319 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2320 * or IPv6 then we have an error.
2322 static struct block
*
2323 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2328 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2331 case ETHERTYPE_IPV6
:
2332 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2339 return gen_false(cstate
);
2343 * Generate code to match a particular packet type.
2345 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2346 * value, if <= ETHERMTU. We use that to determine whether to
2347 * match the type field or to check the type field for the special
2348 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2350 static struct block
*
2351 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2353 struct block
*b0
, *b1
;
2359 case LLCSAP_NETBEUI
:
2361 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2362 * so we check the DSAP and SSAP.
2364 * LLCSAP_IP checks for IP-over-802.2, rather
2365 * than IP-over-Ethernet or IP-over-SNAP.
2367 * XXX - should we check both the DSAP and the
2368 * SSAP, like this, or should we check just the
2369 * DSAP, as we do for other types <= ETHERMTU
2370 * (i.e., other SAP values)?
2372 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2373 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2379 * Ethernet_II frames, which are Ethernet
2380 * frames with a frame type of ETHERTYPE_IPX;
2382 * Ethernet_802.3 frames, which have a frame
2383 * type of LINUX_SLL_P_802_3;
2385 * Ethernet_802.2 frames, which are 802.3
2386 * frames with an 802.2 LLC header (i.e, have
2387 * a frame type of LINUX_SLL_P_802_2) and
2388 * with the IPX LSAP as the DSAP in the LLC
2391 * Ethernet_SNAP frames, which are 802.3
2392 * frames with an LLC header and a SNAP
2393 * header and with an OUI of 0x000000
2394 * (encapsulated Ethernet) and a protocol
2395 * ID of ETHERTYPE_IPX in the SNAP header.
2397 * First, do the checks on LINUX_SLL_P_802_2
2398 * frames; generate the check for either
2399 * Ethernet_802.2 or Ethernet_SNAP frames, and
2400 * then put a check for LINUX_SLL_P_802_2 frames
2403 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2404 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2406 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2410 * Now check for 802.3 frames and OR that with
2411 * the previous test.
2413 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2417 * Now add the check for Ethernet_II frames, and
2418 * do that before checking for the other frame
2421 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2425 case ETHERTYPE_ATALK
:
2426 case ETHERTYPE_AARP
:
2428 * EtherTalk (AppleTalk protocols on Ethernet link
2429 * layer) may use 802.2 encapsulation.
2433 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2434 * we check for the 802.2 protocol type in the
2435 * "Ethernet type" field.
2437 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2440 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2441 * SNAP packets with an organization code of
2442 * 0x080007 (Apple, for Appletalk) and a protocol
2443 * type of ETHERTYPE_ATALK (Appletalk).
2445 * 802.2-encapsulated ETHERTYPE_AARP packets are
2446 * SNAP packets with an organization code of
2447 * 0x000000 (encapsulated Ethernet) and a protocol
2448 * type of ETHERTYPE_AARP (Appletalk ARP).
2450 if (ll_proto
== ETHERTYPE_ATALK
)
2451 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2452 else /* ll_proto == ETHERTYPE_AARP */
2453 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2457 * Check for Ethernet encapsulation (Ethertalk
2458 * phase 1?); we just check for the Ethernet
2461 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2467 if (ll_proto
<= ETHERMTU
) {
2469 * This is an LLC SAP value, so the frames
2470 * that match would be 802.2 frames.
2471 * Check for the 802.2 protocol type
2472 * in the "Ethernet type" field, and
2473 * then check the DSAP.
2475 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2476 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2482 * This is an Ethernet type, so compare
2483 * the length/type field with it (if
2484 * the frame is an 802.2 frame, the length
2485 * field will be <= ETHERMTU, and, as
2486 * "ll_proto" is > ETHERMTU, this test
2487 * will fail and the frame won't match,
2488 * which is what we want).
2490 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2496 * Load a value relative to the beginning of the link-layer header after the
2499 static struct slist
*
2500 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2502 struct slist
*s1
, *s2
;
2505 * Generate code to load the length of the pflog header into
2506 * the register assigned to hold that length, if one has been
2507 * assigned. (If one hasn't been assigned, no code we've
2508 * generated uses that prefix, so we don't need to generate any
2511 if (cstate
->off_linkpl
.reg
!= -1) {
2513 * The length is in the first byte of the header.
2515 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2519 * Round it up to a multiple of 4.
2520 * Add 3, and clear the lower 2 bits.
2522 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2525 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2526 s2
->s
.k
= 0xfffffffc;
2530 * Now allocate a register to hold that value and store
2533 s2
= new_stmt(cstate
, BPF_ST
);
2534 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2538 * Now move it into the X register.
2540 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2548 static struct slist
*
2549 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2551 struct slist
*s1
, *s2
;
2552 struct slist
*sjeq_avs_cookie
;
2553 struct slist
*sjcommon
;
2556 * This code is not compatible with the optimizer, as
2557 * we are generating jmp instructions within a normal
2558 * slist of instructions
2560 cstate
->no_optimize
= 1;
2563 * Generate code to load the length of the radio header into
2564 * the register assigned to hold that length, if one has been
2565 * assigned. (If one hasn't been assigned, no code we've
2566 * generated uses that prefix, so we don't need to generate any
2569 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2570 * or always use the AVS header rather than the Prism header.
2571 * We load a 4-byte big-endian value at the beginning of the
2572 * raw packet data, and see whether, when masked with 0xFFFFF000,
2573 * it's equal to 0x80211000. If so, that indicates that it's
2574 * an AVS header (the masked-out bits are the version number).
2575 * Otherwise, it's a Prism header.
2577 * XXX - the Prism header is also, in theory, variable-length,
2578 * but no known software generates headers that aren't 144
2581 if (cstate
->off_linkhdr
.reg
!= -1) {
2585 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2589 * AND it with 0xFFFFF000.
2591 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2592 s2
->s
.k
= 0xFFFFF000;
2596 * Compare with 0x80211000.
2598 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2599 sjeq_avs_cookie
->s
.k
= 0x80211000;
2600 sappend(s1
, sjeq_avs_cookie
);
2605 * The 4 bytes at an offset of 4 from the beginning of
2606 * the AVS header are the length of the AVS header.
2607 * That field is big-endian.
2609 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2612 sjeq_avs_cookie
->s
.jt
= s2
;
2615 * Now jump to the code to allocate a register
2616 * into which to save the header length and
2617 * store the length there. (The "jump always"
2618 * instruction needs to have the k field set;
2619 * it's added to the PC, so, as we're jumping
2620 * over a single instruction, it should be 1.)
2622 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2624 sappend(s1
, sjcommon
);
2627 * Now for the code that handles the Prism header.
2628 * Just load the length of the Prism header (144)
2629 * into the A register. Have the test for an AVS
2630 * header branch here if we don't have an AVS header.
2632 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2635 sjeq_avs_cookie
->s
.jf
= s2
;
2638 * Now allocate a register to hold that value and store
2639 * it. The code for the AVS header will jump here after
2640 * loading the length of the AVS header.
2642 s2
= new_stmt(cstate
, BPF_ST
);
2643 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2645 sjcommon
->s
.jf
= s2
;
2648 * Now move it into the X register.
2650 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2658 static struct slist
*
2659 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2661 struct slist
*s1
, *s2
;
2664 * Generate code to load the length of the AVS header into
2665 * the register assigned to hold that length, if one has been
2666 * assigned. (If one hasn't been assigned, no code we've
2667 * generated uses that prefix, so we don't need to generate any
2670 if (cstate
->off_linkhdr
.reg
!= -1) {
2672 * The 4 bytes at an offset of 4 from the beginning of
2673 * the AVS header are the length of the AVS header.
2674 * That field is big-endian.
2676 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2680 * Now allocate a register to hold that value and store
2683 s2
= new_stmt(cstate
, BPF_ST
);
2684 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2688 * Now move it into the X register.
2690 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2698 static struct slist
*
2699 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2701 struct slist
*s1
, *s2
;
2704 * Generate code to load the length of the radiotap header into
2705 * the register assigned to hold that length, if one has been
2706 * assigned. (If one hasn't been assigned, no code we've
2707 * generated uses that prefix, so we don't need to generate any
2710 if (cstate
->off_linkhdr
.reg
!= -1) {
2712 * The 2 bytes at offsets of 2 and 3 from the beginning
2713 * of the radiotap header are the length of the radiotap
2714 * header; unfortunately, it's little-endian, so we have
2715 * to load it a byte at a time and construct the value.
2719 * Load the high-order byte, at an offset of 3, shift it
2720 * left a byte, and put the result in the X register.
2722 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2724 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2727 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2731 * Load the next byte, at an offset of 2, and OR the
2732 * value from the X register into it.
2734 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2737 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2741 * Now allocate a register to hold that value and store
2744 s2
= new_stmt(cstate
, BPF_ST
);
2745 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2749 * Now move it into the X register.
2751 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2760 * At the moment we treat PPI as normal Radiotap encoded
2761 * packets. The difference is in the function that generates
2762 * the code at the beginning to compute the header length.
2763 * Since this code generator of PPI supports bare 802.11
2764 * encapsulation only (i.e. the encapsulated DLT should be
2765 * DLT_IEEE802_11) we generate code to check for this too;
2766 * that's done in finish_parse().
2768 static struct slist
*
2769 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2771 struct slist
*s1
, *s2
;
2774 * Generate code to load the length of the radiotap header
2775 * into the register assigned to hold that length, if one has
2778 if (cstate
->off_linkhdr
.reg
!= -1) {
2780 * The 2 bytes at offsets of 2 and 3 from the beginning
2781 * of the radiotap header are the length of the radiotap
2782 * header; unfortunately, it's little-endian, so we have
2783 * to load it a byte at a time and construct the value.
2787 * Load the high-order byte, at an offset of 3, shift it
2788 * left a byte, and put the result in the X register.
2790 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2792 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2795 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2799 * Load the next byte, at an offset of 2, and OR the
2800 * value from the X register into it.
2802 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2805 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2809 * Now allocate a register to hold that value and store
2812 s2
= new_stmt(cstate
, BPF_ST
);
2813 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2817 * Now move it into the X register.
2819 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2828 * Load a value relative to the beginning of the link-layer header after the 802.11
2829 * header, i.e. LLC_SNAP.
2830 * The link-layer header doesn't necessarily begin at the beginning
2831 * of the packet data; there might be a variable-length prefix containing
2832 * radio information.
2834 static struct slist
*
2835 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2838 struct slist
*sjset_data_frame_1
;
2839 struct slist
*sjset_data_frame_2
;
2840 struct slist
*sjset_qos
;
2841 struct slist
*sjset_radiotap_flags_present
;
2842 struct slist
*sjset_radiotap_ext_present
;
2843 struct slist
*sjset_radiotap_tsft_present
;
2844 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2845 struct slist
*s_roundup
;
2847 if (cstate
->off_linkpl
.reg
== -1) {
2849 * No register has been assigned to the offset of
2850 * the link-layer payload, which means nobody needs
2851 * it; don't bother computing it - just return
2852 * what we already have.
2858 * This code is not compatible with the optimizer, as
2859 * we are generating jmp instructions within a normal
2860 * slist of instructions
2862 cstate
->no_optimize
= 1;
2865 * If "s" is non-null, it has code to arrange that the X register
2866 * contains the length of the prefix preceding the link-layer
2869 * Otherwise, the length of the prefix preceding the link-layer
2870 * header is "off_outermostlinkhdr.constant_part".
2874 * There is no variable-length header preceding the
2875 * link-layer header.
2877 * Load the length of the fixed-length prefix preceding
2878 * the link-layer header (if any) into the X register,
2879 * and store it in the cstate->off_linkpl.reg register.
2880 * That length is off_outermostlinkhdr.constant_part.
2882 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2883 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2887 * The X register contains the offset of the beginning of the
2888 * link-layer header; add 24, which is the minimum length
2889 * of the MAC header for a data frame, to that, and store it
2890 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2891 * which is at the offset in the X register, with an indexed load.
2893 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2895 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2898 s2
= new_stmt(cstate
, BPF_ST
);
2899 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2902 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2907 * Check the Frame Control field to see if this is a data frame;
2908 * a data frame has the 0x08 bit (b3) in that field set and the
2909 * 0x04 bit (b2) clear.
2911 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2912 sjset_data_frame_1
->s
.k
= 0x08;
2913 sappend(s
, sjset_data_frame_1
);
2916 * If b3 is set, test b2, otherwise go to the first statement of
2917 * the rest of the program.
2919 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2920 sjset_data_frame_2
->s
.k
= 0x04;
2921 sappend(s
, sjset_data_frame_2
);
2922 sjset_data_frame_1
->s
.jf
= snext
;
2925 * If b2 is not set, this is a data frame; test the QoS bit.
2926 * Otherwise, go to the first statement of the rest of the
2929 sjset_data_frame_2
->s
.jt
= snext
;
2930 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2931 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2932 sappend(s
, sjset_qos
);
2935 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2937 * Otherwise, go to the first statement of the rest of the
2940 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2941 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2943 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2946 s2
= new_stmt(cstate
, BPF_ST
);
2947 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2951 * If we have a radiotap header, look at it to see whether
2952 * there's Atheros padding between the MAC-layer header
2955 * Note: all of the fields in the radiotap header are
2956 * little-endian, so we byte-swap all of the values
2957 * we test against, as they will be loaded as big-endian
2960 * XXX - in the general case, we would have to scan through
2961 * *all* the presence bits, if there's more than one word of
2962 * presence bits. That would require a loop, meaning that
2963 * we wouldn't be able to run the filter in the kernel.
2965 * We assume here that the Atheros adapters that insert the
2966 * annoying padding don't have multiple antennae and therefore
2967 * do not generate radiotap headers with multiple presence words.
2969 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2971 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2972 * in the first presence flag word?
2974 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2978 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2979 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2980 sappend(s
, sjset_radiotap_flags_present
);
2983 * If not, skip all of this.
2985 sjset_radiotap_flags_present
->s
.jf
= snext
;
2988 * Otherwise, is the "extension" bit set in that word?
2990 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2991 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2992 sappend(s
, sjset_radiotap_ext_present
);
2993 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2996 * If so, skip all of this.
2998 sjset_radiotap_ext_present
->s
.jt
= snext
;
3001 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3003 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3004 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3005 sappend(s
, sjset_radiotap_tsft_present
);
3006 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3009 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3010 * at an offset of 16 from the beginning of the raw packet
3011 * data (8 bytes for the radiotap header and 8 bytes for
3014 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3017 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3020 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3022 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3023 sjset_tsft_datapad
->s
.k
= 0x20;
3024 sappend(s
, sjset_tsft_datapad
);
3027 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3028 * at an offset of 8 from the beginning of the raw packet
3029 * data (8 bytes for the radiotap header).
3031 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3034 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3037 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3039 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3040 sjset_notsft_datapad
->s
.k
= 0x20;
3041 sappend(s
, sjset_notsft_datapad
);
3044 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3045 * set, round the length of the 802.11 header to
3046 * a multiple of 4. Do that by adding 3 and then
3047 * dividing by and multiplying by 4, which we do by
3050 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3051 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3052 sappend(s
, s_roundup
);
3053 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3056 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3057 s2
->s
.k
= (bpf_u_int32
)~3;
3059 s2
= new_stmt(cstate
, BPF_ST
);
3060 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3063 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3064 sjset_tsft_datapad
->s
.jf
= snext
;
3065 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3066 sjset_notsft_datapad
->s
.jf
= snext
;
3068 sjset_qos
->s
.jf
= snext
;
3074 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3078 /* There is an implicit dependency between the link
3079 * payload and link header since the payload computation
3080 * includes the variable part of the header. Therefore,
3081 * if nobody else has allocated a register for the link
3082 * header and we need it, do it now. */
3083 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3084 cstate
->off_linkhdr
.reg
== -1)
3085 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3088 * For link-layer types that have a variable-length header
3089 * preceding the link-layer header, generate code to load
3090 * the offset of the link-layer header into the register
3091 * assigned to that offset, if any.
3093 * XXX - this, and the next switch statement, won't handle
3094 * encapsulation of 802.11 or 802.11+radio information in
3095 * some other protocol stack. That's significantly more
3098 switch (cstate
->outermostlinktype
) {
3100 case DLT_PRISM_HEADER
:
3101 s
= gen_load_prism_llprefixlen(cstate
);
3104 case DLT_IEEE802_11_RADIO_AVS
:
3105 s
= gen_load_avs_llprefixlen(cstate
);
3108 case DLT_IEEE802_11_RADIO
:
3109 s
= gen_load_radiotap_llprefixlen(cstate
);
3113 s
= gen_load_ppi_llprefixlen(cstate
);
3122 * For link-layer types that have a variable-length link-layer
3123 * header, generate code to load the offset of the link-layer
3124 * payload into the register assigned to that offset, if any.
3126 switch (cstate
->outermostlinktype
) {
3128 case DLT_IEEE802_11
:
3129 case DLT_PRISM_HEADER
:
3130 case DLT_IEEE802_11_RADIO_AVS
:
3131 case DLT_IEEE802_11_RADIO
:
3133 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3137 s
= gen_load_pflog_llprefixlen(cstate
);
3142 * If there is no initialization yet and we need variable
3143 * length offsets for VLAN, initialize them to zero
3145 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3148 if (cstate
->off_linkpl
.reg
== -1)
3149 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3150 if (cstate
->off_linktype
.reg
== -1)
3151 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3153 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3155 s2
= new_stmt(cstate
, BPF_ST
);
3156 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3158 s2
= new_stmt(cstate
, BPF_ST
);
3159 s2
->s
.k
= cstate
->off_linktype
.reg
;
3164 * If we have any offset-loading code, append all the
3165 * existing statements in the block to those statements,
3166 * and make the resulting list the list of statements
3170 sappend(s
, b
->stmts
);
3175 static struct block
*
3176 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3178 struct slist
*s_load_dlt
;
3181 if (cstate
->linktype
== DLT_PPI
)
3183 /* Create the statements that check for the DLT
3185 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3186 s_load_dlt
->s
.k
= 4;
3188 b
= new_block(cstate
, JMP(BPF_JEQ
));
3190 b
->stmts
= s_load_dlt
;
3191 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3202 * Take an absolute offset, and:
3204 * if it has no variable part, return NULL;
3206 * if it has a variable part, generate code to load the register
3207 * containing that variable part into the X register, returning
3208 * a pointer to that code - if no register for that offset has
3209 * been allocated, allocate it first.
3211 * (The code to set that register will be generated later, but will
3212 * be placed earlier in the code sequence.)
3214 static struct slist
*
3215 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3219 if (off
->is_variable
) {
3220 if (off
->reg
== -1) {
3222 * We haven't yet assigned a register for the
3223 * variable part of the offset of the link-layer
3224 * header; allocate one.
3226 off
->reg
= alloc_reg(cstate
);
3230 * Load the register containing the variable part of the
3231 * offset of the link-layer header into the X register.
3233 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3238 * That offset isn't variable, there's no variable part,
3239 * so we don't need to generate any code.
3246 * Map an Ethernet type to the equivalent PPP type.
3249 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3257 case ETHERTYPE_IPV6
:
3258 ll_proto
= PPP_IPV6
;
3262 ll_proto
= PPP_DECNET
;
3265 case ETHERTYPE_ATALK
:
3266 ll_proto
= PPP_APPLE
;
3279 * I'm assuming the "Bridging PDU"s that go
3280 * over PPP are Spanning Tree Protocol
3283 ll_proto
= PPP_BRPDU
;
3294 * Generate any tests that, for encapsulation of a link-layer packet
3295 * inside another protocol stack, need to be done to check for those
3296 * link-layer packets (and that haven't already been done by a check
3297 * for that encapsulation).
3299 static struct block
*
3300 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3304 if (cstate
->is_encap
)
3305 return gen_encap_ll_check(cstate
);
3307 switch (cstate
->prevlinktype
) {
3311 * This is LANE-encapsulated Ethernet; check that the LANE
3312 * packet doesn't begin with an LE Control marker, i.e.
3313 * that it's data, not a control message.
3315 * (We've already generated a test for LANE.)
3317 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3323 * No such tests are necessary.
3331 * The three different values we should check for when checking for an
3332 * IPv6 packet with DLT_NULL.
3334 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3335 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3336 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3339 * Generate code to match a particular packet type by matching the
3340 * link-layer type field or fields in the 802.2 LLC header.
3342 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3343 * value, if <= ETHERMTU.
3345 static struct block
*
3346 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3348 struct block
*b0
, *b1
, *b2
;
3349 const char *description
;
3351 /* are we checking MPLS-encapsulated packets? */
3352 if (cstate
->label_stack_depth
> 0)
3353 return gen_mpls_linktype(cstate
, ll_proto
);
3355 switch (cstate
->linktype
) {
3358 case DLT_NETANALYZER
:
3359 case DLT_NETANALYZER_TRANSPARENT
:
3360 /* Geneve has an EtherType regardless of whether there is an
3361 * L2 header. VXLAN always has an EtherType. */
3362 if (!cstate
->is_encap
)
3363 b0
= gen_prevlinkhdr_check(cstate
);
3367 b1
= gen_ether_linktype(cstate
, ll_proto
);
3378 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3382 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3386 case DLT_IEEE802_11
:
3387 case DLT_PRISM_HEADER
:
3388 case DLT_IEEE802_11_RADIO_AVS
:
3389 case DLT_IEEE802_11_RADIO
:
3392 * Check that we have a data frame.
3394 b0
= gen_check_802_11_data_frame(cstate
);
3397 * Now check for the specified link-layer type.
3399 b1
= gen_llc_linktype(cstate
, ll_proto
);
3406 * XXX - check for LLC frames.
3408 return gen_llc_linktype(cstate
, ll_proto
);
3413 * XXX - check for LLC PDUs, as per IEEE 802.5.
3415 return gen_llc_linktype(cstate
, ll_proto
);
3418 case DLT_ATM_RFC1483
:
3420 case DLT_IP_OVER_FC
:
3421 return gen_llc_linktype(cstate
, ll_proto
);
3426 * Check for an LLC-encapsulated version of this protocol;
3427 * if we were checking for LANE, linktype would no longer
3430 * Check for LLC encapsulation and then check the protocol.
3432 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3433 b1
= gen_llc_linktype(cstate
, ll_proto
);
3439 return gen_linux_sll_linktype(cstate
, ll_proto
);
3443 case DLT_SLIP_BSDOS
:
3446 * These types don't provide any type field; packets
3447 * are always IPv4 or IPv6.
3449 * XXX - for IPv4, check for a version number of 4, and,
3450 * for IPv6, check for a version number of 6?
3455 /* Check for a version number of 4. */
3456 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3458 case ETHERTYPE_IPV6
:
3459 /* Check for a version number of 6. */
3460 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3463 return gen_false(cstate
); /* always false */
3469 * Raw IPv4, so no type field.
3471 if (ll_proto
== ETHERTYPE_IP
)
3472 return gen_true(cstate
); /* always true */
3474 /* Checking for something other than IPv4; always false */
3475 return gen_false(cstate
);
3480 * Raw IPv6, so no type field.
3482 if (ll_proto
== ETHERTYPE_IPV6
)
3483 return gen_true(cstate
); /* always true */
3485 /* Checking for something other than IPv6; always false */
3486 return gen_false(cstate
);
3491 case DLT_PPP_SERIAL
:
3494 * We use Ethernet protocol types inside libpcap;
3495 * map them to the corresponding PPP protocol types.
3497 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3498 ethertype_to_ppptype(ll_proto
));
3503 * We use Ethernet protocol types inside libpcap;
3504 * map them to the corresponding PPP protocol types.
3510 * Also check for Van Jacobson-compressed IP.
3511 * XXX - do this for other forms of PPP?
3513 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3514 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3516 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3521 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3522 ethertype_to_ppptype(ll_proto
));
3532 return (gen_loopback_linktype(cstate
, AF_INET
));
3534 case ETHERTYPE_IPV6
:
3536 * AF_ values may, unfortunately, be platform-
3537 * dependent; AF_INET isn't, because everybody
3538 * used 4.2BSD's value, but AF_INET6 is, because
3539 * 4.2BSD didn't have a value for it (given that
3540 * IPv6 didn't exist back in the early 1980's),
3541 * and they all picked their own values.
3543 * This means that, if we're reading from a
3544 * savefile, we need to check for all the
3547 * If we're doing a live capture, we only need
3548 * to check for this platform's value; however,
3549 * Npcap uses 24, which isn't Windows's AF_INET6
3550 * value. (Given the multiple different values,
3551 * programs that read pcap files shouldn't be
3552 * checking for their platform's AF_INET6 value
3553 * anyway, they should check for all of the
3554 * possible values. and they might as well do
3555 * that even for live captures.)
3557 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3559 * Savefile - check for all three
3560 * possible IPv6 values.
3562 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3563 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3565 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3570 * Live capture, so we only need to
3571 * check for the value used on this
3576 * Npcap doesn't use Windows's AF_INET6,
3577 * as that collides with AF_IPX on
3578 * some BSDs (both have the value 23).
3579 * Instead, it uses 24.
3581 return (gen_loopback_linktype(cstate
, 24));
3584 return (gen_loopback_linktype(cstate
, AF_INET6
));
3585 #else /* AF_INET6 */
3587 * I guess this platform doesn't support
3588 * IPv6, so we just reject all packets.
3590 return gen_false(cstate
);
3591 #endif /* AF_INET6 */
3597 * Not a type on which we support filtering.
3598 * XXX - support those that have AF_ values
3599 * #defined on this platform, at least?
3601 return gen_false(cstate
);
3606 * af field is host byte order in contrast to the rest of
3609 if (ll_proto
== ETHERTYPE_IP
)
3610 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3612 else if (ll_proto
== ETHERTYPE_IPV6
)
3613 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3616 return gen_false(cstate
);
3620 case DLT_ARCNET_LINUX
:
3622 * XXX should we check for first fragment if the protocol
3628 return gen_false(cstate
);
3630 case ETHERTYPE_IPV6
:
3631 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3635 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3637 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3643 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3645 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3650 case ETHERTYPE_REVARP
:
3651 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3654 case ETHERTYPE_ATALK
:
3655 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3662 case ETHERTYPE_ATALK
:
3663 return gen_true(cstate
);
3665 return gen_false(cstate
);
3671 * XXX - assumes a 2-byte Frame Relay header with
3672 * DLCI and flags. What if the address is longer?
3678 * Check for the special NLPID for IP.
3680 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3682 case ETHERTYPE_IPV6
:
3684 * Check for the special NLPID for IPv6.
3686 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3690 * Check for several OSI protocols.
3692 * Frame Relay packets typically have an OSI
3693 * NLPID at the beginning; we check for each
3696 * What we check for is the NLPID and a frame
3697 * control field of UI, i.e. 0x03 followed
3700 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3701 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3702 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3708 return gen_false(cstate
);
3713 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3715 case DLT_JUNIPER_MFR
:
3716 case DLT_JUNIPER_MLFR
:
3717 case DLT_JUNIPER_MLPPP
:
3718 case DLT_JUNIPER_ATM1
:
3719 case DLT_JUNIPER_ATM2
:
3720 case DLT_JUNIPER_PPPOE
:
3721 case DLT_JUNIPER_PPPOE_ATM
:
3722 case DLT_JUNIPER_GGSN
:
3723 case DLT_JUNIPER_ES
:
3724 case DLT_JUNIPER_MONITOR
:
3725 case DLT_JUNIPER_SERVICES
:
3726 case DLT_JUNIPER_ETHER
:
3727 case DLT_JUNIPER_PPP
:
3728 case DLT_JUNIPER_FRELAY
:
3729 case DLT_JUNIPER_CHDLC
:
3730 case DLT_JUNIPER_VP
:
3731 case DLT_JUNIPER_ST
:
3732 case DLT_JUNIPER_ISM
:
3733 case DLT_JUNIPER_VS
:
3734 case DLT_JUNIPER_SRX_E2E
:
3735 case DLT_JUNIPER_FIBRECHANNEL
:
3736 case DLT_JUNIPER_ATM_CEMIC
:
3738 /* just lets verify the magic number for now -
3739 * on ATM we may have up to 6 different encapsulations on the wire
3740 * and need a lot of heuristics to figure out that the payload
3743 * FIXME encapsulation specific BPF_ filters
3745 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3747 case DLT_BACNET_MS_TP
:
3748 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3751 return gen_ipnet_linktype(cstate
, ll_proto
);
3753 case DLT_LINUX_IRDA
:
3754 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3757 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3760 case DLT_MTP2_WITH_PHDR
:
3761 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3764 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3767 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3769 case DLT_LINUX_LAPD
:
3770 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3772 case DLT_USB_FREEBSD
:
3774 case DLT_USB_LINUX_MMAPPED
:
3776 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3778 case DLT_BLUETOOTH_HCI_H4
:
3779 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3780 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3783 case DLT_CAN_SOCKETCAN
:
3784 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3786 case DLT_IEEE802_15_4
:
3787 case DLT_IEEE802_15_4_LINUX
:
3788 case DLT_IEEE802_15_4_NONASK_PHY
:
3789 case DLT_IEEE802_15_4_NOFCS
:
3790 case DLT_IEEE802_15_4_TAP
:
3791 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3793 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3794 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3797 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3800 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3802 case DLT_IPMB_KONTRON
:
3803 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3806 bpf_error(cstate
, "I2C link-layer type filtering not implemented");
3809 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3812 /* Using the fixed-size NFLOG header it is possible to tell only
3813 * the address family of the packet, other meaningful data is
3814 * either missing or behind TLVs.
3816 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3820 * Does this link-layer header type have a field
3821 * indicating the type of the next protocol? If
3822 * so, off_linktype.constant_part will be the offset of that
3823 * field in the packet; if not, it will be OFFSET_NOT_SET.
3825 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3827 * Yes; assume it's an Ethernet type. (If
3828 * it's not, it needs to be handled specially
3831 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3835 * No; report an error.
3837 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3838 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3846 * Check for an LLC SNAP packet with a given organization code and
3847 * protocol type; we check the entire contents of the 802.2 LLC and
3848 * snap headers, checking for DSAP and SSAP of SNAP and a control
3849 * field of 0x03 in the LLC header, and for the specified organization
3850 * code and protocol type in the SNAP header.
3852 static struct block
*
3853 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3855 u_char snapblock
[8];
3857 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3858 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3859 snapblock
[2] = 0x03; /* control = UI */
3860 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3861 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3862 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3863 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3864 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3865 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3869 * Generate code to match frames with an LLC header.
3871 static struct block
*
3872 gen_llc_internal(compiler_state_t
*cstate
)
3874 struct block
*b0
, *b1
;
3876 switch (cstate
->linktype
) {
3880 * We check for an Ethernet type field less than
3881 * 1500, which means it's an 802.3 length field.
3883 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3887 * Now check for the purported DSAP and SSAP not being
3888 * 0xFF, to rule out NetWare-over-802.3.
3890 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3897 * We check for LLC traffic.
3899 b0
= gen_atmtype_llc(cstate
);
3902 case DLT_IEEE802
: /* Token Ring */
3904 * XXX - check for LLC frames.
3906 return gen_true(cstate
);
3910 * XXX - check for LLC frames.
3912 return gen_true(cstate
);
3914 case DLT_ATM_RFC1483
:
3916 * For LLC encapsulation, these are defined to have an
3919 * For VC encapsulation, they don't, but there's no
3920 * way to check for that; the protocol used on the VC
3921 * is negotiated out of band.
3923 return gen_true(cstate
);
3925 case DLT_IEEE802_11
:
3926 case DLT_PRISM_HEADER
:
3927 case DLT_IEEE802_11_RADIO
:
3928 case DLT_IEEE802_11_RADIO_AVS
:
3931 * Check that we have a data frame.
3933 b0
= gen_check_802_11_data_frame(cstate
);
3937 bpf_error(cstate
, "'llc' not supported for %s",
3938 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3944 gen_llc(compiler_state_t
*cstate
)
3947 * Catch errors reported by us and routines below us, and return NULL
3950 if (setjmp(cstate
->top_ctx
))
3953 return gen_llc_internal(cstate
);
3957 gen_llc_i(compiler_state_t
*cstate
)
3959 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 * Load the control byte and test the low-order bit; it must
3976 * be clear for I frames.
3978 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3979 b1
= new_block(cstate
, JMP(BPF_JSET
));
3988 gen_llc_s(compiler_state_t
*cstate
)
3990 struct block
*b0
, *b1
;
3993 * Catch errors reported by us and routines below us, and return NULL
3996 if (setjmp(cstate
->top_ctx
))
4000 * Check whether this is an LLC frame.
4002 b0
= gen_llc_internal(cstate
);
4005 * Now compare the low-order 2 bit of the control byte against
4006 * the appropriate value for S frames.
4008 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4014 gen_llc_u(compiler_state_t
*cstate
)
4016 struct block
*b0
, *b1
;
4019 * Catch errors reported by us and routines below us, and return NULL
4022 if (setjmp(cstate
->top_ctx
))
4026 * Check whether this is an LLC frame.
4028 b0
= gen_llc_internal(cstate
);
4031 * Now compare the low-order 2 bit of the control byte against
4032 * the appropriate value for U frames.
4034 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4040 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4042 struct block
*b0
, *b1
;
4045 * Catch errors reported by us and routines below us, and return NULL
4048 if (setjmp(cstate
->top_ctx
))
4052 * Check whether this is an LLC frame.
4054 b0
= gen_llc_internal(cstate
);
4057 * Now check for an S frame with the appropriate type.
4059 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4065 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4067 struct block
*b0
, *b1
;
4070 * Catch errors reported by us and routines below us, and return NULL
4073 if (setjmp(cstate
->top_ctx
))
4077 * Check whether this is an LLC frame.
4079 b0
= gen_llc_internal(cstate
);
4082 * Now check for a U frame with the appropriate type.
4084 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4090 * Generate code to match a particular packet type, for link-layer types
4091 * using 802.2 LLC headers.
4093 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4094 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4096 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4097 * value, if <= ETHERMTU. We use that to determine whether to
4098 * match the DSAP or both DSAP and LSAP or to check the OUI and
4099 * protocol ID in a SNAP header.
4101 static struct block
*
4102 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4105 * XXX - handle token-ring variable-length header.
4111 case LLCSAP_NETBEUI
:
4113 * XXX - should we check both the DSAP and the
4114 * SSAP, like this, or should we check just the
4115 * DSAP, as we do for other SAP values?
4117 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4118 ((ll_proto
<< 8) | ll_proto
));
4122 * XXX - are there ever SNAP frames for IPX on
4123 * non-Ethernet 802.x networks?
4125 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4127 case ETHERTYPE_ATALK
:
4129 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4130 * SNAP packets with an organization code of
4131 * 0x080007 (Apple, for Appletalk) and a protocol
4132 * type of ETHERTYPE_ATALK (Appletalk).
4134 * XXX - check for an organization code of
4135 * encapsulated Ethernet as well?
4137 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4141 * XXX - we don't have to check for IPX 802.3
4142 * here, but should we check for the IPX Ethertype?
4144 if (ll_proto
<= ETHERMTU
) {
4146 * This is an LLC SAP value, so check
4149 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4152 * This is an Ethernet type; we assume that it's
4153 * unlikely that it'll appear in the right place
4154 * at random, and therefore check only the
4155 * location that would hold the Ethernet type
4156 * in a SNAP frame with an organization code of
4157 * 0x000000 (encapsulated Ethernet).
4159 * XXX - if we were to check for the SNAP DSAP and
4160 * LSAP, as per XXX, and were also to check for an
4161 * organization code of 0x000000 (encapsulated
4162 * Ethernet), we'd do
4164 * return gen_snap(cstate, 0x000000, ll_proto);
4166 * here; for now, we don't, as per the above.
4167 * I don't know whether it's worth the extra CPU
4168 * time to do the right check or not.
4170 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4175 static struct block
*
4176 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4177 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4179 struct block
*b0
, *b1
;
4193 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4194 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4200 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4201 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4206 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4210 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4214 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4218 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4222 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4226 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4233 b0
= gen_linktype(cstate
, ll_proto
);
4234 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4240 static struct block
*
4241 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4242 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4245 struct block
*b0
, *b1
;
4248 * Code below needs to access four separate 32-bit parts of the 128-bit
4249 * IPv6 address and mask. In some OSes this is as simple as using the
4250 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4251 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4252 * far as libpcap sees it. Hence copy the data before use to avoid
4253 * potential unaligned memory access and the associated compiler
4254 * warnings (whether genuine or not).
4256 bpf_u_int32 a
[4], m
[4];
4269 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4270 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4276 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4277 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4282 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4286 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4290 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4294 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4298 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4302 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4309 /* this order is important */
4310 memcpy(a
, addr
, sizeof(a
));
4311 memcpy(m
, mask
, sizeof(m
));
4312 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4313 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4315 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4317 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4319 b0
= gen_linktype(cstate
, ll_proto
);
4325 static struct block
*
4326 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4328 register struct block
*b0
, *b1
;
4332 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4335 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4338 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4339 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4345 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4346 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4351 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4355 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4359 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4363 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4367 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4371 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4379 * Like gen_ehostop, but for DLT_FDDI
4381 static struct block
*
4382 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4384 struct block
*b0
, *b1
;
4388 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4391 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4394 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4395 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4401 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4402 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4407 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4411 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4415 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4419 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4423 bpf_error(cstate
, "'ra' is only supported on 802.11");
4427 bpf_error(cstate
, "'ta' is only supported on 802.11");
4435 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4437 static struct block
*
4438 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4440 register struct block
*b0
, *b1
;
4444 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4447 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4450 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4451 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4457 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4458 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4463 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4467 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4471 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4475 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4479 bpf_error(cstate
, "'ra' is only supported on 802.11");
4483 bpf_error(cstate
, "'ta' is only supported on 802.11");
4491 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4492 * various 802.11 + radio headers.
4494 static struct block
*
4495 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4497 register struct block
*b0
, *b1
, *b2
;
4498 register struct slist
*s
;
4500 #ifdef ENABLE_WLAN_FILTERING_PATCH
4503 * We need to disable the optimizer because the optimizer is buggy
4504 * and wipes out some LD instructions generated by the below
4505 * code to validate the Frame Control bits
4507 cstate
->no_optimize
= 1;
4508 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4515 * For control frames, there is no SA.
4517 * For management frames, SA is at an
4518 * offset of 10 from the beginning of
4521 * For data frames, SA is at an offset
4522 * of 10 from the beginning of the packet
4523 * if From DS is clear, at an offset of
4524 * 16 from the beginning of the packet
4525 * if From DS is set and To DS is clear,
4526 * and an offset of 24 from the beginning
4527 * of the packet if From DS is set and To DS
4532 * Generate the tests to be done for data frames
4535 * First, check for To DS set, i.e. check "link[1] & 0x01".
4537 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4538 b1
= new_block(cstate
, JMP(BPF_JSET
));
4539 b1
->s
.k
= 0x01; /* To DS */
4543 * If To DS is set, the SA is at 24.
4545 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4549 * Now, check for To DS not set, i.e. check
4550 * "!(link[1] & 0x01)".
4552 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4553 b2
= new_block(cstate
, JMP(BPF_JSET
));
4554 b2
->s
.k
= 0x01; /* To DS */
4559 * If To DS is not set, the SA is at 16.
4561 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4565 * Now OR together the last two checks. That gives
4566 * the complete set of checks for data frames with
4572 * Now check for From DS being set, and AND that with
4573 * the ORed-together checks.
4575 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4576 b1
= new_block(cstate
, JMP(BPF_JSET
));
4577 b1
->s
.k
= 0x02; /* From DS */
4582 * Now check for data frames with From DS not set.
4584 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4585 b2
= new_block(cstate
, JMP(BPF_JSET
));
4586 b2
->s
.k
= 0x02; /* From DS */
4591 * If From DS isn't set, the SA is at 10.
4593 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4597 * Now OR together the checks for data frames with
4598 * From DS not set and for data frames with From DS
4599 * set; that gives the checks done for data frames.
4604 * Now check for a data frame.
4605 * I.e, check "link[0] & 0x08".
4607 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4608 b1
= new_block(cstate
, JMP(BPF_JSET
));
4613 * AND that with the checks done for data frames.
4618 * If the high-order bit of the type value is 0, this
4619 * is a management frame.
4620 * I.e, check "!(link[0] & 0x08)".
4622 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4623 b2
= new_block(cstate
, JMP(BPF_JSET
));
4629 * For management frames, the SA is at 10.
4631 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4635 * OR that with the checks done for data frames.
4636 * That gives the checks done for management and
4642 * If the low-order bit of the type value is 1,
4643 * this is either a control frame or a frame
4644 * with a reserved type, and thus not a
4647 * I.e., check "!(link[0] & 0x04)".
4649 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4650 b1
= new_block(cstate
, JMP(BPF_JSET
));
4656 * AND that with the checks for data and management
4666 * For control frames, there is no DA.
4668 * For management frames, DA is at an
4669 * offset of 4 from the beginning of
4672 * For data frames, DA is at an offset
4673 * of 4 from the beginning of the packet
4674 * if To DS is clear and at an offset of
4675 * 16 from the beginning of the packet
4680 * Generate the tests to be done for data frames.
4682 * First, check for To DS set, i.e. "link[1] & 0x01".
4684 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4685 b1
= new_block(cstate
, JMP(BPF_JSET
));
4686 b1
->s
.k
= 0x01; /* To DS */
4690 * If To DS is set, the DA is at 16.
4692 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4696 * Now, check for To DS not set, i.e. check
4697 * "!(link[1] & 0x01)".
4699 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4700 b2
= new_block(cstate
, JMP(BPF_JSET
));
4701 b2
->s
.k
= 0x01; /* To DS */
4706 * If To DS is not set, the DA is at 4.
4708 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4712 * Now OR together the last two checks. That gives
4713 * the complete set of checks for data frames.
4718 * Now check for a data frame.
4719 * I.e, check "link[0] & 0x08".
4721 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4722 b1
= new_block(cstate
, JMP(BPF_JSET
));
4727 * AND that with the checks done for data frames.
4732 * If the high-order bit of the type value is 0, this
4733 * is a management frame.
4734 * I.e, check "!(link[0] & 0x08)".
4736 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4737 b2
= new_block(cstate
, JMP(BPF_JSET
));
4743 * For management frames, the DA is at 4.
4745 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4749 * OR that with the checks done for data frames.
4750 * That gives the checks done for management and
4756 * If the low-order bit of the type value is 1,
4757 * this is either a control frame or a frame
4758 * with a reserved type, and thus not a
4761 * I.e., check "!(link[0] & 0x04)".
4763 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4764 b1
= new_block(cstate
, JMP(BPF_JSET
));
4770 * AND that with the checks for data and management
4777 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4778 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4784 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4785 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4790 * XXX - add BSSID keyword?
4793 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4797 * Not present in CTS or ACK control frames.
4799 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4800 IEEE80211_FC0_TYPE_MASK
);
4802 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4803 IEEE80211_FC0_SUBTYPE_MASK
);
4805 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4806 IEEE80211_FC0_SUBTYPE_MASK
);
4810 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4816 * Not present in control frames.
4818 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4819 IEEE80211_FC0_TYPE_MASK
);
4821 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4827 * Present only if the direction mask has both "From DS"
4828 * and "To DS" set. Neither control frames nor management
4829 * frames should have both of those set, so we don't
4830 * check the frame type.
4832 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4833 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4834 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4840 * Not present in management frames; addr1 in other
4845 * If the high-order bit of the type value is 0, this
4846 * is a management frame.
4847 * I.e, check "(link[0] & 0x08)".
4849 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4850 b1
= new_block(cstate
, JMP(BPF_JSET
));
4857 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4860 * AND that with the check of addr1.
4867 * Not present in management frames; addr2, if present,
4872 * Not present in CTS or ACK control frames.
4874 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4875 IEEE80211_FC0_TYPE_MASK
);
4877 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4878 IEEE80211_FC0_SUBTYPE_MASK
);
4880 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4881 IEEE80211_FC0_SUBTYPE_MASK
);
4887 * If the high-order bit of the type value is 0, this
4888 * is a management frame.
4889 * I.e, check "(link[0] & 0x08)".
4891 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4892 b1
= new_block(cstate
, JMP(BPF_JSET
));
4897 * AND that with the check for frames other than
4898 * CTS and ACK frames.
4905 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4914 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4915 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4916 * as the RFC states.)
4918 static struct block
*
4919 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4921 register struct block
*b0
, *b1
;
4925 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4928 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4931 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4932 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4938 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4939 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4944 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4948 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4952 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4956 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4960 bpf_error(cstate
, "'ra' is only supported on 802.11");
4964 bpf_error(cstate
, "'ta' is only supported on 802.11");
4972 * This is quite tricky because there may be pad bytes in front of the
4973 * DECNET header, and then there are two possible data packet formats that
4974 * carry both src and dst addresses, plus 5 packet types in a format that
4975 * carries only the src node, plus 2 types that use a different format and
4976 * also carry just the src node.
4980 * Instead of doing those all right, we just look for data packets with
4981 * 0 or 1 bytes of padding. If you want to look at other packets, that
4982 * will require a lot more hacking.
4984 * To add support for filtering on DECNET "areas" (network numbers)
4985 * one would want to add a "mask" argument to this routine. That would
4986 * make the filter even more inefficient, although one could be clever
4987 * and not generate masking instructions if the mask is 0xFFFF.
4989 static struct block
*
4990 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4992 struct block
*b0
, *b1
, *b2
, *tmp
;
4993 u_int offset_lh
; /* offset if long header is received */
4994 u_int offset_sh
; /* offset if short header is received */
4999 offset_sh
= 1; /* follows flags */
5000 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5004 offset_sh
= 3; /* follows flags, dstnode */
5005 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5009 /* Inefficient because we do our Calvinball dance twice */
5010 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5011 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5017 /* Inefficient because we do our Calvinball dance twice */
5018 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5019 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5024 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5028 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5032 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5036 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5040 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5044 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5052 * In a DECnet message inside an Ethernet frame the first two bytes
5053 * immediately after EtherType are the [litle-endian] DECnet message
5054 * length, which is irrelevant in this context.
5056 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5057 * 8-bit bitmap of the optional padding before the packet route header.
5058 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5059 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5060 * means there aren't any PAD bytes after the bitmap, so the header
5061 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5062 * is set to 0, thus the header begins at the third byte.
5064 * The header can be in several (as mentioned above) formats, all of
5065 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5066 * (PF, "pad field") set to 0 regardless of any padding present before
5067 * the header. "Short header" means bits 0-2 of the bitmap encode the
5068 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5070 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5071 * values and the masks, this maps to the required single bytes of
5072 * the message correctly on both big-endian and little-endian hosts.
5073 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5074 * because the wire encoding is little-endian and BPF multiple-byte
5075 * loads are big-endian. When the destination address is near enough
5076 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5079 /* Check for pad = 1, long header case */
5080 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5081 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5082 BPF_H
, SWAPSHORT(addr
));
5084 /* Check for pad = 0, long header case */
5085 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5086 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5090 /* Check for pad = 1, short header case */
5092 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5093 0x81020000U
| SWAPSHORT(addr
),
5096 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5097 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5102 /* Check for pad = 0, short header case */
5104 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5105 0x02000000U
| SWAPSHORT(addr
) << 8,
5108 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5109 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5119 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5120 * test the bottom-of-stack bit, and then check the version number
5121 * field in the IP header.
5123 static struct block
*
5124 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5126 struct block
*b0
, *b1
;
5131 /* match the bottom-of-stack bit */
5132 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5133 /* match the IPv4 version number */
5134 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5138 case ETHERTYPE_IPV6
:
5139 /* match the bottom-of-stack bit */
5140 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5141 /* match the IPv4 version number */
5142 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5147 /* FIXME add other L3 proto IDs */
5148 bpf_error(cstate
, "unsupported protocol over mpls");
5153 static struct block
*
5154 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5155 int proto
, int dir
, int type
)
5157 struct block
*b0
, *b1
;
5158 const char *typestr
;
5168 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5170 * Only check for non-IPv4 addresses if we're not
5171 * checking MPLS-encapsulated packets.
5173 if (cstate
->label_stack_depth
== 0) {
5174 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5176 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5182 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5185 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
5188 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
5191 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
5194 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5197 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5200 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5203 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5206 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5209 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5212 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5215 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5216 b1
= gen_dnhostop(cstate
, addr
, dir
);
5221 bpf_error(cstate
, "LAT host filtering not implemented");
5224 bpf_error(cstate
, "SCA host filtering not implemented");
5227 bpf_error(cstate
, "MOPRC host filtering not implemented");
5230 bpf_error(cstate
, "MOPDL host filtering not implemented");
5233 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5236 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5239 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5242 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5245 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5248 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5251 bpf_error(cstate
, "AARP host filtering not implemented");
5254 bpf_error(cstate
, "ISO host filtering not implemented");
5257 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5260 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5263 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5266 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5269 bpf_error(cstate
, "IPX host filtering not implemented");
5272 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5275 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5278 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5281 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5284 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5287 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5290 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5293 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5296 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5299 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5308 static struct block
*
5309 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5310 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5312 const char *typestr
;
5322 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5325 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5328 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5331 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5334 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5337 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5340 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5343 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5346 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5349 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5352 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5355 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5358 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5361 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5364 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5367 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5370 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5373 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5376 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5379 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5382 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5385 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5388 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5391 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5394 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5397 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5400 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5403 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5406 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5409 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5412 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5415 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5418 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5421 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5424 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5427 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5430 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5433 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5436 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5439 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5449 static struct block
*
5450 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5451 struct addrinfo
*alist
, int proto
, int dir
)
5453 struct block
*b0
, *b1
, *tmp
;
5454 struct addrinfo
*ai
;
5455 struct sockaddr_in
*sin
;
5458 bpf_error(cstate
, "direction applied to 'gateway'");
5465 switch (cstate
->linktype
) {
5467 case DLT_NETANALYZER
:
5468 case DLT_NETANALYZER_TRANSPARENT
:
5469 b1
= gen_prevlinkhdr_check(cstate
);
5470 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5475 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5478 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5480 case DLT_IEEE802_11
:
5481 case DLT_PRISM_HEADER
:
5482 case DLT_IEEE802_11_RADIO_AVS
:
5483 case DLT_IEEE802_11_RADIO
:
5485 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5487 case DLT_IP_OVER_FC
:
5488 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5492 * This is LLC-multiplexed traffic; if it were
5493 * LANE, cstate->linktype would have been set to
5499 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5502 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5504 * Does it have an address?
5506 if (ai
->ai_addr
!= NULL
) {
5508 * Yes. Is it an IPv4 address?
5510 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5512 * Generate an entry for it.
5514 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5515 tmp
= gen_host(cstate
,
5516 ntohl(sin
->sin_addr
.s_addr
),
5517 0xffffffff, proto
, Q_OR
, Q_HOST
);
5519 * Is it the *first* IPv4 address?
5523 * Yes, so start with it.
5528 * No, so OR it into the
5540 * No IPv4 addresses found.
5548 bpf_error(cstate
, "illegal modifier of 'gateway'");
5553 static struct block
*
5554 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5562 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5566 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5570 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5574 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5577 #ifndef IPPROTO_IGMP
5578 #define IPPROTO_IGMP 2
5582 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5585 #ifndef IPPROTO_IGRP
5586 #define IPPROTO_IGRP 9
5589 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5593 #define IPPROTO_PIM 103
5597 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5600 #ifndef IPPROTO_VRRP
5601 #define IPPROTO_VRRP 112
5605 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5608 #ifndef IPPROTO_CARP
5609 #define IPPROTO_CARP 112
5613 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5617 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5621 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5625 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5629 bpf_error(cstate
, "link layer applied in wrong context");
5632 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5636 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5640 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5644 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5648 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5652 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5656 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5660 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5663 #ifndef IPPROTO_ICMPV6
5664 #define IPPROTO_ICMPV6 58
5667 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5671 #define IPPROTO_AH 51
5674 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5678 #define IPPROTO_ESP 50
5681 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5685 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5689 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5693 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5696 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5697 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5698 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5700 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5702 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5704 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5708 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5709 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5710 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5712 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5714 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5716 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5720 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5721 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5722 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5724 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5729 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5730 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5735 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5736 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5738 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5740 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5745 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5746 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5751 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5752 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5757 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5761 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5765 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5769 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5773 bpf_error(cstate
, "'radio' is not a valid protocol type");
5782 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5785 * Catch errors reported by us and routines below us, and return NULL
5788 if (setjmp(cstate
->top_ctx
))
5791 return gen_proto_abbrev_internal(cstate
, proto
);
5794 static struct block
*
5795 gen_ipfrag(compiler_state_t
*cstate
)
5800 /* not IPv4 frag other than the first frag */
5801 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5802 b
= new_block(cstate
, JMP(BPF_JSET
));
5811 * Generate a comparison to a port value in the transport-layer header
5812 * at the specified offset from the beginning of that header.
5814 * XXX - this handles a variable-length prefix preceding the link-layer
5815 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5816 * variable-length link-layer headers (such as Token Ring or 802.11
5819 static struct block
*
5820 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5822 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5825 static struct block
*
5826 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5828 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5831 static struct block
*
5832 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5834 struct block
*b0
, *b1
, *tmp
;
5836 /* ip proto 'proto' and not a fragment other than the first fragment */
5837 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5838 b0
= gen_ipfrag(cstate
);
5843 b1
= gen_portatom(cstate
, 0, port
);
5847 b1
= gen_portatom(cstate
, 2, port
);
5851 tmp
= gen_portatom(cstate
, 0, port
);
5852 b1
= gen_portatom(cstate
, 2, port
);
5858 tmp
= gen_portatom(cstate
, 0, port
);
5859 b1
= gen_portatom(cstate
, 2, port
);
5864 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5868 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5872 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5876 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5880 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5884 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5896 static struct block
*
5897 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5899 struct block
*b0
, *b1
, *tmp
;
5904 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5905 * not LLC encapsulation with LLCSAP_IP.
5907 * For IEEE 802 networks - which includes 802.5 token ring
5908 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5909 * says that SNAP encapsulation is used, not LLC encapsulation
5912 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5913 * RFC 2225 say that SNAP encapsulation is used, not LLC
5914 * encapsulation with LLCSAP_IP.
5916 * So we always check for ETHERTYPE_IP.
5918 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5924 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5928 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5929 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5931 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5943 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5945 struct block
*b0
, *b1
, *tmp
;
5947 /* ip6 proto 'proto' */
5948 /* XXX - catch the first fragment of a fragmented packet? */
5949 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5953 b1
= gen_portatom6(cstate
, 0, port
);
5957 b1
= gen_portatom6(cstate
, 2, port
);
5961 tmp
= gen_portatom6(cstate
, 0, port
);
5962 b1
= gen_portatom6(cstate
, 2, port
);
5968 tmp
= gen_portatom6(cstate
, 0, port
);
5969 b1
= gen_portatom6(cstate
, 2, port
);
5981 static struct block
*
5982 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5984 struct block
*b0
, *b1
, *tmp
;
5986 /* link proto ip6 */
5987 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5993 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5997 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5998 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
6000 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
6011 /* gen_portrange code */
6012 static struct block
*
6013 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6016 struct block
*b1
, *b2
;
6020 * Reverse the order of the ports, so v1 is the lower one.
6029 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
6030 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
6037 static struct block
*
6038 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6039 bpf_u_int32 proto
, int dir
)
6041 struct block
*b0
, *b1
, *tmp
;
6043 /* ip proto 'proto' and not a fragment other than the first fragment */
6044 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
6045 b0
= gen_ipfrag(cstate
);
6050 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6054 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6058 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6059 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6065 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6066 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6071 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6075 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6079 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6083 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6087 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
6091 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
6103 static struct block
*
6104 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6107 struct block
*b0
, *b1
, *tmp
;
6110 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6116 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6121 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6122 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6124 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6135 static struct block
*
6136 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6139 struct block
*b1
, *b2
;
6143 * Reverse the order of the ports, so v1 is the lower one.
6152 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6153 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6160 static struct block
*
6161 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6162 bpf_u_int32 proto
, int dir
)
6164 struct block
*b0
, *b1
, *tmp
;
6166 /* ip6 proto 'proto' */
6167 /* XXX - catch the first fragment of a fragmented packet? */
6168 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6172 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6176 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6180 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6181 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6187 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6188 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6200 static struct block
*
6201 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6204 struct block
*b0
, *b1
, *tmp
;
6206 /* link proto ip6 */
6207 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6213 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6218 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6219 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6221 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6233 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6242 v
= pcap_nametoproto(name
);
6243 if (v
== PROTO_UNDEF
)
6244 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6248 /* XXX should look up h/w protocol type based on cstate->linktype */
6249 v
= pcap_nametoeproto(name
);
6250 if (v
== PROTO_UNDEF
) {
6251 v
= pcap_nametollc(name
);
6252 if (v
== PROTO_UNDEF
)
6253 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6258 if (strcmp(name
, "esis") == 0)
6260 else if (strcmp(name
, "isis") == 0)
6262 else if (strcmp(name
, "clnp") == 0)
6265 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6275 #if !defined(NO_PROTOCHAIN)
6276 static struct block
*
6277 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6279 struct block
*b0
, *b
;
6280 struct slist
*s
[100];
6281 int fix2
, fix3
, fix4
, fix5
;
6282 int ahcheck
, again
, end
;
6284 int reg2
= alloc_reg(cstate
);
6286 memset(s
, 0, sizeof(s
));
6287 fix3
= fix4
= fix5
= 0;
6294 b0
= gen_protochain(cstate
, v
, Q_IP
);
6295 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6299 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6304 * We don't handle variable-length prefixes before the link-layer
6305 * header, or variable-length link-layer headers, here yet.
6306 * We might want to add BPF instructions to do the protochain
6307 * work, to simplify that and, on platforms that have a BPF
6308 * interpreter with the new instructions, let the filtering
6309 * be done in the kernel. (We already require a modified BPF
6310 * engine to do the protochain stuff, to support backward
6311 * branches, and backward branch support is unlikely to appear
6312 * in kernel BPF engines.)
6314 if (cstate
->off_linkpl
.is_variable
)
6315 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6318 * To quote a comment in optimize.c:
6320 * "These data structures are used in a Cocke and Schwartz style
6321 * value numbering scheme. Since the flowgraph is acyclic,
6322 * exit values can be propagated from a node's predecessors
6323 * provided it is uniquely defined."
6325 * "Acyclic" means "no backward branches", which means "no
6326 * loops", so we have to turn the optimizer off.
6328 cstate
->no_optimize
= 1;
6331 * s[0] is a dummy entry to protect other BPF insn from damage
6332 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6333 * hard to find interdependency made by jump table fixup.
6336 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6341 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6344 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6345 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6347 /* X = ip->ip_hl << 2 */
6348 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6349 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6354 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6356 /* A = ip6->ip_nxt */
6357 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6358 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6360 /* X = sizeof(struct ip6_hdr) */
6361 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6367 bpf_error(cstate
, "unsupported proto to gen_protochain");
6371 /* again: if (A == v) goto end; else fall through; */
6373 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6375 s
[i
]->s
.jt
= NULL
; /*later*/
6376 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6380 #ifndef IPPROTO_NONE
6381 #define IPPROTO_NONE 59
6383 /* if (A == IPPROTO_NONE) goto end */
6384 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6385 s
[i
]->s
.jt
= NULL
; /*later*/
6386 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6387 s
[i
]->s
.k
= IPPROTO_NONE
;
6388 s
[fix5
]->s
.jf
= s
[i
];
6392 if (proto
== Q_IPV6
) {
6393 int v6start
, v6end
, v6advance
, j
;
6396 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6397 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6398 s
[i
]->s
.jt
= NULL
; /*later*/
6399 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6400 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6401 s
[fix2
]->s
.jf
= s
[i
];
6403 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6404 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6405 s
[i
]->s
.jt
= NULL
; /*later*/
6406 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6407 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6409 /* if (A == IPPROTO_ROUTING) goto v6advance */
6410 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6411 s
[i
]->s
.jt
= NULL
; /*later*/
6412 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6413 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6415 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6416 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6417 s
[i
]->s
.jt
= NULL
; /*later*/
6418 s
[i
]->s
.jf
= NULL
; /*later*/
6419 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6429 * A = P[X + packet head];
6430 * X = X + (P[X + packet head + 1] + 1) * 8;
6432 /* A = P[X + packet head] */
6433 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6434 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6437 s
[i
] = new_stmt(cstate
, BPF_ST
);
6440 /* A = P[X + packet head + 1]; */
6441 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6442 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6445 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6449 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6453 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6457 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6460 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6464 /* goto again; (must use BPF_JA for backward jump) */
6465 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6466 s
[i
]->s
.k
= again
- i
- 1;
6467 s
[i
- 1]->s
.jf
= s
[i
];
6471 for (j
= v6start
; j
<= v6end
; j
++)
6472 s
[j
]->s
.jt
= s
[v6advance
];
6475 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6477 s
[fix2
]->s
.jf
= s
[i
];
6483 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6484 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6485 s
[i
]->s
.jt
= NULL
; /*later*/
6486 s
[i
]->s
.jf
= NULL
; /*later*/
6487 s
[i
]->s
.k
= IPPROTO_AH
;
6489 s
[fix3
]->s
.jf
= s
[ahcheck
];
6496 * X = X + (P[X + 1] + 2) * 4;
6499 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6501 /* A = P[X + packet head]; */
6502 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6503 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6506 s
[i
] = new_stmt(cstate
, BPF_ST
);
6510 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6513 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6517 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6519 /* A = P[X + packet head] */
6520 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6521 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6524 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6528 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6532 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6535 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6539 /* goto again; (must use BPF_JA for backward jump) */
6540 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6541 s
[i
]->s
.k
= again
- i
- 1;
6546 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6548 s
[fix2
]->s
.jt
= s
[end
];
6549 s
[fix4
]->s
.jf
= s
[end
];
6550 s
[fix5
]->s
.jt
= s
[end
];
6557 for (i
= 0; i
< max
- 1; i
++)
6558 s
[i
]->next
= s
[i
+ 1];
6559 s
[max
- 1]->next
= NULL
;
6564 b
= new_block(cstate
, JMP(BPF_JEQ
));
6565 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6568 free_reg(cstate
, reg2
);
6573 #endif /* !defined(NO_PROTOCHAIN) */
6575 static struct block
*
6576 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6579 struct block
*b0
, *b1
;
6582 * A data frame has the 0x08 bit (b3) in the frame control field set
6583 * and the 0x04 bit (b2) clear.
6585 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6586 b0
= new_block(cstate
, JMP(BPF_JSET
));
6590 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6591 b1
= new_block(cstate
, JMP(BPF_JSET
));
6602 * Generate code that checks whether the packet is a packet for protocol
6603 * <proto> and whether the type field in that protocol's header has
6604 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6605 * IP packet and checks the protocol number in the IP header against <v>.
6607 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6608 * against Q_IP and Q_IPV6.
6610 static struct block
*
6611 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6613 struct block
*b0
, *b1
;
6616 if (dir
!= Q_DEFAULT
)
6617 bpf_error(cstate
, "direction applied to 'proto'");
6621 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6622 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6627 return gen_linktype(cstate
, v
);
6631 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6632 * not LLC encapsulation with LLCSAP_IP.
6634 * For IEEE 802 networks - which includes 802.5 token ring
6635 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6636 * says that SNAP encapsulation is used, not LLC encapsulation
6639 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6640 * RFC 2225 say that SNAP encapsulation is used, not LLC
6641 * encapsulation with LLCSAP_IP.
6643 * So we always check for ETHERTYPE_IP.
6645 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6646 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6651 bpf_error(cstate
, "arp does not encapsulate another protocol");
6655 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6659 bpf_error(cstate
, "'sctp proto' is bogus");
6663 bpf_error(cstate
, "'tcp proto' is bogus");
6667 bpf_error(cstate
, "'udp proto' is bogus");
6671 bpf_error(cstate
, "'icmp proto' is bogus");
6675 bpf_error(cstate
, "'igmp proto' is bogus");
6679 bpf_error(cstate
, "'igrp proto' is bogus");
6683 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6687 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6691 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6695 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6699 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6703 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6707 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6709 * Also check for a fragment header before the final
6712 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6713 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6715 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6721 bpf_error(cstate
, "'icmp6 proto' is bogus");
6725 bpf_error(cstate
, "'ah proto' is bogus");
6729 bpf_error(cstate
, "'esp proto' is bogus");
6733 bpf_error(cstate
, "'pim proto' is bogus");
6737 bpf_error(cstate
, "'vrrp proto' is bogus");
6741 bpf_error(cstate
, "'aarp proto' is bogus");
6745 switch (cstate
->linktype
) {
6749 * Frame Relay packets typically have an OSI
6750 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6751 * generates code to check for all the OSI
6752 * NLPIDs, so calling it and then adding a check
6753 * for the particular NLPID for which we're
6754 * looking is bogus, as we can just check for
6757 * What we check for is the NLPID and a frame
6758 * control field value of UI, i.e. 0x03 followed
6761 * XXX - assumes a 2-byte Frame Relay header with
6762 * DLCI and flags. What if the address is longer?
6764 * XXX - what about SNAP-encapsulated frames?
6766 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6772 * Cisco uses an Ethertype lookalike - for OSI,
6775 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6776 /* OSI in C-HDLC is stuffed with a fudge byte */
6777 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6782 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6783 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6789 bpf_error(cstate
, "'esis proto' is bogus");
6793 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6795 * 4 is the offset of the PDU type relative to the IS-IS
6798 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6803 bpf_error(cstate
, "'clnp proto' is not supported");
6807 bpf_error(cstate
, "'stp proto' is bogus");
6811 bpf_error(cstate
, "'ipx proto' is bogus");
6815 bpf_error(cstate
, "'netbeui proto' is bogus");
6819 bpf_error(cstate
, "'l1 proto' is bogus");
6823 bpf_error(cstate
, "'l2 proto' is bogus");
6827 bpf_error(cstate
, "'iih proto' is bogus");
6831 bpf_error(cstate
, "'snp proto' is bogus");
6835 bpf_error(cstate
, "'csnp proto' is bogus");
6839 bpf_error(cstate
, "'psnp proto' is bogus");
6843 bpf_error(cstate
, "'lsp proto' is bogus");
6847 bpf_error(cstate
, "'radio proto' is bogus");
6851 bpf_error(cstate
, "'carp proto' is bogus");
6862 * Convert a non-numeric name to a port number.
6865 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6867 struct addrinfo hints
, *res
, *ai
;
6869 struct sockaddr_in
*in4
;
6871 struct sockaddr_in6
*in6
;
6876 * We check for both TCP and UDP in case there are
6877 * ambiguous entries.
6879 memset(&hints
, 0, sizeof(hints
));
6880 hints
.ai_family
= PF_UNSPEC
;
6881 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6882 hints
.ai_protocol
= ipproto
;
6883 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6890 * No such port. Just return -1.
6897 * We don't use strerror() because it's not
6898 * guaranteed to be thread-safe on all platforms
6899 * (probably because it might use a non-thread-local
6900 * buffer into which to format an error message
6901 * if the error code isn't one for which it has
6902 * a canned string; three cheers for C string
6905 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6907 port
= -2; /* a real error */
6913 * This is a real error, not just "there's
6914 * no such service name".
6916 * We don't use gai_strerror() because it's not
6917 * guaranteed to be thread-safe on all platforms
6918 * (probably because it might use a non-thread-local
6919 * buffer into which to format an error message
6920 * if the error code isn't one for which it has
6921 * a canned string; three cheers for C string
6924 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6926 port
= -2; /* a real error */
6931 * OK, we found it. Did it find anything?
6933 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6935 * Does it have an address?
6937 if (ai
->ai_addr
!= NULL
) {
6939 * Yes. Get a port number; we're done.
6941 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6942 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6943 port
= ntohs(in4
->sin_port
);
6947 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6948 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6949 port
= ntohs(in6
->sin6_port
);
6961 * Convert a string to a port number.
6964 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6974 * See if it's a number.
6976 ret
= stoulen(string
, string_size
, &val
, cstate
);
6980 /* Unknown port type - it's just a number. */
6981 *proto
= PROTO_UNDEF
;
6984 case STOULEN_NOT_OCTAL_NUMBER
:
6985 case STOULEN_NOT_HEX_NUMBER
:
6986 case STOULEN_NOT_DECIMAL_NUMBER
:
6988 * Not a valid number; try looking it up as a port.
6990 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6991 memcpy(cpy
, string
, string_size
);
6992 cpy
[string_size
] = '\0';
6993 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6994 if (tcp_port
== -2) {
6996 * We got a hard error; the error string has
7000 longjmp(cstate
->top_ctx
, 1);
7003 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
7004 if (udp_port
== -2) {
7006 * We got a hard error; the error string has
7010 longjmp(cstate
->top_ctx
, 1);
7015 * We need to check /etc/services for ambiguous entries.
7016 * If we find an ambiguous entry, and it has the
7017 * same port number, change the proto to PROTO_UNDEF
7018 * so both TCP and UDP will be checked.
7020 if (tcp_port
>= 0) {
7021 val
= (bpf_u_int32
)tcp_port
;
7022 *proto
= IPPROTO_TCP
;
7023 if (udp_port
>= 0) {
7024 if (udp_port
== tcp_port
)
7025 *proto
= PROTO_UNDEF
;
7028 /* Can't handle ambiguous names that refer
7029 to different port numbers. */
7030 warning("ambiguous port %s in /etc/services",
7037 if (udp_port
>= 0) {
7038 val
= (bpf_u_int32
)udp_port
;
7039 *proto
= IPPROTO_UDP
;
7043 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
7045 longjmp(cstate
->top_ctx
, 1);
7052 /* Error already set. */
7053 longjmp(cstate
->top_ctx
, 1);
7060 /* Should not happen */
7061 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
7062 longjmp(cstate
->top_ctx
, 1);
7069 * Convert a string in the form PPP-PPP, which correspond to ports, to
7070 * a starting and ending port in a port range.
7073 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
7074 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
7077 const char *first
, *second
;
7078 size_t first_size
, second_size
;
7081 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
7082 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
7085 * Make sure there are no other hyphens.
7087 * XXX - we support named ports, but there are some port names
7088 * in /etc/services that include hyphens, so this would rule
7091 if (strchr(hyphen_off
+ 1, '-') != NULL
)
7092 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
7096 * Get the length of the first port.
7099 first_size
= hyphen_off
- string
;
7100 if (first_size
== 0) {
7101 /* Range of "-port", which we don't support. */
7102 bpf_error(cstate
, "port range '%s' has no starting port", string
);
7106 * Try to convert it to a port.
7108 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
7109 save_proto
= *proto
;
7112 * Get the length of the second port.
7114 second
= hyphen_off
+ 1;
7115 second_size
= strlen(second
);
7116 if (second_size
== 0) {
7117 /* Range of "port-", which we don't support. */
7118 bpf_error(cstate
, "port range '%s' has no ending port", string
);
7122 * Try to convert it to a port.
7124 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
7125 if (*proto
!= save_proto
)
7126 *proto
= PROTO_UNDEF
;
7130 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
7132 int proto
= q
.proto
;
7136 bpf_u_int32 mask
, addr
;
7137 struct addrinfo
*res
, *res0
;
7138 struct sockaddr_in
*sin4
;
7141 struct sockaddr_in6
*sin6
;
7142 struct in6_addr mask128
;
7144 struct block
*b
, *tmp
;
7145 int port
, real_proto
;
7146 bpf_u_int32 port1
, port2
;
7149 * Catch errors reported by us and routines below us, and return NULL
7152 if (setjmp(cstate
->top_ctx
))
7158 addr
= pcap_nametonetaddr(name
);
7160 bpf_error(cstate
, "unknown network '%s'", name
);
7161 /* Left justify network addr and calculate its network mask */
7163 while (addr
&& (addr
& 0xff000000) == 0) {
7167 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7171 if (proto
== Q_LINK
) {
7172 switch (cstate
->linktype
) {
7175 case DLT_NETANALYZER
:
7176 case DLT_NETANALYZER_TRANSPARENT
:
7177 eaddr
= pcap_ether_hostton(name
);
7180 "unknown ether host '%s'", name
);
7181 tmp
= gen_prevlinkhdr_check(cstate
);
7182 b
= gen_ehostop(cstate
, eaddr
, dir
);
7189 eaddr
= pcap_ether_hostton(name
);
7192 "unknown FDDI host '%s'", name
);
7193 b
= gen_fhostop(cstate
, eaddr
, dir
);
7198 eaddr
= pcap_ether_hostton(name
);
7201 "unknown token ring host '%s'", name
);
7202 b
= gen_thostop(cstate
, eaddr
, dir
);
7206 case DLT_IEEE802_11
:
7207 case DLT_PRISM_HEADER
:
7208 case DLT_IEEE802_11_RADIO_AVS
:
7209 case DLT_IEEE802_11_RADIO
:
7211 eaddr
= pcap_ether_hostton(name
);
7214 "unknown 802.11 host '%s'", name
);
7215 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7219 case DLT_IP_OVER_FC
:
7220 eaddr
= pcap_ether_hostton(name
);
7223 "unknown Fibre Channel host '%s'", name
);
7224 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7229 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7230 } else if (proto
== Q_DECNET
) {
7232 * A long time ago on Ultrix libpcap supported
7233 * translation of DECnet host names into DECnet
7234 * addresses, but this feature is history now.
7236 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7239 memset(&mask128
, 0xff, sizeof(mask128
));
7241 res0
= res
= pcap_nametoaddrinfo(name
);
7243 bpf_error(cstate
, "unknown host '%s'", name
);
7250 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7251 tproto
== Q_DEFAULT
) {
7257 for (res
= res0
; res
; res
= res
->ai_next
) {
7258 switch (res
->ai_family
) {
7261 if (tproto
== Q_IPV6
)
7265 sin4
= (struct sockaddr_in
*)
7267 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7268 0xffffffff, tproto
, dir
, q
.addr
);
7272 if (tproto6
== Q_IP
)
7275 sin6
= (struct sockaddr_in6
*)
7277 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7278 &mask128
, tproto6
, dir
, q
.addr
);
7291 bpf_error(cstate
, "unknown host '%s'%s", name
,
7292 (proto
== Q_DEFAULT
)
7294 : " for specified address family");
7300 if (proto
!= Q_DEFAULT
&&
7301 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7302 bpf_error(cstate
, "illegal qualifier of 'port'");
7303 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7304 bpf_error(cstate
, "unknown port '%s'", name
);
7305 if (proto
== Q_UDP
) {
7306 if (real_proto
== IPPROTO_TCP
)
7307 bpf_error(cstate
, "port '%s' is tcp", name
);
7308 else if (real_proto
== IPPROTO_SCTP
)
7309 bpf_error(cstate
, "port '%s' is sctp", name
);
7311 /* override PROTO_UNDEF */
7312 real_proto
= IPPROTO_UDP
;
7314 if (proto
== Q_TCP
) {
7315 if (real_proto
== IPPROTO_UDP
)
7316 bpf_error(cstate
, "port '%s' is udp", name
);
7318 else if (real_proto
== IPPROTO_SCTP
)
7319 bpf_error(cstate
, "port '%s' is sctp", name
);
7321 /* override PROTO_UNDEF */
7322 real_proto
= IPPROTO_TCP
;
7324 if (proto
== Q_SCTP
) {
7325 if (real_proto
== IPPROTO_UDP
)
7326 bpf_error(cstate
, "port '%s' is udp", name
);
7328 else if (real_proto
== IPPROTO_TCP
)
7329 bpf_error(cstate
, "port '%s' is tcp", name
);
7331 /* override PROTO_UNDEF */
7332 real_proto
= IPPROTO_SCTP
;
7335 bpf_error(cstate
, "illegal port number %d < 0", port
);
7337 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7338 b
= gen_port(cstate
, port
, real_proto
, dir
);
7339 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7343 if (proto
!= Q_DEFAULT
&&
7344 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7345 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7346 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7347 if (proto
== Q_UDP
) {
7348 if (real_proto
== IPPROTO_TCP
)
7349 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7350 else if (real_proto
== IPPROTO_SCTP
)
7351 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7353 /* override PROTO_UNDEF */
7354 real_proto
= IPPROTO_UDP
;
7356 if (proto
== Q_TCP
) {
7357 if (real_proto
== IPPROTO_UDP
)
7358 bpf_error(cstate
, "port in range '%s' is udp", name
);
7359 else if (real_proto
== IPPROTO_SCTP
)
7360 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7362 /* override PROTO_UNDEF */
7363 real_proto
= IPPROTO_TCP
;
7365 if (proto
== Q_SCTP
) {
7366 if (real_proto
== IPPROTO_UDP
)
7367 bpf_error(cstate
, "port in range '%s' is udp", name
);
7368 else if (real_proto
== IPPROTO_TCP
)
7369 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7371 /* override PROTO_UNDEF */
7372 real_proto
= IPPROTO_SCTP
;
7375 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7377 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7379 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7380 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7385 eaddr
= pcap_ether_hostton(name
);
7387 bpf_error(cstate
, "unknown ether host: %s", name
);
7389 res
= pcap_nametoaddrinfo(name
);
7392 bpf_error(cstate
, "unknown host '%s'", name
);
7393 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7398 bpf_error(cstate
, "unknown host '%s'", name
);
7401 bpf_error(cstate
, "'gateway' not supported in this configuration");
7405 real_proto
= lookup_proto(cstate
, name
, proto
);
7406 if (real_proto
>= 0)
7407 return gen_proto(cstate
, real_proto
, proto
, dir
);
7409 bpf_error(cstate
, "unknown protocol: %s", name
);
7411 #if !defined(NO_PROTOCHAIN)
7413 real_proto
= lookup_proto(cstate
, name
, proto
);
7414 if (real_proto
>= 0)
7415 return gen_protochain(cstate
, real_proto
, proto
);
7417 bpf_error(cstate
, "unknown protocol: %s", name
);
7418 #endif /* !defined(NO_PROTOCHAIN) */
7429 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7430 bpf_u_int32 masklen
, struct qual q
)
7432 register int nlen
, mlen
;
7437 * Catch errors reported by us and routines below us, and return NULL
7440 if (setjmp(cstate
->top_ctx
))
7443 nlen
= pcapint_atoin(s1
, &n
);
7445 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7446 /* Promote short ipaddr */
7450 mlen
= pcapint_atoin(s2
, &m
);
7452 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7453 /* Promote short ipaddr */
7456 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7459 /* Convert mask len to mask */
7461 bpf_error(cstate
, "mask length must be <= 32");
7462 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7463 m
= (bpf_u_int32
)m64
;
7465 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7472 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7475 bpf_error(cstate
, "Mask syntax for networks only");
7482 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7490 * Catch errors reported by us and routines below us, and return NULL
7493 if (setjmp(cstate
->top_ctx
))
7500 * v contains a 32-bit unsigned parsed from a string of the
7501 * form {N}, which could be decimal, hexadecimal or octal.
7502 * Although it would be possible to use the value as a raw
7503 * 16-bit DECnet address when the value fits into 16 bits, this
7504 * would be a questionable feature: DECnet address wire
7505 * encoding is little-endian, so this would not work as
7506 * intuitively as the same works for [big-endian] IPv4
7507 * addresses (0x01020304 means 1.2.3.4).
7509 if (proto
== Q_DECNET
)
7510 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7512 } else if (proto
== Q_DECNET
) {
7514 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7515 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7516 * for a valid DECnet address.
7518 vlen
= pcapint_atodn(s
, &v
);
7520 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7523 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7524 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7527 vlen
= pcapint_atoin(s
, &v
);
7529 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7537 if (proto
== Q_DECNET
)
7538 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7539 else if (proto
== Q_LINK
) {
7540 bpf_error(cstate
, "illegal link layer address");
7543 if (s
== NULL
&& q
.addr
== Q_NET
) {
7544 /* Promote short net number */
7545 while (v
&& (v
& 0xff000000) == 0) {
7550 /* Promote short ipaddr */
7552 mask
<<= 32 - vlen
;
7554 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7559 proto
= IPPROTO_UDP
;
7560 else if (proto
== Q_TCP
)
7561 proto
= IPPROTO_TCP
;
7562 else if (proto
== Q_SCTP
)
7563 proto
= IPPROTO_SCTP
;
7564 else if (proto
== Q_DEFAULT
)
7565 proto
= PROTO_UNDEF
;
7567 bpf_error(cstate
, "illegal qualifier of 'port'");
7570 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7574 b
= gen_port(cstate
, v
, proto
, dir
);
7575 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7581 proto
= IPPROTO_UDP
;
7582 else if (proto
== Q_TCP
)
7583 proto
= IPPROTO_TCP
;
7584 else if (proto
== Q_SCTP
)
7585 proto
= IPPROTO_SCTP
;
7586 else if (proto
== Q_DEFAULT
)
7587 proto
= PROTO_UNDEF
;
7589 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7592 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7596 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7597 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7602 bpf_error(cstate
, "'gateway' requires a name");
7606 return gen_proto(cstate
, v
, proto
, dir
);
7608 #if !defined(NO_PROTOCHAIN)
7610 return gen_protochain(cstate
, v
, proto
);
7626 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7629 struct addrinfo
*res
;
7630 struct in6_addr
*addr
;
7631 struct in6_addr mask
;
7633 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7636 * Catch errors reported by us and routines below us, and return NULL
7639 if (setjmp(cstate
->top_ctx
))
7642 res
= pcap_nametoaddrinfo(s
);
7644 bpf_error(cstate
, "invalid ip6 address %s", s
);
7647 bpf_error(cstate
, "%s resolved to multiple address", s
);
7648 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7650 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7651 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7652 memset(&mask
, 0, sizeof(mask
));
7653 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7655 mask
.s6_addr
[masklen
/ 8] =
7656 (0xff << (8 - masklen
% 8)) & 0xff;
7659 memcpy(a
, addr
, sizeof(a
));
7660 memcpy(m
, &mask
, sizeof(m
));
7661 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7662 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7663 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7671 bpf_error(cstate
, "Mask syntax for networks only");
7675 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7681 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7688 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7690 struct block
*b
, *tmp
;
7693 * Catch errors reported by us and routines below us, and return NULL
7696 if (setjmp(cstate
->top_ctx
))
7699 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7700 cstate
->e
= pcap_ether_aton(s
);
7701 if (cstate
->e
== NULL
)
7702 bpf_error(cstate
, "malloc");
7703 switch (cstate
->linktype
) {
7705 case DLT_NETANALYZER
:
7706 case DLT_NETANALYZER_TRANSPARENT
:
7707 tmp
= gen_prevlinkhdr_check(cstate
);
7708 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7713 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7716 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7718 case DLT_IEEE802_11
:
7719 case DLT_PRISM_HEADER
:
7720 case DLT_IEEE802_11_RADIO_AVS
:
7721 case DLT_IEEE802_11_RADIO
:
7723 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7725 case DLT_IP_OVER_FC
:
7726 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7731 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7738 bpf_error(cstate
, "ethernet address used in non-ether expression");
7743 sappend(struct slist
*s0
, struct slist
*s1
)
7746 * This is definitely not the best way to do this, but the
7747 * lists will rarely get long.
7754 static struct slist
*
7755 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7759 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7764 static struct slist
*
7765 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7769 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7775 * Modify "index" to use the value stored into its register as an
7776 * offset relative to the beginning of the header for the protocol
7777 * "proto", and allocate a register and put an item "size" bytes long
7778 * (1, 2, or 4) at that offset into that register, making it the register
7781 static struct arth
*
7782 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7786 struct slist
*s
, *tmp
;
7788 int regno
= alloc_reg(cstate
);
7790 free_reg(cstate
, inst
->regno
);
7794 bpf_error(cstate
, "data size must be 1, 2, or 4");
7811 bpf_error(cstate
, "unsupported index operation");
7815 * The offset is relative to the beginning of the packet
7816 * data, if we have a radio header. (If we don't, this
7819 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7820 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7821 cstate
->linktype
!= DLT_PRISM_HEADER
)
7822 bpf_error(cstate
, "radio information not present in capture");
7825 * Load into the X register the offset computed into the
7826 * register specified by "index".
7828 s
= xfer_to_x(cstate
, inst
);
7831 * Load the item at that offset.
7833 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7835 sappend(inst
->s
, s
);
7840 * The offset is relative to the beginning of
7841 * the link-layer header.
7843 * XXX - what about ATM LANE? Should the index be
7844 * relative to the beginning of the AAL5 frame, so
7845 * that 0 refers to the beginning of the LE Control
7846 * field, or relative to the beginning of the LAN
7847 * frame, so that 0 refers, for Ethernet LANE, to
7848 * the beginning of the destination address?
7850 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7853 * If "s" is non-null, it has code to arrange that the
7854 * X register contains the length of the prefix preceding
7855 * the link-layer header. Add to it the offset computed
7856 * into the register specified by "index", and move that
7857 * into the X register. Otherwise, just load into the X
7858 * register the offset computed into the register specified
7862 sappend(s
, xfer_to_a(cstate
, inst
));
7863 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7864 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7866 s
= xfer_to_x(cstate
, inst
);
7869 * Load the item at the sum of the offset we've put in the
7870 * X register and the offset of the start of the link
7871 * layer header (which is 0 if the radio header is
7872 * variable-length; that header length is what we put
7873 * into the X register and then added to the index).
7875 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7876 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7878 sappend(inst
->s
, s
);
7892 * The offset is relative to the beginning of
7893 * the network-layer header.
7894 * XXX - are there any cases where we want
7895 * cstate->off_nl_nosnap?
7897 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7900 * If "s" is non-null, it has code to arrange that the
7901 * X register contains the variable part of the offset
7902 * of the link-layer payload. Add to it the offset
7903 * computed into the register specified by "index",
7904 * and move that into the X register. Otherwise, just
7905 * load into the X register the offset computed into
7906 * the register specified by "index".
7909 sappend(s
, xfer_to_a(cstate
, inst
));
7910 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7911 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7913 s
= xfer_to_x(cstate
, inst
);
7916 * Load the item at the sum of the offset we've put in the
7917 * X register, the offset of the start of the network
7918 * layer header from the beginning of the link-layer
7919 * payload, and the constant part of the offset of the
7920 * start of the link-layer payload.
7922 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7923 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7925 sappend(inst
->s
, s
);
7928 * Do the computation only if the packet contains
7929 * the protocol in question.
7931 b
= gen_proto_abbrev_internal(cstate
, proto
);
7933 gen_and(inst
->b
, b
);
7947 * The offset is relative to the beginning of
7948 * the transport-layer header.
7950 * Load the X register with the length of the IPv4 header
7951 * (plus the offset of the link-layer header, if it's
7952 * a variable-length header), in bytes.
7954 * XXX - are there any cases where we want
7955 * cstate->off_nl_nosnap?
7956 * XXX - we should, if we're built with
7957 * IPv6 support, generate code to load either
7958 * IPv4, IPv6, or both, as appropriate.
7960 s
= gen_loadx_iphdrlen(cstate
);
7963 * The X register now contains the sum of the variable
7964 * part of the offset of the link-layer payload and the
7965 * length of the network-layer header.
7967 * Load into the A register the offset relative to
7968 * the beginning of the transport layer header,
7969 * add the X register to that, move that to the
7970 * X register, and load with an offset from the
7971 * X register equal to the sum of the constant part of
7972 * the offset of the link-layer payload and the offset,
7973 * relative to the beginning of the link-layer payload,
7974 * of the network-layer header.
7976 sappend(s
, xfer_to_a(cstate
, inst
));
7977 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7978 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7979 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7980 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7981 sappend(inst
->s
, s
);
7984 * Do the computation only if the packet contains
7985 * the protocol in question - which is true only
7986 * if this is an IP datagram and is the first or
7987 * only fragment of that datagram.
7989 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7991 gen_and(inst
->b
, b
);
7992 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7997 * Do the computation only if the packet contains
7998 * the protocol in question.
8000 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
8002 gen_and(inst
->b
, b
);
8006 * Check if we have an icmp6 next header
8008 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
8010 gen_and(inst
->b
, b
);
8013 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8015 * If "s" is non-null, it has code to arrange that the
8016 * X register contains the variable part of the offset
8017 * of the link-layer payload. Add to it the offset
8018 * computed into the register specified by "index",
8019 * and move that into the X register. Otherwise, just
8020 * load into the X register the offset computed into
8021 * the register specified by "index".
8024 sappend(s
, xfer_to_a(cstate
, inst
));
8025 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
8026 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
8028 s
= xfer_to_x(cstate
, inst
);
8031 * Load the item at the sum of the offset we've put in the
8032 * X register, the offset of the start of the network
8033 * layer header from the beginning of the link-layer
8034 * payload, and the constant part of the offset of the
8035 * start of the link-layer payload.
8037 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
8038 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
8041 sappend(inst
->s
, s
);
8045 inst
->regno
= regno
;
8046 s
= new_stmt(cstate
, BPF_ST
);
8048 sappend(inst
->s
, s
);
8054 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
8058 * Catch errors reported by us and routines below us, and return NULL
8061 if (setjmp(cstate
->top_ctx
))
8064 return gen_load_internal(cstate
, proto
, inst
, size
);
8067 static struct block
*
8068 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8069 struct arth
*a1
, int reversed
)
8071 struct slist
*s0
, *s1
, *s2
;
8072 struct block
*b
, *tmp
;
8074 s0
= xfer_to_x(cstate
, a1
);
8075 s1
= xfer_to_a(cstate
, a0
);
8076 if (code
== BPF_JEQ
) {
8077 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
8078 b
= new_block(cstate
, JMP(code
));
8082 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
8088 sappend(a0
->s
, a1
->s
);
8092 free_reg(cstate
, a0
->regno
);
8093 free_reg(cstate
, a1
->regno
);
8095 /* 'and' together protocol checks */
8098 gen_and(a0
->b
, tmp
= a1
->b
);
8112 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8113 struct arth
*a1
, int reversed
)
8116 * Catch errors reported by us and routines below us, and return NULL
8119 if (setjmp(cstate
->top_ctx
))
8122 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
8126 gen_loadlen(compiler_state_t
*cstate
)
8133 * Catch errors reported by us and routines below us, and return NULL
8136 if (setjmp(cstate
->top_ctx
))
8139 regno
= alloc_reg(cstate
);
8140 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8141 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8142 s
->next
= new_stmt(cstate
, BPF_ST
);
8143 s
->next
->s
.k
= regno
;
8150 static struct arth
*
8151 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8157 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8159 reg
= alloc_reg(cstate
);
8161 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8163 s
->next
= new_stmt(cstate
, BPF_ST
);
8172 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8175 * Catch errors reported by us and routines below us, and return NULL
8178 if (setjmp(cstate
->top_ctx
))
8181 return gen_loadi_internal(cstate
, val
);
8185 * The a_arg dance is to avoid annoying whining by compilers that
8186 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8187 * It's not *used* after setjmp returns.
8190 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8192 struct arth
*a
= a_arg
;
8196 * Catch errors reported by us and routines below us, and return NULL
8199 if (setjmp(cstate
->top_ctx
))
8202 s
= xfer_to_a(cstate
, a
);
8204 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8207 s
= new_stmt(cstate
, BPF_ST
);
8215 * The a0_arg dance is to avoid annoying whining by compilers that
8216 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8217 * It's not *used* after setjmp returns.
8220 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8223 struct arth
*a0
= a0_arg
;
8224 struct slist
*s0
, *s1
, *s2
;
8227 * Catch errors reported by us and routines below us, and return NULL
8230 if (setjmp(cstate
->top_ctx
))
8234 * Disallow division by, or modulus by, zero; we do this here
8235 * so that it gets done even if the optimizer is disabled.
8237 * Also disallow shifts by a value greater than 31; we do this
8238 * here, for the same reason.
8240 if (code
== BPF_DIV
) {
8241 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8242 bpf_error(cstate
, "division by zero");
8243 } else if (code
== BPF_MOD
) {
8244 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8245 bpf_error(cstate
, "modulus by zero");
8246 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8247 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8248 bpf_error(cstate
, "shift by more than 31 bits");
8250 s0
= xfer_to_x(cstate
, a1
);
8251 s1
= xfer_to_a(cstate
, a0
);
8252 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8257 sappend(a0
->s
, a1
->s
);
8259 free_reg(cstate
, a0
->regno
);
8260 free_reg(cstate
, a1
->regno
);
8262 s0
= new_stmt(cstate
, BPF_ST
);
8263 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8270 * Initialize the table of used registers and the current register.
8273 init_regs(compiler_state_t
*cstate
)
8276 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8280 * Return the next free register.
8283 alloc_reg(compiler_state_t
*cstate
)
8285 int n
= BPF_MEMWORDS
;
8288 if (cstate
->regused
[cstate
->curreg
])
8289 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8291 cstate
->regused
[cstate
->curreg
] = 1;
8292 return cstate
->curreg
;
8295 bpf_error(cstate
, "too many registers needed to evaluate expression");
8300 * Return a register to the table so it can
8304 free_reg(compiler_state_t
*cstate
, int n
)
8306 cstate
->regused
[n
] = 0;
8309 static struct block
*
8310 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8315 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8316 b
= new_block(cstate
, JMP(jmp
));
8324 gen_greater(compiler_state_t
*cstate
, int n
)
8327 * Catch errors reported by us and routines below us, and return NULL
8330 if (setjmp(cstate
->top_ctx
))
8333 return gen_len(cstate
, BPF_JGE
, n
);
8337 * Actually, this is less than or equal.
8340 gen_less(compiler_state_t
*cstate
, int n
)
8345 * Catch errors reported by us and routines below us, and return NULL
8348 if (setjmp(cstate
->top_ctx
))
8351 b
= gen_len(cstate
, BPF_JGT
, n
);
8358 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8359 * the beginning of the link-layer header.
8360 * XXX - that means you can't test values in the radiotap header, but
8361 * as that header is difficult if not impossible to parse generally
8362 * without a loop, that might not be a severe problem. A new keyword
8363 * "radio" could be added for that, although what you'd really want
8364 * would be a way of testing particular radio header values, which
8365 * would generate code appropriate to the radio header in question.
8368 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8374 * Catch errors reported by us and routines below us, and return NULL
8377 if (setjmp(cstate
->top_ctx
))
8385 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8388 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8392 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8396 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8400 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8404 b
= new_block(cstate
, JMP(BPF_JEQ
));
8412 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8414 bpf_u_int32 hostmask
;
8415 struct block
*b0
, *b1
, *b2
;
8416 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8419 * Catch errors reported by us and routines below us, and return NULL
8422 if (setjmp(cstate
->top_ctx
))
8429 switch (cstate
->linktype
) {
8431 case DLT_ARCNET_LINUX
:
8432 // ARCnet broadcast is [8-bit] destination address 0.
8433 return gen_ahostop(cstate
, 0, Q_DST
);
8435 case DLT_NETANALYZER
:
8436 case DLT_NETANALYZER_TRANSPARENT
:
8437 b1
= gen_prevlinkhdr_check(cstate
);
8438 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8443 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8445 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8446 case DLT_IEEE802_11
:
8447 case DLT_PRISM_HEADER
:
8448 case DLT_IEEE802_11_RADIO_AVS
:
8449 case DLT_IEEE802_11_RADIO
:
8451 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8452 case DLT_IP_OVER_FC
:
8453 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8455 bpf_error(cstate
, "not a broadcast link");
8461 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8462 * as an indication that we don't know the netmask, and fail
8465 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8466 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8467 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8468 hostmask
= ~cstate
->netmask
;
8469 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8470 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8471 ~0 & hostmask
, hostmask
);
8476 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8481 * Generate code to test the low-order bit of a MAC address (that's
8482 * the bottom bit of the *first* byte).
8484 static struct block
*
8485 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8487 register struct block
*b0
;
8488 register struct slist
*s
;
8490 /* link[offset] & 1 != 0 */
8491 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8492 b0
= new_block(cstate
, JMP(BPF_JSET
));
8499 gen_multicast(compiler_state_t
*cstate
, int proto
)
8501 register struct block
*b0
, *b1
, *b2
;
8502 register struct slist
*s
;
8505 * Catch errors reported by us and routines below us, and return NULL
8508 if (setjmp(cstate
->top_ctx
))
8515 switch (cstate
->linktype
) {
8517 case DLT_ARCNET_LINUX
:
8518 // ARCnet multicast is the same as broadcast.
8519 return gen_ahostop(cstate
, 0, Q_DST
);
8521 case DLT_NETANALYZER
:
8522 case DLT_NETANALYZER_TRANSPARENT
:
8523 b1
= gen_prevlinkhdr_check(cstate
);
8524 /* ether[0] & 1 != 0 */
8525 b0
= gen_mac_multicast(cstate
, 0);
8531 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8533 * XXX - was that referring to bit-order issues?
8535 /* fddi[1] & 1 != 0 */
8536 return gen_mac_multicast(cstate
, 1);
8538 /* tr[2] & 1 != 0 */
8539 return gen_mac_multicast(cstate
, 2);
8540 case DLT_IEEE802_11
:
8541 case DLT_PRISM_HEADER
:
8542 case DLT_IEEE802_11_RADIO_AVS
:
8543 case DLT_IEEE802_11_RADIO
:
8548 * For control frames, there is no DA.
8550 * For management frames, DA is at an
8551 * offset of 4 from the beginning of
8554 * For data frames, DA is at an offset
8555 * of 4 from the beginning of the packet
8556 * if To DS is clear and at an offset of
8557 * 16 from the beginning of the packet
8562 * Generate the tests to be done for data frames.
8564 * First, check for To DS set, i.e. "link[1] & 0x01".
8566 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8567 b1
= new_block(cstate
, JMP(BPF_JSET
));
8568 b1
->s
.k
= 0x01; /* To DS */
8572 * If To DS is set, the DA is at 16.
8574 b0
= gen_mac_multicast(cstate
, 16);
8578 * Now, check for To DS not set, i.e. check
8579 * "!(link[1] & 0x01)".
8581 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8582 b2
= new_block(cstate
, JMP(BPF_JSET
));
8583 b2
->s
.k
= 0x01; /* To DS */
8588 * If To DS is not set, the DA is at 4.
8590 b1
= gen_mac_multicast(cstate
, 4);
8594 * Now OR together the last two checks. That gives
8595 * the complete set of checks for data frames.
8600 * Now check for a data frame.
8601 * I.e, check "link[0] & 0x08".
8603 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8604 b1
= new_block(cstate
, JMP(BPF_JSET
));
8609 * AND that with the checks done for data frames.
8614 * If the high-order bit of the type value is 0, this
8615 * is a management frame.
8616 * I.e, check "!(link[0] & 0x08)".
8618 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8619 b2
= new_block(cstate
, JMP(BPF_JSET
));
8625 * For management frames, the DA is at 4.
8627 b1
= gen_mac_multicast(cstate
, 4);
8631 * OR that with the checks done for data frames.
8632 * That gives the checks done for management and
8638 * If the low-order bit of the type value is 1,
8639 * this is either a control frame or a frame
8640 * with a reserved type, and thus not a
8643 * I.e., check "!(link[0] & 0x04)".
8645 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8646 b1
= new_block(cstate
, JMP(BPF_JSET
));
8652 * AND that with the checks for data and management
8657 case DLT_IP_OVER_FC
:
8658 b0
= gen_mac_multicast(cstate
, 2);
8663 /* Link not known to support multicasts */
8667 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8668 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8673 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8674 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8678 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8684 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8685 * we can look at special meta-data in the filter expression; otherwise we
8686 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8687 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8688 * pcap_activate() conditionally sets.
8691 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8693 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8695 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8697 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8702 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8704 register struct block
*b0
;
8707 * Catch errors reported by us and routines below us, and return NULL
8710 if (setjmp(cstate
->top_ctx
))
8714 * Only some data link types support ifindex qualifiers.
8716 switch (cstate
->linktype
) {
8717 case DLT_LINUX_SLL2
:
8718 /* match packets on this interface */
8719 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8722 #if defined(__linux__)
8723 require_basic_bpf_extensions(cstate
, "ifindex");
8725 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8727 #else /* defined(__linux__) */
8728 bpf_error(cstate
, "ifindex not supported on %s",
8729 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8731 #endif /* defined(__linux__) */
8737 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8738 * Outbound traffic is sent by this machine, while inbound traffic is
8739 * sent by a remote machine (and may include packets destined for a
8740 * unicast or multicast link-layer address we are not subscribing to).
8741 * These are the same definitions implemented by pcap_setdirection().
8742 * Capturing only unicast traffic destined for this host is probably
8743 * better accomplished using a higher-layer filter.
8746 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8748 register struct block
*b0
;
8751 * Catch errors reported by us and routines below us, and return NULL
8754 if (setjmp(cstate
->top_ctx
))
8758 * Only some data link types support inbound/outbound qualifiers.
8760 switch (cstate
->linktype
) {
8762 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8763 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8764 gen_loadi_internal(cstate
, 0),
8765 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8769 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8770 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8774 /* match outgoing packets */
8775 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8777 /* to filter on inbound traffic, invert the match */
8782 case DLT_LINUX_SLL2
:
8783 /* match outgoing packets */
8784 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8786 /* to filter on inbound traffic, invert the match */
8792 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8793 outbound
? PF_OUT
: PF_IN
);
8797 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8800 case DLT_JUNIPER_MFR
:
8801 case DLT_JUNIPER_MLFR
:
8802 case DLT_JUNIPER_MLPPP
:
8803 case DLT_JUNIPER_ATM1
:
8804 case DLT_JUNIPER_ATM2
:
8805 case DLT_JUNIPER_PPPOE
:
8806 case DLT_JUNIPER_PPPOE_ATM
:
8807 case DLT_JUNIPER_GGSN
:
8808 case DLT_JUNIPER_ES
:
8809 case DLT_JUNIPER_MONITOR
:
8810 case DLT_JUNIPER_SERVICES
:
8811 case DLT_JUNIPER_ETHER
:
8812 case DLT_JUNIPER_PPP
:
8813 case DLT_JUNIPER_FRELAY
:
8814 case DLT_JUNIPER_CHDLC
:
8815 case DLT_JUNIPER_VP
:
8816 case DLT_JUNIPER_ST
:
8817 case DLT_JUNIPER_ISM
:
8818 case DLT_JUNIPER_VS
:
8819 case DLT_JUNIPER_SRX_E2E
:
8820 case DLT_JUNIPER_FIBRECHANNEL
:
8821 case DLT_JUNIPER_ATM_CEMIC
:
8822 /* juniper flags (including direction) are stored
8823 * the byte after the 3-byte magic number */
8824 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8829 * If we have packet meta-data indicating a direction,
8830 * and that metadata can be checked by BPF code, check
8831 * it. Otherwise, give up, as this link-layer type has
8832 * nothing in the packet data.
8834 * Currently, the only platform where a BPF filter can
8835 * check that metadata is Linux with the in-kernel
8836 * BPF interpreter. If other packet capture mechanisms
8837 * and BPF filters also supported this, it would be
8838 * nice. It would be even better if they made that
8839 * metadata available so that we could provide it
8840 * with newer capture APIs, allowing it to be saved
8843 #if defined(__linux__)
8844 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8845 /* match outgoing packets */
8846 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8849 /* to filter on inbound traffic, invert the match */
8852 #else /* defined(__linux__) */
8853 bpf_error(cstate
, "inbound/outbound not supported on %s",
8854 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8856 #endif /* defined(__linux__) */
8861 /* PF firewall log matched interface */
8863 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8869 * Catch errors reported by us and routines below us, and return NULL
8872 if (setjmp(cstate
->top_ctx
))
8875 if (cstate
->linktype
!= DLT_PFLOG
) {
8876 bpf_error(cstate
, "ifname supported only on PF linktype");
8879 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8880 off
= offsetof(struct pfloghdr
, ifname
);
8881 if (strlen(ifname
) >= len
) {
8882 bpf_error(cstate
, "ifname interface names can only be %d characters",
8886 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8887 (const u_char
*)ifname
);
8891 /* PF firewall log ruleset name */
8893 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8898 * Catch errors reported by us and routines below us, and return NULL
8901 if (setjmp(cstate
->top_ctx
))
8904 if (cstate
->linktype
!= DLT_PFLOG
) {
8905 bpf_error(cstate
, "ruleset supported only on PF linktype");
8909 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8910 bpf_error(cstate
, "ruleset names can only be %ld characters",
8911 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8915 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8916 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8920 /* PF firewall log rule number */
8922 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8927 * Catch errors reported by us and routines below us, and return NULL
8930 if (setjmp(cstate
->top_ctx
))
8933 if (cstate
->linktype
!= DLT_PFLOG
) {
8934 bpf_error(cstate
, "rnr supported only on PF linktype");
8938 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8943 /* PF firewall log sub-rule number */
8945 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8950 * Catch errors reported by us and routines below us, and return NULL
8953 if (setjmp(cstate
->top_ctx
))
8956 if (cstate
->linktype
!= DLT_PFLOG
) {
8957 bpf_error(cstate
, "srnr supported only on PF linktype");
8961 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8966 /* PF firewall log reason code */
8968 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8973 * Catch errors reported by us and routines below us, and return NULL
8976 if (setjmp(cstate
->top_ctx
))
8979 if (cstate
->linktype
!= DLT_PFLOG
) {
8980 bpf_error(cstate
, "reason supported only on PF linktype");
8984 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8985 (bpf_u_int32
)reason
);
8989 /* PF firewall log action */
8991 gen_pf_action(compiler_state_t
*cstate
, int action
)
8996 * Catch errors reported by us and routines below us, and return NULL
8999 if (setjmp(cstate
->top_ctx
))
9002 if (cstate
->linktype
!= DLT_PFLOG
) {
9003 bpf_error(cstate
, "action supported only on PF linktype");
9007 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
9008 (bpf_u_int32
)action
);
9012 /* IEEE 802.11 wireless header */
9014 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
9019 * Catch errors reported by us and routines below us, and return NULL
9022 if (setjmp(cstate
->top_ctx
))
9025 switch (cstate
->linktype
) {
9027 case DLT_IEEE802_11
:
9028 case DLT_PRISM_HEADER
:
9029 case DLT_IEEE802_11_RADIO_AVS
:
9030 case DLT_IEEE802_11_RADIO
:
9031 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
9035 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
9043 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
9048 * Catch errors reported by us and routines below us, and return NULL
9051 if (setjmp(cstate
->top_ctx
))
9054 switch (cstate
->linktype
) {
9056 case DLT_IEEE802_11
:
9057 case DLT_PRISM_HEADER
:
9058 case DLT_IEEE802_11_RADIO_AVS
:
9059 case DLT_IEEE802_11_RADIO
:
9063 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
9067 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
9068 IEEE80211_FC1_DIR_MASK
);
9073 // Process an ARCnet host address string.
9075 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
9078 * Catch errors reported by us and routines below us, and return NULL
9081 if (setjmp(cstate
->top_ctx
))
9084 switch (cstate
->linktype
) {
9087 case DLT_ARCNET_LINUX
:
9088 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
9089 q
.proto
== Q_LINK
) {
9092 * The lexer currently defines the address format in a
9093 * way that makes this error condition never true.
9094 * Let's check it anyway in case this part of the lexer
9095 * changes in future.
9097 if (! pcapint_atoan(s
, &addr
))
9098 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
9099 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
9101 bpf_error(cstate
, "ARCnet address used in non-arc expression");
9105 bpf_error(cstate
, "aid supported only on ARCnet");
9110 // Compare an ARCnet host address with the given value.
9111 static struct block
*
9112 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
9114 register struct block
*b0
, *b1
;
9118 * ARCnet is different from Ethernet: the source address comes before
9119 * the destination address, each is one byte long. This holds for all
9120 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
9121 * in the 1983 edition of the "ARCNET Designer's Handbook" published
9122 * by Datapoint (document number 61610-01).
9125 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
9128 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
9131 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9132 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9138 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9139 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9144 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9148 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9152 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9156 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9160 bpf_error(cstate
, "'ra' is only supported on 802.11");
9164 bpf_error(cstate
, "'ta' is only supported on 802.11");
9171 static struct block
*
9172 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9174 struct block
*b0
, *b1
;
9176 /* check for VLAN, including 802.1ad and QinQ */
9177 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9178 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9181 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9187 static struct block
*
9188 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9190 if (vlan_num
> 0x0fff) {
9191 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9194 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9197 static struct block
*
9198 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9201 struct block
*b0
, *b1
;
9203 b0
= gen_vlan_tpid_test(cstate
);
9206 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9212 * Both payload and link header type follow the VLAN tags so that
9213 * both need to be updated.
9215 cstate
->off_linkpl
.constant_part
+= 4;
9216 cstate
->off_linktype
.constant_part
+= 4;
9221 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9222 /* add v to variable part of off */
9224 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9225 bpf_u_int32 v
, struct slist
*s
)
9229 if (!off
->is_variable
)
9230 off
->is_variable
= 1;
9232 off
->reg
= alloc_reg(cstate
);
9234 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9237 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9240 s2
= new_stmt(cstate
, BPF_ST
);
9246 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9247 * and link type offsets first
9250 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9254 /* offset determined at run time, shift variable part */
9256 cstate
->is_vlan_vloffset
= 1;
9257 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9258 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9260 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9261 sappend(s
.next
, b_tpid
->head
->stmts
);
9262 b_tpid
->head
->stmts
= s
.next
;
9266 * patch block b_vid (VLAN id test) to load VID value either from packet
9267 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9270 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9272 struct slist
*s
, *s2
, *sjeq
;
9275 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9276 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9278 /* true -> next instructions, false -> beginning of b_vid */
9279 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9281 sjeq
->s
.jf
= b_vid
->stmts
;
9284 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
9285 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
9289 /* Jump to the test in b_vid. We need to jump one instruction before
9290 * the end of the b_vid block so that we only skip loading the TCI
9291 * from packet data and not the 'and' instruction extracting VID.
9294 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9296 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9300 /* insert our statements at the beginning of b_vid */
9301 sappend(s
, b_vid
->stmts
);
9306 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9307 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9308 * tag can be either in metadata or in packet data; therefore if the
9309 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9310 * header for VLAN tag. As the decision is done at run time, we need
9311 * update variable part of the offsets
9313 static struct block
*
9314 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9317 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9320 /* generate new filter code based on extracting packet
9322 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9323 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9325 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9330 * This is tricky. We need to insert the statements updating variable
9331 * parts of offsets before the traditional TPID and VID tests so
9332 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9333 * we do not want this update to affect those checks. That's why we
9334 * generate both test blocks first and insert the statements updating
9335 * variable parts of both offsets after that. This wouldn't work if
9336 * there already were variable length link header when entering this
9337 * function but gen_vlan_bpf_extensions() isn't called in that case.
9339 b_tpid
= gen_vlan_tpid_test(cstate
);
9341 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9343 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9348 gen_vlan_patch_vid_test(cstate
, b_vid
);
9358 * support IEEE 802.1Q VLAN trunk over ethernet
9361 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9366 * Catch errors reported by us and routines below us, and return NULL
9369 if (setjmp(cstate
->top_ctx
))
9372 /* can't check for VLAN-encapsulated packets inside MPLS */
9373 if (cstate
->label_stack_depth
> 0)
9374 bpf_error(cstate
, "no VLAN match after MPLS");
9377 * Check for a VLAN packet, and then change the offsets to point
9378 * to the type and data fields within the VLAN packet. Just
9379 * increment the offsets, so that we can support a hierarchy, e.g.
9380 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9383 * XXX - this is a bit of a kludge. If we were to split the
9384 * compiler into a parser that parses an expression and
9385 * generates an expression tree, and a code generator that
9386 * takes an expression tree (which could come from our
9387 * parser or from some other parser) and generates BPF code,
9388 * we could perhaps make the offsets parameters of routines
9389 * and, in the handler for an "AND" node, pass to subnodes
9390 * other than the VLAN node the adjusted offsets.
9392 * This would mean that "vlan" would, instead of changing the
9393 * behavior of *all* tests after it, change only the behavior
9394 * of tests ANDed with it. That would change the documented
9395 * semantics of "vlan", which might break some expressions.
9396 * However, it would mean that "(vlan and ip) or ip" would check
9397 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9398 * checking only for VLAN-encapsulated IP, so that could still
9399 * be considered worth doing; it wouldn't break expressions
9400 * that are of the form "vlan and ..." or "vlan N and ...",
9401 * which I suspect are the most common expressions involving
9402 * "vlan". "vlan or ..." doesn't necessarily do what the user
9403 * would really want, now, as all the "or ..." tests would
9404 * be done assuming a VLAN, even though the "or" could be viewed
9405 * as meaning "or, if this isn't a VLAN packet...".
9407 switch (cstate
->linktype
) {
9411 * Newer version of the Linux kernel pass around
9412 * packets in which the VLAN tag has been removed
9413 * from the packet data and put into metadata.
9415 * This requires special treatment.
9417 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9418 /* Verify that this is the outer part of the packet and
9419 * not encapsulated somehow. */
9420 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9421 cstate
->off_linkhdr
.constant_part
==
9422 cstate
->off_outermostlinkhdr
.constant_part
) {
9424 * Do we need special VLAN handling?
9426 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9427 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9430 b0
= gen_vlan_no_bpf_extensions(cstate
,
9431 vlan_num
, has_vlan_tag
);
9434 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9438 case DLT_NETANALYZER
:
9439 case DLT_NETANALYZER_TRANSPARENT
:
9440 case DLT_IEEE802_11
:
9441 case DLT_PRISM_HEADER
:
9442 case DLT_IEEE802_11_RADIO_AVS
:
9443 case DLT_IEEE802_11_RADIO
:
9445 * These are either Ethernet packets with an additional
9446 * metadata header (the NetAnalyzer types), or 802.11
9447 * packets, possibly with an additional metadata header.
9449 * For the first of those, the VLAN tag is in the normal
9450 * place, so the special-case handling above isn't
9453 * For the second of those, we don't do the special-case
9456 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9460 bpf_error(cstate
, "no VLAN support for %s",
9461 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9465 cstate
->vlan_stack_depth
++;
9473 * The label_num_arg dance is to avoid annoying whining by compilers that
9474 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9475 * It's not *used* after setjmp returns.
9477 static struct block
*
9478 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9481 struct block
*b0
, *b1
;
9483 if (cstate
->label_stack_depth
> 0) {
9484 /* just match the bottom-of-stack bit clear */
9485 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9488 * We're not in an MPLS stack yet, so check the link-layer
9489 * type against MPLS.
9491 switch (cstate
->linktype
) {
9493 case DLT_C_HDLC
: /* fall through */
9496 case DLT_NETANALYZER
:
9497 case DLT_NETANALYZER_TRANSPARENT
:
9498 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9502 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9505 /* FIXME add other DLT_s ...
9506 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9507 * leave it for now */
9510 bpf_error(cstate
, "no MPLS support for %s",
9511 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9516 /* If a specific MPLS label is requested, check it */
9517 if (has_label_num
) {
9518 if (label_num
> 0xFFFFF) {
9519 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9520 label_num
, 0xFFFFF);
9522 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9523 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9524 0xfffff000); /* only compare the first 20 bits */
9530 * Change the offsets to point to the type and data fields within
9531 * the MPLS packet. Just increment the offsets, so that we
9532 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9533 * capture packets with an outer label of 100000 and an inner
9536 * Increment the MPLS stack depth as well; this indicates that
9537 * we're checking MPLS-encapsulated headers, to make sure higher
9538 * level code generators don't try to match against IP-related
9539 * protocols such as Q_ARP, Q_RARP etc.
9541 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9543 cstate
->off_nl_nosnap
+= 4;
9544 cstate
->off_nl
+= 4;
9545 cstate
->label_stack_depth
++;
9550 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9553 * Catch errors reported by us and routines below us, and return NULL
9556 if (setjmp(cstate
->top_ctx
))
9559 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9563 * Support PPPOE discovery and session.
9566 gen_pppoed(compiler_state_t
*cstate
)
9569 * Catch errors reported by us and routines below us, and return NULL
9572 if (setjmp(cstate
->top_ctx
))
9575 /* check for PPPoE discovery */
9576 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9580 * RFC 2516 Section 4:
9582 * The Ethernet payload for PPPoE is as follows:
9585 * 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
9586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9587 * | VER | TYPE | CODE | SESSION_ID |
9588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9589 * | LENGTH | payload ~
9590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9593 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9595 struct block
*b0
, *b1
;
9598 * Catch errors reported by us and routines below us, and return NULL
9601 if (setjmp(cstate
->top_ctx
))
9605 * Test against the PPPoE session link-layer type.
9607 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9609 /* If a specific session is requested, check PPPoE session id */
9611 if (sess_num
> UINT16_MAX
) {
9612 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9613 sess_num
, UINT16_MAX
);
9615 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9621 * Change the offsets to point to the type and data fields within
9622 * the PPP packet, and note that this is PPPoE rather than
9625 * XXX - this is a bit of a kludge. See the comments in
9628 * The "network-layer" protocol is PPPoE, which has a 6-byte
9629 * PPPoE header, followed by a PPP packet.
9631 * There is no HDLC encapsulation for the PPP packet (it's
9632 * encapsulated in PPPoES instead), so the link-layer type
9633 * starts at the first byte of the PPP packet. For PPPoE,
9634 * that offset is relative to the beginning of the total
9635 * link-layer payload, including any 802.2 LLC header, so
9636 * it's 6 bytes past cstate->off_nl.
9638 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9639 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9640 cstate
->off_linkpl
.reg
);
9642 cstate
->off_linktype
= cstate
->off_linkhdr
;
9643 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9646 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9651 /* Check that this is Geneve and the VNI is correct if
9652 * specified. Parameterized to handle both IPv4 and IPv6. */
9653 static struct block
*
9654 gen_geneve_check(compiler_state_t
*cstate
,
9655 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9656 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9658 struct block
*b0
, *b1
;
9660 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9662 /* Check that we are operating on version 0. Otherwise, we
9663 * can't decode the rest of the fields. The version is 2 bits
9664 * in the first byte of the Geneve header. */
9665 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9670 if (vni
> 0xffffff) {
9671 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9674 vni
<<= 8; /* VNI is in the upper 3 bytes */
9675 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9683 /* The IPv4 and IPv6 Geneve checks need to do two things:
9684 * - Verify that this actually is Geneve with the right VNI.
9685 * - Place the IP header length (plus variable link prefix if
9686 * needed) into register A to be used later to compute
9687 * the inner packet offsets. */
9688 static struct block
*
9689 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9691 struct block
*b0
, *b1
;
9692 struct slist
*s
, *s1
;
9694 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9696 /* Load the IP header length into A. */
9697 s
= gen_loadx_iphdrlen(cstate
);
9699 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9702 /* Forcibly append these statements to the true condition
9703 * of the protocol check by creating a new block that is
9704 * always true and ANDing them. */
9705 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9714 static struct block
*
9715 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9717 struct block
*b0
, *b1
;
9718 struct slist
*s
, *s1
;
9720 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9722 /* Load the IP header length. We need to account for a
9723 * variable length link prefix if there is one. */
9724 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9726 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9730 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9734 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9738 /* Forcibly append these statements to the true condition
9739 * of the protocol check by creating a new block that is
9740 * always true and ANDing them. */
9741 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9744 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9753 /* We need to store three values based on the Geneve header::
9754 * - The offset of the linktype.
9755 * - The offset of the end of the Geneve header.
9756 * - The offset of the end of the encapsulated MAC header. */
9757 static struct slist
*
9758 gen_geneve_offsets(compiler_state_t
*cstate
)
9760 struct slist
*s
, *s1
, *s_proto
;
9762 /* First we need to calculate the offset of the Geneve header
9763 * itself. This is composed of the IP header previously calculated
9764 * (include any variable link prefix) and stored in A plus the
9765 * fixed sized headers (fixed link prefix, MAC length, and UDP
9767 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9768 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9770 /* Stash this in X since we'll need it later. */
9771 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9774 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9776 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9780 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9781 cstate
->off_linktype
.is_variable
= 1;
9782 cstate
->off_linktype
.constant_part
= 0;
9784 s1
= new_stmt(cstate
, BPF_ST
);
9785 s1
->s
.k
= cstate
->off_linktype
.reg
;
9788 /* Load the Geneve option length and mask and shift to get the
9789 * number of bytes. It is stored in the first byte of the Geneve
9791 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9795 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9799 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9803 /* Add in the rest of the Geneve base header. */
9804 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9808 /* Add the Geneve header length to its offset and store. */
9809 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9813 /* Set the encapsulated type as Ethernet. Even though we may
9814 * not actually have Ethernet inside there are two reasons this
9816 * - The linktype field is always in EtherType format regardless
9817 * of whether it is in Geneve or an inner Ethernet frame.
9818 * - The only link layer that we have specific support for is
9819 * Ethernet. We will confirm that the packet actually is
9820 * Ethernet at runtime before executing these checks. */
9821 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9823 s1
= new_stmt(cstate
, BPF_ST
);
9824 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9827 /* Calculate whether we have an Ethernet header or just raw IP/
9828 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9829 * and linktype by 14 bytes so that the network header can be found
9830 * seamlessly. Otherwise, keep what we've calculated already. */
9832 /* We have a bare jmp so we can't use the optimizer. */
9833 cstate
->no_optimize
= 1;
9835 /* Load the EtherType in the Geneve header, 2 bytes in. */
9836 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9840 /* Load X with the end of the Geneve header. */
9841 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9842 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9845 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9846 * end of this check, we should have the total length in X. In
9847 * the non-Ethernet case, it's already there. */
9848 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9849 s_proto
->s
.k
= ETHERTYPE_TEB
;
9850 sappend(s
, s_proto
);
9852 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9856 /* Since this is Ethernet, use the EtherType of the payload
9857 * directly as the linktype. Overwrite what we already have. */
9858 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9862 s1
= new_stmt(cstate
, BPF_ST
);
9863 s1
->s
.k
= cstate
->off_linktype
.reg
;
9866 /* Advance two bytes further to get the end of the Ethernet
9868 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9872 /* Move the result to X. */
9873 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9876 /* Store the final result of our linkpl calculation. */
9877 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9878 cstate
->off_linkpl
.is_variable
= 1;
9879 cstate
->off_linkpl
.constant_part
= 0;
9881 s1
= new_stmt(cstate
, BPF_STX
);
9882 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9891 /* Check to see if this is a Geneve packet. */
9893 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9895 struct block
*b0
, *b1
;
9899 * Catch errors reported by us and routines below us, and return NULL
9902 if (setjmp(cstate
->top_ctx
))
9905 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9906 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9911 /* Later filters should act on the payload of the Geneve frame,
9912 * update all of the header pointers. Attach this code so that
9913 * it gets executed in the event that the Geneve filter matches. */
9914 s
= gen_geneve_offsets(cstate
);
9916 b1
= gen_true(cstate
);
9917 sappend(s
, b1
->stmts
);
9922 cstate
->is_encap
= 1;
9927 /* Check that this is VXLAN and the VNI is correct if
9928 * specified. Parameterized to handle both IPv4 and IPv6. */
9929 static struct block
*
9930 gen_vxlan_check(compiler_state_t
*cstate
,
9931 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9932 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9934 struct block
*b0
, *b1
;
9936 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9938 /* Check that the VXLAN header has the flag bits set
9940 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9945 if (vni
> 0xffffff) {
9946 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9949 vni
<<= 8; /* VNI is in the upper 3 bytes */
9950 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9958 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9959 * - Verify that this actually is VXLAN with the right VNI.
9960 * - Place the IP header length (plus variable link prefix if
9961 * needed) into register A to be used later to compute
9962 * the inner packet offsets. */
9963 static struct block
*
9964 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9966 struct block
*b0
, *b1
;
9967 struct slist
*s
, *s1
;
9969 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9971 /* Load the IP header length into A. */
9972 s
= gen_loadx_iphdrlen(cstate
);
9974 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9977 /* Forcibly append these statements to the true condition
9978 * of the protocol check by creating a new block that is
9979 * always true and ANDing them. */
9980 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9989 static struct block
*
9990 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9992 struct block
*b0
, *b1
;
9993 struct slist
*s
, *s1
;
9995 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9997 /* Load the IP header length. We need to account for a
9998 * variable length link prefix if there is one. */
9999 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
10001 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
10005 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
10009 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
10013 /* Forcibly append these statements to the true condition
10014 * of the protocol check by creating a new block that is
10015 * always true and ANDing them. */
10016 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10019 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10028 /* We need to store three values based on the VXLAN header:
10029 * - The offset of the linktype.
10030 * - The offset of the end of the VXLAN header.
10031 * - The offset of the end of the encapsulated MAC header. */
10032 static struct slist
*
10033 gen_vxlan_offsets(compiler_state_t
*cstate
)
10035 struct slist
*s
, *s1
;
10037 /* Calculate the offset of the VXLAN header itself. This
10038 * includes the IP header computed previously (including any
10039 * variable link prefix) and stored in A plus the fixed size
10040 * headers (fixed link prefix, MAC length, UDP header). */
10041 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10042 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
10044 /* Add the VXLAN header length to its offset and store */
10045 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10049 /* Push the link header. VXLAN packets always contain Ethernet
10051 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
10053 s1
= new_stmt(cstate
, BPF_ST
);
10054 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
10057 /* As the payload is an Ethernet packet, we can use the
10058 * EtherType of the payload directly as the linktype. */
10059 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10063 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
10064 cstate
->off_linktype
.is_variable
= 1;
10065 cstate
->off_linktype
.constant_part
= 0;
10067 s1
= new_stmt(cstate
, BPF_ST
);
10068 s1
->s
.k
= cstate
->off_linktype
.reg
;
10071 /* Two bytes further is the end of the Ethernet header and the
10072 * start of the payload. */
10073 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10077 /* Move the result to X. */
10078 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10081 /* Store the final result of our linkpl calculation. */
10082 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
10083 cstate
->off_linkpl
.is_variable
= 1;
10084 cstate
->off_linkpl
.constant_part
= 0;
10086 s1
= new_stmt(cstate
, BPF_STX
);
10087 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10090 cstate
->off_nl
= 0;
10095 /* Check to see if this is a VXLAN packet. */
10097 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
10099 struct block
*b0
, *b1
;
10103 * Catch errors reported by us and routines below us, and return NULL
10106 if (setjmp(cstate
->top_ctx
))
10109 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
10110 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
10115 /* Later filters should act on the payload of the VXLAN frame,
10116 * update all of the header pointers. Attach this code so that
10117 * it gets executed in the event that the VXLAN filter matches. */
10118 s
= gen_vxlan_offsets(cstate
);
10120 b1
= gen_true(cstate
);
10121 sappend(s
, b1
->stmts
);
10126 cstate
->is_encap
= 1;
10131 /* Check that the encapsulated frame has a link layer header
10132 * for Ethernet filters. */
10133 static struct block
*
10134 gen_encap_ll_check(compiler_state_t
*cstate
)
10137 struct slist
*s
, *s1
;
10139 /* The easiest way to see if there is a link layer present
10140 * is to check if the link layer header and payload are not
10143 /* Geneve always generates pure variable offsets so we can
10144 * compare only the registers. */
10145 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
10146 s
->s
.k
= cstate
->off_linkhdr
.reg
;
10148 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
10149 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10152 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10160 static struct block
*
10161 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
10162 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10166 switch (atmfield
) {
10169 if (!cstate
->is_atm
)
10170 bpf_error(cstate
, "'vpi' supported only on raw ATM");
10171 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10173 if (jvalue
> UINT8_MAX
)
10174 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
10175 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10176 0xffffffffU
, jtype
, reverse
, jvalue
);
10180 if (!cstate
->is_atm
)
10181 bpf_error(cstate
, "'vci' supported only on raw ATM");
10182 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10184 if (jvalue
> UINT16_MAX
)
10185 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
10186 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10187 0xffffffffU
, jtype
, reverse
, jvalue
);
10191 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10192 abort(); /* XXX - this isn't on FreeBSD */
10193 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10194 0x0fU
, jtype
, reverse
, jvalue
);
10198 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10200 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10201 0xffffffffU
, jtype
, reverse
, jvalue
);
10204 case A_CALLREFTYPE
:
10205 if (!cstate
->is_atm
)
10206 bpf_error(cstate
, "'callref' supported only on raw ATM");
10207 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10209 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10210 0xffffffffU
, jtype
, reverse
, jvalue
);
10219 static struct block
*
10220 gen_atmtype_metac(compiler_state_t
*cstate
)
10222 struct block
*b0
, *b1
;
10224 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10225 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10230 static struct block
*
10231 gen_atmtype_sc(compiler_state_t
*cstate
)
10233 struct block
*b0
, *b1
;
10235 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10236 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10241 static struct block
*
10242 gen_atmtype_llc(compiler_state_t
*cstate
)
10246 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10247 cstate
->linktype
= cstate
->prevlinktype
;
10252 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10253 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10256 * Catch errors reported by us and routines below us, and return NULL
10259 if (setjmp(cstate
->top_ctx
))
10262 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10267 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10269 struct block
*b0
, *b1
;
10272 * Catch errors reported by us and routines below us, and return NULL
10275 if (setjmp(cstate
->top_ctx
))
10281 /* Get all packets in Meta signalling Circuit */
10282 if (!cstate
->is_atm
)
10283 bpf_error(cstate
, "'metac' supported only on raw ATM");
10284 b1
= gen_atmtype_metac(cstate
);
10288 /* Get all packets in Broadcast Circuit*/
10289 if (!cstate
->is_atm
)
10290 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10291 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10292 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10297 /* Get all cells in Segment OAM F4 circuit*/
10298 if (!cstate
->is_atm
)
10299 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10300 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10301 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10306 /* Get all cells in End-to-End OAM F4 Circuit*/
10307 if (!cstate
->is_atm
)
10308 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10309 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10310 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10315 /* Get all packets in connection Signalling Circuit */
10316 if (!cstate
->is_atm
)
10317 bpf_error(cstate
, "'sc' supported only on raw ATM");
10318 b1
= gen_atmtype_sc(cstate
);
10322 /* Get all packets in ILMI Circuit */
10323 if (!cstate
->is_atm
)
10324 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10325 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10326 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10331 /* Get all LANE packets */
10332 if (!cstate
->is_atm
)
10333 bpf_error(cstate
, "'lane' supported only on raw ATM");
10334 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10337 * Arrange that all subsequent tests assume LANE
10338 * rather than LLC-encapsulated packets, and set
10339 * the offsets appropriately for LANE-encapsulated
10342 * We assume LANE means Ethernet, not Token Ring.
10344 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10345 cstate
->off_payload
+ 2, /* Ethernet header */
10347 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10348 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10349 cstate
->off_nl
= 0; /* Ethernet II */
10350 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10354 /* Get all LLC-encapsulated packets */
10355 if (!cstate
->is_atm
)
10356 bpf_error(cstate
, "'llc' supported only on raw ATM");
10357 b1
= gen_atmtype_llc(cstate
);
10367 * Filtering for MTP2 messages based on li value
10368 * FISU, length is null
10369 * LSSU, length is 1 or 2
10370 * MSU, length is 3 or more
10371 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10374 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10376 struct block
*b0
, *b1
;
10379 * Catch errors reported by us and routines below us, and return NULL
10382 if (setjmp(cstate
->top_ctx
))
10388 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10389 (cstate
->linktype
!= DLT_ERF
) &&
10390 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10391 bpf_error(cstate
, "'fisu' supported only on MTP2");
10392 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10393 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10394 0x3fU
, BPF_JEQ
, 0, 0U);
10398 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10399 (cstate
->linktype
!= DLT_ERF
) &&
10400 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10401 bpf_error(cstate
, "'lssu' supported only on MTP2");
10402 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10403 0x3fU
, BPF_JGT
, 1, 2U);
10404 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10405 0x3fU
, BPF_JGT
, 0, 0U);
10410 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10411 (cstate
->linktype
!= DLT_ERF
) &&
10412 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10413 bpf_error(cstate
, "'msu' supported only on MTP2");
10414 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10415 0x3fU
, BPF_JGT
, 0, 2U);
10419 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10420 (cstate
->linktype
!= DLT_ERF
) &&
10421 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10422 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10423 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10424 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10425 0xff80U
, BPF_JEQ
, 0, 0U);
10429 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10430 (cstate
->linktype
!= DLT_ERF
) &&
10431 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10432 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10433 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10434 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10435 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10436 0xff80U
, BPF_JGT
, 0, 0U);
10441 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10442 (cstate
->linktype
!= DLT_ERF
) &&
10443 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10444 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10445 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10446 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10456 * These maximum valid values are all-ones, so they double as the bitmasks
10457 * before any bitwise shifting.
10459 #define MTP2_SIO_MAXVAL UINT8_MAX
10460 #define MTP3_PC_MAXVAL 0x3fffU
10461 #define MTP3_SLS_MAXVAL 0xfU
10463 static struct block
*
10464 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10465 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10473 newoff_sio
= cstate
->off_sio
;
10474 newoff_opc
= cstate
->off_opc
;
10475 newoff_dpc
= cstate
->off_dpc
;
10476 newoff_sls
= cstate
->off_sls
;
10477 switch (mtp3field
) {
10480 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10482 * SIO is the simplest field: the size is one byte and the offset is a
10483 * multiple of bytes, so the only detail to get right is the value of
10484 * the [right-to-left] field offset.
10487 newoff_sio
+= 3; /* offset for MTP2_HSL */
10491 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10492 bpf_error(cstate
, "'sio' supported only on SS7");
10493 if(jvalue
> MTP2_SIO_MAXVAL
)
10494 bpf_error(cstate
, "sio value %u too big; max value = %u",
10495 jvalue
, MTP2_SIO_MAXVAL
);
10496 // Here the bitmask means "do not apply a bitmask".
10497 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10498 jtype
, reverse
, jvalue
);
10502 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10504 * SLS, OPC and DPC are more complicated: none of these is sized in a
10505 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10506 * diagrams are meant to be read right-to-left. This means in the
10507 * diagrams within individual fields and concatenations thereof
10508 * bitwise shifts and masks can be noted in the common left-to-right
10509 * manner until each final value is ready to be byte-swapped and
10510 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10511 * similar problem in a similar way.
10513 * Offsets of fields within the packet header always have the
10514 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10515 * DLTs the offset does not include the F (Flag) field at the
10516 * beginning of each message.
10518 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10519 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10520 * be tested entirely using a single BPF_W comparison. In this case
10521 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10522 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10523 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10524 * correlates with the [RTL] packet diagram until the byte-swapping is
10527 * The code below uses this approach for OPC, which spans 3 bytes.
10528 * DPC and SLS use shorter loads, SLS also uses a different offset.
10535 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10536 bpf_error(cstate
, "'opc' supported only on SS7");
10537 if (jvalue
> MTP3_PC_MAXVAL
)
10538 bpf_error(cstate
, "opc value %u too big; max value = %u",
10539 jvalue
, MTP3_PC_MAXVAL
);
10540 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10541 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10542 SWAPLONG(jvalue
<< 14));
10550 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10551 bpf_error(cstate
, "'dpc' supported only on SS7");
10552 if (jvalue
> MTP3_PC_MAXVAL
)
10553 bpf_error(cstate
, "dpc value %u too big; max value = %u",
10554 jvalue
, MTP3_PC_MAXVAL
);
10555 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10556 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10557 SWAPSHORT(jvalue
));
10565 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10566 bpf_error(cstate
, "'sls' supported only on SS7");
10567 if (jvalue
> MTP3_SLS_MAXVAL
)
10568 bpf_error(cstate
, "sls value %u too big; max value = %u",
10569 jvalue
, MTP3_SLS_MAXVAL
);
10570 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10571 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10582 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10583 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10586 * Catch errors reported by us and routines below us, and return NULL
10589 if (setjmp(cstate
->top_ctx
))
10592 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10596 static struct block
*
10597 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10602 * Q.2931 signalling protocol messages for handling virtual circuits
10603 * establishment and teardown
10608 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10611 case A_CALLPROCEED
:
10612 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10616 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10620 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10624 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10627 case A_RELEASE_DONE
:
10628 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10638 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10640 struct block
*b0
, *b1
;
10643 * Catch errors reported by us and routines below us, and return NULL
10646 if (setjmp(cstate
->top_ctx
))
10652 if (!cstate
->is_atm
)
10653 bpf_error(cstate
, "'oam' supported only on raw ATM");
10655 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10656 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10658 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10663 if (!cstate
->is_atm
)
10664 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10666 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10667 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10669 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10675 * Get Q.2931 signalling messages for switched
10676 * virtual connection
10678 if (!cstate
->is_atm
)
10679 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10680 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10681 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10683 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10685 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10687 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10689 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10691 b0
= gen_atmtype_sc(cstate
);
10695 case A_METACONNECT
:
10696 if (!cstate
->is_atm
)
10697 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10698 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10699 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10701 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10703 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10705 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10707 b0
= gen_atmtype_metac(cstate
);