]> The Tcpdump Group git mirrors - libpcap/blob - grammar.y
Tony Li's changes, from FreeBSD, to support filtering for OSI packets
[libpcap] / grammar.y
1 %{
2 /*
3 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
4 * The Regents of the University of California. All rights reserved.
5 *
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
17 * written permission.
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.
21 *
22 */
23 #ifndef lint
24 static const char rcsid[] =
25 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.63 2000-10-28 09:30:22 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 #include <stdlib.h>
36
37 #if __STDC__
38 struct mbuf;
39 struct rtentry;
40 #endif
41
42 #include <net/if.h>
43
44 #include <netinet/in.h>
45
46 #include <stdio.h>
47
48 #include "pcap-int.h"
49
50 #include "gencode.h"
51 #include <pcap-namedb.h>
52
53 #ifdef HAVE_OS_PROTO_H
54 #include "os-proto.h"
55 #endif
56
57 #define QSET(q, p, d, a) (q).proto = (p),\
58 (q).dir = (d),\
59 (q).addr = (a)
60
61 int n_errors = 0;
62
63 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
64
65 static void
66 yyerror(char *msg)
67 {
68 ++n_errors;
69 bpf_error("%s", msg);
70 /* NOTREACHED */
71 }
72
73 #ifndef YYBISON
74 int yyparse(void);
75
76 int
77 pcap_parse()
78 {
79 return (yyparse());
80 }
81 #endif
82
83 %}
84
85 %union {
86 int i;
87 bpf_u_int32 h;
88 u_char *e;
89 char *s;
90 struct stmt *stmt;
91 struct arth *a;
92 struct {
93 struct qual q;
94 struct block *b;
95 } blk;
96 struct block *rblk;
97 }
98
99 %type <blk> expr id nid pid term rterm qid
100 %type <blk> head
101 %type <i> pqual dqual aqual ndaqual
102 %type <a> arth narth
103 %type <i> byteop pname pnum relop irelop
104 %type <blk> and or paren not null prog
105 %type <rblk> other
106
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
110 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
111 %token TK_BROADCAST TK_MULTICAST
112 %token NUM INBOUND OUTBOUND
113 %token LINK
114 %token GEQ LEQ NEQ
115 %token ID EID HID HID6
116 %token LSH RSH
117 %token LEN
118 %token IPV6 ICMPV6 AH ESP
119 %token VLAN
120 %token ISO ESIS ISIS
121
122 %type <s> ID
123 %type <e> EID
124 %type <s> HID HID6
125 %type <i> NUM
126
127 %left OR AND
128 %nonassoc '!'
129 %left '|'
130 %left '&'
131 %left LSH RSH
132 %left '+' '-'
133 %left '*' '/'
134 %nonassoc UMINUS
135 %%
136 prog: null expr
137 {
138 finish_parse($2.b);
139 }
140 | null
141 ;
142 null: /* null */ { $$.q = qerr; }
143 ;
144 expr: term
145 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
146 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
147 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
148 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
149 ;
150 and: AND { $$ = $<blk>0; }
151 ;
152 or: OR { $$ = $<blk>0; }
153 ;
154 id: nid
155 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
156 $$.q = $<blk>0.q); }
157 | paren pid ')' { $$ = $2; }
158 ;
159 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
160 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
161 $$.q = $<blk>0.q); }
162 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
163 $$.q = $<blk>0.q); }
164 | HID {
165 /* Decide how to parse HID based on proto */
166 $$.q = $<blk>0.q;
167 $$.b = gen_ncode($1, 0, $$.q);
168 }
169 | HID6 '/' NUM {
170 #ifdef INET6
171 $$.b = gen_mcode6($1, NULL, $3,
172 $$.q = $<blk>0.q);
173 #else
174 bpf_error("'ip6addr/prefixlen' not supported "
175 "in this configuration");
176 #endif /*INET6*/
177 }
178 | HID6 {
179 #ifdef INET6
180 $$.b = gen_mcode6($1, 0, 128,
181 $$.q = $<blk>0.q);
182 #else
183 bpf_error("'ip6addr' not supported "
184 "in this configuration");
185 #endif /*INET6*/
186 }
187 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
188 | not id { gen_not($2.b); $$ = $2; }
189 ;
190 not: '!' { $$ = $<blk>0; }
191 ;
192 paren: '(' { $$ = $<blk>0; }
193 ;
194 pid: nid
195 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
196 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
197 ;
198 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
199 $$.q = $<blk>0.q); }
200 | pid
201 ;
202 term: rterm
203 | not term { gen_not($2.b); $$ = $2; }
204 ;
205 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
206 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
207 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
208 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
209 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
210 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
211 ;
212 rterm: head id { $$ = $2; }
213 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
214 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
215 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
216 $$.q = qerr; }
217 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
218 $$.q = qerr; }
219 | other { $$.b = $1; $$.q = qerr; }
220 ;
221 /* protocol level qualifiers */
222 pqual: pname
223 | { $$ = Q_DEFAULT; }
224 ;
225 /* 'direction' qualifiers */
226 dqual: SRC { $$ = Q_SRC; }
227 | DST { $$ = Q_DST; }
228 | SRC OR DST { $$ = Q_OR; }
229 | DST OR SRC { $$ = Q_OR; }
230 | SRC AND DST { $$ = Q_AND; }
231 | DST AND SRC { $$ = Q_AND; }
232 ;
233 /* address type qualifiers */
234 aqual: HOST { $$ = Q_HOST; }
235 | NET { $$ = Q_NET; }
236 | PORT { $$ = Q_PORT; }
237 ;
238 /* non-directional address type qualifiers */
239 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
240 ;
241 pname: LINK { $$ = Q_LINK; }
242 | IP { $$ = Q_IP; }
243 | ARP { $$ = Q_ARP; }
244 | RARP { $$ = Q_RARP; }
245 | TCP { $$ = Q_TCP; }
246 | UDP { $$ = Q_UDP; }
247 | ICMP { $$ = Q_ICMP; }
248 | IGMP { $$ = Q_IGMP; }
249 | IGRP { $$ = Q_IGRP; }
250 | PIM { $$ = Q_PIM; }
251 | ATALK { $$ = Q_ATALK; }
252 | AARP { $$ = Q_AARP; }
253 | DECNET { $$ = Q_DECNET; }
254 | LAT { $$ = Q_LAT; }
255 | SCA { $$ = Q_SCA; }
256 | MOPDL { $$ = Q_MOPDL; }
257 | MOPRC { $$ = Q_MOPRC; }
258 | IPV6 { $$ = Q_IPV6; }
259 | ICMPV6 { $$ = Q_ICMPV6; }
260 | AH { $$ = Q_AH; }
261 | ESP { $$ = Q_ESP; }
262 | ISO { $$ = Q_ISO; }
263 | ESIS { $$ = Q_ESIS; }
264 | ISIS { $$ = Q_ISIS; }
265 ;
266 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
267 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
268 | LESS NUM { $$ = gen_less($2); }
269 | GREATER NUM { $$ = gen_greater($2); }
270 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
271 | INBOUND { $$ = gen_inbound(0); }
272 | OUTBOUND { $$ = gen_inbound(1); }
273 | VLAN pnum { $$ = gen_vlan($2); }
274 | VLAN { $$ = gen_vlan(-1); }
275 ;
276 relop: '>' { $$ = BPF_JGT; }
277 | GEQ { $$ = BPF_JGE; }
278 | '=' { $$ = BPF_JEQ; }
279 ;
280 irelop: LEQ { $$ = BPF_JGT; }
281 | '<' { $$ = BPF_JGE; }
282 | NEQ { $$ = BPF_JEQ; }
283 ;
284 arth: pnum { $$ = gen_loadi($1); }
285 | narth
286 ;
287 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
288 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
289 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
290 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
291 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
292 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
293 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
294 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
295 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
296 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
297 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
298 | paren narth ')' { $$ = $2; }
299 | LEN { $$ = gen_loadlen(); }
300 ;
301 byteop: '&' { $$ = '&'; }
302 | '|' { $$ = '|'; }
303 | '<' { $$ = '<'; }
304 | '>' { $$ = '>'; }
305 | '=' { $$ = '='; }
306 ;
307 pnum: NUM
308 | paren pnum ')' { $$ = $2; }
309 ;
310 %%