2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <netinet/in.h>
43 #include "ethertype.h"
46 #include "ieee80211.h"
50 #include "pcap/ipnet.h"
51 #include "diag-control.h"
52 #include "pcap-util.h"
56 #if defined(__linux__)
57 #include <linux/types.h>
58 #include <linux/if_packet.h>
59 #include <linux/filter.h>
63 #ifdef HAVE_NPCAP_BPF_H
64 /* Defines BPF extensions for Npcap */
65 #include <npcap-bpf.h>
68 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
75 uint16_t u6_addr16
[8];
76 uint32_t u6_addr32
[4];
78 #define s6_addr in6_u.u6_addr8
79 #define s6_addr16 in6_u.u6_addr16
80 #define s6_addr32 in6_u.u6_addr32
81 #define s6_addr64 in6_u.u6_addr64
84 typedef unsigned short sa_family_t
;
86 #define __SOCKADDR_COMMON(sa_prefix) \
87 sa_family_t sa_prefix##family
89 /* Ditto, for IPv6. */
92 __SOCKADDR_COMMON (sin6_
);
93 uint16_t sin6_port
; /* Transport layer port # */
94 uint32_t sin6_flowinfo
; /* IPv6 flow information */
95 struct in6_addr sin6_addr
; /* IPv6 address */
98 #ifndef EAI_ADDRFAMILY
100 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
101 int ai_family
; /* PF_xxx */
102 int ai_socktype
; /* SOCK_xxx */
103 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
104 size_t ai_addrlen
; /* length of ai_addr */
105 char *ai_canonname
; /* canonical name for hostname */
106 struct sockaddr
*ai_addr
; /* binary address */
107 struct addrinfo
*ai_next
; /* next structure in linked list */
109 #endif /* EAI_ADDRFAMILY */
110 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
113 #include <netdb.h> /* for "struct addrinfo" */
115 #include <pcap/namedb.h>
117 #include "nametoaddr.h"
119 #define ETHERMTU 1500
121 #ifndef IPPROTO_HOPOPTS
122 #define IPPROTO_HOPOPTS 0
124 #ifndef IPPROTO_ROUTING
125 #define IPPROTO_ROUTING 43
127 #ifndef IPPROTO_FRAGMENT
128 #define IPPROTO_FRAGMENT 44
130 #ifndef IPPROTO_DSTOPTS
131 #define IPPROTO_DSTOPTS 60
134 #define IPPROTO_SCTP 132
137 #define GENEVE_PORT 6081
138 #define VXLAN_PORT 4789
142 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
146 #define ARCTYPE_IP_OLD 240 /* IP protocol */
147 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
150 #define ARCTYPE_IP 212 /* IP protocol */
151 #define ARCTYPE_ARP 213 /* address resolution protocol */
152 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
154 #define ARCTYPE_ATALK 221 /* Appletalk */
155 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
156 #define ARCTYPE_IPX 250 /* Novell IPX */
158 #define ARCTYPE_INET6 0xc4 /* IPng */
159 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
162 /* Based on UNI3.1 standard by ATM Forum */
164 /* ATM traffic types based on VPI=0 and (the following VCI */
165 #define VCI_PPC 0x05 /* Point-to-point signal msg */
166 #define VCI_BCC 0x02 /* Broadcast signal msg */
167 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
168 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
169 #define VCI_METAC 0x01 /* Meta signal msg */
170 #define VCI_ILMIC 0x10 /* ILMI msg */
172 /* Q.2931 signalling messages */
173 #define CALL_PROCEED 0x02 /* call proceeding */
174 #define CONNECT 0x07 /* connect */
175 #define CONNECT_ACK 0x0f /* connect_ack */
176 #define SETUP 0x05 /* setup */
177 #define RELEASE 0x4d /* release */
178 #define RELEASE_DONE 0x5a /* release_done */
179 #define RESTART 0x46 /* restart */
180 #define RESTART_ACK 0x4e /* restart ack */
181 #define STATUS 0x7d /* status */
182 #define STATUS_ENQ 0x75 /* status ack */
183 #define ADD_PARTY 0x80 /* add party */
184 #define ADD_PARTY_ACK 0x81 /* add party ack */
185 #define ADD_PARTY_REJ 0x82 /* add party rej */
186 #define DROP_PARTY 0x83 /* drop party */
187 #define DROP_PARTY_ACK 0x84 /* drop party ack */
189 /* Information Element Parameters in the signalling messages */
190 #define CAUSE 0x08 /* cause */
191 #define ENDPT_REF 0x54 /* endpoint reference */
192 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
193 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
194 #define CONNECT_ID 0x5a /* connection identifier */
195 #define QOS_PARA 0x5c /* quality of service parameters */
196 #define B_HIGHER 0x5d /* broadband higher layer information */
197 #define B_BEARER 0x5e /* broadband bearer capability */
198 #define B_LOWER 0x5f /* broadband lower information */
199 #define CALLING_PARTY 0x6c /* calling party number */
200 #define CALLED_PARTY 0x70 /* called party number */
204 /* Q.2931 signalling general messages format */
205 #define PROTO_POS 0 /* offset of protocol discriminator */
206 #define CALL_REF_POS 2 /* offset of call reference value */
207 #define MSG_TYPE_POS 5 /* offset of message type */
208 #define MSG_LEN_POS 7 /* offset of message length */
209 #define IE_BEGIN_POS 9 /* offset of first information element */
211 /* format of signalling messages */
214 #define FIELD_BEGIN_POS 4
217 /* SunATM header for ATM packet */
218 #define SUNATM_DIR_POS 0
219 #define SUNATM_VPI_POS 1
220 #define SUNATM_VCI_POS 2
221 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
223 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
224 #define PT_LANE 0x01 /* LANE */
225 #define PT_LLC 0x02 /* LLC encapsulation */
226 #define PT_ILMI 0x05 /* ILMI */
227 #define PT_QSAAL 0x06 /* Q.SAAL */
230 /* Types missing from some systems */
233 * Network layer protocol identifiers
236 #define ISO8473_CLNP 0x81
239 #define ISO9542_ESIS 0x82
241 #ifndef ISO9542X25_ESIS
242 #define ISO9542X25_ESIS 0x8a
244 #ifndef ISO10589_ISIS
245 #define ISO10589_ISIS 0x83
248 #define ISIS_L1_LAN_IIH 15
249 #define ISIS_L2_LAN_IIH 16
250 #define ISIS_PTP_IIH 17
251 #define ISIS_L1_LSP 18
252 #define ISIS_L2_LSP 20
253 #define ISIS_L1_CSNP 24
254 #define ISIS_L2_CSNP 25
255 #define ISIS_L1_PSNP 26
256 #define ISIS_L2_PSNP 27
258 #ifndef ISO8878A_CONS
259 #define ISO8878A_CONS 0x84
261 #ifndef ISO10747_IDRP
262 #define ISO10747_IDRP 0x85
265 // Same as in tcpdump/print-sl.c.
267 #define SLIPDIR_OUT 1
269 #ifdef HAVE_OS_PROTO_H
270 #include "os-proto.h"
273 #define JMP(c) ((c)|BPF_JMP|BPF_K)
276 * "Push" the current value of the link-layer header type and link-layer
277 * header offset onto a "stack", and set a new value. (It's not a
278 * full-blown stack; we keep only the top two items.)
280 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
282 (cs)->prevlinktype = (cs)->linktype; \
283 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
284 (cs)->linktype = (new_linktype); \
285 (cs)->off_linkhdr.is_variable = (new_is_variable); \
286 (cs)->off_linkhdr.constant_part = (new_constant_part); \
287 (cs)->off_linkhdr.reg = (new_reg); \
288 (cs)->is_encap = 0; \
292 * Offset "not set" value.
294 #define OFFSET_NOT_SET 0xffffffffU
297 * Absolute offsets, which are offsets from the beginning of the raw
298 * packet data, are, in the general case, the sum of a variable value
299 * and a constant value; the variable value may be absent, in which
300 * case the offset is only the constant value, and the constant value
301 * may be zero, in which case the offset is only the variable value.
303 * bpf_abs_offset is a structure containing all that information:
305 * is_variable is 1 if there's a variable part.
307 * constant_part is the constant part of the value, possibly zero;
309 * if is_variable is 1, reg is the register number for a register
310 * containing the variable value if the register has been assigned,
320 * Value passed to gen_load_a() to indicate what the offset argument
321 * is relative to the beginning of.
324 OR_PACKET
, /* full packet data */
325 OR_LINKHDR
, /* link-layer header */
326 OR_PREVLINKHDR
, /* previous link-layer header */
327 OR_LLC
, /* 802.2 LLC header */
328 OR_PREVMPLSHDR
, /* previous MPLS header */
329 OR_LINKTYPE
, /* link-layer type */
330 OR_LINKPL
, /* link-layer payload */
331 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
332 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
333 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
337 * We divvy out chunks of memory rather than call malloc each time so
338 * we don't have to worry about leaking memory. It's probably
339 * not a big deal if all this memory was wasted but if this ever
340 * goes into a library that would probably not be a good idea.
342 * XXX - this *is* in a library....
345 #define CHUNK0SIZE 1024
352 * A chunk can store any of:
353 * - a string (guaranteed alignment 1 but present for completeness)
357 * For this simple allocator every allocated chunk gets rounded up to the
358 * alignment needed for any chunk.
369 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
371 /* Code generator state */
373 struct _compiler_state
{
384 int outermostlinktype
;
389 /* Hack for handling VLAN and MPLS stacks. */
390 u_int label_stack_depth
;
391 u_int vlan_stack_depth
;
397 * As errors are handled by a longjmp, anything allocated must
398 * be freed in the longjmp handler, so it must be reachable
401 * One thing that's allocated is the result of pcap_nametoaddrinfo();
402 * it must be freed with freeaddrinfo(). This variable points to
403 * any addrinfo structure that would need to be freed.
408 * Another thing that's allocated is the result of pcap_ether_aton();
409 * it must be freed with free(). This variable points to any
410 * address that would need to be freed.
415 * Various code constructs need to know the layout of the packet.
416 * These values give the necessary offsets from the beginning
417 * of the packet data.
421 * Absolute offset of the beginning of the link-layer header.
423 bpf_abs_offset off_linkhdr
;
426 * If we're checking a link-layer header for a packet encapsulated
427 * in another protocol layer, this is the equivalent information
428 * for the previous layers' link-layer header from the beginning
429 * of the raw packet data.
431 bpf_abs_offset off_prevlinkhdr
;
434 * This is the equivalent information for the outermost layers'
437 bpf_abs_offset off_outermostlinkhdr
;
440 * Absolute offset of the beginning of the link-layer payload.
442 bpf_abs_offset off_linkpl
;
445 * "off_linktype" is the offset to information in the link-layer
446 * header giving the packet type. This is an absolute offset
447 * from the beginning of the packet.
449 * For Ethernet, it's the offset of the Ethernet type field; this
450 * means that it must have a value that skips VLAN tags.
452 * For link-layer types that always use 802.2 headers, it's the
453 * offset of the LLC header; this means that it must have a value
454 * that skips VLAN tags.
456 * For PPP, it's the offset of the PPP type field.
458 * For Cisco HDLC, it's the offset of the CHDLC type field.
460 * For BSD loopback, it's the offset of the AF_ value.
462 * For Linux cooked sockets, it's the offset of the type field.
464 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
465 * encapsulation, in which case, IP is assumed.
467 bpf_abs_offset off_linktype
;
470 * TRUE if the link layer includes an ATM pseudo-header.
474 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
475 * causes us to generate code that checks for a Geneve or
476 * VXLAN header respectively and assume that later filters
477 * apply to the encapsulated payload.
482 * TRUE if we need variable length part of VLAN offset
484 int is_vlan_vloffset
;
487 * These are offsets for the ATM pseudo-header.
494 * These are offsets for the MTP2 fields.
500 * These are offsets for the MTP3 fields.
508 * This is the offset of the first byte after the ATM pseudo_header,
509 * or -1 if there is no ATM pseudo-header.
514 * These are offsets to the beginning of the network-layer header.
515 * They are relative to the beginning of the link-layer payload
516 * (i.e., they don't include off_linkhdr.constant_part or
517 * off_linkpl.constant_part).
519 * If the link layer never uses 802.2 LLC:
521 * "off_nl" and "off_nl_nosnap" are the same.
523 * If the link layer always uses 802.2 LLC:
525 * "off_nl" is the offset if there's a SNAP header following
528 * "off_nl_nosnap" is the offset if there's no SNAP header.
530 * If the link layer is Ethernet:
532 * "off_nl" is the offset if the packet is an Ethernet II packet
533 * (we assume no 802.3+802.2+SNAP);
535 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
536 * with an 802.2 header following it.
542 * Here we handle simple allocation of the scratch registers.
543 * If too many registers are alloc'd, the allocator punts.
545 int regused
[BPF_MEMWORDS
];
551 struct chunk chunks
[NCHUNKS
];
556 * For use by routines outside this file.
560 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
565 * If we've already set an error, don't override it.
566 * The lexical analyzer reports some errors by setting
567 * the error and then returning a LEX_ERROR token, which
568 * is not recognized by any grammar rule, and thus forces
569 * the parse to stop. We don't want the error reported
570 * by the lexical analyzer to be overwritten by the syntax
573 if (!cstate
->error_set
) {
575 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
578 cstate
->error_set
= 1;
583 * For use *ONLY* in routines in this file.
585 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
586 PCAP_PRINTFLIKE(2, 3);
589 static void PCAP_NORETURN
590 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
595 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
598 longjmp(cstate
->top_ctx
, 1);
605 static int init_linktype(compiler_state_t
*, pcap_t
*);
607 static void init_regs(compiler_state_t
*);
608 static int alloc_reg(compiler_state_t
*);
609 static void free_reg(compiler_state_t
*, int);
611 static void initchunks(compiler_state_t
*cstate
);
612 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
613 static void *newchunk(compiler_state_t
*cstate
, size_t);
614 static void freechunks(compiler_state_t
*cstate
);
615 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
616 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
617 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
618 static inline void syntax(compiler_state_t
*cstate
);
620 static void backpatch(struct block
*, struct block
*);
621 static void merge(struct block
*, struct block
*);
622 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
624 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
626 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
628 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
630 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
632 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
633 u_int
, bpf_u_int32
, bpf_u_int32
);
634 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
635 u_int
, const u_char
*);
636 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
637 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
638 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
640 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
642 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
643 static struct block
*gen_uncond(compiler_state_t
*, int);
644 static inline struct block
*gen_true(compiler_state_t
*);
645 static inline struct block
*gen_false(compiler_state_t
*);
646 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
647 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
648 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
649 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
650 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
651 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
652 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
653 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
654 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
655 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
657 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
658 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
659 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
660 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
661 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
664 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
665 struct in6_addr
*, int, u_int
, u_int
);
667 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
668 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
669 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
670 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
671 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
672 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
673 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
674 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
675 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
678 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
679 struct in6_addr
*, int, int, int);
682 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
683 struct addrinfo
*, int, int);
685 static struct block
*gen_ipfrag(compiler_state_t
*);
686 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
687 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
689 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
690 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
692 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
693 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
694 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
696 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
697 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
698 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
699 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
701 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
702 static int lookup_proto(compiler_state_t
*, const char *, int);
703 #if !defined(NO_PROTOCHAIN)
704 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
705 #endif /* !defined(NO_PROTOCHAIN) */
706 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
707 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
708 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
709 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
710 static struct block
*gen_len(compiler_state_t
*, int, int);
711 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
713 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
714 bpf_u_int32
, int, int);
715 static struct block
*gen_atmtype_llc(compiler_state_t
*);
716 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
719 initchunks(compiler_state_t
*cstate
)
723 for (i
= 0; i
< NCHUNKS
; i
++) {
724 cstate
->chunks
[i
].n_left
= 0;
725 cstate
->chunks
[i
].m
= NULL
;
727 cstate
->cur_chunk
= 0;
731 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
737 /* Round up to chunk alignment. */
738 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
740 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
741 if (n
> cp
->n_left
) {
743 k
= ++cstate
->cur_chunk
;
745 bpf_set_error(cstate
, "out of memory");
748 size
= CHUNK0SIZE
<< k
;
749 cp
->m
= (void *)malloc(size
);
751 bpf_set_error(cstate
, "out of memory");
754 memset((char *)cp
->m
, 0, size
);
757 bpf_set_error(cstate
, "out of memory");
762 return (void *)((char *)cp
->m
+ cp
->n_left
);
766 newchunk(compiler_state_t
*cstate
, size_t n
)
770 p
= newchunk_nolongjmp(cstate
, n
);
772 longjmp(cstate
->top_ctx
, 1);
779 freechunks(compiler_state_t
*cstate
)
783 for (i
= 0; i
< NCHUNKS
; ++i
)
784 if (cstate
->chunks
[i
].m
!= NULL
)
785 free(cstate
->chunks
[i
].m
);
789 * A strdup whose allocations are freed after code generation is over.
790 * This is used by the lexical analyzer, so it can't longjmp; it just
791 * returns NULL on an allocation error, and the callers must check
795 sdup(compiler_state_t
*cstate
, const char *s
)
797 size_t n
= strlen(s
) + 1;
798 char *cp
= newchunk_nolongjmp(cstate
, n
);
802 pcapint_strlcpy(cp
, s
, n
);
806 static inline struct block
*
807 new_block(compiler_state_t
*cstate
, int code
)
811 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
818 static inline struct slist
*
819 new_stmt(compiler_state_t
*cstate
, int code
)
823 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
829 static struct block
*
830 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
832 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
838 static struct block
*
839 gen_retblk(compiler_state_t
*cstate
, int v
)
841 if (setjmp(cstate
->top_ctx
)) {
843 * gen_retblk() only fails because a memory
844 * allocation failed in newchunk(), meaning
845 * that it can't return a pointer.
851 return gen_retblk_internal(cstate
, v
);
854 static inline PCAP_NORETURN_DEF
void
855 syntax(compiler_state_t
*cstate
)
857 bpf_error(cstate
, "syntax error in filter expression");
860 static PCAP_NORETURN_DEF
void
861 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
863 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
864 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
868 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
869 const char *buf
, int optimize
, bpf_u_int32 mask
)
875 compiler_state_t cstate
;
876 yyscan_t scanner
= NULL
;
877 YY_BUFFER_STATE in_buffer
= NULL
;
882 * If this pcap_t hasn't been activated, it doesn't have a
883 * link-layer type, so we can't use it.
886 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
887 "not-yet-activated pcap_t passed to pcap_compile");
893 * Initialize Winsock, asking for the latest version (2.2),
894 * as we may be calling Winsock routines to translate
895 * host names to addresses.
897 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
899 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
900 err
, "Error calling WSAStartup()");
907 * If the device on which we're capturing need to be notified
908 * that a new filter is being compiled, do so.
910 * This allows them to save a copy of it, in case, for example,
911 * they're implementing a form of remote packet capture, and
912 * want the remote machine to filter out the packets in which
913 * it's sending the packets it's captured.
915 * XXX - the fact that we happen to be compiling a filter
916 * doesn't necessarily mean we'll be installing it as the
917 * filter for this pcap_t; we might be running it from userland
918 * on captured packets to do packet classification. We really
919 * need a better way of handling this, but this is all that
920 * the WinPcap remote capture code did.
922 if (p
->save_current_filter_op
!= NULL
)
923 (p
->save_current_filter_op
)(p
, buf
);
927 cstate
.no_optimize
= 0;
932 cstate
.ic
.root
= NULL
;
933 cstate
.ic
.cur_mark
= 0;
935 cstate
.error_set
= 0;
938 cstate
.netmask
= mask
;
940 cstate
.snaplen
= pcap_snapshot(p
);
941 if (cstate
.snaplen
== 0) {
942 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
943 "snaplen of 0 rejects all packets");
948 if (pcap_lex_init(&scanner
) != 0) {
949 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
950 errno
, "can't initialize scanner");
954 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
957 * Associate the compiler state with the lexical analyzer
960 pcap_set_extra(&cstate
, scanner
);
962 if (init_linktype(&cstate
, p
) == -1) {
966 if (pcap_parse(scanner
, &cstate
) != 0) {
968 if (cstate
.ai
!= NULL
)
969 freeaddrinfo(cstate
.ai
);
971 if (cstate
.e
!= NULL
)
977 if (cstate
.ic
.root
== NULL
) {
978 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
981 * Catch errors reported by gen_retblk().
983 if (cstate
.ic
.root
== NULL
) {
989 if (optimize
&& !cstate
.no_optimize
) {
990 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
995 if (cstate
.ic
.root
== NULL
||
996 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
997 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
998 "expression rejects all packets");
1003 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1004 cstate
.ic
.root
, &len
, p
->errbuf
);
1005 if (program
->bf_insns
== NULL
) {
1010 program
->bf_len
= len
;
1012 rc
= 0; /* We're all okay */
1016 * Clean up everything for the lexical analyzer.
1018 if (in_buffer
!= NULL
)
1019 pcap__delete_buffer(in_buffer
, scanner
);
1020 if (scanner
!= NULL
)
1021 pcap_lex_destroy(scanner
);
1024 * Clean up our own allocated memory.
1026 freechunks(&cstate
);
1036 * entry point for using the compiler with no pcap open
1037 * pass in all the stuff that is needed explicitly instead.
1040 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1041 struct bpf_program
*program
,
1042 const char *buf
, int optimize
, bpf_u_int32 mask
)
1047 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1049 return (PCAP_ERROR
);
1050 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1056 * Clean up a "struct bpf_program" by freeing all the memory allocated
1060 pcap_freecode(struct bpf_program
*program
)
1062 program
->bf_len
= 0;
1063 if (program
->bf_insns
!= NULL
) {
1064 free((char *)program
->bf_insns
);
1065 program
->bf_insns
= NULL
;
1070 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1071 * which of the jt and jf fields has been resolved and which is a pointer
1072 * back to another unresolved block (or nil). At least one of the fields
1073 * in each block is already resolved.
1076 backpatch(struct block
*list
, struct block
*target
)
1093 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1094 * which of jt and jf is the link.
1097 merge(struct block
*b0
, struct block
*b1
)
1099 register struct block
**p
= &b0
;
1101 /* Find end of list. */
1103 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1105 /* Concatenate the lists. */
1110 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1113 * Catch errors reported by us and routines below us, and return -1
1116 if (setjmp(cstate
->top_ctx
))
1120 * Insert before the statements of the first (root) block any
1121 * statements needed to load the lengths of any variable-length
1122 * headers into registers.
1124 * XXX - a fancier strategy would be to insert those before the
1125 * statements of all blocks that use those lengths and that
1126 * have no predecessors that use them, so that we only compute
1127 * the lengths if we need them. There might be even better
1128 * approaches than that.
1130 * However, those strategies would be more complicated, and
1131 * as we don't generate code to compute a length if the
1132 * program has no tests that use the length, and as most
1133 * tests will probably use those lengths, we would just
1134 * postpone computing the lengths so that it's not done
1135 * for tests that fail early, and it's not clear that's
1138 insert_compute_vloffsets(cstate
, p
->head
);
1141 * For DLT_PPI captures, generate a check of the per-packet
1142 * DLT value to make sure it's DLT_IEEE802_11.
1144 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1145 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1146 * with appropriate Ethernet information and use that rather
1147 * than using something such as DLT_PPI where you don't know
1148 * the link-layer header type until runtime, which, in the
1149 * general case, would force us to generate both Ethernet *and*
1150 * 802.11 code (*and* anything else for which PPI is used)
1151 * and choose between them early in the BPF program?
1153 if (cstate
->linktype
== DLT_PPI
) {
1154 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1155 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1156 gen_and(ppi_dlt_check
, p
);
1159 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1160 p
->sense
= !p
->sense
;
1161 backpatch(p
, gen_retblk_internal(cstate
, 0));
1162 cstate
->ic
.root
= p
->head
;
1167 gen_and(struct block
*b0
, struct block
*b1
)
1169 backpatch(b0
, b1
->head
);
1170 b0
->sense
= !b0
->sense
;
1171 b1
->sense
= !b1
->sense
;
1173 b1
->sense
= !b1
->sense
;
1174 b1
->head
= b0
->head
;
1178 gen_or(struct block
*b0
, struct block
*b1
)
1180 b0
->sense
= !b0
->sense
;
1181 backpatch(b0
, b1
->head
);
1182 b0
->sense
= !b0
->sense
;
1184 b1
->head
= b0
->head
;
1188 gen_not(struct block
*b
)
1190 b
->sense
= !b
->sense
;
1193 static struct block
*
1194 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1195 u_int size
, bpf_u_int32 v
)
1197 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1200 static struct block
*
1201 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1202 u_int size
, bpf_u_int32 v
)
1204 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1207 static struct block
*
1208 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1209 u_int size
, bpf_u_int32 v
)
1211 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1214 static struct block
*
1215 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1216 u_int size
, bpf_u_int32 v
)
1218 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1221 static struct block
*
1222 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1223 u_int size
, bpf_u_int32 v
)
1225 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1228 static struct block
*
1229 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1230 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1232 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1235 static struct block
*
1236 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1237 u_int size
, const u_char
*v
)
1239 register struct block
*b
, *tmp
;
1243 register const u_char
*p
= &v
[size
- 4];
1245 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1253 register const u_char
*p
= &v
[size
- 2];
1255 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1263 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1272 * AND the field of size "size" at offset "offset" relative to the header
1273 * specified by "offrel" with "mask", and compare it with the value "v"
1274 * with the test specified by "jtype"; if "reverse" is true, the test
1275 * should test the opposite of "jtype".
1277 static struct block
*
1278 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1279 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1282 struct slist
*s
, *s2
;
1285 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1287 if (mask
!= 0xffffffff) {
1288 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1293 b
= new_block(cstate
, JMP(jtype
));
1302 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1304 cstate
->pcap_fddipad
= p
->fddipad
;
1307 * We start out with only one link-layer header.
1309 cstate
->outermostlinktype
= pcap_datalink(p
);
1310 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1311 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1312 cstate
->off_outermostlinkhdr
.reg
= -1;
1314 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1315 cstate
->off_prevlinkhdr
.constant_part
= 0;
1316 cstate
->off_prevlinkhdr
.is_variable
= 0;
1317 cstate
->off_prevlinkhdr
.reg
= -1;
1319 cstate
->linktype
= cstate
->outermostlinktype
;
1320 cstate
->off_linkhdr
.constant_part
= 0;
1321 cstate
->off_linkhdr
.is_variable
= 0;
1322 cstate
->off_linkhdr
.reg
= -1;
1327 cstate
->off_linkpl
.constant_part
= 0;
1328 cstate
->off_linkpl
.is_variable
= 0;
1329 cstate
->off_linkpl
.reg
= -1;
1331 cstate
->off_linktype
.constant_part
= 0;
1332 cstate
->off_linktype
.is_variable
= 0;
1333 cstate
->off_linktype
.reg
= -1;
1336 * Assume it's not raw ATM with a pseudo-header, for now.
1339 cstate
->off_vpi
= OFFSET_NOT_SET
;
1340 cstate
->off_vci
= OFFSET_NOT_SET
;
1341 cstate
->off_proto
= OFFSET_NOT_SET
;
1342 cstate
->off_payload
= OFFSET_NOT_SET
;
1345 * And not encapsulated with either Geneve or VXLAN.
1347 cstate
->is_encap
= 0;
1350 * No variable length VLAN offset by default
1352 cstate
->is_vlan_vloffset
= 0;
1355 * And assume we're not doing SS7.
1357 cstate
->off_li
= OFFSET_NOT_SET
;
1358 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1359 cstate
->off_sio
= OFFSET_NOT_SET
;
1360 cstate
->off_opc
= OFFSET_NOT_SET
;
1361 cstate
->off_dpc
= OFFSET_NOT_SET
;
1362 cstate
->off_sls
= OFFSET_NOT_SET
;
1364 cstate
->label_stack_depth
= 0;
1365 cstate
->vlan_stack_depth
= 0;
1367 switch (cstate
->linktype
) {
1370 cstate
->off_linktype
.constant_part
= 2;
1371 cstate
->off_linkpl
.constant_part
= 6;
1372 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1373 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1376 case DLT_ARCNET_LINUX
:
1377 cstate
->off_linktype
.constant_part
= 4;
1378 cstate
->off_linkpl
.constant_part
= 8;
1379 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1380 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1384 cstate
->off_linktype
.constant_part
= 12;
1385 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1386 cstate
->off_nl
= 0; /* Ethernet II */
1387 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1392 * SLIP doesn't have a link level type. The 16 byte
1393 * header is hacked into our SLIP driver.
1395 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1396 cstate
->off_linkpl
.constant_part
= 16;
1398 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1401 case DLT_SLIP_BSDOS
:
1402 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1403 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1405 cstate
->off_linkpl
.constant_part
= 24;
1407 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1412 cstate
->off_linktype
.constant_part
= 0;
1413 cstate
->off_linkpl
.constant_part
= 4;
1415 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1419 cstate
->off_linktype
.constant_part
= 0;
1420 cstate
->off_linkpl
.constant_part
= 12;
1422 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1427 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1428 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1429 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1430 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1431 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1433 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1438 * This does not include the Ethernet header, and
1439 * only covers session state.
1441 cstate
->off_linktype
.constant_part
= 6;
1442 cstate
->off_linkpl
.constant_part
= 8;
1444 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1448 cstate
->off_linktype
.constant_part
= 5;
1449 cstate
->off_linkpl
.constant_part
= 24;
1451 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1456 * FDDI doesn't really have a link-level type field.
1457 * We set "off_linktype" to the offset of the LLC header.
1459 * To check for Ethernet types, we assume that SSAP = SNAP
1460 * is being used and pick out the encapsulated Ethernet type.
1461 * XXX - should we generate code to check for SNAP?
1463 cstate
->off_linktype
.constant_part
= 13;
1464 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1465 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1466 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1467 cstate
->off_nl
= 8; /* 802.2+SNAP */
1468 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1473 * Token Ring doesn't really have a link-level type field.
1474 * We set "off_linktype" to the offset of the LLC header.
1476 * To check for Ethernet types, we assume that SSAP = SNAP
1477 * is being used and pick out the encapsulated Ethernet type.
1478 * XXX - should we generate code to check for SNAP?
1480 * XXX - the header is actually variable-length.
1481 * Some various Linux patched versions gave 38
1482 * as "off_linktype" and 40 as "off_nl"; however,
1483 * if a token ring packet has *no* routing
1484 * information, i.e. is not source-routed, the correct
1485 * values are 20 and 22, as they are in the vanilla code.
1487 * A packet is source-routed iff the uppermost bit
1488 * of the first byte of the source address, at an
1489 * offset of 8, has the uppermost bit set. If the
1490 * packet is source-routed, the total number of bytes
1491 * of routing information is 2 plus bits 0x1F00 of
1492 * the 16-bit value at an offset of 14 (shifted right
1493 * 8 - figure out which byte that is).
1495 cstate
->off_linktype
.constant_part
= 14;
1496 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1497 cstate
->off_nl
= 8; /* 802.2+SNAP */
1498 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1501 case DLT_PRISM_HEADER
:
1502 case DLT_IEEE802_11_RADIO_AVS
:
1503 case DLT_IEEE802_11_RADIO
:
1504 cstate
->off_linkhdr
.is_variable
= 1;
1505 /* Fall through, 802.11 doesn't have a variable link
1506 * prefix but is otherwise the same. */
1509 case DLT_IEEE802_11
:
1511 * 802.11 doesn't really have a link-level type field.
1512 * We set "off_linktype.constant_part" to the offset of
1515 * To check for Ethernet types, we assume that SSAP = SNAP
1516 * is being used and pick out the encapsulated Ethernet type.
1517 * XXX - should we generate code to check for SNAP?
1519 * We also handle variable-length radio headers here.
1520 * The Prism header is in theory variable-length, but in
1521 * practice it's always 144 bytes long. However, some
1522 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1523 * sometimes or always supply an AVS header, so we
1524 * have to check whether the radio header is a Prism
1525 * header or an AVS header, so, in practice, it's
1528 cstate
->off_linktype
.constant_part
= 24;
1529 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1530 cstate
->off_linkpl
.is_variable
= 1;
1531 cstate
->off_nl
= 8; /* 802.2+SNAP */
1532 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1537 * At the moment we treat PPI the same way that we treat
1538 * normal Radiotap encoded packets. The difference is in
1539 * the function that generates the code at the beginning
1540 * to compute the header length. Since this code generator
1541 * of PPI supports bare 802.11 encapsulation only (i.e.
1542 * the encapsulated DLT should be DLT_IEEE802_11) we
1543 * generate code to check for this too.
1545 cstate
->off_linktype
.constant_part
= 24;
1546 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1547 cstate
->off_linkpl
.is_variable
= 1;
1548 cstate
->off_linkhdr
.is_variable
= 1;
1549 cstate
->off_nl
= 8; /* 802.2+SNAP */
1550 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1553 case DLT_ATM_RFC1483
:
1554 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1556 * assume routed, non-ISO PDUs
1557 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1559 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1560 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1561 * latter would presumably be treated the way PPPoE
1562 * should be, so you can do "pppoe and udp port 2049"
1563 * or "pppoa and tcp port 80" and have it check for
1564 * PPPo{A,E} and a PPP protocol of IP and....
1566 cstate
->off_linktype
.constant_part
= 0;
1567 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1568 cstate
->off_nl
= 8; /* 802.2+SNAP */
1569 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1574 * Full Frontal ATM; you get AALn PDUs with an ATM
1578 cstate
->off_vpi
= SUNATM_VPI_POS
;
1579 cstate
->off_vci
= SUNATM_VCI_POS
;
1580 cstate
->off_proto
= PROTO_POS
;
1581 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1582 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1583 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1584 cstate
->off_nl
= 8; /* 802.2+SNAP */
1585 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1591 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1592 cstate
->off_linkpl
.constant_part
= 0;
1594 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1597 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1598 cstate
->off_linktype
.constant_part
= 14;
1599 cstate
->off_linkpl
.constant_part
= 16;
1601 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1604 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1605 cstate
->off_linktype
.constant_part
= 0;
1606 cstate
->off_linkpl
.constant_part
= 20;
1608 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1613 * LocalTalk does have a 1-byte type field in the LLAP header,
1614 * but really it just indicates whether there is a "short" or
1615 * "long" DDP packet following.
1617 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1618 cstate
->off_linkpl
.constant_part
= 0;
1620 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1623 case DLT_IP_OVER_FC
:
1625 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1626 * link-level type field. We set "off_linktype" to the
1627 * offset of the LLC header.
1629 * To check for Ethernet types, we assume that SSAP = SNAP
1630 * is being used and pick out the encapsulated Ethernet type.
1631 * XXX - should we generate code to check for SNAP? RFC
1632 * 2625 says SNAP should be used.
1634 cstate
->off_linktype
.constant_part
= 16;
1635 cstate
->off_linkpl
.constant_part
= 16;
1636 cstate
->off_nl
= 8; /* 802.2+SNAP */
1637 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1642 * XXX - we should set this to handle SNAP-encapsulated
1643 * frames (NLPID of 0x80).
1645 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1646 cstate
->off_linkpl
.constant_part
= 0;
1648 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1652 * the only BPF-interesting FRF.16 frames are non-control frames;
1653 * Frame Relay has a variable length link-layer
1654 * so lets start with offset 4 for now and increments later on (FIXME);
1657 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1658 cstate
->off_linkpl
.constant_part
= 0;
1660 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1663 case DLT_APPLE_IP_OVER_IEEE1394
:
1664 cstate
->off_linktype
.constant_part
= 16;
1665 cstate
->off_linkpl
.constant_part
= 18;
1667 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1670 case DLT_SYMANTEC_FIREWALL
:
1671 cstate
->off_linktype
.constant_part
= 6;
1672 cstate
->off_linkpl
.constant_part
= 44;
1673 cstate
->off_nl
= 0; /* Ethernet II */
1674 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1678 cstate
->off_linktype
.constant_part
= 0;
1679 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1680 cstate
->off_linkpl
.is_variable
= 1;
1682 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1685 case DLT_JUNIPER_MFR
:
1686 case DLT_JUNIPER_MLFR
:
1687 case DLT_JUNIPER_MLPPP
:
1688 case DLT_JUNIPER_PPP
:
1689 case DLT_JUNIPER_CHDLC
:
1690 case DLT_JUNIPER_FRELAY
:
1691 cstate
->off_linktype
.constant_part
= 4;
1692 cstate
->off_linkpl
.constant_part
= 4;
1694 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1697 case DLT_JUNIPER_ATM1
:
1698 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1699 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1701 cstate
->off_nl_nosnap
= 10;
1704 case DLT_JUNIPER_ATM2
:
1705 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1706 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1708 cstate
->off_nl_nosnap
= 10;
1711 /* frames captured on a Juniper PPPoE service PIC
1712 * contain raw ethernet frames */
1713 case DLT_JUNIPER_PPPOE
:
1714 case DLT_JUNIPER_ETHER
:
1715 cstate
->off_linkpl
.constant_part
= 14;
1716 cstate
->off_linktype
.constant_part
= 16;
1717 cstate
->off_nl
= 18; /* Ethernet II */
1718 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1721 case DLT_JUNIPER_PPPOE_ATM
:
1722 cstate
->off_linktype
.constant_part
= 4;
1723 cstate
->off_linkpl
.constant_part
= 6;
1725 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1728 case DLT_JUNIPER_GGSN
:
1729 cstate
->off_linktype
.constant_part
= 6;
1730 cstate
->off_linkpl
.constant_part
= 12;
1732 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1735 case DLT_JUNIPER_ES
:
1736 cstate
->off_linktype
.constant_part
= 6;
1737 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1738 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1739 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1742 case DLT_JUNIPER_MONITOR
:
1743 cstate
->off_linktype
.constant_part
= 12;
1744 cstate
->off_linkpl
.constant_part
= 12;
1745 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1746 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1749 case DLT_BACNET_MS_TP
:
1750 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1751 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1752 cstate
->off_nl
= OFFSET_NOT_SET
;
1753 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1756 case DLT_JUNIPER_SERVICES
:
1757 cstate
->off_linktype
.constant_part
= 12;
1758 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1759 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1760 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1763 case DLT_JUNIPER_VP
:
1764 cstate
->off_linktype
.constant_part
= 18;
1765 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1766 cstate
->off_nl
= OFFSET_NOT_SET
;
1767 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1770 case DLT_JUNIPER_ST
:
1771 cstate
->off_linktype
.constant_part
= 18;
1772 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1773 cstate
->off_nl
= OFFSET_NOT_SET
;
1774 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1777 case DLT_JUNIPER_ISM
:
1778 cstate
->off_linktype
.constant_part
= 8;
1779 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1780 cstate
->off_nl
= OFFSET_NOT_SET
;
1781 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1784 case DLT_JUNIPER_VS
:
1785 case DLT_JUNIPER_SRX_E2E
:
1786 case DLT_JUNIPER_FIBRECHANNEL
:
1787 case DLT_JUNIPER_ATM_CEMIC
:
1788 cstate
->off_linktype
.constant_part
= 8;
1789 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1790 cstate
->off_nl
= OFFSET_NOT_SET
;
1791 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1796 cstate
->off_li_hsl
= 4;
1797 cstate
->off_sio
= 3;
1798 cstate
->off_opc
= 4;
1799 cstate
->off_dpc
= 4;
1800 cstate
->off_sls
= 7;
1801 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1802 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1803 cstate
->off_nl
= OFFSET_NOT_SET
;
1804 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1807 case DLT_MTP2_WITH_PHDR
:
1809 cstate
->off_li_hsl
= 8;
1810 cstate
->off_sio
= 7;
1811 cstate
->off_opc
= 8;
1812 cstate
->off_dpc
= 8;
1813 cstate
->off_sls
= 11;
1814 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1815 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1816 cstate
->off_nl
= OFFSET_NOT_SET
;
1817 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1821 cstate
->off_li
= 22;
1822 cstate
->off_li_hsl
= 24;
1823 cstate
->off_sio
= 23;
1824 cstate
->off_opc
= 24;
1825 cstate
->off_dpc
= 24;
1826 cstate
->off_sls
= 27;
1827 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1828 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1829 cstate
->off_nl
= OFFSET_NOT_SET
;
1830 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1834 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1835 cstate
->off_linkpl
.constant_part
= 4;
1837 cstate
->off_nl_nosnap
= 0;
1842 * Currently, only raw "link[N:M]" filtering is supported.
1844 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1845 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1846 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1847 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1851 cstate
->off_linktype
.constant_part
= 1;
1852 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1854 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1857 case DLT_NETANALYZER
:
1858 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1859 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1860 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1861 cstate
->off_nl
= 0; /* Ethernet II */
1862 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1865 case DLT_NETANALYZER_TRANSPARENT
:
1866 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1867 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1868 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1869 cstate
->off_nl
= 0; /* Ethernet II */
1870 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1875 * For values in the range in which we've assigned new
1876 * DLT_ values, only raw "link[N:M]" filtering is supported.
1878 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1879 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1880 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1881 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1882 cstate
->off_nl
= OFFSET_NOT_SET
;
1883 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1885 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1886 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1892 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1897 * Load a value relative to the specified absolute offset.
1899 static struct slist
*
1900 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1901 u_int offset
, u_int size
)
1903 struct slist
*s
, *s2
;
1905 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1908 * If "s" is non-null, it has code to arrange that the X register
1909 * contains the variable part of the absolute offset, so we
1910 * generate a load relative to that, with an offset of
1911 * abs_offset->constant_part + offset.
1913 * Otherwise, we can do an absolute load with an offset of
1914 * abs_offset->constant_part + offset.
1918 * "s" points to a list of statements that puts the
1919 * variable part of the absolute offset into the X register.
1920 * Do an indirect load, to use the X register as an offset.
1922 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1923 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1927 * There is no variable part of the absolute offset, so
1928 * just do an absolute load.
1930 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1931 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1937 * Load a value relative to the beginning of the specified header.
1939 static struct slist
*
1940 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1943 struct slist
*s
, *s2
;
1946 * Squelch warnings from compilers that *don't* assume that
1947 * offrel always has a valid enum value and therefore don't
1948 * assume that we'll always go through one of the case arms.
1950 * If we have a default case, compilers that *do* assume that
1951 * will then complain about the default case code being
1954 * Damned if you do, damned if you don't.
1961 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1966 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1969 case OR_PREVLINKHDR
:
1970 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1974 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1977 case OR_PREVMPLSHDR
:
1978 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1982 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1985 case OR_LINKPL_NOSNAP
:
1986 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1990 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1995 * Load the X register with the length of the IPv4 header
1996 * (plus the offset of the link-layer header, if it's
1997 * preceded by a variable-length header such as a radio
1998 * header), in bytes.
2000 s
= gen_loadx_iphdrlen(cstate
);
2003 * Load the item at {offset of the link-layer payload} +
2004 * {offset, relative to the start of the link-layer
2005 * payload, of the IPv4 header} + {length of the IPv4 header} +
2006 * {specified offset}.
2008 * If the offset of the link-layer payload is variable,
2009 * the variable part of that offset is included in the
2010 * value in the X register, and we include the constant
2011 * part in the offset of the load.
2013 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2014 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2019 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2026 * Generate code to load into the X register the sum of the length of
2027 * the IPv4 header and the variable part of the offset of the link-layer
2030 static struct slist
*
2031 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2033 struct slist
*s
, *s2
;
2035 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2038 * The offset of the link-layer payload has a variable
2039 * part. "s" points to a list of statements that put
2040 * the variable part of that offset into the X register.
2042 * The 4*([k]&0xf) addressing mode can't be used, as we
2043 * don't have a constant offset, so we have to load the
2044 * value in question into the A register and add to it
2045 * the value from the X register.
2047 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2048 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2050 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2053 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2058 * The A register now contains the length of the IP header.
2059 * We need to add to it the variable part of the offset of
2060 * the link-layer payload, which is still in the X
2061 * register, and move the result into the X register.
2063 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2064 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2067 * The offset of the link-layer payload is a constant,
2068 * so no code was generated to load the (nonexistent)
2069 * variable part of that offset.
2071 * This means we can use the 4*([k]&0xf) addressing
2072 * mode. Load the length of the IPv4 header, which
2073 * is at an offset of cstate->off_nl from the beginning of
2074 * the link-layer payload, and thus at an offset of
2075 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2076 * of the raw packet data, using that addressing mode.
2078 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2079 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2085 static struct block
*
2086 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2091 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2093 b
= new_block(cstate
, JMP(BPF_JEQ
));
2099 static inline struct block
*
2100 gen_true(compiler_state_t
*cstate
)
2102 return gen_uncond(cstate
, 1);
2105 static inline struct block
*
2106 gen_false(compiler_state_t
*cstate
)
2108 return gen_uncond(cstate
, 0);
2112 * Generate code to match a particular packet type.
2114 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2115 * value, if <= ETHERMTU. We use that to determine whether to
2116 * match the type/length field or to check the type/length field for
2117 * a value <= ETHERMTU to see whether it's a type field and then do
2118 * the appropriate test.
2120 static struct block
*
2121 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2123 struct block
*b0
, *b1
;
2129 case LLCSAP_NETBEUI
:
2131 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2132 * so we check the DSAP and SSAP.
2134 * LLCSAP_IP checks for IP-over-802.2, rather
2135 * than IP-over-Ethernet or IP-over-SNAP.
2137 * XXX - should we check both the DSAP and the
2138 * SSAP, like this, or should we check just the
2139 * DSAP, as we do for other types <= ETHERMTU
2140 * (i.e., other SAP values)?
2142 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2143 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2151 * Ethernet_II frames, which are Ethernet
2152 * frames with a frame type of ETHERTYPE_IPX;
2154 * Ethernet_802.3 frames, which are 802.3
2155 * frames (i.e., the type/length field is
2156 * a length field, <= ETHERMTU, rather than
2157 * a type field) with the first two bytes
2158 * after the Ethernet/802.3 header being
2161 * Ethernet_802.2 frames, which are 802.3
2162 * frames with an 802.2 LLC header and
2163 * with the IPX LSAP as the DSAP in the LLC
2166 * Ethernet_SNAP frames, which are 802.3
2167 * frames with an LLC header and a SNAP
2168 * header and with an OUI of 0x000000
2169 * (encapsulated Ethernet) and a protocol
2170 * ID of ETHERTYPE_IPX in the SNAP header.
2172 * XXX - should we generate the same code both
2173 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2177 * This generates code to check both for the
2178 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2180 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2181 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2185 * Now we add code to check for SNAP frames with
2186 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2188 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2192 * Now we generate code to check for 802.3
2193 * frames in general.
2195 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2198 * Now add the check for 802.3 frames before the
2199 * check for Ethernet_802.2 and Ethernet_802.3,
2200 * as those checks should only be done on 802.3
2201 * frames, not on Ethernet frames.
2206 * Now add the check for Ethernet_II frames, and
2207 * do that before checking for the other frame
2210 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2214 case ETHERTYPE_ATALK
:
2215 case ETHERTYPE_AARP
:
2217 * EtherTalk (AppleTalk protocols on Ethernet link
2218 * layer) may use 802.2 encapsulation.
2222 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2223 * we check for an Ethernet type field less or equal than
2224 * 1500, which means it's an 802.3 length field.
2226 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2229 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2230 * SNAP packets with an organization code of
2231 * 0x080007 (Apple, for Appletalk) and a protocol
2232 * type of ETHERTYPE_ATALK (Appletalk).
2234 * 802.2-encapsulated ETHERTYPE_AARP packets are
2235 * SNAP packets with an organization code of
2236 * 0x000000 (encapsulated Ethernet) and a protocol
2237 * type of ETHERTYPE_AARP (Appletalk ARP).
2239 if (ll_proto
== ETHERTYPE_ATALK
)
2240 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2241 else /* ll_proto == ETHERTYPE_AARP */
2242 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2246 * Check for Ethernet encapsulation (Ethertalk
2247 * phase 1?); we just check for the Ethernet
2250 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2256 if (ll_proto
<= ETHERMTU
) {
2258 * This is an LLC SAP value, so the frames
2259 * that match would be 802.2 frames.
2260 * Check that the frame is an 802.2 frame
2261 * (i.e., that the length/type field is
2262 * a length field, <= ETHERMTU) and
2263 * then check the DSAP.
2265 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2266 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2271 * This is an Ethernet type, so compare
2272 * the length/type field with it (if
2273 * the frame is an 802.2 frame, the length
2274 * field will be <= ETHERMTU, and, as
2275 * "ll_proto" is > ETHERMTU, this test
2276 * will fail and the frame won't match,
2277 * which is what we want).
2279 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2284 static struct block
*
2285 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2288 * For DLT_NULL, the link-layer header is a 32-bit word
2289 * containing an AF_ value in *host* byte order, and for
2290 * DLT_ENC, the link-layer header begins with a 32-bit
2291 * word containing an AF_ value in host byte order.
2293 * In addition, if we're reading a saved capture file,
2294 * the host byte order in the capture may not be the
2295 * same as the host byte order on this machine.
2297 * For DLT_LOOP, the link-layer header is a 32-bit
2298 * word containing an AF_ value in *network* byte order.
2300 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2302 * The AF_ value is in host byte order, but the BPF
2303 * interpreter will convert it to network byte order.
2305 * If this is a save file, and it's from a machine
2306 * with the opposite byte order to ours, we byte-swap
2309 * Then we run it through "htonl()", and generate
2310 * code to compare against the result.
2312 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2313 ll_proto
= SWAPLONG(ll_proto
);
2314 ll_proto
= htonl(ll_proto
);
2316 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2320 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2321 * or IPv6 then we have an error.
2323 static struct block
*
2324 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2329 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2332 case ETHERTYPE_IPV6
:
2333 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2340 return gen_false(cstate
);
2344 * Generate code to match a particular packet type.
2346 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2347 * value, if <= ETHERMTU. We use that to determine whether to
2348 * match the type field or to check the type field for the special
2349 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2351 static struct block
*
2352 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2354 struct block
*b0
, *b1
;
2360 case LLCSAP_NETBEUI
:
2362 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2363 * so we check the DSAP and SSAP.
2365 * LLCSAP_IP checks for IP-over-802.2, rather
2366 * than IP-over-Ethernet or IP-over-SNAP.
2368 * XXX - should we check both the DSAP and the
2369 * SSAP, like this, or should we check just the
2370 * DSAP, as we do for other types <= ETHERMTU
2371 * (i.e., other SAP values)?
2373 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2374 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2380 * Ethernet_II frames, which are Ethernet
2381 * frames with a frame type of ETHERTYPE_IPX;
2383 * Ethernet_802.3 frames, which have a frame
2384 * type of LINUX_SLL_P_802_3;
2386 * Ethernet_802.2 frames, which are 802.3
2387 * frames with an 802.2 LLC header (i.e, have
2388 * a frame type of LINUX_SLL_P_802_2) and
2389 * with the IPX LSAP as the DSAP in the LLC
2392 * Ethernet_SNAP frames, which are 802.3
2393 * frames with an LLC header and a SNAP
2394 * header and with an OUI of 0x000000
2395 * (encapsulated Ethernet) and a protocol
2396 * ID of ETHERTYPE_IPX in the SNAP header.
2398 * First, do the checks on LINUX_SLL_P_802_2
2399 * frames; generate the check for either
2400 * Ethernet_802.2 or Ethernet_SNAP frames, and
2401 * then put a check for LINUX_SLL_P_802_2 frames
2404 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2405 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2407 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2411 * Now check for 802.3 frames and OR that with
2412 * the previous test.
2414 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2418 * Now add the check for Ethernet_II frames, and
2419 * do that before checking for the other frame
2422 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2426 case ETHERTYPE_ATALK
:
2427 case ETHERTYPE_AARP
:
2429 * EtherTalk (AppleTalk protocols on Ethernet link
2430 * layer) may use 802.2 encapsulation.
2434 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2435 * we check for the 802.2 protocol type in the
2436 * "Ethernet type" field.
2438 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2441 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2442 * SNAP packets with an organization code of
2443 * 0x080007 (Apple, for Appletalk) and a protocol
2444 * type of ETHERTYPE_ATALK (Appletalk).
2446 * 802.2-encapsulated ETHERTYPE_AARP packets are
2447 * SNAP packets with an organization code of
2448 * 0x000000 (encapsulated Ethernet) and a protocol
2449 * type of ETHERTYPE_AARP (Appletalk ARP).
2451 if (ll_proto
== ETHERTYPE_ATALK
)
2452 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2453 else /* ll_proto == ETHERTYPE_AARP */
2454 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2458 * Check for Ethernet encapsulation (Ethertalk
2459 * phase 1?); we just check for the Ethernet
2462 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2468 if (ll_proto
<= ETHERMTU
) {
2470 * This is an LLC SAP value, so the frames
2471 * that match would be 802.2 frames.
2472 * Check for the 802.2 protocol type
2473 * in the "Ethernet type" field, and
2474 * then check the DSAP.
2476 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2477 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2483 * This is an Ethernet type, so compare
2484 * the length/type field with it (if
2485 * the frame is an 802.2 frame, the length
2486 * field will be <= ETHERMTU, and, as
2487 * "ll_proto" is > ETHERMTU, this test
2488 * will fail and the frame won't match,
2489 * which is what we want).
2491 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2497 * Load a value relative to the beginning of the link-layer header after the
2500 static struct slist
*
2501 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2503 struct slist
*s1
, *s2
;
2506 * Generate code to load the length of the pflog header into
2507 * the register assigned to hold that length, if one has been
2508 * assigned. (If one hasn't been assigned, no code we've
2509 * generated uses that prefix, so we don't need to generate any
2512 if (cstate
->off_linkpl
.reg
!= -1) {
2514 * The length is in the first byte of the header.
2516 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2520 * Round it up to a multiple of 4.
2521 * Add 3, and clear the lower 2 bits.
2523 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2526 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2527 s2
->s
.k
= 0xfffffffc;
2531 * Now allocate a register to hold that value and store
2534 s2
= new_stmt(cstate
, BPF_ST
);
2535 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2539 * Now move it into the X register.
2541 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2549 static struct slist
*
2550 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2552 struct slist
*s1
, *s2
;
2553 struct slist
*sjeq_avs_cookie
;
2554 struct slist
*sjcommon
;
2557 * This code is not compatible with the optimizer, as
2558 * we are generating jmp instructions within a normal
2559 * slist of instructions
2561 cstate
->no_optimize
= 1;
2564 * Generate code to load the length of the radio header into
2565 * the register assigned to hold that length, if one has been
2566 * assigned. (If one hasn't been assigned, no code we've
2567 * generated uses that prefix, so we don't need to generate any
2570 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2571 * or always use the AVS header rather than the Prism header.
2572 * We load a 4-byte big-endian value at the beginning of the
2573 * raw packet data, and see whether, when masked with 0xFFFFF000,
2574 * it's equal to 0x80211000. If so, that indicates that it's
2575 * an AVS header (the masked-out bits are the version number).
2576 * Otherwise, it's a Prism header.
2578 * XXX - the Prism header is also, in theory, variable-length,
2579 * but no known software generates headers that aren't 144
2582 if (cstate
->off_linkhdr
.reg
!= -1) {
2586 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2590 * AND it with 0xFFFFF000.
2592 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2593 s2
->s
.k
= 0xFFFFF000;
2597 * Compare with 0x80211000.
2599 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2600 sjeq_avs_cookie
->s
.k
= 0x80211000;
2601 sappend(s1
, sjeq_avs_cookie
);
2606 * The 4 bytes at an offset of 4 from the beginning of
2607 * the AVS header are the length of the AVS header.
2608 * That field is big-endian.
2610 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2613 sjeq_avs_cookie
->s
.jt
= s2
;
2616 * Now jump to the code to allocate a register
2617 * into which to save the header length and
2618 * store the length there. (The "jump always"
2619 * instruction needs to have the k field set;
2620 * it's added to the PC, so, as we're jumping
2621 * over a single instruction, it should be 1.)
2623 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2625 sappend(s1
, sjcommon
);
2628 * Now for the code that handles the Prism header.
2629 * Just load the length of the Prism header (144)
2630 * into the A register. Have the test for an AVS
2631 * header branch here if we don't have an AVS header.
2633 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2636 sjeq_avs_cookie
->s
.jf
= s2
;
2639 * Now allocate a register to hold that value and store
2640 * it. The code for the AVS header will jump here after
2641 * loading the length of the AVS header.
2643 s2
= new_stmt(cstate
, BPF_ST
);
2644 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2646 sjcommon
->s
.jf
= s2
;
2649 * Now move it into the X register.
2651 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2659 static struct slist
*
2660 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2662 struct slist
*s1
, *s2
;
2665 * Generate code to load the length of the AVS header into
2666 * the register assigned to hold that length, if one has been
2667 * assigned. (If one hasn't been assigned, no code we've
2668 * generated uses that prefix, so we don't need to generate any
2671 if (cstate
->off_linkhdr
.reg
!= -1) {
2673 * The 4 bytes at an offset of 4 from the beginning of
2674 * the AVS header are the length of the AVS header.
2675 * That field is big-endian.
2677 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2681 * Now allocate a register to hold that value and store
2684 s2
= new_stmt(cstate
, BPF_ST
);
2685 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2689 * Now move it into the X register.
2691 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2699 static struct slist
*
2700 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2702 struct slist
*s1
, *s2
;
2705 * Generate code to load the length of the radiotap header into
2706 * the register assigned to hold that length, if one has been
2707 * assigned. (If one hasn't been assigned, no code we've
2708 * generated uses that prefix, so we don't need to generate any
2711 if (cstate
->off_linkhdr
.reg
!= -1) {
2713 * The 2 bytes at offsets of 2 and 3 from the beginning
2714 * of the radiotap header are the length of the radiotap
2715 * header; unfortunately, it's little-endian, so we have
2716 * to load it a byte at a time and construct the value.
2720 * Load the high-order byte, at an offset of 3, shift it
2721 * left a byte, and put the result in the X register.
2723 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2725 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2728 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2732 * Load the next byte, at an offset of 2, and OR the
2733 * value from the X register into it.
2735 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2738 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2742 * Now allocate a register to hold that value and store
2745 s2
= new_stmt(cstate
, BPF_ST
);
2746 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2750 * Now move it into the X register.
2752 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2761 * At the moment we treat PPI as normal Radiotap encoded
2762 * packets. The difference is in the function that generates
2763 * the code at the beginning to compute the header length.
2764 * Since this code generator of PPI supports bare 802.11
2765 * encapsulation only (i.e. the encapsulated DLT should be
2766 * DLT_IEEE802_11) we generate code to check for this too;
2767 * that's done in finish_parse().
2769 static struct slist
*
2770 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2772 struct slist
*s1
, *s2
;
2775 * Generate code to load the length of the radiotap header
2776 * into the register assigned to hold that length, if one has
2779 if (cstate
->off_linkhdr
.reg
!= -1) {
2781 * The 2 bytes at offsets of 2 and 3 from the beginning
2782 * of the radiotap header are the length of the radiotap
2783 * header; unfortunately, it's little-endian, so we have
2784 * to load it a byte at a time and construct the value.
2788 * Load the high-order byte, at an offset of 3, shift it
2789 * left a byte, and put the result in the X register.
2791 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2793 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2796 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2800 * Load the next byte, at an offset of 2, and OR the
2801 * value from the X register into it.
2803 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2806 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2810 * Now allocate a register to hold that value and store
2813 s2
= new_stmt(cstate
, BPF_ST
);
2814 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2818 * Now move it into the X register.
2820 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2829 * Load a value relative to the beginning of the link-layer header after the 802.11
2830 * header, i.e. LLC_SNAP.
2831 * The link-layer header doesn't necessarily begin at the beginning
2832 * of the packet data; there might be a variable-length prefix containing
2833 * radio information.
2835 static struct slist
*
2836 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2839 struct slist
*sjset_data_frame_1
;
2840 struct slist
*sjset_data_frame_2
;
2841 struct slist
*sjset_qos
;
2842 struct slist
*sjset_radiotap_flags_present
;
2843 struct slist
*sjset_radiotap_ext_present
;
2844 struct slist
*sjset_radiotap_tsft_present
;
2845 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2846 struct slist
*s_roundup
;
2848 if (cstate
->off_linkpl
.reg
== -1) {
2850 * No register has been assigned to the offset of
2851 * the link-layer payload, which means nobody needs
2852 * it; don't bother computing it - just return
2853 * what we already have.
2859 * This code is not compatible with the optimizer, as
2860 * we are generating jmp instructions within a normal
2861 * slist of instructions
2863 cstate
->no_optimize
= 1;
2866 * If "s" is non-null, it has code to arrange that the X register
2867 * contains the length of the prefix preceding the link-layer
2870 * Otherwise, the length of the prefix preceding the link-layer
2871 * header is "off_outermostlinkhdr.constant_part".
2875 * There is no variable-length header preceding the
2876 * link-layer header.
2878 * Load the length of the fixed-length prefix preceding
2879 * the link-layer header (if any) into the X register,
2880 * and store it in the cstate->off_linkpl.reg register.
2881 * That length is off_outermostlinkhdr.constant_part.
2883 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2884 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2888 * The X register contains the offset of the beginning of the
2889 * link-layer header; add 24, which is the minimum length
2890 * of the MAC header for a data frame, to that, and store it
2891 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2892 * which is at the offset in the X register, with an indexed load.
2894 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2896 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2899 s2
= new_stmt(cstate
, BPF_ST
);
2900 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2903 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2908 * Check the Frame Control field to see if this is a data frame;
2909 * a data frame has the 0x08 bit (b3) in that field set and the
2910 * 0x04 bit (b2) clear.
2912 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2913 sjset_data_frame_1
->s
.k
= 0x08;
2914 sappend(s
, sjset_data_frame_1
);
2917 * If b3 is set, test b2, otherwise go to the first statement of
2918 * the rest of the program.
2920 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2921 sjset_data_frame_2
->s
.k
= 0x04;
2922 sappend(s
, sjset_data_frame_2
);
2923 sjset_data_frame_1
->s
.jf
= snext
;
2926 * If b2 is not set, this is a data frame; test the QoS bit.
2927 * Otherwise, go to the first statement of the rest of the
2930 sjset_data_frame_2
->s
.jt
= snext
;
2931 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2932 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2933 sappend(s
, sjset_qos
);
2936 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2938 * Otherwise, go to the first statement of the rest of the
2941 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2942 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2944 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2947 s2
= new_stmt(cstate
, BPF_ST
);
2948 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2952 * If we have a radiotap header, look at it to see whether
2953 * there's Atheros padding between the MAC-layer header
2956 * Note: all of the fields in the radiotap header are
2957 * little-endian, so we byte-swap all of the values
2958 * we test against, as they will be loaded as big-endian
2961 * XXX - in the general case, we would have to scan through
2962 * *all* the presence bits, if there's more than one word of
2963 * presence bits. That would require a loop, meaning that
2964 * we wouldn't be able to run the filter in the kernel.
2966 * We assume here that the Atheros adapters that insert the
2967 * annoying padding don't have multiple antennae and therefore
2968 * do not generate radiotap headers with multiple presence words.
2970 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2972 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2973 * in the first presence flag word?
2975 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2979 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2980 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2981 sappend(s
, sjset_radiotap_flags_present
);
2984 * If not, skip all of this.
2986 sjset_radiotap_flags_present
->s
.jf
= snext
;
2989 * Otherwise, is the "extension" bit set in that word?
2991 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2992 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2993 sappend(s
, sjset_radiotap_ext_present
);
2994 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2997 * If so, skip all of this.
2999 sjset_radiotap_ext_present
->s
.jt
= snext
;
3002 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3004 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3005 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3006 sappend(s
, sjset_radiotap_tsft_present
);
3007 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3010 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3011 * at an offset of 16 from the beginning of the raw packet
3012 * data (8 bytes for the radiotap header and 8 bytes for
3015 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3018 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3021 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3023 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3024 sjset_tsft_datapad
->s
.k
= 0x20;
3025 sappend(s
, sjset_tsft_datapad
);
3028 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3029 * at an offset of 8 from the beginning of the raw packet
3030 * data (8 bytes for the radiotap header).
3032 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3035 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3038 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3040 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3041 sjset_notsft_datapad
->s
.k
= 0x20;
3042 sappend(s
, sjset_notsft_datapad
);
3045 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3046 * set, round the length of the 802.11 header to
3047 * a multiple of 4. Do that by adding 3 and then
3048 * dividing by and multiplying by 4, which we do by
3051 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3052 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3053 sappend(s
, s_roundup
);
3054 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3057 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3058 s2
->s
.k
= (bpf_u_int32
)~3;
3060 s2
= new_stmt(cstate
, BPF_ST
);
3061 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3064 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3065 sjset_tsft_datapad
->s
.jf
= snext
;
3066 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3067 sjset_notsft_datapad
->s
.jf
= snext
;
3069 sjset_qos
->s
.jf
= snext
;
3075 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3079 /* There is an implicit dependency between the link
3080 * payload and link header since the payload computation
3081 * includes the variable part of the header. Therefore,
3082 * if nobody else has allocated a register for the link
3083 * header and we need it, do it now. */
3084 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3085 cstate
->off_linkhdr
.reg
== -1)
3086 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3089 * For link-layer types that have a variable-length header
3090 * preceding the link-layer header, generate code to load
3091 * the offset of the link-layer header into the register
3092 * assigned to that offset, if any.
3094 * XXX - this, and the next switch statement, won't handle
3095 * encapsulation of 802.11 or 802.11+radio information in
3096 * some other protocol stack. That's significantly more
3099 switch (cstate
->outermostlinktype
) {
3101 case DLT_PRISM_HEADER
:
3102 s
= gen_load_prism_llprefixlen(cstate
);
3105 case DLT_IEEE802_11_RADIO_AVS
:
3106 s
= gen_load_avs_llprefixlen(cstate
);
3109 case DLT_IEEE802_11_RADIO
:
3110 s
= gen_load_radiotap_llprefixlen(cstate
);
3114 s
= gen_load_ppi_llprefixlen(cstate
);
3123 * For link-layer types that have a variable-length link-layer
3124 * header, generate code to load the offset of the link-layer
3125 * payload into the register assigned to that offset, if any.
3127 switch (cstate
->outermostlinktype
) {
3129 case DLT_IEEE802_11
:
3130 case DLT_PRISM_HEADER
:
3131 case DLT_IEEE802_11_RADIO_AVS
:
3132 case DLT_IEEE802_11_RADIO
:
3134 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3138 s
= gen_load_pflog_llprefixlen(cstate
);
3143 * If there is no initialization yet and we need variable
3144 * length offsets for VLAN, initialize them to zero
3146 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3149 if (cstate
->off_linkpl
.reg
== -1)
3150 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3151 if (cstate
->off_linktype
.reg
== -1)
3152 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3154 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3156 s2
= new_stmt(cstate
, BPF_ST
);
3157 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3159 s2
= new_stmt(cstate
, BPF_ST
);
3160 s2
->s
.k
= cstate
->off_linktype
.reg
;
3165 * If we have any offset-loading code, append all the
3166 * existing statements in the block to those statements,
3167 * and make the resulting list the list of statements
3171 sappend(s
, b
->stmts
);
3177 * Take an absolute offset, and:
3179 * if it has no variable part, return NULL;
3181 * if it has a variable part, generate code to load the register
3182 * containing that variable part into the X register, returning
3183 * a pointer to that code - if no register for that offset has
3184 * been allocated, allocate it first.
3186 * (The code to set that register will be generated later, but will
3187 * be placed earlier in the code sequence.)
3189 static struct slist
*
3190 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3194 if (off
->is_variable
) {
3195 if (off
->reg
== -1) {
3197 * We haven't yet assigned a register for the
3198 * variable part of the offset of the link-layer
3199 * header; allocate one.
3201 off
->reg
= alloc_reg(cstate
);
3205 * Load the register containing the variable part of the
3206 * offset of the link-layer header into the X register.
3208 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3213 * That offset isn't variable, there's no variable part,
3214 * so we don't need to generate any code.
3221 * Map an Ethernet type to the equivalent PPP type.
3224 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3232 case ETHERTYPE_IPV6
:
3233 ll_proto
= PPP_IPV6
;
3237 ll_proto
= PPP_DECNET
;
3240 case ETHERTYPE_ATALK
:
3241 ll_proto
= PPP_APPLE
;
3254 * I'm assuming the "Bridging PDU"s that go
3255 * over PPP are Spanning Tree Protocol
3258 ll_proto
= PPP_BRPDU
;
3269 * Generate any tests that, for encapsulation of a link-layer packet
3270 * inside another protocol stack, need to be done to check for those
3271 * link-layer packets (and that haven't already been done by a check
3272 * for that encapsulation).
3274 static struct block
*
3275 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3279 if (cstate
->is_encap
)
3280 return gen_encap_ll_check(cstate
);
3282 switch (cstate
->prevlinktype
) {
3286 * This is LANE-encapsulated Ethernet; check that the LANE
3287 * packet doesn't begin with an LE Control marker, i.e.
3288 * that it's data, not a control message.
3290 * (We've already generated a test for LANE.)
3292 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3298 * No such tests are necessary.
3306 * The three different values we should check for when checking for an
3307 * IPv6 packet with DLT_NULL.
3309 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3310 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3311 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3314 * Generate code to match a particular packet type by matching the
3315 * link-layer type field or fields in the 802.2 LLC header.
3317 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3318 * value, if <= ETHERMTU.
3320 static struct block
*
3321 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3323 struct block
*b0
, *b1
, *b2
;
3325 /* are we checking MPLS-encapsulated packets? */
3326 if (cstate
->label_stack_depth
> 0)
3327 return gen_mpls_linktype(cstate
, ll_proto
);
3329 switch (cstate
->linktype
) {
3332 case DLT_NETANALYZER
:
3333 case DLT_NETANALYZER_TRANSPARENT
:
3334 /* Geneve has an EtherType regardless of whether there is an
3335 * L2 header. VXLAN always has an EtherType. */
3336 if (!cstate
->is_encap
)
3337 b0
= gen_prevlinkhdr_check(cstate
);
3341 b1
= gen_ether_linktype(cstate
, ll_proto
);
3352 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3356 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3360 case DLT_IEEE802_11
:
3361 case DLT_PRISM_HEADER
:
3362 case DLT_IEEE802_11_RADIO_AVS
:
3363 case DLT_IEEE802_11_RADIO
:
3366 * Check that we have a data frame.
3368 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3369 IEEE80211_FC0_TYPE_DATA
,
3370 IEEE80211_FC0_TYPE_MASK
);
3373 * Now check for the specified link-layer type.
3375 b1
= gen_llc_linktype(cstate
, ll_proto
);
3382 * XXX - check for LLC frames.
3384 return gen_llc_linktype(cstate
, ll_proto
);
3389 * XXX - check for LLC PDUs, as per IEEE 802.5.
3391 return gen_llc_linktype(cstate
, ll_proto
);
3394 case DLT_ATM_RFC1483
:
3396 case DLT_IP_OVER_FC
:
3397 return gen_llc_linktype(cstate
, ll_proto
);
3402 * Check for an LLC-encapsulated version of this protocol;
3403 * if we were checking for LANE, linktype would no longer
3406 * Check for LLC encapsulation and then check the protocol.
3408 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3409 b1
= gen_llc_linktype(cstate
, ll_proto
);
3415 return gen_linux_sll_linktype(cstate
, ll_proto
);
3419 case DLT_SLIP_BSDOS
:
3422 * These types don't provide any type field; packets
3423 * are always IPv4 or IPv6.
3425 * XXX - for IPv4, check for a version number of 4, and,
3426 * for IPv6, check for a version number of 6?
3431 /* Check for a version number of 4. */
3432 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3434 case ETHERTYPE_IPV6
:
3435 /* Check for a version number of 6. */
3436 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3439 return gen_false(cstate
); /* always false */
3445 * Raw IPv4, so no type field.
3447 if (ll_proto
== ETHERTYPE_IP
)
3448 return gen_true(cstate
); /* always true */
3450 /* Checking for something other than IPv4; always false */
3451 return gen_false(cstate
);
3456 * Raw IPv6, so no type field.
3458 if (ll_proto
== ETHERTYPE_IPV6
)
3459 return gen_true(cstate
); /* always true */
3461 /* Checking for something other than IPv6; always false */
3462 return gen_false(cstate
);
3467 case DLT_PPP_SERIAL
:
3470 * We use Ethernet protocol types inside libpcap;
3471 * map them to the corresponding PPP protocol types.
3473 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3474 ethertype_to_ppptype(ll_proto
));
3479 * We use Ethernet protocol types inside libpcap;
3480 * map them to the corresponding PPP protocol types.
3486 * Also check for Van Jacobson-compressed IP.
3487 * XXX - do this for other forms of PPP?
3489 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3490 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3492 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3497 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3498 ethertype_to_ppptype(ll_proto
));
3508 return (gen_loopback_linktype(cstate
, AF_INET
));
3510 case ETHERTYPE_IPV6
:
3512 * AF_ values may, unfortunately, be platform-
3513 * dependent; AF_INET isn't, because everybody
3514 * used 4.2BSD's value, but AF_INET6 is, because
3515 * 4.2BSD didn't have a value for it (given that
3516 * IPv6 didn't exist back in the early 1980's),
3517 * and they all picked their own values.
3519 * This means that, if we're reading from a
3520 * savefile, we need to check for all the
3523 * If we're doing a live capture, we only need
3524 * to check for this platform's value; however,
3525 * Npcap uses 24, which isn't Windows's AF_INET6
3526 * value. (Given the multiple different values,
3527 * programs that read pcap files shouldn't be
3528 * checking for their platform's AF_INET6 value
3529 * anyway, they should check for all of the
3530 * possible values. and they might as well do
3531 * that even for live captures.)
3533 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3535 * Savefile - check for all three
3536 * possible IPv6 values.
3538 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3539 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3541 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3546 * Live capture, so we only need to
3547 * check for the value used on this
3552 * Npcap doesn't use Windows's AF_INET6,
3553 * as that collides with AF_IPX on
3554 * some BSDs (both have the value 23).
3555 * Instead, it uses 24.
3557 return (gen_loopback_linktype(cstate
, 24));
3560 return (gen_loopback_linktype(cstate
, AF_INET6
));
3561 #else /* AF_INET6 */
3563 * I guess this platform doesn't support
3564 * IPv6, so we just reject all packets.
3566 return gen_false(cstate
);
3567 #endif /* AF_INET6 */
3573 * Not a type on which we support filtering.
3574 * XXX - support those that have AF_ values
3575 * #defined on this platform, at least?
3577 return gen_false(cstate
);
3582 * af field is host byte order in contrast to the rest of
3585 if (ll_proto
== ETHERTYPE_IP
)
3586 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3588 else if (ll_proto
== ETHERTYPE_IPV6
)
3589 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3592 return gen_false(cstate
);
3596 case DLT_ARCNET_LINUX
:
3598 * XXX should we check for first fragment if the protocol
3604 return gen_false(cstate
);
3606 case ETHERTYPE_IPV6
:
3607 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3611 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3613 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3619 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3621 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3626 case ETHERTYPE_REVARP
:
3627 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3630 case ETHERTYPE_ATALK
:
3631 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3638 case ETHERTYPE_ATALK
:
3639 return gen_true(cstate
);
3641 return gen_false(cstate
);
3647 * XXX - assumes a 2-byte Frame Relay header with
3648 * DLCI and flags. What if the address is longer?
3654 * Check for the special NLPID for IP.
3656 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3658 case ETHERTYPE_IPV6
:
3660 * Check for the special NLPID for IPv6.
3662 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3666 * Check for several OSI protocols.
3668 * Frame Relay packets typically have an OSI
3669 * NLPID at the beginning; we check for each
3672 * What we check for is the NLPID and a frame
3673 * control field of UI, i.e. 0x03 followed
3676 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3677 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3678 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3684 return gen_false(cstate
);
3689 break; // not implemented
3691 case DLT_JUNIPER_MFR
:
3692 case DLT_JUNIPER_MLFR
:
3693 case DLT_JUNIPER_MLPPP
:
3694 case DLT_JUNIPER_ATM1
:
3695 case DLT_JUNIPER_ATM2
:
3696 case DLT_JUNIPER_PPPOE
:
3697 case DLT_JUNIPER_PPPOE_ATM
:
3698 case DLT_JUNIPER_GGSN
:
3699 case DLT_JUNIPER_ES
:
3700 case DLT_JUNIPER_MONITOR
:
3701 case DLT_JUNIPER_SERVICES
:
3702 case DLT_JUNIPER_ETHER
:
3703 case DLT_JUNIPER_PPP
:
3704 case DLT_JUNIPER_FRELAY
:
3705 case DLT_JUNIPER_CHDLC
:
3706 case DLT_JUNIPER_VP
:
3707 case DLT_JUNIPER_ST
:
3708 case DLT_JUNIPER_ISM
:
3709 case DLT_JUNIPER_VS
:
3710 case DLT_JUNIPER_SRX_E2E
:
3711 case DLT_JUNIPER_FIBRECHANNEL
:
3712 case DLT_JUNIPER_ATM_CEMIC
:
3714 /* just lets verify the magic number for now -
3715 * on ATM we may have up to 6 different encapsulations on the wire
3716 * and need a lot of heuristics to figure out that the payload
3719 * FIXME encapsulation specific BPF_ filters
3721 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3723 case DLT_BACNET_MS_TP
:
3724 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3727 return gen_ipnet_linktype(cstate
, ll_proto
);
3729 case DLT_LINUX_IRDA
:
3732 case DLT_MTP2_WITH_PHDR
:
3735 case DLT_LINUX_LAPD
:
3736 case DLT_USB_FREEBSD
:
3738 case DLT_USB_LINUX_MMAPPED
:
3740 case DLT_BLUETOOTH_HCI_H4
:
3741 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3743 case DLT_CAN_SOCKETCAN
:
3744 case DLT_IEEE802_15_4
:
3745 case DLT_IEEE802_15_4_LINUX
:
3746 case DLT_IEEE802_15_4_NONASK_PHY
:
3747 case DLT_IEEE802_15_4_NOFCS
:
3748 case DLT_IEEE802_15_4_TAP
:
3749 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3752 case DLT_IPMB_KONTRON
:
3756 /* Using the fixed-size NFLOG header it is possible to tell only
3757 * the address family of the packet, other meaningful data is
3758 * either missing or behind TLVs.
3760 break; // not implemented
3764 * Does this link-layer header type have a field
3765 * indicating the type of the next protocol? If
3766 * so, off_linktype.constant_part will be the offset of that
3767 * field in the packet; if not, it will be OFFSET_NOT_SET.
3769 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3771 * Yes; assume it's an Ethernet type. (If
3772 * it's not, it needs to be handled specially
3775 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3779 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
3780 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3784 * Check for an LLC SNAP packet with a given organization code and
3785 * protocol type; we check the entire contents of the 802.2 LLC and
3786 * snap headers, checking for DSAP and SSAP of SNAP and a control
3787 * field of 0x03 in the LLC header, and for the specified organization
3788 * code and protocol type in the SNAP header.
3790 static struct block
*
3791 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3793 u_char snapblock
[8];
3795 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3796 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3797 snapblock
[2] = 0x03; /* control = UI */
3798 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3799 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3800 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3801 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3802 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3803 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3807 * Generate code to match frames with an LLC header.
3809 static struct block
*
3810 gen_llc_internal(compiler_state_t
*cstate
)
3812 struct block
*b0
, *b1
;
3814 switch (cstate
->linktype
) {
3818 * We check for an Ethernet type field less or equal than
3819 * 1500, which means it's an 802.3 length field.
3821 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3824 * Now check for the purported DSAP and SSAP not being
3825 * 0xFF, to rule out NetWare-over-802.3.
3827 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3834 * We check for LLC traffic.
3836 b0
= gen_atmtype_llc(cstate
);
3839 case DLT_IEEE802
: /* Token Ring */
3841 * XXX - check for LLC frames.
3843 return gen_true(cstate
);
3847 * XXX - check for LLC frames.
3849 return gen_true(cstate
);
3851 case DLT_ATM_RFC1483
:
3853 * For LLC encapsulation, these are defined to have an
3856 * For VC encapsulation, they don't, but there's no
3857 * way to check for that; the protocol used on the VC
3858 * is negotiated out of band.
3860 return gen_true(cstate
);
3862 case DLT_IEEE802_11
:
3863 case DLT_PRISM_HEADER
:
3864 case DLT_IEEE802_11_RADIO
:
3865 case DLT_IEEE802_11_RADIO_AVS
:
3868 * Check that we have a data frame.
3870 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3871 IEEE80211_FC0_TYPE_DATA
,
3872 IEEE80211_FC0_TYPE_MASK
);
3875 fail_kw_on_dlt(cstate
, "llc");
3881 gen_llc(compiler_state_t
*cstate
)
3884 * Catch errors reported by us and routines below us, and return NULL
3887 if (setjmp(cstate
->top_ctx
))
3890 return gen_llc_internal(cstate
);
3894 gen_llc_i(compiler_state_t
*cstate
)
3896 struct block
*b0
, *b1
;
3900 * Catch errors reported by us and routines below us, and return NULL
3903 if (setjmp(cstate
->top_ctx
))
3907 * Check whether this is an LLC frame.
3909 b0
= gen_llc_internal(cstate
);
3912 * Load the control byte and test the low-order bit; it must
3913 * be clear for I frames.
3915 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3916 b1
= new_block(cstate
, JMP(BPF_JSET
));
3925 gen_llc_s(compiler_state_t
*cstate
)
3927 struct block
*b0
, *b1
;
3930 * Catch errors reported by us and routines below us, and return NULL
3933 if (setjmp(cstate
->top_ctx
))
3937 * Check whether this is an LLC frame.
3939 b0
= gen_llc_internal(cstate
);
3942 * Now compare the low-order 2 bit of the control byte against
3943 * the appropriate value for S frames.
3945 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3951 gen_llc_u(compiler_state_t
*cstate
)
3953 struct block
*b0
, *b1
;
3956 * Catch errors reported by us and routines below us, and return NULL
3959 if (setjmp(cstate
->top_ctx
))
3963 * Check whether this is an LLC frame.
3965 b0
= gen_llc_internal(cstate
);
3968 * Now compare the low-order 2 bit of the control byte against
3969 * the appropriate value for U frames.
3971 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3977 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3979 struct block
*b0
, *b1
;
3982 * Catch errors reported by us and routines below us, and return NULL
3985 if (setjmp(cstate
->top_ctx
))
3989 * Check whether this is an LLC frame.
3991 b0
= gen_llc_internal(cstate
);
3994 * Now check for an S frame with the appropriate type.
3996 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4002 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4004 struct block
*b0
, *b1
;
4007 * Catch errors reported by us and routines below us, and return NULL
4010 if (setjmp(cstate
->top_ctx
))
4014 * Check whether this is an LLC frame.
4016 b0
= gen_llc_internal(cstate
);
4019 * Now check for a U frame with the appropriate type.
4021 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4027 * Generate code to match a particular packet type, for link-layer types
4028 * using 802.2 LLC headers.
4030 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4031 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4033 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4034 * value, if <= ETHERMTU. We use that to determine whether to
4035 * match the DSAP or both DSAP and LSAP or to check the OUI and
4036 * protocol ID in a SNAP header.
4038 static struct block
*
4039 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4042 * XXX - handle token-ring variable-length header.
4048 case LLCSAP_NETBEUI
:
4050 * XXX - should we check both the DSAP and the
4051 * SSAP, like this, or should we check just the
4052 * DSAP, as we do for other SAP values?
4054 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4055 ((ll_proto
<< 8) | ll_proto
));
4059 * XXX - are there ever SNAP frames for IPX on
4060 * non-Ethernet 802.x networks?
4062 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4064 case ETHERTYPE_ATALK
:
4066 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4067 * SNAP packets with an organization code of
4068 * 0x080007 (Apple, for Appletalk) and a protocol
4069 * type of ETHERTYPE_ATALK (Appletalk).
4071 * XXX - check for an organization code of
4072 * encapsulated Ethernet as well?
4074 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4078 * XXX - we don't have to check for IPX 802.3
4079 * here, but should we check for the IPX Ethertype?
4081 if (ll_proto
<= ETHERMTU
) {
4083 * This is an LLC SAP value, so check
4086 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4089 * This is an Ethernet type; we assume that it's
4090 * unlikely that it'll appear in the right place
4091 * at random, and therefore check only the
4092 * location that would hold the Ethernet type
4093 * in a SNAP frame with an organization code of
4094 * 0x000000 (encapsulated Ethernet).
4096 * XXX - if we were to check for the SNAP DSAP and
4097 * LSAP, as per XXX, and were also to check for an
4098 * organization code of 0x000000 (encapsulated
4099 * Ethernet), we'd do
4101 * return gen_snap(cstate, 0x000000, ll_proto);
4103 * here; for now, we don't, as per the above.
4104 * I don't know whether it's worth the extra CPU
4105 * time to do the right check or not.
4107 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4112 static struct block
*
4113 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4114 int dir
, u_int src_off
, u_int dst_off
)
4116 struct block
*b0
, *b1
;
4130 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4131 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4137 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4138 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4143 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4147 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4151 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4155 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4159 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4163 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4170 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4174 static struct block
*
4175 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4176 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4178 struct block
*b0
, *b1
;
4181 * Code below needs to access four separate 32-bit parts of the 128-bit
4182 * IPv6 address and mask. In some OSes this is as simple as using the
4183 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4184 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4185 * far as libpcap sees it. Hence copy the data before use to avoid
4186 * potential unaligned memory access and the associated compiler
4187 * warnings (whether genuine or not).
4189 bpf_u_int32 a
[4], m
[4];
4202 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4203 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4209 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4210 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4215 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4219 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4223 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4227 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4231 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4235 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4242 /* this order is important */
4243 memcpy(a
, addr
, sizeof(a
));
4244 memcpy(m
, mask
, sizeof(m
));
4245 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4246 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4248 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4250 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4256 static struct block
*
4257 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4259 register struct block
*b0
, *b1
;
4263 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4266 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4269 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4270 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4276 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4277 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4282 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4286 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4290 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4294 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4298 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4302 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4310 * Like gen_ehostop, but for DLT_FDDI
4312 static struct block
*
4313 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4315 struct block
*b0
, *b1
;
4319 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4322 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4325 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4326 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4332 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4333 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4338 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4342 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4346 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4350 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4354 bpf_error(cstate
, "'ra' is only supported on 802.11");
4358 bpf_error(cstate
, "'ta' is only supported on 802.11");
4366 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4368 static struct block
*
4369 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4371 register struct block
*b0
, *b1
;
4375 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4378 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4381 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4382 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4388 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4389 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4394 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4398 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4402 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4406 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4410 bpf_error(cstate
, "'ra' is only supported on 802.11");
4414 bpf_error(cstate
, "'ta' is only supported on 802.11");
4422 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4423 * various 802.11 + radio headers.
4425 static struct block
*
4426 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4428 register struct block
*b0
, *b1
, *b2
;
4429 register struct slist
*s
;
4431 #ifdef ENABLE_WLAN_FILTERING_PATCH
4434 * We need to disable the optimizer because the optimizer is buggy
4435 * and wipes out some LD instructions generated by the below
4436 * code to validate the Frame Control bits
4438 cstate
->no_optimize
= 1;
4439 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4446 * For control frames, there is no SA.
4448 * For management frames, SA is at an
4449 * offset of 10 from the beginning of
4452 * For data frames, SA is at an offset
4453 * of 10 from the beginning of the packet
4454 * if From DS is clear, at an offset of
4455 * 16 from the beginning of the packet
4456 * if From DS is set and To DS is clear,
4457 * and an offset of 24 from the beginning
4458 * of the packet if From DS is set and To DS
4463 * Generate the tests to be done for data frames
4466 * First, check for To DS set, i.e. check "link[1] & 0x01".
4468 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4469 b1
= new_block(cstate
, JMP(BPF_JSET
));
4470 b1
->s
.k
= 0x01; /* To DS */
4474 * If To DS is set, the SA is at 24.
4476 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4480 * Now, check for To DS not set, i.e. check
4481 * "!(link[1] & 0x01)".
4483 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4484 b2
= new_block(cstate
, JMP(BPF_JSET
));
4485 b2
->s
.k
= 0x01; /* To DS */
4490 * If To DS is not set, the SA is at 16.
4492 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4496 * Now OR together the last two checks. That gives
4497 * the complete set of checks for data frames with
4503 * Now check for From DS being set, and AND that with
4504 * the ORed-together checks.
4506 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4507 b1
= new_block(cstate
, JMP(BPF_JSET
));
4508 b1
->s
.k
= 0x02; /* From DS */
4513 * Now check for data frames with From DS not set.
4515 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4516 b2
= new_block(cstate
, JMP(BPF_JSET
));
4517 b2
->s
.k
= 0x02; /* From DS */
4522 * If From DS isn't set, the SA is at 10.
4524 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4528 * Now OR together the checks for data frames with
4529 * From DS not set and for data frames with From DS
4530 * set; that gives the checks done for data frames.
4535 * Now check for a data frame.
4536 * I.e, check "link[0] & 0x08".
4538 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4539 b1
= new_block(cstate
, JMP(BPF_JSET
));
4544 * AND that with the checks done for data frames.
4549 * If the high-order bit of the type value is 0, this
4550 * is a management frame.
4551 * I.e, check "!(link[0] & 0x08)".
4553 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4554 b2
= new_block(cstate
, JMP(BPF_JSET
));
4560 * For management frames, the SA is at 10.
4562 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4566 * OR that with the checks done for data frames.
4567 * That gives the checks done for management and
4573 * If the low-order bit of the type value is 1,
4574 * this is either a control frame or a frame
4575 * with a reserved type, and thus not a
4578 * I.e., check "!(link[0] & 0x04)".
4580 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4581 b1
= new_block(cstate
, JMP(BPF_JSET
));
4587 * AND that with the checks for data and management
4597 * For control frames, there is no DA.
4599 * For management frames, DA is at an
4600 * offset of 4 from the beginning of
4603 * For data frames, DA is at an offset
4604 * of 4 from the beginning of the packet
4605 * if To DS is clear and at an offset of
4606 * 16 from the beginning of the packet
4611 * Generate the tests to be done for data frames.
4613 * First, check for To DS set, i.e. "link[1] & 0x01".
4615 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4616 b1
= new_block(cstate
, JMP(BPF_JSET
));
4617 b1
->s
.k
= 0x01; /* To DS */
4621 * If To DS is set, the DA is at 16.
4623 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4627 * Now, check for To DS not set, i.e. check
4628 * "!(link[1] & 0x01)".
4630 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4631 b2
= new_block(cstate
, JMP(BPF_JSET
));
4632 b2
->s
.k
= 0x01; /* To DS */
4637 * If To DS is not set, the DA is at 4.
4639 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4643 * Now OR together the last two checks. That gives
4644 * the complete set of checks for data frames.
4649 * Now check for a data frame.
4650 * I.e, check "link[0] & 0x08".
4652 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4653 b1
= new_block(cstate
, JMP(BPF_JSET
));
4658 * AND that with the checks done for data frames.
4663 * If the high-order bit of the type value is 0, this
4664 * is a management frame.
4665 * I.e, check "!(link[0] & 0x08)".
4667 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4668 b2
= new_block(cstate
, JMP(BPF_JSET
));
4674 * For management frames, the DA is at 4.
4676 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4680 * OR that with the checks done for data frames.
4681 * That gives the checks done for management and
4687 * If the low-order bit of the type value is 1,
4688 * this is either a control frame or a frame
4689 * with a reserved type, and thus not a
4692 * I.e., check "!(link[0] & 0x04)".
4694 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4695 b1
= new_block(cstate
, JMP(BPF_JSET
));
4701 * AND that with the checks for data and management
4708 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4709 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4715 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4716 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4721 * XXX - add BSSID keyword?
4724 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4728 * Not present in CTS or ACK control frames.
4730 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4731 IEEE80211_FC0_TYPE_MASK
);
4733 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4734 IEEE80211_FC0_SUBTYPE_MASK
);
4736 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4737 IEEE80211_FC0_SUBTYPE_MASK
);
4741 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4747 * Not present in control frames.
4749 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4750 IEEE80211_FC0_TYPE_MASK
);
4752 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4758 * Present only if the direction mask has both "From DS"
4759 * and "To DS" set. Neither control frames nor management
4760 * frames should have both of those set, so we don't
4761 * check the frame type.
4763 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4764 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4765 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4771 * Not present in management frames; addr1 in other
4776 * If the high-order bit of the type value is 0, this
4777 * is a management frame.
4778 * I.e, check "(link[0] & 0x08)".
4780 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4781 b1
= new_block(cstate
, JMP(BPF_JSET
));
4788 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4791 * AND that with the check of addr1.
4798 * Not present in management frames; addr2, if present,
4803 * Not present in CTS or ACK control frames.
4805 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4806 IEEE80211_FC0_TYPE_MASK
);
4808 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4809 IEEE80211_FC0_SUBTYPE_MASK
);
4811 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4812 IEEE80211_FC0_SUBTYPE_MASK
);
4818 * If the high-order bit of the type value is 0, this
4819 * is a management frame.
4820 * I.e, check "(link[0] & 0x08)".
4822 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4823 b1
= new_block(cstate
, JMP(BPF_JSET
));
4828 * AND that with the check for frames other than
4829 * CTS and ACK frames.
4836 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4845 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4846 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4847 * as the RFC states.)
4849 static struct block
*
4850 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4852 register struct block
*b0
, *b1
;
4856 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4859 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4862 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4863 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4869 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4870 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4875 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4879 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4883 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4887 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4891 bpf_error(cstate
, "'ra' is only supported on 802.11");
4895 bpf_error(cstate
, "'ta' is only supported on 802.11");
4903 * This is quite tricky because there may be pad bytes in front of the
4904 * DECNET header, and then there are two possible data packet formats that
4905 * carry both src and dst addresses, plus 5 packet types in a format that
4906 * carries only the src node, plus 2 types that use a different format and
4907 * also carry just the src node.
4911 * Instead of doing those all right, we just look for data packets with
4912 * 0 or 1 bytes of padding. If you want to look at other packets, that
4913 * will require a lot more hacking.
4915 * To add support for filtering on DECNET "areas" (network numbers)
4916 * one would want to add a "mask" argument to this routine. That would
4917 * make the filter even more inefficient, although one could be clever
4918 * and not generate masking instructions if the mask is 0xFFFF.
4920 static struct block
*
4921 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4923 struct block
*b0
, *b1
, *b2
, *tmp
;
4924 u_int offset_lh
; /* offset if long header is received */
4925 u_int offset_sh
; /* offset if short header is received */
4930 offset_sh
= 1; /* follows flags */
4931 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4935 offset_sh
= 3; /* follows flags, dstnode */
4936 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4940 /* Inefficient because we do our Calvinball dance twice */
4941 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4942 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4948 /* Inefficient because we do our Calvinball dance twice */
4949 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4950 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4955 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4959 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4963 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4967 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4971 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4975 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4983 * In a DECnet message inside an Ethernet frame the first two bytes
4984 * immediately after EtherType are the [litle-endian] DECnet message
4985 * length, which is irrelevant in this context.
4987 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
4988 * 8-bit bitmap of the optional padding before the packet route header.
4989 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
4990 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
4991 * means there aren't any PAD bytes after the bitmap, so the header
4992 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
4993 * is set to 0, thus the header begins at the third byte.
4995 * The header can be in several (as mentioned above) formats, all of
4996 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
4997 * (PF, "pad field") set to 0 regardless of any padding present before
4998 * the header. "Short header" means bits 0-2 of the bitmap encode the
4999 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5001 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5002 * values and the masks, this maps to the required single bytes of
5003 * the message correctly on both big-endian and little-endian hosts.
5004 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5005 * because the wire encoding is little-endian and BPF multiple-byte
5006 * loads are big-endian. When the destination address is near enough
5007 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5010 /* Check for pad = 1, long header case */
5011 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5012 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5013 BPF_H
, SWAPSHORT(addr
));
5015 /* Check for pad = 0, long header case */
5016 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5017 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5021 /* Check for pad = 1, short header case */
5023 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5024 0x81020000U
| SWAPSHORT(addr
),
5027 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5028 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5033 /* Check for pad = 0, short header case */
5035 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5036 0x02000000U
| SWAPSHORT(addr
) << 8,
5039 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5040 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5050 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5051 * test the bottom-of-stack bit, and then check the version number
5052 * field in the IP header.
5054 static struct block
*
5055 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5057 struct block
*b0
, *b1
;
5062 /* match the bottom-of-stack bit */
5063 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5064 /* match the IPv4 version number */
5065 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5069 case ETHERTYPE_IPV6
:
5070 /* match the bottom-of-stack bit */
5071 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5072 /* match the IPv4 version number */
5073 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5078 /* FIXME add other L3 proto IDs */
5079 bpf_error(cstate
, "unsupported protocol over mpls");
5084 static struct block
*
5085 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5086 int proto
, int dir
, int type
)
5088 struct block
*b0
, *b1
;
5089 const char *typestr
;
5099 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5101 * Only check for non-IPv4 addresses if we're not
5102 * checking MPLS-encapsulated packets.
5104 if (cstate
->label_stack_depth
== 0) {
5105 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5107 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5113 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5116 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5117 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5122 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5123 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5128 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5129 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5134 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5137 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5140 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5143 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5146 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5149 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5152 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5155 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5156 b1
= gen_dnhostop(cstate
, addr
, dir
);
5161 bpf_error(cstate
, "LAT host filtering not implemented");
5164 bpf_error(cstate
, "SCA host filtering not implemented");
5167 bpf_error(cstate
, "MOPRC host filtering not implemented");
5170 bpf_error(cstate
, "MOPDL host filtering not implemented");
5173 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5176 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5179 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5182 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5185 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5188 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5191 bpf_error(cstate
, "AARP host filtering not implemented");
5194 bpf_error(cstate
, "ISO host filtering not implemented");
5197 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5200 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5203 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5206 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5209 bpf_error(cstate
, "IPX host filtering not implemented");
5212 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5215 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5218 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5221 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5224 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5227 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5230 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5233 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5236 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5239 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5248 static struct block
*
5249 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5250 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5252 struct block
*b0
, *b1
;
5253 const char *typestr
;
5264 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5265 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5270 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5273 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5276 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5279 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5282 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5285 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5288 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5291 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5294 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5297 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5300 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5303 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5306 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5309 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5312 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5315 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5318 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5321 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5324 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5327 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5330 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5333 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5336 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5339 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5342 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5345 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5348 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5351 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5354 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5357 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5360 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5363 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5366 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5369 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5372 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5375 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5378 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5381 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5391 static struct block
*
5392 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5393 struct addrinfo
*alist
, int proto
, int dir
)
5395 struct block
*b0
, *b1
, *tmp
;
5396 struct addrinfo
*ai
;
5397 struct sockaddr_in
*sin
;
5400 bpf_error(cstate
, "direction applied to 'gateway'");
5407 switch (cstate
->linktype
) {
5409 case DLT_NETANALYZER
:
5410 case DLT_NETANALYZER_TRANSPARENT
:
5411 b1
= gen_prevlinkhdr_check(cstate
);
5412 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5417 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5420 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5422 case DLT_IEEE802_11
:
5423 case DLT_PRISM_HEADER
:
5424 case DLT_IEEE802_11_RADIO_AVS
:
5425 case DLT_IEEE802_11_RADIO
:
5427 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5429 case DLT_IP_OVER_FC
:
5430 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5434 * This is LLC-multiplexed traffic; if it were
5435 * LANE, cstate->linktype would have been set to
5441 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5444 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5446 * Does it have an address?
5448 if (ai
->ai_addr
!= NULL
) {
5450 * Yes. Is it an IPv4 address?
5452 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5454 * Generate an entry for it.
5456 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5457 tmp
= gen_host(cstate
,
5458 ntohl(sin
->sin_addr
.s_addr
),
5459 0xffffffff, proto
, Q_OR
, Q_HOST
);
5461 * Is it the *first* IPv4 address?
5465 * Yes, so start with it.
5470 * No, so OR it into the
5482 * No IPv4 addresses found.
5490 bpf_error(cstate
, "illegal modifier of 'gateway'");
5495 static struct block
*
5496 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5499 struct block
*b1
= NULL
;
5504 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5508 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5512 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5516 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5519 #ifndef IPPROTO_IGMP
5520 #define IPPROTO_IGMP 2
5524 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5527 #ifndef IPPROTO_IGRP
5528 #define IPPROTO_IGRP 9
5531 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5535 #define IPPROTO_PIM 103
5539 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5542 #ifndef IPPROTO_VRRP
5543 #define IPPROTO_VRRP 112
5547 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5550 #ifndef IPPROTO_CARP
5551 #define IPPROTO_CARP 112
5555 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5559 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5563 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5567 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5571 break; // invalid syntax
5574 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5578 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5582 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5586 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5590 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5594 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5598 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5602 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5605 #ifndef IPPROTO_ICMPV6
5606 #define IPPROTO_ICMPV6 58
5609 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5613 #define IPPROTO_AH 51
5616 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5620 #define IPPROTO_ESP 50
5623 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5627 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5631 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5635 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5638 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5639 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5640 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5642 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5644 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5646 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5650 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5651 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5652 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5654 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5656 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5658 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5662 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5663 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5664 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5666 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5671 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5672 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5677 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5678 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5680 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5682 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5687 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5688 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5693 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5694 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5699 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5703 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5707 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5711 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5715 break; // invalid syntax
5722 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5726 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5729 * Catch errors reported by us and routines below us, and return NULL
5732 if (setjmp(cstate
->top_ctx
))
5735 return gen_proto_abbrev_internal(cstate
, proto
);
5738 static struct block
*
5739 gen_ipfrag(compiler_state_t
*cstate
)
5744 /* not IPv4 frag other than the first frag */
5745 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5746 b
= new_block(cstate
, JMP(BPF_JSET
));
5755 * Generate a comparison to a port value in the transport-layer header
5756 * at the specified offset from the beginning of that header.
5758 * XXX - this handles a variable-length prefix preceding the link-layer
5759 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5760 * variable-length link-layer headers (such as Token Ring or 802.11
5763 static struct block
*
5764 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5766 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5769 static struct block
*
5770 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5772 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5775 static struct block
*
5776 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5778 struct block
*b0
, *b1
, *tmp
;
5780 /* ip proto 'proto' and not a fragment other than the first fragment */
5781 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5782 b0
= gen_ipfrag(cstate
);
5787 b1
= gen_portatom(cstate
, 0, port
);
5791 b1
= gen_portatom(cstate
, 2, port
);
5795 tmp
= gen_portatom(cstate
, 0, port
);
5796 b1
= gen_portatom(cstate
, 2, port
);
5802 tmp
= gen_portatom(cstate
, 0, port
);
5803 b1
= gen_portatom(cstate
, 2, port
);
5808 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5812 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5816 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5820 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5824 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5828 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5840 static struct block
*
5841 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5843 struct block
*b0
, *b1
, *tmp
;
5848 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5849 * not LLC encapsulation with LLCSAP_IP.
5851 * For IEEE 802 networks - which includes 802.5 token ring
5852 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5853 * says that SNAP encapsulation is used, not LLC encapsulation
5856 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5857 * RFC 2225 say that SNAP encapsulation is used, not LLC
5858 * encapsulation with LLCSAP_IP.
5860 * So we always check for ETHERTYPE_IP.
5862 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5868 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5872 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5873 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5875 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5887 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5889 struct block
*b0
, *b1
, *tmp
;
5891 /* ip6 proto 'proto' */
5892 /* XXX - catch the first fragment of a fragmented packet? */
5893 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5897 b1
= gen_portatom6(cstate
, 0, port
);
5901 b1
= gen_portatom6(cstate
, 2, port
);
5905 tmp
= gen_portatom6(cstate
, 0, port
);
5906 b1
= gen_portatom6(cstate
, 2, port
);
5912 tmp
= gen_portatom6(cstate
, 0, port
);
5913 b1
= gen_portatom6(cstate
, 2, port
);
5925 static struct block
*
5926 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5928 struct block
*b0
, *b1
, *tmp
;
5930 /* link proto ip6 */
5931 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5937 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5941 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5942 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5944 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5955 /* gen_portrange code */
5956 static struct block
*
5957 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5960 struct block
*b1
, *b2
;
5964 * Reverse the order of the ports, so v1 is the lower one.
5973 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5974 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5981 static struct block
*
5982 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5983 bpf_u_int32 proto
, int dir
)
5985 struct block
*b0
, *b1
, *tmp
;
5987 /* ip proto 'proto' and not a fragment other than the first fragment */
5988 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5989 b0
= gen_ipfrag(cstate
);
5994 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5998 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6002 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6003 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6009 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
6010 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
6015 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6019 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6023 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6027 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6031 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
6035 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
6047 static struct block
*
6048 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6051 struct block
*b0
, *b1
, *tmp
;
6054 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6060 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6065 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6066 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6068 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6079 static struct block
*
6080 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6083 struct block
*b1
, *b2
;
6087 * Reverse the order of the ports, so v1 is the lower one.
6096 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6097 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6104 static struct block
*
6105 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6106 bpf_u_int32 proto
, int dir
)
6108 struct block
*b0
, *b1
, *tmp
;
6110 /* ip6 proto 'proto' */
6111 /* XXX - catch the first fragment of a fragmented packet? */
6112 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6116 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6120 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6124 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6125 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6131 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6132 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6144 static struct block
*
6145 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6148 struct block
*b0
, *b1
, *tmp
;
6150 /* link proto ip6 */
6151 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6157 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6162 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6163 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6165 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6177 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6186 v
= pcap_nametoproto(name
);
6187 if (v
== PROTO_UNDEF
)
6188 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6192 /* XXX should look up h/w protocol type based on cstate->linktype */
6193 v
= pcap_nametoeproto(name
);
6194 if (v
== PROTO_UNDEF
) {
6195 v
= pcap_nametollc(name
);
6196 if (v
== PROTO_UNDEF
)
6197 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6202 if (strcmp(name
, "esis") == 0)
6204 else if (strcmp(name
, "isis") == 0)
6206 else if (strcmp(name
, "clnp") == 0)
6209 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6219 #if !defined(NO_PROTOCHAIN)
6220 static struct block
*
6221 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6223 struct block
*b0
, *b
;
6224 struct slist
*s
[100];
6225 int fix2
, fix3
, fix4
, fix5
;
6226 int ahcheck
, again
, end
;
6228 int reg2
= alloc_reg(cstate
);
6230 memset(s
, 0, sizeof(s
));
6231 fix3
= fix4
= fix5
= 0;
6238 b0
= gen_protochain(cstate
, v
, Q_IP
);
6239 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6243 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6248 * We don't handle variable-length prefixes before the link-layer
6249 * header, or variable-length link-layer headers, here yet.
6250 * We might want to add BPF instructions to do the protochain
6251 * work, to simplify that and, on platforms that have a BPF
6252 * interpreter with the new instructions, let the filtering
6253 * be done in the kernel. (We already require a modified BPF
6254 * engine to do the protochain stuff, to support backward
6255 * branches, and backward branch support is unlikely to appear
6256 * in kernel BPF engines.)
6258 if (cstate
->off_linkpl
.is_variable
)
6259 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6262 * To quote a comment in optimize.c:
6264 * "These data structures are used in a Cocke and Schwartz style
6265 * value numbering scheme. Since the flowgraph is acyclic,
6266 * exit values can be propagated from a node's predecessors
6267 * provided it is uniquely defined."
6269 * "Acyclic" means "no backward branches", which means "no
6270 * loops", so we have to turn the optimizer off.
6272 cstate
->no_optimize
= 1;
6275 * s[0] is a dummy entry to protect other BPF insn from damage
6276 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6277 * hard to find interdependency made by jump table fixup.
6280 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6285 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6288 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6289 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6291 /* X = ip->ip_hl << 2 */
6292 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6293 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6298 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6300 /* A = ip6->ip_nxt */
6301 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6302 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6304 /* X = sizeof(struct ip6_hdr) */
6305 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6311 bpf_error(cstate
, "unsupported proto to gen_protochain");
6315 /* again: if (A == v) goto end; else fall through; */
6317 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6319 s
[i
]->s
.jt
= NULL
; /*later*/
6320 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6324 #ifndef IPPROTO_NONE
6325 #define IPPROTO_NONE 59
6327 /* if (A == IPPROTO_NONE) goto end */
6328 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6329 s
[i
]->s
.jt
= NULL
; /*later*/
6330 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6331 s
[i
]->s
.k
= IPPROTO_NONE
;
6332 s
[fix5
]->s
.jf
= s
[i
];
6336 if (proto
== Q_IPV6
) {
6337 int v6start
, v6end
, v6advance
, j
;
6340 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6341 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6342 s
[i
]->s
.jt
= NULL
; /*later*/
6343 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6344 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6345 s
[fix2
]->s
.jf
= s
[i
];
6347 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6348 s
[i
- 1]->s
.jf
= 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_DSTOPTS
;
6353 /* if (A == IPPROTO_ROUTING) goto v6advance */
6354 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6355 s
[i
]->s
.jt
= NULL
; /*later*/
6356 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6357 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6359 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6360 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6361 s
[i
]->s
.jt
= NULL
; /*later*/
6362 s
[i
]->s
.jf
= NULL
; /*later*/
6363 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6373 * A = P[X + packet head];
6374 * X = X + (P[X + packet head + 1] + 1) * 8;
6376 /* A = P[X + packet head] */
6377 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6378 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6381 s
[i
] = new_stmt(cstate
, BPF_ST
);
6384 /* A = P[X + packet head + 1]; */
6385 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6386 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6389 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6393 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6397 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6401 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6404 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6408 /* goto again; (must use BPF_JA for backward jump) */
6409 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6410 s
[i
]->s
.k
= again
- i
- 1;
6411 s
[i
- 1]->s
.jf
= s
[i
];
6415 for (j
= v6start
; j
<= v6end
; j
++)
6416 s
[j
]->s
.jt
= s
[v6advance
];
6419 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6421 s
[fix2
]->s
.jf
= s
[i
];
6427 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6428 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6429 s
[i
]->s
.jt
= NULL
; /*later*/
6430 s
[i
]->s
.jf
= NULL
; /*later*/
6431 s
[i
]->s
.k
= IPPROTO_AH
;
6433 s
[fix3
]->s
.jf
= s
[ahcheck
];
6440 * X = X + (P[X + 1] + 2) * 4;
6443 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6445 /* A = P[X + packet head]; */
6446 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6447 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6450 s
[i
] = new_stmt(cstate
, BPF_ST
);
6454 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6457 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6461 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6463 /* A = P[X + packet head] */
6464 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6465 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6468 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6472 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6476 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6479 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6483 /* goto again; (must use BPF_JA for backward jump) */
6484 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6485 s
[i
]->s
.k
= again
- i
- 1;
6490 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6492 s
[fix2
]->s
.jt
= s
[end
];
6493 s
[fix4
]->s
.jf
= s
[end
];
6494 s
[fix5
]->s
.jt
= s
[end
];
6501 for (i
= 0; i
< max
- 1; i
++)
6502 s
[i
]->next
= s
[i
+ 1];
6503 s
[max
- 1]->next
= NULL
;
6508 b
= new_block(cstate
, JMP(BPF_JEQ
));
6509 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6512 free_reg(cstate
, reg2
);
6517 #endif /* !defined(NO_PROTOCHAIN) */
6520 * Generate code that checks whether the packet is a packet for protocol
6521 * <proto> and whether the type field in that protocol's header has
6522 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6523 * IP packet and checks the protocol number in the IP header against <v>.
6525 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6526 * against Q_IP and Q_IPV6.
6528 static struct block
*
6529 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6531 struct block
*b0
, *b1
;
6534 if (dir
!= Q_DEFAULT
)
6535 bpf_error(cstate
, "direction applied to 'proto'");
6539 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6540 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6545 return gen_linktype(cstate
, v
);
6549 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6550 * not LLC encapsulation with LLCSAP_IP.
6552 * For IEEE 802 networks - which includes 802.5 token ring
6553 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6554 * says that SNAP encapsulation is used, not LLC encapsulation
6557 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6558 * RFC 2225 say that SNAP encapsulation is used, not LLC
6559 * encapsulation with LLCSAP_IP.
6561 * So we always check for ETHERTYPE_IP.
6563 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6564 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6569 bpf_error(cstate
, "arp does not encapsulate another protocol");
6573 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6577 bpf_error(cstate
, "'sctp proto' is bogus");
6581 bpf_error(cstate
, "'tcp proto' is bogus");
6585 bpf_error(cstate
, "'udp proto' is bogus");
6589 bpf_error(cstate
, "'icmp proto' is bogus");
6593 bpf_error(cstate
, "'igmp proto' is bogus");
6597 bpf_error(cstate
, "'igrp proto' is bogus");
6601 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6605 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6609 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6613 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6617 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6621 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6625 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6627 * Also check for a fragment header before the final
6630 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6631 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6633 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6639 bpf_error(cstate
, "'icmp6 proto' is bogus");
6643 bpf_error(cstate
, "'ah proto' is bogus");
6647 bpf_error(cstate
, "'esp proto' is bogus");
6651 bpf_error(cstate
, "'pim proto' is bogus");
6655 bpf_error(cstate
, "'vrrp proto' is bogus");
6659 bpf_error(cstate
, "'aarp proto' is bogus");
6663 switch (cstate
->linktype
) {
6667 * Frame Relay packets typically have an OSI
6668 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6669 * generates code to check for all the OSI
6670 * NLPIDs, so calling it and then adding a check
6671 * for the particular NLPID for which we're
6672 * looking is bogus, as we can just check for
6675 * What we check for is the NLPID and a frame
6676 * control field value of UI, i.e. 0x03 followed
6679 * XXX - assumes a 2-byte Frame Relay header with
6680 * DLCI and flags. What if the address is longer?
6682 * XXX - what about SNAP-encapsulated frames?
6684 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6690 * Cisco uses an Ethertype lookalike - for OSI,
6693 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6694 /* OSI in C-HDLC is stuffed with a fudge byte */
6695 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6700 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6701 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6707 bpf_error(cstate
, "'esis proto' is bogus");
6711 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6713 * 4 is the offset of the PDU type relative to the IS-IS
6716 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6721 bpf_error(cstate
, "'clnp proto' is not supported");
6725 bpf_error(cstate
, "'stp proto' is bogus");
6729 bpf_error(cstate
, "'ipx proto' is bogus");
6733 bpf_error(cstate
, "'netbeui proto' is bogus");
6737 bpf_error(cstate
, "'l1 proto' is bogus");
6741 bpf_error(cstate
, "'l2 proto' is bogus");
6745 bpf_error(cstate
, "'iih proto' is bogus");
6749 bpf_error(cstate
, "'snp proto' is bogus");
6753 bpf_error(cstate
, "'csnp proto' is bogus");
6757 bpf_error(cstate
, "'psnp proto' is bogus");
6761 bpf_error(cstate
, "'lsp proto' is bogus");
6765 bpf_error(cstate
, "'radio proto' is bogus");
6769 bpf_error(cstate
, "'carp proto' is bogus");
6780 * Convert a non-numeric name to a port number.
6783 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6785 struct addrinfo hints
, *res
, *ai
;
6787 struct sockaddr_in
*in4
;
6789 struct sockaddr_in6
*in6
;
6794 * We check for both TCP and UDP in case there are
6795 * ambiguous entries.
6797 memset(&hints
, 0, sizeof(hints
));
6798 hints
.ai_family
= PF_UNSPEC
;
6799 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6800 hints
.ai_protocol
= ipproto
;
6801 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6808 * No such port. Just return -1.
6815 * We don't use strerror() because it's not
6816 * guaranteed to be thread-safe on all platforms
6817 * (probably because it might use a non-thread-local
6818 * buffer into which to format an error message
6819 * if the error code isn't one for which it has
6820 * a canned string; three cheers for C string
6823 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6825 port
= -2; /* a real error */
6831 * This is a real error, not just "there's
6832 * no such service name".
6834 * We don't use gai_strerror() because it's not
6835 * guaranteed to be thread-safe on all platforms
6836 * (probably because it might use a non-thread-local
6837 * buffer into which to format an error message
6838 * if the error code isn't one for which it has
6839 * a canned string; three cheers for C string
6842 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6844 port
= -2; /* a real error */
6849 * OK, we found it. Did it find anything?
6851 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6853 * Does it have an address?
6855 if (ai
->ai_addr
!= NULL
) {
6857 * Yes. Get a port number; we're done.
6859 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6860 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6861 port
= ntohs(in4
->sin_port
);
6865 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6866 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6867 port
= ntohs(in6
->sin6_port
);
6879 * Convert a string to a port number.
6882 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6892 * See if it's a number.
6894 ret
= stoulen(string
, string_size
, &val
, cstate
);
6898 /* Unknown port type - it's just a number. */
6899 *proto
= PROTO_UNDEF
;
6902 case STOULEN_NOT_OCTAL_NUMBER
:
6903 case STOULEN_NOT_HEX_NUMBER
:
6904 case STOULEN_NOT_DECIMAL_NUMBER
:
6906 * Not a valid number; try looking it up as a port.
6908 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6909 memcpy(cpy
, string
, string_size
);
6910 cpy
[string_size
] = '\0';
6911 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6912 if (tcp_port
== -2) {
6914 * We got a hard error; the error string has
6918 longjmp(cstate
->top_ctx
, 1);
6921 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6922 if (udp_port
== -2) {
6924 * We got a hard error; the error string has
6928 longjmp(cstate
->top_ctx
, 1);
6933 * We need to check /etc/services for ambiguous entries.
6934 * If we find an ambiguous entry, and it has the
6935 * same port number, change the proto to PROTO_UNDEF
6936 * so both TCP and UDP will be checked.
6938 if (tcp_port
>= 0) {
6939 val
= (bpf_u_int32
)tcp_port
;
6940 *proto
= IPPROTO_TCP
;
6941 if (udp_port
>= 0) {
6942 if (udp_port
== tcp_port
)
6943 *proto
= PROTO_UNDEF
;
6946 /* Can't handle ambiguous names that refer
6947 to different port numbers. */
6948 warning("ambiguous port %s in /etc/services",
6955 if (udp_port
>= 0) {
6956 val
= (bpf_u_int32
)udp_port
;
6957 *proto
= IPPROTO_UDP
;
6961 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6963 longjmp(cstate
->top_ctx
, 1);
6970 /* Error already set. */
6971 longjmp(cstate
->top_ctx
, 1);
6978 /* Should not happen */
6979 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6980 longjmp(cstate
->top_ctx
, 1);
6987 * Convert a string in the form PPP-PPP, which correspond to ports, to
6988 * a starting and ending port in a port range.
6991 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6992 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6995 const char *first
, *second
;
6996 size_t first_size
, second_size
;
6999 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
7000 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
7003 * Make sure there are no other hyphens.
7005 * XXX - we support named ports, but there are some port names
7006 * in /etc/services that include hyphens, so this would rule
7009 if (strchr(hyphen_off
+ 1, '-') != NULL
)
7010 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
7014 * Get the length of the first port.
7017 first_size
= hyphen_off
- string
;
7018 if (first_size
== 0) {
7019 /* Range of "-port", which we don't support. */
7020 bpf_error(cstate
, "port range '%s' has no starting port", string
);
7024 * Try to convert it to a port.
7026 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
7027 save_proto
= *proto
;
7030 * Get the length of the second port.
7032 second
= hyphen_off
+ 1;
7033 second_size
= strlen(second
);
7034 if (second_size
== 0) {
7035 /* Range of "port-", which we don't support. */
7036 bpf_error(cstate
, "port range '%s' has no ending port", string
);
7040 * Try to convert it to a port.
7042 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
7043 if (*proto
!= save_proto
)
7044 *proto
= PROTO_UNDEF
;
7048 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
7050 int proto
= q
.proto
;
7054 bpf_u_int32 mask
, addr
;
7055 struct addrinfo
*res
, *res0
;
7056 struct sockaddr_in
*sin4
;
7059 struct sockaddr_in6
*sin6
;
7060 struct in6_addr mask128
;
7062 struct block
*b
, *tmp
;
7063 int port
, real_proto
;
7064 bpf_u_int32 port1
, port2
;
7067 * Catch errors reported by us and routines below us, and return NULL
7070 if (setjmp(cstate
->top_ctx
))
7076 addr
= pcap_nametonetaddr(name
);
7078 bpf_error(cstate
, "unknown network '%s'", name
);
7079 /* Left justify network addr and calculate its network mask */
7081 while (addr
&& (addr
& 0xff000000) == 0) {
7085 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7089 if (proto
== Q_LINK
) {
7090 switch (cstate
->linktype
) {
7093 case DLT_NETANALYZER
:
7094 case DLT_NETANALYZER_TRANSPARENT
:
7095 eaddr
= pcap_ether_hostton(name
);
7098 "unknown ether host '%s'", name
);
7099 tmp
= gen_prevlinkhdr_check(cstate
);
7100 b
= gen_ehostop(cstate
, eaddr
, dir
);
7107 eaddr
= pcap_ether_hostton(name
);
7110 "unknown FDDI host '%s'", name
);
7111 b
= gen_fhostop(cstate
, eaddr
, dir
);
7116 eaddr
= pcap_ether_hostton(name
);
7119 "unknown token ring host '%s'", name
);
7120 b
= gen_thostop(cstate
, eaddr
, dir
);
7124 case DLT_IEEE802_11
:
7125 case DLT_PRISM_HEADER
:
7126 case DLT_IEEE802_11_RADIO_AVS
:
7127 case DLT_IEEE802_11_RADIO
:
7129 eaddr
= pcap_ether_hostton(name
);
7132 "unknown 802.11 host '%s'", name
);
7133 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7137 case DLT_IP_OVER_FC
:
7138 eaddr
= pcap_ether_hostton(name
);
7141 "unknown Fibre Channel host '%s'", name
);
7142 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7147 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7148 } else if (proto
== Q_DECNET
) {
7150 * A long time ago on Ultrix libpcap supported
7151 * translation of DECnet host names into DECnet
7152 * addresses, but this feature is history now.
7154 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7157 memset(&mask128
, 0xff, sizeof(mask128
));
7159 res0
= res
= pcap_nametoaddrinfo(name
);
7161 bpf_error(cstate
, "unknown host '%s'", name
);
7168 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7169 tproto
== Q_DEFAULT
) {
7175 for (res
= res0
; res
; res
= res
->ai_next
) {
7176 switch (res
->ai_family
) {
7179 if (tproto
== Q_IPV6
)
7183 sin4
= (struct sockaddr_in
*)
7185 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7186 0xffffffff, tproto
, dir
, q
.addr
);
7190 if (tproto6
== Q_IP
)
7193 sin6
= (struct sockaddr_in6
*)
7195 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7196 &mask128
, tproto6
, dir
, q
.addr
);
7209 bpf_error(cstate
, "unknown host '%s'%s", name
,
7210 (proto
== Q_DEFAULT
)
7212 : " for specified address family");
7218 if (proto
!= Q_DEFAULT
&&
7219 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7220 bpf_error(cstate
, "illegal qualifier of 'port'");
7221 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7222 bpf_error(cstate
, "unknown port '%s'", name
);
7223 if (proto
== Q_UDP
) {
7224 if (real_proto
== IPPROTO_TCP
)
7225 bpf_error(cstate
, "port '%s' is tcp", name
);
7226 else if (real_proto
== IPPROTO_SCTP
)
7227 bpf_error(cstate
, "port '%s' is sctp", name
);
7229 /* override PROTO_UNDEF */
7230 real_proto
= IPPROTO_UDP
;
7232 if (proto
== Q_TCP
) {
7233 if (real_proto
== IPPROTO_UDP
)
7234 bpf_error(cstate
, "port '%s' is udp", name
);
7236 else if (real_proto
== IPPROTO_SCTP
)
7237 bpf_error(cstate
, "port '%s' is sctp", name
);
7239 /* override PROTO_UNDEF */
7240 real_proto
= IPPROTO_TCP
;
7242 if (proto
== Q_SCTP
) {
7243 if (real_proto
== IPPROTO_UDP
)
7244 bpf_error(cstate
, "port '%s' is udp", name
);
7246 else if (real_proto
== IPPROTO_TCP
)
7247 bpf_error(cstate
, "port '%s' is tcp", name
);
7249 /* override PROTO_UNDEF */
7250 real_proto
= IPPROTO_SCTP
;
7253 bpf_error(cstate
, "illegal port number %d < 0", port
);
7255 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7256 b
= gen_port(cstate
, port
, real_proto
, dir
);
7257 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7261 if (proto
!= Q_DEFAULT
&&
7262 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7263 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7264 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7265 if (proto
== Q_UDP
) {
7266 if (real_proto
== IPPROTO_TCP
)
7267 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7268 else if (real_proto
== IPPROTO_SCTP
)
7269 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7271 /* override PROTO_UNDEF */
7272 real_proto
= IPPROTO_UDP
;
7274 if (proto
== Q_TCP
) {
7275 if (real_proto
== IPPROTO_UDP
)
7276 bpf_error(cstate
, "port in range '%s' is udp", name
);
7277 else if (real_proto
== IPPROTO_SCTP
)
7278 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7280 /* override PROTO_UNDEF */
7281 real_proto
= IPPROTO_TCP
;
7283 if (proto
== Q_SCTP
) {
7284 if (real_proto
== IPPROTO_UDP
)
7285 bpf_error(cstate
, "port in range '%s' is udp", name
);
7286 else if (real_proto
== IPPROTO_TCP
)
7287 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7289 /* override PROTO_UNDEF */
7290 real_proto
= IPPROTO_SCTP
;
7293 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7295 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7297 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7298 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7303 eaddr
= pcap_ether_hostton(name
);
7305 bpf_error(cstate
, "unknown ether host: %s", name
);
7307 res
= pcap_nametoaddrinfo(name
);
7310 bpf_error(cstate
, "unknown host '%s'", name
);
7311 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7316 bpf_error(cstate
, "unknown host '%s'", name
);
7319 bpf_error(cstate
, "'gateway' not supported in this configuration");
7323 real_proto
= lookup_proto(cstate
, name
, proto
);
7324 if (real_proto
>= 0)
7325 return gen_proto(cstate
, real_proto
, proto
, dir
);
7327 bpf_error(cstate
, "unknown protocol: %s", name
);
7329 #if !defined(NO_PROTOCHAIN)
7331 real_proto
= lookup_proto(cstate
, name
, proto
);
7332 if (real_proto
>= 0)
7333 return gen_protochain(cstate
, real_proto
, proto
);
7335 bpf_error(cstate
, "unknown protocol: %s", name
);
7336 #endif /* !defined(NO_PROTOCHAIN) */
7347 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7348 bpf_u_int32 masklen
, struct qual q
)
7350 register int nlen
, mlen
;
7355 * Catch errors reported by us and routines below us, and return NULL
7358 if (setjmp(cstate
->top_ctx
))
7361 nlen
= pcapint_atoin(s1
, &n
);
7363 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7364 /* Promote short ipaddr */
7368 mlen
= pcapint_atoin(s2
, &m
);
7370 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7371 /* Promote short ipaddr */
7374 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7377 /* Convert mask len to mask */
7379 bpf_error(cstate
, "mask length must be <= 32");
7380 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7381 m
= (bpf_u_int32
)m64
;
7383 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7390 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7393 bpf_error(cstate
, "Mask syntax for networks only");
7400 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7408 * Catch errors reported by us and routines below us, and return NULL
7411 if (setjmp(cstate
->top_ctx
))
7418 * v contains a 32-bit unsigned parsed from a string of the
7419 * form {N}, which could be decimal, hexadecimal or octal.
7420 * Although it would be possible to use the value as a raw
7421 * 16-bit DECnet address when the value fits into 16 bits, this
7422 * would be a questionable feature: DECnet address wire
7423 * encoding is little-endian, so this would not work as
7424 * intuitively as the same works for [big-endian] IPv4
7425 * addresses (0x01020304 means 1.2.3.4).
7427 if (proto
== Q_DECNET
)
7428 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7430 } else if (proto
== Q_DECNET
) {
7432 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7433 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7434 * for a valid DECnet address.
7436 vlen
= pcapint_atodn(s
, &v
);
7438 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7441 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7442 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7445 vlen
= pcapint_atoin(s
, &v
);
7447 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7455 if (proto
== Q_DECNET
)
7456 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7457 else if (proto
== Q_LINK
) {
7458 bpf_error(cstate
, "illegal link layer address");
7461 if (s
== NULL
&& q
.addr
== Q_NET
) {
7462 /* Promote short net number */
7463 while (v
&& (v
& 0xff000000) == 0) {
7468 /* Promote short ipaddr */
7470 mask
<<= 32 - vlen
;
7472 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7477 proto
= IPPROTO_UDP
;
7478 else if (proto
== Q_TCP
)
7479 proto
= IPPROTO_TCP
;
7480 else if (proto
== Q_SCTP
)
7481 proto
= IPPROTO_SCTP
;
7482 else if (proto
== Q_DEFAULT
)
7483 proto
= PROTO_UNDEF
;
7485 bpf_error(cstate
, "illegal qualifier of 'port'");
7488 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7492 b
= gen_port(cstate
, v
, proto
, dir
);
7493 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7499 proto
= IPPROTO_UDP
;
7500 else if (proto
== Q_TCP
)
7501 proto
= IPPROTO_TCP
;
7502 else if (proto
== Q_SCTP
)
7503 proto
= IPPROTO_SCTP
;
7504 else if (proto
== Q_DEFAULT
)
7505 proto
= PROTO_UNDEF
;
7507 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7510 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7514 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7515 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7520 bpf_error(cstate
, "'gateway' requires a name");
7524 return gen_proto(cstate
, v
, proto
, dir
);
7526 #if !defined(NO_PROTOCHAIN)
7528 return gen_protochain(cstate
, v
, proto
);
7544 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7547 struct addrinfo
*res
;
7548 struct in6_addr
*addr
;
7549 struct in6_addr mask
;
7551 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7554 * Catch errors reported by us and routines below us, and return NULL
7557 if (setjmp(cstate
->top_ctx
))
7560 res
= pcap_nametoaddrinfo(s
);
7562 bpf_error(cstate
, "invalid ip6 address %s", s
);
7565 bpf_error(cstate
, "%s resolved to multiple address", s
);
7566 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7568 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7569 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7570 memset(&mask
, 0, sizeof(mask
));
7571 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7573 mask
.s6_addr
[masklen
/ 8] =
7574 (0xff << (8 - masklen
% 8)) & 0xff;
7577 memcpy(a
, addr
, sizeof(a
));
7578 memcpy(m
, &mask
, sizeof(m
));
7579 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7580 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7581 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7589 bpf_error(cstate
, "Mask syntax for networks only");
7593 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7599 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7606 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7608 struct block
*b
, *tmp
;
7611 * Catch errors reported by us and routines below us, and return NULL
7614 if (setjmp(cstate
->top_ctx
))
7617 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7618 cstate
->e
= pcap_ether_aton(s
);
7619 if (cstate
->e
== NULL
)
7620 bpf_error(cstate
, "malloc");
7621 switch (cstate
->linktype
) {
7623 case DLT_NETANALYZER
:
7624 case DLT_NETANALYZER_TRANSPARENT
:
7625 tmp
= gen_prevlinkhdr_check(cstate
);
7626 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7631 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7634 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7636 case DLT_IEEE802_11
:
7637 case DLT_PRISM_HEADER
:
7638 case DLT_IEEE802_11_RADIO_AVS
:
7639 case DLT_IEEE802_11_RADIO
:
7641 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7643 case DLT_IP_OVER_FC
:
7644 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7649 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7656 bpf_error(cstate
, "ethernet address used in non-ether expression");
7661 sappend(struct slist
*s0
, struct slist
*s1
)
7664 * This is definitely not the best way to do this, but the
7665 * lists will rarely get long.
7672 static struct slist
*
7673 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7677 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7682 static struct slist
*
7683 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7687 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7693 * Modify "index" to use the value stored into its register as an
7694 * offset relative to the beginning of the header for the protocol
7695 * "proto", and allocate a register and put an item "size" bytes long
7696 * (1, 2, or 4) at that offset into that register, making it the register
7699 static struct arth
*
7700 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7704 struct slist
*s
, *tmp
;
7706 int regno
= alloc_reg(cstate
);
7708 free_reg(cstate
, inst
->regno
);
7712 bpf_error(cstate
, "data size must be 1, 2, or 4");
7729 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7733 * The offset is relative to the beginning of the packet
7734 * data, if we have a radio header. (If we don't, this
7737 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7738 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7739 cstate
->linktype
!= DLT_PRISM_HEADER
)
7740 bpf_error(cstate
, "radio information not present in capture");
7743 * Load into the X register the offset computed into the
7744 * register specified by "index".
7746 s
= xfer_to_x(cstate
, inst
);
7749 * Load the item at that offset.
7751 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7753 sappend(inst
->s
, s
);
7758 * The offset is relative to the beginning of
7759 * the link-layer header.
7761 * XXX - what about ATM LANE? Should the index be
7762 * relative to the beginning of the AAL5 frame, so
7763 * that 0 refers to the beginning of the LE Control
7764 * field, or relative to the beginning of the LAN
7765 * frame, so that 0 refers, for Ethernet LANE, to
7766 * the beginning of the destination address?
7768 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7771 * If "s" is non-null, it has code to arrange that the
7772 * X register contains the length of the prefix preceding
7773 * the link-layer header. Add to it the offset computed
7774 * into the register specified by "index", and move that
7775 * into the X register. Otherwise, just load into the X
7776 * register the offset computed into the register specified
7780 sappend(s
, xfer_to_a(cstate
, inst
));
7781 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7782 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7784 s
= xfer_to_x(cstate
, inst
);
7787 * Load the item at the sum of the offset we've put in the
7788 * X register and the offset of the start of the link
7789 * layer header (which is 0 if the radio header is
7790 * variable-length; that header length is what we put
7791 * into the X register and then added to the index).
7793 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7794 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7796 sappend(inst
->s
, s
);
7810 * The offset is relative to the beginning of
7811 * the network-layer header.
7812 * XXX - are there any cases where we want
7813 * cstate->off_nl_nosnap?
7815 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7818 * If "s" is non-null, it has code to arrange that the
7819 * X register contains the variable part of the offset
7820 * of the link-layer payload. Add to it the offset
7821 * computed into the register specified by "index",
7822 * and move that into the X register. Otherwise, just
7823 * load into the X register the offset computed into
7824 * the register specified by "index".
7827 sappend(s
, xfer_to_a(cstate
, inst
));
7828 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7829 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7831 s
= xfer_to_x(cstate
, inst
);
7834 * Load the item at the sum of the offset we've put in the
7835 * X register, the offset of the start of the network
7836 * layer header from the beginning of the link-layer
7837 * payload, and the constant part of the offset of the
7838 * start of the link-layer payload.
7840 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7841 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7843 sappend(inst
->s
, s
);
7846 * Do the computation only if the packet contains
7847 * the protocol in question.
7849 b
= gen_proto_abbrev_internal(cstate
, proto
);
7851 gen_and(inst
->b
, b
);
7865 * The offset is relative to the beginning of
7866 * the transport-layer header.
7868 * Load the X register with the length of the IPv4 header
7869 * (plus the offset of the link-layer header, if it's
7870 * a variable-length header), in bytes.
7872 * XXX - are there any cases where we want
7873 * cstate->off_nl_nosnap?
7874 * XXX - we should, if we're built with
7875 * IPv6 support, generate code to load either
7876 * IPv4, IPv6, or both, as appropriate.
7878 s
= gen_loadx_iphdrlen(cstate
);
7881 * The X register now contains the sum of the variable
7882 * part of the offset of the link-layer payload and the
7883 * length of the network-layer header.
7885 * Load into the A register the offset relative to
7886 * the beginning of the transport layer header,
7887 * add the X register to that, move that to the
7888 * X register, and load with an offset from the
7889 * X register equal to the sum of the constant part of
7890 * the offset of the link-layer payload and the offset,
7891 * relative to the beginning of the link-layer payload,
7892 * of the network-layer header.
7894 sappend(s
, xfer_to_a(cstate
, inst
));
7895 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7896 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7897 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7898 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7899 sappend(inst
->s
, s
);
7902 * Do the computation only if the packet contains
7903 * the protocol in question - which is true only
7904 * if this is an IP datagram and is the first or
7905 * only fragment of that datagram.
7907 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7909 gen_and(inst
->b
, b
);
7910 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7915 * Do the computation only if the packet contains
7916 * the protocol in question.
7918 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7920 gen_and(inst
->b
, b
);
7924 * Check if we have an icmp6 next header
7926 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7928 gen_and(inst
->b
, b
);
7931 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7933 * If "s" is non-null, it has code to arrange that the
7934 * X register contains the variable part of the offset
7935 * of the link-layer payload. Add to it the offset
7936 * computed into the register specified by "index",
7937 * and move that into the X register. Otherwise, just
7938 * load into the X register the offset computed into
7939 * the register specified by "index".
7942 sappend(s
, xfer_to_a(cstate
, inst
));
7943 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7944 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7946 s
= xfer_to_x(cstate
, inst
);
7949 * Load the item at the sum of the offset we've put in the
7950 * X register, the offset of the start of the network
7951 * layer header from the beginning of the link-layer
7952 * payload, and the constant part of the offset of the
7953 * start of the link-layer payload.
7955 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7956 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7959 sappend(inst
->s
, s
);
7963 inst
->regno
= regno
;
7964 s
= new_stmt(cstate
, BPF_ST
);
7966 sappend(inst
->s
, s
);
7972 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7976 * Catch errors reported by us and routines below us, and return NULL
7979 if (setjmp(cstate
->top_ctx
))
7982 return gen_load_internal(cstate
, proto
, inst
, size
);
7985 static struct block
*
7986 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7987 struct arth
*a1
, int reversed
)
7989 struct slist
*s0
, *s1
, *s2
;
7990 struct block
*b
, *tmp
;
7992 s0
= xfer_to_x(cstate
, a1
);
7993 s1
= xfer_to_a(cstate
, a0
);
7994 if (code
== BPF_JEQ
) {
7995 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7996 b
= new_block(cstate
, JMP(code
));
8000 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
8006 sappend(a0
->s
, a1
->s
);
8010 free_reg(cstate
, a0
->regno
);
8011 free_reg(cstate
, a1
->regno
);
8013 /* 'and' together protocol checks */
8016 gen_and(a0
->b
, tmp
= a1
->b
);
8030 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
8031 struct arth
*a1
, int reversed
)
8034 * Catch errors reported by us and routines below us, and return NULL
8037 if (setjmp(cstate
->top_ctx
))
8040 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
8044 gen_loadlen(compiler_state_t
*cstate
)
8051 * Catch errors reported by us and routines below us, and return NULL
8054 if (setjmp(cstate
->top_ctx
))
8057 regno
= alloc_reg(cstate
);
8058 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8059 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8060 s
->next
= new_stmt(cstate
, BPF_ST
);
8061 s
->next
->s
.k
= regno
;
8068 static struct arth
*
8069 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8075 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8077 reg
= alloc_reg(cstate
);
8079 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8081 s
->next
= new_stmt(cstate
, BPF_ST
);
8090 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8093 * Catch errors reported by us and routines below us, and return NULL
8096 if (setjmp(cstate
->top_ctx
))
8099 return gen_loadi_internal(cstate
, val
);
8103 * The a_arg dance is to avoid annoying whining by compilers that
8104 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8105 * It's not *used* after setjmp returns.
8108 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8110 struct arth
*a
= a_arg
;
8114 * Catch errors reported by us and routines below us, and return NULL
8117 if (setjmp(cstate
->top_ctx
))
8120 s
= xfer_to_a(cstate
, a
);
8122 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8125 s
= new_stmt(cstate
, BPF_ST
);
8133 * The a0_arg dance is to avoid annoying whining by compilers that
8134 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8135 * It's not *used* after setjmp returns.
8138 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8141 struct arth
*a0
= a0_arg
;
8142 struct slist
*s0
, *s1
, *s2
;
8145 * Catch errors reported by us and routines below us, and return NULL
8148 if (setjmp(cstate
->top_ctx
))
8152 * Disallow division by, or modulus by, zero; we do this here
8153 * so that it gets done even if the optimizer is disabled.
8155 * Also disallow shifts by a value greater than 31; we do this
8156 * here, for the same reason.
8158 if (code
== BPF_DIV
) {
8159 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8160 bpf_error(cstate
, "division by zero");
8161 } else if (code
== BPF_MOD
) {
8162 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8163 bpf_error(cstate
, "modulus by zero");
8164 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8165 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8166 bpf_error(cstate
, "shift by more than 31 bits");
8168 s0
= xfer_to_x(cstate
, a1
);
8169 s1
= xfer_to_a(cstate
, a0
);
8170 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8175 sappend(a0
->s
, a1
->s
);
8177 free_reg(cstate
, a0
->regno
);
8178 free_reg(cstate
, a1
->regno
);
8180 s0
= new_stmt(cstate
, BPF_ST
);
8181 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8188 * Initialize the table of used registers and the current register.
8191 init_regs(compiler_state_t
*cstate
)
8194 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8198 * Return the next free register.
8201 alloc_reg(compiler_state_t
*cstate
)
8203 int n
= BPF_MEMWORDS
;
8206 if (cstate
->regused
[cstate
->curreg
])
8207 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8209 cstate
->regused
[cstate
->curreg
] = 1;
8210 return cstate
->curreg
;
8213 bpf_error(cstate
, "too many registers needed to evaluate expression");
8218 * Return a register to the table so it can
8222 free_reg(compiler_state_t
*cstate
, int n
)
8224 cstate
->regused
[n
] = 0;
8227 static struct block
*
8228 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8233 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8234 b
= new_block(cstate
, JMP(jmp
));
8242 gen_greater(compiler_state_t
*cstate
, int n
)
8245 * Catch errors reported by us and routines below us, and return NULL
8248 if (setjmp(cstate
->top_ctx
))
8251 return gen_len(cstate
, BPF_JGE
, n
);
8255 * Actually, this is less than or equal.
8258 gen_less(compiler_state_t
*cstate
, int n
)
8263 * Catch errors reported by us and routines below us, and return NULL
8266 if (setjmp(cstate
->top_ctx
))
8269 b
= gen_len(cstate
, BPF_JGT
, n
);
8276 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8277 * the beginning of the link-layer header.
8278 * XXX - that means you can't test values in the radiotap header, but
8279 * as that header is difficult if not impossible to parse generally
8280 * without a loop, that might not be a severe problem. A new keyword
8281 * "radio" could be added for that, although what you'd really want
8282 * would be a way of testing particular radio header values, which
8283 * would generate code appropriate to the radio header in question.
8286 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8292 * Catch errors reported by us and routines below us, and return NULL
8295 if (setjmp(cstate
->top_ctx
))
8303 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8306 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8310 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8314 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8318 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8322 b
= new_block(cstate
, JMP(BPF_JEQ
));
8330 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8332 bpf_u_int32 hostmask
;
8333 struct block
*b0
, *b1
, *b2
;
8334 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8337 * Catch errors reported by us and routines below us, and return NULL
8340 if (setjmp(cstate
->top_ctx
))
8347 switch (cstate
->linktype
) {
8349 case DLT_ARCNET_LINUX
:
8350 // ARCnet broadcast is [8-bit] destination address 0.
8351 return gen_ahostop(cstate
, 0, Q_DST
);
8353 case DLT_NETANALYZER
:
8354 case DLT_NETANALYZER_TRANSPARENT
:
8355 b1
= gen_prevlinkhdr_check(cstate
);
8356 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8361 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8363 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8364 case DLT_IEEE802_11
:
8365 case DLT_PRISM_HEADER
:
8366 case DLT_IEEE802_11_RADIO_AVS
:
8367 case DLT_IEEE802_11_RADIO
:
8369 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8370 case DLT_IP_OVER_FC
:
8371 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8373 fail_kw_on_dlt(cstate
, "broadcast");
8378 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8379 * as an indication that we don't know the netmask, and fail
8382 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8383 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8384 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8385 hostmask
= ~cstate
->netmask
;
8386 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8387 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8392 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8397 * Generate code to test the low-order bit of a MAC address (that's
8398 * the bottom bit of the *first* byte).
8400 static struct block
*
8401 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8403 register struct block
*b0
;
8404 register struct slist
*s
;
8406 /* link[offset] & 1 != 0 */
8407 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8408 b0
= new_block(cstate
, JMP(BPF_JSET
));
8415 gen_multicast(compiler_state_t
*cstate
, int proto
)
8417 register struct block
*b0
, *b1
, *b2
;
8418 register struct slist
*s
;
8421 * Catch errors reported by us and routines below us, and return NULL
8424 if (setjmp(cstate
->top_ctx
))
8431 switch (cstate
->linktype
) {
8433 case DLT_ARCNET_LINUX
:
8434 // ARCnet multicast is the same as broadcast.
8435 return gen_ahostop(cstate
, 0, Q_DST
);
8437 case DLT_NETANALYZER
:
8438 case DLT_NETANALYZER_TRANSPARENT
:
8439 b1
= gen_prevlinkhdr_check(cstate
);
8440 /* ether[0] & 1 != 0 */
8441 b0
= gen_mac_multicast(cstate
, 0);
8447 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8449 * XXX - was that referring to bit-order issues?
8451 /* fddi[1] & 1 != 0 */
8452 return gen_mac_multicast(cstate
, 1);
8454 /* tr[2] & 1 != 0 */
8455 return gen_mac_multicast(cstate
, 2);
8456 case DLT_IEEE802_11
:
8457 case DLT_PRISM_HEADER
:
8458 case DLT_IEEE802_11_RADIO_AVS
:
8459 case DLT_IEEE802_11_RADIO
:
8464 * For control frames, there is no DA.
8466 * For management frames, DA is at an
8467 * offset of 4 from the beginning of
8470 * For data frames, DA is at an offset
8471 * of 4 from the beginning of the packet
8472 * if To DS is clear and at an offset of
8473 * 16 from the beginning of the packet
8478 * Generate the tests to be done for data frames.
8480 * First, check for To DS set, i.e. "link[1] & 0x01".
8482 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8483 b1
= new_block(cstate
, JMP(BPF_JSET
));
8484 b1
->s
.k
= 0x01; /* To DS */
8488 * If To DS is set, the DA is at 16.
8490 b0
= gen_mac_multicast(cstate
, 16);
8494 * Now, check for To DS not set, i.e. check
8495 * "!(link[1] & 0x01)".
8497 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8498 b2
= new_block(cstate
, JMP(BPF_JSET
));
8499 b2
->s
.k
= 0x01; /* To DS */
8504 * If To DS is not set, the DA is at 4.
8506 b1
= gen_mac_multicast(cstate
, 4);
8510 * Now OR together the last two checks. That gives
8511 * the complete set of checks for data frames.
8516 * Now check for a data frame.
8517 * I.e, check "link[0] & 0x08".
8519 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8520 b1
= new_block(cstate
, JMP(BPF_JSET
));
8525 * AND that with the checks done for data frames.
8530 * If the high-order bit of the type value is 0, this
8531 * is a management frame.
8532 * I.e, check "!(link[0] & 0x08)".
8534 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8535 b2
= new_block(cstate
, JMP(BPF_JSET
));
8541 * For management frames, the DA is at 4.
8543 b1
= gen_mac_multicast(cstate
, 4);
8547 * OR that with the checks done for data frames.
8548 * That gives the checks done for management and
8554 * If the low-order bit of the type value is 1,
8555 * this is either a control frame or a frame
8556 * with a reserved type, and thus not a
8559 * I.e., check "!(link[0] & 0x04)".
8561 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8562 b1
= new_block(cstate
, JMP(BPF_JSET
));
8568 * AND that with the checks for data and management
8573 case DLT_IP_OVER_FC
:
8574 b0
= gen_mac_multicast(cstate
, 2);
8579 fail_kw_on_dlt(cstate
, "multicast");
8583 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8584 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8589 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8590 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8594 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8600 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8601 * we can look at special meta-data in the filter expression; otherwise we
8602 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8603 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8604 * pcap_activate() conditionally sets.
8607 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8609 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8611 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8613 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8618 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8620 register struct block
*b0
;
8623 * Catch errors reported by us and routines below us, and return NULL
8626 if (setjmp(cstate
->top_ctx
))
8630 * Only some data link types support ifindex qualifiers.
8632 switch (cstate
->linktype
) {
8633 case DLT_LINUX_SLL2
:
8634 /* match packets on this interface */
8635 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8638 #if defined(__linux__)
8639 require_basic_bpf_extensions(cstate
, "ifindex");
8641 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8643 #else /* defined(__linux__) */
8644 fail_kw_on_dlt(cstate
, "ifindex");
8646 #endif /* defined(__linux__) */
8652 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8653 * Outbound traffic is sent by this machine, while inbound traffic is
8654 * sent by a remote machine (and may include packets destined for a
8655 * unicast or multicast link-layer address we are not subscribing to).
8656 * These are the same definitions implemented by pcap_setdirection().
8657 * Capturing only unicast traffic destined for this host is probably
8658 * better accomplished using a higher-layer filter.
8661 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8663 register struct block
*b0
;
8666 * Catch errors reported by us and routines below us, and return NULL
8669 if (setjmp(cstate
->top_ctx
))
8673 * Only some data link types support inbound/outbound qualifiers.
8675 switch (cstate
->linktype
) {
8677 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8678 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8682 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8683 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8687 /* match outgoing packets */
8688 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8690 /* to filter on inbound traffic, invert the match */
8695 case DLT_LINUX_SLL2
:
8696 /* match outgoing packets */
8697 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8699 /* to filter on inbound traffic, invert the match */
8705 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8706 outbound
? PF_OUT
: PF_IN
);
8710 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8713 case DLT_JUNIPER_MFR
:
8714 case DLT_JUNIPER_MLFR
:
8715 case DLT_JUNIPER_MLPPP
:
8716 case DLT_JUNIPER_ATM1
:
8717 case DLT_JUNIPER_ATM2
:
8718 case DLT_JUNIPER_PPPOE
:
8719 case DLT_JUNIPER_PPPOE_ATM
:
8720 case DLT_JUNIPER_GGSN
:
8721 case DLT_JUNIPER_ES
:
8722 case DLT_JUNIPER_MONITOR
:
8723 case DLT_JUNIPER_SERVICES
:
8724 case DLT_JUNIPER_ETHER
:
8725 case DLT_JUNIPER_PPP
:
8726 case DLT_JUNIPER_FRELAY
:
8727 case DLT_JUNIPER_CHDLC
:
8728 case DLT_JUNIPER_VP
:
8729 case DLT_JUNIPER_ST
:
8730 case DLT_JUNIPER_ISM
:
8731 case DLT_JUNIPER_VS
:
8732 case DLT_JUNIPER_SRX_E2E
:
8733 case DLT_JUNIPER_FIBRECHANNEL
:
8734 case DLT_JUNIPER_ATM_CEMIC
:
8735 /* juniper flags (including direction) are stored
8736 * the byte after the 3-byte magic number */
8737 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8742 * If we have packet meta-data indicating a direction,
8743 * and that metadata can be checked by BPF code, check
8744 * it. Otherwise, give up, as this link-layer type has
8745 * nothing in the packet data.
8747 * Currently, the only platform where a BPF filter can
8748 * check that metadata is Linux with the in-kernel
8749 * BPF interpreter. If other packet capture mechanisms
8750 * and BPF filters also supported this, it would be
8751 * nice. It would be even better if they made that
8752 * metadata available so that we could provide it
8753 * with newer capture APIs, allowing it to be saved
8756 #if defined(__linux__)
8757 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8758 /* match outgoing packets */
8759 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8762 /* to filter on inbound traffic, invert the match */
8765 #else /* defined(__linux__) */
8766 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8768 #endif /* defined(__linux__) */
8773 /* PF firewall log matched interface */
8775 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8781 * Catch errors reported by us and routines below us, and return NULL
8784 if (setjmp(cstate
->top_ctx
))
8787 if (cstate
->linktype
!= DLT_PFLOG
) {
8788 bpf_error(cstate
, "ifname supported only on PF linktype");
8791 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8792 off
= offsetof(struct pfloghdr
, ifname
);
8793 if (strlen(ifname
) >= len
) {
8794 bpf_error(cstate
, "ifname interface names can only be %d characters",
8798 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8799 (const u_char
*)ifname
);
8803 /* PF firewall log ruleset name */
8805 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8810 * Catch errors reported by us and routines below us, and return NULL
8813 if (setjmp(cstate
->top_ctx
))
8816 if (cstate
->linktype
!= DLT_PFLOG
) {
8817 bpf_error(cstate
, "ruleset supported only on PF linktype");
8821 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8822 bpf_error(cstate
, "ruleset names can only be %ld characters",
8823 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8827 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8828 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8832 /* PF firewall log rule number */
8834 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8839 * Catch errors reported by us and routines below us, and return NULL
8842 if (setjmp(cstate
->top_ctx
))
8845 if (cstate
->linktype
!= DLT_PFLOG
) {
8846 bpf_error(cstate
, "rnr supported only on PF linktype");
8850 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8855 /* PF firewall log sub-rule number */
8857 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8862 * Catch errors reported by us and routines below us, and return NULL
8865 if (setjmp(cstate
->top_ctx
))
8868 if (cstate
->linktype
!= DLT_PFLOG
) {
8869 bpf_error(cstate
, "srnr supported only on PF linktype");
8873 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8878 /* PF firewall log reason code */
8880 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8885 * Catch errors reported by us and routines below us, and return NULL
8888 if (setjmp(cstate
->top_ctx
))
8891 if (cstate
->linktype
!= DLT_PFLOG
) {
8892 bpf_error(cstate
, "reason supported only on PF linktype");
8896 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8897 (bpf_u_int32
)reason
);
8901 /* PF firewall log action */
8903 gen_pf_action(compiler_state_t
*cstate
, int action
)
8908 * Catch errors reported by us and routines below us, and return NULL
8911 if (setjmp(cstate
->top_ctx
))
8914 if (cstate
->linktype
!= DLT_PFLOG
) {
8915 bpf_error(cstate
, "action supported only on PF linktype");
8919 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8920 (bpf_u_int32
)action
);
8924 /* IEEE 802.11 wireless header */
8926 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8931 * Catch errors reported by us and routines below us, and return NULL
8934 if (setjmp(cstate
->top_ctx
))
8937 switch (cstate
->linktype
) {
8939 case DLT_IEEE802_11
:
8940 case DLT_PRISM_HEADER
:
8941 case DLT_IEEE802_11_RADIO_AVS
:
8942 case DLT_IEEE802_11_RADIO
:
8944 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8948 fail_kw_on_dlt(cstate
, "type/subtype");
8956 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8961 * Catch errors reported by us and routines below us, and return NULL
8964 if (setjmp(cstate
->top_ctx
))
8967 switch (cstate
->linktype
) {
8969 case DLT_IEEE802_11
:
8970 case DLT_PRISM_HEADER
:
8971 case DLT_IEEE802_11_RADIO_AVS
:
8972 case DLT_IEEE802_11_RADIO
:
8977 fail_kw_on_dlt(cstate
, "dir");
8981 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8982 IEEE80211_FC1_DIR_MASK
);
8987 // Process an ARCnet host address string.
8989 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8992 * Catch errors reported by us and routines below us, and return NULL
8995 if (setjmp(cstate
->top_ctx
))
8998 switch (cstate
->linktype
) {
9001 case DLT_ARCNET_LINUX
:
9002 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
9003 q
.proto
== Q_LINK
) {
9006 * The lexer currently defines the address format in a
9007 * way that makes this error condition never true.
9008 * Let's check it anyway in case this part of the lexer
9009 * changes in future.
9011 if (! pcapint_atoan(s
, &addr
))
9012 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
9013 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
9015 bpf_error(cstate
, "ARCnet address used in non-arc expression");
9019 bpf_error(cstate
, "aid supported only on ARCnet");
9024 // Compare an ARCnet host address with the given value.
9025 static struct block
*
9026 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
9028 register struct block
*b0
, *b1
;
9032 * ARCnet is different from Ethernet: the source address comes before
9033 * the destination address, each is one byte long. This holds for all
9034 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
9035 * in the 1983 edition of the "ARCNET Designer's Handbook" published
9036 * by Datapoint (document number 61610-01).
9039 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
9042 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
9045 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9046 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9052 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
9053 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
9058 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
9062 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
9066 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
9070 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
9074 bpf_error(cstate
, "'ra' is only supported on 802.11");
9078 bpf_error(cstate
, "'ta' is only supported on 802.11");
9085 static struct block
*
9086 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9088 struct block
*b0
, *b1
;
9090 /* check for VLAN, including 802.1ad and QinQ */
9091 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9092 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9095 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9101 static struct block
*
9102 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9104 if (vlan_num
> 0x0fff) {
9105 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9108 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9111 static struct block
*
9112 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9115 struct block
*b0
, *b1
;
9117 b0
= gen_vlan_tpid_test(cstate
);
9120 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9126 * Both payload and link header type follow the VLAN tags so that
9127 * both need to be updated.
9129 cstate
->off_linkpl
.constant_part
+= 4;
9130 cstate
->off_linktype
.constant_part
+= 4;
9135 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9136 /* add v to variable part of off */
9138 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9139 bpf_u_int32 v
, struct slist
*s
)
9143 if (!off
->is_variable
)
9144 off
->is_variable
= 1;
9146 off
->reg
= alloc_reg(cstate
);
9148 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9151 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9154 s2
= new_stmt(cstate
, BPF_ST
);
9160 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9161 * and link type offsets first
9164 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9168 /* offset determined at run time, shift variable part */
9170 cstate
->is_vlan_vloffset
= 1;
9171 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9172 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9174 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9175 sappend(s
.next
, b_tpid
->head
->stmts
);
9176 b_tpid
->head
->stmts
= s
.next
;
9180 * patch block b_vid (VLAN id test) to load VID value either from packet
9181 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9184 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9186 struct slist
*s
, *s2
, *sjeq
;
9189 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9190 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9192 /* true -> next instructions, false -> beginning of b_vid */
9193 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9195 sjeq
->s
.jf
= b_vid
->stmts
;
9198 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
9199 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
9203 /* Jump to the test in b_vid. We need to jump one instruction before
9204 * the end of the b_vid block so that we only skip loading the TCI
9205 * from packet data and not the 'and' instruction extracting VID.
9208 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9210 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9214 /* insert our statements at the beginning of b_vid */
9215 sappend(s
, b_vid
->stmts
);
9220 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9221 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9222 * tag can be either in metadata or in packet data; therefore if the
9223 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9224 * header for VLAN tag. As the decision is done at run time, we need
9225 * update variable part of the offsets
9227 static struct block
*
9228 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9231 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9234 /* generate new filter code based on extracting packet
9236 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9237 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9239 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9244 * This is tricky. We need to insert the statements updating variable
9245 * parts of offsets before the traditional TPID and VID tests so
9246 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9247 * we do not want this update to affect those checks. That's why we
9248 * generate both test blocks first and insert the statements updating
9249 * variable parts of both offsets after that. This wouldn't work if
9250 * there already were variable length link header when entering this
9251 * function but gen_vlan_bpf_extensions() isn't called in that case.
9253 b_tpid
= gen_vlan_tpid_test(cstate
);
9255 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9257 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9262 gen_vlan_patch_vid_test(cstate
, b_vid
);
9272 * support IEEE 802.1Q VLAN trunk over ethernet
9275 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9280 * Catch errors reported by us and routines below us, and return NULL
9283 if (setjmp(cstate
->top_ctx
))
9286 /* can't check for VLAN-encapsulated packets inside MPLS */
9287 if (cstate
->label_stack_depth
> 0)
9288 bpf_error(cstate
, "no VLAN match after MPLS");
9291 * Check for a VLAN packet, and then change the offsets to point
9292 * to the type and data fields within the VLAN packet. Just
9293 * increment the offsets, so that we can support a hierarchy, e.g.
9294 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9297 * XXX - this is a bit of a kludge. If we were to split the
9298 * compiler into a parser that parses an expression and
9299 * generates an expression tree, and a code generator that
9300 * takes an expression tree (which could come from our
9301 * parser or from some other parser) and generates BPF code,
9302 * we could perhaps make the offsets parameters of routines
9303 * and, in the handler for an "AND" node, pass to subnodes
9304 * other than the VLAN node the adjusted offsets.
9306 * This would mean that "vlan" would, instead of changing the
9307 * behavior of *all* tests after it, change only the behavior
9308 * of tests ANDed with it. That would change the documented
9309 * semantics of "vlan", which might break some expressions.
9310 * However, it would mean that "(vlan and ip) or ip" would check
9311 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9312 * checking only for VLAN-encapsulated IP, so that could still
9313 * be considered worth doing; it wouldn't break expressions
9314 * that are of the form "vlan and ..." or "vlan N and ...",
9315 * which I suspect are the most common expressions involving
9316 * "vlan". "vlan or ..." doesn't necessarily do what the user
9317 * would really want, now, as all the "or ..." tests would
9318 * be done assuming a VLAN, even though the "or" could be viewed
9319 * as meaning "or, if this isn't a VLAN packet...".
9321 switch (cstate
->linktype
) {
9325 * Newer version of the Linux kernel pass around
9326 * packets in which the VLAN tag has been removed
9327 * from the packet data and put into metadata.
9329 * This requires special treatment.
9331 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9332 /* Verify that this is the outer part of the packet and
9333 * not encapsulated somehow. */
9334 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9335 cstate
->off_linkhdr
.constant_part
==
9336 cstate
->off_outermostlinkhdr
.constant_part
) {
9338 * Do we need special VLAN handling?
9340 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9341 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9344 b0
= gen_vlan_no_bpf_extensions(cstate
,
9345 vlan_num
, has_vlan_tag
);
9348 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9352 case DLT_NETANALYZER
:
9353 case DLT_NETANALYZER_TRANSPARENT
:
9354 case DLT_IEEE802_11
:
9355 case DLT_PRISM_HEADER
:
9356 case DLT_IEEE802_11_RADIO_AVS
:
9357 case DLT_IEEE802_11_RADIO
:
9359 * These are either Ethernet packets with an additional
9360 * metadata header (the NetAnalyzer types), or 802.11
9361 * packets, possibly with an additional metadata header.
9363 * For the first of those, the VLAN tag is in the normal
9364 * place, so the special-case handling above isn't
9367 * For the second of those, we don't do the special-case
9370 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9374 bpf_error(cstate
, "no VLAN support for %s",
9375 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9379 cstate
->vlan_stack_depth
++;
9387 * The label_num_arg dance is to avoid annoying whining by compilers that
9388 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9389 * It's not *used* after setjmp returns.
9391 static struct block
*
9392 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9395 struct block
*b0
, *b1
;
9397 if (cstate
->label_stack_depth
> 0) {
9398 /* just match the bottom-of-stack bit clear */
9399 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9402 * We're not in an MPLS stack yet, so check the link-layer
9403 * type against MPLS.
9405 switch (cstate
->linktype
) {
9407 case DLT_C_HDLC
: /* fall through */
9410 case DLT_NETANALYZER
:
9411 case DLT_NETANALYZER_TRANSPARENT
:
9412 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9416 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9419 /* FIXME add other DLT_s ...
9420 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9421 * leave it for now */
9424 bpf_error(cstate
, "no MPLS support for %s",
9425 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9430 /* If a specific MPLS label is requested, check it */
9431 if (has_label_num
) {
9432 if (label_num
> 0xFFFFF) {
9433 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9434 label_num
, 0xFFFFF);
9436 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9437 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9438 0xfffff000); /* only compare the first 20 bits */
9444 * Change the offsets to point to the type and data fields within
9445 * the MPLS packet. Just increment the offsets, so that we
9446 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9447 * capture packets with an outer label of 100000 and an inner
9450 * Increment the MPLS stack depth as well; this indicates that
9451 * we're checking MPLS-encapsulated headers, to make sure higher
9452 * level code generators don't try to match against IP-related
9453 * protocols such as Q_ARP, Q_RARP etc.
9455 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9457 cstate
->off_nl_nosnap
+= 4;
9458 cstate
->off_nl
+= 4;
9459 cstate
->label_stack_depth
++;
9464 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9467 * Catch errors reported by us and routines below us, and return NULL
9470 if (setjmp(cstate
->top_ctx
))
9473 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9477 * Support PPPOE discovery and session.
9480 gen_pppoed(compiler_state_t
*cstate
)
9483 * Catch errors reported by us and routines below us, and return NULL
9486 if (setjmp(cstate
->top_ctx
))
9489 /* check for PPPoE discovery */
9490 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9494 * RFC 2516 Section 4:
9496 * The Ethernet payload for PPPoE is as follows:
9499 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
9500 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9501 * | VER | TYPE | CODE | SESSION_ID |
9502 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9503 * | LENGTH | payload ~
9504 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9507 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9509 struct block
*b0
, *b1
;
9512 * Catch errors reported by us and routines below us, and return NULL
9515 if (setjmp(cstate
->top_ctx
))
9519 * Test against the PPPoE session link-layer type.
9521 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9523 /* If a specific session is requested, check PPPoE session id */
9525 if (sess_num
> UINT16_MAX
) {
9526 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9527 sess_num
, UINT16_MAX
);
9529 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9535 * Change the offsets to point to the type and data fields within
9536 * the PPP packet, and note that this is PPPoE rather than
9539 * XXX - this is a bit of a kludge. See the comments in
9542 * The "network-layer" protocol is PPPoE, which has a 6-byte
9543 * PPPoE header, followed by a PPP packet.
9545 * There is no HDLC encapsulation for the PPP packet (it's
9546 * encapsulated in PPPoES instead), so the link-layer type
9547 * starts at the first byte of the PPP packet. For PPPoE,
9548 * that offset is relative to the beginning of the total
9549 * link-layer payload, including any 802.2 LLC header, so
9550 * it's 6 bytes past cstate->off_nl.
9552 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9553 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9554 cstate
->off_linkpl
.reg
);
9556 cstate
->off_linktype
= cstate
->off_linkhdr
;
9557 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9560 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9565 /* Check that this is Geneve and the VNI is correct if
9566 * specified. Parameterized to handle both IPv4 and IPv6. */
9567 static struct block
*
9568 gen_geneve_check(compiler_state_t
*cstate
,
9569 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9570 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9572 struct block
*b0
, *b1
;
9574 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9576 /* Check that we are operating on version 0. Otherwise, we
9577 * can't decode the rest of the fields. The version is 2 bits
9578 * in the first byte of the Geneve header. */
9579 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9584 if (vni
> 0xffffff) {
9585 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9588 vni
<<= 8; /* VNI is in the upper 3 bytes */
9589 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9597 /* The IPv4 and IPv6 Geneve checks need to do two things:
9598 * - Verify that this actually is Geneve with the right VNI.
9599 * - Place the IP header length (plus variable link prefix if
9600 * needed) into register A to be used later to compute
9601 * the inner packet offsets. */
9602 static struct block
*
9603 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9605 struct block
*b0
, *b1
;
9606 struct slist
*s
, *s1
;
9608 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9610 /* Load the IP header length into A. */
9611 s
= gen_loadx_iphdrlen(cstate
);
9613 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9616 /* Forcibly append these statements to the true condition
9617 * of the protocol check by creating a new block that is
9618 * always true and ANDing them. */
9619 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9628 static struct block
*
9629 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9631 struct block
*b0
, *b1
;
9632 struct slist
*s
, *s1
;
9634 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9636 /* Load the IP header length. We need to account for a
9637 * variable length link prefix if there is one. */
9638 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9640 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9644 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9648 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
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 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9658 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9667 /* We need to store three values based on the Geneve header::
9668 * - The offset of the linktype.
9669 * - The offset of the end of the Geneve header.
9670 * - The offset of the end of the encapsulated MAC header. */
9671 static struct slist
*
9672 gen_geneve_offsets(compiler_state_t
*cstate
)
9674 struct slist
*s
, *s1
, *s_proto
;
9676 /* First we need to calculate the offset of the Geneve header
9677 * itself. This is composed of the IP header previously calculated
9678 * (include any variable link prefix) and stored in A plus the
9679 * fixed sized headers (fixed link prefix, MAC length, and UDP
9681 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9682 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9684 /* Stash this in X since we'll need it later. */
9685 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9688 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9690 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9694 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9695 cstate
->off_linktype
.is_variable
= 1;
9696 cstate
->off_linktype
.constant_part
= 0;
9698 s1
= new_stmt(cstate
, BPF_ST
);
9699 s1
->s
.k
= cstate
->off_linktype
.reg
;
9702 /* Load the Geneve option length and mask and shift to get the
9703 * number of bytes. It is stored in the first byte of the Geneve
9705 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9709 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9713 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9717 /* Add in the rest of the Geneve base header. */
9718 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9722 /* Add the Geneve header length to its offset and store. */
9723 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9727 /* Set the encapsulated type as Ethernet. Even though we may
9728 * not actually have Ethernet inside there are two reasons this
9730 * - The linktype field is always in EtherType format regardless
9731 * of whether it is in Geneve or an inner Ethernet frame.
9732 * - The only link layer that we have specific support for is
9733 * Ethernet. We will confirm that the packet actually is
9734 * Ethernet at runtime before executing these checks. */
9735 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9737 s1
= new_stmt(cstate
, BPF_ST
);
9738 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9741 /* Calculate whether we have an Ethernet header or just raw IP/
9742 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9743 * and linktype by 14 bytes so that the network header can be found
9744 * seamlessly. Otherwise, keep what we've calculated already. */
9746 /* We have a bare jmp so we can't use the optimizer. */
9747 cstate
->no_optimize
= 1;
9749 /* Load the EtherType in the Geneve header, 2 bytes in. */
9750 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9754 /* Load X with the end of the Geneve header. */
9755 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9756 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9759 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9760 * end of this check, we should have the total length in X. In
9761 * the non-Ethernet case, it's already there. */
9762 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9763 s_proto
->s
.k
= ETHERTYPE_TEB
;
9764 sappend(s
, s_proto
);
9766 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9770 /* Since this is Ethernet, use the EtherType of the payload
9771 * directly as the linktype. Overwrite what we already have. */
9772 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9776 s1
= new_stmt(cstate
, BPF_ST
);
9777 s1
->s
.k
= cstate
->off_linktype
.reg
;
9780 /* Advance two bytes further to get the end of the Ethernet
9782 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9786 /* Move the result to X. */
9787 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9790 /* Store the final result of our linkpl calculation. */
9791 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9792 cstate
->off_linkpl
.is_variable
= 1;
9793 cstate
->off_linkpl
.constant_part
= 0;
9795 s1
= new_stmt(cstate
, BPF_STX
);
9796 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9805 /* Check to see if this is a Geneve packet. */
9807 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9809 struct block
*b0
, *b1
;
9813 * Catch errors reported by us and routines below us, and return NULL
9816 if (setjmp(cstate
->top_ctx
))
9819 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9820 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9825 /* Later filters should act on the payload of the Geneve frame,
9826 * update all of the header pointers. Attach this code so that
9827 * it gets executed in the event that the Geneve filter matches. */
9828 s
= gen_geneve_offsets(cstate
);
9830 b1
= gen_true(cstate
);
9831 sappend(s
, b1
->stmts
);
9836 cstate
->is_encap
= 1;
9841 /* Check that this is VXLAN and the VNI is correct if
9842 * specified. Parameterized to handle both IPv4 and IPv6. */
9843 static struct block
*
9844 gen_vxlan_check(compiler_state_t
*cstate
,
9845 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9846 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9848 struct block
*b0
, *b1
;
9850 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9852 /* Check that the VXLAN header has the flag bits set
9854 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9859 if (vni
> 0xffffff) {
9860 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9863 vni
<<= 8; /* VNI is in the upper 3 bytes */
9864 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9872 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9873 * - Verify that this actually is VXLAN with the right VNI.
9874 * - Place the IP header length (plus variable link prefix if
9875 * needed) into register A to be used later to compute
9876 * the inner packet offsets. */
9877 static struct block
*
9878 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9880 struct block
*b0
, *b1
;
9881 struct slist
*s
, *s1
;
9883 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9885 /* Load the IP header length into A. */
9886 s
= gen_loadx_iphdrlen(cstate
);
9888 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9891 /* Forcibly append these statements to the true condition
9892 * of the protocol check by creating a new block that is
9893 * always true and ANDing them. */
9894 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9903 static struct block
*
9904 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9906 struct block
*b0
, *b1
;
9907 struct slist
*s
, *s1
;
9909 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9911 /* Load the IP header length. We need to account for a
9912 * variable length link prefix if there is one. */
9913 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9915 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9919 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9923 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
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 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9933 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9942 /* We need to store three values based on the VXLAN header:
9943 * - The offset of the linktype.
9944 * - The offset of the end of the VXLAN header.
9945 * - The offset of the end of the encapsulated MAC header. */
9946 static struct slist
*
9947 gen_vxlan_offsets(compiler_state_t
*cstate
)
9949 struct slist
*s
, *s1
;
9951 /* Calculate the offset of the VXLAN header itself. This
9952 * includes the IP header computed previously (including any
9953 * variable link prefix) and stored in A plus the fixed size
9954 * headers (fixed link prefix, MAC length, UDP header). */
9955 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9956 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9958 /* Add the VXLAN header length to its offset and store */
9959 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9963 /* Push the link header. VXLAN packets always contain Ethernet
9965 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9967 s1
= new_stmt(cstate
, BPF_ST
);
9968 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9971 /* As the payload is an Ethernet packet, we can use the
9972 * EtherType of the payload directly as the linktype. */
9973 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9977 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9978 cstate
->off_linktype
.is_variable
= 1;
9979 cstate
->off_linktype
.constant_part
= 0;
9981 s1
= new_stmt(cstate
, BPF_ST
);
9982 s1
->s
.k
= cstate
->off_linktype
.reg
;
9985 /* Two bytes further is the end of the Ethernet header and the
9986 * start of the payload. */
9987 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9991 /* Move the result to X. */
9992 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9995 /* Store the final result of our linkpl calculation. */
9996 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9997 cstate
->off_linkpl
.is_variable
= 1;
9998 cstate
->off_linkpl
.constant_part
= 0;
10000 s1
= new_stmt(cstate
, BPF_STX
);
10001 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10004 cstate
->off_nl
= 0;
10009 /* Check to see if this is a VXLAN packet. */
10011 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
10013 struct block
*b0
, *b1
;
10017 * Catch errors reported by us and routines below us, and return NULL
10020 if (setjmp(cstate
->top_ctx
))
10023 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
10024 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
10029 /* Later filters should act on the payload of the VXLAN frame,
10030 * update all of the header pointers. Attach this code so that
10031 * it gets executed in the event that the VXLAN filter matches. */
10032 s
= gen_vxlan_offsets(cstate
);
10034 b1
= gen_true(cstate
);
10035 sappend(s
, b1
->stmts
);
10040 cstate
->is_encap
= 1;
10045 /* Check that the encapsulated frame has a link layer header
10046 * for Ethernet filters. */
10047 static struct block
*
10048 gen_encap_ll_check(compiler_state_t
*cstate
)
10051 struct slist
*s
, *s1
;
10053 /* The easiest way to see if there is a link layer present
10054 * is to check if the link layer header and payload are not
10057 /* Geneve always generates pure variable offsets so we can
10058 * compare only the registers. */
10059 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
10060 s
->s
.k
= cstate
->off_linkhdr
.reg
;
10062 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
10063 s1
->s
.k
= cstate
->off_linkpl
.reg
;
10066 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
10074 static struct block
*
10075 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
10076 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10080 switch (atmfield
) {
10083 if (!cstate
->is_atm
)
10084 bpf_error(cstate
, "'vpi' supported only on raw ATM");
10085 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10087 if (jvalue
> UINT8_MAX
)
10088 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
10089 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10090 0xffffffffU
, jtype
, reverse
, jvalue
);
10094 if (!cstate
->is_atm
)
10095 bpf_error(cstate
, "'vci' supported only on raw ATM");
10096 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10098 if (jvalue
> UINT16_MAX
)
10099 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
10100 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10101 0xffffffffU
, jtype
, reverse
, jvalue
);
10105 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10106 abort(); /* XXX - this isn't on FreeBSD */
10107 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10108 0x0fU
, jtype
, reverse
, jvalue
);
10112 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10114 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10115 0xffffffffU
, jtype
, reverse
, jvalue
);
10124 static struct block
*
10125 gen_atmtype_metac(compiler_state_t
*cstate
)
10127 struct block
*b0
, *b1
;
10129 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10130 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10135 static struct block
*
10136 gen_atmtype_sc(compiler_state_t
*cstate
)
10138 struct block
*b0
, *b1
;
10140 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10141 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10146 static struct block
*
10147 gen_atmtype_llc(compiler_state_t
*cstate
)
10151 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10152 cstate
->linktype
= cstate
->prevlinktype
;
10157 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10158 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10161 * Catch errors reported by us and routines below us, and return NULL
10164 if (setjmp(cstate
->top_ctx
))
10167 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10172 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10174 struct block
*b0
, *b1
;
10177 * Catch errors reported by us and routines below us, and return NULL
10180 if (setjmp(cstate
->top_ctx
))
10186 /* Get all packets in Meta signalling Circuit */
10187 if (!cstate
->is_atm
)
10188 bpf_error(cstate
, "'metac' supported only on raw ATM");
10189 b1
= gen_atmtype_metac(cstate
);
10193 /* Get all packets in Broadcast Circuit*/
10194 if (!cstate
->is_atm
)
10195 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10196 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10197 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10202 /* Get all cells in Segment OAM F4 circuit*/
10203 if (!cstate
->is_atm
)
10204 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10205 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10206 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10211 /* Get all cells in End-to-End OAM F4 Circuit*/
10212 if (!cstate
->is_atm
)
10213 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10214 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10215 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10220 /* Get all packets in connection Signalling Circuit */
10221 if (!cstate
->is_atm
)
10222 bpf_error(cstate
, "'sc' supported only on raw ATM");
10223 b1
= gen_atmtype_sc(cstate
);
10227 /* Get all packets in ILMI Circuit */
10228 if (!cstate
->is_atm
)
10229 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10230 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10231 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10236 /* Get all LANE packets */
10237 if (!cstate
->is_atm
)
10238 bpf_error(cstate
, "'lane' supported only on raw ATM");
10239 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10242 * Arrange that all subsequent tests assume LANE
10243 * rather than LLC-encapsulated packets, and set
10244 * the offsets appropriately for LANE-encapsulated
10247 * We assume LANE means Ethernet, not Token Ring.
10249 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10250 cstate
->off_payload
+ 2, /* Ethernet header */
10252 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10253 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10254 cstate
->off_nl
= 0; /* Ethernet II */
10255 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10265 * Filtering for MTP2 messages based on li value
10266 * FISU, length is null
10267 * LSSU, length is 1 or 2
10268 * MSU, length is 3 or more
10269 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10272 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10274 struct block
*b0
, *b1
;
10277 * Catch errors reported by us and routines below us, and return NULL
10280 if (setjmp(cstate
->top_ctx
))
10286 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10287 (cstate
->linktype
!= DLT_ERF
) &&
10288 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10289 bpf_error(cstate
, "'fisu' supported only on MTP2");
10290 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10291 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10292 0x3fU
, BPF_JEQ
, 0, 0U);
10296 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10297 (cstate
->linktype
!= DLT_ERF
) &&
10298 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10299 bpf_error(cstate
, "'lssu' supported only on MTP2");
10300 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10301 0x3fU
, BPF_JGT
, 1, 2U);
10302 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10303 0x3fU
, BPF_JGT
, 0, 0U);
10308 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10309 (cstate
->linktype
!= DLT_ERF
) &&
10310 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10311 bpf_error(cstate
, "'msu' supported only on MTP2");
10312 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10313 0x3fU
, BPF_JGT
, 0, 2U);
10317 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10318 (cstate
->linktype
!= DLT_ERF
) &&
10319 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10320 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10321 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10322 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10323 0xff80U
, BPF_JEQ
, 0, 0U);
10327 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10328 (cstate
->linktype
!= DLT_ERF
) &&
10329 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10330 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10331 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10332 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10333 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10334 0xff80U
, BPF_JGT
, 0, 0U);
10339 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10340 (cstate
->linktype
!= DLT_ERF
) &&
10341 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10342 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10343 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10344 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10354 * These maximum valid values are all-ones, so they double as the bitmasks
10355 * before any bitwise shifting.
10357 #define MTP2_SIO_MAXVAL UINT8_MAX
10358 #define MTP3_PC_MAXVAL 0x3fffU
10359 #define MTP3_SLS_MAXVAL 0xfU
10361 static struct block
*
10362 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10363 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10371 newoff_sio
= cstate
->off_sio
;
10372 newoff_opc
= cstate
->off_opc
;
10373 newoff_dpc
= cstate
->off_dpc
;
10374 newoff_sls
= cstate
->off_sls
;
10375 switch (mtp3field
) {
10378 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10380 * SIO is the simplest field: the size is one byte and the offset is a
10381 * multiple of bytes, so the only detail to get right is the value of
10382 * the [right-to-left] field offset.
10385 newoff_sio
+= 3; /* offset for MTP2_HSL */
10389 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10390 bpf_error(cstate
, "'sio' supported only on SS7");
10391 if(jvalue
> MTP2_SIO_MAXVAL
)
10392 bpf_error(cstate
, "sio value %u too big; max value = %u",
10393 jvalue
, MTP2_SIO_MAXVAL
);
10394 // Here the bitmask means "do not apply a bitmask".
10395 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10396 jtype
, reverse
, jvalue
);
10400 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10402 * SLS, OPC and DPC are more complicated: none of these is sized in a
10403 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10404 * diagrams are meant to be read right-to-left. This means in the
10405 * diagrams within individual fields and concatenations thereof
10406 * bitwise shifts and masks can be noted in the common left-to-right
10407 * manner until each final value is ready to be byte-swapped and
10408 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10409 * similar problem in a similar way.
10411 * Offsets of fields within the packet header always have the
10412 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10413 * DLTs the offset does not include the F (Flag) field at the
10414 * beginning of each message.
10416 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10417 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10418 * be tested entirely using a single BPF_W comparison. In this case
10419 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10420 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10421 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10422 * correlates with the [RTL] packet diagram until the byte-swapping is
10425 * The code below uses this approach for OPC, which spans 3 bytes.
10426 * DPC and SLS use shorter loads, SLS also uses a different offset.
10433 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10434 bpf_error(cstate
, "'opc' supported only on SS7");
10435 if (jvalue
> MTP3_PC_MAXVAL
)
10436 bpf_error(cstate
, "opc value %u too big; max value = %u",
10437 jvalue
, MTP3_PC_MAXVAL
);
10438 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10439 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10440 SWAPLONG(jvalue
<< 14));
10448 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10449 bpf_error(cstate
, "'dpc' supported only on SS7");
10450 if (jvalue
> MTP3_PC_MAXVAL
)
10451 bpf_error(cstate
, "dpc value %u too big; max value = %u",
10452 jvalue
, MTP3_PC_MAXVAL
);
10453 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10454 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10455 SWAPSHORT(jvalue
));
10463 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10464 bpf_error(cstate
, "'sls' supported only on SS7");
10465 if (jvalue
> MTP3_SLS_MAXVAL
)
10466 bpf_error(cstate
, "sls value %u too big; max value = %u",
10467 jvalue
, MTP3_SLS_MAXVAL
);
10468 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10469 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10480 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10481 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10484 * Catch errors reported by us and routines below us, and return NULL
10487 if (setjmp(cstate
->top_ctx
))
10490 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10494 static struct block
*
10495 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10500 * Q.2931 signalling protocol messages for handling virtual circuits
10501 * establishment and teardown
10506 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10509 case A_CALLPROCEED
:
10510 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10514 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10518 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10522 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10525 case A_RELEASE_DONE
:
10526 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10536 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10538 struct block
*b0
, *b1
;
10541 * Catch errors reported by us and routines below us, and return NULL
10544 if (setjmp(cstate
->top_ctx
))
10550 if (!cstate
->is_atm
)
10551 bpf_error(cstate
, "'oam' supported only on raw ATM");
10553 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10554 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10556 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10561 if (!cstate
->is_atm
)
10562 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10564 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10565 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10567 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10573 * Get Q.2931 signalling messages for switched
10574 * virtual connection
10576 if (!cstate
->is_atm
)
10577 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10578 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10579 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10581 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10583 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10585 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10587 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10589 b0
= gen_atmtype_sc(cstate
);
10593 case A_METACONNECT
:
10594 if (!cstate
->is_atm
)
10595 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10596 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10597 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10599 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10601 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10603 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10605 b0
= gen_atmtype_metac(cstate
);