2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <netinet/in.h>
43 #include "ethertype.h"
46 #include "ieee80211.h"
50 #include "pcap/ipnet.h"
51 #include "diag-control.h"
52 #include "pcap-util.h"
56 #if defined(__linux__)
57 #include <linux/types.h>
58 #include <linux/if_packet.h>
59 #include <linux/filter.h>
63 #ifdef HAVE_NPCAP_BPF_H
64 /* Defines BPF extensions for Npcap */
65 #include <npcap-bpf.h>
68 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
75 uint16_t u6_addr16
[8];
76 uint32_t u6_addr32
[4];
78 #define s6_addr in6_u.u6_addr8
79 #define s6_addr16 in6_u.u6_addr16
80 #define s6_addr32 in6_u.u6_addr32
81 #define s6_addr64 in6_u.u6_addr64
84 typedef unsigned short sa_family_t
;
86 #define __SOCKADDR_COMMON(sa_prefix) \
87 sa_family_t sa_prefix##family
89 /* Ditto, for IPv6. */
92 __SOCKADDR_COMMON (sin6_
);
93 uint16_t sin6_port
; /* Transport layer port # */
94 uint32_t sin6_flowinfo
; /* IPv6 flow information */
95 struct in6_addr sin6_addr
; /* IPv6 address */
98 #ifndef EAI_ADDRFAMILY
100 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
101 int ai_family
; /* PF_xxx */
102 int ai_socktype
; /* SOCK_xxx */
103 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
104 size_t ai_addrlen
; /* length of ai_addr */
105 char *ai_canonname
; /* canonical name for hostname */
106 struct sockaddr
*ai_addr
; /* binary address */
107 struct addrinfo
*ai_next
; /* next structure in linked list */
109 #endif /* EAI_ADDRFAMILY */
110 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
113 #include <netdb.h> /* for "struct addrinfo" */
115 #include <pcap/namedb.h>
117 #include "nametoaddr.h"
119 #define ETHERMTU 1500
121 #ifndef IPPROTO_HOPOPTS
122 #define IPPROTO_HOPOPTS 0
124 #ifndef IPPROTO_ROUTING
125 #define IPPROTO_ROUTING 43
127 #ifndef IPPROTO_FRAGMENT
128 #define IPPROTO_FRAGMENT 44
130 #ifndef IPPROTO_DSTOPTS
131 #define IPPROTO_DSTOPTS 60
134 #define IPPROTO_SCTP 132
137 #define GENEVE_PORT 6081
138 #define VXLAN_PORT 4789
142 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
146 #define ARCTYPE_IP_OLD 240 /* IP protocol */
147 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
150 #define ARCTYPE_IP 212 /* IP protocol */
151 #define ARCTYPE_ARP 213 /* address resolution protocol */
152 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
154 #define ARCTYPE_ATALK 221 /* Appletalk */
155 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
156 #define ARCTYPE_IPX 250 /* Novell IPX */
158 #define ARCTYPE_INET6 0xc4 /* IPng */
159 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
162 /* Based on UNI3.1 standard by ATM Forum */
164 /* ATM traffic types based on VPI=0 and (the following VCI */
165 #define VCI_PPC 0x05 /* Point-to-point signal msg */
166 #define VCI_BCC 0x02 /* Broadcast signal msg */
167 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
168 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
169 #define VCI_METAC 0x01 /* Meta signal msg */
170 #define VCI_ILMIC 0x10 /* ILMI msg */
172 /* Q.2931 signalling messages */
173 #define CALL_PROCEED 0x02 /* call proceeding */
174 #define CONNECT 0x07 /* connect */
175 #define CONNECT_ACK 0x0f /* connect_ack */
176 #define SETUP 0x05 /* setup */
177 #define RELEASE 0x4d /* release */
178 #define RELEASE_DONE 0x5a /* release_done */
179 #define RESTART 0x46 /* restart */
180 #define RESTART_ACK 0x4e /* restart ack */
181 #define STATUS 0x7d /* status */
182 #define STATUS_ENQ 0x75 /* status ack */
183 #define ADD_PARTY 0x80 /* add party */
184 #define ADD_PARTY_ACK 0x81 /* add party ack */
185 #define ADD_PARTY_REJ 0x82 /* add party rej */
186 #define DROP_PARTY 0x83 /* drop party */
187 #define DROP_PARTY_ACK 0x84 /* drop party ack */
189 /* Information Element Parameters in the signalling messages */
190 #define CAUSE 0x08 /* cause */
191 #define ENDPT_REF 0x54 /* endpoint reference */
192 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
193 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
194 #define CONNECT_ID 0x5a /* connection identifier */
195 #define QOS_PARA 0x5c /* quality of service parameters */
196 #define B_HIGHER 0x5d /* broadband higher layer information */
197 #define B_BEARER 0x5e /* broadband bearer capability */
198 #define B_LOWER 0x5f /* broadband lower information */
199 #define CALLING_PARTY 0x6c /* calling party number */
200 #define CALLED_PARTY 0x70 /* called party number */
204 /* Q.2931 signalling general messages format */
205 #define PROTO_POS 0 /* offset of protocol discriminator */
206 #define CALL_REF_POS 2 /* offset of call reference value */
207 #define MSG_TYPE_POS 5 /* offset of message type */
208 #define MSG_LEN_POS 7 /* offset of message length */
209 #define IE_BEGIN_POS 9 /* offset of first information element */
211 /* format of signalling messages */
214 #define FIELD_BEGIN_POS 4
217 /* SunATM header for ATM packet */
218 #define SUNATM_DIR_POS 0
219 #define SUNATM_VPI_POS 1
220 #define SUNATM_VCI_POS 2
221 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
223 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
224 #define PT_LANE 0x01 /* LANE */
225 #define PT_LLC 0x02 /* LLC encapsulation */
226 #define PT_ILMI 0x05 /* ILMI */
227 #define PT_QSAAL 0x06 /* Q.SAAL */
230 /* Types missing from some systems */
233 * Network layer protocol identifiers
236 #define ISO8473_CLNP 0x81
239 #define ISO9542_ESIS 0x82
241 #ifndef ISO9542X25_ESIS
242 #define ISO9542X25_ESIS 0x8a
244 #ifndef ISO10589_ISIS
245 #define ISO10589_ISIS 0x83
248 #define ISIS_L1_LAN_IIH 15
249 #define ISIS_L2_LAN_IIH 16
250 #define ISIS_PTP_IIH 17
251 #define ISIS_L1_LSP 18
252 #define ISIS_L2_LSP 20
253 #define ISIS_L1_CSNP 24
254 #define ISIS_L2_CSNP 25
255 #define ISIS_L1_PSNP 26
256 #define ISIS_L2_PSNP 27
258 #ifndef ISO8878A_CONS
259 #define ISO8878A_CONS 0x84
261 #ifndef ISO10747_IDRP
262 #define ISO10747_IDRP 0x85
265 // Same as in tcpdump/print-sl.c.
267 #define SLIPDIR_OUT 1
269 #ifdef HAVE_OS_PROTO_H
270 #include "os-proto.h"
273 #define JMP(c) ((c)|BPF_JMP|BPF_K)
276 * "Push" the current value of the link-layer header type and link-layer
277 * header offset onto a "stack", and set a new value. (It's not a
278 * full-blown stack; we keep only the top two items.)
280 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
282 (cs)->prevlinktype = (cs)->linktype; \
283 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
284 (cs)->linktype = (new_linktype); \
285 (cs)->off_linkhdr.is_variable = (new_is_variable); \
286 (cs)->off_linkhdr.constant_part = (new_constant_part); \
287 (cs)->off_linkhdr.reg = (new_reg); \
288 (cs)->is_encap = 0; \
292 * Offset "not set" value.
294 #define OFFSET_NOT_SET 0xffffffffU
297 * Absolute offsets, which are offsets from the beginning of the raw
298 * packet data, are, in the general case, the sum of a variable value
299 * and a constant value; the variable value may be absent, in which
300 * case the offset is only the constant value, and the constant value
301 * may be zero, in which case the offset is only the variable value.
303 * bpf_abs_offset is a structure containing all that information:
305 * is_variable is 1 if there's a variable part.
307 * constant_part is the constant part of the value, possibly zero;
309 * if is_variable is 1, reg is the register number for a register
310 * containing the variable value if the register has been assigned,
320 * Value passed to gen_load_a() to indicate what the offset argument
321 * is relative to the beginning of.
324 OR_PACKET
, /* full packet data */
325 OR_LINKHDR
, /* link-layer header */
326 OR_PREVLINKHDR
, /* previous link-layer header */
327 OR_LLC
, /* 802.2 LLC header */
328 OR_PREVMPLSHDR
, /* previous MPLS header */
329 OR_LINKTYPE
, /* link-layer type */
330 OR_LINKPL
, /* link-layer payload */
331 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
332 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
333 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
337 * We divvy out chunks of memory rather than call malloc each time so
338 * we don't have to worry about leaking memory. It's probably
339 * not a big deal if all this memory was wasted but if this ever
340 * goes into a library that would probably not be a good idea.
342 * XXX - this *is* in a library....
345 #define CHUNK0SIZE 1024
352 * A chunk can store any of:
353 * - a string (guaranteed alignment 1 but present for completeness)
357 * For this simple allocator every allocated chunk gets rounded up to the
358 * alignment needed for any chunk.
369 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
371 /* Code generator state */
373 struct _compiler_state
{
384 int outermostlinktype
;
389 /* Hack for handling VLAN and MPLS stacks. */
390 u_int label_stack_depth
;
391 u_int vlan_stack_depth
;
397 * As errors are handled by a longjmp, anything allocated must
398 * be freed in the longjmp handler, so it must be reachable
401 * One thing that's allocated is the result of pcap_nametoaddrinfo();
402 * it must be freed with freeaddrinfo(). This variable points to
403 * any addrinfo structure that would need to be freed.
408 * Another thing that's allocated is the result of pcap_ether_aton();
409 * it must be freed with free(). This variable points to any
410 * address that would need to be freed.
415 * Various code constructs need to know the layout of the packet.
416 * These values give the necessary offsets from the beginning
417 * of the packet data.
421 * Absolute offset of the beginning of the link-layer header.
423 bpf_abs_offset off_linkhdr
;
426 * If we're checking a link-layer header for a packet encapsulated
427 * in another protocol layer, this is the equivalent information
428 * for the previous layers' link-layer header from the beginning
429 * of the raw packet data.
431 bpf_abs_offset off_prevlinkhdr
;
434 * This is the equivalent information for the outermost layers'
437 bpf_abs_offset off_outermostlinkhdr
;
440 * Absolute offset of the beginning of the link-layer payload.
442 bpf_abs_offset off_linkpl
;
445 * "off_linktype" is the offset to information in the link-layer
446 * header giving the packet type. This is an absolute offset
447 * from the beginning of the packet.
449 * For Ethernet, it's the offset of the Ethernet type field; this
450 * means that it must have a value that skips VLAN tags.
452 * For link-layer types that always use 802.2 headers, it's the
453 * offset of the LLC header; this means that it must have a value
454 * that skips VLAN tags.
456 * For PPP, it's the offset of the PPP type field.
458 * For Cisco HDLC, it's the offset of the CHDLC type field.
460 * For BSD loopback, it's the offset of the AF_ value.
462 * For Linux cooked sockets, it's the offset of the type field.
464 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
465 * encapsulation, in which case, IP is assumed.
467 bpf_abs_offset off_linktype
;
470 * TRUE if the link layer includes an ATM pseudo-header.
474 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
475 * causes us to generate code that checks for a Geneve or
476 * VXLAN header respectively and assume that later filters
477 * apply to the encapsulated payload.
482 * TRUE if we need variable length part of VLAN offset
484 int is_vlan_vloffset
;
487 * These are offsets for the ATM pseudo-header.
494 * These are offsets for the MTP2 fields.
500 * These are offsets for the MTP3 fields.
508 * This is the offset of the first byte after the ATM pseudo_header,
509 * or -1 if there is no ATM pseudo-header.
514 * These are offsets to the beginning of the network-layer header.
515 * They are relative to the beginning of the link-layer payload
516 * (i.e., they don't include off_linkhdr.constant_part or
517 * off_linkpl.constant_part).
519 * If the link layer never uses 802.2 LLC:
521 * "off_nl" and "off_nl_nosnap" are the same.
523 * If the link layer always uses 802.2 LLC:
525 * "off_nl" is the offset if there's a SNAP header following
528 * "off_nl_nosnap" is the offset if there's no SNAP header.
530 * If the link layer is Ethernet:
532 * "off_nl" is the offset if the packet is an Ethernet II packet
533 * (we assume no 802.3+802.2+SNAP);
535 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
536 * with an 802.2 header following it.
542 * Here we handle simple allocation of the scratch registers.
543 * If too many registers are alloc'd, the allocator punts.
545 int regused
[BPF_MEMWORDS
];
551 struct chunk chunks
[NCHUNKS
];
556 * For use by routines outside this file.
560 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
565 * If we've already set an error, don't override it.
566 * The lexical analyzer reports some errors by setting
567 * the error and then returning a LEX_ERROR token, which
568 * is not recognized by any grammar rule, and thus forces
569 * the parse to stop. We don't want the error reported
570 * by the lexical analyzer to be overwritten by the syntax
573 if (!cstate
->error_set
) {
575 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
578 cstate
->error_set
= 1;
583 * For use *ONLY* in routines in this file.
585 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
586 PCAP_PRINTFLIKE(2, 3);
589 static void PCAP_NORETURN
590 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
595 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
598 longjmp(cstate
->top_ctx
, 1);
605 static int init_linktype(compiler_state_t
*, pcap_t
*);
607 static void init_regs(compiler_state_t
*);
608 static int alloc_reg(compiler_state_t
*);
609 static void free_reg(compiler_state_t
*, int);
611 static void initchunks(compiler_state_t
*cstate
);
612 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
613 static void *newchunk(compiler_state_t
*cstate
, size_t);
614 static void freechunks(compiler_state_t
*cstate
);
615 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
616 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
617 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
618 static inline void syntax(compiler_state_t
*cstate
);
620 static void backpatch(struct block
*, struct block
*);
621 static void merge(struct block
*, struct block
*);
622 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
624 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
626 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
628 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
630 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
632 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
633 u_int
, bpf_u_int32
, bpf_u_int32
);
634 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
635 u_int
, const u_char
*);
636 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
637 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
638 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
640 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
642 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
643 static struct block
*gen_uncond(compiler_state_t
*, int);
644 static inline struct block
*gen_true(compiler_state_t
*);
645 static inline struct block
*gen_false(compiler_state_t
*);
646 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
647 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
648 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
649 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
650 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
651 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
652 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
653 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
654 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
655 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
657 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
658 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
659 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
660 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
661 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
664 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
665 struct in6_addr
*, int, u_int
, u_int
);
667 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
668 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
669 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
670 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
671 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
672 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
673 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
674 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
675 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
678 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
679 struct in6_addr
*, int, int, int);
682 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
683 struct addrinfo
*, int, int);
685 static struct block
*gen_ipfrag(compiler_state_t
*);
686 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
687 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
689 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
690 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
692 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
693 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
694 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
696 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
697 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
698 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
699 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
701 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
702 static int lookup_proto(compiler_state_t
*, const char *, int);
703 #if !defined(NO_PROTOCHAIN)
704 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
705 #endif /* !defined(NO_PROTOCHAIN) */
706 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
707 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
708 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
709 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
710 static struct block
*gen_len(compiler_state_t
*, int, int);
711 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
713 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
714 bpf_u_int32
, int, int);
715 static struct block
*gen_atmtype_llc(compiler_state_t
*);
716 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
719 initchunks(compiler_state_t
*cstate
)
723 for (i
= 0; i
< NCHUNKS
; i
++) {
724 cstate
->chunks
[i
].n_left
= 0;
725 cstate
->chunks
[i
].m
= NULL
;
727 cstate
->cur_chunk
= 0;
731 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
737 /* Round up to chunk alignment. */
738 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
740 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
741 if (n
> cp
->n_left
) {
743 k
= ++cstate
->cur_chunk
;
745 bpf_set_error(cstate
, "out of memory");
748 size
= CHUNK0SIZE
<< k
;
749 cp
->m
= (void *)malloc(size
);
751 bpf_set_error(cstate
, "out of memory");
754 memset((char *)cp
->m
, 0, size
);
757 bpf_set_error(cstate
, "out of memory");
762 return (void *)((char *)cp
->m
+ cp
->n_left
);
766 newchunk(compiler_state_t
*cstate
, size_t n
)
770 p
= newchunk_nolongjmp(cstate
, n
);
772 longjmp(cstate
->top_ctx
, 1);
779 freechunks(compiler_state_t
*cstate
)
783 for (i
= 0; i
< NCHUNKS
; ++i
)
784 if (cstate
->chunks
[i
].m
!= NULL
)
785 free(cstate
->chunks
[i
].m
);
789 * A strdup whose allocations are freed after code generation is over.
790 * This is used by the lexical analyzer, so it can't longjmp; it just
791 * returns NULL on an allocation error, and the callers must check
795 sdup(compiler_state_t
*cstate
, const char *s
)
797 size_t n
= strlen(s
) + 1;
798 char *cp
= newchunk_nolongjmp(cstate
, n
);
802 pcapint_strlcpy(cp
, s
, n
);
806 static inline struct block
*
807 new_block(compiler_state_t
*cstate
, int code
)
811 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
818 static inline struct slist
*
819 new_stmt(compiler_state_t
*cstate
, int code
)
823 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
829 static struct block
*
830 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
832 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
838 static struct block
*
839 gen_retblk(compiler_state_t
*cstate
, int v
)
841 if (setjmp(cstate
->top_ctx
)) {
843 * gen_retblk() only fails because a memory
844 * allocation failed in newchunk(), meaning
845 * that it can't return a pointer.
851 return gen_retblk_internal(cstate
, v
);
854 static inline PCAP_NORETURN_DEF
void
855 syntax(compiler_state_t
*cstate
)
857 bpf_error(cstate
, "syntax error in filter expression");
861 * For the given integer return a string with the keyword (or the nominal
862 * keyword if there is more than one). This is a simpler version of tok2str()
863 * in tcpdump because in this problem space a valid integer value is not
867 qual2kw(const char *kind
, const unsigned id
, const char *tokens
[],
870 static char buf
[4][64];
873 if (id
< size
&& tokens
[id
])
876 char *ret
= buf
[idx
];
877 idx
= (idx
+ 1) % (sizeof(buf
) / sizeof(buf
[0]));
878 ret
[0] = '\0'; // just in case
879 snprintf(ret
, sizeof(buf
[0]), "<invalid %s %u>", kind
, id
);
883 // direction qualifier keywords
885 dqkw(const unsigned id
)
887 const char * map
[] = {
890 [Q_OR
] = "src or dst",
891 [Q_AND
] = "src and dst",
899 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
902 static PCAP_NORETURN_DEF
void
903 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
905 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
906 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
910 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
912 if (cstate
->linktype
!= DLT_PFLOG
)
913 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
916 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
919 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
920 const char *buf
, int optimize
, bpf_u_int32 mask
)
926 compiler_state_t cstate
;
927 yyscan_t scanner
= NULL
;
928 YY_BUFFER_STATE in_buffer
= NULL
;
933 * If this pcap_t hasn't been activated, it doesn't have a
934 * link-layer type, so we can't use it.
937 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
938 "not-yet-activated pcap_t passed to pcap_compile");
944 * Initialize Winsock, asking for the latest version (2.2),
945 * as we may be calling Winsock routines to translate
946 * host names to addresses.
948 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
950 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
951 err
, "Error calling WSAStartup()");
958 * If the device on which we're capturing need to be notified
959 * that a new filter is being compiled, do so.
961 * This allows them to save a copy of it, in case, for example,
962 * they're implementing a form of remote packet capture, and
963 * want the remote machine to filter out the packets in which
964 * it's sending the packets it's captured.
966 * XXX - the fact that we happen to be compiling a filter
967 * doesn't necessarily mean we'll be installing it as the
968 * filter for this pcap_t; we might be running it from userland
969 * on captured packets to do packet classification. We really
970 * need a better way of handling this, but this is all that
971 * the WinPcap remote capture code did.
973 if (p
->save_current_filter_op
!= NULL
)
974 (p
->save_current_filter_op
)(p
, buf
);
978 cstate
.no_optimize
= 0;
983 cstate
.ic
.root
= NULL
;
984 cstate
.ic
.cur_mark
= 0;
986 cstate
.error_set
= 0;
989 cstate
.netmask
= mask
;
991 cstate
.snaplen
= pcap_snapshot(p
);
992 if (cstate
.snaplen
== 0) {
993 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
994 "snaplen of 0 rejects all packets");
999 if (pcap_lex_init(&scanner
) != 0) {
1000 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1001 errno
, "can't initialize scanner");
1005 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1008 * Associate the compiler state with the lexical analyzer
1011 pcap_set_extra(&cstate
, scanner
);
1013 if (init_linktype(&cstate
, p
) == -1) {
1017 if (pcap_parse(scanner
, &cstate
) != 0) {
1019 if (cstate
.ai
!= NULL
)
1020 freeaddrinfo(cstate
.ai
);
1022 if (cstate
.e
!= NULL
)
1028 if (cstate
.ic
.root
== NULL
) {
1029 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1032 * Catch errors reported by gen_retblk().
1034 if (cstate
.ic
.root
== NULL
) {
1040 if (optimize
&& !cstate
.no_optimize
) {
1041 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1046 if (cstate
.ic
.root
== NULL
||
1047 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1048 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1049 "expression rejects all packets");
1054 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1055 cstate
.ic
.root
, &len
, p
->errbuf
);
1056 if (program
->bf_insns
== NULL
) {
1061 program
->bf_len
= len
;
1063 rc
= 0; /* We're all okay */
1067 * Clean up everything for the lexical analyzer.
1069 if (in_buffer
!= NULL
)
1070 pcap__delete_buffer(in_buffer
, scanner
);
1071 if (scanner
!= NULL
)
1072 pcap_lex_destroy(scanner
);
1075 * Clean up our own allocated memory.
1077 freechunks(&cstate
);
1087 * entry point for using the compiler with no pcap open
1088 * pass in all the stuff that is needed explicitly instead.
1091 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1092 struct bpf_program
*program
,
1093 const char *buf
, int optimize
, bpf_u_int32 mask
)
1098 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1100 return (PCAP_ERROR
);
1101 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1107 * Clean up a "struct bpf_program" by freeing all the memory allocated
1111 pcap_freecode(struct bpf_program
*program
)
1113 program
->bf_len
= 0;
1114 if (program
->bf_insns
!= NULL
) {
1115 free((char *)program
->bf_insns
);
1116 program
->bf_insns
= NULL
;
1121 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1122 * which of the jt and jf fields has been resolved and which is a pointer
1123 * back to another unresolved block (or nil). At least one of the fields
1124 * in each block is already resolved.
1127 backpatch(struct block
*list
, struct block
*target
)
1144 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1145 * which of jt and jf is the link.
1148 merge(struct block
*b0
, struct block
*b1
)
1150 register struct block
**p
= &b0
;
1152 /* Find end of list. */
1154 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1156 /* Concatenate the lists. */
1161 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1164 * Catch errors reported by us and routines below us, and return -1
1167 if (setjmp(cstate
->top_ctx
))
1171 * Insert before the statements of the first (root) block any
1172 * statements needed to load the lengths of any variable-length
1173 * headers into registers.
1175 * XXX - a fancier strategy would be to insert those before the
1176 * statements of all blocks that use those lengths and that
1177 * have no predecessors that use them, so that we only compute
1178 * the lengths if we need them. There might be even better
1179 * approaches than that.
1181 * However, those strategies would be more complicated, and
1182 * as we don't generate code to compute a length if the
1183 * program has no tests that use the length, and as most
1184 * tests will probably use those lengths, we would just
1185 * postpone computing the lengths so that it's not done
1186 * for tests that fail early, and it's not clear that's
1189 insert_compute_vloffsets(cstate
, p
->head
);
1192 * For DLT_PPI captures, generate a check of the per-packet
1193 * DLT value to make sure it's DLT_IEEE802_11.
1195 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1196 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1197 * with appropriate Ethernet information and use that rather
1198 * than using something such as DLT_PPI where you don't know
1199 * the link-layer header type until runtime, which, in the
1200 * general case, would force us to generate both Ethernet *and*
1201 * 802.11 code (*and* anything else for which PPI is used)
1202 * and choose between them early in the BPF program?
1204 if (cstate
->linktype
== DLT_PPI
) {
1205 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1206 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1207 gen_and(ppi_dlt_check
, p
);
1210 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1211 p
->sense
= !p
->sense
;
1212 backpatch(p
, gen_retblk_internal(cstate
, 0));
1213 cstate
->ic
.root
= p
->head
;
1218 gen_and(struct block
*b0
, struct block
*b1
)
1220 backpatch(b0
, b1
->head
);
1221 b0
->sense
= !b0
->sense
;
1222 b1
->sense
= !b1
->sense
;
1224 b1
->sense
= !b1
->sense
;
1225 b1
->head
= b0
->head
;
1229 gen_or(struct block
*b0
, struct block
*b1
)
1231 b0
->sense
= !b0
->sense
;
1232 backpatch(b0
, b1
->head
);
1233 b0
->sense
= !b0
->sense
;
1235 b1
->head
= b0
->head
;
1239 gen_not(struct block
*b
)
1241 b
->sense
= !b
->sense
;
1244 static struct block
*
1245 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1246 u_int size
, bpf_u_int32 v
)
1248 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1251 static struct block
*
1252 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1253 u_int size
, bpf_u_int32 v
)
1255 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1258 static struct block
*
1259 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1260 u_int size
, bpf_u_int32 v
)
1262 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1265 static struct block
*
1266 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1267 u_int size
, bpf_u_int32 v
)
1269 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1272 static struct block
*
1273 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1274 u_int size
, bpf_u_int32 v
)
1276 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1279 static struct block
*
1280 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1281 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1283 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1286 static struct block
*
1287 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1288 u_int size
, const u_char
*v
)
1290 register struct block
*b
, *tmp
;
1294 register const u_char
*p
= &v
[size
- 4];
1296 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1304 register const u_char
*p
= &v
[size
- 2];
1306 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1314 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1323 * AND the field of size "size" at offset "offset" relative to the header
1324 * specified by "offrel" with "mask", and compare it with the value "v"
1325 * with the test specified by "jtype"; if "reverse" is true, the test
1326 * should test the opposite of "jtype".
1328 static struct block
*
1329 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1330 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1333 struct slist
*s
, *s2
;
1336 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1338 if (mask
!= 0xffffffff) {
1339 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1344 b
= new_block(cstate
, JMP(jtype
));
1353 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1355 cstate
->pcap_fddipad
= p
->fddipad
;
1358 * We start out with only one link-layer header.
1360 cstate
->outermostlinktype
= pcap_datalink(p
);
1361 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1362 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1363 cstate
->off_outermostlinkhdr
.reg
= -1;
1365 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1366 cstate
->off_prevlinkhdr
.constant_part
= 0;
1367 cstate
->off_prevlinkhdr
.is_variable
= 0;
1368 cstate
->off_prevlinkhdr
.reg
= -1;
1370 cstate
->linktype
= cstate
->outermostlinktype
;
1371 cstate
->off_linkhdr
.constant_part
= 0;
1372 cstate
->off_linkhdr
.is_variable
= 0;
1373 cstate
->off_linkhdr
.reg
= -1;
1378 cstate
->off_linkpl
.constant_part
= 0;
1379 cstate
->off_linkpl
.is_variable
= 0;
1380 cstate
->off_linkpl
.reg
= -1;
1382 cstate
->off_linktype
.constant_part
= 0;
1383 cstate
->off_linktype
.is_variable
= 0;
1384 cstate
->off_linktype
.reg
= -1;
1387 * Assume it's not raw ATM with a pseudo-header, for now.
1390 cstate
->off_vpi
= OFFSET_NOT_SET
;
1391 cstate
->off_vci
= OFFSET_NOT_SET
;
1392 cstate
->off_proto
= OFFSET_NOT_SET
;
1393 cstate
->off_payload
= OFFSET_NOT_SET
;
1396 * And not encapsulated with either Geneve or VXLAN.
1398 cstate
->is_encap
= 0;
1401 * No variable length VLAN offset by default
1403 cstate
->is_vlan_vloffset
= 0;
1406 * And assume we're not doing SS7.
1408 cstate
->off_li
= OFFSET_NOT_SET
;
1409 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1410 cstate
->off_sio
= OFFSET_NOT_SET
;
1411 cstate
->off_opc
= OFFSET_NOT_SET
;
1412 cstate
->off_dpc
= OFFSET_NOT_SET
;
1413 cstate
->off_sls
= OFFSET_NOT_SET
;
1415 cstate
->label_stack_depth
= 0;
1416 cstate
->vlan_stack_depth
= 0;
1418 switch (cstate
->linktype
) {
1421 cstate
->off_linktype
.constant_part
= 2;
1422 cstate
->off_linkpl
.constant_part
= 6;
1423 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1424 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1427 case DLT_ARCNET_LINUX
:
1428 cstate
->off_linktype
.constant_part
= 4;
1429 cstate
->off_linkpl
.constant_part
= 8;
1430 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1431 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1435 cstate
->off_linktype
.constant_part
= 12;
1436 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1437 cstate
->off_nl
= 0; /* Ethernet II */
1438 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1443 * SLIP doesn't have a link level type. The 16 byte
1444 * header is hacked into our SLIP driver.
1446 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1447 cstate
->off_linkpl
.constant_part
= 16;
1449 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1452 case DLT_SLIP_BSDOS
:
1453 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1454 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1456 cstate
->off_linkpl
.constant_part
= 24;
1458 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1463 cstate
->off_linktype
.constant_part
= 0;
1464 cstate
->off_linkpl
.constant_part
= 4;
1466 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1470 cstate
->off_linktype
.constant_part
= 0;
1471 cstate
->off_linkpl
.constant_part
= 12;
1473 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1478 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1479 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1480 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1481 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1482 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1484 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1489 * This does not include the Ethernet header, and
1490 * only covers session state.
1492 cstate
->off_linktype
.constant_part
= 6;
1493 cstate
->off_linkpl
.constant_part
= 8;
1495 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1499 cstate
->off_linktype
.constant_part
= 5;
1500 cstate
->off_linkpl
.constant_part
= 24;
1502 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1507 * FDDI doesn't really have a link-level type field.
1508 * We set "off_linktype" to the offset of the LLC header.
1510 * To check for Ethernet types, we assume that SSAP = SNAP
1511 * is being used and pick out the encapsulated Ethernet type.
1512 * XXX - should we generate code to check for SNAP?
1514 cstate
->off_linktype
.constant_part
= 13;
1515 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1516 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1517 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1518 cstate
->off_nl
= 8; /* 802.2+SNAP */
1519 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1524 * Token Ring doesn't really have a link-level type field.
1525 * We set "off_linktype" to the offset of the LLC header.
1527 * To check for Ethernet types, we assume that SSAP = SNAP
1528 * is being used and pick out the encapsulated Ethernet type.
1529 * XXX - should we generate code to check for SNAP?
1531 * XXX - the header is actually variable-length.
1532 * Some various Linux patched versions gave 38
1533 * as "off_linktype" and 40 as "off_nl"; however,
1534 * if a token ring packet has *no* routing
1535 * information, i.e. is not source-routed, the correct
1536 * values are 20 and 22, as they are in the vanilla code.
1538 * A packet is source-routed iff the uppermost bit
1539 * of the first byte of the source address, at an
1540 * offset of 8, has the uppermost bit set. If the
1541 * packet is source-routed, the total number of bytes
1542 * of routing information is 2 plus bits 0x1F00 of
1543 * the 16-bit value at an offset of 14 (shifted right
1544 * 8 - figure out which byte that is).
1546 cstate
->off_linktype
.constant_part
= 14;
1547 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1548 cstate
->off_nl
= 8; /* 802.2+SNAP */
1549 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1552 case DLT_PRISM_HEADER
:
1553 case DLT_IEEE802_11_RADIO_AVS
:
1554 case DLT_IEEE802_11_RADIO
:
1555 cstate
->off_linkhdr
.is_variable
= 1;
1556 /* Fall through, 802.11 doesn't have a variable link
1557 * prefix but is otherwise the same. */
1560 case DLT_IEEE802_11
:
1562 * 802.11 doesn't really have a link-level type field.
1563 * We set "off_linktype.constant_part" to the offset of
1566 * To check for Ethernet types, we assume that SSAP = SNAP
1567 * is being used and pick out the encapsulated Ethernet type.
1568 * XXX - should we generate code to check for SNAP?
1570 * We also handle variable-length radio headers here.
1571 * The Prism header is in theory variable-length, but in
1572 * practice it's always 144 bytes long. However, some
1573 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1574 * sometimes or always supply an AVS header, so we
1575 * have to check whether the radio header is a Prism
1576 * header or an AVS header, so, in practice, it's
1579 cstate
->off_linktype
.constant_part
= 24;
1580 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1581 cstate
->off_linkpl
.is_variable
= 1;
1582 cstate
->off_nl
= 8; /* 802.2+SNAP */
1583 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1588 * At the moment we treat PPI the same way that we treat
1589 * normal Radiotap encoded packets. The difference is in
1590 * the function that generates the code at the beginning
1591 * to compute the header length. Since this code generator
1592 * of PPI supports bare 802.11 encapsulation only (i.e.
1593 * the encapsulated DLT should be DLT_IEEE802_11) we
1594 * generate code to check for this too.
1596 cstate
->off_linktype
.constant_part
= 24;
1597 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1598 cstate
->off_linkpl
.is_variable
= 1;
1599 cstate
->off_linkhdr
.is_variable
= 1;
1600 cstate
->off_nl
= 8; /* 802.2+SNAP */
1601 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1604 case DLT_ATM_RFC1483
:
1605 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1607 * assume routed, non-ISO PDUs
1608 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1610 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1611 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1612 * latter would presumably be treated the way PPPoE
1613 * should be, so you can do "pppoe and udp port 2049"
1614 * or "pppoa and tcp port 80" and have it check for
1615 * PPPo{A,E} and a PPP protocol of IP and....
1617 cstate
->off_linktype
.constant_part
= 0;
1618 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1619 cstate
->off_nl
= 8; /* 802.2+SNAP */
1620 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1625 * Full Frontal ATM; you get AALn PDUs with an ATM
1629 cstate
->off_vpi
= SUNATM_VPI_POS
;
1630 cstate
->off_vci
= SUNATM_VCI_POS
;
1631 cstate
->off_proto
= PROTO_POS
;
1632 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1633 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1634 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1635 cstate
->off_nl
= 8; /* 802.2+SNAP */
1636 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1642 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1643 cstate
->off_linkpl
.constant_part
= 0;
1645 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1648 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1649 cstate
->off_linktype
.constant_part
= 14;
1650 cstate
->off_linkpl
.constant_part
= 16;
1652 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1655 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1656 cstate
->off_linktype
.constant_part
= 0;
1657 cstate
->off_linkpl
.constant_part
= 20;
1659 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1664 * LocalTalk does have a 1-byte type field in the LLAP header,
1665 * but really it just indicates whether there is a "short" or
1666 * "long" DDP packet following.
1668 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1669 cstate
->off_linkpl
.constant_part
= 0;
1671 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1674 case DLT_IP_OVER_FC
:
1676 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1677 * link-level type field. We set "off_linktype" to the
1678 * offset of the LLC header.
1680 * To check for Ethernet types, we assume that SSAP = SNAP
1681 * is being used and pick out the encapsulated Ethernet type.
1682 * XXX - should we generate code to check for SNAP? RFC
1683 * 2625 says SNAP should be used.
1685 cstate
->off_linktype
.constant_part
= 16;
1686 cstate
->off_linkpl
.constant_part
= 16;
1687 cstate
->off_nl
= 8; /* 802.2+SNAP */
1688 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1693 * XXX - we should set this to handle SNAP-encapsulated
1694 * frames (NLPID of 0x80).
1696 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1697 cstate
->off_linkpl
.constant_part
= 0;
1699 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1703 * the only BPF-interesting FRF.16 frames are non-control frames;
1704 * Frame Relay has a variable length link-layer
1705 * so lets start with offset 4 for now and increments later on (FIXME);
1708 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1709 cstate
->off_linkpl
.constant_part
= 0;
1711 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1714 case DLT_APPLE_IP_OVER_IEEE1394
:
1715 cstate
->off_linktype
.constant_part
= 16;
1716 cstate
->off_linkpl
.constant_part
= 18;
1718 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1721 case DLT_SYMANTEC_FIREWALL
:
1722 cstate
->off_linktype
.constant_part
= 6;
1723 cstate
->off_linkpl
.constant_part
= 44;
1724 cstate
->off_nl
= 0; /* Ethernet II */
1725 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1729 cstate
->off_linktype
.constant_part
= 0;
1730 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1731 cstate
->off_linkpl
.is_variable
= 1;
1733 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1736 case DLT_JUNIPER_MFR
:
1737 case DLT_JUNIPER_MLFR
:
1738 case DLT_JUNIPER_MLPPP
:
1739 case DLT_JUNIPER_PPP
:
1740 case DLT_JUNIPER_CHDLC
:
1741 case DLT_JUNIPER_FRELAY
:
1742 cstate
->off_linktype
.constant_part
= 4;
1743 cstate
->off_linkpl
.constant_part
= 4;
1745 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1748 case DLT_JUNIPER_ATM1
:
1749 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1750 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1752 cstate
->off_nl_nosnap
= 10;
1755 case DLT_JUNIPER_ATM2
:
1756 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1757 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1759 cstate
->off_nl_nosnap
= 10;
1762 /* frames captured on a Juniper PPPoE service PIC
1763 * contain raw ethernet frames */
1764 case DLT_JUNIPER_PPPOE
:
1765 case DLT_JUNIPER_ETHER
:
1766 cstate
->off_linkpl
.constant_part
= 14;
1767 cstate
->off_linktype
.constant_part
= 16;
1768 cstate
->off_nl
= 18; /* Ethernet II */
1769 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1772 case DLT_JUNIPER_PPPOE_ATM
:
1773 cstate
->off_linktype
.constant_part
= 4;
1774 cstate
->off_linkpl
.constant_part
= 6;
1776 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1779 case DLT_JUNIPER_GGSN
:
1780 cstate
->off_linktype
.constant_part
= 6;
1781 cstate
->off_linkpl
.constant_part
= 12;
1783 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1786 case DLT_JUNIPER_ES
:
1787 cstate
->off_linktype
.constant_part
= 6;
1788 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1789 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1790 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1793 case DLT_JUNIPER_MONITOR
:
1794 cstate
->off_linktype
.constant_part
= 12;
1795 cstate
->off_linkpl
.constant_part
= 12;
1796 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1797 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1800 case DLT_BACNET_MS_TP
:
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_JUNIPER_SERVICES
:
1808 cstate
->off_linktype
.constant_part
= 12;
1809 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1810 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1811 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1814 case DLT_JUNIPER_VP
:
1815 cstate
->off_linktype
.constant_part
= 18;
1816 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1817 cstate
->off_nl
= OFFSET_NOT_SET
;
1818 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1821 case DLT_JUNIPER_ST
:
1822 cstate
->off_linktype
.constant_part
= 18;
1823 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1824 cstate
->off_nl
= OFFSET_NOT_SET
;
1825 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1828 case DLT_JUNIPER_ISM
:
1829 cstate
->off_linktype
.constant_part
= 8;
1830 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1831 cstate
->off_nl
= OFFSET_NOT_SET
;
1832 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1835 case DLT_JUNIPER_VS
:
1836 case DLT_JUNIPER_SRX_E2E
:
1837 case DLT_JUNIPER_FIBRECHANNEL
:
1838 case DLT_JUNIPER_ATM_CEMIC
:
1839 cstate
->off_linktype
.constant_part
= 8;
1840 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1841 cstate
->off_nl
= OFFSET_NOT_SET
;
1842 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1847 cstate
->off_li_hsl
= 4;
1848 cstate
->off_sio
= 3;
1849 cstate
->off_opc
= 4;
1850 cstate
->off_dpc
= 4;
1851 cstate
->off_sls
= 7;
1852 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1853 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1854 cstate
->off_nl
= OFFSET_NOT_SET
;
1855 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1858 case DLT_MTP2_WITH_PHDR
:
1860 cstate
->off_li_hsl
= 8;
1861 cstate
->off_sio
= 7;
1862 cstate
->off_opc
= 8;
1863 cstate
->off_dpc
= 8;
1864 cstate
->off_sls
= 11;
1865 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1866 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1867 cstate
->off_nl
= OFFSET_NOT_SET
;
1868 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1872 cstate
->off_li
= 22;
1873 cstate
->off_li_hsl
= 24;
1874 cstate
->off_sio
= 23;
1875 cstate
->off_opc
= 24;
1876 cstate
->off_dpc
= 24;
1877 cstate
->off_sls
= 27;
1878 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1879 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1880 cstate
->off_nl
= OFFSET_NOT_SET
;
1881 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1885 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1886 cstate
->off_linkpl
.constant_part
= 4;
1888 cstate
->off_nl_nosnap
= 0;
1893 * Currently, only raw "link[N:M]" filtering is supported.
1895 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1896 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1897 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1898 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1902 cstate
->off_linktype
.constant_part
= 1;
1903 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1905 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1908 case DLT_NETANALYZER
:
1909 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1910 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1911 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1912 cstate
->off_nl
= 0; /* Ethernet II */
1913 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1916 case DLT_NETANALYZER_TRANSPARENT
:
1917 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1918 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1919 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1920 cstate
->off_nl
= 0; /* Ethernet II */
1921 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1926 * For values in the range in which we've assigned new
1927 * DLT_ values, only raw "link[N:M]" filtering is supported.
1929 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1930 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1931 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1932 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1933 cstate
->off_nl
= OFFSET_NOT_SET
;
1934 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1936 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1937 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1943 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1948 * Load a value relative to the specified absolute offset.
1950 static struct slist
*
1951 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1952 u_int offset
, u_int size
)
1954 struct slist
*s
, *s2
;
1956 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1959 * If "s" is non-null, it has code to arrange that the X register
1960 * contains the variable part of the absolute offset, so we
1961 * generate a load relative to that, with an offset of
1962 * abs_offset->constant_part + offset.
1964 * Otherwise, we can do an absolute load with an offset of
1965 * abs_offset->constant_part + offset.
1969 * "s" points to a list of statements that puts the
1970 * variable part of the absolute offset into the X register.
1971 * Do an indirect load, to use the X register as an offset.
1973 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1974 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1978 * There is no variable part of the absolute offset, so
1979 * just do an absolute load.
1981 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1982 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1988 * Load a value relative to the beginning of the specified header.
1990 static struct slist
*
1991 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1994 struct slist
*s
, *s2
;
1997 * Squelch warnings from compilers that *don't* assume that
1998 * offrel always has a valid enum value and therefore don't
1999 * assume that we'll always go through one of the case arms.
2001 * If we have a default case, compilers that *do* assume that
2002 * will then complain about the default case code being
2005 * Damned if you do, damned if you don't.
2012 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2017 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2020 case OR_PREVLINKHDR
:
2021 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2025 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2028 case OR_PREVMPLSHDR
:
2029 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2033 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2036 case OR_LINKPL_NOSNAP
:
2037 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2041 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2046 * Load the X register with the length of the IPv4 header
2047 * (plus the offset of the link-layer header, if it's
2048 * preceded by a variable-length header such as a radio
2049 * header), in bytes.
2051 s
= gen_loadx_iphdrlen(cstate
);
2054 * Load the item at {offset of the link-layer payload} +
2055 * {offset, relative to the start of the link-layer
2056 * payload, of the IPv4 header} + {length of the IPv4 header} +
2057 * {specified offset}.
2059 * If the offset of the link-layer payload is variable,
2060 * the variable part of that offset is included in the
2061 * value in the X register, and we include the constant
2062 * part in the offset of the load.
2064 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2065 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2070 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2077 * Generate code to load into the X register the sum of the length of
2078 * the IPv4 header and the variable part of the offset of the link-layer
2081 static struct slist
*
2082 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2084 struct slist
*s
, *s2
;
2086 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2089 * The offset of the link-layer payload has a variable
2090 * part. "s" points to a list of statements that put
2091 * the variable part of that offset into the X register.
2093 * The 4*([k]&0xf) addressing mode can't be used, as we
2094 * don't have a constant offset, so we have to load the
2095 * value in question into the A register and add to it
2096 * the value from the X register.
2098 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2099 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2101 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2104 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2109 * The A register now contains the length of the IP header.
2110 * We need to add to it the variable part of the offset of
2111 * the link-layer payload, which is still in the X
2112 * register, and move the result into the X register.
2114 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2115 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2118 * The offset of the link-layer payload is a constant,
2119 * so no code was generated to load the (nonexistent)
2120 * variable part of that offset.
2122 * This means we can use the 4*([k]&0xf) addressing
2123 * mode. Load the length of the IPv4 header, which
2124 * is at an offset of cstate->off_nl from the beginning of
2125 * the link-layer payload, and thus at an offset of
2126 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2127 * of the raw packet data, using that addressing mode.
2129 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2130 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2136 static struct block
*
2137 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2142 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2144 b
= new_block(cstate
, JMP(BPF_JEQ
));
2150 static inline struct block
*
2151 gen_true(compiler_state_t
*cstate
)
2153 return gen_uncond(cstate
, 1);
2156 static inline struct block
*
2157 gen_false(compiler_state_t
*cstate
)
2159 return gen_uncond(cstate
, 0);
2163 * Generate code to match a particular packet type.
2165 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2166 * value, if <= ETHERMTU. We use that to determine whether to
2167 * match the type/length field or to check the type/length field for
2168 * a value <= ETHERMTU to see whether it's a type field and then do
2169 * the appropriate test.
2171 static struct block
*
2172 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2174 struct block
*b0
, *b1
;
2180 case LLCSAP_NETBEUI
:
2182 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2183 * so we check the DSAP and SSAP.
2185 * LLCSAP_IP checks for IP-over-802.2, rather
2186 * than IP-over-Ethernet or IP-over-SNAP.
2188 * XXX - should we check both the DSAP and the
2189 * SSAP, like this, or should we check just the
2190 * DSAP, as we do for other types <= ETHERMTU
2191 * (i.e., other SAP values)?
2193 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2194 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2202 * Ethernet_II frames, which are Ethernet
2203 * frames with a frame type of ETHERTYPE_IPX;
2205 * Ethernet_802.3 frames, which are 802.3
2206 * frames (i.e., the type/length field is
2207 * a length field, <= ETHERMTU, rather than
2208 * a type field) with the first two bytes
2209 * after the Ethernet/802.3 header being
2212 * Ethernet_802.2 frames, which are 802.3
2213 * frames with an 802.2 LLC header and
2214 * with the IPX LSAP as the DSAP in the LLC
2217 * Ethernet_SNAP frames, which are 802.3
2218 * frames with an LLC header and a SNAP
2219 * header and with an OUI of 0x000000
2220 * (encapsulated Ethernet) and a protocol
2221 * ID of ETHERTYPE_IPX in the SNAP header.
2223 * XXX - should we generate the same code both
2224 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2228 * This generates code to check both for the
2229 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2231 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2232 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2236 * Now we add code to check for SNAP frames with
2237 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2239 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2243 * Now we generate code to check for 802.3
2244 * frames in general.
2246 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2249 * Now add the check for 802.3 frames before the
2250 * check for Ethernet_802.2 and Ethernet_802.3,
2251 * as those checks should only be done on 802.3
2252 * frames, not on Ethernet frames.
2257 * Now add the check for Ethernet_II frames, and
2258 * do that before checking for the other frame
2261 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2265 case ETHERTYPE_ATALK
:
2266 case ETHERTYPE_AARP
:
2268 * EtherTalk (AppleTalk protocols on Ethernet link
2269 * layer) may use 802.2 encapsulation.
2273 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2274 * we check for an Ethernet type field less or equal than
2275 * 1500, which means it's an 802.3 length field.
2277 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2280 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2281 * SNAP packets with an organization code of
2282 * 0x080007 (Apple, for Appletalk) and a protocol
2283 * type of ETHERTYPE_ATALK (Appletalk).
2285 * 802.2-encapsulated ETHERTYPE_AARP packets are
2286 * SNAP packets with an organization code of
2287 * 0x000000 (encapsulated Ethernet) and a protocol
2288 * type of ETHERTYPE_AARP (Appletalk ARP).
2290 if (ll_proto
== ETHERTYPE_ATALK
)
2291 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2292 else /* ll_proto == ETHERTYPE_AARP */
2293 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2297 * Check for Ethernet encapsulation (Ethertalk
2298 * phase 1?); we just check for the Ethernet
2301 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2307 if (ll_proto
<= ETHERMTU
) {
2309 * This is an LLC SAP value, so the frames
2310 * that match would be 802.2 frames.
2311 * Check that the frame is an 802.2 frame
2312 * (i.e., that the length/type field is
2313 * a length field, <= ETHERMTU) and
2314 * then check the DSAP.
2316 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2317 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2322 * This is an Ethernet type, so compare
2323 * the length/type field with it (if
2324 * the frame is an 802.2 frame, the length
2325 * field will be <= ETHERMTU, and, as
2326 * "ll_proto" is > ETHERMTU, this test
2327 * will fail and the frame won't match,
2328 * which is what we want).
2330 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2335 static struct block
*
2336 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2339 * For DLT_NULL, the link-layer header is a 32-bit word
2340 * containing an AF_ value in *host* byte order, and for
2341 * DLT_ENC, the link-layer header begins with a 32-bit
2342 * word containing an AF_ value in host byte order.
2344 * In addition, if we're reading a saved capture file,
2345 * the host byte order in the capture may not be the
2346 * same as the host byte order on this machine.
2348 * For DLT_LOOP, the link-layer header is a 32-bit
2349 * word containing an AF_ value in *network* byte order.
2351 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2353 * The AF_ value is in host byte order, but the BPF
2354 * interpreter will convert it to network byte order.
2356 * If this is a save file, and it's from a machine
2357 * with the opposite byte order to ours, we byte-swap
2360 * Then we run it through "htonl()", and generate
2361 * code to compare against the result.
2363 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2364 ll_proto
= SWAPLONG(ll_proto
);
2365 ll_proto
= htonl(ll_proto
);
2367 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2371 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2372 * or IPv6 then we have an error.
2374 static struct block
*
2375 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2380 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2383 case ETHERTYPE_IPV6
:
2384 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2391 return gen_false(cstate
);
2395 * Generate code to match a particular packet type.
2397 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2398 * value, if <= ETHERMTU. We use that to determine whether to
2399 * match the type field or to check the type field for the special
2400 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2402 static struct block
*
2403 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2405 struct block
*b0
, *b1
;
2411 case LLCSAP_NETBEUI
:
2413 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2414 * so we check the DSAP and SSAP.
2416 * LLCSAP_IP checks for IP-over-802.2, rather
2417 * than IP-over-Ethernet or IP-over-SNAP.
2419 * XXX - should we check both the DSAP and the
2420 * SSAP, like this, or should we check just the
2421 * DSAP, as we do for other types <= ETHERMTU
2422 * (i.e., other SAP values)?
2424 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2425 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2431 * Ethernet_II frames, which are Ethernet
2432 * frames with a frame type of ETHERTYPE_IPX;
2434 * Ethernet_802.3 frames, which have a frame
2435 * type of LINUX_SLL_P_802_3;
2437 * Ethernet_802.2 frames, which are 802.3
2438 * frames with an 802.2 LLC header (i.e, have
2439 * a frame type of LINUX_SLL_P_802_2) and
2440 * with the IPX LSAP as the DSAP in the LLC
2443 * Ethernet_SNAP frames, which are 802.3
2444 * frames with an LLC header and a SNAP
2445 * header and with an OUI of 0x000000
2446 * (encapsulated Ethernet) and a protocol
2447 * ID of ETHERTYPE_IPX in the SNAP header.
2449 * First, do the checks on LINUX_SLL_P_802_2
2450 * frames; generate the check for either
2451 * Ethernet_802.2 or Ethernet_SNAP frames, and
2452 * then put a check for LINUX_SLL_P_802_2 frames
2455 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2456 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2458 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2462 * Now check for 802.3 frames and OR that with
2463 * the previous test.
2465 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2469 * Now add the check for Ethernet_II frames, and
2470 * do that before checking for the other frame
2473 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2477 case ETHERTYPE_ATALK
:
2478 case ETHERTYPE_AARP
:
2480 * EtherTalk (AppleTalk protocols on Ethernet link
2481 * layer) may use 802.2 encapsulation.
2485 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2486 * we check for the 802.2 protocol type in the
2487 * "Ethernet type" field.
2489 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2492 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2493 * SNAP packets with an organization code of
2494 * 0x080007 (Apple, for Appletalk) and a protocol
2495 * type of ETHERTYPE_ATALK (Appletalk).
2497 * 802.2-encapsulated ETHERTYPE_AARP packets are
2498 * SNAP packets with an organization code of
2499 * 0x000000 (encapsulated Ethernet) and a protocol
2500 * type of ETHERTYPE_AARP (Appletalk ARP).
2502 if (ll_proto
== ETHERTYPE_ATALK
)
2503 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2504 else /* ll_proto == ETHERTYPE_AARP */
2505 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2509 * Check for Ethernet encapsulation (Ethertalk
2510 * phase 1?); we just check for the Ethernet
2513 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2519 if (ll_proto
<= ETHERMTU
) {
2521 * This is an LLC SAP value, so the frames
2522 * that match would be 802.2 frames.
2523 * Check for the 802.2 protocol type
2524 * in the "Ethernet type" field, and
2525 * then check the DSAP.
2527 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2528 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2534 * This is an Ethernet type, so compare
2535 * the length/type field with it (if
2536 * the frame is an 802.2 frame, the length
2537 * field will be <= ETHERMTU, and, as
2538 * "ll_proto" is > ETHERMTU, this test
2539 * will fail and the frame won't match,
2540 * which is what we want).
2542 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2548 * Load a value relative to the beginning of the link-layer header after the
2551 static struct slist
*
2552 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2554 struct slist
*s1
, *s2
;
2557 * Generate code to load the length of the pflog header into
2558 * the register assigned to hold that length, if one has been
2559 * assigned. (If one hasn't been assigned, no code we've
2560 * generated uses that prefix, so we don't need to generate any
2563 if (cstate
->off_linkpl
.reg
!= -1) {
2565 * The length is in the first byte of the header.
2567 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2571 * Round it up to a multiple of 4.
2572 * Add 3, and clear the lower 2 bits.
2574 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2577 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2578 s2
->s
.k
= 0xfffffffc;
2582 * Now allocate a register to hold that value and store
2585 s2
= new_stmt(cstate
, BPF_ST
);
2586 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2590 * Now move it into the X register.
2592 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2600 static struct slist
*
2601 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2603 struct slist
*s1
, *s2
;
2604 struct slist
*sjeq_avs_cookie
;
2605 struct slist
*sjcommon
;
2608 * This code is not compatible with the optimizer, as
2609 * we are generating jmp instructions within a normal
2610 * slist of instructions
2612 cstate
->no_optimize
= 1;
2615 * Generate code to load the length of the radio header into
2616 * the register assigned to hold that length, if one has been
2617 * assigned. (If one hasn't been assigned, no code we've
2618 * generated uses that prefix, so we don't need to generate any
2621 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2622 * or always use the AVS header rather than the Prism header.
2623 * We load a 4-byte big-endian value at the beginning of the
2624 * raw packet data, and see whether, when masked with 0xFFFFF000,
2625 * it's equal to 0x80211000. If so, that indicates that it's
2626 * an AVS header (the masked-out bits are the version number).
2627 * Otherwise, it's a Prism header.
2629 * XXX - the Prism header is also, in theory, variable-length,
2630 * but no known software generates headers that aren't 144
2633 if (cstate
->off_linkhdr
.reg
!= -1) {
2637 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2641 * AND it with 0xFFFFF000.
2643 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2644 s2
->s
.k
= 0xFFFFF000;
2648 * Compare with 0x80211000.
2650 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2651 sjeq_avs_cookie
->s
.k
= 0x80211000;
2652 sappend(s1
, sjeq_avs_cookie
);
2657 * The 4 bytes at an offset of 4 from the beginning of
2658 * the AVS header are the length of the AVS header.
2659 * That field is big-endian.
2661 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2664 sjeq_avs_cookie
->s
.jt
= s2
;
2667 * Now jump to the code to allocate a register
2668 * into which to save the header length and
2669 * store the length there. (The "jump always"
2670 * instruction needs to have the k field set;
2671 * it's added to the PC, so, as we're jumping
2672 * over a single instruction, it should be 1.)
2674 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2676 sappend(s1
, sjcommon
);
2679 * Now for the code that handles the Prism header.
2680 * Just load the length of the Prism header (144)
2681 * into the A register. Have the test for an AVS
2682 * header branch here if we don't have an AVS header.
2684 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2687 sjeq_avs_cookie
->s
.jf
= s2
;
2690 * Now allocate a register to hold that value and store
2691 * it. The code for the AVS header will jump here after
2692 * loading the length of the AVS header.
2694 s2
= new_stmt(cstate
, BPF_ST
);
2695 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2697 sjcommon
->s
.jf
= s2
;
2700 * Now move it into the X register.
2702 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2710 static struct slist
*
2711 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2713 struct slist
*s1
, *s2
;
2716 * Generate code to load the length of the AVS header into
2717 * the register assigned to hold that length, if one has been
2718 * assigned. (If one hasn't been assigned, no code we've
2719 * generated uses that prefix, so we don't need to generate any
2722 if (cstate
->off_linkhdr
.reg
!= -1) {
2724 * The 4 bytes at an offset of 4 from the beginning of
2725 * the AVS header are the length of the AVS header.
2726 * That field is big-endian.
2728 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2732 * Now allocate a register to hold that value and store
2735 s2
= new_stmt(cstate
, BPF_ST
);
2736 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2740 * Now move it into the X register.
2742 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2750 static struct slist
*
2751 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2753 struct slist
*s1
, *s2
;
2756 * Generate code to load the length of the radiotap header into
2757 * the register assigned to hold that length, if one has been
2758 * assigned. (If one hasn't been assigned, no code we've
2759 * generated uses that prefix, so we don't need to generate any
2762 if (cstate
->off_linkhdr
.reg
!= -1) {
2764 * The 2 bytes at offsets of 2 and 3 from the beginning
2765 * of the radiotap header are the length of the radiotap
2766 * header; unfortunately, it's little-endian, so we have
2767 * to load it a byte at a time and construct the value.
2771 * Load the high-order byte, at an offset of 3, shift it
2772 * left a byte, and put the result in the X register.
2774 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2776 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2779 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2783 * Load the next byte, at an offset of 2, and OR the
2784 * value from the X register into it.
2786 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2789 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2793 * Now allocate a register to hold that value and store
2796 s2
= new_stmt(cstate
, BPF_ST
);
2797 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2801 * Now move it into the X register.
2803 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2812 * At the moment we treat PPI as normal Radiotap encoded
2813 * packets. The difference is in the function that generates
2814 * the code at the beginning to compute the header length.
2815 * Since this code generator of PPI supports bare 802.11
2816 * encapsulation only (i.e. the encapsulated DLT should be
2817 * DLT_IEEE802_11) we generate code to check for this too;
2818 * that's done in finish_parse().
2820 static struct slist
*
2821 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2823 struct slist
*s1
, *s2
;
2826 * Generate code to load the length of the radiotap header
2827 * into the register assigned to hold that length, if one has
2830 if (cstate
->off_linkhdr
.reg
!= -1) {
2832 * The 2 bytes at offsets of 2 and 3 from the beginning
2833 * of the radiotap header are the length of the radiotap
2834 * header; unfortunately, it's little-endian, so we have
2835 * to load it a byte at a time and construct the value.
2839 * Load the high-order byte, at an offset of 3, shift it
2840 * left a byte, and put the result in the X register.
2842 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2844 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2847 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2851 * Load the next byte, at an offset of 2, and OR the
2852 * value from the X register into it.
2854 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2857 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2861 * Now allocate a register to hold that value and store
2864 s2
= new_stmt(cstate
, BPF_ST
);
2865 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2869 * Now move it into the X register.
2871 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2880 * Load a value relative to the beginning of the link-layer header after the 802.11
2881 * header, i.e. LLC_SNAP.
2882 * The link-layer header doesn't necessarily begin at the beginning
2883 * of the packet data; there might be a variable-length prefix containing
2884 * radio information.
2886 static struct slist
*
2887 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2890 struct slist
*sjset_data_frame_1
;
2891 struct slist
*sjset_data_frame_2
;
2892 struct slist
*sjset_qos
;
2893 struct slist
*sjset_radiotap_flags_present
;
2894 struct slist
*sjset_radiotap_ext_present
;
2895 struct slist
*sjset_radiotap_tsft_present
;
2896 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2897 struct slist
*s_roundup
;
2899 if (cstate
->off_linkpl
.reg
== -1) {
2901 * No register has been assigned to the offset of
2902 * the link-layer payload, which means nobody needs
2903 * it; don't bother computing it - just return
2904 * what we already have.
2910 * This code is not compatible with the optimizer, as
2911 * we are generating jmp instructions within a normal
2912 * slist of instructions
2914 cstate
->no_optimize
= 1;
2917 * If "s" is non-null, it has code to arrange that the X register
2918 * contains the length of the prefix preceding the link-layer
2921 * Otherwise, the length of the prefix preceding the link-layer
2922 * header is "off_outermostlinkhdr.constant_part".
2926 * There is no variable-length header preceding the
2927 * link-layer header.
2929 * Load the length of the fixed-length prefix preceding
2930 * the link-layer header (if any) into the X register,
2931 * and store it in the cstate->off_linkpl.reg register.
2932 * That length is off_outermostlinkhdr.constant_part.
2934 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2935 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2939 * The X register contains the offset of the beginning of the
2940 * link-layer header; add 24, which is the minimum length
2941 * of the MAC header for a data frame, to that, and store it
2942 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2943 * which is at the offset in the X register, with an indexed load.
2945 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2947 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2950 s2
= new_stmt(cstate
, BPF_ST
);
2951 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2954 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2959 * Check the Frame Control field to see if this is a data frame;
2960 * a data frame has the 0x08 bit (b3) in that field set and the
2961 * 0x04 bit (b2) clear.
2963 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2964 sjset_data_frame_1
->s
.k
= 0x08;
2965 sappend(s
, sjset_data_frame_1
);
2968 * If b3 is set, test b2, otherwise go to the first statement of
2969 * the rest of the program.
2971 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2972 sjset_data_frame_2
->s
.k
= 0x04;
2973 sappend(s
, sjset_data_frame_2
);
2974 sjset_data_frame_1
->s
.jf
= snext
;
2977 * If b2 is not set, this is a data frame; test the QoS bit.
2978 * Otherwise, go to the first statement of the rest of the
2981 sjset_data_frame_2
->s
.jt
= snext
;
2982 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2983 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2984 sappend(s
, sjset_qos
);
2987 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2989 * Otherwise, go to the first statement of the rest of the
2992 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2993 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2995 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2998 s2
= new_stmt(cstate
, BPF_ST
);
2999 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3003 * If we have a radiotap header, look at it to see whether
3004 * there's Atheros padding between the MAC-layer header
3007 * Note: all of the fields in the radiotap header are
3008 * little-endian, so we byte-swap all of the values
3009 * we test against, as they will be loaded as big-endian
3012 * XXX - in the general case, we would have to scan through
3013 * *all* the presence bits, if there's more than one word of
3014 * presence bits. That would require a loop, meaning that
3015 * we wouldn't be able to run the filter in the kernel.
3017 * We assume here that the Atheros adapters that insert the
3018 * annoying padding don't have multiple antennae and therefore
3019 * do not generate radiotap headers with multiple presence words.
3021 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3023 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3024 * in the first presence flag word?
3026 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3030 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3031 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3032 sappend(s
, sjset_radiotap_flags_present
);
3035 * If not, skip all of this.
3037 sjset_radiotap_flags_present
->s
.jf
= snext
;
3040 * Otherwise, is the "extension" bit set in that word?
3042 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3043 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3044 sappend(s
, sjset_radiotap_ext_present
);
3045 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3048 * If so, skip all of this.
3050 sjset_radiotap_ext_present
->s
.jt
= snext
;
3053 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3055 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3056 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3057 sappend(s
, sjset_radiotap_tsft_present
);
3058 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3061 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3062 * at an offset of 16 from the beginning of the raw packet
3063 * data (8 bytes for the radiotap header and 8 bytes for
3066 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3069 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3072 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3074 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3075 sjset_tsft_datapad
->s
.k
= 0x20;
3076 sappend(s
, sjset_tsft_datapad
);
3079 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3080 * at an offset of 8 from the beginning of the raw packet
3081 * data (8 bytes for the radiotap header).
3083 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3086 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3089 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3091 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3092 sjset_notsft_datapad
->s
.k
= 0x20;
3093 sappend(s
, sjset_notsft_datapad
);
3096 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3097 * set, round the length of the 802.11 header to
3098 * a multiple of 4. Do that by adding 3 and then
3099 * dividing by and multiplying by 4, which we do by
3102 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3103 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3104 sappend(s
, s_roundup
);
3105 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3108 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3109 s2
->s
.k
= (bpf_u_int32
)~3;
3111 s2
= new_stmt(cstate
, BPF_ST
);
3112 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3115 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3116 sjset_tsft_datapad
->s
.jf
= snext
;
3117 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3118 sjset_notsft_datapad
->s
.jf
= snext
;
3120 sjset_qos
->s
.jf
= snext
;
3126 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3130 /* There is an implicit dependency between the link
3131 * payload and link header since the payload computation
3132 * includes the variable part of the header. Therefore,
3133 * if nobody else has allocated a register for the link
3134 * header and we need it, do it now. */
3135 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3136 cstate
->off_linkhdr
.reg
== -1)
3137 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3140 * For link-layer types that have a variable-length header
3141 * preceding the link-layer header, generate code to load
3142 * the offset of the link-layer header into the register
3143 * assigned to that offset, if any.
3145 * XXX - this, and the next switch statement, won't handle
3146 * encapsulation of 802.11 or 802.11+radio information in
3147 * some other protocol stack. That's significantly more
3150 switch (cstate
->outermostlinktype
) {
3152 case DLT_PRISM_HEADER
:
3153 s
= gen_load_prism_llprefixlen(cstate
);
3156 case DLT_IEEE802_11_RADIO_AVS
:
3157 s
= gen_load_avs_llprefixlen(cstate
);
3160 case DLT_IEEE802_11_RADIO
:
3161 s
= gen_load_radiotap_llprefixlen(cstate
);
3165 s
= gen_load_ppi_llprefixlen(cstate
);
3174 * For link-layer types that have a variable-length link-layer
3175 * header, generate code to load the offset of the link-layer
3176 * payload into the register assigned to that offset, if any.
3178 switch (cstate
->outermostlinktype
) {
3180 case DLT_IEEE802_11
:
3181 case DLT_PRISM_HEADER
:
3182 case DLT_IEEE802_11_RADIO_AVS
:
3183 case DLT_IEEE802_11_RADIO
:
3185 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3189 s
= gen_load_pflog_llprefixlen(cstate
);
3194 * If there is no initialization yet and we need variable
3195 * length offsets for VLAN, initialize them to zero
3197 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3200 if (cstate
->off_linkpl
.reg
== -1)
3201 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3202 if (cstate
->off_linktype
.reg
== -1)
3203 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3205 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3207 s2
= new_stmt(cstate
, BPF_ST
);
3208 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3210 s2
= new_stmt(cstate
, BPF_ST
);
3211 s2
->s
.k
= cstate
->off_linktype
.reg
;
3216 * If we have any offset-loading code, append all the
3217 * existing statements in the block to those statements,
3218 * and make the resulting list the list of statements
3222 sappend(s
, b
->stmts
);
3228 * Take an absolute offset, and:
3230 * if it has no variable part, return NULL;
3232 * if it has a variable part, generate code to load the register
3233 * containing that variable part into the X register, returning
3234 * a pointer to that code - if no register for that offset has
3235 * been allocated, allocate it first.
3237 * (The code to set that register will be generated later, but will
3238 * be placed earlier in the code sequence.)
3240 static struct slist
*
3241 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3245 if (off
->is_variable
) {
3246 if (off
->reg
== -1) {
3248 * We haven't yet assigned a register for the
3249 * variable part of the offset of the link-layer
3250 * header; allocate one.
3252 off
->reg
= alloc_reg(cstate
);
3256 * Load the register containing the variable part of the
3257 * offset of the link-layer header into the X register.
3259 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3264 * That offset isn't variable, there's no variable part,
3265 * so we don't need to generate any code.
3272 * Map an Ethernet type to the equivalent PPP type.
3275 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3283 case ETHERTYPE_IPV6
:
3284 ll_proto
= PPP_IPV6
;
3288 ll_proto
= PPP_DECNET
;
3291 case ETHERTYPE_ATALK
:
3292 ll_proto
= PPP_APPLE
;
3305 * I'm assuming the "Bridging PDU"s that go
3306 * over PPP are Spanning Tree Protocol
3309 ll_proto
= PPP_BRPDU
;
3320 * Generate any tests that, for encapsulation of a link-layer packet
3321 * inside another protocol stack, need to be done to check for those
3322 * link-layer packets (and that haven't already been done by a check
3323 * for that encapsulation).
3325 static struct block
*
3326 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3330 if (cstate
->is_encap
)
3331 return gen_encap_ll_check(cstate
);
3333 switch (cstate
->prevlinktype
) {
3337 * This is LANE-encapsulated Ethernet; check that the LANE
3338 * packet doesn't begin with an LE Control marker, i.e.
3339 * that it's data, not a control message.
3341 * (We've already generated a test for LANE.)
3343 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3349 * No such tests are necessary.
3357 * The three different values we should check for when checking for an
3358 * IPv6 packet with DLT_NULL.
3360 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3361 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3362 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3365 * Generate code to match a particular packet type by matching the
3366 * link-layer type field or fields in the 802.2 LLC header.
3368 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3369 * value, if <= ETHERMTU.
3371 static struct block
*
3372 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3374 struct block
*b0
, *b1
, *b2
;
3376 /* are we checking MPLS-encapsulated packets? */
3377 if (cstate
->label_stack_depth
> 0)
3378 return gen_mpls_linktype(cstate
, ll_proto
);
3380 switch (cstate
->linktype
) {
3383 case DLT_NETANALYZER
:
3384 case DLT_NETANALYZER_TRANSPARENT
:
3385 /* Geneve has an EtherType regardless of whether there is an
3386 * L2 header. VXLAN always has an EtherType. */
3387 if (!cstate
->is_encap
)
3388 b0
= gen_prevlinkhdr_check(cstate
);
3392 b1
= gen_ether_linktype(cstate
, ll_proto
);
3403 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3407 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3411 case DLT_IEEE802_11
:
3412 case DLT_PRISM_HEADER
:
3413 case DLT_IEEE802_11_RADIO_AVS
:
3414 case DLT_IEEE802_11_RADIO
:
3417 * Check that we have a data frame.
3419 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3420 IEEE80211_FC0_TYPE_DATA
,
3421 IEEE80211_FC0_TYPE_MASK
);
3424 * Now check for the specified link-layer type.
3426 b1
= gen_llc_linktype(cstate
, ll_proto
);
3433 * XXX - check for LLC frames.
3435 return gen_llc_linktype(cstate
, ll_proto
);
3440 * XXX - check for LLC PDUs, as per IEEE 802.5.
3442 return gen_llc_linktype(cstate
, ll_proto
);
3445 case DLT_ATM_RFC1483
:
3447 case DLT_IP_OVER_FC
:
3448 return gen_llc_linktype(cstate
, ll_proto
);
3453 * Check for an LLC-encapsulated version of this protocol;
3454 * if we were checking for LANE, linktype would no longer
3457 * Check for LLC encapsulation and then check the protocol.
3459 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3460 b1
= gen_llc_linktype(cstate
, ll_proto
);
3466 return gen_linux_sll_linktype(cstate
, ll_proto
);
3470 case DLT_SLIP_BSDOS
:
3473 * These types don't provide any type field; packets
3474 * are always IPv4 or IPv6.
3476 * XXX - for IPv4, check for a version number of 4, and,
3477 * for IPv6, check for a version number of 6?
3482 /* Check for a version number of 4. */
3483 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3485 case ETHERTYPE_IPV6
:
3486 /* Check for a version number of 6. */
3487 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3490 return gen_false(cstate
); /* always false */
3496 * Raw IPv4, so no type field.
3498 if (ll_proto
== ETHERTYPE_IP
)
3499 return gen_true(cstate
); /* always true */
3501 /* Checking for something other than IPv4; always false */
3502 return gen_false(cstate
);
3507 * Raw IPv6, so no type field.
3509 if (ll_proto
== ETHERTYPE_IPV6
)
3510 return gen_true(cstate
); /* always true */
3512 /* Checking for something other than IPv6; always false */
3513 return gen_false(cstate
);
3518 case DLT_PPP_SERIAL
:
3521 * We use Ethernet protocol types inside libpcap;
3522 * map them to the corresponding PPP protocol types.
3524 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3525 ethertype_to_ppptype(ll_proto
));
3530 * We use Ethernet protocol types inside libpcap;
3531 * map them to the corresponding PPP protocol types.
3537 * Also check for Van Jacobson-compressed IP.
3538 * XXX - do this for other forms of PPP?
3540 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3541 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3543 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3548 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3549 ethertype_to_ppptype(ll_proto
));
3559 return (gen_loopback_linktype(cstate
, AF_INET
));
3561 case ETHERTYPE_IPV6
:
3563 * AF_ values may, unfortunately, be platform-
3564 * dependent; AF_INET isn't, because everybody
3565 * used 4.2BSD's value, but AF_INET6 is, because
3566 * 4.2BSD didn't have a value for it (given that
3567 * IPv6 didn't exist back in the early 1980's),
3568 * and they all picked their own values.
3570 * This means that, if we're reading from a
3571 * savefile, we need to check for all the
3574 * If we're doing a live capture, we only need
3575 * to check for this platform's value; however,
3576 * Npcap uses 24, which isn't Windows's AF_INET6
3577 * value. (Given the multiple different values,
3578 * programs that read pcap files shouldn't be
3579 * checking for their platform's AF_INET6 value
3580 * anyway, they should check for all of the
3581 * possible values. and they might as well do
3582 * that even for live captures.)
3584 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3586 * Savefile - check for all three
3587 * possible IPv6 values.
3589 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3590 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3592 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3597 * Live capture, so we only need to
3598 * check for the value used on this
3603 * Npcap doesn't use Windows's AF_INET6,
3604 * as that collides with AF_IPX on
3605 * some BSDs (both have the value 23).
3606 * Instead, it uses 24.
3608 return (gen_loopback_linktype(cstate
, 24));
3611 return (gen_loopback_linktype(cstate
, AF_INET6
));
3612 #else /* AF_INET6 */
3614 * I guess this platform doesn't support
3615 * IPv6, so we just reject all packets.
3617 return gen_false(cstate
);
3618 #endif /* AF_INET6 */
3624 * Not a type on which we support filtering.
3625 * XXX - support those that have AF_ values
3626 * #defined on this platform, at least?
3628 return gen_false(cstate
);
3633 * af field is host byte order in contrast to the rest of
3636 if (ll_proto
== ETHERTYPE_IP
)
3637 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3639 else if (ll_proto
== ETHERTYPE_IPV6
)
3640 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3643 return gen_false(cstate
);
3647 case DLT_ARCNET_LINUX
:
3649 * XXX should we check for first fragment if the protocol
3655 return gen_false(cstate
);
3657 case ETHERTYPE_IPV6
:
3658 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3662 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3664 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3670 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3672 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3677 case ETHERTYPE_REVARP
:
3678 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3681 case ETHERTYPE_ATALK
:
3682 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3689 case ETHERTYPE_ATALK
:
3690 return gen_true(cstate
);
3692 return gen_false(cstate
);
3698 * XXX - assumes a 2-byte Frame Relay header with
3699 * DLCI and flags. What if the address is longer?
3705 * Check for the special NLPID for IP.
3707 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3709 case ETHERTYPE_IPV6
:
3711 * Check for the special NLPID for IPv6.
3713 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3717 * Check for several OSI protocols.
3719 * Frame Relay packets typically have an OSI
3720 * NLPID at the beginning; we check for each
3723 * What we check for is the NLPID and a frame
3724 * control field of UI, i.e. 0x03 followed
3727 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3728 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3729 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3735 return gen_false(cstate
);
3740 break; // not implemented
3742 case DLT_JUNIPER_MFR
:
3743 case DLT_JUNIPER_MLFR
:
3744 case DLT_JUNIPER_MLPPP
:
3745 case DLT_JUNIPER_ATM1
:
3746 case DLT_JUNIPER_ATM2
:
3747 case DLT_JUNIPER_PPPOE
:
3748 case DLT_JUNIPER_PPPOE_ATM
:
3749 case DLT_JUNIPER_GGSN
:
3750 case DLT_JUNIPER_ES
:
3751 case DLT_JUNIPER_MONITOR
:
3752 case DLT_JUNIPER_SERVICES
:
3753 case DLT_JUNIPER_ETHER
:
3754 case DLT_JUNIPER_PPP
:
3755 case DLT_JUNIPER_FRELAY
:
3756 case DLT_JUNIPER_CHDLC
:
3757 case DLT_JUNIPER_VP
:
3758 case DLT_JUNIPER_ST
:
3759 case DLT_JUNIPER_ISM
:
3760 case DLT_JUNIPER_VS
:
3761 case DLT_JUNIPER_SRX_E2E
:
3762 case DLT_JUNIPER_FIBRECHANNEL
:
3763 case DLT_JUNIPER_ATM_CEMIC
:
3765 /* just lets verify the magic number for now -
3766 * on ATM we may have up to 6 different encapsulations on the wire
3767 * and need a lot of heuristics to figure out that the payload
3770 * FIXME encapsulation specific BPF_ filters
3772 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3774 case DLT_BACNET_MS_TP
:
3775 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3778 return gen_ipnet_linktype(cstate
, ll_proto
);
3780 case DLT_LINUX_IRDA
:
3783 case DLT_MTP2_WITH_PHDR
:
3786 case DLT_LINUX_LAPD
:
3787 case DLT_USB_FREEBSD
:
3789 case DLT_USB_LINUX_MMAPPED
:
3791 case DLT_BLUETOOTH_HCI_H4
:
3792 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3794 case DLT_CAN_SOCKETCAN
:
3795 case DLT_IEEE802_15_4
:
3796 case DLT_IEEE802_15_4_LINUX
:
3797 case DLT_IEEE802_15_4_NONASK_PHY
:
3798 case DLT_IEEE802_15_4_NOFCS
:
3799 case DLT_IEEE802_15_4_TAP
:
3800 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3803 case DLT_IPMB_KONTRON
:
3807 /* Using the fixed-size NFLOG header it is possible to tell only
3808 * the address family of the packet, other meaningful data is
3809 * either missing or behind TLVs.
3811 break; // not implemented
3815 * Does this link-layer header type have a field
3816 * indicating the type of the next protocol? If
3817 * so, off_linktype.constant_part will be the offset of that
3818 * field in the packet; if not, it will be OFFSET_NOT_SET.
3820 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3822 * Yes; assume it's an Ethernet type. (If
3823 * it's not, it needs to be handled specially
3826 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3830 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
3831 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3835 * Check for an LLC SNAP packet with a given organization code and
3836 * protocol type; we check the entire contents of the 802.2 LLC and
3837 * snap headers, checking for DSAP and SSAP of SNAP and a control
3838 * field of 0x03 in the LLC header, and for the specified organization
3839 * code and protocol type in the SNAP header.
3841 static struct block
*
3842 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3844 u_char snapblock
[8];
3846 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3847 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3848 snapblock
[2] = 0x03; /* control = UI */
3849 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3850 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3851 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3852 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3853 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3854 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3858 * Generate code to match frames with an LLC header.
3860 static struct block
*
3861 gen_llc_internal(compiler_state_t
*cstate
)
3863 struct block
*b0
, *b1
;
3865 switch (cstate
->linktype
) {
3869 * We check for an Ethernet type field less or equal than
3870 * 1500, which means it's an 802.3 length field.
3872 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3875 * Now check for the purported DSAP and SSAP not being
3876 * 0xFF, to rule out NetWare-over-802.3.
3878 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3885 * We check for LLC traffic.
3887 b0
= gen_atmtype_llc(cstate
);
3890 case DLT_IEEE802
: /* Token Ring */
3892 * XXX - check for LLC frames.
3894 return gen_true(cstate
);
3898 * XXX - check for LLC frames.
3900 return gen_true(cstate
);
3902 case DLT_ATM_RFC1483
:
3904 * For LLC encapsulation, these are defined to have an
3907 * For VC encapsulation, they don't, but there's no
3908 * way to check for that; the protocol used on the VC
3909 * is negotiated out of band.
3911 return gen_true(cstate
);
3913 case DLT_IEEE802_11
:
3914 case DLT_PRISM_HEADER
:
3915 case DLT_IEEE802_11_RADIO
:
3916 case DLT_IEEE802_11_RADIO_AVS
:
3919 * Check that we have a data frame.
3921 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3922 IEEE80211_FC0_TYPE_DATA
,
3923 IEEE80211_FC0_TYPE_MASK
);
3926 fail_kw_on_dlt(cstate
, "llc");
3932 gen_llc(compiler_state_t
*cstate
)
3935 * Catch errors reported by us and routines below us, and return NULL
3938 if (setjmp(cstate
->top_ctx
))
3941 return gen_llc_internal(cstate
);
3945 gen_llc_i(compiler_state_t
*cstate
)
3947 struct block
*b0
, *b1
;
3951 * Catch errors reported by us and routines below us, and return NULL
3954 if (setjmp(cstate
->top_ctx
))
3958 * Check whether this is an LLC frame.
3960 b0
= gen_llc_internal(cstate
);
3963 * Load the control byte and test the low-order bit; it must
3964 * be clear for I frames.
3966 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3967 b1
= new_block(cstate
, JMP(BPF_JSET
));
3976 gen_llc_s(compiler_state_t
*cstate
)
3978 struct block
*b0
, *b1
;
3981 * Catch errors reported by us and routines below us, and return NULL
3984 if (setjmp(cstate
->top_ctx
))
3988 * Check whether this is an LLC frame.
3990 b0
= gen_llc_internal(cstate
);
3993 * Now compare the low-order 2 bit of the control byte against
3994 * the appropriate value for S frames.
3996 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4002 gen_llc_u(compiler_state_t
*cstate
)
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 compare the low-order 2 bit of the control byte against
4020 * the appropriate value for U frames.
4022 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4028 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4030 struct block
*b0
, *b1
;
4033 * Catch errors reported by us and routines below us, and return NULL
4036 if (setjmp(cstate
->top_ctx
))
4040 * Check whether this is an LLC frame.
4042 b0
= gen_llc_internal(cstate
);
4045 * Now check for an S frame with the appropriate type.
4047 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4053 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4055 struct block
*b0
, *b1
;
4058 * Catch errors reported by us and routines below us, and return NULL
4061 if (setjmp(cstate
->top_ctx
))
4065 * Check whether this is an LLC frame.
4067 b0
= gen_llc_internal(cstate
);
4070 * Now check for a U frame with the appropriate type.
4072 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4078 * Generate code to match a particular packet type, for link-layer types
4079 * using 802.2 LLC headers.
4081 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4082 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4084 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4085 * value, if <= ETHERMTU. We use that to determine whether to
4086 * match the DSAP or both DSAP and LSAP or to check the OUI and
4087 * protocol ID in a SNAP header.
4089 static struct block
*
4090 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4093 * XXX - handle token-ring variable-length header.
4099 case LLCSAP_NETBEUI
:
4101 * XXX - should we check both the DSAP and the
4102 * SSAP, like this, or should we check just the
4103 * DSAP, as we do for other SAP values?
4105 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4106 ((ll_proto
<< 8) | ll_proto
));
4110 * XXX - are there ever SNAP frames for IPX on
4111 * non-Ethernet 802.x networks?
4113 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4115 case ETHERTYPE_ATALK
:
4117 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4118 * SNAP packets with an organization code of
4119 * 0x080007 (Apple, for Appletalk) and a protocol
4120 * type of ETHERTYPE_ATALK (Appletalk).
4122 * XXX - check for an organization code of
4123 * encapsulated Ethernet as well?
4125 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4129 * XXX - we don't have to check for IPX 802.3
4130 * here, but should we check for the IPX Ethertype?
4132 if (ll_proto
<= ETHERMTU
) {
4134 * This is an LLC SAP value, so check
4137 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4140 * This is an Ethernet type; we assume that it's
4141 * unlikely that it'll appear in the right place
4142 * at random, and therefore check only the
4143 * location that would hold the Ethernet type
4144 * in a SNAP frame with an organization code of
4145 * 0x000000 (encapsulated Ethernet).
4147 * XXX - if we were to check for the SNAP DSAP and
4148 * LSAP, as per XXX, and were also to check for an
4149 * organization code of 0x000000 (encapsulated
4150 * Ethernet), we'd do
4152 * return gen_snap(cstate, 0x000000, ll_proto);
4154 * here; for now, we don't, as per the above.
4155 * I don't know whether it's worth the extra CPU
4156 * time to do the right check or not.
4158 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4163 static struct block
*
4164 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4165 int dir
, u_int src_off
, u_int dst_off
)
4167 struct block
*b0
, *b1
;
4181 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4182 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4188 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4189 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4199 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4206 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4210 static struct block
*
4211 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4212 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4214 struct block
*b0
, *b1
;
4217 * Code below needs to access four separate 32-bit parts of the 128-bit
4218 * IPv6 address and mask. In some OSes this is as simple as using the
4219 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4220 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4221 * far as libpcap sees it. Hence copy the data before use to avoid
4222 * potential unaligned memory access and the associated compiler
4223 * warnings (whether genuine or not).
4225 bpf_u_int32 a
[4], m
[4];
4238 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4239 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4245 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4246 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4256 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4263 /* this order is important */
4264 memcpy(a
, addr
, sizeof(a
));
4265 memcpy(m
, mask
, sizeof(m
));
4266 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4267 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4269 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4271 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4277 static struct block
*
4278 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4280 register struct block
*b0
, *b1
;
4284 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4287 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4290 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4291 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4297 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4298 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4308 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4316 * Like gen_ehostop, but for DLT_FDDI
4318 static struct block
*
4319 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4321 struct block
*b0
, *b1
;
4325 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4328 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4331 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4332 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4338 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4339 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4349 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4357 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4359 static struct block
*
4360 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4362 register struct block
*b0
, *b1
;
4366 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4369 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4372 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4373 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4379 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4380 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4390 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4398 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4399 * various 802.11 + radio headers.
4401 static struct block
*
4402 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4404 register struct block
*b0
, *b1
, *b2
;
4405 register struct slist
*s
;
4407 #ifdef ENABLE_WLAN_FILTERING_PATCH
4410 * We need to disable the optimizer because the optimizer is buggy
4411 * and wipes out some LD instructions generated by the below
4412 * code to validate the Frame Control bits
4414 cstate
->no_optimize
= 1;
4415 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4422 * For control frames, there is no SA.
4424 * For management frames, SA is at an
4425 * offset of 10 from the beginning of
4428 * For data frames, SA is at an offset
4429 * of 10 from the beginning of the packet
4430 * if From DS is clear, at an offset of
4431 * 16 from the beginning of the packet
4432 * if From DS is set and To DS is clear,
4433 * and an offset of 24 from the beginning
4434 * of the packet if From DS is set and To DS
4439 * Generate the tests to be done for data frames
4442 * First, check for To DS set, i.e. check "link[1] & 0x01".
4444 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4445 b1
= new_block(cstate
, JMP(BPF_JSET
));
4446 b1
->s
.k
= 0x01; /* To DS */
4450 * If To DS is set, the SA is at 24.
4452 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4456 * Now, check for To DS not set, i.e. check
4457 * "!(link[1] & 0x01)".
4459 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4460 b2
= new_block(cstate
, JMP(BPF_JSET
));
4461 b2
->s
.k
= 0x01; /* To DS */
4466 * If To DS is not set, the SA is at 16.
4468 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4472 * Now OR together the last two checks. That gives
4473 * the complete set of checks for data frames with
4479 * Now check for From DS being set, and AND that with
4480 * the ORed-together checks.
4482 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4483 b1
= new_block(cstate
, JMP(BPF_JSET
));
4484 b1
->s
.k
= 0x02; /* From DS */
4489 * Now check for data frames with From DS not set.
4491 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4492 b2
= new_block(cstate
, JMP(BPF_JSET
));
4493 b2
->s
.k
= 0x02; /* From DS */
4498 * If From DS isn't set, the SA is at 10.
4500 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4504 * Now OR together the checks for data frames with
4505 * From DS not set and for data frames with From DS
4506 * set; that gives the checks done for data frames.
4511 * Now check for a data frame.
4512 * I.e, check "link[0] & 0x08".
4514 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4515 b1
= new_block(cstate
, JMP(BPF_JSET
));
4520 * AND that with the checks done for data frames.
4525 * If the high-order bit of the type value is 0, this
4526 * is a management frame.
4527 * I.e, check "!(link[0] & 0x08)".
4529 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4530 b2
= new_block(cstate
, JMP(BPF_JSET
));
4536 * For management frames, the SA is at 10.
4538 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4542 * OR that with the checks done for data frames.
4543 * That gives the checks done for management and
4549 * If the low-order bit of the type value is 1,
4550 * this is either a control frame or a frame
4551 * with a reserved type, and thus not a
4554 * I.e., check "!(link[0] & 0x04)".
4556 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4557 b1
= new_block(cstate
, JMP(BPF_JSET
));
4563 * AND that with the checks for data and management
4573 * For control frames, there is no DA.
4575 * For management frames, DA is at an
4576 * offset of 4 from the beginning of
4579 * For data frames, DA is at an offset
4580 * of 4 from the beginning of the packet
4581 * if To DS is clear and at an offset of
4582 * 16 from the beginning of the packet
4587 * Generate the tests to be done for data frames.
4589 * First, check for To DS set, i.e. "link[1] & 0x01".
4591 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4592 b1
= new_block(cstate
, JMP(BPF_JSET
));
4593 b1
->s
.k
= 0x01; /* To DS */
4597 * If To DS is set, the DA is at 16.
4599 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4603 * Now, check for To DS not set, i.e. check
4604 * "!(link[1] & 0x01)".
4606 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4607 b2
= new_block(cstate
, JMP(BPF_JSET
));
4608 b2
->s
.k
= 0x01; /* To DS */
4613 * If To DS is not set, the DA is at 4.
4615 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4619 * Now OR together the last two checks. That gives
4620 * the complete set of checks for data frames.
4625 * Now check for a data frame.
4626 * I.e, check "link[0] & 0x08".
4628 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4629 b1
= new_block(cstate
, JMP(BPF_JSET
));
4634 * AND that with the checks done for data frames.
4639 * If the high-order bit of the type value is 0, this
4640 * is a management frame.
4641 * I.e, check "!(link[0] & 0x08)".
4643 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4644 b2
= new_block(cstate
, JMP(BPF_JSET
));
4650 * For management frames, the DA is at 4.
4652 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4656 * OR that with the checks done for data frames.
4657 * That gives the checks done for management and
4663 * If the low-order bit of the type value is 1,
4664 * this is either a control frame or a frame
4665 * with a reserved type, and thus not a
4668 * I.e., check "!(link[0] & 0x04)".
4670 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4671 b1
= new_block(cstate
, JMP(BPF_JSET
));
4677 * AND that with the checks for data and management
4684 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4685 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4691 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4692 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4697 * XXX - add BSSID keyword?
4700 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4704 * Not present in CTS or ACK control frames.
4706 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4707 IEEE80211_FC0_TYPE_MASK
);
4709 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4710 IEEE80211_FC0_SUBTYPE_MASK
);
4712 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4713 IEEE80211_FC0_SUBTYPE_MASK
);
4717 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4723 * Not present in control frames.
4725 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4726 IEEE80211_FC0_TYPE_MASK
);
4728 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4734 * Present only if the direction mask has both "From DS"
4735 * and "To DS" set. Neither control frames nor management
4736 * frames should have both of those set, so we don't
4737 * check the frame type.
4739 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4740 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4741 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4747 * Not present in management frames; addr1 in other
4752 * If the high-order bit of the type value is 0, this
4753 * is a management frame.
4754 * I.e, check "(link[0] & 0x08)".
4756 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4757 b1
= new_block(cstate
, JMP(BPF_JSET
));
4764 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4767 * AND that with the check of addr1.
4774 * Not present in management frames; addr2, if present,
4779 * Not present in CTS or ACK control frames.
4781 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4782 IEEE80211_FC0_TYPE_MASK
);
4784 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4785 IEEE80211_FC0_SUBTYPE_MASK
);
4787 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4788 IEEE80211_FC0_SUBTYPE_MASK
);
4794 * If the high-order bit of the type value is 0, this
4795 * is a management frame.
4796 * I.e, check "(link[0] & 0x08)".
4798 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4799 b1
= new_block(cstate
, JMP(BPF_JSET
));
4804 * AND that with the check for frames other than
4805 * CTS and ACK frames.
4812 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4821 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4822 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4823 * as the RFC states.)
4825 static struct block
*
4826 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4828 register struct block
*b0
, *b1
;
4832 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4835 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4838 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4839 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4845 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4846 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4856 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4864 * This is quite tricky because there may be pad bytes in front of the
4865 * DECNET header, and then there are two possible data packet formats that
4866 * carry both src and dst addresses, plus 5 packet types in a format that
4867 * carries only the src node, plus 2 types that use a different format and
4868 * also carry just the src node.
4872 * Instead of doing those all right, we just look for data packets with
4873 * 0 or 1 bytes of padding. If you want to look at other packets, that
4874 * will require a lot more hacking.
4876 * To add support for filtering on DECNET "areas" (network numbers)
4877 * one would want to add a "mask" argument to this routine. That would
4878 * make the filter even more inefficient, although one could be clever
4879 * and not generate masking instructions if the mask is 0xFFFF.
4881 static struct block
*
4882 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4884 struct block
*b0
, *b1
, *b2
, *tmp
;
4885 u_int offset_lh
; /* offset if long header is received */
4886 u_int offset_sh
; /* offset if short header is received */
4891 offset_sh
= 1; /* follows flags */
4892 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4896 offset_sh
= 3; /* follows flags, dstnode */
4897 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4901 /* Inefficient because we do our Calvinball dance twice */
4902 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4903 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4909 /* Inefficient because we do our Calvinball dance twice */
4910 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4911 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4921 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4929 * In a DECnet message inside an Ethernet frame the first two bytes
4930 * immediately after EtherType are the [litle-endian] DECnet message
4931 * length, which is irrelevant in this context.
4933 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
4934 * 8-bit bitmap of the optional padding before the packet route header.
4935 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
4936 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
4937 * means there aren't any PAD bytes after the bitmap, so the header
4938 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
4939 * is set to 0, thus the header begins at the third byte.
4941 * The header can be in several (as mentioned above) formats, all of
4942 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
4943 * (PF, "pad field") set to 0 regardless of any padding present before
4944 * the header. "Short header" means bits 0-2 of the bitmap encode the
4945 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
4947 * To test PLENGTH and FLAGS, use multiple-byte constants with the
4948 * values and the masks, this maps to the required single bytes of
4949 * the message correctly on both big-endian and little-endian hosts.
4950 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
4951 * because the wire encoding is little-endian and BPF multiple-byte
4952 * loads are big-endian. When the destination address is near enough
4953 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
4956 /* Check for pad = 1, long header case */
4957 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
4958 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4959 BPF_H
, SWAPSHORT(addr
));
4961 /* Check for pad = 0, long header case */
4962 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
4963 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4967 /* Check for pad = 1, short header case */
4969 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
4970 0x81020000U
| SWAPSHORT(addr
),
4973 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
4974 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4979 /* Check for pad = 0, short header case */
4981 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
4982 0x02000000U
| SWAPSHORT(addr
) << 8,
4985 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
4986 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4996 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4997 * test the bottom-of-stack bit, and then check the version number
4998 * field in the IP header.
5000 static struct block
*
5001 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5003 struct block
*b0
, *b1
;
5008 /* match the bottom-of-stack bit */
5009 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5010 /* match the IPv4 version number */
5011 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5015 case ETHERTYPE_IPV6
:
5016 /* match the bottom-of-stack bit */
5017 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5018 /* match the IPv4 version number */
5019 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5024 /* FIXME add other L3 proto IDs */
5025 bpf_error(cstate
, "unsupported protocol over mpls");
5030 static struct block
*
5031 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5032 int proto
, int dir
, int type
)
5034 struct block
*b0
, *b1
;
5035 const char *typestr
;
5045 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5047 * Only check for non-IPv4 addresses if we're not
5048 * checking MPLS-encapsulated packets.
5050 if (cstate
->label_stack_depth
== 0) {
5051 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5053 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5059 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
5062 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5063 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5068 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5069 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5074 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5075 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5080 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
5083 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5086 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5089 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5092 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5095 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5098 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5101 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5102 b1
= gen_dnhostop(cstate
, addr
, dir
);
5107 bpf_error(cstate
, "LAT host filtering not implemented");
5110 bpf_error(cstate
, "SCA host filtering not implemented");
5113 bpf_error(cstate
, "MOPRC host filtering not implemented");
5116 bpf_error(cstate
, "MOPDL host filtering not implemented");
5119 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5122 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5125 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5128 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5131 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5134 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5137 bpf_error(cstate
, "AARP host filtering not implemented");
5140 bpf_error(cstate
, "ISO host filtering not implemented");
5143 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5146 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5149 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5152 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5155 bpf_error(cstate
, "IPX host filtering not implemented");
5158 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5161 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5164 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5167 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5170 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5173 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5176 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5179 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5182 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5185 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5194 static struct block
*
5195 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5196 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5198 struct block
*b0
, *b1
;
5199 const char *typestr
;
5210 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5211 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5216 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5219 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5222 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5225 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5228 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5231 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5234 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5237 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5240 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5243 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5246 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5249 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5252 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5255 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5258 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5261 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5264 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5267 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5270 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5273 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5276 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5279 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5282 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5285 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5288 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5291 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5294 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5297 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5300 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5303 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5306 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5309 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5312 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5315 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5318 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5321 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5324 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5327 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5337 static struct block
*
5338 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5339 struct addrinfo
*alist
, int proto
, int dir
)
5341 struct block
*b0
, *b1
, *tmp
;
5342 struct addrinfo
*ai
;
5343 struct sockaddr_in
*sin
;
5346 bpf_error(cstate
, "direction applied to 'gateway'");
5353 switch (cstate
->linktype
) {
5355 case DLT_NETANALYZER
:
5356 case DLT_NETANALYZER_TRANSPARENT
:
5357 b1
= gen_prevlinkhdr_check(cstate
);
5358 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5363 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5366 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5368 case DLT_IEEE802_11
:
5369 case DLT_PRISM_HEADER
:
5370 case DLT_IEEE802_11_RADIO_AVS
:
5371 case DLT_IEEE802_11_RADIO
:
5373 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5375 case DLT_IP_OVER_FC
:
5376 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5380 * This is LLC-multiplexed traffic; if it were
5381 * LANE, cstate->linktype would have been set to
5387 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5390 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5392 * Does it have an address?
5394 if (ai
->ai_addr
!= NULL
) {
5396 * Yes. Is it an IPv4 address?
5398 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5400 * Generate an entry for it.
5402 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5403 tmp
= gen_host(cstate
,
5404 ntohl(sin
->sin_addr
.s_addr
),
5405 0xffffffff, proto
, Q_OR
, Q_HOST
);
5407 * Is it the *first* IPv4 address?
5411 * Yes, so start with it.
5416 * No, so OR it into the
5428 * No IPv4 addresses found.
5436 bpf_error(cstate
, "illegal modifier of 'gateway'");
5441 static struct block
*
5442 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5445 struct block
*b1
= NULL
;
5450 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5454 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5458 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5462 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5465 #ifndef IPPROTO_IGMP
5466 #define IPPROTO_IGMP 2
5470 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5473 #ifndef IPPROTO_IGRP
5474 #define IPPROTO_IGRP 9
5477 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5481 #define IPPROTO_PIM 103
5485 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5488 #ifndef IPPROTO_VRRP
5489 #define IPPROTO_VRRP 112
5493 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5496 #ifndef IPPROTO_CARP
5497 #define IPPROTO_CARP 112
5501 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5505 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5509 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5513 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5517 break; // invalid syntax
5520 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5524 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5528 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5532 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5536 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5540 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5544 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5548 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5551 #ifndef IPPROTO_ICMPV6
5552 #define IPPROTO_ICMPV6 58
5555 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5559 #define IPPROTO_AH 51
5562 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5566 #define IPPROTO_ESP 50
5569 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5573 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5577 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5581 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5584 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5585 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5586 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5588 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5590 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5592 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5596 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5597 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5598 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5600 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5602 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5604 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5608 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5609 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5610 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5612 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5617 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5618 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5623 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5624 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5626 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5628 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5633 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5634 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5639 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5640 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5645 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5649 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5653 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5657 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5661 break; // invalid syntax
5668 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5672 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5675 * Catch errors reported by us and routines below us, and return NULL
5678 if (setjmp(cstate
->top_ctx
))
5681 return gen_proto_abbrev_internal(cstate
, proto
);
5684 static struct block
*
5685 gen_ipfrag(compiler_state_t
*cstate
)
5690 /* not IPv4 frag other than the first frag */
5691 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5692 b
= new_block(cstate
, JMP(BPF_JSET
));
5701 * Generate a comparison to a port value in the transport-layer header
5702 * at the specified offset from the beginning of that header.
5704 * XXX - this handles a variable-length prefix preceding the link-layer
5705 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5706 * variable-length link-layer headers (such as Token Ring or 802.11
5709 static struct block
*
5710 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5712 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5715 static struct block
*
5716 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5718 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5721 static struct block
*
5722 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5724 struct block
*b0
, *b1
, *tmp
;
5726 /* ip proto 'proto' and not a fragment other than the first fragment */
5727 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5728 b0
= gen_ipfrag(cstate
);
5733 b1
= gen_portatom(cstate
, 0, port
);
5737 b1
= gen_portatom(cstate
, 2, port
);
5741 tmp
= gen_portatom(cstate
, 0, port
);
5742 b1
= gen_portatom(cstate
, 2, port
);
5748 tmp
= gen_portatom(cstate
, 0, port
);
5749 b1
= gen_portatom(cstate
, 2, port
);
5754 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5758 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5762 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5766 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5770 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5774 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5786 static struct block
*
5787 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5789 struct block
*b0
, *b1
, *tmp
;
5794 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5795 * not LLC encapsulation with LLCSAP_IP.
5797 * For IEEE 802 networks - which includes 802.5 token ring
5798 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5799 * says that SNAP encapsulation is used, not LLC encapsulation
5802 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5803 * RFC 2225 say that SNAP encapsulation is used, not LLC
5804 * encapsulation with LLCSAP_IP.
5806 * So we always check for ETHERTYPE_IP.
5808 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5814 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5818 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5819 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5821 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5833 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5835 struct block
*b0
, *b1
, *tmp
;
5837 /* ip6 proto 'proto' */
5838 /* XXX - catch the first fragment of a fragmented packet? */
5839 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5843 b1
= gen_portatom6(cstate
, 0, port
);
5847 b1
= gen_portatom6(cstate
, 2, port
);
5851 tmp
= gen_portatom6(cstate
, 0, port
);
5852 b1
= gen_portatom6(cstate
, 2, port
);
5858 tmp
= gen_portatom6(cstate
, 0, port
);
5859 b1
= gen_portatom6(cstate
, 2, port
);
5871 static struct block
*
5872 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5874 struct block
*b0
, *b1
, *tmp
;
5876 /* link proto ip6 */
5877 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5883 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5887 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5888 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5890 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5901 /* gen_portrange code */
5902 static struct block
*
5903 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5906 struct block
*b1
, *b2
;
5910 * Reverse the order of the ports, so v1 is the lower one.
5919 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5920 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5927 static struct block
*
5928 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5929 bpf_u_int32 proto
, int dir
)
5931 struct block
*b0
, *b1
, *tmp
;
5933 /* ip proto 'proto' and not a fragment other than the first fragment */
5934 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5935 b0
= gen_ipfrag(cstate
);
5940 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5944 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5948 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5949 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5955 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5956 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5961 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5965 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5969 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5973 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5977 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5981 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5993 static struct block
*
5994 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5997 struct block
*b0
, *b1
, *tmp
;
6000 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6006 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6011 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6012 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6014 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6025 static struct block
*
6026 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6029 struct block
*b1
, *b2
;
6033 * Reverse the order of the ports, so v1 is the lower one.
6042 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6043 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6050 static struct block
*
6051 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6052 bpf_u_int32 proto
, int dir
)
6054 struct block
*b0
, *b1
, *tmp
;
6056 /* ip6 proto 'proto' */
6057 /* XXX - catch the first fragment of a fragmented packet? */
6058 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6062 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6066 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6070 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6071 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6077 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6078 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6090 static struct block
*
6091 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6094 struct block
*b0
, *b1
, *tmp
;
6096 /* link proto ip6 */
6097 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6103 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6108 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6109 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6111 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6123 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6132 v
= pcap_nametoproto(name
);
6133 if (v
== PROTO_UNDEF
)
6134 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6138 /* XXX should look up h/w protocol type based on cstate->linktype */
6139 v
= pcap_nametoeproto(name
);
6140 if (v
== PROTO_UNDEF
) {
6141 v
= pcap_nametollc(name
);
6142 if (v
== PROTO_UNDEF
)
6143 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6148 if (strcmp(name
, "esis") == 0)
6150 else if (strcmp(name
, "isis") == 0)
6152 else if (strcmp(name
, "clnp") == 0)
6155 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6165 #if !defined(NO_PROTOCHAIN)
6166 static struct block
*
6167 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6169 struct block
*b0
, *b
;
6170 struct slist
*s
[100];
6171 int fix2
, fix3
, fix4
, fix5
;
6172 int ahcheck
, again
, end
;
6174 int reg2
= alloc_reg(cstate
);
6176 memset(s
, 0, sizeof(s
));
6177 fix3
= fix4
= fix5
= 0;
6184 b0
= gen_protochain(cstate
, v
, Q_IP
);
6185 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6189 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6194 * We don't handle variable-length prefixes before the link-layer
6195 * header, or variable-length link-layer headers, here yet.
6196 * We might want to add BPF instructions to do the protochain
6197 * work, to simplify that and, on platforms that have a BPF
6198 * interpreter with the new instructions, let the filtering
6199 * be done in the kernel. (We already require a modified BPF
6200 * engine to do the protochain stuff, to support backward
6201 * branches, and backward branch support is unlikely to appear
6202 * in kernel BPF engines.)
6204 if (cstate
->off_linkpl
.is_variable
)
6205 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6208 * To quote a comment in optimize.c:
6210 * "These data structures are used in a Cocke and Schwartz style
6211 * value numbering scheme. Since the flowgraph is acyclic,
6212 * exit values can be propagated from a node's predecessors
6213 * provided it is uniquely defined."
6215 * "Acyclic" means "no backward branches", which means "no
6216 * loops", so we have to turn the optimizer off.
6218 cstate
->no_optimize
= 1;
6221 * s[0] is a dummy entry to protect other BPF insn from damage
6222 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6223 * hard to find interdependency made by jump table fixup.
6226 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6231 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6234 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6235 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6237 /* X = ip->ip_hl << 2 */
6238 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6239 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6244 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6246 /* A = ip6->ip_nxt */
6247 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6248 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6250 /* X = sizeof(struct ip6_hdr) */
6251 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6257 bpf_error(cstate
, "unsupported proto to gen_protochain");
6261 /* again: if (A == v) goto end; else fall through; */
6263 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6265 s
[i
]->s
.jt
= NULL
; /*later*/
6266 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6270 #ifndef IPPROTO_NONE
6271 #define IPPROTO_NONE 59
6273 /* if (A == IPPROTO_NONE) goto end */
6274 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6275 s
[i
]->s
.jt
= NULL
; /*later*/
6276 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6277 s
[i
]->s
.k
= IPPROTO_NONE
;
6278 s
[fix5
]->s
.jf
= s
[i
];
6282 if (proto
== Q_IPV6
) {
6283 int v6start
, v6end
, v6advance
, j
;
6286 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6287 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6288 s
[i
]->s
.jt
= NULL
; /*later*/
6289 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6290 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6291 s
[fix2
]->s
.jf
= s
[i
];
6293 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6294 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6295 s
[i
]->s
.jt
= NULL
; /*later*/
6296 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6297 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6299 /* if (A == IPPROTO_ROUTING) goto v6advance */
6300 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6301 s
[i
]->s
.jt
= NULL
; /*later*/
6302 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6303 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6305 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6306 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6307 s
[i
]->s
.jt
= NULL
; /*later*/
6308 s
[i
]->s
.jf
= NULL
; /*later*/
6309 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6319 * A = P[X + packet head];
6320 * X = X + (P[X + packet head + 1] + 1) * 8;
6322 /* A = P[X + packet head] */
6323 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6324 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6327 s
[i
] = new_stmt(cstate
, BPF_ST
);
6330 /* A = P[X + packet head + 1]; */
6331 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6332 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6335 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6339 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6343 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6347 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6350 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6354 /* goto again; (must use BPF_JA for backward jump) */
6355 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6356 s
[i
]->s
.k
= again
- i
- 1;
6357 s
[i
- 1]->s
.jf
= s
[i
];
6361 for (j
= v6start
; j
<= v6end
; j
++)
6362 s
[j
]->s
.jt
= s
[v6advance
];
6365 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6367 s
[fix2
]->s
.jf
= s
[i
];
6373 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6374 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6375 s
[i
]->s
.jt
= NULL
; /*later*/
6376 s
[i
]->s
.jf
= NULL
; /*later*/
6377 s
[i
]->s
.k
= IPPROTO_AH
;
6379 s
[fix3
]->s
.jf
= s
[ahcheck
];
6386 * X = X + (P[X + 1] + 2) * 4;
6389 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6391 /* A = P[X + packet head]; */
6392 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6393 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6396 s
[i
] = new_stmt(cstate
, BPF_ST
);
6400 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6403 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6407 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6409 /* A = P[X + packet head] */
6410 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6411 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6414 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6418 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6422 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6425 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6429 /* goto again; (must use BPF_JA for backward jump) */
6430 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6431 s
[i
]->s
.k
= again
- i
- 1;
6436 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6438 s
[fix2
]->s
.jt
= s
[end
];
6439 s
[fix4
]->s
.jf
= s
[end
];
6440 s
[fix5
]->s
.jt
= s
[end
];
6447 for (i
= 0; i
< max
- 1; i
++)
6448 s
[i
]->next
= s
[i
+ 1];
6449 s
[max
- 1]->next
= NULL
;
6454 b
= new_block(cstate
, JMP(BPF_JEQ
));
6455 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6458 free_reg(cstate
, reg2
);
6463 #endif /* !defined(NO_PROTOCHAIN) */
6466 * Generate code that checks whether the packet is a packet for protocol
6467 * <proto> and whether the type field in that protocol's header has
6468 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6469 * IP packet and checks the protocol number in the IP header against <v>.
6471 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6472 * against Q_IP and Q_IPV6.
6474 static struct block
*
6475 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6477 struct block
*b0
, *b1
;
6480 if (dir
!= Q_DEFAULT
)
6481 bpf_error(cstate
, "direction applied to 'proto'");
6485 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6486 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6491 return gen_linktype(cstate
, v
);
6495 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6496 * not LLC encapsulation with LLCSAP_IP.
6498 * For IEEE 802 networks - which includes 802.5 token ring
6499 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6500 * says that SNAP encapsulation is used, not LLC encapsulation
6503 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6504 * RFC 2225 say that SNAP encapsulation is used, not LLC
6505 * encapsulation with LLCSAP_IP.
6507 * So we always check for ETHERTYPE_IP.
6509 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6510 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6515 bpf_error(cstate
, "arp does not encapsulate another protocol");
6519 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6523 bpf_error(cstate
, "'sctp proto' is bogus");
6527 bpf_error(cstate
, "'tcp proto' is bogus");
6531 bpf_error(cstate
, "'udp proto' is bogus");
6535 bpf_error(cstate
, "'icmp proto' is bogus");
6539 bpf_error(cstate
, "'igmp proto' is bogus");
6543 bpf_error(cstate
, "'igrp proto' is bogus");
6547 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6551 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6555 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6559 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6563 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6567 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6571 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6573 * Also check for a fragment header before the final
6576 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6577 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6579 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6585 bpf_error(cstate
, "'icmp6 proto' is bogus");
6589 bpf_error(cstate
, "'ah proto' is bogus");
6593 bpf_error(cstate
, "'esp proto' is bogus");
6597 bpf_error(cstate
, "'pim proto' is bogus");
6601 bpf_error(cstate
, "'vrrp proto' is bogus");
6605 bpf_error(cstate
, "'aarp proto' is bogus");
6609 switch (cstate
->linktype
) {
6613 * Frame Relay packets typically have an OSI
6614 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6615 * generates code to check for all the OSI
6616 * NLPIDs, so calling it and then adding a check
6617 * for the particular NLPID for which we're
6618 * looking is bogus, as we can just check for
6621 * What we check for is the NLPID and a frame
6622 * control field value of UI, i.e. 0x03 followed
6625 * XXX - assumes a 2-byte Frame Relay header with
6626 * DLCI and flags. What if the address is longer?
6628 * XXX - what about SNAP-encapsulated frames?
6630 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6636 * Cisco uses an Ethertype lookalike - for OSI,
6639 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6640 /* OSI in C-HDLC is stuffed with a fudge byte */
6641 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6646 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6647 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6653 bpf_error(cstate
, "'esis proto' is bogus");
6657 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6659 * 4 is the offset of the PDU type relative to the IS-IS
6662 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6667 bpf_error(cstate
, "'clnp proto' is not supported");
6671 bpf_error(cstate
, "'stp proto' is bogus");
6675 bpf_error(cstate
, "'ipx proto' is bogus");
6679 bpf_error(cstate
, "'netbeui proto' is bogus");
6683 bpf_error(cstate
, "'l1 proto' is bogus");
6687 bpf_error(cstate
, "'l2 proto' is bogus");
6691 bpf_error(cstate
, "'iih proto' is bogus");
6695 bpf_error(cstate
, "'snp proto' is bogus");
6699 bpf_error(cstate
, "'csnp proto' is bogus");
6703 bpf_error(cstate
, "'psnp proto' is bogus");
6707 bpf_error(cstate
, "'lsp proto' is bogus");
6711 bpf_error(cstate
, "'radio proto' is bogus");
6715 bpf_error(cstate
, "'carp proto' is bogus");
6726 * Convert a non-numeric name to a port number.
6729 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6731 struct addrinfo hints
, *res
, *ai
;
6733 struct sockaddr_in
*in4
;
6735 struct sockaddr_in6
*in6
;
6740 * We check for both TCP and UDP in case there are
6741 * ambiguous entries.
6743 memset(&hints
, 0, sizeof(hints
));
6744 hints
.ai_family
= PF_UNSPEC
;
6745 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6746 hints
.ai_protocol
= ipproto
;
6747 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6754 * No such port. Just return -1.
6761 * We don't use strerror() because it's not
6762 * guaranteed to be thread-safe on all platforms
6763 * (probably because it might use a non-thread-local
6764 * buffer into which to format an error message
6765 * if the error code isn't one for which it has
6766 * a canned string; three cheers for C string
6769 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6771 port
= -2; /* a real error */
6777 * This is a real error, not just "there's
6778 * no such service name".
6780 * We don't use gai_strerror() because it's not
6781 * guaranteed to be thread-safe on all platforms
6782 * (probably because it might use a non-thread-local
6783 * buffer into which to format an error message
6784 * if the error code isn't one for which it has
6785 * a canned string; three cheers for C string
6788 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6790 port
= -2; /* a real error */
6795 * OK, we found it. Did it find anything?
6797 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6799 * Does it have an address?
6801 if (ai
->ai_addr
!= NULL
) {
6803 * Yes. Get a port number; we're done.
6805 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6806 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6807 port
= ntohs(in4
->sin_port
);
6811 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6812 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6813 port
= ntohs(in6
->sin6_port
);
6825 * Convert a string to a port number.
6828 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6838 * See if it's a number.
6840 ret
= stoulen(string
, string_size
, &val
, cstate
);
6844 /* Unknown port type - it's just a number. */
6845 *proto
= PROTO_UNDEF
;
6848 case STOULEN_NOT_OCTAL_NUMBER
:
6849 case STOULEN_NOT_HEX_NUMBER
:
6850 case STOULEN_NOT_DECIMAL_NUMBER
:
6852 * Not a valid number; try looking it up as a port.
6854 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6855 memcpy(cpy
, string
, string_size
);
6856 cpy
[string_size
] = '\0';
6857 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6858 if (tcp_port
== -2) {
6860 * We got a hard error; the error string has
6864 longjmp(cstate
->top_ctx
, 1);
6867 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6868 if (udp_port
== -2) {
6870 * We got a hard error; the error string has
6874 longjmp(cstate
->top_ctx
, 1);
6879 * We need to check /etc/services for ambiguous entries.
6880 * If we find an ambiguous entry, and it has the
6881 * same port number, change the proto to PROTO_UNDEF
6882 * so both TCP and UDP will be checked.
6884 if (tcp_port
>= 0) {
6885 val
= (bpf_u_int32
)tcp_port
;
6886 *proto
= IPPROTO_TCP
;
6887 if (udp_port
>= 0) {
6888 if (udp_port
== tcp_port
)
6889 *proto
= PROTO_UNDEF
;
6892 /* Can't handle ambiguous names that refer
6893 to different port numbers. */
6894 warning("ambiguous port %s in /etc/services",
6901 if (udp_port
>= 0) {
6902 val
= (bpf_u_int32
)udp_port
;
6903 *proto
= IPPROTO_UDP
;
6907 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6909 longjmp(cstate
->top_ctx
, 1);
6916 /* Error already set. */
6917 longjmp(cstate
->top_ctx
, 1);
6924 /* Should not happen */
6925 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6926 longjmp(cstate
->top_ctx
, 1);
6933 * Convert a string in the form PPP-PPP, which correspond to ports, to
6934 * a starting and ending port in a port range.
6937 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6938 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6941 const char *first
, *second
;
6942 size_t first_size
, second_size
;
6945 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6946 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6949 * Make sure there are no other hyphens.
6951 * XXX - we support named ports, but there are some port names
6952 * in /etc/services that include hyphens, so this would rule
6955 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6956 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6960 * Get the length of the first port.
6963 first_size
= hyphen_off
- string
;
6964 if (first_size
== 0) {
6965 /* Range of "-port", which we don't support. */
6966 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6970 * Try to convert it to a port.
6972 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6973 save_proto
= *proto
;
6976 * Get the length of the second port.
6978 second
= hyphen_off
+ 1;
6979 second_size
= strlen(second
);
6980 if (second_size
== 0) {
6981 /* Range of "port-", which we don't support. */
6982 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6986 * Try to convert it to a port.
6988 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6989 if (*proto
!= save_proto
)
6990 *proto
= PROTO_UNDEF
;
6994 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6996 int proto
= q
.proto
;
7000 bpf_u_int32 mask
, addr
;
7001 struct addrinfo
*res
, *res0
;
7002 struct sockaddr_in
*sin4
;
7005 struct sockaddr_in6
*sin6
;
7006 struct in6_addr mask128
;
7008 struct block
*b
, *tmp
;
7009 int port
, real_proto
;
7010 bpf_u_int32 port1
, port2
;
7013 * Catch errors reported by us and routines below us, and return NULL
7016 if (setjmp(cstate
->top_ctx
))
7022 addr
= pcap_nametonetaddr(name
);
7024 bpf_error(cstate
, "unknown network '%s'", name
);
7025 /* Left justify network addr and calculate its network mask */
7027 while (addr
&& (addr
& 0xff000000) == 0) {
7031 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
7035 if (proto
== Q_LINK
) {
7036 switch (cstate
->linktype
) {
7039 case DLT_NETANALYZER
:
7040 case DLT_NETANALYZER_TRANSPARENT
:
7041 eaddr
= pcap_ether_hostton(name
);
7044 "unknown ether host '%s'", name
);
7045 tmp
= gen_prevlinkhdr_check(cstate
);
7046 b
= gen_ehostop(cstate
, eaddr
, dir
);
7053 eaddr
= pcap_ether_hostton(name
);
7056 "unknown FDDI host '%s'", name
);
7057 b
= gen_fhostop(cstate
, eaddr
, dir
);
7062 eaddr
= pcap_ether_hostton(name
);
7065 "unknown token ring host '%s'", name
);
7066 b
= gen_thostop(cstate
, eaddr
, dir
);
7070 case DLT_IEEE802_11
:
7071 case DLT_PRISM_HEADER
:
7072 case DLT_IEEE802_11_RADIO_AVS
:
7073 case DLT_IEEE802_11_RADIO
:
7075 eaddr
= pcap_ether_hostton(name
);
7078 "unknown 802.11 host '%s'", name
);
7079 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
7083 case DLT_IP_OVER_FC
:
7084 eaddr
= pcap_ether_hostton(name
);
7087 "unknown Fibre Channel host '%s'", name
);
7088 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
7093 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7094 } else if (proto
== Q_DECNET
) {
7096 * A long time ago on Ultrix libpcap supported
7097 * translation of DECnet host names into DECnet
7098 * addresses, but this feature is history now.
7100 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
7103 memset(&mask128
, 0xff, sizeof(mask128
));
7105 res0
= res
= pcap_nametoaddrinfo(name
);
7107 bpf_error(cstate
, "unknown host '%s'", name
);
7114 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7115 tproto
== Q_DEFAULT
) {
7121 for (res
= res0
; res
; res
= res
->ai_next
) {
7122 switch (res
->ai_family
) {
7125 if (tproto
== Q_IPV6
)
7129 sin4
= (struct sockaddr_in
*)
7131 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7132 0xffffffff, tproto
, dir
, q
.addr
);
7136 if (tproto6
== Q_IP
)
7139 sin6
= (struct sockaddr_in6
*)
7141 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7142 &mask128
, tproto6
, dir
, q
.addr
);
7155 bpf_error(cstate
, "unknown host '%s'%s", name
,
7156 (proto
== Q_DEFAULT
)
7158 : " for specified address family");
7164 if (proto
!= Q_DEFAULT
&&
7165 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7166 bpf_error(cstate
, "illegal qualifier of 'port'");
7167 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7168 bpf_error(cstate
, "unknown port '%s'", name
);
7169 if (proto
== Q_UDP
) {
7170 if (real_proto
== IPPROTO_TCP
)
7171 bpf_error(cstate
, "port '%s' is tcp", name
);
7172 else if (real_proto
== IPPROTO_SCTP
)
7173 bpf_error(cstate
, "port '%s' is sctp", name
);
7175 /* override PROTO_UNDEF */
7176 real_proto
= IPPROTO_UDP
;
7178 if (proto
== Q_TCP
) {
7179 if (real_proto
== IPPROTO_UDP
)
7180 bpf_error(cstate
, "port '%s' is udp", name
);
7182 else if (real_proto
== IPPROTO_SCTP
)
7183 bpf_error(cstate
, "port '%s' is sctp", name
);
7185 /* override PROTO_UNDEF */
7186 real_proto
= IPPROTO_TCP
;
7188 if (proto
== Q_SCTP
) {
7189 if (real_proto
== IPPROTO_UDP
)
7190 bpf_error(cstate
, "port '%s' is udp", name
);
7192 else if (real_proto
== IPPROTO_TCP
)
7193 bpf_error(cstate
, "port '%s' is tcp", name
);
7195 /* override PROTO_UNDEF */
7196 real_proto
= IPPROTO_SCTP
;
7199 bpf_error(cstate
, "illegal port number %d < 0", port
);
7201 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7202 b
= gen_port(cstate
, port
, real_proto
, dir
);
7203 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7207 if (proto
!= Q_DEFAULT
&&
7208 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7209 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7210 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7211 if (proto
== Q_UDP
) {
7212 if (real_proto
== IPPROTO_TCP
)
7213 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7214 else if (real_proto
== IPPROTO_SCTP
)
7215 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7217 /* override PROTO_UNDEF */
7218 real_proto
= IPPROTO_UDP
;
7220 if (proto
== Q_TCP
) {
7221 if (real_proto
== IPPROTO_UDP
)
7222 bpf_error(cstate
, "port in range '%s' is udp", name
);
7223 else if (real_proto
== IPPROTO_SCTP
)
7224 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7226 /* override PROTO_UNDEF */
7227 real_proto
= IPPROTO_TCP
;
7229 if (proto
== Q_SCTP
) {
7230 if (real_proto
== IPPROTO_UDP
)
7231 bpf_error(cstate
, "port in range '%s' is udp", name
);
7232 else if (real_proto
== IPPROTO_TCP
)
7233 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7235 /* override PROTO_UNDEF */
7236 real_proto
= IPPROTO_SCTP
;
7239 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7241 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7243 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7244 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7249 eaddr
= pcap_ether_hostton(name
);
7251 bpf_error(cstate
, "unknown ether host: %s", name
);
7253 res
= pcap_nametoaddrinfo(name
);
7256 bpf_error(cstate
, "unknown host '%s'", name
);
7257 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7262 bpf_error(cstate
, "unknown host '%s'", name
);
7265 bpf_error(cstate
, "'gateway' not supported in this configuration");
7269 real_proto
= lookup_proto(cstate
, name
, proto
);
7270 if (real_proto
>= 0)
7271 return gen_proto(cstate
, real_proto
, proto
, dir
);
7273 bpf_error(cstate
, "unknown protocol: %s", name
);
7275 #if !defined(NO_PROTOCHAIN)
7277 real_proto
= lookup_proto(cstate
, name
, proto
);
7278 if (real_proto
>= 0)
7279 return gen_protochain(cstate
, real_proto
, proto
);
7281 bpf_error(cstate
, "unknown protocol: %s", name
);
7282 #endif /* !defined(NO_PROTOCHAIN) */
7293 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7294 bpf_u_int32 masklen
, struct qual q
)
7296 register int nlen
, mlen
;
7301 * Catch errors reported by us and routines below us, and return NULL
7304 if (setjmp(cstate
->top_ctx
))
7307 nlen
= pcapint_atoin(s1
, &n
);
7309 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7310 /* Promote short ipaddr */
7314 mlen
= pcapint_atoin(s2
, &m
);
7316 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7317 /* Promote short ipaddr */
7320 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7323 /* Convert mask len to mask */
7325 bpf_error(cstate
, "mask length must be <= 32");
7326 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7327 m
= (bpf_u_int32
)m64
;
7329 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7336 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7339 bpf_error(cstate
, "Mask syntax for networks only");
7346 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7354 * Catch errors reported by us and routines below us, and return NULL
7357 if (setjmp(cstate
->top_ctx
))
7364 * v contains a 32-bit unsigned parsed from a string of the
7365 * form {N}, which could be decimal, hexadecimal or octal.
7366 * Although it would be possible to use the value as a raw
7367 * 16-bit DECnet address when the value fits into 16 bits, this
7368 * would be a questionable feature: DECnet address wire
7369 * encoding is little-endian, so this would not work as
7370 * intuitively as the same works for [big-endian] IPv4
7371 * addresses (0x01020304 means 1.2.3.4).
7373 if (proto
== Q_DECNET
)
7374 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7376 } else if (proto
== Q_DECNET
) {
7378 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7379 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7380 * for a valid DECnet address.
7382 vlen
= pcapint_atodn(s
, &v
);
7384 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7387 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7388 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7391 vlen
= pcapint_atoin(s
, &v
);
7393 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7401 if (proto
== Q_DECNET
)
7402 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7403 else if (proto
== Q_LINK
) {
7404 bpf_error(cstate
, "illegal link layer address");
7407 if (s
== NULL
&& q
.addr
== Q_NET
) {
7408 /* Promote short net number */
7409 while (v
&& (v
& 0xff000000) == 0) {
7414 /* Promote short ipaddr */
7416 mask
<<= 32 - vlen
;
7418 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7423 proto
= IPPROTO_UDP
;
7424 else if (proto
== Q_TCP
)
7425 proto
= IPPROTO_TCP
;
7426 else if (proto
== Q_SCTP
)
7427 proto
= IPPROTO_SCTP
;
7428 else if (proto
== Q_DEFAULT
)
7429 proto
= PROTO_UNDEF
;
7431 bpf_error(cstate
, "illegal qualifier of 'port'");
7434 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7438 b
= gen_port(cstate
, v
, proto
, dir
);
7439 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7445 proto
= IPPROTO_UDP
;
7446 else if (proto
== Q_TCP
)
7447 proto
= IPPROTO_TCP
;
7448 else if (proto
== Q_SCTP
)
7449 proto
= IPPROTO_SCTP
;
7450 else if (proto
== Q_DEFAULT
)
7451 proto
= PROTO_UNDEF
;
7453 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7456 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7460 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7461 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7466 bpf_error(cstate
, "'gateway' requires a name");
7470 return gen_proto(cstate
, v
, proto
, dir
);
7472 #if !defined(NO_PROTOCHAIN)
7474 return gen_protochain(cstate
, v
, proto
);
7490 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7493 struct addrinfo
*res
;
7494 struct in6_addr
*addr
;
7495 struct in6_addr mask
;
7497 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7500 * Catch errors reported by us and routines below us, and return NULL
7503 if (setjmp(cstate
->top_ctx
))
7506 res
= pcap_nametoaddrinfo(s
);
7508 bpf_error(cstate
, "invalid ip6 address %s", s
);
7511 bpf_error(cstate
, "%s resolved to multiple address", s
);
7512 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7514 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7515 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7516 memset(&mask
, 0, sizeof(mask
));
7517 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7519 mask
.s6_addr
[masklen
/ 8] =
7520 (0xff << (8 - masklen
% 8)) & 0xff;
7523 memcpy(a
, addr
, sizeof(a
));
7524 memcpy(m
, &mask
, sizeof(m
));
7525 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7526 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7527 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7535 bpf_error(cstate
, "Mask syntax for networks only");
7539 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7545 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7552 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7554 struct block
*b
, *tmp
;
7557 * Catch errors reported by us and routines below us, and return NULL
7560 if (setjmp(cstate
->top_ctx
))
7563 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7564 cstate
->e
= pcap_ether_aton(s
);
7565 if (cstate
->e
== NULL
)
7566 bpf_error(cstate
, "malloc");
7567 switch (cstate
->linktype
) {
7569 case DLT_NETANALYZER
:
7570 case DLT_NETANALYZER_TRANSPARENT
:
7571 tmp
= gen_prevlinkhdr_check(cstate
);
7572 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7577 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7580 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7582 case DLT_IEEE802_11
:
7583 case DLT_PRISM_HEADER
:
7584 case DLT_IEEE802_11_RADIO_AVS
:
7585 case DLT_IEEE802_11_RADIO
:
7587 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7589 case DLT_IP_OVER_FC
:
7590 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7595 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7602 bpf_error(cstate
, "ethernet address used in non-ether expression");
7607 sappend(struct slist
*s0
, struct slist
*s1
)
7610 * This is definitely not the best way to do this, but the
7611 * lists will rarely get long.
7618 static struct slist
*
7619 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7623 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7628 static struct slist
*
7629 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7633 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7639 * Modify "index" to use the value stored into its register as an
7640 * offset relative to the beginning of the header for the protocol
7641 * "proto", and allocate a register and put an item "size" bytes long
7642 * (1, 2, or 4) at that offset into that register, making it the register
7645 static struct arth
*
7646 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7650 struct slist
*s
, *tmp
;
7652 int regno
= alloc_reg(cstate
);
7654 free_reg(cstate
, inst
->regno
);
7658 bpf_error(cstate
, "data size must be 1, 2, or 4");
7675 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7679 * The offset is relative to the beginning of the packet
7680 * data, if we have a radio header. (If we don't, this
7683 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7684 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7685 cstate
->linktype
!= DLT_PRISM_HEADER
)
7686 bpf_error(cstate
, "radio information not present in capture");
7689 * Load into the X register the offset computed into the
7690 * register specified by "index".
7692 s
= xfer_to_x(cstate
, inst
);
7695 * Load the item at that offset.
7697 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7699 sappend(inst
->s
, s
);
7704 * The offset is relative to the beginning of
7705 * the link-layer header.
7707 * XXX - what about ATM LANE? Should the index be
7708 * relative to the beginning of the AAL5 frame, so
7709 * that 0 refers to the beginning of the LE Control
7710 * field, or relative to the beginning of the LAN
7711 * frame, so that 0 refers, for Ethernet LANE, to
7712 * the beginning of the destination address?
7714 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7717 * If "s" is non-null, it has code to arrange that the
7718 * X register contains the length of the prefix preceding
7719 * the link-layer header. Add to it the offset computed
7720 * into the register specified by "index", and move that
7721 * into the X register. Otherwise, just load into the X
7722 * register the offset computed into the register specified
7726 sappend(s
, xfer_to_a(cstate
, inst
));
7727 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7728 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7730 s
= xfer_to_x(cstate
, inst
);
7733 * Load the item at the sum of the offset we've put in the
7734 * X register and the offset of the start of the link
7735 * layer header (which is 0 if the radio header is
7736 * variable-length; that header length is what we put
7737 * into the X register and then added to the index).
7739 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7740 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7742 sappend(inst
->s
, s
);
7756 * The offset is relative to the beginning of
7757 * the network-layer header.
7758 * XXX - are there any cases where we want
7759 * cstate->off_nl_nosnap?
7761 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7764 * If "s" is non-null, it has code to arrange that the
7765 * X register contains the variable part of the offset
7766 * of the link-layer payload. Add to it the offset
7767 * computed into the register specified by "index",
7768 * and move that into the X register. Otherwise, just
7769 * load into the X register the offset computed into
7770 * the register specified by "index".
7773 sappend(s
, xfer_to_a(cstate
, inst
));
7774 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7775 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7777 s
= xfer_to_x(cstate
, inst
);
7780 * Load the item at the sum of the offset we've put in the
7781 * X register, the offset of the start of the network
7782 * layer header from the beginning of the link-layer
7783 * payload, and the constant part of the offset of the
7784 * start of the link-layer payload.
7786 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7787 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7789 sappend(inst
->s
, s
);
7792 * Do the computation only if the packet contains
7793 * the protocol in question.
7795 b
= gen_proto_abbrev_internal(cstate
, proto
);
7797 gen_and(inst
->b
, b
);
7811 * The offset is relative to the beginning of
7812 * the transport-layer header.
7814 * Load the X register with the length of the IPv4 header
7815 * (plus the offset of the link-layer header, if it's
7816 * a variable-length header), in bytes.
7818 * XXX - are there any cases where we want
7819 * cstate->off_nl_nosnap?
7820 * XXX - we should, if we're built with
7821 * IPv6 support, generate code to load either
7822 * IPv4, IPv6, or both, as appropriate.
7824 s
= gen_loadx_iphdrlen(cstate
);
7827 * The X register now contains the sum of the variable
7828 * part of the offset of the link-layer payload and the
7829 * length of the network-layer header.
7831 * Load into the A register the offset relative to
7832 * the beginning of the transport layer header,
7833 * add the X register to that, move that to the
7834 * X register, and load with an offset from the
7835 * X register equal to the sum of the constant part of
7836 * the offset of the link-layer payload and the offset,
7837 * relative to the beginning of the link-layer payload,
7838 * of the network-layer header.
7840 sappend(s
, xfer_to_a(cstate
, inst
));
7841 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7842 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7843 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7844 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7845 sappend(inst
->s
, s
);
7848 * Do the computation only if the packet contains
7849 * the protocol in question - which is true only
7850 * if this is an IP datagram and is the first or
7851 * only fragment of that datagram.
7853 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7855 gen_and(inst
->b
, b
);
7856 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7861 * Do the computation only if the packet contains
7862 * the protocol in question.
7864 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7866 gen_and(inst
->b
, b
);
7870 * Check if we have an icmp6 next header
7872 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7874 gen_and(inst
->b
, b
);
7877 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7879 * If "s" is non-null, it has code to arrange that the
7880 * X register contains the variable part of the offset
7881 * of the link-layer payload. Add to it the offset
7882 * computed into the register specified by "index",
7883 * and move that into the X register. Otherwise, just
7884 * load into the X register the offset computed into
7885 * the register specified by "index".
7888 sappend(s
, xfer_to_a(cstate
, inst
));
7889 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7890 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7892 s
= xfer_to_x(cstate
, inst
);
7895 * Load the item at the sum of the offset we've put in the
7896 * X register, the offset of the start of the network
7897 * layer header from the beginning of the link-layer
7898 * payload, and the constant part of the offset of the
7899 * start of the link-layer payload.
7901 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7902 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7905 sappend(inst
->s
, s
);
7909 inst
->regno
= regno
;
7910 s
= new_stmt(cstate
, BPF_ST
);
7912 sappend(inst
->s
, s
);
7918 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7922 * Catch errors reported by us and routines below us, and return NULL
7925 if (setjmp(cstate
->top_ctx
))
7928 return gen_load_internal(cstate
, proto
, inst
, size
);
7931 static struct block
*
7932 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7933 struct arth
*a1
, int reversed
)
7935 struct slist
*s0
, *s1
, *s2
;
7936 struct block
*b
, *tmp
;
7938 s0
= xfer_to_x(cstate
, a1
);
7939 s1
= xfer_to_a(cstate
, a0
);
7940 if (code
== BPF_JEQ
) {
7941 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7942 b
= new_block(cstate
, JMP(code
));
7946 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7952 sappend(a0
->s
, a1
->s
);
7956 free_reg(cstate
, a0
->regno
);
7957 free_reg(cstate
, a1
->regno
);
7959 /* 'and' together protocol checks */
7962 gen_and(a0
->b
, tmp
= a1
->b
);
7976 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7977 struct arth
*a1
, int reversed
)
7980 * Catch errors reported by us and routines below us, and return NULL
7983 if (setjmp(cstate
->top_ctx
))
7986 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7990 gen_loadlen(compiler_state_t
*cstate
)
7997 * Catch errors reported by us and routines below us, and return NULL
8000 if (setjmp(cstate
->top_ctx
))
8003 regno
= alloc_reg(cstate
);
8004 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8005 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8006 s
->next
= new_stmt(cstate
, BPF_ST
);
8007 s
->next
->s
.k
= regno
;
8014 static struct arth
*
8015 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
8021 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
8023 reg
= alloc_reg(cstate
);
8025 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8027 s
->next
= new_stmt(cstate
, BPF_ST
);
8036 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
8039 * Catch errors reported by us and routines below us, and return NULL
8042 if (setjmp(cstate
->top_ctx
))
8045 return gen_loadi_internal(cstate
, val
);
8049 * The a_arg dance is to avoid annoying whining by compilers that
8050 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8051 * It's not *used* after setjmp returns.
8054 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
8056 struct arth
*a
= a_arg
;
8060 * Catch errors reported by us and routines below us, and return NULL
8063 if (setjmp(cstate
->top_ctx
))
8066 s
= xfer_to_a(cstate
, a
);
8068 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
8071 s
= new_stmt(cstate
, BPF_ST
);
8079 * The a0_arg dance is to avoid annoying whining by compilers that
8080 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8081 * It's not *used* after setjmp returns.
8084 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
8087 struct arth
*a0
= a0_arg
;
8088 struct slist
*s0
, *s1
, *s2
;
8091 * Catch errors reported by us and routines below us, and return NULL
8094 if (setjmp(cstate
->top_ctx
))
8098 * Disallow division by, or modulus by, zero; we do this here
8099 * so that it gets done even if the optimizer is disabled.
8101 * Also disallow shifts by a value greater than 31; we do this
8102 * here, for the same reason.
8104 if (code
== BPF_DIV
) {
8105 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8106 bpf_error(cstate
, "division by zero");
8107 } else if (code
== BPF_MOD
) {
8108 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
8109 bpf_error(cstate
, "modulus by zero");
8110 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
8111 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
8112 bpf_error(cstate
, "shift by more than 31 bits");
8114 s0
= xfer_to_x(cstate
, a1
);
8115 s1
= xfer_to_a(cstate
, a0
);
8116 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
8121 sappend(a0
->s
, a1
->s
);
8123 free_reg(cstate
, a0
->regno
);
8124 free_reg(cstate
, a1
->regno
);
8126 s0
= new_stmt(cstate
, BPF_ST
);
8127 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8134 * Initialize the table of used registers and the current register.
8137 init_regs(compiler_state_t
*cstate
)
8140 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8144 * Return the next free register.
8147 alloc_reg(compiler_state_t
*cstate
)
8149 int n
= BPF_MEMWORDS
;
8152 if (cstate
->regused
[cstate
->curreg
])
8153 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8155 cstate
->regused
[cstate
->curreg
] = 1;
8156 return cstate
->curreg
;
8159 bpf_error(cstate
, "too many registers needed to evaluate expression");
8164 * Return a register to the table so it can
8168 free_reg(compiler_state_t
*cstate
, int n
)
8170 cstate
->regused
[n
] = 0;
8173 static struct block
*
8174 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8179 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8180 b
= new_block(cstate
, JMP(jmp
));
8188 gen_greater(compiler_state_t
*cstate
, int n
)
8191 * Catch errors reported by us and routines below us, and return NULL
8194 if (setjmp(cstate
->top_ctx
))
8197 return gen_len(cstate
, BPF_JGE
, n
);
8201 * Actually, this is less than or equal.
8204 gen_less(compiler_state_t
*cstate
, int n
)
8209 * Catch errors reported by us and routines below us, and return NULL
8212 if (setjmp(cstate
->top_ctx
))
8215 b
= gen_len(cstate
, BPF_JGT
, n
);
8222 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8223 * the beginning of the link-layer header.
8224 * XXX - that means you can't test values in the radiotap header, but
8225 * as that header is difficult if not impossible to parse generally
8226 * without a loop, that might not be a severe problem. A new keyword
8227 * "radio" could be added for that, although what you'd really want
8228 * would be a way of testing particular radio header values, which
8229 * would generate code appropriate to the radio header in question.
8232 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8238 * Catch errors reported by us and routines below us, and return NULL
8241 if (setjmp(cstate
->top_ctx
))
8249 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8252 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8256 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8260 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8264 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8268 b
= new_block(cstate
, JMP(BPF_JEQ
));
8276 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8278 bpf_u_int32 hostmask
;
8279 struct block
*b0
, *b1
, *b2
;
8280 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8283 * Catch errors reported by us and routines below us, and return NULL
8286 if (setjmp(cstate
->top_ctx
))
8293 switch (cstate
->linktype
) {
8295 case DLT_ARCNET_LINUX
:
8296 // ARCnet broadcast is [8-bit] destination address 0.
8297 return gen_ahostop(cstate
, 0, Q_DST
);
8299 case DLT_NETANALYZER
:
8300 case DLT_NETANALYZER_TRANSPARENT
:
8301 b1
= gen_prevlinkhdr_check(cstate
);
8302 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8307 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8309 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8310 case DLT_IEEE802_11
:
8311 case DLT_PRISM_HEADER
:
8312 case DLT_IEEE802_11_RADIO_AVS
:
8313 case DLT_IEEE802_11_RADIO
:
8315 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8316 case DLT_IP_OVER_FC
:
8317 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8319 fail_kw_on_dlt(cstate
, "broadcast");
8324 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8325 * as an indication that we don't know the netmask, and fail
8328 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8329 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8330 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8331 hostmask
= ~cstate
->netmask
;
8332 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8333 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8338 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8343 * Generate code to test the low-order bit of a MAC address (that's
8344 * the bottom bit of the *first* byte).
8346 static struct block
*
8347 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8349 register struct block
*b0
;
8350 register struct slist
*s
;
8352 /* link[offset] & 1 != 0 */
8353 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8354 b0
= new_block(cstate
, JMP(BPF_JSET
));
8361 gen_multicast(compiler_state_t
*cstate
, int proto
)
8363 register struct block
*b0
, *b1
, *b2
;
8364 register struct slist
*s
;
8367 * Catch errors reported by us and routines below us, and return NULL
8370 if (setjmp(cstate
->top_ctx
))
8377 switch (cstate
->linktype
) {
8379 case DLT_ARCNET_LINUX
:
8380 // ARCnet multicast is the same as broadcast.
8381 return gen_ahostop(cstate
, 0, Q_DST
);
8383 case DLT_NETANALYZER
:
8384 case DLT_NETANALYZER_TRANSPARENT
:
8385 b1
= gen_prevlinkhdr_check(cstate
);
8386 /* ether[0] & 1 != 0 */
8387 b0
= gen_mac_multicast(cstate
, 0);
8393 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8395 * XXX - was that referring to bit-order issues?
8397 /* fddi[1] & 1 != 0 */
8398 return gen_mac_multicast(cstate
, 1);
8400 /* tr[2] & 1 != 0 */
8401 return gen_mac_multicast(cstate
, 2);
8402 case DLT_IEEE802_11
:
8403 case DLT_PRISM_HEADER
:
8404 case DLT_IEEE802_11_RADIO_AVS
:
8405 case DLT_IEEE802_11_RADIO
:
8410 * For control frames, there is no DA.
8412 * For management frames, DA is at an
8413 * offset of 4 from the beginning of
8416 * For data frames, DA is at an offset
8417 * of 4 from the beginning of the packet
8418 * if To DS is clear and at an offset of
8419 * 16 from the beginning of the packet
8424 * Generate the tests to be done for data frames.
8426 * First, check for To DS set, i.e. "link[1] & 0x01".
8428 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8429 b1
= new_block(cstate
, JMP(BPF_JSET
));
8430 b1
->s
.k
= 0x01; /* To DS */
8434 * If To DS is set, the DA is at 16.
8436 b0
= gen_mac_multicast(cstate
, 16);
8440 * Now, check for To DS not set, i.e. check
8441 * "!(link[1] & 0x01)".
8443 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8444 b2
= new_block(cstate
, JMP(BPF_JSET
));
8445 b2
->s
.k
= 0x01; /* To DS */
8450 * If To DS is not set, the DA is at 4.
8452 b1
= gen_mac_multicast(cstate
, 4);
8456 * Now OR together the last two checks. That gives
8457 * the complete set of checks for data frames.
8462 * Now check for a data frame.
8463 * I.e, check "link[0] & 0x08".
8465 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8466 b1
= new_block(cstate
, JMP(BPF_JSET
));
8471 * AND that with the checks done for data frames.
8476 * If the high-order bit of the type value is 0, this
8477 * is a management frame.
8478 * I.e, check "!(link[0] & 0x08)".
8480 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8481 b2
= new_block(cstate
, JMP(BPF_JSET
));
8487 * For management frames, the DA is at 4.
8489 b1
= gen_mac_multicast(cstate
, 4);
8493 * OR that with the checks done for data frames.
8494 * That gives the checks done for management and
8500 * If the low-order bit of the type value is 1,
8501 * this is either a control frame or a frame
8502 * with a reserved type, and thus not a
8505 * I.e., check "!(link[0] & 0x04)".
8507 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8508 b1
= new_block(cstate
, JMP(BPF_JSET
));
8514 * AND that with the checks for data and management
8519 case DLT_IP_OVER_FC
:
8520 b0
= gen_mac_multicast(cstate
, 2);
8525 fail_kw_on_dlt(cstate
, "multicast");
8529 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8530 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8535 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8536 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8540 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8546 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8547 * we can look at special meta-data in the filter expression; otherwise we
8548 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8549 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8550 * pcap_activate() conditionally sets.
8553 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8555 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8557 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8559 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8564 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8566 register struct block
*b0
;
8569 * Catch errors reported by us and routines below us, and return NULL
8572 if (setjmp(cstate
->top_ctx
))
8576 * Only some data link types support ifindex qualifiers.
8578 switch (cstate
->linktype
) {
8579 case DLT_LINUX_SLL2
:
8580 /* match packets on this interface */
8581 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8584 #if defined(__linux__)
8585 require_basic_bpf_extensions(cstate
, "ifindex");
8587 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8589 #else /* defined(__linux__) */
8590 fail_kw_on_dlt(cstate
, "ifindex");
8592 #endif /* defined(__linux__) */
8598 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8599 * Outbound traffic is sent by this machine, while inbound traffic is
8600 * sent by a remote machine (and may include packets destined for a
8601 * unicast or multicast link-layer address we are not subscribing to).
8602 * These are the same definitions implemented by pcap_setdirection().
8603 * Capturing only unicast traffic destined for this host is probably
8604 * better accomplished using a higher-layer filter.
8607 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8609 register struct block
*b0
;
8612 * Catch errors reported by us and routines below us, and return NULL
8615 if (setjmp(cstate
->top_ctx
))
8619 * Only some data link types support inbound/outbound qualifiers.
8621 switch (cstate
->linktype
) {
8623 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8624 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8628 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8629 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8633 /* match outgoing packets */
8634 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8636 /* to filter on inbound traffic, invert the match */
8641 case DLT_LINUX_SLL2
:
8642 /* match outgoing packets */
8643 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8645 /* to filter on inbound traffic, invert the match */
8651 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8652 outbound
? PF_OUT
: PF_IN
);
8656 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8659 case DLT_JUNIPER_MFR
:
8660 case DLT_JUNIPER_MLFR
:
8661 case DLT_JUNIPER_MLPPP
:
8662 case DLT_JUNIPER_ATM1
:
8663 case DLT_JUNIPER_ATM2
:
8664 case DLT_JUNIPER_PPPOE
:
8665 case DLT_JUNIPER_PPPOE_ATM
:
8666 case DLT_JUNIPER_GGSN
:
8667 case DLT_JUNIPER_ES
:
8668 case DLT_JUNIPER_MONITOR
:
8669 case DLT_JUNIPER_SERVICES
:
8670 case DLT_JUNIPER_ETHER
:
8671 case DLT_JUNIPER_PPP
:
8672 case DLT_JUNIPER_FRELAY
:
8673 case DLT_JUNIPER_CHDLC
:
8674 case DLT_JUNIPER_VP
:
8675 case DLT_JUNIPER_ST
:
8676 case DLT_JUNIPER_ISM
:
8677 case DLT_JUNIPER_VS
:
8678 case DLT_JUNIPER_SRX_E2E
:
8679 case DLT_JUNIPER_FIBRECHANNEL
:
8680 case DLT_JUNIPER_ATM_CEMIC
:
8681 /* juniper flags (including direction) are stored
8682 * the byte after the 3-byte magic number */
8683 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8688 * If we have packet meta-data indicating a direction,
8689 * and that metadata can be checked by BPF code, check
8690 * it. Otherwise, give up, as this link-layer type has
8691 * nothing in the packet data.
8693 * Currently, the only platform where a BPF filter can
8694 * check that metadata is Linux with the in-kernel
8695 * BPF interpreter. If other packet capture mechanisms
8696 * and BPF filters also supported this, it would be
8697 * nice. It would be even better if they made that
8698 * metadata available so that we could provide it
8699 * with newer capture APIs, allowing it to be saved
8702 #if defined(__linux__)
8703 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8704 /* match outgoing packets */
8705 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8708 /* to filter on inbound traffic, invert the match */
8711 #else /* defined(__linux__) */
8712 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8714 #endif /* defined(__linux__) */
8719 /* PF firewall log matched interface */
8721 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8727 * Catch errors reported by us and routines below us, and return NULL
8730 if (setjmp(cstate
->top_ctx
))
8733 assert_pflog(cstate
, "ifname");
8735 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8736 off
= offsetof(struct pfloghdr
, ifname
);
8737 if (strlen(ifname
) >= len
) {
8738 bpf_error(cstate
, "ifname interface names can only be %d characters",
8742 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8743 (const u_char
*)ifname
);
8747 /* PF firewall log ruleset name */
8749 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8754 * Catch errors reported by us and routines below us, and return NULL
8757 if (setjmp(cstate
->top_ctx
))
8760 assert_pflog(cstate
, "ruleset");
8762 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8763 bpf_error(cstate
, "ruleset names can only be %ld characters",
8764 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8768 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8769 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8773 /* PF firewall log rule number */
8775 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8780 * Catch errors reported by us and routines below us, and return NULL
8783 if (setjmp(cstate
->top_ctx
))
8786 assert_pflog(cstate
, "rnr");
8788 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8793 /* PF firewall log sub-rule number */
8795 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8800 * Catch errors reported by us and routines below us, and return NULL
8803 if (setjmp(cstate
->top_ctx
))
8806 assert_pflog(cstate
, "srnr");
8808 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8813 /* PF firewall log reason code */
8815 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8820 * Catch errors reported by us and routines below us, and return NULL
8823 if (setjmp(cstate
->top_ctx
))
8826 assert_pflog(cstate
, "reason");
8828 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8829 (bpf_u_int32
)reason
);
8833 /* PF firewall log action */
8835 gen_pf_action(compiler_state_t
*cstate
, int action
)
8840 * Catch errors reported by us and routines below us, and return NULL
8843 if (setjmp(cstate
->top_ctx
))
8846 assert_pflog(cstate
, "action");
8848 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8849 (bpf_u_int32
)action
);
8853 /* IEEE 802.11 wireless header */
8855 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8860 * Catch errors reported by us and routines below us, and return NULL
8863 if (setjmp(cstate
->top_ctx
))
8866 switch (cstate
->linktype
) {
8868 case DLT_IEEE802_11
:
8869 case DLT_PRISM_HEADER
:
8870 case DLT_IEEE802_11_RADIO_AVS
:
8871 case DLT_IEEE802_11_RADIO
:
8873 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8877 fail_kw_on_dlt(cstate
, "type/subtype");
8885 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8890 * Catch errors reported by us and routines below us, and return NULL
8893 if (setjmp(cstate
->top_ctx
))
8896 switch (cstate
->linktype
) {
8898 case DLT_IEEE802_11
:
8899 case DLT_PRISM_HEADER
:
8900 case DLT_IEEE802_11_RADIO_AVS
:
8901 case DLT_IEEE802_11_RADIO
:
8906 fail_kw_on_dlt(cstate
, "dir");
8910 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8911 IEEE80211_FC1_DIR_MASK
);
8916 // Process an ARCnet host address string.
8918 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8921 * Catch errors reported by us and routines below us, and return NULL
8924 if (setjmp(cstate
->top_ctx
))
8927 switch (cstate
->linktype
) {
8930 case DLT_ARCNET_LINUX
:
8931 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8932 q
.proto
== Q_LINK
) {
8935 * The lexer currently defines the address format in a
8936 * way that makes this error condition never true.
8937 * Let's check it anyway in case this part of the lexer
8938 * changes in future.
8940 if (! pcapint_atoan(s
, &addr
))
8941 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8942 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8944 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8948 bpf_error(cstate
, "aid supported only on ARCnet");
8953 // Compare an ARCnet host address with the given value.
8954 static struct block
*
8955 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8957 register struct block
*b0
, *b1
;
8961 * ARCnet is different from Ethernet: the source address comes before
8962 * the destination address, each is one byte long. This holds for all
8963 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8964 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8965 * by Datapoint (document number 61610-01).
8968 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8971 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8974 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8975 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8981 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8982 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8992 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8999 static struct block
*
9000 gen_vlan_tpid_test(compiler_state_t
*cstate
)
9002 struct block
*b0
, *b1
;
9004 /* check for VLAN, including 802.1ad and QinQ */
9005 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
9006 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
9009 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
9015 static struct block
*
9016 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
9018 if (vlan_num
> 0x0fff) {
9019 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
9022 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
9025 static struct block
*
9026 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9029 struct block
*b0
, *b1
;
9031 b0
= gen_vlan_tpid_test(cstate
);
9034 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
9040 * Both payload and link header type follow the VLAN tags so that
9041 * both need to be updated.
9043 cstate
->off_linkpl
.constant_part
+= 4;
9044 cstate
->off_linktype
.constant_part
+= 4;
9049 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9050 /* add v to variable part of off */
9052 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
9053 bpf_u_int32 v
, struct slist
*s
)
9057 if (!off
->is_variable
)
9058 off
->is_variable
= 1;
9060 off
->reg
= alloc_reg(cstate
);
9062 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9065 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
9068 s2
= new_stmt(cstate
, BPF_ST
);
9074 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9075 * and link type offsets first
9078 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
9082 /* offset determined at run time, shift variable part */
9084 cstate
->is_vlan_vloffset
= 1;
9085 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
9086 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
9088 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9089 sappend(s
.next
, b_tpid
->head
->stmts
);
9090 b_tpid
->head
->stmts
= s
.next
;
9094 * patch block b_vid (VLAN id test) to load VID value either from packet
9095 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9098 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
9100 struct slist
*s
, *s2
, *sjeq
;
9103 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9104 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9106 /* true -> next instructions, false -> beginning of b_vid */
9107 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
9109 sjeq
->s
.jf
= b_vid
->stmts
;
9112 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
9113 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
9117 /* Jump to the test in b_vid. We need to jump one instruction before
9118 * the end of the b_vid block so that we only skip loading the TCI
9119 * from packet data and not the 'and' instruction extracting VID.
9122 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
9124 s2
= new_stmt(cstate
, JMP(BPF_JA
));
9128 /* insert our statements at the beginning of b_vid */
9129 sappend(s
, b_vid
->stmts
);
9134 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9135 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9136 * tag can be either in metadata or in packet data; therefore if the
9137 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9138 * header for VLAN tag. As the decision is done at run time, we need
9139 * update variable part of the offsets
9141 static struct block
*
9142 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9145 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9148 /* generate new filter code based on extracting packet
9150 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9151 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9153 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9158 * This is tricky. We need to insert the statements updating variable
9159 * parts of offsets before the traditional TPID and VID tests so
9160 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9161 * we do not want this update to affect those checks. That's why we
9162 * generate both test blocks first and insert the statements updating
9163 * variable parts of both offsets after that. This wouldn't work if
9164 * there already were variable length link header when entering this
9165 * function but gen_vlan_bpf_extensions() isn't called in that case.
9167 b_tpid
= gen_vlan_tpid_test(cstate
);
9169 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9171 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9176 gen_vlan_patch_vid_test(cstate
, b_vid
);
9186 * support IEEE 802.1Q VLAN trunk over ethernet
9189 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9194 * Catch errors reported by us and routines below us, and return NULL
9197 if (setjmp(cstate
->top_ctx
))
9200 /* can't check for VLAN-encapsulated packets inside MPLS */
9201 if (cstate
->label_stack_depth
> 0)
9202 bpf_error(cstate
, "no VLAN match after MPLS");
9205 * Check for a VLAN packet, and then change the offsets to point
9206 * to the type and data fields within the VLAN packet. Just
9207 * increment the offsets, so that we can support a hierarchy, e.g.
9208 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9211 * XXX - this is a bit of a kludge. If we were to split the
9212 * compiler into a parser that parses an expression and
9213 * generates an expression tree, and a code generator that
9214 * takes an expression tree (which could come from our
9215 * parser or from some other parser) and generates BPF code,
9216 * we could perhaps make the offsets parameters of routines
9217 * and, in the handler for an "AND" node, pass to subnodes
9218 * other than the VLAN node the adjusted offsets.
9220 * This would mean that "vlan" would, instead of changing the
9221 * behavior of *all* tests after it, change only the behavior
9222 * of tests ANDed with it. That would change the documented
9223 * semantics of "vlan", which might break some expressions.
9224 * However, it would mean that "(vlan and ip) or ip" would check
9225 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9226 * checking only for VLAN-encapsulated IP, so that could still
9227 * be considered worth doing; it wouldn't break expressions
9228 * that are of the form "vlan and ..." or "vlan N and ...",
9229 * which I suspect are the most common expressions involving
9230 * "vlan". "vlan or ..." doesn't necessarily do what the user
9231 * would really want, now, as all the "or ..." tests would
9232 * be done assuming a VLAN, even though the "or" could be viewed
9233 * as meaning "or, if this isn't a VLAN packet...".
9235 switch (cstate
->linktype
) {
9239 * Newer version of the Linux kernel pass around
9240 * packets in which the VLAN tag has been removed
9241 * from the packet data and put into metadata.
9243 * This requires special treatment.
9245 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9246 /* Verify that this is the outer part of the packet and
9247 * not encapsulated somehow. */
9248 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9249 cstate
->off_linkhdr
.constant_part
==
9250 cstate
->off_outermostlinkhdr
.constant_part
) {
9252 * Do we need special VLAN handling?
9254 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9255 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9258 b0
= gen_vlan_no_bpf_extensions(cstate
,
9259 vlan_num
, has_vlan_tag
);
9262 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9266 case DLT_NETANALYZER
:
9267 case DLT_NETANALYZER_TRANSPARENT
:
9268 case DLT_IEEE802_11
:
9269 case DLT_PRISM_HEADER
:
9270 case DLT_IEEE802_11_RADIO_AVS
:
9271 case DLT_IEEE802_11_RADIO
:
9273 * These are either Ethernet packets with an additional
9274 * metadata header (the NetAnalyzer types), or 802.11
9275 * packets, possibly with an additional metadata header.
9277 * For the first of those, the VLAN tag is in the normal
9278 * place, so the special-case handling above isn't
9281 * For the second of those, we don't do the special-case
9284 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9288 bpf_error(cstate
, "no VLAN support for %s",
9289 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9293 cstate
->vlan_stack_depth
++;
9301 * The label_num_arg dance is to avoid annoying whining by compilers that
9302 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9303 * It's not *used* after setjmp returns.
9305 static struct block
*
9306 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9309 struct block
*b0
, *b1
;
9311 if (cstate
->label_stack_depth
> 0) {
9312 /* just match the bottom-of-stack bit clear */
9313 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9316 * We're not in an MPLS stack yet, so check the link-layer
9317 * type against MPLS.
9319 switch (cstate
->linktype
) {
9321 case DLT_C_HDLC
: /* fall through */
9324 case DLT_NETANALYZER
:
9325 case DLT_NETANALYZER_TRANSPARENT
:
9326 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9330 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9333 /* FIXME add other DLT_s ...
9334 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9335 * leave it for now */
9338 bpf_error(cstate
, "no MPLS support for %s",
9339 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9344 /* If a specific MPLS label is requested, check it */
9345 if (has_label_num
) {
9346 if (label_num
> 0xFFFFF) {
9347 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9348 label_num
, 0xFFFFF);
9350 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9351 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9352 0xfffff000); /* only compare the first 20 bits */
9358 * Change the offsets to point to the type and data fields within
9359 * the MPLS packet. Just increment the offsets, so that we
9360 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9361 * capture packets with an outer label of 100000 and an inner
9364 * Increment the MPLS stack depth as well; this indicates that
9365 * we're checking MPLS-encapsulated headers, to make sure higher
9366 * level code generators don't try to match against IP-related
9367 * protocols such as Q_ARP, Q_RARP etc.
9369 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9371 cstate
->off_nl_nosnap
+= 4;
9372 cstate
->off_nl
+= 4;
9373 cstate
->label_stack_depth
++;
9378 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9381 * Catch errors reported by us and routines below us, and return NULL
9384 if (setjmp(cstate
->top_ctx
))
9387 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9391 * Support PPPOE discovery and session.
9394 gen_pppoed(compiler_state_t
*cstate
)
9397 * Catch errors reported by us and routines below us, and return NULL
9400 if (setjmp(cstate
->top_ctx
))
9403 /* check for PPPoE discovery */
9404 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9408 * RFC 2516 Section 4:
9410 * The Ethernet payload for PPPoE is as follows:
9413 * 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
9414 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9415 * | VER | TYPE | CODE | SESSION_ID |
9416 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9417 * | LENGTH | payload ~
9418 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9421 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9423 struct block
*b0
, *b1
;
9426 * Catch errors reported by us and routines below us, and return NULL
9429 if (setjmp(cstate
->top_ctx
))
9433 * Test against the PPPoE session link-layer type.
9435 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9437 /* If a specific session is requested, check PPPoE session id */
9439 if (sess_num
> UINT16_MAX
) {
9440 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9441 sess_num
, UINT16_MAX
);
9443 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9449 * Change the offsets to point to the type and data fields within
9450 * the PPP packet, and note that this is PPPoE rather than
9453 * XXX - this is a bit of a kludge. See the comments in
9456 * The "network-layer" protocol is PPPoE, which has a 6-byte
9457 * PPPoE header, followed by a PPP packet.
9459 * There is no HDLC encapsulation for the PPP packet (it's
9460 * encapsulated in PPPoES instead), so the link-layer type
9461 * starts at the first byte of the PPP packet. For PPPoE,
9462 * that offset is relative to the beginning of the total
9463 * link-layer payload, including any 802.2 LLC header, so
9464 * it's 6 bytes past cstate->off_nl.
9466 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9467 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9468 cstate
->off_linkpl
.reg
);
9470 cstate
->off_linktype
= cstate
->off_linkhdr
;
9471 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9474 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9479 /* Check that this is Geneve and the VNI is correct if
9480 * specified. Parameterized to handle both IPv4 and IPv6. */
9481 static struct block
*
9482 gen_geneve_check(compiler_state_t
*cstate
,
9483 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9484 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9486 struct block
*b0
, *b1
;
9488 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9490 /* Check that we are operating on version 0. Otherwise, we
9491 * can't decode the rest of the fields. The version is 2 bits
9492 * in the first byte of the Geneve header. */
9493 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9498 if (vni
> 0xffffff) {
9499 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9502 vni
<<= 8; /* VNI is in the upper 3 bytes */
9503 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9511 /* The IPv4 and IPv6 Geneve checks need to do two things:
9512 * - Verify that this actually is Geneve with the right VNI.
9513 * - Place the IP header length (plus variable link prefix if
9514 * needed) into register A to be used later to compute
9515 * the inner packet offsets. */
9516 static struct block
*
9517 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9519 struct block
*b0
, *b1
;
9520 struct slist
*s
, *s1
;
9522 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9524 /* Load the IP header length into A. */
9525 s
= gen_loadx_iphdrlen(cstate
);
9527 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9530 /* Forcibly append these statements to the true condition
9531 * of the protocol check by creating a new block that is
9532 * always true and ANDing them. */
9533 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9542 static struct block
*
9543 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9545 struct block
*b0
, *b1
;
9546 struct slist
*s
, *s1
;
9548 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9550 /* Load the IP header length. We need to account for a
9551 * variable length link prefix if there is one. */
9552 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9554 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9558 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9562 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9566 /* Forcibly append these statements to the true condition
9567 * of the protocol check by creating a new block that is
9568 * always true and ANDing them. */
9569 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9572 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9581 /* We need to store three values based on the Geneve header::
9582 * - The offset of the linktype.
9583 * - The offset of the end of the Geneve header.
9584 * - The offset of the end of the encapsulated MAC header. */
9585 static struct slist
*
9586 gen_geneve_offsets(compiler_state_t
*cstate
)
9588 struct slist
*s
, *s1
, *s_proto
;
9590 /* First we need to calculate the offset of the Geneve header
9591 * itself. This is composed of the IP header previously calculated
9592 * (include any variable link prefix) and stored in A plus the
9593 * fixed sized headers (fixed link prefix, MAC length, and UDP
9595 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9596 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9598 /* Stash this in X since we'll need it later. */
9599 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9602 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9604 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9608 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9609 cstate
->off_linktype
.is_variable
= 1;
9610 cstate
->off_linktype
.constant_part
= 0;
9612 s1
= new_stmt(cstate
, BPF_ST
);
9613 s1
->s
.k
= cstate
->off_linktype
.reg
;
9616 /* Load the Geneve option length and mask and shift to get the
9617 * number of bytes. It is stored in the first byte of the Geneve
9619 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9623 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9627 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9631 /* Add in the rest of the Geneve base header. */
9632 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9636 /* Add the Geneve header length to its offset and store. */
9637 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9641 /* Set the encapsulated type as Ethernet. Even though we may
9642 * not actually have Ethernet inside there are two reasons this
9644 * - The linktype field is always in EtherType format regardless
9645 * of whether it is in Geneve or an inner Ethernet frame.
9646 * - The only link layer that we have specific support for is
9647 * Ethernet. We will confirm that the packet actually is
9648 * Ethernet at runtime before executing these checks. */
9649 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9651 s1
= new_stmt(cstate
, BPF_ST
);
9652 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9655 /* Calculate whether we have an Ethernet header or just raw IP/
9656 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9657 * and linktype by 14 bytes so that the network header can be found
9658 * seamlessly. Otherwise, keep what we've calculated already. */
9660 /* We have a bare jmp so we can't use the optimizer. */
9661 cstate
->no_optimize
= 1;
9663 /* Load the EtherType in the Geneve header, 2 bytes in. */
9664 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9668 /* Load X with the end of the Geneve header. */
9669 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9670 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9673 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9674 * end of this check, we should have the total length in X. In
9675 * the non-Ethernet case, it's already there. */
9676 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9677 s_proto
->s
.k
= ETHERTYPE_TEB
;
9678 sappend(s
, s_proto
);
9680 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9684 /* Since this is Ethernet, use the EtherType of the payload
9685 * directly as the linktype. Overwrite what we already have. */
9686 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9690 s1
= new_stmt(cstate
, BPF_ST
);
9691 s1
->s
.k
= cstate
->off_linktype
.reg
;
9694 /* Advance two bytes further to get the end of the Ethernet
9696 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9700 /* Move the result to X. */
9701 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9704 /* Store the final result of our linkpl calculation. */
9705 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9706 cstate
->off_linkpl
.is_variable
= 1;
9707 cstate
->off_linkpl
.constant_part
= 0;
9709 s1
= new_stmt(cstate
, BPF_STX
);
9710 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9719 /* Check to see if this is a Geneve packet. */
9721 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9723 struct block
*b0
, *b1
;
9727 * Catch errors reported by us and routines below us, and return NULL
9730 if (setjmp(cstate
->top_ctx
))
9733 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9734 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9739 /* Later filters should act on the payload of the Geneve frame,
9740 * update all of the header pointers. Attach this code so that
9741 * it gets executed in the event that the Geneve filter matches. */
9742 s
= gen_geneve_offsets(cstate
);
9744 b1
= gen_true(cstate
);
9745 sappend(s
, b1
->stmts
);
9750 cstate
->is_encap
= 1;
9755 /* Check that this is VXLAN and the VNI is correct if
9756 * specified. Parameterized to handle both IPv4 and IPv6. */
9757 static struct block
*
9758 gen_vxlan_check(compiler_state_t
*cstate
,
9759 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9760 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9762 struct block
*b0
, *b1
;
9764 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9766 /* Check that the VXLAN header has the flag bits set
9768 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9773 if (vni
> 0xffffff) {
9774 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9777 vni
<<= 8; /* VNI is in the upper 3 bytes */
9778 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9786 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9787 * - Verify that this actually is VXLAN with the right VNI.
9788 * - Place the IP header length (plus variable link prefix if
9789 * needed) into register A to be used later to compute
9790 * the inner packet offsets. */
9791 static struct block
*
9792 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9794 struct block
*b0
, *b1
;
9795 struct slist
*s
, *s1
;
9797 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9799 /* Load the IP header length into A. */
9800 s
= gen_loadx_iphdrlen(cstate
);
9802 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9805 /* Forcibly append these statements to the true condition
9806 * of the protocol check by creating a new block that is
9807 * always true and ANDing them. */
9808 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9817 static struct block
*
9818 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9820 struct block
*b0
, *b1
;
9821 struct slist
*s
, *s1
;
9823 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9825 /* Load the IP header length. We need to account for a
9826 * variable length link prefix if there is one. */
9827 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9829 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9833 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9837 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9841 /* Forcibly append these statements to the true condition
9842 * of the protocol check by creating a new block that is
9843 * always true and ANDing them. */
9844 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9847 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9856 /* We need to store three values based on the VXLAN header:
9857 * - The offset of the linktype.
9858 * - The offset of the end of the VXLAN header.
9859 * - The offset of the end of the encapsulated MAC header. */
9860 static struct slist
*
9861 gen_vxlan_offsets(compiler_state_t
*cstate
)
9863 struct slist
*s
, *s1
;
9865 /* Calculate the offset of the VXLAN header itself. This
9866 * includes the IP header computed previously (including any
9867 * variable link prefix) and stored in A plus the fixed size
9868 * headers (fixed link prefix, MAC length, UDP header). */
9869 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9870 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9872 /* Add the VXLAN header length to its offset and store */
9873 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9877 /* Push the link header. VXLAN packets always contain Ethernet
9879 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9881 s1
= new_stmt(cstate
, BPF_ST
);
9882 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9885 /* As the payload is an Ethernet packet, we can use the
9886 * EtherType of the payload directly as the linktype. */
9887 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9891 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9892 cstate
->off_linktype
.is_variable
= 1;
9893 cstate
->off_linktype
.constant_part
= 0;
9895 s1
= new_stmt(cstate
, BPF_ST
);
9896 s1
->s
.k
= cstate
->off_linktype
.reg
;
9899 /* Two bytes further is the end of the Ethernet header and the
9900 * start of the payload. */
9901 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9905 /* Move the result to X. */
9906 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9909 /* Store the final result of our linkpl calculation. */
9910 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9911 cstate
->off_linkpl
.is_variable
= 1;
9912 cstate
->off_linkpl
.constant_part
= 0;
9914 s1
= new_stmt(cstate
, BPF_STX
);
9915 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9923 /* Check to see if this is a VXLAN packet. */
9925 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9927 struct block
*b0
, *b1
;
9931 * Catch errors reported by us and routines below us, and return NULL
9934 if (setjmp(cstate
->top_ctx
))
9937 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9938 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9943 /* Later filters should act on the payload of the VXLAN frame,
9944 * update all of the header pointers. Attach this code so that
9945 * it gets executed in the event that the VXLAN filter matches. */
9946 s
= gen_vxlan_offsets(cstate
);
9948 b1
= gen_true(cstate
);
9949 sappend(s
, b1
->stmts
);
9954 cstate
->is_encap
= 1;
9959 /* Check that the encapsulated frame has a link layer header
9960 * for Ethernet filters. */
9961 static struct block
*
9962 gen_encap_ll_check(compiler_state_t
*cstate
)
9965 struct slist
*s
, *s1
;
9967 /* The easiest way to see if there is a link layer present
9968 * is to check if the link layer header and payload are not
9971 /* Geneve always generates pure variable offsets so we can
9972 * compare only the registers. */
9973 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9974 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9976 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9977 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9980 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9988 static struct block
*
9989 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9990 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9997 if (!cstate
->is_atm
)
9998 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9999 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
10001 if (jvalue
> UINT8_MAX
)
10002 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
10003 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
10004 0xffffffffU
, jtype
, reverse
, jvalue
);
10008 if (!cstate
->is_atm
)
10009 bpf_error(cstate
, "'vci' supported only on raw ATM");
10010 if (cstate
->off_vci
== OFFSET_NOT_SET
)
10012 if (jvalue
> UINT16_MAX
)
10013 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
10014 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
10015 0xffffffffU
, jtype
, reverse
, jvalue
);
10019 if (cstate
->off_proto
== OFFSET_NOT_SET
)
10020 abort(); /* XXX - this isn't on FreeBSD */
10021 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
10022 0x0fU
, jtype
, reverse
, jvalue
);
10026 if (cstate
->off_payload
== OFFSET_NOT_SET
)
10028 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
10029 0xffffffffU
, jtype
, reverse
, jvalue
);
10038 static struct block
*
10039 gen_atmtype_metac(compiler_state_t
*cstate
)
10041 struct block
*b0
, *b1
;
10043 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10044 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
10049 static struct block
*
10050 gen_atmtype_sc(compiler_state_t
*cstate
)
10052 struct block
*b0
, *b1
;
10054 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10055 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
10060 static struct block
*
10061 gen_atmtype_llc(compiler_state_t
*cstate
)
10065 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
10066 cstate
->linktype
= cstate
->prevlinktype
;
10071 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
10072 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10075 * Catch errors reported by us and routines below us, and return NULL
10078 if (setjmp(cstate
->top_ctx
))
10081 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
10086 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
10088 struct block
*b0
, *b1
;
10091 * Catch errors reported by us and routines below us, and return NULL
10094 if (setjmp(cstate
->top_ctx
))
10100 /* Get all packets in Meta signalling Circuit */
10101 if (!cstate
->is_atm
)
10102 bpf_error(cstate
, "'metac' supported only on raw ATM");
10103 b1
= gen_atmtype_metac(cstate
);
10107 /* Get all packets in Broadcast Circuit*/
10108 if (!cstate
->is_atm
)
10109 bpf_error(cstate
, "'bcc' supported only on raw ATM");
10110 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10111 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
10116 /* Get all cells in Segment OAM F4 circuit*/
10117 if (!cstate
->is_atm
)
10118 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
10119 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10120 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10125 /* Get all cells in End-to-End OAM F4 Circuit*/
10126 if (!cstate
->is_atm
)
10127 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
10128 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10129 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10134 /* Get all packets in connection Signalling Circuit */
10135 if (!cstate
->is_atm
)
10136 bpf_error(cstate
, "'sc' supported only on raw ATM");
10137 b1
= gen_atmtype_sc(cstate
);
10141 /* Get all packets in ILMI Circuit */
10142 if (!cstate
->is_atm
)
10143 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
10144 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10145 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10150 /* Get all LANE packets */
10151 if (!cstate
->is_atm
)
10152 bpf_error(cstate
, "'lane' supported only on raw ATM");
10153 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10156 * Arrange that all subsequent tests assume LANE
10157 * rather than LLC-encapsulated packets, and set
10158 * the offsets appropriately for LANE-encapsulated
10161 * We assume LANE means Ethernet, not Token Ring.
10163 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10164 cstate
->off_payload
+ 2, /* Ethernet header */
10166 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10167 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10168 cstate
->off_nl
= 0; /* Ethernet II */
10169 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10179 * Filtering for MTP2 messages based on li value
10180 * FISU, length is null
10181 * LSSU, length is 1 or 2
10182 * MSU, length is 3 or more
10183 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10186 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10188 struct block
*b0
, *b1
;
10191 * Catch errors reported by us and routines below us, and return NULL
10194 if (setjmp(cstate
->top_ctx
))
10200 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10201 (cstate
->linktype
!= DLT_ERF
) &&
10202 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10203 bpf_error(cstate
, "'fisu' supported only on MTP2");
10204 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10205 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10206 0x3fU
, BPF_JEQ
, 0, 0U);
10210 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10211 (cstate
->linktype
!= DLT_ERF
) &&
10212 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10213 bpf_error(cstate
, "'lssu' supported only on MTP2");
10214 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10215 0x3fU
, BPF_JGT
, 1, 2U);
10216 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10217 0x3fU
, BPF_JGT
, 0, 0U);
10222 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10223 (cstate
->linktype
!= DLT_ERF
) &&
10224 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10225 bpf_error(cstate
, "'msu' supported only on MTP2");
10226 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10227 0x3fU
, BPF_JGT
, 0, 2U);
10231 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10232 (cstate
->linktype
!= DLT_ERF
) &&
10233 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10234 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10235 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10236 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10237 0xff80U
, BPF_JEQ
, 0, 0U);
10241 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10242 (cstate
->linktype
!= DLT_ERF
) &&
10243 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10244 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10245 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10246 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10247 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10248 0xff80U
, BPF_JGT
, 0, 0U);
10253 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10254 (cstate
->linktype
!= DLT_ERF
) &&
10255 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10256 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10257 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10258 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10268 * These maximum valid values are all-ones, so they double as the bitmasks
10269 * before any bitwise shifting.
10271 #define MTP2_SIO_MAXVAL UINT8_MAX
10272 #define MTP3_PC_MAXVAL 0x3fffU
10273 #define MTP3_SLS_MAXVAL 0xfU
10275 static struct block
*
10276 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10277 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10285 newoff_sio
= cstate
->off_sio
;
10286 newoff_opc
= cstate
->off_opc
;
10287 newoff_dpc
= cstate
->off_dpc
;
10288 newoff_sls
= cstate
->off_sls
;
10289 switch (mtp3field
) {
10292 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10294 * SIO is the simplest field: the size is one byte and the offset is a
10295 * multiple of bytes, so the only detail to get right is the value of
10296 * the [right-to-left] field offset.
10299 newoff_sio
+= 3; /* offset for MTP2_HSL */
10303 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10304 bpf_error(cstate
, "'sio' supported only on SS7");
10305 if(jvalue
> MTP2_SIO_MAXVAL
)
10306 bpf_error(cstate
, "sio value %u too big; max value = %u",
10307 jvalue
, MTP2_SIO_MAXVAL
);
10308 // Here the bitmask means "do not apply a bitmask".
10309 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10310 jtype
, reverse
, jvalue
);
10314 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10316 * SLS, OPC and DPC are more complicated: none of these is sized in a
10317 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10318 * diagrams are meant to be read right-to-left. This means in the
10319 * diagrams within individual fields and concatenations thereof
10320 * bitwise shifts and masks can be noted in the common left-to-right
10321 * manner until each final value is ready to be byte-swapped and
10322 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10323 * similar problem in a similar way.
10325 * Offsets of fields within the packet header always have the
10326 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10327 * DLTs the offset does not include the F (Flag) field at the
10328 * beginning of each message.
10330 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10331 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10332 * be tested entirely using a single BPF_W comparison. In this case
10333 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10334 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10335 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10336 * correlates with the [RTL] packet diagram until the byte-swapping is
10339 * The code below uses this approach for OPC, which spans 3 bytes.
10340 * DPC and SLS use shorter loads, SLS also uses a different offset.
10347 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10348 bpf_error(cstate
, "'opc' supported only on SS7");
10349 if (jvalue
> MTP3_PC_MAXVAL
)
10350 bpf_error(cstate
, "opc value %u too big; max value = %u",
10351 jvalue
, MTP3_PC_MAXVAL
);
10352 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10353 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10354 SWAPLONG(jvalue
<< 14));
10362 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10363 bpf_error(cstate
, "'dpc' supported only on SS7");
10364 if (jvalue
> MTP3_PC_MAXVAL
)
10365 bpf_error(cstate
, "dpc value %u too big; max value = %u",
10366 jvalue
, MTP3_PC_MAXVAL
);
10367 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10368 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10369 SWAPSHORT(jvalue
));
10377 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10378 bpf_error(cstate
, "'sls' supported only on SS7");
10379 if (jvalue
> MTP3_SLS_MAXVAL
)
10380 bpf_error(cstate
, "sls value %u too big; max value = %u",
10381 jvalue
, MTP3_SLS_MAXVAL
);
10382 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10383 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10394 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10395 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10398 * Catch errors reported by us and routines below us, and return NULL
10401 if (setjmp(cstate
->top_ctx
))
10404 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10408 static struct block
*
10409 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10414 * Q.2931 signalling protocol messages for handling virtual circuits
10415 * establishment and teardown
10420 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10423 case A_CALLPROCEED
:
10424 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10428 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10432 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10436 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10439 case A_RELEASE_DONE
:
10440 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10450 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10452 struct block
*b0
, *b1
;
10455 * Catch errors reported by us and routines below us, and return NULL
10458 if (setjmp(cstate
->top_ctx
))
10464 if (!cstate
->is_atm
)
10465 bpf_error(cstate
, "'oam' supported only on raw ATM");
10467 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10468 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10470 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10475 if (!cstate
->is_atm
)
10476 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10478 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10479 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10481 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10487 * Get Q.2931 signalling messages for switched
10488 * virtual connection
10490 if (!cstate
->is_atm
)
10491 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10492 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10493 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10495 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10497 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10499 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10501 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10503 b0
= gen_atmtype_sc(cstate
);
10507 case A_METACONNECT
:
10508 if (!cstate
->is_atm
)
10509 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10510 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10511 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10513 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10515 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10517 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10519 b0
= gen_atmtype_metac(cstate
);