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