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