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>
64 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
71 uint16_t u6_addr16
[8];
72 uint32_t u6_addr32
[4];
74 #define s6_addr in6_u.u6_addr8
75 #define s6_addr16 in6_u.u6_addr16
76 #define s6_addr32 in6_u.u6_addr32
77 #define s6_addr64 in6_u.u6_addr64
80 typedef unsigned short sa_family_t
;
82 #define __SOCKADDR_COMMON(sa_prefix) \
83 sa_family_t sa_prefix##family
85 /* Ditto, for IPv6. */
88 __SOCKADDR_COMMON (sin6_
);
89 uint16_t sin6_port
; /* Transport layer port # */
90 uint32_t sin6_flowinfo
; /* IPv6 flow information */
91 struct in6_addr sin6_addr
; /* IPv6 address */
94 #ifndef EAI_ADDRFAMILY
96 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
97 int ai_family
; /* PF_xxx */
98 int ai_socktype
; /* SOCK_xxx */
99 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
100 size_t ai_addrlen
; /* length of ai_addr */
101 char *ai_canonname
; /* canonical name for hostname */
102 struct sockaddr
*ai_addr
; /* binary address */
103 struct addrinfo
*ai_next
; /* next structure in linked list */
105 #endif /* EAI_ADDRFAMILY */
106 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
109 #include <netdb.h> /* for "struct addrinfo" */
111 #include <pcap/namedb.h>
113 #include "nametoaddr.h"
115 #define ETHERMTU 1500
117 #ifndef IPPROTO_HOPOPTS
118 #define IPPROTO_HOPOPTS 0
120 #ifndef IPPROTO_ROUTING
121 #define IPPROTO_ROUTING 43
123 #ifndef IPPROTO_FRAGMENT
124 #define IPPROTO_FRAGMENT 44
126 #ifndef IPPROTO_DSTOPTS
127 #define IPPROTO_DSTOPTS 60
130 #define IPPROTO_SCTP 132
133 #define GENEVE_PORT 6081
134 #define VXLAN_PORT 4789
138 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
142 #define ARCTYPE_IP_OLD 240 /* IP protocol */
143 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
146 #define ARCTYPE_IP 212 /* IP protocol */
147 #define ARCTYPE_ARP 213 /* address resolution protocol */
148 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
150 #define ARCTYPE_ATALK 221 /* Appletalk */
151 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
152 #define ARCTYPE_IPX 250 /* Novell IPX */
154 #define ARCTYPE_INET6 0xc4 /* IPng */
155 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
158 /* Based on UNI3.1 standard by ATM Forum */
160 /* ATM traffic types based on VPI=0 and (the following VCI */
161 #define VCI_PPC 0x05 /* Point-to-point signal msg */
162 #define VCI_BCC 0x02 /* Broadcast signal msg */
163 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
164 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
165 #define VCI_METAC 0x01 /* Meta signal msg */
166 #define VCI_ILMIC 0x10 /* ILMI msg */
168 /* Q.2931 signalling messages */
169 #define CALL_PROCEED 0x02 /* call proceeding */
170 #define CONNECT 0x07 /* connect */
171 #define CONNECT_ACK 0x0f /* connect_ack */
172 #define SETUP 0x05 /* setup */
173 #define RELEASE 0x4d /* release */
174 #define RELEASE_DONE 0x5a /* release_done */
175 #define RESTART 0x46 /* restart */
176 #define RESTART_ACK 0x4e /* restart ack */
177 #define STATUS 0x7d /* status */
178 #define STATUS_ENQ 0x75 /* status ack */
179 #define ADD_PARTY 0x80 /* add party */
180 #define ADD_PARTY_ACK 0x81 /* add party ack */
181 #define ADD_PARTY_REJ 0x82 /* add party rej */
182 #define DROP_PARTY 0x83 /* drop party */
183 #define DROP_PARTY_ACK 0x84 /* drop party ack */
185 /* Information Element Parameters in the signalling messages */
186 #define CAUSE 0x08 /* cause */
187 #define ENDPT_REF 0x54 /* endpoint reference */
188 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
189 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
190 #define CONNECT_ID 0x5a /* connection identifier */
191 #define QOS_PARA 0x5c /* quality of service parameters */
192 #define B_HIGHER 0x5d /* broadband higher layer information */
193 #define B_BEARER 0x5e /* broadband bearer capability */
194 #define B_LOWER 0x5f /* broadband lower information */
195 #define CALLING_PARTY 0x6c /* calling party number */
196 #define CALLED_PARTY 0x70 /* called party number */
200 /* Q.2931 signalling general messages format */
201 #define PROTO_POS 0 /* offset of protocol discriminator */
202 #define CALL_REF_POS 2 /* offset of call reference value */
203 #define MSG_TYPE_POS 5 /* offset of message type */
204 #define MSG_LEN_POS 7 /* offset of message length */
205 #define IE_BEGIN_POS 9 /* offset of first information element */
207 /* format of signalling messages */
210 #define FIELD_BEGIN_POS 4
213 /* SunATM header for ATM packet */
214 #define SUNATM_DIR_POS 0
215 #define SUNATM_VPI_POS 1
216 #define SUNATM_VCI_POS 2
217 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
219 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
220 #define PT_LANE 0x01 /* LANE */
221 #define PT_LLC 0x02 /* LLC encapsulation */
222 #define PT_ILMI 0x05 /* ILMI */
223 #define PT_QSAAL 0x06 /* Q.SAAL */
226 /* Types missing from some systems */
229 * Network layer protocol identifiers
232 #define ISO8473_CLNP 0x81
235 #define ISO9542_ESIS 0x82
237 #ifndef ISO9542X25_ESIS
238 #define ISO9542X25_ESIS 0x8a
240 #ifndef ISO10589_ISIS
241 #define ISO10589_ISIS 0x83
244 #define ISIS_L1_LAN_IIH 15
245 #define ISIS_L2_LAN_IIH 16
246 #define ISIS_PTP_IIH 17
247 #define ISIS_L1_LSP 18
248 #define ISIS_L2_LSP 20
249 #define ISIS_L1_CSNP 24
250 #define ISIS_L2_CSNP 25
251 #define ISIS_L1_PSNP 26
252 #define ISIS_L2_PSNP 27
254 #ifndef ISO8878A_CONS
255 #define ISO8878A_CONS 0x84
257 #ifndef ISO10747_IDRP
258 #define ISO10747_IDRP 0x85
261 // Same as in tcpdump/print-sl.c.
263 #define SLIPDIR_OUT 1
265 #ifdef HAVE_OS_PROTO_H
266 #include "os-proto.h"
269 #define JMP(c) ((c)|BPF_JMP|BPF_K)
272 * "Push" the current value of the link-layer header type and link-layer
273 * header offset onto a "stack", and set a new value. (It's not a
274 * full-blown stack; we keep only the top two items.)
276 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
278 (cs)->prevlinktype = (cs)->linktype; \
279 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
280 (cs)->linktype = (new_linktype); \
281 (cs)->off_linkhdr.is_variable = (new_is_variable); \
282 (cs)->off_linkhdr.constant_part = (new_constant_part); \
283 (cs)->off_linkhdr.reg = (new_reg); \
284 (cs)->is_encap = 0; \
288 * Offset "not set" value.
290 #define OFFSET_NOT_SET 0xffffffffU
293 * Absolute offsets, which are offsets from the beginning of the raw
294 * packet data, are, in the general case, the sum of a variable value
295 * and a constant value; the variable value may be absent, in which
296 * case the offset is only the constant value, and the constant value
297 * may be zero, in which case the offset is only the variable value.
299 * bpf_abs_offset is a structure containing all that information:
301 * is_variable is 1 if there's a variable part.
303 * constant_part is the constant part of the value, possibly zero;
305 * if is_variable is 1, reg is the register number for a register
306 * containing the variable value if the register has been assigned,
316 * Value passed to gen_load_a() to indicate what the offset argument
317 * is relative to the beginning of.
320 OR_PACKET
, /* full packet data */
321 OR_LINKHDR
, /* link-layer header */
322 OR_PREVLINKHDR
, /* previous link-layer header */
323 OR_LLC
, /* 802.2 LLC header */
324 OR_PREVMPLSHDR
, /* previous MPLS header */
325 OR_LINKTYPE
, /* link-layer type */
326 OR_LINKPL
, /* link-layer payload */
327 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
328 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
329 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
333 * We divvy out chunks of memory rather than call malloc each time so
334 * we don't have to worry about leaking memory. It's probably
335 * not a big deal if all this memory was wasted but if this ever
336 * goes into a library that would probably not be a good idea.
338 * XXX - this *is* in a library....
341 #define CHUNK0SIZE 1024
348 * A chunk can store any of:
349 * - a string (guaranteed alignment 1 but present for completeness)
353 * For this simple allocator every allocated chunk gets rounded up to the
354 * alignment needed for any chunk.
365 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
367 /* Code generator state */
369 struct _compiler_state
{
380 int outermostlinktype
;
385 /* Hack for handling VLAN and MPLS stacks. */
386 u_int label_stack_depth
;
387 u_int vlan_stack_depth
;
393 * As errors are handled by a longjmp, anything allocated must
394 * be freed in the longjmp handler, so it must be reachable
397 * One thing that's allocated is the result of pcap_nametoaddrinfo();
398 * it must be freed with freeaddrinfo(). This variable points to
399 * any addrinfo structure that would need to be freed.
404 * Another thing that's allocated is the result of pcap_ether_aton();
405 * it must be freed with free(). This variable points to any
406 * address that would need to be freed.
411 * Various code constructs need to know the layout of the packet.
412 * These values give the necessary offsets from the beginning
413 * of the packet data.
417 * Absolute offset of the beginning of the link-layer header.
419 bpf_abs_offset off_linkhdr
;
422 * If we're checking a link-layer header for a packet encapsulated
423 * in another protocol layer, this is the equivalent information
424 * for the previous layers' link-layer header from the beginning
425 * of the raw packet data.
427 bpf_abs_offset off_prevlinkhdr
;
430 * This is the equivalent information for the outermost layers'
433 bpf_abs_offset off_outermostlinkhdr
;
436 * Absolute offset of the beginning of the link-layer payload.
438 bpf_abs_offset off_linkpl
;
441 * "off_linktype" is the offset to information in the link-layer
442 * header giving the packet type. This is an absolute offset
443 * from the beginning of the packet.
445 * For Ethernet, it's the offset of the Ethernet type field; this
446 * means that it must have a value that skips VLAN tags.
448 * For link-layer types that always use 802.2 headers, it's the
449 * offset of the LLC header; this means that it must have a value
450 * that skips VLAN tags.
452 * For PPP, it's the offset of the PPP type field.
454 * For Cisco HDLC, it's the offset of the CHDLC type field.
456 * For BSD loopback, it's the offset of the AF_ value.
458 * For Linux cooked sockets, it's the offset of the type field.
460 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
461 * encapsulation, in which case, IP is assumed.
463 bpf_abs_offset off_linktype
;
466 * TRUE if the link layer includes an ATM pseudo-header.
470 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
471 * causes us to generate code that checks for a Geneve or
472 * VXLAN header respectively and assume that later filters
473 * apply to the encapsulated payload.
478 * TRUE if we need variable length part of VLAN offset
480 int is_vlan_vloffset
;
483 * These are offsets for the ATM pseudo-header.
490 * These are offsets for the MTP2 fields.
496 * These are offsets for the MTP3 fields.
504 * This is the offset of the first byte after the ATM pseudo_header,
505 * or -1 if there is no ATM pseudo-header.
510 * These are offsets to the beginning of the network-layer header.
511 * They are relative to the beginning of the link-layer payload
512 * (i.e., they don't include off_linkhdr.constant_part or
513 * off_linkpl.constant_part).
515 * If the link layer never uses 802.2 LLC:
517 * "off_nl" and "off_nl_nosnap" are the same.
519 * If the link layer always uses 802.2 LLC:
521 * "off_nl" is the offset if there's a SNAP header following
524 * "off_nl_nosnap" is the offset if there's no SNAP header.
526 * If the link layer is Ethernet:
528 * "off_nl" is the offset if the packet is an Ethernet II packet
529 * (we assume no 802.3+802.2+SNAP);
531 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
532 * with an 802.2 header following it.
538 * Here we handle simple allocation of the scratch registers.
539 * If too many registers are alloc'd, the allocator punts.
541 int regused
[BPF_MEMWORDS
];
547 struct chunk chunks
[NCHUNKS
];
552 * For use by routines outside this file.
556 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
561 * If we've already set an error, don't override it.
562 * The lexical analyzer reports some errors by setting
563 * the error and then returning a LEX_ERROR token, which
564 * is not recognized by any grammar rule, and thus forces
565 * the parse to stop. We don't want the error reported
566 * by the lexical analyzer to be overwritten by the syntax
569 if (!cstate
->error_set
) {
571 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
574 cstate
->error_set
= 1;
579 * For use *ONLY* in routines in this file.
581 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
582 PCAP_PRINTFLIKE(2, 3);
585 static void PCAP_NORETURN
586 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
591 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
594 longjmp(cstate
->top_ctx
, 1);
601 static int init_linktype(compiler_state_t
*, pcap_t
*);
603 static void init_regs(compiler_state_t
*);
604 static int alloc_reg(compiler_state_t
*);
605 static void free_reg(compiler_state_t
*, int);
607 static void initchunks(compiler_state_t
*cstate
);
608 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
609 static void *newchunk(compiler_state_t
*cstate
, size_t);
610 static void freechunks(compiler_state_t
*cstate
);
611 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
612 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
613 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
614 static inline void syntax(compiler_state_t
*cstate
);
616 static void backpatch(struct block
*, struct block
*);
617 static void merge(struct block
*, struct block
*);
618 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
620 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
622 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
624 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
626 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
628 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
629 u_int
, bpf_u_int32
, bpf_u_int32
);
630 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
631 u_int
, const u_char
*);
632 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
633 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
634 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
636 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
638 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
639 static struct block
*gen_uncond(compiler_state_t
*, int);
640 static inline struct block
*gen_true(compiler_state_t
*);
641 static inline struct block
*gen_false(compiler_state_t
*);
642 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
643 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
644 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
645 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
646 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
647 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
648 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
649 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
650 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
651 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
653 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
654 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
655 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
656 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
657 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
658 int, bpf_u_int32
, u_int
, u_int
);
660 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
661 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
663 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
664 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
665 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
666 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
667 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
668 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
669 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
670 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
671 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
674 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
675 struct in6_addr
*, int, int, int);
678 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
679 struct addrinfo
*, int, int);
681 static struct block
*gen_ipfrag(compiler_state_t
*);
682 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
683 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
685 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
686 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
688 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
689 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
690 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
692 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
693 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
694 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
695 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
697 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
698 static int lookup_proto(compiler_state_t
*, const char *, int);
699 #if !defined(NO_PROTOCHAIN)
700 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
701 #endif /* !defined(NO_PROTOCHAIN) */
702 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
703 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
704 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
705 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
706 static struct block
*gen_len(compiler_state_t
*, int, int);
707 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
708 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
710 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
711 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
712 bpf_u_int32
, int, int);
713 static struct block
*gen_atmtype_llc(compiler_state_t
*);
714 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
717 initchunks(compiler_state_t
*cstate
)
721 for (i
= 0; i
< NCHUNKS
; i
++) {
722 cstate
->chunks
[i
].n_left
= 0;
723 cstate
->chunks
[i
].m
= NULL
;
725 cstate
->cur_chunk
= 0;
729 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
735 /* Round up to chunk alignment. */
736 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
738 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
739 if (n
> cp
->n_left
) {
741 k
= ++cstate
->cur_chunk
;
743 bpf_set_error(cstate
, "out of memory");
746 size
= CHUNK0SIZE
<< k
;
747 cp
->m
= (void *)malloc(size
);
749 bpf_set_error(cstate
, "out of memory");
752 memset((char *)cp
->m
, 0, size
);
755 bpf_set_error(cstate
, "out of memory");
760 return (void *)((char *)cp
->m
+ cp
->n_left
);
764 newchunk(compiler_state_t
*cstate
, size_t n
)
768 p
= newchunk_nolongjmp(cstate
, n
);
770 longjmp(cstate
->top_ctx
, 1);
777 freechunks(compiler_state_t
*cstate
)
781 for (i
= 0; i
< NCHUNKS
; ++i
)
782 if (cstate
->chunks
[i
].m
!= NULL
)
783 free(cstate
->chunks
[i
].m
);
787 * A strdup whose allocations are freed after code generation is over.
788 * This is used by the lexical analyzer, so it can't longjmp; it just
789 * returns NULL on an allocation error, and the callers must check
793 sdup(compiler_state_t
*cstate
, const char *s
)
795 size_t n
= strlen(s
) + 1;
796 char *cp
= newchunk_nolongjmp(cstate
, n
);
800 pcapint_strlcpy(cp
, s
, n
);
804 static inline struct block
*
805 new_block(compiler_state_t
*cstate
, int code
)
809 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
816 static inline struct slist
*
817 new_stmt(compiler_state_t
*cstate
, int code
)
821 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
827 static struct block
*
828 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
830 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
836 static struct block
*
837 gen_retblk(compiler_state_t
*cstate
, int v
)
839 if (setjmp(cstate
->top_ctx
)) {
841 * gen_retblk() only fails because a memory
842 * allocation failed in newchunk(), meaning
843 * that it can't return a pointer.
849 return gen_retblk_internal(cstate
, v
);
852 static inline PCAP_NORETURN_DEF
void
853 syntax(compiler_state_t
*cstate
)
855 bpf_error(cstate
, "syntax error in filter expression");
859 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
860 const char *buf
, int optimize
, bpf_u_int32 mask
)
866 compiler_state_t cstate
;
867 yyscan_t scanner
= NULL
;
868 YY_BUFFER_STATE in_buffer
= NULL
;
873 * If this pcap_t hasn't been activated, it doesn't have a
874 * link-layer type, so we can't use it.
877 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
878 "not-yet-activated pcap_t passed to pcap_compile");
884 * Initialize Winsock, asking for the latest version (2.2),
885 * as we may be calling Winsock routines to translate
886 * host names to addresses.
888 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
890 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
891 err
, "Error calling WSAStartup()");
898 * If the device on which we're capturing need to be notified
899 * that a new filter is being compiled, do so.
901 * This allows them to save a copy of it, in case, for example,
902 * they're implementing a form of remote packet capture, and
903 * want the remote machine to filter out the packets in which
904 * it's sending the packets it's captured.
906 * XXX - the fact that we happen to be compiling a filter
907 * doesn't necessarily mean we'll be installing it as the
908 * filter for this pcap_t; we might be running it from userland
909 * on captured packets to do packet classification. We really
910 * need a better way of handling this, but this is all that
911 * the WinPcap remote capture code did.
913 if (p
->save_current_filter_op
!= NULL
)
914 (p
->save_current_filter_op
)(p
, buf
);
918 cstate
.no_optimize
= 0;
923 cstate
.ic
.root
= NULL
;
924 cstate
.ic
.cur_mark
= 0;
926 cstate
.error_set
= 0;
929 cstate
.netmask
= mask
;
931 cstate
.snaplen
= pcap_snapshot(p
);
932 if (cstate
.snaplen
== 0) {
933 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
934 "snaplen of 0 rejects all packets");
939 if (pcap_lex_init(&scanner
) != 0) {
940 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
941 errno
, "can't initialize scanner");
945 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
948 * Associate the compiler state with the lexical analyzer
951 pcap_set_extra(&cstate
, scanner
);
953 if (init_linktype(&cstate
, p
) == -1) {
957 if (pcap_parse(scanner
, &cstate
) != 0) {
959 if (cstate
.ai
!= NULL
)
960 freeaddrinfo(cstate
.ai
);
962 if (cstate
.e
!= NULL
)
968 if (cstate
.ic
.root
== NULL
) {
969 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
972 * Catch errors reported by gen_retblk().
974 if (cstate
.ic
.root
== NULL
) {
980 if (optimize
&& !cstate
.no_optimize
) {
981 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
986 if (cstate
.ic
.root
== NULL
||
987 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
988 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
989 "expression rejects all packets");
994 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
995 cstate
.ic
.root
, &len
, p
->errbuf
);
996 if (program
->bf_insns
== NULL
) {
1001 program
->bf_len
= len
;
1003 rc
= 0; /* We're all okay */
1007 * Clean up everything for the lexical analyzer.
1009 if (in_buffer
!= NULL
)
1010 pcap__delete_buffer(in_buffer
, scanner
);
1011 if (scanner
!= NULL
)
1012 pcap_lex_destroy(scanner
);
1015 * Clean up our own allocated memory.
1017 freechunks(&cstate
);
1027 * entry point for using the compiler with no pcap open
1028 * pass in all the stuff that is needed explicitly instead.
1031 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1032 struct bpf_program
*program
,
1033 const char *buf
, int optimize
, bpf_u_int32 mask
)
1038 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1040 return (PCAP_ERROR
);
1041 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1047 * Clean up a "struct bpf_program" by freeing all the memory allocated
1051 pcap_freecode(struct bpf_program
*program
)
1053 program
->bf_len
= 0;
1054 if (program
->bf_insns
!= NULL
) {
1055 free((char *)program
->bf_insns
);
1056 program
->bf_insns
= NULL
;
1061 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1062 * which of the jt and jf fields has been resolved and which is a pointer
1063 * back to another unresolved block (or nil). At least one of the fields
1064 * in each block is already resolved.
1067 backpatch(struct block
*list
, struct block
*target
)
1084 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1085 * which of jt and jf is the link.
1088 merge(struct block
*b0
, struct block
*b1
)
1090 register struct block
**p
= &b0
;
1092 /* Find end of list. */
1094 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1096 /* Concatenate the lists. */
1101 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1103 struct block
*ppi_dlt_check
;
1106 * Catch errors reported by us and routines below us, and return -1
1109 if (setjmp(cstate
->top_ctx
))
1113 * Insert before the statements of the first (root) block any
1114 * statements needed to load the lengths of any variable-length
1115 * headers into registers.
1117 * XXX - a fancier strategy would be to insert those before the
1118 * statements of all blocks that use those lengths and that
1119 * have no predecessors that use them, so that we only compute
1120 * the lengths if we need them. There might be even better
1121 * approaches than that.
1123 * However, those strategies would be more complicated, and
1124 * as we don't generate code to compute a length if the
1125 * program has no tests that use the length, and as most
1126 * tests will probably use those lengths, we would just
1127 * postpone computing the lengths so that it's not done
1128 * for tests that fail early, and it's not clear that's
1131 insert_compute_vloffsets(cstate
, p
->head
);
1134 * For DLT_PPI captures, generate a check of the per-packet
1135 * DLT value to make sure it's DLT_IEEE802_11.
1137 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1138 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1139 * with appropriate Ethernet information and use that rather
1140 * than using something such as DLT_PPI where you don't know
1141 * the link-layer header type until runtime, which, in the
1142 * general case, would force us to generate both Ethernet *and*
1143 * 802.11 code (*and* anything else for which PPI is used)
1144 * and choose between them early in the BPF program?
1146 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
1147 if (ppi_dlt_check
!= NULL
)
1148 gen_and(ppi_dlt_check
, p
);
1150 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1151 p
->sense
= !p
->sense
;
1152 backpatch(p
, gen_retblk_internal(cstate
, 0));
1153 cstate
->ic
.root
= p
->head
;
1158 gen_and(struct block
*b0
, struct block
*b1
)
1160 backpatch(b0
, b1
->head
);
1161 b0
->sense
= !b0
->sense
;
1162 b1
->sense
= !b1
->sense
;
1164 b1
->sense
= !b1
->sense
;
1165 b1
->head
= b0
->head
;
1169 gen_or(struct block
*b0
, struct block
*b1
)
1171 b0
->sense
= !b0
->sense
;
1172 backpatch(b0
, b1
->head
);
1173 b0
->sense
= !b0
->sense
;
1175 b1
->head
= b0
->head
;
1179 gen_not(struct block
*b
)
1181 b
->sense
= !b
->sense
;
1184 static struct block
*
1185 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1186 u_int size
, bpf_u_int32 v
)
1188 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1191 static struct block
*
1192 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1193 u_int size
, bpf_u_int32 v
)
1195 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1198 static struct block
*
1199 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1200 u_int size
, bpf_u_int32 v
)
1202 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1205 static struct block
*
1206 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1207 u_int size
, bpf_u_int32 v
)
1209 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1212 static struct block
*
1213 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1214 u_int size
, bpf_u_int32 v
)
1216 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1219 static struct block
*
1220 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1221 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1223 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1226 static struct block
*
1227 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1228 u_int size
, const u_char
*v
)
1230 register struct block
*b
, *tmp
;
1234 register const u_char
*p
= &v
[size
- 4];
1236 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1244 register const u_char
*p
= &v
[size
- 2];
1246 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1254 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1263 * AND the field of size "size" at offset "offset" relative to the header
1264 * specified by "offrel" with "mask", and compare it with the value "v"
1265 * with the test specified by "jtype"; if "reverse" is true, the test
1266 * should test the opposite of "jtype".
1268 static struct block
*
1269 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1270 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1273 struct slist
*s
, *s2
;
1276 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1278 if (mask
!= 0xffffffff) {
1279 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1284 b
= new_block(cstate
, JMP(jtype
));
1293 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1295 cstate
->pcap_fddipad
= p
->fddipad
;
1298 * We start out with only one link-layer header.
1300 cstate
->outermostlinktype
= pcap_datalink(p
);
1301 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1302 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1303 cstate
->off_outermostlinkhdr
.reg
= -1;
1305 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1306 cstate
->off_prevlinkhdr
.constant_part
= 0;
1307 cstate
->off_prevlinkhdr
.is_variable
= 0;
1308 cstate
->off_prevlinkhdr
.reg
= -1;
1310 cstate
->linktype
= cstate
->outermostlinktype
;
1311 cstate
->off_linkhdr
.constant_part
= 0;
1312 cstate
->off_linkhdr
.is_variable
= 0;
1313 cstate
->off_linkhdr
.reg
= -1;
1318 cstate
->off_linkpl
.constant_part
= 0;
1319 cstate
->off_linkpl
.is_variable
= 0;
1320 cstate
->off_linkpl
.reg
= -1;
1322 cstate
->off_linktype
.constant_part
= 0;
1323 cstate
->off_linktype
.is_variable
= 0;
1324 cstate
->off_linktype
.reg
= -1;
1327 * Assume it's not raw ATM with a pseudo-header, for now.
1330 cstate
->off_vpi
= OFFSET_NOT_SET
;
1331 cstate
->off_vci
= OFFSET_NOT_SET
;
1332 cstate
->off_proto
= OFFSET_NOT_SET
;
1333 cstate
->off_payload
= OFFSET_NOT_SET
;
1336 * And not encapsulated with either Geneve or VXLAN.
1338 cstate
->is_encap
= 0;
1341 * No variable length VLAN offset by default
1343 cstate
->is_vlan_vloffset
= 0;
1346 * And assume we're not doing SS7.
1348 cstate
->off_li
= OFFSET_NOT_SET
;
1349 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1350 cstate
->off_sio
= OFFSET_NOT_SET
;
1351 cstate
->off_opc
= OFFSET_NOT_SET
;
1352 cstate
->off_dpc
= OFFSET_NOT_SET
;
1353 cstate
->off_sls
= OFFSET_NOT_SET
;
1355 cstate
->label_stack_depth
= 0;
1356 cstate
->vlan_stack_depth
= 0;
1358 switch (cstate
->linktype
) {
1361 cstate
->off_linktype
.constant_part
= 2;
1362 cstate
->off_linkpl
.constant_part
= 6;
1363 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1364 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1367 case DLT_ARCNET_LINUX
:
1368 cstate
->off_linktype
.constant_part
= 4;
1369 cstate
->off_linkpl
.constant_part
= 8;
1370 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1371 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1375 cstate
->off_linktype
.constant_part
= 12;
1376 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1377 cstate
->off_nl
= 0; /* Ethernet II */
1378 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1383 * SLIP doesn't have a link level type. The 16 byte
1384 * header is hacked into our SLIP driver.
1386 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1387 cstate
->off_linkpl
.constant_part
= 16;
1389 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1392 case DLT_SLIP_BSDOS
:
1393 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1394 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1396 cstate
->off_linkpl
.constant_part
= 24;
1398 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1403 cstate
->off_linktype
.constant_part
= 0;
1404 cstate
->off_linkpl
.constant_part
= 4;
1406 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1410 cstate
->off_linktype
.constant_part
= 0;
1411 cstate
->off_linkpl
.constant_part
= 12;
1413 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1418 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1419 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1420 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1421 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1422 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1424 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1429 * This does no include the Ethernet header, and
1430 * only covers session state.
1432 cstate
->off_linktype
.constant_part
= 6;
1433 cstate
->off_linkpl
.constant_part
= 8;
1435 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1439 cstate
->off_linktype
.constant_part
= 5;
1440 cstate
->off_linkpl
.constant_part
= 24;
1442 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1447 * FDDI doesn't really have a link-level type field.
1448 * We set "off_linktype" to the offset of the LLC header.
1450 * To check for Ethernet types, we assume that SSAP = SNAP
1451 * is being used and pick out the encapsulated Ethernet type.
1452 * XXX - should we generate code to check for SNAP?
1454 cstate
->off_linktype
.constant_part
= 13;
1455 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1456 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1457 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1458 cstate
->off_nl
= 8; /* 802.2+SNAP */
1459 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1464 * Token Ring doesn't really have a link-level type field.
1465 * We set "off_linktype" to the offset of the LLC header.
1467 * To check for Ethernet types, we assume that SSAP = SNAP
1468 * is being used and pick out the encapsulated Ethernet type.
1469 * XXX - should we generate code to check for SNAP?
1471 * XXX - the header is actually variable-length.
1472 * Some various Linux patched versions gave 38
1473 * as "off_linktype" and 40 as "off_nl"; however,
1474 * if a token ring packet has *no* routing
1475 * information, i.e. is not source-routed, the correct
1476 * values are 20 and 22, as they are in the vanilla code.
1478 * A packet is source-routed iff the uppermost bit
1479 * of the first byte of the source address, at an
1480 * offset of 8, has the uppermost bit set. If the
1481 * packet is source-routed, the total number of bytes
1482 * of routing information is 2 plus bits 0x1F00 of
1483 * the 16-bit value at an offset of 14 (shifted right
1484 * 8 - figure out which byte that is).
1486 cstate
->off_linktype
.constant_part
= 14;
1487 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1488 cstate
->off_nl
= 8; /* 802.2+SNAP */
1489 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1492 case DLT_PRISM_HEADER
:
1493 case DLT_IEEE802_11_RADIO_AVS
:
1494 case DLT_IEEE802_11_RADIO
:
1495 cstate
->off_linkhdr
.is_variable
= 1;
1496 /* Fall through, 802.11 doesn't have a variable link
1497 * prefix but is otherwise the same. */
1500 case DLT_IEEE802_11
:
1502 * 802.11 doesn't really have a link-level type field.
1503 * We set "off_linktype.constant_part" to the offset of
1506 * To check for Ethernet types, we assume that SSAP = SNAP
1507 * is being used and pick out the encapsulated Ethernet type.
1508 * XXX - should we generate code to check for SNAP?
1510 * We also handle variable-length radio headers here.
1511 * The Prism header is in theory variable-length, but in
1512 * practice it's always 144 bytes long. However, some
1513 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1514 * sometimes or always supply an AVS header, so we
1515 * have to check whether the radio header is a Prism
1516 * header or an AVS header, so, in practice, it's
1519 cstate
->off_linktype
.constant_part
= 24;
1520 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1521 cstate
->off_linkpl
.is_variable
= 1;
1522 cstate
->off_nl
= 8; /* 802.2+SNAP */
1523 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1528 * At the moment we treat PPI the same way that we treat
1529 * normal Radiotap encoded packets. The difference is in
1530 * the function that generates the code at the beginning
1531 * to compute the header length. Since this code generator
1532 * of PPI supports bare 802.11 encapsulation only (i.e.
1533 * the encapsulated DLT should be DLT_IEEE802_11) we
1534 * generate code to check for this too.
1536 cstate
->off_linktype
.constant_part
= 24;
1537 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1538 cstate
->off_linkpl
.is_variable
= 1;
1539 cstate
->off_linkhdr
.is_variable
= 1;
1540 cstate
->off_nl
= 8; /* 802.2+SNAP */
1541 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1544 case DLT_ATM_RFC1483
:
1545 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1547 * assume routed, non-ISO PDUs
1548 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1550 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1551 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1552 * latter would presumably be treated the way PPPoE
1553 * should be, so you can do "pppoe and udp port 2049"
1554 * or "pppoa and tcp port 80" and have it check for
1555 * PPPo{A,E} and a PPP protocol of IP and....
1557 cstate
->off_linktype
.constant_part
= 0;
1558 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1559 cstate
->off_nl
= 8; /* 802.2+SNAP */
1560 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1565 * Full Frontal ATM; you get AALn PDUs with an ATM
1569 cstate
->off_vpi
= SUNATM_VPI_POS
;
1570 cstate
->off_vci
= SUNATM_VCI_POS
;
1571 cstate
->off_proto
= PROTO_POS
;
1572 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1573 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1574 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1575 cstate
->off_nl
= 8; /* 802.2+SNAP */
1576 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1582 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1583 cstate
->off_linkpl
.constant_part
= 0;
1585 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1588 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1589 cstate
->off_linktype
.constant_part
= 14;
1590 cstate
->off_linkpl
.constant_part
= 16;
1592 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1595 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1596 cstate
->off_linktype
.constant_part
= 0;
1597 cstate
->off_linkpl
.constant_part
= 20;
1599 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1604 * LocalTalk does have a 1-byte type field in the LLAP header,
1605 * but really it just indicates whether there is a "short" or
1606 * "long" DDP packet following.
1608 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1609 cstate
->off_linkpl
.constant_part
= 0;
1611 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1614 case DLT_IP_OVER_FC
:
1616 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1617 * link-level type field. We set "off_linktype" to the
1618 * offset of the LLC header.
1620 * To check for Ethernet types, we assume that SSAP = SNAP
1621 * is being used and pick out the encapsulated Ethernet type.
1622 * XXX - should we generate code to check for SNAP? RFC
1623 * 2625 says SNAP should be used.
1625 cstate
->off_linktype
.constant_part
= 16;
1626 cstate
->off_linkpl
.constant_part
= 16;
1627 cstate
->off_nl
= 8; /* 802.2+SNAP */
1628 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1633 * XXX - we should set this to handle SNAP-encapsulated
1634 * frames (NLPID of 0x80).
1636 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1637 cstate
->off_linkpl
.constant_part
= 0;
1639 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1643 * the only BPF-interesting FRF.16 frames are non-control frames;
1644 * Frame Relay has a variable length link-layer
1645 * so lets start with offset 4 for now and increments later on (FIXME);
1648 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1649 cstate
->off_linkpl
.constant_part
= 0;
1651 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1654 case DLT_APPLE_IP_OVER_IEEE1394
:
1655 cstate
->off_linktype
.constant_part
= 16;
1656 cstate
->off_linkpl
.constant_part
= 18;
1658 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1661 case DLT_SYMANTEC_FIREWALL
:
1662 cstate
->off_linktype
.constant_part
= 6;
1663 cstate
->off_linkpl
.constant_part
= 44;
1664 cstate
->off_nl
= 0; /* Ethernet II */
1665 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1669 cstate
->off_linktype
.constant_part
= 0;
1670 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1671 cstate
->off_linkpl
.is_variable
= 1;
1673 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1676 case DLT_JUNIPER_MFR
:
1677 case DLT_JUNIPER_MLFR
:
1678 case DLT_JUNIPER_MLPPP
:
1679 case DLT_JUNIPER_PPP
:
1680 case DLT_JUNIPER_CHDLC
:
1681 case DLT_JUNIPER_FRELAY
:
1682 cstate
->off_linktype
.constant_part
= 4;
1683 cstate
->off_linkpl
.constant_part
= 4;
1685 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1688 case DLT_JUNIPER_ATM1
:
1689 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1690 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1692 cstate
->off_nl_nosnap
= 10;
1695 case DLT_JUNIPER_ATM2
:
1696 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1697 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1699 cstate
->off_nl_nosnap
= 10;
1702 /* frames captured on a Juniper PPPoE service PIC
1703 * contain raw ethernet frames */
1704 case DLT_JUNIPER_PPPOE
:
1705 case DLT_JUNIPER_ETHER
:
1706 cstate
->off_linkpl
.constant_part
= 14;
1707 cstate
->off_linktype
.constant_part
= 16;
1708 cstate
->off_nl
= 18; /* Ethernet II */
1709 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1712 case DLT_JUNIPER_PPPOE_ATM
:
1713 cstate
->off_linktype
.constant_part
= 4;
1714 cstate
->off_linkpl
.constant_part
= 6;
1716 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1719 case DLT_JUNIPER_GGSN
:
1720 cstate
->off_linktype
.constant_part
= 6;
1721 cstate
->off_linkpl
.constant_part
= 12;
1723 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1726 case DLT_JUNIPER_ES
:
1727 cstate
->off_linktype
.constant_part
= 6;
1728 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1729 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1730 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1733 case DLT_JUNIPER_MONITOR
:
1734 cstate
->off_linktype
.constant_part
= 12;
1735 cstate
->off_linkpl
.constant_part
= 12;
1736 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1737 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1740 case DLT_BACNET_MS_TP
:
1741 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1742 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1743 cstate
->off_nl
= OFFSET_NOT_SET
;
1744 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1747 case DLT_JUNIPER_SERVICES
:
1748 cstate
->off_linktype
.constant_part
= 12;
1749 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1750 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1751 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1754 case DLT_JUNIPER_VP
:
1755 cstate
->off_linktype
.constant_part
= 18;
1756 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1757 cstate
->off_nl
= OFFSET_NOT_SET
;
1758 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1761 case DLT_JUNIPER_ST
:
1762 cstate
->off_linktype
.constant_part
= 18;
1763 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1764 cstate
->off_nl
= OFFSET_NOT_SET
;
1765 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1768 case DLT_JUNIPER_ISM
:
1769 cstate
->off_linktype
.constant_part
= 8;
1770 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1771 cstate
->off_nl
= OFFSET_NOT_SET
;
1772 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1775 case DLT_JUNIPER_VS
:
1776 case DLT_JUNIPER_SRX_E2E
:
1777 case DLT_JUNIPER_FIBRECHANNEL
:
1778 case DLT_JUNIPER_ATM_CEMIC
:
1779 cstate
->off_linktype
.constant_part
= 8;
1780 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1781 cstate
->off_nl
= OFFSET_NOT_SET
;
1782 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1787 cstate
->off_li_hsl
= 4;
1788 cstate
->off_sio
= 3;
1789 cstate
->off_opc
= 4;
1790 cstate
->off_dpc
= 4;
1791 cstate
->off_sls
= 7;
1792 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1793 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1794 cstate
->off_nl
= OFFSET_NOT_SET
;
1795 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1798 case DLT_MTP2_WITH_PHDR
:
1800 cstate
->off_li_hsl
= 8;
1801 cstate
->off_sio
= 7;
1802 cstate
->off_opc
= 8;
1803 cstate
->off_dpc
= 8;
1804 cstate
->off_sls
= 11;
1805 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1806 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1807 cstate
->off_nl
= OFFSET_NOT_SET
;
1808 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1812 cstate
->off_li
= 22;
1813 cstate
->off_li_hsl
= 24;
1814 cstate
->off_sio
= 23;
1815 cstate
->off_opc
= 24;
1816 cstate
->off_dpc
= 24;
1817 cstate
->off_sls
= 27;
1818 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1819 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1820 cstate
->off_nl
= OFFSET_NOT_SET
;
1821 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1825 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1826 cstate
->off_linkpl
.constant_part
= 4;
1828 cstate
->off_nl_nosnap
= 0;
1833 * Currently, only raw "link[N:M]" filtering is supported.
1835 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1836 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1837 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1838 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1842 cstate
->off_linktype
.constant_part
= 1;
1843 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1845 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1848 case DLT_NETANALYZER
:
1849 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1850 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1851 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1852 cstate
->off_nl
= 0; /* Ethernet II */
1853 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1856 case DLT_NETANALYZER_TRANSPARENT
:
1857 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1858 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1859 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1860 cstate
->off_nl
= 0; /* Ethernet II */
1861 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1866 * For values in the range in which we've assigned new
1867 * DLT_ values, only raw "link[N:M]" filtering is supported.
1869 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1870 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1871 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1872 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1873 cstate
->off_nl
= OFFSET_NOT_SET
;
1874 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1876 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1877 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1883 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1888 * Load a value relative to the specified absolute offset.
1890 static struct slist
*
1891 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1892 u_int offset
, u_int size
)
1894 struct slist
*s
, *s2
;
1896 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1899 * If "s" is non-null, it has code to arrange that the X register
1900 * contains the variable part of the absolute offset, so we
1901 * generate a load relative to that, with an offset of
1902 * abs_offset->constant_part + offset.
1904 * Otherwise, we can do an absolute load with an offset of
1905 * abs_offset->constant_part + offset.
1909 * "s" points to a list of statements that puts the
1910 * variable part of the absolute offset into the X register.
1911 * Do an indirect load, to use the X register as an offset.
1913 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1914 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1918 * There is no variable part of the absolute offset, so
1919 * just do an absolute load.
1921 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1922 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1928 * Load a value relative to the beginning of the specified header.
1930 static struct slist
*
1931 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1934 struct slist
*s
, *s2
;
1937 * Squelch warnings from compilers that *don't* assume that
1938 * offrel always has a valid enum value and therefore don't
1939 * assume that we'll always go through one of the case arms.
1941 * If we have a default case, compilers that *do* assume that
1942 * will then complain about the default case code being
1945 * Damned if you do, damned if you don't.
1952 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1957 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1960 case OR_PREVLINKHDR
:
1961 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1965 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1968 case OR_PREVMPLSHDR
:
1969 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1973 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1976 case OR_LINKPL_NOSNAP
:
1977 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1981 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1986 * Load the X register with the length of the IPv4 header
1987 * (plus the offset of the link-layer header, if it's
1988 * preceded by a variable-length header such as a radio
1989 * header), in bytes.
1991 s
= gen_loadx_iphdrlen(cstate
);
1994 * Load the item at {offset of the link-layer payload} +
1995 * {offset, relative to the start of the link-layer
1996 * payload, of the IPv4 header} + {length of the IPv4 header} +
1997 * {specified offset}.
1999 * If the offset of the link-layer payload is variable,
2000 * the variable part of that offset is included in the
2001 * value in the X register, and we include the constant
2002 * part in the offset of the load.
2004 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2005 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2010 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2017 * Generate code to load into the X register the sum of the length of
2018 * the IPv4 header and the variable part of the offset of the link-layer
2021 static struct slist
*
2022 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2024 struct slist
*s
, *s2
;
2026 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2029 * The offset of the link-layer payload has a variable
2030 * part. "s" points to a list of statements that put
2031 * the variable part of that offset into the X register.
2033 * The 4*([k]&0xf) addressing mode can't be used, as we
2034 * don't have a constant offset, so we have to load the
2035 * value in question into the A register and add to it
2036 * the value from the X register.
2038 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2039 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2041 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2044 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2049 * The A register now contains the length of the IP header.
2050 * We need to add to it the variable part of the offset of
2051 * the link-layer payload, which is still in the X
2052 * register, and move the result into the X register.
2054 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2055 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2058 * The offset of the link-layer payload is a constant,
2059 * so no code was generated to load the (nonexistent)
2060 * variable part of that offset.
2062 * This means we can use the 4*([k]&0xf) addressing
2063 * mode. Load the length of the IPv4 header, which
2064 * is at an offset of cstate->off_nl from the beginning of
2065 * the link-layer payload, and thus at an offset of
2066 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2067 * of the raw packet data, using that addressing mode.
2069 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2070 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2076 static struct block
*
2077 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2082 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2084 b
= new_block(cstate
, JMP(BPF_JEQ
));
2090 static inline struct block
*
2091 gen_true(compiler_state_t
*cstate
)
2093 return gen_uncond(cstate
, 1);
2096 static inline struct block
*
2097 gen_false(compiler_state_t
*cstate
)
2099 return gen_uncond(cstate
, 0);
2103 * Generate code to match a particular packet type.
2105 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2106 * value, if <= ETHERMTU. We use that to determine whether to
2107 * match the type/length field or to check the type/length field for
2108 * a value <= ETHERMTU to see whether it's a type field and then do
2109 * the appropriate test.
2111 static struct block
*
2112 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2114 struct block
*b0
, *b1
;
2120 case LLCSAP_NETBEUI
:
2122 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2123 * so we check the DSAP and SSAP.
2125 * LLCSAP_IP checks for IP-over-802.2, rather
2126 * than IP-over-Ethernet or IP-over-SNAP.
2128 * XXX - should we check both the DSAP and the
2129 * SSAP, like this, or should we check just the
2130 * DSAP, as we do for other types <= ETHERMTU
2131 * (i.e., other SAP values)?
2133 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2135 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2143 * Ethernet_II frames, which are Ethernet
2144 * frames with a frame type of ETHERTYPE_IPX;
2146 * Ethernet_802.3 frames, which are 802.3
2147 * frames (i.e., the type/length field is
2148 * a length field, <= ETHERMTU, rather than
2149 * a type field) with the first two bytes
2150 * after the Ethernet/802.3 header being
2153 * Ethernet_802.2 frames, which are 802.3
2154 * frames with an 802.2 LLC header and
2155 * with the IPX LSAP as the DSAP in the LLC
2158 * Ethernet_SNAP frames, which are 802.3
2159 * frames with an LLC header and a SNAP
2160 * header and with an OUI of 0x000000
2161 * (encapsulated Ethernet) and a protocol
2162 * ID of ETHERTYPE_IPX in the SNAP header.
2164 * XXX - should we generate the same code both
2165 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2169 * This generates code to check both for the
2170 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2172 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2173 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2177 * Now we add code to check for SNAP frames with
2178 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2180 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2184 * Now we generate code to check for 802.3
2185 * frames in general.
2187 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2191 * Now add the check for 802.3 frames before the
2192 * check for Ethernet_802.2 and Ethernet_802.3,
2193 * as those checks should only be done on 802.3
2194 * frames, not on Ethernet frames.
2199 * Now add the check for Ethernet_II frames, and
2200 * do that before checking for the other frame
2203 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2207 case ETHERTYPE_ATALK
:
2208 case ETHERTYPE_AARP
:
2210 * EtherTalk (AppleTalk protocols on Ethernet link
2211 * layer) may use 802.2 encapsulation.
2215 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2216 * we check for an Ethernet type field less than
2217 * 1500, which means it's an 802.3 length field.
2219 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2223 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2224 * SNAP packets with an organization code of
2225 * 0x080007 (Apple, for Appletalk) and a protocol
2226 * type of ETHERTYPE_ATALK (Appletalk).
2228 * 802.2-encapsulated ETHERTYPE_AARP packets are
2229 * SNAP packets with an organization code of
2230 * 0x000000 (encapsulated Ethernet) and a protocol
2231 * type of ETHERTYPE_AARP (Appletalk ARP).
2233 if (ll_proto
== ETHERTYPE_ATALK
)
2234 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2235 else /* ll_proto == ETHERTYPE_AARP */
2236 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2240 * Check for Ethernet encapsulation (Ethertalk
2241 * phase 1?); we just check for the Ethernet
2244 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2250 if (ll_proto
<= ETHERMTU
) {
2252 * This is an LLC SAP value, so the frames
2253 * that match would be 802.2 frames.
2254 * Check that the frame is an 802.2 frame
2255 * (i.e., that the length/type field is
2256 * a length field, <= ETHERMTU) and
2257 * then check the DSAP.
2259 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2261 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2266 * This is an Ethernet type, so compare
2267 * the length/type field with it (if
2268 * the frame is an 802.2 frame, the length
2269 * field will be <= ETHERMTU, and, as
2270 * "ll_proto" is > ETHERMTU, this test
2271 * will fail and the frame won't match,
2272 * which is what we want).
2274 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2279 static struct block
*
2280 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2283 * For DLT_NULL, the link-layer header is a 32-bit word
2284 * containing an AF_ value in *host* byte order, and for
2285 * DLT_ENC, the link-layer header begins with a 32-bit
2286 * word containing an AF_ value in host byte order.
2288 * In addition, if we're reading a saved capture file,
2289 * the host byte order in the capture may not be the
2290 * same as the host byte order on this machine.
2292 * For DLT_LOOP, the link-layer header is a 32-bit
2293 * word containing an AF_ value in *network* byte order.
2295 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2297 * The AF_ value is in host byte order, but the BPF
2298 * interpreter will convert it to network byte order.
2300 * If this is a save file, and it's from a machine
2301 * with the opposite byte order to ours, we byte-swap
2304 * Then we run it through "htonl()", and generate
2305 * code to compare against the result.
2307 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2308 ll_proto
= SWAPLONG(ll_proto
);
2309 ll_proto
= htonl(ll_proto
);
2311 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2315 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2316 * or IPv6 then we have an error.
2318 static struct block
*
2319 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2324 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2327 case ETHERTYPE_IPV6
:
2328 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2335 return gen_false(cstate
);
2339 * Generate code to match a particular packet type.
2341 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2342 * value, if <= ETHERMTU. We use that to determine whether to
2343 * match the type field or to check the type field for the special
2344 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2346 static struct block
*
2347 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2349 struct block
*b0
, *b1
;
2355 case LLCSAP_NETBEUI
:
2357 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2358 * so we check the DSAP and SSAP.
2360 * LLCSAP_IP checks for IP-over-802.2, rather
2361 * than IP-over-Ethernet or IP-over-SNAP.
2363 * XXX - should we check both the DSAP and the
2364 * SSAP, like this, or should we check just the
2365 * DSAP, as we do for other types <= ETHERMTU
2366 * (i.e., other SAP values)?
2368 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2369 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2375 * Ethernet_II frames, which are Ethernet
2376 * frames with a frame type of ETHERTYPE_IPX;
2378 * Ethernet_802.3 frames, which have a frame
2379 * type of LINUX_SLL_P_802_3;
2381 * Ethernet_802.2 frames, which are 802.3
2382 * frames with an 802.2 LLC header (i.e, have
2383 * a frame type of LINUX_SLL_P_802_2) and
2384 * with the IPX LSAP as the DSAP in the LLC
2387 * Ethernet_SNAP frames, which are 802.3
2388 * frames with an LLC header and a SNAP
2389 * header and with an OUI of 0x000000
2390 * (encapsulated Ethernet) and a protocol
2391 * ID of ETHERTYPE_IPX in the SNAP header.
2393 * First, do the checks on LINUX_SLL_P_802_2
2394 * frames; generate the check for either
2395 * Ethernet_802.2 or Ethernet_SNAP frames, and
2396 * then put a check for LINUX_SLL_P_802_2 frames
2399 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2400 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2402 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2406 * Now check for 802.3 frames and OR that with
2407 * the previous test.
2409 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2413 * Now add the check for Ethernet_II frames, and
2414 * do that before checking for the other frame
2417 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2421 case ETHERTYPE_ATALK
:
2422 case ETHERTYPE_AARP
:
2424 * EtherTalk (AppleTalk protocols on Ethernet link
2425 * layer) may use 802.2 encapsulation.
2429 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2430 * we check for the 802.2 protocol type in the
2431 * "Ethernet type" field.
2433 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2436 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2437 * SNAP packets with an organization code of
2438 * 0x080007 (Apple, for Appletalk) and a protocol
2439 * type of ETHERTYPE_ATALK (Appletalk).
2441 * 802.2-encapsulated ETHERTYPE_AARP packets are
2442 * SNAP packets with an organization code of
2443 * 0x000000 (encapsulated Ethernet) and a protocol
2444 * type of ETHERTYPE_AARP (Appletalk ARP).
2446 if (ll_proto
== ETHERTYPE_ATALK
)
2447 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2448 else /* ll_proto == ETHERTYPE_AARP */
2449 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2453 * Check for Ethernet encapsulation (Ethertalk
2454 * phase 1?); we just check for the Ethernet
2457 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2463 if (ll_proto
<= ETHERMTU
) {
2465 * This is an LLC SAP value, so the frames
2466 * that match would be 802.2 frames.
2467 * Check for the 802.2 protocol type
2468 * in the "Ethernet type" field, and
2469 * then check the DSAP.
2471 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2472 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2478 * This is an Ethernet type, so compare
2479 * the length/type field with it (if
2480 * the frame is an 802.2 frame, the length
2481 * field will be <= ETHERMTU, and, as
2482 * "ll_proto" is > ETHERMTU, this test
2483 * will fail and the frame won't match,
2484 * which is what we want).
2486 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2492 * Load a value relative to the beginning of the link-layer header after the
2495 static struct slist
*
2496 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2498 struct slist
*s1
, *s2
;
2501 * Generate code to load the length of the pflog header into
2502 * the register assigned to hold that length, if one has been
2503 * assigned. (If one hasn't been assigned, no code we've
2504 * generated uses that prefix, so we don't need to generate any
2507 if (cstate
->off_linkpl
.reg
!= -1) {
2509 * The length is in the first byte of the header.
2511 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2515 * Round it up to a multiple of 4.
2516 * Add 3, and clear the lower 2 bits.
2518 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2521 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2522 s2
->s
.k
= 0xfffffffc;
2526 * Now allocate a register to hold that value and store
2529 s2
= new_stmt(cstate
, BPF_ST
);
2530 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2534 * Now move it into the X register.
2536 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2544 static struct slist
*
2545 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2547 struct slist
*s1
, *s2
;
2548 struct slist
*sjeq_avs_cookie
;
2549 struct slist
*sjcommon
;
2552 * This code is not compatible with the optimizer, as
2553 * we are generating jmp instructions within a normal
2554 * slist of instructions
2556 cstate
->no_optimize
= 1;
2559 * Generate code to load the length of the radio header into
2560 * the register assigned to hold that length, if one has been
2561 * assigned. (If one hasn't been assigned, no code we've
2562 * generated uses that prefix, so we don't need to generate any
2565 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2566 * or always use the AVS header rather than the Prism header.
2567 * We load a 4-byte big-endian value at the beginning of the
2568 * raw packet data, and see whether, when masked with 0xFFFFF000,
2569 * it's equal to 0x80211000. If so, that indicates that it's
2570 * an AVS header (the masked-out bits are the version number).
2571 * Otherwise, it's a Prism header.
2573 * XXX - the Prism header is also, in theory, variable-length,
2574 * but no known software generates headers that aren't 144
2577 if (cstate
->off_linkhdr
.reg
!= -1) {
2581 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2585 * AND it with 0xFFFFF000.
2587 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2588 s2
->s
.k
= 0xFFFFF000;
2592 * Compare with 0x80211000.
2594 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2595 sjeq_avs_cookie
->s
.k
= 0x80211000;
2596 sappend(s1
, sjeq_avs_cookie
);
2601 * The 4 bytes at an offset of 4 from the beginning of
2602 * the AVS header are the length of the AVS header.
2603 * That field is big-endian.
2605 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2608 sjeq_avs_cookie
->s
.jt
= s2
;
2611 * Now jump to the code to allocate a register
2612 * into which to save the header length and
2613 * store the length there. (The "jump always"
2614 * instruction needs to have the k field set;
2615 * it's added to the PC, so, as we're jumping
2616 * over a single instruction, it should be 1.)
2618 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2620 sappend(s1
, sjcommon
);
2623 * Now for the code that handles the Prism header.
2624 * Just load the length of the Prism header (144)
2625 * into the A register. Have the test for an AVS
2626 * header branch here if we don't have an AVS header.
2628 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2631 sjeq_avs_cookie
->s
.jf
= s2
;
2634 * Now allocate a register to hold that value and store
2635 * it. The code for the AVS header will jump here after
2636 * loading the length of the AVS header.
2638 s2
= new_stmt(cstate
, BPF_ST
);
2639 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2641 sjcommon
->s
.jf
= s2
;
2644 * Now move it into the X register.
2646 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2654 static struct slist
*
2655 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2657 struct slist
*s1
, *s2
;
2660 * Generate code to load the length of the AVS header into
2661 * the register assigned to hold that length, if one has been
2662 * assigned. (If one hasn't been assigned, no code we've
2663 * generated uses that prefix, so we don't need to generate any
2666 if (cstate
->off_linkhdr
.reg
!= -1) {
2668 * The 4 bytes at an offset of 4 from the beginning of
2669 * the AVS header are the length of the AVS header.
2670 * That field is big-endian.
2672 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2676 * Now allocate a register to hold that value and store
2679 s2
= new_stmt(cstate
, BPF_ST
);
2680 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2684 * Now move it into the X register.
2686 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2694 static struct slist
*
2695 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2697 struct slist
*s1
, *s2
;
2700 * Generate code to load the length of the radiotap header into
2701 * the register assigned to hold that length, if one has been
2702 * assigned. (If one hasn't been assigned, no code we've
2703 * generated uses that prefix, so we don't need to generate any
2706 if (cstate
->off_linkhdr
.reg
!= -1) {
2708 * The 2 bytes at offsets of 2 and 3 from the beginning
2709 * of the radiotap header are the length of the radiotap
2710 * header; unfortunately, it's little-endian, so we have
2711 * to load it a byte at a time and construct the value.
2715 * Load the high-order byte, at an offset of 3, shift it
2716 * left a byte, and put the result in the X register.
2718 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2720 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2723 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2727 * Load the next byte, at an offset of 2, and OR the
2728 * value from the X register into it.
2730 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2733 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2737 * Now allocate a register to hold that value and store
2740 s2
= new_stmt(cstate
, BPF_ST
);
2741 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2745 * Now move it into the X register.
2747 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2756 * At the moment we treat PPI as normal Radiotap encoded
2757 * packets. The difference is in the function that generates
2758 * the code at the beginning to compute the header length.
2759 * Since this code generator of PPI supports bare 802.11
2760 * encapsulation only (i.e. the encapsulated DLT should be
2761 * DLT_IEEE802_11) we generate code to check for this too;
2762 * that's done in finish_parse().
2764 static struct slist
*
2765 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2767 struct slist
*s1
, *s2
;
2770 * Generate code to load the length of the radiotap header
2771 * into the register assigned to hold that length, if one has
2774 if (cstate
->off_linkhdr
.reg
!= -1) {
2776 * The 2 bytes at offsets of 2 and 3 from the beginning
2777 * of the radiotap header are the length of the radiotap
2778 * header; unfortunately, it's little-endian, so we have
2779 * to load it a byte at a time and construct the value.
2783 * Load the high-order byte, at an offset of 3, shift it
2784 * left a byte, and put the result in the X register.
2786 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2788 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2791 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2795 * Load the next byte, at an offset of 2, and OR the
2796 * value from the X register into it.
2798 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2801 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2805 * Now allocate a register to hold that value and store
2808 s2
= new_stmt(cstate
, BPF_ST
);
2809 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2813 * Now move it into the X register.
2815 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2824 * Load a value relative to the beginning of the link-layer header after the 802.11
2825 * header, i.e. LLC_SNAP.
2826 * The link-layer header doesn't necessarily begin at the beginning
2827 * of the packet data; there might be a variable-length prefix containing
2828 * radio information.
2830 static struct slist
*
2831 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2834 struct slist
*sjset_data_frame_1
;
2835 struct slist
*sjset_data_frame_2
;
2836 struct slist
*sjset_qos
;
2837 struct slist
*sjset_radiotap_flags_present
;
2838 struct slist
*sjset_radiotap_ext_present
;
2839 struct slist
*sjset_radiotap_tsft_present
;
2840 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2841 struct slist
*s_roundup
;
2843 if (cstate
->off_linkpl
.reg
== -1) {
2845 * No register has been assigned to the offset of
2846 * the link-layer payload, which means nobody needs
2847 * it; don't bother computing it - just return
2848 * what we already have.
2854 * This code is not compatible with the optimizer, as
2855 * we are generating jmp instructions within a normal
2856 * slist of instructions
2858 cstate
->no_optimize
= 1;
2861 * If "s" is non-null, it has code to arrange that the X register
2862 * contains the length of the prefix preceding the link-layer
2865 * Otherwise, the length of the prefix preceding the link-layer
2866 * header is "off_outermostlinkhdr.constant_part".
2870 * There is no variable-length header preceding the
2871 * link-layer header.
2873 * Load the length of the fixed-length prefix preceding
2874 * the link-layer header (if any) into the X register,
2875 * and store it in the cstate->off_linkpl.reg register.
2876 * That length is off_outermostlinkhdr.constant_part.
2878 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2879 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2883 * The X register contains the offset of the beginning of the
2884 * link-layer header; add 24, which is the minimum length
2885 * of the MAC header for a data frame, to that, and store it
2886 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2887 * which is at the offset in the X register, with an indexed load.
2889 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2891 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2894 s2
= new_stmt(cstate
, BPF_ST
);
2895 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2898 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2903 * Check the Frame Control field to see if this is a data frame;
2904 * a data frame has the 0x08 bit (b3) in that field set and the
2905 * 0x04 bit (b2) clear.
2907 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2908 sjset_data_frame_1
->s
.k
= 0x08;
2909 sappend(s
, sjset_data_frame_1
);
2912 * If b3 is set, test b2, otherwise go to the first statement of
2913 * the rest of the program.
2915 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2916 sjset_data_frame_2
->s
.k
= 0x04;
2917 sappend(s
, sjset_data_frame_2
);
2918 sjset_data_frame_1
->s
.jf
= snext
;
2921 * If b2 is not set, this is a data frame; test the QoS bit.
2922 * Otherwise, go to the first statement of the rest of the
2925 sjset_data_frame_2
->s
.jt
= snext
;
2926 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2927 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2928 sappend(s
, sjset_qos
);
2931 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2933 * Otherwise, go to the first statement of the rest of the
2936 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2937 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2939 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2942 s2
= new_stmt(cstate
, BPF_ST
);
2943 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2947 * If we have a radiotap header, look at it to see whether
2948 * there's Atheros padding between the MAC-layer header
2951 * Note: all of the fields in the radiotap header are
2952 * little-endian, so we byte-swap all of the values
2953 * we test against, as they will be loaded as big-endian
2956 * XXX - in the general case, we would have to scan through
2957 * *all* the presence bits, if there's more than one word of
2958 * presence bits. That would require a loop, meaning that
2959 * we wouldn't be able to run the filter in the kernel.
2961 * We assume here that the Atheros adapters that insert the
2962 * annoying padding don't have multiple antennae and therefore
2963 * do not generate radiotap headers with multiple presence words.
2965 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2967 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2968 * in the first presence flag word?
2970 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2974 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2975 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2976 sappend(s
, sjset_radiotap_flags_present
);
2979 * If not, skip all of this.
2981 sjset_radiotap_flags_present
->s
.jf
= snext
;
2984 * Otherwise, is the "extension" bit set in that word?
2986 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2987 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2988 sappend(s
, sjset_radiotap_ext_present
);
2989 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2992 * If so, skip all of this.
2994 sjset_radiotap_ext_present
->s
.jt
= snext
;
2997 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2999 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3000 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3001 sappend(s
, sjset_radiotap_tsft_present
);
3002 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3005 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3006 * at an offset of 16 from the beginning of the raw packet
3007 * data (8 bytes for the radiotap header and 8 bytes for
3010 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3013 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3016 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3018 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3019 sjset_tsft_datapad
->s
.k
= 0x20;
3020 sappend(s
, sjset_tsft_datapad
);
3023 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3024 * at an offset of 8 from the beginning of the raw packet
3025 * data (8 bytes for the radiotap header).
3027 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3030 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3033 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3035 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3036 sjset_notsft_datapad
->s
.k
= 0x20;
3037 sappend(s
, sjset_notsft_datapad
);
3040 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3041 * set, round the length of the 802.11 header to
3042 * a multiple of 4. Do that by adding 3 and then
3043 * dividing by and multiplying by 4, which we do by
3046 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3047 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3048 sappend(s
, s_roundup
);
3049 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3052 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3053 s2
->s
.k
= (bpf_u_int32
)~3;
3055 s2
= new_stmt(cstate
, BPF_ST
);
3056 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3059 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3060 sjset_tsft_datapad
->s
.jf
= snext
;
3061 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3062 sjset_notsft_datapad
->s
.jf
= snext
;
3064 sjset_qos
->s
.jf
= snext
;
3070 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3074 /* There is an implicit dependency between the link
3075 * payload and link header since the payload computation
3076 * includes the variable part of the header. Therefore,
3077 * if nobody else has allocated a register for the link
3078 * header and we need it, do it now. */
3079 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3080 cstate
->off_linkhdr
.reg
== -1)
3081 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3084 * For link-layer types that have a variable-length header
3085 * preceding the link-layer header, generate code to load
3086 * the offset of the link-layer header into the register
3087 * assigned to that offset, if any.
3089 * XXX - this, and the next switch statement, won't handle
3090 * encapsulation of 802.11 or 802.11+radio information in
3091 * some other protocol stack. That's significantly more
3094 switch (cstate
->outermostlinktype
) {
3096 case DLT_PRISM_HEADER
:
3097 s
= gen_load_prism_llprefixlen(cstate
);
3100 case DLT_IEEE802_11_RADIO_AVS
:
3101 s
= gen_load_avs_llprefixlen(cstate
);
3104 case DLT_IEEE802_11_RADIO
:
3105 s
= gen_load_radiotap_llprefixlen(cstate
);
3109 s
= gen_load_ppi_llprefixlen(cstate
);
3118 * For link-layer types that have a variable-length link-layer
3119 * header, generate code to load the offset of the link-layer
3120 * payload into the register assigned to that offset, if any.
3122 switch (cstate
->outermostlinktype
) {
3124 case DLT_IEEE802_11
:
3125 case DLT_PRISM_HEADER
:
3126 case DLT_IEEE802_11_RADIO_AVS
:
3127 case DLT_IEEE802_11_RADIO
:
3129 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3133 s
= gen_load_pflog_llprefixlen(cstate
);
3138 * If there is no initialization yet and we need variable
3139 * length offsets for VLAN, initialize them to zero
3141 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3144 if (cstate
->off_linkpl
.reg
== -1)
3145 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3146 if (cstate
->off_linktype
.reg
== -1)
3147 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3149 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3151 s2
= new_stmt(cstate
, BPF_ST
);
3152 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3154 s2
= new_stmt(cstate
, BPF_ST
);
3155 s2
->s
.k
= cstate
->off_linktype
.reg
;
3160 * If we have any offset-loading code, append all the
3161 * existing statements in the block to those statements,
3162 * and make the resulting list the list of statements
3166 sappend(s
, b
->stmts
);
3171 static struct block
*
3172 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3174 struct slist
*s_load_dlt
;
3177 if (cstate
->linktype
== DLT_PPI
)
3179 /* Create the statements that check for the DLT
3181 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3182 s_load_dlt
->s
.k
= 4;
3184 b
= new_block(cstate
, JMP(BPF_JEQ
));
3186 b
->stmts
= s_load_dlt
;
3187 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3198 * Take an absolute offset, and:
3200 * if it has no variable part, return NULL;
3202 * if it has a variable part, generate code to load the register
3203 * containing that variable part into the X register, returning
3204 * a pointer to that code - if no register for that offset has
3205 * been allocated, allocate it first.
3207 * (The code to set that register will be generated later, but will
3208 * be placed earlier in the code sequence.)
3210 static struct slist
*
3211 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3215 if (off
->is_variable
) {
3216 if (off
->reg
== -1) {
3218 * We haven't yet assigned a register for the
3219 * variable part of the offset of the link-layer
3220 * header; allocate one.
3222 off
->reg
= alloc_reg(cstate
);
3226 * Load the register containing the variable part of the
3227 * offset of the link-layer header into the X register.
3229 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3234 * That offset isn't variable, there's no variable part,
3235 * so we don't need to generate any code.
3242 * Map an Ethernet type to the equivalent PPP type.
3245 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3253 case ETHERTYPE_IPV6
:
3254 ll_proto
= PPP_IPV6
;
3258 ll_proto
= PPP_DECNET
;
3261 case ETHERTYPE_ATALK
:
3262 ll_proto
= PPP_APPLE
;
3275 * I'm assuming the "Bridging PDU"s that go
3276 * over PPP are Spanning Tree Protocol
3279 ll_proto
= PPP_BRPDU
;
3290 * Generate any tests that, for encapsulation of a link-layer packet
3291 * inside another protocol stack, need to be done to check for those
3292 * link-layer packets (and that haven't already been done by a check
3293 * for that encapsulation).
3295 static struct block
*
3296 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3300 if (cstate
->is_encap
)
3301 return gen_encap_ll_check(cstate
);
3303 switch (cstate
->prevlinktype
) {
3307 * This is LANE-encapsulated Ethernet; check that the LANE
3308 * packet doesn't begin with an LE Control marker, i.e.
3309 * that it's data, not a control message.
3311 * (We've already generated a test for LANE.)
3313 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3319 * No such tests are necessary.
3327 * The three different values we should check for when checking for an
3328 * IPv6 packet with DLT_NULL.
3330 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3331 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3332 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3335 * Generate code to match a particular packet type by matching the
3336 * link-layer type field or fields in the 802.2 LLC header.
3338 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3339 * value, if <= ETHERMTU.
3341 static struct block
*
3342 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3344 struct block
*b0
, *b1
, *b2
;
3345 const char *description
;
3347 /* are we checking MPLS-encapsulated packets? */
3348 if (cstate
->label_stack_depth
> 0)
3349 return gen_mpls_linktype(cstate
, ll_proto
);
3351 switch (cstate
->linktype
) {
3354 case DLT_NETANALYZER
:
3355 case DLT_NETANALYZER_TRANSPARENT
:
3356 /* Geneve has an EtherType regardless of whether there is an
3357 * L2 header. VXLAN always has an EtherType. */
3358 if (!cstate
->is_encap
)
3359 b0
= gen_prevlinkhdr_check(cstate
);
3363 b1
= gen_ether_linktype(cstate
, ll_proto
);
3374 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3378 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3382 case DLT_IEEE802_11
:
3383 case DLT_PRISM_HEADER
:
3384 case DLT_IEEE802_11_RADIO_AVS
:
3385 case DLT_IEEE802_11_RADIO
:
3388 * Check that we have a data frame.
3390 b0
= gen_check_802_11_data_frame(cstate
);
3393 * Now check for the specified link-layer type.
3395 b1
= gen_llc_linktype(cstate
, ll_proto
);
3402 * XXX - check for LLC frames.
3404 return gen_llc_linktype(cstate
, ll_proto
);
3409 * XXX - check for LLC PDUs, as per IEEE 802.5.
3411 return gen_llc_linktype(cstate
, ll_proto
);
3414 case DLT_ATM_RFC1483
:
3416 case DLT_IP_OVER_FC
:
3417 return gen_llc_linktype(cstate
, ll_proto
);
3422 * Check for an LLC-encapsulated version of this protocol;
3423 * if we were checking for LANE, linktype would no longer
3426 * Check for LLC encapsulation and then check the protocol.
3428 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3429 b1
= gen_llc_linktype(cstate
, ll_proto
);
3435 return gen_linux_sll_linktype(cstate
, ll_proto
);
3439 case DLT_SLIP_BSDOS
:
3442 * These types don't provide any type field; packets
3443 * are always IPv4 or IPv6.
3445 * XXX - for IPv4, check for a version number of 4, and,
3446 * for IPv6, check for a version number of 6?
3451 /* Check for a version number of 4. */
3452 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3454 case ETHERTYPE_IPV6
:
3455 /* Check for a version number of 6. */
3456 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3459 return gen_false(cstate
); /* always false */
3465 * Raw IPv4, so no type field.
3467 if (ll_proto
== ETHERTYPE_IP
)
3468 return gen_true(cstate
); /* always true */
3470 /* Checking for something other than IPv4; always false */
3471 return gen_false(cstate
);
3476 * Raw IPv6, so no type field.
3478 if (ll_proto
== ETHERTYPE_IPV6
)
3479 return gen_true(cstate
); /* always true */
3481 /* Checking for something other than IPv6; always false */
3482 return gen_false(cstate
);
3487 case DLT_PPP_SERIAL
:
3490 * We use Ethernet protocol types inside libpcap;
3491 * map them to the corresponding PPP protocol types.
3493 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3494 ethertype_to_ppptype(ll_proto
));
3499 * We use Ethernet protocol types inside libpcap;
3500 * map them to the corresponding PPP protocol types.
3506 * Also check for Van Jacobson-compressed IP.
3507 * XXX - do this for other forms of PPP?
3509 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3510 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3512 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3517 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3518 ethertype_to_ppptype(ll_proto
));
3528 return (gen_loopback_linktype(cstate
, AF_INET
));
3530 case ETHERTYPE_IPV6
:
3532 * AF_ values may, unfortunately, be platform-
3533 * dependent; AF_INET isn't, because everybody
3534 * used 4.2BSD's value, but AF_INET6 is, because
3535 * 4.2BSD didn't have a value for it (given that
3536 * IPv6 didn't exist back in the early 1980's),
3537 * and they all picked their own values.
3539 * This means that, if we're reading from a
3540 * savefile, we need to check for all the
3543 * If we're doing a live capture, we only need
3544 * to check for this platform's value; however,
3545 * Npcap uses 24, which isn't Windows's AF_INET6
3546 * value. (Given the multiple different values,
3547 * programs that read pcap files shouldn't be
3548 * checking for their platform's AF_INET6 value
3549 * anyway, they should check for all of the
3550 * possible values. and they might as well do
3551 * that even for live captures.)
3553 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3555 * Savefile - check for all three
3556 * possible IPv6 values.
3558 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3559 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3561 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3566 * Live capture, so we only need to
3567 * check for the value used on this
3572 * Npcap doesn't use Windows's AF_INET6,
3573 * as that collides with AF_IPX on
3574 * some BSDs (both have the value 23).
3575 * Instead, it uses 24.
3577 return (gen_loopback_linktype(cstate
, 24));
3580 return (gen_loopback_linktype(cstate
, AF_INET6
));
3581 #else /* AF_INET6 */
3583 * I guess this platform doesn't support
3584 * IPv6, so we just reject all packets.
3586 return gen_false(cstate
);
3587 #endif /* AF_INET6 */
3593 * Not a type on which we support filtering.
3594 * XXX - support those that have AF_ values
3595 * #defined on this platform, at least?
3597 return gen_false(cstate
);
3602 * af field is host byte order in contrast to the rest of
3605 if (ll_proto
== ETHERTYPE_IP
)
3606 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3608 else if (ll_proto
== ETHERTYPE_IPV6
)
3609 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3612 return gen_false(cstate
);
3616 case DLT_ARCNET_LINUX
:
3618 * XXX should we check for first fragment if the protocol
3624 return gen_false(cstate
);
3626 case ETHERTYPE_IPV6
:
3627 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3631 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3633 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3639 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3641 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3646 case ETHERTYPE_REVARP
:
3647 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3650 case ETHERTYPE_ATALK
:
3651 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3658 case ETHERTYPE_ATALK
:
3659 return gen_true(cstate
);
3661 return gen_false(cstate
);
3667 * XXX - assumes a 2-byte Frame Relay header with
3668 * DLCI and flags. What if the address is longer?
3674 * Check for the special NLPID for IP.
3676 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3678 case ETHERTYPE_IPV6
:
3680 * Check for the special NLPID for IPv6.
3682 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3686 * Check for several OSI protocols.
3688 * Frame Relay packets typically have an OSI
3689 * NLPID at the beginning; we check for each
3692 * What we check for is the NLPID and a frame
3693 * control field of UI, i.e. 0x03 followed
3696 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3697 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3698 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3704 return gen_false(cstate
);
3709 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3711 case DLT_JUNIPER_MFR
:
3712 case DLT_JUNIPER_MLFR
:
3713 case DLT_JUNIPER_MLPPP
:
3714 case DLT_JUNIPER_ATM1
:
3715 case DLT_JUNIPER_ATM2
:
3716 case DLT_JUNIPER_PPPOE
:
3717 case DLT_JUNIPER_PPPOE_ATM
:
3718 case DLT_JUNIPER_GGSN
:
3719 case DLT_JUNIPER_ES
:
3720 case DLT_JUNIPER_MONITOR
:
3721 case DLT_JUNIPER_SERVICES
:
3722 case DLT_JUNIPER_ETHER
:
3723 case DLT_JUNIPER_PPP
:
3724 case DLT_JUNIPER_FRELAY
:
3725 case DLT_JUNIPER_CHDLC
:
3726 case DLT_JUNIPER_VP
:
3727 case DLT_JUNIPER_ST
:
3728 case DLT_JUNIPER_ISM
:
3729 case DLT_JUNIPER_VS
:
3730 case DLT_JUNIPER_SRX_E2E
:
3731 case DLT_JUNIPER_FIBRECHANNEL
:
3732 case DLT_JUNIPER_ATM_CEMIC
:
3734 /* just lets verify the magic number for now -
3735 * on ATM we may have up to 6 different encapsulations on the wire
3736 * and need a lot of heuristics to figure out that the payload
3739 * FIXME encapsulation specific BPF_ filters
3741 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3743 case DLT_BACNET_MS_TP
:
3744 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3747 return gen_ipnet_linktype(cstate
, ll_proto
);
3749 case DLT_LINUX_IRDA
:
3750 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3753 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3756 case DLT_MTP2_WITH_PHDR
:
3757 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3760 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3763 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3765 case DLT_LINUX_LAPD
:
3766 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3768 case DLT_USB_FREEBSD
:
3770 case DLT_USB_LINUX_MMAPPED
:
3772 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3774 case DLT_BLUETOOTH_HCI_H4
:
3775 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3776 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3779 case DLT_CAN_SOCKETCAN
:
3780 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3782 case DLT_IEEE802_15_4
:
3783 case DLT_IEEE802_15_4_LINUX
:
3784 case DLT_IEEE802_15_4_NONASK_PHY
:
3785 case DLT_IEEE802_15_4_NOFCS
:
3786 case DLT_IEEE802_15_4_TAP
:
3787 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3789 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3790 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3793 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3796 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3798 case DLT_IPMB_KONTRON
:
3799 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3802 bpf_error(cstate
, "I2C link-layer type filtering not implemented");
3805 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3808 /* Using the fixed-size NFLOG header it is possible to tell only
3809 * the address family of the packet, other meaningful data is
3810 * either missing or behind TLVs.
3812 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3816 * Does this link-layer header type have a field
3817 * indicating the type of the next protocol? If
3818 * so, off_linktype.constant_part will be the offset of that
3819 * field in the packet; if not, it will be OFFSET_NOT_SET.
3821 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3823 * Yes; assume it's an Ethernet type. (If
3824 * it's not, it needs to be handled specially
3827 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3831 * No; report an error.
3833 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3834 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3842 * Check for an LLC SNAP packet with a given organization code and
3843 * protocol type; we check the entire contents of the 802.2 LLC and
3844 * snap headers, checking for DSAP and SSAP of SNAP and a control
3845 * field of 0x03 in the LLC header, and for the specified organization
3846 * code and protocol type in the SNAP header.
3848 static struct block
*
3849 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3851 u_char snapblock
[8];
3853 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3854 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3855 snapblock
[2] = 0x03; /* control = UI */
3856 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3857 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3858 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3859 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3860 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3861 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3865 * Generate code to match frames with an LLC header.
3867 static struct block
*
3868 gen_llc_internal(compiler_state_t
*cstate
)
3870 struct block
*b0
, *b1
;
3872 switch (cstate
->linktype
) {
3876 * We check for an Ethernet type field less than
3877 * 1500, which means it's an 802.3 length field.
3879 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3883 * Now check for the purported DSAP and SSAP not being
3884 * 0xFF, to rule out NetWare-over-802.3.
3886 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3893 * We check for LLC traffic.
3895 b0
= gen_atmtype_llc(cstate
);
3898 case DLT_IEEE802
: /* Token Ring */
3900 * XXX - check for LLC frames.
3902 return gen_true(cstate
);
3906 * XXX - check for LLC frames.
3908 return gen_true(cstate
);
3910 case DLT_ATM_RFC1483
:
3912 * For LLC encapsulation, these are defined to have an
3915 * For VC encapsulation, they don't, but there's no
3916 * way to check for that; the protocol used on the VC
3917 * is negotiated out of band.
3919 return gen_true(cstate
);
3921 case DLT_IEEE802_11
:
3922 case DLT_PRISM_HEADER
:
3923 case DLT_IEEE802_11_RADIO
:
3924 case DLT_IEEE802_11_RADIO_AVS
:
3927 * Check that we have a data frame.
3929 b0
= gen_check_802_11_data_frame(cstate
);
3933 bpf_error(cstate
, "'llc' not supported for %s",
3934 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3940 gen_llc(compiler_state_t
*cstate
)
3943 * Catch errors reported by us and routines below us, and return NULL
3946 if (setjmp(cstate
->top_ctx
))
3949 return gen_llc_internal(cstate
);
3953 gen_llc_i(compiler_state_t
*cstate
)
3955 struct block
*b0
, *b1
;
3959 * Catch errors reported by us and routines below us, and return NULL
3962 if (setjmp(cstate
->top_ctx
))
3966 * Check whether this is an LLC frame.
3968 b0
= gen_llc_internal(cstate
);
3971 * Load the control byte and test the low-order bit; it must
3972 * be clear for I frames.
3974 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3975 b1
= new_block(cstate
, JMP(BPF_JSET
));
3984 gen_llc_s(compiler_state_t
*cstate
)
3986 struct block
*b0
, *b1
;
3989 * Catch errors reported by us and routines below us, and return NULL
3992 if (setjmp(cstate
->top_ctx
))
3996 * Check whether this is an LLC frame.
3998 b0
= gen_llc_internal(cstate
);
4001 * Now compare the low-order 2 bit of the control byte against
4002 * the appropriate value for S frames.
4004 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4010 gen_llc_u(compiler_state_t
*cstate
)
4012 struct block
*b0
, *b1
;
4015 * Catch errors reported by us and routines below us, and return NULL
4018 if (setjmp(cstate
->top_ctx
))
4022 * Check whether this is an LLC frame.
4024 b0
= gen_llc_internal(cstate
);
4027 * Now compare the low-order 2 bit of the control byte against
4028 * the appropriate value for U frames.
4030 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4036 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4038 struct block
*b0
, *b1
;
4041 * Catch errors reported by us and routines below us, and return NULL
4044 if (setjmp(cstate
->top_ctx
))
4048 * Check whether this is an LLC frame.
4050 b0
= gen_llc_internal(cstate
);
4053 * Now check for an S frame with the appropriate type.
4055 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4061 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4063 struct block
*b0
, *b1
;
4066 * Catch errors reported by us and routines below us, and return NULL
4069 if (setjmp(cstate
->top_ctx
))
4073 * Check whether this is an LLC frame.
4075 b0
= gen_llc_internal(cstate
);
4078 * Now check for a U frame with the appropriate type.
4080 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4086 * Generate code to match a particular packet type, for link-layer types
4087 * using 802.2 LLC headers.
4089 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4090 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4092 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4093 * value, if <= ETHERMTU. We use that to determine whether to
4094 * match the DSAP or both DSAP and LSAP or to check the OUI and
4095 * protocol ID in a SNAP header.
4097 static struct block
*
4098 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4101 * XXX - handle token-ring variable-length header.
4107 case LLCSAP_NETBEUI
:
4109 * XXX - should we check both the DSAP and the
4110 * SSAP, like this, or should we check just the
4111 * DSAP, as we do for other SAP values?
4113 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4114 ((ll_proto
<< 8) | ll_proto
));
4118 * XXX - are there ever SNAP frames for IPX on
4119 * non-Ethernet 802.x networks?
4121 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4123 case ETHERTYPE_ATALK
:
4125 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4126 * SNAP packets with an organization code of
4127 * 0x080007 (Apple, for Appletalk) and a protocol
4128 * type of ETHERTYPE_ATALK (Appletalk).
4130 * XXX - check for an organization code of
4131 * encapsulated Ethernet as well?
4133 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4137 * XXX - we don't have to check for IPX 802.3
4138 * here, but should we check for the IPX Ethertype?
4140 if (ll_proto
<= ETHERMTU
) {
4142 * This is an LLC SAP value, so check
4145 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4148 * This is an Ethernet type; we assume that it's
4149 * unlikely that it'll appear in the right place
4150 * at random, and therefore check only the
4151 * location that would hold the Ethernet type
4152 * in a SNAP frame with an organization code of
4153 * 0x000000 (encapsulated Ethernet).
4155 * XXX - if we were to check for the SNAP DSAP and
4156 * LSAP, as per XXX, and were also to check for an
4157 * organization code of 0x000000 (encapsulated
4158 * Ethernet), we'd do
4160 * return gen_snap(cstate, 0x000000, ll_proto);
4162 * here; for now, we don't, as per the above.
4163 * I don't know whether it's worth the extra CPU
4164 * time to do the right check or not.
4166 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4171 static struct block
*
4172 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4173 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4175 struct block
*b0
, *b1
;
4189 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4190 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4196 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4197 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4202 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4206 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4210 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4214 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4218 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4222 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4229 b0
= gen_linktype(cstate
, ll_proto
);
4230 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4236 static struct block
*
4237 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4238 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4241 struct block
*b0
, *b1
;
4244 * Code below needs to access four separate 32-bit parts of the 128-bit
4245 * IPv6 address and mask. In some OSes this is as simple as using the
4246 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4247 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4248 * far as libpcap sees it. Hence copy the data before use to avoid
4249 * potential unaligned memory access and the associated compiler
4250 * warnings (whether genuine or not).
4252 bpf_u_int32 a
[4], m
[4];
4265 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4266 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4272 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4273 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4278 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4282 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4286 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4290 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4294 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4298 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4305 /* this order is important */
4306 memcpy(a
, addr
, sizeof(a
));
4307 memcpy(m
, mask
, sizeof(m
));
4308 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4309 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4311 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4313 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4315 b0
= gen_linktype(cstate
, ll_proto
);
4321 static struct block
*
4322 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4324 register struct block
*b0
, *b1
;
4328 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4331 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4334 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4335 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4341 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4342 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4347 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4351 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4355 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4359 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4363 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4367 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4375 * Like gen_ehostop, but for DLT_FDDI
4377 static struct block
*
4378 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4380 struct block
*b0
, *b1
;
4384 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4387 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4390 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4391 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4397 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4398 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4403 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4407 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4411 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4415 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4419 bpf_error(cstate
, "'ra' is only supported on 802.11");
4423 bpf_error(cstate
, "'ta' is only supported on 802.11");
4431 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4433 static struct block
*
4434 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4436 register struct block
*b0
, *b1
;
4440 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4443 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4446 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4447 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4453 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4454 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4459 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4463 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4467 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4471 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4475 bpf_error(cstate
, "'ra' is only supported on 802.11");
4479 bpf_error(cstate
, "'ta' is only supported on 802.11");
4487 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4488 * various 802.11 + radio headers.
4490 static struct block
*
4491 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4493 register struct block
*b0
, *b1
, *b2
;
4494 register struct slist
*s
;
4496 #ifdef ENABLE_WLAN_FILTERING_PATCH
4499 * We need to disable the optimizer because the optimizer is buggy
4500 * and wipes out some LD instructions generated by the below
4501 * code to validate the Frame Control bits
4503 cstate
->no_optimize
= 1;
4504 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4511 * For control frames, there is no SA.
4513 * For management frames, SA is at an
4514 * offset of 10 from the beginning of
4517 * For data frames, SA is at an offset
4518 * of 10 from the beginning of the packet
4519 * if From DS is clear, at an offset of
4520 * 16 from the beginning of the packet
4521 * if From DS is set and To DS is clear,
4522 * and an offset of 24 from the beginning
4523 * of the packet if From DS is set and To DS
4528 * Generate the tests to be done for data frames
4531 * First, check for To DS set, i.e. check "link[1] & 0x01".
4533 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4534 b1
= new_block(cstate
, JMP(BPF_JSET
));
4535 b1
->s
.k
= 0x01; /* To DS */
4539 * If To DS is set, the SA is at 24.
4541 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4545 * Now, check for To DS not set, i.e. check
4546 * "!(link[1] & 0x01)".
4548 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4549 b2
= new_block(cstate
, JMP(BPF_JSET
));
4550 b2
->s
.k
= 0x01; /* To DS */
4555 * If To DS is not set, the SA is at 16.
4557 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4561 * Now OR together the last two checks. That gives
4562 * the complete set of checks for data frames with
4568 * Now check for From DS being set, and AND that with
4569 * the ORed-together checks.
4571 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4572 b1
= new_block(cstate
, JMP(BPF_JSET
));
4573 b1
->s
.k
= 0x02; /* From DS */
4578 * Now check for data frames with From DS not set.
4580 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4581 b2
= new_block(cstate
, JMP(BPF_JSET
));
4582 b2
->s
.k
= 0x02; /* From DS */
4587 * If From DS isn't set, the SA is at 10.
4589 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4593 * Now OR together the checks for data frames with
4594 * From DS not set and for data frames with From DS
4595 * set; that gives the checks done for data frames.
4600 * Now check for a data frame.
4601 * I.e, check "link[0] & 0x08".
4603 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4604 b1
= new_block(cstate
, JMP(BPF_JSET
));
4609 * AND that with the checks done for data frames.
4614 * If the high-order bit of the type value is 0, this
4615 * is a management frame.
4616 * I.e, check "!(link[0] & 0x08)".
4618 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4619 b2
= new_block(cstate
, JMP(BPF_JSET
));
4625 * For management frames, the SA is at 10.
4627 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4631 * OR that with the checks done for data frames.
4632 * That gives the checks done for management and
4638 * If the low-order bit of the type value is 1,
4639 * this is either a control frame or a frame
4640 * with a reserved type, and thus not a
4643 * I.e., check "!(link[0] & 0x04)".
4645 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4646 b1
= new_block(cstate
, JMP(BPF_JSET
));
4652 * AND that with the checks for data and management
4662 * For control frames, there is no DA.
4664 * For management frames, DA is at an
4665 * offset of 4 from the beginning of
4668 * For data frames, DA is at an offset
4669 * of 4 from the beginning of the packet
4670 * if To DS is clear and at an offset of
4671 * 16 from the beginning of the packet
4676 * Generate the tests to be done for data frames.
4678 * First, check for To DS set, i.e. "link[1] & 0x01".
4680 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4681 b1
= new_block(cstate
, JMP(BPF_JSET
));
4682 b1
->s
.k
= 0x01; /* To DS */
4686 * If To DS is set, the DA is at 16.
4688 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4692 * Now, check for To DS not set, i.e. check
4693 * "!(link[1] & 0x01)".
4695 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4696 b2
= new_block(cstate
, JMP(BPF_JSET
));
4697 b2
->s
.k
= 0x01; /* To DS */
4702 * If To DS is not set, the DA is at 4.
4704 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4708 * Now OR together the last two checks. That gives
4709 * the complete set of checks for data frames.
4714 * Now check for a data frame.
4715 * I.e, check "link[0] & 0x08".
4717 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4718 b1
= new_block(cstate
, JMP(BPF_JSET
));
4723 * AND that with the checks done for data frames.
4728 * If the high-order bit of the type value is 0, this
4729 * is a management frame.
4730 * I.e, check "!(link[0] & 0x08)".
4732 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4733 b2
= new_block(cstate
, JMP(BPF_JSET
));
4739 * For management frames, the DA is at 4.
4741 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4745 * OR that with the checks done for data frames.
4746 * That gives the checks done for management and
4752 * If the low-order bit of the type value is 1,
4753 * this is either a control frame or a frame
4754 * with a reserved type, and thus not a
4757 * I.e., check "!(link[0] & 0x04)".
4759 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4760 b1
= new_block(cstate
, JMP(BPF_JSET
));
4766 * AND that with the checks for data and management
4773 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4774 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4780 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4781 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4786 * XXX - add BSSID keyword?
4789 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4793 * Not present in CTS or ACK control frames.
4795 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4796 IEEE80211_FC0_TYPE_MASK
);
4798 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4799 IEEE80211_FC0_SUBTYPE_MASK
);
4801 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4802 IEEE80211_FC0_SUBTYPE_MASK
);
4806 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4812 * Not present in control frames.
4814 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4815 IEEE80211_FC0_TYPE_MASK
);
4817 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4823 * Present only if the direction mask has both "From DS"
4824 * and "To DS" set. Neither control frames nor management
4825 * frames should have both of those set, so we don't
4826 * check the frame type.
4828 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4829 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4830 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4836 * Not present in management frames; addr1 in other
4841 * If the high-order bit of the type value is 0, this
4842 * is a management frame.
4843 * I.e, check "(link[0] & 0x08)".
4845 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4846 b1
= new_block(cstate
, JMP(BPF_JSET
));
4853 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4856 * AND that with the check of addr1.
4863 * Not present in management frames; addr2, if present,
4868 * Not present in CTS or ACK control frames.
4870 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4871 IEEE80211_FC0_TYPE_MASK
);
4873 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4874 IEEE80211_FC0_SUBTYPE_MASK
);
4876 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4877 IEEE80211_FC0_SUBTYPE_MASK
);
4883 * If the high-order bit of the type value is 0, this
4884 * is a management frame.
4885 * I.e, check "(link[0] & 0x08)".
4887 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4888 b1
= new_block(cstate
, JMP(BPF_JSET
));
4893 * AND that with the check for frames other than
4894 * CTS and ACK frames.
4901 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4910 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4911 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4912 * as the RFC states.)
4914 static struct block
*
4915 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4917 register struct block
*b0
, *b1
;
4921 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4924 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4927 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4928 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4934 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4935 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4940 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4944 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4948 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4952 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4956 bpf_error(cstate
, "'ra' is only supported on 802.11");
4960 bpf_error(cstate
, "'ta' is only supported on 802.11");
4968 * This is quite tricky because there may be pad bytes in front of the
4969 * DECNET header, and then there are two possible data packet formats that
4970 * carry both src and dst addresses, plus 5 packet types in a format that
4971 * carries only the src node, plus 2 types that use a different format and
4972 * also carry just the src node.
4976 * Instead of doing those all right, we just look for data packets with
4977 * 0 or 1 bytes of padding. If you want to look at other packets, that
4978 * will require a lot more hacking.
4980 * To add support for filtering on DECNET "areas" (network numbers)
4981 * one would want to add a "mask" argument to this routine. That would
4982 * make the filter even more inefficient, although one could be clever
4983 * and not generate masking instructions if the mask is 0xFFFF.
4985 static struct block
*
4986 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4988 struct block
*b0
, *b1
, *b2
, *tmp
;
4989 u_int offset_lh
; /* offset if long header is received */
4990 u_int offset_sh
; /* offset if short header is received */
4995 offset_sh
= 1; /* follows flags */
4996 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5000 offset_sh
= 3; /* follows flags, dstnode */
5001 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5005 /* Inefficient because we do our Calvinball dance twice */
5006 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5007 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5013 /* Inefficient because we do our Calvinball dance twice */
5014 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5015 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5020 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5024 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5028 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5032 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5036 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5040 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5048 * In a DECnet message inside an Ethernet frame the first two bytes
5049 * immediately after EtherType are the [litle-endian] DECnet message
5050 * length, which is irrelevant in this context.
5052 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5053 * 8-bit bitmap of the optional padding before the packet route header.
5054 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5055 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5056 * means there aren't any PAD bytes after the bitmap, so the header
5057 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5058 * is set to 0, thus the header begins at the third byte.
5060 * The header can be in several (as mentioned above) formats, all of
5061 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5062 * (PF, "pad field") set to 0 regardless of any padding present before
5063 * the header. "Short header" means bits 0-2 of the bitmap encode the
5064 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5066 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5067 * values and the masks, this maps to the required single bytes of
5068 * the message correctly on both big-endian and little-endian hosts.
5069 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5070 * because the wire encoding is little-endian and BPF multiple-byte
5071 * loads are big-endian. When the destination address is near enough
5072 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5075 /* Check for pad = 1, long header case */
5076 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5077 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5078 BPF_H
, SWAPSHORT(addr
));
5080 /* Check for pad = 0, long header case */
5081 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5082 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5086 /* Check for pad = 1, short header case */
5088 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5089 0x81020000U
| SWAPSHORT(addr
),
5092 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5093 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5098 /* Check for pad = 0, short header case */
5100 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5101 0x02000000U
| SWAPSHORT(addr
) << 8,
5104 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5105 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5115 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5116 * test the bottom-of-stack bit, and then check the version number
5117 * field in the IP header.
5119 static struct block
*
5120 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5122 struct block
*b0
, *b1
;
5127 /* match the bottom-of-stack bit */
5128 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5129 /* match the IPv4 version number */
5130 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5134 case ETHERTYPE_IPV6
:
5135 /* match the bottom-of-stack bit */
5136 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5137 /* match the IPv4 version number */
5138 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5143 /* FIXME add other L3 proto IDs */
5144 bpf_error(cstate
, "unsupported protocol over mpls");
5149 static struct block
*
5150 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5151 int proto
, int dir
, int type
)
5153 struct block
*b0
, *b1
;
5154 const char *typestr
;
5164 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5166 * Only check for non-IPv4 addresses if we're not
5167 * checking MPLS-encapsulated packets.
5169 if (cstate
->label_stack_depth
== 0) {
5170 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5172 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5178 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5181 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
5184 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
5187 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
5190 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5193 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5196 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5199 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5202 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5205 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5208 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5211 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5212 b1
= gen_dnhostop(cstate
, addr
, dir
);
5217 bpf_error(cstate
, "LAT host filtering not implemented");
5220 bpf_error(cstate
, "SCA host filtering not implemented");
5223 bpf_error(cstate
, "MOPRC host filtering not implemented");
5226 bpf_error(cstate
, "MOPDL host filtering not implemented");
5229 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5232 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5235 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5238 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5241 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5244 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5247 bpf_error(cstate
, "AARP host filtering not implemented");
5250 bpf_error(cstate
, "ISO host filtering not implemented");
5253 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5256 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5259 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5262 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5265 bpf_error(cstate
, "IPX host filtering not implemented");
5268 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5271 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5274 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5277 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5280 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5283 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5286 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5289 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5292 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5295 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5304 static struct block
*
5305 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5306 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5308 const char *typestr
;
5318 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5321 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5324 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5327 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5330 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5333 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5336 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5339 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5342 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5345 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5348 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5351 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5354 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5357 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5360 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5363 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5366 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5369 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5372 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5375 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5378 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5381 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5384 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5387 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5390 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5393 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5396 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5399 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5402 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5405 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5408 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5411 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5414 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5417 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5420 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5423 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5426 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5429 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5432 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5435 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5445 static struct block
*
5446 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5447 struct addrinfo
*alist
, int proto
, int dir
)
5449 struct block
*b0
, *b1
, *tmp
;
5450 struct addrinfo
*ai
;
5451 struct sockaddr_in
*sin
;
5454 bpf_error(cstate
, "direction applied to 'gateway'");
5461 switch (cstate
->linktype
) {
5463 case DLT_NETANALYZER
:
5464 case DLT_NETANALYZER_TRANSPARENT
:
5465 b1
= gen_prevlinkhdr_check(cstate
);
5466 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5471 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5474 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5476 case DLT_IEEE802_11
:
5477 case DLT_PRISM_HEADER
:
5478 case DLT_IEEE802_11_RADIO_AVS
:
5479 case DLT_IEEE802_11_RADIO
:
5481 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5485 * This is LLC-multiplexed traffic; if it were
5486 * LANE, cstate->linktype would have been set to
5490 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5491 case DLT_IP_OVER_FC
:
5492 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5496 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5499 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5501 * Does it have an address?
5503 if (ai
->ai_addr
!= NULL
) {
5505 * Yes. Is it an IPv4 address?
5507 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5509 * Generate an entry for it.
5511 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5512 tmp
= gen_host(cstate
,
5513 ntohl(sin
->sin_addr
.s_addr
),
5514 0xffffffff, proto
, Q_OR
, Q_HOST
);
5516 * Is it the *first* IPv4 address?
5520 * Yes, so start with it.
5525 * No, so OR it into the
5537 * No IPv4 addresses found.
5545 bpf_error(cstate
, "illegal modifier of 'gateway'");
5550 static struct block
*
5551 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5559 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5563 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5567 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5571 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5574 #ifndef IPPROTO_IGMP
5575 #define IPPROTO_IGMP 2
5579 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5582 #ifndef IPPROTO_IGRP
5583 #define IPPROTO_IGRP 9
5586 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5590 #define IPPROTO_PIM 103
5594 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5597 #ifndef IPPROTO_VRRP
5598 #define IPPROTO_VRRP 112
5602 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5605 #ifndef IPPROTO_CARP
5606 #define IPPROTO_CARP 112
5610 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5614 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5618 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5622 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5626 bpf_error(cstate
, "link layer applied in wrong context");
5629 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5633 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5637 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5641 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5645 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5649 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5653 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5657 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5660 #ifndef IPPROTO_ICMPV6
5661 #define IPPROTO_ICMPV6 58
5664 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5668 #define IPPROTO_AH 51
5671 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5675 #define IPPROTO_ESP 50
5678 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5682 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5686 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5690 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5693 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5694 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5695 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5697 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5699 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5701 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5705 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5706 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5707 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5709 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5711 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5713 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5717 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5718 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5719 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5721 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5726 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5727 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5732 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5733 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5735 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5737 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5742 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5743 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5748 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5749 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5754 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5758 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5762 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5766 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5770 bpf_error(cstate
, "'radio' is not a valid protocol type");
5779 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5782 * Catch errors reported by us and routines below us, and return NULL
5785 if (setjmp(cstate
->top_ctx
))
5788 return gen_proto_abbrev_internal(cstate
, proto
);
5791 static struct block
*
5792 gen_ipfrag(compiler_state_t
*cstate
)
5797 /* not IPv4 frag other than the first frag */
5798 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5799 b
= new_block(cstate
, JMP(BPF_JSET
));
5808 * Generate a comparison to a port value in the transport-layer header
5809 * at the specified offset from the beginning of that header.
5811 * XXX - this handles a variable-length prefix preceding the link-layer
5812 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5813 * variable-length link-layer headers (such as Token Ring or 802.11
5816 static struct block
*
5817 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5819 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5822 static struct block
*
5823 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5825 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5828 static struct block
*
5829 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5831 struct block
*b0
, *b1
, *tmp
;
5833 /* ip proto 'proto' and not a fragment other than the first fragment */
5834 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5835 b0
= gen_ipfrag(cstate
);
5840 b1
= gen_portatom(cstate
, 0, port
);
5844 b1
= gen_portatom(cstate
, 2, port
);
5848 tmp
= gen_portatom(cstate
, 0, port
);
5849 b1
= gen_portatom(cstate
, 2, port
);
5855 tmp
= gen_portatom(cstate
, 0, port
);
5856 b1
= gen_portatom(cstate
, 2, port
);
5861 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5865 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5869 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5873 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5877 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5881 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5893 static struct block
*
5894 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5896 struct block
*b0
, *b1
, *tmp
;
5901 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5902 * not LLC encapsulation with LLCSAP_IP.
5904 * For IEEE 802 networks - which includes 802.5 token ring
5905 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5906 * says that SNAP encapsulation is used, not LLC encapsulation
5909 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5910 * RFC 2225 say that SNAP encapsulation is used, not LLC
5911 * encapsulation with LLCSAP_IP.
5913 * So we always check for ETHERTYPE_IP.
5915 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5921 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5925 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5926 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5928 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5940 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5942 struct block
*b0
, *b1
, *tmp
;
5944 /* ip6 proto 'proto' */
5945 /* XXX - catch the first fragment of a fragmented packet? */
5946 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5950 b1
= gen_portatom6(cstate
, 0, port
);
5954 b1
= gen_portatom6(cstate
, 2, port
);
5958 tmp
= gen_portatom6(cstate
, 0, port
);
5959 b1
= gen_portatom6(cstate
, 2, port
);
5965 tmp
= gen_portatom6(cstate
, 0, port
);
5966 b1
= gen_portatom6(cstate
, 2, port
);
5978 static struct block
*
5979 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5981 struct block
*b0
, *b1
, *tmp
;
5983 /* link proto ip6 */
5984 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5990 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5994 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5995 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5997 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
6008 /* gen_portrange code */
6009 static struct block
*
6010 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6013 struct block
*b1
, *b2
;
6017 * Reverse the order of the ports, so v1 is the lower one.
6026 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
6027 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
6034 static struct block
*
6035 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6036 bpf_u_int32 proto
, int dir
)
6038 struct block
*b0
, *b1
, *tmp
;
6040 /* ip proto 'proto' and not a fragment other than the first fragment */
6041 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
6042 b0
= gen_ipfrag(cstate
);
6047 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6051 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6055 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6056 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6062 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6063 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6068 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6072 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6076 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6080 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6084 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
6088 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
6100 static struct block
*
6101 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6104 struct block
*b0
, *b1
, *tmp
;
6107 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6113 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6118 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6119 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6121 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6132 static struct block
*
6133 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6136 struct block
*b1
, *b2
;
6140 * Reverse the order of the ports, so v1 is the lower one.
6149 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6150 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6157 static struct block
*
6158 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6159 bpf_u_int32 proto
, int dir
)
6161 struct block
*b0
, *b1
, *tmp
;
6163 /* ip6 proto 'proto' */
6164 /* XXX - catch the first fragment of a fragmented packet? */
6165 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6169 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6173 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6177 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6178 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6184 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6185 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6197 static struct block
*
6198 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6201 struct block
*b0
, *b1
, *tmp
;
6203 /* link proto ip6 */
6204 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6210 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6215 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6216 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6218 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6230 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6239 v
= pcap_nametoproto(name
);
6240 if (v
== PROTO_UNDEF
)
6241 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6245 /* XXX should look up h/w protocol type based on cstate->linktype */
6246 v
= pcap_nametoeproto(name
);
6247 if (v
== PROTO_UNDEF
) {
6248 v
= pcap_nametollc(name
);
6249 if (v
== PROTO_UNDEF
)
6250 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6255 if (strcmp(name
, "esis") == 0)
6257 else if (strcmp(name
, "isis") == 0)
6259 else if (strcmp(name
, "clnp") == 0)
6262 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6272 #if !defined(NO_PROTOCHAIN)
6273 static struct block
*
6274 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6276 struct block
*b0
, *b
;
6277 struct slist
*s
[100];
6278 int fix2
, fix3
, fix4
, fix5
;
6279 int ahcheck
, again
, end
;
6281 int reg2
= alloc_reg(cstate
);
6283 memset(s
, 0, sizeof(s
));
6284 fix3
= fix4
= fix5
= 0;
6291 b0
= gen_protochain(cstate
, v
, Q_IP
);
6292 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6296 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6301 * We don't handle variable-length prefixes before the link-layer
6302 * header, or variable-length link-layer headers, here yet.
6303 * We might want to add BPF instructions to do the protochain
6304 * work, to simplify that and, on platforms that have a BPF
6305 * interpreter with the new instructions, let the filtering
6306 * be done in the kernel. (We already require a modified BPF
6307 * engine to do the protochain stuff, to support backward
6308 * branches, and backward branch support is unlikely to appear
6309 * in kernel BPF engines.)
6311 if (cstate
->off_linkpl
.is_variable
)
6312 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6315 * To quote a comment in optimize.c:
6317 * "These data structures are used in a Cocke and Schwartz style
6318 * value numbering scheme. Since the flowgraph is acyclic,
6319 * exit values can be propagated from a node's predecessors
6320 * provided it is uniquely defined."
6322 * "Acyclic" means "no backward branches", which means "no
6323 * loops", so we have to turn the optimizer off.
6325 cstate
->no_optimize
= 1;
6328 * s[0] is a dummy entry to protect other BPF insn from damage
6329 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6330 * hard to find interdependency made by jump table fixup.
6333 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6338 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6341 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6342 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6344 /* X = ip->ip_hl << 2 */
6345 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6346 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6351 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6353 /* A = ip6->ip_nxt */
6354 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6355 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6357 /* X = sizeof(struct ip6_hdr) */
6358 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6364 bpf_error(cstate
, "unsupported proto to gen_protochain");
6368 /* again: if (A == v) goto end; else fall through; */
6370 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6372 s
[i
]->s
.jt
= NULL
; /*later*/
6373 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6377 #ifndef IPPROTO_NONE
6378 #define IPPROTO_NONE 59
6380 /* if (A == IPPROTO_NONE) goto end */
6381 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6382 s
[i
]->s
.jt
= NULL
; /*later*/
6383 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6384 s
[i
]->s
.k
= IPPROTO_NONE
;
6385 s
[fix5
]->s
.jf
= s
[i
];
6389 if (proto
== Q_IPV6
) {
6390 int v6start
, v6end
, v6advance
, j
;
6393 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6394 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6395 s
[i
]->s
.jt
= NULL
; /*later*/
6396 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6397 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6398 s
[fix2
]->s
.jf
= s
[i
];
6400 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6401 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6402 s
[i
]->s
.jt
= NULL
; /*later*/
6403 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6404 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6406 /* if (A == IPPROTO_ROUTING) goto v6advance */
6407 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6408 s
[i
]->s
.jt
= NULL
; /*later*/
6409 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6410 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6412 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6413 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6414 s
[i
]->s
.jt
= NULL
; /*later*/
6415 s
[i
]->s
.jf
= NULL
; /*later*/
6416 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6426 * A = P[X + packet head];
6427 * X = X + (P[X + packet head + 1] + 1) * 8;
6429 /* A = P[X + packet head] */
6430 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6431 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6434 s
[i
] = new_stmt(cstate
, BPF_ST
);
6437 /* A = P[X + packet head + 1]; */
6438 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6439 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6442 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6446 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6450 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6454 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6457 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6461 /* goto again; (must use BPF_JA for backward jump) */
6462 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6463 s
[i
]->s
.k
= again
- i
- 1;
6464 s
[i
- 1]->s
.jf
= s
[i
];
6468 for (j
= v6start
; j
<= v6end
; j
++)
6469 s
[j
]->s
.jt
= s
[v6advance
];
6472 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6474 s
[fix2
]->s
.jf
= s
[i
];
6480 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6481 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6482 s
[i
]->s
.jt
= NULL
; /*later*/
6483 s
[i
]->s
.jf
= NULL
; /*later*/
6484 s
[i
]->s
.k
= IPPROTO_AH
;
6486 s
[fix3
]->s
.jf
= s
[ahcheck
];
6493 * X = X + (P[X + 1] + 2) * 4;
6496 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6498 /* A = P[X + packet head]; */
6499 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6500 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6503 s
[i
] = new_stmt(cstate
, BPF_ST
);
6507 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6510 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6514 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6516 /* A = P[X + packet head] */
6517 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6518 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6521 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6525 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6529 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6532 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6536 /* goto again; (must use BPF_JA for backward jump) */
6537 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6538 s
[i
]->s
.k
= again
- i
- 1;
6543 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6545 s
[fix2
]->s
.jt
= s
[end
];
6546 s
[fix4
]->s
.jf
= s
[end
];
6547 s
[fix5
]->s
.jt
= s
[end
];
6554 for (i
= 0; i
< max
- 1; i
++)
6555 s
[i
]->next
= s
[i
+ 1];
6556 s
[max
- 1]->next
= NULL
;
6561 b
= new_block(cstate
, JMP(BPF_JEQ
));
6562 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6565 free_reg(cstate
, reg2
);
6570 #endif /* !defined(NO_PROTOCHAIN) */
6572 static struct block
*
6573 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6576 struct block
*b0
, *b1
;
6579 * A data frame has the 0x08 bit (b3) in the frame control field set
6580 * and the 0x04 bit (b2) clear.
6582 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6583 b0
= new_block(cstate
, JMP(BPF_JSET
));
6587 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6588 b1
= new_block(cstate
, JMP(BPF_JSET
));
6599 * Generate code that checks whether the packet is a packet for protocol
6600 * <proto> and whether the type field in that protocol's header has
6601 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6602 * IP packet and checks the protocol number in the IP header against <v>.
6604 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6605 * against Q_IP and Q_IPV6.
6607 static struct block
*
6608 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6610 struct block
*b0
, *b1
;
6613 if (dir
!= Q_DEFAULT
)
6614 bpf_error(cstate
, "direction applied to 'proto'");
6618 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6619 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6624 return gen_linktype(cstate
, v
);
6628 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6629 * not LLC encapsulation with LLCSAP_IP.
6631 * For IEEE 802 networks - which includes 802.5 token ring
6632 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6633 * says that SNAP encapsulation is used, not LLC encapsulation
6636 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6637 * RFC 2225 say that SNAP encapsulation is used, not LLC
6638 * encapsulation with LLCSAP_IP.
6640 * So we always check for ETHERTYPE_IP.
6642 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6643 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6648 bpf_error(cstate
, "arp does not encapsulate another protocol");
6652 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6656 bpf_error(cstate
, "'sctp proto' is bogus");
6660 bpf_error(cstate
, "'tcp proto' is bogus");
6664 bpf_error(cstate
, "'udp proto' is bogus");
6668 bpf_error(cstate
, "'icmp proto' is bogus");
6672 bpf_error(cstate
, "'igmp proto' is bogus");
6676 bpf_error(cstate
, "'igrp proto' is bogus");
6680 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6684 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6688 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6692 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6696 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6700 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6704 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6706 * Also check for a fragment header before the final
6709 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6710 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6712 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6718 bpf_error(cstate
, "'icmp6 proto' is bogus");
6722 bpf_error(cstate
, "'ah proto' is bogus");
6726 bpf_error(cstate
, "'esp proto' is bogus");
6730 bpf_error(cstate
, "'pim proto' is bogus");
6734 bpf_error(cstate
, "'vrrp proto' is bogus");
6738 bpf_error(cstate
, "'aarp proto' is bogus");
6742 switch (cstate
->linktype
) {
6746 * Frame Relay packets typically have an OSI
6747 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6748 * generates code to check for all the OSI
6749 * NLPIDs, so calling it and then adding a check
6750 * for the particular NLPID for which we're
6751 * looking is bogus, as we can just check for
6754 * What we check for is the NLPID and a frame
6755 * control field value of UI, i.e. 0x03 followed
6758 * XXX - assumes a 2-byte Frame Relay header with
6759 * DLCI and flags. What if the address is longer?
6761 * XXX - what about SNAP-encapsulated frames?
6763 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6769 * Cisco uses an Ethertype lookalike - for OSI,
6772 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6773 /* OSI in C-HDLC is stuffed with a fudge byte */
6774 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6779 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6780 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6786 bpf_error(cstate
, "'esis proto' is bogus");
6790 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6792 * 4 is the offset of the PDU type relative to the IS-IS
6795 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6800 bpf_error(cstate
, "'clnp proto' is not supported");
6804 bpf_error(cstate
, "'stp proto' is bogus");
6808 bpf_error(cstate
, "'ipx proto' is bogus");
6812 bpf_error(cstate
, "'netbeui proto' is bogus");
6816 bpf_error(cstate
, "'l1 proto' is bogus");
6820 bpf_error(cstate
, "'l2 proto' is bogus");
6824 bpf_error(cstate
, "'iih proto' is bogus");
6828 bpf_error(cstate
, "'snp proto' is bogus");
6832 bpf_error(cstate
, "'csnp proto' is bogus");
6836 bpf_error(cstate
, "'psnp proto' is bogus");
6840 bpf_error(cstate
, "'lsp proto' is bogus");
6844 bpf_error(cstate
, "'radio proto' is bogus");
6848 bpf_error(cstate
, "'carp proto' is bogus");
6859 * Convert a non-numeric name to a port number.
6862 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6864 struct addrinfo hints
, *res
, *ai
;
6866 struct sockaddr_in
*in4
;
6868 struct sockaddr_in6
*in6
;
6873 * We check for both TCP and UDP in case there are
6874 * ambiguous entries.
6876 memset(&hints
, 0, sizeof(hints
));
6877 hints
.ai_family
= PF_UNSPEC
;
6878 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6879 hints
.ai_protocol
= ipproto
;
6880 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6887 * No such port. Just return -1.
6894 * We don't use strerror() because it's not
6895 * guaranteed to be thread-safe on all platforms
6896 * (probably because it might use a non-thread-local
6897 * buffer into which to format an error message
6898 * if the error code isn't one for which it has
6899 * a canned string; three cheers for C string
6902 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6904 port
= -2; /* a real error */
6910 * This is a real error, not just "there's
6911 * no such service name".
6913 * We don't use gai_strerror() because it's not
6914 * guaranteed to be thread-safe on all platforms
6915 * (probably because it might use a non-thread-local
6916 * buffer into which to format an error message
6917 * if the error code isn't one for which it has
6918 * a canned string; three cheers for C string
6921 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6923 port
= -2; /* a real error */
6928 * OK, we found it. Did it find anything?
6930 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6932 * Does it have an address?
6934 if (ai
->ai_addr
!= NULL
) {
6936 * Yes. Get a port number; we're done.
6938 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6939 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6940 port
= ntohs(in4
->sin_port
);
6944 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6945 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6946 port
= ntohs(in6
->sin6_port
);
6958 * Convert a string to a port number.
6961 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6971 * See if it's a number.
6973 ret
= stoulen(string
, string_size
, &val
, cstate
);
6977 /* Unknown port type - it's just a number. */
6978 *proto
= PROTO_UNDEF
;
6981 case STOULEN_NOT_OCTAL_NUMBER
:
6982 case STOULEN_NOT_HEX_NUMBER
:
6983 case STOULEN_NOT_DECIMAL_NUMBER
:
6985 * Not a valid number; try looking it up as a port.
6987 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6988 memcpy(cpy
, string
, string_size
);
6989 cpy
[string_size
] = '\0';
6990 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6991 if (tcp_port
== -2) {
6993 * We got a hard error; the error string has
6997 longjmp(cstate
->top_ctx
, 1);
7000 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
7001 if (udp_port
== -2) {
7003 * We got a hard error; the error string has
7007 longjmp(cstate
->top_ctx
, 1);
7012 * We need to check /etc/services for ambiguous entries.
7013 * If we find an ambiguous entry, and it has the
7014 * same port number, change the proto to PROTO_UNDEF
7015 * so both TCP and UDP will be checked.
7017 if (tcp_port
>= 0) {
7018 val
= (bpf_u_int32
)tcp_port
;
7019 *proto
= IPPROTO_TCP
;
7020 if (udp_port
>= 0) {
7021 if (udp_port
== tcp_port
)
7022 *proto
= PROTO_UNDEF
;
7025 /* Can't handle ambiguous names that refer
7026 to different port numbers. */
7027 warning("ambiguous port %s in /etc/services",
7034 if (udp_port
>= 0) {
7035 val
= (bpf_u_int32
)udp_port
;
7036 *proto
= IPPROTO_UDP
;
7040 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
7042 longjmp(cstate
->top_ctx
, 1);
7049 /* Error already set. */
7050 longjmp(cstate
->top_ctx
, 1);
7057 /* Should not happen */
7058 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
7059 longjmp(cstate
->top_ctx
, 1);
7066 * Convert a string in the form PPP-PPP, which correspond to ports, to
7067 * a starting and ending port in a port range.
7070 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
7071 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
7074 const char *first
, *second
;
7075 size_t first_size
, second_size
;
7078 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
7079 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
7082 * Make sure there are no other hyphens.
7084 * XXX - we support named ports, but there are some port names
7085 * in /etc/services that include hyphens, so this would rule
7088 if (strchr(hyphen_off
+ 1, '-') != NULL
)
7089 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
7093 * Get the length of the first port.
7096 first_size
= hyphen_off
- string
;
7097 if (first_size
== 0) {
7098 /* Range of "-port", which we don't support. */
7099 bpf_error(cstate
, "port range '%s' has no starting port", string
);
7103 * Try to convert it to a port.
7105 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
7106 save_proto
= *proto
;
7109 * Get the length of the second port.
7111 second
= hyphen_off
+ 1;
7112 second_size
= strlen(second
);
7113 if (second_size
== 0) {
7114 /* Range of "port-", which we don't support. */
7115 bpf_error(cstate
, "port range '%s' has no ending port", string
);
7119 * Try to convert it to a port.
7121 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
7122 if (*proto
!= save_proto
)
7123 *proto
= PROTO_UNDEF
;
7127 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
7129 int proto
= q
.proto
;
7133 bpf_u_int32 mask
, addr
;
7134 struct addrinfo
*res
, *res0
;
7135 struct sockaddr_in
*sin4
;
7138 struct sockaddr_in6
*sin6
;
7139 struct in6_addr mask128
;
7141 struct block
*b
, *tmp
;
7142 int port
, real_proto
;
7143 bpf_u_int32 port1
, port2
;
7146 * Catch errors reported by us and routines below us, and return NULL
7149 if (setjmp(cstate
->top_ctx
))
7155 addr
= pcap_nametonetaddr(name
);
7157 bpf_error(cstate
, "unknown network '%s'", name
);
7158 /* Left justify network addr and calculate its network mask */
7160 while (addr
&& (addr
& 0xff000000) == 0) {
7164 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7168 if (proto
== Q_LINK
) {
7169 switch (cstate
->linktype
) {
7172 case DLT_NETANALYZER
:
7173 case DLT_NETANALYZER_TRANSPARENT
:
7174 eaddr
= pcap_ether_hostton(name
);
7177 "unknown ether host '%s'", name
);
7178 tmp
= gen_prevlinkhdr_check(cstate
);
7179 b
= gen_ehostop(cstate
, eaddr
, dir
);
7186 eaddr
= pcap_ether_hostton(name
);
7189 "unknown FDDI host '%s'", name
);
7190 b
= gen_fhostop(cstate
, eaddr
, dir
);
7195 eaddr
= pcap_ether_hostton(name
);
7198 "unknown token ring host '%s'", name
);
7199 b
= gen_thostop(cstate
, eaddr
, dir
);
7203 case DLT_IEEE802_11
:
7204 case DLT_PRISM_HEADER
:
7205 case DLT_IEEE802_11_RADIO_AVS
:
7206 case DLT_IEEE802_11_RADIO
:
7208 eaddr
= pcap_ether_hostton(name
);
7211 "unknown 802.11 host '%s'", name
);
7212 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7216 case DLT_IP_OVER_FC
:
7217 eaddr
= pcap_ether_hostton(name
);
7220 "unknown Fibre Channel host '%s'", name
);
7221 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7226 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7227 } else if (proto
== Q_DECNET
) {
7229 * A long time ago on Ultrix libpcap supported
7230 * translation of DECnet host names into DECnet
7231 * addresses, but this feature is history now.
7233 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7236 memset(&mask128
, 0xff, sizeof(mask128
));
7238 res0
= res
= pcap_nametoaddrinfo(name
);
7240 bpf_error(cstate
, "unknown host '%s'", name
);
7247 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7248 tproto
== Q_DEFAULT
) {
7254 for (res
= res0
; res
; res
= res
->ai_next
) {
7255 switch (res
->ai_family
) {
7258 if (tproto
== Q_IPV6
)
7262 sin4
= (struct sockaddr_in
*)
7264 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7265 0xffffffff, tproto
, dir
, q
.addr
);
7269 if (tproto6
== Q_IP
)
7272 sin6
= (struct sockaddr_in6
*)
7274 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7275 &mask128
, tproto6
, dir
, q
.addr
);
7288 bpf_error(cstate
, "unknown host '%s'%s", name
,
7289 (proto
== Q_DEFAULT
)
7291 : " for specified address family");
7297 if (proto
!= Q_DEFAULT
&&
7298 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7299 bpf_error(cstate
, "illegal qualifier of 'port'");
7300 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7301 bpf_error(cstate
, "unknown port '%s'", name
);
7302 if (proto
== Q_UDP
) {
7303 if (real_proto
== IPPROTO_TCP
)
7304 bpf_error(cstate
, "port '%s' is tcp", name
);
7305 else if (real_proto
== IPPROTO_SCTP
)
7306 bpf_error(cstate
, "port '%s' is sctp", name
);
7308 /* override PROTO_UNDEF */
7309 real_proto
= IPPROTO_UDP
;
7311 if (proto
== Q_TCP
) {
7312 if (real_proto
== IPPROTO_UDP
)
7313 bpf_error(cstate
, "port '%s' is udp", name
);
7315 else if (real_proto
== IPPROTO_SCTP
)
7316 bpf_error(cstate
, "port '%s' is sctp", name
);
7318 /* override PROTO_UNDEF */
7319 real_proto
= IPPROTO_TCP
;
7321 if (proto
== Q_SCTP
) {
7322 if (real_proto
== IPPROTO_UDP
)
7323 bpf_error(cstate
, "port '%s' is udp", name
);
7325 else if (real_proto
== IPPROTO_TCP
)
7326 bpf_error(cstate
, "port '%s' is tcp", name
);
7328 /* override PROTO_UNDEF */
7329 real_proto
= IPPROTO_SCTP
;
7332 bpf_error(cstate
, "illegal port number %d < 0", port
);
7334 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7335 b
= gen_port(cstate
, port
, real_proto
, dir
);
7336 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7340 if (proto
!= Q_DEFAULT
&&
7341 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7342 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7343 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7344 if (proto
== Q_UDP
) {
7345 if (real_proto
== IPPROTO_TCP
)
7346 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7347 else if (real_proto
== IPPROTO_SCTP
)
7348 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7350 /* override PROTO_UNDEF */
7351 real_proto
= IPPROTO_UDP
;
7353 if (proto
== Q_TCP
) {
7354 if (real_proto
== IPPROTO_UDP
)
7355 bpf_error(cstate
, "port in range '%s' is udp", name
);
7356 else if (real_proto
== IPPROTO_SCTP
)
7357 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7359 /* override PROTO_UNDEF */
7360 real_proto
= IPPROTO_TCP
;
7362 if (proto
== Q_SCTP
) {
7363 if (real_proto
== IPPROTO_UDP
)
7364 bpf_error(cstate
, "port in range '%s' is udp", name
);
7365 else if (real_proto
== IPPROTO_TCP
)
7366 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7368 /* override PROTO_UNDEF */
7369 real_proto
= IPPROTO_SCTP
;
7372 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7374 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7376 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7377 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7382 eaddr
= pcap_ether_hostton(name
);
7384 bpf_error(cstate
, "unknown ether host: %s", name
);
7386 res
= pcap_nametoaddrinfo(name
);
7389 bpf_error(cstate
, "unknown host '%s'", name
);
7390 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7395 bpf_error(cstate
, "unknown host '%s'", name
);
7398 bpf_error(cstate
, "'gateway' not supported in this configuration");
7402 real_proto
= lookup_proto(cstate
, name
, proto
);
7403 if (real_proto
>= 0)
7404 return gen_proto(cstate
, real_proto
, proto
, dir
);
7406 bpf_error(cstate
, "unknown protocol: %s", name
);
7408 #if !defined(NO_PROTOCHAIN)
7410 real_proto
= lookup_proto(cstate
, name
, proto
);
7411 if (real_proto
>= 0)
7412 return gen_protochain(cstate
, real_proto
, proto
);
7414 bpf_error(cstate
, "unknown protocol: %s", name
);
7415 #endif /* !defined(NO_PROTOCHAIN) */
7426 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7427 bpf_u_int32 masklen
, struct qual q
)
7429 register int nlen
, mlen
;
7434 * Catch errors reported by us and routines below us, and return NULL
7437 if (setjmp(cstate
->top_ctx
))
7440 nlen
= pcapint_atoin(s1
, &n
);
7442 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7443 /* Promote short ipaddr */
7447 mlen
= pcapint_atoin(s2
, &m
);
7449 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7450 /* Promote short ipaddr */
7453 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7456 /* Convert mask len to mask */
7458 bpf_error(cstate
, "mask length must be <= 32");
7459 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7460 m
= (bpf_u_int32
)m64
;
7462 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7469 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7472 bpf_error(cstate
, "Mask syntax for networks only");
7479 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7487 * Catch errors reported by us and routines below us, and return NULL
7490 if (setjmp(cstate
->top_ctx
))
7497 * v contains a 32-bit unsigned parsed from a string of the
7498 * form {N}, which could be decimal, hexadecimal or octal.
7499 * Although it would be possible to use the value as a raw
7500 * 16-bit DECnet address when the value fits into 16 bits, this
7501 * would be a questionable feature: DECnet address wire
7502 * encoding is little-endian, so this would not work as
7503 * intuitively as the same works for [big-endian] IPv4
7504 * addresses (0x01020304 means 1.2.3.4).
7506 if (proto
== Q_DECNET
)
7507 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7509 } else if (proto
== Q_DECNET
) {
7511 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7512 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7513 * for a valid DECnet address.
7515 vlen
= pcapint_atodn(s
, &v
);
7517 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7520 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7521 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7524 vlen
= pcapint_atoin(s
, &v
);
7526 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7534 if (proto
== Q_DECNET
)
7535 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7536 else if (proto
== Q_LINK
) {
7537 bpf_error(cstate
, "illegal link layer address");
7540 if (s
== NULL
&& q
.addr
== Q_NET
) {
7541 /* Promote short net number */
7542 while (v
&& (v
& 0xff000000) == 0) {
7547 /* Promote short ipaddr */
7549 mask
<<= 32 - vlen
;
7551 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7556 proto
= IPPROTO_UDP
;
7557 else if (proto
== Q_TCP
)
7558 proto
= IPPROTO_TCP
;
7559 else if (proto
== Q_SCTP
)
7560 proto
= IPPROTO_SCTP
;
7561 else if (proto
== Q_DEFAULT
)
7562 proto
= PROTO_UNDEF
;
7564 bpf_error(cstate
, "illegal qualifier of 'port'");
7567 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7571 b
= gen_port(cstate
, v
, proto
, dir
);
7572 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7578 proto
= IPPROTO_UDP
;
7579 else if (proto
== Q_TCP
)
7580 proto
= IPPROTO_TCP
;
7581 else if (proto
== Q_SCTP
)
7582 proto
= IPPROTO_SCTP
;
7583 else if (proto
== Q_DEFAULT
)
7584 proto
= PROTO_UNDEF
;
7586 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7589 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7593 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7594 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7599 bpf_error(cstate
, "'gateway' requires a name");
7603 return gen_proto(cstate
, v
, proto
, dir
);
7605 #if !defined(NO_PROTOCHAIN)
7607 return gen_protochain(cstate
, v
, proto
);
7623 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7626 struct addrinfo
*res
;
7627 struct in6_addr
*addr
;
7628 struct in6_addr mask
;
7630 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7633 * Catch errors reported by us and routines below us, and return NULL
7636 if (setjmp(cstate
->top_ctx
))
7639 res
= pcap_nametoaddrinfo(s
);
7641 bpf_error(cstate
, "invalid ip6 address %s", s
);
7644 bpf_error(cstate
, "%s resolved to multiple address", s
);
7645 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7647 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7648 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7649 memset(&mask
, 0, sizeof(mask
));
7650 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7652 mask
.s6_addr
[masklen
/ 8] =
7653 (0xff << (8 - masklen
% 8)) & 0xff;
7656 memcpy(a
, addr
, sizeof(a
));
7657 memcpy(m
, &mask
, sizeof(m
));
7658 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7659 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7660 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7668 bpf_error(cstate
, "Mask syntax for networks only");
7672 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7678 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7685 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7687 struct block
*b
, *tmp
;
7690 * Catch errors reported by us and routines below us, and return NULL
7693 if (setjmp(cstate
->top_ctx
))
7696 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7697 cstate
->e
= pcap_ether_aton(s
);
7698 if (cstate
->e
== NULL
)
7699 bpf_error(cstate
, "malloc");
7700 switch (cstate
->linktype
) {
7702 case DLT_NETANALYZER
:
7703 case DLT_NETANALYZER_TRANSPARENT
:
7704 tmp
= gen_prevlinkhdr_check(cstate
);
7705 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7710 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7713 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7715 case DLT_IEEE802_11
:
7716 case DLT_PRISM_HEADER
:
7717 case DLT_IEEE802_11_RADIO_AVS
:
7718 case DLT_IEEE802_11_RADIO
:
7720 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7722 case DLT_IP_OVER_FC
:
7723 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7728 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7735 bpf_error(cstate
, "ethernet address used in non-ether expression");
7740 sappend(struct slist
*s0
, struct slist
*s1
)
7743 * This is definitely not the best way to do this, but the
7744 * lists will rarely get long.
7751 static struct slist
*
7752 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7756 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7761 static struct slist
*
7762 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7766 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7772 * Modify "index" to use the value stored into its register as an
7773 * offset relative to the beginning of the header for the protocol
7774 * "proto", and allocate a register and put an item "size" bytes long
7775 * (1, 2, or 4) at that offset into that register, making it the register
7778 static struct arth
*
7779 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7783 struct slist
*s
, *tmp
;
7785 int regno
= alloc_reg(cstate
);
7787 free_reg(cstate
, inst
->regno
);
7791 bpf_error(cstate
, "data size must be 1, 2, or 4");
7808 bpf_error(cstate
, "unsupported index operation");
7812 * The offset is relative to the beginning of the packet
7813 * data, if we have a radio header. (If we don't, this
7816 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7817 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7818 cstate
->linktype
!= DLT_PRISM_HEADER
)
7819 bpf_error(cstate
, "radio information not present in capture");
7822 * Load into the X register the offset computed into the
7823 * register specified by "index".
7825 s
= xfer_to_x(cstate
, inst
);
7828 * Load the item at that offset.
7830 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7832 sappend(inst
->s
, s
);
7837 * The offset is relative to the beginning of
7838 * the link-layer header.
7840 * XXX - what about ATM LANE? Should the index be
7841 * relative to the beginning of the AAL5 frame, so
7842 * that 0 refers to the beginning of the LE Control
7843 * field, or relative to the beginning of the LAN
7844 * frame, so that 0 refers, for Ethernet LANE, to
7845 * the beginning of the destination address?
7847 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7850 * If "s" is non-null, it has code to arrange that the
7851 * X register contains the length of the prefix preceding
7852 * the link-layer header. Add to it the offset computed
7853 * into the register specified by "index", and move that
7854 * into the X register. Otherwise, just load into the X
7855 * register the offset computed into the register specified
7859 sappend(s
, xfer_to_a(cstate
, inst
));
7860 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7861 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7863 s
= xfer_to_x(cstate
, inst
);
7866 * Load the item at the sum of the offset we've put in the
7867 * X register and the offset of the start of the link
7868 * layer header (which is 0 if the radio header is
7869 * variable-length; that header length is what we put
7870 * into the X register and then added to the index).
7872 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7873 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7875 sappend(inst
->s
, s
);
7889 * The offset is relative to the beginning of
7890 * the network-layer header.
7891 * XXX - are there any cases where we want
7892 * cstate->off_nl_nosnap?
7894 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7897 * If "s" is non-null, it has code to arrange that the
7898 * X register contains the variable part of the offset
7899 * of the link-layer payload. Add to it the offset
7900 * computed into the register specified by "index",
7901 * and move that into the X register. Otherwise, just
7902 * load into the X register the offset computed into
7903 * the register specified by "index".
7906 sappend(s
, xfer_to_a(cstate
, inst
));
7907 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7908 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7910 s
= xfer_to_x(cstate
, inst
);
7913 * Load the item at the sum of the offset we've put in the
7914 * X register, the offset of the start of the network
7915 * layer header from the beginning of the link-layer
7916 * payload, and the constant part of the offset of the
7917 * start of the link-layer payload.
7919 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7920 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7922 sappend(inst
->s
, s
);
7925 * Do the computation only if the packet contains
7926 * the protocol in question.
7928 b
= gen_proto_abbrev_internal(cstate
, proto
);
7930 gen_and(inst
->b
, b
);
7944 * The offset is relative to the beginning of
7945 * the transport-layer header.
7947 * Load the X register with the length of the IPv4 header
7948 * (plus the offset of the link-layer header, if it's
7949 * a variable-length header), in bytes.
7951 * XXX - are there any cases where we want
7952 * cstate->off_nl_nosnap?
7953 * XXX - we should, if we're built with
7954 * IPv6 support, generate code to load either
7955 * IPv4, IPv6, or both, as appropriate.
7957 s
= gen_loadx_iphdrlen(cstate
);
7960 * The X register now contains the sum of the variable
7961 * part of the offset of the link-layer payload and the
7962 * length of the network-layer header.
7964 * Load into the A register the offset relative to
7965 * the beginning of the transport layer header,
7966 * add the X register to that, move that to the
7967 * X register, and load with an offset from the
7968 * X register equal to the sum of the constant part of
7969 * the offset of the link-layer payload and the offset,
7970 * relative to the beginning of the link-layer payload,
7971 * of the network-layer header.
7973 sappend(s
, xfer_to_a(cstate
, inst
));
7974 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7975 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7976 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7977 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7978 sappend(inst
->s
, s
);
7981 * Do the computation only if the packet contains
7982 * the protocol in question - which is true only
7983 * if this is an IP datagram and is the first or
7984 * only fragment of that datagram.
7986 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7988 gen_and(inst
->b
, b
);
7989 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7994 * Do the computation only if the packet contains
7995 * the protocol in question.
7997 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7999 gen_and(inst
->b
, b
);
8003 * Check if we have an icmp6 next header
8005 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
8007 gen_and(inst
->b
, b
);
8010 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8012 * If "s" is non-null, it has code to arrange that the
8013 * X register contains the variable part of the offset
8014 * of the link-layer payload. Add to it the offset
8015 * computed into the register specified by "index",
8016 * and move that into the X register. Otherwise, just
8017 * load into the X register the offset computed into
8018 * the register specified by "index".
8021 sappend(s
, xfer_to_a(cstate
, inst
));
8022 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
8023 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
8025 s
= xfer_to_x(cstate
, inst
);
8028 * Load the item at the sum of the offset we've put in the
8029 * X register, the offset of the start of the network
8030 * layer header from the beginning of the link-layer
8031 * payload, and the constant part of the offset of the
8032 * start of the link-layer payload.
8034 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
8035 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
8038 sappend(inst
->s
, s
);
8042 inst
->regno
= regno
;
8043 s
= new_stmt(cstate
, BPF_ST
);
8045 sappend(inst
->s
, s
);
8051 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
8055 * Catch errors reported by us and routines below us, and return NULL
8058 if (setjmp(cstate
->top_ctx
))
8061 return gen_load_internal(cstate
, proto
, inst
, size
);
8064 static struct block
*
8065 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8066 struct arth
*a1
, int reversed
)
8068 struct slist
*s0
, *s1
, *s2
;
8069 struct block
*b
, *tmp
;
8071 s0
= xfer_to_x(cstate
, a1
);
8072 s1
= xfer_to_a(cstate
, a0
);
8073 if (code
== BPF_JEQ
) {
8074 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
8075 b
= new_block(cstate
, JMP(code
));
8079 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
8085 sappend(a0
->s
, a1
->s
);
8089 free_reg(cstate
, a0
->regno
);
8090 free_reg(cstate
, a1
->regno
);
8092 /* 'and' together protocol checks */
8095 gen_and(a0
->b
, tmp
= a1
->b
);
8109 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8110 struct arth
*a1
, int reversed
)
8113 * Catch errors reported by us and routines below us, and return NULL
8116 if (setjmp(cstate
->top_ctx
))
8119 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
8123 gen_loadlen(compiler_state_t
*cstate
)
8130 * Catch errors reported by us and routines below us, and return NULL
8133 if (setjmp(cstate
->top_ctx
))
8136 regno
= alloc_reg(cstate
);
8137 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8138 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8139 s
->next
= new_stmt(cstate
, BPF_ST
);
8140 s
->next
->s
.k
= regno
;
8147 static struct arth
*
8148 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8154 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8156 reg
= alloc_reg(cstate
);
8158 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8160 s
->next
= new_stmt(cstate
, BPF_ST
);
8169 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8172 * Catch errors reported by us and routines below us, and return NULL
8175 if (setjmp(cstate
->top_ctx
))
8178 return gen_loadi_internal(cstate
, val
);
8182 * The a_arg dance is to avoid annoying whining by compilers that
8183 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8184 * It's not *used* after setjmp returns.
8187 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8189 struct arth
*a
= a_arg
;
8193 * Catch errors reported by us and routines below us, and return NULL
8196 if (setjmp(cstate
->top_ctx
))
8199 s
= xfer_to_a(cstate
, a
);
8201 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8204 s
= new_stmt(cstate
, BPF_ST
);
8212 * The a0_arg dance is to avoid annoying whining by compilers that
8213 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8214 * It's not *used* after setjmp returns.
8217 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8220 struct arth
*a0
= a0_arg
;
8221 struct slist
*s0
, *s1
, *s2
;
8224 * Catch errors reported by us and routines below us, and return NULL
8227 if (setjmp(cstate
->top_ctx
))
8231 * Disallow division by, or modulus by, zero; we do this here
8232 * so that it gets done even if the optimizer is disabled.
8234 * Also disallow shifts by a value greater than 31; we do this
8235 * here, for the same reason.
8237 if (code
== BPF_DIV
) {
8238 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8239 bpf_error(cstate
, "division by zero");
8240 } else if (code
== BPF_MOD
) {
8241 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8242 bpf_error(cstate
, "modulus by zero");
8243 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8244 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8245 bpf_error(cstate
, "shift by more than 31 bits");
8247 s0
= xfer_to_x(cstate
, a1
);
8248 s1
= xfer_to_a(cstate
, a0
);
8249 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8254 sappend(a0
->s
, a1
->s
);
8256 free_reg(cstate
, a0
->regno
);
8257 free_reg(cstate
, a1
->regno
);
8259 s0
= new_stmt(cstate
, BPF_ST
);
8260 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8267 * Initialize the table of used registers and the current register.
8270 init_regs(compiler_state_t
*cstate
)
8273 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8277 * Return the next free register.
8280 alloc_reg(compiler_state_t
*cstate
)
8282 int n
= BPF_MEMWORDS
;
8285 if (cstate
->regused
[cstate
->curreg
])
8286 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8288 cstate
->regused
[cstate
->curreg
] = 1;
8289 return cstate
->curreg
;
8292 bpf_error(cstate
, "too many registers needed to evaluate expression");
8297 * Return a register to the table so it can
8301 free_reg(compiler_state_t
*cstate
, int n
)
8303 cstate
->regused
[n
] = 0;
8306 static struct block
*
8307 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8312 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8313 b
= new_block(cstate
, JMP(jmp
));
8321 gen_greater(compiler_state_t
*cstate
, int n
)
8324 * Catch errors reported by us and routines below us, and return NULL
8327 if (setjmp(cstate
->top_ctx
))
8330 return gen_len(cstate
, BPF_JGE
, n
);
8334 * Actually, this is less than or equal.
8337 gen_less(compiler_state_t
*cstate
, int n
)
8342 * Catch errors reported by us and routines below us, and return NULL
8345 if (setjmp(cstate
->top_ctx
))
8348 b
= gen_len(cstate
, BPF_JGT
, n
);
8355 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8356 * the beginning of the link-layer header.
8357 * XXX - that means you can't test values in the radiotap header, but
8358 * as that header is difficult if not impossible to parse generally
8359 * without a loop, that might not be a severe problem. A new keyword
8360 * "radio" could be added for that, although what you'd really want
8361 * would be a way of testing particular radio header values, which
8362 * would generate code appropriate to the radio header in question.
8365 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8371 * Catch errors reported by us and routines below us, and return NULL
8374 if (setjmp(cstate
->top_ctx
))
8382 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8385 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8389 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8393 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8397 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8401 b
= new_block(cstate
, JMP(BPF_JEQ
));
8409 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8411 bpf_u_int32 hostmask
;
8412 struct block
*b0
, *b1
, *b2
;
8413 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8416 * Catch errors reported by us and routines below us, and return NULL
8419 if (setjmp(cstate
->top_ctx
))
8426 switch (cstate
->linktype
) {
8428 case DLT_ARCNET_LINUX
:
8429 // ARCnet broadcast is [8-bit] destination address 0.
8430 return gen_ahostop(cstate
, 0, Q_DST
);
8432 case DLT_NETANALYZER
:
8433 case DLT_NETANALYZER_TRANSPARENT
:
8434 b1
= gen_prevlinkhdr_check(cstate
);
8435 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8440 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8442 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8443 case DLT_IEEE802_11
:
8444 case DLT_PRISM_HEADER
:
8445 case DLT_IEEE802_11_RADIO_AVS
:
8446 case DLT_IEEE802_11_RADIO
:
8448 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8449 case DLT_IP_OVER_FC
:
8450 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8452 bpf_error(cstate
, "not a broadcast link");
8458 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8459 * as an indication that we don't know the netmask, and fail
8462 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8463 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8464 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8465 hostmask
= ~cstate
->netmask
;
8466 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8467 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8468 ~0 & hostmask
, hostmask
);
8473 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8478 * Generate code to test the low-order bit of a MAC address (that's
8479 * the bottom bit of the *first* byte).
8481 static struct block
*
8482 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8484 register struct block
*b0
;
8485 register struct slist
*s
;
8487 /* link[offset] & 1 != 0 */
8488 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8489 b0
= new_block(cstate
, JMP(BPF_JSET
));
8496 gen_multicast(compiler_state_t
*cstate
, int proto
)
8498 register struct block
*b0
, *b1
, *b2
;
8499 register struct slist
*s
;
8502 * Catch errors reported by us and routines below us, and return NULL
8505 if (setjmp(cstate
->top_ctx
))
8512 switch (cstate
->linktype
) {
8514 case DLT_ARCNET_LINUX
:
8515 // ARCnet multicast is the same as broadcast.
8516 return gen_ahostop(cstate
, 0, Q_DST
);
8518 case DLT_NETANALYZER
:
8519 case DLT_NETANALYZER_TRANSPARENT
:
8520 b1
= gen_prevlinkhdr_check(cstate
);
8521 /* ether[0] & 1 != 0 */
8522 b0
= gen_mac_multicast(cstate
, 0);
8528 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8530 * XXX - was that referring to bit-order issues?
8532 /* fddi[1] & 1 != 0 */
8533 return gen_mac_multicast(cstate
, 1);
8535 /* tr[2] & 1 != 0 */
8536 return gen_mac_multicast(cstate
, 2);
8537 case DLT_IEEE802_11
:
8538 case DLT_PRISM_HEADER
:
8539 case DLT_IEEE802_11_RADIO_AVS
:
8540 case DLT_IEEE802_11_RADIO
:
8545 * For control frames, there is no DA.
8547 * For management frames, DA is at an
8548 * offset of 4 from the beginning of
8551 * For data frames, DA is at an offset
8552 * of 4 from the beginning of the packet
8553 * if To DS is clear and at an offset of
8554 * 16 from the beginning of the packet
8559 * Generate the tests to be done for data frames.
8561 * First, check for To DS set, i.e. "link[1] & 0x01".
8563 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8564 b1
= new_block(cstate
, JMP(BPF_JSET
));
8565 b1
->s
.k
= 0x01; /* To DS */
8569 * If To DS is set, the DA is at 16.
8571 b0
= gen_mac_multicast(cstate
, 16);
8575 * Now, check for To DS not set, i.e. check
8576 * "!(link[1] & 0x01)".
8578 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8579 b2
= new_block(cstate
, JMP(BPF_JSET
));
8580 b2
->s
.k
= 0x01; /* To DS */
8585 * If To DS is not set, the DA is at 4.
8587 b1
= gen_mac_multicast(cstate
, 4);
8591 * Now OR together the last two checks. That gives
8592 * the complete set of checks for data frames.
8597 * Now check for a data frame.
8598 * I.e, check "link[0] & 0x08".
8600 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8601 b1
= new_block(cstate
, JMP(BPF_JSET
));
8606 * AND that with the checks done for data frames.
8611 * If the high-order bit of the type value is 0, this
8612 * is a management frame.
8613 * I.e, check "!(link[0] & 0x08)".
8615 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8616 b2
= new_block(cstate
, JMP(BPF_JSET
));
8622 * For management frames, the DA is at 4.
8624 b1
= gen_mac_multicast(cstate
, 4);
8628 * OR that with the checks done for data frames.
8629 * That gives the checks done for management and
8635 * If the low-order bit of the type value is 1,
8636 * this is either a control frame or a frame
8637 * with a reserved type, and thus not a
8640 * I.e., check "!(link[0] & 0x04)".
8642 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8643 b1
= new_block(cstate
, JMP(BPF_JSET
));
8649 * AND that with the checks for data and management
8654 case DLT_IP_OVER_FC
:
8655 b0
= gen_mac_multicast(cstate
, 2);
8660 /* Link not known to support multicasts */
8664 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8665 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8670 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8671 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8675 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8681 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8682 * we can look at special meta-data in the filter expression; otherwise we
8683 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8684 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8685 * pcap_activate() conditionally sets.
8688 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8690 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8692 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8694 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8699 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8701 register struct block
*b0
;
8704 * Catch errors reported by us and routines below us, and return NULL
8707 if (setjmp(cstate
->top_ctx
))
8711 * Only some data link types support ifindex qualifiers.
8713 switch (cstate
->linktype
) {
8714 case DLT_LINUX_SLL2
:
8715 /* match packets on this interface */
8716 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8719 #if defined(__linux__)
8720 require_basic_bpf_extensions(cstate
, "ifindex");
8722 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8724 #else /* defined(__linux__) */
8725 bpf_error(cstate
, "ifindex not supported on %s",
8726 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8728 #endif /* defined(__linux__) */
8734 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8735 * Outbound traffic is sent by this machine, while inbound traffic is
8736 * sent by a remote machine (and may include packets destined for a
8737 * unicast or multicast link-layer address we are not subscribing to).
8738 * These are the same definitions implemented by pcap_setdirection().
8739 * Capturing only unicast traffic destined for this host is probably
8740 * better accomplished using a higher-layer filter.
8743 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8745 register struct block
*b0
;
8748 * Catch errors reported by us and routines below us, and return NULL
8751 if (setjmp(cstate
->top_ctx
))
8755 * Only some data link types support inbound/outbound qualifiers.
8757 switch (cstate
->linktype
) {
8759 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8760 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8761 gen_loadi_internal(cstate
, 0),
8762 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8766 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8767 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8771 /* match outgoing packets */
8772 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8774 /* to filter on inbound traffic, invert the match */
8779 case DLT_LINUX_SLL2
:
8780 /* match outgoing packets */
8781 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8783 /* to filter on inbound traffic, invert the match */
8789 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8790 outbound
? PF_OUT
: PF_IN
);
8794 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8797 case DLT_JUNIPER_MFR
:
8798 case DLT_JUNIPER_MLFR
:
8799 case DLT_JUNIPER_MLPPP
:
8800 case DLT_JUNIPER_ATM1
:
8801 case DLT_JUNIPER_ATM2
:
8802 case DLT_JUNIPER_PPPOE
:
8803 case DLT_JUNIPER_PPPOE_ATM
:
8804 case DLT_JUNIPER_GGSN
:
8805 case DLT_JUNIPER_ES
:
8806 case DLT_JUNIPER_MONITOR
:
8807 case DLT_JUNIPER_SERVICES
:
8808 case DLT_JUNIPER_ETHER
:
8809 case DLT_JUNIPER_PPP
:
8810 case DLT_JUNIPER_FRELAY
:
8811 case DLT_JUNIPER_CHDLC
:
8812 case DLT_JUNIPER_VP
:
8813 case DLT_JUNIPER_ST
:
8814 case DLT_JUNIPER_ISM
:
8815 case DLT_JUNIPER_VS
:
8816 case DLT_JUNIPER_SRX_E2E
:
8817 case DLT_JUNIPER_FIBRECHANNEL
:
8818 case DLT_JUNIPER_ATM_CEMIC
:
8819 /* juniper flags (including direction) are stored
8820 * the byte after the 3-byte magic number */
8821 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8826 * If we have packet meta-data indicating a direction,
8827 * and that metadata can be checked by BPF code, check
8828 * it. Otherwise, give up, as this link-layer type has
8829 * nothing in the packet data.
8831 * Currently, the only platform where a BPF filter can
8832 * check that metadata is Linux with the in-kernel
8833 * BPF interpreter. If other packet capture mechanisms
8834 * and BPF filters also supported this, it would be
8835 * nice. It would be even better if they made that
8836 * metadata available so that we could provide it
8837 * with newer capture APIs, allowing it to be saved
8840 #if defined(__linux__)
8841 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8842 /* match outgoing packets */
8843 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8846 /* to filter on inbound traffic, invert the match */
8849 #else /* defined(__linux__) */
8850 bpf_error(cstate
, "inbound/outbound not supported on %s",
8851 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8853 #endif /* defined(__linux__) */
8858 /* PF firewall log matched interface */
8860 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8866 * Catch errors reported by us and routines below us, and return NULL
8869 if (setjmp(cstate
->top_ctx
))
8872 if (cstate
->linktype
!= DLT_PFLOG
) {
8873 bpf_error(cstate
, "ifname supported only on PF linktype");
8876 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8877 off
= offsetof(struct pfloghdr
, ifname
);
8878 if (strlen(ifname
) >= len
) {
8879 bpf_error(cstate
, "ifname interface names can only be %d characters",
8883 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8884 (const u_char
*)ifname
);
8888 /* PF firewall log ruleset name */
8890 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8895 * Catch errors reported by us and routines below us, and return NULL
8898 if (setjmp(cstate
->top_ctx
))
8901 if (cstate
->linktype
!= DLT_PFLOG
) {
8902 bpf_error(cstate
, "ruleset supported only on PF linktype");
8906 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8907 bpf_error(cstate
, "ruleset names can only be %ld characters",
8908 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8912 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8913 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8917 /* PF firewall log rule number */
8919 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8924 * Catch errors reported by us and routines below us, and return NULL
8927 if (setjmp(cstate
->top_ctx
))
8930 if (cstate
->linktype
!= DLT_PFLOG
) {
8931 bpf_error(cstate
, "rnr supported only on PF linktype");
8935 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8940 /* PF firewall log sub-rule number */
8942 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8947 * Catch errors reported by us and routines below us, and return NULL
8950 if (setjmp(cstate
->top_ctx
))
8953 if (cstate
->linktype
!= DLT_PFLOG
) {
8954 bpf_error(cstate
, "srnr supported only on PF linktype");
8958 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8963 /* PF firewall log reason code */
8965 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8970 * Catch errors reported by us and routines below us, and return NULL
8973 if (setjmp(cstate
->top_ctx
))
8976 if (cstate
->linktype
!= DLT_PFLOG
) {
8977 bpf_error(cstate
, "reason supported only on PF linktype");
8981 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8982 (bpf_u_int32
)reason
);
8986 /* PF firewall log action */
8988 gen_pf_action(compiler_state_t
*cstate
, int action
)
8993 * Catch errors reported by us and routines below us, and return NULL
8996 if (setjmp(cstate
->top_ctx
))
8999 if (cstate
->linktype
!= DLT_PFLOG
) {
9000 bpf_error(cstate
, "action supported only on PF linktype");
9004 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
9005 (bpf_u_int32
)action
);
9009 /* IEEE 802.11 wireless header */
9011 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
9016 * Catch errors reported by us and routines below us, and return NULL
9019 if (setjmp(cstate
->top_ctx
))
9022 switch (cstate
->linktype
) {
9024 case DLT_IEEE802_11
:
9025 case DLT_PRISM_HEADER
:
9026 case DLT_IEEE802_11_RADIO_AVS
:
9027 case DLT_IEEE802_11_RADIO
:
9028 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
9032 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
9040 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
9045 * Catch errors reported by us and routines below us, and return NULL
9048 if (setjmp(cstate
->top_ctx
))
9051 switch (cstate
->linktype
) {
9053 case DLT_IEEE802_11
:
9054 case DLT_PRISM_HEADER
:
9055 case DLT_IEEE802_11_RADIO_AVS
:
9056 case DLT_IEEE802_11_RADIO
:
9060 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
9064 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
9065 IEEE80211_FC1_DIR_MASK
);
9070 // Process an ARCnet host address string.
9072 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
9075 * Catch errors reported by us and routines below us, and return NULL
9078 if (setjmp(cstate
->top_ctx
))
9081 switch (cstate
->linktype
) {
9084 case DLT_ARCNET_LINUX
:
9085 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
9086 q
.proto
== Q_LINK
) {
9089 * The lexer currently defines the address format in a
9090 * way that makes this error condition never true.
9091 * Let's check it anyway in case this part of the lexer
9092 * changes in future.
9094 if (! pcapint_atoan(s
, &addr
))
9095 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
9096 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
9098 bpf_error(cstate
, "ARCnet address used in non-arc expression");
9102 bpf_error(cstate
, "aid supported only on ARCnet");
9107 // Compare an ARCnet host address with the given value.
9108 static struct block
*
9109 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
9111 register struct block
*b0
, *b1
;
9115 * ARCnet is different from Ethernet: the source address comes before
9116 * the destination address, each is one byte long. This holds for all
9117 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
9118 * in the 1983 edition of the "ARCNET Designer's Handbook" published
9119 * by Datapoint (document number 61610-01).
9122 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
9125 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
9128 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9129 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9135 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9136 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9141 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9145 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9149 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9153 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9157 bpf_error(cstate
, "'ra' is only supported on 802.11");
9161 bpf_error(cstate
, "'ta' is only supported on 802.11");
9168 static struct block
*
9169 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9171 struct block
*b0
, *b1
;
9173 /* check for VLAN, including 802.1ad and QinQ */
9174 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9175 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9178 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9184 static struct block
*
9185 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9187 if (vlan_num
> 0x0fff) {
9188 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9191 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9194 static struct block
*
9195 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9198 struct block
*b0
, *b1
;
9200 b0
= gen_vlan_tpid_test(cstate
);
9203 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9209 * Both payload and link header type follow the VLAN tags so that
9210 * both need to be updated.
9212 cstate
->off_linkpl
.constant_part
+= 4;
9213 cstate
->off_linktype
.constant_part
+= 4;
9218 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9219 /* add v to variable part of off */
9221 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9222 bpf_u_int32 v
, struct slist
*s
)
9226 if (!off
->is_variable
)
9227 off
->is_variable
= 1;
9229 off
->reg
= alloc_reg(cstate
);
9231 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9234 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9237 s2
= new_stmt(cstate
, BPF_ST
);
9243 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9244 * and link type offsets first
9247 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9251 /* offset determined at run time, shift variable part */
9253 cstate
->is_vlan_vloffset
= 1;
9254 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9255 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9257 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9258 sappend(s
.next
, b_tpid
->head
->stmts
);
9259 b_tpid
->head
->stmts
= s
.next
;
9263 * patch block b_vid (VLAN id test) to load VID value either from packet
9264 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9267 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9269 struct slist
*s
, *s2
, *sjeq
;
9272 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9273 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9275 /* true -> next instructions, false -> beginning of b_vid */
9276 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9278 sjeq
->s
.jf
= b_vid
->stmts
;
9281 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
9282 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
9286 /* Jump to the test in b_vid. We need to jump one instruction before
9287 * the end of the b_vid block so that we only skip loading the TCI
9288 * from packet data and not the 'and' instruction extracting VID.
9291 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9293 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9297 /* insert our statements at the beginning of b_vid */
9298 sappend(s
, b_vid
->stmts
);
9303 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9304 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9305 * tag can be either in metadata or in packet data; therefore if the
9306 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9307 * header for VLAN tag. As the decision is done at run time, we need
9308 * update variable part of the offsets
9310 static struct block
*
9311 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9314 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9317 /* generate new filter code based on extracting packet
9319 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9320 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9322 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9327 * This is tricky. We need to insert the statements updating variable
9328 * parts of offsets before the traditional TPID and VID tests so
9329 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9330 * we do not want this update to affect those checks. That's why we
9331 * generate both test blocks first and insert the statements updating
9332 * variable parts of both offsets after that. This wouldn't work if
9333 * there already were variable length link header when entering this
9334 * function but gen_vlan_bpf_extensions() isn't called in that case.
9336 b_tpid
= gen_vlan_tpid_test(cstate
);
9338 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9340 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9345 gen_vlan_patch_vid_test(cstate
, b_vid
);
9355 * support IEEE 802.1Q VLAN trunk over ethernet
9358 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9363 * Catch errors reported by us and routines below us, and return NULL
9366 if (setjmp(cstate
->top_ctx
))
9369 /* can't check for VLAN-encapsulated packets inside MPLS */
9370 if (cstate
->label_stack_depth
> 0)
9371 bpf_error(cstate
, "no VLAN match after MPLS");
9374 * Check for a VLAN packet, and then change the offsets to point
9375 * to the type and data fields within the VLAN packet. Just
9376 * increment the offsets, so that we can support a hierarchy, e.g.
9377 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9380 * XXX - this is a bit of a kludge. If we were to split the
9381 * compiler into a parser that parses an expression and
9382 * generates an expression tree, and a code generator that
9383 * takes an expression tree (which could come from our
9384 * parser or from some other parser) and generates BPF code,
9385 * we could perhaps make the offsets parameters of routines
9386 * and, in the handler for an "AND" node, pass to subnodes
9387 * other than the VLAN node the adjusted offsets.
9389 * This would mean that "vlan" would, instead of changing the
9390 * behavior of *all* tests after it, change only the behavior
9391 * of tests ANDed with it. That would change the documented
9392 * semantics of "vlan", which might break some expressions.
9393 * However, it would mean that "(vlan and ip) or ip" would check
9394 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9395 * checking only for VLAN-encapsulated IP, so that could still
9396 * be considered worth doing; it wouldn't break expressions
9397 * that are of the form "vlan and ..." or "vlan N and ...",
9398 * which I suspect are the most common expressions involving
9399 * "vlan". "vlan or ..." doesn't necessarily do what the user
9400 * would really want, now, as all the "or ..." tests would
9401 * be done assuming a VLAN, even though the "or" could be viewed
9402 * as meaning "or, if this isn't a VLAN packet...".
9404 switch (cstate
->linktype
) {
9408 * Newer version of the Linux kernel pass around
9409 * packets in which the VLAN tag has been removed
9410 * from the packet data and put into metadata.
9412 * This requires special treatment.
9414 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9415 /* Verify that this is the outer part of the packet and
9416 * not encapsulated somehow. */
9417 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9418 cstate
->off_linkhdr
.constant_part
==
9419 cstate
->off_outermostlinkhdr
.constant_part
) {
9421 * Do we need special VLAN handling?
9423 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9424 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9427 b0
= gen_vlan_no_bpf_extensions(cstate
,
9428 vlan_num
, has_vlan_tag
);
9431 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9435 case DLT_NETANALYZER
:
9436 case DLT_NETANALYZER_TRANSPARENT
:
9437 case DLT_IEEE802_11
:
9438 case DLT_PRISM_HEADER
:
9439 case DLT_IEEE802_11_RADIO_AVS
:
9440 case DLT_IEEE802_11_RADIO
:
9442 * These are either Ethernet packets with an additional
9443 * metadata header (the NetAnalyzer types), or 802.11
9444 * packets, possibly with an additional metadata header.
9446 * For the first of those, the VLAN tag is in the normal
9447 * place, so the special-case handling above isn't
9450 * For the second of those, we don't do the special-case
9453 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9457 bpf_error(cstate
, "no VLAN support for %s",
9458 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9462 cstate
->vlan_stack_depth
++;
9470 * The label_num_arg dance is to avoid annoying whining by compilers that
9471 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9472 * It's not *used* after setjmp returns.
9474 static struct block
*
9475 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9478 struct block
*b0
, *b1
;
9480 if (cstate
->label_stack_depth
> 0) {
9481 /* just match the bottom-of-stack bit clear */
9482 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9485 * We're not in an MPLS stack yet, so check the link-layer
9486 * type against MPLS.
9488 switch (cstate
->linktype
) {
9490 case DLT_C_HDLC
: /* fall through */
9493 case DLT_NETANALYZER
:
9494 case DLT_NETANALYZER_TRANSPARENT
:
9495 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9499 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9502 /* FIXME add other DLT_s ...
9503 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9504 * leave it for now */
9507 bpf_error(cstate
, "no MPLS support for %s",
9508 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9513 /* If a specific MPLS label is requested, check it */
9514 if (has_label_num
) {
9515 if (label_num
> 0xFFFFF) {
9516 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9517 label_num
, 0xFFFFF);
9519 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9520 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9521 0xfffff000); /* only compare the first 20 bits */
9527 * Change the offsets to point to the type and data fields within
9528 * the MPLS packet. Just increment the offsets, so that we
9529 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9530 * capture packets with an outer label of 100000 and an inner
9533 * Increment the MPLS stack depth as well; this indicates that
9534 * we're checking MPLS-encapsulated headers, to make sure higher
9535 * level code generators don't try to match against IP-related
9536 * protocols such as Q_ARP, Q_RARP etc.
9538 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9540 cstate
->off_nl_nosnap
+= 4;
9541 cstate
->off_nl
+= 4;
9542 cstate
->label_stack_depth
++;
9547 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9550 * Catch errors reported by us and routines below us, and return NULL
9553 if (setjmp(cstate
->top_ctx
))
9556 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9560 * Support PPPOE discovery and session.
9563 gen_pppoed(compiler_state_t
*cstate
)
9566 * Catch errors reported by us and routines below us, and return NULL
9569 if (setjmp(cstate
->top_ctx
))
9572 /* check for PPPoE discovery */
9573 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9577 * RFC 2516 Section 4:
9579 * The Ethernet payload for PPPoE is as follows:
9582 * 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
9583 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9584 * | VER | TYPE | CODE | SESSION_ID |
9585 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9586 * | LENGTH | payload ~
9587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9590 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9592 struct block
*b0
, *b1
;
9595 * Catch errors reported by us and routines below us, and return NULL
9598 if (setjmp(cstate
->top_ctx
))
9602 * Test against the PPPoE session link-layer type.
9604 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9606 /* If a specific session is requested, check PPPoE session id */
9608 if (sess_num
> UINT16_MAX
) {
9609 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9610 sess_num
, UINT16_MAX
);
9612 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9618 * Change the offsets to point to the type and data fields within
9619 * the PPP packet, and note that this is PPPoE rather than
9622 * XXX - this is a bit of a kludge. See the comments in
9625 * The "network-layer" protocol is PPPoE, which has a 6-byte
9626 * PPPoE header, followed by a PPP packet.
9628 * There is no HDLC encapsulation for the PPP packet (it's
9629 * encapsulated in PPPoES instead), so the link-layer type
9630 * starts at the first byte of the PPP packet. For PPPoE,
9631 * that offset is relative to the beginning of the total
9632 * link-layer payload, including any 802.2 LLC header, so
9633 * it's 6 bytes past cstate->off_nl.
9635 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9636 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9637 cstate
->off_linkpl
.reg
);
9639 cstate
->off_linktype
= cstate
->off_linkhdr
;
9640 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9643 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9648 /* Check that this is Geneve and the VNI is correct if
9649 * specified. Parameterized to handle both IPv4 and IPv6. */
9650 static struct block
*
9651 gen_geneve_check(compiler_state_t
*cstate
,
9652 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9653 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9655 struct block
*b0
, *b1
;
9657 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9659 /* Check that we are operating on version 0. Otherwise, we
9660 * can't decode the rest of the fields. The version is 2 bits
9661 * in the first byte of the Geneve header. */
9662 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9667 if (vni
> 0xffffff) {
9668 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9671 vni
<<= 8; /* VNI is in the upper 3 bytes */
9672 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9680 /* The IPv4 and IPv6 Geneve checks need to do two things:
9681 * - Verify that this actually is Geneve with the right VNI.
9682 * - Place the IP header length (plus variable link prefix if
9683 * needed) into register A to be used later to compute
9684 * the inner packet offsets. */
9685 static struct block
*
9686 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9688 struct block
*b0
, *b1
;
9689 struct slist
*s
, *s1
;
9691 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9693 /* Load the IP header length into A. */
9694 s
= gen_loadx_iphdrlen(cstate
);
9696 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9699 /* Forcibly append these statements to the true condition
9700 * of the protocol check by creating a new block that is
9701 * always true and ANDing them. */
9702 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9711 static struct block
*
9712 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9714 struct block
*b0
, *b1
;
9715 struct slist
*s
, *s1
;
9717 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9719 /* Load the IP header length. We need to account for a
9720 * variable length link prefix if there is one. */
9721 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9723 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9727 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9731 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9735 /* Forcibly append these statements to the true condition
9736 * of the protocol check by creating a new block that is
9737 * always true and ANDing them. */
9738 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9741 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9750 /* We need to store three values based on the Geneve header::
9751 * - The offset of the linktype.
9752 * - The offset of the end of the Geneve header.
9753 * - The offset of the end of the encapsulated MAC header. */
9754 static struct slist
*
9755 gen_geneve_offsets(compiler_state_t
*cstate
)
9757 struct slist
*s
, *s1
, *s_proto
;
9759 /* First we need to calculate the offset of the Geneve header
9760 * itself. This is composed of the IP header previously calculated
9761 * (include any variable link prefix) and stored in A plus the
9762 * fixed sized headers (fixed link prefix, MAC length, and UDP
9764 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9765 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9767 /* Stash this in X since we'll need it later. */
9768 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9771 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9773 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9777 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9778 cstate
->off_linktype
.is_variable
= 1;
9779 cstate
->off_linktype
.constant_part
= 0;
9781 s1
= new_stmt(cstate
, BPF_ST
);
9782 s1
->s
.k
= cstate
->off_linktype
.reg
;
9785 /* Load the Geneve option length and mask and shift to get the
9786 * number of bytes. It is stored in the first byte of the Geneve
9788 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9792 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9796 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9800 /* Add in the rest of the Geneve base header. */
9801 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9805 /* Add the Geneve header length to its offset and store. */
9806 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9810 /* Set the encapsulated type as Ethernet. Even though we may
9811 * not actually have Ethernet inside there are two reasons this
9813 * - The linktype field is always in EtherType format regardless
9814 * of whether it is in Geneve or an inner Ethernet frame.
9815 * - The only link layer that we have specific support for is
9816 * Ethernet. We will confirm that the packet actually is
9817 * Ethernet at runtime before executing these checks. */
9818 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9820 s1
= new_stmt(cstate
, BPF_ST
);
9821 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9824 /* Calculate whether we have an Ethernet header or just raw IP/
9825 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9826 * and linktype by 14 bytes so that the network header can be found
9827 * seamlessly. Otherwise, keep what we've calculated already. */
9829 /* We have a bare jmp so we can't use the optimizer. */
9830 cstate
->no_optimize
= 1;
9832 /* Load the EtherType in the Geneve header, 2 bytes in. */
9833 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9837 /* Load X with the end of the Geneve header. */
9838 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9839 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9842 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9843 * end of this check, we should have the total length in X. In
9844 * the non-Ethernet case, it's already there. */
9845 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9846 s_proto
->s
.k
= ETHERTYPE_TEB
;
9847 sappend(s
, s_proto
);
9849 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9853 /* Since this is Ethernet, use the EtherType of the payload
9854 * directly as the linktype. Overwrite what we already have. */
9855 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9859 s1
= new_stmt(cstate
, BPF_ST
);
9860 s1
->s
.k
= cstate
->off_linktype
.reg
;
9863 /* Advance two bytes further to get the end of the Ethernet
9865 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9869 /* Move the result to X. */
9870 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9873 /* Store the final result of our linkpl calculation. */
9874 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9875 cstate
->off_linkpl
.is_variable
= 1;
9876 cstate
->off_linkpl
.constant_part
= 0;
9878 s1
= new_stmt(cstate
, BPF_STX
);
9879 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9888 /* Check to see if this is a Geneve packet. */
9890 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9892 struct block
*b0
, *b1
;
9896 * Catch errors reported by us and routines below us, and return NULL
9899 if (setjmp(cstate
->top_ctx
))
9902 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9903 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9908 /* Later filters should act on the payload of the Geneve frame,
9909 * update all of the header pointers. Attach this code so that
9910 * it gets executed in the event that the Geneve filter matches. */
9911 s
= gen_geneve_offsets(cstate
);
9913 b1
= gen_true(cstate
);
9914 sappend(s
, b1
->stmts
);
9919 cstate
->is_encap
= 1;
9924 /* Check that this is VXLAN and the VNI is correct if
9925 * specified. Parameterized to handle both IPv4 and IPv6. */
9926 static struct block
*
9927 gen_vxlan_check(compiler_state_t
*cstate
,
9928 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9929 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9931 struct block
*b0
, *b1
;
9933 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9935 /* Check that the VXLAN header has the flag bits set
9937 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9942 if (vni
> 0xffffff) {
9943 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9946 vni
<<= 8; /* VNI is in the upper 3 bytes */
9947 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9955 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9956 * - Verify that this actually is VXLAN with the right VNI.
9957 * - Place the IP header length (plus variable link prefix if
9958 * needed) into register A to be used later to compute
9959 * the inner packet offsets. */
9960 static struct block
*
9961 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9963 struct block
*b0
, *b1
;
9964 struct slist
*s
, *s1
;
9966 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9968 /* Load the IP header length into A. */
9969 s
= gen_loadx_iphdrlen(cstate
);
9971 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9974 /* Forcibly append these statements to the true condition
9975 * of the protocol check by creating a new block that is
9976 * always true and ANDing them. */
9977 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9986 static struct block
*
9987 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9989 struct block
*b0
, *b1
;
9990 struct slist
*s
, *s1
;
9992 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9994 /* Load the IP header length. We need to account for a
9995 * variable length link prefix if there is one. */
9996 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9998 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
10002 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
10006 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
10010 /* Forcibly append these statements to the true condition
10011 * of the protocol check by creating a new block that is
10012 * always true and ANDing them. */
10013 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10016 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10025 /* We need to store three values based on the VXLAN header:
10026 * - The offset of the linktype.
10027 * - The offset of the end of the VXLAN header.
10028 * - The offset of the end of the encapsulated MAC header. */
10029 static struct slist
*
10030 gen_vxlan_offsets(compiler_state_t
*cstate
)
10032 struct slist
*s
, *s1
;
10034 /* Calculate the offset of the VXLAN header itself. This
10035 * includes the IP header computed previously (including any
10036 * variable link prefix) and stored in A plus the fixed size
10037 * headers (fixed link prefix, MAC length, UDP header). */
10038 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10039 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
10041 /* Add the VXLAN header length to its offset and store */
10042 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10046 /* Push the link header. VXLAN packets always contain Ethernet
10048 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
10050 s1
= new_stmt(cstate
, BPF_ST
);
10051 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
10054 /* As the payload is an Ethernet packet, we can use the
10055 * EtherType of the payload directly as the linktype. */
10056 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10060 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
10061 cstate
->off_linktype
.is_variable
= 1;
10062 cstate
->off_linktype
.constant_part
= 0;
10064 s1
= new_stmt(cstate
, BPF_ST
);
10065 s1
->s
.k
= cstate
->off_linktype
.reg
;
10068 /* Two bytes further is the end of the Ethernet header and the
10069 * start of the payload. */
10070 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10074 /* Move the result to X. */
10075 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10078 /* Store the final result of our linkpl calculation. */
10079 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
10080 cstate
->off_linkpl
.is_variable
= 1;
10081 cstate
->off_linkpl
.constant_part
= 0;
10083 s1
= new_stmt(cstate
, BPF_STX
);
10084 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10087 cstate
->off_nl
= 0;
10092 /* Check to see if this is a VXLAN packet. */
10094 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
10096 struct block
*b0
, *b1
;
10100 * Catch errors reported by us and routines below us, and return NULL
10103 if (setjmp(cstate
->top_ctx
))
10106 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
10107 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
10112 /* Later filters should act on the payload of the VXLAN frame,
10113 * update all of the header pointers. Attach this code so that
10114 * it gets executed in the event that the VXLAN filter matches. */
10115 s
= gen_vxlan_offsets(cstate
);
10117 b1
= gen_true(cstate
);
10118 sappend(s
, b1
->stmts
);
10123 cstate
->is_encap
= 1;
10128 /* Check that the encapsulated frame has a link layer header
10129 * for Ethernet filters. */
10130 static struct block
*
10131 gen_encap_ll_check(compiler_state_t
*cstate
)
10134 struct slist
*s
, *s1
;
10136 /* The easiest way to see if there is a link layer present
10137 * is to check if the link layer header and payload are not
10140 /* Geneve always generates pure variable offsets so we can
10141 * compare only the registers. */
10142 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
10143 s
->s
.k
= cstate
->off_linkhdr
.reg
;
10145 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
10146 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10149 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10157 static struct block
*
10158 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
10159 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10163 switch (atmfield
) {
10166 if (!cstate
->is_atm
)
10167 bpf_error(cstate
, "'vpi' supported only on raw ATM");
10168 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10170 if (jvalue
> UINT8_MAX
)
10171 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
10172 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10173 0xffffffffU
, jtype
, reverse
, jvalue
);
10177 if (!cstate
->is_atm
)
10178 bpf_error(cstate
, "'vci' supported only on raw ATM");
10179 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10181 if (jvalue
> UINT16_MAX
)
10182 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
10183 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10184 0xffffffffU
, jtype
, reverse
, jvalue
);
10188 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10189 abort(); /* XXX - this isn't on FreeBSD */
10190 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10191 0x0fU
, jtype
, reverse
, jvalue
);
10195 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10197 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10198 0xffffffffU
, jtype
, reverse
, jvalue
);
10201 case A_CALLREFTYPE
:
10202 if (!cstate
->is_atm
)
10203 bpf_error(cstate
, "'callref' supported only on raw ATM");
10204 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10206 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10207 0xffffffffU
, jtype
, reverse
, jvalue
);
10216 static struct block
*
10217 gen_atmtype_metac(compiler_state_t
*cstate
)
10219 struct block
*b0
, *b1
;
10221 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10222 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10227 static struct block
*
10228 gen_atmtype_sc(compiler_state_t
*cstate
)
10230 struct block
*b0
, *b1
;
10232 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10233 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10238 static struct block
*
10239 gen_atmtype_llc(compiler_state_t
*cstate
)
10243 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10244 cstate
->linktype
= cstate
->prevlinktype
;
10249 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10250 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10253 * Catch errors reported by us and routines below us, and return NULL
10256 if (setjmp(cstate
->top_ctx
))
10259 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10264 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10266 struct block
*b0
, *b1
;
10269 * Catch errors reported by us and routines below us, and return NULL
10272 if (setjmp(cstate
->top_ctx
))
10278 /* Get all packets in Meta signalling Circuit */
10279 if (!cstate
->is_atm
)
10280 bpf_error(cstate
, "'metac' supported only on raw ATM");
10281 b1
= gen_atmtype_metac(cstate
);
10285 /* Get all packets in Broadcast Circuit*/
10286 if (!cstate
->is_atm
)
10287 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10288 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10289 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10294 /* Get all cells in Segment OAM F4 circuit*/
10295 if (!cstate
->is_atm
)
10296 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10297 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10298 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10303 /* Get all cells in End-to-End OAM F4 Circuit*/
10304 if (!cstate
->is_atm
)
10305 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10306 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10307 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10312 /* Get all packets in connection Signalling Circuit */
10313 if (!cstate
->is_atm
)
10314 bpf_error(cstate
, "'sc' supported only on raw ATM");
10315 b1
= gen_atmtype_sc(cstate
);
10319 /* Get all packets in ILMI Circuit */
10320 if (!cstate
->is_atm
)
10321 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10322 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10323 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10328 /* Get all LANE packets */
10329 if (!cstate
->is_atm
)
10330 bpf_error(cstate
, "'lane' supported only on raw ATM");
10331 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10334 * Arrange that all subsequent tests assume LANE
10335 * rather than LLC-encapsulated packets, and set
10336 * the offsets appropriately for LANE-encapsulated
10339 * We assume LANE means Ethernet, not Token Ring.
10341 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10342 cstate
->off_payload
+ 2, /* Ethernet header */
10344 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10345 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10346 cstate
->off_nl
= 0; /* Ethernet II */
10347 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10351 /* Get all LLC-encapsulated packets */
10352 if (!cstate
->is_atm
)
10353 bpf_error(cstate
, "'llc' supported only on raw ATM");
10354 b1
= gen_atmtype_llc(cstate
);
10364 * Filtering for MTP2 messages based on li value
10365 * FISU, length is null
10366 * LSSU, length is 1 or 2
10367 * MSU, length is 3 or more
10368 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10371 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10373 struct block
*b0
, *b1
;
10376 * Catch errors reported by us and routines below us, and return NULL
10379 if (setjmp(cstate
->top_ctx
))
10385 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10386 (cstate
->linktype
!= DLT_ERF
) &&
10387 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10388 bpf_error(cstate
, "'fisu' supported only on MTP2");
10389 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10390 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10391 0x3fU
, BPF_JEQ
, 0, 0U);
10395 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10396 (cstate
->linktype
!= DLT_ERF
) &&
10397 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10398 bpf_error(cstate
, "'lssu' supported only on MTP2");
10399 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10400 0x3fU
, BPF_JGT
, 1, 2U);
10401 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10402 0x3fU
, BPF_JGT
, 0, 0U);
10407 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10408 (cstate
->linktype
!= DLT_ERF
) &&
10409 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10410 bpf_error(cstate
, "'msu' supported only on MTP2");
10411 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10412 0x3fU
, BPF_JGT
, 0, 2U);
10416 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10417 (cstate
->linktype
!= DLT_ERF
) &&
10418 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10419 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10420 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10421 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10422 0xff80U
, BPF_JEQ
, 0, 0U);
10426 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10427 (cstate
->linktype
!= DLT_ERF
) &&
10428 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10429 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10430 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10431 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10432 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10433 0xff80U
, BPF_JGT
, 0, 0U);
10438 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10439 (cstate
->linktype
!= DLT_ERF
) &&
10440 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10441 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10442 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10443 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10453 * These maximum valid values are all-ones, so they double as the bitmasks
10454 * before any bitwise shifting.
10456 #define MTP2_SIO_MAXVAL UINT8_MAX
10457 #define MTP3_PC_MAXVAL 0x3fffU
10458 #define MTP3_SLS_MAXVAL 0xfU
10460 static struct block
*
10461 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10462 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10470 newoff_sio
= cstate
->off_sio
;
10471 newoff_opc
= cstate
->off_opc
;
10472 newoff_dpc
= cstate
->off_dpc
;
10473 newoff_sls
= cstate
->off_sls
;
10474 switch (mtp3field
) {
10477 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10479 * SIO is the simplest field: the size is one byte and the offset is a
10480 * multiple of bytes, so the only detail to get right is the value of
10481 * the [right-to-left] field offset.
10484 newoff_sio
+= 3; /* offset for MTP2_HSL */
10488 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10489 bpf_error(cstate
, "'sio' supported only on SS7");
10490 if(jvalue
> MTP2_SIO_MAXVAL
)
10491 bpf_error(cstate
, "sio value %u too big; max value = %u",
10492 jvalue
, MTP2_SIO_MAXVAL
);
10493 // Here the bitmask means "do not apply a bitmask".
10494 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10495 jtype
, reverse
, jvalue
);
10499 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10501 * SLS, OPC and DPC are more complicated: none of these is sized in a
10502 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10503 * diagrams are meant to be read right-to-left. This means in the
10504 * diagrams within individual fields and concatenations thereof
10505 * bitwise shifts and masks can be noted in the common left-to-right
10506 * manner until each final value is ready to be byte-swapped and
10507 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10508 * similar problem in a similar way.
10510 * Offsets of fields within the packet header always have the
10511 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10512 * DLTs the offset does not include the F (Flag) field at the
10513 * beginning of each message.
10515 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10516 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10517 * be tested entirely using a single BPF_W comparison. In this case
10518 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10519 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10520 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10521 * correlates with the [RTL] packet diagram until the byte-swapping is
10524 * The code below uses this approach for OPC, which spans 3 bytes.
10525 * DPC and SLS use shorter loads, SLS also uses a different offset.
10532 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10533 bpf_error(cstate
, "'opc' supported only on SS7");
10534 if (jvalue
> MTP3_PC_MAXVAL
)
10535 bpf_error(cstate
, "opc value %u too big; max value = %u",
10536 jvalue
, MTP3_PC_MAXVAL
);
10537 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10538 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10539 SWAPLONG(jvalue
<< 14));
10547 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10548 bpf_error(cstate
, "'dpc' supported only on SS7");
10549 if (jvalue
> MTP3_PC_MAXVAL
)
10550 bpf_error(cstate
, "dpc value %u too big; max value = %u",
10551 jvalue
, MTP3_PC_MAXVAL
);
10552 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10553 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10554 SWAPSHORT(jvalue
));
10562 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10563 bpf_error(cstate
, "'sls' supported only on SS7");
10564 if (jvalue
> MTP3_SLS_MAXVAL
)
10565 bpf_error(cstate
, "sls value %u too big; max value = %u",
10566 jvalue
, MTP3_SLS_MAXVAL
);
10567 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10568 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10579 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10580 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10583 * Catch errors reported by us and routines below us, and return NULL
10586 if (setjmp(cstate
->top_ctx
))
10589 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10593 static struct block
*
10594 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10599 * Q.2931 signalling protocol messages for handling virtual circuits
10600 * establishment and teardown
10605 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10608 case A_CALLPROCEED
:
10609 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10613 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10617 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10621 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10624 case A_RELEASE_DONE
:
10625 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10635 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10637 struct block
*b0
, *b1
;
10640 * Catch errors reported by us and routines below us, and return NULL
10643 if (setjmp(cstate
->top_ctx
))
10649 if (!cstate
->is_atm
)
10650 bpf_error(cstate
, "'oam' supported only on raw ATM");
10652 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10653 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10655 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10660 if (!cstate
->is_atm
)
10661 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10663 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10664 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10666 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10672 * Get Q.2931 signalling messages for switched
10673 * virtual connection
10675 if (!cstate
->is_atm
)
10676 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10677 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10678 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10680 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10682 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10684 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10686 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10688 b0
= gen_atmtype_sc(cstate
);
10692 case A_METACONNECT
:
10693 if (!cstate
->is_atm
)
10694 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10695 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10696 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10698 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10700 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10702 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10704 b0
= gen_atmtype_metac(cstate
);