]> The Tcpdump Group git mirrors - libpcap/blob - grammar.y
Make "link[N:M]" refer to the 802.11 header for all 802.11 DLT_ values,
[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[] _U_ =
25 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.89 2005-05-02 21:13:09 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #ifdef WIN32
33 #include <pcap-stdinc.h>
34 #else /* WIN32 */
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #endif /* WIN32 */
38
39 #include <stdlib.h>
40
41 #ifndef WIN32
42 #if __STDC__
43 struct mbuf;
44 struct rtentry;
45 #endif
46
47 #include <netinet/in.h>
48 #endif /* WIN32 */
49
50 #include <stdio.h>
51
52 #include "pcap-int.h"
53
54 #include "gencode.h"
55 #include "pf.h"
56 #include <pcap-namedb.h>
57
58 #ifdef HAVE_OS_PROTO_H
59 #include "os-proto.h"
60 #endif
61
62 #define QSET(q, p, d, a) (q).proto = (p),\
63 (q).dir = (d),\
64 (q).addr = (a)
65
66 int n_errors = 0;
67
68 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
69
70 static void
71 yyerror(char *msg)
72 {
73 ++n_errors;
74 bpf_error("%s", msg);
75 /* NOTREACHED */
76 }
77
78 #ifndef YYBISON
79 int yyparse(void);
80
81 int
82 pcap_parse()
83 {
84 return (yyparse());
85 }
86 #endif
87
88 %}
89
90 %union {
91 int i;
92 bpf_u_int32 h;
93 u_char *e;
94 char *s;
95 struct stmt *stmt;
96 struct arth *a;
97 struct {
98 struct qual q;
99 int atmfieldtype;
100 struct block *b;
101 } blk;
102 struct block *rblk;
103 }
104
105 %type <blk> expr id nid pid term rterm qid
106 %type <blk> head
107 %type <i> pqual dqual aqual ndaqual
108 %type <a> arth narth
109 %type <i> byteop pname pnum relop irelop
110 %type <blk> and or paren not null prog
111 %type <rblk> other pfvar
112 %type <i> atmtype atmmultitype
113 %type <blk> atmfield
114 %type <blk> atmfieldvalue atmvalue atmlistvalue
115
116 %token DST SRC HOST GATEWAY
117 %token NET NETMASK PORT PORTRANGE LESS GREATER PROTO PROTOCHAIN CBYTE
118 %token ARP RARP IP SCTP TCP UDP ICMP IGMP IGRP PIM VRRP
119 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
120 %token TK_BROADCAST TK_MULTICAST
121 %token NUM INBOUND OUTBOUND
122 %token PF_IFNAME PF_RSET PF_RNR PF_SRNR PF_REASON PF_ACTION
123 %token LINK
124 %token GEQ LEQ NEQ
125 %token ID EID HID HID6 AID
126 %token LSH RSH
127 %token LEN
128 %token IPV6 ICMPV6 AH ESP
129 %token VLAN MPLS
130 %token ISO ESIS CLNP ISIS L1 L2 IIH LSP SNP CSNP PSNP
131 %token STP
132 %token IPX
133 %token NETBEUI
134 %token LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
135 %token OAM OAMF4 CONNECTMSG METACONNECT
136 %token VPI VCI
137 %token RADIO
138
139 %type <s> ID
140 %type <e> EID
141 %type <e> AID
142 %type <s> HID HID6
143 %type <i> NUM action reason
144
145 %left OR AND
146 %nonassoc '!'
147 %left '|'
148 %left '&'
149 %left LSH RSH
150 %left '+' '-'
151 %left '*' '/'
152 %nonassoc UMINUS
153 %%
154 prog: null expr
155 {
156 finish_parse($2.b);
157 }
158 | null
159 ;
160 null: /* null */ { $$.q = qerr; }
161 ;
162 expr: term
163 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
164 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
165 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
166 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
167 ;
168 and: AND { $$ = $<blk>0; }
169 ;
170 or: OR { $$ = $<blk>0; }
171 ;
172 id: nid
173 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
174 $$.q = $<blk>0.q); }
175 | paren pid ')' { $$ = $2; }
176 ;
177 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
178 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
179 $$.q = $<blk>0.q); }
180 | HID NETMASK HID { $$.b = gen_mcode($1, $3, 0,
181 $$.q = $<blk>0.q); }
182 | HID {
183 /* Decide how to parse HID based on proto */
184 $$.q = $<blk>0.q;
185 $$.b = gen_ncode($1, 0, $$.q);
186 }
187 | HID6 '/' NUM {
188 #ifdef INET6
189 $$.b = gen_mcode6($1, NULL, $3,
190 $$.q = $<blk>0.q);
191 #else
192 bpf_error("'ip6addr/prefixlen' not supported "
193 "in this configuration");
194 #endif /*INET6*/
195 }
196 | HID6 {
197 #ifdef INET6
198 $$.b = gen_mcode6($1, 0, 128,
199 $$.q = $<blk>0.q);
200 #else
201 bpf_error("'ip6addr' not supported "
202 "in this configuration");
203 #endif /*INET6*/
204 }
205 | EID {
206 $$.b = gen_ecode($1, $$.q = $<blk>0.q);
207 /*
208 * $1 was allocated by "pcap_ether_aton()",
209 * so we must free it now that we're done
210 * with it.
211 */
212 free($1);
213 }
214 | AID {
215 $$.b = gen_acode($1, $$.q = $<blk>0.q);
216 /*
217 * $1 was allocated by "pcap_ether_aton()",
218 * so we must free it now that we're done
219 * with it.
220 */
221 free($1);
222 }
223 | not id { gen_not($2.b); $$ = $2; }
224 ;
225 not: '!' { $$ = $<blk>0; }
226 ;
227 paren: '(' { $$ = $<blk>0; }
228 ;
229 pid: nid
230 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
231 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
232 ;
233 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
234 $$.q = $<blk>0.q); }
235 | pid
236 ;
237 term: rterm
238 | not term { gen_not($2.b); $$ = $2; }
239 ;
240 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
241 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
242 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
243 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
244 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
245 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
246 ;
247 rterm: head id { $$ = $2; }
248 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
249 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
250 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
251 $$.q = qerr; }
252 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
253 $$.q = qerr; }
254 | other { $$.b = $1; $$.q = qerr; }
255 | atmtype { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
256 | atmmultitype { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
257 | atmfield atmvalue { $$.b = $2.b; $$.q = qerr; }
258 ;
259 /* protocol level qualifiers */
260 pqual: pname
261 | { $$ = Q_DEFAULT; }
262 ;
263 /* 'direction' qualifiers */
264 dqual: SRC { $$ = Q_SRC; }
265 | DST { $$ = Q_DST; }
266 | SRC OR DST { $$ = Q_OR; }
267 | DST OR SRC { $$ = Q_OR; }
268 | SRC AND DST { $$ = Q_AND; }
269 | DST AND SRC { $$ = Q_AND; }
270 ;
271 /* address type qualifiers */
272 aqual: HOST { $$ = Q_HOST; }
273 | NET { $$ = Q_NET; }
274 | PORT { $$ = Q_PORT; }
275 | PORTRANGE { $$ = Q_PORTRANGE; }
276 ;
277 /* non-directional address type qualifiers */
278 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
279 ;
280 pname: LINK { $$ = Q_LINK; }
281 | IP { $$ = Q_IP; }
282 | ARP { $$ = Q_ARP; }
283 | RARP { $$ = Q_RARP; }
284 | SCTP { $$ = Q_SCTP; }
285 | TCP { $$ = Q_TCP; }
286 | UDP { $$ = Q_UDP; }
287 | ICMP { $$ = Q_ICMP; }
288 | IGMP { $$ = Q_IGMP; }
289 | IGRP { $$ = Q_IGRP; }
290 | PIM { $$ = Q_PIM; }
291 | VRRP { $$ = Q_VRRP; }
292 | ATALK { $$ = Q_ATALK; }
293 | AARP { $$ = Q_AARP; }
294 | DECNET { $$ = Q_DECNET; }
295 | LAT { $$ = Q_LAT; }
296 | SCA { $$ = Q_SCA; }
297 | MOPDL { $$ = Q_MOPDL; }
298 | MOPRC { $$ = Q_MOPRC; }
299 | IPV6 { $$ = Q_IPV6; }
300 | ICMPV6 { $$ = Q_ICMPV6; }
301 | AH { $$ = Q_AH; }
302 | ESP { $$ = Q_ESP; }
303 | ISO { $$ = Q_ISO; }
304 | ESIS { $$ = Q_ESIS; }
305 | ISIS { $$ = Q_ISIS; }
306 | L1 { $$ = Q_ISIS_L1; }
307 | L2 { $$ = Q_ISIS_L2; }
308 | IIH { $$ = Q_ISIS_IIH; }
309 | LSP { $$ = Q_ISIS_LSP; }
310 | SNP { $$ = Q_ISIS_SNP; }
311 | PSNP { $$ = Q_ISIS_PSNP; }
312 | CSNP { $$ = Q_ISIS_CSNP; }
313 | CLNP { $$ = Q_CLNP; }
314 | STP { $$ = Q_STP; }
315 | IPX { $$ = Q_IPX; }
316 | NETBEUI { $$ = Q_NETBEUI; }
317 | RADIO { $$ = Q_RADIO; }
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 | MPLS pnum { $$ = gen_mpls($2); }
329 | MPLS { $$ = gen_mpls(-1); }
330 | pfvar { $$ = $1; }
331 ;
332
333 pfvar: PF_IFNAME ID { $$ = gen_pf_ifname($2); }
334 | PF_RSET ID { $$ = gen_pf_ruleset($2); }
335 | PF_RNR NUM { $$ = gen_pf_rnr($2); }
336 | PF_SRNR NUM { $$ = gen_pf_srnr($2); }
337 | PF_REASON reason { $$ = gen_pf_reason($2); }
338 | PF_ACTION action { $$ = gen_pf_action($2); }
339 ;
340
341 reason: NUM { $$ = $1; }
342 | ID { const char *reasons[] = PFRES_NAMES;
343 int i;
344 for (i = 0; reasons[i]; i++) {
345 if (pcap_strcasecmp($1, reasons[i]) == 0) {
346 $$ = i;
347 break;
348 }
349 }
350 if (reasons[i] == NULL)
351 bpf_error("unknown PF reason");
352 }
353 ;
354
355 action: ID { if (pcap_strcasecmp($1, "pass") == 0 ||
356 pcap_strcasecmp($1, "accept") == 0)
357 $$ = PF_PASS;
358 else if (pcap_strcasecmp($1, "drop") == 0 ||
359 pcap_strcasecmp($1, "block") == 0)
360 $$ = PF_DROP;
361 else
362 bpf_error("unknown PF action");
363 }
364 ;
365
366 relop: '>' { $$ = BPF_JGT; }
367 | GEQ { $$ = BPF_JGE; }
368 | '=' { $$ = BPF_JEQ; }
369 ;
370 irelop: LEQ { $$ = BPF_JGT; }
371 | '<' { $$ = BPF_JGE; }
372 | NEQ { $$ = BPF_JEQ; }
373 ;
374 arth: pnum { $$ = gen_loadi($1); }
375 | narth
376 ;
377 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
378 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
379 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
380 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
381 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
382 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
383 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
384 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
385 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
386 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
387 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
388 | paren narth ')' { $$ = $2; }
389 | LEN { $$ = gen_loadlen(); }
390 ;
391 byteop: '&' { $$ = '&'; }
392 | '|' { $$ = '|'; }
393 | '<' { $$ = '<'; }
394 | '>' { $$ = '>'; }
395 | '=' { $$ = '='; }
396 ;
397 pnum: NUM
398 | paren pnum ')' { $$ = $2; }
399 ;
400 atmtype: LANE { $$ = A_LANE; }
401 | LLC { $$ = A_LLC; }
402 | METAC { $$ = A_METAC; }
403 | BCC { $$ = A_BCC; }
404 | OAMF4EC { $$ = A_OAMF4EC; }
405 | OAMF4SC { $$ = A_OAMF4SC; }
406 | SC { $$ = A_SC; }
407 | ILMIC { $$ = A_ILMIC; }
408 ;
409 atmmultitype: OAM { $$ = A_OAM; }
410 | OAMF4 { $$ = A_OAMF4; }
411 | CONNECTMSG { $$ = A_CONNECTMSG; }
412 | METACONNECT { $$ = A_METACONNECT; }
413 ;
414 /* ATM field types quantifier */
415 atmfield: VPI { $$.atmfieldtype = A_VPI; }
416 | VCI { $$.atmfieldtype = A_VCI; }
417 ;
418 atmvalue: atmfieldvalue
419 | relop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 0); }
420 | irelop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 1); }
421 | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
422 ;
423 atmfieldvalue: NUM {
424 $$.atmfieldtype = $<blk>0.atmfieldtype;
425 if ($$.atmfieldtype == A_VPI ||
426 $$.atmfieldtype == A_VCI)
427 $$.b = gen_atmfield_code($$.atmfieldtype, (bpf_int32) $1, BPF_JEQ, 0);
428 }
429 ;
430 atmlistvalue: atmfieldvalue
431 | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
432 ;
433 %%