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]));
980 static PCAP_NORETURN_DEF
void
981 fail_kw_on_dlt(compiler_state_t
*cstate
, const char *keyword
)
983 bpf_error(cstate
, "'%s' not supported on %s", keyword
,
984 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
988 assert_pflog(compiler_state_t
*cstate
, const char *kw
)
990 if (cstate
->linktype
!= DLT_PFLOG
)
991 bpf_error(cstate
, "'%s' supported only on PFLOG linktype", kw
);
995 assert_atm(compiler_state_t
*cstate
, const char *kw
)
998 * Belt and braces: init_linktype() sets either all of these struct
999 * members (for DLT_SUNATM) or none (otherwise).
1001 if (cstate
->linktype
!= DLT_SUNATM
||
1003 cstate
->off_vpi
== OFFSET_NOT_SET
||
1004 cstate
->off_vci
== OFFSET_NOT_SET
||
1005 cstate
->off_proto
== OFFSET_NOT_SET
||
1006 cstate
->off_payload
== OFFSET_NOT_SET
)
1007 bpf_error(cstate
, "'%s' supported only on SUNATM", kw
);
1010 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
1011 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
1014 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
1015 const char *buf
, int optimize
, bpf_u_int32 mask
)
1021 compiler_state_t cstate
;
1022 yyscan_t scanner
= NULL
;
1023 YY_BUFFER_STATE in_buffer
= NULL
;
1028 * If this pcap_t hasn't been activated, it doesn't have a
1029 * link-layer type, so we can't use it.
1031 if (!p
->activated
) {
1032 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1033 "not-yet-activated pcap_t passed to pcap_compile");
1034 return (PCAP_ERROR
);
1039 * Initialize Winsock, asking for the latest version (2.2),
1040 * as we may be calling Winsock routines to translate
1041 * host names to addresses.
1043 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1045 pcapint_fmt_errmsg_for_win32_err(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1046 err
, "Error calling WSAStartup()");
1047 return (PCAP_ERROR
);
1051 #ifdef ENABLE_REMOTE
1053 * If the device on which we're capturing need to be notified
1054 * that a new filter is being compiled, do so.
1056 * This allows them to save a copy of it, in case, for example,
1057 * they're implementing a form of remote packet capture, and
1058 * want the remote machine to filter out the packets in which
1059 * it's sending the packets it's captured.
1061 * XXX - the fact that we happen to be compiling a filter
1062 * doesn't necessarily mean we'll be installing it as the
1063 * filter for this pcap_t; we might be running it from userland
1064 * on captured packets to do packet classification. We really
1065 * need a better way of handling this, but this is all that
1066 * the WinPcap remote capture code did.
1068 if (p
->save_current_filter_op
!= NULL
)
1069 (p
->save_current_filter_op
)(p
, buf
);
1072 initchunks(&cstate
);
1073 cstate
.no_optimize
= 0;
1078 cstate
.ic
.root
= NULL
;
1079 cstate
.ic
.cur_mark
= 0;
1080 cstate
.bpf_pcap
= p
;
1081 cstate
.error_set
= 0;
1084 cstate
.netmask
= mask
;
1086 cstate
.snaplen
= pcap_snapshot(p
);
1087 if (cstate
.snaplen
== 0) {
1088 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1089 "snaplen of 0 rejects all packets");
1094 if (pcap_lex_init(&scanner
) != 0) {
1095 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1096 errno
, "can't initialize scanner");
1100 in_buffer
= pcap__scan_string(buf
? buf
: "", scanner
);
1103 * Associate the compiler state with the lexical analyzer
1106 pcap_set_extra(&cstate
, scanner
);
1108 if (init_linktype(&cstate
, p
) == -1) {
1112 if (pcap_parse(scanner
, &cstate
) != 0) {
1114 if (cstate
.ai
!= NULL
)
1115 freeaddrinfo(cstate
.ai
);
1117 if (cstate
.e
!= NULL
)
1123 if (cstate
.ic
.root
== NULL
) {
1124 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
1127 * Catch errors reported by gen_retblk().
1129 if (cstate
.ic
.root
== NULL
) {
1135 if (optimize
&& !cstate
.no_optimize
) {
1136 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
1141 if (cstate
.ic
.root
== NULL
||
1142 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
1143 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1144 "expression rejects all packets");
1149 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
1150 cstate
.ic
.root
, &len
, p
->errbuf
);
1151 if (program
->bf_insns
== NULL
) {
1156 program
->bf_len
= len
;
1158 rc
= 0; /* We're all okay */
1162 * Clean up everything for the lexical analyzer.
1164 if (in_buffer
!= NULL
)
1165 pcap__delete_buffer(in_buffer
, scanner
);
1166 if (scanner
!= NULL
)
1167 pcap_lex_destroy(scanner
);
1170 * Clean up our own allocated memory.
1172 freechunks(&cstate
);
1182 * entry point for using the compiler with no pcap open
1183 * pass in all the stuff that is needed explicitly instead.
1186 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
1187 struct bpf_program
*program
,
1188 const char *buf
, int optimize
, bpf_u_int32 mask
)
1193 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
1195 return (PCAP_ERROR
);
1196 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
1202 * Clean up a "struct bpf_program" by freeing all the memory allocated
1206 pcap_freecode(struct bpf_program
*program
)
1208 program
->bf_len
= 0;
1209 if (program
->bf_insns
!= NULL
) {
1210 free((char *)program
->bf_insns
);
1211 program
->bf_insns
= NULL
;
1216 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
1217 * which of the jt and jf fields has been resolved and which is a pointer
1218 * back to another unresolved block (or nil). At least one of the fields
1219 * in each block is already resolved.
1222 backpatch(struct block
*list
, struct block
*target
)
1239 * Merge the lists in b0 and b1, using the 'sense' field to indicate
1240 * which of jt and jf is the link.
1243 merge(struct block
*b0
, struct block
*b1
)
1245 register struct block
**p
= &b0
;
1247 /* Find end of list. */
1249 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
1251 /* Concatenate the lists. */
1256 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
1259 * Catch errors reported by us and routines below us, and return -1
1262 if (setjmp(cstate
->top_ctx
))
1266 * Insert before the statements of the first (root) block any
1267 * statements needed to load the lengths of any variable-length
1268 * headers into registers.
1270 * XXX - a fancier strategy would be to insert those before the
1271 * statements of all blocks that use those lengths and that
1272 * have no predecessors that use them, so that we only compute
1273 * the lengths if we need them. There might be even better
1274 * approaches than that.
1276 * However, those strategies would be more complicated, and
1277 * as we don't generate code to compute a length if the
1278 * program has no tests that use the length, and as most
1279 * tests will probably use those lengths, we would just
1280 * postpone computing the lengths so that it's not done
1281 * for tests that fail early, and it's not clear that's
1284 insert_compute_vloffsets(cstate
, p
->head
);
1287 * For DLT_PPI captures, generate a check of the per-packet
1288 * DLT value to make sure it's DLT_IEEE802_11.
1290 * XXX - TurboCap cards use DLT_PPI for Ethernet.
1291 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
1292 * with appropriate Ethernet information and use that rather
1293 * than using something such as DLT_PPI where you don't know
1294 * the link-layer header type until runtime, which, in the
1295 * general case, would force us to generate both Ethernet *and*
1296 * 802.11 code (*and* anything else for which PPI is used)
1297 * and choose between them early in the BPF program?
1299 if (cstate
->linktype
== DLT_PPI
) {
1300 struct block
*ppi_dlt_check
= gen_cmp(cstate
, OR_PACKET
,
1301 4, BPF_W
, SWAPLONG(DLT_IEEE802_11
));
1302 gen_and(ppi_dlt_check
, p
);
1305 backpatch(p
, gen_retblk_internal(cstate
, cstate
->snaplen
));
1306 p
->sense
= !p
->sense
;
1307 backpatch(p
, gen_retblk_internal(cstate
, 0));
1308 cstate
->ic
.root
= p
->head
;
1313 gen_and(struct block
*b0
, struct block
*b1
)
1315 backpatch(b0
, b1
->head
);
1316 b0
->sense
= !b0
->sense
;
1317 b1
->sense
= !b1
->sense
;
1319 b1
->sense
= !b1
->sense
;
1320 b1
->head
= b0
->head
;
1324 gen_or(struct block
*b0
, struct block
*b1
)
1326 b0
->sense
= !b0
->sense
;
1327 backpatch(b0
, b1
->head
);
1328 b0
->sense
= !b0
->sense
;
1330 b1
->head
= b0
->head
;
1334 gen_not(struct block
*b
)
1336 b
->sense
= !b
->sense
;
1339 static struct block
*
1340 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1341 u_int size
, bpf_u_int32 v
)
1343 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1346 static struct block
*
1347 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1348 u_int size
, bpf_u_int32 v
)
1350 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1353 static struct block
*
1354 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1355 u_int size
, bpf_u_int32 v
)
1357 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1360 static struct block
*
1361 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1362 u_int size
, bpf_u_int32 v
)
1364 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1367 static struct block
*
1368 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1369 u_int size
, bpf_u_int32 v
)
1371 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1374 static struct block
*
1375 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1376 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1378 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1381 static struct block
*
1382 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1383 u_int size
, const u_char
*v
)
1385 register struct block
*b
, *tmp
;
1389 register const u_char
*p
= &v
[size
- 4];
1391 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1399 register const u_char
*p
= &v
[size
- 2];
1401 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1409 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1418 * AND the field of size "size" at offset "offset" relative to the header
1419 * specified by "offrel" with "mask", and compare it with the value "v"
1420 * with the test specified by "jtype"; if "reverse" is true, the test
1421 * should test the opposite of "jtype".
1423 static struct block
*
1424 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1425 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1428 struct slist
*s
, *s2
;
1431 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1433 if (mask
!= 0xffffffff) {
1434 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1439 b
= new_block(cstate
, JMP(jtype
));
1448 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1450 cstate
->pcap_fddipad
= p
->fddipad
;
1453 * We start out with only one link-layer header.
1455 cstate
->outermostlinktype
= pcap_datalink(p
);
1456 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1457 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1458 cstate
->off_outermostlinkhdr
.reg
= -1;
1460 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1461 cstate
->off_prevlinkhdr
.constant_part
= 0;
1462 cstate
->off_prevlinkhdr
.is_variable
= 0;
1463 cstate
->off_prevlinkhdr
.reg
= -1;
1465 cstate
->linktype
= cstate
->outermostlinktype
;
1466 cstate
->off_linkhdr
.constant_part
= 0;
1467 cstate
->off_linkhdr
.is_variable
= 0;
1468 cstate
->off_linkhdr
.reg
= -1;
1473 cstate
->off_linkpl
.constant_part
= 0;
1474 cstate
->off_linkpl
.is_variable
= 0;
1475 cstate
->off_linkpl
.reg
= -1;
1477 cstate
->off_linktype
.constant_part
= 0;
1478 cstate
->off_linktype
.is_variable
= 0;
1479 cstate
->off_linktype
.reg
= -1;
1482 * Assume it's not raw ATM with a pseudo-header, for now.
1485 cstate
->off_vpi
= OFFSET_NOT_SET
;
1486 cstate
->off_vci
= OFFSET_NOT_SET
;
1487 cstate
->off_proto
= OFFSET_NOT_SET
;
1488 cstate
->off_payload
= OFFSET_NOT_SET
;
1491 * And not encapsulated with either Geneve or VXLAN.
1493 cstate
->is_encap
= 0;
1496 * No variable length VLAN offset by default
1498 cstate
->is_vlan_vloffset
= 0;
1501 * And assume we're not doing SS7.
1503 cstate
->off_li
= OFFSET_NOT_SET
;
1504 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1505 cstate
->off_sio
= OFFSET_NOT_SET
;
1506 cstate
->off_opc
= OFFSET_NOT_SET
;
1507 cstate
->off_dpc
= OFFSET_NOT_SET
;
1508 cstate
->off_sls
= OFFSET_NOT_SET
;
1510 cstate
->label_stack_depth
= 0;
1511 cstate
->vlan_stack_depth
= 0;
1513 switch (cstate
->linktype
) {
1516 cstate
->off_linktype
.constant_part
= 2;
1517 cstate
->off_linkpl
.constant_part
= 6;
1518 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1519 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1522 case DLT_ARCNET_LINUX
:
1523 cstate
->off_linktype
.constant_part
= 4;
1524 cstate
->off_linkpl
.constant_part
= 8;
1525 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1526 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1530 cstate
->off_linktype
.constant_part
= 12;
1531 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1532 cstate
->off_nl
= 0; /* Ethernet II */
1533 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1538 * SLIP doesn't have a link level type. The 16 byte
1539 * header is hacked into our SLIP driver.
1541 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1542 cstate
->off_linkpl
.constant_part
= 16;
1544 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1547 case DLT_SLIP_BSDOS
:
1548 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1549 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1551 cstate
->off_linkpl
.constant_part
= 24;
1553 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1558 cstate
->off_linktype
.constant_part
= 0;
1559 cstate
->off_linkpl
.constant_part
= 4;
1561 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1565 cstate
->off_linktype
.constant_part
= 0;
1566 cstate
->off_linkpl
.constant_part
= 12;
1568 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1573 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1574 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1575 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1576 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1577 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1579 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1584 * This does not include the Ethernet header, and
1585 * only covers session state.
1587 cstate
->off_linktype
.constant_part
= 6;
1588 cstate
->off_linkpl
.constant_part
= 8;
1590 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1594 cstate
->off_linktype
.constant_part
= 5;
1595 cstate
->off_linkpl
.constant_part
= 24;
1597 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1602 * FDDI doesn't really have a link-level type field.
1603 * We set "off_linktype" to the offset of the LLC header.
1605 * To check for Ethernet types, we assume that SSAP = SNAP
1606 * is being used and pick out the encapsulated Ethernet type.
1607 * XXX - should we generate code to check for SNAP?
1609 cstate
->off_linktype
.constant_part
= 13;
1610 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1611 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1612 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1613 cstate
->off_nl
= 8; /* 802.2+SNAP */
1614 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1619 * Token Ring doesn't really have a link-level type field.
1620 * We set "off_linktype" to the offset of the LLC header.
1622 * To check for Ethernet types, we assume that SSAP = SNAP
1623 * is being used and pick out the encapsulated Ethernet type.
1624 * XXX - should we generate code to check for SNAP?
1626 * XXX - the header is actually variable-length.
1627 * Some various Linux patched versions gave 38
1628 * as "off_linktype" and 40 as "off_nl"; however,
1629 * if a token ring packet has *no* routing
1630 * information, i.e. is not source-routed, the correct
1631 * values are 20 and 22, as they are in the vanilla code.
1633 * A packet is source-routed iff the uppermost bit
1634 * of the first byte of the source address, at an
1635 * offset of 8, has the uppermost bit set. If the
1636 * packet is source-routed, the total number of bytes
1637 * of routing information is 2 plus bits 0x1F00 of
1638 * the 16-bit value at an offset of 14 (shifted right
1639 * 8 - figure out which byte that is).
1641 cstate
->off_linktype
.constant_part
= 14;
1642 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1643 cstate
->off_nl
= 8; /* 802.2+SNAP */
1644 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1647 case DLT_PRISM_HEADER
:
1648 case DLT_IEEE802_11_RADIO_AVS
:
1649 case DLT_IEEE802_11_RADIO
:
1650 cstate
->off_linkhdr
.is_variable
= 1;
1651 /* Fall through, 802.11 doesn't have a variable link
1652 * prefix but is otherwise the same. */
1655 case DLT_IEEE802_11
:
1657 * 802.11 doesn't really have a link-level type field.
1658 * We set "off_linktype.constant_part" to the offset of
1661 * To check for Ethernet types, we assume that SSAP = SNAP
1662 * is being used and pick out the encapsulated Ethernet type.
1663 * XXX - should we generate code to check for SNAP?
1665 * We also handle variable-length radio headers here.
1666 * The Prism header is in theory variable-length, but in
1667 * practice it's always 144 bytes long. However, some
1668 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1669 * sometimes or always supply an AVS header, so we
1670 * have to check whether the radio header is a Prism
1671 * header or an AVS header, so, in practice, it's
1674 cstate
->off_linktype
.constant_part
= 24;
1675 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1676 cstate
->off_linkpl
.is_variable
= 1;
1677 cstate
->off_nl
= 8; /* 802.2+SNAP */
1678 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1683 * At the moment we treat PPI the same way that we treat
1684 * normal Radiotap encoded packets. The difference is in
1685 * the function that generates the code at the beginning
1686 * to compute the header length. Since this code generator
1687 * of PPI supports bare 802.11 encapsulation only (i.e.
1688 * the encapsulated DLT should be DLT_IEEE802_11) we
1689 * generate code to check for this too.
1691 cstate
->off_linktype
.constant_part
= 24;
1692 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1693 cstate
->off_linkpl
.is_variable
= 1;
1694 cstate
->off_linkhdr
.is_variable
= 1;
1695 cstate
->off_nl
= 8; /* 802.2+SNAP */
1696 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1699 case DLT_ATM_RFC1483
:
1700 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1702 * assume routed, non-ISO PDUs
1703 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1705 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1706 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1707 * latter would presumably be treated the way PPPoE
1708 * should be, so you can do "pppoe and udp port 2049"
1709 * or "pppoa and tcp port 80" and have it check for
1710 * PPPo{A,E} and a PPP protocol of IP and....
1712 cstate
->off_linktype
.constant_part
= 0;
1713 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1714 cstate
->off_nl
= 8; /* 802.2+SNAP */
1715 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1720 * Full Frontal ATM; you get AALn PDUs with an ATM
1724 cstate
->off_vpi
= SUNATM_VPI_POS
;
1725 cstate
->off_vci
= SUNATM_VCI_POS
;
1726 cstate
->off_proto
= PROTO_POS
;
1727 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1728 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1729 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1730 cstate
->off_nl
= 8; /* 802.2+SNAP */
1731 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1737 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1738 cstate
->off_linkpl
.constant_part
= 0;
1740 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1743 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1744 cstate
->off_linktype
.constant_part
= 14;
1745 cstate
->off_linkpl
.constant_part
= 16;
1747 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1750 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1751 cstate
->off_linktype
.constant_part
= 0;
1752 cstate
->off_linkpl
.constant_part
= 20;
1754 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1759 * LocalTalk does have a 1-byte type field in the LLAP header,
1760 * but really it just indicates whether there is a "short" or
1761 * "long" DDP packet following.
1763 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1764 cstate
->off_linkpl
.constant_part
= 0;
1766 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1769 case DLT_IP_OVER_FC
:
1771 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1772 * link-level type field. We set "off_linktype" to the
1773 * offset of the LLC header.
1775 * To check for Ethernet types, we assume that SSAP = SNAP
1776 * is being used and pick out the encapsulated Ethernet type.
1777 * XXX - should we generate code to check for SNAP? RFC
1778 * 2625 says SNAP should be used.
1780 cstate
->off_linktype
.constant_part
= 16;
1781 cstate
->off_linkpl
.constant_part
= 16;
1782 cstate
->off_nl
= 8; /* 802.2+SNAP */
1783 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1788 * XXX - we should set this to handle SNAP-encapsulated
1789 * frames (NLPID of 0x80).
1791 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1792 cstate
->off_linkpl
.constant_part
= 0;
1794 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1798 * the only BPF-interesting FRF.16 frames are non-control frames;
1799 * Frame Relay has a variable length link-layer
1800 * so lets start with offset 4 for now and increments later on (FIXME);
1803 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1804 cstate
->off_linkpl
.constant_part
= 0;
1806 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1809 case DLT_APPLE_IP_OVER_IEEE1394
:
1810 cstate
->off_linktype
.constant_part
= 16;
1811 cstate
->off_linkpl
.constant_part
= 18;
1813 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1816 case DLT_SYMANTEC_FIREWALL
:
1817 cstate
->off_linktype
.constant_part
= 6;
1818 cstate
->off_linkpl
.constant_part
= 44;
1819 cstate
->off_nl
= 0; /* Ethernet II */
1820 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1824 cstate
->off_linktype
.constant_part
= 0;
1825 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1826 cstate
->off_linkpl
.is_variable
= 1;
1828 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1831 case DLT_JUNIPER_MFR
:
1832 case DLT_JUNIPER_MLFR
:
1833 case DLT_JUNIPER_MLPPP
:
1834 case DLT_JUNIPER_PPP
:
1835 case DLT_JUNIPER_CHDLC
:
1836 case DLT_JUNIPER_FRELAY
:
1837 cstate
->off_linktype
.constant_part
= 4;
1838 cstate
->off_linkpl
.constant_part
= 4;
1840 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1843 case DLT_JUNIPER_ATM1
:
1844 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1845 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1847 cstate
->off_nl_nosnap
= 10;
1850 case DLT_JUNIPER_ATM2
:
1851 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1852 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1854 cstate
->off_nl_nosnap
= 10;
1857 /* frames captured on a Juniper PPPoE service PIC
1858 * contain raw ethernet frames */
1859 case DLT_JUNIPER_PPPOE
:
1860 case DLT_JUNIPER_ETHER
:
1861 cstate
->off_linkpl
.constant_part
= 14;
1862 cstate
->off_linktype
.constant_part
= 16;
1863 cstate
->off_nl
= 18; /* Ethernet II */
1864 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1867 case DLT_JUNIPER_PPPOE_ATM
:
1868 cstate
->off_linktype
.constant_part
= 4;
1869 cstate
->off_linkpl
.constant_part
= 6;
1871 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1874 case DLT_JUNIPER_GGSN
:
1875 cstate
->off_linktype
.constant_part
= 6;
1876 cstate
->off_linkpl
.constant_part
= 12;
1878 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1881 case DLT_JUNIPER_ES
:
1882 cstate
->off_linktype
.constant_part
= 6;
1883 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1884 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1885 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1888 case DLT_JUNIPER_MONITOR
:
1889 cstate
->off_linktype
.constant_part
= 12;
1890 cstate
->off_linkpl
.constant_part
= 12;
1891 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1892 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1895 case DLT_BACNET_MS_TP
:
1896 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1897 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1898 cstate
->off_nl
= OFFSET_NOT_SET
;
1899 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1902 case DLT_JUNIPER_SERVICES
:
1903 cstate
->off_linktype
.constant_part
= 12;
1904 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1905 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1906 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1909 case DLT_JUNIPER_VP
:
1910 cstate
->off_linktype
.constant_part
= 18;
1911 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1912 cstate
->off_nl
= OFFSET_NOT_SET
;
1913 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1916 case DLT_JUNIPER_ST
:
1917 cstate
->off_linktype
.constant_part
= 18;
1918 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1919 cstate
->off_nl
= OFFSET_NOT_SET
;
1920 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1923 case DLT_JUNIPER_ISM
:
1924 cstate
->off_linktype
.constant_part
= 8;
1925 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1926 cstate
->off_nl
= OFFSET_NOT_SET
;
1927 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1930 case DLT_JUNIPER_VS
:
1931 case DLT_JUNIPER_SRX_E2E
:
1932 case DLT_JUNIPER_FIBRECHANNEL
:
1933 case DLT_JUNIPER_ATM_CEMIC
:
1934 cstate
->off_linktype
.constant_part
= 8;
1935 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1936 cstate
->off_nl
= OFFSET_NOT_SET
;
1937 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1942 cstate
->off_li_hsl
= 4;
1943 cstate
->off_sio
= 3;
1944 cstate
->off_opc
= 4;
1945 cstate
->off_dpc
= 4;
1946 cstate
->off_sls
= 7;
1947 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1948 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1949 cstate
->off_nl
= OFFSET_NOT_SET
;
1950 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1953 case DLT_MTP2_WITH_PHDR
:
1955 cstate
->off_li_hsl
= 8;
1956 cstate
->off_sio
= 7;
1957 cstate
->off_opc
= 8;
1958 cstate
->off_dpc
= 8;
1959 cstate
->off_sls
= 11;
1960 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1961 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1962 cstate
->off_nl
= OFFSET_NOT_SET
;
1963 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1967 cstate
->off_li
= 22;
1968 cstate
->off_li_hsl
= 24;
1969 cstate
->off_sio
= 23;
1970 cstate
->off_opc
= 24;
1971 cstate
->off_dpc
= 24;
1972 cstate
->off_sls
= 27;
1973 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1974 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1975 cstate
->off_nl
= OFFSET_NOT_SET
;
1976 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1980 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1981 cstate
->off_linkpl
.constant_part
= 4;
1983 cstate
->off_nl_nosnap
= 0;
1988 * Currently, only raw "link[N:M]" filtering is supported.
1990 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1991 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1992 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1993 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1997 cstate
->off_linktype
.constant_part
= 1;
1998 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
2000 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2003 case DLT_NETANALYZER
:
2004 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
2005 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2006 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
2007 cstate
->off_nl
= 0; /* Ethernet II */
2008 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2011 case DLT_NETANALYZER_TRANSPARENT
:
2012 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
2013 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
2014 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
2015 cstate
->off_nl
= 0; /* Ethernet II */
2016 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
2021 * For values in the range in which we've assigned new
2022 * DLT_ values, only raw "link[N:M]" filtering is supported.
2024 if (cstate
->linktype
>= DLT_HIGH_MATCHING_MIN
&&
2025 cstate
->linktype
<= DLT_HIGH_MATCHING_MAX
) {
2026 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
2027 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
2028 cstate
->off_nl
= OFFSET_NOT_SET
;
2029 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
2031 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
2032 cstate
->linktype
, DLT_HIGH_MATCHING_MIN
, DLT_HIGH_MATCHING_MAX
);
2038 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
2043 * Load a value relative to the specified absolute offset.
2045 static struct slist
*
2046 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
2047 u_int offset
, u_int size
)
2049 struct slist
*s
, *s2
;
2051 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
2054 * If "s" is non-null, it has code to arrange that the X register
2055 * contains the variable part of the absolute offset, so we
2056 * generate a load relative to that, with an offset of
2057 * abs_offset->constant_part + offset.
2059 * Otherwise, we can do an absolute load with an offset of
2060 * abs_offset->constant_part + offset.
2064 * "s" points to a list of statements that puts the
2065 * variable part of the absolute offset into the X register.
2066 * Do an indirect load, to use the X register as an offset.
2068 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2069 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
2073 * There is no variable part of the absolute offset, so
2074 * just do an absolute load.
2076 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2077 s
->s
.k
= abs_offset
->constant_part
+ offset
;
2083 * Load a value relative to the beginning of the specified header.
2085 static struct slist
*
2086 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
2089 struct slist
*s
, *s2
;
2092 * Squelch warnings from compilers that *don't* assume that
2093 * offrel always has a valid enum value and therefore don't
2094 * assume that we'll always go through one of the case arms.
2096 * If we have a default case, compilers that *do* assume that
2097 * will then complain about the default case code being
2100 * Damned if you do, damned if you don't.
2107 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
2112 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
2115 case OR_PREVLINKHDR
:
2116 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
2120 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
2123 case OR_PREVMPLSHDR
:
2124 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
2128 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
2131 case OR_LINKPL_NOSNAP
:
2132 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
2136 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
2141 * Load the X register with the length of the IPv4 header
2142 * (plus the offset of the link-layer header, if it's
2143 * preceded by a variable-length header such as a radio
2144 * header), in bytes.
2146 s
= gen_loadx_iphdrlen(cstate
);
2149 * Load the item at {offset of the link-layer payload} +
2150 * {offset, relative to the start of the link-layer
2151 * payload, of the IPv4 header} + {length of the IPv4 header} +
2152 * {specified offset}.
2154 * If the offset of the link-layer payload is variable,
2155 * the variable part of that offset is included in the
2156 * value in the X register, and we include the constant
2157 * part in the offset of the load.
2159 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
2160 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
2165 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
2172 * Generate code to load into the X register the sum of the length of
2173 * the IPv4 header and the variable part of the offset of the link-layer
2176 static struct slist
*
2177 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
2179 struct slist
*s
, *s2
;
2181 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
2184 * The offset of the link-layer payload has a variable
2185 * part. "s" points to a list of statements that put
2186 * the variable part of that offset into the X register.
2188 * The 4*([k]&0xf) addressing mode can't be used, as we
2189 * don't have a constant offset, so we have to load the
2190 * value in question into the A register and add to it
2191 * the value from the X register.
2193 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2194 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2196 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2199 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2204 * The A register now contains the length of the IP header.
2205 * We need to add to it the variable part of the offset of
2206 * the link-layer payload, which is still in the X
2207 * register, and move the result into the X register.
2209 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
2210 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
2213 * The offset of the link-layer payload is a constant,
2214 * so no code was generated to load the (nonexistent)
2215 * variable part of that offset.
2217 * This means we can use the 4*([k]&0xf) addressing
2218 * mode. Load the length of the IPv4 header, which
2219 * is at an offset of cstate->off_nl from the beginning of
2220 * the link-layer payload, and thus at an offset of
2221 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
2222 * of the raw packet data, using that addressing mode.
2224 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
2225 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
2231 static struct block
*
2232 gen_uncond(compiler_state_t
*cstate
, int rsense
)
2237 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
2239 b
= new_block(cstate
, JMP(BPF_JEQ
));
2245 static inline struct block
*
2246 gen_true(compiler_state_t
*cstate
)
2248 return gen_uncond(cstate
, 1);
2251 static inline struct block
*
2252 gen_false(compiler_state_t
*cstate
)
2254 return gen_uncond(cstate
, 0);
2258 * Generate code to match a particular packet type.
2260 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2261 * value, if <= ETHERMTU. We use that to determine whether to
2262 * match the type/length field or to check the type/length field for
2263 * a value <= ETHERMTU to see whether it's a type field and then do
2264 * the appropriate test.
2266 static struct block
*
2267 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2269 struct block
*b0
, *b1
;
2275 case LLCSAP_NETBEUI
:
2277 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2278 * so we check the DSAP and SSAP.
2280 * LLCSAP_IP checks for IP-over-802.2, rather
2281 * than IP-over-Ethernet or IP-over-SNAP.
2283 * XXX - should we check both the DSAP and the
2284 * SSAP, like this, or should we check just the
2285 * DSAP, as we do for other types <= ETHERMTU
2286 * (i.e., other SAP values)?
2288 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2289 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2297 * Ethernet_II frames, which are Ethernet
2298 * frames with a frame type of ETHERTYPE_IPX;
2300 * Ethernet_802.3 frames, which are 802.3
2301 * frames (i.e., the type/length field is
2302 * a length field, <= ETHERMTU, rather than
2303 * a type field) with the first two bytes
2304 * after the Ethernet/802.3 header being
2307 * Ethernet_802.2 frames, which are 802.3
2308 * frames with an 802.2 LLC header and
2309 * with the IPX LSAP as the DSAP in the LLC
2312 * Ethernet_SNAP frames, which are 802.3
2313 * frames with an LLC header and a SNAP
2314 * header and with an OUI of 0x000000
2315 * (encapsulated Ethernet) and a protocol
2316 * ID of ETHERTYPE_IPX in the SNAP header.
2318 * XXX - should we generate the same code both
2319 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2323 * This generates code to check both for the
2324 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2326 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2327 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2331 * Now we add code to check for SNAP frames with
2332 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2334 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2338 * Now we generate code to check for 802.3
2339 * frames in general.
2341 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2344 * Now add the check for 802.3 frames before the
2345 * check for Ethernet_802.2 and Ethernet_802.3,
2346 * as those checks should only be done on 802.3
2347 * frames, not on Ethernet frames.
2352 * Now add the check for Ethernet_II frames, and
2353 * do that before checking for the other frame
2356 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2360 case ETHERTYPE_ATALK
:
2361 case ETHERTYPE_AARP
:
2363 * EtherTalk (AppleTalk protocols on Ethernet link
2364 * layer) may use 802.2 encapsulation.
2368 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2369 * we check for an Ethernet type field less or equal than
2370 * 1500, which means it's an 802.3 length field.
2372 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2375 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2376 * SNAP packets with an organization code of
2377 * 0x080007 (Apple, for Appletalk) and a protocol
2378 * type of ETHERTYPE_ATALK (Appletalk).
2380 * 802.2-encapsulated ETHERTYPE_AARP packets are
2381 * SNAP packets with an organization code of
2382 * 0x000000 (encapsulated Ethernet) and a protocol
2383 * type of ETHERTYPE_AARP (Appletalk ARP).
2385 if (ll_proto
== ETHERTYPE_ATALK
)
2386 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2387 else /* ll_proto == ETHERTYPE_AARP */
2388 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2392 * Check for Ethernet encapsulation (Ethertalk
2393 * phase 1?); we just check for the Ethernet
2396 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2402 if (ll_proto
<= ETHERMTU
) {
2404 * This is an LLC SAP value, so the frames
2405 * that match would be 802.2 frames.
2406 * Check that the frame is an 802.2 frame
2407 * (i.e., that the length/type field is
2408 * a length field, <= ETHERMTU) and
2409 * then check the DSAP.
2411 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2412 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2417 * This is an Ethernet type, so compare
2418 * the length/type field with it (if
2419 * the frame is an 802.2 frame, the length
2420 * field will be <= ETHERMTU, and, as
2421 * "ll_proto" is > ETHERMTU, this test
2422 * will fail and the frame won't match,
2423 * which is what we want).
2425 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2430 static struct block
*
2431 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2434 * For DLT_NULL, the link-layer header is a 32-bit word
2435 * containing an AF_ value in *host* byte order, and for
2436 * DLT_ENC, the link-layer header begins with a 32-bit
2437 * word containing an AF_ value in host byte order.
2439 * In addition, if we're reading a saved capture file,
2440 * the host byte order in the capture may not be the
2441 * same as the host byte order on this machine.
2443 * For DLT_LOOP, the link-layer header is a 32-bit
2444 * word containing an AF_ value in *network* byte order.
2446 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2448 * The AF_ value is in host byte order, but the BPF
2449 * interpreter will convert it to network byte order.
2451 * If this is a save file, and it's from a machine
2452 * with the opposite byte order to ours, we byte-swap
2455 * Then we run it through "htonl()", and generate
2456 * code to compare against the result.
2458 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2459 ll_proto
= SWAPLONG(ll_proto
);
2460 ll_proto
= htonl(ll_proto
);
2462 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2466 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2467 * or IPv6 then we have an error.
2469 static struct block
*
2470 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2475 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2478 case ETHERTYPE_IPV6
:
2479 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2486 return gen_false(cstate
);
2490 * Generate code to match a particular packet type.
2492 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2493 * value, if <= ETHERMTU. We use that to determine whether to
2494 * match the type field or to check the type field for the special
2495 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2497 static struct block
*
2498 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2500 struct block
*b0
, *b1
;
2506 case LLCSAP_NETBEUI
:
2508 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2509 * so we check the DSAP and SSAP.
2511 * LLCSAP_IP checks for IP-over-802.2, rather
2512 * than IP-over-Ethernet or IP-over-SNAP.
2514 * XXX - should we check both the DSAP and the
2515 * SSAP, like this, or should we check just the
2516 * DSAP, as we do for other types <= ETHERMTU
2517 * (i.e., other SAP values)?
2519 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2520 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2526 * Ethernet_II frames, which are Ethernet
2527 * frames with a frame type of ETHERTYPE_IPX;
2529 * Ethernet_802.3 frames, which have a frame
2530 * type of LINUX_SLL_P_802_3;
2532 * Ethernet_802.2 frames, which are 802.3
2533 * frames with an 802.2 LLC header (i.e, have
2534 * a frame type of LINUX_SLL_P_802_2) and
2535 * with the IPX LSAP as the DSAP in the LLC
2538 * Ethernet_SNAP frames, which are 802.3
2539 * frames with an LLC header and a SNAP
2540 * header and with an OUI of 0x000000
2541 * (encapsulated Ethernet) and a protocol
2542 * ID of ETHERTYPE_IPX in the SNAP header.
2544 * First, do the checks on LINUX_SLL_P_802_2
2545 * frames; generate the check for either
2546 * Ethernet_802.2 or Ethernet_SNAP frames, and
2547 * then put a check for LINUX_SLL_P_802_2 frames
2550 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2551 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2553 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2557 * Now check for 802.3 frames and OR that with
2558 * the previous test.
2560 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2564 * Now add the check for Ethernet_II frames, and
2565 * do that before checking for the other frame
2568 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2572 case ETHERTYPE_ATALK
:
2573 case ETHERTYPE_AARP
:
2575 * EtherTalk (AppleTalk protocols on Ethernet link
2576 * layer) may use 802.2 encapsulation.
2580 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2581 * we check for the 802.2 protocol type in the
2582 * "Ethernet type" field.
2584 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2587 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2588 * SNAP packets with an organization code of
2589 * 0x080007 (Apple, for Appletalk) and a protocol
2590 * type of ETHERTYPE_ATALK (Appletalk).
2592 * 802.2-encapsulated ETHERTYPE_AARP packets are
2593 * SNAP packets with an organization code of
2594 * 0x000000 (encapsulated Ethernet) and a protocol
2595 * type of ETHERTYPE_AARP (Appletalk ARP).
2597 if (ll_proto
== ETHERTYPE_ATALK
)
2598 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2599 else /* ll_proto == ETHERTYPE_AARP */
2600 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2604 * Check for Ethernet encapsulation (Ethertalk
2605 * phase 1?); we just check for the Ethernet
2608 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2614 if (ll_proto
<= ETHERMTU
) {
2616 * This is an LLC SAP value, so the frames
2617 * that match would be 802.2 frames.
2618 * Check for the 802.2 protocol type
2619 * in the "Ethernet type" field, and
2620 * then check the DSAP.
2622 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2623 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2629 * This is an Ethernet type, so compare
2630 * the length/type field with it (if
2631 * the frame is an 802.2 frame, the length
2632 * field will be <= ETHERMTU, and, as
2633 * "ll_proto" is > ETHERMTU, this test
2634 * will fail and the frame won't match,
2635 * which is what we want).
2637 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2643 * Load a value relative to the beginning of the link-layer header after the
2646 static struct slist
*
2647 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2649 struct slist
*s1
, *s2
;
2652 * Generate code to load the length of the pflog header into
2653 * the register assigned to hold that length, if one has been
2654 * assigned. (If one hasn't been assigned, no code we've
2655 * generated uses that prefix, so we don't need to generate any
2658 if (cstate
->off_linkpl
.reg
!= -1) {
2660 * The length is in the first byte of the header.
2662 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2666 * Round it up to a multiple of 4.
2667 * Add 3, and clear the lower 2 bits.
2669 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2672 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2673 s2
->s
.k
= 0xfffffffc;
2677 * Now allocate a register to hold that value and store
2680 s2
= new_stmt(cstate
, BPF_ST
);
2681 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2685 * Now move it into the X register.
2687 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2695 static struct slist
*
2696 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2698 struct slist
*s1
, *s2
;
2699 struct slist
*sjeq_avs_cookie
;
2700 struct slist
*sjcommon
;
2703 * This code is not compatible with the optimizer, as
2704 * we are generating jmp instructions within a normal
2705 * slist of instructions
2707 cstate
->no_optimize
= 1;
2710 * Generate code to load the length of the radio header into
2711 * the register assigned to hold that length, if one has been
2712 * assigned. (If one hasn't been assigned, no code we've
2713 * generated uses that prefix, so we don't need to generate any
2716 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2717 * or always use the AVS header rather than the Prism header.
2718 * We load a 4-byte big-endian value at the beginning of the
2719 * raw packet data, and see whether, when masked with 0xFFFFF000,
2720 * it's equal to 0x80211000. If so, that indicates that it's
2721 * an AVS header (the masked-out bits are the version number).
2722 * Otherwise, it's a Prism header.
2724 * XXX - the Prism header is also, in theory, variable-length,
2725 * but no known software generates headers that aren't 144
2728 if (cstate
->off_linkhdr
.reg
!= -1) {
2732 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2736 * AND it with 0xFFFFF000.
2738 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2739 s2
->s
.k
= 0xFFFFF000;
2743 * Compare with 0x80211000.
2745 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2746 sjeq_avs_cookie
->s
.k
= 0x80211000;
2747 sappend(s1
, sjeq_avs_cookie
);
2752 * The 4 bytes at an offset of 4 from the beginning of
2753 * the AVS header are the length of the AVS header.
2754 * That field is big-endian.
2756 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2759 sjeq_avs_cookie
->s
.jt
= s2
;
2762 * Now jump to the code to allocate a register
2763 * into which to save the header length and
2764 * store the length there. (The "jump always"
2765 * instruction needs to have the k field set;
2766 * it's added to the PC, so, as we're jumping
2767 * over a single instruction, it should be 1.)
2769 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2771 sappend(s1
, sjcommon
);
2774 * Now for the code that handles the Prism header.
2775 * Just load the length of the Prism header (144)
2776 * into the A register. Have the test for an AVS
2777 * header branch here if we don't have an AVS header.
2779 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2782 sjeq_avs_cookie
->s
.jf
= s2
;
2785 * Now allocate a register to hold that value and store
2786 * it. The code for the AVS header will jump here after
2787 * loading the length of the AVS header.
2789 s2
= new_stmt(cstate
, BPF_ST
);
2790 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2792 sjcommon
->s
.jf
= s2
;
2795 * Now move it into the X register.
2797 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2805 static struct slist
*
2806 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2808 struct slist
*s1
, *s2
;
2811 * Generate code to load the length of the AVS header into
2812 * the register assigned to hold that length, if one has been
2813 * assigned. (If one hasn't been assigned, no code we've
2814 * generated uses that prefix, so we don't need to generate any
2817 if (cstate
->off_linkhdr
.reg
!= -1) {
2819 * The 4 bytes at an offset of 4 from the beginning of
2820 * the AVS header are the length of the AVS header.
2821 * That field is big-endian.
2823 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2827 * Now allocate a register to hold that value and store
2830 s2
= new_stmt(cstate
, BPF_ST
);
2831 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2835 * Now move it into the X register.
2837 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2845 static struct slist
*
2846 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2848 struct slist
*s1
, *s2
;
2851 * Generate code to load the length of the radiotap 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 2 bytes at offsets of 2 and 3 from the beginning
2860 * of the radiotap header are the length of the radiotap
2861 * header; unfortunately, it's little-endian, so we have
2862 * to load it a byte at a time and construct the value.
2866 * Load the high-order byte, at an offset of 3, shift it
2867 * left a byte, and put the result in the X register.
2869 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2871 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2874 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2878 * Load the next byte, at an offset of 2, and OR the
2879 * value from the X register into it.
2881 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2884 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2888 * Now allocate a register to hold that value and store
2891 s2
= new_stmt(cstate
, BPF_ST
);
2892 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2896 * Now move it into the X register.
2898 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2907 * At the moment we treat PPI as normal Radiotap encoded
2908 * packets. The difference is in the function that generates
2909 * the code at the beginning to compute the header length.
2910 * Since this code generator of PPI supports bare 802.11
2911 * encapsulation only (i.e. the encapsulated DLT should be
2912 * DLT_IEEE802_11) we generate code to check for this too;
2913 * that's done in finish_parse().
2915 static struct slist
*
2916 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2918 struct slist
*s1
, *s2
;
2921 * Generate code to load the length of the radiotap header
2922 * into the register assigned to hold that length, if one has
2925 if (cstate
->off_linkhdr
.reg
!= -1) {
2927 * The 2 bytes at offsets of 2 and 3 from the beginning
2928 * of the radiotap header are the length of the radiotap
2929 * header; unfortunately, it's little-endian, so we have
2930 * to load it a byte at a time and construct the value.
2934 * Load the high-order byte, at an offset of 3, shift it
2935 * left a byte, and put the result in the X register.
2937 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2939 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2942 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2946 * Load the next byte, at an offset of 2, and OR the
2947 * value from the X register into it.
2949 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2952 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2956 * Now allocate a register to hold that value and store
2959 s2
= new_stmt(cstate
, BPF_ST
);
2960 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2964 * Now move it into the X register.
2966 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2975 * Load a value relative to the beginning of the link-layer header after the 802.11
2976 * header, i.e. LLC_SNAP.
2977 * The link-layer header doesn't necessarily begin at the beginning
2978 * of the packet data; there might be a variable-length prefix containing
2979 * radio information.
2981 static struct slist
*
2982 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2985 struct slist
*sjset_data_frame_1
;
2986 struct slist
*sjset_data_frame_2
;
2987 struct slist
*sjset_qos
;
2988 struct slist
*sjset_radiotap_flags_present
;
2989 struct slist
*sjset_radiotap_ext_present
;
2990 struct slist
*sjset_radiotap_tsft_present
;
2991 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2992 struct slist
*s_roundup
;
2994 if (cstate
->off_linkpl
.reg
== -1) {
2996 * No register has been assigned to the offset of
2997 * the link-layer payload, which means nobody needs
2998 * it; don't bother computing it - just return
2999 * what we already have.
3005 * This code is not compatible with the optimizer, as
3006 * we are generating jmp instructions within a normal
3007 * slist of instructions
3009 cstate
->no_optimize
= 1;
3012 * If "s" is non-null, it has code to arrange that the X register
3013 * contains the length of the prefix preceding the link-layer
3016 * Otherwise, the length of the prefix preceding the link-layer
3017 * header is "off_outermostlinkhdr.constant_part".
3021 * There is no variable-length header preceding the
3022 * link-layer header.
3024 * Load the length of the fixed-length prefix preceding
3025 * the link-layer header (if any) into the X register,
3026 * and store it in the cstate->off_linkpl.reg register.
3027 * That length is off_outermostlinkhdr.constant_part.
3029 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
3030 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
3034 * The X register contains the offset of the beginning of the
3035 * link-layer header; add 24, which is the minimum length
3036 * of the MAC header for a data frame, to that, and store it
3037 * in cstate->off_linkpl.reg, and then load the Frame Control field,
3038 * which is at the offset in the X register, with an indexed load.
3040 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
3042 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
3045 s2
= new_stmt(cstate
, BPF_ST
);
3046 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3049 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
3054 * Check the Frame Control field to see if this is a data frame;
3055 * a data frame has the 0x08 bit (b3) in that field set and the
3056 * 0x04 bit (b2) clear.
3058 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
3059 sjset_data_frame_1
->s
.k
= 0x08;
3060 sappend(s
, sjset_data_frame_1
);
3063 * If b3 is set, test b2, otherwise go to the first statement of
3064 * the rest of the program.
3066 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
3067 sjset_data_frame_2
->s
.k
= 0x04;
3068 sappend(s
, sjset_data_frame_2
);
3069 sjset_data_frame_1
->s
.jf
= snext
;
3072 * If b2 is not set, this is a data frame; test the QoS bit.
3073 * Otherwise, go to the first statement of the rest of the
3076 sjset_data_frame_2
->s
.jt
= snext
;
3077 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
3078 sjset_qos
->s
.k
= 0x80; /* QoS bit */
3079 sappend(s
, sjset_qos
);
3082 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
3084 * Otherwise, go to the first statement of the rest of the
3087 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3088 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3090 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3093 s2
= new_stmt(cstate
, BPF_ST
);
3094 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3098 * If we have a radiotap header, look at it to see whether
3099 * there's Atheros padding between the MAC-layer header
3102 * Note: all of the fields in the radiotap header are
3103 * little-endian, so we byte-swap all of the values
3104 * we test against, as they will be loaded as big-endian
3107 * XXX - in the general case, we would have to scan through
3108 * *all* the presence bits, if there's more than one word of
3109 * presence bits. That would require a loop, meaning that
3110 * we wouldn't be able to run the filter in the kernel.
3112 * We assume here that the Atheros adapters that insert the
3113 * annoying padding don't have multiple antennae and therefore
3114 * do not generate radiotap headers with multiple presence words.
3116 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
3118 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
3119 * in the first presence flag word?
3121 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
3125 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3126 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
3127 sappend(s
, sjset_radiotap_flags_present
);
3130 * If not, skip all of this.
3132 sjset_radiotap_flags_present
->s
.jf
= snext
;
3135 * Otherwise, is the "extension" bit set in that word?
3137 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3138 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
3139 sappend(s
, sjset_radiotap_ext_present
);
3140 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
3143 * If so, skip all of this.
3145 sjset_radiotap_ext_present
->s
.jt
= snext
;
3148 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
3150 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
3151 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
3152 sappend(s
, sjset_radiotap_tsft_present
);
3153 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
3156 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
3157 * at an offset of 16 from the beginning of the raw packet
3158 * data (8 bytes for the radiotap header and 8 bytes for
3161 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3164 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3167 sjset_radiotap_tsft_present
->s
.jt
= s2
;
3169 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3170 sjset_tsft_datapad
->s
.k
= 0x20;
3171 sappend(s
, sjset_tsft_datapad
);
3174 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
3175 * at an offset of 8 from the beginning of the raw packet
3176 * data (8 bytes for the radiotap header).
3178 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
3181 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
3184 sjset_radiotap_tsft_present
->s
.jf
= s2
;
3186 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
3187 sjset_notsft_datapad
->s
.k
= 0x20;
3188 sappend(s
, sjset_notsft_datapad
);
3191 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
3192 * set, round the length of the 802.11 header to
3193 * a multiple of 4. Do that by adding 3 and then
3194 * dividing by and multiplying by 4, which we do by
3197 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
3198 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
3199 sappend(s
, s_roundup
);
3200 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
3203 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
3204 s2
->s
.k
= (bpf_u_int32
)~3;
3206 s2
= new_stmt(cstate
, BPF_ST
);
3207 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3210 sjset_tsft_datapad
->s
.jt
= s_roundup
;
3211 sjset_tsft_datapad
->s
.jf
= snext
;
3212 sjset_notsft_datapad
->s
.jt
= s_roundup
;
3213 sjset_notsft_datapad
->s
.jf
= snext
;
3215 sjset_qos
->s
.jf
= snext
;
3221 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
3225 /* There is an implicit dependency between the link
3226 * payload and link header since the payload computation
3227 * includes the variable part of the header. Therefore,
3228 * if nobody else has allocated a register for the link
3229 * header and we need it, do it now. */
3230 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
3231 cstate
->off_linkhdr
.reg
== -1)
3232 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
3235 * For link-layer types that have a variable-length header
3236 * preceding the link-layer header, generate code to load
3237 * the offset of the link-layer header into the register
3238 * assigned to that offset, if any.
3240 * XXX - this, and the next switch statement, won't handle
3241 * encapsulation of 802.11 or 802.11+radio information in
3242 * some other protocol stack. That's significantly more
3245 switch (cstate
->outermostlinktype
) {
3247 case DLT_PRISM_HEADER
:
3248 s
= gen_load_prism_llprefixlen(cstate
);
3251 case DLT_IEEE802_11_RADIO_AVS
:
3252 s
= gen_load_avs_llprefixlen(cstate
);
3255 case DLT_IEEE802_11_RADIO
:
3256 s
= gen_load_radiotap_llprefixlen(cstate
);
3260 s
= gen_load_ppi_llprefixlen(cstate
);
3269 * For link-layer types that have a variable-length link-layer
3270 * header, generate code to load the offset of the link-layer
3271 * payload into the register assigned to that offset, if any.
3273 switch (cstate
->outermostlinktype
) {
3275 case DLT_IEEE802_11
:
3276 case DLT_PRISM_HEADER
:
3277 case DLT_IEEE802_11_RADIO_AVS
:
3278 case DLT_IEEE802_11_RADIO
:
3280 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
3284 s
= gen_load_pflog_llprefixlen(cstate
);
3289 * If there is no initialization yet and we need variable
3290 * length offsets for VLAN, initialize them to zero
3292 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
3295 if (cstate
->off_linkpl
.reg
== -1)
3296 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
3297 if (cstate
->off_linktype
.reg
== -1)
3298 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
3300 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
3302 s2
= new_stmt(cstate
, BPF_ST
);
3303 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3305 s2
= new_stmt(cstate
, BPF_ST
);
3306 s2
->s
.k
= cstate
->off_linktype
.reg
;
3311 * If we have any offset-loading code, append all the
3312 * existing statements in the block to those statements,
3313 * and make the resulting list the list of statements
3317 sappend(s
, b
->stmts
);
3323 * Take an absolute offset, and:
3325 * if it has no variable part, return NULL;
3327 * if it has a variable part, generate code to load the register
3328 * containing that variable part into the X register, returning
3329 * a pointer to that code - if no register for that offset has
3330 * been allocated, allocate it first.
3332 * (The code to set that register will be generated later, but will
3333 * be placed earlier in the code sequence.)
3335 static struct slist
*
3336 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3340 if (off
->is_variable
) {
3341 if (off
->reg
== -1) {
3343 * We haven't yet assigned a register for the
3344 * variable part of the offset of the link-layer
3345 * header; allocate one.
3347 off
->reg
= alloc_reg(cstate
);
3351 * Load the register containing the variable part of the
3352 * offset of the link-layer header into the X register.
3354 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3359 * That offset isn't variable, there's no variable part,
3360 * so we don't need to generate any code.
3367 * Map an Ethernet type to the equivalent PPP type.
3370 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3378 case ETHERTYPE_IPV6
:
3379 ll_proto
= PPP_IPV6
;
3383 ll_proto
= PPP_DECNET
;
3386 case ETHERTYPE_ATALK
:
3387 ll_proto
= PPP_APPLE
;
3400 * I'm assuming the "Bridging PDU"s that go
3401 * over PPP are Spanning Tree Protocol
3404 ll_proto
= PPP_BRPDU
;
3415 * Generate any tests that, for encapsulation of a link-layer packet
3416 * inside another protocol stack, need to be done to check for those
3417 * link-layer packets (and that haven't already been done by a check
3418 * for that encapsulation).
3420 static struct block
*
3421 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3425 if (cstate
->is_encap
)
3426 return gen_encap_ll_check(cstate
);
3428 switch (cstate
->prevlinktype
) {
3432 * This is LANE-encapsulated Ethernet; check that the LANE
3433 * packet doesn't begin with an LE Control marker, i.e.
3434 * that it's data, not a control message.
3436 * (We've already generated a test for LANE.)
3438 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3444 * No such tests are necessary.
3452 * The three different values we should check for when checking for an
3453 * IPv6 packet with DLT_NULL.
3455 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3456 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3457 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3460 * Generate code to match a particular packet type by matching the
3461 * link-layer type field or fields in the 802.2 LLC header.
3463 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3464 * value, if <= ETHERMTU.
3466 static struct block
*
3467 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3469 struct block
*b0
, *b1
, *b2
;
3471 /* are we checking MPLS-encapsulated packets? */
3472 if (cstate
->label_stack_depth
> 0)
3473 return gen_mpls_linktype(cstate
, ll_proto
);
3475 switch (cstate
->linktype
) {
3478 case DLT_NETANALYZER
:
3479 case DLT_NETANALYZER_TRANSPARENT
:
3480 /* Geneve has an EtherType regardless of whether there is an
3481 * L2 header. VXLAN always has an EtherType. */
3482 if (!cstate
->is_encap
)
3483 b0
= gen_prevlinkhdr_check(cstate
);
3487 b1
= gen_ether_linktype(cstate
, ll_proto
);
3498 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3502 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3506 case DLT_IEEE802_11
:
3507 case DLT_PRISM_HEADER
:
3508 case DLT_IEEE802_11_RADIO_AVS
:
3509 case DLT_IEEE802_11_RADIO
:
3512 * Check that we have a data frame.
3514 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
3515 IEEE80211_FC0_TYPE_DATA
,
3516 IEEE80211_FC0_TYPE_MASK
);
3519 * Now check for the specified link-layer type.
3521 b1
= gen_llc_linktype(cstate
, ll_proto
);
3528 * XXX - check for LLC frames.
3530 return gen_llc_linktype(cstate
, ll_proto
);
3535 * XXX - check for LLC PDUs, as per IEEE 802.5.
3537 return gen_llc_linktype(cstate
, ll_proto
);
3540 case DLT_ATM_RFC1483
:
3542 case DLT_IP_OVER_FC
:
3543 return gen_llc_linktype(cstate
, ll_proto
);
3548 * Check for an LLC-encapsulated version of this protocol;
3549 * if we were checking for LANE, linktype would no longer
3552 * Check for LLC encapsulation and then check the protocol.
3554 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3555 b1
= gen_llc_linktype(cstate
, ll_proto
);
3561 return gen_linux_sll_linktype(cstate
, ll_proto
);
3565 case DLT_SLIP_BSDOS
:
3568 * These types don't provide any type field; packets
3569 * are always IPv4 or IPv6.
3571 * XXX - for IPv4, check for a version number of 4, and,
3572 * for IPv6, check for a version number of 6?
3577 /* Check for a version number of 4. */
3578 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3580 case ETHERTYPE_IPV6
:
3581 /* Check for a version number of 6. */
3582 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3585 return gen_false(cstate
); /* always false */
3591 * Raw IPv4, so no type field.
3593 if (ll_proto
== ETHERTYPE_IP
)
3594 return gen_true(cstate
); /* always true */
3596 /* Checking for something other than IPv4; always false */
3597 return gen_false(cstate
);
3602 * Raw IPv6, so no type field.
3604 if (ll_proto
== ETHERTYPE_IPV6
)
3605 return gen_true(cstate
); /* always true */
3607 /* Checking for something other than IPv6; always false */
3608 return gen_false(cstate
);
3613 case DLT_PPP_SERIAL
:
3616 * We use Ethernet protocol types inside libpcap;
3617 * map them to the corresponding PPP protocol types.
3619 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3620 ethertype_to_ppptype(ll_proto
));
3625 * We use Ethernet protocol types inside libpcap;
3626 * map them to the corresponding PPP protocol types.
3632 * Also check for Van Jacobson-compressed IP.
3633 * XXX - do this for other forms of PPP?
3635 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3636 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3638 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3643 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3644 ethertype_to_ppptype(ll_proto
));
3654 return (gen_loopback_linktype(cstate
, AF_INET
));
3656 case ETHERTYPE_IPV6
:
3658 * AF_ values may, unfortunately, be platform-
3659 * dependent; AF_INET isn't, because everybody
3660 * used 4.2BSD's value, but AF_INET6 is, because
3661 * 4.2BSD didn't have a value for it (given that
3662 * IPv6 didn't exist back in the early 1980's),
3663 * and they all picked their own values.
3665 * This means that, if we're reading from a
3666 * savefile, we need to check for all the
3669 * If we're doing a live capture, we only need
3670 * to check for this platform's value; however,
3671 * Npcap uses 24, which isn't Windows's AF_INET6
3672 * value. (Given the multiple different values,
3673 * programs that read pcap files shouldn't be
3674 * checking for their platform's AF_INET6 value
3675 * anyway, they should check for all of the
3676 * possible values. and they might as well do
3677 * that even for live captures.)
3679 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3681 * Savefile - check for all three
3682 * possible IPv6 values.
3684 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3685 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3687 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3692 * Live capture, so we only need to
3693 * check for the value used on this
3698 * Npcap doesn't use Windows's AF_INET6,
3699 * as that collides with AF_IPX on
3700 * some BSDs (both have the value 23).
3701 * Instead, it uses 24.
3703 return (gen_loopback_linktype(cstate
, 24));
3706 return (gen_loopback_linktype(cstate
, AF_INET6
));
3707 #else /* AF_INET6 */
3709 * I guess this platform doesn't support
3710 * IPv6, so we just reject all packets.
3712 return gen_false(cstate
);
3713 #endif /* AF_INET6 */
3719 * Not a type on which we support filtering.
3720 * XXX - support those that have AF_ values
3721 * #defined on this platform, at least?
3723 return gen_false(cstate
);
3728 * af field is host byte order in contrast to the rest of
3731 if (ll_proto
== ETHERTYPE_IP
)
3732 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3734 else if (ll_proto
== ETHERTYPE_IPV6
)
3735 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3738 return gen_false(cstate
);
3742 case DLT_ARCNET_LINUX
:
3744 * XXX should we check for first fragment if the protocol
3750 return gen_false(cstate
);
3752 case ETHERTYPE_IPV6
:
3753 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3757 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3759 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3765 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3767 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3772 case ETHERTYPE_REVARP
:
3773 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3776 case ETHERTYPE_ATALK
:
3777 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3784 case ETHERTYPE_ATALK
:
3785 return gen_true(cstate
);
3787 return gen_false(cstate
);
3793 * XXX - assumes a 2-byte Frame Relay header with
3794 * DLCI and flags. What if the address is longer?
3800 * Check for the special NLPID for IP.
3802 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3804 case ETHERTYPE_IPV6
:
3806 * Check for the special NLPID for IPv6.
3808 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3812 * Check for several OSI protocols.
3814 * Frame Relay packets typically have an OSI
3815 * NLPID at the beginning; we check for each
3818 * What we check for is the NLPID and a frame
3819 * control field of UI, i.e. 0x03 followed
3822 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3823 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3824 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3830 return gen_false(cstate
);
3835 break; // not implemented
3837 case DLT_JUNIPER_MFR
:
3838 case DLT_JUNIPER_MLFR
:
3839 case DLT_JUNIPER_MLPPP
:
3840 case DLT_JUNIPER_ATM1
:
3841 case DLT_JUNIPER_ATM2
:
3842 case DLT_JUNIPER_PPPOE
:
3843 case DLT_JUNIPER_PPPOE_ATM
:
3844 case DLT_JUNIPER_GGSN
:
3845 case DLT_JUNIPER_ES
:
3846 case DLT_JUNIPER_MONITOR
:
3847 case DLT_JUNIPER_SERVICES
:
3848 case DLT_JUNIPER_ETHER
:
3849 case DLT_JUNIPER_PPP
:
3850 case DLT_JUNIPER_FRELAY
:
3851 case DLT_JUNIPER_CHDLC
:
3852 case DLT_JUNIPER_VP
:
3853 case DLT_JUNIPER_ST
:
3854 case DLT_JUNIPER_ISM
:
3855 case DLT_JUNIPER_VS
:
3856 case DLT_JUNIPER_SRX_E2E
:
3857 case DLT_JUNIPER_FIBRECHANNEL
:
3858 case DLT_JUNIPER_ATM_CEMIC
:
3860 /* just lets verify the magic number for now -
3861 * on ATM we may have up to 6 different encapsulations on the wire
3862 * and need a lot of heuristics to figure out that the payload
3865 * FIXME encapsulation specific BPF_ filters
3867 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3869 case DLT_BACNET_MS_TP
:
3870 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3873 return gen_ipnet_linktype(cstate
, ll_proto
);
3875 case DLT_LINUX_IRDA
:
3878 case DLT_MTP2_WITH_PHDR
:
3881 case DLT_LINUX_LAPD
:
3882 case DLT_USB_FREEBSD
:
3884 case DLT_USB_LINUX_MMAPPED
:
3886 case DLT_BLUETOOTH_HCI_H4
:
3887 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3889 case DLT_CAN_SOCKETCAN
:
3890 case DLT_IEEE802_15_4
:
3891 case DLT_IEEE802_15_4_LINUX
:
3892 case DLT_IEEE802_15_4_NONASK_PHY
:
3893 case DLT_IEEE802_15_4_NOFCS
:
3894 case DLT_IEEE802_15_4_TAP
:
3895 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3898 case DLT_IPMB_KONTRON
:
3902 /* Using the fixed-size NFLOG header it is possible to tell only
3903 * the address family of the packet, other meaningful data is
3904 * either missing or behind TLVs.
3906 break; // not implemented
3910 * Does this link-layer header type have a field
3911 * indicating the type of the next protocol? If
3912 * so, off_linktype.constant_part will be the offset of that
3913 * field in the packet; if not, it will be OFFSET_NOT_SET.
3915 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3917 * Yes; assume it's an Ethernet type. (If
3918 * it's not, it needs to be handled specially
3921 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3925 bpf_error(cstate
, "link-layer type filtering not implemented for %s",
3926 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3930 * Check for an LLC SNAP packet with a given organization code and
3931 * protocol type; we check the entire contents of the 802.2 LLC and
3932 * snap headers, checking for DSAP and SSAP of SNAP and a control
3933 * field of 0x03 in the LLC header, and for the specified organization
3934 * code and protocol type in the SNAP header.
3936 static struct block
*
3937 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3939 u_char snapblock
[8];
3941 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3942 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3943 snapblock
[2] = 0x03; /* control = UI */
3944 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3945 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3946 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3947 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3948 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3949 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3953 * Generate code to match frames with an LLC header.
3955 static struct block
*
3956 gen_llc_internal(compiler_state_t
*cstate
)
3958 struct block
*b0
, *b1
;
3960 switch (cstate
->linktype
) {
3964 * We check for an Ethernet type field less or equal than
3965 * 1500, which means it's an 802.3 length field.
3967 b0
= gen_cmp_le(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3970 * Now check for the purported DSAP and SSAP not being
3971 * 0xFF, to rule out NetWare-over-802.3.
3973 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3980 * We check for LLC traffic.
3982 b0
= gen_atmtype_llc(cstate
);
3985 case DLT_IEEE802
: /* Token Ring */
3987 * XXX - check for LLC frames.
3989 return gen_true(cstate
);
3993 * XXX - check for LLC frames.
3995 return gen_true(cstate
);
3997 case DLT_ATM_RFC1483
:
3999 * For LLC encapsulation, these are defined to have an
4002 * For VC encapsulation, they don't, but there's no
4003 * way to check for that; the protocol used on the VC
4004 * is negotiated out of band.
4006 return gen_true(cstate
);
4008 case DLT_IEEE802_11
:
4009 case DLT_PRISM_HEADER
:
4010 case DLT_IEEE802_11_RADIO
:
4011 case DLT_IEEE802_11_RADIO_AVS
:
4014 * Check that we have a data frame.
4016 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
4017 IEEE80211_FC0_TYPE_DATA
,
4018 IEEE80211_FC0_TYPE_MASK
);
4021 fail_kw_on_dlt(cstate
, "llc");
4027 gen_llc(compiler_state_t
*cstate
)
4030 * Catch errors reported by us and routines below us, and return NULL
4033 if (setjmp(cstate
->top_ctx
))
4036 return gen_llc_internal(cstate
);
4040 gen_llc_i(compiler_state_t
*cstate
)
4042 struct block
*b0
, *b1
;
4046 * Catch errors reported by us and routines below us, and return NULL
4049 if (setjmp(cstate
->top_ctx
))
4053 * Check whether this is an LLC frame.
4055 b0
= gen_llc_internal(cstate
);
4058 * Load the control byte and test the low-order bit; it must
4059 * be clear for I frames.
4061 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
4062 b1
= new_block(cstate
, JMP(BPF_JSET
));
4071 gen_llc_s(compiler_state_t
*cstate
)
4073 struct block
*b0
, *b1
;
4076 * Catch errors reported by us and routines below us, and return NULL
4079 if (setjmp(cstate
->top_ctx
))
4083 * Check whether this is an LLC frame.
4085 b0
= gen_llc_internal(cstate
);
4088 * Now compare the low-order 2 bit of the control byte against
4089 * the appropriate value for S frames.
4091 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
4097 gen_llc_u(compiler_state_t
*cstate
)
4099 struct block
*b0
, *b1
;
4102 * Catch errors reported by us and routines below us, and return NULL
4105 if (setjmp(cstate
->top_ctx
))
4109 * Check whether this is an LLC frame.
4111 b0
= gen_llc_internal(cstate
);
4114 * Now compare the low-order 2 bit of the control byte against
4115 * the appropriate value for U frames.
4117 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
4123 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4125 struct block
*b0
, *b1
;
4128 * Catch errors reported by us and routines below us, and return NULL
4131 if (setjmp(cstate
->top_ctx
))
4135 * Check whether this is an LLC frame.
4137 b0
= gen_llc_internal(cstate
);
4140 * Now check for an S frame with the appropriate type.
4142 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
4148 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
4150 struct block
*b0
, *b1
;
4153 * Catch errors reported by us and routines below us, and return NULL
4156 if (setjmp(cstate
->top_ctx
))
4160 * Check whether this is an LLC frame.
4162 b0
= gen_llc_internal(cstate
);
4165 * Now check for a U frame with the appropriate type.
4167 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
4173 * Generate code to match a particular packet type, for link-layer types
4174 * using 802.2 LLC headers.
4176 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
4177 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
4179 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
4180 * value, if <= ETHERMTU. We use that to determine whether to
4181 * match the DSAP or both DSAP and LSAP or to check the OUI and
4182 * protocol ID in a SNAP header.
4184 static struct block
*
4185 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4188 * XXX - handle token-ring variable-length header.
4194 case LLCSAP_NETBEUI
:
4196 * XXX - should we check both the DSAP and the
4197 * SSAP, like this, or should we check just the
4198 * DSAP, as we do for other SAP values?
4200 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
4201 ((ll_proto
<< 8) | ll_proto
));
4205 * XXX - are there ever SNAP frames for IPX on
4206 * non-Ethernet 802.x networks?
4208 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
4210 case ETHERTYPE_ATALK
:
4212 * 802.2-encapsulated ETHERTYPE_ATALK packets are
4213 * SNAP packets with an organization code of
4214 * 0x080007 (Apple, for Appletalk) and a protocol
4215 * type of ETHERTYPE_ATALK (Appletalk).
4217 * XXX - check for an organization code of
4218 * encapsulated Ethernet as well?
4220 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
4224 * XXX - we don't have to check for IPX 802.3
4225 * here, but should we check for the IPX Ethertype?
4227 if (ll_proto
<= ETHERMTU
) {
4229 * This is an LLC SAP value, so check
4232 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
4235 * This is an Ethernet type; we assume that it's
4236 * unlikely that it'll appear in the right place
4237 * at random, and therefore check only the
4238 * location that would hold the Ethernet type
4239 * in a SNAP frame with an organization code of
4240 * 0x000000 (encapsulated Ethernet).
4242 * XXX - if we were to check for the SNAP DSAP and
4243 * LSAP, as per XXX, and were also to check for an
4244 * organization code of 0x000000 (encapsulated
4245 * Ethernet), we'd do
4247 * return gen_snap(cstate, 0x000000, ll_proto);
4249 * here; for now, we don't, as per the above.
4250 * I don't know whether it's worth the extra CPU
4251 * time to do the right check or not.
4253 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4258 static struct block
*
4259 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4260 int dir
, u_int src_off
, u_int dst_off
)
4262 struct block
*b0
, *b1
;
4276 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4277 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4283 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4284 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4294 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4301 return gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4305 static struct block
*
4306 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4307 struct in6_addr
*mask
, int dir
, u_int src_off
, u_int dst_off
)
4309 struct block
*b0
, *b1
;
4312 * Code below needs to access four separate 32-bit parts of the 128-bit
4313 * IPv6 address and mask. In some OSes this is as simple as using the
4314 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of
4315 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as
4316 * far as libpcap sees it. Hence copy the data before use to avoid
4317 * potential unaligned memory access and the associated compiler
4318 * warnings (whether genuine or not).
4320 bpf_u_int32 a
[4], m
[4];
4333 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4334 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4340 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, src_off
, dst_off
);
4341 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, src_off
, dst_off
);
4351 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4358 /* this order is important */
4359 memcpy(a
, addr
, sizeof(a
));
4360 memcpy(m
, mask
, sizeof(m
));
4361 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4362 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4364 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4366 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4372 static struct block
*
4373 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4375 register struct block
*b0
, *b1
;
4379 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4382 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4385 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4386 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4392 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4393 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4403 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4411 * Like gen_ehostop, but for DLT_FDDI
4413 static struct block
*
4414 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4416 struct block
*b0
, *b1
;
4420 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4423 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4426 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4427 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4433 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4434 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4444 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4452 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4454 static struct block
*
4455 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4457 register struct block
*b0
, *b1
;
4461 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4464 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4467 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4468 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4474 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4475 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4485 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4493 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4494 * various 802.11 + radio headers.
4496 static struct block
*
4497 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4499 register struct block
*b0
, *b1
, *b2
;
4500 register struct slist
*s
;
4502 #ifdef ENABLE_WLAN_FILTERING_PATCH
4505 * We need to disable the optimizer because the optimizer is buggy
4506 * and wipes out some LD instructions generated by the below
4507 * code to validate the Frame Control bits
4509 cstate
->no_optimize
= 1;
4510 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4517 * For control frames, there is no SA.
4519 * For management frames, SA is at an
4520 * offset of 10 from the beginning of
4523 * For data frames, SA is at an offset
4524 * of 10 from the beginning of the packet
4525 * if From DS is clear, at an offset of
4526 * 16 from the beginning of the packet
4527 * if From DS is set and To DS is clear,
4528 * and an offset of 24 from the beginning
4529 * of the packet if From DS is set and To DS
4534 * Generate the tests to be done for data frames
4537 * First, check for To DS set, i.e. check "link[1] & 0x01".
4539 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4540 b1
= new_block(cstate
, JMP(BPF_JSET
));
4541 b1
->s
.k
= 0x01; /* To DS */
4545 * If To DS is set, the SA is at 24.
4547 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4551 * Now, check for To DS not set, i.e. check
4552 * "!(link[1] & 0x01)".
4554 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4555 b2
= new_block(cstate
, JMP(BPF_JSET
));
4556 b2
->s
.k
= 0x01; /* To DS */
4561 * If To DS is not set, the SA is at 16.
4563 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4567 * Now OR together the last two checks. That gives
4568 * the complete set of checks for data frames with
4574 * Now check for From DS being set, and AND that with
4575 * the ORed-together checks.
4577 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4578 b1
= new_block(cstate
, JMP(BPF_JSET
));
4579 b1
->s
.k
= 0x02; /* From DS */
4584 * Now check for data frames with From DS not set.
4586 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4587 b2
= new_block(cstate
, JMP(BPF_JSET
));
4588 b2
->s
.k
= 0x02; /* From DS */
4593 * If From DS isn't set, the SA is at 10.
4595 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4599 * Now OR together the checks for data frames with
4600 * From DS not set and for data frames with From DS
4601 * set; that gives the checks done for data frames.
4606 * Now check for a data frame.
4607 * I.e, check "link[0] & 0x08".
4609 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4610 b1
= new_block(cstate
, JMP(BPF_JSET
));
4615 * AND that with the checks done for data frames.
4620 * If the high-order bit of the type value is 0, this
4621 * is a management frame.
4622 * I.e, check "!(link[0] & 0x08)".
4624 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4625 b2
= new_block(cstate
, JMP(BPF_JSET
));
4631 * For management frames, the SA is at 10.
4633 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4637 * OR that with the checks done for data frames.
4638 * That gives the checks done for management and
4644 * If the low-order bit of the type value is 1,
4645 * this is either a control frame or a frame
4646 * with a reserved type, and thus not a
4649 * I.e., check "!(link[0] & 0x04)".
4651 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4652 b1
= new_block(cstate
, JMP(BPF_JSET
));
4658 * AND that with the checks for data and management
4668 * For control frames, there is no DA.
4670 * For management frames, DA is at an
4671 * offset of 4 from the beginning of
4674 * For data frames, DA is at an offset
4675 * of 4 from the beginning of the packet
4676 * if To DS is clear and at an offset of
4677 * 16 from the beginning of the packet
4682 * Generate the tests to be done for data frames.
4684 * First, check for To DS set, i.e. "link[1] & 0x01".
4686 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4687 b1
= new_block(cstate
, JMP(BPF_JSET
));
4688 b1
->s
.k
= 0x01; /* To DS */
4692 * If To DS is set, the DA is at 16.
4694 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4698 * Now, check for To DS not set, i.e. check
4699 * "!(link[1] & 0x01)".
4701 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4702 b2
= new_block(cstate
, JMP(BPF_JSET
));
4703 b2
->s
.k
= 0x01; /* To DS */
4708 * If To DS is not set, the DA is at 4.
4710 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4714 * Now OR together the last two checks. That gives
4715 * the complete set of checks for data frames.
4720 * Now check for a data frame.
4721 * I.e, check "link[0] & 0x08".
4723 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4724 b1
= new_block(cstate
, JMP(BPF_JSET
));
4729 * AND that with the checks done for data frames.
4734 * If the high-order bit of the type value is 0, this
4735 * is a management frame.
4736 * I.e, check "!(link[0] & 0x08)".
4738 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4739 b2
= new_block(cstate
, JMP(BPF_JSET
));
4745 * For management frames, the DA is at 4.
4747 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4751 * OR that with the checks done for data frames.
4752 * That gives the checks done for management and
4758 * If the low-order bit of the type value is 1,
4759 * this is either a control frame or a frame
4760 * with a reserved type, and thus not a
4763 * I.e., check "!(link[0] & 0x04)".
4765 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4766 b1
= new_block(cstate
, JMP(BPF_JSET
));
4772 * AND that with the checks for data and management
4779 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4780 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4786 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4787 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4792 * XXX - add BSSID keyword?
4795 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4799 * Not present in CTS or ACK control frames.
4801 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4802 IEEE80211_FC0_TYPE_MASK
);
4804 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4805 IEEE80211_FC0_SUBTYPE_MASK
);
4807 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4808 IEEE80211_FC0_SUBTYPE_MASK
);
4812 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4818 * Not present in control frames.
4820 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4821 IEEE80211_FC0_TYPE_MASK
);
4823 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4829 * Present only if the direction mask has both "From DS"
4830 * and "To DS" set. Neither control frames nor management
4831 * frames should have both of those set, so we don't
4832 * check the frame type.
4834 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4835 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4836 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4842 * Not present in management frames; addr1 in other
4847 * If the high-order bit of the type value is 0, this
4848 * is a management frame.
4849 * I.e, check "(link[0] & 0x08)".
4851 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4852 b1
= new_block(cstate
, JMP(BPF_JSET
));
4859 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4862 * AND that with the check of addr1.
4869 * Not present in management frames; addr2, if present,
4874 * Not present in CTS or ACK control frames.
4876 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4877 IEEE80211_FC0_TYPE_MASK
);
4879 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4880 IEEE80211_FC0_SUBTYPE_MASK
);
4882 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4883 IEEE80211_FC0_SUBTYPE_MASK
);
4889 * If the high-order bit of the type value is 0, this
4890 * is a management frame.
4891 * I.e, check "(link[0] & 0x08)".
4893 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4894 b1
= new_block(cstate
, JMP(BPF_JSET
));
4899 * AND that with the check for frames other than
4900 * CTS and ACK frames.
4907 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4916 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4917 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4918 * as the RFC states.)
4920 static struct block
*
4921 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4923 register struct block
*b0
, *b1
;
4927 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4930 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4933 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4934 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4940 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4941 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4951 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
4959 * This is quite tricky because there may be pad bytes in front of the
4960 * DECNET header, and then there are two possible data packet formats that
4961 * carry both src and dst addresses, plus 5 packet types in a format that
4962 * carries only the src node, plus 2 types that use a different format and
4963 * also carry just the src node.
4967 * Instead of doing those all right, we just look for data packets with
4968 * 0 or 1 bytes of padding. If you want to look at other packets, that
4969 * will require a lot more hacking.
4971 * To add support for filtering on DECNET "areas" (network numbers)
4972 * one would want to add a "mask" argument to this routine. That would
4973 * make the filter even more inefficient, although one could be clever
4974 * and not generate masking instructions if the mask is 0xFFFF.
4976 static struct block
*
4977 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4979 struct block
*b0
, *b1
, *b2
, *tmp
;
4980 u_int offset_lh
; /* offset if long header is received */
4981 u_int offset_sh
; /* offset if short header is received */
4986 offset_sh
= 1; /* follows flags */
4987 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4991 offset_sh
= 3; /* follows flags, dstnode */
4992 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4996 /* Inefficient because we do our Calvinball dance twice */
4997 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4998 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5004 /* Inefficient because we do our Calvinball dance twice */
5005 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
5006 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
5016 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
5024 * In a DECnet message inside an Ethernet frame the first two bytes
5025 * immediately after EtherType are the [litle-endian] DECnet message
5026 * length, which is irrelevant in this context.
5028 * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH
5029 * 8-bit bitmap of the optional padding before the packet route header.
5030 * The bitmap always has bit 7 set to 1 and in this case has bits 0-6
5031 * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter
5032 * means there aren't any PAD bytes after the bitmap, so the header
5033 * begins at the fourth byte. "pad = 0" means bit 7 of the third byte
5034 * is set to 0, thus the header begins at the third byte.
5036 * The header can be in several (as mentioned above) formats, all of
5037 * which begin with the FLAGS 8-bit bitmap, which always has bit 7
5038 * (PF, "pad field") set to 0 regardless of any padding present before
5039 * the header. "Short header" means bits 0-2 of the bitmap encode the
5040 * integer value 2 (SFDP), and "long header" means value 6 (LFDP).
5042 * To test PLENGTH and FLAGS, use multiple-byte constants with the
5043 * values and the masks, this maps to the required single bytes of
5044 * the message correctly on both big-endian and little-endian hosts.
5045 * For the DECnet address use SWAPSHORT(), which always swaps bytes,
5046 * because the wire encoding is little-endian and BPF multiple-byte
5047 * loads are big-endian. When the destination address is near enough
5048 * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two
5051 /* Check for pad = 1, long header case */
5052 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8106U
, 0xFF07U
);
5053 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
5054 BPF_H
, SWAPSHORT(addr
));
5056 /* Check for pad = 0, long header case */
5057 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x06U
, 0x07U
);
5058 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
5062 /* Check for pad = 1, short header case */
5064 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5065 0x81020000U
| SWAPSHORT(addr
),
5068 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
, 0x8102U
, 0xFF07U
);
5069 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
5074 /* Check for pad = 0, short header case */
5076 b2
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_W
,
5077 0x02000000U
| SWAPSHORT(addr
) << 8,
5080 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, 0x02U
, 0x07U
);
5081 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
5091 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
5092 * test the bottom-of-stack bit, and then check the version number
5093 * field in the IP header.
5095 static struct block
*
5096 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
5098 struct block
*b0
, *b1
;
5103 /* match the bottom-of-stack bit */
5104 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5105 /* match the IPv4 version number */
5106 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
5110 case ETHERTYPE_IPV6
:
5111 /* match the bottom-of-stack bit */
5112 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
5113 /* match the IPv4 version number */
5114 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
5119 /* FIXME add other L3 proto IDs */
5120 bpf_error(cstate
, "unsupported protocol over mpls");
5125 static struct block
*
5126 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
5127 int proto
, int dir
, int type
)
5129 struct block
*b0
, *b1
;
5134 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
5136 * Only check for non-IPv4 addresses if we're not
5137 * checking MPLS-encapsulated packets.
5139 if (cstate
->label_stack_depth
== 0) {
5140 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
5142 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
5148 break; // invalid qualifier
5151 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5152 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 12, 16);
5157 b0
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5158 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5163 b0
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5164 b1
= gen_hostop(cstate
, addr
, mask
, dir
, 14, 24);
5175 break; // invalid qualifier
5178 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
5179 b1
= gen_dnhostop(cstate
, addr
, dir
);
5210 break; // invalid qualifier
5215 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5216 type
== Q_NET
? "ip net" : "ip host");
5221 static struct block
*
5222 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5223 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5225 struct block
*b0
, *b1
;
5231 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5232 b1
= gen_hostop6(cstate
, addr
, mask
, dir
, 8, 24);
5274 break; // invalid qualifier
5279 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
),
5280 type
== Q_NET
? "ip6 net" : "ip6 host");
5286 static struct block
*
5287 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5288 struct addrinfo
*alist
, int proto
, int dir
)
5290 struct block
*b0
, *b1
, *tmp
;
5291 struct addrinfo
*ai
;
5292 struct sockaddr_in
*sin
;
5295 bpf_error(cstate
, "direction applied to 'gateway'");
5302 switch (cstate
->linktype
) {
5304 case DLT_NETANALYZER
:
5305 case DLT_NETANALYZER_TRANSPARENT
:
5306 b1
= gen_prevlinkhdr_check(cstate
);
5307 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5312 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5315 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5317 case DLT_IEEE802_11
:
5318 case DLT_PRISM_HEADER
:
5319 case DLT_IEEE802_11_RADIO_AVS
:
5320 case DLT_IEEE802_11_RADIO
:
5322 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5324 case DLT_IP_OVER_FC
:
5325 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5329 * This is LLC-multiplexed traffic; if it were
5330 * LANE, cstate->linktype would have been set to
5336 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5339 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5341 * Does it have an address?
5343 if (ai
->ai_addr
!= NULL
) {
5345 * Yes. Is it an IPv4 address?
5347 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5349 * Generate an entry for it.
5351 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5352 tmp
= gen_host(cstate
,
5353 ntohl(sin
->sin_addr
.s_addr
),
5354 0xffffffff, proto
, Q_OR
, Q_HOST
);
5356 * Is it the *first* IPv4 address?
5360 * Yes, so start with it.
5365 * No, so OR it into the
5377 * No IPv4 addresses found.
5385 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "gateway");
5390 static struct block
*
5391 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5394 struct block
*b1
= NULL
;
5399 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5403 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5407 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5411 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5414 #ifndef IPPROTO_IGMP
5415 #define IPPROTO_IGMP 2
5419 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5422 #ifndef IPPROTO_IGRP
5423 #define IPPROTO_IGRP 9
5426 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5430 #define IPPROTO_PIM 103
5434 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5437 #ifndef IPPROTO_VRRP
5438 #define IPPROTO_VRRP 112
5442 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5445 #ifndef IPPROTO_CARP
5446 #define IPPROTO_CARP 112
5450 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5454 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5458 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5462 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5466 break; // invalid syntax
5469 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5473 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5477 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5481 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5485 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5489 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5493 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5497 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5500 #ifndef IPPROTO_ICMPV6
5501 #define IPPROTO_ICMPV6 58
5504 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5508 #define IPPROTO_AH 51
5511 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5515 #define IPPROTO_ESP 50
5518 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5522 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5526 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5530 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5533 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5534 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5535 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5537 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5539 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5541 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5545 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5546 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5547 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5549 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5551 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5553 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5557 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5558 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5559 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5561 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5566 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5567 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5572 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5573 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5575 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5577 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5582 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5583 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5588 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5589 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5594 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5598 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5602 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5606 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5610 break; // invalid syntax
5617 bpf_error(cstate
, "'%s' cannot be used as an abbreviation", pqkw(proto
));
5621 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5624 * Catch errors reported by us and routines below us, and return NULL
5627 if (setjmp(cstate
->top_ctx
))
5630 return gen_proto_abbrev_internal(cstate
, proto
);
5633 static struct block
*
5634 gen_ipfrag(compiler_state_t
*cstate
)
5639 /* not IPv4 frag other than the first frag */
5640 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5641 b
= new_block(cstate
, JMP(BPF_JSET
));
5650 * Generate a comparison to a port value in the transport-layer header
5651 * at the specified offset from the beginning of that header.
5653 * XXX - this handles a variable-length prefix preceding the link-layer
5654 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5655 * variable-length link-layer headers (such as Token Ring or 802.11
5658 static struct block
*
5659 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5661 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5664 static struct block
*
5665 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5667 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5670 static struct block
*
5671 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5673 struct block
*b0
, *b1
, *tmp
;
5675 /* ip proto 'proto' and not a fragment other than the first fragment */
5676 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5677 b0
= gen_ipfrag(cstate
);
5682 b1
= gen_portatom(cstate
, 0, port
);
5686 b1
= gen_portatom(cstate
, 2, port
);
5690 tmp
= gen_portatom(cstate
, 0, port
);
5691 b1
= gen_portatom(cstate
, 2, port
);
5697 tmp
= gen_portatom(cstate
, 0, port
);
5698 b1
= gen_portatom(cstate
, 2, port
);
5708 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "port");
5720 static struct block
*
5721 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5723 struct block
*b0
, *b1
, *tmp
;
5728 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5729 * not LLC encapsulation with LLCSAP_IP.
5731 * For IEEE 802 networks - which includes 802.5 token ring
5732 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5733 * says that SNAP encapsulation is used, not LLC encapsulation
5736 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5737 * RFC 2225 say that SNAP encapsulation is used, not LLC
5738 * encapsulation with LLCSAP_IP.
5740 * So we always check for ETHERTYPE_IP.
5742 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5748 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5752 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5753 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5755 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5767 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5769 struct block
*b0
, *b1
, *tmp
;
5771 /* ip6 proto 'proto' */
5772 /* XXX - catch the first fragment of a fragmented packet? */
5773 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5777 b1
= gen_portatom6(cstate
, 0, port
);
5781 b1
= gen_portatom6(cstate
, 2, port
);
5785 tmp
= gen_portatom6(cstate
, 0, port
);
5786 b1
= gen_portatom6(cstate
, 2, port
);
5792 tmp
= gen_portatom6(cstate
, 0, port
);
5793 b1
= gen_portatom6(cstate
, 2, port
);
5805 static struct block
*
5806 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5808 struct block
*b0
, *b1
, *tmp
;
5810 /* link proto ip6 */
5811 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5817 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5821 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5822 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5824 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5835 /* gen_portrange code */
5836 static struct block
*
5837 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5840 struct block
*b1
, *b2
;
5844 * Reverse the order of the ports, so v1 is the lower one.
5853 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5854 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5861 static struct block
*
5862 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5863 bpf_u_int32 proto
, int dir
)
5865 struct block
*b0
, *b1
, *tmp
;
5867 /* ip proto 'proto' and not a fragment other than the first fragment */
5868 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5869 b0
= gen_ipfrag(cstate
);
5874 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5878 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5882 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5883 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5889 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5890 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5900 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, dqkw(dir
), "portrange");
5912 static struct block
*
5913 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5916 struct block
*b0
, *b1
, *tmp
;
5919 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5925 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5930 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5931 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5933 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5944 static struct block
*
5945 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5948 struct block
*b1
, *b2
;
5952 * Reverse the order of the ports, so v1 is the lower one.
5961 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5962 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5969 static struct block
*
5970 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5971 bpf_u_int32 proto
, int dir
)
5973 struct block
*b0
, *b1
, *tmp
;
5975 /* ip6 proto 'proto' */
5976 /* XXX - catch the first fragment of a fragmented packet? */
5977 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5981 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5985 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5989 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5990 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5996 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5997 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
6009 static struct block
*
6010 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6013 struct block
*b0
, *b1
, *tmp
;
6015 /* link proto ip6 */
6016 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6022 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6027 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6028 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6030 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6042 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6051 v
= pcap_nametoproto(name
);
6052 if (v
== PROTO_UNDEF
)
6053 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6057 /* XXX should look up h/w protocol type based on cstate->linktype */
6058 v
= pcap_nametoeproto(name
);
6059 if (v
== PROTO_UNDEF
) {
6060 v
= pcap_nametollc(name
);
6061 if (v
== PROTO_UNDEF
)
6062 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6067 if (strcmp(name
, "esis") == 0)
6069 else if (strcmp(name
, "isis") == 0)
6071 else if (strcmp(name
, "clnp") == 0)
6074 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6084 #if !defined(NO_PROTOCHAIN)
6085 static struct block
*
6086 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6088 struct block
*b0
, *b
;
6089 struct slist
*s
[100];
6090 int fix2
, fix3
, fix4
, fix5
;
6091 int ahcheck
, again
, end
;
6093 int reg2
= alloc_reg(cstate
);
6095 memset(s
, 0, sizeof(s
));
6096 fix3
= fix4
= fix5
= 0;
6103 b0
= gen_protochain(cstate
, v
, Q_IP
);
6104 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6108 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "protochain");
6113 * We don't handle variable-length prefixes before the link-layer
6114 * header, or variable-length link-layer headers, here yet.
6115 * We might want to add BPF instructions to do the protochain
6116 * work, to simplify that and, on platforms that have a BPF
6117 * interpreter with the new instructions, let the filtering
6118 * be done in the kernel. (We already require a modified BPF
6119 * engine to do the protochain stuff, to support backward
6120 * branches, and backward branch support is unlikely to appear
6121 * in kernel BPF engines.)
6123 if (cstate
->off_linkpl
.is_variable
)
6124 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6127 * To quote a comment in optimize.c:
6129 * "These data structures are used in a Cocke and Schwartz style
6130 * value numbering scheme. Since the flowgraph is acyclic,
6131 * exit values can be propagated from a node's predecessors
6132 * provided it is uniquely defined."
6134 * "Acyclic" means "no backward branches", which means "no
6135 * loops", so we have to turn the optimizer off.
6137 cstate
->no_optimize
= 1;
6140 * s[0] is a dummy entry to protect other BPF insn from damage
6141 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6142 * hard to find interdependency made by jump table fixup.
6145 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6150 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6153 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6154 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6156 /* X = ip->ip_hl << 2 */
6157 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6158 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6163 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6165 /* A = ip6->ip_nxt */
6166 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6167 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6169 /* X = sizeof(struct ip6_hdr) */
6170 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6176 bpf_error(cstate
, "unsupported proto to gen_protochain");
6180 /* again: if (A == v) goto end; else fall through; */
6182 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6184 s
[i
]->s
.jt
= NULL
; /*later*/
6185 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6189 #ifndef IPPROTO_NONE
6190 #define IPPROTO_NONE 59
6192 /* if (A == IPPROTO_NONE) goto end */
6193 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6194 s
[i
]->s
.jt
= NULL
; /*later*/
6195 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6196 s
[i
]->s
.k
= IPPROTO_NONE
;
6197 s
[fix5
]->s
.jf
= s
[i
];
6201 if (proto
== Q_IPV6
) {
6202 int v6start
, v6end
, v6advance
, j
;
6205 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6206 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6207 s
[i
]->s
.jt
= NULL
; /*later*/
6208 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6209 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6210 s
[fix2
]->s
.jf
= s
[i
];
6212 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6213 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6214 s
[i
]->s
.jt
= NULL
; /*later*/
6215 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6216 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6218 /* if (A == IPPROTO_ROUTING) goto v6advance */
6219 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6220 s
[i
]->s
.jt
= NULL
; /*later*/
6221 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6222 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6224 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6225 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6226 s
[i
]->s
.jt
= NULL
; /*later*/
6227 s
[i
]->s
.jf
= NULL
; /*later*/
6228 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6238 * A = P[X + packet head];
6239 * X = X + (P[X + packet head + 1] + 1) * 8;
6241 /* A = P[X + packet head] */
6242 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6243 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6246 s
[i
] = new_stmt(cstate
, BPF_ST
);
6249 /* A = P[X + packet head + 1]; */
6250 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6251 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6254 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6258 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6262 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6266 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6269 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6273 /* goto again; (must use BPF_JA for backward jump) */
6274 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6275 s
[i
]->s
.k
= again
- i
- 1;
6276 s
[i
- 1]->s
.jf
= s
[i
];
6280 for (j
= v6start
; j
<= v6end
; j
++)
6281 s
[j
]->s
.jt
= s
[v6advance
];
6284 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6286 s
[fix2
]->s
.jf
= s
[i
];
6292 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6293 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6294 s
[i
]->s
.jt
= NULL
; /*later*/
6295 s
[i
]->s
.jf
= NULL
; /*later*/
6296 s
[i
]->s
.k
= IPPROTO_AH
;
6298 s
[fix3
]->s
.jf
= s
[ahcheck
];
6305 * X = X + (P[X + 1] + 2) * 4;
6308 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6310 /* A = P[X + packet head]; */
6311 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6312 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6315 s
[i
] = new_stmt(cstate
, BPF_ST
);
6319 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6322 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6326 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6328 /* A = P[X + packet head] */
6329 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6330 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6333 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6337 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6341 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6344 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6348 /* goto again; (must use BPF_JA for backward jump) */
6349 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6350 s
[i
]->s
.k
= again
- i
- 1;
6355 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6357 s
[fix2
]->s
.jt
= s
[end
];
6358 s
[fix4
]->s
.jf
= s
[end
];
6359 s
[fix5
]->s
.jt
= s
[end
];
6366 for (i
= 0; i
< max
- 1; i
++)
6367 s
[i
]->next
= s
[i
+ 1];
6368 s
[max
- 1]->next
= NULL
;
6373 b
= new_block(cstate
, JMP(BPF_JEQ
));
6374 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6377 free_reg(cstate
, reg2
);
6382 #endif /* !defined(NO_PROTOCHAIN) */
6385 * Generate code that checks whether the packet is a packet for protocol
6386 * <proto> and whether the type field in that protocol's header has
6387 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6388 * IP packet and checks the protocol number in the IP header against <v>.
6390 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6391 * against Q_IP and Q_IPV6.
6393 static struct block
*
6394 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6396 struct block
*b0
, *b1
;
6399 if (dir
!= Q_DEFAULT
)
6400 bpf_error(cstate
, "direction applied to 'proto'");
6404 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6405 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6410 return gen_linktype(cstate
, v
);
6414 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6415 * not LLC encapsulation with LLCSAP_IP.
6417 * For IEEE 802 networks - which includes 802.5 token ring
6418 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6419 * says that SNAP encapsulation is used, not LLC encapsulation
6422 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6423 * RFC 2225 say that SNAP encapsulation is used, not LLC
6424 * encapsulation with LLCSAP_IP.
6426 * So we always check for ETHERTYPE_IP.
6428 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6429 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6447 break; // invalid qualifier
6450 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6452 * Also check for a fragment header before the final
6455 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6456 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6458 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6469 break; // invalid qualifier
6472 switch (cstate
->linktype
) {
6476 * Frame Relay packets typically have an OSI
6477 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6478 * generates code to check for all the OSI
6479 * NLPIDs, so calling it and then adding a check
6480 * for the particular NLPID for which we're
6481 * looking is bogus, as we can just check for
6484 * What we check for is the NLPID and a frame
6485 * control field value of UI, i.e. 0x03 followed
6488 * XXX - assumes a 2-byte Frame Relay header with
6489 * DLCI and flags. What if the address is longer?
6491 * XXX - what about SNAP-encapsulated frames?
6493 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6499 * Cisco uses an Ethertype lookalike - for OSI,
6502 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6503 /* OSI in C-HDLC is stuffed with a fudge byte */
6504 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6509 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6510 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6516 break; // invalid qualifier
6519 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6521 * 4 is the offset of the PDU type relative to the IS-IS
6524 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6541 break; // invalid qualifier
6547 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "proto");
6552 * Convert a non-numeric name to a port number.
6555 nametoport(compiler_state_t
*cstate
, const char *name
, int ipproto
)
6557 struct addrinfo hints
, *res
, *ai
;
6559 struct sockaddr_in
*in4
;
6561 struct sockaddr_in6
*in6
;
6566 * We check for both TCP and UDP in case there are
6567 * ambiguous entries.
6569 memset(&hints
, 0, sizeof(hints
));
6570 hints
.ai_family
= PF_UNSPEC
;
6571 hints
.ai_socktype
= (ipproto
== IPPROTO_TCP
) ? SOCK_STREAM
: SOCK_DGRAM
;
6572 hints
.ai_protocol
= ipproto
;
6573 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
6580 * No such port. Just return -1.
6587 * We don't use strerror() because it's not
6588 * guaranteed to be thread-safe on all platforms
6589 * (probably because it might use a non-thread-local
6590 * buffer into which to format an error message
6591 * if the error code isn't one for which it has
6592 * a canned string; three cheers for C string
6595 bpf_set_error(cstate
, "getaddrinfo(\"%s\" fails with system error: %d",
6597 port
= -2; /* a real error */
6603 * This is a real error, not just "there's
6604 * no such service name".
6606 * We don't use gai_strerror() because it's not
6607 * guaranteed to be thread-safe on all platforms
6608 * (probably because it might use a non-thread-local
6609 * buffer into which to format an error message
6610 * if the error code isn't one for which it has
6611 * a canned string; three cheers for C string
6614 bpf_set_error(cstate
, "getaddrinfo(\"%s\") fails with error: %d",
6616 port
= -2; /* a real error */
6621 * OK, we found it. Did it find anything?
6623 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
6625 * Does it have an address?
6627 if (ai
->ai_addr
!= NULL
) {
6629 * Yes. Get a port number; we're done.
6631 if (ai
->ai_addr
->sa_family
== AF_INET
) {
6632 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
6633 port
= ntohs(in4
->sin_port
);
6637 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
6638 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
6639 port
= ntohs(in6
->sin6_port
);
6651 * Convert a string to a port number.
6654 stringtoport(compiler_state_t
*cstate
, const char *string
, size_t string_size
,
6664 * See if it's a number.
6666 ret
= stoulen(string
, string_size
, &val
, cstate
);
6670 /* Unknown port type - it's just a number. */
6671 *proto
= PROTO_UNDEF
;
6674 case STOULEN_NOT_OCTAL_NUMBER
:
6675 case STOULEN_NOT_HEX_NUMBER
:
6676 case STOULEN_NOT_DECIMAL_NUMBER
:
6678 * Not a valid number; try looking it up as a port.
6680 cpy
= malloc(string_size
+ 1); /* +1 for terminating '\0' */
6681 memcpy(cpy
, string
, string_size
);
6682 cpy
[string_size
] = '\0';
6683 tcp_port
= nametoport(cstate
, cpy
, IPPROTO_TCP
);
6684 if (tcp_port
== -2) {
6686 * We got a hard error; the error string has
6690 longjmp(cstate
->top_ctx
, 1);
6693 udp_port
= nametoport(cstate
, cpy
, IPPROTO_UDP
);
6694 if (udp_port
== -2) {
6696 * We got a hard error; the error string has
6700 longjmp(cstate
->top_ctx
, 1);
6705 * We need to check /etc/services for ambiguous entries.
6706 * If we find an ambiguous entry, and it has the
6707 * same port number, change the proto to PROTO_UNDEF
6708 * so both TCP and UDP will be checked.
6710 if (tcp_port
>= 0) {
6711 val
= (bpf_u_int32
)tcp_port
;
6712 *proto
= IPPROTO_TCP
;
6713 if (udp_port
>= 0) {
6714 if (udp_port
== tcp_port
)
6715 *proto
= PROTO_UNDEF
;
6718 /* Can't handle ambiguous names that refer
6719 to different port numbers. */
6720 warning("ambiguous port %s in /etc/services",
6727 if (udp_port
>= 0) {
6728 val
= (bpf_u_int32
)udp_port
;
6729 *proto
= IPPROTO_UDP
;
6733 bpf_set_error(cstate
, "'%s' is not a valid port", cpy
);
6735 longjmp(cstate
->top_ctx
, 1);
6742 /* Error already set. */
6743 longjmp(cstate
->top_ctx
, 1);
6750 /* Should not happen */
6751 bpf_set_error(cstate
, "stoulen returned %d - this should not happen", ret
);
6752 longjmp(cstate
->top_ctx
, 1);
6759 * Convert a string in the form PPP-PPP, which correspond to ports, to
6760 * a starting and ending port in a port range.
6763 stringtoportrange(compiler_state_t
*cstate
, const char *string
,
6764 bpf_u_int32
*port1
, bpf_u_int32
*port2
, int *proto
)
6767 const char *first
, *second
;
6768 size_t first_size
, second_size
;
6771 if ((hyphen_off
= strchr(string
, '-')) == NULL
)
6772 bpf_error(cstate
, "port range '%s' contains no hyphen", string
);
6775 * Make sure there are no other hyphens.
6777 * XXX - we support named ports, but there are some port names
6778 * in /etc/services that include hyphens, so this would rule
6781 if (strchr(hyphen_off
+ 1, '-') != NULL
)
6782 bpf_error(cstate
, "port range '%s' contains more than one hyphen",
6786 * Get the length of the first port.
6789 first_size
= hyphen_off
- string
;
6790 if (first_size
== 0) {
6791 /* Range of "-port", which we don't support. */
6792 bpf_error(cstate
, "port range '%s' has no starting port", string
);
6796 * Try to convert it to a port.
6798 *port1
= stringtoport(cstate
, first
, first_size
, proto
);
6799 save_proto
= *proto
;
6802 * Get the length of the second port.
6804 second
= hyphen_off
+ 1;
6805 second_size
= strlen(second
);
6806 if (second_size
== 0) {
6807 /* Range of "port-", which we don't support. */
6808 bpf_error(cstate
, "port range '%s' has no ending port", string
);
6812 * Try to convert it to a port.
6814 *port2
= stringtoport(cstate
, second
, second_size
, proto
);
6815 if (*proto
!= save_proto
)
6816 *proto
= PROTO_UNDEF
;
6820 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6822 int proto
= q
.proto
;
6826 bpf_u_int32 mask
, addr
;
6827 struct addrinfo
*res
, *res0
;
6828 struct sockaddr_in
*sin4
;
6831 struct sockaddr_in6
*sin6
;
6832 struct in6_addr mask128
;
6834 struct block
*b
, *tmp
;
6835 int port
, real_proto
;
6836 bpf_u_int32 port1
, port2
;
6839 * Catch errors reported by us and routines below us, and return NULL
6842 if (setjmp(cstate
->top_ctx
))
6848 addr
= pcap_nametonetaddr(name
);
6850 bpf_error(cstate
, "unknown network '%s'", name
);
6851 /* Left justify network addr and calculate its network mask */
6853 while (addr
&& (addr
& 0xff000000) == 0) {
6857 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6861 if (proto
== Q_LINK
) {
6862 switch (cstate
->linktype
) {
6865 case DLT_NETANALYZER
:
6866 case DLT_NETANALYZER_TRANSPARENT
:
6867 eaddr
= pcap_ether_hostton(name
);
6870 "unknown ether host '%s'", name
);
6871 tmp
= gen_prevlinkhdr_check(cstate
);
6872 b
= gen_ehostop(cstate
, eaddr
, dir
);
6879 eaddr
= pcap_ether_hostton(name
);
6882 "unknown FDDI host '%s'", name
);
6883 b
= gen_fhostop(cstate
, eaddr
, dir
);
6888 eaddr
= pcap_ether_hostton(name
);
6891 "unknown token ring host '%s'", name
);
6892 b
= gen_thostop(cstate
, eaddr
, dir
);
6896 case DLT_IEEE802_11
:
6897 case DLT_PRISM_HEADER
:
6898 case DLT_IEEE802_11_RADIO_AVS
:
6899 case DLT_IEEE802_11_RADIO
:
6901 eaddr
= pcap_ether_hostton(name
);
6904 "unknown 802.11 host '%s'", name
);
6905 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6909 case DLT_IP_OVER_FC
:
6910 eaddr
= pcap_ether_hostton(name
);
6913 "unknown Fibre Channel host '%s'", name
);
6914 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6919 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6920 } else if (proto
== Q_DECNET
) {
6922 * A long time ago on Ultrix libpcap supported
6923 * translation of DECnet host names into DECnet
6924 * addresses, but this feature is history now.
6926 bpf_error(cstate
, "invalid DECnet address '%s'", name
);
6929 memset(&mask128
, 0xff, sizeof(mask128
));
6931 res0
= res
= pcap_nametoaddrinfo(name
);
6933 bpf_error(cstate
, "unknown host '%s'", name
);
6940 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6941 tproto
== Q_DEFAULT
) {
6947 for (res
= res0
; res
; res
= res
->ai_next
) {
6948 switch (res
->ai_family
) {
6951 if (tproto
== Q_IPV6
)
6955 sin4
= (struct sockaddr_in
*)
6957 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6958 0xffffffff, tproto
, dir
, q
.addr
);
6962 if (tproto6
== Q_IP
)
6965 sin6
= (struct sockaddr_in6
*)
6967 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6968 &mask128
, tproto6
, dir
, q
.addr
);
6981 bpf_error(cstate
, "unknown host '%s'%s", name
,
6982 (proto
== Q_DEFAULT
)
6984 : " for specified address family");
6990 if (proto
!= Q_DEFAULT
&&
6991 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6992 bpf_error(cstate
, "illegal qualifier of 'port'");
6993 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6994 bpf_error(cstate
, "unknown port '%s'", name
);
6995 if (proto
== Q_UDP
) {
6996 if (real_proto
== IPPROTO_TCP
)
6997 bpf_error(cstate
, "port '%s' is tcp", name
);
6998 else if (real_proto
== IPPROTO_SCTP
)
6999 bpf_error(cstate
, "port '%s' is sctp", name
);
7001 /* override PROTO_UNDEF */
7002 real_proto
= IPPROTO_UDP
;
7004 if (proto
== Q_TCP
) {
7005 if (real_proto
== IPPROTO_UDP
)
7006 bpf_error(cstate
, "port '%s' is udp", name
);
7008 else if (real_proto
== IPPROTO_SCTP
)
7009 bpf_error(cstate
, "port '%s' is sctp", name
);
7011 /* override PROTO_UNDEF */
7012 real_proto
= IPPROTO_TCP
;
7014 if (proto
== Q_SCTP
) {
7015 if (real_proto
== IPPROTO_UDP
)
7016 bpf_error(cstate
, "port '%s' is udp", name
);
7018 else if (real_proto
== IPPROTO_TCP
)
7019 bpf_error(cstate
, "port '%s' is tcp", name
);
7021 /* override PROTO_UNDEF */
7022 real_proto
= IPPROTO_SCTP
;
7025 bpf_error(cstate
, "illegal port number %d < 0", port
);
7027 bpf_error(cstate
, "illegal port number %d > 65535", port
);
7028 b
= gen_port(cstate
, port
, real_proto
, dir
);
7029 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
7033 if (proto
!= Q_DEFAULT
&&
7034 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
7035 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7036 stringtoportrange(cstate
, name
, &port1
, &port2
, &real_proto
);
7037 if (proto
== Q_UDP
) {
7038 if (real_proto
== IPPROTO_TCP
)
7039 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7040 else if (real_proto
== IPPROTO_SCTP
)
7041 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7043 /* override PROTO_UNDEF */
7044 real_proto
= IPPROTO_UDP
;
7046 if (proto
== Q_TCP
) {
7047 if (real_proto
== IPPROTO_UDP
)
7048 bpf_error(cstate
, "port in range '%s' is udp", name
);
7049 else if (real_proto
== IPPROTO_SCTP
)
7050 bpf_error(cstate
, "port in range '%s' is sctp", name
);
7052 /* override PROTO_UNDEF */
7053 real_proto
= IPPROTO_TCP
;
7055 if (proto
== Q_SCTP
) {
7056 if (real_proto
== IPPROTO_UDP
)
7057 bpf_error(cstate
, "port in range '%s' is udp", name
);
7058 else if (real_proto
== IPPROTO_TCP
)
7059 bpf_error(cstate
, "port in range '%s' is tcp", name
);
7061 /* override PROTO_UNDEF */
7062 real_proto
= IPPROTO_SCTP
;
7065 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
7067 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
7069 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
7070 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
7075 eaddr
= pcap_ether_hostton(name
);
7077 bpf_error(cstate
, "unknown ether host: %s", name
);
7079 res
= pcap_nametoaddrinfo(name
);
7082 bpf_error(cstate
, "unknown host '%s'", name
);
7083 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
7088 bpf_error(cstate
, "unknown host '%s'", name
);
7091 bpf_error(cstate
, "'gateway' not supported in this configuration");
7095 real_proto
= lookup_proto(cstate
, name
, proto
);
7096 if (real_proto
>= 0)
7097 return gen_proto(cstate
, real_proto
, proto
, dir
);
7099 bpf_error(cstate
, "unknown protocol: %s", name
);
7101 #if !defined(NO_PROTOCHAIN)
7103 real_proto
= lookup_proto(cstate
, name
, proto
);
7104 if (real_proto
>= 0)
7105 return gen_protochain(cstate
, real_proto
, proto
);
7107 bpf_error(cstate
, "unknown protocol: %s", name
);
7108 #endif /* !defined(NO_PROTOCHAIN) */
7119 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7120 bpf_u_int32 masklen
, struct qual q
)
7122 register int nlen
, mlen
;
7127 * Catch errors reported by us and routines below us, and return NULL
7130 if (setjmp(cstate
->top_ctx
))
7133 nlen
= pcapint_atoin(s1
, &n
);
7135 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7136 /* Promote short ipaddr */
7140 mlen
= pcapint_atoin(s2
, &m
);
7142 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7143 /* Promote short ipaddr */
7146 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7149 /* Convert mask len to mask */
7151 bpf_error(cstate
, "mask length must be <= 32");
7152 m64
= UINT64_C(0xffffffff) << (32 - masklen
);
7153 m
= (bpf_u_int32
)m64
;
7155 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7162 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7165 bpf_error(cstate
, "Mask syntax for networks only");
7172 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7180 * Catch errors reported by us and routines below us, and return NULL
7183 if (setjmp(cstate
->top_ctx
))
7190 * v contains a 32-bit unsigned parsed from a string of the
7191 * form {N}, which could be decimal, hexadecimal or octal.
7192 * Although it would be possible to use the value as a raw
7193 * 16-bit DECnet address when the value fits into 16 bits, this
7194 * would be a questionable feature: DECnet address wire
7195 * encoding is little-endian, so this would not work as
7196 * intuitively as the same works for [big-endian] IPv4
7197 * addresses (0x01020304 means 1.2.3.4).
7199 if (proto
== Q_DECNET
)
7200 bpf_error(cstate
, "invalid DECnet address '%u'", v
);
7202 } else if (proto
== Q_DECNET
) {
7204 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7205 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7206 * for a valid DECnet address.
7208 vlen
= pcapint_atodn(s
, &v
);
7210 bpf_error(cstate
, "invalid DECnet address '%s'", s
);
7213 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7214 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7217 vlen
= pcapint_atoin(s
, &v
);
7219 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7227 if (proto
== Q_DECNET
)
7228 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7229 else if (proto
== Q_LINK
) {
7230 bpf_error(cstate
, "illegal link layer address");
7233 if (s
== NULL
&& q
.addr
== Q_NET
) {
7234 /* Promote short net number */
7235 while (v
&& (v
& 0xff000000) == 0) {
7240 /* Promote short ipaddr */
7242 mask
<<= 32 - vlen
;
7244 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7249 proto
= IPPROTO_UDP
;
7250 else if (proto
== Q_TCP
)
7251 proto
= IPPROTO_TCP
;
7252 else if (proto
== Q_SCTP
)
7253 proto
= IPPROTO_SCTP
;
7254 else if (proto
== Q_DEFAULT
)
7255 proto
= PROTO_UNDEF
;
7257 bpf_error(cstate
, "illegal qualifier of 'port'");
7260 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7264 b
= gen_port(cstate
, v
, proto
, dir
);
7265 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7271 proto
= IPPROTO_UDP
;
7272 else if (proto
== Q_TCP
)
7273 proto
= IPPROTO_TCP
;
7274 else if (proto
== Q_SCTP
)
7275 proto
= IPPROTO_SCTP
;
7276 else if (proto
== Q_DEFAULT
)
7277 proto
= PROTO_UNDEF
;
7279 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7282 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7286 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7287 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7292 bpf_error(cstate
, "'gateway' requires a name");
7296 return gen_proto(cstate
, v
, proto
, dir
);
7298 #if !defined(NO_PROTOCHAIN)
7300 return gen_protochain(cstate
, v
, proto
);
7316 gen_mcode6(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 masklen
,
7319 struct addrinfo
*res
;
7320 struct in6_addr
*addr
;
7321 struct in6_addr mask
;
7323 bpf_u_int32 a
[4], m
[4]; /* Same as in gen_hostop6(). */
7326 * Catch errors reported by us and routines below us, and return NULL
7329 if (setjmp(cstate
->top_ctx
))
7332 res
= pcap_nametoaddrinfo(s
);
7334 bpf_error(cstate
, "invalid ip6 address %s", s
);
7337 bpf_error(cstate
, "%s resolved to multiple address", s
);
7338 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7340 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7341 bpf_error(cstate
, "mask length must be <= %zu", sizeof(mask
.s6_addr
) * 8);
7342 memset(&mask
, 0, sizeof(mask
));
7343 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7345 mask
.s6_addr
[masklen
/ 8] =
7346 (0xff << (8 - masklen
% 8)) & 0xff;
7349 memcpy(a
, addr
, sizeof(a
));
7350 memcpy(m
, &mask
, sizeof(m
));
7351 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7352 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7353 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s
, masklen
);
7361 bpf_error(cstate
, "Mask syntax for networks only");
7365 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7371 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7378 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7380 struct block
*b
, *tmp
;
7383 * Catch errors reported by us and routines below us, and return NULL
7386 if (setjmp(cstate
->top_ctx
))
7389 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7390 cstate
->e
= pcap_ether_aton(s
);
7391 if (cstate
->e
== NULL
)
7392 bpf_error(cstate
, "malloc");
7393 switch (cstate
->linktype
) {
7395 case DLT_NETANALYZER
:
7396 case DLT_NETANALYZER_TRANSPARENT
:
7397 tmp
= gen_prevlinkhdr_check(cstate
);
7398 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7403 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7406 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7408 case DLT_IEEE802_11
:
7409 case DLT_PRISM_HEADER
:
7410 case DLT_IEEE802_11_RADIO_AVS
:
7411 case DLT_IEEE802_11_RADIO
:
7413 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7415 case DLT_IP_OVER_FC
:
7416 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7421 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7428 bpf_error(cstate
, "ethernet address used in non-ether expression");
7433 sappend(struct slist
*s0
, struct slist
*s1
)
7436 * This is definitely not the best way to do this, but the
7437 * lists will rarely get long.
7444 static struct slist
*
7445 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7449 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7454 static struct slist
*
7455 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7459 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7465 * Modify "index" to use the value stored into its register as an
7466 * offset relative to the beginning of the header for the protocol
7467 * "proto", and allocate a register and put an item "size" bytes long
7468 * (1, 2, or 4) at that offset into that register, making it the register
7471 static struct arth
*
7472 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7476 struct slist
*s
, *tmp
;
7478 int regno
= alloc_reg(cstate
);
7480 free_reg(cstate
, inst
->regno
);
7484 bpf_error(cstate
, "data size must be 1, 2, or 4");
7501 bpf_error(cstate
, "'%s' does not support the index operation", pqkw(proto
));
7505 * The offset is relative to the beginning of the packet
7506 * data, if we have a radio header. (If we don't, this
7509 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7510 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7511 cstate
->linktype
!= DLT_PRISM_HEADER
)
7512 bpf_error(cstate
, "radio information not present in capture");
7515 * Load into the X register the offset computed into the
7516 * register specified by "index".
7518 s
= xfer_to_x(cstate
, inst
);
7521 * Load the item at that offset.
7523 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7525 sappend(inst
->s
, s
);
7530 * The offset is relative to the beginning of
7531 * the link-layer header.
7533 * XXX - what about ATM LANE? Should the index be
7534 * relative to the beginning of the AAL5 frame, so
7535 * that 0 refers to the beginning of the LE Control
7536 * field, or relative to the beginning of the LAN
7537 * frame, so that 0 refers, for Ethernet LANE, to
7538 * the beginning of the destination address?
7540 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7543 * If "s" is non-null, it has code to arrange that the
7544 * X register contains the length of the prefix preceding
7545 * the link-layer header. Add to it the offset computed
7546 * into the register specified by "index", and move that
7547 * into the X register. Otherwise, just load into the X
7548 * register the offset computed into the register specified
7552 sappend(s
, xfer_to_a(cstate
, inst
));
7553 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7554 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7556 s
= xfer_to_x(cstate
, inst
);
7559 * Load the item at the sum of the offset we've put in the
7560 * X register and the offset of the start of the link
7561 * layer header (which is 0 if the radio header is
7562 * variable-length; that header length is what we put
7563 * into the X register and then added to the index).
7565 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7566 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7568 sappend(inst
->s
, s
);
7582 * The offset is relative to the beginning of
7583 * the network-layer header.
7584 * XXX - are there any cases where we want
7585 * cstate->off_nl_nosnap?
7587 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7590 * If "s" is non-null, it has code to arrange that the
7591 * X register contains the variable part of the offset
7592 * of the link-layer payload. Add to it the offset
7593 * computed into the register specified by "index",
7594 * and move that into the X register. Otherwise, just
7595 * load into the X register the offset computed into
7596 * the register specified by "index".
7599 sappend(s
, xfer_to_a(cstate
, inst
));
7600 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7601 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7603 s
= xfer_to_x(cstate
, inst
);
7606 * Load the item at the sum of the offset we've put in the
7607 * X register, the offset of the start of the network
7608 * layer header from the beginning of the link-layer
7609 * payload, and the constant part of the offset of the
7610 * start of the link-layer payload.
7612 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7613 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7615 sappend(inst
->s
, s
);
7618 * Do the computation only if the packet contains
7619 * the protocol in question.
7621 b
= gen_proto_abbrev_internal(cstate
, proto
);
7623 gen_and(inst
->b
, b
);
7637 * The offset is relative to the beginning of
7638 * the transport-layer header.
7640 * Load the X register with the length of the IPv4 header
7641 * (plus the offset of the link-layer header, if it's
7642 * a variable-length header), in bytes.
7644 * XXX - are there any cases where we want
7645 * cstate->off_nl_nosnap?
7646 * XXX - we should, if we're built with
7647 * IPv6 support, generate code to load either
7648 * IPv4, IPv6, or both, as appropriate.
7650 s
= gen_loadx_iphdrlen(cstate
);
7653 * The X register now contains the sum of the variable
7654 * part of the offset of the link-layer payload and the
7655 * length of the network-layer header.
7657 * Load into the A register the offset relative to
7658 * the beginning of the transport layer header,
7659 * add the X register to that, move that to the
7660 * X register, and load with an offset from the
7661 * X register equal to the sum of the constant part of
7662 * the offset of the link-layer payload and the offset,
7663 * relative to the beginning of the link-layer payload,
7664 * of the network-layer header.
7666 sappend(s
, xfer_to_a(cstate
, inst
));
7667 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7668 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7669 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7670 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7671 sappend(inst
->s
, s
);
7674 * Do the computation only if the packet contains
7675 * the protocol in question - which is true only
7676 * if this is an IP datagram and is the first or
7677 * only fragment of that datagram.
7679 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7681 gen_and(inst
->b
, b
);
7682 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7687 * Do the computation only if the packet contains
7688 * the protocol in question.
7690 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7692 gen_and(inst
->b
, b
);
7696 * Check if we have an icmp6 next header
7698 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7700 gen_and(inst
->b
, b
);
7703 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7705 * If "s" is non-null, it has code to arrange that the
7706 * X register contains the variable part of the offset
7707 * of the link-layer payload. Add to it the offset
7708 * computed into the register specified by "index",
7709 * and move that into the X register. Otherwise, just
7710 * load into the X register the offset computed into
7711 * the register specified by "index".
7714 sappend(s
, xfer_to_a(cstate
, inst
));
7715 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7716 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7718 s
= xfer_to_x(cstate
, inst
);
7721 * Load the item at the sum of the offset we've put in the
7722 * X register, the offset of the start of the network
7723 * layer header from the beginning of the link-layer
7724 * payload, and the constant part of the offset of the
7725 * start of the link-layer payload.
7727 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7728 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7731 sappend(inst
->s
, s
);
7735 inst
->regno
= regno
;
7736 s
= new_stmt(cstate
, BPF_ST
);
7738 sappend(inst
->s
, s
);
7744 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7748 * Catch errors reported by us and routines below us, and return NULL
7751 if (setjmp(cstate
->top_ctx
))
7754 return gen_load_internal(cstate
, proto
, inst
, size
);
7757 static struct block
*
7758 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7759 struct arth
*a1
, int reversed
)
7761 struct slist
*s0
, *s1
, *s2
;
7762 struct block
*b
, *tmp
;
7764 s0
= xfer_to_x(cstate
, a1
);
7765 s1
= xfer_to_a(cstate
, a0
);
7766 if (code
== BPF_JEQ
) {
7767 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7768 b
= new_block(cstate
, JMP(code
));
7772 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7778 sappend(a0
->s
, a1
->s
);
7782 free_reg(cstate
, a0
->regno
);
7783 free_reg(cstate
, a1
->regno
);
7785 /* 'and' together protocol checks */
7788 gen_and(a0
->b
, tmp
= a1
->b
);
7802 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7803 struct arth
*a1
, int reversed
)
7806 * Catch errors reported by us and routines below us, and return NULL
7809 if (setjmp(cstate
->top_ctx
))
7812 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7816 gen_loadlen(compiler_state_t
*cstate
)
7823 * Catch errors reported by us and routines below us, and return NULL
7826 if (setjmp(cstate
->top_ctx
))
7829 regno
= alloc_reg(cstate
);
7830 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7831 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7832 s
->next
= new_stmt(cstate
, BPF_ST
);
7833 s
->next
->s
.k
= regno
;
7840 static struct arth
*
7841 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7847 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7849 reg
= alloc_reg(cstate
);
7851 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7853 s
->next
= new_stmt(cstate
, BPF_ST
);
7862 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7865 * Catch errors reported by us and routines below us, and return NULL
7868 if (setjmp(cstate
->top_ctx
))
7871 return gen_loadi_internal(cstate
, val
);
7875 * The a_arg dance is to avoid annoying whining by compilers that
7876 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7877 * It's not *used* after setjmp returns.
7880 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7882 struct arth
*a
= a_arg
;
7886 * Catch errors reported by us and routines below us, and return NULL
7889 if (setjmp(cstate
->top_ctx
))
7892 s
= xfer_to_a(cstate
, a
);
7894 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7897 s
= new_stmt(cstate
, BPF_ST
);
7905 * The a0_arg dance is to avoid annoying whining by compilers that
7906 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7907 * It's not *used* after setjmp returns.
7910 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7913 struct arth
*a0
= a0_arg
;
7914 struct slist
*s0
, *s1
, *s2
;
7917 * Catch errors reported by us and routines below us, and return NULL
7920 if (setjmp(cstate
->top_ctx
))
7924 * Disallow division by, or modulus by, zero; we do this here
7925 * so that it gets done even if the optimizer is disabled.
7927 * Also disallow shifts by a value greater than 31; we do this
7928 * here, for the same reason.
7930 if (code
== BPF_DIV
) {
7931 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7932 bpf_error(cstate
, "division by zero");
7933 } else if (code
== BPF_MOD
) {
7934 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7935 bpf_error(cstate
, "modulus by zero");
7936 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7937 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7938 bpf_error(cstate
, "shift by more than 31 bits");
7940 s0
= xfer_to_x(cstate
, a1
);
7941 s1
= xfer_to_a(cstate
, a0
);
7942 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7947 sappend(a0
->s
, a1
->s
);
7949 free_reg(cstate
, a0
->regno
);
7950 free_reg(cstate
, a1
->regno
);
7952 s0
= new_stmt(cstate
, BPF_ST
);
7953 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7960 * Initialize the table of used registers and the current register.
7963 init_regs(compiler_state_t
*cstate
)
7966 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7970 * Return the next free register.
7973 alloc_reg(compiler_state_t
*cstate
)
7975 int n
= BPF_MEMWORDS
;
7978 if (cstate
->regused
[cstate
->curreg
])
7979 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7981 cstate
->regused
[cstate
->curreg
] = 1;
7982 return cstate
->curreg
;
7985 bpf_error(cstate
, "too many registers needed to evaluate expression");
7990 * Return a register to the table so it can
7994 free_reg(compiler_state_t
*cstate
, int n
)
7996 cstate
->regused
[n
] = 0;
7999 static struct block
*
8000 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
8005 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
8006 b
= new_block(cstate
, JMP(jmp
));
8014 gen_greater(compiler_state_t
*cstate
, int n
)
8017 * Catch errors reported by us and routines below us, and return NULL
8020 if (setjmp(cstate
->top_ctx
))
8023 return gen_len(cstate
, BPF_JGE
, n
);
8027 * Actually, this is less than or equal.
8030 gen_less(compiler_state_t
*cstate
, int n
)
8035 * Catch errors reported by us and routines below us, and return NULL
8038 if (setjmp(cstate
->top_ctx
))
8041 b
= gen_len(cstate
, BPF_JGT
, n
);
8048 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8049 * the beginning of the link-layer header.
8050 * XXX - that means you can't test values in the radiotap header, but
8051 * as that header is difficult if not impossible to parse generally
8052 * without a loop, that might not be a severe problem. A new keyword
8053 * "radio" could be added for that, although what you'd really want
8054 * would be a way of testing particular radio header values, which
8055 * would generate code appropriate to the radio header in question.
8058 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
8064 * Catch errors reported by us and routines below us, and return NULL
8067 if (setjmp(cstate
->top_ctx
))
8075 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8078 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8082 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
8086 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
8090 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8094 b
= new_block(cstate
, JMP(BPF_JEQ
));
8102 gen_broadcast(compiler_state_t
*cstate
, int proto
)
8104 bpf_u_int32 hostmask
;
8105 struct block
*b0
, *b1
, *b2
;
8106 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8109 * Catch errors reported by us and routines below us, and return NULL
8112 if (setjmp(cstate
->top_ctx
))
8119 switch (cstate
->linktype
) {
8121 case DLT_ARCNET_LINUX
:
8122 // ARCnet broadcast is [8-bit] destination address 0.
8123 return gen_ahostop(cstate
, 0, Q_DST
);
8125 case DLT_NETANALYZER
:
8126 case DLT_NETANALYZER_TRANSPARENT
:
8127 b1
= gen_prevlinkhdr_check(cstate
);
8128 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
8133 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
8135 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
8136 case DLT_IEEE802_11
:
8137 case DLT_PRISM_HEADER
:
8138 case DLT_IEEE802_11_RADIO_AVS
:
8139 case DLT_IEEE802_11_RADIO
:
8141 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8142 case DLT_IP_OVER_FC
:
8143 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8145 fail_kw_on_dlt(cstate
, "broadcast");
8150 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8151 * as an indication that we don't know the netmask, and fail
8154 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8155 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8156 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8157 hostmask
= ~cstate
->netmask
;
8158 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8159 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, hostmask
, hostmask
);
8164 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "broadcast");
8169 * Generate code to test the low-order bit of a MAC address (that's
8170 * the bottom bit of the *first* byte).
8172 static struct block
*
8173 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8175 register struct block
*b0
;
8176 register struct slist
*s
;
8178 /* link[offset] & 1 != 0 */
8179 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8180 b0
= new_block(cstate
, JMP(BPF_JSET
));
8187 gen_multicast(compiler_state_t
*cstate
, int proto
)
8189 register struct block
*b0
, *b1
, *b2
;
8190 register struct slist
*s
;
8193 * Catch errors reported by us and routines below us, and return NULL
8196 if (setjmp(cstate
->top_ctx
))
8203 switch (cstate
->linktype
) {
8205 case DLT_ARCNET_LINUX
:
8206 // ARCnet multicast is the same as broadcast.
8207 return gen_ahostop(cstate
, 0, Q_DST
);
8209 case DLT_NETANALYZER
:
8210 case DLT_NETANALYZER_TRANSPARENT
:
8211 b1
= gen_prevlinkhdr_check(cstate
);
8212 /* ether[0] & 1 != 0 */
8213 b0
= gen_mac_multicast(cstate
, 0);
8219 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8221 * XXX - was that referring to bit-order issues?
8223 /* fddi[1] & 1 != 0 */
8224 return gen_mac_multicast(cstate
, 1);
8226 /* tr[2] & 1 != 0 */
8227 return gen_mac_multicast(cstate
, 2);
8228 case DLT_IEEE802_11
:
8229 case DLT_PRISM_HEADER
:
8230 case DLT_IEEE802_11_RADIO_AVS
:
8231 case DLT_IEEE802_11_RADIO
:
8236 * For control frames, there is no DA.
8238 * For management frames, DA is at an
8239 * offset of 4 from the beginning of
8242 * For data frames, DA is at an offset
8243 * of 4 from the beginning of the packet
8244 * if To DS is clear and at an offset of
8245 * 16 from the beginning of the packet
8250 * Generate the tests to be done for data frames.
8252 * First, check for To DS set, i.e. "link[1] & 0x01".
8254 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8255 b1
= new_block(cstate
, JMP(BPF_JSET
));
8256 b1
->s
.k
= 0x01; /* To DS */
8260 * If To DS is set, the DA is at 16.
8262 b0
= gen_mac_multicast(cstate
, 16);
8266 * Now, check for To DS not set, i.e. check
8267 * "!(link[1] & 0x01)".
8269 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8270 b2
= new_block(cstate
, JMP(BPF_JSET
));
8271 b2
->s
.k
= 0x01; /* To DS */
8276 * If To DS is not set, the DA is at 4.
8278 b1
= gen_mac_multicast(cstate
, 4);
8282 * Now OR together the last two checks. That gives
8283 * the complete set of checks for data frames.
8288 * Now check for a data frame.
8289 * I.e, check "link[0] & 0x08".
8291 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8292 b1
= new_block(cstate
, JMP(BPF_JSET
));
8297 * AND that with the checks done for data frames.
8302 * If the high-order bit of the type value is 0, this
8303 * is a management frame.
8304 * I.e, check "!(link[0] & 0x08)".
8306 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8307 b2
= new_block(cstate
, JMP(BPF_JSET
));
8313 * For management frames, the DA is at 4.
8315 b1
= gen_mac_multicast(cstate
, 4);
8319 * OR that with the checks done for data frames.
8320 * That gives the checks done for management and
8326 * If the low-order bit of the type value is 1,
8327 * this is either a control frame or a frame
8328 * with a reserved type, and thus not a
8331 * I.e., check "!(link[0] & 0x04)".
8333 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8334 b1
= new_block(cstate
, JMP(BPF_JSET
));
8340 * AND that with the checks for data and management
8345 case DLT_IP_OVER_FC
:
8346 b0
= gen_mac_multicast(cstate
, 2);
8351 fail_kw_on_dlt(cstate
, "multicast");
8355 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8356 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8361 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8362 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8366 bpf_error(cstate
, ERRSTR_INVALID_QUAL
, pqkw(proto
), "multicast");
8372 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8373 * we can look at special meta-data in the filter expression; otherwise we
8374 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8375 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8376 * pcap_activate() conditionally sets.
8379 require_basic_bpf_extensions(compiler_state_t
*cstate
, const char *keyword
)
8381 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_BASIC_HANDLING
)
8383 bpf_error(cstate
, "%s not supported on %s (not a live capture)",
8385 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8390 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8392 register struct block
*b0
;
8395 * Catch errors reported by us and routines below us, and return NULL
8398 if (setjmp(cstate
->top_ctx
))
8402 * Only some data link types support ifindex qualifiers.
8404 switch (cstate
->linktype
) {
8405 case DLT_LINUX_SLL2
:
8406 /* match packets on this interface */
8407 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8410 #if defined(__linux__)
8411 require_basic_bpf_extensions(cstate
, "ifindex");
8413 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8415 #else /* defined(__linux__) */
8416 fail_kw_on_dlt(cstate
, "ifindex");
8418 #endif /* defined(__linux__) */
8424 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8425 * Outbound traffic is sent by this machine, while inbound traffic is
8426 * sent by a remote machine (and may include packets destined for a
8427 * unicast or multicast link-layer address we are not subscribing to).
8428 * These are the same definitions implemented by pcap_setdirection().
8429 * Capturing only unicast traffic destined for this host is probably
8430 * better accomplished using a higher-layer filter.
8433 gen_inbound_outbound(compiler_state_t
*cstate
, const int outbound
)
8435 register struct block
*b0
;
8438 * Catch errors reported by us and routines below us, and return NULL
8441 if (setjmp(cstate
->top_ctx
))
8445 * Only some data link types support inbound/outbound qualifiers.
8447 switch (cstate
->linktype
) {
8449 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
,
8450 outbound
? SLIPDIR_OUT
: SLIPDIR_IN
);
8454 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
,
8455 outbound
? IPNET_OUTBOUND
: IPNET_INBOUND
);
8459 /* match outgoing packets */
8460 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8462 /* to filter on inbound traffic, invert the match */
8467 case DLT_LINUX_SLL2
:
8468 /* match outgoing packets */
8469 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8471 /* to filter on inbound traffic, invert the match */
8477 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8478 outbound
? PF_OUT
: PF_IN
);
8482 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, outbound
? PPP_PPPD_OUT
: PPP_PPPD_IN
);
8485 case DLT_JUNIPER_MFR
:
8486 case DLT_JUNIPER_MLFR
:
8487 case DLT_JUNIPER_MLPPP
:
8488 case DLT_JUNIPER_ATM1
:
8489 case DLT_JUNIPER_ATM2
:
8490 case DLT_JUNIPER_PPPOE
:
8491 case DLT_JUNIPER_PPPOE_ATM
:
8492 case DLT_JUNIPER_GGSN
:
8493 case DLT_JUNIPER_ES
:
8494 case DLT_JUNIPER_MONITOR
:
8495 case DLT_JUNIPER_SERVICES
:
8496 case DLT_JUNIPER_ETHER
:
8497 case DLT_JUNIPER_PPP
:
8498 case DLT_JUNIPER_FRELAY
:
8499 case DLT_JUNIPER_CHDLC
:
8500 case DLT_JUNIPER_VP
:
8501 case DLT_JUNIPER_ST
:
8502 case DLT_JUNIPER_ISM
:
8503 case DLT_JUNIPER_VS
:
8504 case DLT_JUNIPER_SRX_E2E
:
8505 case DLT_JUNIPER_FIBRECHANNEL
:
8506 case DLT_JUNIPER_ATM_CEMIC
:
8507 /* juniper flags (including direction) are stored
8508 * the byte after the 3-byte magic number */
8509 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, outbound
? 0 : 1, 0x01);
8514 * If we have packet meta-data indicating a direction,
8515 * and that metadata can be checked by BPF code, check
8516 * it. Otherwise, give up, as this link-layer type has
8517 * nothing in the packet data.
8519 * Currently, the only platform where a BPF filter can
8520 * check that metadata is Linux with the in-kernel
8521 * BPF interpreter. If other packet capture mechanisms
8522 * and BPF filters also supported this, it would be
8523 * nice. It would be even better if they made that
8524 * metadata available so that we could provide it
8525 * with newer capture APIs, allowing it to be saved
8528 #if defined(__linux__)
8529 require_basic_bpf_extensions(cstate
, outbound
? "outbound" : "inbound");
8530 /* match outgoing packets */
8531 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8534 /* to filter on inbound traffic, invert the match */
8537 #else /* defined(__linux__) */
8538 fail_kw_on_dlt(cstate
, outbound
? "outbound" : "inbound");
8540 #endif /* defined(__linux__) */
8545 /* PF firewall log matched interface */
8547 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8553 * Catch errors reported by us and routines below us, and return NULL
8556 if (setjmp(cstate
->top_ctx
))
8559 assert_pflog(cstate
, "ifname");
8561 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8562 off
= offsetof(struct pfloghdr
, ifname
);
8563 if (strlen(ifname
) >= len
) {
8564 bpf_error(cstate
, "ifname interface names can only be %d characters",
8568 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8569 (const u_char
*)ifname
);
8573 /* PF firewall log ruleset name */
8575 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8580 * Catch errors reported by us and routines below us, and return NULL
8583 if (setjmp(cstate
->top_ctx
))
8586 assert_pflog(cstate
, "ruleset");
8588 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8589 bpf_error(cstate
, "ruleset names can only be %ld characters",
8590 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8594 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8595 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8599 /* PF firewall log rule number */
8601 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8606 * Catch errors reported by us and routines below us, and return NULL
8609 if (setjmp(cstate
->top_ctx
))
8612 assert_pflog(cstate
, "rnr");
8614 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8619 /* PF firewall log sub-rule number */
8621 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8626 * Catch errors reported by us and routines below us, and return NULL
8629 if (setjmp(cstate
->top_ctx
))
8632 assert_pflog(cstate
, "srnr");
8634 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8639 /* PF firewall log reason code */
8641 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8646 * Catch errors reported by us and routines below us, and return NULL
8649 if (setjmp(cstate
->top_ctx
))
8652 assert_pflog(cstate
, "reason");
8654 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8655 (bpf_u_int32
)reason
);
8659 /* PF firewall log action */
8661 gen_pf_action(compiler_state_t
*cstate
, int action
)
8666 * Catch errors reported by us and routines below us, and return NULL
8669 if (setjmp(cstate
->top_ctx
))
8672 assert_pflog(cstate
, "action");
8674 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8675 (bpf_u_int32
)action
);
8679 /* IEEE 802.11 wireless header */
8681 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8686 * Catch errors reported by us and routines below us, and return NULL
8689 if (setjmp(cstate
->top_ctx
))
8692 switch (cstate
->linktype
) {
8694 case DLT_IEEE802_11
:
8695 case DLT_PRISM_HEADER
:
8696 case DLT_IEEE802_11_RADIO_AVS
:
8697 case DLT_IEEE802_11_RADIO
:
8699 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8703 fail_kw_on_dlt(cstate
, "type/subtype");
8711 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8716 * Catch errors reported by us and routines below us, and return NULL
8719 if (setjmp(cstate
->top_ctx
))
8722 switch (cstate
->linktype
) {
8724 case DLT_IEEE802_11
:
8725 case DLT_PRISM_HEADER
:
8726 case DLT_IEEE802_11_RADIO_AVS
:
8727 case DLT_IEEE802_11_RADIO
:
8732 fail_kw_on_dlt(cstate
, "dir");
8736 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8737 IEEE80211_FC1_DIR_MASK
);
8742 // Process an ARCnet host address string.
8744 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8747 * Catch errors reported by us and routines below us, and return NULL
8750 if (setjmp(cstate
->top_ctx
))
8753 switch (cstate
->linktype
) {
8756 case DLT_ARCNET_LINUX
:
8757 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8758 q
.proto
== Q_LINK
) {
8761 * The lexer currently defines the address format in a
8762 * way that makes this error condition never true.
8763 * Let's check it anyway in case this part of the lexer
8764 * changes in future.
8766 if (! pcapint_atoan(s
, &addr
))
8767 bpf_error(cstate
, "invalid ARCnet address '%s'", s
);
8768 return gen_ahostop(cstate
, addr
, (int)q
.dir
);
8770 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8774 bpf_error(cstate
, "aid supported only on ARCnet");
8779 // Compare an ARCnet host address with the given value.
8780 static struct block
*
8781 gen_ahostop(compiler_state_t
*cstate
, const uint8_t eaddr
, int dir
)
8783 register struct block
*b0
, *b1
;
8787 * ARCnet is different from Ethernet: the source address comes before
8788 * the destination address, each is one byte long. This holds for all
8789 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
8790 * in the 1983 edition of the "ARCNET Designer's Handbook" published
8791 * by Datapoint (document number 61610-01).
8794 return gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, eaddr
);
8797 return gen_cmp(cstate
, OR_LINKHDR
, 1, BPF_B
, eaddr
);
8800 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8801 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8807 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8808 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8818 bpf_error(cstate
, ERRSTR_802_11_ONLY_KW
, dqkw(dir
));
8825 static struct block
*
8826 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8828 struct block
*b0
, *b1
;
8830 /* check for VLAN, including 802.1ad and QinQ */
8831 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8832 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8835 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8841 static struct block
*
8842 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8844 if (vlan_num
> 0x0fff) {
8845 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8848 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8851 static struct block
*
8852 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8855 struct block
*b0
, *b1
;
8857 b0
= gen_vlan_tpid_test(cstate
);
8860 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8866 * Both payload and link header type follow the VLAN tags so that
8867 * both need to be updated.
8869 cstate
->off_linkpl
.constant_part
+= 4;
8870 cstate
->off_linktype
.constant_part
+= 4;
8875 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8876 /* add v to variable part of off */
8878 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8879 bpf_u_int32 v
, struct slist
*s
)
8883 if (!off
->is_variable
)
8884 off
->is_variable
= 1;
8886 off
->reg
= alloc_reg(cstate
);
8888 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8891 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8894 s2
= new_stmt(cstate
, BPF_ST
);
8900 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8901 * and link type offsets first
8904 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8908 /* offset determined at run time, shift variable part */
8910 cstate
->is_vlan_vloffset
= 1;
8911 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8912 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8914 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8915 sappend(s
.next
, b_tpid
->head
->stmts
);
8916 b_tpid
->head
->stmts
= s
.next
;
8920 * patch block b_vid (VLAN id test) to load VID value either from packet
8921 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8924 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8926 struct slist
*s
, *s2
, *sjeq
;
8929 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8930 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8932 /* true -> next instructions, false -> beginning of b_vid */
8933 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8935 sjeq
->s
.jf
= b_vid
->stmts
;
8938 s2
= new_stmt(cstate
, BPF_LD
|BPF_H
|BPF_ABS
);
8939 s2
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG
);
8943 /* Jump to the test in b_vid. We need to jump one instruction before
8944 * the end of the b_vid block so that we only skip loading the TCI
8945 * from packet data and not the 'and' instruction extracting VID.
8948 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8950 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8954 /* insert our statements at the beginning of b_vid */
8955 sappend(s
, b_vid
->stmts
);
8960 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8961 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8962 * tag can be either in metadata or in packet data; therefore if the
8963 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8964 * header for VLAN tag. As the decision is done at run time, we need
8965 * update variable part of the offsets
8967 static struct block
*
8968 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8971 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8974 /* generate new filter code based on extracting packet
8976 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8977 s
->s
.k
= (bpf_u_int32
)(SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
);
8979 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8984 * This is tricky. We need to insert the statements updating variable
8985 * parts of offsets before the traditional TPID and VID tests so
8986 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8987 * we do not want this update to affect those checks. That's why we
8988 * generate both test blocks first and insert the statements updating
8989 * variable parts of both offsets after that. This wouldn't work if
8990 * there already were variable length link header when entering this
8991 * function but gen_vlan_bpf_extensions() isn't called in that case.
8993 b_tpid
= gen_vlan_tpid_test(cstate
);
8995 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8997 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
9002 gen_vlan_patch_vid_test(cstate
, b_vid
);
9012 * support IEEE 802.1Q VLAN trunk over ethernet
9015 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
9020 * Catch errors reported by us and routines below us, and return NULL
9023 if (setjmp(cstate
->top_ctx
))
9026 /* can't check for VLAN-encapsulated packets inside MPLS */
9027 if (cstate
->label_stack_depth
> 0)
9028 bpf_error(cstate
, "no VLAN match after MPLS");
9031 * Check for a VLAN packet, and then change the offsets to point
9032 * to the type and data fields within the VLAN packet. Just
9033 * increment the offsets, so that we can support a hierarchy, e.g.
9034 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9037 * XXX - this is a bit of a kludge. If we were to split the
9038 * compiler into a parser that parses an expression and
9039 * generates an expression tree, and a code generator that
9040 * takes an expression tree (which could come from our
9041 * parser or from some other parser) and generates BPF code,
9042 * we could perhaps make the offsets parameters of routines
9043 * and, in the handler for an "AND" node, pass to subnodes
9044 * other than the VLAN node the adjusted offsets.
9046 * This would mean that "vlan" would, instead of changing the
9047 * behavior of *all* tests after it, change only the behavior
9048 * of tests ANDed with it. That would change the documented
9049 * semantics of "vlan", which might break some expressions.
9050 * However, it would mean that "(vlan and ip) or ip" would check
9051 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9052 * checking only for VLAN-encapsulated IP, so that could still
9053 * be considered worth doing; it wouldn't break expressions
9054 * that are of the form "vlan and ..." or "vlan N and ...",
9055 * which I suspect are the most common expressions involving
9056 * "vlan". "vlan or ..." doesn't necessarily do what the user
9057 * would really want, now, as all the "or ..." tests would
9058 * be done assuming a VLAN, even though the "or" could be viewed
9059 * as meaning "or, if this isn't a VLAN packet...".
9061 switch (cstate
->linktype
) {
9065 * Newer version of the Linux kernel pass around
9066 * packets in which the VLAN tag has been removed
9067 * from the packet data and put into metadata.
9069 * This requires special treatment.
9071 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9072 /* Verify that this is the outer part of the packet and
9073 * not encapsulated somehow. */
9074 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9075 cstate
->off_linkhdr
.constant_part
==
9076 cstate
->off_outermostlinkhdr
.constant_part
) {
9078 * Do we need special VLAN handling?
9080 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9081 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9084 b0
= gen_vlan_no_bpf_extensions(cstate
,
9085 vlan_num
, has_vlan_tag
);
9088 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9092 case DLT_NETANALYZER
:
9093 case DLT_NETANALYZER_TRANSPARENT
:
9094 case DLT_IEEE802_11
:
9095 case DLT_PRISM_HEADER
:
9096 case DLT_IEEE802_11_RADIO_AVS
:
9097 case DLT_IEEE802_11_RADIO
:
9099 * These are either Ethernet packets with an additional
9100 * metadata header (the NetAnalyzer types), or 802.11
9101 * packets, possibly with an additional metadata header.
9103 * For the first of those, the VLAN tag is in the normal
9104 * place, so the special-case handling above isn't
9107 * For the second of those, we don't do the special-case
9110 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9114 bpf_error(cstate
, "no VLAN support for %s",
9115 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9119 cstate
->vlan_stack_depth
++;
9127 * The label_num_arg dance is to avoid annoying whining by compilers that
9128 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9129 * It's not *used* after setjmp returns.
9131 static struct block
*
9132 gen_mpls_internal(compiler_state_t
*cstate
, bpf_u_int32 label_num
,
9135 struct block
*b0
, *b1
;
9137 if (cstate
->label_stack_depth
> 0) {
9138 /* just match the bottom-of-stack bit clear */
9139 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9142 * We're not in an MPLS stack yet, so check the link-layer
9143 * type against MPLS.
9145 switch (cstate
->linktype
) {
9147 case DLT_C_HDLC
: /* fall through */
9150 case DLT_NETANALYZER
:
9151 case DLT_NETANALYZER_TRANSPARENT
:
9152 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9156 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9159 /* FIXME add other DLT_s ...
9160 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9161 * leave it for now */
9164 bpf_error(cstate
, "no MPLS support for %s",
9165 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9170 /* If a specific MPLS label is requested, check it */
9171 if (has_label_num
) {
9172 if (label_num
> 0xFFFFF) {
9173 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9174 label_num
, 0xFFFFF);
9176 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9177 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9178 0xfffff000); /* only compare the first 20 bits */
9184 * Change the offsets to point to the type and data fields within
9185 * the MPLS packet. Just increment the offsets, so that we
9186 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9187 * capture packets with an outer label of 100000 and an inner
9190 * Increment the MPLS stack depth as well; this indicates that
9191 * we're checking MPLS-encapsulated headers, to make sure higher
9192 * level code generators don't try to match against IP-related
9193 * protocols such as Q_ARP, Q_RARP etc.
9195 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9197 cstate
->off_nl_nosnap
+= 4;
9198 cstate
->off_nl
+= 4;
9199 cstate
->label_stack_depth
++;
9204 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
9207 * Catch errors reported by us and routines below us, and return NULL
9210 if (setjmp(cstate
->top_ctx
))
9213 return gen_mpls_internal(cstate
, label_num
, has_label_num
);
9217 * Support PPPOE discovery and session.
9220 gen_pppoed(compiler_state_t
*cstate
)
9223 * Catch errors reported by us and routines below us, and return NULL
9226 if (setjmp(cstate
->top_ctx
))
9229 /* check for PPPoE discovery */
9230 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9234 * RFC 2516 Section 4:
9236 * The Ethernet payload for PPPoE is as follows:
9239 * 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
9240 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9241 * | VER | TYPE | CODE | SESSION_ID |
9242 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9243 * | LENGTH | payload ~
9244 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9247 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9249 struct block
*b0
, *b1
;
9252 * Catch errors reported by us and routines below us, and return NULL
9255 if (setjmp(cstate
->top_ctx
))
9259 * Test against the PPPoE session link-layer type.
9261 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9263 /* If a specific session is requested, check PPPoE session id */
9265 if (sess_num
> UINT16_MAX
) {
9266 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9267 sess_num
, UINT16_MAX
);
9269 b1
= gen_cmp(cstate
, OR_LINKPL
, 2, BPF_H
, sess_num
);
9275 * Change the offsets to point to the type and data fields within
9276 * the PPP packet, and note that this is PPPoE rather than
9279 * XXX - this is a bit of a kludge. See the comments in
9282 * The "network-layer" protocol is PPPoE, which has a 6-byte
9283 * PPPoE header, followed by a PPP packet.
9285 * There is no HDLC encapsulation for the PPP packet (it's
9286 * encapsulated in PPPoES instead), so the link-layer type
9287 * starts at the first byte of the PPP packet. For PPPoE,
9288 * that offset is relative to the beginning of the total
9289 * link-layer payload, including any 802.2 LLC header, so
9290 * it's 6 bytes past cstate->off_nl.
9292 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9293 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9294 cstate
->off_linkpl
.reg
);
9296 cstate
->off_linktype
= cstate
->off_linkhdr
;
9297 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9300 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9305 /* Check that this is Geneve and the VNI is correct if
9306 * specified. Parameterized to handle both IPv4 and IPv6. */
9307 static struct block
*
9308 gen_geneve_check(compiler_state_t
*cstate
,
9309 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9310 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9312 struct block
*b0
, *b1
;
9314 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9316 /* Check that we are operating on version 0. Otherwise, we
9317 * can't decode the rest of the fields. The version is 2 bits
9318 * in the first byte of the Geneve header. */
9319 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9324 if (vni
> 0xffffff) {
9325 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9328 vni
<<= 8; /* VNI is in the upper 3 bytes */
9329 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9337 /* The IPv4 and IPv6 Geneve checks need to do two things:
9338 * - Verify that this actually is Geneve with the right VNI.
9339 * - Place the IP header length (plus variable link prefix if
9340 * needed) into register A to be used later to compute
9341 * the inner packet offsets. */
9342 static struct block
*
9343 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9345 struct block
*b0
, *b1
;
9346 struct slist
*s
, *s1
;
9348 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9350 /* Load the IP header length into A. */
9351 s
= gen_loadx_iphdrlen(cstate
);
9353 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9356 /* Forcibly append these statements to the true condition
9357 * of the protocol check by creating a new block that is
9358 * always true and ANDing them. */
9359 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9368 static struct block
*
9369 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9371 struct block
*b0
, *b1
;
9372 struct slist
*s
, *s1
;
9374 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9376 /* Load the IP header length. We need to account for a
9377 * variable length link prefix if there is one. */
9378 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9380 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9384 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9388 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9392 /* Forcibly append these statements to the true condition
9393 * of the protocol check by creating a new block that is
9394 * always true and ANDing them. */
9395 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9398 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9407 /* We need to store three values based on the Geneve header::
9408 * - The offset of the linktype.
9409 * - The offset of the end of the Geneve header.
9410 * - The offset of the end of the encapsulated MAC header. */
9411 static struct slist
*
9412 gen_geneve_offsets(compiler_state_t
*cstate
)
9414 struct slist
*s
, *s1
, *s_proto
;
9416 /* First we need to calculate the offset of the Geneve header
9417 * itself. This is composed of the IP header previously calculated
9418 * (include any variable link prefix) and stored in A plus the
9419 * fixed sized headers (fixed link prefix, MAC length, and UDP
9421 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9422 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9424 /* Stash this in X since we'll need it later. */
9425 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9428 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9430 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9434 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9435 cstate
->off_linktype
.is_variable
= 1;
9436 cstate
->off_linktype
.constant_part
= 0;
9438 s1
= new_stmt(cstate
, BPF_ST
);
9439 s1
->s
.k
= cstate
->off_linktype
.reg
;
9442 /* Load the Geneve option length and mask and shift to get the
9443 * number of bytes. It is stored in the first byte of the Geneve
9445 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9449 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9453 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9457 /* Add in the rest of the Geneve base header. */
9458 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9462 /* Add the Geneve header length to its offset and store. */
9463 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9467 /* Set the encapsulated type as Ethernet. Even though we may
9468 * not actually have Ethernet inside there are two reasons this
9470 * - The linktype field is always in EtherType format regardless
9471 * of whether it is in Geneve or an inner Ethernet frame.
9472 * - The only link layer that we have specific support for is
9473 * Ethernet. We will confirm that the packet actually is
9474 * Ethernet at runtime before executing these checks. */
9475 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9477 s1
= new_stmt(cstate
, BPF_ST
);
9478 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9481 /* Calculate whether we have an Ethernet header or just raw IP/
9482 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9483 * and linktype by 14 bytes so that the network header can be found
9484 * seamlessly. Otherwise, keep what we've calculated already. */
9486 /* We have a bare jmp so we can't use the optimizer. */
9487 cstate
->no_optimize
= 1;
9489 /* Load the EtherType in the Geneve header, 2 bytes in. */
9490 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9494 /* Load X with the end of the Geneve header. */
9495 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9496 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9499 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9500 * end of this check, we should have the total length in X. In
9501 * the non-Ethernet case, it's already there. */
9502 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9503 s_proto
->s
.k
= ETHERTYPE_TEB
;
9504 sappend(s
, s_proto
);
9506 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9510 /* Since this is Ethernet, use the EtherType of the payload
9511 * directly as the linktype. Overwrite what we already have. */
9512 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9516 s1
= new_stmt(cstate
, BPF_ST
);
9517 s1
->s
.k
= cstate
->off_linktype
.reg
;
9520 /* Advance two bytes further to get the end of the Ethernet
9522 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9526 /* Move the result to X. */
9527 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9530 /* Store the final result of our linkpl calculation. */
9531 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9532 cstate
->off_linkpl
.is_variable
= 1;
9533 cstate
->off_linkpl
.constant_part
= 0;
9535 s1
= new_stmt(cstate
, BPF_STX
);
9536 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9545 /* Check to see if this is a Geneve packet. */
9547 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9549 struct block
*b0
, *b1
;
9553 * Catch errors reported by us and routines below us, and return NULL
9556 if (setjmp(cstate
->top_ctx
))
9559 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9560 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9565 /* Later filters should act on the payload of the Geneve frame,
9566 * update all of the header pointers. Attach this code so that
9567 * it gets executed in the event that the Geneve filter matches. */
9568 s
= gen_geneve_offsets(cstate
);
9570 b1
= gen_true(cstate
);
9571 sappend(s
, b1
->stmts
);
9576 cstate
->is_encap
= 1;
9581 /* Check that this is VXLAN and the VNI is correct if
9582 * specified. Parameterized to handle both IPv4 and IPv6. */
9583 static struct block
*
9584 gen_vxlan_check(compiler_state_t
*cstate
,
9585 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9586 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9588 struct block
*b0
, *b1
;
9590 b0
= gen_portfn(cstate
, VXLAN_PORT
, IPPROTO_UDP
, Q_DST
);
9592 /* Check that the VXLAN header has the flag bits set
9594 b1
= gen_cmp(cstate
, offrel
, 8, BPF_B
, 0x08);
9599 if (vni
> 0xffffff) {
9600 bpf_error(cstate
, "VXLAN VNI %u greater than maximum %u",
9603 vni
<<= 8; /* VNI is in the upper 3 bytes */
9604 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9612 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9613 * - Verify that this actually is VXLAN with the right VNI.
9614 * - Place the IP header length (plus variable link prefix if
9615 * needed) into register A to be used later to compute
9616 * the inner packet offsets. */
9617 static struct block
*
9618 gen_vxlan4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9620 struct block
*b0
, *b1
;
9621 struct slist
*s
, *s1
;
9623 b0
= gen_vxlan_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9625 /* Load the IP header length into A. */
9626 s
= gen_loadx_iphdrlen(cstate
);
9628 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9631 /* Forcibly append these statements to the true condition
9632 * of the protocol check by creating a new block that is
9633 * always true and ANDing them. */
9634 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9643 static struct block
*
9644 gen_vxlan6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9646 struct block
*b0
, *b1
;
9647 struct slist
*s
, *s1
;
9649 b0
= gen_vxlan_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9651 /* Load the IP header length. We need to account for a
9652 * variable length link prefix if there is one. */
9653 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9655 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9659 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9663 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9667 /* Forcibly append these statements to the true condition
9668 * of the protocol check by creating a new block that is
9669 * always true and ANDing them. */
9670 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9673 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9682 /* We need to store three values based on the VXLAN header:
9683 * - The offset of the linktype.
9684 * - The offset of the end of the VXLAN header.
9685 * - The offset of the end of the encapsulated MAC header. */
9686 static struct slist
*
9687 gen_vxlan_offsets(compiler_state_t
*cstate
)
9689 struct slist
*s
, *s1
;
9691 /* Calculate the offset of the VXLAN header itself. This
9692 * includes the IP header computed previously (including any
9693 * variable link prefix) and stored in A plus the fixed size
9694 * headers (fixed link prefix, MAC length, UDP header). */
9695 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9696 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9698 /* Add the VXLAN header length to its offset and store */
9699 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9703 /* Push the link header. VXLAN packets always contain Ethernet
9705 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9707 s1
= new_stmt(cstate
, BPF_ST
);
9708 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9711 /* As the payload is an Ethernet packet, we can use the
9712 * EtherType of the payload directly as the linktype. */
9713 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9717 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9718 cstate
->off_linktype
.is_variable
= 1;
9719 cstate
->off_linktype
.constant_part
= 0;
9721 s1
= new_stmt(cstate
, BPF_ST
);
9722 s1
->s
.k
= cstate
->off_linktype
.reg
;
9725 /* Two bytes further is the end of the Ethernet header and the
9726 * start of the payload. */
9727 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9731 /* Move the result to X. */
9732 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9735 /* Store the final result of our linkpl calculation. */
9736 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9737 cstate
->off_linkpl
.is_variable
= 1;
9738 cstate
->off_linkpl
.constant_part
= 0;
9740 s1
= new_stmt(cstate
, BPF_STX
);
9741 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9749 /* Check to see if this is a VXLAN packet. */
9751 gen_vxlan(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9753 struct block
*b0
, *b1
;
9757 * Catch errors reported by us and routines below us, and return NULL
9760 if (setjmp(cstate
->top_ctx
))
9763 b0
= gen_vxlan4(cstate
, vni
, has_vni
);
9764 b1
= gen_vxlan6(cstate
, vni
, has_vni
);
9769 /* Later filters should act on the payload of the VXLAN frame,
9770 * update all of the header pointers. Attach this code so that
9771 * it gets executed in the event that the VXLAN filter matches. */
9772 s
= gen_vxlan_offsets(cstate
);
9774 b1
= gen_true(cstate
);
9775 sappend(s
, b1
->stmts
);
9780 cstate
->is_encap
= 1;
9785 /* Check that the encapsulated frame has a link layer header
9786 * for Ethernet filters. */
9787 static struct block
*
9788 gen_encap_ll_check(compiler_state_t
*cstate
)
9791 struct slist
*s
, *s1
;
9793 /* The easiest way to see if there is a link layer present
9794 * is to check if the link layer header and payload are not
9797 /* Geneve always generates pure variable offsets so we can
9798 * compare only the registers. */
9799 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9800 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9802 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9803 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9806 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9814 static struct block
*
9815 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9816 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9821 * This check is a no-op for A_MSGTYPE so long as the only incoming
9822 * code path is from gen_atmmulti_abbrev(), which makes the same
9823 * check first; also for A_PROTOTYPE so long as the only incoming code
9824 * paths are from gen_atmtype_abbrev(), which makes the same check
9825 * first, or from gen_llc_internal() or gen_linktype(), which restrict
9828 assert_atm(cstate
, atmkw(atmfield
));
9833 if (jvalue
> UINT8_MAX
)
9834 bpf_error(cstate
, "VPI value %u > %u", jvalue
, UINT8_MAX
);
9835 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9836 0xffffffffU
, jtype
, reverse
, jvalue
);
9840 if (jvalue
> UINT16_MAX
)
9841 bpf_error(cstate
, "VCI value %u > %u", jvalue
, UINT16_MAX
);
9842 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9843 0xffffffffU
, jtype
, reverse
, jvalue
);
9847 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9848 0x0fU
, jtype
, reverse
, jvalue
);
9852 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9853 0xffffffffU
, jtype
, reverse
, jvalue
);
9862 static struct block
*
9863 gen_atmtype_metac(compiler_state_t
*cstate
)
9865 struct block
*b0
, *b1
;
9867 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9868 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9873 static struct block
*
9874 gen_atmtype_sc(compiler_state_t
*cstate
)
9876 struct block
*b0
, *b1
;
9878 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9879 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9884 static struct block
*
9885 gen_atmtype_llc(compiler_state_t
*cstate
)
9889 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9890 cstate
->linktype
= cstate
->prevlinktype
;
9895 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9896 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9899 * Catch errors reported by us and routines below us, and return NULL
9902 if (setjmp(cstate
->top_ctx
))
9905 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9910 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9912 struct block
*b0
, *b1
;
9915 * Catch errors reported by us and routines below us, and return NULL
9918 if (setjmp(cstate
->top_ctx
))
9921 assert_atm(cstate
, atmkw(type
));
9926 /* Get all packets in Meta signalling Circuit */
9927 b1
= gen_atmtype_metac(cstate
);
9931 /* Get all packets in Broadcast Circuit*/
9932 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9933 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9938 /* Get all cells in Segment OAM F4 circuit*/
9939 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9940 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9945 /* Get all cells in End-to-End OAM F4 Circuit*/
9946 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9947 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9952 /* Get all packets in connection Signalling Circuit */
9953 b1
= gen_atmtype_sc(cstate
);
9957 /* Get all packets in ILMI Circuit */
9958 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9959 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9964 /* Get all LANE packets */
9965 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9968 * Arrange that all subsequent tests assume LANE
9969 * rather than LLC-encapsulated packets, and set
9970 * the offsets appropriately for LANE-encapsulated
9973 * We assume LANE means Ethernet, not Token Ring.
9975 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9976 cstate
->off_payload
+ 2, /* Ethernet header */
9978 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9979 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9980 cstate
->off_nl
= 0; /* Ethernet II */
9981 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9991 * Filtering for MTP2 messages based on li value
9992 * FISU, length is null
9993 * LSSU, length is 1 or 2
9994 * MSU, length is 3 or more
9995 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9998 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
10000 struct block
*b0
, *b1
;
10003 * Catch errors reported by us and routines below us, and return NULL
10006 if (setjmp(cstate
->top_ctx
))
10012 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10013 (cstate
->linktype
!= DLT_ERF
) &&
10014 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10015 bpf_error(cstate
, "'fisu' supported only on MTP2");
10016 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10017 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10018 0x3fU
, BPF_JEQ
, 0, 0U);
10022 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10023 (cstate
->linktype
!= DLT_ERF
) &&
10024 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10025 bpf_error(cstate
, "'lssu' supported only on MTP2");
10026 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10027 0x3fU
, BPF_JGT
, 1, 2U);
10028 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10029 0x3fU
, BPF_JGT
, 0, 0U);
10034 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10035 (cstate
->linktype
!= DLT_ERF
) &&
10036 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10037 bpf_error(cstate
, "'msu' supported only on MTP2");
10038 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
10039 0x3fU
, BPF_JGT
, 0, 2U);
10043 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10044 (cstate
->linktype
!= DLT_ERF
) &&
10045 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10046 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
10047 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10048 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10049 0xff80U
, BPF_JEQ
, 0, 0U);
10053 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10054 (cstate
->linktype
!= DLT_ERF
) &&
10055 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10056 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
10057 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10058 0xff80U
, BPF_JGT
, 1, 0x0100U
);
10059 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10060 0xff80U
, BPF_JGT
, 0, 0U);
10065 if ( (cstate
->linktype
!= DLT_MTP2
) &&
10066 (cstate
->linktype
!= DLT_ERF
) &&
10067 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
10068 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
10069 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
10070 0xff80U
, BPF_JGT
, 0, 0x0100U
);
10080 * These maximum valid values are all-ones, so they double as the bitmasks
10081 * before any bitwise shifting.
10083 #define MTP2_SIO_MAXVAL UINT8_MAX
10084 #define MTP3_PC_MAXVAL 0x3fffU
10085 #define MTP3_SLS_MAXVAL 0xfU
10087 static struct block
*
10088 gen_mtp3field_code_internal(compiler_state_t
*cstate
, int mtp3field
,
10089 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10097 newoff_sio
= cstate
->off_sio
;
10098 newoff_opc
= cstate
->off_opc
;
10099 newoff_dpc
= cstate
->off_dpc
;
10100 newoff_sls
= cstate
->off_sls
;
10101 switch (mtp3field
) {
10104 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10106 * SIO is the simplest field: the size is one byte and the offset is a
10107 * multiple of bytes, so the only detail to get right is the value of
10108 * the [right-to-left] field offset.
10111 newoff_sio
+= 3; /* offset for MTP2_HSL */
10115 if (cstate
->off_sio
== OFFSET_NOT_SET
)
10116 bpf_error(cstate
, "'sio' supported only on SS7");
10117 if(jvalue
> MTP2_SIO_MAXVAL
)
10118 bpf_error(cstate
, "sio value %u too big; max value = %u",
10119 jvalue
, MTP2_SIO_MAXVAL
);
10120 // Here the bitmask means "do not apply a bitmask".
10121 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, UINT32_MAX
,
10122 jtype
, reverse
, jvalue
);
10126 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10128 * SLS, OPC and DPC are more complicated: none of these is sized in a
10129 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10130 * diagrams are meant to be read right-to-left. This means in the
10131 * diagrams within individual fields and concatenations thereof
10132 * bitwise shifts and masks can be noted in the common left-to-right
10133 * manner until each final value is ready to be byte-swapped and
10134 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10135 * similar problem in a similar way.
10137 * Offsets of fields within the packet header always have the
10138 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10139 * DLTs the offset does not include the F (Flag) field at the
10140 * beginning of each message.
10142 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10143 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10144 * be tested entirely using a single BPF_W comparison. In this case
10145 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10146 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10147 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10148 * correlates with the [RTL] packet diagram until the byte-swapping is
10151 * The code below uses this approach for OPC, which spans 3 bytes.
10152 * DPC and SLS use shorter loads, SLS also uses a different offset.
10159 if (cstate
->off_opc
== OFFSET_NOT_SET
)
10160 bpf_error(cstate
, "'opc' supported only on SS7");
10161 if (jvalue
> MTP3_PC_MAXVAL
)
10162 bpf_error(cstate
, "opc value %u too big; max value = %u",
10163 jvalue
, MTP3_PC_MAXVAL
);
10164 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
,
10165 SWAPLONG(MTP3_PC_MAXVAL
<< 14), jtype
, reverse
,
10166 SWAPLONG(jvalue
<< 14));
10174 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
10175 bpf_error(cstate
, "'dpc' supported only on SS7");
10176 if (jvalue
> MTP3_PC_MAXVAL
)
10177 bpf_error(cstate
, "dpc value %u too big; max value = %u",
10178 jvalue
, MTP3_PC_MAXVAL
);
10179 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_H
,
10180 SWAPSHORT(MTP3_PC_MAXVAL
), jtype
, reverse
,
10181 SWAPSHORT(jvalue
));
10189 if (cstate
->off_sls
== OFFSET_NOT_SET
)
10190 bpf_error(cstate
, "'sls' supported only on SS7");
10191 if (jvalue
> MTP3_SLS_MAXVAL
)
10192 bpf_error(cstate
, "sls value %u too big; max value = %u",
10193 jvalue
, MTP3_SLS_MAXVAL
);
10194 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
,
10195 MTP3_SLS_MAXVAL
<< 4, jtype
, reverse
,
10206 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
10207 bpf_u_int32 jvalue
, int jtype
, int reverse
)
10210 * Catch errors reported by us and routines below us, and return NULL
10213 if (setjmp(cstate
->top_ctx
))
10216 return gen_mtp3field_code_internal(cstate
, mtp3field
, jvalue
, jtype
,
10220 static struct block
*
10221 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
10226 * Q.2931 signalling protocol messages for handling virtual circuits
10227 * establishment and teardown
10232 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
10235 case A_CALLPROCEED
:
10236 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
10240 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
10244 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
10248 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
10251 case A_RELEASE_DONE
:
10252 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
10262 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
10264 struct block
*b0
, *b1
;
10267 * Catch errors reported by us and routines below us, and return NULL
10270 if (setjmp(cstate
->top_ctx
))
10273 assert_atm(cstate
, atmkw(type
));
10279 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10280 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10282 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10288 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10289 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10291 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10297 * Get Q.2931 signalling messages for switched
10298 * virtual connection
10300 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10301 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10303 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10305 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10307 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10309 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10311 b0
= gen_atmtype_sc(cstate
);
10315 case A_METACONNECT
:
10316 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10317 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10319 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10321 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10323 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10325 b0
= gen_atmtype_metac(cstate
);