xref: /freebsd/contrib/libpcap/scanner.l (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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.9 2007/06/11 09:52:05 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 18400
86 %o 21500
87 %e 7600
88 %k 4550
89 %p 27600
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 fisu		return FISU;
293 lssu		return LSSU;
294 lsu		return LSSU;
295 msu		return MSU;
296 sio		return SIO;
297 opc		return OPC;
298 dpc		return DPC;
299 sls		return SLS;
300 
301 [ \r\n\t]		;
302 [+\-*/:\[\]!<>()&|=]	return yytext[0];
303 ">="			return GEQ;
304 "<="			return LEQ;
305 "!="			return NEQ;
306 "=="			return '=';
307 "<<"			return LSH;
308 ">>"			return RSH;
309 ${B}			{ yylval.e = pcap_ether_aton(((char *)yytext)+1);
310 			  return AID; }
311 {N}			{ yylval.i = stoi((char *)yytext); return NUM; }
312 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N})	{
313 			yylval.s = sdup((char *)yytext); return HID; }
314 {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
315 			  return EID; }
316 {V6}			{
317 #ifdef INET6
318 			  struct addrinfo hints, *res;
319 			  memset(&hints, 0, sizeof(hints));
320 			  hints.ai_family = AF_INET6;
321 			  hints.ai_flags = AI_NUMERICHOST;
322 			  if (getaddrinfo(yytext, NULL, &hints, &res))
323 				bpf_error("bogus IPv6 address %s", yytext);
324 			  else {
325 				yylval.s = sdup((char *)yytext); return HID6;
326 			  }
327 #else
328 			  bpf_error("IPv6 address %s not supported", yytext);
329 #endif /*INET6*/
330 			}
331 {B}:+({B}:+)+		{ bpf_error("bogus ethernet address %s", yytext); }
332 icmptype		{ yylval.i = 0; return NUM; }
333 icmpcode		{ yylval.i = 1; return NUM; }
334 icmp-echoreply		{ yylval.i = 0; return NUM; }
335 icmp-unreach		{ yylval.i = 3; return NUM; }
336 icmp-sourcequench	{ yylval.i = 4; return NUM; }
337 icmp-redirect		{ yylval.i = 5; return NUM; }
338 icmp-echo		{ yylval.i = 8; return NUM; }
339 icmp-routeradvert	{ yylval.i = 9; return NUM; }
340 icmp-routersolicit	{ yylval.i = 10; return NUM; }
341 icmp-timxceed		{ yylval.i = 11; return NUM; }
342 icmp-paramprob		{ yylval.i = 12; return NUM; }
343 icmp-tstamp		{ yylval.i = 13; return NUM; }
344 icmp-tstampreply	{ yylval.i = 14; return NUM; }
345 icmp-ireq		{ yylval.i = 15; return NUM; }
346 icmp-ireqreply		{ yylval.i = 16; return NUM; }
347 icmp-maskreq		{ yylval.i = 17; return NUM; }
348 icmp-maskreply		{ yylval.i = 18; return NUM; }
349 tcpflags		{ yylval.i = 13; return NUM; }
350 tcp-fin			{ yylval.i = 0x01; return NUM; }
351 tcp-syn			{ yylval.i = 0x02; return NUM; }
352 tcp-rst			{ yylval.i = 0x04; return NUM; }
353 tcp-push		{ yylval.i = 0x08; return NUM; }
354 tcp-ack			{ yylval.i = 0x10; return NUM; }
355 tcp-urg			{ yylval.i = 0x20; return NUM; }
356 [A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
357 			 yylval.s = sdup((char *)yytext); return ID; }
358 "\\"[^ !()\n\t]+	{ yylval.s = sdup((char *)yytext + 1); return ID; }
359 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
360 			bpf_error("illegal token: %s", yytext); }
361 .			{ bpf_error("illegal char '%c'", *yytext); }
362 %%
363 void
364 lex_init(buf)
365 	const char *buf;
366 {
367 #ifdef FLEX_SCANNER
368 	in_buffer = yy_scan_string(buf);
369 #else
370 	in_buffer = buf;
371 #endif
372 }
373 
374 /*
375  * Do any cleanup necessary after parsing.
376  */
377 void
378 lex_cleanup()
379 {
380 #ifdef FLEX_SCANNER
381 	if (in_buffer != NULL)
382 		yy_delete_buffer(in_buffer);
383 	in_buffer = NULL;
384 #endif
385 }
386 
387 /*
388  * Also define a yywrap.  Note that if we're using flex, it will
389  * define a macro to map this identifier to pcap_wrap.
390  */
391 int
392 yywrap()
393 {
394 	return 1;
395 }
396 
397 /* Hex digit to integer. */
398 static inline int
399 xdtoi(c)
400 	register int c;
401 {
402 	if (isdigit(c))
403 		return c - '0';
404 	else if (islower(c))
405 		return c - 'a' + 10;
406 	else
407 		return c - 'A' + 10;
408 }
409 
410 /*
411  * Convert string to integer.  Just like atoi(), but checks for
412  * preceding 0x or 0 and uses hex or octal instead of decimal.
413  */
414 static int
415 stoi(s)
416 	char *s;
417 {
418 	int base = 10;
419 	int n = 0;
420 
421 	if (*s == '0') {
422 		if (s[1] == 'x' || s[1] == 'X') {
423 			s += 2;
424 			base = 16;
425 		}
426 		else {
427 			base = 8;
428 			s += 1;
429 		}
430 	}
431 	while (*s)
432 		n = n * base + xdtoi(*s++);
433 
434 	return n;
435 }
436