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]));
950 static PCAP_NORETURN_DEF
void
951 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
953 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
954 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
958 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
960 if (cstate
->linktype
!= DLT_PFLOG
)
961 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
964 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
965 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
968 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
969 const char *buf
, int optimize
, bpf_u_int32 mask
)
975 compiler_state_t cstate
;
976 yyscan_t scanner
= NULL
;
977 YY_BUFFER_STATE in_buffer
= NULL
;
982 * If this pcap_t hasn't been activated, it doesn't have a
983 * link-layer type, so we can't use it.
986 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
987 "not-yet-activated pcap_t passed to pcap_compile");
993 * Initialize Winsock, asking for the latest version (2.2),
994 * as we may be calling Winsock routines to translate
995 * host names to addresses.
997 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
999 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1000 err
, "Error calling WSAStartup()");
1001 return (PCAP_ERROR
);
1005 #ifdef ENABLE_REMOTE
1007 * If the device on which we're capturing need to be notified
1008 * that a new filter is being compiled, do so.
1010 * This allows them to save a copy of it, in case, for example,
1011 * they're implementing a form of remote packet capture, and
1012 * want the remote machine to filter out the packets in which
1013 * it's sending the packets it's captured.
1015 * XXX - the fact that we happen to be compiling a filter
1016 * doesn't necessarily mean we'll be installing it as the
1017 * filter for this pcap_t; we might be running it from userland
1018 * on captured packets to do packet classification. We really
1019 * need a better way of handling this, but this is all that
1020 * the WinPcap remote capture code did.
1022 if (p
->save_current_filter_op
!= NULL
)
1023 (p
->save_current_filter_op
)(p
, buf
);
1026 initchunks(&cstate
);
1027 cstate
.no_optimize
= 0;
1032 cstate
.ic
.root
= NULL
;
1033 cstate
.ic
.cur_mark
= 0;
1034 cstate
.bpf_pcap
= p
;
1035 cstate
.error_set
= 0;
1038 cstate
.netmask
= mask
;
1040 cstate
.snaplen
= pcap_snapshot(p
);
1041 if (cstate
.snaplen
== 0) {
1042 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1043 "snaplen of 0 rejects all packets");
1048 if (pcap_lex_init(&scanner
) != 0) {
1049 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1050 errno
, "can't initialize scanner");
1054 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1057 * Associate the compiler state with the lexical analyzer
1060 pcap_set_extra(&cstate
, scanner
);
1062 if (init_linktype(&cstate
, p
) == -1) {
1066 if (pcap_parse(scanner
, &cstate
) != 0) {
1068 if (cstate
.ai
!= NULL
)
1069 freeaddrinfo(cstate
.ai
);
1071 if (cstate
.e
!= NULL
)
1077 if (cstate
.ic
.root
== NULL
) {
1078 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1081 * Catch errors reported by gen_retblk().
1083 if (cstate
.ic
.root
== NULL
) {
1089 if (optimize
&& !cstate
.no_optimize
) {
1090 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1095 if (cstate
.ic
.root
== NULL
||
1096 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1097 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1098 "expression rejects all packets");
1103 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1104 cstate
.ic
.root
, &len
, p
->errbuf
);
1105 if (program
->bf_insns
== NULL
) {
1110 program
->bf_len
= len
;
1112 rc
= 0; /* We're all okay */
1116 * Clean up everything for the lexical analyzer.
1118 if (in_buffer
!= NULL
)
1119 pcap__delete_buffer(in_buffer
, scanner
);
1120 if (scanner
!= NULL
)
1121 pcap_lex_destroy(scanner
);
1124 * Clean up our own allocated memory.
1126 freechunks(&cstate
);
1136 * entry point for using the compiler with no pcap open
1137 * pass in all the stuff that is needed explicitly instead.
1140 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1141 struct bpf_program
*program
,
1142 const char *buf
, int optimize
, bpf_u_int32 mask
)
1147 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1149 return (PCAP_ERROR
);
1150 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1156 * Clean up a "struct bpf_program" by freeing all the memory allocated
1160 pcap_freecode(struct bpf_program
*program
)
1162 program
->bf_len
= 0;
1163 if (program
->bf_insns
!= NULL
) {
1164 free((char *)program
->bf_insns
);
1165 program
->bf_insns
= NULL
;
1170 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1171 * which of the jt and jf fields has been resolved and which is a pointer
1172 * back to another unresolved block (or nil). At least one of the fields
1173 * in each block is already resolved.
1176 backpatch(struct block
*list
, struct block
*target
)
1193 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1194 * which of jt and jf is the link.
1197 merge(struct block
*b0
, struct block
*b1
)
1199 register struct block
**p
= &b0
;
1201 /* Find end of list. */
1203 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1205 /* Concatenate the lists. */
1210 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1213 * Catch errors reported by us and routines below us, and return -1
1216 if (setjmp(cstate
->top_ctx
))
1220 * Insert before the statements of the first (root) block any
1221 * statements needed to load the lengths of any variable-length
1222 * headers into registers.
1224 * XXX - a fancier strategy would be to insert those before the
1225 * statements of all blocks that use those lengths and that
1226 * have no predecessors that use them, so that we only compute
1227 * the lengths if we need them. There might be even better
1228 * approaches than that.
1230 * However, those strategies would be more complicated, and
1231 * as we don't generate code to compute a length if the
1232 * program has no tests that use the length, and as most
1233 * tests will probably use those lengths, we would just
1234 * postpone computing the lengths so that it's not done
1235 * for tests that fail early, and it's not clear that's
1238 insert_compute_vloffsets(cstate
, p
->head
);
1241 * For DLT_PPI captures, generate a check of the per-packet
1242 * DLT value to make sure it's DLT_IEEE802_11.
1244 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1245 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1246 * with appropriate Ethernet information and use that rather
1247 * than using something such as DLT_PPI where you don't know
1248 * the link-layer header type until runtime, which, in the
1249 * general case, would force us to generate both Ethernet *and*
1250 * 802.11 code (*and* anything else for which PPI is used)
1251 * and choose between them early in the BPF program?
1253 if (cstate
->linktype
== DLT_PPI
) {
1254 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1255 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1256 gen_and(ppi_dlt_check
, p
);
1259 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1260 p
->sense
= !p
->sense
;
1261 backpatch(p
, gen_retblk_internal(cstate
, 0));
1262 cstate
->ic
.root
= p
->head
;
1267 gen_and(struct block
*b0
, struct block
*b1
)
1269 backpatch(b0
, b1
->head
);
1270 b0
->sense
= !b0
->sense
;
1271 b1
->sense
= !b1
->sense
;
1273 b1
->sense
= !b1
->sense
;
1274 b1
->head
= b0
->head
;
1278 gen_or(struct block
*b0
, struct block
*b1
)
1280 b0
->sense
= !b0
->sense
;
1281 backpatch(b0
, b1
->head
);
1282 b0
->sense
= !b0
->sense
;
1284 b1
->head
= b0
->head
;
1288 gen_not(struct block
*b
)
1290 b
->sense
= !b
->sense
;
1293 static struct block
*
1294 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1295 u_int size
, bpf_u_int32 v
)
1297 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1300 static struct block
*
1301 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1302 u_int size
, bpf_u_int32 v
)
1304 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1307 static struct block
*
1308 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1309 u_int size
, bpf_u_int32 v
)
1311 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1314 static struct block
*
1315 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1316 u_int size
, bpf_u_int32 v
)
1318 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1321 static struct block
*
1322 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1323 u_int size
, bpf_u_int32 v
)
1325 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1328 static struct block
*
1329 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1330 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1332 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1335 static struct block
*
1336 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1337 u_int size
, const u_char
*v
)
1339 register struct block
*b
, *tmp
;
1343 register const u_char
*p
= &v
[size
- 4];
1345 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1353 register const u_char
*p
= &v
[size
- 2];
1355 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1363 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1372 * AND the field of size "size" at offset "offset" relative to the header
1373 * specified by "offrel" with "mask", and compare it with the value "v"
1374 * with the test specified by "jtype"; if "reverse" is true, the test
1375 * should test the opposite of "jtype".
1377 static struct block
*
1378 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1379 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1382 struct slist
*s
, *s2
;
1385 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1387 if (mask
!= 0xffffffff) {
1388 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1393 b
= new_block(cstate
, JMP(jtype
));
1402 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1404 cstate
->pcap_fddipad
= p
->fddipad
;
1407 * We start out with only one link-layer header.
1409 cstate
->outermostlinktype
= pcap_datalink(p
);
1410 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1411 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1412 cstate
->off_outermostlinkhdr
.reg
= -1;
1414 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1415 cstate
->off_prevlinkhdr
.constant_part
= 0;
1416 cstate
->off_prevlinkhdr
.is_variable
= 0;
1417 cstate
->off_prevlinkhdr
.reg
= -1;
1419 cstate
->linktype
= cstate
->outermostlinktype
;
1420 cstate
->off_linkhdr
.constant_part
= 0;
1421 cstate
->off_linkhdr
.is_variable
= 0;
1422 cstate
->off_linkhdr
.reg
= -1;
1427 cstate
->off_linkpl
.constant_part
= 0;
1428 cstate
->off_linkpl
.is_variable
= 0;
1429 cstate
->off_linkpl
.reg
= -1;
1431 cstate
->off_linktype
.constant_part
= 0;
1432 cstate
->off_linktype
.is_variable
= 0;
1433 cstate
->off_linktype
.reg
= -1;
1436 * Assume it's not raw ATM with a pseudo-header, for now.
1439 cstate
->off_vpi
= OFFSET_NOT_SET
;
1440 cstate
->off_vci
= OFFSET_NOT_SET
;
1441 cstate
->off_proto
= OFFSET_NOT_SET
;
1442 cstate
->off_payload
= OFFSET_NOT_SET
;
1445 * And not encapsulated with either Geneve or VXLAN.
1447 cstate
->is_encap
= 0;
1450 * No variable length VLAN offset by default
1452 cstate
->is_vlan_vloffset
= 0;
1455 * And assume we're not doing SS7.
1457 cstate
->off_li
= OFFSET_NOT_SET
;
1458 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1459 cstate
->off_sio
= OFFSET_NOT_SET
;
1460 cstate
->off_opc
= OFFSET_NOT_SET
;
1461 cstate
->off_dpc
= OFFSET_NOT_SET
;
1462 cstate
->off_sls
= OFFSET_NOT_SET
;
1464 cstate
->label_stack_depth
= 0;
1465 cstate
->vlan_stack_depth
= 0;
1467 switch (cstate
->linktype
) {
1470 cstate
->off_linktype
.constant_part
= 2;
1471 cstate
->off_linkpl
.constant_part
= 6;
1472 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1473 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1476 case DLT_ARCNET_LINUX
:
1477 cstate
->off_linktype
.constant_part
= 4;
1478 cstate
->off_linkpl
.constant_part
= 8;
1479 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1480 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1484 cstate
->off_linktype
.constant_part
= 12;
1485 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1486 cstate
->off_nl
= 0; /* Ethernet II */
1487 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1492 * SLIP doesn't have a link level type. The 16 byte
1493 * header is hacked into our SLIP driver.
1495 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1496 cstate
->off_linkpl
.constant_part
= 16;
1498 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1501 case DLT_SLIP_BSDOS
:
1502 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1503 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1505 cstate
->off_linkpl
.constant_part
= 24;
1507 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1512 cstate
->off_linktype
.constant_part
= 0;
1513 cstate
->off_linkpl
.constant_part
= 4;
1515 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1519 cstate
->off_linktype
.constant_part
= 0;
1520 cstate
->off_linkpl
.constant_part
= 12;
1522 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1527 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1528 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1529 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1530 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1531 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1533 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1538 * This does not include the Ethernet header, and
1539 * only covers session state.
1541 cstate
->off_linktype
.constant_part
= 6;
1542 cstate
->off_linkpl
.constant_part
= 8;
1544 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1548 cstate
->off_linktype
.constant_part
= 5;
1549 cstate
->off_linkpl
.constant_part
= 24;
1551 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1556 * FDDI doesn't really have a link-level type field.
1557 * We set "off_linktype" to the offset of the LLC header.
1559 * To check for Ethernet types, we assume that SSAP = SNAP
1560 * is being used and pick out the encapsulated Ethernet type.
1561 * XXX - should we generate code to check for SNAP?
1563 cstate
->off_linktype
.constant_part
= 13;
1564 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1565 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1566 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1567 cstate
->off_nl
= 8; /* 802.2+SNAP */
1568 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1573 * Token Ring doesn't really have a link-level type field.
1574 * We set "off_linktype" to the offset of the LLC header.
1576 * To check for Ethernet types, we assume that SSAP = SNAP
1577 * is being used and pick out the encapsulated Ethernet type.
1578 * XXX - should we generate code to check for SNAP?
1580 * XXX - the header is actually variable-length.
1581 * Some various Linux patched versions gave 38
1582 * as "off_linktype" and 40 as "off_nl"; however,
1583 * if a token ring packet has *no* routing
1584 * information, i.e. is not source-routed, the correct
1585 * values are 20 and 22, as they are in the vanilla code.
1587 * A packet is source-routed iff the uppermost bit
1588 * of the first byte of the source address, at an
1589 * offset of 8, has the uppermost bit set. If the
1590 * packet is source-routed, the total number of bytes
1591 * of routing information is 2 plus bits 0x1F00 of
1592 * the 16-bit value at an offset of 14 (shifted right
1593 * 8 - figure out which byte that is).
1595 cstate
->off_linktype
.constant_part
= 14;
1596 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1597 cstate
->off_nl
= 8; /* 802.2+SNAP */
1598 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1601 case DLT_PRISM_HEADER
:
1602 case DLT_IEEE802_11_RADIO_AVS
:
1603 case DLT_IEEE802_11_RADIO
:
1604 cstate
->off_linkhdr
.is_variable
= 1;
1605 /* Fall through, 802.11 doesn't have a variable link
1606 * prefix but is otherwise the same. */
1609 case DLT_IEEE802_11
:
1611 * 802.11 doesn't really have a link-level type field.
1612 * We set "off_linktype.constant_part" to the offset of
1615 * To check for Ethernet types, we assume that SSAP = SNAP
1616 * is being used and pick out the encapsulated Ethernet type.
1617 * XXX - should we generate code to check for SNAP?
1619 * We also handle variable-length radio headers here.
1620 * The Prism header is in theory variable-length, but in
1621 * practice it's always 144 bytes long. However, some
1622 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1623 * sometimes or always supply an AVS header, so we
1624 * have to check whether the radio header is a Prism
1625 * header or an AVS header, so, in practice, it's
1628 cstate
->off_linktype
.constant_part
= 24;
1629 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1630 cstate
->off_linkpl
.is_variable
= 1;
1631 cstate
->off_nl
= 8; /* 802.2+SNAP */
1632 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1637 * At the moment we treat PPI the same way that we treat
1638 * normal Radiotap encoded packets. The difference is in
1639 * the function that generates the code at the beginning
1640 * to compute the header length. Since this code generator
1641 * of PPI supports bare 802.11 encapsulation only (i.e.
1642 * the encapsulated DLT should be DLT_IEEE802_11) we
1643 * generate code to check for this too.
1645 cstate
->off_linktype
.constant_part
= 24;
1646 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1647 cstate
->off_linkpl
.is_variable
= 1;
1648 cstate
->off_linkhdr
.is_variable
= 1;
1649 cstate
->off_nl
= 8; /* 802.2+SNAP */
1650 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1653 case DLT_ATM_RFC1483
:
1654 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1656 * assume routed, non-ISO PDUs
1657 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1659 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1660 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1661 * latter would presumably be treated the way PPPoE
1662 * should be, so you can do "pppoe and udp port 2049"
1663 * or "pppoa and tcp port 80" and have it check for
1664 * PPPo{A,E} and a PPP protocol of IP and....
1666 cstate
->off_linktype
.constant_part
= 0;
1667 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1668 cstate
->off_nl
= 8; /* 802.2+SNAP */
1669 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1674 * Full Frontal ATM; you get AALn PDUs with an ATM
1678 cstate
->off_vpi
= SUNATM_VPI_POS
;
1679 cstate
->off_vci
= SUNATM_VCI_POS
;
1680 cstate
->off_proto
= PROTO_POS
;
1681 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1682 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1683 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1684 cstate
->off_nl
= 8; /* 802.2+SNAP */
1685 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1691 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1692 cstate
->off_linkpl
.constant_part
= 0;
1694 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1697 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1698 cstate
->off_linktype
.constant_part
= 14;
1699 cstate
->off_linkpl
.constant_part
= 16;
1701 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1704 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1705 cstate
->off_linktype
.constant_part
= 0;
1706 cstate
->off_linkpl
.constant_part
= 20;
1708 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1713 * LocalTalk does have a 1-byte type field in the LLAP header,
1714 * but really it just indicates whether there is a "short" or
1715 * "long" DDP packet following.
1717 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1718 cstate
->off_linkpl
.constant_part
= 0;
1720 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1723 case DLT_IP_OVER_FC
:
1725 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1726 * link-level type field. We set "off_linktype" to the
1727 * offset of the LLC header.
1729 * To check for Ethernet types, we assume that SSAP = SNAP
1730 * is being used and pick out the encapsulated Ethernet type.
1731 * XXX - should we generate code to check for SNAP? RFC
1732 * 2625 says SNAP should be used.
1734 cstate
->off_linktype
.constant_part
= 16;
1735 cstate
->off_linkpl
.constant_part
= 16;
1736 cstate
->off_nl
= 8; /* 802.2+SNAP */
1737 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1742 * XXX - we should set this to handle SNAP-encapsulated
1743 * frames (NLPID of 0x80).
1745 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1746 cstate
->off_linkpl
.constant_part
= 0;
1748 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1752 * the only BPF-interesting FRF.16 frames are non-control frames;
1753 * Frame Relay has a variable length link-layer
1754 * so lets start with offset 4 for now and increments later on (FIXME);
1757 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1758 cstate
->off_linkpl
.constant_part
= 0;
1760 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1763 case DLT_APPLE_IP_OVER_IEEE1394
:
1764 cstate
->off_linktype
.constant_part
= 16;
1765 cstate
->off_linkpl
.constant_part
= 18;
1767 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1770 case DLT_SYMANTEC_FIREWALL
:
1771 cstate
->off_linktype
.constant_part
= 6;
1772 cstate
->off_linkpl
.constant_part
= 44;
1773 cstate
->off_nl
= 0; /* Ethernet II */
1774 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1778 cstate
->off_linktype
.constant_part
= 0;
1779 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1780 cstate
->off_linkpl
.is_variable
= 1;
1782 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1785 case DLT_JUNIPER_MFR
:
1786 case DLT_JUNIPER_MLFR
:
1787 case DLT_JUNIPER_MLPPP
:
1788 case DLT_JUNIPER_PPP
:
1789 case DLT_JUNIPER_CHDLC
:
1790 case DLT_JUNIPER_FRELAY
:
1791 cstate
->off_linktype
.constant_part
= 4;
1792 cstate
->off_linkpl
.constant_part
= 4;
1794 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1797 case DLT_JUNIPER_ATM1
:
1798 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1799 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1801 cstate
->off_nl_nosnap
= 10;
1804 case DLT_JUNIPER_ATM2
:
1805 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1806 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1808 cstate
->off_nl_nosnap
= 10;
1811 /* frames captured on a Juniper PPPoE service PIC
1812 * contain raw ethernet frames */
1813 case DLT_JUNIPER_PPPOE
:
1814 case DLT_JUNIPER_ETHER
:
1815 cstate
->off_linkpl
.constant_part
= 14;
1816 cstate
->off_linktype
.constant_part
= 16;
1817 cstate
->off_nl
= 18; /* Ethernet II */
1818 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1821 case DLT_JUNIPER_PPPOE_ATM
:
1822 cstate
->off_linktype
.constant_part
= 4;
1823 cstate
->off_linkpl
.constant_part
= 6;
1825 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1828 case DLT_JUNIPER_GGSN
:
1829 cstate
->off_linktype
.constant_part
= 6;
1830 cstate
->off_linkpl
.constant_part
= 12;
1832 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1835 case DLT_JUNIPER_ES
:
1836 cstate
->off_linktype
.constant_part
= 6;
1837 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1838 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1839 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1842 case DLT_JUNIPER_MONITOR
:
1843 cstate
->off_linktype
.constant_part
= 12;
1844 cstate
->off_linkpl
.constant_part
= 12;
1845 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1846 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1849 case DLT_BACNET_MS_TP
:
1850 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1851 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1852 cstate
->off_nl
= OFFSET_NOT_SET
;
1853 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1856 case DLT_JUNIPER_SERVICES
:
1857 cstate
->off_linktype
.constant_part
= 12;
1858 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1859 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1860 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1863 case DLT_JUNIPER_VP
:
1864 cstate
->off_linktype
.constant_part
= 18;
1865 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1866 cstate
->off_nl
= OFFSET_NOT_SET
;
1867 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1870 case DLT_JUNIPER_ST
:
1871 cstate
->off_linktype
.constant_part
= 18;
1872 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1873 cstate
->off_nl
= OFFSET_NOT_SET
;
1874 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1877 case DLT_JUNIPER_ISM
:
1878 cstate
->off_linktype
.constant_part
= 8;
1879 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1880 cstate
->off_nl
= OFFSET_NOT_SET
;
1881 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1884 case DLT_JUNIPER_VS
:
1885 case DLT_JUNIPER_SRX_E2E
:
1886 case DLT_JUNIPER_FIBRECHANNEL
:
1887 case DLT_JUNIPER_ATM_CEMIC
:
1888 cstate
->off_linktype
.constant_part
= 8;
1889 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1890 cstate
->off_nl
= OFFSET_NOT_SET
;
1891 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1896 cstate
->off_li_hsl
= 4;
1897 cstate
->off_sio
= 3;
1898 cstate
->off_opc
= 4;
1899 cstate
->off_dpc
= 4;
1900 cstate
->off_sls
= 7;
1901 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1902 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1903 cstate
->off_nl
= OFFSET_NOT_SET
;
1904 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1907 case DLT_MTP2_WITH_PHDR
:
1909 cstate
->off_li_hsl
= 8;
1910 cstate
->off_sio
= 7;
1911 cstate
->off_opc
= 8;
1912 cstate
->off_dpc
= 8;
1913 cstate
->off_sls
= 11;
1914 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1915 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1916 cstate
->off_nl
= OFFSET_NOT_SET
;
1917 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1921 cstate
->off_li
= 22;
1922 cstate
->off_li_hsl
= 24;
1923 cstate
->off_sio
= 23;
1924 cstate
->off_opc
= 24;
1925 cstate
->off_dpc
= 24;
1926 cstate
->off_sls
= 27;
1927 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1928 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1929 cstate
->off_nl
= OFFSET_NOT_SET
;
1930 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1934 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1935 cstate
->off_linkpl
.constant_part
= 4;
1937 cstate
->off_nl_nosnap
= 0;
1942 * Currently, only raw "link[N:M]" filtering is supported.
1944 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1945 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1946 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1947 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1951 cstate
->off_linktype
.constant_part
= 1;
1952 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1954 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1957 case DLT_NETANALYZER
:
1958 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1959 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1960 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1961 cstate
->off_nl
= 0; /* Ethernet II */
1962 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1965 case DLT_NETANALYZER_TRANSPARENT
:
1966 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1967 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1968 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1969 cstate
->off_nl
= 0; /* Ethernet II */
1970 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1975 * For values in the range in which we've assigned new
1976 * DLT_ values, only raw "link[N:M]" filtering is supported.
1978 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
1979 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
1980 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1981 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1982 cstate
->off_nl
= OFFSET_NOT_SET
;
1983 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1985 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1986 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
1992 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1997 * Load a value relative to the specified absolute offset.
1999 static struct slist
*
2000 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2001 u_int offset
, u_int size
)
2003 struct slist
*s
, *s2
;
2005 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2008 * If "s" is non-null, it has code to arrange that the X register
2009 * contains the variable part of the absolute offset, so we
2010 * generate a load relative to that, with an offset of
2011 * abs_offset->constant_part + offset.
2013 * Otherwise, we can do an absolute load with an offset of
2014 * abs_offset->constant_part + offset.
2018 * "s" points to a list of statements that puts the
2019 * variable part of the absolute offset into the X register.
2020 * Do an indirect load, to use the X register as an offset.
2022 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2023 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2027 * There is no variable part of the absolute offset, so
2028 * just do an absolute load.
2030 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2031 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2037 * Load a value relative to the beginning of the specified header.
2039 static struct slist
*
2040 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2043 struct slist
*s
, *s2
;
2046 * Squelch warnings from compilers that *don't* assume that
2047 * offrel always has a valid enum value and therefore don't
2048 * assume that we'll always go through one of the case arms.
2050 * If we have a default case, compilers that *do* assume that
2051 * will then complain about the default case code being
2054 * Damned if you do, damned if you don't.
2061 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2066 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2069 case OR_PREVLINKHDR
:
2070 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2074 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2077 case OR_PREVMPLSHDR
:
2078 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2082 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2085 case OR_LINKPL_NOSNAP
:
2086 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2090 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2095 * Load the X register with the length of the IPv4 header
2096 * (plus the offset of the link-layer header, if it's
2097 * preceded by a variable-length header such as a radio
2098 * header), in bytes.
2100 s
= gen_loadx_iphdrlen(cstate
);
2103 * Load the item at {offset of the link-layer payload} +
2104 * {offset, relative to the start of the link-layer
2105 * payload, of the IPv4 header} + {length of the IPv4 header} +
2106 * {specified offset}.
2108 * If the offset of the link-layer payload is variable,
2109 * the variable part of that offset is included in the
2110 * value in the X register, and we include the constant
2111 * part in the offset of the load.
2113 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2114 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2119 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2126 * Generate code to load into the X register the sum of the length of
2127 * the IPv4 header and the variable part of the offset of the link-layer
2130 static struct slist
*
2131 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2133 struct slist
*s
, *s2
;
2135 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2138 * The offset of the link-layer payload has a variable
2139 * part. "s" points to a list of statements that put
2140 * the variable part of that offset into the X register.
2142 * The 4*([k]&0xf) addressing mode can't be used, as we
2143 * don't have a constant offset, so we have to load the
2144 * value in question into the A register and add to it
2145 * the value from the X register.
2147 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2148 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2150 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2153 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2158 * The A register now contains the length of the IP header.
2159 * We need to add to it the variable part of the offset of
2160 * the link-layer payload, which is still in the X
2161 * register, and move the result into the X register.
2163 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2164 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2167 * The offset of the link-layer payload is a constant,
2168 * so no code was generated to load the (nonexistent)
2169 * variable part of that offset.
2171 * This means we can use the 4*([k]&0xf) addressing
2172 * mode. Load the length of the IPv4 header, which
2173 * is at an offset of cstate->off_nl from the beginning of
2174 * the link-layer payload, and thus at an offset of
2175 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2176 * of the raw packet data, using that addressing mode.
2178 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2179 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2185 static struct block
*
2186 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2191 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2193 b
= new_block(cstate
, JMP(BPF_JEQ
));
2199 static inline struct block
*
2200 gen_true(compiler_state_t
*cstate
)
2202 return gen_uncond(cstate
, 1);
2205 static inline struct block
*
2206 gen_false(compiler_state_t
*cstate
)
2208 return gen_uncond(cstate
, 0);
2212 * Generate code to match a particular packet type.
2214 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2215 * value, if <= ETHERMTU. We use that to determine whether to
2216 * match the type/length field or to check the type/length field for
2217 * a value <= ETHERMTU to see whether it's a type field and then do
2218 * the appropriate test.
2220 static struct block
*
2221 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2223 struct block
*b0
, *b1
;
2229 case LLCSAP_NETBEUI
:
2231 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2232 * so we check the DSAP and SSAP.
2234 * LLCSAP_IP checks for IP-over-802.2, rather
2235 * than IP-over-Ethernet or IP-over-SNAP.
2237 * XXX - should we check both the DSAP and the
2238 * SSAP, like this, or should we check just the
2239 * DSAP, as we do for other types <= ETHERMTU
2240 * (i.e., other SAP values)?
2242 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2243 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2251 * Ethernet_II frames, which are Ethernet
2252 * frames with a frame type of ETHERTYPE_IPX;
2254 * Ethernet_802.3 frames, which are 802.3
2255 * frames (i.e., the type/length field is
2256 * a length field, <= ETHERMTU, rather than
2257 * a type field) with the first two bytes
2258 * after the Ethernet/802.3 header being
2261 * Ethernet_802.2 frames, which are 802.3
2262 * frames with an 802.2 LLC header and
2263 * with the IPX LSAP as the DSAP in the LLC
2266 * Ethernet_SNAP frames, which are 802.3
2267 * frames with an LLC header and a SNAP
2268 * header and with an OUI of 0x000000
2269 * (encapsulated Ethernet) and a protocol
2270 * ID of ETHERTYPE_IPX in the SNAP header.
2272 * XXX - should we generate the same code both
2273 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2277 * This generates code to check both for the
2278 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2280 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2281 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2285 * Now we add code to check for SNAP frames with
2286 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2288 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2292 * Now we generate code to check for 802.3
2293 * frames in general.
2295 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2298 * Now add the check for 802.3 frames before the
2299 * check for Ethernet_802.2 and Ethernet_802.3,
2300 * as those checks should only be done on 802.3
2301 * frames, not on Ethernet frames.
2306 * Now add the check for Ethernet_II frames, and
2307 * do that before checking for the other frame
2310 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2314 case ETHERTYPE_ATALK
:
2315 case ETHERTYPE_AARP
:
2317 * EtherTalk (AppleTalk protocols on Ethernet link
2318 * layer) may use 802.2 encapsulation.
2322 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2323 * we check for an Ethernet type field less or equal than
2324 * 1500, which means it's an 802.3 length field.
2326 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2329 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2330 * SNAP packets with an organization code of
2331 * 0x080007 (Apple, for Appletalk) and a protocol
2332 * type of ETHERTYPE_ATALK (Appletalk).
2334 * 802.2-encapsulated ETHERTYPE_AARP packets are
2335 * SNAP packets with an organization code of
2336 * 0x000000 (encapsulated Ethernet) and a protocol
2337 * type of ETHERTYPE_AARP (Appletalk ARP).
2339 if (ll_proto
== ETHERTYPE_ATALK
)
2340 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2341 else /* ll_proto == ETHERTYPE_AARP */
2342 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2346 * Check for Ethernet encapsulation (Ethertalk
2347 * phase 1?); we just check for the Ethernet
2350 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2356 if (ll_proto
<= ETHERMTU
) {
2358 * This is an LLC SAP value, so the frames
2359 * that match would be 802.2 frames.
2360 * Check that the frame is an 802.2 frame
2361 * (i.e., that the length/type field is
2362 * a length field, <= ETHERMTU) and
2363 * then check the DSAP.
2365 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2366 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2371 * This is an Ethernet type, so compare
2372 * the length/type field with it (if
2373 * the frame is an 802.2 frame, the length
2374 * field will be <= ETHERMTU, and, as
2375 * "ll_proto" is > ETHERMTU, this test
2376 * will fail and the frame won't match,
2377 * which is what we want).
2379 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2384 static struct block
*
2385 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2388 * For DLT_NULL, the link-layer header is a 32-bit word
2389 * containing an AF_ value in *host* byte order, and for
2390 * DLT_ENC, the link-layer header begins with a 32-bit
2391 * word containing an AF_ value in host byte order.
2393 * In addition, if we're reading a saved capture file,
2394 * the host byte order in the capture may not be the
2395 * same as the host byte order on this machine.
2397 * For DLT_LOOP, the link-layer header is a 32-bit
2398 * word containing an AF_ value in *network* byte order.
2400 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2402 * The AF_ value is in host byte order, but the BPF
2403 * interpreter will convert it to network byte order.
2405 * If this is a save file, and it's from a machine
2406 * with the opposite byte order to ours, we byte-swap
2409 * Then we run it through "htonl()", and generate
2410 * code to compare against the result.
2412 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2413 ll_proto
= SWAPLONG(ll_proto
);
2414 ll_proto
= htonl(ll_proto
);
2416 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2420 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2421 * or IPv6 then we have an error.
2423 static struct block
*
2424 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2429 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2432 case ETHERTYPE_IPV6
:
2433 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2440 return gen_false(cstate
);
2444 * Generate code to match a particular packet type.
2446 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2447 * value, if <= ETHERMTU. We use that to determine whether to
2448 * match the type field or to check the type field for the special
2449 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2451 static struct block
*
2452 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2454 struct block
*b0
, *b1
;
2460 case LLCSAP_NETBEUI
:
2462 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2463 * so we check the DSAP and SSAP.
2465 * LLCSAP_IP checks for IP-over-802.2, rather
2466 * than IP-over-Ethernet or IP-over-SNAP.
2468 * XXX - should we check both the DSAP and the
2469 * SSAP, like this, or should we check just the
2470 * DSAP, as we do for other types <= ETHERMTU
2471 * (i.e., other SAP values)?
2473 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2474 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2480 * Ethernet_II frames, which are Ethernet
2481 * frames with a frame type of ETHERTYPE_IPX;
2483 * Ethernet_802.3 frames, which have a frame
2484 * type of LINUX_SLL_P_802_3;
2486 * Ethernet_802.2 frames, which are 802.3
2487 * frames with an 802.2 LLC header (i.e, have
2488 * a frame type of LINUX_SLL_P_802_2) and
2489 * with the IPX LSAP as the DSAP in the LLC
2492 * Ethernet_SNAP frames, which are 802.3
2493 * frames with an LLC header and a SNAP
2494 * header and with an OUI of 0x000000
2495 * (encapsulated Ethernet) and a protocol
2496 * ID of ETHERTYPE_IPX in the SNAP header.
2498 * First, do the checks on LINUX_SLL_P_802_2
2499 * frames; generate the check for either
2500 * Ethernet_802.2 or Ethernet_SNAP frames, and
2501 * then put a check for LINUX_SLL_P_802_2 frames
2504 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2505 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2507 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2511 * Now check for 802.3 frames and OR that with
2512 * the previous test.
2514 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2518 * Now add the check for Ethernet_II frames, and
2519 * do that before checking for the other frame
2522 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2526 case ETHERTYPE_ATALK
:
2527 case ETHERTYPE_AARP
:
2529 * EtherTalk (AppleTalk protocols on Ethernet link
2530 * layer) may use 802.2 encapsulation.
2534 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2535 * we check for the 802.2 protocol type in the
2536 * "Ethernet type" field.
2538 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2541 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2542 * SNAP packets with an organization code of
2543 * 0x080007 (Apple, for Appletalk) and a protocol
2544 * type of ETHERTYPE_ATALK (Appletalk).
2546 * 802.2-encapsulated ETHERTYPE_AARP packets are
2547 * SNAP packets with an organization code of
2548 * 0x000000 (encapsulated Ethernet) and a protocol
2549 * type of ETHERTYPE_AARP (Appletalk ARP).
2551 if (ll_proto
== ETHERTYPE_ATALK
)
2552 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2553 else /* ll_proto == ETHERTYPE_AARP */
2554 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2558 * Check for Ethernet encapsulation (Ethertalk
2559 * phase 1?); we just check for the Ethernet
2562 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2568 if (ll_proto
<= ETHERMTU
) {
2570 * This is an LLC SAP value, so the frames
2571 * that match would be 802.2 frames.
2572 * Check for the 802.2 protocol type
2573 * in the "Ethernet type" field, and
2574 * then check the DSAP.
2576 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2577 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2583 * This is an Ethernet type, so compare
2584 * the length/type field with it (if
2585 * the frame is an 802.2 frame, the length
2586 * field will be <= ETHERMTU, and, as
2587 * "ll_proto" is > ETHERMTU, this test
2588 * will fail and the frame won't match,
2589 * which is what we want).
2591 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2597 * Load a value relative to the beginning of the link-layer header after the
2600 static struct slist
*
2601 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2603 struct slist
*s1
, *s2
;
2606 * Generate code to load the length of the pflog header into
2607 * the register assigned to hold that length, if one has been
2608 * assigned. (If one hasn't been assigned, no code we've
2609 * generated uses that prefix, so we don't need to generate any
2612 if (cstate
->off_linkpl
.reg
!= -1) {
2614 * The length is in the first byte of the header.
2616 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2620 * Round it up to a multiple of 4.
2621 * Add 3, and clear the lower 2 bits.
2623 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2626 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2627 s2
->s
.k
= 0xfffffffc;
2631 * Now allocate a register to hold that value and store
2634 s2
= new_stmt(cstate
, BPF_ST
);
2635 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2639 * Now move it into the X register.
2641 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2649 static struct slist
*
2650 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2652 struct slist
*s1
, *s2
;
2653 struct slist
*sjeq_avs_cookie
;
2654 struct slist
*sjcommon
;
2657 * This code is not compatible with the optimizer, as
2658 * we are generating jmp instructions within a normal
2659 * slist of instructions
2661 cstate
->no_optimize
= 1;
2664 * Generate code to load the length of the radio header into
2665 * the register assigned to hold that length, if one has been
2666 * assigned. (If one hasn't been assigned, no code we've
2667 * generated uses that prefix, so we don't need to generate any
2670 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2671 * or always use the AVS header rather than the Prism header.
2672 * We load a 4-byte big-endian value at the beginning of the
2673 * raw packet data, and see whether, when masked with 0xFFFFF000,
2674 * it's equal to 0x80211000. If so, that indicates that it's
2675 * an AVS header (the masked-out bits are the version number).
2676 * Otherwise, it's a Prism header.
2678 * XXX - the Prism header is also, in theory, variable-length,
2679 * but no known software generates headers that aren't 144
2682 if (cstate
->off_linkhdr
.reg
!= -1) {
2686 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2690 * AND it with 0xFFFFF000.
2692 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2693 s2
->s
.k
= 0xFFFFF000;
2697 * Compare with 0x80211000.
2699 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2700 sjeq_avs_cookie
->s
.k
= 0x80211000;
2701 sappend(s1
, sjeq_avs_cookie
);
2706 * The 4 bytes at an offset of 4 from the beginning of
2707 * the AVS header are the length of the AVS header.
2708 * That field is big-endian.
2710 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2713 sjeq_avs_cookie
->s
.jt
= s2
;
2716 * Now jump to the code to allocate a register
2717 * into which to save the header length and
2718 * store the length there. (The "jump always"
2719 * instruction needs to have the k field set;
2720 * it's added to the PC, so, as we're jumping
2721 * over a single instruction, it should be 1.)
2723 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2725 sappend(s1
, sjcommon
);
2728 * Now for the code that handles the Prism header.
2729 * Just load the length of the Prism header (144)
2730 * into the A register. Have the test for an AVS
2731 * header branch here if we don't have an AVS header.
2733 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2736 sjeq_avs_cookie
->s
.jf
= s2
;
2739 * Now allocate a register to hold that value and store
2740 * it. The code for the AVS header will jump here after
2741 * loading the length of the AVS header.
2743 s2
= new_stmt(cstate
, BPF_ST
);
2744 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2746 sjcommon
->s
.jf
= s2
;
2749 * Now move it into the X register.
2751 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2759 static struct slist
*
2760 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2762 struct slist
*s1
, *s2
;
2765 * Generate code to load the length of the AVS header into
2766 * the register assigned to hold that length, if one has been
2767 * assigned. (If one hasn't been assigned, no code we've
2768 * generated uses that prefix, so we don't need to generate any
2771 if (cstate
->off_linkhdr
.reg
!= -1) {
2773 * The 4 bytes at an offset of 4 from the beginning of
2774 * the AVS header are the length of the AVS header.
2775 * That field is big-endian.
2777 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2781 * Now allocate a register to hold that value and store
2784 s2
= new_stmt(cstate
, BPF_ST
);
2785 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2789 * Now move it into the X register.
2791 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2799 static struct slist
*
2800 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2802 struct slist
*s1
, *s2
;
2805 * Generate code to load the length of the radiotap header into
2806 * the register assigned to hold that length, if one has been
2807 * assigned. (If one hasn't been assigned, no code we've
2808 * generated uses that prefix, so we don't need to generate any
2811 if (cstate
->off_linkhdr
.reg
!= -1) {
2813 * The 2 bytes at offsets of 2 and 3 from the beginning
2814 * of the radiotap header are the length of the radiotap
2815 * header; unfortunately, it's little-endian, so we have
2816 * to load it a byte at a time and construct the value.
2820 * Load the high-order byte, at an offset of 3, shift it
2821 * left a byte, and put the result in the X register.
2823 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2825 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2828 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2832 * Load the next byte, at an offset of 2, and OR the
2833 * value from the X register into it.
2835 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2838 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2842 * Now allocate a register to hold that value and store
2845 s2
= new_stmt(cstate
, BPF_ST
);
2846 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2850 * Now move it into the X register.
2852 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2861 * At the moment we treat PPI as normal Radiotap encoded
2862 * packets. The difference is in the function that generates
2863 * the code at the beginning to compute the header length.
2864 * Since this code generator of PPI supports bare 802.11
2865 * encapsulation only (i.e. the encapsulated DLT should be
2866 * DLT_IEEE802_11) we generate code to check for this too;
2867 * that's done in finish_parse().
2869 static struct slist
*
2870 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2872 struct slist
*s1
, *s2
;
2875 * Generate code to load the length of the radiotap header
2876 * into the register assigned to hold that length, if one has
2879 if (cstate
->off_linkhdr
.reg
!= -1) {
2881 * The 2 bytes at offsets of 2 and 3 from the beginning
2882 * of the radiotap header are the length of the radiotap
2883 * header; unfortunately, it's little-endian, so we have
2884 * to load it a byte at a time and construct the value.
2888 * Load the high-order byte, at an offset of 3, shift it
2889 * left a byte, and put the result in the X register.
2891 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2893 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2896 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2900 * Load the next byte, at an offset of 2, and OR the
2901 * value from the X register into it.
2903 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2906 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2910 * Now allocate a register to hold that value and store
2913 s2
= new_stmt(cstate
, BPF_ST
);
2914 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2918 * Now move it into the X register.
2920 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2929 * Load a value relative to the beginning of the link-layer header after the 802.11
2930 * header, i.e. LLC_SNAP.
2931 * The link-layer header doesn't necessarily begin at the beginning
2932 * of the packet data; there might be a variable-length prefix containing
2933 * radio information.
2935 static struct slist
*
2936 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2939 struct slist
*sjset_data_frame_1
;
2940 struct slist
*sjset_data_frame_2
;
2941 struct slist
*sjset_qos
;
2942 struct slist
*sjset_radiotap_flags_present
;
2943 struct slist
*sjset_radiotap_ext_present
;
2944 struct slist
*sjset_radiotap_tsft_present
;
2945 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2946 struct slist
*s_roundup
;
2948 if (cstate
->off_linkpl
.reg
== -1) {
2950 * No register has been assigned to the offset of
2951 * the link-layer payload, which means nobody needs
2952 * it; don't bother computing it - just return
2953 * what we already have.
2959 * This code is not compatible with the optimizer, as
2960 * we are generating jmp instructions within a normal
2961 * slist of instructions
2963 cstate
->no_optimize
= 1;
2966 * If "s" is non-null, it has code to arrange that the X register
2967 * contains the length of the prefix preceding the link-layer
2970 * Otherwise, the length of the prefix preceding the link-layer
2971 * header is "off_outermostlinkhdr.constant_part".
2975 * There is no variable-length header preceding the
2976 * link-layer header.
2978 * Load the length of the fixed-length prefix preceding
2979 * the link-layer header (if any) into the X register,
2980 * and store it in the cstate->off_linkpl.reg register.
2981 * That length is off_outermostlinkhdr.constant_part.
2983 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2984 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2988 * The X register contains the offset of the beginning of the
2989 * link-layer header; add 24, which is the minimum length
2990 * of the MAC header for a data frame, to that, and store it
2991 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2992 * which is at the offset in the X register, with an indexed load.
2994 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2996 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2999 s2
= new_stmt(cstate
, BPF_ST
);
3000 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3003 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3008 * Check the Frame Control field to see if this is a data frame;
3009 * a data frame has the 0x08 bit (b3) in that field set and the
3010 * 0x04 bit (b2) clear.
3012 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3013 sjset_data_frame_1
->s
.k
= 0x08;
3014 sappend(s
, sjset_data_frame_1
);
3017 * If b3 is set, test b2, otherwise go to the first statement of
3018 * the rest of the program.
3020 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3021 sjset_data_frame_2
->s
.k
= 0x04;
3022 sappend(s
, sjset_data_frame_2
);
3023 sjset_data_frame_1
->s
.jf
= snext
;
3026 * If b2 is not set, this is a data frame; test the QoS bit.
3027 * Otherwise, go to the first statement of the rest of the
3030 sjset_data_frame_2
->s
.jt
= snext
;
3031 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3032 sjset_qos
->s
.k
= 0x80; /* QoS bit */
3033 sappend(s
, sjset_qos
);
3036 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3038 * Otherwise, go to the first statement of the rest of the
3041 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3042 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3044 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3047 s2
= new_stmt(cstate
, BPF_ST
);
3048 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3052 * If we have a radiotap header, look at it to see whether
3053 * there's Atheros padding between the MAC-layer header
3056 * Note: all of the fields in the radiotap header are
3057 * little-endian, so we byte-swap all of the values
3058 * we test against, as they will be loaded as big-endian
3061 * XXX - in the general case, we would have to scan through
3062 * *all* the presence bits, if there's more than one word of
3063 * presence bits. That would require a loop, meaning that
3064 * we wouldn't be able to run the filter in the kernel.
3066 * We assume here that the Atheros adapters that insert the
3067 * annoying padding don't have multiple antennae and therefore
3068 * do not generate radiotap headers with multiple presence words.
3070 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3072 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3073 * in the first presence flag word?
3075 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3079 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3080 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3081 sappend(s
, sjset_radiotap_flags_present
);
3084 * If not, skip all of this.
3086 sjset_radiotap_flags_present
->s
.jf
= snext
;
3089 * Otherwise, is the "extension" bit set in that word?
3091 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3092 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3093 sappend(s
, sjset_radiotap_ext_present
);
3094 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3097 * If so, skip all of this.
3099 sjset_radiotap_ext_present
->s
.jt
= snext
;
3102 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3104 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3105 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3106 sappend(s
, sjset_radiotap_tsft_present
);
3107 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3110 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3111 * at an offset of 16 from the beginning of the raw packet
3112 * data (8 bytes for the radiotap header and 8 bytes for
3115 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3118 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3121 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3123 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3124 sjset_tsft_datapad
->s
.k
= 0x20;
3125 sappend(s
, sjset_tsft_datapad
);
3128 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3129 * at an offset of 8 from the beginning of the raw packet
3130 * data (8 bytes for the radiotap header).
3132 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3135 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3138 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3140 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3141 sjset_notsft_datapad
->s
.k
= 0x20;
3142 sappend(s
, sjset_notsft_datapad
);
3145 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3146 * set, round the length of the 802.11 header to
3147 * a multiple of 4. Do that by adding 3 and then
3148 * dividing by and multiplying by 4, which we do by
3151 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3152 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3153 sappend(s
, s_roundup
);
3154 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3157 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3158 s2
->s
.k
= (bpf_u_int32
)~3;
3160 s2
= new_stmt(cstate
, BPF_ST
);
3161 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3164 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3165 sjset_tsft_datapad
->s
.jf
= snext
;
3166 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3167 sjset_notsft_datapad
->s
.jf
= snext
;
3169 sjset_qos
->s
.jf
= snext
;
3175 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3179 /* There is an implicit dependency between the link
3180 * payload and link header since the payload computation
3181 * includes the variable part of the header. Therefore,
3182 * if nobody else has allocated a register for the link
3183 * header and we need it, do it now. */
3184 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3185 cstate
->off_linkhdr
.reg
== -1)
3186 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3189 * For link-layer types that have a variable-length header
3190 * preceding the link-layer header, generate code to load
3191 * the offset of the link-layer header into the register
3192 * assigned to that offset, if any.
3194 * XXX - this, and the next switch statement, won't handle
3195 * encapsulation of 802.11 or 802.11+radio information in
3196 * some other protocol stack. That's significantly more
3199 switch (cstate
->outermostlinktype
) {
3201 case DLT_PRISM_HEADER
:
3202 s
= gen_load_prism_llprefixlen(cstate
);
3205 case DLT_IEEE802_11_RADIO_AVS
:
3206 s
= gen_load_avs_llprefixlen(cstate
);
3209 case DLT_IEEE802_11_RADIO
:
3210 s
= gen_load_radiotap_llprefixlen(cstate
);
3214 s
= gen_load_ppi_llprefixlen(cstate
);
3223 * For link-layer types that have a variable-length link-layer
3224 * header, generate code to load the offset of the link-layer
3225 * payload into the register assigned to that offset, if any.
3227 switch (cstate
->outermostlinktype
) {
3229 case DLT_IEEE802_11
:
3230 case DLT_PRISM_HEADER
:
3231 case DLT_IEEE802_11_RADIO_AVS
:
3232 case DLT_IEEE802_11_RADIO
:
3234 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3238 s
= gen_load_pflog_llprefixlen(cstate
);
3243 * If there is no initialization yet and we need variable
3244 * length offsets for VLAN, initialize them to zero
3246 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3249 if (cstate
->off_linkpl
.reg
== -1)
3250 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3251 if (cstate
->off_linktype
.reg
== -1)
3252 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3254 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3256 s2
= new_stmt(cstate
, BPF_ST
);
3257 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3259 s2
= new_stmt(cstate
, BPF_ST
);
3260 s2
->s
.k
= cstate
->off_linktype
.reg
;
3265 * If we have any offset-loading code, append all the
3266 * existing statements in the block to those statements,
3267 * and make the resulting list the list of statements
3271 sappend(s
, b
->stmts
);
3277 * Take an absolute offset, and:
3279 * if it has no variable part, return NULL;
3281 * if it has a variable part, generate code to load the register
3282 * containing that variable part into the X register, returning
3283 * a pointer to that code - if no register for that offset has
3284 * been allocated, allocate it first.
3286 * (The code to set that register will be generated later, but will
3287 * be placed earlier in the code sequence.)
3289 static struct slist
*
3290 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3294 if (off
->is_variable
) {
3295 if (off
->reg
== -1) {
3297 * We haven't yet assigned a register for the
3298 * variable part of the offset of the link-layer
3299 * header; allocate one.
3301 off
->reg
= alloc_reg(cstate
);
3305 * Load the register containing the variable part of the
3306 * offset of the link-layer header into the X register.
3308 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3313 * That offset isn't variable, there's no variable part,
3314 * so we don't need to generate any code.
3321 * Map an Ethernet type to the equivalent PPP type.
3324 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3332 case ETHERTYPE_IPV6
:
3333 ll_proto
= PPP_IPV6
;
3337 ll_proto
= PPP_DECNET
;
3340 case ETHERTYPE_ATALK
:
3341 ll_proto
= PPP_APPLE
;
3354 * I'm assuming the "Bridging PDU"s that go
3355 * over PPP are Spanning Tree Protocol
3358 ll_proto
= PPP_BRPDU
;
3369 * Generate any tests that, for encapsulation of a link-layer packet
3370 * inside another protocol stack, need to be done to check for those
3371 * link-layer packets (and that haven't already been done by a check
3372 * for that encapsulation).
3374 static struct block
*
3375 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3379 if (cstate
->is_encap
)
3380 return gen_encap_ll_check(cstate
);
3382 switch (cstate
->prevlinktype
) {
3386 * This is LANE-encapsulated Ethernet; check that the LANE
3387 * packet doesn't begin with an LE Control marker, i.e.
3388 * that it's data, not a control message.
3390 * (We've already generated a test for LANE.)
3392 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3398 * No such tests are necessary.
3406 * The three different values we should check for when checking for an
3407 * IPv6 packet with DLT_NULL.
3409 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3410 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3411 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3414 * Generate code to match a particular packet type by matching the
3415 * link-layer type field or fields in the 802.2 LLC header.
3417 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3418 * value, if <= ETHERMTU.
3420 static struct block
*
3421 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3423 struct block
*b0
, *b1
, *b2
;
3425 /* are we checking MPLS-encapsulated packets? */
3426 if (cstate
->label_stack_depth
> 0)
3427 return gen_mpls_linktype(cstate
, ll_proto
);
3429 switch (cstate
->linktype
) {
3432 case DLT_NETANALYZER
:
3433 case DLT_NETANALYZER_TRANSPARENT
:
3434 /* Geneve has an EtherType regardless of whether there is an
3435 * L2 header. VXLAN always has an EtherType. */
3436 if (!cstate
->is_encap
)
3437 b0
= gen_prevlinkhdr_check(cstate
);
3441 b1
= gen_ether_linktype(cstate
, ll_proto
);
3452 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3456 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3460 case DLT_IEEE802_11
:
3461 case DLT_PRISM_HEADER
:
3462 case DLT_IEEE802_11_RADIO_AVS
:
3463 case DLT_IEEE802_11_RADIO
:
3466 * Check that we have a data frame.
3468 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3469 IEEE80211_FC0_TYPE_DATA
,
3470 IEEE80211_FC0_TYPE_MASK
);
3473 * Now check for the specified link-layer type.
3475 b1
= gen_llc_linktype(cstate
, ll_proto
);
3482 * XXX - check for LLC frames.
3484 return gen_llc_linktype(cstate
, ll_proto
);
3489 * XXX - check for LLC PDUs, as per IEEE 802.5.
3491 return gen_llc_linktype(cstate
, ll_proto
);
3494 case DLT_ATM_RFC1483
:
3496 case DLT_IP_OVER_FC
:
3497 return gen_llc_linktype(cstate
, ll_proto
);
3502 * Check for an LLC-encapsulated version of this protocol;
3503 * if we were checking for LANE, linktype would no longer
3506 * Check for LLC encapsulation and then check the protocol.
3508 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3509 b1
= gen_llc_linktype(cstate
, ll_proto
);
3515 return gen_linux_sll_linktype(cstate
, ll_proto
);
3519 case DLT_SLIP_BSDOS
:
3522 * These types don't provide any type field; packets
3523 * are always IPv4 or IPv6.
3525 * XXX - for IPv4, check for a version number of 4, and,
3526 * for IPv6, check for a version number of 6?
3531 /* Check for a version number of 4. */
3532 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3534 case ETHERTYPE_IPV6
:
3535 /* Check for a version number of 6. */
3536 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3539 return gen_false(cstate
); /* always false */
3545 * Raw IPv4, so no type field.
3547 if (ll_proto
== ETHERTYPE_IP
)
3548 return gen_true(cstate
); /* always true */
3550 /* Checking for something other than IPv4; always false */
3551 return gen_false(cstate
);
3556 * Raw IPv6, so no type field.
3558 if (ll_proto
== ETHERTYPE_IPV6
)
3559 return gen_true(cstate
); /* always true */
3561 /* Checking for something other than IPv6; always false */
3562 return gen_false(cstate
);
3567 case DLT_PPP_SERIAL
:
3570 * We use Ethernet protocol types inside libpcap;
3571 * map them to the corresponding PPP protocol types.
3573 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3574 ethertype_to_ppptype(ll_proto
));
3579 * We use Ethernet protocol types inside libpcap;
3580 * map them to the corresponding PPP protocol types.
3586 * Also check for Van Jacobson-compressed IP.
3587 * XXX - do this for other forms of PPP?
3589 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3590 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3592 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3597 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3598 ethertype_to_ppptype(ll_proto
));
3608 return (gen_loopback_linktype(cstate
, AF_INET
));
3610 case ETHERTYPE_IPV6
:
3612 * AF_ values may, unfortunately, be platform-
3613 * dependent; AF_INET isn't, because everybody
3614 * used 4.2BSD's value, but AF_INET6 is, because
3615 * 4.2BSD didn't have a value for it (given that
3616 * IPv6 didn't exist back in the early 1980's),
3617 * and they all picked their own values.
3619 * This means that, if we're reading from a
3620 * savefile, we need to check for all the
3623 * If we're doing a live capture, we only need
3624 * to check for this platform's value; however,
3625 * Npcap uses 24, which isn't Windows's AF_INET6
3626 * value. (Given the multiple different values,
3627 * programs that read pcap files shouldn't be
3628 * checking for their platform's AF_INET6 value
3629 * anyway, they should check for all of the
3630 * possible values. and they might as well do
3631 * that even for live captures.)
3633 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3635 * Savefile - check for all three
3636 * possible IPv6 values.
3638 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3639 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3641 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3646 * Live capture, so we only need to
3647 * check for the value used on this
3652 * Npcap doesn't use Windows's AF_INET6,
3653 * as that collides with AF_IPX on
3654 * some BSDs (both have the value 23).
3655 * Instead, it uses 24.
3657 return (gen_loopback_linktype(cstate
, 24));
3660 return (gen_loopback_linktype(cstate
, AF_INET6
));
3661 #else /* AF_INET6 */
3663 * I guess this platform doesn't support
3664 * IPv6, so we just reject all packets.
3666 return gen_false(cstate
);
3667 #endif /* AF_INET6 */
3673 * Not a type on which we support filtering.
3674 * XXX - support those that have AF_ values
3675 * #defined on this platform, at least?
3677 return gen_false(cstate
);
3682 * af field is host byte order in contrast to the rest of
3685 if (ll_proto
== ETHERTYPE_IP
)
3686 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3688 else if (ll_proto
== ETHERTYPE_IPV6
)
3689 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3692 return gen_false(cstate
);
3696 case DLT_ARCNET_LINUX
:
3698 * XXX should we check for first fragment if the protocol
3704 return gen_false(cstate
);
3706 case ETHERTYPE_IPV6
:
3707 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3711 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3713 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3719 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3721 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3726 case ETHERTYPE_REVARP
:
3727 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3730 case ETHERTYPE_ATALK
:
3731 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3738 case ETHERTYPE_ATALK
:
3739 return gen_true(cstate
);
3741 return gen_false(cstate
);
3747 * XXX - assumes a 2-byte Frame Relay header with
3748 * DLCI and flags. What if the address is longer?
3754 * Check for the special NLPID for IP.
3756 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3758 case ETHERTYPE_IPV6
:
3760 * Check for the special NLPID for IPv6.
3762 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3766 * Check for several OSI protocols.
3768 * Frame Relay packets typically have an OSI
3769 * NLPID at the beginning; we check for each
3772 * What we check for is the NLPID and a frame
3773 * control field of UI, i.e. 0x03 followed
3776 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3777 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3778 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3784 return gen_false(cstate
);
3789 break; // not implemented
3791 case DLT_JUNIPER_MFR
:
3792 case DLT_JUNIPER_MLFR
:
3793 case DLT_JUNIPER_MLPPP
:
3794 case DLT_JUNIPER_ATM1
:
3795 case DLT_JUNIPER_ATM2
:
3796 case DLT_JUNIPER_PPPOE
:
3797 case DLT_JUNIPER_PPPOE_ATM
:
3798 case DLT_JUNIPER_GGSN
:
3799 case DLT_JUNIPER_ES
:
3800 case DLT_JUNIPER_MONITOR
:
3801 case DLT_JUNIPER_SERVICES
:
3802 case DLT_JUNIPER_ETHER
:
3803 case DLT_JUNIPER_PPP
:
3804 case DLT_JUNIPER_FRELAY
:
3805 case DLT_JUNIPER_CHDLC
:
3806 case DLT_JUNIPER_VP
:
3807 case DLT_JUNIPER_ST
:
3808 case DLT_JUNIPER_ISM
:
3809 case DLT_JUNIPER_VS
:
3810 case DLT_JUNIPER_SRX_E2E
:
3811 case DLT_JUNIPER_FIBRECHANNEL
:
3812 case DLT_JUNIPER_ATM_CEMIC
:
3814 /* just lets verify the magic number for now -
3815 * on ATM we may have up to 6 different encapsulations on the wire
3816 * and need a lot of heuristics to figure out that the payload
3819 * FIXME encapsulation specific BPF_ filters
3821 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3823 case DLT_BACNET_MS_TP
:
3824 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3827 return gen_ipnet_linktype(cstate
, ll_proto
);
3829 case DLT_LINUX_IRDA
:
3832 case DLT_MTP2_WITH_PHDR
:
3835 case DLT_LINUX_LAPD
:
3836 case DLT_USB_FREEBSD
:
3838 case DLT_USB_LINUX_MMAPPED
:
3840 case DLT_BLUETOOTH_HCI_H4
:
3841 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3843 case DLT_CAN_SOCKETCAN
:
3844 case DLT_IEEE802_15_4
:
3845 case DLT_IEEE802_15_4_LINUX
:
3846 case DLT_IEEE802_15_4_NONASK_PHY
:
3847 case DLT_IEEE802_15_4_NOFCS
:
3848 case DLT_IEEE802_15_4_TAP
:
3849 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3852 case DLT_IPMB_KONTRON
:
3856 /* Using the fixed-size NFLOG header it is possible to tell only
3857 * the address family of the packet, other meaningful data is
3858 * either missing or behind TLVs.
3860 break; // not implemented
3864 * Does this link-layer header type have a field
3865 * indicating the type of the next protocol? If
3866 * so, off_linktype.constant_part will be the offset of that
3867 * field in the packet; if not, it will be OFFSET_NOT_SET.
3869 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3871 * Yes; assume it's an Ethernet type. (If
3872 * it's not, it needs to be handled specially
3875 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3879 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
3880 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3884 * Check for an LLC SNAP packet with a given organization code and
3885 * protocol type; we check the entire contents of the 802.2 LLC and
3886 * snap headers, checking for DSAP and SSAP of SNAP and a control
3887 * field of 0x03 in the LLC header, and for the specified organization
3888 * code and protocol type in the SNAP header.
3890 static struct block
*
3891 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3893 u_char snapblock
[8];
3895 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3896 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3897 snapblock
[2] = 0x03; /* control = UI */
3898 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3899 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3900 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3901 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3902 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3903 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3907 * Generate code to match frames with an LLC header.
3909 static struct block
*
3910 gen_llc_internal(compiler_state_t
*cstate
)
3912 struct block
*b0
, *b1
;
3914 switch (cstate
->linktype
) {
3918 * We check for an Ethernet type field less or equal than
3919 * 1500, which means it's an 802.3 length field.
3921 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3924 * Now check for the purported DSAP and SSAP not being
3925 * 0xFF, to rule out NetWare-over-802.3.
3927 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3934 * We check for LLC traffic.
3936 b0
= gen_atmtype_llc(cstate
);
3939 case DLT_IEEE802
: /* Token Ring */
3941 * XXX - check for LLC frames.
3943 return gen_true(cstate
);
3947 * XXX - check for LLC frames.
3949 return gen_true(cstate
);
3951 case DLT_ATM_RFC1483
:
3953 * For LLC encapsulation, these are defined to have an
3956 * For VC encapsulation, they don't, but there's no
3957 * way to check for that; the protocol used on the VC
3958 * is negotiated out of band.
3960 return gen_true(cstate
);
3962 case DLT_IEEE802_11
:
3963 case DLT_PRISM_HEADER
:
3964 case DLT_IEEE802_11_RADIO
:
3965 case DLT_IEEE802_11_RADIO_AVS
:
3968 * Check that we have a data frame.
3970 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3971 IEEE80211_FC0_TYPE_DATA
,
3972 IEEE80211_FC0_TYPE_MASK
);
3975 fail_kw_on_dlt(cstate
, "llc");
3981 gen_llc(compiler_state_t
*cstate
)
3984 * Catch errors reported by us and routines below us, and return NULL
3987 if (setjmp(cstate
->top_ctx
))
3990 return gen_llc_internal(cstate
);
3994 gen_llc_i(compiler_state_t
*cstate
)
3996 struct block
*b0
, *b1
;
4000 * Catch errors reported by us and routines below us, and return NULL
4003 if (setjmp(cstate
->top_ctx
))
4007 * Check whether this is an LLC frame.
4009 b0
= gen_llc_internal(cstate
);
4012 * Load the control byte and test the low-order bit; it must
4013 * be clear for I frames.
4015 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4016 b1
= new_block(cstate
, JMP(BPF_JSET
));
4025 gen_llc_s(compiler_state_t
*cstate
)
4027 struct block
*b0
, *b1
;
4030 * Catch errors reported by us and routines below us, and return NULL
4033 if (setjmp(cstate
->top_ctx
))
4037 * Check whether this is an LLC frame.
4039 b0
= gen_llc_internal(cstate
);
4042 * Now compare the low-order 2 bit of the control byte against
4043 * the appropriate value for S frames.
4045 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4051 gen_llc_u(compiler_state_t
*cstate
)
4053 struct block
*b0
, *b1
;
4056 * Catch errors reported by us and routines below us, and return NULL
4059 if (setjmp(cstate
->top_ctx
))
4063 * Check whether this is an LLC frame.
4065 b0
= gen_llc_internal(cstate
);
4068 * Now compare the low-order 2 bit of the control byte against
4069 * the appropriate value for U frames.
4071 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4077 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4079 struct block
*b0
, *b1
;
4082 * Catch errors reported by us and routines below us, and return NULL
4085 if (setjmp(cstate
->top_ctx
))
4089 * Check whether this is an LLC frame.
4091 b0
= gen_llc_internal(cstate
);
4094 * Now check for an S frame with the appropriate type.
4096 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4102 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4104 struct block
*b0
, *b1
;
4107 * Catch errors reported by us and routines below us, and return NULL
4110 if (setjmp(cstate
->top_ctx
))
4114 * Check whether this is an LLC frame.
4116 b0
= gen_llc_internal(cstate
);
4119 * Now check for a U frame with the appropriate type.
4121 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4127 * Generate code to match a particular packet type, for link-layer types
4128 * using 802.2 LLC headers.
4130 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4131 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4133 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4134 * value, if <= ETHERMTU. We use that to determine whether to
4135 * match the DSAP or both DSAP and LSAP or to check the OUI and
4136 * protocol ID in a SNAP header.
4138 static struct block
*
4139 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4142 * XXX - handle token-ring variable-length header.
4148 case LLCSAP_NETBEUI
:
4150 * XXX - should we check both the DSAP and the
4151 * SSAP, like this, or should we check just the
4152 * DSAP, as we do for other SAP values?
4154 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4155 ((ll_proto
<< 8) | ll_proto
));
4159 * XXX - are there ever SNAP frames for IPX on
4160 * non-Ethernet 802.x networks?
4162 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4164 case ETHERTYPE_ATALK
:
4166 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4167 * SNAP packets with an organization code of
4168 * 0x080007 (Apple, for Appletalk) and a protocol
4169 * type of ETHERTYPE_ATALK (Appletalk).
4171 * XXX - check for an organization code of
4172 * encapsulated Ethernet as well?
4174 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4178 * XXX - we don't have to check for IPX 802.3
4179 * here, but should we check for the IPX Ethertype?
4181 if (ll_proto
<= ETHERMTU
) {
4183 * This is an LLC SAP value, so check
4186 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4189 * This is an Ethernet type; we assume that it's
4190 * unlikely that it'll appear in the right place
4191 * at random, and therefore check only the
4192 * location that would hold the Ethernet type
4193 * in a SNAP frame with an organization code of
4194 * 0x000000 (encapsulated Ethernet).
4196 * XXX - if we were to check for the SNAP DSAP and
4197 * LSAP, as per XXX, and were also to check for an
4198 * organization code of 0x000000 (encapsulated
4199 * Ethernet), we'd do
4201 * return gen_snap(cstate, 0x000000, ll_proto);
4203 * here; for now, we don't, as per the above.
4204 * I don't know whether it's worth the extra CPU
4205 * time to do the right check or not.
4207 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4212 static struct block
*
4213 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4214 int dir
, u_int src_off
, u_int dst_off
)
4216 struct block
*b0
, *b1
;
4230 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4231 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4237 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4238 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4248 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4255 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4259 static struct block
*
4260 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4261 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4263 struct block
*b0
, *b1
;
4266 * Code below needs to access four separate 32-bit parts of the 128-bit
4267 * IPv6 address and mask. In some OSes this is as simple as using the
4268 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4269 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4270 * far as libpcap sees it. Hence copy the data before use to avoid
4271 * potential unaligned memory access and the associated compiler
4272 * warnings (whether genuine or not).
4274 bpf_u_int32 a
[4], m
[4];
4287 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4288 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4294 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4295 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4305 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4312 /* this order is important */
4313 memcpy(a
, addr
, sizeof(a
));
4314 memcpy(m
, mask
, sizeof(m
));
4315 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4316 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4318 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4320 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4326 static struct block
*
4327 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4329 register struct block
*b0
, *b1
;
4333 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4336 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4339 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4340 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4346 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4347 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4357 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4365 * Like gen_ehostop, but for DLT_FDDI
4367 static struct block
*
4368 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4370 struct block
*b0
, *b1
;
4374 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4377 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4380 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4381 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4387 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4388 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4398 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4406 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4408 static struct block
*
4409 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4411 register struct block
*b0
, *b1
;
4415 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4418 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4421 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4422 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4428 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4429 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4439 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4447 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4448 * various 802.11 + radio headers.
4450 static struct block
*
4451 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4453 register struct block
*b0
, *b1
, *b2
;
4454 register struct slist
*s
;
4456 #ifdef ENABLE_WLAN_FILTERING_PATCH
4459 * We need to disable the optimizer because the optimizer is buggy
4460 * and wipes out some LD instructions generated by the below
4461 * code to validate the Frame Control bits
4463 cstate
->no_optimize
= 1;
4464 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4471 * For control frames, there is no SA.
4473 * For management frames, SA is at an
4474 * offset of 10 from the beginning of
4477 * For data frames, SA is at an offset
4478 * of 10 from the beginning of the packet
4479 * if From DS is clear, at an offset of
4480 * 16 from the beginning of the packet
4481 * if From DS is set and To DS is clear,
4482 * and an offset of 24 from the beginning
4483 * of the packet if From DS is set and To DS
4488 * Generate the tests to be done for data frames
4491 * First, check for To DS set, i.e. check "link[1] & 0x01".
4493 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4494 b1
= new_block(cstate
, JMP(BPF_JSET
));
4495 b1
->s
.k
= 0x01; /* To DS */
4499 * If To DS is set, the SA is at 24.
4501 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4505 * Now, check for To DS not set, i.e. check
4506 * "!(link[1] & 0x01)".
4508 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4509 b2
= new_block(cstate
, JMP(BPF_JSET
));
4510 b2
->s
.k
= 0x01; /* To DS */
4515 * If To DS is not set, the SA is at 16.
4517 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4521 * Now OR together the last two checks. That gives
4522 * the complete set of checks for data frames with
4528 * Now check for From DS being set, and AND that with
4529 * the ORed-together checks.
4531 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4532 b1
= new_block(cstate
, JMP(BPF_JSET
));
4533 b1
->s
.k
= 0x02; /* From DS */
4538 * Now check for data frames with From DS not set.
4540 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4541 b2
= new_block(cstate
, JMP(BPF_JSET
));
4542 b2
->s
.k
= 0x02; /* From DS */
4547 * If From DS isn't set, the SA is at 10.
4549 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4553 * Now OR together the checks for data frames with
4554 * From DS not set and for data frames with From DS
4555 * set; that gives the checks done for data frames.
4560 * Now check for a data frame.
4561 * I.e, check "link[0] & 0x08".
4563 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4564 b1
= new_block(cstate
, JMP(BPF_JSET
));
4569 * AND that with the checks done for data frames.
4574 * If the high-order bit of the type value is 0, this
4575 * is a management frame.
4576 * I.e, check "!(link[0] & 0x08)".
4578 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4579 b2
= new_block(cstate
, JMP(BPF_JSET
));
4585 * For management frames, the SA is at 10.
4587 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4591 * OR that with the checks done for data frames.
4592 * That gives the checks done for management and
4598 * If the low-order bit of the type value is 1,
4599 * this is either a control frame or a frame
4600 * with a reserved type, and thus not a
4603 * I.e., check "!(link[0] & 0x04)".
4605 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4606 b1
= new_block(cstate
, JMP(BPF_JSET
));
4612 * AND that with the checks for data and management
4622 * For control frames, there is no DA.
4624 * For management frames, DA is at an
4625 * offset of 4 from the beginning of
4628 * For data frames, DA is at an offset
4629 * of 4 from the beginning of the packet
4630 * if To DS is clear and at an offset of
4631 * 16 from the beginning of the packet
4636 * Generate the tests to be done for data frames.
4638 * First, check for To DS set, i.e. "link[1] & 0x01".
4640 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4641 b1
= new_block(cstate
, JMP(BPF_JSET
));
4642 b1
->s
.k
= 0x01; /* To DS */
4646 * If To DS is set, the DA is at 16.
4648 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4652 * Now, check for To DS not set, i.e. check
4653 * "!(link[1] & 0x01)".
4655 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4656 b2
= new_block(cstate
, JMP(BPF_JSET
));
4657 b2
->s
.k
= 0x01; /* To DS */
4662 * If To DS is not set, the DA is at 4.
4664 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4668 * Now OR together the last two checks. That gives
4669 * the complete set of checks for data frames.
4674 * Now check for a data frame.
4675 * I.e, check "link[0] & 0x08".
4677 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4678 b1
= new_block(cstate
, JMP(BPF_JSET
));
4683 * AND that with the checks done for data frames.
4688 * If the high-order bit of the type value is 0, this
4689 * is a management frame.
4690 * I.e, check "!(link[0] & 0x08)".
4692 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4693 b2
= new_block(cstate
, JMP(BPF_JSET
));
4699 * For management frames, the DA is at 4.
4701 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4705 * OR that with the checks done for data frames.
4706 * That gives the checks done for management and
4712 * If the low-order bit of the type value is 1,
4713 * this is either a control frame or a frame
4714 * with a reserved type, and thus not a
4717 * I.e., check "!(link[0] & 0x04)".
4719 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4720 b1
= new_block(cstate
, JMP(BPF_JSET
));
4726 * AND that with the checks for data and management
4733 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4734 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4740 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4741 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4746 * XXX - add BSSID keyword?
4749 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4753 * Not present in CTS or ACK control frames.
4755 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4756 IEEE80211_FC0_TYPE_MASK
);
4758 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4759 IEEE80211_FC0_SUBTYPE_MASK
);
4761 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4762 IEEE80211_FC0_SUBTYPE_MASK
);
4766 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4772 * Not present in control frames.
4774 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4775 IEEE80211_FC0_TYPE_MASK
);
4777 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4783 * Present only if the direction mask has both "From DS"
4784 * and "To DS" set. Neither control frames nor management
4785 * frames should have both of those set, so we don't
4786 * check the frame type.
4788 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4789 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4790 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4796 * Not present in management frames; addr1 in other
4801 * If the high-order bit of the type value is 0, this
4802 * is a management frame.
4803 * I.e, check "(link[0] & 0x08)".
4805 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4806 b1
= new_block(cstate
, JMP(BPF_JSET
));
4813 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4816 * AND that with the check of addr1.
4823 * Not present in management frames; addr2, if present,
4828 * Not present in CTS or ACK control frames.
4830 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4831 IEEE80211_FC0_TYPE_MASK
);
4833 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4834 IEEE80211_FC0_SUBTYPE_MASK
);
4836 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4837 IEEE80211_FC0_SUBTYPE_MASK
);
4843 * If the high-order bit of the type value is 0, this
4844 * is a management frame.
4845 * I.e, check "(link[0] & 0x08)".
4847 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4848 b1
= new_block(cstate
, JMP(BPF_JSET
));
4853 * AND that with the check for frames other than
4854 * CTS and ACK frames.
4861 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4870 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4871 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4872 * as the RFC states.)
4874 static struct block
*
4875 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4877 register struct block
*b0
, *b1
;
4881 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4884 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4887 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4888 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4894 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4895 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4905 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4913 * This is quite tricky because there may be pad bytes in front of the
4914 * DECNET header, and then there are two possible data packet formats that
4915 * carry both src and dst addresses, plus 5 packet types in a format that
4916 * carries only the src node, plus 2 types that use a different format and
4917 * also carry just the src node.
4921 * Instead of doing those all right, we just look for data packets with
4922 * 0 or 1 bytes of padding. If you want to look at other packets, that
4923 * will require a lot more hacking.
4925 * To add support for filtering on DECNET "areas" (network numbers)
4926 * one would want to add a "mask" argument to this routine. That would
4927 * make the filter even more inefficient, although one could be clever
4928 * and not generate masking instructions if the mask is 0xFFFF.
4930 static struct block
*
4931 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4933 struct block
*b0
, *b1
, *b2
, *tmp
;
4934 u_int offset_lh
; /* offset if long header is received */
4935 u_int offset_sh
; /* offset if short header is received */
4940 offset_sh
= 1; /* follows flags */
4941 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4945 offset_sh
= 3; /* follows flags, dstnode */
4946 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4950 /* Inefficient because we do our Calvinball dance twice */
4951 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4952 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4958 /* Inefficient because we do our Calvinball dance twice */
4959 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4960 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4970 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4978 * In a DECnet message inside an Ethernet frame the first two bytes
4979 * immediately after EtherType are the [litle-endian] DECnet message
4980 * length, which is irrelevant in this context.
4982 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
4983 * 8-bit bitmap of the optional padding before the packet route header.
4984 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
4985 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
4986 * means there aren't any PAD bytes after the bitmap, so the header
4987 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
4988 * is set to 0, thus the header begins at the third byte.
4990 * The header can be in several (as mentioned above) formats, all of
4991 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
4992 * (PF, "pad field") set to 0 regardless of any padding present before
4993 * the header. "Short header" means bits 0-2 of the bitmap encode the
4994 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
4996 * To test PLENGTH and FLAGS, use multiple-byte constants with the
4997 * values and the masks, this maps to the required single bytes of
4998 * the message correctly on both big-endian and little-endian hosts.
4999 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5000 * because the wire encoding is little-endian and BPF multiple-byte
5001 * loads are big-endian. When the destination address is near enough
5002 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5005 /* Check for pad = 1, long header case */
5006 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5007 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5008 BPF_H
, SWAPSHORT(addr
));
5010 /* Check for pad = 0, long header case */
5011 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5012 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5016 /* Check for pad = 1, short header case */
5018 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5019 0x81020000U
| SWAPSHORT(addr
),
5022 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5023 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5028 /* Check for pad = 0, short header case */
5030 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5031 0x02000000U
| SWAPSHORT(addr
) << 8,
5034 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5035 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5045 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5046 * test the bottom-of-stack bit, and then check the version number
5047 * field in the IP header.
5049 static struct block
*
5050 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5052 struct block
*b0
, *b1
;
5057 /* match the bottom-of-stack bit */
5058 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5059 /* match the IPv4 version number */
5060 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5064 case ETHERTYPE_IPV6
:
5065 /* match the bottom-of-stack bit */
5066 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5067 /* match the IPv4 version number */
5068 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5073 /* FIXME add other L3 proto IDs */
5074 bpf_error(cstate
, "unsupported protocol over mpls");
5079 static struct block
*
5080 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5081 int proto
, int dir
, int type
)
5083 struct block
*b0
, *b1
;
5088 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5090 * Only check for non-IPv4 addresses if we're not
5091 * checking MPLS-encapsulated packets.
5093 if (cstate
->label_stack_depth
== 0) {
5094 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5096 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5102 break; // invalid qualifier
5105 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5106 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5111 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5112 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5117 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5118 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5129 break; // invalid qualifier
5132 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5133 b1
= gen_dnhostop(cstate
, addr
, dir
);
5164 break; // invalid qualifier
5169 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5170 type
== Q_NET
? "ip net" : "ip host");
5175 static struct block
*
5176 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5177 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5179 struct block
*b0
, *b1
;
5185 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5186 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5228 break; // invalid qualifier
5233 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5234 type
== Q_NET
? "ip6 net" : "ip6 host");
5240 static struct block
*
5241 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5242 struct addrinfo
*alist
, int proto
, int dir
)
5244 struct block
*b0
, *b1
, *tmp
;
5245 struct addrinfo
*ai
;
5246 struct sockaddr_in
*sin
;
5249 bpf_error(cstate
, "direction applied to 'gateway'");
5256 switch (cstate
->linktype
) {
5258 case DLT_NETANALYZER
:
5259 case DLT_NETANALYZER_TRANSPARENT
:
5260 b1
= gen_prevlinkhdr_check(cstate
);
5261 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5266 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5269 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5271 case DLT_IEEE802_11
:
5272 case DLT_PRISM_HEADER
:
5273 case DLT_IEEE802_11_RADIO_AVS
:
5274 case DLT_IEEE802_11_RADIO
:
5276 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5278 case DLT_IP_OVER_FC
:
5279 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5283 * This is LLC-multiplexed traffic; if it were
5284 * LANE, cstate->linktype would have been set to
5290 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5293 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5295 * Does it have an address?
5297 if (ai
->ai_addr
!= NULL
) {
5299 * Yes. Is it an IPv4 address?
5301 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5303 * Generate an entry for it.
5305 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5306 tmp
= gen_host(cstate
,
5307 ntohl(sin
->sin_addr
.s_addr
),
5308 0xffffffff, proto
, Q_OR
, Q_HOST
);
5310 * Is it the *first* IPv4 address?
5314 * Yes, so start with it.
5319 * No, so OR it into the
5331 * No IPv4 addresses found.
5339 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5344 static struct block
*
5345 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5348 struct block
*b1
= NULL
;
5353 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5357 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5361 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5365 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5368 #ifndef IPPROTO_IGMP
5369 #define IPPROTO_IGMP 2
5373 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5376 #ifndef IPPROTO_IGRP
5377 #define IPPROTO_IGRP 9
5380 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5384 #define IPPROTO_PIM 103
5388 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5391 #ifndef IPPROTO_VRRP
5392 #define IPPROTO_VRRP 112
5396 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5399 #ifndef IPPROTO_CARP
5400 #define IPPROTO_CARP 112
5404 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5408 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5412 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5416 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5420 break; // invalid syntax
5423 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5427 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5431 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5435 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5439 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5443 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5447 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5451 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5454 #ifndef IPPROTO_ICMPV6
5455 #define IPPROTO_ICMPV6 58
5458 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5462 #define IPPROTO_AH 51
5465 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5469 #define IPPROTO_ESP 50
5472 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5476 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5480 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5484 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5487 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5488 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5489 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5491 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5493 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5495 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5499 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5500 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5501 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5503 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5505 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5507 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5511 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5512 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5513 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5515 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5520 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5521 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5526 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5527 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5529 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5531 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5536 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5537 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5542 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5543 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5548 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5552 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5556 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5560 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5564 break; // invalid syntax
5571 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5575 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5578 * Catch errors reported by us and routines below us, and return NULL
5581 if (setjmp(cstate
->top_ctx
))
5584 return gen_proto_abbrev_internal(cstate
, proto
);
5587 static struct block
*
5588 gen_ipfrag(compiler_state_t
*cstate
)
5593 /* not IPv4 frag other than the first frag */
5594 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5595 b
= new_block(cstate
, JMP(BPF_JSET
));
5604 * Generate a comparison to a port value in the transport-layer header
5605 * at the specified offset from the beginning of that header.
5607 * XXX - this handles a variable-length prefix preceding the link-layer
5608 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5609 * variable-length link-layer headers (such as Token Ring or 802.11
5612 static struct block
*
5613 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5615 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5618 static struct block
*
5619 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5621 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5624 static struct block
*
5625 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5627 struct block
*b0
, *b1
, *tmp
;
5629 /* ip proto 'proto' and not a fragment other than the first fragment */
5630 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5631 b0
= gen_ipfrag(cstate
);
5636 b1
= gen_portatom(cstate
, 0, port
);
5640 b1
= gen_portatom(cstate
, 2, port
);
5644 tmp
= gen_portatom(cstate
, 0, port
);
5645 b1
= gen_portatom(cstate
, 2, port
);
5651 tmp
= gen_portatom(cstate
, 0, port
);
5652 b1
= gen_portatom(cstate
, 2, port
);
5662 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5674 static struct block
*
5675 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5677 struct block
*b0
, *b1
, *tmp
;
5682 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5683 * not LLC encapsulation with LLCSAP_IP.
5685 * For IEEE 802 networks - which includes 802.5 token ring
5686 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5687 * says that SNAP encapsulation is used, not LLC encapsulation
5690 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5691 * RFC 2225 say that SNAP encapsulation is used, not LLC
5692 * encapsulation with LLCSAP_IP.
5694 * So we always check for ETHERTYPE_IP.
5696 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5702 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5706 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5707 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5709 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5721 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5723 struct block
*b0
, *b1
, *tmp
;
5725 /* ip6 proto 'proto' */
5726 /* XXX - catch the first fragment of a fragmented packet? */
5727 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5731 b1
= gen_portatom6(cstate
, 0, port
);
5735 b1
= gen_portatom6(cstate
, 2, port
);
5739 tmp
= gen_portatom6(cstate
, 0, port
);
5740 b1
= gen_portatom6(cstate
, 2, port
);
5746 tmp
= gen_portatom6(cstate
, 0, port
);
5747 b1
= gen_portatom6(cstate
, 2, port
);
5759 static struct block
*
5760 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5762 struct block
*b0
, *b1
, *tmp
;
5764 /* link proto ip6 */
5765 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5771 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5775 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5776 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5778 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5789 /* gen_portrange code */
5790 static struct block
*
5791 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5794 struct block
*b1
, *b2
;
5798 * Reverse the order of the ports, so v1 is the lower one.
5807 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5808 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5815 static struct block
*
5816 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5817 bpf_u_int32 proto
, int dir
)
5819 struct block
*b0
, *b1
, *tmp
;
5821 /* ip proto 'proto' and not a fragment other than the first fragment */
5822 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5823 b0
= gen_ipfrag(cstate
);
5828 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5832 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5836 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5837 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5843 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5844 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5854 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5866 static struct block
*
5867 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5870 struct block
*b0
, *b1
, *tmp
;
5873 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5879 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5884 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5885 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5887 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5898 static struct block
*
5899 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5902 struct block
*b1
, *b2
;
5906 * Reverse the order of the ports, so v1 is the lower one.
5915 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5916 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5923 static struct block
*
5924 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5925 bpf_u_int32 proto
, int dir
)
5927 struct block
*b0
, *b1
, *tmp
;
5929 /* ip6 proto 'proto' */
5930 /* XXX - catch the first fragment of a fragmented packet? */
5931 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5935 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5939 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5943 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5944 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5950 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5951 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5963 static struct block
*
5964 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5967 struct block
*b0
, *b1
, *tmp
;
5969 /* link proto ip6 */
5970 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5976 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5981 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5982 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5984 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5996 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6005 v
= pcap_nametoproto(name
);
6006 if (v
== PROTO_UNDEF
)
6007 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6011 /* XXX should look up h/w protocol type based on cstate->linktype */
6012 v
= pcap_nametoeproto(name
);
6013 if (v
== PROTO_UNDEF
) {
6014 v
= pcap_nametollc(name
);
6015 if (v
== PROTO_UNDEF
)
6016 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6021 if (strcmp(name
, "esis") == 0)
6023 else if (strcmp(name
, "isis") == 0)
6025 else if (strcmp(name
, "clnp") == 0)
6028 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6038 #if !defined(NO_PROTOCHAIN)
6039 static struct block
*
6040 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6042 struct block
*b0
, *b
;
6043 struct slist
*s
[100];
6044 int fix2
, fix3
, fix4
, fix5
;
6045 int ahcheck
, again
, end
;
6047 int reg2
= alloc_reg(cstate
);
6049 memset(s
, 0, sizeof(s
));
6050 fix3
= fix4
= fix5
= 0;
6057 b0
= gen_protochain(cstate
, v
, Q_IP
);
6058 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6062 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
6067 * We don't handle variable-length prefixes before the link-layer
6068 * header, or variable-length link-layer headers, here yet.
6069 * We might want to add BPF instructions to do the protochain
6070 * work, to simplify that and, on platforms that have a BPF
6071 * interpreter with the new instructions, let the filtering
6072 * be done in the kernel. (We already require a modified BPF
6073 * engine to do the protochain stuff, to support backward
6074 * branches, and backward branch support is unlikely to appear
6075 * in kernel BPF engines.)
6077 if (cstate
->off_linkpl
.is_variable
)
6078 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6081 * To quote a comment in optimize.c:
6083 * "These data structures are used in a Cocke and Schwartz style
6084 * value numbering scheme. Since the flowgraph is acyclic,
6085 * exit values can be propagated from a node's predecessors
6086 * provided it is uniquely defined."
6088 * "Acyclic" means "no backward branches", which means "no
6089 * loops", so we have to turn the optimizer off.
6091 cstate
->no_optimize
= 1;
6094 * s[0] is a dummy entry to protect other BPF insn from damage
6095 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6096 * hard to find interdependency made by jump table fixup.
6099 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6104 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6107 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6108 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6110 /* X = ip->ip_hl << 2 */
6111 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6112 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6117 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6119 /* A = ip6->ip_nxt */
6120 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6121 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6123 /* X = sizeof(struct ip6_hdr) */
6124 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6130 bpf_error(cstate
, "unsupported proto to gen_protochain");
6134 /* again: if (A == v) goto end; else fall through; */
6136 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6138 s
[i
]->s
.jt
= NULL
; /*later*/
6139 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6143 #ifndef IPPROTO_NONE
6144 #define IPPROTO_NONE 59
6146 /* if (A == IPPROTO_NONE) goto end */
6147 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6148 s
[i
]->s
.jt
= NULL
; /*later*/
6149 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6150 s
[i
]->s
.k
= IPPROTO_NONE
;
6151 s
[fix5
]->s
.jf
= s
[i
];
6155 if (proto
== Q_IPV6
) {
6156 int v6start
, v6end
, v6advance
, j
;
6159 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6160 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6161 s
[i
]->s
.jt
= NULL
; /*later*/
6162 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6163 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6164 s
[fix2
]->s
.jf
= s
[i
];
6166 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6167 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6168 s
[i
]->s
.jt
= NULL
; /*later*/
6169 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6170 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6172 /* if (A == IPPROTO_ROUTING) goto v6advance */
6173 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6174 s
[i
]->s
.jt
= NULL
; /*later*/
6175 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6176 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6178 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6179 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6180 s
[i
]->s
.jt
= NULL
; /*later*/
6181 s
[i
]->s
.jf
= NULL
; /*later*/
6182 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6192 * A = P[X + packet head];
6193 * X = X + (P[X + packet head + 1] + 1) * 8;
6195 /* A = P[X + packet head] */
6196 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6197 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6200 s
[i
] = new_stmt(cstate
, BPF_ST
);
6203 /* A = P[X + packet head + 1]; */
6204 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6205 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6208 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6212 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6216 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6220 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6223 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6227 /* goto again; (must use BPF_JA for backward jump) */
6228 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6229 s
[i
]->s
.k
= again
- i
- 1;
6230 s
[i
- 1]->s
.jf
= s
[i
];
6234 for (j
= v6start
; j
<= v6end
; j
++)
6235 s
[j
]->s
.jt
= s
[v6advance
];
6238 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6240 s
[fix2
]->s
.jf
= s
[i
];
6246 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6247 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6248 s
[i
]->s
.jt
= NULL
; /*later*/
6249 s
[i
]->s
.jf
= NULL
; /*later*/
6250 s
[i
]->s
.k
= IPPROTO_AH
;
6252 s
[fix3
]->s
.jf
= s
[ahcheck
];
6259 * X = X + (P[X + 1] + 2) * 4;
6262 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6264 /* A = P[X + packet head]; */
6265 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6266 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6269 s
[i
] = new_stmt(cstate
, BPF_ST
);
6273 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6276 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6280 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6282 /* A = P[X + packet head] */
6283 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6284 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6287 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6291 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6295 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6298 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6302 /* goto again; (must use BPF_JA for backward jump) */
6303 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6304 s
[i
]->s
.k
= again
- i
- 1;
6309 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6311 s
[fix2
]->s
.jt
= s
[end
];
6312 s
[fix4
]->s
.jf
= s
[end
];
6313 s
[fix5
]->s
.jt
= s
[end
];
6320 for (i
= 0; i
< max
- 1; i
++)
6321 s
[i
]->next
= s
[i
+ 1];
6322 s
[max
- 1]->next
= NULL
;
6327 b
= new_block(cstate
, JMP(BPF_JEQ
));
6328 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6331 free_reg(cstate
, reg2
);
6336 #endif /* !defined(NO_PROTOCHAIN) */
6339 * Generate code that checks whether the packet is a packet for protocol
6340 * <proto> and whether the type field in that protocol's header has
6341 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6342 * IP packet and checks the protocol number in the IP header against <v>.
6344 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6345 * against Q_IP and Q_IPV6.
6347 static struct block
*
6348 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6350 struct block
*b0
, *b1
;
6353 if (dir
!= Q_DEFAULT
)
6354 bpf_error(cstate
, "direction applied to 'proto'");
6358 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6359 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6364 return gen_linktype(cstate
, v
);
6368 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6369 * not LLC encapsulation with LLCSAP_IP.
6371 * For IEEE 802 networks - which includes 802.5 token ring
6372 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6373 * says that SNAP encapsulation is used, not LLC encapsulation
6376 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6377 * RFC 2225 say that SNAP encapsulation is used, not LLC
6378 * encapsulation with LLCSAP_IP.
6380 * So we always check for ETHERTYPE_IP.
6382 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6383 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6401 break; // invalid qualifier
6404 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6406 * Also check for a fragment header before the final
6409 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6410 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6412 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6423 break; // invalid qualifier
6426 switch (cstate
->linktype
) {
6430 * Frame Relay packets typically have an OSI
6431 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6432 * generates code to check for all the OSI
6433 * NLPIDs, so calling it and then adding a check
6434 * for the particular NLPID for which we're
6435 * looking is bogus, as we can just check for
6438 * What we check for is the NLPID and a frame
6439 * control field value of UI, i.e. 0x03 followed
6442 * XXX - assumes a 2-byte Frame Relay header with
6443 * DLCI and flags. What if the address is longer?
6445 * XXX - what about SNAP-encapsulated frames?
6447 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6453 * Cisco uses an Ethertype lookalike - for OSI,
6456 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6457 /* OSI in C-HDLC is stuffed with a fudge byte */
6458 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6463 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6464 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6470 break; // invalid qualifier
6473 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6475 * 4 is the offset of the PDU type relative to the IS-IS
6478 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6495 break; // invalid qualifier
6501 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6506 * Convert a non-numeric name to a port number.
6509 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6511 struct addrinfo hints
, *res
, *ai
;
6513 struct sockaddr_in
*in4
;
6515 struct sockaddr_in6
*in6
;
6520 * We check for both TCP and UDP in case there are
6521 * ambiguous entries.
6523 memset(&hints
, 0, sizeof(hints
));
6524 hints
.ai_family
= PF_UNSPEC
;
6525 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6526 hints
.ai_protocol
= ipproto
;
6527 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6534 * No such port. Just return -1.
6541 * We don't use strerror() because it's not
6542 * guaranteed to be thread-safe on all platforms
6543 * (probably because it might use a non-thread-local
6544 * buffer into which to format an error message
6545 * if the error code isn't one for which it has
6546 * a canned string; three cheers for C string
6549 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6551 port
= -2; /* a real error */
6557 * This is a real error, not just "there's
6558 * no such service name".
6560 * We don't use gai_strerror() because it's not
6561 * guaranteed to be thread-safe on all platforms
6562 * (probably because it might use a non-thread-local
6563 * buffer into which to format an error message
6564 * if the error code isn't one for which it has
6565 * a canned string; three cheers for C string
6568 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6570 port
= -2; /* a real error */
6575 * OK, we found it. Did it find anything?
6577 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6579 * Does it have an address?
6581 if (ai
->ai_addr
!= NULL
) {
6583 * Yes. Get a port number; we're done.
6585 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6586 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6587 port
= ntohs(in4
->sin_port
);
6591 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6592 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6593 port
= ntohs(in6
->sin6_port
);
6605 * Convert a string to a port number.
6608 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6618 * See if it's a number.
6620 ret
= stoulen(string
, string_size
, &val
, cstate
);
6624 /* Unknown port type - it's just a number. */
6625 *proto
= PROTO_UNDEF
;
6628 case STOULEN_NOT_OCTAL_NUMBER
:
6629 case STOULEN_NOT_HEX_NUMBER
:
6630 case STOULEN_NOT_DECIMAL_NUMBER
:
6632 * Not a valid number; try looking it up as a port.
6634 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6635 memcpy(cpy
, string
, string_size
);
6636 cpy
[string_size
] = '\0';
6637 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6638 if (tcp_port
== -2) {
6640 * We got a hard error; the error string has
6644 longjmp(cstate
->top_ctx
, 1);
6647 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6648 if (udp_port
== -2) {
6650 * We got a hard error; the error string has
6654 longjmp(cstate
->top_ctx
, 1);
6659 * We need to check /etc/services for ambiguous entries.
6660 * If we find an ambiguous entry, and it has the
6661 * same port number, change the proto to PROTO_UNDEF
6662 * so both TCP and UDP will be checked.
6664 if (tcp_port
>= 0) {
6665 val
= (bpf_u_int32
)tcp_port
;
6666 *proto
= IPPROTO_TCP
;
6667 if (udp_port
>= 0) {
6668 if (udp_port
== tcp_port
)
6669 *proto
= PROTO_UNDEF
;
6672 /* Can't handle ambiguous names that refer
6673 to different port numbers. */
6674 warning("ambiguous port %s in /etc/services",
6681 if (udp_port
>= 0) {
6682 val
= (bpf_u_int32
)udp_port
;
6683 *proto
= IPPROTO_UDP
;
6687 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6689 longjmp(cstate
->top_ctx
, 1);
6696 /* Error already set. */
6697 longjmp(cstate
->top_ctx
, 1);
6704 /* Should not happen */
6705 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6706 longjmp(cstate
->top_ctx
, 1);
6713 * Convert a string in the form PPP-PPP, which correspond to ports, to
6714 * a starting and ending port in a port range.
6717 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6718 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6721 const char *first
, *second
;
6722 size_t first_size
, second_size
;
6725 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6726 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6729 * Make sure there are no other hyphens.
6731 * XXX - we support named ports, but there are some port names
6732 * in /etc/services that include hyphens, so this would rule
6735 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6736 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6740 * Get the length of the first port.
6743 first_size
= hyphen_off
- string
;
6744 if (first_size
== 0) {
6745 /* Range of "-port", which we don't support. */
6746 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6750 * Try to convert it to a port.
6752 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6753 save_proto
= *proto
;
6756 * Get the length of the second port.
6758 second
= hyphen_off
+ 1;
6759 second_size
= strlen(second
);
6760 if (second_size
== 0) {
6761 /* Range of "port-", which we don't support. */
6762 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6766 * Try to convert it to a port.
6768 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6769 if (*proto
!= save_proto
)
6770 *proto
= PROTO_UNDEF
;
6774 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6776 int proto
= q
.proto
;
6780 bpf_u_int32 mask
, addr
;
6781 struct addrinfo
*res
, *res0
;
6782 struct sockaddr_in
*sin4
;
6785 struct sockaddr_in6
*sin6
;
6786 struct in6_addr mask128
;
6788 struct block
*b
, *tmp
;
6789 int port
, real_proto
;
6790 bpf_u_int32 port1
, port2
;
6793 * Catch errors reported by us and routines below us, and return NULL
6796 if (setjmp(cstate
->top_ctx
))
6802 addr
= pcap_nametonetaddr(name
);
6804 bpf_error(cstate
, "unknown network '%s'", name
);
6805 /* Left justify network addr and calculate its network mask */
6807 while (addr
&& (addr
& 0xff000000) == 0) {
6811 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6815 if (proto
== Q_LINK
) {
6816 switch (cstate
->linktype
) {
6819 case DLT_NETANALYZER
:
6820 case DLT_NETANALYZER_TRANSPARENT
:
6821 eaddr
= pcap_ether_hostton(name
);
6824 "unknown ether host '%s'", name
);
6825 tmp
= gen_prevlinkhdr_check(cstate
);
6826 b
= gen_ehostop(cstate
, eaddr
, dir
);
6833 eaddr
= pcap_ether_hostton(name
);
6836 "unknown FDDI host '%s'", name
);
6837 b
= gen_fhostop(cstate
, eaddr
, dir
);
6842 eaddr
= pcap_ether_hostton(name
);
6845 "unknown token ring host '%s'", name
);
6846 b
= gen_thostop(cstate
, eaddr
, dir
);
6850 case DLT_IEEE802_11
:
6851 case DLT_PRISM_HEADER
:
6852 case DLT_IEEE802_11_RADIO_AVS
:
6853 case DLT_IEEE802_11_RADIO
:
6855 eaddr
= pcap_ether_hostton(name
);
6858 "unknown 802.11 host '%s'", name
);
6859 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6863 case DLT_IP_OVER_FC
:
6864 eaddr
= pcap_ether_hostton(name
);
6867 "unknown Fibre Channel host '%s'", name
);
6868 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6873 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6874 } else if (proto
== Q_DECNET
) {
6876 * A long time ago on Ultrix libpcap supported
6877 * translation of DECnet host names into DECnet
6878 * addresses, but this feature is history now.
6880 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6883 memset(&mask128
, 0xff, sizeof(mask128
));
6885 res0
= res
= pcap_nametoaddrinfo(name
);
6887 bpf_error(cstate
, "unknown host '%s'", name
);
6894 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6895 tproto
== Q_DEFAULT
) {
6901 for (res
= res0
; res
; res
= res
->ai_next
) {
6902 switch (res
->ai_family
) {
6905 if (tproto
== Q_IPV6
)
6909 sin4
= (struct sockaddr_in
*)
6911 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6912 0xffffffff, tproto
, dir
, q
.addr
);
6916 if (tproto6
== Q_IP
)
6919 sin6
= (struct sockaddr_in6
*)
6921 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6922 &mask128
, tproto6
, dir
, q
.addr
);
6935 bpf_error(cstate
, "unknown host '%s'%s", name
,
6936 (proto
== Q_DEFAULT
)
6938 : " for specified address family");
6944 if (proto
!= Q_DEFAULT
&&
6945 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6946 bpf_error(cstate
, "illegal qualifier of 'port'");
6947 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6948 bpf_error(cstate
, "unknown port '%s'", name
);
6949 if (proto
== Q_UDP
) {
6950 if (real_proto
== IPPROTO_TCP
)
6951 bpf_error(cstate
, "port '%s' is tcp", name
);
6952 else if (real_proto
== IPPROTO_SCTP
)
6953 bpf_error(cstate
, "port '%s' is sctp", name
);
6955 /* override PROTO_UNDEF */
6956 real_proto
= IPPROTO_UDP
;
6958 if (proto
== Q_TCP
) {
6959 if (real_proto
== IPPROTO_UDP
)
6960 bpf_error(cstate
, "port '%s' is udp", name
);
6962 else if (real_proto
== IPPROTO_SCTP
)
6963 bpf_error(cstate
, "port '%s' is sctp", name
);
6965 /* override PROTO_UNDEF */
6966 real_proto
= IPPROTO_TCP
;
6968 if (proto
== Q_SCTP
) {
6969 if (real_proto
== IPPROTO_UDP
)
6970 bpf_error(cstate
, "port '%s' is udp", name
);
6972 else if (real_proto
== IPPROTO_TCP
)
6973 bpf_error(cstate
, "port '%s' is tcp", name
);
6975 /* override PROTO_UNDEF */
6976 real_proto
= IPPROTO_SCTP
;
6979 bpf_error(cstate
, "illegal port number %d < 0", port
);
6981 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6982 b
= gen_port(cstate
, port
, real_proto
, dir
);
6983 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6987 if (proto
!= Q_DEFAULT
&&
6988 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6989 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6990 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
6991 if (proto
== Q_UDP
) {
6992 if (real_proto
== IPPROTO_TCP
)
6993 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6994 else if (real_proto
== IPPROTO_SCTP
)
6995 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6997 /* override PROTO_UNDEF */
6998 real_proto
= IPPROTO_UDP
;
7000 if (proto
== Q_TCP
) {
7001 if (real_proto
== IPPROTO_UDP
)
7002 bpf_error(cstate
, "port in range '%s' is udp", name
);
7003 else if (real_proto
== IPPROTO_SCTP
)
7004 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7006 /* override PROTO_UNDEF */
7007 real_proto
= IPPROTO_TCP
;
7009 if (proto
== Q_SCTP
) {
7010 if (real_proto
== IPPROTO_UDP
)
7011 bpf_error(cstate
, "port in range '%s' is udp", name
);
7012 else if (real_proto
== IPPROTO_TCP
)
7013 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7015 /* override PROTO_UNDEF */
7016 real_proto
= IPPROTO_SCTP
;
7019 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7021 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7023 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7024 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7029 eaddr
= pcap_ether_hostton(name
);
7031 bpf_error(cstate
, "unknown ether host: %s", name
);
7033 res
= pcap_nametoaddrinfo(name
);
7036 bpf_error(cstate
, "unknown host '%s'", name
);
7037 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7042 bpf_error(cstate
, "unknown host '%s'", name
);
7045 bpf_error(cstate
, "'gateway' not supported in this configuration");
7049 real_proto
= lookup_proto(cstate
, name
, proto
);
7050 if (real_proto
>= 0)
7051 return gen_proto(cstate
, real_proto
, proto
, dir
);
7053 bpf_error(cstate
, "unknown protocol: %s", name
);
7055 #if !defined(NO_PROTOCHAIN)
7057 real_proto
= lookup_proto(cstate
, name
, proto
);
7058 if (real_proto
>= 0)
7059 return gen_protochain(cstate
, real_proto
, proto
);
7061 bpf_error(cstate
, "unknown protocol: %s", name
);
7062 #endif /* !defined(NO_PROTOCHAIN) */
7073 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7074 bpf_u_int32 masklen
, struct qual q
)
7076 register int nlen
, mlen
;
7081 * Catch errors reported by us and routines below us, and return NULL
7084 if (setjmp(cstate
->top_ctx
))
7087 nlen
= pcapint_atoin(s1
, &n
);
7089 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7090 /* Promote short ipaddr */
7094 mlen
= pcapint_atoin(s2
, &m
);
7096 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7097 /* Promote short ipaddr */
7100 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7103 /* Convert mask len to mask */
7105 bpf_error(cstate
, "mask length must be <= 32");
7106 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7107 m
= (bpf_u_int32
)m64
;
7109 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7116 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7119 bpf_error(cstate
, "Mask syntax for networks only");
7126 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7134 * Catch errors reported by us and routines below us, and return NULL
7137 if (setjmp(cstate
->top_ctx
))
7144 * v contains a 32-bit unsigned parsed from a string of the
7145 * form {N}, which could be decimal, hexadecimal or octal.
7146 * Although it would be possible to use the value as a raw
7147 * 16-bit DECnet address when the value fits into 16 bits, this
7148 * would be a questionable feature: DECnet address wire
7149 * encoding is little-endian, so this would not work as
7150 * intuitively as the same works for [big-endian] IPv4
7151 * addresses (0x01020304 means 1.2.3.4).
7153 if (proto
== Q_DECNET
)
7154 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7156 } else if (proto
== Q_DECNET
) {
7158 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7159 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7160 * for a valid DECnet address.
7162 vlen
= pcapint_atodn(s
, &v
);
7164 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7167 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7168 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7171 vlen
= pcapint_atoin(s
, &v
);
7173 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7181 if (proto
== Q_DECNET
)
7182 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7183 else if (proto
== Q_LINK
) {
7184 bpf_error(cstate
, "illegal link layer address");
7187 if (s
== NULL
&& q
.addr
== Q_NET
) {
7188 /* Promote short net number */
7189 while (v
&& (v
& 0xff000000) == 0) {
7194 /* Promote short ipaddr */
7196 mask
<<= 32 - vlen
;
7198 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7203 proto
= IPPROTO_UDP
;
7204 else if (proto
== Q_TCP
)
7205 proto
= IPPROTO_TCP
;
7206 else if (proto
== Q_SCTP
)
7207 proto
= IPPROTO_SCTP
;
7208 else if (proto
== Q_DEFAULT
)
7209 proto
= PROTO_UNDEF
;
7211 bpf_error(cstate
, "illegal qualifier of 'port'");
7214 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7218 b
= gen_port(cstate
, v
, proto
, dir
);
7219 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7225 proto
= IPPROTO_UDP
;
7226 else if (proto
== Q_TCP
)
7227 proto
= IPPROTO_TCP
;
7228 else if (proto
== Q_SCTP
)
7229 proto
= IPPROTO_SCTP
;
7230 else if (proto
== Q_DEFAULT
)
7231 proto
= PROTO_UNDEF
;
7233 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7236 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7240 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7241 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7246 bpf_error(cstate
, "'gateway' requires a name");
7250 return gen_proto(cstate
, v
, proto
, dir
);
7252 #if !defined(NO_PROTOCHAIN)
7254 return gen_protochain(cstate
, v
, proto
);
7270 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7273 struct addrinfo
*res
;
7274 struct in6_addr
*addr
;
7275 struct in6_addr mask
;
7277 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7280 * Catch errors reported by us and routines below us, and return NULL
7283 if (setjmp(cstate
->top_ctx
))
7286 res
= pcap_nametoaddrinfo(s
);
7288 bpf_error(cstate
, "invalid ip6 address %s", s
);
7291 bpf_error(cstate
, "%s resolved to multiple address", s
);
7292 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7294 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7295 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7296 memset(&mask
, 0, sizeof(mask
));
7297 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7299 mask
.s6_addr
[masklen
/ 8] =
7300 (0xff << (8 - masklen
% 8)) & 0xff;
7303 memcpy(a
, addr
, sizeof(a
));
7304 memcpy(m
, &mask
, sizeof(m
));
7305 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7306 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7307 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7315 bpf_error(cstate
, "Mask syntax for networks only");
7319 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7325 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7332 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7334 struct block
*b
, *tmp
;
7337 * Catch errors reported by us and routines below us, and return NULL
7340 if (setjmp(cstate
->top_ctx
))
7343 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7344 cstate
->e
= pcap_ether_aton(s
);
7345 if (cstate
->e
== NULL
)
7346 bpf_error(cstate
, "malloc");
7347 switch (cstate
->linktype
) {
7349 case DLT_NETANALYZER
:
7350 case DLT_NETANALYZER_TRANSPARENT
:
7351 tmp
= gen_prevlinkhdr_check(cstate
);
7352 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7357 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7360 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7362 case DLT_IEEE802_11
:
7363 case DLT_PRISM_HEADER
:
7364 case DLT_IEEE802_11_RADIO_AVS
:
7365 case DLT_IEEE802_11_RADIO
:
7367 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7369 case DLT_IP_OVER_FC
:
7370 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7375 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7382 bpf_error(cstate
, "ethernet address used in non-ether expression");
7387 sappend(struct slist
*s0
, struct slist
*s1
)
7390 * This is definitely not the best way to do this, but the
7391 * lists will rarely get long.
7398 static struct slist
*
7399 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7403 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7408 static struct slist
*
7409 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7413 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7419 * Modify "index" to use the value stored into its register as an
7420 * offset relative to the beginning of the header for the protocol
7421 * "proto", and allocate a register and put an item "size" bytes long
7422 * (1, 2, or 4) at that offset into that register, making it the register
7425 static struct arth
*
7426 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7430 struct slist
*s
, *tmp
;
7432 int regno
= alloc_reg(cstate
);
7434 free_reg(cstate
, inst
->regno
);
7438 bpf_error(cstate
, "data size must be 1, 2, or 4");
7455 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7459 * The offset is relative to the beginning of the packet
7460 * data, if we have a radio header. (If we don't, this
7463 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7464 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7465 cstate
->linktype
!= DLT_PRISM_HEADER
)
7466 bpf_error(cstate
, "radio information not present in capture");
7469 * Load into the X register the offset computed into the
7470 * register specified by "index".
7472 s
= xfer_to_x(cstate
, inst
);
7475 * Load the item at that offset.
7477 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7479 sappend(inst
->s
, s
);
7484 * The offset is relative to the beginning of
7485 * the link-layer header.
7487 * XXX - what about ATM LANE? Should the index be
7488 * relative to the beginning of the AAL5 frame, so
7489 * that 0 refers to the beginning of the LE Control
7490 * field, or relative to the beginning of the LAN
7491 * frame, so that 0 refers, for Ethernet LANE, to
7492 * the beginning of the destination address?
7494 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7497 * If "s" is non-null, it has code to arrange that the
7498 * X register contains the length of the prefix preceding
7499 * the link-layer header. Add to it the offset computed
7500 * into the register specified by "index", and move that
7501 * into the X register. Otherwise, just load into the X
7502 * register the offset computed into the register specified
7506 sappend(s
, xfer_to_a(cstate
, inst
));
7507 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7508 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7510 s
= xfer_to_x(cstate
, inst
);
7513 * Load the item at the sum of the offset we've put in the
7514 * X register and the offset of the start of the link
7515 * layer header (which is 0 if the radio header is
7516 * variable-length; that header length is what we put
7517 * into the X register and then added to the index).
7519 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7520 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7522 sappend(inst
->s
, s
);
7536 * The offset is relative to the beginning of
7537 * the network-layer header.
7538 * XXX - are there any cases where we want
7539 * cstate->off_nl_nosnap?
7541 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7544 * If "s" is non-null, it has code to arrange that the
7545 * X register contains the variable part of the offset
7546 * of the link-layer payload. Add to it the offset
7547 * computed into the register specified by "index",
7548 * and move that into the X register. Otherwise, just
7549 * load into the X register the offset computed into
7550 * the register specified by "index".
7553 sappend(s
, xfer_to_a(cstate
, inst
));
7554 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7555 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7557 s
= xfer_to_x(cstate
, inst
);
7560 * Load the item at the sum of the offset we've put in the
7561 * X register, the offset of the start of the network
7562 * layer header from the beginning of the link-layer
7563 * payload, and the constant part of the offset of the
7564 * start of the link-layer payload.
7566 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7567 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7569 sappend(inst
->s
, s
);
7572 * Do the computation only if the packet contains
7573 * the protocol in question.
7575 b
= gen_proto_abbrev_internal(cstate
, proto
);
7577 gen_and(inst
->b
, b
);
7591 * The offset is relative to the beginning of
7592 * the transport-layer header.
7594 * Load the X register with the length of the IPv4 header
7595 * (plus the offset of the link-layer header, if it's
7596 * a variable-length header), in bytes.
7598 * XXX - are there any cases where we want
7599 * cstate->off_nl_nosnap?
7600 * XXX - we should, if we're built with
7601 * IPv6 support, generate code to load either
7602 * IPv4, IPv6, or both, as appropriate.
7604 s
= gen_loadx_iphdrlen(cstate
);
7607 * The X register now contains the sum of the variable
7608 * part of the offset of the link-layer payload and the
7609 * length of the network-layer header.
7611 * Load into the A register the offset relative to
7612 * the beginning of the transport layer header,
7613 * add the X register to that, move that to the
7614 * X register, and load with an offset from the
7615 * X register equal to the sum of the constant part of
7616 * the offset of the link-layer payload and the offset,
7617 * relative to the beginning of the link-layer payload,
7618 * of the network-layer header.
7620 sappend(s
, xfer_to_a(cstate
, inst
));
7621 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7622 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7623 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7624 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7625 sappend(inst
->s
, s
);
7628 * Do the computation only if the packet contains
7629 * the protocol in question - which is true only
7630 * if this is an IP datagram and is the first or
7631 * only fragment of that datagram.
7633 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7635 gen_and(inst
->b
, b
);
7636 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7641 * Do the computation only if the packet contains
7642 * the protocol in question.
7644 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7646 gen_and(inst
->b
, b
);
7650 * Check if we have an icmp6 next header
7652 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7654 gen_and(inst
->b
, b
);
7657 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7659 * If "s" is non-null, it has code to arrange that the
7660 * X register contains the variable part of the offset
7661 * of the link-layer payload. Add to it the offset
7662 * computed into the register specified by "index",
7663 * and move that into the X register. Otherwise, just
7664 * load into the X register the offset computed into
7665 * the register specified by "index".
7668 sappend(s
, xfer_to_a(cstate
, inst
));
7669 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7670 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7672 s
= xfer_to_x(cstate
, inst
);
7675 * Load the item at the sum of the offset we've put in the
7676 * X register, the offset of the start of the network
7677 * layer header from the beginning of the link-layer
7678 * payload, and the constant part of the offset of the
7679 * start of the link-layer payload.
7681 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7682 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7685 sappend(inst
->s
, s
);
7689 inst
->regno
= regno
;
7690 s
= new_stmt(cstate
, BPF_ST
);
7692 sappend(inst
->s
, s
);
7698 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7702 * Catch errors reported by us and routines below us, and return NULL
7705 if (setjmp(cstate
->top_ctx
))
7708 return gen_load_internal(cstate
, proto
, inst
, size
);
7711 static struct block
*
7712 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7713 struct arth
*a1
, int reversed
)
7715 struct slist
*s0
, *s1
, *s2
;
7716 struct block
*b
, *tmp
;
7718 s0
= xfer_to_x(cstate
, a1
);
7719 s1
= xfer_to_a(cstate
, a0
);
7720 if (code
== BPF_JEQ
) {
7721 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7722 b
= new_block(cstate
, JMP(code
));
7726 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7732 sappend(a0
->s
, a1
->s
);
7736 free_reg(cstate
, a0
->regno
);
7737 free_reg(cstate
, a1
->regno
);
7739 /* 'and' together protocol checks */
7742 gen_and(a0
->b
, tmp
= a1
->b
);
7756 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7757 struct arth
*a1
, int reversed
)
7760 * Catch errors reported by us and routines below us, and return NULL
7763 if (setjmp(cstate
->top_ctx
))
7766 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7770 gen_loadlen(compiler_state_t
*cstate
)
7777 * Catch errors reported by us and routines below us, and return NULL
7780 if (setjmp(cstate
->top_ctx
))
7783 regno
= alloc_reg(cstate
);
7784 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7785 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7786 s
->next
= new_stmt(cstate
, BPF_ST
);
7787 s
->next
->s
.k
= regno
;
7794 static struct arth
*
7795 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7801 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7803 reg
= alloc_reg(cstate
);
7805 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7807 s
->next
= new_stmt(cstate
, BPF_ST
);
7816 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7819 * Catch errors reported by us and routines below us, and return NULL
7822 if (setjmp(cstate
->top_ctx
))
7825 return gen_loadi_internal(cstate
, val
);
7829 * The a_arg dance is to avoid annoying whining by compilers that
7830 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7831 * It's not *used* after setjmp returns.
7834 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7836 struct arth
*a
= a_arg
;
7840 * Catch errors reported by us and routines below us, and return NULL
7843 if (setjmp(cstate
->top_ctx
))
7846 s
= xfer_to_a(cstate
, a
);
7848 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7851 s
= new_stmt(cstate
, BPF_ST
);
7859 * The a0_arg dance is to avoid annoying whining by compilers that
7860 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7861 * It's not *used* after setjmp returns.
7864 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7867 struct arth
*a0
= a0_arg
;
7868 struct slist
*s0
, *s1
, *s2
;
7871 * Catch errors reported by us and routines below us, and return NULL
7874 if (setjmp(cstate
->top_ctx
))
7878 * Disallow division by, or modulus by, zero; we do this here
7879 * so that it gets done even if the optimizer is disabled.
7881 * Also disallow shifts by a value greater than 31; we do this
7882 * here, for the same reason.
7884 if (code
== BPF_DIV
) {
7885 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7886 bpf_error(cstate
, "division by zero");
7887 } else if (code
== BPF_MOD
) {
7888 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7889 bpf_error(cstate
, "modulus by zero");
7890 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7891 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7892 bpf_error(cstate
, "shift by more than 31 bits");
7894 s0
= xfer_to_x(cstate
, a1
);
7895 s1
= xfer_to_a(cstate
, a0
);
7896 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7901 sappend(a0
->s
, a1
->s
);
7903 free_reg(cstate
, a0
->regno
);
7904 free_reg(cstate
, a1
->regno
);
7906 s0
= new_stmt(cstate
, BPF_ST
);
7907 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7914 * Initialize the table of used registers and the current register.
7917 init_regs(compiler_state_t
*cstate
)
7920 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7924 * Return the next free register.
7927 alloc_reg(compiler_state_t
*cstate
)
7929 int n
= BPF_MEMWORDS
;
7932 if (cstate
->regused
[cstate
->curreg
])
7933 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7935 cstate
->regused
[cstate
->curreg
] = 1;
7936 return cstate
->curreg
;
7939 bpf_error(cstate
, "too many registers needed to evaluate expression");
7944 * Return a register to the table so it can
7948 free_reg(compiler_state_t
*cstate
, int n
)
7950 cstate
->regused
[n
] = 0;
7953 static struct block
*
7954 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7959 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7960 b
= new_block(cstate
, JMP(jmp
));
7968 gen_greater(compiler_state_t
*cstate
, int n
)
7971 * Catch errors reported by us and routines below us, and return NULL
7974 if (setjmp(cstate
->top_ctx
))
7977 return gen_len(cstate
, BPF_JGE
, n
);
7981 * Actually, this is less than or equal.
7984 gen_less(compiler_state_t
*cstate
, int n
)
7989 * Catch errors reported by us and routines below us, and return NULL
7992 if (setjmp(cstate
->top_ctx
))
7995 b
= gen_len(cstate
, BPF_JGT
, n
);
8002 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8003 * the beginning of the link-layer header.
8004 * XXX - that means you can't test values in the radiotap header, but
8005 * as that header is difficult if not impossible to parse generally
8006 * without a loop, that might not be a severe problem. A new keyword
8007 * "radio" could be added for that, although what you'd really want
8008 * would be a way of testing particular radio header values, which
8009 * would generate code appropriate to the radio header in question.
8012 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8018 * Catch errors reported by us and routines below us, and return NULL
8021 if (setjmp(cstate
->top_ctx
))
8029 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8032 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8036 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8040 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8044 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8048 b
= new_block(cstate
, JMP(BPF_JEQ
));
8056 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8058 bpf_u_int32 hostmask
;
8059 struct block
*b0
, *b1
, *b2
;
8060 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8063 * Catch errors reported by us and routines below us, and return NULL
8066 if (setjmp(cstate
->top_ctx
))
8073 switch (cstate
->linktype
) {
8075 case DLT_ARCNET_LINUX
:
8076 // ARCnet broadcast is [8-bit] destination address 0.
8077 return gen_ahostop(cstate
, 0, Q_DST
);
8079 case DLT_NETANALYZER
:
8080 case DLT_NETANALYZER_TRANSPARENT
:
8081 b1
= gen_prevlinkhdr_check(cstate
);
8082 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8087 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8089 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8090 case DLT_IEEE802_11
:
8091 case DLT_PRISM_HEADER
:
8092 case DLT_IEEE802_11_RADIO_AVS
:
8093 case DLT_IEEE802_11_RADIO
:
8095 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8096 case DLT_IP_OVER_FC
:
8097 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8099 fail_kw_on_dlt(cstate
, "broadcast");
8104 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8105 * as an indication that we don't know the netmask, and fail
8108 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8109 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8110 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8111 hostmask
= ~cstate
->netmask
;
8112 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8113 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8118 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
8123 * Generate code to test the low-order bit of a MAC address (that's
8124 * the bottom bit of the *first* byte).
8126 static struct block
*
8127 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8129 register struct block
*b0
;
8130 register struct slist
*s
;
8132 /* link[offset] & 1 != 0 */
8133 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8134 b0
= new_block(cstate
, JMP(BPF_JSET
));
8141 gen_multicast(compiler_state_t
*cstate
, int proto
)
8143 register struct block
*b0
, *b1
, *b2
;
8144 register struct slist
*s
;
8147 * Catch errors reported by us and routines below us, and return NULL
8150 if (setjmp(cstate
->top_ctx
))
8157 switch (cstate
->linktype
) {
8159 case DLT_ARCNET_LINUX
:
8160 // ARCnet multicast is the same as broadcast.
8161 return gen_ahostop(cstate
, 0, Q_DST
);
8163 case DLT_NETANALYZER
:
8164 case DLT_NETANALYZER_TRANSPARENT
:
8165 b1
= gen_prevlinkhdr_check(cstate
);
8166 /* ether[0] & 1 != 0 */
8167 b0
= gen_mac_multicast(cstate
, 0);
8173 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8175 * XXX - was that referring to bit-order issues?
8177 /* fddi[1] & 1 != 0 */
8178 return gen_mac_multicast(cstate
, 1);
8180 /* tr[2] & 1 != 0 */
8181 return gen_mac_multicast(cstate
, 2);
8182 case DLT_IEEE802_11
:
8183 case DLT_PRISM_HEADER
:
8184 case DLT_IEEE802_11_RADIO_AVS
:
8185 case DLT_IEEE802_11_RADIO
:
8190 * For control frames, there is no DA.
8192 * For management frames, DA is at an
8193 * offset of 4 from the beginning of
8196 * For data frames, DA is at an offset
8197 * of 4 from the beginning of the packet
8198 * if To DS is clear and at an offset of
8199 * 16 from the beginning of the packet
8204 * Generate the tests to be done for data frames.
8206 * First, check for To DS set, i.e. "link[1] & 0x01".
8208 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8209 b1
= new_block(cstate
, JMP(BPF_JSET
));
8210 b1
->s
.k
= 0x01; /* To DS */
8214 * If To DS is set, the DA is at 16.
8216 b0
= gen_mac_multicast(cstate
, 16);
8220 * Now, check for To DS not set, i.e. check
8221 * "!(link[1] & 0x01)".
8223 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8224 b2
= new_block(cstate
, JMP(BPF_JSET
));
8225 b2
->s
.k
= 0x01; /* To DS */
8230 * If To DS is not set, the DA is at 4.
8232 b1
= gen_mac_multicast(cstate
, 4);
8236 * Now OR together the last two checks. That gives
8237 * the complete set of checks for data frames.
8242 * Now check for a data frame.
8243 * I.e, check "link[0] & 0x08".
8245 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8246 b1
= new_block(cstate
, JMP(BPF_JSET
));
8251 * AND that with the checks done for data frames.
8256 * If the high-order bit of the type value is 0, this
8257 * is a management frame.
8258 * I.e, check "!(link[0] & 0x08)".
8260 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8261 b2
= new_block(cstate
, JMP(BPF_JSET
));
8267 * For management frames, the DA is at 4.
8269 b1
= gen_mac_multicast(cstate
, 4);
8273 * OR that with the checks done for data frames.
8274 * That gives the checks done for management and
8280 * If the low-order bit of the type value is 1,
8281 * this is either a control frame or a frame
8282 * with a reserved type, and thus not a
8285 * I.e., check "!(link[0] & 0x04)".
8287 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8288 b1
= new_block(cstate
, JMP(BPF_JSET
));
8294 * AND that with the checks for data and management
8299 case DLT_IP_OVER_FC
:
8300 b0
= gen_mac_multicast(cstate
, 2);
8305 fail_kw_on_dlt(cstate
, "multicast");
8309 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8310 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8315 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8316 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8320 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8326 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8327 * we can look at special meta-data in the filter expression; otherwise we
8328 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8329 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8330 * pcap_activate() conditionally sets.
8333 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8335 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8337 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8339 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8344 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8346 register struct block
*b0
;
8349 * Catch errors reported by us and routines below us, and return NULL
8352 if (setjmp(cstate
->top_ctx
))
8356 * Only some data link types support ifindex qualifiers.
8358 switch (cstate
->linktype
) {
8359 case DLT_LINUX_SLL2
:
8360 /* match packets on this interface */
8361 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8364 #if defined(__linux__)
8365 require_basic_bpf_extensions(cstate
, "ifindex");
8367 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8369 #else /* defined(__linux__) */
8370 fail_kw_on_dlt(cstate
, "ifindex");
8372 #endif /* defined(__linux__) */
8378 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8379 * Outbound traffic is sent by this machine, while inbound traffic is
8380 * sent by a remote machine (and may include packets destined for a
8381 * unicast or multicast link-layer address we are not subscribing to).
8382 * These are the same definitions implemented by pcap_setdirection().
8383 * Capturing only unicast traffic destined for this host is probably
8384 * better accomplished using a higher-layer filter.
8387 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8389 register struct block
*b0
;
8392 * Catch errors reported by us and routines below us, and return NULL
8395 if (setjmp(cstate
->top_ctx
))
8399 * Only some data link types support inbound/outbound qualifiers.
8401 switch (cstate
->linktype
) {
8403 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8404 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8408 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8409 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8413 /* match outgoing packets */
8414 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8416 /* to filter on inbound traffic, invert the match */
8421 case DLT_LINUX_SLL2
:
8422 /* match outgoing packets */
8423 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8425 /* to filter on inbound traffic, invert the match */
8431 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8432 outbound
? PF_OUT
: PF_IN
);
8436 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8439 case DLT_JUNIPER_MFR
:
8440 case DLT_JUNIPER_MLFR
:
8441 case DLT_JUNIPER_MLPPP
:
8442 case DLT_JUNIPER_ATM1
:
8443 case DLT_JUNIPER_ATM2
:
8444 case DLT_JUNIPER_PPPOE
:
8445 case DLT_JUNIPER_PPPOE_ATM
:
8446 case DLT_JUNIPER_GGSN
:
8447 case DLT_JUNIPER_ES
:
8448 case DLT_JUNIPER_MONITOR
:
8449 case DLT_JUNIPER_SERVICES
:
8450 case DLT_JUNIPER_ETHER
:
8451 case DLT_JUNIPER_PPP
:
8452 case DLT_JUNIPER_FRELAY
:
8453 case DLT_JUNIPER_CHDLC
:
8454 case DLT_JUNIPER_VP
:
8455 case DLT_JUNIPER_ST
:
8456 case DLT_JUNIPER_ISM
:
8457 case DLT_JUNIPER_VS
:
8458 case DLT_JUNIPER_SRX_E2E
:
8459 case DLT_JUNIPER_FIBRECHANNEL
:
8460 case DLT_JUNIPER_ATM_CEMIC
:
8461 /* juniper flags (including direction) are stored
8462 * the byte after the 3-byte magic number */
8463 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8468 * If we have packet meta-data indicating a direction,
8469 * and that metadata can be checked by BPF code, check
8470 * it. Otherwise, give up, as this link-layer type has
8471 * nothing in the packet data.
8473 * Currently, the only platform where a BPF filter can
8474 * check that metadata is Linux with the in-kernel
8475 * BPF interpreter. If other packet capture mechanisms
8476 * and BPF filters also supported this, it would be
8477 * nice. It would be even better if they made that
8478 * metadata available so that we could provide it
8479 * with newer capture APIs, allowing it to be saved
8482 #if defined(__linux__)
8483 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8484 /* match outgoing packets */
8485 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8488 /* to filter on inbound traffic, invert the match */
8491 #else /* defined(__linux__) */
8492 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8494 #endif /* defined(__linux__) */
8499 /* PF firewall log matched interface */
8501 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8507 * Catch errors reported by us and routines below us, and return NULL
8510 if (setjmp(cstate
->top_ctx
))
8513 assert_pflog(cstate
, "ifname");
8515 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8516 off
= offsetof(struct pfloghdr
, ifname
);
8517 if (strlen(ifname
) >= len
) {
8518 bpf_error(cstate
, "ifname interface names can only be %d characters",
8522 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8523 (const u_char
*)ifname
);
8527 /* PF firewall log ruleset name */
8529 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8534 * Catch errors reported by us and routines below us, and return NULL
8537 if (setjmp(cstate
->top_ctx
))
8540 assert_pflog(cstate
, "ruleset");
8542 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8543 bpf_error(cstate
, "ruleset names can only be %ld characters",
8544 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8548 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8549 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8553 /* PF firewall log rule number */
8555 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8560 * Catch errors reported by us and routines below us, and return NULL
8563 if (setjmp(cstate
->top_ctx
))
8566 assert_pflog(cstate
, "rnr");
8568 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8573 /* PF firewall log sub-rule number */
8575 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8580 * Catch errors reported by us and routines below us, and return NULL
8583 if (setjmp(cstate
->top_ctx
))
8586 assert_pflog(cstate
, "srnr");
8588 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8593 /* PF firewall log reason code */
8595 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8600 * Catch errors reported by us and routines below us, and return NULL
8603 if (setjmp(cstate
->top_ctx
))
8606 assert_pflog(cstate
, "reason");
8608 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8609 (bpf_u_int32
)reason
);
8613 /* PF firewall log action */
8615 gen_pf_action(compiler_state_t
*cstate
, int action
)
8620 * Catch errors reported by us and routines below us, and return NULL
8623 if (setjmp(cstate
->top_ctx
))
8626 assert_pflog(cstate
, "action");
8628 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8629 (bpf_u_int32
)action
);
8633 /* IEEE 802.11 wireless header */
8635 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8640 * Catch errors reported by us and routines below us, and return NULL
8643 if (setjmp(cstate
->top_ctx
))
8646 switch (cstate
->linktype
) {
8648 case DLT_IEEE802_11
:
8649 case DLT_PRISM_HEADER
:
8650 case DLT_IEEE802_11_RADIO_AVS
:
8651 case DLT_IEEE802_11_RADIO
:
8653 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8657 fail_kw_on_dlt(cstate
, "type/subtype");
8665 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8670 * Catch errors reported by us and routines below us, and return NULL
8673 if (setjmp(cstate
->top_ctx
))
8676 switch (cstate
->linktype
) {
8678 case DLT_IEEE802_11
:
8679 case DLT_PRISM_HEADER
:
8680 case DLT_IEEE802_11_RADIO_AVS
:
8681 case DLT_IEEE802_11_RADIO
:
8686 fail_kw_on_dlt(cstate
, "dir");
8690 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8691 IEEE80211_FC1_DIR_MASK
);
8696 // Process an ARCnet host address string.
8698 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8701 * Catch errors reported by us and routines below us, and return NULL
8704 if (setjmp(cstate
->top_ctx
))
8707 switch (cstate
->linktype
) {
8710 case DLT_ARCNET_LINUX
:
8711 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8712 q
.proto
== Q_LINK
) {
8715 * The lexer currently defines the address format in a
8716 * way that makes this error condition never true.
8717 * Let's check it anyway in case this part of the lexer
8718 * changes in future.
8720 if (! pcapint_atoan(s
, &addr
))
8721 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8722 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8724 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8728 bpf_error(cstate
, "aid supported only on ARCnet");
8733 // Compare an ARCnet host address with the given value.
8734 static struct block
*
8735 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8737 register struct block
*b0
, *b1
;
8741 * ARCnet is different from Ethernet: the source address comes before
8742 * the destination address, each is one byte long. This holds for all
8743 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8744 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8745 * by Datapoint (document number 61610-01).
8748 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8751 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8754 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8755 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8761 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8762 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8772 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8779 static struct block
*
8780 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8782 struct block
*b0
, *b1
;
8784 /* check for VLAN, including 802.1ad and QinQ */
8785 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8786 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8789 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8795 static struct block
*
8796 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8798 if (vlan_num
> 0x0fff) {
8799 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8802 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8805 static struct block
*
8806 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8809 struct block
*b0
, *b1
;
8811 b0
= gen_vlan_tpid_test(cstate
);
8814 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8820 * Both payload and link header type follow the VLAN tags so that
8821 * both need to be updated.
8823 cstate
->off_linkpl
.constant_part
+= 4;
8824 cstate
->off_linktype
.constant_part
+= 4;
8829 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8830 /* add v to variable part of off */
8832 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8833 bpf_u_int32 v
, struct slist
*s
)
8837 if (!off
->is_variable
)
8838 off
->is_variable
= 1;
8840 off
->reg
= alloc_reg(cstate
);
8842 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8845 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8848 s2
= new_stmt(cstate
, BPF_ST
);
8854 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8855 * and link type offsets first
8858 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8862 /* offset determined at run time, shift variable part */
8864 cstate
->is_vlan_vloffset
= 1;
8865 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8866 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8868 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8869 sappend(s
.next
, b_tpid
->head
->stmts
);
8870 b_tpid
->head
->stmts
= s
.next
;
8874 * patch block b_vid (VLAN id test) to load VID value either from packet
8875 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8878 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8880 struct slist
*s
, *s2
, *sjeq
;
8883 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8884 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8886 /* true -> next instructions, false -> beginning of b_vid */
8887 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8889 sjeq
->s
.jf
= b_vid
->stmts
;
8892 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8893 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8897 /* Jump to the test in b_vid. We need to jump one instruction before
8898 * the end of the b_vid block so that we only skip loading the TCI
8899 * from packet data and not the 'and' instruction extracting VID.
8902 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8904 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8908 /* insert our statements at the beginning of b_vid */
8909 sappend(s
, b_vid
->stmts
);
8914 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8915 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8916 * tag can be either in metadata or in packet data; therefore if the
8917 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8918 * header for VLAN tag. As the decision is done at run time, we need
8919 * update variable part of the offsets
8921 static struct block
*
8922 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8925 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8928 /* generate new filter code based on extracting packet
8930 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8931 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8933 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8938 * This is tricky. We need to insert the statements updating variable
8939 * parts of offsets before the traditional TPID and VID tests so
8940 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8941 * we do not want this update to affect those checks. That's why we
8942 * generate both test blocks first and insert the statements updating
8943 * variable parts of both offsets after that. This wouldn't work if
8944 * there already were variable length link header when entering this
8945 * function but gen_vlan_bpf_extensions() isn't called in that case.
8947 b_tpid
= gen_vlan_tpid_test(cstate
);
8949 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8951 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8956 gen_vlan_patch_vid_test(cstate
, b_vid
);
8966 * support IEEE 802.1Q VLAN trunk over ethernet
8969 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8974 * Catch errors reported by us and routines below us, and return NULL
8977 if (setjmp(cstate
->top_ctx
))
8980 /* can't check for VLAN-encapsulated packets inside MPLS */
8981 if (cstate
->label_stack_depth
> 0)
8982 bpf_error(cstate
, "no VLAN match after MPLS");
8985 * Check for a VLAN packet, and then change the offsets to point
8986 * to the type and data fields within the VLAN packet. Just
8987 * increment the offsets, so that we can support a hierarchy, e.g.
8988 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
8991 * XXX - this is a bit of a kludge. If we were to split the
8992 * compiler into a parser that parses an expression and
8993 * generates an expression tree, and a code generator that
8994 * takes an expression tree (which could come from our
8995 * parser or from some other parser) and generates BPF code,
8996 * we could perhaps make the offsets parameters of routines
8997 * and, in the handler for an "AND" node, pass to subnodes
8998 * other than the VLAN node the adjusted offsets.
9000 * This would mean that "vlan" would, instead of changing the
9001 * behavior of *all* tests after it, change only the behavior
9002 * of tests ANDed with it. That would change the documented
9003 * semantics of "vlan", which might break some expressions.
9004 * However, it would mean that "(vlan and ip) or ip" would check
9005 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9006 * checking only for VLAN-encapsulated IP, so that could still
9007 * be considered worth doing; it wouldn't break expressions
9008 * that are of the form "vlan and ..." or "vlan N and ...",
9009 * which I suspect are the most common expressions involving
9010 * "vlan". "vlan or ..." doesn't necessarily do what the user
9011 * would really want, now, as all the "or ..." tests would
9012 * be done assuming a VLAN, even though the "or" could be viewed
9013 * as meaning "or, if this isn't a VLAN packet...".
9015 switch (cstate
->linktype
) {
9019 * Newer version of the Linux kernel pass around
9020 * packets in which the VLAN tag has been removed
9021 * from the packet data and put into metadata.
9023 * This requires special treatment.
9025 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9026 /* Verify that this is the outer part of the packet and
9027 * not encapsulated somehow. */
9028 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9029 cstate
->off_linkhdr
.constant_part
==
9030 cstate
->off_outermostlinkhdr
.constant_part
) {
9032 * Do we need special VLAN handling?
9034 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9035 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9038 b0
= gen_vlan_no_bpf_extensions(cstate
,
9039 vlan_num
, has_vlan_tag
);
9042 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9046 case DLT_NETANALYZER
:
9047 case DLT_NETANALYZER_TRANSPARENT
:
9048 case DLT_IEEE802_11
:
9049 case DLT_PRISM_HEADER
:
9050 case DLT_IEEE802_11_RADIO_AVS
:
9051 case DLT_IEEE802_11_RADIO
:
9053 * These are either Ethernet packets with an additional
9054 * metadata header (the NetAnalyzer types), or 802.11
9055 * packets, possibly with an additional metadata header.
9057 * For the first of those, the VLAN tag is in the normal
9058 * place, so the special-case handling above isn't
9061 * For the second of those, we don't do the special-case
9064 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9068 bpf_error(cstate
, "no VLAN support for %s",
9069 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9073 cstate
->vlan_stack_depth
++;
9081 * The label_num_arg dance is to avoid annoying whining by compilers that
9082 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9083 * It's not *used* after setjmp returns.
9085 static struct block
*
9086 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9089 struct block
*b0
, *b1
;
9091 if (cstate
->label_stack_depth
> 0) {
9092 /* just match the bottom-of-stack bit clear */
9093 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9096 * We're not in an MPLS stack yet, so check the link-layer
9097 * type against MPLS.
9099 switch (cstate
->linktype
) {
9101 case DLT_C_HDLC
: /* fall through */
9104 case DLT_NETANALYZER
:
9105 case DLT_NETANALYZER_TRANSPARENT
:
9106 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9110 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9113 /* FIXME add other DLT_s ...
9114 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9115 * leave it for now */
9118 bpf_error(cstate
, "no MPLS support for %s",
9119 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9124 /* If a specific MPLS label is requested, check it */
9125 if (has_label_num
) {
9126 if (label_num
> 0xFFFFF) {
9127 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9128 label_num
, 0xFFFFF);
9130 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9131 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9132 0xfffff000); /* only compare the first 20 bits */
9138 * Change the offsets to point to the type and data fields within
9139 * the MPLS packet. Just increment the offsets, so that we
9140 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9141 * capture packets with an outer label of 100000 and an inner
9144 * Increment the MPLS stack depth as well; this indicates that
9145 * we're checking MPLS-encapsulated headers, to make sure higher
9146 * level code generators don't try to match against IP-related
9147 * protocols such as Q_ARP, Q_RARP etc.
9149 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9151 cstate
->off_nl_nosnap
+= 4;
9152 cstate
->off_nl
+= 4;
9153 cstate
->label_stack_depth
++;
9158 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9161 * Catch errors reported by us and routines below us, and return NULL
9164 if (setjmp(cstate
->top_ctx
))
9167 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9171 * Support PPPOE discovery and session.
9174 gen_pppoed(compiler_state_t
*cstate
)
9177 * Catch errors reported by us and routines below us, and return NULL
9180 if (setjmp(cstate
->top_ctx
))
9183 /* check for PPPoE discovery */
9184 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9188 * RFC 2516 Section 4:
9190 * The Ethernet payload for PPPoE is as follows:
9193 * 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
9194 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9195 * | VER | TYPE | CODE | SESSION_ID |
9196 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9197 * | LENGTH | payload ~
9198 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9201 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9203 struct block
*b0
, *b1
;
9206 * Catch errors reported by us and routines below us, and return NULL
9209 if (setjmp(cstate
->top_ctx
))
9213 * Test against the PPPoE session link-layer type.
9215 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9217 /* If a specific session is requested, check PPPoE session id */
9219 if (sess_num
> UINT16_MAX
) {
9220 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9221 sess_num
, UINT16_MAX
);
9223 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9229 * Change the offsets to point to the type and data fields within
9230 * the PPP packet, and note that this is PPPoE rather than
9233 * XXX - this is a bit of a kludge. See the comments in
9236 * The "network-layer" protocol is PPPoE, which has a 6-byte
9237 * PPPoE header, followed by a PPP packet.
9239 * There is no HDLC encapsulation for the PPP packet (it's
9240 * encapsulated in PPPoES instead), so the link-layer type
9241 * starts at the first byte of the PPP packet. For PPPoE,
9242 * that offset is relative to the beginning of the total
9243 * link-layer payload, including any 802.2 LLC header, so
9244 * it's 6 bytes past cstate->off_nl.
9246 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9247 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9248 cstate
->off_linkpl
.reg
);
9250 cstate
->off_linktype
= cstate
->off_linkhdr
;
9251 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9254 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9259 /* Check that this is Geneve and the VNI is correct if
9260 * specified. Parameterized to handle both IPv4 and IPv6. */
9261 static struct block
*
9262 gen_geneve_check(compiler_state_t
*cstate
,
9263 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9264 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9266 struct block
*b0
, *b1
;
9268 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9270 /* Check that we are operating on version 0. Otherwise, we
9271 * can't decode the rest of the fields. The version is 2 bits
9272 * in the first byte of the Geneve header. */
9273 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9278 if (vni
> 0xffffff) {
9279 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9282 vni
<<= 8; /* VNI is in the upper 3 bytes */
9283 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9291 /* The IPv4 and IPv6 Geneve checks need to do two things:
9292 * - Verify that this actually is Geneve with the right VNI.
9293 * - Place the IP header length (plus variable link prefix if
9294 * needed) into register A to be used later to compute
9295 * the inner packet offsets. */
9296 static struct block
*
9297 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9299 struct block
*b0
, *b1
;
9300 struct slist
*s
, *s1
;
9302 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9304 /* Load the IP header length into A. */
9305 s
= gen_loadx_iphdrlen(cstate
);
9307 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9310 /* Forcibly append these statements to the true condition
9311 * of the protocol check by creating a new block that is
9312 * always true and ANDing them. */
9313 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9322 static struct block
*
9323 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9325 struct block
*b0
, *b1
;
9326 struct slist
*s
, *s1
;
9328 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9330 /* Load the IP header length. We need to account for a
9331 * variable length link prefix if there is one. */
9332 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9334 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9338 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9342 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9346 /* Forcibly append these statements to the true condition
9347 * of the protocol check by creating a new block that is
9348 * always true and ANDing them. */
9349 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9352 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9361 /* We need to store three values based on the Geneve header::
9362 * - The offset of the linktype.
9363 * - The offset of the end of the Geneve header.
9364 * - The offset of the end of the encapsulated MAC header. */
9365 static struct slist
*
9366 gen_geneve_offsets(compiler_state_t
*cstate
)
9368 struct slist
*s
, *s1
, *s_proto
;
9370 /* First we need to calculate the offset of the Geneve header
9371 * itself. This is composed of the IP header previously calculated
9372 * (include any variable link prefix) and stored in A plus the
9373 * fixed sized headers (fixed link prefix, MAC length, and UDP
9375 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9376 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9378 /* Stash this in X since we'll need it later. */
9379 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9382 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9384 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9388 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9389 cstate
->off_linktype
.is_variable
= 1;
9390 cstate
->off_linktype
.constant_part
= 0;
9392 s1
= new_stmt(cstate
, BPF_ST
);
9393 s1
->s
.k
= cstate
->off_linktype
.reg
;
9396 /* Load the Geneve option length and mask and shift to get the
9397 * number of bytes. It is stored in the first byte of the Geneve
9399 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9403 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9407 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9411 /* Add in the rest of the Geneve base header. */
9412 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9416 /* Add the Geneve header length to its offset and store. */
9417 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9421 /* Set the encapsulated type as Ethernet. Even though we may
9422 * not actually have Ethernet inside there are two reasons this
9424 * - The linktype field is always in EtherType format regardless
9425 * of whether it is in Geneve or an inner Ethernet frame.
9426 * - The only link layer that we have specific support for is
9427 * Ethernet. We will confirm that the packet actually is
9428 * Ethernet at runtime before executing these checks. */
9429 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9431 s1
= new_stmt(cstate
, BPF_ST
);
9432 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9435 /* Calculate whether we have an Ethernet header or just raw IP/
9436 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9437 * and linktype by 14 bytes so that the network header can be found
9438 * seamlessly. Otherwise, keep what we've calculated already. */
9440 /* We have a bare jmp so we can't use the optimizer. */
9441 cstate
->no_optimize
= 1;
9443 /* Load the EtherType in the Geneve header, 2 bytes in. */
9444 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9448 /* Load X with the end of the Geneve header. */
9449 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9450 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9453 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9454 * end of this check, we should have the total length in X. In
9455 * the non-Ethernet case, it's already there. */
9456 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9457 s_proto
->s
.k
= ETHERTYPE_TEB
;
9458 sappend(s
, s_proto
);
9460 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9464 /* Since this is Ethernet, use the EtherType of the payload
9465 * directly as the linktype. Overwrite what we already have. */
9466 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9470 s1
= new_stmt(cstate
, BPF_ST
);
9471 s1
->s
.k
= cstate
->off_linktype
.reg
;
9474 /* Advance two bytes further to get the end of the Ethernet
9476 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9480 /* Move the result to X. */
9481 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9484 /* Store the final result of our linkpl calculation. */
9485 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9486 cstate
->off_linkpl
.is_variable
= 1;
9487 cstate
->off_linkpl
.constant_part
= 0;
9489 s1
= new_stmt(cstate
, BPF_STX
);
9490 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9499 /* Check to see if this is a Geneve packet. */
9501 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9503 struct block
*b0
, *b1
;
9507 * Catch errors reported by us and routines below us, and return NULL
9510 if (setjmp(cstate
->top_ctx
))
9513 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9514 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9519 /* Later filters should act on the payload of the Geneve frame,
9520 * update all of the header pointers. Attach this code so that
9521 * it gets executed in the event that the Geneve filter matches. */
9522 s
= gen_geneve_offsets(cstate
);
9524 b1
= gen_true(cstate
);
9525 sappend(s
, b1
->stmts
);
9530 cstate
->is_encap
= 1;
9535 /* Check that this is VXLAN and the VNI is correct if
9536 * specified. Parameterized to handle both IPv4 and IPv6. */
9537 static struct block
*
9538 gen_vxlan_check(compiler_state_t
*cstate
,
9539 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9540 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9542 struct block
*b0
, *b1
;
9544 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9546 /* Check that the VXLAN header has the flag bits set
9548 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9553 if (vni
> 0xffffff) {
9554 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9557 vni
<<= 8; /* VNI is in the upper 3 bytes */
9558 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9566 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9567 * - Verify that this actually is VXLAN with the right VNI.
9568 * - Place the IP header length (plus variable link prefix if
9569 * needed) into register A to be used later to compute
9570 * the inner packet offsets. */
9571 static struct block
*
9572 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9574 struct block
*b0
, *b1
;
9575 struct slist
*s
, *s1
;
9577 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9579 /* Load the IP header length into A. */
9580 s
= gen_loadx_iphdrlen(cstate
);
9582 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9585 /* Forcibly append these statements to the true condition
9586 * of the protocol check by creating a new block that is
9587 * always true and ANDing them. */
9588 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9597 static struct block
*
9598 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9600 struct block
*b0
, *b1
;
9601 struct slist
*s
, *s1
;
9603 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9605 /* Load the IP header length. We need to account for a
9606 * variable length link prefix if there is one. */
9607 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9609 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9613 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9617 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9621 /* Forcibly append these statements to the true condition
9622 * of the protocol check by creating a new block that is
9623 * always true and ANDing them. */
9624 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9627 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9636 /* We need to store three values based on the VXLAN header:
9637 * - The offset of the linktype.
9638 * - The offset of the end of the VXLAN header.
9639 * - The offset of the end of the encapsulated MAC header. */
9640 static struct slist
*
9641 gen_vxlan_offsets(compiler_state_t
*cstate
)
9643 struct slist
*s
, *s1
;
9645 /* Calculate the offset of the VXLAN header itself. This
9646 * includes the IP header computed previously (including any
9647 * variable link prefix) and stored in A plus the fixed size
9648 * headers (fixed link prefix, MAC length, UDP header). */
9649 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9650 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9652 /* Add the VXLAN header length to its offset and store */
9653 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9657 /* Push the link header. VXLAN packets always contain Ethernet
9659 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9661 s1
= new_stmt(cstate
, BPF_ST
);
9662 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9665 /* As the payload is an Ethernet packet, we can use the
9666 * EtherType of the payload directly as the linktype. */
9667 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9671 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9672 cstate
->off_linktype
.is_variable
= 1;
9673 cstate
->off_linktype
.constant_part
= 0;
9675 s1
= new_stmt(cstate
, BPF_ST
);
9676 s1
->s
.k
= cstate
->off_linktype
.reg
;
9679 /* Two bytes further is the end of the Ethernet header and the
9680 * start of the payload. */
9681 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9685 /* Move the result to X. */
9686 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9689 /* Store the final result of our linkpl calculation. */
9690 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9691 cstate
->off_linkpl
.is_variable
= 1;
9692 cstate
->off_linkpl
.constant_part
= 0;
9694 s1
= new_stmt(cstate
, BPF_STX
);
9695 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9703 /* Check to see if this is a VXLAN packet. */
9705 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9707 struct block
*b0
, *b1
;
9711 * Catch errors reported by us and routines below us, and return NULL
9714 if (setjmp(cstate
->top_ctx
))
9717 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9718 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9723 /* Later filters should act on the payload of the VXLAN frame,
9724 * update all of the header pointers. Attach this code so that
9725 * it gets executed in the event that the VXLAN filter matches. */
9726 s
= gen_vxlan_offsets(cstate
);
9728 b1
= gen_true(cstate
);
9729 sappend(s
, b1
->stmts
);
9734 cstate
->is_encap
= 1;
9739 /* Check that the encapsulated frame has a link layer header
9740 * for Ethernet filters. */
9741 static struct block
*
9742 gen_encap_ll_check(compiler_state_t
*cstate
)
9745 struct slist
*s
, *s1
;
9747 /* The easiest way to see if there is a link layer present
9748 * is to check if the link layer header and payload are not
9751 /* Geneve always generates pure variable offsets so we can
9752 * compare only the registers. */
9753 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9754 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9756 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9757 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9760 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9768 static struct block
*
9769 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9770 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9777 if (!cstate
->is_atm
)
9778 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9779 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9781 if (jvalue
> UINT8_MAX
)
9782 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
9783 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9784 0xffffffffU
, jtype
, reverse
, jvalue
);
9788 if (!cstate
->is_atm
)
9789 bpf_error(cstate
, "'vci' supported only on raw ATM");
9790 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9792 if (jvalue
> UINT16_MAX
)
9793 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
9794 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9795 0xffffffffU
, jtype
, reverse
, jvalue
);
9799 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9800 abort(); /* XXX - this isn't on FreeBSD */
9801 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9802 0x0fU
, jtype
, reverse
, jvalue
);
9806 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9808 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9809 0xffffffffU
, jtype
, reverse
, jvalue
);
9818 static struct block
*
9819 gen_atmtype_metac(compiler_state_t
*cstate
)
9821 struct block
*b0
, *b1
;
9823 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9824 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9829 static struct block
*
9830 gen_atmtype_sc(compiler_state_t
*cstate
)
9832 struct block
*b0
, *b1
;
9834 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9835 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9840 static struct block
*
9841 gen_atmtype_llc(compiler_state_t
*cstate
)
9845 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9846 cstate
->linktype
= cstate
->prevlinktype
;
9851 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9852 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9855 * Catch errors reported by us and routines below us, and return NULL
9858 if (setjmp(cstate
->top_ctx
))
9861 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9866 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9868 struct block
*b0
, *b1
;
9871 * Catch errors reported by us and routines below us, and return NULL
9874 if (setjmp(cstate
->top_ctx
))
9880 /* Get all packets in Meta signalling Circuit */
9881 if (!cstate
->is_atm
)
9882 bpf_error(cstate
, "'metac' supported only on raw ATM");
9883 b1
= gen_atmtype_metac(cstate
);
9887 /* Get all packets in Broadcast Circuit*/
9888 if (!cstate
->is_atm
)
9889 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9890 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9891 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9896 /* Get all cells in Segment OAM F4 circuit*/
9897 if (!cstate
->is_atm
)
9898 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9899 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9900 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9905 /* Get all cells in End-to-End OAM F4 Circuit*/
9906 if (!cstate
->is_atm
)
9907 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9908 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9909 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9914 /* Get all packets in connection Signalling Circuit */
9915 if (!cstate
->is_atm
)
9916 bpf_error(cstate
, "'sc' supported only on raw ATM");
9917 b1
= gen_atmtype_sc(cstate
);
9921 /* Get all packets in ILMI Circuit */
9922 if (!cstate
->is_atm
)
9923 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9924 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9925 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9930 /* Get all LANE packets */
9931 if (!cstate
->is_atm
)
9932 bpf_error(cstate
, "'lane' supported only on raw ATM");
9933 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9936 * Arrange that all subsequent tests assume LANE
9937 * rather than LLC-encapsulated packets, and set
9938 * the offsets appropriately for LANE-encapsulated
9941 * We assume LANE means Ethernet, not Token Ring.
9943 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9944 cstate
->off_payload
+ 2, /* Ethernet header */
9946 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9947 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9948 cstate
->off_nl
= 0; /* Ethernet II */
9949 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9959 * Filtering for MTP2 messages based on li value
9960 * FISU, length is null
9961 * LSSU, length is 1 or 2
9962 * MSU, length is 3 or more
9963 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9966 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9968 struct block
*b0
, *b1
;
9971 * Catch errors reported by us and routines below us, and return NULL
9974 if (setjmp(cstate
->top_ctx
))
9980 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9981 (cstate
->linktype
!= DLT_ERF
) &&
9982 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9983 bpf_error(cstate
, "'fisu' supported only on MTP2");
9984 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9985 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9986 0x3fU
, BPF_JEQ
, 0, 0U);
9990 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9991 (cstate
->linktype
!= DLT_ERF
) &&
9992 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9993 bpf_error(cstate
, "'lssu' supported only on MTP2");
9994 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9995 0x3fU
, BPF_JGT
, 1, 2U);
9996 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9997 0x3fU
, BPF_JGT
, 0, 0U);
10002 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10003 (cstate
->linktype
!= DLT_ERF
) &&
10004 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10005 bpf_error(cstate
, "'msu' supported only on MTP2");
10006 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10007 0x3fU
, BPF_JGT
, 0, 2U);
10011 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10012 (cstate
->linktype
!= DLT_ERF
) &&
10013 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10014 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10015 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10016 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10017 0xff80U
, BPF_JEQ
, 0, 0U);
10021 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10022 (cstate
->linktype
!= DLT_ERF
) &&
10023 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10024 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10025 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10026 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10027 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10028 0xff80U
, BPF_JGT
, 0, 0U);
10033 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10034 (cstate
->linktype
!= DLT_ERF
) &&
10035 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10036 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10037 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10038 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10048 * These maximum valid values are all-ones, so they double as the bitmasks
10049 * before any bitwise shifting.
10051 #define MTP2_SIO_MAXVAL UINT8_MAX
10052 #define MTP3_PC_MAXVAL 0x3fffU
10053 #define MTP3_SLS_MAXVAL 0xfU
10055 static struct block
*
10056 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10057 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10065 newoff_sio
= cstate
->off_sio
;
10066 newoff_opc
= cstate
->off_opc
;
10067 newoff_dpc
= cstate
->off_dpc
;
10068 newoff_sls
= cstate
->off_sls
;
10069 switch (mtp3field
) {
10072 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10074 * SIO is the simplest field: the size is one byte and the offset is a
10075 * multiple of bytes, so the only detail to get right is the value of
10076 * the [right-to-left] field offset.
10079 newoff_sio
+= 3; /* offset for MTP2_HSL */
10083 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10084 bpf_error(cstate
, "'sio' supported only on SS7");
10085 if(jvalue
> MTP2_SIO_MAXVAL
)
10086 bpf_error(cstate
, "sio value %u too big; max value = %u",
10087 jvalue
, MTP2_SIO_MAXVAL
);
10088 // Here the bitmask means "do not apply a bitmask".
10089 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10090 jtype
, reverse
, jvalue
);
10094 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10096 * SLS, OPC and DPC are more complicated: none of these is sized in a
10097 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10098 * diagrams are meant to be read right-to-left. This means in the
10099 * diagrams within individual fields and concatenations thereof
10100 * bitwise shifts and masks can be noted in the common left-to-right
10101 * manner until each final value is ready to be byte-swapped and
10102 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10103 * similar problem in a similar way.
10105 * Offsets of fields within the packet header always have the
10106 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10107 * DLTs the offset does not include the F (Flag) field at the
10108 * beginning of each message.
10110 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10111 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10112 * be tested entirely using a single BPF_W comparison. In this case
10113 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10114 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10115 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10116 * correlates with the [RTL] packet diagram until the byte-swapping is
10119 * The code below uses this approach for OPC, which spans 3 bytes.
10120 * DPC and SLS use shorter loads, SLS also uses a different offset.
10127 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10128 bpf_error(cstate
, "'opc' supported only on SS7");
10129 if (jvalue
> MTP3_PC_MAXVAL
)
10130 bpf_error(cstate
, "opc value %u too big; max value = %u",
10131 jvalue
, MTP3_PC_MAXVAL
);
10132 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10133 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10134 SWAPLONG(jvalue
<< 14));
10142 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10143 bpf_error(cstate
, "'dpc' supported only on SS7");
10144 if (jvalue
> MTP3_PC_MAXVAL
)
10145 bpf_error(cstate
, "dpc value %u too big; max value = %u",
10146 jvalue
, MTP3_PC_MAXVAL
);
10147 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10148 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10149 SWAPSHORT(jvalue
));
10157 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10158 bpf_error(cstate
, "'sls' supported only on SS7");
10159 if (jvalue
> MTP3_SLS_MAXVAL
)
10160 bpf_error(cstate
, "sls value %u too big; max value = %u",
10161 jvalue
, MTP3_SLS_MAXVAL
);
10162 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10163 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10174 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10175 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10178 * Catch errors reported by us and routines below us, and return NULL
10181 if (setjmp(cstate
->top_ctx
))
10184 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10188 static struct block
*
10189 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10194 * Q.2931 signalling protocol messages for handling virtual circuits
10195 * establishment and teardown
10200 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10203 case A_CALLPROCEED
:
10204 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10208 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10212 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10216 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10219 case A_RELEASE_DONE
:
10220 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10230 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10232 struct block
*b0
, *b1
;
10235 * Catch errors reported by us and routines below us, and return NULL
10238 if (setjmp(cstate
->top_ctx
))
10244 if (!cstate
->is_atm
)
10245 bpf_error(cstate
, "'oam' supported only on raw ATM");
10247 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10248 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10250 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10255 if (!cstate
->is_atm
)
10256 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10258 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10259 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10261 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10267 * Get Q.2931 signalling messages for switched
10268 * virtual connection
10270 if (!cstate
->is_atm
)
10271 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10272 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10273 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10275 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10277 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10279 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10281 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10283 b0
= gen_atmtype_sc(cstate
);
10287 case A_METACONNECT
:
10288 if (!cstate
->is_atm
)
10289 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10290 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10291 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10293 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10295 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10297 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10299 b0
= gen_atmtype_metac(cstate
);