xref: /aosp_15_r20/external/libpcap/gencode.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3*8b26181fSAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker  *
5*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker  * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker  * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker  * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker  * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker  * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker  * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker  * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker  * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker  * written permission.
17*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker  */
21*8b26181fSAndroid Build Coastguard Worker 
22*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
23*8b26181fSAndroid Build Coastguard Worker #include <config.h>
24*8b26181fSAndroid Build Coastguard Worker #endif
25*8b26181fSAndroid Build Coastguard Worker 
26*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
27*8b26181fSAndroid Build Coastguard Worker   #include <ws2tcpip.h>
28*8b26181fSAndroid Build Coastguard Worker #else
29*8b26181fSAndroid Build Coastguard Worker   #include <sys/socket.h>
30*8b26181fSAndroid Build Coastguard Worker 
31*8b26181fSAndroid Build Coastguard Worker   #ifdef __NetBSD__
32*8b26181fSAndroid Build Coastguard Worker     #include <sys/param.h>
33*8b26181fSAndroid Build Coastguard Worker   #endif
34*8b26181fSAndroid Build Coastguard Worker 
35*8b26181fSAndroid Build Coastguard Worker   #include <netinet/in.h>
36*8b26181fSAndroid Build Coastguard Worker   #include <arpa/inet.h>
37*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
38*8b26181fSAndroid Build Coastguard Worker 
39*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
40*8b26181fSAndroid Build Coastguard Worker #include <string.h>
41*8b26181fSAndroid Build Coastguard Worker #include <memory.h>
42*8b26181fSAndroid Build Coastguard Worker #include <setjmp.h>
43*8b26181fSAndroid Build Coastguard Worker #include <stdarg.h>
44*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
45*8b26181fSAndroid Build Coastguard Worker 
46*8b26181fSAndroid Build Coastguard Worker #ifdef MSDOS
47*8b26181fSAndroid Build Coastguard Worker #include "pcap-dos.h"
48*8b26181fSAndroid Build Coastguard Worker #endif
49*8b26181fSAndroid Build Coastguard Worker 
50*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
51*8b26181fSAndroid Build Coastguard Worker 
52*8b26181fSAndroid Build Coastguard Worker #include "extract.h"
53*8b26181fSAndroid Build Coastguard Worker 
54*8b26181fSAndroid Build Coastguard Worker #include "ethertype.h"
55*8b26181fSAndroid Build Coastguard Worker #include "nlpid.h"
56*8b26181fSAndroid Build Coastguard Worker #include "llc.h"
57*8b26181fSAndroid Build Coastguard Worker #include "gencode.h"
58*8b26181fSAndroid Build Coastguard Worker #include "ieee80211.h"
59*8b26181fSAndroid Build Coastguard Worker #include "atmuni31.h"
60*8b26181fSAndroid Build Coastguard Worker #include "sunatmpos.h"
61*8b26181fSAndroid Build Coastguard Worker #include "pflog.h"
62*8b26181fSAndroid Build Coastguard Worker #include "ppp.h"
63*8b26181fSAndroid Build Coastguard Worker #include "pcap/sll.h"
64*8b26181fSAndroid Build Coastguard Worker #include "pcap/ipnet.h"
65*8b26181fSAndroid Build Coastguard Worker #include "arcnet.h"
66*8b26181fSAndroid Build Coastguard Worker #include "diag-control.h"
67*8b26181fSAndroid Build Coastguard Worker 
68*8b26181fSAndroid Build Coastguard Worker #include "scanner.h"
69*8b26181fSAndroid Build Coastguard Worker 
70*8b26181fSAndroid Build Coastguard Worker #if defined(linux)
71*8b26181fSAndroid Build Coastguard Worker #include <linux/types.h>
72*8b26181fSAndroid Build Coastguard Worker #include <linux/if_packet.h>
73*8b26181fSAndroid Build Coastguard Worker #include <linux/filter.h>
74*8b26181fSAndroid Build Coastguard Worker #endif
75*8b26181fSAndroid Build Coastguard Worker 
76*8b26181fSAndroid Build Coastguard Worker #ifndef offsetof
77*8b26181fSAndroid Build Coastguard Worker #define offsetof(s, e) ((size_t)&((s *)0)->e)
78*8b26181fSAndroid Build Coastguard Worker #endif
79*8b26181fSAndroid Build Coastguard Worker 
80*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
81*8b26181fSAndroid Build Coastguard Worker   #ifdef INET6
82*8b26181fSAndroid Build Coastguard Worker     #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
83*8b26181fSAndroid Build Coastguard Worker /* IPv6 address */
84*8b26181fSAndroid Build Coastguard Worker struct in6_addr
85*8b26181fSAndroid Build Coastguard Worker   {
86*8b26181fSAndroid Build Coastguard Worker     union
87*8b26181fSAndroid Build Coastguard Worker       {
88*8b26181fSAndroid Build Coastguard Worker 	uint8_t		u6_addr8[16];
89*8b26181fSAndroid Build Coastguard Worker 	uint16_t	u6_addr16[8];
90*8b26181fSAndroid Build Coastguard Worker 	uint32_t	u6_addr32[4];
91*8b26181fSAndroid Build Coastguard Worker       } in6_u;
92*8b26181fSAndroid Build Coastguard Worker #define s6_addr			in6_u.u6_addr8
93*8b26181fSAndroid Build Coastguard Worker #define s6_addr16		in6_u.u6_addr16
94*8b26181fSAndroid Build Coastguard Worker #define s6_addr32		in6_u.u6_addr32
95*8b26181fSAndroid Build Coastguard Worker #define s6_addr64		in6_u.u6_addr64
96*8b26181fSAndroid Build Coastguard Worker   };
97*8b26181fSAndroid Build Coastguard Worker 
98*8b26181fSAndroid Build Coastguard Worker typedef unsigned short	sa_family_t;
99*8b26181fSAndroid Build Coastguard Worker 
100*8b26181fSAndroid Build Coastguard Worker #define	__SOCKADDR_COMMON(sa_prefix) \
101*8b26181fSAndroid Build Coastguard Worker   sa_family_t sa_prefix##family
102*8b26181fSAndroid Build Coastguard Worker 
103*8b26181fSAndroid Build Coastguard Worker /* Ditto, for IPv6.  */
104*8b26181fSAndroid Build Coastguard Worker struct sockaddr_in6
105*8b26181fSAndroid Build Coastguard Worker   {
106*8b26181fSAndroid Build Coastguard Worker     __SOCKADDR_COMMON (sin6_);
107*8b26181fSAndroid Build Coastguard Worker     uint16_t sin6_port;		/* Transport layer port # */
108*8b26181fSAndroid Build Coastguard Worker     uint32_t sin6_flowinfo;	/* IPv6 flow information */
109*8b26181fSAndroid Build Coastguard Worker     struct in6_addr sin6_addr;	/* IPv6 address */
110*8b26181fSAndroid Build Coastguard Worker   };
111*8b26181fSAndroid Build Coastguard Worker 
112*8b26181fSAndroid Build Coastguard Worker       #ifndef EAI_ADDRFAMILY
113*8b26181fSAndroid Build Coastguard Worker struct addrinfo {
114*8b26181fSAndroid Build Coastguard Worker 	int	ai_flags;	/* AI_PASSIVE, AI_CANONNAME */
115*8b26181fSAndroid Build Coastguard Worker 	int	ai_family;	/* PF_xxx */
116*8b26181fSAndroid Build Coastguard Worker 	int	ai_socktype;	/* SOCK_xxx */
117*8b26181fSAndroid Build Coastguard Worker 	int	ai_protocol;	/* 0 or IPPROTO_xxx for IPv4 and IPv6 */
118*8b26181fSAndroid Build Coastguard Worker 	size_t	ai_addrlen;	/* length of ai_addr */
119*8b26181fSAndroid Build Coastguard Worker 	char	*ai_canonname;	/* canonical name for hostname */
120*8b26181fSAndroid Build Coastguard Worker 	struct sockaddr *ai_addr;	/* binary address */
121*8b26181fSAndroid Build Coastguard Worker 	struct addrinfo *ai_next;	/* next structure in linked list */
122*8b26181fSAndroid Build Coastguard Worker };
123*8b26181fSAndroid Build Coastguard Worker       #endif /* EAI_ADDRFAMILY */
124*8b26181fSAndroid Build Coastguard Worker     #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
125*8b26181fSAndroid Build Coastguard Worker   #endif /* INET6 */
126*8b26181fSAndroid Build Coastguard Worker #else /* _WIN32 */
127*8b26181fSAndroid Build Coastguard Worker   #include <netdb.h>	/* for "struct addrinfo" */
128*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
129*8b26181fSAndroid Build Coastguard Worker #include <pcap/namedb.h>
130*8b26181fSAndroid Build Coastguard Worker 
131*8b26181fSAndroid Build Coastguard Worker #include "nametoaddr.h"
132*8b26181fSAndroid Build Coastguard Worker 
133*8b26181fSAndroid Build Coastguard Worker #define ETHERMTU	1500
134*8b26181fSAndroid Build Coastguard Worker 
135*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_HOPOPTS
136*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_HOPOPTS 0
137*8b26181fSAndroid Build Coastguard Worker #endif
138*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_ROUTING
139*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_ROUTING 43
140*8b26181fSAndroid Build Coastguard Worker #endif
141*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_FRAGMENT
142*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_FRAGMENT 44
143*8b26181fSAndroid Build Coastguard Worker #endif
144*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_DSTOPTS
145*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_DSTOPTS 60
146*8b26181fSAndroid Build Coastguard Worker #endif
147*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_SCTP
148*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_SCTP 132
149*8b26181fSAndroid Build Coastguard Worker #endif
150*8b26181fSAndroid Build Coastguard Worker 
151*8b26181fSAndroid Build Coastguard Worker #define GENEVE_PORT 6081
152*8b26181fSAndroid Build Coastguard Worker 
153*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
154*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
155*8b26181fSAndroid Build Coastguard Worker #endif
156*8b26181fSAndroid Build Coastguard Worker 
157*8b26181fSAndroid Build Coastguard Worker #define JMP(c) ((c)|BPF_JMP|BPF_K)
158*8b26181fSAndroid Build Coastguard Worker 
159*8b26181fSAndroid Build Coastguard Worker /*
160*8b26181fSAndroid Build Coastguard Worker  * "Push" the current value of the link-layer header type and link-layer
161*8b26181fSAndroid Build Coastguard Worker  * header offset onto a "stack", and set a new value.  (It's not a
162*8b26181fSAndroid Build Coastguard Worker  * full-blown stack; we keep only the top two items.)
163*8b26181fSAndroid Build Coastguard Worker  */
164*8b26181fSAndroid Build Coastguard Worker #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
165*8b26181fSAndroid Build Coastguard Worker { \
166*8b26181fSAndroid Build Coastguard Worker 	(cs)->prevlinktype = (cs)->linktype; \
167*8b26181fSAndroid Build Coastguard Worker 	(cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
168*8b26181fSAndroid Build Coastguard Worker 	(cs)->linktype = (new_linktype); \
169*8b26181fSAndroid Build Coastguard Worker 	(cs)->off_linkhdr.is_variable = (new_is_variable); \
170*8b26181fSAndroid Build Coastguard Worker 	(cs)->off_linkhdr.constant_part = (new_constant_part); \
171*8b26181fSAndroid Build Coastguard Worker 	(cs)->off_linkhdr.reg = (new_reg); \
172*8b26181fSAndroid Build Coastguard Worker 	(cs)->is_geneve = 0; \
173*8b26181fSAndroid Build Coastguard Worker }
174*8b26181fSAndroid Build Coastguard Worker 
175*8b26181fSAndroid Build Coastguard Worker /*
176*8b26181fSAndroid Build Coastguard Worker  * Offset "not set" value.
177*8b26181fSAndroid Build Coastguard Worker  */
178*8b26181fSAndroid Build Coastguard Worker #define OFFSET_NOT_SET	0xffffffffU
179*8b26181fSAndroid Build Coastguard Worker 
180*8b26181fSAndroid Build Coastguard Worker /*
181*8b26181fSAndroid Build Coastguard Worker  * Absolute offsets, which are offsets from the beginning of the raw
182*8b26181fSAndroid Build Coastguard Worker  * packet data, are, in the general case, the sum of a variable value
183*8b26181fSAndroid Build Coastguard Worker  * and a constant value; the variable value may be absent, in which
184*8b26181fSAndroid Build Coastguard Worker  * case the offset is only the constant value, and the constant value
185*8b26181fSAndroid Build Coastguard Worker  * may be zero, in which case the offset is only the variable value.
186*8b26181fSAndroid Build Coastguard Worker  *
187*8b26181fSAndroid Build Coastguard Worker  * bpf_abs_offset is a structure containing all that information:
188*8b26181fSAndroid Build Coastguard Worker  *
189*8b26181fSAndroid Build Coastguard Worker  *   is_variable is 1 if there's a variable part.
190*8b26181fSAndroid Build Coastguard Worker  *
191*8b26181fSAndroid Build Coastguard Worker  *   constant_part is the constant part of the value, possibly zero;
192*8b26181fSAndroid Build Coastguard Worker  *
193*8b26181fSAndroid Build Coastguard Worker  *   if is_variable is 1, reg is the register number for a register
194*8b26181fSAndroid Build Coastguard Worker  *   containing the variable value if the register has been assigned,
195*8b26181fSAndroid Build Coastguard Worker  *   and -1 otherwise.
196*8b26181fSAndroid Build Coastguard Worker  */
197*8b26181fSAndroid Build Coastguard Worker typedef struct {
198*8b26181fSAndroid Build Coastguard Worker 	int	is_variable;
199*8b26181fSAndroid Build Coastguard Worker 	u_int	constant_part;
200*8b26181fSAndroid Build Coastguard Worker 	int	reg;
201*8b26181fSAndroid Build Coastguard Worker } bpf_abs_offset;
202*8b26181fSAndroid Build Coastguard Worker 
203*8b26181fSAndroid Build Coastguard Worker /*
204*8b26181fSAndroid Build Coastguard Worker  * Value passed to gen_load_a() to indicate what the offset argument
205*8b26181fSAndroid Build Coastguard Worker  * is relative to the beginning of.
206*8b26181fSAndroid Build Coastguard Worker  */
207*8b26181fSAndroid Build Coastguard Worker enum e_offrel {
208*8b26181fSAndroid Build Coastguard Worker 	OR_PACKET,		/* full packet data */
209*8b26181fSAndroid Build Coastguard Worker 	OR_LINKHDR,		/* link-layer header */
210*8b26181fSAndroid Build Coastguard Worker 	OR_PREVLINKHDR,		/* previous link-layer header */
211*8b26181fSAndroid Build Coastguard Worker 	OR_LLC,			/* 802.2 LLC header */
212*8b26181fSAndroid Build Coastguard Worker 	OR_PREVMPLSHDR,		/* previous MPLS header */
213*8b26181fSAndroid Build Coastguard Worker 	OR_LINKTYPE,		/* link-layer type */
214*8b26181fSAndroid Build Coastguard Worker 	OR_LINKPL,		/* link-layer payload */
215*8b26181fSAndroid Build Coastguard Worker 	OR_LINKPL_NOSNAP,	/* link-layer payload, with no SNAP header at the link layer */
216*8b26181fSAndroid Build Coastguard Worker 	OR_TRAN_IPV4,		/* transport-layer header, with IPv4 network layer */
217*8b26181fSAndroid Build Coastguard Worker 	OR_TRAN_IPV6		/* transport-layer header, with IPv6 network layer */
218*8b26181fSAndroid Build Coastguard Worker };
219*8b26181fSAndroid Build Coastguard Worker 
220*8b26181fSAndroid Build Coastguard Worker /*
221*8b26181fSAndroid Build Coastguard Worker  * We divy out chunks of memory rather than call malloc each time so
222*8b26181fSAndroid Build Coastguard Worker  * we don't have to worry about leaking memory.  It's probably
223*8b26181fSAndroid Build Coastguard Worker  * not a big deal if all this memory was wasted but if this ever
224*8b26181fSAndroid Build Coastguard Worker  * goes into a library that would probably not be a good idea.
225*8b26181fSAndroid Build Coastguard Worker  *
226*8b26181fSAndroid Build Coastguard Worker  * XXX - this *is* in a library....
227*8b26181fSAndroid Build Coastguard Worker  */
228*8b26181fSAndroid Build Coastguard Worker #define NCHUNKS 16
229*8b26181fSAndroid Build Coastguard Worker #define CHUNK0SIZE 1024
230*8b26181fSAndroid Build Coastguard Worker struct chunk {
231*8b26181fSAndroid Build Coastguard Worker 	size_t n_left;
232*8b26181fSAndroid Build Coastguard Worker 	void *m;
233*8b26181fSAndroid Build Coastguard Worker };
234*8b26181fSAndroid Build Coastguard Worker 
235*8b26181fSAndroid Build Coastguard Worker /* Code generator state */
236*8b26181fSAndroid Build Coastguard Worker 
237*8b26181fSAndroid Build Coastguard Worker struct _compiler_state {
238*8b26181fSAndroid Build Coastguard Worker 	jmp_buf top_ctx;
239*8b26181fSAndroid Build Coastguard Worker 	pcap_t *bpf_pcap;
240*8b26181fSAndroid Build Coastguard Worker 	int error_set;
241*8b26181fSAndroid Build Coastguard Worker 
242*8b26181fSAndroid Build Coastguard Worker 	struct icode ic;
243*8b26181fSAndroid Build Coastguard Worker 
244*8b26181fSAndroid Build Coastguard Worker 	int snaplen;
245*8b26181fSAndroid Build Coastguard Worker 
246*8b26181fSAndroid Build Coastguard Worker 	int linktype;
247*8b26181fSAndroid Build Coastguard Worker 	int prevlinktype;
248*8b26181fSAndroid Build Coastguard Worker 	int outermostlinktype;
249*8b26181fSAndroid Build Coastguard Worker 
250*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 netmask;
251*8b26181fSAndroid Build Coastguard Worker 	int no_optimize;
252*8b26181fSAndroid Build Coastguard Worker 
253*8b26181fSAndroid Build Coastguard Worker 	/* Hack for handling VLAN and MPLS stacks. */
254*8b26181fSAndroid Build Coastguard Worker 	u_int label_stack_depth;
255*8b26181fSAndroid Build Coastguard Worker 	u_int vlan_stack_depth;
256*8b26181fSAndroid Build Coastguard Worker 
257*8b26181fSAndroid Build Coastguard Worker 	/* XXX */
258*8b26181fSAndroid Build Coastguard Worker 	u_int pcap_fddipad;
259*8b26181fSAndroid Build Coastguard Worker 
260*8b26181fSAndroid Build Coastguard Worker 	/*
261*8b26181fSAndroid Build Coastguard Worker 	 * As errors are handled by a longjmp, anything allocated must
262*8b26181fSAndroid Build Coastguard Worker 	 * be freed in the longjmp handler, so it must be reachable
263*8b26181fSAndroid Build Coastguard Worker 	 * from that handler.
264*8b26181fSAndroid Build Coastguard Worker 	 *
265*8b26181fSAndroid Build Coastguard Worker 	 * One thing that's allocated is the result of pcap_nametoaddrinfo();
266*8b26181fSAndroid Build Coastguard Worker 	 * it must be freed with freeaddrinfo().  This variable points to
267*8b26181fSAndroid Build Coastguard Worker 	 * any addrinfo structure that would need to be freed.
268*8b26181fSAndroid Build Coastguard Worker 	 */
269*8b26181fSAndroid Build Coastguard Worker 	struct addrinfo *ai;
270*8b26181fSAndroid Build Coastguard Worker 
271*8b26181fSAndroid Build Coastguard Worker 	/*
272*8b26181fSAndroid Build Coastguard Worker 	 * Another thing that's allocated is the result of pcap_ether_aton();
273*8b26181fSAndroid Build Coastguard Worker 	 * it must be freed with free().  This variable points to any
274*8b26181fSAndroid Build Coastguard Worker 	 * address that would need to be freed.
275*8b26181fSAndroid Build Coastguard Worker 	 */
276*8b26181fSAndroid Build Coastguard Worker 	u_char *e;
277*8b26181fSAndroid Build Coastguard Worker 
278*8b26181fSAndroid Build Coastguard Worker 	/*
279*8b26181fSAndroid Build Coastguard Worker 	 * Various code constructs need to know the layout of the packet.
280*8b26181fSAndroid Build Coastguard Worker 	 * These values give the necessary offsets from the beginning
281*8b26181fSAndroid Build Coastguard Worker 	 * of the packet data.
282*8b26181fSAndroid Build Coastguard Worker 	 */
283*8b26181fSAndroid Build Coastguard Worker 
284*8b26181fSAndroid Build Coastguard Worker 	/*
285*8b26181fSAndroid Build Coastguard Worker 	 * Absolute offset of the beginning of the link-layer header.
286*8b26181fSAndroid Build Coastguard Worker 	 */
287*8b26181fSAndroid Build Coastguard Worker 	bpf_abs_offset off_linkhdr;
288*8b26181fSAndroid Build Coastguard Worker 
289*8b26181fSAndroid Build Coastguard Worker 	/*
290*8b26181fSAndroid Build Coastguard Worker 	 * If we're checking a link-layer header for a packet encapsulated
291*8b26181fSAndroid Build Coastguard Worker 	 * in another protocol layer, this is the equivalent information
292*8b26181fSAndroid Build Coastguard Worker 	 * for the previous layers' link-layer header from the beginning
293*8b26181fSAndroid Build Coastguard Worker 	 * of the raw packet data.
294*8b26181fSAndroid Build Coastguard Worker 	 */
295*8b26181fSAndroid Build Coastguard Worker 	bpf_abs_offset off_prevlinkhdr;
296*8b26181fSAndroid Build Coastguard Worker 
297*8b26181fSAndroid Build Coastguard Worker 	/*
298*8b26181fSAndroid Build Coastguard Worker 	 * This is the equivalent information for the outermost layers'
299*8b26181fSAndroid Build Coastguard Worker 	 * link-layer header.
300*8b26181fSAndroid Build Coastguard Worker 	 */
301*8b26181fSAndroid Build Coastguard Worker 	bpf_abs_offset off_outermostlinkhdr;
302*8b26181fSAndroid Build Coastguard Worker 
303*8b26181fSAndroid Build Coastguard Worker 	/*
304*8b26181fSAndroid Build Coastguard Worker 	 * Absolute offset of the beginning of the link-layer payload.
305*8b26181fSAndroid Build Coastguard Worker 	 */
306*8b26181fSAndroid Build Coastguard Worker 	bpf_abs_offset off_linkpl;
307*8b26181fSAndroid Build Coastguard Worker 
308*8b26181fSAndroid Build Coastguard Worker 	/*
309*8b26181fSAndroid Build Coastguard Worker 	 * "off_linktype" is the offset to information in the link-layer
310*8b26181fSAndroid Build Coastguard Worker 	 * header giving the packet type. This is an absolute offset
311*8b26181fSAndroid Build Coastguard Worker 	 * from the beginning of the packet.
312*8b26181fSAndroid Build Coastguard Worker 	 *
313*8b26181fSAndroid Build Coastguard Worker 	 * For Ethernet, it's the offset of the Ethernet type field; this
314*8b26181fSAndroid Build Coastguard Worker 	 * means that it must have a value that skips VLAN tags.
315*8b26181fSAndroid Build Coastguard Worker 	 *
316*8b26181fSAndroid Build Coastguard Worker 	 * For link-layer types that always use 802.2 headers, it's the
317*8b26181fSAndroid Build Coastguard Worker 	 * offset of the LLC header; this means that it must have a value
318*8b26181fSAndroid Build Coastguard Worker 	 * that skips VLAN tags.
319*8b26181fSAndroid Build Coastguard Worker 	 *
320*8b26181fSAndroid Build Coastguard Worker 	 * For PPP, it's the offset of the PPP type field.
321*8b26181fSAndroid Build Coastguard Worker 	 *
322*8b26181fSAndroid Build Coastguard Worker 	 * For Cisco HDLC, it's the offset of the CHDLC type field.
323*8b26181fSAndroid Build Coastguard Worker 	 *
324*8b26181fSAndroid Build Coastguard Worker 	 * For BSD loopback, it's the offset of the AF_ value.
325*8b26181fSAndroid Build Coastguard Worker 	 *
326*8b26181fSAndroid Build Coastguard Worker 	 * For Linux cooked sockets, it's the offset of the type field.
327*8b26181fSAndroid Build Coastguard Worker 	 *
328*8b26181fSAndroid Build Coastguard Worker 	 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
329*8b26181fSAndroid Build Coastguard Worker 	 * encapsulation, in which case, IP is assumed.
330*8b26181fSAndroid Build Coastguard Worker 	 */
331*8b26181fSAndroid Build Coastguard Worker 	bpf_abs_offset off_linktype;
332*8b26181fSAndroid Build Coastguard Worker 
333*8b26181fSAndroid Build Coastguard Worker 	/*
334*8b26181fSAndroid Build Coastguard Worker 	 * TRUE if the link layer includes an ATM pseudo-header.
335*8b26181fSAndroid Build Coastguard Worker 	 */
336*8b26181fSAndroid Build Coastguard Worker 	int is_atm;
337*8b26181fSAndroid Build Coastguard Worker 
338*8b26181fSAndroid Build Coastguard Worker 	/*
339*8b26181fSAndroid Build Coastguard Worker 	 * TRUE if "geneve" appeared in the filter; it causes us to
340*8b26181fSAndroid Build Coastguard Worker 	 * generate code that checks for a Geneve header and assume
341*8b26181fSAndroid Build Coastguard Worker 	 * that later filters apply to the encapsulated payload.
342*8b26181fSAndroid Build Coastguard Worker 	 */
343*8b26181fSAndroid Build Coastguard Worker 	int is_geneve;
344*8b26181fSAndroid Build Coastguard Worker 
345*8b26181fSAndroid Build Coastguard Worker 	/*
346*8b26181fSAndroid Build Coastguard Worker 	 * TRUE if we need variable length part of VLAN offset
347*8b26181fSAndroid Build Coastguard Worker 	 */
348*8b26181fSAndroid Build Coastguard Worker 	int is_vlan_vloffset;
349*8b26181fSAndroid Build Coastguard Worker 
350*8b26181fSAndroid Build Coastguard Worker 	/*
351*8b26181fSAndroid Build Coastguard Worker 	 * These are offsets for the ATM pseudo-header.
352*8b26181fSAndroid Build Coastguard Worker 	 */
353*8b26181fSAndroid Build Coastguard Worker 	u_int off_vpi;
354*8b26181fSAndroid Build Coastguard Worker 	u_int off_vci;
355*8b26181fSAndroid Build Coastguard Worker 	u_int off_proto;
356*8b26181fSAndroid Build Coastguard Worker 
357*8b26181fSAndroid Build Coastguard Worker 	/*
358*8b26181fSAndroid Build Coastguard Worker 	 * These are offsets for the MTP2 fields.
359*8b26181fSAndroid Build Coastguard Worker 	 */
360*8b26181fSAndroid Build Coastguard Worker 	u_int off_li;
361*8b26181fSAndroid Build Coastguard Worker 	u_int off_li_hsl;
362*8b26181fSAndroid Build Coastguard Worker 
363*8b26181fSAndroid Build Coastguard Worker 	/*
364*8b26181fSAndroid Build Coastguard Worker 	 * These are offsets for the MTP3 fields.
365*8b26181fSAndroid Build Coastguard Worker 	 */
366*8b26181fSAndroid Build Coastguard Worker 	u_int off_sio;
367*8b26181fSAndroid Build Coastguard Worker 	u_int off_opc;
368*8b26181fSAndroid Build Coastguard Worker 	u_int off_dpc;
369*8b26181fSAndroid Build Coastguard Worker 	u_int off_sls;
370*8b26181fSAndroid Build Coastguard Worker 
371*8b26181fSAndroid Build Coastguard Worker 	/*
372*8b26181fSAndroid Build Coastguard Worker 	 * This is the offset of the first byte after the ATM pseudo_header,
373*8b26181fSAndroid Build Coastguard Worker 	 * or -1 if there is no ATM pseudo-header.
374*8b26181fSAndroid Build Coastguard Worker 	 */
375*8b26181fSAndroid Build Coastguard Worker 	u_int off_payload;
376*8b26181fSAndroid Build Coastguard Worker 
377*8b26181fSAndroid Build Coastguard Worker 	/*
378*8b26181fSAndroid Build Coastguard Worker 	 * These are offsets to the beginning of the network-layer header.
379*8b26181fSAndroid Build Coastguard Worker 	 * They are relative to the beginning of the link-layer payload
380*8b26181fSAndroid Build Coastguard Worker 	 * (i.e., they don't include off_linkhdr.constant_part or
381*8b26181fSAndroid Build Coastguard Worker 	 * off_linkpl.constant_part).
382*8b26181fSAndroid Build Coastguard Worker 	 *
383*8b26181fSAndroid Build Coastguard Worker 	 * If the link layer never uses 802.2 LLC:
384*8b26181fSAndroid Build Coastguard Worker 	 *
385*8b26181fSAndroid Build Coastguard Worker 	 *	"off_nl" and "off_nl_nosnap" are the same.
386*8b26181fSAndroid Build Coastguard Worker 	 *
387*8b26181fSAndroid Build Coastguard Worker 	 * If the link layer always uses 802.2 LLC:
388*8b26181fSAndroid Build Coastguard Worker 	 *
389*8b26181fSAndroid Build Coastguard Worker 	 *	"off_nl" is the offset if there's a SNAP header following
390*8b26181fSAndroid Build Coastguard Worker 	 *	the 802.2 header;
391*8b26181fSAndroid Build Coastguard Worker 	 *
392*8b26181fSAndroid Build Coastguard Worker 	 *	"off_nl_nosnap" is the offset if there's no SNAP header.
393*8b26181fSAndroid Build Coastguard Worker 	 *
394*8b26181fSAndroid Build Coastguard Worker 	 * If the link layer is Ethernet:
395*8b26181fSAndroid Build Coastguard Worker 	 *
396*8b26181fSAndroid Build Coastguard Worker 	 *	"off_nl" is the offset if the packet is an Ethernet II packet
397*8b26181fSAndroid Build Coastguard Worker 	 *	(we assume no 802.3+802.2+SNAP);
398*8b26181fSAndroid Build Coastguard Worker 	 *
399*8b26181fSAndroid Build Coastguard Worker 	 *	"off_nl_nosnap" is the offset if the packet is an 802.3 packet
400*8b26181fSAndroid Build Coastguard Worker 	 *	with an 802.2 header following it.
401*8b26181fSAndroid Build Coastguard Worker 	 */
402*8b26181fSAndroid Build Coastguard Worker 	u_int off_nl;
403*8b26181fSAndroid Build Coastguard Worker 	u_int off_nl_nosnap;
404*8b26181fSAndroid Build Coastguard Worker 
405*8b26181fSAndroid Build Coastguard Worker 	/*
406*8b26181fSAndroid Build Coastguard Worker 	 * Here we handle simple allocation of the scratch registers.
407*8b26181fSAndroid Build Coastguard Worker 	 * If too many registers are alloc'd, the allocator punts.
408*8b26181fSAndroid Build Coastguard Worker 	 */
409*8b26181fSAndroid Build Coastguard Worker 	int regused[BPF_MEMWORDS];
410*8b26181fSAndroid Build Coastguard Worker 	int curreg;
411*8b26181fSAndroid Build Coastguard Worker 
412*8b26181fSAndroid Build Coastguard Worker 	/*
413*8b26181fSAndroid Build Coastguard Worker 	 * Memory chunks.
414*8b26181fSAndroid Build Coastguard Worker 	 */
415*8b26181fSAndroid Build Coastguard Worker 	struct chunk chunks[NCHUNKS];
416*8b26181fSAndroid Build Coastguard Worker 	int cur_chunk;
417*8b26181fSAndroid Build Coastguard Worker };
418*8b26181fSAndroid Build Coastguard Worker 
419*8b26181fSAndroid Build Coastguard Worker /*
420*8b26181fSAndroid Build Coastguard Worker  * For use by routines outside this file.
421*8b26181fSAndroid Build Coastguard Worker  */
422*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
423*8b26181fSAndroid Build Coastguard Worker void
bpf_set_error(compiler_state_t * cstate,const char * fmt,...)424*8b26181fSAndroid Build Coastguard Worker bpf_set_error(compiler_state_t *cstate, const char *fmt, ...)
425*8b26181fSAndroid Build Coastguard Worker {
426*8b26181fSAndroid Build Coastguard Worker 	va_list ap;
427*8b26181fSAndroid Build Coastguard Worker 
428*8b26181fSAndroid Build Coastguard Worker 	/*
429*8b26181fSAndroid Build Coastguard Worker 	 * If we've already set an error, don't override it.
430*8b26181fSAndroid Build Coastguard Worker 	 * The lexical analyzer reports some errors by setting
431*8b26181fSAndroid Build Coastguard Worker 	 * the error and then returning a LEX_ERROR token, which
432*8b26181fSAndroid Build Coastguard Worker 	 * is not recognized by any grammar rule, and thus forces
433*8b26181fSAndroid Build Coastguard Worker 	 * the parse to stop.  We don't want the error reported
434*8b26181fSAndroid Build Coastguard Worker 	 * by the lexical analyzer to be overwritten by the syntax
435*8b26181fSAndroid Build Coastguard Worker 	 * error.
436*8b26181fSAndroid Build Coastguard Worker 	 */
437*8b26181fSAndroid Build Coastguard Worker 	if (!cstate->error_set) {
438*8b26181fSAndroid Build Coastguard Worker 		va_start(ap, fmt);
439*8b26181fSAndroid Build Coastguard Worker 		(void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,
440*8b26181fSAndroid Build Coastguard Worker 		    fmt, ap);
441*8b26181fSAndroid Build Coastguard Worker 		va_end(ap);
442*8b26181fSAndroid Build Coastguard Worker 		cstate->error_set = 1;
443*8b26181fSAndroid Build Coastguard Worker 	}
444*8b26181fSAndroid Build Coastguard Worker }
445*8b26181fSAndroid Build Coastguard Worker 
446*8b26181fSAndroid Build Coastguard Worker /*
447*8b26181fSAndroid Build Coastguard Worker  * For use *ONLY* in routines in this file.
448*8b26181fSAndroid Build Coastguard Worker  */
449*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...)
450*8b26181fSAndroid Build Coastguard Worker     PCAP_PRINTFLIKE(2, 3);
451*8b26181fSAndroid Build Coastguard Worker 
452*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
453*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN
bpf_error(compiler_state_t * cstate,const char * fmt,...)454*8b26181fSAndroid Build Coastguard Worker bpf_error(compiler_state_t *cstate, const char *fmt, ...)
455*8b26181fSAndroid Build Coastguard Worker {
456*8b26181fSAndroid Build Coastguard Worker 	va_list ap;
457*8b26181fSAndroid Build Coastguard Worker 
458*8b26181fSAndroid Build Coastguard Worker 	va_start(ap, fmt);
459*8b26181fSAndroid Build Coastguard Worker 	(void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,
460*8b26181fSAndroid Build Coastguard Worker 	    fmt, ap);
461*8b26181fSAndroid Build Coastguard Worker 	va_end(ap);
462*8b26181fSAndroid Build Coastguard Worker 	longjmp(cstate->top_ctx, 1);
463*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
464*8b26181fSAndroid Build Coastguard Worker #ifdef _AIX
465*8b26181fSAndroid Build Coastguard Worker 	PCAP_UNREACHABLE
466*8b26181fSAndroid Build Coastguard Worker #endif /* _AIX */
467*8b26181fSAndroid Build Coastguard Worker }
468*8b26181fSAndroid Build Coastguard Worker 
469*8b26181fSAndroid Build Coastguard Worker static int init_linktype(compiler_state_t *, pcap_t *);
470*8b26181fSAndroid Build Coastguard Worker 
471*8b26181fSAndroid Build Coastguard Worker static void init_regs(compiler_state_t *);
472*8b26181fSAndroid Build Coastguard Worker static int alloc_reg(compiler_state_t *);
473*8b26181fSAndroid Build Coastguard Worker static void free_reg(compiler_state_t *, int);
474*8b26181fSAndroid Build Coastguard Worker 
475*8b26181fSAndroid Build Coastguard Worker static void initchunks(compiler_state_t *cstate);
476*8b26181fSAndroid Build Coastguard Worker static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t);
477*8b26181fSAndroid Build Coastguard Worker static void *newchunk(compiler_state_t *cstate, size_t);
478*8b26181fSAndroid Build Coastguard Worker static void freechunks(compiler_state_t *cstate);
479*8b26181fSAndroid Build Coastguard Worker static inline struct block *new_block(compiler_state_t *cstate, int);
480*8b26181fSAndroid Build Coastguard Worker static inline struct slist *new_stmt(compiler_state_t *cstate, int);
481*8b26181fSAndroid Build Coastguard Worker static struct block *gen_retblk(compiler_state_t *cstate, int);
482*8b26181fSAndroid Build Coastguard Worker static inline void syntax(compiler_state_t *cstate);
483*8b26181fSAndroid Build Coastguard Worker 
484*8b26181fSAndroid Build Coastguard Worker static void backpatch(struct block *, struct block *);
485*8b26181fSAndroid Build Coastguard Worker static void merge(struct block *, struct block *);
486*8b26181fSAndroid Build Coastguard Worker static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int,
487*8b26181fSAndroid Build Coastguard Worker     u_int, bpf_u_int32);
488*8b26181fSAndroid Build Coastguard Worker static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int,
489*8b26181fSAndroid Build Coastguard Worker     u_int, bpf_u_int32);
490*8b26181fSAndroid Build Coastguard Worker static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int,
491*8b26181fSAndroid Build Coastguard Worker     u_int, bpf_u_int32);
492*8b26181fSAndroid Build Coastguard Worker static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int,
493*8b26181fSAndroid Build Coastguard Worker     u_int, bpf_u_int32);
494*8b26181fSAndroid Build Coastguard Worker static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int,
495*8b26181fSAndroid Build Coastguard Worker     u_int, bpf_u_int32);
496*8b26181fSAndroid Build Coastguard Worker static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int,
497*8b26181fSAndroid Build Coastguard Worker     u_int, bpf_u_int32, bpf_u_int32);
498*8b26181fSAndroid Build Coastguard Worker static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int,
499*8b26181fSAndroid Build Coastguard Worker     u_int, const u_char *);
500*8b26181fSAndroid Build Coastguard Worker static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, u_int,
501*8b26181fSAndroid Build Coastguard Worker     u_int, bpf_u_int32, int, int, bpf_u_int32);
502*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *,
503*8b26181fSAndroid Build Coastguard Worker     u_int, u_int);
504*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int,
505*8b26181fSAndroid Build Coastguard Worker     u_int);
506*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_loadx_iphdrlen(compiler_state_t *);
507*8b26181fSAndroid Build Coastguard Worker static struct block *gen_uncond(compiler_state_t *, int);
508*8b26181fSAndroid Build Coastguard Worker static inline struct block *gen_true(compiler_state_t *);
509*8b26181fSAndroid Build Coastguard Worker static inline struct block *gen_false(compiler_state_t *);
510*8b26181fSAndroid Build Coastguard Worker static struct block *gen_ether_linktype(compiler_state_t *, bpf_u_int32);
511*8b26181fSAndroid Build Coastguard Worker static struct block *gen_ipnet_linktype(compiler_state_t *, bpf_u_int32);
512*8b26181fSAndroid Build Coastguard Worker static struct block *gen_linux_sll_linktype(compiler_state_t *, bpf_u_int32);
513*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_load_pflog_llprefixlen(compiler_state_t *);
514*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_load_prism_llprefixlen(compiler_state_t *);
515*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_load_avs_llprefixlen(compiler_state_t *);
516*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *);
517*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *);
518*8b26181fSAndroid Build Coastguard Worker static void insert_compute_vloffsets(compiler_state_t *, struct block *);
519*8b26181fSAndroid Build Coastguard Worker static struct slist *gen_abs_offset_varpart(compiler_state_t *,
520*8b26181fSAndroid Build Coastguard Worker     bpf_abs_offset *);
521*8b26181fSAndroid Build Coastguard Worker static bpf_u_int32 ethertype_to_ppptype(bpf_u_int32);
522*8b26181fSAndroid Build Coastguard Worker static struct block *gen_linktype(compiler_state_t *, bpf_u_int32);
523*8b26181fSAndroid Build Coastguard Worker static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32);
524*8b26181fSAndroid Build Coastguard Worker static struct block *gen_llc_linktype(compiler_state_t *, bpf_u_int32);
525*8b26181fSAndroid Build Coastguard Worker static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32,
526*8b26181fSAndroid Build Coastguard Worker     int, bpf_u_int32, u_int, u_int);
527*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
528*8b26181fSAndroid Build Coastguard Worker static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *,
529*8b26181fSAndroid Build Coastguard Worker     struct in6_addr *, int, bpf_u_int32, u_int, u_int);
530*8b26181fSAndroid Build Coastguard Worker #endif
531*8b26181fSAndroid Build Coastguard Worker static struct block *gen_ahostop(compiler_state_t *, const u_char *, int);
532*8b26181fSAndroid Build Coastguard Worker static struct block *gen_ehostop(compiler_state_t *, const u_char *, int);
533*8b26181fSAndroid Build Coastguard Worker static struct block *gen_fhostop(compiler_state_t *, const u_char *, int);
534*8b26181fSAndroid Build Coastguard Worker static struct block *gen_thostop(compiler_state_t *, const u_char *, int);
535*8b26181fSAndroid Build Coastguard Worker static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int);
536*8b26181fSAndroid Build Coastguard Worker static struct block *gen_ipfchostop(compiler_state_t *, const u_char *, int);
537*8b26181fSAndroid Build Coastguard Worker static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int);
538*8b26181fSAndroid Build Coastguard Worker static struct block *gen_mpls_linktype(compiler_state_t *, bpf_u_int32);
539*8b26181fSAndroid Build Coastguard Worker static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32,
540*8b26181fSAndroid Build Coastguard Worker     int, int, int);
541*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
542*8b26181fSAndroid Build Coastguard Worker static struct block *gen_host6(compiler_state_t *, struct in6_addr *,
543*8b26181fSAndroid Build Coastguard Worker     struct in6_addr *, int, int, int);
544*8b26181fSAndroid Build Coastguard Worker #endif
545*8b26181fSAndroid Build Coastguard Worker #ifndef INET6
546*8b26181fSAndroid Build Coastguard Worker static struct block *gen_gateway(compiler_state_t *, const u_char *,
547*8b26181fSAndroid Build Coastguard Worker     struct addrinfo *, int, int);
548*8b26181fSAndroid Build Coastguard Worker #endif
549*8b26181fSAndroid Build Coastguard Worker static struct block *gen_ipfrag(compiler_state_t *);
550*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portatom(compiler_state_t *, int, bpf_u_int32);
551*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portrangeatom(compiler_state_t *, u_int, bpf_u_int32,
552*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32);
553*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portatom6(compiler_state_t *, int, bpf_u_int32);
554*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portrangeatom6(compiler_state_t *, u_int, bpf_u_int32,
555*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32);
556*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portop(compiler_state_t *, u_int, u_int, int);
557*8b26181fSAndroid Build Coastguard Worker static struct block *gen_port(compiler_state_t *, u_int, int, int);
558*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portrangeop(compiler_state_t *, u_int, u_int,
559*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32, int);
560*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portrange(compiler_state_t *, u_int, u_int, int, int);
561*8b26181fSAndroid Build Coastguard Worker struct block *gen_portop6(compiler_state_t *, u_int, u_int, int);
562*8b26181fSAndroid Build Coastguard Worker static struct block *gen_port6(compiler_state_t *, u_int, int, int);
563*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portrangeop6(compiler_state_t *, u_int, u_int,
564*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32, int);
565*8b26181fSAndroid Build Coastguard Worker static struct block *gen_portrange6(compiler_state_t *, u_int, u_int, int, int);
566*8b26181fSAndroid Build Coastguard Worker static int lookup_proto(compiler_state_t *, const char *, int);
567*8b26181fSAndroid Build Coastguard Worker #if !defined(NO_PROTOCHAIN)
568*8b26181fSAndroid Build Coastguard Worker static struct block *gen_protochain(compiler_state_t *, bpf_u_int32, int);
569*8b26181fSAndroid Build Coastguard Worker #endif /* !defined(NO_PROTOCHAIN) */
570*8b26181fSAndroid Build Coastguard Worker static struct block *gen_proto(compiler_state_t *, bpf_u_int32, int, int);
571*8b26181fSAndroid Build Coastguard Worker static struct slist *xfer_to_x(compiler_state_t *, struct arth *);
572*8b26181fSAndroid Build Coastguard Worker static struct slist *xfer_to_a(compiler_state_t *, struct arth *);
573*8b26181fSAndroid Build Coastguard Worker static struct block *gen_mac_multicast(compiler_state_t *, int);
574*8b26181fSAndroid Build Coastguard Worker static struct block *gen_len(compiler_state_t *, int, int);
575*8b26181fSAndroid Build Coastguard Worker static struct block *gen_check_802_11_data_frame(compiler_state_t *);
576*8b26181fSAndroid Build Coastguard Worker static struct block *gen_geneve_ll_check(compiler_state_t *cstate);
577*8b26181fSAndroid Build Coastguard Worker 
578*8b26181fSAndroid Build Coastguard Worker static struct block *gen_ppi_dlt_check(compiler_state_t *);
579*8b26181fSAndroid Build Coastguard Worker static struct block *gen_atmfield_code_internal(compiler_state_t *, int,
580*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32, int, int);
581*8b26181fSAndroid Build Coastguard Worker static struct block *gen_atmtype_llc(compiler_state_t *);
582*8b26181fSAndroid Build Coastguard Worker static struct block *gen_msg_abbrev(compiler_state_t *, int type);
583*8b26181fSAndroid Build Coastguard Worker 
584*8b26181fSAndroid Build Coastguard Worker static void
initchunks(compiler_state_t * cstate)585*8b26181fSAndroid Build Coastguard Worker initchunks(compiler_state_t *cstate)
586*8b26181fSAndroid Build Coastguard Worker {
587*8b26181fSAndroid Build Coastguard Worker 	int i;
588*8b26181fSAndroid Build Coastguard Worker 
589*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; i < NCHUNKS; i++) {
590*8b26181fSAndroid Build Coastguard Worker 		cstate->chunks[i].n_left = 0;
591*8b26181fSAndroid Build Coastguard Worker 		cstate->chunks[i].m = NULL;
592*8b26181fSAndroid Build Coastguard Worker 	}
593*8b26181fSAndroid Build Coastguard Worker 	cstate->cur_chunk = 0;
594*8b26181fSAndroid Build Coastguard Worker }
595*8b26181fSAndroid Build Coastguard Worker 
596*8b26181fSAndroid Build Coastguard Worker static void *
newchunk_nolongjmp(compiler_state_t * cstate,size_t n)597*8b26181fSAndroid Build Coastguard Worker newchunk_nolongjmp(compiler_state_t *cstate, size_t n)
598*8b26181fSAndroid Build Coastguard Worker {
599*8b26181fSAndroid Build Coastguard Worker 	struct chunk *cp;
600*8b26181fSAndroid Build Coastguard Worker 	int k;
601*8b26181fSAndroid Build Coastguard Worker 	size_t size;
602*8b26181fSAndroid Build Coastguard Worker 
603*8b26181fSAndroid Build Coastguard Worker #ifndef __NetBSD__
604*8b26181fSAndroid Build Coastguard Worker 	/* XXX Round up to nearest long. */
605*8b26181fSAndroid Build Coastguard Worker 	n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
606*8b26181fSAndroid Build Coastguard Worker #else
607*8b26181fSAndroid Build Coastguard Worker 	/* XXX Round up to structure boundary. */
608*8b26181fSAndroid Build Coastguard Worker 	n = ALIGN(n);
609*8b26181fSAndroid Build Coastguard Worker #endif
610*8b26181fSAndroid Build Coastguard Worker 
611*8b26181fSAndroid Build Coastguard Worker 	cp = &cstate->chunks[cstate->cur_chunk];
612*8b26181fSAndroid Build Coastguard Worker 	if (n > cp->n_left) {
613*8b26181fSAndroid Build Coastguard Worker 		++cp;
614*8b26181fSAndroid Build Coastguard Worker 		k = ++cstate->cur_chunk;
615*8b26181fSAndroid Build Coastguard Worker 		if (k >= NCHUNKS) {
616*8b26181fSAndroid Build Coastguard Worker 			bpf_set_error(cstate, "out of memory");
617*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
618*8b26181fSAndroid Build Coastguard Worker 		}
619*8b26181fSAndroid Build Coastguard Worker 		size = CHUNK0SIZE << k;
620*8b26181fSAndroid Build Coastguard Worker 		cp->m = (void *)malloc(size);
621*8b26181fSAndroid Build Coastguard Worker 		if (cp->m == NULL) {
622*8b26181fSAndroid Build Coastguard Worker 			bpf_set_error(cstate, "out of memory");
623*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
624*8b26181fSAndroid Build Coastguard Worker 		}
625*8b26181fSAndroid Build Coastguard Worker 		memset((char *)cp->m, 0, size);
626*8b26181fSAndroid Build Coastguard Worker 		cp->n_left = size;
627*8b26181fSAndroid Build Coastguard Worker 		if (n > size) {
628*8b26181fSAndroid Build Coastguard Worker 			bpf_set_error(cstate, "out of memory");
629*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
630*8b26181fSAndroid Build Coastguard Worker 		}
631*8b26181fSAndroid Build Coastguard Worker 	}
632*8b26181fSAndroid Build Coastguard Worker 	cp->n_left -= n;
633*8b26181fSAndroid Build Coastguard Worker 	return (void *)((char *)cp->m + cp->n_left);
634*8b26181fSAndroid Build Coastguard Worker }
635*8b26181fSAndroid Build Coastguard Worker 
636*8b26181fSAndroid Build Coastguard Worker static void *
newchunk(compiler_state_t * cstate,size_t n)637*8b26181fSAndroid Build Coastguard Worker newchunk(compiler_state_t *cstate, size_t n)
638*8b26181fSAndroid Build Coastguard Worker {
639*8b26181fSAndroid Build Coastguard Worker 	void *p;
640*8b26181fSAndroid Build Coastguard Worker 
641*8b26181fSAndroid Build Coastguard Worker 	p = newchunk_nolongjmp(cstate, n);
642*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL) {
643*8b26181fSAndroid Build Coastguard Worker 		longjmp(cstate->top_ctx, 1);
644*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
645*8b26181fSAndroid Build Coastguard Worker 	}
646*8b26181fSAndroid Build Coastguard Worker 	return (p);
647*8b26181fSAndroid Build Coastguard Worker }
648*8b26181fSAndroid Build Coastguard Worker 
649*8b26181fSAndroid Build Coastguard Worker static void
freechunks(compiler_state_t * cstate)650*8b26181fSAndroid Build Coastguard Worker freechunks(compiler_state_t *cstate)
651*8b26181fSAndroid Build Coastguard Worker {
652*8b26181fSAndroid Build Coastguard Worker 	int i;
653*8b26181fSAndroid Build Coastguard Worker 
654*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; i < NCHUNKS; ++i)
655*8b26181fSAndroid Build Coastguard Worker 		if (cstate->chunks[i].m != NULL)
656*8b26181fSAndroid Build Coastguard Worker 			free(cstate->chunks[i].m);
657*8b26181fSAndroid Build Coastguard Worker }
658*8b26181fSAndroid Build Coastguard Worker 
659*8b26181fSAndroid Build Coastguard Worker /*
660*8b26181fSAndroid Build Coastguard Worker  * A strdup whose allocations are freed after code generation is over.
661*8b26181fSAndroid Build Coastguard Worker  * This is used by the lexical analyzer, so it can't longjmp; it just
662*8b26181fSAndroid Build Coastguard Worker  * returns NULL on an allocation error, and the callers must check
663*8b26181fSAndroid Build Coastguard Worker  * for it.
664*8b26181fSAndroid Build Coastguard Worker  */
665*8b26181fSAndroid Build Coastguard Worker char *
sdup(compiler_state_t * cstate,const char * s)666*8b26181fSAndroid Build Coastguard Worker sdup(compiler_state_t *cstate, const char *s)
667*8b26181fSAndroid Build Coastguard Worker {
668*8b26181fSAndroid Build Coastguard Worker 	size_t n = strlen(s) + 1;
669*8b26181fSAndroid Build Coastguard Worker 	char *cp = newchunk_nolongjmp(cstate, n);
670*8b26181fSAndroid Build Coastguard Worker 
671*8b26181fSAndroid Build Coastguard Worker 	if (cp == NULL)
672*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
673*8b26181fSAndroid Build Coastguard Worker 	pcap_strlcpy(cp, s, n);
674*8b26181fSAndroid Build Coastguard Worker 	return (cp);
675*8b26181fSAndroid Build Coastguard Worker }
676*8b26181fSAndroid Build Coastguard Worker 
677*8b26181fSAndroid Build Coastguard Worker static inline struct block *
new_block(compiler_state_t * cstate,int code)678*8b26181fSAndroid Build Coastguard Worker new_block(compiler_state_t *cstate, int code)
679*8b26181fSAndroid Build Coastguard Worker {
680*8b26181fSAndroid Build Coastguard Worker 	struct block *p;
681*8b26181fSAndroid Build Coastguard Worker 
682*8b26181fSAndroid Build Coastguard Worker 	p = (struct block *)newchunk(cstate, sizeof(*p));
683*8b26181fSAndroid Build Coastguard Worker 	p->s.code = code;
684*8b26181fSAndroid Build Coastguard Worker 	p->head = p;
685*8b26181fSAndroid Build Coastguard Worker 
686*8b26181fSAndroid Build Coastguard Worker 	return p;
687*8b26181fSAndroid Build Coastguard Worker }
688*8b26181fSAndroid Build Coastguard Worker 
689*8b26181fSAndroid Build Coastguard Worker static inline struct slist *
new_stmt(compiler_state_t * cstate,int code)690*8b26181fSAndroid Build Coastguard Worker new_stmt(compiler_state_t *cstate, int code)
691*8b26181fSAndroid Build Coastguard Worker {
692*8b26181fSAndroid Build Coastguard Worker 	struct slist *p;
693*8b26181fSAndroid Build Coastguard Worker 
694*8b26181fSAndroid Build Coastguard Worker 	p = (struct slist *)newchunk(cstate, sizeof(*p));
695*8b26181fSAndroid Build Coastguard Worker 	p->s.code = code;
696*8b26181fSAndroid Build Coastguard Worker 
697*8b26181fSAndroid Build Coastguard Worker 	return p;
698*8b26181fSAndroid Build Coastguard Worker }
699*8b26181fSAndroid Build Coastguard Worker 
700*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_retblk(compiler_state_t * cstate,int v)701*8b26181fSAndroid Build Coastguard Worker gen_retblk(compiler_state_t *cstate, int v)
702*8b26181fSAndroid Build Coastguard Worker {
703*8b26181fSAndroid Build Coastguard Worker 	struct block *b = new_block(cstate, BPF_RET|BPF_K);
704*8b26181fSAndroid Build Coastguard Worker 
705*8b26181fSAndroid Build Coastguard Worker 	b->s.k = v;
706*8b26181fSAndroid Build Coastguard Worker 	return b;
707*8b26181fSAndroid Build Coastguard Worker }
708*8b26181fSAndroid Build Coastguard Worker 
709*8b26181fSAndroid Build Coastguard Worker static inline PCAP_NORETURN_DEF void
syntax(compiler_state_t * cstate)710*8b26181fSAndroid Build Coastguard Worker syntax(compiler_state_t *cstate)
711*8b26181fSAndroid Build Coastguard Worker {
712*8b26181fSAndroid Build Coastguard Worker 	bpf_error(cstate, "syntax error in filter expression");
713*8b26181fSAndroid Build Coastguard Worker }
714*8b26181fSAndroid Build Coastguard Worker 
715*8b26181fSAndroid Build Coastguard Worker int
pcap_compile(pcap_t * p,struct bpf_program * program,const char * buf,int optimize,bpf_u_int32 mask)716*8b26181fSAndroid Build Coastguard Worker pcap_compile(pcap_t *p, struct bpf_program *program,
717*8b26181fSAndroid Build Coastguard Worker 	     const char *buf, int optimize, bpf_u_int32 mask)
718*8b26181fSAndroid Build Coastguard Worker {
719*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
720*8b26181fSAndroid Build Coastguard Worker 	static int done = 0;
721*8b26181fSAndroid Build Coastguard Worker #endif
722*8b26181fSAndroid Build Coastguard Worker 	compiler_state_t cstate;
723*8b26181fSAndroid Build Coastguard Worker 	const char * volatile xbuf = buf;
724*8b26181fSAndroid Build Coastguard Worker 	yyscan_t scanner = NULL;
725*8b26181fSAndroid Build Coastguard Worker 	volatile YY_BUFFER_STATE in_buffer = NULL;
726*8b26181fSAndroid Build Coastguard Worker 	u_int len;
727*8b26181fSAndroid Build Coastguard Worker 	int  rc;
728*8b26181fSAndroid Build Coastguard Worker 
729*8b26181fSAndroid Build Coastguard Worker 	/*
730*8b26181fSAndroid Build Coastguard Worker 	 * If this pcap_t hasn't been activated, it doesn't have a
731*8b26181fSAndroid Build Coastguard Worker 	 * link-layer type, so we can't use it.
732*8b26181fSAndroid Build Coastguard Worker 	 */
733*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated) {
734*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
735*8b26181fSAndroid Build Coastguard Worker 		    "not-yet-activated pcap_t passed to pcap_compile");
736*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR);
737*8b26181fSAndroid Build Coastguard Worker 	}
738*8b26181fSAndroid Build Coastguard Worker 
739*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
740*8b26181fSAndroid Build Coastguard Worker 	if (!done)
741*8b26181fSAndroid Build Coastguard Worker 		pcap_wsockinit();
742*8b26181fSAndroid Build Coastguard Worker 	done = 1;
743*8b26181fSAndroid Build Coastguard Worker #endif
744*8b26181fSAndroid Build Coastguard Worker 
745*8b26181fSAndroid Build Coastguard Worker #ifdef ENABLE_REMOTE
746*8b26181fSAndroid Build Coastguard Worker 	/*
747*8b26181fSAndroid Build Coastguard Worker 	 * If the device on which we're capturing need to be notified
748*8b26181fSAndroid Build Coastguard Worker 	 * that a new filter is being compiled, do so.
749*8b26181fSAndroid Build Coastguard Worker 	 *
750*8b26181fSAndroid Build Coastguard Worker 	 * This allows them to save a copy of it, in case, for example,
751*8b26181fSAndroid Build Coastguard Worker 	 * they're implementing a form of remote packet capture, and
752*8b26181fSAndroid Build Coastguard Worker 	 * want the remote machine to filter out the packets in which
753*8b26181fSAndroid Build Coastguard Worker 	 * it's sending the packets it's captured.
754*8b26181fSAndroid Build Coastguard Worker 	 *
755*8b26181fSAndroid Build Coastguard Worker 	 * XXX - the fact that we happen to be compiling a filter
756*8b26181fSAndroid Build Coastguard Worker 	 * doesn't necessarily mean we'll be installing it as the
757*8b26181fSAndroid Build Coastguard Worker 	 * filter for this pcap_t; we might be running it from userland
758*8b26181fSAndroid Build Coastguard Worker 	 * on captured packets to do packet classification.  We really
759*8b26181fSAndroid Build Coastguard Worker 	 * need a better way of handling this, but this is all that
760*8b26181fSAndroid Build Coastguard Worker 	 * the WinPcap remote capture code did.
761*8b26181fSAndroid Build Coastguard Worker 	 */
762*8b26181fSAndroid Build Coastguard Worker 	if (p->save_current_filter_op != NULL)
763*8b26181fSAndroid Build Coastguard Worker 		(p->save_current_filter_op)(p, buf);
764*8b26181fSAndroid Build Coastguard Worker #endif
765*8b26181fSAndroid Build Coastguard Worker 
766*8b26181fSAndroid Build Coastguard Worker 	initchunks(&cstate);
767*8b26181fSAndroid Build Coastguard Worker 	cstate.no_optimize = 0;
768*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
769*8b26181fSAndroid Build Coastguard Worker 	cstate.ai = NULL;
770*8b26181fSAndroid Build Coastguard Worker #endif
771*8b26181fSAndroid Build Coastguard Worker 	cstate.e = NULL;
772*8b26181fSAndroid Build Coastguard Worker 	cstate.ic.root = NULL;
773*8b26181fSAndroid Build Coastguard Worker 	cstate.ic.cur_mark = 0;
774*8b26181fSAndroid Build Coastguard Worker 	cstate.bpf_pcap = p;
775*8b26181fSAndroid Build Coastguard Worker 	cstate.error_set = 0;
776*8b26181fSAndroid Build Coastguard Worker 	init_regs(&cstate);
777*8b26181fSAndroid Build Coastguard Worker 
778*8b26181fSAndroid Build Coastguard Worker 	cstate.netmask = mask;
779*8b26181fSAndroid Build Coastguard Worker 
780*8b26181fSAndroid Build Coastguard Worker 	cstate.snaplen = pcap_snapshot(p);
781*8b26181fSAndroid Build Coastguard Worker 	if (cstate.snaplen == 0) {
782*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
783*8b26181fSAndroid Build Coastguard Worker 			 "snaplen of 0 rejects all packets");
784*8b26181fSAndroid Build Coastguard Worker 		rc = PCAP_ERROR;
785*8b26181fSAndroid Build Coastguard Worker 		goto quit;
786*8b26181fSAndroid Build Coastguard Worker 	}
787*8b26181fSAndroid Build Coastguard Worker 
788*8b26181fSAndroid Build Coastguard Worker 	if (pcap_lex_init(&scanner) != 0)
789*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
790*8b26181fSAndroid Build Coastguard Worker 		    errno, "can't initialize scanner");
791*8b26181fSAndroid Build Coastguard Worker 	in_buffer = pcap__scan_string(xbuf ? xbuf : "", scanner);
792*8b26181fSAndroid Build Coastguard Worker 
793*8b26181fSAndroid Build Coastguard Worker 	/*
794*8b26181fSAndroid Build Coastguard Worker 	 * Associate the compiler state with the lexical analyzer
795*8b26181fSAndroid Build Coastguard Worker 	 * state.
796*8b26181fSAndroid Build Coastguard Worker 	 */
797*8b26181fSAndroid Build Coastguard Worker 	pcap_set_extra(&cstate, scanner);
798*8b26181fSAndroid Build Coastguard Worker 
799*8b26181fSAndroid Build Coastguard Worker 	if (init_linktype(&cstate, p) == -1) {
800*8b26181fSAndroid Build Coastguard Worker 		rc = PCAP_ERROR;
801*8b26181fSAndroid Build Coastguard Worker 		goto quit;
802*8b26181fSAndroid Build Coastguard Worker 	}
803*8b26181fSAndroid Build Coastguard Worker 	if (pcap_parse(scanner, &cstate) != 0) {
804*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
805*8b26181fSAndroid Build Coastguard Worker 		if (cstate.ai != NULL)
806*8b26181fSAndroid Build Coastguard Worker 			freeaddrinfo(cstate.ai);
807*8b26181fSAndroid Build Coastguard Worker #endif
808*8b26181fSAndroid Build Coastguard Worker 		if (cstate.e != NULL)
809*8b26181fSAndroid Build Coastguard Worker 			free(cstate.e);
810*8b26181fSAndroid Build Coastguard Worker 		rc = PCAP_ERROR;
811*8b26181fSAndroid Build Coastguard Worker 		goto quit;
812*8b26181fSAndroid Build Coastguard Worker 	}
813*8b26181fSAndroid Build Coastguard Worker 
814*8b26181fSAndroid Build Coastguard Worker 	if (cstate.ic.root == NULL) {
815*8b26181fSAndroid Build Coastguard Worker 		/*
816*8b26181fSAndroid Build Coastguard Worker 		 * Catch errors reported by gen_retblk().
817*8b26181fSAndroid Build Coastguard Worker 		 */
818*8b26181fSAndroid Build Coastguard Worker 		if (setjmp(cstate.top_ctx)) {
819*8b26181fSAndroid Build Coastguard Worker 			rc = PCAP_ERROR;
820*8b26181fSAndroid Build Coastguard Worker 			goto quit;
821*8b26181fSAndroid Build Coastguard Worker 		}
822*8b26181fSAndroid Build Coastguard Worker 		cstate.ic.root = gen_retblk(&cstate, cstate.snaplen);
823*8b26181fSAndroid Build Coastguard Worker 	}
824*8b26181fSAndroid Build Coastguard Worker 
825*8b26181fSAndroid Build Coastguard Worker 	if (optimize && !cstate.no_optimize) {
826*8b26181fSAndroid Build Coastguard Worker 		if (bpf_optimize(&cstate.ic, p->errbuf) == -1) {
827*8b26181fSAndroid Build Coastguard Worker 			/* Failure */
828*8b26181fSAndroid Build Coastguard Worker 			rc = PCAP_ERROR;
829*8b26181fSAndroid Build Coastguard Worker 			goto quit;
830*8b26181fSAndroid Build Coastguard Worker 		}
831*8b26181fSAndroid Build Coastguard Worker 		if (cstate.ic.root == NULL ||
832*8b26181fSAndroid Build Coastguard Worker 		    (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) {
833*8b26181fSAndroid Build Coastguard Worker 			(void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
834*8b26181fSAndroid Build Coastguard Worker 			    "expression rejects all packets");
835*8b26181fSAndroid Build Coastguard Worker 			rc = PCAP_ERROR;
836*8b26181fSAndroid Build Coastguard Worker 			goto quit;
837*8b26181fSAndroid Build Coastguard Worker 		}
838*8b26181fSAndroid Build Coastguard Worker 	}
839*8b26181fSAndroid Build Coastguard Worker 	program->bf_insns = icode_to_fcode(&cstate.ic,
840*8b26181fSAndroid Build Coastguard Worker 	    cstate.ic.root, &len, p->errbuf);
841*8b26181fSAndroid Build Coastguard Worker 	if (program->bf_insns == NULL) {
842*8b26181fSAndroid Build Coastguard Worker 		/* Failure */
843*8b26181fSAndroid Build Coastguard Worker 		rc = PCAP_ERROR;
844*8b26181fSAndroid Build Coastguard Worker 		goto quit;
845*8b26181fSAndroid Build Coastguard Worker 	}
846*8b26181fSAndroid Build Coastguard Worker 	program->bf_len = len;
847*8b26181fSAndroid Build Coastguard Worker 
848*8b26181fSAndroid Build Coastguard Worker 	rc = 0;  /* We're all okay */
849*8b26181fSAndroid Build Coastguard Worker 
850*8b26181fSAndroid Build Coastguard Worker quit:
851*8b26181fSAndroid Build Coastguard Worker 	/*
852*8b26181fSAndroid Build Coastguard Worker 	 * Clean up everything for the lexical analyzer.
853*8b26181fSAndroid Build Coastguard Worker 	 */
854*8b26181fSAndroid Build Coastguard Worker 	if (in_buffer != NULL)
855*8b26181fSAndroid Build Coastguard Worker 		pcap__delete_buffer(in_buffer, scanner);
856*8b26181fSAndroid Build Coastguard Worker 	if (scanner != NULL)
857*8b26181fSAndroid Build Coastguard Worker 		pcap_lex_destroy(scanner);
858*8b26181fSAndroid Build Coastguard Worker 
859*8b26181fSAndroid Build Coastguard Worker 	/*
860*8b26181fSAndroid Build Coastguard Worker 	 * Clean up our own allocated memory.
861*8b26181fSAndroid Build Coastguard Worker 	 */
862*8b26181fSAndroid Build Coastguard Worker 	freechunks(&cstate);
863*8b26181fSAndroid Build Coastguard Worker 
864*8b26181fSAndroid Build Coastguard Worker 	return (rc);
865*8b26181fSAndroid Build Coastguard Worker }
866*8b26181fSAndroid Build Coastguard Worker 
867*8b26181fSAndroid Build Coastguard Worker /*
868*8b26181fSAndroid Build Coastguard Worker  * entry point for using the compiler with no pcap open
869*8b26181fSAndroid Build Coastguard Worker  * pass in all the stuff that is needed explicitly instead.
870*8b26181fSAndroid Build Coastguard Worker  */
871*8b26181fSAndroid Build Coastguard Worker int
pcap_compile_nopcap(int snaplen_arg,int linktype_arg,struct bpf_program * program,const char * buf,int optimize,bpf_u_int32 mask)872*8b26181fSAndroid Build Coastguard Worker pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
873*8b26181fSAndroid Build Coastguard Worker 		    struct bpf_program *program,
874*8b26181fSAndroid Build Coastguard Worker 	     const char *buf, int optimize, bpf_u_int32 mask)
875*8b26181fSAndroid Build Coastguard Worker {
876*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
877*8b26181fSAndroid Build Coastguard Worker 	int ret;
878*8b26181fSAndroid Build Coastguard Worker 
879*8b26181fSAndroid Build Coastguard Worker 	p = pcap_open_dead(linktype_arg, snaplen_arg);
880*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL)
881*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR);
882*8b26181fSAndroid Build Coastguard Worker 	ret = pcap_compile(p, program, buf, optimize, mask);
883*8b26181fSAndroid Build Coastguard Worker 	pcap_close(p);
884*8b26181fSAndroid Build Coastguard Worker 	return (ret);
885*8b26181fSAndroid Build Coastguard Worker }
886*8b26181fSAndroid Build Coastguard Worker 
887*8b26181fSAndroid Build Coastguard Worker /*
888*8b26181fSAndroid Build Coastguard Worker  * Clean up a "struct bpf_program" by freeing all the memory allocated
889*8b26181fSAndroid Build Coastguard Worker  * in it.
890*8b26181fSAndroid Build Coastguard Worker  */
891*8b26181fSAndroid Build Coastguard Worker void
pcap_freecode(struct bpf_program * program)892*8b26181fSAndroid Build Coastguard Worker pcap_freecode(struct bpf_program *program)
893*8b26181fSAndroid Build Coastguard Worker {
894*8b26181fSAndroid Build Coastguard Worker 	program->bf_len = 0;
895*8b26181fSAndroid Build Coastguard Worker 	if (program->bf_insns != NULL) {
896*8b26181fSAndroid Build Coastguard Worker 		free((char *)program->bf_insns);
897*8b26181fSAndroid Build Coastguard Worker 		program->bf_insns = NULL;
898*8b26181fSAndroid Build Coastguard Worker 	}
899*8b26181fSAndroid Build Coastguard Worker }
900*8b26181fSAndroid Build Coastguard Worker 
901*8b26181fSAndroid Build Coastguard Worker /*
902*8b26181fSAndroid Build Coastguard Worker  * Backpatch the blocks in 'list' to 'target'.  The 'sense' field indicates
903*8b26181fSAndroid Build Coastguard Worker  * which of the jt and jf fields has been resolved and which is a pointer
904*8b26181fSAndroid Build Coastguard Worker  * back to another unresolved block (or nil).  At least one of the fields
905*8b26181fSAndroid Build Coastguard Worker  * in each block is already resolved.
906*8b26181fSAndroid Build Coastguard Worker  */
907*8b26181fSAndroid Build Coastguard Worker static void
backpatch(struct block * list,struct block * target)908*8b26181fSAndroid Build Coastguard Worker backpatch(struct block *list, struct block *target)
909*8b26181fSAndroid Build Coastguard Worker {
910*8b26181fSAndroid Build Coastguard Worker 	struct block *next;
911*8b26181fSAndroid Build Coastguard Worker 
912*8b26181fSAndroid Build Coastguard Worker 	while (list) {
913*8b26181fSAndroid Build Coastguard Worker 		if (!list->sense) {
914*8b26181fSAndroid Build Coastguard Worker 			next = JT(list);
915*8b26181fSAndroid Build Coastguard Worker 			JT(list) = target;
916*8b26181fSAndroid Build Coastguard Worker 		} else {
917*8b26181fSAndroid Build Coastguard Worker 			next = JF(list);
918*8b26181fSAndroid Build Coastguard Worker 			JF(list) = target;
919*8b26181fSAndroid Build Coastguard Worker 		}
920*8b26181fSAndroid Build Coastguard Worker 		list = next;
921*8b26181fSAndroid Build Coastguard Worker 	}
922*8b26181fSAndroid Build Coastguard Worker }
923*8b26181fSAndroid Build Coastguard Worker 
924*8b26181fSAndroid Build Coastguard Worker /*
925*8b26181fSAndroid Build Coastguard Worker  * Merge the lists in b0 and b1, using the 'sense' field to indicate
926*8b26181fSAndroid Build Coastguard Worker  * which of jt and jf is the link.
927*8b26181fSAndroid Build Coastguard Worker  */
928*8b26181fSAndroid Build Coastguard Worker static void
merge(struct block * b0,struct block * b1)929*8b26181fSAndroid Build Coastguard Worker merge(struct block *b0, struct block *b1)
930*8b26181fSAndroid Build Coastguard Worker {
931*8b26181fSAndroid Build Coastguard Worker 	register struct block **p = &b0;
932*8b26181fSAndroid Build Coastguard Worker 
933*8b26181fSAndroid Build Coastguard Worker 	/* Find end of list. */
934*8b26181fSAndroid Build Coastguard Worker 	while (*p)
935*8b26181fSAndroid Build Coastguard Worker 		p = !((*p)->sense) ? &JT(*p) : &JF(*p);
936*8b26181fSAndroid Build Coastguard Worker 
937*8b26181fSAndroid Build Coastguard Worker 	/* Concatenate the lists. */
938*8b26181fSAndroid Build Coastguard Worker 	*p = b1;
939*8b26181fSAndroid Build Coastguard Worker }
940*8b26181fSAndroid Build Coastguard Worker 
941*8b26181fSAndroid Build Coastguard Worker int
finish_parse(compiler_state_t * cstate,struct block * p)942*8b26181fSAndroid Build Coastguard Worker finish_parse(compiler_state_t *cstate, struct block *p)
943*8b26181fSAndroid Build Coastguard Worker {
944*8b26181fSAndroid Build Coastguard Worker 	struct block *ppi_dlt_check;
945*8b26181fSAndroid Build Coastguard Worker 
946*8b26181fSAndroid Build Coastguard Worker 	/*
947*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return -1
948*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
949*8b26181fSAndroid Build Coastguard Worker 	 */
950*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
951*8b26181fSAndroid Build Coastguard Worker 		return (-1);
952*8b26181fSAndroid Build Coastguard Worker 
953*8b26181fSAndroid Build Coastguard Worker 	/*
954*8b26181fSAndroid Build Coastguard Worker 	 * Insert before the statements of the first (root) block any
955*8b26181fSAndroid Build Coastguard Worker 	 * statements needed to load the lengths of any variable-length
956*8b26181fSAndroid Build Coastguard Worker 	 * headers into registers.
957*8b26181fSAndroid Build Coastguard Worker 	 *
958*8b26181fSAndroid Build Coastguard Worker 	 * XXX - a fancier strategy would be to insert those before the
959*8b26181fSAndroid Build Coastguard Worker 	 * statements of all blocks that use those lengths and that
960*8b26181fSAndroid Build Coastguard Worker 	 * have no predecessors that use them, so that we only compute
961*8b26181fSAndroid Build Coastguard Worker 	 * the lengths if we need them.  There might be even better
962*8b26181fSAndroid Build Coastguard Worker 	 * approaches than that.
963*8b26181fSAndroid Build Coastguard Worker 	 *
964*8b26181fSAndroid Build Coastguard Worker 	 * However, those strategies would be more complicated, and
965*8b26181fSAndroid Build Coastguard Worker 	 * as we don't generate code to compute a length if the
966*8b26181fSAndroid Build Coastguard Worker 	 * program has no tests that use the length, and as most
967*8b26181fSAndroid Build Coastguard Worker 	 * tests will probably use those lengths, we would just
968*8b26181fSAndroid Build Coastguard Worker 	 * postpone computing the lengths so that it's not done
969*8b26181fSAndroid Build Coastguard Worker 	 * for tests that fail early, and it's not clear that's
970*8b26181fSAndroid Build Coastguard Worker 	 * worth the effort.
971*8b26181fSAndroid Build Coastguard Worker 	 */
972*8b26181fSAndroid Build Coastguard Worker 	insert_compute_vloffsets(cstate, p->head);
973*8b26181fSAndroid Build Coastguard Worker 
974*8b26181fSAndroid Build Coastguard Worker 	/*
975*8b26181fSAndroid Build Coastguard Worker 	 * For DLT_PPI captures, generate a check of the per-packet
976*8b26181fSAndroid Build Coastguard Worker 	 * DLT value to make sure it's DLT_IEEE802_11.
977*8b26181fSAndroid Build Coastguard Worker 	 *
978*8b26181fSAndroid Build Coastguard Worker 	 * XXX - TurboCap cards use DLT_PPI for Ethernet.
979*8b26181fSAndroid Build Coastguard Worker 	 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
980*8b26181fSAndroid Build Coastguard Worker 	 * with appropriate Ethernet information and use that rather
981*8b26181fSAndroid Build Coastguard Worker 	 * than using something such as DLT_PPI where you don't know
982*8b26181fSAndroid Build Coastguard Worker 	 * the link-layer header type until runtime, which, in the
983*8b26181fSAndroid Build Coastguard Worker 	 * general case, would force us to generate both Ethernet *and*
984*8b26181fSAndroid Build Coastguard Worker 	 * 802.11 code (*and* anything else for which PPI is used)
985*8b26181fSAndroid Build Coastguard Worker 	 * and choose between them early in the BPF program?
986*8b26181fSAndroid Build Coastguard Worker 	 */
987*8b26181fSAndroid Build Coastguard Worker 	ppi_dlt_check = gen_ppi_dlt_check(cstate);
988*8b26181fSAndroid Build Coastguard Worker 	if (ppi_dlt_check != NULL)
989*8b26181fSAndroid Build Coastguard Worker 		gen_and(ppi_dlt_check, p);
990*8b26181fSAndroid Build Coastguard Worker 
991*8b26181fSAndroid Build Coastguard Worker 	backpatch(p, gen_retblk(cstate, cstate->snaplen));
992*8b26181fSAndroid Build Coastguard Worker 	p->sense = !p->sense;
993*8b26181fSAndroid Build Coastguard Worker 	backpatch(p, gen_retblk(cstate, 0));
994*8b26181fSAndroid Build Coastguard Worker 	cstate->ic.root = p->head;
995*8b26181fSAndroid Build Coastguard Worker 	return (0);
996*8b26181fSAndroid Build Coastguard Worker }
997*8b26181fSAndroid Build Coastguard Worker 
998*8b26181fSAndroid Build Coastguard Worker void
gen_and(struct block * b0,struct block * b1)999*8b26181fSAndroid Build Coastguard Worker gen_and(struct block *b0, struct block *b1)
1000*8b26181fSAndroid Build Coastguard Worker {
1001*8b26181fSAndroid Build Coastguard Worker 	backpatch(b0, b1->head);
1002*8b26181fSAndroid Build Coastguard Worker 	b0->sense = !b0->sense;
1003*8b26181fSAndroid Build Coastguard Worker 	b1->sense = !b1->sense;
1004*8b26181fSAndroid Build Coastguard Worker 	merge(b1, b0);
1005*8b26181fSAndroid Build Coastguard Worker 	b1->sense = !b1->sense;
1006*8b26181fSAndroid Build Coastguard Worker 	b1->head = b0->head;
1007*8b26181fSAndroid Build Coastguard Worker }
1008*8b26181fSAndroid Build Coastguard Worker 
1009*8b26181fSAndroid Build Coastguard Worker void
gen_or(struct block * b0,struct block * b1)1010*8b26181fSAndroid Build Coastguard Worker gen_or(struct block *b0, struct block *b1)
1011*8b26181fSAndroid Build Coastguard Worker {
1012*8b26181fSAndroid Build Coastguard Worker 	b0->sense = !b0->sense;
1013*8b26181fSAndroid Build Coastguard Worker 	backpatch(b0, b1->head);
1014*8b26181fSAndroid Build Coastguard Worker 	b0->sense = !b0->sense;
1015*8b26181fSAndroid Build Coastguard Worker 	merge(b1, b0);
1016*8b26181fSAndroid Build Coastguard Worker 	b1->head = b0->head;
1017*8b26181fSAndroid Build Coastguard Worker }
1018*8b26181fSAndroid Build Coastguard Worker 
1019*8b26181fSAndroid Build Coastguard Worker void
gen_not(struct block * b)1020*8b26181fSAndroid Build Coastguard Worker gen_not(struct block *b)
1021*8b26181fSAndroid Build Coastguard Worker {
1022*8b26181fSAndroid Build Coastguard Worker 	b->sense = !b->sense;
1023*8b26181fSAndroid Build Coastguard Worker }
1024*8b26181fSAndroid Build Coastguard Worker 
1025*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_cmp(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size,bpf_u_int32 v)1026*8b26181fSAndroid Build Coastguard Worker gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1027*8b26181fSAndroid Build Coastguard Worker     u_int size, bpf_u_int32 v)
1028*8b26181fSAndroid Build Coastguard Worker {
1029*8b26181fSAndroid Build Coastguard Worker 	return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
1030*8b26181fSAndroid Build Coastguard Worker }
1031*8b26181fSAndroid Build Coastguard Worker 
1032*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_cmp_gt(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size,bpf_u_int32 v)1033*8b26181fSAndroid Build Coastguard Worker gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1034*8b26181fSAndroid Build Coastguard Worker     u_int size, bpf_u_int32 v)
1035*8b26181fSAndroid Build Coastguard Worker {
1036*8b26181fSAndroid Build Coastguard Worker 	return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
1037*8b26181fSAndroid Build Coastguard Worker }
1038*8b26181fSAndroid Build Coastguard Worker 
1039*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_cmp_ge(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size,bpf_u_int32 v)1040*8b26181fSAndroid Build Coastguard Worker gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1041*8b26181fSAndroid Build Coastguard Worker     u_int size, bpf_u_int32 v)
1042*8b26181fSAndroid Build Coastguard Worker {
1043*8b26181fSAndroid Build Coastguard Worker 	return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
1044*8b26181fSAndroid Build Coastguard Worker }
1045*8b26181fSAndroid Build Coastguard Worker 
1046*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_cmp_lt(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size,bpf_u_int32 v)1047*8b26181fSAndroid Build Coastguard Worker gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1048*8b26181fSAndroid Build Coastguard Worker     u_int size, bpf_u_int32 v)
1049*8b26181fSAndroid Build Coastguard Worker {
1050*8b26181fSAndroid Build Coastguard Worker 	return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
1051*8b26181fSAndroid Build Coastguard Worker }
1052*8b26181fSAndroid Build Coastguard Worker 
1053*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_cmp_le(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size,bpf_u_int32 v)1054*8b26181fSAndroid Build Coastguard Worker gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1055*8b26181fSAndroid Build Coastguard Worker     u_int size, bpf_u_int32 v)
1056*8b26181fSAndroid Build Coastguard Worker {
1057*8b26181fSAndroid Build Coastguard Worker 	return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
1058*8b26181fSAndroid Build Coastguard Worker }
1059*8b26181fSAndroid Build Coastguard Worker 
1060*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_mcmp(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size,bpf_u_int32 v,bpf_u_int32 mask)1061*8b26181fSAndroid Build Coastguard Worker gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1062*8b26181fSAndroid Build Coastguard Worker     u_int size, bpf_u_int32 v, bpf_u_int32 mask)
1063*8b26181fSAndroid Build Coastguard Worker {
1064*8b26181fSAndroid Build Coastguard Worker 	return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v);
1065*8b26181fSAndroid Build Coastguard Worker }
1066*8b26181fSAndroid Build Coastguard Worker 
1067*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_bcmp(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size,const u_char * v)1068*8b26181fSAndroid Build Coastguard Worker gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1069*8b26181fSAndroid Build Coastguard Worker     u_int size, const u_char *v)
1070*8b26181fSAndroid Build Coastguard Worker {
1071*8b26181fSAndroid Build Coastguard Worker 	register struct block *b, *tmp;
1072*8b26181fSAndroid Build Coastguard Worker 
1073*8b26181fSAndroid Build Coastguard Worker 	b = NULL;
1074*8b26181fSAndroid Build Coastguard Worker 	while (size >= 4) {
1075*8b26181fSAndroid Build Coastguard Worker 		register const u_char *p = &v[size - 4];
1076*8b26181fSAndroid Build Coastguard Worker 
1077*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W,
1078*8b26181fSAndroid Build Coastguard Worker 		    EXTRACT_BE_U_4(p));
1079*8b26181fSAndroid Build Coastguard Worker 		if (b != NULL)
1080*8b26181fSAndroid Build Coastguard Worker 			gen_and(b, tmp);
1081*8b26181fSAndroid Build Coastguard Worker 		b = tmp;
1082*8b26181fSAndroid Build Coastguard Worker 		size -= 4;
1083*8b26181fSAndroid Build Coastguard Worker 	}
1084*8b26181fSAndroid Build Coastguard Worker 	while (size >= 2) {
1085*8b26181fSAndroid Build Coastguard Worker 		register const u_char *p = &v[size - 2];
1086*8b26181fSAndroid Build Coastguard Worker 
1087*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H,
1088*8b26181fSAndroid Build Coastguard Worker 		    EXTRACT_BE_U_2(p));
1089*8b26181fSAndroid Build Coastguard Worker 		if (b != NULL)
1090*8b26181fSAndroid Build Coastguard Worker 			gen_and(b, tmp);
1091*8b26181fSAndroid Build Coastguard Worker 		b = tmp;
1092*8b26181fSAndroid Build Coastguard Worker 		size -= 2;
1093*8b26181fSAndroid Build Coastguard Worker 	}
1094*8b26181fSAndroid Build Coastguard Worker 	if (size > 0) {
1095*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_cmp(cstate, offrel, offset, BPF_B, v[0]);
1096*8b26181fSAndroid Build Coastguard Worker 		if (b != NULL)
1097*8b26181fSAndroid Build Coastguard Worker 			gen_and(b, tmp);
1098*8b26181fSAndroid Build Coastguard Worker 		b = tmp;
1099*8b26181fSAndroid Build Coastguard Worker 	}
1100*8b26181fSAndroid Build Coastguard Worker 	return b;
1101*8b26181fSAndroid Build Coastguard Worker }
1102*8b26181fSAndroid Build Coastguard Worker 
1103*8b26181fSAndroid Build Coastguard Worker /*
1104*8b26181fSAndroid Build Coastguard Worker  * AND the field of size "size" at offset "offset" relative to the header
1105*8b26181fSAndroid Build Coastguard Worker  * specified by "offrel" with "mask", and compare it with the value "v"
1106*8b26181fSAndroid Build Coastguard Worker  * with the test specified by "jtype"; if "reverse" is true, the test
1107*8b26181fSAndroid Build Coastguard Worker  * should test the opposite of "jtype".
1108*8b26181fSAndroid Build Coastguard Worker  */
1109*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_ncmp(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size,bpf_u_int32 mask,int jtype,int reverse,bpf_u_int32 v)1110*8b26181fSAndroid Build Coastguard Worker gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1111*8b26181fSAndroid Build Coastguard Worker     u_int size, bpf_u_int32 mask, int jtype, int reverse,
1112*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 v)
1113*8b26181fSAndroid Build Coastguard Worker {
1114*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s2;
1115*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
1116*8b26181fSAndroid Build Coastguard Worker 
1117*8b26181fSAndroid Build Coastguard Worker 	s = gen_load_a(cstate, offrel, offset, size);
1118*8b26181fSAndroid Build Coastguard Worker 
1119*8b26181fSAndroid Build Coastguard Worker 	if (mask != 0xffffffff) {
1120*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
1121*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = mask;
1122*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
1123*8b26181fSAndroid Build Coastguard Worker 	}
1124*8b26181fSAndroid Build Coastguard Worker 
1125*8b26181fSAndroid Build Coastguard Worker 	b = new_block(cstate, JMP(jtype));
1126*8b26181fSAndroid Build Coastguard Worker 	b->stmts = s;
1127*8b26181fSAndroid Build Coastguard Worker 	b->s.k = v;
1128*8b26181fSAndroid Build Coastguard Worker 	if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
1129*8b26181fSAndroid Build Coastguard Worker 		gen_not(b);
1130*8b26181fSAndroid Build Coastguard Worker 	return b;
1131*8b26181fSAndroid Build Coastguard Worker }
1132*8b26181fSAndroid Build Coastguard Worker 
1133*8b26181fSAndroid Build Coastguard Worker static int
init_linktype(compiler_state_t * cstate,pcap_t * p)1134*8b26181fSAndroid Build Coastguard Worker init_linktype(compiler_state_t *cstate, pcap_t *p)
1135*8b26181fSAndroid Build Coastguard Worker {
1136*8b26181fSAndroid Build Coastguard Worker 	cstate->pcap_fddipad = p->fddipad;
1137*8b26181fSAndroid Build Coastguard Worker 
1138*8b26181fSAndroid Build Coastguard Worker 	/*
1139*8b26181fSAndroid Build Coastguard Worker 	 * We start out with only one link-layer header.
1140*8b26181fSAndroid Build Coastguard Worker 	 */
1141*8b26181fSAndroid Build Coastguard Worker 	cstate->outermostlinktype = pcap_datalink(p);
1142*8b26181fSAndroid Build Coastguard Worker 	cstate->off_outermostlinkhdr.constant_part = 0;
1143*8b26181fSAndroid Build Coastguard Worker 	cstate->off_outermostlinkhdr.is_variable = 0;
1144*8b26181fSAndroid Build Coastguard Worker 	cstate->off_outermostlinkhdr.reg = -1;
1145*8b26181fSAndroid Build Coastguard Worker 
1146*8b26181fSAndroid Build Coastguard Worker 	cstate->prevlinktype = cstate->outermostlinktype;
1147*8b26181fSAndroid Build Coastguard Worker 	cstate->off_prevlinkhdr.constant_part = 0;
1148*8b26181fSAndroid Build Coastguard Worker 	cstate->off_prevlinkhdr.is_variable = 0;
1149*8b26181fSAndroid Build Coastguard Worker 	cstate->off_prevlinkhdr.reg = -1;
1150*8b26181fSAndroid Build Coastguard Worker 
1151*8b26181fSAndroid Build Coastguard Worker 	cstate->linktype = cstate->outermostlinktype;
1152*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkhdr.constant_part = 0;
1153*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkhdr.is_variable = 0;
1154*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkhdr.reg = -1;
1155*8b26181fSAndroid Build Coastguard Worker 
1156*8b26181fSAndroid Build Coastguard Worker 	/*
1157*8b26181fSAndroid Build Coastguard Worker 	 * XXX
1158*8b26181fSAndroid Build Coastguard Worker 	 */
1159*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkpl.constant_part = 0;
1160*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkpl.is_variable = 0;
1161*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkpl.reg = -1;
1162*8b26181fSAndroid Build Coastguard Worker 
1163*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linktype.constant_part = 0;
1164*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linktype.is_variable = 0;
1165*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linktype.reg = -1;
1166*8b26181fSAndroid Build Coastguard Worker 
1167*8b26181fSAndroid Build Coastguard Worker 	/*
1168*8b26181fSAndroid Build Coastguard Worker 	 * Assume it's not raw ATM with a pseudo-header, for now.
1169*8b26181fSAndroid Build Coastguard Worker 	 */
1170*8b26181fSAndroid Build Coastguard Worker 	cstate->is_atm = 0;
1171*8b26181fSAndroid Build Coastguard Worker 	cstate->off_vpi = OFFSET_NOT_SET;
1172*8b26181fSAndroid Build Coastguard Worker 	cstate->off_vci = OFFSET_NOT_SET;
1173*8b26181fSAndroid Build Coastguard Worker 	cstate->off_proto = OFFSET_NOT_SET;
1174*8b26181fSAndroid Build Coastguard Worker 	cstate->off_payload = OFFSET_NOT_SET;
1175*8b26181fSAndroid Build Coastguard Worker 
1176*8b26181fSAndroid Build Coastguard Worker 	/*
1177*8b26181fSAndroid Build Coastguard Worker 	 * And not Geneve.
1178*8b26181fSAndroid Build Coastguard Worker 	 */
1179*8b26181fSAndroid Build Coastguard Worker 	cstate->is_geneve = 0;
1180*8b26181fSAndroid Build Coastguard Worker 
1181*8b26181fSAndroid Build Coastguard Worker 	/*
1182*8b26181fSAndroid Build Coastguard Worker 	 * No variable length VLAN offset by default
1183*8b26181fSAndroid Build Coastguard Worker 	 */
1184*8b26181fSAndroid Build Coastguard Worker 	cstate->is_vlan_vloffset = 0;
1185*8b26181fSAndroid Build Coastguard Worker 
1186*8b26181fSAndroid Build Coastguard Worker 	/*
1187*8b26181fSAndroid Build Coastguard Worker 	 * And assume we're not doing SS7.
1188*8b26181fSAndroid Build Coastguard Worker 	 */
1189*8b26181fSAndroid Build Coastguard Worker 	cstate->off_li = OFFSET_NOT_SET;
1190*8b26181fSAndroid Build Coastguard Worker 	cstate->off_li_hsl = OFFSET_NOT_SET;
1191*8b26181fSAndroid Build Coastguard Worker 	cstate->off_sio = OFFSET_NOT_SET;
1192*8b26181fSAndroid Build Coastguard Worker 	cstate->off_opc = OFFSET_NOT_SET;
1193*8b26181fSAndroid Build Coastguard Worker 	cstate->off_dpc = OFFSET_NOT_SET;
1194*8b26181fSAndroid Build Coastguard Worker 	cstate->off_sls = OFFSET_NOT_SET;
1195*8b26181fSAndroid Build Coastguard Worker 
1196*8b26181fSAndroid Build Coastguard Worker 	cstate->label_stack_depth = 0;
1197*8b26181fSAndroid Build Coastguard Worker 	cstate->vlan_stack_depth = 0;
1198*8b26181fSAndroid Build Coastguard Worker 
1199*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
1200*8b26181fSAndroid Build Coastguard Worker 
1201*8b26181fSAndroid Build Coastguard Worker 	case DLT_ARCNET:
1202*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 2;
1203*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 6;
1204*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;		/* XXX in reality, variable! */
1205*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1206*8b26181fSAndroid Build Coastguard Worker 		break;
1207*8b26181fSAndroid Build Coastguard Worker 
1208*8b26181fSAndroid Build Coastguard Worker 	case DLT_ARCNET_LINUX:
1209*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 4;
1210*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 8;
1211*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;		/* XXX in reality, variable! */
1212*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1213*8b26181fSAndroid Build Coastguard Worker 		break;
1214*8b26181fSAndroid Build Coastguard Worker 
1215*8b26181fSAndroid Build Coastguard Worker 	case DLT_EN10MB:
1216*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 12;
1217*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 14;	/* Ethernet header length */
1218*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;		/* Ethernet II */
1219*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.3+802.2 */
1220*8b26181fSAndroid Build Coastguard Worker 		break;
1221*8b26181fSAndroid Build Coastguard Worker 
1222*8b26181fSAndroid Build Coastguard Worker 	case DLT_SLIP:
1223*8b26181fSAndroid Build Coastguard Worker 		/*
1224*8b26181fSAndroid Build Coastguard Worker 		 * SLIP doesn't have a link level type.  The 16 byte
1225*8b26181fSAndroid Build Coastguard Worker 		 * header is hacked into our SLIP driver.
1226*8b26181fSAndroid Build Coastguard Worker 		 */
1227*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1228*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 16;
1229*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1230*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1231*8b26181fSAndroid Build Coastguard Worker 		break;
1232*8b26181fSAndroid Build Coastguard Worker 
1233*8b26181fSAndroid Build Coastguard Worker 	case DLT_SLIP_BSDOS:
1234*8b26181fSAndroid Build Coastguard Worker 		/* XXX this may be the same as the DLT_PPP_BSDOS case */
1235*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1236*8b26181fSAndroid Build Coastguard Worker 		/* XXX end */
1237*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 24;
1238*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1239*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1240*8b26181fSAndroid Build Coastguard Worker 		break;
1241*8b26181fSAndroid Build Coastguard Worker 
1242*8b26181fSAndroid Build Coastguard Worker 	case DLT_NULL:
1243*8b26181fSAndroid Build Coastguard Worker 	case DLT_LOOP:
1244*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 0;
1245*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 4;
1246*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1247*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1248*8b26181fSAndroid Build Coastguard Worker 		break;
1249*8b26181fSAndroid Build Coastguard Worker 
1250*8b26181fSAndroid Build Coastguard Worker 	case DLT_ENC:
1251*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 0;
1252*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 12;
1253*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1254*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1255*8b26181fSAndroid Build Coastguard Worker 		break;
1256*8b26181fSAndroid Build Coastguard Worker 
1257*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP:
1258*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_PPPD:
1259*8b26181fSAndroid Build Coastguard Worker 	case DLT_C_HDLC:		/* BSD/OS Cisco HDLC */
1260*8b26181fSAndroid Build Coastguard Worker 	case DLT_HDLC:			/* NetBSD (Cisco) HDLC */
1261*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_SERIAL:		/* NetBSD sync/async serial PPP */
1262*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 2;	/* skip HDLC-like framing */
1263*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 4;	/* skip HDLC-like framing and protocol field */
1264*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1265*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1266*8b26181fSAndroid Build Coastguard Worker 		break;
1267*8b26181fSAndroid Build Coastguard Worker 
1268*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_ETHER:
1269*8b26181fSAndroid Build Coastguard Worker 		/*
1270*8b26181fSAndroid Build Coastguard Worker 		 * This does no include the Ethernet header, and
1271*8b26181fSAndroid Build Coastguard Worker 		 * only covers session state.
1272*8b26181fSAndroid Build Coastguard Worker 		 */
1273*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 6;
1274*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 8;
1275*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1276*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1277*8b26181fSAndroid Build Coastguard Worker 		break;
1278*8b26181fSAndroid Build Coastguard Worker 
1279*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_BSDOS:
1280*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 5;
1281*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 24;
1282*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1283*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1284*8b26181fSAndroid Build Coastguard Worker 		break;
1285*8b26181fSAndroid Build Coastguard Worker 
1286*8b26181fSAndroid Build Coastguard Worker 	case DLT_FDDI:
1287*8b26181fSAndroid Build Coastguard Worker 		/*
1288*8b26181fSAndroid Build Coastguard Worker 		 * FDDI doesn't really have a link-level type field.
1289*8b26181fSAndroid Build Coastguard Worker 		 * We set "off_linktype" to the offset of the LLC header.
1290*8b26181fSAndroid Build Coastguard Worker 		 *
1291*8b26181fSAndroid Build Coastguard Worker 		 * To check for Ethernet types, we assume that SSAP = SNAP
1292*8b26181fSAndroid Build Coastguard Worker 		 * is being used and pick out the encapsulated Ethernet type.
1293*8b26181fSAndroid Build Coastguard Worker 		 * XXX - should we generate code to check for SNAP?
1294*8b26181fSAndroid Build Coastguard Worker 		 */
1295*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 13;
1296*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part += cstate->pcap_fddipad;
1297*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 13;	/* FDDI MAC header length */
1298*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part += cstate->pcap_fddipad;
1299*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 8;		/* 802.2+SNAP */
1300*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.2 */
1301*8b26181fSAndroid Build Coastguard Worker 		break;
1302*8b26181fSAndroid Build Coastguard Worker 
1303*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802:
1304*8b26181fSAndroid Build Coastguard Worker 		/*
1305*8b26181fSAndroid Build Coastguard Worker 		 * Token Ring doesn't really have a link-level type field.
1306*8b26181fSAndroid Build Coastguard Worker 		 * We set "off_linktype" to the offset of the LLC header.
1307*8b26181fSAndroid Build Coastguard Worker 		 *
1308*8b26181fSAndroid Build Coastguard Worker 		 * To check for Ethernet types, we assume that SSAP = SNAP
1309*8b26181fSAndroid Build Coastguard Worker 		 * is being used and pick out the encapsulated Ethernet type.
1310*8b26181fSAndroid Build Coastguard Worker 		 * XXX - should we generate code to check for SNAP?
1311*8b26181fSAndroid Build Coastguard Worker 		 *
1312*8b26181fSAndroid Build Coastguard Worker 		 * XXX - the header is actually variable-length.
1313*8b26181fSAndroid Build Coastguard Worker 		 * Some various Linux patched versions gave 38
1314*8b26181fSAndroid Build Coastguard Worker 		 * as "off_linktype" and 40 as "off_nl"; however,
1315*8b26181fSAndroid Build Coastguard Worker 		 * if a token ring packet has *no* routing
1316*8b26181fSAndroid Build Coastguard Worker 		 * information, i.e. is not source-routed, the correct
1317*8b26181fSAndroid Build Coastguard Worker 		 * values are 20 and 22, as they are in the vanilla code.
1318*8b26181fSAndroid Build Coastguard Worker 		 *
1319*8b26181fSAndroid Build Coastguard Worker 		 * A packet is source-routed iff the uppermost bit
1320*8b26181fSAndroid Build Coastguard Worker 		 * of the first byte of the source address, at an
1321*8b26181fSAndroid Build Coastguard Worker 		 * offset of 8, has the uppermost bit set.  If the
1322*8b26181fSAndroid Build Coastguard Worker 		 * packet is source-routed, the total number of bytes
1323*8b26181fSAndroid Build Coastguard Worker 		 * of routing information is 2 plus bits 0x1F00 of
1324*8b26181fSAndroid Build Coastguard Worker 		 * the 16-bit value at an offset of 14 (shifted right
1325*8b26181fSAndroid Build Coastguard Worker 		 * 8 - figure out which byte that is).
1326*8b26181fSAndroid Build Coastguard Worker 		 */
1327*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 14;
1328*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 14;	/* Token Ring MAC header length */
1329*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 8;		/* 802.2+SNAP */
1330*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.2 */
1331*8b26181fSAndroid Build Coastguard Worker 		break;
1332*8b26181fSAndroid Build Coastguard Worker 
1333*8b26181fSAndroid Build Coastguard Worker 	case DLT_PRISM_HEADER:
1334*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO_AVS:
1335*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO:
1336*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkhdr.is_variable = 1;
1337*8b26181fSAndroid Build Coastguard Worker 		/* Fall through, 802.11 doesn't have a variable link
1338*8b26181fSAndroid Build Coastguard Worker 		 * prefix but is otherwise the same. */
1339*8b26181fSAndroid Build Coastguard Worker 		/* FALLTHROUGH */
1340*8b26181fSAndroid Build Coastguard Worker 
1341*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11:
1342*8b26181fSAndroid Build Coastguard Worker 		/*
1343*8b26181fSAndroid Build Coastguard Worker 		 * 802.11 doesn't really have a link-level type field.
1344*8b26181fSAndroid Build Coastguard Worker 		 * We set "off_linktype.constant_part" to the offset of
1345*8b26181fSAndroid Build Coastguard Worker 		 * the LLC header.
1346*8b26181fSAndroid Build Coastguard Worker 		 *
1347*8b26181fSAndroid Build Coastguard Worker 		 * To check for Ethernet types, we assume that SSAP = SNAP
1348*8b26181fSAndroid Build Coastguard Worker 		 * is being used and pick out the encapsulated Ethernet type.
1349*8b26181fSAndroid Build Coastguard Worker 		 * XXX - should we generate code to check for SNAP?
1350*8b26181fSAndroid Build Coastguard Worker 		 *
1351*8b26181fSAndroid Build Coastguard Worker 		 * We also handle variable-length radio headers here.
1352*8b26181fSAndroid Build Coastguard Worker 		 * The Prism header is in theory variable-length, but in
1353*8b26181fSAndroid Build Coastguard Worker 		 * practice it's always 144 bytes long.  However, some
1354*8b26181fSAndroid Build Coastguard Worker 		 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1355*8b26181fSAndroid Build Coastguard Worker 		 * sometimes or always supply an AVS header, so we
1356*8b26181fSAndroid Build Coastguard Worker 		 * have to check whether the radio header is a Prism
1357*8b26181fSAndroid Build Coastguard Worker 		 * header or an AVS header, so, in practice, it's
1358*8b26181fSAndroid Build Coastguard Worker 		 * variable-length.
1359*8b26181fSAndroid Build Coastguard Worker 		 */
1360*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 24;
1361*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 0;	/* link-layer header is variable-length */
1362*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.is_variable = 1;
1363*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 8;		/* 802.2+SNAP */
1364*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.2 */
1365*8b26181fSAndroid Build Coastguard Worker 		break;
1366*8b26181fSAndroid Build Coastguard Worker 
1367*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPI:
1368*8b26181fSAndroid Build Coastguard Worker 		/*
1369*8b26181fSAndroid Build Coastguard Worker 		 * At the moment we treat PPI the same way that we treat
1370*8b26181fSAndroid Build Coastguard Worker 		 * normal Radiotap encoded packets. The difference is in
1371*8b26181fSAndroid Build Coastguard Worker 		 * the function that generates the code at the beginning
1372*8b26181fSAndroid Build Coastguard Worker 		 * to compute the header length.  Since this code generator
1373*8b26181fSAndroid Build Coastguard Worker 		 * of PPI supports bare 802.11 encapsulation only (i.e.
1374*8b26181fSAndroid Build Coastguard Worker 		 * the encapsulated DLT should be DLT_IEEE802_11) we
1375*8b26181fSAndroid Build Coastguard Worker 		 * generate code to check for this too.
1376*8b26181fSAndroid Build Coastguard Worker 		 */
1377*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 24;
1378*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 0;	/* link-layer header is variable-length */
1379*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.is_variable = 1;
1380*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkhdr.is_variable = 1;
1381*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 8;		/* 802.2+SNAP */
1382*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.2 */
1383*8b26181fSAndroid Build Coastguard Worker 		break;
1384*8b26181fSAndroid Build Coastguard Worker 
1385*8b26181fSAndroid Build Coastguard Worker 	case DLT_ATM_RFC1483:
1386*8b26181fSAndroid Build Coastguard Worker 	case DLT_ATM_CLIP:	/* Linux ATM defines this */
1387*8b26181fSAndroid Build Coastguard Worker 		/*
1388*8b26181fSAndroid Build Coastguard Worker 		 * assume routed, non-ISO PDUs
1389*8b26181fSAndroid Build Coastguard Worker 		 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1390*8b26181fSAndroid Build Coastguard Worker 		 *
1391*8b26181fSAndroid Build Coastguard Worker 		 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1392*8b26181fSAndroid Build Coastguard Worker 		 * or PPP with the PPP NLPID (e.g., PPPoA)?  The
1393*8b26181fSAndroid Build Coastguard Worker 		 * latter would presumably be treated the way PPPoE
1394*8b26181fSAndroid Build Coastguard Worker 		 * should be, so you can do "pppoe and udp port 2049"
1395*8b26181fSAndroid Build Coastguard Worker 		 * or "pppoa and tcp port 80" and have it check for
1396*8b26181fSAndroid Build Coastguard Worker 		 * PPPo{A,E} and a PPP protocol of IP and....
1397*8b26181fSAndroid Build Coastguard Worker 		 */
1398*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 0;
1399*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 0;	/* packet begins with LLC header */
1400*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 8;		/* 802.2+SNAP */
1401*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.2 */
1402*8b26181fSAndroid Build Coastguard Worker 		break;
1403*8b26181fSAndroid Build Coastguard Worker 
1404*8b26181fSAndroid Build Coastguard Worker 	case DLT_SUNATM:
1405*8b26181fSAndroid Build Coastguard Worker 		/*
1406*8b26181fSAndroid Build Coastguard Worker 		 * Full Frontal ATM; you get AALn PDUs with an ATM
1407*8b26181fSAndroid Build Coastguard Worker 		 * pseudo-header.
1408*8b26181fSAndroid Build Coastguard Worker 		 */
1409*8b26181fSAndroid Build Coastguard Worker 		cstate->is_atm = 1;
1410*8b26181fSAndroid Build Coastguard Worker 		cstate->off_vpi = SUNATM_VPI_POS;
1411*8b26181fSAndroid Build Coastguard Worker 		cstate->off_vci = SUNATM_VCI_POS;
1412*8b26181fSAndroid Build Coastguard Worker 		cstate->off_proto = PROTO_POS;
1413*8b26181fSAndroid Build Coastguard Worker 		cstate->off_payload = SUNATM_PKT_BEGIN_POS;
1414*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = cstate->off_payload;
1415*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = cstate->off_payload;	/* if LLC-encapsulated */
1416*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 8;		/* 802.2+SNAP */
1417*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.2 */
1418*8b26181fSAndroid Build Coastguard Worker 		break;
1419*8b26181fSAndroid Build Coastguard Worker 
1420*8b26181fSAndroid Build Coastguard Worker 	case DLT_RAW:
1421*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPV4:
1422*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPV6:
1423*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1424*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 0;
1425*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1426*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1427*8b26181fSAndroid Build Coastguard Worker 		break;
1428*8b26181fSAndroid Build Coastguard Worker 
1429*8b26181fSAndroid Build Coastguard Worker 	case DLT_LINUX_SLL:	/* fake header for Linux cooked socket v1 */
1430*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 14;
1431*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 16;
1432*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1433*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1434*8b26181fSAndroid Build Coastguard Worker 		break;
1435*8b26181fSAndroid Build Coastguard Worker 
1436*8b26181fSAndroid Build Coastguard Worker 	case DLT_LINUX_SLL2:	/* fake header for Linux cooked socket v2 */
1437*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 0;
1438*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 20;
1439*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1440*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1441*8b26181fSAndroid Build Coastguard Worker 		break;
1442*8b26181fSAndroid Build Coastguard Worker 
1443*8b26181fSAndroid Build Coastguard Worker 	case DLT_LTALK:
1444*8b26181fSAndroid Build Coastguard Worker 		/*
1445*8b26181fSAndroid Build Coastguard Worker 		 * LocalTalk does have a 1-byte type field in the LLAP header,
1446*8b26181fSAndroid Build Coastguard Worker 		 * but really it just indicates whether there is a "short" or
1447*8b26181fSAndroid Build Coastguard Worker 		 * "long" DDP packet following.
1448*8b26181fSAndroid Build Coastguard Worker 		 */
1449*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1450*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 0;
1451*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1452*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1453*8b26181fSAndroid Build Coastguard Worker 		break;
1454*8b26181fSAndroid Build Coastguard Worker 
1455*8b26181fSAndroid Build Coastguard Worker 	case DLT_IP_OVER_FC:
1456*8b26181fSAndroid Build Coastguard Worker 		/*
1457*8b26181fSAndroid Build Coastguard Worker 		 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1458*8b26181fSAndroid Build Coastguard Worker 		 * link-level type field.  We set "off_linktype" to the
1459*8b26181fSAndroid Build Coastguard Worker 		 * offset of the LLC header.
1460*8b26181fSAndroid Build Coastguard Worker 		 *
1461*8b26181fSAndroid Build Coastguard Worker 		 * To check for Ethernet types, we assume that SSAP = SNAP
1462*8b26181fSAndroid Build Coastguard Worker 		 * is being used and pick out the encapsulated Ethernet type.
1463*8b26181fSAndroid Build Coastguard Worker 		 * XXX - should we generate code to check for SNAP? RFC
1464*8b26181fSAndroid Build Coastguard Worker 		 * 2625 says SNAP should be used.
1465*8b26181fSAndroid Build Coastguard Worker 		 */
1466*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 16;
1467*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 16;
1468*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 8;		/* 802.2+SNAP */
1469*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.2 */
1470*8b26181fSAndroid Build Coastguard Worker 		break;
1471*8b26181fSAndroid Build Coastguard Worker 
1472*8b26181fSAndroid Build Coastguard Worker 	case DLT_FRELAY:
1473*8b26181fSAndroid Build Coastguard Worker 		/*
1474*8b26181fSAndroid Build Coastguard Worker 		 * XXX - we should set this to handle SNAP-encapsulated
1475*8b26181fSAndroid Build Coastguard Worker 		 * frames (NLPID of 0x80).
1476*8b26181fSAndroid Build Coastguard Worker 		 */
1477*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1478*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 0;
1479*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1480*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1481*8b26181fSAndroid Build Coastguard Worker 		break;
1482*8b26181fSAndroid Build Coastguard Worker 
1483*8b26181fSAndroid Build Coastguard Worker                 /*
1484*8b26181fSAndroid Build Coastguard Worker                  * the only BPF-interesting FRF.16 frames are non-control frames;
1485*8b26181fSAndroid Build Coastguard Worker                  * Frame Relay has a variable length link-layer
1486*8b26181fSAndroid Build Coastguard Worker                  * so lets start with offset 4 for now and increments later on (FIXME);
1487*8b26181fSAndroid Build Coastguard Worker                  */
1488*8b26181fSAndroid Build Coastguard Worker 	case DLT_MFR:
1489*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1490*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 0;
1491*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 4;
1492*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* XXX - for now -> no 802.2 LLC */
1493*8b26181fSAndroid Build Coastguard Worker 		break;
1494*8b26181fSAndroid Build Coastguard Worker 
1495*8b26181fSAndroid Build Coastguard Worker 	case DLT_APPLE_IP_OVER_IEEE1394:
1496*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 16;
1497*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 18;
1498*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1499*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1500*8b26181fSAndroid Build Coastguard Worker 		break;
1501*8b26181fSAndroid Build Coastguard Worker 
1502*8b26181fSAndroid Build Coastguard Worker 	case DLT_SYMANTEC_FIREWALL:
1503*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 6;
1504*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 44;
1505*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;		/* Ethernet II */
1506*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* XXX - what does it do with 802.3 packets? */
1507*8b26181fSAndroid Build Coastguard Worker 		break;
1508*8b26181fSAndroid Build Coastguard Worker 
1509*8b26181fSAndroid Build Coastguard Worker 	case DLT_PFLOG:
1510*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 0;
1511*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 0;	/* link-layer header is variable-length */
1512*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.is_variable = 1;
1513*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1514*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
1515*8b26181fSAndroid Build Coastguard Worker 		break;
1516*8b26181fSAndroid Build Coastguard Worker 
1517*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MFR:
1518*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MLFR:
1519*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MLPPP:
1520*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_PPP:
1521*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_CHDLC:
1522*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_FRELAY:
1523*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 4;
1524*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 4;
1525*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1526*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;	/* no 802.2 LLC */
1527*8b26181fSAndroid Build Coastguard Worker                 break;
1528*8b26181fSAndroid Build Coastguard Worker 
1529*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM1:
1530*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 4;		/* in reality variable between 4-8 */
1531*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 4;	/* in reality variable between 4-8 */
1532*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1533*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 10;
1534*8b26181fSAndroid Build Coastguard Worker 		break;
1535*8b26181fSAndroid Build Coastguard Worker 
1536*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM2:
1537*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 8;		/* in reality variable between 8-12 */
1538*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 8;	/* in reality variable between 8-12 */
1539*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1540*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 10;
1541*8b26181fSAndroid Build Coastguard Worker 		break;
1542*8b26181fSAndroid Build Coastguard Worker 
1543*8b26181fSAndroid Build Coastguard Worker 		/* frames captured on a Juniper PPPoE service PIC
1544*8b26181fSAndroid Build Coastguard Worker 		 * contain raw ethernet frames */
1545*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_PPPOE:
1546*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ETHER:
1547*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 14;
1548*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 16;
1549*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 18;		/* Ethernet II */
1550*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 21;	/* 802.3+802.2 */
1551*8b26181fSAndroid Build Coastguard Worker 		break;
1552*8b26181fSAndroid Build Coastguard Worker 
1553*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_PPPOE_ATM:
1554*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 4;
1555*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 6;
1556*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1557*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;	/* no 802.2 LLC */
1558*8b26181fSAndroid Build Coastguard Worker 		break;
1559*8b26181fSAndroid Build Coastguard Worker 
1560*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_GGSN:
1561*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 6;
1562*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 12;
1563*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1564*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;	/* no 802.2 LLC */
1565*8b26181fSAndroid Build Coastguard Worker 		break;
1566*8b26181fSAndroid Build Coastguard Worker 
1567*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ES:
1568*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 6;
1569*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;	/* not really a network layer but raw IP addresses */
1570*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;	/* not really a network layer but raw IP addresses */
1571*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;	/* no 802.2 LLC */
1572*8b26181fSAndroid Build Coastguard Worker 		break;
1573*8b26181fSAndroid Build Coastguard Worker 
1574*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_MONITOR:
1575*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 12;
1576*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 12;
1577*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;			/* raw IP/IP6 header */
1578*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;	/* no 802.2 LLC */
1579*8b26181fSAndroid Build Coastguard Worker 		break;
1580*8b26181fSAndroid Build Coastguard Worker 
1581*8b26181fSAndroid Build Coastguard Worker 	case DLT_BACNET_MS_TP:
1582*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1583*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1584*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;
1585*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1586*8b26181fSAndroid Build Coastguard Worker 		break;
1587*8b26181fSAndroid Build Coastguard Worker 
1588*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_SERVICES:
1589*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 12;
1590*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;	/* L3 proto location dep. on cookie type */
1591*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;	/* L3 proto location dep. on cookie type */
1592*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;	/* no 802.2 LLC */
1593*8b26181fSAndroid Build Coastguard Worker 		break;
1594*8b26181fSAndroid Build Coastguard Worker 
1595*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_VP:
1596*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 18;
1597*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1598*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;
1599*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1600*8b26181fSAndroid Build Coastguard Worker 		break;
1601*8b26181fSAndroid Build Coastguard Worker 
1602*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ST:
1603*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 18;
1604*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1605*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;
1606*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1607*8b26181fSAndroid Build Coastguard Worker 		break;
1608*8b26181fSAndroid Build Coastguard Worker 
1609*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ISM:
1610*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 8;
1611*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1612*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;
1613*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1614*8b26181fSAndroid Build Coastguard Worker 		break;
1615*8b26181fSAndroid Build Coastguard Worker 
1616*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_VS:
1617*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_SRX_E2E:
1618*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_FIBRECHANNEL:
1619*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM_CEMIC:
1620*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 8;
1621*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1622*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;
1623*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1624*8b26181fSAndroid Build Coastguard Worker 		break;
1625*8b26181fSAndroid Build Coastguard Worker 
1626*8b26181fSAndroid Build Coastguard Worker 	case DLT_MTP2:
1627*8b26181fSAndroid Build Coastguard Worker 		cstate->off_li = 2;
1628*8b26181fSAndroid Build Coastguard Worker 		cstate->off_li_hsl = 4;
1629*8b26181fSAndroid Build Coastguard Worker 		cstate->off_sio = 3;
1630*8b26181fSAndroid Build Coastguard Worker 		cstate->off_opc = 4;
1631*8b26181fSAndroid Build Coastguard Worker 		cstate->off_dpc = 4;
1632*8b26181fSAndroid Build Coastguard Worker 		cstate->off_sls = 7;
1633*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1634*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1635*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;
1636*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1637*8b26181fSAndroid Build Coastguard Worker 		break;
1638*8b26181fSAndroid Build Coastguard Worker 
1639*8b26181fSAndroid Build Coastguard Worker 	case DLT_MTP2_WITH_PHDR:
1640*8b26181fSAndroid Build Coastguard Worker 		cstate->off_li = 6;
1641*8b26181fSAndroid Build Coastguard Worker 		cstate->off_li_hsl = 8;
1642*8b26181fSAndroid Build Coastguard Worker 		cstate->off_sio = 7;
1643*8b26181fSAndroid Build Coastguard Worker 		cstate->off_opc = 8;
1644*8b26181fSAndroid Build Coastguard Worker 		cstate->off_dpc = 8;
1645*8b26181fSAndroid Build Coastguard Worker 		cstate->off_sls = 11;
1646*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1647*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1648*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;
1649*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1650*8b26181fSAndroid Build Coastguard Worker 		break;
1651*8b26181fSAndroid Build Coastguard Worker 
1652*8b26181fSAndroid Build Coastguard Worker 	case DLT_ERF:
1653*8b26181fSAndroid Build Coastguard Worker 		cstate->off_li = 22;
1654*8b26181fSAndroid Build Coastguard Worker 		cstate->off_li_hsl = 24;
1655*8b26181fSAndroid Build Coastguard Worker 		cstate->off_sio = 23;
1656*8b26181fSAndroid Build Coastguard Worker 		cstate->off_opc = 24;
1657*8b26181fSAndroid Build Coastguard Worker 		cstate->off_dpc = 24;
1658*8b26181fSAndroid Build Coastguard Worker 		cstate->off_sls = 27;
1659*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1660*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1661*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;
1662*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1663*8b26181fSAndroid Build Coastguard Worker 		break;
1664*8b26181fSAndroid Build Coastguard Worker 
1665*8b26181fSAndroid Build Coastguard Worker 	case DLT_PFSYNC:
1666*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1667*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 4;
1668*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1669*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 0;
1670*8b26181fSAndroid Build Coastguard Worker 		break;
1671*8b26181fSAndroid Build Coastguard Worker 
1672*8b26181fSAndroid Build Coastguard Worker 	case DLT_AX25_KISS:
1673*8b26181fSAndroid Build Coastguard Worker 		/*
1674*8b26181fSAndroid Build Coastguard Worker 		 * Currently, only raw "link[N:M]" filtering is supported.
1675*8b26181fSAndroid Build Coastguard Worker 		 */
1676*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = OFFSET_NOT_SET;	/* variable, min 15, max 71 steps of 7 */
1677*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1678*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = OFFSET_NOT_SET;	/* variable, min 16, max 71 steps of 7 */
1679*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;	/* no 802.2 LLC */
1680*8b26181fSAndroid Build Coastguard Worker 		break;
1681*8b26181fSAndroid Build Coastguard Worker 
1682*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPNET:
1683*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = 1;
1684*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = 24;	/* ipnet header length */
1685*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;
1686*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = OFFSET_NOT_SET;
1687*8b26181fSAndroid Build Coastguard Worker 		break;
1688*8b26181fSAndroid Build Coastguard Worker 
1689*8b26181fSAndroid Build Coastguard Worker 	case DLT_NETANALYZER:
1690*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkhdr.constant_part = 4;	/* Ethernet header is past 4-byte pseudo-header */
1691*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
1692*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;	/* pseudo-header+Ethernet header length */
1693*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;		/* Ethernet II */
1694*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.3+802.2 */
1695*8b26181fSAndroid Build Coastguard Worker 		break;
1696*8b26181fSAndroid Build Coastguard Worker 
1697*8b26181fSAndroid Build Coastguard Worker 	case DLT_NETANALYZER_TRANSPARENT:
1698*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkhdr.constant_part = 12;	/* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1699*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
1700*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;	/* pseudo-header+preamble+SFD+Ethernet header length */
1701*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;		/* Ethernet II */
1702*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;	/* 802.3+802.2 */
1703*8b26181fSAndroid Build Coastguard Worker 		break;
1704*8b26181fSAndroid Build Coastguard Worker 
1705*8b26181fSAndroid Build Coastguard Worker 	default:
1706*8b26181fSAndroid Build Coastguard Worker 		/*
1707*8b26181fSAndroid Build Coastguard Worker 		 * For values in the range in which we've assigned new
1708*8b26181fSAndroid Build Coastguard Worker 		 * DLT_ values, only raw "link[N:M]" filtering is supported.
1709*8b26181fSAndroid Build Coastguard Worker 		 */
1710*8b26181fSAndroid Build Coastguard Worker 		if (cstate->linktype >= DLT_MATCHING_MIN &&
1711*8b26181fSAndroid Build Coastguard Worker 		    cstate->linktype <= DLT_MATCHING_MAX) {
1712*8b26181fSAndroid Build Coastguard Worker 			cstate->off_linktype.constant_part = OFFSET_NOT_SET;
1713*8b26181fSAndroid Build Coastguard Worker 			cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
1714*8b26181fSAndroid Build Coastguard Worker 			cstate->off_nl = OFFSET_NOT_SET;
1715*8b26181fSAndroid Build Coastguard Worker 			cstate->off_nl_nosnap = OFFSET_NOT_SET;
1716*8b26181fSAndroid Build Coastguard Worker 		} else {
1717*8b26181fSAndroid Build Coastguard Worker 			bpf_set_error(cstate, "unknown data link type %d (min %d, max %d)",
1718*8b26181fSAndroid Build Coastguard Worker 			    cstate->linktype, DLT_MATCHING_MIN, DLT_MATCHING_MAX);
1719*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1720*8b26181fSAndroid Build Coastguard Worker 		}
1721*8b26181fSAndroid Build Coastguard Worker 		break;
1722*8b26181fSAndroid Build Coastguard Worker 	}
1723*8b26181fSAndroid Build Coastguard Worker 
1724*8b26181fSAndroid Build Coastguard Worker 	cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr;
1725*8b26181fSAndroid Build Coastguard Worker 	return (0);
1726*8b26181fSAndroid Build Coastguard Worker }
1727*8b26181fSAndroid Build Coastguard Worker 
1728*8b26181fSAndroid Build Coastguard Worker /*
1729*8b26181fSAndroid Build Coastguard Worker  * Load a value relative to the specified absolute offset.
1730*8b26181fSAndroid Build Coastguard Worker  */
1731*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_load_absoffsetrel(compiler_state_t * cstate,bpf_abs_offset * abs_offset,u_int offset,u_int size)1732*8b26181fSAndroid Build Coastguard Worker gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset,
1733*8b26181fSAndroid Build Coastguard Worker     u_int offset, u_int size)
1734*8b26181fSAndroid Build Coastguard Worker {
1735*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s2;
1736*8b26181fSAndroid Build Coastguard Worker 
1737*8b26181fSAndroid Build Coastguard Worker 	s = gen_abs_offset_varpart(cstate, abs_offset);
1738*8b26181fSAndroid Build Coastguard Worker 
1739*8b26181fSAndroid Build Coastguard Worker 	/*
1740*8b26181fSAndroid Build Coastguard Worker 	 * If "s" is non-null, it has code to arrange that the X register
1741*8b26181fSAndroid Build Coastguard Worker 	 * contains the variable part of the absolute offset, so we
1742*8b26181fSAndroid Build Coastguard Worker 	 * generate a load relative to that, with an offset of
1743*8b26181fSAndroid Build Coastguard Worker 	 * abs_offset->constant_part + offset.
1744*8b26181fSAndroid Build Coastguard Worker 	 *
1745*8b26181fSAndroid Build Coastguard Worker 	 * Otherwise, we can do an absolute load with an offset of
1746*8b26181fSAndroid Build Coastguard Worker 	 * abs_offset->constant_part + offset.
1747*8b26181fSAndroid Build Coastguard Worker 	 */
1748*8b26181fSAndroid Build Coastguard Worker 	if (s != NULL) {
1749*8b26181fSAndroid Build Coastguard Worker 		/*
1750*8b26181fSAndroid Build Coastguard Worker 		 * "s" points to a list of statements that puts the
1751*8b26181fSAndroid Build Coastguard Worker 		 * variable part of the absolute offset into the X register.
1752*8b26181fSAndroid Build Coastguard Worker 		 * Do an indirect load, to use the X register as an offset.
1753*8b26181fSAndroid Build Coastguard Worker 		 */
1754*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
1755*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = abs_offset->constant_part + offset;
1756*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
1757*8b26181fSAndroid Build Coastguard Worker 	} else {
1758*8b26181fSAndroid Build Coastguard Worker 		/*
1759*8b26181fSAndroid Build Coastguard Worker 		 * There is no variable part of the absolute offset, so
1760*8b26181fSAndroid Build Coastguard Worker 		 * just do an absolute load.
1761*8b26181fSAndroid Build Coastguard Worker 		 */
1762*8b26181fSAndroid Build Coastguard Worker 		s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
1763*8b26181fSAndroid Build Coastguard Worker 		s->s.k = abs_offset->constant_part + offset;
1764*8b26181fSAndroid Build Coastguard Worker 	}
1765*8b26181fSAndroid Build Coastguard Worker 	return s;
1766*8b26181fSAndroid Build Coastguard Worker }
1767*8b26181fSAndroid Build Coastguard Worker 
1768*8b26181fSAndroid Build Coastguard Worker /*
1769*8b26181fSAndroid Build Coastguard Worker  * Load a value relative to the beginning of the specified header.
1770*8b26181fSAndroid Build Coastguard Worker  */
1771*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_load_a(compiler_state_t * cstate,enum e_offrel offrel,u_int offset,u_int size)1772*8b26181fSAndroid Build Coastguard Worker gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
1773*8b26181fSAndroid Build Coastguard Worker     u_int size)
1774*8b26181fSAndroid Build Coastguard Worker {
1775*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s2;
1776*8b26181fSAndroid Build Coastguard Worker 
1777*8b26181fSAndroid Build Coastguard Worker 	/*
1778*8b26181fSAndroid Build Coastguard Worker 	 * Squelch warnings from compilers that *don't* assume that
1779*8b26181fSAndroid Build Coastguard Worker 	 * offrel always has a valid enum value and therefore don't
1780*8b26181fSAndroid Build Coastguard Worker 	 * assume that we'll always go through one of the case arms.
1781*8b26181fSAndroid Build Coastguard Worker 	 *
1782*8b26181fSAndroid Build Coastguard Worker 	 * If we have a default case, compilers that *do* assume that
1783*8b26181fSAndroid Build Coastguard Worker 	 * will then complain about the default case code being
1784*8b26181fSAndroid Build Coastguard Worker 	 * unreachable.
1785*8b26181fSAndroid Build Coastguard Worker 	 *
1786*8b26181fSAndroid Build Coastguard Worker 	 * Damned if you do, damned if you don't.
1787*8b26181fSAndroid Build Coastguard Worker 	 */
1788*8b26181fSAndroid Build Coastguard Worker 	s = NULL;
1789*8b26181fSAndroid Build Coastguard Worker 
1790*8b26181fSAndroid Build Coastguard Worker 	switch (offrel) {
1791*8b26181fSAndroid Build Coastguard Worker 
1792*8b26181fSAndroid Build Coastguard Worker 	case OR_PACKET:
1793*8b26181fSAndroid Build Coastguard Worker                 s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
1794*8b26181fSAndroid Build Coastguard Worker                 s->s.k = offset;
1795*8b26181fSAndroid Build Coastguard Worker 		break;
1796*8b26181fSAndroid Build Coastguard Worker 
1797*8b26181fSAndroid Build Coastguard Worker 	case OR_LINKHDR:
1798*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size);
1799*8b26181fSAndroid Build Coastguard Worker 		break;
1800*8b26181fSAndroid Build Coastguard Worker 
1801*8b26181fSAndroid Build Coastguard Worker 	case OR_PREVLINKHDR:
1802*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size);
1803*8b26181fSAndroid Build Coastguard Worker 		break;
1804*8b26181fSAndroid Build Coastguard Worker 
1805*8b26181fSAndroid Build Coastguard Worker 	case OR_LLC:
1806*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size);
1807*8b26181fSAndroid Build Coastguard Worker 		break;
1808*8b26181fSAndroid Build Coastguard Worker 
1809*8b26181fSAndroid Build Coastguard Worker 	case OR_PREVMPLSHDR:
1810*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size);
1811*8b26181fSAndroid Build Coastguard Worker 		break;
1812*8b26181fSAndroid Build Coastguard Worker 
1813*8b26181fSAndroid Build Coastguard Worker 	case OR_LINKPL:
1814*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size);
1815*8b26181fSAndroid Build Coastguard Worker 		break;
1816*8b26181fSAndroid Build Coastguard Worker 
1817*8b26181fSAndroid Build Coastguard Worker 	case OR_LINKPL_NOSNAP:
1818*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size);
1819*8b26181fSAndroid Build Coastguard Worker 		break;
1820*8b26181fSAndroid Build Coastguard Worker 
1821*8b26181fSAndroid Build Coastguard Worker 	case OR_LINKTYPE:
1822*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size);
1823*8b26181fSAndroid Build Coastguard Worker 		break;
1824*8b26181fSAndroid Build Coastguard Worker 
1825*8b26181fSAndroid Build Coastguard Worker 	case OR_TRAN_IPV4:
1826*8b26181fSAndroid Build Coastguard Worker 		/*
1827*8b26181fSAndroid Build Coastguard Worker 		 * Load the X register with the length of the IPv4 header
1828*8b26181fSAndroid Build Coastguard Worker 		 * (plus the offset of the link-layer header, if it's
1829*8b26181fSAndroid Build Coastguard Worker 		 * preceded by a variable-length header such as a radio
1830*8b26181fSAndroid Build Coastguard Worker 		 * header), in bytes.
1831*8b26181fSAndroid Build Coastguard Worker 		 */
1832*8b26181fSAndroid Build Coastguard Worker 		s = gen_loadx_iphdrlen(cstate);
1833*8b26181fSAndroid Build Coastguard Worker 
1834*8b26181fSAndroid Build Coastguard Worker 		/*
1835*8b26181fSAndroid Build Coastguard Worker 		 * Load the item at {offset of the link-layer payload} +
1836*8b26181fSAndroid Build Coastguard Worker 		 * {offset, relative to the start of the link-layer
1837*8b26181fSAndroid Build Coastguard Worker 		 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1838*8b26181fSAndroid Build Coastguard Worker 		 * {specified offset}.
1839*8b26181fSAndroid Build Coastguard Worker 		 *
1840*8b26181fSAndroid Build Coastguard Worker 		 * If the offset of the link-layer payload is variable,
1841*8b26181fSAndroid Build Coastguard Worker 		 * the variable part of that offset is included in the
1842*8b26181fSAndroid Build Coastguard Worker 		 * value in the X register, and we include the constant
1843*8b26181fSAndroid Build Coastguard Worker 		 * part in the offset of the load.
1844*8b26181fSAndroid Build Coastguard Worker 		 */
1845*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
1846*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset;
1847*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
1848*8b26181fSAndroid Build Coastguard Worker 		break;
1849*8b26181fSAndroid Build Coastguard Worker 
1850*8b26181fSAndroid Build Coastguard Worker 	case OR_TRAN_IPV6:
1851*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size);
1852*8b26181fSAndroid Build Coastguard Worker 		break;
1853*8b26181fSAndroid Build Coastguard Worker 	}
1854*8b26181fSAndroid Build Coastguard Worker 	return s;
1855*8b26181fSAndroid Build Coastguard Worker }
1856*8b26181fSAndroid Build Coastguard Worker 
1857*8b26181fSAndroid Build Coastguard Worker /*
1858*8b26181fSAndroid Build Coastguard Worker  * Generate code to load into the X register the sum of the length of
1859*8b26181fSAndroid Build Coastguard Worker  * the IPv4 header and the variable part of the offset of the link-layer
1860*8b26181fSAndroid Build Coastguard Worker  * payload.
1861*8b26181fSAndroid Build Coastguard Worker  */
1862*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_loadx_iphdrlen(compiler_state_t * cstate)1863*8b26181fSAndroid Build Coastguard Worker gen_loadx_iphdrlen(compiler_state_t *cstate)
1864*8b26181fSAndroid Build Coastguard Worker {
1865*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s2;
1866*8b26181fSAndroid Build Coastguard Worker 
1867*8b26181fSAndroid Build Coastguard Worker 	s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
1868*8b26181fSAndroid Build Coastguard Worker 	if (s != NULL) {
1869*8b26181fSAndroid Build Coastguard Worker 		/*
1870*8b26181fSAndroid Build Coastguard Worker 		 * The offset of the link-layer payload has a variable
1871*8b26181fSAndroid Build Coastguard Worker 		 * part.  "s" points to a list of statements that put
1872*8b26181fSAndroid Build Coastguard Worker 		 * the variable part of that offset into the X register.
1873*8b26181fSAndroid Build Coastguard Worker 		 *
1874*8b26181fSAndroid Build Coastguard Worker 		 * The 4*([k]&0xf) addressing mode can't be used, as we
1875*8b26181fSAndroid Build Coastguard Worker 		 * don't have a constant offset, so we have to load the
1876*8b26181fSAndroid Build Coastguard Worker 		 * value in question into the A register and add to it
1877*8b26181fSAndroid Build Coastguard Worker 		 * the value from the X register.
1878*8b26181fSAndroid Build Coastguard Worker 		 */
1879*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
1880*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
1881*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
1882*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
1883*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 0xf;
1884*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
1885*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
1886*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 2;
1887*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
1888*8b26181fSAndroid Build Coastguard Worker 
1889*8b26181fSAndroid Build Coastguard Worker 		/*
1890*8b26181fSAndroid Build Coastguard Worker 		 * The A register now contains the length of the IP header.
1891*8b26181fSAndroid Build Coastguard Worker 		 * We need to add to it the variable part of the offset of
1892*8b26181fSAndroid Build Coastguard Worker 		 * the link-layer payload, which is still in the X
1893*8b26181fSAndroid Build Coastguard Worker 		 * register, and move the result into the X register.
1894*8b26181fSAndroid Build Coastguard Worker 		 */
1895*8b26181fSAndroid Build Coastguard Worker 		sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
1896*8b26181fSAndroid Build Coastguard Worker 		sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
1897*8b26181fSAndroid Build Coastguard Worker 	} else {
1898*8b26181fSAndroid Build Coastguard Worker 		/*
1899*8b26181fSAndroid Build Coastguard Worker 		 * The offset of the link-layer payload is a constant,
1900*8b26181fSAndroid Build Coastguard Worker 		 * so no code was generated to load the (non-existent)
1901*8b26181fSAndroid Build Coastguard Worker 		 * variable part of that offset.
1902*8b26181fSAndroid Build Coastguard Worker 		 *
1903*8b26181fSAndroid Build Coastguard Worker 		 * This means we can use the 4*([k]&0xf) addressing
1904*8b26181fSAndroid Build Coastguard Worker 		 * mode.  Load the length of the IPv4 header, which
1905*8b26181fSAndroid Build Coastguard Worker 		 * is at an offset of cstate->off_nl from the beginning of
1906*8b26181fSAndroid Build Coastguard Worker 		 * the link-layer payload, and thus at an offset of
1907*8b26181fSAndroid Build Coastguard Worker 		 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1908*8b26181fSAndroid Build Coastguard Worker 		 * of the raw packet data, using that addressing mode.
1909*8b26181fSAndroid Build Coastguard Worker 		 */
1910*8b26181fSAndroid Build Coastguard Worker 		s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
1911*8b26181fSAndroid Build Coastguard Worker 		s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
1912*8b26181fSAndroid Build Coastguard Worker 	}
1913*8b26181fSAndroid Build Coastguard Worker 	return s;
1914*8b26181fSAndroid Build Coastguard Worker }
1915*8b26181fSAndroid Build Coastguard Worker 
1916*8b26181fSAndroid Build Coastguard Worker 
1917*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_uncond(compiler_state_t * cstate,int rsense)1918*8b26181fSAndroid Build Coastguard Worker gen_uncond(compiler_state_t *cstate, int rsense)
1919*8b26181fSAndroid Build Coastguard Worker {
1920*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
1921*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
1922*8b26181fSAndroid Build Coastguard Worker 
1923*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_LD|BPF_IMM);
1924*8b26181fSAndroid Build Coastguard Worker 	s->s.k = !rsense;
1925*8b26181fSAndroid Build Coastguard Worker 	b = new_block(cstate, JMP(BPF_JEQ));
1926*8b26181fSAndroid Build Coastguard Worker 	b->stmts = s;
1927*8b26181fSAndroid Build Coastguard Worker 
1928*8b26181fSAndroid Build Coastguard Worker 	return b;
1929*8b26181fSAndroid Build Coastguard Worker }
1930*8b26181fSAndroid Build Coastguard Worker 
1931*8b26181fSAndroid Build Coastguard Worker static inline struct block *
gen_true(compiler_state_t * cstate)1932*8b26181fSAndroid Build Coastguard Worker gen_true(compiler_state_t *cstate)
1933*8b26181fSAndroid Build Coastguard Worker {
1934*8b26181fSAndroid Build Coastguard Worker 	return gen_uncond(cstate, 1);
1935*8b26181fSAndroid Build Coastguard Worker }
1936*8b26181fSAndroid Build Coastguard Worker 
1937*8b26181fSAndroid Build Coastguard Worker static inline struct block *
gen_false(compiler_state_t * cstate)1938*8b26181fSAndroid Build Coastguard Worker gen_false(compiler_state_t *cstate)
1939*8b26181fSAndroid Build Coastguard Worker {
1940*8b26181fSAndroid Build Coastguard Worker 	return gen_uncond(cstate, 0);
1941*8b26181fSAndroid Build Coastguard Worker }
1942*8b26181fSAndroid Build Coastguard Worker 
1943*8b26181fSAndroid Build Coastguard Worker /*
1944*8b26181fSAndroid Build Coastguard Worker  * Byte-swap a 32-bit number.
1945*8b26181fSAndroid Build Coastguard Worker  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1946*8b26181fSAndroid Build Coastguard Worker  * big-endian platforms.)
1947*8b26181fSAndroid Build Coastguard Worker  */
1948*8b26181fSAndroid Build Coastguard Worker #define	SWAPLONG(y) \
1949*8b26181fSAndroid Build Coastguard Worker ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1950*8b26181fSAndroid Build Coastguard Worker 
1951*8b26181fSAndroid Build Coastguard Worker /*
1952*8b26181fSAndroid Build Coastguard Worker  * Generate code to match a particular packet type.
1953*8b26181fSAndroid Build Coastguard Worker  *
1954*8b26181fSAndroid Build Coastguard Worker  * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1955*8b26181fSAndroid Build Coastguard Worker  * value, if <= ETHERMTU.  We use that to determine whether to
1956*8b26181fSAndroid Build Coastguard Worker  * match the type/length field or to check the type/length field for
1957*8b26181fSAndroid Build Coastguard Worker  * a value <= ETHERMTU to see whether it's a type field and then do
1958*8b26181fSAndroid Build Coastguard Worker  * the appropriate test.
1959*8b26181fSAndroid Build Coastguard Worker  */
1960*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_ether_linktype(compiler_state_t * cstate,bpf_u_int32 ll_proto)1961*8b26181fSAndroid Build Coastguard Worker gen_ether_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
1962*8b26181fSAndroid Build Coastguard Worker {
1963*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
1964*8b26181fSAndroid Build Coastguard Worker 
1965*8b26181fSAndroid Build Coastguard Worker 	switch (ll_proto) {
1966*8b26181fSAndroid Build Coastguard Worker 
1967*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_ISONS:
1968*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_IP:
1969*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_NETBEUI:
1970*8b26181fSAndroid Build Coastguard Worker 		/*
1971*8b26181fSAndroid Build Coastguard Worker 		 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1972*8b26181fSAndroid Build Coastguard Worker 		 * so we check the DSAP and SSAP.
1973*8b26181fSAndroid Build Coastguard Worker 		 *
1974*8b26181fSAndroid Build Coastguard Worker 		 * LLCSAP_IP checks for IP-over-802.2, rather
1975*8b26181fSAndroid Build Coastguard Worker 		 * than IP-over-Ethernet or IP-over-SNAP.
1976*8b26181fSAndroid Build Coastguard Worker 		 *
1977*8b26181fSAndroid Build Coastguard Worker 		 * XXX - should we check both the DSAP and the
1978*8b26181fSAndroid Build Coastguard Worker 		 * SSAP, like this, or should we check just the
1979*8b26181fSAndroid Build Coastguard Worker 		 * DSAP, as we do for other types <= ETHERMTU
1980*8b26181fSAndroid Build Coastguard Worker 		 * (i.e., other SAP values)?
1981*8b26181fSAndroid Build Coastguard Worker 		 */
1982*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
1983*8b26181fSAndroid Build Coastguard Worker 		gen_not(b0);
1984*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);
1985*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
1986*8b26181fSAndroid Build Coastguard Worker 		return b1;
1987*8b26181fSAndroid Build Coastguard Worker 
1988*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_IPX:
1989*8b26181fSAndroid Build Coastguard Worker 		/*
1990*8b26181fSAndroid Build Coastguard Worker 		 * Check for;
1991*8b26181fSAndroid Build Coastguard Worker 		 *
1992*8b26181fSAndroid Build Coastguard Worker 		 *	Ethernet_II frames, which are Ethernet
1993*8b26181fSAndroid Build Coastguard Worker 		 *	frames with a frame type of ETHERTYPE_IPX;
1994*8b26181fSAndroid Build Coastguard Worker 		 *
1995*8b26181fSAndroid Build Coastguard Worker 		 *	Ethernet_802.3 frames, which are 802.3
1996*8b26181fSAndroid Build Coastguard Worker 		 *	frames (i.e., the type/length field is
1997*8b26181fSAndroid Build Coastguard Worker 		 *	a length field, <= ETHERMTU, rather than
1998*8b26181fSAndroid Build Coastguard Worker 		 *	a type field) with the first two bytes
1999*8b26181fSAndroid Build Coastguard Worker 		 *	after the Ethernet/802.3 header being
2000*8b26181fSAndroid Build Coastguard Worker 		 *	0xFFFF;
2001*8b26181fSAndroid Build Coastguard Worker 		 *
2002*8b26181fSAndroid Build Coastguard Worker 		 *	Ethernet_802.2 frames, which are 802.3
2003*8b26181fSAndroid Build Coastguard Worker 		 *	frames with an 802.2 LLC header and
2004*8b26181fSAndroid Build Coastguard Worker 		 *	with the IPX LSAP as the DSAP in the LLC
2005*8b26181fSAndroid Build Coastguard Worker 		 *	header;
2006*8b26181fSAndroid Build Coastguard Worker 		 *
2007*8b26181fSAndroid Build Coastguard Worker 		 *	Ethernet_SNAP frames, which are 802.3
2008*8b26181fSAndroid Build Coastguard Worker 		 *	frames with an LLC header and a SNAP
2009*8b26181fSAndroid Build Coastguard Worker 		 *	header and with an OUI of 0x000000
2010*8b26181fSAndroid Build Coastguard Worker 		 *	(encapsulated Ethernet) and a protocol
2011*8b26181fSAndroid Build Coastguard Worker 		 *	ID of ETHERTYPE_IPX in the SNAP header.
2012*8b26181fSAndroid Build Coastguard Worker 		 *
2013*8b26181fSAndroid Build Coastguard Worker 		 * XXX - should we generate the same code both
2014*8b26181fSAndroid Build Coastguard Worker 		 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2015*8b26181fSAndroid Build Coastguard Worker 		 */
2016*8b26181fSAndroid Build Coastguard Worker 
2017*8b26181fSAndroid Build Coastguard Worker 		/*
2018*8b26181fSAndroid Build Coastguard Worker 		 * This generates code to check both for the
2019*8b26181fSAndroid Build Coastguard Worker 		 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2020*8b26181fSAndroid Build Coastguard Worker 		 */
2021*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
2022*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
2023*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
2024*8b26181fSAndroid Build Coastguard Worker 
2025*8b26181fSAndroid Build Coastguard Worker 		/*
2026*8b26181fSAndroid Build Coastguard Worker 		 * Now we add code to check for SNAP frames with
2027*8b26181fSAndroid Build Coastguard Worker 		 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2028*8b26181fSAndroid Build Coastguard Worker 		 */
2029*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
2030*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
2031*8b26181fSAndroid Build Coastguard Worker 
2032*8b26181fSAndroid Build Coastguard Worker 		/*
2033*8b26181fSAndroid Build Coastguard Worker 		 * Now we generate code to check for 802.3
2034*8b26181fSAndroid Build Coastguard Worker 		 * frames in general.
2035*8b26181fSAndroid Build Coastguard Worker 		 */
2036*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2037*8b26181fSAndroid Build Coastguard Worker 		gen_not(b0);
2038*8b26181fSAndroid Build Coastguard Worker 
2039*8b26181fSAndroid Build Coastguard Worker 		/*
2040*8b26181fSAndroid Build Coastguard Worker 		 * Now add the check for 802.3 frames before the
2041*8b26181fSAndroid Build Coastguard Worker 		 * check for Ethernet_802.2 and Ethernet_802.3,
2042*8b26181fSAndroid Build Coastguard Worker 		 * as those checks should only be done on 802.3
2043*8b26181fSAndroid Build Coastguard Worker 		 * frames, not on Ethernet frames.
2044*8b26181fSAndroid Build Coastguard Worker 		 */
2045*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
2046*8b26181fSAndroid Build Coastguard Worker 
2047*8b26181fSAndroid Build Coastguard Worker 		/*
2048*8b26181fSAndroid Build Coastguard Worker 		 * Now add the check for Ethernet_II frames, and
2049*8b26181fSAndroid Build Coastguard Worker 		 * do that before checking for the other frame
2050*8b26181fSAndroid Build Coastguard Worker 		 * types.
2051*8b26181fSAndroid Build Coastguard Worker 		 */
2052*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);
2053*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
2054*8b26181fSAndroid Build Coastguard Worker 		return b1;
2055*8b26181fSAndroid Build Coastguard Worker 
2056*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_ATALK:
2057*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_AARP:
2058*8b26181fSAndroid Build Coastguard Worker 		/*
2059*8b26181fSAndroid Build Coastguard Worker 		 * EtherTalk (AppleTalk protocols on Ethernet link
2060*8b26181fSAndroid Build Coastguard Worker 		 * layer) may use 802.2 encapsulation.
2061*8b26181fSAndroid Build Coastguard Worker 		 */
2062*8b26181fSAndroid Build Coastguard Worker 
2063*8b26181fSAndroid Build Coastguard Worker 		/*
2064*8b26181fSAndroid Build Coastguard Worker 		 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2065*8b26181fSAndroid Build Coastguard Worker 		 * we check for an Ethernet type field less than
2066*8b26181fSAndroid Build Coastguard Worker 		 * 1500, which means it's an 802.3 length field.
2067*8b26181fSAndroid Build Coastguard Worker 		 */
2068*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2069*8b26181fSAndroid Build Coastguard Worker 		gen_not(b0);
2070*8b26181fSAndroid Build Coastguard Worker 
2071*8b26181fSAndroid Build Coastguard Worker 		/*
2072*8b26181fSAndroid Build Coastguard Worker 		 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2073*8b26181fSAndroid Build Coastguard Worker 		 * SNAP packets with an organization code of
2074*8b26181fSAndroid Build Coastguard Worker 		 * 0x080007 (Apple, for Appletalk) and a protocol
2075*8b26181fSAndroid Build Coastguard Worker 		 * type of ETHERTYPE_ATALK (Appletalk).
2076*8b26181fSAndroid Build Coastguard Worker 		 *
2077*8b26181fSAndroid Build Coastguard Worker 		 * 802.2-encapsulated ETHERTYPE_AARP packets are
2078*8b26181fSAndroid Build Coastguard Worker 		 * SNAP packets with an organization code of
2079*8b26181fSAndroid Build Coastguard Worker 		 * 0x000000 (encapsulated Ethernet) and a protocol
2080*8b26181fSAndroid Build Coastguard Worker 		 * type of ETHERTYPE_AARP (Appletalk ARP).
2081*8b26181fSAndroid Build Coastguard Worker 		 */
2082*8b26181fSAndroid Build Coastguard Worker 		if (ll_proto == ETHERTYPE_ATALK)
2083*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
2084*8b26181fSAndroid Build Coastguard Worker 		else	/* ll_proto == ETHERTYPE_AARP */
2085*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
2086*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
2087*8b26181fSAndroid Build Coastguard Worker 
2088*8b26181fSAndroid Build Coastguard Worker 		/*
2089*8b26181fSAndroid Build Coastguard Worker 		 * Check for Ethernet encapsulation (Ethertalk
2090*8b26181fSAndroid Build Coastguard Worker 		 * phase 1?); we just check for the Ethernet
2091*8b26181fSAndroid Build Coastguard Worker 		 * protocol type.
2092*8b26181fSAndroid Build Coastguard Worker 		 */
2093*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2094*8b26181fSAndroid Build Coastguard Worker 
2095*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
2096*8b26181fSAndroid Build Coastguard Worker 		return b1;
2097*8b26181fSAndroid Build Coastguard Worker 
2098*8b26181fSAndroid Build Coastguard Worker 	default:
2099*8b26181fSAndroid Build Coastguard Worker 		if (ll_proto <= ETHERMTU) {
2100*8b26181fSAndroid Build Coastguard Worker 			/*
2101*8b26181fSAndroid Build Coastguard Worker 			 * This is an LLC SAP value, so the frames
2102*8b26181fSAndroid Build Coastguard Worker 			 * that match would be 802.2 frames.
2103*8b26181fSAndroid Build Coastguard Worker 			 * Check that the frame is an 802.2 frame
2104*8b26181fSAndroid Build Coastguard Worker 			 * (i.e., that the length/type field is
2105*8b26181fSAndroid Build Coastguard Worker 			 * a length field, <= ETHERMTU) and
2106*8b26181fSAndroid Build Coastguard Worker 			 * then check the DSAP.
2107*8b26181fSAndroid Build Coastguard Worker 			 */
2108*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
2109*8b26181fSAndroid Build Coastguard Worker 			gen_not(b0);
2110*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, ll_proto);
2111*8b26181fSAndroid Build Coastguard Worker 			gen_and(b0, b1);
2112*8b26181fSAndroid Build Coastguard Worker 			return b1;
2113*8b26181fSAndroid Build Coastguard Worker 		} else {
2114*8b26181fSAndroid Build Coastguard Worker 			/*
2115*8b26181fSAndroid Build Coastguard Worker 			 * This is an Ethernet type, so compare
2116*8b26181fSAndroid Build Coastguard Worker 			 * the length/type field with it (if
2117*8b26181fSAndroid Build Coastguard Worker 			 * the frame is an 802.2 frame, the length
2118*8b26181fSAndroid Build Coastguard Worker 			 * field will be <= ETHERMTU, and, as
2119*8b26181fSAndroid Build Coastguard Worker 			 * "ll_proto" is > ETHERMTU, this test
2120*8b26181fSAndroid Build Coastguard Worker 			 * will fail and the frame won't match,
2121*8b26181fSAndroid Build Coastguard Worker 			 * which is what we want).
2122*8b26181fSAndroid Build Coastguard Worker 			 */
2123*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2124*8b26181fSAndroid Build Coastguard Worker 		}
2125*8b26181fSAndroid Build Coastguard Worker 	}
2126*8b26181fSAndroid Build Coastguard Worker }
2127*8b26181fSAndroid Build Coastguard Worker 
2128*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_loopback_linktype(compiler_state_t * cstate,bpf_u_int32 ll_proto)2129*8b26181fSAndroid Build Coastguard Worker gen_loopback_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2130*8b26181fSAndroid Build Coastguard Worker {
2131*8b26181fSAndroid Build Coastguard Worker 	/*
2132*8b26181fSAndroid Build Coastguard Worker 	 * For DLT_NULL, the link-layer header is a 32-bit word
2133*8b26181fSAndroid Build Coastguard Worker 	 * containing an AF_ value in *host* byte order, and for
2134*8b26181fSAndroid Build Coastguard Worker 	 * DLT_ENC, the link-layer header begins with a 32-bit
2135*8b26181fSAndroid Build Coastguard Worker 	 * word containing an AF_ value in host byte order.
2136*8b26181fSAndroid Build Coastguard Worker 	 *
2137*8b26181fSAndroid Build Coastguard Worker 	 * In addition, if we're reading a saved capture file,
2138*8b26181fSAndroid Build Coastguard Worker 	 * the host byte order in the capture may not be the
2139*8b26181fSAndroid Build Coastguard Worker 	 * same as the host byte order on this machine.
2140*8b26181fSAndroid Build Coastguard Worker 	 *
2141*8b26181fSAndroid Build Coastguard Worker 	 * For DLT_LOOP, the link-layer header is a 32-bit
2142*8b26181fSAndroid Build Coastguard Worker 	 * word containing an AF_ value in *network* byte order.
2143*8b26181fSAndroid Build Coastguard Worker 	 */
2144*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) {
2145*8b26181fSAndroid Build Coastguard Worker 		/*
2146*8b26181fSAndroid Build Coastguard Worker 		 * The AF_ value is in host byte order, but the BPF
2147*8b26181fSAndroid Build Coastguard Worker 		 * interpreter will convert it to network byte order.
2148*8b26181fSAndroid Build Coastguard Worker 		 *
2149*8b26181fSAndroid Build Coastguard Worker 		 * If this is a save file, and it's from a machine
2150*8b26181fSAndroid Build Coastguard Worker 		 * with the opposite byte order to ours, we byte-swap
2151*8b26181fSAndroid Build Coastguard Worker 		 * the AF_ value.
2152*8b26181fSAndroid Build Coastguard Worker 		 *
2153*8b26181fSAndroid Build Coastguard Worker 		 * Then we run it through "htonl()", and generate
2154*8b26181fSAndroid Build Coastguard Worker 		 * code to compare against the result.
2155*8b26181fSAndroid Build Coastguard Worker 		 */
2156*8b26181fSAndroid Build Coastguard Worker 		if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped)
2157*8b26181fSAndroid Build Coastguard Worker 			ll_proto = SWAPLONG(ll_proto);
2158*8b26181fSAndroid Build Coastguard Worker 		ll_proto = htonl(ll_proto);
2159*8b26181fSAndroid Build Coastguard Worker 	}
2160*8b26181fSAndroid Build Coastguard Worker 	return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, ll_proto));
2161*8b26181fSAndroid Build Coastguard Worker }
2162*8b26181fSAndroid Build Coastguard Worker 
2163*8b26181fSAndroid Build Coastguard Worker /*
2164*8b26181fSAndroid Build Coastguard Worker  * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2165*8b26181fSAndroid Build Coastguard Worker  * or IPv6 then we have an error.
2166*8b26181fSAndroid Build Coastguard Worker  */
2167*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_ipnet_linktype(compiler_state_t * cstate,bpf_u_int32 ll_proto)2168*8b26181fSAndroid Build Coastguard Worker gen_ipnet_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2169*8b26181fSAndroid Build Coastguard Worker {
2170*8b26181fSAndroid Build Coastguard Worker 	switch (ll_proto) {
2171*8b26181fSAndroid Build Coastguard Worker 
2172*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_IP:
2173*8b26181fSAndroid Build Coastguard Worker 		return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET);
2174*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
2175*8b26181fSAndroid Build Coastguard Worker 
2176*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_IPV6:
2177*8b26181fSAndroid Build Coastguard Worker 		return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET6);
2178*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
2179*8b26181fSAndroid Build Coastguard Worker 
2180*8b26181fSAndroid Build Coastguard Worker 	default:
2181*8b26181fSAndroid Build Coastguard Worker 		break;
2182*8b26181fSAndroid Build Coastguard Worker 	}
2183*8b26181fSAndroid Build Coastguard Worker 
2184*8b26181fSAndroid Build Coastguard Worker 	return gen_false(cstate);
2185*8b26181fSAndroid Build Coastguard Worker }
2186*8b26181fSAndroid Build Coastguard Worker 
2187*8b26181fSAndroid Build Coastguard Worker /*
2188*8b26181fSAndroid Build Coastguard Worker  * Generate code to match a particular packet type.
2189*8b26181fSAndroid Build Coastguard Worker  *
2190*8b26181fSAndroid Build Coastguard Worker  * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2191*8b26181fSAndroid Build Coastguard Worker  * value, if <= ETHERMTU.  We use that to determine whether to
2192*8b26181fSAndroid Build Coastguard Worker  * match the type field or to check the type field for the special
2193*8b26181fSAndroid Build Coastguard Worker  * LINUX_SLL_P_802_2 value and then do the appropriate test.
2194*8b26181fSAndroid Build Coastguard Worker  */
2195*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_linux_sll_linktype(compiler_state_t * cstate,bpf_u_int32 ll_proto)2196*8b26181fSAndroid Build Coastguard Worker gen_linux_sll_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
2197*8b26181fSAndroid Build Coastguard Worker {
2198*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
2199*8b26181fSAndroid Build Coastguard Worker 
2200*8b26181fSAndroid Build Coastguard Worker 	switch (ll_proto) {
2201*8b26181fSAndroid Build Coastguard Worker 
2202*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_ISONS:
2203*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_IP:
2204*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_NETBEUI:
2205*8b26181fSAndroid Build Coastguard Worker 		/*
2206*8b26181fSAndroid Build Coastguard Worker 		 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2207*8b26181fSAndroid Build Coastguard Worker 		 * so we check the DSAP and SSAP.
2208*8b26181fSAndroid Build Coastguard Worker 		 *
2209*8b26181fSAndroid Build Coastguard Worker 		 * LLCSAP_IP checks for IP-over-802.2, rather
2210*8b26181fSAndroid Build Coastguard Worker 		 * than IP-over-Ethernet or IP-over-SNAP.
2211*8b26181fSAndroid Build Coastguard Worker 		 *
2212*8b26181fSAndroid Build Coastguard Worker 		 * XXX - should we check both the DSAP and the
2213*8b26181fSAndroid Build Coastguard Worker 		 * SSAP, like this, or should we check just the
2214*8b26181fSAndroid Build Coastguard Worker 		 * DSAP, as we do for other types <= ETHERMTU
2215*8b26181fSAndroid Build Coastguard Worker 		 * (i.e., other SAP values)?
2216*8b26181fSAndroid Build Coastguard Worker 		 */
2217*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2218*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);
2219*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
2220*8b26181fSAndroid Build Coastguard Worker 		return b1;
2221*8b26181fSAndroid Build Coastguard Worker 
2222*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_IPX:
2223*8b26181fSAndroid Build Coastguard Worker 		/*
2224*8b26181fSAndroid Build Coastguard Worker 		 *	Ethernet_II frames, which are Ethernet
2225*8b26181fSAndroid Build Coastguard Worker 		 *	frames with a frame type of ETHERTYPE_IPX;
2226*8b26181fSAndroid Build Coastguard Worker 		 *
2227*8b26181fSAndroid Build Coastguard Worker 		 *	Ethernet_802.3 frames, which have a frame
2228*8b26181fSAndroid Build Coastguard Worker 		 *	type of LINUX_SLL_P_802_3;
2229*8b26181fSAndroid Build Coastguard Worker 		 *
2230*8b26181fSAndroid Build Coastguard Worker 		 *	Ethernet_802.2 frames, which are 802.3
2231*8b26181fSAndroid Build Coastguard Worker 		 *	frames with an 802.2 LLC header (i.e, have
2232*8b26181fSAndroid Build Coastguard Worker 		 *	a frame type of LINUX_SLL_P_802_2) and
2233*8b26181fSAndroid Build Coastguard Worker 		 *	with the IPX LSAP as the DSAP in the LLC
2234*8b26181fSAndroid Build Coastguard Worker 		 *	header;
2235*8b26181fSAndroid Build Coastguard Worker 		 *
2236*8b26181fSAndroid Build Coastguard Worker 		 *	Ethernet_SNAP frames, which are 802.3
2237*8b26181fSAndroid Build Coastguard Worker 		 *	frames with an LLC header and a SNAP
2238*8b26181fSAndroid Build Coastguard Worker 		 *	header and with an OUI of 0x000000
2239*8b26181fSAndroid Build Coastguard Worker 		 *	(encapsulated Ethernet) and a protocol
2240*8b26181fSAndroid Build Coastguard Worker 		 *	ID of ETHERTYPE_IPX in the SNAP header.
2241*8b26181fSAndroid Build Coastguard Worker 		 *
2242*8b26181fSAndroid Build Coastguard Worker 		 * First, do the checks on LINUX_SLL_P_802_2
2243*8b26181fSAndroid Build Coastguard Worker 		 * frames; generate the check for either
2244*8b26181fSAndroid Build Coastguard Worker 		 * Ethernet_802.2 or Ethernet_SNAP frames, and
2245*8b26181fSAndroid Build Coastguard Worker 		 * then put a check for LINUX_SLL_P_802_2 frames
2246*8b26181fSAndroid Build Coastguard Worker 		 * before it.
2247*8b26181fSAndroid Build Coastguard Worker 		 */
2248*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
2249*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
2250*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
2251*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2252*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
2253*8b26181fSAndroid Build Coastguard Worker 
2254*8b26181fSAndroid Build Coastguard Worker 		/*
2255*8b26181fSAndroid Build Coastguard Worker 		 * Now check for 802.3 frames and OR that with
2256*8b26181fSAndroid Build Coastguard Worker 		 * the previous test.
2257*8b26181fSAndroid Build Coastguard Worker 		 */
2258*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3);
2259*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
2260*8b26181fSAndroid Build Coastguard Worker 
2261*8b26181fSAndroid Build Coastguard Worker 		/*
2262*8b26181fSAndroid Build Coastguard Worker 		 * Now add the check for Ethernet_II frames, and
2263*8b26181fSAndroid Build Coastguard Worker 		 * do that before checking for the other frame
2264*8b26181fSAndroid Build Coastguard Worker 		 * types.
2265*8b26181fSAndroid Build Coastguard Worker 		 */
2266*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);
2267*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
2268*8b26181fSAndroid Build Coastguard Worker 		return b1;
2269*8b26181fSAndroid Build Coastguard Worker 
2270*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_ATALK:
2271*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_AARP:
2272*8b26181fSAndroid Build Coastguard Worker 		/*
2273*8b26181fSAndroid Build Coastguard Worker 		 * EtherTalk (AppleTalk protocols on Ethernet link
2274*8b26181fSAndroid Build Coastguard Worker 		 * layer) may use 802.2 encapsulation.
2275*8b26181fSAndroid Build Coastguard Worker 		 */
2276*8b26181fSAndroid Build Coastguard Worker 
2277*8b26181fSAndroid Build Coastguard Worker 		/*
2278*8b26181fSAndroid Build Coastguard Worker 		 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2279*8b26181fSAndroid Build Coastguard Worker 		 * we check for the 802.2 protocol type in the
2280*8b26181fSAndroid Build Coastguard Worker 		 * "Ethernet type" field.
2281*8b26181fSAndroid Build Coastguard Worker 		 */
2282*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2283*8b26181fSAndroid Build Coastguard Worker 
2284*8b26181fSAndroid Build Coastguard Worker 		/*
2285*8b26181fSAndroid Build Coastguard Worker 		 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2286*8b26181fSAndroid Build Coastguard Worker 		 * SNAP packets with an organization code of
2287*8b26181fSAndroid Build Coastguard Worker 		 * 0x080007 (Apple, for Appletalk) and a protocol
2288*8b26181fSAndroid Build Coastguard Worker 		 * type of ETHERTYPE_ATALK (Appletalk).
2289*8b26181fSAndroid Build Coastguard Worker 		 *
2290*8b26181fSAndroid Build Coastguard Worker 		 * 802.2-encapsulated ETHERTYPE_AARP packets are
2291*8b26181fSAndroid Build Coastguard Worker 		 * SNAP packets with an organization code of
2292*8b26181fSAndroid Build Coastguard Worker 		 * 0x000000 (encapsulated Ethernet) and a protocol
2293*8b26181fSAndroid Build Coastguard Worker 		 * type of ETHERTYPE_AARP (Appletalk ARP).
2294*8b26181fSAndroid Build Coastguard Worker 		 */
2295*8b26181fSAndroid Build Coastguard Worker 		if (ll_proto == ETHERTYPE_ATALK)
2296*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
2297*8b26181fSAndroid Build Coastguard Worker 		else	/* ll_proto == ETHERTYPE_AARP */
2298*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
2299*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
2300*8b26181fSAndroid Build Coastguard Worker 
2301*8b26181fSAndroid Build Coastguard Worker 		/*
2302*8b26181fSAndroid Build Coastguard Worker 		 * Check for Ethernet encapsulation (Ethertalk
2303*8b26181fSAndroid Build Coastguard Worker 		 * phase 1?); we just check for the Ethernet
2304*8b26181fSAndroid Build Coastguard Worker 		 * protocol type.
2305*8b26181fSAndroid Build Coastguard Worker 		 */
2306*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2307*8b26181fSAndroid Build Coastguard Worker 
2308*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
2309*8b26181fSAndroid Build Coastguard Worker 		return b1;
2310*8b26181fSAndroid Build Coastguard Worker 
2311*8b26181fSAndroid Build Coastguard Worker 	default:
2312*8b26181fSAndroid Build Coastguard Worker 		if (ll_proto <= ETHERMTU) {
2313*8b26181fSAndroid Build Coastguard Worker 			/*
2314*8b26181fSAndroid Build Coastguard Worker 			 * This is an LLC SAP value, so the frames
2315*8b26181fSAndroid Build Coastguard Worker 			 * that match would be 802.2 frames.
2316*8b26181fSAndroid Build Coastguard Worker 			 * Check for the 802.2 protocol type
2317*8b26181fSAndroid Build Coastguard Worker 			 * in the "Ethernet type" field, and
2318*8b26181fSAndroid Build Coastguard Worker 			 * then check the DSAP.
2319*8b26181fSAndroid Build Coastguard Worker 			 */
2320*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
2321*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B,
2322*8b26181fSAndroid Build Coastguard Worker 			     ll_proto);
2323*8b26181fSAndroid Build Coastguard Worker 			gen_and(b0, b1);
2324*8b26181fSAndroid Build Coastguard Worker 			return b1;
2325*8b26181fSAndroid Build Coastguard Worker 		} else {
2326*8b26181fSAndroid Build Coastguard Worker 			/*
2327*8b26181fSAndroid Build Coastguard Worker 			 * This is an Ethernet type, so compare
2328*8b26181fSAndroid Build Coastguard Worker 			 * the length/type field with it (if
2329*8b26181fSAndroid Build Coastguard Worker 			 * the frame is an 802.2 frame, the length
2330*8b26181fSAndroid Build Coastguard Worker 			 * field will be <= ETHERMTU, and, as
2331*8b26181fSAndroid Build Coastguard Worker 			 * "ll_proto" is > ETHERMTU, this test
2332*8b26181fSAndroid Build Coastguard Worker 			 * will fail and the frame won't match,
2333*8b26181fSAndroid Build Coastguard Worker 			 * which is what we want).
2334*8b26181fSAndroid Build Coastguard Worker 			 */
2335*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
2336*8b26181fSAndroid Build Coastguard Worker 		}
2337*8b26181fSAndroid Build Coastguard Worker 	}
2338*8b26181fSAndroid Build Coastguard Worker }
2339*8b26181fSAndroid Build Coastguard Worker 
2340*8b26181fSAndroid Build Coastguard Worker /*
2341*8b26181fSAndroid Build Coastguard Worker  * Load a value relative to the beginning of the link-layer header after the
2342*8b26181fSAndroid Build Coastguard Worker  * pflog header.
2343*8b26181fSAndroid Build Coastguard Worker  */
2344*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_load_pflog_llprefixlen(compiler_state_t * cstate)2345*8b26181fSAndroid Build Coastguard Worker gen_load_pflog_llprefixlen(compiler_state_t *cstate)
2346*8b26181fSAndroid Build Coastguard Worker {
2347*8b26181fSAndroid Build Coastguard Worker 	struct slist *s1, *s2;
2348*8b26181fSAndroid Build Coastguard Worker 
2349*8b26181fSAndroid Build Coastguard Worker 	/*
2350*8b26181fSAndroid Build Coastguard Worker 	 * Generate code to load the length of the pflog header into
2351*8b26181fSAndroid Build Coastguard Worker 	 * the register assigned to hold that length, if one has been
2352*8b26181fSAndroid Build Coastguard Worker 	 * assigned.  (If one hasn't been assigned, no code we've
2353*8b26181fSAndroid Build Coastguard Worker 	 * generated uses that prefix, so we don't need to generate any
2354*8b26181fSAndroid Build Coastguard Worker 	 * code to load it.)
2355*8b26181fSAndroid Build Coastguard Worker 	 */
2356*8b26181fSAndroid Build Coastguard Worker 	if (cstate->off_linkpl.reg != -1) {
2357*8b26181fSAndroid Build Coastguard Worker 		/*
2358*8b26181fSAndroid Build Coastguard Worker 		 * The length is in the first byte of the header.
2359*8b26181fSAndroid Build Coastguard Worker 		 */
2360*8b26181fSAndroid Build Coastguard Worker 		s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2361*8b26181fSAndroid Build Coastguard Worker 		s1->s.k = 0;
2362*8b26181fSAndroid Build Coastguard Worker 
2363*8b26181fSAndroid Build Coastguard Worker 		/*
2364*8b26181fSAndroid Build Coastguard Worker 		 * Round it up to a multiple of 4.
2365*8b26181fSAndroid Build Coastguard Worker 		 * Add 3, and clear the lower 2 bits.
2366*8b26181fSAndroid Build Coastguard Worker 		 */
2367*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
2368*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 3;
2369*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2370*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
2371*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 0xfffffffc;
2372*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2373*8b26181fSAndroid Build Coastguard Worker 
2374*8b26181fSAndroid Build Coastguard Worker 		/*
2375*8b26181fSAndroid Build Coastguard Worker 		 * Now allocate a register to hold that value and store
2376*8b26181fSAndroid Build Coastguard Worker 		 * it.
2377*8b26181fSAndroid Build Coastguard Worker 		 */
2378*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ST);
2379*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkpl.reg;
2380*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2381*8b26181fSAndroid Build Coastguard Worker 
2382*8b26181fSAndroid Build Coastguard Worker 		/*
2383*8b26181fSAndroid Build Coastguard Worker 		 * Now move it into the X register.
2384*8b26181fSAndroid Build Coastguard Worker 		 */
2385*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2386*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2387*8b26181fSAndroid Build Coastguard Worker 
2388*8b26181fSAndroid Build Coastguard Worker 		return (s1);
2389*8b26181fSAndroid Build Coastguard Worker 	} else
2390*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2391*8b26181fSAndroid Build Coastguard Worker }
2392*8b26181fSAndroid Build Coastguard Worker 
2393*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_load_prism_llprefixlen(compiler_state_t * cstate)2394*8b26181fSAndroid Build Coastguard Worker gen_load_prism_llprefixlen(compiler_state_t *cstate)
2395*8b26181fSAndroid Build Coastguard Worker {
2396*8b26181fSAndroid Build Coastguard Worker 	struct slist *s1, *s2;
2397*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjeq_avs_cookie;
2398*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjcommon;
2399*8b26181fSAndroid Build Coastguard Worker 
2400*8b26181fSAndroid Build Coastguard Worker 	/*
2401*8b26181fSAndroid Build Coastguard Worker 	 * This code is not compatible with the optimizer, as
2402*8b26181fSAndroid Build Coastguard Worker 	 * we are generating jmp instructions within a normal
2403*8b26181fSAndroid Build Coastguard Worker 	 * slist of instructions
2404*8b26181fSAndroid Build Coastguard Worker 	 */
2405*8b26181fSAndroid Build Coastguard Worker 	cstate->no_optimize = 1;
2406*8b26181fSAndroid Build Coastguard Worker 
2407*8b26181fSAndroid Build Coastguard Worker 	/*
2408*8b26181fSAndroid Build Coastguard Worker 	 * Generate code to load the length of the radio header into
2409*8b26181fSAndroid Build Coastguard Worker 	 * the register assigned to hold that length, if one has been
2410*8b26181fSAndroid Build Coastguard Worker 	 * assigned.  (If one hasn't been assigned, no code we've
2411*8b26181fSAndroid Build Coastguard Worker 	 * generated uses that prefix, so we don't need to generate any
2412*8b26181fSAndroid Build Coastguard Worker 	 * code to load it.)
2413*8b26181fSAndroid Build Coastguard Worker 	 *
2414*8b26181fSAndroid Build Coastguard Worker 	 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2415*8b26181fSAndroid Build Coastguard Worker 	 * or always use the AVS header rather than the Prism header.
2416*8b26181fSAndroid Build Coastguard Worker 	 * We load a 4-byte big-endian value at the beginning of the
2417*8b26181fSAndroid Build Coastguard Worker 	 * raw packet data, and see whether, when masked with 0xFFFFF000,
2418*8b26181fSAndroid Build Coastguard Worker 	 * it's equal to 0x80211000.  If so, that indicates that it's
2419*8b26181fSAndroid Build Coastguard Worker 	 * an AVS header (the masked-out bits are the version number).
2420*8b26181fSAndroid Build Coastguard Worker 	 * Otherwise, it's a Prism header.
2421*8b26181fSAndroid Build Coastguard Worker 	 *
2422*8b26181fSAndroid Build Coastguard Worker 	 * XXX - the Prism header is also, in theory, variable-length,
2423*8b26181fSAndroid Build Coastguard Worker 	 * but no known software generates headers that aren't 144
2424*8b26181fSAndroid Build Coastguard Worker 	 * bytes long.
2425*8b26181fSAndroid Build Coastguard Worker 	 */
2426*8b26181fSAndroid Build Coastguard Worker 	if (cstate->off_linkhdr.reg != -1) {
2427*8b26181fSAndroid Build Coastguard Worker 		/*
2428*8b26181fSAndroid Build Coastguard Worker 		 * Load the cookie.
2429*8b26181fSAndroid Build Coastguard Worker 		 */
2430*8b26181fSAndroid Build Coastguard Worker 		s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2431*8b26181fSAndroid Build Coastguard Worker 		s1->s.k = 0;
2432*8b26181fSAndroid Build Coastguard Worker 
2433*8b26181fSAndroid Build Coastguard Worker 		/*
2434*8b26181fSAndroid Build Coastguard Worker 		 * AND it with 0xFFFFF000.
2435*8b26181fSAndroid Build Coastguard Worker 		 */
2436*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
2437*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 0xFFFFF000;
2438*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2439*8b26181fSAndroid Build Coastguard Worker 
2440*8b26181fSAndroid Build Coastguard Worker 		/*
2441*8b26181fSAndroid Build Coastguard Worker 		 * Compare with 0x80211000.
2442*8b26181fSAndroid Build Coastguard Worker 		 */
2443*8b26181fSAndroid Build Coastguard Worker 		sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ));
2444*8b26181fSAndroid Build Coastguard Worker 		sjeq_avs_cookie->s.k = 0x80211000;
2445*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, sjeq_avs_cookie);
2446*8b26181fSAndroid Build Coastguard Worker 
2447*8b26181fSAndroid Build Coastguard Worker 		/*
2448*8b26181fSAndroid Build Coastguard Worker 		 * If it's AVS:
2449*8b26181fSAndroid Build Coastguard Worker 		 *
2450*8b26181fSAndroid Build Coastguard Worker 		 * The 4 bytes at an offset of 4 from the beginning of
2451*8b26181fSAndroid Build Coastguard Worker 		 * the AVS header are the length of the AVS header.
2452*8b26181fSAndroid Build Coastguard Worker 		 * That field is big-endian.
2453*8b26181fSAndroid Build Coastguard Worker 		 */
2454*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2455*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 4;
2456*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2457*8b26181fSAndroid Build Coastguard Worker 		sjeq_avs_cookie->s.jt = s2;
2458*8b26181fSAndroid Build Coastguard Worker 
2459*8b26181fSAndroid Build Coastguard Worker 		/*
2460*8b26181fSAndroid Build Coastguard Worker 		 * Now jump to the code to allocate a register
2461*8b26181fSAndroid Build Coastguard Worker 		 * into which to save the header length and
2462*8b26181fSAndroid Build Coastguard Worker 		 * store the length there.  (The "jump always"
2463*8b26181fSAndroid Build Coastguard Worker 		 * instruction needs to have the k field set;
2464*8b26181fSAndroid Build Coastguard Worker 		 * it's added to the PC, so, as we're jumping
2465*8b26181fSAndroid Build Coastguard Worker 		 * over a single instruction, it should be 1.)
2466*8b26181fSAndroid Build Coastguard Worker 		 */
2467*8b26181fSAndroid Build Coastguard Worker 		sjcommon = new_stmt(cstate, JMP(BPF_JA));
2468*8b26181fSAndroid Build Coastguard Worker 		sjcommon->s.k = 1;
2469*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, sjcommon);
2470*8b26181fSAndroid Build Coastguard Worker 
2471*8b26181fSAndroid Build Coastguard Worker 		/*
2472*8b26181fSAndroid Build Coastguard Worker 		 * Now for the code that handles the Prism header.
2473*8b26181fSAndroid Build Coastguard Worker 		 * Just load the length of the Prism header (144)
2474*8b26181fSAndroid Build Coastguard Worker 		 * into the A register.  Have the test for an AVS
2475*8b26181fSAndroid Build Coastguard Worker 		 * header branch here if we don't have an AVS header.
2476*8b26181fSAndroid Build Coastguard Worker 		 */
2477*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
2478*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 144;
2479*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2480*8b26181fSAndroid Build Coastguard Worker 		sjeq_avs_cookie->s.jf = s2;
2481*8b26181fSAndroid Build Coastguard Worker 
2482*8b26181fSAndroid Build Coastguard Worker 		/*
2483*8b26181fSAndroid Build Coastguard Worker 		 * Now allocate a register to hold that value and store
2484*8b26181fSAndroid Build Coastguard Worker 		 * it.  The code for the AVS header will jump here after
2485*8b26181fSAndroid Build Coastguard Worker 		 * loading the length of the AVS header.
2486*8b26181fSAndroid Build Coastguard Worker 		 */
2487*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ST);
2488*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkhdr.reg;
2489*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2490*8b26181fSAndroid Build Coastguard Worker 		sjcommon->s.jf = s2;
2491*8b26181fSAndroid Build Coastguard Worker 
2492*8b26181fSAndroid Build Coastguard Worker 		/*
2493*8b26181fSAndroid Build Coastguard Worker 		 * Now move it into the X register.
2494*8b26181fSAndroid Build Coastguard Worker 		 */
2495*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2496*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2497*8b26181fSAndroid Build Coastguard Worker 
2498*8b26181fSAndroid Build Coastguard Worker 		return (s1);
2499*8b26181fSAndroid Build Coastguard Worker 	} else
2500*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2501*8b26181fSAndroid Build Coastguard Worker }
2502*8b26181fSAndroid Build Coastguard Worker 
2503*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_load_avs_llprefixlen(compiler_state_t * cstate)2504*8b26181fSAndroid Build Coastguard Worker gen_load_avs_llprefixlen(compiler_state_t *cstate)
2505*8b26181fSAndroid Build Coastguard Worker {
2506*8b26181fSAndroid Build Coastguard Worker 	struct slist *s1, *s2;
2507*8b26181fSAndroid Build Coastguard Worker 
2508*8b26181fSAndroid Build Coastguard Worker 	/*
2509*8b26181fSAndroid Build Coastguard Worker 	 * Generate code to load the length of the AVS header into
2510*8b26181fSAndroid Build Coastguard Worker 	 * the register assigned to hold that length, if one has been
2511*8b26181fSAndroid Build Coastguard Worker 	 * assigned.  (If one hasn't been assigned, no code we've
2512*8b26181fSAndroid Build Coastguard Worker 	 * generated uses that prefix, so we don't need to generate any
2513*8b26181fSAndroid Build Coastguard Worker 	 * code to load it.)
2514*8b26181fSAndroid Build Coastguard Worker 	 */
2515*8b26181fSAndroid Build Coastguard Worker 	if (cstate->off_linkhdr.reg != -1) {
2516*8b26181fSAndroid Build Coastguard Worker 		/*
2517*8b26181fSAndroid Build Coastguard Worker 		 * The 4 bytes at an offset of 4 from the beginning of
2518*8b26181fSAndroid Build Coastguard Worker 		 * the AVS header are the length of the AVS header.
2519*8b26181fSAndroid Build Coastguard Worker 		 * That field is big-endian.
2520*8b26181fSAndroid Build Coastguard Worker 		 */
2521*8b26181fSAndroid Build Coastguard Worker 		s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
2522*8b26181fSAndroid Build Coastguard Worker 		s1->s.k = 4;
2523*8b26181fSAndroid Build Coastguard Worker 
2524*8b26181fSAndroid Build Coastguard Worker 		/*
2525*8b26181fSAndroid Build Coastguard Worker 		 * Now allocate a register to hold that value and store
2526*8b26181fSAndroid Build Coastguard Worker 		 * it.
2527*8b26181fSAndroid Build Coastguard Worker 		 */
2528*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ST);
2529*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkhdr.reg;
2530*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2531*8b26181fSAndroid Build Coastguard Worker 
2532*8b26181fSAndroid Build Coastguard Worker 		/*
2533*8b26181fSAndroid Build Coastguard Worker 		 * Now move it into the X register.
2534*8b26181fSAndroid Build Coastguard Worker 		 */
2535*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2536*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2537*8b26181fSAndroid Build Coastguard Worker 
2538*8b26181fSAndroid Build Coastguard Worker 		return (s1);
2539*8b26181fSAndroid Build Coastguard Worker 	} else
2540*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2541*8b26181fSAndroid Build Coastguard Worker }
2542*8b26181fSAndroid Build Coastguard Worker 
2543*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_load_radiotap_llprefixlen(compiler_state_t * cstate)2544*8b26181fSAndroid Build Coastguard Worker gen_load_radiotap_llprefixlen(compiler_state_t *cstate)
2545*8b26181fSAndroid Build Coastguard Worker {
2546*8b26181fSAndroid Build Coastguard Worker 	struct slist *s1, *s2;
2547*8b26181fSAndroid Build Coastguard Worker 
2548*8b26181fSAndroid Build Coastguard Worker 	/*
2549*8b26181fSAndroid Build Coastguard Worker 	 * Generate code to load the length of the radiotap header into
2550*8b26181fSAndroid Build Coastguard Worker 	 * the register assigned to hold that length, if one has been
2551*8b26181fSAndroid Build Coastguard Worker 	 * assigned.  (If one hasn't been assigned, no code we've
2552*8b26181fSAndroid Build Coastguard Worker 	 * generated uses that prefix, so we don't need to generate any
2553*8b26181fSAndroid Build Coastguard Worker 	 * code to load it.)
2554*8b26181fSAndroid Build Coastguard Worker 	 */
2555*8b26181fSAndroid Build Coastguard Worker 	if (cstate->off_linkhdr.reg != -1) {
2556*8b26181fSAndroid Build Coastguard Worker 		/*
2557*8b26181fSAndroid Build Coastguard Worker 		 * The 2 bytes at offsets of 2 and 3 from the beginning
2558*8b26181fSAndroid Build Coastguard Worker 		 * of the radiotap header are the length of the radiotap
2559*8b26181fSAndroid Build Coastguard Worker 		 * header; unfortunately, it's little-endian, so we have
2560*8b26181fSAndroid Build Coastguard Worker 		 * to load it a byte at a time and construct the value.
2561*8b26181fSAndroid Build Coastguard Worker 		 */
2562*8b26181fSAndroid Build Coastguard Worker 
2563*8b26181fSAndroid Build Coastguard Worker 		/*
2564*8b26181fSAndroid Build Coastguard Worker 		 * Load the high-order byte, at an offset of 3, shift it
2565*8b26181fSAndroid Build Coastguard Worker 		 * left a byte, and put the result in the X register.
2566*8b26181fSAndroid Build Coastguard Worker 		 */
2567*8b26181fSAndroid Build Coastguard Worker 		s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2568*8b26181fSAndroid Build Coastguard Worker 		s1->s.k = 3;
2569*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
2570*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2571*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 8;
2572*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2573*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2574*8b26181fSAndroid Build Coastguard Worker 
2575*8b26181fSAndroid Build Coastguard Worker 		/*
2576*8b26181fSAndroid Build Coastguard Worker 		 * Load the next byte, at an offset of 2, and OR the
2577*8b26181fSAndroid Build Coastguard Worker 		 * value from the X register into it.
2578*8b26181fSAndroid Build Coastguard Worker 		 */
2579*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2580*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2581*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 2;
2582*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
2583*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2584*8b26181fSAndroid Build Coastguard Worker 
2585*8b26181fSAndroid Build Coastguard Worker 		/*
2586*8b26181fSAndroid Build Coastguard Worker 		 * Now allocate a register to hold that value and store
2587*8b26181fSAndroid Build Coastguard Worker 		 * it.
2588*8b26181fSAndroid Build Coastguard Worker 		 */
2589*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ST);
2590*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkhdr.reg;
2591*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2592*8b26181fSAndroid Build Coastguard Worker 
2593*8b26181fSAndroid Build Coastguard Worker 		/*
2594*8b26181fSAndroid Build Coastguard Worker 		 * Now move it into the X register.
2595*8b26181fSAndroid Build Coastguard Worker 		 */
2596*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2597*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2598*8b26181fSAndroid Build Coastguard Worker 
2599*8b26181fSAndroid Build Coastguard Worker 		return (s1);
2600*8b26181fSAndroid Build Coastguard Worker 	} else
2601*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2602*8b26181fSAndroid Build Coastguard Worker }
2603*8b26181fSAndroid Build Coastguard Worker 
2604*8b26181fSAndroid Build Coastguard Worker /*
2605*8b26181fSAndroid Build Coastguard Worker  * At the moment we treat PPI as normal Radiotap encoded
2606*8b26181fSAndroid Build Coastguard Worker  * packets. The difference is in the function that generates
2607*8b26181fSAndroid Build Coastguard Worker  * the code at the beginning to compute the header length.
2608*8b26181fSAndroid Build Coastguard Worker  * Since this code generator of PPI supports bare 802.11
2609*8b26181fSAndroid Build Coastguard Worker  * encapsulation only (i.e. the encapsulated DLT should be
2610*8b26181fSAndroid Build Coastguard Worker  * DLT_IEEE802_11) we generate code to check for this too;
2611*8b26181fSAndroid Build Coastguard Worker  * that's done in finish_parse().
2612*8b26181fSAndroid Build Coastguard Worker  */
2613*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_load_ppi_llprefixlen(compiler_state_t * cstate)2614*8b26181fSAndroid Build Coastguard Worker gen_load_ppi_llprefixlen(compiler_state_t *cstate)
2615*8b26181fSAndroid Build Coastguard Worker {
2616*8b26181fSAndroid Build Coastguard Worker 	struct slist *s1, *s2;
2617*8b26181fSAndroid Build Coastguard Worker 
2618*8b26181fSAndroid Build Coastguard Worker 	/*
2619*8b26181fSAndroid Build Coastguard Worker 	 * Generate code to load the length of the radiotap header
2620*8b26181fSAndroid Build Coastguard Worker 	 * into the register assigned to hold that length, if one has
2621*8b26181fSAndroid Build Coastguard Worker 	 * been assigned.
2622*8b26181fSAndroid Build Coastguard Worker 	 */
2623*8b26181fSAndroid Build Coastguard Worker 	if (cstate->off_linkhdr.reg != -1) {
2624*8b26181fSAndroid Build Coastguard Worker 		/*
2625*8b26181fSAndroid Build Coastguard Worker 		 * The 2 bytes at offsets of 2 and 3 from the beginning
2626*8b26181fSAndroid Build Coastguard Worker 		 * of the radiotap header are the length of the radiotap
2627*8b26181fSAndroid Build Coastguard Worker 		 * header; unfortunately, it's little-endian, so we have
2628*8b26181fSAndroid Build Coastguard Worker 		 * to load it a byte at a time and construct the value.
2629*8b26181fSAndroid Build Coastguard Worker 		 */
2630*8b26181fSAndroid Build Coastguard Worker 
2631*8b26181fSAndroid Build Coastguard Worker 		/*
2632*8b26181fSAndroid Build Coastguard Worker 		 * Load the high-order byte, at an offset of 3, shift it
2633*8b26181fSAndroid Build Coastguard Worker 		 * left a byte, and put the result in the X register.
2634*8b26181fSAndroid Build Coastguard Worker 		 */
2635*8b26181fSAndroid Build Coastguard Worker 		s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2636*8b26181fSAndroid Build Coastguard Worker 		s1->s.k = 3;
2637*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
2638*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2639*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 8;
2640*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2641*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2642*8b26181fSAndroid Build Coastguard Worker 
2643*8b26181fSAndroid Build Coastguard Worker 		/*
2644*8b26181fSAndroid Build Coastguard Worker 		 * Load the next byte, at an offset of 2, and OR the
2645*8b26181fSAndroid Build Coastguard Worker 		 * value from the X register into it.
2646*8b26181fSAndroid Build Coastguard Worker 		 */
2647*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
2648*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2649*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 2;
2650*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
2651*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2652*8b26181fSAndroid Build Coastguard Worker 
2653*8b26181fSAndroid Build Coastguard Worker 		/*
2654*8b26181fSAndroid Build Coastguard Worker 		 * Now allocate a register to hold that value and store
2655*8b26181fSAndroid Build Coastguard Worker 		 * it.
2656*8b26181fSAndroid Build Coastguard Worker 		 */
2657*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ST);
2658*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkhdr.reg;
2659*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2660*8b26181fSAndroid Build Coastguard Worker 
2661*8b26181fSAndroid Build Coastguard Worker 		/*
2662*8b26181fSAndroid Build Coastguard Worker 		 * Now move it into the X register.
2663*8b26181fSAndroid Build Coastguard Worker 		 */
2664*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
2665*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
2666*8b26181fSAndroid Build Coastguard Worker 
2667*8b26181fSAndroid Build Coastguard Worker 		return (s1);
2668*8b26181fSAndroid Build Coastguard Worker 	} else
2669*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2670*8b26181fSAndroid Build Coastguard Worker }
2671*8b26181fSAndroid Build Coastguard Worker 
2672*8b26181fSAndroid Build Coastguard Worker /*
2673*8b26181fSAndroid Build Coastguard Worker  * Load a value relative to the beginning of the link-layer header after the 802.11
2674*8b26181fSAndroid Build Coastguard Worker  * header, i.e. LLC_SNAP.
2675*8b26181fSAndroid Build Coastguard Worker  * The link-layer header doesn't necessarily begin at the beginning
2676*8b26181fSAndroid Build Coastguard Worker  * of the packet data; there might be a variable-length prefix containing
2677*8b26181fSAndroid Build Coastguard Worker  * radio information.
2678*8b26181fSAndroid Build Coastguard Worker  */
2679*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_load_802_11_header_len(compiler_state_t * cstate,struct slist * s,struct slist * snext)2680*8b26181fSAndroid Build Coastguard Worker gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext)
2681*8b26181fSAndroid Build Coastguard Worker {
2682*8b26181fSAndroid Build Coastguard Worker 	struct slist *s2;
2683*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjset_data_frame_1;
2684*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjset_data_frame_2;
2685*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjset_qos;
2686*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjset_radiotap_flags_present;
2687*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjset_radiotap_ext_present;
2688*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjset_radiotap_tsft_present;
2689*8b26181fSAndroid Build Coastguard Worker 	struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2690*8b26181fSAndroid Build Coastguard Worker 	struct slist *s_roundup;
2691*8b26181fSAndroid Build Coastguard Worker 
2692*8b26181fSAndroid Build Coastguard Worker 	if (cstate->off_linkpl.reg == -1) {
2693*8b26181fSAndroid Build Coastguard Worker 		/*
2694*8b26181fSAndroid Build Coastguard Worker 		 * No register has been assigned to the offset of
2695*8b26181fSAndroid Build Coastguard Worker 		 * the link-layer payload, which means nobody needs
2696*8b26181fSAndroid Build Coastguard Worker 		 * it; don't bother computing it - just return
2697*8b26181fSAndroid Build Coastguard Worker 		 * what we already have.
2698*8b26181fSAndroid Build Coastguard Worker 		 */
2699*8b26181fSAndroid Build Coastguard Worker 		return (s);
2700*8b26181fSAndroid Build Coastguard Worker 	}
2701*8b26181fSAndroid Build Coastguard Worker 
2702*8b26181fSAndroid Build Coastguard Worker 	/*
2703*8b26181fSAndroid Build Coastguard Worker 	 * This code is not compatible with the optimizer, as
2704*8b26181fSAndroid Build Coastguard Worker 	 * we are generating jmp instructions within a normal
2705*8b26181fSAndroid Build Coastguard Worker 	 * slist of instructions
2706*8b26181fSAndroid Build Coastguard Worker 	 */
2707*8b26181fSAndroid Build Coastguard Worker 	cstate->no_optimize = 1;
2708*8b26181fSAndroid Build Coastguard Worker 
2709*8b26181fSAndroid Build Coastguard Worker 	/*
2710*8b26181fSAndroid Build Coastguard Worker 	 * If "s" is non-null, it has code to arrange that the X register
2711*8b26181fSAndroid Build Coastguard Worker 	 * contains the length of the prefix preceding the link-layer
2712*8b26181fSAndroid Build Coastguard Worker 	 * header.
2713*8b26181fSAndroid Build Coastguard Worker 	 *
2714*8b26181fSAndroid Build Coastguard Worker 	 * Otherwise, the length of the prefix preceding the link-layer
2715*8b26181fSAndroid Build Coastguard Worker 	 * header is "off_outermostlinkhdr.constant_part".
2716*8b26181fSAndroid Build Coastguard Worker 	 */
2717*8b26181fSAndroid Build Coastguard Worker 	if (s == NULL) {
2718*8b26181fSAndroid Build Coastguard Worker 		/*
2719*8b26181fSAndroid Build Coastguard Worker 		 * There is no variable-length header preceding the
2720*8b26181fSAndroid Build Coastguard Worker 		 * link-layer header.
2721*8b26181fSAndroid Build Coastguard Worker 		 *
2722*8b26181fSAndroid Build Coastguard Worker 		 * Load the length of the fixed-length prefix preceding
2723*8b26181fSAndroid Build Coastguard Worker 		 * the link-layer header (if any) into the X register,
2724*8b26181fSAndroid Build Coastguard Worker 		 * and store it in the cstate->off_linkpl.reg register.
2725*8b26181fSAndroid Build Coastguard Worker 		 * That length is off_outermostlinkhdr.constant_part.
2726*8b26181fSAndroid Build Coastguard Worker 		 */
2727*8b26181fSAndroid Build Coastguard Worker 		s = new_stmt(cstate, BPF_LDX|BPF_IMM);
2728*8b26181fSAndroid Build Coastguard Worker 		s->s.k = cstate->off_outermostlinkhdr.constant_part;
2729*8b26181fSAndroid Build Coastguard Worker 	}
2730*8b26181fSAndroid Build Coastguard Worker 
2731*8b26181fSAndroid Build Coastguard Worker 	/*
2732*8b26181fSAndroid Build Coastguard Worker 	 * The X register contains the offset of the beginning of the
2733*8b26181fSAndroid Build Coastguard Worker 	 * link-layer header; add 24, which is the minimum length
2734*8b26181fSAndroid Build Coastguard Worker 	 * of the MAC header for a data frame, to that, and store it
2735*8b26181fSAndroid Build Coastguard Worker 	 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2736*8b26181fSAndroid Build Coastguard Worker 	 * which is at the offset in the X register, with an indexed load.
2737*8b26181fSAndroid Build Coastguard Worker 	 */
2738*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_MISC|BPF_TXA);
2739*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
2740*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
2741*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = 24;
2742*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
2743*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_ST);
2744*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = cstate->off_linkpl.reg;
2745*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
2746*8b26181fSAndroid Build Coastguard Worker 
2747*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
2748*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = 0;
2749*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
2750*8b26181fSAndroid Build Coastguard Worker 
2751*8b26181fSAndroid Build Coastguard Worker 	/*
2752*8b26181fSAndroid Build Coastguard Worker 	 * Check the Frame Control field to see if this is a data frame;
2753*8b26181fSAndroid Build Coastguard Worker 	 * a data frame has the 0x08 bit (b3) in that field set and the
2754*8b26181fSAndroid Build Coastguard Worker 	 * 0x04 bit (b2) clear.
2755*8b26181fSAndroid Build Coastguard Worker 	 */
2756*8b26181fSAndroid Build Coastguard Worker 	sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET));
2757*8b26181fSAndroid Build Coastguard Worker 	sjset_data_frame_1->s.k = 0x08;
2758*8b26181fSAndroid Build Coastguard Worker 	sappend(s, sjset_data_frame_1);
2759*8b26181fSAndroid Build Coastguard Worker 
2760*8b26181fSAndroid Build Coastguard Worker 	/*
2761*8b26181fSAndroid Build Coastguard Worker 	 * If b3 is set, test b2, otherwise go to the first statement of
2762*8b26181fSAndroid Build Coastguard Worker 	 * the rest of the program.
2763*8b26181fSAndroid Build Coastguard Worker 	 */
2764*8b26181fSAndroid Build Coastguard Worker 	sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET));
2765*8b26181fSAndroid Build Coastguard Worker 	sjset_data_frame_2->s.k = 0x04;
2766*8b26181fSAndroid Build Coastguard Worker 	sappend(s, sjset_data_frame_2);
2767*8b26181fSAndroid Build Coastguard Worker 	sjset_data_frame_1->s.jf = snext;
2768*8b26181fSAndroid Build Coastguard Worker 
2769*8b26181fSAndroid Build Coastguard Worker 	/*
2770*8b26181fSAndroid Build Coastguard Worker 	 * If b2 is not set, this is a data frame; test the QoS bit.
2771*8b26181fSAndroid Build Coastguard Worker 	 * Otherwise, go to the first statement of the rest of the
2772*8b26181fSAndroid Build Coastguard Worker 	 * program.
2773*8b26181fSAndroid Build Coastguard Worker 	 */
2774*8b26181fSAndroid Build Coastguard Worker 	sjset_data_frame_2->s.jt = snext;
2775*8b26181fSAndroid Build Coastguard Worker 	sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET));
2776*8b26181fSAndroid Build Coastguard Worker 	sjset_qos->s.k = 0x80;	/* QoS bit */
2777*8b26181fSAndroid Build Coastguard Worker 	sappend(s, sjset_qos);
2778*8b26181fSAndroid Build Coastguard Worker 
2779*8b26181fSAndroid Build Coastguard Worker 	/*
2780*8b26181fSAndroid Build Coastguard Worker 	 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2781*8b26181fSAndroid Build Coastguard Worker 	 * field.
2782*8b26181fSAndroid Build Coastguard Worker 	 * Otherwise, go to the first statement of the rest of the
2783*8b26181fSAndroid Build Coastguard Worker 	 * program.
2784*8b26181fSAndroid Build Coastguard Worker 	 */
2785*8b26181fSAndroid Build Coastguard Worker 	sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
2786*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = cstate->off_linkpl.reg;
2787*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
2788*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
2789*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = 2;
2790*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
2791*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_ST);
2792*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = cstate->off_linkpl.reg;
2793*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
2794*8b26181fSAndroid Build Coastguard Worker 
2795*8b26181fSAndroid Build Coastguard Worker 	/*
2796*8b26181fSAndroid Build Coastguard Worker 	 * If we have a radiotap header, look at it to see whether
2797*8b26181fSAndroid Build Coastguard Worker 	 * there's Atheros padding between the MAC-layer header
2798*8b26181fSAndroid Build Coastguard Worker 	 * and the payload.
2799*8b26181fSAndroid Build Coastguard Worker 	 *
2800*8b26181fSAndroid Build Coastguard Worker 	 * Note: all of the fields in the radiotap header are
2801*8b26181fSAndroid Build Coastguard Worker 	 * little-endian, so we byte-swap all of the values
2802*8b26181fSAndroid Build Coastguard Worker 	 * we test against, as they will be loaded as big-endian
2803*8b26181fSAndroid Build Coastguard Worker 	 * values.
2804*8b26181fSAndroid Build Coastguard Worker 	 *
2805*8b26181fSAndroid Build Coastguard Worker 	 * XXX - in the general case, we would have to scan through
2806*8b26181fSAndroid Build Coastguard Worker 	 * *all* the presence bits, if there's more than one word of
2807*8b26181fSAndroid Build Coastguard Worker 	 * presence bits.  That would require a loop, meaning that
2808*8b26181fSAndroid Build Coastguard Worker 	 * we wouldn't be able to run the filter in the kernel.
2809*8b26181fSAndroid Build Coastguard Worker 	 *
2810*8b26181fSAndroid Build Coastguard Worker 	 * We assume here that the Atheros adapters that insert the
2811*8b26181fSAndroid Build Coastguard Worker 	 * annoying padding don't have multiple antennae and therefore
2812*8b26181fSAndroid Build Coastguard Worker 	 * do not generate radiotap headers with multiple presence words.
2813*8b26181fSAndroid Build Coastguard Worker 	 */
2814*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype == DLT_IEEE802_11_RADIO) {
2815*8b26181fSAndroid Build Coastguard Worker 		/*
2816*8b26181fSAndroid Build Coastguard Worker 		 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2817*8b26181fSAndroid Build Coastguard Worker 		 * in the first presence flag word?
2818*8b26181fSAndroid Build Coastguard Worker 		 */
2819*8b26181fSAndroid Build Coastguard Worker 		sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W);
2820*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 4;
2821*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
2822*8b26181fSAndroid Build Coastguard Worker 
2823*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET));
2824*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002);
2825*8b26181fSAndroid Build Coastguard Worker 		sappend(s, sjset_radiotap_flags_present);
2826*8b26181fSAndroid Build Coastguard Worker 
2827*8b26181fSAndroid Build Coastguard Worker 		/*
2828*8b26181fSAndroid Build Coastguard Worker 		 * If not, skip all of this.
2829*8b26181fSAndroid Build Coastguard Worker 		 */
2830*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_flags_present->s.jf = snext;
2831*8b26181fSAndroid Build Coastguard Worker 
2832*8b26181fSAndroid Build Coastguard Worker 		/*
2833*8b26181fSAndroid Build Coastguard Worker 		 * Otherwise, is the "extension" bit set in that word?
2834*8b26181fSAndroid Build Coastguard Worker 		 */
2835*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET));
2836*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000);
2837*8b26181fSAndroid Build Coastguard Worker 		sappend(s, sjset_radiotap_ext_present);
2838*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present;
2839*8b26181fSAndroid Build Coastguard Worker 
2840*8b26181fSAndroid Build Coastguard Worker 		/*
2841*8b26181fSAndroid Build Coastguard Worker 		 * If so, skip all of this.
2842*8b26181fSAndroid Build Coastguard Worker 		 */
2843*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_ext_present->s.jt = snext;
2844*8b26181fSAndroid Build Coastguard Worker 
2845*8b26181fSAndroid Build Coastguard Worker 		/*
2846*8b26181fSAndroid Build Coastguard Worker 		 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2847*8b26181fSAndroid Build Coastguard Worker 		 */
2848*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET));
2849*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001);
2850*8b26181fSAndroid Build Coastguard Worker 		sappend(s, sjset_radiotap_tsft_present);
2851*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present;
2852*8b26181fSAndroid Build Coastguard Worker 
2853*8b26181fSAndroid Build Coastguard Worker 		/*
2854*8b26181fSAndroid Build Coastguard Worker 		 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2855*8b26181fSAndroid Build Coastguard Worker 		 * at an offset of 16 from the beginning of the raw packet
2856*8b26181fSAndroid Build Coastguard Worker 		 * data (8 bytes for the radiotap header and 8 bytes for
2857*8b26181fSAndroid Build Coastguard Worker 		 * the TSFT field).
2858*8b26181fSAndroid Build Coastguard Worker 		 *
2859*8b26181fSAndroid Build Coastguard Worker 		 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2860*8b26181fSAndroid Build Coastguard Worker 		 * is set.
2861*8b26181fSAndroid Build Coastguard Worker 		 */
2862*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
2863*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 16;
2864*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
2865*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_tsft_present->s.jt = s2;
2866*8b26181fSAndroid Build Coastguard Worker 
2867*8b26181fSAndroid Build Coastguard Worker 		sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
2868*8b26181fSAndroid Build Coastguard Worker 		sjset_tsft_datapad->s.k = 0x20;
2869*8b26181fSAndroid Build Coastguard Worker 		sappend(s, sjset_tsft_datapad);
2870*8b26181fSAndroid Build Coastguard Worker 
2871*8b26181fSAndroid Build Coastguard Worker 		/*
2872*8b26181fSAndroid Build Coastguard Worker 		 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2873*8b26181fSAndroid Build Coastguard Worker 		 * at an offset of 8 from the beginning of the raw packet
2874*8b26181fSAndroid Build Coastguard Worker 		 * data (8 bytes for the radiotap header).
2875*8b26181fSAndroid Build Coastguard Worker 		 *
2876*8b26181fSAndroid Build Coastguard Worker 		 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2877*8b26181fSAndroid Build Coastguard Worker 		 * is set.
2878*8b26181fSAndroid Build Coastguard Worker 		 */
2879*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
2880*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 8;
2881*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
2882*8b26181fSAndroid Build Coastguard Worker 		sjset_radiotap_tsft_present->s.jf = s2;
2883*8b26181fSAndroid Build Coastguard Worker 
2884*8b26181fSAndroid Build Coastguard Worker 		sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
2885*8b26181fSAndroid Build Coastguard Worker 		sjset_notsft_datapad->s.k = 0x20;
2886*8b26181fSAndroid Build Coastguard Worker 		sappend(s, sjset_notsft_datapad);
2887*8b26181fSAndroid Build Coastguard Worker 
2888*8b26181fSAndroid Build Coastguard Worker 		/*
2889*8b26181fSAndroid Build Coastguard Worker 		 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2890*8b26181fSAndroid Build Coastguard Worker 		 * set, round the length of the 802.11 header to
2891*8b26181fSAndroid Build Coastguard Worker 		 * a multiple of 4.  Do that by adding 3 and then
2892*8b26181fSAndroid Build Coastguard Worker 		 * dividing by and multiplying by 4, which we do by
2893*8b26181fSAndroid Build Coastguard Worker 		 * ANDing with ~3.
2894*8b26181fSAndroid Build Coastguard Worker 		 */
2895*8b26181fSAndroid Build Coastguard Worker 		s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM);
2896*8b26181fSAndroid Build Coastguard Worker 		s_roundup->s.k = cstate->off_linkpl.reg;
2897*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s_roundup);
2898*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
2899*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = 3;
2900*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
2901*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM);
2902*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = (bpf_u_int32)~3;
2903*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
2904*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ST);
2905*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkpl.reg;
2906*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
2907*8b26181fSAndroid Build Coastguard Worker 
2908*8b26181fSAndroid Build Coastguard Worker 		sjset_tsft_datapad->s.jt = s_roundup;
2909*8b26181fSAndroid Build Coastguard Worker 		sjset_tsft_datapad->s.jf = snext;
2910*8b26181fSAndroid Build Coastguard Worker 		sjset_notsft_datapad->s.jt = s_roundup;
2911*8b26181fSAndroid Build Coastguard Worker 		sjset_notsft_datapad->s.jf = snext;
2912*8b26181fSAndroid Build Coastguard Worker 	} else
2913*8b26181fSAndroid Build Coastguard Worker 		sjset_qos->s.jf = snext;
2914*8b26181fSAndroid Build Coastguard Worker 
2915*8b26181fSAndroid Build Coastguard Worker 	return s;
2916*8b26181fSAndroid Build Coastguard Worker }
2917*8b26181fSAndroid Build Coastguard Worker 
2918*8b26181fSAndroid Build Coastguard Worker static void
insert_compute_vloffsets(compiler_state_t * cstate,struct block * b)2919*8b26181fSAndroid Build Coastguard Worker insert_compute_vloffsets(compiler_state_t *cstate, struct block *b)
2920*8b26181fSAndroid Build Coastguard Worker {
2921*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
2922*8b26181fSAndroid Build Coastguard Worker 
2923*8b26181fSAndroid Build Coastguard Worker 	/* There is an implicit dependency between the link
2924*8b26181fSAndroid Build Coastguard Worker 	 * payload and link header since the payload computation
2925*8b26181fSAndroid Build Coastguard Worker 	 * includes the variable part of the header. Therefore,
2926*8b26181fSAndroid Build Coastguard Worker 	 * if nobody else has allocated a register for the link
2927*8b26181fSAndroid Build Coastguard Worker 	 * header and we need it, do it now. */
2928*8b26181fSAndroid Build Coastguard Worker 	if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable &&
2929*8b26181fSAndroid Build Coastguard Worker 	    cstate->off_linkhdr.reg == -1)
2930*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkhdr.reg = alloc_reg(cstate);
2931*8b26181fSAndroid Build Coastguard Worker 
2932*8b26181fSAndroid Build Coastguard Worker 	/*
2933*8b26181fSAndroid Build Coastguard Worker 	 * For link-layer types that have a variable-length header
2934*8b26181fSAndroid Build Coastguard Worker 	 * preceding the link-layer header, generate code to load
2935*8b26181fSAndroid Build Coastguard Worker 	 * the offset of the link-layer header into the register
2936*8b26181fSAndroid Build Coastguard Worker 	 * assigned to that offset, if any.
2937*8b26181fSAndroid Build Coastguard Worker 	 *
2938*8b26181fSAndroid Build Coastguard Worker 	 * XXX - this, and the next switch statement, won't handle
2939*8b26181fSAndroid Build Coastguard Worker 	 * encapsulation of 802.11 or 802.11+radio information in
2940*8b26181fSAndroid Build Coastguard Worker 	 * some other protocol stack.  That's significantly more
2941*8b26181fSAndroid Build Coastguard Worker 	 * complicated.
2942*8b26181fSAndroid Build Coastguard Worker 	 */
2943*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->outermostlinktype) {
2944*8b26181fSAndroid Build Coastguard Worker 
2945*8b26181fSAndroid Build Coastguard Worker 	case DLT_PRISM_HEADER:
2946*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_prism_llprefixlen(cstate);
2947*8b26181fSAndroid Build Coastguard Worker 		break;
2948*8b26181fSAndroid Build Coastguard Worker 
2949*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO_AVS:
2950*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_avs_llprefixlen(cstate);
2951*8b26181fSAndroid Build Coastguard Worker 		break;
2952*8b26181fSAndroid Build Coastguard Worker 
2953*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO:
2954*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_radiotap_llprefixlen(cstate);
2955*8b26181fSAndroid Build Coastguard Worker 		break;
2956*8b26181fSAndroid Build Coastguard Worker 
2957*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPI:
2958*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_ppi_llprefixlen(cstate);
2959*8b26181fSAndroid Build Coastguard Worker 		break;
2960*8b26181fSAndroid Build Coastguard Worker 
2961*8b26181fSAndroid Build Coastguard Worker 	default:
2962*8b26181fSAndroid Build Coastguard Worker 		s = NULL;
2963*8b26181fSAndroid Build Coastguard Worker 		break;
2964*8b26181fSAndroid Build Coastguard Worker 	}
2965*8b26181fSAndroid Build Coastguard Worker 
2966*8b26181fSAndroid Build Coastguard Worker 	/*
2967*8b26181fSAndroid Build Coastguard Worker 	 * For link-layer types that have a variable-length link-layer
2968*8b26181fSAndroid Build Coastguard Worker 	 * header, generate code to load the offset of the link-layer
2969*8b26181fSAndroid Build Coastguard Worker 	 * payload into the register assigned to that offset, if any.
2970*8b26181fSAndroid Build Coastguard Worker 	 */
2971*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->outermostlinktype) {
2972*8b26181fSAndroid Build Coastguard Worker 
2973*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11:
2974*8b26181fSAndroid Build Coastguard Worker 	case DLT_PRISM_HEADER:
2975*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO_AVS:
2976*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO:
2977*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPI:
2978*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_802_11_header_len(cstate, s, b->stmts);
2979*8b26181fSAndroid Build Coastguard Worker 		break;
2980*8b26181fSAndroid Build Coastguard Worker 
2981*8b26181fSAndroid Build Coastguard Worker 	case DLT_PFLOG:
2982*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_pflog_llprefixlen(cstate);
2983*8b26181fSAndroid Build Coastguard Worker 		break;
2984*8b26181fSAndroid Build Coastguard Worker 	}
2985*8b26181fSAndroid Build Coastguard Worker 
2986*8b26181fSAndroid Build Coastguard Worker 	/*
2987*8b26181fSAndroid Build Coastguard Worker 	 * If there is no initialization yet and we need variable
2988*8b26181fSAndroid Build Coastguard Worker 	 * length offsets for VLAN, initialize them to zero
2989*8b26181fSAndroid Build Coastguard Worker 	 */
2990*8b26181fSAndroid Build Coastguard Worker 	if (s == NULL && cstate->is_vlan_vloffset) {
2991*8b26181fSAndroid Build Coastguard Worker 		struct slist *s2;
2992*8b26181fSAndroid Build Coastguard Worker 
2993*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_linkpl.reg == -1)
2994*8b26181fSAndroid Build Coastguard Worker 			cstate->off_linkpl.reg = alloc_reg(cstate);
2995*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_linktype.reg == -1)
2996*8b26181fSAndroid Build Coastguard Worker 			cstate->off_linktype.reg = alloc_reg(cstate);
2997*8b26181fSAndroid Build Coastguard Worker 
2998*8b26181fSAndroid Build Coastguard Worker 		s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
2999*8b26181fSAndroid Build Coastguard Worker 		s->s.k = 0;
3000*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ST);
3001*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linkpl.reg;
3002*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
3003*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ST);
3004*8b26181fSAndroid Build Coastguard Worker 		s2->s.k = cstate->off_linktype.reg;
3005*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s2);
3006*8b26181fSAndroid Build Coastguard Worker 	}
3007*8b26181fSAndroid Build Coastguard Worker 
3008*8b26181fSAndroid Build Coastguard Worker 	/*
3009*8b26181fSAndroid Build Coastguard Worker 	 * If we have any offset-loading code, append all the
3010*8b26181fSAndroid Build Coastguard Worker 	 * existing statements in the block to those statements,
3011*8b26181fSAndroid Build Coastguard Worker 	 * and make the resulting list the list of statements
3012*8b26181fSAndroid Build Coastguard Worker 	 * for the block.
3013*8b26181fSAndroid Build Coastguard Worker 	 */
3014*8b26181fSAndroid Build Coastguard Worker 	if (s != NULL) {
3015*8b26181fSAndroid Build Coastguard Worker 		sappend(s, b->stmts);
3016*8b26181fSAndroid Build Coastguard Worker 		b->stmts = s;
3017*8b26181fSAndroid Build Coastguard Worker 	}
3018*8b26181fSAndroid Build Coastguard Worker }
3019*8b26181fSAndroid Build Coastguard Worker 
3020*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_ppi_dlt_check(compiler_state_t * cstate)3021*8b26181fSAndroid Build Coastguard Worker gen_ppi_dlt_check(compiler_state_t *cstate)
3022*8b26181fSAndroid Build Coastguard Worker {
3023*8b26181fSAndroid Build Coastguard Worker 	struct slist *s_load_dlt;
3024*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
3025*8b26181fSAndroid Build Coastguard Worker 
3026*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype == DLT_PPI)
3027*8b26181fSAndroid Build Coastguard Worker 	{
3028*8b26181fSAndroid Build Coastguard Worker 		/* Create the statements that check for the DLT
3029*8b26181fSAndroid Build Coastguard Worker 		 */
3030*8b26181fSAndroid Build Coastguard Worker 		s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
3031*8b26181fSAndroid Build Coastguard Worker 		s_load_dlt->s.k = 4;
3032*8b26181fSAndroid Build Coastguard Worker 
3033*8b26181fSAndroid Build Coastguard Worker 		b = new_block(cstate, JMP(BPF_JEQ));
3034*8b26181fSAndroid Build Coastguard Worker 
3035*8b26181fSAndroid Build Coastguard Worker 		b->stmts = s_load_dlt;
3036*8b26181fSAndroid Build Coastguard Worker 		b->s.k = SWAPLONG(DLT_IEEE802_11);
3037*8b26181fSAndroid Build Coastguard Worker 	}
3038*8b26181fSAndroid Build Coastguard Worker 	else
3039*8b26181fSAndroid Build Coastguard Worker 	{
3040*8b26181fSAndroid Build Coastguard Worker 		b = NULL;
3041*8b26181fSAndroid Build Coastguard Worker 	}
3042*8b26181fSAndroid Build Coastguard Worker 
3043*8b26181fSAndroid Build Coastguard Worker 	return b;
3044*8b26181fSAndroid Build Coastguard Worker }
3045*8b26181fSAndroid Build Coastguard Worker 
3046*8b26181fSAndroid Build Coastguard Worker /*
3047*8b26181fSAndroid Build Coastguard Worker  * Take an absolute offset, and:
3048*8b26181fSAndroid Build Coastguard Worker  *
3049*8b26181fSAndroid Build Coastguard Worker  *    if it has no variable part, return NULL;
3050*8b26181fSAndroid Build Coastguard Worker  *
3051*8b26181fSAndroid Build Coastguard Worker  *    if it has a variable part, generate code to load the register
3052*8b26181fSAndroid Build Coastguard Worker  *    containing that variable part into the X register, returning
3053*8b26181fSAndroid Build Coastguard Worker  *    a pointer to that code - if no register for that offset has
3054*8b26181fSAndroid Build Coastguard Worker  *    been allocated, allocate it first.
3055*8b26181fSAndroid Build Coastguard Worker  *
3056*8b26181fSAndroid Build Coastguard Worker  * (The code to set that register will be generated later, but will
3057*8b26181fSAndroid Build Coastguard Worker  * be placed earlier in the code sequence.)
3058*8b26181fSAndroid Build Coastguard Worker  */
3059*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_abs_offset_varpart(compiler_state_t * cstate,bpf_abs_offset * off)3060*8b26181fSAndroid Build Coastguard Worker gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off)
3061*8b26181fSAndroid Build Coastguard Worker {
3062*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
3063*8b26181fSAndroid Build Coastguard Worker 
3064*8b26181fSAndroid Build Coastguard Worker 	if (off->is_variable) {
3065*8b26181fSAndroid Build Coastguard Worker 		if (off->reg == -1) {
3066*8b26181fSAndroid Build Coastguard Worker 			/*
3067*8b26181fSAndroid Build Coastguard Worker 			 * We haven't yet assigned a register for the
3068*8b26181fSAndroid Build Coastguard Worker 			 * variable part of the offset of the link-layer
3069*8b26181fSAndroid Build Coastguard Worker 			 * header; allocate one.
3070*8b26181fSAndroid Build Coastguard Worker 			 */
3071*8b26181fSAndroid Build Coastguard Worker 			off->reg = alloc_reg(cstate);
3072*8b26181fSAndroid Build Coastguard Worker 		}
3073*8b26181fSAndroid Build Coastguard Worker 
3074*8b26181fSAndroid Build Coastguard Worker 		/*
3075*8b26181fSAndroid Build Coastguard Worker 		 * Load the register containing the variable part of the
3076*8b26181fSAndroid Build Coastguard Worker 		 * offset of the link-layer header into the X register.
3077*8b26181fSAndroid Build Coastguard Worker 		 */
3078*8b26181fSAndroid Build Coastguard Worker 		s = new_stmt(cstate, BPF_LDX|BPF_MEM);
3079*8b26181fSAndroid Build Coastguard Worker 		s->s.k = off->reg;
3080*8b26181fSAndroid Build Coastguard Worker 		return s;
3081*8b26181fSAndroid Build Coastguard Worker 	} else {
3082*8b26181fSAndroid Build Coastguard Worker 		/*
3083*8b26181fSAndroid Build Coastguard Worker 		 * That offset isn't variable, there's no variable part,
3084*8b26181fSAndroid Build Coastguard Worker 		 * so we don't need to generate any code.
3085*8b26181fSAndroid Build Coastguard Worker 		 */
3086*8b26181fSAndroid Build Coastguard Worker 		return NULL;
3087*8b26181fSAndroid Build Coastguard Worker 	}
3088*8b26181fSAndroid Build Coastguard Worker }
3089*8b26181fSAndroid Build Coastguard Worker 
3090*8b26181fSAndroid Build Coastguard Worker /*
3091*8b26181fSAndroid Build Coastguard Worker  * Map an Ethernet type to the equivalent PPP type.
3092*8b26181fSAndroid Build Coastguard Worker  */
3093*8b26181fSAndroid Build Coastguard Worker static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32 ll_proto)3094*8b26181fSAndroid Build Coastguard Worker ethertype_to_ppptype(bpf_u_int32 ll_proto)
3095*8b26181fSAndroid Build Coastguard Worker {
3096*8b26181fSAndroid Build Coastguard Worker 	switch (ll_proto) {
3097*8b26181fSAndroid Build Coastguard Worker 
3098*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_IP:
3099*8b26181fSAndroid Build Coastguard Worker 		ll_proto = PPP_IP;
3100*8b26181fSAndroid Build Coastguard Worker 		break;
3101*8b26181fSAndroid Build Coastguard Worker 
3102*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_IPV6:
3103*8b26181fSAndroid Build Coastguard Worker 		ll_proto = PPP_IPV6;
3104*8b26181fSAndroid Build Coastguard Worker 		break;
3105*8b26181fSAndroid Build Coastguard Worker 
3106*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_DN:
3107*8b26181fSAndroid Build Coastguard Worker 		ll_proto = PPP_DECNET;
3108*8b26181fSAndroid Build Coastguard Worker 		break;
3109*8b26181fSAndroid Build Coastguard Worker 
3110*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_ATALK:
3111*8b26181fSAndroid Build Coastguard Worker 		ll_proto = PPP_APPLE;
3112*8b26181fSAndroid Build Coastguard Worker 		break;
3113*8b26181fSAndroid Build Coastguard Worker 
3114*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_NS:
3115*8b26181fSAndroid Build Coastguard Worker 		ll_proto = PPP_NS;
3116*8b26181fSAndroid Build Coastguard Worker 		break;
3117*8b26181fSAndroid Build Coastguard Worker 
3118*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_ISONS:
3119*8b26181fSAndroid Build Coastguard Worker 		ll_proto = PPP_OSI;
3120*8b26181fSAndroid Build Coastguard Worker 		break;
3121*8b26181fSAndroid Build Coastguard Worker 
3122*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_8021D:
3123*8b26181fSAndroid Build Coastguard Worker 		/*
3124*8b26181fSAndroid Build Coastguard Worker 		 * I'm assuming the "Bridging PDU"s that go
3125*8b26181fSAndroid Build Coastguard Worker 		 * over PPP are Spanning Tree Protocol
3126*8b26181fSAndroid Build Coastguard Worker 		 * Bridging PDUs.
3127*8b26181fSAndroid Build Coastguard Worker 		 */
3128*8b26181fSAndroid Build Coastguard Worker 		ll_proto = PPP_BRPDU;
3129*8b26181fSAndroid Build Coastguard Worker 		break;
3130*8b26181fSAndroid Build Coastguard Worker 
3131*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_IPX:
3132*8b26181fSAndroid Build Coastguard Worker 		ll_proto = PPP_IPX;
3133*8b26181fSAndroid Build Coastguard Worker 		break;
3134*8b26181fSAndroid Build Coastguard Worker 	}
3135*8b26181fSAndroid Build Coastguard Worker 	return (ll_proto);
3136*8b26181fSAndroid Build Coastguard Worker }
3137*8b26181fSAndroid Build Coastguard Worker 
3138*8b26181fSAndroid Build Coastguard Worker /*
3139*8b26181fSAndroid Build Coastguard Worker  * Generate any tests that, for encapsulation of a link-layer packet
3140*8b26181fSAndroid Build Coastguard Worker  * inside another protocol stack, need to be done to check for those
3141*8b26181fSAndroid Build Coastguard Worker  * link-layer packets (and that haven't already been done by a check
3142*8b26181fSAndroid Build Coastguard Worker  * for that encapsulation).
3143*8b26181fSAndroid Build Coastguard Worker  */
3144*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_prevlinkhdr_check(compiler_state_t * cstate)3145*8b26181fSAndroid Build Coastguard Worker gen_prevlinkhdr_check(compiler_state_t *cstate)
3146*8b26181fSAndroid Build Coastguard Worker {
3147*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
3148*8b26181fSAndroid Build Coastguard Worker 
3149*8b26181fSAndroid Build Coastguard Worker 	if (cstate->is_geneve)
3150*8b26181fSAndroid Build Coastguard Worker 		return gen_geneve_ll_check(cstate);
3151*8b26181fSAndroid Build Coastguard Worker 
3152*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->prevlinktype) {
3153*8b26181fSAndroid Build Coastguard Worker 
3154*8b26181fSAndroid Build Coastguard Worker 	case DLT_SUNATM:
3155*8b26181fSAndroid Build Coastguard Worker 		/*
3156*8b26181fSAndroid Build Coastguard Worker 		 * This is LANE-encapsulated Ethernet; check that the LANE
3157*8b26181fSAndroid Build Coastguard Worker 		 * packet doesn't begin with an LE Control marker, i.e.
3158*8b26181fSAndroid Build Coastguard Worker 		 * that it's data, not a control message.
3159*8b26181fSAndroid Build Coastguard Worker 		 *
3160*8b26181fSAndroid Build Coastguard Worker 		 * (We've already generated a test for LANE.)
3161*8b26181fSAndroid Build Coastguard Worker 		 */
3162*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
3163*8b26181fSAndroid Build Coastguard Worker 		gen_not(b0);
3164*8b26181fSAndroid Build Coastguard Worker 		return b0;
3165*8b26181fSAndroid Build Coastguard Worker 
3166*8b26181fSAndroid Build Coastguard Worker 	default:
3167*8b26181fSAndroid Build Coastguard Worker 		/*
3168*8b26181fSAndroid Build Coastguard Worker 		 * No such tests are necessary.
3169*8b26181fSAndroid Build Coastguard Worker 		 */
3170*8b26181fSAndroid Build Coastguard Worker 		return NULL;
3171*8b26181fSAndroid Build Coastguard Worker 	}
3172*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
3173*8b26181fSAndroid Build Coastguard Worker }
3174*8b26181fSAndroid Build Coastguard Worker 
3175*8b26181fSAndroid Build Coastguard Worker /*
3176*8b26181fSAndroid Build Coastguard Worker  * The three different values we should check for when checking for an
3177*8b26181fSAndroid Build Coastguard Worker  * IPv6 packet with DLT_NULL.
3178*8b26181fSAndroid Build Coastguard Worker  */
3179*8b26181fSAndroid Build Coastguard Worker #define BSD_AFNUM_INET6_BSD	24	/* NetBSD, OpenBSD, BSD/OS, Npcap */
3180*8b26181fSAndroid Build Coastguard Worker #define BSD_AFNUM_INET6_FREEBSD	28	/* FreeBSD */
3181*8b26181fSAndroid Build Coastguard Worker #define BSD_AFNUM_INET6_DARWIN	30	/* macOS, iOS, other Darwin-based OSes */
3182*8b26181fSAndroid Build Coastguard Worker 
3183*8b26181fSAndroid Build Coastguard Worker /*
3184*8b26181fSAndroid Build Coastguard Worker  * Generate code to match a particular packet type by matching the
3185*8b26181fSAndroid Build Coastguard Worker  * link-layer type field or fields in the 802.2 LLC header.
3186*8b26181fSAndroid Build Coastguard Worker  *
3187*8b26181fSAndroid Build Coastguard Worker  * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3188*8b26181fSAndroid Build Coastguard Worker  * value, if <= ETHERMTU.
3189*8b26181fSAndroid Build Coastguard Worker  */
3190*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_linktype(compiler_state_t * cstate,bpf_u_int32 ll_proto)3191*8b26181fSAndroid Build Coastguard Worker gen_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
3192*8b26181fSAndroid Build Coastguard Worker {
3193*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *b2;
3194*8b26181fSAndroid Build Coastguard Worker 	const char *description;
3195*8b26181fSAndroid Build Coastguard Worker 
3196*8b26181fSAndroid Build Coastguard Worker 	/* are we checking MPLS-encapsulated packets? */
3197*8b26181fSAndroid Build Coastguard Worker 	if (cstate->label_stack_depth > 0)
3198*8b26181fSAndroid Build Coastguard Worker 		return gen_mpls_linktype(cstate, ll_proto);
3199*8b26181fSAndroid Build Coastguard Worker 
3200*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
3201*8b26181fSAndroid Build Coastguard Worker 
3202*8b26181fSAndroid Build Coastguard Worker 	case DLT_EN10MB:
3203*8b26181fSAndroid Build Coastguard Worker 	case DLT_NETANALYZER:
3204*8b26181fSAndroid Build Coastguard Worker 	case DLT_NETANALYZER_TRANSPARENT:
3205*8b26181fSAndroid Build Coastguard Worker 		/* Geneve has an EtherType regardless of whether there is an
3206*8b26181fSAndroid Build Coastguard Worker 		 * L2 header. */
3207*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_geneve)
3208*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_prevlinkhdr_check(cstate);
3209*8b26181fSAndroid Build Coastguard Worker 		else
3210*8b26181fSAndroid Build Coastguard Worker 			b0 = NULL;
3211*8b26181fSAndroid Build Coastguard Worker 
3212*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ether_linktype(cstate, ll_proto);
3213*8b26181fSAndroid Build Coastguard Worker 		if (b0 != NULL)
3214*8b26181fSAndroid Build Coastguard Worker 			gen_and(b0, b1);
3215*8b26181fSAndroid Build Coastguard Worker 		return b1;
3216*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3217*8b26181fSAndroid Build Coastguard Worker 
3218*8b26181fSAndroid Build Coastguard Worker 	case DLT_C_HDLC:
3219*8b26181fSAndroid Build Coastguard Worker 	case DLT_HDLC:
3220*8b26181fSAndroid Build Coastguard Worker 		switch (ll_proto) {
3221*8b26181fSAndroid Build Coastguard Worker 
3222*8b26181fSAndroid Build Coastguard Worker 		case LLCSAP_ISONS:
3223*8b26181fSAndroid Build Coastguard Worker 			ll_proto = (ll_proto << 8 | LLCSAP_ISONS);
3224*8b26181fSAndroid Build Coastguard Worker 			/* fall through */
3225*8b26181fSAndroid Build Coastguard Worker 
3226*8b26181fSAndroid Build Coastguard Worker 		default:
3227*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
3228*8b26181fSAndroid Build Coastguard Worker 			/*NOTREACHED*/
3229*8b26181fSAndroid Build Coastguard Worker 		}
3230*8b26181fSAndroid Build Coastguard Worker 
3231*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11:
3232*8b26181fSAndroid Build Coastguard Worker 	case DLT_PRISM_HEADER:
3233*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO_AVS:
3234*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO:
3235*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPI:
3236*8b26181fSAndroid Build Coastguard Worker 		/*
3237*8b26181fSAndroid Build Coastguard Worker 		 * Check that we have a data frame.
3238*8b26181fSAndroid Build Coastguard Worker 		 */
3239*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_check_802_11_data_frame(cstate);
3240*8b26181fSAndroid Build Coastguard Worker 
3241*8b26181fSAndroid Build Coastguard Worker 		/*
3242*8b26181fSAndroid Build Coastguard Worker 		 * Now check for the specified link-layer type.
3243*8b26181fSAndroid Build Coastguard Worker 		 */
3244*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_llc_linktype(cstate, ll_proto);
3245*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
3246*8b26181fSAndroid Build Coastguard Worker 		return b1;
3247*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3248*8b26181fSAndroid Build Coastguard Worker 
3249*8b26181fSAndroid Build Coastguard Worker 	case DLT_FDDI:
3250*8b26181fSAndroid Build Coastguard Worker 		/*
3251*8b26181fSAndroid Build Coastguard Worker 		 * XXX - check for LLC frames.
3252*8b26181fSAndroid Build Coastguard Worker 		 */
3253*8b26181fSAndroid Build Coastguard Worker 		return gen_llc_linktype(cstate, ll_proto);
3254*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3255*8b26181fSAndroid Build Coastguard Worker 
3256*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802:
3257*8b26181fSAndroid Build Coastguard Worker 		/*
3258*8b26181fSAndroid Build Coastguard Worker 		 * XXX - check for LLC PDUs, as per IEEE 802.5.
3259*8b26181fSAndroid Build Coastguard Worker 		 */
3260*8b26181fSAndroid Build Coastguard Worker 		return gen_llc_linktype(cstate, ll_proto);
3261*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3262*8b26181fSAndroid Build Coastguard Worker 
3263*8b26181fSAndroid Build Coastguard Worker 	case DLT_ATM_RFC1483:
3264*8b26181fSAndroid Build Coastguard Worker 	case DLT_ATM_CLIP:
3265*8b26181fSAndroid Build Coastguard Worker 	case DLT_IP_OVER_FC:
3266*8b26181fSAndroid Build Coastguard Worker 		return gen_llc_linktype(cstate, ll_proto);
3267*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3268*8b26181fSAndroid Build Coastguard Worker 
3269*8b26181fSAndroid Build Coastguard Worker 	case DLT_SUNATM:
3270*8b26181fSAndroid Build Coastguard Worker 		/*
3271*8b26181fSAndroid Build Coastguard Worker 		 * Check for an LLC-encapsulated version of this protocol;
3272*8b26181fSAndroid Build Coastguard Worker 		 * if we were checking for LANE, linktype would no longer
3273*8b26181fSAndroid Build Coastguard Worker 		 * be DLT_SUNATM.
3274*8b26181fSAndroid Build Coastguard Worker 		 *
3275*8b26181fSAndroid Build Coastguard Worker 		 * Check for LLC encapsulation and then check the protocol.
3276*8b26181fSAndroid Build Coastguard Worker 		 */
3277*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3278*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_llc_linktype(cstate, ll_proto);
3279*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
3280*8b26181fSAndroid Build Coastguard Worker 		return b1;
3281*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3282*8b26181fSAndroid Build Coastguard Worker 
3283*8b26181fSAndroid Build Coastguard Worker 	case DLT_LINUX_SLL:
3284*8b26181fSAndroid Build Coastguard Worker 		return gen_linux_sll_linktype(cstate, ll_proto);
3285*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3286*8b26181fSAndroid Build Coastguard Worker 
3287*8b26181fSAndroid Build Coastguard Worker 	case DLT_SLIP:
3288*8b26181fSAndroid Build Coastguard Worker 	case DLT_SLIP_BSDOS:
3289*8b26181fSAndroid Build Coastguard Worker 	case DLT_RAW:
3290*8b26181fSAndroid Build Coastguard Worker 		/*
3291*8b26181fSAndroid Build Coastguard Worker 		 * These types don't provide any type field; packets
3292*8b26181fSAndroid Build Coastguard Worker 		 * are always IPv4 or IPv6.
3293*8b26181fSAndroid Build Coastguard Worker 		 *
3294*8b26181fSAndroid Build Coastguard Worker 		 * XXX - for IPv4, check for a version number of 4, and,
3295*8b26181fSAndroid Build Coastguard Worker 		 * for IPv6, check for a version number of 6?
3296*8b26181fSAndroid Build Coastguard Worker 		 */
3297*8b26181fSAndroid Build Coastguard Worker 		switch (ll_proto) {
3298*8b26181fSAndroid Build Coastguard Worker 
3299*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IP:
3300*8b26181fSAndroid Build Coastguard Worker 			/* Check for a version number of 4. */
3301*8b26181fSAndroid Build Coastguard Worker 			return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0);
3302*8b26181fSAndroid Build Coastguard Worker 
3303*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IPV6:
3304*8b26181fSAndroid Build Coastguard Worker 			/* Check for a version number of 6. */
3305*8b26181fSAndroid Build Coastguard Worker 			return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0);
3306*8b26181fSAndroid Build Coastguard Worker 
3307*8b26181fSAndroid Build Coastguard Worker 		default:
3308*8b26181fSAndroid Build Coastguard Worker 			return gen_false(cstate);	/* always false */
3309*8b26181fSAndroid Build Coastguard Worker 		}
3310*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3311*8b26181fSAndroid Build Coastguard Worker 
3312*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPV4:
3313*8b26181fSAndroid Build Coastguard Worker 		/*
3314*8b26181fSAndroid Build Coastguard Worker 		 * Raw IPv4, so no type field.
3315*8b26181fSAndroid Build Coastguard Worker 		 */
3316*8b26181fSAndroid Build Coastguard Worker 		if (ll_proto == ETHERTYPE_IP)
3317*8b26181fSAndroid Build Coastguard Worker 			return gen_true(cstate);	/* always true */
3318*8b26181fSAndroid Build Coastguard Worker 
3319*8b26181fSAndroid Build Coastguard Worker 		/* Checking for something other than IPv4; always false */
3320*8b26181fSAndroid Build Coastguard Worker 		return gen_false(cstate);
3321*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3322*8b26181fSAndroid Build Coastguard Worker 
3323*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPV6:
3324*8b26181fSAndroid Build Coastguard Worker 		/*
3325*8b26181fSAndroid Build Coastguard Worker 		 * Raw IPv6, so no type field.
3326*8b26181fSAndroid Build Coastguard Worker 		 */
3327*8b26181fSAndroid Build Coastguard Worker 		if (ll_proto == ETHERTYPE_IPV6)
3328*8b26181fSAndroid Build Coastguard Worker 			return gen_true(cstate);	/* always true */
3329*8b26181fSAndroid Build Coastguard Worker 
3330*8b26181fSAndroid Build Coastguard Worker 		/* Checking for something other than IPv6; always false */
3331*8b26181fSAndroid Build Coastguard Worker 		return gen_false(cstate);
3332*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3333*8b26181fSAndroid Build Coastguard Worker 
3334*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP:
3335*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_PPPD:
3336*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_SERIAL:
3337*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_ETHER:
3338*8b26181fSAndroid Build Coastguard Worker 		/*
3339*8b26181fSAndroid Build Coastguard Worker 		 * We use Ethernet protocol types inside libpcap;
3340*8b26181fSAndroid Build Coastguard Worker 		 * map them to the corresponding PPP protocol types.
3341*8b26181fSAndroid Build Coastguard Worker 		 */
3342*8b26181fSAndroid Build Coastguard Worker 		return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
3343*8b26181fSAndroid Build Coastguard Worker 		    ethertype_to_ppptype(ll_proto));
3344*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3345*8b26181fSAndroid Build Coastguard Worker 
3346*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_BSDOS:
3347*8b26181fSAndroid Build Coastguard Worker 		/*
3348*8b26181fSAndroid Build Coastguard Worker 		 * We use Ethernet protocol types inside libpcap;
3349*8b26181fSAndroid Build Coastguard Worker 		 * map them to the corresponding PPP protocol types.
3350*8b26181fSAndroid Build Coastguard Worker 		 */
3351*8b26181fSAndroid Build Coastguard Worker 		switch (ll_proto) {
3352*8b26181fSAndroid Build Coastguard Worker 
3353*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IP:
3354*8b26181fSAndroid Build Coastguard Worker 			/*
3355*8b26181fSAndroid Build Coastguard Worker 			 * Also check for Van Jacobson-compressed IP.
3356*8b26181fSAndroid Build Coastguard Worker 			 * XXX - do this for other forms of PPP?
3357*8b26181fSAndroid Build Coastguard Worker 			 */
3358*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP);
3359*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC);
3360*8b26181fSAndroid Build Coastguard Worker 			gen_or(b0, b1);
3361*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC);
3362*8b26181fSAndroid Build Coastguard Worker 			gen_or(b1, b0);
3363*8b26181fSAndroid Build Coastguard Worker 			return b0;
3364*8b26181fSAndroid Build Coastguard Worker 
3365*8b26181fSAndroid Build Coastguard Worker 		default:
3366*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
3367*8b26181fSAndroid Build Coastguard Worker 			    ethertype_to_ppptype(ll_proto));
3368*8b26181fSAndroid Build Coastguard Worker 		}
3369*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3370*8b26181fSAndroid Build Coastguard Worker 
3371*8b26181fSAndroid Build Coastguard Worker 	case DLT_NULL:
3372*8b26181fSAndroid Build Coastguard Worker 	case DLT_LOOP:
3373*8b26181fSAndroid Build Coastguard Worker 	case DLT_ENC:
3374*8b26181fSAndroid Build Coastguard Worker 		switch (ll_proto) {
3375*8b26181fSAndroid Build Coastguard Worker 
3376*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IP:
3377*8b26181fSAndroid Build Coastguard Worker 			return (gen_loopback_linktype(cstate, AF_INET));
3378*8b26181fSAndroid Build Coastguard Worker 
3379*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IPV6:
3380*8b26181fSAndroid Build Coastguard Worker 			/*
3381*8b26181fSAndroid Build Coastguard Worker 			 * AF_ values may, unfortunately, be platform-
3382*8b26181fSAndroid Build Coastguard Worker 			 * dependent; AF_INET isn't, because everybody
3383*8b26181fSAndroid Build Coastguard Worker 			 * used 4.2BSD's value, but AF_INET6 is, because
3384*8b26181fSAndroid Build Coastguard Worker 			 * 4.2BSD didn't have a value for it (given that
3385*8b26181fSAndroid Build Coastguard Worker 			 * IPv6 didn't exist back in the early 1980's),
3386*8b26181fSAndroid Build Coastguard Worker 			 * and they all picked their own values.
3387*8b26181fSAndroid Build Coastguard Worker 			 *
3388*8b26181fSAndroid Build Coastguard Worker 			 * This means that, if we're reading from a
3389*8b26181fSAndroid Build Coastguard Worker 			 * savefile, we need to check for all the
3390*8b26181fSAndroid Build Coastguard Worker 			 * possible values.
3391*8b26181fSAndroid Build Coastguard Worker 			 *
3392*8b26181fSAndroid Build Coastguard Worker 			 * If we're doing a live capture, we only need
3393*8b26181fSAndroid Build Coastguard Worker 			 * to check for this platform's value; however,
3394*8b26181fSAndroid Build Coastguard Worker 			 * Npcap uses 24, which isn't Windows's AF_INET6
3395*8b26181fSAndroid Build Coastguard Worker 			 * value.  (Given the multiple different values,
3396*8b26181fSAndroid Build Coastguard Worker 			 * programs that read pcap files shouldn't be
3397*8b26181fSAndroid Build Coastguard Worker 			 * checking for their platform's AF_INET6 value
3398*8b26181fSAndroid Build Coastguard Worker 			 * anyway, they should check for all of the
3399*8b26181fSAndroid Build Coastguard Worker 			 * possible values. and they might as well do
3400*8b26181fSAndroid Build Coastguard Worker 			 * that even for live captures.)
3401*8b26181fSAndroid Build Coastguard Worker 			 */
3402*8b26181fSAndroid Build Coastguard Worker 			if (cstate->bpf_pcap->rfile != NULL) {
3403*8b26181fSAndroid Build Coastguard Worker 				/*
3404*8b26181fSAndroid Build Coastguard Worker 				 * Savefile - check for all three
3405*8b26181fSAndroid Build Coastguard Worker 				 * possible IPv6 values.
3406*8b26181fSAndroid Build Coastguard Worker 				 */
3407*8b26181fSAndroid Build Coastguard Worker 				b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD);
3408*8b26181fSAndroid Build Coastguard Worker 				b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD);
3409*8b26181fSAndroid Build Coastguard Worker 				gen_or(b0, b1);
3410*8b26181fSAndroid Build Coastguard Worker 				b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN);
3411*8b26181fSAndroid Build Coastguard Worker 				gen_or(b0, b1);
3412*8b26181fSAndroid Build Coastguard Worker 				return (b1);
3413*8b26181fSAndroid Build Coastguard Worker 			} else {
3414*8b26181fSAndroid Build Coastguard Worker 				/*
3415*8b26181fSAndroid Build Coastguard Worker 				 * Live capture, so we only need to
3416*8b26181fSAndroid Build Coastguard Worker 				 * check for the value used on this
3417*8b26181fSAndroid Build Coastguard Worker 				 * platform.
3418*8b26181fSAndroid Build Coastguard Worker 				 */
3419*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
3420*8b26181fSAndroid Build Coastguard Worker 				/*
3421*8b26181fSAndroid Build Coastguard Worker 				 * Npcap doesn't use Windows's AF_INET6,
3422*8b26181fSAndroid Build Coastguard Worker 				 * as that collides with AF_IPX on
3423*8b26181fSAndroid Build Coastguard Worker 				 * some BSDs (both have the value 23).
3424*8b26181fSAndroid Build Coastguard Worker 				 * Instead, it uses 24.
3425*8b26181fSAndroid Build Coastguard Worker 				 */
3426*8b26181fSAndroid Build Coastguard Worker 				return (gen_loopback_linktype(cstate, 24));
3427*8b26181fSAndroid Build Coastguard Worker #else /* _WIN32 */
3428*8b26181fSAndroid Build Coastguard Worker #ifdef AF_INET6
3429*8b26181fSAndroid Build Coastguard Worker 				return (gen_loopback_linktype(cstate, AF_INET6));
3430*8b26181fSAndroid Build Coastguard Worker #else /* AF_INET6 */
3431*8b26181fSAndroid Build Coastguard Worker 				/*
3432*8b26181fSAndroid Build Coastguard Worker 				 * I guess this platform doesn't support
3433*8b26181fSAndroid Build Coastguard Worker 				 * IPv6, so we just reject all packets.
3434*8b26181fSAndroid Build Coastguard Worker 				 */
3435*8b26181fSAndroid Build Coastguard Worker 				return gen_false(cstate);
3436*8b26181fSAndroid Build Coastguard Worker #endif /* AF_INET6 */
3437*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
3438*8b26181fSAndroid Build Coastguard Worker 			}
3439*8b26181fSAndroid Build Coastguard Worker 
3440*8b26181fSAndroid Build Coastguard Worker 		default:
3441*8b26181fSAndroid Build Coastguard Worker 			/*
3442*8b26181fSAndroid Build Coastguard Worker 			 * Not a type on which we support filtering.
3443*8b26181fSAndroid Build Coastguard Worker 			 * XXX - support those that have AF_ values
3444*8b26181fSAndroid Build Coastguard Worker 			 * #defined on this platform, at least?
3445*8b26181fSAndroid Build Coastguard Worker 			 */
3446*8b26181fSAndroid Build Coastguard Worker 			return gen_false(cstate);
3447*8b26181fSAndroid Build Coastguard Worker 		}
3448*8b26181fSAndroid Build Coastguard Worker 
3449*8b26181fSAndroid Build Coastguard Worker 	case DLT_PFLOG:
3450*8b26181fSAndroid Build Coastguard Worker 		/*
3451*8b26181fSAndroid Build Coastguard Worker 		 * af field is host byte order in contrast to the rest of
3452*8b26181fSAndroid Build Coastguard Worker 		 * the packet.
3453*8b26181fSAndroid Build Coastguard Worker 		 */
3454*8b26181fSAndroid Build Coastguard Worker 		if (ll_proto == ETHERTYPE_IP)
3455*8b26181fSAndroid Build Coastguard Worker 			return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
3456*8b26181fSAndroid Build Coastguard Worker 			    BPF_B, AF_INET));
3457*8b26181fSAndroid Build Coastguard Worker 		else if (ll_proto == ETHERTYPE_IPV6)
3458*8b26181fSAndroid Build Coastguard Worker 			return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
3459*8b26181fSAndroid Build Coastguard Worker 			    BPF_B, AF_INET6));
3460*8b26181fSAndroid Build Coastguard Worker 		else
3461*8b26181fSAndroid Build Coastguard Worker 			return gen_false(cstate);
3462*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3463*8b26181fSAndroid Build Coastguard Worker 
3464*8b26181fSAndroid Build Coastguard Worker 	case DLT_ARCNET:
3465*8b26181fSAndroid Build Coastguard Worker 	case DLT_ARCNET_LINUX:
3466*8b26181fSAndroid Build Coastguard Worker 		/*
3467*8b26181fSAndroid Build Coastguard Worker 		 * XXX should we check for first fragment if the protocol
3468*8b26181fSAndroid Build Coastguard Worker 		 * uses PHDS?
3469*8b26181fSAndroid Build Coastguard Worker 		 */
3470*8b26181fSAndroid Build Coastguard Worker 		switch (ll_proto) {
3471*8b26181fSAndroid Build Coastguard Worker 
3472*8b26181fSAndroid Build Coastguard Worker 		default:
3473*8b26181fSAndroid Build Coastguard Worker 			return gen_false(cstate);
3474*8b26181fSAndroid Build Coastguard Worker 
3475*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IPV6:
3476*8b26181fSAndroid Build Coastguard Worker 			return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3477*8b26181fSAndroid Build Coastguard Worker 				ARCTYPE_INET6));
3478*8b26181fSAndroid Build Coastguard Worker 
3479*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IP:
3480*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3481*8b26181fSAndroid Build Coastguard Worker 			    ARCTYPE_IP);
3482*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3483*8b26181fSAndroid Build Coastguard Worker 			    ARCTYPE_IP_OLD);
3484*8b26181fSAndroid Build Coastguard Worker 			gen_or(b0, b1);
3485*8b26181fSAndroid Build Coastguard Worker 			return (b1);
3486*8b26181fSAndroid Build Coastguard Worker 
3487*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_ARP:
3488*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3489*8b26181fSAndroid Build Coastguard Worker 			    ARCTYPE_ARP);
3490*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3491*8b26181fSAndroid Build Coastguard Worker 			    ARCTYPE_ARP_OLD);
3492*8b26181fSAndroid Build Coastguard Worker 			gen_or(b0, b1);
3493*8b26181fSAndroid Build Coastguard Worker 			return (b1);
3494*8b26181fSAndroid Build Coastguard Worker 
3495*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_REVARP:
3496*8b26181fSAndroid Build Coastguard Worker 			return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3497*8b26181fSAndroid Build Coastguard Worker 			    ARCTYPE_REVARP));
3498*8b26181fSAndroid Build Coastguard Worker 
3499*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_ATALK:
3500*8b26181fSAndroid Build Coastguard Worker 			return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
3501*8b26181fSAndroid Build Coastguard Worker 			    ARCTYPE_ATALK));
3502*8b26181fSAndroid Build Coastguard Worker 		}
3503*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3504*8b26181fSAndroid Build Coastguard Worker 
3505*8b26181fSAndroid Build Coastguard Worker 	case DLT_LTALK:
3506*8b26181fSAndroid Build Coastguard Worker 		switch (ll_proto) {
3507*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_ATALK:
3508*8b26181fSAndroid Build Coastguard Worker 			return gen_true(cstate);
3509*8b26181fSAndroid Build Coastguard Worker 		default:
3510*8b26181fSAndroid Build Coastguard Worker 			return gen_false(cstate);
3511*8b26181fSAndroid Build Coastguard Worker 		}
3512*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3513*8b26181fSAndroid Build Coastguard Worker 
3514*8b26181fSAndroid Build Coastguard Worker 	case DLT_FRELAY:
3515*8b26181fSAndroid Build Coastguard Worker 		/*
3516*8b26181fSAndroid Build Coastguard Worker 		 * XXX - assumes a 2-byte Frame Relay header with
3517*8b26181fSAndroid Build Coastguard Worker 		 * DLCI and flags.  What if the address is longer?
3518*8b26181fSAndroid Build Coastguard Worker 		 */
3519*8b26181fSAndroid Build Coastguard Worker 		switch (ll_proto) {
3520*8b26181fSAndroid Build Coastguard Worker 
3521*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IP:
3522*8b26181fSAndroid Build Coastguard Worker 			/*
3523*8b26181fSAndroid Build Coastguard Worker 			 * Check for the special NLPID for IP.
3524*8b26181fSAndroid Build Coastguard Worker 			 */
3525*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc);
3526*8b26181fSAndroid Build Coastguard Worker 
3527*8b26181fSAndroid Build Coastguard Worker 		case ETHERTYPE_IPV6:
3528*8b26181fSAndroid Build Coastguard Worker 			/*
3529*8b26181fSAndroid Build Coastguard Worker 			 * Check for the special NLPID for IPv6.
3530*8b26181fSAndroid Build Coastguard Worker 			 */
3531*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e);
3532*8b26181fSAndroid Build Coastguard Worker 
3533*8b26181fSAndroid Build Coastguard Worker 		case LLCSAP_ISONS:
3534*8b26181fSAndroid Build Coastguard Worker 			/*
3535*8b26181fSAndroid Build Coastguard Worker 			 * Check for several OSI protocols.
3536*8b26181fSAndroid Build Coastguard Worker 			 *
3537*8b26181fSAndroid Build Coastguard Worker 			 * Frame Relay packets typically have an OSI
3538*8b26181fSAndroid Build Coastguard Worker 			 * NLPID at the beginning; we check for each
3539*8b26181fSAndroid Build Coastguard Worker 			 * of them.
3540*8b26181fSAndroid Build Coastguard Worker 			 *
3541*8b26181fSAndroid Build Coastguard Worker 			 * What we check for is the NLPID and a frame
3542*8b26181fSAndroid Build Coastguard Worker 			 * control field of UI, i.e. 0x03 followed
3543*8b26181fSAndroid Build Coastguard Worker 			 * by the NLPID.
3544*8b26181fSAndroid Build Coastguard Worker 			 */
3545*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3546*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3547*8b26181fSAndroid Build Coastguard Worker 			b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3548*8b26181fSAndroid Build Coastguard Worker 			gen_or(b1, b2);
3549*8b26181fSAndroid Build Coastguard Worker 			gen_or(b0, b2);
3550*8b26181fSAndroid Build Coastguard Worker 			return b2;
3551*8b26181fSAndroid Build Coastguard Worker 
3552*8b26181fSAndroid Build Coastguard Worker 		default:
3553*8b26181fSAndroid Build Coastguard Worker 			return gen_false(cstate);
3554*8b26181fSAndroid Build Coastguard Worker 		}
3555*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3556*8b26181fSAndroid Build Coastguard Worker 
3557*8b26181fSAndroid Build Coastguard Worker 	case DLT_MFR:
3558*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented");
3559*8b26181fSAndroid Build Coastguard Worker 
3560*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MFR:
3561*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MLFR:
3562*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MLPPP:
3563*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM1:
3564*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM2:
3565*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_PPPOE:
3566*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_PPPOE_ATM:
3567*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_GGSN:
3568*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ES:
3569*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MONITOR:
3570*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_SERVICES:
3571*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ETHER:
3572*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_PPP:
3573*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_FRELAY:
3574*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_CHDLC:
3575*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_VP:
3576*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ST:
3577*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ISM:
3578*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_VS:
3579*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_SRX_E2E:
3580*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_FIBRECHANNEL:
3581*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM_CEMIC:
3582*8b26181fSAndroid Build Coastguard Worker 
3583*8b26181fSAndroid Build Coastguard Worker 		/* just lets verify the magic number for now -
3584*8b26181fSAndroid Build Coastguard Worker 		 * on ATM we may have up to 6 different encapsulations on the wire
3585*8b26181fSAndroid Build Coastguard Worker 		 * and need a lot of heuristics to figure out that the payload
3586*8b26181fSAndroid Build Coastguard Worker 		 * might be;
3587*8b26181fSAndroid Build Coastguard Worker 		 *
3588*8b26181fSAndroid Build Coastguard Worker 		 * FIXME encapsulation specific BPF_ filters
3589*8b26181fSAndroid Build Coastguard Worker 		 */
3590*8b26181fSAndroid Build Coastguard Worker 		return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3591*8b26181fSAndroid Build Coastguard Worker 
3592*8b26181fSAndroid Build Coastguard Worker 	case DLT_BACNET_MS_TP:
3593*8b26181fSAndroid Build Coastguard Worker 		return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000);
3594*8b26181fSAndroid Build Coastguard Worker 
3595*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPNET:
3596*8b26181fSAndroid Build Coastguard Worker 		return gen_ipnet_linktype(cstate, ll_proto);
3597*8b26181fSAndroid Build Coastguard Worker 
3598*8b26181fSAndroid Build Coastguard Worker 	case DLT_LINUX_IRDA:
3599*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "IrDA link-layer type filtering not implemented");
3600*8b26181fSAndroid Build Coastguard Worker 
3601*8b26181fSAndroid Build Coastguard Worker 	case DLT_DOCSIS:
3602*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "DOCSIS link-layer type filtering not implemented");
3603*8b26181fSAndroid Build Coastguard Worker 
3604*8b26181fSAndroid Build Coastguard Worker 	case DLT_MTP2:
3605*8b26181fSAndroid Build Coastguard Worker 	case DLT_MTP2_WITH_PHDR:
3606*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "MTP2 link-layer type filtering not implemented");
3607*8b26181fSAndroid Build Coastguard Worker 
3608*8b26181fSAndroid Build Coastguard Worker 	case DLT_ERF:
3609*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "ERF link-layer type filtering not implemented");
3610*8b26181fSAndroid Build Coastguard Worker 
3611*8b26181fSAndroid Build Coastguard Worker 	case DLT_PFSYNC:
3612*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "PFSYNC link-layer type filtering not implemented");
3613*8b26181fSAndroid Build Coastguard Worker 
3614*8b26181fSAndroid Build Coastguard Worker 	case DLT_LINUX_LAPD:
3615*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "LAPD link-layer type filtering not implemented");
3616*8b26181fSAndroid Build Coastguard Worker 
3617*8b26181fSAndroid Build Coastguard Worker 	case DLT_USB_FREEBSD:
3618*8b26181fSAndroid Build Coastguard Worker 	case DLT_USB_LINUX:
3619*8b26181fSAndroid Build Coastguard Worker 	case DLT_USB_LINUX_MMAPPED:
3620*8b26181fSAndroid Build Coastguard Worker 	case DLT_USBPCAP:
3621*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "USB link-layer type filtering not implemented");
3622*8b26181fSAndroid Build Coastguard Worker 
3623*8b26181fSAndroid Build Coastguard Worker 	case DLT_BLUETOOTH_HCI_H4:
3624*8b26181fSAndroid Build Coastguard Worker 	case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3625*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "Bluetooth link-layer type filtering not implemented");
3626*8b26181fSAndroid Build Coastguard Worker 
3627*8b26181fSAndroid Build Coastguard Worker 	case DLT_CAN20B:
3628*8b26181fSAndroid Build Coastguard Worker 	case DLT_CAN_SOCKETCAN:
3629*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "CAN link-layer type filtering not implemented");
3630*8b26181fSAndroid Build Coastguard Worker 
3631*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_15_4:
3632*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_15_4_LINUX:
3633*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_15_4_NONASK_PHY:
3634*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_15_4_NOFCS:
3635*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_15_4_TAP:
3636*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented");
3637*8b26181fSAndroid Build Coastguard Worker 
3638*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_16_MAC_CPS_RADIO:
3639*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented");
3640*8b26181fSAndroid Build Coastguard Worker 
3641*8b26181fSAndroid Build Coastguard Worker 	case DLT_SITA:
3642*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "SITA link-layer type filtering not implemented");
3643*8b26181fSAndroid Build Coastguard Worker 
3644*8b26181fSAndroid Build Coastguard Worker 	case DLT_RAIF1:
3645*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "RAIF1 link-layer type filtering not implemented");
3646*8b26181fSAndroid Build Coastguard Worker 
3647*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPMB_KONTRON:
3648*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPMB_LINUX:
3649*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "IPMB link-layer type filtering not implemented");
3650*8b26181fSAndroid Build Coastguard Worker 
3651*8b26181fSAndroid Build Coastguard Worker 	case DLT_AX25_KISS:
3652*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "AX.25 link-layer type filtering not implemented");
3653*8b26181fSAndroid Build Coastguard Worker 
3654*8b26181fSAndroid Build Coastguard Worker 	case DLT_NFLOG:
3655*8b26181fSAndroid Build Coastguard Worker 		/* Using the fixed-size NFLOG header it is possible to tell only
3656*8b26181fSAndroid Build Coastguard Worker 		 * the address family of the packet, other meaningful data is
3657*8b26181fSAndroid Build Coastguard Worker 		 * either missing or behind TLVs.
3658*8b26181fSAndroid Build Coastguard Worker 		 */
3659*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "NFLOG link-layer type filtering not implemented");
3660*8b26181fSAndroid Build Coastguard Worker 
3661*8b26181fSAndroid Build Coastguard Worker 	default:
3662*8b26181fSAndroid Build Coastguard Worker 		/*
3663*8b26181fSAndroid Build Coastguard Worker 		 * Does this link-layer header type have a field
3664*8b26181fSAndroid Build Coastguard Worker 		 * indicating the type of the next protocol?  If
3665*8b26181fSAndroid Build Coastguard Worker 		 * so, off_linktype.constant_part will be the offset of that
3666*8b26181fSAndroid Build Coastguard Worker 		 * field in the packet; if not, it will be OFFSET_NOT_SET.
3667*8b26181fSAndroid Build Coastguard Worker 		 */
3668*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) {
3669*8b26181fSAndroid Build Coastguard Worker 			/*
3670*8b26181fSAndroid Build Coastguard Worker 			 * Yes; assume it's an Ethernet type.  (If
3671*8b26181fSAndroid Build Coastguard Worker 			 * it's not, it needs to be handled specially
3672*8b26181fSAndroid Build Coastguard Worker 			 * above.)
3673*8b26181fSAndroid Build Coastguard Worker 			 */
3674*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
3675*8b26181fSAndroid Build Coastguard Worker 			/*NOTREACHED */
3676*8b26181fSAndroid Build Coastguard Worker 		} else {
3677*8b26181fSAndroid Build Coastguard Worker 			/*
3678*8b26181fSAndroid Build Coastguard Worker 			 * No; report an error.
3679*8b26181fSAndroid Build Coastguard Worker 			 */
3680*8b26181fSAndroid Build Coastguard Worker 			description = pcap_datalink_val_to_description_or_dlt(cstate->linktype);
3681*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "%s link-layer type filtering not implemented",
3682*8b26181fSAndroid Build Coastguard Worker 			    description);
3683*8b26181fSAndroid Build Coastguard Worker 			/*NOTREACHED */
3684*8b26181fSAndroid Build Coastguard Worker 		}
3685*8b26181fSAndroid Build Coastguard Worker 	}
3686*8b26181fSAndroid Build Coastguard Worker }
3687*8b26181fSAndroid Build Coastguard Worker 
3688*8b26181fSAndroid Build Coastguard Worker /*
3689*8b26181fSAndroid Build Coastguard Worker  * Check for an LLC SNAP packet with a given organization code and
3690*8b26181fSAndroid Build Coastguard Worker  * protocol type; we check the entire contents of the 802.2 LLC and
3691*8b26181fSAndroid Build Coastguard Worker  * snap headers, checking for DSAP and SSAP of SNAP and a control
3692*8b26181fSAndroid Build Coastguard Worker  * field of 0x03 in the LLC header, and for the specified organization
3693*8b26181fSAndroid Build Coastguard Worker  * code and protocol type in the SNAP header.
3694*8b26181fSAndroid Build Coastguard Worker  */
3695*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_snap(compiler_state_t * cstate,bpf_u_int32 orgcode,bpf_u_int32 ptype)3696*8b26181fSAndroid Build Coastguard Worker gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype)
3697*8b26181fSAndroid Build Coastguard Worker {
3698*8b26181fSAndroid Build Coastguard Worker 	u_char snapblock[8];
3699*8b26181fSAndroid Build Coastguard Worker 
3700*8b26181fSAndroid Build Coastguard Worker 	snapblock[0] = LLCSAP_SNAP;		/* DSAP = SNAP */
3701*8b26181fSAndroid Build Coastguard Worker 	snapblock[1] = LLCSAP_SNAP;		/* SSAP = SNAP */
3702*8b26181fSAndroid Build Coastguard Worker 	snapblock[2] = 0x03;			/* control = UI */
3703*8b26181fSAndroid Build Coastguard Worker 	snapblock[3] = (u_char)(orgcode >> 16);	/* upper 8 bits of organization code */
3704*8b26181fSAndroid Build Coastguard Worker 	snapblock[4] = (u_char)(orgcode >> 8);	/* middle 8 bits of organization code */
3705*8b26181fSAndroid Build Coastguard Worker 	snapblock[5] = (u_char)(orgcode >> 0);	/* lower 8 bits of organization code */
3706*8b26181fSAndroid Build Coastguard Worker 	snapblock[6] = (u_char)(ptype >> 8);	/* upper 8 bits of protocol type */
3707*8b26181fSAndroid Build Coastguard Worker 	snapblock[7] = (u_char)(ptype >> 0);	/* lower 8 bits of protocol type */
3708*8b26181fSAndroid Build Coastguard Worker 	return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock);
3709*8b26181fSAndroid Build Coastguard Worker }
3710*8b26181fSAndroid Build Coastguard Worker 
3711*8b26181fSAndroid Build Coastguard Worker /*
3712*8b26181fSAndroid Build Coastguard Worker  * Generate code to match frames with an LLC header.
3713*8b26181fSAndroid Build Coastguard Worker  */
3714*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_llc_internal(compiler_state_t * cstate)3715*8b26181fSAndroid Build Coastguard Worker gen_llc_internal(compiler_state_t *cstate)
3716*8b26181fSAndroid Build Coastguard Worker {
3717*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
3718*8b26181fSAndroid Build Coastguard Worker 
3719*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
3720*8b26181fSAndroid Build Coastguard Worker 
3721*8b26181fSAndroid Build Coastguard Worker 	case DLT_EN10MB:
3722*8b26181fSAndroid Build Coastguard Worker 		/*
3723*8b26181fSAndroid Build Coastguard Worker 		 * We check for an Ethernet type field less than
3724*8b26181fSAndroid Build Coastguard Worker 		 * 1500, which means it's an 802.3 length field.
3725*8b26181fSAndroid Build Coastguard Worker 		 */
3726*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
3727*8b26181fSAndroid Build Coastguard Worker 		gen_not(b0);
3728*8b26181fSAndroid Build Coastguard Worker 
3729*8b26181fSAndroid Build Coastguard Worker 		/*
3730*8b26181fSAndroid Build Coastguard Worker 		 * Now check for the purported DSAP and SSAP not being
3731*8b26181fSAndroid Build Coastguard Worker 		 * 0xFF, to rule out NetWare-over-802.3.
3732*8b26181fSAndroid Build Coastguard Worker 		 */
3733*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
3734*8b26181fSAndroid Build Coastguard Worker 		gen_not(b1);
3735*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
3736*8b26181fSAndroid Build Coastguard Worker 		return b1;
3737*8b26181fSAndroid Build Coastguard Worker 
3738*8b26181fSAndroid Build Coastguard Worker 	case DLT_SUNATM:
3739*8b26181fSAndroid Build Coastguard Worker 		/*
3740*8b26181fSAndroid Build Coastguard Worker 		 * We check for LLC traffic.
3741*8b26181fSAndroid Build Coastguard Worker 		 */
3742*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmtype_llc(cstate);
3743*8b26181fSAndroid Build Coastguard Worker 		return b0;
3744*8b26181fSAndroid Build Coastguard Worker 
3745*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802:	/* Token Ring */
3746*8b26181fSAndroid Build Coastguard Worker 		/*
3747*8b26181fSAndroid Build Coastguard Worker 		 * XXX - check for LLC frames.
3748*8b26181fSAndroid Build Coastguard Worker 		 */
3749*8b26181fSAndroid Build Coastguard Worker 		return gen_true(cstate);
3750*8b26181fSAndroid Build Coastguard Worker 
3751*8b26181fSAndroid Build Coastguard Worker 	case DLT_FDDI:
3752*8b26181fSAndroid Build Coastguard Worker 		/*
3753*8b26181fSAndroid Build Coastguard Worker 		 * XXX - check for LLC frames.
3754*8b26181fSAndroid Build Coastguard Worker 		 */
3755*8b26181fSAndroid Build Coastguard Worker 		return gen_true(cstate);
3756*8b26181fSAndroid Build Coastguard Worker 
3757*8b26181fSAndroid Build Coastguard Worker 	case DLT_ATM_RFC1483:
3758*8b26181fSAndroid Build Coastguard Worker 		/*
3759*8b26181fSAndroid Build Coastguard Worker 		 * For LLC encapsulation, these are defined to have an
3760*8b26181fSAndroid Build Coastguard Worker 		 * 802.2 LLC header.
3761*8b26181fSAndroid Build Coastguard Worker 		 *
3762*8b26181fSAndroid Build Coastguard Worker 		 * For VC encapsulation, they don't, but there's no
3763*8b26181fSAndroid Build Coastguard Worker 		 * way to check for that; the protocol used on the VC
3764*8b26181fSAndroid Build Coastguard Worker 		 * is negotiated out of band.
3765*8b26181fSAndroid Build Coastguard Worker 		 */
3766*8b26181fSAndroid Build Coastguard Worker 		return gen_true(cstate);
3767*8b26181fSAndroid Build Coastguard Worker 
3768*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11:
3769*8b26181fSAndroid Build Coastguard Worker 	case DLT_PRISM_HEADER:
3770*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO:
3771*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO_AVS:
3772*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPI:
3773*8b26181fSAndroid Build Coastguard Worker 		/*
3774*8b26181fSAndroid Build Coastguard Worker 		 * Check that we have a data frame.
3775*8b26181fSAndroid Build Coastguard Worker 		 */
3776*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_check_802_11_data_frame(cstate);
3777*8b26181fSAndroid Build Coastguard Worker 		return b0;
3778*8b26181fSAndroid Build Coastguard Worker 
3779*8b26181fSAndroid Build Coastguard Worker 	default:
3780*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'llc' not supported for %s",
3781*8b26181fSAndroid Build Coastguard Worker 			  pcap_datalink_val_to_description_or_dlt(cstate->linktype));
3782*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
3783*8b26181fSAndroid Build Coastguard Worker 	}
3784*8b26181fSAndroid Build Coastguard Worker }
3785*8b26181fSAndroid Build Coastguard Worker 
3786*8b26181fSAndroid Build Coastguard Worker struct block *
gen_llc(compiler_state_t * cstate)3787*8b26181fSAndroid Build Coastguard Worker gen_llc(compiler_state_t *cstate)
3788*8b26181fSAndroid Build Coastguard Worker {
3789*8b26181fSAndroid Build Coastguard Worker 	/*
3790*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
3791*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
3792*8b26181fSAndroid Build Coastguard Worker 	 */
3793*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
3794*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
3795*8b26181fSAndroid Build Coastguard Worker 
3796*8b26181fSAndroid Build Coastguard Worker 	return gen_llc_internal(cstate);
3797*8b26181fSAndroid Build Coastguard Worker }
3798*8b26181fSAndroid Build Coastguard Worker 
3799*8b26181fSAndroid Build Coastguard Worker struct block *
gen_llc_i(compiler_state_t * cstate)3800*8b26181fSAndroid Build Coastguard Worker gen_llc_i(compiler_state_t *cstate)
3801*8b26181fSAndroid Build Coastguard Worker {
3802*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
3803*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
3804*8b26181fSAndroid Build Coastguard Worker 
3805*8b26181fSAndroid Build Coastguard Worker 	/*
3806*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
3807*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
3808*8b26181fSAndroid Build Coastguard Worker 	 */
3809*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
3810*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
3811*8b26181fSAndroid Build Coastguard Worker 
3812*8b26181fSAndroid Build Coastguard Worker 	/*
3813*8b26181fSAndroid Build Coastguard Worker 	 * Check whether this is an LLC frame.
3814*8b26181fSAndroid Build Coastguard Worker 	 */
3815*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_llc_internal(cstate);
3816*8b26181fSAndroid Build Coastguard Worker 
3817*8b26181fSAndroid Build Coastguard Worker 	/*
3818*8b26181fSAndroid Build Coastguard Worker 	 * Load the control byte and test the low-order bit; it must
3819*8b26181fSAndroid Build Coastguard Worker 	 * be clear for I frames.
3820*8b26181fSAndroid Build Coastguard Worker 	 */
3821*8b26181fSAndroid Build Coastguard Worker 	s = gen_load_a(cstate, OR_LLC, 2, BPF_B);
3822*8b26181fSAndroid Build Coastguard Worker 	b1 = new_block(cstate, JMP(BPF_JSET));
3823*8b26181fSAndroid Build Coastguard Worker 	b1->s.k = 0x01;
3824*8b26181fSAndroid Build Coastguard Worker 	b1->stmts = s;
3825*8b26181fSAndroid Build Coastguard Worker 	gen_not(b1);
3826*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
3827*8b26181fSAndroid Build Coastguard Worker 	return b1;
3828*8b26181fSAndroid Build Coastguard Worker }
3829*8b26181fSAndroid Build Coastguard Worker 
3830*8b26181fSAndroid Build Coastguard Worker struct block *
gen_llc_s(compiler_state_t * cstate)3831*8b26181fSAndroid Build Coastguard Worker gen_llc_s(compiler_state_t *cstate)
3832*8b26181fSAndroid Build Coastguard Worker {
3833*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
3834*8b26181fSAndroid Build Coastguard Worker 
3835*8b26181fSAndroid Build Coastguard Worker 	/*
3836*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
3837*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
3838*8b26181fSAndroid Build Coastguard Worker 	 */
3839*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
3840*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
3841*8b26181fSAndroid Build Coastguard Worker 
3842*8b26181fSAndroid Build Coastguard Worker 	/*
3843*8b26181fSAndroid Build Coastguard Worker 	 * Check whether this is an LLC frame.
3844*8b26181fSAndroid Build Coastguard Worker 	 */
3845*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_llc_internal(cstate);
3846*8b26181fSAndroid Build Coastguard Worker 
3847*8b26181fSAndroid Build Coastguard Worker 	/*
3848*8b26181fSAndroid Build Coastguard Worker 	 * Now compare the low-order 2 bit of the control byte against
3849*8b26181fSAndroid Build Coastguard Worker 	 * the appropriate value for S frames.
3850*8b26181fSAndroid Build Coastguard Worker 	 */
3851*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);
3852*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
3853*8b26181fSAndroid Build Coastguard Worker 	return b1;
3854*8b26181fSAndroid Build Coastguard Worker }
3855*8b26181fSAndroid Build Coastguard Worker 
3856*8b26181fSAndroid Build Coastguard Worker struct block *
gen_llc_u(compiler_state_t * cstate)3857*8b26181fSAndroid Build Coastguard Worker gen_llc_u(compiler_state_t *cstate)
3858*8b26181fSAndroid Build Coastguard Worker {
3859*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
3860*8b26181fSAndroid Build Coastguard Worker 
3861*8b26181fSAndroid Build Coastguard Worker 	/*
3862*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
3863*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
3864*8b26181fSAndroid Build Coastguard Worker 	 */
3865*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
3866*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
3867*8b26181fSAndroid Build Coastguard Worker 
3868*8b26181fSAndroid Build Coastguard Worker 	/*
3869*8b26181fSAndroid Build Coastguard Worker 	 * Check whether this is an LLC frame.
3870*8b26181fSAndroid Build Coastguard Worker 	 */
3871*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_llc_internal(cstate);
3872*8b26181fSAndroid Build Coastguard Worker 
3873*8b26181fSAndroid Build Coastguard Worker 	/*
3874*8b26181fSAndroid Build Coastguard Worker 	 * Now compare the low-order 2 bit of the control byte against
3875*8b26181fSAndroid Build Coastguard Worker 	 * the appropriate value for U frames.
3876*8b26181fSAndroid Build Coastguard Worker 	 */
3877*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);
3878*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
3879*8b26181fSAndroid Build Coastguard Worker 	return b1;
3880*8b26181fSAndroid Build Coastguard Worker }
3881*8b26181fSAndroid Build Coastguard Worker 
3882*8b26181fSAndroid Build Coastguard Worker struct block *
gen_llc_s_subtype(compiler_state_t * cstate,bpf_u_int32 subtype)3883*8b26181fSAndroid Build Coastguard Worker gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3884*8b26181fSAndroid Build Coastguard Worker {
3885*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
3886*8b26181fSAndroid Build Coastguard Worker 
3887*8b26181fSAndroid Build Coastguard Worker 	/*
3888*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
3889*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
3890*8b26181fSAndroid Build Coastguard Worker 	 */
3891*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
3892*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
3893*8b26181fSAndroid Build Coastguard Worker 
3894*8b26181fSAndroid Build Coastguard Worker 	/*
3895*8b26181fSAndroid Build Coastguard Worker 	 * Check whether this is an LLC frame.
3896*8b26181fSAndroid Build Coastguard Worker 	 */
3897*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_llc_internal(cstate);
3898*8b26181fSAndroid Build Coastguard Worker 
3899*8b26181fSAndroid Build Coastguard Worker 	/*
3900*8b26181fSAndroid Build Coastguard Worker 	 * Now check for an S frame with the appropriate type.
3901*8b26181fSAndroid Build Coastguard Worker 	 */
3902*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);
3903*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
3904*8b26181fSAndroid Build Coastguard Worker 	return b1;
3905*8b26181fSAndroid Build Coastguard Worker }
3906*8b26181fSAndroid Build Coastguard Worker 
3907*8b26181fSAndroid Build Coastguard Worker struct block *
gen_llc_u_subtype(compiler_state_t * cstate,bpf_u_int32 subtype)3908*8b26181fSAndroid Build Coastguard Worker gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3909*8b26181fSAndroid Build Coastguard Worker {
3910*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
3911*8b26181fSAndroid Build Coastguard Worker 
3912*8b26181fSAndroid Build Coastguard Worker 	/*
3913*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
3914*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
3915*8b26181fSAndroid Build Coastguard Worker 	 */
3916*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
3917*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
3918*8b26181fSAndroid Build Coastguard Worker 
3919*8b26181fSAndroid Build Coastguard Worker 	/*
3920*8b26181fSAndroid Build Coastguard Worker 	 * Check whether this is an LLC frame.
3921*8b26181fSAndroid Build Coastguard Worker 	 */
3922*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_llc_internal(cstate);
3923*8b26181fSAndroid Build Coastguard Worker 
3924*8b26181fSAndroid Build Coastguard Worker 	/*
3925*8b26181fSAndroid Build Coastguard Worker 	 * Now check for a U frame with the appropriate type.
3926*8b26181fSAndroid Build Coastguard Worker 	 */
3927*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);
3928*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
3929*8b26181fSAndroid Build Coastguard Worker 	return b1;
3930*8b26181fSAndroid Build Coastguard Worker }
3931*8b26181fSAndroid Build Coastguard Worker 
3932*8b26181fSAndroid Build Coastguard Worker /*
3933*8b26181fSAndroid Build Coastguard Worker  * Generate code to match a particular packet type, for link-layer types
3934*8b26181fSAndroid Build Coastguard Worker  * using 802.2 LLC headers.
3935*8b26181fSAndroid Build Coastguard Worker  *
3936*8b26181fSAndroid Build Coastguard Worker  * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3937*8b26181fSAndroid Build Coastguard Worker  * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3938*8b26181fSAndroid Build Coastguard Worker  *
3939*8b26181fSAndroid Build Coastguard Worker  * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3940*8b26181fSAndroid Build Coastguard Worker  * value, if <= ETHERMTU.  We use that to determine whether to
3941*8b26181fSAndroid Build Coastguard Worker  * match the DSAP or both DSAP and LSAP or to check the OUI and
3942*8b26181fSAndroid Build Coastguard Worker  * protocol ID in a SNAP header.
3943*8b26181fSAndroid Build Coastguard Worker  */
3944*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_llc_linktype(compiler_state_t * cstate,bpf_u_int32 ll_proto)3945*8b26181fSAndroid Build Coastguard Worker gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
3946*8b26181fSAndroid Build Coastguard Worker {
3947*8b26181fSAndroid Build Coastguard Worker 	/*
3948*8b26181fSAndroid Build Coastguard Worker 	 * XXX - handle token-ring variable-length header.
3949*8b26181fSAndroid Build Coastguard Worker 	 */
3950*8b26181fSAndroid Build Coastguard Worker 	switch (ll_proto) {
3951*8b26181fSAndroid Build Coastguard Worker 
3952*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_IP:
3953*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_ISONS:
3954*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_NETBEUI:
3955*8b26181fSAndroid Build Coastguard Worker 		/*
3956*8b26181fSAndroid Build Coastguard Worker 		 * XXX - should we check both the DSAP and the
3957*8b26181fSAndroid Build Coastguard Worker 		 * SSAP, like this, or should we check just the
3958*8b26181fSAndroid Build Coastguard Worker 		 * DSAP, as we do for other SAP values?
3959*8b26181fSAndroid Build Coastguard Worker 		 */
3960*8b26181fSAndroid Build Coastguard Worker 		return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32)
3961*8b26181fSAndroid Build Coastguard Worker 			     ((ll_proto << 8) | ll_proto));
3962*8b26181fSAndroid Build Coastguard Worker 
3963*8b26181fSAndroid Build Coastguard Worker 	case LLCSAP_IPX:
3964*8b26181fSAndroid Build Coastguard Worker 		/*
3965*8b26181fSAndroid Build Coastguard Worker 		 * XXX - are there ever SNAP frames for IPX on
3966*8b26181fSAndroid Build Coastguard Worker 		 * non-Ethernet 802.x networks?
3967*8b26181fSAndroid Build Coastguard Worker 		 */
3968*8b26181fSAndroid Build Coastguard Worker 		return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
3969*8b26181fSAndroid Build Coastguard Worker 
3970*8b26181fSAndroid Build Coastguard Worker 	case ETHERTYPE_ATALK:
3971*8b26181fSAndroid Build Coastguard Worker 		/*
3972*8b26181fSAndroid Build Coastguard Worker 		 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3973*8b26181fSAndroid Build Coastguard Worker 		 * SNAP packets with an organization code of
3974*8b26181fSAndroid Build Coastguard Worker 		 * 0x080007 (Apple, for Appletalk) and a protocol
3975*8b26181fSAndroid Build Coastguard Worker 		 * type of ETHERTYPE_ATALK (Appletalk).
3976*8b26181fSAndroid Build Coastguard Worker 		 *
3977*8b26181fSAndroid Build Coastguard Worker 		 * XXX - check for an organization code of
3978*8b26181fSAndroid Build Coastguard Worker 		 * encapsulated Ethernet as well?
3979*8b26181fSAndroid Build Coastguard Worker 		 */
3980*8b26181fSAndroid Build Coastguard Worker 		return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
3981*8b26181fSAndroid Build Coastguard Worker 
3982*8b26181fSAndroid Build Coastguard Worker 	default:
3983*8b26181fSAndroid Build Coastguard Worker 		/*
3984*8b26181fSAndroid Build Coastguard Worker 		 * XXX - we don't have to check for IPX 802.3
3985*8b26181fSAndroid Build Coastguard Worker 		 * here, but should we check for the IPX Ethertype?
3986*8b26181fSAndroid Build Coastguard Worker 		 */
3987*8b26181fSAndroid Build Coastguard Worker 		if (ll_proto <= ETHERMTU) {
3988*8b26181fSAndroid Build Coastguard Worker 			/*
3989*8b26181fSAndroid Build Coastguard Worker 			 * This is an LLC SAP value, so check
3990*8b26181fSAndroid Build Coastguard Worker 			 * the DSAP.
3991*8b26181fSAndroid Build Coastguard Worker 			 */
3992*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto);
3993*8b26181fSAndroid Build Coastguard Worker 		} else {
3994*8b26181fSAndroid Build Coastguard Worker 			/*
3995*8b26181fSAndroid Build Coastguard Worker 			 * This is an Ethernet type; we assume that it's
3996*8b26181fSAndroid Build Coastguard Worker 			 * unlikely that it'll appear in the right place
3997*8b26181fSAndroid Build Coastguard Worker 			 * at random, and therefore check only the
3998*8b26181fSAndroid Build Coastguard Worker 			 * location that would hold the Ethernet type
3999*8b26181fSAndroid Build Coastguard Worker 			 * in a SNAP frame with an organization code of
4000*8b26181fSAndroid Build Coastguard Worker 			 * 0x000000 (encapsulated Ethernet).
4001*8b26181fSAndroid Build Coastguard Worker 			 *
4002*8b26181fSAndroid Build Coastguard Worker 			 * XXX - if we were to check for the SNAP DSAP and
4003*8b26181fSAndroid Build Coastguard Worker 			 * LSAP, as per XXX, and were also to check for an
4004*8b26181fSAndroid Build Coastguard Worker 			 * organization code of 0x000000 (encapsulated
4005*8b26181fSAndroid Build Coastguard Worker 			 * Ethernet), we'd do
4006*8b26181fSAndroid Build Coastguard Worker 			 *
4007*8b26181fSAndroid Build Coastguard Worker 			 *	return gen_snap(cstate, 0x000000, ll_proto);
4008*8b26181fSAndroid Build Coastguard Worker 			 *
4009*8b26181fSAndroid Build Coastguard Worker 			 * here; for now, we don't, as per the above.
4010*8b26181fSAndroid Build Coastguard Worker 			 * I don't know whether it's worth the extra CPU
4011*8b26181fSAndroid Build Coastguard Worker 			 * time to do the right check or not.
4012*8b26181fSAndroid Build Coastguard Worker 			 */
4013*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto);
4014*8b26181fSAndroid Build Coastguard Worker 		}
4015*8b26181fSAndroid Build Coastguard Worker 	}
4016*8b26181fSAndroid Build Coastguard Worker }
4017*8b26181fSAndroid Build Coastguard Worker 
4018*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_hostop(compiler_state_t * cstate,bpf_u_int32 addr,bpf_u_int32 mask,int dir,bpf_u_int32 ll_proto,u_int src_off,u_int dst_off)4019*8b26181fSAndroid Build Coastguard Worker gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4020*8b26181fSAndroid Build Coastguard Worker     int dir, bpf_u_int32 ll_proto, u_int src_off, u_int dst_off)
4021*8b26181fSAndroid Build Coastguard Worker {
4022*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
4023*8b26181fSAndroid Build Coastguard Worker 	u_int offset;
4024*8b26181fSAndroid Build Coastguard Worker 
4025*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
4026*8b26181fSAndroid Build Coastguard Worker 
4027*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
4028*8b26181fSAndroid Build Coastguard Worker 		offset = src_off;
4029*8b26181fSAndroid Build Coastguard Worker 		break;
4030*8b26181fSAndroid Build Coastguard Worker 
4031*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
4032*8b26181fSAndroid Build Coastguard Worker 		offset = dst_off;
4033*8b26181fSAndroid Build Coastguard Worker 		break;
4034*8b26181fSAndroid Build Coastguard Worker 
4035*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
4036*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4037*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4038*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4039*8b26181fSAndroid Build Coastguard Worker 		return b1;
4040*8b26181fSAndroid Build Coastguard Worker 
4041*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4042*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
4043*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4044*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4045*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
4046*8b26181fSAndroid Build Coastguard Worker 		return b1;
4047*8b26181fSAndroid Build Coastguard Worker 
4048*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
4049*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4050*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4051*8b26181fSAndroid Build Coastguard Worker 
4052*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
4053*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4054*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4055*8b26181fSAndroid Build Coastguard Worker 
4056*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
4057*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4058*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4059*8b26181fSAndroid Build Coastguard Worker 
4060*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
4061*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4062*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4063*8b26181fSAndroid Build Coastguard Worker 
4064*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
4065*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4066*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4067*8b26181fSAndroid Build Coastguard Worker 
4068*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
4069*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4070*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4071*8b26181fSAndroid Build Coastguard Worker 
4072*8b26181fSAndroid Build Coastguard Worker 	default:
4073*8b26181fSAndroid Build Coastguard Worker 		abort();
4074*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4075*8b26181fSAndroid Build Coastguard Worker 	}
4076*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ll_proto);
4077*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask);
4078*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
4079*8b26181fSAndroid Build Coastguard Worker 	return b1;
4080*8b26181fSAndroid Build Coastguard Worker }
4081*8b26181fSAndroid Build Coastguard Worker 
4082*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
4083*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_hostop6(compiler_state_t * cstate,struct in6_addr * addr,struct in6_addr * mask,int dir,bpf_u_int32 ll_proto,u_int src_off,u_int dst_off)4084*8b26181fSAndroid Build Coastguard Worker gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
4085*8b26181fSAndroid Build Coastguard Worker     struct in6_addr *mask, int dir, bpf_u_int32 ll_proto, u_int src_off,
4086*8b26181fSAndroid Build Coastguard Worker     u_int dst_off)
4087*8b26181fSAndroid Build Coastguard Worker {
4088*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
4089*8b26181fSAndroid Build Coastguard Worker 	u_int offset;
4090*8b26181fSAndroid Build Coastguard Worker 	uint32_t *a, *m;
4091*8b26181fSAndroid Build Coastguard Worker 
4092*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
4093*8b26181fSAndroid Build Coastguard Worker 
4094*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
4095*8b26181fSAndroid Build Coastguard Worker 		offset = src_off;
4096*8b26181fSAndroid Build Coastguard Worker 		break;
4097*8b26181fSAndroid Build Coastguard Worker 
4098*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
4099*8b26181fSAndroid Build Coastguard Worker 		offset = dst_off;
4100*8b26181fSAndroid Build Coastguard Worker 		break;
4101*8b26181fSAndroid Build Coastguard Worker 
4102*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
4103*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4104*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4105*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4106*8b26181fSAndroid Build Coastguard Worker 		return b1;
4107*8b26181fSAndroid Build Coastguard Worker 
4108*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4109*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
4110*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4111*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4112*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
4113*8b26181fSAndroid Build Coastguard Worker 		return b1;
4114*8b26181fSAndroid Build Coastguard Worker 
4115*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
4116*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4117*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4118*8b26181fSAndroid Build Coastguard Worker 
4119*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
4120*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4121*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4122*8b26181fSAndroid Build Coastguard Worker 
4123*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
4124*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4125*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4126*8b26181fSAndroid Build Coastguard Worker 
4127*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
4128*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4129*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4130*8b26181fSAndroid Build Coastguard Worker 
4131*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
4132*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4133*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4134*8b26181fSAndroid Build Coastguard Worker 
4135*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
4136*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4137*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4138*8b26181fSAndroid Build Coastguard Worker 
4139*8b26181fSAndroid Build Coastguard Worker 	default:
4140*8b26181fSAndroid Build Coastguard Worker 		abort();
4141*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4142*8b26181fSAndroid Build Coastguard Worker 	}
4143*8b26181fSAndroid Build Coastguard Worker 	/* this order is important */
4144*8b26181fSAndroid Build Coastguard Worker 	a = (uint32_t *)addr;
4145*8b26181fSAndroid Build Coastguard Worker 	m = (uint32_t *)mask;
4146*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
4147*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
4148*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
4149*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
4150*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
4151*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
4152*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
4153*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ll_proto);
4154*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
4155*8b26181fSAndroid Build Coastguard Worker 	return b1;
4156*8b26181fSAndroid Build Coastguard Worker }
4157*8b26181fSAndroid Build Coastguard Worker #endif
4158*8b26181fSAndroid Build Coastguard Worker 
4159*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_ehostop(compiler_state_t * cstate,const u_char * eaddr,int dir)4160*8b26181fSAndroid Build Coastguard Worker gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4161*8b26181fSAndroid Build Coastguard Worker {
4162*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0, *b1;
4163*8b26181fSAndroid Build Coastguard Worker 
4164*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
4165*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
4166*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr);
4167*8b26181fSAndroid Build Coastguard Worker 
4168*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
4169*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr);
4170*8b26181fSAndroid Build Coastguard Worker 
4171*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
4172*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ehostop(cstate, eaddr, Q_SRC);
4173*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ehostop(cstate, eaddr, Q_DST);
4174*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4175*8b26181fSAndroid Build Coastguard Worker 		return b1;
4176*8b26181fSAndroid Build Coastguard Worker 
4177*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4178*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
4179*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ehostop(cstate, eaddr, Q_SRC);
4180*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ehostop(cstate, eaddr, Q_DST);
4181*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
4182*8b26181fSAndroid Build Coastguard Worker 		return b1;
4183*8b26181fSAndroid Build Coastguard Worker 
4184*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
4185*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4186*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4187*8b26181fSAndroid Build Coastguard Worker 
4188*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
4189*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4190*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4191*8b26181fSAndroid Build Coastguard Worker 
4192*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
4193*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4194*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4195*8b26181fSAndroid Build Coastguard Worker 
4196*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
4197*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4198*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4199*8b26181fSAndroid Build Coastguard Worker 
4200*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
4201*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers");
4202*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4203*8b26181fSAndroid Build Coastguard Worker 
4204*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
4205*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers");
4206*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4207*8b26181fSAndroid Build Coastguard Worker 	}
4208*8b26181fSAndroid Build Coastguard Worker 	abort();
4209*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
4210*8b26181fSAndroid Build Coastguard Worker }
4211*8b26181fSAndroid Build Coastguard Worker 
4212*8b26181fSAndroid Build Coastguard Worker /*
4213*8b26181fSAndroid Build Coastguard Worker  * Like gen_ehostop, but for DLT_FDDI
4214*8b26181fSAndroid Build Coastguard Worker  */
4215*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_fhostop(compiler_state_t * cstate,const u_char * eaddr,int dir)4216*8b26181fSAndroid Build Coastguard Worker gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4217*8b26181fSAndroid Build Coastguard Worker {
4218*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
4219*8b26181fSAndroid Build Coastguard Worker 
4220*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
4221*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
4222*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr);
4223*8b26181fSAndroid Build Coastguard Worker 
4224*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
4225*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr);
4226*8b26181fSAndroid Build Coastguard Worker 
4227*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
4228*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4229*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_fhostop(cstate, eaddr, Q_DST);
4230*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4231*8b26181fSAndroid Build Coastguard Worker 		return b1;
4232*8b26181fSAndroid Build Coastguard Worker 
4233*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4234*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
4235*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4236*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_fhostop(cstate, eaddr, Q_DST);
4237*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
4238*8b26181fSAndroid Build Coastguard Worker 		return b1;
4239*8b26181fSAndroid Build Coastguard Worker 
4240*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
4241*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4242*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4243*8b26181fSAndroid Build Coastguard Worker 
4244*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
4245*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4246*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4247*8b26181fSAndroid Build Coastguard Worker 
4248*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
4249*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4250*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4251*8b26181fSAndroid Build Coastguard Worker 
4252*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
4253*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4254*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4255*8b26181fSAndroid Build Coastguard Worker 
4256*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
4257*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is only supported on 802.11");
4258*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4259*8b26181fSAndroid Build Coastguard Worker 
4260*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
4261*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is only supported on 802.11");
4262*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4263*8b26181fSAndroid Build Coastguard Worker 	}
4264*8b26181fSAndroid Build Coastguard Worker 	abort();
4265*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
4266*8b26181fSAndroid Build Coastguard Worker }
4267*8b26181fSAndroid Build Coastguard Worker 
4268*8b26181fSAndroid Build Coastguard Worker /*
4269*8b26181fSAndroid Build Coastguard Worker  * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4270*8b26181fSAndroid Build Coastguard Worker  */
4271*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_thostop(compiler_state_t * cstate,const u_char * eaddr,int dir)4272*8b26181fSAndroid Build Coastguard Worker gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4273*8b26181fSAndroid Build Coastguard Worker {
4274*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0, *b1;
4275*8b26181fSAndroid Build Coastguard Worker 
4276*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
4277*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
4278*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr);
4279*8b26181fSAndroid Build Coastguard Worker 
4280*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
4281*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4282*8b26181fSAndroid Build Coastguard Worker 
4283*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
4284*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_thostop(cstate, eaddr, Q_SRC);
4285*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_thostop(cstate, eaddr, Q_DST);
4286*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4287*8b26181fSAndroid Build Coastguard Worker 		return b1;
4288*8b26181fSAndroid Build Coastguard Worker 
4289*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4290*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
4291*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_thostop(cstate, eaddr, Q_SRC);
4292*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_thostop(cstate, eaddr, Q_DST);
4293*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
4294*8b26181fSAndroid Build Coastguard Worker 		return b1;
4295*8b26181fSAndroid Build Coastguard Worker 
4296*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
4297*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4298*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4299*8b26181fSAndroid Build Coastguard Worker 
4300*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
4301*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4302*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4303*8b26181fSAndroid Build Coastguard Worker 
4304*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
4305*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4306*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4307*8b26181fSAndroid Build Coastguard Worker 
4308*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
4309*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4310*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4311*8b26181fSAndroid Build Coastguard Worker 
4312*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
4313*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is only supported on 802.11");
4314*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4315*8b26181fSAndroid Build Coastguard Worker 
4316*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
4317*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is only supported on 802.11");
4318*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4319*8b26181fSAndroid Build Coastguard Worker 	}
4320*8b26181fSAndroid Build Coastguard Worker 	abort();
4321*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
4322*8b26181fSAndroid Build Coastguard Worker }
4323*8b26181fSAndroid Build Coastguard Worker 
4324*8b26181fSAndroid Build Coastguard Worker /*
4325*8b26181fSAndroid Build Coastguard Worker  * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4326*8b26181fSAndroid Build Coastguard Worker  * various 802.11 + radio headers.
4327*8b26181fSAndroid Build Coastguard Worker  */
4328*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_wlanhostop(compiler_state_t * cstate,const u_char * eaddr,int dir)4329*8b26181fSAndroid Build Coastguard Worker gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4330*8b26181fSAndroid Build Coastguard Worker {
4331*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0, *b1, *b2;
4332*8b26181fSAndroid Build Coastguard Worker 	register struct slist *s;
4333*8b26181fSAndroid Build Coastguard Worker 
4334*8b26181fSAndroid Build Coastguard Worker #ifdef ENABLE_WLAN_FILTERING_PATCH
4335*8b26181fSAndroid Build Coastguard Worker 	/*
4336*8b26181fSAndroid Build Coastguard Worker 	 * TODO GV 20070613
4337*8b26181fSAndroid Build Coastguard Worker 	 * We need to disable the optimizer because the optimizer is buggy
4338*8b26181fSAndroid Build Coastguard Worker 	 * and wipes out some LD instructions generated by the below
4339*8b26181fSAndroid Build Coastguard Worker 	 * code to validate the Frame Control bits
4340*8b26181fSAndroid Build Coastguard Worker 	 */
4341*8b26181fSAndroid Build Coastguard Worker 	cstate->no_optimize = 1;
4342*8b26181fSAndroid Build Coastguard Worker #endif /* ENABLE_WLAN_FILTERING_PATCH */
4343*8b26181fSAndroid Build Coastguard Worker 
4344*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
4345*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
4346*8b26181fSAndroid Build Coastguard Worker 		/*
4347*8b26181fSAndroid Build Coastguard Worker 		 * Oh, yuk.
4348*8b26181fSAndroid Build Coastguard Worker 		 *
4349*8b26181fSAndroid Build Coastguard Worker 		 *	For control frames, there is no SA.
4350*8b26181fSAndroid Build Coastguard Worker 		 *
4351*8b26181fSAndroid Build Coastguard Worker 		 *	For management frames, SA is at an
4352*8b26181fSAndroid Build Coastguard Worker 		 *	offset of 10 from the beginning of
4353*8b26181fSAndroid Build Coastguard Worker 		 *	the packet.
4354*8b26181fSAndroid Build Coastguard Worker 		 *
4355*8b26181fSAndroid Build Coastguard Worker 		 *	For data frames, SA is at an offset
4356*8b26181fSAndroid Build Coastguard Worker 		 *	of 10 from the beginning of the packet
4357*8b26181fSAndroid Build Coastguard Worker 		 *	if From DS is clear, at an offset of
4358*8b26181fSAndroid Build Coastguard Worker 		 *	16 from the beginning of the packet
4359*8b26181fSAndroid Build Coastguard Worker 		 *	if From DS is set and To DS is clear,
4360*8b26181fSAndroid Build Coastguard Worker 		 *	and an offset of 24 from the beginning
4361*8b26181fSAndroid Build Coastguard Worker 		 *	of the packet if From DS is set and To DS
4362*8b26181fSAndroid Build Coastguard Worker 		 *	is set.
4363*8b26181fSAndroid Build Coastguard Worker 		 */
4364*8b26181fSAndroid Build Coastguard Worker 
4365*8b26181fSAndroid Build Coastguard Worker 		/*
4366*8b26181fSAndroid Build Coastguard Worker 		 * Generate the tests to be done for data frames
4367*8b26181fSAndroid Build Coastguard Worker 		 * with From DS set.
4368*8b26181fSAndroid Build Coastguard Worker 		 *
4369*8b26181fSAndroid Build Coastguard Worker 		 * First, check for To DS set, i.e. check "link[1] & 0x01".
4370*8b26181fSAndroid Build Coastguard Worker 		 */
4371*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4372*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4373*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x01;	/* To DS */
4374*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4375*8b26181fSAndroid Build Coastguard Worker 
4376*8b26181fSAndroid Build Coastguard Worker 		/*
4377*8b26181fSAndroid Build Coastguard Worker 		 * If To DS is set, the SA is at 24.
4378*8b26181fSAndroid Build Coastguard Worker 		 */
4379*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4380*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
4381*8b26181fSAndroid Build Coastguard Worker 
4382*8b26181fSAndroid Build Coastguard Worker 		/*
4383*8b26181fSAndroid Build Coastguard Worker 		 * Now, check for To DS not set, i.e. check
4384*8b26181fSAndroid Build Coastguard Worker 		 * "!(link[1] & 0x01)".
4385*8b26181fSAndroid Build Coastguard Worker 		 */
4386*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4387*8b26181fSAndroid Build Coastguard Worker 		b2 = new_block(cstate, JMP(BPF_JSET));
4388*8b26181fSAndroid Build Coastguard Worker 		b2->s.k = 0x01;	/* To DS */
4389*8b26181fSAndroid Build Coastguard Worker 		b2->stmts = s;
4390*8b26181fSAndroid Build Coastguard Worker 		gen_not(b2);
4391*8b26181fSAndroid Build Coastguard Worker 
4392*8b26181fSAndroid Build Coastguard Worker 		/*
4393*8b26181fSAndroid Build Coastguard Worker 		 * If To DS is not set, the SA is at 16.
4394*8b26181fSAndroid Build Coastguard Worker 		 */
4395*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4396*8b26181fSAndroid Build Coastguard Worker 		gen_and(b2, b1);
4397*8b26181fSAndroid Build Coastguard Worker 
4398*8b26181fSAndroid Build Coastguard Worker 		/*
4399*8b26181fSAndroid Build Coastguard Worker 		 * Now OR together the last two checks.  That gives
4400*8b26181fSAndroid Build Coastguard Worker 		 * the complete set of checks for data frames with
4401*8b26181fSAndroid Build Coastguard Worker 		 * From DS set.
4402*8b26181fSAndroid Build Coastguard Worker 		 */
4403*8b26181fSAndroid Build Coastguard Worker 		gen_or(b1, b0);
4404*8b26181fSAndroid Build Coastguard Worker 
4405*8b26181fSAndroid Build Coastguard Worker 		/*
4406*8b26181fSAndroid Build Coastguard Worker 		 * Now check for From DS being set, and AND that with
4407*8b26181fSAndroid Build Coastguard Worker 		 * the ORed-together checks.
4408*8b26181fSAndroid Build Coastguard Worker 		 */
4409*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4410*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4411*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x02;	/* From DS */
4412*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4413*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
4414*8b26181fSAndroid Build Coastguard Worker 
4415*8b26181fSAndroid Build Coastguard Worker 		/*
4416*8b26181fSAndroid Build Coastguard Worker 		 * Now check for data frames with From DS not set.
4417*8b26181fSAndroid Build Coastguard Worker 		 */
4418*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4419*8b26181fSAndroid Build Coastguard Worker 		b2 = new_block(cstate, JMP(BPF_JSET));
4420*8b26181fSAndroid Build Coastguard Worker 		b2->s.k = 0x02;	/* From DS */
4421*8b26181fSAndroid Build Coastguard Worker 		b2->stmts = s;
4422*8b26181fSAndroid Build Coastguard Worker 		gen_not(b2);
4423*8b26181fSAndroid Build Coastguard Worker 
4424*8b26181fSAndroid Build Coastguard Worker 		/*
4425*8b26181fSAndroid Build Coastguard Worker 		 * If From DS isn't set, the SA is at 10.
4426*8b26181fSAndroid Build Coastguard Worker 		 */
4427*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4428*8b26181fSAndroid Build Coastguard Worker 		gen_and(b2, b1);
4429*8b26181fSAndroid Build Coastguard Worker 
4430*8b26181fSAndroid Build Coastguard Worker 		/*
4431*8b26181fSAndroid Build Coastguard Worker 		 * Now OR together the checks for data frames with
4432*8b26181fSAndroid Build Coastguard Worker 		 * From DS not set and for data frames with From DS
4433*8b26181fSAndroid Build Coastguard Worker 		 * set; that gives the checks done for data frames.
4434*8b26181fSAndroid Build Coastguard Worker 		 */
4435*8b26181fSAndroid Build Coastguard Worker 		gen_or(b1, b0);
4436*8b26181fSAndroid Build Coastguard Worker 
4437*8b26181fSAndroid Build Coastguard Worker 		/*
4438*8b26181fSAndroid Build Coastguard Worker 		 * Now check for a data frame.
4439*8b26181fSAndroid Build Coastguard Worker 		 * I.e, check "link[0] & 0x08".
4440*8b26181fSAndroid Build Coastguard Worker 		 */
4441*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4442*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4443*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x08;
4444*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4445*8b26181fSAndroid Build Coastguard Worker 
4446*8b26181fSAndroid Build Coastguard Worker 		/*
4447*8b26181fSAndroid Build Coastguard Worker 		 * AND that with the checks done for data frames.
4448*8b26181fSAndroid Build Coastguard Worker 		 */
4449*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
4450*8b26181fSAndroid Build Coastguard Worker 
4451*8b26181fSAndroid Build Coastguard Worker 		/*
4452*8b26181fSAndroid Build Coastguard Worker 		 * If the high-order bit of the type value is 0, this
4453*8b26181fSAndroid Build Coastguard Worker 		 * is a management frame.
4454*8b26181fSAndroid Build Coastguard Worker 		 * I.e, check "!(link[0] & 0x08)".
4455*8b26181fSAndroid Build Coastguard Worker 		 */
4456*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4457*8b26181fSAndroid Build Coastguard Worker 		b2 = new_block(cstate, JMP(BPF_JSET));
4458*8b26181fSAndroid Build Coastguard Worker 		b2->s.k = 0x08;
4459*8b26181fSAndroid Build Coastguard Worker 		b2->stmts = s;
4460*8b26181fSAndroid Build Coastguard Worker 		gen_not(b2);
4461*8b26181fSAndroid Build Coastguard Worker 
4462*8b26181fSAndroid Build Coastguard Worker 		/*
4463*8b26181fSAndroid Build Coastguard Worker 		 * For management frames, the SA is at 10.
4464*8b26181fSAndroid Build Coastguard Worker 		 */
4465*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4466*8b26181fSAndroid Build Coastguard Worker 		gen_and(b2, b1);
4467*8b26181fSAndroid Build Coastguard Worker 
4468*8b26181fSAndroid Build Coastguard Worker 		/*
4469*8b26181fSAndroid Build Coastguard Worker 		 * OR that with the checks done for data frames.
4470*8b26181fSAndroid Build Coastguard Worker 		 * That gives the checks done for management and
4471*8b26181fSAndroid Build Coastguard Worker 		 * data frames.
4472*8b26181fSAndroid Build Coastguard Worker 		 */
4473*8b26181fSAndroid Build Coastguard Worker 		gen_or(b1, b0);
4474*8b26181fSAndroid Build Coastguard Worker 
4475*8b26181fSAndroid Build Coastguard Worker 		/*
4476*8b26181fSAndroid Build Coastguard Worker 		 * If the low-order bit of the type value is 1,
4477*8b26181fSAndroid Build Coastguard Worker 		 * this is either a control frame or a frame
4478*8b26181fSAndroid Build Coastguard Worker 		 * with a reserved type, and thus not a
4479*8b26181fSAndroid Build Coastguard Worker 		 * frame with an SA.
4480*8b26181fSAndroid Build Coastguard Worker 		 *
4481*8b26181fSAndroid Build Coastguard Worker 		 * I.e., check "!(link[0] & 0x04)".
4482*8b26181fSAndroid Build Coastguard Worker 		 */
4483*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4484*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4485*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x04;
4486*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4487*8b26181fSAndroid Build Coastguard Worker 		gen_not(b1);
4488*8b26181fSAndroid Build Coastguard Worker 
4489*8b26181fSAndroid Build Coastguard Worker 		/*
4490*8b26181fSAndroid Build Coastguard Worker 		 * AND that with the checks for data and management
4491*8b26181fSAndroid Build Coastguard Worker 		 * frames.
4492*8b26181fSAndroid Build Coastguard Worker 		 */
4493*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
4494*8b26181fSAndroid Build Coastguard Worker 		return b0;
4495*8b26181fSAndroid Build Coastguard Worker 
4496*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
4497*8b26181fSAndroid Build Coastguard Worker 		/*
4498*8b26181fSAndroid Build Coastguard Worker 		 * Oh, yuk.
4499*8b26181fSAndroid Build Coastguard Worker 		 *
4500*8b26181fSAndroid Build Coastguard Worker 		 *	For control frames, there is no DA.
4501*8b26181fSAndroid Build Coastguard Worker 		 *
4502*8b26181fSAndroid Build Coastguard Worker 		 *	For management frames, DA is at an
4503*8b26181fSAndroid Build Coastguard Worker 		 *	offset of 4 from the beginning of
4504*8b26181fSAndroid Build Coastguard Worker 		 *	the packet.
4505*8b26181fSAndroid Build Coastguard Worker 		 *
4506*8b26181fSAndroid Build Coastguard Worker 		 *	For data frames, DA is at an offset
4507*8b26181fSAndroid Build Coastguard Worker 		 *	of 4 from the beginning of the packet
4508*8b26181fSAndroid Build Coastguard Worker 		 *	if To DS is clear and at an offset of
4509*8b26181fSAndroid Build Coastguard Worker 		 *	16 from the beginning of the packet
4510*8b26181fSAndroid Build Coastguard Worker 		 *	if To DS is set.
4511*8b26181fSAndroid Build Coastguard Worker 		 */
4512*8b26181fSAndroid Build Coastguard Worker 
4513*8b26181fSAndroid Build Coastguard Worker 		/*
4514*8b26181fSAndroid Build Coastguard Worker 		 * Generate the tests to be done for data frames.
4515*8b26181fSAndroid Build Coastguard Worker 		 *
4516*8b26181fSAndroid Build Coastguard Worker 		 * First, check for To DS set, i.e. "link[1] & 0x01".
4517*8b26181fSAndroid Build Coastguard Worker 		 */
4518*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4519*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4520*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x01;	/* To DS */
4521*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4522*8b26181fSAndroid Build Coastguard Worker 
4523*8b26181fSAndroid Build Coastguard Worker 		/*
4524*8b26181fSAndroid Build Coastguard Worker 		 * If To DS is set, the DA is at 16.
4525*8b26181fSAndroid Build Coastguard Worker 		 */
4526*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4527*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
4528*8b26181fSAndroid Build Coastguard Worker 
4529*8b26181fSAndroid Build Coastguard Worker 		/*
4530*8b26181fSAndroid Build Coastguard Worker 		 * Now, check for To DS not set, i.e. check
4531*8b26181fSAndroid Build Coastguard Worker 		 * "!(link[1] & 0x01)".
4532*8b26181fSAndroid Build Coastguard Worker 		 */
4533*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4534*8b26181fSAndroid Build Coastguard Worker 		b2 = new_block(cstate, JMP(BPF_JSET));
4535*8b26181fSAndroid Build Coastguard Worker 		b2->s.k = 0x01;	/* To DS */
4536*8b26181fSAndroid Build Coastguard Worker 		b2->stmts = s;
4537*8b26181fSAndroid Build Coastguard Worker 		gen_not(b2);
4538*8b26181fSAndroid Build Coastguard Worker 
4539*8b26181fSAndroid Build Coastguard Worker 		/*
4540*8b26181fSAndroid Build Coastguard Worker 		 * If To DS is not set, the DA is at 4.
4541*8b26181fSAndroid Build Coastguard Worker 		 */
4542*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4543*8b26181fSAndroid Build Coastguard Worker 		gen_and(b2, b1);
4544*8b26181fSAndroid Build Coastguard Worker 
4545*8b26181fSAndroid Build Coastguard Worker 		/*
4546*8b26181fSAndroid Build Coastguard Worker 		 * Now OR together the last two checks.  That gives
4547*8b26181fSAndroid Build Coastguard Worker 		 * the complete set of checks for data frames.
4548*8b26181fSAndroid Build Coastguard Worker 		 */
4549*8b26181fSAndroid Build Coastguard Worker 		gen_or(b1, b0);
4550*8b26181fSAndroid Build Coastguard Worker 
4551*8b26181fSAndroid Build Coastguard Worker 		/*
4552*8b26181fSAndroid Build Coastguard Worker 		 * Now check for a data frame.
4553*8b26181fSAndroid Build Coastguard Worker 		 * I.e, check "link[0] & 0x08".
4554*8b26181fSAndroid Build Coastguard Worker 		 */
4555*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4556*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4557*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x08;
4558*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4559*8b26181fSAndroid Build Coastguard Worker 
4560*8b26181fSAndroid Build Coastguard Worker 		/*
4561*8b26181fSAndroid Build Coastguard Worker 		 * AND that with the checks done for data frames.
4562*8b26181fSAndroid Build Coastguard Worker 		 */
4563*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
4564*8b26181fSAndroid Build Coastguard Worker 
4565*8b26181fSAndroid Build Coastguard Worker 		/*
4566*8b26181fSAndroid Build Coastguard Worker 		 * If the high-order bit of the type value is 0, this
4567*8b26181fSAndroid Build Coastguard Worker 		 * is a management frame.
4568*8b26181fSAndroid Build Coastguard Worker 		 * I.e, check "!(link[0] & 0x08)".
4569*8b26181fSAndroid Build Coastguard Worker 		 */
4570*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4571*8b26181fSAndroid Build Coastguard Worker 		b2 = new_block(cstate, JMP(BPF_JSET));
4572*8b26181fSAndroid Build Coastguard Worker 		b2->s.k = 0x08;
4573*8b26181fSAndroid Build Coastguard Worker 		b2->stmts = s;
4574*8b26181fSAndroid Build Coastguard Worker 		gen_not(b2);
4575*8b26181fSAndroid Build Coastguard Worker 
4576*8b26181fSAndroid Build Coastguard Worker 		/*
4577*8b26181fSAndroid Build Coastguard Worker 		 * For management frames, the DA is at 4.
4578*8b26181fSAndroid Build Coastguard Worker 		 */
4579*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4580*8b26181fSAndroid Build Coastguard Worker 		gen_and(b2, b1);
4581*8b26181fSAndroid Build Coastguard Worker 
4582*8b26181fSAndroid Build Coastguard Worker 		/*
4583*8b26181fSAndroid Build Coastguard Worker 		 * OR that with the checks done for data frames.
4584*8b26181fSAndroid Build Coastguard Worker 		 * That gives the checks done for management and
4585*8b26181fSAndroid Build Coastguard Worker 		 * data frames.
4586*8b26181fSAndroid Build Coastguard Worker 		 */
4587*8b26181fSAndroid Build Coastguard Worker 		gen_or(b1, b0);
4588*8b26181fSAndroid Build Coastguard Worker 
4589*8b26181fSAndroid Build Coastguard Worker 		/*
4590*8b26181fSAndroid Build Coastguard Worker 		 * If the low-order bit of the type value is 1,
4591*8b26181fSAndroid Build Coastguard Worker 		 * this is either a control frame or a frame
4592*8b26181fSAndroid Build Coastguard Worker 		 * with a reserved type, and thus not a
4593*8b26181fSAndroid Build Coastguard Worker 		 * frame with an SA.
4594*8b26181fSAndroid Build Coastguard Worker 		 *
4595*8b26181fSAndroid Build Coastguard Worker 		 * I.e., check "!(link[0] & 0x04)".
4596*8b26181fSAndroid Build Coastguard Worker 		 */
4597*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4598*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4599*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x04;
4600*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4601*8b26181fSAndroid Build Coastguard Worker 		gen_not(b1);
4602*8b26181fSAndroid Build Coastguard Worker 
4603*8b26181fSAndroid Build Coastguard Worker 		/*
4604*8b26181fSAndroid Build Coastguard Worker 		 * AND that with the checks for data and management
4605*8b26181fSAndroid Build Coastguard Worker 		 * frames.
4606*8b26181fSAndroid Build Coastguard Worker 		 */
4607*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
4608*8b26181fSAndroid Build Coastguard Worker 		return b0;
4609*8b26181fSAndroid Build Coastguard Worker 
4610*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
4611*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4612*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4613*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4614*8b26181fSAndroid Build Coastguard Worker 		return b1;
4615*8b26181fSAndroid Build Coastguard Worker 
4616*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4617*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
4618*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4619*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4620*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
4621*8b26181fSAndroid Build Coastguard Worker 		return b1;
4622*8b26181fSAndroid Build Coastguard Worker 
4623*8b26181fSAndroid Build Coastguard Worker 	/*
4624*8b26181fSAndroid Build Coastguard Worker 	 * XXX - add BSSID keyword?
4625*8b26181fSAndroid Build Coastguard Worker 	 */
4626*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
4627*8b26181fSAndroid Build Coastguard Worker 		return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
4628*8b26181fSAndroid Build Coastguard Worker 
4629*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
4630*8b26181fSAndroid Build Coastguard Worker 		/*
4631*8b26181fSAndroid Build Coastguard Worker 		 * Not present in CTS or ACK control frames.
4632*8b26181fSAndroid Build Coastguard Worker 		 */
4633*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4634*8b26181fSAndroid Build Coastguard Worker 			IEEE80211_FC0_TYPE_MASK);
4635*8b26181fSAndroid Build Coastguard Worker 		gen_not(b0);
4636*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4637*8b26181fSAndroid Build Coastguard Worker 			IEEE80211_FC0_SUBTYPE_MASK);
4638*8b26181fSAndroid Build Coastguard Worker 		gen_not(b1);
4639*8b26181fSAndroid Build Coastguard Worker 		b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4640*8b26181fSAndroid Build Coastguard Worker 			IEEE80211_FC0_SUBTYPE_MASK);
4641*8b26181fSAndroid Build Coastguard Worker 		gen_not(b2);
4642*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b2);
4643*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b2);
4644*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4645*8b26181fSAndroid Build Coastguard Worker 		gen_and(b2, b1);
4646*8b26181fSAndroid Build Coastguard Worker 		return b1;
4647*8b26181fSAndroid Build Coastguard Worker 
4648*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
4649*8b26181fSAndroid Build Coastguard Worker 		/*
4650*8b26181fSAndroid Build Coastguard Worker 		 * Not present in control frames.
4651*8b26181fSAndroid Build Coastguard Worker 		 */
4652*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4653*8b26181fSAndroid Build Coastguard Worker 			IEEE80211_FC0_TYPE_MASK);
4654*8b26181fSAndroid Build Coastguard Worker 		gen_not(b0);
4655*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4656*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4657*8b26181fSAndroid Build Coastguard Worker 		return b1;
4658*8b26181fSAndroid Build Coastguard Worker 
4659*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
4660*8b26181fSAndroid Build Coastguard Worker 		/*
4661*8b26181fSAndroid Build Coastguard Worker 		 * Present only if the direction mask has both "From DS"
4662*8b26181fSAndroid Build Coastguard Worker 		 * and "To DS" set.  Neither control frames nor management
4663*8b26181fSAndroid Build Coastguard Worker 		 * frames should have both of those set, so we don't
4664*8b26181fSAndroid Build Coastguard Worker 		 * check the frame type.
4665*8b26181fSAndroid Build Coastguard Worker 		 */
4666*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
4667*8b26181fSAndroid Build Coastguard Worker 			IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4668*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4669*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4670*8b26181fSAndroid Build Coastguard Worker 		return b1;
4671*8b26181fSAndroid Build Coastguard Worker 
4672*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
4673*8b26181fSAndroid Build Coastguard Worker 		/*
4674*8b26181fSAndroid Build Coastguard Worker 		 * Not present in management frames; addr1 in other
4675*8b26181fSAndroid Build Coastguard Worker 		 * frames.
4676*8b26181fSAndroid Build Coastguard Worker 		 */
4677*8b26181fSAndroid Build Coastguard Worker 
4678*8b26181fSAndroid Build Coastguard Worker 		/*
4679*8b26181fSAndroid Build Coastguard Worker 		 * If the high-order bit of the type value is 0, this
4680*8b26181fSAndroid Build Coastguard Worker 		 * is a management frame.
4681*8b26181fSAndroid Build Coastguard Worker 		 * I.e, check "(link[0] & 0x08)".
4682*8b26181fSAndroid Build Coastguard Worker 		 */
4683*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4684*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4685*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x08;
4686*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4687*8b26181fSAndroid Build Coastguard Worker 
4688*8b26181fSAndroid Build Coastguard Worker 		/*
4689*8b26181fSAndroid Build Coastguard Worker 		 * Check addr1.
4690*8b26181fSAndroid Build Coastguard Worker 		 */
4691*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4692*8b26181fSAndroid Build Coastguard Worker 
4693*8b26181fSAndroid Build Coastguard Worker 		/*
4694*8b26181fSAndroid Build Coastguard Worker 		 * AND that with the check of addr1.
4695*8b26181fSAndroid Build Coastguard Worker 		 */
4696*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
4697*8b26181fSAndroid Build Coastguard Worker 		return (b0);
4698*8b26181fSAndroid Build Coastguard Worker 
4699*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
4700*8b26181fSAndroid Build Coastguard Worker 		/*
4701*8b26181fSAndroid Build Coastguard Worker 		 * Not present in management frames; addr2, if present,
4702*8b26181fSAndroid Build Coastguard Worker 		 * in other frames.
4703*8b26181fSAndroid Build Coastguard Worker 		 */
4704*8b26181fSAndroid Build Coastguard Worker 
4705*8b26181fSAndroid Build Coastguard Worker 		/*
4706*8b26181fSAndroid Build Coastguard Worker 		 * Not present in CTS or ACK control frames.
4707*8b26181fSAndroid Build Coastguard Worker 		 */
4708*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4709*8b26181fSAndroid Build Coastguard Worker 			IEEE80211_FC0_TYPE_MASK);
4710*8b26181fSAndroid Build Coastguard Worker 		gen_not(b0);
4711*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4712*8b26181fSAndroid Build Coastguard Worker 			IEEE80211_FC0_SUBTYPE_MASK);
4713*8b26181fSAndroid Build Coastguard Worker 		gen_not(b1);
4714*8b26181fSAndroid Build Coastguard Worker 		b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4715*8b26181fSAndroid Build Coastguard Worker 			IEEE80211_FC0_SUBTYPE_MASK);
4716*8b26181fSAndroid Build Coastguard Worker 		gen_not(b2);
4717*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b2);
4718*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b2);
4719*8b26181fSAndroid Build Coastguard Worker 
4720*8b26181fSAndroid Build Coastguard Worker 		/*
4721*8b26181fSAndroid Build Coastguard Worker 		 * If the high-order bit of the type value is 0, this
4722*8b26181fSAndroid Build Coastguard Worker 		 * is a management frame.
4723*8b26181fSAndroid Build Coastguard Worker 		 * I.e, check "(link[0] & 0x08)".
4724*8b26181fSAndroid Build Coastguard Worker 		 */
4725*8b26181fSAndroid Build Coastguard Worker 		s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4726*8b26181fSAndroid Build Coastguard Worker 		b1 = new_block(cstate, JMP(BPF_JSET));
4727*8b26181fSAndroid Build Coastguard Worker 		b1->s.k = 0x08;
4728*8b26181fSAndroid Build Coastguard Worker 		b1->stmts = s;
4729*8b26181fSAndroid Build Coastguard Worker 
4730*8b26181fSAndroid Build Coastguard Worker 		/*
4731*8b26181fSAndroid Build Coastguard Worker 		 * AND that with the check for frames other than
4732*8b26181fSAndroid Build Coastguard Worker 		 * CTS and ACK frames.
4733*8b26181fSAndroid Build Coastguard Worker 		 */
4734*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b2);
4735*8b26181fSAndroid Build Coastguard Worker 
4736*8b26181fSAndroid Build Coastguard Worker 		/*
4737*8b26181fSAndroid Build Coastguard Worker 		 * Check addr2.
4738*8b26181fSAndroid Build Coastguard Worker 		 */
4739*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4740*8b26181fSAndroid Build Coastguard Worker 		gen_and(b2, b1);
4741*8b26181fSAndroid Build Coastguard Worker 		return b1;
4742*8b26181fSAndroid Build Coastguard Worker 	}
4743*8b26181fSAndroid Build Coastguard Worker 	abort();
4744*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
4745*8b26181fSAndroid Build Coastguard Worker }
4746*8b26181fSAndroid Build Coastguard Worker 
4747*8b26181fSAndroid Build Coastguard Worker /*
4748*8b26181fSAndroid Build Coastguard Worker  * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4749*8b26181fSAndroid Build Coastguard Worker  * (We assume that the addresses are IEEE 48-bit MAC addresses,
4750*8b26181fSAndroid Build Coastguard Worker  * as the RFC states.)
4751*8b26181fSAndroid Build Coastguard Worker  */
4752*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_ipfchostop(compiler_state_t * cstate,const u_char * eaddr,int dir)4753*8b26181fSAndroid Build Coastguard Worker gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4754*8b26181fSAndroid Build Coastguard Worker {
4755*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0, *b1;
4756*8b26181fSAndroid Build Coastguard Worker 
4757*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
4758*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
4759*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4760*8b26181fSAndroid Build Coastguard Worker 
4761*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
4762*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4763*8b26181fSAndroid Build Coastguard Worker 
4764*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
4765*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4766*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4767*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4768*8b26181fSAndroid Build Coastguard Worker 		return b1;
4769*8b26181fSAndroid Build Coastguard Worker 
4770*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4771*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
4772*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4773*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4774*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
4775*8b26181fSAndroid Build Coastguard Worker 		return b1;
4776*8b26181fSAndroid Build Coastguard Worker 
4777*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
4778*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4779*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4780*8b26181fSAndroid Build Coastguard Worker 
4781*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
4782*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4783*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4784*8b26181fSAndroid Build Coastguard Worker 
4785*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
4786*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4787*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4788*8b26181fSAndroid Build Coastguard Worker 
4789*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
4790*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4791*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4792*8b26181fSAndroid Build Coastguard Worker 
4793*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
4794*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is only supported on 802.11");
4795*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4796*8b26181fSAndroid Build Coastguard Worker 
4797*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
4798*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is only supported on 802.11");
4799*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4800*8b26181fSAndroid Build Coastguard Worker 	}
4801*8b26181fSAndroid Build Coastguard Worker 	abort();
4802*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
4803*8b26181fSAndroid Build Coastguard Worker }
4804*8b26181fSAndroid Build Coastguard Worker 
4805*8b26181fSAndroid Build Coastguard Worker /*
4806*8b26181fSAndroid Build Coastguard Worker  * This is quite tricky because there may be pad bytes in front of the
4807*8b26181fSAndroid Build Coastguard Worker  * DECNET header, and then there are two possible data packet formats that
4808*8b26181fSAndroid Build Coastguard Worker  * carry both src and dst addresses, plus 5 packet types in a format that
4809*8b26181fSAndroid Build Coastguard Worker  * carries only the src node, plus 2 types that use a different format and
4810*8b26181fSAndroid Build Coastguard Worker  * also carry just the src node.
4811*8b26181fSAndroid Build Coastguard Worker  *
4812*8b26181fSAndroid Build Coastguard Worker  * Yuck.
4813*8b26181fSAndroid Build Coastguard Worker  *
4814*8b26181fSAndroid Build Coastguard Worker  * Instead of doing those all right, we just look for data packets with
4815*8b26181fSAndroid Build Coastguard Worker  * 0 or 1 bytes of padding.  If you want to look at other packets, that
4816*8b26181fSAndroid Build Coastguard Worker  * will require a lot more hacking.
4817*8b26181fSAndroid Build Coastguard Worker  *
4818*8b26181fSAndroid Build Coastguard Worker  * To add support for filtering on DECNET "areas" (network numbers)
4819*8b26181fSAndroid Build Coastguard Worker  * one would want to add a "mask" argument to this routine.  That would
4820*8b26181fSAndroid Build Coastguard Worker  * make the filter even more inefficient, although one could be clever
4821*8b26181fSAndroid Build Coastguard Worker  * and not generate masking instructions if the mask is 0xFFFF.
4822*8b26181fSAndroid Build Coastguard Worker  */
4823*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_dnhostop(compiler_state_t * cstate,bpf_u_int32 addr,int dir)4824*8b26181fSAndroid Build Coastguard Worker gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)
4825*8b26181fSAndroid Build Coastguard Worker {
4826*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *b2, *tmp;
4827*8b26181fSAndroid Build Coastguard Worker 	u_int offset_lh;	/* offset if long header is received */
4828*8b26181fSAndroid Build Coastguard Worker 	u_int offset_sh;	/* offset if short header is received */
4829*8b26181fSAndroid Build Coastguard Worker 
4830*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
4831*8b26181fSAndroid Build Coastguard Worker 
4832*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
4833*8b26181fSAndroid Build Coastguard Worker 		offset_sh = 1;	/* follows flags */
4834*8b26181fSAndroid Build Coastguard Worker 		offset_lh = 7;	/* flgs,darea,dsubarea,HIORD */
4835*8b26181fSAndroid Build Coastguard Worker 		break;
4836*8b26181fSAndroid Build Coastguard Worker 
4837*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
4838*8b26181fSAndroid Build Coastguard Worker 		offset_sh = 3;	/* follows flags, dstnode */
4839*8b26181fSAndroid Build Coastguard Worker 		offset_lh = 15;	/* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4840*8b26181fSAndroid Build Coastguard Worker 		break;
4841*8b26181fSAndroid Build Coastguard Worker 
4842*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
4843*8b26181fSAndroid Build Coastguard Worker 		/* Inefficient because we do our Calvinball dance twice */
4844*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_dnhostop(cstate, addr, Q_SRC);
4845*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_dnhostop(cstate, addr, Q_DST);
4846*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
4847*8b26181fSAndroid Build Coastguard Worker 		return b1;
4848*8b26181fSAndroid Build Coastguard Worker 
4849*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4850*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
4851*8b26181fSAndroid Build Coastguard Worker 		/* Inefficient because we do our Calvinball dance twice */
4852*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_dnhostop(cstate, addr, Q_SRC);
4853*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_dnhostop(cstate, addr, Q_DST);
4854*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
4855*8b26181fSAndroid Build Coastguard Worker 		return b1;
4856*8b26181fSAndroid Build Coastguard Worker 
4857*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
4858*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4859*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4860*8b26181fSAndroid Build Coastguard Worker 
4861*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
4862*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4863*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4864*8b26181fSAndroid Build Coastguard Worker 
4865*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
4866*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4867*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4868*8b26181fSAndroid Build Coastguard Worker 
4869*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
4870*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4871*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4872*8b26181fSAndroid Build Coastguard Worker 
4873*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
4874*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4875*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4876*8b26181fSAndroid Build Coastguard Worker 
4877*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
4878*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4879*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4880*8b26181fSAndroid Build Coastguard Worker 
4881*8b26181fSAndroid Build Coastguard Worker 	default:
4882*8b26181fSAndroid Build Coastguard Worker 		abort();
4883*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
4884*8b26181fSAndroid Build Coastguard Worker 	}
4885*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ETHERTYPE_DN);
4886*8b26181fSAndroid Build Coastguard Worker 	/* Check for pad = 1, long header case */
4887*8b26181fSAndroid Build Coastguard Worker 	tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4888*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)ntohs(0x0681), (bpf_u_int32)ntohs(0x07FF));
4889*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh,
4890*8b26181fSAndroid Build Coastguard Worker 	    BPF_H, (bpf_u_int32)ntohs((u_short)addr));
4891*8b26181fSAndroid Build Coastguard Worker 	gen_and(tmp, b1);
4892*8b26181fSAndroid Build Coastguard Worker 	/* Check for pad = 0, long header case */
4893*8b26181fSAndroid Build Coastguard Worker 	tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x06,
4894*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)0x7);
4895*8b26181fSAndroid Build Coastguard Worker 	b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H,
4896*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)ntohs((u_short)addr));
4897*8b26181fSAndroid Build Coastguard Worker 	gen_and(tmp, b2);
4898*8b26181fSAndroid Build Coastguard Worker 	gen_or(b2, b1);
4899*8b26181fSAndroid Build Coastguard Worker 	/* Check for pad = 1, short header case */
4900*8b26181fSAndroid Build Coastguard Worker 	tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4901*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)ntohs(0x0281), (bpf_u_int32)ntohs(0x07FF));
4902*8b26181fSAndroid Build Coastguard Worker 	b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H,
4903*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)ntohs((u_short)addr));
4904*8b26181fSAndroid Build Coastguard Worker 	gen_and(tmp, b2);
4905*8b26181fSAndroid Build Coastguard Worker 	gen_or(b2, b1);
4906*8b26181fSAndroid Build Coastguard Worker 	/* Check for pad = 0, short header case */
4907*8b26181fSAndroid Build Coastguard Worker 	tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x02,
4908*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)0x7);
4909*8b26181fSAndroid Build Coastguard Worker 	b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H,
4910*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)ntohs((u_short)addr));
4911*8b26181fSAndroid Build Coastguard Worker 	gen_and(tmp, b2);
4912*8b26181fSAndroid Build Coastguard Worker 	gen_or(b2, b1);
4913*8b26181fSAndroid Build Coastguard Worker 
4914*8b26181fSAndroid Build Coastguard Worker 	/* Combine with test for cstate->linktype */
4915*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
4916*8b26181fSAndroid Build Coastguard Worker 	return b1;
4917*8b26181fSAndroid Build Coastguard Worker }
4918*8b26181fSAndroid Build Coastguard Worker 
4919*8b26181fSAndroid Build Coastguard Worker /*
4920*8b26181fSAndroid Build Coastguard Worker  * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4921*8b26181fSAndroid Build Coastguard Worker  * test the bottom-of-stack bit, and then check the version number
4922*8b26181fSAndroid Build Coastguard Worker  * field in the IP header.
4923*8b26181fSAndroid Build Coastguard Worker  */
4924*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_mpls_linktype(compiler_state_t * cstate,bpf_u_int32 ll_proto)4925*8b26181fSAndroid Build Coastguard Worker gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
4926*8b26181fSAndroid Build Coastguard Worker {
4927*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
4928*8b26181fSAndroid Build Coastguard Worker 
4929*8b26181fSAndroid Build Coastguard Worker         switch (ll_proto) {
4930*8b26181fSAndroid Build Coastguard Worker 
4931*8b26181fSAndroid Build Coastguard Worker         case ETHERTYPE_IP:
4932*8b26181fSAndroid Build Coastguard Worker                 /* match the bottom-of-stack bit */
4933*8b26181fSAndroid Build Coastguard Worker                 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4934*8b26181fSAndroid Build Coastguard Worker                 /* match the IPv4 version number */
4935*8b26181fSAndroid Build Coastguard Worker                 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0);
4936*8b26181fSAndroid Build Coastguard Worker                 gen_and(b0, b1);
4937*8b26181fSAndroid Build Coastguard Worker                 return b1;
4938*8b26181fSAndroid Build Coastguard Worker 
4939*8b26181fSAndroid Build Coastguard Worker         case ETHERTYPE_IPV6:
4940*8b26181fSAndroid Build Coastguard Worker                 /* match the bottom-of-stack bit */
4941*8b26181fSAndroid Build Coastguard Worker                 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4942*8b26181fSAndroid Build Coastguard Worker                 /* match the IPv4 version number */
4943*8b26181fSAndroid Build Coastguard Worker                 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0);
4944*8b26181fSAndroid Build Coastguard Worker                 gen_and(b0, b1);
4945*8b26181fSAndroid Build Coastguard Worker                 return b1;
4946*8b26181fSAndroid Build Coastguard Worker 
4947*8b26181fSAndroid Build Coastguard Worker         default:
4948*8b26181fSAndroid Build Coastguard Worker                /* FIXME add other L3 proto IDs */
4949*8b26181fSAndroid Build Coastguard Worker                bpf_error(cstate, "unsupported protocol over mpls");
4950*8b26181fSAndroid Build Coastguard Worker                /*NOTREACHED*/
4951*8b26181fSAndroid Build Coastguard Worker         }
4952*8b26181fSAndroid Build Coastguard Worker }
4953*8b26181fSAndroid Build Coastguard Worker 
4954*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_host(compiler_state_t * cstate,bpf_u_int32 addr,bpf_u_int32 mask,int proto,int dir,int type)4955*8b26181fSAndroid Build Coastguard Worker gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4956*8b26181fSAndroid Build Coastguard Worker     int proto, int dir, int type)
4957*8b26181fSAndroid Build Coastguard Worker {
4958*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
4959*8b26181fSAndroid Build Coastguard Worker 	const char *typestr;
4960*8b26181fSAndroid Build Coastguard Worker 
4961*8b26181fSAndroid Build Coastguard Worker 	if (type == Q_NET)
4962*8b26181fSAndroid Build Coastguard Worker 		typestr = "net";
4963*8b26181fSAndroid Build Coastguard Worker 	else
4964*8b26181fSAndroid Build Coastguard Worker 		typestr = "host";
4965*8b26181fSAndroid Build Coastguard Worker 
4966*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
4967*8b26181fSAndroid Build Coastguard Worker 
4968*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
4969*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_host(cstate, addr, mask, Q_IP, dir, type);
4970*8b26181fSAndroid Build Coastguard Worker 		/*
4971*8b26181fSAndroid Build Coastguard Worker 		 * Only check for non-IPv4 addresses if we're not
4972*8b26181fSAndroid Build Coastguard Worker 		 * checking MPLS-encapsulated packets.
4973*8b26181fSAndroid Build Coastguard Worker 		 */
4974*8b26181fSAndroid Build Coastguard Worker 		if (cstate->label_stack_depth == 0) {
4975*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type);
4976*8b26181fSAndroid Build Coastguard Worker 			gen_or(b0, b1);
4977*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type);
4978*8b26181fSAndroid Build Coastguard Worker 			gen_or(b1, b0);
4979*8b26181fSAndroid Build Coastguard Worker 		}
4980*8b26181fSAndroid Build Coastguard Worker 		return b0;
4981*8b26181fSAndroid Build Coastguard Worker 
4982*8b26181fSAndroid Build Coastguard Worker 	case Q_LINK:
4983*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "link-layer modifier applied to %s", typestr);
4984*8b26181fSAndroid Build Coastguard Worker 
4985*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
4986*8b26181fSAndroid Build Coastguard Worker 		return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
4987*8b26181fSAndroid Build Coastguard Worker 
4988*8b26181fSAndroid Build Coastguard Worker 	case Q_RARP:
4989*8b26181fSAndroid Build Coastguard Worker 		return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4990*8b26181fSAndroid Build Coastguard Worker 
4991*8b26181fSAndroid Build Coastguard Worker 	case Q_ARP:
4992*8b26181fSAndroid Build Coastguard Worker 		return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4993*8b26181fSAndroid Build Coastguard Worker 
4994*8b26181fSAndroid Build Coastguard Worker 	case Q_SCTP:
4995*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
4996*8b26181fSAndroid Build Coastguard Worker 
4997*8b26181fSAndroid Build Coastguard Worker 	case Q_TCP:
4998*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
4999*8b26181fSAndroid Build Coastguard Worker 
5000*8b26181fSAndroid Build Coastguard Worker 	case Q_UDP:
5001*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'udp' modifier applied to %s", typestr);
5002*8b26181fSAndroid Build Coastguard Worker 
5003*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMP:
5004*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
5005*8b26181fSAndroid Build Coastguard Worker 
5006*8b26181fSAndroid Build Coastguard Worker 	case Q_IGMP:
5007*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
5008*8b26181fSAndroid Build Coastguard Worker 
5009*8b26181fSAndroid Build Coastguard Worker 	case Q_IGRP:
5010*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
5011*8b26181fSAndroid Build Coastguard Worker 
5012*8b26181fSAndroid Build Coastguard Worker 	case Q_ATALK:
5013*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "AppleTalk host filtering not implemented");
5014*8b26181fSAndroid Build Coastguard Worker 
5015*8b26181fSAndroid Build Coastguard Worker 	case Q_DECNET:
5016*8b26181fSAndroid Build Coastguard Worker 		return gen_dnhostop(cstate, addr, dir);
5017*8b26181fSAndroid Build Coastguard Worker 
5018*8b26181fSAndroid Build Coastguard Worker 	case Q_LAT:
5019*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "LAT host filtering not implemented");
5020*8b26181fSAndroid Build Coastguard Worker 
5021*8b26181fSAndroid Build Coastguard Worker 	case Q_SCA:
5022*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "SCA host filtering not implemented");
5023*8b26181fSAndroid Build Coastguard Worker 
5024*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPRC:
5025*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "MOPRC host filtering not implemented");
5026*8b26181fSAndroid Build Coastguard Worker 
5027*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPDL:
5028*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "MOPDL host filtering not implemented");
5029*8b26181fSAndroid Build Coastguard Worker 
5030*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
5031*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ip6' modifier applied to ip host");
5032*8b26181fSAndroid Build Coastguard Worker 
5033*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMPV6:
5034*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
5035*8b26181fSAndroid Build Coastguard Worker 
5036*8b26181fSAndroid Build Coastguard Worker 	case Q_AH:
5037*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ah' modifier applied to %s", typestr);
5038*8b26181fSAndroid Build Coastguard Worker 
5039*8b26181fSAndroid Build Coastguard Worker 	case Q_ESP:
5040*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'esp' modifier applied to %s", typestr);
5041*8b26181fSAndroid Build Coastguard Worker 
5042*8b26181fSAndroid Build Coastguard Worker 	case Q_PIM:
5043*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'pim' modifier applied to %s", typestr);
5044*8b26181fSAndroid Build Coastguard Worker 
5045*8b26181fSAndroid Build Coastguard Worker 	case Q_VRRP:
5046*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
5047*8b26181fSAndroid Build Coastguard Worker 
5048*8b26181fSAndroid Build Coastguard Worker 	case Q_AARP:
5049*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "AARP host filtering not implemented");
5050*8b26181fSAndroid Build Coastguard Worker 
5051*8b26181fSAndroid Build Coastguard Worker 	case Q_ISO:
5052*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "ISO host filtering not implemented");
5053*8b26181fSAndroid Build Coastguard Worker 
5054*8b26181fSAndroid Build Coastguard Worker 	case Q_ESIS:
5055*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'esis' modifier applied to %s", typestr);
5056*8b26181fSAndroid Build Coastguard Worker 
5057*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS:
5058*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'isis' modifier applied to %s", typestr);
5059*8b26181fSAndroid Build Coastguard Worker 
5060*8b26181fSAndroid Build Coastguard Worker 	case Q_CLNP:
5061*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
5062*8b26181fSAndroid Build Coastguard Worker 
5063*8b26181fSAndroid Build Coastguard Worker 	case Q_STP:
5064*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'stp' modifier applied to %s", typestr);
5065*8b26181fSAndroid Build Coastguard Worker 
5066*8b26181fSAndroid Build Coastguard Worker 	case Q_IPX:
5067*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "IPX host filtering not implemented");
5068*8b26181fSAndroid Build Coastguard Worker 
5069*8b26181fSAndroid Build Coastguard Worker 	case Q_NETBEUI:
5070*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
5071*8b26181fSAndroid Build Coastguard Worker 
5072*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_L1:
5073*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'l1' modifier applied to %s", typestr);
5074*8b26181fSAndroid Build Coastguard Worker 
5075*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_L2:
5076*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'l2' modifier applied to %s", typestr);
5077*8b26181fSAndroid Build Coastguard Worker 
5078*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_IIH:
5079*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'iih' modifier applied to %s", typestr);
5080*8b26181fSAndroid Build Coastguard Worker 
5081*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_SNP:
5082*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'snp' modifier applied to %s", typestr);
5083*8b26181fSAndroid Build Coastguard Worker 
5084*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_CSNP:
5085*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'csnp' modifier applied to %s", typestr);
5086*8b26181fSAndroid Build Coastguard Worker 
5087*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_PSNP:
5088*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'psnp' modifier applied to %s", typestr);
5089*8b26181fSAndroid Build Coastguard Worker 
5090*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_LSP:
5091*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'lsp' modifier applied to %s", typestr);
5092*8b26181fSAndroid Build Coastguard Worker 
5093*8b26181fSAndroid Build Coastguard Worker 	case Q_RADIO:
5094*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'radio' modifier applied to %s", typestr);
5095*8b26181fSAndroid Build Coastguard Worker 
5096*8b26181fSAndroid Build Coastguard Worker 	case Q_CARP:
5097*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'carp' modifier applied to %s", typestr);
5098*8b26181fSAndroid Build Coastguard Worker 
5099*8b26181fSAndroid Build Coastguard Worker 	default:
5100*8b26181fSAndroid Build Coastguard Worker 		abort();
5101*8b26181fSAndroid Build Coastguard Worker 	}
5102*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
5103*8b26181fSAndroid Build Coastguard Worker }
5104*8b26181fSAndroid Build Coastguard Worker 
5105*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
5106*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_host6(compiler_state_t * cstate,struct in6_addr * addr,struct in6_addr * mask,int proto,int dir,int type)5107*8b26181fSAndroid Build Coastguard Worker gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
5108*8b26181fSAndroid Build Coastguard Worker     struct in6_addr *mask, int proto, int dir, int type)
5109*8b26181fSAndroid Build Coastguard Worker {
5110*8b26181fSAndroid Build Coastguard Worker 	const char *typestr;
5111*8b26181fSAndroid Build Coastguard Worker 
5112*8b26181fSAndroid Build Coastguard Worker 	if (type == Q_NET)
5113*8b26181fSAndroid Build Coastguard Worker 		typestr = "net";
5114*8b26181fSAndroid Build Coastguard Worker 	else
5115*8b26181fSAndroid Build Coastguard Worker 		typestr = "host";
5116*8b26181fSAndroid Build Coastguard Worker 
5117*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
5118*8b26181fSAndroid Build Coastguard Worker 
5119*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
5120*8b26181fSAndroid Build Coastguard Worker 		return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);
5121*8b26181fSAndroid Build Coastguard Worker 
5122*8b26181fSAndroid Build Coastguard Worker 	case Q_LINK:
5123*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);
5124*8b26181fSAndroid Build Coastguard Worker 
5125*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
5126*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr);
5127*8b26181fSAndroid Build Coastguard Worker 
5128*8b26181fSAndroid Build Coastguard Worker 	case Q_RARP:
5129*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr);
5130*8b26181fSAndroid Build Coastguard Worker 
5131*8b26181fSAndroid Build Coastguard Worker 	case Q_ARP:
5132*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);
5133*8b26181fSAndroid Build Coastguard Worker 
5134*8b26181fSAndroid Build Coastguard Worker 	case Q_SCTP:
5135*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr);
5136*8b26181fSAndroid Build Coastguard Worker 
5137*8b26181fSAndroid Build Coastguard Worker 	case Q_TCP:
5138*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr);
5139*8b26181fSAndroid Build Coastguard Worker 
5140*8b26181fSAndroid Build Coastguard Worker 	case Q_UDP:
5141*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr);
5142*8b26181fSAndroid Build Coastguard Worker 
5143*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMP:
5144*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr);
5145*8b26181fSAndroid Build Coastguard Worker 
5146*8b26181fSAndroid Build Coastguard Worker 	case Q_IGMP:
5147*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr);
5148*8b26181fSAndroid Build Coastguard Worker 
5149*8b26181fSAndroid Build Coastguard Worker 	case Q_IGRP:
5150*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr);
5151*8b26181fSAndroid Build Coastguard Worker 
5152*8b26181fSAndroid Build Coastguard Worker 	case Q_ATALK:
5153*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr);
5154*8b26181fSAndroid Build Coastguard Worker 
5155*8b26181fSAndroid Build Coastguard Worker 	case Q_DECNET:
5156*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);
5157*8b26181fSAndroid Build Coastguard Worker 
5158*8b26181fSAndroid Build Coastguard Worker 	case Q_LAT:
5159*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr);
5160*8b26181fSAndroid Build Coastguard Worker 
5161*8b26181fSAndroid Build Coastguard Worker 	case Q_SCA:
5162*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr);
5163*8b26181fSAndroid Build Coastguard Worker 
5164*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPRC:
5165*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr);
5166*8b26181fSAndroid Build Coastguard Worker 
5167*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPDL:
5168*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr);
5169*8b26181fSAndroid Build Coastguard Worker 
5170*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
5171*8b26181fSAndroid Build Coastguard Worker 		return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
5172*8b26181fSAndroid Build Coastguard Worker 
5173*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMPV6:
5174*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr);
5175*8b26181fSAndroid Build Coastguard Worker 
5176*8b26181fSAndroid Build Coastguard Worker 	case Q_AH:
5177*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr);
5178*8b26181fSAndroid Build Coastguard Worker 
5179*8b26181fSAndroid Build Coastguard Worker 	case Q_ESP:
5180*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr);
5181*8b26181fSAndroid Build Coastguard Worker 
5182*8b26181fSAndroid Build Coastguard Worker 	case Q_PIM:
5183*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr);
5184*8b26181fSAndroid Build Coastguard Worker 
5185*8b26181fSAndroid Build Coastguard Worker 	case Q_VRRP:
5186*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr);
5187*8b26181fSAndroid Build Coastguard Worker 
5188*8b26181fSAndroid Build Coastguard Worker 	case Q_AARP:
5189*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr);
5190*8b26181fSAndroid Build Coastguard Worker 
5191*8b26181fSAndroid Build Coastguard Worker 	case Q_ISO:
5192*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr);
5193*8b26181fSAndroid Build Coastguard Worker 
5194*8b26181fSAndroid Build Coastguard Worker 	case Q_ESIS:
5195*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr);
5196*8b26181fSAndroid Build Coastguard Worker 
5197*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS:
5198*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr);
5199*8b26181fSAndroid Build Coastguard Worker 
5200*8b26181fSAndroid Build Coastguard Worker 	case Q_CLNP:
5201*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr);
5202*8b26181fSAndroid Build Coastguard Worker 
5203*8b26181fSAndroid Build Coastguard Worker 	case Q_STP:
5204*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr);
5205*8b26181fSAndroid Build Coastguard Worker 
5206*8b26181fSAndroid Build Coastguard Worker 	case Q_IPX:
5207*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr);
5208*8b26181fSAndroid Build Coastguard Worker 
5209*8b26181fSAndroid Build Coastguard Worker 	case Q_NETBEUI:
5210*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr);
5211*8b26181fSAndroid Build Coastguard Worker 
5212*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_L1:
5213*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr);
5214*8b26181fSAndroid Build Coastguard Worker 
5215*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_L2:
5216*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr);
5217*8b26181fSAndroid Build Coastguard Worker 
5218*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_IIH:
5219*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr);
5220*8b26181fSAndroid Build Coastguard Worker 
5221*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_SNP:
5222*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr);
5223*8b26181fSAndroid Build Coastguard Worker 
5224*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_CSNP:
5225*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr);
5226*8b26181fSAndroid Build Coastguard Worker 
5227*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_PSNP:
5228*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr);
5229*8b26181fSAndroid Build Coastguard Worker 
5230*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_LSP:
5231*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr);
5232*8b26181fSAndroid Build Coastguard Worker 
5233*8b26181fSAndroid Build Coastguard Worker 	case Q_RADIO:
5234*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr);
5235*8b26181fSAndroid Build Coastguard Worker 
5236*8b26181fSAndroid Build Coastguard Worker 	case Q_CARP:
5237*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr);
5238*8b26181fSAndroid Build Coastguard Worker 
5239*8b26181fSAndroid Build Coastguard Worker 	default:
5240*8b26181fSAndroid Build Coastguard Worker 		abort();
5241*8b26181fSAndroid Build Coastguard Worker 	}
5242*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
5243*8b26181fSAndroid Build Coastguard Worker }
5244*8b26181fSAndroid Build Coastguard Worker #endif
5245*8b26181fSAndroid Build Coastguard Worker 
5246*8b26181fSAndroid Build Coastguard Worker #ifndef INET6
5247*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_gateway(compiler_state_t * cstate,const u_char * eaddr,struct addrinfo * alist,int proto,int dir)5248*8b26181fSAndroid Build Coastguard Worker gen_gateway(compiler_state_t *cstate, const u_char *eaddr,
5249*8b26181fSAndroid Build Coastguard Worker     struct addrinfo *alist, int proto, int dir)
5250*8b26181fSAndroid Build Coastguard Worker {
5251*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
5252*8b26181fSAndroid Build Coastguard Worker 	struct addrinfo *ai;
5253*8b26181fSAndroid Build Coastguard Worker 	struct sockaddr_in *sin;
5254*8b26181fSAndroid Build Coastguard Worker 
5255*8b26181fSAndroid Build Coastguard Worker 	if (dir != 0)
5256*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "direction applied to 'gateway'");
5257*8b26181fSAndroid Build Coastguard Worker 
5258*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
5259*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
5260*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
5261*8b26181fSAndroid Build Coastguard Worker 	case Q_ARP:
5262*8b26181fSAndroid Build Coastguard Worker 	case Q_RARP:
5263*8b26181fSAndroid Build Coastguard Worker 		switch (cstate->linktype) {
5264*8b26181fSAndroid Build Coastguard Worker 		case DLT_EN10MB:
5265*8b26181fSAndroid Build Coastguard Worker 		case DLT_NETANALYZER:
5266*8b26181fSAndroid Build Coastguard Worker 		case DLT_NETANALYZER_TRANSPARENT:
5267*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_prevlinkhdr_check(cstate);
5268*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_ehostop(cstate, eaddr, Q_OR);
5269*8b26181fSAndroid Build Coastguard Worker 			if (b1 != NULL)
5270*8b26181fSAndroid Build Coastguard Worker 				gen_and(b1, b0);
5271*8b26181fSAndroid Build Coastguard Worker 			break;
5272*8b26181fSAndroid Build Coastguard Worker 		case DLT_FDDI:
5273*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_fhostop(cstate, eaddr, Q_OR);
5274*8b26181fSAndroid Build Coastguard Worker 			break;
5275*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802:
5276*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_thostop(cstate, eaddr, Q_OR);
5277*8b26181fSAndroid Build Coastguard Worker 			break;
5278*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11:
5279*8b26181fSAndroid Build Coastguard Worker 		case DLT_PRISM_HEADER:
5280*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11_RADIO_AVS:
5281*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11_RADIO:
5282*8b26181fSAndroid Build Coastguard Worker 		case DLT_PPI:
5283*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_wlanhostop(cstate, eaddr, Q_OR);
5284*8b26181fSAndroid Build Coastguard Worker 			break;
5285*8b26181fSAndroid Build Coastguard Worker 		case DLT_SUNATM:
5286*8b26181fSAndroid Build Coastguard Worker 			/*
5287*8b26181fSAndroid Build Coastguard Worker 			 * This is LLC-multiplexed traffic; if it were
5288*8b26181fSAndroid Build Coastguard Worker 			 * LANE, cstate->linktype would have been set to
5289*8b26181fSAndroid Build Coastguard Worker 			 * DLT_EN10MB.
5290*8b26181fSAndroid Build Coastguard Worker 			 */
5291*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate,
5292*8b26181fSAndroid Build Coastguard Worker 			    "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5293*8b26181fSAndroid Build Coastguard Worker 			break;
5294*8b26181fSAndroid Build Coastguard Worker 		case DLT_IP_OVER_FC:
5295*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_ipfchostop(cstate, eaddr, Q_OR);
5296*8b26181fSAndroid Build Coastguard Worker 			break;
5297*8b26181fSAndroid Build Coastguard Worker 		default:
5298*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate,
5299*8b26181fSAndroid Build Coastguard Worker 			    "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5300*8b26181fSAndroid Build Coastguard Worker 		}
5301*8b26181fSAndroid Build Coastguard Worker 		b1 = NULL;
5302*8b26181fSAndroid Build Coastguard Worker 		for (ai = alist; ai != NULL; ai = ai->ai_next) {
5303*8b26181fSAndroid Build Coastguard Worker 			/*
5304*8b26181fSAndroid Build Coastguard Worker 			 * Does it have an address?
5305*8b26181fSAndroid Build Coastguard Worker 			 */
5306*8b26181fSAndroid Build Coastguard Worker 			if (ai->ai_addr != NULL) {
5307*8b26181fSAndroid Build Coastguard Worker 				/*
5308*8b26181fSAndroid Build Coastguard Worker 				 * Yes.  Is it an IPv4 address?
5309*8b26181fSAndroid Build Coastguard Worker 				 */
5310*8b26181fSAndroid Build Coastguard Worker 				if (ai->ai_addr->sa_family == AF_INET) {
5311*8b26181fSAndroid Build Coastguard Worker 					/*
5312*8b26181fSAndroid Build Coastguard Worker 					 * Generate an entry for it.
5313*8b26181fSAndroid Build Coastguard Worker 					 */
5314*8b26181fSAndroid Build Coastguard Worker 					sin = (struct sockaddr_in *)ai->ai_addr;
5315*8b26181fSAndroid Build Coastguard Worker 					tmp = gen_host(cstate,
5316*8b26181fSAndroid Build Coastguard Worker 					    ntohl(sin->sin_addr.s_addr),
5317*8b26181fSAndroid Build Coastguard Worker 					    0xffffffff, proto, Q_OR, Q_HOST);
5318*8b26181fSAndroid Build Coastguard Worker 					/*
5319*8b26181fSAndroid Build Coastguard Worker 					 * Is it the *first* IPv4 address?
5320*8b26181fSAndroid Build Coastguard Worker 					 */
5321*8b26181fSAndroid Build Coastguard Worker 					if (b1 == NULL) {
5322*8b26181fSAndroid Build Coastguard Worker 						/*
5323*8b26181fSAndroid Build Coastguard Worker 						 * Yes, so start with it.
5324*8b26181fSAndroid Build Coastguard Worker 						 */
5325*8b26181fSAndroid Build Coastguard Worker 						b1 = tmp;
5326*8b26181fSAndroid Build Coastguard Worker 					} else {
5327*8b26181fSAndroid Build Coastguard Worker 						/*
5328*8b26181fSAndroid Build Coastguard Worker 						 * No, so OR it into the
5329*8b26181fSAndroid Build Coastguard Worker 						 * existing set of
5330*8b26181fSAndroid Build Coastguard Worker 						 * addresses.
5331*8b26181fSAndroid Build Coastguard Worker 						 */
5332*8b26181fSAndroid Build Coastguard Worker 						gen_or(b1, tmp);
5333*8b26181fSAndroid Build Coastguard Worker 						b1 = tmp;
5334*8b26181fSAndroid Build Coastguard Worker 					}
5335*8b26181fSAndroid Build Coastguard Worker 				}
5336*8b26181fSAndroid Build Coastguard Worker 			}
5337*8b26181fSAndroid Build Coastguard Worker 		}
5338*8b26181fSAndroid Build Coastguard Worker 		if (b1 == NULL) {
5339*8b26181fSAndroid Build Coastguard Worker 			/*
5340*8b26181fSAndroid Build Coastguard Worker 			 * No IPv4 addresses found.
5341*8b26181fSAndroid Build Coastguard Worker 			 */
5342*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
5343*8b26181fSAndroid Build Coastguard Worker 		}
5344*8b26181fSAndroid Build Coastguard Worker 		gen_not(b1);
5345*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
5346*8b26181fSAndroid Build Coastguard Worker 		return b1;
5347*8b26181fSAndroid Build Coastguard Worker 	}
5348*8b26181fSAndroid Build Coastguard Worker 	bpf_error(cstate, "illegal modifier of 'gateway'");
5349*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
5350*8b26181fSAndroid Build Coastguard Worker }
5351*8b26181fSAndroid Build Coastguard Worker #endif
5352*8b26181fSAndroid Build Coastguard Worker 
5353*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_proto_abbrev_internal(compiler_state_t * cstate,int proto)5354*8b26181fSAndroid Build Coastguard Worker gen_proto_abbrev_internal(compiler_state_t *cstate, int proto)
5355*8b26181fSAndroid Build Coastguard Worker {
5356*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
5357*8b26181fSAndroid Build Coastguard Worker 	struct block *b1;
5358*8b26181fSAndroid Build Coastguard Worker 
5359*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
5360*8b26181fSAndroid Build Coastguard Worker 
5361*8b26181fSAndroid Build Coastguard Worker 	case Q_SCTP:
5362*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_SCTP, Q_DEFAULT, Q_DEFAULT);
5363*8b26181fSAndroid Build Coastguard Worker 		break;
5364*8b26181fSAndroid Build Coastguard Worker 
5365*8b26181fSAndroid Build Coastguard Worker 	case Q_TCP:
5366*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_TCP, Q_DEFAULT, Q_DEFAULT);
5367*8b26181fSAndroid Build Coastguard Worker 		break;
5368*8b26181fSAndroid Build Coastguard Worker 
5369*8b26181fSAndroid Build Coastguard Worker 	case Q_UDP:
5370*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_UDP, Q_DEFAULT, Q_DEFAULT);
5371*8b26181fSAndroid Build Coastguard Worker 		break;
5372*8b26181fSAndroid Build Coastguard Worker 
5373*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMP:
5374*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);
5375*8b26181fSAndroid Build Coastguard Worker 		break;
5376*8b26181fSAndroid Build Coastguard Worker 
5377*8b26181fSAndroid Build Coastguard Worker #ifndef	IPPROTO_IGMP
5378*8b26181fSAndroid Build Coastguard Worker #define	IPPROTO_IGMP	2
5379*8b26181fSAndroid Build Coastguard Worker #endif
5380*8b26181fSAndroid Build Coastguard Worker 
5381*8b26181fSAndroid Build Coastguard Worker 	case Q_IGMP:
5382*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);
5383*8b26181fSAndroid Build Coastguard Worker 		break;
5384*8b26181fSAndroid Build Coastguard Worker 
5385*8b26181fSAndroid Build Coastguard Worker #ifndef	IPPROTO_IGRP
5386*8b26181fSAndroid Build Coastguard Worker #define	IPPROTO_IGRP	9
5387*8b26181fSAndroid Build Coastguard Worker #endif
5388*8b26181fSAndroid Build Coastguard Worker 	case Q_IGRP:
5389*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);
5390*8b26181fSAndroid Build Coastguard Worker 		break;
5391*8b26181fSAndroid Build Coastguard Worker 
5392*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_PIM
5393*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_PIM	103
5394*8b26181fSAndroid Build Coastguard Worker #endif
5395*8b26181fSAndroid Build Coastguard Worker 
5396*8b26181fSAndroid Build Coastguard Worker 	case Q_PIM:
5397*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_PIM, Q_DEFAULT, Q_DEFAULT);
5398*8b26181fSAndroid Build Coastguard Worker 		break;
5399*8b26181fSAndroid Build Coastguard Worker 
5400*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_VRRP
5401*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_VRRP	112
5402*8b26181fSAndroid Build Coastguard Worker #endif
5403*8b26181fSAndroid Build Coastguard Worker 
5404*8b26181fSAndroid Build Coastguard Worker 	case Q_VRRP:
5405*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);
5406*8b26181fSAndroid Build Coastguard Worker 		break;
5407*8b26181fSAndroid Build Coastguard Worker 
5408*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_CARP
5409*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_CARP	112
5410*8b26181fSAndroid Build Coastguard Worker #endif
5411*8b26181fSAndroid Build Coastguard Worker 
5412*8b26181fSAndroid Build Coastguard Worker 	case Q_CARP:
5413*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);
5414*8b26181fSAndroid Build Coastguard Worker 		break;
5415*8b26181fSAndroid Build Coastguard Worker 
5416*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
5417*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_IP);
5418*8b26181fSAndroid Build Coastguard Worker 		break;
5419*8b26181fSAndroid Build Coastguard Worker 
5420*8b26181fSAndroid Build Coastguard Worker 	case Q_ARP:
5421*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_ARP);
5422*8b26181fSAndroid Build Coastguard Worker 		break;
5423*8b26181fSAndroid Build Coastguard Worker 
5424*8b26181fSAndroid Build Coastguard Worker 	case Q_RARP:
5425*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_REVARP);
5426*8b26181fSAndroid Build Coastguard Worker 		break;
5427*8b26181fSAndroid Build Coastguard Worker 
5428*8b26181fSAndroid Build Coastguard Worker 	case Q_LINK:
5429*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "link layer applied in wrong context");
5430*8b26181fSAndroid Build Coastguard Worker 
5431*8b26181fSAndroid Build Coastguard Worker 	case Q_ATALK:
5432*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_ATALK);
5433*8b26181fSAndroid Build Coastguard Worker 		break;
5434*8b26181fSAndroid Build Coastguard Worker 
5435*8b26181fSAndroid Build Coastguard Worker 	case Q_AARP:
5436*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_AARP);
5437*8b26181fSAndroid Build Coastguard Worker 		break;
5438*8b26181fSAndroid Build Coastguard Worker 
5439*8b26181fSAndroid Build Coastguard Worker 	case Q_DECNET:
5440*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_DN);
5441*8b26181fSAndroid Build Coastguard Worker 		break;
5442*8b26181fSAndroid Build Coastguard Worker 
5443*8b26181fSAndroid Build Coastguard Worker 	case Q_SCA:
5444*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_SCA);
5445*8b26181fSAndroid Build Coastguard Worker 		break;
5446*8b26181fSAndroid Build Coastguard Worker 
5447*8b26181fSAndroid Build Coastguard Worker 	case Q_LAT:
5448*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_LAT);
5449*8b26181fSAndroid Build Coastguard Worker 		break;
5450*8b26181fSAndroid Build Coastguard Worker 
5451*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPDL:
5452*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);
5453*8b26181fSAndroid Build Coastguard Worker 		break;
5454*8b26181fSAndroid Build Coastguard Worker 
5455*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPRC:
5456*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);
5457*8b26181fSAndroid Build Coastguard Worker 		break;
5458*8b26181fSAndroid Build Coastguard Worker 
5459*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
5460*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, ETHERTYPE_IPV6);
5461*8b26181fSAndroid Build Coastguard Worker 		break;
5462*8b26181fSAndroid Build Coastguard Worker 
5463*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_ICMPV6
5464*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_ICMPV6	58
5465*8b26181fSAndroid Build Coastguard Worker #endif
5466*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMPV6:
5467*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
5468*8b26181fSAndroid Build Coastguard Worker 		break;
5469*8b26181fSAndroid Build Coastguard Worker 
5470*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_AH
5471*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_AH	51
5472*8b26181fSAndroid Build Coastguard Worker #endif
5473*8b26181fSAndroid Build Coastguard Worker 	case Q_AH:
5474*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_AH, Q_DEFAULT, Q_DEFAULT);
5475*8b26181fSAndroid Build Coastguard Worker 		break;
5476*8b26181fSAndroid Build Coastguard Worker 
5477*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_ESP
5478*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_ESP	50
5479*8b26181fSAndroid Build Coastguard Worker #endif
5480*8b26181fSAndroid Build Coastguard Worker 	case Q_ESP:
5481*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, IPPROTO_ESP, Q_DEFAULT, Q_DEFAULT);
5482*8b26181fSAndroid Build Coastguard Worker 		break;
5483*8b26181fSAndroid Build Coastguard Worker 
5484*8b26181fSAndroid Build Coastguard Worker 	case Q_ISO:
5485*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, LLCSAP_ISONS);
5486*8b26181fSAndroid Build Coastguard Worker 		break;
5487*8b26181fSAndroid Build Coastguard Worker 
5488*8b26181fSAndroid Build Coastguard Worker 	case Q_ESIS:
5489*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);
5490*8b26181fSAndroid Build Coastguard Worker 		break;
5491*8b26181fSAndroid Build Coastguard Worker 
5492*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS:
5493*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5494*8b26181fSAndroid Build Coastguard Worker 		break;
5495*8b26181fSAndroid Build Coastguard Worker 
5496*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
5497*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5498*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5499*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5500*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5501*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5502*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5503*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5504*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5505*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5506*8b26181fSAndroid Build Coastguard Worker 		break;
5507*8b26181fSAndroid Build Coastguard Worker 
5508*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
5509*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5510*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5511*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5512*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5513*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5514*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5515*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5516*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5517*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5518*8b26181fSAndroid Build Coastguard Worker 		break;
5519*8b26181fSAndroid Build Coastguard Worker 
5520*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
5521*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5522*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5523*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5524*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
5525*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5526*8b26181fSAndroid Build Coastguard Worker 		break;
5527*8b26181fSAndroid Build Coastguard Worker 
5528*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_LSP:
5529*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5530*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5531*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5532*8b26181fSAndroid Build Coastguard Worker 		break;
5533*8b26181fSAndroid Build Coastguard Worker 
5534*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_SNP:
5535*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5536*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5537*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5538*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5539*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5540*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5541*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5542*8b26181fSAndroid Build Coastguard Worker 		break;
5543*8b26181fSAndroid Build Coastguard Worker 
5544*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_CSNP:
5545*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5546*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5547*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5548*8b26181fSAndroid Build Coastguard Worker 		break;
5549*8b26181fSAndroid Build Coastguard Worker 
5550*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_PSNP:
5551*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5552*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5553*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
5554*8b26181fSAndroid Build Coastguard Worker 		break;
5555*8b26181fSAndroid Build Coastguard Worker 
5556*8b26181fSAndroid Build Coastguard Worker 	case Q_CLNP:
5557*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);
5558*8b26181fSAndroid Build Coastguard Worker 		break;
5559*8b26181fSAndroid Build Coastguard Worker 
5560*8b26181fSAndroid Build Coastguard Worker 	case Q_STP:
5561*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, LLCSAP_8021D);
5562*8b26181fSAndroid Build Coastguard Worker 		break;
5563*8b26181fSAndroid Build Coastguard Worker 
5564*8b26181fSAndroid Build Coastguard Worker 	case Q_IPX:
5565*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, LLCSAP_IPX);
5566*8b26181fSAndroid Build Coastguard Worker 		break;
5567*8b26181fSAndroid Build Coastguard Worker 
5568*8b26181fSAndroid Build Coastguard Worker 	case Q_NETBEUI:
5569*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_linktype(cstate, LLCSAP_NETBEUI);
5570*8b26181fSAndroid Build Coastguard Worker 		break;
5571*8b26181fSAndroid Build Coastguard Worker 
5572*8b26181fSAndroid Build Coastguard Worker 	case Q_RADIO:
5573*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'radio' is not a valid protocol type");
5574*8b26181fSAndroid Build Coastguard Worker 
5575*8b26181fSAndroid Build Coastguard Worker 	default:
5576*8b26181fSAndroid Build Coastguard Worker 		abort();
5577*8b26181fSAndroid Build Coastguard Worker 	}
5578*8b26181fSAndroid Build Coastguard Worker 	return b1;
5579*8b26181fSAndroid Build Coastguard Worker }
5580*8b26181fSAndroid Build Coastguard Worker 
5581*8b26181fSAndroid Build Coastguard Worker struct block *
gen_proto_abbrev(compiler_state_t * cstate,int proto)5582*8b26181fSAndroid Build Coastguard Worker gen_proto_abbrev(compiler_state_t *cstate, int proto)
5583*8b26181fSAndroid Build Coastguard Worker {
5584*8b26181fSAndroid Build Coastguard Worker 	/*
5585*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
5586*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
5587*8b26181fSAndroid Build Coastguard Worker 	 */
5588*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
5589*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
5590*8b26181fSAndroid Build Coastguard Worker 
5591*8b26181fSAndroid Build Coastguard Worker 	return gen_proto_abbrev_internal(cstate, proto);
5592*8b26181fSAndroid Build Coastguard Worker }
5593*8b26181fSAndroid Build Coastguard Worker 
5594*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_ipfrag(compiler_state_t * cstate)5595*8b26181fSAndroid Build Coastguard Worker gen_ipfrag(compiler_state_t *cstate)
5596*8b26181fSAndroid Build Coastguard Worker {
5597*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
5598*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
5599*8b26181fSAndroid Build Coastguard Worker 
5600*8b26181fSAndroid Build Coastguard Worker 	/* not IPv4 frag other than the first frag */
5601*8b26181fSAndroid Build Coastguard Worker 	s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);
5602*8b26181fSAndroid Build Coastguard Worker 	b = new_block(cstate, JMP(BPF_JSET));
5603*8b26181fSAndroid Build Coastguard Worker 	b->s.k = 0x1fff;
5604*8b26181fSAndroid Build Coastguard Worker 	b->stmts = s;
5605*8b26181fSAndroid Build Coastguard Worker 	gen_not(b);
5606*8b26181fSAndroid Build Coastguard Worker 
5607*8b26181fSAndroid Build Coastguard Worker 	return b;
5608*8b26181fSAndroid Build Coastguard Worker }
5609*8b26181fSAndroid Build Coastguard Worker 
5610*8b26181fSAndroid Build Coastguard Worker /*
5611*8b26181fSAndroid Build Coastguard Worker  * Generate a comparison to a port value in the transport-layer header
5612*8b26181fSAndroid Build Coastguard Worker  * at the specified offset from the beginning of that header.
5613*8b26181fSAndroid Build Coastguard Worker  *
5614*8b26181fSAndroid Build Coastguard Worker  * XXX - this handles a variable-length prefix preceding the link-layer
5615*8b26181fSAndroid Build Coastguard Worker  * header, such as the radiotap or AVS radio prefix, but doesn't handle
5616*8b26181fSAndroid Build Coastguard Worker  * variable-length link-layer headers (such as Token Ring or 802.11
5617*8b26181fSAndroid Build Coastguard Worker  * headers).
5618*8b26181fSAndroid Build Coastguard Worker  */
5619*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portatom(compiler_state_t * cstate,int off,bpf_u_int32 v)5620*8b26181fSAndroid Build Coastguard Worker gen_portatom(compiler_state_t *cstate, int off, bpf_u_int32 v)
5621*8b26181fSAndroid Build Coastguard Worker {
5622*8b26181fSAndroid Build Coastguard Worker 	return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);
5623*8b26181fSAndroid Build Coastguard Worker }
5624*8b26181fSAndroid Build Coastguard Worker 
5625*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portatom6(compiler_state_t * cstate,int off,bpf_u_int32 v)5626*8b26181fSAndroid Build Coastguard Worker gen_portatom6(compiler_state_t *cstate, int off, bpf_u_int32 v)
5627*8b26181fSAndroid Build Coastguard Worker {
5628*8b26181fSAndroid Build Coastguard Worker 	return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);
5629*8b26181fSAndroid Build Coastguard Worker }
5630*8b26181fSAndroid Build Coastguard Worker 
5631*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portop(compiler_state_t * cstate,u_int port,u_int proto,int dir)5632*8b26181fSAndroid Build Coastguard Worker gen_portop(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5633*8b26181fSAndroid Build Coastguard Worker {
5634*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
5635*8b26181fSAndroid Build Coastguard Worker 
5636*8b26181fSAndroid Build Coastguard Worker 	/* ip proto 'proto' and not a fragment other than the first fragment */
5637*8b26181fSAndroid Build Coastguard Worker 	tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5638*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_ipfrag(cstate);
5639*8b26181fSAndroid Build Coastguard Worker 	gen_and(tmp, b0);
5640*8b26181fSAndroid Build Coastguard Worker 
5641*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
5642*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
5643*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portatom(cstate, 0, port);
5644*8b26181fSAndroid Build Coastguard Worker 		break;
5645*8b26181fSAndroid Build Coastguard Worker 
5646*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
5647*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portatom(cstate, 2, port);
5648*8b26181fSAndroid Build Coastguard Worker 		break;
5649*8b26181fSAndroid Build Coastguard Worker 
5650*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
5651*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portatom(cstate, 0, port);
5652*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portatom(cstate, 2, port);
5653*8b26181fSAndroid Build Coastguard Worker 		gen_and(tmp, b1);
5654*8b26181fSAndroid Build Coastguard Worker 		break;
5655*8b26181fSAndroid Build Coastguard Worker 
5656*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
5657*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
5658*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portatom(cstate, 0, port);
5659*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portatom(cstate, 2, port);
5660*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5661*8b26181fSAndroid Build Coastguard Worker 		break;
5662*8b26181fSAndroid Build Coastguard Worker 
5663*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
5664*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports");
5665*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5666*8b26181fSAndroid Build Coastguard Worker 
5667*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
5668*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports");
5669*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5670*8b26181fSAndroid Build Coastguard Worker 
5671*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
5672*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports");
5673*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5674*8b26181fSAndroid Build Coastguard Worker 
5675*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
5676*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports");
5677*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5678*8b26181fSAndroid Build Coastguard Worker 
5679*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
5680*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is not a valid qualifier for ports");
5681*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5682*8b26181fSAndroid Build Coastguard Worker 
5683*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
5684*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is not a valid qualifier for ports");
5685*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5686*8b26181fSAndroid Build Coastguard Worker 
5687*8b26181fSAndroid Build Coastguard Worker 	default:
5688*8b26181fSAndroid Build Coastguard Worker 		abort();
5689*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5690*8b26181fSAndroid Build Coastguard Worker 	}
5691*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
5692*8b26181fSAndroid Build Coastguard Worker 
5693*8b26181fSAndroid Build Coastguard Worker 	return b1;
5694*8b26181fSAndroid Build Coastguard Worker }
5695*8b26181fSAndroid Build Coastguard Worker 
5696*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_port(compiler_state_t * cstate,u_int port,int ip_proto,int dir)5697*8b26181fSAndroid Build Coastguard Worker gen_port(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5698*8b26181fSAndroid Build Coastguard Worker {
5699*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
5700*8b26181fSAndroid Build Coastguard Worker 
5701*8b26181fSAndroid Build Coastguard Worker 	/*
5702*8b26181fSAndroid Build Coastguard Worker 	 * ether proto ip
5703*8b26181fSAndroid Build Coastguard Worker 	 *
5704*8b26181fSAndroid Build Coastguard Worker 	 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5705*8b26181fSAndroid Build Coastguard Worker 	 * not LLC encapsulation with LLCSAP_IP.
5706*8b26181fSAndroid Build Coastguard Worker 	 *
5707*8b26181fSAndroid Build Coastguard Worker 	 * For IEEE 802 networks - which includes 802.5 token ring
5708*8b26181fSAndroid Build Coastguard Worker 	 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5709*8b26181fSAndroid Build Coastguard Worker 	 * says that SNAP encapsulation is used, not LLC encapsulation
5710*8b26181fSAndroid Build Coastguard Worker 	 * with LLCSAP_IP.
5711*8b26181fSAndroid Build Coastguard Worker 	 *
5712*8b26181fSAndroid Build Coastguard Worker 	 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5713*8b26181fSAndroid Build Coastguard Worker 	 * RFC 2225 say that SNAP encapsulation is used, not LLC
5714*8b26181fSAndroid Build Coastguard Worker 	 * encapsulation with LLCSAP_IP.
5715*8b26181fSAndroid Build Coastguard Worker 	 *
5716*8b26181fSAndroid Build Coastguard Worker 	 * So we always check for ETHERTYPE_IP.
5717*8b26181fSAndroid Build Coastguard Worker 	 */
5718*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ETHERTYPE_IP);
5719*8b26181fSAndroid Build Coastguard Worker 
5720*8b26181fSAndroid Build Coastguard Worker 	switch (ip_proto) {
5721*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_UDP:
5722*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_TCP:
5723*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_SCTP:
5724*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portop(cstate, port, (u_int)ip_proto, dir);
5725*8b26181fSAndroid Build Coastguard Worker 		break;
5726*8b26181fSAndroid Build Coastguard Worker 
5727*8b26181fSAndroid Build Coastguard Worker 	case PROTO_UNDEF:
5728*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);
5729*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);
5730*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5731*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);
5732*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5733*8b26181fSAndroid Build Coastguard Worker 		break;
5734*8b26181fSAndroid Build Coastguard Worker 
5735*8b26181fSAndroid Build Coastguard Worker 	default:
5736*8b26181fSAndroid Build Coastguard Worker 		abort();
5737*8b26181fSAndroid Build Coastguard Worker 	}
5738*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
5739*8b26181fSAndroid Build Coastguard Worker 	return b1;
5740*8b26181fSAndroid Build Coastguard Worker }
5741*8b26181fSAndroid Build Coastguard Worker 
5742*8b26181fSAndroid Build Coastguard Worker struct block *
gen_portop6(compiler_state_t * cstate,u_int port,u_int proto,int dir)5743*8b26181fSAndroid Build Coastguard Worker gen_portop6(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5744*8b26181fSAndroid Build Coastguard Worker {
5745*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
5746*8b26181fSAndroid Build Coastguard Worker 
5747*8b26181fSAndroid Build Coastguard Worker 	/* ip6 proto 'proto' */
5748*8b26181fSAndroid Build Coastguard Worker 	/* XXX - catch the first fragment of a fragmented packet? */
5749*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5750*8b26181fSAndroid Build Coastguard Worker 
5751*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
5752*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
5753*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portatom6(cstate, 0, port);
5754*8b26181fSAndroid Build Coastguard Worker 		break;
5755*8b26181fSAndroid Build Coastguard Worker 
5756*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
5757*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portatom6(cstate, 2, port);
5758*8b26181fSAndroid Build Coastguard Worker 		break;
5759*8b26181fSAndroid Build Coastguard Worker 
5760*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
5761*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portatom6(cstate, 0, port);
5762*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portatom6(cstate, 2, port);
5763*8b26181fSAndroid Build Coastguard Worker 		gen_and(tmp, b1);
5764*8b26181fSAndroid Build Coastguard Worker 		break;
5765*8b26181fSAndroid Build Coastguard Worker 
5766*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
5767*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
5768*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portatom6(cstate, 0, port);
5769*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portatom6(cstate, 2, port);
5770*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5771*8b26181fSAndroid Build Coastguard Worker 		break;
5772*8b26181fSAndroid Build Coastguard Worker 
5773*8b26181fSAndroid Build Coastguard Worker 	default:
5774*8b26181fSAndroid Build Coastguard Worker 		abort();
5775*8b26181fSAndroid Build Coastguard Worker 	}
5776*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
5777*8b26181fSAndroid Build Coastguard Worker 
5778*8b26181fSAndroid Build Coastguard Worker 	return b1;
5779*8b26181fSAndroid Build Coastguard Worker }
5780*8b26181fSAndroid Build Coastguard Worker 
5781*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_port6(compiler_state_t * cstate,u_int port,int ip_proto,int dir)5782*8b26181fSAndroid Build Coastguard Worker gen_port6(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5783*8b26181fSAndroid Build Coastguard Worker {
5784*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
5785*8b26181fSAndroid Build Coastguard Worker 
5786*8b26181fSAndroid Build Coastguard Worker 	/* link proto ip6 */
5787*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5788*8b26181fSAndroid Build Coastguard Worker 
5789*8b26181fSAndroid Build Coastguard Worker 	switch (ip_proto) {
5790*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_UDP:
5791*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_TCP:
5792*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_SCTP:
5793*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portop6(cstate, port, (u_int)ip_proto, dir);
5794*8b26181fSAndroid Build Coastguard Worker 		break;
5795*8b26181fSAndroid Build Coastguard Worker 
5796*8b26181fSAndroid Build Coastguard Worker 	case PROTO_UNDEF:
5797*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);
5798*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);
5799*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5800*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);
5801*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5802*8b26181fSAndroid Build Coastguard Worker 		break;
5803*8b26181fSAndroid Build Coastguard Worker 
5804*8b26181fSAndroid Build Coastguard Worker 	default:
5805*8b26181fSAndroid Build Coastguard Worker 		abort();
5806*8b26181fSAndroid Build Coastguard Worker 	}
5807*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
5808*8b26181fSAndroid Build Coastguard Worker 	return b1;
5809*8b26181fSAndroid Build Coastguard Worker }
5810*8b26181fSAndroid Build Coastguard Worker 
5811*8b26181fSAndroid Build Coastguard Worker /* gen_portrange code */
5812*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portrangeatom(compiler_state_t * cstate,u_int off,bpf_u_int32 v1,bpf_u_int32 v2)5813*8b26181fSAndroid Build Coastguard Worker gen_portrangeatom(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
5814*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 v2)
5815*8b26181fSAndroid Build Coastguard Worker {
5816*8b26181fSAndroid Build Coastguard Worker 	struct block *b1, *b2;
5817*8b26181fSAndroid Build Coastguard Worker 
5818*8b26181fSAndroid Build Coastguard Worker 	if (v1 > v2) {
5819*8b26181fSAndroid Build Coastguard Worker 		/*
5820*8b26181fSAndroid Build Coastguard Worker 		 * Reverse the order of the ports, so v1 is the lower one.
5821*8b26181fSAndroid Build Coastguard Worker 		 */
5822*8b26181fSAndroid Build Coastguard Worker 		bpf_u_int32 vtemp;
5823*8b26181fSAndroid Build Coastguard Worker 
5824*8b26181fSAndroid Build Coastguard Worker 		vtemp = v1;
5825*8b26181fSAndroid Build Coastguard Worker 		v1 = v2;
5826*8b26181fSAndroid Build Coastguard Worker 		v2 = vtemp;
5827*8b26181fSAndroid Build Coastguard Worker 	}
5828*8b26181fSAndroid Build Coastguard Worker 
5829*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);
5830*8b26181fSAndroid Build Coastguard Worker 	b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);
5831*8b26181fSAndroid Build Coastguard Worker 
5832*8b26181fSAndroid Build Coastguard Worker 	gen_and(b1, b2);
5833*8b26181fSAndroid Build Coastguard Worker 
5834*8b26181fSAndroid Build Coastguard Worker 	return b2;
5835*8b26181fSAndroid Build Coastguard Worker }
5836*8b26181fSAndroid Build Coastguard Worker 
5837*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portrangeop(compiler_state_t * cstate,u_int port1,u_int port2,bpf_u_int32 proto,int dir)5838*8b26181fSAndroid Build Coastguard Worker gen_portrangeop(compiler_state_t *cstate, u_int port1, u_int port2,
5839*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 proto, int dir)
5840*8b26181fSAndroid Build Coastguard Worker {
5841*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
5842*8b26181fSAndroid Build Coastguard Worker 
5843*8b26181fSAndroid Build Coastguard Worker 	/* ip proto 'proto' and not a fragment other than the first fragment */
5844*8b26181fSAndroid Build Coastguard Worker 	tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5845*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_ipfrag(cstate);
5846*8b26181fSAndroid Build Coastguard Worker 	gen_and(tmp, b0);
5847*8b26181fSAndroid Build Coastguard Worker 
5848*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
5849*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
5850*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeatom(cstate, 0, port1, port2);
5851*8b26181fSAndroid Build Coastguard Worker 		break;
5852*8b26181fSAndroid Build Coastguard Worker 
5853*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
5854*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeatom(cstate, 2, port1, port2);
5855*8b26181fSAndroid Build Coastguard Worker 		break;
5856*8b26181fSAndroid Build Coastguard Worker 
5857*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
5858*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portrangeatom(cstate, 0, port1, port2);
5859*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeatom(cstate, 2, port1, port2);
5860*8b26181fSAndroid Build Coastguard Worker 		gen_and(tmp, b1);
5861*8b26181fSAndroid Build Coastguard Worker 		break;
5862*8b26181fSAndroid Build Coastguard Worker 
5863*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
5864*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
5865*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portrangeatom(cstate, 0, port1, port2);
5866*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeatom(cstate, 2, port1, port2);
5867*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5868*8b26181fSAndroid Build Coastguard Worker 		break;
5869*8b26181fSAndroid Build Coastguard Worker 
5870*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
5871*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5872*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5873*8b26181fSAndroid Build Coastguard Worker 
5874*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
5875*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5876*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5877*8b26181fSAndroid Build Coastguard Worker 
5878*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
5879*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5880*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5881*8b26181fSAndroid Build Coastguard Worker 
5882*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
5883*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5884*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5885*8b26181fSAndroid Build Coastguard Worker 
5886*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
5887*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is not a valid qualifier for port ranges");
5888*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5889*8b26181fSAndroid Build Coastguard Worker 
5890*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
5891*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is not a valid qualifier for port ranges");
5892*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5893*8b26181fSAndroid Build Coastguard Worker 
5894*8b26181fSAndroid Build Coastguard Worker 	default:
5895*8b26181fSAndroid Build Coastguard Worker 		abort();
5896*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
5897*8b26181fSAndroid Build Coastguard Worker 	}
5898*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
5899*8b26181fSAndroid Build Coastguard Worker 
5900*8b26181fSAndroid Build Coastguard Worker 	return b1;
5901*8b26181fSAndroid Build Coastguard Worker }
5902*8b26181fSAndroid Build Coastguard Worker 
5903*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portrange(compiler_state_t * cstate,u_int port1,u_int port2,int ip_proto,int dir)5904*8b26181fSAndroid Build Coastguard Worker gen_portrange(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
5905*8b26181fSAndroid Build Coastguard Worker     int dir)
5906*8b26181fSAndroid Build Coastguard Worker {
5907*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
5908*8b26181fSAndroid Build Coastguard Worker 
5909*8b26181fSAndroid Build Coastguard Worker 	/* link proto ip */
5910*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ETHERTYPE_IP);
5911*8b26181fSAndroid Build Coastguard Worker 
5912*8b26181fSAndroid Build Coastguard Worker 	switch (ip_proto) {
5913*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_UDP:
5914*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_TCP:
5915*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_SCTP:
5916*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeop(cstate, port1, port2, (bpf_u_int32)ip_proto,
5917*8b26181fSAndroid Build Coastguard Worker 		    dir);
5918*8b26181fSAndroid Build Coastguard Worker 		break;
5919*8b26181fSAndroid Build Coastguard Worker 
5920*8b26181fSAndroid Build Coastguard Worker 	case PROTO_UNDEF:
5921*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);
5922*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);
5923*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5924*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);
5925*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5926*8b26181fSAndroid Build Coastguard Worker 		break;
5927*8b26181fSAndroid Build Coastguard Worker 
5928*8b26181fSAndroid Build Coastguard Worker 	default:
5929*8b26181fSAndroid Build Coastguard Worker 		abort();
5930*8b26181fSAndroid Build Coastguard Worker 	}
5931*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
5932*8b26181fSAndroid Build Coastguard Worker 	return b1;
5933*8b26181fSAndroid Build Coastguard Worker }
5934*8b26181fSAndroid Build Coastguard Worker 
5935*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portrangeatom6(compiler_state_t * cstate,u_int off,bpf_u_int32 v1,bpf_u_int32 v2)5936*8b26181fSAndroid Build Coastguard Worker gen_portrangeatom6(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
5937*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 v2)
5938*8b26181fSAndroid Build Coastguard Worker {
5939*8b26181fSAndroid Build Coastguard Worker 	struct block *b1, *b2;
5940*8b26181fSAndroid Build Coastguard Worker 
5941*8b26181fSAndroid Build Coastguard Worker 	if (v1 > v2) {
5942*8b26181fSAndroid Build Coastguard Worker 		/*
5943*8b26181fSAndroid Build Coastguard Worker 		 * Reverse the order of the ports, so v1 is the lower one.
5944*8b26181fSAndroid Build Coastguard Worker 		 */
5945*8b26181fSAndroid Build Coastguard Worker 		bpf_u_int32 vtemp;
5946*8b26181fSAndroid Build Coastguard Worker 
5947*8b26181fSAndroid Build Coastguard Worker 		vtemp = v1;
5948*8b26181fSAndroid Build Coastguard Worker 		v1 = v2;
5949*8b26181fSAndroid Build Coastguard Worker 		v2 = vtemp;
5950*8b26181fSAndroid Build Coastguard Worker 	}
5951*8b26181fSAndroid Build Coastguard Worker 
5952*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);
5953*8b26181fSAndroid Build Coastguard Worker 	b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);
5954*8b26181fSAndroid Build Coastguard Worker 
5955*8b26181fSAndroid Build Coastguard Worker 	gen_and(b1, b2);
5956*8b26181fSAndroid Build Coastguard Worker 
5957*8b26181fSAndroid Build Coastguard Worker 	return b2;
5958*8b26181fSAndroid Build Coastguard Worker }
5959*8b26181fSAndroid Build Coastguard Worker 
5960*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portrangeop6(compiler_state_t * cstate,u_int port1,u_int port2,bpf_u_int32 proto,int dir)5961*8b26181fSAndroid Build Coastguard Worker gen_portrangeop6(compiler_state_t *cstate, u_int port1, u_int port2,
5962*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 proto, int dir)
5963*8b26181fSAndroid Build Coastguard Worker {
5964*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
5965*8b26181fSAndroid Build Coastguard Worker 
5966*8b26181fSAndroid Build Coastguard Worker 	/* ip6 proto 'proto' */
5967*8b26181fSAndroid Build Coastguard Worker 	/* XXX - catch the first fragment of a fragmented packet? */
5968*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5969*8b26181fSAndroid Build Coastguard Worker 
5970*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
5971*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
5972*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeatom6(cstate, 0, port1, port2);
5973*8b26181fSAndroid Build Coastguard Worker 		break;
5974*8b26181fSAndroid Build Coastguard Worker 
5975*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
5976*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5977*8b26181fSAndroid Build Coastguard Worker 		break;
5978*8b26181fSAndroid Build Coastguard Worker 
5979*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
5980*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portrangeatom6(cstate, 0, port1, port2);
5981*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5982*8b26181fSAndroid Build Coastguard Worker 		gen_and(tmp, b1);
5983*8b26181fSAndroid Build Coastguard Worker 		break;
5984*8b26181fSAndroid Build Coastguard Worker 
5985*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
5986*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
5987*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portrangeatom6(cstate, 0, port1, port2);
5988*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5989*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
5990*8b26181fSAndroid Build Coastguard Worker 		break;
5991*8b26181fSAndroid Build Coastguard Worker 
5992*8b26181fSAndroid Build Coastguard Worker 	default:
5993*8b26181fSAndroid Build Coastguard Worker 		abort();
5994*8b26181fSAndroid Build Coastguard Worker 	}
5995*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
5996*8b26181fSAndroid Build Coastguard Worker 
5997*8b26181fSAndroid Build Coastguard Worker 	return b1;
5998*8b26181fSAndroid Build Coastguard Worker }
5999*8b26181fSAndroid Build Coastguard Worker 
6000*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_portrange6(compiler_state_t * cstate,u_int port1,u_int port2,int ip_proto,int dir)6001*8b26181fSAndroid Build Coastguard Worker gen_portrange6(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
6002*8b26181fSAndroid Build Coastguard Worker     int dir)
6003*8b26181fSAndroid Build Coastguard Worker {
6004*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *tmp;
6005*8b26181fSAndroid Build Coastguard Worker 
6006*8b26181fSAndroid Build Coastguard Worker 	/* link proto ip6 */
6007*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6008*8b26181fSAndroid Build Coastguard Worker 
6009*8b26181fSAndroid Build Coastguard Worker 	switch (ip_proto) {
6010*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_UDP:
6011*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_TCP:
6012*8b26181fSAndroid Build Coastguard Worker 	case IPPROTO_SCTP:
6013*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeop6(cstate, port1, port2, (bpf_u_int32)ip_proto,
6014*8b26181fSAndroid Build Coastguard Worker 		    dir);
6015*8b26181fSAndroid Build Coastguard Worker 		break;
6016*8b26181fSAndroid Build Coastguard Worker 
6017*8b26181fSAndroid Build Coastguard Worker 	case PROTO_UNDEF:
6018*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);
6019*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);
6020*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
6021*8b26181fSAndroid Build Coastguard Worker 		tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);
6022*8b26181fSAndroid Build Coastguard Worker 		gen_or(tmp, b1);
6023*8b26181fSAndroid Build Coastguard Worker 		break;
6024*8b26181fSAndroid Build Coastguard Worker 
6025*8b26181fSAndroid Build Coastguard Worker 	default:
6026*8b26181fSAndroid Build Coastguard Worker 		abort();
6027*8b26181fSAndroid Build Coastguard Worker 	}
6028*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
6029*8b26181fSAndroid Build Coastguard Worker 	return b1;
6030*8b26181fSAndroid Build Coastguard Worker }
6031*8b26181fSAndroid Build Coastguard Worker 
6032*8b26181fSAndroid Build Coastguard Worker static int
lookup_proto(compiler_state_t * cstate,const char * name,int proto)6033*8b26181fSAndroid Build Coastguard Worker lookup_proto(compiler_state_t *cstate, const char *name, int proto)
6034*8b26181fSAndroid Build Coastguard Worker {
6035*8b26181fSAndroid Build Coastguard Worker 	register int v;
6036*8b26181fSAndroid Build Coastguard Worker 
6037*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
6038*8b26181fSAndroid Build Coastguard Worker 
6039*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
6040*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
6041*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
6042*8b26181fSAndroid Build Coastguard Worker 		v = pcap_nametoproto(name);
6043*8b26181fSAndroid Build Coastguard Worker 		if (v == PROTO_UNDEF)
6044*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown ip proto '%s'", name);
6045*8b26181fSAndroid Build Coastguard Worker 		break;
6046*8b26181fSAndroid Build Coastguard Worker 
6047*8b26181fSAndroid Build Coastguard Worker 	case Q_LINK:
6048*8b26181fSAndroid Build Coastguard Worker 		/* XXX should look up h/w protocol type based on cstate->linktype */
6049*8b26181fSAndroid Build Coastguard Worker 		v = pcap_nametoeproto(name);
6050*8b26181fSAndroid Build Coastguard Worker 		if (v == PROTO_UNDEF) {
6051*8b26181fSAndroid Build Coastguard Worker 			v = pcap_nametollc(name);
6052*8b26181fSAndroid Build Coastguard Worker 			if (v == PROTO_UNDEF)
6053*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "unknown ether proto '%s'", name);
6054*8b26181fSAndroid Build Coastguard Worker 		}
6055*8b26181fSAndroid Build Coastguard Worker 		break;
6056*8b26181fSAndroid Build Coastguard Worker 
6057*8b26181fSAndroid Build Coastguard Worker 	case Q_ISO:
6058*8b26181fSAndroid Build Coastguard Worker 		if (strcmp(name, "esis") == 0)
6059*8b26181fSAndroid Build Coastguard Worker 			v = ISO9542_ESIS;
6060*8b26181fSAndroid Build Coastguard Worker 		else if (strcmp(name, "isis") == 0)
6061*8b26181fSAndroid Build Coastguard Worker 			v = ISO10589_ISIS;
6062*8b26181fSAndroid Build Coastguard Worker 		else if (strcmp(name, "clnp") == 0)
6063*8b26181fSAndroid Build Coastguard Worker 			v = ISO8473_CLNP;
6064*8b26181fSAndroid Build Coastguard Worker 		else
6065*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown osi proto '%s'", name);
6066*8b26181fSAndroid Build Coastguard Worker 		break;
6067*8b26181fSAndroid Build Coastguard Worker 
6068*8b26181fSAndroid Build Coastguard Worker 	default:
6069*8b26181fSAndroid Build Coastguard Worker 		v = PROTO_UNDEF;
6070*8b26181fSAndroid Build Coastguard Worker 		break;
6071*8b26181fSAndroid Build Coastguard Worker 	}
6072*8b26181fSAndroid Build Coastguard Worker 	return v;
6073*8b26181fSAndroid Build Coastguard Worker }
6074*8b26181fSAndroid Build Coastguard Worker 
6075*8b26181fSAndroid Build Coastguard Worker #if !defined(NO_PROTOCHAIN)
6076*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_protochain(compiler_state_t * cstate,bpf_u_int32 v,int proto)6077*8b26181fSAndroid Build Coastguard Worker gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto)
6078*8b26181fSAndroid Build Coastguard Worker {
6079*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b;
6080*8b26181fSAndroid Build Coastguard Worker 	struct slist *s[100];
6081*8b26181fSAndroid Build Coastguard Worker 	int fix2, fix3, fix4, fix5;
6082*8b26181fSAndroid Build Coastguard Worker 	int ahcheck, again, end;
6083*8b26181fSAndroid Build Coastguard Worker 	int i, max;
6084*8b26181fSAndroid Build Coastguard Worker 	int reg2 = alloc_reg(cstate);
6085*8b26181fSAndroid Build Coastguard Worker 
6086*8b26181fSAndroid Build Coastguard Worker 	memset(s, 0, sizeof(s));
6087*8b26181fSAndroid Build Coastguard Worker 	fix3 = fix4 = fix5 = 0;
6088*8b26181fSAndroid Build Coastguard Worker 
6089*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
6090*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
6091*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
6092*8b26181fSAndroid Build Coastguard Worker 		break;
6093*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
6094*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_protochain(cstate, v, Q_IP);
6095*8b26181fSAndroid Build Coastguard Worker 		b = gen_protochain(cstate, v, Q_IPV6);
6096*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b);
6097*8b26181fSAndroid Build Coastguard Worker 		return b;
6098*8b26181fSAndroid Build Coastguard Worker 	default:
6099*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "bad protocol applied for 'protochain'");
6100*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6101*8b26181fSAndroid Build Coastguard Worker 	}
6102*8b26181fSAndroid Build Coastguard Worker 
6103*8b26181fSAndroid Build Coastguard Worker 	/*
6104*8b26181fSAndroid Build Coastguard Worker 	 * We don't handle variable-length prefixes before the link-layer
6105*8b26181fSAndroid Build Coastguard Worker 	 * header, or variable-length link-layer headers, here yet.
6106*8b26181fSAndroid Build Coastguard Worker 	 * We might want to add BPF instructions to do the protochain
6107*8b26181fSAndroid Build Coastguard Worker 	 * work, to simplify that and, on platforms that have a BPF
6108*8b26181fSAndroid Build Coastguard Worker 	 * interpreter with the new instructions, let the filtering
6109*8b26181fSAndroid Build Coastguard Worker 	 * be done in the kernel.  (We already require a modified BPF
6110*8b26181fSAndroid Build Coastguard Worker 	 * engine to do the protochain stuff, to support backward
6111*8b26181fSAndroid Build Coastguard Worker 	 * branches, and backward branch support is unlikely to appear
6112*8b26181fSAndroid Build Coastguard Worker 	 * in kernel BPF engines.)
6113*8b26181fSAndroid Build Coastguard Worker 	 */
6114*8b26181fSAndroid Build Coastguard Worker 	if (cstate->off_linkpl.is_variable)
6115*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'protochain' not supported with variable length headers");
6116*8b26181fSAndroid Build Coastguard Worker 
6117*8b26181fSAndroid Build Coastguard Worker 	/*
6118*8b26181fSAndroid Build Coastguard Worker 	 * To quote a comment in optimize.c:
6119*8b26181fSAndroid Build Coastguard Worker 	 *
6120*8b26181fSAndroid Build Coastguard Worker 	 * "These data structures are used in a Cocke and Shwarz style
6121*8b26181fSAndroid Build Coastguard Worker 	 * value numbering scheme.  Since the flowgraph is acyclic,
6122*8b26181fSAndroid Build Coastguard Worker 	 * exit values can be propagated from a node's predecessors
6123*8b26181fSAndroid Build Coastguard Worker 	 * provided it is uniquely defined."
6124*8b26181fSAndroid Build Coastguard Worker 	 *
6125*8b26181fSAndroid Build Coastguard Worker 	 * "Acyclic" means "no backward branches", which means "no
6126*8b26181fSAndroid Build Coastguard Worker 	 * loops", so we have to turn the optimizer off.
6127*8b26181fSAndroid Build Coastguard Worker 	 */
6128*8b26181fSAndroid Build Coastguard Worker 	cstate->no_optimize = 1;
6129*8b26181fSAndroid Build Coastguard Worker 
6130*8b26181fSAndroid Build Coastguard Worker 	/*
6131*8b26181fSAndroid Build Coastguard Worker 	 * s[0] is a dummy entry to protect other BPF insn from damage
6132*8b26181fSAndroid Build Coastguard Worker 	 * by s[fix] = foo with uninitialized variable "fix".  It is somewhat
6133*8b26181fSAndroid Build Coastguard Worker 	 * hard to find interdependency made by jump table fixup.
6134*8b26181fSAndroid Build Coastguard Worker 	 */
6135*8b26181fSAndroid Build Coastguard Worker 	i = 0;
6136*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, 0);	/*dummy*/
6137*8b26181fSAndroid Build Coastguard Worker 	i++;
6138*8b26181fSAndroid Build Coastguard Worker 
6139*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
6140*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
6141*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_linktype(cstate, ETHERTYPE_IP);
6142*8b26181fSAndroid Build Coastguard Worker 
6143*8b26181fSAndroid Build Coastguard Worker 		/* A = ip->ip_p */
6144*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6145*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;
6146*8b26181fSAndroid Build Coastguard Worker 		i++;
6147*8b26181fSAndroid Build Coastguard Worker 		/* X = ip->ip_hl << 2 */
6148*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
6149*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6150*8b26181fSAndroid Build Coastguard Worker 		i++;
6151*8b26181fSAndroid Build Coastguard Worker 		break;
6152*8b26181fSAndroid Build Coastguard Worker 
6153*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
6154*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6155*8b26181fSAndroid Build Coastguard Worker 
6156*8b26181fSAndroid Build Coastguard Worker 		/* A = ip6->ip_nxt */
6157*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6158*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;
6159*8b26181fSAndroid Build Coastguard Worker 		i++;
6160*8b26181fSAndroid Build Coastguard Worker 		/* X = sizeof(struct ip6_hdr) */
6161*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);
6162*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = 40;
6163*8b26181fSAndroid Build Coastguard Worker 		i++;
6164*8b26181fSAndroid Build Coastguard Worker 		break;
6165*8b26181fSAndroid Build Coastguard Worker 
6166*8b26181fSAndroid Build Coastguard Worker 	default:
6167*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "unsupported proto to gen_protochain");
6168*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6169*8b26181fSAndroid Build Coastguard Worker 	}
6170*8b26181fSAndroid Build Coastguard Worker 
6171*8b26181fSAndroid Build Coastguard Worker 	/* again: if (A == v) goto end; else fall through; */
6172*8b26181fSAndroid Build Coastguard Worker 	again = i;
6173*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6174*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = v;
6175*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.jt = NULL;		/*later*/
6176*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.jf = NULL;		/*update in next stmt*/
6177*8b26181fSAndroid Build Coastguard Worker 	fix5 = i;
6178*8b26181fSAndroid Build Coastguard Worker 	i++;
6179*8b26181fSAndroid Build Coastguard Worker 
6180*8b26181fSAndroid Build Coastguard Worker #ifndef IPPROTO_NONE
6181*8b26181fSAndroid Build Coastguard Worker #define IPPROTO_NONE	59
6182*8b26181fSAndroid Build Coastguard Worker #endif
6183*8b26181fSAndroid Build Coastguard Worker 	/* if (A == IPPROTO_NONE) goto end */
6184*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6185*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.jt = NULL;	/*later*/
6186*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.jf = NULL;	/*update in next stmt*/
6187*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = IPPROTO_NONE;
6188*8b26181fSAndroid Build Coastguard Worker 	s[fix5]->s.jf = s[i];
6189*8b26181fSAndroid Build Coastguard Worker 	fix2 = i;
6190*8b26181fSAndroid Build Coastguard Worker 	i++;
6191*8b26181fSAndroid Build Coastguard Worker 
6192*8b26181fSAndroid Build Coastguard Worker 	if (proto == Q_IPV6) {
6193*8b26181fSAndroid Build Coastguard Worker 		int v6start, v6end, v6advance, j;
6194*8b26181fSAndroid Build Coastguard Worker 
6195*8b26181fSAndroid Build Coastguard Worker 		v6start = i;
6196*8b26181fSAndroid Build Coastguard Worker 		/* if (A == IPPROTO_HOPOPTS) goto v6advance */
6197*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6198*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.jt = NULL;	/*later*/
6199*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.jf = NULL;	/*update in next stmt*/
6200*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = IPPROTO_HOPOPTS;
6201*8b26181fSAndroid Build Coastguard Worker 		s[fix2]->s.jf = s[i];
6202*8b26181fSAndroid Build Coastguard Worker 		i++;
6203*8b26181fSAndroid Build Coastguard Worker 		/* if (A == IPPROTO_DSTOPTS) goto v6advance */
6204*8b26181fSAndroid Build Coastguard Worker 		s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6205*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.jt = NULL;	/*later*/
6206*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.jf = NULL;	/*update in next stmt*/
6207*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = IPPROTO_DSTOPTS;
6208*8b26181fSAndroid Build Coastguard Worker 		i++;
6209*8b26181fSAndroid Build Coastguard Worker 		/* if (A == IPPROTO_ROUTING) goto v6advance */
6210*8b26181fSAndroid Build Coastguard Worker 		s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6211*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.jt = NULL;	/*later*/
6212*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.jf = NULL;	/*update in next stmt*/
6213*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = IPPROTO_ROUTING;
6214*8b26181fSAndroid Build Coastguard Worker 		i++;
6215*8b26181fSAndroid Build Coastguard Worker 		/* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6216*8b26181fSAndroid Build Coastguard Worker 		s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6217*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.jt = NULL;	/*later*/
6218*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.jf = NULL;	/*later*/
6219*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = IPPROTO_FRAGMENT;
6220*8b26181fSAndroid Build Coastguard Worker 		fix3 = i;
6221*8b26181fSAndroid Build Coastguard Worker 		v6end = i;
6222*8b26181fSAndroid Build Coastguard Worker 		i++;
6223*8b26181fSAndroid Build Coastguard Worker 
6224*8b26181fSAndroid Build Coastguard Worker 		/* v6advance: */
6225*8b26181fSAndroid Build Coastguard Worker 		v6advance = i;
6226*8b26181fSAndroid Build Coastguard Worker 
6227*8b26181fSAndroid Build Coastguard Worker 		/*
6228*8b26181fSAndroid Build Coastguard Worker 		 * in short,
6229*8b26181fSAndroid Build Coastguard Worker 		 * A = P[X + packet head];
6230*8b26181fSAndroid Build Coastguard Worker 		 * X = X + (P[X + packet head + 1] + 1) * 8;
6231*8b26181fSAndroid Build Coastguard Worker 		 */
6232*8b26181fSAndroid Build Coastguard Worker 		/* A = P[X + packet head] */
6233*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6234*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6235*8b26181fSAndroid Build Coastguard Worker 		i++;
6236*8b26181fSAndroid Build Coastguard Worker 		/* MEM[reg2] = A */
6237*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_ST);
6238*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = reg2;
6239*8b26181fSAndroid Build Coastguard Worker 		i++;
6240*8b26181fSAndroid Build Coastguard Worker 		/* A = P[X + packet head + 1]; */
6241*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6242*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;
6243*8b26181fSAndroid Build Coastguard Worker 		i++;
6244*8b26181fSAndroid Build Coastguard Worker 		/* A += 1 */
6245*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6246*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = 1;
6247*8b26181fSAndroid Build Coastguard Worker 		i++;
6248*8b26181fSAndroid Build Coastguard Worker 		/* A *= 8 */
6249*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6250*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = 8;
6251*8b26181fSAndroid Build Coastguard Worker 		i++;
6252*8b26181fSAndroid Build Coastguard Worker 		/* A += X */
6253*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
6254*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = 0;
6255*8b26181fSAndroid Build Coastguard Worker 		i++;
6256*8b26181fSAndroid Build Coastguard Worker 		/* X = A; */
6257*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6258*8b26181fSAndroid Build Coastguard Worker 		i++;
6259*8b26181fSAndroid Build Coastguard Worker 		/* A = MEM[reg2] */
6260*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6261*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = reg2;
6262*8b26181fSAndroid Build Coastguard Worker 		i++;
6263*8b26181fSAndroid Build Coastguard Worker 
6264*8b26181fSAndroid Build Coastguard Worker 		/* goto again; (must use BPF_JA for backward jump) */
6265*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6266*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = again - i - 1;
6267*8b26181fSAndroid Build Coastguard Worker 		s[i - 1]->s.jf = s[i];
6268*8b26181fSAndroid Build Coastguard Worker 		i++;
6269*8b26181fSAndroid Build Coastguard Worker 
6270*8b26181fSAndroid Build Coastguard Worker 		/* fixup */
6271*8b26181fSAndroid Build Coastguard Worker 		for (j = v6start; j <= v6end; j++)
6272*8b26181fSAndroid Build Coastguard Worker 			s[j]->s.jt = s[v6advance];
6273*8b26181fSAndroid Build Coastguard Worker 	} else {
6274*8b26181fSAndroid Build Coastguard Worker 		/* nop */
6275*8b26181fSAndroid Build Coastguard Worker 		s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6276*8b26181fSAndroid Build Coastguard Worker 		s[i]->s.k = 0;
6277*8b26181fSAndroid Build Coastguard Worker 		s[fix2]->s.jf = s[i];
6278*8b26181fSAndroid Build Coastguard Worker 		i++;
6279*8b26181fSAndroid Build Coastguard Worker 	}
6280*8b26181fSAndroid Build Coastguard Worker 
6281*8b26181fSAndroid Build Coastguard Worker 	/* ahcheck: */
6282*8b26181fSAndroid Build Coastguard Worker 	ahcheck = i;
6283*8b26181fSAndroid Build Coastguard Worker 	/* if (A == IPPROTO_AH) then fall through; else goto end; */
6284*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6285*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.jt = NULL;	/*later*/
6286*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.jf = NULL;	/*later*/
6287*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = IPPROTO_AH;
6288*8b26181fSAndroid Build Coastguard Worker 	if (fix3)
6289*8b26181fSAndroid Build Coastguard Worker 		s[fix3]->s.jf = s[ahcheck];
6290*8b26181fSAndroid Build Coastguard Worker 	fix4 = i;
6291*8b26181fSAndroid Build Coastguard Worker 	i++;
6292*8b26181fSAndroid Build Coastguard Worker 
6293*8b26181fSAndroid Build Coastguard Worker 	/*
6294*8b26181fSAndroid Build Coastguard Worker 	 * in short,
6295*8b26181fSAndroid Build Coastguard Worker 	 * A = P[X];
6296*8b26181fSAndroid Build Coastguard Worker 	 * X = X + (P[X + 1] + 2) * 4;
6297*8b26181fSAndroid Build Coastguard Worker 	 */
6298*8b26181fSAndroid Build Coastguard Worker 	/* A = X */
6299*8b26181fSAndroid Build Coastguard Worker 	s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6300*8b26181fSAndroid Build Coastguard Worker 	i++;
6301*8b26181fSAndroid Build Coastguard Worker 	/* A = P[X + packet head]; */
6302*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6303*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6304*8b26181fSAndroid Build Coastguard Worker 	i++;
6305*8b26181fSAndroid Build Coastguard Worker 	/* MEM[reg2] = A */
6306*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_ST);
6307*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = reg2;
6308*8b26181fSAndroid Build Coastguard Worker 	i++;
6309*8b26181fSAndroid Build Coastguard Worker 	/* A = X */
6310*8b26181fSAndroid Build Coastguard Worker 	s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6311*8b26181fSAndroid Build Coastguard Worker 	i++;
6312*8b26181fSAndroid Build Coastguard Worker 	/* A += 1 */
6313*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6314*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = 1;
6315*8b26181fSAndroid Build Coastguard Worker 	i++;
6316*8b26181fSAndroid Build Coastguard Worker 	/* X = A */
6317*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6318*8b26181fSAndroid Build Coastguard Worker 	i++;
6319*8b26181fSAndroid Build Coastguard Worker 	/* A = P[X + packet head] */
6320*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6321*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6322*8b26181fSAndroid Build Coastguard Worker 	i++;
6323*8b26181fSAndroid Build Coastguard Worker 	/* A += 2 */
6324*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6325*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = 2;
6326*8b26181fSAndroid Build Coastguard Worker 	i++;
6327*8b26181fSAndroid Build Coastguard Worker 	/* A *= 4 */
6328*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6329*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = 4;
6330*8b26181fSAndroid Build Coastguard Worker 	i++;
6331*8b26181fSAndroid Build Coastguard Worker 	/* X = A; */
6332*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6333*8b26181fSAndroid Build Coastguard Worker 	i++;
6334*8b26181fSAndroid Build Coastguard Worker 	/* A = MEM[reg2] */
6335*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6336*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = reg2;
6337*8b26181fSAndroid Build Coastguard Worker 	i++;
6338*8b26181fSAndroid Build Coastguard Worker 
6339*8b26181fSAndroid Build Coastguard Worker 	/* goto again; (must use BPF_JA for backward jump) */
6340*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6341*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = again - i - 1;
6342*8b26181fSAndroid Build Coastguard Worker 	i++;
6343*8b26181fSAndroid Build Coastguard Worker 
6344*8b26181fSAndroid Build Coastguard Worker 	/* end: nop */
6345*8b26181fSAndroid Build Coastguard Worker 	end = i;
6346*8b26181fSAndroid Build Coastguard Worker 	s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6347*8b26181fSAndroid Build Coastguard Worker 	s[i]->s.k = 0;
6348*8b26181fSAndroid Build Coastguard Worker 	s[fix2]->s.jt = s[end];
6349*8b26181fSAndroid Build Coastguard Worker 	s[fix4]->s.jf = s[end];
6350*8b26181fSAndroid Build Coastguard Worker 	s[fix5]->s.jt = s[end];
6351*8b26181fSAndroid Build Coastguard Worker 	i++;
6352*8b26181fSAndroid Build Coastguard Worker 
6353*8b26181fSAndroid Build Coastguard Worker 	/*
6354*8b26181fSAndroid Build Coastguard Worker 	 * make slist chain
6355*8b26181fSAndroid Build Coastguard Worker 	 */
6356*8b26181fSAndroid Build Coastguard Worker 	max = i;
6357*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; i < max - 1; i++)
6358*8b26181fSAndroid Build Coastguard Worker 		s[i]->next = s[i + 1];
6359*8b26181fSAndroid Build Coastguard Worker 	s[max - 1]->next = NULL;
6360*8b26181fSAndroid Build Coastguard Worker 
6361*8b26181fSAndroid Build Coastguard Worker 	/*
6362*8b26181fSAndroid Build Coastguard Worker 	 * emit final check
6363*8b26181fSAndroid Build Coastguard Worker 	 */
6364*8b26181fSAndroid Build Coastguard Worker 	b = new_block(cstate, JMP(BPF_JEQ));
6365*8b26181fSAndroid Build Coastguard Worker 	b->stmts = s[1];	/*remember, s[0] is dummy*/
6366*8b26181fSAndroid Build Coastguard Worker 	b->s.k = v;
6367*8b26181fSAndroid Build Coastguard Worker 
6368*8b26181fSAndroid Build Coastguard Worker 	free_reg(cstate, reg2);
6369*8b26181fSAndroid Build Coastguard Worker 
6370*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b);
6371*8b26181fSAndroid Build Coastguard Worker 	return b;
6372*8b26181fSAndroid Build Coastguard Worker }
6373*8b26181fSAndroid Build Coastguard Worker #endif /* !defined(NO_PROTOCHAIN) */
6374*8b26181fSAndroid Build Coastguard Worker 
6375*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_check_802_11_data_frame(compiler_state_t * cstate)6376*8b26181fSAndroid Build Coastguard Worker gen_check_802_11_data_frame(compiler_state_t *cstate)
6377*8b26181fSAndroid Build Coastguard Worker {
6378*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
6379*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
6380*8b26181fSAndroid Build Coastguard Worker 
6381*8b26181fSAndroid Build Coastguard Worker 	/*
6382*8b26181fSAndroid Build Coastguard Worker 	 * A data frame has the 0x08 bit (b3) in the frame control field set
6383*8b26181fSAndroid Build Coastguard Worker 	 * and the 0x04 bit (b2) clear.
6384*8b26181fSAndroid Build Coastguard Worker 	 */
6385*8b26181fSAndroid Build Coastguard Worker 	s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6386*8b26181fSAndroid Build Coastguard Worker 	b0 = new_block(cstate, JMP(BPF_JSET));
6387*8b26181fSAndroid Build Coastguard Worker 	b0->s.k = 0x08;
6388*8b26181fSAndroid Build Coastguard Worker 	b0->stmts = s;
6389*8b26181fSAndroid Build Coastguard Worker 
6390*8b26181fSAndroid Build Coastguard Worker 	s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6391*8b26181fSAndroid Build Coastguard Worker 	b1 = new_block(cstate, JMP(BPF_JSET));
6392*8b26181fSAndroid Build Coastguard Worker 	b1->s.k = 0x04;
6393*8b26181fSAndroid Build Coastguard Worker 	b1->stmts = s;
6394*8b26181fSAndroid Build Coastguard Worker 	gen_not(b1);
6395*8b26181fSAndroid Build Coastguard Worker 
6396*8b26181fSAndroid Build Coastguard Worker 	gen_and(b1, b0);
6397*8b26181fSAndroid Build Coastguard Worker 
6398*8b26181fSAndroid Build Coastguard Worker 	return b0;
6399*8b26181fSAndroid Build Coastguard Worker }
6400*8b26181fSAndroid Build Coastguard Worker 
6401*8b26181fSAndroid Build Coastguard Worker /*
6402*8b26181fSAndroid Build Coastguard Worker  * Generate code that checks whether the packet is a packet for protocol
6403*8b26181fSAndroid Build Coastguard Worker  * <proto> and whether the type field in that protocol's header has
6404*8b26181fSAndroid Build Coastguard Worker  * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6405*8b26181fSAndroid Build Coastguard Worker  * IP packet and checks the protocol number in the IP header against <v>.
6406*8b26181fSAndroid Build Coastguard Worker  *
6407*8b26181fSAndroid Build Coastguard Worker  * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6408*8b26181fSAndroid Build Coastguard Worker  * against Q_IP and Q_IPV6.
6409*8b26181fSAndroid Build Coastguard Worker  */
6410*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_proto(compiler_state_t * cstate,bpf_u_int32 v,int proto,int dir)6411*8b26181fSAndroid Build Coastguard Worker gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto, int dir)
6412*8b26181fSAndroid Build Coastguard Worker {
6413*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
6414*8b26181fSAndroid Build Coastguard Worker 	struct block *b2;
6415*8b26181fSAndroid Build Coastguard Worker 
6416*8b26181fSAndroid Build Coastguard Worker 	if (dir != Q_DEFAULT)
6417*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "direction applied to 'proto'");
6418*8b26181fSAndroid Build Coastguard Worker 
6419*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
6420*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
6421*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, v, Q_IP, dir);
6422*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_proto(cstate, v, Q_IPV6, dir);
6423*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
6424*8b26181fSAndroid Build Coastguard Worker 		return b1;
6425*8b26181fSAndroid Build Coastguard Worker 
6426*8b26181fSAndroid Build Coastguard Worker 	case Q_LINK:
6427*8b26181fSAndroid Build Coastguard Worker 		return gen_linktype(cstate, v);
6428*8b26181fSAndroid Build Coastguard Worker 
6429*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
6430*8b26181fSAndroid Build Coastguard Worker 		/*
6431*8b26181fSAndroid Build Coastguard Worker 		 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6432*8b26181fSAndroid Build Coastguard Worker 		 * not LLC encapsulation with LLCSAP_IP.
6433*8b26181fSAndroid Build Coastguard Worker 		 *
6434*8b26181fSAndroid Build Coastguard Worker 		 * For IEEE 802 networks - which includes 802.5 token ring
6435*8b26181fSAndroid Build Coastguard Worker 		 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6436*8b26181fSAndroid Build Coastguard Worker 		 * says that SNAP encapsulation is used, not LLC encapsulation
6437*8b26181fSAndroid Build Coastguard Worker 		 * with LLCSAP_IP.
6438*8b26181fSAndroid Build Coastguard Worker 		 *
6439*8b26181fSAndroid Build Coastguard Worker 		 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6440*8b26181fSAndroid Build Coastguard Worker 		 * RFC 2225 say that SNAP encapsulation is used, not LLC
6441*8b26181fSAndroid Build Coastguard Worker 		 * encapsulation with LLCSAP_IP.
6442*8b26181fSAndroid Build Coastguard Worker 		 *
6443*8b26181fSAndroid Build Coastguard Worker 		 * So we always check for ETHERTYPE_IP.
6444*8b26181fSAndroid Build Coastguard Worker 		 */
6445*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_linktype(cstate, ETHERTYPE_IP);
6446*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, v);
6447*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
6448*8b26181fSAndroid Build Coastguard Worker 		return b1;
6449*8b26181fSAndroid Build Coastguard Worker 
6450*8b26181fSAndroid Build Coastguard Worker 	case Q_ARP:
6451*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "arp does not encapsulate another protocol");
6452*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6453*8b26181fSAndroid Build Coastguard Worker 
6454*8b26181fSAndroid Build Coastguard Worker 	case Q_RARP:
6455*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "rarp does not encapsulate another protocol");
6456*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6457*8b26181fSAndroid Build Coastguard Worker 
6458*8b26181fSAndroid Build Coastguard Worker 	case Q_SCTP:
6459*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'sctp proto' is bogus");
6460*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6461*8b26181fSAndroid Build Coastguard Worker 
6462*8b26181fSAndroid Build Coastguard Worker 	case Q_TCP:
6463*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'tcp proto' is bogus");
6464*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6465*8b26181fSAndroid Build Coastguard Worker 
6466*8b26181fSAndroid Build Coastguard Worker 	case Q_UDP:
6467*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'udp proto' is bogus");
6468*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6469*8b26181fSAndroid Build Coastguard Worker 
6470*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMP:
6471*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'icmp proto' is bogus");
6472*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6473*8b26181fSAndroid Build Coastguard Worker 
6474*8b26181fSAndroid Build Coastguard Worker 	case Q_IGMP:
6475*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'igmp proto' is bogus");
6476*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6477*8b26181fSAndroid Build Coastguard Worker 
6478*8b26181fSAndroid Build Coastguard Worker 	case Q_IGRP:
6479*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'igrp proto' is bogus");
6480*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6481*8b26181fSAndroid Build Coastguard Worker 
6482*8b26181fSAndroid Build Coastguard Worker 	case Q_ATALK:
6483*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "AppleTalk encapsulation is not specifiable");
6484*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6485*8b26181fSAndroid Build Coastguard Worker 
6486*8b26181fSAndroid Build Coastguard Worker 	case Q_DECNET:
6487*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "DECNET encapsulation is not specifiable");
6488*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6489*8b26181fSAndroid Build Coastguard Worker 
6490*8b26181fSAndroid Build Coastguard Worker 	case Q_LAT:
6491*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "LAT does not encapsulate another protocol");
6492*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6493*8b26181fSAndroid Build Coastguard Worker 
6494*8b26181fSAndroid Build Coastguard Worker 	case Q_SCA:
6495*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "SCA does not encapsulate another protocol");
6496*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6497*8b26181fSAndroid Build Coastguard Worker 
6498*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPRC:
6499*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "MOPRC does not encapsulate another protocol");
6500*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6501*8b26181fSAndroid Build Coastguard Worker 
6502*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPDL:
6503*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "MOPDL does not encapsulate another protocol");
6504*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6505*8b26181fSAndroid Build Coastguard Worker 
6506*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
6507*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6508*8b26181fSAndroid Build Coastguard Worker 		/*
6509*8b26181fSAndroid Build Coastguard Worker 		 * Also check for a fragment header before the final
6510*8b26181fSAndroid Build Coastguard Worker 		 * header.
6511*8b26181fSAndroid Build Coastguard Worker 		 */
6512*8b26181fSAndroid Build Coastguard Worker 		b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
6513*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v);
6514*8b26181fSAndroid Build Coastguard Worker 		gen_and(b2, b1);
6515*8b26181fSAndroid Build Coastguard Worker 		b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, v);
6516*8b26181fSAndroid Build Coastguard Worker 		gen_or(b2, b1);
6517*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
6518*8b26181fSAndroid Build Coastguard Worker 		return b1;
6519*8b26181fSAndroid Build Coastguard Worker 
6520*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMPV6:
6521*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'icmp6 proto' is bogus");
6522*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6523*8b26181fSAndroid Build Coastguard Worker 
6524*8b26181fSAndroid Build Coastguard Worker 	case Q_AH:
6525*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ah proto' is bogus");
6526*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6527*8b26181fSAndroid Build Coastguard Worker 
6528*8b26181fSAndroid Build Coastguard Worker 	case Q_ESP:
6529*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'esp proto' is bogus");
6530*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6531*8b26181fSAndroid Build Coastguard Worker 
6532*8b26181fSAndroid Build Coastguard Worker 	case Q_PIM:
6533*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'pim proto' is bogus");
6534*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6535*8b26181fSAndroid Build Coastguard Worker 
6536*8b26181fSAndroid Build Coastguard Worker 	case Q_VRRP:
6537*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'vrrp proto' is bogus");
6538*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6539*8b26181fSAndroid Build Coastguard Worker 
6540*8b26181fSAndroid Build Coastguard Worker 	case Q_AARP:
6541*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'aarp proto' is bogus");
6542*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6543*8b26181fSAndroid Build Coastguard Worker 
6544*8b26181fSAndroid Build Coastguard Worker 	case Q_ISO:
6545*8b26181fSAndroid Build Coastguard Worker 		switch (cstate->linktype) {
6546*8b26181fSAndroid Build Coastguard Worker 
6547*8b26181fSAndroid Build Coastguard Worker 		case DLT_FRELAY:
6548*8b26181fSAndroid Build Coastguard Worker 			/*
6549*8b26181fSAndroid Build Coastguard Worker 			 * Frame Relay packets typically have an OSI
6550*8b26181fSAndroid Build Coastguard Worker 			 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6551*8b26181fSAndroid Build Coastguard Worker 			 * generates code to check for all the OSI
6552*8b26181fSAndroid Build Coastguard Worker 			 * NLPIDs, so calling it and then adding a check
6553*8b26181fSAndroid Build Coastguard Worker 			 * for the particular NLPID for which we're
6554*8b26181fSAndroid Build Coastguard Worker 			 * looking is bogus, as we can just check for
6555*8b26181fSAndroid Build Coastguard Worker 			 * the NLPID.
6556*8b26181fSAndroid Build Coastguard Worker 			 *
6557*8b26181fSAndroid Build Coastguard Worker 			 * What we check for is the NLPID and a frame
6558*8b26181fSAndroid Build Coastguard Worker 			 * control field value of UI, i.e. 0x03 followed
6559*8b26181fSAndroid Build Coastguard Worker 			 * by the NLPID.
6560*8b26181fSAndroid Build Coastguard Worker 			 *
6561*8b26181fSAndroid Build Coastguard Worker 			 * XXX - assumes a 2-byte Frame Relay header with
6562*8b26181fSAndroid Build Coastguard Worker 			 * DLCI and flags.  What if the address is longer?
6563*8b26181fSAndroid Build Coastguard Worker 			 *
6564*8b26181fSAndroid Build Coastguard Worker 			 * XXX - what about SNAP-encapsulated frames?
6565*8b26181fSAndroid Build Coastguard Worker 			 */
6566*8b26181fSAndroid Build Coastguard Worker 			return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
6567*8b26181fSAndroid Build Coastguard Worker 			/*NOTREACHED*/
6568*8b26181fSAndroid Build Coastguard Worker 
6569*8b26181fSAndroid Build Coastguard Worker 		case DLT_C_HDLC:
6570*8b26181fSAndroid Build Coastguard Worker 		case DLT_HDLC:
6571*8b26181fSAndroid Build Coastguard Worker 			/*
6572*8b26181fSAndroid Build Coastguard Worker 			 * Cisco uses an Ethertype lookalike - for OSI,
6573*8b26181fSAndroid Build Coastguard Worker 			 * it's 0xfefe.
6574*8b26181fSAndroid Build Coastguard Worker 			 */
6575*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
6576*8b26181fSAndroid Build Coastguard Worker 			/* OSI in C-HDLC is stuffed with a fudge byte */
6577*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v);
6578*8b26181fSAndroid Build Coastguard Worker 			gen_and(b0, b1);
6579*8b26181fSAndroid Build Coastguard Worker 			return b1;
6580*8b26181fSAndroid Build Coastguard Worker 
6581*8b26181fSAndroid Build Coastguard Worker 		default:
6582*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_linktype(cstate, LLCSAP_ISONS);
6583*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v);
6584*8b26181fSAndroid Build Coastguard Worker 			gen_and(b0, b1);
6585*8b26181fSAndroid Build Coastguard Worker 			return b1;
6586*8b26181fSAndroid Build Coastguard Worker 		}
6587*8b26181fSAndroid Build Coastguard Worker 
6588*8b26181fSAndroid Build Coastguard Worker 	case Q_ESIS:
6589*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'esis proto' is bogus");
6590*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6591*8b26181fSAndroid Build Coastguard Worker 
6592*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS:
6593*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
6594*8b26181fSAndroid Build Coastguard Worker 		/*
6595*8b26181fSAndroid Build Coastguard Worker 		 * 4 is the offset of the PDU type relative to the IS-IS
6596*8b26181fSAndroid Build Coastguard Worker 		 * header.
6597*8b26181fSAndroid Build Coastguard Worker 		 */
6598*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, v);
6599*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
6600*8b26181fSAndroid Build Coastguard Worker 		return b1;
6601*8b26181fSAndroid Build Coastguard Worker 
6602*8b26181fSAndroid Build Coastguard Worker 	case Q_CLNP:
6603*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'clnp proto' is not supported");
6604*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6605*8b26181fSAndroid Build Coastguard Worker 
6606*8b26181fSAndroid Build Coastguard Worker 	case Q_STP:
6607*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'stp proto' is bogus");
6608*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6609*8b26181fSAndroid Build Coastguard Worker 
6610*8b26181fSAndroid Build Coastguard Worker 	case Q_IPX:
6611*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ipx proto' is bogus");
6612*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6613*8b26181fSAndroid Build Coastguard Worker 
6614*8b26181fSAndroid Build Coastguard Worker 	case Q_NETBEUI:
6615*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'netbeui proto' is bogus");
6616*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6617*8b26181fSAndroid Build Coastguard Worker 
6618*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_L1:
6619*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'l1 proto' is bogus");
6620*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6621*8b26181fSAndroid Build Coastguard Worker 
6622*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_L2:
6623*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'l2 proto' is bogus");
6624*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6625*8b26181fSAndroid Build Coastguard Worker 
6626*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_IIH:
6627*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'iih proto' is bogus");
6628*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6629*8b26181fSAndroid Build Coastguard Worker 
6630*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_SNP:
6631*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'snp proto' is bogus");
6632*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6633*8b26181fSAndroid Build Coastguard Worker 
6634*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_CSNP:
6635*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'csnp proto' is bogus");
6636*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6637*8b26181fSAndroid Build Coastguard Worker 
6638*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_PSNP:
6639*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'psnp proto' is bogus");
6640*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6641*8b26181fSAndroid Build Coastguard Worker 
6642*8b26181fSAndroid Build Coastguard Worker 	case Q_ISIS_LSP:
6643*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'lsp proto' is bogus");
6644*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6645*8b26181fSAndroid Build Coastguard Worker 
6646*8b26181fSAndroid Build Coastguard Worker 	case Q_RADIO:
6647*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'radio proto' is bogus");
6648*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6649*8b26181fSAndroid Build Coastguard Worker 
6650*8b26181fSAndroid Build Coastguard Worker 	case Q_CARP:
6651*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'carp proto' is bogus");
6652*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6653*8b26181fSAndroid Build Coastguard Worker 
6654*8b26181fSAndroid Build Coastguard Worker 	default:
6655*8b26181fSAndroid Build Coastguard Worker 		abort();
6656*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6657*8b26181fSAndroid Build Coastguard Worker 	}
6658*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
6659*8b26181fSAndroid Build Coastguard Worker }
6660*8b26181fSAndroid Build Coastguard Worker 
6661*8b26181fSAndroid Build Coastguard Worker struct block *
gen_scode(compiler_state_t * cstate,const char * name,struct qual q)6662*8b26181fSAndroid Build Coastguard Worker gen_scode(compiler_state_t *cstate, const char *name, struct qual q)
6663*8b26181fSAndroid Build Coastguard Worker {
6664*8b26181fSAndroid Build Coastguard Worker 	int proto = q.proto;
6665*8b26181fSAndroid Build Coastguard Worker 	int dir = q.dir;
6666*8b26181fSAndroid Build Coastguard Worker 	int tproto;
6667*8b26181fSAndroid Build Coastguard Worker 	u_char *eaddr;
6668*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 mask, addr;
6669*8b26181fSAndroid Build Coastguard Worker 	struct addrinfo *res, *res0;
6670*8b26181fSAndroid Build Coastguard Worker 	struct sockaddr_in *sin4;
6671*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
6672*8b26181fSAndroid Build Coastguard Worker 	int tproto6;
6673*8b26181fSAndroid Build Coastguard Worker 	struct sockaddr_in6 *sin6;
6674*8b26181fSAndroid Build Coastguard Worker 	struct in6_addr mask128;
6675*8b26181fSAndroid Build Coastguard Worker #endif /*INET6*/
6676*8b26181fSAndroid Build Coastguard Worker 	struct block *b, *tmp;
6677*8b26181fSAndroid Build Coastguard Worker 	int port, real_proto;
6678*8b26181fSAndroid Build Coastguard Worker 	int port1, port2;
6679*8b26181fSAndroid Build Coastguard Worker 
6680*8b26181fSAndroid Build Coastguard Worker 	/*
6681*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
6682*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
6683*8b26181fSAndroid Build Coastguard Worker 	 */
6684*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
6685*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
6686*8b26181fSAndroid Build Coastguard Worker 
6687*8b26181fSAndroid Build Coastguard Worker 	switch (q.addr) {
6688*8b26181fSAndroid Build Coastguard Worker 
6689*8b26181fSAndroid Build Coastguard Worker 	case Q_NET:
6690*8b26181fSAndroid Build Coastguard Worker 		addr = pcap_nametonetaddr(name);
6691*8b26181fSAndroid Build Coastguard Worker 		if (addr == 0)
6692*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown network '%s'", name);
6693*8b26181fSAndroid Build Coastguard Worker 		/* Left justify network addr and calculate its network mask */
6694*8b26181fSAndroid Build Coastguard Worker 		mask = 0xffffffff;
6695*8b26181fSAndroid Build Coastguard Worker 		while (addr && (addr & 0xff000000) == 0) {
6696*8b26181fSAndroid Build Coastguard Worker 			addr <<= 8;
6697*8b26181fSAndroid Build Coastguard Worker 			mask <<= 8;
6698*8b26181fSAndroid Build Coastguard Worker 		}
6699*8b26181fSAndroid Build Coastguard Worker 		return gen_host(cstate, addr, mask, proto, dir, q.addr);
6700*8b26181fSAndroid Build Coastguard Worker 
6701*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
6702*8b26181fSAndroid Build Coastguard Worker 	case Q_HOST:
6703*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_LINK) {
6704*8b26181fSAndroid Build Coastguard Worker 			switch (cstate->linktype) {
6705*8b26181fSAndroid Build Coastguard Worker 
6706*8b26181fSAndroid Build Coastguard Worker 			case DLT_EN10MB:
6707*8b26181fSAndroid Build Coastguard Worker 			case DLT_NETANALYZER:
6708*8b26181fSAndroid Build Coastguard Worker 			case DLT_NETANALYZER_TRANSPARENT:
6709*8b26181fSAndroid Build Coastguard Worker 				eaddr = pcap_ether_hostton(name);
6710*8b26181fSAndroid Build Coastguard Worker 				if (eaddr == NULL)
6711*8b26181fSAndroid Build Coastguard Worker 					bpf_error(cstate,
6712*8b26181fSAndroid Build Coastguard Worker 					    "unknown ether host '%s'", name);
6713*8b26181fSAndroid Build Coastguard Worker 				tmp = gen_prevlinkhdr_check(cstate);
6714*8b26181fSAndroid Build Coastguard Worker 				b = gen_ehostop(cstate, eaddr, dir);
6715*8b26181fSAndroid Build Coastguard Worker 				if (tmp != NULL)
6716*8b26181fSAndroid Build Coastguard Worker 					gen_and(tmp, b);
6717*8b26181fSAndroid Build Coastguard Worker 				free(eaddr);
6718*8b26181fSAndroid Build Coastguard Worker 				return b;
6719*8b26181fSAndroid Build Coastguard Worker 
6720*8b26181fSAndroid Build Coastguard Worker 			case DLT_FDDI:
6721*8b26181fSAndroid Build Coastguard Worker 				eaddr = pcap_ether_hostton(name);
6722*8b26181fSAndroid Build Coastguard Worker 				if (eaddr == NULL)
6723*8b26181fSAndroid Build Coastguard Worker 					bpf_error(cstate,
6724*8b26181fSAndroid Build Coastguard Worker 					    "unknown FDDI host '%s'", name);
6725*8b26181fSAndroid Build Coastguard Worker 				b = gen_fhostop(cstate, eaddr, dir);
6726*8b26181fSAndroid Build Coastguard Worker 				free(eaddr);
6727*8b26181fSAndroid Build Coastguard Worker 				return b;
6728*8b26181fSAndroid Build Coastguard Worker 
6729*8b26181fSAndroid Build Coastguard Worker 			case DLT_IEEE802:
6730*8b26181fSAndroid Build Coastguard Worker 				eaddr = pcap_ether_hostton(name);
6731*8b26181fSAndroid Build Coastguard Worker 				if (eaddr == NULL)
6732*8b26181fSAndroid Build Coastguard Worker 					bpf_error(cstate,
6733*8b26181fSAndroid Build Coastguard Worker 					    "unknown token ring host '%s'", name);
6734*8b26181fSAndroid Build Coastguard Worker 				b = gen_thostop(cstate, eaddr, dir);
6735*8b26181fSAndroid Build Coastguard Worker 				free(eaddr);
6736*8b26181fSAndroid Build Coastguard Worker 				return b;
6737*8b26181fSAndroid Build Coastguard Worker 
6738*8b26181fSAndroid Build Coastguard Worker 			case DLT_IEEE802_11:
6739*8b26181fSAndroid Build Coastguard Worker 			case DLT_PRISM_HEADER:
6740*8b26181fSAndroid Build Coastguard Worker 			case DLT_IEEE802_11_RADIO_AVS:
6741*8b26181fSAndroid Build Coastguard Worker 			case DLT_IEEE802_11_RADIO:
6742*8b26181fSAndroid Build Coastguard Worker 			case DLT_PPI:
6743*8b26181fSAndroid Build Coastguard Worker 				eaddr = pcap_ether_hostton(name);
6744*8b26181fSAndroid Build Coastguard Worker 				if (eaddr == NULL)
6745*8b26181fSAndroid Build Coastguard Worker 					bpf_error(cstate,
6746*8b26181fSAndroid Build Coastguard Worker 					    "unknown 802.11 host '%s'", name);
6747*8b26181fSAndroid Build Coastguard Worker 				b = gen_wlanhostop(cstate, eaddr, dir);
6748*8b26181fSAndroid Build Coastguard Worker 				free(eaddr);
6749*8b26181fSAndroid Build Coastguard Worker 				return b;
6750*8b26181fSAndroid Build Coastguard Worker 
6751*8b26181fSAndroid Build Coastguard Worker 			case DLT_IP_OVER_FC:
6752*8b26181fSAndroid Build Coastguard Worker 				eaddr = pcap_ether_hostton(name);
6753*8b26181fSAndroid Build Coastguard Worker 				if (eaddr == NULL)
6754*8b26181fSAndroid Build Coastguard Worker 					bpf_error(cstate,
6755*8b26181fSAndroid Build Coastguard Worker 					    "unknown Fibre Channel host '%s'", name);
6756*8b26181fSAndroid Build Coastguard Worker 				b = gen_ipfchostop(cstate, eaddr, dir);
6757*8b26181fSAndroid Build Coastguard Worker 				free(eaddr);
6758*8b26181fSAndroid Build Coastguard Worker 				return b;
6759*8b26181fSAndroid Build Coastguard Worker 			}
6760*8b26181fSAndroid Build Coastguard Worker 
6761*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6762*8b26181fSAndroid Build Coastguard Worker 		} else if (proto == Q_DECNET) {
6763*8b26181fSAndroid Build Coastguard Worker 			unsigned short dn_addr;
6764*8b26181fSAndroid Build Coastguard Worker 
6765*8b26181fSAndroid Build Coastguard Worker 			if (!__pcap_nametodnaddr(name, &dn_addr)) {
6766*8b26181fSAndroid Build Coastguard Worker #ifdef	DECNETLIB
6767*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "unknown decnet host name '%s'\n", name);
6768*8b26181fSAndroid Build Coastguard Worker #else
6769*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n",
6770*8b26181fSAndroid Build Coastguard Worker 					name);
6771*8b26181fSAndroid Build Coastguard Worker #endif
6772*8b26181fSAndroid Build Coastguard Worker 			}
6773*8b26181fSAndroid Build Coastguard Worker 			/*
6774*8b26181fSAndroid Build Coastguard Worker 			 * I don't think DECNET hosts can be multihomed, so
6775*8b26181fSAndroid Build Coastguard Worker 			 * there is no need to build up a list of addresses
6776*8b26181fSAndroid Build Coastguard Worker 			 */
6777*8b26181fSAndroid Build Coastguard Worker 			return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr));
6778*8b26181fSAndroid Build Coastguard Worker 		} else {
6779*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
6780*8b26181fSAndroid Build Coastguard Worker 			memset(&mask128, 0xff, sizeof(mask128));
6781*8b26181fSAndroid Build Coastguard Worker #endif
6782*8b26181fSAndroid Build Coastguard Worker 			res0 = res = pcap_nametoaddrinfo(name);
6783*8b26181fSAndroid Build Coastguard Worker 			if (res == NULL)
6784*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "unknown host '%s'", name);
6785*8b26181fSAndroid Build Coastguard Worker 			cstate->ai = res;
6786*8b26181fSAndroid Build Coastguard Worker 			b = tmp = NULL;
6787*8b26181fSAndroid Build Coastguard Worker 			tproto = proto;
6788*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
6789*8b26181fSAndroid Build Coastguard Worker 			tproto6 = proto;
6790*8b26181fSAndroid Build Coastguard Worker #endif
6791*8b26181fSAndroid Build Coastguard Worker 			if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&
6792*8b26181fSAndroid Build Coastguard Worker 			    tproto == Q_DEFAULT) {
6793*8b26181fSAndroid Build Coastguard Worker 				tproto = Q_IP;
6794*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
6795*8b26181fSAndroid Build Coastguard Worker 				tproto6 = Q_IPV6;
6796*8b26181fSAndroid Build Coastguard Worker #endif
6797*8b26181fSAndroid Build Coastguard Worker 			}
6798*8b26181fSAndroid Build Coastguard Worker 			for (res = res0; res; res = res->ai_next) {
6799*8b26181fSAndroid Build Coastguard Worker 				switch (res->ai_family) {
6800*8b26181fSAndroid Build Coastguard Worker 				case AF_INET:
6801*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
6802*8b26181fSAndroid Build Coastguard Worker 					if (tproto == Q_IPV6)
6803*8b26181fSAndroid Build Coastguard Worker 						continue;
6804*8b26181fSAndroid Build Coastguard Worker #endif
6805*8b26181fSAndroid Build Coastguard Worker 
6806*8b26181fSAndroid Build Coastguard Worker 					sin4 = (struct sockaddr_in *)
6807*8b26181fSAndroid Build Coastguard Worker 						res->ai_addr;
6808*8b26181fSAndroid Build Coastguard Worker 					tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),
6809*8b26181fSAndroid Build Coastguard Worker 						0xffffffff, tproto, dir, q.addr);
6810*8b26181fSAndroid Build Coastguard Worker 					break;
6811*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
6812*8b26181fSAndroid Build Coastguard Worker 				case AF_INET6:
6813*8b26181fSAndroid Build Coastguard Worker 					if (tproto6 == Q_IP)
6814*8b26181fSAndroid Build Coastguard Worker 						continue;
6815*8b26181fSAndroid Build Coastguard Worker 
6816*8b26181fSAndroid Build Coastguard Worker 					sin6 = (struct sockaddr_in6 *)
6817*8b26181fSAndroid Build Coastguard Worker 						res->ai_addr;
6818*8b26181fSAndroid Build Coastguard Worker 					tmp = gen_host6(cstate, &sin6->sin6_addr,
6819*8b26181fSAndroid Build Coastguard Worker 						&mask128, tproto6, dir, q.addr);
6820*8b26181fSAndroid Build Coastguard Worker 					break;
6821*8b26181fSAndroid Build Coastguard Worker #endif
6822*8b26181fSAndroid Build Coastguard Worker 				default:
6823*8b26181fSAndroid Build Coastguard Worker 					continue;
6824*8b26181fSAndroid Build Coastguard Worker 				}
6825*8b26181fSAndroid Build Coastguard Worker 				if (b)
6826*8b26181fSAndroid Build Coastguard Worker 					gen_or(b, tmp);
6827*8b26181fSAndroid Build Coastguard Worker 				b = tmp;
6828*8b26181fSAndroid Build Coastguard Worker 			}
6829*8b26181fSAndroid Build Coastguard Worker 			cstate->ai = NULL;
6830*8b26181fSAndroid Build Coastguard Worker 			freeaddrinfo(res0);
6831*8b26181fSAndroid Build Coastguard Worker 			if (b == NULL) {
6832*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "unknown host '%s'%s", name,
6833*8b26181fSAndroid Build Coastguard Worker 				    (proto == Q_DEFAULT)
6834*8b26181fSAndroid Build Coastguard Worker 					? ""
6835*8b26181fSAndroid Build Coastguard Worker 					: " for specified address family");
6836*8b26181fSAndroid Build Coastguard Worker 			}
6837*8b26181fSAndroid Build Coastguard Worker 			return b;
6838*8b26181fSAndroid Build Coastguard Worker 		}
6839*8b26181fSAndroid Build Coastguard Worker 
6840*8b26181fSAndroid Build Coastguard Worker 	case Q_PORT:
6841*8b26181fSAndroid Build Coastguard Worker 		if (proto != Q_DEFAULT &&
6842*8b26181fSAndroid Build Coastguard Worker 		    proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6843*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal qualifier of 'port'");
6844*8b26181fSAndroid Build Coastguard Worker 		if (pcap_nametoport(name, &port, &real_proto) == 0)
6845*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown port '%s'", name);
6846*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_UDP) {
6847*8b26181fSAndroid Build Coastguard Worker 			if (real_proto == IPPROTO_TCP)
6848*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port '%s' is tcp", name);
6849*8b26181fSAndroid Build Coastguard Worker 			else if (real_proto == IPPROTO_SCTP)
6850*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port '%s' is sctp", name);
6851*8b26181fSAndroid Build Coastguard Worker 			else
6852*8b26181fSAndroid Build Coastguard Worker 				/* override PROTO_UNDEF */
6853*8b26181fSAndroid Build Coastguard Worker 				real_proto = IPPROTO_UDP;
6854*8b26181fSAndroid Build Coastguard Worker 		}
6855*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_TCP) {
6856*8b26181fSAndroid Build Coastguard Worker 			if (real_proto == IPPROTO_UDP)
6857*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port '%s' is udp", name);
6858*8b26181fSAndroid Build Coastguard Worker 
6859*8b26181fSAndroid Build Coastguard Worker 			else if (real_proto == IPPROTO_SCTP)
6860*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port '%s' is sctp", name);
6861*8b26181fSAndroid Build Coastguard Worker 			else
6862*8b26181fSAndroid Build Coastguard Worker 				/* override PROTO_UNDEF */
6863*8b26181fSAndroid Build Coastguard Worker 				real_proto = IPPROTO_TCP;
6864*8b26181fSAndroid Build Coastguard Worker 		}
6865*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_SCTP) {
6866*8b26181fSAndroid Build Coastguard Worker 			if (real_proto == IPPROTO_UDP)
6867*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port '%s' is udp", name);
6868*8b26181fSAndroid Build Coastguard Worker 
6869*8b26181fSAndroid Build Coastguard Worker 			else if (real_proto == IPPROTO_TCP)
6870*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port '%s' is tcp", name);
6871*8b26181fSAndroid Build Coastguard Worker 			else
6872*8b26181fSAndroid Build Coastguard Worker 				/* override PROTO_UNDEF */
6873*8b26181fSAndroid Build Coastguard Worker 				real_proto = IPPROTO_SCTP;
6874*8b26181fSAndroid Build Coastguard Worker 		}
6875*8b26181fSAndroid Build Coastguard Worker 		if (port < 0)
6876*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal port number %d < 0", port);
6877*8b26181fSAndroid Build Coastguard Worker 		if (port > 65535)
6878*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal port number %d > 65535", port);
6879*8b26181fSAndroid Build Coastguard Worker 		b = gen_port(cstate, port, real_proto, dir);
6880*8b26181fSAndroid Build Coastguard Worker 		gen_or(gen_port6(cstate, port, real_proto, dir), b);
6881*8b26181fSAndroid Build Coastguard Worker 		return b;
6882*8b26181fSAndroid Build Coastguard Worker 
6883*8b26181fSAndroid Build Coastguard Worker 	case Q_PORTRANGE:
6884*8b26181fSAndroid Build Coastguard Worker 		if (proto != Q_DEFAULT &&
6885*8b26181fSAndroid Build Coastguard Worker 		    proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6886*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal qualifier of 'portrange'");
6887*8b26181fSAndroid Build Coastguard Worker 		if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6888*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown port in range '%s'", name);
6889*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_UDP) {
6890*8b26181fSAndroid Build Coastguard Worker 			if (real_proto == IPPROTO_TCP)
6891*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port in range '%s' is tcp", name);
6892*8b26181fSAndroid Build Coastguard Worker 			else if (real_proto == IPPROTO_SCTP)
6893*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port in range '%s' is sctp", name);
6894*8b26181fSAndroid Build Coastguard Worker 			else
6895*8b26181fSAndroid Build Coastguard Worker 				/* override PROTO_UNDEF */
6896*8b26181fSAndroid Build Coastguard Worker 				real_proto = IPPROTO_UDP;
6897*8b26181fSAndroid Build Coastguard Worker 		}
6898*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_TCP) {
6899*8b26181fSAndroid Build Coastguard Worker 			if (real_proto == IPPROTO_UDP)
6900*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port in range '%s' is udp", name);
6901*8b26181fSAndroid Build Coastguard Worker 			else if (real_proto == IPPROTO_SCTP)
6902*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port in range '%s' is sctp", name);
6903*8b26181fSAndroid Build Coastguard Worker 			else
6904*8b26181fSAndroid Build Coastguard Worker 				/* override PROTO_UNDEF */
6905*8b26181fSAndroid Build Coastguard Worker 				real_proto = IPPROTO_TCP;
6906*8b26181fSAndroid Build Coastguard Worker 		}
6907*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_SCTP) {
6908*8b26181fSAndroid Build Coastguard Worker 			if (real_proto == IPPROTO_UDP)
6909*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port in range '%s' is udp", name);
6910*8b26181fSAndroid Build Coastguard Worker 			else if (real_proto == IPPROTO_TCP)
6911*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "port in range '%s' is tcp", name);
6912*8b26181fSAndroid Build Coastguard Worker 			else
6913*8b26181fSAndroid Build Coastguard Worker 				/* override PROTO_UNDEF */
6914*8b26181fSAndroid Build Coastguard Worker 				real_proto = IPPROTO_SCTP;
6915*8b26181fSAndroid Build Coastguard Worker 		}
6916*8b26181fSAndroid Build Coastguard Worker 		if (port1 < 0)
6917*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal port number %d < 0", port1);
6918*8b26181fSAndroid Build Coastguard Worker 		if (port1 > 65535)
6919*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal port number %d > 65535", port1);
6920*8b26181fSAndroid Build Coastguard Worker 		if (port2 < 0)
6921*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal port number %d < 0", port2);
6922*8b26181fSAndroid Build Coastguard Worker 		if (port2 > 65535)
6923*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal port number %d > 65535", port2);
6924*8b26181fSAndroid Build Coastguard Worker 
6925*8b26181fSAndroid Build Coastguard Worker 		b = gen_portrange(cstate, port1, port2, real_proto, dir);
6926*8b26181fSAndroid Build Coastguard Worker 		gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);
6927*8b26181fSAndroid Build Coastguard Worker 		return b;
6928*8b26181fSAndroid Build Coastguard Worker 
6929*8b26181fSAndroid Build Coastguard Worker 	case Q_GATEWAY:
6930*8b26181fSAndroid Build Coastguard Worker #ifndef INET6
6931*8b26181fSAndroid Build Coastguard Worker 		eaddr = pcap_ether_hostton(name);
6932*8b26181fSAndroid Build Coastguard Worker 		if (eaddr == NULL)
6933*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown ether host: %s", name);
6934*8b26181fSAndroid Build Coastguard Worker 
6935*8b26181fSAndroid Build Coastguard Worker 		res = pcap_nametoaddrinfo(name);
6936*8b26181fSAndroid Build Coastguard Worker 		cstate->ai = res;
6937*8b26181fSAndroid Build Coastguard Worker 		if (res == NULL)
6938*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown host '%s'", name);
6939*8b26181fSAndroid Build Coastguard Worker 		b = gen_gateway(cstate, eaddr, res, proto, dir);
6940*8b26181fSAndroid Build Coastguard Worker 		cstate->ai = NULL;
6941*8b26181fSAndroid Build Coastguard Worker 		freeaddrinfo(res);
6942*8b26181fSAndroid Build Coastguard Worker 		if (b == NULL)
6943*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown host '%s'", name);
6944*8b26181fSAndroid Build Coastguard Worker 		return b;
6945*8b26181fSAndroid Build Coastguard Worker #else
6946*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'gateway' not supported in this configuration");
6947*8b26181fSAndroid Build Coastguard Worker #endif /*INET6*/
6948*8b26181fSAndroid Build Coastguard Worker 
6949*8b26181fSAndroid Build Coastguard Worker 	case Q_PROTO:
6950*8b26181fSAndroid Build Coastguard Worker 		real_proto = lookup_proto(cstate, name, proto);
6951*8b26181fSAndroid Build Coastguard Worker 		if (real_proto >= 0)
6952*8b26181fSAndroid Build Coastguard Worker 			return gen_proto(cstate, real_proto, proto, dir);
6953*8b26181fSAndroid Build Coastguard Worker 		else
6954*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown protocol: %s", name);
6955*8b26181fSAndroid Build Coastguard Worker 
6956*8b26181fSAndroid Build Coastguard Worker #if !defined(NO_PROTOCHAIN)
6957*8b26181fSAndroid Build Coastguard Worker 	case Q_PROTOCHAIN:
6958*8b26181fSAndroid Build Coastguard Worker 		real_proto = lookup_proto(cstate, name, proto);
6959*8b26181fSAndroid Build Coastguard Worker 		if (real_proto >= 0)
6960*8b26181fSAndroid Build Coastguard Worker 			return gen_protochain(cstate, real_proto, proto);
6961*8b26181fSAndroid Build Coastguard Worker 		else
6962*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "unknown protocol: %s", name);
6963*8b26181fSAndroid Build Coastguard Worker #endif /* !defined(NO_PROTOCHAIN) */
6964*8b26181fSAndroid Build Coastguard Worker 
6965*8b26181fSAndroid Build Coastguard Worker 	case Q_UNDEF:
6966*8b26181fSAndroid Build Coastguard Worker 		syntax(cstate);
6967*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
6968*8b26181fSAndroid Build Coastguard Worker 	}
6969*8b26181fSAndroid Build Coastguard Worker 	abort();
6970*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
6971*8b26181fSAndroid Build Coastguard Worker }
6972*8b26181fSAndroid Build Coastguard Worker 
6973*8b26181fSAndroid Build Coastguard Worker struct block *
gen_mcode(compiler_state_t * cstate,const char * s1,const char * s2,bpf_u_int32 masklen,struct qual q)6974*8b26181fSAndroid Build Coastguard Worker gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
6975*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 masklen, struct qual q)
6976*8b26181fSAndroid Build Coastguard Worker {
6977*8b26181fSAndroid Build Coastguard Worker 	register int nlen, mlen;
6978*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 n, m;
6979*8b26181fSAndroid Build Coastguard Worker 
6980*8b26181fSAndroid Build Coastguard Worker 	/*
6981*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
6982*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
6983*8b26181fSAndroid Build Coastguard Worker 	 */
6984*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
6985*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
6986*8b26181fSAndroid Build Coastguard Worker 
6987*8b26181fSAndroid Build Coastguard Worker 	nlen = __pcap_atoin(s1, &n);
6988*8b26181fSAndroid Build Coastguard Worker 	if (nlen < 0)
6989*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "invalid IPv4 address '%s'", s1);
6990*8b26181fSAndroid Build Coastguard Worker 	/* Promote short ipaddr */
6991*8b26181fSAndroid Build Coastguard Worker 	n <<= 32 - nlen;
6992*8b26181fSAndroid Build Coastguard Worker 
6993*8b26181fSAndroid Build Coastguard Worker 	if (s2 != NULL) {
6994*8b26181fSAndroid Build Coastguard Worker 		mlen = __pcap_atoin(s2, &m);
6995*8b26181fSAndroid Build Coastguard Worker 		if (mlen < 0)
6996*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "invalid IPv4 address '%s'", s2);
6997*8b26181fSAndroid Build Coastguard Worker 		/* Promote short ipaddr */
6998*8b26181fSAndroid Build Coastguard Worker 		m <<= 32 - mlen;
6999*8b26181fSAndroid Build Coastguard Worker 		if ((n & ~m) != 0)
7000*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "non-network bits set in \"%s mask %s\"",
7001*8b26181fSAndroid Build Coastguard Worker 			    s1, s2);
7002*8b26181fSAndroid Build Coastguard Worker 	} else {
7003*8b26181fSAndroid Build Coastguard Worker 		/* Convert mask len to mask */
7004*8b26181fSAndroid Build Coastguard Worker 		if (masklen > 32)
7005*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "mask length must be <= 32");
7006*8b26181fSAndroid Build Coastguard Worker 		if (masklen == 0) {
7007*8b26181fSAndroid Build Coastguard Worker 			/*
7008*8b26181fSAndroid Build Coastguard Worker 			 * X << 32 is not guaranteed by C to be 0; it's
7009*8b26181fSAndroid Build Coastguard Worker 			 * undefined.
7010*8b26181fSAndroid Build Coastguard Worker 			 */
7011*8b26181fSAndroid Build Coastguard Worker 			m = 0;
7012*8b26181fSAndroid Build Coastguard Worker 		} else
7013*8b26181fSAndroid Build Coastguard Worker 			m = 0xffffffff << (32 - masklen);
7014*8b26181fSAndroid Build Coastguard Worker 		if ((n & ~m) != 0)
7015*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "non-network bits set in \"%s/%d\"",
7016*8b26181fSAndroid Build Coastguard Worker 			    s1, masklen);
7017*8b26181fSAndroid Build Coastguard Worker 	}
7018*8b26181fSAndroid Build Coastguard Worker 
7019*8b26181fSAndroid Build Coastguard Worker 	switch (q.addr) {
7020*8b26181fSAndroid Build Coastguard Worker 
7021*8b26181fSAndroid Build Coastguard Worker 	case Q_NET:
7022*8b26181fSAndroid Build Coastguard Worker 		return gen_host(cstate, n, m, q.proto, q.dir, q.addr);
7023*8b26181fSAndroid Build Coastguard Worker 
7024*8b26181fSAndroid Build Coastguard Worker 	default:
7025*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "Mask syntax for networks only");
7026*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
7027*8b26181fSAndroid Build Coastguard Worker 	}
7028*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
7029*8b26181fSAndroid Build Coastguard Worker }
7030*8b26181fSAndroid Build Coastguard Worker 
7031*8b26181fSAndroid Build Coastguard Worker struct block *
gen_ncode(compiler_state_t * cstate,const char * s,bpf_u_int32 v,struct qual q)7032*8b26181fSAndroid Build Coastguard Worker gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
7033*8b26181fSAndroid Build Coastguard Worker {
7034*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 mask;
7035*8b26181fSAndroid Build Coastguard Worker 	int proto;
7036*8b26181fSAndroid Build Coastguard Worker 	int dir;
7037*8b26181fSAndroid Build Coastguard Worker 	register int vlen;
7038*8b26181fSAndroid Build Coastguard Worker 
7039*8b26181fSAndroid Build Coastguard Worker 	/*
7040*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7041*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7042*8b26181fSAndroid Build Coastguard Worker 	 */
7043*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7044*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7045*8b26181fSAndroid Build Coastguard Worker 
7046*8b26181fSAndroid Build Coastguard Worker 	proto = q.proto;
7047*8b26181fSAndroid Build Coastguard Worker 	dir = q.dir;
7048*8b26181fSAndroid Build Coastguard Worker 	if (s == NULL)
7049*8b26181fSAndroid Build Coastguard Worker 		vlen = 32;
7050*8b26181fSAndroid Build Coastguard Worker 	else if (q.proto == Q_DECNET) {
7051*8b26181fSAndroid Build Coastguard Worker 		vlen = __pcap_atodn(s, &v);
7052*8b26181fSAndroid Build Coastguard Worker 		if (vlen == 0)
7053*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "malformed decnet address '%s'", s);
7054*8b26181fSAndroid Build Coastguard Worker 	} else {
7055*8b26181fSAndroid Build Coastguard Worker 		vlen = __pcap_atoin(s, &v);
7056*8b26181fSAndroid Build Coastguard Worker 		if (vlen < 0)
7057*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "invalid IPv4 address '%s'", s);
7058*8b26181fSAndroid Build Coastguard Worker 	}
7059*8b26181fSAndroid Build Coastguard Worker 
7060*8b26181fSAndroid Build Coastguard Worker 	switch (q.addr) {
7061*8b26181fSAndroid Build Coastguard Worker 
7062*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
7063*8b26181fSAndroid Build Coastguard Worker 	case Q_HOST:
7064*8b26181fSAndroid Build Coastguard Worker 	case Q_NET:
7065*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_DECNET)
7066*8b26181fSAndroid Build Coastguard Worker 			return gen_host(cstate, v, 0, proto, dir, q.addr);
7067*8b26181fSAndroid Build Coastguard Worker 		else if (proto == Q_LINK) {
7068*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal link layer address");
7069*8b26181fSAndroid Build Coastguard Worker 		} else {
7070*8b26181fSAndroid Build Coastguard Worker 			mask = 0xffffffff;
7071*8b26181fSAndroid Build Coastguard Worker 			if (s == NULL && q.addr == Q_NET) {
7072*8b26181fSAndroid Build Coastguard Worker 				/* Promote short net number */
7073*8b26181fSAndroid Build Coastguard Worker 				while (v && (v & 0xff000000) == 0) {
7074*8b26181fSAndroid Build Coastguard Worker 					v <<= 8;
7075*8b26181fSAndroid Build Coastguard Worker 					mask <<= 8;
7076*8b26181fSAndroid Build Coastguard Worker 				}
7077*8b26181fSAndroid Build Coastguard Worker 			} else {
7078*8b26181fSAndroid Build Coastguard Worker 				/* Promote short ipaddr */
7079*8b26181fSAndroid Build Coastguard Worker 				v <<= 32 - vlen;
7080*8b26181fSAndroid Build Coastguard Worker 				mask <<= 32 - vlen ;
7081*8b26181fSAndroid Build Coastguard Worker 			}
7082*8b26181fSAndroid Build Coastguard Worker 			return gen_host(cstate, v, mask, proto, dir, q.addr);
7083*8b26181fSAndroid Build Coastguard Worker 		}
7084*8b26181fSAndroid Build Coastguard Worker 
7085*8b26181fSAndroid Build Coastguard Worker 	case Q_PORT:
7086*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_UDP)
7087*8b26181fSAndroid Build Coastguard Worker 			proto = IPPROTO_UDP;
7088*8b26181fSAndroid Build Coastguard Worker 		else if (proto == Q_TCP)
7089*8b26181fSAndroid Build Coastguard Worker 			proto = IPPROTO_TCP;
7090*8b26181fSAndroid Build Coastguard Worker 		else if (proto == Q_SCTP)
7091*8b26181fSAndroid Build Coastguard Worker 			proto = IPPROTO_SCTP;
7092*8b26181fSAndroid Build Coastguard Worker 		else if (proto == Q_DEFAULT)
7093*8b26181fSAndroid Build Coastguard Worker 			proto = PROTO_UNDEF;
7094*8b26181fSAndroid Build Coastguard Worker 		else
7095*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal qualifier of 'port'");
7096*8b26181fSAndroid Build Coastguard Worker 
7097*8b26181fSAndroid Build Coastguard Worker 		if (v > 65535)
7098*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal port number %u > 65535", v);
7099*8b26181fSAndroid Build Coastguard Worker 
7100*8b26181fSAndroid Build Coastguard Worker 	    {
7101*8b26181fSAndroid Build Coastguard Worker 		struct block *b;
7102*8b26181fSAndroid Build Coastguard Worker 		b = gen_port(cstate, v, proto, dir);
7103*8b26181fSAndroid Build Coastguard Worker 		gen_or(gen_port6(cstate, v, proto, dir), b);
7104*8b26181fSAndroid Build Coastguard Worker 		return b;
7105*8b26181fSAndroid Build Coastguard Worker 	    }
7106*8b26181fSAndroid Build Coastguard Worker 
7107*8b26181fSAndroid Build Coastguard Worker 	case Q_PORTRANGE:
7108*8b26181fSAndroid Build Coastguard Worker 		if (proto == Q_UDP)
7109*8b26181fSAndroid Build Coastguard Worker 			proto = IPPROTO_UDP;
7110*8b26181fSAndroid Build Coastguard Worker 		else if (proto == Q_TCP)
7111*8b26181fSAndroid Build Coastguard Worker 			proto = IPPROTO_TCP;
7112*8b26181fSAndroid Build Coastguard Worker 		else if (proto == Q_SCTP)
7113*8b26181fSAndroid Build Coastguard Worker 			proto = IPPROTO_SCTP;
7114*8b26181fSAndroid Build Coastguard Worker 		else if (proto == Q_DEFAULT)
7115*8b26181fSAndroid Build Coastguard Worker 			proto = PROTO_UNDEF;
7116*8b26181fSAndroid Build Coastguard Worker 		else
7117*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal qualifier of 'portrange'");
7118*8b26181fSAndroid Build Coastguard Worker 
7119*8b26181fSAndroid Build Coastguard Worker 		if (v > 65535)
7120*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "illegal port number %u > 65535", v);
7121*8b26181fSAndroid Build Coastguard Worker 
7122*8b26181fSAndroid Build Coastguard Worker 	    {
7123*8b26181fSAndroid Build Coastguard Worker 		struct block *b;
7124*8b26181fSAndroid Build Coastguard Worker 		b = gen_portrange(cstate, v, v, proto, dir);
7125*8b26181fSAndroid Build Coastguard Worker 		gen_or(gen_portrange6(cstate, v, v, proto, dir), b);
7126*8b26181fSAndroid Build Coastguard Worker 		return b;
7127*8b26181fSAndroid Build Coastguard Worker 	    }
7128*8b26181fSAndroid Build Coastguard Worker 
7129*8b26181fSAndroid Build Coastguard Worker 	case Q_GATEWAY:
7130*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'gateway' requires a name");
7131*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
7132*8b26181fSAndroid Build Coastguard Worker 
7133*8b26181fSAndroid Build Coastguard Worker 	case Q_PROTO:
7134*8b26181fSAndroid Build Coastguard Worker 		return gen_proto(cstate, v, proto, dir);
7135*8b26181fSAndroid Build Coastguard Worker 
7136*8b26181fSAndroid Build Coastguard Worker #if !defined(NO_PROTOCHAIN)
7137*8b26181fSAndroid Build Coastguard Worker 	case Q_PROTOCHAIN:
7138*8b26181fSAndroid Build Coastguard Worker 		return gen_protochain(cstate, v, proto);
7139*8b26181fSAndroid Build Coastguard Worker #endif
7140*8b26181fSAndroid Build Coastguard Worker 
7141*8b26181fSAndroid Build Coastguard Worker 	case Q_UNDEF:
7142*8b26181fSAndroid Build Coastguard Worker 		syntax(cstate);
7143*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
7144*8b26181fSAndroid Build Coastguard Worker 
7145*8b26181fSAndroid Build Coastguard Worker 	default:
7146*8b26181fSAndroid Build Coastguard Worker 		abort();
7147*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
7148*8b26181fSAndroid Build Coastguard Worker 	}
7149*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
7150*8b26181fSAndroid Build Coastguard Worker }
7151*8b26181fSAndroid Build Coastguard Worker 
7152*8b26181fSAndroid Build Coastguard Worker #ifdef INET6
7153*8b26181fSAndroid Build Coastguard Worker struct block *
gen_mcode6(compiler_state_t * cstate,const char * s1,const char * s2,bpf_u_int32 masklen,struct qual q)7154*8b26181fSAndroid Build Coastguard Worker gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,
7155*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 masklen, struct qual q)
7156*8b26181fSAndroid Build Coastguard Worker {
7157*8b26181fSAndroid Build Coastguard Worker 	struct addrinfo *res;
7158*8b26181fSAndroid Build Coastguard Worker 	struct in6_addr *addr;
7159*8b26181fSAndroid Build Coastguard Worker 	struct in6_addr mask;
7160*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
7161*8b26181fSAndroid Build Coastguard Worker 	uint32_t *a, *m;
7162*8b26181fSAndroid Build Coastguard Worker 
7163*8b26181fSAndroid Build Coastguard Worker 	/*
7164*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7165*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7166*8b26181fSAndroid Build Coastguard Worker 	 */
7167*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7168*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7169*8b26181fSAndroid Build Coastguard Worker 
7170*8b26181fSAndroid Build Coastguard Worker 	if (s2)
7171*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "no mask %s supported", s2);
7172*8b26181fSAndroid Build Coastguard Worker 
7173*8b26181fSAndroid Build Coastguard Worker 	res = pcap_nametoaddrinfo(s1);
7174*8b26181fSAndroid Build Coastguard Worker 	if (!res)
7175*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "invalid ip6 address %s", s1);
7176*8b26181fSAndroid Build Coastguard Worker 	cstate->ai = res;
7177*8b26181fSAndroid Build Coastguard Worker 	if (res->ai_next)
7178*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "%s resolved to multiple address", s1);
7179*8b26181fSAndroid Build Coastguard Worker 	addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
7180*8b26181fSAndroid Build Coastguard Worker 
7181*8b26181fSAndroid Build Coastguard Worker 	if (masklen > sizeof(mask.s6_addr) * 8)
7182*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask.s6_addr) * 8));
7183*8b26181fSAndroid Build Coastguard Worker 	memset(&mask, 0, sizeof(mask));
7184*8b26181fSAndroid Build Coastguard Worker 	memset(&mask.s6_addr, 0xff, masklen / 8);
7185*8b26181fSAndroid Build Coastguard Worker 	if (masklen % 8) {
7186*8b26181fSAndroid Build Coastguard Worker 		mask.s6_addr[masklen / 8] =
7187*8b26181fSAndroid Build Coastguard Worker 			(0xff << (8 - masklen % 8)) & 0xff;
7188*8b26181fSAndroid Build Coastguard Worker 	}
7189*8b26181fSAndroid Build Coastguard Worker 
7190*8b26181fSAndroid Build Coastguard Worker 	a = (uint32_t *)addr;
7191*8b26181fSAndroid Build Coastguard Worker 	m = (uint32_t *)&mask;
7192*8b26181fSAndroid Build Coastguard Worker 	if ((a[0] & ~m[0]) || (a[1] & ~m[1])
7193*8b26181fSAndroid Build Coastguard Worker 	 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
7194*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen);
7195*8b26181fSAndroid Build Coastguard Worker 	}
7196*8b26181fSAndroid Build Coastguard Worker 
7197*8b26181fSAndroid Build Coastguard Worker 	switch (q.addr) {
7198*8b26181fSAndroid Build Coastguard Worker 
7199*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
7200*8b26181fSAndroid Build Coastguard Worker 	case Q_HOST:
7201*8b26181fSAndroid Build Coastguard Worker 		if (masklen != 128)
7202*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "Mask syntax for networks only");
7203*8b26181fSAndroid Build Coastguard Worker 		/* FALLTHROUGH */
7204*8b26181fSAndroid Build Coastguard Worker 
7205*8b26181fSAndroid Build Coastguard Worker 	case Q_NET:
7206*8b26181fSAndroid Build Coastguard Worker 		b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);
7207*8b26181fSAndroid Build Coastguard Worker 		cstate->ai = NULL;
7208*8b26181fSAndroid Build Coastguard Worker 		freeaddrinfo(res);
7209*8b26181fSAndroid Build Coastguard Worker 		return b;
7210*8b26181fSAndroid Build Coastguard Worker 
7211*8b26181fSAndroid Build Coastguard Worker 	default:
7212*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "invalid qualifier against IPv6 address");
7213*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
7214*8b26181fSAndroid Build Coastguard Worker 	}
7215*8b26181fSAndroid Build Coastguard Worker }
7216*8b26181fSAndroid Build Coastguard Worker #endif /*INET6*/
7217*8b26181fSAndroid Build Coastguard Worker 
7218*8b26181fSAndroid Build Coastguard Worker struct block *
gen_ecode(compiler_state_t * cstate,const char * s,struct qual q)7219*8b26181fSAndroid Build Coastguard Worker gen_ecode(compiler_state_t *cstate, const char *s, struct qual q)
7220*8b26181fSAndroid Build Coastguard Worker {
7221*8b26181fSAndroid Build Coastguard Worker 	struct block *b, *tmp;
7222*8b26181fSAndroid Build Coastguard Worker 
7223*8b26181fSAndroid Build Coastguard Worker 	/*
7224*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7225*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7226*8b26181fSAndroid Build Coastguard Worker 	 */
7227*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7228*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7229*8b26181fSAndroid Build Coastguard Worker 
7230*8b26181fSAndroid Build Coastguard Worker 	if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
7231*8b26181fSAndroid Build Coastguard Worker 		cstate->e = pcap_ether_aton(s);
7232*8b26181fSAndroid Build Coastguard Worker 		if (cstate->e == NULL)
7233*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "malloc");
7234*8b26181fSAndroid Build Coastguard Worker 		switch (cstate->linktype) {
7235*8b26181fSAndroid Build Coastguard Worker 		case DLT_EN10MB:
7236*8b26181fSAndroid Build Coastguard Worker 		case DLT_NETANALYZER:
7237*8b26181fSAndroid Build Coastguard Worker 		case DLT_NETANALYZER_TRANSPARENT:
7238*8b26181fSAndroid Build Coastguard Worker 			tmp = gen_prevlinkhdr_check(cstate);
7239*8b26181fSAndroid Build Coastguard Worker 			b = gen_ehostop(cstate, cstate->e, (int)q.dir);
7240*8b26181fSAndroid Build Coastguard Worker 			if (tmp != NULL)
7241*8b26181fSAndroid Build Coastguard Worker 				gen_and(tmp, b);
7242*8b26181fSAndroid Build Coastguard Worker 			break;
7243*8b26181fSAndroid Build Coastguard Worker 		case DLT_FDDI:
7244*8b26181fSAndroid Build Coastguard Worker 			b = gen_fhostop(cstate, cstate->e, (int)q.dir);
7245*8b26181fSAndroid Build Coastguard Worker 			break;
7246*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802:
7247*8b26181fSAndroid Build Coastguard Worker 			b = gen_thostop(cstate, cstate->e, (int)q.dir);
7248*8b26181fSAndroid Build Coastguard Worker 			break;
7249*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11:
7250*8b26181fSAndroid Build Coastguard Worker 		case DLT_PRISM_HEADER:
7251*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11_RADIO_AVS:
7252*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11_RADIO:
7253*8b26181fSAndroid Build Coastguard Worker 		case DLT_PPI:
7254*8b26181fSAndroid Build Coastguard Worker 			b = gen_wlanhostop(cstate, cstate->e, (int)q.dir);
7255*8b26181fSAndroid Build Coastguard Worker 			break;
7256*8b26181fSAndroid Build Coastguard Worker 		case DLT_IP_OVER_FC:
7257*8b26181fSAndroid Build Coastguard Worker 			b = gen_ipfchostop(cstate, cstate->e, (int)q.dir);
7258*8b26181fSAndroid Build Coastguard Worker 			break;
7259*8b26181fSAndroid Build Coastguard Worker 		default:
7260*8b26181fSAndroid Build Coastguard Worker 			free(cstate->e);
7261*8b26181fSAndroid Build Coastguard Worker 			cstate->e = NULL;
7262*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7263*8b26181fSAndroid Build Coastguard Worker 			/*NOTREACHED*/
7264*8b26181fSAndroid Build Coastguard Worker 		}
7265*8b26181fSAndroid Build Coastguard Worker 		free(cstate->e);
7266*8b26181fSAndroid Build Coastguard Worker 		cstate->e = NULL;
7267*8b26181fSAndroid Build Coastguard Worker 		return (b);
7268*8b26181fSAndroid Build Coastguard Worker 	}
7269*8b26181fSAndroid Build Coastguard Worker 	bpf_error(cstate, "ethernet address used in non-ether expression");
7270*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
7271*8b26181fSAndroid Build Coastguard Worker }
7272*8b26181fSAndroid Build Coastguard Worker 
7273*8b26181fSAndroid Build Coastguard Worker void
sappend(struct slist * s0,struct slist * s1)7274*8b26181fSAndroid Build Coastguard Worker sappend(struct slist *s0, struct slist *s1)
7275*8b26181fSAndroid Build Coastguard Worker {
7276*8b26181fSAndroid Build Coastguard Worker 	/*
7277*8b26181fSAndroid Build Coastguard Worker 	 * This is definitely not the best way to do this, but the
7278*8b26181fSAndroid Build Coastguard Worker 	 * lists will rarely get long.
7279*8b26181fSAndroid Build Coastguard Worker 	 */
7280*8b26181fSAndroid Build Coastguard Worker 	while (s0->next)
7281*8b26181fSAndroid Build Coastguard Worker 		s0 = s0->next;
7282*8b26181fSAndroid Build Coastguard Worker 	s0->next = s1;
7283*8b26181fSAndroid Build Coastguard Worker }
7284*8b26181fSAndroid Build Coastguard Worker 
7285*8b26181fSAndroid Build Coastguard Worker static struct slist *
xfer_to_x(compiler_state_t * cstate,struct arth * a)7286*8b26181fSAndroid Build Coastguard Worker xfer_to_x(compiler_state_t *cstate, struct arth *a)
7287*8b26181fSAndroid Build Coastguard Worker {
7288*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
7289*8b26181fSAndroid Build Coastguard Worker 
7290*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_LDX|BPF_MEM);
7291*8b26181fSAndroid Build Coastguard Worker 	s->s.k = a->regno;
7292*8b26181fSAndroid Build Coastguard Worker 	return s;
7293*8b26181fSAndroid Build Coastguard Worker }
7294*8b26181fSAndroid Build Coastguard Worker 
7295*8b26181fSAndroid Build Coastguard Worker static struct slist *
xfer_to_a(compiler_state_t * cstate,struct arth * a)7296*8b26181fSAndroid Build Coastguard Worker xfer_to_a(compiler_state_t *cstate, struct arth *a)
7297*8b26181fSAndroid Build Coastguard Worker {
7298*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
7299*8b26181fSAndroid Build Coastguard Worker 
7300*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_LD|BPF_MEM);
7301*8b26181fSAndroid Build Coastguard Worker 	s->s.k = a->regno;
7302*8b26181fSAndroid Build Coastguard Worker 	return s;
7303*8b26181fSAndroid Build Coastguard Worker }
7304*8b26181fSAndroid Build Coastguard Worker 
7305*8b26181fSAndroid Build Coastguard Worker /*
7306*8b26181fSAndroid Build Coastguard Worker  * Modify "index" to use the value stored into its register as an
7307*8b26181fSAndroid Build Coastguard Worker  * offset relative to the beginning of the header for the protocol
7308*8b26181fSAndroid Build Coastguard Worker  * "proto", and allocate a register and put an item "size" bytes long
7309*8b26181fSAndroid Build Coastguard Worker  * (1, 2, or 4) at that offset into that register, making it the register
7310*8b26181fSAndroid Build Coastguard Worker  * for "index".
7311*8b26181fSAndroid Build Coastguard Worker  */
7312*8b26181fSAndroid Build Coastguard Worker static struct arth *
gen_load_internal(compiler_state_t * cstate,int proto,struct arth * inst,bpf_u_int32 size)7313*8b26181fSAndroid Build Coastguard Worker gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst,
7314*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 size)
7315*8b26181fSAndroid Build Coastguard Worker {
7316*8b26181fSAndroid Build Coastguard Worker 	int size_code;
7317*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *tmp;
7318*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
7319*8b26181fSAndroid Build Coastguard Worker 	int regno = alloc_reg(cstate);
7320*8b26181fSAndroid Build Coastguard Worker 
7321*8b26181fSAndroid Build Coastguard Worker 	free_reg(cstate, inst->regno);
7322*8b26181fSAndroid Build Coastguard Worker 	switch (size) {
7323*8b26181fSAndroid Build Coastguard Worker 
7324*8b26181fSAndroid Build Coastguard Worker 	default:
7325*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "data size must be 1, 2, or 4");
7326*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
7327*8b26181fSAndroid Build Coastguard Worker 
7328*8b26181fSAndroid Build Coastguard Worker 	case 1:
7329*8b26181fSAndroid Build Coastguard Worker 		size_code = BPF_B;
7330*8b26181fSAndroid Build Coastguard Worker 		break;
7331*8b26181fSAndroid Build Coastguard Worker 
7332*8b26181fSAndroid Build Coastguard Worker 	case 2:
7333*8b26181fSAndroid Build Coastguard Worker 		size_code = BPF_H;
7334*8b26181fSAndroid Build Coastguard Worker 		break;
7335*8b26181fSAndroid Build Coastguard Worker 
7336*8b26181fSAndroid Build Coastguard Worker 	case 4:
7337*8b26181fSAndroid Build Coastguard Worker 		size_code = BPF_W;
7338*8b26181fSAndroid Build Coastguard Worker 		break;
7339*8b26181fSAndroid Build Coastguard Worker 	}
7340*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
7341*8b26181fSAndroid Build Coastguard Worker 	default:
7342*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "unsupported index operation");
7343*8b26181fSAndroid Build Coastguard Worker 
7344*8b26181fSAndroid Build Coastguard Worker 	case Q_RADIO:
7345*8b26181fSAndroid Build Coastguard Worker 		/*
7346*8b26181fSAndroid Build Coastguard Worker 		 * The offset is relative to the beginning of the packet
7347*8b26181fSAndroid Build Coastguard Worker 		 * data, if we have a radio header.  (If we don't, this
7348*8b26181fSAndroid Build Coastguard Worker 		 * is an error.)
7349*8b26181fSAndroid Build Coastguard Worker 		 */
7350*8b26181fSAndroid Build Coastguard Worker 		if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&
7351*8b26181fSAndroid Build Coastguard Worker 		    cstate->linktype != DLT_IEEE802_11_RADIO &&
7352*8b26181fSAndroid Build Coastguard Worker 		    cstate->linktype != DLT_PRISM_HEADER)
7353*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "radio information not present in capture");
7354*8b26181fSAndroid Build Coastguard Worker 
7355*8b26181fSAndroid Build Coastguard Worker 		/*
7356*8b26181fSAndroid Build Coastguard Worker 		 * Load into the X register the offset computed into the
7357*8b26181fSAndroid Build Coastguard Worker 		 * register specified by "index".
7358*8b26181fSAndroid Build Coastguard Worker 		 */
7359*8b26181fSAndroid Build Coastguard Worker 		s = xfer_to_x(cstate, inst);
7360*8b26181fSAndroid Build Coastguard Worker 
7361*8b26181fSAndroid Build Coastguard Worker 		/*
7362*8b26181fSAndroid Build Coastguard Worker 		 * Load the item at that offset.
7363*8b26181fSAndroid Build Coastguard Worker 		 */
7364*8b26181fSAndroid Build Coastguard Worker 		tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7365*8b26181fSAndroid Build Coastguard Worker 		sappend(s, tmp);
7366*8b26181fSAndroid Build Coastguard Worker 		sappend(inst->s, s);
7367*8b26181fSAndroid Build Coastguard Worker 		break;
7368*8b26181fSAndroid Build Coastguard Worker 
7369*8b26181fSAndroid Build Coastguard Worker 	case Q_LINK:
7370*8b26181fSAndroid Build Coastguard Worker 		/*
7371*8b26181fSAndroid Build Coastguard Worker 		 * The offset is relative to the beginning of
7372*8b26181fSAndroid Build Coastguard Worker 		 * the link-layer header.
7373*8b26181fSAndroid Build Coastguard Worker 		 *
7374*8b26181fSAndroid Build Coastguard Worker 		 * XXX - what about ATM LANE?  Should the index be
7375*8b26181fSAndroid Build Coastguard Worker 		 * relative to the beginning of the AAL5 frame, so
7376*8b26181fSAndroid Build Coastguard Worker 		 * that 0 refers to the beginning of the LE Control
7377*8b26181fSAndroid Build Coastguard Worker 		 * field, or relative to the beginning of the LAN
7378*8b26181fSAndroid Build Coastguard Worker 		 * frame, so that 0 refers, for Ethernet LANE, to
7379*8b26181fSAndroid Build Coastguard Worker 		 * the beginning of the destination address?
7380*8b26181fSAndroid Build Coastguard Worker 		 */
7381*8b26181fSAndroid Build Coastguard Worker 		s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);
7382*8b26181fSAndroid Build Coastguard Worker 
7383*8b26181fSAndroid Build Coastguard Worker 		/*
7384*8b26181fSAndroid Build Coastguard Worker 		 * If "s" is non-null, it has code to arrange that the
7385*8b26181fSAndroid Build Coastguard Worker 		 * X register contains the length of the prefix preceding
7386*8b26181fSAndroid Build Coastguard Worker 		 * the link-layer header.  Add to it the offset computed
7387*8b26181fSAndroid Build Coastguard Worker 		 * into the register specified by "index", and move that
7388*8b26181fSAndroid Build Coastguard Worker 		 * into the X register.  Otherwise, just load into the X
7389*8b26181fSAndroid Build Coastguard Worker 		 * register the offset computed into the register specified
7390*8b26181fSAndroid Build Coastguard Worker 		 * by "index".
7391*8b26181fSAndroid Build Coastguard Worker 		 */
7392*8b26181fSAndroid Build Coastguard Worker 		if (s != NULL) {
7393*8b26181fSAndroid Build Coastguard Worker 			sappend(s, xfer_to_a(cstate, inst));
7394*8b26181fSAndroid Build Coastguard Worker 			sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7395*8b26181fSAndroid Build Coastguard Worker 			sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7396*8b26181fSAndroid Build Coastguard Worker 		} else
7397*8b26181fSAndroid Build Coastguard Worker 			s = xfer_to_x(cstate, inst);
7398*8b26181fSAndroid Build Coastguard Worker 
7399*8b26181fSAndroid Build Coastguard Worker 		/*
7400*8b26181fSAndroid Build Coastguard Worker 		 * Load the item at the sum of the offset we've put in the
7401*8b26181fSAndroid Build Coastguard Worker 		 * X register and the offset of the start of the link
7402*8b26181fSAndroid Build Coastguard Worker 		 * layer header (which is 0 if the radio header is
7403*8b26181fSAndroid Build Coastguard Worker 		 * variable-length; that header length is what we put
7404*8b26181fSAndroid Build Coastguard Worker 		 * into the X register and then added to the index).
7405*8b26181fSAndroid Build Coastguard Worker 		 */
7406*8b26181fSAndroid Build Coastguard Worker 		tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7407*8b26181fSAndroid Build Coastguard Worker 		tmp->s.k = cstate->off_linkhdr.constant_part;
7408*8b26181fSAndroid Build Coastguard Worker 		sappend(s, tmp);
7409*8b26181fSAndroid Build Coastguard Worker 		sappend(inst->s, s);
7410*8b26181fSAndroid Build Coastguard Worker 		break;
7411*8b26181fSAndroid Build Coastguard Worker 
7412*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
7413*8b26181fSAndroid Build Coastguard Worker 	case Q_ARP:
7414*8b26181fSAndroid Build Coastguard Worker 	case Q_RARP:
7415*8b26181fSAndroid Build Coastguard Worker 	case Q_ATALK:
7416*8b26181fSAndroid Build Coastguard Worker 	case Q_DECNET:
7417*8b26181fSAndroid Build Coastguard Worker 	case Q_SCA:
7418*8b26181fSAndroid Build Coastguard Worker 	case Q_LAT:
7419*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPRC:
7420*8b26181fSAndroid Build Coastguard Worker 	case Q_MOPDL:
7421*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
7422*8b26181fSAndroid Build Coastguard Worker 		/*
7423*8b26181fSAndroid Build Coastguard Worker 		 * The offset is relative to the beginning of
7424*8b26181fSAndroid Build Coastguard Worker 		 * the network-layer header.
7425*8b26181fSAndroid Build Coastguard Worker 		 * XXX - are there any cases where we want
7426*8b26181fSAndroid Build Coastguard Worker 		 * cstate->off_nl_nosnap?
7427*8b26181fSAndroid Build Coastguard Worker 		 */
7428*8b26181fSAndroid Build Coastguard Worker 		s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7429*8b26181fSAndroid Build Coastguard Worker 
7430*8b26181fSAndroid Build Coastguard Worker 		/*
7431*8b26181fSAndroid Build Coastguard Worker 		 * If "s" is non-null, it has code to arrange that the
7432*8b26181fSAndroid Build Coastguard Worker 		 * X register contains the variable part of the offset
7433*8b26181fSAndroid Build Coastguard Worker 		 * of the link-layer payload.  Add to it the offset
7434*8b26181fSAndroid Build Coastguard Worker 		 * computed into the register specified by "index",
7435*8b26181fSAndroid Build Coastguard Worker 		 * and move that into the X register.  Otherwise, just
7436*8b26181fSAndroid Build Coastguard Worker 		 * load into the X register the offset computed into
7437*8b26181fSAndroid Build Coastguard Worker 		 * the register specified by "index".
7438*8b26181fSAndroid Build Coastguard Worker 		 */
7439*8b26181fSAndroid Build Coastguard Worker 		if (s != NULL) {
7440*8b26181fSAndroid Build Coastguard Worker 			sappend(s, xfer_to_a(cstate, inst));
7441*8b26181fSAndroid Build Coastguard Worker 			sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7442*8b26181fSAndroid Build Coastguard Worker 			sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7443*8b26181fSAndroid Build Coastguard Worker 		} else
7444*8b26181fSAndroid Build Coastguard Worker 			s = xfer_to_x(cstate, inst);
7445*8b26181fSAndroid Build Coastguard Worker 
7446*8b26181fSAndroid Build Coastguard Worker 		/*
7447*8b26181fSAndroid Build Coastguard Worker 		 * Load the item at the sum of the offset we've put in the
7448*8b26181fSAndroid Build Coastguard Worker 		 * X register, the offset of the start of the network
7449*8b26181fSAndroid Build Coastguard Worker 		 * layer header from the beginning of the link-layer
7450*8b26181fSAndroid Build Coastguard Worker 		 * payload, and the constant part of the offset of the
7451*8b26181fSAndroid Build Coastguard Worker 		 * start of the link-layer payload.
7452*8b26181fSAndroid Build Coastguard Worker 		 */
7453*8b26181fSAndroid Build Coastguard Worker 		tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7454*8b26181fSAndroid Build Coastguard Worker 		tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7455*8b26181fSAndroid Build Coastguard Worker 		sappend(s, tmp);
7456*8b26181fSAndroid Build Coastguard Worker 		sappend(inst->s, s);
7457*8b26181fSAndroid Build Coastguard Worker 
7458*8b26181fSAndroid Build Coastguard Worker 		/*
7459*8b26181fSAndroid Build Coastguard Worker 		 * Do the computation only if the packet contains
7460*8b26181fSAndroid Build Coastguard Worker 		 * the protocol in question.
7461*8b26181fSAndroid Build Coastguard Worker 		 */
7462*8b26181fSAndroid Build Coastguard Worker 		b = gen_proto_abbrev_internal(cstate, proto);
7463*8b26181fSAndroid Build Coastguard Worker 		if (inst->b)
7464*8b26181fSAndroid Build Coastguard Worker 			gen_and(inst->b, b);
7465*8b26181fSAndroid Build Coastguard Worker 		inst->b = b;
7466*8b26181fSAndroid Build Coastguard Worker 		break;
7467*8b26181fSAndroid Build Coastguard Worker 
7468*8b26181fSAndroid Build Coastguard Worker 	case Q_SCTP:
7469*8b26181fSAndroid Build Coastguard Worker 	case Q_TCP:
7470*8b26181fSAndroid Build Coastguard Worker 	case Q_UDP:
7471*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMP:
7472*8b26181fSAndroid Build Coastguard Worker 	case Q_IGMP:
7473*8b26181fSAndroid Build Coastguard Worker 	case Q_IGRP:
7474*8b26181fSAndroid Build Coastguard Worker 	case Q_PIM:
7475*8b26181fSAndroid Build Coastguard Worker 	case Q_VRRP:
7476*8b26181fSAndroid Build Coastguard Worker 	case Q_CARP:
7477*8b26181fSAndroid Build Coastguard Worker 		/*
7478*8b26181fSAndroid Build Coastguard Worker 		 * The offset is relative to the beginning of
7479*8b26181fSAndroid Build Coastguard Worker 		 * the transport-layer header.
7480*8b26181fSAndroid Build Coastguard Worker 		 *
7481*8b26181fSAndroid Build Coastguard Worker 		 * Load the X register with the length of the IPv4 header
7482*8b26181fSAndroid Build Coastguard Worker 		 * (plus the offset of the link-layer header, if it's
7483*8b26181fSAndroid Build Coastguard Worker 		 * a variable-length header), in bytes.
7484*8b26181fSAndroid Build Coastguard Worker 		 *
7485*8b26181fSAndroid Build Coastguard Worker 		 * XXX - are there any cases where we want
7486*8b26181fSAndroid Build Coastguard Worker 		 * cstate->off_nl_nosnap?
7487*8b26181fSAndroid Build Coastguard Worker 		 * XXX - we should, if we're built with
7488*8b26181fSAndroid Build Coastguard Worker 		 * IPv6 support, generate code to load either
7489*8b26181fSAndroid Build Coastguard Worker 		 * IPv4, IPv6, or both, as appropriate.
7490*8b26181fSAndroid Build Coastguard Worker 		 */
7491*8b26181fSAndroid Build Coastguard Worker 		s = gen_loadx_iphdrlen(cstate);
7492*8b26181fSAndroid Build Coastguard Worker 
7493*8b26181fSAndroid Build Coastguard Worker 		/*
7494*8b26181fSAndroid Build Coastguard Worker 		 * The X register now contains the sum of the variable
7495*8b26181fSAndroid Build Coastguard Worker 		 * part of the offset of the link-layer payload and the
7496*8b26181fSAndroid Build Coastguard Worker 		 * length of the network-layer header.
7497*8b26181fSAndroid Build Coastguard Worker 		 *
7498*8b26181fSAndroid Build Coastguard Worker 		 * Load into the A register the offset relative to
7499*8b26181fSAndroid Build Coastguard Worker 		 * the beginning of the transport layer header,
7500*8b26181fSAndroid Build Coastguard Worker 		 * add the X register to that, move that to the
7501*8b26181fSAndroid Build Coastguard Worker 		 * X register, and load with an offset from the
7502*8b26181fSAndroid Build Coastguard Worker 		 * X register equal to the sum of the constant part of
7503*8b26181fSAndroid Build Coastguard Worker 		 * the offset of the link-layer payload and the offset,
7504*8b26181fSAndroid Build Coastguard Worker 		 * relative to the beginning of the link-layer payload,
7505*8b26181fSAndroid Build Coastguard Worker 		 * of the network-layer header.
7506*8b26181fSAndroid Build Coastguard Worker 		 */
7507*8b26181fSAndroid Build Coastguard Worker 		sappend(s, xfer_to_a(cstate, inst));
7508*8b26181fSAndroid Build Coastguard Worker 		sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7509*8b26181fSAndroid Build Coastguard Worker 		sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7510*8b26181fSAndroid Build Coastguard Worker 		sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code));
7511*8b26181fSAndroid Build Coastguard Worker 		tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7512*8b26181fSAndroid Build Coastguard Worker 		sappend(inst->s, s);
7513*8b26181fSAndroid Build Coastguard Worker 
7514*8b26181fSAndroid Build Coastguard Worker 		/*
7515*8b26181fSAndroid Build Coastguard Worker 		 * Do the computation only if the packet contains
7516*8b26181fSAndroid Build Coastguard Worker 		 * the protocol in question - which is true only
7517*8b26181fSAndroid Build Coastguard Worker 		 * if this is an IP datagram and is the first or
7518*8b26181fSAndroid Build Coastguard Worker 		 * only fragment of that datagram.
7519*8b26181fSAndroid Build Coastguard Worker 		 */
7520*8b26181fSAndroid Build Coastguard Worker 		gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate));
7521*8b26181fSAndroid Build Coastguard Worker 		if (inst->b)
7522*8b26181fSAndroid Build Coastguard Worker 			gen_and(inst->b, b);
7523*8b26181fSAndroid Build Coastguard Worker 		gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b);
7524*8b26181fSAndroid Build Coastguard Worker 		inst->b = b;
7525*8b26181fSAndroid Build Coastguard Worker 		break;
7526*8b26181fSAndroid Build Coastguard Worker 	case Q_ICMPV6:
7527*8b26181fSAndroid Build Coastguard Worker         /*
7528*8b26181fSAndroid Build Coastguard Worker         * Do the computation only if the packet contains
7529*8b26181fSAndroid Build Coastguard Worker         * the protocol in question.
7530*8b26181fSAndroid Build Coastguard Worker         */
7531*8b26181fSAndroid Build Coastguard Worker         b = gen_proto_abbrev_internal(cstate, Q_IPV6);
7532*8b26181fSAndroid Build Coastguard Worker         if (inst->b) {
7533*8b26181fSAndroid Build Coastguard Worker             gen_and(inst->b, b);
7534*8b26181fSAndroid Build Coastguard Worker         }
7535*8b26181fSAndroid Build Coastguard Worker         inst->b = b;
7536*8b26181fSAndroid Build Coastguard Worker 
7537*8b26181fSAndroid Build Coastguard Worker         /*
7538*8b26181fSAndroid Build Coastguard Worker         * Check if we have an icmp6 next header
7539*8b26181fSAndroid Build Coastguard Worker         */
7540*8b26181fSAndroid Build Coastguard Worker         b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);
7541*8b26181fSAndroid Build Coastguard Worker         if (inst->b) {
7542*8b26181fSAndroid Build Coastguard Worker             gen_and(inst->b, b);
7543*8b26181fSAndroid Build Coastguard Worker         }
7544*8b26181fSAndroid Build Coastguard Worker         inst->b = b;
7545*8b26181fSAndroid Build Coastguard Worker 
7546*8b26181fSAndroid Build Coastguard Worker 
7547*8b26181fSAndroid Build Coastguard Worker         s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7548*8b26181fSAndroid Build Coastguard Worker         /*
7549*8b26181fSAndroid Build Coastguard Worker         * If "s" is non-null, it has code to arrange that the
7550*8b26181fSAndroid Build Coastguard Worker         * X register contains the variable part of the offset
7551*8b26181fSAndroid Build Coastguard Worker         * of the link-layer payload.  Add to it the offset
7552*8b26181fSAndroid Build Coastguard Worker         * computed into the register specified by "index",
7553*8b26181fSAndroid Build Coastguard Worker         * and move that into the X register.  Otherwise, just
7554*8b26181fSAndroid Build Coastguard Worker         * load into the X register the offset computed into
7555*8b26181fSAndroid Build Coastguard Worker         * the register specified by "index".
7556*8b26181fSAndroid Build Coastguard Worker         */
7557*8b26181fSAndroid Build Coastguard Worker         if (s != NULL) {
7558*8b26181fSAndroid Build Coastguard Worker             sappend(s, xfer_to_a(cstate, inst));
7559*8b26181fSAndroid Build Coastguard Worker             sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7560*8b26181fSAndroid Build Coastguard Worker             sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7561*8b26181fSAndroid Build Coastguard Worker         } else {
7562*8b26181fSAndroid Build Coastguard Worker             s = xfer_to_x(cstate, inst);
7563*8b26181fSAndroid Build Coastguard Worker         }
7564*8b26181fSAndroid Build Coastguard Worker 
7565*8b26181fSAndroid Build Coastguard Worker         /*
7566*8b26181fSAndroid Build Coastguard Worker         * Load the item at the sum of the offset we've put in the
7567*8b26181fSAndroid Build Coastguard Worker         * X register, the offset of the start of the network
7568*8b26181fSAndroid Build Coastguard Worker         * layer header from the beginning of the link-layer
7569*8b26181fSAndroid Build Coastguard Worker         * payload, and the constant part of the offset of the
7570*8b26181fSAndroid Build Coastguard Worker         * start of the link-layer payload.
7571*8b26181fSAndroid Build Coastguard Worker         */
7572*8b26181fSAndroid Build Coastguard Worker         tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7573*8b26181fSAndroid Build Coastguard Worker         tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;
7574*8b26181fSAndroid Build Coastguard Worker 
7575*8b26181fSAndroid Build Coastguard Worker         sappend(s, tmp);
7576*8b26181fSAndroid Build Coastguard Worker         sappend(inst->s, s);
7577*8b26181fSAndroid Build Coastguard Worker 
7578*8b26181fSAndroid Build Coastguard Worker         break;
7579*8b26181fSAndroid Build Coastguard Worker 	}
7580*8b26181fSAndroid Build Coastguard Worker 	inst->regno = regno;
7581*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_ST);
7582*8b26181fSAndroid Build Coastguard Worker 	s->s.k = regno;
7583*8b26181fSAndroid Build Coastguard Worker 	sappend(inst->s, s);
7584*8b26181fSAndroid Build Coastguard Worker 
7585*8b26181fSAndroid Build Coastguard Worker 	return inst;
7586*8b26181fSAndroid Build Coastguard Worker }
7587*8b26181fSAndroid Build Coastguard Worker 
7588*8b26181fSAndroid Build Coastguard Worker struct arth *
gen_load(compiler_state_t * cstate,int proto,struct arth * inst,bpf_u_int32 size)7589*8b26181fSAndroid Build Coastguard Worker gen_load(compiler_state_t *cstate, int proto, struct arth *inst,
7590*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 size)
7591*8b26181fSAndroid Build Coastguard Worker {
7592*8b26181fSAndroid Build Coastguard Worker 	/*
7593*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7594*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7595*8b26181fSAndroid Build Coastguard Worker 	 */
7596*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7597*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7598*8b26181fSAndroid Build Coastguard Worker 
7599*8b26181fSAndroid Build Coastguard Worker 	return gen_load_internal(cstate, proto, inst, size);
7600*8b26181fSAndroid Build Coastguard Worker }
7601*8b26181fSAndroid Build Coastguard Worker 
7602*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_relation_internal(compiler_state_t * cstate,int code,struct arth * a0,struct arth * a1,int reversed)7603*8b26181fSAndroid Build Coastguard Worker gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0,
7604*8b26181fSAndroid Build Coastguard Worker     struct arth *a1, int reversed)
7605*8b26181fSAndroid Build Coastguard Worker {
7606*8b26181fSAndroid Build Coastguard Worker 	struct slist *s0, *s1, *s2;
7607*8b26181fSAndroid Build Coastguard Worker 	struct block *b, *tmp;
7608*8b26181fSAndroid Build Coastguard Worker 
7609*8b26181fSAndroid Build Coastguard Worker 	s0 = xfer_to_x(cstate, a1);
7610*8b26181fSAndroid Build Coastguard Worker 	s1 = xfer_to_a(cstate, a0);
7611*8b26181fSAndroid Build Coastguard Worker 	if (code == BPF_JEQ) {
7612*8b26181fSAndroid Build Coastguard Worker 		s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);
7613*8b26181fSAndroid Build Coastguard Worker 		b = new_block(cstate, JMP(code));
7614*8b26181fSAndroid Build Coastguard Worker 		sappend(s1, s2);
7615*8b26181fSAndroid Build Coastguard Worker 	}
7616*8b26181fSAndroid Build Coastguard Worker 	else
7617*8b26181fSAndroid Build Coastguard Worker 		b = new_block(cstate, BPF_JMP|code|BPF_X);
7618*8b26181fSAndroid Build Coastguard Worker 	if (reversed)
7619*8b26181fSAndroid Build Coastguard Worker 		gen_not(b);
7620*8b26181fSAndroid Build Coastguard Worker 
7621*8b26181fSAndroid Build Coastguard Worker 	sappend(s0, s1);
7622*8b26181fSAndroid Build Coastguard Worker 	sappend(a1->s, s0);
7623*8b26181fSAndroid Build Coastguard Worker 	sappend(a0->s, a1->s);
7624*8b26181fSAndroid Build Coastguard Worker 
7625*8b26181fSAndroid Build Coastguard Worker 	b->stmts = a0->s;
7626*8b26181fSAndroid Build Coastguard Worker 
7627*8b26181fSAndroid Build Coastguard Worker 	free_reg(cstate, a0->regno);
7628*8b26181fSAndroid Build Coastguard Worker 	free_reg(cstate, a1->regno);
7629*8b26181fSAndroid Build Coastguard Worker 
7630*8b26181fSAndroid Build Coastguard Worker 	/* 'and' together protocol checks */
7631*8b26181fSAndroid Build Coastguard Worker 	if (a0->b) {
7632*8b26181fSAndroid Build Coastguard Worker 		if (a1->b) {
7633*8b26181fSAndroid Build Coastguard Worker 			gen_and(a0->b, tmp = a1->b);
7634*8b26181fSAndroid Build Coastguard Worker 		}
7635*8b26181fSAndroid Build Coastguard Worker 		else
7636*8b26181fSAndroid Build Coastguard Worker 			tmp = a0->b;
7637*8b26181fSAndroid Build Coastguard Worker 	} else
7638*8b26181fSAndroid Build Coastguard Worker 		tmp = a1->b;
7639*8b26181fSAndroid Build Coastguard Worker 
7640*8b26181fSAndroid Build Coastguard Worker 	if (tmp)
7641*8b26181fSAndroid Build Coastguard Worker 		gen_and(tmp, b);
7642*8b26181fSAndroid Build Coastguard Worker 
7643*8b26181fSAndroid Build Coastguard Worker 	return b;
7644*8b26181fSAndroid Build Coastguard Worker }
7645*8b26181fSAndroid Build Coastguard Worker 
7646*8b26181fSAndroid Build Coastguard Worker struct block *
gen_relation(compiler_state_t * cstate,int code,struct arth * a0,struct arth * a1,int reversed)7647*8b26181fSAndroid Build Coastguard Worker gen_relation(compiler_state_t *cstate, int code, struct arth *a0,
7648*8b26181fSAndroid Build Coastguard Worker     struct arth *a1, int reversed)
7649*8b26181fSAndroid Build Coastguard Worker {
7650*8b26181fSAndroid Build Coastguard Worker 	/*
7651*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7652*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7653*8b26181fSAndroid Build Coastguard Worker 	 */
7654*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7655*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7656*8b26181fSAndroid Build Coastguard Worker 
7657*8b26181fSAndroid Build Coastguard Worker 	return gen_relation_internal(cstate, code, a0, a1, reversed);
7658*8b26181fSAndroid Build Coastguard Worker }
7659*8b26181fSAndroid Build Coastguard Worker 
7660*8b26181fSAndroid Build Coastguard Worker struct arth *
gen_loadlen(compiler_state_t * cstate)7661*8b26181fSAndroid Build Coastguard Worker gen_loadlen(compiler_state_t *cstate)
7662*8b26181fSAndroid Build Coastguard Worker {
7663*8b26181fSAndroid Build Coastguard Worker 	int regno;
7664*8b26181fSAndroid Build Coastguard Worker 	struct arth *a;
7665*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
7666*8b26181fSAndroid Build Coastguard Worker 
7667*8b26181fSAndroid Build Coastguard Worker 	/*
7668*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7669*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7670*8b26181fSAndroid Build Coastguard Worker 	 */
7671*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7672*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7673*8b26181fSAndroid Build Coastguard Worker 
7674*8b26181fSAndroid Build Coastguard Worker 	regno = alloc_reg(cstate);
7675*8b26181fSAndroid Build Coastguard Worker 	a = (struct arth *)newchunk(cstate, sizeof(*a));
7676*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_LD|BPF_LEN);
7677*8b26181fSAndroid Build Coastguard Worker 	s->next = new_stmt(cstate, BPF_ST);
7678*8b26181fSAndroid Build Coastguard Worker 	s->next->s.k = regno;
7679*8b26181fSAndroid Build Coastguard Worker 	a->s = s;
7680*8b26181fSAndroid Build Coastguard Worker 	a->regno = regno;
7681*8b26181fSAndroid Build Coastguard Worker 
7682*8b26181fSAndroid Build Coastguard Worker 	return a;
7683*8b26181fSAndroid Build Coastguard Worker }
7684*8b26181fSAndroid Build Coastguard Worker 
7685*8b26181fSAndroid Build Coastguard Worker static struct arth *
gen_loadi_internal(compiler_state_t * cstate,bpf_u_int32 val)7686*8b26181fSAndroid Build Coastguard Worker gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val)
7687*8b26181fSAndroid Build Coastguard Worker {
7688*8b26181fSAndroid Build Coastguard Worker 	struct arth *a;
7689*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
7690*8b26181fSAndroid Build Coastguard Worker 	int reg;
7691*8b26181fSAndroid Build Coastguard Worker 
7692*8b26181fSAndroid Build Coastguard Worker 	a = (struct arth *)newchunk(cstate, sizeof(*a));
7693*8b26181fSAndroid Build Coastguard Worker 
7694*8b26181fSAndroid Build Coastguard Worker 	reg = alloc_reg(cstate);
7695*8b26181fSAndroid Build Coastguard Worker 
7696*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_LD|BPF_IMM);
7697*8b26181fSAndroid Build Coastguard Worker 	s->s.k = val;
7698*8b26181fSAndroid Build Coastguard Worker 	s->next = new_stmt(cstate, BPF_ST);
7699*8b26181fSAndroid Build Coastguard Worker 	s->next->s.k = reg;
7700*8b26181fSAndroid Build Coastguard Worker 	a->s = s;
7701*8b26181fSAndroid Build Coastguard Worker 	a->regno = reg;
7702*8b26181fSAndroid Build Coastguard Worker 
7703*8b26181fSAndroid Build Coastguard Worker 	return a;
7704*8b26181fSAndroid Build Coastguard Worker }
7705*8b26181fSAndroid Build Coastguard Worker 
7706*8b26181fSAndroid Build Coastguard Worker struct arth *
gen_loadi(compiler_state_t * cstate,bpf_u_int32 val)7707*8b26181fSAndroid Build Coastguard Worker gen_loadi(compiler_state_t *cstate, bpf_u_int32 val)
7708*8b26181fSAndroid Build Coastguard Worker {
7709*8b26181fSAndroid Build Coastguard Worker 	/*
7710*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7711*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7712*8b26181fSAndroid Build Coastguard Worker 	 */
7713*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7714*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7715*8b26181fSAndroid Build Coastguard Worker 
7716*8b26181fSAndroid Build Coastguard Worker 	return gen_loadi_internal(cstate, val);
7717*8b26181fSAndroid Build Coastguard Worker }
7718*8b26181fSAndroid Build Coastguard Worker 
7719*8b26181fSAndroid Build Coastguard Worker /*
7720*8b26181fSAndroid Build Coastguard Worker  * The a_arg dance is to avoid annoying whining by compilers that
7721*8b26181fSAndroid Build Coastguard Worker  * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7722*8b26181fSAndroid Build Coastguard Worker  * It's not *used* after setjmp returns.
7723*8b26181fSAndroid Build Coastguard Worker  */
7724*8b26181fSAndroid Build Coastguard Worker struct arth *
gen_neg(compiler_state_t * cstate,struct arth * a_arg)7725*8b26181fSAndroid Build Coastguard Worker gen_neg(compiler_state_t *cstate, struct arth *a_arg)
7726*8b26181fSAndroid Build Coastguard Worker {
7727*8b26181fSAndroid Build Coastguard Worker 	struct arth *a = a_arg;
7728*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
7729*8b26181fSAndroid Build Coastguard Worker 
7730*8b26181fSAndroid Build Coastguard Worker 	/*
7731*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7732*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7733*8b26181fSAndroid Build Coastguard Worker 	 */
7734*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7735*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7736*8b26181fSAndroid Build Coastguard Worker 
7737*8b26181fSAndroid Build Coastguard Worker 	s = xfer_to_a(cstate, a);
7738*8b26181fSAndroid Build Coastguard Worker 	sappend(a->s, s);
7739*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_ALU|BPF_NEG);
7740*8b26181fSAndroid Build Coastguard Worker 	s->s.k = 0;
7741*8b26181fSAndroid Build Coastguard Worker 	sappend(a->s, s);
7742*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_ST);
7743*8b26181fSAndroid Build Coastguard Worker 	s->s.k = a->regno;
7744*8b26181fSAndroid Build Coastguard Worker 	sappend(a->s, s);
7745*8b26181fSAndroid Build Coastguard Worker 
7746*8b26181fSAndroid Build Coastguard Worker 	return a;
7747*8b26181fSAndroid Build Coastguard Worker }
7748*8b26181fSAndroid Build Coastguard Worker 
7749*8b26181fSAndroid Build Coastguard Worker /*
7750*8b26181fSAndroid Build Coastguard Worker  * The a0_arg dance is to avoid annoying whining by compilers that
7751*8b26181fSAndroid Build Coastguard Worker  * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7752*8b26181fSAndroid Build Coastguard Worker  * It's not *used* after setjmp returns.
7753*8b26181fSAndroid Build Coastguard Worker  */
7754*8b26181fSAndroid Build Coastguard Worker struct arth *
gen_arth(compiler_state_t * cstate,int code,struct arth * a0_arg,struct arth * a1)7755*8b26181fSAndroid Build Coastguard Worker gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg,
7756*8b26181fSAndroid Build Coastguard Worker     struct arth *a1)
7757*8b26181fSAndroid Build Coastguard Worker {
7758*8b26181fSAndroid Build Coastguard Worker 	struct arth *a0 = a0_arg;
7759*8b26181fSAndroid Build Coastguard Worker 	struct slist *s0, *s1, *s2;
7760*8b26181fSAndroid Build Coastguard Worker 
7761*8b26181fSAndroid Build Coastguard Worker 	/*
7762*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7763*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7764*8b26181fSAndroid Build Coastguard Worker 	 */
7765*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7766*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7767*8b26181fSAndroid Build Coastguard Worker 
7768*8b26181fSAndroid Build Coastguard Worker 	/*
7769*8b26181fSAndroid Build Coastguard Worker 	 * Disallow division by, or modulus by, zero; we do this here
7770*8b26181fSAndroid Build Coastguard Worker 	 * so that it gets done even if the optimizer is disabled.
7771*8b26181fSAndroid Build Coastguard Worker 	 *
7772*8b26181fSAndroid Build Coastguard Worker 	 * Also disallow shifts by a value greater than 31; we do this
7773*8b26181fSAndroid Build Coastguard Worker 	 * here, for the same reason.
7774*8b26181fSAndroid Build Coastguard Worker 	 */
7775*8b26181fSAndroid Build Coastguard Worker 	if (code == BPF_DIV) {
7776*8b26181fSAndroid Build Coastguard Worker 		if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
7777*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "division by zero");
7778*8b26181fSAndroid Build Coastguard Worker 	} else if (code == BPF_MOD) {
7779*8b26181fSAndroid Build Coastguard Worker 		if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
7780*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "modulus by zero");
7781*8b26181fSAndroid Build Coastguard Worker 	} else if (code == BPF_LSH || code == BPF_RSH) {
7782*8b26181fSAndroid Build Coastguard Worker 		if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31)
7783*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "shift by more than 31 bits");
7784*8b26181fSAndroid Build Coastguard Worker 	}
7785*8b26181fSAndroid Build Coastguard Worker 	s0 = xfer_to_x(cstate, a1);
7786*8b26181fSAndroid Build Coastguard Worker 	s1 = xfer_to_a(cstate, a0);
7787*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);
7788*8b26181fSAndroid Build Coastguard Worker 
7789*8b26181fSAndroid Build Coastguard Worker 	sappend(s1, s2);
7790*8b26181fSAndroid Build Coastguard Worker 	sappend(s0, s1);
7791*8b26181fSAndroid Build Coastguard Worker 	sappend(a1->s, s0);
7792*8b26181fSAndroid Build Coastguard Worker 	sappend(a0->s, a1->s);
7793*8b26181fSAndroid Build Coastguard Worker 
7794*8b26181fSAndroid Build Coastguard Worker 	free_reg(cstate, a0->regno);
7795*8b26181fSAndroid Build Coastguard Worker 	free_reg(cstate, a1->regno);
7796*8b26181fSAndroid Build Coastguard Worker 
7797*8b26181fSAndroid Build Coastguard Worker 	s0 = new_stmt(cstate, BPF_ST);
7798*8b26181fSAndroid Build Coastguard Worker 	a0->regno = s0->s.k = alloc_reg(cstate);
7799*8b26181fSAndroid Build Coastguard Worker 	sappend(a0->s, s0);
7800*8b26181fSAndroid Build Coastguard Worker 
7801*8b26181fSAndroid Build Coastguard Worker 	return a0;
7802*8b26181fSAndroid Build Coastguard Worker }
7803*8b26181fSAndroid Build Coastguard Worker 
7804*8b26181fSAndroid Build Coastguard Worker /*
7805*8b26181fSAndroid Build Coastguard Worker  * Initialize the table of used registers and the current register.
7806*8b26181fSAndroid Build Coastguard Worker  */
7807*8b26181fSAndroid Build Coastguard Worker static void
init_regs(compiler_state_t * cstate)7808*8b26181fSAndroid Build Coastguard Worker init_regs(compiler_state_t *cstate)
7809*8b26181fSAndroid Build Coastguard Worker {
7810*8b26181fSAndroid Build Coastguard Worker 	cstate->curreg = 0;
7811*8b26181fSAndroid Build Coastguard Worker 	memset(cstate->regused, 0, sizeof cstate->regused);
7812*8b26181fSAndroid Build Coastguard Worker }
7813*8b26181fSAndroid Build Coastguard Worker 
7814*8b26181fSAndroid Build Coastguard Worker /*
7815*8b26181fSAndroid Build Coastguard Worker  * Return the next free register.
7816*8b26181fSAndroid Build Coastguard Worker  */
7817*8b26181fSAndroid Build Coastguard Worker static int
alloc_reg(compiler_state_t * cstate)7818*8b26181fSAndroid Build Coastguard Worker alloc_reg(compiler_state_t *cstate)
7819*8b26181fSAndroid Build Coastguard Worker {
7820*8b26181fSAndroid Build Coastguard Worker 	int n = BPF_MEMWORDS;
7821*8b26181fSAndroid Build Coastguard Worker 
7822*8b26181fSAndroid Build Coastguard Worker 	while (--n >= 0) {
7823*8b26181fSAndroid Build Coastguard Worker 		if (cstate->regused[cstate->curreg])
7824*8b26181fSAndroid Build Coastguard Worker 			cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;
7825*8b26181fSAndroid Build Coastguard Worker 		else {
7826*8b26181fSAndroid Build Coastguard Worker 			cstate->regused[cstate->curreg] = 1;
7827*8b26181fSAndroid Build Coastguard Worker 			return cstate->curreg;
7828*8b26181fSAndroid Build Coastguard Worker 		}
7829*8b26181fSAndroid Build Coastguard Worker 	}
7830*8b26181fSAndroid Build Coastguard Worker 	bpf_error(cstate, "too many registers needed to evaluate expression");
7831*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
7832*8b26181fSAndroid Build Coastguard Worker }
7833*8b26181fSAndroid Build Coastguard Worker 
7834*8b26181fSAndroid Build Coastguard Worker /*
7835*8b26181fSAndroid Build Coastguard Worker  * Return a register to the table so it can
7836*8b26181fSAndroid Build Coastguard Worker  * be used later.
7837*8b26181fSAndroid Build Coastguard Worker  */
7838*8b26181fSAndroid Build Coastguard Worker static void
free_reg(compiler_state_t * cstate,int n)7839*8b26181fSAndroid Build Coastguard Worker free_reg(compiler_state_t *cstate, int n)
7840*8b26181fSAndroid Build Coastguard Worker {
7841*8b26181fSAndroid Build Coastguard Worker 	cstate->regused[n] = 0;
7842*8b26181fSAndroid Build Coastguard Worker }
7843*8b26181fSAndroid Build Coastguard Worker 
7844*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_len(compiler_state_t * cstate,int jmp,int n)7845*8b26181fSAndroid Build Coastguard Worker gen_len(compiler_state_t *cstate, int jmp, int n)
7846*8b26181fSAndroid Build Coastguard Worker {
7847*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
7848*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
7849*8b26181fSAndroid Build Coastguard Worker 
7850*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_LD|BPF_LEN);
7851*8b26181fSAndroid Build Coastguard Worker 	b = new_block(cstate, JMP(jmp));
7852*8b26181fSAndroid Build Coastguard Worker 	b->stmts = s;
7853*8b26181fSAndroid Build Coastguard Worker 	b->s.k = n;
7854*8b26181fSAndroid Build Coastguard Worker 
7855*8b26181fSAndroid Build Coastguard Worker 	return b;
7856*8b26181fSAndroid Build Coastguard Worker }
7857*8b26181fSAndroid Build Coastguard Worker 
7858*8b26181fSAndroid Build Coastguard Worker struct block *
gen_greater(compiler_state_t * cstate,int n)7859*8b26181fSAndroid Build Coastguard Worker gen_greater(compiler_state_t *cstate, int n)
7860*8b26181fSAndroid Build Coastguard Worker {
7861*8b26181fSAndroid Build Coastguard Worker 	/*
7862*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7863*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7864*8b26181fSAndroid Build Coastguard Worker 	 */
7865*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7866*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7867*8b26181fSAndroid Build Coastguard Worker 
7868*8b26181fSAndroid Build Coastguard Worker 	return gen_len(cstate, BPF_JGE, n);
7869*8b26181fSAndroid Build Coastguard Worker }
7870*8b26181fSAndroid Build Coastguard Worker 
7871*8b26181fSAndroid Build Coastguard Worker /*
7872*8b26181fSAndroid Build Coastguard Worker  * Actually, this is less than or equal.
7873*8b26181fSAndroid Build Coastguard Worker  */
7874*8b26181fSAndroid Build Coastguard Worker struct block *
gen_less(compiler_state_t * cstate,int n)7875*8b26181fSAndroid Build Coastguard Worker gen_less(compiler_state_t *cstate, int n)
7876*8b26181fSAndroid Build Coastguard Worker {
7877*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
7878*8b26181fSAndroid Build Coastguard Worker 
7879*8b26181fSAndroid Build Coastguard Worker 	/*
7880*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7881*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7882*8b26181fSAndroid Build Coastguard Worker 	 */
7883*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7884*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7885*8b26181fSAndroid Build Coastguard Worker 
7886*8b26181fSAndroid Build Coastguard Worker 	b = gen_len(cstate, BPF_JGT, n);
7887*8b26181fSAndroid Build Coastguard Worker 	gen_not(b);
7888*8b26181fSAndroid Build Coastguard Worker 
7889*8b26181fSAndroid Build Coastguard Worker 	return b;
7890*8b26181fSAndroid Build Coastguard Worker }
7891*8b26181fSAndroid Build Coastguard Worker 
7892*8b26181fSAndroid Build Coastguard Worker /*
7893*8b26181fSAndroid Build Coastguard Worker  * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7894*8b26181fSAndroid Build Coastguard Worker  * the beginning of the link-layer header.
7895*8b26181fSAndroid Build Coastguard Worker  * XXX - that means you can't test values in the radiotap header, but
7896*8b26181fSAndroid Build Coastguard Worker  * as that header is difficult if not impossible to parse generally
7897*8b26181fSAndroid Build Coastguard Worker  * without a loop, that might not be a severe problem.  A new keyword
7898*8b26181fSAndroid Build Coastguard Worker  * "radio" could be added for that, although what you'd really want
7899*8b26181fSAndroid Build Coastguard Worker  * would be a way of testing particular radio header values, which
7900*8b26181fSAndroid Build Coastguard Worker  * would generate code appropriate to the radio header in question.
7901*8b26181fSAndroid Build Coastguard Worker  */
7902*8b26181fSAndroid Build Coastguard Worker struct block *
gen_byteop(compiler_state_t * cstate,int op,int idx,bpf_u_int32 val)7903*8b26181fSAndroid Build Coastguard Worker gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val)
7904*8b26181fSAndroid Build Coastguard Worker {
7905*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
7906*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
7907*8b26181fSAndroid Build Coastguard Worker 
7908*8b26181fSAndroid Build Coastguard Worker 	/*
7909*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7910*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7911*8b26181fSAndroid Build Coastguard Worker 	 */
7912*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7913*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7914*8b26181fSAndroid Build Coastguard Worker 
7915*8b26181fSAndroid Build Coastguard Worker 	switch (op) {
7916*8b26181fSAndroid Build Coastguard Worker 	default:
7917*8b26181fSAndroid Build Coastguard Worker 		abort();
7918*8b26181fSAndroid Build Coastguard Worker 
7919*8b26181fSAndroid Build Coastguard Worker 	case '=':
7920*8b26181fSAndroid Build Coastguard Worker 		return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7921*8b26181fSAndroid Build Coastguard Worker 
7922*8b26181fSAndroid Build Coastguard Worker 	case '<':
7923*8b26181fSAndroid Build Coastguard Worker 		b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7924*8b26181fSAndroid Build Coastguard Worker 		return b;
7925*8b26181fSAndroid Build Coastguard Worker 
7926*8b26181fSAndroid Build Coastguard Worker 	case '>':
7927*8b26181fSAndroid Build Coastguard Worker 		b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7928*8b26181fSAndroid Build Coastguard Worker 		return b;
7929*8b26181fSAndroid Build Coastguard Worker 
7930*8b26181fSAndroid Build Coastguard Worker 	case '|':
7931*8b26181fSAndroid Build Coastguard Worker 		s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);
7932*8b26181fSAndroid Build Coastguard Worker 		break;
7933*8b26181fSAndroid Build Coastguard Worker 
7934*8b26181fSAndroid Build Coastguard Worker 	case '&':
7935*8b26181fSAndroid Build Coastguard Worker 		s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
7936*8b26181fSAndroid Build Coastguard Worker 		break;
7937*8b26181fSAndroid Build Coastguard Worker 	}
7938*8b26181fSAndroid Build Coastguard Worker 	s->s.k = val;
7939*8b26181fSAndroid Build Coastguard Worker 	b = new_block(cstate, JMP(BPF_JEQ));
7940*8b26181fSAndroid Build Coastguard Worker 	b->stmts = s;
7941*8b26181fSAndroid Build Coastguard Worker 	gen_not(b);
7942*8b26181fSAndroid Build Coastguard Worker 
7943*8b26181fSAndroid Build Coastguard Worker 	return b;
7944*8b26181fSAndroid Build Coastguard Worker }
7945*8b26181fSAndroid Build Coastguard Worker 
7946*8b26181fSAndroid Build Coastguard Worker static const u_char abroadcast[] = { 0x0 };
7947*8b26181fSAndroid Build Coastguard Worker 
7948*8b26181fSAndroid Build Coastguard Worker struct block *
gen_broadcast(compiler_state_t * cstate,int proto)7949*8b26181fSAndroid Build Coastguard Worker gen_broadcast(compiler_state_t *cstate, int proto)
7950*8b26181fSAndroid Build Coastguard Worker {
7951*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 hostmask;
7952*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1, *b2;
7953*8b26181fSAndroid Build Coastguard Worker 	static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7954*8b26181fSAndroid Build Coastguard Worker 
7955*8b26181fSAndroid Build Coastguard Worker 	/*
7956*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
7957*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
7958*8b26181fSAndroid Build Coastguard Worker 	 */
7959*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
7960*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
7961*8b26181fSAndroid Build Coastguard Worker 
7962*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
7963*8b26181fSAndroid Build Coastguard Worker 
7964*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
7965*8b26181fSAndroid Build Coastguard Worker 	case Q_LINK:
7966*8b26181fSAndroid Build Coastguard Worker 		switch (cstate->linktype) {
7967*8b26181fSAndroid Build Coastguard Worker 		case DLT_ARCNET:
7968*8b26181fSAndroid Build Coastguard Worker 		case DLT_ARCNET_LINUX:
7969*8b26181fSAndroid Build Coastguard Worker 			return gen_ahostop(cstate, abroadcast, Q_DST);
7970*8b26181fSAndroid Build Coastguard Worker 		case DLT_EN10MB:
7971*8b26181fSAndroid Build Coastguard Worker 		case DLT_NETANALYZER:
7972*8b26181fSAndroid Build Coastguard Worker 		case DLT_NETANALYZER_TRANSPARENT:
7973*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_prevlinkhdr_check(cstate);
7974*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_ehostop(cstate, ebroadcast, Q_DST);
7975*8b26181fSAndroid Build Coastguard Worker 			if (b1 != NULL)
7976*8b26181fSAndroid Build Coastguard Worker 				gen_and(b1, b0);
7977*8b26181fSAndroid Build Coastguard Worker 			return b0;
7978*8b26181fSAndroid Build Coastguard Worker 		case DLT_FDDI:
7979*8b26181fSAndroid Build Coastguard Worker 			return gen_fhostop(cstate, ebroadcast, Q_DST);
7980*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802:
7981*8b26181fSAndroid Build Coastguard Worker 			return gen_thostop(cstate, ebroadcast, Q_DST);
7982*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11:
7983*8b26181fSAndroid Build Coastguard Worker 		case DLT_PRISM_HEADER:
7984*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11_RADIO_AVS:
7985*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11_RADIO:
7986*8b26181fSAndroid Build Coastguard Worker 		case DLT_PPI:
7987*8b26181fSAndroid Build Coastguard Worker 			return gen_wlanhostop(cstate, ebroadcast, Q_DST);
7988*8b26181fSAndroid Build Coastguard Worker 		case DLT_IP_OVER_FC:
7989*8b26181fSAndroid Build Coastguard Worker 			return gen_ipfchostop(cstate, ebroadcast, Q_DST);
7990*8b26181fSAndroid Build Coastguard Worker 		default:
7991*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "not a broadcast link");
7992*8b26181fSAndroid Build Coastguard Worker 		}
7993*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
7994*8b26181fSAndroid Build Coastguard Worker 
7995*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
7996*8b26181fSAndroid Build Coastguard Worker 		/*
7997*8b26181fSAndroid Build Coastguard Worker 		 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7998*8b26181fSAndroid Build Coastguard Worker 		 * as an indication that we don't know the netmask, and fail
7999*8b26181fSAndroid Build Coastguard Worker 		 * in that case.
8000*8b26181fSAndroid Build Coastguard Worker 		 */
8001*8b26181fSAndroid Build Coastguard Worker 		if (cstate->netmask == PCAP_NETMASK_UNKNOWN)
8002*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");
8003*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_linktype(cstate, ETHERTYPE_IP);
8004*8b26181fSAndroid Build Coastguard Worker 		hostmask = ~cstate->netmask;
8005*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask);
8006*8b26181fSAndroid Build Coastguard Worker 		b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,
8007*8b26181fSAndroid Build Coastguard Worker 			      ~0 & hostmask, hostmask);
8008*8b26181fSAndroid Build Coastguard Worker 		gen_or(b1, b2);
8009*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b2);
8010*8b26181fSAndroid Build Coastguard Worker 		return b2;
8011*8b26181fSAndroid Build Coastguard Worker 	}
8012*8b26181fSAndroid Build Coastguard Worker 	bpf_error(cstate, "only link-layer/IP broadcast filters supported");
8013*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
8014*8b26181fSAndroid Build Coastguard Worker }
8015*8b26181fSAndroid Build Coastguard Worker 
8016*8b26181fSAndroid Build Coastguard Worker /*
8017*8b26181fSAndroid Build Coastguard Worker  * Generate code to test the low-order bit of a MAC address (that's
8018*8b26181fSAndroid Build Coastguard Worker  * the bottom bit of the *first* byte).
8019*8b26181fSAndroid Build Coastguard Worker  */
8020*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_mac_multicast(compiler_state_t * cstate,int offset)8021*8b26181fSAndroid Build Coastguard Worker gen_mac_multicast(compiler_state_t *cstate, int offset)
8022*8b26181fSAndroid Build Coastguard Worker {
8023*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0;
8024*8b26181fSAndroid Build Coastguard Worker 	register struct slist *s;
8025*8b26181fSAndroid Build Coastguard Worker 
8026*8b26181fSAndroid Build Coastguard Worker 	/* link[offset] & 1 != 0 */
8027*8b26181fSAndroid Build Coastguard Worker 	s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);
8028*8b26181fSAndroid Build Coastguard Worker 	b0 = new_block(cstate, JMP(BPF_JSET));
8029*8b26181fSAndroid Build Coastguard Worker 	b0->s.k = 1;
8030*8b26181fSAndroid Build Coastguard Worker 	b0->stmts = s;
8031*8b26181fSAndroid Build Coastguard Worker 	return b0;
8032*8b26181fSAndroid Build Coastguard Worker }
8033*8b26181fSAndroid Build Coastguard Worker 
8034*8b26181fSAndroid Build Coastguard Worker struct block *
gen_multicast(compiler_state_t * cstate,int proto)8035*8b26181fSAndroid Build Coastguard Worker gen_multicast(compiler_state_t *cstate, int proto)
8036*8b26181fSAndroid Build Coastguard Worker {
8037*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0, *b1, *b2;
8038*8b26181fSAndroid Build Coastguard Worker 	register struct slist *s;
8039*8b26181fSAndroid Build Coastguard Worker 
8040*8b26181fSAndroid Build Coastguard Worker 	/*
8041*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8042*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8043*8b26181fSAndroid Build Coastguard Worker 	 */
8044*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8045*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8046*8b26181fSAndroid Build Coastguard Worker 
8047*8b26181fSAndroid Build Coastguard Worker 	switch (proto) {
8048*8b26181fSAndroid Build Coastguard Worker 
8049*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
8050*8b26181fSAndroid Build Coastguard Worker 	case Q_LINK:
8051*8b26181fSAndroid Build Coastguard Worker 		switch (cstate->linktype) {
8052*8b26181fSAndroid Build Coastguard Worker 		case DLT_ARCNET:
8053*8b26181fSAndroid Build Coastguard Worker 		case DLT_ARCNET_LINUX:
8054*8b26181fSAndroid Build Coastguard Worker 			/* all ARCnet multicasts use the same address */
8055*8b26181fSAndroid Build Coastguard Worker 			return gen_ahostop(cstate, abroadcast, Q_DST);
8056*8b26181fSAndroid Build Coastguard Worker 		case DLT_EN10MB:
8057*8b26181fSAndroid Build Coastguard Worker 		case DLT_NETANALYZER:
8058*8b26181fSAndroid Build Coastguard Worker 		case DLT_NETANALYZER_TRANSPARENT:
8059*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_prevlinkhdr_check(cstate);
8060*8b26181fSAndroid Build Coastguard Worker 			/* ether[0] & 1 != 0 */
8061*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_mac_multicast(cstate, 0);
8062*8b26181fSAndroid Build Coastguard Worker 			if (b1 != NULL)
8063*8b26181fSAndroid Build Coastguard Worker 				gen_and(b1, b0);
8064*8b26181fSAndroid Build Coastguard Worker 			return b0;
8065*8b26181fSAndroid Build Coastguard Worker 		case DLT_FDDI:
8066*8b26181fSAndroid Build Coastguard Worker 			/*
8067*8b26181fSAndroid Build Coastguard Worker 			 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8068*8b26181fSAndroid Build Coastguard Worker 			 *
8069*8b26181fSAndroid Build Coastguard Worker 			 * XXX - was that referring to bit-order issues?
8070*8b26181fSAndroid Build Coastguard Worker 			 */
8071*8b26181fSAndroid Build Coastguard Worker 			/* fddi[1] & 1 != 0 */
8072*8b26181fSAndroid Build Coastguard Worker 			return gen_mac_multicast(cstate, 1);
8073*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802:
8074*8b26181fSAndroid Build Coastguard Worker 			/* tr[2] & 1 != 0 */
8075*8b26181fSAndroid Build Coastguard Worker 			return gen_mac_multicast(cstate, 2);
8076*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11:
8077*8b26181fSAndroid Build Coastguard Worker 		case DLT_PRISM_HEADER:
8078*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11_RADIO_AVS:
8079*8b26181fSAndroid Build Coastguard Worker 		case DLT_IEEE802_11_RADIO:
8080*8b26181fSAndroid Build Coastguard Worker 		case DLT_PPI:
8081*8b26181fSAndroid Build Coastguard Worker 			/*
8082*8b26181fSAndroid Build Coastguard Worker 			 * Oh, yuk.
8083*8b26181fSAndroid Build Coastguard Worker 			 *
8084*8b26181fSAndroid Build Coastguard Worker 			 *	For control frames, there is no DA.
8085*8b26181fSAndroid Build Coastguard Worker 			 *
8086*8b26181fSAndroid Build Coastguard Worker 			 *	For management frames, DA is at an
8087*8b26181fSAndroid Build Coastguard Worker 			 *	offset of 4 from the beginning of
8088*8b26181fSAndroid Build Coastguard Worker 			 *	the packet.
8089*8b26181fSAndroid Build Coastguard Worker 			 *
8090*8b26181fSAndroid Build Coastguard Worker 			 *	For data frames, DA is at an offset
8091*8b26181fSAndroid Build Coastguard Worker 			 *	of 4 from the beginning of the packet
8092*8b26181fSAndroid Build Coastguard Worker 			 *	if To DS is clear and at an offset of
8093*8b26181fSAndroid Build Coastguard Worker 			 *	16 from the beginning of the packet
8094*8b26181fSAndroid Build Coastguard Worker 			 *	if To DS is set.
8095*8b26181fSAndroid Build Coastguard Worker 			 */
8096*8b26181fSAndroid Build Coastguard Worker 
8097*8b26181fSAndroid Build Coastguard Worker 			/*
8098*8b26181fSAndroid Build Coastguard Worker 			 * Generate the tests to be done for data frames.
8099*8b26181fSAndroid Build Coastguard Worker 			 *
8100*8b26181fSAndroid Build Coastguard Worker 			 * First, check for To DS set, i.e. "link[1] & 0x01".
8101*8b26181fSAndroid Build Coastguard Worker 			 */
8102*8b26181fSAndroid Build Coastguard Worker 			s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8103*8b26181fSAndroid Build Coastguard Worker 			b1 = new_block(cstate, JMP(BPF_JSET));
8104*8b26181fSAndroid Build Coastguard Worker 			b1->s.k = 0x01;	/* To DS */
8105*8b26181fSAndroid Build Coastguard Worker 			b1->stmts = s;
8106*8b26181fSAndroid Build Coastguard Worker 
8107*8b26181fSAndroid Build Coastguard Worker 			/*
8108*8b26181fSAndroid Build Coastguard Worker 			 * If To DS is set, the DA is at 16.
8109*8b26181fSAndroid Build Coastguard Worker 			 */
8110*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_mac_multicast(cstate, 16);
8111*8b26181fSAndroid Build Coastguard Worker 			gen_and(b1, b0);
8112*8b26181fSAndroid Build Coastguard Worker 
8113*8b26181fSAndroid Build Coastguard Worker 			/*
8114*8b26181fSAndroid Build Coastguard Worker 			 * Now, check for To DS not set, i.e. check
8115*8b26181fSAndroid Build Coastguard Worker 			 * "!(link[1] & 0x01)".
8116*8b26181fSAndroid Build Coastguard Worker 			 */
8117*8b26181fSAndroid Build Coastguard Worker 			s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8118*8b26181fSAndroid Build Coastguard Worker 			b2 = new_block(cstate, JMP(BPF_JSET));
8119*8b26181fSAndroid Build Coastguard Worker 			b2->s.k = 0x01;	/* To DS */
8120*8b26181fSAndroid Build Coastguard Worker 			b2->stmts = s;
8121*8b26181fSAndroid Build Coastguard Worker 			gen_not(b2);
8122*8b26181fSAndroid Build Coastguard Worker 
8123*8b26181fSAndroid Build Coastguard Worker 			/*
8124*8b26181fSAndroid Build Coastguard Worker 			 * If To DS is not set, the DA is at 4.
8125*8b26181fSAndroid Build Coastguard Worker 			 */
8126*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_mac_multicast(cstate, 4);
8127*8b26181fSAndroid Build Coastguard Worker 			gen_and(b2, b1);
8128*8b26181fSAndroid Build Coastguard Worker 
8129*8b26181fSAndroid Build Coastguard Worker 			/*
8130*8b26181fSAndroid Build Coastguard Worker 			 * Now OR together the last two checks.  That gives
8131*8b26181fSAndroid Build Coastguard Worker 			 * the complete set of checks for data frames.
8132*8b26181fSAndroid Build Coastguard Worker 			 */
8133*8b26181fSAndroid Build Coastguard Worker 			gen_or(b1, b0);
8134*8b26181fSAndroid Build Coastguard Worker 
8135*8b26181fSAndroid Build Coastguard Worker 			/*
8136*8b26181fSAndroid Build Coastguard Worker 			 * Now check for a data frame.
8137*8b26181fSAndroid Build Coastguard Worker 			 * I.e, check "link[0] & 0x08".
8138*8b26181fSAndroid Build Coastguard Worker 			 */
8139*8b26181fSAndroid Build Coastguard Worker 			s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8140*8b26181fSAndroid Build Coastguard Worker 			b1 = new_block(cstate, JMP(BPF_JSET));
8141*8b26181fSAndroid Build Coastguard Worker 			b1->s.k = 0x08;
8142*8b26181fSAndroid Build Coastguard Worker 			b1->stmts = s;
8143*8b26181fSAndroid Build Coastguard Worker 
8144*8b26181fSAndroid Build Coastguard Worker 			/*
8145*8b26181fSAndroid Build Coastguard Worker 			 * AND that with the checks done for data frames.
8146*8b26181fSAndroid Build Coastguard Worker 			 */
8147*8b26181fSAndroid Build Coastguard Worker 			gen_and(b1, b0);
8148*8b26181fSAndroid Build Coastguard Worker 
8149*8b26181fSAndroid Build Coastguard Worker 			/*
8150*8b26181fSAndroid Build Coastguard Worker 			 * If the high-order bit of the type value is 0, this
8151*8b26181fSAndroid Build Coastguard Worker 			 * is a management frame.
8152*8b26181fSAndroid Build Coastguard Worker 			 * I.e, check "!(link[0] & 0x08)".
8153*8b26181fSAndroid Build Coastguard Worker 			 */
8154*8b26181fSAndroid Build Coastguard Worker 			s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8155*8b26181fSAndroid Build Coastguard Worker 			b2 = new_block(cstate, JMP(BPF_JSET));
8156*8b26181fSAndroid Build Coastguard Worker 			b2->s.k = 0x08;
8157*8b26181fSAndroid Build Coastguard Worker 			b2->stmts = s;
8158*8b26181fSAndroid Build Coastguard Worker 			gen_not(b2);
8159*8b26181fSAndroid Build Coastguard Worker 
8160*8b26181fSAndroid Build Coastguard Worker 			/*
8161*8b26181fSAndroid Build Coastguard Worker 			 * For management frames, the DA is at 4.
8162*8b26181fSAndroid Build Coastguard Worker 			 */
8163*8b26181fSAndroid Build Coastguard Worker 			b1 = gen_mac_multicast(cstate, 4);
8164*8b26181fSAndroid Build Coastguard Worker 			gen_and(b2, b1);
8165*8b26181fSAndroid Build Coastguard Worker 
8166*8b26181fSAndroid Build Coastguard Worker 			/*
8167*8b26181fSAndroid Build Coastguard Worker 			 * OR that with the checks done for data frames.
8168*8b26181fSAndroid Build Coastguard Worker 			 * That gives the checks done for management and
8169*8b26181fSAndroid Build Coastguard Worker 			 * data frames.
8170*8b26181fSAndroid Build Coastguard Worker 			 */
8171*8b26181fSAndroid Build Coastguard Worker 			gen_or(b1, b0);
8172*8b26181fSAndroid Build Coastguard Worker 
8173*8b26181fSAndroid Build Coastguard Worker 			/*
8174*8b26181fSAndroid Build Coastguard Worker 			 * If the low-order bit of the type value is 1,
8175*8b26181fSAndroid Build Coastguard Worker 			 * this is either a control frame or a frame
8176*8b26181fSAndroid Build Coastguard Worker 			 * with a reserved type, and thus not a
8177*8b26181fSAndroid Build Coastguard Worker 			 * frame with an SA.
8178*8b26181fSAndroid Build Coastguard Worker 			 *
8179*8b26181fSAndroid Build Coastguard Worker 			 * I.e., check "!(link[0] & 0x04)".
8180*8b26181fSAndroid Build Coastguard Worker 			 */
8181*8b26181fSAndroid Build Coastguard Worker 			s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8182*8b26181fSAndroid Build Coastguard Worker 			b1 = new_block(cstate, JMP(BPF_JSET));
8183*8b26181fSAndroid Build Coastguard Worker 			b1->s.k = 0x04;
8184*8b26181fSAndroid Build Coastguard Worker 			b1->stmts = s;
8185*8b26181fSAndroid Build Coastguard Worker 			gen_not(b1);
8186*8b26181fSAndroid Build Coastguard Worker 
8187*8b26181fSAndroid Build Coastguard Worker 			/*
8188*8b26181fSAndroid Build Coastguard Worker 			 * AND that with the checks for data and management
8189*8b26181fSAndroid Build Coastguard Worker 			 * frames.
8190*8b26181fSAndroid Build Coastguard Worker 			 */
8191*8b26181fSAndroid Build Coastguard Worker 			gen_and(b1, b0);
8192*8b26181fSAndroid Build Coastguard Worker 			return b0;
8193*8b26181fSAndroid Build Coastguard Worker 		case DLT_IP_OVER_FC:
8194*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_mac_multicast(cstate, 2);
8195*8b26181fSAndroid Build Coastguard Worker 			return b0;
8196*8b26181fSAndroid Build Coastguard Worker 		default:
8197*8b26181fSAndroid Build Coastguard Worker 			break;
8198*8b26181fSAndroid Build Coastguard Worker 		}
8199*8b26181fSAndroid Build Coastguard Worker 		/* Link not known to support multicasts */
8200*8b26181fSAndroid Build Coastguard Worker 		break;
8201*8b26181fSAndroid Build Coastguard Worker 
8202*8b26181fSAndroid Build Coastguard Worker 	case Q_IP:
8203*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_linktype(cstate, ETHERTYPE_IP);
8204*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, 224);
8205*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
8206*8b26181fSAndroid Build Coastguard Worker 		return b1;
8207*8b26181fSAndroid Build Coastguard Worker 
8208*8b26181fSAndroid Build Coastguard Worker 	case Q_IPV6:
8209*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
8210*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255);
8211*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
8212*8b26181fSAndroid Build Coastguard Worker 		return b1;
8213*8b26181fSAndroid Build Coastguard Worker 	}
8214*8b26181fSAndroid Build Coastguard Worker 	bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8215*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
8216*8b26181fSAndroid Build Coastguard Worker }
8217*8b26181fSAndroid Build Coastguard Worker 
8218*8b26181fSAndroid Build Coastguard Worker struct block *
gen_ifindex(compiler_state_t * cstate,int ifindex)8219*8b26181fSAndroid Build Coastguard Worker gen_ifindex(compiler_state_t *cstate, int ifindex)
8220*8b26181fSAndroid Build Coastguard Worker {
8221*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0;
8222*8b26181fSAndroid Build Coastguard Worker 
8223*8b26181fSAndroid Build Coastguard Worker 	/*
8224*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8225*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8226*8b26181fSAndroid Build Coastguard Worker 	 */
8227*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8228*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8229*8b26181fSAndroid Build Coastguard Worker 
8230*8b26181fSAndroid Build Coastguard Worker 	/*
8231*8b26181fSAndroid Build Coastguard Worker 	 * Only some data link types support ifindex qualifiers.
8232*8b26181fSAndroid Build Coastguard Worker 	 */
8233*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
8234*8b26181fSAndroid Build Coastguard Worker 	case DLT_LINUX_SLL2:
8235*8b26181fSAndroid Build Coastguard Worker 		/* match packets on this interface */
8236*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex);
8237*8b26181fSAndroid Build Coastguard Worker 		break;
8238*8b26181fSAndroid Build Coastguard Worker         default:
8239*8b26181fSAndroid Build Coastguard Worker #if defined(linux)
8240*8b26181fSAndroid Build Coastguard Worker 		/*
8241*8b26181fSAndroid Build Coastguard Worker 		 * This is Linux; we require PF_PACKET support.
8242*8b26181fSAndroid Build Coastguard Worker 		 * If this is a *live* capture, we can look at
8243*8b26181fSAndroid Build Coastguard Worker 		 * special meta-data in the filter expression;
8244*8b26181fSAndroid Build Coastguard Worker 		 * if it's a savefile, we can't.
8245*8b26181fSAndroid Build Coastguard Worker 		 */
8246*8b26181fSAndroid Build Coastguard Worker 		if (cstate->bpf_pcap->rfile != NULL) {
8247*8b26181fSAndroid Build Coastguard Worker 			/* We have a FILE *, so this is a savefile */
8248*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "ifindex not supported on %s when reading savefiles",
8249*8b26181fSAndroid Build Coastguard Worker 			    pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8250*8b26181fSAndroid Build Coastguard Worker 			b0 = NULL;
8251*8b26181fSAndroid Build Coastguard Worker 			/*NOTREACHED*/
8252*8b26181fSAndroid Build Coastguard Worker 		}
8253*8b26181fSAndroid Build Coastguard Worker 		/* match ifindex */
8254*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W,
8255*8b26181fSAndroid Build Coastguard Worker 		             ifindex);
8256*8b26181fSAndroid Build Coastguard Worker #else /* defined(linux) */
8257*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "ifindex not supported on %s",
8258*8b26181fSAndroid Build Coastguard Worker 		    pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8259*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8260*8b26181fSAndroid Build Coastguard Worker #endif /* defined(linux) */
8261*8b26181fSAndroid Build Coastguard Worker 	}
8262*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8263*8b26181fSAndroid Build Coastguard Worker }
8264*8b26181fSAndroid Build Coastguard Worker 
8265*8b26181fSAndroid Build Coastguard Worker /*
8266*8b26181fSAndroid Build Coastguard Worker  * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8267*8b26181fSAndroid Build Coastguard Worker  * Outbound traffic is sent by this machine, while inbound traffic is
8268*8b26181fSAndroid Build Coastguard Worker  * sent by a remote machine (and may include packets destined for a
8269*8b26181fSAndroid Build Coastguard Worker  * unicast or multicast link-layer address we are not subscribing to).
8270*8b26181fSAndroid Build Coastguard Worker  * These are the same definitions implemented by pcap_setdirection().
8271*8b26181fSAndroid Build Coastguard Worker  * Capturing only unicast traffic destined for this host is probably
8272*8b26181fSAndroid Build Coastguard Worker  * better accomplished using a higher-layer filter.
8273*8b26181fSAndroid Build Coastguard Worker  */
8274*8b26181fSAndroid Build Coastguard Worker struct block *
gen_inbound(compiler_state_t * cstate,int dir)8275*8b26181fSAndroid Build Coastguard Worker gen_inbound(compiler_state_t *cstate, int dir)
8276*8b26181fSAndroid Build Coastguard Worker {
8277*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0;
8278*8b26181fSAndroid Build Coastguard Worker 
8279*8b26181fSAndroid Build Coastguard Worker 	/*
8280*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8281*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8282*8b26181fSAndroid Build Coastguard Worker 	 */
8283*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8284*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8285*8b26181fSAndroid Build Coastguard Worker 
8286*8b26181fSAndroid Build Coastguard Worker 	/*
8287*8b26181fSAndroid Build Coastguard Worker 	 * Only some data link types support inbound/outbound qualifiers.
8288*8b26181fSAndroid Build Coastguard Worker 	 */
8289*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
8290*8b26181fSAndroid Build Coastguard Worker 	case DLT_SLIP:
8291*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_relation_internal(cstate, BPF_JEQ,
8292*8b26181fSAndroid Build Coastguard Worker 			  gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1),
8293*8b26181fSAndroid Build Coastguard Worker 			  gen_loadi_internal(cstate, 0),
8294*8b26181fSAndroid Build Coastguard Worker 			  dir);
8295*8b26181fSAndroid Build Coastguard Worker 		break;
8296*8b26181fSAndroid Build Coastguard Worker 
8297*8b26181fSAndroid Build Coastguard Worker 	case DLT_IPNET:
8298*8b26181fSAndroid Build Coastguard Worker 		if (dir) {
8299*8b26181fSAndroid Build Coastguard Worker 			/* match outgoing packets */
8300*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);
8301*8b26181fSAndroid Build Coastguard Worker 		} else {
8302*8b26181fSAndroid Build Coastguard Worker 			/* match incoming packets */
8303*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);
8304*8b26181fSAndroid Build Coastguard Worker 		}
8305*8b26181fSAndroid Build Coastguard Worker 		break;
8306*8b26181fSAndroid Build Coastguard Worker 
8307*8b26181fSAndroid Build Coastguard Worker 	case DLT_LINUX_SLL:
8308*8b26181fSAndroid Build Coastguard Worker 		/* match outgoing packets */
8309*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
8310*8b26181fSAndroid Build Coastguard Worker 		if (!dir) {
8311*8b26181fSAndroid Build Coastguard Worker 			/* to filter on inbound traffic, invert the match */
8312*8b26181fSAndroid Build Coastguard Worker 			gen_not(b0);
8313*8b26181fSAndroid Build Coastguard Worker 		}
8314*8b26181fSAndroid Build Coastguard Worker 		break;
8315*8b26181fSAndroid Build Coastguard Worker 
8316*8b26181fSAndroid Build Coastguard Worker 	case DLT_LINUX_SLL2:
8317*8b26181fSAndroid Build Coastguard Worker 		/* match outgoing packets */
8318*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING);
8319*8b26181fSAndroid Build Coastguard Worker 		if (!dir) {
8320*8b26181fSAndroid Build Coastguard Worker 			/* to filter on inbound traffic, invert the match */
8321*8b26181fSAndroid Build Coastguard Worker 			gen_not(b0);
8322*8b26181fSAndroid Build Coastguard Worker 		}
8323*8b26181fSAndroid Build Coastguard Worker 		break;
8324*8b26181fSAndroid Build Coastguard Worker 
8325*8b26181fSAndroid Build Coastguard Worker 	case DLT_PFLOG:
8326*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
8327*8b26181fSAndroid Build Coastguard Worker 		    ((dir == 0) ? PF_IN : PF_OUT));
8328*8b26181fSAndroid Build Coastguard Worker 		break;
8329*8b26181fSAndroid Build Coastguard Worker 
8330*8b26181fSAndroid Build Coastguard Worker 	case DLT_PPP_PPPD:
8331*8b26181fSAndroid Build Coastguard Worker 		if (dir) {
8332*8b26181fSAndroid Build Coastguard Worker 			/* match outgoing packets */
8333*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);
8334*8b26181fSAndroid Build Coastguard Worker 		} else {
8335*8b26181fSAndroid Build Coastguard Worker 			/* match incoming packets */
8336*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);
8337*8b26181fSAndroid Build Coastguard Worker 		}
8338*8b26181fSAndroid Build Coastguard Worker 		break;
8339*8b26181fSAndroid Build Coastguard Worker 
8340*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MFR:
8341*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MLFR:
8342*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MLPPP:
8343*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM1:
8344*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM2:
8345*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_PPPOE:
8346*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_PPPOE_ATM:
8347*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_GGSN:
8348*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ES:
8349*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_MONITOR:
8350*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_SERVICES:
8351*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ETHER:
8352*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_PPP:
8353*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_FRELAY:
8354*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_CHDLC:
8355*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_VP:
8356*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ST:
8357*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_ISM:
8358*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_VS:
8359*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_SRX_E2E:
8360*8b26181fSAndroid Build Coastguard Worker         case DLT_JUNIPER_FIBRECHANNEL:
8361*8b26181fSAndroid Build Coastguard Worker 	case DLT_JUNIPER_ATM_CEMIC:
8362*8b26181fSAndroid Build Coastguard Worker 
8363*8b26181fSAndroid Build Coastguard Worker 		/* juniper flags (including direction) are stored
8364*8b26181fSAndroid Build Coastguard Worker 		 * the byte after the 3-byte magic number */
8365*8b26181fSAndroid Build Coastguard Worker 		if (dir) {
8366*8b26181fSAndroid Build Coastguard Worker 			/* match outgoing packets */
8367*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01);
8368*8b26181fSAndroid Build Coastguard Worker 		} else {
8369*8b26181fSAndroid Build Coastguard Worker 			/* match incoming packets */
8370*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01);
8371*8b26181fSAndroid Build Coastguard Worker 		}
8372*8b26181fSAndroid Build Coastguard Worker 		break;
8373*8b26181fSAndroid Build Coastguard Worker 
8374*8b26181fSAndroid Build Coastguard Worker 	default:
8375*8b26181fSAndroid Build Coastguard Worker 		/*
8376*8b26181fSAndroid Build Coastguard Worker 		 * If we have packet meta-data indicating a direction,
8377*8b26181fSAndroid Build Coastguard Worker 		 * and that metadata can be checked by BPF code, check
8378*8b26181fSAndroid Build Coastguard Worker 		 * it.  Otherwise, give up, as this link-layer type has
8379*8b26181fSAndroid Build Coastguard Worker 		 * nothing in the packet data.
8380*8b26181fSAndroid Build Coastguard Worker 		 *
8381*8b26181fSAndroid Build Coastguard Worker 		 * Currently, the only platform where a BPF filter can
8382*8b26181fSAndroid Build Coastguard Worker 		 * check that metadata is Linux with the in-kernel
8383*8b26181fSAndroid Build Coastguard Worker 		 * BPF interpreter.  If other packet capture mechanisms
8384*8b26181fSAndroid Build Coastguard Worker 		 * and BPF filters also supported this, it would be
8385*8b26181fSAndroid Build Coastguard Worker 		 * nice.  It would be even better if they made that
8386*8b26181fSAndroid Build Coastguard Worker 		 * metadata available so that we could provide it
8387*8b26181fSAndroid Build Coastguard Worker 		 * with newer capture APIs, allowing it to be saved
8388*8b26181fSAndroid Build Coastguard Worker 		 * in pcapng files.
8389*8b26181fSAndroid Build Coastguard Worker 		 */
8390*8b26181fSAndroid Build Coastguard Worker #if defined(linux)
8391*8b26181fSAndroid Build Coastguard Worker 		/*
8392*8b26181fSAndroid Build Coastguard Worker 		 * This is Linux; we require PF_PACKET support.
8393*8b26181fSAndroid Build Coastguard Worker 		 * If this is a *live* capture, we can look at
8394*8b26181fSAndroid Build Coastguard Worker 		 * special meta-data in the filter expression;
8395*8b26181fSAndroid Build Coastguard Worker 		 * if it's a savefile, we can't.
8396*8b26181fSAndroid Build Coastguard Worker 		 */
8397*8b26181fSAndroid Build Coastguard Worker 		if (cstate->bpf_pcap->rfile != NULL) {
8398*8b26181fSAndroid Build Coastguard Worker 			/* We have a FILE *, so this is a savefile */
8399*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "inbound/outbound not supported on %s when reading savefiles",
8400*8b26181fSAndroid Build Coastguard Worker 			    pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8401*8b26181fSAndroid Build Coastguard Worker 			/*NOTREACHED*/
8402*8b26181fSAndroid Build Coastguard Worker 		}
8403*8b26181fSAndroid Build Coastguard Worker 		/* match outgoing packets */
8404*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
8405*8b26181fSAndroid Build Coastguard Worker 		             PACKET_OUTGOING);
8406*8b26181fSAndroid Build Coastguard Worker 		if (!dir) {
8407*8b26181fSAndroid Build Coastguard Worker 			/* to filter on inbound traffic, invert the match */
8408*8b26181fSAndroid Build Coastguard Worker 			gen_not(b0);
8409*8b26181fSAndroid Build Coastguard Worker 		}
8410*8b26181fSAndroid Build Coastguard Worker #else /* defined(linux) */
8411*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "inbound/outbound not supported on %s",
8412*8b26181fSAndroid Build Coastguard Worker 		    pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8413*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8414*8b26181fSAndroid Build Coastguard Worker #endif /* defined(linux) */
8415*8b26181fSAndroid Build Coastguard Worker 	}
8416*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8417*8b26181fSAndroid Build Coastguard Worker }
8418*8b26181fSAndroid Build Coastguard Worker 
8419*8b26181fSAndroid Build Coastguard Worker /* PF firewall log matched interface */
8420*8b26181fSAndroid Build Coastguard Worker struct block *
gen_pf_ifname(compiler_state_t * cstate,const char * ifname)8421*8b26181fSAndroid Build Coastguard Worker gen_pf_ifname(compiler_state_t *cstate, const char *ifname)
8422*8b26181fSAndroid Build Coastguard Worker {
8423*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
8424*8b26181fSAndroid Build Coastguard Worker 	u_int len, off;
8425*8b26181fSAndroid Build Coastguard Worker 
8426*8b26181fSAndroid Build Coastguard Worker 	/*
8427*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8428*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8429*8b26181fSAndroid Build Coastguard Worker 	 */
8430*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8431*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8432*8b26181fSAndroid Build Coastguard Worker 
8433*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype != DLT_PFLOG) {
8434*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "ifname supported only on PF linktype");
8435*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8436*8b26181fSAndroid Build Coastguard Worker 	}
8437*8b26181fSAndroid Build Coastguard Worker 	len = sizeof(((struct pfloghdr *)0)->ifname);
8438*8b26181fSAndroid Build Coastguard Worker 	off = offsetof(struct pfloghdr, ifname);
8439*8b26181fSAndroid Build Coastguard Worker 	if (strlen(ifname) >= len) {
8440*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "ifname interface names can only be %d characters",
8441*8b26181fSAndroid Build Coastguard Worker 		    len-1);
8442*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8443*8b26181fSAndroid Build Coastguard Worker 	}
8444*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname),
8445*8b26181fSAndroid Build Coastguard Worker 	    (const u_char *)ifname);
8446*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8447*8b26181fSAndroid Build Coastguard Worker }
8448*8b26181fSAndroid Build Coastguard Worker 
8449*8b26181fSAndroid Build Coastguard Worker /* PF firewall log ruleset name */
8450*8b26181fSAndroid Build Coastguard Worker struct block *
gen_pf_ruleset(compiler_state_t * cstate,char * ruleset)8451*8b26181fSAndroid Build Coastguard Worker gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)
8452*8b26181fSAndroid Build Coastguard Worker {
8453*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
8454*8b26181fSAndroid Build Coastguard Worker 
8455*8b26181fSAndroid Build Coastguard Worker 	/*
8456*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8457*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8458*8b26181fSAndroid Build Coastguard Worker 	 */
8459*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8460*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8461*8b26181fSAndroid Build Coastguard Worker 
8462*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype != DLT_PFLOG) {
8463*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "ruleset supported only on PF linktype");
8464*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8465*8b26181fSAndroid Build Coastguard Worker 	}
8466*8b26181fSAndroid Build Coastguard Worker 
8467*8b26181fSAndroid Build Coastguard Worker 	if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
8468*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "ruleset names can only be %ld characters",
8469*8b26181fSAndroid Build Coastguard Worker 		    (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
8470*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8471*8b26181fSAndroid Build Coastguard Worker 	}
8472*8b26181fSAndroid Build Coastguard Worker 
8473*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
8474*8b26181fSAndroid Build Coastguard Worker 	    (u_int)strlen(ruleset), (const u_char *)ruleset);
8475*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8476*8b26181fSAndroid Build Coastguard Worker }
8477*8b26181fSAndroid Build Coastguard Worker 
8478*8b26181fSAndroid Build Coastguard Worker /* PF firewall log rule number */
8479*8b26181fSAndroid Build Coastguard Worker struct block *
gen_pf_rnr(compiler_state_t * cstate,int rnr)8480*8b26181fSAndroid Build Coastguard Worker gen_pf_rnr(compiler_state_t *cstate, int rnr)
8481*8b26181fSAndroid Build Coastguard Worker {
8482*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
8483*8b26181fSAndroid Build Coastguard Worker 
8484*8b26181fSAndroid Build Coastguard Worker 	/*
8485*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8486*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8487*8b26181fSAndroid Build Coastguard Worker 	 */
8488*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8489*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8490*8b26181fSAndroid Build Coastguard Worker 
8491*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype != DLT_PFLOG) {
8492*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "rnr supported only on PF linktype");
8493*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8494*8b26181fSAndroid Build Coastguard Worker 	}
8495*8b26181fSAndroid Build Coastguard Worker 
8496*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
8497*8b26181fSAndroid Build Coastguard Worker 		 (bpf_u_int32)rnr);
8498*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8499*8b26181fSAndroid Build Coastguard Worker }
8500*8b26181fSAndroid Build Coastguard Worker 
8501*8b26181fSAndroid Build Coastguard Worker /* PF firewall log sub-rule number */
8502*8b26181fSAndroid Build Coastguard Worker struct block *
gen_pf_srnr(compiler_state_t * cstate,int srnr)8503*8b26181fSAndroid Build Coastguard Worker gen_pf_srnr(compiler_state_t *cstate, int srnr)
8504*8b26181fSAndroid Build Coastguard Worker {
8505*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
8506*8b26181fSAndroid Build Coastguard Worker 
8507*8b26181fSAndroid Build Coastguard Worker 	/*
8508*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8509*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8510*8b26181fSAndroid Build Coastguard Worker 	 */
8511*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8512*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8513*8b26181fSAndroid Build Coastguard Worker 
8514*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype != DLT_PFLOG) {
8515*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "srnr supported only on PF linktype");
8516*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8517*8b26181fSAndroid Build Coastguard Worker 	}
8518*8b26181fSAndroid Build Coastguard Worker 
8519*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
8520*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)srnr);
8521*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8522*8b26181fSAndroid Build Coastguard Worker }
8523*8b26181fSAndroid Build Coastguard Worker 
8524*8b26181fSAndroid Build Coastguard Worker /* PF firewall log reason code */
8525*8b26181fSAndroid Build Coastguard Worker struct block *
gen_pf_reason(compiler_state_t * cstate,int reason)8526*8b26181fSAndroid Build Coastguard Worker gen_pf_reason(compiler_state_t *cstate, int reason)
8527*8b26181fSAndroid Build Coastguard Worker {
8528*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
8529*8b26181fSAndroid Build Coastguard Worker 
8530*8b26181fSAndroid Build Coastguard Worker 	/*
8531*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8532*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8533*8b26181fSAndroid Build Coastguard Worker 	 */
8534*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8535*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8536*8b26181fSAndroid Build Coastguard Worker 
8537*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype != DLT_PFLOG) {
8538*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "reason supported only on PF linktype");
8539*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8540*8b26181fSAndroid Build Coastguard Worker 	}
8541*8b26181fSAndroid Build Coastguard Worker 
8542*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
8543*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)reason);
8544*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8545*8b26181fSAndroid Build Coastguard Worker }
8546*8b26181fSAndroid Build Coastguard Worker 
8547*8b26181fSAndroid Build Coastguard Worker /* PF firewall log action */
8548*8b26181fSAndroid Build Coastguard Worker struct block *
gen_pf_action(compiler_state_t * cstate,int action)8549*8b26181fSAndroid Build Coastguard Worker gen_pf_action(compiler_state_t *cstate, int action)
8550*8b26181fSAndroid Build Coastguard Worker {
8551*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
8552*8b26181fSAndroid Build Coastguard Worker 
8553*8b26181fSAndroid Build Coastguard Worker 	/*
8554*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8555*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8556*8b26181fSAndroid Build Coastguard Worker 	 */
8557*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8558*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8559*8b26181fSAndroid Build Coastguard Worker 
8560*8b26181fSAndroid Build Coastguard Worker 	if (cstate->linktype != DLT_PFLOG) {
8561*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "action supported only on PF linktype");
8562*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8563*8b26181fSAndroid Build Coastguard Worker 	}
8564*8b26181fSAndroid Build Coastguard Worker 
8565*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
8566*8b26181fSAndroid Build Coastguard Worker 	    (bpf_u_int32)action);
8567*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8568*8b26181fSAndroid Build Coastguard Worker }
8569*8b26181fSAndroid Build Coastguard Worker 
8570*8b26181fSAndroid Build Coastguard Worker /* IEEE 802.11 wireless header */
8571*8b26181fSAndroid Build Coastguard Worker struct block *
gen_p80211_type(compiler_state_t * cstate,bpf_u_int32 type,bpf_u_int32 mask)8572*8b26181fSAndroid Build Coastguard Worker gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask)
8573*8b26181fSAndroid Build Coastguard Worker {
8574*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
8575*8b26181fSAndroid Build Coastguard Worker 
8576*8b26181fSAndroid Build Coastguard Worker 	/*
8577*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8578*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8579*8b26181fSAndroid Build Coastguard Worker 	 */
8580*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8581*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8582*8b26181fSAndroid Build Coastguard Worker 
8583*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
8584*8b26181fSAndroid Build Coastguard Worker 
8585*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11:
8586*8b26181fSAndroid Build Coastguard Worker 	case DLT_PRISM_HEADER:
8587*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO_AVS:
8588*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO:
8589*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask);
8590*8b26181fSAndroid Build Coastguard Worker 		break;
8591*8b26181fSAndroid Build Coastguard Worker 
8592*8b26181fSAndroid Build Coastguard Worker 	default:
8593*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "802.11 link-layer types supported only on 802.11");
8594*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8595*8b26181fSAndroid Build Coastguard Worker 	}
8596*8b26181fSAndroid Build Coastguard Worker 
8597*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8598*8b26181fSAndroid Build Coastguard Worker }
8599*8b26181fSAndroid Build Coastguard Worker 
8600*8b26181fSAndroid Build Coastguard Worker struct block *
gen_p80211_fcdir(compiler_state_t * cstate,bpf_u_int32 fcdir)8601*8b26181fSAndroid Build Coastguard Worker gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir)
8602*8b26181fSAndroid Build Coastguard Worker {
8603*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
8604*8b26181fSAndroid Build Coastguard Worker 
8605*8b26181fSAndroid Build Coastguard Worker 	/*
8606*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8607*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8608*8b26181fSAndroid Build Coastguard Worker 	 */
8609*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8610*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8611*8b26181fSAndroid Build Coastguard Worker 
8612*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
8613*8b26181fSAndroid Build Coastguard Worker 
8614*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11:
8615*8b26181fSAndroid Build Coastguard Worker 	case DLT_PRISM_HEADER:
8616*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO_AVS:
8617*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO:
8618*8b26181fSAndroid Build Coastguard Worker 		break;
8619*8b26181fSAndroid Build Coastguard Worker 
8620*8b26181fSAndroid Build Coastguard Worker 	default:
8621*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "frame direction supported only with 802.11 headers");
8622*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8623*8b26181fSAndroid Build Coastguard Worker 	}
8624*8b26181fSAndroid Build Coastguard Worker 
8625*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir,
8626*8b26181fSAndroid Build Coastguard Worker 	    IEEE80211_FC1_DIR_MASK);
8627*8b26181fSAndroid Build Coastguard Worker 
8628*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8629*8b26181fSAndroid Build Coastguard Worker }
8630*8b26181fSAndroid Build Coastguard Worker 
8631*8b26181fSAndroid Build Coastguard Worker struct block *
gen_acode(compiler_state_t * cstate,const char * s,struct qual q)8632*8b26181fSAndroid Build Coastguard Worker gen_acode(compiler_state_t *cstate, const char *s, struct qual q)
8633*8b26181fSAndroid Build Coastguard Worker {
8634*8b26181fSAndroid Build Coastguard Worker 	struct block *b;
8635*8b26181fSAndroid Build Coastguard Worker 
8636*8b26181fSAndroid Build Coastguard Worker 	/*
8637*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8638*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8639*8b26181fSAndroid Build Coastguard Worker 	 */
8640*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8641*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8642*8b26181fSAndroid Build Coastguard Worker 
8643*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
8644*8b26181fSAndroid Build Coastguard Worker 
8645*8b26181fSAndroid Build Coastguard Worker 	case DLT_ARCNET:
8646*8b26181fSAndroid Build Coastguard Worker 	case DLT_ARCNET_LINUX:
8647*8b26181fSAndroid Build Coastguard Worker 		if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
8648*8b26181fSAndroid Build Coastguard Worker 		    q.proto == Q_LINK) {
8649*8b26181fSAndroid Build Coastguard Worker 			cstate->e = pcap_ether_aton(s);
8650*8b26181fSAndroid Build Coastguard Worker 			if (cstate->e == NULL)
8651*8b26181fSAndroid Build Coastguard Worker 				bpf_error(cstate, "malloc");
8652*8b26181fSAndroid Build Coastguard Worker 			b = gen_ahostop(cstate, cstate->e, (int)q.dir);
8653*8b26181fSAndroid Build Coastguard Worker 			free(cstate->e);
8654*8b26181fSAndroid Build Coastguard Worker 			cstate->e = NULL;
8655*8b26181fSAndroid Build Coastguard Worker 			return (b);
8656*8b26181fSAndroid Build Coastguard Worker 		} else
8657*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "ARCnet address used in non-arc expression");
8658*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8659*8b26181fSAndroid Build Coastguard Worker 
8660*8b26181fSAndroid Build Coastguard Worker 	default:
8661*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "aid supported only on ARCnet");
8662*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8663*8b26181fSAndroid Build Coastguard Worker 	}
8664*8b26181fSAndroid Build Coastguard Worker }
8665*8b26181fSAndroid Build Coastguard Worker 
8666*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_ahostop(compiler_state_t * cstate,const u_char * eaddr,int dir)8667*8b26181fSAndroid Build Coastguard Worker gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
8668*8b26181fSAndroid Build Coastguard Worker {
8669*8b26181fSAndroid Build Coastguard Worker 	register struct block *b0, *b1;
8670*8b26181fSAndroid Build Coastguard Worker 
8671*8b26181fSAndroid Build Coastguard Worker 	switch (dir) {
8672*8b26181fSAndroid Build Coastguard Worker 	/* src comes first, different from Ethernet */
8673*8b26181fSAndroid Build Coastguard Worker 	case Q_SRC:
8674*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr);
8675*8b26181fSAndroid Build Coastguard Worker 
8676*8b26181fSAndroid Build Coastguard Worker 	case Q_DST:
8677*8b26181fSAndroid Build Coastguard Worker 		return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr);
8678*8b26181fSAndroid Build Coastguard Worker 
8679*8b26181fSAndroid Build Coastguard Worker 	case Q_AND:
8680*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8681*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ahostop(cstate, eaddr, Q_DST);
8682*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
8683*8b26181fSAndroid Build Coastguard Worker 		return b1;
8684*8b26181fSAndroid Build Coastguard Worker 
8685*8b26181fSAndroid Build Coastguard Worker 	case Q_DEFAULT:
8686*8b26181fSAndroid Build Coastguard Worker 	case Q_OR:
8687*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8688*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ahostop(cstate, eaddr, Q_DST);
8689*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
8690*8b26181fSAndroid Build Coastguard Worker 		return b1;
8691*8b26181fSAndroid Build Coastguard Worker 
8692*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR1:
8693*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
8694*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8695*8b26181fSAndroid Build Coastguard Worker 
8696*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR2:
8697*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
8698*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8699*8b26181fSAndroid Build Coastguard Worker 
8700*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR3:
8701*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
8702*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8703*8b26181fSAndroid Build Coastguard Worker 
8704*8b26181fSAndroid Build Coastguard Worker 	case Q_ADDR4:
8705*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
8706*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8707*8b26181fSAndroid Build Coastguard Worker 
8708*8b26181fSAndroid Build Coastguard Worker 	case Q_RA:
8709*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ra' is only supported on 802.11");
8710*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8711*8b26181fSAndroid Build Coastguard Worker 
8712*8b26181fSAndroid Build Coastguard Worker 	case Q_TA:
8713*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "'ta' is only supported on 802.11");
8714*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8715*8b26181fSAndroid Build Coastguard Worker 	}
8716*8b26181fSAndroid Build Coastguard Worker 	abort();
8717*8b26181fSAndroid Build Coastguard Worker 	/*NOTREACHED*/
8718*8b26181fSAndroid Build Coastguard Worker }
8719*8b26181fSAndroid Build Coastguard Worker 
8720*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_vlan_tpid_test(compiler_state_t * cstate)8721*8b26181fSAndroid Build Coastguard Worker gen_vlan_tpid_test(compiler_state_t *cstate)
8722*8b26181fSAndroid Build Coastguard Worker {
8723*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
8724*8b26181fSAndroid Build Coastguard Worker 
8725*8b26181fSAndroid Build Coastguard Worker 	/* check for VLAN, including 802.1ad and QinQ */
8726*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ETHERTYPE_8021Q);
8727*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_linktype(cstate, ETHERTYPE_8021AD);
8728*8b26181fSAndroid Build Coastguard Worker 	gen_or(b0,b1);
8729*8b26181fSAndroid Build Coastguard Worker 	b0 = b1;
8730*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);
8731*8b26181fSAndroid Build Coastguard Worker 	gen_or(b0,b1);
8732*8b26181fSAndroid Build Coastguard Worker 
8733*8b26181fSAndroid Build Coastguard Worker 	return b1;
8734*8b26181fSAndroid Build Coastguard Worker }
8735*8b26181fSAndroid Build Coastguard Worker 
8736*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_vlan_vid_test(compiler_state_t * cstate,bpf_u_int32 vlan_num)8737*8b26181fSAndroid Build Coastguard Worker gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num)
8738*8b26181fSAndroid Build Coastguard Worker {
8739*8b26181fSAndroid Build Coastguard Worker 	if (vlan_num > 0x0fff) {
8740*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "VLAN tag %u greater than maximum %u",
8741*8b26181fSAndroid Build Coastguard Worker 		    vlan_num, 0x0fff);
8742*8b26181fSAndroid Build Coastguard Worker 	}
8743*8b26181fSAndroid Build Coastguard Worker 	return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff);
8744*8b26181fSAndroid Build Coastguard Worker }
8745*8b26181fSAndroid Build Coastguard Worker 
8746*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_vlan_no_bpf_extensions(compiler_state_t * cstate,bpf_u_int32 vlan_num,int has_vlan_tag)8747*8b26181fSAndroid Build Coastguard Worker gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
8748*8b26181fSAndroid Build Coastguard Worker     int has_vlan_tag)
8749*8b26181fSAndroid Build Coastguard Worker {
8750*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
8751*8b26181fSAndroid Build Coastguard Worker 
8752*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_vlan_tpid_test(cstate);
8753*8b26181fSAndroid Build Coastguard Worker 
8754*8b26181fSAndroid Build Coastguard Worker 	if (has_vlan_tag) {
8755*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_vlan_vid_test(cstate, vlan_num);
8756*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
8757*8b26181fSAndroid Build Coastguard Worker 		b0 = b1;
8758*8b26181fSAndroid Build Coastguard Worker 	}
8759*8b26181fSAndroid Build Coastguard Worker 
8760*8b26181fSAndroid Build Coastguard Worker 	/*
8761*8b26181fSAndroid Build Coastguard Worker 	 * Both payload and link header type follow the VLAN tags so that
8762*8b26181fSAndroid Build Coastguard Worker 	 * both need to be updated.
8763*8b26181fSAndroid Build Coastguard Worker 	 */
8764*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkpl.constant_part += 4;
8765*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linktype.constant_part += 4;
8766*8b26181fSAndroid Build Coastguard Worker 
8767*8b26181fSAndroid Build Coastguard Worker 	return b0;
8768*8b26181fSAndroid Build Coastguard Worker }
8769*8b26181fSAndroid Build Coastguard Worker 
8770*8b26181fSAndroid Build Coastguard Worker #if defined(SKF_AD_VLAN_TAG_PRESENT)
8771*8b26181fSAndroid Build Coastguard Worker /* add v to variable part of off */
8772*8b26181fSAndroid Build Coastguard Worker static void
gen_vlan_vloffset_add(compiler_state_t * cstate,bpf_abs_offset * off,bpf_u_int32 v,struct slist * s)8773*8b26181fSAndroid Build Coastguard Worker gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off,
8774*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 v, struct slist *s)
8775*8b26181fSAndroid Build Coastguard Worker {
8776*8b26181fSAndroid Build Coastguard Worker 	struct slist *s2;
8777*8b26181fSAndroid Build Coastguard Worker 
8778*8b26181fSAndroid Build Coastguard Worker 	if (!off->is_variable)
8779*8b26181fSAndroid Build Coastguard Worker 		off->is_variable = 1;
8780*8b26181fSAndroid Build Coastguard Worker 	if (off->reg == -1)
8781*8b26181fSAndroid Build Coastguard Worker 		off->reg = alloc_reg(cstate);
8782*8b26181fSAndroid Build Coastguard Worker 
8783*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
8784*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = off->reg;
8785*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
8786*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
8787*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = v;
8788*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
8789*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_ST);
8790*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = off->reg;
8791*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
8792*8b26181fSAndroid Build Coastguard Worker }
8793*8b26181fSAndroid Build Coastguard Worker 
8794*8b26181fSAndroid Build Coastguard Worker /*
8795*8b26181fSAndroid Build Coastguard Worker  * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8796*8b26181fSAndroid Build Coastguard Worker  * and link type offsets first
8797*8b26181fSAndroid Build Coastguard Worker  */
8798*8b26181fSAndroid Build Coastguard Worker static void
gen_vlan_patch_tpid_test(compiler_state_t * cstate,struct block * b_tpid)8799*8b26181fSAndroid Build Coastguard Worker gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid)
8800*8b26181fSAndroid Build Coastguard Worker {
8801*8b26181fSAndroid Build Coastguard Worker 	struct slist s;
8802*8b26181fSAndroid Build Coastguard Worker 
8803*8b26181fSAndroid Build Coastguard Worker 	/* offset determined at run time, shift variable part */
8804*8b26181fSAndroid Build Coastguard Worker 	s.next = NULL;
8805*8b26181fSAndroid Build Coastguard Worker 	cstate->is_vlan_vloffset = 1;
8806*8b26181fSAndroid Build Coastguard Worker 	gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);
8807*8b26181fSAndroid Build Coastguard Worker 	gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);
8808*8b26181fSAndroid Build Coastguard Worker 
8809*8b26181fSAndroid Build Coastguard Worker 	/* we get a pointer to a chain of or-ed blocks, patch first of them */
8810*8b26181fSAndroid Build Coastguard Worker 	sappend(s.next, b_tpid->head->stmts);
8811*8b26181fSAndroid Build Coastguard Worker 	b_tpid->head->stmts = s.next;
8812*8b26181fSAndroid Build Coastguard Worker }
8813*8b26181fSAndroid Build Coastguard Worker 
8814*8b26181fSAndroid Build Coastguard Worker /*
8815*8b26181fSAndroid Build Coastguard Worker  * patch block b_vid (VLAN id test) to load VID value either from packet
8816*8b26181fSAndroid Build Coastguard Worker  * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8817*8b26181fSAndroid Build Coastguard Worker  */
8818*8b26181fSAndroid Build Coastguard Worker static void
gen_vlan_patch_vid_test(compiler_state_t * cstate,struct block * b_vid)8819*8b26181fSAndroid Build Coastguard Worker gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
8820*8b26181fSAndroid Build Coastguard Worker {
8821*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s2, *sjeq;
8822*8b26181fSAndroid Build Coastguard Worker 	unsigned cnt;
8823*8b26181fSAndroid Build Coastguard Worker 
8824*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8825*8b26181fSAndroid Build Coastguard Worker 	s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
8826*8b26181fSAndroid Build Coastguard Worker 
8827*8b26181fSAndroid Build Coastguard Worker 	/* true -> next instructions, false -> beginning of b_vid */
8828*8b26181fSAndroid Build Coastguard Worker 	sjeq = new_stmt(cstate, JMP(BPF_JEQ));
8829*8b26181fSAndroid Build Coastguard Worker 	sjeq->s.k = 1;
8830*8b26181fSAndroid Build Coastguard Worker 	sjeq->s.jf = b_vid->stmts;
8831*8b26181fSAndroid Build Coastguard Worker 	sappend(s, sjeq);
8832*8b26181fSAndroid Build Coastguard Worker 
8833*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8834*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG;
8835*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
8836*8b26181fSAndroid Build Coastguard Worker 	sjeq->s.jt = s2;
8837*8b26181fSAndroid Build Coastguard Worker 
8838*8b26181fSAndroid Build Coastguard Worker 	/* Jump to the test in b_vid. We need to jump one instruction before
8839*8b26181fSAndroid Build Coastguard Worker 	 * the end of the b_vid block so that we only skip loading the TCI
8840*8b26181fSAndroid Build Coastguard Worker 	 * from packet data and not the 'and' instruction extractging VID.
8841*8b26181fSAndroid Build Coastguard Worker 	 */
8842*8b26181fSAndroid Build Coastguard Worker 	cnt = 0;
8843*8b26181fSAndroid Build Coastguard Worker 	for (s2 = b_vid->stmts; s2; s2 = s2->next)
8844*8b26181fSAndroid Build Coastguard Worker 		cnt++;
8845*8b26181fSAndroid Build Coastguard Worker 	s2 = new_stmt(cstate, JMP(BPF_JA));
8846*8b26181fSAndroid Build Coastguard Worker 	s2->s.k = cnt - 1;
8847*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s2);
8848*8b26181fSAndroid Build Coastguard Worker 
8849*8b26181fSAndroid Build Coastguard Worker 	/* insert our statements at the beginning of b_vid */
8850*8b26181fSAndroid Build Coastguard Worker 	sappend(s, b_vid->stmts);
8851*8b26181fSAndroid Build Coastguard Worker 	b_vid->stmts = s;
8852*8b26181fSAndroid Build Coastguard Worker }
8853*8b26181fSAndroid Build Coastguard Worker 
8854*8b26181fSAndroid Build Coastguard Worker /*
8855*8b26181fSAndroid Build Coastguard Worker  * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8856*8b26181fSAndroid Build Coastguard Worker  * extensions.  Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8857*8b26181fSAndroid Build Coastguard Worker  * tag can be either in metadata or in packet data; therefore if the
8858*8b26181fSAndroid Build Coastguard Worker  * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8859*8b26181fSAndroid Build Coastguard Worker  * header for VLAN tag. As the decision is done at run time, we need
8860*8b26181fSAndroid Build Coastguard Worker  * update variable part of the offsets
8861*8b26181fSAndroid Build Coastguard Worker  */
8862*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_vlan_bpf_extensions(compiler_state_t * cstate,bpf_u_int32 vlan_num,int has_vlan_tag)8863*8b26181fSAndroid Build Coastguard Worker gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
8864*8b26181fSAndroid Build Coastguard Worker     int has_vlan_tag)
8865*8b26181fSAndroid Build Coastguard Worker {
8866*8b26181fSAndroid Build Coastguard Worker         struct block *b0, *b_tpid, *b_vid = NULL;
8867*8b26181fSAndroid Build Coastguard Worker         struct slist *s;
8868*8b26181fSAndroid Build Coastguard Worker 
8869*8b26181fSAndroid Build Coastguard Worker         /* generate new filter code based on extracting packet
8870*8b26181fSAndroid Build Coastguard Worker          * metadata */
8871*8b26181fSAndroid Build Coastguard Worker         s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8872*8b26181fSAndroid Build Coastguard Worker         s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
8873*8b26181fSAndroid Build Coastguard Worker 
8874*8b26181fSAndroid Build Coastguard Worker         b0 = new_block(cstate, JMP(BPF_JEQ));
8875*8b26181fSAndroid Build Coastguard Worker         b0->stmts = s;
8876*8b26181fSAndroid Build Coastguard Worker         b0->s.k = 1;
8877*8b26181fSAndroid Build Coastguard Worker 
8878*8b26181fSAndroid Build Coastguard Worker 	/*
8879*8b26181fSAndroid Build Coastguard Worker 	 * This is tricky. We need to insert the statements updating variable
8880*8b26181fSAndroid Build Coastguard Worker 	 * parts of offsets before the traditional TPID and VID tests so
8881*8b26181fSAndroid Build Coastguard Worker 	 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8882*8b26181fSAndroid Build Coastguard Worker 	 * we do not want this update to affect those checks. That's why we
8883*8b26181fSAndroid Build Coastguard Worker 	 * generate both test blocks first and insert the statements updating
8884*8b26181fSAndroid Build Coastguard Worker 	 * variable parts of both offsets after that. This wouldn't work if
8885*8b26181fSAndroid Build Coastguard Worker 	 * there already were variable length link header when entering this
8886*8b26181fSAndroid Build Coastguard Worker 	 * function but gen_vlan_bpf_extensions() isn't called in that case.
8887*8b26181fSAndroid Build Coastguard Worker 	 */
8888*8b26181fSAndroid Build Coastguard Worker 	b_tpid = gen_vlan_tpid_test(cstate);
8889*8b26181fSAndroid Build Coastguard Worker 	if (has_vlan_tag)
8890*8b26181fSAndroid Build Coastguard Worker 		b_vid = gen_vlan_vid_test(cstate, vlan_num);
8891*8b26181fSAndroid Build Coastguard Worker 
8892*8b26181fSAndroid Build Coastguard Worker 	gen_vlan_patch_tpid_test(cstate, b_tpid);
8893*8b26181fSAndroid Build Coastguard Worker 	gen_or(b0, b_tpid);
8894*8b26181fSAndroid Build Coastguard Worker 	b0 = b_tpid;
8895*8b26181fSAndroid Build Coastguard Worker 
8896*8b26181fSAndroid Build Coastguard Worker 	if (has_vlan_tag) {
8897*8b26181fSAndroid Build Coastguard Worker 		gen_vlan_patch_vid_test(cstate, b_vid);
8898*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b_vid);
8899*8b26181fSAndroid Build Coastguard Worker 		b0 = b_vid;
8900*8b26181fSAndroid Build Coastguard Worker 	}
8901*8b26181fSAndroid Build Coastguard Worker 
8902*8b26181fSAndroid Build Coastguard Worker         return b0;
8903*8b26181fSAndroid Build Coastguard Worker }
8904*8b26181fSAndroid Build Coastguard Worker #endif
8905*8b26181fSAndroid Build Coastguard Worker 
8906*8b26181fSAndroid Build Coastguard Worker /*
8907*8b26181fSAndroid Build Coastguard Worker  * support IEEE 802.1Q VLAN trunk over ethernet
8908*8b26181fSAndroid Build Coastguard Worker  */
8909*8b26181fSAndroid Build Coastguard Worker struct block *
gen_vlan(compiler_state_t * cstate,bpf_u_int32 vlan_num,int has_vlan_tag)8910*8b26181fSAndroid Build Coastguard Worker gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag)
8911*8b26181fSAndroid Build Coastguard Worker {
8912*8b26181fSAndroid Build Coastguard Worker 	struct	block	*b0;
8913*8b26181fSAndroid Build Coastguard Worker 
8914*8b26181fSAndroid Build Coastguard Worker 	/*
8915*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
8916*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
8917*8b26181fSAndroid Build Coastguard Worker 	 */
8918*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
8919*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
8920*8b26181fSAndroid Build Coastguard Worker 
8921*8b26181fSAndroid Build Coastguard Worker 	/* can't check for VLAN-encapsulated packets inside MPLS */
8922*8b26181fSAndroid Build Coastguard Worker 	if (cstate->label_stack_depth > 0)
8923*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "no VLAN match after MPLS");
8924*8b26181fSAndroid Build Coastguard Worker 
8925*8b26181fSAndroid Build Coastguard Worker 	/*
8926*8b26181fSAndroid Build Coastguard Worker 	 * Check for a VLAN packet, and then change the offsets to point
8927*8b26181fSAndroid Build Coastguard Worker 	 * to the type and data fields within the VLAN packet.  Just
8928*8b26181fSAndroid Build Coastguard Worker 	 * increment the offsets, so that we can support a hierarchy, e.g.
8929*8b26181fSAndroid Build Coastguard Worker 	 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8930*8b26181fSAndroid Build Coastguard Worker 	 * VLAN 100.
8931*8b26181fSAndroid Build Coastguard Worker 	 *
8932*8b26181fSAndroid Build Coastguard Worker 	 * XXX - this is a bit of a kludge.  If we were to split the
8933*8b26181fSAndroid Build Coastguard Worker 	 * compiler into a parser that parses an expression and
8934*8b26181fSAndroid Build Coastguard Worker 	 * generates an expression tree, and a code generator that
8935*8b26181fSAndroid Build Coastguard Worker 	 * takes an expression tree (which could come from our
8936*8b26181fSAndroid Build Coastguard Worker 	 * parser or from some other parser) and generates BPF code,
8937*8b26181fSAndroid Build Coastguard Worker 	 * we could perhaps make the offsets parameters of routines
8938*8b26181fSAndroid Build Coastguard Worker 	 * and, in the handler for an "AND" node, pass to subnodes
8939*8b26181fSAndroid Build Coastguard Worker 	 * other than the VLAN node the adjusted offsets.
8940*8b26181fSAndroid Build Coastguard Worker 	 *
8941*8b26181fSAndroid Build Coastguard Worker 	 * This would mean that "vlan" would, instead of changing the
8942*8b26181fSAndroid Build Coastguard Worker 	 * behavior of *all* tests after it, change only the behavior
8943*8b26181fSAndroid Build Coastguard Worker 	 * of tests ANDed with it.  That would change the documented
8944*8b26181fSAndroid Build Coastguard Worker 	 * semantics of "vlan", which might break some expressions.
8945*8b26181fSAndroid Build Coastguard Worker 	 * However, it would mean that "(vlan and ip) or ip" would check
8946*8b26181fSAndroid Build Coastguard Worker 	 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8947*8b26181fSAndroid Build Coastguard Worker 	 * checking only for VLAN-encapsulated IP, so that could still
8948*8b26181fSAndroid Build Coastguard Worker 	 * be considered worth doing; it wouldn't break expressions
8949*8b26181fSAndroid Build Coastguard Worker 	 * that are of the form "vlan and ..." or "vlan N and ...",
8950*8b26181fSAndroid Build Coastguard Worker 	 * which I suspect are the most common expressions involving
8951*8b26181fSAndroid Build Coastguard Worker 	 * "vlan".  "vlan or ..." doesn't necessarily do what the user
8952*8b26181fSAndroid Build Coastguard Worker 	 * would really want, now, as all the "or ..." tests would
8953*8b26181fSAndroid Build Coastguard Worker 	 * be done assuming a VLAN, even though the "or" could be viewed
8954*8b26181fSAndroid Build Coastguard Worker 	 * as meaning "or, if this isn't a VLAN packet...".
8955*8b26181fSAndroid Build Coastguard Worker 	 */
8956*8b26181fSAndroid Build Coastguard Worker 	switch (cstate->linktype) {
8957*8b26181fSAndroid Build Coastguard Worker 
8958*8b26181fSAndroid Build Coastguard Worker 	case DLT_EN10MB:
8959*8b26181fSAndroid Build Coastguard Worker 	case DLT_NETANALYZER:
8960*8b26181fSAndroid Build Coastguard Worker 	case DLT_NETANALYZER_TRANSPARENT:
8961*8b26181fSAndroid Build Coastguard Worker #if defined(SKF_AD_VLAN_TAG_PRESENT)
8962*8b26181fSAndroid Build Coastguard Worker 		/* Verify that this is the outer part of the packet and
8963*8b26181fSAndroid Build Coastguard Worker 		 * not encapsulated somehow. */
8964*8b26181fSAndroid Build Coastguard Worker 		if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
8965*8b26181fSAndroid Build Coastguard Worker 		    cstate->off_linkhdr.constant_part ==
8966*8b26181fSAndroid Build Coastguard Worker 		    cstate->off_outermostlinkhdr.constant_part) {
8967*8b26181fSAndroid Build Coastguard Worker 			/*
8968*8b26181fSAndroid Build Coastguard Worker 			 * Do we need special VLAN handling?
8969*8b26181fSAndroid Build Coastguard Worker 			 */
8970*8b26181fSAndroid Build Coastguard Worker 			if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
8971*8b26181fSAndroid Build Coastguard Worker 				b0 = gen_vlan_bpf_extensions(cstate, vlan_num,
8972*8b26181fSAndroid Build Coastguard Worker 				    has_vlan_tag);
8973*8b26181fSAndroid Build Coastguard Worker 			else
8974*8b26181fSAndroid Build Coastguard Worker 				b0 = gen_vlan_no_bpf_extensions(cstate,
8975*8b26181fSAndroid Build Coastguard Worker 				    vlan_num, has_vlan_tag);
8976*8b26181fSAndroid Build Coastguard Worker 		} else
8977*8b26181fSAndroid Build Coastguard Worker #endif
8978*8b26181fSAndroid Build Coastguard Worker 			b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num,
8979*8b26181fSAndroid Build Coastguard Worker 			    has_vlan_tag);
8980*8b26181fSAndroid Build Coastguard Worker                 break;
8981*8b26181fSAndroid Build Coastguard Worker 
8982*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11:
8983*8b26181fSAndroid Build Coastguard Worker 	case DLT_PRISM_HEADER:
8984*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO_AVS:
8985*8b26181fSAndroid Build Coastguard Worker 	case DLT_IEEE802_11_RADIO:
8986*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag);
8987*8b26181fSAndroid Build Coastguard Worker 		break;
8988*8b26181fSAndroid Build Coastguard Worker 
8989*8b26181fSAndroid Build Coastguard Worker 	default:
8990*8b26181fSAndroid Build Coastguard Worker 		bpf_error(cstate, "no VLAN support for %s",
8991*8b26181fSAndroid Build Coastguard Worker 		      pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8992*8b26181fSAndroid Build Coastguard Worker 		/*NOTREACHED*/
8993*8b26181fSAndroid Build Coastguard Worker 	}
8994*8b26181fSAndroid Build Coastguard Worker 
8995*8b26181fSAndroid Build Coastguard Worker         cstate->vlan_stack_depth++;
8996*8b26181fSAndroid Build Coastguard Worker 
8997*8b26181fSAndroid Build Coastguard Worker 	return (b0);
8998*8b26181fSAndroid Build Coastguard Worker }
8999*8b26181fSAndroid Build Coastguard Worker 
9000*8b26181fSAndroid Build Coastguard Worker /*
9001*8b26181fSAndroid Build Coastguard Worker  * support for MPLS
9002*8b26181fSAndroid Build Coastguard Worker  *
9003*8b26181fSAndroid Build Coastguard Worker  * The label_num_arg dance is to avoid annoying whining by compilers that
9004*8b26181fSAndroid Build Coastguard Worker  * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9005*8b26181fSAndroid Build Coastguard Worker  * It's not *used* after setjmp returns.
9006*8b26181fSAndroid Build Coastguard Worker  */
9007*8b26181fSAndroid Build Coastguard Worker struct block *
gen_mpls(compiler_state_t * cstate,bpf_u_int32 label_num_arg,int has_label_num)9008*8b26181fSAndroid Build Coastguard Worker gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num_arg,
9009*8b26181fSAndroid Build Coastguard Worker     int has_label_num)
9010*8b26181fSAndroid Build Coastguard Worker {
9011*8b26181fSAndroid Build Coastguard Worker 	volatile bpf_u_int32 label_num = label_num_arg;
9012*8b26181fSAndroid Build Coastguard Worker 	struct	block	*b0, *b1;
9013*8b26181fSAndroid Build Coastguard Worker 
9014*8b26181fSAndroid Build Coastguard Worker 	/*
9015*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9016*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9017*8b26181fSAndroid Build Coastguard Worker 	 */
9018*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9019*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9020*8b26181fSAndroid Build Coastguard Worker 
9021*8b26181fSAndroid Build Coastguard Worker         if (cstate->label_stack_depth > 0) {
9022*8b26181fSAndroid Build Coastguard Worker             /* just match the bottom-of-stack bit clear */
9023*8b26181fSAndroid Build Coastguard Worker             b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
9024*8b26181fSAndroid Build Coastguard Worker         } else {
9025*8b26181fSAndroid Build Coastguard Worker             /*
9026*8b26181fSAndroid Build Coastguard Worker              * We're not in an MPLS stack yet, so check the link-layer
9027*8b26181fSAndroid Build Coastguard Worker              * type against MPLS.
9028*8b26181fSAndroid Build Coastguard Worker              */
9029*8b26181fSAndroid Build Coastguard Worker             switch (cstate->linktype) {
9030*8b26181fSAndroid Build Coastguard Worker 
9031*8b26181fSAndroid Build Coastguard Worker             case DLT_C_HDLC: /* fall through */
9032*8b26181fSAndroid Build Coastguard Worker             case DLT_HDLC:
9033*8b26181fSAndroid Build Coastguard Worker             case DLT_EN10MB:
9034*8b26181fSAndroid Build Coastguard Worker             case DLT_NETANALYZER:
9035*8b26181fSAndroid Build Coastguard Worker             case DLT_NETANALYZER_TRANSPARENT:
9036*8b26181fSAndroid Build Coastguard Worker                     b0 = gen_linktype(cstate, ETHERTYPE_MPLS);
9037*8b26181fSAndroid Build Coastguard Worker                     break;
9038*8b26181fSAndroid Build Coastguard Worker 
9039*8b26181fSAndroid Build Coastguard Worker             case DLT_PPP:
9040*8b26181fSAndroid Build Coastguard Worker                     b0 = gen_linktype(cstate, PPP_MPLS_UCAST);
9041*8b26181fSAndroid Build Coastguard Worker                     break;
9042*8b26181fSAndroid Build Coastguard Worker 
9043*8b26181fSAndroid Build Coastguard Worker                     /* FIXME add other DLT_s ...
9044*8b26181fSAndroid Build Coastguard Worker                      * for Frame-Relay/and ATM this may get messy due to SNAP headers
9045*8b26181fSAndroid Build Coastguard Worker                      * leave it for now */
9046*8b26181fSAndroid Build Coastguard Worker 
9047*8b26181fSAndroid Build Coastguard Worker             default:
9048*8b26181fSAndroid Build Coastguard Worker                     bpf_error(cstate, "no MPLS support for %s",
9049*8b26181fSAndroid Build Coastguard Worker                           pcap_datalink_val_to_description_or_dlt(cstate->linktype));
9050*8b26181fSAndroid Build Coastguard Worker                     /*NOTREACHED*/
9051*8b26181fSAndroid Build Coastguard Worker             }
9052*8b26181fSAndroid Build Coastguard Worker         }
9053*8b26181fSAndroid Build Coastguard Worker 
9054*8b26181fSAndroid Build Coastguard Worker 	/* If a specific MPLS label is requested, check it */
9055*8b26181fSAndroid Build Coastguard Worker 	if (has_label_num) {
9056*8b26181fSAndroid Build Coastguard Worker 		if (label_num > 0xFFFFF) {
9057*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "MPLS label %u greater than maximum %u",
9058*8b26181fSAndroid Build Coastguard Worker 			    label_num, 0xFFFFF);
9059*8b26181fSAndroid Build Coastguard Worker 		}
9060*8b26181fSAndroid Build Coastguard Worker 		label_num = label_num << 12; /* label is shifted 12 bits on the wire */
9061*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num,
9062*8b26181fSAndroid Build Coastguard Worker 		    0xfffff000); /* only compare the first 20 bits */
9063*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9064*8b26181fSAndroid Build Coastguard Worker 		b0 = b1;
9065*8b26181fSAndroid Build Coastguard Worker 	}
9066*8b26181fSAndroid Build Coastguard Worker 
9067*8b26181fSAndroid Build Coastguard Worker         /*
9068*8b26181fSAndroid Build Coastguard Worker          * Change the offsets to point to the type and data fields within
9069*8b26181fSAndroid Build Coastguard Worker          * the MPLS packet.  Just increment the offsets, so that we
9070*8b26181fSAndroid Build Coastguard Worker          * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9071*8b26181fSAndroid Build Coastguard Worker          * capture packets with an outer label of 100000 and an inner
9072*8b26181fSAndroid Build Coastguard Worker          * label of 1024.
9073*8b26181fSAndroid Build Coastguard Worker          *
9074*8b26181fSAndroid Build Coastguard Worker          * Increment the MPLS stack depth as well; this indicates that
9075*8b26181fSAndroid Build Coastguard Worker          * we're checking MPLS-encapsulated headers, to make sure higher
9076*8b26181fSAndroid Build Coastguard Worker          * level code generators don't try to match against IP-related
9077*8b26181fSAndroid Build Coastguard Worker          * protocols such as Q_ARP, Q_RARP etc.
9078*8b26181fSAndroid Build Coastguard Worker          *
9079*8b26181fSAndroid Build Coastguard Worker          * XXX - this is a bit of a kludge.  See comments in gen_vlan().
9080*8b26181fSAndroid Build Coastguard Worker          */
9081*8b26181fSAndroid Build Coastguard Worker         cstate->off_nl_nosnap += 4;
9082*8b26181fSAndroid Build Coastguard Worker         cstate->off_nl += 4;
9083*8b26181fSAndroid Build Coastguard Worker         cstate->label_stack_depth++;
9084*8b26181fSAndroid Build Coastguard Worker 	return (b0);
9085*8b26181fSAndroid Build Coastguard Worker }
9086*8b26181fSAndroid Build Coastguard Worker 
9087*8b26181fSAndroid Build Coastguard Worker /*
9088*8b26181fSAndroid Build Coastguard Worker  * Support PPPOE discovery and session.
9089*8b26181fSAndroid Build Coastguard Worker  */
9090*8b26181fSAndroid Build Coastguard Worker struct block *
gen_pppoed(compiler_state_t * cstate)9091*8b26181fSAndroid Build Coastguard Worker gen_pppoed(compiler_state_t *cstate)
9092*8b26181fSAndroid Build Coastguard Worker {
9093*8b26181fSAndroid Build Coastguard Worker 	/*
9094*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9095*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9096*8b26181fSAndroid Build Coastguard Worker 	 */
9097*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9098*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9099*8b26181fSAndroid Build Coastguard Worker 
9100*8b26181fSAndroid Build Coastguard Worker 	/* check for PPPoE discovery */
9101*8b26181fSAndroid Build Coastguard Worker 	return gen_linktype(cstate, ETHERTYPE_PPPOED);
9102*8b26181fSAndroid Build Coastguard Worker }
9103*8b26181fSAndroid Build Coastguard Worker 
9104*8b26181fSAndroid Build Coastguard Worker struct block *
gen_pppoes(compiler_state_t * cstate,bpf_u_int32 sess_num,int has_sess_num)9105*8b26181fSAndroid Build Coastguard Worker gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num)
9106*8b26181fSAndroid Build Coastguard Worker {
9107*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9108*8b26181fSAndroid Build Coastguard Worker 
9109*8b26181fSAndroid Build Coastguard Worker 	/*
9110*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9111*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9112*8b26181fSAndroid Build Coastguard Worker 	 */
9113*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9114*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9115*8b26181fSAndroid Build Coastguard Worker 
9116*8b26181fSAndroid Build Coastguard Worker 	/*
9117*8b26181fSAndroid Build Coastguard Worker 	 * Test against the PPPoE session link-layer type.
9118*8b26181fSAndroid Build Coastguard Worker 	 */
9119*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_linktype(cstate, ETHERTYPE_PPPOES);
9120*8b26181fSAndroid Build Coastguard Worker 
9121*8b26181fSAndroid Build Coastguard Worker 	/* If a specific session is requested, check PPPoE session id */
9122*8b26181fSAndroid Build Coastguard Worker 	if (has_sess_num) {
9123*8b26181fSAndroid Build Coastguard Worker 		if (sess_num > 0x0000ffff) {
9124*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "PPPoE session number %u greater than maximum %u",
9125*8b26181fSAndroid Build Coastguard Worker 			    sess_num, 0x0000ffff);
9126*8b26181fSAndroid Build Coastguard Worker 		}
9127*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, sess_num, 0x0000ffff);
9128*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9129*8b26181fSAndroid Build Coastguard Worker 		b0 = b1;
9130*8b26181fSAndroid Build Coastguard Worker 	}
9131*8b26181fSAndroid Build Coastguard Worker 
9132*8b26181fSAndroid Build Coastguard Worker 	/*
9133*8b26181fSAndroid Build Coastguard Worker 	 * Change the offsets to point to the type and data fields within
9134*8b26181fSAndroid Build Coastguard Worker 	 * the PPP packet, and note that this is PPPoE rather than
9135*8b26181fSAndroid Build Coastguard Worker 	 * raw PPP.
9136*8b26181fSAndroid Build Coastguard Worker 	 *
9137*8b26181fSAndroid Build Coastguard Worker 	 * XXX - this is a bit of a kludge.  See the comments in
9138*8b26181fSAndroid Build Coastguard Worker 	 * gen_vlan().
9139*8b26181fSAndroid Build Coastguard Worker 	 *
9140*8b26181fSAndroid Build Coastguard Worker 	 * The "network-layer" protocol is PPPoE, which has a 6-byte
9141*8b26181fSAndroid Build Coastguard Worker 	 * PPPoE header, followed by a PPP packet.
9142*8b26181fSAndroid Build Coastguard Worker 	 *
9143*8b26181fSAndroid Build Coastguard Worker 	 * There is no HDLC encapsulation for the PPP packet (it's
9144*8b26181fSAndroid Build Coastguard Worker 	 * encapsulated in PPPoES instead), so the link-layer type
9145*8b26181fSAndroid Build Coastguard Worker 	 * starts at the first byte of the PPP packet.  For PPPoE,
9146*8b26181fSAndroid Build Coastguard Worker 	 * that offset is relative to the beginning of the total
9147*8b26181fSAndroid Build Coastguard Worker 	 * link-layer payload, including any 802.2 LLC header, so
9148*8b26181fSAndroid Build Coastguard Worker 	 * it's 6 bytes past cstate->off_nl.
9149*8b26181fSAndroid Build Coastguard Worker 	 */
9150*8b26181fSAndroid Build Coastguard Worker 	PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,
9151*8b26181fSAndroid Build Coastguard Worker 	    cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */
9152*8b26181fSAndroid Build Coastguard Worker 	    cstate->off_linkpl.reg);
9153*8b26181fSAndroid Build Coastguard Worker 
9154*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linktype = cstate->off_linkhdr;
9155*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;
9156*8b26181fSAndroid Build Coastguard Worker 
9157*8b26181fSAndroid Build Coastguard Worker 	cstate->off_nl = 0;
9158*8b26181fSAndroid Build Coastguard Worker 	cstate->off_nl_nosnap = 0;	/* no 802.2 LLC */
9159*8b26181fSAndroid Build Coastguard Worker 
9160*8b26181fSAndroid Build Coastguard Worker 	return b0;
9161*8b26181fSAndroid Build Coastguard Worker }
9162*8b26181fSAndroid Build Coastguard Worker 
9163*8b26181fSAndroid Build Coastguard Worker /* Check that this is Geneve and the VNI is correct if
9164*8b26181fSAndroid Build Coastguard Worker  * specified. Parameterized to handle both IPv4 and IPv6. */
9165*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_geneve_check(compiler_state_t * cstate,struct block * (* gen_portfn)(compiler_state_t *,u_int,int,int),enum e_offrel offrel,bpf_u_int32 vni,int has_vni)9166*8b26181fSAndroid Build Coastguard Worker gen_geneve_check(compiler_state_t *cstate,
9167*8b26181fSAndroid Build Coastguard Worker     struct block *(*gen_portfn)(compiler_state_t *, u_int, int, int),
9168*8b26181fSAndroid Build Coastguard Worker     enum e_offrel offrel, bpf_u_int32 vni, int has_vni)
9169*8b26181fSAndroid Build Coastguard Worker {
9170*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9171*8b26181fSAndroid Build Coastguard Worker 
9172*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);
9173*8b26181fSAndroid Build Coastguard Worker 
9174*8b26181fSAndroid Build Coastguard Worker 	/* Check that we are operating on version 0. Otherwise, we
9175*8b26181fSAndroid Build Coastguard Worker 	 * can't decode the rest of the fields. The version is 2 bits
9176*8b26181fSAndroid Build Coastguard Worker 	 * in the first byte of the Geneve header. */
9177*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0);
9178*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
9179*8b26181fSAndroid Build Coastguard Worker 	b0 = b1;
9180*8b26181fSAndroid Build Coastguard Worker 
9181*8b26181fSAndroid Build Coastguard Worker 	if (has_vni) {
9182*8b26181fSAndroid Build Coastguard Worker 		if (vni > 0xffffff) {
9183*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "Geneve VNI %u greater than maximum %u",
9184*8b26181fSAndroid Build Coastguard Worker 			    vni, 0xffffff);
9185*8b26181fSAndroid Build Coastguard Worker 		}
9186*8b26181fSAndroid Build Coastguard Worker 		vni <<= 8; /* VNI is in the upper 3 bytes */
9187*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00);
9188*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9189*8b26181fSAndroid Build Coastguard Worker 		b0 = b1;
9190*8b26181fSAndroid Build Coastguard Worker 	}
9191*8b26181fSAndroid Build Coastguard Worker 
9192*8b26181fSAndroid Build Coastguard Worker 	return b0;
9193*8b26181fSAndroid Build Coastguard Worker }
9194*8b26181fSAndroid Build Coastguard Worker 
9195*8b26181fSAndroid Build Coastguard Worker /* The IPv4 and IPv6 Geneve checks need to do two things:
9196*8b26181fSAndroid Build Coastguard Worker  * - Verify that this actually is Geneve with the right VNI.
9197*8b26181fSAndroid Build Coastguard Worker  * - Place the IP header length (plus variable link prefix if
9198*8b26181fSAndroid Build Coastguard Worker  *   needed) into register A to be used later to compute
9199*8b26181fSAndroid Build Coastguard Worker  *   the inner packet offsets. */
9200*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_geneve4(compiler_state_t * cstate,bpf_u_int32 vni,int has_vni)9201*8b26181fSAndroid Build Coastguard Worker gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9202*8b26181fSAndroid Build Coastguard Worker {
9203*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9204*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s1;
9205*8b26181fSAndroid Build Coastguard Worker 
9206*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni);
9207*8b26181fSAndroid Build Coastguard Worker 
9208*8b26181fSAndroid Build Coastguard Worker 	/* Load the IP header length into A. */
9209*8b26181fSAndroid Build Coastguard Worker 	s = gen_loadx_iphdrlen(cstate);
9210*8b26181fSAndroid Build Coastguard Worker 
9211*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9212*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9213*8b26181fSAndroid Build Coastguard Worker 
9214*8b26181fSAndroid Build Coastguard Worker 	/* Forcibly append these statements to the true condition
9215*8b26181fSAndroid Build Coastguard Worker 	 * of the protocol check by creating a new block that is
9216*8b26181fSAndroid Build Coastguard Worker 	 * always true and ANDing them. */
9217*8b26181fSAndroid Build Coastguard Worker 	b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9218*8b26181fSAndroid Build Coastguard Worker 	b1->stmts = s;
9219*8b26181fSAndroid Build Coastguard Worker 	b1->s.k = 0;
9220*8b26181fSAndroid Build Coastguard Worker 
9221*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
9222*8b26181fSAndroid Build Coastguard Worker 
9223*8b26181fSAndroid Build Coastguard Worker 	return b1;
9224*8b26181fSAndroid Build Coastguard Worker }
9225*8b26181fSAndroid Build Coastguard Worker 
9226*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_geneve6(compiler_state_t * cstate,bpf_u_int32 vni,int has_vni)9227*8b26181fSAndroid Build Coastguard Worker gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9228*8b26181fSAndroid Build Coastguard Worker {
9229*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9230*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s1;
9231*8b26181fSAndroid Build Coastguard Worker 
9232*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni);
9233*8b26181fSAndroid Build Coastguard Worker 
9234*8b26181fSAndroid Build Coastguard Worker 	/* Load the IP header length. We need to account for a
9235*8b26181fSAndroid Build Coastguard Worker 	 * variable length link prefix if there is one. */
9236*8b26181fSAndroid Build Coastguard Worker 	s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
9237*8b26181fSAndroid Build Coastguard Worker 	if (s) {
9238*8b26181fSAndroid Build Coastguard Worker 		s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
9239*8b26181fSAndroid Build Coastguard Worker 		s1->s.k = 40;
9240*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s1);
9241*8b26181fSAndroid Build Coastguard Worker 
9242*8b26181fSAndroid Build Coastguard Worker 		s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9243*8b26181fSAndroid Build Coastguard Worker 		s1->s.k = 0;
9244*8b26181fSAndroid Build Coastguard Worker 		sappend(s, s1);
9245*8b26181fSAndroid Build Coastguard Worker 	} else {
9246*8b26181fSAndroid Build Coastguard Worker 		s = new_stmt(cstate, BPF_LD|BPF_IMM);
9247*8b26181fSAndroid Build Coastguard Worker 		s->s.k = 40;
9248*8b26181fSAndroid Build Coastguard Worker 	}
9249*8b26181fSAndroid Build Coastguard Worker 
9250*8b26181fSAndroid Build Coastguard Worker 	/* Forcibly append these statements to the true condition
9251*8b26181fSAndroid Build Coastguard Worker 	 * of the protocol check by creating a new block that is
9252*8b26181fSAndroid Build Coastguard Worker 	 * always true and ANDing them. */
9253*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9254*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9255*8b26181fSAndroid Build Coastguard Worker 
9256*8b26181fSAndroid Build Coastguard Worker 	b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9257*8b26181fSAndroid Build Coastguard Worker 	b1->stmts = s;
9258*8b26181fSAndroid Build Coastguard Worker 	b1->s.k = 0;
9259*8b26181fSAndroid Build Coastguard Worker 
9260*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
9261*8b26181fSAndroid Build Coastguard Worker 
9262*8b26181fSAndroid Build Coastguard Worker 	return b1;
9263*8b26181fSAndroid Build Coastguard Worker }
9264*8b26181fSAndroid Build Coastguard Worker 
9265*8b26181fSAndroid Build Coastguard Worker /* We need to store three values based on the Geneve header::
9266*8b26181fSAndroid Build Coastguard Worker  * - The offset of the linktype.
9267*8b26181fSAndroid Build Coastguard Worker  * - The offset of the end of the Geneve header.
9268*8b26181fSAndroid Build Coastguard Worker  * - The offset of the end of the encapsulated MAC header. */
9269*8b26181fSAndroid Build Coastguard Worker static struct slist *
gen_geneve_offsets(compiler_state_t * cstate)9270*8b26181fSAndroid Build Coastguard Worker gen_geneve_offsets(compiler_state_t *cstate)
9271*8b26181fSAndroid Build Coastguard Worker {
9272*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s1, *s_proto;
9273*8b26181fSAndroid Build Coastguard Worker 
9274*8b26181fSAndroid Build Coastguard Worker 	/* First we need to calculate the offset of the Geneve header
9275*8b26181fSAndroid Build Coastguard Worker 	 * itself. This is composed of the IP header previously calculated
9276*8b26181fSAndroid Build Coastguard Worker 	 * (include any variable link prefix) and stored in A plus the
9277*8b26181fSAndroid Build Coastguard Worker 	 * fixed sized headers (fixed link prefix, MAC length, and UDP
9278*8b26181fSAndroid Build Coastguard Worker 	 * header). */
9279*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9280*8b26181fSAndroid Build Coastguard Worker 	s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
9281*8b26181fSAndroid Build Coastguard Worker 
9282*8b26181fSAndroid Build Coastguard Worker 	/* Stash this in X since we'll need it later. */
9283*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9284*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9285*8b26181fSAndroid Build Coastguard Worker 
9286*8b26181fSAndroid Build Coastguard Worker 	/* The EtherType in Geneve is 2 bytes in. Calculate this and
9287*8b26181fSAndroid Build Coastguard Worker 	 * store it. */
9288*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9289*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 2;
9290*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9291*8b26181fSAndroid Build Coastguard Worker 
9292*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linktype.reg = alloc_reg(cstate);
9293*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linktype.is_variable = 1;
9294*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linktype.constant_part = 0;
9295*8b26181fSAndroid Build Coastguard Worker 
9296*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ST);
9297*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = cstate->off_linktype.reg;
9298*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9299*8b26181fSAndroid Build Coastguard Worker 
9300*8b26181fSAndroid Build Coastguard Worker 	/* Load the Geneve option length and mask and shift to get the
9301*8b26181fSAndroid Build Coastguard Worker 	 * number of bytes. It is stored in the first byte of the Geneve
9302*8b26181fSAndroid Build Coastguard Worker 	 * header. */
9303*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
9304*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 0;
9305*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9306*8b26181fSAndroid Build Coastguard Worker 
9307*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
9308*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 0x3f;
9309*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9310*8b26181fSAndroid Build Coastguard Worker 
9311*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
9312*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 4;
9313*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9314*8b26181fSAndroid Build Coastguard Worker 
9315*8b26181fSAndroid Build Coastguard Worker 	/* Add in the rest of the Geneve base header. */
9316*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9317*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 8;
9318*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9319*8b26181fSAndroid Build Coastguard Worker 
9320*8b26181fSAndroid Build Coastguard Worker 	/* Add the Geneve header length to its offset and store. */
9321*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9322*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 0;
9323*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9324*8b26181fSAndroid Build Coastguard Worker 
9325*8b26181fSAndroid Build Coastguard Worker 	/* Set the encapsulated type as Ethernet. Even though we may
9326*8b26181fSAndroid Build Coastguard Worker 	 * not actually have Ethernet inside there are two reasons this
9327*8b26181fSAndroid Build Coastguard Worker 	 * is useful:
9328*8b26181fSAndroid Build Coastguard Worker 	 * - The linktype field is always in EtherType format regardless
9329*8b26181fSAndroid Build Coastguard Worker 	 *   of whether it is in Geneve or an inner Ethernet frame.
9330*8b26181fSAndroid Build Coastguard Worker 	 * - The only link layer that we have specific support for is
9331*8b26181fSAndroid Build Coastguard Worker 	 *   Ethernet. We will confirm that the packet actually is
9332*8b26181fSAndroid Build Coastguard Worker 	 *   Ethernet at runtime before executing these checks. */
9333*8b26181fSAndroid Build Coastguard Worker 	PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
9334*8b26181fSAndroid Build Coastguard Worker 
9335*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ST);
9336*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = cstate->off_linkhdr.reg;
9337*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9338*8b26181fSAndroid Build Coastguard Worker 
9339*8b26181fSAndroid Build Coastguard Worker 	/* Calculate whether we have an Ethernet header or just raw IP/
9340*8b26181fSAndroid Build Coastguard Worker 	 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9341*8b26181fSAndroid Build Coastguard Worker 	 * and linktype by 14 bytes so that the network header can be found
9342*8b26181fSAndroid Build Coastguard Worker 	 * seamlessly. Otherwise, keep what we've calculated already. */
9343*8b26181fSAndroid Build Coastguard Worker 
9344*8b26181fSAndroid Build Coastguard Worker 	/* We have a bare jmp so we can't use the optimizer. */
9345*8b26181fSAndroid Build Coastguard Worker 	cstate->no_optimize = 1;
9346*8b26181fSAndroid Build Coastguard Worker 
9347*8b26181fSAndroid Build Coastguard Worker 	/* Load the EtherType in the Geneve header, 2 bytes in. */
9348*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);
9349*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 2;
9350*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9351*8b26181fSAndroid Build Coastguard Worker 
9352*8b26181fSAndroid Build Coastguard Worker 	/* Load X with the end of the Geneve header. */
9353*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9354*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = cstate->off_linkhdr.reg;
9355*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9356*8b26181fSAndroid Build Coastguard Worker 
9357*8b26181fSAndroid Build Coastguard Worker 	/* Check if the EtherType is Transparent Ethernet Bridging. At the
9358*8b26181fSAndroid Build Coastguard Worker 	 * end of this check, we should have the total length in X. In
9359*8b26181fSAndroid Build Coastguard Worker 	 * the non-Ethernet case, it's already there. */
9360*8b26181fSAndroid Build Coastguard Worker 	s_proto = new_stmt(cstate, JMP(BPF_JEQ));
9361*8b26181fSAndroid Build Coastguard Worker 	s_proto->s.k = ETHERTYPE_TEB;
9362*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s_proto);
9363*8b26181fSAndroid Build Coastguard Worker 
9364*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9365*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9366*8b26181fSAndroid Build Coastguard Worker 	s_proto->s.jt = s1;
9367*8b26181fSAndroid Build Coastguard Worker 
9368*8b26181fSAndroid Build Coastguard Worker 	/* Since this is Ethernet, use the EtherType of the payload
9369*8b26181fSAndroid Build Coastguard Worker 	 * directly as the linktype. Overwrite what we already have. */
9370*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9371*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 12;
9372*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9373*8b26181fSAndroid Build Coastguard Worker 
9374*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ST);
9375*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = cstate->off_linktype.reg;
9376*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9377*8b26181fSAndroid Build Coastguard Worker 
9378*8b26181fSAndroid Build Coastguard Worker 	/* Advance two bytes further to get the end of the Ethernet
9379*8b26181fSAndroid Build Coastguard Worker 	 * header. */
9380*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9381*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = 2;
9382*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9383*8b26181fSAndroid Build Coastguard Worker 
9384*8b26181fSAndroid Build Coastguard Worker 	/* Move the result to X. */
9385*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9386*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9387*8b26181fSAndroid Build Coastguard Worker 
9388*8b26181fSAndroid Build Coastguard Worker 	/* Store the final result of our linkpl calculation. */
9389*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkpl.reg = alloc_reg(cstate);
9390*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkpl.is_variable = 1;
9391*8b26181fSAndroid Build Coastguard Worker 	cstate->off_linkpl.constant_part = 0;
9392*8b26181fSAndroid Build Coastguard Worker 
9393*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_STX);
9394*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = cstate->off_linkpl.reg;
9395*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9396*8b26181fSAndroid Build Coastguard Worker 	s_proto->s.jf = s1;
9397*8b26181fSAndroid Build Coastguard Worker 
9398*8b26181fSAndroid Build Coastguard Worker 	cstate->off_nl = 0;
9399*8b26181fSAndroid Build Coastguard Worker 
9400*8b26181fSAndroid Build Coastguard Worker 	return s;
9401*8b26181fSAndroid Build Coastguard Worker }
9402*8b26181fSAndroid Build Coastguard Worker 
9403*8b26181fSAndroid Build Coastguard Worker /* Check to see if this is a Geneve packet. */
9404*8b26181fSAndroid Build Coastguard Worker struct block *
gen_geneve(compiler_state_t * cstate,bpf_u_int32 vni,int has_vni)9405*8b26181fSAndroid Build Coastguard Worker gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9406*8b26181fSAndroid Build Coastguard Worker {
9407*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9408*8b26181fSAndroid Build Coastguard Worker 	struct slist *s;
9409*8b26181fSAndroid Build Coastguard Worker 
9410*8b26181fSAndroid Build Coastguard Worker 	/*
9411*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9412*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9413*8b26181fSAndroid Build Coastguard Worker 	 */
9414*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9415*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9416*8b26181fSAndroid Build Coastguard Worker 
9417*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_geneve4(cstate, vni, has_vni);
9418*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_geneve6(cstate, vni, has_vni);
9419*8b26181fSAndroid Build Coastguard Worker 
9420*8b26181fSAndroid Build Coastguard Worker 	gen_or(b0, b1);
9421*8b26181fSAndroid Build Coastguard Worker 	b0 = b1;
9422*8b26181fSAndroid Build Coastguard Worker 
9423*8b26181fSAndroid Build Coastguard Worker 	/* Later filters should act on the payload of the Geneve frame,
9424*8b26181fSAndroid Build Coastguard Worker 	 * update all of the header pointers. Attach this code so that
9425*8b26181fSAndroid Build Coastguard Worker 	 * it gets executed in the event that the Geneve filter matches. */
9426*8b26181fSAndroid Build Coastguard Worker 	s = gen_geneve_offsets(cstate);
9427*8b26181fSAndroid Build Coastguard Worker 
9428*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_true(cstate);
9429*8b26181fSAndroid Build Coastguard Worker 	sappend(s, b1->stmts);
9430*8b26181fSAndroid Build Coastguard Worker 	b1->stmts = s;
9431*8b26181fSAndroid Build Coastguard Worker 
9432*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
9433*8b26181fSAndroid Build Coastguard Worker 
9434*8b26181fSAndroid Build Coastguard Worker 	cstate->is_geneve = 1;
9435*8b26181fSAndroid Build Coastguard Worker 
9436*8b26181fSAndroid Build Coastguard Worker 	return b1;
9437*8b26181fSAndroid Build Coastguard Worker }
9438*8b26181fSAndroid Build Coastguard Worker 
9439*8b26181fSAndroid Build Coastguard Worker /* Check that the encapsulated frame has a link layer header
9440*8b26181fSAndroid Build Coastguard Worker  * for Ethernet filters. */
9441*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_geneve_ll_check(compiler_state_t * cstate)9442*8b26181fSAndroid Build Coastguard Worker gen_geneve_ll_check(compiler_state_t *cstate)
9443*8b26181fSAndroid Build Coastguard Worker {
9444*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
9445*8b26181fSAndroid Build Coastguard Worker 	struct slist *s, *s1;
9446*8b26181fSAndroid Build Coastguard Worker 
9447*8b26181fSAndroid Build Coastguard Worker 	/* The easiest way to see if there is a link layer present
9448*8b26181fSAndroid Build Coastguard Worker 	 * is to check if the link layer header and payload are not
9449*8b26181fSAndroid Build Coastguard Worker 	 * the same. */
9450*8b26181fSAndroid Build Coastguard Worker 
9451*8b26181fSAndroid Build Coastguard Worker 	/* Geneve always generates pure variable offsets so we can
9452*8b26181fSAndroid Build Coastguard Worker 	 * compare only the registers. */
9453*8b26181fSAndroid Build Coastguard Worker 	s = new_stmt(cstate, BPF_LD|BPF_MEM);
9454*8b26181fSAndroid Build Coastguard Worker 	s->s.k = cstate->off_linkhdr.reg;
9455*8b26181fSAndroid Build Coastguard Worker 
9456*8b26181fSAndroid Build Coastguard Worker 	s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9457*8b26181fSAndroid Build Coastguard Worker 	s1->s.k = cstate->off_linkpl.reg;
9458*8b26181fSAndroid Build Coastguard Worker 	sappend(s, s1);
9459*8b26181fSAndroid Build Coastguard Worker 
9460*8b26181fSAndroid Build Coastguard Worker 	b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9461*8b26181fSAndroid Build Coastguard Worker 	b0->stmts = s;
9462*8b26181fSAndroid Build Coastguard Worker 	b0->s.k = 0;
9463*8b26181fSAndroid Build Coastguard Worker 	gen_not(b0);
9464*8b26181fSAndroid Build Coastguard Worker 
9465*8b26181fSAndroid Build Coastguard Worker 	return b0;
9466*8b26181fSAndroid Build Coastguard Worker }
9467*8b26181fSAndroid Build Coastguard Worker 
9468*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_atmfield_code_internal(compiler_state_t * cstate,int atmfield,bpf_u_int32 jvalue,int jtype,int reverse)9469*8b26181fSAndroid Build Coastguard Worker gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield,
9470*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 jvalue, int jtype, int reverse)
9471*8b26181fSAndroid Build Coastguard Worker {
9472*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
9473*8b26181fSAndroid Build Coastguard Worker 
9474*8b26181fSAndroid Build Coastguard Worker 	switch (atmfield) {
9475*8b26181fSAndroid Build Coastguard Worker 
9476*8b26181fSAndroid Build Coastguard Worker 	case A_VPI:
9477*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9478*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'vpi' supported only on raw ATM");
9479*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_vpi == OFFSET_NOT_SET)
9480*8b26181fSAndroid Build Coastguard Worker 			abort();
9481*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B,
9482*8b26181fSAndroid Build Coastguard Worker 		    0xffffffffU, jtype, reverse, jvalue);
9483*8b26181fSAndroid Build Coastguard Worker 		break;
9484*8b26181fSAndroid Build Coastguard Worker 
9485*8b26181fSAndroid Build Coastguard Worker 	case A_VCI:
9486*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9487*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'vci' supported only on raw ATM");
9488*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_vci == OFFSET_NOT_SET)
9489*8b26181fSAndroid Build Coastguard Worker 			abort();
9490*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H,
9491*8b26181fSAndroid Build Coastguard Worker 		    0xffffffffU, jtype, reverse, jvalue);
9492*8b26181fSAndroid Build Coastguard Worker 		break;
9493*8b26181fSAndroid Build Coastguard Worker 
9494*8b26181fSAndroid Build Coastguard Worker 	case A_PROTOTYPE:
9495*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_proto == OFFSET_NOT_SET)
9496*8b26181fSAndroid Build Coastguard Worker 			abort();	/* XXX - this isn't on FreeBSD */
9497*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
9498*8b26181fSAndroid Build Coastguard Worker 		    0x0fU, jtype, reverse, jvalue);
9499*8b26181fSAndroid Build Coastguard Worker 		break;
9500*8b26181fSAndroid Build Coastguard Worker 
9501*8b26181fSAndroid Build Coastguard Worker 	case A_MSGTYPE:
9502*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_payload == OFFSET_NOT_SET)
9503*8b26181fSAndroid Build Coastguard Worker 			abort();
9504*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,
9505*8b26181fSAndroid Build Coastguard Worker 		    0xffffffffU, jtype, reverse, jvalue);
9506*8b26181fSAndroid Build Coastguard Worker 		break;
9507*8b26181fSAndroid Build Coastguard Worker 
9508*8b26181fSAndroid Build Coastguard Worker 	case A_CALLREFTYPE:
9509*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9510*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'callref' supported only on raw ATM");
9511*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_proto == OFFSET_NOT_SET)
9512*8b26181fSAndroid Build Coastguard Worker 			abort();
9513*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
9514*8b26181fSAndroid Build Coastguard Worker 		    0xffffffffU, jtype, reverse, jvalue);
9515*8b26181fSAndroid Build Coastguard Worker 		break;
9516*8b26181fSAndroid Build Coastguard Worker 
9517*8b26181fSAndroid Build Coastguard Worker 	default:
9518*8b26181fSAndroid Build Coastguard Worker 		abort();
9519*8b26181fSAndroid Build Coastguard Worker 	}
9520*8b26181fSAndroid Build Coastguard Worker 	return b0;
9521*8b26181fSAndroid Build Coastguard Worker }
9522*8b26181fSAndroid Build Coastguard Worker 
9523*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_atmtype_metac(compiler_state_t * cstate)9524*8b26181fSAndroid Build Coastguard Worker gen_atmtype_metac(compiler_state_t *cstate)
9525*8b26181fSAndroid Build Coastguard Worker {
9526*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9527*8b26181fSAndroid Build Coastguard Worker 
9528*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9529*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0);
9530*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
9531*8b26181fSAndroid Build Coastguard Worker 	return b1;
9532*8b26181fSAndroid Build Coastguard Worker }
9533*8b26181fSAndroid Build Coastguard Worker 
9534*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_atmtype_sc(compiler_state_t * cstate)9535*8b26181fSAndroid Build Coastguard Worker gen_atmtype_sc(compiler_state_t *cstate)
9536*8b26181fSAndroid Build Coastguard Worker {
9537*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9538*8b26181fSAndroid Build Coastguard Worker 
9539*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9540*8b26181fSAndroid Build Coastguard Worker 	b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0);
9541*8b26181fSAndroid Build Coastguard Worker 	gen_and(b0, b1);
9542*8b26181fSAndroid Build Coastguard Worker 	return b1;
9543*8b26181fSAndroid Build Coastguard Worker }
9544*8b26181fSAndroid Build Coastguard Worker 
9545*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_atmtype_llc(compiler_state_t * cstate)9546*8b26181fSAndroid Build Coastguard Worker gen_atmtype_llc(compiler_state_t *cstate)
9547*8b26181fSAndroid Build Coastguard Worker {
9548*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
9549*8b26181fSAndroid Build Coastguard Worker 
9550*8b26181fSAndroid Build Coastguard Worker 	b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
9551*8b26181fSAndroid Build Coastguard Worker 	cstate->linktype = cstate->prevlinktype;
9552*8b26181fSAndroid Build Coastguard Worker 	return b0;
9553*8b26181fSAndroid Build Coastguard Worker }
9554*8b26181fSAndroid Build Coastguard Worker 
9555*8b26181fSAndroid Build Coastguard Worker struct block *
gen_atmfield_code(compiler_state_t * cstate,int atmfield,bpf_u_int32 jvalue,int jtype,int reverse)9556*8b26181fSAndroid Build Coastguard Worker gen_atmfield_code(compiler_state_t *cstate, int atmfield,
9557*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 jvalue, int jtype, int reverse)
9558*8b26181fSAndroid Build Coastguard Worker {
9559*8b26181fSAndroid Build Coastguard Worker 	/*
9560*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9561*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9562*8b26181fSAndroid Build Coastguard Worker 	 */
9563*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9564*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9565*8b26181fSAndroid Build Coastguard Worker 
9566*8b26181fSAndroid Build Coastguard Worker 	return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype,
9567*8b26181fSAndroid Build Coastguard Worker 	    reverse);
9568*8b26181fSAndroid Build Coastguard Worker }
9569*8b26181fSAndroid Build Coastguard Worker 
9570*8b26181fSAndroid Build Coastguard Worker struct block *
gen_atmtype_abbrev(compiler_state_t * cstate,int type)9571*8b26181fSAndroid Build Coastguard Worker gen_atmtype_abbrev(compiler_state_t *cstate, int type)
9572*8b26181fSAndroid Build Coastguard Worker {
9573*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9574*8b26181fSAndroid Build Coastguard Worker 
9575*8b26181fSAndroid Build Coastguard Worker 	/*
9576*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9577*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9578*8b26181fSAndroid Build Coastguard Worker 	 */
9579*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9580*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9581*8b26181fSAndroid Build Coastguard Worker 
9582*8b26181fSAndroid Build Coastguard Worker 	switch (type) {
9583*8b26181fSAndroid Build Coastguard Worker 
9584*8b26181fSAndroid Build Coastguard Worker 	case A_METAC:
9585*8b26181fSAndroid Build Coastguard Worker 		/* Get all packets in Meta signalling Circuit */
9586*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9587*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'metac' supported only on raw ATM");
9588*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmtype_metac(cstate);
9589*8b26181fSAndroid Build Coastguard Worker 		break;
9590*8b26181fSAndroid Build Coastguard Worker 
9591*8b26181fSAndroid Build Coastguard Worker 	case A_BCC:
9592*8b26181fSAndroid Build Coastguard Worker 		/* Get all packets in Broadcast Circuit*/
9593*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9594*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'bcc' supported only on raw ATM");
9595*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9596*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0);
9597*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9598*8b26181fSAndroid Build Coastguard Worker 		break;
9599*8b26181fSAndroid Build Coastguard Worker 
9600*8b26181fSAndroid Build Coastguard Worker 	case A_OAMF4SC:
9601*8b26181fSAndroid Build Coastguard Worker 		/* Get all cells in Segment OAM F4 circuit*/
9602*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9603*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'oam4sc' supported only on raw ATM");
9604*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9605*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9606*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9607*8b26181fSAndroid Build Coastguard Worker 		break;
9608*8b26181fSAndroid Build Coastguard Worker 
9609*8b26181fSAndroid Build Coastguard Worker 	case A_OAMF4EC:
9610*8b26181fSAndroid Build Coastguard Worker 		/* Get all cells in End-to-End OAM F4 Circuit*/
9611*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9612*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'oam4ec' supported only on raw ATM");
9613*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9614*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9615*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9616*8b26181fSAndroid Build Coastguard Worker 		break;
9617*8b26181fSAndroid Build Coastguard Worker 
9618*8b26181fSAndroid Build Coastguard Worker 	case A_SC:
9619*8b26181fSAndroid Build Coastguard Worker 		/*  Get all packets in connection Signalling Circuit */
9620*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9621*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'sc' supported only on raw ATM");
9622*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmtype_sc(cstate);
9623*8b26181fSAndroid Build Coastguard Worker 		break;
9624*8b26181fSAndroid Build Coastguard Worker 
9625*8b26181fSAndroid Build Coastguard Worker 	case A_ILMIC:
9626*8b26181fSAndroid Build Coastguard Worker 		/* Get all packets in ILMI Circuit */
9627*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9628*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'ilmic' supported only on raw ATM");
9629*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9630*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0);
9631*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9632*8b26181fSAndroid Build Coastguard Worker 		break;
9633*8b26181fSAndroid Build Coastguard Worker 
9634*8b26181fSAndroid Build Coastguard Worker 	case A_LANE:
9635*8b26181fSAndroid Build Coastguard Worker 		/* Get all LANE packets */
9636*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9637*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'lane' supported only on raw ATM");
9638*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
9639*8b26181fSAndroid Build Coastguard Worker 
9640*8b26181fSAndroid Build Coastguard Worker 		/*
9641*8b26181fSAndroid Build Coastguard Worker 		 * Arrange that all subsequent tests assume LANE
9642*8b26181fSAndroid Build Coastguard Worker 		 * rather than LLC-encapsulated packets, and set
9643*8b26181fSAndroid Build Coastguard Worker 		 * the offsets appropriately for LANE-encapsulated
9644*8b26181fSAndroid Build Coastguard Worker 		 * Ethernet.
9645*8b26181fSAndroid Build Coastguard Worker 		 *
9646*8b26181fSAndroid Build Coastguard Worker 		 * We assume LANE means Ethernet, not Token Ring.
9647*8b26181fSAndroid Build Coastguard Worker 		 */
9648*8b26181fSAndroid Build Coastguard Worker 		PUSH_LINKHDR(cstate, DLT_EN10MB, 0,
9649*8b26181fSAndroid Build Coastguard Worker 		    cstate->off_payload + 2,	/* Ethernet header */
9650*8b26181fSAndroid Build Coastguard Worker 		    -1);
9651*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
9652*8b26181fSAndroid Build Coastguard Worker 		cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;	/* Ethernet */
9653*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl = 0;			/* Ethernet II */
9654*8b26181fSAndroid Build Coastguard Worker 		cstate->off_nl_nosnap = 3;		/* 802.3+802.2 */
9655*8b26181fSAndroid Build Coastguard Worker 		break;
9656*8b26181fSAndroid Build Coastguard Worker 
9657*8b26181fSAndroid Build Coastguard Worker 	case A_LLC:
9658*8b26181fSAndroid Build Coastguard Worker 		/* Get all LLC-encapsulated packets */
9659*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9660*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'llc' supported only on raw ATM");
9661*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmtype_llc(cstate);
9662*8b26181fSAndroid Build Coastguard Worker 		break;
9663*8b26181fSAndroid Build Coastguard Worker 
9664*8b26181fSAndroid Build Coastguard Worker 	default:
9665*8b26181fSAndroid Build Coastguard Worker 		abort();
9666*8b26181fSAndroid Build Coastguard Worker 	}
9667*8b26181fSAndroid Build Coastguard Worker 	return b1;
9668*8b26181fSAndroid Build Coastguard Worker }
9669*8b26181fSAndroid Build Coastguard Worker 
9670*8b26181fSAndroid Build Coastguard Worker /*
9671*8b26181fSAndroid Build Coastguard Worker  * Filtering for MTP2 messages based on li value
9672*8b26181fSAndroid Build Coastguard Worker  * FISU, length is null
9673*8b26181fSAndroid Build Coastguard Worker  * LSSU, length is 1 or 2
9674*8b26181fSAndroid Build Coastguard Worker  * MSU, length is 3 or more
9675*8b26181fSAndroid Build Coastguard Worker  * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9676*8b26181fSAndroid Build Coastguard Worker  */
9677*8b26181fSAndroid Build Coastguard Worker struct block *
gen_mtp2type_abbrev(compiler_state_t * cstate,int type)9678*8b26181fSAndroid Build Coastguard Worker gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
9679*8b26181fSAndroid Build Coastguard Worker {
9680*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9681*8b26181fSAndroid Build Coastguard Worker 
9682*8b26181fSAndroid Build Coastguard Worker 	/*
9683*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9684*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9685*8b26181fSAndroid Build Coastguard Worker 	 */
9686*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9687*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9688*8b26181fSAndroid Build Coastguard Worker 
9689*8b26181fSAndroid Build Coastguard Worker 	switch (type) {
9690*8b26181fSAndroid Build Coastguard Worker 
9691*8b26181fSAndroid Build Coastguard Worker 	case M_FISU:
9692*8b26181fSAndroid Build Coastguard Worker 		if ( (cstate->linktype != DLT_MTP2) &&
9693*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_ERF) &&
9694*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9695*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'fisu' supported only on MTP2");
9696*8b26181fSAndroid Build Coastguard Worker 		/* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9697*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9698*8b26181fSAndroid Build Coastguard Worker 		    0x3fU, BPF_JEQ, 0, 0U);
9699*8b26181fSAndroid Build Coastguard Worker 		break;
9700*8b26181fSAndroid Build Coastguard Worker 
9701*8b26181fSAndroid Build Coastguard Worker 	case M_LSSU:
9702*8b26181fSAndroid Build Coastguard Worker 		if ( (cstate->linktype != DLT_MTP2) &&
9703*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_ERF) &&
9704*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9705*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'lssu' supported only on MTP2");
9706*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9707*8b26181fSAndroid Build Coastguard Worker 		    0x3fU, BPF_JGT, 1, 2U);
9708*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9709*8b26181fSAndroid Build Coastguard Worker 		    0x3fU, BPF_JGT, 0, 0U);
9710*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
9711*8b26181fSAndroid Build Coastguard Worker 		break;
9712*8b26181fSAndroid Build Coastguard Worker 
9713*8b26181fSAndroid Build Coastguard Worker 	case M_MSU:
9714*8b26181fSAndroid Build Coastguard Worker 		if ( (cstate->linktype != DLT_MTP2) &&
9715*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_ERF) &&
9716*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9717*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'msu' supported only on MTP2");
9718*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9719*8b26181fSAndroid Build Coastguard Worker 		    0x3fU, BPF_JGT, 0, 2U);
9720*8b26181fSAndroid Build Coastguard Worker 		break;
9721*8b26181fSAndroid Build Coastguard Worker 
9722*8b26181fSAndroid Build Coastguard Worker 	case MH_FISU:
9723*8b26181fSAndroid Build Coastguard Worker 		if ( (cstate->linktype != DLT_MTP2) &&
9724*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_ERF) &&
9725*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9726*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
9727*8b26181fSAndroid Build Coastguard Worker 		/* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9728*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9729*8b26181fSAndroid Build Coastguard Worker 		    0xff80U, BPF_JEQ, 0, 0U);
9730*8b26181fSAndroid Build Coastguard Worker 		break;
9731*8b26181fSAndroid Build Coastguard Worker 
9732*8b26181fSAndroid Build Coastguard Worker 	case MH_LSSU:
9733*8b26181fSAndroid Build Coastguard Worker 		if ( (cstate->linktype != DLT_MTP2) &&
9734*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_ERF) &&
9735*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9736*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
9737*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9738*8b26181fSAndroid Build Coastguard Worker 		    0xff80U, BPF_JGT, 1, 0x0100U);
9739*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9740*8b26181fSAndroid Build Coastguard Worker 		    0xff80U, BPF_JGT, 0, 0U);
9741*8b26181fSAndroid Build Coastguard Worker 		gen_and(b1, b0);
9742*8b26181fSAndroid Build Coastguard Worker 		break;
9743*8b26181fSAndroid Build Coastguard Worker 
9744*8b26181fSAndroid Build Coastguard Worker 	case MH_MSU:
9745*8b26181fSAndroid Build Coastguard Worker 		if ( (cstate->linktype != DLT_MTP2) &&
9746*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_ERF) &&
9747*8b26181fSAndroid Build Coastguard Worker 		     (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9748*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
9749*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9750*8b26181fSAndroid Build Coastguard Worker 		    0xff80U, BPF_JGT, 0, 0x0100U);
9751*8b26181fSAndroid Build Coastguard Worker 		break;
9752*8b26181fSAndroid Build Coastguard Worker 
9753*8b26181fSAndroid Build Coastguard Worker 	default:
9754*8b26181fSAndroid Build Coastguard Worker 		abort();
9755*8b26181fSAndroid Build Coastguard Worker 	}
9756*8b26181fSAndroid Build Coastguard Worker 	return b0;
9757*8b26181fSAndroid Build Coastguard Worker }
9758*8b26181fSAndroid Build Coastguard Worker 
9759*8b26181fSAndroid Build Coastguard Worker /*
9760*8b26181fSAndroid Build Coastguard Worker  * The jvalue_arg dance is to avoid annoying whining by compilers that
9761*8b26181fSAndroid Build Coastguard Worker  * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9762*8b26181fSAndroid Build Coastguard Worker  * It's not *used* after setjmp returns.
9763*8b26181fSAndroid Build Coastguard Worker  */
9764*8b26181fSAndroid Build Coastguard Worker struct block *
gen_mtp3field_code(compiler_state_t * cstate,int mtp3field,bpf_u_int32 jvalue_arg,int jtype,int reverse)9765*8b26181fSAndroid Build Coastguard Worker gen_mtp3field_code(compiler_state_t *cstate, int mtp3field,
9766*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 jvalue_arg, int jtype, int reverse)
9767*8b26181fSAndroid Build Coastguard Worker {
9768*8b26181fSAndroid Build Coastguard Worker 	volatile bpf_u_int32 jvalue = jvalue_arg;
9769*8b26181fSAndroid Build Coastguard Worker 	struct block *b0;
9770*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 val1 , val2 , val3;
9771*8b26181fSAndroid Build Coastguard Worker 	u_int newoff_sio;
9772*8b26181fSAndroid Build Coastguard Worker 	u_int newoff_opc;
9773*8b26181fSAndroid Build Coastguard Worker 	u_int newoff_dpc;
9774*8b26181fSAndroid Build Coastguard Worker 	u_int newoff_sls;
9775*8b26181fSAndroid Build Coastguard Worker 
9776*8b26181fSAndroid Build Coastguard Worker 	/*
9777*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9778*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9779*8b26181fSAndroid Build Coastguard Worker 	 */
9780*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9781*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9782*8b26181fSAndroid Build Coastguard Worker 
9783*8b26181fSAndroid Build Coastguard Worker 	newoff_sio = cstate->off_sio;
9784*8b26181fSAndroid Build Coastguard Worker 	newoff_opc = cstate->off_opc;
9785*8b26181fSAndroid Build Coastguard Worker 	newoff_dpc = cstate->off_dpc;
9786*8b26181fSAndroid Build Coastguard Worker 	newoff_sls = cstate->off_sls;
9787*8b26181fSAndroid Build Coastguard Worker 	switch (mtp3field) {
9788*8b26181fSAndroid Build Coastguard Worker 
9789*8b26181fSAndroid Build Coastguard Worker 	case MH_SIO:
9790*8b26181fSAndroid Build Coastguard Worker 		newoff_sio += 3; /* offset for MTP2_HSL */
9791*8b26181fSAndroid Build Coastguard Worker 		/* FALLTHROUGH */
9792*8b26181fSAndroid Build Coastguard Worker 
9793*8b26181fSAndroid Build Coastguard Worker 	case M_SIO:
9794*8b26181fSAndroid Build Coastguard Worker 		if (cstate->off_sio == OFFSET_NOT_SET)
9795*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'sio' supported only on SS7");
9796*8b26181fSAndroid Build Coastguard Worker 		/* sio coded on 1 byte so max value 255 */
9797*8b26181fSAndroid Build Coastguard Worker 		if(jvalue > 255)
9798*8b26181fSAndroid Build Coastguard Worker 		        bpf_error(cstate, "sio value %u too big; max value = 255",
9799*8b26181fSAndroid Build Coastguard Worker 		            jvalue);
9800*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffffU,
9801*8b26181fSAndroid Build Coastguard Worker 		    jtype, reverse, jvalue);
9802*8b26181fSAndroid Build Coastguard Worker 		break;
9803*8b26181fSAndroid Build Coastguard Worker 
9804*8b26181fSAndroid Build Coastguard Worker 	case MH_OPC:
9805*8b26181fSAndroid Build Coastguard Worker 		newoff_opc += 3;
9806*8b26181fSAndroid Build Coastguard Worker 
9807*8b26181fSAndroid Build Coastguard Worker 		/* FALLTHROUGH */
9808*8b26181fSAndroid Build Coastguard Worker         case M_OPC:
9809*8b26181fSAndroid Build Coastguard Worker 	        if (cstate->off_opc == OFFSET_NOT_SET)
9810*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'opc' supported only on SS7");
9811*8b26181fSAndroid Build Coastguard Worker 		/* opc coded on 14 bits so max value 16383 */
9812*8b26181fSAndroid Build Coastguard Worker 		if (jvalue > 16383)
9813*8b26181fSAndroid Build Coastguard Worker 		        bpf_error(cstate, "opc value %u too big; max value = 16383",
9814*8b26181fSAndroid Build Coastguard Worker 		            jvalue);
9815*8b26181fSAndroid Build Coastguard Worker 		/* the following instructions are made to convert jvalue
9816*8b26181fSAndroid Build Coastguard Worker 		 * to the form used to write opc in an ss7 message*/
9817*8b26181fSAndroid Build Coastguard Worker 		val1 = jvalue & 0x00003c00;
9818*8b26181fSAndroid Build Coastguard Worker 		val1 = val1 >>10;
9819*8b26181fSAndroid Build Coastguard Worker 		val2 = jvalue & 0x000003fc;
9820*8b26181fSAndroid Build Coastguard Worker 		val2 = val2 <<6;
9821*8b26181fSAndroid Build Coastguard Worker 		val3 = jvalue & 0x00000003;
9822*8b26181fSAndroid Build Coastguard Worker 		val3 = val3 <<22;
9823*8b26181fSAndroid Build Coastguard Worker 		jvalue = val1 + val2 + val3;
9824*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0fU,
9825*8b26181fSAndroid Build Coastguard Worker 		    jtype, reverse, jvalue);
9826*8b26181fSAndroid Build Coastguard Worker 		break;
9827*8b26181fSAndroid Build Coastguard Worker 
9828*8b26181fSAndroid Build Coastguard Worker 	case MH_DPC:
9829*8b26181fSAndroid Build Coastguard Worker 		newoff_dpc += 3;
9830*8b26181fSAndroid Build Coastguard Worker 		/* FALLTHROUGH */
9831*8b26181fSAndroid Build Coastguard Worker 
9832*8b26181fSAndroid Build Coastguard Worker 	case M_DPC:
9833*8b26181fSAndroid Build Coastguard Worker 	        if (cstate->off_dpc == OFFSET_NOT_SET)
9834*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'dpc' supported only on SS7");
9835*8b26181fSAndroid Build Coastguard Worker 		/* dpc coded on 14 bits so max value 16383 */
9836*8b26181fSAndroid Build Coastguard Worker 		if (jvalue > 16383)
9837*8b26181fSAndroid Build Coastguard Worker 		        bpf_error(cstate, "dpc value %u too big; max value = 16383",
9838*8b26181fSAndroid Build Coastguard Worker 		            jvalue);
9839*8b26181fSAndroid Build Coastguard Worker 		/* the following instructions are made to convert jvalue
9840*8b26181fSAndroid Build Coastguard Worker 		 * to the forme used to write dpc in an ss7 message*/
9841*8b26181fSAndroid Build Coastguard Worker 		val1 = jvalue & 0x000000ff;
9842*8b26181fSAndroid Build Coastguard Worker 		val1 = val1 << 24;
9843*8b26181fSAndroid Build Coastguard Worker 		val2 = jvalue & 0x00003f00;
9844*8b26181fSAndroid Build Coastguard Worker 		val2 = val2 << 8;
9845*8b26181fSAndroid Build Coastguard Worker 		jvalue = val1 + val2;
9846*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000U,
9847*8b26181fSAndroid Build Coastguard Worker 		    jtype, reverse, jvalue);
9848*8b26181fSAndroid Build Coastguard Worker 		break;
9849*8b26181fSAndroid Build Coastguard Worker 
9850*8b26181fSAndroid Build Coastguard Worker 	case MH_SLS:
9851*8b26181fSAndroid Build Coastguard Worker 		newoff_sls += 3;
9852*8b26181fSAndroid Build Coastguard Worker 		/* FALLTHROUGH */
9853*8b26181fSAndroid Build Coastguard Worker 
9854*8b26181fSAndroid Build Coastguard Worker 	case M_SLS:
9855*8b26181fSAndroid Build Coastguard Worker 	        if (cstate->off_sls == OFFSET_NOT_SET)
9856*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'sls' supported only on SS7");
9857*8b26181fSAndroid Build Coastguard Worker 		/* sls coded on 4 bits so max value 15 */
9858*8b26181fSAndroid Build Coastguard Worker 		if (jvalue > 15)
9859*8b26181fSAndroid Build Coastguard Worker 		         bpf_error(cstate, "sls value %u too big; max value = 15",
9860*8b26181fSAndroid Build Coastguard Worker 		             jvalue);
9861*8b26181fSAndroid Build Coastguard Worker 		/* the following instruction is made to convert jvalue
9862*8b26181fSAndroid Build Coastguard Worker 		 * to the forme used to write sls in an ss7 message*/
9863*8b26181fSAndroid Build Coastguard Worker 		jvalue = jvalue << 4;
9864*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0U,
9865*8b26181fSAndroid Build Coastguard Worker 		    jtype, reverse, jvalue);
9866*8b26181fSAndroid Build Coastguard Worker 		break;
9867*8b26181fSAndroid Build Coastguard Worker 
9868*8b26181fSAndroid Build Coastguard Worker 	default:
9869*8b26181fSAndroid Build Coastguard Worker 		abort();
9870*8b26181fSAndroid Build Coastguard Worker 	}
9871*8b26181fSAndroid Build Coastguard Worker 	return b0;
9872*8b26181fSAndroid Build Coastguard Worker }
9873*8b26181fSAndroid Build Coastguard Worker 
9874*8b26181fSAndroid Build Coastguard Worker static struct block *
gen_msg_abbrev(compiler_state_t * cstate,int type)9875*8b26181fSAndroid Build Coastguard Worker gen_msg_abbrev(compiler_state_t *cstate, int type)
9876*8b26181fSAndroid Build Coastguard Worker {
9877*8b26181fSAndroid Build Coastguard Worker 	struct block *b1;
9878*8b26181fSAndroid Build Coastguard Worker 
9879*8b26181fSAndroid Build Coastguard Worker 	/*
9880*8b26181fSAndroid Build Coastguard Worker 	 * Q.2931 signalling protocol messages for handling virtual circuits
9881*8b26181fSAndroid Build Coastguard Worker 	 * establishment and teardown
9882*8b26181fSAndroid Build Coastguard Worker 	 */
9883*8b26181fSAndroid Build Coastguard Worker 	switch (type) {
9884*8b26181fSAndroid Build Coastguard Worker 
9885*8b26181fSAndroid Build Coastguard Worker 	case A_SETUP:
9886*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);
9887*8b26181fSAndroid Build Coastguard Worker 		break;
9888*8b26181fSAndroid Build Coastguard Worker 
9889*8b26181fSAndroid Build Coastguard Worker 	case A_CALLPROCEED:
9890*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
9891*8b26181fSAndroid Build Coastguard Worker 		break;
9892*8b26181fSAndroid Build Coastguard Worker 
9893*8b26181fSAndroid Build Coastguard Worker 	case A_CONNECT:
9894*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);
9895*8b26181fSAndroid Build Coastguard Worker 		break;
9896*8b26181fSAndroid Build Coastguard Worker 
9897*8b26181fSAndroid Build Coastguard Worker 	case A_CONNECTACK:
9898*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
9899*8b26181fSAndroid Build Coastguard Worker 		break;
9900*8b26181fSAndroid Build Coastguard Worker 
9901*8b26181fSAndroid Build Coastguard Worker 	case A_RELEASE:
9902*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);
9903*8b26181fSAndroid Build Coastguard Worker 		break;
9904*8b26181fSAndroid Build Coastguard Worker 
9905*8b26181fSAndroid Build Coastguard Worker 	case A_RELEASE_DONE:
9906*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
9907*8b26181fSAndroid Build Coastguard Worker 		break;
9908*8b26181fSAndroid Build Coastguard Worker 
9909*8b26181fSAndroid Build Coastguard Worker 	default:
9910*8b26181fSAndroid Build Coastguard Worker 		abort();
9911*8b26181fSAndroid Build Coastguard Worker 	}
9912*8b26181fSAndroid Build Coastguard Worker 	return b1;
9913*8b26181fSAndroid Build Coastguard Worker }
9914*8b26181fSAndroid Build Coastguard Worker 
9915*8b26181fSAndroid Build Coastguard Worker struct block *
gen_atmmulti_abbrev(compiler_state_t * cstate,int type)9916*8b26181fSAndroid Build Coastguard Worker gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
9917*8b26181fSAndroid Build Coastguard Worker {
9918*8b26181fSAndroid Build Coastguard Worker 	struct block *b0, *b1;
9919*8b26181fSAndroid Build Coastguard Worker 
9920*8b26181fSAndroid Build Coastguard Worker 	/*
9921*8b26181fSAndroid Build Coastguard Worker 	 * Catch errors reported by us and routines below us, and return NULL
9922*8b26181fSAndroid Build Coastguard Worker 	 * on an error.
9923*8b26181fSAndroid Build Coastguard Worker 	 */
9924*8b26181fSAndroid Build Coastguard Worker 	if (setjmp(cstate->top_ctx))
9925*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
9926*8b26181fSAndroid Build Coastguard Worker 
9927*8b26181fSAndroid Build Coastguard Worker 	switch (type) {
9928*8b26181fSAndroid Build Coastguard Worker 
9929*8b26181fSAndroid Build Coastguard Worker 	case A_OAM:
9930*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9931*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'oam' supported only on raw ATM");
9932*8b26181fSAndroid Build Coastguard Worker 		/* OAM F4 type */
9933*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9934*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9935*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9936*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9937*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9938*8b26181fSAndroid Build Coastguard Worker 		break;
9939*8b26181fSAndroid Build Coastguard Worker 
9940*8b26181fSAndroid Build Coastguard Worker 	case A_OAMF4:
9941*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9942*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'oamf4' supported only on raw ATM");
9943*8b26181fSAndroid Build Coastguard Worker 		/* OAM F4 type */
9944*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9945*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9946*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9947*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9948*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9949*8b26181fSAndroid Build Coastguard Worker 		break;
9950*8b26181fSAndroid Build Coastguard Worker 
9951*8b26181fSAndroid Build Coastguard Worker 	case A_CONNECTMSG:
9952*8b26181fSAndroid Build Coastguard Worker 		/*
9953*8b26181fSAndroid Build Coastguard Worker 		 * Get Q.2931 signalling messages for switched
9954*8b26181fSAndroid Build Coastguard Worker 		 * virtual connection
9955*8b26181fSAndroid Build Coastguard Worker 		 */
9956*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9957*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'connectmsg' supported only on raw ATM");
9958*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_SETUP);
9959*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9960*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9961*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_CONNECT);
9962*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9963*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_CONNECTACK);
9964*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9965*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_RELEASE);
9966*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9967*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9968*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9969*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmtype_sc(cstate);
9970*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9971*8b26181fSAndroid Build Coastguard Worker 		break;
9972*8b26181fSAndroid Build Coastguard Worker 
9973*8b26181fSAndroid Build Coastguard Worker 	case A_METACONNECT:
9974*8b26181fSAndroid Build Coastguard Worker 		if (!cstate->is_atm)
9975*8b26181fSAndroid Build Coastguard Worker 			bpf_error(cstate, "'metaconnect' supported only on raw ATM");
9976*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_SETUP);
9977*8b26181fSAndroid Build Coastguard Worker 		b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9978*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9979*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_CONNECT);
9980*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9981*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_RELEASE);
9982*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9983*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9984*8b26181fSAndroid Build Coastguard Worker 		gen_or(b0, b1);
9985*8b26181fSAndroid Build Coastguard Worker 		b0 = gen_atmtype_metac(cstate);
9986*8b26181fSAndroid Build Coastguard Worker 		gen_and(b0, b1);
9987*8b26181fSAndroid Build Coastguard Worker 		break;
9988*8b26181fSAndroid Build Coastguard Worker 
9989*8b26181fSAndroid Build Coastguard Worker 	default:
9990*8b26181fSAndroid Build Coastguard Worker 		abort();
9991*8b26181fSAndroid Build Coastguard Worker 	}
9992*8b26181fSAndroid Build Coastguard Worker 	return b1;
9993*8b26181fSAndroid Build Coastguard Worker }
9994