xref: /src/contrib/libpcap/gencode.h (revision ee2dd488bf795a49670e92d0c5a838cc80522088)
18cf6c252SPaul Traina /*
28cf6c252SPaul Traina  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
38cf6c252SPaul Traina  *	The Regents of the University of California.  All rights reserved.
48cf6c252SPaul Traina  *
58cf6c252SPaul Traina  * Redistribution and use in source and binary forms, with or without
68cf6c252SPaul Traina  * modification, are permitted provided that: (1) source code distributions
78cf6c252SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
88cf6c252SPaul Traina  * distributions including binary code include the above copyright notice and
98cf6c252SPaul Traina  * this paragraph in its entirety in the documentation or other materials
108cf6c252SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
118cf6c252SPaul Traina  * features or use of this software display the following acknowledgement:
128cf6c252SPaul Traina  * ``This product includes software developed by the University of California,
138cf6c252SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
148cf6c252SPaul Traina  * the University nor the names of its contributors may be used to endorse
158cf6c252SPaul Traina  * or promote products derived from this software without specific prior
168cf6c252SPaul Traina  * written permission.
178cf6c252SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
188cf6c252SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
198cf6c252SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
208cf6c252SPaul Traina  *
21ee2dd488SSam Leffler  * @(#) $Header: /tcpdump/master/libpcap/gencode.h,v 1.60.2.5 2005/06/20 21:30:17 guy Exp $ (LBL)
22feb4ecdbSBruce M Simpson  */
23feb4ecdbSBruce M Simpson 
24feb4ecdbSBruce M Simpson /*
25feb4ecdbSBruce M Simpson  * ATM support:
26feb4ecdbSBruce M Simpson  *
27feb4ecdbSBruce M Simpson  * Copyright (c) 1997 Yen Yen Lim and North Dakota State University
28feb4ecdbSBruce M Simpson  * All rights reserved.
29feb4ecdbSBruce M Simpson  *
30feb4ecdbSBruce M Simpson  * Redistribution and use in source and binary forms, with or without
31feb4ecdbSBruce M Simpson  * modification, are permitted provided that the following conditions
32feb4ecdbSBruce M Simpson  * are met:
33feb4ecdbSBruce M Simpson  * 1. Redistributions of source code must retain the above copyright
34feb4ecdbSBruce M Simpson  *    notice, this list of conditions and the following disclaimer.
35feb4ecdbSBruce M Simpson  * 2. Redistributions in binary form must reproduce the above copyright
36feb4ecdbSBruce M Simpson  *    notice, this list of conditions and the following disclaimer in the
37feb4ecdbSBruce M Simpson  *    documentation and/or other materials provided with the distribution.
38feb4ecdbSBruce M Simpson  * 3. All advertising materials mentioning features or use of this software
39feb4ecdbSBruce M Simpson  *    must display the following acknowledgement:
40feb4ecdbSBruce M Simpson  *      This product includes software developed by Yen Yen Lim and
41feb4ecdbSBruce M Simpson  *      North Dakota State University
42feb4ecdbSBruce M Simpson  * 4. The name of the author may not be used to endorse or promote products
43feb4ecdbSBruce M Simpson  *    derived from this software without specific prior written permission.
44feb4ecdbSBruce M Simpson  *
45feb4ecdbSBruce M Simpson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46feb4ecdbSBruce M Simpson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47feb4ecdbSBruce M Simpson  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48feb4ecdbSBruce M Simpson  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
49feb4ecdbSBruce M Simpson  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50feb4ecdbSBruce M Simpson  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51feb4ecdbSBruce M Simpson  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52feb4ecdbSBruce M Simpson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53feb4ecdbSBruce M Simpson  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
54feb4ecdbSBruce M Simpson  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55feb4ecdbSBruce M Simpson  * POSSIBILITY OF SUCH DAMAGE.
568cf6c252SPaul Traina  */
578cf6c252SPaul Traina 
58ee2dd488SSam Leffler #ifndef HAVE___ATTRIBUTE__
59ee2dd488SSam Leffler #define __attribute__(x)
60ee2dd488SSam Leffler #endif /* HAVE___ATTRIBUTE__ */
61ee2dd488SSam Leffler 
628cf6c252SPaul Traina /* Address qualifiers. */
638cf6c252SPaul Traina 
648cf6c252SPaul Traina #define Q_HOST		1
658cf6c252SPaul Traina #define Q_NET		2
668cf6c252SPaul Traina #define Q_PORT		3
678cf6c252SPaul Traina #define Q_GATEWAY	4
688cf6c252SPaul Traina #define Q_PROTO		5
698751327cSBill Fenner #define Q_PROTOCHAIN	6
70ee2dd488SSam Leffler #define Q_PORTRANGE	7
718cf6c252SPaul Traina 
728cf6c252SPaul Traina /* Protocol qualifiers. */
738cf6c252SPaul Traina 
748cf6c252SPaul Traina #define Q_LINK		1
758cf6c252SPaul Traina #define Q_IP		2
768cf6c252SPaul Traina #define Q_ARP		3
778cf6c252SPaul Traina #define Q_RARP		4
780a94d38fSBill Fenner #define Q_SCTP		5
790a94d38fSBill Fenner #define Q_TCP		6
800a94d38fSBill Fenner #define Q_UDP		7
810a94d38fSBill Fenner #define Q_ICMP		8
820a94d38fSBill Fenner #define Q_IGMP		9
830a94d38fSBill Fenner #define Q_IGRP		10
848cf6c252SPaul Traina 
858cf6c252SPaul Traina 
860a94d38fSBill Fenner #define	Q_ATALK		11
870a94d38fSBill Fenner #define	Q_DECNET	12
880a94d38fSBill Fenner #define	Q_LAT		13
890a94d38fSBill Fenner #define Q_SCA		14
900a94d38fSBill Fenner #define	Q_MOPRC		15
910a94d38fSBill Fenner #define	Q_MOPDL		16
928cf6c252SPaul Traina 
938751327cSBill Fenner 
940a94d38fSBill Fenner #define Q_IPV6		17
950a94d38fSBill Fenner #define Q_ICMPV6	18
960a94d38fSBill Fenner #define Q_AH		19
970a94d38fSBill Fenner #define Q_ESP		20
988751327cSBill Fenner 
990a94d38fSBill Fenner #define Q_PIM		21
1000a94d38fSBill Fenner #define Q_VRRP		22
1018751327cSBill Fenner 
1020a94d38fSBill Fenner #define Q_AARP		23
103dc2c7305SBill Fenner 
1040a94d38fSBill Fenner #define Q_ISO		24
1050a94d38fSBill Fenner #define Q_ESIS		25
1060a94d38fSBill Fenner #define Q_ISIS		26
1070a94d38fSBill Fenner #define Q_CLNP		27
1080a94d38fSBill Fenner 
1090a94d38fSBill Fenner #define Q_STP		28
1100a94d38fSBill Fenner 
1110a94d38fSBill Fenner #define Q_IPX		29
1120a94d38fSBill Fenner 
1130a94d38fSBill Fenner #define Q_NETBEUI	30
114dc2c7305SBill Fenner 
115feb4ecdbSBruce M Simpson /* IS-IS Levels */
116feb4ecdbSBruce M Simpson #define Q_ISIS_L1       31
117feb4ecdbSBruce M Simpson #define Q_ISIS_L2       32
118feb4ecdbSBruce M Simpson /* PDU types */
119feb4ecdbSBruce M Simpson #define Q_ISIS_IIH      33
120feb4ecdbSBruce M Simpson #define Q_ISIS_LAN_IIH  34
121feb4ecdbSBruce M Simpson #define Q_ISIS_PTP_IIH  35
122feb4ecdbSBruce M Simpson #define Q_ISIS_SNP      36
123feb4ecdbSBruce M Simpson #define Q_ISIS_CSNP     37
124feb4ecdbSBruce M Simpson #define Q_ISIS_PSNP     38
125feb4ecdbSBruce M Simpson #define Q_ISIS_LSP      39
126feb4ecdbSBruce M Simpson 
127ee2dd488SSam Leffler #define Q_RADIO		40
128ee2dd488SSam Leffler 
1298cf6c252SPaul Traina /* Directional qualifiers. */
1308cf6c252SPaul Traina 
1318cf6c252SPaul Traina #define Q_SRC		1
1328cf6c252SPaul Traina #define Q_DST		2
1338cf6c252SPaul Traina #define Q_OR		3
1348cf6c252SPaul Traina #define Q_AND		4
1358cf6c252SPaul Traina 
1368cf6c252SPaul Traina #define Q_DEFAULT	0
1378cf6c252SPaul Traina #define Q_UNDEF		255
1388cf6c252SPaul Traina 
139feb4ecdbSBruce M Simpson /* ATM types */
140feb4ecdbSBruce M Simpson #define A_METAC		22	/* Meta signalling Circuit */
141feb4ecdbSBruce M Simpson #define A_BCC		23	/* Broadcast Circuit */
142feb4ecdbSBruce M Simpson #define A_OAMF4SC	24	/* Segment OAM F4 Circuit */
143feb4ecdbSBruce M Simpson #define A_OAMF4EC	25	/* End-to-End OAM F4 Circuit */
144feb4ecdbSBruce M Simpson #define A_SC		26	/* Signalling Circuit*/
145feb4ecdbSBruce M Simpson #define A_ILMIC		27	/* ILMI Circuit */
146feb4ecdbSBruce M Simpson #define A_OAM		28	/* OAM cells : F4 only */
147feb4ecdbSBruce M Simpson #define A_OAMF4		29	/* OAM F4 cells: Segment + End-to-end */
148feb4ecdbSBruce M Simpson #define A_LANE		30	/* LANE traffic */
149feb4ecdbSBruce M Simpson #define A_LLC		31	/* LLC-encapsulated traffic */
150feb4ecdbSBruce M Simpson 
151feb4ecdbSBruce M Simpson /* Based on Q.2931 signalling protocol */
152feb4ecdbSBruce M Simpson #define A_SETUP		41	/* Setup message */
153feb4ecdbSBruce M Simpson #define A_CALLPROCEED	42	/* Call proceeding message */
154feb4ecdbSBruce M Simpson #define A_CONNECT	43	/* Connect message */
155feb4ecdbSBruce M Simpson #define A_CONNECTACK	44	/* Connect Ack message */
156feb4ecdbSBruce M Simpson #define A_RELEASE	45	/* Release message */
157feb4ecdbSBruce M Simpson #define A_RELEASE_DONE	46	/* Release message */
158feb4ecdbSBruce M Simpson 
159feb4ecdbSBruce M Simpson /* ATM field types */
160feb4ecdbSBruce M Simpson #define A_VPI		51
161feb4ecdbSBruce M Simpson #define A_VCI		52
162feb4ecdbSBruce M Simpson #define A_PROTOTYPE	53
163feb4ecdbSBruce M Simpson #define A_MSGTYPE	54
164feb4ecdbSBruce M Simpson #define A_CALLREFTYPE	55
165feb4ecdbSBruce M Simpson 
166feb4ecdbSBruce M Simpson #define A_CONNECTMSG	70	/* returns Q.2931 signalling messages for
167feb4ecdbSBruce M Simpson 				   establishing and destroying switched
168feb4ecdbSBruce M Simpson 				   virtual connection */
169feb4ecdbSBruce M Simpson #define A_METACONNECT	71	/* returns Q.2931 signalling messages for
170feb4ecdbSBruce M Simpson 				   establishing and destroying predefined
171feb4ecdbSBruce M Simpson 				   virtual circuits, such as broadcast
172feb4ecdbSBruce M Simpson 				   circuit, oamf4 segment circuit, oamf4
173feb4ecdbSBruce M Simpson 				   end-to-end circuits, ILMI circuits or
174feb4ecdbSBruce M Simpson 				   connection signalling circuit. */
175feb4ecdbSBruce M Simpson 
176ee2dd488SSam Leffler /*MTP3 field types */
177ee2dd488SSam Leffler #define M_SIO 1
178ee2dd488SSam Leffler #define M_OPC 2
179ee2dd488SSam Leffler #define M_DPC 3
180ee2dd488SSam Leffler #define M_SLS 4
181ee2dd488SSam Leffler 
182ee2dd488SSam Leffler 
1838751327cSBill Fenner struct slist;
1848751327cSBill Fenner 
1858cf6c252SPaul Traina struct stmt {
1868cf6c252SPaul Traina 	int code;
1878751327cSBill Fenner 	struct slist *jt;	/*only for relative jump in block*/
1888751327cSBill Fenner 	struct slist *jf;	/*only for relative jump in block*/
1898cf6c252SPaul Traina 	bpf_int32 k;
1908cf6c252SPaul Traina };
1918cf6c252SPaul Traina 
1928cf6c252SPaul Traina struct slist {
1938cf6c252SPaul Traina 	struct stmt s;
1948cf6c252SPaul Traina 	struct slist *next;
1958cf6c252SPaul Traina };
1968cf6c252SPaul Traina 
1978cf6c252SPaul Traina /*
1988cf6c252SPaul Traina  * A bit vector to represent definition sets.  We assume TOT_REGISTERS
1998cf6c252SPaul Traina  * is smaller than 8*sizeof(atomset).
2008cf6c252SPaul Traina  */
2018cf6c252SPaul Traina typedef bpf_u_int32 atomset;
2028cf6c252SPaul Traina #define ATOMMASK(n) (1 << (n))
2038cf6c252SPaul Traina #define ATOMELEM(d, n) (d & ATOMMASK(n))
2048cf6c252SPaul Traina 
2058cf6c252SPaul Traina /*
2068cf6c252SPaul Traina  * An unbounded set.
2078cf6c252SPaul Traina  */
2088cf6c252SPaul Traina typedef bpf_u_int32 *uset;
2098cf6c252SPaul Traina 
2108cf6c252SPaul Traina /*
2118cf6c252SPaul Traina  * Total number of atomic entities, including accumulator (A) and index (X).
2128cf6c252SPaul Traina  * We treat all these guys similarly during flow analysis.
2138cf6c252SPaul Traina  */
2148cf6c252SPaul Traina #define N_ATOMS (BPF_MEMWORDS+2)
2158cf6c252SPaul Traina 
2168cf6c252SPaul Traina struct edge {
2178cf6c252SPaul Traina 	int id;
2188cf6c252SPaul Traina 	int code;
2198cf6c252SPaul Traina 	uset edom;
2208cf6c252SPaul Traina 	struct block *succ;
2218cf6c252SPaul Traina 	struct block *pred;
2228cf6c252SPaul Traina 	struct edge *next;	/* link list of incoming edges for a node */
2238cf6c252SPaul Traina };
2248cf6c252SPaul Traina 
2258cf6c252SPaul Traina struct block {
2268cf6c252SPaul Traina 	int id;
2278cf6c252SPaul Traina 	struct slist *stmts;	/* side effect stmts */
2288cf6c252SPaul Traina 	struct stmt s;		/* branch stmt */
2298cf6c252SPaul Traina 	int mark;
2308cf6c252SPaul Traina 	int longjt;		/* jt branch requires long jump */
2318cf6c252SPaul Traina 	int longjf;		/* jf branch requires long jump */
2328cf6c252SPaul Traina 	int level;
2338cf6c252SPaul Traina 	int offset;
2348cf6c252SPaul Traina 	int sense;
2358cf6c252SPaul Traina 	struct edge et;
2368cf6c252SPaul Traina 	struct edge ef;
2378cf6c252SPaul Traina 	struct block *head;
2388cf6c252SPaul Traina 	struct block *link;	/* link field used by optimizer */
2398cf6c252SPaul Traina 	uset dom;
2408cf6c252SPaul Traina 	uset closure;
2418cf6c252SPaul Traina 	struct edge *in_edges;
2428cf6c252SPaul Traina 	atomset def, kill;
2438cf6c252SPaul Traina 	atomset in_use;
2448cf6c252SPaul Traina 	atomset out_use;
2458cf6c252SPaul Traina 	int oval;
2468cf6c252SPaul Traina 	int val[N_ATOMS];
2478cf6c252SPaul Traina };
2488cf6c252SPaul Traina 
2498cf6c252SPaul Traina struct arth {
2508cf6c252SPaul Traina 	struct block *b;	/* protocol checks */
2518cf6c252SPaul Traina 	struct slist *s;	/* stmt list */
2528cf6c252SPaul Traina 	int regno;		/* virtual register number of result */
2538cf6c252SPaul Traina };
2548cf6c252SPaul Traina 
2558cf6c252SPaul Traina struct qual {
2568cf6c252SPaul Traina 	unsigned char addr;
2578cf6c252SPaul Traina 	unsigned char proto;
2588cf6c252SPaul Traina 	unsigned char dir;
2598cf6c252SPaul Traina 	unsigned char pad;
2608cf6c252SPaul Traina };
2618cf6c252SPaul Traina 
2628cf6c252SPaul Traina struct arth *gen_loadi(int);
2638cf6c252SPaul Traina struct arth *gen_load(int, struct arth *, int);
2648cf6c252SPaul Traina struct arth *gen_loadlen(void);
2658cf6c252SPaul Traina struct arth *gen_neg(struct arth *);
2668cf6c252SPaul Traina struct arth *gen_arth(int, struct arth *, struct arth *);
2678cf6c252SPaul Traina 
2688cf6c252SPaul Traina void gen_and(struct block *, struct block *);
2698cf6c252SPaul Traina void gen_or(struct block *, struct block *);
2708cf6c252SPaul Traina void gen_not(struct block *);
2718cf6c252SPaul Traina 
2728cf6c252SPaul Traina struct block *gen_scode(const char *, struct qual);
2738cf6c252SPaul Traina struct block *gen_ecode(const u_char *, struct qual);
2740a94d38fSBill Fenner struct block *gen_acode(const u_char *, struct qual);
2758cf6c252SPaul Traina struct block *gen_mcode(const char *, const char *, int, struct qual);
2768751327cSBill Fenner #ifdef INET6
2778751327cSBill Fenner struct block *gen_mcode6(const char *, const char *, int, struct qual);
2788751327cSBill Fenner #endif
2798cf6c252SPaul Traina struct block *gen_ncode(const char *, bpf_u_int32, struct qual);
2808cf6c252SPaul Traina struct block *gen_proto_abbrev(int);
2818cf6c252SPaul Traina struct block *gen_relation(int, struct arth *, struct arth *, int);
2828cf6c252SPaul Traina struct block *gen_less(int);
2838cf6c252SPaul Traina struct block *gen_greater(int);
2848cf6c252SPaul Traina struct block *gen_byteop(int, int, int);
2858cf6c252SPaul Traina struct block *gen_broadcast(int);
2868cf6c252SPaul Traina struct block *gen_multicast(int);
2878cf6c252SPaul Traina struct block *gen_inbound(int);
2888cf6c252SPaul Traina 
289dc2c7305SBill Fenner struct block *gen_vlan(int);
29004fb2745SSam Leffler struct block *gen_mpls(int);
291dc2c7305SBill Fenner 
292ee2dd488SSam Leffler struct block *gen_atmfield_code(int atmfield, bpf_int32 jvalue, bpf_u_int32 jtype, int reverse);
293feb4ecdbSBruce M Simpson struct block *gen_atmtype_abbrev(int type);
294feb4ecdbSBruce M Simpson struct block *gen_atmmulti_abbrev(int type);
295feb4ecdbSBruce M Simpson 
296ee2dd488SSam Leffler struct block *gen_mtp3field_code(int mtp3field, bpf_u_int32 jvalue, bpf_u_int32 jtype, int reverse);
297ee2dd488SSam Leffler 
298feb4ecdbSBruce M Simpson struct block *gen_pf_ifname(const char *);
299feb4ecdbSBruce M Simpson struct block *gen_pf_rnr(int);
300feb4ecdbSBruce M Simpson struct block *gen_pf_srnr(int);
301feb4ecdbSBruce M Simpson struct block *gen_pf_ruleset(char *);
302feb4ecdbSBruce M Simpson struct block *gen_pf_reason(int);
303feb4ecdbSBruce M Simpson struct block *gen_pf_action(int);
304feb4ecdbSBruce M Simpson struct block *gen_pf_dir(int);
305feb4ecdbSBruce M Simpson 
3068cf6c252SPaul Traina void bpf_optimize(struct block **);
307dc2c7305SBill Fenner void bpf_error(const char *, ...)
308ee2dd488SSam Leffler     __attribute__((noreturn, format (printf, 1, 2)));
3098cf6c252SPaul Traina 
3108cf6c252SPaul Traina void finish_parse(struct block *);
3118cf6c252SPaul Traina char *sdup(const char *);
3128cf6c252SPaul Traina 
3138cf6c252SPaul Traina struct bpf_insn *icode_to_fcode(struct block *, int *);
3148cf6c252SPaul Traina int pcap_parse(void);
3158cf6c252SPaul Traina void lex_init(char *);
316dc2c7305SBill Fenner void lex_cleanup(void);
3178cf6c252SPaul Traina void sappend(struct slist *, struct slist *);
3188cf6c252SPaul Traina 
3198cf6c252SPaul Traina /* XXX */
3208cf6c252SPaul Traina #define JT(b)  ((b)->et.succ)
3218cf6c252SPaul Traina #define JF(b)  ((b)->ef.succ)
3228751327cSBill Fenner 
3238751327cSBill Fenner extern int no_optimize;
324