blob: 06b9acc14ce16d955c642a84d3119c831321e954 [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
Haibo Huang165065a2018-07-23 17:26:52 -07004#include <config.h>
Elliott Hughes965a4b52017-05-15 10:37:39 -07005#endif
Haibo Huang165065a2018-07-23 17:26:52 -07006
7/*
8 * Must come first to avoid warnings on Windows.
9 *
10 * Flex-generated scanners may only include <inttypes.h> if __STDC_VERSION__
11 * is defined with a value >= 199901, meaning "full C99", and MSVC may not
12 * define it with that value, because it isn't 100% C99-compliant, even
13 * though it has an <inttypes.h> capable of defining everything the Flex
14 * scanner needs.
15 *
16 * We, however, will include it if we know we have an MSVC version that has
17 * it; this means that we may define the INTn_MAX and UINTn_MAX values in
18 * scanner.c, and then include <stdint.h>, which may define them differently
19 * (same value, but different string of characters), causing compiler warnings.
20 *
21 * If we include it here, and they're defined, that'll prevent scanner.c
22 * from defining them. So we include <pcap/pcap-inttypes.h>, to get
23 * <inttypes.h> if we have it.
24 */
25#include <pcap/pcap-inttypes.h>
26
27#include "diag-control.h"
Elliott Hughes965a4b52017-05-15 10:37:39 -070028}
29
30/*
31 * We want a reentrant scanner.
32 */
33%option reentrant
34
35/*
36 * And we need to pass the compiler state to the scanner.
37 */
38%option extra-type="compiler_state_t *"
39
40/*
41 * We don't use input, so don't generate code for it.
42 */
43%option noinput
44
45/*
46 * We don't use unput, so don't generate code for it.
47 */
48%option nounput
49
50/*
51 * We don't read from the terminal.
52 */
53%option never-interactive
54
55/*
56 * We want to stop processing when we get to the end of the input.
57 */
58%option noyywrap
59
60/*
61 * We want to generate code that can be used by a reentrant parser
62 * generated by Bison or Berkeley YACC.
63 */
64%option bison-bridge
65
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080066%{
67/*
68 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
69 * The Regents of the University of California. All rights reserved.
70 *
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that: (1) source code distributions
73 * retain the above copyright notice and this paragraph in its entirety, (2)
74 * distributions including binary code include the above copyright notice and
75 * this paragraph in its entirety in the documentation or other materials
76 * provided with the distribution, and (3) all advertising materials mentioning
77 * features or use of this software display the following acknowledgement:
78 * ``This product includes software developed by the University of California,
79 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
80 * the University nor the names of its contributors may be used to endorse
81 * or promote products derived from this software without specific prior
82 * written permission.
83 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
84 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
85 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
86 */
87
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080088#include <string.h>
89
90#include "pcap-int.h"
91
92#include "gencode.h"
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080093
Elliott Hughes965a4b52017-05-15 10:37:39 -070094#include "grammar.h"
95
96/*
97 * Earlier versions of Flex don't declare these, so we declare them
98 * ourselves to squelch warnings.
99 */
100int pcap_get_column(yyscan_t);
101void pcap_set_column(int, yyscan_t);
102
103#ifdef INET6
104
105#ifdef _WIN32
Haibo Huang165065a2018-07-23 17:26:52 -0700106#include <winsock2.h>
107#include <ws2tcpip.h>
Elliott Hughes965a4b52017-05-15 10:37:39 -0700108/*
109 * To quote the MSDN page for getaddrinfo() at
110 *
111 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
112 *
113 * "Support for getaddrinfo on Windows 2000 and older versions
114 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
115 * later. To execute an application that uses this function on earlier
116 * versions of Windows, then you need to include the Ws2tcpip.h and
117 * Wspiapi.h files. When the Wspiapi.h include file is added, the
118 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
119 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
120 * function is implemented in such a way that if the Ws2_32.dll or the
121 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
122 * Preview for Windows 2000) does not include getaddrinfo, then a
123 * version of getaddrinfo is implemented inline based on code in the
124 * Wspiapi.h header file. This inline code will be used on older Windows
125 * platforms that do not natively support the getaddrinfo function."
126 *
Haibo Huang165065a2018-07-23 17:26:52 -0700127 * We use getaddrinfo(), so we include Wspiapi.h here.
Elliott Hughes965a4b52017-05-15 10:37:39 -0700128 */
Haibo Huang165065a2018-07-23 17:26:52 -0700129#include <wspiapi.h>
Elliott Hughes965a4b52017-05-15 10:37:39 -0700130#else /* _WIN32 */
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800131#include <sys/socket.h> /* for "struct sockaddr" in "struct addrinfo" */
132#include <netdb.h> /* for "struct addrinfo" */
Elliott Hughes965a4b52017-05-15 10:37:39 -0700133#endif /* _WIN32 */
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800134
135/* Workaround for AIX 4.3 */
136#if !defined(AI_NUMERICHOST)
137#define AI_NUMERICHOST 0x04
138#endif
Elliott Hughes965a4b52017-05-15 10:37:39 -0700139
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800140#endif /*INET6*/
Elliott Hughes965a4b52017-05-15 10:37:39 -0700141
JP Abgrall511eca32014-02-12 13:46:45 -0800142#include <pcap/namedb.h>
Elliott Hughes965a4b52017-05-15 10:37:39 -0700143#include "grammar.h"
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800144
145#ifdef HAVE_OS_PROTO_H
146#include "os-proto.h"
147#endif
148
Haibo Huangee759ce2021-01-05 21:34:29 -0800149static int stou(char *, YYSTYPE *, compiler_state_t *);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800150
Haibo Huang165065a2018-07-23 17:26:52 -0700151/*
152 * Disable diagnostics in the code generated by Flex.
153 */
154DIAG_OFF_FLEX
155
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800156%}
157
158N ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
159B ([0-9A-Fa-f][0-9A-Fa-f]?)
JP Abgrall511eca32014-02-12 13:46:45 -0800160B2 ([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 -0800161W ([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
162
163%a 18400
164%o 21500
165%e 7600
166%k 4550
167%p 27600
168%n 2000
169
170V680 {W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
171
172V670 ::{W}:{W}:{W}:{W}:{W}:{W}:{W}
173V671 {W}::{W}:{W}:{W}:{W}:{W}:{W}
174V672 {W}:{W}::{W}:{W}:{W}:{W}:{W}
175V673 {W}:{W}:{W}::{W}:{W}:{W}:{W}
176V674 {W}:{W}:{W}:{W}::{W}:{W}:{W}
177V675 {W}:{W}:{W}:{W}:{W}::{W}:{W}
178V676 {W}:{W}:{W}:{W}:{W}:{W}::{W}
179V677 {W}:{W}:{W}:{W}:{W}:{W}:{W}::
180
181V660 ::{W}:{W}:{W}:{W}:{W}:{W}
182V661 {W}::{W}:{W}:{W}:{W}:{W}
183V662 {W}:{W}::{W}:{W}:{W}:{W}
184V663 {W}:{W}:{W}::{W}:{W}:{W}
185V664 {W}:{W}:{W}:{W}::{W}:{W}
186V665 {W}:{W}:{W}:{W}:{W}::{W}
187V666 {W}:{W}:{W}:{W}:{W}:{W}::
188
189V650 ::{W}:{W}:{W}:{W}:{W}
190V651 {W}::{W}:{W}:{W}:{W}
191V652 {W}:{W}::{W}:{W}:{W}
192V653 {W}:{W}:{W}::{W}:{W}
193V654 {W}:{W}:{W}:{W}::{W}
194V655 {W}:{W}:{W}:{W}:{W}::
195
196V640 ::{W}:{W}:{W}:{W}
197V641 {W}::{W}:{W}:{W}
198V642 {W}:{W}::{W}:{W}
199V643 {W}:{W}:{W}::{W}
200V644 {W}:{W}:{W}:{W}::
201
202V630 ::{W}:{W}:{W}
203V631 {W}::{W}:{W}
204V632 {W}:{W}::{W}
205V633 {W}:{W}:{W}::
206
207V620 ::{W}:{W}
208V621 {W}::{W}
209V622 {W}:{W}::
210
211V610 ::{W}
212V611 {W}::
213
214V600 ::
215
216V6604 {W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
217
218V6504 ::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
219V6514 {W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
220V6524 {W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
221V6534 {W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
222V6544 {W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
223V6554 {W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
224
225V6404 ::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
226V6414 {W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
227V6424 {W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
228V6434 {W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
229V6444 {W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
230
231V6304 ::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
232V6314 {W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
233V6324 {W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
234V6334 {W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
235
236V6204 ::{W}:{W}:{N}\.{N}\.{N}\.{N}
237V6214 {W}::{W}:{N}\.{N}\.{N}\.{N}
238V6224 {W}:{W}::{N}\.{N}\.{N}\.{N}
239
240V6104 ::{W}:{N}\.{N}\.{N}\.{N}
241V6114 {W}::{N}\.{N}\.{N}\.{N}
242
243V6004 ::{N}\.{N}\.{N}\.{N}
244
245
246V6 ({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})
247
JP Abgrall511eca32014-02-12 13:46:45 -0800248MAC ({B}:{B}:{B}:{B}:{B}:{B}|{B}\-{B}\-{B}\-{B}\-{B}\-{B}|{B}\.{B}\.{B}\.{B}\.{B}\.{B}|{B2}\.{B2}\.{B2}|{B2}{3})
249
250
251
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800252%%
253dst return DST;
254src return SRC;
255
256link|ether|ppp|slip return LINK;
257fddi|tr|wlan return LINK;
258arp return ARP;
259rarp return RARP;
260ip return IP;
261sctp return SCTP;
262tcp return TCP;
263udp return UDP;
264icmp return ICMP;
265igmp return IGMP;
266igrp return IGRP;
267pim return PIM;
268vrrp return VRRP;
JP Abgrall511eca32014-02-12 13:46:45 -0800269carp return CARP;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800270radio return RADIO;
271
JP Abgrall511eca32014-02-12 13:46:45 -0800272ip6 return IPV6;
273icmp6 return ICMPV6;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800274ah return AH;
275esp return ESP;
276
277atalk return ATALK;
278aarp return AARP;
279decnet return DECNET;
280lat return LAT;
281sca return SCA;
282moprc return MOPRC;
283mopdl return MOPDL;
284
285iso return ISO;
286esis return ESIS;
287es-is return ESIS;
288isis return ISIS;
289is-is return ISIS;
290l1 return L1;
291l2 return L2;
292iih return IIH;
293lsp return LSP;
294snp return SNP;
295csnp return CSNP;
296psnp return PSNP;
297
298clnp return CLNP;
299
300stp return STP;
301
302ipx return IPX;
303
304netbeui return NETBEUI;
305
306host return HOST;
307net return NET;
308mask return NETMASK;
309port return PORT;
310portrange return PORTRANGE;
311proto return PROTO;
Haibo Huang4ccd6832020-04-23 18:03:48 -0700312protochain return PROTOCHAIN;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800313
314gateway return GATEWAY;
315
JP Abgrall511eca32014-02-12 13:46:45 -0800316type return TYPE;
317subtype return SUBTYPE;
318direction|dir return DIR;
319address1|addr1 return ADDR1;
320address2|addr2 return ADDR2;
321address3|addr3 return ADDR3;
322address4|addr4 return ADDR4;
323ra return RA;
324ta return TA;
325
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800326less return LESS;
327greater return GREATER;
328byte return CBYTE;
329broadcast return TK_BROADCAST;
330multicast return TK_MULTICAST;
331
332and|"&&" return AND;
333or|"||" return OR;
334not return '!';
335
336len|length return LEN;
337inbound return INBOUND;
338outbound return OUTBOUND;
339
Haibo Huangee759ce2021-01-05 21:34:29 -0800340ifindex return IFINDEX;
341
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800342vlan return VLAN;
343mpls return MPLS;
344pppoed return PPPOED;
345pppoes return PPPOES;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700346geneve return GENEVE;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800347
348lane return LANE;
349llc return LLC;
350metac return METAC;
351bcc return BCC;
352oam return OAM;
353oamf4 return OAMF4;
354oamf4ec return OAMF4EC;
355oamf4sc return OAMF4SC;
356sc return SC;
357ilmic return ILMIC;
358vpi return VPI;
359vci return VCI;
360connectmsg return CONNECTMSG;
361metaconnect return METACONNECT;
362
363on|ifname return PF_IFNAME;
364rset|ruleset return PF_RSET;
365rnr|rulenum return PF_RNR;
366srnr|subrulenum return PF_SRNR;
367reason return PF_REASON;
368action return PF_ACTION;
369
370fisu return FISU;
371lssu return LSSU;
372lsu return LSSU;
373msu return MSU;
JP Abgrall511eca32014-02-12 13:46:45 -0800374hfisu return HFISU;
375hlssu return HLSSU;
376hmsu return HMSU;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800377sio return SIO;
378opc return OPC;
379dpc return DPC;
380sls return SLS;
JP Abgrall511eca32014-02-12 13:46:45 -0800381hsio return HSIO;
382hopc return HOPC;
383hdpc return HDPC;
384hsls return HSLS;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800385
386[ \r\n\t] ;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700387[+\-*/%:\[\]!<>()&|\^=] return yytext[0];
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800388">=" return GEQ;
389"<=" return LEQ;
390"!=" return NEQ;
391"==" return '=';
392"<<" return LSH;
393">>" return RSH;
Haibo Huang4ccd6832020-04-23 18:03:48 -0700394${B} { yylval->s = sdup(yyextra, yytext); return AID; }
395{MAC} { yylval->s = sdup(yyextra, yytext); return EID; }
Haibo Huangee759ce2021-01-05 21:34:29 -0800396{N} { return stou(yytext, yylval, yyextra); }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800397({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) {
Elliott Hughes965a4b52017-05-15 10:37:39 -0700398 yylval->s = sdup(yyextra, (char *)yytext); return HID; }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800399{V6} {
400#ifdef INET6
401 struct addrinfo hints, *res;
402 memset(&hints, 0, sizeof(hints));
403 hints.ai_family = AF_INET6;
404 hints.ai_flags = AI_NUMERICHOST;
Haibo Huang4ccd6832020-04-23 18:03:48 -0700405 if (getaddrinfo(yytext, NULL, &hints, &res)) {
406 bpf_set_error(yyextra, "bogus IPv6 address %s", yytext);
407 yylval->s = NULL;
408 } else {
JP Abgrall511eca32014-02-12 13:46:45 -0800409 freeaddrinfo(res);
Haibo Huang4ccd6832020-04-23 18:03:48 -0700410 yylval->s = sdup(yyextra, (char *)yytext);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800411 }
412#else
Haibo Huang4ccd6832020-04-23 18:03:48 -0700413 bpf_set_error(yyextra, "IPv6 address %s not supported", yytext);
414 yylval->s = NULL;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800415#endif /*INET6*/
Haibo Huang4ccd6832020-04-23 18:03:48 -0700416 return HID6;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800417 }
Haibo Huang4ccd6832020-04-23 18:03:48 -0700418{B}:+({B}:+)+ { bpf_set_error(yyextra, "bogus ethernet address %s", yytext); yylval->s = NULL; return EID; }
Haibo Huangee759ce2021-01-05 21:34:29 -0800419icmptype { yylval->h = 0; return NUM; }
420icmpcode { yylval->h = 1; return NUM; }
421icmp-echoreply { yylval->h = 0; return NUM; }
422icmp-unreach { yylval->h = 3; return NUM; }
423icmp-sourcequench { yylval->h = 4; return NUM; }
424icmp-redirect { yylval->h = 5; return NUM; }
425icmp-echo { yylval->h = 8; return NUM; }
426icmp-routeradvert { yylval->h = 9; return NUM; }
427icmp-routersolicit { yylval->h = 10; return NUM; }
428icmp-timxceed { yylval->h = 11; return NUM; }
429icmp-paramprob { yylval->h = 12; return NUM; }
430icmp-tstamp { yylval->h = 13; return NUM; }
431icmp-tstampreply { yylval->h = 14; return NUM; }
432icmp-ireq { yylval->h = 15; return NUM; }
433icmp-ireqreply { yylval->h = 16; return NUM; }
434icmp-maskreq { yylval->h = 17; return NUM; }
435icmp-maskreply { yylval->h = 18; return NUM; }
Haibo Huang165065a2018-07-23 17:26:52 -0700436
Haibo Huangee759ce2021-01-05 21:34:29 -0800437icmp6type { yylval->h = 0; return NUM; }
438icmp6code { yylval->h = 1; return NUM; }
Haibo Huang165065a2018-07-23 17:26:52 -0700439
Haibo Huangee759ce2021-01-05 21:34:29 -0800440icmp6-destinationunreach { yylval->h = 1; return NUM; }
441icmp6-packettoobig { yylval->h = 2; return NUM; }
442icmp6-timeexceeded { yylval->h = 3; return NUM; }
443icmp6-parameterproblem { yylval->h = 4; return NUM; }
444icmp6-echo { yylval->h = 128; return NUM; }
445icmp6-echoreply { yylval->h = 129; return NUM; }
446icmp6-multicastlistenerquery { yylval->h = 130; return NUM; }
447icmp6-multicastlistenerreportv1 { yylval->h = 131; return NUM; }
448icmp6-multicastlistenerdone { yylval->h = 132; return NUM; }
449icmp6-routersolicit { yylval->h = 133; return NUM; }
450icmp6-routeradvert { yylval->h = 134; return NUM; }
451icmp6-neighborsolicit { yylval->h = 135; return NUM; }
452icmp6-neighboradvert { yylval->h = 136; return NUM; }
453icmp6-redirect { yylval->h = 137; return NUM; }
454icmp6-routerrenum { yylval->h = 138; return NUM; }
455icmp6-nodeinformationquery { yylval->h = 139; return NUM; }
456icmp6-nodeinformationresponse { yylval->h = 140; return NUM; }
457icmp6-ineighbordiscoverysolicit { yylval->h = 141; return NUM; }
458icmp6-ineighbordiscoveryadvert { yylval->h = 142; return NUM; }
459icmp6-multicastlistenerreportv2 { yylval->h = 143; return NUM; }
460icmp6-homeagentdiscoveryrequest { yylval->h = 144; return NUM; }
461icmp6-homeagentdiscoveryreply { yylval->h = 145; return NUM; }
462icmp6-mobileprefixsolicit { yylval->h = 146; return NUM; }
463icmp6-mobileprefixadvert { yylval->h = 147; return NUM; }
464icmp6-certpathsolicit { yylval->h = 148; return NUM; }
465icmp6-certpathadvert { yylval->h = 149; return NUM; }
466icmp6-multicastrouteradvert { yylval->h = 151; return NUM; }
467icmp6-multicastroutersolicit { yylval->h = 152; return NUM; }
468icmp6-multicastrouterterm { yylval->h = 153; return NUM; }
Haibo Huang165065a2018-07-23 17:26:52 -0700469
Haibo Huangee759ce2021-01-05 21:34:29 -0800470tcpflags { yylval->h = 13; return NUM; }
471tcp-fin { yylval->h = 0x01; return NUM; }
472tcp-syn { yylval->h = 0x02; return NUM; }
473tcp-rst { yylval->h = 0x04; return NUM; }
474tcp-push { yylval->h = 0x08; return NUM; }
475tcp-ack { yylval->h = 0x10; return NUM; }
476tcp-urg { yylval->h = 0x20; return NUM; }
477tcp-ece { yylval->h = 0x40; return NUM; }
478tcp-cwr { yylval->h = 0x80; return NUM; }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800479[A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
Elliott Hughes965a4b52017-05-15 10:37:39 -0700480 yylval->s = sdup(yyextra, (char *)yytext); return ID; }
481"\\"[^ !()\n\t]+ { yylval->s = sdup(yyextra, (char *)yytext + 1); return ID; }
Haibo Huang4ccd6832020-04-23 18:03:48 -0700482. { return LEX_ERROR; }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800483%%
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800484
Haibo Huang165065a2018-07-23 17:26:52 -0700485/*
486 * Turn diagnostics back on, so we check the code that we've written.
487 */
488DIAG_ON_FLEX
489
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800490/*
Haibo Huangee759ce2021-01-05 21:34:29 -0800491 * Convert string to 32-bit unsigned integer. Just like atoi(), but checks for
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800492 * preceding 0x or 0 and uses hex or octal instead of decimal.
Haibo Huangee759ce2021-01-05 21:34:29 -0800493 *
494 * On success, sets yylval->h to the value and returns NUM.
495 * On failure, sets the BPF error string and returns LEX_ERROR, to force
496 * the parse to stop.
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800497 */
498static int
Haibo Huangee759ce2021-01-05 21:34:29 -0800499stou(char *yytext_arg, YYSTYPE *yylval_arg, compiler_state_t *yyextra_arg)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800500{
Haibo Huangee759ce2021-01-05 21:34:29 -0800501 bpf_u_int32 n = 0;
502 unsigned int digit;
503 char *s = yytext_arg;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800504
Haibo Huangee759ce2021-01-05 21:34:29 -0800505 /*
506 * yytext_arg is guaranteed either to be a string of decimal digits
507 * or 0[xX] followed by a string of hex digits.
508 */
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800509 if (*s == '0') {
510 if (s[1] == 'x' || s[1] == 'X') {
Haibo Huangee759ce2021-01-05 21:34:29 -0800511 /*
512 * Begins with 0x or 0X, so hex.
513 * Guaranteed to be all hex digits following the
514 * prefix, so anything that's not 0-9 or a-f is
515 * A-F.
516 */
517 s += 2; /* skip the prefix */
518 while ((digit = *s++) != '\0') {
519 if (digit >= '0' && digit <= '9')
520 digit = digit - '0';
521 else if (digit >= 'a' && digit <= 'f')
522 digit = digit - 'a' + 10;
523 else
524 digit = digit - 'A' + 10;
525
526 /*
527 * Check for overflow.
528 */
529 if (n > 0xFFFFFFFU) {
530 /*
531 * We have more than 28 bits of
532 * number, and are about to
533 * add 4 more; that won't fit
534 * in 32 bits.
535 */
536 bpf_set_error(yyextra_arg,
537 "number %s overflows 32 bits",
538 yytext_arg);
539 return LEX_ERROR;
540 }
541 n = (n << 4) + digit;
542 }
543 } else {
544 /*
545 * Begins with 0, but not 0x or 0X, so octal.
546 * Guaranteed to be all *decimal* digits following
547 * the prefix, so we need to catch 8 and 9 and
548 * report an error.
549 */
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800550 s += 1;
Haibo Huangee759ce2021-01-05 21:34:29 -0800551 while ((digit = *s++) != '\0') {
552 if (digit >= '0' && digit <= '7')
553 digit = digit - '0';
554 else {
555 bpf_set_error(yyextra_arg,
556 "number %s contains non-octal digit",
557 yytext_arg);
558 return LEX_ERROR;
559 }
560 if (n > 03777777777U) {
561 /*
562 * We have more than 29 bits of
563 * number, and are about to add
564 * 3 more; that won't fit in
565 * 32 bits.
566 */
567 bpf_set_error(yyextra_arg,
568 "number %s overflows 32 bits",
569 yytext_arg);
570 return LEX_ERROR;
571 }
572 n = (n << 3) + digit;
573 }
574 }
575 } else {
576 /*
577 * Decimal.
578 */
579 while ((digit = *s++) != '\0') {
580 digit = digit - '0';
581#define CUTOFF_DEC (0xFFFFFFFFU / 10U)
582#define CUTLIM_DEC (0xFFFFFFFFU % 10U)
583 if (n > CUTOFF_DEC ||
584 (n == CUTOFF_DEC && digit > CUTLIM_DEC)) {
585 bpf_set_error(yyextra_arg,
586 "number %s overflows 32 bits",
587 yytext_arg);
588 return LEX_ERROR;
589 }
590 n = (n * 10) + digit;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800591 }
592 }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800593
Haibo Huangee759ce2021-01-05 21:34:29 -0800594 yylval_arg->h = n;
595 return NUM;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800596}