117683Spst/*
217683Spst * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that: (1) source code distributions
717683Spst * retain the above copyright notice and this paragraph in its entirety, (2)
817683Spst * distributions including binary code include the above copyright notice and
917683Spst * this paragraph in its entirety in the documentation or other materials
1017683Spst * provided with the distribution, and (3) all advertising materials mentioning
1117683Spst * features or use of this software display the following acknowledgement:
1217683Spst * ``This product includes software developed by the University of California,
1317683Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1417683Spst * the University nor the names of its contributors may be used to endorse
1517683Spst * or promote products derived from this software without specific prior
1617683Spst * written permission.
1717683Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1817683Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1917683Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2017683Spst *
21214518Srpaulo * @(#) $Header: /tcpdump/master/libpcap/gencode.h,v 1.71 2007-11-18 02:03:52 guy Exp $ (LBL)
2217683Spst */
2317683Spst
24127667Sbms/*
25127667Sbms * ATM support:
26127667Sbms *
27127667Sbms * Copyright (c) 1997 Yen Yen Lim and North Dakota State University
28127667Sbms * All rights reserved.
29127667Sbms *
30127667Sbms * Redistribution and use in source and binary forms, with or without
31127667Sbms * modification, are permitted provided that the following conditions
32127667Sbms * are met:
33127667Sbms * 1. Redistributions of source code must retain the above copyright
34127667Sbms *    notice, this list of conditions and the following disclaimer.
35127667Sbms * 2. Redistributions in binary form must reproduce the above copyright
36127667Sbms *    notice, this list of conditions and the following disclaimer in the
37127667Sbms *    documentation and/or other materials provided with the distribution.
38127667Sbms * 3. All advertising materials mentioning features or use of this software
39127667Sbms *    must display the following acknowledgement:
40127667Sbms *      This product includes software developed by Yen Yen Lim and
41127667Sbms *      North Dakota State University
42127667Sbms * 4. The name of the author may not be used to endorse or promote products
43127667Sbms *    derived from this software without specific prior written permission.
44127667Sbms *
45127667Sbms * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46127667Sbms * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47127667Sbms * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48127667Sbms * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
49127667Sbms * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50127667Sbms * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51127667Sbms * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52127667Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53127667Sbms * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
54127667Sbms * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55127667Sbms * POSSIBILITY OF SUCH DAMAGE.
56127667Sbms */
57127667Sbms
58147897Ssam#ifndef HAVE___ATTRIBUTE__
59147897Ssam#define __attribute__(x)
60147897Ssam#endif /* HAVE___ATTRIBUTE__ */
61147897Ssam
6217683Spst/* Address qualifiers. */
6317683Spst
6417683Spst#define Q_HOST		1
6517683Spst#define Q_NET		2
6617683Spst#define Q_PORT		3
6717683Spst#define Q_GATEWAY	4
6817683Spst#define Q_PROTO		5
6956891Sfenner#define Q_PROTOCHAIN	6
70147897Ssam#define Q_PORTRANGE	7
7117683Spst
7217683Spst/* Protocol qualifiers. */
7317683Spst
7417683Spst#define Q_LINK		1
7517683Spst#define Q_IP		2
7617683Spst#define Q_ARP		3
7717683Spst#define Q_RARP		4
7898533Sfenner#define Q_SCTP		5
7998533Sfenner#define Q_TCP		6
8098533Sfenner#define Q_UDP		7
8198533Sfenner#define Q_ICMP		8
8298533Sfenner#define Q_IGMP		9
8398533Sfenner#define Q_IGRP		10
8417683Spst
8517683Spst
8698533Sfenner#define	Q_ATALK		11
8798533Sfenner#define	Q_DECNET	12
8898533Sfenner#define	Q_LAT		13
8998533Sfenner#define Q_SCA		14
9098533Sfenner#define	Q_MOPRC		15
9198533Sfenner#define	Q_MOPDL		16
9217683Spst
9375110Sfenner
9498533Sfenner#define Q_IPV6		17
9598533Sfenner#define Q_ICMPV6	18
9698533Sfenner#define Q_AH		19
9798533Sfenner#define Q_ESP		20
9856891Sfenner
9998533Sfenner#define Q_PIM		21
10098533Sfenner#define Q_VRRP		22
10156891Sfenner
10298533Sfenner#define Q_AARP		23
10356891Sfenner
10498533Sfenner#define Q_ISO		24
10598533Sfenner#define Q_ESIS		25
10698533Sfenner#define Q_ISIS		26
10798533Sfenner#define Q_CLNP		27
10875110Sfenner
10998533Sfenner#define Q_STP		28
11098533Sfenner
11198533Sfenner#define Q_IPX		29
11298533Sfenner
11398533Sfenner#define Q_NETBEUI	30
11498533Sfenner
115127667Sbms/* IS-IS Levels */
116127667Sbms#define Q_ISIS_L1       31
117127667Sbms#define Q_ISIS_L2       32
118127667Sbms/* PDU types */
119127667Sbms#define Q_ISIS_IIH      33
120127667Sbms#define Q_ISIS_LAN_IIH  34
121127667Sbms#define Q_ISIS_PTP_IIH  35
122127667Sbms#define Q_ISIS_SNP      36
123127667Sbms#define Q_ISIS_CSNP     37
124127667Sbms#define Q_ISIS_PSNP     38
125127667Sbms#define Q_ISIS_LSP      39
126127667Sbms
127147897Ssam#define Q_RADIO		40
128147897Ssam
129235426Sdelphij#define Q_CARP		41
130235426Sdelphij
13117683Spst/* Directional qualifiers. */
13217683Spst
13317683Spst#define Q_SRC		1
13417683Spst#define Q_DST		2
13517683Spst#define Q_OR		3
13617683Spst#define Q_AND		4
137190225Srpaulo#define Q_ADDR1		5
138190225Srpaulo#define Q_ADDR2		6
139190225Srpaulo#define Q_ADDR3		7
140190225Srpaulo#define Q_ADDR4		8
141235426Sdelphij#define Q_RA		9
142235426Sdelphij#define Q_TA		10
14317683Spst
14417683Spst#define Q_DEFAULT	0
14517683Spst#define Q_UNDEF		255
14617683Spst
147127667Sbms/* ATM types */
148127667Sbms#define A_METAC		22	/* Meta signalling Circuit */
149127667Sbms#define A_BCC		23	/* Broadcast Circuit */
150127667Sbms#define A_OAMF4SC	24	/* Segment OAM F4 Circuit */
151127667Sbms#define A_OAMF4EC	25	/* End-to-End OAM F4 Circuit */
152127667Sbms#define A_SC		26	/* Signalling Circuit*/
153127667Sbms#define A_ILMIC		27	/* ILMI Circuit */
154127667Sbms#define A_OAM		28	/* OAM cells : F4 only */
155127667Sbms#define A_OAMF4		29	/* OAM F4 cells: Segment + End-to-end */
156127667Sbms#define A_LANE		30	/* LANE traffic */
157127667Sbms#define A_LLC		31	/* LLC-encapsulated traffic */
158127667Sbms
159127667Sbms/* Based on Q.2931 signalling protocol */
160127667Sbms#define A_SETUP		41	/* Setup message */
161127667Sbms#define A_CALLPROCEED	42	/* Call proceeding message */
162127667Sbms#define A_CONNECT	43	/* Connect message */
163127667Sbms#define A_CONNECTACK	44	/* Connect Ack message */
164127667Sbms#define A_RELEASE	45	/* Release message */
165127667Sbms#define A_RELEASE_DONE	46	/* Release message */
166127667Sbms
167127667Sbms/* ATM field types */
168127667Sbms#define A_VPI		51
169127667Sbms#define A_VCI		52
170127667Sbms#define A_PROTOTYPE	53
171127667Sbms#define A_MSGTYPE	54
172127667Sbms#define A_CALLREFTYPE	55
173127667Sbms
174127667Sbms#define A_CONNECTMSG	70	/* returns Q.2931 signalling messages for
175127667Sbms				   establishing and destroying switched
176127667Sbms				   virtual connection */
177127667Sbms#define A_METACONNECT	71	/* returns Q.2931 signalling messages for
178127667Sbms				   establishing and destroying predefined
179127667Sbms				   virtual circuits, such as broadcast
180127667Sbms				   circuit, oamf4 segment circuit, oamf4
181127667Sbms				   end-to-end circuits, ILMI circuits or
182127667Sbms				   connection signalling circuit. */
183127667Sbms
184172680Smlaier/* MTP2 types */
185172680Smlaier#define M_FISU		22	/* FISU */
186172680Smlaier#define M_LSSU		23	/* LSSU */
187172680Smlaier#define M_MSU		24	/* MSU */
188147897Ssam
189172680Smlaier/* MTP3 field types */
190172680Smlaier#define M_SIO		1
191172680Smlaier#define M_OPC		2
192172680Smlaier#define M_DPC		3
193172680Smlaier#define M_SLS		4
194147897Ssam
195172680Smlaier
19656891Sfennerstruct slist;
19756891Sfenner
19817683Spststruct stmt {
19917683Spst	int code;
20056891Sfenner	struct slist *jt;	/*only for relative jump in block*/
20156891Sfenner	struct slist *jf;	/*only for relative jump in block*/
20217683Spst	bpf_int32 k;
20317683Spst};
20417683Spst
20517683Spststruct slist {
20617683Spst	struct stmt s;
20717683Spst	struct slist *next;
20817683Spst};
20917683Spst
210127667Sbms/*
21117683Spst * A bit vector to represent definition sets.  We assume TOT_REGISTERS
21217683Spst * is smaller than 8*sizeof(atomset).
21317683Spst */
21417683Spsttypedef bpf_u_int32 atomset;
21517683Spst#define ATOMMASK(n) (1 << (n))
21617683Spst#define ATOMELEM(d, n) (d & ATOMMASK(n))
21717683Spst
21817683Spst/*
21917683Spst * An unbounded set.
22017683Spst */
22117683Spsttypedef bpf_u_int32 *uset;
22217683Spst
22317683Spst/*
22417683Spst * Total number of atomic entities, including accumulator (A) and index (X).
22517683Spst * We treat all these guys similarly during flow analysis.
22617683Spst */
22717683Spst#define N_ATOMS (BPF_MEMWORDS+2)
22817683Spst
22917683Spststruct edge {
23017683Spst	int id;
23117683Spst	int code;
23217683Spst	uset edom;
23317683Spst	struct block *succ;
23417683Spst	struct block *pred;
23517683Spst	struct edge *next;	/* link list of incoming edges for a node */
23617683Spst};
23717683Spst
23817683Spststruct block {
23917683Spst	int id;
24017683Spst	struct slist *stmts;	/* side effect stmts */
24117683Spst	struct stmt s;		/* branch stmt */
24217683Spst	int mark;
243241231Sdelphij	u_int longjt;		/* jt branch requires long jump */
244241231Sdelphij	u_int longjf;		/* jf branch requires long jump */
24517683Spst	int level;
24617683Spst	int offset;
24717683Spst	int sense;
24817683Spst	struct edge et;
24917683Spst	struct edge ef;
25017683Spst	struct block *head;
25117683Spst	struct block *link;	/* link field used by optimizer */
25217683Spst	uset dom;
25317683Spst	uset closure;
25417683Spst	struct edge *in_edges;
25517683Spst	atomset def, kill;
25617683Spst	atomset in_use;
25717683Spst	atomset out_use;
25817683Spst	int oval;
25917683Spst	int val[N_ATOMS];
26017683Spst};
26117683Spst
26217683Spststruct arth {
26317683Spst	struct block *b;	/* protocol checks */
26417683Spst	struct slist *s;	/* stmt list */
26517683Spst	int regno;		/* virtual register number of result */
26617683Spst};
26717683Spst
26817683Spststruct qual {
26917683Spst	unsigned char addr;
27017683Spst	unsigned char proto;
27117683Spst	unsigned char dir;
27217683Spst	unsigned char pad;
27317683Spst};
27417683Spst
27517683Spststruct arth *gen_loadi(int);
27617683Spststruct arth *gen_load(int, struct arth *, int);
27717683Spststruct arth *gen_loadlen(void);
27817683Spststruct arth *gen_neg(struct arth *);
27917683Spststruct arth *gen_arth(int, struct arth *, struct arth *);
28017683Spst
28117683Spstvoid gen_and(struct block *, struct block *);
28217683Spstvoid gen_or(struct block *, struct block *);
28317683Spstvoid gen_not(struct block *);
28417683Spst
28517683Spststruct block *gen_scode(const char *, struct qual);
28617683Spststruct block *gen_ecode(const u_char *, struct qual);
28798533Sfennerstruct block *gen_acode(const u_char *, struct qual);
28817683Spststruct block *gen_mcode(const char *, const char *, int, struct qual);
28956891Sfenner#ifdef INET6
29056891Sfennerstruct block *gen_mcode6(const char *, const char *, int, struct qual);
29156891Sfenner#endif
29217683Spststruct block *gen_ncode(const char *, bpf_u_int32, struct qual);
29317683Spststruct block *gen_proto_abbrev(int);
29417683Spststruct block *gen_relation(int, struct arth *, struct arth *, int);
29517683Spststruct block *gen_less(int);
29617683Spststruct block *gen_greater(int);
29717683Spststruct block *gen_byteop(int, int, int);
29817683Spststruct block *gen_broadcast(int);
29917683Spststruct block *gen_multicast(int);
30017683Spststruct block *gen_inbound(int);
30117683Spst
30275110Sfennerstruct block *gen_vlan(int);
303146771Ssamstruct block *gen_mpls(int);
30475110Sfenner
305162015Ssamstruct block *gen_pppoed(void);
306162015Ssamstruct block *gen_pppoes(void);
307162015Ssam
308147897Ssamstruct block *gen_atmfield_code(int atmfield, bpf_int32 jvalue, bpf_u_int32 jtype, int reverse);
309127667Sbmsstruct block *gen_atmtype_abbrev(int type);
310127667Sbmsstruct block *gen_atmmulti_abbrev(int type);
311127667Sbms
312172680Smlaierstruct block *gen_mtp2type_abbrev(int type);
313147897Ssamstruct block *gen_mtp3field_code(int mtp3field, bpf_u_int32 jvalue, bpf_u_int32 jtype, int reverse);
314147897Ssam
315127667Sbmsstruct block *gen_pf_ifname(const char *);
316127667Sbmsstruct block *gen_pf_rnr(int);
317127667Sbmsstruct block *gen_pf_srnr(int);
318127667Sbmsstruct block *gen_pf_ruleset(char *);
319127667Sbmsstruct block *gen_pf_reason(int);
320127667Sbmsstruct block *gen_pf_action(int);
321127667Sbmsstruct block *gen_pf_dir(int);
322127667Sbms
323190225Srpaulostruct block *gen_p80211_type(int, int);
324190225Srpaulostruct block *gen_p80211_fcdir(int);
325190225Srpaulo
32617683Spstvoid bpf_optimize(struct block **);
32775110Sfennervoid bpf_error(const char *, ...)
328147897Ssam    __attribute__((noreturn, format (printf, 1, 2)));
32917683Spst
33017683Spstvoid finish_parse(struct block *);
33117683Spstchar *sdup(const char *);
33217683Spst
333241231Sdelphijstruct bpf_insn *icode_to_fcode(struct block *, u_int *);
33417683Spstint pcap_parse(void);
335172680Smlaiervoid lex_init(const char *);
33675110Sfennervoid lex_cleanup(void);
33717683Spstvoid sappend(struct slist *, struct slist *);
33817683Spst
33917683Spst/* XXX */
34017683Spst#define JT(b)  ((b)->et.succ)
34117683Spst#define JF(b)  ((b)->ef.succ)
34256891Sfenner
34356891Sfennerextern int no_optimize;
344