2001-11-02 06:57:25 +03:00
|
|
|
/* $NetBSD: parse.y,v 1.7 2001/11/02 03:57:25 lukem Exp $ */
|
2001-09-07 08:12:10 +04:00
|
|
|
/* $KAME: parse.y,v 1.63 2001/08/17 06:28:49 itojun Exp $ */
|
1999-07-06 17:13:03 +04:00
|
|
|
|
1999-07-02 21:41:23 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
1999-07-02 21:41:23 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
1999-07-02 21:41:23 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
%{
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
2000-02-06 14:21:45 +03:00
|
|
|
#include <net/pfkeyv2.h>
|
1999-07-02 21:41:23 +04:00
|
|
|
#include <netkey/key_var.h>
|
|
|
|
#include <netinet6/ipsec.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
2000-06-12 14:40:37 +04:00
|
|
|
#include <netdb.h>
|
1999-07-02 21:41:23 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
#include "libpfkey.h"
|
1999-07-02 21:41:23 +04:00
|
|
|
#include "vchar.h"
|
|
|
|
|
|
|
|
#define ATOX(c) \
|
2001-09-07 08:12:10 +04:00
|
|
|
(isdigit(c) ? (c - '0') : (isupper(c) ? (c - 'A' + 10) : (c - 'a' + 10)))
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
u_int32_t p_spi;
|
2001-09-07 08:12:10 +04:00
|
|
|
u_int p_ext, p_alg_enc, p_alg_auth, p_replay, p_mode;
|
2000-01-31 17:22:41 +03:00
|
|
|
u_int32_t p_reqid;
|
1999-07-02 21:41:23 +04:00
|
|
|
u_int p_key_enc_len, p_key_auth_len;
|
|
|
|
caddr_t p_key_enc, p_key_auth;
|
|
|
|
time_t p_lt_hard, p_lt_soft;
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
static int p_aiflags = 0, p_aifamily = PF_UNSPEC;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
static struct addrinfo *parse_addr __P((char *, char *));
|
|
|
|
static int setvarbuf __P((char *, int *, struct sadb_ext *, int, caddr_t, int));
|
1999-07-02 21:41:23 +04:00
|
|
|
void parse_init __P((void));
|
|
|
|
void free_buffer __P((void));
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
int setkeymsg0 __P((struct sadb_msg *, unsigned int, unsigned int, size_t));
|
|
|
|
static int setkeymsg_spdaddr __P((unsigned int, unsigned int, vchar_t *,
|
|
|
|
struct addrinfo *, int, struct addrinfo *, int));
|
|
|
|
static int setkeymsg_addr __P((unsigned int, unsigned int,
|
|
|
|
struct addrinfo *, struct addrinfo *, int));
|
|
|
|
static int setkeymsg_add __P((unsigned int, unsigned int,
|
|
|
|
struct addrinfo *, struct addrinfo *));
|
|
|
|
extern int setkeymsg __P((char *, size_t *));
|
|
|
|
extern int sendkeymsg __P((char *, size_t));
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
extern int yylex __P((void));
|
2000-06-12 14:40:37 +04:00
|
|
|
extern void yyfatal __P((const char *));
|
|
|
|
extern void yyerror __P((const char *));
|
1999-07-02 21:41:23 +04:00
|
|
|
%}
|
|
|
|
|
|
|
|
%union {
|
2001-09-07 08:12:10 +04:00
|
|
|
int num;
|
|
|
|
unsigned long ulnum;
|
1999-07-02 21:41:23 +04:00
|
|
|
vchar_t val;
|
2001-09-07 08:12:10 +04:00
|
|
|
struct addrinfo *res;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
%token EOT SLASH BLCL ELCL
|
|
|
|
%token ADD GET DELETE DELETEALL FLUSH DUMP
|
|
|
|
%token PR_ESP PR_AH PR_IPCOMP
|
1999-07-02 21:41:23 +04:00
|
|
|
%token F_PROTOCOL F_AUTH F_ENC F_REPLAY F_COMP F_RAWCPI
|
2000-01-31 17:22:41 +03:00
|
|
|
%token F_MODE MODE F_REQID
|
|
|
|
%token F_EXT EXTENSION NOCYCLICSEQ
|
|
|
|
%token ALG_AUTH ALG_ENC ALG_ENC_DESDERIV ALG_ENC_DES32IV ALG_COMP
|
1999-07-02 21:41:23 +04:00
|
|
|
%token F_LIFETIME_HARD F_LIFETIME_SOFT
|
2001-09-07 08:12:10 +04:00
|
|
|
%token DECSTRING QUOTEDSTRING HEXSTRING STRING ANY
|
1999-07-02 21:41:23 +04:00
|
|
|
/* SPD management */
|
|
|
|
%token SPDADD SPDDELETE SPDDUMP SPDFLUSH
|
|
|
|
%token F_POLICY PL_REQUESTS
|
2001-09-07 08:12:10 +04:00
|
|
|
%token F_AIFLAGS
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
%type <num> prefix protocol_spec upper_spec
|
2000-01-31 17:22:41 +03:00
|
|
|
%type <num> ALG_AUTH ALG_ENC ALG_ENC_DESDERIV ALG_ENC_DES32IV ALG_COMP
|
2001-09-07 08:12:10 +04:00
|
|
|
%type <num> PR_ESP PR_AH PR_IPCOMP
|
|
|
|
%type <num> EXTENSION MODE
|
|
|
|
%type <ulnum> DECSTRING
|
|
|
|
%type <val> PL_REQUESTS portstr key_string
|
|
|
|
%type <val> policy_requests
|
|
|
|
%type <val> QUOTEDSTRING HEXSTRING STRING
|
|
|
|
%type <val> F_AIFLAGS
|
|
|
|
%type <val> policy_spec
|
|
|
|
%type <res> ipaddr
|
2000-01-31 17:22:41 +03:00
|
|
|
|
1999-07-02 21:41:23 +04:00
|
|
|
%%
|
2000-01-31 17:22:41 +03:00
|
|
|
commands
|
|
|
|
: /*NOTHING*/
|
1999-07-02 21:41:23 +04:00
|
|
|
| commands command
|
|
|
|
{
|
|
|
|
free_buffer();
|
|
|
|
parse_init();
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
command
|
|
|
|
: add_command
|
1999-07-02 21:41:23 +04:00
|
|
|
| get_command
|
|
|
|
| delete_command
|
2001-02-17 02:55:05 +03:00
|
|
|
| deleteall_command
|
1999-07-02 21:41:23 +04:00
|
|
|
| flush_command
|
|
|
|
| dump_command
|
|
|
|
| spdadd_command
|
|
|
|
| spddelete_command
|
|
|
|
| spddump_command
|
|
|
|
| spdflush_command
|
|
|
|
;
|
|
|
|
/* commands concerned with management, there is in tail of this file. */
|
|
|
|
|
|
|
|
/* add command */
|
2000-01-31 17:22:41 +03:00
|
|
|
add_command
|
2001-09-07 08:12:10 +04:00
|
|
|
: ADD ipaddropts ipaddr ipaddr protocol_spec spi extension_spec algorithm_spec EOT
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = setkeymsg_add(SADB_ADD, $5, $3, $4);
|
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
|
|
|
/* delete */
|
2000-01-31 17:22:41 +03:00
|
|
|
delete_command
|
2001-09-07 08:12:10 +04:00
|
|
|
: DELETE ipaddropts ipaddr ipaddr protocol_spec spi extension_spec EOT
|
2000-06-12 14:40:37 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
int status;
|
|
|
|
|
|
|
|
if ($3->ai_next || $4->ai_next) {
|
|
|
|
yyerror("multiple address specified");
|
|
|
|
return -1;
|
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
if (p_mode != IPSEC_MODE_ANY)
|
|
|
|
yyerror("WARNING: mode is obsoleted.");
|
2001-09-07 08:12:10 +04:00
|
|
|
|
|
|
|
status = setkeymsg_addr(SADB_DELETE, $5, $3, $4, 0);
|
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2001-02-17 02:55:05 +03:00
|
|
|
/* deleteall command */
|
|
|
|
deleteall_command
|
2001-09-07 08:12:10 +04:00
|
|
|
: DELETEALL ipaddropts ipaddr ipaddr protocol_spec EOT
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = setkeymsg_addr(SADB_DELETE, $5, $3, $4, 1);
|
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2001-02-17 02:55:05 +03:00
|
|
|
;
|
|
|
|
|
1999-07-02 21:41:23 +04:00
|
|
|
/* get command */
|
2000-01-31 17:22:41 +03:00
|
|
|
get_command
|
2001-09-07 08:12:10 +04:00
|
|
|
: GET ipaddropts ipaddr ipaddr protocol_spec spi extension_spec EOT
|
2000-06-12 14:40:37 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
int status;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (p_mode != IPSEC_MODE_ANY)
|
|
|
|
yyerror("WARNING: mode is obsoleted.");
|
2001-09-07 08:12:10 +04:00
|
|
|
|
|
|
|
status = setkeymsg_addr(SADB_GET, $5, $3, $4, 0);
|
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
|
|
|
/* flush */
|
2000-01-31 17:22:41 +03:00
|
|
|
flush_command
|
2001-09-07 08:12:10 +04:00
|
|
|
: FLUSH protocol_spec EOT
|
|
|
|
{
|
|
|
|
struct sadb_msg msg;
|
|
|
|
setkeymsg0(&msg, SADB_FLUSH, $2, sizeof(msg));
|
|
|
|
sendkeymsg((char *)&msg, sizeof(msg));
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
|
|
|
/* dump */
|
2000-01-31 17:22:41 +03:00
|
|
|
dump_command
|
2001-09-07 08:12:10 +04:00
|
|
|
: DUMP protocol_spec EOT
|
|
|
|
{
|
|
|
|
struct sadb_msg msg;
|
|
|
|
setkeymsg0(&msg, SADB_DUMP, $2, sizeof(msg));
|
|
|
|
sendkeymsg((char *)&msg, sizeof(msg));
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
protocol_spec
|
2001-09-07 08:12:10 +04:00
|
|
|
: /*NOTHING*/
|
|
|
|
{
|
|
|
|
$$ = SADB_SATYPE_UNSPEC;
|
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
| PR_ESP
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
$$ = SADB_SATYPE_ESP;
|
2000-01-31 17:22:41 +03:00
|
|
|
if ($1 == 1)
|
|
|
|
p_ext |= SADB_X_EXT_OLD;
|
|
|
|
else
|
|
|
|
p_ext &= ~SADB_X_EXT_OLD;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
| PR_AH
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
$$ = SADB_SATYPE_AH;
|
2000-01-31 17:22:41 +03:00
|
|
|
if ($1 == 1)
|
|
|
|
p_ext |= SADB_X_EXT_OLD;
|
|
|
|
else
|
|
|
|
p_ext &= ~SADB_X_EXT_OLD;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
| PR_IPCOMP
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
$$ = SADB_X_SATYPE_IPCOMP;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
;
|
2000-01-31 17:22:41 +03:00
|
|
|
|
|
|
|
spi
|
|
|
|
: DECSTRING { p_spi = $1; }
|
1999-07-02 21:41:23 +04:00
|
|
|
| HEXSTRING
|
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
char *ep;
|
|
|
|
unsigned long v;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
ep = NULL;
|
|
|
|
v = strtoul($1.buf, &ep, 16);
|
|
|
|
if (!ep || *ep) {
|
|
|
|
yyerror("invalid SPI");
|
1999-07-02 21:41:23 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2001-09-07 08:12:10 +04:00
|
|
|
if (v & ~0xffffffff) {
|
|
|
|
yyerror("SPI too big.");
|
|
|
|
return -1;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
p_spi = v;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
algorithm_spec
|
|
|
|
: esp_spec
|
|
|
|
| ah_spec
|
|
|
|
| ipcomp_spec
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
esp_spec
|
|
|
|
: F_ENC enc_alg enc_key F_AUTH auth_alg auth_key
|
|
|
|
| F_ENC enc_alg enc_key
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
ah_spec
|
|
|
|
: F_AUTH auth_alg auth_key
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
ipcomp_spec
|
2001-09-07 08:12:10 +04:00
|
|
|
: F_COMP ALG_COMP
|
|
|
|
{
|
|
|
|
if ($2 < 0) {
|
|
|
|
yyerror("unsupported algorithm");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p_alg_enc = $2;
|
|
|
|
}
|
|
|
|
| F_COMP ALG_COMP F_RAWCPI
|
|
|
|
{
|
|
|
|
if ($2 < 0) {
|
|
|
|
yyerror("unsupported algorithm");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p_alg_enc = $2;
|
|
|
|
p_ext |= SADB_X_EXT_RAWCPI;
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
enc_alg
|
2001-09-07 08:12:10 +04:00
|
|
|
: ALG_ENC {
|
|
|
|
if ($1 < 0) {
|
|
|
|
yyerror("unsupported algorithm");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p_alg_enc = $1;
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
| ALG_ENC_DESDERIV
|
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
if ($1 < 0) {
|
|
|
|
yyerror("unsupported algorithm");
|
|
|
|
return -1;
|
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
p_alg_enc = $1;
|
1999-07-02 21:41:23 +04:00
|
|
|
if (p_ext & SADB_X_EXT_OLD) {
|
|
|
|
yyerror("algorithm mismatched.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p_ext |= SADB_X_EXT_DERIV;
|
|
|
|
}
|
|
|
|
| ALG_ENC_DES32IV
|
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
if ($1 < 0) {
|
|
|
|
yyerror("unsupported algorithm");
|
|
|
|
return -1;
|
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
p_alg_enc = $1;
|
1999-07-02 21:41:23 +04:00
|
|
|
if (!(p_ext & SADB_X_EXT_OLD)) {
|
|
|
|
yyerror("algorithm mismatched.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p_ext |= SADB_X_EXT_IV4B;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
enc_key
|
|
|
|
: /*NOTHING*/
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2000-01-31 17:22:41 +03:00
|
|
|
if (p_alg_enc != SADB_EALG_NULL) {
|
|
|
|
yyerror("no key found.");
|
1999-07-02 21:41:23 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
| key_string
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2000-01-31 17:22:41 +03:00
|
|
|
p_key_enc_len = $1.len;
|
2001-09-07 08:12:10 +04:00
|
|
|
p_key_enc = $1.buf;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
if (ipsec_check_keylen(SADB_EXT_SUPPORTED_ENCRYPT,
|
|
|
|
p_alg_enc,
|
|
|
|
PFKEY_UNUNIT64(p_key_enc_len)) < 0) {
|
|
|
|
yyerror(ipsec_strerror());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
auth_alg
|
2001-09-07 08:12:10 +04:00
|
|
|
: ALG_AUTH {
|
|
|
|
if ($1 < 0) {
|
|
|
|
yyerror("unsupported algorithm");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p_alg_auth = $1;
|
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
;
|
|
|
|
|
|
|
|
auth_key
|
|
|
|
: /*NOTHING*/
|
|
|
|
{
|
2000-07-19 03:29:48 +04:00
|
|
|
if (p_alg_auth != SADB_X_AALG_NULL) {
|
2000-01-31 17:22:41 +03:00
|
|
|
yyerror("no key found.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
| key_string
|
|
|
|
{
|
2000-01-31 17:22:41 +03:00
|
|
|
p_key_auth_len = $1.len;
|
2001-09-07 08:12:10 +04:00
|
|
|
p_key_auth = $1.buf;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
if (ipsec_check_keylen(SADB_EXT_SUPPORTED_AUTH,
|
|
|
|
p_alg_auth,
|
|
|
|
PFKEY_UNUNIT64(p_key_auth_len)) < 0) {
|
|
|
|
yyerror(ipsec_strerror());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
key_string
|
|
|
|
: QUOTEDSTRING
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
$$ = $1;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
| HEXSTRING
|
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
caddr_t pp_key;
|
1999-07-02 21:41:23 +04:00
|
|
|
caddr_t bp;
|
2000-01-31 17:22:41 +03:00
|
|
|
caddr_t yp = $1.buf;
|
2001-09-07 08:12:10 +04:00
|
|
|
int l;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
l = strlen(yp) % 2 + strlen(yp) / 2;
|
|
|
|
if ((pp_key = malloc(l)) == 0) {
|
2000-06-12 14:40:37 +04:00
|
|
|
yyerror("not enough core");
|
1999-07-02 21:41:23 +04:00
|
|
|
return -1;
|
2000-01-31 17:22:41 +03:00
|
|
|
}
|
2001-09-07 08:12:10 +04:00
|
|
|
memset(pp_key, 0, l);
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
bp = pp_key;
|
2001-09-07 08:12:10 +04:00
|
|
|
if (strlen(yp) % 2) {
|
|
|
|
*bp = ATOX(yp[0]);
|
|
|
|
yp++, bp++;
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
while (*yp) {
|
|
|
|
*bp = (ATOX(yp[0]) << 4) | ATOX(yp[1]);
|
|
|
|
yp += 2, bp++;
|
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
$$.len = l;
|
|
|
|
$$.buf = pp_key;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
extension_spec
|
|
|
|
: /*NOTHING*/
|
|
|
|
| extension_spec extension
|
|
|
|
;
|
|
|
|
|
|
|
|
extension
|
|
|
|
: F_EXT EXTENSION { p_ext |= $2; }
|
2000-06-12 14:40:37 +04:00
|
|
|
| F_EXT NOCYCLICSEQ { p_ext &= ~SADB_X_EXT_CYCSEQ; }
|
2000-01-31 17:22:41 +03:00
|
|
|
| F_MODE MODE { p_mode = $2; }
|
|
|
|
| F_MODE ANY { p_mode = IPSEC_MODE_ANY; }
|
|
|
|
| F_REQID DECSTRING { p_reqid = $2; }
|
|
|
|
| F_REPLAY DECSTRING
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
if ((p_ext & SADB_X_EXT_OLD) != 0) {
|
|
|
|
yyerror("replay prevention cannot be used with "
|
|
|
|
"ah/esp-old.");
|
2000-01-31 17:22:41 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p_replay = $2;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
2000-01-31 17:22:41 +03:00
|
|
|
| F_LIFETIME_HARD DECSTRING { p_lt_hard = $2; }
|
|
|
|
| F_LIFETIME_SOFT DECSTRING { p_lt_soft = $2; }
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
/* definition about command for SPD management */
|
|
|
|
/* spdadd */
|
|
|
|
spdadd_command
|
2001-09-07 08:12:10 +04:00
|
|
|
: SPDADD ipaddropts STRING prefix portstr STRING prefix portstr upper_spec policy_spec EOT
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
int status;
|
|
|
|
|
|
|
|
status = setkeymsg_spdaddr(SADB_X_SPDADD, $9, &$10,
|
|
|
|
parse_addr($3.buf, $5.buf), $4,
|
|
|
|
parse_addr($6.buf, $8.buf), $7);
|
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
2000-01-31 17:22:41 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
spddelete_command
|
|
|
|
: SPDDELETE ipaddropts STRING prefix portstr STRING prefix portstr upper_spec policy_spec EOT
|
2000-01-31 17:22:41 +03:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
int status;
|
|
|
|
struct addrinfo *src, *dst;
|
|
|
|
|
|
|
|
src = parse_addr($3.buf, $5.buf);
|
|
|
|
dst = parse_addr($6.buf, $8.buf);
|
|
|
|
if (!src || !dst) {
|
|
|
|
/* yyerror is already called */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (src->ai_next || dst->ai_next) {
|
|
|
|
yyerror("multiple address specified");
|
|
|
|
freeaddrinfo(src);
|
|
|
|
freeaddrinfo(dst);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = setkeymsg_spdaddr(SADB_X_SPDDELETE, $9, &$10,
|
|
|
|
src, $4, dst, $7);
|
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
|
|
|
freeaddrinfo(src);
|
|
|
|
freeaddrinfo(dst);
|
2000-01-31 17:22:41 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
spddump_command:
|
2001-09-07 08:12:10 +04:00
|
|
|
SPDDUMP EOT
|
2000-01-31 17:22:41 +03:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
struct sadb_msg msg;
|
|
|
|
setkeymsg0(&msg, SADB_X_SPDDUMP, SADB_SATYPE_UNSPEC,
|
|
|
|
sizeof(msg));
|
|
|
|
sendkeymsg((char *)&msg, sizeof(msg));
|
2000-01-31 17:22:41 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
spdflush_command:
|
2001-09-07 08:12:10 +04:00
|
|
|
SPDFLUSH EOT
|
2000-01-31 17:22:41 +03:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
struct sadb_msg msg;
|
|
|
|
setkeymsg0(&msg, SADB_X_SPDFLUSH, SADB_SATYPE_UNSPEC,
|
|
|
|
sizeof(msg));
|
|
|
|
sendkeymsg((char *)&msg, sizeof(msg));
|
2000-01-31 17:22:41 +03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
ipaddropts
|
|
|
|
: /* nothing */
|
|
|
|
| ipaddropts ipaddropt
|
|
|
|
;
|
|
|
|
|
|
|
|
ipaddropt
|
|
|
|
: /* nothing */
|
|
|
|
| F_AIFLAGS
|
2000-06-12 14:40:37 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (p = $1.buf + 1; *p; p++)
|
|
|
|
switch (*p) {
|
|
|
|
case '4':
|
|
|
|
p_aifamily = AF_INET;
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#ifdef INET6
|
2001-09-07 08:12:10 +04:00
|
|
|
case '6':
|
|
|
|
p_aifamily = AF_INET6;
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#endif
|
2001-09-07 08:12:10 +04:00
|
|
|
case 'n':
|
|
|
|
p_aiflags = AI_NUMERICHOST;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
yyerror("invalid flag");
|
2000-06-12 14:40:37 +04:00
|
|
|
return -1;
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
ipaddr
|
|
|
|
: STRING
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
$$ = parse_addr($1.buf, NULL);
|
|
|
|
if ($$ == NULL) {
|
|
|
|
/* yyerror already called by parse_addr */
|
1999-07-02 21:41:23 +04:00
|
|
|
return -1;
|
2000-01-31 17:22:41 +03:00
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
prefix
|
2001-09-07 08:12:10 +04:00
|
|
|
: /*NOTHING*/ { $$ = -1; }
|
|
|
|
| SLASH DECSTRING { $$ = $2; }
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
portstr
|
|
|
|
: /*NOTHING*/
|
|
|
|
{
|
|
|
|
$$.buf = strdup("0");
|
|
|
|
$$.len = strlen($$.buf);
|
|
|
|
}
|
|
|
|
| BLCL ANY ELCL
|
|
|
|
{
|
|
|
|
$$.buf = strdup("0");
|
|
|
|
$$.len = strlen($$.buf);
|
|
|
|
}
|
|
|
|
| BLCL DECSTRING ELCL
|
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
snprintf(buf, sizeof(buf), "%lu", $2);
|
|
|
|
$$.buf = strdup(buf);
|
|
|
|
$$.len = strlen($$.buf);
|
|
|
|
}
|
|
|
|
| BLCL STRING ELCL
|
|
|
|
{
|
|
|
|
$$ = $2;
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
upper_spec
|
2001-09-07 08:12:10 +04:00
|
|
|
: DECSTRING { $$ = $1; }
|
|
|
|
| ANY { $$ = IPSEC_ULPROTO_ANY; }
|
|
|
|
| STRING
|
|
|
|
{
|
|
|
|
struct protoent *ent;
|
|
|
|
|
|
|
|
ent = getprotobyname($1.buf);
|
|
|
|
if (ent)
|
|
|
|
$$ = ent->p_proto;
|
|
|
|
else {
|
|
|
|
if (strcmp("icmp6", $1.buf) == 0) {
|
|
|
|
$$ = IPPROTO_ICMPV6;
|
|
|
|
} else if(strcmp("ip4", $1.buf) == 0) {
|
|
|
|
$$ = IPPROTO_IPV4;
|
|
|
|
} else {
|
|
|
|
yyerror("invalid upper layer protocol");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endprotoent();
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
policy_spec
|
|
|
|
: F_POLICY policy_requests
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
char *policy;
|
|
|
|
|
|
|
|
policy = ipsec_set_policy($2.buf, $2.len);
|
|
|
|
if (policy == NULL) {
|
1999-07-02 21:41:23 +04:00
|
|
|
yyerror(ipsec_strerror());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
$$.buf = policy;
|
|
|
|
$$.len = ipsec_get_policylen(policy);
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
policy_requests
|
|
|
|
: PL_REQUESTS { $$ = $1; }
|
1999-07-02 21:41:23 +04:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
int
|
2001-09-07 08:12:10 +04:00
|
|
|
setkeymsg0(msg, type, satype, l)
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
unsigned int type;
|
|
|
|
unsigned int satype;
|
|
|
|
size_t l;
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
|
|
|
|
msg->sadb_msg_version = PF_KEY_V2;
|
|
|
|
msg->sadb_msg_type = type;
|
|
|
|
msg->sadb_msg_errno = 0;
|
|
|
|
msg->sadb_msg_satype = satype;
|
|
|
|
msg->sadb_msg_reserved = 0;
|
|
|
|
msg->sadb_msg_seq = 0;
|
|
|
|
msg->sadb_msg_pid = getpid();
|
|
|
|
msg->sadb_msg_len = PFKEY_UNIT64(l);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX NO BUFFER OVERRUN CHECK! BAD BAD! */
|
|
|
|
static int
|
|
|
|
setkeymsg_spdaddr(type, upper, policy, srcs, splen, dsts, dplen)
|
|
|
|
unsigned int type;
|
|
|
|
unsigned int upper;
|
|
|
|
vchar_t *policy;
|
|
|
|
struct addrinfo *srcs;
|
|
|
|
int splen;
|
|
|
|
struct addrinfo *dsts;
|
|
|
|
int dplen;
|
|
|
|
{
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int l, l0;
|
|
|
|
struct sadb_address m_addr;
|
|
|
|
struct addrinfo *s, *d;
|
|
|
|
int n;
|
|
|
|
int plen;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
int salen;
|
|
|
|
|
|
|
|
msg = (struct sadb_msg *)buf;
|
|
|
|
|
|
|
|
if (!srcs || !dsts)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* fix up length afterwards */
|
|
|
|
setkeymsg0(msg, type, SADB_SATYPE_UNSPEC, 0);
|
|
|
|
l = sizeof(struct sadb_msg);
|
|
|
|
|
|
|
|
memcpy(buf + l, policy->buf, policy->len);
|
|
|
|
l += policy->len;
|
|
|
|
|
|
|
|
l0 = l;
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
/* do it for all src/dst pairs */
|
|
|
|
for (s = srcs; s; s = s->ai_next) {
|
|
|
|
for (d = dsts; d; d = d->ai_next) {
|
|
|
|
/* rewind pointer */
|
|
|
|
l = l0;
|
|
|
|
|
|
|
|
if (s->ai_addr->sa_family != d->ai_addr->sa_family)
|
|
|
|
continue;
|
|
|
|
switch (s->ai_addr->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
plen = sizeof(struct in_addr) << 3;
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#ifdef INET6
|
2001-09-07 08:12:10 +04:00
|
|
|
case AF_INET6:
|
|
|
|
plen = sizeof(struct in6_addr) << 3;
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#endif
|
2001-09-07 08:12:10 +04:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set src */
|
|
|
|
sa = s->ai_addr;
|
|
|
|
salen = s->ai_addr->sa_len;
|
|
|
|
m_addr.sadb_address_len = PFKEY_UNIT64(sizeof(m_addr) +
|
|
|
|
PFKEY_ALIGN8(salen));
|
|
|
|
m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
|
|
|
|
m_addr.sadb_address_proto = upper;
|
2000-06-12 14:40:37 +04:00
|
|
|
m_addr.sadb_address_prefixlen =
|
2001-09-07 08:12:10 +04:00
|
|
|
(splen >= 0 ? splen : plen);
|
|
|
|
m_addr.sadb_address_reserved = 0;
|
|
|
|
|
|
|
|
setvarbuf(buf, &l, (struct sadb_ext *)&m_addr,
|
|
|
|
sizeof(m_addr), (caddr_t)sa, salen);
|
|
|
|
|
|
|
|
/* set dst */
|
|
|
|
sa = d->ai_addr;
|
|
|
|
salen = d->ai_addr->sa_len;
|
|
|
|
m_addr.sadb_address_len = PFKEY_UNIT64(sizeof(m_addr) +
|
|
|
|
PFKEY_ALIGN8(salen));
|
|
|
|
m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_DST;
|
|
|
|
m_addr.sadb_address_proto = upper;
|
2000-06-12 14:40:37 +04:00
|
|
|
m_addr.sadb_address_prefixlen =
|
2001-09-07 08:12:10 +04:00
|
|
|
(dplen >= 0 ? dplen : plen);
|
|
|
|
m_addr.sadb_address_reserved = 0;
|
|
|
|
|
|
|
|
setvarbuf(buf, &l, (struct sadb_ext *)&m_addr,
|
|
|
|
sizeof(m_addr), (caddr_t)sa, salen);
|
|
|
|
|
|
|
|
msg->sadb_msg_len = PFKEY_UNIT64(l);
|
|
|
|
|
|
|
|
sendkeymsg(buf, l);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX NO BUFFER OVERRUN CHECK! BAD BAD! */
|
|
|
|
static int
|
|
|
|
setkeymsg_addr(type, satype, srcs, dsts, no_spi)
|
|
|
|
unsigned int type;
|
|
|
|
unsigned int satype;
|
|
|
|
struct addrinfo *srcs;
|
|
|
|
struct addrinfo *dsts;
|
|
|
|
int no_spi;
|
|
|
|
{
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int l, l0, len;
|
|
|
|
struct sadb_sa m_sa;
|
|
|
|
struct sadb_x_sa2 m_sa2;
|
|
|
|
struct sadb_address m_addr;
|
|
|
|
struct addrinfo *s, *d;
|
|
|
|
int n;
|
|
|
|
int plen;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
int salen;
|
|
|
|
|
|
|
|
msg = (struct sadb_msg *)buf;
|
|
|
|
|
|
|
|
if (!srcs || !dsts)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* fix up length afterwards */
|
|
|
|
setkeymsg0(msg, type, satype, 0);
|
|
|
|
l = sizeof(struct sadb_msg);
|
|
|
|
|
|
|
|
if (!no_spi) {
|
|
|
|
len = sizeof(struct sadb_sa);
|
|
|
|
m_sa.sadb_sa_len = PFKEY_UNIT64(len);
|
|
|
|
m_sa.sadb_sa_exttype = SADB_EXT_SA;
|
|
|
|
m_sa.sadb_sa_spi = htonl(p_spi);
|
|
|
|
m_sa.sadb_sa_replay = p_replay;
|
|
|
|
m_sa.sadb_sa_state = 0;
|
|
|
|
m_sa.sadb_sa_auth = p_alg_auth;
|
|
|
|
m_sa.sadb_sa_encrypt = p_alg_enc;
|
|
|
|
m_sa.sadb_sa_flags = p_ext;
|
|
|
|
|
|
|
|
memcpy(buf + l, &m_sa, len);
|
|
|
|
l += len;
|
|
|
|
|
|
|
|
len = sizeof(struct sadb_x_sa2);
|
|
|
|
m_sa2.sadb_x_sa2_len = PFKEY_UNIT64(len);
|
|
|
|
m_sa2.sadb_x_sa2_exttype = SADB_X_EXT_SA2;
|
|
|
|
m_sa2.sadb_x_sa2_mode = p_mode;
|
|
|
|
m_sa2.sadb_x_sa2_reqid = p_reqid;
|
|
|
|
|
|
|
|
memcpy(buf + l, &m_sa2, len);
|
|
|
|
l += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
l0 = l;
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
/* do it for all src/dst pairs */
|
|
|
|
for (s = srcs; s; s = s->ai_next) {
|
|
|
|
for (d = dsts; d; d = d->ai_next) {
|
|
|
|
/* rewind pointer */
|
|
|
|
l = l0;
|
|
|
|
|
|
|
|
if (s->ai_addr->sa_family != d->ai_addr->sa_family)
|
|
|
|
continue;
|
|
|
|
switch (s->ai_addr->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
plen = sizeof(struct in_addr) << 3;
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#ifdef INET6
|
2001-09-07 08:12:10 +04:00
|
|
|
case AF_INET6:
|
|
|
|
plen = sizeof(struct in6_addr) << 3;
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#endif
|
2001-09-07 08:12:10 +04:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set src */
|
|
|
|
sa = s->ai_addr;
|
|
|
|
salen = s->ai_addr->sa_len;
|
|
|
|
m_addr.sadb_address_len = PFKEY_UNIT64(sizeof(m_addr) +
|
|
|
|
PFKEY_ALIGN8(salen));
|
|
|
|
m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
|
|
|
|
m_addr.sadb_address_proto = IPSEC_ULPROTO_ANY;
|
|
|
|
m_addr.sadb_address_prefixlen = plen;
|
|
|
|
m_addr.sadb_address_reserved = 0;
|
|
|
|
|
|
|
|
setvarbuf(buf, &l, (struct sadb_ext *)&m_addr,
|
|
|
|
sizeof(m_addr), (caddr_t)sa, salen);
|
|
|
|
|
|
|
|
/* set dst */
|
|
|
|
sa = d->ai_addr;
|
|
|
|
salen = d->ai_addr->sa_len;
|
|
|
|
m_addr.sadb_address_len = PFKEY_UNIT64(sizeof(m_addr) +
|
|
|
|
PFKEY_ALIGN8(salen));
|
|
|
|
m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_DST;
|
|
|
|
m_addr.sadb_address_proto = IPSEC_ULPROTO_ANY;
|
|
|
|
m_addr.sadb_address_prefixlen = plen;
|
|
|
|
m_addr.sadb_address_reserved = 0;
|
|
|
|
|
|
|
|
setvarbuf(buf, &l, (struct sadb_ext *)&m_addr,
|
|
|
|
sizeof(m_addr), (caddr_t)sa, salen);
|
|
|
|
|
|
|
|
msg->sadb_msg_len = PFKEY_UNIT64(l);
|
|
|
|
|
|
|
|
sendkeymsg(buf, l);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX NO BUFFER OVERRUN CHECK! BAD BAD! */
|
|
|
|
static int
|
|
|
|
setkeymsg_add(type, satype, srcs, dsts)
|
|
|
|
unsigned int type;
|
|
|
|
unsigned int satype;
|
|
|
|
struct addrinfo *srcs;
|
|
|
|
struct addrinfo *dsts;
|
|
|
|
{
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int l, l0, len;
|
|
|
|
struct sadb_sa m_sa;
|
|
|
|
struct sadb_x_sa2 m_sa2;
|
|
|
|
struct sadb_address m_addr;
|
|
|
|
struct addrinfo *s, *d;
|
|
|
|
int n;
|
|
|
|
int plen;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
int salen;
|
|
|
|
|
|
|
|
msg = (struct sadb_msg *)buf;
|
|
|
|
|
|
|
|
if (!srcs || !dsts)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* fix up length afterwards */
|
|
|
|
setkeymsg0(msg, type, satype, 0);
|
|
|
|
l = sizeof(struct sadb_msg);
|
|
|
|
|
|
|
|
/* set encryption algorithm, if present. */
|
|
|
|
if (satype != SADB_X_SATYPE_IPCOMP && p_alg_enc != SADB_EALG_NONE) {
|
|
|
|
struct sadb_key m_key;
|
|
|
|
|
|
|
|
m_key.sadb_key_len =
|
|
|
|
PFKEY_UNIT64(sizeof(m_key)
|
|
|
|
+ PFKEY_ALIGN8(p_key_enc_len));
|
|
|
|
m_key.sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
|
|
|
|
m_key.sadb_key_bits = p_key_enc_len * 8;
|
|
|
|
m_key.sadb_key_reserved = 0;
|
|
|
|
|
|
|
|
setvarbuf(buf, &l,
|
|
|
|
(struct sadb_ext *)&m_key, sizeof(m_key),
|
|
|
|
(caddr_t)p_key_enc, p_key_enc_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set authentication algorithm, if present. */
|
|
|
|
if (p_alg_auth != SADB_AALG_NONE) {
|
|
|
|
struct sadb_key m_key;
|
|
|
|
|
|
|
|
m_key.sadb_key_len =
|
|
|
|
PFKEY_UNIT64(sizeof(m_key)
|
|
|
|
+ PFKEY_ALIGN8(p_key_auth_len));
|
|
|
|
m_key.sadb_key_exttype = SADB_EXT_KEY_AUTH;
|
|
|
|
m_key.sadb_key_bits = p_key_auth_len * 8;
|
|
|
|
m_key.sadb_key_reserved = 0;
|
|
|
|
|
|
|
|
setvarbuf(buf, &l,
|
|
|
|
(struct sadb_ext *)&m_key, sizeof(m_key),
|
|
|
|
(caddr_t)p_key_auth, p_key_auth_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set lifetime for HARD */
|
|
|
|
if (p_lt_hard != 0) {
|
|
|
|
struct sadb_lifetime m_lt;
|
2001-11-02 06:57:25 +03:00
|
|
|
|
|
|
|
len = sizeof(struct sadb_lifetime);
|
2001-09-07 08:12:10 +04:00
|
|
|
|
|
|
|
m_lt.sadb_lifetime_len = PFKEY_UNIT64(len);
|
|
|
|
m_lt.sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
|
|
|
|
m_lt.sadb_lifetime_allocations = 0;
|
|
|
|
m_lt.sadb_lifetime_bytes = 0;
|
|
|
|
m_lt.sadb_lifetime_addtime = p_lt_hard;
|
|
|
|
m_lt.sadb_lifetime_usetime = 0;
|
|
|
|
|
|
|
|
memcpy(buf + l, &m_lt, len);
|
|
|
|
l += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set lifetime for SOFT */
|
|
|
|
if (p_lt_soft != 0) {
|
|
|
|
struct sadb_lifetime m_lt;
|
2001-11-02 06:57:25 +03:00
|
|
|
|
|
|
|
len = sizeof(struct sadb_lifetime);
|
2001-09-07 08:12:10 +04:00
|
|
|
|
|
|
|
m_lt.sadb_lifetime_len = PFKEY_UNIT64(len);
|
|
|
|
m_lt.sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
|
|
|
|
m_lt.sadb_lifetime_allocations = 0;
|
|
|
|
m_lt.sadb_lifetime_bytes = 0;
|
|
|
|
m_lt.sadb_lifetime_addtime = p_lt_soft;
|
|
|
|
m_lt.sadb_lifetime_usetime = 0;
|
|
|
|
|
|
|
|
memcpy(buf + l, &m_lt, len);
|
|
|
|
l += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = sizeof(struct sadb_sa);
|
|
|
|
m_sa.sadb_sa_len = PFKEY_UNIT64(len);
|
|
|
|
m_sa.sadb_sa_exttype = SADB_EXT_SA;
|
|
|
|
m_sa.sadb_sa_spi = htonl(p_spi);
|
|
|
|
m_sa.sadb_sa_replay = p_replay;
|
|
|
|
m_sa.sadb_sa_state = 0;
|
|
|
|
m_sa.sadb_sa_auth = p_alg_auth;
|
|
|
|
m_sa.sadb_sa_encrypt = p_alg_enc;
|
|
|
|
m_sa.sadb_sa_flags = p_ext;
|
|
|
|
|
|
|
|
memcpy(buf + l, &m_sa, len);
|
|
|
|
l += len;
|
|
|
|
|
|
|
|
len = sizeof(struct sadb_x_sa2);
|
|
|
|
m_sa2.sadb_x_sa2_len = PFKEY_UNIT64(len);
|
|
|
|
m_sa2.sadb_x_sa2_exttype = SADB_X_EXT_SA2;
|
|
|
|
m_sa2.sadb_x_sa2_mode = p_mode;
|
|
|
|
m_sa2.sadb_x_sa2_reqid = p_reqid;
|
|
|
|
|
|
|
|
memcpy(buf + l, &m_sa2, len);
|
|
|
|
l += len;
|
|
|
|
|
|
|
|
l0 = l;
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
/* do it for all src/dst pairs */
|
|
|
|
for (s = srcs; s; s = s->ai_next) {
|
|
|
|
for (d = dsts; d; d = d->ai_next) {
|
|
|
|
/* rewind pointer */
|
|
|
|
l = l0;
|
|
|
|
|
|
|
|
if (s->ai_addr->sa_family != d->ai_addr->sa_family)
|
|
|
|
continue;
|
|
|
|
switch (s->ai_addr->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
plen = sizeof(struct in_addr) << 3;
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#ifdef INET6
|
2001-09-07 08:12:10 +04:00
|
|
|
case AF_INET6:
|
|
|
|
plen = sizeof(struct in6_addr) << 3;
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#endif
|
2001-09-07 08:12:10 +04:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
/* set src */
|
|
|
|
sa = s->ai_addr;
|
|
|
|
salen = s->ai_addr->sa_len;
|
|
|
|
m_addr.sadb_address_len = PFKEY_UNIT64(sizeof(m_addr) +
|
|
|
|
PFKEY_ALIGN8(salen));
|
|
|
|
m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
|
|
|
|
m_addr.sadb_address_proto = IPSEC_ULPROTO_ANY;
|
|
|
|
m_addr.sadb_address_prefixlen = plen;
|
|
|
|
m_addr.sadb_address_reserved = 0;
|
|
|
|
|
|
|
|
setvarbuf(buf, &l, (struct sadb_ext *)&m_addr,
|
|
|
|
sizeof(m_addr), (caddr_t)sa, salen);
|
|
|
|
|
|
|
|
/* set dst */
|
2001-09-20 04:01:10 +04:00
|
|
|
sa = d->ai_addr;
|
2001-09-07 08:12:10 +04:00
|
|
|
salen = d->ai_addr->sa_len;
|
|
|
|
m_addr.sadb_address_len = PFKEY_UNIT64(sizeof(m_addr) +
|
|
|
|
PFKEY_ALIGN8(salen));
|
|
|
|
m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_DST;
|
|
|
|
m_addr.sadb_address_proto = IPSEC_ULPROTO_ANY;
|
|
|
|
m_addr.sadb_address_prefixlen = plen;
|
|
|
|
m_addr.sadb_address_reserved = 0;
|
|
|
|
|
|
|
|
setvarbuf(buf, &l, (struct sadb_ext *)&m_addr,
|
|
|
|
sizeof(m_addr), (caddr_t)sa, salen);
|
|
|
|
|
|
|
|
msg->sadb_msg_len = PFKEY_UNIT64(l);
|
|
|
|
|
|
|
|
sendkeymsg(buf, l);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
if (n == 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct addrinfo *
|
2001-09-07 08:12:10 +04:00
|
|
|
parse_addr(host, port)
|
2000-06-12 14:40:37 +04:00
|
|
|
char *host;
|
|
|
|
char *port;
|
|
|
|
{
|
|
|
|
struct addrinfo hints, *res = NULL;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2001-09-07 08:12:10 +04:00
|
|
|
hints.ai_family = p_aifamily;
|
2000-06-12 14:40:37 +04:00
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2001-09-07 08:12:10 +04:00
|
|
|
hints.ai_flags = p_aiflags;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = getaddrinfo(host, port, &hints, &res);
|
|
|
|
if (error != 0) {
|
|
|
|
yyerror(gai_strerror(error));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-07-02 21:41:23 +04:00
|
|
|
static int
|
2001-09-07 08:12:10 +04:00
|
|
|
setvarbuf(buf, off, ebuf, elen, vbuf, vlen)
|
|
|
|
char *buf;
|
|
|
|
int *off;
|
1999-07-02 21:41:23 +04:00
|
|
|
struct sadb_ext *ebuf;
|
2001-09-07 08:12:10 +04:00
|
|
|
int elen;
|
|
|
|
caddr_t vbuf;
|
|
|
|
int vlen;
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
memset(buf + *off, 0, PFKEY_UNUNIT64(ebuf->sadb_ext_len));
|
|
|
|
memcpy(buf + *off, (caddr_t)ebuf, elen);
|
|
|
|
memcpy(buf + *off + elen, vbuf, vlen);
|
1999-07-02 21:41:23 +04:00
|
|
|
(*off) += PFKEY_ALIGN8(elen + vlen);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parse_init()
|
|
|
|
{
|
|
|
|
p_spi = 0;
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
p_ext = SADB_X_EXT_CYCSEQ;
|
1999-07-02 21:41:23 +04:00
|
|
|
p_alg_enc = SADB_EALG_NONE;
|
|
|
|
p_alg_auth = SADB_AALG_NONE;
|
2000-01-31 17:22:41 +03:00
|
|
|
p_mode = IPSEC_MODE_ANY;
|
|
|
|
p_reqid = 0;
|
2000-03-07 01:19:27 +03:00
|
|
|
p_replay = 0;
|
1999-07-02 21:41:23 +04:00
|
|
|
p_key_enc_len = p_key_auth_len = 0;
|
|
|
|
p_key_enc = p_key_auth = 0;
|
|
|
|
p_lt_hard = p_lt_soft = 0;
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
p_aiflags = 0;
|
|
|
|
p_aifamily = PF_UNSPEC;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free_buffer()
|
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
/* we got tons of memory leaks in the parser anyways, leave them */
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|