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