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 // protocol qualifier keywords
885 pqkw(const unsigned id
)
887 const char * tokens
[] = {
899 [Q_DECNET
] = "decnet",
905 [Q_ICMPV6
] = "icmp6",
917 [Q_NETBEUI
] = "netbeui",
920 [Q_ISIS_IIH
] = "iih",
921 [Q_ISIS_SNP
] = "snp",
922 [Q_ISIS_CSNP
] = "csnp",
923 [Q_ISIS_PSNP
] = "psnp",
924 [Q_ISIS_LSP
] = "lsp",
928 return qual2kw("proto", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
931 // direction qualifier keywords
933 dqkw(const unsigned id
)
935 const char * map
[] = {
938 [Q_OR
] = "src or dst",
939 [Q_AND
] = "src and dst",
947 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
952 atmkw(const unsigned id
)
954 const char * tokens
[] = {
957 [A_OAMF4SC
] = "oamf4sc",
958 [A_OAMF4EC
] = "oamf4ec",
964 // no keyword for A_SETUP
965 // no keyword for A_CALLPROCEED
966 // no keyword for A_CONNECT
967 // no keyword for A_CONNECTACK
968 // no keyword for A_RELEASE
969 // no keyword for A_RELEASE_DONE
972 // no keyword for A_PROTOTYPE
973 // no keyword for A_MSGTYPE
974 [A_CONNECTMSG
] = "connectmsg",
975 [A_METACONNECT
] = "metaconnect",
977 return qual2kw("ATM keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
982 ss7kw(const unsigned id
)
984 const char * tokens
[] = {
1000 return qual2kw("MTP keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
1003 static PCAP_NORETURN_DEF
void
1004 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
1006 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
1007 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
1011 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
1013 if (cstate
->linktype
!= DLT_PFLOG
)
1014 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
1018 assert_atm(compiler_state_t
*cstate
, const char *kw
)
1021 * Belt and braces: init_linktype() sets either all of these struct
1022 * members (for DLT_SUNATM) or none (otherwise).
1024 if (cstate
->linktype
!= DLT_SUNATM
||
1026 cstate
->off_vpi
== OFFSET_NOT_SET
||
1027 cstate
->off_vci
== OFFSET_NOT_SET
||
1028 cstate
->off_proto
== OFFSET_NOT_SET
||
1029 cstate
->off_payload
== OFFSET_NOT_SET
)
1030 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1034 assert_ss7(compiler_state_t
*cstate
, const char *kw
)
1036 switch (cstate
->linktype
) {
1039 case DLT_MTP2_WITH_PHDR
:
1040 // Belt and braces, same as in assert_atm().
1041 if (cstate
->off_sio
!= OFFSET_NOT_SET
&&
1042 cstate
->off_opc
!= OFFSET_NOT_SET
&&
1043 cstate
->off_dpc
!= OFFSET_NOT_SET
&&
1044 cstate
->off_sls
!= OFFSET_NOT_SET
)
1047 bpf_error(cstate
, "'%s' supported only on SS7", kw
);
1051 assert_maxval(compiler_state_t
*cstate
, const char *name
,
1052 const bpf_u_int32 val
, const bpf_u_int32 maxval
)
1055 bpf_error(cstate
, "%s %u greater than maximum %u",
1059 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1060 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1062 // Validate a port/portrange proto qualifier and map to an IP protocol number.
1064 port_pq_to_ipproto(compiler_state_t
*cstate
, const int proto
, const char *kw
)
1072 return IPPROTO_SCTP
;
1076 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), kw
);
1080 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1081 const char *buf
, int optimize
, bpf_u_int32 mask
)
1087 compiler_state_t cstate
;
1088 yyscan_t scanner
= NULL
;
1089 YY_BUFFER_STATE in_buffer
= NULL
;
1094 * If this pcap_t hasn't been activated, it doesn't have a
1095 * link-layer type, so we can't use it.
1097 if (!p
->activated
) {
1098 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1099 "not-yet-activated pcap_t passed to pcap_compile");
1100 return (PCAP_ERROR
);
1105 * Initialize Winsock, asking for the latest version (2.2),
1106 * as we may be calling Winsock routines to translate
1107 * host names to addresses.
1109 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1111 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1112 err
, "Error calling WSAStartup()");
1113 return (PCAP_ERROR
);
1117 #ifdef ENABLE_REMOTE
1119 * If the device on which we're capturing need to be notified
1120 * that a new filter is being compiled, do so.
1122 * This allows them to save a copy of it, in case, for example,
1123 * they're implementing a form of remote packet capture, and
1124 * want the remote machine to filter out the packets in which
1125 * it's sending the packets it's captured.
1127 * XXX - the fact that we happen to be compiling a filter
1128 * doesn't necessarily mean we'll be installing it as the
1129 * filter for this pcap_t; we might be running it from userland
1130 * on captured packets to do packet classification. We really
1131 * need a better way of handling this, but this is all that
1132 * the WinPcap remote capture code did.
1134 if (p
->save_current_filter_op
!= NULL
)
1135 (p
->save_current_filter_op
)(p
, buf
);
1138 initchunks(&cstate
);
1139 cstate
.no_optimize
= 0;
1144 cstate
.ic
.root
= NULL
;
1145 cstate
.ic
.cur_mark
= 0;
1146 cstate
.bpf_pcap
= p
;
1147 cstate
.error_set
= 0;
1150 cstate
.netmask
= mask
;
1152 cstate
.snaplen
= pcap_snapshot(p
);
1153 if (cstate
.snaplen
== 0) {
1154 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1155 "snaplen of 0 rejects all packets");
1160 if (pcap_lex_init(&scanner
) != 0) {
1161 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1162 errno
, "can't initialize scanner");
1166 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1169 * Associate the compiler state with the lexical analyzer
1172 pcap_set_extra(&cstate
, scanner
);
1174 if (init_linktype(&cstate
, p
) == -1) {
1178 if (pcap_parse(scanner
, &cstate
) != 0) {
1180 if (cstate
.ai
!= NULL
)
1181 freeaddrinfo(cstate
.ai
);
1183 if (cstate
.e
!= NULL
)
1189 if (cstate
.ic
.root
== NULL
) {
1190 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1193 * Catch errors reported by gen_retblk().
1195 if (cstate
.ic
.root
== NULL
) {
1201 if (optimize
&& !cstate
.no_optimize
) {
1202 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1207 if (cstate
.ic
.root
== NULL
||
1208 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1209 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1210 "expression rejects all packets");
1215 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1216 cstate
.ic
.root
, &len
, p
->errbuf
);
1217 if (program
->bf_insns
== NULL
) {
1222 program
->bf_len
= len
;
1224 rc
= 0; /* We're all okay */
1228 * Clean up everything for the lexical analyzer.
1230 if (in_buffer
!= NULL
)
1231 pcap__delete_buffer(in_buffer
, scanner
);
1232 if (scanner
!= NULL
)
1233 pcap_lex_destroy(scanner
);
1236 * Clean up our own allocated memory.
1238 freechunks(&cstate
);
1248 * entry point for using the compiler with no pcap open
1249 * pass in all the stuff that is needed explicitly instead.
1252 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1253 struct bpf_program
*program
,
1254 const char *buf
, int optimize
, bpf_u_int32 mask
)
1259 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1261 return (PCAP_ERROR
);
1262 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1268 * Clean up a "struct bpf_program" by freeing all the memory allocated
1272 pcap_freecode(struct bpf_program
*program
)
1274 program
->bf_len
= 0;
1275 if (program
->bf_insns
!= NULL
) {
1276 free((char *)program
->bf_insns
);
1277 program
->bf_insns
= NULL
;
1282 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1283 * which of the jt and jf fields has been resolved and which is a pointer
1284 * back to another unresolved block (or nil). At least one of the fields
1285 * in each block is already resolved.
1288 backpatch(struct block
*list
, struct block
*target
)
1305 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1306 * which of jt and jf is the link.
1309 merge(struct block
*b0
, struct block
*b1
)
1311 register struct block
**p
= &b0
;
1313 /* Find end of list. */
1315 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1317 /* Concatenate the lists. */
1322 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1325 * Catch errors reported by us and routines below us, and return -1
1328 if (setjmp(cstate
->top_ctx
))
1332 * Insert before the statements of the first (root) block any
1333 * statements needed to load the lengths of any variable-length
1334 * headers into registers.
1336 * XXX - a fancier strategy would be to insert those before the
1337 * statements of all blocks that use those lengths and that
1338 * have no predecessors that use them, so that we only compute
1339 * the lengths if we need them. There might be even better
1340 * approaches than that.
1342 * However, those strategies would be more complicated, and
1343 * as we don't generate code to compute a length if the
1344 * program has no tests that use the length, and as most
1345 * tests will probably use those lengths, we would just
1346 * postpone computing the lengths so that it's not done
1347 * for tests that fail early, and it's not clear that's
1350 insert_compute_vloffsets(cstate
, p
->head
);
1353 * For DLT_PPI captures, generate a check of the per-packet
1354 * DLT value to make sure it's DLT_IEEE802_11.
1356 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1357 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1358 * with appropriate Ethernet information and use that rather
1359 * than using something such as DLT_PPI where you don't know
1360 * the link-layer header type until runtime, which, in the
1361 * general case, would force us to generate both Ethernet *and*
1362 * 802.11 code (*and* anything else for which PPI is used)
1363 * and choose between them early in the BPF program?
1365 if (cstate
->linktype
== DLT_PPI
) {
1366 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1367 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1368 gen_and(ppi_dlt_check
, p
);
1371 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1372 p
->sense
= !p
->sense
;
1373 backpatch(p
, gen_retblk_internal(cstate
, 0));
1374 cstate
->ic
.root
= p
->head
;
1379 gen_and(struct block
*b0
, struct block
*b1
)
1381 backpatch(b0
, b1
->head
);
1382 b0
->sense
= !b0
->sense
;
1383 b1
->sense
= !b1
->sense
;
1385 b1
->sense
= !b1
->sense
;
1386 b1
->head
= b0
->head
;
1390 gen_or(struct block
*b0
, struct block
*b1
)
1392 b0
->sense
= !b0
->sense
;
1393 backpatch(b0
, b1
->head
);
1394 b0
->sense
= !b0
->sense
;
1396 b1
->head
= b0
->head
;
1400 gen_not(struct block
*b
)
1402 b
->sense
= !b
->sense
;
1405 static struct block
*
1406 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1407 u_int size
, bpf_u_int32 v
)
1409 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1412 static struct block
*
1413 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1414 u_int size
, bpf_u_int32 v
)
1416 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1419 static struct block
*
1420 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1421 u_int size
, bpf_u_int32 v
)
1423 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1426 static struct block
*
1427 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1428 u_int size
, bpf_u_int32 v
)
1430 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1433 static struct block
*
1434 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1435 u_int size
, bpf_u_int32 v
)
1437 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1440 static struct block
*
1441 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1442 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1444 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1447 static struct block
*
1448 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1449 u_int size
, const u_char
*v
)
1451 register struct block
*b
, *tmp
;
1455 register const u_char
*p
= &v
[size
- 4];
1457 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1465 register const u_char
*p
= &v
[size
- 2];
1467 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1475 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1484 * AND the field of size "size" at offset "offset" relative to the header
1485 * specified by "offrel" with "mask", and compare it with the value "v"
1486 * with the test specified by "jtype"; if "reverse" is true, the test
1487 * should test the opposite of "jtype".
1489 static struct block
*
1490 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1491 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1494 struct slist
*s
, *s2
;
1497 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1499 if (mask
!= 0xffffffff) {
1500 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1505 b
= new_block(cstate
, JMP(jtype
));
1514 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1516 cstate
->pcap_fddipad
= p
->fddipad
;
1519 * We start out with only one link-layer header.
1521 cstate
->outermostlinktype
= pcap_datalink(p
);
1522 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1523 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1524 cstate
->off_outermostlinkhdr
.reg
= -1;
1526 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1527 cstate
->off_prevlinkhdr
.constant_part
= 0;
1528 cstate
->off_prevlinkhdr
.is_variable
= 0;
1529 cstate
->off_prevlinkhdr
.reg
= -1;
1531 cstate
->linktype
= cstate
->outermostlinktype
;
1532 cstate
->off_linkhdr
.constant_part
= 0;
1533 cstate
->off_linkhdr
.is_variable
= 0;
1534 cstate
->off_linkhdr
.reg
= -1;
1539 cstate
->off_linkpl
.constant_part
= 0;
1540 cstate
->off_linkpl
.is_variable
= 0;
1541 cstate
->off_linkpl
.reg
= -1;
1543 cstate
->off_linktype
.constant_part
= 0;
1544 cstate
->off_linktype
.is_variable
= 0;
1545 cstate
->off_linktype
.reg
= -1;
1548 * Assume it's not raw ATM with a pseudo-header, for now.
1551 cstate
->off_vpi
= OFFSET_NOT_SET
;
1552 cstate
->off_vci
= OFFSET_NOT_SET
;
1553 cstate
->off_proto
= OFFSET_NOT_SET
;
1554 cstate
->off_payload
= OFFSET_NOT_SET
;
1557 * And not encapsulated with either Geneve or VXLAN.
1559 cstate
->is_encap
= 0;
1562 * No variable length VLAN offset by default
1564 cstate
->is_vlan_vloffset
= 0;
1567 * And assume we're not doing SS7.
1569 cstate
->off_li
= OFFSET_NOT_SET
;
1570 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1571 cstate
->off_sio
= OFFSET_NOT_SET
;
1572 cstate
->off_opc
= OFFSET_NOT_SET
;
1573 cstate
->off_dpc
= OFFSET_NOT_SET
;
1574 cstate
->off_sls
= OFFSET_NOT_SET
;
1576 cstate
->label_stack_depth
= 0;
1577 cstate
->vlan_stack_depth
= 0;
1579 switch (cstate
->linktype
) {
1582 cstate
->off_linktype
.constant_part
= 2;
1583 cstate
->off_linkpl
.constant_part
= 6;
1584 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1585 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1588 case DLT_ARCNET_LINUX
:
1589 cstate
->off_linktype
.constant_part
= 4;
1590 cstate
->off_linkpl
.constant_part
= 8;
1591 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1592 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1596 cstate
->off_linktype
.constant_part
= 12;
1597 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1598 cstate
->off_nl
= 0; /* Ethernet II */
1599 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1604 * SLIP doesn't have a link level type. The 16 byte
1605 * header is hacked into our SLIP driver.
1607 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1608 cstate
->off_linkpl
.constant_part
= 16;
1610 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1613 case DLT_SLIP_BSDOS
:
1614 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1615 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1617 cstate
->off_linkpl
.constant_part
= 24;
1619 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1624 cstate
->off_linktype
.constant_part
= 0;
1625 cstate
->off_linkpl
.constant_part
= 4;
1627 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1631 cstate
->off_linktype
.constant_part
= 0;
1632 cstate
->off_linkpl
.constant_part
= 12;
1634 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1639 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1640 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1641 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1642 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1643 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1645 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1650 * This does not include the Ethernet header, and
1651 * only covers session state.
1653 cstate
->off_linktype
.constant_part
= 6;
1654 cstate
->off_linkpl
.constant_part
= 8;
1656 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1660 cstate
->off_linktype
.constant_part
= 5;
1661 cstate
->off_linkpl
.constant_part
= 24;
1663 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1668 * FDDI doesn't really have a link-level type field.
1669 * We set "off_linktype" to the offset of the LLC header.
1671 * To check for Ethernet types, we assume that SSAP = SNAP
1672 * is being used and pick out the encapsulated Ethernet type.
1673 * XXX - should we generate code to check for SNAP?
1675 cstate
->off_linktype
.constant_part
= 13;
1676 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1677 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1678 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1679 cstate
->off_nl
= 8; /* 802.2+SNAP */
1680 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1685 * Token Ring doesn't really have a link-level type field.
1686 * We set "off_linktype" to the offset of the LLC header.
1688 * To check for Ethernet types, we assume that SSAP = SNAP
1689 * is being used and pick out the encapsulated Ethernet type.
1690 * XXX - should we generate code to check for SNAP?
1692 * XXX - the header is actually variable-length.
1693 * Some various Linux patched versions gave 38
1694 * as "off_linktype" and 40 as "off_nl"; however,
1695 * if a token ring packet has *no* routing
1696 * information, i.e. is not source-routed, the correct
1697 * values are 20 and 22, as they are in the vanilla code.
1699 * A packet is source-routed iff the uppermost bit
1700 * of the first byte of the source address, at an
1701 * offset of 8, has the uppermost bit set. If the
1702 * packet is source-routed, the total number of bytes
1703 * of routing information is 2 plus bits 0x1F00 of
1704 * the 16-bit value at an offset of 14 (shifted right
1705 * 8 - figure out which byte that is).
1707 cstate
->off_linktype
.constant_part
= 14;
1708 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1709 cstate
->off_nl
= 8; /* 802.2+SNAP */
1710 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1713 case DLT_PRISM_HEADER
:
1714 case DLT_IEEE802_11_RADIO_AVS
:
1715 case DLT_IEEE802_11_RADIO
:
1716 cstate
->off_linkhdr
.is_variable
= 1;
1717 /* Fall through, 802.11 doesn't have a variable link
1718 * prefix but is otherwise the same. */
1721 case DLT_IEEE802_11
:
1723 * 802.11 doesn't really have a link-level type field.
1724 * We set "off_linktype.constant_part" to the offset of
1727 * To check for Ethernet types, we assume that SSAP = SNAP
1728 * is being used and pick out the encapsulated Ethernet type.
1729 * XXX - should we generate code to check for SNAP?
1731 * We also handle variable-length radio headers here.
1732 * The Prism header is in theory variable-length, but in
1733 * practice it's always 144 bytes long. However, some
1734 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1735 * sometimes or always supply an AVS header, so we
1736 * have to check whether the radio header is a Prism
1737 * header or an AVS header, so, in practice, it's
1740 cstate
->off_linktype
.constant_part
= 24;
1741 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1742 cstate
->off_linkpl
.is_variable
= 1;
1743 cstate
->off_nl
= 8; /* 802.2+SNAP */
1744 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1749 * At the moment we treat PPI the same way that we treat
1750 * normal Radiotap encoded packets. The difference is in
1751 * the function that generates the code at the beginning
1752 * to compute the header length. Since this code generator
1753 * of PPI supports bare 802.11 encapsulation only (i.e.
1754 * the encapsulated DLT should be DLT_IEEE802_11) we
1755 * generate code to check for this too.
1757 cstate
->off_linktype
.constant_part
= 24;
1758 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1759 cstate
->off_linkpl
.is_variable
= 1;
1760 cstate
->off_linkhdr
.is_variable
= 1;
1761 cstate
->off_nl
= 8; /* 802.2+SNAP */
1762 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1765 case DLT_ATM_RFC1483
:
1766 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1768 * assume routed, non-ISO PDUs
1769 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1771 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1772 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1773 * latter would presumably be treated the way PPPoE
1774 * should be, so you can do "pppoe and udp port 2049"
1775 * or "pppoa and tcp port 80" and have it check for
1776 * PPPo{A,E} and a PPP protocol of IP and....
1778 cstate
->off_linktype
.constant_part
= 0;
1779 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1780 cstate
->off_nl
= 8; /* 802.2+SNAP */
1781 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1786 * Full Frontal ATM; you get AALn PDUs with an ATM
1790 cstate
->off_vpi
= SUNATM_VPI_POS
;
1791 cstate
->off_vci
= SUNATM_VCI_POS
;
1792 cstate
->off_proto
= PROTO_POS
;
1793 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1794 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1795 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1796 cstate
->off_nl
= 8; /* 802.2+SNAP */
1797 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1803 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1804 cstate
->off_linkpl
.constant_part
= 0;
1806 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1809 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1810 cstate
->off_linktype
.constant_part
= 14;
1811 cstate
->off_linkpl
.constant_part
= 16;
1813 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1816 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1817 cstate
->off_linktype
.constant_part
= 0;
1818 cstate
->off_linkpl
.constant_part
= 20;
1820 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1825 * LocalTalk does have a 1-byte type field in the LLAP header,
1826 * but really it just indicates whether there is a "short" or
1827 * "long" DDP packet following.
1829 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1830 cstate
->off_linkpl
.constant_part
= 0;
1832 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1835 case DLT_IP_OVER_FC
:
1837 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1838 * link-level type field. We set "off_linktype" to the
1839 * offset of the LLC header.
1841 * To check for Ethernet types, we assume that SSAP = SNAP
1842 * is being used and pick out the encapsulated Ethernet type.
1843 * XXX - should we generate code to check for SNAP? RFC
1844 * 2625 says SNAP should be used.
1846 cstate
->off_linktype
.constant_part
= 16;
1847 cstate
->off_linkpl
.constant_part
= 16;
1848 cstate
->off_nl
= 8; /* 802.2+SNAP */
1849 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1854 * XXX - we should set this to handle SNAP-encapsulated
1855 * frames (NLPID of 0x80).
1857 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1858 cstate
->off_linkpl
.constant_part
= 0;
1860 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1864 * the only BPF-interesting FRF.16 frames are non-control frames;
1865 * Frame Relay has a variable length link-layer
1866 * so lets start with offset 4 for now and increments later on (FIXME);
1869 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1870 cstate
->off_linkpl
.constant_part
= 0;
1872 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1875 case DLT_APPLE_IP_OVER_IEEE1394
:
1876 cstate
->off_linktype
.constant_part
= 16;
1877 cstate
->off_linkpl
.constant_part
= 18;
1879 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1882 case DLT_SYMANTEC_FIREWALL
:
1883 cstate
->off_linktype
.constant_part
= 6;
1884 cstate
->off_linkpl
.constant_part
= 44;
1885 cstate
->off_nl
= 0; /* Ethernet II */
1886 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1890 cstate
->off_linktype
.constant_part
= 0;
1891 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1892 cstate
->off_linkpl
.is_variable
= 1;
1894 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1897 case DLT_JUNIPER_MFR
:
1898 case DLT_JUNIPER_MLFR
:
1899 case DLT_JUNIPER_MLPPP
:
1900 case DLT_JUNIPER_PPP
:
1901 case DLT_JUNIPER_CHDLC
:
1902 case DLT_JUNIPER_FRELAY
:
1903 cstate
->off_linktype
.constant_part
= 4;
1904 cstate
->off_linkpl
.constant_part
= 4;
1906 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1909 case DLT_JUNIPER_ATM1
:
1910 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1911 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1913 cstate
->off_nl_nosnap
= 10;
1916 case DLT_JUNIPER_ATM2
:
1917 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1918 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1920 cstate
->off_nl_nosnap
= 10;
1923 /* frames captured on a Juniper PPPoE service PIC
1924 * contain raw ethernet frames */
1925 case DLT_JUNIPER_PPPOE
:
1926 case DLT_JUNIPER_ETHER
:
1927 cstate
->off_linkpl
.constant_part
= 14;
1928 cstate
->off_linktype
.constant_part
= 16;
1929 cstate
->off_nl
= 18; /* Ethernet II */
1930 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1933 case DLT_JUNIPER_PPPOE_ATM
:
1934 cstate
->off_linktype
.constant_part
= 4;
1935 cstate
->off_linkpl
.constant_part
= 6;
1937 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1940 case DLT_JUNIPER_GGSN
:
1941 cstate
->off_linktype
.constant_part
= 6;
1942 cstate
->off_linkpl
.constant_part
= 12;
1944 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1947 case DLT_JUNIPER_ES
:
1948 cstate
->off_linktype
.constant_part
= 6;
1949 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1950 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1951 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1954 case DLT_JUNIPER_MONITOR
:
1955 cstate
->off_linktype
.constant_part
= 12;
1956 cstate
->off_linkpl
.constant_part
= 12;
1957 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1958 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1961 case DLT_BACNET_MS_TP
:
1962 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1963 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1964 cstate
->off_nl
= OFFSET_NOT_SET
;
1965 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1968 case DLT_JUNIPER_SERVICES
:
1969 cstate
->off_linktype
.constant_part
= 12;
1970 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1971 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1972 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1975 case DLT_JUNIPER_VP
:
1976 cstate
->off_linktype
.constant_part
= 18;
1977 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1978 cstate
->off_nl
= OFFSET_NOT_SET
;
1979 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1982 case DLT_JUNIPER_ST
:
1983 cstate
->off_linktype
.constant_part
= 18;
1984 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1985 cstate
->off_nl
= OFFSET_NOT_SET
;
1986 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1989 case DLT_JUNIPER_ISM
:
1990 cstate
->off_linktype
.constant_part
= 8;
1991 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1992 cstate
->off_nl
= OFFSET_NOT_SET
;
1993 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1996 case DLT_JUNIPER_VS
:
1997 case DLT_JUNIPER_SRX_E2E
:
1998 case DLT_JUNIPER_FIBRECHANNEL
:
1999 case DLT_JUNIPER_ATM_CEMIC
:
2000 cstate
->off_linktype
.constant_part
= 8;
2001 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2002 cstate
->off_nl
= OFFSET_NOT_SET
;
2003 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2008 cstate
->off_li_hsl
= 4;
2009 cstate
->off_sio
= 3;
2010 cstate
->off_opc
= 4;
2011 cstate
->off_dpc
= 4;
2012 cstate
->off_sls
= 7;
2013 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2014 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2015 cstate
->off_nl
= OFFSET_NOT_SET
;
2016 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2019 case DLT_MTP2_WITH_PHDR
:
2021 cstate
->off_li_hsl
= 8;
2022 cstate
->off_sio
= 7;
2023 cstate
->off_opc
= 8;
2024 cstate
->off_dpc
= 8;
2025 cstate
->off_sls
= 11;
2026 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2027 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2028 cstate
->off_nl
= OFFSET_NOT_SET
;
2029 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2033 cstate
->off_li
= 22;
2034 cstate
->off_li_hsl
= 24;
2035 cstate
->off_sio
= 23;
2036 cstate
->off_opc
= 24;
2037 cstate
->off_dpc
= 24;
2038 cstate
->off_sls
= 27;
2039 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2040 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2041 cstate
->off_nl
= OFFSET_NOT_SET
;
2042 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2046 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2047 cstate
->off_linkpl
.constant_part
= 4;
2049 cstate
->off_nl_nosnap
= 0;
2054 * Currently, only raw "link[N:M]" filtering is supported.
2056 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
2057 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2058 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
2059 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2063 cstate
->off_linktype
.constant_part
= 1;
2064 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2066 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2069 case DLT_NETANALYZER
:
2070 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
2071 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2072 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
2073 cstate
->off_nl
= 0; /* Ethernet II */
2074 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2077 case DLT_NETANALYZER_TRANSPARENT
:
2078 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2079 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2080 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2081 cstate
->off_nl
= 0; /* Ethernet II */
2082 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2087 * For values in the range in which we've assigned new
2088 * DLT_ values, only raw "link[N:M]" filtering is supported.
2090 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2091 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2092 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2093 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2094 cstate
->off_nl
= OFFSET_NOT_SET
;
2095 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2097 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2098 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2104 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2109 * Load a value relative to the specified absolute offset.
2111 static struct slist
*
2112 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2113 u_int offset
, u_int size
)
2115 struct slist
*s
, *s2
;
2117 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2120 * If "s" is non-null, it has code to arrange that the X register
2121 * contains the variable part of the absolute offset, so we
2122 * generate a load relative to that, with an offset of
2123 * abs_offset->constant_part + offset.
2125 * Otherwise, we can do an absolute load with an offset of
2126 * abs_offset->constant_part + offset.
2130 * "s" points to a list of statements that puts the
2131 * variable part of the absolute offset into the X register.
2132 * Do an indirect load, to use the X register as an offset.
2134 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2135 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2139 * There is no variable part of the absolute offset, so
2140 * just do an absolute load.
2142 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2143 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2149 * Load a value relative to the beginning of the specified header.
2151 static struct slist
*
2152 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2155 struct slist
*s
, *s2
;
2158 * Squelch warnings from compilers that *don't* assume that
2159 * offrel always has a valid enum value and therefore don't
2160 * assume that we'll always go through one of the case arms.
2162 * If we have a default case, compilers that *do* assume that
2163 * will then complain about the default case code being
2166 * Damned if you do, damned if you don't.
2173 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2178 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2181 case OR_PREVLINKHDR
:
2182 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2186 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2189 case OR_PREVMPLSHDR
:
2190 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2194 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2197 case OR_LINKPL_NOSNAP
:
2198 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2202 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2207 * Load the X register with the length of the IPv4 header
2208 * (plus the offset of the link-layer header, if it's
2209 * preceded by a variable-length header such as a radio
2210 * header), in bytes.
2212 s
= gen_loadx_iphdrlen(cstate
);
2215 * Load the item at {offset of the link-layer payload} +
2216 * {offset, relative to the start of the link-layer
2217 * payload, of the IPv4 header} + {length of the IPv4 header} +
2218 * {specified offset}.
2220 * If the offset of the link-layer payload is variable,
2221 * the variable part of that offset is included in the
2222 * value in the X register, and we include the constant
2223 * part in the offset of the load.
2225 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2226 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2231 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2238 * Generate code to load into the X register the sum of the length of
2239 * the IPv4 header and the variable part of the offset of the link-layer
2242 static struct slist
*
2243 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2245 struct slist
*s
, *s2
;
2247 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2250 * The offset of the link-layer payload has a variable
2251 * part. "s" points to a list of statements that put
2252 * the variable part of that offset into the X register.
2254 * The 4*([k]&0xf) addressing mode can't be used, as we
2255 * don't have a constant offset, so we have to load the
2256 * value in question into the A register and add to it
2257 * the value from the X register.
2259 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2260 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2262 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2265 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2270 * The A register now contains the length of the IP header.
2271 * We need to add to it the variable part of the offset of
2272 * the link-layer payload, which is still in the X
2273 * register, and move the result into the X register.
2275 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2276 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2279 * The offset of the link-layer payload is a constant,
2280 * so no code was generated to load the (nonexistent)
2281 * variable part of that offset.
2283 * This means we can use the 4*([k]&0xf) addressing
2284 * mode. Load the length of the IPv4 header, which
2285 * is at an offset of cstate->off_nl from the beginning of
2286 * the link-layer payload, and thus at an offset of
2287 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2288 * of the raw packet data, using that addressing mode.
2290 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2291 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2297 static struct block
*
2298 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2303 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2305 b
= new_block(cstate
, JMP(BPF_JEQ
));
2311 static inline struct block
*
2312 gen_true(compiler_state_t
*cstate
)
2314 return gen_uncond(cstate
, 1);
2317 static inline struct block
*
2318 gen_false(compiler_state_t
*cstate
)
2320 return gen_uncond(cstate
, 0);
2324 * Generate code to match a particular packet type.
2326 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2327 * value, if <= ETHERMTU. We use that to determine whether to
2328 * match the type/length field or to check the type/length field for
2329 * a value <= ETHERMTU to see whether it's a type field and then do
2330 * the appropriate test.
2332 static struct block
*
2333 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2335 struct block
*b0
, *b1
;
2341 case LLCSAP_NETBEUI
:
2343 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2344 * so we check the DSAP and SSAP.
2346 * LLCSAP_IP checks for IP-over-802.2, rather
2347 * than IP-over-Ethernet or IP-over-SNAP.
2349 * XXX - should we check both the DSAP and the
2350 * SSAP, like this, or should we check just the
2351 * DSAP, as we do for other types <= ETHERMTU
2352 * (i.e., other SAP values)?
2354 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2355 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2363 * Ethernet_II frames, which are Ethernet
2364 * frames with a frame type of ETHERTYPE_IPX;
2366 * Ethernet_802.3 frames, which are 802.3
2367 * frames (i.e., the type/length field is
2368 * a length field, <= ETHERMTU, rather than
2369 * a type field) with the first two bytes
2370 * after the Ethernet/802.3 header being
2373 * Ethernet_802.2 frames, which are 802.3
2374 * frames with an 802.2 LLC header and
2375 * with the IPX LSAP as the DSAP in the LLC
2378 * Ethernet_SNAP frames, which are 802.3
2379 * frames with an LLC header and a SNAP
2380 * header and with an OUI of 0x000000
2381 * (encapsulated Ethernet) and a protocol
2382 * ID of ETHERTYPE_IPX in the SNAP header.
2384 * XXX - should we generate the same code both
2385 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2389 * This generates code to check both for the
2390 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2392 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2393 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2397 * Now we add code to check for SNAP frames with
2398 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2400 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2404 * Now we generate code to check for 802.3
2405 * frames in general.
2407 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2410 * Now add the check for 802.3 frames before the
2411 * check for Ethernet_802.2 and Ethernet_802.3,
2412 * as those checks should only be done on 802.3
2413 * frames, not on Ethernet frames.
2418 * Now add the check for Ethernet_II frames, and
2419 * do that before checking for the other frame
2422 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2426 case ETHERTYPE_ATALK
:
2427 case ETHERTYPE_AARP
:
2429 * EtherTalk (AppleTalk protocols on Ethernet link
2430 * layer) may use 802.2 encapsulation.
2434 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2435 * we check for an Ethernet type field less or equal than
2436 * 1500, which means it's an 802.3 length field.
2438 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2441 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2442 * SNAP packets with an organization code of
2443 * 0x080007 (Apple, for Appletalk) and a protocol
2444 * type of ETHERTYPE_ATALK (Appletalk).
2446 * 802.2-encapsulated ETHERTYPE_AARP packets are
2447 * SNAP packets with an organization code of
2448 * 0x000000 (encapsulated Ethernet) and a protocol
2449 * type of ETHERTYPE_AARP (Appletalk ARP).
2451 if (ll_proto
== ETHERTYPE_ATALK
)
2452 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2453 else /* ll_proto == ETHERTYPE_AARP */
2454 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2458 * Check for Ethernet encapsulation (Ethertalk
2459 * phase 1?); we just check for the Ethernet
2462 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2468 if (ll_proto
<= ETHERMTU
) {
2470 * This is an LLC SAP value, so the frames
2471 * that match would be 802.2 frames.
2472 * Check that the frame is an 802.2 frame
2473 * (i.e., that the length/type field is
2474 * a length field, <= ETHERMTU) and
2475 * then check the DSAP.
2477 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2478 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2483 * This is an Ethernet type, so compare
2484 * the length/type field with it (if
2485 * the frame is an 802.2 frame, the length
2486 * field will be <= ETHERMTU, and, as
2487 * "ll_proto" is > ETHERMTU, this test
2488 * will fail and the frame won't match,
2489 * which is what we want).
2491 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2496 static struct block
*
2497 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2500 * For DLT_NULL, the link-layer header is a 32-bit word
2501 * containing an AF_ value in *host* byte order, and for
2502 * DLT_ENC, the link-layer header begins with a 32-bit
2503 * word containing an AF_ value in host byte order.
2505 * In addition, if we're reading a saved capture file,
2506 * the host byte order in the capture may not be the
2507 * same as the host byte order on this machine.
2509 * For DLT_LOOP, the link-layer header is a 32-bit
2510 * word containing an AF_ value in *network* byte order.
2512 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2514 * The AF_ value is in host byte order, but the BPF
2515 * interpreter will convert it to network byte order.
2517 * If this is a save file, and it's from a machine
2518 * with the opposite byte order to ours, we byte-swap
2521 * Then we run it through "htonl()", and generate
2522 * code to compare against the result.
2524 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2525 ll_proto
= SWAPLONG(ll_proto
);
2526 ll_proto
= htonl(ll_proto
);
2528 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2532 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2533 * or IPv6 then we have an error.
2535 static struct block
*
2536 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2541 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2544 case ETHERTYPE_IPV6
:
2545 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2552 return gen_false(cstate
);
2556 * Generate code to match a particular packet type.
2558 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2559 * value, if <= ETHERMTU. We use that to determine whether to
2560 * match the type field or to check the type field for the special
2561 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2563 static struct block
*
2564 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2566 struct block
*b0
, *b1
;
2572 case LLCSAP_NETBEUI
:
2574 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2575 * so we check the DSAP and SSAP.
2577 * LLCSAP_IP checks for IP-over-802.2, rather
2578 * than IP-over-Ethernet or IP-over-SNAP.
2580 * XXX - should we check both the DSAP and the
2581 * SSAP, like this, or should we check just the
2582 * DSAP, as we do for other types <= ETHERMTU
2583 * (i.e., other SAP values)?
2585 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2586 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2592 * Ethernet_II frames, which are Ethernet
2593 * frames with a frame type of ETHERTYPE_IPX;
2595 * Ethernet_802.3 frames, which have a frame
2596 * type of LINUX_SLL_P_802_3;
2598 * Ethernet_802.2 frames, which are 802.3
2599 * frames with an 802.2 LLC header (i.e, have
2600 * a frame type of LINUX_SLL_P_802_2) and
2601 * with the IPX LSAP as the DSAP in the LLC
2604 * Ethernet_SNAP frames, which are 802.3
2605 * frames with an LLC header and a SNAP
2606 * header and with an OUI of 0x000000
2607 * (encapsulated Ethernet) and a protocol
2608 * ID of ETHERTYPE_IPX in the SNAP header.
2610 * First, do the checks on LINUX_SLL_P_802_2
2611 * frames; generate the check for either
2612 * Ethernet_802.2 or Ethernet_SNAP frames, and
2613 * then put a check for LINUX_SLL_P_802_2 frames
2616 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2617 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2619 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2623 * Now check for 802.3 frames and OR that with
2624 * the previous test.
2626 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2630 * Now add the check for Ethernet_II frames, and
2631 * do that before checking for the other frame
2634 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2638 case ETHERTYPE_ATALK
:
2639 case ETHERTYPE_AARP
:
2641 * EtherTalk (AppleTalk protocols on Ethernet link
2642 * layer) may use 802.2 encapsulation.
2646 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2647 * we check for the 802.2 protocol type in the
2648 * "Ethernet type" field.
2650 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2653 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2654 * SNAP packets with an organization code of
2655 * 0x080007 (Apple, for Appletalk) and a protocol
2656 * type of ETHERTYPE_ATALK (Appletalk).
2658 * 802.2-encapsulated ETHERTYPE_AARP packets are
2659 * SNAP packets with an organization code of
2660 * 0x000000 (encapsulated Ethernet) and a protocol
2661 * type of ETHERTYPE_AARP (Appletalk ARP).
2663 if (ll_proto
== ETHERTYPE_ATALK
)
2664 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2665 else /* ll_proto == ETHERTYPE_AARP */
2666 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2670 * Check for Ethernet encapsulation (Ethertalk
2671 * phase 1?); we just check for the Ethernet
2674 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2680 if (ll_proto
<= ETHERMTU
) {
2682 * This is an LLC SAP value, so the frames
2683 * that match would be 802.2 frames.
2684 * Check for the 802.2 protocol type
2685 * in the "Ethernet type" field, and
2686 * then check the DSAP.
2688 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2689 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2695 * This is an Ethernet type, so compare
2696 * the length/type field with it (if
2697 * the frame is an 802.2 frame, the length
2698 * field will be <= ETHERMTU, and, as
2699 * "ll_proto" is > ETHERMTU, this test
2700 * will fail and the frame won't match,
2701 * which is what we want).
2703 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2709 * Load a value relative to the beginning of the link-layer header after the
2712 static struct slist
*
2713 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2715 struct slist
*s1
, *s2
;
2718 * Generate code to load the length of the pflog header into
2719 * the register assigned to hold that length, if one has been
2720 * assigned. (If one hasn't been assigned, no code we've
2721 * generated uses that prefix, so we don't need to generate any
2724 if (cstate
->off_linkpl
.reg
!= -1) {
2726 * The length is in the first byte of the header.
2728 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2732 * Round it up to a multiple of 4.
2733 * Add 3, and clear the lower 2 bits.
2735 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2738 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2739 s2
->s
.k
= 0xfffffffc;
2743 * Now allocate a register to hold that value and store
2746 s2
= new_stmt(cstate
, BPF_ST
);
2747 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2751 * Now move it into the X register.
2753 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2761 static struct slist
*
2762 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2764 struct slist
*s1
, *s2
;
2765 struct slist
*sjeq_avs_cookie
;
2766 struct slist
*sjcommon
;
2769 * This code is not compatible with the optimizer, as
2770 * we are generating jmp instructions within a normal
2771 * slist of instructions
2773 cstate
->no_optimize
= 1;
2776 * Generate code to load the length of the radio header into
2777 * the register assigned to hold that length, if one has been
2778 * assigned. (If one hasn't been assigned, no code we've
2779 * generated uses that prefix, so we don't need to generate any
2782 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2783 * or always use the AVS header rather than the Prism header.
2784 * We load a 4-byte big-endian value at the beginning of the
2785 * raw packet data, and see whether, when masked with 0xFFFFF000,
2786 * it's equal to 0x80211000. If so, that indicates that it's
2787 * an AVS header (the masked-out bits are the version number).
2788 * Otherwise, it's a Prism header.
2790 * XXX - the Prism header is also, in theory, variable-length,
2791 * but no known software generates headers that aren't 144
2794 if (cstate
->off_linkhdr
.reg
!= -1) {
2798 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2802 * AND it with 0xFFFFF000.
2804 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2805 s2
->s
.k
= 0xFFFFF000;
2809 * Compare with 0x80211000.
2811 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2812 sjeq_avs_cookie
->s
.k
= 0x80211000;
2813 sappend(s1
, sjeq_avs_cookie
);
2818 * The 4 bytes at an offset of 4 from the beginning of
2819 * the AVS header are the length of the AVS header.
2820 * That field is big-endian.
2822 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2825 sjeq_avs_cookie
->s
.jt
= s2
;
2828 * Now jump to the code to allocate a register
2829 * into which to save the header length and
2830 * store the length there. (The "jump always"
2831 * instruction needs to have the k field set;
2832 * it's added to the PC, so, as we're jumping
2833 * over a single instruction, it should be 1.)
2835 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2837 sappend(s1
, sjcommon
);
2840 * Now for the code that handles the Prism header.
2841 * Just load the length of the Prism header (144)
2842 * into the A register. Have the test for an AVS
2843 * header branch here if we don't have an AVS header.
2845 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2848 sjeq_avs_cookie
->s
.jf
= s2
;
2851 * Now allocate a register to hold that value and store
2852 * it. The code for the AVS header will jump here after
2853 * loading the length of the AVS header.
2855 s2
= new_stmt(cstate
, BPF_ST
);
2856 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2858 sjcommon
->s
.jf
= s2
;
2861 * Now move it into the X register.
2863 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2871 static struct slist
*
2872 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2874 struct slist
*s1
, *s2
;
2877 * Generate code to load the length of the AVS header into
2878 * the register assigned to hold that length, if one has been
2879 * assigned. (If one hasn't been assigned, no code we've
2880 * generated uses that prefix, so we don't need to generate any
2883 if (cstate
->off_linkhdr
.reg
!= -1) {
2885 * The 4 bytes at an offset of 4 from the beginning of
2886 * the AVS header are the length of the AVS header.
2887 * That field is big-endian.
2889 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2893 * Now allocate a register to hold that value and store
2896 s2
= new_stmt(cstate
, BPF_ST
);
2897 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2901 * Now move it into the X register.
2903 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2911 static struct slist
*
2912 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2914 struct slist
*s1
, *s2
;
2917 * Generate code to load the length of the radiotap header into
2918 * the register assigned to hold that length, if one has been
2919 * assigned. (If one hasn't been assigned, no code we've
2920 * generated uses that prefix, so we don't need to generate any
2923 if (cstate
->off_linkhdr
.reg
!= -1) {
2925 * The 2 bytes at offsets of 2 and 3 from the beginning
2926 * of the radiotap header are the length of the radiotap
2927 * header; unfortunately, it's little-endian, so we have
2928 * to load it a byte at a time and construct the value.
2932 * Load the high-order byte, at an offset of 3, shift it
2933 * left a byte, and put the result in the X register.
2935 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2937 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2940 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2944 * Load the next byte, at an offset of 2, and OR the
2945 * value from the X register into it.
2947 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2950 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2954 * Now allocate a register to hold that value and store
2957 s2
= new_stmt(cstate
, BPF_ST
);
2958 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2962 * Now move it into the X register.
2964 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2973 * At the moment we treat PPI as normal Radiotap encoded
2974 * packets. The difference is in the function that generates
2975 * the code at the beginning to compute the header length.
2976 * Since this code generator of PPI supports bare 802.11
2977 * encapsulation only (i.e. the encapsulated DLT should be
2978 * DLT_IEEE802_11) we generate code to check for this too;
2979 * that's done in finish_parse().
2981 static struct slist
*
2982 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2984 struct slist
*s1
, *s2
;
2987 * Generate code to load the length of the radiotap header
2988 * into the register assigned to hold that length, if one has
2991 if (cstate
->off_linkhdr
.reg
!= -1) {
2993 * The 2 bytes at offsets of 2 and 3 from the beginning
2994 * of the radiotap header are the length of the radiotap
2995 * header; unfortunately, it's little-endian, so we have
2996 * to load it a byte at a time and construct the value.
3000 * Load the high-order byte, at an offset of 3, shift it
3001 * left a byte, and put the result in the X register.
3003 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3005 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
3008 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3012 * Load the next byte, at an offset of 2, and OR the
3013 * value from the X register into it.
3015 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
3018 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
3022 * Now allocate a register to hold that value and store
3025 s2
= new_stmt(cstate
, BPF_ST
);
3026 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3030 * Now move it into the X register.
3032 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3041 * Load a value relative to the beginning of the link-layer header after the 802.11
3042 * header, i.e. LLC_SNAP.
3043 * The link-layer header doesn't necessarily begin at the beginning
3044 * of the packet data; there might be a variable-length prefix containing
3045 * radio information.
3047 static struct slist
*
3048 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
3051 struct slist
*sjset_data_frame_1
;
3052 struct slist
*sjset_data_frame_2
;
3053 struct slist
*sjset_qos
;
3054 struct slist
*sjset_radiotap_flags_present
;
3055 struct slist
*sjset_radiotap_ext_present
;
3056 struct slist
*sjset_radiotap_tsft_present
;
3057 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
3058 struct slist
*s_roundup
;
3060 if (cstate
->off_linkpl
.reg
== -1) {
3062 * No register has been assigned to the offset of
3063 * the link-layer payload, which means nobody needs
3064 * it; don't bother computing it - just return
3065 * what we already have.
3071 * This code is not compatible with the optimizer, as
3072 * we are generating jmp instructions within a normal
3073 * slist of instructions
3075 cstate
->no_optimize
= 1;
3078 * If "s" is non-null, it has code to arrange that the X register
3079 * contains the length of the prefix preceding the link-layer
3082 * Otherwise, the length of the prefix preceding the link-layer
3083 * header is "off_outermostlinkhdr.constant_part".
3087 * There is no variable-length header preceding the
3088 * link-layer header.
3090 * Load the length of the fixed-length prefix preceding
3091 * the link-layer header (if any) into the X register,
3092 * and store it in the cstate->off_linkpl.reg register.
3093 * That length is off_outermostlinkhdr.constant_part.
3095 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3096 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3100 * The X register contains the offset of the beginning of the
3101 * link-layer header; add 24, which is the minimum length
3102 * of the MAC header for a data frame, to that, and store it
3103 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3104 * which is at the offset in the X register, with an indexed load.
3106 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3108 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3111 s2
= new_stmt(cstate
, BPF_ST
);
3112 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3115 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3120 * Check the Frame Control field to see if this is a data frame;
3121 * a data frame has the 0x08 bit (b3) in that field set and the
3122 * 0x04 bit (b2) clear.
3124 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3125 sjset_data_frame_1
->s
.k
= 0x08;
3126 sappend(s
, sjset_data_frame_1
);
3129 * If b3 is set, test b2, otherwise go to the first statement of
3130 * the rest of the program.
3132 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3133 sjset_data_frame_2
->s
.k
= 0x04;
3134 sappend(s
, sjset_data_frame_2
);
3135 sjset_data_frame_1
->s
.jf
= snext
;
3138 * If b2 is not set, this is a data frame; test the QoS bit.
3139 * Otherwise, go to the first statement of the rest of the
3142 sjset_data_frame_2
->s
.jt
= snext
;
3143 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3144 sjset_qos
->s
.k
= 0x80; /* QoS bit */
3145 sappend(s
, sjset_qos
);
3148 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3150 * Otherwise, go to the first statement of the rest of the
3153 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3154 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3156 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3159 s2
= new_stmt(cstate
, BPF_ST
);
3160 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3164 * If we have a radiotap header, look at it to see whether
3165 * there's Atheros padding between the MAC-layer header
3168 * Note: all of the fields in the radiotap header are
3169 * little-endian, so we byte-swap all of the values
3170 * we test against, as they will be loaded as big-endian
3173 * XXX - in the general case, we would have to scan through
3174 * *all* the presence bits, if there's more than one word of
3175 * presence bits. That would require a loop, meaning that
3176 * we wouldn't be able to run the filter in the kernel.
3178 * We assume here that the Atheros adapters that insert the
3179 * annoying padding don't have multiple antennae and therefore
3180 * do not generate radiotap headers with multiple presence words.
3182 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3184 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3185 * in the first presence flag word?
3187 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3191 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3192 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3193 sappend(s
, sjset_radiotap_flags_present
);
3196 * If not, skip all of this.
3198 sjset_radiotap_flags_present
->s
.jf
= snext
;
3201 * Otherwise, is the "extension" bit set in that word?
3203 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3204 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3205 sappend(s
, sjset_radiotap_ext_present
);
3206 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3209 * If so, skip all of this.
3211 sjset_radiotap_ext_present
->s
.jt
= snext
;
3214 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3216 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3217 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3218 sappend(s
, sjset_radiotap_tsft_present
);
3219 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3222 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3223 * at an offset of 16 from the beginning of the raw packet
3224 * data (8 bytes for the radiotap header and 8 bytes for
3227 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3230 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3233 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3235 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3236 sjset_tsft_datapad
->s
.k
= 0x20;
3237 sappend(s
, sjset_tsft_datapad
);
3240 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3241 * at an offset of 8 from the beginning of the raw packet
3242 * data (8 bytes for the radiotap header).
3244 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3247 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3250 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3252 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3253 sjset_notsft_datapad
->s
.k
= 0x20;
3254 sappend(s
, sjset_notsft_datapad
);
3257 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3258 * set, round the length of the 802.11 header to
3259 * a multiple of 4. Do that by adding 3 and then
3260 * dividing by and multiplying by 4, which we do by
3263 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3264 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3265 sappend(s
, s_roundup
);
3266 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3269 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3270 s2
->s
.k
= (bpf_u_int32
)~3;
3272 s2
= new_stmt(cstate
, BPF_ST
);
3273 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3276 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3277 sjset_tsft_datapad
->s
.jf
= snext
;
3278 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3279 sjset_notsft_datapad
->s
.jf
= snext
;
3281 sjset_qos
->s
.jf
= snext
;
3287 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3291 /* There is an implicit dependency between the link
3292 * payload and link header since the payload computation
3293 * includes the variable part of the header. Therefore,
3294 * if nobody else has allocated a register for the link
3295 * header and we need it, do it now. */
3296 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3297 cstate
->off_linkhdr
.reg
== -1)
3298 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3301 * For link-layer types that have a variable-length header
3302 * preceding the link-layer header, generate code to load
3303 * the offset of the link-layer header into the register
3304 * assigned to that offset, if any.
3306 * XXX - this, and the next switch statement, won't handle
3307 * encapsulation of 802.11 or 802.11+radio information in
3308 * some other protocol stack. That's significantly more
3311 switch (cstate
->outermostlinktype
) {
3313 case DLT_PRISM_HEADER
:
3314 s
= gen_load_prism_llprefixlen(cstate
);
3317 case DLT_IEEE802_11_RADIO_AVS
:
3318 s
= gen_load_avs_llprefixlen(cstate
);
3321 case DLT_IEEE802_11_RADIO
:
3322 s
= gen_load_radiotap_llprefixlen(cstate
);
3326 s
= gen_load_ppi_llprefixlen(cstate
);
3335 * For link-layer types that have a variable-length link-layer
3336 * header, generate code to load the offset of the link-layer
3337 * payload into the register assigned to that offset, if any.
3339 switch (cstate
->outermostlinktype
) {
3341 case DLT_IEEE802_11
:
3342 case DLT_PRISM_HEADER
:
3343 case DLT_IEEE802_11_RADIO_AVS
:
3344 case DLT_IEEE802_11_RADIO
:
3346 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3350 s
= gen_load_pflog_llprefixlen(cstate
);
3355 * If there is no initialization yet and we need variable
3356 * length offsets for VLAN, initialize them to zero
3358 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3361 if (cstate
->off_linkpl
.reg
== -1)
3362 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3363 if (cstate
->off_linktype
.reg
== -1)
3364 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3366 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3368 s2
= new_stmt(cstate
, BPF_ST
);
3369 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3371 s2
= new_stmt(cstate
, BPF_ST
);
3372 s2
->s
.k
= cstate
->off_linktype
.reg
;
3377 * If we have any offset-loading code, append all the
3378 * existing statements in the block to those statements,
3379 * and make the resulting list the list of statements
3383 sappend(s
, b
->stmts
);
3389 * Take an absolute offset, and:
3391 * if it has no variable part, return NULL;
3393 * if it has a variable part, generate code to load the register
3394 * containing that variable part into the X register, returning
3395 * a pointer to that code - if no register for that offset has
3396 * been allocated, allocate it first.
3398 * (The code to set that register will be generated later, but will
3399 * be placed earlier in the code sequence.)
3401 static struct slist
*
3402 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3406 if (off
->is_variable
) {
3407 if (off
->reg
== -1) {
3409 * We haven't yet assigned a register for the
3410 * variable part of the offset of the link-layer
3411 * header; allocate one.
3413 off
->reg
= alloc_reg(cstate
);
3417 * Load the register containing the variable part of the
3418 * offset of the link-layer header into the X register.
3420 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3425 * That offset isn't variable, there's no variable part,
3426 * so we don't need to generate any code.
3433 * Map an Ethernet type to the equivalent PPP type.
3436 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3444 case ETHERTYPE_IPV6
:
3445 ll_proto
= PPP_IPV6
;
3449 ll_proto
= PPP_DECNET
;
3452 case ETHERTYPE_ATALK
:
3453 ll_proto
= PPP_APPLE
;
3466 * I'm assuming the "Bridging PDU"s that go
3467 * over PPP are Spanning Tree Protocol
3470 ll_proto
= PPP_BRPDU
;
3481 * Generate any tests that, for encapsulation of a link-layer packet
3482 * inside another protocol stack, need to be done to check for those
3483 * link-layer packets (and that haven't already been done by a check
3484 * for that encapsulation).
3486 static struct block
*
3487 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3491 if (cstate
->is_encap
)
3492 return gen_encap_ll_check(cstate
);
3494 switch (cstate
->prevlinktype
) {
3498 * This is LANE-encapsulated Ethernet; check that the LANE
3499 * packet doesn't begin with an LE Control marker, i.e.
3500 * that it's data, not a control message.
3502 * (We've already generated a test for LANE.)
3504 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3510 * No such tests are necessary.
3518 * The three different values we should check for when checking for an
3519 * IPv6 packet with DLT_NULL.
3521 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3522 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3523 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3526 * Generate code to match a particular packet type by matching the
3527 * link-layer type field or fields in the 802.2 LLC header.
3529 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3530 * value, if <= ETHERMTU.
3532 static struct block
*
3533 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3535 struct block
*b0
, *b1
, *b2
;
3537 /* are we checking MPLS-encapsulated packets? */
3538 if (cstate
->label_stack_depth
> 0)
3539 return gen_mpls_linktype(cstate
, ll_proto
);
3541 switch (cstate
->linktype
) {
3544 case DLT_NETANALYZER
:
3545 case DLT_NETANALYZER_TRANSPARENT
:
3546 /* Geneve has an EtherType regardless of whether there is an
3547 * L2 header. VXLAN always has an EtherType. */
3548 if (!cstate
->is_encap
)
3549 b0
= gen_prevlinkhdr_check(cstate
);
3553 b1
= gen_ether_linktype(cstate
, ll_proto
);
3564 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3568 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3572 case DLT_IEEE802_11
:
3573 case DLT_PRISM_HEADER
:
3574 case DLT_IEEE802_11_RADIO_AVS
:
3575 case DLT_IEEE802_11_RADIO
:
3578 * Check that we have a data frame.
3580 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3581 IEEE80211_FC0_TYPE_DATA
,
3582 IEEE80211_FC0_TYPE_MASK
);
3585 * Now check for the specified link-layer type.
3587 b1
= gen_llc_linktype(cstate
, ll_proto
);
3594 * XXX - check for LLC frames.
3596 return gen_llc_linktype(cstate
, ll_proto
);
3601 * XXX - check for LLC PDUs, as per IEEE 802.5.
3603 return gen_llc_linktype(cstate
, ll_proto
);
3606 case DLT_ATM_RFC1483
:
3608 case DLT_IP_OVER_FC
:
3609 return gen_llc_linktype(cstate
, ll_proto
);
3614 * Check for an LLC-encapsulated version of this protocol;
3615 * if we were checking for LANE, linktype would no longer
3618 * Check for LLC encapsulation and then check the protocol.
3620 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3621 b1
= gen_llc_linktype(cstate
, ll_proto
);
3627 return gen_linux_sll_linktype(cstate
, ll_proto
);
3631 case DLT_SLIP_BSDOS
:
3634 * These types don't provide any type field; packets
3635 * are always IPv4 or IPv6.
3637 * XXX - for IPv4, check for a version number of 4, and,
3638 * for IPv6, check for a version number of 6?
3643 /* Check for a version number of 4. */
3644 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3646 case ETHERTYPE_IPV6
:
3647 /* Check for a version number of 6. */
3648 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3651 return gen_false(cstate
); /* always false */
3657 * Raw IPv4, so no type field.
3659 if (ll_proto
== ETHERTYPE_IP
)
3660 return gen_true(cstate
); /* always true */
3662 /* Checking for something other than IPv4; always false */
3663 return gen_false(cstate
);
3668 * Raw IPv6, so no type field.
3670 if (ll_proto
== ETHERTYPE_IPV6
)
3671 return gen_true(cstate
); /* always true */
3673 /* Checking for something other than IPv6; always false */
3674 return gen_false(cstate
);
3679 case DLT_PPP_SERIAL
:
3682 * We use Ethernet protocol types inside libpcap;
3683 * map them to the corresponding PPP protocol types.
3685 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3686 ethertype_to_ppptype(ll_proto
));
3691 * We use Ethernet protocol types inside libpcap;
3692 * map them to the corresponding PPP protocol types.
3698 * Also check for Van Jacobson-compressed IP.
3699 * XXX - do this for other forms of PPP?
3701 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3702 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3704 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3709 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3710 ethertype_to_ppptype(ll_proto
));
3720 return (gen_loopback_linktype(cstate
, AF_INET
));
3722 case ETHERTYPE_IPV6
:
3724 * AF_ values may, unfortunately, be platform-
3725 * dependent; AF_INET isn't, because everybody
3726 * used 4.2BSD's value, but AF_INET6 is, because
3727 * 4.2BSD didn't have a value for it (given that
3728 * IPv6 didn't exist back in the early 1980's),
3729 * and they all picked their own values.
3731 * This means that, if we're reading from a
3732 * savefile, we need to check for all the
3735 * If we're doing a live capture, we only need
3736 * to check for this platform's value; however,
3737 * Npcap uses 24, which isn't Windows's AF_INET6
3738 * value. (Given the multiple different values,
3739 * programs that read pcap files shouldn't be
3740 * checking for their platform's AF_INET6 value
3741 * anyway, they should check for all of the
3742 * possible values. and they might as well do
3743 * that even for live captures.)
3745 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3747 * Savefile - check for all three
3748 * possible IPv6 values.
3750 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3751 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3753 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3758 * Live capture, so we only need to
3759 * check for the value used on this
3764 * Npcap doesn't use Windows's AF_INET6,
3765 * as that collides with AF_IPX on
3766 * some BSDs (both have the value 23).
3767 * Instead, it uses 24.
3769 return (gen_loopback_linktype(cstate
, 24));
3772 return (gen_loopback_linktype(cstate
, AF_INET6
));
3773 #else /* AF_INET6 */
3775 * I guess this platform doesn't support
3776 * IPv6, so we just reject all packets.
3778 return gen_false(cstate
);
3779 #endif /* AF_INET6 */
3785 * Not a type on which we support filtering.
3786 * XXX - support those that have AF_ values
3787 * #defined on this platform, at least?
3789 return gen_false(cstate
);
3794 * af field is host byte order in contrast to the rest of
3797 if (ll_proto
== ETHERTYPE_IP
)
3798 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3800 else if (ll_proto
== ETHERTYPE_IPV6
)
3801 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3804 return gen_false(cstate
);
3808 case DLT_ARCNET_LINUX
:
3810 * XXX should we check for first fragment if the protocol
3816 return gen_false(cstate
);
3818 case ETHERTYPE_IPV6
:
3819 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3823 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3825 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3831 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3833 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3838 case ETHERTYPE_REVARP
:
3839 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3842 case ETHERTYPE_ATALK
:
3843 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3850 case ETHERTYPE_ATALK
:
3851 return gen_true(cstate
);
3853 return gen_false(cstate
);
3859 * XXX - assumes a 2-byte Frame Relay header with
3860 * DLCI and flags. What if the address is longer?
3866 * Check for the special NLPID for IP.
3868 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3870 case ETHERTYPE_IPV6
:
3872 * Check for the special NLPID for IPv6.
3874 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3878 * Check for several OSI protocols.
3880 * Frame Relay packets typically have an OSI
3881 * NLPID at the beginning; we check for each
3884 * What we check for is the NLPID and a frame
3885 * control field of UI, i.e. 0x03 followed
3888 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3889 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3890 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3896 return gen_false(cstate
);
3901 break; // not implemented
3903 case DLT_JUNIPER_MFR
:
3904 case DLT_JUNIPER_MLFR
:
3905 case DLT_JUNIPER_MLPPP
:
3906 case DLT_JUNIPER_ATM1
:
3907 case DLT_JUNIPER_ATM2
:
3908 case DLT_JUNIPER_PPPOE
:
3909 case DLT_JUNIPER_PPPOE_ATM
:
3910 case DLT_JUNIPER_GGSN
:
3911 case DLT_JUNIPER_ES
:
3912 case DLT_JUNIPER_MONITOR
:
3913 case DLT_JUNIPER_SERVICES
:
3914 case DLT_JUNIPER_ETHER
:
3915 case DLT_JUNIPER_PPP
:
3916 case DLT_JUNIPER_FRELAY
:
3917 case DLT_JUNIPER_CHDLC
:
3918 case DLT_JUNIPER_VP
:
3919 case DLT_JUNIPER_ST
:
3920 case DLT_JUNIPER_ISM
:
3921 case DLT_JUNIPER_VS
:
3922 case DLT_JUNIPER_SRX_E2E
:
3923 case DLT_JUNIPER_FIBRECHANNEL
:
3924 case DLT_JUNIPER_ATM_CEMIC
:
3926 /* just lets verify the magic number for now -
3927 * on ATM we may have up to 6 different encapsulations on the wire
3928 * and need a lot of heuristics to figure out that the payload
3931 * FIXME encapsulation specific BPF_ filters
3933 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3935 case DLT_BACNET_MS_TP
:
3936 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3939 return gen_ipnet_linktype(cstate
, ll_proto
);
3941 case DLT_LINUX_IRDA
:
3944 case DLT_MTP2_WITH_PHDR
:
3947 case DLT_LINUX_LAPD
:
3948 case DLT_USB_FREEBSD
:
3950 case DLT_USB_LINUX_MMAPPED
:
3952 case DLT_BLUETOOTH_HCI_H4
:
3953 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3955 case DLT_CAN_SOCKETCAN
:
3956 case DLT_IEEE802_15_4
:
3957 case DLT_IEEE802_15_4_LINUX
:
3958 case DLT_IEEE802_15_4_NONASK_PHY
:
3959 case DLT_IEEE802_15_4_NOFCS
:
3960 case DLT_IEEE802_15_4_TAP
:
3961 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3964 case DLT_IPMB_KONTRON
:
3968 /* Using the fixed-size NFLOG header it is possible to tell only
3969 * the address family of the packet, other meaningful data is
3970 * either missing or behind TLVs.
3972 break; // not implemented
3976 * Does this link-layer header type have a field
3977 * indicating the type of the next protocol? If
3978 * so, off_linktype.constant_part will be the offset of that
3979 * field in the packet; if not, it will be OFFSET_NOT_SET.
3981 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3983 * Yes; assume it's an Ethernet type. (If
3984 * it's not, it needs to be handled specially
3987 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3991 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
3992 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3996 * Check for an LLC SNAP packet with a given organization code and
3997 * protocol type; we check the entire contents of the 802.2 LLC and
3998 * snap headers, checking for DSAP and SSAP of SNAP and a control
3999 * field of 0x03 in the LLC header, and for the specified organization
4000 * code and protocol type in the SNAP header.
4002 static struct block
*
4003 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
4005 u_char snapblock
[8];
4007 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
4008 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
4009 snapblock
[2] = 0x03; /* control = UI */
4010 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
4011 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
4012 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
4013 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
4014 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
4015 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
4019 * Generate code to match frames with an LLC header.
4021 static struct block
*
4022 gen_llc_internal(compiler_state_t
*cstate
)
4024 struct block
*b0
, *b1
;
4026 switch (cstate
->linktype
) {
4030 * We check for an Ethernet type field less or equal than
4031 * 1500, which means it's an 802.3 length field.
4033 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
4036 * Now check for the purported DSAP and SSAP not being
4037 * 0xFF, to rule out NetWare-over-802.3.
4039 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
4046 * We check for LLC traffic.
4048 b0
= gen_atmtype_llc(cstate
);
4051 case DLT_IEEE802
: /* Token Ring */
4053 * XXX - check for LLC frames.
4055 return gen_true(cstate
);
4059 * XXX - check for LLC frames.
4061 return gen_true(cstate
);
4063 case DLT_ATM_RFC1483
:
4065 * For LLC encapsulation, these are defined to have an
4068 * For VC encapsulation, they don't, but there's no
4069 * way to check for that; the protocol used on the VC
4070 * is negotiated out of band.
4072 return gen_true(cstate
);
4074 case DLT_IEEE802_11
:
4075 case DLT_PRISM_HEADER
:
4076 case DLT_IEEE802_11_RADIO
:
4077 case DLT_IEEE802_11_RADIO_AVS
:
4080 * Check that we have a data frame.
4082 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4083 IEEE80211_FC0_TYPE_DATA
,
4084 IEEE80211_FC0_TYPE_MASK
);
4087 fail_kw_on_dlt(cstate
, "llc");
4093 gen_llc(compiler_state_t
*cstate
)
4096 * Catch errors reported by us and routines below us, and return NULL
4099 if (setjmp(cstate
->top_ctx
))
4102 return gen_llc_internal(cstate
);
4106 gen_llc_i(compiler_state_t
*cstate
)
4108 struct block
*b0
, *b1
;
4112 * Catch errors reported by us and routines below us, and return NULL
4115 if (setjmp(cstate
->top_ctx
))
4119 * Check whether this is an LLC frame.
4121 b0
= gen_llc_internal(cstate
);
4124 * Load the control byte and test the low-order bit; it must
4125 * be clear for I frames.
4127 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4128 b1
= new_block(cstate
, JMP(BPF_JSET
));
4137 gen_llc_s(compiler_state_t
*cstate
)
4139 struct block
*b0
, *b1
;
4142 * Catch errors reported by us and routines below us, and return NULL
4145 if (setjmp(cstate
->top_ctx
))
4149 * Check whether this is an LLC frame.
4151 b0
= gen_llc_internal(cstate
);
4154 * Now compare the low-order 2 bit of the control byte against
4155 * the appropriate value for S frames.
4157 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4163 gen_llc_u(compiler_state_t
*cstate
)
4165 struct block
*b0
, *b1
;
4168 * Catch errors reported by us and routines below us, and return NULL
4171 if (setjmp(cstate
->top_ctx
))
4175 * Check whether this is an LLC frame.
4177 b0
= gen_llc_internal(cstate
);
4180 * Now compare the low-order 2 bit of the control byte against
4181 * the appropriate value for U frames.
4183 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4189 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4191 struct block
*b0
, *b1
;
4194 * Catch errors reported by us and routines below us, and return NULL
4197 if (setjmp(cstate
->top_ctx
))
4201 * Check whether this is an LLC frame.
4203 b0
= gen_llc_internal(cstate
);
4206 * Now check for an S frame with the appropriate type.
4208 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4214 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4216 struct block
*b0
, *b1
;
4219 * Catch errors reported by us and routines below us, and return NULL
4222 if (setjmp(cstate
->top_ctx
))
4226 * Check whether this is an LLC frame.
4228 b0
= gen_llc_internal(cstate
);
4231 * Now check for a U frame with the appropriate type.
4233 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4239 * Generate code to match a particular packet type, for link-layer types
4240 * using 802.2 LLC headers.
4242 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4243 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4245 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4246 * value, if <= ETHERMTU. We use that to determine whether to
4247 * match the DSAP or both DSAP and LSAP or to check the OUI and
4248 * protocol ID in a SNAP header.
4250 static struct block
*
4251 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4254 * XXX - handle token-ring variable-length header.
4260 case LLCSAP_NETBEUI
:
4262 * XXX - should we check both the DSAP and the
4263 * SSAP, like this, or should we check just the
4264 * DSAP, as we do for other SAP values?
4266 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4267 ((ll_proto
<< 8) | ll_proto
));
4271 * XXX - are there ever SNAP frames for IPX on
4272 * non-Ethernet 802.x networks?
4274 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4276 case ETHERTYPE_ATALK
:
4278 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4279 * SNAP packets with an organization code of
4280 * 0x080007 (Apple, for Appletalk) and a protocol
4281 * type of ETHERTYPE_ATALK (Appletalk).
4283 * XXX - check for an organization code of
4284 * encapsulated Ethernet as well?
4286 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4290 * XXX - we don't have to check for IPX 802.3
4291 * here, but should we check for the IPX Ethertype?
4293 if (ll_proto
<= ETHERMTU
) {
4295 * This is an LLC SAP value, so check
4298 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4301 * This is an Ethernet type; we assume that it's
4302 * unlikely that it'll appear in the right place
4303 * at random, and therefore check only the
4304 * location that would hold the Ethernet type
4305 * in a SNAP frame with an organization code of
4306 * 0x000000 (encapsulated Ethernet).
4308 * XXX - if we were to check for the SNAP DSAP and
4309 * LSAP, as per XXX, and were also to check for an
4310 * organization code of 0x000000 (encapsulated
4311 * Ethernet), we'd do
4313 * return gen_snap(cstate, 0x000000, ll_proto);
4315 * here; for now, we don't, as per the above.
4316 * I don't know whether it's worth the extra CPU
4317 * time to do the right check or not.
4319 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4324 static struct block
*
4325 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4326 int dir
, u_int src_off
, u_int dst_off
)
4328 struct block
*b0
, *b1
;
4342 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4343 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4349 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4350 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4360 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4367 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4371 static struct block
*
4372 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4373 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4375 struct block
*b0
, *b1
;
4378 * Code below needs to access four separate 32-bit parts of the 128-bit
4379 * IPv6 address and mask. In some OSes this is as simple as using the
4380 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4381 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4382 * far as libpcap sees it. Hence copy the data before use to avoid
4383 * potential unaligned memory access and the associated compiler
4384 * warnings (whether genuine or not).
4386 bpf_u_int32 a
[4], m
[4];
4399 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4400 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4406 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4407 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4417 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4424 /* this order is important */
4425 memcpy(a
, addr
, sizeof(a
));
4426 memcpy(m
, mask
, sizeof(m
));
4427 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4428 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4430 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4432 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4438 static struct block
*
4439 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4441 register struct block
*b0
, *b1
;
4445 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4448 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4451 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4452 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4458 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4459 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4469 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4477 * Like gen_ehostop, but for DLT_FDDI
4479 static struct block
*
4480 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4482 struct block
*b0
, *b1
;
4486 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4489 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4492 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4493 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4499 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4500 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4510 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4518 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4520 static struct block
*
4521 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4523 register struct block
*b0
, *b1
;
4527 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4530 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4533 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4534 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4540 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4541 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4551 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4559 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4560 * various 802.11 + radio headers.
4562 static struct block
*
4563 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4565 register struct block
*b0
, *b1
, *b2
;
4566 register struct slist
*s
;
4568 #ifdef ENABLE_WLAN_FILTERING_PATCH
4571 * We need to disable the optimizer because the optimizer is buggy
4572 * and wipes out some LD instructions generated by the below
4573 * code to validate the Frame Control bits
4575 cstate
->no_optimize
= 1;
4576 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4583 * For control frames, there is no SA.
4585 * For management frames, SA is at an
4586 * offset of 10 from the beginning of
4589 * For data frames, SA is at an offset
4590 * of 10 from the beginning of the packet
4591 * if From DS is clear, at an offset of
4592 * 16 from the beginning of the packet
4593 * if From DS is set and To DS is clear,
4594 * and an offset of 24 from the beginning
4595 * of the packet if From DS is set and To DS
4600 * Generate the tests to be done for data frames
4603 * First, check for To DS set, i.e. check "link[1] & 0x01".
4605 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4606 b1
= new_block(cstate
, JMP(BPF_JSET
));
4607 b1
->s
.k
= 0x01; /* To DS */
4611 * If To DS is set, the SA is at 24.
4613 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4617 * Now, check for To DS not set, i.e. check
4618 * "!(link[1] & 0x01)".
4620 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4621 b2
= new_block(cstate
, JMP(BPF_JSET
));
4622 b2
->s
.k
= 0x01; /* To DS */
4627 * If To DS is not set, the SA is at 16.
4629 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4633 * Now OR together the last two checks. That gives
4634 * the complete set of checks for data frames with
4640 * Now check for From DS being set, and AND that with
4641 * the ORed-together checks.
4643 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4644 b1
= new_block(cstate
, JMP(BPF_JSET
));
4645 b1
->s
.k
= 0x02; /* From DS */
4650 * Now check for data frames with From DS not set.
4652 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4653 b2
= new_block(cstate
, JMP(BPF_JSET
));
4654 b2
->s
.k
= 0x02; /* From DS */
4659 * If From DS isn't set, the SA is at 10.
4661 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4665 * Now OR together the checks for data frames with
4666 * From DS not set and for data frames with From DS
4667 * set; that gives the checks done for data frames.
4672 * Now check for a data frame.
4673 * I.e, check "link[0] & 0x08".
4675 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4676 b1
= new_block(cstate
, JMP(BPF_JSET
));
4681 * AND that with the checks done for data frames.
4686 * If the high-order bit of the type value is 0, this
4687 * is a management frame.
4688 * I.e, check "!(link[0] & 0x08)".
4690 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4691 b2
= new_block(cstate
, JMP(BPF_JSET
));
4697 * For management frames, the SA is at 10.
4699 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4703 * OR that with the checks done for data frames.
4704 * That gives the checks done for management and
4710 * If the low-order bit of the type value is 1,
4711 * this is either a control frame or a frame
4712 * with a reserved type, and thus not a
4715 * I.e., check "!(link[0] & 0x04)".
4717 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4718 b1
= new_block(cstate
, JMP(BPF_JSET
));
4724 * AND that with the checks for data and management
4734 * For control frames, there is no DA.
4736 * For management frames, DA is at an
4737 * offset of 4 from the beginning of
4740 * For data frames, DA is at an offset
4741 * of 4 from the beginning of the packet
4742 * if To DS is clear and at an offset of
4743 * 16 from the beginning of the packet
4748 * Generate the tests to be done for data frames.
4750 * First, check for To DS set, i.e. "link[1] & 0x01".
4752 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4753 b1
= new_block(cstate
, JMP(BPF_JSET
));
4754 b1
->s
.k
= 0x01; /* To DS */
4758 * If To DS is set, the DA is at 16.
4760 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4764 * Now, check for To DS not set, i.e. check
4765 * "!(link[1] & 0x01)".
4767 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4768 b2
= new_block(cstate
, JMP(BPF_JSET
));
4769 b2
->s
.k
= 0x01; /* To DS */
4774 * If To DS is not set, the DA is at 4.
4776 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4780 * Now OR together the last two checks. That gives
4781 * the complete set of checks for data frames.
4786 * Now check for a data frame.
4787 * I.e, check "link[0] & 0x08".
4789 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4790 b1
= new_block(cstate
, JMP(BPF_JSET
));
4795 * AND that with the checks done for data frames.
4800 * If the high-order bit of the type value is 0, this
4801 * is a management frame.
4802 * I.e, check "!(link[0] & 0x08)".
4804 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4805 b2
= new_block(cstate
, JMP(BPF_JSET
));
4811 * For management frames, the DA is at 4.
4813 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4817 * OR that with the checks done for data frames.
4818 * That gives the checks done for management and
4824 * If the low-order bit of the type value is 1,
4825 * this is either a control frame or a frame
4826 * with a reserved type, and thus not a
4829 * I.e., check "!(link[0] & 0x04)".
4831 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4832 b1
= new_block(cstate
, JMP(BPF_JSET
));
4838 * AND that with the checks for data and management
4845 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4846 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4852 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4853 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4858 * XXX - add BSSID keyword?
4861 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4865 * Not present in CTS or ACK control frames.
4867 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4868 IEEE80211_FC0_TYPE_MASK
);
4870 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4871 IEEE80211_FC0_SUBTYPE_MASK
);
4873 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4874 IEEE80211_FC0_SUBTYPE_MASK
);
4878 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4884 * Not present in control frames.
4886 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4887 IEEE80211_FC0_TYPE_MASK
);
4889 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4895 * Present only if the direction mask has both "From DS"
4896 * and "To DS" set. Neither control frames nor management
4897 * frames should have both of those set, so we don't
4898 * check the frame type.
4900 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4901 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4902 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4908 * Not present in management frames; addr1 in other
4913 * If the high-order bit of the type value is 0, this
4914 * is a management frame.
4915 * I.e, check "(link[0] & 0x08)".
4917 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4918 b1
= new_block(cstate
, JMP(BPF_JSET
));
4925 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4928 * AND that with the check of addr1.
4935 * Not present in management frames; addr2, if present,
4940 * Not present in CTS or ACK control frames.
4942 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4943 IEEE80211_FC0_TYPE_MASK
);
4945 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4946 IEEE80211_FC0_SUBTYPE_MASK
);
4948 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4949 IEEE80211_FC0_SUBTYPE_MASK
);
4955 * If the high-order bit of the type value is 0, this
4956 * is a management frame.
4957 * I.e, check "(link[0] & 0x08)".
4959 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4960 b1
= new_block(cstate
, JMP(BPF_JSET
));
4965 * AND that with the check for frames other than
4966 * CTS and ACK frames.
4973 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4982 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4983 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4984 * as the RFC states.)
4986 static struct block
*
4987 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4989 register struct block
*b0
, *b1
;
4993 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4996 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4999 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
5000 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
5006 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
5007 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
5017 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5025 * This is quite tricky because there may be pad bytes in front of the
5026 * DECNET header, and then there are two possible data packet formats that
5027 * carry both src and dst addresses, plus 5 packet types in a format that
5028 * carries only the src node, plus 2 types that use a different format and
5029 * also carry just the src node.
5033 * Instead of doing those all right, we just look for data packets with
5034 * 0 or 1 bytes of padding. If you want to look at other packets, that
5035 * will require a lot more hacking.
5037 * To add support for filtering on DECNET "areas" (network numbers)
5038 * one would want to add a "mask" argument to this routine. That would
5039 * make the filter even more inefficient, although one could be clever
5040 * and not generate masking instructions if the mask is 0xFFFF.
5042 static struct block
*
5043 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
5045 struct block
*b0
, *b1
, *b2
, *tmp
;
5046 u_int offset_lh
; /* offset if long header is received */
5047 u_int offset_sh
; /* offset if short header is received */
5052 offset_sh
= 1; /* follows flags */
5053 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5057 offset_sh
= 3; /* follows flags, dstnode */
5058 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5062 /* Inefficient because we do our Calvinball dance twice */
5063 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5064 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5070 /* Inefficient because we do our Calvinball dance twice */
5071 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5072 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5082 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5090 * In a DECnet message inside an Ethernet frame the first two bytes
5091 * immediately after EtherType are the [litle-endian] DECnet message
5092 * length, which is irrelevant in this context.
5094 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5095 * 8-bit bitmap of the optional padding before the packet route header.
5096 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5097 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5098 * means there aren't any PAD bytes after the bitmap, so the header
5099 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5100 * is set to 0, thus the header begins at the third byte.
5102 * The header can be in several (as mentioned above) formats, all of
5103 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5104 * (PF, "pad field") set to 0 regardless of any padding present before
5105 * the header. "Short header" means bits 0-2 of the bitmap encode the
5106 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5108 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5109 * values and the masks, this maps to the required single bytes of
5110 * the message correctly on both big-endian and little-endian hosts.
5111 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5112 * because the wire encoding is little-endian and BPF multiple-byte
5113 * loads are big-endian. When the destination address is near enough
5114 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5117 /* Check for pad = 1, long header case */
5118 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5119 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5120 BPF_H
, SWAPSHORT(addr
));
5122 /* Check for pad = 0, long header case */
5123 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5124 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5128 /* Check for pad = 1, short header case */
5130 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5131 0x81020000U
| SWAPSHORT(addr
),
5134 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5135 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5140 /* Check for pad = 0, short header case */
5142 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5143 0x02000000U
| SWAPSHORT(addr
) << 8,
5146 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5147 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5157 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5158 * test the bottom-of-stack bit, and then check the version number
5159 * field in the IP header.
5161 static struct block
*
5162 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5164 struct block
*b0
, *b1
;
5169 /* match the bottom-of-stack bit */
5170 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5171 /* match the IPv4 version number */
5172 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5176 case ETHERTYPE_IPV6
:
5177 /* match the bottom-of-stack bit */
5178 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5179 /* match the IPv4 version number */
5180 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5185 /* FIXME add other L3 proto IDs */
5186 bpf_error(cstate
, "unsupported protocol over mpls");
5191 static struct block
*
5192 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5193 int proto
, int dir
, int type
)
5195 struct block
*b0
, *b1
;
5200 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5202 * Only check for non-IPv4 addresses if we're not
5203 * checking MPLS-encapsulated packets.
5205 if (cstate
->label_stack_depth
== 0) {
5206 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5208 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5214 // "link net NETNAME" and variations thereof
5215 break; // invalid qualifier
5218 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5219 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5224 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5225 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5230 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5231 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5242 break; // invalid qualifier
5245 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5246 b1
= gen_dnhostop(cstate
, addr
, dir
);
5277 break; // invalid qualifier
5282 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5283 type
== Q_NET
? "ip net" : "ip host");
5288 static struct block
*
5289 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5290 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5292 struct block
*b0
, *b1
;
5298 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5299 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5341 break; // invalid qualifier
5346 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5347 type
== Q_NET
? "ip6 net" : "ip6 host");
5353 static struct block
*
5354 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5355 struct addrinfo
*alist
, int proto
, int dir
)
5357 struct block
*b0
, *b1
, *tmp
;
5358 struct addrinfo
*ai
;
5359 struct sockaddr_in
*sin
;
5362 bpf_error(cstate
, "direction applied to 'gateway'");
5369 switch (cstate
->linktype
) {
5371 case DLT_NETANALYZER
:
5372 case DLT_NETANALYZER_TRANSPARENT
:
5373 b1
= gen_prevlinkhdr_check(cstate
);
5374 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5379 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5382 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5384 case DLT_IEEE802_11
:
5385 case DLT_PRISM_HEADER
:
5386 case DLT_IEEE802_11_RADIO_AVS
:
5387 case DLT_IEEE802_11_RADIO
:
5389 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5391 case DLT_IP_OVER_FC
:
5392 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5396 * This is LLC-multiplexed traffic; if it were
5397 * LANE, cstate->linktype would have been set to
5403 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5406 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5408 * Does it have an address?
5410 if (ai
->ai_addr
!= NULL
) {
5412 * Yes. Is it an IPv4 address?
5414 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5416 * Generate an entry for it.
5418 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5419 tmp
= gen_host(cstate
,
5420 ntohl(sin
->sin_addr
.s_addr
),
5421 0xffffffff, proto
, Q_OR
, Q_HOST
);
5423 * Is it the *first* IPv4 address?
5427 * Yes, so start with it.
5432 * No, so OR it into the
5444 * No IPv4 addresses found.
5452 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5457 static struct block
*
5458 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5461 struct block
*b1
= NULL
;
5466 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5470 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5474 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5478 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5481 #ifndef IPPROTO_IGMP
5482 #define IPPROTO_IGMP 2
5486 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5489 #ifndef IPPROTO_IGRP
5490 #define IPPROTO_IGRP 9
5493 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5497 #define IPPROTO_PIM 103
5501 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5504 #ifndef IPPROTO_VRRP
5505 #define IPPROTO_VRRP 112
5509 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5512 #ifndef IPPROTO_CARP
5513 #define IPPROTO_CARP 112
5517 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5521 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5525 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5529 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5533 break; // invalid syntax
5536 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5540 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5544 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5548 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5552 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5556 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5560 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5564 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5567 #ifndef IPPROTO_ICMPV6
5568 #define IPPROTO_ICMPV6 58
5571 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5575 #define IPPROTO_AH 51
5578 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5582 #define IPPROTO_ESP 50
5585 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5589 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5593 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5597 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5600 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5601 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5602 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5604 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5606 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5608 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5612 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5613 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5614 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5616 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5618 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5620 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5624 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5625 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5626 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5628 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5633 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5634 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5639 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5640 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5642 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5644 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5649 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5650 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5655 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5656 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5661 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5665 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5669 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5673 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5677 break; // invalid syntax
5684 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5688 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5691 * Catch errors reported by us and routines below us, and return NULL
5694 if (setjmp(cstate
->top_ctx
))
5697 return gen_proto_abbrev_internal(cstate
, proto
);
5700 static struct block
*
5701 gen_ipfrag(compiler_state_t
*cstate
)
5706 /* not IPv4 frag other than the first frag */
5707 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5708 b
= new_block(cstate
, JMP(BPF_JSET
));
5717 * Generate a comparison to a port value in the transport-layer header
5718 * at the specified offset from the beginning of that header.
5720 * XXX - this handles a variable-length prefix preceding the link-layer
5721 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5722 * variable-length link-layer headers (such as Token Ring or 802.11
5725 static struct block
*
5726 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5728 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5731 static struct block
*
5732 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5734 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5737 static struct block
*
5738 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5740 struct block
*b0
, *b1
, *tmp
;
5742 /* ip proto 'proto' and not a fragment other than the first fragment */
5743 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5744 b0
= gen_ipfrag(cstate
);
5749 b1
= gen_portatom(cstate
, 0, port
);
5753 b1
= gen_portatom(cstate
, 2, port
);
5757 tmp
= gen_portatom(cstate
, 0, port
);
5758 b1
= gen_portatom(cstate
, 2, port
);
5764 tmp
= gen_portatom(cstate
, 0, port
);
5765 b1
= gen_portatom(cstate
, 2, port
);
5775 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5787 static struct block
*
5788 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5790 struct block
*b0
, *b1
, *tmp
;
5795 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5796 * not LLC encapsulation with LLCSAP_IP.
5798 * For IEEE 802 networks - which includes 802.5 token ring
5799 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5800 * says that SNAP encapsulation is used, not LLC encapsulation
5803 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5804 * RFC 2225 say that SNAP encapsulation is used, not LLC
5805 * encapsulation with LLCSAP_IP.
5807 * So we always check for ETHERTYPE_IP.
5809 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5815 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5819 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5820 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5822 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5834 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5836 struct block
*b0
, *b1
, *tmp
;
5838 /* ip6 proto 'proto' */
5839 /* XXX - catch the first fragment of a fragmented packet? */
5840 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5844 b1
= gen_portatom6(cstate
, 0, port
);
5848 b1
= gen_portatom6(cstate
, 2, port
);
5852 tmp
= gen_portatom6(cstate
, 0, port
);
5853 b1
= gen_portatom6(cstate
, 2, port
);
5859 tmp
= gen_portatom6(cstate
, 0, port
);
5860 b1
= gen_portatom6(cstate
, 2, port
);
5872 static struct block
*
5873 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5875 struct block
*b0
, *b1
, *tmp
;
5877 /* link proto ip6 */
5878 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5884 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5888 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5889 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5891 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5902 /* gen_portrange code */
5903 static struct block
*
5904 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5907 struct block
*b1
, *b2
;
5911 * Reverse the order of the ports, so v1 is the lower one.
5920 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5921 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5928 static struct block
*
5929 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5930 bpf_u_int32 proto
, int dir
)
5932 struct block
*b0
, *b1
, *tmp
;
5934 /* ip proto 'proto' and not a fragment other than the first fragment */
5935 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5936 b0
= gen_ipfrag(cstate
);
5941 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5945 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5949 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5950 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5956 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5957 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5967 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5979 static struct block
*
5980 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5983 struct block
*b0
, *b1
, *tmp
;
5986 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5992 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5997 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5998 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6000 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6011 static struct block
*
6012 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
6015 struct block
*b1
, *b2
;
6019 * Reverse the order of the ports, so v1 is the lower one.
6028 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6029 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6036 static struct block
*
6037 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6038 bpf_u_int32 proto
, int dir
)
6040 struct block
*b0
, *b1
, *tmp
;
6042 /* ip6 proto 'proto' */
6043 /* XXX - catch the first fragment of a fragmented packet? */
6044 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6048 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6052 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6056 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6057 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6063 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6064 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6076 static struct block
*
6077 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6080 struct block
*b0
, *b1
, *tmp
;
6082 /* link proto ip6 */
6083 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6089 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6094 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6095 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6097 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6109 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6118 v
= pcap_nametoproto(name
);
6119 if (v
== PROTO_UNDEF
)
6120 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6124 /* XXX should look up h/w protocol type based on cstate->linktype */
6125 v
= pcap_nametoeproto(name
);
6126 if (v
== PROTO_UNDEF
) {
6127 v
= pcap_nametollc(name
);
6128 if (v
== PROTO_UNDEF
)
6129 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6134 if (strcmp(name
, "esis") == 0)
6136 else if (strcmp(name
, "isis") == 0)
6138 else if (strcmp(name
, "clnp") == 0)
6141 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6151 #if !defined(NO_PROTOCHAIN)
6152 static struct block
*
6153 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6155 struct block
*b0
, *b
;
6156 struct slist
*s
[100];
6157 int fix2
, fix3
, fix4
, fix5
;
6158 int ahcheck
, again
, end
;
6160 int reg2
= alloc_reg(cstate
);
6162 memset(s
, 0, sizeof(s
));
6163 fix3
= fix4
= fix5
= 0;
6170 b0
= gen_protochain(cstate
, v
, Q_IP
);
6171 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6175 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
6180 * We don't handle variable-length prefixes before the link-layer
6181 * header, or variable-length link-layer headers, here yet.
6182 * We might want to add BPF instructions to do the protochain
6183 * work, to simplify that and, on platforms that have a BPF
6184 * interpreter with the new instructions, let the filtering
6185 * be done in the kernel. (We already require a modified BPF
6186 * engine to do the protochain stuff, to support backward
6187 * branches, and backward branch support is unlikely to appear
6188 * in kernel BPF engines.)
6190 if (cstate
->off_linkpl
.is_variable
)
6191 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6194 * To quote a comment in optimize.c:
6196 * "These data structures are used in a Cocke and Schwartz style
6197 * value numbering scheme. Since the flowgraph is acyclic,
6198 * exit values can be propagated from a node's predecessors
6199 * provided it is uniquely defined."
6201 * "Acyclic" means "no backward branches", which means "no
6202 * loops", so we have to turn the optimizer off.
6204 cstate
->no_optimize
= 1;
6207 * s[0] is a dummy entry to protect other BPF insn from damage
6208 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6209 * hard to find interdependency made by jump table fixup.
6212 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6217 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6220 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6221 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6223 /* X = ip->ip_hl << 2 */
6224 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6225 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6230 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6232 /* A = ip6->ip_nxt */
6233 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6234 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6236 /* X = sizeof(struct ip6_hdr) */
6237 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6243 bpf_error(cstate
, "unsupported proto to gen_protochain");
6247 /* again: if (A == v) goto end; else fall through; */
6249 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6251 s
[i
]->s
.jt
= NULL
; /*later*/
6252 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6256 #ifndef IPPROTO_NONE
6257 #define IPPROTO_NONE 59
6259 /* if (A == IPPROTO_NONE) goto end */
6260 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6261 s
[i
]->s
.jt
= NULL
; /*later*/
6262 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6263 s
[i
]->s
.k
= IPPROTO_NONE
;
6264 s
[fix5
]->s
.jf
= s
[i
];
6268 if (proto
== Q_IPV6
) {
6269 int v6start
, v6end
, v6advance
, j
;
6272 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6273 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6274 s
[i
]->s
.jt
= NULL
; /*later*/
6275 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6276 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6277 s
[fix2
]->s
.jf
= s
[i
];
6279 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6280 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6281 s
[i
]->s
.jt
= NULL
; /*later*/
6282 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6283 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6285 /* if (A == IPPROTO_ROUTING) goto v6advance */
6286 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6287 s
[i
]->s
.jt
= NULL
; /*later*/
6288 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6289 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6291 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6292 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6293 s
[i
]->s
.jt
= NULL
; /*later*/
6294 s
[i
]->s
.jf
= NULL
; /*later*/
6295 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6305 * A = P[X + packet head];
6306 * X = X + (P[X + packet head + 1] + 1) * 8;
6308 /* A = P[X + packet head] */
6309 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6310 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6313 s
[i
] = new_stmt(cstate
, BPF_ST
);
6316 /* A = P[X + packet head + 1]; */
6317 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6318 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6321 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6325 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6329 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6333 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6336 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6340 /* goto again; (must use BPF_JA for backward jump) */
6341 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6342 s
[i
]->s
.k
= again
- i
- 1;
6343 s
[i
- 1]->s
.jf
= s
[i
];
6347 for (j
= v6start
; j
<= v6end
; j
++)
6348 s
[j
]->s
.jt
= s
[v6advance
];
6351 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6353 s
[fix2
]->s
.jf
= s
[i
];
6359 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6360 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6361 s
[i
]->s
.jt
= NULL
; /*later*/
6362 s
[i
]->s
.jf
= NULL
; /*later*/
6363 s
[i
]->s
.k
= IPPROTO_AH
;
6365 s
[fix3
]->s
.jf
= s
[ahcheck
];
6372 * X = X + (P[X + 1] + 2) * 4;
6375 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6377 /* A = P[X + packet head]; */
6378 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6379 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6382 s
[i
] = new_stmt(cstate
, BPF_ST
);
6386 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6389 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6393 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6395 /* A = P[X + packet head] */
6396 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6397 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6400 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6404 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6408 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6411 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6415 /* goto again; (must use BPF_JA for backward jump) */
6416 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6417 s
[i
]->s
.k
= again
- i
- 1;
6422 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6424 s
[fix2
]->s
.jt
= s
[end
];
6425 s
[fix4
]->s
.jf
= s
[end
];
6426 s
[fix5
]->s
.jt
= s
[end
];
6433 for (i
= 0; i
< max
- 1; i
++)
6434 s
[i
]->next
= s
[i
+ 1];
6435 s
[max
- 1]->next
= NULL
;
6440 b
= new_block(cstate
, JMP(BPF_JEQ
));
6441 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6444 free_reg(cstate
, reg2
);
6449 #endif /* !defined(NO_PROTOCHAIN) */
6452 * Generate code that checks whether the packet is a packet for protocol
6453 * <proto> and whether the type field in that protocol's header has
6454 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6455 * IP packet and checks the protocol number in the IP header against <v>.
6457 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6458 * against Q_IP and Q_IPV6.
6460 static struct block
*
6461 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6463 struct block
*b0
, *b1
;
6466 if (dir
!= Q_DEFAULT
)
6467 bpf_error(cstate
, "direction applied to 'proto'");
6471 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6472 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6477 return gen_linktype(cstate
, v
);
6481 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6482 * not LLC encapsulation with LLCSAP_IP.
6484 * For IEEE 802 networks - which includes 802.5 token ring
6485 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6486 * says that SNAP encapsulation is used, not LLC encapsulation
6489 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6490 * RFC 2225 say that SNAP encapsulation is used, not LLC
6491 * encapsulation with LLCSAP_IP.
6493 * So we always check for ETHERTYPE_IP.
6495 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6496 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6514 break; // invalid qualifier
6517 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6519 * Also check for a fragment header before the final
6522 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6523 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6525 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6536 break; // invalid qualifier
6539 switch (cstate
->linktype
) {
6543 * Frame Relay packets typically have an OSI
6544 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6545 * generates code to check for all the OSI
6546 * NLPIDs, so calling it and then adding a check
6547 * for the particular NLPID for which we're
6548 * looking is bogus, as we can just check for
6551 * What we check for is the NLPID and a frame
6552 * control field value of UI, i.e. 0x03 followed
6555 * XXX - assumes a 2-byte Frame Relay header with
6556 * DLCI and flags. What if the address is longer?
6558 * XXX - what about SNAP-encapsulated frames?
6560 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6566 * Cisco uses an Ethertype lookalike - for OSI,
6569 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6570 /* OSI in C-HDLC is stuffed with a fudge byte */
6571 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6576 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6577 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6583 break; // invalid qualifier
6586 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6588 * 4 is the offset of the PDU type relative to the IS-IS
6591 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6608 break; // invalid qualifier
6614 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6619 * Convert a non-numeric name to a port number.
6622 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6624 struct addrinfo hints
, *res
, *ai
;
6626 struct sockaddr_in
*in4
;
6628 struct sockaddr_in6
*in6
;
6633 * We check for both TCP and UDP in case there are
6634 * ambiguous entries.
6636 memset(&hints
, 0, sizeof(hints
));
6637 hints
.ai_family
= PF_UNSPEC
;
6638 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6639 hints
.ai_protocol
= ipproto
;
6640 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6647 * No such port. Just return -1.
6654 * We don't use strerror() because it's not
6655 * guaranteed to be thread-safe on all platforms
6656 * (probably because it might use a non-thread-local
6657 * buffer into which to format an error message
6658 * if the error code isn't one for which it has
6659 * a canned string; three cheers for C string
6662 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6664 port
= -2; /* a real error */
6670 * This is a real error, not just "there's
6671 * no such service name".
6673 * We don't use gai_strerror() because it's not
6674 * guaranteed to be thread-safe on all platforms
6675 * (probably because it might use a non-thread-local
6676 * buffer into which to format an error message
6677 * if the error code isn't one for which it has
6678 * a canned string; three cheers for C string
6681 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6683 port
= -2; /* a real error */
6688 * OK, we found it. Did it find anything?
6690 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6692 * Does it have an address?
6694 if (ai
->ai_addr
!= NULL
) {
6696 * Yes. Get a port number; we're done.
6698 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6699 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6700 port
= ntohs(in4
->sin_port
);
6704 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6705 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6706 port
= ntohs(in6
->sin6_port
);
6718 * Convert a string to a port number.
6721 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6731 * See if it's a number.
6733 ret
= stoulen(string
, string_size
, &val
, cstate
);
6737 /* Unknown port type - it's just a number. */
6738 *proto
= PROTO_UNDEF
;
6741 case STOULEN_NOT_OCTAL_NUMBER
:
6742 case STOULEN_NOT_HEX_NUMBER
:
6743 case STOULEN_NOT_DECIMAL_NUMBER
:
6745 * Not a valid number; try looking it up as a port.
6747 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6748 memcpy(cpy
, string
, string_size
);
6749 cpy
[string_size
] = '\0';
6750 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6751 if (tcp_port
== -2) {
6753 * We got a hard error; the error string has
6757 longjmp(cstate
->top_ctx
, 1);
6760 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6761 if (udp_port
== -2) {
6763 * We got a hard error; the error string has
6767 longjmp(cstate
->top_ctx
, 1);
6772 * We need to check /etc/services for ambiguous entries.
6773 * If we find an ambiguous entry, and it has the
6774 * same port number, change the proto to PROTO_UNDEF
6775 * so both TCP and UDP will be checked.
6777 if (tcp_port
>= 0) {
6778 val
= (bpf_u_int32
)tcp_port
;
6779 *proto
= IPPROTO_TCP
;
6780 if (udp_port
>= 0) {
6781 if (udp_port
== tcp_port
)
6782 *proto
= PROTO_UNDEF
;
6785 /* Can't handle ambiguous names that refer
6786 to different port numbers. */
6787 warning("ambiguous port %s in /etc/services",
6794 if (udp_port
>= 0) {
6795 val
= (bpf_u_int32
)udp_port
;
6796 *proto
= IPPROTO_UDP
;
6800 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6802 longjmp(cstate
->top_ctx
, 1);
6809 /* Error already set. */
6810 longjmp(cstate
->top_ctx
, 1);
6817 /* Should not happen */
6818 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6819 longjmp(cstate
->top_ctx
, 1);
6826 * Convert a string in the form PPP-PPP, which correspond to ports, to
6827 * a starting and ending port in a port range.
6830 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6831 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6834 const char *first
, *second
;
6835 size_t first_size
, second_size
;
6838 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6839 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6842 * Make sure there are no other hyphens.
6844 * XXX - we support named ports, but there are some port names
6845 * in /etc/services that include hyphens, so this would rule
6848 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6849 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6853 * Get the length of the first port.
6856 first_size
= hyphen_off
- string
;
6857 if (first_size
== 0) {
6858 /* Range of "-port", which we don't support. */
6859 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6863 * Try to convert it to a port.
6865 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6866 save_proto
= *proto
;
6869 * Get the length of the second port.
6871 second
= hyphen_off
+ 1;
6872 second_size
= strlen(second
);
6873 if (second_size
== 0) {
6874 /* Range of "port-", which we don't support. */
6875 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6879 * Try to convert it to a port.
6881 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6882 if (*proto
!= save_proto
)
6883 *proto
= PROTO_UNDEF
;
6887 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6889 int proto
= q
.proto
;
6893 bpf_u_int32 mask
, addr
;
6894 struct addrinfo
*res
, *res0
;
6895 struct sockaddr_in
*sin4
;
6898 struct sockaddr_in6
*sin6
;
6899 struct in6_addr mask128
;
6901 struct block
*b
, *tmp
;
6902 int port
, real_proto
;
6903 bpf_u_int32 port1
, port2
;
6906 * Catch errors reported by us and routines below us, and return NULL
6909 if (setjmp(cstate
->top_ctx
))
6915 addr
= pcap_nametonetaddr(name
);
6917 bpf_error(cstate
, "unknown network '%s'", name
);
6918 /* Left justify network addr and calculate its network mask */
6920 while (addr
&& (addr
& 0xff000000) == 0) {
6924 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6928 if (proto
== Q_LINK
) {
6929 switch (cstate
->linktype
) {
6932 case DLT_NETANALYZER
:
6933 case DLT_NETANALYZER_TRANSPARENT
:
6934 eaddr
= pcap_ether_hostton(name
);
6937 "unknown ether host '%s'", name
);
6938 tmp
= gen_prevlinkhdr_check(cstate
);
6939 b
= gen_ehostop(cstate
, eaddr
, dir
);
6946 eaddr
= pcap_ether_hostton(name
);
6949 "unknown FDDI host '%s'", name
);
6950 b
= gen_fhostop(cstate
, eaddr
, dir
);
6955 eaddr
= pcap_ether_hostton(name
);
6958 "unknown token ring host '%s'", name
);
6959 b
= gen_thostop(cstate
, eaddr
, dir
);
6963 case DLT_IEEE802_11
:
6964 case DLT_PRISM_HEADER
:
6965 case DLT_IEEE802_11_RADIO_AVS
:
6966 case DLT_IEEE802_11_RADIO
:
6968 eaddr
= pcap_ether_hostton(name
);
6971 "unknown 802.11 host '%s'", name
);
6972 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6976 case DLT_IP_OVER_FC
:
6977 eaddr
= pcap_ether_hostton(name
);
6980 "unknown Fibre Channel host '%s'", name
);
6981 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6986 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6987 } else if (proto
== Q_DECNET
) {
6989 * A long time ago on Ultrix libpcap supported
6990 * translation of DECnet host names into DECnet
6991 * addresses, but this feature is history now.
6993 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6996 memset(&mask128
, 0xff, sizeof(mask128
));
6998 res0
= res
= pcap_nametoaddrinfo(name
);
7000 bpf_error(cstate
, "unknown host '%s'", name
);
7007 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
7008 tproto
== Q_DEFAULT
) {
7014 for (res
= res0
; res
; res
= res
->ai_next
) {
7015 switch (res
->ai_family
) {
7018 if (tproto
== Q_IPV6
)
7022 sin4
= (struct sockaddr_in
*)
7024 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
7025 0xffffffff, tproto
, dir
, q
.addr
);
7029 if (tproto6
== Q_IP
)
7032 sin6
= (struct sockaddr_in6
*)
7034 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7035 &mask128
, tproto6
, dir
, q
.addr
);
7048 bpf_error(cstate
, "unknown host '%s'%s", name
,
7049 (proto
== Q_DEFAULT
)
7051 : " for specified address family");
7057 (void)port_pq_to_ipproto(cstate
, proto
, "port"); // validate only
7058 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7059 bpf_error(cstate
, "unknown port '%s'", name
);
7060 if (proto
== Q_UDP
) {
7061 if (real_proto
== IPPROTO_TCP
)
7062 bpf_error(cstate
, "port '%s' is tcp", name
);
7063 else if (real_proto
== IPPROTO_SCTP
)
7064 bpf_error(cstate
, "port '%s' is sctp", name
);
7066 /* override PROTO_UNDEF */
7067 real_proto
= IPPROTO_UDP
;
7069 if (proto
== Q_TCP
) {
7070 if (real_proto
== IPPROTO_UDP
)
7071 bpf_error(cstate
, "port '%s' is udp", name
);
7073 else if (real_proto
== IPPROTO_SCTP
)
7074 bpf_error(cstate
, "port '%s' is sctp", name
);
7076 /* override PROTO_UNDEF */
7077 real_proto
= IPPROTO_TCP
;
7079 if (proto
== Q_SCTP
) {
7080 if (real_proto
== IPPROTO_UDP
)
7081 bpf_error(cstate
, "port '%s' is udp", name
);
7083 else if (real_proto
== IPPROTO_TCP
)
7084 bpf_error(cstate
, "port '%s' is tcp", name
);
7086 /* override PROTO_UNDEF */
7087 real_proto
= IPPROTO_SCTP
;
7090 bpf_error(cstate
, "illegal port number %d < 0", port
);
7092 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7093 b
= gen_port(cstate
, port
, real_proto
, dir
);
7094 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7098 (void)port_pq_to_ipproto(cstate
, proto
, "portrange"); // validate only
7099 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7100 if (proto
== Q_UDP
) {
7101 if (real_proto
== IPPROTO_TCP
)
7102 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7103 else if (real_proto
== IPPROTO_SCTP
)
7104 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7106 /* override PROTO_UNDEF */
7107 real_proto
= IPPROTO_UDP
;
7109 if (proto
== Q_TCP
) {
7110 if (real_proto
== IPPROTO_UDP
)
7111 bpf_error(cstate
, "port in range '%s' is udp", name
);
7112 else if (real_proto
== IPPROTO_SCTP
)
7113 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7115 /* override PROTO_UNDEF */
7116 real_proto
= IPPROTO_TCP
;
7118 if (proto
== Q_SCTP
) {
7119 if (real_proto
== IPPROTO_UDP
)
7120 bpf_error(cstate
, "port in range '%s' is udp", name
);
7121 else if (real_proto
== IPPROTO_TCP
)
7122 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7124 /* override PROTO_UNDEF */
7125 real_proto
= IPPROTO_SCTP
;
7128 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7130 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7132 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7133 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7138 eaddr
= pcap_ether_hostton(name
);
7140 bpf_error(cstate
, "unknown ether host: %s", name
);
7142 res
= pcap_nametoaddrinfo(name
);
7145 bpf_error(cstate
, "unknown host '%s'", name
);
7146 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7151 bpf_error(cstate
, "unknown host '%s'", name
);
7154 bpf_error(cstate
, "'gateway' not supported in this configuration");
7158 real_proto
= lookup_proto(cstate
, name
, proto
);
7159 if (real_proto
>= 0)
7160 return gen_proto(cstate
, real_proto
, proto
, dir
);
7162 bpf_error(cstate
, "unknown protocol: %s", name
);
7164 #if !defined(NO_PROTOCHAIN)
7166 real_proto
= lookup_proto(cstate
, name
, proto
);
7167 if (real_proto
>= 0)
7168 return gen_protochain(cstate
, real_proto
, proto
);
7170 bpf_error(cstate
, "unknown protocol: %s", name
);
7171 #endif /* !defined(NO_PROTOCHAIN) */
7182 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7183 bpf_u_int32 masklen
, struct qual q
)
7185 register int nlen
, mlen
;
7190 * Catch errors reported by us and routines below us, and return NULL
7193 if (setjmp(cstate
->top_ctx
))
7196 nlen
= pcapint_atoin(s1
, &n
);
7198 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7199 /* Promote short ipaddr */
7203 mlen
= pcapint_atoin(s2
, &m
);
7205 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7206 /* Promote short ipaddr */
7209 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7212 /* Convert mask len to mask */
7214 bpf_error(cstate
, "mask length must be <= 32");
7215 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7216 m
= (bpf_u_int32
)m64
;
7218 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7225 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7228 // Q_HOST and Q_GATEWAY only (see the grammar)
7229 bpf_error(cstate
, "Mask syntax for networks only");
7236 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7244 * Catch errors reported by us and routines below us, and return NULL
7247 if (setjmp(cstate
->top_ctx
))
7254 * v contains a 32-bit unsigned parsed from a string of the
7255 * form {N}, which could be decimal, hexadecimal or octal.
7256 * Although it would be possible to use the value as a raw
7257 * 16-bit DECnet address when the value fits into 16 bits, this
7258 * would be a questionable feature: DECnet address wire
7259 * encoding is little-endian, so this would not work as
7260 * intuitively as the same works for [big-endian] IPv4
7261 * addresses (0x01020304 means 1.2.3.4).
7263 if (proto
== Q_DECNET
)
7264 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7266 } else if (proto
== Q_DECNET
) {
7268 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7269 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7270 * for a valid DECnet address.
7272 vlen
= pcapint_atodn(s
, &v
);
7274 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7277 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7278 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7281 vlen
= pcapint_atoin(s
, &v
);
7283 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7291 if (proto
== Q_DECNET
)
7292 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7293 else if (proto
== Q_LINK
) {
7294 // "link (host|net) IPV4ADDR" and variations thereof
7295 bpf_error(cstate
, "illegal link layer address");
7298 if (s
== NULL
&& q
.addr
== Q_NET
) {
7299 /* Promote short net number */
7300 while (v
&& (v
& 0xff000000) == 0) {
7305 /* Promote short ipaddr */
7307 mask
<<= 32 - vlen
;
7309 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7313 proto
= port_pq_to_ipproto(cstate
, proto
, "port");
7316 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7320 b
= gen_port(cstate
, v
, proto
, dir
);
7321 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7326 proto
= port_pq_to_ipproto(cstate
, proto
, "portrange");
7329 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7333 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7334 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7339 bpf_error(cstate
, "'gateway' requires a name");
7343 return gen_proto(cstate
, v
, proto
, dir
);
7345 #if !defined(NO_PROTOCHAIN)
7347 return gen_protochain(cstate
, v
, proto
);
7363 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7366 struct addrinfo
*res
;
7367 struct in6_addr
*addr
;
7368 struct in6_addr mask
;
7370 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7373 * Catch errors reported by us and routines below us, and return NULL
7376 if (setjmp(cstate
->top_ctx
))
7379 res
= pcap_nametoaddrinfo(s
);
7381 bpf_error(cstate
, "invalid ip6 address %s", s
);
7384 bpf_error(cstate
, "%s resolved to multiple address", s
);
7385 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7387 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7388 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7389 memset(&mask
, 0, sizeof(mask
));
7390 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7392 mask
.s6_addr
[masklen
/ 8] =
7393 (0xff << (8 - masklen
% 8)) & 0xff;
7396 memcpy(a
, addr
, sizeof(a
));
7397 memcpy(m
, &mask
, sizeof(m
));
7398 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7399 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7400 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7408 bpf_error(cstate
, "Mask syntax for networks only");
7412 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7418 // Q_GATEWAY only (see the grammar)
7419 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7426 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7428 struct block
*b
, *tmp
;
7431 * Catch errors reported by us and routines below us, and return NULL
7434 if (setjmp(cstate
->top_ctx
))
7437 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7438 cstate
->e
= pcap_ether_aton(s
);
7439 if (cstate
->e
== NULL
)
7440 bpf_error(cstate
, "malloc");
7441 switch (cstate
->linktype
) {
7443 case DLT_NETANALYZER
:
7444 case DLT_NETANALYZER_TRANSPARENT
:
7445 tmp
= gen_prevlinkhdr_check(cstate
);
7446 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7451 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7454 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7456 case DLT_IEEE802_11
:
7457 case DLT_PRISM_HEADER
:
7458 case DLT_IEEE802_11_RADIO_AVS
:
7459 case DLT_IEEE802_11_RADIO
:
7461 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7463 case DLT_IP_OVER_FC
:
7464 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7469 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7476 bpf_error(cstate
, "ethernet address used in non-ether expression");
7481 sappend(struct slist
*s0
, struct slist
*s1
)
7484 * This is definitely not the best way to do this, but the
7485 * lists will rarely get long.
7492 static struct slist
*
7493 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7497 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7502 static struct slist
*
7503 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7507 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7513 * Modify "index" to use the value stored into its register as an
7514 * offset relative to the beginning of the header for the protocol
7515 * "proto", and allocate a register and put an item "size" bytes long
7516 * (1, 2, or 4) at that offset into that register, making it the register
7519 static struct arth
*
7520 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7524 struct slist
*s
, *tmp
;
7526 int regno
= alloc_reg(cstate
);
7528 free_reg(cstate
, inst
->regno
);
7532 bpf_error(cstate
, "data size must be 1, 2, or 4");
7549 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7553 * The offset is relative to the beginning of the packet
7554 * data, if we have a radio header. (If we don't, this
7557 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7558 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7559 cstate
->linktype
!= DLT_PRISM_HEADER
)
7560 bpf_error(cstate
, "radio information not present in capture");
7563 * Load into the X register the offset computed into the
7564 * register specified by "index".
7566 s
= xfer_to_x(cstate
, inst
);
7569 * Load the item at that offset.
7571 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7573 sappend(inst
->s
, s
);
7578 * The offset is relative to the beginning of
7579 * the link-layer header.
7581 * XXX - what about ATM LANE? Should the index be
7582 * relative to the beginning of the AAL5 frame, so
7583 * that 0 refers to the beginning of the LE Control
7584 * field, or relative to the beginning of the LAN
7585 * frame, so that 0 refers, for Ethernet LANE, to
7586 * the beginning of the destination address?
7588 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7591 * If "s" is non-null, it has code to arrange that the
7592 * X register contains the length of the prefix preceding
7593 * the link-layer header. Add to it the offset computed
7594 * into the register specified by "index", and move that
7595 * into the X register. Otherwise, just load into the X
7596 * register the offset computed into the register specified
7600 sappend(s
, xfer_to_a(cstate
, inst
));
7601 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7602 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7604 s
= xfer_to_x(cstate
, inst
);
7607 * Load the item at the sum of the offset we've put in the
7608 * X register and the offset of the start of the link
7609 * layer header (which is 0 if the radio header is
7610 * variable-length; that header length is what we put
7611 * into the X register and then added to the index).
7613 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7614 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7616 sappend(inst
->s
, s
);
7630 * The offset is relative to the beginning of
7631 * the network-layer header.
7632 * XXX - are there any cases where we want
7633 * cstate->off_nl_nosnap?
7635 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7638 * If "s" is non-null, it has code to arrange that the
7639 * X register contains the variable part of the offset
7640 * of the link-layer payload. Add to it the offset
7641 * computed into the register specified by "index",
7642 * and move that into the X register. Otherwise, just
7643 * load into the X register the offset computed into
7644 * the register specified by "index".
7647 sappend(s
, xfer_to_a(cstate
, inst
));
7648 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7649 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7651 s
= xfer_to_x(cstate
, inst
);
7654 * Load the item at the sum of the offset we've put in the
7655 * X register, the offset of the start of the network
7656 * layer header from the beginning of the link-layer
7657 * payload, and the constant part of the offset of the
7658 * start of the link-layer payload.
7660 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7661 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7663 sappend(inst
->s
, s
);
7666 * Do the computation only if the packet contains
7667 * the protocol in question.
7669 b
= gen_proto_abbrev_internal(cstate
, proto
);
7671 gen_and(inst
->b
, b
);
7685 * The offset is relative to the beginning of
7686 * the transport-layer header.
7688 * Load the X register with the length of the IPv4 header
7689 * (plus the offset of the link-layer header, if it's
7690 * a variable-length header), in bytes.
7692 * XXX - are there any cases where we want
7693 * cstate->off_nl_nosnap?
7694 * XXX - we should, if we're built with
7695 * IPv6 support, generate code to load either
7696 * IPv4, IPv6, or both, as appropriate.
7698 s
= gen_loadx_iphdrlen(cstate
);
7701 * The X register now contains the sum of the variable
7702 * part of the offset of the link-layer payload and the
7703 * length of the network-layer header.
7705 * Load into the A register the offset relative to
7706 * the beginning of the transport layer header,
7707 * add the X register to that, move that to the
7708 * X register, and load with an offset from the
7709 * X register equal to the sum of the constant part of
7710 * the offset of the link-layer payload and the offset,
7711 * relative to the beginning of the link-layer payload,
7712 * of the network-layer header.
7714 sappend(s
, xfer_to_a(cstate
, inst
));
7715 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7716 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7717 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7718 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7719 sappend(inst
->s
, s
);
7722 * Do the computation only if the packet contains
7723 * the protocol in question - which is true only
7724 * if this is an IP datagram and is the first or
7725 * only fragment of that datagram.
7727 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7729 gen_and(inst
->b
, b
);
7730 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7735 * Do the computation only if the packet contains
7736 * the protocol in question.
7738 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7740 gen_and(inst
->b
, b
);
7744 * Check if we have an icmp6 next header
7746 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7748 gen_and(inst
->b
, b
);
7751 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7753 * If "s" is non-null, it has code to arrange that the
7754 * X register contains the variable part of the offset
7755 * of the link-layer payload. Add to it the offset
7756 * computed into the register specified by "index",
7757 * and move that into the X register. Otherwise, just
7758 * load into the X register the offset computed into
7759 * the register specified by "index".
7762 sappend(s
, xfer_to_a(cstate
, inst
));
7763 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7764 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7766 s
= xfer_to_x(cstate
, inst
);
7769 * Load the item at the sum of the offset we've put in the
7770 * X register, the offset of the start of the network
7771 * layer header from the beginning of the link-layer
7772 * payload, and the constant part of the offset of the
7773 * start of the link-layer payload.
7775 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7776 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7779 sappend(inst
->s
, s
);
7783 inst
->regno
= regno
;
7784 s
= new_stmt(cstate
, BPF_ST
);
7786 sappend(inst
->s
, s
);
7792 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7796 * Catch errors reported by us and routines below us, and return NULL
7799 if (setjmp(cstate
->top_ctx
))
7802 return gen_load_internal(cstate
, proto
, inst
, size
);
7805 static struct block
*
7806 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7807 struct arth
*a1
, int reversed
)
7809 struct slist
*s0
, *s1
, *s2
;
7810 struct block
*b
, *tmp
;
7812 s0
= xfer_to_x(cstate
, a1
);
7813 s1
= xfer_to_a(cstate
, a0
);
7814 if (code
== BPF_JEQ
) {
7815 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7816 b
= new_block(cstate
, JMP(code
));
7820 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7826 sappend(a0
->s
, a1
->s
);
7830 free_reg(cstate
, a0
->regno
);
7831 free_reg(cstate
, a1
->regno
);
7833 /* 'and' together protocol checks */
7836 gen_and(a0
->b
, tmp
= a1
->b
);
7850 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7851 struct arth
*a1
, int reversed
)
7854 * Catch errors reported by us and routines below us, and return NULL
7857 if (setjmp(cstate
->top_ctx
))
7860 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7864 gen_loadlen(compiler_state_t
*cstate
)
7871 * Catch errors reported by us and routines below us, and return NULL
7874 if (setjmp(cstate
->top_ctx
))
7877 regno
= alloc_reg(cstate
);
7878 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7879 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7880 s
->next
= new_stmt(cstate
, BPF_ST
);
7881 s
->next
->s
.k
= regno
;
7888 static struct arth
*
7889 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7895 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7897 reg
= alloc_reg(cstate
);
7899 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7901 s
->next
= new_stmt(cstate
, BPF_ST
);
7910 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7913 * Catch errors reported by us and routines below us, and return NULL
7916 if (setjmp(cstate
->top_ctx
))
7919 return gen_loadi_internal(cstate
, val
);
7923 * The a_arg dance is to avoid annoying whining by compilers that
7924 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7925 * It's not *used* after setjmp returns.
7928 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7930 struct arth
*a
= a_arg
;
7934 * Catch errors reported by us and routines below us, and return NULL
7937 if (setjmp(cstate
->top_ctx
))
7940 s
= xfer_to_a(cstate
, a
);
7942 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7945 s
= new_stmt(cstate
, BPF_ST
);
7953 * The a0_arg dance is to avoid annoying whining by compilers that
7954 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7955 * It's not *used* after setjmp returns.
7958 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7961 struct arth
*a0
= a0_arg
;
7962 struct slist
*s0
, *s1
, *s2
;
7965 * Catch errors reported by us and routines below us, and return NULL
7968 if (setjmp(cstate
->top_ctx
))
7972 * Disallow division by, or modulus by, zero; we do this here
7973 * so that it gets done even if the optimizer is disabled.
7975 * Also disallow shifts by a value greater than 31; we do this
7976 * here, for the same reason.
7978 if (code
== BPF_DIV
) {
7979 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7980 bpf_error(cstate
, "division by zero");
7981 } else if (code
== BPF_MOD
) {
7982 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7983 bpf_error(cstate
, "modulus by zero");
7984 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7985 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7986 bpf_error(cstate
, "shift by more than 31 bits");
7988 s0
= xfer_to_x(cstate
, a1
);
7989 s1
= xfer_to_a(cstate
, a0
);
7990 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7995 sappend(a0
->s
, a1
->s
);
7997 free_reg(cstate
, a0
->regno
);
7998 free_reg(cstate
, a1
->regno
);
8000 s0
= new_stmt(cstate
, BPF_ST
);
8001 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8008 * Initialize the table of used registers and the current register.
8011 init_regs(compiler_state_t
*cstate
)
8014 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8018 * Return the next free register.
8021 alloc_reg(compiler_state_t
*cstate
)
8023 int n
= BPF_MEMWORDS
;
8026 if (cstate
->regused
[cstate
->curreg
])
8027 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8029 cstate
->regused
[cstate
->curreg
] = 1;
8030 return cstate
->curreg
;
8033 bpf_error(cstate
, "too many registers needed to evaluate expression");
8038 * Return a register to the table so it can
8042 free_reg(compiler_state_t
*cstate
, int n
)
8044 cstate
->regused
[n
] = 0;
8047 static struct block
*
8048 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8053 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8054 b
= new_block(cstate
, JMP(jmp
));
8062 gen_greater(compiler_state_t
*cstate
, int n
)
8065 * Catch errors reported by us and routines below us, and return NULL
8068 if (setjmp(cstate
->top_ctx
))
8071 return gen_len(cstate
, BPF_JGE
, n
);
8075 * Actually, this is less than or equal.
8078 gen_less(compiler_state_t
*cstate
, int n
)
8083 * Catch errors reported by us and routines below us, and return NULL
8086 if (setjmp(cstate
->top_ctx
))
8089 b
= gen_len(cstate
, BPF_JGT
, n
);
8096 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8097 * the beginning of the link-layer header.
8098 * XXX - that means you can't test values in the radiotap header, but
8099 * as that header is difficult if not impossible to parse generally
8100 * without a loop, that might not be a severe problem. A new keyword
8101 * "radio" could be added for that, although what you'd really want
8102 * would be a way of testing particular radio header values, which
8103 * would generate code appropriate to the radio header in question.
8106 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8112 * Catch errors reported by us and routines below us, and return NULL
8115 if (setjmp(cstate
->top_ctx
))
8123 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8126 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8130 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8134 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8138 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8142 b
= new_block(cstate
, JMP(BPF_JEQ
));
8150 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8152 bpf_u_int32 hostmask
;
8153 struct block
*b0
, *b1
, *b2
;
8154 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8157 * Catch errors reported by us and routines below us, and return NULL
8160 if (setjmp(cstate
->top_ctx
))
8167 switch (cstate
->linktype
) {
8169 case DLT_ARCNET_LINUX
:
8170 // ARCnet broadcast is [8-bit] destination address 0.
8171 return gen_ahostop(cstate
, 0, Q_DST
);
8173 case DLT_NETANALYZER
:
8174 case DLT_NETANALYZER_TRANSPARENT
:
8175 b1
= gen_prevlinkhdr_check(cstate
);
8176 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8181 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8183 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8184 case DLT_IEEE802_11
:
8185 case DLT_PRISM_HEADER
:
8186 case DLT_IEEE802_11_RADIO_AVS
:
8187 case DLT_IEEE802_11_RADIO
:
8189 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8190 case DLT_IP_OVER_FC
:
8191 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8193 fail_kw_on_dlt(cstate
, "broadcast");
8198 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8199 * as an indication that we don't know the netmask, and fail
8202 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8203 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8204 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8205 hostmask
= ~cstate
->netmask
;
8206 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8207 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8212 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
8217 * Generate code to test the low-order bit of a MAC address (that's
8218 * the bottom bit of the *first* byte).
8220 static struct block
*
8221 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8223 register struct block
*b0
;
8224 register struct slist
*s
;
8226 /* link[offset] & 1 != 0 */
8227 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8228 b0
= new_block(cstate
, JMP(BPF_JSET
));
8235 gen_multicast(compiler_state_t
*cstate
, int proto
)
8237 register struct block
*b0
, *b1
, *b2
;
8238 register struct slist
*s
;
8241 * Catch errors reported by us and routines below us, and return NULL
8244 if (setjmp(cstate
->top_ctx
))
8251 switch (cstate
->linktype
) {
8253 case DLT_ARCNET_LINUX
:
8254 // ARCnet multicast is the same as broadcast.
8255 return gen_ahostop(cstate
, 0, Q_DST
);
8257 case DLT_NETANALYZER
:
8258 case DLT_NETANALYZER_TRANSPARENT
:
8259 b1
= gen_prevlinkhdr_check(cstate
);
8260 /* ether[0] & 1 != 0 */
8261 b0
= gen_mac_multicast(cstate
, 0);
8267 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8269 * XXX - was that referring to bit-order issues?
8271 /* fddi[1] & 1 != 0 */
8272 return gen_mac_multicast(cstate
, 1);
8274 /* tr[2] & 1 != 0 */
8275 return gen_mac_multicast(cstate
, 2);
8276 case DLT_IEEE802_11
:
8277 case DLT_PRISM_HEADER
:
8278 case DLT_IEEE802_11_RADIO_AVS
:
8279 case DLT_IEEE802_11_RADIO
:
8284 * For control frames, there is no DA.
8286 * For management frames, DA is at an
8287 * offset of 4 from the beginning of
8290 * For data frames, DA is at an offset
8291 * of 4 from the beginning of the packet
8292 * if To DS is clear and at an offset of
8293 * 16 from the beginning of the packet
8298 * Generate the tests to be done for data frames.
8300 * First, check for To DS set, i.e. "link[1] & 0x01".
8302 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8303 b1
= new_block(cstate
, JMP(BPF_JSET
));
8304 b1
->s
.k
= 0x01; /* To DS */
8308 * If To DS is set, the DA is at 16.
8310 b0
= gen_mac_multicast(cstate
, 16);
8314 * Now, check for To DS not set, i.e. check
8315 * "!(link[1] & 0x01)".
8317 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8318 b2
= new_block(cstate
, JMP(BPF_JSET
));
8319 b2
->s
.k
= 0x01; /* To DS */
8324 * If To DS is not set, the DA is at 4.
8326 b1
= gen_mac_multicast(cstate
, 4);
8330 * Now OR together the last two checks. That gives
8331 * the complete set of checks for data frames.
8336 * Now check for a data frame.
8337 * I.e, check "link[0] & 0x08".
8339 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8340 b1
= new_block(cstate
, JMP(BPF_JSET
));
8345 * AND that with the checks done for data frames.
8350 * If the high-order bit of the type value is 0, this
8351 * is a management frame.
8352 * I.e, check "!(link[0] & 0x08)".
8354 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8355 b2
= new_block(cstate
, JMP(BPF_JSET
));
8361 * For management frames, the DA is at 4.
8363 b1
= gen_mac_multicast(cstate
, 4);
8367 * OR that with the checks done for data frames.
8368 * That gives the checks done for management and
8374 * If the low-order bit of the type value is 1,
8375 * this is either a control frame or a frame
8376 * with a reserved type, and thus not a
8379 * I.e., check "!(link[0] & 0x04)".
8381 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8382 b1
= new_block(cstate
, JMP(BPF_JSET
));
8388 * AND that with the checks for data and management
8393 case DLT_IP_OVER_FC
:
8394 b0
= gen_mac_multicast(cstate
, 2);
8399 fail_kw_on_dlt(cstate
, "multicast");
8403 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8404 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8409 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8410 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8414 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8420 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8421 * we can look at special meta-data in the filter expression; otherwise we
8422 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8423 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8424 * pcap_activate() conditionally sets.
8427 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8429 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8431 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8433 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8438 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8440 register struct block
*b0
;
8443 * Catch errors reported by us and routines below us, and return NULL
8446 if (setjmp(cstate
->top_ctx
))
8450 * Only some data link types support ifindex qualifiers.
8452 switch (cstate
->linktype
) {
8453 case DLT_LINUX_SLL2
:
8454 /* match packets on this interface */
8455 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8458 #if defined(__linux__)
8459 require_basic_bpf_extensions(cstate
, "ifindex");
8461 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8463 #else /* defined(__linux__) */
8464 fail_kw_on_dlt(cstate
, "ifindex");
8466 #endif /* defined(__linux__) */
8472 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8473 * Outbound traffic is sent by this machine, while inbound traffic is
8474 * sent by a remote machine (and may include packets destined for a
8475 * unicast or multicast link-layer address we are not subscribing to).
8476 * These are the same definitions implemented by pcap_setdirection().
8477 * Capturing only unicast traffic destined for this host is probably
8478 * better accomplished using a higher-layer filter.
8481 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8483 register struct block
*b0
;
8486 * Catch errors reported by us and routines below us, and return NULL
8489 if (setjmp(cstate
->top_ctx
))
8493 * Only some data link types support inbound/outbound qualifiers.
8495 switch (cstate
->linktype
) {
8497 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8498 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8502 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8503 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8507 /* match outgoing packets */
8508 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8510 /* to filter on inbound traffic, invert the match */
8515 case DLT_LINUX_SLL2
:
8516 /* match outgoing packets */
8517 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8519 /* to filter on inbound traffic, invert the match */
8525 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8526 outbound
? PF_OUT
: PF_IN
);
8530 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8533 case DLT_JUNIPER_MFR
:
8534 case DLT_JUNIPER_MLFR
:
8535 case DLT_JUNIPER_MLPPP
:
8536 case DLT_JUNIPER_ATM1
:
8537 case DLT_JUNIPER_ATM2
:
8538 case DLT_JUNIPER_PPPOE
:
8539 case DLT_JUNIPER_PPPOE_ATM
:
8540 case DLT_JUNIPER_GGSN
:
8541 case DLT_JUNIPER_ES
:
8542 case DLT_JUNIPER_MONITOR
:
8543 case DLT_JUNIPER_SERVICES
:
8544 case DLT_JUNIPER_ETHER
:
8545 case DLT_JUNIPER_PPP
:
8546 case DLT_JUNIPER_FRELAY
:
8547 case DLT_JUNIPER_CHDLC
:
8548 case DLT_JUNIPER_VP
:
8549 case DLT_JUNIPER_ST
:
8550 case DLT_JUNIPER_ISM
:
8551 case DLT_JUNIPER_VS
:
8552 case DLT_JUNIPER_SRX_E2E
:
8553 case DLT_JUNIPER_FIBRECHANNEL
:
8554 case DLT_JUNIPER_ATM_CEMIC
:
8555 /* juniper flags (including direction) are stored
8556 * the byte after the 3-byte magic number */
8557 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8562 * If we have packet meta-data indicating a direction,
8563 * and that metadata can be checked by BPF code, check
8564 * it. Otherwise, give up, as this link-layer type has
8565 * nothing in the packet data.
8567 * Currently, the only platform where a BPF filter can
8568 * check that metadata is Linux with the in-kernel
8569 * BPF interpreter. If other packet capture mechanisms
8570 * and BPF filters also supported this, it would be
8571 * nice. It would be even better if they made that
8572 * metadata available so that we could provide it
8573 * with newer capture APIs, allowing it to be saved
8576 #if defined(__linux__)
8577 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8578 /* match outgoing packets */
8579 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8582 /* to filter on inbound traffic, invert the match */
8585 #else /* defined(__linux__) */
8586 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8588 #endif /* defined(__linux__) */
8593 /* PF firewall log matched interface */
8595 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8601 * Catch errors reported by us and routines below us, and return NULL
8604 if (setjmp(cstate
->top_ctx
))
8607 assert_pflog(cstate
, "ifname");
8609 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8610 off
= offsetof(struct pfloghdr
, ifname
);
8611 if (strlen(ifname
) >= len
) {
8612 bpf_error(cstate
, "ifname interface names can only be %d characters",
8616 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8617 (const u_char
*)ifname
);
8621 /* PF firewall log ruleset name */
8623 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8628 * Catch errors reported by us and routines below us, and return NULL
8631 if (setjmp(cstate
->top_ctx
))
8634 assert_pflog(cstate
, "ruleset");
8636 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8637 bpf_error(cstate
, "ruleset names can only be %ld characters",
8638 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8642 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8643 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8647 /* PF firewall log rule number */
8649 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8654 * Catch errors reported by us and routines below us, and return NULL
8657 if (setjmp(cstate
->top_ctx
))
8660 assert_pflog(cstate
, "rnr");
8662 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8667 /* PF firewall log sub-rule number */
8669 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8674 * Catch errors reported by us and routines below us, and return NULL
8677 if (setjmp(cstate
->top_ctx
))
8680 assert_pflog(cstate
, "srnr");
8682 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8687 /* PF firewall log reason code */
8689 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8694 * Catch errors reported by us and routines below us, and return NULL
8697 if (setjmp(cstate
->top_ctx
))
8700 assert_pflog(cstate
, "reason");
8702 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8703 (bpf_u_int32
)reason
);
8707 /* PF firewall log action */
8709 gen_pf_action(compiler_state_t
*cstate
, int action
)
8714 * Catch errors reported by us and routines below us, and return NULL
8717 if (setjmp(cstate
->top_ctx
))
8720 assert_pflog(cstate
, "action");
8722 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8723 (bpf_u_int32
)action
);
8727 /* IEEE 802.11 wireless header */
8729 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8734 * Catch errors reported by us and routines below us, and return NULL
8737 if (setjmp(cstate
->top_ctx
))
8740 switch (cstate
->linktype
) {
8742 case DLT_IEEE802_11
:
8743 case DLT_PRISM_HEADER
:
8744 case DLT_IEEE802_11_RADIO_AVS
:
8745 case DLT_IEEE802_11_RADIO
:
8747 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8751 fail_kw_on_dlt(cstate
, "type/subtype");
8759 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8764 * Catch errors reported by us and routines below us, and return NULL
8767 if (setjmp(cstate
->top_ctx
))
8770 switch (cstate
->linktype
) {
8772 case DLT_IEEE802_11
:
8773 case DLT_PRISM_HEADER
:
8774 case DLT_IEEE802_11_RADIO_AVS
:
8775 case DLT_IEEE802_11_RADIO
:
8780 fail_kw_on_dlt(cstate
, "dir");
8784 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8785 IEEE80211_FC1_DIR_MASK
);
8790 // Process an ARCnet host address string.
8792 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8795 * Catch errors reported by us and routines below us, and return NULL
8798 if (setjmp(cstate
->top_ctx
))
8801 switch (cstate
->linktype
) {
8804 case DLT_ARCNET_LINUX
:
8805 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8806 q
.proto
== Q_LINK
) {
8809 * The lexer currently defines the address format in a
8810 * way that makes this error condition never true.
8811 * Let's check it anyway in case this part of the lexer
8812 * changes in future.
8814 if (! pcapint_atoan(s
, &addr
))
8815 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8816 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8818 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8822 bpf_error(cstate
, "aid supported only on ARCnet");
8827 // Compare an ARCnet host address with the given value.
8828 static struct block
*
8829 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8831 register struct block
*b0
, *b1
;
8835 * ARCnet is different from Ethernet: the source address comes before
8836 * the destination address, each is one byte long. This holds for all
8837 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8838 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8839 * by Datapoint (document number 61610-01).
8842 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8845 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8848 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8849 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8855 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8856 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8866 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8873 static struct block
*
8874 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8876 struct block
*b0
, *b1
;
8878 /* check for VLAN, including 802.1ad and QinQ */
8879 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8880 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8883 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8889 static struct block
*
8890 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8892 assert_maxval(cstate
, "VLAN tag", vlan_num
, 0x0fff);
8893 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8896 static struct block
*
8897 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8900 struct block
*b0
, *b1
;
8902 b0
= gen_vlan_tpid_test(cstate
);
8905 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8911 * Both payload and link header type follow the VLAN tags so that
8912 * both need to be updated.
8914 cstate
->off_linkpl
.constant_part
+= 4;
8915 cstate
->off_linktype
.constant_part
+= 4;
8920 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8921 /* add v to variable part of off */
8923 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8924 bpf_u_int32 v
, struct slist
*s
)
8928 if (!off
->is_variable
)
8929 off
->is_variable
= 1;
8931 off
->reg
= alloc_reg(cstate
);
8933 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8936 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8939 s2
= new_stmt(cstate
, BPF_ST
);
8945 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8946 * and link type offsets first
8949 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8953 /* offset determined at run time, shift variable part */
8955 cstate
->is_vlan_vloffset
= 1;
8956 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8957 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8959 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8960 sappend(s
.next
, b_tpid
->head
->stmts
);
8961 b_tpid
->head
->stmts
= s
.next
;
8965 * patch block b_vid (VLAN id test) to load VID value either from packet
8966 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8969 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8971 struct slist
*s
, *s2
, *sjeq
;
8974 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8975 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8977 /* true -> next instructions, false -> beginning of b_vid */
8978 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8980 sjeq
->s
.jf
= b_vid
->stmts
;
8983 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8984 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8988 /* Jump to the test in b_vid. We need to jump one instruction before
8989 * the end of the b_vid block so that we only skip loading the TCI
8990 * from packet data and not the 'and' instruction extracting VID.
8993 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8995 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8999 /* insert our statements at the beginning of b_vid */
9000 sappend(s
, b_vid
->stmts
);
9005 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9006 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9007 * tag can be either in metadata or in packet data; therefore if the
9008 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9009 * header for VLAN tag. As the decision is done at run time, we need
9010 * update variable part of the offsets
9012 static struct block
*
9013 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9016 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9019 /* generate new filter code based on extracting packet
9021 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9022 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9024 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9029 * This is tricky. We need to insert the statements updating variable
9030 * parts of offsets before the traditional TPID and VID tests so
9031 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9032 * we do not want this update to affect those checks. That's why we
9033 * generate both test blocks first and insert the statements updating
9034 * variable parts of both offsets after that. This wouldn't work if
9035 * there already were variable length link header when entering this
9036 * function but gen_vlan_bpf_extensions() isn't called in that case.
9038 b_tpid
= gen_vlan_tpid_test(cstate
);
9040 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9042 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9047 gen_vlan_patch_vid_test(cstate
, b_vid
);
9057 * support IEEE 802.1Q VLAN trunk over ethernet
9060 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9065 * Catch errors reported by us and routines below us, and return NULL
9068 if (setjmp(cstate
->top_ctx
))
9071 /* can't check for VLAN-encapsulated packets inside MPLS */
9072 if (cstate
->label_stack_depth
> 0)
9073 bpf_error(cstate
, "no VLAN match after MPLS");
9076 * Check for a VLAN packet, and then change the offsets to point
9077 * to the type and data fields within the VLAN packet. Just
9078 * increment the offsets, so that we can support a hierarchy, e.g.
9079 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9082 * XXX - this is a bit of a kludge. If we were to split the
9083 * compiler into a parser that parses an expression and
9084 * generates an expression tree, and a code generator that
9085 * takes an expression tree (which could come from our
9086 * parser or from some other parser) and generates BPF code,
9087 * we could perhaps make the offsets parameters of routines
9088 * and, in the handler for an "AND" node, pass to subnodes
9089 * other than the VLAN node the adjusted offsets.
9091 * This would mean that "vlan" would, instead of changing the
9092 * behavior of *all* tests after it, change only the behavior
9093 * of tests ANDed with it. That would change the documented
9094 * semantics of "vlan", which might break some expressions.
9095 * However, it would mean that "(vlan and ip) or ip" would check
9096 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9097 * checking only for VLAN-encapsulated IP, so that could still
9098 * be considered worth doing; it wouldn't break expressions
9099 * that are of the form "vlan and ..." or "vlan N and ...",
9100 * which I suspect are the most common expressions involving
9101 * "vlan". "vlan or ..." doesn't necessarily do what the user
9102 * would really want, now, as all the "or ..." tests would
9103 * be done assuming a VLAN, even though the "or" could be viewed
9104 * as meaning "or, if this isn't a VLAN packet...".
9106 switch (cstate
->linktype
) {
9110 * Newer version of the Linux kernel pass around
9111 * packets in which the VLAN tag has been removed
9112 * from the packet data and put into metadata.
9114 * This requires special treatment.
9116 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9117 /* Verify that this is the outer part of the packet and
9118 * not encapsulated somehow. */
9119 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9120 cstate
->off_linkhdr
.constant_part
==
9121 cstate
->off_outermostlinkhdr
.constant_part
) {
9123 * Do we need special VLAN handling?
9125 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9126 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9129 b0
= gen_vlan_no_bpf_extensions(cstate
,
9130 vlan_num
, has_vlan_tag
);
9133 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9137 case DLT_NETANALYZER
:
9138 case DLT_NETANALYZER_TRANSPARENT
:
9139 case DLT_IEEE802_11
:
9140 case DLT_PRISM_HEADER
:
9141 case DLT_IEEE802_11_RADIO_AVS
:
9142 case DLT_IEEE802_11_RADIO
:
9144 * These are either Ethernet packets with an additional
9145 * metadata header (the NetAnalyzer types), or 802.11
9146 * packets, possibly with an additional metadata header.
9148 * For the first of those, the VLAN tag is in the normal
9149 * place, so the special-case handling above isn't
9152 * For the second of those, we don't do the special-case
9155 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9159 bpf_error(cstate
, "no VLAN support for %s",
9160 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9164 cstate
->vlan_stack_depth
++;
9172 * The label_num_arg dance is to avoid annoying whining by compilers that
9173 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9174 * It's not *used* after setjmp returns.
9176 static struct block
*
9177 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9180 struct block
*b0
, *b1
;
9182 if (cstate
->label_stack_depth
> 0) {
9183 /* just match the bottom-of-stack bit clear */
9184 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9187 * We're not in an MPLS stack yet, so check the link-layer
9188 * type against MPLS.
9190 switch (cstate
->linktype
) {
9192 case DLT_C_HDLC
: /* fall through */
9195 case DLT_NETANALYZER
:
9196 case DLT_NETANALYZER_TRANSPARENT
:
9197 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9201 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9204 /* FIXME add other DLT_s ...
9205 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9206 * leave it for now */
9209 bpf_error(cstate
, "no MPLS support for %s",
9210 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9215 /* If a specific MPLS label is requested, check it */
9216 if (has_label_num
) {
9217 assert_maxval(cstate
, "MPLS label", label_num
, 0xFFFFF);
9218 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9219 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9220 0xfffff000); /* only compare the first 20 bits */
9226 * Change the offsets to point to the type and data fields within
9227 * the MPLS packet. Just increment the offsets, so that we
9228 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9229 * capture packets with an outer label of 100000 and an inner
9232 * Increment the MPLS stack depth as well; this indicates that
9233 * we're checking MPLS-encapsulated headers, to make sure higher
9234 * level code generators don't try to match against IP-related
9235 * protocols such as Q_ARP, Q_RARP etc.
9237 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9239 cstate
->off_nl_nosnap
+= 4;
9240 cstate
->off_nl
+= 4;
9241 cstate
->label_stack_depth
++;
9246 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9249 * Catch errors reported by us and routines below us, and return NULL
9252 if (setjmp(cstate
->top_ctx
))
9255 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9259 * Support PPPOE discovery and session.
9262 gen_pppoed(compiler_state_t
*cstate
)
9265 * Catch errors reported by us and routines below us, and return NULL
9268 if (setjmp(cstate
->top_ctx
))
9271 /* check for PPPoE discovery */
9272 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9276 * RFC 2516 Section 4:
9278 * The Ethernet payload for PPPoE is as follows:
9281 * 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
9282 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9283 * | VER | TYPE | CODE | SESSION_ID |
9284 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9285 * | LENGTH | payload ~
9286 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9289 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9291 struct block
*b0
, *b1
;
9294 * Catch errors reported by us and routines below us, and return NULL
9297 if (setjmp(cstate
->top_ctx
))
9301 * Test against the PPPoE session link-layer type.
9303 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9305 /* If a specific session is requested, check PPPoE session id */
9307 assert_maxval(cstate
, "PPPoE session number", sess_num
, UINT16_MAX
);
9308 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9314 * Change the offsets to point to the type and data fields within
9315 * the PPP packet, and note that this is PPPoE rather than
9318 * XXX - this is a bit of a kludge. See the comments in
9321 * The "network-layer" protocol is PPPoE, which has a 6-byte
9322 * PPPoE header, followed by a PPP packet.
9324 * There is no HDLC encapsulation for the PPP packet (it's
9325 * encapsulated in PPPoES instead), so the link-layer type
9326 * starts at the first byte of the PPP packet. For PPPoE,
9327 * that offset is relative to the beginning of the total
9328 * link-layer payload, including any 802.2 LLC header, so
9329 * it's 6 bytes past cstate->off_nl.
9331 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9332 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9333 cstate
->off_linkpl
.reg
);
9335 cstate
->off_linktype
= cstate
->off_linkhdr
;
9336 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9339 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9344 /* Check that this is Geneve and the VNI is correct if
9345 * specified. Parameterized to handle both IPv4 and IPv6. */
9346 static struct block
*
9347 gen_geneve_check(compiler_state_t
*cstate
,
9348 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9349 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9351 struct block
*b0
, *b1
;
9353 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9355 /* Check that we are operating on version 0. Otherwise, we
9356 * can't decode the rest of the fields. The version is 2 bits
9357 * in the first byte of the Geneve header. */
9358 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9363 assert_maxval(cstate
, "Geneve VNI", vni
, 0xffffff);
9364 vni
<<= 8; /* VNI is in the upper 3 bytes */
9365 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9373 /* The IPv4 and IPv6 Geneve checks need to do two things:
9374 * - Verify that this actually is Geneve with the right VNI.
9375 * - Place the IP header length (plus variable link prefix if
9376 * needed) into register A to be used later to compute
9377 * the inner packet offsets. */
9378 static struct block
*
9379 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9381 struct block
*b0
, *b1
;
9382 struct slist
*s
, *s1
;
9384 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9386 /* Load the IP header length into A. */
9387 s
= gen_loadx_iphdrlen(cstate
);
9389 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9392 /* Forcibly append these statements to the true condition
9393 * of the protocol check by creating a new block that is
9394 * always true and ANDing them. */
9395 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9404 static struct block
*
9405 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9407 struct block
*b0
, *b1
;
9408 struct slist
*s
, *s1
;
9410 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9412 /* Load the IP header length. We need to account for a
9413 * variable length link prefix if there is one. */
9414 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9416 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9420 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9424 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9428 /* Forcibly append these statements to the true condition
9429 * of the protocol check by creating a new block that is
9430 * always true and ANDing them. */
9431 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9434 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9443 /* We need to store three values based on the Geneve header::
9444 * - The offset of the linktype.
9445 * - The offset of the end of the Geneve header.
9446 * - The offset of the end of the encapsulated MAC header. */
9447 static struct slist
*
9448 gen_geneve_offsets(compiler_state_t
*cstate
)
9450 struct slist
*s
, *s1
, *s_proto
;
9452 /* First we need to calculate the offset of the Geneve header
9453 * itself. This is composed of the IP header previously calculated
9454 * (include any variable link prefix) and stored in A plus the
9455 * fixed sized headers (fixed link prefix, MAC length, and UDP
9457 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9458 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9460 /* Stash this in X since we'll need it later. */
9461 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9464 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9466 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9470 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9471 cstate
->off_linktype
.is_variable
= 1;
9472 cstate
->off_linktype
.constant_part
= 0;
9474 s1
= new_stmt(cstate
, BPF_ST
);
9475 s1
->s
.k
= cstate
->off_linktype
.reg
;
9478 /* Load the Geneve option length and mask and shift to get the
9479 * number of bytes. It is stored in the first byte of the Geneve
9481 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9485 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9489 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9493 /* Add in the rest of the Geneve base header. */
9494 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9498 /* Add the Geneve header length to its offset and store. */
9499 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9503 /* Set the encapsulated type as Ethernet. Even though we may
9504 * not actually have Ethernet inside there are two reasons this
9506 * - The linktype field is always in EtherType format regardless
9507 * of whether it is in Geneve or an inner Ethernet frame.
9508 * - The only link layer that we have specific support for is
9509 * Ethernet. We will confirm that the packet actually is
9510 * Ethernet at runtime before executing these checks. */
9511 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9513 s1
= new_stmt(cstate
, BPF_ST
);
9514 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9517 /* Calculate whether we have an Ethernet header or just raw IP/
9518 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9519 * and linktype by 14 bytes so that the network header can be found
9520 * seamlessly. Otherwise, keep what we've calculated already. */
9522 /* We have a bare jmp so we can't use the optimizer. */
9523 cstate
->no_optimize
= 1;
9525 /* Load the EtherType in the Geneve header, 2 bytes in. */
9526 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9530 /* Load X with the end of the Geneve header. */
9531 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9532 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9535 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9536 * end of this check, we should have the total length in X. In
9537 * the non-Ethernet case, it's already there. */
9538 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9539 s_proto
->s
.k
= ETHERTYPE_TEB
;
9540 sappend(s
, s_proto
);
9542 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9546 /* Since this is Ethernet, use the EtherType of the payload
9547 * directly as the linktype. Overwrite what we already have. */
9548 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9552 s1
= new_stmt(cstate
, BPF_ST
);
9553 s1
->s
.k
= cstate
->off_linktype
.reg
;
9556 /* Advance two bytes further to get the end of the Ethernet
9558 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9562 /* Move the result to X. */
9563 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9566 /* Store the final result of our linkpl calculation. */
9567 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9568 cstate
->off_linkpl
.is_variable
= 1;
9569 cstate
->off_linkpl
.constant_part
= 0;
9571 s1
= new_stmt(cstate
, BPF_STX
);
9572 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9581 /* Check to see if this is a Geneve packet. */
9583 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9585 struct block
*b0
, *b1
;
9589 * Catch errors reported by us and routines below us, and return NULL
9592 if (setjmp(cstate
->top_ctx
))
9595 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9596 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9601 /* Later filters should act on the payload of the Geneve frame,
9602 * update all of the header pointers. Attach this code so that
9603 * it gets executed in the event that the Geneve filter matches. */
9604 s
= gen_geneve_offsets(cstate
);
9606 b1
= gen_true(cstate
);
9607 sappend(s
, b1
->stmts
);
9612 cstate
->is_encap
= 1;
9617 /* Check that this is VXLAN and the VNI is correct if
9618 * specified. Parameterized to handle both IPv4 and IPv6. */
9619 static struct block
*
9620 gen_vxlan_check(compiler_state_t
*cstate
,
9621 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9622 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9624 struct block
*b0
, *b1
;
9626 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9628 /* Check that the VXLAN header has the flag bits set
9630 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9635 assert_maxval(cstate
, "VXLAN VNI", vni
, 0xffffff);
9636 vni
<<= 8; /* VNI is in the upper 3 bytes */
9637 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9645 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9646 * - Verify that this actually is VXLAN with the right VNI.
9647 * - Place the IP header length (plus variable link prefix if
9648 * needed) into register A to be used later to compute
9649 * the inner packet offsets. */
9650 static struct block
*
9651 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9653 struct block
*b0
, *b1
;
9654 struct slist
*s
, *s1
;
9656 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9658 /* Load the IP header length into A. */
9659 s
= gen_loadx_iphdrlen(cstate
);
9661 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9664 /* Forcibly append these statements to the true condition
9665 * of the protocol check by creating a new block that is
9666 * always true and ANDing them. */
9667 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9676 static struct block
*
9677 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9679 struct block
*b0
, *b1
;
9680 struct slist
*s
, *s1
;
9682 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9684 /* Load the IP header length. We need to account for a
9685 * variable length link prefix if there is one. */
9686 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9688 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9692 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9696 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9700 /* Forcibly append these statements to the true condition
9701 * of the protocol check by creating a new block that is
9702 * always true and ANDing them. */
9703 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9706 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9715 /* We need to store three values based on the VXLAN header:
9716 * - The offset of the linktype.
9717 * - The offset of the end of the VXLAN header.
9718 * - The offset of the end of the encapsulated MAC header. */
9719 static struct slist
*
9720 gen_vxlan_offsets(compiler_state_t
*cstate
)
9722 struct slist
*s
, *s1
;
9724 /* Calculate the offset of the VXLAN header itself. This
9725 * includes the IP header computed previously (including any
9726 * variable link prefix) and stored in A plus the fixed size
9727 * headers (fixed link prefix, MAC length, UDP header). */
9728 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9729 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9731 /* Add the VXLAN header length to its offset and store */
9732 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9736 /* Push the link header. VXLAN packets always contain Ethernet
9738 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9740 s1
= new_stmt(cstate
, BPF_ST
);
9741 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9744 /* As the payload is an Ethernet packet, we can use the
9745 * EtherType of the payload directly as the linktype. */
9746 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9750 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9751 cstate
->off_linktype
.is_variable
= 1;
9752 cstate
->off_linktype
.constant_part
= 0;
9754 s1
= new_stmt(cstate
, BPF_ST
);
9755 s1
->s
.k
= cstate
->off_linktype
.reg
;
9758 /* Two bytes further is the end of the Ethernet header and the
9759 * start of the payload. */
9760 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9764 /* Move the result to X. */
9765 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9768 /* Store the final result of our linkpl calculation. */
9769 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9770 cstate
->off_linkpl
.is_variable
= 1;
9771 cstate
->off_linkpl
.constant_part
= 0;
9773 s1
= new_stmt(cstate
, BPF_STX
);
9774 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9782 /* Check to see if this is a VXLAN packet. */
9784 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9786 struct block
*b0
, *b1
;
9790 * Catch errors reported by us and routines below us, and return NULL
9793 if (setjmp(cstate
->top_ctx
))
9796 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9797 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9802 /* Later filters should act on the payload of the VXLAN frame,
9803 * update all of the header pointers. Attach this code so that
9804 * it gets executed in the event that the VXLAN filter matches. */
9805 s
= gen_vxlan_offsets(cstate
);
9807 b1
= gen_true(cstate
);
9808 sappend(s
, b1
->stmts
);
9813 cstate
->is_encap
= 1;
9818 /* Check that the encapsulated frame has a link layer header
9819 * for Ethernet filters. */
9820 static struct block
*
9821 gen_encap_ll_check(compiler_state_t
*cstate
)
9824 struct slist
*s
, *s1
;
9826 /* The easiest way to see if there is a link layer present
9827 * is to check if the link layer header and payload are not
9830 /* Geneve always generates pure variable offsets so we can
9831 * compare only the registers. */
9832 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9833 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9835 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9836 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9839 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9847 static struct block
*
9848 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9849 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9854 * This check is a no-op for A_MSGTYPE so long as the only incoming
9855 * code path is from gen_atmmulti_abbrev(), which makes the same
9856 * check first; also for A_PROTOTYPE so long as the only incoming code
9857 * paths are from gen_atmtype_abbrev(), which makes the same check
9858 * first, or from gen_llc_internal() or gen_linktype(), which restrict
9861 assert_atm(cstate
, atmkw(atmfield
));
9866 assert_maxval(cstate
, "VPI", jvalue
, UINT8_MAX
);
9867 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9868 0xffffffffU
, jtype
, reverse
, jvalue
);
9872 assert_maxval(cstate
, "VCI", jvalue
, UINT16_MAX
);
9873 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9874 0xffffffffU
, jtype
, reverse
, jvalue
);
9878 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9879 0x0fU
, jtype
, reverse
, jvalue
);
9883 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9884 0xffffffffU
, jtype
, reverse
, jvalue
);
9893 static struct block
*
9894 gen_atmtype_metac(compiler_state_t
*cstate
)
9896 struct block
*b0
, *b1
;
9898 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9899 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9904 static struct block
*
9905 gen_atmtype_sc(compiler_state_t
*cstate
)
9907 struct block
*b0
, *b1
;
9909 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9910 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9915 static struct block
*
9916 gen_atmtype_llc(compiler_state_t
*cstate
)
9920 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9921 cstate
->linktype
= cstate
->prevlinktype
;
9926 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9927 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9930 * Catch errors reported by us and routines below us, and return NULL
9933 if (setjmp(cstate
->top_ctx
))
9936 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9941 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9943 struct block
*b0
, *b1
;
9946 * Catch errors reported by us and routines below us, and return NULL
9949 if (setjmp(cstate
->top_ctx
))
9952 assert_atm(cstate
, atmkw(type
));
9957 /* Get all packets in Meta signalling Circuit */
9958 b1
= gen_atmtype_metac(cstate
);
9962 /* Get all packets in Broadcast Circuit*/
9963 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9964 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9969 /* Get all cells in Segment OAM F4 circuit*/
9970 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9971 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9976 /* Get all cells in End-to-End OAM F4 Circuit*/
9977 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9978 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9983 /* Get all packets in connection Signalling Circuit */
9984 b1
= gen_atmtype_sc(cstate
);
9988 /* Get all packets in ILMI Circuit */
9989 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9990 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9995 /* Get all LANE packets */
9996 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9999 * Arrange that all subsequent tests assume LANE
10000 * rather than LLC-encapsulated packets, and set
10001 * the offsets appropriately for LANE-encapsulated
10004 * We assume LANE means Ethernet, not Token Ring.
10006 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10007 cstate
->off_payload
+ 2, /* Ethernet header */
10009 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10010 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10011 cstate
->off_nl
= 0; /* Ethernet II */
10012 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10022 * Filtering for MTP2 messages based on li value
10023 * FISU, length is null
10024 * LSSU, length is 1 or 2
10025 * MSU, length is 3 or more
10026 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10029 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10031 struct block
*b0
, *b1
;
10034 * Catch errors reported by us and routines below us, and return NULL
10037 if (setjmp(cstate
->top_ctx
))
10040 assert_ss7(cstate
, ss7kw(type
));
10045 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10046 0x3fU
, BPF_JEQ
, 0, 0U);
10050 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10051 0x3fU
, BPF_JGT
, 1, 2U);
10052 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10053 0x3fU
, BPF_JGT
, 0, 0U);
10058 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10059 0x3fU
, BPF_JGT
, 0, 2U);
10063 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10064 0xff80U
, BPF_JEQ
, 0, 0U);
10068 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10069 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10070 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10071 0xff80U
, BPF_JGT
, 0, 0U);
10076 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10077 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10087 * These maximum valid values are all-ones, so they double as the bitmasks
10088 * before any bitwise shifting.
10090 #define MTP2_SIO_MAXVAL UINT8_MAX
10091 #define MTP3_PC_MAXVAL 0x3fffU
10092 #define MTP3_SLS_MAXVAL 0xfU
10094 static struct block
*
10095 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10096 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10104 newoff_sio
= cstate
->off_sio
;
10105 newoff_opc
= cstate
->off_opc
;
10106 newoff_dpc
= cstate
->off_dpc
;
10107 newoff_sls
= cstate
->off_sls
;
10109 assert_ss7(cstate
, ss7kw(mtp3field
));
10111 switch (mtp3field
) {
10114 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10116 * SIO is the simplest field: the size is one byte and the offset is a
10117 * multiple of bytes, so the only detail to get right is the value of
10118 * the [right-to-left] field offset.
10121 newoff_sio
+= 3; /* offset for MTP2_HSL */
10125 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP2_SIO_MAXVAL
);
10126 // Here the bitmask means "do not apply a bitmask".
10127 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10128 jtype
, reverse
, jvalue
);
10132 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10134 * SLS, OPC and DPC are more complicated: none of these is sized in a
10135 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10136 * diagrams are meant to be read right-to-left. This means in the
10137 * diagrams within individual fields and concatenations thereof
10138 * bitwise shifts and masks can be noted in the common left-to-right
10139 * manner until each final value is ready to be byte-swapped and
10140 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10141 * similar problem in a similar way.
10143 * Offsets of fields within the packet header always have the
10144 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10145 * DLTs the offset does not include the F (Flag) field at the
10146 * beginning of each message.
10148 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10149 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10150 * be tested entirely using a single BPF_W comparison. In this case
10151 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10152 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10153 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10154 * correlates with the [RTL] packet diagram until the byte-swapping is
10157 * The code below uses this approach for OPC, which spans 3 bytes.
10158 * DPC and SLS use shorter loads, SLS also uses a different offset.
10165 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
10166 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10167 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10168 SWAPLONG(jvalue
<< 14));
10176 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_PC_MAXVAL
);
10177 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10178 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10179 SWAPSHORT(jvalue
));
10187 assert_maxval(cstate
, ss7kw(mtp3field
), jvalue
, MTP3_SLS_MAXVAL
);
10188 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10189 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10200 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10201 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10204 * Catch errors reported by us and routines below us, and return NULL
10207 if (setjmp(cstate
->top_ctx
))
10210 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10214 static struct block
*
10215 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10220 * Q.2931 signalling protocol messages for handling virtual circuits
10221 * establishment and teardown
10226 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10229 case A_CALLPROCEED
:
10230 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10234 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10238 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10242 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10245 case A_RELEASE_DONE
:
10246 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10256 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10258 struct block
*b0
, *b1
;
10261 * Catch errors reported by us and routines below us, and return NULL
10264 if (setjmp(cstate
->top_ctx
))
10267 assert_atm(cstate
, atmkw(type
));
10273 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10274 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10276 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10282 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10283 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10285 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10291 * Get Q.2931 signalling messages for switched
10292 * virtual connection
10294 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10295 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10297 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10299 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10301 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10303 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10305 b0
= gen_atmtype_sc(cstate
);
10309 case A_METACONNECT
:
10310 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10311 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10313 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10315 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10317 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10319 b0
= gen_atmtype_metac(cstate
);