1191005Sdelphij/*
2204977Simp * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
331899Ssef *	The Regents of the University of California.  All rights reserved.
431899Ssef *
531899Ssef * Redistribution and use in source and binary forms, with or without
631899Ssef * modification, are permitted provided that: (1) source code distributions
731899Ssef * retain the above copyright notice and this paragraph in its entirety, (2)
831899Ssef * distributions including binary code include the above copyright notice and
931899Ssef * this paragraph in its entirety in the documentation or other materials
1031899Ssef * provided with the distribution, and (3) all advertising materials mentioning
1131899Ssef * features or use of this software display the following acknowledgement:
1231899Ssef * ``This product includes software developed by the University of California,
1331899Ssef * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1431899Ssef * the University nor the names of its contributors may be used to endorse
1531899Ssef * or promote products derived from this software without specific prior
1631899Ssef * written permission.
1731899Ssef * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1831899Ssef * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1931899Ssef * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2031899Ssef *
2131899Ssef * @(#) $Header: /tcpdump/master/libpcap/gencode.h,v 1.71 2007-11-18 02:03:52 guy Exp $ (LBL)
2231899Ssef */
2331899Ssef
2431899Ssef/*
2531899Ssef * ATM support:
2631899Ssef *
2731899Ssef * Copyright (c) 1997 Yen Yen Lim and North Dakota State University
2831899Ssef * All rights reserved.
2931899Ssef *
3031899Ssef * Redistribution and use in source and binary forms, with or without
3131899Ssef * modification, are permitted provided that the following conditions
32119852Scharnier * are met:
33119852Scharnier * 1. Redistributions of source code must retain the above copyright
3432275Scharnier *    notice, this list of conditions and the following disclaimer.
3531899Ssef * 2. Redistributions in binary form must reproduce the above copyright
36222103Sbcr *    notice, this list of conditions and the following disclaimer in the
3731567Ssef *    documentation and/or other materials provided with the distribution.
3831567Ssef * 3. All advertising materials mentioning features or use of this software
3931567Ssef *    must display the following acknowledgement:
4031567Ssef *      This product includes software developed by Yen Yen Lim and
41290052Sjhb *      North Dakota State University
4285301Sdes * 4. The name of the author may not be used to endorse or promote products
4332275Scharnier *    derived from this software without specific prior written permission.
4432275Scharnier *
4531567Ssef * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4631567Ssef * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47101423Smdodd * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
4831579Speter * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
4931567Ssef * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50101282Smdodd * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
5187703Smarkm * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52192025Sdds * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
5331567Ssef * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
54144177Salfred * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5532275Scharnier * POSSIBILITY OF SUCH DAMAGE.
5632275Scharnier */
57144177Salfred
58324779Semaste#ifndef HAVE___ATTRIBUTE__
59324779Semaste#define __attribute__(x)
60144177Salfred#endif /* HAVE___ATTRIBUTE__ */
6131567Ssef
6231567Ssef/* Address qualifiers. */
63132306Salfred
64132306Salfred#define Q_HOST		1
65132306Salfred#define Q_NET		2
66290052Sjhb#define Q_PORT		3
67132306Salfred#define Q_GATEWAY	4
68132306Salfred#define Q_PROTO		5
69290052Sjhb#define Q_PROTOCHAIN	6
70290052Sjhb#define Q_PORTRANGE	7
71132306Salfred
72290052Sjhb/* Protocol qualifiers. */
73132306Salfred
74132306Salfred#define Q_LINK		1
7532275Scharnier#define Q_IP		2
76144177Salfred#define Q_ARP		3
77144177Salfred#define Q_RARP		4
78240393Szont#define Q_SCTP		5
79240005Szont#define Q_TCP		6
80144178Salfred#define Q_UDP		7
81240005Szont#define Q_ICMP		8
82290052Sjhb#define Q_IGMP		9
83290052Sjhb#define Q_IGRP		10
8431567Ssef
85144178Salfred
86144178Salfred#define	Q_ATALK		11
87144177Salfred#define	Q_DECNET	12
88192153Sdelphij#define	Q_LAT		13
89144177Salfred#define Q_SCA		14
90192153Sdelphij#define	Q_MOPRC		15
91192153Sdelphij#define	Q_MOPDL		16
92290052Sjhb
93144177Salfred
94153963Sbrian#define Q_IPV6		17
95168569Sdelphij#define Q_ICMPV6	18
96290052Sjhb#define Q_AH		19
97290052Sjhb#define Q_ESP		20
98298427Sjhb
99144177Salfred#define Q_PIM		21
100144177Salfred#define Q_VRRP		22
101290052Sjhb
102171055Sdelphij#define Q_AARP		23
103290052Sjhb
104290052Sjhb#define Q_ISO		24
105171055Sdelphij#define Q_ESIS		25
106144177Salfred#define Q_ISIS		26
107144177Salfred#define Q_CLNP		27
108144177Salfred
109144177Salfred#define Q_STP		28
110144177Salfred
111144177Salfred#define Q_IPX		29
112144177Salfred
113192025Sdds#define Q_NETBEUI	30
114290052Sjhb
115192025Sdds/* IS-IS Levels */
116144177Salfred#define Q_ISIS_L1       31
117144177Salfred#define Q_ISIS_L2       32
118144177Salfred/* PDU types */
119144177Salfred#define Q_ISIS_IIH      33
120144177Salfred#define Q_ISIS_LAN_IIH  34
121144177Salfred#define Q_ISIS_PTP_IIH  35
122144177Salfred#define Q_ISIS_SNP      36
123144177Salfred#define Q_ISIS_CSNP     37
124144177Salfred#define Q_ISIS_PSNP     38
125144177Salfred#define Q_ISIS_LSP      39
126144177Salfred
127144177Salfred#define Q_RADIO		40
128153963Sbrian
129153963Sbrian#define Q_CARP		41
130153963Sbrian
131240005Szont/* Directional qualifiers. */
132144177Salfred
133144177Salfred#define Q_SRC		1
134298427Sjhb#define Q_DST		2
135298427Sjhb#define Q_OR		3
136298427Sjhb#define Q_AND		4
137144177Salfred#define Q_ADDR1		5
138144177Salfred#define Q_ADDR2		6
139144177Salfred#define Q_ADDR3		7
140144177Salfred#define Q_ADDR4		8
14131567Ssef#define Q_RA		9
142144177Salfred#define Q_TA		10
143290052Sjhb
144290052Sjhb#define Q_DEFAULT	0
145144177Salfred#define Q_UNDEF		255
14631567Ssef
147144177Salfred/* ATM types */
148215235Sjh#define A_METAC		22	/* Meta signalling Circuit */
149245957Smjg#define A_BCC		23	/* Broadcast Circuit */
150245957Smjg#define A_OAMF4SC	24	/* Segment OAM F4 Circuit */
151215235Sjh#define A_OAMF4EC	25	/* End-to-End OAM F4 Circuit */
152245957Smjg#define A_SC		26	/* Signalling Circuit*/
153245957Smjg#define A_ILMIC		27	/* ILMI Circuit */
154144177Salfred#define A_OAM		28	/* OAM cells : F4 only */
15532275Scharnier#define A_OAMF4		29	/* OAM F4 cells: Segment + End-to-end */
156144177Salfred#define A_LANE		30	/* LANE traffic */
157144177Salfred#define A_LLC		31	/* LLC-encapsulated traffic */
158144177Salfred
159144177Salfred/* Based on Q.2931 signalling protocol */
160144177Salfred#define A_SETUP		41	/* Setup message */
161144177Salfred#define A_CALLPROCEED	42	/* Call proceeding message */
162290052Sjhb#define A_CONNECT	43	/* Connect message */
163290052Sjhb#define A_CONNECTACK	44	/* Connect Ack message */
164144177Salfred#define A_RELEASE	45	/* Release message */
165290052Sjhb#define A_RELEASE_DONE	46	/* Release message */
166144177Salfred
167144177Salfred/* ATM field types */
168144177Salfred#define A_VPI		51
169144177Salfred#define A_VCI		52
170240393Szont#define A_PROTOTYPE	53
171240393Szont#define A_MSGTYPE	54
172240393Szont#define A_CALLREFTYPE	55
173240393Szont
174240393Szont#define A_CONNECTMSG	70	/* returns Q.2931 signalling messages for
175240393Szont				   establishing and destroying switched
176290052Sjhb				   virtual connection */
177144177Salfred#define A_METACONNECT	71	/* returns Q.2931 signalling messages for
17831567Ssef				   establishing and destroying predefined
179144177Salfred				   virtual circuits, such as broadcast
180144177Salfred				   circuit, oamf4 segment circuit, oamf4
181144177Salfred				   end-to-end circuits, ILMI circuits or
182144177Salfred				   connection signalling circuit. */
183290052Sjhb
184290052Sjhb/* MTP2 types */
185290052Sjhb#define M_FISU		22	/* FISU */
186290052Sjhb#define M_LSSU		23	/* LSSU */
187290052Sjhb#define M_MSU		24	/* MSU */
188290052Sjhb
189290052Sjhb/* MTP3 field types */
190290052Sjhb#define M_SIO		1
191290052Sjhb#define M_OPC		2
192290052Sjhb#define M_DPC		3
193290052Sjhb#define M_SLS		4
194290052Sjhb
195290052Sjhb
196290052Sjhbstruct slist;
197290052Sjhb
19831567Ssefstruct stmt {
199144177Salfred	int code;
200144177Salfred	struct slist *jt;	/*only for relative jump in block*/
201144177Salfred	struct slist *jf;	/*only for relative jump in block*/
202144177Salfred	bpf_int32 k;
203144177Salfred};
204144177Salfred
205101285Smdoddstruct slist {
206290052Sjhb	struct stmt s;
20731567Ssef	struct slist *next;
208240005Szont};
209240005Szont
210192025Sdds/*
211192025Sdds * A bit vector to represent definition sets.  We assume TOT_REGISTERS
212192025Sdds * is smaller than 8*sizeof(atomset).
213144177Salfred */
21431567Sseftypedef bpf_u_int32 atomset;
215#define ATOMMASK(n) (1 << (n))
216#define ATOMELEM(d, n) (d & ATOMMASK(n))
217
218/*
219 * An unbounded set.
220 */
221typedef bpf_u_int32 *uset;
222
223/*
224 * Total number of atomic entities, including accumulator (A) and index (X).
225 * We treat all these guys similarly during flow analysis.
226 */
227#define N_ATOMS (BPF_MEMWORDS+2)
228
229struct edge {
230	int id;
231	int code;
232	uset edom;
233	struct block *succ;
234	struct block *pred;
235	struct edge *next;	/* link list of incoming edges for a node */
236};
237
238struct block {
239	int id;
240	struct slist *stmts;	/* side effect stmts */
241	struct stmt s;		/* branch stmt */
242	int mark;
243	u_int longjt;		/* jt branch requires long jump */
244	u_int longjf;		/* jf branch requires long jump */
245	int level;
246	int offset;
247	int sense;
248	struct edge et;
249	struct edge ef;
250	struct block *head;
251	struct block *link;	/* link field used by optimizer */
252	uset dom;
253	uset closure;
254	struct edge *in_edges;
255	atomset def, kill;
256	atomset in_use;
257	atomset out_use;
258	int oval;
259	int val[N_ATOMS];
260};
261
262struct arth {
263	struct block *b;	/* protocol checks */
264	struct slist *s;	/* stmt list */
265	int regno;		/* virtual register number of result */
266};
267
268struct qual {
269	unsigned char addr;
270	unsigned char proto;
271	unsigned char dir;
272	unsigned char pad;
273};
274
275struct arth *gen_loadi(int);
276struct arth *gen_load(int, struct arth *, int);
277struct arth *gen_loadlen(void);
278struct arth *gen_neg(struct arth *);
279struct arth *gen_arth(int, struct arth *, struct arth *);
280
281void gen_and(struct block *, struct block *);
282void gen_or(struct block *, struct block *);
283void gen_not(struct block *);
284
285struct block *gen_scode(const char *, struct qual);
286struct block *gen_ecode(const u_char *, struct qual);
287struct block *gen_acode(const u_char *, struct qual);
288struct block *gen_mcode(const char *, const char *, int, struct qual);
289#ifdef INET6
290struct block *gen_mcode6(const char *, const char *, int, struct qual);
291#endif
292struct block *gen_ncode(const char *, bpf_u_int32, struct qual);
293struct block *gen_proto_abbrev(int);
294struct block *gen_relation(int, struct arth *, struct arth *, int);
295struct block *gen_less(int);
296struct block *gen_greater(int);
297struct block *gen_byteop(int, int, int);
298struct block *gen_broadcast(int);
299struct block *gen_multicast(int);
300struct block *gen_inbound(int);
301
302struct block *gen_vlan(int);
303struct block *gen_mpls(int);
304
305struct block *gen_pppoed(void);
306struct block *gen_pppoes(void);
307
308struct block *gen_atmfield_code(int atmfield, bpf_int32 jvalue, bpf_u_int32 jtype, int reverse);
309struct block *gen_atmtype_abbrev(int type);
310struct block *gen_atmmulti_abbrev(int type);
311
312struct block *gen_mtp2type_abbrev(int type);
313struct block *gen_mtp3field_code(int mtp3field, bpf_u_int32 jvalue, bpf_u_int32 jtype, int reverse);
314
315struct block *gen_pf_ifname(const char *);
316struct block *gen_pf_rnr(int);
317struct block *gen_pf_srnr(int);
318struct block *gen_pf_ruleset(char *);
319struct block *gen_pf_reason(int);
320struct block *gen_pf_action(int);
321struct block *gen_pf_dir(int);
322
323struct block *gen_p80211_type(int, int);
324struct block *gen_p80211_fcdir(int);
325
326void bpf_optimize(struct block **);
327void bpf_error(const char *, ...)
328    __attribute__((noreturn, format (printf, 1, 2)));
329
330void finish_parse(struct block *);
331char *sdup(const char *);
332
333struct bpf_insn *icode_to_fcode(struct block *, u_int *);
334int pcap_parse(void);
335void lex_init(const char *);
336void lex_cleanup(void);
337void sappend(struct slist *, struct slist *);
338
339/* XXX */
340#define JT(b)  ((b)->et.succ)
341#define JF(b)  ((b)->ef.succ)
342
343extern int no_optimize;
344