]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libpcap/grammar.y
This commit was generated by cvs2svn to compensate for changes in r95908,
[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.64 2000/10/28 10:18:40 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 TCP UDP ICMP IGMP IGRP PIM
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
117 %token  LSH RSH
118 %token  LEN
119 %token  IPV6 ICMPV6 AH ESP
120 %token  VLAN
121 %token  ISO ESIS ISIS CLNP
122
123 %type   <s> ID
124 %type   <e> EID
125 %type   <s> HID HID6
126 %type   <i> NUM
127
128 %left OR AND
129 %nonassoc  '!'
130 %left '|'
131 %left '&'
132 %left LSH RSH
133 %left '+' '-'
134 %left '*' '/'
135 %nonassoc UMINUS
136 %%
137 prog:     null expr
138 {
139         finish_parse($2.b);
140 }
141         | null
142         ;
143 null:     /* null */            { $$.q = qerr; }
144         ;
145 expr:     term
146         | expr and term         { gen_and($1.b, $3.b); $$ = $3; }
147         | expr and id           { gen_and($1.b, $3.b); $$ = $3; }
148         | expr or term          { gen_or($1.b, $3.b); $$ = $3; }
149         | expr or id            { gen_or($1.b, $3.b); $$ = $3; }
150         ;
151 and:      AND                   { $$ = $<blk>0; }
152         ;
153 or:       OR                    { $$ = $<blk>0; }
154         ;
155 id:       nid
156         | pnum                  { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
157                                                    $$.q = $<blk>0.q); }
158         | paren pid ')'         { $$ = $2; }
159         ;
160 nid:      ID                    { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
161         | HID '/' NUM           { $$.b = gen_mcode($1, NULL, $3,
162                                     $$.q = $<blk>0.q); }
163         | HID MASK HID          { $$.b = gen_mcode($1, $3, 0,
164                                     $$.q = $<blk>0.q); }
165         | HID                   {
166                                   /* Decide how to parse HID based on proto */
167                                   $$.q = $<blk>0.q;
168                                   $$.b = gen_ncode($1, 0, $$.q);
169                                 }
170         | HID6 '/' NUM          {
171 #ifdef INET6
172                                   $$.b = gen_mcode6($1, NULL, $3,
173                                     $$.q = $<blk>0.q);
174 #else
175                                   bpf_error("'ip6addr/prefixlen' not supported "
176                                         "in this configuration");
177 #endif /*INET6*/
178                                 }
179         | HID6                  {
180 #ifdef INET6
181                                   $$.b = gen_mcode6($1, 0, 128,
182                                     $$.q = $<blk>0.q);
183 #else
184                                   bpf_error("'ip6addr' not supported "
185                                         "in this configuration");
186 #endif /*INET6*/
187                                 }
188         | EID                   { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
189         | not id                { gen_not($2.b); $$ = $2; }
190         ;
191 not:      '!'                   { $$ = $<blk>0; }
192         ;
193 paren:    '('                   { $$ = $<blk>0; }
194         ;
195 pid:      nid
196         | qid and id            { gen_and($1.b, $3.b); $$ = $3; }
197         | qid or id             { gen_or($1.b, $3.b); $$ = $3; }
198         ;
199 qid:      pnum                  { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
200                                                    $$.q = $<blk>0.q); }
201         | pid
202         ;
203 term:     rterm
204         | not term              { gen_not($2.b); $$ = $2; }
205         ;
206 head:     pqual dqual aqual     { QSET($$.q, $1, $2, $3); }
207         | pqual dqual           { QSET($$.q, $1, $2, Q_DEFAULT); }
208         | pqual aqual           { QSET($$.q, $1, Q_DEFAULT, $2); }
209         | pqual PROTO           { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
210         | pqual PROTOCHAIN      { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
211         | pqual ndaqual         { QSET($$.q, $1, Q_DEFAULT, $2); }
212         ;
213 rterm:    head id               { $$ = $2; }
214         | paren expr ')'        { $$.b = $2.b; $$.q = $1.q; }
215         | pname                 { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
216         | arth relop arth       { $$.b = gen_relation($2, $1, $3, 0);
217                                   $$.q = qerr; }
218         | arth irelop arth      { $$.b = gen_relation($2, $1, $3, 1);
219                                   $$.q = qerr; }
220         | other                 { $$.b = $1; $$.q = qerr; }
221         ;
222 /* protocol level qualifiers */
223 pqual:    pname
224         |                       { $$ = Q_DEFAULT; }
225         ;
226 /* 'direction' qualifiers */
227 dqual:    SRC                   { $$ = Q_SRC; }
228         | DST                   { $$ = Q_DST; }
229         | SRC OR DST            { $$ = Q_OR; }
230         | DST OR SRC            { $$ = Q_OR; }
231         | SRC AND DST           { $$ = Q_AND; }
232         | DST AND SRC           { $$ = Q_AND; }
233         ;
234 /* address type qualifiers */
235 aqual:    HOST                  { $$ = Q_HOST; }
236         | NET                   { $$ = Q_NET; }
237         | PORT                  { $$ = Q_PORT; }
238         ;
239 /* non-directional address type qualifiers */
240 ndaqual:  GATEWAY               { $$ = Q_GATEWAY; }
241         ;
242 pname:    LINK                  { $$ = Q_LINK; }
243         | IP                    { $$ = Q_IP; }
244         | ARP                   { $$ = Q_ARP; }
245         | RARP                  { $$ = Q_RARP; }
246         | TCP                   { $$ = Q_TCP; }
247         | UDP                   { $$ = Q_UDP; }
248         | ICMP                  { $$ = Q_ICMP; }
249         | IGMP                  { $$ = Q_IGMP; }
250         | IGRP                  { $$ = Q_IGRP; }
251         | PIM                   { $$ = Q_PIM; }
252         | ATALK                 { $$ = Q_ATALK; }
253         | AARP                  { $$ = Q_AARP; }
254         | DECNET                { $$ = Q_DECNET; }
255         | LAT                   { $$ = Q_LAT; }
256         | SCA                   { $$ = Q_SCA; }
257         | MOPDL                 { $$ = Q_MOPDL; }
258         | MOPRC                 { $$ = Q_MOPRC; }
259         | IPV6                  { $$ = Q_IPV6; }
260         | ICMPV6                { $$ = Q_ICMPV6; }
261         | AH                    { $$ = Q_AH; }
262         | ESP                   { $$ = Q_ESP; }
263         | ISO                   { $$ = Q_ISO; }
264         | ESIS                  { $$ = Q_ESIS; }
265         | ISIS                  { $$ = Q_ISIS; }
266         | CLNP                  { $$ = Q_CLNP; }
267         ;
268 other:    pqual TK_BROADCAST    { $$ = gen_broadcast($1); }
269         | pqual TK_MULTICAST    { $$ = gen_multicast($1); }
270         | LESS NUM              { $$ = gen_less($2); }
271         | GREATER NUM           { $$ = gen_greater($2); }
272         | BYTE NUM byteop NUM   { $$ = gen_byteop($3, $2, $4); }
273         | INBOUND               { $$ = gen_inbound(0); }
274         | OUTBOUND              { $$ = gen_inbound(1); }
275         | VLAN pnum             { $$ = gen_vlan($2); }
276         | VLAN                  { $$ = gen_vlan(-1); }
277         ;
278 relop:    '>'                   { $$ = BPF_JGT; }
279         | GEQ                   { $$ = BPF_JGE; }
280         | '='                   { $$ = BPF_JEQ; }
281         ;
282 irelop:   LEQ                   { $$ = BPF_JGT; }
283         | '<'                   { $$ = BPF_JGE; }
284         | NEQ                   { $$ = BPF_JEQ; }
285         ;
286 arth:     pnum                  { $$ = gen_loadi($1); }
287         | narth
288         ;
289 narth:    pname '[' arth ']'            { $$ = gen_load($1, $3, 1); }
290         | pname '[' arth ':' NUM ']'    { $$ = gen_load($1, $3, $5); }
291         | arth '+' arth                 { $$ = gen_arth(BPF_ADD, $1, $3); }
292         | arth '-' arth                 { $$ = gen_arth(BPF_SUB, $1, $3); }
293         | arth '*' arth                 { $$ = gen_arth(BPF_MUL, $1, $3); }
294         | arth '/' arth                 { $$ = gen_arth(BPF_DIV, $1, $3); }
295         | arth '&' arth                 { $$ = gen_arth(BPF_AND, $1, $3); }
296         | arth '|' arth                 { $$ = gen_arth(BPF_OR, $1, $3); }
297         | arth LSH arth                 { $$ = gen_arth(BPF_LSH, $1, $3); }
298         | arth RSH arth                 { $$ = gen_arth(BPF_RSH, $1, $3); }
299         | '-' arth %prec UMINUS         { $$ = gen_neg($2); }
300         | paren narth ')'               { $$ = $2; }
301         | LEN                           { $$ = gen_loadlen(); }
302         ;
303 byteop:   '&'                   { $$ = '&'; }
304         | '|'                   { $$ = '|'; }
305         | '<'                   { $$ = '<'; }
306         | '>'                   { $$ = '>'; }
307         | '='                   { $$ = '='; }
308         ;
309 pnum:     NUM
310         | paren pnum ')'        { $$ = $2; }
311         ;
312 %%