1ada6f083SXin LI %top {
2ada6f083SXin LI /* Must come first for _LARGE_FILE_API on AIX. */
3b00ab754SHans Petter Selasky #include <config.h>
4b00ab754SHans Petter Selasky
5b00ab754SHans Petter Selasky /*
6b00ab754SHans Petter Selasky * Must come first to avoid warnings on Windows.
7b00ab754SHans Petter Selasky *
8b00ab754SHans Petter Selasky * Flex-generated scanners may only include <inttypes.h> if __STDC_VERSION__
9b00ab754SHans Petter Selasky * is defined with a value >= 199901, meaning "full C99", and MSVC may not
10b00ab754SHans Petter Selasky * define it with that value, because it isn't 100% C99-compliant, even
11b00ab754SHans Petter Selasky * though it has an <inttypes.h> capable of defining everything the Flex
12b00ab754SHans Petter Selasky * scanner needs.
13b00ab754SHans Petter Selasky *
14b00ab754SHans Petter Selasky * We, however, will include it if we know we have an MSVC version that has
15b00ab754SHans Petter Selasky * it; this means that we may define the INTn_MAX and UINTn_MAX values in
16b00ab754SHans Petter Selasky * scanner.c, and then include <stdint.h>, which may define them differently
17b00ab754SHans Petter Selasky * (same value, but different string of characters), causing compiler warnings.
18b00ab754SHans Petter Selasky *
19b00ab754SHans Petter Selasky * If we include it here, and they're defined, that'll prevent scanner.c
20b00ab754SHans Petter Selasky * from defining them. So we include <pcap/pcap-inttypes.h>, to get
21b00ab754SHans Petter Selasky * <inttypes.h> if we have it.
22b00ab754SHans Petter Selasky */
23b00ab754SHans Petter Selasky #include <pcap/pcap-inttypes.h>
24b00ab754SHans Petter Selasky
256f9cba8fSJoseph Mingrone /*
266f9cba8fSJoseph Mingrone * grammar.h requires gencode.h and sometimes breaks in a polluted namespace
276f9cba8fSJoseph Mingrone * (see ftmacros.h), so include it early.
286f9cba8fSJoseph Mingrone */
296f9cba8fSJoseph Mingrone #include "gencode.h"
306f9cba8fSJoseph Mingrone #include "grammar.h"
316f9cba8fSJoseph Mingrone
32b00ab754SHans Petter Selasky #include "diag-control.h"
33*afdbf109SJoseph Mingrone
34*afdbf109SJoseph Mingrone /*
35*afdbf109SJoseph Mingrone * Convert string to 32-bit unsigned integer; the string starts at
36*afdbf109SJoseph Mingrone * string and is string_len bytes long.
37*afdbf109SJoseph Mingrone *
38*afdbf109SJoseph Mingrone * On success, sets *val to the value and returns 1.
39*afdbf109SJoseph Mingrone * On failure, sets the BPF error string and returns 0.
40*afdbf109SJoseph Mingrone *
41*afdbf109SJoseph Mingrone * Also used in gencode.c
42*afdbf109SJoseph Mingrone */
43*afdbf109SJoseph Mingrone typedef enum {
44*afdbf109SJoseph Mingrone STOULEN_OK,
45*afdbf109SJoseph Mingrone STOULEN_NOT_HEX_NUMBER,
46*afdbf109SJoseph Mingrone STOULEN_NOT_OCTAL_NUMBER,
47*afdbf109SJoseph Mingrone STOULEN_NOT_DECIMAL_NUMBER,
48*afdbf109SJoseph Mingrone STOULEN_ERROR
49*afdbf109SJoseph Mingrone } stoulen_ret;
50*afdbf109SJoseph Mingrone
51*afdbf109SJoseph Mingrone stoulen_ret stoulen(const char *string, size_t stringlen, bpf_u_int32 *val,
52*afdbf109SJoseph Mingrone compiler_state_t *cstate);
53ada6f083SXin LI }
54ada6f083SXin LI
55ada6f083SXin LI /*
56ada6f083SXin LI * We want a reentrant scanner.
57ada6f083SXin LI */
58ada6f083SXin LI %option reentrant
59ada6f083SXin LI
60ada6f083SXin LI /*
61ada6f083SXin LI * And we need to pass the compiler state to the scanner.
62ada6f083SXin LI */
63ada6f083SXin LI %option extra-type="compiler_state_t *"
64ada6f083SXin LI
65ada6f083SXin LI /*
66ada6f083SXin LI * We don't use input, so don't generate code for it.
67ada6f083SXin LI */
68ada6f083SXin LI %option noinput
69ada6f083SXin LI
70ada6f083SXin LI /*
71ada6f083SXin LI * We don't use unput, so don't generate code for it.
72ada6f083SXin LI */
73ada6f083SXin LI %option nounput
74ada6f083SXin LI
75ada6f083SXin LI /*
76ada6f083SXin LI * We don't read from the terminal.
77ada6f083SXin LI */
78ada6f083SXin LI %option never-interactive
79ada6f083SXin LI
80ada6f083SXin LI /*
81ada6f083SXin LI * We want to stop processing when we get to the end of the input.
82ada6f083SXin LI */
83ada6f083SXin LI %option noyywrap
84ada6f083SXin LI
85ada6f083SXin LI /*
86ada6f083SXin LI * We want to generate code that can be used by a reentrant parser
87ada6f083SXin LI * generated by Bison or Berkeley YACC.
88ada6f083SXin LI */
89ada6f083SXin LI %option bison-bridge
90ada6f083SXin LI
918cf6c252SPaul Traina %{
928cf6c252SPaul Traina /*
93dfd1ee14SBill Fenner * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
948cf6c252SPaul Traina * The Regents of the University of California. All rights reserved.
958cf6c252SPaul Traina *
968cf6c252SPaul Traina * Redistribution and use in source and binary forms, with or without
978cf6c252SPaul Traina * modification, are permitted provided that: (1) source code distributions
988cf6c252SPaul Traina * retain the above copyright notice and this paragraph in its entirety, (2)
998cf6c252SPaul Traina * distributions including binary code include the above copyright notice and
1008cf6c252SPaul Traina * this paragraph in its entirety in the documentation or other materials
1018cf6c252SPaul Traina * provided with the distribution, and (3) all advertising materials mentioning
1028cf6c252SPaul Traina * features or use of this software display the following acknowledgement:
1038cf6c252SPaul Traina * ``This product includes software developed by the University of California,
1048cf6c252SPaul Traina * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1058cf6c252SPaul Traina * the University nor the names of its contributors may be used to endorse
1068cf6c252SPaul Traina * or promote products derived from this software without specific prior
1078cf6c252SPaul Traina * written permission.
1088cf6c252SPaul Traina * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1098cf6c252SPaul Traina * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1108cf6c252SPaul Traina * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1118cf6c252SPaul Traina */
1128cf6c252SPaul Traina
113fae05455SBill Fenner #include <string.h>
1148cf6c252SPaul Traina
1158cf6c252SPaul Traina #include "pcap-int.h"
1168cf6c252SPaul Traina
117ada6f083SXin LI /*
118ada6f083SXin LI * Earlier versions of Flex don't declare these, so we declare them
119ada6f083SXin LI * ourselves to squelch warnings.
120ada6f083SXin LI */
121ada6f083SXin LI int pcap_get_column(yyscan_t);
122ada6f083SXin LI void pcap_set_column(int, yyscan_t);
123ada6f083SXin LI
124ada6f083SXin LI #ifdef INET6
125ada6f083SXin LI
126ada6f083SXin LI #ifdef _WIN32
127b00ab754SHans Petter Selasky #include <winsock2.h>
128b00ab754SHans Petter Selasky #include <ws2tcpip.h>
129ada6f083SXin LI /*
130ada6f083SXin LI * To quote the MSDN page for getaddrinfo() at
131ada6f083SXin LI *
132ada6f083SXin LI * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
133ada6f083SXin LI *
134ada6f083SXin LI * "Support for getaddrinfo on Windows 2000 and older versions
135ada6f083SXin LI * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
136ada6f083SXin LI * later. To execute an application that uses this function on earlier
137ada6f083SXin LI * versions of Windows, then you need to include the Ws2tcpip.h and
138ada6f083SXin LI * Wspiapi.h files. When the Wspiapi.h include file is added, the
139ada6f083SXin LI * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
140ada6f083SXin LI * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
141ada6f083SXin LI * function is implemented in such a way that if the Ws2_32.dll or the
142ada6f083SXin LI * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
143ada6f083SXin LI * Preview for Windows 2000) does not include getaddrinfo, then a
144ada6f083SXin LI * version of getaddrinfo is implemented inline based on code in the
145ada6f083SXin LI * Wspiapi.h header file. This inline code will be used on older Windows
146ada6f083SXin LI * platforms that do not natively support the getaddrinfo function."
147ada6f083SXin LI *
148b00ab754SHans Petter Selasky * We use getaddrinfo(), so we include Wspiapi.h here.
149ada6f083SXin LI */
150b00ab754SHans Petter Selasky #include <wspiapi.h>
151ada6f083SXin LI #else /* _WIN32 */
152eb9f0330SBruce M Simpson #include <sys/socket.h> /* for "struct sockaddr" in "struct addrinfo" */
153eb9f0330SBruce M Simpson #include <netdb.h> /* for "struct addrinfo" */
154ada6f083SXin LI #endif /* _WIN32 */
155eb9f0330SBruce M Simpson
1568e1481d1SBill Fenner /* Workaround for AIX 4.3 */
1578e1481d1SBill Fenner #if !defined(AI_NUMERICHOST)
1588e1481d1SBill Fenner #define AI_NUMERICHOST 0x04
1598e1481d1SBill Fenner #endif
160ada6f083SXin LI
161fae05455SBill Fenner #endif /*INET6*/
162ada6f083SXin LI
163a8e07101SRui Paulo #include <pcap/namedb.h>
164ada6f083SXin LI #include "grammar.h"
1658cf6c252SPaul Traina
1668cf6c252SPaul Traina #ifdef HAVE_OS_PROTO_H
1678cf6c252SPaul Traina #include "os-proto.h"
1688cf6c252SPaul Traina #endif
1698cf6c252SPaul Traina
170*afdbf109SJoseph Mingrone static int stou(const char *, YYSTYPE *, compiler_state_t *);
1718cf6c252SPaul Traina
172b00ab754SHans Petter Selasky /*
173b00ab754SHans Petter Selasky * Disable diagnostics in the code generated by Flex.
174b00ab754SHans Petter Selasky */
175b00ab754SHans Petter Selasky DIAG_OFF_FLEX
176b00ab754SHans Petter Selasky
1778cf6c252SPaul Traina %}
1788cf6c252SPaul Traina
1798cf6c252SPaul Traina N ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
1808cf6c252SPaul Traina B ([0-9A-Fa-f][0-9A-Fa-f]?)
181a8e07101SRui Paulo B2 ([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])
182fae05455SBill Fenner W ([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
1838cf6c252SPaul Traina
1845357e0feSMax Laier %a 18400
1855357e0feSMax Laier %o 21500
1865357e0feSMax Laier %e 7600
1875357e0feSMax Laier %k 4550
1885357e0feSMax Laier %p 27600
1898e1481d1SBill Fenner %n 2000
190fae05455SBill Fenner
191fae05455SBill Fenner V680 {W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
192fae05455SBill Fenner
193fae05455SBill Fenner V670 ::{W}:{W}:{W}:{W}:{W}:{W}:{W}
194fae05455SBill Fenner V671 {W}::{W}:{W}:{W}:{W}:{W}:{W}
195fae05455SBill Fenner V672 {W}:{W}::{W}:{W}:{W}:{W}:{W}
196fae05455SBill Fenner V673 {W}:{W}:{W}::{W}:{W}:{W}:{W}
197fae05455SBill Fenner V674 {W}:{W}:{W}:{W}::{W}:{W}:{W}
198fae05455SBill Fenner V675 {W}:{W}:{W}:{W}:{W}::{W}:{W}
199fae05455SBill Fenner V676 {W}:{W}:{W}:{W}:{W}:{W}::{W}
200fae05455SBill Fenner V677 {W}:{W}:{W}:{W}:{W}:{W}:{W}::
201fae05455SBill Fenner
202fae05455SBill Fenner V660 ::{W}:{W}:{W}:{W}:{W}:{W}
203fae05455SBill Fenner V661 {W}::{W}:{W}:{W}:{W}:{W}
204fae05455SBill Fenner V662 {W}:{W}::{W}:{W}:{W}:{W}
205fae05455SBill Fenner V663 {W}:{W}:{W}::{W}:{W}:{W}
206fae05455SBill Fenner V664 {W}:{W}:{W}:{W}::{W}:{W}
207fae05455SBill Fenner V665 {W}:{W}:{W}:{W}:{W}::{W}
208fae05455SBill Fenner V666 {W}:{W}:{W}:{W}:{W}:{W}::
209fae05455SBill Fenner
210fae05455SBill Fenner V650 ::{W}:{W}:{W}:{W}:{W}
211fae05455SBill Fenner V651 {W}::{W}:{W}:{W}:{W}
212fae05455SBill Fenner V652 {W}:{W}::{W}:{W}:{W}
213fae05455SBill Fenner V653 {W}:{W}:{W}::{W}:{W}
214fae05455SBill Fenner V654 {W}:{W}:{W}:{W}::{W}
215fae05455SBill Fenner V655 {W}:{W}:{W}:{W}:{W}::
216fae05455SBill Fenner
217fae05455SBill Fenner V640 ::{W}:{W}:{W}:{W}
218fae05455SBill Fenner V641 {W}::{W}:{W}:{W}
219fae05455SBill Fenner V642 {W}:{W}::{W}:{W}
220fae05455SBill Fenner V643 {W}:{W}:{W}::{W}
221fae05455SBill Fenner V644 {W}:{W}:{W}:{W}::
222fae05455SBill Fenner
223fae05455SBill Fenner V630 ::{W}:{W}:{W}
224fae05455SBill Fenner V631 {W}::{W}:{W}
225fae05455SBill Fenner V632 {W}:{W}::{W}
226fae05455SBill Fenner V633 {W}:{W}:{W}::
227fae05455SBill Fenner
228fae05455SBill Fenner V620 ::{W}:{W}
229fae05455SBill Fenner V621 {W}::{W}
230fae05455SBill Fenner V622 {W}:{W}::
231fae05455SBill Fenner
232fae05455SBill Fenner V610 ::{W}
233fae05455SBill Fenner V611 {W}::
234fae05455SBill Fenner
235fae05455SBill Fenner V600 ::
236fae05455SBill Fenner
237fae05455SBill Fenner V6604 {W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
238fae05455SBill Fenner
239fae05455SBill Fenner V6504 ::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
240fae05455SBill Fenner V6514 {W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
241fae05455SBill Fenner V6524 {W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
242fae05455SBill Fenner V6534 {W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
243fae05455SBill Fenner V6544 {W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
244fae05455SBill Fenner V6554 {W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
245fae05455SBill Fenner
246fae05455SBill Fenner V6404 ::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
247fae05455SBill Fenner V6414 {W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
248fae05455SBill Fenner V6424 {W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
249fae05455SBill Fenner V6434 {W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
250fae05455SBill Fenner V6444 {W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
251fae05455SBill Fenner
252fae05455SBill Fenner V6304 ::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
253fae05455SBill Fenner V6314 {W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
254fae05455SBill Fenner V6324 {W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
255fae05455SBill Fenner V6334 {W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
256fae05455SBill Fenner
257fae05455SBill Fenner V6204 ::{W}:{W}:{N}\.{N}\.{N}\.{N}
258fae05455SBill Fenner V6214 {W}::{W}:{N}\.{N}\.{N}\.{N}
259fae05455SBill Fenner V6224 {W}:{W}::{N}\.{N}\.{N}\.{N}
260fae05455SBill Fenner
261fae05455SBill Fenner V6104 ::{W}:{N}\.{N}\.{N}\.{N}
262fae05455SBill Fenner V6114 {W}::{N}\.{N}\.{N}\.{N}
263fae05455SBill Fenner
264fae05455SBill Fenner V6004 ::{N}\.{N}\.{N}\.{N}
265fae05455SBill Fenner
266fae05455SBill Fenner
267fae05455SBill Fenner V6 ({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})
2688cf6c252SPaul Traina
269a8e07101SRui Paulo MAC ({B}:{B}:{B}:{B}:{B}:{B}|{B}\-{B}\-{B}\-{B}\-{B}\-{B}|{B}\.{B}\.{B}\.{B}\.{B}\.{B}|{B2}\.{B2}\.{B2}|{B2}{3})
270a8e07101SRui Paulo
271a8e07101SRui Paulo
272a8e07101SRui Paulo
2738cf6c252SPaul Traina %%
2748cf6c252SPaul Traina dst return DST;
2758cf6c252SPaul Traina src return SRC;
2768cf6c252SPaul Traina
2778cf6c252SPaul Traina link|ether|ppp|slip return LINK;
278eb9f0330SBruce M Simpson fddi|tr|wlan return LINK;
2798cf6c252SPaul Traina arp return ARP;
2808cf6c252SPaul Traina rarp return RARP;
2818cf6c252SPaul Traina ip return IP;
282c0653930SBill Fenner sctp return SCTP;
2838cf6c252SPaul Traina tcp return TCP;
2848cf6c252SPaul Traina udp return UDP;
2858cf6c252SPaul Traina icmp return ICMP;
2868cf6c252SPaul Traina igmp return IGMP;
2878cf6c252SPaul Traina igrp return IGRP;
288fae05455SBill Fenner pim return PIM;
289c0653930SBill Fenner vrrp return VRRP;
290d1e87331SXin LI carp return CARP;
2914238c2cdSSam Leffler radio return RADIO;
292fae05455SBill Fenner
293edc89b24SXin LI ip6 return IPV6;
294edc89b24SXin LI icmp6 return ICMPV6;
295fae05455SBill Fenner ah return AH;
296fae05455SBill Fenner esp return ESP;
2978cf6c252SPaul Traina
2988cf6c252SPaul Traina atalk return ATALK;
2998e1481d1SBill Fenner aarp return AARP;
3008cf6c252SPaul Traina decnet return DECNET;
3018cf6c252SPaul Traina lat return LAT;
3028cf6c252SPaul Traina sca return SCA;
3038cf6c252SPaul Traina moprc return MOPRC;
3048cf6c252SPaul Traina mopdl return MOPDL;
3058cf6c252SPaul Traina
3060d086d29SPaul Traina iso return ISO;
3070d086d29SPaul Traina esis return ESIS;
3080d086d29SPaul Traina es-is return ESIS;
3090d086d29SPaul Traina isis return ISIS;
3100d086d29SPaul Traina is-is return ISIS;
311eb9f0330SBruce M Simpson l1 return L1;
312eb9f0330SBruce M Simpson l2 return L2;
313eb9f0330SBruce M Simpson iih return IIH;
314eb9f0330SBruce M Simpson lsp return LSP;
315eb9f0330SBruce M Simpson snp return SNP;
316eb9f0330SBruce M Simpson csnp return CSNP;
317eb9f0330SBruce M Simpson psnp return PSNP;
318eb9f0330SBruce M Simpson
3198e1481d1SBill Fenner clnp return CLNP;
3200d086d29SPaul Traina
321c0653930SBill Fenner stp return STP;
322c0653930SBill Fenner
323c0653930SBill Fenner ipx return IPX;
324c0653930SBill Fenner
325c0653930SBill Fenner netbeui return NETBEUI;
326c0653930SBill Fenner
3278cf6c252SPaul Traina host return HOST;
3288cf6c252SPaul Traina net return NET;
329eb9f0330SBruce M Simpson mask return NETMASK;
3308cf6c252SPaul Traina port return PORT;
3314238c2cdSSam Leffler portrange return PORTRANGE;
3328cf6c252SPaul Traina proto return PROTO;
33357e22627SCy Schubert protochain return PROTOCHAIN;
3348cf6c252SPaul Traina
3358cf6c252SPaul Traina gateway return GATEWAY;
3368cf6c252SPaul Traina
337a8e07101SRui Paulo type return TYPE;
338a8e07101SRui Paulo subtype return SUBTYPE;
339a8e07101SRui Paulo direction|dir return DIR;
340a8e07101SRui Paulo address1|addr1 return ADDR1;
341a8e07101SRui Paulo address2|addr2 return ADDR2;
342a8e07101SRui Paulo address3|addr3 return ADDR3;
343a8e07101SRui Paulo address4|addr4 return ADDR4;
344d1e87331SXin LI ra return RA;
345d1e87331SXin LI ta return TA;
346a8e07101SRui Paulo
3478cf6c252SPaul Traina less return LESS;
3488cf6c252SPaul Traina greater return GREATER;
349eb9f0330SBruce M Simpson byte return CBYTE;
3508cf6c252SPaul Traina broadcast return TK_BROADCAST;
3518cf6c252SPaul Traina multicast return TK_MULTICAST;
3528cf6c252SPaul Traina
3538cf6c252SPaul Traina and|"&&" return AND;
3548cf6c252SPaul Traina or|"||" return OR;
3558cf6c252SPaul Traina not return '!';
3568cf6c252SPaul Traina
3578cf6c252SPaul Traina len|length return LEN;
3588cf6c252SPaul Traina inbound return INBOUND;
3598cf6c252SPaul Traina outbound return OUTBOUND;
3608cf6c252SPaul Traina
3616f9cba8fSJoseph Mingrone ifindex return IFINDEX;
3626f9cba8fSJoseph Mingrone
3638e1481d1SBill Fenner vlan return VLAN;
364c761ebcbSSam Leffler mpls return MPLS;
365ff252dbeSSam Leffler pppoed return PPPOED;
366ff252dbeSSam Leffler pppoes return PPPOES;
367ada6f083SXin LI geneve return GENEVE;
3688e1481d1SBill Fenner
369eb9f0330SBruce M Simpson lane return LANE;
370eb9f0330SBruce M Simpson llc return LLC;
371eb9f0330SBruce M Simpson metac return METAC;
372eb9f0330SBruce M Simpson bcc return BCC;
373eb9f0330SBruce M Simpson oam return OAM;
374eb9f0330SBruce M Simpson oamf4 return OAMF4;
375eb9f0330SBruce M Simpson oamf4ec return OAMF4EC;
376eb9f0330SBruce M Simpson oamf4sc return OAMF4SC;
377eb9f0330SBruce M Simpson sc return SC;
378eb9f0330SBruce M Simpson ilmic return ILMIC;
379eb9f0330SBruce M Simpson vpi return VPI;
380eb9f0330SBruce M Simpson vci return VCI;
381eb9f0330SBruce M Simpson connectmsg return CONNECTMSG;
382eb9f0330SBruce M Simpson metaconnect return METACONNECT;
383eb9f0330SBruce M Simpson
384eb9f0330SBruce M Simpson on|ifname return PF_IFNAME;
385eb9f0330SBruce M Simpson rset|ruleset return PF_RSET;
386eb9f0330SBruce M Simpson rnr|rulenum return PF_RNR;
387eb9f0330SBruce M Simpson srnr|subrulenum return PF_SRNR;
388eb9f0330SBruce M Simpson reason return PF_REASON;
389eb9f0330SBruce M Simpson action return PF_ACTION;
390eb9f0330SBruce M Simpson
3915357e0feSMax Laier fisu return FISU;
3925357e0feSMax Laier lssu return LSSU;
3935357e0feSMax Laier lsu return LSSU;
3945357e0feSMax Laier msu return MSU;
395681ed54cSXin LI hfisu return HFISU;
396681ed54cSXin LI hlssu return HLSSU;
397681ed54cSXin LI hmsu return HMSU;
3984238c2cdSSam Leffler sio return SIO;
3994238c2cdSSam Leffler opc return OPC;
4004238c2cdSSam Leffler dpc return DPC;
4014238c2cdSSam Leffler sls return SLS;
402681ed54cSXin LI hsio return HSIO;
403681ed54cSXin LI hopc return HOPC;
404681ed54cSXin LI hdpc return HDPC;
405681ed54cSXin LI hsls return HSLS;
4064238c2cdSSam Leffler
407eb9f0330SBruce M Simpson [ \r\n\t] ;
408681ed54cSXin LI [+\-*/%:\[\]!<>()&|\^=] return yytext[0];
4098cf6c252SPaul Traina ">=" return GEQ;
4108cf6c252SPaul Traina "<=" return LEQ;
4118cf6c252SPaul Traina "!=" return NEQ;
4128cf6c252SPaul Traina "==" return '=';
4138cf6c252SPaul Traina "<<" return LSH;
4148cf6c252SPaul Traina ">>" return RSH;
41557e22627SCy Schubert ${B} { yylval->s = sdup(yyextra, yytext); return AID; }
41657e22627SCy Schubert {MAC} { yylval->s = sdup(yyextra, yytext); return EID; }
4176f9cba8fSJoseph Mingrone {N} { return stou(yytext, yylval, yyextra); }
4188cf6c252SPaul Traina ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) {
419ada6f083SXin LI yylval->s = sdup(yyextra, (char *)yytext); return HID; }
420fae05455SBill Fenner {V6} {
421fae05455SBill Fenner #ifdef INET6
422fae05455SBill Fenner struct addrinfo hints, *res;
423fae05455SBill Fenner memset(&hints, 0, sizeof(hints));
424fae05455SBill Fenner hints.ai_family = AF_INET6;
425fae05455SBill Fenner hints.ai_flags = AI_NUMERICHOST;
42657e22627SCy Schubert if (getaddrinfo(yytext, NULL, &hints, &res)) {
42757e22627SCy Schubert bpf_set_error(yyextra, "bogus IPv6 address %s", yytext);
42857e22627SCy Schubert yylval->s = NULL;
42957e22627SCy Schubert } else {
430a0ee43a1SRui Paulo freeaddrinfo(res);
43157e22627SCy Schubert yylval->s = sdup(yyextra, (char *)yytext);
432fae05455SBill Fenner }
433fae05455SBill Fenner #else
43457e22627SCy Schubert bpf_set_error(yyextra, "IPv6 address %s not supported", yytext);
43557e22627SCy Schubert yylval->s = NULL;
436fae05455SBill Fenner #endif /*INET6*/
43757e22627SCy Schubert return HID6;
438fae05455SBill Fenner }
43957e22627SCy Schubert {B}:+({B}:+)+ { bpf_set_error(yyextra, "bogus ethernet address %s", yytext); yylval->s = NULL; return EID; }
4406f9cba8fSJoseph Mingrone icmptype { yylval->h = 0; return NUM; }
4416f9cba8fSJoseph Mingrone icmpcode { yylval->h = 1; return NUM; }
4426f9cba8fSJoseph Mingrone icmp-echoreply { yylval->h = 0; return NUM; }
4436f9cba8fSJoseph Mingrone icmp-unreach { yylval->h = 3; return NUM; }
4446f9cba8fSJoseph Mingrone icmp-sourcequench { yylval->h = 4; return NUM; }
4456f9cba8fSJoseph Mingrone icmp-redirect { yylval->h = 5; return NUM; }
4466f9cba8fSJoseph Mingrone icmp-echo { yylval->h = 8; return NUM; }
4476f9cba8fSJoseph Mingrone icmp-routeradvert { yylval->h = 9; return NUM; }
4486f9cba8fSJoseph Mingrone icmp-routersolicit { yylval->h = 10; return NUM; }
4496f9cba8fSJoseph Mingrone icmp-timxceed { yylval->h = 11; return NUM; }
4506f9cba8fSJoseph Mingrone icmp-paramprob { yylval->h = 12; return NUM; }
4516f9cba8fSJoseph Mingrone icmp-tstamp { yylval->h = 13; return NUM; }
4526f9cba8fSJoseph Mingrone icmp-tstampreply { yylval->h = 14; return NUM; }
4536f9cba8fSJoseph Mingrone icmp-ireq { yylval->h = 15; return NUM; }
4546f9cba8fSJoseph Mingrone icmp-ireqreply { yylval->h = 16; return NUM; }
4556f9cba8fSJoseph Mingrone icmp-maskreq { yylval->h = 17; return NUM; }
4566f9cba8fSJoseph Mingrone icmp-maskreply { yylval->h = 18; return NUM; }
457b00ab754SHans Petter Selasky
4586f9cba8fSJoseph Mingrone icmp6type { yylval->h = 0; return NUM; }
4596f9cba8fSJoseph Mingrone icmp6code { yylval->h = 1; return NUM; }
460b00ab754SHans Petter Selasky
4616f9cba8fSJoseph Mingrone icmp6-destinationunreach { yylval->h = 1; return NUM; }
4626f9cba8fSJoseph Mingrone icmp6-packettoobig { yylval->h = 2; return NUM; }
4636f9cba8fSJoseph Mingrone icmp6-timeexceeded { yylval->h = 3; return NUM; }
4646f9cba8fSJoseph Mingrone icmp6-parameterproblem { yylval->h = 4; return NUM; }
4656f9cba8fSJoseph Mingrone icmp6-echo { yylval->h = 128; return NUM; }
4666f9cba8fSJoseph Mingrone icmp6-echoreply { yylval->h = 129; return NUM; }
4676f9cba8fSJoseph Mingrone icmp6-multicastlistenerquery { yylval->h = 130; return NUM; }
4686f9cba8fSJoseph Mingrone icmp6-multicastlistenerreportv1 { yylval->h = 131; return NUM; }
4696f9cba8fSJoseph Mingrone icmp6-multicastlistenerdone { yylval->h = 132; return NUM; }
4706f9cba8fSJoseph Mingrone icmp6-routersolicit { yylval->h = 133; return NUM; }
4716f9cba8fSJoseph Mingrone icmp6-routeradvert { yylval->h = 134; return NUM; }
4726f9cba8fSJoseph Mingrone icmp6-neighborsolicit { yylval->h = 135; return NUM; }
4736f9cba8fSJoseph Mingrone icmp6-neighboradvert { yylval->h = 136; return NUM; }
4746f9cba8fSJoseph Mingrone icmp6-redirect { yylval->h = 137; return NUM; }
4756f9cba8fSJoseph Mingrone icmp6-routerrenum { yylval->h = 138; return NUM; }
4766f9cba8fSJoseph Mingrone icmp6-nodeinformationquery { yylval->h = 139; return NUM; }
4776f9cba8fSJoseph Mingrone icmp6-nodeinformationresponse { yylval->h = 140; return NUM; }
4786f9cba8fSJoseph Mingrone icmp6-ineighbordiscoverysolicit { yylval->h = 141; return NUM; }
4796f9cba8fSJoseph Mingrone icmp6-ineighbordiscoveryadvert { yylval->h = 142; return NUM; }
4806f9cba8fSJoseph Mingrone icmp6-multicastlistenerreportv2 { yylval->h = 143; return NUM; }
4816f9cba8fSJoseph Mingrone icmp6-homeagentdiscoveryrequest { yylval->h = 144; return NUM; }
4826f9cba8fSJoseph Mingrone icmp6-homeagentdiscoveryreply { yylval->h = 145; return NUM; }
4836f9cba8fSJoseph Mingrone icmp6-mobileprefixsolicit { yylval->h = 146; return NUM; }
4846f9cba8fSJoseph Mingrone icmp6-mobileprefixadvert { yylval->h = 147; return NUM; }
4856f9cba8fSJoseph Mingrone icmp6-certpathsolicit { yylval->h = 148; return NUM; }
4866f9cba8fSJoseph Mingrone icmp6-certpathadvert { yylval->h = 149; return NUM; }
4876f9cba8fSJoseph Mingrone icmp6-multicastrouteradvert { yylval->h = 151; return NUM; }
4886f9cba8fSJoseph Mingrone icmp6-multicastroutersolicit { yylval->h = 152; return NUM; }
4896f9cba8fSJoseph Mingrone icmp6-multicastrouterterm { yylval->h = 153; return NUM; }
490b00ab754SHans Petter Selasky
4916f9cba8fSJoseph Mingrone tcpflags { yylval->h = 13; return NUM; }
4926f9cba8fSJoseph Mingrone tcp-fin { yylval->h = 0x01; return NUM; }
4936f9cba8fSJoseph Mingrone tcp-syn { yylval->h = 0x02; return NUM; }
4946f9cba8fSJoseph Mingrone tcp-rst { yylval->h = 0x04; return NUM; }
4956f9cba8fSJoseph Mingrone tcp-push { yylval->h = 0x08; return NUM; }
4966f9cba8fSJoseph Mingrone tcp-ack { yylval->h = 0x10; return NUM; }
4976f9cba8fSJoseph Mingrone tcp-urg { yylval->h = 0x20; return NUM; }
4986f9cba8fSJoseph Mingrone tcp-ece { yylval->h = 0x40; return NUM; }
4996f9cba8fSJoseph Mingrone tcp-cwr { yylval->h = 0x80; return NUM; }
500c0653930SBill Fenner [A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
501ada6f083SXin LI yylval->s = sdup(yyextra, (char *)yytext); return ID; }
502ada6f083SXin LI "\\"[^ !()\n\t]+ { yylval->s = sdup(yyextra, (char *)yytext + 1); return ID; }
50357e22627SCy Schubert . { return LEX_ERROR; }
5048cf6c252SPaul Traina %%
5058cf6c252SPaul Traina
506b00ab754SHans Petter Selasky /*
507b00ab754SHans Petter Selasky * Turn diagnostics back on, so we check the code that we've written.
508b00ab754SHans Petter Selasky */
509b00ab754SHans Petter Selasky DIAG_ON_FLEX
510b00ab754SHans Petter Selasky
511*afdbf109SJoseph Mingrone stoulen_ret
512*afdbf109SJoseph Mingrone stoulen(const char *string, size_t string_len, bpf_u_int32 *val,
513*afdbf109SJoseph Mingrone compiler_state_t *cstate)
5148cf6c252SPaul Traina {
5156f9cba8fSJoseph Mingrone bpf_u_int32 n = 0;
5166f9cba8fSJoseph Mingrone unsigned int digit;
517*afdbf109SJoseph Mingrone const char *s = string;
5188cf6c252SPaul Traina
5196f9cba8fSJoseph Mingrone /*
520*afdbf109SJoseph Mingrone * string is guaranteed either to be a string of decimal digits
5216f9cba8fSJoseph Mingrone * or 0[xX] followed by a string of hex digits.
5226f9cba8fSJoseph Mingrone */
523*afdbf109SJoseph Mingrone if (string_len >= 1 && *s == '0') {
524*afdbf109SJoseph Mingrone if (string_len >= 2 && (s[1] == 'x' || s[1] == 'X')) {
5256f9cba8fSJoseph Mingrone /*
5266f9cba8fSJoseph Mingrone * Begins with 0x or 0X, so hex.
5276f9cba8fSJoseph Mingrone * Guaranteed to be all hex digits following the
5286f9cba8fSJoseph Mingrone * prefix, so anything that's not 0-9 or a-f is
5296f9cba8fSJoseph Mingrone * A-F.
5306f9cba8fSJoseph Mingrone */
5316f9cba8fSJoseph Mingrone s += 2; /* skip the prefix */
532*afdbf109SJoseph Mingrone string_len -= 2;
533*afdbf109SJoseph Mingrone while (string_len != 0) {
534*afdbf109SJoseph Mingrone digit = *s++;
535*afdbf109SJoseph Mingrone string_len--;
5366f9cba8fSJoseph Mingrone if (digit >= '0' && digit <= '9')
5376f9cba8fSJoseph Mingrone digit = digit - '0';
5386f9cba8fSJoseph Mingrone else if (digit >= 'a' && digit <= 'f')
5396f9cba8fSJoseph Mingrone digit = digit - 'a' + 10;
540*afdbf109SJoseph Mingrone else if (digit >= 'A' && digit <= 'F')
5416f9cba8fSJoseph Mingrone digit = digit - 'A' + 10;
542*afdbf109SJoseph Mingrone else {
543*afdbf109SJoseph Mingrone /*
544*afdbf109SJoseph Mingrone * Not a valid hex number.
545*afdbf109SJoseph Mingrone * Don't treat this as an error,
546*afdbf109SJoseph Mingrone * in case the caller wants to
547*afdbf109SJoseph Mingrone * interpret it as something else.
548*afdbf109SJoseph Mingrone */
549*afdbf109SJoseph Mingrone return STOULEN_NOT_HEX_NUMBER;
550*afdbf109SJoseph Mingrone }
5518cf6c252SPaul Traina
5526f9cba8fSJoseph Mingrone /*
5536f9cba8fSJoseph Mingrone * Check for overflow.
5546f9cba8fSJoseph Mingrone */
5556f9cba8fSJoseph Mingrone if (n > 0xFFFFFFFU) {
5566f9cba8fSJoseph Mingrone /*
5576f9cba8fSJoseph Mingrone * We have more than 28 bits of
5586f9cba8fSJoseph Mingrone * number, and are about to
5596f9cba8fSJoseph Mingrone * add 4 more; that won't fit
5606f9cba8fSJoseph Mingrone * in 32 bits.
5616f9cba8fSJoseph Mingrone */
562*afdbf109SJoseph Mingrone bpf_set_error(cstate,
563*afdbf109SJoseph Mingrone "number %.*s overflows 32 bits",
564*afdbf109SJoseph Mingrone (int)string_len, string);
565*afdbf109SJoseph Mingrone return STOULEN_ERROR;
5666f9cba8fSJoseph Mingrone }
5676f9cba8fSJoseph Mingrone n = (n << 4) + digit;
5686f9cba8fSJoseph Mingrone }
5696f9cba8fSJoseph Mingrone } else {
5706f9cba8fSJoseph Mingrone /*
5716f9cba8fSJoseph Mingrone * Begins with 0, but not 0x or 0X, so octal.
5726f9cba8fSJoseph Mingrone * Guaranteed to be all *decimal* digits following
5736f9cba8fSJoseph Mingrone * the prefix, so we need to catch 8 and 9 and
5746f9cba8fSJoseph Mingrone * report an error.
5756f9cba8fSJoseph Mingrone */
5766f9cba8fSJoseph Mingrone s += 1;
577*afdbf109SJoseph Mingrone string_len -= 1;
578*afdbf109SJoseph Mingrone while (string_len != 0) {
579*afdbf109SJoseph Mingrone digit = *s++;
580*afdbf109SJoseph Mingrone string_len--;
5816f9cba8fSJoseph Mingrone if (digit >= '0' && digit <= '7')
5826f9cba8fSJoseph Mingrone digit = digit - '0';
5836f9cba8fSJoseph Mingrone else {
584*afdbf109SJoseph Mingrone /*
585*afdbf109SJoseph Mingrone * Not a valid octal number.
586*afdbf109SJoseph Mingrone * Don't treat this as an error,
587*afdbf109SJoseph Mingrone * in case the caller wants to
588*afdbf109SJoseph Mingrone * interpret it as something else.
589*afdbf109SJoseph Mingrone */
590*afdbf109SJoseph Mingrone return STOULEN_NOT_OCTAL_NUMBER;
5916f9cba8fSJoseph Mingrone }
5926f9cba8fSJoseph Mingrone if (n > 03777777777U) {
5936f9cba8fSJoseph Mingrone /*
5946f9cba8fSJoseph Mingrone * We have more than 29 bits of
5956f9cba8fSJoseph Mingrone * number, and are about to add
5966f9cba8fSJoseph Mingrone * 3 more; that won't fit in
5976f9cba8fSJoseph Mingrone * 32 bits.
5986f9cba8fSJoseph Mingrone */
599*afdbf109SJoseph Mingrone bpf_set_error(cstate,
600*afdbf109SJoseph Mingrone "number %.*s overflows 32 bits",
601*afdbf109SJoseph Mingrone (int)string_len, string);
602*afdbf109SJoseph Mingrone return STOULEN_ERROR;
6036f9cba8fSJoseph Mingrone }
6046f9cba8fSJoseph Mingrone n = (n << 3) + digit;
6056f9cba8fSJoseph Mingrone }
6066f9cba8fSJoseph Mingrone }
6076f9cba8fSJoseph Mingrone } else {
6086f9cba8fSJoseph Mingrone /*
6096f9cba8fSJoseph Mingrone * Decimal.
6106f9cba8fSJoseph Mingrone */
611*afdbf109SJoseph Mingrone while (string_len != 0) {
612*afdbf109SJoseph Mingrone digit = *s++;
613*afdbf109SJoseph Mingrone string_len--;
614*afdbf109SJoseph Mingrone if (digit >= '0' && digit <= '9')
6156f9cba8fSJoseph Mingrone digit = digit - '0';
616*afdbf109SJoseph Mingrone else {
617*afdbf109SJoseph Mingrone /*
618*afdbf109SJoseph Mingrone * Not a valid decimal number.
619*afdbf109SJoseph Mingrone * Don't treat this as an error,
620*afdbf109SJoseph Mingrone * in case the caller wants to
621*afdbf109SJoseph Mingrone * interpret it as something else.
622*afdbf109SJoseph Mingrone */
623*afdbf109SJoseph Mingrone return STOULEN_NOT_DECIMAL_NUMBER;
624*afdbf109SJoseph Mingrone }
6256f9cba8fSJoseph Mingrone #define CUTOFF_DEC (0xFFFFFFFFU / 10U)
6266f9cba8fSJoseph Mingrone #define CUTLIM_DEC (0xFFFFFFFFU % 10U)
6276f9cba8fSJoseph Mingrone if (n > CUTOFF_DEC ||
6286f9cba8fSJoseph Mingrone (n == CUTOFF_DEC && digit > CUTLIM_DEC)) {
629*afdbf109SJoseph Mingrone /*
630*afdbf109SJoseph Mingrone * Adding that digit will result in a
631*afdbf109SJoseph Mingrone * number that won't fit in 32 bits.
632*afdbf109SJoseph Mingrone */
633*afdbf109SJoseph Mingrone bpf_set_error(cstate,
634*afdbf109SJoseph Mingrone "number %.*s overflows 32 bits",
635*afdbf109SJoseph Mingrone (int)string_len, string);
636*afdbf109SJoseph Mingrone return STOULEN_ERROR;
6376f9cba8fSJoseph Mingrone }
6386f9cba8fSJoseph Mingrone n = (n * 10) + digit;
6396f9cba8fSJoseph Mingrone }
6406f9cba8fSJoseph Mingrone }
6416f9cba8fSJoseph Mingrone
642*afdbf109SJoseph Mingrone *val = n;
643*afdbf109SJoseph Mingrone return STOULEN_OK;
644*afdbf109SJoseph Mingrone }
645*afdbf109SJoseph Mingrone
646*afdbf109SJoseph Mingrone /*
647*afdbf109SJoseph Mingrone * Convert string to 32-bit unsigned integer. Just like atoi(), but checks for
648*afdbf109SJoseph Mingrone * preceding 0x or 0 and uses hex or octal instead of decimal.
649*afdbf109SJoseph Mingrone *
650*afdbf109SJoseph Mingrone * On success, sets yylval->h to the value and returns NUM.
651*afdbf109SJoseph Mingrone * On failure, sets the BPF error string and returns LEX_ERROR, to force
652*afdbf109SJoseph Mingrone * the parse to stop.
653*afdbf109SJoseph Mingrone */
654*afdbf109SJoseph Mingrone static int
stou(const char * yytext_arg,YYSTYPE * yylval_arg,compiler_state_t * yyextra_arg)655*afdbf109SJoseph Mingrone stou(const char *yytext_arg, YYSTYPE *yylval_arg, compiler_state_t *yyextra_arg)
656*afdbf109SJoseph Mingrone {
657*afdbf109SJoseph Mingrone stoulen_ret ret;
658*afdbf109SJoseph Mingrone
659*afdbf109SJoseph Mingrone ret = stoulen(yytext_arg, strlen(yytext_arg), &yylval_arg->h,
660*afdbf109SJoseph Mingrone yyextra_arg);
661*afdbf109SJoseph Mingrone switch (ret) {
662*afdbf109SJoseph Mingrone
663*afdbf109SJoseph Mingrone case STOULEN_OK:
6646f9cba8fSJoseph Mingrone return NUM;
665*afdbf109SJoseph Mingrone
666*afdbf109SJoseph Mingrone case STOULEN_NOT_OCTAL_NUMBER:
667*afdbf109SJoseph Mingrone bpf_set_error(yyextra_arg, "number %s contains non-octal digit",
668*afdbf109SJoseph Mingrone yytext_arg);
669*afdbf109SJoseph Mingrone return LEX_ERROR;
670*afdbf109SJoseph Mingrone
671*afdbf109SJoseph Mingrone case STOULEN_NOT_HEX_NUMBER:
672*afdbf109SJoseph Mingrone bpf_set_error(yyextra_arg, "number %s contains non-hex digit",
673*afdbf109SJoseph Mingrone yytext_arg);
674*afdbf109SJoseph Mingrone return LEX_ERROR;
675*afdbf109SJoseph Mingrone
676*afdbf109SJoseph Mingrone case STOULEN_NOT_DECIMAL_NUMBER:
677*afdbf109SJoseph Mingrone bpf_set_error(yyextra_arg, "number %s contains non-decimal digit",
678*afdbf109SJoseph Mingrone yytext_arg);
679*afdbf109SJoseph Mingrone return LEX_ERROR;
680*afdbf109SJoseph Mingrone
681*afdbf109SJoseph Mingrone case STOULEN_ERROR:
682*afdbf109SJoseph Mingrone /* Error already set. */
683*afdbf109SJoseph Mingrone return LEX_ERROR;
684*afdbf109SJoseph Mingrone
685*afdbf109SJoseph Mingrone default:
686*afdbf109SJoseph Mingrone /* Should not happen */
687*afdbf109SJoseph Mingrone bpf_set_error(yyextra_arg, "stoulen returned %d - this should not happen", ret);
688*afdbf109SJoseph Mingrone return LEX_ERROR;
689*afdbf109SJoseph Mingrone }
6908cf6c252SPaul Traina }
691