]> The Tcpdump Group git mirrors - libpcap/blob - grammar.y
Fix up the RCS ID, and put in a line indicating the NetBSD header file
[libpcap] / grammar.y
1 %{
2 /*
3 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
17 * written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 *
22 */
23 #ifndef lint
24 static const char rcsid[] =
25 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.69 2001-04-17 08:25:23 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 #include <stdlib.h>
36
37 #if __STDC__
38 struct mbuf;
39 struct rtentry;
40 #endif
41
42 #include <net/if.h>
43
44 #include <netinet/in.h>
45
46 #include <stdio.h>
47
48 #include "pcap-int.h"
49
50 #include "gencode.h"
51 #include <pcap-namedb.h>
52
53 #ifdef HAVE_OS_PROTO_H
54 #include "os-proto.h"
55 #endif
56
57 #define QSET(q, p, d, a) (q).proto = (p),\
58 (q).dir = (d),\
59 (q).addr = (a)
60
61 int n_errors = 0;
62
63 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
64
65 static void
66 yyerror(char *msg)
67 {
68 ++n_errors;
69 bpf_error("%s", msg);
70 /* NOTREACHED */
71 }
72
73 #ifndef YYBISON
74 int yyparse(void);
75
76 int
77 pcap_parse()
78 {
79 return (yyparse());
80 }
81 #endif
82
83 %}
84
85 %union {
86 int i;
87 bpf_u_int32 h;
88 u_char *e;
89 char *s;
90 struct stmt *stmt;
91 struct arth *a;
92 struct {
93 struct qual q;
94 struct block *b;
95 } blk;
96 struct block *rblk;
97 }
98
99 %type <blk> expr id nid pid term rterm qid
100 %type <blk> head
101 %type <i> pqual dqual aqual ndaqual
102 %type <a> arth narth
103 %type <i> byteop pname pnum relop irelop
104 %type <blk> and or paren not null prog
105 %type <rblk> other
106
107 %token DST SRC HOST GATEWAY
108 %token NET MASK PORT LESS GREATER PROTO PROTOCHAIN BYTE
109 %token ARP RARP IP TCP UDP ICMP IGMP IGRP PIM VRRP
110 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
111 %token TK_BROADCAST TK_MULTICAST
112 %token NUM INBOUND OUTBOUND
113 %token LINK
114 %token GEQ LEQ NEQ
115 %token ID EID HID HID6 AID
116 %token LSH RSH
117 %token LEN
118 %token IPV6 ICMPV6 AH ESP
119 %token VLAN
120 %token ISO ESIS ISIS CLNP
121 %token STP
122 %token IPX
123 %token NETBEUI
124
125 %type <s> ID
126 %type <e> EID
127 %type <e> AID
128 %type <s> HID HID6
129 %type <i> NUM
130
131 %left OR AND
132 %nonassoc '!'
133 %left '|'
134 %left '&'
135 %left LSH RSH
136 %left '+' '-'
137 %left '*' '/'
138 %nonassoc UMINUS
139 %%
140 prog: null expr
141 {
142 finish_parse($2.b);
143 }
144 | null
145 ;
146 null: /* null */ { $$.q = qerr; }
147 ;
148 expr: term
149 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
150 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
151 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
152 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
153 ;
154 and: AND { $$ = $<blk>0; }
155 ;
156 or: OR { $$ = $<blk>0; }
157 ;
158 id: nid
159 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
160 $$.q = $<blk>0.q); }
161 | paren pid ')' { $$ = $2; }
162 ;
163 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
164 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
165 $$.q = $<blk>0.q); }
166 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
167 $$.q = $<blk>0.q); }
168 | HID {
169 /* Decide how to parse HID based on proto */
170 $$.q = $<blk>0.q;
171 $$.b = gen_ncode($1, 0, $$.q);
172 }
173 | HID6 '/' NUM {
174 #ifdef INET6
175 $$.b = gen_mcode6($1, NULL, $3,
176 $$.q = $<blk>0.q);
177 #else
178 bpf_error("'ip6addr/prefixlen' not supported "
179 "in this configuration");
180 #endif /*INET6*/
181 }
182 | HID6 {
183 #ifdef INET6
184 $$.b = gen_mcode6($1, 0, 128,
185 $$.q = $<blk>0.q);
186 #else
187 bpf_error("'ip6addr' not supported "
188 "in this configuration");
189 #endif /*INET6*/
190 }
191 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
192 | AID { $$.b = gen_acode($1, $$.q = $<blk>0.q); }
193 | not id { gen_not($2.b); $$ = $2; }
194 ;
195 not: '!' { $$ = $<blk>0; }
196 ;
197 paren: '(' { $$ = $<blk>0; }
198 ;
199 pid: nid
200 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
201 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
202 ;
203 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
204 $$.q = $<blk>0.q); }
205 | pid
206 ;
207 term: rterm
208 | not term { gen_not($2.b); $$ = $2; }
209 ;
210 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
211 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
212 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
213 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
214 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
215 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
216 ;
217 rterm: head id { $$ = $2; }
218 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
219 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
220 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
221 $$.q = qerr; }
222 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
223 $$.q = qerr; }
224 | other { $$.b = $1; $$.q = qerr; }
225 ;
226 /* protocol level qualifiers */
227 pqual: pname
228 | { $$ = Q_DEFAULT; }
229 ;
230 /* 'direction' qualifiers */
231 dqual: SRC { $$ = Q_SRC; }
232 | DST { $$ = Q_DST; }
233 | SRC OR DST { $$ = Q_OR; }
234 | DST OR SRC { $$ = Q_OR; }
235 | SRC AND DST { $$ = Q_AND; }
236 | DST AND SRC { $$ = Q_AND; }
237 ;
238 /* address type qualifiers */
239 aqual: HOST { $$ = Q_HOST; }
240 | NET { $$ = Q_NET; }
241 | PORT { $$ = Q_PORT; }
242 ;
243 /* non-directional address type qualifiers */
244 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
245 ;
246 pname: LINK { $$ = Q_LINK; }
247 | IP { $$ = Q_IP; }
248 | ARP { $$ = Q_ARP; }
249 | RARP { $$ = Q_RARP; }
250 | TCP { $$ = Q_TCP; }
251 | UDP { $$ = Q_UDP; }
252 | ICMP { $$ = Q_ICMP; }
253 | IGMP { $$ = Q_IGMP; }
254 | IGRP { $$ = Q_IGRP; }
255 | PIM { $$ = Q_PIM; }
256 | VRRP { $$ = Q_VRRP; }
257 | ATALK { $$ = Q_ATALK; }
258 | AARP { $$ = Q_AARP; }
259 | DECNET { $$ = Q_DECNET; }
260 | LAT { $$ = Q_LAT; }
261 | SCA { $$ = Q_SCA; }
262 | MOPDL { $$ = Q_MOPDL; }
263 | MOPRC { $$ = Q_MOPRC; }
264 | IPV6 { $$ = Q_IPV6; }
265 | ICMPV6 { $$ = Q_ICMPV6; }
266 | AH { $$ = Q_AH; }
267 | ESP { $$ = Q_ESP; }
268 | ISO { $$ = Q_ISO; }
269 | ESIS { $$ = Q_ESIS; }
270 | ISIS { $$ = Q_ISIS; }
271 | CLNP { $$ = Q_CLNP; }
272 | STP { $$ = Q_STP; }
273 | IPX { $$ = Q_IPX; }
274 | NETBEUI { $$ = Q_NETBEUI; }
275 ;
276 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
277 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
278 | LESS NUM { $$ = gen_less($2); }
279 | GREATER NUM { $$ = gen_greater($2); }
280 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
281 | INBOUND { $$ = gen_inbound(0); }
282 | OUTBOUND { $$ = gen_inbound(1); }
283 | VLAN pnum { $$ = gen_vlan($2); }
284 | VLAN { $$ = gen_vlan(-1); }
285 ;
286 relop: '>' { $$ = BPF_JGT; }
287 | GEQ { $$ = BPF_JGE; }
288 | '=' { $$ = BPF_JEQ; }
289 ;
290 irelop: LEQ { $$ = BPF_JGT; }
291 | '<' { $$ = BPF_JGE; }
292 | NEQ { $$ = BPF_JEQ; }
293 ;
294 arth: pnum { $$ = gen_loadi($1); }
295 | narth
296 ;
297 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
298 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
299 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
300 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
301 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
302 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
303 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
304 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
305 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
306 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
307 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
308 | paren narth ')' { $$ = $2; }
309 | LEN { $$ = gen_loadlen(); }
310 ;
311 byteop: '&' { $$ = '&'; }
312 | '|' { $$ = '|'; }
313 | '<' { $$ = '<'; }
314 | '>' { $$ = '>'; }
315 | '=' { $$ = '='; }
316 ;
317 pnum: NUM
318 | paren pnum ')' { $$ = $2; }
319 ;
320 %%