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: /usr/local/cvs/linux/tools/build/e100boot/libpcap-0.4/grammar.y,v 1.1 1999/08/26 10:05:22 johana Exp $ (LBL)";
28 #include <sys/types.h>
30 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
47 #include <pcap-namedb.h>
50 #ifdef HAVE_OS_PROTO_H
54 #define QSET(q, p, d, a) (q).proto = (p),\
60 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
96 %type <blk> expr id nid pid term rterm qid
98 %type <i> pqual dqual aqual ndaqual
100 %type <i> byteop pname pnum relop irelop
101 %type <blk> and or paren not null prog
104 %token DST SRC HOST GATEWAY
105 %token NET MASK PORT LESS GREATER PROTO BYTE
106 %token ARP RARP IP TCP UDP ICMP IGMP IGRP
107 %token ATALK DECNET LAT SCA MOPRC MOPDL
108 %token TK_BROADCAST TK_MULTICAST
109 %token NUM INBOUND OUTBOUND
136 null: /* null */ { $$.q = qerr; }
139 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
140 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
141 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
142 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
144 and: AND { $$ = $<blk>0; }
146 or: OR { $$ = $<blk>0; }
149 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
151 | paren pid ')' { $$ = $2; }
153 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
154 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
156 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
159 /* Decide how to parse HID based on proto */
161 switch ($$.q.proto) {
163 $$.b = gen_ncode($1, 0, $$.q);
166 $$.b = gen_ncode($1, 0, $$.q);
170 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
171 | not id { gen_not($2.b); $$ = $2; }
173 not: '!' { $$ = $<blk>0; }
175 paren: '(' { $$ = $<blk>0; }
178 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
179 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
181 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
186 | not term { gen_not($2.b); $$ = $2; }
188 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
189 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
190 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
191 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
192 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
194 rterm: head id { $$ = $2; }
195 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
196 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
197 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
199 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
201 | other { $$.b = $1; $$.q = qerr; }
203 /* protocol level qualifiers */
205 | { $$ = Q_DEFAULT; }
207 /* 'direction' qualifiers */
208 dqual: SRC { $$ = Q_SRC; }
209 | DST { $$ = Q_DST; }
210 | SRC OR DST { $$ = Q_OR; }
211 | DST OR SRC { $$ = Q_OR; }
212 | SRC AND DST { $$ = Q_AND; }
213 | DST AND SRC { $$ = Q_AND; }
215 /* address type qualifiers */
216 aqual: HOST { $$ = Q_HOST; }
217 | NET { $$ = Q_NET; }
218 | PORT { $$ = Q_PORT; }
220 /* non-directional address type qualifiers */
221 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
223 pname: LINK { $$ = Q_LINK; }
225 | ARP { $$ = Q_ARP; }
226 | RARP { $$ = Q_RARP; }
227 | TCP { $$ = Q_TCP; }
228 | UDP { $$ = Q_UDP; }
229 | ICMP { $$ = Q_ICMP; }
230 | IGMP { $$ = Q_IGMP; }
231 | IGRP { $$ = Q_IGRP; }
232 | ATALK { $$ = Q_ATALK; }
233 | DECNET { $$ = Q_DECNET; }
234 | LAT { $$ = Q_LAT; }
235 | SCA { $$ = Q_SCA; }
236 | MOPDL { $$ = Q_MOPDL; }
237 | MOPRC { $$ = Q_MOPRC; }
239 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
240 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
241 | LESS NUM { $$ = gen_less($2); }
242 | GREATER NUM { $$ = gen_greater($2); }
243 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
244 | INBOUND { $$ = gen_inbound(0); }
245 | OUTBOUND { $$ = gen_inbound(1); }
247 relop: '>' { $$ = BPF_JGT; }
248 | GEQ { $$ = BPF_JGE; }
249 | '=' { $$ = BPF_JEQ; }
251 irelop: LEQ { $$ = BPF_JGT; }
252 | '<' { $$ = BPF_JGE; }
253 | NEQ { $$ = BPF_JEQ; }
255 arth: pnum { $$ = gen_loadi($1); }
258 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
259 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
260 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
261 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
262 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
263 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
264 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
265 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
266 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
267 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
268 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
269 | paren narth ')' { $$ = $2; }
270 | LEN { $$ = gen_loadlen(); }
272 byteop: '&' { $$ = '&'; }
279 | paren pnum ')' { $$ = $2; }