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[] =
25 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.69 2001-04-17 08:25:23 guy Exp $ (LBL)";
32 #include <sys/types.h>
34 #include <sys/socket.h>
44 #include <netinet/in.h>
51 #include <pcap-namedb.h>
53 #ifdef HAVE_OS_PROTO_H
57 #define QSET(q, p, d, a) (q).proto = (p),\
63 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
99 %type <blk> expr id nid pid term rterm qid
101 %type <i> pqual dqual aqual ndaqual
103 %type <i> byteop pname pnum relop irelop
104 %type <blk> and or paren not null prog
107 %token DST SRC HOST GATEWAY
108 %token NET MASK PORT LESS GREATER PROTO PROTOCHAIN BYTE
109 %token ARP RARP IP TCP UDP ICMP IGMP IGRP PIM VRRP
110 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
111 %token TK_BROADCAST TK_MULTICAST
112 %token NUM INBOUND OUTBOUND
115 %token ID EID HID HID6 AID
118 %token IPV6 ICMPV6 AH ESP
120 %token ISO ESIS ISIS CLNP
146 null: /* null */ { $$.q = qerr; }
149 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
150 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
151 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
152 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
154 and: AND { $$ = $<blk>0; }
156 or: OR { $$ = $<blk>0; }
159 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
161 | paren pid ')' { $$ = $2; }
163 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
164 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
166 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
169 /* Decide how to parse HID based on proto */
171 $$.b = gen_ncode($1, 0, $$.q);
175 $$.b = gen_mcode6($1, NULL, $3,
178 bpf_error("'ip6addr/prefixlen' not supported "
179 "in this configuration");
184 $$.b = gen_mcode6($1, 0, 128,
187 bpf_error("'ip6addr' not supported "
188 "in this configuration");
191 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
192 | AID { $$.b = gen_acode($1, $$.q = $<blk>0.q); }
193 | not id { gen_not($2.b); $$ = $2; }
195 not: '!' { $$ = $<blk>0; }
197 paren: '(' { $$ = $<blk>0; }
200 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
201 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
203 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
208 | not term { gen_not($2.b); $$ = $2; }
210 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
211 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
212 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
213 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
214 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
215 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
217 rterm: head id { $$ = $2; }
218 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
219 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
220 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
222 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
224 | other { $$.b = $1; $$.q = qerr; }
226 /* protocol level qualifiers */
228 | { $$ = Q_DEFAULT; }
230 /* 'direction' qualifiers */
231 dqual: SRC { $$ = Q_SRC; }
232 | DST { $$ = Q_DST; }
233 | SRC OR DST { $$ = Q_OR; }
234 | DST OR SRC { $$ = Q_OR; }
235 | SRC AND DST { $$ = Q_AND; }
236 | DST AND SRC { $$ = Q_AND; }
238 /* address type qualifiers */
239 aqual: HOST { $$ = Q_HOST; }
240 | NET { $$ = Q_NET; }
241 | PORT { $$ = Q_PORT; }
243 /* non-directional address type qualifiers */
244 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
246 pname: LINK { $$ = Q_LINK; }
248 | ARP { $$ = Q_ARP; }
249 | RARP { $$ = Q_RARP; }
250 | TCP { $$ = Q_TCP; }
251 | UDP { $$ = Q_UDP; }
252 | ICMP { $$ = Q_ICMP; }
253 | IGMP { $$ = Q_IGMP; }
254 | IGRP { $$ = Q_IGRP; }
255 | PIM { $$ = Q_PIM; }
256 | VRRP { $$ = Q_VRRP; }
257 | ATALK { $$ = Q_ATALK; }
258 | AARP { $$ = Q_AARP; }
259 | DECNET { $$ = Q_DECNET; }
260 | LAT { $$ = Q_LAT; }
261 | SCA { $$ = Q_SCA; }
262 | MOPDL { $$ = Q_MOPDL; }
263 | MOPRC { $$ = Q_MOPRC; }
264 | IPV6 { $$ = Q_IPV6; }
265 | ICMPV6 { $$ = Q_ICMPV6; }
267 | ESP { $$ = Q_ESP; }
268 | ISO { $$ = Q_ISO; }
269 | ESIS { $$ = Q_ESIS; }
270 | ISIS { $$ = Q_ISIS; }
271 | CLNP { $$ = Q_CLNP; }
272 | STP { $$ = Q_STP; }
273 | IPX { $$ = Q_IPX; }
274 | NETBEUI { $$ = Q_NETBEUI; }
276 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
277 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
278 | LESS NUM { $$ = gen_less($2); }
279 | GREATER NUM { $$ = gen_greater($2); }
280 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
281 | INBOUND { $$ = gen_inbound(0); }
282 | OUTBOUND { $$ = gen_inbound(1); }
283 | VLAN pnum { $$ = gen_vlan($2); }
284 | VLAN { $$ = gen_vlan(-1); }
286 relop: '>' { $$ = BPF_JGT; }
287 | GEQ { $$ = BPF_JGE; }
288 | '=' { $$ = BPF_JEQ; }
290 irelop: LEQ { $$ = BPF_JGT; }
291 | '<' { $$ = BPF_JGE; }
292 | NEQ { $$ = BPF_JEQ; }
294 arth: pnum { $$ = gen_loadi($1); }
297 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
298 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
299 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
300 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
301 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
302 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
303 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
304 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
305 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
306 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
307 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
308 | paren narth ')' { $$ = $2; }
309 | LEN { $$ = gen_loadlen(); }
311 byteop: '&' { $$ = '&'; }
318 | paren pnum ')' { $$ = $2; }