3 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
4 * The Regents of the University of California. All rights reserved.
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
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.
24 static const char rcsid[] _U_ =
25 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.86.2.6 2006-12-21 19:45:04 guy Exp $ (LBL)";
33 #include <pcap-stdinc.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
47 #include <netinet/in.h>
56 #include <pcap-namedb.h>
58 #ifdef HAVE_OS_PROTO_H
62 #define QSET(q, p, d, a) (q).proto = (p),\
68 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
106 %type <blk> expr id nid pid term rterm qid
108 %type <i> pqual dqual aqual ndaqual
110 %type <i> byteop pname pnum relop irelop
111 %type <blk> and or paren not null prog
112 %type <rblk> other pfvar
113 %type <i> atmtype atmmultitype
115 %type <blk> atmfieldvalue atmvalue atmlistvalue
117 %type <blk> mtp3field
118 %type <blk> mtp3fieldvalue mtp3value mtp3listvalue
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
130 %token ID EID HID HID6 AID
133 %token IPV6 ICMPV6 AH ESP
136 %token ISO ESIS CLNP ISIS L1 L2 IIH LSP SNP CSNP PSNP
140 %token LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
141 %token OAM OAMF4 CONNECTMSG METACONNECT
145 %token SIO OPC DPC SLS
151 %type <i> NUM action reason
168 null: /* null */ { $$.q = qerr; }
171 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
172 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
173 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
174 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
176 and: AND { $$ = $<blk>0; }
178 or: OR { $$ = $<blk>0; }
181 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
183 | paren pid ')' { $$ = $2; }
185 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
186 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
188 | HID NETMASK HID { $$.b = gen_mcode($1, $3, 0,
191 /* Decide how to parse HID based on proto */
193 $$.b = gen_ncode($1, 0, $$.q);
197 $$.b = gen_mcode6($1, NULL, $3,
200 bpf_error("'ip6addr/prefixlen' not supported "
201 "in this configuration");
206 $$.b = gen_mcode6($1, 0, 128,
209 bpf_error("'ip6addr' not supported "
210 "in this configuration");
214 $$.b = gen_ecode($1, $$.q = $<blk>0.q);
216 * $1 was allocated by "pcap_ether_aton()",
217 * so we must free it now that we're done
223 $$.b = gen_acode($1, $$.q = $<blk>0.q);
225 * $1 was allocated by "pcap_ether_aton()",
226 * so we must free it now that we're done
231 | not id { gen_not($2.b); $$ = $2; }
233 not: '!' { $$ = $<blk>0; }
235 paren: '(' { $$ = $<blk>0; }
238 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
239 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
241 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
246 | not term { gen_not($2.b); $$ = $2; }
248 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
249 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
250 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
251 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
252 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
253 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
255 rterm: head id { $$ = $2; }
256 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
257 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
258 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
260 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
262 | other { $$.b = $1; $$.q = qerr; }
263 | atmtype { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
264 | atmmultitype { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
265 | atmfield atmvalue { $$.b = $2.b; $$.q = qerr; }
266 | mtp2type { $$.b = gen_mtp2type_abbrev($1); $$.q = qerr; }
267 | mtp3field mtp3value { $$.b = $2.b; $$.q = qerr; }
269 /* protocol level qualifiers */
271 | { $$ = Q_DEFAULT; }
273 /* 'direction' qualifiers */
274 dqual: SRC { $$ = Q_SRC; }
275 | DST { $$ = Q_DST; }
276 | SRC OR DST { $$ = Q_OR; }
277 | DST OR SRC { $$ = Q_OR; }
278 | SRC AND DST { $$ = Q_AND; }
279 | DST AND SRC { $$ = Q_AND; }
281 /* address type qualifiers */
282 aqual: HOST { $$ = Q_HOST; }
283 | NET { $$ = Q_NET; }
284 | PORT { $$ = Q_PORT; }
285 | PORTRANGE { $$ = Q_PORTRANGE; }
287 /* non-directional address type qualifiers */
288 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
290 pname: LINK { $$ = Q_LINK; }
292 | ARP { $$ = Q_ARP; }
293 | RARP { $$ = Q_RARP; }
294 | SCTP { $$ = Q_SCTP; }
295 | TCP { $$ = Q_TCP; }
296 | UDP { $$ = Q_UDP; }
297 | ICMP { $$ = Q_ICMP; }
298 | IGMP { $$ = Q_IGMP; }
299 | IGRP { $$ = Q_IGRP; }
300 | PIM { $$ = Q_PIM; }
301 | VRRP { $$ = Q_VRRP; }
302 | ATALK { $$ = Q_ATALK; }
303 | AARP { $$ = Q_AARP; }
304 | DECNET { $$ = Q_DECNET; }
305 | LAT { $$ = Q_LAT; }
306 | SCA { $$ = Q_SCA; }
307 | MOPDL { $$ = Q_MOPDL; }
308 | MOPRC { $$ = Q_MOPRC; }
309 | IPV6 { $$ = Q_IPV6; }
310 | ICMPV6 { $$ = Q_ICMPV6; }
312 | ESP { $$ = Q_ESP; }
313 | ISO { $$ = Q_ISO; }
314 | ESIS { $$ = Q_ESIS; }
315 | ISIS { $$ = Q_ISIS; }
316 | L1 { $$ = Q_ISIS_L1; }
317 | L2 { $$ = Q_ISIS_L2; }
318 | IIH { $$ = Q_ISIS_IIH; }
319 | LSP { $$ = Q_ISIS_LSP; }
320 | SNP { $$ = Q_ISIS_SNP; }
321 | PSNP { $$ = Q_ISIS_PSNP; }
322 | CSNP { $$ = Q_ISIS_CSNP; }
323 | CLNP { $$ = Q_CLNP; }
324 | STP { $$ = Q_STP; }
325 | IPX { $$ = Q_IPX; }
326 | NETBEUI { $$ = Q_NETBEUI; }
327 | RADIO { $$ = Q_RADIO; }
329 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
330 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
331 | LESS NUM { $$ = gen_less($2); }
332 | GREATER NUM { $$ = gen_greater($2); }
333 | CBYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
334 | INBOUND { $$ = gen_inbound(0); }
335 | OUTBOUND { $$ = gen_inbound(1); }
336 | VLAN pnum { $$ = gen_vlan($2); }
337 | VLAN { $$ = gen_vlan(-1); }
338 | MPLS pnum { $$ = gen_mpls($2); }
339 | MPLS { $$ = gen_mpls(-1); }
340 | PPPOED { $$ = gen_pppoed(); }
341 | PPPOES { $$ = gen_pppoes(); }
345 pfvar: PF_IFNAME ID { $$ = gen_pf_ifname($2); }
346 | PF_RSET ID { $$ = gen_pf_ruleset($2); }
347 | PF_RNR NUM { $$ = gen_pf_rnr($2); }
348 | PF_SRNR NUM { $$ = gen_pf_srnr($2); }
349 | PF_REASON reason { $$ = gen_pf_reason($2); }
350 | PF_ACTION action { $$ = gen_pf_action($2); }
353 reason: NUM { $$ = $1; }
354 | ID { const char *reasons[] = PFRES_NAMES;
356 for (i = 0; reasons[i]; i++) {
357 if (pcap_strcasecmp($1, reasons[i]) == 0) {
362 if (reasons[i] == NULL)
363 bpf_error("unknown PF reason");
367 action: ID { if (pcap_strcasecmp($1, "pass") == 0 ||
368 pcap_strcasecmp($1, "accept") == 0)
370 else if (pcap_strcasecmp($1, "drop") == 0 ||
371 pcap_strcasecmp($1, "block") == 0)
374 bpf_error("unknown PF action");
378 relop: '>' { $$ = BPF_JGT; }
379 | GEQ { $$ = BPF_JGE; }
380 | '=' { $$ = BPF_JEQ; }
382 irelop: LEQ { $$ = BPF_JGT; }
383 | '<' { $$ = BPF_JGE; }
384 | NEQ { $$ = BPF_JEQ; }
386 arth: pnum { $$ = gen_loadi($1); }
389 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
390 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
391 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
392 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
393 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
394 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
395 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
396 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
397 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
398 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
399 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
400 | paren narth ')' { $$ = $2; }
401 | LEN { $$ = gen_loadlen(); }
403 byteop: '&' { $$ = '&'; }
410 | paren pnum ')' { $$ = $2; }
412 atmtype: LANE { $$ = A_LANE; }
413 | LLC { $$ = A_LLC; }
414 | METAC { $$ = A_METAC; }
415 | BCC { $$ = A_BCC; }
416 | OAMF4EC { $$ = A_OAMF4EC; }
417 | OAMF4SC { $$ = A_OAMF4SC; }
419 | ILMIC { $$ = A_ILMIC; }
421 atmmultitype: OAM { $$ = A_OAM; }
422 | OAMF4 { $$ = A_OAMF4; }
423 | CONNECTMSG { $$ = A_CONNECTMSG; }
424 | METACONNECT { $$ = A_METACONNECT; }
426 /* ATM field types quantifier */
427 atmfield: VPI { $$.atmfieldtype = A_VPI; }
428 | VCI { $$.atmfieldtype = A_VCI; }
430 atmvalue: atmfieldvalue
431 | relop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 0); }
432 | irelop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 1); }
433 | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
436 $$.atmfieldtype = $<blk>0.atmfieldtype;
437 if ($$.atmfieldtype == A_VPI ||
438 $$.atmfieldtype == A_VCI)
439 $$.b = gen_atmfield_code($$.atmfieldtype, (bpf_int32) $1, BPF_JEQ, 0);
442 atmlistvalue: atmfieldvalue
443 | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
445 /* MTP2 types quantifier */
446 mtp2type: FISU { $$ = M_FISU; }
447 | LSU { $$ = M_LSU; }
448 | MSU { $$ = M_MSU; }
450 /* MTP3 field types quantifier */
451 mtp3field: SIO { $$.mtp3fieldtype = M_SIO; }
452 | OPC { $$.mtp3fieldtype = M_OPC; }
453 | DPC { $$.mtp3fieldtype = M_DPC; }
454 | SLS { $$.mtp3fieldtype = M_SLS; }
456 mtp3value: mtp3fieldvalue
457 | relop NUM { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 0); }
458 | irelop NUM { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 1); }
459 | paren mtp3listvalue ')' { $$.b = $2.b; $$.q = qerr; }
461 mtp3fieldvalue: NUM {
462 $$.mtp3fieldtype = $<blk>0.mtp3fieldtype;
463 if ($$.mtp3fieldtype == M_SIO ||
464 $$.mtp3fieldtype == M_OPC ||
465 $$.mtp3fieldtype == M_DPC ||
466 $$.mtp3fieldtype == M_SLS )
467 $$.b = gen_mtp3field_code($$.mtp3fieldtype, (u_int) $1, BPF_JEQ, 0);
470 mtp3listvalue: mtp3fieldvalue
471 | mtp3listvalue or mtp3fieldvalue { gen_or($1.b, $3.b); $$ = $3; }