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