]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libpcap/grammar.y
This commit was generated by cvs2svn to compensate for changes in r141867,
[FreeBSD/FreeBSD.git] / contrib / 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  * $FreeBSD$
23  */
24 #ifndef lint
25 static const char rcsid[] _U_ =
26     "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.79.2.3 2004/03/28 21:45:32 fenner Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #ifdef WIN32
34 #include <pcap-stdinc.h>
35 #else /* WIN32 */
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/socket.h>
39 #endif /* WIN32 */
40
41 #include <stdlib.h>
42
43 #ifndef WIN32
44 #if __STDC__
45 struct mbuf;
46 struct rtentry;
47 #endif
48
49 #include <netinet/in.h>
50 #endif /* WIN32 */
51
52 #include <stdio.h>
53 #include <strings.h>
54
55 #include "pcap-int.h"
56
57 #include "gencode.h"
58 #include "pf.h"
59 #include <pcap-namedb.h>
60
61 #ifdef HAVE_OS_PROTO_H
62 #include "os-proto.h"
63 #endif
64
65 #define QSET(q, p, d, a) (q).proto = (p),\
66                          (q).dir = (d),\
67                          (q).addr = (a)
68
69 int n_errors = 0;
70
71 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
72
73 static void
74 yyerror(char *msg)
75 {
76         ++n_errors;
77         bpf_error("%s", msg);
78         /* NOTREACHED */
79 }
80
81 #ifndef YYBISON
82 int yyparse(void);
83
84 int
85 pcap_parse()
86 {
87         return (yyparse());
88 }
89 #endif
90
91 %}
92
93 %union {
94         int i;
95         bpf_u_int32 h;
96         u_char *e;
97         char *s;
98         struct stmt *stmt;
99         struct arth *a;
100         struct {
101                 struct qual q;
102                 int atmfieldtype;
103                 struct block *b;
104         } blk;
105         struct block *rblk;
106 }
107
108 %type   <blk>   expr id nid pid term rterm qid
109 %type   <blk>   head
110 %type   <i>     pqual dqual aqual ndaqual
111 %type   <a>     arth narth
112 %type   <i>     byteop pname pnum relop irelop
113 %type   <blk>   and or paren not null prog
114 %type   <rblk>  other pfvar
115 %type   <i>     atmtype atmmultitype
116 %type   <blk>   atmfield
117 %type   <blk>   atmfieldvalue atmvalue atmlistvalue
118
119 %token  DST SRC HOST GATEWAY
120 %token  NET NETMASK PORT LESS GREATER PROTO PROTOCHAIN CBYTE
121 %token  ARP RARP IP SCTP TCP UDP ICMP IGMP IGRP PIM VRRP
122 %token  ATALK AARP DECNET LAT SCA MOPRC MOPDL
123 %token  TK_BROADCAST TK_MULTICAST
124 %token  NUM INBOUND OUTBOUND
125 %token  PF_IFNAME PF_RSET PF_RNR PF_SRNR PF_REASON PF_ACTION
126 %token  LINK
127 %token  GEQ LEQ NEQ
128 %token  ID EID HID HID6 AID
129 %token  LSH RSH
130 %token  LEN
131 %token  IPV6 ICMPV6 AH ESP
132 %token  VLAN
133 %token  ISO ESIS CLNP ISIS L1 L2 IIH LSP SNP CSNP PSNP 
134 %token  STP
135 %token  IPX
136 %token  NETBEUI
137 %token  LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
138 %token  OAM OAMF4 CONNECTMSG METACONNECT
139 %token  VPI VCI
140
141 %type   <s> ID
142 %type   <e> EID
143 %type   <e> AID
144 %type   <s> HID HID6
145 %type   <i> NUM action reason
146
147 %left OR AND
148 %nonassoc  '!'
149 %left '|'
150 %left '&'
151 %left LSH RSH
152 %left '+' '-'
153 %left '*' '/'
154 %nonassoc UMINUS
155 %%
156 prog:     null expr
157 {
158         finish_parse($2.b);
159 }
160         | null
161         ;
162 null:     /* null */            { $$.q = qerr; }
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 NETMASK 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         | atmtype               { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
258         | atmmultitype          { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
259         | atmfield atmvalue     { $$.b = $2.b; $$.q = qerr; }
260         ;
261 /* protocol level qualifiers */
262 pqual:    pname
263         |                       { $$ = Q_DEFAULT; }
264         ;
265 /* 'direction' qualifiers */
266 dqual:    SRC                   { $$ = Q_SRC; }
267         | DST                   { $$ = Q_DST; }
268         | SRC OR DST            { $$ = Q_OR; }
269         | DST OR SRC            { $$ = Q_OR; }
270         | SRC AND DST           { $$ = Q_AND; }
271         | DST AND SRC           { $$ = Q_AND; }
272         ;
273 /* address type qualifiers */
274 aqual:    HOST                  { $$ = Q_HOST; }
275         | NET                   { $$ = Q_NET; }
276         | PORT                  { $$ = Q_PORT; }
277         ;
278 /* non-directional address type qualifiers */
279 ndaqual:  GATEWAY               { $$ = Q_GATEWAY; }
280         ;
281 pname:    LINK                  { $$ = Q_LINK; }
282         | IP                    { $$ = Q_IP; }
283         | ARP                   { $$ = Q_ARP; }
284         | RARP                  { $$ = Q_RARP; }
285         | SCTP                  { $$ = Q_SCTP; }
286         | TCP                   { $$ = Q_TCP; }
287         | UDP                   { $$ = Q_UDP; }
288         | ICMP                  { $$ = Q_ICMP; }
289         | IGMP                  { $$ = Q_IGMP; }
290         | IGRP                  { $$ = Q_IGRP; }
291         | PIM                   { $$ = Q_PIM; }
292         | VRRP                  { $$ = Q_VRRP; }
293         | ATALK                 { $$ = Q_ATALK; }
294         | AARP                  { $$ = Q_AARP; }
295         | DECNET                { $$ = Q_DECNET; }
296         | LAT                   { $$ = Q_LAT; }
297         | SCA                   { $$ = Q_SCA; }
298         | MOPDL                 { $$ = Q_MOPDL; }
299         | MOPRC                 { $$ = Q_MOPRC; }
300         | IPV6                  { $$ = Q_IPV6; }
301         | ICMPV6                { $$ = Q_ICMPV6; }
302         | AH                    { $$ = Q_AH; }
303         | ESP                   { $$ = Q_ESP; }
304         | ISO                   { $$ = Q_ISO; }
305         | ESIS                  { $$ = Q_ESIS; }
306         | ISIS                  { $$ = Q_ISIS; }
307         | L1                    { $$ = Q_ISIS_L1; }
308         | L2                    { $$ = Q_ISIS_L2; }
309         | IIH                   { $$ = Q_ISIS_IIH; }
310         | LSP                   { $$ = Q_ISIS_LSP; }
311         | SNP                   { $$ = Q_ISIS_SNP; }
312         | PSNP                  { $$ = Q_ISIS_PSNP; }
313         | CSNP                  { $$ = Q_ISIS_CSNP; }
314         | CLNP                  { $$ = Q_CLNP; }
315         | STP                   { $$ = Q_STP; }
316         | IPX                   { $$ = Q_IPX; }
317         | NETBEUI               { $$ = Q_NETBEUI; }
318         ;
319 other:    pqual TK_BROADCAST    { $$ = gen_broadcast($1); }
320         | pqual TK_MULTICAST    { $$ = gen_multicast($1); }
321         | LESS NUM              { $$ = gen_less($2); }
322         | GREATER NUM           { $$ = gen_greater($2); }
323         | CBYTE NUM byteop NUM  { $$ = gen_byteop($3, $2, $4); }
324         | INBOUND               { $$ = gen_inbound(0); }
325         | OUTBOUND              { $$ = gen_inbound(1); }
326         | VLAN pnum             { $$ = gen_vlan($2); }
327         | VLAN                  { $$ = gen_vlan(-1); }
328         | pfvar                 { $$ = $1; }
329         ;
330
331 pfvar:    PF_IFNAME ID          { $$ = gen_pf_ifname($2); }
332         | PF_RSET ID            { $$ = gen_pf_ruleset($2); }
333         | PF_RNR NUM            { $$ = gen_pf_rnr($2); }
334         | PF_SRNR NUM           { $$ = gen_pf_srnr($2); }
335         | PF_REASON reason      { $$ = gen_pf_reason($2); }
336         | PF_ACTION action      { $$ = gen_pf_action($2); }
337         ;
338
339 reason:   NUM                   { $$ = $1; }
340         | ID                    { const char *reasons[] = PFRES_NAMES;
341                                   int i;
342                                   for (i = 0; reasons[i]; i++) {
343                                           if (pcap_strcasecmp($1, reasons[i]) == 0) {
344                                                   $$ = i;
345                                                   break;
346                                           }
347                                   }
348                                   if (reasons[i] == NULL)
349                                           bpf_error("unknown PF reason");
350                                 }
351         ;
352
353 action:   ID                    { if (pcap_strcasecmp($1, "pass") == 0 ||
354                                       pcap_strcasecmp($1, "accept") == 0)
355                                         $$ = PF_PASS;
356                                   else if (pcap_strcasecmp($1, "drop") == 0 ||
357                                       pcap_strcasecmp($1, "block") == 0)
358                                         $$ = PF_DROP;
359                                   else
360                                           bpf_error("unknown PF action");
361                                 }
362         ;
363
364 relop:    '>'                   { $$ = BPF_JGT; }
365         | GEQ                   { $$ = BPF_JGE; }
366         | '='                   { $$ = BPF_JEQ; }
367         ;
368 irelop:   LEQ                   { $$ = BPF_JGT; }
369         | '<'                   { $$ = BPF_JGE; }
370         | NEQ                   { $$ = BPF_JEQ; }
371         ;
372 arth:     pnum                  { $$ = gen_loadi($1); }
373         | narth
374         ;
375 narth:    pname '[' arth ']'            { $$ = gen_load($1, $3, 1); }
376         | pname '[' arth ':' NUM ']'    { $$ = gen_load($1, $3, $5); }
377         | arth '+' arth                 { $$ = gen_arth(BPF_ADD, $1, $3); }
378         | arth '-' arth                 { $$ = gen_arth(BPF_SUB, $1, $3); }
379         | arth '*' arth                 { $$ = gen_arth(BPF_MUL, $1, $3); }
380         | arth '/' arth                 { $$ = gen_arth(BPF_DIV, $1, $3); }
381         | arth '&' arth                 { $$ = gen_arth(BPF_AND, $1, $3); }
382         | arth '|' arth                 { $$ = gen_arth(BPF_OR, $1, $3); }
383         | arth LSH arth                 { $$ = gen_arth(BPF_LSH, $1, $3); }
384         | arth RSH arth                 { $$ = gen_arth(BPF_RSH, $1, $3); }
385         | '-' arth %prec UMINUS         { $$ = gen_neg($2); }
386         | paren narth ')'               { $$ = $2; }
387         | LEN                           { $$ = gen_loadlen(); }
388         ;
389 byteop:   '&'                   { $$ = '&'; }
390         | '|'                   { $$ = '|'; }
391         | '<'                   { $$ = '<'; }
392         | '>'                   { $$ = '>'; }
393         | '='                   { $$ = '='; }
394         ;
395 pnum:     NUM
396         | paren pnum ')'        { $$ = $2; }
397         ;
398 atmtype: LANE                   { $$ = A_LANE; }
399         | LLC                   { $$ = A_LLC; }
400         | METAC                 { $$ = A_METAC; }
401         | BCC                   { $$ = A_BCC; }
402         | OAMF4EC               { $$ = A_OAMF4EC; }
403         | OAMF4SC               { $$ = A_OAMF4SC; }
404         | SC                    { $$ = A_SC; }
405         | ILMIC                 { $$ = A_ILMIC; }
406         ;
407 atmmultitype: OAM               { $$ = A_OAM; }
408         | OAMF4                 { $$ = A_OAMF4; }
409         | CONNECTMSG            { $$ = A_CONNECTMSG; }
410         | METACONNECT           { $$ = A_METACONNECT; }
411         ;
412         /* ATM field types quantifier */
413 atmfield: VPI                   { $$.atmfieldtype = A_VPI; }
414         | VCI                   { $$.atmfieldtype = A_VCI; }
415         ;
416 atmvalue: atmfieldvalue
417         | relop NUM             { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (u_int)$2, (u_int)$1, 0); }
418         | irelop NUM            { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (u_int)$2, (u_int)$1, 1); }
419         | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
420         ;
421 atmfieldvalue: NUM {
422         $$.atmfieldtype = $<blk>0.atmfieldtype;
423         if ($$.atmfieldtype == A_VPI ||
424             $$.atmfieldtype == A_VCI)
425                 $$.b = gen_atmfield_code($$.atmfieldtype, (u_int) $1, BPF_JEQ, 0);
426         }
427         ;
428 atmlistvalue: atmfieldvalue
429         | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
430         ;
431 %%