]> The Tcpdump Group git mirrors - libpcap/blob - grammar.y
Add support for reading capture files from programs using Alexey
[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 */
23 #ifndef lint
24 static const char rcsid[] =
25 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.59 2000-07-11 00:37:08 assar Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 #include <stdlib.h>
36
37 #if __STDC__
38 struct mbuf;
39 struct rtentry;
40 #endif
41
42 #include <net/if.h>
43
44 #include <netinet/in.h>
45 #include <netinet/if_ether.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 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
121 %type <s> ID
122 %type <e> EID
123 %type <s> HID HID6
124 %type <i> NUM
125
126 %left OR AND
127 %nonassoc '!'
128 %left '|'
129 %left '&'
130 %left LSH RSH
131 %left '+' '-'
132 %left '*' '/'
133 %nonassoc UMINUS
134 %%
135 prog: null expr
136 {
137 finish_parse($2.b);
138 }
139 | null
140 ;
141 null: /* null */ { $$.q = qerr; }
142 ;
143 expr: term
144 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
145 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
146 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
147 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
148 ;
149 and: AND { $$ = $<blk>0; }
150 ;
151 or: OR { $$ = $<blk>0; }
152 ;
153 id: nid
154 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
155 $$.q = $<blk>0.q); }
156 | paren pid ')' { $$ = $2; }
157 ;
158 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
159 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
160 $$.q = $<blk>0.q); }
161 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
162 $$.q = $<blk>0.q); }
163 | HID {
164 /* Decide how to parse HID based on proto */
165 $$.q = $<blk>0.q;
166 switch ($$.q.proto) {
167 case Q_DECNET:
168 $$.b = gen_ncode($1, 0, $$.q);
169 break;
170 default:
171 $$.b = gen_ncode($1, 0, $$.q);
172 break;
173 }
174 }
175 | HID6 '/' NUM {
176 #ifdef INET6
177 $$.b = gen_mcode6($1, NULL, $3,
178 $$.q = $<blk>0.q);
179 #else
180 bpf_error("'ip6addr/prefixlen' not supported "
181 "in this configuration");
182 #endif /*INET6*/
183 }
184 | HID6 {
185 #ifdef INET6
186 $$.b = gen_mcode6($1, 0, 128,
187 $$.q = $<blk>0.q);
188 #else
189 bpf_error("'ip6addr' not supported "
190 "in this configuration");
191 #endif /*INET6*/
192 }
193 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
194 | not id { gen_not($2.b); $$ = $2; }
195 ;
196 not: '!' { $$ = $<blk>0; }
197 ;
198 paren: '(' { $$ = $<blk>0; }
199 ;
200 pid: nid
201 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
202 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
203 ;
204 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
205 $$.q = $<blk>0.q); }
206 | pid
207 ;
208 term: rterm
209 | not term { gen_not($2.b); $$ = $2; }
210 ;
211 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
212 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
213 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
214 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
215 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
216 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
217 ;
218 rterm: head id { $$ = $2; }
219 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
220 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
221 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
222 $$.q = qerr; }
223 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
224 $$.q = qerr; }
225 | other { $$.b = $1; $$.q = qerr; }
226 ;
227 /* protocol level qualifiers */
228 pqual: pname
229 | { $$ = Q_DEFAULT; }
230 ;
231 /* 'direction' qualifiers */
232 dqual: SRC { $$ = Q_SRC; }
233 | DST { $$ = Q_DST; }
234 | SRC OR DST { $$ = Q_OR; }
235 | DST OR SRC { $$ = Q_OR; }
236 | SRC AND DST { $$ = Q_AND; }
237 | DST AND SRC { $$ = Q_AND; }
238 ;
239 /* address type qualifiers */
240 aqual: HOST { $$ = Q_HOST; }
241 | NET { $$ = Q_NET; }
242 | PORT { $$ = Q_PORT; }
243 ;
244 /* non-directional address type qualifiers */
245 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
246 ;
247 pname: LINK { $$ = Q_LINK; }
248 | IP { $$ = Q_IP; }
249 | ARP { $$ = Q_ARP; }
250 | RARP { $$ = Q_RARP; }
251 | TCP { $$ = Q_TCP; }
252 | UDP { $$ = Q_UDP; }
253 | ICMP { $$ = Q_ICMP; }
254 | IGMP { $$ = Q_IGMP; }
255 | IGRP { $$ = Q_IGRP; }
256 | PIM { $$ = Q_PIM; }
257 | ATALK { $$ = Q_ATALK; }
258 | DECNET { $$ = Q_DECNET; }
259 | LAT { $$ = Q_LAT; }
260 | SCA { $$ = Q_SCA; }
261 | MOPDL { $$ = Q_MOPDL; }
262 | MOPRC { $$ = Q_MOPRC; }
263 | IPV6 { $$ = Q_IPV6; }
264 | ICMPV6 { $$ = Q_ICMPV6; }
265 | AH { $$ = Q_AH; }
266 | ESP { $$ = Q_ESP; }
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 ;
276 relop: '>' { $$ = BPF_JGT; }
277 | GEQ { $$ = BPF_JGE; }
278 | '=' { $$ = BPF_JEQ; }
279 ;
280 irelop: LEQ { $$ = BPF_JGT; }
281 | '<' { $$ = BPF_JGE; }
282 | NEQ { $$ = BPF_JEQ; }
283 ;
284 arth: pnum { $$ = gen_loadi($1); }
285 | narth
286 ;
287 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
288 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
289 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
290 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
291 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
292 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
293 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
294 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
295 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
296 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
297 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
298 | paren narth ')' { $$ = $2; }
299 | LEN { $$ = gen_loadlen(); }
300 ;
301 byteop: '&' { $$ = '&'; }
302 | '|' { $$ = '|'; }
303 | '<' { $$ = '<'; }
304 | '>' { $$ = '>'; }
305 | '=' { $$ = '='; }
306 ;
307 pnum: NUM
308 | paren pnum ')' { $$ = $2; }
309 ;
310 %%