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