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