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