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.71 2001/07/03 19:15:48 guy Exp $ (LBL)";
33 #include <sys/types.h>
35 #include <sys/socket.h>
45 #include <netinet/in.h>
52 #include <pcap-namedb.h>
54 #ifdef HAVE_OS_PROTO_H
58 #define QSET(q, p, d, a) (q).proto = (p),\
64 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
100 %type <blk> expr id nid pid term rterm qid
102 %type <i> pqual dqual aqual ndaqual
104 %type <i> byteop pname pnum relop irelop
105 %type <blk> and or paren not null prog
108 %token DST SRC HOST GATEWAY
109 %token NET MASK PORT LESS GREATER PROTO PROTOCHAIN BYTE
110 %token ARP RARP IP SCTP TCP UDP ICMP IGMP IGRP PIM VRRP
111 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
112 %token TK_BROADCAST TK_MULTICAST
113 %token NUM INBOUND OUTBOUND
116 %token ID EID HID HID6 AID
119 %token IPV6 ICMPV6 AH ESP
121 %token ISO ESIS ISIS CLNP
147 null: /* null */ { $$.q = qerr; }
150 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
151 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
152 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
153 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
155 and: AND { $$ = $<blk>0; }
157 or: OR { $$ = $<blk>0; }
160 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
162 | paren pid ')' { $$ = $2; }
164 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
165 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
167 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
170 /* Decide how to parse HID based on proto */
172 $$.b = gen_ncode($1, 0, $$.q);
176 $$.b = gen_mcode6($1, NULL, $3,
179 bpf_error("'ip6addr/prefixlen' not supported "
180 "in this configuration");
185 $$.b = gen_mcode6($1, 0, 128,
188 bpf_error("'ip6addr' not supported "
189 "in this configuration");
193 $$.b = gen_ecode($1, $$.q = $<blk>0.q);
195 * $1 was allocated by "pcap_ether_aton()",
196 * so we must free it now that we're done
202 $$.b = gen_acode($1, $$.q = $<blk>0.q);
204 * $1 was allocated by "pcap_ether_aton()",
205 * so we must free it now that we're done
210 | not id { gen_not($2.b); $$ = $2; }
212 not: '!' { $$ = $<blk>0; }
214 paren: '(' { $$ = $<blk>0; }
217 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
218 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
220 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
225 | not term { gen_not($2.b); $$ = $2; }
227 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
228 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
229 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
230 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
231 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
232 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
234 rterm: head id { $$ = $2; }
235 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
236 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
237 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
239 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
241 | other { $$.b = $1; $$.q = qerr; }
243 /* protocol level qualifiers */
245 | { $$ = Q_DEFAULT; }
247 /* 'direction' qualifiers */
248 dqual: SRC { $$ = Q_SRC; }
249 | DST { $$ = Q_DST; }
250 | SRC OR DST { $$ = Q_OR; }
251 | DST OR SRC { $$ = Q_OR; }
252 | SRC AND DST { $$ = Q_AND; }
253 | DST AND SRC { $$ = Q_AND; }
255 /* address type qualifiers */
256 aqual: HOST { $$ = Q_HOST; }
257 | NET { $$ = Q_NET; }
258 | PORT { $$ = Q_PORT; }
260 /* non-directional address type qualifiers */
261 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
263 pname: LINK { $$ = Q_LINK; }
265 | ARP { $$ = Q_ARP; }
266 | RARP { $$ = Q_RARP; }
267 | SCTP { $$ = Q_SCTP; }
268 | TCP { $$ = Q_TCP; }
269 | UDP { $$ = Q_UDP; }
270 | ICMP { $$ = Q_ICMP; }
271 | IGMP { $$ = Q_IGMP; }
272 | IGRP { $$ = Q_IGRP; }
273 | PIM { $$ = Q_PIM; }
274 | VRRP { $$ = Q_VRRP; }
275 | ATALK { $$ = Q_ATALK; }
276 | AARP { $$ = Q_AARP; }
277 | DECNET { $$ = Q_DECNET; }
278 | LAT { $$ = Q_LAT; }
279 | SCA { $$ = Q_SCA; }
280 | MOPDL { $$ = Q_MOPDL; }
281 | MOPRC { $$ = Q_MOPRC; }
282 | IPV6 { $$ = Q_IPV6; }
283 | ICMPV6 { $$ = Q_ICMPV6; }
285 | ESP { $$ = Q_ESP; }
286 | ISO { $$ = Q_ISO; }
287 | ESIS { $$ = Q_ESIS; }
288 | ISIS { $$ = Q_ISIS; }
289 | CLNP { $$ = Q_CLNP; }
290 | STP { $$ = Q_STP; }
291 | IPX { $$ = Q_IPX; }
292 | NETBEUI { $$ = Q_NETBEUI; }
294 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
295 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
296 | LESS NUM { $$ = gen_less($2); }
297 | GREATER NUM { $$ = gen_greater($2); }
298 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
299 | INBOUND { $$ = gen_inbound(0); }
300 | OUTBOUND { $$ = gen_inbound(1); }
301 | VLAN pnum { $$ = gen_vlan($2); }
302 | VLAN { $$ = gen_vlan(-1); }
304 relop: '>' { $$ = BPF_JGT; }
305 | GEQ { $$ = BPF_JGE; }
306 | '=' { $$ = BPF_JEQ; }
308 irelop: LEQ { $$ = BPF_JGT; }
309 | '<' { $$ = BPF_JGE; }
310 | NEQ { $$ = BPF_JEQ; }
312 arth: pnum { $$ = gen_loadi($1); }
315 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
316 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
317 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
318 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
319 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
320 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
321 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
322 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
323 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
324 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
325 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
326 | paren narth ')' { $$ = $2; }
327 | LEN { $$ = gen_loadlen(); }
329 byteop: '&' { $$ = '&'; }
336 | paren pnum ')' { $$ = $2; }