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