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.
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.86.2.5 2005/09/05 09:08:06 guy Exp $ (LBL)";
34 #include <pcap-stdinc.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
48 #include <netinet/in.h>
57 #include <pcap-namedb.h>
59 #ifdef HAVE_OS_PROTO_H
63 #define QSET(q, p, d, a) (q).proto = (p),\
69 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
107 %type <blk> expr id nid pid term rterm qid
109 %type <i> pqual dqual aqual ndaqual
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
116 %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
144 %token SIO OPC DPC SLS
150 %type <i> NUM action reason
167 null: /* null */ { $$.q = qerr; }
170 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
171 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
172 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
173 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
175 and: AND { $$ = $<blk>0; }
177 or: OR { $$ = $<blk>0; }
180 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
182 | paren pid ')' { $$ = $2; }
184 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
185 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
187 | HID NETMASK HID { $$.b = gen_mcode($1, $3, 0,
190 /* Decide how to parse HID based on proto */
192 $$.b = gen_ncode($1, 0, $$.q);
196 $$.b = gen_mcode6($1, NULL, $3,
199 bpf_error("'ip6addr/prefixlen' not supported "
200 "in this configuration");
205 $$.b = gen_mcode6($1, 0, 128,
208 bpf_error("'ip6addr' not supported "
209 "in this configuration");
213 $$.b = gen_ecode($1, $$.q = $<blk>0.q);
215 * $1 was allocated by "pcap_ether_aton()",
216 * so we must free it now that we're done
222 $$.b = gen_acode($1, $$.q = $<blk>0.q);
224 * $1 was allocated by "pcap_ether_aton()",
225 * so we must free it now that we're done
230 | not id { gen_not($2.b); $$ = $2; }
232 not: '!' { $$ = $<blk>0; }
234 paren: '(' { $$ = $<blk>0; }
237 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
238 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
240 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
245 | not term { gen_not($2.b); $$ = $2; }
247 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
248 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
249 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
250 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
251 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
252 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
254 rterm: head id { $$ = $2; }
255 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
256 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
257 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
259 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
261 | other { $$.b = $1; $$.q = qerr; }
262 | atmtype { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
263 | atmmultitype { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
264 | atmfield atmvalue { $$.b = $2.b; $$.q = qerr; }
265 | mtp3field mtp3value { $$.b = $2.b; $$.q = qerr; }
267 /* protocol level qualifiers */
269 | { $$ = Q_DEFAULT; }
271 /* 'direction' qualifiers */
272 dqual: SRC { $$ = Q_SRC; }
273 | DST { $$ = Q_DST; }
274 | SRC OR DST { $$ = Q_OR; }
275 | DST OR SRC { $$ = Q_OR; }
276 | SRC AND DST { $$ = Q_AND; }
277 | DST AND SRC { $$ = Q_AND; }
279 /* address type qualifiers */
280 aqual: HOST { $$ = Q_HOST; }
281 | NET { $$ = Q_NET; }
282 | PORT { $$ = Q_PORT; }
283 | PORTRANGE { $$ = Q_PORTRANGE; }
285 /* non-directional address type qualifiers */
286 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
288 pname: LINK { $$ = Q_LINK; }
290 | ARP { $$ = Q_ARP; }
291 | RARP { $$ = Q_RARP; }
292 | SCTP { $$ = Q_SCTP; }
293 | TCP { $$ = Q_TCP; }
294 | UDP { $$ = Q_UDP; }
295 | ICMP { $$ = Q_ICMP; }
296 | IGMP { $$ = Q_IGMP; }
297 | IGRP { $$ = Q_IGRP; }
298 | PIM { $$ = Q_PIM; }
299 | VRRP { $$ = Q_VRRP; }
300 | ATALK { $$ = Q_ATALK; }
301 | AARP { $$ = Q_AARP; }
302 | DECNET { $$ = Q_DECNET; }
303 | LAT { $$ = Q_LAT; }
304 | SCA { $$ = Q_SCA; }
305 | MOPDL { $$ = Q_MOPDL; }
306 | MOPRC { $$ = Q_MOPRC; }
307 | IPV6 { $$ = Q_IPV6; }
308 | ICMPV6 { $$ = Q_ICMPV6; }
310 | ESP { $$ = Q_ESP; }
311 | ISO { $$ = Q_ISO; }
312 | ESIS { $$ = Q_ESIS; }
313 | ISIS { $$ = Q_ISIS; }
314 | L1 { $$ = Q_ISIS_L1; }
315 | L2 { $$ = Q_ISIS_L2; }
316 | IIH { $$ = Q_ISIS_IIH; }
317 | LSP { $$ = Q_ISIS_LSP; }
318 | SNP { $$ = Q_ISIS_SNP; }
319 | PSNP { $$ = Q_ISIS_PSNP; }
320 | CSNP { $$ = Q_ISIS_CSNP; }
321 | CLNP { $$ = Q_CLNP; }
322 | STP { $$ = Q_STP; }
323 | IPX { $$ = Q_IPX; }
324 | NETBEUI { $$ = Q_NETBEUI; }
325 | RADIO { $$ = Q_RADIO; }
327 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
328 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
329 | LESS NUM { $$ = gen_less($2); }
330 | GREATER NUM { $$ = gen_greater($2); }
331 | CBYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
332 | INBOUND { $$ = gen_inbound(0); }
333 | OUTBOUND { $$ = gen_inbound(1); }
334 | VLAN pnum { $$ = gen_vlan($2); }
335 | VLAN { $$ = gen_vlan(-1); }
336 | MPLS pnum { $$ = gen_mpls($2); }
337 | MPLS { $$ = gen_mpls(-1); }
338 | PPPOED { $$ = gen_pppoed(); }
339 | PPPOES { $$ = gen_pppoes(); }
343 pfvar: PF_IFNAME ID { $$ = gen_pf_ifname($2); }
344 | PF_RSET ID { $$ = gen_pf_ruleset($2); }
345 | PF_RNR NUM { $$ = gen_pf_rnr($2); }
346 | PF_SRNR NUM { $$ = gen_pf_srnr($2); }
347 | PF_REASON reason { $$ = gen_pf_reason($2); }
348 | PF_ACTION action { $$ = gen_pf_action($2); }
351 reason: NUM { $$ = $1; }
352 | ID { const char *reasons[] = PFRES_NAMES;
354 for (i = 0; reasons[i]; i++) {
355 if (pcap_strcasecmp($1, reasons[i]) == 0) {
360 if (reasons[i] == NULL)
361 bpf_error("unknown PF reason");
365 action: ID { if (pcap_strcasecmp($1, "pass") == 0 ||
366 pcap_strcasecmp($1, "accept") == 0)
368 else if (pcap_strcasecmp($1, "drop") == 0 ||
369 pcap_strcasecmp($1, "block") == 0)
372 bpf_error("unknown PF action");
376 relop: '>' { $$ = BPF_JGT; }
377 | GEQ { $$ = BPF_JGE; }
378 | '=' { $$ = BPF_JEQ; }
380 irelop: LEQ { $$ = BPF_JGT; }
381 | '<' { $$ = BPF_JGE; }
382 | NEQ { $$ = BPF_JEQ; }
384 arth: pnum { $$ = gen_loadi($1); }
387 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
388 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
389 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
390 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
391 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
392 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
393 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
394 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
395 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
396 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
397 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
398 | paren narth ')' { $$ = $2; }
399 | LEN { $$ = gen_loadlen(); }
401 byteop: '&' { $$ = '&'; }
408 | paren pnum ')' { $$ = $2; }
410 atmtype: LANE { $$ = A_LANE; }
411 | LLC { $$ = A_LLC; }
412 | METAC { $$ = A_METAC; }
413 | BCC { $$ = A_BCC; }
414 | OAMF4EC { $$ = A_OAMF4EC; }
415 | OAMF4SC { $$ = A_OAMF4SC; }
417 | ILMIC { $$ = A_ILMIC; }
419 atmmultitype: OAM { $$ = A_OAM; }
420 | OAMF4 { $$ = A_OAMF4; }
421 | CONNECTMSG { $$ = A_CONNECTMSG; }
422 | METACONNECT { $$ = A_METACONNECT; }
424 /* ATM field types quantifier */
425 atmfield: VPI { $$.atmfieldtype = A_VPI; }
426 | VCI { $$.atmfieldtype = A_VCI; }
428 atmvalue: atmfieldvalue
429 | relop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 0); }
430 | irelop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 1); }
431 | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
434 $$.atmfieldtype = $<blk>0.atmfieldtype;
435 if ($$.atmfieldtype == A_VPI ||
436 $$.atmfieldtype == A_VCI)
437 $$.b = gen_atmfield_code($$.atmfieldtype, (bpf_int32) $1, BPF_JEQ, 0);
440 atmlistvalue: atmfieldvalue
441 | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
443 /* MTP3 field types quantifier */
444 mtp3field: SIO { $$.mtp3fieldtype = M_SIO; }
445 | OPC { $$.mtp3fieldtype = M_OPC; }
446 | DPC { $$.mtp3fieldtype = M_DPC; }
447 | SLS { $$.mtp3fieldtype = M_SLS; }
449 mtp3value: mtp3fieldvalue
450 | relop NUM { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 0); }
451 | irelop NUM { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 1); }
452 | paren mtp3listvalue ')' { $$.b = $2.b; $$.q = qerr; }
454 mtp3fieldvalue: NUM {
455 $$.mtp3fieldtype = $<blk>0.mtp3fieldtype;
456 if ($$.mtp3fieldtype == M_SIO ||
457 $$.mtp3fieldtype == M_OPC ||
458 $$.mtp3fieldtype == M_DPC ||
459 $$.mtp3fieldtype == M_SLS )
460 $$.b = gen_mtp3field_code($$.mtp3fieldtype, (u_int) $1, BPF_JEQ, 0);
463 mtp3listvalue: mtp3fieldvalue
464 | mtp3listvalue or mtp3fieldvalue { gen_or($1.b, $3.b); $$ = $3; }