1 /* $NetBSD: token.l,v 1.13 2009/10/28 21:22:49 christos Exp $ */
3 /* $KAME: token.l,v 1.44 2003/10/21 07:20:58 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <net/pfkeyv2.h>
44 #include <netinet/in.h>
55 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || \
56 (defined(__APPLE__) && defined(__MACH__))
64 /* make the code compile on *BSD-current */
65 #ifndef SADB_X_AALG_SHA2_256
66 #define SADB_X_AALG_SHA2_256 (-1)
68 #ifndef SADB_X_AALG_SHA2_384
69 #define SADB_X_AALG_SHA2_384 (-1)
71 #ifndef SADB_X_AALG_SHA2_512
72 #define SADB_X_AALG_SHA2_512 (-1)
74 #ifndef SADB_X_AALG_RIPEMD160HMAC
75 #define SADB_X_AALG_RIPEMD160HMAC (-1)
77 #ifndef SADB_X_AALG_AES_XCBC_MAC
78 #define SADB_X_AALG_AES_XCBC_MAC (-1)
80 #ifndef SADB_X_EALG_TWOFISHCBC
81 #define SADB_X_EALG_TWOFISHCBC (-1)
83 #ifndef SADB_X_EALG_AESCTR
84 #define SADB_X_EALG_AESCTR (-1)
86 #if defined(SADB_X_EALG_AES) && ! defined(SADB_X_EALG_AESCBC)
87 #define SADB_X_EALG_AESCBC SADB_X_EALG_AES
104 quotedstring \"[^"]*\"
106 hexstring 0[xX]{hexdigit}+
107 ipaddress [a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*)
108 ipaddrmask {slash}{digit}{1,3}
109 name {letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))*
110 hostname {name}(({dot}{name})+{dot}?)?
112 %s S_PL S_AUTHALG S_ENCALG
114 %option noinput nounput
119 delete { return(DELETE); }
120 deleteall { return(DELETEALL); }
122 flush { return(FLUSH); }
123 dump { return(DUMP); }
124 exit { return(EXIT); }
125 quit { return(EXIT); }
126 bye { return(EXIT); }
128 /* for management SPD */
129 spdadd { return(SPDADD); }
130 spddelete { return(SPDDELETE); }
131 spddump { return(SPDDUMP); }
132 spdflush { return(SPDFLUSH); }
133 tagged { return(TAGGED); }
134 {hyphen}P { BEGIN S_PL; return(F_POLICY); }
135 <S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.%\-+_/ \n\t\]\[]* {
138 /* count up for nl */
141 for (p = yytext; *p != '\0'; p++)
146 yylval.val.len = strlen(yytext);
147 yylval.val.buf = strdup(yytext);
149 yyfatal("insufficient memory");
153 <S_PL>{semi} { BEGIN INITIAL; return(EOT); }
155 /* address resolution flags */
156 {hyphen}[n46][n46]* {
157 yylval.val.len = strlen(yytext);
158 yylval.val.buf = strdup(yytext);
160 yyfatal("insufficient memory");
164 /* security protocols */
165 ah { yylval.num = 0; return(PR_AH); }
166 esp { yylval.num = 0; return(PR_ESP); }
167 ah-old { yylval.num = 1; return(PR_AH); }
168 esp-old { yylval.num = 1; return(PR_ESP); }
169 esp-udp { yylval.num = 0; return(PR_ESPUDP); }
170 ipcomp { yylval.num = 0; return(PR_IPCOMP); }
172 yylval.num = 0; return(PR_TCP);
175 /* authentication alogorithm */
176 {hyphen}A { BEGIN S_AUTHALG; return(F_AUTH); }
177 <S_AUTHALG>hmac-md5 { yylval.num = SADB_AALG_MD5HMAC; BEGIN INITIAL; return(ALG_AUTH); }
178 <S_AUTHALG>hmac-sha1 { yylval.num = SADB_AALG_SHA1HMAC; BEGIN INITIAL; return(ALG_AUTH); }
179 <S_AUTHALG>keyed-md5 { yylval.num = SADB_X_AALG_MD5; BEGIN INITIAL; return(ALG_AUTH); }
180 <S_AUTHALG>keyed-sha1 { yylval.num = SADB_X_AALG_SHA; BEGIN INITIAL; return(ALG_AUTH); }
181 <S_AUTHALG>hmac-sha2-256 { yylval.num = SADB_X_AALG_SHA2_256; BEGIN INITIAL; return(ALG_AUTH); }
182 <S_AUTHALG>hmac-sha256 { yylval.num = SADB_X_AALG_SHA2_256; BEGIN INITIAL; return(ALG_AUTH); }
183 <S_AUTHALG>hmac-sha2-384 { yylval.num = SADB_X_AALG_SHA2_384; BEGIN INITIAL; return(ALG_AUTH); }
184 <S_AUTHALG>hmac-sha384 { yylval.num = SADB_X_AALG_SHA2_384; BEGIN INITIAL; return(ALG_AUTH); }
185 <S_AUTHALG>hmac-sha2-512 { yylval.num = SADB_X_AALG_SHA2_512; BEGIN INITIAL; return(ALG_AUTH); }
186 <S_AUTHALG>hmac-sha512 { yylval.num = SADB_X_AALG_SHA2_512; BEGIN INITIAL; return(ALG_AUTH); }
187 <S_AUTHALG>hmac-ripemd160 { yylval.num = SADB_X_AALG_RIPEMD160HMAC; BEGIN INITIAL; return(ALG_AUTH); }
188 <S_AUTHALG>aes-xcbc-mac { yylval.num = SADB_X_AALG_AES_XCBC_MAC; BEGIN INITIAL; return(ALG_AUTH); }
190 #ifdef SADB_X_AALG_TCP_MD5
191 yylval.num = SADB_X_AALG_TCP_MD5;
196 <S_AUTHALG>null { yylval.num = SADB_X_AALG_NULL; BEGIN INITIAL; return(ALG_AUTH_NOKEY); }
198 /* encryption alogorithm */
199 {hyphen}E { BEGIN S_ENCALG; return(F_ENC); }
200 <S_ENCALG>des-cbc { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC); }
201 <S_ENCALG>3des-cbc { yylval.num = SADB_EALG_3DESCBC; BEGIN INITIAL; return(ALG_ENC); }
202 <S_ENCALG>null { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_NOKEY); }
203 <S_ENCALG>simple { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_OLD); }
204 <S_ENCALG>blowfish-cbc { yylval.num = SADB_X_EALG_BLOWFISHCBC; BEGIN INITIAL; return(ALG_ENC); }
205 <S_ENCALG>cast128-cbc { yylval.num = SADB_X_EALG_CAST128CBC; BEGIN INITIAL; return(ALG_ENC); }
206 <S_ENCALG>des-deriv { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DESDERIV); }
207 <S_ENCALG>des-32iv { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DES32IV); }
208 <S_ENCALG>twofish-cbc { yylval.num = SADB_X_EALG_TWOFISHCBC; BEGIN INITIAL; return(ALG_ENC); }
210 #ifdef SADB_X_EALG_AESCBC
211 yylval.num = SADB_X_EALG_AESCBC; BEGIN INITIAL; return(ALG_ENC);
214 <S_ENCALG>rijndael-cbc {
215 #ifdef SADB_X_EALG_AESCBC
216 yylval.num = SADB_X_EALG_AESCBC; BEGIN INITIAL; return(ALG_ENC);
219 <S_ENCALG>aes-ctr { yylval.num = SADB_X_EALG_AESCTR; BEGIN INITIAL; return(ALG_ENC); }
220 <S_ENCALG>camellia-cbc {
221 #ifdef SADB_X_EALG_CAMELLIACBC
222 yylval.num = SADB_X_EALG_CAMELLIACBC; BEGIN INITIAL; return(ALG_ENC);
226 /* compression algorithms */
227 {hyphen}C { return(F_COMP); }
228 oui { yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); }
229 deflate { yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); }
230 lzs { yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); }
231 {hyphen}R { return(F_RAWCPI); }
234 {hyphen}m { return(F_MODE); }
235 transport { yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); }
236 tunnel { yylval.num = IPSEC_MODE_TUNNEL; return(MODE); }
237 {hyphen}u { return(F_REQID); }
238 {hyphen}f { return(F_EXT); }
239 random-pad { yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); }
240 seq-pad { yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); }
241 zero-pad { yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); }
242 nocyclic-seq { return(NOCYCLICSEQ); }
243 {hyphen}r { return(F_REPLAY); }
244 {hyphen}lh { return(F_LIFETIME_HARD); }
245 {hyphen}ls { return(F_LIFETIME_SOFT); }
246 {hyphen}bh { return(F_LIFEBYTE_HARD); }
247 {hyphen}bs { return(F_LIFEBYTE_SOFT); }
248 {hyphen}ctx { return(SECURITY_CTX); }
255 {semi} { return(EOT); }
257 /* for address parameters: /prefix, [port] */
258 {slash} { return SLASH; }
259 {blcl} { return BLCL; }
260 {elcl} { return ELCL; }
266 yylval.ulnum = strtoul(yytext, &bp, 10);
271 yylval.val.buf = strdup(yytext + 2);
273 yyfatal("insufficient memory");
274 yylval.val.len = strlen(yylval.val.buf);
281 while (*++p != '"') ;
284 yylval.val.len = yyleng - 2;
285 yylval.val.buf = strdup(yytext);
287 yyfatal("insufficient memory");
289 return(QUOTEDSTRING);
292 [A-Za-z0-9:][A-Za-z0-9:%\.-]* {
293 yylval.val.len = yyleng;
294 yylval.val.buf = strdup(yytext);
296 yyfatal("insufficient memory");
301 yylval.val.len = yyleng;
302 yylval.val.buf = strdup(yytext);
304 yyfatal("insufficient memory");
309 yyfatal("Syntax error");
327 printf("line %d: %s at [%s]\n", lineno, s, yytext);
340 printf("parse failed, line %d.\n", lineno);
348 parse_string (char *src)
351 YY_BUFFER_STATE buf_state;
353 buf_state = yy_scan_string(src);
355 yy_delete_buffer(buf_state);