xref: /freebsd/contrib/libpcap/gencode.h (revision eb9f0330c07fd11402519316dd91a323c93f84fa)
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  *
21fae05455SBill Fenner  * $FreeBSD$
22eb9f0330SBruce M Simpson  * @(#) $Header: /tcpdump/master/libpcap/gencode.h,v 1.58.2.1 2004/03/28 21:45:31 fenner Exp $ (LBL)
23eb9f0330SBruce M Simpson  */
24eb9f0330SBruce M Simpson 
25eb9f0330SBruce M Simpson /*
26eb9f0330SBruce M Simpson  * ATM support:
27eb9f0330SBruce M Simpson  *
28eb9f0330SBruce M Simpson  * Copyright (c) 1997 Yen Yen Lim and North Dakota State University
29eb9f0330SBruce M Simpson  * All rights reserved.
30eb9f0330SBruce M Simpson  *
31eb9f0330SBruce M Simpson  * Redistribution and use in source and binary forms, with or without
32eb9f0330SBruce M Simpson  * modification, are permitted provided that the following conditions
33eb9f0330SBruce M Simpson  * are met:
34eb9f0330SBruce M Simpson  * 1. Redistributions of source code must retain the above copyright
35eb9f0330SBruce M Simpson  *    notice, this list of conditions and the following disclaimer.
36eb9f0330SBruce M Simpson  * 2. Redistributions in binary form must reproduce the above copyright
37eb9f0330SBruce M Simpson  *    notice, this list of conditions and the following disclaimer in the
38eb9f0330SBruce M Simpson  *    documentation and/or other materials provided with the distribution.
39eb9f0330SBruce M Simpson  * 3. All advertising materials mentioning features or use of this software
40eb9f0330SBruce M Simpson  *    must display the following acknowledgement:
41eb9f0330SBruce M Simpson  *      This product includes software developed by Yen Yen Lim and
42eb9f0330SBruce M Simpson  *      North Dakota State University
43eb9f0330SBruce M Simpson  * 4. The name of the author may not be used to endorse or promote products
44eb9f0330SBruce M Simpson  *    derived from this software without specific prior written permission.
45eb9f0330SBruce M Simpson  *
46eb9f0330SBruce M Simpson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47eb9f0330SBruce M Simpson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
48eb9f0330SBruce M Simpson  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49eb9f0330SBruce M Simpson  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
50eb9f0330SBruce M Simpson  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51eb9f0330SBruce M Simpson  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52eb9f0330SBruce M Simpson  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53eb9f0330SBruce M Simpson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54eb9f0330SBruce M Simpson  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
55eb9f0330SBruce M Simpson  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56eb9f0330SBruce M Simpson  * POSSIBILITY OF SUCH DAMAGE.
578cf6c252SPaul Traina  */
588cf6c252SPaul Traina 
598cf6c252SPaul Traina /* Address qualifiers. */
608cf6c252SPaul Traina 
618cf6c252SPaul Traina #define Q_HOST		1
628cf6c252SPaul Traina #define Q_NET		2
638cf6c252SPaul Traina #define Q_PORT		3
648cf6c252SPaul Traina #define Q_GATEWAY	4
658cf6c252SPaul Traina #define Q_PROTO		5
66fae05455SBill Fenner #define Q_PROTOCHAIN	6
678cf6c252SPaul Traina 
688cf6c252SPaul Traina /* Protocol qualifiers. */
698cf6c252SPaul Traina 
708cf6c252SPaul Traina #define Q_LINK		1
718cf6c252SPaul Traina #define Q_IP		2
728cf6c252SPaul Traina #define Q_ARP		3
738cf6c252SPaul Traina #define Q_RARP		4
74c0653930SBill Fenner #define Q_SCTP		5
75c0653930SBill Fenner #define Q_TCP		6
76c0653930SBill Fenner #define Q_UDP		7
77c0653930SBill Fenner #define Q_ICMP		8
78c0653930SBill Fenner #define Q_IGMP		9
79c0653930SBill Fenner #define Q_IGRP		10
808cf6c252SPaul Traina 
818cf6c252SPaul Traina 
82c0653930SBill Fenner #define	Q_ATALK		11
83c0653930SBill Fenner #define	Q_DECNET	12
84c0653930SBill Fenner #define	Q_LAT		13
85c0653930SBill Fenner #define Q_SCA		14
86c0653930SBill Fenner #define	Q_MOPRC		15
87c0653930SBill Fenner #define	Q_MOPDL		16
88fae05455SBill Fenner 
898e1481d1SBill Fenner 
90c0653930SBill Fenner #define Q_IPV6		17
91c0653930SBill Fenner #define Q_ICMPV6	18
92c0653930SBill Fenner #define Q_AH		19
93c0653930SBill Fenner #define Q_ESP		20
94fae05455SBill Fenner 
95c0653930SBill Fenner #define Q_PIM		21
96c0653930SBill Fenner #define Q_VRRP		22
97fae05455SBill Fenner 
98c0653930SBill Fenner #define Q_AARP		23
998e1481d1SBill Fenner 
100c0653930SBill Fenner #define Q_ISO		24
101c0653930SBill Fenner #define Q_ESIS		25
102c0653930SBill Fenner #define Q_ISIS		26
103c0653930SBill Fenner #define Q_CLNP		27
104c0653930SBill Fenner 
105c0653930SBill Fenner #define Q_STP		28
106c0653930SBill Fenner 
107c0653930SBill Fenner #define Q_IPX		29
108c0653930SBill Fenner 
109c0653930SBill Fenner #define Q_NETBEUI	30
1108cf6c252SPaul Traina 
111eb9f0330SBruce M Simpson /* IS-IS Levels */
112eb9f0330SBruce M Simpson #define Q_ISIS_L1       31
113eb9f0330SBruce M Simpson #define Q_ISIS_L2       32
114eb9f0330SBruce M Simpson /* PDU types */
115eb9f0330SBruce M Simpson #define Q_ISIS_IIH      33
116eb9f0330SBruce M Simpson #define Q_ISIS_LAN_IIH  34
117eb9f0330SBruce M Simpson #define Q_ISIS_PTP_IIH  35
118eb9f0330SBruce M Simpson #define Q_ISIS_SNP      36
119eb9f0330SBruce M Simpson #define Q_ISIS_CSNP     37
120eb9f0330SBruce M Simpson #define Q_ISIS_PSNP     38
121eb9f0330SBruce M Simpson #define Q_ISIS_LSP      39
122eb9f0330SBruce M Simpson 
1238cf6c252SPaul Traina /* Directional qualifiers. */
1248cf6c252SPaul Traina 
1258cf6c252SPaul Traina #define Q_SRC		1
1268cf6c252SPaul Traina #define Q_DST		2
1278cf6c252SPaul Traina #define Q_OR		3
1288cf6c252SPaul Traina #define Q_AND		4
1298cf6c252SPaul Traina 
1308cf6c252SPaul Traina #define Q_DEFAULT	0
1318cf6c252SPaul Traina #define Q_UNDEF		255
1328cf6c252SPaul Traina 
133eb9f0330SBruce M Simpson /* ATM types */
134eb9f0330SBruce M Simpson #define A_METAC		22	/* Meta signalling Circuit */
135eb9f0330SBruce M Simpson #define A_BCC		23	/* Broadcast Circuit */
136eb9f0330SBruce M Simpson #define A_OAMF4SC	24	/* Segment OAM F4 Circuit */
137eb9f0330SBruce M Simpson #define A_OAMF4EC	25	/* End-to-End OAM F4 Circuit */
138eb9f0330SBruce M Simpson #define A_SC		26	/* Signalling Circuit*/
139eb9f0330SBruce M Simpson #define A_ILMIC		27	/* ILMI Circuit */
140eb9f0330SBruce M Simpson #define A_OAM		28	/* OAM cells : F4 only */
141eb9f0330SBruce M Simpson #define A_OAMF4		29	/* OAM F4 cells: Segment + End-to-end */
142eb9f0330SBruce M Simpson #define A_LANE		30	/* LANE traffic */
143eb9f0330SBruce M Simpson #define A_LLC		31	/* LLC-encapsulated traffic */
144eb9f0330SBruce M Simpson 
145eb9f0330SBruce M Simpson /* Based on Q.2931 signalling protocol */
146eb9f0330SBruce M Simpson #define A_SETUP		41	/* Setup message */
147eb9f0330SBruce M Simpson #define A_CALLPROCEED	42	/* Call proceeding message */
148eb9f0330SBruce M Simpson #define A_CONNECT	43	/* Connect message */
149eb9f0330SBruce M Simpson #define A_CONNECTACK	44	/* Connect Ack message */
150eb9f0330SBruce M Simpson #define A_RELEASE	45	/* Release message */
151eb9f0330SBruce M Simpson #define A_RELEASE_DONE	46	/* Release message */
152eb9f0330SBruce M Simpson 
153eb9f0330SBruce M Simpson /* ATM field types */
154eb9f0330SBruce M Simpson #define A_VPI		51
155eb9f0330SBruce M Simpson #define A_VCI		52
156eb9f0330SBruce M Simpson #define A_PROTOTYPE	53
157eb9f0330SBruce M Simpson #define A_MSGTYPE	54
158eb9f0330SBruce M Simpson #define A_CALLREFTYPE	55
159eb9f0330SBruce M Simpson 
160eb9f0330SBruce M Simpson #define A_CONNECTMSG	70	/* returns Q.2931 signalling messages for
161eb9f0330SBruce M Simpson 				   establishing and destroying switched
162eb9f0330SBruce M Simpson 				   virtual connection */
163eb9f0330SBruce M Simpson #define A_METACONNECT	71	/* returns Q.2931 signalling messages for
164eb9f0330SBruce M Simpson 				   establishing and destroying predefined
165eb9f0330SBruce M Simpson 				   virtual circuits, such as broadcast
166eb9f0330SBruce M Simpson 				   circuit, oamf4 segment circuit, oamf4
167eb9f0330SBruce M Simpson 				   end-to-end circuits, ILMI circuits or
168eb9f0330SBruce M Simpson 				   connection signalling circuit. */
169eb9f0330SBruce M Simpson 
170fae05455SBill Fenner struct slist;
171fae05455SBill Fenner 
1728cf6c252SPaul Traina struct stmt {
1738cf6c252SPaul Traina 	int code;
174fae05455SBill Fenner 	struct slist *jt;	/*only for relative jump in block*/
175fae05455SBill Fenner 	struct slist *jf;	/*only for relative jump in block*/
1768cf6c252SPaul Traina 	bpf_int32 k;
1778cf6c252SPaul Traina };
1788cf6c252SPaul Traina 
1798cf6c252SPaul Traina struct slist {
1808cf6c252SPaul Traina 	struct stmt s;
1818cf6c252SPaul Traina 	struct slist *next;
1828cf6c252SPaul Traina };
1838cf6c252SPaul Traina 
1848cf6c252SPaul Traina /*
1858cf6c252SPaul Traina  * A bit vector to represent definition sets.  We assume TOT_REGISTERS
1868cf6c252SPaul Traina  * is smaller than 8*sizeof(atomset).
1878cf6c252SPaul Traina  */
1888cf6c252SPaul Traina typedef bpf_u_int32 atomset;
1898cf6c252SPaul Traina #define ATOMMASK(n) (1 << (n))
1908cf6c252SPaul Traina #define ATOMELEM(d, n) (d & ATOMMASK(n))
1918cf6c252SPaul Traina 
1928cf6c252SPaul Traina /*
1938cf6c252SPaul Traina  * An unbounded set.
1948cf6c252SPaul Traina  */
1958cf6c252SPaul Traina typedef bpf_u_int32 *uset;
1968cf6c252SPaul Traina 
1978cf6c252SPaul Traina /*
1988cf6c252SPaul Traina  * Total number of atomic entities, including accumulator (A) and index (X).
1998cf6c252SPaul Traina  * We treat all these guys similarly during flow analysis.
2008cf6c252SPaul Traina  */
2018cf6c252SPaul Traina #define N_ATOMS (BPF_MEMWORDS+2)
2028cf6c252SPaul Traina 
2038cf6c252SPaul Traina struct edge {
2048cf6c252SPaul Traina 	int id;
2058cf6c252SPaul Traina 	int code;
2068cf6c252SPaul Traina 	uset edom;
2078cf6c252SPaul Traina 	struct block *succ;
2088cf6c252SPaul Traina 	struct block *pred;
2098cf6c252SPaul Traina 	struct edge *next;	/* link list of incoming edges for a node */
2108cf6c252SPaul Traina };
2118cf6c252SPaul Traina 
2128cf6c252SPaul Traina struct block {
2138cf6c252SPaul Traina 	int id;
2148cf6c252SPaul Traina 	struct slist *stmts;	/* side effect stmts */
2158cf6c252SPaul Traina 	struct stmt s;		/* branch stmt */
2168cf6c252SPaul Traina 	int mark;
2178cf6c252SPaul Traina 	int longjt;		/* jt branch requires long jump */
2188cf6c252SPaul Traina 	int longjf;		/* jf branch requires long jump */
2198cf6c252SPaul Traina 	int level;
2208cf6c252SPaul Traina 	int offset;
2218cf6c252SPaul Traina 	int sense;
2228cf6c252SPaul Traina 	struct edge et;
2238cf6c252SPaul Traina 	struct edge ef;
2248cf6c252SPaul Traina 	struct block *head;
2258cf6c252SPaul Traina 	struct block *link;	/* link field used by optimizer */
2268cf6c252SPaul Traina 	uset dom;
2278cf6c252SPaul Traina 	uset closure;
2288cf6c252SPaul Traina 	struct edge *in_edges;
2298cf6c252SPaul Traina 	atomset def, kill;
2308cf6c252SPaul Traina 	atomset in_use;
2318cf6c252SPaul Traina 	atomset out_use;
2328cf6c252SPaul Traina 	int oval;
2338cf6c252SPaul Traina 	int val[N_ATOMS];
2348cf6c252SPaul Traina };
2358cf6c252SPaul Traina 
2368cf6c252SPaul Traina struct arth {
2378cf6c252SPaul Traina 	struct block *b;	/* protocol checks */
2388cf6c252SPaul Traina 	struct slist *s;	/* stmt list */
2398cf6c252SPaul Traina 	int regno;		/* virtual register number of result */
2408cf6c252SPaul Traina };
2418cf6c252SPaul Traina 
2428cf6c252SPaul Traina struct qual {
2438cf6c252SPaul Traina 	unsigned char addr;
2448cf6c252SPaul Traina 	unsigned char proto;
2458cf6c252SPaul Traina 	unsigned char dir;
2468cf6c252SPaul Traina 	unsigned char pad;
2478cf6c252SPaul Traina };
2488cf6c252SPaul Traina 
2498cf6c252SPaul Traina struct arth *gen_loadi(int);
2508cf6c252SPaul Traina struct arth *gen_load(int, struct arth *, int);
2518cf6c252SPaul Traina struct arth *gen_loadlen(void);
2528cf6c252SPaul Traina struct arth *gen_neg(struct arth *);
2538cf6c252SPaul Traina struct arth *gen_arth(int, struct arth *, struct arth *);
2548cf6c252SPaul Traina 
2558cf6c252SPaul Traina void gen_and(struct block *, struct block *);
2568cf6c252SPaul Traina void gen_or(struct block *, struct block *);
2578cf6c252SPaul Traina void gen_not(struct block *);
2588cf6c252SPaul Traina 
2598cf6c252SPaul Traina struct block *gen_scode(const char *, struct qual);
2608cf6c252SPaul Traina struct block *gen_ecode(const u_char *, struct qual);
261c0653930SBill Fenner struct block *gen_acode(const u_char *, struct qual);
2628cf6c252SPaul Traina struct block *gen_mcode(const char *, const char *, int, struct qual);
263fae05455SBill Fenner #ifdef INET6
264fae05455SBill Fenner struct block *gen_mcode6(const char *, const char *, int, struct qual);
265fae05455SBill Fenner #endif
2668cf6c252SPaul Traina struct block *gen_ncode(const char *, bpf_u_int32, struct qual);
2678cf6c252SPaul Traina struct block *gen_proto_abbrev(int);
2688cf6c252SPaul Traina struct block *gen_relation(int, struct arth *, struct arth *, int);
2698cf6c252SPaul Traina struct block *gen_less(int);
2708cf6c252SPaul Traina struct block *gen_greater(int);
2718cf6c252SPaul Traina struct block *gen_byteop(int, int, int);
2728cf6c252SPaul Traina struct block *gen_broadcast(int);
2738cf6c252SPaul Traina struct block *gen_multicast(int);
2748cf6c252SPaul Traina struct block *gen_inbound(int);
2758cf6c252SPaul Traina 
2768e1481d1SBill Fenner struct block *gen_vlan(int);
2778e1481d1SBill Fenner 
278eb9f0330SBruce M Simpson struct block *gen_atmfield_code(int atmfield, bpf_u_int32 jvalue, bpf_u_int32 jtype, int reverse);
279eb9f0330SBruce M Simpson struct block *gen_atmtype_abbrev(int type);
280eb9f0330SBruce M Simpson struct block *gen_atmmulti_abbrev(int type);
281eb9f0330SBruce M Simpson 
282eb9f0330SBruce M Simpson struct block *gen_pf_ifname(const char *);
283eb9f0330SBruce M Simpson struct block *gen_pf_rnr(int);
284eb9f0330SBruce M Simpson struct block *gen_pf_srnr(int);
285eb9f0330SBruce M Simpson struct block *gen_pf_ruleset(char *);
286eb9f0330SBruce M Simpson struct block *gen_pf_reason(int);
287eb9f0330SBruce M Simpson struct block *gen_pf_action(int);
288eb9f0330SBruce M Simpson struct block *gen_pf_dir(int);
289eb9f0330SBruce M Simpson 
2908cf6c252SPaul Traina void bpf_optimize(struct block **);
2918e1481d1SBill Fenner void bpf_error(const char *, ...)
2928e1481d1SBill Fenner #if HAVE___ATTRIBUTE__
2938e1481d1SBill Fenner     __attribute__((noreturn, format (printf, 1, 2)))
2948cf6c252SPaul Traina #endif
2958e1481d1SBill Fenner ;
2968cf6c252SPaul Traina 
2978cf6c252SPaul Traina void finish_parse(struct block *);
2988cf6c252SPaul Traina char *sdup(const char *);
2998cf6c252SPaul Traina 
3008cf6c252SPaul Traina struct bpf_insn *icode_to_fcode(struct block *, int *);
3018cf6c252SPaul Traina int pcap_parse(void);
3028cf6c252SPaul Traina void lex_init(char *);
3038e1481d1SBill Fenner void lex_cleanup(void);
3048cf6c252SPaul Traina void sappend(struct slist *, struct slist *);
3058cf6c252SPaul Traina 
3068cf6c252SPaul Traina /* XXX */
3078cf6c252SPaul Traina #define JT(b)  ((b)->et.succ)
3088cf6c252SPaul Traina #define JF(b)  ((b)->ef.succ)
309fae05455SBill Fenner 
310fae05455SBill Fenner extern int no_optimize;
311