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[] =
26 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.57 1999/10/19 15:18:30 itojun Exp $ (LBL)";
29 #include <sys/types.h>
31 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <net/ethernet.h>
48 #include <pcap-namedb.h>
51 #ifdef HAVE_OS_PROTO_H
55 #define QSET(q, p, d, a) (q).proto = (p),\
61 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
97 %type <blk> expr id nid pid term rterm qid
99 %type <i> pqual dqual aqual ndaqual
101 %type <i> byteop pname pnum relop irelop
102 %type <blk> and or paren not null prog
105 %token DST SRC HOST GATEWAY
106 %token NET MASK PORT LESS GREATER PROTO PROTOCHAIN BYTE
107 %token ARP RARP IP TCP UDP ICMP IGMP IGRP PIM
108 %token ATALK DECNET LAT SCA MOPRC MOPDL
109 %token TK_BROADCAST TK_MULTICAST
110 %token NUM INBOUND OUTBOUND
113 %token ID EID HID HID6
117 %token IPV6 ICMPV6 AH ESP
139 null: /* null */ { $$.q = qerr; }
142 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
143 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
144 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
145 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
147 and: AND { $$ = $<blk>0; }
149 or: OR { $$ = $<blk>0; }
152 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
154 | paren pid ')' { $$ = $2; }
156 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
157 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
159 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
162 /* Decide how to parse HID based on proto */
164 switch ($$.q.proto) {
166 $$.b = gen_ncode($1, 0, $$.q);
169 $$.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 | not id { gen_not($2.b); $$ = $2; }
194 not: '!' { $$ = $<blk>0; }
196 paren: '(' { $$ = $<blk>0; }
199 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
200 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
202 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
207 | not term { gen_not($2.b); $$ = $2; }
209 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
210 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
211 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
212 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
213 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
214 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
216 rterm: head id { $$ = $2; }
217 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
218 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
219 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
221 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
223 | other { $$.b = $1; $$.q = qerr; }
225 /* protocol level qualifiers */
227 | { $$ = Q_DEFAULT; }
229 /* 'direction' qualifiers */
230 dqual: SRC { $$ = Q_SRC; }
231 | DST { $$ = Q_DST; }
232 | SRC OR DST { $$ = Q_OR; }
233 | DST OR SRC { $$ = Q_OR; }
234 | SRC AND DST { $$ = Q_AND; }
235 | DST AND SRC { $$ = Q_AND; }
237 /* address type qualifiers */
238 aqual: HOST { $$ = Q_HOST; }
239 | NET { $$ = Q_NET; }
240 | PORT { $$ = Q_PORT; }
242 /* non-directional address type qualifiers */
243 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
245 pname: LINK { $$ = Q_LINK; }
247 | ARP { $$ = Q_ARP; }
248 | RARP { $$ = Q_RARP; }
249 | TCP { $$ = Q_TCP; }
250 | UDP { $$ = Q_UDP; }
251 | ICMP { $$ = Q_ICMP; }
252 | IGMP { $$ = Q_IGMP; }
253 | IGRP { $$ = Q_IGRP; }
254 | PIM { $$ = Q_PIM; }
255 | ATALK { $$ = Q_ATALK; }
256 | DECNET { $$ = Q_DECNET; }
257 | LAT { $$ = Q_LAT; }
258 | SCA { $$ = Q_SCA; }
259 | MOPDL { $$ = Q_MOPDL; }
260 | MOPRC { $$ = Q_MOPRC; }
261 | IPV6 { $$ = Q_IPV6; }
262 | ICMPV6 { $$ = Q_ICMPV6; }
264 | ESP { $$ = Q_ESP; }
265 | ISO { $$ = Q_ISO; }
266 | ESIS { $$ = Q_ESIS; }
267 | ISIS { $$ = Q_ISIS; }
269 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
270 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
271 | LESS NUM { $$ = gen_less($2); }
272 | GREATER NUM { $$ = gen_greater($2); }
273 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
274 | INBOUND { $$ = gen_inbound(0); }
275 | OUTBOUND { $$ = gen_inbound(1); }
277 relop: '>' { $$ = BPF_JGT; }
278 | GEQ { $$ = BPF_JGE; }
279 | '=' { $$ = BPF_JEQ; }
281 irelop: LEQ { $$ = BPF_JGT; }
282 | '<' { $$ = BPF_JGE; }
283 | NEQ { $$ = BPF_JEQ; }
285 arth: pnum { $$ = gen_loadi($1); }
288 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
289 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
290 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
291 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
292 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
293 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
294 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
295 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
296 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
297 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
298 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
299 | paren narth ')' { $$ = $2; }
300 | LEN { $$ = gen_loadlen(); }
302 byteop: '&' { $$ = '&'; }
309 | paren pnum ')' { $$ = $2; }