blob: 1000b7a3c903e588a76c76205083285e3edb2e5c [file] [log] [blame]
Elliott Hughes965a4b52017-05-15 10:37:39 -07001%top {
2/* Must come first for _LARGE_FILE_API on AIX. */
3#ifdef HAVE_CONFIG_H
4#include "config.h"
5#endif
6}
7
8/*
9 * We want a reentrant scanner.
10 */
11%option reentrant
12
13/*
14 * And we need to pass the compiler state to the scanner.
15 */
16%option extra-type="compiler_state_t *"
17
18/*
19 * We don't use input, so don't generate code for it.
20 */
21%option noinput
22
23/*
24 * We don't use unput, so don't generate code for it.
25 */
26%option nounput
27
28/*
29 * We don't read from the terminal.
30 */
31%option never-interactive
32
33/*
34 * We want to stop processing when we get to the end of the input.
35 */
36%option noyywrap
37
38/*
39 * We want to generate code that can be used by a reentrant parser
40 * generated by Bison or Berkeley YACC.
41 */
42%option bison-bridge
43
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080044%{
45/*
46 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
47 * The Regents of the University of California. All rights reserved.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that: (1) source code distributions
51 * retain the above copyright notice and this paragraph in its entirety, (2)
52 * distributions including binary code include the above copyright notice and
53 * this paragraph in its entirety in the documentation or other materials
54 * provided with the distribution, and (3) all advertising materials mentioning
55 * features or use of this software display the following acknowledgement:
56 * ``This product includes software developed by the University of California,
57 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
58 * the University nor the names of its contributors may be used to endorse
59 * or promote products derived from this software without specific prior
60 * written permission.
61 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
62 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
63 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
64 */
65
Elliott Hughes965a4b52017-05-15 10:37:39 -070066#ifdef _WIN32
67 #include <pcap-stdinc.h>
68#else
69 #if HAVE_INTTYPES_H
70 #include <inttypes.h>
71 #elif HAVE_STDINT_H
72 #include <stdint.h>
73 #endif
74 #ifdef HAVE_SYS_BITYPES_H
75 #include <sys/bitypes.h>
76 #endif
77 #include <sys/types.h>
JP Abgrall511eca32014-02-12 13:46:45 -080078#endif
JP Abgrall511eca32014-02-12 13:46:45 -080079
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080080#include <ctype.h>
81#include <string.h>
82
83#include "pcap-int.h"
84
85#include "gencode.h"
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080086
Elliott Hughes965a4b52017-05-15 10:37:39 -070087#include "grammar.h"
88
89/*
90 * Earlier versions of Flex don't declare these, so we declare them
91 * ourselves to squelch warnings.
92 */
93int pcap_get_column(yyscan_t);
94void pcap_set_column(int, yyscan_t);
95
96#ifdef INET6
97
98#ifdef _WIN32
99/*
100 * To quote the MSDN page for getaddrinfo() at
101 *
102 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
103 *
104 * "Support for getaddrinfo on Windows 2000 and older versions
105 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
106 * later. To execute an application that uses this function on earlier
107 * versions of Windows, then you need to include the Ws2tcpip.h and
108 * Wspiapi.h files. When the Wspiapi.h include file is added, the
109 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
110 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
111 * function is implemented in such a way that if the Ws2_32.dll or the
112 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
113 * Preview for Windows 2000) does not include getaddrinfo, then a
114 * version of getaddrinfo is implemented inline based on code in the
115 * Wspiapi.h header file. This inline code will be used on older Windows
116 * platforms that do not natively support the getaddrinfo function."
117 *
118 * We use getaddrinfo(), so we include Wspiapi.h here. pcap-stdinc.h
119 * includes Ws2tcpip.h, so we don't need to include it ourselves.
120 */
121#include <Wspiapi.h>
122#else /* _WIN32 */
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800123#include <sys/socket.h> /* for "struct sockaddr" in "struct addrinfo" */
124#include <netdb.h> /* for "struct addrinfo" */
Elliott Hughes965a4b52017-05-15 10:37:39 -0700125#endif /* _WIN32 */
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800126
127/* Workaround for AIX 4.3 */
128#if !defined(AI_NUMERICHOST)
129#define AI_NUMERICHOST 0x04
130#endif
Elliott Hughes965a4b52017-05-15 10:37:39 -0700131
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800132#endif /*INET6*/
Elliott Hughes965a4b52017-05-15 10:37:39 -0700133
JP Abgrall511eca32014-02-12 13:46:45 -0800134#include <pcap/namedb.h>
Elliott Hughes965a4b52017-05-15 10:37:39 -0700135#include "grammar.h"
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800136
137#ifdef HAVE_OS_PROTO_H
138#include "os-proto.h"
139#endif
140
141static int stoi(char *);
142static inline int xdtoi(int);
143
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800144%}
145
146N ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
147B ([0-9A-Fa-f][0-9A-Fa-f]?)
JP Abgrall511eca32014-02-12 13:46:45 -0800148B2 ([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 -0800149W ([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
150
151%a 18400
152%o 21500
153%e 7600
154%k 4550
155%p 27600
156%n 2000
157
158V680 {W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
159
160V670 ::{W}:{W}:{W}:{W}:{W}:{W}:{W}
161V671 {W}::{W}:{W}:{W}:{W}:{W}:{W}
162V672 {W}:{W}::{W}:{W}:{W}:{W}:{W}
163V673 {W}:{W}:{W}::{W}:{W}:{W}:{W}
164V674 {W}:{W}:{W}:{W}::{W}:{W}:{W}
165V675 {W}:{W}:{W}:{W}:{W}::{W}:{W}
166V676 {W}:{W}:{W}:{W}:{W}:{W}::{W}
167V677 {W}:{W}:{W}:{W}:{W}:{W}:{W}::
168
169V660 ::{W}:{W}:{W}:{W}:{W}:{W}
170V661 {W}::{W}:{W}:{W}:{W}:{W}
171V662 {W}:{W}::{W}:{W}:{W}:{W}
172V663 {W}:{W}:{W}::{W}:{W}:{W}
173V664 {W}:{W}:{W}:{W}::{W}:{W}
174V665 {W}:{W}:{W}:{W}:{W}::{W}
175V666 {W}:{W}:{W}:{W}:{W}:{W}::
176
177V650 ::{W}:{W}:{W}:{W}:{W}
178V651 {W}::{W}:{W}:{W}:{W}
179V652 {W}:{W}::{W}:{W}:{W}
180V653 {W}:{W}:{W}::{W}:{W}
181V654 {W}:{W}:{W}:{W}::{W}
182V655 {W}:{W}:{W}:{W}:{W}::
183
184V640 ::{W}:{W}:{W}:{W}
185V641 {W}::{W}:{W}:{W}
186V642 {W}:{W}::{W}:{W}
187V643 {W}:{W}:{W}::{W}
188V644 {W}:{W}:{W}:{W}::
189
190V630 ::{W}:{W}:{W}
191V631 {W}::{W}:{W}
192V632 {W}:{W}::{W}
193V633 {W}:{W}:{W}::
194
195V620 ::{W}:{W}
196V621 {W}::{W}
197V622 {W}:{W}::
198
199V610 ::{W}
200V611 {W}::
201
202V600 ::
203
204V6604 {W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
205
206V6504 ::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
207V6514 {W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
208V6524 {W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
209V6534 {W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
210V6544 {W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
211V6554 {W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
212
213V6404 ::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
214V6414 {W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
215V6424 {W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
216V6434 {W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
217V6444 {W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
218
219V6304 ::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
220V6314 {W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
221V6324 {W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
222V6334 {W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
223
224V6204 ::{W}:{W}:{N}\.{N}\.{N}\.{N}
225V6214 {W}::{W}:{N}\.{N}\.{N}\.{N}
226V6224 {W}:{W}::{N}\.{N}\.{N}\.{N}
227
228V6104 ::{W}:{N}\.{N}\.{N}\.{N}
229V6114 {W}::{N}\.{N}\.{N}\.{N}
230
231V6004 ::{N}\.{N}\.{N}\.{N}
232
233
234V6 ({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})
235
JP Abgrall511eca32014-02-12 13:46:45 -0800236MAC ({B}:{B}:{B}:{B}:{B}:{B}|{B}\-{B}\-{B}\-{B}\-{B}\-{B}|{B}\.{B}\.{B}\.{B}\.{B}\.{B}|{B2}\.{B2}\.{B2}|{B2}{3})
237
238
239
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800240%%
241dst return DST;
242src return SRC;
243
244link|ether|ppp|slip return LINK;
245fddi|tr|wlan return LINK;
246arp return ARP;
247rarp return RARP;
248ip return IP;
249sctp return SCTP;
250tcp return TCP;
251udp return UDP;
252icmp return ICMP;
253igmp return IGMP;
254igrp return IGRP;
255pim return PIM;
256vrrp return VRRP;
JP Abgrall511eca32014-02-12 13:46:45 -0800257carp return CARP;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800258radio return RADIO;
259
JP Abgrall511eca32014-02-12 13:46:45 -0800260ip6 return IPV6;
261icmp6 return ICMPV6;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800262ah return AH;
263esp return ESP;
264
265atalk return ATALK;
266aarp return AARP;
267decnet return DECNET;
268lat return LAT;
269sca return SCA;
270moprc return MOPRC;
271mopdl return MOPDL;
272
273iso return ISO;
274esis return ESIS;
275es-is return ESIS;
276isis return ISIS;
277is-is return ISIS;
278l1 return L1;
279l2 return L2;
280iih return IIH;
281lsp return LSP;
282snp return SNP;
283csnp return CSNP;
284psnp return PSNP;
285
286clnp return CLNP;
287
288stp return STP;
289
290ipx return IPX;
291
292netbeui return NETBEUI;
293
294host return HOST;
295net return NET;
296mask return NETMASK;
297port return PORT;
298portrange return PORTRANGE;
299proto return PROTO;
300protochain {
301#ifdef NO_PROTOCHAIN
Elliott Hughes965a4b52017-05-15 10:37:39 -0700302 bpf_error(yyextra, "%s not supported", yytext);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800303#else
304 return PROTOCHAIN;
305#endif
306 }
307
308gateway return GATEWAY;
309
JP Abgrall511eca32014-02-12 13:46:45 -0800310type return TYPE;
311subtype return SUBTYPE;
312direction|dir return DIR;
313address1|addr1 return ADDR1;
314address2|addr2 return ADDR2;
315address3|addr3 return ADDR3;
316address4|addr4 return ADDR4;
317ra return RA;
318ta return TA;
319
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800320less return LESS;
321greater return GREATER;
322byte return CBYTE;
323broadcast return TK_BROADCAST;
324multicast return TK_MULTICAST;
325
326and|"&&" return AND;
327or|"||" return OR;
328not return '!';
329
330len|length return LEN;
331inbound return INBOUND;
332outbound return OUTBOUND;
333
334vlan return VLAN;
335mpls return MPLS;
336pppoed return PPPOED;
337pppoes return PPPOES;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700338geneve return GENEVE;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800339
340lane return LANE;
341llc return LLC;
342metac return METAC;
343bcc return BCC;
344oam return OAM;
345oamf4 return OAMF4;
346oamf4ec return OAMF4EC;
347oamf4sc return OAMF4SC;
348sc return SC;
349ilmic return ILMIC;
350vpi return VPI;
351vci return VCI;
352connectmsg return CONNECTMSG;
353metaconnect return METACONNECT;
354
355on|ifname return PF_IFNAME;
356rset|ruleset return PF_RSET;
357rnr|rulenum return PF_RNR;
358srnr|subrulenum return PF_SRNR;
359reason return PF_REASON;
360action return PF_ACTION;
361
362fisu return FISU;
363lssu return LSSU;
364lsu return LSSU;
365msu return MSU;
JP Abgrall511eca32014-02-12 13:46:45 -0800366hfisu return HFISU;
367hlssu return HLSSU;
368hmsu return HMSU;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800369sio return SIO;
370opc return OPC;
371dpc return DPC;
372sls return SLS;
JP Abgrall511eca32014-02-12 13:46:45 -0800373hsio return HSIO;
374hopc return HOPC;
375hdpc return HDPC;
376hsls return HSLS;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800377
378[ \r\n\t] ;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700379[+\-*/%:\[\]!<>()&|\^=] return yytext[0];
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800380">=" return GEQ;
381"<=" return LEQ;
382"!=" return NEQ;
383"==" return '=';
384"<<" return LSH;
385">>" return RSH;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700386${B} { yylval->e = pcap_ether_aton(((char *)yytext)+1);
387 if (yylval->e == NULL)
388 bpf_error(yyextra, "malloc");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800389 return AID; }
Elliott Hughes965a4b52017-05-15 10:37:39 -0700390{MAC} { yylval->e = pcap_ether_aton((char *)yytext);
391 if (yylval->e == NULL)
392 bpf_error(yyextra, "malloc");
JP Abgrall511eca32014-02-12 13:46:45 -0800393 return EID; }
Elliott Hughes965a4b52017-05-15 10:37:39 -0700394{N} { yylval->i = stoi((char *)yytext); return NUM; }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800395({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) {
Elliott Hughes965a4b52017-05-15 10:37:39 -0700396 yylval->s = sdup(yyextra, (char *)yytext); return HID; }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800397{V6} {
398#ifdef INET6
399 struct addrinfo hints, *res;
400 memset(&hints, 0, sizeof(hints));
401 hints.ai_family = AF_INET6;
402 hints.ai_flags = AI_NUMERICHOST;
403 if (getaddrinfo(yytext, NULL, &hints, &res))
Elliott Hughes965a4b52017-05-15 10:37:39 -0700404 bpf_error(yyextra, "bogus IPv6 address %s", yytext);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800405 else {
JP Abgrall511eca32014-02-12 13:46:45 -0800406 freeaddrinfo(res);
Elliott Hughes965a4b52017-05-15 10:37:39 -0700407 yylval->s = sdup(yyextra, (char *)yytext); return HID6;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800408 }
409#else
Elliott Hughes965a4b52017-05-15 10:37:39 -0700410 bpf_error(yyextra, "IPv6 address %s not supported", yytext);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800411#endif /*INET6*/
412 }
Elliott Hughes965a4b52017-05-15 10:37:39 -0700413{B}:+({B}:+)+ { bpf_error(yyextra, "bogus ethernet address %s", yytext); }
414icmptype { yylval->i = 0; return NUM; }
415icmpcode { yylval->i = 1; return NUM; }
416icmp-echoreply { yylval->i = 0; return NUM; }
417icmp-unreach { yylval->i = 3; return NUM; }
418icmp-sourcequench { yylval->i = 4; return NUM; }
419icmp-redirect { yylval->i = 5; return NUM; }
420icmp-echo { yylval->i = 8; return NUM; }
421icmp-routeradvert { yylval->i = 9; return NUM; }
422icmp-routersolicit { yylval->i = 10; return NUM; }
423icmp-timxceed { yylval->i = 11; return NUM; }
424icmp-paramprob { yylval->i = 12; return NUM; }
425icmp-tstamp { yylval->i = 13; return NUM; }
426icmp-tstampreply { yylval->i = 14; return NUM; }
427icmp-ireq { yylval->i = 15; return NUM; }
428icmp-ireqreply { yylval->i = 16; return NUM; }
429icmp-maskreq { yylval->i = 17; return NUM; }
430icmp-maskreply { yylval->i = 18; return NUM; }
431tcpflags { yylval->i = 13; return NUM; }
432tcp-fin { yylval->i = 0x01; return NUM; }
433tcp-syn { yylval->i = 0x02; return NUM; }
434tcp-rst { yylval->i = 0x04; return NUM; }
435tcp-push { yylval->i = 0x08; return NUM; }
436tcp-ack { yylval->i = 0x10; return NUM; }
437tcp-urg { yylval->i = 0x20; return NUM; }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800438[A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
Elliott Hughes965a4b52017-05-15 10:37:39 -0700439 yylval->s = sdup(yyextra, (char *)yytext); return ID; }
440"\\"[^ !()\n\t]+ { yylval->s = sdup(yyextra, (char *)yytext + 1); return ID; }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800441[^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
Elliott Hughes965a4b52017-05-15 10:37:39 -0700442 bpf_error(yyextra, "illegal token: %s", yytext); }
443. { bpf_error(yyextra, "illegal char '%c'", *yytext); }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800444%%
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800445
446/* Hex digit to integer. */
447static inline int
448xdtoi(c)
449 register int c;
450{
451 if (isdigit(c))
452 return c - '0';
453 else if (islower(c))
454 return c - 'a' + 10;
455 else
456 return c - 'A' + 10;
457}
458
459/*
460 * Convert string to integer. Just like atoi(), but checks for
461 * preceding 0x or 0 and uses hex or octal instead of decimal.
462 */
463static int
464stoi(s)
465 char *s;
466{
467 int base = 10;
468 int n = 0;
469
470 if (*s == '0') {
471 if (s[1] == 'x' || s[1] == 'X') {
472 s += 2;
473 base = 16;
474 }
475 else {
476 base = 8;
477 s += 1;
478 }
479 }
480 while (*s)
481 n = n * base + xdtoi(*s++);
482
483 return n;
484}