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