1 %{
2 /*
3  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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 #ifdef WIN32
24 #include <pcap-stdinc.h>
25 #else /* WIN32 */
26 #if HAVE_INTTYPES_H
27 #include <inttypes.h>
28 #elif HAVE_STDINT_H
29 #include <stdint.h>
30 #endif
31 #ifdef HAVE_SYS_BITYPES_H
32 #include <sys/bitypes.h>
33 #endif
34 #include <sys/types.h>
35 #endif /* WIN32 */
36 
37 #include <ctype.h>
38 #include <string.h>
39 
40 #include "pcap-int.h"
41 
42 #include "gencode.h"
43 #ifdef INET6
44 #ifdef WIN32
45 #include <pcap-stdinc.h>
46 
47 #ifdef __MINGW32__
48 #include "ip6_misc.h"
49 #endif
50 #else /* WIN32 */
51 #include <sys/socket.h>	/* for "struct sockaddr" in "struct addrinfo" */
52 #include <netdb.h>	/* for "struct addrinfo" */
53 #endif /* WIN32 */
54 
55 /* Workaround for AIX 4.3 */
56 #if !defined(AI_NUMERICHOST)
57 #define AI_NUMERICHOST 0x04
58 #endif
59 #endif /*INET6*/
60 #include <pcap/namedb.h>
61 #include "tokdefs.h"
62 
63 #ifdef HAVE_OS_PROTO_H
64 #include "os-proto.h"
65 #endif
66 
67 static int stoi(char *);
68 static inline int xdtoi(int);
69 
70 #ifdef FLEX_SCANNER
71 #define YY_NO_INPUT
72 #define YY_NO_UNPUT
73 static YY_BUFFER_STATE in_buffer;
74 #else
75 static const char *in_buffer;
76 
77 #undef getc
78 #define getc(fp)  (*in_buffer == 0 ? EOF : *in_buffer++)
79 #endif
80 
81 #define yylval pcap_lval
82 extern YYSTYPE yylval;
83 
84 %}
85 
86 N		([0-9]+|(0X|0x)[0-9A-Fa-f]+)
87 B		([0-9A-Fa-f][0-9A-Fa-f]?)
88 B2		([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])
89 W		([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
90 
91 %a 18400
92 %o 21500
93 %e 7600
94 %k 4550
95 %p 27600
96 %n 2000
97 
98 V680		{W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
99 
100 V670		::{W}:{W}:{W}:{W}:{W}:{W}:{W}
101 V671		{W}::{W}:{W}:{W}:{W}:{W}:{W}
102 V672		{W}:{W}::{W}:{W}:{W}:{W}:{W}
103 V673		{W}:{W}:{W}::{W}:{W}:{W}:{W}
104 V674		{W}:{W}:{W}:{W}::{W}:{W}:{W}
105 V675		{W}:{W}:{W}:{W}:{W}::{W}:{W}
106 V676		{W}:{W}:{W}:{W}:{W}:{W}::{W}
107 V677		{W}:{W}:{W}:{W}:{W}:{W}:{W}::
108 
109 V660		::{W}:{W}:{W}:{W}:{W}:{W}
110 V661		{W}::{W}:{W}:{W}:{W}:{W}
111 V662		{W}:{W}::{W}:{W}:{W}:{W}
112 V663		{W}:{W}:{W}::{W}:{W}:{W}
113 V664		{W}:{W}:{W}:{W}::{W}:{W}
114 V665		{W}:{W}:{W}:{W}:{W}::{W}
115 V666		{W}:{W}:{W}:{W}:{W}:{W}::
116 
117 V650		::{W}:{W}:{W}:{W}:{W}
118 V651		{W}::{W}:{W}:{W}:{W}
119 V652		{W}:{W}::{W}:{W}:{W}
120 V653		{W}:{W}:{W}::{W}:{W}
121 V654		{W}:{W}:{W}:{W}::{W}
122 V655		{W}:{W}:{W}:{W}:{W}::
123 
124 V640		::{W}:{W}:{W}:{W}
125 V641		{W}::{W}:{W}:{W}
126 V642		{W}:{W}::{W}:{W}
127 V643		{W}:{W}:{W}::{W}
128 V644		{W}:{W}:{W}:{W}::
129 
130 V630		::{W}:{W}:{W}
131 V631		{W}::{W}:{W}
132 V632		{W}:{W}::{W}
133 V633		{W}:{W}:{W}::
134 
135 V620		::{W}:{W}
136 V621		{W}::{W}
137 V622		{W}:{W}::
138 
139 V610		::{W}
140 V611		{W}::
141 
142 V600		::
143 
144 V6604		{W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
145 
146 V6504		::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
147 V6514		{W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
148 V6524		{W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
149 V6534		{W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
150 V6544		{W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
151 V6554		{W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
152 
153 V6404		::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
154 V6414		{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
155 V6424		{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
156 V6434		{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
157 V6444		{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
158 
159 V6304		::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
160 V6314		{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
161 V6324		{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
162 V6334		{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
163 
164 V6204		::{W}:{W}:{N}\.{N}\.{N}\.{N}
165 V6214		{W}::{W}:{N}\.{N}\.{N}\.{N}
166 V6224		{W}:{W}::{N}\.{N}\.{N}\.{N}
167 
168 V6104		::{W}:{N}\.{N}\.{N}\.{N}
169 V6114		{W}::{N}\.{N}\.{N}\.{N}
170 
171 V6004		::{N}\.{N}\.{N}\.{N}
172 
173 
174 V6		({V680}|{V670}|{V671}|{V672}|{V673}|{V674}|{V675}|{V676}|{V677}|{V660}|{V661}|{V662}|{V663}|{V664}|{V665}|{V666}|{V650}|{V651}|{V652}|{V653}|{V654}|{V655}|{V640}|{V641}|{V642}|{V643}|{V644}|{V630}|{V631}|{V632}|{V633}|{V620}|{V621}|{V622}|{V610}|{V611}|{V600}|{V6604}|{V6504}|{V6514}|{V6524}|{V6534}|{V6544}|{V6554}|{V6404}|{V6414}|{V6424}|{V6434}|{V6444}|{V6304}|{V6314}|{V6324}|{V6334}|{V6204}|{V6214}|{V6224}|{V6104}|{V6114}|{V6004})
175 
176 MAC		({B}:{B}:{B}:{B}:{B}:{B}|{B}\-{B}\-{B}\-{B}\-{B}\-{B}|{B}\.{B}\.{B}\.{B}\.{B}\.{B}|{B2}\.{B2}\.{B2}|{B2}{3})
177 
178 
179 
180 %%
181 dst		return DST;
182 src		return SRC;
183 
184 link|ether|ppp|slip  return LINK;
185 fddi|tr|wlan	return LINK;
186 arp		return ARP;
187 rarp		return RARP;
188 ip		return IP;
189 sctp		return SCTP;
190 tcp		return TCP;
191 udp		return UDP;
192 icmp		return ICMP;
193 igmp		return IGMP;
194 igrp		return IGRP;
195 pim		return PIM;
196 vrrp		return VRRP;
197 carp		return CARP;
198 radio		return RADIO;
199 
200 ip6		return IPV6;
201 icmp6		return ICMPV6;
202 ah		return AH;
203 esp		return ESP;
204 
205 atalk		return ATALK;
206 aarp		return AARP;
207 decnet		return DECNET;
208 lat		return LAT;
209 sca		return SCA;
210 moprc		return MOPRC;
211 mopdl		return MOPDL;
212 
213 iso		return ISO;
214 esis		return ESIS;
215 es-is		return ESIS;
216 isis		return ISIS;
217 is-is		return ISIS;
218 l1              return L1;
219 l2              return L2;
220 iih             return IIH;
221 lsp             return LSP;
222 snp             return SNP;
223 csnp            return CSNP;
224 psnp            return PSNP;
225 
226 clnp		return CLNP;
227 
228 stp		return STP;
229 
230 ipx		return IPX;
231 
232 netbeui		return NETBEUI;
233 
234 host		return HOST;
235 net		return NET;
236 mask		return NETMASK;
237 port		return PORT;
238 portrange	return PORTRANGE;
239 proto		return PROTO;
240 protochain	{
241 #ifdef NO_PROTOCHAIN
242 		  bpf_error("%s not supported", yytext);
243 #else
244 		  return PROTOCHAIN;
245 #endif
246 		}
247 
248 gateway		return GATEWAY;
249 
250 type		return TYPE;
251 subtype		return SUBTYPE;
252 direction|dir	return DIR;
253 address1|addr1	return ADDR1;
254 address2|addr2	return ADDR2;
255 address3|addr3	return ADDR3;
256 address4|addr4	return ADDR4;
257 ra		return RA;
258 ta		return TA;
259 
260 less		return LESS;
261 greater		return GREATER;
262 byte		return CBYTE;
263 broadcast	return TK_BROADCAST;
264 multicast	return TK_MULTICAST;
265 
266 and|"&&"	return AND;
267 or|"||"		return OR;
268 not		return '!';
269 
270 len|length	return LEN;
271 inbound		return INBOUND;
272 outbound	return OUTBOUND;
273 
274 vlan		return VLAN;
275 mpls		return MPLS;
276 pppoed		return PPPOED;
277 pppoes		return PPPOES;
278 geneve		return GENEVE;
279 
280 lane		return LANE;
281 llc		return LLC;
282 metac		return METAC;
283 bcc		return BCC;
284 oam		return OAM;
285 oamf4		return OAMF4;
286 oamf4ec		return OAMF4EC;
287 oamf4sc		return OAMF4SC;
288 sc		return SC;
289 ilmic		return ILMIC;
290 vpi		return VPI;
291 vci		return VCI;
292 connectmsg	return CONNECTMSG;
293 metaconnect	return METACONNECT;
294 
295 on|ifname	return PF_IFNAME;
296 rset|ruleset	return PF_RSET;
297 rnr|rulenum	return PF_RNR;
298 srnr|subrulenum	return PF_SRNR;
299 reason		return PF_REASON;
300 action		return PF_ACTION;
301 
302 fisu		return FISU;
303 lssu		return LSSU;
304 lsu		return LSSU;
305 msu		return MSU;
306 hfisu		return HFISU;
307 hlssu		return HLSSU;
308 hmsu		return HMSU;
309 sio		return SIO;
310 opc		return OPC;
311 dpc		return DPC;
312 sls		return SLS;
313 hsio		return HSIO;
314 hopc		return HOPC;
315 hdpc		return HDPC;
316 hsls		return HSLS;
317 
318 [ \r\n\t]		;
319 [+\-*/%:\[\]!<>()&|\^=]	return yytext[0];
320 ">="			return GEQ;
321 "<="			return LEQ;
322 "!="			return NEQ;
323 "=="			return '=';
324 "<<"			return LSH;
325 ">>"			return RSH;
326 ${B}			{ yylval.e = pcap_ether_aton(((char *)yytext)+1);
327 			  if (yylval.e == NULL)
328 				bpf_error("malloc");
329 			  return AID; }
330 {MAC}			{ yylval.e = pcap_ether_aton((char *)yytext);
331 			  if (yylval.e == NULL)
332 				bpf_error("malloc");
333 			  return EID; }
334 {N}			{ yylval.i = stoi((char *)yytext); return NUM; }
335 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N})	{
336 			yylval.s = sdup((char *)yytext); return HID; }
337 {V6}			{
338 #ifdef INET6
339 			  struct addrinfo hints, *res;
340 			  memset(&hints, 0, sizeof(hints));
341 			  hints.ai_family = AF_INET6;
342 			  hints.ai_flags = AI_NUMERICHOST;
343 			  if (getaddrinfo(yytext, NULL, &hints, &res))
344 				bpf_error("bogus IPv6 address %s", yytext);
345 			  else {
346 				freeaddrinfo(res);
347 				yylval.s = sdup((char *)yytext); return HID6;
348 			  }
349 #else
350 			  bpf_error("IPv6 address %s not supported", yytext);
351 #endif /*INET6*/
352 			}
353 {B}:+({B}:+)+		{ bpf_error("bogus ethernet address %s", yytext); }
354 icmptype		{ yylval.i = 0; return NUM; }
355 icmpcode		{ yylval.i = 1; return NUM; }
356 icmp-echoreply		{ yylval.i = 0; return NUM; }
357 icmp-unreach		{ yylval.i = 3; return NUM; }
358 icmp-sourcequench	{ yylval.i = 4; return NUM; }
359 icmp-redirect		{ yylval.i = 5; return NUM; }
360 icmp-echo		{ yylval.i = 8; return NUM; }
361 icmp-routeradvert	{ yylval.i = 9; return NUM; }
362 icmp-routersolicit	{ yylval.i = 10; return NUM; }
363 icmp-timxceed		{ yylval.i = 11; return NUM; }
364 icmp-paramprob		{ yylval.i = 12; return NUM; }
365 icmp-tstamp		{ yylval.i = 13; return NUM; }
366 icmp-tstampreply	{ yylval.i = 14; return NUM; }
367 icmp-ireq		{ yylval.i = 15; return NUM; }
368 icmp-ireqreply		{ yylval.i = 16; return NUM; }
369 icmp-maskreq		{ yylval.i = 17; return NUM; }
370 icmp-maskreply		{ yylval.i = 18; return NUM; }
371 tcpflags		{ yylval.i = 13; return NUM; }
372 tcp-fin			{ yylval.i = 0x01; return NUM; }
373 tcp-syn			{ yylval.i = 0x02; return NUM; }
374 tcp-rst			{ yylval.i = 0x04; return NUM; }
375 tcp-push		{ yylval.i = 0x08; return NUM; }
376 tcp-ack			{ yylval.i = 0x10; return NUM; }
377 tcp-urg			{ yylval.i = 0x20; return NUM; }
378 [A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
379 			 yylval.s = sdup((char *)yytext); return ID; }
380 "\\"[^ !()\n\t]+	{ yylval.s = sdup((char *)yytext + 1); return ID; }
381 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
382 			bpf_error("illegal token: %s", yytext); }
383 .			{ bpf_error("illegal char '%c'", *yytext); }
384 %%
385 void
lex_init(buf)386 lex_init(buf)
387 	const char *buf;
388 {
389 #ifdef FLEX_SCANNER
390 	in_buffer = yy_scan_string(buf);
391 #else
392 	in_buffer = buf;
393 #endif
394 }
395 
396 /*
397  * Do any cleanup necessary after parsing.
398  */
399 void
lex_cleanup()400 lex_cleanup()
401 {
402 #ifdef FLEX_SCANNER
403 	if (in_buffer != NULL)
404 		yy_delete_buffer(in_buffer);
405 	in_buffer = NULL;
406 #endif
407 }
408 
409 /*
410  * Also define a yywrap.  Note that if we're using flex, it will
411  * define a macro to map this identifier to pcap_wrap.
412  */
413 int
yywrap()414 yywrap()
415 {
416 	return 1;
417 }
418 
419 /* Hex digit to integer. */
420 static inline int
xdtoi(c)421 xdtoi(c)
422 	register int c;
423 {
424 	if (isdigit(c))
425 		return c - '0';
426 	else if (islower(c))
427 		return c - 'a' + 10;
428 	else
429 		return c - 'A' + 10;
430 }
431 
432 /*
433  * Convert string to integer.  Just like atoi(), but checks for
434  * preceding 0x or 0 and uses hex or octal instead of decimal.
435  */
436 static int
stoi(s)437 stoi(s)
438 	char *s;
439 {
440 	int base = 10;
441 	int n = 0;
442 
443 	if (*s == '0') {
444 		if (s[1] == 'x' || s[1] == 'X') {
445 			s += 2;
446 			base = 16;
447 		}
448 		else {
449 			base = 8;
450 			s += 1;
451 		}
452 	}
453 	while (*s)
454 		n = n * base + xdtoi(*s++);
455 
456 	return n;
457 }
458