xref: /openbsd-src/lib/libpcap/gencode.h (revision 01efc7ef8ab68f8ab38c9fcffd34efd1d036b4b9)
1 /*	$OpenBSD: gencode.h,v 1.6 1999/07/20 04:49:54 deraadt 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: /home/cvs/src/lib/libpcap/gencode.h,v 1.6 1999/07/20 04:49:54 deraadt 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 #define Q_IGRP		9
45 
46 
47 #define	Q_ATALK		10
48 #define	Q_DECNET	11
49 #define	Q_LAT		12
50 #define Q_SCA		13
51 #define	Q_MOPRC		14
52 #define	Q_MOPDL		15
53 
54 /* Directional qualifiers. */
55 
56 #define Q_SRC		1
57 #define Q_DST		2
58 #define Q_OR		3
59 #define Q_AND		4
60 
61 #define Q_DEFAULT	0
62 #define Q_UNDEF		255
63 
64 struct stmt {
65 	int code;
66 	bpf_int32 k;
67 };
68 
69 struct slist {
70 	struct stmt s;
71 	struct slist *next;
72 };
73 
74 /*
75  * A bit vector to represent definition sets.  We assume TOT_REGISTERS
76  * is smaller than 8*sizeof(atomset).
77  */
78 typedef bpf_u_int32 atomset;
79 #define ATOMMASK(n) (1 << (n))
80 #define ATOMELEM(d, n) (d & ATOMMASK(n))
81 
82 /*
83  * An unbounded set.
84  */
85 typedef bpf_u_int32 *uset;
86 
87 /*
88  * Total number of atomic entities, including accumulator (A) and index (X).
89  * We treat all these guys similarly during flow analysis.
90  */
91 #define N_ATOMS (BPF_MEMWORDS+2)
92 
93 struct edge {
94 	int id;
95 	int code;
96 	uset edom;
97 	struct block *succ;
98 	struct block *pred;
99 	struct edge *next;	/* link list of incoming edges for a node */
100 };
101 
102 struct block {
103 	int id;
104 	struct slist *stmts;	/* side effect stmts */
105 	struct stmt s;		/* branch stmt */
106 	int mark;
107 	int longjt;		/* jt branch requires long jump */
108 	int longjf;		/* jf branch requires long jump */
109 	int level;
110 	int offset;
111 	int sense;
112 	struct edge et;
113 	struct edge ef;
114 	struct block *head;
115 	struct block *link;	/* link field used by optimizer */
116 	uset dom;
117 	uset closure;
118 	struct edge *in_edges;
119 	atomset def, kill;
120 	atomset in_use;
121 	atomset out_use;
122 	int oval;
123 	int val[N_ATOMS];
124 };
125 
126 struct arth {
127 	struct block *b;	/* protocol checks */
128 	struct slist *s;	/* stmt list */
129 	int regno;		/* virtual register number of result */
130 };
131 
132 struct qual {
133 	unsigned char addr;
134 	unsigned char proto;
135 	unsigned char dir;
136 	unsigned char pad;
137 };
138 
139 struct arth *gen_loadi(int);
140 struct arth *gen_load(int, struct arth *, int);
141 struct arth *gen_loadlen(void);
142 struct arth *gen_neg(struct arth *);
143 struct arth *gen_arth(int, struct arth *, struct arth *);
144 
145 void gen_and(struct block *, struct block *);
146 void gen_or(struct block *, struct block *);
147 void gen_not(struct block *);
148 
149 struct block *gen_scode(const char *, struct qual);
150 struct block *gen_ecode(const u_char *, struct qual);
151 struct block *gen_mcode(const char *, const char *, int, struct qual);
152 struct block *gen_ncode(const char *, bpf_u_int32, struct qual);
153 struct block *gen_proto_abbrev(int);
154 struct block *gen_relation(int, struct arth *, struct arth *, int);
155 struct block *gen_less(int);
156 struct block *gen_greater(int);
157 struct block *gen_byteop(int, int, int);
158 struct block *gen_broadcast(int);
159 struct block *gen_multicast(int);
160 struct block *gen_inbound(int);
161 
162 void bpf_optimize(struct block **);
163 #ifdef __STDC__
164 __dead void bpf_error(const char *, ...)
165     __attribute__((volatile, format (printf, 1, 2)));
166 #endif
167 
168 void finish_parse(struct block *);
169 char *sdup(const char *);
170 
171 struct bpf_insn *icode_to_fcode(struct block *, int *);
172 int pcap_parse(void);
173 void lex_init(char *);
174 void sappend(struct slist *, struct slist *);
175 
176 /* XXX */
177 #define JT(b)  ((b)->et.succ)
178 #define JF(b)  ((b)->ef.succ)
179