2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <netinet/in.h>
43 #include "ethertype.h"
46 #include "ieee80211.h"
50 #include "pcap/ipnet.h"
51 #include "diag-control.h"
52 #include "pcap-util.h"
56 #if defined(__linux__)
57 #include <linux/types.h>
58 #include <linux/if_packet.h>
59 #include <linux/filter.h>
63 #ifdef HAVE_NPCAP_BPF_H
64 /* Defines BPF extensions for Npcap */
65 #include <npcap-bpf.h>
68 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
75 uint16_t u6_addr16
[8];
76 uint32_t u6_addr32
[4];
78 #define s6_addr in6_u.u6_addr8
79 #define s6_addr16 in6_u.u6_addr16
80 #define s6_addr32 in6_u.u6_addr32
81 #define s6_addr64 in6_u.u6_addr64
84 typedef unsigned short sa_family_t
;
86 #define __SOCKADDR_COMMON(sa_prefix) \
87 sa_family_t sa_prefix##family
89 /* Ditto, for IPv6. */
92 __SOCKADDR_COMMON (sin6_
);
93 uint16_t sin6_port
; /* Transport layer port # */
94 uint32_t sin6_flowinfo
; /* IPv6 flow information */
95 struct in6_addr sin6_addr
; /* IPv6 address */
98 #ifndef EAI_ADDRFAMILY
100 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
101 int ai_family
; /* PF_xxx */
102 int ai_socktype
; /* SOCK_xxx */
103 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
104 size_t ai_addrlen
; /* length of ai_addr */
105 char *ai_canonname
; /* canonical name for hostname */
106 struct sockaddr
*ai_addr
; /* binary address */
107 struct addrinfo
*ai_next
; /* next structure in linked list */
109 #endif /* EAI_ADDRFAMILY */
110 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
113 #include <netdb.h> /* for "struct addrinfo" */
115 #include <pcap/namedb.h>
117 #include "nametoaddr.h"
119 #define ETHERMTU 1500
121 #ifndef IPPROTO_HOPOPTS
122 #define IPPROTO_HOPOPTS 0
124 #ifndef IPPROTO_ROUTING
125 #define IPPROTO_ROUTING 43
127 #ifndef IPPROTO_FRAGMENT
128 #define IPPROTO_FRAGMENT 44
130 #ifndef IPPROTO_DSTOPTS
131 #define IPPROTO_DSTOPTS 60
134 #define IPPROTO_SCTP 132
137 #define GENEVE_PORT 6081
138 #define VXLAN_PORT 4789
142 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
146 #define ARCTYPE_IP_OLD 240 /* IP protocol */
147 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
150 #define ARCTYPE_IP 212 /* IP protocol */
151 #define ARCTYPE_ARP 213 /* address resolution protocol */
152 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
154 #define ARCTYPE_ATALK 221 /* Appletalk */
155 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
156 #define ARCTYPE_IPX 250 /* Novell IPX */
158 #define ARCTYPE_INET6 0xc4 /* IPng */
159 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
162 /* Based on UNI3.1 standard by ATM Forum */
164 /* ATM traffic types based on VPI=0 and (the following VCI */
165 #define VCI_PPC 0x05 /* Point-to-point signal msg */
166 #define VCI_BCC 0x02 /* Broadcast signal msg */
167 #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */
168 #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */
169 #define VCI_METAC 0x01 /* Meta signal msg */
170 #define VCI_ILMIC 0x10 /* ILMI msg */
172 /* Q.2931 signalling messages */
173 #define CALL_PROCEED 0x02 /* call proceeding */
174 #define CONNECT 0x07 /* connect */
175 #define CONNECT_ACK 0x0f /* connect_ack */
176 #define SETUP 0x05 /* setup */
177 #define RELEASE 0x4d /* release */
178 #define RELEASE_DONE 0x5a /* release_done */
179 #define RESTART 0x46 /* restart */
180 #define RESTART_ACK 0x4e /* restart ack */
181 #define STATUS 0x7d /* status */
182 #define STATUS_ENQ 0x75 /* status ack */
183 #define ADD_PARTY 0x80 /* add party */
184 #define ADD_PARTY_ACK 0x81 /* add party ack */
185 #define ADD_PARTY_REJ 0x82 /* add party rej */
186 #define DROP_PARTY 0x83 /* drop party */
187 #define DROP_PARTY_ACK 0x84 /* drop party ack */
189 /* Information Element Parameters in the signalling messages */
190 #define CAUSE 0x08 /* cause */
191 #define ENDPT_REF 0x54 /* endpoint reference */
192 #define AAL_PARA 0x58 /* ATM adaptation layer parameters */
193 #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */
194 #define CONNECT_ID 0x5a /* connection identifier */
195 #define QOS_PARA 0x5c /* quality of service parameters */
196 #define B_HIGHER 0x5d /* broadband higher layer information */
197 #define B_BEARER 0x5e /* broadband bearer capability */
198 #define B_LOWER 0x5f /* broadband lower information */
199 #define CALLING_PARTY 0x6c /* calling party number */
200 #define CALLED_PARTY 0x70 /* called party number */
204 /* Q.2931 signalling general messages format */
205 #define PROTO_POS 0 /* offset of protocol discriminator */
206 #define CALL_REF_POS 2 /* offset of call reference value */
207 #define MSG_TYPE_POS 5 /* offset of message type */
208 #define MSG_LEN_POS 7 /* offset of message length */
209 #define IE_BEGIN_POS 9 /* offset of first information element */
211 /* format of signalling messages */
214 #define FIELD_BEGIN_POS 4
217 /* SunATM header for ATM packet */
218 #define SUNATM_DIR_POS 0
219 #define SUNATM_VPI_POS 1
220 #define SUNATM_VCI_POS 2
221 #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */
223 /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */
224 #define PT_LANE 0x01 /* LANE */
225 #define PT_LLC 0x02 /* LLC encapsulation */
226 #define PT_ILMI 0x05 /* ILMI */
227 #define PT_QSAAL 0x06 /* Q.SAAL */
230 /* Types missing from some systems */
233 * Network layer protocol identifiers
236 #define ISO8473_CLNP 0x81
239 #define ISO9542_ESIS 0x82
241 #ifndef ISO9542X25_ESIS
242 #define ISO9542X25_ESIS 0x8a
244 #ifndef ISO10589_ISIS
245 #define ISO10589_ISIS 0x83
248 #define ISIS_L1_LAN_IIH 15
249 #define ISIS_L2_LAN_IIH 16
250 #define ISIS_PTP_IIH 17
251 #define ISIS_L1_LSP 18
252 #define ISIS_L2_LSP 20
253 #define ISIS_L1_CSNP 24
254 #define ISIS_L2_CSNP 25
255 #define ISIS_L1_PSNP 26
256 #define ISIS_L2_PSNP 27
258 #ifndef ISO8878A_CONS
259 #define ISO8878A_CONS 0x84
261 #ifndef ISO10747_IDRP
262 #define ISO10747_IDRP 0x85
265 // Same as in tcpdump/print-sl.c.
267 #define SLIPDIR_OUT 1
269 #ifdef HAVE_OS_PROTO_H
270 #include "os-proto.h"
273 #define JMP(c) ((c)|BPF_JMP|BPF_K)
276 * "Push" the current value of the link-layer header type and link-layer
277 * header offset onto a "stack", and set a new value. (It's not a
278 * full-blown stack; we keep only the top two items.)
280 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
282 (cs)->prevlinktype = (cs)->linktype; \
283 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
284 (cs)->linktype = (new_linktype); \
285 (cs)->off_linkhdr.is_variable = (new_is_variable); \
286 (cs)->off_linkhdr.constant_part = (new_constant_part); \
287 (cs)->off_linkhdr.reg = (new_reg); \
288 (cs)->is_encap = 0; \
292 * Offset "not set" value.
294 #define OFFSET_NOT_SET 0xffffffffU
297 * Absolute offsets, which are offsets from the beginning of the raw
298 * packet data, are, in the general case, the sum of a variable value
299 * and a constant value; the variable value may be absent, in which
300 * case the offset is only the constant value, and the constant value
301 * may be zero, in which case the offset is only the variable value.
303 * bpf_abs_offset is a structure containing all that information:
305 * is_variable is 1 if there's a variable part.
307 * constant_part is the constant part of the value, possibly zero;
309 * if is_variable is 1, reg is the register number for a register
310 * containing the variable value if the register has been assigned,
320 * Value passed to gen_load_a() to indicate what the offset argument
321 * is relative to the beginning of.
324 OR_PACKET
, /* full packet data */
325 OR_LINKHDR
, /* link-layer header */
326 OR_PREVLINKHDR
, /* previous link-layer header */
327 OR_LLC
, /* 802.2 LLC header */
328 OR_PREVMPLSHDR
, /* previous MPLS header */
329 OR_LINKTYPE
, /* link-layer type */
330 OR_LINKPL
, /* link-layer payload */
331 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
332 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
333 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
337 * We divvy out chunks of memory rather than call malloc each time so
338 * we don't have to worry about leaking memory. It's probably
339 * not a big deal if all this memory was wasted but if this ever
340 * goes into a library that would probably not be a good idea.
342 * XXX - this *is* in a library....
345 #define CHUNK0SIZE 1024
352 * A chunk can store any of:
353 * - a string (guaranteed alignment 1 but present for completeness)
357 * For this simple allocator every allocated chunk gets rounded up to the
358 * alignment needed for any chunk.
369 #define CHUNK_ALIGN (offsetof(struct chunk_align, u))
371 /* Code generator state */
373 struct _compiler_state
{
384 int outermostlinktype
;
389 /* Hack for handling VLAN and MPLS stacks. */
390 u_int label_stack_depth
;
391 u_int vlan_stack_depth
;
397 * As errors are handled by a longjmp, anything allocated must
398 * be freed in the longjmp handler, so it must be reachable
401 * One thing that's allocated is the result of pcap_nametoaddrinfo();
402 * it must be freed with freeaddrinfo(). This variable points to
403 * any addrinfo structure that would need to be freed.
408 * Another thing that's allocated is the result of pcap_ether_aton();
409 * it must be freed with free(). This variable points to any
410 * address that would need to be freed.
415 * Various code constructs need to know the layout of the packet.
416 * These values give the necessary offsets from the beginning
417 * of the packet data.
421 * Absolute offset of the beginning of the link-layer header.
423 bpf_abs_offset off_linkhdr
;
426 * If we're checking a link-layer header for a packet encapsulated
427 * in another protocol layer, this is the equivalent information
428 * for the previous layers' link-layer header from the beginning
429 * of the raw packet data.
431 bpf_abs_offset off_prevlinkhdr
;
434 * This is the equivalent information for the outermost layers'
437 bpf_abs_offset off_outermostlinkhdr
;
440 * Absolute offset of the beginning of the link-layer payload.
442 bpf_abs_offset off_linkpl
;
445 * "off_linktype" is the offset to information in the link-layer
446 * header giving the packet type. This is an absolute offset
447 * from the beginning of the packet.
449 * For Ethernet, it's the offset of the Ethernet type field; this
450 * means that it must have a value that skips VLAN tags.
452 * For link-layer types that always use 802.2 headers, it's the
453 * offset of the LLC header; this means that it must have a value
454 * that skips VLAN tags.
456 * For PPP, it's the offset of the PPP type field.
458 * For Cisco HDLC, it's the offset of the CHDLC type field.
460 * For BSD loopback, it's the offset of the AF_ value.
462 * For Linux cooked sockets, it's the offset of the type field.
464 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
465 * encapsulation, in which case, IP is assumed.
467 bpf_abs_offset off_linktype
;
470 * TRUE if the link layer includes an ATM pseudo-header.
474 /* TRUE if "geneve" or "vxlan" appeared in the filter; it
475 * causes us to generate code that checks for a Geneve or
476 * VXLAN header respectively and assume that later filters
477 * apply to the encapsulated payload.
482 * TRUE if we need variable length part of VLAN offset
484 int is_vlan_vloffset
;
487 * These are offsets for the ATM pseudo-header.
494 * These are offsets for the MTP2 fields.
500 * These are offsets for the MTP3 fields.
508 * This is the offset of the first byte after the ATM pseudo_header,
509 * or -1 if there is no ATM pseudo-header.
514 * These are offsets to the beginning of the network-layer header.
515 * They are relative to the beginning of the link-layer payload
516 * (i.e., they don't include off_linkhdr.constant_part or
517 * off_linkpl.constant_part).
519 * If the link layer never uses 802.2 LLC:
521 * "off_nl" and "off_nl_nosnap" are the same.
523 * If the link layer always uses 802.2 LLC:
525 * "off_nl" is the offset if there's a SNAP header following
528 * "off_nl_nosnap" is the offset if there's no SNAP header.
530 * If the link layer is Ethernet:
532 * "off_nl" is the offset if the packet is an Ethernet II packet
533 * (we assume no 802.3+802.2+SNAP);
535 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
536 * with an 802.2 header following it.
542 * Here we handle simple allocation of the scratch registers.
543 * If too many registers are alloc'd, the allocator punts.
545 int regused
[BPF_MEMWORDS
];
551 struct chunk chunks
[NCHUNKS
];
556 * For use by routines outside this file.
560 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
565 * If we've already set an error, don't override it.
566 * The lexical analyzer reports some errors by setting
567 * the error and then returning a LEX_ERROR token, which
568 * is not recognized by any grammar rule, and thus forces
569 * the parse to stop. We don't want the error reported
570 * by the lexical analyzer to be overwritten by the syntax
573 if (!cstate
->error_set
) {
575 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
578 cstate
->error_set
= 1;
583 * For use *ONLY* in routines in this file.
585 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
586 PCAP_PRINTFLIKE(2, 3);
589 static void PCAP_NORETURN
590 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
595 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
598 longjmp(cstate
->top_ctx
, 1);
605 static int init_linktype(compiler_state_t
*, pcap_t
*);
607 static void init_regs(compiler_state_t
*);
608 static int alloc_reg(compiler_state_t
*);
609 static void free_reg(compiler_state_t
*, int);
611 static void initchunks(compiler_state_t
*cstate
);
612 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
613 static void *newchunk(compiler_state_t
*cstate
, size_t);
614 static void freechunks(compiler_state_t
*cstate
);
615 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
616 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
617 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
618 static inline void syntax(compiler_state_t
*cstate
);
620 static void backpatch(struct block
*, struct block
*);
621 static void merge(struct block
*, struct block
*);
622 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
624 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
626 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
628 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
630 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
632 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
633 u_int
, bpf_u_int32
, bpf_u_int32
);
634 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
635 u_int
, const u_char
*);
636 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
637 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
638 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
640 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
642 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
643 static struct block
*gen_uncond(compiler_state_t
*, int);
644 static inline struct block
*gen_true(compiler_state_t
*);
645 static inline struct block
*gen_false(compiler_state_t
*);
646 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
647 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
648 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
649 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
650 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
651 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
652 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
653 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
654 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
655 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
657 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
658 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
659 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
660 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
661 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
664 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
665 struct in6_addr
*, int, u_int
, u_int
);
667 static struct block
*gen_ahostop(compiler_state_t
*, const uint8_t, int);
668 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
669 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
670 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
671 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
672 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
673 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
674 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
675 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
678 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
679 struct in6_addr
*, int, int, int);
682 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
683 struct addrinfo
*, int, int);
685 static struct block
*gen_ipfrag(compiler_state_t
*);
686 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
687 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
689 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
690 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
692 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
693 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
694 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
696 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
697 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
698 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
699 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
701 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
702 static int lookup_proto(compiler_state_t
*, const char *, int);
703 #if !defined(NO_PROTOCHAIN)
704 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
705 #endif /* !defined(NO_PROTOCHAIN) */
706 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
707 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
708 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
709 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
710 static struct block
*gen_len(compiler_state_t
*, int, int);
711 static struct block
*gen_encap_ll_check(compiler_state_t
*cstate
);
713 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
714 bpf_u_int32
, int, int);
715 static struct block
*gen_atmtype_llc(compiler_state_t
*);
716 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
719 initchunks(compiler_state_t
*cstate
)
723 for (i
= 0; i
< NCHUNKS
; i
++) {
724 cstate
->chunks
[i
].n_left
= 0;
725 cstate
->chunks
[i
].m
= NULL
;
727 cstate
->cur_chunk
= 0;
731 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
737 /* Round up to chunk alignment. */
738 n
= (n
+ CHUNK_ALIGN
- 1) & ~(CHUNK_ALIGN
- 1);
740 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
741 if (n
> cp
->n_left
) {
743 k
= ++cstate
->cur_chunk
;
745 bpf_set_error(cstate
, "out of memory");
748 size
= CHUNK0SIZE
<< k
;
749 cp
->m
= (void *)malloc(size
);
751 bpf_set_error(cstate
, "out of memory");
754 memset((char *)cp
->m
, 0, size
);
757 bpf_set_error(cstate
, "out of memory");
762 return (void *)((char *)cp
->m
+ cp
->n_left
);
766 newchunk(compiler_state_t
*cstate
, size_t n
)
770 p
= newchunk_nolongjmp(cstate
, n
);
772 longjmp(cstate
->top_ctx
, 1);
779 freechunks(compiler_state_t
*cstate
)
783 for (i
= 0; i
< NCHUNKS
; ++i
)
784 if (cstate
->chunks
[i
].m
!= NULL
)
785 free(cstate
->chunks
[i
].m
);
789 * A strdup whose allocations are freed after code generation is over.
790 * This is used by the lexical analyzer, so it can't longjmp; it just
791 * returns NULL on an allocation error, and the callers must check
795 sdup(compiler_state_t
*cstate
, const char *s
)
797 size_t n
= strlen(s
) + 1;
798 char *cp
= newchunk_nolongjmp(cstate
, n
);
802 pcapint_strlcpy(cp
, s
, n
);
806 static inline struct block
*
807 new_block(compiler_state_t
*cstate
, int code
)
811 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
818 static inline struct slist
*
819 new_stmt(compiler_state_t
*cstate
, int code
)
823 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
829 static struct block
*
830 gen_retblk_internal(compiler_state_t
*cstate
, int v
)
832 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
838 static struct block
*
839 gen_retblk(compiler_state_t
*cstate
, int v
)
841 if (setjmp(cstate
->top_ctx
)) {
843 * gen_retblk() only fails because a memory
844 * allocation failed in newchunk(), meaning
845 * that it can't return a pointer.
851 return gen_retblk_internal(cstate
, v
);
854 static inline PCAP_NORETURN_DEF
void
855 syntax(compiler_state_t
*cstate
)
857 bpf_error(cstate
, "syntax error in filter expression");
861 * For the given integer return a string with the keyword (or the nominal
862 * keyword if there is more than one). This is a simpler version of tok2str()
863 * in tcpdump because in this problem space a valid integer value is not
867 qual2kw(const char *kind
, const unsigned id
, const char *tokens
[],
870 static char buf
[4][64];
873 if (id
< size
&& tokens
[id
])
876 char *ret
= buf
[idx
];
877 idx
= (idx
+ 1) % (sizeof(buf
) / sizeof(buf
[0]));
878 ret
[0] = '\0'; // just in case
879 snprintf(ret
, sizeof(buf
[0]), "<invalid %s %u>", kind
, id
);
883 // protocol qualifier keywords
885 pqkw(const unsigned id
)
887 const char * tokens
[] = {
899 [Q_DECNET
] = "decnet",
905 [Q_ICMPV6
] = "icmp6",
917 [Q_NETBEUI
] = "netbeui",
920 [Q_ISIS_IIH
] = "iih",
921 [Q_ISIS_SNP
] = "snp",
922 [Q_ISIS_CSNP
] = "csnp",
923 [Q_ISIS_PSNP
] = "psnp",
924 [Q_ISIS_LSP
] = "lsp",
928 return qual2kw("proto", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
931 // direction qualifier keywords
933 dqkw(const unsigned id
)
935 const char * map
[] = {
938 [Q_OR
] = "src or dst",
939 [Q_AND
] = "src and dst",
947 return qual2kw("dir", id
, map
, sizeof(map
) / sizeof(map
[0]));
952 atmkw(const unsigned id
)
954 const char * tokens
[] = {
957 [A_OAMF4SC
] = "oamf4sc",
958 [A_OAMF4EC
] = "oamf4ec",
964 // no keyword for A_SETUP
965 // no keyword for A_CALLPROCEED
966 // no keyword for A_CONNECT
967 // no keyword for A_CONNECTACK
968 // no keyword for A_RELEASE
969 // no keyword for A_RELEASE_DONE
972 // no keyword for A_PROTOTYPE
973 // no keyword for A_MSGTYPE
974 [A_CONNECTMSG
] = "connectmsg",
975 [A_METACONNECT
] = "metaconnect",
977 return qual2kw("ATM keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
982 ss7kw(const unsigned id
)
984 const char * tokens
[] = {
1000 return qual2kw("MTP keyword", id
, tokens
, sizeof(tokens
) / sizeof(tokens
[0]));
1003 static PCAP_NORETURN_DEF
void
1004 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
1006 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
1007 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
1011 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
1013 if (cstate
->linktype
!= DLT_PFLOG
)
1014 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
1018 assert_atm(compiler_state_t
*cstate
, const char *kw
)
1021 * Belt and braces: init_linktype() sets either all of these struct
1022 * members (for DLT_SUNATM) or none (otherwise).
1024 if (cstate
->linktype
!= DLT_SUNATM
||
1026 cstate
->off_vpi
== OFFSET_NOT_SET
||
1027 cstate
->off_vci
== OFFSET_NOT_SET
||
1028 cstate
->off_proto
== OFFSET_NOT_SET
||
1029 cstate
->off_payload
== OFFSET_NOT_SET
)
1030 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1034 assert_ss7(compiler_state_t
*cstate
, const char *kw
)
1036 switch (cstate
->linktype
) {
1039 case DLT_MTP2_WITH_PHDR
:
1040 // Belt and braces, same as in assert_atm().
1041 if (cstate
->off_sio
!= OFFSET_NOT_SET
&&
1042 cstate
->off_opc
!= OFFSET_NOT_SET
&&
1043 cstate
->off_dpc
!= OFFSET_NOT_SET
&&
1044 cstate
->off_sls
!= OFFSET_NOT_SET
)
1047 bpf_error(cstate
, "'%s' supported only on SS7", kw
);
1050 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1051 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1054 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1055 const char *buf
, int optimize
, bpf_u_int32 mask
)
1061 compiler_state_t cstate
;
1062 yyscan_t scanner
= NULL
;
1063 YY_BUFFER_STATE in_buffer
= NULL
;
1068 * If this pcap_t hasn't been activated, it doesn't have a
1069 * link-layer type, so we can't use it.
1071 if (!p
->activated
) {
1072 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1073 "not-yet-activated pcap_t passed to pcap_compile");
1074 return (PCAP_ERROR
);
1079 * Initialize Winsock, asking for the latest version (2.2),
1080 * as we may be calling Winsock routines to translate
1081 * host names to addresses.
1083 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1085 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1086 err
, "Error calling WSAStartup()");
1087 return (PCAP_ERROR
);
1091 #ifdef ENABLE_REMOTE
1093 * If the device on which we're capturing need to be notified
1094 * that a new filter is being compiled, do so.
1096 * This allows them to save a copy of it, in case, for example,
1097 * they're implementing a form of remote packet capture, and
1098 * want the remote machine to filter out the packets in which
1099 * it's sending the packets it's captured.
1101 * XXX - the fact that we happen to be compiling a filter
1102 * doesn't necessarily mean we'll be installing it as the
1103 * filter for this pcap_t; we might be running it from userland
1104 * on captured packets to do packet classification. We really
1105 * need a better way of handling this, but this is all that
1106 * the WinPcap remote capture code did.
1108 if (p
->save_current_filter_op
!= NULL
)
1109 (p
->save_current_filter_op
)(p
, buf
);
1112 initchunks(&cstate
);
1113 cstate
.no_optimize
= 0;
1118 cstate
.ic
.root
= NULL
;
1119 cstate
.ic
.cur_mark
= 0;
1120 cstate
.bpf_pcap
= p
;
1121 cstate
.error_set
= 0;
1124 cstate
.netmask
= mask
;
1126 cstate
.snaplen
= pcap_snapshot(p
);
1127 if (cstate
.snaplen
== 0) {
1128 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1129 "snaplen of 0 rejects all packets");
1134 if (pcap_lex_init(&scanner
) != 0) {
1135 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1136 errno
, "can't initialize scanner");
1140 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1143 * Associate the compiler state with the lexical analyzer
1146 pcap_set_extra(&cstate
, scanner
);
1148 if (init_linktype(&cstate
, p
) == -1) {
1152 if (pcap_parse(scanner
, &cstate
) != 0) {
1154 if (cstate
.ai
!= NULL
)
1155 freeaddrinfo(cstate
.ai
);
1157 if (cstate
.e
!= NULL
)
1163 if (cstate
.ic
.root
== NULL
) {
1164 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1167 * Catch errors reported by gen_retblk().
1169 if (cstate
.ic
.root
== NULL
) {
1175 if (optimize
&& !cstate
.no_optimize
) {
1176 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1181 if (cstate
.ic
.root
== NULL
||
1182 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1183 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1184 "expression rejects all packets");
1189 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1190 cstate
.ic
.root
, &len
, p
->errbuf
);
1191 if (program
->bf_insns
== NULL
) {
1196 program
->bf_len
= len
;
1198 rc
= 0; /* We're all okay */
1202 * Clean up everything for the lexical analyzer.
1204 if (in_buffer
!= NULL
)
1205 pcap__delete_buffer(in_buffer
, scanner
);
1206 if (scanner
!= NULL
)
1207 pcap_lex_destroy(scanner
);
1210 * Clean up our own allocated memory.
1212 freechunks(&cstate
);
1222 * entry point for using the compiler with no pcap open
1223 * pass in all the stuff that is needed explicitly instead.
1226 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1227 struct bpf_program
*program
,
1228 const char *buf
, int optimize
, bpf_u_int32 mask
)
1233 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1235 return (PCAP_ERROR
);
1236 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1242 * Clean up a "struct bpf_program" by freeing all the memory allocated
1246 pcap_freecode(struct bpf_program
*program
)
1248 program
->bf_len
= 0;
1249 if (program
->bf_insns
!= NULL
) {
1250 free((char *)program
->bf_insns
);
1251 program
->bf_insns
= NULL
;
1256 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1257 * which of the jt and jf fields has been resolved and which is a pointer
1258 * back to another unresolved block (or nil). At least one of the fields
1259 * in each block is already resolved.
1262 backpatch(struct block
*list
, struct block
*target
)
1279 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1280 * which of jt and jf is the link.
1283 merge(struct block
*b0
, struct block
*b1
)
1285 register struct block
**p
= &b0
;
1287 /* Find end of list. */
1289 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1291 /* Concatenate the lists. */
1296 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1299 * Catch errors reported by us and routines below us, and return -1
1302 if (setjmp(cstate
->top_ctx
))
1306 * Insert before the statements of the first (root) block any
1307 * statements needed to load the lengths of any variable-length
1308 * headers into registers.
1310 * XXX - a fancier strategy would be to insert those before the
1311 * statements of all blocks that use those lengths and that
1312 * have no predecessors that use them, so that we only compute
1313 * the lengths if we need them. There might be even better
1314 * approaches than that.
1316 * However, those strategies would be more complicated, and
1317 * as we don't generate code to compute a length if the
1318 * program has no tests that use the length, and as most
1319 * tests will probably use those lengths, we would just
1320 * postpone computing the lengths so that it's not done
1321 * for tests that fail early, and it's not clear that's
1324 insert_compute_vloffsets(cstate
, p
->head
);
1327 * For DLT_PPI captures, generate a check of the per-packet
1328 * DLT value to make sure it's DLT_IEEE802_11.
1330 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1331 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1332 * with appropriate Ethernet information and use that rather
1333 * than using something such as DLT_PPI where you don't know
1334 * the link-layer header type until runtime, which, in the
1335 * general case, would force us to generate both Ethernet *and*
1336 * 802.11 code (*and* anything else for which PPI is used)
1337 * and choose between them early in the BPF program?
1339 if (cstate
->linktype
== DLT_PPI
) {
1340 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1341 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1342 gen_and(ppi_dlt_check
, p
);
1345 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1346 p
->sense
= !p
->sense
;
1347 backpatch(p
, gen_retblk_internal(cstate
, 0));
1348 cstate
->ic
.root
= p
->head
;
1353 gen_and(struct block
*b0
, struct block
*b1
)
1355 backpatch(b0
, b1
->head
);
1356 b0
->sense
= !b0
->sense
;
1357 b1
->sense
= !b1
->sense
;
1359 b1
->sense
= !b1
->sense
;
1360 b1
->head
= b0
->head
;
1364 gen_or(struct block
*b0
, struct block
*b1
)
1366 b0
->sense
= !b0
->sense
;
1367 backpatch(b0
, b1
->head
);
1368 b0
->sense
= !b0
->sense
;
1370 b1
->head
= b0
->head
;
1374 gen_not(struct block
*b
)
1376 b
->sense
= !b
->sense
;
1379 static struct block
*
1380 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1381 u_int size
, bpf_u_int32 v
)
1383 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1386 static struct block
*
1387 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1388 u_int size
, bpf_u_int32 v
)
1390 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1393 static struct block
*
1394 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1395 u_int size
, bpf_u_int32 v
)
1397 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1400 static struct block
*
1401 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1402 u_int size
, bpf_u_int32 v
)
1404 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1407 static struct block
*
1408 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1409 u_int size
, bpf_u_int32 v
)
1411 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1414 static struct block
*
1415 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1416 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1418 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1421 static struct block
*
1422 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1423 u_int size
, const u_char
*v
)
1425 register struct block
*b
, *tmp
;
1429 register const u_char
*p
= &v
[size
- 4];
1431 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1439 register const u_char
*p
= &v
[size
- 2];
1441 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1449 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1458 * AND the field of size "size" at offset "offset" relative to the header
1459 * specified by "offrel" with "mask", and compare it with the value "v"
1460 * with the test specified by "jtype"; if "reverse" is true, the test
1461 * should test the opposite of "jtype".
1463 static struct block
*
1464 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1465 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1468 struct slist
*s
, *s2
;
1471 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1473 if (mask
!= 0xffffffff) {
1474 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1479 b
= new_block(cstate
, JMP(jtype
));
1488 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1490 cstate
->pcap_fddipad
= p
->fddipad
;
1493 * We start out with only one link-layer header.
1495 cstate
->outermostlinktype
= pcap_datalink(p
);
1496 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1497 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1498 cstate
->off_outermostlinkhdr
.reg
= -1;
1500 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1501 cstate
->off_prevlinkhdr
.constant_part
= 0;
1502 cstate
->off_prevlinkhdr
.is_variable
= 0;
1503 cstate
->off_prevlinkhdr
.reg
= -1;
1505 cstate
->linktype
= cstate
->outermostlinktype
;
1506 cstate
->off_linkhdr
.constant_part
= 0;
1507 cstate
->off_linkhdr
.is_variable
= 0;
1508 cstate
->off_linkhdr
.reg
= -1;
1513 cstate
->off_linkpl
.constant_part
= 0;
1514 cstate
->off_linkpl
.is_variable
= 0;
1515 cstate
->off_linkpl
.reg
= -1;
1517 cstate
->off_linktype
.constant_part
= 0;
1518 cstate
->off_linktype
.is_variable
= 0;
1519 cstate
->off_linktype
.reg
= -1;
1522 * Assume it's not raw ATM with a pseudo-header, for now.
1525 cstate
->off_vpi
= OFFSET_NOT_SET
;
1526 cstate
->off_vci
= OFFSET_NOT_SET
;
1527 cstate
->off_proto
= OFFSET_NOT_SET
;
1528 cstate
->off_payload
= OFFSET_NOT_SET
;
1531 * And not encapsulated with either Geneve or VXLAN.
1533 cstate
->is_encap
= 0;
1536 * No variable length VLAN offset by default
1538 cstate
->is_vlan_vloffset
= 0;
1541 * And assume we're not doing SS7.
1543 cstate
->off_li
= OFFSET_NOT_SET
;
1544 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1545 cstate
->off_sio
= OFFSET_NOT_SET
;
1546 cstate
->off_opc
= OFFSET_NOT_SET
;
1547 cstate
->off_dpc
= OFFSET_NOT_SET
;
1548 cstate
->off_sls
= OFFSET_NOT_SET
;
1550 cstate
->label_stack_depth
= 0;
1551 cstate
->vlan_stack_depth
= 0;
1553 switch (cstate
->linktype
) {
1556 cstate
->off_linktype
.constant_part
= 2;
1557 cstate
->off_linkpl
.constant_part
= 6;
1558 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1559 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1562 case DLT_ARCNET_LINUX
:
1563 cstate
->off_linktype
.constant_part
= 4;
1564 cstate
->off_linkpl
.constant_part
= 8;
1565 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1566 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1570 cstate
->off_linktype
.constant_part
= 12;
1571 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1572 cstate
->off_nl
= 0; /* Ethernet II */
1573 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1578 * SLIP doesn't have a link level type. The 16 byte
1579 * header is hacked into our SLIP driver.
1581 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1582 cstate
->off_linkpl
.constant_part
= 16;
1584 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1587 case DLT_SLIP_BSDOS
:
1588 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1589 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1591 cstate
->off_linkpl
.constant_part
= 24;
1593 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1598 cstate
->off_linktype
.constant_part
= 0;
1599 cstate
->off_linkpl
.constant_part
= 4;
1601 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1605 cstate
->off_linktype
.constant_part
= 0;
1606 cstate
->off_linkpl
.constant_part
= 12;
1608 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1613 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1614 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1615 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1616 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1617 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1619 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1624 * This does not include the Ethernet header, and
1625 * only covers session state.
1627 cstate
->off_linktype
.constant_part
= 6;
1628 cstate
->off_linkpl
.constant_part
= 8;
1630 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1634 cstate
->off_linktype
.constant_part
= 5;
1635 cstate
->off_linkpl
.constant_part
= 24;
1637 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1642 * FDDI doesn't really have a link-level type field.
1643 * We set "off_linktype" to the offset of the LLC header.
1645 * To check for Ethernet types, we assume that SSAP = SNAP
1646 * is being used and pick out the encapsulated Ethernet type.
1647 * XXX - should we generate code to check for SNAP?
1649 cstate
->off_linktype
.constant_part
= 13;
1650 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1651 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1652 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1653 cstate
->off_nl
= 8; /* 802.2+SNAP */
1654 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1659 * Token Ring doesn't really have a link-level type field.
1660 * We set "off_linktype" to the offset of the LLC header.
1662 * To check for Ethernet types, we assume that SSAP = SNAP
1663 * is being used and pick out the encapsulated Ethernet type.
1664 * XXX - should we generate code to check for SNAP?
1666 * XXX - the header is actually variable-length.
1667 * Some various Linux patched versions gave 38
1668 * as "off_linktype" and 40 as "off_nl"; however,
1669 * if a token ring packet has *no* routing
1670 * information, i.e. is not source-routed, the correct
1671 * values are 20 and 22, as they are in the vanilla code.
1673 * A packet is source-routed iff the uppermost bit
1674 * of the first byte of the source address, at an
1675 * offset of 8, has the uppermost bit set. If the
1676 * packet is source-routed, the total number of bytes
1677 * of routing information is 2 plus bits 0x1F00 of
1678 * the 16-bit value at an offset of 14 (shifted right
1679 * 8 - figure out which byte that is).
1681 cstate
->off_linktype
.constant_part
= 14;
1682 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1683 cstate
->off_nl
= 8; /* 802.2+SNAP */
1684 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1687 case DLT_PRISM_HEADER
:
1688 case DLT_IEEE802_11_RADIO_AVS
:
1689 case DLT_IEEE802_11_RADIO
:
1690 cstate
->off_linkhdr
.is_variable
= 1;
1691 /* Fall through, 802.11 doesn't have a variable link
1692 * prefix but is otherwise the same. */
1695 case DLT_IEEE802_11
:
1697 * 802.11 doesn't really have a link-level type field.
1698 * We set "off_linktype.constant_part" to the offset of
1701 * To check for Ethernet types, we assume that SSAP = SNAP
1702 * is being used and pick out the encapsulated Ethernet type.
1703 * XXX - should we generate code to check for SNAP?
1705 * We also handle variable-length radio headers here.
1706 * The Prism header is in theory variable-length, but in
1707 * practice it's always 144 bytes long. However, some
1708 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1709 * sometimes or always supply an AVS header, so we
1710 * have to check whether the radio header is a Prism
1711 * header or an AVS header, so, in practice, it's
1714 cstate
->off_linktype
.constant_part
= 24;
1715 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1716 cstate
->off_linkpl
.is_variable
= 1;
1717 cstate
->off_nl
= 8; /* 802.2+SNAP */
1718 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1723 * At the moment we treat PPI the same way that we treat
1724 * normal Radiotap encoded packets. The difference is in
1725 * the function that generates the code at the beginning
1726 * to compute the header length. Since this code generator
1727 * of PPI supports bare 802.11 encapsulation only (i.e.
1728 * the encapsulated DLT should be DLT_IEEE802_11) we
1729 * generate code to check for this too.
1731 cstate
->off_linktype
.constant_part
= 24;
1732 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1733 cstate
->off_linkpl
.is_variable
= 1;
1734 cstate
->off_linkhdr
.is_variable
= 1;
1735 cstate
->off_nl
= 8; /* 802.2+SNAP */
1736 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1739 case DLT_ATM_RFC1483
:
1740 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1742 * assume routed, non-ISO PDUs
1743 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1745 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1746 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1747 * latter would presumably be treated the way PPPoE
1748 * should be, so you can do "pppoe and udp port 2049"
1749 * or "pppoa and tcp port 80" and have it check for
1750 * PPPo{A,E} and a PPP protocol of IP and....
1752 cstate
->off_linktype
.constant_part
= 0;
1753 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1754 cstate
->off_nl
= 8; /* 802.2+SNAP */
1755 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1760 * Full Frontal ATM; you get AALn PDUs with an ATM
1764 cstate
->off_vpi
= SUNATM_VPI_POS
;
1765 cstate
->off_vci
= SUNATM_VCI_POS
;
1766 cstate
->off_proto
= PROTO_POS
;
1767 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1768 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1769 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1770 cstate
->off_nl
= 8; /* 802.2+SNAP */
1771 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1777 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1778 cstate
->off_linkpl
.constant_part
= 0;
1780 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1783 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1784 cstate
->off_linktype
.constant_part
= 14;
1785 cstate
->off_linkpl
.constant_part
= 16;
1787 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1790 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1791 cstate
->off_linktype
.constant_part
= 0;
1792 cstate
->off_linkpl
.constant_part
= 20;
1794 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1799 * LocalTalk does have a 1-byte type field in the LLAP header,
1800 * but really it just indicates whether there is a "short" or
1801 * "long" DDP packet following.
1803 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1804 cstate
->off_linkpl
.constant_part
= 0;
1806 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1809 case DLT_IP_OVER_FC
:
1811 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1812 * link-level type field. We set "off_linktype" to the
1813 * offset of the LLC header.
1815 * To check for Ethernet types, we assume that SSAP = SNAP
1816 * is being used and pick out the encapsulated Ethernet type.
1817 * XXX - should we generate code to check for SNAP? RFC
1818 * 2625 says SNAP should be used.
1820 cstate
->off_linktype
.constant_part
= 16;
1821 cstate
->off_linkpl
.constant_part
= 16;
1822 cstate
->off_nl
= 8; /* 802.2+SNAP */
1823 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1828 * XXX - we should set this to handle SNAP-encapsulated
1829 * frames (NLPID of 0x80).
1831 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1832 cstate
->off_linkpl
.constant_part
= 0;
1834 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1838 * the only BPF-interesting FRF.16 frames are non-control frames;
1839 * Frame Relay has a variable length link-layer
1840 * so lets start with offset 4 for now and increments later on (FIXME);
1843 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1844 cstate
->off_linkpl
.constant_part
= 0;
1846 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1849 case DLT_APPLE_IP_OVER_IEEE1394
:
1850 cstate
->off_linktype
.constant_part
= 16;
1851 cstate
->off_linkpl
.constant_part
= 18;
1853 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1856 case DLT_SYMANTEC_FIREWALL
:
1857 cstate
->off_linktype
.constant_part
= 6;
1858 cstate
->off_linkpl
.constant_part
= 44;
1859 cstate
->off_nl
= 0; /* Ethernet II */
1860 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1864 cstate
->off_linktype
.constant_part
= 0;
1865 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1866 cstate
->off_linkpl
.is_variable
= 1;
1868 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1871 case DLT_JUNIPER_MFR
:
1872 case DLT_JUNIPER_MLFR
:
1873 case DLT_JUNIPER_MLPPP
:
1874 case DLT_JUNIPER_PPP
:
1875 case DLT_JUNIPER_CHDLC
:
1876 case DLT_JUNIPER_FRELAY
:
1877 cstate
->off_linktype
.constant_part
= 4;
1878 cstate
->off_linkpl
.constant_part
= 4;
1880 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1883 case DLT_JUNIPER_ATM1
:
1884 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1885 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1887 cstate
->off_nl_nosnap
= 10;
1890 case DLT_JUNIPER_ATM2
:
1891 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1892 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1894 cstate
->off_nl_nosnap
= 10;
1897 /* frames captured on a Juniper PPPoE service PIC
1898 * contain raw ethernet frames */
1899 case DLT_JUNIPER_PPPOE
:
1900 case DLT_JUNIPER_ETHER
:
1901 cstate
->off_linkpl
.constant_part
= 14;
1902 cstate
->off_linktype
.constant_part
= 16;
1903 cstate
->off_nl
= 18; /* Ethernet II */
1904 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1907 case DLT_JUNIPER_PPPOE_ATM
:
1908 cstate
->off_linktype
.constant_part
= 4;
1909 cstate
->off_linkpl
.constant_part
= 6;
1911 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1914 case DLT_JUNIPER_GGSN
:
1915 cstate
->off_linktype
.constant_part
= 6;
1916 cstate
->off_linkpl
.constant_part
= 12;
1918 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1921 case DLT_JUNIPER_ES
:
1922 cstate
->off_linktype
.constant_part
= 6;
1923 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1924 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1925 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1928 case DLT_JUNIPER_MONITOR
:
1929 cstate
->off_linktype
.constant_part
= 12;
1930 cstate
->off_linkpl
.constant_part
= 12;
1931 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1932 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1935 case DLT_BACNET_MS_TP
:
1936 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1937 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1938 cstate
->off_nl
= OFFSET_NOT_SET
;
1939 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1942 case DLT_JUNIPER_SERVICES
:
1943 cstate
->off_linktype
.constant_part
= 12;
1944 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1945 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1946 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1949 case DLT_JUNIPER_VP
:
1950 cstate
->off_linktype
.constant_part
= 18;
1951 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1952 cstate
->off_nl
= OFFSET_NOT_SET
;
1953 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1956 case DLT_JUNIPER_ST
:
1957 cstate
->off_linktype
.constant_part
= 18;
1958 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1959 cstate
->off_nl
= OFFSET_NOT_SET
;
1960 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1963 case DLT_JUNIPER_ISM
:
1964 cstate
->off_linktype
.constant_part
= 8;
1965 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1966 cstate
->off_nl
= OFFSET_NOT_SET
;
1967 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1970 case DLT_JUNIPER_VS
:
1971 case DLT_JUNIPER_SRX_E2E
:
1972 case DLT_JUNIPER_FIBRECHANNEL
:
1973 case DLT_JUNIPER_ATM_CEMIC
:
1974 cstate
->off_linktype
.constant_part
= 8;
1975 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1976 cstate
->off_nl
= OFFSET_NOT_SET
;
1977 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1982 cstate
->off_li_hsl
= 4;
1983 cstate
->off_sio
= 3;
1984 cstate
->off_opc
= 4;
1985 cstate
->off_dpc
= 4;
1986 cstate
->off_sls
= 7;
1987 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1988 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1989 cstate
->off_nl
= OFFSET_NOT_SET
;
1990 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1993 case DLT_MTP2_WITH_PHDR
:
1995 cstate
->off_li_hsl
= 8;
1996 cstate
->off_sio
= 7;
1997 cstate
->off_opc
= 8;
1998 cstate
->off_dpc
= 8;
1999 cstate
->off_sls
= 11;
2000 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2001 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2002 cstate
->off_nl
= OFFSET_NOT_SET
;
2003 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2007 cstate
->off_li
= 22;
2008 cstate
->off_li_hsl
= 24;
2009 cstate
->off_sio
= 23;
2010 cstate
->off_opc
= 24;
2011 cstate
->off_dpc
= 24;
2012 cstate
->off_sls
= 27;
2013 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2014 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2015 cstate
->off_nl
= OFFSET_NOT_SET
;
2016 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2020 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2021 cstate
->off_linkpl
.constant_part
= 4;
2023 cstate
->off_nl_nosnap
= 0;
2028 * Currently, only raw "link[N:M]" filtering is supported.
2030 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
2031 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2032 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
2033 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
2037 cstate
->off_linktype
.constant_part
= 1;
2038 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2040 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2043 case DLT_NETANALYZER
:
2044 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
2045 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2046 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
2047 cstate
->off_nl
= 0; /* Ethernet II */
2048 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2051 case DLT_NETANALYZER_TRANSPARENT
:
2052 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2053 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2054 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2055 cstate
->off_nl
= 0; /* Ethernet II */
2056 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2061 * For values in the range in which we've assigned new
2062 * DLT_ values, only raw "link[N:M]" filtering is supported.
2064 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2065 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2066 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2067 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2068 cstate
->off_nl
= OFFSET_NOT_SET
;
2069 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2071 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2072 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2078 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2083 * Load a value relative to the specified absolute offset.
2085 static struct slist
*
2086 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2087 u_int offset
, u_int size
)
2089 struct slist
*s
, *s2
;
2091 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2094 * If "s" is non-null, it has code to arrange that the X register
2095 * contains the variable part of the absolute offset, so we
2096 * generate a load relative to that, with an offset of
2097 * abs_offset->constant_part + offset.
2099 * Otherwise, we can do an absolute load with an offset of
2100 * abs_offset->constant_part + offset.
2104 * "s" points to a list of statements that puts the
2105 * variable part of the absolute offset into the X register.
2106 * Do an indirect load, to use the X register as an offset.
2108 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2109 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2113 * There is no variable part of the absolute offset, so
2114 * just do an absolute load.
2116 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2117 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2123 * Load a value relative to the beginning of the specified header.
2125 static struct slist
*
2126 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2129 struct slist
*s
, *s2
;
2132 * Squelch warnings from compilers that *don't* assume that
2133 * offrel always has a valid enum value and therefore don't
2134 * assume that we'll always go through one of the case arms.
2136 * If we have a default case, compilers that *do* assume that
2137 * will then complain about the default case code being
2140 * Damned if you do, damned if you don't.
2147 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2152 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2155 case OR_PREVLINKHDR
:
2156 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2160 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2163 case OR_PREVMPLSHDR
:
2164 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2168 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2171 case OR_LINKPL_NOSNAP
:
2172 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2176 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2181 * Load the X register with the length of the IPv4 header
2182 * (plus the offset of the link-layer header, if it's
2183 * preceded by a variable-length header such as a radio
2184 * header), in bytes.
2186 s
= gen_loadx_iphdrlen(cstate
);
2189 * Load the item at {offset of the link-layer payload} +
2190 * {offset, relative to the start of the link-layer
2191 * payload, of the IPv4 header} + {length of the IPv4 header} +
2192 * {specified offset}.
2194 * If the offset of the link-layer payload is variable,
2195 * the variable part of that offset is included in the
2196 * value in the X register, and we include the constant
2197 * part in the offset of the load.
2199 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2200 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2205 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2212 * Generate code to load into the X register the sum of the length of
2213 * the IPv4 header and the variable part of the offset of the link-layer
2216 static struct slist
*
2217 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2219 struct slist
*s
, *s2
;
2221 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2224 * The offset of the link-layer payload has a variable
2225 * part. "s" points to a list of statements that put
2226 * the variable part of that offset into the X register.
2228 * The 4*([k]&0xf) addressing mode can't be used, as we
2229 * don't have a constant offset, so we have to load the
2230 * value in question into the A register and add to it
2231 * the value from the X register.
2233 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2234 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2236 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2239 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2244 * The A register now contains the length of the IP header.
2245 * We need to add to it the variable part of the offset of
2246 * the link-layer payload, which is still in the X
2247 * register, and move the result into the X register.
2249 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2250 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2253 * The offset of the link-layer payload is a constant,
2254 * so no code was generated to load the (nonexistent)
2255 * variable part of that offset.
2257 * This means we can use the 4*([k]&0xf) addressing
2258 * mode. Load the length of the IPv4 header, which
2259 * is at an offset of cstate->off_nl from the beginning of
2260 * the link-layer payload, and thus at an offset of
2261 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2262 * of the raw packet data, using that addressing mode.
2264 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2265 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2271 static struct block
*
2272 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2277 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2279 b
= new_block(cstate
, JMP(BPF_JEQ
));
2285 static inline struct block
*
2286 gen_true(compiler_state_t
*cstate
)
2288 return gen_uncond(cstate
, 1);
2291 static inline struct block
*
2292 gen_false(compiler_state_t
*cstate
)
2294 return gen_uncond(cstate
, 0);
2298 * Generate code to match a particular packet type.
2300 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2301 * value, if <= ETHERMTU. We use that to determine whether to
2302 * match the type/length field or to check the type/length field for
2303 * a value <= ETHERMTU to see whether it's a type field and then do
2304 * the appropriate test.
2306 static struct block
*
2307 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2309 struct block
*b0
, *b1
;
2315 case LLCSAP_NETBEUI
:
2317 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2318 * so we check the DSAP and SSAP.
2320 * LLCSAP_IP checks for IP-over-802.2, rather
2321 * than IP-over-Ethernet or IP-over-SNAP.
2323 * XXX - should we check both the DSAP and the
2324 * SSAP, like this, or should we check just the
2325 * DSAP, as we do for other types <= ETHERMTU
2326 * (i.e., other SAP values)?
2328 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2329 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2337 * Ethernet_II frames, which are Ethernet
2338 * frames with a frame type of ETHERTYPE_IPX;
2340 * Ethernet_802.3 frames, which are 802.3
2341 * frames (i.e., the type/length field is
2342 * a length field, <= ETHERMTU, rather than
2343 * a type field) with the first two bytes
2344 * after the Ethernet/802.3 header being
2347 * Ethernet_802.2 frames, which are 802.3
2348 * frames with an 802.2 LLC header and
2349 * with the IPX LSAP as the DSAP in the LLC
2352 * Ethernet_SNAP frames, which are 802.3
2353 * frames with an LLC header and a SNAP
2354 * header and with an OUI of 0x000000
2355 * (encapsulated Ethernet) and a protocol
2356 * ID of ETHERTYPE_IPX in the SNAP header.
2358 * XXX - should we generate the same code both
2359 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2363 * This generates code to check both for the
2364 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2366 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2367 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2371 * Now we add code to check for SNAP frames with
2372 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2374 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2378 * Now we generate code to check for 802.3
2379 * frames in general.
2381 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2384 * Now add the check for 802.3 frames before the
2385 * check for Ethernet_802.2 and Ethernet_802.3,
2386 * as those checks should only be done on 802.3
2387 * frames, not on Ethernet frames.
2392 * Now add the check for Ethernet_II frames, and
2393 * do that before checking for the other frame
2396 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2400 case ETHERTYPE_ATALK
:
2401 case ETHERTYPE_AARP
:
2403 * EtherTalk (AppleTalk protocols on Ethernet link
2404 * layer) may use 802.2 encapsulation.
2408 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2409 * we check for an Ethernet type field less or equal than
2410 * 1500, which means it's an 802.3 length field.
2412 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2415 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2416 * SNAP packets with an organization code of
2417 * 0x080007 (Apple, for Appletalk) and a protocol
2418 * type of ETHERTYPE_ATALK (Appletalk).
2420 * 802.2-encapsulated ETHERTYPE_AARP packets are
2421 * SNAP packets with an organization code of
2422 * 0x000000 (encapsulated Ethernet) and a protocol
2423 * type of ETHERTYPE_AARP (Appletalk ARP).
2425 if (ll_proto
== ETHERTYPE_ATALK
)
2426 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2427 else /* ll_proto == ETHERTYPE_AARP */
2428 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2432 * Check for Ethernet encapsulation (Ethertalk
2433 * phase 1?); we just check for the Ethernet
2436 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2442 if (ll_proto
<= ETHERMTU
) {
2444 * This is an LLC SAP value, so the frames
2445 * that match would be 802.2 frames.
2446 * Check that the frame is an 802.2 frame
2447 * (i.e., that the length/type field is
2448 * a length field, <= ETHERMTU) and
2449 * then check the DSAP.
2451 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2452 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2457 * This is an Ethernet type, so compare
2458 * the length/type field with it (if
2459 * the frame is an 802.2 frame, the length
2460 * field will be <= ETHERMTU, and, as
2461 * "ll_proto" is > ETHERMTU, this test
2462 * will fail and the frame won't match,
2463 * which is what we want).
2465 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2470 static struct block
*
2471 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2474 * For DLT_NULL, the link-layer header is a 32-bit word
2475 * containing an AF_ value in *host* byte order, and for
2476 * DLT_ENC, the link-layer header begins with a 32-bit
2477 * word containing an AF_ value in host byte order.
2479 * In addition, if we're reading a saved capture file,
2480 * the host byte order in the capture may not be the
2481 * same as the host byte order on this machine.
2483 * For DLT_LOOP, the link-layer header is a 32-bit
2484 * word containing an AF_ value in *network* byte order.
2486 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2488 * The AF_ value is in host byte order, but the BPF
2489 * interpreter will convert it to network byte order.
2491 * If this is a save file, and it's from a machine
2492 * with the opposite byte order to ours, we byte-swap
2495 * Then we run it through "htonl()", and generate
2496 * code to compare against the result.
2498 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2499 ll_proto
= SWAPLONG(ll_proto
);
2500 ll_proto
= htonl(ll_proto
);
2502 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2506 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2507 * or IPv6 then we have an error.
2509 static struct block
*
2510 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2515 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2518 case ETHERTYPE_IPV6
:
2519 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2526 return gen_false(cstate
);
2530 * Generate code to match a particular packet type.
2532 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2533 * value, if <= ETHERMTU. We use that to determine whether to
2534 * match the type field or to check the type field for the special
2535 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2537 static struct block
*
2538 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2540 struct block
*b0
, *b1
;
2546 case LLCSAP_NETBEUI
:
2548 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2549 * so we check the DSAP and SSAP.
2551 * LLCSAP_IP checks for IP-over-802.2, rather
2552 * than IP-over-Ethernet or IP-over-SNAP.
2554 * XXX - should we check both the DSAP and the
2555 * SSAP, like this, or should we check just the
2556 * DSAP, as we do for other types <= ETHERMTU
2557 * (i.e., other SAP values)?
2559 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2560 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2566 * Ethernet_II frames, which are Ethernet
2567 * frames with a frame type of ETHERTYPE_IPX;
2569 * Ethernet_802.3 frames, which have a frame
2570 * type of LINUX_SLL_P_802_3;
2572 * Ethernet_802.2 frames, which are 802.3
2573 * frames with an 802.2 LLC header (i.e, have
2574 * a frame type of LINUX_SLL_P_802_2) and
2575 * with the IPX LSAP as the DSAP in the LLC
2578 * Ethernet_SNAP frames, which are 802.3
2579 * frames with an LLC header and a SNAP
2580 * header and with an OUI of 0x000000
2581 * (encapsulated Ethernet) and a protocol
2582 * ID of ETHERTYPE_IPX in the SNAP header.
2584 * First, do the checks on LINUX_SLL_P_802_2
2585 * frames; generate the check for either
2586 * Ethernet_802.2 or Ethernet_SNAP frames, and
2587 * then put a check for LINUX_SLL_P_802_2 frames
2590 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2591 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2593 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2597 * Now check for 802.3 frames and OR that with
2598 * the previous test.
2600 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2604 * Now add the check for Ethernet_II frames, and
2605 * do that before checking for the other frame
2608 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2612 case ETHERTYPE_ATALK
:
2613 case ETHERTYPE_AARP
:
2615 * EtherTalk (AppleTalk protocols on Ethernet link
2616 * layer) may use 802.2 encapsulation.
2620 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2621 * we check for the 802.2 protocol type in the
2622 * "Ethernet type" field.
2624 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2627 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2628 * SNAP packets with an organization code of
2629 * 0x080007 (Apple, for Appletalk) and a protocol
2630 * type of ETHERTYPE_ATALK (Appletalk).
2632 * 802.2-encapsulated ETHERTYPE_AARP packets are
2633 * SNAP packets with an organization code of
2634 * 0x000000 (encapsulated Ethernet) and a protocol
2635 * type of ETHERTYPE_AARP (Appletalk ARP).
2637 if (ll_proto
== ETHERTYPE_ATALK
)
2638 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2639 else /* ll_proto == ETHERTYPE_AARP */
2640 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2644 * Check for Ethernet encapsulation (Ethertalk
2645 * phase 1?); we just check for the Ethernet
2648 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2654 if (ll_proto
<= ETHERMTU
) {
2656 * This is an LLC SAP value, so the frames
2657 * that match would be 802.2 frames.
2658 * Check for the 802.2 protocol type
2659 * in the "Ethernet type" field, and
2660 * then check the DSAP.
2662 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2663 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2669 * This is an Ethernet type, so compare
2670 * the length/type field with it (if
2671 * the frame is an 802.2 frame, the length
2672 * field will be <= ETHERMTU, and, as
2673 * "ll_proto" is > ETHERMTU, this test
2674 * will fail and the frame won't match,
2675 * which is what we want).
2677 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2683 * Load a value relative to the beginning of the link-layer header after the
2686 static struct slist
*
2687 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2689 struct slist
*s1
, *s2
;
2692 * Generate code to load the length of the pflog header into
2693 * the register assigned to hold that length, if one has been
2694 * assigned. (If one hasn't been assigned, no code we've
2695 * generated uses that prefix, so we don't need to generate any
2698 if (cstate
->off_linkpl
.reg
!= -1) {
2700 * The length is in the first byte of the header.
2702 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2706 * Round it up to a multiple of 4.
2707 * Add 3, and clear the lower 2 bits.
2709 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2712 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2713 s2
->s
.k
= 0xfffffffc;
2717 * Now allocate a register to hold that value and store
2720 s2
= new_stmt(cstate
, BPF_ST
);
2721 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2725 * Now move it into the X register.
2727 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2735 static struct slist
*
2736 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2738 struct slist
*s1
, *s2
;
2739 struct slist
*sjeq_avs_cookie
;
2740 struct slist
*sjcommon
;
2743 * This code is not compatible with the optimizer, as
2744 * we are generating jmp instructions within a normal
2745 * slist of instructions
2747 cstate
->no_optimize
= 1;
2750 * Generate code to load the length of the radio header into
2751 * the register assigned to hold that length, if one has been
2752 * assigned. (If one hasn't been assigned, no code we've
2753 * generated uses that prefix, so we don't need to generate any
2756 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2757 * or always use the AVS header rather than the Prism header.
2758 * We load a 4-byte big-endian value at the beginning of the
2759 * raw packet data, and see whether, when masked with 0xFFFFF000,
2760 * it's equal to 0x80211000. If so, that indicates that it's
2761 * an AVS header (the masked-out bits are the version number).
2762 * Otherwise, it's a Prism header.
2764 * XXX - the Prism header is also, in theory, variable-length,
2765 * but no known software generates headers that aren't 144
2768 if (cstate
->off_linkhdr
.reg
!= -1) {
2772 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2776 * AND it with 0xFFFFF000.
2778 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2779 s2
->s
.k
= 0xFFFFF000;
2783 * Compare with 0x80211000.
2785 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2786 sjeq_avs_cookie
->s
.k
= 0x80211000;
2787 sappend(s1
, sjeq_avs_cookie
);
2792 * The 4 bytes at an offset of 4 from the beginning of
2793 * the AVS header are the length of the AVS header.
2794 * That field is big-endian.
2796 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2799 sjeq_avs_cookie
->s
.jt
= s2
;
2802 * Now jump to the code to allocate a register
2803 * into which to save the header length and
2804 * store the length there. (The "jump always"
2805 * instruction needs to have the k field set;
2806 * it's added to the PC, so, as we're jumping
2807 * over a single instruction, it should be 1.)
2809 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2811 sappend(s1
, sjcommon
);
2814 * Now for the code that handles the Prism header.
2815 * Just load the length of the Prism header (144)
2816 * into the A register. Have the test for an AVS
2817 * header branch here if we don't have an AVS header.
2819 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2822 sjeq_avs_cookie
->s
.jf
= s2
;
2825 * Now allocate a register to hold that value and store
2826 * it. The code for the AVS header will jump here after
2827 * loading the length of the AVS header.
2829 s2
= new_stmt(cstate
, BPF_ST
);
2830 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2832 sjcommon
->s
.jf
= s2
;
2835 * Now move it into the X register.
2837 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2845 static struct slist
*
2846 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2848 struct slist
*s1
, *s2
;
2851 * Generate code to load the length of the AVS header into
2852 * the register assigned to hold that length, if one has been
2853 * assigned. (If one hasn't been assigned, no code we've
2854 * generated uses that prefix, so we don't need to generate any
2857 if (cstate
->off_linkhdr
.reg
!= -1) {
2859 * The 4 bytes at an offset of 4 from the beginning of
2860 * the AVS header are the length of the AVS header.
2861 * That field is big-endian.
2863 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2867 * Now allocate a register to hold that value and store
2870 s2
= new_stmt(cstate
, BPF_ST
);
2871 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2875 * Now move it into the X register.
2877 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2885 static struct slist
*
2886 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2888 struct slist
*s1
, *s2
;
2891 * Generate code to load the length of the radiotap header into
2892 * the register assigned to hold that length, if one has been
2893 * assigned. (If one hasn't been assigned, no code we've
2894 * generated uses that prefix, so we don't need to generate any
2897 if (cstate
->off_linkhdr
.reg
!= -1) {
2899 * The 2 bytes at offsets of 2 and 3 from the beginning
2900 * of the radiotap header are the length of the radiotap
2901 * header; unfortunately, it's little-endian, so we have
2902 * to load it a byte at a time and construct the value.
2906 * Load the high-order byte, at an offset of 3, shift it
2907 * left a byte, and put the result in the X register.
2909 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2911 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2914 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2918 * Load the next byte, at an offset of 2, and OR the
2919 * value from the X register into it.
2921 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2924 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2928 * Now allocate a register to hold that value and store
2931 s2
= new_stmt(cstate
, BPF_ST
);
2932 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2936 * Now move it into the X register.
2938 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2947 * At the moment we treat PPI as normal Radiotap encoded
2948 * packets. The difference is in the function that generates
2949 * the code at the beginning to compute the header length.
2950 * Since this code generator of PPI supports bare 802.11
2951 * encapsulation only (i.e. the encapsulated DLT should be
2952 * DLT_IEEE802_11) we generate code to check for this too;
2953 * that's done in finish_parse().
2955 static struct slist
*
2956 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2958 struct slist
*s1
, *s2
;
2961 * Generate code to load the length of the radiotap header
2962 * into the register assigned to hold that length, if one has
2965 if (cstate
->off_linkhdr
.reg
!= -1) {
2967 * The 2 bytes at offsets of 2 and 3 from the beginning
2968 * of the radiotap header are the length of the radiotap
2969 * header; unfortunately, it's little-endian, so we have
2970 * to load it a byte at a time and construct the value.
2974 * Load the high-order byte, at an offset of 3, shift it
2975 * left a byte, and put the result in the X register.
2977 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2979 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2982 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2986 * Load the next byte, at an offset of 2, and OR the
2987 * value from the X register into it.
2989 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2992 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2996 * Now allocate a register to hold that value and store
2999 s2
= new_stmt(cstate
, BPF_ST
);
3000 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
3004 * Now move it into the X register.
3006 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
3015 * Load a value relative to the beginning of the link-layer header after the 802.11
3016 * header, i.e. LLC_SNAP.
3017 * The link-layer header doesn't necessarily begin at the beginning
3018 * of the packet data; there might be a variable-length prefix containing
3019 * radio information.
3021 static struct slist
*
3022 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
3025 struct slist
*sjset_data_frame_1
;
3026 struct slist
*sjset_data_frame_2
;
3027 struct slist
*sjset_qos
;
3028 struct slist
*sjset_radiotap_flags_present
;
3029 struct slist
*sjset_radiotap_ext_present
;
3030 struct slist
*sjset_radiotap_tsft_present
;
3031 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
3032 struct slist
*s_roundup
;
3034 if (cstate
->off_linkpl
.reg
== -1) {
3036 * No register has been assigned to the offset of
3037 * the link-layer payload, which means nobody needs
3038 * it; don't bother computing it - just return
3039 * what we already have.
3045 * This code is not compatible with the optimizer, as
3046 * we are generating jmp instructions within a normal
3047 * slist of instructions
3049 cstate
->no_optimize
= 1;
3052 * If "s" is non-null, it has code to arrange that the X register
3053 * contains the length of the prefix preceding the link-layer
3056 * Otherwise, the length of the prefix preceding the link-layer
3057 * header is "off_outermostlinkhdr.constant_part".
3061 * There is no variable-length header preceding the
3062 * link-layer header.
3064 * Load the length of the fixed-length prefix preceding
3065 * the link-layer header (if any) into the X register,
3066 * and store it in the cstate->off_linkpl.reg register.
3067 * That length is off_outermostlinkhdr.constant_part.
3069 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3070 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3074 * The X register contains the offset of the beginning of the
3075 * link-layer header; add 24, which is the minimum length
3076 * of the MAC header for a data frame, to that, and store it
3077 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3078 * which is at the offset in the X register, with an indexed load.
3080 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3082 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3085 s2
= new_stmt(cstate
, BPF_ST
);
3086 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3089 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3094 * Check the Frame Control field to see if this is a data frame;
3095 * a data frame has the 0x08 bit (b3) in that field set and the
3096 * 0x04 bit (b2) clear.
3098 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3099 sjset_data_frame_1
->s
.k
= 0x08;
3100 sappend(s
, sjset_data_frame_1
);
3103 * If b3 is set, test b2, otherwise go to the first statement of
3104 * the rest of the program.
3106 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3107 sjset_data_frame_2
->s
.k
= 0x04;
3108 sappend(s
, sjset_data_frame_2
);
3109 sjset_data_frame_1
->s
.jf
= snext
;
3112 * If b2 is not set, this is a data frame; test the QoS bit.
3113 * Otherwise, go to the first statement of the rest of the
3116 sjset_data_frame_2
->s
.jt
= snext
;
3117 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3118 sjset_qos
->s
.k
= 0x80; /* QoS bit */
3119 sappend(s
, sjset_qos
);
3122 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3124 * Otherwise, go to the first statement of the rest of the
3127 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3128 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3130 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3133 s2
= new_stmt(cstate
, BPF_ST
);
3134 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3138 * If we have a radiotap header, look at it to see whether
3139 * there's Atheros padding between the MAC-layer header
3142 * Note: all of the fields in the radiotap header are
3143 * little-endian, so we byte-swap all of the values
3144 * we test against, as they will be loaded as big-endian
3147 * XXX - in the general case, we would have to scan through
3148 * *all* the presence bits, if there's more than one word of
3149 * presence bits. That would require a loop, meaning that
3150 * we wouldn't be able to run the filter in the kernel.
3152 * We assume here that the Atheros adapters that insert the
3153 * annoying padding don't have multiple antennae and therefore
3154 * do not generate radiotap headers with multiple presence words.
3156 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3158 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3159 * in the first presence flag word?
3161 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3165 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3166 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3167 sappend(s
, sjset_radiotap_flags_present
);
3170 * If not, skip all of this.
3172 sjset_radiotap_flags_present
->s
.jf
= snext
;
3175 * Otherwise, is the "extension" bit set in that word?
3177 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3178 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3179 sappend(s
, sjset_radiotap_ext_present
);
3180 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3183 * If so, skip all of this.
3185 sjset_radiotap_ext_present
->s
.jt
= snext
;
3188 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3190 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3191 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3192 sappend(s
, sjset_radiotap_tsft_present
);
3193 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3196 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3197 * at an offset of 16 from the beginning of the raw packet
3198 * data (8 bytes for the radiotap header and 8 bytes for
3201 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3204 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3207 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3209 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3210 sjset_tsft_datapad
->s
.k
= 0x20;
3211 sappend(s
, sjset_tsft_datapad
);
3214 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3215 * at an offset of 8 from the beginning of the raw packet
3216 * data (8 bytes for the radiotap header).
3218 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3221 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3224 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3226 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3227 sjset_notsft_datapad
->s
.k
= 0x20;
3228 sappend(s
, sjset_notsft_datapad
);
3231 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3232 * set, round the length of the 802.11 header to
3233 * a multiple of 4. Do that by adding 3 and then
3234 * dividing by and multiplying by 4, which we do by
3237 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3238 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3239 sappend(s
, s_roundup
);
3240 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3243 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3244 s2
->s
.k
= (bpf_u_int32
)~3;
3246 s2
= new_stmt(cstate
, BPF_ST
);
3247 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3250 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3251 sjset_tsft_datapad
->s
.jf
= snext
;
3252 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3253 sjset_notsft_datapad
->s
.jf
= snext
;
3255 sjset_qos
->s
.jf
= snext
;
3261 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3265 /* There is an implicit dependency between the link
3266 * payload and link header since the payload computation
3267 * includes the variable part of the header. Therefore,
3268 * if nobody else has allocated a register for the link
3269 * header and we need it, do it now. */
3270 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3271 cstate
->off_linkhdr
.reg
== -1)
3272 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3275 * For link-layer types that have a variable-length header
3276 * preceding the link-layer header, generate code to load
3277 * the offset of the link-layer header into the register
3278 * assigned to that offset, if any.
3280 * XXX - this, and the next switch statement, won't handle
3281 * encapsulation of 802.11 or 802.11+radio information in
3282 * some other protocol stack. That's significantly more
3285 switch (cstate
->outermostlinktype
) {
3287 case DLT_PRISM_HEADER
:
3288 s
= gen_load_prism_llprefixlen(cstate
);
3291 case DLT_IEEE802_11_RADIO_AVS
:
3292 s
= gen_load_avs_llprefixlen(cstate
);
3295 case DLT_IEEE802_11_RADIO
:
3296 s
= gen_load_radiotap_llprefixlen(cstate
);
3300 s
= gen_load_ppi_llprefixlen(cstate
);
3309 * For link-layer types that have a variable-length link-layer
3310 * header, generate code to load the offset of the link-layer
3311 * payload into the register assigned to that offset, if any.
3313 switch (cstate
->outermostlinktype
) {
3315 case DLT_IEEE802_11
:
3316 case DLT_PRISM_HEADER
:
3317 case DLT_IEEE802_11_RADIO_AVS
:
3318 case DLT_IEEE802_11_RADIO
:
3320 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3324 s
= gen_load_pflog_llprefixlen(cstate
);
3329 * If there is no initialization yet and we need variable
3330 * length offsets for VLAN, initialize them to zero
3332 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3335 if (cstate
->off_linkpl
.reg
== -1)
3336 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3337 if (cstate
->off_linktype
.reg
== -1)
3338 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3340 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3342 s2
= new_stmt(cstate
, BPF_ST
);
3343 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3345 s2
= new_stmt(cstate
, BPF_ST
);
3346 s2
->s
.k
= cstate
->off_linktype
.reg
;
3351 * If we have any offset-loading code, append all the
3352 * existing statements in the block to those statements,
3353 * and make the resulting list the list of statements
3357 sappend(s
, b
->stmts
);
3363 * Take an absolute offset, and:
3365 * if it has no variable part, return NULL;
3367 * if it has a variable part, generate code to load the register
3368 * containing that variable part into the X register, returning
3369 * a pointer to that code - if no register for that offset has
3370 * been allocated, allocate it first.
3372 * (The code to set that register will be generated later, but will
3373 * be placed earlier in the code sequence.)
3375 static struct slist
*
3376 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3380 if (off
->is_variable
) {
3381 if (off
->reg
== -1) {
3383 * We haven't yet assigned a register for the
3384 * variable part of the offset of the link-layer
3385 * header; allocate one.
3387 off
->reg
= alloc_reg(cstate
);
3391 * Load the register containing the variable part of the
3392 * offset of the link-layer header into the X register.
3394 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3399 * That offset isn't variable, there's no variable part,
3400 * so we don't need to generate any code.
3407 * Map an Ethernet type to the equivalent PPP type.
3410 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3418 case ETHERTYPE_IPV6
:
3419 ll_proto
= PPP_IPV6
;
3423 ll_proto
= PPP_DECNET
;
3426 case ETHERTYPE_ATALK
:
3427 ll_proto
= PPP_APPLE
;
3440 * I'm assuming the "Bridging PDU"s that go
3441 * over PPP are Spanning Tree Protocol
3444 ll_proto
= PPP_BRPDU
;
3455 * Generate any tests that, for encapsulation of a link-layer packet
3456 * inside another protocol stack, need to be done to check for those
3457 * link-layer packets (and that haven't already been done by a check
3458 * for that encapsulation).
3460 static struct block
*
3461 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3465 if (cstate
->is_encap
)
3466 return gen_encap_ll_check(cstate
);
3468 switch (cstate
->prevlinktype
) {
3472 * This is LANE-encapsulated Ethernet; check that the LANE
3473 * packet doesn't begin with an LE Control marker, i.e.
3474 * that it's data, not a control message.
3476 * (We've already generated a test for LANE.)
3478 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3484 * No such tests are necessary.
3492 * The three different values we should check for when checking for an
3493 * IPv6 packet with DLT_NULL.
3495 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3496 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3497 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3500 * Generate code to match a particular packet type by matching the
3501 * link-layer type field or fields in the 802.2 LLC header.
3503 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3504 * value, if <= ETHERMTU.
3506 static struct block
*
3507 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3509 struct block
*b0
, *b1
, *b2
;
3511 /* are we checking MPLS-encapsulated packets? */
3512 if (cstate
->label_stack_depth
> 0)
3513 return gen_mpls_linktype(cstate
, ll_proto
);
3515 switch (cstate
->linktype
) {
3518 case DLT_NETANALYZER
:
3519 case DLT_NETANALYZER_TRANSPARENT
:
3520 /* Geneve has an EtherType regardless of whether there is an
3521 * L2 header. VXLAN always has an EtherType. */
3522 if (!cstate
->is_encap
)
3523 b0
= gen_prevlinkhdr_check(cstate
);
3527 b1
= gen_ether_linktype(cstate
, ll_proto
);
3538 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3542 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3546 case DLT_IEEE802_11
:
3547 case DLT_PRISM_HEADER
:
3548 case DLT_IEEE802_11_RADIO_AVS
:
3549 case DLT_IEEE802_11_RADIO
:
3552 * Check that we have a data frame.
3554 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3555 IEEE80211_FC0_TYPE_DATA
,
3556 IEEE80211_FC0_TYPE_MASK
);
3559 * Now check for the specified link-layer type.
3561 b1
= gen_llc_linktype(cstate
, ll_proto
);
3568 * XXX - check for LLC frames.
3570 return gen_llc_linktype(cstate
, ll_proto
);
3575 * XXX - check for LLC PDUs, as per IEEE 802.5.
3577 return gen_llc_linktype(cstate
, ll_proto
);
3580 case DLT_ATM_RFC1483
:
3582 case DLT_IP_OVER_FC
:
3583 return gen_llc_linktype(cstate
, ll_proto
);
3588 * Check for an LLC-encapsulated version of this protocol;
3589 * if we were checking for LANE, linktype would no longer
3592 * Check for LLC encapsulation and then check the protocol.
3594 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3595 b1
= gen_llc_linktype(cstate
, ll_proto
);
3601 return gen_linux_sll_linktype(cstate
, ll_proto
);
3605 case DLT_SLIP_BSDOS
:
3608 * These types don't provide any type field; packets
3609 * are always IPv4 or IPv6.
3611 * XXX - for IPv4, check for a version number of 4, and,
3612 * for IPv6, check for a version number of 6?
3617 /* Check for a version number of 4. */
3618 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3620 case ETHERTYPE_IPV6
:
3621 /* Check for a version number of 6. */
3622 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3625 return gen_false(cstate
); /* always false */
3631 * Raw IPv4, so no type field.
3633 if (ll_proto
== ETHERTYPE_IP
)
3634 return gen_true(cstate
); /* always true */
3636 /* Checking for something other than IPv4; always false */
3637 return gen_false(cstate
);
3642 * Raw IPv6, so no type field.
3644 if (ll_proto
== ETHERTYPE_IPV6
)
3645 return gen_true(cstate
); /* always true */
3647 /* Checking for something other than IPv6; always false */
3648 return gen_false(cstate
);
3653 case DLT_PPP_SERIAL
:
3656 * We use Ethernet protocol types inside libpcap;
3657 * map them to the corresponding PPP protocol types.
3659 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3660 ethertype_to_ppptype(ll_proto
));
3665 * We use Ethernet protocol types inside libpcap;
3666 * map them to the corresponding PPP protocol types.
3672 * Also check for Van Jacobson-compressed IP.
3673 * XXX - do this for other forms of PPP?
3675 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3676 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3678 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3683 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3684 ethertype_to_ppptype(ll_proto
));
3694 return (gen_loopback_linktype(cstate
, AF_INET
));
3696 case ETHERTYPE_IPV6
:
3698 * AF_ values may, unfortunately, be platform-
3699 * dependent; AF_INET isn't, because everybody
3700 * used 4.2BSD's value, but AF_INET6 is, because
3701 * 4.2BSD didn't have a value for it (given that
3702 * IPv6 didn't exist back in the early 1980's),
3703 * and they all picked their own values.
3705 * This means that, if we're reading from a
3706 * savefile, we need to check for all the
3709 * If we're doing a live capture, we only need
3710 * to check for this platform's value; however,
3711 * Npcap uses 24, which isn't Windows's AF_INET6
3712 * value. (Given the multiple different values,
3713 * programs that read pcap files shouldn't be
3714 * checking for their platform's AF_INET6 value
3715 * anyway, they should check for all of the
3716 * possible values. and they might as well do
3717 * that even for live captures.)
3719 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3721 * Savefile - check for all three
3722 * possible IPv6 values.
3724 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3725 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3727 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3732 * Live capture, so we only need to
3733 * check for the value used on this
3738 * Npcap doesn't use Windows's AF_INET6,
3739 * as that collides with AF_IPX on
3740 * some BSDs (both have the value 23).
3741 * Instead, it uses 24.
3743 return (gen_loopback_linktype(cstate
, 24));
3746 return (gen_loopback_linktype(cstate
, AF_INET6
));
3747 #else /* AF_INET6 */
3749 * I guess this platform doesn't support
3750 * IPv6, so we just reject all packets.
3752 return gen_false(cstate
);
3753 #endif /* AF_INET6 */
3759 * Not a type on which we support filtering.
3760 * XXX - support those that have AF_ values
3761 * #defined on this platform, at least?
3763 return gen_false(cstate
);
3768 * af field is host byte order in contrast to the rest of
3771 if (ll_proto
== ETHERTYPE_IP
)
3772 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3774 else if (ll_proto
== ETHERTYPE_IPV6
)
3775 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3778 return gen_false(cstate
);
3782 case DLT_ARCNET_LINUX
:
3784 * XXX should we check for first fragment if the protocol
3790 return gen_false(cstate
);
3792 case ETHERTYPE_IPV6
:
3793 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3797 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3799 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3805 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3807 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3812 case ETHERTYPE_REVARP
:
3813 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3816 case ETHERTYPE_ATALK
:
3817 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3824 case ETHERTYPE_ATALK
:
3825 return gen_true(cstate
);
3827 return gen_false(cstate
);
3833 * XXX - assumes a 2-byte Frame Relay header with
3834 * DLCI and flags. What if the address is longer?
3840 * Check for the special NLPID for IP.
3842 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3844 case ETHERTYPE_IPV6
:
3846 * Check for the special NLPID for IPv6.
3848 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3852 * Check for several OSI protocols.
3854 * Frame Relay packets typically have an OSI
3855 * NLPID at the beginning; we check for each
3858 * What we check for is the NLPID and a frame
3859 * control field of UI, i.e. 0x03 followed
3862 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3863 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3864 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3870 return gen_false(cstate
);
3875 break; // not implemented
3877 case DLT_JUNIPER_MFR
:
3878 case DLT_JUNIPER_MLFR
:
3879 case DLT_JUNIPER_MLPPP
:
3880 case DLT_JUNIPER_ATM1
:
3881 case DLT_JUNIPER_ATM2
:
3882 case DLT_JUNIPER_PPPOE
:
3883 case DLT_JUNIPER_PPPOE_ATM
:
3884 case DLT_JUNIPER_GGSN
:
3885 case DLT_JUNIPER_ES
:
3886 case DLT_JUNIPER_MONITOR
:
3887 case DLT_JUNIPER_SERVICES
:
3888 case DLT_JUNIPER_ETHER
:
3889 case DLT_JUNIPER_PPP
:
3890 case DLT_JUNIPER_FRELAY
:
3891 case DLT_JUNIPER_CHDLC
:
3892 case DLT_JUNIPER_VP
:
3893 case DLT_JUNIPER_ST
:
3894 case DLT_JUNIPER_ISM
:
3895 case DLT_JUNIPER_VS
:
3896 case DLT_JUNIPER_SRX_E2E
:
3897 case DLT_JUNIPER_FIBRECHANNEL
:
3898 case DLT_JUNIPER_ATM_CEMIC
:
3900 /* just lets verify the magic number for now -
3901 * on ATM we may have up to 6 different encapsulations on the wire
3902 * and need a lot of heuristics to figure out that the payload
3905 * FIXME encapsulation specific BPF_ filters
3907 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3909 case DLT_BACNET_MS_TP
:
3910 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3913 return gen_ipnet_linktype(cstate
, ll_proto
);
3915 case DLT_LINUX_IRDA
:
3918 case DLT_MTP2_WITH_PHDR
:
3921 case DLT_LINUX_LAPD
:
3922 case DLT_USB_FREEBSD
:
3924 case DLT_USB_LINUX_MMAPPED
:
3926 case DLT_BLUETOOTH_HCI_H4
:
3927 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3929 case DLT_CAN_SOCKETCAN
:
3930 case DLT_IEEE802_15_4
:
3931 case DLT_IEEE802_15_4_LINUX
:
3932 case DLT_IEEE802_15_4_NONASK_PHY
:
3933 case DLT_IEEE802_15_4_NOFCS
:
3934 case DLT_IEEE802_15_4_TAP
:
3935 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3938 case DLT_IPMB_KONTRON
:
3942 /* Using the fixed-size NFLOG header it is possible to tell only
3943 * the address family of the packet, other meaningful data is
3944 * either missing or behind TLVs.
3946 break; // not implemented
3950 * Does this link-layer header type have a field
3951 * indicating the type of the next protocol? If
3952 * so, off_linktype.constant_part will be the offset of that
3953 * field in the packet; if not, it will be OFFSET_NOT_SET.
3955 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3957 * Yes; assume it's an Ethernet type. (If
3958 * it's not, it needs to be handled specially
3961 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3965 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
3966 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3970 * Check for an LLC SNAP packet with a given organization code and
3971 * protocol type; we check the entire contents of the 802.2 LLC and
3972 * snap headers, checking for DSAP and SSAP of SNAP and a control
3973 * field of 0x03 in the LLC header, and for the specified organization
3974 * code and protocol type in the SNAP header.
3976 static struct block
*
3977 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3979 u_char snapblock
[8];
3981 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3982 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3983 snapblock
[2] = 0x03; /* control = UI */
3984 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3985 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3986 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3987 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3988 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3989 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3993 * Generate code to match frames with an LLC header.
3995 static struct block
*
3996 gen_llc_internal(compiler_state_t
*cstate
)
3998 struct block
*b0
, *b1
;
4000 switch (cstate
->linktype
) {
4004 * We check for an Ethernet type field less or equal than
4005 * 1500, which means it's an 802.3 length field.
4007 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
4010 * Now check for the purported DSAP and SSAP not being
4011 * 0xFF, to rule out NetWare-over-802.3.
4013 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
4020 * We check for LLC traffic.
4022 b0
= gen_atmtype_llc(cstate
);
4025 case DLT_IEEE802
: /* Token Ring */
4027 * XXX - check for LLC frames.
4029 return gen_true(cstate
);
4033 * XXX - check for LLC frames.
4035 return gen_true(cstate
);
4037 case DLT_ATM_RFC1483
:
4039 * For LLC encapsulation, these are defined to have an
4042 * For VC encapsulation, they don't, but there's no
4043 * way to check for that; the protocol used on the VC
4044 * is negotiated out of band.
4046 return gen_true(cstate
);
4048 case DLT_IEEE802_11
:
4049 case DLT_PRISM_HEADER
:
4050 case DLT_IEEE802_11_RADIO
:
4051 case DLT_IEEE802_11_RADIO_AVS
:
4054 * Check that we have a data frame.
4056 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4057 IEEE80211_FC0_TYPE_DATA
,
4058 IEEE80211_FC0_TYPE_MASK
);
4061 fail_kw_on_dlt(cstate
, "llc");
4067 gen_llc(compiler_state_t
*cstate
)
4070 * Catch errors reported by us and routines below us, and return NULL
4073 if (setjmp(cstate
->top_ctx
))
4076 return gen_llc_internal(cstate
);
4080 gen_llc_i(compiler_state_t
*cstate
)
4082 struct block
*b0
, *b1
;
4086 * Catch errors reported by us and routines below us, and return NULL
4089 if (setjmp(cstate
->top_ctx
))
4093 * Check whether this is an LLC frame.
4095 b0
= gen_llc_internal(cstate
);
4098 * Load the control byte and test the low-order bit; it must
4099 * be clear for I frames.
4101 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4102 b1
= new_block(cstate
, JMP(BPF_JSET
));
4111 gen_llc_s(compiler_state_t
*cstate
)
4113 struct block
*b0
, *b1
;
4116 * Catch errors reported by us and routines below us, and return NULL
4119 if (setjmp(cstate
->top_ctx
))
4123 * Check whether this is an LLC frame.
4125 b0
= gen_llc_internal(cstate
);
4128 * Now compare the low-order 2 bit of the control byte against
4129 * the appropriate value for S frames.
4131 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4137 gen_llc_u(compiler_state_t
*cstate
)
4139 struct block
*b0
, *b1
;
4142 * Catch errors reported by us and routines below us, and return NULL
4145 if (setjmp(cstate
->top_ctx
))
4149 * Check whether this is an LLC frame.
4151 b0
= gen_llc_internal(cstate
);
4154 * Now compare the low-order 2 bit of the control byte against
4155 * the appropriate value for U frames.
4157 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4163 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4165 struct block
*b0
, *b1
;
4168 * Catch errors reported by us and routines below us, and return NULL
4171 if (setjmp(cstate
->top_ctx
))
4175 * Check whether this is an LLC frame.
4177 b0
= gen_llc_internal(cstate
);
4180 * Now check for an S frame with the appropriate type.
4182 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4188 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4190 struct block
*b0
, *b1
;
4193 * Catch errors reported by us and routines below us, and return NULL
4196 if (setjmp(cstate
->top_ctx
))
4200 * Check whether this is an LLC frame.
4202 b0
= gen_llc_internal(cstate
);
4205 * Now check for a U frame with the appropriate type.
4207 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4213 * Generate code to match a particular packet type, for link-layer types
4214 * using 802.2 LLC headers.
4216 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4217 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4219 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4220 * value, if <= ETHERMTU. We use that to determine whether to
4221 * match the DSAP or both DSAP and LSAP or to check the OUI and
4222 * protocol ID in a SNAP header.
4224 static struct block
*
4225 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4228 * XXX - handle token-ring variable-length header.
4234 case LLCSAP_NETBEUI
:
4236 * XXX - should we check both the DSAP and the
4237 * SSAP, like this, or should we check just the
4238 * DSAP, as we do for other SAP values?
4240 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4241 ((ll_proto
<< 8) | ll_proto
));
4245 * XXX - are there ever SNAP frames for IPX on
4246 * non-Ethernet 802.x networks?
4248 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4250 case ETHERTYPE_ATALK
:
4252 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4253 * SNAP packets with an organization code of
4254 * 0x080007 (Apple, for Appletalk) and a protocol
4255 * type of ETHERTYPE_ATALK (Appletalk).
4257 * XXX - check for an organization code of
4258 * encapsulated Ethernet as well?
4260 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4264 * XXX - we don't have to check for IPX 802.3
4265 * here, but should we check for the IPX Ethertype?
4267 if (ll_proto
<= ETHERMTU
) {
4269 * This is an LLC SAP value, so check
4272 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4275 * This is an Ethernet type; we assume that it's
4276 * unlikely that it'll appear in the right place
4277 * at random, and therefore check only the
4278 * location that would hold the Ethernet type
4279 * in a SNAP frame with an organization code of
4280 * 0x000000 (encapsulated Ethernet).
4282 * XXX - if we were to check for the SNAP DSAP and
4283 * LSAP, as per XXX, and were also to check for an
4284 * organization code of 0x000000 (encapsulated
4285 * Ethernet), we'd do
4287 * return gen_snap(cstate, 0x000000, ll_proto);
4289 * here; for now, we don't, as per the above.
4290 * I don't know whether it's worth the extra CPU
4291 * time to do the right check or not.
4293 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4298 static struct block
*
4299 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4300 int dir
, u_int src_off
, u_int dst_off
)
4302 struct block
*b0
, *b1
;
4316 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4317 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4323 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4324 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4334 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4341 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4345 static struct block
*
4346 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4347 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4349 struct block
*b0
, *b1
;
4352 * Code below needs to access four separate 32-bit parts of the 128-bit
4353 * IPv6 address and mask. In some OSes this is as simple as using the
4354 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4355 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4356 * far as libpcap sees it. Hence copy the data before use to avoid
4357 * potential unaligned memory access and the associated compiler
4358 * warnings (whether genuine or not).
4360 bpf_u_int32 a
[4], m
[4];
4373 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4374 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4380 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4381 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4391 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4398 /* this order is important */
4399 memcpy(a
, addr
, sizeof(a
));
4400 memcpy(m
, mask
, sizeof(m
));
4401 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4402 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4404 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4406 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4412 static struct block
*
4413 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4415 register struct block
*b0
, *b1
;
4419 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4422 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4425 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4426 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4432 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4433 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4443 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4451 * Like gen_ehostop, but for DLT_FDDI
4453 static struct block
*
4454 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4456 struct block
*b0
, *b1
;
4460 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4463 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4466 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4467 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4473 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4474 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4484 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4492 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4494 static struct block
*
4495 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4497 register struct block
*b0
, *b1
;
4501 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4504 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4507 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4508 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4514 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4515 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4525 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4533 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4534 * various 802.11 + radio headers.
4536 static struct block
*
4537 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4539 register struct block
*b0
, *b1
, *b2
;
4540 register struct slist
*s
;
4542 #ifdef ENABLE_WLAN_FILTERING_PATCH
4545 * We need to disable the optimizer because the optimizer is buggy
4546 * and wipes out some LD instructions generated by the below
4547 * code to validate the Frame Control bits
4549 cstate
->no_optimize
= 1;
4550 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4557 * For control frames, there is no SA.
4559 * For management frames, SA is at an
4560 * offset of 10 from the beginning of
4563 * For data frames, SA is at an offset
4564 * of 10 from the beginning of the packet
4565 * if From DS is clear, at an offset of
4566 * 16 from the beginning of the packet
4567 * if From DS is set and To DS is clear,
4568 * and an offset of 24 from the beginning
4569 * of the packet if From DS is set and To DS
4574 * Generate the tests to be done for data frames
4577 * First, check for To DS set, i.e. check "link[1] & 0x01".
4579 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4580 b1
= new_block(cstate
, JMP(BPF_JSET
));
4581 b1
->s
.k
= 0x01; /* To DS */
4585 * If To DS is set, the SA is at 24.
4587 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4591 * Now, check for To DS not set, i.e. check
4592 * "!(link[1] & 0x01)".
4594 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4595 b2
= new_block(cstate
, JMP(BPF_JSET
));
4596 b2
->s
.k
= 0x01; /* To DS */
4601 * If To DS is not set, the SA is at 16.
4603 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4607 * Now OR together the last two checks. That gives
4608 * the complete set of checks for data frames with
4614 * Now check for From DS being set, and AND that with
4615 * the ORed-together checks.
4617 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4618 b1
= new_block(cstate
, JMP(BPF_JSET
));
4619 b1
->s
.k
= 0x02; /* From DS */
4624 * Now check for data frames with From DS not set.
4626 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4627 b2
= new_block(cstate
, JMP(BPF_JSET
));
4628 b2
->s
.k
= 0x02; /* From DS */
4633 * If From DS isn't set, the SA is at 10.
4635 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4639 * Now OR together the checks for data frames with
4640 * From DS not set and for data frames with From DS
4641 * set; that gives the checks done for data frames.
4646 * Now check for a data frame.
4647 * I.e, check "link[0] & 0x08".
4649 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4650 b1
= new_block(cstate
, JMP(BPF_JSET
));
4655 * AND that with the checks done for data frames.
4660 * If the high-order bit of the type value is 0, this
4661 * is a management frame.
4662 * I.e, check "!(link[0] & 0x08)".
4664 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4665 b2
= new_block(cstate
, JMP(BPF_JSET
));
4671 * For management frames, the SA is at 10.
4673 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4677 * OR that with the checks done for data frames.
4678 * That gives the checks done for management and
4684 * If the low-order bit of the type value is 1,
4685 * this is either a control frame or a frame
4686 * with a reserved type, and thus not a
4689 * I.e., check "!(link[0] & 0x04)".
4691 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4692 b1
= new_block(cstate
, JMP(BPF_JSET
));
4698 * AND that with the checks for data and management
4708 * For control frames, there is no DA.
4710 * For management frames, DA is at an
4711 * offset of 4 from the beginning of
4714 * For data frames, DA is at an offset
4715 * of 4 from the beginning of the packet
4716 * if To DS is clear and at an offset of
4717 * 16 from the beginning of the packet
4722 * Generate the tests to be done for data frames.
4724 * First, check for To DS set, i.e. "link[1] & 0x01".
4726 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4727 b1
= new_block(cstate
, JMP(BPF_JSET
));
4728 b1
->s
.k
= 0x01; /* To DS */
4732 * If To DS is set, the DA is at 16.
4734 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4738 * Now, check for To DS not set, i.e. check
4739 * "!(link[1] & 0x01)".
4741 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4742 b2
= new_block(cstate
, JMP(BPF_JSET
));
4743 b2
->s
.k
= 0x01; /* To DS */
4748 * If To DS is not set, the DA is at 4.
4750 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4754 * Now OR together the last two checks. That gives
4755 * the complete set of checks for data frames.
4760 * Now check for a data frame.
4761 * I.e, check "link[0] & 0x08".
4763 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4764 b1
= new_block(cstate
, JMP(BPF_JSET
));
4769 * AND that with the checks done for data frames.
4774 * If the high-order bit of the type value is 0, this
4775 * is a management frame.
4776 * I.e, check "!(link[0] & 0x08)".
4778 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4779 b2
= new_block(cstate
, JMP(BPF_JSET
));
4785 * For management frames, the DA is at 4.
4787 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4791 * OR that with the checks done for data frames.
4792 * That gives the checks done for management and
4798 * If the low-order bit of the type value is 1,
4799 * this is either a control frame or a frame
4800 * with a reserved type, and thus not a
4803 * I.e., check "!(link[0] & 0x04)".
4805 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4806 b1
= new_block(cstate
, JMP(BPF_JSET
));
4812 * AND that with the checks for data and management
4819 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4820 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4826 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4827 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4832 * XXX - add BSSID keyword?
4835 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4839 * Not present in CTS or ACK control frames.
4841 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4842 IEEE80211_FC0_TYPE_MASK
);
4844 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4845 IEEE80211_FC0_SUBTYPE_MASK
);
4847 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4848 IEEE80211_FC0_SUBTYPE_MASK
);
4852 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4858 * Not present in control frames.
4860 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4861 IEEE80211_FC0_TYPE_MASK
);
4863 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4869 * Present only if the direction mask has both "From DS"
4870 * and "To DS" set. Neither control frames nor management
4871 * frames should have both of those set, so we don't
4872 * check the frame type.
4874 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4875 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4876 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4882 * Not present in management frames; addr1 in other
4887 * If the high-order bit of the type value is 0, this
4888 * is a management frame.
4889 * I.e, check "(link[0] & 0x08)".
4891 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4892 b1
= new_block(cstate
, JMP(BPF_JSET
));
4899 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4902 * AND that with the check of addr1.
4909 * Not present in management frames; addr2, if present,
4914 * Not present in CTS or ACK control frames.
4916 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4917 IEEE80211_FC0_TYPE_MASK
);
4919 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4920 IEEE80211_FC0_SUBTYPE_MASK
);
4922 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4923 IEEE80211_FC0_SUBTYPE_MASK
);
4929 * If the high-order bit of the type value is 0, this
4930 * is a management frame.
4931 * I.e, check "(link[0] & 0x08)".
4933 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4934 b1
= new_block(cstate
, JMP(BPF_JSET
));
4939 * AND that with the check for frames other than
4940 * CTS and ACK frames.
4947 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4956 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4957 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4958 * as the RFC states.)
4960 static struct block
*
4961 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4963 register struct block
*b0
, *b1
;
4967 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4970 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4973 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4974 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4980 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4981 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4991 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4999 * This is quite tricky because there may be pad bytes in front of the
5000 * DECNET header, and then there are two possible data packet formats that
5001 * carry both src and dst addresses, plus 5 packet types in a format that
5002 * carries only the src node, plus 2 types that use a different format and
5003 * also carry just the src node.
5007 * Instead of doing those all right, we just look for data packets with
5008 * 0 or 1 bytes of padding. If you want to look at other packets, that
5009 * will require a lot more hacking.
5011 * To add support for filtering on DECNET "areas" (network numbers)
5012 * one would want to add a "mask" argument to this routine. That would
5013 * make the filter even more inefficient, although one could be clever
5014 * and not generate masking instructions if the mask is 0xFFFF.
5016 static struct block
*
5017 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
5019 struct block
*b0
, *b1
, *b2
, *tmp
;
5020 u_int offset_lh
; /* offset if long header is received */
5021 u_int offset_sh
; /* offset if short header is received */
5026 offset_sh
= 1; /* follows flags */
5027 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
5031 offset_sh
= 3; /* follows flags, dstnode */
5032 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
5036 /* Inefficient because we do our Calvinball dance twice */
5037 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5038 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5044 /* Inefficient because we do our Calvinball dance twice */
5045 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5046 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5056 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5064 * In a DECnet message inside an Ethernet frame the first two bytes
5065 * immediately after EtherType are the [litle-endian] DECnet message
5066 * length, which is irrelevant in this context.
5068 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5069 * 8-bit bitmap of the optional padding before the packet route header.
5070 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5071 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5072 * means there aren't any PAD bytes after the bitmap, so the header
5073 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5074 * is set to 0, thus the header begins at the third byte.
5076 * The header can be in several (as mentioned above) formats, all of
5077 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5078 * (PF, "pad field") set to 0 regardless of any padding present before
5079 * the header. "Short header" means bits 0-2 of the bitmap encode the
5080 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5082 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5083 * values and the masks, this maps to the required single bytes of
5084 * the message correctly on both big-endian and little-endian hosts.
5085 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5086 * because the wire encoding is little-endian and BPF multiple-byte
5087 * loads are big-endian. When the destination address is near enough
5088 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5091 /* Check for pad = 1, long header case */
5092 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5093 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5094 BPF_H
, SWAPSHORT(addr
));
5096 /* Check for pad = 0, long header case */
5097 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5098 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5102 /* Check for pad = 1, short header case */
5104 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5105 0x81020000U
| SWAPSHORT(addr
),
5108 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5109 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5114 /* Check for pad = 0, short header case */
5116 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5117 0x02000000U
| SWAPSHORT(addr
) << 8,
5120 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5121 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5131 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5132 * test the bottom-of-stack bit, and then check the version number
5133 * field in the IP header.
5135 static struct block
*
5136 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5138 struct block
*b0
, *b1
;
5143 /* match the bottom-of-stack bit */
5144 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5145 /* match the IPv4 version number */
5146 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5150 case ETHERTYPE_IPV6
:
5151 /* match the bottom-of-stack bit */
5152 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5153 /* match the IPv4 version number */
5154 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5159 /* FIXME add other L3 proto IDs */
5160 bpf_error(cstate
, "unsupported protocol over mpls");
5165 static struct block
*
5166 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5167 int proto
, int dir
, int type
)
5169 struct block
*b0
, *b1
;
5174 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5176 * Only check for non-IPv4 addresses if we're not
5177 * checking MPLS-encapsulated packets.
5179 if (cstate
->label_stack_depth
== 0) {
5180 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5182 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5188 break; // invalid qualifier
5191 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5192 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5197 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5198 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5203 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5204 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5215 break; // invalid qualifier
5218 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5219 b1
= gen_dnhostop(cstate
, addr
, dir
);
5250 break; // invalid qualifier
5255 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5256 type
== Q_NET
? "ip net" : "ip host");
5261 static struct block
*
5262 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5263 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5265 struct block
*b0
, *b1
;
5271 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5272 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5314 break; // invalid qualifier
5319 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5320 type
== Q_NET
? "ip6 net" : "ip6 host");
5326 static struct block
*
5327 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5328 struct addrinfo
*alist
, int proto
, int dir
)
5330 struct block
*b0
, *b1
, *tmp
;
5331 struct addrinfo
*ai
;
5332 struct sockaddr_in
*sin
;
5335 bpf_error(cstate
, "direction applied to 'gateway'");
5342 switch (cstate
->linktype
) {
5344 case DLT_NETANALYZER
:
5345 case DLT_NETANALYZER_TRANSPARENT
:
5346 b1
= gen_prevlinkhdr_check(cstate
);
5347 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5352 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5355 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5357 case DLT_IEEE802_11
:
5358 case DLT_PRISM_HEADER
:
5359 case DLT_IEEE802_11_RADIO_AVS
:
5360 case DLT_IEEE802_11_RADIO
:
5362 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5364 case DLT_IP_OVER_FC
:
5365 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5369 * This is LLC-multiplexed traffic; if it were
5370 * LANE, cstate->linktype would have been set to
5376 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5379 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5381 * Does it have an address?
5383 if (ai
->ai_addr
!= NULL
) {
5385 * Yes. Is it an IPv4 address?
5387 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5389 * Generate an entry for it.
5391 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5392 tmp
= gen_host(cstate
,
5393 ntohl(sin
->sin_addr
.s_addr
),
5394 0xffffffff, proto
, Q_OR
, Q_HOST
);
5396 * Is it the *first* IPv4 address?
5400 * Yes, so start with it.
5405 * No, so OR it into the
5417 * No IPv4 addresses found.
5425 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5430 static struct block
*
5431 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5434 struct block
*b1
= NULL
;
5439 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5443 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5447 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5451 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5454 #ifndef IPPROTO_IGMP
5455 #define IPPROTO_IGMP 2
5459 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5462 #ifndef IPPROTO_IGRP
5463 #define IPPROTO_IGRP 9
5466 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5470 #define IPPROTO_PIM 103
5474 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5477 #ifndef IPPROTO_VRRP
5478 #define IPPROTO_VRRP 112
5482 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5485 #ifndef IPPROTO_CARP
5486 #define IPPROTO_CARP 112
5490 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5494 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5498 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5502 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5506 break; // invalid syntax
5509 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5513 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5517 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5521 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5525 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5529 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5533 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5537 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5540 #ifndef IPPROTO_ICMPV6
5541 #define IPPROTO_ICMPV6 58
5544 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5548 #define IPPROTO_AH 51
5551 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5555 #define IPPROTO_ESP 50
5558 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5562 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5566 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5570 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5573 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5574 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5575 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5577 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5579 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5581 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5585 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5586 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5587 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5589 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5591 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5593 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5597 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5598 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5599 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5601 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5606 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5607 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5612 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5613 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5615 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5617 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5622 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5623 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5628 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5629 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5634 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5638 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5642 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5646 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5650 break; // invalid syntax
5657 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5661 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5664 * Catch errors reported by us and routines below us, and return NULL
5667 if (setjmp(cstate
->top_ctx
))
5670 return gen_proto_abbrev_internal(cstate
, proto
);
5673 static struct block
*
5674 gen_ipfrag(compiler_state_t
*cstate
)
5679 /* not IPv4 frag other than the first frag */
5680 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5681 b
= new_block(cstate
, JMP(BPF_JSET
));
5690 * Generate a comparison to a port value in the transport-layer header
5691 * at the specified offset from the beginning of that header.
5693 * XXX - this handles a variable-length prefix preceding the link-layer
5694 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5695 * variable-length link-layer headers (such as Token Ring or 802.11
5698 static struct block
*
5699 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5701 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5704 static struct block
*
5705 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5707 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5710 static struct block
*
5711 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5713 struct block
*b0
, *b1
, *tmp
;
5715 /* ip proto 'proto' and not a fragment other than the first fragment */
5716 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5717 b0
= gen_ipfrag(cstate
);
5722 b1
= gen_portatom(cstate
, 0, port
);
5726 b1
= gen_portatom(cstate
, 2, port
);
5730 tmp
= gen_portatom(cstate
, 0, port
);
5731 b1
= gen_portatom(cstate
, 2, port
);
5737 tmp
= gen_portatom(cstate
, 0, port
);
5738 b1
= gen_portatom(cstate
, 2, port
);
5748 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5760 static struct block
*
5761 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5763 struct block
*b0
, *b1
, *tmp
;
5768 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5769 * not LLC encapsulation with LLCSAP_IP.
5771 * For IEEE 802 networks - which includes 802.5 token ring
5772 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5773 * says that SNAP encapsulation is used, not LLC encapsulation
5776 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5777 * RFC 2225 say that SNAP encapsulation is used, not LLC
5778 * encapsulation with LLCSAP_IP.
5780 * So we always check for ETHERTYPE_IP.
5782 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5788 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5792 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5793 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5795 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5807 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5809 struct block
*b0
, *b1
, *tmp
;
5811 /* ip6 proto 'proto' */
5812 /* XXX - catch the first fragment of a fragmented packet? */
5813 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5817 b1
= gen_portatom6(cstate
, 0, port
);
5821 b1
= gen_portatom6(cstate
, 2, port
);
5825 tmp
= gen_portatom6(cstate
, 0, port
);
5826 b1
= gen_portatom6(cstate
, 2, port
);
5832 tmp
= gen_portatom6(cstate
, 0, port
);
5833 b1
= gen_portatom6(cstate
, 2, port
);
5845 static struct block
*
5846 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5848 struct block
*b0
, *b1
, *tmp
;
5850 /* link proto ip6 */
5851 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5857 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5861 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5862 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5864 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5875 /* gen_portrange code */
5876 static struct block
*
5877 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5880 struct block
*b1
, *b2
;
5884 * Reverse the order of the ports, so v1 is the lower one.
5893 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5894 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5901 static struct block
*
5902 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5903 bpf_u_int32 proto
, int dir
)
5905 struct block
*b0
, *b1
, *tmp
;
5907 /* ip proto 'proto' and not a fragment other than the first fragment */
5908 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5909 b0
= gen_ipfrag(cstate
);
5914 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5918 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5922 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5923 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5929 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5930 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5940 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5952 static struct block
*
5953 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5956 struct block
*b0
, *b1
, *tmp
;
5959 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5965 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5970 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5971 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5973 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5984 static struct block
*
5985 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5988 struct block
*b1
, *b2
;
5992 * Reverse the order of the ports, so v1 is the lower one.
6001 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
6002 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
6009 static struct block
*
6010 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
6011 bpf_u_int32 proto
, int dir
)
6013 struct block
*b0
, *b1
, *tmp
;
6015 /* ip6 proto 'proto' */
6016 /* XXX - catch the first fragment of a fragmented packet? */
6017 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
6021 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6025 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6029 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6030 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6036 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
6037 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6049 static struct block
*
6050 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6053 struct block
*b0
, *b1
, *tmp
;
6055 /* link proto ip6 */
6056 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6062 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6067 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6068 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6070 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6082 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6091 v
= pcap_nametoproto(name
);
6092 if (v
== PROTO_UNDEF
)
6093 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6097 /* XXX should look up h/w protocol type based on cstate->linktype */
6098 v
= pcap_nametoeproto(name
);
6099 if (v
== PROTO_UNDEF
) {
6100 v
= pcap_nametollc(name
);
6101 if (v
== PROTO_UNDEF
)
6102 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6107 if (strcmp(name
, "esis") == 0)
6109 else if (strcmp(name
, "isis") == 0)
6111 else if (strcmp(name
, "clnp") == 0)
6114 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6124 #if !defined(NO_PROTOCHAIN)
6125 static struct block
*
6126 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6128 struct block
*b0
, *b
;
6129 struct slist
*s
[100];
6130 int fix2
, fix3
, fix4
, fix5
;
6131 int ahcheck
, again
, end
;
6133 int reg2
= alloc_reg(cstate
);
6135 memset(s
, 0, sizeof(s
));
6136 fix3
= fix4
= fix5
= 0;
6143 b0
= gen_protochain(cstate
, v
, Q_IP
);
6144 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6148 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
6153 * We don't handle variable-length prefixes before the link-layer
6154 * header, or variable-length link-layer headers, here yet.
6155 * We might want to add BPF instructions to do the protochain
6156 * work, to simplify that and, on platforms that have a BPF
6157 * interpreter with the new instructions, let the filtering
6158 * be done in the kernel. (We already require a modified BPF
6159 * engine to do the protochain stuff, to support backward
6160 * branches, and backward branch support is unlikely to appear
6161 * in kernel BPF engines.)
6163 if (cstate
->off_linkpl
.is_variable
)
6164 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6167 * To quote a comment in optimize.c:
6169 * "These data structures are used in a Cocke and Schwartz style
6170 * value numbering scheme. Since the flowgraph is acyclic,
6171 * exit values can be propagated from a node's predecessors
6172 * provided it is uniquely defined."
6174 * "Acyclic" means "no backward branches", which means "no
6175 * loops", so we have to turn the optimizer off.
6177 cstate
->no_optimize
= 1;
6180 * s[0] is a dummy entry to protect other BPF insn from damage
6181 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6182 * hard to find interdependency made by jump table fixup.
6185 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6190 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6193 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6194 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6196 /* X = ip->ip_hl << 2 */
6197 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6198 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6203 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6205 /* A = ip6->ip_nxt */
6206 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6207 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6209 /* X = sizeof(struct ip6_hdr) */
6210 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6216 bpf_error(cstate
, "unsupported proto to gen_protochain");
6220 /* again: if (A == v) goto end; else fall through; */
6222 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6224 s
[i
]->s
.jt
= NULL
; /*later*/
6225 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6229 #ifndef IPPROTO_NONE
6230 #define IPPROTO_NONE 59
6232 /* if (A == IPPROTO_NONE) goto end */
6233 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6234 s
[i
]->s
.jt
= NULL
; /*later*/
6235 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6236 s
[i
]->s
.k
= IPPROTO_NONE
;
6237 s
[fix5
]->s
.jf
= s
[i
];
6241 if (proto
== Q_IPV6
) {
6242 int v6start
, v6end
, v6advance
, j
;
6245 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6246 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6247 s
[i
]->s
.jt
= NULL
; /*later*/
6248 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6249 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6250 s
[fix2
]->s
.jf
= s
[i
];
6252 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6253 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6254 s
[i
]->s
.jt
= NULL
; /*later*/
6255 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6256 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6258 /* if (A == IPPROTO_ROUTING) goto v6advance */
6259 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6260 s
[i
]->s
.jt
= NULL
; /*later*/
6261 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6262 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6264 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6265 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6266 s
[i
]->s
.jt
= NULL
; /*later*/
6267 s
[i
]->s
.jf
= NULL
; /*later*/
6268 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6278 * A = P[X + packet head];
6279 * X = X + (P[X + packet head + 1] + 1) * 8;
6281 /* A = P[X + packet head] */
6282 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6283 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6286 s
[i
] = new_stmt(cstate
, BPF_ST
);
6289 /* A = P[X + packet head + 1]; */
6290 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6291 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6294 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6298 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6302 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6306 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6309 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6313 /* goto again; (must use BPF_JA for backward jump) */
6314 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6315 s
[i
]->s
.k
= again
- i
- 1;
6316 s
[i
- 1]->s
.jf
= s
[i
];
6320 for (j
= v6start
; j
<= v6end
; j
++)
6321 s
[j
]->s
.jt
= s
[v6advance
];
6324 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6326 s
[fix2
]->s
.jf
= s
[i
];
6332 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6333 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6334 s
[i
]->s
.jt
= NULL
; /*later*/
6335 s
[i
]->s
.jf
= NULL
; /*later*/
6336 s
[i
]->s
.k
= IPPROTO_AH
;
6338 s
[fix3
]->s
.jf
= s
[ahcheck
];
6345 * X = X + (P[X + 1] + 2) * 4;
6348 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6350 /* A = P[X + packet head]; */
6351 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6352 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6355 s
[i
] = new_stmt(cstate
, BPF_ST
);
6359 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6362 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6366 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6368 /* A = P[X + packet head] */
6369 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6370 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6373 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6377 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6381 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6384 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6388 /* goto again; (must use BPF_JA for backward jump) */
6389 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6390 s
[i
]->s
.k
= again
- i
- 1;
6395 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6397 s
[fix2
]->s
.jt
= s
[end
];
6398 s
[fix4
]->s
.jf
= s
[end
];
6399 s
[fix5
]->s
.jt
= s
[end
];
6406 for (i
= 0; i
< max
- 1; i
++)
6407 s
[i
]->next
= s
[i
+ 1];
6408 s
[max
- 1]->next
= NULL
;
6413 b
= new_block(cstate
, JMP(BPF_JEQ
));
6414 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6417 free_reg(cstate
, reg2
);
6422 #endif /* !defined(NO_PROTOCHAIN) */
6425 * Generate code that checks whether the packet is a packet for protocol
6426 * <proto> and whether the type field in that protocol's header has
6427 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6428 * IP packet and checks the protocol number in the IP header against <v>.
6430 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6431 * against Q_IP and Q_IPV6.
6433 static struct block
*
6434 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6436 struct block
*b0
, *b1
;
6439 if (dir
!= Q_DEFAULT
)
6440 bpf_error(cstate
, "direction applied to 'proto'");
6444 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6445 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6450 return gen_linktype(cstate
, v
);
6454 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6455 * not LLC encapsulation with LLCSAP_IP.
6457 * For IEEE 802 networks - which includes 802.5 token ring
6458 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6459 * says that SNAP encapsulation is used, not LLC encapsulation
6462 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6463 * RFC 2225 say that SNAP encapsulation is used, not LLC
6464 * encapsulation with LLCSAP_IP.
6466 * So we always check for ETHERTYPE_IP.
6468 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6469 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6487 break; // invalid qualifier
6490 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6492 * Also check for a fragment header before the final
6495 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6496 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6498 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6509 break; // invalid qualifier
6512 switch (cstate
->linktype
) {
6516 * Frame Relay packets typically have an OSI
6517 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6518 * generates code to check for all the OSI
6519 * NLPIDs, so calling it and then adding a check
6520 * for the particular NLPID for which we're
6521 * looking is bogus, as we can just check for
6524 * What we check for is the NLPID and a frame
6525 * control field value of UI, i.e. 0x03 followed
6528 * XXX - assumes a 2-byte Frame Relay header with
6529 * DLCI and flags. What if the address is longer?
6531 * XXX - what about SNAP-encapsulated frames?
6533 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6539 * Cisco uses an Ethertype lookalike - for OSI,
6542 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6543 /* OSI in C-HDLC is stuffed with a fudge byte */
6544 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6549 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6550 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6556 break; // invalid qualifier
6559 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6561 * 4 is the offset of the PDU type relative to the IS-IS
6564 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6581 break; // invalid qualifier
6587 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6592 * Convert a non-numeric name to a port number.
6595 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6597 struct addrinfo hints
, *res
, *ai
;
6599 struct sockaddr_in
*in4
;
6601 struct sockaddr_in6
*in6
;
6606 * We check for both TCP and UDP in case there are
6607 * ambiguous entries.
6609 memset(&hints
, 0, sizeof(hints
));
6610 hints
.ai_family
= PF_UNSPEC
;
6611 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6612 hints
.ai_protocol
= ipproto
;
6613 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6620 * No such port. Just return -1.
6627 * We don't use strerror() because it's not
6628 * guaranteed to be thread-safe on all platforms
6629 * (probably because it might use a non-thread-local
6630 * buffer into which to format an error message
6631 * if the error code isn't one for which it has
6632 * a canned string; three cheers for C string
6635 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6637 port
= -2; /* a real error */
6643 * This is a real error, not just "there's
6644 * no such service name".
6646 * We don't use gai_strerror() because it's not
6647 * guaranteed to be thread-safe on all platforms
6648 * (probably because it might use a non-thread-local
6649 * buffer into which to format an error message
6650 * if the error code isn't one for which it has
6651 * a canned string; three cheers for C string
6654 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6656 port
= -2; /* a real error */
6661 * OK, we found it. Did it find anything?
6663 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6665 * Does it have an address?
6667 if (ai
->ai_addr
!= NULL
) {
6669 * Yes. Get a port number; we're done.
6671 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6672 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6673 port
= ntohs(in4
->sin_port
);
6677 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6678 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6679 port
= ntohs(in6
->sin6_port
);
6691 * Convert a string to a port number.
6694 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6704 * See if it's a number.
6706 ret
= stoulen(string
, string_size
, &val
, cstate
);
6710 /* Unknown port type - it's just a number. */
6711 *proto
= PROTO_UNDEF
;
6714 case STOULEN_NOT_OCTAL_NUMBER
:
6715 case STOULEN_NOT_HEX_NUMBER
:
6716 case STOULEN_NOT_DECIMAL_NUMBER
:
6718 * Not a valid number; try looking it up as a port.
6720 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6721 memcpy(cpy
, string
, string_size
);
6722 cpy
[string_size
] = '\0';
6723 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6724 if (tcp_port
== -2) {
6726 * We got a hard error; the error string has
6730 longjmp(cstate
->top_ctx
, 1);
6733 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6734 if (udp_port
== -2) {
6736 * We got a hard error; the error string has
6740 longjmp(cstate
->top_ctx
, 1);
6745 * We need to check /etc/services for ambiguous entries.
6746 * If we find an ambiguous entry, and it has the
6747 * same port number, change the proto to PROTO_UNDEF
6748 * so both TCP and UDP will be checked.
6750 if (tcp_port
>= 0) {
6751 val
= (bpf_u_int32
)tcp_port
;
6752 *proto
= IPPROTO_TCP
;
6753 if (udp_port
>= 0) {
6754 if (udp_port
== tcp_port
)
6755 *proto
= PROTO_UNDEF
;
6758 /* Can't handle ambiguous names that refer
6759 to different port numbers. */
6760 warning("ambiguous port %s in /etc/services",
6767 if (udp_port
>= 0) {
6768 val
= (bpf_u_int32
)udp_port
;
6769 *proto
= IPPROTO_UDP
;
6773 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6775 longjmp(cstate
->top_ctx
, 1);
6782 /* Error already set. */
6783 longjmp(cstate
->top_ctx
, 1);
6790 /* Should not happen */
6791 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6792 longjmp(cstate
->top_ctx
, 1);
6799 * Convert a string in the form PPP-PPP, which correspond to ports, to
6800 * a starting and ending port in a port range.
6803 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6804 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6807 const char *first
, *second
;
6808 size_t first_size
, second_size
;
6811 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6812 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6815 * Make sure there are no other hyphens.
6817 * XXX - we support named ports, but there are some port names
6818 * in /etc/services that include hyphens, so this would rule
6821 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6822 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6826 * Get the length of the first port.
6829 first_size
= hyphen_off
- string
;
6830 if (first_size
== 0) {
6831 /* Range of "-port", which we don't support. */
6832 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6836 * Try to convert it to a port.
6838 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6839 save_proto
= *proto
;
6842 * Get the length of the second port.
6844 second
= hyphen_off
+ 1;
6845 second_size
= strlen(second
);
6846 if (second_size
== 0) {
6847 /* Range of "port-", which we don't support. */
6848 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6852 * Try to convert it to a port.
6854 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6855 if (*proto
!= save_proto
)
6856 *proto
= PROTO_UNDEF
;
6860 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6862 int proto
= q
.proto
;
6866 bpf_u_int32 mask
, addr
;
6867 struct addrinfo
*res
, *res0
;
6868 struct sockaddr_in
*sin4
;
6871 struct sockaddr_in6
*sin6
;
6872 struct in6_addr mask128
;
6874 struct block
*b
, *tmp
;
6875 int port
, real_proto
;
6876 bpf_u_int32 port1
, port2
;
6879 * Catch errors reported by us and routines below us, and return NULL
6882 if (setjmp(cstate
->top_ctx
))
6888 addr
= pcap_nametonetaddr(name
);
6890 bpf_error(cstate
, "unknown network '%s'", name
);
6891 /* Left justify network addr and calculate its network mask */
6893 while (addr
&& (addr
& 0xff000000) == 0) {
6897 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6901 if (proto
== Q_LINK
) {
6902 switch (cstate
->linktype
) {
6905 case DLT_NETANALYZER
:
6906 case DLT_NETANALYZER_TRANSPARENT
:
6907 eaddr
= pcap_ether_hostton(name
);
6910 "unknown ether host '%s'", name
);
6911 tmp
= gen_prevlinkhdr_check(cstate
);
6912 b
= gen_ehostop(cstate
, eaddr
, dir
);
6919 eaddr
= pcap_ether_hostton(name
);
6922 "unknown FDDI host '%s'", name
);
6923 b
= gen_fhostop(cstate
, eaddr
, dir
);
6928 eaddr
= pcap_ether_hostton(name
);
6931 "unknown token ring host '%s'", name
);
6932 b
= gen_thostop(cstate
, eaddr
, dir
);
6936 case DLT_IEEE802_11
:
6937 case DLT_PRISM_HEADER
:
6938 case DLT_IEEE802_11_RADIO_AVS
:
6939 case DLT_IEEE802_11_RADIO
:
6941 eaddr
= pcap_ether_hostton(name
);
6944 "unknown 802.11 host '%s'", name
);
6945 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6949 case DLT_IP_OVER_FC
:
6950 eaddr
= pcap_ether_hostton(name
);
6953 "unknown Fibre Channel host '%s'", name
);
6954 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6959 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6960 } else if (proto
== Q_DECNET
) {
6962 * A long time ago on Ultrix libpcap supported
6963 * translation of DECnet host names into DECnet
6964 * addresses, but this feature is history now.
6966 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6969 memset(&mask128
, 0xff, sizeof(mask128
));
6971 res0
= res
= pcap_nametoaddrinfo(name
);
6973 bpf_error(cstate
, "unknown host '%s'", name
);
6980 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6981 tproto
== Q_DEFAULT
) {
6987 for (res
= res0
; res
; res
= res
->ai_next
) {
6988 switch (res
->ai_family
) {
6991 if (tproto
== Q_IPV6
)
6995 sin4
= (struct sockaddr_in
*)
6997 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6998 0xffffffff, tproto
, dir
, q
.addr
);
7002 if (tproto6
== Q_IP
)
7005 sin6
= (struct sockaddr_in6
*)
7007 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
7008 &mask128
, tproto6
, dir
, q
.addr
);
7021 bpf_error(cstate
, "unknown host '%s'%s", name
,
7022 (proto
== Q_DEFAULT
)
7024 : " for specified address family");
7030 if (proto
!= Q_DEFAULT
&&
7031 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7032 bpf_error(cstate
, "illegal qualifier of 'port'");
7033 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
7034 bpf_error(cstate
, "unknown port '%s'", name
);
7035 if (proto
== Q_UDP
) {
7036 if (real_proto
== IPPROTO_TCP
)
7037 bpf_error(cstate
, "port '%s' is tcp", name
);
7038 else if (real_proto
== IPPROTO_SCTP
)
7039 bpf_error(cstate
, "port '%s' is sctp", name
);
7041 /* override PROTO_UNDEF */
7042 real_proto
= IPPROTO_UDP
;
7044 if (proto
== Q_TCP
) {
7045 if (real_proto
== IPPROTO_UDP
)
7046 bpf_error(cstate
, "port '%s' is udp", name
);
7048 else if (real_proto
== IPPROTO_SCTP
)
7049 bpf_error(cstate
, "port '%s' is sctp", name
);
7051 /* override PROTO_UNDEF */
7052 real_proto
= IPPROTO_TCP
;
7054 if (proto
== Q_SCTP
) {
7055 if (real_proto
== IPPROTO_UDP
)
7056 bpf_error(cstate
, "port '%s' is udp", name
);
7058 else if (real_proto
== IPPROTO_TCP
)
7059 bpf_error(cstate
, "port '%s' is tcp", name
);
7061 /* override PROTO_UNDEF */
7062 real_proto
= IPPROTO_SCTP
;
7065 bpf_error(cstate
, "illegal port number %d < 0", port
);
7067 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7068 b
= gen_port(cstate
, port
, real_proto
, dir
);
7069 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7073 if (proto
!= Q_DEFAULT
&&
7074 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7075 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7076 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7077 if (proto
== Q_UDP
) {
7078 if (real_proto
== IPPROTO_TCP
)
7079 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7080 else if (real_proto
== IPPROTO_SCTP
)
7081 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7083 /* override PROTO_UNDEF */
7084 real_proto
= IPPROTO_UDP
;
7086 if (proto
== Q_TCP
) {
7087 if (real_proto
== IPPROTO_UDP
)
7088 bpf_error(cstate
, "port in range '%s' is udp", name
);
7089 else if (real_proto
== IPPROTO_SCTP
)
7090 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7092 /* override PROTO_UNDEF */
7093 real_proto
= IPPROTO_TCP
;
7095 if (proto
== Q_SCTP
) {
7096 if (real_proto
== IPPROTO_UDP
)
7097 bpf_error(cstate
, "port in range '%s' is udp", name
);
7098 else if (real_proto
== IPPROTO_TCP
)
7099 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7101 /* override PROTO_UNDEF */
7102 real_proto
= IPPROTO_SCTP
;
7105 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7107 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7109 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7110 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7115 eaddr
= pcap_ether_hostton(name
);
7117 bpf_error(cstate
, "unknown ether host: %s", name
);
7119 res
= pcap_nametoaddrinfo(name
);
7122 bpf_error(cstate
, "unknown host '%s'", name
);
7123 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7128 bpf_error(cstate
, "unknown host '%s'", name
);
7131 bpf_error(cstate
, "'gateway' not supported in this configuration");
7135 real_proto
= lookup_proto(cstate
, name
, proto
);
7136 if (real_proto
>= 0)
7137 return gen_proto(cstate
, real_proto
, proto
, dir
);
7139 bpf_error(cstate
, "unknown protocol: %s", name
);
7141 #if !defined(NO_PROTOCHAIN)
7143 real_proto
= lookup_proto(cstate
, name
, proto
);
7144 if (real_proto
>= 0)
7145 return gen_protochain(cstate
, real_proto
, proto
);
7147 bpf_error(cstate
, "unknown protocol: %s", name
);
7148 #endif /* !defined(NO_PROTOCHAIN) */
7159 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7160 bpf_u_int32 masklen
, struct qual q
)
7162 register int nlen
, mlen
;
7167 * Catch errors reported by us and routines below us, and return NULL
7170 if (setjmp(cstate
->top_ctx
))
7173 nlen
= pcapint_atoin(s1
, &n
);
7175 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7176 /* Promote short ipaddr */
7180 mlen
= pcapint_atoin(s2
, &m
);
7182 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7183 /* Promote short ipaddr */
7186 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7189 /* Convert mask len to mask */
7191 bpf_error(cstate
, "mask length must be <= 32");
7192 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7193 m
= (bpf_u_int32
)m64
;
7195 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7202 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7205 bpf_error(cstate
, "Mask syntax for networks only");
7212 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7220 * Catch errors reported by us and routines below us, and return NULL
7223 if (setjmp(cstate
->top_ctx
))
7230 * v contains a 32-bit unsigned parsed from a string of the
7231 * form {N}, which could be decimal, hexadecimal or octal.
7232 * Although it would be possible to use the value as a raw
7233 * 16-bit DECnet address when the value fits into 16 bits, this
7234 * would be a questionable feature: DECnet address wire
7235 * encoding is little-endian, so this would not work as
7236 * intuitively as the same works for [big-endian] IPv4
7237 * addresses (0x01020304 means 1.2.3.4).
7239 if (proto
== Q_DECNET
)
7240 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7242 } else if (proto
== Q_DECNET
) {
7244 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7245 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7246 * for a valid DECnet address.
7248 vlen
= pcapint_atodn(s
, &v
);
7250 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7253 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7254 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7257 vlen
= pcapint_atoin(s
, &v
);
7259 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7267 if (proto
== Q_DECNET
)
7268 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7269 else if (proto
== Q_LINK
) {
7270 bpf_error(cstate
, "illegal link layer address");
7273 if (s
== NULL
&& q
.addr
== Q_NET
) {
7274 /* Promote short net number */
7275 while (v
&& (v
& 0xff000000) == 0) {
7280 /* Promote short ipaddr */
7282 mask
<<= 32 - vlen
;
7284 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7289 proto
= IPPROTO_UDP
;
7290 else if (proto
== Q_TCP
)
7291 proto
= IPPROTO_TCP
;
7292 else if (proto
== Q_SCTP
)
7293 proto
= IPPROTO_SCTP
;
7294 else if (proto
== Q_DEFAULT
)
7295 proto
= PROTO_UNDEF
;
7297 bpf_error(cstate
, "illegal qualifier of 'port'");
7300 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7304 b
= gen_port(cstate
, v
, proto
, dir
);
7305 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7311 proto
= IPPROTO_UDP
;
7312 else if (proto
== Q_TCP
)
7313 proto
= IPPROTO_TCP
;
7314 else if (proto
== Q_SCTP
)
7315 proto
= IPPROTO_SCTP
;
7316 else if (proto
== Q_DEFAULT
)
7317 proto
= PROTO_UNDEF
;
7319 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7322 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7326 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7327 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7332 bpf_error(cstate
, "'gateway' requires a name");
7336 return gen_proto(cstate
, v
, proto
, dir
);
7338 #if !defined(NO_PROTOCHAIN)
7340 return gen_protochain(cstate
, v
, proto
);
7356 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7359 struct addrinfo
*res
;
7360 struct in6_addr
*addr
;
7361 struct in6_addr mask
;
7363 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7366 * Catch errors reported by us and routines below us, and return NULL
7369 if (setjmp(cstate
->top_ctx
))
7372 res
= pcap_nametoaddrinfo(s
);
7374 bpf_error(cstate
, "invalid ip6 address %s", s
);
7377 bpf_error(cstate
, "%s resolved to multiple address", s
);
7378 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7380 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7381 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7382 memset(&mask
, 0, sizeof(mask
));
7383 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7385 mask
.s6_addr
[masklen
/ 8] =
7386 (0xff << (8 - masklen
% 8)) & 0xff;
7389 memcpy(a
, addr
, sizeof(a
));
7390 memcpy(m
, &mask
, sizeof(m
));
7391 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7392 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7393 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7401 bpf_error(cstate
, "Mask syntax for networks only");
7405 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7411 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7418 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7420 struct block
*b
, *tmp
;
7423 * Catch errors reported by us and routines below us, and return NULL
7426 if (setjmp(cstate
->top_ctx
))
7429 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7430 cstate
->e
= pcap_ether_aton(s
);
7431 if (cstate
->e
== NULL
)
7432 bpf_error(cstate
, "malloc");
7433 switch (cstate
->linktype
) {
7435 case DLT_NETANALYZER
:
7436 case DLT_NETANALYZER_TRANSPARENT
:
7437 tmp
= gen_prevlinkhdr_check(cstate
);
7438 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7443 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7446 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7448 case DLT_IEEE802_11
:
7449 case DLT_PRISM_HEADER
:
7450 case DLT_IEEE802_11_RADIO_AVS
:
7451 case DLT_IEEE802_11_RADIO
:
7453 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7455 case DLT_IP_OVER_FC
:
7456 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7461 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7468 bpf_error(cstate
, "ethernet address used in non-ether expression");
7473 sappend(struct slist
*s0
, struct slist
*s1
)
7476 * This is definitely not the best way to do this, but the
7477 * lists will rarely get long.
7484 static struct slist
*
7485 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7489 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7494 static struct slist
*
7495 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7499 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7505 * Modify "index" to use the value stored into its register as an
7506 * offset relative to the beginning of the header for the protocol
7507 * "proto", and allocate a register and put an item "size" bytes long
7508 * (1, 2, or 4) at that offset into that register, making it the register
7511 static struct arth
*
7512 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7516 struct slist
*s
, *tmp
;
7518 int regno
= alloc_reg(cstate
);
7520 free_reg(cstate
, inst
->regno
);
7524 bpf_error(cstate
, "data size must be 1, 2, or 4");
7541 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7545 * The offset is relative to the beginning of the packet
7546 * data, if we have a radio header. (If we don't, this
7549 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7550 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7551 cstate
->linktype
!= DLT_PRISM_HEADER
)
7552 bpf_error(cstate
, "radio information not present in capture");
7555 * Load into the X register the offset computed into the
7556 * register specified by "index".
7558 s
= xfer_to_x(cstate
, inst
);
7561 * Load the item at that offset.
7563 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7565 sappend(inst
->s
, s
);
7570 * The offset is relative to the beginning of
7571 * the link-layer header.
7573 * XXX - what about ATM LANE? Should the index be
7574 * relative to the beginning of the AAL5 frame, so
7575 * that 0 refers to the beginning of the LE Control
7576 * field, or relative to the beginning of the LAN
7577 * frame, so that 0 refers, for Ethernet LANE, to
7578 * the beginning of the destination address?
7580 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7583 * If "s" is non-null, it has code to arrange that the
7584 * X register contains the length of the prefix preceding
7585 * the link-layer header. Add to it the offset computed
7586 * into the register specified by "index", and move that
7587 * into the X register. Otherwise, just load into the X
7588 * register the offset computed into the register specified
7592 sappend(s
, xfer_to_a(cstate
, inst
));
7593 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7594 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7596 s
= xfer_to_x(cstate
, inst
);
7599 * Load the item at the sum of the offset we've put in the
7600 * X register and the offset of the start of the link
7601 * layer header (which is 0 if the radio header is
7602 * variable-length; that header length is what we put
7603 * into the X register and then added to the index).
7605 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7606 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7608 sappend(inst
->s
, s
);
7622 * The offset is relative to the beginning of
7623 * the network-layer header.
7624 * XXX - are there any cases where we want
7625 * cstate->off_nl_nosnap?
7627 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7630 * If "s" is non-null, it has code to arrange that the
7631 * X register contains the variable part of the offset
7632 * of the link-layer payload. Add to it the offset
7633 * computed into the register specified by "index",
7634 * and move that into the X register. Otherwise, just
7635 * load into the X register the offset computed into
7636 * the register specified by "index".
7639 sappend(s
, xfer_to_a(cstate
, inst
));
7640 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7641 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7643 s
= xfer_to_x(cstate
, inst
);
7646 * Load the item at the sum of the offset we've put in the
7647 * X register, the offset of the start of the network
7648 * layer header from the beginning of the link-layer
7649 * payload, and the constant part of the offset of the
7650 * start of the link-layer payload.
7652 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7653 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7655 sappend(inst
->s
, s
);
7658 * Do the computation only if the packet contains
7659 * the protocol in question.
7661 b
= gen_proto_abbrev_internal(cstate
, proto
);
7663 gen_and(inst
->b
, b
);
7677 * The offset is relative to the beginning of
7678 * the transport-layer header.
7680 * Load the X register with the length of the IPv4 header
7681 * (plus the offset of the link-layer header, if it's
7682 * a variable-length header), in bytes.
7684 * XXX - are there any cases where we want
7685 * cstate->off_nl_nosnap?
7686 * XXX - we should, if we're built with
7687 * IPv6 support, generate code to load either
7688 * IPv4, IPv6, or both, as appropriate.
7690 s
= gen_loadx_iphdrlen(cstate
);
7693 * The X register now contains the sum of the variable
7694 * part of the offset of the link-layer payload and the
7695 * length of the network-layer header.
7697 * Load into the A register the offset relative to
7698 * the beginning of the transport layer header,
7699 * add the X register to that, move that to the
7700 * X register, and load with an offset from the
7701 * X register equal to the sum of the constant part of
7702 * the offset of the link-layer payload and the offset,
7703 * relative to the beginning of the link-layer payload,
7704 * of the network-layer header.
7706 sappend(s
, xfer_to_a(cstate
, inst
));
7707 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7708 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7709 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7710 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7711 sappend(inst
->s
, s
);
7714 * Do the computation only if the packet contains
7715 * the protocol in question - which is true only
7716 * if this is an IP datagram and is the first or
7717 * only fragment of that datagram.
7719 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7721 gen_and(inst
->b
, b
);
7722 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7727 * Do the computation only if the packet contains
7728 * the protocol in question.
7730 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7732 gen_and(inst
->b
, b
);
7736 * Check if we have an icmp6 next header
7738 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7740 gen_and(inst
->b
, b
);
7743 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7745 * If "s" is non-null, it has code to arrange that the
7746 * X register contains the variable part of the offset
7747 * of the link-layer payload. Add to it the offset
7748 * computed into the register specified by "index",
7749 * and move that into the X register. Otherwise, just
7750 * load into the X register the offset computed into
7751 * the register specified by "index".
7754 sappend(s
, xfer_to_a(cstate
, inst
));
7755 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7756 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7758 s
= xfer_to_x(cstate
, inst
);
7761 * Load the item at the sum of the offset we've put in the
7762 * X register, the offset of the start of the network
7763 * layer header from the beginning of the link-layer
7764 * payload, and the constant part of the offset of the
7765 * start of the link-layer payload.
7767 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7768 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7771 sappend(inst
->s
, s
);
7775 inst
->regno
= regno
;
7776 s
= new_stmt(cstate
, BPF_ST
);
7778 sappend(inst
->s
, s
);
7784 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7788 * Catch errors reported by us and routines below us, and return NULL
7791 if (setjmp(cstate
->top_ctx
))
7794 return gen_load_internal(cstate
, proto
, inst
, size
);
7797 static struct block
*
7798 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7799 struct arth
*a1
, int reversed
)
7801 struct slist
*s0
, *s1
, *s2
;
7802 struct block
*b
, *tmp
;
7804 s0
= xfer_to_x(cstate
, a1
);
7805 s1
= xfer_to_a(cstate
, a0
);
7806 if (code
== BPF_JEQ
) {
7807 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7808 b
= new_block(cstate
, JMP(code
));
7812 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7818 sappend(a0
->s
, a1
->s
);
7822 free_reg(cstate
, a0
->regno
);
7823 free_reg(cstate
, a1
->regno
);
7825 /* 'and' together protocol checks */
7828 gen_and(a0
->b
, tmp
= a1
->b
);
7842 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7843 struct arth
*a1
, int reversed
)
7846 * Catch errors reported by us and routines below us, and return NULL
7849 if (setjmp(cstate
->top_ctx
))
7852 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7856 gen_loadlen(compiler_state_t
*cstate
)
7863 * Catch errors reported by us and routines below us, and return NULL
7866 if (setjmp(cstate
->top_ctx
))
7869 regno
= alloc_reg(cstate
);
7870 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7871 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7872 s
->next
= new_stmt(cstate
, BPF_ST
);
7873 s
->next
->s
.k
= regno
;
7880 static struct arth
*
7881 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7887 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7889 reg
= alloc_reg(cstate
);
7891 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7893 s
->next
= new_stmt(cstate
, BPF_ST
);
7902 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7905 * Catch errors reported by us and routines below us, and return NULL
7908 if (setjmp(cstate
->top_ctx
))
7911 return gen_loadi_internal(cstate
, val
);
7915 * The a_arg dance is to avoid annoying whining by compilers that
7916 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7917 * It's not *used* after setjmp returns.
7920 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7922 struct arth
*a
= a_arg
;
7926 * Catch errors reported by us and routines below us, and return NULL
7929 if (setjmp(cstate
->top_ctx
))
7932 s
= xfer_to_a(cstate
, a
);
7934 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7937 s
= new_stmt(cstate
, BPF_ST
);
7945 * The a0_arg dance is to avoid annoying whining by compilers that
7946 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7947 * It's not *used* after setjmp returns.
7950 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7953 struct arth
*a0
= a0_arg
;
7954 struct slist
*s0
, *s1
, *s2
;
7957 * Catch errors reported by us and routines below us, and return NULL
7960 if (setjmp(cstate
->top_ctx
))
7964 * Disallow division by, or modulus by, zero; we do this here
7965 * so that it gets done even if the optimizer is disabled.
7967 * Also disallow shifts by a value greater than 31; we do this
7968 * here, for the same reason.
7970 if (code
== BPF_DIV
) {
7971 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7972 bpf_error(cstate
, "division by zero");
7973 } else if (code
== BPF_MOD
) {
7974 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7975 bpf_error(cstate
, "modulus by zero");
7976 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7977 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7978 bpf_error(cstate
, "shift by more than 31 bits");
7980 s0
= xfer_to_x(cstate
, a1
);
7981 s1
= xfer_to_a(cstate
, a0
);
7982 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7987 sappend(a0
->s
, a1
->s
);
7989 free_reg(cstate
, a0
->regno
);
7990 free_reg(cstate
, a1
->regno
);
7992 s0
= new_stmt(cstate
, BPF_ST
);
7993 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
8000 * Initialize the table of used registers and the current register.
8003 init_regs(compiler_state_t
*cstate
)
8006 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
8010 * Return the next free register.
8013 alloc_reg(compiler_state_t
*cstate
)
8015 int n
= BPF_MEMWORDS
;
8018 if (cstate
->regused
[cstate
->curreg
])
8019 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
8021 cstate
->regused
[cstate
->curreg
] = 1;
8022 return cstate
->curreg
;
8025 bpf_error(cstate
, "too many registers needed to evaluate expression");
8030 * Return a register to the table so it can
8034 free_reg(compiler_state_t
*cstate
, int n
)
8036 cstate
->regused
[n
] = 0;
8039 static struct block
*
8040 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8045 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8046 b
= new_block(cstate
, JMP(jmp
));
8054 gen_greater(compiler_state_t
*cstate
, int n
)
8057 * Catch errors reported by us and routines below us, and return NULL
8060 if (setjmp(cstate
->top_ctx
))
8063 return gen_len(cstate
, BPF_JGE
, n
);
8067 * Actually, this is less than or equal.
8070 gen_less(compiler_state_t
*cstate
, int n
)
8075 * Catch errors reported by us and routines below us, and return NULL
8078 if (setjmp(cstate
->top_ctx
))
8081 b
= gen_len(cstate
, BPF_JGT
, n
);
8088 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8089 * the beginning of the link-layer header.
8090 * XXX - that means you can't test values in the radiotap header, but
8091 * as that header is difficult if not impossible to parse generally
8092 * without a loop, that might not be a severe problem. A new keyword
8093 * "radio" could be added for that, although what you'd really want
8094 * would be a way of testing particular radio header values, which
8095 * would generate code appropriate to the radio header in question.
8098 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8104 * Catch errors reported by us and routines below us, and return NULL
8107 if (setjmp(cstate
->top_ctx
))
8115 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8118 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8122 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8126 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8130 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8134 b
= new_block(cstate
, JMP(BPF_JEQ
));
8142 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8144 bpf_u_int32 hostmask
;
8145 struct block
*b0
, *b1
, *b2
;
8146 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8149 * Catch errors reported by us and routines below us, and return NULL
8152 if (setjmp(cstate
->top_ctx
))
8159 switch (cstate
->linktype
) {
8161 case DLT_ARCNET_LINUX
:
8162 // ARCnet broadcast is [8-bit] destination address 0.
8163 return gen_ahostop(cstate
, 0, Q_DST
);
8165 case DLT_NETANALYZER
:
8166 case DLT_NETANALYZER_TRANSPARENT
:
8167 b1
= gen_prevlinkhdr_check(cstate
);
8168 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8173 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8175 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8176 case DLT_IEEE802_11
:
8177 case DLT_PRISM_HEADER
:
8178 case DLT_IEEE802_11_RADIO_AVS
:
8179 case DLT_IEEE802_11_RADIO
:
8181 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8182 case DLT_IP_OVER_FC
:
8183 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8185 fail_kw_on_dlt(cstate
, "broadcast");
8190 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8191 * as an indication that we don't know the netmask, and fail
8194 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8195 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8196 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8197 hostmask
= ~cstate
->netmask
;
8198 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8199 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8204 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
8209 * Generate code to test the low-order bit of a MAC address (that's
8210 * the bottom bit of the *first* byte).
8212 static struct block
*
8213 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8215 register struct block
*b0
;
8216 register struct slist
*s
;
8218 /* link[offset] & 1 != 0 */
8219 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8220 b0
= new_block(cstate
, JMP(BPF_JSET
));
8227 gen_multicast(compiler_state_t
*cstate
, int proto
)
8229 register struct block
*b0
, *b1
, *b2
;
8230 register struct slist
*s
;
8233 * Catch errors reported by us and routines below us, and return NULL
8236 if (setjmp(cstate
->top_ctx
))
8243 switch (cstate
->linktype
) {
8245 case DLT_ARCNET_LINUX
:
8246 // ARCnet multicast is the same as broadcast.
8247 return gen_ahostop(cstate
, 0, Q_DST
);
8249 case DLT_NETANALYZER
:
8250 case DLT_NETANALYZER_TRANSPARENT
:
8251 b1
= gen_prevlinkhdr_check(cstate
);
8252 /* ether[0] & 1 != 0 */
8253 b0
= gen_mac_multicast(cstate
, 0);
8259 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8261 * XXX - was that referring to bit-order issues?
8263 /* fddi[1] & 1 != 0 */
8264 return gen_mac_multicast(cstate
, 1);
8266 /* tr[2] & 1 != 0 */
8267 return gen_mac_multicast(cstate
, 2);
8268 case DLT_IEEE802_11
:
8269 case DLT_PRISM_HEADER
:
8270 case DLT_IEEE802_11_RADIO_AVS
:
8271 case DLT_IEEE802_11_RADIO
:
8276 * For control frames, there is no DA.
8278 * For management frames, DA is at an
8279 * offset of 4 from the beginning of
8282 * For data frames, DA is at an offset
8283 * of 4 from the beginning of the packet
8284 * if To DS is clear and at an offset of
8285 * 16 from the beginning of the packet
8290 * Generate the tests to be done for data frames.
8292 * First, check for To DS set, i.e. "link[1] & 0x01".
8294 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8295 b1
= new_block(cstate
, JMP(BPF_JSET
));
8296 b1
->s
.k
= 0x01; /* To DS */
8300 * If To DS is set, the DA is at 16.
8302 b0
= gen_mac_multicast(cstate
, 16);
8306 * Now, check for To DS not set, i.e. check
8307 * "!(link[1] & 0x01)".
8309 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8310 b2
= new_block(cstate
, JMP(BPF_JSET
));
8311 b2
->s
.k
= 0x01; /* To DS */
8316 * If To DS is not set, the DA is at 4.
8318 b1
= gen_mac_multicast(cstate
, 4);
8322 * Now OR together the last two checks. That gives
8323 * the complete set of checks for data frames.
8328 * Now check for a data frame.
8329 * I.e, check "link[0] & 0x08".
8331 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8332 b1
= new_block(cstate
, JMP(BPF_JSET
));
8337 * AND that with the checks done for data frames.
8342 * If the high-order bit of the type value is 0, this
8343 * is a management frame.
8344 * I.e, check "!(link[0] & 0x08)".
8346 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8347 b2
= new_block(cstate
, JMP(BPF_JSET
));
8353 * For management frames, the DA is at 4.
8355 b1
= gen_mac_multicast(cstate
, 4);
8359 * OR that with the checks done for data frames.
8360 * That gives the checks done for management and
8366 * If the low-order bit of the type value is 1,
8367 * this is either a control frame or a frame
8368 * with a reserved type, and thus not a
8371 * I.e., check "!(link[0] & 0x04)".
8373 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8374 b1
= new_block(cstate
, JMP(BPF_JSET
));
8380 * AND that with the checks for data and management
8385 case DLT_IP_OVER_FC
:
8386 b0
= gen_mac_multicast(cstate
, 2);
8391 fail_kw_on_dlt(cstate
, "multicast");
8395 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8396 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8401 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8402 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8406 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8412 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8413 * we can look at special meta-data in the filter expression; otherwise we
8414 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8415 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8416 * pcap_activate() conditionally sets.
8419 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8421 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8423 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8425 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8430 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8432 register struct block
*b0
;
8435 * Catch errors reported by us and routines below us, and return NULL
8438 if (setjmp(cstate
->top_ctx
))
8442 * Only some data link types support ifindex qualifiers.
8444 switch (cstate
->linktype
) {
8445 case DLT_LINUX_SLL2
:
8446 /* match packets on this interface */
8447 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8450 #if defined(__linux__)
8451 require_basic_bpf_extensions(cstate
, "ifindex");
8453 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8455 #else /* defined(__linux__) */
8456 fail_kw_on_dlt(cstate
, "ifindex");
8458 #endif /* defined(__linux__) */
8464 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8465 * Outbound traffic is sent by this machine, while inbound traffic is
8466 * sent by a remote machine (and may include packets destined for a
8467 * unicast or multicast link-layer address we are not subscribing to).
8468 * These are the same definitions implemented by pcap_setdirection().
8469 * Capturing only unicast traffic destined for this host is probably
8470 * better accomplished using a higher-layer filter.
8473 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8475 register struct block
*b0
;
8478 * Catch errors reported by us and routines below us, and return NULL
8481 if (setjmp(cstate
->top_ctx
))
8485 * Only some data link types support inbound/outbound qualifiers.
8487 switch (cstate
->linktype
) {
8489 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8490 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8494 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8495 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8499 /* match outgoing packets */
8500 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8502 /* to filter on inbound traffic, invert the match */
8507 case DLT_LINUX_SLL2
:
8508 /* match outgoing packets */
8509 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8511 /* to filter on inbound traffic, invert the match */
8517 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8518 outbound
? PF_OUT
: PF_IN
);
8522 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8525 case DLT_JUNIPER_MFR
:
8526 case DLT_JUNIPER_MLFR
:
8527 case DLT_JUNIPER_MLPPP
:
8528 case DLT_JUNIPER_ATM1
:
8529 case DLT_JUNIPER_ATM2
:
8530 case DLT_JUNIPER_PPPOE
:
8531 case DLT_JUNIPER_PPPOE_ATM
:
8532 case DLT_JUNIPER_GGSN
:
8533 case DLT_JUNIPER_ES
:
8534 case DLT_JUNIPER_MONITOR
:
8535 case DLT_JUNIPER_SERVICES
:
8536 case DLT_JUNIPER_ETHER
:
8537 case DLT_JUNIPER_PPP
:
8538 case DLT_JUNIPER_FRELAY
:
8539 case DLT_JUNIPER_CHDLC
:
8540 case DLT_JUNIPER_VP
:
8541 case DLT_JUNIPER_ST
:
8542 case DLT_JUNIPER_ISM
:
8543 case DLT_JUNIPER_VS
:
8544 case DLT_JUNIPER_SRX_E2E
:
8545 case DLT_JUNIPER_FIBRECHANNEL
:
8546 case DLT_JUNIPER_ATM_CEMIC
:
8547 /* juniper flags (including direction) are stored
8548 * the byte after the 3-byte magic number */
8549 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8554 * If we have packet meta-data indicating a direction,
8555 * and that metadata can be checked by BPF code, check
8556 * it. Otherwise, give up, as this link-layer type has
8557 * nothing in the packet data.
8559 * Currently, the only platform where a BPF filter can
8560 * check that metadata is Linux with the in-kernel
8561 * BPF interpreter. If other packet capture mechanisms
8562 * and BPF filters also supported this, it would be
8563 * nice. It would be even better if they made that
8564 * metadata available so that we could provide it
8565 * with newer capture APIs, allowing it to be saved
8568 #if defined(__linux__)
8569 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8570 /* match outgoing packets */
8571 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8574 /* to filter on inbound traffic, invert the match */
8577 #else /* defined(__linux__) */
8578 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8580 #endif /* defined(__linux__) */
8585 /* PF firewall log matched interface */
8587 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8593 * Catch errors reported by us and routines below us, and return NULL
8596 if (setjmp(cstate
->top_ctx
))
8599 assert_pflog(cstate
, "ifname");
8601 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8602 off
= offsetof(struct pfloghdr
, ifname
);
8603 if (strlen(ifname
) >= len
) {
8604 bpf_error(cstate
, "ifname interface names can only be %d characters",
8608 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8609 (const u_char
*)ifname
);
8613 /* PF firewall log ruleset name */
8615 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8620 * Catch errors reported by us and routines below us, and return NULL
8623 if (setjmp(cstate
->top_ctx
))
8626 assert_pflog(cstate
, "ruleset");
8628 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8629 bpf_error(cstate
, "ruleset names can only be %ld characters",
8630 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8634 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8635 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8639 /* PF firewall log rule number */
8641 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8646 * Catch errors reported by us and routines below us, and return NULL
8649 if (setjmp(cstate
->top_ctx
))
8652 assert_pflog(cstate
, "rnr");
8654 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8659 /* PF firewall log sub-rule number */
8661 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8666 * Catch errors reported by us and routines below us, and return NULL
8669 if (setjmp(cstate
->top_ctx
))
8672 assert_pflog(cstate
, "srnr");
8674 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8679 /* PF firewall log reason code */
8681 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8686 * Catch errors reported by us and routines below us, and return NULL
8689 if (setjmp(cstate
->top_ctx
))
8692 assert_pflog(cstate
, "reason");
8694 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8695 (bpf_u_int32
)reason
);
8699 /* PF firewall log action */
8701 gen_pf_action(compiler_state_t
*cstate
, int action
)
8706 * Catch errors reported by us and routines below us, and return NULL
8709 if (setjmp(cstate
->top_ctx
))
8712 assert_pflog(cstate
, "action");
8714 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8715 (bpf_u_int32
)action
);
8719 /* IEEE 802.11 wireless header */
8721 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8726 * Catch errors reported by us and routines below us, and return NULL
8729 if (setjmp(cstate
->top_ctx
))
8732 switch (cstate
->linktype
) {
8734 case DLT_IEEE802_11
:
8735 case DLT_PRISM_HEADER
:
8736 case DLT_IEEE802_11_RADIO_AVS
:
8737 case DLT_IEEE802_11_RADIO
:
8739 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8743 fail_kw_on_dlt(cstate
, "type/subtype");
8751 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8756 * Catch errors reported by us and routines below us, and return NULL
8759 if (setjmp(cstate
->top_ctx
))
8762 switch (cstate
->linktype
) {
8764 case DLT_IEEE802_11
:
8765 case DLT_PRISM_HEADER
:
8766 case DLT_IEEE802_11_RADIO_AVS
:
8767 case DLT_IEEE802_11_RADIO
:
8772 fail_kw_on_dlt(cstate
, "dir");
8776 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8777 IEEE80211_FC1_DIR_MASK
);
8782 // Process an ARCnet host address string.
8784 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8787 * Catch errors reported by us and routines below us, and return NULL
8790 if (setjmp(cstate
->top_ctx
))
8793 switch (cstate
->linktype
) {
8796 case DLT_ARCNET_LINUX
:
8797 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8798 q
.proto
== Q_LINK
) {
8801 * The lexer currently defines the address format in a
8802 * way that makes this error condition never true.
8803 * Let's check it anyway in case this part of the lexer
8804 * changes in future.
8806 if (! pcapint_atoan(s
, &addr
))
8807 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8808 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8810 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8814 bpf_error(cstate
, "aid supported only on ARCnet");
8819 // Compare an ARCnet host address with the given value.
8820 static struct block
*
8821 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8823 register struct block
*b0
, *b1
;
8827 * ARCnet is different from Ethernet: the source address comes before
8828 * the destination address, each is one byte long. This holds for all
8829 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8830 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8831 * by Datapoint (document number 61610-01).
8834 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8837 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8840 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8841 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8847 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8848 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8858 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8865 static struct block
*
8866 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8868 struct block
*b0
, *b1
;
8870 /* check for VLAN, including 802.1ad and QinQ */
8871 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8872 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8875 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8881 static struct block
*
8882 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8884 if (vlan_num
> 0x0fff) {
8885 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8888 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8891 static struct block
*
8892 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8895 struct block
*b0
, *b1
;
8897 b0
= gen_vlan_tpid_test(cstate
);
8900 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8906 * Both payload and link header type follow the VLAN tags so that
8907 * both need to be updated.
8909 cstate
->off_linkpl
.constant_part
+= 4;
8910 cstate
->off_linktype
.constant_part
+= 4;
8915 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8916 /* add v to variable part of off */
8918 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8919 bpf_u_int32 v
, struct slist
*s
)
8923 if (!off
->is_variable
)
8924 off
->is_variable
= 1;
8926 off
->reg
= alloc_reg(cstate
);
8928 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8931 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8934 s2
= new_stmt(cstate
, BPF_ST
);
8940 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8941 * and link type offsets first
8944 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8948 /* offset determined at run time, shift variable part */
8950 cstate
->is_vlan_vloffset
= 1;
8951 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8952 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8954 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8955 sappend(s
.next
, b_tpid
->head
->stmts
);
8956 b_tpid
->head
->stmts
= s
.next
;
8960 * patch block b_vid (VLAN id test) to load VID value either from packet
8961 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8964 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8966 struct slist
*s
, *s2
, *sjeq
;
8969 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8970 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8972 /* true -> next instructions, false -> beginning of b_vid */
8973 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8975 sjeq
->s
.jf
= b_vid
->stmts
;
8978 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8979 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8983 /* Jump to the test in b_vid. We need to jump one instruction before
8984 * the end of the b_vid block so that we only skip loading the TCI
8985 * from packet data and not the 'and' instruction extracting VID.
8988 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8990 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8994 /* insert our statements at the beginning of b_vid */
8995 sappend(s
, b_vid
->stmts
);
9000 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9001 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9002 * tag can be either in metadata or in packet data; therefore if the
9003 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9004 * header for VLAN tag. As the decision is done at run time, we need
9005 * update variable part of the offsets
9007 static struct block
*
9008 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
9011 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
9014 /* generate new filter code based on extracting packet
9016 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
9017 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
9019 b0
= new_block(cstate
, JMP(BPF_JEQ
));
9024 * This is tricky. We need to insert the statements updating variable
9025 * parts of offsets before the traditional TPID and VID tests so
9026 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9027 * we do not want this update to affect those checks. That's why we
9028 * generate both test blocks first and insert the statements updating
9029 * variable parts of both offsets after that. This wouldn't work if
9030 * there already were variable length link header when entering this
9031 * function but gen_vlan_bpf_extensions() isn't called in that case.
9033 b_tpid
= gen_vlan_tpid_test(cstate
);
9035 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
9037 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9042 gen_vlan_patch_vid_test(cstate
, b_vid
);
9052 * support IEEE 802.1Q VLAN trunk over ethernet
9055 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9060 * Catch errors reported by us and routines below us, and return NULL
9063 if (setjmp(cstate
->top_ctx
))
9066 /* can't check for VLAN-encapsulated packets inside MPLS */
9067 if (cstate
->label_stack_depth
> 0)
9068 bpf_error(cstate
, "no VLAN match after MPLS");
9071 * Check for a VLAN packet, and then change the offsets to point
9072 * to the type and data fields within the VLAN packet. Just
9073 * increment the offsets, so that we can support a hierarchy, e.g.
9074 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9077 * XXX - this is a bit of a kludge. If we were to split the
9078 * compiler into a parser that parses an expression and
9079 * generates an expression tree, and a code generator that
9080 * takes an expression tree (which could come from our
9081 * parser or from some other parser) and generates BPF code,
9082 * we could perhaps make the offsets parameters of routines
9083 * and, in the handler for an "AND" node, pass to subnodes
9084 * other than the VLAN node the adjusted offsets.
9086 * This would mean that "vlan" would, instead of changing the
9087 * behavior of *all* tests after it, change only the behavior
9088 * of tests ANDed with it. That would change the documented
9089 * semantics of "vlan", which might break some expressions.
9090 * However, it would mean that "(vlan and ip) or ip" would check
9091 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9092 * checking only for VLAN-encapsulated IP, so that could still
9093 * be considered worth doing; it wouldn't break expressions
9094 * that are of the form "vlan and ..." or "vlan N and ...",
9095 * which I suspect are the most common expressions involving
9096 * "vlan". "vlan or ..." doesn't necessarily do what the user
9097 * would really want, now, as all the "or ..." tests would
9098 * be done assuming a VLAN, even though the "or" could be viewed
9099 * as meaning "or, if this isn't a VLAN packet...".
9101 switch (cstate
->linktype
) {
9105 * Newer version of the Linux kernel pass around
9106 * packets in which the VLAN tag has been removed
9107 * from the packet data and put into metadata.
9109 * This requires special treatment.
9111 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9112 /* Verify that this is the outer part of the packet and
9113 * not encapsulated somehow. */
9114 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9115 cstate
->off_linkhdr
.constant_part
==
9116 cstate
->off_outermostlinkhdr
.constant_part
) {
9118 * Do we need special VLAN handling?
9120 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9121 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9124 b0
= gen_vlan_no_bpf_extensions(cstate
,
9125 vlan_num
, has_vlan_tag
);
9128 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9132 case DLT_NETANALYZER
:
9133 case DLT_NETANALYZER_TRANSPARENT
:
9134 case DLT_IEEE802_11
:
9135 case DLT_PRISM_HEADER
:
9136 case DLT_IEEE802_11_RADIO_AVS
:
9137 case DLT_IEEE802_11_RADIO
:
9139 * These are either Ethernet packets with an additional
9140 * metadata header (the NetAnalyzer types), or 802.11
9141 * packets, possibly with an additional metadata header.
9143 * For the first of those, the VLAN tag is in the normal
9144 * place, so the special-case handling above isn't
9147 * For the second of those, we don't do the special-case
9150 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9154 bpf_error(cstate
, "no VLAN support for %s",
9155 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9159 cstate
->vlan_stack_depth
++;
9167 * The label_num_arg dance is to avoid annoying whining by compilers that
9168 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9169 * It's not *used* after setjmp returns.
9171 static struct block
*
9172 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9175 struct block
*b0
, *b1
;
9177 if (cstate
->label_stack_depth
> 0) {
9178 /* just match the bottom-of-stack bit clear */
9179 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9182 * We're not in an MPLS stack yet, so check the link-layer
9183 * type against MPLS.
9185 switch (cstate
->linktype
) {
9187 case DLT_C_HDLC
: /* fall through */
9190 case DLT_NETANALYZER
:
9191 case DLT_NETANALYZER_TRANSPARENT
:
9192 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9196 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9199 /* FIXME add other DLT_s ...
9200 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9201 * leave it for now */
9204 bpf_error(cstate
, "no MPLS support for %s",
9205 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9210 /* If a specific MPLS label is requested, check it */
9211 if (has_label_num
) {
9212 if (label_num
> 0xFFFFF) {
9213 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9214 label_num
, 0xFFFFF);
9216 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9217 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9218 0xfffff000); /* only compare the first 20 bits */
9224 * Change the offsets to point to the type and data fields within
9225 * the MPLS packet. Just increment the offsets, so that we
9226 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9227 * capture packets with an outer label of 100000 and an inner
9230 * Increment the MPLS stack depth as well; this indicates that
9231 * we're checking MPLS-encapsulated headers, to make sure higher
9232 * level code generators don't try to match against IP-related
9233 * protocols such as Q_ARP, Q_RARP etc.
9235 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9237 cstate
->off_nl_nosnap
+= 4;
9238 cstate
->off_nl
+= 4;
9239 cstate
->label_stack_depth
++;
9244 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9247 * Catch errors reported by us and routines below us, and return NULL
9250 if (setjmp(cstate
->top_ctx
))
9253 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9257 * Support PPPOE discovery and session.
9260 gen_pppoed(compiler_state_t
*cstate
)
9263 * Catch errors reported by us and routines below us, and return NULL
9266 if (setjmp(cstate
->top_ctx
))
9269 /* check for PPPoE discovery */
9270 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9274 * RFC 2516 Section 4:
9276 * The Ethernet payload for PPPoE is as follows:
9279 * 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
9280 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9281 * | VER | TYPE | CODE | SESSION_ID |
9282 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9283 * | LENGTH | payload ~
9284 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9287 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9289 struct block
*b0
, *b1
;
9292 * Catch errors reported by us and routines below us, and return NULL
9295 if (setjmp(cstate
->top_ctx
))
9299 * Test against the PPPoE session link-layer type.
9301 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9303 /* If a specific session is requested, check PPPoE session id */
9305 if (sess_num
> UINT16_MAX
) {
9306 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9307 sess_num
, UINT16_MAX
);
9309 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9315 * Change the offsets to point to the type and data fields within
9316 * the PPP packet, and note that this is PPPoE rather than
9319 * XXX - this is a bit of a kludge. See the comments in
9322 * The "network-layer" protocol is PPPoE, which has a 6-byte
9323 * PPPoE header, followed by a PPP packet.
9325 * There is no HDLC encapsulation for the PPP packet (it's
9326 * encapsulated in PPPoES instead), so the link-layer type
9327 * starts at the first byte of the PPP packet. For PPPoE,
9328 * that offset is relative to the beginning of the total
9329 * link-layer payload, including any 802.2 LLC header, so
9330 * it's 6 bytes past cstate->off_nl.
9332 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9333 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9334 cstate
->off_linkpl
.reg
);
9336 cstate
->off_linktype
= cstate
->off_linkhdr
;
9337 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9340 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9345 /* Check that this is Geneve and the VNI is correct if
9346 * specified. Parameterized to handle both IPv4 and IPv6. */
9347 static struct block
*
9348 gen_geneve_check(compiler_state_t
*cstate
,
9349 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9350 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9352 struct block
*b0
, *b1
;
9354 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9356 /* Check that we are operating on version 0. Otherwise, we
9357 * can't decode the rest of the fields. The version is 2 bits
9358 * in the first byte of the Geneve header. */
9359 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9364 if (vni
> 0xffffff) {
9365 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9368 vni
<<= 8; /* VNI is in the upper 3 bytes */
9369 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9377 /* The IPv4 and IPv6 Geneve checks need to do two things:
9378 * - Verify that this actually is Geneve with the right VNI.
9379 * - Place the IP header length (plus variable link prefix if
9380 * needed) into register A to be used later to compute
9381 * the inner packet offsets. */
9382 static struct block
*
9383 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9385 struct block
*b0
, *b1
;
9386 struct slist
*s
, *s1
;
9388 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9390 /* Load the IP header length into A. */
9391 s
= gen_loadx_iphdrlen(cstate
);
9393 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9396 /* Forcibly append these statements to the true condition
9397 * of the protocol check by creating a new block that is
9398 * always true and ANDing them. */
9399 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9408 static struct block
*
9409 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9411 struct block
*b0
, *b1
;
9412 struct slist
*s
, *s1
;
9414 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9416 /* Load the IP header length. We need to account for a
9417 * variable length link prefix if there is one. */
9418 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9420 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9424 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9428 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9432 /* Forcibly append these statements to the true condition
9433 * of the protocol check by creating a new block that is
9434 * always true and ANDing them. */
9435 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9438 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9447 /* We need to store three values based on the Geneve header::
9448 * - The offset of the linktype.
9449 * - The offset of the end of the Geneve header.
9450 * - The offset of the end of the encapsulated MAC header. */
9451 static struct slist
*
9452 gen_geneve_offsets(compiler_state_t
*cstate
)
9454 struct slist
*s
, *s1
, *s_proto
;
9456 /* First we need to calculate the offset of the Geneve header
9457 * itself. This is composed of the IP header previously calculated
9458 * (include any variable link prefix) and stored in A plus the
9459 * fixed sized headers (fixed link prefix, MAC length, and UDP
9461 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9462 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9464 /* Stash this in X since we'll need it later. */
9465 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9468 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9470 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9474 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9475 cstate
->off_linktype
.is_variable
= 1;
9476 cstate
->off_linktype
.constant_part
= 0;
9478 s1
= new_stmt(cstate
, BPF_ST
);
9479 s1
->s
.k
= cstate
->off_linktype
.reg
;
9482 /* Load the Geneve option length and mask and shift to get the
9483 * number of bytes. It is stored in the first byte of the Geneve
9485 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9489 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9493 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9497 /* Add in the rest of the Geneve base header. */
9498 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9502 /* Add the Geneve header length to its offset and store. */
9503 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9507 /* Set the encapsulated type as Ethernet. Even though we may
9508 * not actually have Ethernet inside there are two reasons this
9510 * - The linktype field is always in EtherType format regardless
9511 * of whether it is in Geneve or an inner Ethernet frame.
9512 * - The only link layer that we have specific support for is
9513 * Ethernet. We will confirm that the packet actually is
9514 * Ethernet at runtime before executing these checks. */
9515 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9517 s1
= new_stmt(cstate
, BPF_ST
);
9518 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9521 /* Calculate whether we have an Ethernet header or just raw IP/
9522 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9523 * and linktype by 14 bytes so that the network header can be found
9524 * seamlessly. Otherwise, keep what we've calculated already. */
9526 /* We have a bare jmp so we can't use the optimizer. */
9527 cstate
->no_optimize
= 1;
9529 /* Load the EtherType in the Geneve header, 2 bytes in. */
9530 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9534 /* Load X with the end of the Geneve header. */
9535 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9536 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9539 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9540 * end of this check, we should have the total length in X. In
9541 * the non-Ethernet case, it's already there. */
9542 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9543 s_proto
->s
.k
= ETHERTYPE_TEB
;
9544 sappend(s
, s_proto
);
9546 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9550 /* Since this is Ethernet, use the EtherType of the payload
9551 * directly as the linktype. Overwrite what we already have. */
9552 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9556 s1
= new_stmt(cstate
, BPF_ST
);
9557 s1
->s
.k
= cstate
->off_linktype
.reg
;
9560 /* Advance two bytes further to get the end of the Ethernet
9562 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9566 /* Move the result to X. */
9567 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9570 /* Store the final result of our linkpl calculation. */
9571 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9572 cstate
->off_linkpl
.is_variable
= 1;
9573 cstate
->off_linkpl
.constant_part
= 0;
9575 s1
= new_stmt(cstate
, BPF_STX
);
9576 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9585 /* Check to see if this is a Geneve packet. */
9587 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9589 struct block
*b0
, *b1
;
9593 * Catch errors reported by us and routines below us, and return NULL
9596 if (setjmp(cstate
->top_ctx
))
9599 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9600 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9605 /* Later filters should act on the payload of the Geneve frame,
9606 * update all of the header pointers. Attach this code so that
9607 * it gets executed in the event that the Geneve filter matches. */
9608 s
= gen_geneve_offsets(cstate
);
9610 b1
= gen_true(cstate
);
9611 sappend(s
, b1
->stmts
);
9616 cstate
->is_encap
= 1;
9621 /* Check that this is VXLAN and the VNI is correct if
9622 * specified. Parameterized to handle both IPv4 and IPv6. */
9623 static struct block
*
9624 gen_vxlan_check(compiler_state_t
*cstate
,
9625 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9626 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9628 struct block
*b0
, *b1
;
9630 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9632 /* Check that the VXLAN header has the flag bits set
9634 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9639 if (vni
> 0xffffff) {
9640 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9643 vni
<<= 8; /* VNI is in the upper 3 bytes */
9644 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9652 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9653 * - Verify that this actually is VXLAN with the right VNI.
9654 * - Place the IP header length (plus variable link prefix if
9655 * needed) into register A to be used later to compute
9656 * the inner packet offsets. */
9657 static struct block
*
9658 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9660 struct block
*b0
, *b1
;
9661 struct slist
*s
, *s1
;
9663 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9665 /* Load the IP header length into A. */
9666 s
= gen_loadx_iphdrlen(cstate
);
9668 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9671 /* Forcibly append these statements to the true condition
9672 * of the protocol check by creating a new block that is
9673 * always true and ANDing them. */
9674 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9683 static struct block
*
9684 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9686 struct block
*b0
, *b1
;
9687 struct slist
*s
, *s1
;
9689 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9691 /* Load the IP header length. We need to account for a
9692 * variable length link prefix if there is one. */
9693 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9695 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9699 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9703 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9707 /* Forcibly append these statements to the true condition
9708 * of the protocol check by creating a new block that is
9709 * always true and ANDing them. */
9710 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9713 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9722 /* We need to store three values based on the VXLAN header:
9723 * - The offset of the linktype.
9724 * - The offset of the end of the VXLAN header.
9725 * - The offset of the end of the encapsulated MAC header. */
9726 static struct slist
*
9727 gen_vxlan_offsets(compiler_state_t
*cstate
)
9729 struct slist
*s
, *s1
;
9731 /* Calculate the offset of the VXLAN header itself. This
9732 * includes the IP header computed previously (including any
9733 * variable link prefix) and stored in A plus the fixed size
9734 * headers (fixed link prefix, MAC length, UDP header). */
9735 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9736 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9738 /* Add the VXLAN header length to its offset and store */
9739 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9743 /* Push the link header. VXLAN packets always contain Ethernet
9745 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9747 s1
= new_stmt(cstate
, BPF_ST
);
9748 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9751 /* As the payload is an Ethernet packet, we can use the
9752 * EtherType of the payload directly as the linktype. */
9753 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9757 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9758 cstate
->off_linktype
.is_variable
= 1;
9759 cstate
->off_linktype
.constant_part
= 0;
9761 s1
= new_stmt(cstate
, BPF_ST
);
9762 s1
->s
.k
= cstate
->off_linktype
.reg
;
9765 /* Two bytes further is the end of the Ethernet header and the
9766 * start of the payload. */
9767 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9771 /* Move the result to X. */
9772 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9775 /* Store the final result of our linkpl calculation. */
9776 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9777 cstate
->off_linkpl
.is_variable
= 1;
9778 cstate
->off_linkpl
.constant_part
= 0;
9780 s1
= new_stmt(cstate
, BPF_STX
);
9781 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9789 /* Check to see if this is a VXLAN packet. */
9791 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9793 struct block
*b0
, *b1
;
9797 * Catch errors reported by us and routines below us, and return NULL
9800 if (setjmp(cstate
->top_ctx
))
9803 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9804 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9809 /* Later filters should act on the payload of the VXLAN frame,
9810 * update all of the header pointers. Attach this code so that
9811 * it gets executed in the event that the VXLAN filter matches. */
9812 s
= gen_vxlan_offsets(cstate
);
9814 b1
= gen_true(cstate
);
9815 sappend(s
, b1
->stmts
);
9820 cstate
->is_encap
= 1;
9825 /* Check that the encapsulated frame has a link layer header
9826 * for Ethernet filters. */
9827 static struct block
*
9828 gen_encap_ll_check(compiler_state_t
*cstate
)
9831 struct slist
*s
, *s1
;
9833 /* The easiest way to see if there is a link layer present
9834 * is to check if the link layer header and payload are not
9837 /* Geneve always generates pure variable offsets so we can
9838 * compare only the registers. */
9839 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9840 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9842 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9843 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9846 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9854 static struct block
*
9855 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9856 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9861 * This check is a no-op for A_MSGTYPE so long as the only incoming
9862 * code path is from gen_atmmulti_abbrev(), which makes the same
9863 * check first; also for A_PROTOTYPE so long as the only incoming code
9864 * paths are from gen_atmtype_abbrev(), which makes the same check
9865 * first, or from gen_llc_internal() or gen_linktype(), which restrict
9868 assert_atm(cstate
, atmkw(atmfield
));
9873 if (jvalue
> UINT8_MAX
)
9874 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
9875 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9876 0xffffffffU
, jtype
, reverse
, jvalue
);
9880 if (jvalue
> UINT16_MAX
)
9881 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
9882 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9883 0xffffffffU
, jtype
, reverse
, jvalue
);
9887 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9888 0x0fU
, jtype
, reverse
, jvalue
);
9892 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9893 0xffffffffU
, jtype
, reverse
, jvalue
);
9902 static struct block
*
9903 gen_atmtype_metac(compiler_state_t
*cstate
)
9905 struct block
*b0
, *b1
;
9907 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9908 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9913 static struct block
*
9914 gen_atmtype_sc(compiler_state_t
*cstate
)
9916 struct block
*b0
, *b1
;
9918 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9919 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9924 static struct block
*
9925 gen_atmtype_llc(compiler_state_t
*cstate
)
9929 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9930 cstate
->linktype
= cstate
->prevlinktype
;
9935 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9936 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9939 * Catch errors reported by us and routines below us, and return NULL
9942 if (setjmp(cstate
->top_ctx
))
9945 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9950 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9952 struct block
*b0
, *b1
;
9955 * Catch errors reported by us and routines below us, and return NULL
9958 if (setjmp(cstate
->top_ctx
))
9961 assert_atm(cstate
, atmkw(type
));
9966 /* Get all packets in Meta signalling Circuit */
9967 b1
= gen_atmtype_metac(cstate
);
9971 /* Get all packets in Broadcast Circuit*/
9972 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9973 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9978 /* Get all cells in Segment OAM F4 circuit*/
9979 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9980 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9985 /* Get all cells in End-to-End OAM F4 Circuit*/
9986 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9987 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9992 /* Get all packets in connection Signalling Circuit */
9993 b1
= gen_atmtype_sc(cstate
);
9997 /* Get all packets in ILMI Circuit */
9998 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9999 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
10004 /* Get all LANE packets */
10005 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
10008 * Arrange that all subsequent tests assume LANE
10009 * rather than LLC-encapsulated packets, and set
10010 * the offsets appropriately for LANE-encapsulated
10013 * We assume LANE means Ethernet, not Token Ring.
10015 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
10016 cstate
->off_payload
+ 2, /* Ethernet header */
10018 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
10019 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
10020 cstate
->off_nl
= 0; /* Ethernet II */
10021 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
10031 * Filtering for MTP2 messages based on li value
10032 * FISU, length is null
10033 * LSSU, length is 1 or 2
10034 * MSU, length is 3 or more
10035 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10038 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10040 struct block
*b0
, *b1
;
10043 * Catch errors reported by us and routines below us, and return NULL
10046 if (setjmp(cstate
->top_ctx
))
10049 assert_ss7(cstate
, ss7kw(type
));
10054 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10055 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10056 0x3fU
, BPF_JEQ
, 0, 0U);
10060 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10061 0x3fU
, BPF_JGT
, 1, 2U);
10062 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10063 0x3fU
, BPF_JGT
, 0, 0U);
10068 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10069 0x3fU
, BPF_JGT
, 0, 2U);
10073 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10074 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10075 0xff80U
, BPF_JEQ
, 0, 0U);
10079 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10080 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10081 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10082 0xff80U
, BPF_JGT
, 0, 0U);
10087 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10088 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10098 * These maximum valid values are all-ones, so they double as the bitmasks
10099 * before any bitwise shifting.
10101 #define MTP2_SIO_MAXVAL UINT8_MAX
10102 #define MTP3_PC_MAXVAL 0x3fffU
10103 #define MTP3_SLS_MAXVAL 0xfU
10105 static struct block
*
10106 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10107 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10115 newoff_sio
= cstate
->off_sio
;
10116 newoff_opc
= cstate
->off_opc
;
10117 newoff_dpc
= cstate
->off_dpc
;
10118 newoff_sls
= cstate
->off_sls
;
10120 assert_ss7(cstate
, ss7kw(mtp3field
));
10122 switch (mtp3field
) {
10125 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10127 * SIO is the simplest field: the size is one byte and the offset is a
10128 * multiple of bytes, so the only detail to get right is the value of
10129 * the [right-to-left] field offset.
10132 newoff_sio
+= 3; /* offset for MTP2_HSL */
10136 if(jvalue
> MTP2_SIO_MAXVAL
)
10137 bpf_error(cstate
, "sio value %u too big; max value = %u",
10138 jvalue
, MTP2_SIO_MAXVAL
);
10139 // Here the bitmask means "do not apply a bitmask".
10140 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10141 jtype
, reverse
, jvalue
);
10145 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10147 * SLS, OPC and DPC are more complicated: none of these is sized in a
10148 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10149 * diagrams are meant to be read right-to-left. This means in the
10150 * diagrams within individual fields and concatenations thereof
10151 * bitwise shifts and masks can be noted in the common left-to-right
10152 * manner until each final value is ready to be byte-swapped and
10153 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10154 * similar problem in a similar way.
10156 * Offsets of fields within the packet header always have the
10157 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10158 * DLTs the offset does not include the F (Flag) field at the
10159 * beginning of each message.
10161 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10162 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10163 * be tested entirely using a single BPF_W comparison. In this case
10164 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10165 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10166 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10167 * correlates with the [RTL] packet diagram until the byte-swapping is
10170 * The code below uses this approach for OPC, which spans 3 bytes.
10171 * DPC and SLS use shorter loads, SLS also uses a different offset.
10178 if (jvalue
> MTP3_PC_MAXVAL
)
10179 bpf_error(cstate
, "opc value %u too big; max value = %u",
10180 jvalue
, MTP3_PC_MAXVAL
);
10181 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10182 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10183 SWAPLONG(jvalue
<< 14));
10191 if (jvalue
> MTP3_PC_MAXVAL
)
10192 bpf_error(cstate
, "dpc value %u too big; max value = %u",
10193 jvalue
, MTP3_PC_MAXVAL
);
10194 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10195 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10196 SWAPSHORT(jvalue
));
10204 if (jvalue
> MTP3_SLS_MAXVAL
)
10205 bpf_error(cstate
, "sls value %u too big; max value = %u",
10206 jvalue
, MTP3_SLS_MAXVAL
);
10207 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10208 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10219 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10220 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10223 * Catch errors reported by us and routines below us, and return NULL
10226 if (setjmp(cstate
->top_ctx
))
10229 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10233 static struct block
*
10234 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10239 * Q.2931 signalling protocol messages for handling virtual circuits
10240 * establishment and teardown
10245 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10248 case A_CALLPROCEED
:
10249 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10253 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10257 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10261 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10264 case A_RELEASE_DONE
:
10265 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10275 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10277 struct block
*b0
, *b1
;
10280 * Catch errors reported by us and routines below us, and return NULL
10283 if (setjmp(cstate
->top_ctx
))
10286 assert_atm(cstate
, atmkw(type
));
10292 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10293 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10295 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10301 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10302 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10304 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10310 * Get Q.2931 signalling messages for switched
10311 * virtual connection
10313 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10314 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10316 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10318 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10320 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10322 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10324 b0
= gen_atmtype_sc(cstate
);
10328 case A_METACONNECT
:
10329 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10330 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10332 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10334 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10336 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10338 b0
= gen_atmtype_metac(cstate
);