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.9 2007/09/12 19:17:25 guy Exp $ (LBL)";
33 #include <pcap-stdinc.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
47 #include <netinet/in.h>
55 #ifdef HAVE_NET_PFVAR_H
57 #include <net/if_var.h>
58 #include <net/pf/pfvar.h>
59 #include <net/pf/if_pflog.h>
61 #include <pcap-namedb.h>
63 #include <netproto/802_11/ieee80211.h>
65 #ifdef HAVE_OS_PROTO_H
69 #define QSET(q, p, d, a) (q).proto = (p),\
73 static const char *ieee80211_mgt_names
[] = IEEE80211_MGT_SUBTYPE_NAMES
;
74 static const char *ieee80211_ctl_names
[] = IEEE80211_CTL_SUBTYPE_NAMES
;
75 static const char *ieee80211_data_names
[] = IEEE80211_DATA_SUBTYPE_NAMES
;
79 static struct qual qerr
= { Q_UNDEF
, Q_UNDEF
, Q_UNDEF
, Q_UNDEF
};
82 yyerror(const char *msg
)
99 #ifdef HAVE_NET_PFVAR_H
101 pfreason_to_num
(const char *reason
)
103 const char *reasons
[] = PFRES_NAMES
;
106 for
(i
= 0; reasons
[i
]; i
++) {
107 if
(pcap_strcasecmp
(reason
, reasons
[i
]) == 0)
110 bpf_error
("unknown PF reason");
115 pfaction_to_num
(const char *action
)
117 if
(pcap_strcasecmp
(action
, "pass") == 0 ||
118 pcap_strcasecmp
(action
, "accept") == 0)
120 else if
(pcap_strcasecmp
(action
, "drop") == 0 ||
121 pcap_strcasecmp
(action
, "block") == 0)
124 bpf_error
("unknown PF action");
128 #else /* !HAVE_NET_PFVAR_H */
130 pfreason_to_num
(const char *reason
)
132 bpf_error
("libpcap was compiled on a machine without pf support");
137 pfaction_to_num
(const char *action
)
139 bpf_error
("libpcap was compiled on a machine without pf support");
142 #endif /* HAVE_NET_PFVAR_H */
161 %type
<blk
> expr id nid pid term rterm qid
163 %type
<i
> pqual dqual aqual ndaqual
165 %type
<i
> byteop pname pnum relop irelop
166 %type
<blk
> and or paren not null prog
167 %type
<rblk
> other pfvar p80211
168 %type
<i
> atmtype atmmultitype
170 %type
<blk
> atmfieldvalue atmvalue atmlistvalue
172 %type
<blk
> mtp3field
173 %type
<blk
> mtp3fieldvalue mtp3value mtp3listvalue
176 %token DST SRC HOST GATEWAY
177 %token NET NETMASK PORT PORTRANGE LESS GREATER PROTO PROTOCHAIN CBYTE
178 %token ARP RARP IP SCTP TCP UDP ICMP IGMP IGRP PIM VRRP
179 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
180 %token TK_BROADCAST TK_MULTICAST
181 %token NUM INBOUND OUTBOUND
182 %token PF_IFNAME PF_RSET PF_RNR PF_SRNR PF_REASON PF_ACTION
185 %token ID EID HID HID6 AID
188 %token IPV6 ICMPV6 AH ESP
191 %token ISO ESIS CLNP ISIS L1 L2 IIH LSP SNP CSNP PSNP
195 %token LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
196 %token OAM OAMF4 CONNECTMSG METACONNECT
200 %token SIO OPC DPC SLS
207 %type
<i
> NUM action reason type subtype type_subtype
224 null: /* null */ { $$.q
= qerr
; }
227 | expr and term
{ gen_and
($1.b
, $3.b
); $$
= $3; }
228 | expr and id
{ gen_and
($1.b
, $3.b
); $$
= $3; }
229 | expr or term
{ gen_or
($1.b
, $3.b
); $$
= $3; }
230 | expr or id
{ gen_or
($1.b
, $3.b
); $$
= $3; }
232 and: AND
{ $$
= $
<blk
>0; }
234 or: OR
{ $$
= $
<blk
>0; }
237 | pnum
{ $$.b
= gen_ncode
(NULL
, (bpf_u_int32
)$1,
239 | paren pid
')' { $$
= $2; }
241 nid: ID
{ $$.b
= gen_scode
($1, $$.q
= $
<blk
>0.q
); }
242 | HID
'/' NUM
{ $$.b
= gen_mcode
($1, NULL
, $3,
244 | HID NETMASK HID
{ $$.b
= gen_mcode
($1, $3, 0,
247 /* Decide how to parse HID based on proto */
249 $$.b
= gen_ncode
($1, 0, $$.q
);
253 $$.b
= gen_mcode6
($1, NULL
, $3,
256 bpf_error
("'ip6addr/prefixlen' not supported "
257 "in this configuration");
262 $$.b
= gen_mcode6
($1, 0, 128,
265 bpf_error
("'ip6addr' not supported "
266 "in this configuration");
270 $$.b
= gen_ecode
($1, $$.q
= $
<blk
>0.q
);
272 * $1 was allocated by "pcap_ether_aton()",
273 * so we must free it now that we're done
279 $$.b
= gen_acode
($1, $$.q
= $
<blk
>0.q
);
281 * $1 was allocated by "pcap_ether_aton()",
282 * so we must free it now that we're done
287 | not id
{ gen_not
($2.b
); $$
= $2; }
289 not: '!' { $$
= $
<blk
>0; }
291 paren: '(' { $$
= $
<blk
>0; }
294 | qid and id
{ gen_and
($1.b
, $3.b
); $$
= $3; }
295 | qid or id
{ gen_or
($1.b
, $3.b
); $$
= $3; }
297 qid: pnum
{ $$.b
= gen_ncode
(NULL
, (bpf_u_int32
)$1,
302 | not term
{ gen_not
($2.b
); $$
= $2; }
304 head: pqual dqual aqual
{ QSET
($$.q
, $1, $2, $3); }
305 | pqual dqual
{ QSET
($$.q
, $1, $2, Q_DEFAULT
); }
306 | pqual aqual
{ QSET
($$.q
, $1, Q_DEFAULT
, $2); }
307 | pqual PROTO
{ QSET
($$.q
, $1, Q_DEFAULT
, Q_PROTO
); }
308 | pqual PROTOCHAIN
{ QSET
($$.q
, $1, Q_DEFAULT
, Q_PROTOCHAIN
); }
309 | pqual ndaqual
{ QSET
($$.q
, $1, Q_DEFAULT
, $2); }
311 rterm: head id
{ $$
= $2; }
312 | paren expr
')' { $$.b
= $2.b
; $$.q
= $1.q
; }
313 | pname
{ $$.b
= gen_proto_abbrev
($1); $$.q
= qerr
; }
314 | arth relop arth
{ $$.b
= gen_relation
($2, $1, $3, 0);
316 | arth irelop arth
{ $$.b
= gen_relation
($2, $1, $3, 1);
318 | other
{ $$.b
= $1; $$.q
= qerr
; }
319 | atmtype
{ $$.b
= gen_atmtype_abbrev
($1); $$.q
= qerr
; }
320 | atmmultitype
{ $$.b
= gen_atmmulti_abbrev
($1); $$.q
= qerr
; }
321 | atmfield atmvalue
{ $$.b
= $2.b
; $$.q
= qerr
; }
322 | mtp2type
{ $$.b
= gen_mtp2type_abbrev
($1); $$.q
= qerr
; }
323 | mtp3field mtp3value
{ $$.b
= $2.b
; $$.q
= qerr
; }
325 /* protocol level qualifiers */
327 |
{ $$
= Q_DEFAULT
; }
329 /* 'direction' qualifiers */
330 dqual: SRC
{ $$
= Q_SRC
; }
331 | DST
{ $$
= Q_DST
; }
332 | SRC OR DST
{ $$
= Q_OR
; }
333 | DST OR SRC
{ $$
= Q_OR
; }
334 | SRC AND DST
{ $$
= Q_AND
; }
335 | DST AND SRC
{ $$
= Q_AND
; }
337 /* address type qualifiers */
338 aqual: HOST
{ $$
= Q_HOST
; }
339 | NET
{ $$
= Q_NET
; }
340 | PORT
{ $$
= Q_PORT
; }
341 | PORTRANGE
{ $$
= Q_PORTRANGE
; }
343 /* non-directional address type qualifiers */
344 ndaqual: GATEWAY
{ $$
= Q_GATEWAY
; }
346 pname: LINK
{ $$
= Q_LINK
; }
348 | ARP
{ $$
= Q_ARP
; }
349 | RARP
{ $$
= Q_RARP
; }
350 | SCTP
{ $$
= Q_SCTP
; }
351 | TCP
{ $$
= Q_TCP
; }
352 | UDP
{ $$
= Q_UDP
; }
353 | ICMP
{ $$
= Q_ICMP
; }
354 | IGMP
{ $$
= Q_IGMP
; }
355 | IGRP
{ $$
= Q_IGRP
; }
356 | PIM
{ $$
= Q_PIM
; }
357 | VRRP
{ $$
= Q_VRRP
; }
358 | ATALK
{ $$
= Q_ATALK
; }
359 | AARP
{ $$
= Q_AARP
; }
360 | DECNET
{ $$
= Q_DECNET
; }
361 | LAT
{ $$
= Q_LAT
; }
362 | SCA
{ $$
= Q_SCA
; }
363 | MOPDL
{ $$
= Q_MOPDL
; }
364 | MOPRC
{ $$
= Q_MOPRC
; }
365 | IPV6
{ $$
= Q_IPV6
; }
366 | ICMPV6
{ $$
= Q_ICMPV6
; }
368 | ESP
{ $$
= Q_ESP
; }
369 | ISO
{ $$
= Q_ISO
; }
370 | ESIS
{ $$
= Q_ESIS
; }
371 | ISIS
{ $$
= Q_ISIS
; }
372 | L1
{ $$
= Q_ISIS_L1
; }
373 | L2
{ $$
= Q_ISIS_L2
; }
374 | IIH
{ $$
= Q_ISIS_IIH
; }
375 | LSP
{ $$
= Q_ISIS_LSP
; }
376 | SNP
{ $$
= Q_ISIS_SNP
; }
377 | PSNP
{ $$
= Q_ISIS_PSNP
; }
378 | CSNP
{ $$
= Q_ISIS_CSNP
; }
379 | CLNP
{ $$
= Q_CLNP
; }
380 | STP
{ $$
= Q_STP
; }
381 | IPX
{ $$
= Q_IPX
; }
382 | NETBEUI
{ $$
= Q_NETBEUI
; }
383 | RADIO
{ $$
= Q_RADIO
; }
385 other: pqual TK_BROADCAST
{ $$
= gen_broadcast
($1); }
386 | pqual TK_MULTICAST
{ $$
= gen_multicast
($1); }
387 | LESS NUM
{ $$
= gen_less
($2); }
388 | GREATER NUM
{ $$
= gen_greater
($2); }
389 | CBYTE NUM byteop NUM
{ $$
= gen_byteop
($3, $2, $4); }
390 | INBOUND
{ $$
= gen_inbound
(0); }
391 | OUTBOUND
{ $$
= gen_inbound
(1); }
392 | VLAN pnum
{ $$
= gen_vlan
($2); }
393 | VLAN
{ $$
= gen_vlan
(-1); }
394 | MPLS pnum
{ $$
= gen_mpls
($2); }
395 | MPLS
{ $$
= gen_mpls
(-1); }
396 | PPPOED
{ $$
= gen_pppoed
(); }
397 | PPPOES
{ $$
= gen_pppoes
(); }
399 | pqual p80211
{ $$
= $2; }
402 pfvar: PF_IFNAME ID
{ $$
= gen_pf_ifname
($2); }
403 | PF_RSET ID
{ $$
= gen_pf_ruleset
($2); }
404 | PF_RNR NUM
{ $$
= gen_pf_rnr
($2); }
405 | PF_SRNR NUM
{ $$
= gen_pf_srnr
($2); }
406 | PF_REASON reason
{ $$
= gen_pf_reason
($2); }
407 | PF_ACTION action
{ $$
= gen_pf_action
($2); }
410 p80211: TYPE type SUBTYPE subtype
411 { $$
= gen_p80211_type
($2 |
$4,
412 IEEE80211_FC0_TYPE_MASK |
413 IEEE80211_FC0_SUBTYPE_MASK
);
415 | TYPE type
{ $$
= gen_p80211_type
($2,
416 IEEE80211_FC0_TYPE_MASK
);
418 | SUBTYPE type_subtype
{ $$
= gen_p80211_type
($2,
419 IEEE80211_FC0_TYPE_MASK |
420 IEEE80211_FC0_SUBTYPE_MASK
);
425 | ID
{ const char *names
[] = IEEE80211_TYPE_NAMES
;
427 lim
= (IEEE80211_FC0_TYPE_MASK
>> IEEE80211_FC0_TYPE_SHIFT
) + 1;
428 for
(i
= 0; i
< lim
; ++i
) {
429 if
(pcap_strcasecmp
($1, names
[i
]) == 0) {
430 $$
= i
<< IEEE80211_FC0_TYPE_SHIFT
;
435 bpf_error
("unknown 802.11 type name");
440 | ID
{ const char **names
;
442 if
($
<i
>-1 == IEEE80211_FC0_TYPE_MGT
)
443 names
= ieee80211_mgt_names
;
444 else if
($
<i
>-1 == IEEE80211_FC0_TYPE_CTL
)
445 names
= ieee80211_ctl_names
;
446 else if
($
<i
>-1 == IEEE80211_FC0_TYPE_DATA
)
447 names
= ieee80211_data_names
;
449 bpf_error
("unknown 802.11 type");
450 lim
= (IEEE80211_FC0_SUBTYPE_MASK
>> IEEE80211_FC0_SUBTYPE_SHIFT
) + 1;
451 for
(i
= 0; i
< lim
; ++i
) {
452 if
(pcap_strcasecmp
($1, names
[i
]) == 0) {
453 $$
= i
<< IEEE80211_FC0_SUBTYPE_SHIFT
;
458 bpf_error
("unknown 802.11 subtype name");
462 type_subtype: ID
{ const char **sub_names
[] = {
467 int i
, j
, lim
, sub_lim
;
468 sub_lim
= sizeof
(sub_names
) / sizeof
(sub_names
[0]);
469 lim
= (IEEE80211_FC0_SUBTYPE_MASK
>> IEEE80211_FC0_SUBTYPE_SHIFT
) + 1;
470 for
(i
= 0; i
< sub_lim
; ++i
) {
471 const char **names
= sub_names
[i
];
472 for
(j
= 0; j
< lim
; ++j
) {
473 if
(pcap_strcasecmp
($1, names
[j
]) == 0)
477 $$
= (i
<< IEEE80211_FC0_TYPE_SHIFT
) |
478 (j
<< IEEE80211_FC0_SUBTYPE_SHIFT
);
483 bpf_error
("unknown 802.11 subtype name");
487 reason: NUM
{ $$
= $1; }
488 | ID
{ $$
= pfreason_to_num
($1); }
491 action: ID
{ $$
= pfaction_to_num
($1); }
494 relop: '>' { $$
= BPF_JGT
; }
495 | GEQ
{ $$
= BPF_JGE
; }
496 |
'=' { $$
= BPF_JEQ
; }
498 irelop: LEQ
{ $$
= BPF_JGT
; }
499 |
'<' { $$
= BPF_JGE
; }
500 | NEQ
{ $$
= BPF_JEQ
; }
502 arth: pnum
{ $$
= gen_loadi
($1); }
505 narth: pname
'[' arth
']' { $$
= gen_load
($1, $3, 1); }
506 | pname
'[' arth
':' NUM
']' { $$
= gen_load
($1, $3, $5); }
507 | arth
'+' arth
{ $$
= gen_arth
(BPF_ADD
, $1, $3); }
508 | arth
'-' arth
{ $$
= gen_arth
(BPF_SUB
, $1, $3); }
509 | arth
'*' arth
{ $$
= gen_arth
(BPF_MUL
, $1, $3); }
510 | arth
'/' arth
{ $$
= gen_arth
(BPF_DIV
, $1, $3); }
511 | arth
'&' arth
{ $$
= gen_arth
(BPF_AND
, $1, $3); }
512 | arth
'|' arth
{ $$
= gen_arth
(BPF_OR
, $1, $3); }
513 | arth LSH arth
{ $$
= gen_arth
(BPF_LSH
, $1, $3); }
514 | arth RSH arth
{ $$
= gen_arth
(BPF_RSH
, $1, $3); }
515 |
'-' arth %prec UMINUS
{ $$
= gen_neg
($2); }
516 | paren narth
')' { $$
= $2; }
517 | LEN
{ $$
= gen_loadlen
(); }
519 byteop: '&' { $$
= '&'; }
526 | paren pnum
')' { $$
= $2; }
528 atmtype: LANE
{ $$
= A_LANE
; }
529 | LLC
{ $$
= A_LLC
; }
530 | METAC
{ $$
= A_METAC
; }
531 | BCC
{ $$
= A_BCC
; }
532 | OAMF4EC
{ $$
= A_OAMF4EC
; }
533 | OAMF4SC
{ $$
= A_OAMF4SC
; }
535 | ILMIC
{ $$
= A_ILMIC
; }
537 atmmultitype: OAM
{ $$
= A_OAM
; }
538 | OAMF4
{ $$
= A_OAMF4
; }
539 | CONNECTMSG
{ $$
= A_CONNECTMSG
; }
540 | METACONNECT
{ $$
= A_METACONNECT
; }
542 /* ATM field types quantifier */
543 atmfield: VPI
{ $$.atmfieldtype
= A_VPI
; }
544 | VCI
{ $$.atmfieldtype
= A_VCI
; }
546 atmvalue: atmfieldvalue
547 | relop NUM
{ $$.b
= gen_atmfield_code
($
<blk
>0.atmfieldtype
, (bpf_int32
)$2, (bpf_u_int32
)$1, 0); }
548 | irelop NUM
{ $$.b
= gen_atmfield_code
($
<blk
>0.atmfieldtype
, (bpf_int32
)$2, (bpf_u_int32
)$1, 1); }
549 | paren atmlistvalue
')' { $$.b
= $2.b
; $$.q
= qerr
; }
552 $$.atmfieldtype
= $
<blk
>0.atmfieldtype
;
553 if
($$.atmfieldtype
== A_VPI ||
554 $$.atmfieldtype
== A_VCI
)
555 $$.b
= gen_atmfield_code
($$.atmfieldtype
, (bpf_int32
) $1, BPF_JEQ
, 0);
558 atmlistvalue: atmfieldvalue
559 | atmlistvalue or atmfieldvalue
{ gen_or
($1.b
, $3.b
); $$
= $3; }
561 /* MTP2 types quantifier */
562 mtp2type: FISU
{ $$
= M_FISU
; }
563 | LSSU
{ $$
= M_LSSU
; }
564 | MSU
{ $$
= M_MSU
; }
566 /* MTP3 field types quantifier */
567 mtp3field: SIO
{ $$.mtp3fieldtype
= M_SIO
; }
568 | OPC
{ $$.mtp3fieldtype
= M_OPC
; }
569 | DPC
{ $$.mtp3fieldtype
= M_DPC
; }
570 | SLS
{ $$.mtp3fieldtype
= M_SLS
; }
572 mtp3value: mtp3fieldvalue
573 | relop NUM
{ $$.b
= gen_mtp3field_code
($
<blk
>0.mtp3fieldtype
, (u_int
)$2, (u_int
)$1, 0); }
574 | irelop NUM
{ $$.b
= gen_mtp3field_code
($
<blk
>0.mtp3fieldtype
, (u_int
)$2, (u_int
)$1, 1); }
575 | paren mtp3listvalue
')' { $$.b
= $2.b
; $$.q
= qerr
; }
577 mtp3fieldvalue: NUM
{
578 $$.mtp3fieldtype
= $
<blk
>0.mtp3fieldtype
;
579 if
($$.mtp3fieldtype
== M_SIO ||
580 $$.mtp3fieldtype
== M_OPC ||
581 $$.mtp3fieldtype
== M_DPC ||
582 $$.mtp3fieldtype
== M_SLS
)
583 $$.b
= gen_mtp3field_code
($$.mtp3fieldtype
, (u_int
) $1, BPF_JEQ
, 0);
586 mtp3listvalue: mtp3fieldvalue
587 | mtp3listvalue or mtp3fieldvalue
{ gen_or
($1.b
, $3.b
); $$
= $3; }