xref: /freebsd/contrib/libpcap/scanner.l (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
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.95.2.3 2004/03/28 21:45:33 fenner 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 
188 ip6		{
189 #ifdef INET6
190 		return IPV6;
191 #else
192 		bpf_error("%s not supported", yytext);
193 #endif
194 		}
195 icmp6		{
196 #ifdef INET6
197 		return ICMPV6;
198 #else
199 		bpf_error("%s not supported", yytext);
200 #endif
201 		}
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 proto		return PROTO;
239 protochain	{
240 #ifdef NO_PROTOCHAIN
241 		  bpf_error("%s not supported", yytext);
242 #else
243 		  return PROTOCHAIN;
244 #endif
245 		}
246 
247 gateway		return GATEWAY;
248 
249 less		return LESS;
250 greater		return GREATER;
251 byte		return CBYTE;
252 broadcast	return TK_BROADCAST;
253 multicast	return TK_MULTICAST;
254 
255 and|"&&"	return AND;
256 or|"||"		return OR;
257 not		return '!';
258 
259 len|length	return LEN;
260 inbound		return INBOUND;
261 outbound	return OUTBOUND;
262 
263 vlan		return VLAN;
264 
265 lane		return LANE;
266 llc		return LLC;
267 metac		return METAC;
268 bcc		return BCC;
269 oam		return OAM;
270 oamf4		return OAMF4;
271 oamf4ec		return OAMF4EC;
272 oamf4sc		return OAMF4SC;
273 sc		return SC;
274 ilmic		return ILMIC;
275 vpi		return VPI;
276 vci		return VCI;
277 connectmsg	return CONNECTMSG;
278 metaconnect	return METACONNECT;
279 
280 on|ifname	return PF_IFNAME;
281 rset|ruleset	return PF_RSET;
282 rnr|rulenum	return PF_RNR;
283 srnr|subrulenum	return PF_SRNR;
284 reason		return PF_REASON;
285 action		return PF_ACTION;
286 
287 [ \r\n\t]		;
288 [+\-*/:\[\]!<>()&|=]	return yytext[0];
289 ">="			return GEQ;
290 "<="			return LEQ;
291 "!="			return NEQ;
292 "=="			return '=';
293 "<<"			return LSH;
294 ">>"			return RSH;
295 ${B}			{ yylval.e = pcap_ether_aton(((char *)yytext)+1);
296 			  return AID; }
297 {N}			{ yylval.i = stoi((char *)yytext); return NUM; }
298 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N})	{
299 			yylval.s = sdup((char *)yytext); return HID; }
300 {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
301 			  return EID; }
302 {V6}			{
303 #ifdef INET6
304 			  struct addrinfo hints, *res;
305 			  memset(&hints, 0, sizeof(hints));
306 			  hints.ai_family = AF_INET6;
307 			  hints.ai_flags = AI_NUMERICHOST;
308 			  if (getaddrinfo(yytext, NULL, &hints, &res))
309 				bpf_error("bogus IPv6 address %s", yytext);
310 			  else {
311 				yylval.s = sdup((char *)yytext); return HID6;
312 			  }
313 #else
314 			  bpf_error("IPv6 address %s not supported", yytext);
315 #endif /*INET6*/
316 			}
317 {B}:+({B}:+)+		{ bpf_error("bogus ethernet address %s", yytext); }
318 icmptype		{ yylval.i = 0; return NUM; }
319 icmpcode		{ yylval.i = 1; return NUM; }
320 icmp-echoreply		{ yylval.i = 0; return NUM; }
321 icmp-unreach		{ yylval.i = 3; return NUM; }
322 icmp-sourcequench	{ yylval.i = 4; return NUM; }
323 icmp-redirect		{ yylval.i = 5; return NUM; }
324 icmp-echo		{ yylval.i = 8; return NUM; }
325 icmp-routeradvert	{ yylval.i = 9; return NUM; }
326 icmp-routersolicit	{ yylval.i = 10; return NUM; }
327 icmp-timxceed		{ yylval.i = 11; return NUM; }
328 icmp-paramprob		{ yylval.i = 12; return NUM; }
329 icmp-tstamp		{ yylval.i = 13; return NUM; }
330 icmp-tstampreply	{ yylval.i = 14; return NUM; }
331 icmp-ireq		{ yylval.i = 15; return NUM; }
332 icmp-ireqreply		{ yylval.i = 16; return NUM; }
333 icmp-maskreq		{ yylval.i = 17; return NUM; }
334 icmp-maskreply		{ yylval.i = 18; return NUM; }
335 tcpflags		{ yylval.i = 13; return NUM; }
336 tcp-fin			{ yylval.i = 0x01; return NUM; }
337 tcp-syn			{ yylval.i = 0x02; return NUM; }
338 tcp-rst			{ yylval.i = 0x04; return NUM; }
339 tcp-push		{ yylval.i = 0x08; return NUM; }
340 tcp-ack			{ yylval.i = 0x10; return NUM; }
341 tcp-urg			{ yylval.i = 0x20; return NUM; }
342 [A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
343 			 yylval.s = sdup((char *)yytext); return ID; }
344 "\\"[^ !()\n\t]+	{ yylval.s = sdup((char *)yytext + 1); return ID; }
345 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
346 			bpf_error("illegal token: %s", yytext); }
347 .			{ bpf_error("illegal char '%c'", *yytext); }
348 %%
349 void
350 lex_init(buf)
351 	char *buf;
352 {
353 #ifdef FLEX_SCANNER
354 	in_buffer = yy_scan_string(buf);
355 #else
356 	in_buffer = buf;
357 #endif
358 }
359 
360 /*
361  * Do any cleanup necessary after parsing.
362  */
363 void
364 lex_cleanup()
365 {
366 #ifdef FLEX_SCANNER
367 	if (in_buffer != NULL)
368 		yy_delete_buffer(in_buffer);
369 	in_buffer = NULL;
370 #endif
371 }
372 
373 /*
374  * Also define a yywrap.  Note that if we're using flex, it will
375  * define a macro to map this identifier to pcap_wrap.
376  */
377 int
378 yywrap()
379 {
380 	return 1;
381 }
382 
383 /* Hex digit to integer. */
384 static inline int
385 xdtoi(c)
386 	register int c;
387 {
388 	if (isdigit(c))
389 		return c - '0';
390 	else if (islower(c))
391 		return c - 'a' + 10;
392 	else
393 		return c - 'A' + 10;
394 }
395 
396 /*
397  * Convert string to integer.  Just like atoi(), but checks for
398  * preceding 0x or 0 and uses hex or octal instead of decimal.
399  */
400 static int
401 stoi(s)
402 	char *s;
403 {
404 	int base = 10;
405 	int n = 0;
406 
407 	if (*s == '0') {
408 		if (s[1] == 'x' || s[1] == 'X') {
409 			s += 2;
410 			base = 16;
411 		}
412 		else {
413 			base = 8;
414 			s += 1;
415 		}
416 	}
417 	while (*s)
418 		n = n * base + xdtoi(*s++);
419 
420 	return n;
421 }
422