1 /* $FreeBSD$ */ 2 /* $KAME: token.l,v 1.13 2000/06/07 00:29:14 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 6 * All rights reserved. 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 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 %{ 34 #include <sys/types.h> 35 #include <sys/param.h> 36 #include <sys/socket.h> 37 #include <net/route.h> 38 #include <net/pfkeyv2.h> 39 #include <netkey/keydb.h> 40 #include <netkey/key_debug.h> 41 #include <netinet/in.h> 42 #include <netinet6/ipsec.h> 43 44 #include <stdlib.h> 45 #include <limits.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <errno.h> 49 #include "vchar.h" 50 #include "y.tab.h" 51 52 #define DECHO \ 53 if (f_debug) {printf("<%d>", yy_start); ECHO ; printf("\n"); } 54 55 #define CMDARG \ 56 { \ 57 char *__buf__ = strdup(yytext), *__p__; \ 58 for (__p__ = __buf__; *__p__ != NULL; __p__++) \ 59 if (*__p__ == '\n' || *__p__ == '\t') \ 60 *__p__ = ' '; \ 61 strcat(cmdarg, __buf__); \ 62 free(__buf__); \ 63 } 64 65 #define PREPROC DECHO CMDARG 66 67 int lineno = 1; 68 char cmdarg[8192]; /* XXX: BUFSIZ is the better ? */ 69 70 extern u_char m_buf[BUFSIZ]; 71 extern u_int m_len; 72 extern int f_debug; 73 74 int yylex __P((void)); 75 void yyfatal __P((const char *s)); 76 void yyerror __P((const char *s)); 77 extern void parse_init __P((void)); 78 int parse __P((FILE **)); 79 int yyparse __P((void)); 80 81 %} 82 83 /* common section */ 84 nl \n 85 ws [ \t]+ 86 digit [0-9] 87 letter [0-9A-Za-z] 88 hexdigit [0-9A-Fa-f] 89 /*octet (([01]?{digit}?{digit})|((2([0-4]{digit}))|(25[0-5])))*/ 90 special [()+\|\?\*,] 91 dot \. 92 comma \, 93 hyphen \- 94 colon \: 95 slash \/ 96 bcl \{ 97 ecl \} 98 blcl \[ 99 elcl \] 100 percent \% 101 semi \; 102 usec {dot}{digit}{1,6} 103 comment \#.* 104 ccomment "/*" 105 bracketstring \<[^>]*\> 106 quotedstring \"[^"]*\" 107 decstring {digit}+ 108 hexpair {hexdigit}{hexdigit} 109 hexstring 0[xX]{hexdigit}+ 110 octetstring {octet}({dot}{octet})+ 111 ipaddress [a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*) 112 ipaddrmask {slash}{digit}{1,3} 113 ipaddrport {blcl}{decstring}{elcl} 114 keyword {letter}{letter}+ 115 name {letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))* 116 hostname {name}(({dot}{name})+{dot}?)? 117 118 %s S_PL 119 120 %% 121 122 add { PREPROC; return(ADD); } 123 delete { PREPROC; return(DELETE); } 124 get { PREPROC; return(GET); } 125 flush { PREPROC; return(FLUSH); } 126 dump { PREPROC; return(DUMP); } 127 128 /* for management SPD */ 129 spdadd { PREPROC; return(SPDADD); } 130 spddelete { PREPROC; return(SPDDELETE); } 131 spddump { PREPROC; return(SPDDUMP); } 132 spdflush { PREPROC; return(SPDFLUSH); } 133 {hyphen}P { BEGIN S_PL; PREPROC; return(F_POLICY); } 134 <S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.\-_/ \n\t]* { 135 yymore(); 136 137 /* count up for nl */ 138 { 139 char *p; 140 for (p = yytext; *p != NULL; p++) 141 if (*p == '\n') 142 lineno++; 143 } 144 145 yylval.val.len = strlen(yytext); 146 yylval.val.buf = strdup(yytext); 147 148 return(PL_REQUESTS); 149 } 150 <S_PL>{semi} { PREPROC; BEGIN INITIAL; return(EOT); } 151 152 /* security protocols */ 153 ah { PREPROC; yylval.num = 0; return(PR_AH); } 154 esp { PREPROC; yylval.num = 0; return(PR_ESP); } 155 ah-old { PREPROC; yylval.num = 1; return(PR_AH); } 156 esp-old { PREPROC; yylval.num = 1; return(PR_ESP); } 157 ipcomp { PREPROC; yylval.num = 0; return(PR_IPCOMP); } 158 159 /* authentication alogorithm */ 160 {hyphen}A { PREPROC; return(F_AUTH); } 161 hmac-md5 { PREPROC; yylval.num = SADB_AALG_MD5HMAC; return(ALG_AUTH); } 162 hmac-sha1 { PREPROC; yylval.num = SADB_AALG_SHA1HMAC; return(ALG_AUTH); } 163 keyed-md5 { PREPROC; yylval.num = SADB_AALG_MD5; return(ALG_AUTH); } 164 keyed-sha1 { PREPROC; yylval.num = SADB_AALG_SHA; return(ALG_AUTH); } 165 null { PREPROC; yylval.num = SADB_AALG_NULL; return(ALG_AUTH); } 166 167 /* encryption alogorithm */ 168 {hyphen}E { PREPROC; return(F_ENC); } 169 des-cbc { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC); } 170 3des-cbc { PREPROC; yylval.num = SADB_EALG_3DESCBC; return(ALG_ENC); } 171 simple { PREPROC; yylval.num = SADB_EALG_NULL; return(ALG_ENC); } 172 blowfish-cbc { PREPROC; yylval.num = SADB_EALG_BLOWFISHCBC; return(ALG_ENC); } 173 cast128-cbc { PREPROC; yylval.num = SADB_EALG_CAST128CBC; return(ALG_ENC); } 174 rc5-cbc { PREPROC; yylval.num = SADB_EALG_RC5CBC; return(ALG_ENC); } 175 des-deriv { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC_DESDERIV); } 176 des-32iv { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC_DES32IV); } 177 178 /* compression algorithms */ 179 {hyphen}C { PREPROC; return(F_COMP); } 180 oui { PREPROC; yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); } 181 deflate { PREPROC; yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); } 182 lzs { PREPROC; yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); } 183 {hyphen}R { PREPROC; return(F_RAWCPI); } 184 185 /* extension */ 186 {hyphen}m { PREPROC; return(F_MODE); } 187 transport { PREPROC; yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); } 188 tunnel { PREPROC; yylval.num = IPSEC_MODE_TUNNEL; return(MODE); } 189 {hyphen}u { PREPROC; return(F_REQID); } 190 {hyphen}f { PREPROC; return(F_EXT); } 191 random-pad { PREPROC; yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); } 192 seq-pad { PREPROC; yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); } 193 zero-pad { PREPROC; yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); } 194 nocyclic-seq { PREPROC; return(NOCYCLICSEQ); } 195 {hyphen}r { PREPROC; return(F_REPLAY); } 196 {hyphen}lh { PREPROC; return(F_LIFETIME_HARD); } 197 {hyphen}ls { PREPROC; return(F_LIFETIME_SOFT); } 198 199 200 /* upper layer protocols */ 201 icmp { PREPROC; yylval.num = IPPROTO_ICMP; return(UP_PROTO); } 202 icmp6 { PREPROC; yylval.num = IPPROTO_ICMPV6; return(UP_PROTO); } 203 tcp { PREPROC; yylval.num = IPPROTO_TCP; return(UP_PROTO); } 204 udp { PREPROC; yylval.num = IPPROTO_UDP; return(UP_PROTO); } 205 206 /* ... */ 207 any { PREPROC; return(ANY); } 208 {ws} { PREPROC; } 209 {nl} { lineno++; } 210 {comment} 211 {semi} { PREPROC; return(EOT); } 212 213 /* parameter */ 214 {decstring} { 215 char *bp; 216 217 PREPROC; 218 yylval.num = strtoul(yytext, &bp, 10); 219 return(DECSTRING); 220 } 221 222 {ipaddress} { 223 PREPROC; 224 225 yylval.val.len = yyleng; 226 yylval.val.buf = strdup(yytext); 227 228 return(ADDRESS); 229 } 230 231 {ipaddrmask} { 232 PREPROC; 233 yytext++; 234 yylval.num = atoi(yytext); 235 return(PREFIX); 236 } 237 238 {ipaddrport} { 239 char *p = yytext; 240 PREPROC; 241 while (*++p != ']') ; 242 *p = NULL; 243 yytext++; 244 yylval.num = atoi(yytext); 245 return(PORT); 246 } 247 248 {blcl}any{elcl} { 249 PREPROC; 250 return(PORTANY); 251 } 252 253 {hexstring} { 254 int len = yyleng - 2; /* (str - "0x") */ 255 PREPROC; 256 yylval.val.len = (len & 1) + (len / 2); 257 /* fixed string if length is odd. */ 258 if (len & 1) { 259 yytext[1] = '0'; 260 yylval.val.buf = strdup(yytext + 1); 261 } else 262 yylval.val.buf = strdup(yytext + 2); 263 264 return(HEXSTRING); 265 } 266 267 {quotedstring} { 268 char *p = yytext; 269 PREPROC; 270 while (*++p != '"') ; 271 *p = NULL; 272 yytext++; 273 yylval.val.len = yyleng - 2; 274 yylval.val.buf = strdup(yytext); 275 276 return(QUOTEDSTRING); 277 } 278 279 . { 280 yyfatal("Syntax error"); 281 /*NOTREACHED*/ 282 } 283 284 %% 285 286 void 287 yyfatal(s) 288 const char *s; 289 { 290 yyerror(s); 291 exit(1); 292 } 293 294 void 295 yyerror(s) 296 const char *s; 297 { 298 printf("line %d: %s at [%s]\n", lineno, s, yytext); 299 } 300 301 int 302 parse(fp) 303 FILE **fp; 304 { 305 yyin = *fp; 306 307 parse_init(); 308 309 if (yyparse()) { 310 printf("parse failed, line %d.\n", lineno); 311 return(-1); 312 } 313 314 return(0); 315 } 316