Merge from vendor branch LESS:
[dragonfly.git] / contrib / libpcap-0.9 / 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.86.2.8 2007/06/11 09:52:04 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(const 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 LSSU 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                                   $$.b = gen_ncode($1, 0, $$.q);
194                                 }
195         | HID6 '/' NUM          {
196 #ifdef INET6
197                                   $$.b = gen_mcode6($1, NULL, $3,
198                                     $$.q = $<blk>0.q);
199 #else
200                                   bpf_error("'ip6addr/prefixlen' not supported "
201                                         "in this configuration");
202 #endif /*INET6*/
203                                 }
204         | HID6                  {
205 #ifdef INET6
206                                   $$.b = gen_mcode6($1, 0, 128,
207                                     $$.q = $<blk>0.q);
208 #else
209                                   bpf_error("'ip6addr' not supported "
210                                         "in this configuration");
211 #endif /*INET6*/
212                                 }
213         | EID                   { 
214                                   $$.b = gen_ecode($1, $$.q = $<blk>0.q);
215                                   /*
216                                    * $1 was allocated by "pcap_ether_aton()",
217                                    * so we must free it now that we're done
218                                    * with it.
219                                    */
220                                   free($1);
221                                 }
222         | AID                   {
223                                   $$.b = gen_acode($1, $$.q = $<blk>0.q);
224                                   /*
225                                    * $1 was allocated by "pcap_ether_aton()",
226                                    * so we must free it now that we're done
227                                    * with it.
228                                    */
229                                   free($1);
230                                 }
231         | not id                { gen_not($2.b); $$ = $2; }
232         ;
233 not:      '!'                   { $$ = $<blk>0; }
234         ;
235 paren:    '('                   { $$ = $<blk>0; }
236         ;
237 pid:      nid
238         | qid and id            { gen_and($1.b, $3.b); $$ = $3; }
239         | qid or id             { gen_or($1.b, $3.b); $$ = $3; }
240         ;
241 qid:      pnum                  { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
242                                                    $$.q = $<blk>0.q); }
243         | pid
244         ;
245 term:     rterm
246         | not term              { gen_not($2.b); $$ = $2; }
247         ;
248 head:     pqual dqual aqual     { QSET($$.q, $1, $2, $3); }
249         | pqual dqual           { QSET($$.q, $1, $2, Q_DEFAULT); }
250         | pqual aqual           { QSET($$.q, $1, Q_DEFAULT, $2); }
251         | pqual PROTO           { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
252         | pqual PROTOCHAIN      { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
253         | pqual ndaqual         { QSET($$.q, $1, Q_DEFAULT, $2); }
254         ;
255 rterm:    head id               { $$ = $2; }
256         | paren expr ')'        { $$.b = $2.b; $$.q = $1.q; }
257         | pname                 { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
258         | arth relop arth       { $$.b = gen_relation($2, $1, $3, 0);
259                                   $$.q = qerr; }
260         | arth irelop arth      { $$.b = gen_relation($2, $1, $3, 1);
261                                   $$.q = qerr; }
262         | other                 { $$.b = $1; $$.q = qerr; }
263         | atmtype               { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
264         | atmmultitype          { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
265         | atmfield atmvalue     { $$.b = $2.b; $$.q = qerr; }
266         | mtp2type              { $$.b = gen_mtp2type_abbrev($1); $$.q = qerr; }
267         | mtp3field mtp3value   { $$.b = $2.b; $$.q = qerr; }
268         ;
269 /* protocol level qualifiers */
270 pqual:    pname
271         |                       { $$ = Q_DEFAULT; }
272         ;
273 /* 'direction' qualifiers */
274 dqual:    SRC                   { $$ = Q_SRC; }
275         | DST                   { $$ = Q_DST; }
276         | SRC OR DST            { $$ = Q_OR; }
277         | DST OR SRC            { $$ = Q_OR; }
278         | SRC AND DST           { $$ = Q_AND; }
279         | DST AND SRC           { $$ = Q_AND; }
280         ;
281 /* address type qualifiers */
282 aqual:    HOST                  { $$ = Q_HOST; }
283         | NET                   { $$ = Q_NET; }
284         | PORT                  { $$ = Q_PORT; }
285         | PORTRANGE             { $$ = Q_PORTRANGE; }
286         ;
287 /* non-directional address type qualifiers */
288 ndaqual:  GATEWAY               { $$ = Q_GATEWAY; }
289         ;
290 pname:    LINK                  { $$ = Q_LINK; }
291         | IP                    { $$ = Q_IP; }
292         | ARP                   { $$ = Q_ARP; }
293         | RARP                  { $$ = Q_RARP; }
294         | SCTP                  { $$ = Q_SCTP; }
295         | TCP                   { $$ = Q_TCP; }
296         | UDP                   { $$ = Q_UDP; }
297         | ICMP                  { $$ = Q_ICMP; }
298         | IGMP                  { $$ = Q_IGMP; }
299         | IGRP                  { $$ = Q_IGRP; }
300         | PIM                   { $$ = Q_PIM; }
301         | VRRP                  { $$ = Q_VRRP; }
302         | ATALK                 { $$ = Q_ATALK; }
303         | AARP                  { $$ = Q_AARP; }
304         | DECNET                { $$ = Q_DECNET; }
305         | LAT                   { $$ = Q_LAT; }
306         | SCA                   { $$ = Q_SCA; }
307         | MOPDL                 { $$ = Q_MOPDL; }
308         | MOPRC                 { $$ = Q_MOPRC; }
309         | IPV6                  { $$ = Q_IPV6; }
310         | ICMPV6                { $$ = Q_ICMPV6; }
311         | AH                    { $$ = Q_AH; }
312         | ESP                   { $$ = Q_ESP; }
313         | ISO                   { $$ = Q_ISO; }
314         | ESIS                  { $$ = Q_ESIS; }
315         | ISIS                  { $$ = Q_ISIS; }
316         | L1                    { $$ = Q_ISIS_L1; }
317         | L2                    { $$ = Q_ISIS_L2; }
318         | IIH                   { $$ = Q_ISIS_IIH; }
319         | LSP                   { $$ = Q_ISIS_LSP; }
320         | SNP                   { $$ = Q_ISIS_SNP; }
321         | PSNP                  { $$ = Q_ISIS_PSNP; }
322         | CSNP                  { $$ = Q_ISIS_CSNP; }
323         | CLNP                  { $$ = Q_CLNP; }
324         | STP                   { $$ = Q_STP; }
325         | IPX                   { $$ = Q_IPX; }
326         | NETBEUI               { $$ = Q_NETBEUI; }
327         | RADIO                 { $$ = Q_RADIO; }
328         ;
329 other:    pqual TK_BROADCAST    { $$ = gen_broadcast($1); }
330         | pqual TK_MULTICAST    { $$ = gen_multicast($1); }
331         | LESS NUM              { $$ = gen_less($2); }
332         | GREATER NUM           { $$ = gen_greater($2); }
333         | CBYTE NUM byteop NUM  { $$ = gen_byteop($3, $2, $4); }
334         | INBOUND               { $$ = gen_inbound(0); }
335         | OUTBOUND              { $$ = gen_inbound(1); }
336         | VLAN pnum             { $$ = gen_vlan($2); }
337         | VLAN                  { $$ = gen_vlan(-1); }
338         | MPLS pnum             { $$ = gen_mpls($2); }
339         | MPLS                  { $$ = gen_mpls(-1); }
340         | PPPOED                { $$ = gen_pppoed(); }
341         | PPPOES                { $$ = gen_pppoes(); }
342         | pfvar                 { $$ = $1; }
343         ;
344
345 pfvar:    PF_IFNAME ID          { $$ = gen_pf_ifname($2); }
346         | PF_RSET ID            { $$ = gen_pf_ruleset($2); }
347         | PF_RNR NUM            { $$ = gen_pf_rnr($2); }
348         | PF_SRNR NUM           { $$ = gen_pf_srnr($2); }
349         | PF_REASON reason      { $$ = gen_pf_reason($2); }
350         | PF_ACTION action      { $$ = gen_pf_action($2); }
351         ;
352
353 reason:   NUM                   { $$ = $1; }
354         | ID                    { const char *reasons[] = PFRES_NAMES;
355                                   int i;
356                                   for (i = 0; reasons[i]; i++) {
357                                           if (pcap_strcasecmp($1, reasons[i]) == 0) {
358                                                   $$ = i;
359                                                   break;
360                                           }
361                                   }
362                                   if (reasons[i] == NULL)
363                                           bpf_error("unknown PF reason");
364                                 }
365         ;
366
367 action:   ID                    { if (pcap_strcasecmp($1, "pass") == 0 ||
368                                       pcap_strcasecmp($1, "accept") == 0)
369                                         $$ = PF_PASS;
370                                   else if (pcap_strcasecmp($1, "drop") == 0 ||
371                                       pcap_strcasecmp($1, "block") == 0)
372                                         $$ = PF_DROP;
373                                   else
374                                           bpf_error("unknown PF action");
375                                 }
376         ;
377
378 relop:    '>'                   { $$ = BPF_JGT; }
379         | GEQ                   { $$ = BPF_JGE; }
380         | '='                   { $$ = BPF_JEQ; }
381         ;
382 irelop:   LEQ                   { $$ = BPF_JGT; }
383         | '<'                   { $$ = BPF_JGE; }
384         | NEQ                   { $$ = BPF_JEQ; }
385         ;
386 arth:     pnum                  { $$ = gen_loadi($1); }
387         | narth
388         ;
389 narth:    pname '[' arth ']'            { $$ = gen_load($1, $3, 1); }
390         | pname '[' arth ':' NUM ']'    { $$ = gen_load($1, $3, $5); }
391         | arth '+' arth                 { $$ = gen_arth(BPF_ADD, $1, $3); }
392         | arth '-' arth                 { $$ = gen_arth(BPF_SUB, $1, $3); }
393         | arth '*' arth                 { $$ = gen_arth(BPF_MUL, $1, $3); }
394         | arth '/' arth                 { $$ = gen_arth(BPF_DIV, $1, $3); }
395         | arth '&' arth                 { $$ = gen_arth(BPF_AND, $1, $3); }
396         | arth '|' arth                 { $$ = gen_arth(BPF_OR, $1, $3); }
397         | arth LSH arth                 { $$ = gen_arth(BPF_LSH, $1, $3); }
398         | arth RSH arth                 { $$ = gen_arth(BPF_RSH, $1, $3); }
399         | '-' arth %prec UMINUS         { $$ = gen_neg($2); }
400         | paren narth ')'               { $$ = $2; }
401         | LEN                           { $$ = gen_loadlen(); }
402         ;
403 byteop:   '&'                   { $$ = '&'; }
404         | '|'                   { $$ = '|'; }
405         | '<'                   { $$ = '<'; }
406         | '>'                   { $$ = '>'; }
407         | '='                   { $$ = '='; }
408         ;
409 pnum:     NUM
410         | paren pnum ')'        { $$ = $2; }
411         ;
412 atmtype: LANE                   { $$ = A_LANE; }
413         | LLC                   { $$ = A_LLC; }
414         | METAC                 { $$ = A_METAC; }
415         | BCC                   { $$ = A_BCC; }
416         | OAMF4EC               { $$ = A_OAMF4EC; }
417         | OAMF4SC               { $$ = A_OAMF4SC; }
418         | SC                    { $$ = A_SC; }
419         | ILMIC                 { $$ = A_ILMIC; }
420         ;
421 atmmultitype: OAM               { $$ = A_OAM; }
422         | OAMF4                 { $$ = A_OAMF4; }
423         | CONNECTMSG            { $$ = A_CONNECTMSG; }
424         | METACONNECT           { $$ = A_METACONNECT; }
425         ;
426         /* ATM field types quantifier */
427 atmfield: VPI                   { $$.atmfieldtype = A_VPI; }
428         | VCI                   { $$.atmfieldtype = A_VCI; }
429         ;
430 atmvalue: atmfieldvalue
431         | relop NUM             { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 0); }
432         | irelop NUM            { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 1); }
433         | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
434         ;
435 atmfieldvalue: NUM {
436         $$.atmfieldtype = $<blk>0.atmfieldtype;
437         if ($$.atmfieldtype == A_VPI ||
438             $$.atmfieldtype == A_VCI)
439                 $$.b = gen_atmfield_code($$.atmfieldtype, (bpf_int32) $1, BPF_JEQ, 0);
440         }
441         ;
442 atmlistvalue: atmfieldvalue
443         | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
444         ;
445         /* MTP2 types quantifier */
446 mtp2type: FISU                  { $$ = M_FISU; }
447         | LSSU                  { $$ = M_LSSU; }
448         | MSU                   { $$ = M_MSU; }
449         ;
450         /* MTP3 field types quantifier */
451 mtp3field: SIO                  { $$.mtp3fieldtype = M_SIO; }
452         | OPC                   { $$.mtp3fieldtype = M_OPC; }
453         | DPC                   { $$.mtp3fieldtype = M_DPC; }
454         | SLS                   { $$.mtp3fieldtype = M_SLS; }
455         ;
456 mtp3value: mtp3fieldvalue
457         | relop NUM             { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 0); }
458         | irelop NUM            { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 1); }
459         | paren mtp3listvalue ')' { $$.b = $2.b; $$.q = qerr; }
460         ;
461 mtp3fieldvalue: NUM {
462         $$.mtp3fieldtype = $<blk>0.mtp3fieldtype;
463         if ($$.mtp3fieldtype == M_SIO ||
464             $$.mtp3fieldtype == M_OPC ||
465             $$.mtp3fieldtype == M_DPC ||
466             $$.mtp3fieldtype == M_SLS )
467                 $$.b = gen_mtp3field_code($$.mtp3fieldtype, (u_int) $1, BPF_JEQ, 0);
468         }
469         ;
470 mtp3listvalue: mtp3fieldvalue
471         | mtp3listvalue or mtp3fieldvalue { gen_or($1.b, $3.b); $$ = $3; }
472         ;
473 %%