token.l revision 171135
162583Sitojun/* $FreeBSD: head/sbin/setkey/token.l 171135 2007-07-01 12:08:08Z gnn $ */ 2122108Sume/* $KAME: token.l,v 1.43 2003/07/25 09:35:28 itojun Exp $ */ 362583Sitojun 455505Sshin/* 555505Sshin * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 655505Sshin * All rights reserved. 762583Sitojun * 855505Sshin * Redistribution and use in source and binary forms, with or without 955505Sshin * modification, are permitted provided that the following conditions 1055505Sshin * are met: 1155505Sshin * 1. Redistributions of source code must retain the above copyright 1255505Sshin * notice, this list of conditions and the following disclaimer. 1355505Sshin * 2. Redistributions in binary form must reproduce the above copyright 1455505Sshin * notice, this list of conditions and the following disclaimer in the 1555505Sshin * documentation and/or other materials provided with the distribution. 1655505Sshin * 3. Neither the name of the project nor the names of its contributors 1755505Sshin * may be used to endorse or promote products derived from this software 1855505Sshin * without specific prior written permission. 1962583Sitojun * 2055505Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2155505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2255505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2355505Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2455505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2555505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2655505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2755505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2855505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2955505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3055505Sshin * SUCH DAMAGE. 3155505Sshin */ 3255505Sshin 3355505Sshin%{ 3455505Sshin#include <sys/types.h> 3555505Sshin#include <sys/param.h> 3655505Sshin#include <sys/socket.h> 3755505Sshin#include <net/route.h> 3855505Sshin#include <net/pfkeyv2.h> 39171135Sgnn#include <netipsec/keydb.h> 40171135Sgnn#include <netipsec/key_debug.h> 4155505Sshin#include <netinet/in.h> 42171135Sgnn#include <netipsec/ipsec.h> 4355505Sshin 4455505Sshin#include <stdlib.h> 4555505Sshin#include <limits.h> 4655505Sshin#include <string.h> 4755505Sshin#include <unistd.h> 4855505Sshin#include <errno.h> 49122108Sume#include <netdb.h> 50122108Sume 5155505Sshin#include "vchar.h" 5255505Sshin#include "y.tab.h" 5355505Sshin 5462583Sitojunint lineno = 1; 5555505Sshin 5662583Sitojunextern u_char m_buf[BUFSIZ]; 5762583Sitojunextern u_int m_len; 5862583Sitojunextern int f_debug; 5955505Sshin 6062583Sitojunint yylex __P((void)); 6162583Sitojunvoid yyfatal __P((const char *s)); 6262583Sitojunvoid yyerror __P((const char *s)); 6362583Sitojunextern void parse_init __P((void)); 6462583Sitojunint parse __P((FILE **)); 6562583Sitojunint yyparse __P((void)); 6655505Sshin%} 6755505Sshin 6855505Sshin/* common section */ 6955505Sshinnl \n 7055505Sshinws [ \t]+ 7155505Sshindigit [0-9] 7255505Sshinletter [0-9A-Za-z] 7355505Sshinhexdigit [0-9A-Fa-f] 7455505Sshindot \. 7555505Sshinhyphen \- 7655505Sshinslash \/ 7755505Sshinblcl \[ 7855505Sshinelcl \] 7955505Sshinsemi \; 8055505Sshincomment \#.* 8155505Sshinquotedstring \"[^"]*\" 8255505Sshindecstring {digit}+ 8355505Sshinhexstring 0[xX]{hexdigit}+ 8462583Sitojunipaddress [a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*) 8555505Sshinipaddrmask {slash}{digit}{1,3} 8655505Sshinname {letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))* 8755505Sshinhostname {name}(({dot}{name})+{dot}?)? 8855505Sshin 89122108Sume%s S_PL S_AUTHALG S_ENCALG 9055505Sshin 9155505Sshin%% 9255505Sshin 93122108Sumeadd { return(ADD); } 94122108Sumedelete { return(DELETE); } 95122108Sumedeleteall { return(DELETEALL); } 96122108Sumeget { return(GET); } 97122108Sumeflush { return(FLUSH); } 98122108Sumedump { return(DUMP); } 9955505Sshin 10055505Sshin /* for management SPD */ 101122108Sumespdadd { return(SPDADD); } 102122108Sumespddelete { return(SPDDELETE); } 103122108Sumespddump { return(SPDDUMP); } 104122108Sumespdflush { return(SPDFLUSH); } 105122108Sumetagged { return(TAGGED); } 106122108Sume{hyphen}P { BEGIN S_PL; return(F_POLICY); } 107122108Sume<S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.%\-_/ \n\t]* { 108122108Sume yymore(); 10955505Sshin 110122108Sume /* count up for nl */ 111122108Sume { 112122108Sume char *p; 113126843Sbde for (p = yytext; *p != '\0'; p++) 114122108Sume if (*p == '\n') 115122108Sume lineno++; 116122108Sume } 11755505Sshin 118122108Sume yylval.val.len = strlen(yytext); 119122108Sume yylval.val.buf = strdup(yytext); 120122108Sume if (!yylval.val.buf) 121122108Sume yyfatal("insufficient memory"); 12255505Sshin 123122108Sume return(PL_REQUESTS); 124122108Sume } 125122108Sume<S_PL>{semi} { BEGIN INITIAL; return(EOT); } 12655505Sshin 127122108Sume /* address resolution flags */ 128122108Sume{hyphen}[n46][n46]* { 129122108Sume yylval.val.len = strlen(yytext); 130122108Sume yylval.val.buf = strdup(yytext); 131122108Sume if (!yylval.val.buf) 132122108Sume yyfatal("insufficient memory"); 133122108Sume return(F_AIFLAGS); 134122108Sume } 135122108Sume 13655505Sshin /* security protocols */ 137122108Sumeah { yylval.num = 0; return(PR_AH); } 138122108Sumeesp { yylval.num = 0; return(PR_ESP); } 139122108Sumeah-old { yylval.num = 1; return(PR_AH); } 140122108Sumeesp-old { yylval.num = 1; return(PR_ESP); } 141122108Sumeipcomp { yylval.num = 0; return(PR_IPCOMP); } 142125681Sbmstcp { yylval.num = 0; return(PR_TCP); } 14355505Sshin 14455505Sshin /* authentication alogorithm */ 145122108Sume{hyphen}A { BEGIN S_AUTHALG; return(F_AUTH); } 146122108Sume<S_AUTHALG>hmac-md5 { yylval.num = SADB_AALG_MD5HMAC; BEGIN INITIAL; return(ALG_AUTH); } 147122108Sume<S_AUTHALG>hmac-sha1 { yylval.num = SADB_AALG_SHA1HMAC; BEGIN INITIAL; return(ALG_AUTH); } 148122108Sume<S_AUTHALG>keyed-md5 { yylval.num = SADB_X_AALG_MD5; BEGIN INITIAL; return(ALG_AUTH); } 149122108Sume<S_AUTHALG>keyed-sha1 { yylval.num = SADB_X_AALG_SHA; BEGIN INITIAL; return(ALG_AUTH); } 150122108Sume<S_AUTHALG>hmac-sha2-256 { yylval.num = SADB_X_AALG_SHA2_256; BEGIN INITIAL; return(ALG_AUTH); } 151122108Sume<S_AUTHALG>hmac-sha2-384 { yylval.num = SADB_X_AALG_SHA2_384; BEGIN INITIAL; return(ALG_AUTH); } 152122108Sume<S_AUTHALG>hmac-sha2-512 { yylval.num = SADB_X_AALG_SHA2_512; BEGIN INITIAL; return(ALG_AUTH); } 153122108Sume<S_AUTHALG>hmac-ripemd160 { yylval.num = SADB_X_AALG_RIPEMD160HMAC; BEGIN INITIAL; return(ALG_AUTH); } 154122108Sume<S_AUTHALG>aes-xcbc-mac { yylval.num = SADB_X_AALG_AES_XCBC_MAC; BEGIN INITIAL; return(ALG_AUTH); } 155125681Sbms<S_AUTHALG>tcp-md5 { yylval.num = SADB_X_AALG_TCP_MD5; BEGIN INITIAL; return(ALG_AUTH); } 156122108Sume<S_AUTHALG>null { yylval.num = SADB_X_AALG_NULL; BEGIN INITIAL; return(ALG_AUTH_NOKEY); } 15755505Sshin 15855505Sshin /* encryption alogorithm */ 159122108Sume{hyphen}E { BEGIN S_ENCALG; return(F_ENC); } 160122108Sume<S_ENCALG>des-cbc { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC); } 161122108Sume<S_ENCALG>3des-cbc { yylval.num = SADB_EALG_3DESCBC; BEGIN INITIAL; return(ALG_ENC); } 162122108Sume<S_ENCALG>null { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_NOKEY); } 163122108Sume<S_ENCALG>simple { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_OLD); } 164122108Sume<S_ENCALG>blowfish-cbc { yylval.num = SADB_X_EALG_BLOWFISHCBC; BEGIN INITIAL; return(ALG_ENC); } 165122108Sume<S_ENCALG>cast128-cbc { yylval.num = SADB_X_EALG_CAST128CBC; BEGIN INITIAL; return(ALG_ENC); } 166122108Sume<S_ENCALG>des-deriv { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DESDERIV); } 167122108Sume<S_ENCALG>des-32iv { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DES32IV); } 168122108Sume<S_ENCALG>rijndael-cbc { yylval.num = SADB_X_EALG_RIJNDAELCBC; BEGIN INITIAL; return(ALG_ENC); } 169122108Sume<S_ENCALG>aes-ctr { yylval.num = SADB_X_EALG_AESCTR; BEGIN INITIAL; return(ALG_ENC); } 170169425Sgnn<S_ENCALG>camellia-cbc { yylval.num = SADB_X_EALG_CAMELLIACBC; BEGIN INITIAL; return(ALG_ENC); } 17155505Sshin 17255505Sshin /* compression algorithms */ 173122108Sume{hyphen}C { return(F_COMP); } 174122108Sumeoui { yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); } 175122108Sumedeflate { yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); } 176122108Sumelzs { yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); } 177122108Sume{hyphen}R { return(F_RAWCPI); } 17855505Sshin 17955505Sshin /* extension */ 180122108Sume{hyphen}m { return(F_MODE); } 181122108Sumetransport { yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); } 182122108Sumetunnel { yylval.num = IPSEC_MODE_TUNNEL; return(MODE); } 183122108Sume{hyphen}u { return(F_REQID); } 184122108Sume{hyphen}f { return(F_EXT); } 185122108Sumerandom-pad { yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); } 186122108Sumeseq-pad { yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); } 187122108Sumezero-pad { yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); } 188122108Sumenocyclic-seq { return(NOCYCLICSEQ); } 189122108Sume{hyphen}r { return(F_REPLAY); } 190122108Sume{hyphen}lh { return(F_LIFETIME_HARD); } 191122108Sume{hyphen}ls { return(F_LIFETIME_SOFT); } 19255505Sshin 19355505Sshin /* ... */ 194122108Sumeany { return(ANY); } 195122108Sume{ws} { } 19655505Sshin{nl} { lineno++; } 19755505Sshin{comment} 198122108Sume{semi} { return(EOT); } 19955505Sshin 200122108Sume /* for address parameters: /prefix, [port] */ 201122108Sume{slash} { return SLASH; } 202122108Sume{blcl} { return BLCL; } 203122108Sume{elcl} { return ELCL; } 204122108Sume 20555505Sshin /* parameter */ 20655505Sshin{decstring} { 20755505Sshin char *bp; 20855505Sshin 209122108Sume yylval.ulnum = strtoul(yytext, &bp, 10); 21055505Sshin return(DECSTRING); 21155505Sshin } 21255505Sshin 21355505Sshin{hexstring} { 214122108Sume yylval.val.buf = strdup(yytext + 2); 215122108Sume if (!yylval.val.buf) 216122108Sume yyfatal("insufficient memory"); 217122108Sume yylval.val.len = strlen(yylval.val.buf); 21855505Sshin 21955505Sshin return(HEXSTRING); 22055505Sshin } 22155505Sshin 22255505Sshin{quotedstring} { 22355505Sshin char *p = yytext; 22455505Sshin while (*++p != '"') ; 225126843Sbde *p = '\0'; 22655505Sshin yytext++; 22755505Sshin yylval.val.len = yyleng - 2; 22855505Sshin yylval.val.buf = strdup(yytext); 229122108Sume if (!yylval.val.buf) 230122108Sume yyfatal("insufficient memory"); 23155505Sshin 23255505Sshin return(QUOTEDSTRING); 23355505Sshin } 23455505Sshin 235122108Sume[A-Za-z0-9:][A-Za-z0-9:%\.-]* { 23678064Sume yylval.val.len = yyleng; 23778064Sume yylval.val.buf = strdup(yytext); 238122108Sume if (!yylval.val.buf) 239122108Sume yyfatal("insufficient memory"); 24078064Sume return(STRING); 24178064Sume } 24278064Sume 243122108Sume[0-9,]+ { 244122108Sume yylval.val.len = yyleng; 245122108Sume yylval.val.buf = strdup(yytext); 246122108Sume if (!yylval.val.buf) 247122108Sume yyfatal("insufficient memory"); 248122108Sume return(STRING); 249122108Sume } 250122108Sume 25162583Sitojun. { 25262583Sitojun yyfatal("Syntax error"); 25362583Sitojun /*NOTREACHED*/ 25462583Sitojun } 25555505Sshin 25655505Sshin%% 25755505Sshin 25855505Sshinvoid 25962583Sitojunyyfatal(s) 26062583Sitojun const char *s; 26155505Sshin{ 26262583Sitojun yyerror(s); 26362583Sitojun exit(1); 26462583Sitojun} 26562583Sitojun 26662583Sitojunvoid 26762583Sitojunyyerror(s) 26862583Sitojun const char *s; 26962583Sitojun{ 27055505Sshin printf("line %d: %s at [%s]\n", lineno, s, yytext); 27155505Sshin} 27255505Sshin 27355505Sshinint 27455505Sshinparse(fp) 27555505Sshin FILE **fp; 27655505Sshin{ 27755505Sshin yyin = *fp; 27855505Sshin 27955505Sshin parse_init(); 28055505Sshin 28155505Sshin if (yyparse()) { 28255505Sshin printf("parse failed, line %d.\n", lineno); 28355505Sshin return(-1); 28455505Sshin } 28555505Sshin 28655505Sshin return(0); 28755505Sshin} 288