]> The Tcpdump Group git mirrors - libpcap/blob - grammar.y
add BPF_ filters for misc. IS-IS PDU Types
[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.78 2002-12-06 00:01:34 hannes 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/time.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 <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
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 MASK PORT 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 LINK
123 %token GEQ LEQ NEQ
124 %token ID EID HID HID6 AID
125 %token LSH RSH
126 %token LEN
127 %token IPV6 ICMPV6 AH ESP
128 %token VLAN
129 %token ISO ESIS CLNP ISIS L1 L2 IIH LSP SNP CSNP PSNP
130 %token STP
131 %token IPX
132 %token NETBEUI
133 %token LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
134 %token OAM OAMF4 CONNECTMSG METACONNECT
135 %token VPI VCI
136
137 %type <s> ID
138 %type <e> EID
139 %type <e> AID
140 %type <s> HID HID6
141 %type <i> NUM
142
143 %left OR AND
144 %nonassoc '!'
145 %left '|'
146 %left '&'
147 %left LSH RSH
148 %left '+' '-'
149 %left '*' '/'
150 %nonassoc UMINUS
151 %%
152 prog: null expr
153 {
154 finish_parse($2.b);
155 }
156 | null
157 ;
158 null: /* null */ { $$.q = qerr; }
159 ;
160 expr: term
161 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
162 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
163 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
164 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
165 ;
166 and: AND { $$ = $<blk>0; }
167 ;
168 or: OR { $$ = $<blk>0; }
169 ;
170 id: nid
171 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
172 $$.q = $<blk>0.q); }
173 | paren pid ')' { $$ = $2; }
174 ;
175 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
176 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
177 $$.q = $<blk>0.q); }
178 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
179 $$.q = $<blk>0.q); }
180 | HID {
181 /* Decide how to parse HID based on proto */
182 $$.q = $<blk>0.q;
183 $$.b = gen_ncode($1, 0, $$.q);
184 }
185 | HID6 '/' NUM {
186 #ifdef INET6
187 $$.b = gen_mcode6($1, NULL, $3,
188 $$.q = $<blk>0.q);
189 #else
190 bpf_error("'ip6addr/prefixlen' not supported "
191 "in this configuration");
192 #endif /*INET6*/
193 }
194 | HID6 {
195 #ifdef INET6
196 $$.b = gen_mcode6($1, 0, 128,
197 $$.q = $<blk>0.q);
198 #else
199 bpf_error("'ip6addr' not supported "
200 "in this configuration");
201 #endif /*INET6*/
202 }
203 | EID {
204 $$.b = gen_ecode($1, $$.q = $<blk>0.q);
205 /*
206 * $1 was allocated by "pcap_ether_aton()",
207 * so we must free it now that we're done
208 * with it.
209 */
210 free($1);
211 }
212 | AID {
213 $$.b = gen_acode($1, $$.q = $<blk>0.q);
214 /*
215 * $1 was allocated by "pcap_ether_aton()",
216 * so we must free it now that we're done
217 * with it.
218 */
219 free($1);
220 }
221 | not id { gen_not($2.b); $$ = $2; }
222 ;
223 not: '!' { $$ = $<blk>0; }
224 ;
225 paren: '(' { $$ = $<blk>0; }
226 ;
227 pid: nid
228 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
229 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
230 ;
231 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
232 $$.q = $<blk>0.q); }
233 | pid
234 ;
235 term: rterm
236 | not term { gen_not($2.b); $$ = $2; }
237 ;
238 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
239 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
240 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
241 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
242 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
243 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
244 ;
245 rterm: head id { $$ = $2; }
246 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
247 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
248 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
249 $$.q = qerr; }
250 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
251 $$.q = qerr; }
252 | other { $$.b = $1; $$.q = qerr; }
253 | atmtype { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
254 | atmmultitype { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
255 | atmfield atmvalue { $$.b = $2.b; $$.q = qerr; }
256 ;
257 /* protocol level qualifiers */
258 pqual: pname
259 | { $$ = Q_DEFAULT; }
260 ;
261 /* 'direction' qualifiers */
262 dqual: SRC { $$ = Q_SRC; }
263 | DST { $$ = Q_DST; }
264 | SRC OR DST { $$ = Q_OR; }
265 | DST OR SRC { $$ = Q_OR; }
266 | SRC AND DST { $$ = Q_AND; }
267 | DST AND SRC { $$ = Q_AND; }
268 ;
269 /* address type qualifiers */
270 aqual: HOST { $$ = Q_HOST; }
271 | NET { $$ = Q_NET; }
272 | PORT { $$ = Q_PORT; }
273 ;
274 /* non-directional address type qualifiers */
275 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
276 ;
277 pname: LINK { $$ = Q_LINK; }
278 | IP { $$ = Q_IP; }
279 | ARP { $$ = Q_ARP; }
280 | RARP { $$ = Q_RARP; }
281 | SCTP { $$ = Q_SCTP; }
282 | TCP { $$ = Q_TCP; }
283 | UDP { $$ = Q_UDP; }
284 | ICMP { $$ = Q_ICMP; }
285 | IGMP { $$ = Q_IGMP; }
286 | IGRP { $$ = Q_IGRP; }
287 | PIM { $$ = Q_PIM; }
288 | VRRP { $$ = Q_VRRP; }
289 | ATALK { $$ = Q_ATALK; }
290 | AARP { $$ = Q_AARP; }
291 | DECNET { $$ = Q_DECNET; }
292 | LAT { $$ = Q_LAT; }
293 | SCA { $$ = Q_SCA; }
294 | MOPDL { $$ = Q_MOPDL; }
295 | MOPRC { $$ = Q_MOPRC; }
296 | IPV6 { $$ = Q_IPV6; }
297 | ICMPV6 { $$ = Q_ICMPV6; }
298 | AH { $$ = Q_AH; }
299 | ESP { $$ = Q_ESP; }
300 | ISO { $$ = Q_ISO; }
301 | ESIS { $$ = Q_ESIS; }
302 | ISIS { $$ = Q_ISIS; }
303 | L1 { $$ = Q_ISIS_L1; }
304 | L2 { $$ = Q_ISIS_L2; }
305 | IIH { $$ = Q_ISIS_IIH; }
306 | LSP { $$ = Q_ISIS_LSP; }
307 | SNP { $$ = Q_ISIS_SNP; }
308 | PSNP { $$ = Q_ISIS_PSNP; }
309 | CSNP { $$ = Q_ISIS_CSNP; }
310 | CLNP { $$ = Q_CLNP; }
311 | STP { $$ = Q_STP; }
312 | IPX { $$ = Q_IPX; }
313 | NETBEUI { $$ = Q_NETBEUI; }
314 ;
315 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
316 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
317 | LESS NUM { $$ = gen_less($2); }
318 | GREATER NUM { $$ = gen_greater($2); }
319 | CBYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
320 | INBOUND { $$ = gen_inbound(0); }
321 | OUTBOUND { $$ = gen_inbound(1); }
322 | VLAN pnum { $$ = gen_vlan($2); }
323 | VLAN { $$ = gen_vlan(-1); }
324 ;
325 relop: '>' { $$ = BPF_JGT; }
326 | GEQ { $$ = BPF_JGE; }
327 | '=' { $$ = BPF_JEQ; }
328 ;
329 irelop: LEQ { $$ = BPF_JGT; }
330 | '<' { $$ = BPF_JGE; }
331 | NEQ { $$ = BPF_JEQ; }
332 ;
333 arth: pnum { $$ = gen_loadi($1); }
334 | narth
335 ;
336 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
337 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
338 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
339 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
340 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
341 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
342 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
343 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
344 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
345 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
346 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
347 | paren narth ')' { $$ = $2; }
348 | LEN { $$ = gen_loadlen(); }
349 ;
350 byteop: '&' { $$ = '&'; }
351 | '|' { $$ = '|'; }
352 | '<' { $$ = '<'; }
353 | '>' { $$ = '>'; }
354 | '=' { $$ = '='; }
355 ;
356 pnum: NUM
357 | paren pnum ')' { $$ = $2; }
358 ;
359 atmtype: LANE { $$ = A_LANE; }
360 | LLC { $$ = A_LLC; }
361 | METAC { $$ = A_METAC; }
362 | BCC { $$ = A_BCC; }
363 | OAMF4EC { $$ = A_OAMF4EC; }
364 | OAMF4SC { $$ = A_OAMF4SC; }
365 | SC { $$ = A_SC; }
366 | ILMIC { $$ = A_ILMIC; }
367 ;
368 atmmultitype: OAM { $$ = A_OAM; }
369 | OAMF4 { $$ = A_OAMF4; }
370 | CONNECTMSG { $$ = A_CONNECTMSG; }
371 | METACONNECT { $$ = A_METACONNECT; }
372 ;
373 /* ATM field types quantifier */
374 atmfield: VPI { $$.atmfieldtype = A_VPI; }
375 | VCI { $$.atmfieldtype = A_VCI; }
376 ;
377 atmvalue: atmfieldvalue
378 | relop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (u_int)$2, (u_int)$1, 0); }
379 | irelop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (u_int)$2, (u_int)$1, 1); }
380 | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
381 ;
382 atmfieldvalue: NUM {
383 $$.atmfieldtype = $<blk>0.atmfieldtype;
384 if ($$.atmfieldtype == A_VPI ||
385 $$.atmfieldtype == A_VCI)
386 $$.b = gen_atmfield_code($$.atmfieldtype, (u_int) $1, BPF_JEQ, 0);
387 }
388 ;
389 atmlistvalue: atmfieldvalue
390 | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
391 ;
392 %%