]> The Tcpdump Group git mirrors - libpcap/blob - grammar.y
From Stephen Donnelly: change to handle some name changes in the DAG
[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[] _U_ =
25 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.94 2006-12-21 19:44:06 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/socket.h>
37 #endif /* WIN32 */
38
39 #include <stdlib.h>
40
41 #ifndef WIN32
42 #if __STDC__
43 struct mbuf;
44 struct rtentry;
45 #endif
46
47 #include <netinet/in.h>
48 #endif /* WIN32 */
49
50 #include <stdio.h>
51
52 #include "pcap-int.h"
53
54 #include "gencode.h"
55 #include "pf.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 int mtp3fieldtype;
101 struct block *b;
102 } blk;
103 struct block *rblk;
104 }
105
106 %type <blk> expr id nid pid term rterm qid
107 %type <blk> head
108 %type <i> pqual dqual aqual ndaqual
109 %type <a> arth narth
110 %type <i> byteop pname pnum relop irelop
111 %type <blk> and or paren not null prog
112 %type <rblk> other pfvar
113 %type <i> atmtype atmmultitype
114 %type <blk> atmfield
115 %type <blk> atmfieldvalue atmvalue atmlistvalue
116 %type <i> mtp2type
117 %type <blk> mtp3field
118 %type <blk> mtp3fieldvalue mtp3value mtp3listvalue
119
120
121 %token DST SRC HOST GATEWAY
122 %token NET NETMASK PORT PORTRANGE LESS GREATER PROTO PROTOCHAIN CBYTE
123 %token ARP RARP IP SCTP TCP UDP ICMP IGMP IGRP PIM VRRP
124 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
125 %token TK_BROADCAST TK_MULTICAST
126 %token NUM INBOUND OUTBOUND
127 %token PF_IFNAME PF_RSET PF_RNR PF_SRNR PF_REASON PF_ACTION
128 %token LINK
129 %token GEQ LEQ NEQ
130 %token ID EID HID HID6 AID
131 %token LSH RSH
132 %token LEN
133 %token IPV6 ICMPV6 AH ESP
134 %token VLAN MPLS
135 %token PPPOED PPPOES
136 %token ISO ESIS CLNP ISIS L1 L2 IIH LSP SNP CSNP PSNP
137 %token STP
138 %token IPX
139 %token NETBEUI
140 %token LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
141 %token OAM OAMF4 CONNECTMSG METACONNECT
142 %token VPI VCI
143 %token RADIO
144 %token FISU LSU MSU
145 %token SIO OPC DPC SLS
146
147 %type <s> ID
148 %type <e> EID
149 %type <e> AID
150 %type <s> HID HID6
151 %type <i> NUM action reason
152
153 %left OR AND
154 %nonassoc '!'
155 %left '|'
156 %left '&'
157 %left LSH RSH
158 %left '+' '-'
159 %left '*' '/'
160 %nonassoc UMINUS
161 %%
162 prog: null expr
163 {
164 finish_parse($2.b);
165 }
166 | null
167 ;
168 null: /* null */ { $$.q = qerr; }
169 ;
170 expr: term
171 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
172 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
173 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
174 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
175 ;
176 and: AND { $$ = $<blk>0; }
177 ;
178 or: OR { $$ = $<blk>0; }
179 ;
180 id: nid
181 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
182 $$.q = $<blk>0.q); }
183 | paren pid ')' { $$ = $2; }
184 ;
185 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
186 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
187 $$.q = $<blk>0.q); }
188 | HID NETMASK HID { $$.b = gen_mcode($1, $3, 0,
189 $$.q = $<blk>0.q); }
190 | HID {
191 /* Decide how to parse HID based on proto */
192 $$.q = $<blk>0.q;
193 if ($$.q.addr == Q_PORT)
194 bpf_error("'port' modifier applied to ip host");
195 else if ($$.q.addr == Q_PORTRANGE)
196 bpf_error("'portrange' modifier applied to ip host");
197 else if ($$.q.addr == Q_PROTO)
198 bpf_error("'proto' modifier applied to ip host");
199 else if ($$.q.addr == Q_PROTOCHAIN)
200 bpf_error("'protochain' modifier applied to ip host");
201 $$.b = gen_ncode($1, 0, $$.q);
202 }
203 | HID6 '/' NUM {
204 #ifdef INET6
205 $$.b = gen_mcode6($1, NULL, $3,
206 $$.q = $<blk>0.q);
207 #else
208 bpf_error("'ip6addr/prefixlen' not supported "
209 "in this configuration");
210 #endif /*INET6*/
211 }
212 | HID6 {
213 #ifdef INET6
214 $$.b = gen_mcode6($1, 0, 128,
215 $$.q = $<blk>0.q);
216 #else
217 bpf_error("'ip6addr' not supported "
218 "in this configuration");
219 #endif /*INET6*/
220 }
221 | EID {
222 $$.b = gen_ecode($1, $$.q = $<blk>0.q);
223 /*
224 * $1 was allocated by "pcap_ether_aton()",
225 * so we must free it now that we're done
226 * with it.
227 */
228 free($1);
229 }
230 | AID {
231 $$.b = gen_acode($1, $$.q = $<blk>0.q);
232 /*
233 * $1 was allocated by "pcap_ether_aton()",
234 * so we must free it now that we're done
235 * with it.
236 */
237 free($1);
238 }
239 | not id { gen_not($2.b); $$ = $2; }
240 ;
241 not: '!' { $$ = $<blk>0; }
242 ;
243 paren: '(' { $$ = $<blk>0; }
244 ;
245 pid: nid
246 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
247 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
248 ;
249 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
250 $$.q = $<blk>0.q); }
251 | pid
252 ;
253 term: rterm
254 | not term { gen_not($2.b); $$ = $2; }
255 ;
256 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
257 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
258 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
259 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
260 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
261 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
262 ;
263 rterm: head id { $$ = $2; }
264 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
265 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
266 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
267 $$.q = qerr; }
268 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
269 $$.q = qerr; }
270 | other { $$.b = $1; $$.q = qerr; }
271 | atmtype { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
272 | atmmultitype { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
273 | atmfield atmvalue { $$.b = $2.b; $$.q = qerr; }
274 | mtp2type { $$.b = gen_mtp2type_abbrev($1); $$.q = qerr; }
275 | mtp3field mtp3value { $$.b = $2.b; $$.q = qerr; }
276 ;
277 /* protocol level qualifiers */
278 pqual: pname
279 | { $$ = Q_DEFAULT; }
280 ;
281 /* 'direction' qualifiers */
282 dqual: SRC { $$ = Q_SRC; }
283 | DST { $$ = Q_DST; }
284 | SRC OR DST { $$ = Q_OR; }
285 | DST OR SRC { $$ = Q_OR; }
286 | SRC AND DST { $$ = Q_AND; }
287 | DST AND SRC { $$ = Q_AND; }
288 ;
289 /* address type qualifiers */
290 aqual: HOST { $$ = Q_HOST; }
291 | NET { $$ = Q_NET; }
292 | PORT { $$ = Q_PORT; }
293 | PORTRANGE { $$ = Q_PORTRANGE; }
294 ;
295 /* non-directional address type qualifiers */
296 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
297 ;
298 pname: LINK { $$ = Q_LINK; }
299 | IP { $$ = Q_IP; }
300 | ARP { $$ = Q_ARP; }
301 | RARP { $$ = Q_RARP; }
302 | SCTP { $$ = Q_SCTP; }
303 | TCP { $$ = Q_TCP; }
304 | UDP { $$ = Q_UDP; }
305 | ICMP { $$ = Q_ICMP; }
306 | IGMP { $$ = Q_IGMP; }
307 | IGRP { $$ = Q_IGRP; }
308 | PIM { $$ = Q_PIM; }
309 | VRRP { $$ = Q_VRRP; }
310 | ATALK { $$ = Q_ATALK; }
311 | AARP { $$ = Q_AARP; }
312 | DECNET { $$ = Q_DECNET; }
313 | LAT { $$ = Q_LAT; }
314 | SCA { $$ = Q_SCA; }
315 | MOPDL { $$ = Q_MOPDL; }
316 | MOPRC { $$ = Q_MOPRC; }
317 | IPV6 { $$ = Q_IPV6; }
318 | ICMPV6 { $$ = Q_ICMPV6; }
319 | AH { $$ = Q_AH; }
320 | ESP { $$ = Q_ESP; }
321 | ISO { $$ = Q_ISO; }
322 | ESIS { $$ = Q_ESIS; }
323 | ISIS { $$ = Q_ISIS; }
324 | L1 { $$ = Q_ISIS_L1; }
325 | L2 { $$ = Q_ISIS_L2; }
326 | IIH { $$ = Q_ISIS_IIH; }
327 | LSP { $$ = Q_ISIS_LSP; }
328 | SNP { $$ = Q_ISIS_SNP; }
329 | PSNP { $$ = Q_ISIS_PSNP; }
330 | CSNP { $$ = Q_ISIS_CSNP; }
331 | CLNP { $$ = Q_CLNP; }
332 | STP { $$ = Q_STP; }
333 | IPX { $$ = Q_IPX; }
334 | NETBEUI { $$ = Q_NETBEUI; }
335 | RADIO { $$ = Q_RADIO; }
336 ;
337 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
338 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
339 | LESS NUM { $$ = gen_less($2); }
340 | GREATER NUM { $$ = gen_greater($2); }
341 | CBYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
342 | INBOUND { $$ = gen_inbound(0); }
343 | OUTBOUND { $$ = gen_inbound(1); }
344 | VLAN pnum { $$ = gen_vlan($2); }
345 | VLAN { $$ = gen_vlan(-1); }
346 | MPLS pnum { $$ = gen_mpls($2); }
347 | MPLS { $$ = gen_mpls(-1); }
348 | PPPOED { $$ = gen_pppoed(); }
349 | PPPOES { $$ = gen_pppoes(); }
350 | pfvar { $$ = $1; }
351 ;
352
353 pfvar: PF_IFNAME ID { $$ = gen_pf_ifname($2); }
354 | PF_RSET ID { $$ = gen_pf_ruleset($2); }
355 | PF_RNR NUM { $$ = gen_pf_rnr($2); }
356 | PF_SRNR NUM { $$ = gen_pf_srnr($2); }
357 | PF_REASON reason { $$ = gen_pf_reason($2); }
358 | PF_ACTION action { $$ = gen_pf_action($2); }
359 ;
360
361 reason: NUM { $$ = $1; }
362 | ID { const char *reasons[] = PFRES_NAMES;
363 int i;
364 for (i = 0; reasons[i]; i++) {
365 if (pcap_strcasecmp($1, reasons[i]) == 0) {
366 $$ = i;
367 break;
368 }
369 }
370 if (reasons[i] == NULL)
371 bpf_error("unknown PF reason");
372 }
373 ;
374
375 action: ID { if (pcap_strcasecmp($1, "pass") == 0 ||
376 pcap_strcasecmp($1, "accept") == 0)
377 $$ = PF_PASS;
378 else if (pcap_strcasecmp($1, "drop") == 0 ||
379 pcap_strcasecmp($1, "block") == 0)
380 $$ = PF_DROP;
381 else
382 bpf_error("unknown PF action");
383 }
384 ;
385
386 relop: '>' { $$ = BPF_JGT; }
387 | GEQ { $$ = BPF_JGE; }
388 | '=' { $$ = BPF_JEQ; }
389 ;
390 irelop: LEQ { $$ = BPF_JGT; }
391 | '<' { $$ = BPF_JGE; }
392 | NEQ { $$ = BPF_JEQ; }
393 ;
394 arth: pnum { $$ = gen_loadi($1); }
395 | narth
396 ;
397 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
398 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
399 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
400 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
401 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
402 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
403 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
404 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
405 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
406 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
407 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
408 | paren narth ')' { $$ = $2; }
409 | LEN { $$ = gen_loadlen(); }
410 ;
411 byteop: '&' { $$ = '&'; }
412 | '|' { $$ = '|'; }
413 | '<' { $$ = '<'; }
414 | '>' { $$ = '>'; }
415 | '=' { $$ = '='; }
416 ;
417 pnum: NUM
418 | paren pnum ')' { $$ = $2; }
419 ;
420 atmtype: LANE { $$ = A_LANE; }
421 | LLC { $$ = A_LLC; }
422 | METAC { $$ = A_METAC; }
423 | BCC { $$ = A_BCC; }
424 | OAMF4EC { $$ = A_OAMF4EC; }
425 | OAMF4SC { $$ = A_OAMF4SC; }
426 | SC { $$ = A_SC; }
427 | ILMIC { $$ = A_ILMIC; }
428 ;
429 atmmultitype: OAM { $$ = A_OAM; }
430 | OAMF4 { $$ = A_OAMF4; }
431 | CONNECTMSG { $$ = A_CONNECTMSG; }
432 | METACONNECT { $$ = A_METACONNECT; }
433 ;
434 /* ATM field types quantifier */
435 atmfield: VPI { $$.atmfieldtype = A_VPI; }
436 | VCI { $$.atmfieldtype = A_VCI; }
437 ;
438 atmvalue: atmfieldvalue
439 | relop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 0); }
440 | irelop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 1); }
441 | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
442 ;
443 atmfieldvalue: NUM {
444 $$.atmfieldtype = $<blk>0.atmfieldtype;
445 if ($$.atmfieldtype == A_VPI ||
446 $$.atmfieldtype == A_VCI)
447 $$.b = gen_atmfield_code($$.atmfieldtype, (bpf_int32) $1, BPF_JEQ, 0);
448 }
449 ;
450 atmlistvalue: atmfieldvalue
451 | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
452 ;
453 /* MTP2 types quantifier */
454 mtp2type: FISU { $$ = M_FISU; }
455 | LSU { $$ = M_LSU; }
456 | MSU { $$ = M_MSU; }
457 ;
458 /* MTP3 field types quantifier */
459 mtp3field: SIO { $$.mtp3fieldtype = M_SIO; }
460 | OPC { $$.mtp3fieldtype = M_OPC; }
461 | DPC { $$.mtp3fieldtype = M_DPC; }
462 | SLS { $$.mtp3fieldtype = M_SLS; }
463 ;
464 mtp3value: mtp3fieldvalue
465 | relop NUM { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 0); }
466 | irelop NUM { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 1); }
467 | paren mtp3listvalue ')' { $$.b = $2.b; $$.q = qerr; }
468 ;
469 mtp3fieldvalue: NUM {
470 $$.mtp3fieldtype = $<blk>0.mtp3fieldtype;
471 if ($$.mtp3fieldtype == M_SIO ||
472 $$.mtp3fieldtype == M_OPC ||
473 $$.mtp3fieldtype == M_DPC ||
474 $$.mtp3fieldtype == M_SLS )
475 $$.b = gen_mtp3field_code($$.mtp3fieldtype, (u_int) $1, BPF_JEQ, 0);
476 }
477 ;
478 mtp3listvalue: mtp3fieldvalue
479 | mtp3listvalue or mtp3fieldvalue { gen_or($1.b, $3.b); $$ = $3; }
480 ;
481 %%