npf_scan.l revision 1.29 1 /*-
2 * Copyright (c) 2011-2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Martin Husemann.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 %{
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <err.h>
34
35 #include "npfctl.h"
36 #include "npf_parse.h"
37
38 int yycolumn;
39
40 #define YY_USER_ACTION yycolumn += yyleng;
41
42 extern int yyparsetarget;
43 extern int yylineno;
44 extern const char * yyfilename;
45 extern int yyparse(void);
46 extern void yyrestart(FILE *);
47
48 void
49 npfctl_parse_file(const char *name)
50 {
51 FILE *fp;
52
53 fp = fopen(name, "r");
54 if (fp == NULL) {
55 err(EXIT_FAILURE, "open '%s'", name);
56 }
57 yyparsetarget = NPFCTL_PARSE_FILE;
58 yyrestart(fp);
59 yylineno = 1;
60 yycolumn = 0;
61 yyfilename = name;
62 yyparse();
63 fclose(fp);
64 }
65
66 void
67 npfctl_parse_string(const char *str)
68 {
69 YY_BUFFER_STATE bs;
70
71 yyparsetarget = NPFCTL_PARSE_STRING;
72 bs = yy_scan_string(str);
73 yyfilename = "stdin";
74 yyparse();
75 yy_delete_buffer(bs);
76 }
77
78 %}
79
80 %option noyywrap nounput noinput
81
82 ID [a-zA-Z_][a-zA-Z_0-9]*
83 DID [a-zA-Z_][a-zA-Z_0-9-]*
84 NUMBER [0-9]+
85 HEXDIG [0-9a-fA-F]+
86
87 %%
88 alg return ALG;
89 table return TABLE;
90 type return TYPE;
91 hash return HASH;
92 tree return TREE;
93 lpm return LPM;
94 cdb return CDB;
95 const return CONST;
96 static return TSTATIC;
97 dynamic return TDYNAMIC;
98 file return TFILE;
99 map return MAP;
100 no-ports return NO_PORTS;
101 set return SET;
102 "<->" return ARROWBOTH;
103 "<-" return ARROWLEFT;
104 "->" return ARROWRIGHT;
105 algo return ALGO;
106 netmap return NETMAP;
107 ipset return IPSET;
108 "ip-hash" return IPHASH;
109 "round-robin" return ROUNDROBIN;
110 npt66 return NPT66;
111 "-" return MINUS;
112 procedure return PROCEDURE;
113 \\\n yylineno++; yycolumn = 0;
114 \n yylineno++; yycolumn = 0; return SEPLINE;
115 ; return SEPLINE;
116 name return NAME;
117 group return GROUP;
118 default return DEFAULT;
119 in return IN;
120 out return OUT;
121 forw return FORW;
122 interface return INTERFACE;
123 all return ALL;
124 block return BLOCK;
125 pass return PASS;
126 pcap-filter return PCAP_FILTER;
127 stateful return STATEFUL;
128 stateful-all return STATEFUL_ALL;
129 apply return APPLY;
130 final return FINAL;
131 quick return FINAL;
132 on return ON;
133 off return OFF;
134 inet6 return INET6;
135 inet4 return INET4;
136 ifaddrs return IFADDRS;
137 proto return PROTO;
138 family return FAMILY;
139 tcp return TCP;
140 icmp { yylval.num = IPPROTO_ICMP; return ICMP; }
141 ipv6-icmp { yylval.num = IPPROTO_ICMPV6; return ICMP6; }
142 \"ipv6-icmp\" { yylval.num = IPPROTO_ICMPV6; return ICMP6; }
143 return-rst return RETURNRST;
144 return-icmp return RETURNICMP;
145 return return RETURN;
146 ruleset return RULESET;
147 from return FROM;
148 to return TO;
149 port return PORT;
150 flags return FLAGS;
151 icmp-type return ICMPTYPE;
152 code return CODE;
153 any return ANY;
154
155 "/" return SLASH;
156 "{" return CURLY_OPEN;
157 "}" return CURLY_CLOSE;
158 "(" return PAR_OPEN;
159 ")" return PAR_CLOSE;
160 "," return COMMA;
161 "=" return EQ;
162 "!" return EXCL_MARK;
163
164 "0x"{HEXDIG} {
165 char *endp, *buf = ecalloc(1, yyleng + 1);
166 buf[yyleng] = 0;
167 yylval.num = strtoul(buf+2, &endp, 16);
168 free(buf);
169 return HEX;
170 }
171
172 {NUMBER}"."{NUMBER} {
173 char *endp, *buf = estrndup(yytext, yyleng);
174 yylval.fpnum = strtod(buf, &endp);
175 free(buf);
176 return FPNUM;
177 }
178
179 {HEXDIG}":"[0-9a-fA-F:]* {
180 yylval.str = estrndup(yytext, yyleng);
181 return IPV6ADDR;
182 }
183
184 "::"{HEXDIG}[0-9a-fA-F:.]* {
185 yylval.str = estrndup(yytext, yyleng);
186 return IPV6ADDR;
187 }
188
189 {NUMBER}"."[0-9][0-9.]* {
190 yylval.str = estrndup(yytext, yyleng);
191 return IPV4ADDR;
192 }
193
194 {NUMBER} {
195 char *endp, *buf = estrndup(yytext, yyleng);
196 yylval.num = strtoul(buf, &endp, 10);
197 free(buf);
198 return NUM;
199 }
200
201 "<"{DID}">" {
202 yylval.str = estrndup(yytext + 1, yyleng - 2);
203 return TABLE_ID;
204 }
205
206 "$"{ID} {
207 yylval.str = estrndup(yytext + 1, yyleng - 1);
208 return VAR_ID;
209 }
210
211 [a-z]*"."[a-z.]* {
212 yylval.str = estrndup(yytext, yyleng);
213 return PARAM;
214 }
215
216 {ID} {
217 yylval.str = estrndup(yytext, yyleng);
218 return IDENTIFIER;
219 }
220
221 \"[^\"]*\" {
222 yylval.str = estrndup(yytext + 1, yyleng - 2);
223 return STRING;
224 }
225
226 #.*$ /* drop comment until end of line */
227 [ \t] /* eat whitespace */
228
229 : return COLON;
230