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