1 /*	$MirOS: src/lib/libpcap/gencode.h,v 1.2 2013/10/31 20:06:37 tg Exp $ */
2 /*	$OpenBSD: gencode.h,v 1.14 2007/01/02 18:31:21 reyk Exp $	*/
3 
4 /*
5  * Copyright © 2013
6  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
7  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that: (1) source code distributions
12  * retain the above copyright notice and this paragraph in its entirety, (2)
13  * distributions including binary code include the above copyright notice and
14  * this paragraph in its entirety in the documentation or other materials
15  * provided with the distribution, and (3) all advertising materials mentioning
16  * features or use of this software display the following acknowledgement:
17  * ``This product includes software developed by the University of California,
18  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
19  * the University nor the names of its contributors may be used to endorse
20  * or promote products derived from this software without specific prior
21  * written permission.
22  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25  *
26  * @(#) $Header: /cvs/src/lib/libpcap/gencode.h,v 1.14 2007/01/02 18:31:21 reyk Exp $ (LBL)
27  */
28 
29 /* Address qualifiers. */
30 
31 #define Q_HOST		1
32 #define Q_NET		2
33 #define Q_PORT		3
34 #define Q_GATEWAY	4
35 #define Q_PROTO		5
36 #define Q_PROTOCHAIN	6
37 
38 /* Protocol qualifiers. */
39 
40 #define Q_LINK		1
41 #define Q_IP		2
42 #define Q_ARP		3
43 #define Q_RARP		4
44 #define Q_TCP		5
45 #define Q_UDP		6
46 #define Q_ICMP		7
47 #define Q_IGMP		8
48 #define Q_IGRP		9
49 
50 
51 #define	Q_ATALK		10
52 #define	Q_DECNET	11
53 #define	Q_LAT		12
54 #define Q_SCA		13
55 #define	Q_MOPRC		14
56 #define	Q_MOPDL		15
57 
58 
59 #define Q_IPV6		16
60 #define Q_ICMPV6	17
61 #define Q_AH		18
62 #define Q_ESP		19
63 
64 #define Q_PIM		20
65 #define Q_STP		21
66 
67 /* Directional qualifiers. */
68 
69 #define Q_SRC		1
70 #define Q_DST		2
71 #define Q_OR		3
72 #define Q_AND		4
73 #define Q_ADDR1		5
74 #define Q_ADDR2		6
75 #define Q_ADDR3		7
76 #define Q_ADDR4		8
77 
78 #define Q_DEFAULT	0
79 #define Q_UNDEF		255
80 
81 struct slist;
82 
83 struct stmt {
84 	int code;
85 	struct slist *jt;	/*only for relative jump in block*/
86 	struct slist *jf;	/*only for relative jump in block*/
87 	bpf_int32 k;
88 };
89 
90 struct slist {
91 	struct stmt s;
92 	struct slist *next;
93 };
94 
95 /*
96  * A bit vector to represent definition sets.  We assume TOT_REGISTERS
97  * is smaller than 8*sizeof(atomset).
98  */
99 typedef bpf_u_int32 atomset;
100 #define ATOMMASK(n) (1 << (n))
101 #define ATOMELEM(d, n) (d & ATOMMASK(n))
102 
103 /*
104  * An unbounded set.
105  */
106 typedef bpf_u_int32 *uset;
107 
108 /*
109  * Total number of atomic entities, including accumulator (A) and index (X).
110  * We treat all these guys similarly during flow analysis.
111  */
112 #define N_ATOMS (BPF_MEMWORDS+2)
113 
114 struct edge {
115 	int id;
116 	int code;
117 	uset edom;
118 	struct block *succ;
119 	struct block *pred;
120 	struct edge *next;	/* link list of incoming edges for a node */
121 };
122 
123 struct block {
124 	int id;
125 	struct slist *stmts;	/* side effect stmts */
126 	struct stmt s;		/* branch stmt */
127 	int mark;
128 	int longjt;		/* jt branch requires long jump */
129 	int longjf;		/* jf branch requires long jump */
130 	int level;
131 	int offset;
132 	int sense;
133 	struct edge et;
134 	struct edge ef;
135 	struct block *head;
136 	struct block *link;	/* link field used by optimizer */
137 	uset dom;
138 	uset closure;
139 	struct edge *in_edges;
140 	atomset def, kill;
141 	atomset in_use;
142 	atomset out_use;
143 	int oval;
144 	int val[N_ATOMS];
145 };
146 
147 struct arth {
148 	struct block *b;	/* protocol checks */
149 	struct slist *s;	/* stmt list */
150 	int regno;		/* virtual register number of result */
151 };
152 
153 struct qual {
154 	unsigned char addr;
155 	unsigned char proto;
156 	unsigned char dir;
157 	unsigned char pad;
158 };
159 
160 struct arth *gen_loadi(int);
161 struct arth *gen_load(int, struct arth *, int);
162 struct arth *gen_loadlen(void);
163 struct arth *gen_neg(struct arth *);
164 struct arth *gen_arth(int, struct arth *, struct arth *);
165 
166 void gen_and(struct block *, struct block *);
167 void gen_or(struct block *, struct block *);
168 void gen_not(struct block *);
169 
170 struct block *gen_scode(const char *, struct qual);
171 struct block *gen_ecode(const u_char *, struct qual);
172 struct block *gen_mcode(const char *, const char *, int, struct qual);
173 #ifdef INET6
174 struct block *gen_mcode6(const char *, const char *, int, struct qual);
175 #endif
176 struct block *gen_ncode(const char *, bpf_u_int32, struct qual);
177 struct block *gen_proto_abbrev(int);
178 struct block *gen_relation(int, struct arth *, struct arth *, int);
179 struct block *gen_less(int);
180 struct block *gen_greater(int);
181 struct block *gen_byteop(int, int, int);
182 struct block *gen_broadcast(int);
183 struct block *gen_multicast(int);
184 struct block *gen_inbound(int);
185 
186 struct block *gen_pf_ifname(char *);
187 struct block *gen_pf_rnr(int);
188 struct block *gen_pf_srnr(int);
189 struct block *gen_pf_ruleset(char *);
190 struct block *gen_pf_reason(int);
191 struct block *gen_pf_action(int);
192 struct block *gen_pf_dir(int);
193 
194 struct block *gen_p80211_type(int, int);
195 struct block *gen_p80211_fcdir(int);
196 
197 void bpf_optimize(struct block **);
198 __dead void bpf_error(const char *, ...)
199     __attribute__((__volatile__, __format__ (__printf__, 1, 2)));
200 
201 void finish_parse(struct block *);
202 char *sdup(const char *);
203 
204 struct bpf_insn *icode_to_fcode(struct block *, int *);
205 int pcap_parse(void);
206 void lex_init(char *);
207 void sappend(struct slist *, struct slist *);
208 
209 /* XXX */
210 #define JT(b)  ((b)->et.succ)
211 #define JF(b)  ((b)->ef.succ)
212 
213 extern int no_optimize;
214