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.8 2007/06/11 09:52: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 #include <netproto/802_11/ieee80211.h>
60 #ifdef HAVE_OS_PROTO_H
64 #define QSET(q, p, d, a) (q).proto = (p),\
68 static const char *ieee80211_mgt_names[] = IEEE80211_MGT_SUBTYPE_NAMES;
69 static const char *ieee80211_ctl_names[] = IEEE80211_CTL_SUBTYPE_NAMES;
70 static const char *ieee80211_data_names[] = IEEE80211_DATA_SUBTYPE_NAMES;
74 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
77 yyerror(const char *msg)
112 %type <blk> expr id nid pid term rterm qid
114 %type <i> pqual dqual aqual ndaqual
116 %type <i> byteop pname pnum relop irelop
117 %type <blk> and or paren not null prog
118 %type <rblk> other pfvar p80211
119 %type <i> atmtype atmmultitype
121 %type <blk> atmfieldvalue atmvalue atmlistvalue
123 %type <blk> mtp3field
124 %type <blk> mtp3fieldvalue mtp3value mtp3listvalue
127 %token DST SRC HOST GATEWAY
128 %token NET NETMASK PORT PORTRANGE LESS GREATER PROTO PROTOCHAIN CBYTE
129 %token ARP RARP IP SCTP TCP UDP ICMP IGMP IGRP PIM VRRP
130 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
131 %token TK_BROADCAST TK_MULTICAST
132 %token NUM INBOUND OUTBOUND
133 %token PF_IFNAME PF_RSET PF_RNR PF_SRNR PF_REASON PF_ACTION
136 %token ID EID HID HID6 AID
139 %token IPV6 ICMPV6 AH ESP
142 %token ISO ESIS CLNP ISIS L1 L2 IIH LSP SNP CSNP PSNP
146 %token LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
147 %token OAM OAMF4 CONNECTMSG METACONNECT
151 %token SIO OPC DPC SLS
158 %type <i> NUM action reason type subtype type_subtype
175 null: /* null */ { $$.q = qerr; }
178 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
179 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
180 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
181 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
183 and: AND { $$ = $<blk>0; }
185 or: OR { $$ = $<blk>0; }
188 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
190 | paren pid ')' { $$ = $2; }
192 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
193 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
195 | HID NETMASK HID { $$.b = gen_mcode($1, $3, 0,
198 /* Decide how to parse HID based on proto */
200 $$.b = gen_ncode($1, 0, $$.q);
204 $$.b = gen_mcode6($1, NULL, $3,
207 bpf_error("'ip6addr/prefixlen' not supported "
208 "in this configuration");
213 $$.b = gen_mcode6($1, 0, 128,
216 bpf_error("'ip6addr' not supported "
217 "in this configuration");
221 $$.b = gen_ecode($1, $$.q = $<blk>0.q);
223 * $1 was allocated by "pcap_ether_aton()",
224 * so we must free it now that we're done
230 $$.b = gen_acode($1, $$.q = $<blk>0.q);
232 * $1 was allocated by "pcap_ether_aton()",
233 * so we must free it now that we're done
238 | not id { gen_not($2.b); $$ = $2; }
240 not: '!' { $$ = $<blk>0; }
242 paren: '(' { $$ = $<blk>0; }
245 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
246 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
248 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
253 | not term { gen_not($2.b); $$ = $2; }
255 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
256 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
257 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
258 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
259 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
260 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
262 rterm: head id { $$ = $2; }
263 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
264 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
265 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
267 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
269 | other { $$.b = $1; $$.q = qerr; }
270 | atmtype { $$.b = gen_atmtype_abbrev($1); $$.q = qerr; }
271 | atmmultitype { $$.b = gen_atmmulti_abbrev($1); $$.q = qerr; }
272 | atmfield atmvalue { $$.b = $2.b; $$.q = qerr; }
273 | mtp2type { $$.b = gen_mtp2type_abbrev($1); $$.q = qerr; }
274 | mtp3field mtp3value { $$.b = $2.b; $$.q = qerr; }
276 /* protocol level qualifiers */
278 | { $$ = Q_DEFAULT; }
280 /* 'direction' qualifiers */
281 dqual: SRC { $$ = Q_SRC; }
282 | DST { $$ = Q_DST; }
283 | SRC OR DST { $$ = Q_OR; }
284 | DST OR SRC { $$ = Q_OR; }
285 | SRC AND DST { $$ = Q_AND; }
286 | DST AND SRC { $$ = Q_AND; }
288 /* address type qualifiers */
289 aqual: HOST { $$ = Q_HOST; }
290 | NET { $$ = Q_NET; }
291 | PORT { $$ = Q_PORT; }
292 | PORTRANGE { $$ = Q_PORTRANGE; }
294 /* non-directional address type qualifiers */
295 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
297 pname: LINK { $$ = Q_LINK; }
299 | ARP { $$ = Q_ARP; }
300 | RARP { $$ = Q_RARP; }
301 | SCTP { $$ = Q_SCTP; }
302 | TCP { $$ = Q_TCP; }
303 | UDP { $$ = Q_UDP; }
304 | ICMP { $$ = Q_ICMP; }
305 | IGMP { $$ = Q_IGMP; }
306 | IGRP { $$ = Q_IGRP; }
307 | PIM { $$ = Q_PIM; }
308 | VRRP { $$ = Q_VRRP; }
309 | ATALK { $$ = Q_ATALK; }
310 | AARP { $$ = Q_AARP; }
311 | DECNET { $$ = Q_DECNET; }
312 | LAT { $$ = Q_LAT; }
313 | SCA { $$ = Q_SCA; }
314 | MOPDL { $$ = Q_MOPDL; }
315 | MOPRC { $$ = Q_MOPRC; }
316 | IPV6 { $$ = Q_IPV6; }
317 | ICMPV6 { $$ = Q_ICMPV6; }
319 | ESP { $$ = Q_ESP; }
320 | ISO { $$ = Q_ISO; }
321 | ESIS { $$ = Q_ESIS; }
322 | ISIS { $$ = Q_ISIS; }
323 | L1 { $$ = Q_ISIS_L1; }
324 | L2 { $$ = Q_ISIS_L2; }
325 | IIH { $$ = Q_ISIS_IIH; }
326 | LSP { $$ = Q_ISIS_LSP; }
327 | SNP { $$ = Q_ISIS_SNP; }
328 | PSNP { $$ = Q_ISIS_PSNP; }
329 | CSNP { $$ = Q_ISIS_CSNP; }
330 | CLNP { $$ = Q_CLNP; }
331 | STP { $$ = Q_STP; }
332 | IPX { $$ = Q_IPX; }
333 | NETBEUI { $$ = Q_NETBEUI; }
334 | RADIO { $$ = Q_RADIO; }
336 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
337 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
338 | LESS NUM { $$ = gen_less($2); }
339 | GREATER NUM { $$ = gen_greater($2); }
340 | CBYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
341 | INBOUND { $$ = gen_inbound(0); }
342 | OUTBOUND { $$ = gen_inbound(1); }
343 | VLAN pnum { $$ = gen_vlan($2); }
344 | VLAN { $$ = gen_vlan(-1); }
345 | MPLS pnum { $$ = gen_mpls($2); }
346 | MPLS { $$ = gen_mpls(-1); }
347 | PPPOED { $$ = gen_pppoed(); }
348 | PPPOES { $$ = gen_pppoes(); }
350 | pqual p80211 { $$ = $2; }
353 pfvar: PF_IFNAME ID { $$ = gen_pf_ifname($2); }
354 | PF_RSET ID { $$ = gen_pf_ruleset($2); }
355 | PF_RNR NUM { $$ = gen_pf_rnr($2); }
356 | PF_SRNR NUM { $$ = gen_pf_srnr($2); }
357 | PF_REASON reason { $$ = gen_pf_reason($2); }
358 | PF_ACTION action { $$ = gen_pf_action($2); }
361 p80211: TYPE type SUBTYPE subtype
362 { $$ = gen_p80211_type($2 | $4,
363 IEEE80211_FC0_TYPE_MASK |
364 IEEE80211_FC0_SUBTYPE_MASK);
366 | TYPE type { $$ = gen_p80211_type($2,
367 IEEE80211_FC0_TYPE_MASK);
369 | SUBTYPE type_subtype { $$ = gen_p80211_type($2,
370 IEEE80211_FC0_TYPE_MASK |
371 IEEE80211_FC0_SUBTYPE_MASK);
376 | ID { const char *names[] = IEEE80211_TYPE_NAMES;
378 lim = (IEEE80211_FC0_TYPE_MASK >> IEEE80211_FC0_TYPE_SHIFT) + 1;
379 for (i = 0; i < lim; ++i) {
380 if (pcap_strcasecmp($1, names[i]) == 0) {
381 $$ = i << IEEE80211_FC0_TYPE_SHIFT;
386 bpf_error("unknown 802.11 type name");
391 | ID { const char **names;
393 if ($<i>-1 == IEEE80211_FC0_TYPE_MGT)
394 names = ieee80211_mgt_names;
395 else if ($<i>-1 == IEEE80211_FC0_TYPE_CTL)
396 names = ieee80211_ctl_names;
397 else if ($<i>-1 == IEEE80211_FC0_TYPE_DATA)
398 names = ieee80211_data_names;
400 bpf_error("unknown 802.11 type");
401 lim = (IEEE80211_FC0_SUBTYPE_MASK >> IEEE80211_FC0_SUBTYPE_SHIFT) + 1;
402 for (i = 0; i < lim; ++i) {
403 if (pcap_strcasecmp($1, names[i]) == 0) {
404 $$ = i << IEEE80211_FC0_SUBTYPE_SHIFT;
409 bpf_error("unknown 802.11 subtype name");
413 type_subtype: ID { const char **sub_names[] = {
418 int i, j, lim, sub_lim;
419 sub_lim = sizeof(sub_names) / sizeof(sub_names[0]);
420 lim = (IEEE80211_FC0_SUBTYPE_MASK >> IEEE80211_FC0_SUBTYPE_SHIFT) + 1;
421 for (i = 0; i < sub_lim; ++i) {
422 const char **names = sub_names[i];
423 for (j = 0; j < lim; ++j) {
424 if (pcap_strcasecmp($1, names[j]) == 0)
428 $$ = (i << IEEE80211_FC0_TYPE_SHIFT) |
429 (j << IEEE80211_FC0_SUBTYPE_SHIFT);
434 bpf_error("unknown 802.11 subtype name");
438 reason: NUM { $$ = $1; }
439 | ID { const char *reasons[] = PFRES_NAMES;
441 for (i = 0; reasons[i]; i++) {
442 if (pcap_strcasecmp($1, reasons[i]) == 0) {
447 if (reasons[i] == NULL)
448 bpf_error("unknown PF reason");
452 action: ID { if (pcap_strcasecmp($1, "pass") == 0 ||
453 pcap_strcasecmp($1, "accept") == 0)
455 else if (pcap_strcasecmp($1, "drop") == 0 ||
456 pcap_strcasecmp($1, "block") == 0)
459 bpf_error("unknown PF action");
463 relop: '>' { $$ = BPF_JGT; }
464 | GEQ { $$ = BPF_JGE; }
465 | '=' { $$ = BPF_JEQ; }
467 irelop: LEQ { $$ = BPF_JGT; }
468 | '<' { $$ = BPF_JGE; }
469 | NEQ { $$ = BPF_JEQ; }
471 arth: pnum { $$ = gen_loadi($1); }
474 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
475 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
476 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
477 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
478 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
479 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
480 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
481 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
482 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
483 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
484 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
485 | paren narth ')' { $$ = $2; }
486 | LEN { $$ = gen_loadlen(); }
488 byteop: '&' { $$ = '&'; }
495 | paren pnum ')' { $$ = $2; }
497 atmtype: LANE { $$ = A_LANE; }
498 | LLC { $$ = A_LLC; }
499 | METAC { $$ = A_METAC; }
500 | BCC { $$ = A_BCC; }
501 | OAMF4EC { $$ = A_OAMF4EC; }
502 | OAMF4SC { $$ = A_OAMF4SC; }
504 | ILMIC { $$ = A_ILMIC; }
506 atmmultitype: OAM { $$ = A_OAM; }
507 | OAMF4 { $$ = A_OAMF4; }
508 | CONNECTMSG { $$ = A_CONNECTMSG; }
509 | METACONNECT { $$ = A_METACONNECT; }
511 /* ATM field types quantifier */
512 atmfield: VPI { $$.atmfieldtype = A_VPI; }
513 | VCI { $$.atmfieldtype = A_VCI; }
515 atmvalue: atmfieldvalue
516 | relop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 0); }
517 | irelop NUM { $$.b = gen_atmfield_code($<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 1); }
518 | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
521 $$.atmfieldtype = $<blk>0.atmfieldtype;
522 if ($$.atmfieldtype == A_VPI ||
523 $$.atmfieldtype == A_VCI)
524 $$.b = gen_atmfield_code($$.atmfieldtype, (bpf_int32) $1, BPF_JEQ, 0);
527 atmlistvalue: atmfieldvalue
528 | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
530 /* MTP2 types quantifier */
531 mtp2type: FISU { $$ = M_FISU; }
532 | LSSU { $$ = M_LSSU; }
533 | MSU { $$ = M_MSU; }
535 /* MTP3 field types quantifier */
536 mtp3field: SIO { $$.mtp3fieldtype = M_SIO; }
537 | OPC { $$.mtp3fieldtype = M_OPC; }
538 | DPC { $$.mtp3fieldtype = M_DPC; }
539 | SLS { $$.mtp3fieldtype = M_SLS; }
541 mtp3value: mtp3fieldvalue
542 | relop NUM { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 0); }
543 | irelop NUM { $$.b = gen_mtp3field_code($<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 1); }
544 | paren mtp3listvalue ')' { $$.b = $2.b; $$.q = qerr; }
546 mtp3fieldvalue: NUM {
547 $$.mtp3fieldtype = $<blk>0.mtp3fieldtype;
548 if ($$.mtp3fieldtype == M_SIO ||
549 $$.mtp3fieldtype == M_OPC ||
550 $$.mtp3fieldtype == M_DPC ||
551 $$.mtp3fieldtype == M_SLS )
552 $$.b = gen_mtp3field_code($$.mtp3fieldtype, (u_int) $1, BPF_JEQ, 0);
555 mtp3listvalue: mtp3fieldvalue
556 | mtp3listvalue or mtp3fieldvalue { gen_or($1.b, $3.b); $$ = $3; }