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