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>
41 #include "ethertype.h"
44 #include "ieee80211.h"
48 #include "pcap/ipnet.h"
49 #include "diag-control.h"
53 #if defined(__linux__)
54 #include <linux/types.h>
55 #include <linux/if_packet.h>
56 #include <linux/filter.h>
60 #define offsetof(s, e) ((size_t)&((s *)0)->e)
65 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
72 uint16_t u6_addr16
[8];
73 uint32_t u6_addr32
[4];
75 #define s6_addr in6_u.u6_addr8
76 #define s6_addr16 in6_u.u6_addr16
77 #define s6_addr32 in6_u.u6_addr32
78 #define s6_addr64 in6_u.u6_addr64
81 typedef unsigned short sa_family_t
;
83 #define __SOCKADDR_COMMON(sa_prefix) \
84 sa_family_t sa_prefix##family
86 /* Ditto, for IPv6. */
89 __SOCKADDR_COMMON (sin6_
);
90 uint16_t sin6_port
; /* Transport layer port # */
91 uint32_t sin6_flowinfo
; /* IPv6 flow information */
92 struct in6_addr sin6_addr
; /* IPv6 address */
95 #ifndef EAI_ADDRFAMILY
97 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
98 int ai_family
; /* PF_xxx */
99 int ai_socktype
; /* SOCK_xxx */
100 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
101 size_t ai_addrlen
; /* length of ai_addr */
102 char *ai_canonname
; /* canonical name for hostname */
103 struct sockaddr
*ai_addr
; /* binary address */
104 struct addrinfo
*ai_next
; /* next structure in linked list */
106 #endif /* EAI_ADDRFAMILY */
107 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
110 #include <netdb.h> /* for "struct addrinfo" */
112 #include <pcap/namedb.h>
114 #include "nametoaddr.h"
116 #define ETHERMTU 1500
118 #ifndef IPPROTO_HOPOPTS
119 #define IPPROTO_HOPOPTS 0
121 #ifndef IPPROTO_ROUTING
122 #define IPPROTO_ROUTING 43
124 #ifndef IPPROTO_FRAGMENT
125 #define IPPROTO_FRAGMENT 44
127 #ifndef IPPROTO_DSTOPTS
128 #define IPPROTO_DSTOPTS 60
131 #define IPPROTO_SCTP 132
134 #define GENEVE_PORT 6081
135 #define VXLAN_PORT 4789
139 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
143 #define ARCTYPE_IP_OLD 240 /* IP protocol */
144 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
147 #define ARCTYPE_IP 212 /* IP protocol */
148 #define ARCTYPE_ARP 213 /* address resolution protocol */
149 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
151 #define ARCTYPE_ATALK 221 /* Appletalk */
152 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
153 #define ARCTYPE_IPX 250 /* Novell IPX */
155 #define ARCTYPE_INET6 0xc4 /* IPng */
156 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
159 /* Based on UNI3.1 standard by ATM Forum */
161 /* ATM traffic types based on VPI=0 and (the following VCI */
162 #define VCI_PPC 0x05 /* Point-to-point signal msg */
163 #define VCI_BCC 0x02 /* Broadcast signal msg */
164 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
165 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
166 #define VCI_METAC 0x01 /* Meta signal msg */
167 #define VCI_ILMIC 0x10 /* ILMI msg */
169 /* Q.2931 signalling messages */
170 #define CALL_PROCEED 0x02 /* call proceeding */
171 #define CONNECT 0x07 /* connect */
172 #define CONNECT_ACK 0x0f /* connect_ack */
173 #define SETUP 0x05 /* setup */
174 #define RELEASE 0x4d /* release */
175 #define RELEASE_DONE 0x5a /* release_done */
176 #define RESTART 0x46 /* restart */
177 #define RESTART_ACK 0x4e /* restart ack */
178 #define STATUS 0x7d /* status */
179 #define STATUS_ENQ 0x75 /* status ack */
180 #define ADD_PARTY 0x80 /* add party */
181 #define ADD_PARTY_ACK 0x81 /* add party ack */
182 #define ADD_PARTY_REJ 0x82 /* add party rej */
183 #define DROP_PARTY 0x83 /* drop party */
184 #define DROP_PARTY_ACK 0x84 /* drop party ack */
186 /* Information Element Parameters in the signalling messages */
187 #define CAUSE 0x08 /* cause */
188 #define ENDPT_REF 0x54 /* endpoint reference */
189 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
190 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
191 #define CONNECT_ID 0x5a /* connection identifier */
192 #define QOS_PARA 0x5c /* quality of service parameters */
193 #define B_HIGHER 0x5d /* broadband higher layer information */
194 #define B_BEARER 0x5e /* broadband bearer capability */
195 #define B_LOWER 0x5f /* broadband lower information */
196 #define CALLING_PARTY 0x6c /* calling party number */
197 #define CALLED_PARTY 0x70 /* called party number */
201 /* Q.2931 signalling general messages format */
202 #define PROTO_POS 0 /* offset of protocol discriminator */
203 #define CALL_REF_POS 2 /* offset of call reference value */
204 #define MSG_TYPE_POS 5 /* offset of message type */
205 #define MSG_LEN_POS 7 /* offset of message length */
206 #define IE_BEGIN_POS 9 /* offset of first information element */
208 /* format of signalling messages */
211 #define FIELD_BEGIN_POS 4
214 /* SunATM header for ATM packet */
215 #define SUNATM_DIR_POS 0
216 #define SUNATM_VPI_POS 1
217 #define SUNATM_VCI_POS 2
218 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
220 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
221 #define PT_LANE 0x01 /* LANE */
222 #define PT_LLC 0x02 /* LLC encapsulation */
223 #define PT_ILMI 0x05 /* ILMI */
224 #define PT_QSAAL 0x06 /* Q.SAAL */
227 /* Types missing from some systems */
230 * Network layer protocol identifiers
233 #define ISO8473_CLNP 0x81
236 #define ISO9542_ESIS 0x82
238 #ifndef ISO9542X25_ESIS
239 #define ISO9542X25_ESIS 0x8a
241 #ifndef ISO10589_ISIS
242 #define ISO10589_ISIS 0x83
245 #define ISIS_L1_LAN_IIH 15
246 #define ISIS_L2_LAN_IIH 16
247 #define ISIS_PTP_IIH 17
248 #define ISIS_L1_LSP 18
249 #define ISIS_L2_LSP 20
250 #define ISIS_L1_CSNP 24
251 #define ISIS_L2_CSNP 25
252 #define ISIS_L1_PSNP 26
253 #define ISIS_L2_PSNP 27
255 #ifndef ISO8878A_CONS
256 #define ISO8878A_CONS 0x84
258 #ifndef ISO10747_IDRP
259 #define ISO10747_IDRP 0x85
263 #ifdef HAVE_OS_PROTO_H
264 #include "os-proto.h"
267 #define JMP(c) ((c)|BPF_JMP|BPF_K)
270 * "Push" the current value of the link-layer header type and link-layer
271 * header offset onto a "stack", and set a new value. (It's not a
272 * full-blown stack; we keep only the top two items.)
274 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
276 (cs)->prevlinktype = (cs)->linktype; \
277 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
278 (cs)->linktype = (new_linktype); \
279 (cs)->off_linkhdr.is_variable = (new_is_variable); \
280 (cs)->off_linkhdr.constant_part = (new_constant_part); \
281 (cs)->off_linkhdr.reg = (new_reg); \
282 (cs)->is_encap = 0; \
286 * Offset "not set" value.
288 #define OFFSET_NOT_SET 0xffffffffU
291 * Absolute offsets, which are offsets from the beginning of the raw
292 * packet data, are, in the general case, the sum of a variable value
293 * and a constant value; the variable value may be absent, in which
294 * case the offset is only the constant value, and the constant value
295 * may be zero, in which case the offset is only the variable value.
297 * bpf_abs_offset is a structure containing all that information:
299 * is_variable is 1 if there's a variable part.
301 * constant_part is the constant part of the value, possibly zero;
303 * if is_variable is 1, reg is the register number for a register
304 * containing the variable value if the register has been assigned,
314 * Value passed to gen_load_a() to indicate what the offset argument
315 * is relative to the beginning of.
318 OR_PACKET
, /* full packet data */
319 OR_LINKHDR
, /* link-layer header */
320 OR_PREVLINKHDR
, /* previous link-layer header */
321 OR_LLC
, /* 802.2 LLC header */
322 OR_PREVMPLSHDR
, /* previous MPLS header */
323 OR_LINKTYPE
, /* link-layer type */
324 OR_LINKPL
, /* link-layer payload */
325 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
326 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
327 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
331 * We divvy out chunks of memory rather than call malloc each time so
332 * we don't have to worry about leaking memory. It's probably
333 * not a big deal if all this memory was wasted but if this ever
334 * goes into a library that would probably not be a good idea.
336 * XXX - this *is* in a library....
339 #define CHUNK0SIZE 1024
346 * A chunk can store any of:
347 * - a string (guaranteed alignment 1 but present for completeness)
351 * For this simple allocator every allocated chunk gets rounded up to the
352 * alignment needed for any chunk.
363 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
365 /* Code generator state */
367 struct _compiler_state
{
378 int outermostlinktype
;
383 /* Hack for handling VLAN and MPLS stacks. */
384 u_int label_stack_depth
;
385 u_int vlan_stack_depth
;
391 * As errors are handled by a longjmp, anything allocated must
392 * be freed in the longjmp handler, so it must be reachable
395 * One thing that's allocated is the result of pcap_nametoaddrinfo();
396 * it must be freed with freeaddrinfo(). This variable points to
397 * any addrinfo structure that would need to be freed.
402 * Another thing that's allocated is the result of pcap_ether_aton();
403 * it must be freed with free(). This variable points to any
404 * address that would need to be freed.
409 * Various code constructs need to know the layout of the packet.
410 * These values give the necessary offsets from the beginning
411 * of the packet data.
415 * Absolute offset of the beginning of the link-layer header.
417 bpf_abs_offset off_linkhdr
;
420 * If we're checking a link-layer header for a packet encapsulated
421 * in another protocol layer, this is the equivalent information
422 * for the previous layers' link-layer header from the beginning
423 * of the raw packet data.
425 bpf_abs_offset off_prevlinkhdr
;
428 * This is the equivalent information for the outermost layers'
431 bpf_abs_offset off_outermostlinkhdr
;
434 * Absolute offset of the beginning of the link-layer payload.
436 bpf_abs_offset off_linkpl
;
439 * "off_linktype" is the offset to information in the link-layer
440 * header giving the packet type. This is an absolute offset
441 * from the beginning of the packet.
443 * For Ethernet, it's the offset of the Ethernet type field; this
444 * means that it must have a value that skips VLAN tags.
446 * For link-layer types that always use 802.2 headers, it's the
447 * offset of the LLC header; this means that it must have a value
448 * that skips VLAN tags.
450 * For PPP, it's the offset of the PPP type field.
452 * For Cisco HDLC, it's the offset of the CHDLC type field.
454 * For BSD loopback, it's the offset of the AF_ value.
456 * For Linux cooked sockets, it's the offset of the type field.
458 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
459 * encapsulation, in which case, IP is assumed.
461 bpf_abs_offset off_linktype
;
464 * TRUE if the link layer includes an ATM pseudo-header.
468 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
469 * causes us to generate code that checks for a Geneve or
470 * VXLAN header respectively and assume that later filters
471 * apply to the encapsulated payload.
476 * TRUE if we need variable length part of VLAN offset
478 int is_vlan_vloffset
;
481 * These are offsets for the ATM pseudo-header.
488 * These are offsets for the MTP2 fields.
494 * These are offsets for the MTP3 fields.
502 * This is the offset of the first byte after the ATM pseudo_header,
503 * or -1 if there is no ATM pseudo-header.
508 * These are offsets to the beginning of the network-layer header.
509 * They are relative to the beginning of the link-layer payload
510 * (i.e., they don't include off_linkhdr.constant_part or
511 * off_linkpl.constant_part).
513 * If the link layer never uses 802.2 LLC:
515 * "off_nl" and "off_nl_nosnap" are the same.
517 * If the link layer always uses 802.2 LLC:
519 * "off_nl" is the offset if there's a SNAP header following
522 * "off_nl_nosnap" is the offset if there's no SNAP header.
524 * If the link layer is Ethernet:
526 * "off_nl" is the offset if the packet is an Ethernet II packet
527 * (we assume no 802.3+802.2+SNAP);
529 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
530 * with an 802.2 header following it.
536 * Here we handle simple allocation of the scratch registers.
537 * If too many registers are alloc'd, the allocator punts.
539 int regused
[BPF_MEMWORDS
];
545 struct chunk chunks
[NCHUNKS
];
550 * For use by routines outside this file.
554 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
559 * If we've already set an error, don't override it.
560 * The lexical analyzer reports some errors by setting
561 * the error and then returning a LEX_ERROR token, which
562 * is not recognized by any grammar rule, and thus forces
563 * the parse to stop. We don't want the error reported
564 * by the lexical analyzer to be overwritten by the syntax
567 if (!cstate
->error_set
) {
569 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
572 cstate
->error_set
= 1;
577 * For use *ONLY* in routines in this file.
579 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
580 PCAP_PRINTFLIKE(2, 3);
583 static void PCAP_NORETURN
584 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
589 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
592 longjmp(cstate
->top_ctx
, 1);
599 static int init_linktype(compiler_state_t
*, pcap_t
*);
601 static void init_regs(compiler_state_t
*);
602 static int alloc_reg(compiler_state_t
*);
603 static void free_reg(compiler_state_t
*, int);
605 static void initchunks(compiler_state_t
*cstate
);
606 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
607 static void *newchunk(compiler_state_t
*cstate
, size_t);
608 static void freechunks(compiler_state_t
*cstate
);
609 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
610 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
611 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
612 static inline void syntax(compiler_state_t
*cstate
);
614 static void backpatch(struct block
*, struct block
*);
615 static void merge(struct block
*, struct block
*);
616 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
618 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
620 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
622 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
624 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
626 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
627 u_int
, bpf_u_int32
, bpf_u_int32
);
628 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
629 u_int
, const u_char
*);
630 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
631 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
632 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
634 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
636 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
637 static struct block
*gen_uncond(compiler_state_t
*, int);
638 static inline struct block
*gen_true(compiler_state_t
*);
639 static inline struct block
*gen_false(compiler_state_t
*);
640 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
641 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
642 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
643 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
644 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
645 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
646 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
647 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
648 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
649 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
651 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
652 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
653 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
654 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
655 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
656 int, bpf_u_int32
, u_int
, u_int
);
658 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
659 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
661 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
662 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
663 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
664 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
665 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
666 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
667 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
668 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
669 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
672 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
673 struct in6_addr
*, int, int, int);
676 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
677 struct addrinfo
*, int, int);
679 static struct block
*gen_ipfrag(compiler_state_t
*);
680 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
681 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
683 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
684 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
686 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
687 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
688 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
690 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
691 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
692 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
693 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
695 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
696 static int lookup_proto(compiler_state_t
*, const char *, int);
697 #if !defined(NO_PROTOCHAIN)
698 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
699 #endif /* !defined(NO_PROTOCHAIN) */
700 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
701 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
702 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
703 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
704 static struct block
*gen_len(compiler_state_t
*, int, int);
705 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
706 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
708 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
709 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
710 bpf_u_int32
, int, int);
711 static struct block
*gen_atmtype_llc(compiler_state_t
*);
712 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
715 initchunks(compiler_state_t
*cstate
)
719 for (i
= 0; i
< NCHUNKS
; i
++) {
720 cstate
->chunks
[i
].n_left
= 0;
721 cstate
->chunks
[i
].m
= NULL
;
723 cstate
->cur_chunk
= 0;
727 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
733 /* Round up to chunk alignment. */
734 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
736 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
737 if (n
> cp
->n_left
) {
739 k
= ++cstate
->cur_chunk
;
741 bpf_set_error(cstate
, "out of memory");
744 size
= CHUNK0SIZE
<< k
;
745 cp
->m
= (void *)malloc(size
);
747 bpf_set_error(cstate
, "out of memory");
750 memset((char *)cp
->m
, 0, size
);
753 bpf_set_error(cstate
, "out of memory");
758 return (void *)((char *)cp
->m
+ cp
->n_left
);
762 newchunk(compiler_state_t
*cstate
, size_t n
)
766 p
= newchunk_nolongjmp(cstate
, n
);
768 longjmp(cstate
->top_ctx
, 1);
775 freechunks(compiler_state_t
*cstate
)
779 for (i
= 0; i
< NCHUNKS
; ++i
)
780 if (cstate
->chunks
[i
].m
!= NULL
)
781 free(cstate
->chunks
[i
].m
);
785 * A strdup whose allocations are freed after code generation is over.
786 * This is used by the lexical analyzer, so it can't longjmp; it just
787 * returns NULL on an allocation error, and the callers must check
791 sdup(compiler_state_t
*cstate
, const char *s
)
793 size_t n
= strlen(s
) + 1;
794 char *cp
= newchunk_nolongjmp(cstate
, n
);
798 pcapint_strlcpy(cp
, s
, n
);
802 static inline struct block
*
803 new_block(compiler_state_t
*cstate
, int code
)
807 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
814 static inline struct slist
*
815 new_stmt(compiler_state_t
*cstate
, int code
)
819 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
825 static struct block
*
826 gen_retblk(compiler_state_t
*cstate
, int v
)
828 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
834 static inline PCAP_NORETURN_DEF
void
835 syntax(compiler_state_t
*cstate
)
837 bpf_error(cstate
, "syntax error in filter expression");
841 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
842 const char *buf
, int optimize
, bpf_u_int32 mask
)
848 compiler_state_t cstate
;
849 const char * volatile xbuf
= buf
;
850 yyscan_t scanner
= NULL
;
851 volatile YY_BUFFER_STATE in_buffer
= NULL
;
856 * If this pcap_t hasn't been activated, it doesn't have a
857 * link-layer type, so we can't use it.
860 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
861 "not-yet-activated pcap_t passed to pcap_compile");
867 * Initialize Winsock, asking for the latest version (2.2),
868 * as we may be calling Winsock routines to translate
869 * host names to addresses.
871 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
873 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
874 err
, "Error calling WSAStartup()");
881 * If the device on which we're capturing need to be notified
882 * that a new filter is being compiled, do so.
884 * This allows them to save a copy of it, in case, for example,
885 * they're implementing a form of remote packet capture, and
886 * want the remote machine to filter out the packets in which
887 * it's sending the packets it's captured.
889 * XXX - the fact that we happen to be compiling a filter
890 * doesn't necessarily mean we'll be installing it as the
891 * filter for this pcap_t; we might be running it from userland
892 * on captured packets to do packet classification. We really
893 * need a better way of handling this, but this is all that
894 * the WinPcap remote capture code did.
896 if (p
->save_current_filter_op
!= NULL
)
897 (p
->save_current_filter_op
)(p
, buf
);
901 cstate
.no_optimize
= 0;
906 cstate
.ic
.root
= NULL
;
907 cstate
.ic
.cur_mark
= 0;
909 cstate
.error_set
= 0;
912 cstate
.netmask
= mask
;
914 cstate
.snaplen
= pcap_snapshot(p
);
915 if (cstate
.snaplen
== 0) {
916 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
917 "snaplen of 0 rejects all packets");
922 if (pcap_lex_init(&scanner
) != 0) {
923 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
924 errno
, "can't initialize scanner");
928 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
931 * Associate the compiler state with the lexical analyzer
934 pcap_set_extra(&cstate
, scanner
);
936 if (init_linktype(&cstate
, p
) == -1) {
940 if (pcap_parse(scanner
, &cstate
) != 0) {
942 if (cstate
.ai
!= NULL
)
943 freeaddrinfo(cstate
.ai
);
945 if (cstate
.e
!= NULL
)
951 if (cstate
.ic
.root
== NULL
) {
953 * Catch errors reported by gen_retblk().
955 if (setjmp(cstate
.top_ctx
)) {
959 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
962 if (optimize
&& !cstate
.no_optimize
) {
963 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
968 if (cstate
.ic
.root
== NULL
||
969 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
970 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
971 "expression rejects all packets");
976 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
977 cstate
.ic
.root
, &len
, p
->errbuf
);
978 if (program
->bf_insns
== NULL
) {
983 program
->bf_len
= len
;
985 rc
= 0; /* We're all okay */
989 * Clean up everything for the lexical analyzer.
991 if (in_buffer
!= NULL
)
992 pcap__delete_buffer(in_buffer
, scanner
);
994 pcap_lex_destroy(scanner
);
997 * Clean up our own allocated memory.
1009 * entry point for using the compiler with no pcap open
1010 * pass in all the stuff that is needed explicitly instead.
1013 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1014 struct bpf_program
*program
,
1015 const char *buf
, int optimize
, bpf_u_int32 mask
)
1020 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1022 return (PCAP_ERROR
);
1023 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1029 * Clean up a "struct bpf_program" by freeing all the memory allocated
1033 pcap_freecode(struct bpf_program
*program
)
1035 program
->bf_len
= 0;
1036 if (program
->bf_insns
!= NULL
) {
1037 free((char *)program
->bf_insns
);
1038 program
->bf_insns
= NULL
;
1043 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1044 * which of the jt and jf fields has been resolved and which is a pointer
1045 * back to another unresolved block (or nil). At least one of the fields
1046 * in each block is already resolved.
1049 backpatch(struct block
*list
, struct block
*target
)
1066 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1067 * which of jt and jf is the link.
1070 merge(struct block
*b0
, struct block
*b1
)
1072 register struct block
**p
= &b0
;
1074 /* Find end of list. */
1076 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1078 /* Concatenate the lists. */
1083 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1085 struct block
*ppi_dlt_check
;
1088 * Catch errors reported by us and routines below us, and return -1
1091 if (setjmp(cstate
->top_ctx
))
1095 * Insert before the statements of the first (root) block any
1096 * statements needed to load the lengths of any variable-length
1097 * headers into registers.
1099 * XXX - a fancier strategy would be to insert those before the
1100 * statements of all blocks that use those lengths and that
1101 * have no predecessors that use them, so that we only compute
1102 * the lengths if we need them. There might be even better
1103 * approaches than that.
1105 * However, those strategies would be more complicated, and
1106 * as we don't generate code to compute a length if the
1107 * program has no tests that use the length, and as most
1108 * tests will probably use those lengths, we would just
1109 * postpone computing the lengths so that it's not done
1110 * for tests that fail early, and it's not clear that's
1113 insert_compute_vloffsets(cstate
, p
->head
);
1116 * For DLT_PPI captures, generate a check of the per-packet
1117 * DLT value to make sure it's DLT_IEEE802_11.
1119 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1120 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1121 * with appropriate Ethernet information and use that rather
1122 * than using something such as DLT_PPI where you don't know
1123 * the link-layer header type until runtime, which, in the
1124 * general case, would force us to generate both Ethernet *and*
1125 * 802.11 code (*and* anything else for which PPI is used)
1126 * and choose between them early in the BPF program?
1128 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
1129 if (ppi_dlt_check
!= NULL
)
1130 gen_and(ppi_dlt_check
, p
);
1132 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
1133 p
->sense
= !p
->sense
;
1134 backpatch(p
, gen_retblk(cstate
, 0));
1135 cstate
->ic
.root
= p
->head
;
1140 gen_and(struct block
*b0
, struct block
*b1
)
1142 backpatch(b0
, b1
->head
);
1143 b0
->sense
= !b0
->sense
;
1144 b1
->sense
= !b1
->sense
;
1146 b1
->sense
= !b1
->sense
;
1147 b1
->head
= b0
->head
;
1151 gen_or(struct block
*b0
, struct block
*b1
)
1153 b0
->sense
= !b0
->sense
;
1154 backpatch(b0
, b1
->head
);
1155 b0
->sense
= !b0
->sense
;
1157 b1
->head
= b0
->head
;
1161 gen_not(struct block
*b
)
1163 b
->sense
= !b
->sense
;
1166 static struct block
*
1167 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1168 u_int size
, bpf_u_int32 v
)
1170 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1173 static struct block
*
1174 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1175 u_int size
, bpf_u_int32 v
)
1177 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1180 static struct block
*
1181 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1182 u_int size
, bpf_u_int32 v
)
1184 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1187 static struct block
*
1188 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1189 u_int size
, bpf_u_int32 v
)
1191 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1194 static struct block
*
1195 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1196 u_int size
, bpf_u_int32 v
)
1198 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1201 static struct block
*
1202 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1203 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1205 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1208 static struct block
*
1209 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1210 u_int size
, const u_char
*v
)
1212 register struct block
*b
, *tmp
;
1216 register const u_char
*p
= &v
[size
- 4];
1218 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1226 register const u_char
*p
= &v
[size
- 2];
1228 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1236 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1245 * AND the field of size "size" at offset "offset" relative to the header
1246 * specified by "offrel" with "mask", and compare it with the value "v"
1247 * with the test specified by "jtype"; if "reverse" is true, the test
1248 * should test the opposite of "jtype".
1250 static struct block
*
1251 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1252 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1255 struct slist
*s
, *s2
;
1258 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1260 if (mask
!= 0xffffffff) {
1261 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1266 b
= new_block(cstate
, JMP(jtype
));
1269 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1275 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1277 cstate
->pcap_fddipad
= p
->fddipad
;
1280 * We start out with only one link-layer header.
1282 cstate
->outermostlinktype
= pcap_datalink(p
);
1283 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1284 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1285 cstate
->off_outermostlinkhdr
.reg
= -1;
1287 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1288 cstate
->off_prevlinkhdr
.constant_part
= 0;
1289 cstate
->off_prevlinkhdr
.is_variable
= 0;
1290 cstate
->off_prevlinkhdr
.reg
= -1;
1292 cstate
->linktype
= cstate
->outermostlinktype
;
1293 cstate
->off_linkhdr
.constant_part
= 0;
1294 cstate
->off_linkhdr
.is_variable
= 0;
1295 cstate
->off_linkhdr
.reg
= -1;
1300 cstate
->off_linkpl
.constant_part
= 0;
1301 cstate
->off_linkpl
.is_variable
= 0;
1302 cstate
->off_linkpl
.reg
= -1;
1304 cstate
->off_linktype
.constant_part
= 0;
1305 cstate
->off_linktype
.is_variable
= 0;
1306 cstate
->off_linktype
.reg
= -1;
1309 * Assume it's not raw ATM with a pseudo-header, for now.
1312 cstate
->off_vpi
= OFFSET_NOT_SET
;
1313 cstate
->off_vci
= OFFSET_NOT_SET
;
1314 cstate
->off_proto
= OFFSET_NOT_SET
;
1315 cstate
->off_payload
= OFFSET_NOT_SET
;
1318 * And not encapsulated with either Geneve or VXLAN.
1320 cstate
->is_encap
= 0;
1323 * No variable length VLAN offset by default
1325 cstate
->is_vlan_vloffset
= 0;
1328 * And assume we're not doing SS7.
1330 cstate
->off_li
= OFFSET_NOT_SET
;
1331 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1332 cstate
->off_sio
= OFFSET_NOT_SET
;
1333 cstate
->off_opc
= OFFSET_NOT_SET
;
1334 cstate
->off_dpc
= OFFSET_NOT_SET
;
1335 cstate
->off_sls
= OFFSET_NOT_SET
;
1337 cstate
->label_stack_depth
= 0;
1338 cstate
->vlan_stack_depth
= 0;
1340 switch (cstate
->linktype
) {
1343 cstate
->off_linktype
.constant_part
= 2;
1344 cstate
->off_linkpl
.constant_part
= 6;
1345 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1346 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1349 case DLT_ARCNET_LINUX
:
1350 cstate
->off_linktype
.constant_part
= 4;
1351 cstate
->off_linkpl
.constant_part
= 8;
1352 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1353 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1357 cstate
->off_linktype
.constant_part
= 12;
1358 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1359 cstate
->off_nl
= 0; /* Ethernet II */
1360 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1365 * SLIP doesn't have a link level type. The 16 byte
1366 * header is hacked into our SLIP driver.
1368 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1369 cstate
->off_linkpl
.constant_part
= 16;
1371 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1374 case DLT_SLIP_BSDOS
:
1375 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1376 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1378 cstate
->off_linkpl
.constant_part
= 24;
1380 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1385 cstate
->off_linktype
.constant_part
= 0;
1386 cstate
->off_linkpl
.constant_part
= 4;
1388 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1392 cstate
->off_linktype
.constant_part
= 0;
1393 cstate
->off_linkpl
.constant_part
= 12;
1395 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1400 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1401 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1402 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1403 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1404 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1406 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1411 * This does no include the Ethernet header, and
1412 * only covers session state.
1414 cstate
->off_linktype
.constant_part
= 6;
1415 cstate
->off_linkpl
.constant_part
= 8;
1417 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1421 cstate
->off_linktype
.constant_part
= 5;
1422 cstate
->off_linkpl
.constant_part
= 24;
1424 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1429 * FDDI doesn't really have a link-level type field.
1430 * We set "off_linktype" to the offset of the LLC header.
1432 * To check for Ethernet types, we assume that SSAP = SNAP
1433 * is being used and pick out the encapsulated Ethernet type.
1434 * XXX - should we generate code to check for SNAP?
1436 cstate
->off_linktype
.constant_part
= 13;
1437 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1438 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1439 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1440 cstate
->off_nl
= 8; /* 802.2+SNAP */
1441 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1446 * Token Ring doesn't really have a link-level type field.
1447 * We set "off_linktype" to the offset of the LLC header.
1449 * To check for Ethernet types, we assume that SSAP = SNAP
1450 * is being used and pick out the encapsulated Ethernet type.
1451 * XXX - should we generate code to check for SNAP?
1453 * XXX - the header is actually variable-length.
1454 * Some various Linux patched versions gave 38
1455 * as "off_linktype" and 40 as "off_nl"; however,
1456 * if a token ring packet has *no* routing
1457 * information, i.e. is not source-routed, the correct
1458 * values are 20 and 22, as they are in the vanilla code.
1460 * A packet is source-routed iff the uppermost bit
1461 * of the first byte of the source address, at an
1462 * offset of 8, has the uppermost bit set. If the
1463 * packet is source-routed, the total number of bytes
1464 * of routing information is 2 plus bits 0x1F00 of
1465 * the 16-bit value at an offset of 14 (shifted right
1466 * 8 - figure out which byte that is).
1468 cstate
->off_linktype
.constant_part
= 14;
1469 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1470 cstate
->off_nl
= 8; /* 802.2+SNAP */
1471 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1474 case DLT_PRISM_HEADER
:
1475 case DLT_IEEE802_11_RADIO_AVS
:
1476 case DLT_IEEE802_11_RADIO
:
1477 cstate
->off_linkhdr
.is_variable
= 1;
1478 /* Fall through, 802.11 doesn't have a variable link
1479 * prefix but is otherwise the same. */
1482 case DLT_IEEE802_11
:
1484 * 802.11 doesn't really have a link-level type field.
1485 * We set "off_linktype.constant_part" to the offset of
1488 * To check for Ethernet types, we assume that SSAP = SNAP
1489 * is being used and pick out the encapsulated Ethernet type.
1490 * XXX - should we generate code to check for SNAP?
1492 * We also handle variable-length radio headers here.
1493 * The Prism header is in theory variable-length, but in
1494 * practice it's always 144 bytes long. However, some
1495 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1496 * sometimes or always supply an AVS header, so we
1497 * have to check whether the radio header is a Prism
1498 * header or an AVS header, so, in practice, it's
1501 cstate
->off_linktype
.constant_part
= 24;
1502 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1503 cstate
->off_linkpl
.is_variable
= 1;
1504 cstate
->off_nl
= 8; /* 802.2+SNAP */
1505 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1510 * At the moment we treat PPI the same way that we treat
1511 * normal Radiotap encoded packets. The difference is in
1512 * the function that generates the code at the beginning
1513 * to compute the header length. Since this code generator
1514 * of PPI supports bare 802.11 encapsulation only (i.e.
1515 * the encapsulated DLT should be DLT_IEEE802_11) we
1516 * generate code to check for this too.
1518 cstate
->off_linktype
.constant_part
= 24;
1519 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1520 cstate
->off_linkpl
.is_variable
= 1;
1521 cstate
->off_linkhdr
.is_variable
= 1;
1522 cstate
->off_nl
= 8; /* 802.2+SNAP */
1523 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1526 case DLT_ATM_RFC1483
:
1527 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1529 * assume routed, non-ISO PDUs
1530 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1532 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1533 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1534 * latter would presumably be treated the way PPPoE
1535 * should be, so you can do "pppoe and udp port 2049"
1536 * or "pppoa and tcp port 80" and have it check for
1537 * PPPo{A,E} and a PPP protocol of IP and....
1539 cstate
->off_linktype
.constant_part
= 0;
1540 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1541 cstate
->off_nl
= 8; /* 802.2+SNAP */
1542 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1547 * Full Frontal ATM; you get AALn PDUs with an ATM
1551 cstate
->off_vpi
= SUNATM_VPI_POS
;
1552 cstate
->off_vci
= SUNATM_VCI_POS
;
1553 cstate
->off_proto
= PROTO_POS
;
1554 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1555 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1556 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1557 cstate
->off_nl
= 8; /* 802.2+SNAP */
1558 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1564 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1565 cstate
->off_linkpl
.constant_part
= 0;
1567 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1570 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1571 cstate
->off_linktype
.constant_part
= 14;
1572 cstate
->off_linkpl
.constant_part
= 16;
1574 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1577 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1578 cstate
->off_linktype
.constant_part
= 0;
1579 cstate
->off_linkpl
.constant_part
= 20;
1581 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1586 * LocalTalk does have a 1-byte type field in the LLAP header,
1587 * but really it just indicates whether there is a "short" or
1588 * "long" DDP packet following.
1590 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1591 cstate
->off_linkpl
.constant_part
= 0;
1593 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1596 case DLT_IP_OVER_FC
:
1598 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1599 * link-level type field. We set "off_linktype" to the
1600 * offset of the LLC header.
1602 * To check for Ethernet types, we assume that SSAP = SNAP
1603 * is being used and pick out the encapsulated Ethernet type.
1604 * XXX - should we generate code to check for SNAP? RFC
1605 * 2625 says SNAP should be used.
1607 cstate
->off_linktype
.constant_part
= 16;
1608 cstate
->off_linkpl
.constant_part
= 16;
1609 cstate
->off_nl
= 8; /* 802.2+SNAP */
1610 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1615 * XXX - we should set this to handle SNAP-encapsulated
1616 * frames (NLPID of 0x80).
1618 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1619 cstate
->off_linkpl
.constant_part
= 0;
1621 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1625 * the only BPF-interesting FRF.16 frames are non-control frames;
1626 * Frame Relay has a variable length link-layer
1627 * so lets start with offset 4 for now and increments later on (FIXME);
1630 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1631 cstate
->off_linkpl
.constant_part
= 0;
1633 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1636 case DLT_APPLE_IP_OVER_IEEE1394
:
1637 cstate
->off_linktype
.constant_part
= 16;
1638 cstate
->off_linkpl
.constant_part
= 18;
1640 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1643 case DLT_SYMANTEC_FIREWALL
:
1644 cstate
->off_linktype
.constant_part
= 6;
1645 cstate
->off_linkpl
.constant_part
= 44;
1646 cstate
->off_nl
= 0; /* Ethernet II */
1647 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1651 cstate
->off_linktype
.constant_part
= 0;
1652 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1653 cstate
->off_linkpl
.is_variable
= 1;
1655 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1658 case DLT_JUNIPER_MFR
:
1659 case DLT_JUNIPER_MLFR
:
1660 case DLT_JUNIPER_MLPPP
:
1661 case DLT_JUNIPER_PPP
:
1662 case DLT_JUNIPER_CHDLC
:
1663 case DLT_JUNIPER_FRELAY
:
1664 cstate
->off_linktype
.constant_part
= 4;
1665 cstate
->off_linkpl
.constant_part
= 4;
1667 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1670 case DLT_JUNIPER_ATM1
:
1671 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1672 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1674 cstate
->off_nl_nosnap
= 10;
1677 case DLT_JUNIPER_ATM2
:
1678 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1679 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1681 cstate
->off_nl_nosnap
= 10;
1684 /* frames captured on a Juniper PPPoE service PIC
1685 * contain raw ethernet frames */
1686 case DLT_JUNIPER_PPPOE
:
1687 case DLT_JUNIPER_ETHER
:
1688 cstate
->off_linkpl
.constant_part
= 14;
1689 cstate
->off_linktype
.constant_part
= 16;
1690 cstate
->off_nl
= 18; /* Ethernet II */
1691 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1694 case DLT_JUNIPER_PPPOE_ATM
:
1695 cstate
->off_linktype
.constant_part
= 4;
1696 cstate
->off_linkpl
.constant_part
= 6;
1698 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1701 case DLT_JUNIPER_GGSN
:
1702 cstate
->off_linktype
.constant_part
= 6;
1703 cstate
->off_linkpl
.constant_part
= 12;
1705 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1708 case DLT_JUNIPER_ES
:
1709 cstate
->off_linktype
.constant_part
= 6;
1710 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1711 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1712 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1715 case DLT_JUNIPER_MONITOR
:
1716 cstate
->off_linktype
.constant_part
= 12;
1717 cstate
->off_linkpl
.constant_part
= 12;
1718 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1719 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1722 case DLT_BACNET_MS_TP
:
1723 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1724 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1725 cstate
->off_nl
= OFFSET_NOT_SET
;
1726 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1729 case DLT_JUNIPER_SERVICES
:
1730 cstate
->off_linktype
.constant_part
= 12;
1731 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1732 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1733 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1736 case DLT_JUNIPER_VP
:
1737 cstate
->off_linktype
.constant_part
= 18;
1738 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1739 cstate
->off_nl
= OFFSET_NOT_SET
;
1740 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1743 case DLT_JUNIPER_ST
:
1744 cstate
->off_linktype
.constant_part
= 18;
1745 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1746 cstate
->off_nl
= OFFSET_NOT_SET
;
1747 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1750 case DLT_JUNIPER_ISM
:
1751 cstate
->off_linktype
.constant_part
= 8;
1752 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1753 cstate
->off_nl
= OFFSET_NOT_SET
;
1754 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1757 case DLT_JUNIPER_VS
:
1758 case DLT_JUNIPER_SRX_E2E
:
1759 case DLT_JUNIPER_FIBRECHANNEL
:
1760 case DLT_JUNIPER_ATM_CEMIC
:
1761 cstate
->off_linktype
.constant_part
= 8;
1762 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1763 cstate
->off_nl
= OFFSET_NOT_SET
;
1764 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1769 cstate
->off_li_hsl
= 4;
1770 cstate
->off_sio
= 3;
1771 cstate
->off_opc
= 4;
1772 cstate
->off_dpc
= 4;
1773 cstate
->off_sls
= 7;
1774 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1775 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1776 cstate
->off_nl
= OFFSET_NOT_SET
;
1777 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1780 case DLT_MTP2_WITH_PHDR
:
1782 cstate
->off_li_hsl
= 8;
1783 cstate
->off_sio
= 7;
1784 cstate
->off_opc
= 8;
1785 cstate
->off_dpc
= 8;
1786 cstate
->off_sls
= 11;
1787 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1788 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1789 cstate
->off_nl
= OFFSET_NOT_SET
;
1790 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1794 cstate
->off_li
= 22;
1795 cstate
->off_li_hsl
= 24;
1796 cstate
->off_sio
= 23;
1797 cstate
->off_opc
= 24;
1798 cstate
->off_dpc
= 24;
1799 cstate
->off_sls
= 27;
1800 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1801 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1802 cstate
->off_nl
= OFFSET_NOT_SET
;
1803 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1807 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1808 cstate
->off_linkpl
.constant_part
= 4;
1810 cstate
->off_nl_nosnap
= 0;
1815 * Currently, only raw "link[N:M]" filtering is supported.
1817 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1818 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1819 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1820 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1824 cstate
->off_linktype
.constant_part
= 1;
1825 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1827 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1830 case DLT_NETANALYZER
:
1831 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1832 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1833 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1834 cstate
->off_nl
= 0; /* Ethernet II */
1835 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1838 case DLT_NETANALYZER_TRANSPARENT
:
1839 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1840 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1841 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1842 cstate
->off_nl
= 0; /* Ethernet II */
1843 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1848 * For values in the range in which we've assigned new
1849 * DLT_ values, only raw "link[N:M]" filtering is supported.
1851 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1852 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1853 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1854 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1855 cstate
->off_nl
= OFFSET_NOT_SET
;
1856 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1858 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1859 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1865 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1870 * Load a value relative to the specified absolute offset.
1872 static struct slist
*
1873 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1874 u_int offset
, u_int size
)
1876 struct slist
*s
, *s2
;
1878 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1881 * If "s" is non-null, it has code to arrange that the X register
1882 * contains the variable part of the absolute offset, so we
1883 * generate a load relative to that, with an offset of
1884 * abs_offset->constant_part + offset.
1886 * Otherwise, we can do an absolute load with an offset of
1887 * abs_offset->constant_part + offset.
1891 * "s" points to a list of statements that puts the
1892 * variable part of the absolute offset into the X register.
1893 * Do an indirect load, to use the X register as an offset.
1895 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1896 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1900 * There is no variable part of the absolute offset, so
1901 * just do an absolute load.
1903 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1904 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1910 * Load a value relative to the beginning of the specified header.
1912 static struct slist
*
1913 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1916 struct slist
*s
, *s2
;
1919 * Squelch warnings from compilers that *don't* assume that
1920 * offrel always has a valid enum value and therefore don't
1921 * assume that we'll always go through one of the case arms.
1923 * If we have a default case, compilers that *do* assume that
1924 * will then complain about the default case code being
1927 * Damned if you do, damned if you don't.
1934 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1939 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1942 case OR_PREVLINKHDR
:
1943 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1947 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1950 case OR_PREVMPLSHDR
:
1951 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1955 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1958 case OR_LINKPL_NOSNAP
:
1959 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1963 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1968 * Load the X register with the length of the IPv4 header
1969 * (plus the offset of the link-layer header, if it's
1970 * preceded by a variable-length header such as a radio
1971 * header), in bytes.
1973 s
= gen_loadx_iphdrlen(cstate
);
1976 * Load the item at {offset of the link-layer payload} +
1977 * {offset, relative to the start of the link-layer
1978 * payload, of the IPv4 header} + {length of the IPv4 header} +
1979 * {specified offset}.
1981 * If the offset of the link-layer payload is variable,
1982 * the variable part of that offset is included in the
1983 * value in the X register, and we include the constant
1984 * part in the offset of the load.
1986 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1987 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1992 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1999 * Generate code to load into the X register the sum of the length of
2000 * the IPv4 header and the variable part of the offset of the link-layer
2003 static struct slist
*
2004 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2006 struct slist
*s
, *s2
;
2008 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2011 * The offset of the link-layer payload has a variable
2012 * part. "s" points to a list of statements that put
2013 * the variable part of that offset into the X register.
2015 * The 4*([k]&0xf) addressing mode can't be used, as we
2016 * don't have a constant offset, so we have to load the
2017 * value in question into the A register and add to it
2018 * the value from the X register.
2020 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2021 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2023 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2026 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2031 * The A register now contains the length of the IP header.
2032 * We need to add to it the variable part of the offset of
2033 * the link-layer payload, which is still in the X
2034 * register, and move the result into the X register.
2036 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2037 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2040 * The offset of the link-layer payload is a constant,
2041 * so no code was generated to load the (nonexistent)
2042 * variable part of that offset.
2044 * This means we can use the 4*([k]&0xf) addressing
2045 * mode. Load the length of the IPv4 header, which
2046 * is at an offset of cstate->off_nl from the beginning of
2047 * the link-layer payload, and thus at an offset of
2048 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2049 * of the raw packet data, using that addressing mode.
2051 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2052 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2058 static struct block
*
2059 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2064 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2066 b
= new_block(cstate
, JMP(BPF_JEQ
));
2072 static inline struct block
*
2073 gen_true(compiler_state_t
*cstate
)
2075 return gen_uncond(cstate
, 1);
2078 static inline struct block
*
2079 gen_false(compiler_state_t
*cstate
)
2081 return gen_uncond(cstate
, 0);
2085 * Byte-swap a 32-bit number.
2086 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
2087 * big-endian platforms.)
2089 #define SWAPLONG(y) \
2090 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
2093 * Generate code to match a particular packet type.
2095 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2096 * value, if <= ETHERMTU. We use that to determine whether to
2097 * match the type/length field or to check the type/length field for
2098 * a value <= ETHERMTU to see whether it's a type field and then do
2099 * the appropriate test.
2101 static struct block
*
2102 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2104 struct block
*b0
, *b1
;
2110 case LLCSAP_NETBEUI
:
2112 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2113 * so we check the DSAP and SSAP.
2115 * LLCSAP_IP checks for IP-over-802.2, rather
2116 * than IP-over-Ethernet or IP-over-SNAP.
2118 * XXX - should we check both the DSAP and the
2119 * SSAP, like this, or should we check just the
2120 * DSAP, as we do for other types <= ETHERMTU
2121 * (i.e., other SAP values)?
2123 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2125 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2133 * Ethernet_II frames, which are Ethernet
2134 * frames with a frame type of ETHERTYPE_IPX;
2136 * Ethernet_802.3 frames, which are 802.3
2137 * frames (i.e., the type/length field is
2138 * a length field, <= ETHERMTU, rather than
2139 * a type field) with the first two bytes
2140 * after the Ethernet/802.3 header being
2143 * Ethernet_802.2 frames, which are 802.3
2144 * frames with an 802.2 LLC header and
2145 * with the IPX LSAP as the DSAP in the LLC
2148 * Ethernet_SNAP frames, which are 802.3
2149 * frames with an LLC header and a SNAP
2150 * header and with an OUI of 0x000000
2151 * (encapsulated Ethernet) and a protocol
2152 * ID of ETHERTYPE_IPX in the SNAP header.
2154 * XXX - should we generate the same code both
2155 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2159 * This generates code to check both for the
2160 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2162 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2163 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2167 * Now we add code to check for SNAP frames with
2168 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2170 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2174 * Now we generate code to check for 802.3
2175 * frames in general.
2177 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2181 * Now add the check for 802.3 frames before the
2182 * check for Ethernet_802.2 and Ethernet_802.3,
2183 * as those checks should only be done on 802.3
2184 * frames, not on Ethernet frames.
2189 * Now add the check for Ethernet_II frames, and
2190 * do that before checking for the other frame
2193 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2197 case ETHERTYPE_ATALK
:
2198 case ETHERTYPE_AARP
:
2200 * EtherTalk (AppleTalk protocols on Ethernet link
2201 * layer) may use 802.2 encapsulation.
2205 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2206 * we check for an Ethernet type field less than
2207 * 1500, which means it's an 802.3 length field.
2209 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2213 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2214 * SNAP packets with an organization code of
2215 * 0x080007 (Apple, for Appletalk) and a protocol
2216 * type of ETHERTYPE_ATALK (Appletalk).
2218 * 802.2-encapsulated ETHERTYPE_AARP packets are
2219 * SNAP packets with an organization code of
2220 * 0x000000 (encapsulated Ethernet) and a protocol
2221 * type of ETHERTYPE_AARP (Appletalk ARP).
2223 if (ll_proto
== ETHERTYPE_ATALK
)
2224 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2225 else /* ll_proto == ETHERTYPE_AARP */
2226 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2230 * Check for Ethernet encapsulation (Ethertalk
2231 * phase 1?); we just check for the Ethernet
2234 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2240 if (ll_proto
<= ETHERMTU
) {
2242 * This is an LLC SAP value, so the frames
2243 * that match would be 802.2 frames.
2244 * Check that the frame is an 802.2 frame
2245 * (i.e., that the length/type field is
2246 * a length field, <= ETHERMTU) and
2247 * then check the DSAP.
2249 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2251 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2256 * This is an Ethernet type, so compare
2257 * the length/type field with it (if
2258 * the frame is an 802.2 frame, the length
2259 * field will be <= ETHERMTU, and, as
2260 * "ll_proto" is > ETHERMTU, this test
2261 * will fail and the frame won't match,
2262 * which is what we want).
2264 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2269 static struct block
*
2270 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2273 * For DLT_NULL, the link-layer header is a 32-bit word
2274 * containing an AF_ value in *host* byte order, and for
2275 * DLT_ENC, the link-layer header begins with a 32-bit
2276 * word containing an AF_ value in host byte order.
2278 * In addition, if we're reading a saved capture file,
2279 * the host byte order in the capture may not be the
2280 * same as the host byte order on this machine.
2282 * For DLT_LOOP, the link-layer header is a 32-bit
2283 * word containing an AF_ value in *network* byte order.
2285 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2287 * The AF_ value is in host byte order, but the BPF
2288 * interpreter will convert it to network byte order.
2290 * If this is a save file, and it's from a machine
2291 * with the opposite byte order to ours, we byte-swap
2294 * Then we run it through "htonl()", and generate
2295 * code to compare against the result.
2297 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2298 ll_proto
= SWAPLONG(ll_proto
);
2299 ll_proto
= htonl(ll_proto
);
2301 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2305 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2306 * or IPv6 then we have an error.
2308 static struct block
*
2309 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2314 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2317 case ETHERTYPE_IPV6
:
2318 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2325 return gen_false(cstate
);
2329 * Generate code to match a particular packet type.
2331 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2332 * value, if <= ETHERMTU. We use that to determine whether to
2333 * match the type field or to check the type field for the special
2334 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2336 static struct block
*
2337 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2339 struct block
*b0
, *b1
;
2345 case LLCSAP_NETBEUI
:
2347 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2348 * so we check the DSAP and SSAP.
2350 * LLCSAP_IP checks for IP-over-802.2, rather
2351 * than IP-over-Ethernet or IP-over-SNAP.
2353 * XXX - should we check both the DSAP and the
2354 * SSAP, like this, or should we check just the
2355 * DSAP, as we do for other types <= ETHERMTU
2356 * (i.e., other SAP values)?
2358 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2359 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2365 * Ethernet_II frames, which are Ethernet
2366 * frames with a frame type of ETHERTYPE_IPX;
2368 * Ethernet_802.3 frames, which have a frame
2369 * type of LINUX_SLL_P_802_3;
2371 * Ethernet_802.2 frames, which are 802.3
2372 * frames with an 802.2 LLC header (i.e, have
2373 * a frame type of LINUX_SLL_P_802_2) and
2374 * with the IPX LSAP as the DSAP in the LLC
2377 * Ethernet_SNAP frames, which are 802.3
2378 * frames with an LLC header and a SNAP
2379 * header and with an OUI of 0x000000
2380 * (encapsulated Ethernet) and a protocol
2381 * ID of ETHERTYPE_IPX in the SNAP header.
2383 * First, do the checks on LINUX_SLL_P_802_2
2384 * frames; generate the check for either
2385 * Ethernet_802.2 or Ethernet_SNAP frames, and
2386 * then put a check for LINUX_SLL_P_802_2 frames
2389 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2390 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2392 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2396 * Now check for 802.3 frames and OR that with
2397 * the previous test.
2399 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2403 * Now add the check for Ethernet_II frames, and
2404 * do that before checking for the other frame
2407 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2411 case ETHERTYPE_ATALK
:
2412 case ETHERTYPE_AARP
:
2414 * EtherTalk (AppleTalk protocols on Ethernet link
2415 * layer) may use 802.2 encapsulation.
2419 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2420 * we check for the 802.2 protocol type in the
2421 * "Ethernet type" field.
2423 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2426 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2427 * SNAP packets with an organization code of
2428 * 0x080007 (Apple, for Appletalk) and a protocol
2429 * type of ETHERTYPE_ATALK (Appletalk).
2431 * 802.2-encapsulated ETHERTYPE_AARP packets are
2432 * SNAP packets with an organization code of
2433 * 0x000000 (encapsulated Ethernet) and a protocol
2434 * type of ETHERTYPE_AARP (Appletalk ARP).
2436 if (ll_proto
== ETHERTYPE_ATALK
)
2437 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2438 else /* ll_proto == ETHERTYPE_AARP */
2439 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2443 * Check for Ethernet encapsulation (Ethertalk
2444 * phase 1?); we just check for the Ethernet
2447 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2453 if (ll_proto
<= ETHERMTU
) {
2455 * This is an LLC SAP value, so the frames
2456 * that match would be 802.2 frames.
2457 * Check for the 802.2 protocol type
2458 * in the "Ethernet type" field, and
2459 * then check the DSAP.
2461 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2462 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2468 * This is an Ethernet type, so compare
2469 * the length/type field with it (if
2470 * the frame is an 802.2 frame, the length
2471 * field will be <= ETHERMTU, and, as
2472 * "ll_proto" is > ETHERMTU, this test
2473 * will fail and the frame won't match,
2474 * which is what we want).
2476 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2482 * Load a value relative to the beginning of the link-layer header after the
2485 static struct slist
*
2486 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2488 struct slist
*s1
, *s2
;
2491 * Generate code to load the length of the pflog header into
2492 * the register assigned to hold that length, if one has been
2493 * assigned. (If one hasn't been assigned, no code we've
2494 * generated uses that prefix, so we don't need to generate any
2497 if (cstate
->off_linkpl
.reg
!= -1) {
2499 * The length is in the first byte of the header.
2501 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2505 * Round it up to a multiple of 4.
2506 * Add 3, and clear the lower 2 bits.
2508 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2511 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2512 s2
->s
.k
= 0xfffffffc;
2516 * Now allocate a register to hold that value and store
2519 s2
= new_stmt(cstate
, BPF_ST
);
2520 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2524 * Now move it into the X register.
2526 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2534 static struct slist
*
2535 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2537 struct slist
*s1
, *s2
;
2538 struct slist
*sjeq_avs_cookie
;
2539 struct slist
*sjcommon
;
2542 * This code is not compatible with the optimizer, as
2543 * we are generating jmp instructions within a normal
2544 * slist of instructions
2546 cstate
->no_optimize
= 1;
2549 * Generate code to load the length of the radio header into
2550 * the register assigned to hold that length, if one has been
2551 * assigned. (If one hasn't been assigned, no code we've
2552 * generated uses that prefix, so we don't need to generate any
2555 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2556 * or always use the AVS header rather than the Prism header.
2557 * We load a 4-byte big-endian value at the beginning of the
2558 * raw packet data, and see whether, when masked with 0xFFFFF000,
2559 * it's equal to 0x80211000. If so, that indicates that it's
2560 * an AVS header (the masked-out bits are the version number).
2561 * Otherwise, it's a Prism header.
2563 * XXX - the Prism header is also, in theory, variable-length,
2564 * but no known software generates headers that aren't 144
2567 if (cstate
->off_linkhdr
.reg
!= -1) {
2571 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2575 * AND it with 0xFFFFF000.
2577 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2578 s2
->s
.k
= 0xFFFFF000;
2582 * Compare with 0x80211000.
2584 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2585 sjeq_avs_cookie
->s
.k
= 0x80211000;
2586 sappend(s1
, sjeq_avs_cookie
);
2591 * The 4 bytes at an offset of 4 from the beginning of
2592 * the AVS header are the length of the AVS header.
2593 * That field is big-endian.
2595 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2598 sjeq_avs_cookie
->s
.jt
= s2
;
2601 * Now jump to the code to allocate a register
2602 * into which to save the header length and
2603 * store the length there. (The "jump always"
2604 * instruction needs to have the k field set;
2605 * it's added to the PC, so, as we're jumping
2606 * over a single instruction, it should be 1.)
2608 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2610 sappend(s1
, sjcommon
);
2613 * Now for the code that handles the Prism header.
2614 * Just load the length of the Prism header (144)
2615 * into the A register. Have the test for an AVS
2616 * header branch here if we don't have an AVS header.
2618 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2621 sjeq_avs_cookie
->s
.jf
= s2
;
2624 * Now allocate a register to hold that value and store
2625 * it. The code for the AVS header will jump here after
2626 * loading the length of the AVS header.
2628 s2
= new_stmt(cstate
, BPF_ST
);
2629 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2631 sjcommon
->s
.jf
= s2
;
2634 * Now move it into the X register.
2636 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2644 static struct slist
*
2645 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2647 struct slist
*s1
, *s2
;
2650 * Generate code to load the length of the AVS header into
2651 * the register assigned to hold that length, if one has been
2652 * assigned. (If one hasn't been assigned, no code we've
2653 * generated uses that prefix, so we don't need to generate any
2656 if (cstate
->off_linkhdr
.reg
!= -1) {
2658 * The 4 bytes at an offset of 4 from the beginning of
2659 * the AVS header are the length of the AVS header.
2660 * That field is big-endian.
2662 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2666 * Now allocate a register to hold that value and store
2669 s2
= new_stmt(cstate
, BPF_ST
);
2670 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2674 * Now move it into the X register.
2676 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2684 static struct slist
*
2685 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2687 struct slist
*s1
, *s2
;
2690 * Generate code to load the length of the radiotap header into
2691 * the register assigned to hold that length, if one has been
2692 * assigned. (If one hasn't been assigned, no code we've
2693 * generated uses that prefix, so we don't need to generate any
2696 if (cstate
->off_linkhdr
.reg
!= -1) {
2698 * The 2 bytes at offsets of 2 and 3 from the beginning
2699 * of the radiotap header are the length of the radiotap
2700 * header; unfortunately, it's little-endian, so we have
2701 * to load it a byte at a time and construct the value.
2705 * Load the high-order byte, at an offset of 3, shift it
2706 * left a byte, and put the result in the X register.
2708 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2710 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2713 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2717 * Load the next byte, at an offset of 2, and OR the
2718 * value from the X register into it.
2720 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2723 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2727 * Now allocate a register to hold that value and store
2730 s2
= new_stmt(cstate
, BPF_ST
);
2731 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2735 * Now move it into the X register.
2737 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2746 * At the moment we treat PPI as normal Radiotap encoded
2747 * packets. The difference is in the function that generates
2748 * the code at the beginning to compute the header length.
2749 * Since this code generator of PPI supports bare 802.11
2750 * encapsulation only (i.e. the encapsulated DLT should be
2751 * DLT_IEEE802_11) we generate code to check for this too;
2752 * that's done in finish_parse().
2754 static struct slist
*
2755 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2757 struct slist
*s1
, *s2
;
2760 * Generate code to load the length of the radiotap header
2761 * into the register assigned to hold that length, if one has
2764 if (cstate
->off_linkhdr
.reg
!= -1) {
2766 * The 2 bytes at offsets of 2 and 3 from the beginning
2767 * of the radiotap header are the length of the radiotap
2768 * header; unfortunately, it's little-endian, so we have
2769 * to load it a byte at a time and construct the value.
2773 * Load the high-order byte, at an offset of 3, shift it
2774 * left a byte, and put the result in the X register.
2776 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2778 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2781 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2785 * Load the next byte, at an offset of 2, and OR the
2786 * value from the X register into it.
2788 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2791 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2795 * Now allocate a register to hold that value and store
2798 s2
= new_stmt(cstate
, BPF_ST
);
2799 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2803 * Now move it into the X register.
2805 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2814 * Load a value relative to the beginning of the link-layer header after the 802.11
2815 * header, i.e. LLC_SNAP.
2816 * The link-layer header doesn't necessarily begin at the beginning
2817 * of the packet data; there might be a variable-length prefix containing
2818 * radio information.
2820 static struct slist
*
2821 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2824 struct slist
*sjset_data_frame_1
;
2825 struct slist
*sjset_data_frame_2
;
2826 struct slist
*sjset_qos
;
2827 struct slist
*sjset_radiotap_flags_present
;
2828 struct slist
*sjset_radiotap_ext_present
;
2829 struct slist
*sjset_radiotap_tsft_present
;
2830 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2831 struct slist
*s_roundup
;
2833 if (cstate
->off_linkpl
.reg
== -1) {
2835 * No register has been assigned to the offset of
2836 * the link-layer payload, which means nobody needs
2837 * it; don't bother computing it - just return
2838 * what we already have.
2844 * This code is not compatible with the optimizer, as
2845 * we are generating jmp instructions within a normal
2846 * slist of instructions
2848 cstate
->no_optimize
= 1;
2851 * If "s" is non-null, it has code to arrange that the X register
2852 * contains the length of the prefix preceding the link-layer
2855 * Otherwise, the length of the prefix preceding the link-layer
2856 * header is "off_outermostlinkhdr.constant_part".
2860 * There is no variable-length header preceding the
2861 * link-layer header.
2863 * Load the length of the fixed-length prefix preceding
2864 * the link-layer header (if any) into the X register,
2865 * and store it in the cstate->off_linkpl.reg register.
2866 * That length is off_outermostlinkhdr.constant_part.
2868 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2869 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2873 * The X register contains the offset of the beginning of the
2874 * link-layer header; add 24, which is the minimum length
2875 * of the MAC header for a data frame, to that, and store it
2876 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2877 * which is at the offset in the X register, with an indexed load.
2879 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2881 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2884 s2
= new_stmt(cstate
, BPF_ST
);
2885 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2888 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2893 * Check the Frame Control field to see if this is a data frame;
2894 * a data frame has the 0x08 bit (b3) in that field set and the
2895 * 0x04 bit (b2) clear.
2897 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2898 sjset_data_frame_1
->s
.k
= 0x08;
2899 sappend(s
, sjset_data_frame_1
);
2902 * If b3 is set, test b2, otherwise go to the first statement of
2903 * the rest of the program.
2905 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2906 sjset_data_frame_2
->s
.k
= 0x04;
2907 sappend(s
, sjset_data_frame_2
);
2908 sjset_data_frame_1
->s
.jf
= snext
;
2911 * If b2 is not set, this is a data frame; test the QoS bit.
2912 * Otherwise, go to the first statement of the rest of the
2915 sjset_data_frame_2
->s
.jt
= snext
;
2916 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2917 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2918 sappend(s
, sjset_qos
);
2921 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2923 * Otherwise, go to the first statement of the rest of the
2926 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2927 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2929 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2932 s2
= new_stmt(cstate
, BPF_ST
);
2933 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2937 * If we have a radiotap header, look at it to see whether
2938 * there's Atheros padding between the MAC-layer header
2941 * Note: all of the fields in the radiotap header are
2942 * little-endian, so we byte-swap all of the values
2943 * we test against, as they will be loaded as big-endian
2946 * XXX - in the general case, we would have to scan through
2947 * *all* the presence bits, if there's more than one word of
2948 * presence bits. That would require a loop, meaning that
2949 * we wouldn't be able to run the filter in the kernel.
2951 * We assume here that the Atheros adapters that insert the
2952 * annoying padding don't have multiple antennae and therefore
2953 * do not generate radiotap headers with multiple presence words.
2955 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2957 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2958 * in the first presence flag word?
2960 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2964 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2965 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2966 sappend(s
, sjset_radiotap_flags_present
);
2969 * If not, skip all of this.
2971 sjset_radiotap_flags_present
->s
.jf
= snext
;
2974 * Otherwise, is the "extension" bit set in that word?
2976 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2977 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2978 sappend(s
, sjset_radiotap_ext_present
);
2979 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2982 * If so, skip all of this.
2984 sjset_radiotap_ext_present
->s
.jt
= snext
;
2987 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2989 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2990 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2991 sappend(s
, sjset_radiotap_tsft_present
);
2992 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2995 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2996 * at an offset of 16 from the beginning of the raw packet
2997 * data (8 bytes for the radiotap header and 8 bytes for
3000 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3003 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3006 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3008 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3009 sjset_tsft_datapad
->s
.k
= 0x20;
3010 sappend(s
, sjset_tsft_datapad
);
3013 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3014 * at an offset of 8 from the beginning of the raw packet
3015 * data (8 bytes for the radiotap header).
3017 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3020 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3023 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3025 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3026 sjset_notsft_datapad
->s
.k
= 0x20;
3027 sappend(s
, sjset_notsft_datapad
);
3030 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3031 * set, round the length of the 802.11 header to
3032 * a multiple of 4. Do that by adding 3 and then
3033 * dividing by and multiplying by 4, which we do by
3036 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3037 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3038 sappend(s
, s_roundup
);
3039 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3042 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3043 s2
->s
.k
= (bpf_u_int32
)~3;
3045 s2
= new_stmt(cstate
, BPF_ST
);
3046 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3049 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3050 sjset_tsft_datapad
->s
.jf
= snext
;
3051 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3052 sjset_notsft_datapad
->s
.jf
= snext
;
3054 sjset_qos
->s
.jf
= snext
;
3060 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3064 /* There is an implicit dependency between the link
3065 * payload and link header since the payload computation
3066 * includes the variable part of the header. Therefore,
3067 * if nobody else has allocated a register for the link
3068 * header and we need it, do it now. */
3069 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3070 cstate
->off_linkhdr
.reg
== -1)
3071 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3074 * For link-layer types that have a variable-length header
3075 * preceding the link-layer header, generate code to load
3076 * the offset of the link-layer header into the register
3077 * assigned to that offset, if any.
3079 * XXX - this, and the next switch statement, won't handle
3080 * encapsulation of 802.11 or 802.11+radio information in
3081 * some other protocol stack. That's significantly more
3084 switch (cstate
->outermostlinktype
) {
3086 case DLT_PRISM_HEADER
:
3087 s
= gen_load_prism_llprefixlen(cstate
);
3090 case DLT_IEEE802_11_RADIO_AVS
:
3091 s
= gen_load_avs_llprefixlen(cstate
);
3094 case DLT_IEEE802_11_RADIO
:
3095 s
= gen_load_radiotap_llprefixlen(cstate
);
3099 s
= gen_load_ppi_llprefixlen(cstate
);
3108 * For link-layer types that have a variable-length link-layer
3109 * header, generate code to load the offset of the link-layer
3110 * payload into the register assigned to that offset, if any.
3112 switch (cstate
->outermostlinktype
) {
3114 case DLT_IEEE802_11
:
3115 case DLT_PRISM_HEADER
:
3116 case DLT_IEEE802_11_RADIO_AVS
:
3117 case DLT_IEEE802_11_RADIO
:
3119 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3123 s
= gen_load_pflog_llprefixlen(cstate
);
3128 * If there is no initialization yet and we need variable
3129 * length offsets for VLAN, initialize them to zero
3131 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3134 if (cstate
->off_linkpl
.reg
== -1)
3135 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3136 if (cstate
->off_linktype
.reg
== -1)
3137 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3139 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3141 s2
= new_stmt(cstate
, BPF_ST
);
3142 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3144 s2
= new_stmt(cstate
, BPF_ST
);
3145 s2
->s
.k
= cstate
->off_linktype
.reg
;
3150 * If we have any offset-loading code, append all the
3151 * existing statements in the block to those statements,
3152 * and make the resulting list the list of statements
3156 sappend(s
, b
->stmts
);
3161 static struct block
*
3162 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3164 struct slist
*s_load_dlt
;
3167 if (cstate
->linktype
== DLT_PPI
)
3169 /* Create the statements that check for the DLT
3171 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3172 s_load_dlt
->s
.k
= 4;
3174 b
= new_block(cstate
, JMP(BPF_JEQ
));
3176 b
->stmts
= s_load_dlt
;
3177 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3188 * Take an absolute offset, and:
3190 * if it has no variable part, return NULL;
3192 * if it has a variable part, generate code to load the register
3193 * containing that variable part into the X register, returning
3194 * a pointer to that code - if no register for that offset has
3195 * been allocated, allocate it first.
3197 * (The code to set that register will be generated later, but will
3198 * be placed earlier in the code sequence.)
3200 static struct slist
*
3201 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3205 if (off
->is_variable
) {
3206 if (off
->reg
== -1) {
3208 * We haven't yet assigned a register for the
3209 * variable part of the offset of the link-layer
3210 * header; allocate one.
3212 off
->reg
= alloc_reg(cstate
);
3216 * Load the register containing the variable part of the
3217 * offset of the link-layer header into the X register.
3219 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3224 * That offset isn't variable, there's no variable part,
3225 * so we don't need to generate any code.
3232 * Map an Ethernet type to the equivalent PPP type.
3235 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3243 case ETHERTYPE_IPV6
:
3244 ll_proto
= PPP_IPV6
;
3248 ll_proto
= PPP_DECNET
;
3251 case ETHERTYPE_ATALK
:
3252 ll_proto
= PPP_APPLE
;
3265 * I'm assuming the "Bridging PDU"s that go
3266 * over PPP are Spanning Tree Protocol
3269 ll_proto
= PPP_BRPDU
;
3280 * Generate any tests that, for encapsulation of a link-layer packet
3281 * inside another protocol stack, need to be done to check for those
3282 * link-layer packets (and that haven't already been done by a check
3283 * for that encapsulation).
3285 static struct block
*
3286 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3290 if (cstate
->is_encap
)
3291 return gen_encap_ll_check(cstate
);
3293 switch (cstate
->prevlinktype
) {
3297 * This is LANE-encapsulated Ethernet; check that the LANE
3298 * packet doesn't begin with an LE Control marker, i.e.
3299 * that it's data, not a control message.
3301 * (We've already generated a test for LANE.)
3303 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3309 * No such tests are necessary.
3317 * The three different values we should check for when checking for an
3318 * IPv6 packet with DLT_NULL.
3320 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3321 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3322 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3325 * Generate code to match a particular packet type by matching the
3326 * link-layer type field or fields in the 802.2 LLC header.
3328 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3329 * value, if <= ETHERMTU.
3331 static struct block
*
3332 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3334 struct block
*b0
, *b1
, *b2
;
3335 const char *description
;
3337 /* are we checking MPLS-encapsulated packets? */
3338 if (cstate
->label_stack_depth
> 0)
3339 return gen_mpls_linktype(cstate
, ll_proto
);
3341 switch (cstate
->linktype
) {
3344 case DLT_NETANALYZER
:
3345 case DLT_NETANALYZER_TRANSPARENT
:
3346 /* Geneve has an EtherType regardless of whether there is an
3347 * L2 header. VXLAN always has an EtherType. */
3348 if (!cstate
->is_encap
)
3349 b0
= gen_prevlinkhdr_check(cstate
);
3353 b1
= gen_ether_linktype(cstate
, ll_proto
);
3364 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3368 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3372 case DLT_IEEE802_11
:
3373 case DLT_PRISM_HEADER
:
3374 case DLT_IEEE802_11_RADIO_AVS
:
3375 case DLT_IEEE802_11_RADIO
:
3378 * Check that we have a data frame.
3380 b0
= gen_check_802_11_data_frame(cstate
);
3383 * Now check for the specified link-layer type.
3385 b1
= gen_llc_linktype(cstate
, ll_proto
);
3392 * XXX - check for LLC frames.
3394 return gen_llc_linktype(cstate
, ll_proto
);
3399 * XXX - check for LLC PDUs, as per IEEE 802.5.
3401 return gen_llc_linktype(cstate
, ll_proto
);
3404 case DLT_ATM_RFC1483
:
3406 case DLT_IP_OVER_FC
:
3407 return gen_llc_linktype(cstate
, ll_proto
);
3412 * Check for an LLC-encapsulated version of this protocol;
3413 * if we were checking for LANE, linktype would no longer
3416 * Check for LLC encapsulation and then check the protocol.
3418 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3419 b1
= gen_llc_linktype(cstate
, ll_proto
);
3425 return gen_linux_sll_linktype(cstate
, ll_proto
);
3429 case DLT_SLIP_BSDOS
:
3432 * These types don't provide any type field; packets
3433 * are always IPv4 or IPv6.
3435 * XXX - for IPv4, check for a version number of 4, and,
3436 * for IPv6, check for a version number of 6?
3441 /* Check for a version number of 4. */
3442 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3444 case ETHERTYPE_IPV6
:
3445 /* Check for a version number of 6. */
3446 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3449 return gen_false(cstate
); /* always false */
3455 * Raw IPv4, so no type field.
3457 if (ll_proto
== ETHERTYPE_IP
)
3458 return gen_true(cstate
); /* always true */
3460 /* Checking for something other than IPv4; always false */
3461 return gen_false(cstate
);
3466 * Raw IPv6, so no type field.
3468 if (ll_proto
== ETHERTYPE_IPV6
)
3469 return gen_true(cstate
); /* always true */
3471 /* Checking for something other than IPv6; always false */
3472 return gen_false(cstate
);
3477 case DLT_PPP_SERIAL
:
3480 * We use Ethernet protocol types inside libpcap;
3481 * map them to the corresponding PPP protocol types.
3483 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3484 ethertype_to_ppptype(ll_proto
));
3489 * We use Ethernet protocol types inside libpcap;
3490 * map them to the corresponding PPP protocol types.
3496 * Also check for Van Jacobson-compressed IP.
3497 * XXX - do this for other forms of PPP?
3499 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3500 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3502 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3507 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3508 ethertype_to_ppptype(ll_proto
));
3518 return (gen_loopback_linktype(cstate
, AF_INET
));
3520 case ETHERTYPE_IPV6
:
3522 * AF_ values may, unfortunately, be platform-
3523 * dependent; AF_INET isn't, because everybody
3524 * used 4.2BSD's value, but AF_INET6 is, because
3525 * 4.2BSD didn't have a value for it (given that
3526 * IPv6 didn't exist back in the early 1980's),
3527 * and they all picked their own values.
3529 * This means that, if we're reading from a
3530 * savefile, we need to check for all the
3533 * If we're doing a live capture, we only need
3534 * to check for this platform's value; however,
3535 * Npcap uses 24, which isn't Windows's AF_INET6
3536 * value. (Given the multiple different values,
3537 * programs that read pcap files shouldn't be
3538 * checking for their platform's AF_INET6 value
3539 * anyway, they should check for all of the
3540 * possible values. and they might as well do
3541 * that even for live captures.)
3543 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3545 * Savefile - check for all three
3546 * possible IPv6 values.
3548 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3549 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3551 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3556 * Live capture, so we only need to
3557 * check for the value used on this
3562 * Npcap doesn't use Windows's AF_INET6,
3563 * as that collides with AF_IPX on
3564 * some BSDs (both have the value 23).
3565 * Instead, it uses 24.
3567 return (gen_loopback_linktype(cstate
, 24));
3570 return (gen_loopback_linktype(cstate
, AF_INET6
));
3571 #else /* AF_INET6 */
3573 * I guess this platform doesn't support
3574 * IPv6, so we just reject all packets.
3576 return gen_false(cstate
);
3577 #endif /* AF_INET6 */
3583 * Not a type on which we support filtering.
3584 * XXX - support those that have AF_ values
3585 * #defined on this platform, at least?
3587 return gen_false(cstate
);
3592 * af field is host byte order in contrast to the rest of
3595 if (ll_proto
== ETHERTYPE_IP
)
3596 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3598 else if (ll_proto
== ETHERTYPE_IPV6
)
3599 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3602 return gen_false(cstate
);
3606 case DLT_ARCNET_LINUX
:
3608 * XXX should we check for first fragment if the protocol
3614 return gen_false(cstate
);
3616 case ETHERTYPE_IPV6
:
3617 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3621 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3623 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3629 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3631 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3636 case ETHERTYPE_REVARP
:
3637 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3640 case ETHERTYPE_ATALK
:
3641 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3648 case ETHERTYPE_ATALK
:
3649 return gen_true(cstate
);
3651 return gen_false(cstate
);
3657 * XXX - assumes a 2-byte Frame Relay header with
3658 * DLCI and flags. What if the address is longer?
3664 * Check for the special NLPID for IP.
3666 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3668 case ETHERTYPE_IPV6
:
3670 * Check for the special NLPID for IPv6.
3672 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3676 * Check for several OSI protocols.
3678 * Frame Relay packets typically have an OSI
3679 * NLPID at the beginning; we check for each
3682 * What we check for is the NLPID and a frame
3683 * control field of UI, i.e. 0x03 followed
3686 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3687 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3688 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3694 return gen_false(cstate
);
3699 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3701 case DLT_JUNIPER_MFR
:
3702 case DLT_JUNIPER_MLFR
:
3703 case DLT_JUNIPER_MLPPP
:
3704 case DLT_JUNIPER_ATM1
:
3705 case DLT_JUNIPER_ATM2
:
3706 case DLT_JUNIPER_PPPOE
:
3707 case DLT_JUNIPER_PPPOE_ATM
:
3708 case DLT_JUNIPER_GGSN
:
3709 case DLT_JUNIPER_ES
:
3710 case DLT_JUNIPER_MONITOR
:
3711 case DLT_JUNIPER_SERVICES
:
3712 case DLT_JUNIPER_ETHER
:
3713 case DLT_JUNIPER_PPP
:
3714 case DLT_JUNIPER_FRELAY
:
3715 case DLT_JUNIPER_CHDLC
:
3716 case DLT_JUNIPER_VP
:
3717 case DLT_JUNIPER_ST
:
3718 case DLT_JUNIPER_ISM
:
3719 case DLT_JUNIPER_VS
:
3720 case DLT_JUNIPER_SRX_E2E
:
3721 case DLT_JUNIPER_FIBRECHANNEL
:
3722 case DLT_JUNIPER_ATM_CEMIC
:
3724 /* just lets verify the magic number for now -
3725 * on ATM we may have up to 6 different encapsulations on the wire
3726 * and need a lot of heuristics to figure out that the payload
3729 * FIXME encapsulation specific BPF_ filters
3731 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3733 case DLT_BACNET_MS_TP
:
3734 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3737 return gen_ipnet_linktype(cstate
, ll_proto
);
3739 case DLT_LINUX_IRDA
:
3740 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3743 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3746 case DLT_MTP2_WITH_PHDR
:
3747 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3750 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3753 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3755 case DLT_LINUX_LAPD
:
3756 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3758 case DLT_USB_FREEBSD
:
3760 case DLT_USB_LINUX_MMAPPED
:
3762 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3764 case DLT_BLUETOOTH_HCI_H4
:
3765 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3766 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3769 case DLT_CAN_SOCKETCAN
:
3770 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3772 case DLT_IEEE802_15_4
:
3773 case DLT_IEEE802_15_4_LINUX
:
3774 case DLT_IEEE802_15_4_NONASK_PHY
:
3775 case DLT_IEEE802_15_4_NOFCS
:
3776 case DLT_IEEE802_15_4_TAP
:
3777 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3779 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3780 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3783 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3786 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3788 case DLT_IPMB_KONTRON
:
3789 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3792 bpf_error(cstate
, "I2C link-layer type filtering not implemented");
3795 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3798 /* Using the fixed-size NFLOG header it is possible to tell only
3799 * the address family of the packet, other meaningful data is
3800 * either missing or behind TLVs.
3802 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3806 * Does this link-layer header type have a field
3807 * indicating the type of the next protocol? If
3808 * so, off_linktype.constant_part will be the offset of that
3809 * field in the packet; if not, it will be OFFSET_NOT_SET.
3811 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3813 * Yes; assume it's an Ethernet type. (If
3814 * it's not, it needs to be handled specially
3817 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3821 * No; report an error.
3823 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3824 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3832 * Check for an LLC SNAP packet with a given organization code and
3833 * protocol type; we check the entire contents of the 802.2 LLC and
3834 * snap headers, checking for DSAP and SSAP of SNAP and a control
3835 * field of 0x03 in the LLC header, and for the specified organization
3836 * code and protocol type in the SNAP header.
3838 static struct block
*
3839 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3841 u_char snapblock
[8];
3843 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3844 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3845 snapblock
[2] = 0x03; /* control = UI */
3846 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3847 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3848 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3849 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3850 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3851 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3855 * Generate code to match frames with an LLC header.
3857 static struct block
*
3858 gen_llc_internal(compiler_state_t
*cstate
)
3860 struct block
*b0
, *b1
;
3862 switch (cstate
->linktype
) {
3866 * We check for an Ethernet type field less than
3867 * 1500, which means it's an 802.3 length field.
3869 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3873 * Now check for the purported DSAP and SSAP not being
3874 * 0xFF, to rule out NetWare-over-802.3.
3876 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3883 * We check for LLC traffic.
3885 b0
= gen_atmtype_llc(cstate
);
3888 case DLT_IEEE802
: /* Token Ring */
3890 * XXX - check for LLC frames.
3892 return gen_true(cstate
);
3896 * XXX - check for LLC frames.
3898 return gen_true(cstate
);
3900 case DLT_ATM_RFC1483
:
3902 * For LLC encapsulation, these are defined to have an
3905 * For VC encapsulation, they don't, but there's no
3906 * way to check for that; the protocol used on the VC
3907 * is negotiated out of band.
3909 return gen_true(cstate
);
3911 case DLT_IEEE802_11
:
3912 case DLT_PRISM_HEADER
:
3913 case DLT_IEEE802_11_RADIO
:
3914 case DLT_IEEE802_11_RADIO_AVS
:
3917 * Check that we have a data frame.
3919 b0
= gen_check_802_11_data_frame(cstate
);
3923 bpf_error(cstate
, "'llc' not supported for %s",
3924 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3930 gen_llc(compiler_state_t
*cstate
)
3933 * Catch errors reported by us and routines below us, and return NULL
3936 if (setjmp(cstate
->top_ctx
))
3939 return gen_llc_internal(cstate
);
3943 gen_llc_i(compiler_state_t
*cstate
)
3945 struct block
*b0
, *b1
;
3949 * Catch errors reported by us and routines below us, and return NULL
3952 if (setjmp(cstate
->top_ctx
))
3956 * Check whether this is an LLC frame.
3958 b0
= gen_llc_internal(cstate
);
3961 * Load the control byte and test the low-order bit; it must
3962 * be clear for I frames.
3964 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3965 b1
= new_block(cstate
, JMP(BPF_JSET
));
3974 gen_llc_s(compiler_state_t
*cstate
)
3976 struct block
*b0
, *b1
;
3979 * Catch errors reported by us and routines below us, and return NULL
3982 if (setjmp(cstate
->top_ctx
))
3986 * Check whether this is an LLC frame.
3988 b0
= gen_llc_internal(cstate
);
3991 * Now compare the low-order 2 bit of the control byte against
3992 * the appropriate value for S frames.
3994 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4000 gen_llc_u(compiler_state_t
*cstate
)
4002 struct block
*b0
, *b1
;
4005 * Catch errors reported by us and routines below us, and return NULL
4008 if (setjmp(cstate
->top_ctx
))
4012 * Check whether this is an LLC frame.
4014 b0
= gen_llc_internal(cstate
);
4017 * Now compare the low-order 2 bit of the control byte against
4018 * the appropriate value for U frames.
4020 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4026 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4028 struct block
*b0
, *b1
;
4031 * Catch errors reported by us and routines below us, and return NULL
4034 if (setjmp(cstate
->top_ctx
))
4038 * Check whether this is an LLC frame.
4040 b0
= gen_llc_internal(cstate
);
4043 * Now check for an S frame with the appropriate type.
4045 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4051 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4053 struct block
*b0
, *b1
;
4056 * Catch errors reported by us and routines below us, and return NULL
4059 if (setjmp(cstate
->top_ctx
))
4063 * Check whether this is an LLC frame.
4065 b0
= gen_llc_internal(cstate
);
4068 * Now check for a U frame with the appropriate type.
4070 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4076 * Generate code to match a particular packet type, for link-layer types
4077 * using 802.2 LLC headers.
4079 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4080 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4082 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4083 * value, if <= ETHERMTU. We use that to determine whether to
4084 * match the DSAP or both DSAP and LSAP or to check the OUI and
4085 * protocol ID in a SNAP header.
4087 static struct block
*
4088 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4091 * XXX - handle token-ring variable-length header.
4097 case LLCSAP_NETBEUI
:
4099 * XXX - should we check both the DSAP and the
4100 * SSAP, like this, or should we check just the
4101 * DSAP, as we do for other SAP values?
4103 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4104 ((ll_proto
<< 8) | ll_proto
));
4108 * XXX - are there ever SNAP frames for IPX on
4109 * non-Ethernet 802.x networks?
4111 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4113 case ETHERTYPE_ATALK
:
4115 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4116 * SNAP packets with an organization code of
4117 * 0x080007 (Apple, for Appletalk) and a protocol
4118 * type of ETHERTYPE_ATALK (Appletalk).
4120 * XXX - check for an organization code of
4121 * encapsulated Ethernet as well?
4123 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4127 * XXX - we don't have to check for IPX 802.3
4128 * here, but should we check for the IPX Ethertype?
4130 if (ll_proto
<= ETHERMTU
) {
4132 * This is an LLC SAP value, so check
4135 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4138 * This is an Ethernet type; we assume that it's
4139 * unlikely that it'll appear in the right place
4140 * at random, and therefore check only the
4141 * location that would hold the Ethernet type
4142 * in a SNAP frame with an organization code of
4143 * 0x000000 (encapsulated Ethernet).
4145 * XXX - if we were to check for the SNAP DSAP and
4146 * LSAP, as per XXX, and were also to check for an
4147 * organization code of 0x000000 (encapsulated
4148 * Ethernet), we'd do
4150 * return gen_snap(cstate, 0x000000, ll_proto);
4152 * here; for now, we don't, as per the above.
4153 * I don't know whether it's worth the extra CPU
4154 * time to do the right check or not.
4156 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4161 static struct block
*
4162 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4163 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4165 struct block
*b0
, *b1
;
4179 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4180 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4186 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4187 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4192 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4196 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4200 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4204 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4208 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4212 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4219 b0
= gen_linktype(cstate
, ll_proto
);
4220 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4226 static struct block
*
4227 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4228 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4231 struct block
*b0
, *b1
;
4234 * Code below needs to access four separate 32-bit parts of the 128-bit
4235 * IPv6 address and mask. In some OSes this is as simple as using the
4236 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4237 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4238 * far as libpcap sees it. Hence copy the data before use to avoid
4239 * potential unaligned memory access and the associated compiler
4240 * warnings (whether genuine or not).
4242 bpf_u_int32 a
[4], m
[4];
4255 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4256 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4262 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4263 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4268 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4272 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4276 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4280 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4284 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4288 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4295 /* this order is important */
4296 memcpy(a
, addr
, sizeof(a
));
4297 memcpy(m
, mask
, sizeof(m
));
4298 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4299 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4301 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4303 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4305 b0
= gen_linktype(cstate
, ll_proto
);
4311 static struct block
*
4312 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4314 register struct block
*b0
, *b1
;
4318 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4321 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4324 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4325 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4331 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4332 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4337 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4341 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4345 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4349 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4353 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4357 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4365 * Like gen_ehostop, but for DLT_FDDI
4367 static struct block
*
4368 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4370 struct block
*b0
, *b1
;
4374 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4377 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4380 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4381 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4387 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4388 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4393 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4397 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4401 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4405 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4409 bpf_error(cstate
, "'ra' is only supported on 802.11");
4413 bpf_error(cstate
, "'ta' is only supported on 802.11");
4421 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4423 static struct block
*
4424 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4426 register struct block
*b0
, *b1
;
4430 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4433 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4436 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4437 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4443 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4444 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4449 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4453 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4457 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4461 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4465 bpf_error(cstate
, "'ra' is only supported on 802.11");
4469 bpf_error(cstate
, "'ta' is only supported on 802.11");
4477 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4478 * various 802.11 + radio headers.
4480 static struct block
*
4481 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4483 register struct block
*b0
, *b1
, *b2
;
4484 register struct slist
*s
;
4486 #ifdef ENABLE_WLAN_FILTERING_PATCH
4489 * We need to disable the optimizer because the optimizer is buggy
4490 * and wipes out some LD instructions generated by the below
4491 * code to validate the Frame Control bits
4493 cstate
->no_optimize
= 1;
4494 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4501 * For control frames, there is no SA.
4503 * For management frames, SA is at an
4504 * offset of 10 from the beginning of
4507 * For data frames, SA is at an offset
4508 * of 10 from the beginning of the packet
4509 * if From DS is clear, at an offset of
4510 * 16 from the beginning of the packet
4511 * if From DS is set and To DS is clear,
4512 * and an offset of 24 from the beginning
4513 * of the packet if From DS is set and To DS
4518 * Generate the tests to be done for data frames
4521 * First, check for To DS set, i.e. check "link[1] & 0x01".
4523 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4524 b1
= new_block(cstate
, JMP(BPF_JSET
));
4525 b1
->s
.k
= 0x01; /* To DS */
4529 * If To DS is set, the SA is at 24.
4531 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4535 * Now, check for To DS not set, i.e. check
4536 * "!(link[1] & 0x01)".
4538 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4539 b2
= new_block(cstate
, JMP(BPF_JSET
));
4540 b2
->s
.k
= 0x01; /* To DS */
4545 * If To DS is not set, the SA is at 16.
4547 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4551 * Now OR together the last two checks. That gives
4552 * the complete set of checks for data frames with
4558 * Now check for From DS being set, and AND that with
4559 * the ORed-together checks.
4561 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4562 b1
= new_block(cstate
, JMP(BPF_JSET
));
4563 b1
->s
.k
= 0x02; /* From DS */
4568 * Now check for data frames with From DS not set.
4570 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4571 b2
= new_block(cstate
, JMP(BPF_JSET
));
4572 b2
->s
.k
= 0x02; /* From DS */
4577 * If From DS isn't set, the SA is at 10.
4579 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4583 * Now OR together the checks for data frames with
4584 * From DS not set and for data frames with From DS
4585 * set; that gives the checks done for data frames.
4590 * Now check for a data frame.
4591 * I.e, check "link[0] & 0x08".
4593 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4594 b1
= new_block(cstate
, JMP(BPF_JSET
));
4599 * AND that with the checks done for data frames.
4604 * If the high-order bit of the type value is 0, this
4605 * is a management frame.
4606 * I.e, check "!(link[0] & 0x08)".
4608 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4609 b2
= new_block(cstate
, JMP(BPF_JSET
));
4615 * For management frames, the SA is at 10.
4617 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4621 * OR that with the checks done for data frames.
4622 * That gives the checks done for management and
4628 * If the low-order bit of the type value is 1,
4629 * this is either a control frame or a frame
4630 * with a reserved type, and thus not a
4633 * I.e., check "!(link[0] & 0x04)".
4635 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4636 b1
= new_block(cstate
, JMP(BPF_JSET
));
4642 * AND that with the checks for data and management
4652 * For control frames, there is no DA.
4654 * For management frames, DA is at an
4655 * offset of 4 from the beginning of
4658 * For data frames, DA is at an offset
4659 * of 4 from the beginning of the packet
4660 * if To DS is clear and at an offset of
4661 * 16 from the beginning of the packet
4666 * Generate the tests to be done for data frames.
4668 * First, check for To DS set, i.e. "link[1] & 0x01".
4670 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4671 b1
= new_block(cstate
, JMP(BPF_JSET
));
4672 b1
->s
.k
= 0x01; /* To DS */
4676 * If To DS is set, the DA is at 16.
4678 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4682 * Now, check for To DS not set, i.e. check
4683 * "!(link[1] & 0x01)".
4685 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4686 b2
= new_block(cstate
, JMP(BPF_JSET
));
4687 b2
->s
.k
= 0x01; /* To DS */
4692 * If To DS is not set, the DA is at 4.
4694 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4698 * Now OR together the last two checks. That gives
4699 * the complete set of checks for data frames.
4704 * Now check for a data frame.
4705 * I.e, check "link[0] & 0x08".
4707 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4708 b1
= new_block(cstate
, JMP(BPF_JSET
));
4713 * AND that with the checks done for data frames.
4718 * If the high-order bit of the type value is 0, this
4719 * is a management frame.
4720 * I.e, check "!(link[0] & 0x08)".
4722 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4723 b2
= new_block(cstate
, JMP(BPF_JSET
));
4729 * For management frames, the DA is at 4.
4731 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4735 * OR that with the checks done for data frames.
4736 * That gives the checks done for management and
4742 * If the low-order bit of the type value is 1,
4743 * this is either a control frame or a frame
4744 * with a reserved type, and thus not a
4747 * I.e., check "!(link[0] & 0x04)".
4749 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4750 b1
= new_block(cstate
, JMP(BPF_JSET
));
4756 * AND that with the checks for data and management
4763 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4764 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4770 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4771 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4776 * XXX - add BSSID keyword?
4779 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4783 * Not present in CTS or ACK control frames.
4785 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4786 IEEE80211_FC0_TYPE_MASK
);
4788 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4789 IEEE80211_FC0_SUBTYPE_MASK
);
4791 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4792 IEEE80211_FC0_SUBTYPE_MASK
);
4796 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4802 * Not present in control frames.
4804 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4805 IEEE80211_FC0_TYPE_MASK
);
4807 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4813 * Present only if the direction mask has both "From DS"
4814 * and "To DS" set. Neither control frames nor management
4815 * frames should have both of those set, so we don't
4816 * check the frame type.
4818 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4819 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4820 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4826 * Not present in management frames; addr1 in other
4831 * If the high-order bit of the type value is 0, this
4832 * is a management frame.
4833 * I.e, check "(link[0] & 0x08)".
4835 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4836 b1
= new_block(cstate
, JMP(BPF_JSET
));
4843 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4846 * AND that with the check of addr1.
4853 * Not present in management frames; addr2, if present,
4858 * Not present in CTS or ACK control frames.
4860 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4861 IEEE80211_FC0_TYPE_MASK
);
4863 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4864 IEEE80211_FC0_SUBTYPE_MASK
);
4866 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4867 IEEE80211_FC0_SUBTYPE_MASK
);
4873 * If the high-order bit of the type value is 0, this
4874 * is a management frame.
4875 * I.e, check "(link[0] & 0x08)".
4877 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4878 b1
= new_block(cstate
, JMP(BPF_JSET
));
4883 * AND that with the check for frames other than
4884 * CTS and ACK frames.
4891 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4900 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4901 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4902 * as the RFC states.)
4904 static struct block
*
4905 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4907 register struct block
*b0
, *b1
;
4911 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4914 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4917 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4918 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4924 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4925 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4930 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4934 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4938 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4942 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4946 bpf_error(cstate
, "'ra' is only supported on 802.11");
4950 bpf_error(cstate
, "'ta' is only supported on 802.11");
4958 * This is quite tricky because there may be pad bytes in front of the
4959 * DECNET header, and then there are two possible data packet formats that
4960 * carry both src and dst addresses, plus 5 packet types in a format that
4961 * carries only the src node, plus 2 types that use a different format and
4962 * also carry just the src node.
4966 * Instead of doing those all right, we just look for data packets with
4967 * 0 or 1 bytes of padding. If you want to look at other packets, that
4968 * will require a lot more hacking.
4970 * To add support for filtering on DECNET "areas" (network numbers)
4971 * one would want to add a "mask" argument to this routine. That would
4972 * make the filter even more inefficient, although one could be clever
4973 * and not generate masking instructions if the mask is 0xFFFF.
4975 static struct block
*
4976 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4978 struct block
*b0
, *b1
, *b2
, *tmp
;
4979 u_int offset_lh
; /* offset if long header is received */
4980 u_int offset_sh
; /* offset if short header is received */
4985 offset_sh
= 1; /* follows flags */
4986 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4990 offset_sh
= 3; /* follows flags, dstnode */
4991 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4995 /* Inefficient because we do our Calvinball dance twice */
4996 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4997 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5003 /* Inefficient because we do our Calvinball dance twice */
5004 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5005 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5010 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5014 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5018 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5022 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
5026 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5030 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
5037 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5038 /* Check for pad = 1, long header case */
5039 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
5040 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
5041 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5042 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
5044 /* Check for pad = 0, long header case */
5045 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
5047 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5048 (bpf_u_int32
)ntohs((u_short
)addr
));
5051 /* Check for pad = 1, short header case */
5052 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
5053 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
5054 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5055 (bpf_u_int32
)ntohs((u_short
)addr
));
5058 /* Check for pad = 0, short header case */
5059 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
5061 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5062 (bpf_u_int32
)ntohs((u_short
)addr
));
5066 /* Combine with test for cstate->linktype */
5072 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5073 * test the bottom-of-stack bit, and then check the version number
5074 * field in the IP header.
5076 static struct block
*
5077 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5079 struct block
*b0
, *b1
;
5084 /* match the bottom-of-stack bit */
5085 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5086 /* match the IPv4 version number */
5087 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5091 case ETHERTYPE_IPV6
:
5092 /* match the bottom-of-stack bit */
5093 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5094 /* match the IPv4 version number */
5095 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5100 /* FIXME add other L3 proto IDs */
5101 bpf_error(cstate
, "unsupported protocol over mpls");
5106 static struct block
*
5107 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5108 int proto
, int dir
, int type
)
5110 struct block
*b0
, *b1
;
5111 const char *typestr
;
5121 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5123 * Only check for non-IPv4 addresses if we're not
5124 * checking MPLS-encapsulated packets.
5126 if (cstate
->label_stack_depth
== 0) {
5127 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5129 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5135 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5138 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
5141 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
5144 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
5147 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5150 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5153 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5156 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5159 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5162 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5165 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5168 return gen_dnhostop(cstate
, addr
, dir
);
5171 bpf_error(cstate
, "LAT host filtering not implemented");
5174 bpf_error(cstate
, "SCA host filtering not implemented");
5177 bpf_error(cstate
, "MOPRC host filtering not implemented");
5180 bpf_error(cstate
, "MOPDL host filtering not implemented");
5183 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5186 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5189 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5192 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5195 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5198 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5201 bpf_error(cstate
, "AARP host filtering not implemented");
5204 bpf_error(cstate
, "ISO host filtering not implemented");
5207 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5210 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5213 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5216 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5219 bpf_error(cstate
, "IPX host filtering not implemented");
5222 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5225 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5228 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5231 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5234 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5237 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5240 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5243 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5246 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5249 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5258 static struct block
*
5259 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5260 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5262 const char *typestr
;
5272 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5275 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5278 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5281 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5284 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5287 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5290 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5293 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5296 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5299 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5302 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5305 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5308 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5311 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5314 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5317 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5320 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5323 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5326 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5329 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5332 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5335 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5338 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5341 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5344 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5347 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5350 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5353 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5356 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5359 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5362 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5365 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5368 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5371 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5374 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5377 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5380 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5383 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5386 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5389 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5399 static struct block
*
5400 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5401 struct addrinfo
*alist
, int proto
, int dir
)
5403 struct block
*b0
, *b1
, *tmp
;
5404 struct addrinfo
*ai
;
5405 struct sockaddr_in
*sin
;
5408 bpf_error(cstate
, "direction applied to 'gateway'");
5415 switch (cstate
->linktype
) {
5417 case DLT_NETANALYZER
:
5418 case DLT_NETANALYZER_TRANSPARENT
:
5419 b1
= gen_prevlinkhdr_check(cstate
);
5420 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5425 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5428 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5430 case DLT_IEEE802_11
:
5431 case DLT_PRISM_HEADER
:
5432 case DLT_IEEE802_11_RADIO_AVS
:
5433 case DLT_IEEE802_11_RADIO
:
5435 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5439 * This is LLC-multiplexed traffic; if it were
5440 * LANE, cstate->linktype would have been set to
5444 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5445 case DLT_IP_OVER_FC
:
5446 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5450 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5453 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5455 * Does it have an address?
5457 if (ai
->ai_addr
!= NULL
) {
5459 * Yes. Is it an IPv4 address?
5461 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5463 * Generate an entry for it.
5465 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5466 tmp
= gen_host(cstate
,
5467 ntohl(sin
->sin_addr
.s_addr
),
5468 0xffffffff, proto
, Q_OR
, Q_HOST
);
5470 * Is it the *first* IPv4 address?
5474 * Yes, so start with it.
5479 * No, so OR it into the
5491 * No IPv4 addresses found.
5499 bpf_error(cstate
, "illegal modifier of 'gateway'");
5504 static struct block
*
5505 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5513 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5517 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5521 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5525 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5528 #ifndef IPPROTO_IGMP
5529 #define IPPROTO_IGMP 2
5533 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5536 #ifndef IPPROTO_IGRP
5537 #define IPPROTO_IGRP 9
5540 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5544 #define IPPROTO_PIM 103
5548 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5551 #ifndef IPPROTO_VRRP
5552 #define IPPROTO_VRRP 112
5556 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5559 #ifndef IPPROTO_CARP
5560 #define IPPROTO_CARP 112
5564 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5568 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5572 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5576 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5580 bpf_error(cstate
, "link layer applied in wrong context");
5583 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5587 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5591 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5595 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5599 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5603 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5607 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5611 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5614 #ifndef IPPROTO_ICMPV6
5615 #define IPPROTO_ICMPV6 58
5618 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5622 #define IPPROTO_AH 51
5625 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5629 #define IPPROTO_ESP 50
5632 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5636 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5640 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5644 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5647 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5648 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5649 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5651 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5653 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5655 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5659 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5660 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5661 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5663 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5665 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5667 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5671 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5672 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5673 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5675 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5680 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5681 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5686 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5687 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5689 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5691 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5696 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5697 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5702 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5703 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5708 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5712 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5716 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5720 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5724 bpf_error(cstate
, "'radio' is not a valid protocol type");
5733 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5736 * Catch errors reported by us and routines below us, and return NULL
5739 if (setjmp(cstate
->top_ctx
))
5742 return gen_proto_abbrev_internal(cstate
, proto
);
5745 static struct block
*
5746 gen_ipfrag(compiler_state_t
*cstate
)
5751 /* not IPv4 frag other than the first frag */
5752 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5753 b
= new_block(cstate
, JMP(BPF_JSET
));
5762 * Generate a comparison to a port value in the transport-layer header
5763 * at the specified offset from the beginning of that header.
5765 * XXX - this handles a variable-length prefix preceding the link-layer
5766 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5767 * variable-length link-layer headers (such as Token Ring or 802.11
5770 static struct block
*
5771 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5773 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5776 static struct block
*
5777 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5779 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5782 static struct block
*
5783 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5785 struct block
*b0
, *b1
, *tmp
;
5787 /* ip proto 'proto' and not a fragment other than the first fragment */
5788 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5789 b0
= gen_ipfrag(cstate
);
5794 b1
= gen_portatom(cstate
, 0, port
);
5798 b1
= gen_portatom(cstate
, 2, port
);
5802 tmp
= gen_portatom(cstate
, 0, port
);
5803 b1
= gen_portatom(cstate
, 2, port
);
5809 tmp
= gen_portatom(cstate
, 0, port
);
5810 b1
= gen_portatom(cstate
, 2, port
);
5815 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5819 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5823 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5827 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5831 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5835 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5847 static struct block
*
5848 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5850 struct block
*b0
, *b1
, *tmp
;
5855 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5856 * not LLC encapsulation with LLCSAP_IP.
5858 * For IEEE 802 networks - which includes 802.5 token ring
5859 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5860 * says that SNAP encapsulation is used, not LLC encapsulation
5863 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5864 * RFC 2225 say that SNAP encapsulation is used, not LLC
5865 * encapsulation with LLCSAP_IP.
5867 * So we always check for ETHERTYPE_IP.
5869 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5875 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5879 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5880 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5882 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5894 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5896 struct block
*b0
, *b1
, *tmp
;
5898 /* ip6 proto 'proto' */
5899 /* XXX - catch the first fragment of a fragmented packet? */
5900 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5904 b1
= gen_portatom6(cstate
, 0, port
);
5908 b1
= gen_portatom6(cstate
, 2, port
);
5912 tmp
= gen_portatom6(cstate
, 0, port
);
5913 b1
= gen_portatom6(cstate
, 2, port
);
5919 tmp
= gen_portatom6(cstate
, 0, port
);
5920 b1
= gen_portatom6(cstate
, 2, port
);
5932 static struct block
*
5933 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5935 struct block
*b0
, *b1
, *tmp
;
5937 /* link proto ip6 */
5938 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5944 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5948 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5949 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5951 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5962 /* gen_portrange code */
5963 static struct block
*
5964 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5967 struct block
*b1
, *b2
;
5971 * Reverse the order of the ports, so v1 is the lower one.
5980 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5981 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5988 static struct block
*
5989 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5990 bpf_u_int32 proto
, int dir
)
5992 struct block
*b0
, *b1
, *tmp
;
5994 /* ip proto 'proto' and not a fragment other than the first fragment */
5995 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5996 b0
= gen_ipfrag(cstate
);
6001 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6005 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6009 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6010 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6016 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6017 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6022 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6026 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6030 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6034 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6038 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
6042 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
6054 static struct block
*
6055 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6058 struct block
*b0
, *b1
, *tmp
;
6061 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6067 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6072 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6073 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6075 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6086 static struct block
*
6087 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6090 struct block
*b1
, *b2
;
6094 * Reverse the order of the ports, so v1 is the lower one.
6103 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6104 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6111 static struct block
*
6112 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6113 bpf_u_int32 proto
, int dir
)
6115 struct block
*b0
, *b1
, *tmp
;
6117 /* ip6 proto 'proto' */
6118 /* XXX - catch the first fragment of a fragmented packet? */
6119 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6123 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6127 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6131 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6132 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6138 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6139 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6151 static struct block
*
6152 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6155 struct block
*b0
, *b1
, *tmp
;
6157 /* link proto ip6 */
6158 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6164 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6169 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6170 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6172 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6184 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6193 v
= pcap_nametoproto(name
);
6194 if (v
== PROTO_UNDEF
)
6195 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6199 /* XXX should look up h/w protocol type based on cstate->linktype */
6200 v
= pcap_nametoeproto(name
);
6201 if (v
== PROTO_UNDEF
) {
6202 v
= pcap_nametollc(name
);
6203 if (v
== PROTO_UNDEF
)
6204 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6209 if (strcmp(name
, "esis") == 0)
6211 else if (strcmp(name
, "isis") == 0)
6213 else if (strcmp(name
, "clnp") == 0)
6216 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6226 #if !defined(NO_PROTOCHAIN)
6227 static struct block
*
6228 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6230 struct block
*b0
, *b
;
6231 struct slist
*s
[100];
6232 int fix2
, fix3
, fix4
, fix5
;
6233 int ahcheck
, again
, end
;
6235 int reg2
= alloc_reg(cstate
);
6237 memset(s
, 0, sizeof(s
));
6238 fix3
= fix4
= fix5
= 0;
6245 b0
= gen_protochain(cstate
, v
, Q_IP
);
6246 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6250 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6255 * We don't handle variable-length prefixes before the link-layer
6256 * header, or variable-length link-layer headers, here yet.
6257 * We might want to add BPF instructions to do the protochain
6258 * work, to simplify that and, on platforms that have a BPF
6259 * interpreter with the new instructions, let the filtering
6260 * be done in the kernel. (We already require a modified BPF
6261 * engine to do the protochain stuff, to support backward
6262 * branches, and backward branch support is unlikely to appear
6263 * in kernel BPF engines.)
6265 if (cstate
->off_linkpl
.is_variable
)
6266 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6269 * To quote a comment in optimize.c:
6271 * "These data structures are used in a Cocke and Schwartz style
6272 * value numbering scheme. Since the flowgraph is acyclic,
6273 * exit values can be propagated from a node's predecessors
6274 * provided it is uniquely defined."
6276 * "Acyclic" means "no backward branches", which means "no
6277 * loops", so we have to turn the optimizer off.
6279 cstate
->no_optimize
= 1;
6282 * s[0] is a dummy entry to protect other BPF insn from damage
6283 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6284 * hard to find interdependency made by jump table fixup.
6287 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6292 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6295 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6296 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6298 /* X = ip->ip_hl << 2 */
6299 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6300 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6305 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6307 /* A = ip6->ip_nxt */
6308 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6309 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6311 /* X = sizeof(struct ip6_hdr) */
6312 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6318 bpf_error(cstate
, "unsupported proto to gen_protochain");
6322 /* again: if (A == v) goto end; else fall through; */
6324 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6326 s
[i
]->s
.jt
= NULL
; /*later*/
6327 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6331 #ifndef IPPROTO_NONE
6332 #define IPPROTO_NONE 59
6334 /* if (A == IPPROTO_NONE) goto end */
6335 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6336 s
[i
]->s
.jt
= NULL
; /*later*/
6337 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6338 s
[i
]->s
.k
= IPPROTO_NONE
;
6339 s
[fix5
]->s
.jf
= s
[i
];
6343 if (proto
== Q_IPV6
) {
6344 int v6start
, v6end
, v6advance
, j
;
6347 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6348 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6349 s
[i
]->s
.jt
= NULL
; /*later*/
6350 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6351 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6352 s
[fix2
]->s
.jf
= s
[i
];
6354 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6355 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6356 s
[i
]->s
.jt
= NULL
; /*later*/
6357 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6358 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6360 /* if (A == IPPROTO_ROUTING) goto v6advance */
6361 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6362 s
[i
]->s
.jt
= NULL
; /*later*/
6363 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6364 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6366 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6367 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6368 s
[i
]->s
.jt
= NULL
; /*later*/
6369 s
[i
]->s
.jf
= NULL
; /*later*/
6370 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6380 * A = P[X + packet head];
6381 * X = X + (P[X + packet head + 1] + 1) * 8;
6383 /* A = P[X + packet head] */
6384 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6385 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6388 s
[i
] = new_stmt(cstate
, BPF_ST
);
6391 /* A = P[X + packet head + 1]; */
6392 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6393 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6396 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6400 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6404 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6408 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6411 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6415 /* goto again; (must use BPF_JA for backward jump) */
6416 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6417 s
[i
]->s
.k
= again
- i
- 1;
6418 s
[i
- 1]->s
.jf
= s
[i
];
6422 for (j
= v6start
; j
<= v6end
; j
++)
6423 s
[j
]->s
.jt
= s
[v6advance
];
6426 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6428 s
[fix2
]->s
.jf
= s
[i
];
6434 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6435 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6436 s
[i
]->s
.jt
= NULL
; /*later*/
6437 s
[i
]->s
.jf
= NULL
; /*later*/
6438 s
[i
]->s
.k
= IPPROTO_AH
;
6440 s
[fix3
]->s
.jf
= s
[ahcheck
];
6447 * X = X + (P[X + 1] + 2) * 4;
6450 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6452 /* A = P[X + packet head]; */
6453 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6454 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6457 s
[i
] = new_stmt(cstate
, BPF_ST
);
6461 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6464 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6468 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6470 /* A = P[X + packet head] */
6471 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6472 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6475 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6479 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6483 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6486 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6490 /* goto again; (must use BPF_JA for backward jump) */
6491 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6492 s
[i
]->s
.k
= again
- i
- 1;
6497 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6499 s
[fix2
]->s
.jt
= s
[end
];
6500 s
[fix4
]->s
.jf
= s
[end
];
6501 s
[fix5
]->s
.jt
= s
[end
];
6508 for (i
= 0; i
< max
- 1; i
++)
6509 s
[i
]->next
= s
[i
+ 1];
6510 s
[max
- 1]->next
= NULL
;
6515 b
= new_block(cstate
, JMP(BPF_JEQ
));
6516 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6519 free_reg(cstate
, reg2
);
6524 #endif /* !defined(NO_PROTOCHAIN) */
6526 static struct block
*
6527 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6530 struct block
*b0
, *b1
;
6533 * A data frame has the 0x08 bit (b3) in the frame control field set
6534 * and the 0x04 bit (b2) clear.
6536 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6537 b0
= new_block(cstate
, JMP(BPF_JSET
));
6541 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6542 b1
= new_block(cstate
, JMP(BPF_JSET
));
6553 * Generate code that checks whether the packet is a packet for protocol
6554 * <proto> and whether the type field in that protocol's header has
6555 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6556 * IP packet and checks the protocol number in the IP header against <v>.
6558 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6559 * against Q_IP and Q_IPV6.
6561 static struct block
*
6562 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6564 struct block
*b0
, *b1
;
6567 if (dir
!= Q_DEFAULT
)
6568 bpf_error(cstate
, "direction applied to 'proto'");
6572 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6573 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6578 return gen_linktype(cstate
, v
);
6582 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6583 * not LLC encapsulation with LLCSAP_IP.
6585 * For IEEE 802 networks - which includes 802.5 token ring
6586 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6587 * says that SNAP encapsulation is used, not LLC encapsulation
6590 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6591 * RFC 2225 say that SNAP encapsulation is used, not LLC
6592 * encapsulation with LLCSAP_IP.
6594 * So we always check for ETHERTYPE_IP.
6596 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6597 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6602 bpf_error(cstate
, "arp does not encapsulate another protocol");
6606 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6610 bpf_error(cstate
, "'sctp proto' is bogus");
6614 bpf_error(cstate
, "'tcp proto' is bogus");
6618 bpf_error(cstate
, "'udp proto' is bogus");
6622 bpf_error(cstate
, "'icmp proto' is bogus");
6626 bpf_error(cstate
, "'igmp proto' is bogus");
6630 bpf_error(cstate
, "'igrp proto' is bogus");
6634 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6638 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6642 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6646 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6650 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6654 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6658 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6660 * Also check for a fragment header before the final
6663 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6664 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6666 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6672 bpf_error(cstate
, "'icmp6 proto' is bogus");
6676 bpf_error(cstate
, "'ah proto' is bogus");
6680 bpf_error(cstate
, "'esp proto' is bogus");
6684 bpf_error(cstate
, "'pim proto' is bogus");
6688 bpf_error(cstate
, "'vrrp proto' is bogus");
6692 bpf_error(cstate
, "'aarp proto' is bogus");
6696 switch (cstate
->linktype
) {
6700 * Frame Relay packets typically have an OSI
6701 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6702 * generates code to check for all the OSI
6703 * NLPIDs, so calling it and then adding a check
6704 * for the particular NLPID for which we're
6705 * looking is bogus, as we can just check for
6708 * What we check for is the NLPID and a frame
6709 * control field value of UI, i.e. 0x03 followed
6712 * XXX - assumes a 2-byte Frame Relay header with
6713 * DLCI and flags. What if the address is longer?
6715 * XXX - what about SNAP-encapsulated frames?
6717 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6723 * Cisco uses an Ethertype lookalike - for OSI,
6726 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6727 /* OSI in C-HDLC is stuffed with a fudge byte */
6728 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6733 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6734 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6740 bpf_error(cstate
, "'esis proto' is bogus");
6744 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6746 * 4 is the offset of the PDU type relative to the IS-IS
6749 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6754 bpf_error(cstate
, "'clnp proto' is not supported");
6758 bpf_error(cstate
, "'stp proto' is bogus");
6762 bpf_error(cstate
, "'ipx proto' is bogus");
6766 bpf_error(cstate
, "'netbeui proto' is bogus");
6770 bpf_error(cstate
, "'l1 proto' is bogus");
6774 bpf_error(cstate
, "'l2 proto' is bogus");
6778 bpf_error(cstate
, "'iih proto' is bogus");
6782 bpf_error(cstate
, "'snp proto' is bogus");
6786 bpf_error(cstate
, "'csnp proto' is bogus");
6790 bpf_error(cstate
, "'psnp proto' is bogus");
6794 bpf_error(cstate
, "'lsp proto' is bogus");
6798 bpf_error(cstate
, "'radio proto' is bogus");
6802 bpf_error(cstate
, "'carp proto' is bogus");
6813 * Convert a non-numeric name to a port number.
6816 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6818 struct addrinfo hints
, *res
, *ai
;
6820 struct sockaddr_in
*in4
;
6822 struct sockaddr_in6
*in6
;
6827 * We check for both TCP and UDP in case there are
6828 * ambiguous entries.
6830 memset(&hints
, 0, sizeof(hints
));
6831 hints
.ai_family
= PF_UNSPEC
;
6832 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6833 hints
.ai_protocol
= ipproto
;
6834 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6841 * No such port. Just return -1.
6848 * We don't use strerror() because it's not
6849 * guaranteed to be thread-safe on all platforms
6850 * (probably because it might use a non-thread-local
6851 * buffer into which to format an error message
6852 * if the error code isn't one for which it has
6853 * a canned string; three cheers for C string
6856 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6858 port
= -2; /* a real error */
6864 * This is a real error, not just "there's
6865 * no such service name".
6867 * We don't use gai_strerror() because it's not
6868 * guaranteed to be thread-safe on all platforms
6869 * (probably because it might use a non-thread-local
6870 * buffer into which to format an error message
6871 * if the error code isn't one for which it has
6872 * a canned string; three cheers for C string
6875 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6877 port
= -2; /* a real error */
6882 * OK, we found it. Did it find anything?
6884 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6886 * Does it have an address?
6888 if (ai
->ai_addr
!= NULL
) {
6890 * Yes. Get a port number; we're done.
6892 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6893 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6894 port
= ntohs(in4
->sin_port
);
6898 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6899 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6900 port
= ntohs(in6
->sin6_port
);
6912 * Convert a string to a port number.
6915 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6925 * See if it's a number.
6927 ret
= stoulen(string
, string_size
, &val
, cstate
);
6931 /* Unknown port type - it's just a number. */
6932 *proto
= PROTO_UNDEF
;
6935 case STOULEN_NOT_OCTAL_NUMBER
:
6936 case STOULEN_NOT_HEX_NUMBER
:
6937 case STOULEN_NOT_DECIMAL_NUMBER
:
6939 * Not a valid number; try looking it up as a port.
6941 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6942 memcpy(cpy
, string
, string_size
);
6943 cpy
[string_size
] = '\0';
6944 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6945 if (tcp_port
== -2) {
6947 * We got a hard error; the error string has
6951 longjmp(cstate
->top_ctx
, 1);
6954 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6955 if (udp_port
== -2) {
6957 * We got a hard error; the error string has
6961 longjmp(cstate
->top_ctx
, 1);
6966 * We need to check /etc/services for ambiguous entries.
6967 * If we find an ambiguous entry, and it has the
6968 * same port number, change the proto to PROTO_UNDEF
6969 * so both TCP and UDP will be checked.
6971 if (tcp_port
>= 0) {
6972 val
= (bpf_u_int32
)tcp_port
;
6973 *proto
= IPPROTO_TCP
;
6974 if (udp_port
>= 0) {
6975 if (udp_port
== tcp_port
)
6976 *proto
= PROTO_UNDEF
;
6979 /* Can't handle ambiguous names that refer
6980 to different port numbers. */
6981 warning("ambiguous port %s in /etc/services",
6988 if (udp_port
>= 0) {
6989 val
= (bpf_u_int32
)udp_port
;
6990 *proto
= IPPROTO_UDP
;
6994 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6996 longjmp(cstate
->top_ctx
, 1);
7003 /* Error already set. */
7004 longjmp(cstate
->top_ctx
, 1);
7011 /* Should not happen */
7012 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
7013 longjmp(cstate
->top_ctx
, 1);
7020 * Convert a string in the form PPP-PPP, which correspond to ports, to
7021 * a starting and ending port in a port range.
7024 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
7025 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
7028 const char *first
, *second
;
7029 size_t first_size
, second_size
;
7032 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
7033 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
7036 * Make sure there are no other hyphens.
7038 * XXX - we support named ports, but there are some port names
7039 * in /etc/services that include hyphens, so this would rule
7042 if (strchr(hyphen_off
+ 1, '-') != NULL
)
7043 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
7047 * Get the length of the first port.
7050 first_size
= hyphen_off
- string
;
7051 if (first_size
== 0) {
7052 /* Range of "-port", which we don't support. */
7053 bpf_error(cstate
, "port range '%s' has no starting port", string
);
7057 * Try to convert it to a port.
7059 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
7060 save_proto
= *proto
;
7063 * Get the length of the second port.
7065 second
= hyphen_off
+ 1;
7066 second_size
= strlen(second
);
7067 if (second_size
== 0) {
7068 /* Range of "port-", which we don't support. */
7069 bpf_error(cstate
, "port range '%s' has no ending port", string
);
7073 * Try to convert it to a port.
7075 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
7076 if (*proto
!= save_proto
)
7077 *proto
= PROTO_UNDEF
;
7081 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
7083 int proto
= q
.proto
;
7087 bpf_u_int32 mask
, addr
;
7088 struct addrinfo
*res
, *res0
;
7089 struct sockaddr_in
*sin4
;
7092 struct sockaddr_in6
*sin6
;
7093 struct in6_addr mask128
;
7095 struct block
*b
, *tmp
;
7096 int port
, real_proto
;
7097 bpf_u_int32 port1
, port2
;
7100 * Catch errors reported by us and routines below us, and return NULL
7103 if (setjmp(cstate
->top_ctx
))
7109 addr
= pcap_nametonetaddr(name
);
7111 bpf_error(cstate
, "unknown network '%s'", name
);
7112 /* Left justify network addr and calculate its network mask */
7114 while (addr
&& (addr
& 0xff000000) == 0) {
7118 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7122 if (proto
== Q_LINK
) {
7123 switch (cstate
->linktype
) {
7126 case DLT_NETANALYZER
:
7127 case DLT_NETANALYZER_TRANSPARENT
:
7128 eaddr
= pcap_ether_hostton(name
);
7131 "unknown ether host '%s'", name
);
7132 tmp
= gen_prevlinkhdr_check(cstate
);
7133 b
= gen_ehostop(cstate
, eaddr
, dir
);
7140 eaddr
= pcap_ether_hostton(name
);
7143 "unknown FDDI host '%s'", name
);
7144 b
= gen_fhostop(cstate
, eaddr
, dir
);
7149 eaddr
= pcap_ether_hostton(name
);
7152 "unknown token ring host '%s'", name
);
7153 b
= gen_thostop(cstate
, eaddr
, dir
);
7157 case DLT_IEEE802_11
:
7158 case DLT_PRISM_HEADER
:
7159 case DLT_IEEE802_11_RADIO_AVS
:
7160 case DLT_IEEE802_11_RADIO
:
7162 eaddr
= pcap_ether_hostton(name
);
7165 "unknown 802.11 host '%s'", name
);
7166 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7170 case DLT_IP_OVER_FC
:
7171 eaddr
= pcap_ether_hostton(name
);
7174 "unknown Fibre Channel host '%s'", name
);
7175 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7180 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7181 } else if (proto
== Q_DECNET
) {
7183 * A long time ago on Ultrix libpcap supported
7184 * translation of DECnet host names into DECnet
7185 * addresses, but this feature is history now.
7187 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7190 memset(&mask128
, 0xff, sizeof(mask128
));
7192 res0
= res
= pcap_nametoaddrinfo(name
);
7194 bpf_error(cstate
, "unknown host '%s'", name
);
7201 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7202 tproto
== Q_DEFAULT
) {
7208 for (res
= res0
; res
; res
= res
->ai_next
) {
7209 switch (res
->ai_family
) {
7212 if (tproto
== Q_IPV6
)
7216 sin4
= (struct sockaddr_in
*)
7218 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7219 0xffffffff, tproto
, dir
, q
.addr
);
7223 if (tproto6
== Q_IP
)
7226 sin6
= (struct sockaddr_in6
*)
7228 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7229 &mask128
, tproto6
, dir
, q
.addr
);
7242 bpf_error(cstate
, "unknown host '%s'%s", name
,
7243 (proto
== Q_DEFAULT
)
7245 : " for specified address family");
7251 if (proto
!= Q_DEFAULT
&&
7252 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7253 bpf_error(cstate
, "illegal qualifier of 'port'");
7254 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7255 bpf_error(cstate
, "unknown port '%s'", name
);
7256 if (proto
== Q_UDP
) {
7257 if (real_proto
== IPPROTO_TCP
)
7258 bpf_error(cstate
, "port '%s' is tcp", name
);
7259 else if (real_proto
== IPPROTO_SCTP
)
7260 bpf_error(cstate
, "port '%s' is sctp", name
);
7262 /* override PROTO_UNDEF */
7263 real_proto
= IPPROTO_UDP
;
7265 if (proto
== Q_TCP
) {
7266 if (real_proto
== IPPROTO_UDP
)
7267 bpf_error(cstate
, "port '%s' is udp", name
);
7269 else if (real_proto
== IPPROTO_SCTP
)
7270 bpf_error(cstate
, "port '%s' is sctp", name
);
7272 /* override PROTO_UNDEF */
7273 real_proto
= IPPROTO_TCP
;
7275 if (proto
== Q_SCTP
) {
7276 if (real_proto
== IPPROTO_UDP
)
7277 bpf_error(cstate
, "port '%s' is udp", name
);
7279 else if (real_proto
== IPPROTO_TCP
)
7280 bpf_error(cstate
, "port '%s' is tcp", name
);
7282 /* override PROTO_UNDEF */
7283 real_proto
= IPPROTO_SCTP
;
7286 bpf_error(cstate
, "illegal port number %d < 0", port
);
7288 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7289 b
= gen_port(cstate
, port
, real_proto
, dir
);
7290 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7294 if (proto
!= Q_DEFAULT
&&
7295 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7296 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7297 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7298 if (proto
== Q_UDP
) {
7299 if (real_proto
== IPPROTO_TCP
)
7300 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7301 else if (real_proto
== IPPROTO_SCTP
)
7302 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7304 /* override PROTO_UNDEF */
7305 real_proto
= IPPROTO_UDP
;
7307 if (proto
== Q_TCP
) {
7308 if (real_proto
== IPPROTO_UDP
)
7309 bpf_error(cstate
, "port in range '%s' is udp", name
);
7310 else if (real_proto
== IPPROTO_SCTP
)
7311 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7313 /* override PROTO_UNDEF */
7314 real_proto
= IPPROTO_TCP
;
7316 if (proto
== Q_SCTP
) {
7317 if (real_proto
== IPPROTO_UDP
)
7318 bpf_error(cstate
, "port in range '%s' is udp", name
);
7319 else if (real_proto
== IPPROTO_TCP
)
7320 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7322 /* override PROTO_UNDEF */
7323 real_proto
= IPPROTO_SCTP
;
7326 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7328 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7330 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7331 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7336 eaddr
= pcap_ether_hostton(name
);
7338 bpf_error(cstate
, "unknown ether host: %s", name
);
7340 res
= pcap_nametoaddrinfo(name
);
7343 bpf_error(cstate
, "unknown host '%s'", name
);
7344 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7348 bpf_error(cstate
, "unknown host '%s'", name
);
7351 bpf_error(cstate
, "'gateway' not supported in this configuration");
7355 real_proto
= lookup_proto(cstate
, name
, proto
);
7356 if (real_proto
>= 0)
7357 return gen_proto(cstate
, real_proto
, proto
, dir
);
7359 bpf_error(cstate
, "unknown protocol: %s", name
);
7361 #if !defined(NO_PROTOCHAIN)
7363 real_proto
= lookup_proto(cstate
, name
, proto
);
7364 if (real_proto
>= 0)
7365 return gen_protochain(cstate
, real_proto
, proto
);
7367 bpf_error(cstate
, "unknown protocol: %s", name
);
7368 #endif /* !defined(NO_PROTOCHAIN) */
7379 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7380 bpf_u_int32 masklen
, struct qual q
)
7382 register int nlen
, mlen
;
7386 * Catch errors reported by us and routines below us, and return NULL
7389 if (setjmp(cstate
->top_ctx
))
7392 nlen
= __pcap_atoin(s1
, &n
);
7394 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7395 /* Promote short ipaddr */
7399 mlen
= __pcap_atoin(s2
, &m
);
7401 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7402 /* Promote short ipaddr */
7405 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7408 /* Convert mask len to mask */
7410 bpf_error(cstate
, "mask length must be <= 32");
7413 * X << 32 is not guaranteed by C to be 0; it's
7418 m
= 0xffffffff << (32 - masklen
);
7420 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7427 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7430 bpf_error(cstate
, "Mask syntax for networks only");
7437 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7445 * Catch errors reported by us and routines below us, and return NULL
7448 if (setjmp(cstate
->top_ctx
))
7455 * v contains a 32-bit unsigned parsed from a string of the
7456 * form {N}, which could be decimal, hexadecimal or octal.
7457 * Although it would be possible to use the value as a raw
7458 * 16-bit DECnet address when the value fits into 16 bits, this
7459 * would be a questionable feature: DECnet address wire
7460 * encoding is little-endian, so this would not work as
7461 * intuitively as the same works for [big-endian] IPv4
7462 * addresses (0x01020304 means 1.2.3.4).
7464 if (proto
== Q_DECNET
)
7465 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7467 } else if (proto
== Q_DECNET
) {
7469 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7470 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7471 * for a valid DECnet address.
7473 vlen
= __pcap_atodn(s
, &v
);
7475 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7478 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7479 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7482 vlen
= __pcap_atoin(s
, &v
);
7484 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7492 if (proto
== Q_DECNET
)
7493 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7494 else if (proto
== Q_LINK
) {
7495 bpf_error(cstate
, "illegal link layer address");
7498 if (s
== NULL
&& q
.addr
== Q_NET
) {
7499 /* Promote short net number */
7500 while (v
&& (v
& 0xff000000) == 0) {
7505 /* Promote short ipaddr */
7507 mask
<<= 32 - vlen
;
7509 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7514 proto
= IPPROTO_UDP
;
7515 else if (proto
== Q_TCP
)
7516 proto
= IPPROTO_TCP
;
7517 else if (proto
== Q_SCTP
)
7518 proto
= IPPROTO_SCTP
;
7519 else if (proto
== Q_DEFAULT
)
7520 proto
= PROTO_UNDEF
;
7522 bpf_error(cstate
, "illegal qualifier of 'port'");
7525 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7529 b
= gen_port(cstate
, v
, proto
, dir
);
7530 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7536 proto
= IPPROTO_UDP
;
7537 else if (proto
== Q_TCP
)
7538 proto
= IPPROTO_TCP
;
7539 else if (proto
== Q_SCTP
)
7540 proto
= IPPROTO_SCTP
;
7541 else if (proto
== Q_DEFAULT
)
7542 proto
= PROTO_UNDEF
;
7544 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7547 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7551 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7552 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7557 bpf_error(cstate
, "'gateway' requires a name");
7561 return gen_proto(cstate
, v
, proto
, dir
);
7563 #if !defined(NO_PROTOCHAIN)
7565 return gen_protochain(cstate
, v
, proto
);
7581 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7584 struct addrinfo
*res
;
7585 struct in6_addr
*addr
;
7586 struct in6_addr mask
;
7588 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7591 * Catch errors reported by us and routines below us, and return NULL
7594 if (setjmp(cstate
->top_ctx
))
7597 res
= pcap_nametoaddrinfo(s
);
7599 bpf_error(cstate
, "invalid ip6 address %s", s
);
7602 bpf_error(cstate
, "%s resolved to multiple address", s
);
7603 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7605 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7606 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7607 memset(&mask
, 0, sizeof(mask
));
7608 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7610 mask
.s6_addr
[masklen
/ 8] =
7611 (0xff << (8 - masklen
% 8)) & 0xff;
7614 memcpy(a
, addr
, sizeof(a
));
7615 memcpy(m
, &mask
, sizeof(m
));
7616 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7617 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7618 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7626 bpf_error(cstate
, "Mask syntax for networks only");
7630 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7636 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7643 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7645 struct block
*b
, *tmp
;
7648 * Catch errors reported by us and routines below us, and return NULL
7651 if (setjmp(cstate
->top_ctx
))
7654 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7655 cstate
->e
= pcap_ether_aton(s
);
7656 if (cstate
->e
== NULL
)
7657 bpf_error(cstate
, "malloc");
7658 switch (cstate
->linktype
) {
7660 case DLT_NETANALYZER
:
7661 case DLT_NETANALYZER_TRANSPARENT
:
7662 tmp
= gen_prevlinkhdr_check(cstate
);
7663 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7668 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7671 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7673 case DLT_IEEE802_11
:
7674 case DLT_PRISM_HEADER
:
7675 case DLT_IEEE802_11_RADIO_AVS
:
7676 case DLT_IEEE802_11_RADIO
:
7678 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7680 case DLT_IP_OVER_FC
:
7681 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7686 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7693 bpf_error(cstate
, "ethernet address used in non-ether expression");
7698 sappend(struct slist
*s0
, struct slist
*s1
)
7701 * This is definitely not the best way to do this, but the
7702 * lists will rarely get long.
7709 static struct slist
*
7710 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7714 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7719 static struct slist
*
7720 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7724 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7730 * Modify "index" to use the value stored into its register as an
7731 * offset relative to the beginning of the header for the protocol
7732 * "proto", and allocate a register and put an item "size" bytes long
7733 * (1, 2, or 4) at that offset into that register, making it the register
7736 static struct arth
*
7737 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7741 struct slist
*s
, *tmp
;
7743 int regno
= alloc_reg(cstate
);
7745 free_reg(cstate
, inst
->regno
);
7749 bpf_error(cstate
, "data size must be 1, 2, or 4");
7766 bpf_error(cstate
, "unsupported index operation");
7770 * The offset is relative to the beginning of the packet
7771 * data, if we have a radio header. (If we don't, this
7774 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7775 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7776 cstate
->linktype
!= DLT_PRISM_HEADER
)
7777 bpf_error(cstate
, "radio information not present in capture");
7780 * Load into the X register the offset computed into the
7781 * register specified by "index".
7783 s
= xfer_to_x(cstate
, inst
);
7786 * Load the item at that offset.
7788 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7790 sappend(inst
->s
, s
);
7795 * The offset is relative to the beginning of
7796 * the link-layer header.
7798 * XXX - what about ATM LANE? Should the index be
7799 * relative to the beginning of the AAL5 frame, so
7800 * that 0 refers to the beginning of the LE Control
7801 * field, or relative to the beginning of the LAN
7802 * frame, so that 0 refers, for Ethernet LANE, to
7803 * the beginning of the destination address?
7805 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7808 * If "s" is non-null, it has code to arrange that the
7809 * X register contains the length of the prefix preceding
7810 * the link-layer header. Add to it the offset computed
7811 * into the register specified by "index", and move that
7812 * into the X register. Otherwise, just load into the X
7813 * register the offset computed into the register specified
7817 sappend(s
, xfer_to_a(cstate
, inst
));
7818 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7819 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7821 s
= xfer_to_x(cstate
, inst
);
7824 * Load the item at the sum of the offset we've put in the
7825 * X register and the offset of the start of the link
7826 * layer header (which is 0 if the radio header is
7827 * variable-length; that header length is what we put
7828 * into the X register and then added to the index).
7830 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7831 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7833 sappend(inst
->s
, s
);
7847 * The offset is relative to the beginning of
7848 * the network-layer header.
7849 * XXX - are there any cases where we want
7850 * cstate->off_nl_nosnap?
7852 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7855 * If "s" is non-null, it has code to arrange that the
7856 * X register contains the variable part of the offset
7857 * of the link-layer payload. Add to it the offset
7858 * computed into the register specified by "index",
7859 * and move that into the X register. Otherwise, just
7860 * load into the X register the offset computed into
7861 * the register specified by "index".
7864 sappend(s
, xfer_to_a(cstate
, inst
));
7865 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7866 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7868 s
= xfer_to_x(cstate
, inst
);
7871 * Load the item at the sum of the offset we've put in the
7872 * X register, the offset of the start of the network
7873 * layer header from the beginning of the link-layer
7874 * payload, and the constant part of the offset of the
7875 * start of the link-layer payload.
7877 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7878 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7880 sappend(inst
->s
, s
);
7883 * Do the computation only if the packet contains
7884 * the protocol in question.
7886 b
= gen_proto_abbrev_internal(cstate
, proto
);
7888 gen_and(inst
->b
, b
);
7902 * The offset is relative to the beginning of
7903 * the transport-layer header.
7905 * Load the X register with the length of the IPv4 header
7906 * (plus the offset of the link-layer header, if it's
7907 * a variable-length header), in bytes.
7909 * XXX - are there any cases where we want
7910 * cstate->off_nl_nosnap?
7911 * XXX - we should, if we're built with
7912 * IPv6 support, generate code to load either
7913 * IPv4, IPv6, or both, as appropriate.
7915 s
= gen_loadx_iphdrlen(cstate
);
7918 * The X register now contains the sum of the variable
7919 * part of the offset of the link-layer payload and the
7920 * length of the network-layer header.
7922 * Load into the A register the offset relative to
7923 * the beginning of the transport layer header,
7924 * add the X register to that, move that to the
7925 * X register, and load with an offset from the
7926 * X register equal to the sum of the constant part of
7927 * the offset of the link-layer payload and the offset,
7928 * relative to the beginning of the link-layer payload,
7929 * of the network-layer header.
7931 sappend(s
, xfer_to_a(cstate
, inst
));
7932 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7933 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7934 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7935 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7936 sappend(inst
->s
, s
);
7939 * Do the computation only if the packet contains
7940 * the protocol in question - which is true only
7941 * if this is an IP datagram and is the first or
7942 * only fragment of that datagram.
7944 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7946 gen_and(inst
->b
, b
);
7947 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7952 * Do the computation only if the packet contains
7953 * the protocol in question.
7955 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7957 gen_and(inst
->b
, b
);
7961 * Check if we have an icmp6 next header
7963 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7965 gen_and(inst
->b
, b
);
7968 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7970 * If "s" is non-null, it has code to arrange that the
7971 * X register contains the variable part of the offset
7972 * of the link-layer payload. Add to it the offset
7973 * computed into the register specified by "index",
7974 * and move that into the X register. Otherwise, just
7975 * load into the X register the offset computed into
7976 * the register specified by "index".
7979 sappend(s
, xfer_to_a(cstate
, inst
));
7980 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7981 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7983 s
= xfer_to_x(cstate
, inst
);
7986 * Load the item at the sum of the offset we've put in the
7987 * X register, the offset of the start of the network
7988 * layer header from the beginning of the link-layer
7989 * payload, and the constant part of the offset of the
7990 * start of the link-layer payload.
7992 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7993 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7996 sappend(inst
->s
, s
);
8000 inst
->regno
= regno
;
8001 s
= new_stmt(cstate
, BPF_ST
);
8003 sappend(inst
->s
, s
);
8009 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
8013 * Catch errors reported by us and routines below us, and return NULL
8016 if (setjmp(cstate
->top_ctx
))
8019 return gen_load_internal(cstate
, proto
, inst
, size
);
8022 static struct block
*
8023 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8024 struct arth
*a1
, int reversed
)
8026 struct slist
*s0
, *s1
, *s2
;
8027 struct block
*b
, *tmp
;
8029 s0
= xfer_to_x(cstate
, a1
);
8030 s1
= xfer_to_a(cstate
, a0
);
8031 if (code
== BPF_JEQ
) {
8032 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
8033 b
= new_block(cstate
, JMP(code
));
8037 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
8043 sappend(a0
->s
, a1
->s
);
8047 free_reg(cstate
, a0
->regno
);
8048 free_reg(cstate
, a1
->regno
);
8050 /* 'and' together protocol checks */
8053 gen_and(a0
->b
, tmp
= a1
->b
);
8067 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8068 struct arth
*a1
, int reversed
)
8071 * Catch errors reported by us and routines below us, and return NULL
8074 if (setjmp(cstate
->top_ctx
))
8077 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
8081 gen_loadlen(compiler_state_t
*cstate
)
8088 * Catch errors reported by us and routines below us, and return NULL
8091 if (setjmp(cstate
->top_ctx
))
8094 regno
= alloc_reg(cstate
);
8095 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8096 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8097 s
->next
= new_stmt(cstate
, BPF_ST
);
8098 s
->next
->s
.k
= regno
;
8105 static struct arth
*
8106 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8112 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8114 reg
= alloc_reg(cstate
);
8116 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8118 s
->next
= new_stmt(cstate
, BPF_ST
);
8127 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8130 * Catch errors reported by us and routines below us, and return NULL
8133 if (setjmp(cstate
->top_ctx
))
8136 return gen_loadi_internal(cstate
, val
);
8140 * The a_arg dance is to avoid annoying whining by compilers that
8141 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8142 * It's not *used* after setjmp returns.
8145 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8147 struct arth
*a
= a_arg
;
8151 * Catch errors reported by us and routines below us, and return NULL
8154 if (setjmp(cstate
->top_ctx
))
8157 s
= xfer_to_a(cstate
, a
);
8159 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8162 s
= new_stmt(cstate
, BPF_ST
);
8170 * The a0_arg dance is to avoid annoying whining by compilers that
8171 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8172 * It's not *used* after setjmp returns.
8175 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8178 struct arth
*a0
= a0_arg
;
8179 struct slist
*s0
, *s1
, *s2
;
8182 * Catch errors reported by us and routines below us, and return NULL
8185 if (setjmp(cstate
->top_ctx
))
8189 * Disallow division by, or modulus by, zero; we do this here
8190 * so that it gets done even if the optimizer is disabled.
8192 * Also disallow shifts by a value greater than 31; we do this
8193 * here, for the same reason.
8195 if (code
== BPF_DIV
) {
8196 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8197 bpf_error(cstate
, "division by zero");
8198 } else if (code
== BPF_MOD
) {
8199 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8200 bpf_error(cstate
, "modulus by zero");
8201 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8202 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8203 bpf_error(cstate
, "shift by more than 31 bits");
8205 s0
= xfer_to_x(cstate
, a1
);
8206 s1
= xfer_to_a(cstate
, a0
);
8207 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8212 sappend(a0
->s
, a1
->s
);
8214 free_reg(cstate
, a0
->regno
);
8215 free_reg(cstate
, a1
->regno
);
8217 s0
= new_stmt(cstate
, BPF_ST
);
8218 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8225 * Initialize the table of used registers and the current register.
8228 init_regs(compiler_state_t
*cstate
)
8231 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8235 * Return the next free register.
8238 alloc_reg(compiler_state_t
*cstate
)
8240 int n
= BPF_MEMWORDS
;
8243 if (cstate
->regused
[cstate
->curreg
])
8244 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8246 cstate
->regused
[cstate
->curreg
] = 1;
8247 return cstate
->curreg
;
8250 bpf_error(cstate
, "too many registers needed to evaluate expression");
8255 * Return a register to the table so it can
8259 free_reg(compiler_state_t
*cstate
, int n
)
8261 cstate
->regused
[n
] = 0;
8264 static struct block
*
8265 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8270 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8271 b
= new_block(cstate
, JMP(jmp
));
8279 gen_greater(compiler_state_t
*cstate
, int n
)
8282 * Catch errors reported by us and routines below us, and return NULL
8285 if (setjmp(cstate
->top_ctx
))
8288 return gen_len(cstate
, BPF_JGE
, n
);
8292 * Actually, this is less than or equal.
8295 gen_less(compiler_state_t
*cstate
, int n
)
8300 * Catch errors reported by us and routines below us, and return NULL
8303 if (setjmp(cstate
->top_ctx
))
8306 b
= gen_len(cstate
, BPF_JGT
, n
);
8313 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8314 * the beginning of the link-layer header.
8315 * XXX - that means you can't test values in the radiotap header, but
8316 * as that header is difficult if not impossible to parse generally
8317 * without a loop, that might not be a severe problem. A new keyword
8318 * "radio" could be added for that, although what you'd really want
8319 * would be a way of testing particular radio header values, which
8320 * would generate code appropriate to the radio header in question.
8323 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8329 * Catch errors reported by us and routines below us, and return NULL
8332 if (setjmp(cstate
->top_ctx
))
8340 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8343 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8347 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8351 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8355 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8359 b
= new_block(cstate
, JMP(BPF_JEQ
));
8366 static const u_char abroadcast
[] = { 0x0 };
8369 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8371 bpf_u_int32 hostmask
;
8372 struct block
*b0
, *b1
, *b2
;
8373 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8376 * Catch errors reported by us and routines below us, and return NULL
8379 if (setjmp(cstate
->top_ctx
))
8386 switch (cstate
->linktype
) {
8388 case DLT_ARCNET_LINUX
:
8389 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8391 case DLT_NETANALYZER
:
8392 case DLT_NETANALYZER_TRANSPARENT
:
8393 b1
= gen_prevlinkhdr_check(cstate
);
8394 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8399 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8401 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8402 case DLT_IEEE802_11
:
8403 case DLT_PRISM_HEADER
:
8404 case DLT_IEEE802_11_RADIO_AVS
:
8405 case DLT_IEEE802_11_RADIO
:
8407 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8408 case DLT_IP_OVER_FC
:
8409 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8411 bpf_error(cstate
, "not a broadcast link");
8417 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8418 * as an indication that we don't know the netmask, and fail
8421 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8422 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8423 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8424 hostmask
= ~cstate
->netmask
;
8425 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8426 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8427 ~0 & hostmask
, hostmask
);
8432 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8437 * Generate code to test the low-order bit of a MAC address (that's
8438 * the bottom bit of the *first* byte).
8440 static struct block
*
8441 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8443 register struct block
*b0
;
8444 register struct slist
*s
;
8446 /* link[offset] & 1 != 0 */
8447 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8448 b0
= new_block(cstate
, JMP(BPF_JSET
));
8455 gen_multicast(compiler_state_t
*cstate
, int proto
)
8457 register struct block
*b0
, *b1
, *b2
;
8458 register struct slist
*s
;
8461 * Catch errors reported by us and routines below us, and return NULL
8464 if (setjmp(cstate
->top_ctx
))
8471 switch (cstate
->linktype
) {
8473 case DLT_ARCNET_LINUX
:
8474 /* all ARCnet multicasts use the same address */
8475 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8477 case DLT_NETANALYZER
:
8478 case DLT_NETANALYZER_TRANSPARENT
:
8479 b1
= gen_prevlinkhdr_check(cstate
);
8480 /* ether[0] & 1 != 0 */
8481 b0
= gen_mac_multicast(cstate
, 0);
8487 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8489 * XXX - was that referring to bit-order issues?
8491 /* fddi[1] & 1 != 0 */
8492 return gen_mac_multicast(cstate
, 1);
8494 /* tr[2] & 1 != 0 */
8495 return gen_mac_multicast(cstate
, 2);
8496 case DLT_IEEE802_11
:
8497 case DLT_PRISM_HEADER
:
8498 case DLT_IEEE802_11_RADIO_AVS
:
8499 case DLT_IEEE802_11_RADIO
:
8504 * For control frames, there is no DA.
8506 * For management frames, DA is at an
8507 * offset of 4 from the beginning of
8510 * For data frames, DA is at an offset
8511 * of 4 from the beginning of the packet
8512 * if To DS is clear and at an offset of
8513 * 16 from the beginning of the packet
8518 * Generate the tests to be done for data frames.
8520 * First, check for To DS set, i.e. "link[1] & 0x01".
8522 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8523 b1
= new_block(cstate
, JMP(BPF_JSET
));
8524 b1
->s
.k
= 0x01; /* To DS */
8528 * If To DS is set, the DA is at 16.
8530 b0
= gen_mac_multicast(cstate
, 16);
8534 * Now, check for To DS not set, i.e. check
8535 * "!(link[1] & 0x01)".
8537 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8538 b2
= new_block(cstate
, JMP(BPF_JSET
));
8539 b2
->s
.k
= 0x01; /* To DS */
8544 * If To DS is not set, the DA is at 4.
8546 b1
= gen_mac_multicast(cstate
, 4);
8550 * Now OR together the last two checks. That gives
8551 * the complete set of checks for data frames.
8556 * Now check for a data frame.
8557 * I.e, check "link[0] & 0x08".
8559 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8560 b1
= new_block(cstate
, JMP(BPF_JSET
));
8565 * AND that with the checks done for data frames.
8570 * If the high-order bit of the type value is 0, this
8571 * is a management frame.
8572 * I.e, check "!(link[0] & 0x08)".
8574 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8575 b2
= new_block(cstate
, JMP(BPF_JSET
));
8581 * For management frames, the DA is at 4.
8583 b1
= gen_mac_multicast(cstate
, 4);
8587 * OR that with the checks done for data frames.
8588 * That gives the checks done for management and
8594 * If the low-order bit of the type value is 1,
8595 * this is either a control frame or a frame
8596 * with a reserved type, and thus not a
8599 * I.e., check "!(link[0] & 0x04)".
8601 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8602 b1
= new_block(cstate
, JMP(BPF_JSET
));
8608 * AND that with the checks for data and management
8613 case DLT_IP_OVER_FC
:
8614 b0
= gen_mac_multicast(cstate
, 2);
8619 /* Link not known to support multicasts */
8623 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8624 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8629 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8630 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8634 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8639 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8641 register struct block
*b0
;
8644 * Catch errors reported by us and routines below us, and return NULL
8647 if (setjmp(cstate
->top_ctx
))
8651 * Only some data link types support ifindex qualifiers.
8653 switch (cstate
->linktype
) {
8654 case DLT_LINUX_SLL2
:
8655 /* match packets on this interface */
8656 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8659 #if defined(__linux__)
8661 * This is Linux; we require PF_PACKET support.
8662 * If this is a *live* capture, we can look at
8663 * special meta-data in the filter expression;
8664 * if it's a savefile, we can't.
8666 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8667 /* We have a FILE *, so this is a savefile */
8668 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8669 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8673 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8675 #else /* defined(__linux__) */
8676 bpf_error(cstate
, "ifindex not supported on %s",
8677 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8679 #endif /* defined(__linux__) */
8685 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8686 * Outbound traffic is sent by this machine, while inbound traffic is
8687 * sent by a remote machine (and may include packets destined for a
8688 * unicast or multicast link-layer address we are not subscribing to).
8689 * These are the same definitions implemented by pcap_setdirection().
8690 * Capturing only unicast traffic destined for this host is probably
8691 * better accomplished using a higher-layer filter.
8694 gen_inbound(compiler_state_t
*cstate
, int dir
)
8696 register struct block
*b0
;
8699 * Catch errors reported by us and routines below us, and return NULL
8702 if (setjmp(cstate
->top_ctx
))
8706 * Only some data link types support inbound/outbound qualifiers.
8708 switch (cstate
->linktype
) {
8710 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8711 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8712 gen_loadi_internal(cstate
, 0),
8718 /* match outgoing packets */
8719 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8721 /* match incoming packets */
8722 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8727 /* match outgoing packets */
8728 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8730 /* to filter on inbound traffic, invert the match */
8735 case DLT_LINUX_SLL2
:
8736 /* match outgoing packets */
8737 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8739 /* to filter on inbound traffic, invert the match */
8745 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8746 ((dir
== 0) ? PF_IN
: PF_OUT
));
8751 /* match outgoing packets */
8752 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8754 /* match incoming packets */
8755 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8759 case DLT_JUNIPER_MFR
:
8760 case DLT_JUNIPER_MLFR
:
8761 case DLT_JUNIPER_MLPPP
:
8762 case DLT_JUNIPER_ATM1
:
8763 case DLT_JUNIPER_ATM2
:
8764 case DLT_JUNIPER_PPPOE
:
8765 case DLT_JUNIPER_PPPOE_ATM
:
8766 case DLT_JUNIPER_GGSN
:
8767 case DLT_JUNIPER_ES
:
8768 case DLT_JUNIPER_MONITOR
:
8769 case DLT_JUNIPER_SERVICES
:
8770 case DLT_JUNIPER_ETHER
:
8771 case DLT_JUNIPER_PPP
:
8772 case DLT_JUNIPER_FRELAY
:
8773 case DLT_JUNIPER_CHDLC
:
8774 case DLT_JUNIPER_VP
:
8775 case DLT_JUNIPER_ST
:
8776 case DLT_JUNIPER_ISM
:
8777 case DLT_JUNIPER_VS
:
8778 case DLT_JUNIPER_SRX_E2E
:
8779 case DLT_JUNIPER_FIBRECHANNEL
:
8780 case DLT_JUNIPER_ATM_CEMIC
:
8782 /* juniper flags (including direction) are stored
8783 * the byte after the 3-byte magic number */
8785 /* match outgoing packets */
8786 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8788 /* match incoming packets */
8789 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8795 * If we have packet meta-data indicating a direction,
8796 * and that metadata can be checked by BPF code, check
8797 * it. Otherwise, give up, as this link-layer type has
8798 * nothing in the packet data.
8800 * Currently, the only platform where a BPF filter can
8801 * check that metadata is Linux with the in-kernel
8802 * BPF interpreter. If other packet capture mechanisms
8803 * and BPF filters also supported this, it would be
8804 * nice. It would be even better if they made that
8805 * metadata available so that we could provide it
8806 * with newer capture APIs, allowing it to be saved
8809 #if defined(__linux__)
8811 * This is Linux; we require PF_PACKET support.
8812 * If this is a *live* capture, we can look at
8813 * special meta-data in the filter expression;
8814 * if it's a savefile, we can't.
8816 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8817 /* We have a FILE *, so this is a savefile */
8818 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8819 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8822 /* match outgoing packets */
8823 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8826 /* to filter on inbound traffic, invert the match */
8829 #else /* defined(__linux__) */
8830 bpf_error(cstate
, "inbound/outbound not supported on %s",
8831 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8833 #endif /* defined(__linux__) */
8838 /* PF firewall log matched interface */
8840 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8846 * Catch errors reported by us and routines below us, and return NULL
8849 if (setjmp(cstate
->top_ctx
))
8852 if (cstate
->linktype
!= DLT_PFLOG
) {
8853 bpf_error(cstate
, "ifname supported only on PF linktype");
8856 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8857 off
= offsetof(struct pfloghdr
, ifname
);
8858 if (strlen(ifname
) >= len
) {
8859 bpf_error(cstate
, "ifname interface names can only be %d characters",
8863 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8864 (const u_char
*)ifname
);
8868 /* PF firewall log ruleset name */
8870 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8875 * Catch errors reported by us and routines below us, and return NULL
8878 if (setjmp(cstate
->top_ctx
))
8881 if (cstate
->linktype
!= DLT_PFLOG
) {
8882 bpf_error(cstate
, "ruleset supported only on PF linktype");
8886 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8887 bpf_error(cstate
, "ruleset names can only be %ld characters",
8888 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8892 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8893 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8897 /* PF firewall log rule number */
8899 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8904 * Catch errors reported by us and routines below us, and return NULL
8907 if (setjmp(cstate
->top_ctx
))
8910 if (cstate
->linktype
!= DLT_PFLOG
) {
8911 bpf_error(cstate
, "rnr supported only on PF linktype");
8915 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8920 /* PF firewall log sub-rule number */
8922 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8927 * Catch errors reported by us and routines below us, and return NULL
8930 if (setjmp(cstate
->top_ctx
))
8933 if (cstate
->linktype
!= DLT_PFLOG
) {
8934 bpf_error(cstate
, "srnr supported only on PF linktype");
8938 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8943 /* PF firewall log reason code */
8945 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8950 * Catch errors reported by us and routines below us, and return NULL
8953 if (setjmp(cstate
->top_ctx
))
8956 if (cstate
->linktype
!= DLT_PFLOG
) {
8957 bpf_error(cstate
, "reason supported only on PF linktype");
8961 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8962 (bpf_u_int32
)reason
);
8966 /* PF firewall log action */
8968 gen_pf_action(compiler_state_t
*cstate
, int action
)
8973 * Catch errors reported by us and routines below us, and return NULL
8976 if (setjmp(cstate
->top_ctx
))
8979 if (cstate
->linktype
!= DLT_PFLOG
) {
8980 bpf_error(cstate
, "action supported only on PF linktype");
8984 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8985 (bpf_u_int32
)action
);
8989 /* IEEE 802.11 wireless header */
8991 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8996 * Catch errors reported by us and routines below us, and return NULL
8999 if (setjmp(cstate
->top_ctx
))
9002 switch (cstate
->linktype
) {
9004 case DLT_IEEE802_11
:
9005 case DLT_PRISM_HEADER
:
9006 case DLT_IEEE802_11_RADIO_AVS
:
9007 case DLT_IEEE802_11_RADIO
:
9008 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
9012 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
9020 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
9025 * Catch errors reported by us and routines below us, and return NULL
9028 if (setjmp(cstate
->top_ctx
))
9031 switch (cstate
->linktype
) {
9033 case DLT_IEEE802_11
:
9034 case DLT_PRISM_HEADER
:
9035 case DLT_IEEE802_11_RADIO_AVS
:
9036 case DLT_IEEE802_11_RADIO
:
9040 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
9044 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
9045 IEEE80211_FC1_DIR_MASK
);
9051 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
9056 * Catch errors reported by us and routines below us, and return NULL
9059 if (setjmp(cstate
->top_ctx
))
9062 switch (cstate
->linktype
) {
9065 case DLT_ARCNET_LINUX
:
9066 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
9067 q
.proto
== Q_LINK
) {
9068 cstate
->e
= pcap_ether_aton(s
);
9069 if (cstate
->e
== NULL
)
9070 bpf_error(cstate
, "malloc");
9071 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
9076 bpf_error(cstate
, "ARCnet address used in non-arc expression");
9080 bpf_error(cstate
, "aid supported only on ARCnet");
9085 static struct block
*
9086 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
9088 register struct block
*b0
, *b1
;
9091 /* src comes first, different from Ethernet */
9093 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
9096 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
9099 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9100 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9106 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9107 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9112 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9116 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9120 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9124 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9128 bpf_error(cstate
, "'ra' is only supported on 802.11");
9132 bpf_error(cstate
, "'ta' is only supported on 802.11");
9139 static struct block
*
9140 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9142 struct block
*b0
, *b1
;
9144 /* check for VLAN, including 802.1ad and QinQ */
9145 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9146 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9149 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9155 static struct block
*
9156 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9158 if (vlan_num
> 0x0fff) {
9159 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9162 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9165 static struct block
*
9166 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9169 struct block
*b0
, *b1
;
9171 b0
= gen_vlan_tpid_test(cstate
);
9174 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9180 * Both payload and link header type follow the VLAN tags so that
9181 * both need to be updated.
9183 cstate
->off_linkpl
.constant_part
+= 4;
9184 cstate
->off_linktype
.constant_part
+= 4;
9189 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9190 /* add v to variable part of off */
9192 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9193 bpf_u_int32 v
, struct slist
*s
)
9197 if (!off
->is_variable
)
9198 off
->is_variable
= 1;
9200 off
->reg
= alloc_reg(cstate
);
9202 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9205 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9208 s2
= new_stmt(cstate
, BPF_ST
);
9214 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9215 * and link type offsets first
9218 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9222 /* offset determined at run time, shift variable part */
9224 cstate
->is_vlan_vloffset
= 1;
9225 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9226 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9228 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9229 sappend(s
.next
, b_tpid
->head
->stmts
);
9230 b_tpid
->head
->stmts
= s
.next
;
9234 * patch block b_vid (VLAN id test) to load VID value either from packet
9235 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9238 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9240 struct slist
*s
, *s2
, *sjeq
;
9243 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9244 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9246 /* true -> next instructions, false -> beginning of b_vid */
9247 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9249 sjeq
->s
.jf
= b_vid
->stmts
;
9252 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9253 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
9257 /* Jump to the test in b_vid. We need to jump one instruction before
9258 * the end of the b_vid block so that we only skip loading the TCI
9259 * from packet data and not the 'and' instruction extracting VID.
9262 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9264 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9268 /* insert our statements at the beginning of b_vid */
9269 sappend(s
, b_vid
->stmts
);
9274 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9275 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9276 * tag can be either in metadata or in packet data; therefore if the
9277 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9278 * header for VLAN tag. As the decision is done at run time, we need
9279 * update variable part of the offsets
9281 static struct block
*
9282 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9285 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9288 /* generate new filter code based on extracting packet
9290 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9291 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
9293 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9298 * This is tricky. We need to insert the statements updating variable
9299 * parts of offsets before the traditional TPID and VID tests so
9300 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9301 * we do not want this update to affect those checks. That's why we
9302 * generate both test blocks first and insert the statements updating
9303 * variable parts of both offsets after that. This wouldn't work if
9304 * there already were variable length link header when entering this
9305 * function but gen_vlan_bpf_extensions() isn't called in that case.
9307 b_tpid
= gen_vlan_tpid_test(cstate
);
9309 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9311 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9316 gen_vlan_patch_vid_test(cstate
, b_vid
);
9326 * support IEEE 802.1Q VLAN trunk over ethernet
9329 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9334 * Catch errors reported by us and routines below us, and return NULL
9337 if (setjmp(cstate
->top_ctx
))
9340 /* can't check for VLAN-encapsulated packets inside MPLS */
9341 if (cstate
->label_stack_depth
> 0)
9342 bpf_error(cstate
, "no VLAN match after MPLS");
9345 * Check for a VLAN packet, and then change the offsets to point
9346 * to the type and data fields within the VLAN packet. Just
9347 * increment the offsets, so that we can support a hierarchy, e.g.
9348 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
9351 * XXX - this is a bit of a kludge. If we were to split the
9352 * compiler into a parser that parses an expression and
9353 * generates an expression tree, and a code generator that
9354 * takes an expression tree (which could come from our
9355 * parser or from some other parser) and generates BPF code,
9356 * we could perhaps make the offsets parameters of routines
9357 * and, in the handler for an "AND" node, pass to subnodes
9358 * other than the VLAN node the adjusted offsets.
9360 * This would mean that "vlan" would, instead of changing the
9361 * behavior of *all* tests after it, change only the behavior
9362 * of tests ANDed with it. That would change the documented
9363 * semantics of "vlan", which might break some expressions.
9364 * However, it would mean that "(vlan and ip) or ip" would check
9365 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9366 * checking only for VLAN-encapsulated IP, so that could still
9367 * be considered worth doing; it wouldn't break expressions
9368 * that are of the form "vlan and ..." or "vlan N and ...",
9369 * which I suspect are the most common expressions involving
9370 * "vlan". "vlan or ..." doesn't necessarily do what the user
9371 * would really want, now, as all the "or ..." tests would
9372 * be done assuming a VLAN, even though the "or" could be viewed
9373 * as meaning "or, if this isn't a VLAN packet...".
9375 switch (cstate
->linktype
) {
9379 * Newer version of the Linux kernel pass around
9380 * packets in which the VLAN tag has been removed
9381 * from the packet data and put into metadata.
9383 * This requires special treatment.
9385 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9386 /* Verify that this is the outer part of the packet and
9387 * not encapsulated somehow. */
9388 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9389 cstate
->off_linkhdr
.constant_part
==
9390 cstate
->off_outermostlinkhdr
.constant_part
) {
9392 * Do we need special VLAN handling?
9394 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9395 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9398 b0
= gen_vlan_no_bpf_extensions(cstate
,
9399 vlan_num
, has_vlan_tag
);
9402 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9406 case DLT_NETANALYZER
:
9407 case DLT_NETANALYZER_TRANSPARENT
:
9408 case DLT_IEEE802_11
:
9409 case DLT_PRISM_HEADER
:
9410 case DLT_IEEE802_11_RADIO_AVS
:
9411 case DLT_IEEE802_11_RADIO
:
9413 * These are either Ethernet packets with an additional
9414 * metadata header (the NetAnalyzer types), or 802.11
9415 * packets, possibly with an additional metadata header.
9417 * For the first of those, the VLAN tag is in the normal
9418 * place, so the special-case handling above isn't
9421 * For the second of those, we don't do the special-case
9424 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9428 bpf_error(cstate
, "no VLAN support for %s",
9429 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9433 cstate
->vlan_stack_depth
++;
9441 * The label_num_arg dance is to avoid annoying whining by compilers that
9442 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9443 * It's not *used* after setjmp returns.
9446 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9449 volatile bpf_u_int32 label_num
= label_num_arg
;
9450 struct block
*b0
, *b1
;
9453 * Catch errors reported by us and routines below us, and return NULL
9456 if (setjmp(cstate
->top_ctx
))
9459 if (cstate
->label_stack_depth
> 0) {
9460 /* just match the bottom-of-stack bit clear */
9461 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9464 * We're not in an MPLS stack yet, so check the link-layer
9465 * type against MPLS.
9467 switch (cstate
->linktype
) {
9469 case DLT_C_HDLC
: /* fall through */
9472 case DLT_NETANALYZER
:
9473 case DLT_NETANALYZER_TRANSPARENT
:
9474 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9478 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9481 /* FIXME add other DLT_s ...
9482 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9483 * leave it for now */
9486 bpf_error(cstate
, "no MPLS support for %s",
9487 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9492 /* If a specific MPLS label is requested, check it */
9493 if (has_label_num
) {
9494 if (label_num
> 0xFFFFF) {
9495 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9496 label_num
, 0xFFFFF);
9498 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9499 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9500 0xfffff000); /* only compare the first 20 bits */
9506 * Change the offsets to point to the type and data fields within
9507 * the MPLS packet. Just increment the offsets, so that we
9508 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9509 * capture packets with an outer label of 100000 and an inner
9512 * Increment the MPLS stack depth as well; this indicates that
9513 * we're checking MPLS-encapsulated headers, to make sure higher
9514 * level code generators don't try to match against IP-related
9515 * protocols such as Q_ARP, Q_RARP etc.
9517 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9519 cstate
->off_nl_nosnap
+= 4;
9520 cstate
->off_nl
+= 4;
9521 cstate
->label_stack_depth
++;
9526 * Support PPPOE discovery and session.
9529 gen_pppoed(compiler_state_t
*cstate
)
9532 * Catch errors reported by us and routines below us, and return NULL
9535 if (setjmp(cstate
->top_ctx
))
9538 /* check for PPPoE discovery */
9539 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9543 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9545 struct block
*b0
, *b1
;
9548 * Catch errors reported by us and routines below us, and return NULL
9551 if (setjmp(cstate
->top_ctx
))
9555 * Test against the PPPoE session link-layer type.
9557 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9559 /* If a specific session is requested, check PPPoE session id */
9561 if (sess_num
> 0x0000ffff) {
9562 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9563 sess_num
, 0x0000ffff);
9565 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9571 * Change the offsets to point to the type and data fields within
9572 * the PPP packet, and note that this is PPPoE rather than
9575 * XXX - this is a bit of a kludge. See the comments in
9578 * The "network-layer" protocol is PPPoE, which has a 6-byte
9579 * PPPoE header, followed by a PPP packet.
9581 * There is no HDLC encapsulation for the PPP packet (it's
9582 * encapsulated in PPPoES instead), so the link-layer type
9583 * starts at the first byte of the PPP packet. For PPPoE,
9584 * that offset is relative to the beginning of the total
9585 * link-layer payload, including any 802.2 LLC header, so
9586 * it's 6 bytes past cstate->off_nl.
9588 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9589 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9590 cstate
->off_linkpl
.reg
);
9592 cstate
->off_linktype
= cstate
->off_linkhdr
;
9593 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9596 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9601 /* Check that this is Geneve and the VNI is correct if
9602 * specified. Parameterized to handle both IPv4 and IPv6. */
9603 static struct block
*
9604 gen_geneve_check(compiler_state_t
*cstate
,
9605 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9606 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9608 struct block
*b0
, *b1
;
9610 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9612 /* Check that we are operating on version 0. Otherwise, we
9613 * can't decode the rest of the fields. The version is 2 bits
9614 * in the first byte of the Geneve header. */
9615 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9620 if (vni
> 0xffffff) {
9621 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9624 vni
<<= 8; /* VNI is in the upper 3 bytes */
9625 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9633 /* The IPv4 and IPv6 Geneve checks need to do two things:
9634 * - Verify that this actually is Geneve with the right VNI.
9635 * - Place the IP header length (plus variable link prefix if
9636 * needed) into register A to be used later to compute
9637 * the inner packet offsets. */
9638 static struct block
*
9639 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9641 struct block
*b0
, *b1
;
9642 struct slist
*s
, *s1
;
9644 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9646 /* Load the IP header length into A. */
9647 s
= gen_loadx_iphdrlen(cstate
);
9649 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9652 /* Forcibly append these statements to the true condition
9653 * of the protocol check by creating a new block that is
9654 * always true and ANDing them. */
9655 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9664 static struct block
*
9665 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9667 struct block
*b0
, *b1
;
9668 struct slist
*s
, *s1
;
9670 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9672 /* Load the IP header length. We need to account for a
9673 * variable length link prefix if there is one. */
9674 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9676 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9680 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9684 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9688 /* Forcibly append these statements to the true condition
9689 * of the protocol check by creating a new block that is
9690 * always true and ANDing them. */
9691 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9694 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9703 /* We need to store three values based on the Geneve header::
9704 * - The offset of the linktype.
9705 * - The offset of the end of the Geneve header.
9706 * - The offset of the end of the encapsulated MAC header. */
9707 static struct slist
*
9708 gen_geneve_offsets(compiler_state_t
*cstate
)
9710 struct slist
*s
, *s1
, *s_proto
;
9712 /* First we need to calculate the offset of the Geneve header
9713 * itself. This is composed of the IP header previously calculated
9714 * (include any variable link prefix) and stored in A plus the
9715 * fixed sized headers (fixed link prefix, MAC length, and UDP
9717 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9718 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9720 /* Stash this in X since we'll need it later. */
9721 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9724 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9726 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9730 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9731 cstate
->off_linktype
.is_variable
= 1;
9732 cstate
->off_linktype
.constant_part
= 0;
9734 s1
= new_stmt(cstate
, BPF_ST
);
9735 s1
->s
.k
= cstate
->off_linktype
.reg
;
9738 /* Load the Geneve option length and mask and shift to get the
9739 * number of bytes. It is stored in the first byte of the Geneve
9741 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9745 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9749 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9753 /* Add in the rest of the Geneve base header. */
9754 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9758 /* Add the Geneve header length to its offset and store. */
9759 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9763 /* Set the encapsulated type as Ethernet. Even though we may
9764 * not actually have Ethernet inside there are two reasons this
9766 * - The linktype field is always in EtherType format regardless
9767 * of whether it is in Geneve or an inner Ethernet frame.
9768 * - The only link layer that we have specific support for is
9769 * Ethernet. We will confirm that the packet actually is
9770 * Ethernet at runtime before executing these checks. */
9771 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9773 s1
= new_stmt(cstate
, BPF_ST
);
9774 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9777 /* Calculate whether we have an Ethernet header or just raw IP/
9778 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9779 * and linktype by 14 bytes so that the network header can be found
9780 * seamlessly. Otherwise, keep what we've calculated already. */
9782 /* We have a bare jmp so we can't use the optimizer. */
9783 cstate
->no_optimize
= 1;
9785 /* Load the EtherType in the Geneve header, 2 bytes in. */
9786 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9790 /* Load X with the end of the Geneve header. */
9791 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9792 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9795 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9796 * end of this check, we should have the total length in X. In
9797 * the non-Ethernet case, it's already there. */
9798 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9799 s_proto
->s
.k
= ETHERTYPE_TEB
;
9800 sappend(s
, s_proto
);
9802 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9806 /* Since this is Ethernet, use the EtherType of the payload
9807 * directly as the linktype. Overwrite what we already have. */
9808 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9812 s1
= new_stmt(cstate
, BPF_ST
);
9813 s1
->s
.k
= cstate
->off_linktype
.reg
;
9816 /* Advance two bytes further to get the end of the Ethernet
9818 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9822 /* Move the result to X. */
9823 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9826 /* Store the final result of our linkpl calculation. */
9827 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9828 cstate
->off_linkpl
.is_variable
= 1;
9829 cstate
->off_linkpl
.constant_part
= 0;
9831 s1
= new_stmt(cstate
, BPF_STX
);
9832 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9841 /* Check to see if this is a Geneve packet. */
9843 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9845 struct block
*b0
, *b1
;
9849 * Catch errors reported by us and routines below us, and return NULL
9852 if (setjmp(cstate
->top_ctx
))
9855 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9856 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9861 /* Later filters should act on the payload of the Geneve frame,
9862 * update all of the header pointers. Attach this code so that
9863 * it gets executed in the event that the Geneve filter matches. */
9864 s
= gen_geneve_offsets(cstate
);
9866 b1
= gen_true(cstate
);
9867 sappend(s
, b1
->stmts
);
9872 cstate
->is_encap
= 1;
9877 /* Check that this is VXLAN and the VNI is correct if
9878 * specified. Parameterized to handle both IPv4 and IPv6. */
9879 static struct block
*
9880 gen_vxlan_check(compiler_state_t
*cstate
,
9881 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9882 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9884 struct block
*b0
, *b1
;
9886 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9888 /* Check that the VXLAN header has the flag bits set
9890 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9895 if (vni
> 0xffffff) {
9896 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9899 vni
<<= 8; /* VNI is in the upper 3 bytes */
9900 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9908 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9909 * - Verify that this actually is VXLAN with the right VNI.
9910 * - Place the IP header length (plus variable link prefix if
9911 * needed) into register A to be used later to compute
9912 * the inner packet offsets. */
9913 static struct block
*
9914 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9916 struct block
*b0
, *b1
;
9917 struct slist
*s
, *s1
;
9919 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9921 /* Load the IP header length into A. */
9922 s
= gen_loadx_iphdrlen(cstate
);
9924 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9927 /* Forcibly append these statements to the true condition
9928 * of the protocol check by creating a new block that is
9929 * always true and ANDing them. */
9930 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9939 static struct block
*
9940 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9942 struct block
*b0
, *b1
;
9943 struct slist
*s
, *s1
;
9945 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9947 /* Load the IP header length. We need to account for a
9948 * variable length link prefix if there is one. */
9949 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9951 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9955 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9959 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9963 /* Forcibly append these statements to the true condition
9964 * of the protocol check by creating a new block that is
9965 * always true and ANDing them. */
9966 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9969 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9978 /* We need to store three values based on the VXLAN header:
9979 * - The offset of the linktype.
9980 * - The offset of the end of the VXLAN header.
9981 * - The offset of the end of the encapsulated MAC header. */
9982 static struct slist
*
9983 gen_vxlan_offsets(compiler_state_t
*cstate
)
9985 struct slist
*s
, *s1
;
9987 /* Calculate the offset of the VXLAN header itself. This
9988 * includes the IP header computed previously (including any
9989 * variable link prefix) and stored in A plus the fixed size
9990 * headers (fixed link prefix, MAC length, UDP header). */
9991 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9992 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9994 /* Add the VXLAN header length to its offset and store */
9995 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9999 /* Push the link header. VXLAN packets always contain Ethernet
10001 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
10003 s1
= new_stmt(cstate
, BPF_ST
);
10004 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
10007 /* As the payload is an Ethernet packet, we can use the
10008 * EtherType of the payload directly as the linktype. */
10009 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10013 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
10014 cstate
->off_linktype
.is_variable
= 1;
10015 cstate
->off_linktype
.constant_part
= 0;
10017 s1
= new_stmt(cstate
, BPF_ST
);
10018 s1
->s
.k
= cstate
->off_linktype
.reg
;
10021 /* Two bytes further is the end of the Ethernet header and the
10022 * start of the payload. */
10023 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
10027 /* Move the result to X. */
10028 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
10031 /* Store the final result of our linkpl calculation. */
10032 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
10033 cstate
->off_linkpl
.is_variable
= 1;
10034 cstate
->off_linkpl
.constant_part
= 0;
10036 s1
= new_stmt(cstate
, BPF_STX
);
10037 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10040 cstate
->off_nl
= 0;
10045 /* Check to see if this is a VXLAN packet. */
10047 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
10049 struct block
*b0
, *b1
;
10053 * Catch errors reported by us and routines below us, and return NULL
10056 if (setjmp(cstate
->top_ctx
))
10059 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
10060 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
10065 /* Later filters should act on the payload of the VXLAN frame,
10066 * update all of the header pointers. Attach this code so that
10067 * it gets executed in the event that the VXLAN filter matches. */
10068 s
= gen_vxlan_offsets(cstate
);
10070 b1
= gen_true(cstate
);
10071 sappend(s
, b1
->stmts
);
10076 cstate
->is_encap
= 1;
10081 /* Check that the encapsulated frame has a link layer header
10082 * for Ethernet filters. */
10083 static struct block
*
10084 gen_encap_ll_check(compiler_state_t
*cstate
)
10087 struct slist
*s
, *s1
;
10089 /* The easiest way to see if there is a link layer present
10090 * is to check if the link layer header and payload are not
10093 /* Geneve always generates pure variable offsets so we can
10094 * compare only the registers. */
10095 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
10096 s
->s
.k
= cstate
->off_linkhdr
.reg
;
10098 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
10099 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10102 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10110 static struct block
*
10111 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
10112 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10116 switch (atmfield
) {
10119 if (!cstate
->is_atm
)
10120 bpf_error(cstate
, "'vpi' supported only on raw ATM");
10121 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10123 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10124 0xffffffffU
, jtype
, reverse
, jvalue
);
10128 if (!cstate
->is_atm
)
10129 bpf_error(cstate
, "'vci' supported only on raw ATM");
10130 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10132 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10133 0xffffffffU
, jtype
, reverse
, jvalue
);
10137 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10138 abort(); /* XXX - this isn't on FreeBSD */
10139 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10140 0x0fU
, jtype
, reverse
, jvalue
);
10144 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10146 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10147 0xffffffffU
, jtype
, reverse
, jvalue
);
10150 case A_CALLREFTYPE
:
10151 if (!cstate
->is_atm
)
10152 bpf_error(cstate
, "'callref' supported only on raw ATM");
10153 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10155 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10156 0xffffffffU
, jtype
, reverse
, jvalue
);
10165 static struct block
*
10166 gen_atmtype_metac(compiler_state_t
*cstate
)
10168 struct block
*b0
, *b1
;
10170 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10171 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10176 static struct block
*
10177 gen_atmtype_sc(compiler_state_t
*cstate
)
10179 struct block
*b0
, *b1
;
10181 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10182 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10187 static struct block
*
10188 gen_atmtype_llc(compiler_state_t
*cstate
)
10192 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10193 cstate
->linktype
= cstate
->prevlinktype
;
10198 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10199 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10202 * Catch errors reported by us and routines below us, and return NULL
10205 if (setjmp(cstate
->top_ctx
))
10208 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10213 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10215 struct block
*b0
, *b1
;
10218 * Catch errors reported by us and routines below us, and return NULL
10221 if (setjmp(cstate
->top_ctx
))
10227 /* Get all packets in Meta signalling Circuit */
10228 if (!cstate
->is_atm
)
10229 bpf_error(cstate
, "'metac' supported only on raw ATM");
10230 b1
= gen_atmtype_metac(cstate
);
10234 /* Get all packets in Broadcast Circuit*/
10235 if (!cstate
->is_atm
)
10236 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10237 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10238 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10243 /* Get all cells in Segment OAM F4 circuit*/
10244 if (!cstate
->is_atm
)
10245 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10246 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10247 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10252 /* Get all cells in End-to-End OAM F4 Circuit*/
10253 if (!cstate
->is_atm
)
10254 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10255 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10256 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10261 /* Get all packets in connection Signalling Circuit */
10262 if (!cstate
->is_atm
)
10263 bpf_error(cstate
, "'sc' supported only on raw ATM");
10264 b1
= gen_atmtype_sc(cstate
);
10268 /* Get all packets in ILMI Circuit */
10269 if (!cstate
->is_atm
)
10270 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10271 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10272 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10277 /* Get all LANE packets */
10278 if (!cstate
->is_atm
)
10279 bpf_error(cstate
, "'lane' supported only on raw ATM");
10280 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10283 * Arrange that all subsequent tests assume LANE
10284 * rather than LLC-encapsulated packets, and set
10285 * the offsets appropriately for LANE-encapsulated
10288 * We assume LANE means Ethernet, not Token Ring.
10290 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10291 cstate
->off_payload
+ 2, /* Ethernet header */
10293 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10294 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10295 cstate
->off_nl
= 0; /* Ethernet II */
10296 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10300 /* Get all LLC-encapsulated packets */
10301 if (!cstate
->is_atm
)
10302 bpf_error(cstate
, "'llc' supported only on raw ATM");
10303 b1
= gen_atmtype_llc(cstate
);
10313 * Filtering for MTP2 messages based on li value
10314 * FISU, length is null
10315 * LSSU, length is 1 or 2
10316 * MSU, length is 3 or more
10317 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10320 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10322 struct block
*b0
, *b1
;
10325 * Catch errors reported by us and routines below us, and return NULL
10328 if (setjmp(cstate
->top_ctx
))
10334 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10335 (cstate
->linktype
!= DLT_ERF
) &&
10336 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10337 bpf_error(cstate
, "'fisu' supported only on MTP2");
10338 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10339 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10340 0x3fU
, BPF_JEQ
, 0, 0U);
10344 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10345 (cstate
->linktype
!= DLT_ERF
) &&
10346 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10347 bpf_error(cstate
, "'lssu' supported only on MTP2");
10348 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10349 0x3fU
, BPF_JGT
, 1, 2U);
10350 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10351 0x3fU
, BPF_JGT
, 0, 0U);
10356 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10357 (cstate
->linktype
!= DLT_ERF
) &&
10358 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10359 bpf_error(cstate
, "'msu' supported only on MTP2");
10360 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10361 0x3fU
, BPF_JGT
, 0, 2U);
10365 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10366 (cstate
->linktype
!= DLT_ERF
) &&
10367 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10368 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10369 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10370 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10371 0xff80U
, BPF_JEQ
, 0, 0U);
10375 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10376 (cstate
->linktype
!= DLT_ERF
) &&
10377 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10378 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10379 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10380 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10381 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10382 0xff80U
, BPF_JGT
, 0, 0U);
10387 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10388 (cstate
->linktype
!= DLT_ERF
) &&
10389 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10390 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10391 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10392 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10402 * The jvalue_arg dance is to avoid annoying whining by compilers that
10403 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
10404 * It's not *used* after setjmp returns.
10407 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10408 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
10410 volatile bpf_u_int32 jvalue
= jvalue_arg
;
10412 bpf_u_int32 val1
, val2
, val3
;
10419 * Catch errors reported by us and routines below us, and return NULL
10422 if (setjmp(cstate
->top_ctx
))
10425 newoff_sio
= cstate
->off_sio
;
10426 newoff_opc
= cstate
->off_opc
;
10427 newoff_dpc
= cstate
->off_dpc
;
10428 newoff_sls
= cstate
->off_sls
;
10429 switch (mtp3field
) {
10432 newoff_sio
+= 3; /* offset for MTP2_HSL */
10436 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10437 bpf_error(cstate
, "'sio' supported only on SS7");
10438 /* sio coded on 1 byte so max value 255 */
10440 bpf_error(cstate
, "sio value %u too big; max value = 255",
10442 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
10443 jtype
, reverse
, jvalue
);
10451 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10452 bpf_error(cstate
, "'opc' supported only on SS7");
10453 /* opc coded on 14 bits so max value 16383 */
10454 if (jvalue
> 16383)
10455 bpf_error(cstate
, "opc value %u too big; max value = 16383",
10457 /* the following instructions are made to convert jvalue
10458 * to the form used to write opc in an ss7 message*/
10459 val1
= jvalue
& 0x00003c00;
10461 val2
= jvalue
& 0x000003fc;
10463 val3
= jvalue
& 0x00000003;
10465 jvalue
= val1
+ val2
+ val3
;
10466 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
10467 jtype
, reverse
, jvalue
);
10475 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10476 bpf_error(cstate
, "'dpc' supported only on SS7");
10477 /* dpc coded on 14 bits so max value 16383 */
10478 if (jvalue
> 16383)
10479 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
10481 /* the following instructions are made to convert jvalue
10482 * to the forme used to write dpc in an ss7 message*/
10483 val1
= jvalue
& 0x000000ff;
10485 val2
= jvalue
& 0x00003f00;
10487 jvalue
= val1
+ val2
;
10488 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
10489 jtype
, reverse
, jvalue
);
10497 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10498 bpf_error(cstate
, "'sls' supported only on SS7");
10499 /* sls coded on 4 bits so max value 15 */
10501 bpf_error(cstate
, "sls value %u too big; max value = 15",
10503 /* the following instruction is made to convert jvalue
10504 * to the forme used to write sls in an ss7 message*/
10505 jvalue
= jvalue
<< 4;
10506 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
10507 jtype
, reverse
, jvalue
);
10516 static struct block
*
10517 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10522 * Q.2931 signalling protocol messages for handling virtual circuits
10523 * establishment and teardown
10528 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10531 case A_CALLPROCEED
:
10532 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10536 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10540 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10544 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10547 case A_RELEASE_DONE
:
10548 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10558 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10560 struct block
*b0
, *b1
;
10563 * Catch errors reported by us and routines below us, and return NULL
10566 if (setjmp(cstate
->top_ctx
))
10572 if (!cstate
->is_atm
)
10573 bpf_error(cstate
, "'oam' supported only on raw ATM");
10575 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10576 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10578 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10583 if (!cstate
->is_atm
)
10584 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10586 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10587 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10589 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10595 * Get Q.2931 signalling messages for switched
10596 * virtual connection
10598 if (!cstate
->is_atm
)
10599 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10600 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10601 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10603 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10605 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10607 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10609 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10611 b0
= gen_atmtype_sc(cstate
);
10615 case A_METACONNECT
:
10616 if (!cstate
->is_atm
)
10617 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10618 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10619 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10621 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10623 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10625 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10627 b0
= gen_atmtype_metac(cstate
);