]> The Tcpdump Group git mirrors - libpcap/blob - grammar.y
Get rid of probably-unnecessary includes of <net/if.h>.
[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.76 2002-08-05 07:59:43 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #ifdef WIN32
33 #include <pcap-stdinc.h>
34 #else /* WIN32 */
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
38 #endif /* WIN32 */
39
40 #include <stdlib.h>
41
42 #ifndef WIN32
43 #if __STDC__
44 struct mbuf;
45 struct rtentry;
46 #endif
47
48 #include <netinet/in.h>
49 #endif /* WIN32 */
50
51 #include <stdio.h>
52
53 #include "pcap-int.h"
54
55 #include "gencode.h"
56 #include <pcap-namedb.h>
57
58 #ifdef HAVE_OS_PROTO_H
59 #include "os-proto.h"
60 #endif
61
62 #define QSET(q, p, d, a) (q).proto = (p),\
63 (q).dir = (d),\
64 (q).addr = (a)
65
66 int n_errors = 0;
67
68 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
69
70 static void
71 yyerror(char *msg)
72 {
73 ++n_errors;
74 bpf_error("%s", msg);
75 /* NOTREACHED */
76 }
77
78 #ifndef YYBISON
79 int yyparse(void);
80
81 int
82 pcap_parse()
83 {
84 return (yyparse());
85 }
86 #endif
87
88 %}
89
90 %union {
91 int i;
92 bpf_u_int32 h;
93 u_char *e;
94 char *s;
95 struct stmt *stmt;
96 struct arth *a;
97 struct {
98 struct qual q;
99 int atmfieldtype;
100 struct block *b;
101 } blk;
102 struct block *rblk;
103 }
104
105 %type <blk> expr id nid pid term rterm qid
106 %type <blk> head
107 %type <i> pqual dqual aqual ndaqual
108 %type <a> arth narth
109 %type <i> byteop pname pnum relop irelop
110 %type <blk> and or paren not null prog
111 %type <rblk> other
112 %type <blk> gexpr
113 %type <i> atmtype atmmultitype
114 %type <blk> atmexpr atmfield atmhead ratmhead
115 %type <blk> atmfieldvalue atmvalue atmlistvalue
116
117 %token DST SRC HOST GATEWAY
118 %token NET MASK PORT LESS GREATER PROTO PROTOCHAIN CBYTE
119 %token ARP RARP IP SCTP TCP UDP ICMP IGMP IGRP PIM VRRP
120 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
121 %token TK_BROADCAST TK_MULTICAST
122 %token NUM INBOUND OUTBOUND
123 %token LINK
124 %token GEQ LEQ NEQ
125 %token ID EID HID HID6 AID
126 %token LSH RSH
127 %token LEN
128 %token IPV6 ICMPV6 AH ESP
129 %token VLAN
130 %token ISO ESIS ISIS CLNP
131 %token STP
132 %token IPX
133 %token NETBEUI
134 %token LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
135 %token OAM OAMF4 CONNECTMSG METACONNECT
136 %token VPI VCI
137
138 %type <s> ID
139 %type <e> EID
140 %type <e> AID
141 %type <s> HID HID6
142 %type <i> NUM
143
144 %left OR AND
145 %nonassoc '!'
146 %left '|'
147 %left '&'
148 %left LSH RSH
149 %left '+' '-'
150 %left '*' '/'
151 %nonassoc UMINUS
152 %%
153 prog: null gexpr
154 {
155 finish_parse($2.b);
156 }
157 | null
158 ;
159 null: /* null */ { $$.q = qerr; }
160 ;
161 gexpr: expr
162 | atmexpr
163 ;
164 expr: term
165 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
166 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
167 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
168 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
169 ;
170 and: AND { $$ = $<blk>0; }
171 ;
172 or: OR { $$ = $<blk>0; }
173 ;
174 id: nid
175 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
176 $$.q = $<blk>0.q); }
177 | paren pid ')' { $$ = $2; }
178 ;
179 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
180 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
181 $$.q = $<blk>0.q); }
182 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
183 $$.q = $<blk>0.q); }
184 | HID {
185 /* Decide how to parse HID based on proto */
186 $$.q = $<blk>0.q;
187 $$.b = gen_ncode($1, 0, $$.q);
188 }
189 | HID6 '/' NUM {
190 #ifdef INET6
191 $$.b = gen_mcode6($1, NULL, $3,
192 $$.q = $<blk>0.q);
193 #else
194 bpf_error("'ip6addr/prefixlen' not supported "
195 "in this configuration");
196 #endif /*INET6*/
197 }
198 | HID6 {
199 #ifdef INET6
200 $$.b = gen_mcode6($1, 0, 128,
201 $$.q = $<blk>0.q);
202 #else
203 bpf_error("'ip6addr' not supported "
204 "in this configuration");
205 #endif /*INET6*/
206 }
207 | EID {
208 $$.b = gen_ecode($1, $$.q = $<blk>0.q);
209 /*
210 * $1 was allocated by "pcap_ether_aton()",
211 * so we must free it now that we're done
212 * with it.
213 */
214 free($1);
215 }
216 | AID {
217 $$.b = gen_acode($1, $$.q = $<blk>0.q);
218 /*
219 * $1 was allocated by "pcap_ether_aton()",
220 * so we must free it now that we're done
221 * with it.
222 */
223 free($1);
224 }
225 | not id { gen_not($2.b); $$ = $2; }
226 ;
227 not: '!' { $$ = $<blk>0; }
228 ;
229 paren: '(' { $$ = $<blk>0; }
230 ;
231 pid: nid
232 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
233 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
234 ;
235 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
236 $$.q = $<blk>0.q); }
237 | pid
238 ;
239 term: rterm
240 | not term { gen_not($2.b); $$ = $2; }
241 ;
242 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
243 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
244 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
245 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
246 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
247 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
248 ;
249 rterm: head id { $$ = $2; }
250 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
251 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
252 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
253 $$.q = qerr; }
254 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
255 $$.q = qerr; }
256 | other { $$.b = $1; $$.q = qerr; }
257 ;
258 /* protocol level qualifiers */
259 pqual: pname
260 | { $$ = Q_DEFAULT; }
261 ;
262 /* 'direction' qualifiers */
263 dqual: SRC { $$ = Q_SRC; }
264 | DST { $$ = Q_DST; }
265 | SRC OR DST { $$ = Q_OR; }
266 | DST OR SRC { $$ = Q_OR; }
267 | SRC AND DST { $$ = Q_AND; }
268 | DST AND SRC { $$ = Q_AND; }
269 ;
270 /* address type qualifiers */
271 aqual: HOST { $$ = Q_HOST; }
272 | NET { $$ = Q_NET; }
273 | PORT { $$ = Q_PORT; }
274 ;
275 /* non-directional address type qualifiers */
276 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
277 ;
278 pname: LINK { $$ = Q_LINK; }
279 | IP { $$ = Q_IP; }
280 | ARP { $$ = Q_ARP; }
281 | RARP { $$ = Q_RARP; }
282 | SCTP { $$ = Q_SCTP; }
283 | TCP { $$ = Q_TCP; }
284 | UDP { $$ = Q_UDP; }
285 | ICMP { $$ = Q_ICMP; }
286 | IGMP { $$ = Q_IGMP; }
287 | IGRP { $$ = Q_IGRP; }
288 | PIM { $$ = Q_PIM; }
289 | VRRP { $$ = Q_VRRP; }
290 | ATALK { $$ = Q_ATALK; }
291 | AARP { $$ = Q_AARP; }
292 | DECNET { $$ = Q_DECNET; }
293 | LAT { $$ = Q_LAT; }
294 | SCA { $$ = Q_SCA; }
295 | MOPDL { $$ = Q_MOPDL; }
296 | MOPRC { $$ = Q_MOPRC; }
297 | IPV6 { $$ = Q_IPV6; }
298 | ICMPV6 { $$ = Q_ICMPV6; }
299 | AH { $$ = Q_AH; }
300 | ESP { $$ = Q_ESP; }
301 | ISO { $$ = Q_ISO; }
302 | ESIS { $$ = Q_ESIS; }
303 | ISIS { $$ = Q_ISIS; }
304 | CLNP { $$ = Q_CLNP; }
305 | STP { $$ = Q_STP; }
306 | IPX { $$ = Q_IPX; }
307 | NETBEUI { $$ = Q_NETBEUI; }
308 ;
309 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
310 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
311 | LESS NUM { $$ = gen_less($2); }
312 | GREATER NUM { $$ = gen_greater($2); }
313 | CBYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
314 | INBOUND { $$ = gen_inbound(0); }
315 | OUTBOUND { $$ = gen_inbound(1); }
316 | VLAN pnum { $$ = gen_vlan($2); }
317 | VLAN { $$ = gen_vlan(-1); }
318 ;
319 relop: '>' { $$ = BPF_JGT; }
320 | GEQ { $$ = BPF_JGE; }
321 | '=' { $$ = BPF_JEQ; }
322 ;
323 irelop: LEQ { $$ = BPF_JGT; }
324 | '<' { $$ = BPF_JGE; }
325 | NEQ { $$ = BPF_JEQ; }
326 ;
327 arth: pnum { $$ = gen_loadi($1); }
328 | narth
329 ;
330 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
331 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
332 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
333 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
334 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
335 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
336 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
337 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
338 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
339 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
340 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
341 | paren narth ')' { $$ = $2; }
342 | LEN { $$ = gen_loadlen(); }
343 ;
344 byteop: '&' { $$ = '&'; }
345 | '|' { $$ = '|'; }
346 | '<' { $$ = '<'; }
347 | '>' { $$ = '>'; }
348 | '=' { $$ = '='; }
349 ;
350 pnum: NUM
351 | paren pnum ')' { $$ = $2; }
352 ;
353 atmexpr: atmhead
354 | atmexpr and atmhead { gen_and($1.b, $3.b); $$ = $3; }
355 | atmexpr or atmhead { gen_or($1.b, $3.b); $$ = $3; }
356 ;
357 atmhead: ratmhead
358 | not atmhead { gen_not($2.b); $$ = $2; }
359 ;
360 ratmhead: atmtype { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
361 | atmmultitype { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
362 | atmfield atmvalue { $$.b = $2.b; }
363 | paren atmexpr ')' { $$.b = $2.b; $$.q = qerr; }
364 ;
365 atmtype: LANE { $$ = A_LANE; }
366 | LLC { $$ = A_LLC; }
367 | METAC { $$ = A_METAC; }
368 | BCC { $$ = A_BCC; }
369 | OAMF4EC { $$ = A_OAMF4EC; }
370 | OAMF4SC { $$ = A_OAMF4SC; }
371 | SC { $$ = A_SC; }
372 | ILMIC { $$ = A_ILMIC; }
373 ;
374 atmmultitype: OAM { $$ = A_OAM; }
375 | OAMF4 { $$ = A_OAMF4; }
376 | CONNECTMSG { $$ = A_CONNECTMSG; }
377 | METACONNECT { $$ = A_METACONNECT; }
378 ;
379 /* ATM field types quantifier */
380 atmfield: VPI { $$.atmfieldtype = A_VPI; }
381 | VCI { $$.atmfieldtype = A_VCI; }
382 ;
383 atmvalue: atmfieldvalue
384 | relop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (u_int)$2, (u_int)$1, 0); }
385 | irelop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (u_int)$2, (u_int)$1, 1); }
386 | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
387 ;
388 atmfieldvalue: NUM {
389 $$.atmfieldtype = $<blk>0.atmfieldtype;
390 if ($$.atmfieldtype == A_VPI ||
391 $$.atmfieldtype == A_VCI)
392 $$.b = gen_atmfield_code($$.atmfieldtype, (u_int) $1, BPF_JEQ, 0);
393 }
394 ;
395 atmlistvalue: atmfieldvalue
396 | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
397 ;
398 %%