and added files
[bcm963xx.git] / userapps / opensource / ipsec-tools / src / setkey / token.l
1 /*      $KAME: token.l,v 1.44 2003/10/21 07:20:58 itojun Exp $  */
2
3 /*
4  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 %{
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <net/pfkeyv2.h>
42 #include <netinet/in.h>
43 #ifdef HAVE_NETINET6_IPSEC
44 #  include <netinet6/ipsec.h>
45 #else 
46 #  include <netinet/ipsec.h>
47 #endif
48
49 #include <stdlib.h>
50 #include <limits.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <errno.h>
54 #include <netdb.h>
55
56 #include "vchar.h"
57 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__)
58 #include "parse.h"
59 #else
60 #include "y.tab.h"
61 #endif
62
63 int lineno = 1;
64
65 extern u_char m_buf[BUFSIZ];
66 extern u_int m_len;
67 extern int f_debug;
68
69 int yylex __P((void));
70 void yyfatal __P((const char *s));
71 void yyerror __P((const char *s));
72 extern void parse_init __P((void));
73 int parse __P((FILE **));
74 int yyparse __P((void));
75
76 /* make the code compile on *BSD-current */
77 #ifndef SADB_X_AALG_SHA2_256
78 #define SADB_X_AALG_SHA2_256    (-1)
79 #endif
80 #ifndef SADB_X_AALG_SHA2_384
81 #define SADB_X_AALG_SHA2_384    (-1)
82 #endif
83 #ifndef SADB_X_AALG_SHA2_512
84 #define SADB_X_AALG_SHA2_512    (-1)
85 #endif
86 #ifndef SADB_X_AALG_RIPEMD160HMAC
87 #define SADB_X_AALG_RIPEMD160HMAC       (-1)
88 #endif
89 #ifndef SADB_X_AALG_AES_XCBC_MAC
90 #define SADB_X_AALG_AES_XCBC_MAC        (-1)
91 #endif
92 #ifndef SADB_X_EALG_TWOFISHCBC
93 #define SADB_X_EALG_TWOFISHCBC  (-1)
94 #endif
95 #ifndef SADB_X_EALG_AESCTR
96 #define SADB_X_EALG_AESCTR      (-1)
97 #endif
98 %}
99
100 /* common section */
101 nl              \n
102 ws              [ \t]+
103 digit           [0-9]
104 letter          [0-9A-Za-z]
105 hexdigit        [0-9A-Fa-f]
106 dot             \.
107 hyphen          \-
108 slash           \/
109 blcl            \[
110 elcl            \]
111 semi            \;
112 comment         \#.*
113 quotedstring    \"[^"]*\"
114 decstring       {digit}+
115 hexstring       0[xX]{hexdigit}+
116 ipaddress       [a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*)
117 ipaddrmask      {slash}{digit}{1,3}
118 name            {letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))*
119 hostname        {name}(({dot}{name})+{dot}?)?
120
121 %s S_PL S_AUTHALG S_ENCALG
122
123 %%
124
125 add             { return(ADD); }
126 delete          { return(DELETE); }
127 deleteall       { return(DELETEALL); }
128 get             { return(GET); }
129 flush           { return(FLUSH); }
130 dump            { return(DUMP); }
131 exit            { return(EXIT); }
132 quit            { return(EXIT); }
133 bye             { return(EXIT); }
134
135         /* for management SPD */
136 spdadd          { return(SPDADD); }
137 spddelete       { return(SPDDELETE); }
138 spddump         { return(SPDDUMP); }
139 spdflush        { return(SPDFLUSH); }
140 tagged          { return(TAGGED); }
141 {hyphen}P       { BEGIN S_PL; return(F_POLICY); }
142 <S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.%\-+_/ \n\t]* {
143                         yymore();
144
145                         /* count up for nl */
146                             {
147                                 char *p;
148                                 for (p = yytext; *p != '\0'; p++)
149                                         if (*p == '\n')
150                                                 lineno++;
151                             }
152
153                         yylval.val.len = strlen(yytext);
154                         yylval.val.buf = strdup(yytext);
155                         if (!yylval.val.buf)
156                                 yyfatal("insufficient memory");
157
158                         return(PL_REQUESTS);
159                 }
160 <S_PL>{semi}    { BEGIN INITIAL; return(EOT); }
161
162         /* address resolution flags */
163 {hyphen}[n46][n46]*     {
164                         yylval.val.len = strlen(yytext);
165                         yylval.val.buf = strdup(yytext);
166                         if (!yylval.val.buf)
167                                 yyfatal("insufficient memory");
168                         return(F_AIFLAGS);
169                 }
170
171         /* security protocols */
172 ah              { yylval.num = 0; return(PR_AH); }
173 esp             { yylval.num = 0; return(PR_ESP); }
174 ah-old          { yylval.num = 1; return(PR_AH); }
175 esp-old         { yylval.num = 1; return(PR_ESP); }
176 esp-udp         { yylval.num = 0; return(PR_ESPUDP); }
177 ipcomp          { yylval.num = 0; return(PR_IPCOMP); }
178
179         /* authentication alogorithm */
180 {hyphen}A       { BEGIN S_AUTHALG; return(F_AUTH); }
181 <S_AUTHALG>hmac-md5     { yylval.num = SADB_AALG_MD5HMAC; BEGIN INITIAL; return(ALG_AUTH); }
182 <S_AUTHALG>hmac-sha1    { yylval.num = SADB_AALG_SHA1HMAC; BEGIN INITIAL; return(ALG_AUTH); }
183 <S_AUTHALG>keyed-md5    { yylval.num = SADB_X_AALG_MD5; BEGIN INITIAL; return(ALG_AUTH); }
184 <S_AUTHALG>keyed-sha1   { yylval.num = SADB_X_AALG_SHA; BEGIN INITIAL; return(ALG_AUTH); }
185 <S_AUTHALG>hmac-sha2-256 { yylval.num = SADB_X_AALG_SHA2_256; BEGIN INITIAL; return(ALG_AUTH); }
186 <S_AUTHALG>hmac-sha2-384 { yylval.num = SADB_X_AALG_SHA2_384; BEGIN INITIAL; return(ALG_AUTH); }
187 <S_AUTHALG>hmac-sha2-512 { yylval.num = SADB_X_AALG_SHA2_512; BEGIN INITIAL; return(ALG_AUTH); }
188 <S_AUTHALG>hmac-ripemd160 { yylval.num = SADB_X_AALG_RIPEMD160HMAC; BEGIN INITIAL; return(ALG_AUTH); }
189 <S_AUTHALG>aes-xcbc-mac { yylval.num = SADB_X_AALG_AES_XCBC_MAC; BEGIN INITIAL; return(ALG_AUTH); }
190 <S_AUTHALG>null { yylval.num = SADB_X_AALG_NULL; BEGIN INITIAL; return(ALG_AUTH_NOKEY); }
191
192         /* encryption alogorithm */
193 {hyphen}E       { BEGIN S_ENCALG; return(F_ENC); }
194 <S_ENCALG>des-cbc       { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC); }
195 <S_ENCALG>3des-cbc      { yylval.num = SADB_EALG_3DESCBC; BEGIN INITIAL; return(ALG_ENC); }
196 <S_ENCALG>null          { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_NOKEY); }
197 <S_ENCALG>simple        { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_OLD); }
198 <S_ENCALG>blowfish-cbc  { yylval.num = SADB_X_EALG_BLOWFISHCBC; BEGIN INITIAL; return(ALG_ENC); }
199 <S_ENCALG>cast128-cbc   { yylval.num = SADB_X_EALG_CAST128CBC; BEGIN INITIAL; return(ALG_ENC); }
200 <S_ENCALG>des-deriv     { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DESDERIV); }
201 <S_ENCALG>des-32iv      { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DES32IV); }
202 <S_ENCALG>twofish-cbc   { yylval.num = SADB_X_EALG_TWOFISHCBC; BEGIN INITIAL; return(ALG_ENC); }
203 <S_ENCALG>aes-cbc       { 
204 #ifdef SADB_X_EALG_AESCBC
205         yylval.num = SADB_X_EALG_AESCBC; BEGIN INITIAL; return(ALG_ENC); 
206 #endif
207 }
208 <S_ENCALG>rijndael-cbc  { 
209 #ifdef SADB_X_EALG_AESCBC
210         yylval.num = SADB_X_EALG_AESCBC; BEGIN INITIAL; return(ALG_ENC); 
211 #endif
212 }
213 <S_ENCALG>aes-ctr       { yylval.num = SADB_X_EALG_AESCTR; BEGIN INITIAL; return(ALG_ENC); }
214
215         /* compression algorithms */
216 {hyphen}C       { return(F_COMP); }
217 oui             { yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); }
218 deflate         { yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); }
219 lzs             { yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); }
220 {hyphen}R       { return(F_RAWCPI); }
221
222         /* extension */
223 {hyphen}m       { return(F_MODE); }
224 transport       { yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); }
225 tunnel          { yylval.num = IPSEC_MODE_TUNNEL; return(MODE); }
226 {hyphen}u       { return(F_REQID); }
227 {hyphen}f       { return(F_EXT); }
228 random-pad      { yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); }
229 seq-pad         { yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); }
230 zero-pad        { yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); }
231 nocyclic-seq    { return(NOCYCLICSEQ); }
232 {hyphen}r       { return(F_REPLAY); }
233 {hyphen}lh      { return(F_LIFETIME_HARD); }
234 {hyphen}ls      { return(F_LIFETIME_SOFT); }
235 {hyphen}bh      { return(F_LIFEBYTE_HARD); }
236 {hyphen}bs      { return(F_LIFEBYTE_SOFT); }
237
238         /* ... */
239 any             { return(ANY); }
240 {ws}            { }
241 {nl}            { lineno++; }
242 {comment}
243 {semi}          { return(EOT); }
244
245         /* for address parameters: /prefix, [port] */
246 {slash}         { return SLASH; }
247 {blcl}          { return BLCL; }
248 {elcl}          { return ELCL; }
249
250         /* parameter */
251 {decstring}     {
252                         char *bp;
253
254                         yylval.ulnum = strtoul(yytext, &bp, 10);
255                         return(DECSTRING);
256                 }
257
258 {hexstring}     {
259                         yylval.val.buf = strdup(yytext + 2);
260                         if (!yylval.val.buf)
261                                 yyfatal("insufficient memory");
262                         yylval.val.len = strlen(yylval.val.buf);
263
264                         return(HEXSTRING);
265                 }
266
267 {quotedstring}  {
268                         char *p = yytext;
269                         while (*++p != '"') ;
270                         *p = '\0';
271                         yytext++;
272                         yylval.val.len = yyleng - 2;
273                         yylval.val.buf = strdup(yytext);
274                         if (!yylval.val.buf)
275                                 yyfatal("insufficient memory");
276
277                         return(QUOTEDSTRING);
278                 }
279
280 [A-Za-z0-9:][A-Za-z0-9:%\.-]* {
281                         yylval.val.len = yyleng;
282                         yylval.val.buf = strdup(yytext);
283                         if (!yylval.val.buf)
284                                 yyfatal("insufficient memory");
285                         return(STRING);
286                 }
287
288 [0-9,]+ {
289                         yylval.val.len = yyleng;
290                         yylval.val.buf = strdup(yytext);
291                         if (!yylval.val.buf)
292                                 yyfatal("insufficient memory");
293                         return(STRING);
294                 }
295
296 .               {
297                         yyfatal("Syntax error");
298                         /*NOTREACHED*/
299                 }
300
301 %%
302
303 void
304 yyfatal(s)
305         const char *s;
306 {
307         yyerror(s);
308         exit(1);
309 }
310
311 void
312 yyerror(s)
313         const char *s;
314 {
315         printf("line %d: %s at [%s]\n", lineno, s, yytext);
316 }
317
318 int
319 parse(fp)
320         FILE **fp;
321 {
322         yyin = *fp;
323
324         parse_init();
325
326         if (yyparse()) {
327                 printf("parse failed, line %d.\n", lineno);
328                 return(-1);
329         }
330
331         return(0);
332 }
333
334 int
335 parse_string (char *src)
336 {
337         int             result;
338         YY_BUFFER_STATE buf_state;
339
340         buf_state = yy_scan_string(src);
341         result = yyparse();
342         yy_delete_buffer(buf_state);
343         return result;
344 }
345