Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
/* $NetBSD: key.c,v 1.108 2003/12/04 19:38:25 atatat Exp $ */
|
2003-09-08 10:51:53 +04:00
|
|
|
/* $KAME: key.c,v 1.310 2003/09/08 02:23:44 itojun Exp $ */
|
1999-07-04 01:24:45 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +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-06-28 10:36:47 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
* This code is referred to RFC 2367
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.108 2003/12/04 19:38:25 atatat Exp $");
|
2001-11-13 03:56:55 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#include "opt_inet.h"
|
1999-07-10 02:57:15 +04:00
|
|
|
#include "opt_ipsec.h"
|
2003-09-08 10:51:53 +04:00
|
|
|
#include "fs_kernfs.h"
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2000-03-23 10:01:25 +03:00
|
|
|
#include <sys/callout.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/proc.h>
|
2000-01-31 17:18:52 +03:00
|
|
|
#include <sys/queue.h>
|
2001-10-29 10:02:30 +03:00
|
|
|
#include <sys/sysctl.h>
|
2002-01-31 09:35:25 +03:00
|
|
|
#include <sys/syslog.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/raw_cb.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
|
|
|
|
#ifdef INET6
|
2000-02-06 15:49:37 +03:00
|
|
|
#include <netinet/ip6.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/in6_var.h>
|
2000-01-31 17:18:52 +03:00
|
|
|
#include <netinet6/ip6_var.h>
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
|
|
|
#ifdef INET
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/in6_pcb.h>
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
2000-02-06 15:49:37 +03:00
|
|
|
#include <net/pfkeyv2.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netkey/keydb.h>
|
|
|
|
#include <netkey/key.h>
|
|
|
|
#include <netkey/keysock.h>
|
|
|
|
#include <netkey/key_debug.h>
|
|
|
|
|
|
|
|
#include <netinet6/ipsec.h>
|
|
|
|
#include <netinet6/ah.h>
|
1999-07-01 12:12:45 +04:00
|
|
|
#ifdef IPSEC_ESP
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/esp.h>
|
1999-07-01 12:12:45 +04:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/ipcomp.h>
|
|
|
|
|
2003-09-08 10:51:53 +04:00
|
|
|
#ifdef KERNFS
|
|
|
|
#include <miscfs/kernfs/kernfs.h>
|
|
|
|
#endif
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
|
|
|
#include "rnd.h"
|
|
|
|
#if NRND > 0
|
|
|
|
#include <sys/rnd.h>
|
|
|
|
#endif
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
#include <net/net_osdep.h>
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
#ifndef offsetof
|
|
|
|
#define offsetof(type, member) ((size_t)(&((type *)0)->member))
|
|
|
|
#endif
|
|
|
|
#ifndef satosin
|
|
|
|
#define satosin(s) ((struct sockaddr_in *)s)
|
|
|
|
#endif
|
|
|
|
|
2001-10-19 05:57:20 +04:00
|
|
|
#define FULLMASK 0xff
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* Note on SA reference counting:
|
|
|
|
* - SAs that are not in DEAD state will have (total external reference + 1)
|
|
|
|
* following value in reference count field. they cannot be freed and are
|
|
|
|
* referenced from SA header.
|
|
|
|
* - SAs that are in DEAD state will have (total external reference)
|
|
|
|
* in reference count field. they are ready to be freed. reference from
|
2003-09-07 19:59:36 +04:00
|
|
|
* SA header will be removed in keydb_delsecasvar(), when the reference count
|
2000-01-31 17:18:52 +03:00
|
|
|
* field hits 0 (= no external reference other than from SA header.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
u_int32_t key_debug_level = 0;
|
|
|
|
static u_int key_spi_trycnt = 1000;
|
|
|
|
static u_int32_t key_spi_minval = 0x100;
|
|
|
|
static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
|
|
|
|
static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
|
|
|
|
static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
|
|
|
|
static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
|
|
|
|
static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
|
|
|
|
|
|
|
|
static u_int32_t acq_seq = 0;
|
|
|
|
static int key_tick_init_random = 0;
|
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
struct _satailq satailq; /* list of all SAD entry */
|
|
|
|
struct _sptailq sptailq; /* SPD table + pcb */
|
2002-06-12 21:56:45 +04:00
|
|
|
static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD table */
|
2000-01-31 17:18:52 +03:00
|
|
|
static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
|
|
|
|
static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
|
|
|
|
/* registed list */
|
2003-09-07 19:59:36 +04:00
|
|
|
|
2003-09-14 11:30:32 +04:00
|
|
|
#define SPIHASHSIZE 128
|
|
|
|
#define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE)
|
2003-09-07 19:59:36 +04:00
|
|
|
static LIST_HEAD(_spihash, secasvar) spihash[SPIHASHSIZE];
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
2000-01-31 17:18:52 +03:00
|
|
|
static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
|
2000-01-31 17:18:52 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
struct key_cb key_cb;
|
|
|
|
|
|
|
|
/* search order for SAs */
|
2001-02-22 00:39:52 +03:00
|
|
|
static const u_int saorder_state_valid[] = {
|
2000-01-31 17:18:52 +03:00
|
|
|
SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
|
|
|
|
/*
|
|
|
|
* This order is important because we must select a oldest SA
|
|
|
|
* for outbound processing. For inbound, This is not important.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
};
|
2001-02-22 00:39:52 +03:00
|
|
|
static const u_int saorder_state_alive[] = {
|
2000-01-31 17:18:52 +03:00
|
|
|
/* except DEAD */
|
1999-06-28 10:36:47 +04:00
|
|
|
SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
|
|
|
|
};
|
2001-02-22 00:39:52 +03:00
|
|
|
static const u_int saorder_state_any[] = {
|
2000-06-12 14:40:37 +04:00
|
|
|
SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
|
1999-06-28 10:36:47 +04:00
|
|
|
SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
|
|
|
|
};
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
static const int minsize[] = {
|
|
|
|
sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
|
|
|
|
sizeof(struct sadb_sa), /* SADB_EXT_SA */
|
|
|
|
sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
|
|
|
|
sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
|
|
|
|
sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
|
|
|
|
sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
|
|
|
|
sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
|
|
|
|
sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
|
|
|
|
sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
|
|
|
|
sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
|
|
|
|
sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
|
|
|
|
sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
|
|
|
|
sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
|
|
|
|
sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
|
|
|
|
sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
|
|
|
|
sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
|
|
|
|
sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
|
|
|
|
0, /* SADB_X_EXT_KMPRIVATE */
|
|
|
|
sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
|
|
|
|
sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
sizeof(struct sadb_x_tag), /* SADB_X_TAG */
|
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
};
|
|
|
|
static const int maxsize[] = {
|
|
|
|
sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
|
|
|
|
sizeof(struct sadb_sa), /* SADB_EXT_SA */
|
|
|
|
sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
|
|
|
|
sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
|
|
|
|
sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
|
|
|
|
0, /* SADB_EXT_ADDRESS_SRC */
|
|
|
|
0, /* SADB_EXT_ADDRESS_DST */
|
|
|
|
0, /* SADB_EXT_ADDRESS_PROXY */
|
|
|
|
0, /* SADB_EXT_KEY_AUTH */
|
|
|
|
0, /* SADB_EXT_KEY_ENCRYPT */
|
|
|
|
0, /* SADB_EXT_IDENTITY_SRC */
|
|
|
|
0, /* SADB_EXT_IDENTITY_DST */
|
|
|
|
0, /* SADB_EXT_SENSITIVITY */
|
|
|
|
0, /* SADB_EXT_PROPOSAL */
|
|
|
|
0, /* SADB_EXT_SUPPORTED_AUTH */
|
|
|
|
0, /* SADB_EXT_SUPPORTED_ENCRYPT */
|
|
|
|
sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
|
|
|
|
0, /* SADB_X_EXT_KMPRIVATE */
|
|
|
|
0, /* SADB_X_EXT_POLICY */
|
|
|
|
sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
sizeof(struct sadb_x_tag), /* SADB_X_TAG */
|
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
};
|
|
|
|
|
2002-05-19 12:12:55 +04:00
|
|
|
static int ipsec_esp_keymin = 256;
|
|
|
|
static int ipsec_esp_auth = 0;
|
|
|
|
static int ipsec_ah_keymin = 128;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
#define __LIST_CHAINED(elm) \
|
|
|
|
(!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
|
|
|
|
#define LIST_INSERT_TAIL(head, elm, type, field) \
|
|
|
|
do {\
|
|
|
|
struct type *curelm = LIST_FIRST(head); \
|
|
|
|
if (curelm == NULL) {\
|
|
|
|
LIST_INSERT_HEAD(head, elm, field); \
|
|
|
|
} else { \
|
|
|
|
while (LIST_NEXT(curelm, field)) \
|
|
|
|
curelm = LIST_NEXT(curelm, field);\
|
|
|
|
LIST_INSERT_AFTER(curelm, elm, field);\
|
|
|
|
}\
|
2002-11-02 10:30:55 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
#define KEY_CHKSASTATE(head, sav, name) \
|
|
|
|
do { \
|
2002-01-31 09:35:25 +03:00
|
|
|
if ((head) != (sav)) { \
|
2002-06-27 18:39:45 +04:00
|
|
|
ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%u SA=%u)\n", \
|
2002-01-31 09:35:25 +03:00
|
|
|
(name), (head), (sav))); \
|
|
|
|
continue; \
|
|
|
|
} \
|
2002-11-02 10:30:55 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
#define KEY_CHKSPDIR(head, sp, name) \
|
|
|
|
do { \
|
2002-01-31 09:35:25 +03:00
|
|
|
if ((head) != (sp)) { \
|
2002-06-27 18:39:45 +04:00
|
|
|
ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%u SP=%u), " \
|
2002-01-31 09:35:25 +03:00
|
|
|
"anyway continue.\n", \
|
|
|
|
(name), (head), (sp))); \
|
|
|
|
} \
|
2002-11-02 10:30:55 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#if 1
|
2000-01-31 17:18:52 +03:00
|
|
|
#define KMALLOC(p, t, n) \
|
1999-06-28 10:36:47 +04:00
|
|
|
((p) = (t) malloc((unsigned long)(n), M_SECA, M_NOWAIT))
|
2000-01-31 17:18:52 +03:00
|
|
|
#define KFREE(p) \
|
2002-01-31 09:35:25 +03:00
|
|
|
free((caddr_t)(p), M_SECA)
|
1999-06-28 10:36:47 +04:00
|
|
|
#else
|
|
|
|
#define KMALLOC(p, t, n) \
|
2000-01-31 17:18:52 +03:00
|
|
|
do { \
|
|
|
|
((p) = (t)malloc((unsigned long)(n), M_SECA, M_NOWAIT)); \
|
|
|
|
printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
|
|
|
|
__FILE__, __LINE__, (p), #t, n); \
|
2002-11-02 10:30:55 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
#define KFREE(p) \
|
|
|
|
do { \
|
|
|
|
printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
|
|
|
|
free((caddr_t)(p), M_SECA); \
|
2002-11-02 10:30:55 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* set parameters into secpolicyindex buffer.
|
|
|
|
* Must allocate secpolicyindex buffer passed to this function.
|
|
|
|
*/
|
2002-06-12 05:47:34 +04:00
|
|
|
#define KEY_SETSECSPIDX(s, d, ps, pd, ulp, idx) \
|
2000-01-31 17:18:52 +03:00
|
|
|
do { \
|
|
|
|
bzero((idx), sizeof(struct secpolicyindex)); \
|
|
|
|
(idx)->prefs = (ps); \
|
|
|
|
(idx)->prefd = (pd); \
|
|
|
|
(idx)->ul_proto = (ulp); \
|
|
|
|
bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
|
|
|
|
bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
|
2002-11-02 10:30:55 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* set parameters into secasindex buffer.
|
|
|
|
* Must allocate secasindex buffer before calling this function.
|
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
#define KEY_SETSECASIDX(p, m, r, s, d, idx) \
|
2000-01-31 17:18:52 +03:00
|
|
|
do { \
|
|
|
|
bzero((idx), sizeof(struct secasindex)); \
|
|
|
|
(idx)->proto = (p); \
|
2000-06-12 14:40:37 +04:00
|
|
|
(idx)->mode = (m); \
|
2000-07-01 05:01:34 +04:00
|
|
|
(idx)->reqid = (r); \
|
2000-01-31 17:18:52 +03:00
|
|
|
bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
|
|
|
|
bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
|
2002-11-02 10:30:55 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* key statistics */
|
|
|
|
struct _keystat {
|
|
|
|
u_long getspi_count; /* the avarage of count to try to get new SPI */
|
|
|
|
} keystat;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
struct sadb_msghdr {
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
struct sadb_ext *ext[SADB_EXT_MAX + 1];
|
|
|
|
int extoff[SADB_EXT_MAX + 1];
|
|
|
|
int extlen[SADB_EXT_MAX + 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct secasvar *key_allocsa_policy __P((struct secasindex *));
|
|
|
|
static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
|
2003-09-07 19:59:36 +04:00
|
|
|
static void key_delsav __P((struct secasvar *));
|
2000-06-12 14:40:37 +04:00
|
|
|
static void key_delsp __P((struct secpolicy *));
|
2002-06-12 05:47:34 +04:00
|
|
|
static struct secpolicy *key_getsp __P((struct secpolicyindex *, int));
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
static struct secpolicy *key_getspbytag __P((u_int16_t, int));
|
|
|
|
#endif
|
2000-01-31 17:18:52 +03:00
|
|
|
static u_int32_t key_newreqid __P((void));
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *key_gather_mbuf __P((struct mbuf *,
|
|
|
|
const struct sadb_msghdr *, int, int, ...));
|
|
|
|
static int key_spdadd __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_spddelete __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_spddelete2 __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_spdget __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_spdflush __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_spddump __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
2003-09-12 11:38:10 +04:00
|
|
|
static struct mbuf *key_setspddump __P((int *));
|
2000-06-12 14:40:37 +04:00
|
|
|
static u_int key_getspreqmsglen __P((struct secpolicy *));
|
2002-05-19 12:22:12 +04:00
|
|
|
static int key_spdexpire __P((struct secpolicy *));
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct secashead *key_newsah __P((struct secasindex *));
|
|
|
|
static void key_delsah __P((struct secashead *));
|
|
|
|
static struct secasvar *key_newsav __P((struct mbuf *,
|
|
|
|
const struct sadb_msghdr *, struct secashead *, int *));
|
|
|
|
static struct secashead *key_getsah __P((struct secasindex *));
|
|
|
|
static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
|
2003-09-07 19:59:36 +04:00
|
|
|
static void key_setspi __P((struct secasvar *, u_int32_t));
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
|
|
|
|
static int key_setsaval __P((struct secasvar *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_mature __P((struct secasvar *));
|
|
|
|
static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
|
|
|
|
u_int8_t, u_int32_t, u_int32_t));
|
|
|
|
static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
|
|
|
|
u_int32_t, pid_t, u_int16_t));
|
|
|
|
static struct mbuf *key_setsadbsa __P((struct secasvar *));
|
|
|
|
static struct mbuf *key_setsadbaddr __P((u_int16_t,
|
|
|
|
struct sockaddr *, u_int8_t, u_int16_t));
|
2000-07-01 05:01:34 +04:00
|
|
|
#if 0
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *key_setsadbident __P((u_int16_t, u_int16_t, caddr_t,
|
|
|
|
int, u_int64_t));
|
2000-07-01 05:01:34 +04:00
|
|
|
#endif
|
2001-08-02 16:10:14 +04:00
|
|
|
static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
static struct mbuf *key_setsadbxtag __P((u_int16_t));
|
|
|
|
#endif
|
2002-05-19 12:22:12 +04:00
|
|
|
static struct mbuf *key_setsadblifetime __P((u_int16_t, u_int32_t,
|
|
|
|
u_int64_t, u_int64_t, u_int64_t));
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
|
|
|
|
u_int32_t));
|
|
|
|
static void *key_newbuf __P((const void *, u_int));
|
2002-06-12 07:46:16 +04:00
|
|
|
static int key_ismyaddr __P((struct sockaddr *));
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifdef INET6
|
2000-06-12 14:40:37 +04:00
|
|
|
static int key_ismyaddr6 __P((struct sockaddr_in6 *));
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
2000-01-31 17:18:52 +03:00
|
|
|
static int key_cmpsaidx_exactly
|
2000-06-12 14:40:37 +04:00
|
|
|
__P((struct secasindex *, struct secasindex *));
|
2000-01-31 17:18:52 +03:00
|
|
|
static int key_cmpsaidx_withmode
|
2000-06-12 14:40:37 +04:00
|
|
|
__P((struct secasindex *, struct secasindex *));
|
2000-07-01 05:01:34 +04:00
|
|
|
static int key_cmpsaidx_withoutmode
|
|
|
|
__P((struct secasindex *, struct secasindex *));
|
2000-06-12 14:40:37 +04:00
|
|
|
static int key_sockaddrcmp __P((struct sockaddr *, struct sockaddr *, int));
|
|
|
|
static int key_bbcmp __P((caddr_t, caddr_t, u_int));
|
|
|
|
static void key_srandom __P((void));
|
2002-06-12 07:46:16 +04:00
|
|
|
static u_long key_random __P((void));
|
2000-06-12 14:40:37 +04:00
|
|
|
static u_int16_t key_satype2proto __P((u_int8_t));
|
|
|
|
static u_int8_t key_proto2satype __P((u_int16_t));
|
|
|
|
|
|
|
|
static int key_getspi __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
|
|
|
|
struct secasindex *));
|
|
|
|
static int key_update __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifdef IPSEC_DOSEQCHECK
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
|
|
|
|
#endif
|
|
|
|
static int key_add __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_setident __P((struct secashead *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_delete __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_get __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
|
2000-09-22 20:55:04 +04:00
|
|
|
static void key_getcomb_setlifetime __P((struct sadb_comb *));
|
2000-06-15 17:44:22 +04:00
|
|
|
#ifdef IPSEC_ESP
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *key_getcomb_esp __P((void));
|
2000-06-15 17:44:22 +04:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *key_getcomb_ah __P((void));
|
2000-09-20 04:08:42 +04:00
|
|
|
static struct mbuf *key_getcomb_ipcomp __P((void));
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *key_getprop __P((const struct secasindex *));
|
|
|
|
|
|
|
|
static int key_acquire __P((struct secasindex *, struct secpolicy *));
|
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
|
|
|
static struct secacq *key_newacq __P((struct secasindex *));
|
|
|
|
static struct secacq *key_getacq __P((struct secasindex *));
|
|
|
|
static struct secacq *key_getacqbyseq __P((u_int32_t));
|
|
|
|
#endif
|
|
|
|
static struct secspacq *key_newspacq __P((struct secpolicyindex *));
|
|
|
|
static struct secspacq *key_getspacq __P((struct secpolicyindex *));
|
|
|
|
static int key_acquire2 __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_register __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_expire __P((struct secasvar *));
|
|
|
|
static int key_flush __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_dump __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
2003-09-12 11:38:10 +04:00
|
|
|
static struct mbuf *key_setdump __P((u_int8_t, int *));
|
2000-06-12 14:40:37 +04:00
|
|
|
static int key_promisc __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *));
|
|
|
|
static int key_senderror __P((struct socket *, struct mbuf *, int));
|
|
|
|
static int key_validate_ext __P((const struct sadb_ext *, int));
|
|
|
|
static int key_align __P((struct mbuf *, struct sadb_msghdr *));
|
1999-06-28 10:36:47 +04:00
|
|
|
#if 0
|
|
|
|
static const char *key_getfqdn __P((void));
|
|
|
|
static const char *key_getuserfqdn __P((void));
|
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
|
2002-05-19 12:22:12 +04:00
|
|
|
static void key_sp_dead __P((struct secpolicy *));
|
|
|
|
static void key_sp_unlink __P((struct secpolicy *));
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *key_alloc_mbuf __P((int));
|
2000-03-23 10:01:25 +03:00
|
|
|
struct callout key_timehandler_ch;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* %%% IPsec policy management */
|
|
|
|
/*
|
|
|
|
* allocating a SP for OUTBOUND or INBOUND packet.
|
|
|
|
* Must call key_freesp() later.
|
|
|
|
* OUT: NULL: not found
|
|
|
|
* others: found and return the pointer.
|
|
|
|
*/
|
|
|
|
struct secpolicy *
|
2003-09-12 11:38:10 +04:00
|
|
|
key_allocsp(tag, spidx, dir)
|
|
|
|
u_int16_t tag;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secpolicyindex *spidx;
|
|
|
|
u_int dir;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
2000-01-31 17:18:52 +03:00
|
|
|
int s;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* check direction */
|
1999-06-28 10:36:47 +04:00
|
|
|
switch (dir) {
|
2000-01-31 17:18:52 +03:00
|
|
|
case IPSEC_DIR_INBOUND:
|
|
|
|
case IPSEC_DIR_OUTBOUND:
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
default:
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_allocsp: Invalid direction is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get a SP entry */
|
2000-01-31 17:18:52 +03:00
|
|
|
s = splsoftnet(); /*called from softclock()*/
|
2003-09-12 11:38:10 +04:00
|
|
|
if (spidx) {
|
2003-08-22 09:48:27 +04:00
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_DATA,
|
2003-09-12 11:38:10 +04:00
|
|
|
printf("*** objects\n");
|
|
|
|
kdebug_secpolicyindex(spidx));
|
|
|
|
}
|
2003-08-22 09:48:27 +04:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
LIST_FOREACH(sp, &sptree[dir], chain) {
|
1999-07-31 22:41:15 +04:00
|
|
|
if (sp->state == IPSEC_SPSTATE_DEAD)
|
|
|
|
continue;
|
2003-09-12 11:38:10 +04:00
|
|
|
if (!sp->spidx) {
|
|
|
|
if (!tag)
|
|
|
|
continue;
|
|
|
|
if (sp->tag == tag)
|
|
|
|
goto found;
|
|
|
|
} else {
|
|
|
|
if (!spidx)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_DATA,
|
|
|
|
printf("*** in SPD\n");
|
|
|
|
kdebug_secpolicyindex(sp->spidx));
|
|
|
|
|
|
|
|
if (key_cmpspidx_withmask(sp->spidx, spidx))
|
|
|
|
goto found;
|
|
|
|
}
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
splx(s);
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
found:
|
2000-01-31 17:18:52 +03:00
|
|
|
/* sanity check */
|
2002-06-12 05:47:34 +04:00
|
|
|
KEY_CHKSPDIR(sp->dir, dir, "key_allocsp");
|
2000-01-31 17:18:52 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* found a SPD entry */
|
2002-05-19 12:22:12 +04:00
|
|
|
sp->lastused = time.tv_sec;
|
1999-06-28 10:36:47 +04:00
|
|
|
sp->refcnt++;
|
2000-01-31 17:18:52 +03:00
|
|
|
splx(s);
|
1999-06-28 10:36:47 +04:00
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
|
|
|
|
printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
|
|
|
|
sp->refcnt, sp));
|
|
|
|
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-07-22 15:12:15 +04:00
|
|
|
* allocating an SA entry for an *OUTBOUND* packet.
|
2002-05-19 12:22:12 +04:00
|
|
|
* checking each request entries in SP, and acquire an SA if need.
|
1999-06-28 10:36:47 +04:00
|
|
|
* OUT: 0: there are valid requests.
|
|
|
|
* ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
|
|
|
|
*/
|
|
|
|
int
|
2000-01-31 17:18:52 +03:00
|
|
|
key_checkrequest(isr, saidx)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct ipsecrequest *isr;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasindex *saidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
u_int level;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (isr == NULL || saidx == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_checkrequest: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* check mode */
|
|
|
|
switch (saidx->mode) {
|
1999-06-28 10:36:47 +04:00
|
|
|
case IPSEC_MODE_TRANSPORT:
|
|
|
|
case IPSEC_MODE_TUNNEL:
|
|
|
|
break;
|
2000-01-31 17:18:52 +03:00
|
|
|
case IPSEC_MODE_ANY:
|
1999-06-28 10:36:47 +04:00
|
|
|
default:
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_checkrequest: Invalid policy defined.");
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get current level */
|
2002-06-12 05:47:34 +04:00
|
|
|
level = ipsec_get_reqlevel(isr, saidx->src.ss_family);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-03-05 08:42:33 +03:00
|
|
|
#if 0
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* We do allocate new SA only if the state of SA in the holder is
|
|
|
|
* SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
|
|
|
|
*/
|
|
|
|
if (isr->sav != NULL) {
|
|
|
|
if (isr->sav->sah == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_checkrequest: sah is null.");
|
2003-09-07 19:59:36 +04:00
|
|
|
if (isr->sav ==
|
|
|
|
LIST_FIRST(&isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
|
2000-01-31 17:18:52 +03:00
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
|
|
|
|
printf("DP checkrequest calls free SA:%p\n",
|
|
|
|
isr->sav));
|
|
|
|
key_freesav(isr->sav);
|
|
|
|
isr->sav = NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-03-05 08:42:33 +03:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* we free any SA stashed in the IPsec request because a different
|
|
|
|
* SA may be involved each time this request is checked, either
|
|
|
|
* because new SAs are being configured, or this request is
|
|
|
|
* associated with an unconnected datagram socket, or this request
|
|
|
|
* is associated with a system default policy.
|
|
|
|
*
|
|
|
|
* The operation may have negative impact to performance. We may
|
|
|
|
* want to check cached SA carefully, rather than picking new SA
|
|
|
|
* every time.
|
|
|
|
*/
|
|
|
|
if (isr->sav != NULL) {
|
|
|
|
key_freesav(isr->sav);
|
|
|
|
isr->sav = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-03-05 08:42:33 +03:00
|
|
|
/*
|
|
|
|
* new SA allocation if no SA found.
|
|
|
|
* key_allocsa_policy should allocate the oldest SA available.
|
|
|
|
* See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
if (isr->sav == NULL)
|
|
|
|
isr->sav = key_allocsa_policy(saidx);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* When there is SA. */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (isr->sav != NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* there is no SA */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((error = key_acquire(saidx, isr->sp)) != 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
/* XXX What should I do ? */
|
|
|
|
ipseclog((LOG_DEBUG, "key_checkrequest: error %d returned "
|
|
|
|
"from key_acquire.\n", error));
|
1999-06-28 10:36:47 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return level == IPSEC_LEVEL_REQUIRE ? ENOENT : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* allocating a SA for policy entry from SAD.
|
|
|
|
* NOTE: searching SAD of aliving state.
|
1999-06-28 10:36:47 +04:00
|
|
|
* OUT: NULL: not found.
|
|
|
|
* others: found and return the pointer.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static struct secasvar *
|
|
|
|
key_allocsa_policy(saidx)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct secasindex *saidx;
|
2000-01-31 17:18:52 +03:00
|
|
|
{
|
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int stateidx, state;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
if (sah->state == SADB_SASTATE_DEAD)
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
2000-01-31 17:18:52 +03:00
|
|
|
if (key_cmpsaidx_withmode(&sah->saidx, saidx))
|
1999-07-31 22:41:15 +04:00
|
|
|
goto found;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
found:
|
|
|
|
|
|
|
|
/* search valid state */
|
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_valid);
|
|
|
|
stateidx++) {
|
|
|
|
|
|
|
|
state = saorder_state_valid[stateidx];
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
sav = key_do_allocsa_policy(sah, state);
|
|
|
|
if (sav != NULL)
|
|
|
|
return sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* searching SAD with direction, protocol, mode and state.
|
1999-06-28 10:36:47 +04:00
|
|
|
* called by key_allocsa_policy().
|
|
|
|
* OUT:
|
|
|
|
* NULL : not found
|
|
|
|
* others : found, pointer to a SA.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static struct secasvar *
|
|
|
|
key_do_allocsa_policy(sah, state)
|
|
|
|
struct secashead *sah;
|
|
|
|
u_int state;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav, *candidate;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* initilize */
|
|
|
|
candidate = NULL;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_FOREACH(sav, &sah->savtree[state], chain) {
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
KEY_CHKSASTATE(sav->state, state, "key_do_allocsa_policy");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* initialize */
|
|
|
|
if (candidate == NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
candidate = sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Which SA is the better ? */
|
|
|
|
|
|
|
|
/* sanity check 2 */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (candidate->lft_c == NULL || sav->lft_c == NULL)
|
|
|
|
panic("key_do_allocsa_policy: "
|
2002-09-27 19:35:29 +04:00
|
|
|
"lifetime_current is NULL.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* XXX What the best method is to compare ? */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (candidate->lft_c->sadb_lifetime_addtime >
|
|
|
|
sav->lft_c->sadb_lifetime_addtime) {
|
|
|
|
candidate = sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (candidate) {
|
1999-06-28 10:36:47 +04:00
|
|
|
candidate->refcnt++;
|
2000-01-31 17:18:52 +03:00
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
|
|
|
|
printf("DP allocsa_policy cause "
|
|
|
|
"refcnt++:%d SA:%p\n",
|
|
|
|
candidate->refcnt, candidate));
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocating a SA entry for a *INBOUND* packet.
|
2000-01-31 17:18:52 +03:00
|
|
|
* Must call key_freesav() later.
|
|
|
|
* OUT: positive: pointer to a sav.
|
2002-01-31 09:35:25 +03:00
|
|
|
* NULL: not found, or error occured.
|
2000-01-31 17:18:52 +03:00
|
|
|
*
|
|
|
|
* In the comparison, source address will be ignored for RFC2401 conformance.
|
|
|
|
* To quote, from section 4.1:
|
|
|
|
* A security association is uniquely identified by a triple consisting
|
|
|
|
* of a Security Parameter Index (SPI), an IP Destination Address, and a
|
|
|
|
* security protocol (AH or ESP) identifier.
|
|
|
|
* Note that, however, we do need to keep source address in IPsec SA.
|
2000-06-12 14:40:37 +04:00
|
|
|
* IKE specification and PF_KEY specification do assume that we
|
2000-01-31 17:18:52 +03:00
|
|
|
* keep source address in IPsec SA. We see a tricky situation here.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *
|
1999-06-28 10:36:47 +04:00
|
|
|
key_allocsa(family, src, dst, proto, spi)
|
|
|
|
u_int family, proto;
|
|
|
|
caddr_t src, dst;
|
|
|
|
u_int32_t spi;
|
|
|
|
{
|
2003-09-08 00:41:27 +04:00
|
|
|
struct secasvar *sav, *match;
|
|
|
|
u_int stateidx, state, tmpidx, matchidx;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_in6 sin6;
|
2000-01-31 17:18:52 +03:00
|
|
|
int s;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (src == NULL || dst == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_allocsa: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* searching SAD.
|
|
|
|
* XXX: to be checked internal IP header somewhere. Also when
|
|
|
|
* IPsec tunnel packet is received. But ESP tunnel mode is
|
1999-06-28 10:36:47 +04:00
|
|
|
* encrypted so we can't check internal IP header.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
s = splsoftnet(); /*called from softclock()*/
|
2003-09-07 19:59:36 +04:00
|
|
|
/* search valid state */
|
2003-09-08 00:41:27 +04:00
|
|
|
match = NULL;
|
|
|
|
matchidx = _ARRAYLEN(saorder_state_valid);
|
2003-09-14 11:30:32 +04:00
|
|
|
LIST_FOREACH(sav, &spihash[SPIHASH(spi)], spihash) {
|
2003-09-08 00:41:27 +04:00
|
|
|
if (sav->spi != spi)
|
|
|
|
continue;
|
|
|
|
if (proto != sav->sah->saidx.proto)
|
|
|
|
continue;
|
|
|
|
if (family != sav->sah->saidx.src.ss_family ||
|
|
|
|
family != sav->sah->saidx.dst.ss_family)
|
|
|
|
continue;
|
|
|
|
tmpidx = _ARRAYLEN(saorder_state_valid);
|
|
|
|
for (stateidx = 0; stateidx < matchidx; stateidx++) {
|
|
|
|
state = saorder_state_valid[stateidx];
|
|
|
|
if (sav->state == state) {
|
|
|
|
tmpidx = stateidx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tmpidx >= matchidx)
|
|
|
|
continue;
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
#if 0 /* don't check src */
|
2003-09-08 00:41:27 +04:00
|
|
|
/* check src address */
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
bzero(&sin, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_len = sizeof(sin);
|
|
|
|
bcopy(src, &sin.sin_addr,
|
|
|
|
sizeof(sin.sin_addr));
|
|
|
|
if (key_sockaddrcmp((struct sockaddr*)&sin,
|
|
|
|
(struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
|
2003-09-07 19:59:36 +04:00
|
|
|
continue;
|
2003-09-08 00:41:27 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
bzero(&sin6, sizeof(sin6));
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_len = sizeof(sin6);
|
|
|
|
bcopy(src, &sin6.sin6_addr,
|
|
|
|
sizeof(sin6.sin6_addr));
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
|
|
|
|
/* kame fake scopeid */
|
|
|
|
sin6.sin6_scope_id =
|
|
|
|
ntohs(sin6.sin6_addr.s6_addr16[1]);
|
|
|
|
sin6.sin6_addr.s6_addr16[1] = 0;
|
2003-09-07 19:59:36 +04:00
|
|
|
}
|
2003-09-08 00:41:27 +04:00
|
|
|
if (key_sockaddrcmp((struct sockaddr*)&sin6,
|
|
|
|
(struct sockaddr *)&sav->sah->saidx.src, 0) != 0)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ipseclog((LOG_DEBUG, "key_allocsa: "
|
|
|
|
"unknown address family=%d.\n",
|
|
|
|
family));
|
|
|
|
continue;
|
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
#endif
|
2003-09-08 00:41:27 +04:00
|
|
|
/* check dst address */
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
bzero(&sin, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_len = sizeof(sin);
|
|
|
|
bcopy(dst, &sin.sin_addr,
|
|
|
|
sizeof(sin.sin_addr));
|
|
|
|
if (key_sockaddrcmp((struct sockaddr*)&sin,
|
|
|
|
(struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
|
2003-09-07 19:59:36 +04:00
|
|
|
continue;
|
|
|
|
|
2003-09-08 00:41:27 +04:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
bzero(&sin6, sizeof(sin6));
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_len = sizeof(sin6);
|
|
|
|
bcopy(dst, &sin6.sin6_addr,
|
|
|
|
sizeof(sin6.sin6_addr));
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
|
|
|
|
/* kame fake scopeid */
|
|
|
|
sin6.sin6_scope_id =
|
|
|
|
ntohs(sin6.sin6_addr.s6_addr16[1]);
|
|
|
|
sin6.sin6_addr.s6_addr16[1] = 0;
|
|
|
|
}
|
|
|
|
if (key_sockaddrcmp((struct sockaddr*)&sin6,
|
|
|
|
(struct sockaddr *)&sav->sah->saidx.dst, 0) != 0)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ipseclog((LOG_DEBUG, "key_allocsa: "
|
|
|
|
"unknown address family=%d.\n", family));
|
|
|
|
continue;
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
2003-09-08 00:41:27 +04:00
|
|
|
|
|
|
|
match = sav;
|
|
|
|
matchidx = tmpidx;
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-08 00:41:27 +04:00
|
|
|
if (match)
|
|
|
|
goto found;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* not found */
|
2000-01-31 17:18:52 +03:00
|
|
|
splx(s);
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
found:
|
2003-09-08 00:41:27 +04:00
|
|
|
match->refcnt++;
|
2000-01-31 17:18:52 +03:00
|
|
|
splx(s);
|
1999-06-28 10:36:47 +04:00
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
|
|
|
|
printf("DP allocsa cause refcnt++:%d SA:%p\n",
|
2003-09-08 00:41:27 +04:00
|
|
|
match->refcnt, match));
|
|
|
|
return match;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must be called after calling key_allocsp().
|
|
|
|
* For both the packet without socket and key_freeso().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
key_freesp(sp)
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
|
|
|
/* sanity check */
|
|
|
|
if (sp == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_freesp: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
sp->refcnt--;
|
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
|
|
|
|
printf("DP freesp cause refcnt--:%d SP:%p\n",
|
|
|
|
sp->refcnt, sp));
|
|
|
|
|
|
|
|
if (sp->refcnt == 0)
|
|
|
|
key_delsp(sp);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must be called after calling key_allocsa().
|
|
|
|
* This function is called by key_freesp() to free some SA allocated
|
|
|
|
* for a policy.
|
|
|
|
*/
|
|
|
|
void
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesav(sav)
|
|
|
|
struct secasvar *sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_freesav: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->refcnt--;
|
1999-06-28 10:36:47 +04:00
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
|
2000-09-20 23:55:05 +04:00
|
|
|
printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->refcnt, sav, (u_int32_t)ntohl(sav->spi)));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
if (sav->refcnt > 0)
|
|
|
|
return;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
key_delsav(sav);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
key_delsav(sav)
|
|
|
|
struct secasvar *sav;
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (sav == NULL)
|
|
|
|
panic("key_delsav: NULL pointer is passed.");
|
|
|
|
|
|
|
|
if (sav->refcnt > 0)
|
|
|
|
panic("key_delsav: called with positive refcnt");
|
|
|
|
|
|
|
|
s = splsoftnet();
|
|
|
|
|
|
|
|
#ifdef KERNFS
|
|
|
|
kernfs_revoke_sa(sav);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (__LIST_CHAINED(sav))
|
|
|
|
LIST_REMOVE(sav, chain);
|
|
|
|
|
|
|
|
if (sav->spihash.le_prev || sav->spihash.le_next)
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
|
|
|
|
if (sav->key_auth != NULL) {
|
|
|
|
bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
|
|
|
|
KFREE(sav->key_auth);
|
|
|
|
sav->key_auth = NULL;
|
|
|
|
}
|
|
|
|
if (sav->key_enc != NULL) {
|
|
|
|
bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
|
|
|
|
KFREE(sav->key_enc);
|
|
|
|
sav->key_enc = NULL;
|
|
|
|
}
|
|
|
|
if (sav->sched) {
|
|
|
|
bzero(sav->sched, sav->schedlen);
|
|
|
|
KFREE(sav->sched);
|
|
|
|
sav->sched = NULL;
|
|
|
|
}
|
|
|
|
if (sav->replay != NULL) {
|
|
|
|
keydb_delsecreplay(sav->replay);
|
|
|
|
sav->replay = NULL;
|
|
|
|
}
|
|
|
|
if (sav->lft_c != NULL) {
|
|
|
|
KFREE(sav->lft_c);
|
|
|
|
sav->lft_c = NULL;
|
|
|
|
}
|
|
|
|
if (sav->lft_h != NULL) {
|
|
|
|
KFREE(sav->lft_h);
|
|
|
|
sav->lft_h = NULL;
|
|
|
|
}
|
|
|
|
if (sav->lft_s != NULL) {
|
|
|
|
KFREE(sav->lft_s);
|
|
|
|
sav->lft_s = NULL;
|
|
|
|
}
|
|
|
|
if (sav->iv != NULL) {
|
|
|
|
KFREE(sav->iv);
|
|
|
|
sav->iv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
keydb_delsecasvar(sav);
|
|
|
|
|
|
|
|
splx(s);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* %%% SPD management */
|
|
|
|
/*
|
|
|
|
* free security policy entry.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
key_delsp(sp)
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
int s;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* sanity check */
|
|
|
|
if (sp == NULL)
|
2002-05-19 12:22:12 +04:00
|
|
|
panic("key_delsp: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
if (sp->refcnt > 0)
|
2002-05-19 12:22:12 +04:00
|
|
|
panic("key_delsp: called with positive refcnt");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
s = splsoftnet(); /*called from softclock()*/
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-08 10:51:53 +04:00
|
|
|
#ifdef KERNFS
|
|
|
|
kernfs_revoke_sp(sp);
|
|
|
|
#endif
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct ipsecrequest *isr = sp->req, *nextisr;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
while (isr != NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
if (isr->sav != NULL) {
|
1999-06-28 10:36:47 +04:00
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
|
|
|
|
printf("DP delsp calls free SA:%p\n",
|
2000-01-31 17:18:52 +03:00
|
|
|
isr->sav));
|
|
|
|
key_freesav(isr->sav);
|
|
|
|
isr->sav = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextisr = isr->next;
|
1999-06-28 10:36:47 +04:00
|
|
|
KFREE(isr);
|
2000-01-31 17:18:52 +03:00
|
|
|
isr = nextisr;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
keydb_delsecpolicy(sp);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
splx(s);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
return;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* search SPD
|
|
|
|
* OUT: NULL : not found
|
|
|
|
* others : found, pointer to a SP.
|
|
|
|
*/
|
|
|
|
static struct secpolicy *
|
2002-06-12 05:47:34 +04:00
|
|
|
key_getsp(spidx, dir)
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secpolicyindex *spidx;
|
2002-06-12 05:47:34 +04:00
|
|
|
int dir;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (spidx == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_getsp: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2002-06-12 05:47:34 +04:00
|
|
|
LIST_FOREACH(sp, &sptree[dir], chain) {
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sp->state == IPSEC_SPSTATE_DEAD)
|
1999-07-31 22:41:15 +04:00
|
|
|
continue;
|
2003-09-12 11:38:10 +04:00
|
|
|
if (!sp->spidx)
|
|
|
|
continue;
|
2002-06-12 05:47:34 +04:00
|
|
|
if (key_cmpspidx_exactly(spidx, sp->spidx)) {
|
2000-01-31 17:18:52 +03:00
|
|
|
sp->refcnt++;
|
1999-07-31 22:41:15 +04:00
|
|
|
return sp;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
static struct secpolicy *
|
|
|
|
key_getspbytag(tag, dir)
|
|
|
|
u_int16_t tag;
|
|
|
|
int dir;
|
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
|
|
|
|
|
|
|
LIST_FOREACH(sp, &sptree[dir], chain) {
|
|
|
|
if (sp->state == IPSEC_SPSTATE_DEAD)
|
|
|
|
continue;
|
|
|
|
if (sp->spidx)
|
|
|
|
continue;
|
|
|
|
if (sp->tag == tag) {
|
|
|
|
sp->refcnt++;
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/*
|
|
|
|
* get SP by index.
|
|
|
|
* OUT: NULL : not found
|
|
|
|
* others : found, pointer to a SP.
|
|
|
|
*/
|
2003-09-08 10:51:53 +04:00
|
|
|
struct secpolicy *
|
2000-06-12 14:40:37 +04:00
|
|
|
key_getspbyid(id)
|
|
|
|
u_int32_t id;
|
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
|
|
|
|
2002-06-12 21:56:45 +04:00
|
|
|
TAILQ_FOREACH(sp, &sptailq, tailq) {
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sp->id == id) {
|
|
|
|
sp->refcnt++;
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
struct secpolicy *
|
2003-08-22 10:22:21 +04:00
|
|
|
key_newsp(id)
|
|
|
|
u_int32_t id;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-08-22 10:22:21 +04:00
|
|
|
struct secpolicy *newsp = NULL, *sp;
|
|
|
|
u_int32_t newid;
|
|
|
|
|
|
|
|
if (id > IPSEC_MANUAL_POLICYID_MAX) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_newsp: policy_id=%u range "
|
|
|
|
"violation, updated by kernel.\n", id));
|
|
|
|
id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == 0) {
|
|
|
|
if ((newid = keydb_newspid()) == 0) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_newsp: new policy_id allocation failed."));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sp = key_getspbyid(id);
|
|
|
|
if (sp != NULL) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_newsp: policy_id(%u) has been used.\n", id));
|
|
|
|
key_freesp(sp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
newid = id;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
newsp = keydb_newsecpolicy();
|
|
|
|
if (!newsp)
|
|
|
|
return newsp;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-08-22 10:22:21 +04:00
|
|
|
newsp->id = newid;
|
1999-06-28 10:36:47 +04:00
|
|
|
newsp->refcnt = 1;
|
|
|
|
newsp->req = NULL;
|
|
|
|
|
|
|
|
return newsp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create secpolicy structure from sadb_x_policy structure.
|
2000-01-31 17:18:52 +03:00
|
|
|
* NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
|
1999-06-28 10:36:47 +04:00
|
|
|
* so must be set properly later.
|
|
|
|
*/
|
|
|
|
struct secpolicy *
|
2000-01-31 17:18:52 +03:00
|
|
|
key_msg2sp(xpl0, len, error)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sadb_x_policy *xpl0;
|
2000-01-31 17:18:52 +03:00
|
|
|
size_t len;
|
|
|
|
int *error;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct secpolicy *newsp;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (xpl0 == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_msg2sp: NULL pointer was passed.");
|
2000-01-31 17:18:52 +03:00
|
|
|
if (len < sizeof(*xpl0))
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_msg2sp: invalid length.");
|
2000-01-31 17:18:52 +03:00
|
|
|
if (len != PFKEY_EXTLEN(xpl0)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_msg2sp: Invalid msg length.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-08-22 10:22:21 +04:00
|
|
|
if ((newsp = key_newsp(xpl0->sadb_x_policy_id)) == NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
*error = ENOBUFS;
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
2002-06-12 05:47:34 +04:00
|
|
|
newsp->dir = xpl0->sadb_x_policy_dir;
|
2000-01-31 17:18:52 +03:00
|
|
|
newsp->policy = xpl0->sadb_x_policy_type;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* check policy */
|
|
|
|
switch (xpl0->sadb_x_policy_type) {
|
|
|
|
case IPSEC_POLICY_DISCARD:
|
|
|
|
case IPSEC_POLICY_NONE:
|
|
|
|
case IPSEC_POLICY_ENTRUST:
|
|
|
|
case IPSEC_POLICY_BYPASS:
|
|
|
|
newsp->req = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPSEC_POLICY_IPSEC:
|
|
|
|
{
|
|
|
|
int tlen;
|
|
|
|
struct sadb_x_ipsecrequest *xisr;
|
|
|
|
struct ipsecrequest **p_isr = &newsp->req;
|
|
|
|
|
|
|
|
/* validity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_msg2sp: Invalid msg length.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
key_freesp(newsp);
|
2000-01-31 17:18:52 +03:00
|
|
|
*error = EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
|
|
|
|
xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
while (tlen > 0) {
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* length check */
|
|
|
|
if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_msg2sp: "
|
|
|
|
"invalid ipsecrequest length.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesp(newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate request buffer */
|
1999-06-28 10:36:47 +04:00
|
|
|
KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
|
|
|
|
if ((*p_isr) == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_msg2sp: No more memory.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
key_freesp(newsp);
|
2000-01-31 17:18:52 +03:00
|
|
|
*error = ENOBUFS;
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
bzero(*p_isr, sizeof(**p_isr));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* set values */
|
1999-06-28 10:36:47 +04:00
|
|
|
(*p_isr)->next = NULL;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
switch (xisr->sadb_x_ipsecrequest_proto) {
|
|
|
|
case IPPROTO_ESP:
|
|
|
|
case IPPROTO_AH:
|
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
break;
|
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_msg2sp: invalid proto type=%u\n",
|
|
|
|
xisr->sadb_x_ipsecrequest_proto));
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesp(newsp);
|
|
|
|
*error = EPROTONOSUPPORT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
(*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
switch (xisr->sadb_x_ipsecrequest_mode) {
|
|
|
|
case IPSEC_MODE_TRANSPORT:
|
|
|
|
case IPSEC_MODE_TUNNEL:
|
|
|
|
break;
|
|
|
|
case IPSEC_MODE_ANY:
|
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_msg2sp: invalid mode=%u\n",
|
|
|
|
xisr->sadb_x_ipsecrequest_mode));
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesp(newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
(*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
switch (xisr->sadb_x_ipsecrequest_level) {
|
|
|
|
case IPSEC_LEVEL_DEFAULT:
|
|
|
|
case IPSEC_LEVEL_USE:
|
|
|
|
case IPSEC_LEVEL_REQUIRE:
|
|
|
|
break;
|
|
|
|
case IPSEC_LEVEL_UNIQUE:
|
|
|
|
/* validity check */
|
|
|
|
/*
|
|
|
|
* If range violation of reqid, kernel will
|
|
|
|
* update it, don't refuse it.
|
|
|
|
*/
|
|
|
|
if (xisr->sadb_x_ipsecrequest_reqid
|
|
|
|
> IPSEC_MANUAL_REQID_MAX) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
2002-06-27 18:39:45 +04:00
|
|
|
"key_msg2sp: reqid=%u range "
|
2002-01-31 09:35:25 +03:00
|
|
|
"violation, updated by kernel.\n",
|
|
|
|
xisr->sadb_x_ipsecrequest_reqid));
|
2000-01-31 17:18:52 +03:00
|
|
|
xisr->sadb_x_ipsecrequest_reqid = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* allocate new reqid id if reqid is zero. */
|
|
|
|
if (xisr->sadb_x_ipsecrequest_reqid == 0) {
|
|
|
|
u_int32_t reqid;
|
|
|
|
if ((reqid = key_newreqid()) == 0) {
|
|
|
|
key_freesp(newsp);
|
|
|
|
*error = ENOBUFS;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
(*p_isr)->saidx.reqid = reqid;
|
|
|
|
xisr->sadb_x_ipsecrequest_reqid = reqid;
|
|
|
|
} else {
|
|
|
|
/* set it for manual keying. */
|
|
|
|
(*p_isr)->saidx.reqid =
|
|
|
|
xisr->sadb_x_ipsecrequest_reqid;
|
|
|
|
}
|
|
|
|
break;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_msg2sp: invalid level=%u\n",
|
|
|
|
xisr->sadb_x_ipsecrequest_level));
|
1999-06-28 10:36:47 +04:00
|
|
|
key_freesp(newsp);
|
2000-01-31 17:18:52 +03:00
|
|
|
*error = EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
(*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
|
|
|
|
|
|
|
|
/* set IP addresses if there */
|
|
|
|
if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
|
|
|
|
struct sockaddr *paddr;
|
|
|
|
|
|
|
|
paddr = (struct sockaddr *)(xisr + 1);
|
|
|
|
|
|
|
|
/* validity check */
|
|
|
|
if (paddr->sa_len
|
|
|
|
> sizeof((*p_isr)->saidx.src)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
|
|
|
|
"address length.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesp(newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bcopy(paddr, &(*p_isr)->saidx.src,
|
|
|
|
paddr->sa_len);
|
|
|
|
|
|
|
|
paddr = (struct sockaddr *)((caddr_t)paddr
|
|
|
|
+ paddr->sa_len);
|
|
|
|
|
|
|
|
/* validity check */
|
|
|
|
if (paddr->sa_len
|
|
|
|
> sizeof((*p_isr)->saidx.dst)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_msg2sp: invalid request "
|
|
|
|
"address length.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesp(newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bcopy(paddr, &(*p_isr)->saidx.dst,
|
|
|
|
paddr->sa_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*p_isr)->sav = NULL;
|
|
|
|
(*p_isr)->sp = newsp;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* initialization for the next. */
|
|
|
|
p_isr = &(*p_isr)->next;
|
|
|
|
tlen -= xisr->sadb_x_ipsecrequest_len;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* validity check */
|
|
|
|
if (tlen < 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_msg2sp: becoming tlen < 0.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesp(newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
|
|
|
|
+ xisr->sadb_x_ipsecrequest_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_msg2sp: invalid policy type.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
key_freesp(newsp);
|
2000-01-31 17:18:52 +03:00
|
|
|
*error = EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
*error = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
return newsp;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
static u_int32_t
|
|
|
|
key_newreqid()
|
|
|
|
{
|
|
|
|
static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
|
|
|
|
|
|
|
|
auto_reqid = (auto_reqid == ~0
|
|
|
|
? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
|
|
|
|
|
|
|
|
/* XXX should be unique check */
|
|
|
|
|
|
|
|
return auto_reqid;
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* copy secpolicy struct to sadb_x_policy structure indicated.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
struct mbuf *
|
1999-06-28 10:36:47 +04:00
|
|
|
key_sp2msg(sp)
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
|
|
|
struct sadb_x_policy *xpl;
|
2000-01-31 17:18:52 +03:00
|
|
|
int tlen;
|
1999-06-28 10:36:47 +04:00
|
|
|
caddr_t p;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct mbuf *m;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check. */
|
|
|
|
if (sp == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_sp2msg: NULL pointer was passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
tlen = key_getspreqmsglen(sp);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_alloc_mbuf(tlen);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
2000-01-31 17:18:52 +03:00
|
|
|
if (m)
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
m->m_len = tlen;
|
|
|
|
m->m_next = NULL;
|
|
|
|
xpl = mtod(m, struct sadb_x_policy *);
|
|
|
|
bzero(xpl, tlen);
|
|
|
|
|
|
|
|
xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
|
1999-06-28 10:36:47 +04:00
|
|
|
xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
|
|
|
|
xpl->sadb_x_policy_type = sp->policy;
|
2002-06-12 05:47:34 +04:00
|
|
|
xpl->sadb_x_policy_dir = sp->dir;
|
2000-06-12 14:40:37 +04:00
|
|
|
xpl->sadb_x_policy_id = sp->id;
|
1999-06-28 10:36:47 +04:00
|
|
|
p = (caddr_t)xpl + sizeof(*xpl);
|
|
|
|
|
|
|
|
/* if is the policy for ipsec ? */
|
|
|
|
if (sp->policy == IPSEC_POLICY_IPSEC) {
|
|
|
|
struct sadb_x_ipsecrequest *xisr;
|
|
|
|
struct ipsecrequest *isr;
|
|
|
|
|
|
|
|
for (isr = sp->req; isr != NULL; isr = isr->next) {
|
|
|
|
|
|
|
|
xisr = (struct sadb_x_ipsecrequest *)p;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
|
|
|
|
xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
|
|
|
|
xisr->sadb_x_ipsecrequest_level = isr->level;
|
|
|
|
xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
|
|
|
|
|
|
|
|
p += sizeof(*xisr);
|
|
|
|
bcopy(&isr->saidx.src, p, isr->saidx.src.ss_len);
|
|
|
|
p += isr->saidx.src.ss_len;
|
|
|
|
bcopy(&isr->saidx.dst, p, isr->saidx.dst.ss_len);
|
|
|
|
p += isr->saidx.src.ss_len;
|
|
|
|
|
|
|
|
xisr->sadb_x_ipsecrequest_len =
|
|
|
|
PFKEY_ALIGN8(sizeof(*xisr)
|
|
|
|
+ isr->saidx.src.ss_len
|
|
|
|
+ isr->saidx.dst.ss_len);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
return m;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* m will not be freed nor modified */
|
|
|
|
static struct mbuf *
|
|
|
|
#ifdef __STDC__
|
|
|
|
key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
|
|
|
|
int ndeep, int nitem, ...)
|
|
|
|
#else
|
|
|
|
key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
|
|
|
int ndeep;
|
|
|
|
int nitem;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int idx;
|
|
|
|
int i;
|
|
|
|
struct mbuf *result = NULL, *n;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (m == NULL || mhp == NULL)
|
|
|
|
panic("null pointer passed to key_gather");
|
|
|
|
|
|
|
|
va_start(ap, nitem);
|
|
|
|
for (i = 0; i < nitem; i++) {
|
|
|
|
idx = va_arg(ap, int);
|
|
|
|
if (idx < 0 || idx > SADB_EXT_MAX)
|
|
|
|
goto fail;
|
|
|
|
/* don't attempt to pull empty extension */
|
|
|
|
if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
|
|
|
|
continue;
|
|
|
|
if (idx != SADB_EXT_RESERVED &&
|
|
|
|
(mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (idx == SADB_EXT_RESERVED) {
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (len > MHLEN)
|
|
|
|
panic("assumption failed");
|
|
|
|
#endif
|
|
|
|
MGETHDR(n, M_DONTWAIT, MT_DATA);
|
|
|
|
if (!n)
|
|
|
|
goto fail;
|
|
|
|
n->m_len = len;
|
|
|
|
n->m_next = NULL;
|
|
|
|
m_copydata(m, 0, sizeof(struct sadb_msg),
|
|
|
|
mtod(n, caddr_t));
|
|
|
|
} else if (i < ndeep) {
|
|
|
|
len = mhp->extlen[idx];
|
|
|
|
n = key_alloc_mbuf(len);
|
|
|
|
if (!n || n->m_next) { /*XXX*/
|
|
|
|
if (n)
|
|
|
|
m_freem(n);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
|
|
|
|
mtod(n, caddr_t));
|
|
|
|
} else {
|
|
|
|
n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
|
|
|
|
M_DONTWAIT);
|
|
|
|
}
|
|
|
|
if (n == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
m_cat(result, n);
|
|
|
|
else
|
|
|
|
result = n;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if ((result->m_flags & M_PKTHDR) != 0) {
|
|
|
|
result->m_pkthdr.len = 0;
|
|
|
|
for (n = result; n; n = n->m_next)
|
|
|
|
result->m_pkthdr.len += n->m_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
fail:
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
2000-06-12 14:40:37 +04:00
|
|
|
* SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
|
2003-07-22 15:12:15 +04:00
|
|
|
* add an entry to SP database, when received
|
2002-05-19 12:22:12 +04:00
|
|
|
* <base, address(SD), (lifetime(H),) policy>
|
1999-06-28 10:36:47 +04:00
|
|
|
* from the user(?).
|
|
|
|
* Adding to SP database,
|
|
|
|
* and send
|
2002-05-19 12:22:12 +04:00
|
|
|
* <base, address(SD), (lifetime(H),) policy>
|
1999-06-28 10:36:47 +04:00
|
|
|
* to the socket which was send.
|
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* SPDADD set a unique policy entry.
|
|
|
|
* SPDSETIDX like SPDADD without a part of policy requests.
|
|
|
|
* SPDUPDATE replace a unique policy entry.
|
1999-06-28 10:36:47 +04:00
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_spdadd(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-11-04 08:50:54 +03:00
|
|
|
struct sadb_address *src0 = NULL, *dst0 = NULL;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct sadb_x_policy *xpl0, *xpl;
|
2002-05-19 12:22:12 +04:00
|
|
|
struct sadb_lifetime *lft = NULL;
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
struct sadb_x_tag *tag = NULL;
|
|
|
|
#endif
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secpolicyindex spidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct secpolicy *newsp;
|
2002-10-04 09:45:22 +04:00
|
|
|
struct ipsecrequest *isr;
|
2000-01-31 17:18:52 +03:00
|
|
|
int error;
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
2003-11-04 08:50:54 +03:00
|
|
|
u_int16_t tagvalue = 0;
|
2003-09-12 11:38:10 +04:00
|
|
|
#endif
|
|
|
|
int spidxmode;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spdadd: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
if ((mhp->ext[SADB_EXT_ADDRESS_SRC] != NULL &&
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] != NULL) ||
|
|
|
|
mhp->ext[SADB_X_EXT_TAG] != NULL)
|
|
|
|
#else
|
|
|
|
if (mhp->ext[SADB_EXT_ADDRESS_SRC] != NULL &&
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] != NULL)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
;
|
|
|
|
} else {
|
2003-08-22 09:46:37 +04:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
if (mhp->ext[SADB_X_EXT_TAG] != NULL) {
|
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: tag not supported.\n"));
|
|
|
|
return key_senderror(so, m, EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (mhp->ext[SADB_X_EXT_POLICY] == NULL) {
|
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if ((mhp->extlen[SADB_EXT_ADDRESS_SRC] &&
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address)) ||
|
|
|
|
(mhp->extlen[SADB_EXT_ADDRESS_DST] &&
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) ||
|
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
(mhp->extlen[SADB_X_EXT_TAG] &&
|
|
|
|
mhp->extlen[SADB_X_EXT_TAG] < sizeof(struct sadb_x_tag)) ||
|
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2002-05-19 12:22:12 +04:00
|
|
|
if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
|
|
|
|
if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
|
|
|
|
< sizeof(struct sadb_lifetime)) {
|
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
/* spidx mode, or tag mode */
|
|
|
|
spidxmode = (mhp->ext[SADB_EXT_ADDRESS_SRC] != NULL);
|
|
|
|
#ifndef SADB_X_EXT_TAG
|
|
|
|
if (!spidxmode)
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
if (spidxmode) {
|
|
|
|
src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
|
|
|
|
dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
|
|
|
|
/* make secindex */
|
|
|
|
/* XXX boundary check against sa_len */
|
|
|
|
KEY_SETSECSPIDX(src0 + 1, dst0 + 1,
|
|
|
|
src0->sadb_address_prefixlen, dst0->sadb_address_prefixlen,
|
|
|
|
src0->sadb_address_proto, &spidx);
|
|
|
|
}
|
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
else
|
|
|
|
tag = (struct sadb_x_tag *)mhp->ext[SADB_X_EXT_TAG];
|
|
|
|
#endif
|
|
|
|
xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
|
2003-08-22 09:48:27 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* checking the direciton. */
|
2000-01-31 17:18:52 +03:00
|
|
|
switch (xpl0->sadb_x_policy_dir) {
|
|
|
|
case IPSEC_DIR_INBOUND:
|
|
|
|
case IPSEC_DIR_OUTBOUND:
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
2000-01-31 17:18:52 +03:00
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: Invalid SP direction.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
mhp->msg->sadb_msg_errno = EINVAL;
|
2000-06-24 04:15:52 +04:00
|
|
|
return 0;
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* check policy */
|
|
|
|
/* key_spdadd() accepts DISCARD, NONE and IPSEC. */
|
2003-07-22 15:12:15 +04:00
|
|
|
if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST ||
|
|
|
|
xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: Invalid policy type.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* policy requests are mandatory when action is ipsec. */
|
2003-07-22 15:12:15 +04:00
|
|
|
if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX &&
|
|
|
|
xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
|
|
|
|
mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: some policy requests part required.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* checking there is SP already or not.
|
2002-01-31 09:17:03 +03:00
|
|
|
* SPDUPDATE doesn't depend on whether there is a SP or not.
|
|
|
|
* If the type is either SPDADD or SPDSETIDX AND a SP is found,
|
|
|
|
* then error.
|
2000-06-12 14:40:37 +04:00
|
|
|
*/
|
2003-09-12 11:38:10 +04:00
|
|
|
if (xpl0->sadb_x_policy_id != 0)
|
|
|
|
newsp = key_getspbyid(xpl0->sadb_x_policy_id);
|
|
|
|
else if (spidxmode)
|
|
|
|
newsp = key_getsp(&spidx, xpl0->sadb_x_policy_dir);
|
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
else {
|
|
|
|
tagvalue = m_nametag_tagname2tag(tag->sadb_x_tag_name);
|
|
|
|
/* tag refcnt++ */
|
|
|
|
newsp = key_getspbytag(tagvalue, xpl0->sadb_x_policy_dir);
|
|
|
|
}
|
2003-10-25 12:27:12 +04:00
|
|
|
#else
|
|
|
|
else
|
|
|
|
newsp = NULL;
|
2003-09-12 11:38:10 +04:00
|
|
|
#endif
|
2003-09-22 08:47:43 +04:00
|
|
|
|
|
|
|
if (newsp && (newsp->readonly || newsp->persist)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_spdadd: tried to alter readonly/persistent SP.\n"));
|
|
|
|
return key_senderror(so, m, EPERM);
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
|
2002-01-31 09:17:03 +03:00
|
|
|
if (newsp) {
|
2002-05-19 12:22:12 +04:00
|
|
|
key_sp_dead(newsp);
|
|
|
|
key_freesp(newsp); /* ref gained by key_getsp */
|
|
|
|
key_sp_unlink(newsp);
|
|
|
|
newsp = NULL;
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (newsp != NULL) {
|
|
|
|
key_freesp(newsp);
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdadd: a SP entry exists already.\n"));
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
if (!mhp->ext[SADB_EXT_ADDRESS_SRC])
|
|
|
|
m_nametag_unref(tagvalue);
|
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EEXIST);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocation new SP entry */
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
if (!spidxmode)
|
|
|
|
m_nametag_unref(tagvalue);
|
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, error);
|
|
|
|
}
|
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
if (spidxmode) {
|
|
|
|
error = keydb_setsecpolicyindex(newsp, &spidx);
|
|
|
|
if (error) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, error);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
/* sanity check on addr pair */
|
|
|
|
if (((struct sockaddr *)(src0 + 1))->sa_family !=
|
|
|
|
((struct sockaddr *)(dst0 + 1))->sa_family) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (((struct sockaddr *)(src0 + 1))->sa_len !=
|
|
|
|
((struct sockaddr *)(dst0 + 1))->sa_len) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2003-08-22 09:48:27 +04:00
|
|
|
}
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
else {
|
|
|
|
newsp->tag = tagvalue;
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
2003-09-12 11:38:10 +04:00
|
|
|
#endif
|
2002-10-04 09:45:22 +04:00
|
|
|
|
|
|
|
for (isr = newsp->req; isr; isr = isr->next) {
|
2000-06-12 14:40:37 +04:00
|
|
|
struct sockaddr *sa;
|
2002-10-04 09:45:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* port spec is not permitted for tunnel mode
|
|
|
|
*/
|
2003-09-12 11:38:10 +04:00
|
|
|
if (isr->saidx.mode == IPSEC_MODE_TUNNEL && src0 && dst0) {
|
2002-10-04 09:45:22 +04:00
|
|
|
sa = (struct sockaddr *)(src0 + 1);
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (((struct sockaddr_in *)sa)->sin_port) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (((struct sockaddr_in6 *)sa)->sin6_port) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2002-12-09 06:20:45 +03:00
|
|
|
sa = (struct sockaddr *)(dst0 + 1);
|
2002-10-04 09:45:22 +04:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (((struct sockaddr_in *)sa)->sin_port) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (((struct sockaddr_in6 *)sa)->sin6_port) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
2003-06-16 12:11:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bark if we have different address family on tunnel address
|
|
|
|
* specification. applies only if we decapsulate in RFC2401
|
|
|
|
* IPsec (implementation limitation).
|
|
|
|
*/
|
|
|
|
for (isr = newsp->req; isr; isr = isr->next) {
|
|
|
|
struct sockaddr *sa;
|
2002-10-04 09:45:22 +04:00
|
|
|
|
2003-08-22 10:22:21 +04:00
|
|
|
if (isr->saidx.src.ss_family && src0) {
|
2002-10-04 09:45:22 +04:00
|
|
|
sa = (struct sockaddr *)(src0 + 1);
|
|
|
|
if (sa->sa_family != isr->saidx.src.ss_family) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
}
|
2003-08-22 10:22:21 +04:00
|
|
|
if (isr->saidx.dst.ss_family && dst0) {
|
2002-10-04 09:45:22 +04:00
|
|
|
sa = (struct sockaddr *)(dst0 + 1);
|
|
|
|
if (sa->sa_family != isr->saidx.dst.ss_family) {
|
|
|
|
keydb_delsecpolicy(newsp);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
2002-10-04 09:45:22 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
|
2002-05-19 12:22:12 +04:00
|
|
|
newsp->created = time.tv_sec;
|
|
|
|
newsp->lastused = time.tv_sec;
|
|
|
|
newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
|
|
|
|
newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
newsp->refcnt = 1; /* do not reclaim until I say I do */
|
|
|
|
newsp->state = IPSEC_SPSTATE_ALIVE;
|
2002-06-12 05:47:34 +04:00
|
|
|
LIST_INSERT_TAIL(&sptree[newsp->dir], newsp, secpolicy, chain);
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
/* delete the entry in spacqtree */
|
2003-08-22 10:22:21 +04:00
|
|
|
if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE &&
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_SRC]) {
|
2000-06-12 14:40:37 +04:00
|
|
|
struct secspacq *spacq;
|
|
|
|
if ((spacq = key_getspacq(&spidx)) != NULL) {
|
2000-09-22 20:55:04 +04:00
|
|
|
/* reset counter in order to deletion by timehandler. */
|
2002-01-31 09:35:25 +03:00
|
|
|
spacq->created = time.tv_sec;
|
2000-06-12 14:40:37 +04:00
|
|
|
spacq->count = 0;
|
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2001-08-06 14:25:00 +04:00
|
|
|
/* invalidate all cached SPD pointers on pcb */
|
|
|
|
ipsec_invalpcbcacheall();
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-14 07:16:23 +04:00
|
|
|
struct mbuf *n, *mpolicy;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sadb_msg *newmsg;
|
2000-06-14 07:16:23 +04:00
|
|
|
int off;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2002-05-19 12:22:12 +04:00
|
|
|
if (lft) {
|
|
|
|
n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
|
|
|
|
SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
|
|
|
|
SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
|
|
|
|
} else {
|
|
|
|
n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
|
|
|
|
SADB_X_EXT_POLICY,
|
|
|
|
SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
|
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-14 07:16:23 +04:00
|
|
|
if (n->m_len < sizeof(*newmsg)) {
|
|
|
|
n = m_pullup(n, sizeof(*newmsg));
|
|
|
|
if (!n)
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg = mtod(n, struct sadb_msg *);
|
1999-06-28 10:36:47 +04:00
|
|
|
newmsg->sadb_msg_errno = 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-14 07:16:23 +04:00
|
|
|
off = 0;
|
|
|
|
mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
|
|
|
|
sizeof(*xpl), &off);
|
|
|
|
if (mpolicy == NULL) {
|
|
|
|
/* n is already freed */
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
|
|
|
|
if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
|
|
|
|
m_freem(n);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
xpl->sadb_x_policy_id = newsp->id;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* SADB_SPDDELETE processing
|
|
|
|
* receive
|
2000-01-31 17:18:52 +03:00
|
|
|
* <base, address(SD), policy(*)>
|
1999-06-28 10:36:47 +04:00
|
|
|
* from the user(?), and set SADB_SASTATE_DEAD,
|
|
|
|
* and send,
|
2000-01-31 17:18:52 +03:00
|
|
|
* <base, address(SD), policy(*)>
|
1999-06-28 10:36:47 +04:00
|
|
|
* to the ikmpd.
|
2003-07-22 15:12:15 +04:00
|
|
|
* policy(*) including the direction of the policy.
|
1999-06-28 10:36:47 +04:00
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_spddelete(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct sadb_address *src0, *dst0;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct sadb_x_policy *xpl0;
|
|
|
|
struct secpolicyindex spidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct secpolicy *sp;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spddelete: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
|
|
|
|
mhp->ext[SADB_X_EXT_POLICY] == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
|
|
|
|
mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spddelete: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
|
|
|
|
dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
|
|
|
|
xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* make secindex */
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX boundary check against sa_len */
|
2002-06-12 05:47:34 +04:00
|
|
|
KEY_SETSECSPIDX(src0 + 1,
|
2000-01-31 17:18:52 +03:00
|
|
|
dst0 + 1,
|
|
|
|
src0->sadb_address_prefixlen,
|
|
|
|
dst0->sadb_address_prefixlen,
|
|
|
|
src0->sadb_address_proto,
|
|
|
|
&spidx);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* checking the direciton. */
|
2000-01-31 17:18:52 +03:00
|
|
|
switch (xpl0->sadb_x_policy_dir) {
|
|
|
|
case IPSEC_DIR_INBOUND:
|
|
|
|
case IPSEC_DIR_OUTBOUND:
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
2000-01-31 17:18:52 +03:00
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spddelete: Invalid SP direction.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is there SP in SPD ? */
|
2002-06-12 05:47:34 +04:00
|
|
|
if ((sp = key_getsp(&spidx, xpl0->sadb_x_policy_dir)) == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spddelete: no SP found.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-22 08:47:43 +04:00
|
|
|
if (sp->persist) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_spddelete2: attempt to remove persistent SP:%u.\n",
|
|
|
|
sp->id));
|
|
|
|
return key_senderror(so, m, EPERM);
|
|
|
|
}
|
|
|
|
|
2003-07-22 15:12:15 +04:00
|
|
|
/* save policy id to be returned. */
|
2000-06-12 14:40:37 +04:00
|
|
|
xpl0->sadb_x_policy_id = sp->id;
|
|
|
|
|
2002-05-19 12:22:12 +04:00
|
|
|
key_sp_dead(sp);
|
|
|
|
key_freesp(sp); /* ref gained by key_getsp */
|
|
|
|
key_sp_unlink(sp);
|
|
|
|
sp = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2001-08-06 14:25:00 +04:00
|
|
|
/* invalidate all cached SPD pointers on pcb */
|
|
|
|
ipsec_invalpcbcacheall();
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2000-06-12 14:40:37 +04:00
|
|
|
n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
|
|
|
|
SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
|
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg = mtod(n, struct sadb_msg *);
|
1999-06-28 10:36:47 +04:00
|
|
|
newmsg->sadb_msg_errno = 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-06-12 14:40:37 +04:00
|
|
|
* SADB_SPDDELETE2 processing
|
1999-06-28 10:36:47 +04:00
|
|
|
* receive
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, policy(*)>
|
|
|
|
* from the user(?), and set SADB_SASTATE_DEAD,
|
1999-06-28 10:36:47 +04:00
|
|
|
* and send,
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, policy(*)>
|
|
|
|
* to the ikmpd.
|
2003-07-22 15:12:15 +04:00
|
|
|
* policy(*) including the policy id.
|
1999-06-28 10:36:47 +04:00
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_spddelete2(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
u_int32_t id;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct secpolicy *sp;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spddelete2: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
|
|
|
|
mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spddelete2: invalid message is passed.\n"));
|
2002-08-20 12:17:02 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
|
|
|
|
|
|
|
|
/* Is there SP in SPD ? */
|
|
|
|
if ((sp = key_getspbyid(id)) == NULL) {
|
2003-09-22 08:47:43 +04:00
|
|
|
ipseclog((LOG_DEBUG, "key_spddelete2: no SP found id:%u.\n",
|
|
|
|
id));
|
2002-08-20 12:17:02 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-22 08:47:43 +04:00
|
|
|
if (sp->persist) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_spddelete2: attempt to remove persistent SP:%u.\n",
|
|
|
|
id));
|
|
|
|
return key_senderror(so, m, EPERM);
|
|
|
|
}
|
|
|
|
|
2002-05-19 12:22:12 +04:00
|
|
|
key_sp_dead(sp);
|
|
|
|
key_freesp(sp); /* ref gained by key_getsp */
|
|
|
|
key_sp_unlink(sp);
|
|
|
|
sp = NULL;
|
2000-06-12 14:40:37 +04:00
|
|
|
|
2001-08-06 14:25:00 +04:00
|
|
|
/* invalidate all cached SPD pointers on pcb */
|
|
|
|
ipsec_invalpcbcacheall();
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n, *nn;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sadb_msg *newmsg;
|
2000-06-12 14:40:37 +04:00
|
|
|
int off, len;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2000-06-12 14:40:37 +04:00
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (len > MCLBYTES)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
MGETHDR(n, M_DONTWAIT, MT_DATA);
|
|
|
|
if (n && len > MHLEN) {
|
|
|
|
MCLGET(n, M_DONTWAIT);
|
|
|
|
if ((n->m_flags & M_EXT) == 0) {
|
|
|
|
m_freem(n);
|
|
|
|
n = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
|
|
|
|
n->m_len = len;
|
|
|
|
n->m_next = NULL;
|
|
|
|
off = 0;
|
|
|
|
|
|
|
|
m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
|
|
|
|
off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (off != len)
|
|
|
|
panic("length inconsistency in key_spddelete2");
|
2000-01-31 17:18:52 +03:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
|
|
|
|
mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
|
|
|
|
if (!n->m_next) {
|
|
|
|
m_freem(n);
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
n->m_pkthdr.len = 0;
|
|
|
|
for (nn = n; nn; nn = nn->m_next)
|
|
|
|
n->m_pkthdr.len += nn->m_len;
|
|
|
|
|
|
|
|
newmsg = mtod(n, struct sadb_msg *);
|
1999-06-28 10:36:47 +04:00
|
|
|
newmsg->sadb_msg_errno = 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-07-22 15:12:15 +04:00
|
|
|
* SADB_X_SPDGET processing
|
1999-06-28 10:36:47 +04:00
|
|
|
* receive
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, policy(*)>
|
|
|
|
* from the user(?),
|
|
|
|
* and send,
|
|
|
|
* <base, address(SD), policy>
|
|
|
|
* to the ikmpd.
|
|
|
|
* policy(*) including direction of policy.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_spdget(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
|
|
|
{
|
|
|
|
u_int32_t id;
|
|
|
|
struct secpolicy *sp;
|
|
|
|
struct mbuf *n;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spdget: NULL pointer is passed.");
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
|
|
|
|
mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdget: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
|
|
|
|
|
|
|
|
/* Is there SP in SPD ? */
|
|
|
|
if ((sp = key_getspbyid(id)) == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdget: no SP found id:%u.\n", id));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
|
|
|
|
if (n != NULL) {
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
|
|
|
} else
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_X_SPDACQUIRE processing.
|
|
|
|
* Acquire policy and SA(s) for a *OUTBOUND* packet.
|
|
|
|
* send
|
|
|
|
* <base, policy(*)>
|
|
|
|
* to KMD, and expect to receive
|
2002-01-31 09:35:25 +03:00
|
|
|
* <base> with SADB_X_SPDACQUIRE if error occured,
|
2000-06-12 14:40:37 +04:00
|
|
|
* or
|
|
|
|
* <base, policy>
|
|
|
|
* with SADB_X_SPDUPDATE from KMD by PF_KEY.
|
|
|
|
* policy(*) is without policy requests.
|
|
|
|
*
|
|
|
|
* 0 : succeed
|
|
|
|
* others: error number
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
key_spdacquire(sp)
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
|
|
|
struct mbuf *result = NULL, *m;
|
2003-07-25 13:04:48 +04:00
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
2000-06-12 14:40:37 +04:00
|
|
|
struct secspacq *newspacq;
|
2003-07-25 13:04:48 +04:00
|
|
|
#endif
|
|
|
|
int error = -1;
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (sp == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spdacquire: NULL pointer is passed.");
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sp->req != NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spdacquire: called but there is request.");
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sp->policy != IPSEC_POLICY_IPSEC)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spdacquire: policy mismathed. IPsec is expected.");
|
2003-08-22 10:22:21 +04:00
|
|
|
if (!sp->spidx) {
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
goto fail;
|
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
|
2003-07-25 13:04:48 +04:00
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
2003-07-22 15:12:15 +04:00
|
|
|
/* get an entry to check whether sent message or not. */
|
2002-06-12 05:47:34 +04:00
|
|
|
if ((newspacq = key_getspacq(sp->spidx)) != NULL) {
|
2000-06-12 14:40:37 +04:00
|
|
|
if (key_blockacq_count < newspacq->count) {
|
|
|
|
/* reset counter and do send message. */
|
|
|
|
newspacq->count = 0;
|
|
|
|
} else {
|
|
|
|
/* increment counter and do nothing. */
|
|
|
|
newspacq->count++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* make new entry for blocking to send SADB_ACQUIRE. */
|
2002-06-12 05:47:34 +04:00
|
|
|
if ((newspacq = key_newspacq(sp->spidx)) == NULL)
|
2000-06-12 14:40:37 +04:00
|
|
|
return ENOBUFS;
|
|
|
|
|
|
|
|
/* add to acqtree */
|
|
|
|
LIST_INSERT_HEAD(&spacqtree, newspacq, chain);
|
|
|
|
}
|
2003-07-25 13:04:48 +04:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
result = m;
|
|
|
|
|
2003-07-25 13:04:48 +04:00
|
|
|
/* set sadb_x_policy */
|
|
|
|
if (sp) {
|
|
|
|
m = key_setsadbxpolicy(sp->policy, sp->dir, sp->id);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
result->m_pkthdr.len = 0;
|
|
|
|
for (m = result; m; m = m->m_next)
|
|
|
|
result->m_pkthdr.len += m->m_len;
|
|
|
|
|
|
|
|
mtod(result, struct sadb_msg *)->sadb_msg_len =
|
|
|
|
PFKEY_UNIT64(result->m_pkthdr.len);
|
|
|
|
|
2003-07-25 13:04:48 +04:00
|
|
|
return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_SPDFLUSH processing
|
|
|
|
* receive
|
|
|
|
* <base>
|
|
|
|
* from the user, and free all entries in secpctree.
|
|
|
|
* and send,
|
|
|
|
* <base>
|
|
|
|
* to the user.
|
|
|
|
* NOTE: what to do is only marking SADB_SASTATE_DEAD.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_spdflush(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
|
|
|
{
|
|
|
|
struct sadb_msg *newmsg;
|
2002-05-19 12:22:12 +04:00
|
|
|
struct secpolicy *sp, *nextsp;
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spdflush: NULL pointer is passed.");
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
for (sp = TAILQ_FIRST(&sptailq); sp; sp = nextsp) {
|
|
|
|
nextsp = TAILQ_NEXT(sp, tailq);
|
2003-09-22 08:47:43 +04:00
|
|
|
if (sp->persist)
|
|
|
|
continue;
|
2003-09-07 19:59:36 +04:00
|
|
|
if (sp->state == IPSEC_SPSTATE_DEAD)
|
|
|
|
continue;
|
|
|
|
key_sp_dead(sp);
|
|
|
|
key_sp_unlink(sp);
|
|
|
|
sp = NULL;
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
|
|
|
|
2001-08-06 14:25:00 +04:00
|
|
|
/* invalidate all cached SPD pointers on pcb */
|
|
|
|
ipsec_invalpcbcacheall();
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_spdflush: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->m_next)
|
|
|
|
m_freem(m->m_next);
|
|
|
|
m->m_next = NULL;
|
|
|
|
m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
|
|
|
newmsg = mtod(m, struct sadb_msg *);
|
|
|
|
newmsg->sadb_msg_errno = 0;
|
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
|
|
|
|
|
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_SPDDUMP processing
|
|
|
|
* receive
|
|
|
|
* <base>
|
|
|
|
* from the user, and dump all SP leaves
|
1999-06-28 10:36:47 +04:00
|
|
|
* and send,
|
|
|
|
* <base> .....
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
static int
|
2000-06-12 14:40:37 +04:00
|
|
|
key_spddump(so, m, mhp)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct socket *so;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
2000-01-31 17:18:52 +03:00
|
|
|
int cnt;
|
|
|
|
u_int dir;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spddump: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* search SPD entry and get buffer size. */
|
2000-01-31 17:18:52 +03:00
|
|
|
cnt = 0;
|
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
|
|
|
LIST_FOREACH(sp, &sptree[dir], chain) {
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
if (cnt == 0)
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOENT);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
|
|
|
LIST_FOREACH(sp, &sptree[dir], chain) {
|
|
|
|
--cnt;
|
2000-06-12 14:40:37 +04:00
|
|
|
n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
|
|
|
|
mhp->msg->sadb_msg_pid);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (n)
|
|
|
|
key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
static struct mbuf *
|
|
|
|
key_setspddump(errorp)
|
|
|
|
int *errorp;
|
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
|
|
|
int cnt;
|
|
|
|
u_int dir;
|
|
|
|
struct mbuf *m, *n;
|
|
|
|
|
|
|
|
/* search SPD entry and get buffer size. */
|
|
|
|
cnt = 0;
|
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
|
|
|
LIST_FOREACH(sp, &sptree[dir], chain) {
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt == 0) {
|
|
|
|
*errorp = ENOENT;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
m = NULL;
|
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
|
|
|
LIST_FOREACH(sp, &sptree[dir], chain) {
|
|
|
|
--cnt;
|
|
|
|
n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt, 0);
|
|
|
|
|
|
|
|
if (!n) {
|
|
|
|
*errorp = ENOBUFS;
|
|
|
|
m_freem(m);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (!m)
|
|
|
|
m = n;
|
2003-10-13 12:55:59 +04:00
|
|
|
else {
|
|
|
|
m->m_pkthdr.len += n->m_pkthdr.len;
|
2003-09-12 11:38:10 +04:00
|
|
|
m_cat(m, n);
|
2003-10-13 12:55:59 +04:00
|
|
|
}
|
2003-09-12 11:38:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*errorp = 0;
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
2003-09-08 10:51:53 +04:00
|
|
|
struct mbuf *
|
2000-01-31 17:18:52 +03:00
|
|
|
key_setdumpsp(sp, type, seq, pid)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct secpolicy *sp;
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int8_t type;
|
|
|
|
u_int32_t seq, pid;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *result = NULL, *m;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
result = m;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
if (sp->spidx) {
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
|
|
|
(struct sockaddr *)&sp->spidx->src, sp->spidx->prefs,
|
|
|
|
sp->spidx->ul_proto);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
|
|
|
(struct sockaddr *)&sp->spidx->dst, sp->spidx->prefd,
|
|
|
|
sp->spidx->ul_proto);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
else if (sp->tag) {
|
|
|
|
m = key_setsadbxtag(sp->tag);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_sp2msg(sp);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2002-05-19 12:22:12 +04:00
|
|
|
m = key_setsadblifetime(SADB_EXT_LIFETIME_CURRENT,
|
|
|
|
0, 0, (u_int64_t)sp->created, (u_int64_t)sp->lastused);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
m = key_setsadblifetime(SADB_EXT_LIFETIME_HARD,
|
|
|
|
0, 0, (u_int64_t)sp->lifetime, (u_int64_t)sp->validtime);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((result->m_flags & M_PKTHDR) == 0)
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (result->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
result = m_pullup(result, sizeof(struct sadb_msg));
|
|
|
|
if (result == NULL)
|
|
|
|
goto fail;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
result->m_pkthdr.len = 0;
|
|
|
|
for (m = result; m; m = m->m_next)
|
|
|
|
result->m_pkthdr.len += m->m_len;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
mtod(result, struct sadb_msg *)->sadb_msg_len =
|
|
|
|
PFKEY_UNIT64(result->m_pkthdr.len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return result;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
fail:
|
|
|
|
m_freem(result);
|
|
|
|
return NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* get PFKEY message length for security policy and request.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
static u_int
|
|
|
|
key_getspreqmsglen(sp)
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
|
|
|
u_int tlen;
|
|
|
|
|
|
|
|
tlen = sizeof(struct sadb_x_policy);
|
|
|
|
|
|
|
|
/* if is the policy for ipsec ? */
|
|
|
|
if (sp->policy != IPSEC_POLICY_IPSEC)
|
|
|
|
return tlen;
|
|
|
|
|
|
|
|
/* get length of ipsec requests */
|
|
|
|
{
|
|
|
|
struct ipsecrequest *isr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
for (isr = sp->req; isr != NULL; isr = isr->next) {
|
2000-01-31 17:18:52 +03:00
|
|
|
len = sizeof(struct sadb_x_ipsecrequest)
|
|
|
|
+ isr->saidx.src.ss_len
|
|
|
|
+ isr->saidx.dst.ss_len;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
tlen += PFKEY_ALIGN8(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlen;
|
|
|
|
}
|
|
|
|
|
2002-05-19 12:22:12 +04:00
|
|
|
/*
|
2003-07-22 15:12:15 +04:00
|
|
|
* SADB_X_SPDEXPIRE processing
|
2002-05-19 12:22:12 +04:00
|
|
|
* send
|
|
|
|
* <base, address(SD), lifetime(CH), policy>
|
|
|
|
* to KMD by PF_KEY.
|
|
|
|
*
|
|
|
|
* OUT: 0 : succeed
|
|
|
|
* others : error number
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_spdexpire(sp)
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct mbuf *result = NULL, *m;
|
|
|
|
int len;
|
|
|
|
int error = -1;
|
|
|
|
struct sadb_lifetime *lt;
|
|
|
|
|
|
|
|
/* XXX: Why do we lock ? */
|
|
|
|
s = splsoftnet(); /*called from softclock()*/
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (sp == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_spdexpire: NULL pointer is passed.");
|
2002-05-19 12:22:12 +04:00
|
|
|
|
|
|
|
/* set msg header */
|
|
|
|
m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
result = m;
|
|
|
|
|
|
|
|
/* create lifetime extension (current and hard) */
|
|
|
|
len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
bzero(mtod(m, caddr_t), len);
|
|
|
|
lt = mtod(m, struct sadb_lifetime *);
|
|
|
|
lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
|
|
|
|
lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
|
|
|
|
lt->sadb_lifetime_allocations = 0;
|
|
|
|
lt->sadb_lifetime_bytes = 0;
|
|
|
|
lt->sadb_lifetime_addtime = sp->created;
|
|
|
|
lt->sadb_lifetime_usetime = sp->lastused;
|
|
|
|
lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
|
|
|
|
lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
|
|
|
|
lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
|
|
|
|
lt->sadb_lifetime_allocations = 0;
|
|
|
|
lt->sadb_lifetime_bytes = 0;
|
|
|
|
lt->sadb_lifetime_addtime = sp->lifetime;
|
|
|
|
lt->sadb_lifetime_usetime = sp->validtime;
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
/* set sadb_address for source */
|
2003-09-12 11:38:10 +04:00
|
|
|
if (sp->spidx) {
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
|
|
|
(struct sockaddr *)&sp->spidx->src,
|
|
|
|
sp->spidx->prefs, sp->spidx->ul_proto);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
2002-05-19 12:22:12 +04:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
/* set sadb_address for destination */
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
|
|
|
(struct sockaddr *)&sp->spidx->dst,
|
|
|
|
sp->spidx->prefd, sp->spidx->ul_proto);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
2002-05-19 12:22:12 +04:00
|
|
|
}
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
else if (sp->tag) {
|
|
|
|
m = key_setsadbxtag(sp->tag);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
#endif
|
2002-05-19 12:22:12 +04:00
|
|
|
|
|
|
|
/* set secpolicy */
|
|
|
|
m = key_sp2msg(sp);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
if ((result->m_flags & M_PKTHDR) == 0) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
result = m_pullup(result, sizeof(struct sadb_msg));
|
|
|
|
if (result == NULL) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result->m_pkthdr.len = 0;
|
|
|
|
for (m = result; m; m = m->m_next)
|
|
|
|
result->m_pkthdr.len += m->m_len;
|
|
|
|
|
|
|
|
mtod(result, struct sadb_msg *)->sadb_msg_len =
|
|
|
|
PFKEY_UNIT64(result->m_pkthdr.len);
|
|
|
|
|
2003-11-27 21:26:46 +03:00
|
|
|
splx(s);
|
2002-05-19 12:22:12 +04:00
|
|
|
return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* %%% SAD management */
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* allocating a memory for new SA head, and copy from the values of mhp.
|
|
|
|
* OUT: NULL : failure due to the lack of memory.
|
|
|
|
* others : pointer to new SA head.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static struct secashead *
|
|
|
|
key_newsah(saidx)
|
|
|
|
struct secasindex *saidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secashead *newsah;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (saidx == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_newsaidx: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
newsah = keydb_newsecashead();
|
|
|
|
if (newsah == NULL)
|
|
|
|
return NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
bcopy(saidx, &newsah->saidx, sizeof(newsah->saidx));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* add to saidxtree */
|
|
|
|
newsah->state = SADB_SASTATE_MATURE;
|
|
|
|
LIST_INSERT_HEAD(&sahtree, newsah, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2002-09-11 06:46:42 +04:00
|
|
|
return (newsah);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* delete SA index and all SA registerd.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static void
|
|
|
|
key_delsah(sah)
|
|
|
|
struct secashead *sah;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav, *nextsav;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int stateidx, state;
|
|
|
|
int s;
|
2000-01-31 17:18:52 +03:00
|
|
|
int zombie = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sah == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_delsah: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
1999-07-04 06:01:15 +04:00
|
|
|
s = splsoftnet(); /*called from softclock()*/
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* searching all SA registerd in the secindex. */
|
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_any);
|
|
|
|
stateidx++) {
|
|
|
|
|
|
|
|
state = saorder_state_any[stateidx];
|
2003-09-07 19:59:36 +04:00
|
|
|
for (sav = LIST_FIRST(&sah->savtree[state]);
|
2000-01-31 17:18:52 +03:00
|
|
|
sav != NULL;
|
|
|
|
sav = nextsav) {
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextsav = LIST_NEXT(sav, chain);
|
|
|
|
|
|
|
|
if (sav->refcnt > 0) {
|
|
|
|
/* give up to delete this sa */
|
|
|
|
zombie++;
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* sanity check */
|
|
|
|
KEY_CHKSASTATE(state, sav->state, "key_delsah");
|
|
|
|
|
|
|
|
key_freesav(sav);
|
|
|
|
|
|
|
|
/* remove back pointer */
|
|
|
|
sav->sah = NULL;
|
|
|
|
sav = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-21 05:27:50 +03:00
|
|
|
/* delete sah only if there's no sav. */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (zombie) {
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sah->sa_route.ro_rt) {
|
|
|
|
RTFREE(sah->sa_route.ro_rt);
|
|
|
|
sah->sa_route.ro_rt = (struct rtentry *)NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* remove from tree of SA index */
|
|
|
|
if (__LIST_CHAINED(sah))
|
|
|
|
LIST_REMOVE(sah, chain);
|
|
|
|
|
|
|
|
KFREE(sah);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* allocating a new SA with LARVAL state. key_add() and key_getspi() call,
|
1999-06-28 10:36:47 +04:00
|
|
|
* and copy the values of mhp into new buffer.
|
2000-01-31 17:18:52 +03:00
|
|
|
* When SAD message type is GETSPI:
|
1999-06-28 10:36:47 +04:00
|
|
|
* to set sequence number from acq_seq++,
|
|
|
|
* to set zero to SPI.
|
|
|
|
* not to call key_setsava().
|
|
|
|
* OUT: NULL : fail
|
2000-01-31 17:18:52 +03:00
|
|
|
* others : pointer to new secasvar.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
|
|
|
* does not modify mbuf. does not free mbuf on error.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static struct secasvar *
|
2000-06-12 14:40:37 +04:00
|
|
|
key_newsav(m, mhp, sah, errp)
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secashead *sah;
|
2000-06-12 14:40:37 +04:00
|
|
|
int *errp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *newsav;
|
2000-06-12 14:40:37 +04:00
|
|
|
const struct sadb_sa *xsa;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m == NULL || mhp == NULL || mhp->msg == NULL || sah == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_newsa: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
newsav = keydb_newsecasvar();
|
2000-01-31 17:18:52 +03:00
|
|
|
if (newsav == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
*errp = ENOBUFS;
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
switch (mhp->msg->sadb_msg_type) {
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_GETSPI:
|
2003-09-07 19:59:36 +04:00
|
|
|
key_setspi(newsav, 0);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifdef IPSEC_DOSEQCHECK
|
1999-06-28 10:36:47 +04:00
|
|
|
/* sync sequence number */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->msg->sadb_msg_seq == 0)
|
2000-01-31 17:18:52 +03:00
|
|
|
newsav->seq =
|
1999-06-28 10:36:47 +04:00
|
|
|
(acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
|
|
|
|
else
|
2000-01-31 17:18:52 +03:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
newsav->seq = mhp->msg->sadb_msg_seq;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_ADD:
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_SA] == NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
KFREE(newsav);
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
*errp = EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
|
2003-09-07 19:59:36 +04:00
|
|
|
key_setspi(newsav, xsa->sadb_sa_spi);
|
2000-06-12 14:40:37 +04:00
|
|
|
newsav->seq = mhp->msg->sadb_msg_seq;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
default:
|
2000-01-31 17:18:52 +03:00
|
|
|
KFREE(newsav);
|
2000-06-12 14:40:37 +04:00
|
|
|
*errp = EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* copy sav values */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
|
|
|
|
*errp = key_setsaval(newsav, m, mhp);
|
|
|
|
if (*errp) {
|
|
|
|
KFREE(newsav);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-09-22 20:55:04 +04:00
|
|
|
/* reset created */
|
2002-01-31 09:35:25 +03:00
|
|
|
newsav->created = time.tv_sec;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
newsav->pid = mhp->msg->sadb_msg_pid;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* add to satree */
|
2000-01-31 17:18:52 +03:00
|
|
|
newsav->sah = sah;
|
|
|
|
newsav->refcnt = 1;
|
|
|
|
newsav->state = SADB_SASTATE_LARVAL;
|
|
|
|
LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
|
|
|
|
secasvar, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
return newsav;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* search SAD.
|
1999-06-28 10:36:47 +04:00
|
|
|
* OUT:
|
|
|
|
* NULL : not found
|
|
|
|
* others : found, pointer to a SA.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static struct secashead *
|
|
|
|
key_getsah(saidx)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct secasindex *saidx;
|
2000-01-31 17:18:52 +03:00
|
|
|
{
|
|
|
|
struct secashead *sah;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
if (sah->state == SADB_SASTATE_DEAD)
|
|
|
|
continue;
|
|
|
|
if (key_cmpsaidx_exactly(&sah->saidx, saidx))
|
2002-09-11 06:46:42 +04:00
|
|
|
return (sah);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* check not to be duplicated SPI.
|
|
|
|
* NOTE: this function is too slow due to searching all SAD.
|
1999-06-28 10:36:47 +04:00
|
|
|
* OUT:
|
|
|
|
* NULL : not found
|
|
|
|
* others : found, pointer to a SA.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static struct secasvar *
|
|
|
|
key_checkspidup(saidx, spi)
|
|
|
|
struct secasindex *saidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int32_t spi;
|
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav;
|
2003-09-07 19:59:36 +04:00
|
|
|
u_int stateidx, state;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* check address family */
|
|
|
|
if (saidx->src.ss_family != saidx->dst.ss_family) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_checkspidup: address family mismatched.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
return NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* check all SAD */
|
2003-09-14 11:30:32 +04:00
|
|
|
LIST_FOREACH(sav, &spihash[SPIHASH(spi)], spihash) {
|
2003-09-07 19:59:36 +04:00
|
|
|
if (sav->spi != spi)
|
2000-01-31 17:18:52 +03:00
|
|
|
continue;
|
2003-09-07 19:59:36 +04:00
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_alive);
|
|
|
|
stateidx++) {
|
|
|
|
state = saorder_state_alive[stateidx];
|
|
|
|
if (sav->state == state &&
|
|
|
|
key_ismyaddr((struct sockaddr *)&sav->sah->saidx.dst))
|
|
|
|
return sav;
|
|
|
|
}
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
static void
|
|
|
|
key_setspi(sav, spi)
|
|
|
|
struct secasvar *sav;
|
|
|
|
u_int32_t spi;
|
|
|
|
{
|
2003-09-08 05:55:09 +04:00
|
|
|
int s;
|
2003-09-07 19:59:36 +04:00
|
|
|
|
2003-09-08 05:55:09 +04:00
|
|
|
s = splsoftnet();
|
2003-09-07 19:59:36 +04:00
|
|
|
sav->spi = spi;
|
|
|
|
if (sav->spihash.le_prev || sav->spihash.le_next)
|
|
|
|
LIST_REMOVE(sav, spihash);
|
2003-09-14 11:30:32 +04:00
|
|
|
LIST_INSERT_HEAD(&spihash[SPIHASH(spi)], sav, spihash);
|
2003-09-08 05:55:09 +04:00
|
|
|
splx(s);
|
2003-09-07 19:59:36 +04:00
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* search SAD litmited alive SA, protocol, SPI.
|
1999-06-28 10:36:47 +04:00
|
|
|
* OUT:
|
|
|
|
* NULL : not found
|
|
|
|
* others : found, pointer to a SA.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static struct secasvar *
|
|
|
|
key_getsavbyspi(sah, spi)
|
|
|
|
struct secashead *sah;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int32_t spi;
|
|
|
|
{
|
2003-09-08 00:41:27 +04:00
|
|
|
struct secasvar *sav, *match;
|
|
|
|
u_int stateidx, state, matchidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-08 00:41:27 +04:00
|
|
|
match = NULL;
|
|
|
|
matchidx = _ARRAYLEN(saorder_state_alive);
|
2003-09-14 11:30:32 +04:00
|
|
|
LIST_FOREACH(sav, &spihash[SPIHASH(spi)], spihash) {
|
2003-09-08 00:41:27 +04:00
|
|
|
if (sav->spi != spi)
|
|
|
|
continue;
|
|
|
|
if (sav->sah != sah)
|
|
|
|
continue;
|
|
|
|
for (stateidx = 0; stateidx < matchidx; stateidx++) {
|
|
|
|
state = saorder_state_alive[stateidx];
|
|
|
|
if (sav->state == state) {
|
|
|
|
match = sav;
|
|
|
|
matchidx = stateidx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2003-09-08 00:41:27 +04:00
|
|
|
return match;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
|
|
|
|
* You must update these if need.
|
|
|
|
* OUT: 0: success.
|
2000-06-12 14:40:37 +04:00
|
|
|
* !0: failure.
|
|
|
|
*
|
|
|
|
* does not modify mbuf. does not free mbuf on error.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
static int
|
2000-06-12 14:40:37 +04:00
|
|
|
key_setsaval(sav, m, mhp)
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-15 17:44:22 +04:00
|
|
|
#ifdef IPSEC_ESP
|
2000-06-12 14:40:37 +04:00
|
|
|
const struct esp_algorithm *algo;
|
2000-06-15 17:44:22 +04:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_setsaval: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* initialization */
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->replay = NULL;
|
|
|
|
sav->key_auth = NULL;
|
|
|
|
sav->key_enc = NULL;
|
2000-07-23 09:23:04 +04:00
|
|
|
sav->sched = NULL;
|
|
|
|
sav->schedlen = 0;
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->iv = NULL;
|
|
|
|
sav->lft_c = NULL;
|
|
|
|
sav->lft_h = NULL;
|
|
|
|
sav->lft_s = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* SA */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_SA] != NULL) {
|
|
|
|
const struct sadb_sa *sa0;
|
|
|
|
|
|
|
|
sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
|
|
|
|
if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->alg_auth = sa0->sadb_sa_auth;
|
|
|
|
sav->alg_enc = sa0->sadb_sa_encrypt;
|
|
|
|
sav->flags = sa0->sadb_sa_flags;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* replay window */
|
|
|
|
if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->replay = keydb_newsecreplay(sa0->sadb_sa_replay);
|
|
|
|
if (sav->replay == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
error = ENOBUFS;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Authentication keys */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
|
|
|
|
const struct sadb_key *key0;
|
|
|
|
int len;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
|
|
|
|
len = mhp->extlen[SADB_EXT_KEY_AUTH];
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
error = 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
if (len < sizeof(*key0)) {
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
switch (mhp->msg->sadb_msg_satype) {
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_SATYPE_AH:
|
|
|
|
case SADB_SATYPE_ESP:
|
2000-06-12 14:40:37 +04:00
|
|
|
if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
|
2000-07-18 18:56:42 +04:00
|
|
|
sav->alg_auth != SADB_X_AALG_NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: invalid key_auth values.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->key_auth = (struct sadb_key *)key_newbuf(key0, len);
|
|
|
|
if (sav->key_auth == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
error = ENOBUFS;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encryption key */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
|
|
|
|
const struct sadb_key *key0;
|
|
|
|
int len;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
|
|
|
|
len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
error = 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
if (len < sizeof(*key0)) {
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
switch (mhp->msg->sadb_msg_satype) {
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_SATYPE_ESP:
|
2000-06-12 14:40:37 +04:00
|
|
|
if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
|
2000-09-20 04:42:47 +04:00
|
|
|
sav->alg_enc != SADB_EALG_NULL) {
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
2000-09-20 04:42:47 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
sav->key_enc = (struct sadb_key *)key_newbuf(key0, len);
|
|
|
|
if (sav->key_enc == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
|
2000-09-20 04:42:47 +04:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
2000-09-20 04:42:47 +04:00
|
|
|
if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
|
|
|
|
error = EINVAL;
|
|
|
|
sav->key_enc = NULL; /*just in case*/
|
|
|
|
break;
|
|
|
|
case SADB_SATYPE_AH:
|
|
|
|
default:
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsatval: invalid key_enc value.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* set iv */
|
|
|
|
sav->ivlen = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
switch (mhp->msg->sadb_msg_satype) {
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_SATYPE_ESP:
|
|
|
|
#ifdef IPSEC_ESP
|
2000-07-18 18:56:42 +04:00
|
|
|
algo = esp_algorithm_lookup(sav->alg_enc);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (algo && algo->ivlen)
|
2000-08-29 13:08:42 +04:00
|
|
|
sav->ivlen = (*algo->ivlen)(algo, sav);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sav->ivlen == 0)
|
|
|
|
break;
|
2000-01-31 17:18:52 +03:00
|
|
|
KMALLOC(sav->iv, caddr_t, sav->ivlen);
|
|
|
|
if (sav->iv == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
error = ENOBUFS;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-08-27 21:41:12 +04:00
|
|
|
|
|
|
|
/* initialize */
|
2000-10-07 16:08:33 +04:00
|
|
|
key_randomfill(sav->iv, sav->ivlen);
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
2000-10-07 16:08:33 +04:00
|
|
|
break;
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_SATYPE_AH:
|
2003-09-12 15:09:32 +04:00
|
|
|
break;
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
|
|
|
break;
|
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: invalid SA type.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
error = EINVAL;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-09-22 20:55:04 +04:00
|
|
|
/* reset created */
|
2002-01-31 09:35:25 +03:00
|
|
|
sav->created = time.tv_sec;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* make lifetime for CURRENT */
|
2000-01-31 17:18:52 +03:00
|
|
|
KMALLOC(sav->lft_c, struct sadb_lifetime *,
|
2000-06-12 14:40:37 +04:00
|
|
|
sizeof(struct sadb_lifetime));
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->lft_c == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
error = ENOBUFS;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->lft_c->sadb_lifetime_len =
|
2000-06-12 14:40:37 +04:00
|
|
|
PFKEY_UNIT64(sizeof(struct sadb_lifetime));
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
|
|
|
|
sav->lft_c->sadb_lifetime_allocations = 0;
|
|
|
|
sav->lft_c->sadb_lifetime_bytes = 0;
|
2002-01-31 09:35:25 +03:00
|
|
|
sav->lft_c->sadb_lifetime_addtime = time.tv_sec;
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->lft_c->sadb_lifetime_usetime = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* lifetimes for HARD and SOFT */
|
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
const struct sadb_lifetime *lft0;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
|
1999-06-28 10:36:47 +04:00
|
|
|
if (lft0 != NULL) {
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->lft_h = (struct sadb_lifetime *)key_newbuf(lft0,
|
2000-06-12 14:40:37 +04:00
|
|
|
sizeof(*lft0));
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->lft_h == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
error = ENOBUFS;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2002-05-19 12:22:12 +04:00
|
|
|
/* we no longer support byte lifetime */
|
|
|
|
if (sav->lft_h->sadb_lifetime_bytes) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* initialize? */
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
|
1999-06-28 10:36:47 +04:00
|
|
|
if (lft0 != NULL) {
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->lft_s = (struct sadb_lifetime *)key_newbuf(lft0,
|
2000-06-12 14:40:37 +04:00
|
|
|
sizeof(*lft0));
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->lft_s == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
error = ENOBUFS;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2002-05-19 12:22:12 +04:00
|
|
|
/* we no longer support byte lifetime */
|
|
|
|
if (sav->lft_s->sadb_lifetime_bytes) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* initialize? */
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
fail:
|
2000-01-31 17:18:52 +03:00
|
|
|
/* initialization */
|
2000-09-20 04:08:42 +04:00
|
|
|
if (sav->replay != NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
keydb_delsecreplay(sav->replay);
|
2000-09-20 04:08:42 +04:00
|
|
|
sav->replay = NULL;
|
|
|
|
}
|
|
|
|
if (sav->key_auth != NULL) {
|
2003-07-22 15:01:09 +04:00
|
|
|
bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
|
2000-01-31 17:18:52 +03:00
|
|
|
KFREE(sav->key_auth);
|
2000-09-20 04:08:42 +04:00
|
|
|
sav->key_auth = NULL;
|
|
|
|
}
|
|
|
|
if (sav->key_enc != NULL) {
|
2003-07-22 15:01:09 +04:00
|
|
|
bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
|
2000-01-31 17:18:52 +03:00
|
|
|
KFREE(sav->key_enc);
|
2000-09-20 04:08:42 +04:00
|
|
|
sav->key_enc = NULL;
|
|
|
|
}
|
|
|
|
if (sav->sched) {
|
2003-07-22 12:54:27 +04:00
|
|
|
bzero(sav->sched, sav->schedlen);
|
2000-07-23 09:23:04 +04:00
|
|
|
KFREE(sav->sched);
|
2000-09-20 04:08:42 +04:00
|
|
|
sav->sched = NULL;
|
|
|
|
}
|
|
|
|
if (sav->iv != NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
KFREE(sav->iv);
|
2000-09-20 04:08:42 +04:00
|
|
|
sav->iv = NULL;
|
|
|
|
}
|
|
|
|
if (sav->lft_c != NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
KFREE(sav->lft_c);
|
2000-09-20 04:08:42 +04:00
|
|
|
sav->lft_c = NULL;
|
|
|
|
}
|
|
|
|
if (sav->lft_h != NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
KFREE(sav->lft_h);
|
2000-09-20 04:08:42 +04:00
|
|
|
sav->lft_h = NULL;
|
|
|
|
}
|
|
|
|
if (sav->lft_s != NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
KFREE(sav->lft_s);
|
2000-09-20 04:08:42 +04:00
|
|
|
sav->lft_s = NULL;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return error;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* validation with a secasvar entry, and set SADB_SATYPE_MATURE.
|
1999-06-28 10:36:47 +04:00
|
|
|
* OUT: 0: valid
|
|
|
|
* other: errno
|
|
|
|
*/
|
|
|
|
static int
|
2000-01-31 17:18:52 +03:00
|
|
|
key_mature(sav)
|
|
|
|
struct secasvar *sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
int mature;
|
|
|
|
int checkmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
|
|
|
|
int mustmask = 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
|
|
|
|
|
|
|
|
mature = 0;
|
|
|
|
|
|
|
|
/* check SPI value */
|
2000-09-20 23:55:05 +04:00
|
|
|
switch (sav->sah->saidx.proto) {
|
|
|
|
case IPPROTO_ESP:
|
|
|
|
case IPPROTO_AH:
|
|
|
|
if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_mature: illegal range of SPI %u.\n",
|
|
|
|
(u_int32_t)ntohl(sav->spi)));
|
2000-09-20 23:55:05 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check satype */
|
2000-01-31 17:18:52 +03:00
|
|
|
switch (sav->sah->saidx.proto) {
|
2003-09-12 15:09:32 +04:00
|
|
|
#ifdef IPSEC_ESP
|
2000-01-31 17:18:52 +03:00
|
|
|
case IPPROTO_ESP:
|
1999-06-28 10:36:47 +04:00
|
|
|
/* check flags */
|
2003-07-22 15:12:15 +04:00
|
|
|
if ((sav->flags & SADB_X_EXT_OLD) &&
|
|
|
|
(sav->flags & SADB_X_EXT_DERIV)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: "
|
|
|
|
"invalid flag (derived) given to old-esp.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
2000-07-18 18:56:42 +04:00
|
|
|
if (sav->alg_auth == SADB_AALG_NONE)
|
|
|
|
checkmask = 1;
|
|
|
|
else
|
|
|
|
checkmask = 3;
|
1999-06-28 10:36:47 +04:00
|
|
|
mustmask = 1;
|
|
|
|
break;
|
2003-09-12 15:09:32 +04:00
|
|
|
#endif
|
2000-01-31 17:18:52 +03:00
|
|
|
case IPPROTO_AH:
|
1999-06-28 10:36:47 +04:00
|
|
|
/* check flags */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->flags & SADB_X_EXT_DERIV) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: "
|
|
|
|
"invalid flag (derived) given to AH SA.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->alg_enc != SADB_EALG_NONE) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: "
|
|
|
|
"protocol and algorithm mismated.\n"));
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
checkmask = 2;
|
|
|
|
mustmask = 2;
|
|
|
|
break;
|
2000-01-31 17:18:52 +03:00
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
if (sav->alg_auth != SADB_AALG_NONE) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: "
|
|
|
|
"protocol and algorithm mismated.\n"));
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2003-07-22 15:12:15 +04:00
|
|
|
if ((sav->flags & SADB_X_EXT_RAWCPI) == 0 &&
|
|
|
|
ntohl(sav->spi) >= 0x10000) {
|
2003-09-12 15:09:32 +04:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_mature: invalid cpi for IPComp.\n"));
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
checkmask = 4;
|
|
|
|
mustmask = 4;
|
|
|
|
break;
|
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: Invalid satype.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check authentication algorithm */
|
|
|
|
if ((checkmask & 2) != 0) {
|
2000-07-18 18:56:42 +04:00
|
|
|
const struct ah_algorithm *algo;
|
1999-06-28 10:36:47 +04:00
|
|
|
int keylen;
|
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
algo = ah_algorithm_lookup(sav->alg_auth);
|
|
|
|
if (!algo) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,"key_mature: "
|
|
|
|
"unknown authentication algorithm.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* algorithm-dependent check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->key_auth)
|
|
|
|
keylen = sav->key_auth->sadb_key_bits;
|
1999-06-28 10:36:47 +04:00
|
|
|
else
|
|
|
|
keylen = 0;
|
|
|
|
if (keylen < algo->keymin || algo->keymax < keylen) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_mature: invalid AH key length %d "
|
|
|
|
"(%d-%d allowed)\n",
|
|
|
|
keylen, algo->keymin, algo->keymax));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (algo->mature) {
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((*algo->mature)(sav)) {
|
|
|
|
/* message generated in per-algorithm function*/
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
} else
|
|
|
|
mature = SADB_SATYPE_AH;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((mustmask & 2) != 0 && mature != SADB_SATYPE_AH) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for AH\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check encryption algorithm */
|
|
|
|
if ((checkmask & 1) != 0) {
|
|
|
|
#ifdef IPSEC_ESP
|
2000-07-18 18:56:42 +04:00
|
|
|
const struct esp_algorithm *algo;
|
1999-06-28 10:36:47 +04:00
|
|
|
int keylen;
|
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
algo = esp_algorithm_lookup(sav->alg_enc);
|
|
|
|
if (!algo) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: unknown encryption algorithm.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
return EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* algorithm-dependent check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->key_enc)
|
|
|
|
keylen = sav->key_enc->sadb_key_bits;
|
1999-06-28 10:36:47 +04:00
|
|
|
else
|
|
|
|
keylen = 0;
|
|
|
|
if (keylen < algo->keymin || algo->keymax < keylen) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_mature: invalid ESP key length %d "
|
|
|
|
"(%d-%d allowed)\n",
|
|
|
|
keylen, algo->keymin, algo->keymax));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (algo->mature) {
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((*algo->mature)(sav)) {
|
|
|
|
/* message generated in per-algorithm function*/
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
} else
|
|
|
|
mature = SADB_SATYPE_ESP;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((mustmask & 1) != 0 && mature != SADB_SATYPE_ESP) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: no satisfy algorithm for ESP\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
#else /*IPSEC_ESP*/
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: ESP not supported in this configuration\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check compression algorithm */
|
|
|
|
if ((checkmask & 4) != 0) {
|
2000-09-26 12:40:23 +04:00
|
|
|
const struct ipcomp_algorithm *algo;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* algorithm-dependent check */
|
2000-09-26 12:40:23 +04:00
|
|
|
algo = ipcomp_algorithm_lookup(sav->alg_enc);
|
|
|
|
if (!algo) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_mature: unknown compression algorithm.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_MATURE);
|
2003-09-12 15:09:32 +04:00
|
|
|
return (0);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subroutine for SADB_GET and SADB_DUMP.
|
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *
|
|
|
|
key_setdumpsa(sav, type, satype, seq, pid)
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav;
|
|
|
|
u_int8_t type, satype;
|
|
|
|
u_int32_t seq, pid;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *result = NULL, *tres = NULL, *m;
|
|
|
|
int l = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
int i;
|
2000-06-12 14:40:37 +04:00
|
|
|
void *p;
|
|
|
|
int dumporder[] = {
|
|
|
|
SADB_EXT_SA, SADB_X_EXT_SA2,
|
|
|
|
SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
|
|
|
|
SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
|
|
|
|
SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
|
|
|
|
SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
|
|
|
|
SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
|
|
|
|
};
|
|
|
|
|
|
|
|
m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
|
|
|
|
if (m == NULL)
|
|
|
|
goto fail;
|
|
|
|
result = m;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-15 16:37:07 +04:00
|
|
|
for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
|
2000-06-12 14:40:37 +04:00
|
|
|
m = NULL;
|
|
|
|
p = NULL;
|
|
|
|
switch (dumporder[i]) {
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_EXT_SA:
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_setsadbsa(sav);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_X_EXT_SA2:
|
|
|
|
m = key_setsadbxsa2(sav->sah->saidx.mode,
|
2003-09-07 19:59:36 +04:00
|
|
|
sav->replay ? (sav->replay->count & 0xffffffff) : 0,
|
|
|
|
sav->sah->saidx.reqid);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!m)
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_ADDRESS_SRC:
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
|
|
|
(struct sockaddr *)&sav->sah->saidx.src,
|
2001-10-19 05:57:20 +04:00
|
|
|
FULLMASK, IPSEC_ULPROTO_ANY);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!m)
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_ADDRESS_DST:
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
|
|
|
(struct sockaddr *)&sav->sah->saidx.dst,
|
2001-10-19 05:57:20 +04:00
|
|
|
FULLMASK, IPSEC_ULPROTO_ANY);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!m)
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_KEY_AUTH:
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!sav->key_auth)
|
|
|
|
continue;
|
2000-06-15 16:37:07 +04:00
|
|
|
l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
|
2000-06-12 14:40:37 +04:00
|
|
|
p = sav->key_auth;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_KEY_ENCRYPT:
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!sav->key_enc)
|
|
|
|
continue;
|
2000-06-15 16:37:07 +04:00
|
|
|
l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
|
2000-06-12 14:40:37 +04:00
|
|
|
p = sav->key_enc;
|
|
|
|
break;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
case SADB_EXT_LIFETIME_CURRENT:
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!sav->lft_c)
|
|
|
|
continue;
|
|
|
|
l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
|
|
|
|
p = sav->lft_c;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_LIFETIME_HARD:
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!sav->lft_h)
|
|
|
|
continue;
|
|
|
|
l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
|
|
|
|
p = sav->lft_h;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_LIFETIME_SOFT:
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!sav->lft_s)
|
|
|
|
continue;
|
|
|
|
l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
|
|
|
|
p = sav->lft_s;
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
case SADB_EXT_ADDRESS_PROXY:
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_EXT_IDENTITY_SRC:
|
|
|
|
case SADB_EXT_IDENTITY_DST:
|
2000-01-31 17:18:52 +03:00
|
|
|
/* XXX: should we brought from SPD ? */
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_EXT_SENSITIVITY:
|
|
|
|
default:
|
2000-06-12 14:40:37 +04:00
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((!m && !p) || (m && p))
|
|
|
|
goto fail;
|
|
|
|
if (p && tres) {
|
|
|
|
M_PREPEND(tres, l, M_DONTWAIT);
|
|
|
|
if (!tres)
|
|
|
|
goto fail;
|
|
|
|
bcopy(p, mtod(tres, caddr_t), l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p) {
|
|
|
|
m = key_alloc_mbuf(l);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_copyback(m, 0, l, p);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (tres)
|
|
|
|
m_cat(m, tres);
|
|
|
|
tres = m;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_cat(result, tres);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (result->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
result = m_pullup(result, sizeof(struct sadb_msg));
|
|
|
|
if (result == NULL)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->m_pkthdr.len = 0;
|
|
|
|
for (m = result; m; m = m->m_next)
|
|
|
|
result->m_pkthdr.len += m->m_len;
|
|
|
|
|
|
|
|
mtod(result, struct sadb_msg *)->sadb_msg_len =
|
|
|
|
PFKEY_UNIT64(result->m_pkthdr.len);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
m_freem(result);
|
|
|
|
m_freem(tres);
|
|
|
|
return NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* set data into sadb_msg.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int8_t type, satype;
|
|
|
|
u_int16_t tlen;
|
|
|
|
u_int32_t seq;
|
|
|
|
pid_t pid;
|
2000-06-12 14:40:37 +04:00
|
|
|
u_int16_t reserved;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct sadb_msg *p;
|
2000-06-12 14:40:37 +04:00
|
|
|
int len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
|
|
|
if (len > MCLBYTES)
|
|
|
|
return NULL;
|
|
|
|
MGETHDR(m, M_DONTWAIT, MT_DATA);
|
|
|
|
if (m && len > MHLEN) {
|
|
|
|
MCLGET(m, M_DONTWAIT);
|
|
|
|
if ((m->m_flags & M_EXT) == 0) {
|
|
|
|
m_freem(m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
m->m_pkthdr.len = m->m_len = len;
|
|
|
|
m->m_next = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
p = mtod(m, struct sadb_msg *);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_msg_version = PF_KEY_V2;
|
|
|
|
p->sadb_msg_type = type;
|
|
|
|
p->sadb_msg_errno = 0;
|
|
|
|
p->sadb_msg_satype = satype;
|
|
|
|
p->sadb_msg_len = PFKEY_UNIT64(tlen);
|
2000-06-12 14:40:37 +04:00
|
|
|
p->sadb_msg_reserved = reserved;
|
2000-01-31 17:18:52 +03:00
|
|
|
p->sadb_msg_seq = seq;
|
|
|
|
p->sadb_msg_pid = (u_int32_t)pid;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return m;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* copy secasvar data into sadb_address.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbsa(sav)
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct sadb_sa *p;
|
2000-06-12 14:40:37 +04:00
|
|
|
int len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
p = mtod(m, struct sadb_sa *);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_sa_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_sa_exttype = SADB_EXT_SA;
|
|
|
|
p->sadb_sa_spi = sav->spi;
|
|
|
|
p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
|
|
|
|
p->sadb_sa_state = sav->state;
|
|
|
|
p->sadb_sa_auth = sav->alg_auth;
|
|
|
|
p->sadb_sa_encrypt = sav->alg_enc;
|
|
|
|
p->sadb_sa_flags = sav->flags;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return m;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* set data into sadb_address.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int16_t exttype;
|
|
|
|
struct sockaddr *saddr;
|
|
|
|
u_int8_t prefixlen;
|
|
|
|
u_int16_t ul_proto;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct sadb_address *p;
|
|
|
|
size_t len;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
|
|
|
|
PFKEY_ALIGN8(saddr->sa_len);
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = mtod(m, struct sadb_address *);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_address_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_address_exttype = exttype;
|
|
|
|
p->sadb_address_proto = ul_proto;
|
2001-10-19 05:57:20 +04:00
|
|
|
if (prefixlen == FULLMASK) {
|
|
|
|
switch (saddr->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
prefixlen = sizeof(struct in_addr) << 3;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
prefixlen = sizeof(struct in6_addr) << 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
; /*XXX*/
|
|
|
|
}
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
p->sadb_address_prefixlen = prefixlen;
|
|
|
|
p->sadb_address_reserved = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
bcopy(saddr,
|
|
|
|
mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
|
|
|
|
saddr->sa_len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return m;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-07-01 05:01:34 +04:00
|
|
|
#if 0
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* set data into sadb_ident.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbident(exttype, idtype, string, stringlen, id)
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int16_t exttype, idtype;
|
|
|
|
caddr_t string;
|
|
|
|
int stringlen;
|
|
|
|
u_int64_t id;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct sadb_ident *p;
|
2000-06-12 14:40:37 +04:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_ident)) + PFKEY_ALIGN8(stringlen);
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
p = mtod(m, struct sadb_ident *);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_ident_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_ident_exttype = exttype;
|
|
|
|
p->sadb_ident_type = idtype;
|
|
|
|
p->sadb_ident_reserved = 0;
|
|
|
|
p->sadb_ident_id = id;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
bcopy(string,
|
|
|
|
mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_ident)),
|
|
|
|
stringlen);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return m;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-07-01 05:01:34 +04:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
2000-06-12 14:40:37 +04:00
|
|
|
* set data into sadb_x_sa2.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *
|
2001-08-02 16:10:14 +04:00
|
|
|
key_setsadbxsa2(mode, seq, reqid)
|
2000-06-12 14:40:37 +04:00
|
|
|
u_int8_t mode;
|
2001-08-02 16:10:14 +04:00
|
|
|
u_int32_t seq, reqid;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_x_sa2 *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = mtod(m, struct sadb_x_sa2 *);
|
|
|
|
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_x_sa2_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
|
|
|
|
p->sadb_x_sa2_mode = mode;
|
|
|
|
p->sadb_x_sa2_reserved1 = 0;
|
|
|
|
p->sadb_x_sa2_reserved2 = 0;
|
2001-08-02 16:10:14 +04:00
|
|
|
p->sadb_x_sa2_sequence = seq;
|
2000-06-12 14:40:37 +04:00
|
|
|
p->sadb_x_sa2_reqid = reqid;
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
/*
|
|
|
|
* set data into sadb_x_tag.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbxtag(tag)
|
|
|
|
u_int16_t tag;
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_x_tag *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_x_tag));
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = mtod(m, struct sadb_x_tag *);
|
|
|
|
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_x_tag_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_x_tag_exttype = SADB_X_EXT_TAG;
|
|
|
|
m_nametag_tag2tagname(tag, p->sadb_x_tag_name);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-05-19 12:22:12 +04:00
|
|
|
/*
|
|
|
|
* set data into sadb_lifetime
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_setsadblifetime(type, alloc, bytes, addtime, usetime)
|
|
|
|
u_int16_t type;
|
|
|
|
u_int32_t alloc;
|
|
|
|
u_int64_t bytes, addtime, usetime;
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_lifetime *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = mtod(m, struct sadb_lifetime *);
|
|
|
|
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_lifetime_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_lifetime_exttype = type;
|
|
|
|
p->sadb_lifetime_allocations = alloc;
|
|
|
|
p->sadb_lifetime_bytes = bytes;
|
|
|
|
p->sadb_lifetime_addtime = addtime;
|
|
|
|
p->sadb_lifetime_usetime = usetime;
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/*
|
|
|
|
* set data into sadb_x_policy
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbxpolicy(type, dir, id)
|
|
|
|
u_int16_t type;
|
|
|
|
u_int8_t dir;
|
|
|
|
u_int32_t id;
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_x_policy *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
p = mtod(m, struct sadb_x_policy *);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_x_policy_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
|
|
|
|
p->sadb_x_policy_type = type;
|
|
|
|
p->sadb_x_policy_dir = dir;
|
|
|
|
p->sadb_x_policy_id = id;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return m;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* %%% utilities */
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* copy a buffer into the new buffer allocated.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
static void *
|
|
|
|
key_newbuf(src, len)
|
2000-06-12 14:40:37 +04:00
|
|
|
const void *src;
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int len;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
caddr_t new;
|
|
|
|
|
|
|
|
KMALLOC(new, caddr_t, len);
|
|
|
|
if (new == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_newbuf: No more memory.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
bcopy(src, new, len);
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
return new;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compare my own address
|
|
|
|
* OUT: 1: true, i.e. my address.
|
|
|
|
* 0: false
|
|
|
|
*/
|
2002-06-12 07:46:16 +04:00
|
|
|
static int
|
2000-06-12 14:40:37 +04:00
|
|
|
key_ismyaddr(sa)
|
|
|
|
struct sockaddr *sa;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
#ifdef INET
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
struct in_ifaddr *ia;
|
|
|
|
#endif
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sa == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_ismyaddr: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
#ifdef INET
|
1999-06-28 10:36:47 +04:00
|
|
|
case AF_INET:
|
2000-06-12 14:40:37 +04:00
|
|
|
sin = (struct sockaddr_in *)sa;
|
2003-11-12 00:41:11 +03:00
|
|
|
TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sin->sin_family == ia->ia_addr.sin_family &&
|
|
|
|
sin->sin_len == ia->ia_addr.sin_len &&
|
|
|
|
sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
|
|
|
|
{
|
1999-06-28 10:36:47 +04:00
|
|
|
return 1;
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_ismyaddr6((struct sockaddr_in6 *)sa);
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
/*
|
|
|
|
* compare my own address for IPv6.
|
|
|
|
* 1: ours
|
|
|
|
* 0: other
|
|
|
|
* NOTE: derived ip6_input() in KAME. This is necessary to modify more.
|
|
|
|
*/
|
|
|
|
#include <netinet6/in6_var.h>
|
|
|
|
|
|
|
|
static int
|
2000-06-12 14:40:37 +04:00
|
|
|
key_ismyaddr6(sin6)
|
|
|
|
struct sockaddr_in6 *sin6;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct in6_ifaddr *ia;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct in6_multi *in6m;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
|
2000-06-12 14:40:37 +04:00
|
|
|
if (key_sockaddrcmp((struct sockaddr *)&sin6,
|
|
|
|
(struct sockaddr *)&ia->ia_addr, 0) == 0)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 1;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/*
|
|
|
|
* XXX Multicast
|
|
|
|
* XXX why do we care about multlicast here while we don't care
|
|
|
|
* about IPv4 multicast??
|
|
|
|
* XXX scope
|
|
|
|
*/
|
|
|
|
in6m = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
for ((in6m) = ia->ia6_multiaddrs.lh_first;
|
|
|
|
(in6m) != NULL &&
|
2000-06-12 14:40:37 +04:00
|
|
|
!IN6_ARE_ADDR_EQUAL(&(in6m)->in6m_addr, &sin6->sin6_addr);
|
1999-06-28 10:36:47 +04:00
|
|
|
(in6m) = in6m->in6m_entry.le_next)
|
|
|
|
continue;
|
|
|
|
if (in6m)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loopback, just for safety */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
|
1999-06-28 10:36:47 +04:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1999-07-31 22:41:15 +04:00
|
|
|
#endif /*INET6*/
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* compare two secasindex structure exactly.
|
|
|
|
* IN:
|
|
|
|
* saidx0: source, it can be in SAD.
|
|
|
|
* saidx1: object.
|
|
|
|
* OUT:
|
|
|
|
* 1 : equal
|
|
|
|
* 0 : not equal
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_cmpsaidx_exactly(saidx0, saidx1)
|
|
|
|
struct secasindex *saidx0, *saidx1;
|
|
|
|
{
|
|
|
|
/* sanity */
|
|
|
|
if (saidx0 == NULL && saidx1 == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (saidx0 == NULL || saidx1 == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2003-07-22 15:12:15 +04:00
|
|
|
if (saidx0->proto != saidx1->proto || saidx0->mode != saidx1->mode ||
|
|
|
|
saidx0->reqid != saidx1->reqid)
|
2000-01-31 17:18:52 +03:00
|
|
|
return 0;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.ss_len) != 0 ||
|
|
|
|
bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.ss_len) != 0)
|
2000-01-31 17:18:52 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* compare two secasindex structure with consideration mode.
|
|
|
|
* don't compare port.
|
1999-06-28 10:36:47 +04:00
|
|
|
* IN:
|
2000-01-31 17:18:52 +03:00
|
|
|
* saidx0: source, it is often in SAD.
|
|
|
|
* saidx1: object, it is often from SPD.
|
1999-06-28 10:36:47 +04:00
|
|
|
* OUT:
|
|
|
|
* 1 : equal
|
|
|
|
* 0 : not equal
|
|
|
|
*/
|
|
|
|
static int
|
2000-01-31 17:18:52 +03:00
|
|
|
key_cmpsaidx_withmode(saidx0, saidx1)
|
|
|
|
struct secasindex *saidx0, *saidx1;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
/* sanity */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (saidx0 == NULL && saidx1 == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 1;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (saidx0 == NULL || saidx1 == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (saidx0->proto != saidx1->proto)
|
2000-01-31 17:18:52 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If reqid of SPD is non-zero, unique SA is required.
|
|
|
|
* The result must be of same reqid in this case.
|
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
|
2000-01-31 17:18:52 +03:00
|
|
|
return 0;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (saidx0->mode != IPSEC_MODE_ANY && saidx0->mode != saidx1->mode)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (key_sockaddrcmp((struct sockaddr *)&saidx0->src,
|
|
|
|
(struct sockaddr *)&saidx1->src, 0) != 0) {
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
|
|
|
if (key_sockaddrcmp((struct sockaddr *)&saidx0->dst,
|
|
|
|
(struct sockaddr *)&saidx1->dst, 0) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-07-01 05:01:34 +04:00
|
|
|
/*
|
|
|
|
* compare two secasindex structure without mode.
|
|
|
|
* don't compare port.
|
|
|
|
* IN:
|
|
|
|
* saidx0: source, it is often in SAD.
|
|
|
|
* saidx1: object, it is often from user.
|
|
|
|
* OUT:
|
|
|
|
* 1 : equal
|
|
|
|
* 0 : not equal
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_cmpsaidx_withoutmode(saidx0, saidx1)
|
|
|
|
struct secasindex *saidx0, *saidx1;
|
|
|
|
{
|
|
|
|
/* sanity */
|
|
|
|
if (saidx0 == NULL && saidx1 == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (saidx0 == NULL || saidx1 == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (saidx0->proto != saidx1->proto)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (key_sockaddrcmp((struct sockaddr *)&saidx0->src,
|
|
|
|
(struct sockaddr *)&saidx1->src, 0) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (key_sockaddrcmp((struct sockaddr *)&saidx0->dst,
|
|
|
|
(struct sockaddr *)&saidx1->dst, 0) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* compare two secindex structure exactly.
|
|
|
|
* IN:
|
|
|
|
* spidx0: source, it is often in SPD.
|
|
|
|
* spidx1: object, it is often from PFKEY message.
|
|
|
|
* OUT:
|
|
|
|
* 1 : equal
|
|
|
|
* 0 : not equal
|
|
|
|
*/
|
2001-08-06 14:25:00 +04:00
|
|
|
int
|
2000-01-31 17:18:52 +03:00
|
|
|
key_cmpspidx_exactly(spidx0, spidx1)
|
|
|
|
struct secpolicyindex *spidx0, *spidx1;
|
|
|
|
{
|
|
|
|
/* sanity */
|
|
|
|
if (spidx0 == NULL && spidx1 == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (spidx0 == NULL || spidx1 == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2002-06-12 05:47:34 +04:00
|
|
|
if (spidx0->prefs != spidx1->prefs || spidx0->prefd != spidx1->prefd ||
|
|
|
|
spidx0->ul_proto != spidx1->ul_proto)
|
2000-01-31 17:18:52 +03:00
|
|
|
return 0;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (key_sockaddrcmp((struct sockaddr *)&spidx0->src,
|
|
|
|
(struct sockaddr *)&spidx1->src, 1) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (key_sockaddrcmp((struct sockaddr *)&spidx0->dst,
|
|
|
|
(struct sockaddr *)&spidx1->dst, 1) != 0) {
|
2000-01-31 17:18:52 +03:00
|
|
|
return 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* compare two secindex structure with mask.
|
|
|
|
* IN:
|
2000-01-31 17:18:52 +03:00
|
|
|
* spidx0: source, it is often in SPD.
|
|
|
|
* spidx1: object, it is often from IP header.
|
1999-06-28 10:36:47 +04:00
|
|
|
* OUT:
|
|
|
|
* 1 : equal
|
|
|
|
* 0 : not equal
|
|
|
|
*/
|
2001-08-06 14:25:00 +04:00
|
|
|
int
|
2000-01-31 17:18:52 +03:00
|
|
|
key_cmpspidx_withmask(spidx0, spidx1)
|
|
|
|
struct secpolicyindex *spidx0, *spidx1;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
/* sanity */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (spidx0 == NULL && spidx1 == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 1;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (spidx0 == NULL || spidx1 == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (spidx0->src.ss_family != spidx1->src.ss_family ||
|
|
|
|
spidx0->dst.ss_family != spidx1->dst.ss_family ||
|
|
|
|
spidx0->src.ss_len != spidx1->src.ss_len ||
|
|
|
|
spidx0->dst.ss_len != spidx1->dst.ss_len)
|
2000-01-31 17:18:52 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
|
2003-07-22 15:12:15 +04:00
|
|
|
if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY &&
|
|
|
|
spidx0->ul_proto != spidx1->ul_proto)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
switch (spidx0->src.ss_family) {
|
|
|
|
case AF_INET:
|
2003-07-22 15:12:15 +04:00
|
|
|
if (satosin(&spidx0->src)->sin_port != IPSEC_PORT_ANY &&
|
|
|
|
satosin(&spidx0->src)->sin_port !=
|
2000-06-12 14:40:37 +04:00
|
|
|
satosin(&spidx1->src)->sin_port)
|
|
|
|
return 0;
|
|
|
|
if (!key_bbcmp((caddr_t)&satosin(&spidx0->src)->sin_addr,
|
|
|
|
(caddr_t)&satosin(&spidx1->src)->sin_addr, spidx0->prefs))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2003-07-22 15:12:15 +04:00
|
|
|
if (satosin6(&spidx0->src)->sin6_port != IPSEC_PORT_ANY &&
|
|
|
|
satosin6(&spidx0->src)->sin6_port !=
|
2000-06-12 14:40:37 +04:00
|
|
|
satosin6(&spidx1->src)->sin6_port)
|
|
|
|
return 0;
|
2001-08-06 14:25:00 +04:00
|
|
|
/*
|
|
|
|
* scope_id check. if sin6_scope_id is 0, we regard it
|
|
|
|
* as a wildcard scope, which matches any scope zone ID.
|
|
|
|
*/
|
|
|
|
if (satosin6(&spidx0->src)->sin6_scope_id &&
|
|
|
|
satosin6(&spidx1->src)->sin6_scope_id &&
|
|
|
|
satosin6(&spidx0->src)->sin6_scope_id !=
|
2000-06-12 14:40:37 +04:00
|
|
|
satosin6(&spidx1->src)->sin6_scope_id)
|
|
|
|
return 0;
|
|
|
|
if (!key_bbcmp((caddr_t)&satosin6(&spidx0->src)->sin6_addr,
|
|
|
|
(caddr_t)&satosin6(&spidx1->src)->sin6_addr, spidx0->prefs))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* XXX */
|
|
|
|
if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.ss_len) != 0)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
switch (spidx0->dst.ss_family) {
|
|
|
|
case AF_INET:
|
2003-07-22 15:12:15 +04:00
|
|
|
if (satosin(&spidx0->dst)->sin_port != IPSEC_PORT_ANY &&
|
|
|
|
satosin(&spidx0->dst)->sin_port !=
|
2000-06-12 14:40:37 +04:00
|
|
|
satosin(&spidx1->dst)->sin_port)
|
|
|
|
return 0;
|
|
|
|
if (!key_bbcmp((caddr_t)&satosin(&spidx0->dst)->sin_addr,
|
|
|
|
(caddr_t)&satosin(&spidx1->dst)->sin_addr, spidx0->prefd))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2003-07-22 15:12:15 +04:00
|
|
|
if (satosin6(&spidx0->dst)->sin6_port != IPSEC_PORT_ANY &&
|
|
|
|
satosin6(&spidx0->dst)->sin6_port !=
|
2000-06-12 14:40:37 +04:00
|
|
|
satosin6(&spidx1->dst)->sin6_port)
|
|
|
|
return 0;
|
2001-08-06 14:25:00 +04:00
|
|
|
/*
|
|
|
|
* scope_id check. if sin6_scope_id is 0, we regard it
|
|
|
|
* as a wildcard scope, which matches any scope zone ID.
|
|
|
|
*/
|
|
|
|
if (satosin6(&spidx0->src)->sin6_scope_id &&
|
|
|
|
satosin6(&spidx1->src)->sin6_scope_id &&
|
|
|
|
satosin6(&spidx0->dst)->sin6_scope_id !=
|
2000-06-12 14:40:37 +04:00
|
|
|
satosin6(&spidx1->dst)->sin6_scope_id)
|
|
|
|
return 0;
|
|
|
|
if (!key_bbcmp((caddr_t)&satosin6(&spidx0->dst)->sin6_addr,
|
|
|
|
(caddr_t)&satosin6(&spidx1->dst)->sin6_addr, spidx0->prefd))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* XXX */
|
|
|
|
if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.ss_len) != 0)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX Do we check other field ? e.g. flowinfo */
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return 1;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* returns 0 on match */
|
|
|
|
static int
|
|
|
|
key_sockaddrcmp(sa1, sa2, port)
|
|
|
|
struct sockaddr *sa1;
|
|
|
|
struct sockaddr *sa2;
|
|
|
|
int port;
|
|
|
|
{
|
|
|
|
if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
|
|
|
|
return 1;
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
switch (sa1->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (sa1->sa_len != sizeof(struct sockaddr_in))
|
|
|
|
return 1;
|
|
|
|
if (satosin(sa1)->sin_addr.s_addr !=
|
|
|
|
satosin(sa2)->sin_addr.s_addr) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (sa1->sa_len != sizeof(struct sockaddr_in6))
|
|
|
|
return 1; /*EINVAL*/
|
|
|
|
if (satosin6(sa1)->sin6_scope_id !=
|
|
|
|
satosin6(sa2)->sin6_scope_id) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
|
|
|
|
&satosin6(sa2)->sin6_addr)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (port &&
|
|
|
|
satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (bcmp(sa1, sa2, sa1->sa_len) != 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compare two buffers with mask.
|
|
|
|
* IN:
|
|
|
|
* addr1: source
|
|
|
|
* addr2: object
|
|
|
|
* bits: Number of bits to compare
|
|
|
|
* OUT:
|
|
|
|
* 1 : equal
|
|
|
|
* 0 : not equal
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_bbcmp(p1, p2, bits)
|
2000-03-30 16:51:13 +04:00
|
|
|
caddr_t p1, p2;
|
|
|
|
u_int bits;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
u_int8_t mask;
|
|
|
|
|
|
|
|
/* XXX: This could be considerably faster if we compare a word
|
|
|
|
* at a time, but it is complicated on LSB Endian machines */
|
|
|
|
|
|
|
|
/* Handle null pointers */
|
|
|
|
if (p1 == NULL || p2 == NULL)
|
|
|
|
return (p1 == p2);
|
|
|
|
|
|
|
|
while (bits >= 8) {
|
|
|
|
if (*p1++ != *p2++)
|
|
|
|
return 0;
|
|
|
|
bits -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bits > 0) {
|
|
|
|
mask = ~((1<<(8-bits))-1);
|
|
|
|
if ((*p1 & mask) != (*p2 & mask))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1; /* Match! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* time handler.
|
2000-01-31 17:18:52 +03:00
|
|
|
* scanning SPD and SAD to check status for each entries,
|
1999-06-28 10:36:47 +04:00
|
|
|
* and do to remove or to expire.
|
2000-09-22 20:55:04 +04:00
|
|
|
* XXX: year 2038 problem may remain.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
void
|
2002-01-31 10:05:43 +03:00
|
|
|
key_timehandler(arg)
|
|
|
|
void *arg;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int dir;
|
1999-06-28 10:36:47 +04:00
|
|
|
int s;
|
2000-09-22 20:55:04 +04:00
|
|
|
struct timeval tv;
|
|
|
|
|
2002-01-31 09:35:25 +03:00
|
|
|
tv = time;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
1999-07-04 06:01:15 +04:00
|
|
|
s = splsoftnet(); /*called from softclock()*/
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* SPD */
|
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secpolicy *sp, *nextsp;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
|
|
|
for (sp = LIST_FIRST(&sptree[dir]);
|
1999-06-28 10:36:47 +04:00
|
|
|
sp != NULL;
|
2000-01-31 17:18:52 +03:00
|
|
|
sp = nextsp) {
|
|
|
|
nextsp = LIST_NEXT(sp, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2002-05-19 12:22:12 +04:00
|
|
|
if (sp->state == IPSEC_SPSTATE_DEAD) {
|
|
|
|
key_sp_unlink(sp); /*XXX*/
|
|
|
|
sp = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sp->lifetime == 0 && sp->validtime == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* the deletion will occur next time */
|
|
|
|
if ((sp->lifetime &&
|
|
|
|
tv.tv_sec - sp->created > sp->lifetime) ||
|
|
|
|
(sp->validtime &&
|
|
|
|
tv.tv_sec - sp->lastused > sp->validtime)) {
|
|
|
|
key_sp_dead(sp);
|
|
|
|
key_spdexpire(sp);
|
|
|
|
continue;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SAD */
|
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secashead *sah, *nextsah;
|
|
|
|
struct secasvar *sav, *nextsav;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
for (sah = LIST_FIRST(&sahtree);
|
|
|
|
sah != NULL;
|
|
|
|
sah = nextsah) {
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextsah = LIST_NEXT(sah, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* if sah has been dead, then delete it and process next sah. */
|
|
|
|
if (sah->state == SADB_SASTATE_DEAD) {
|
|
|
|
key_delsah(sah);
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
1999-07-31 22:41:15 +04:00
|
|
|
|
|
|
|
/* if LARVAL entry doesn't become MATURE, delete it. */
|
2000-01-31 17:18:52 +03:00
|
|
|
for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_LARVAL]);
|
|
|
|
sav != NULL;
|
|
|
|
sav = nextsav) {
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextsav = LIST_NEXT(sav, chain);
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-09-22 20:55:04 +04:00
|
|
|
if (tv.tv_sec - sav->created > key_larval_lifetime) {
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesav(sav);
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check MATURE entry to start to send expire message
|
|
|
|
* whether or not.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_MATURE]);
|
|
|
|
sav != NULL;
|
|
|
|
sav = nextsav) {
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextsav = LIST_NEXT(sav, chain);
|
1999-07-31 22:41:15 +04:00
|
|
|
|
|
|
|
/* we don't need to check. */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->lft_s == NULL)
|
1999-07-31 22:41:15 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->lft_c == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_timehandler: "
|
|
|
|
"There is no CURRENT time, why?\n"));
|
1999-07-31 22:41:15 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-09-22 20:55:04 +04:00
|
|
|
/* check SOFT lifetime */
|
2003-07-22 15:12:15 +04:00
|
|
|
if (sav->lft_s->sadb_lifetime_addtime != 0 &&
|
|
|
|
tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
|
1999-07-31 22:41:15 +04:00
|
|
|
/*
|
2003-07-22 15:12:15 +04:00
|
|
|
* check the SA if it has been used.
|
|
|
|
* when it hasn't been used, delete it.
|
|
|
|
* i don't think such SA will be used.
|
1999-07-31 22:41:15 +04:00
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->lft_c->sadb_lifetime_usetime == 0) {
|
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_DEAD);
|
|
|
|
key_freesav(sav);
|
|
|
|
sav = NULL;
|
1999-07-31 22:41:15 +04:00
|
|
|
} else {
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_DYING);
|
1999-07-31 22:41:15 +04:00
|
|
|
/*
|
|
|
|
* XXX If we keep to send expire
|
|
|
|
* message in the status of
|
|
|
|
* DYING. Do remove below code.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
key_expire(sav);
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
|
|
|
}
|
2003-07-22 15:12:15 +04:00
|
|
|
|
1999-07-31 22:41:15 +04:00
|
|
|
/* check SOFT lifetime by bytes */
|
|
|
|
/*
|
|
|
|
* XXX I don't know the way to delete this SA
|
|
|
|
* when new SA is installed. Caution when it's
|
|
|
|
* installed too big lifetime by time.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
else if (sav->lft_s->sadb_lifetime_bytes != 0
|
|
|
|
&& sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_DYING);
|
1999-07-31 22:41:15 +04:00
|
|
|
/*
|
|
|
|
* XXX If we keep to send expire
|
|
|
|
* message in the status of
|
|
|
|
* DYING. Do remove below code.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
key_expire(sav);
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check DYING entry to change status to DEAD. */
|
2000-01-31 17:18:52 +03:00
|
|
|
for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DYING]);
|
|
|
|
sav != NULL;
|
|
|
|
sav = nextsav) {
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextsav = LIST_NEXT(sav, chain);
|
1999-07-31 22:41:15 +04:00
|
|
|
|
|
|
|
/* we don't need to check. */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->lft_h == NULL)
|
1999-07-31 22:41:15 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->lft_c == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,"key_timehandler: "
|
|
|
|
"There is no CURRENT time, why?\n"));
|
1999-07-31 22:41:15 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-07-22 15:12:15 +04:00
|
|
|
if (sav->lft_h->sadb_lifetime_addtime != 0 &&
|
|
|
|
tv.tv_sec - sav->created > sav->lft_h->sadb_lifetime_addtime) {
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_DEAD);
|
|
|
|
key_freesav(sav);
|
|
|
|
sav = NULL;
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
|
|
|
#if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
|
2000-01-31 17:18:52 +03:00
|
|
|
else if (sav->lft_s != NULL
|
|
|
|
&& sav->lft_s->sadb_lifetime_addtime != 0
|
2000-09-22 20:55:04 +04:00
|
|
|
&& tv.tv_sec - sav->created > sav->lft_s->sadb_lifetime_addtime) {
|
1999-07-31 22:41:15 +04:00
|
|
|
/*
|
|
|
|
* XXX: should be checked to be
|
|
|
|
* installed the valid SA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is no SA then sending
|
|
|
|
* expire message.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
key_expire(sav);
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* check HARD lifetime by bytes */
|
2000-01-31 17:18:52 +03:00
|
|
|
else if (sav->lft_h->sadb_lifetime_bytes != 0
|
|
|
|
&& sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
|
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_DEAD);
|
|
|
|
key_freesav(sav);
|
|
|
|
sav = NULL;
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete entry in DEAD */
|
2000-01-31 17:18:52 +03:00
|
|
|
for (sav = LIST_FIRST(&sah->savtree[SADB_SASTATE_DEAD]);
|
|
|
|
sav != NULL;
|
|
|
|
sav = nextsav) {
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextsav = LIST_NEXT(sav, chain);
|
1999-07-31 22:41:15 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->state != SADB_SASTATE_DEAD) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_timehandler: "
|
2000-01-31 17:18:52 +03:00
|
|
|
"invalid sav->state "
|
2002-06-27 18:39:45 +04:00
|
|
|
"(queue: %u SA: %u): "
|
1999-07-31 22:41:15 +04:00
|
|
|
"kill it anyway\n",
|
2002-01-31 09:35:25 +03:00
|
|
|
SADB_SASTATE_DEAD, sav->state));
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* do not call key_freesav() here.
|
|
|
|
* sav should already be freed, and sav->refcnt
|
|
|
|
* shows other references to sav
|
1999-07-31 22:41:15 +04:00
|
|
|
* (such as from SPD).
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
|
|
|
/* ACQ tree */
|
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secacq *acq, *nextacq;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
for (acq = LIST_FIRST(&acqtree);
|
1999-06-28 10:36:47 +04:00
|
|
|
acq != NULL;
|
2000-01-31 17:18:52 +03:00
|
|
|
acq = nextacq) {
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextacq = LIST_NEXT(acq, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-07-22 15:12:15 +04:00
|
|
|
if (tv.tv_sec - acq->created > key_blockacq_lifetime &&
|
|
|
|
__LIST_CHAINED(acq)) {
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_REMOVE(acq, chain);
|
|
|
|
KFREE(acq);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* SP ACQ tree */
|
|
|
|
{
|
|
|
|
struct secspacq *acq, *nextacq;
|
|
|
|
|
|
|
|
for (acq = LIST_FIRST(&spacqtree);
|
|
|
|
acq != NULL;
|
|
|
|
acq = nextacq) {
|
|
|
|
|
|
|
|
nextacq = LIST_NEXT(acq, chain);
|
|
|
|
|
2003-07-22 15:12:15 +04:00
|
|
|
if (tv.tv_sec - acq->created > key_blockacq_lifetime &&
|
|
|
|
__LIST_CHAINED(acq)) {
|
2000-06-12 14:40:37 +04:00
|
|
|
LIST_REMOVE(acq, chain);
|
|
|
|
KFREE(acq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* initialize random seed */
|
|
|
|
if (key_tick_init_random++ > key_int_random) {
|
|
|
|
key_tick_init_random = 0;
|
|
|
|
key_srandom();
|
|
|
|
}
|
|
|
|
|
2002-01-31 10:05:43 +03:00
|
|
|
callout_reset(&key_timehandler_ch, hz, key_timehandler, (void *)0);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* to initialize a seed for random()
|
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static void
|
1999-06-28 10:36:47 +04:00
|
|
|
key_srandom()
|
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-06-12 07:46:16 +04:00
|
|
|
static u_long
|
2000-06-12 14:40:37 +04:00
|
|
|
key_random()
|
|
|
|
{
|
|
|
|
u_long value;
|
|
|
|
|
2000-10-05 08:49:17 +04:00
|
|
|
key_randomfill(&value, sizeof(value));
|
2000-06-12 14:40:37 +04:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2000-10-05 08:49:17 +04:00
|
|
|
void
|
|
|
|
key_randomfill(p, l)
|
|
|
|
void *p;
|
|
|
|
size_t l;
|
|
|
|
{
|
2002-10-07 04:40:15 +04:00
|
|
|
#if NRND == 0
|
2000-10-05 08:49:17 +04:00
|
|
|
static int warn = 1;
|
2002-10-07 04:40:15 +04:00
|
|
|
#endif
|
2000-10-05 08:49:17 +04:00
|
|
|
|
2002-10-06 12:51:44 +04:00
|
|
|
arc4randbytes(p, l);
|
2002-10-07 04:40:15 +04:00
|
|
|
|
|
|
|
#if NRND == 0
|
2002-10-06 12:51:44 +04:00
|
|
|
/* the arc4 generator is keyed with junk. */
|
|
|
|
if (warn) {
|
|
|
|
printf("WARNING: pseudo-random number generator "
|
|
|
|
"used for IPsec processing\n");
|
|
|
|
warn = 0;
|
2000-10-05 08:49:17 +04:00
|
|
|
}
|
2002-10-06 12:51:44 +04:00
|
|
|
#endif
|
2000-10-05 08:49:17 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/*
|
|
|
|
* map SADB_SATYPE_* to IPPROTO_*.
|
|
|
|
* if satype == SADB_SATYPE then satype is mapped to ~0.
|
2000-01-31 17:18:52 +03:00
|
|
|
* OUT:
|
|
|
|
* 0: invalid satype.
|
|
|
|
*/
|
|
|
|
static u_int16_t
|
|
|
|
key_satype2proto(satype)
|
|
|
|
u_int8_t satype;
|
|
|
|
{
|
|
|
|
switch (satype) {
|
|
|
|
case SADB_SATYPE_UNSPEC:
|
|
|
|
return IPSEC_PROTO_ANY;
|
|
|
|
case SADB_SATYPE_AH:
|
|
|
|
return IPPROTO_AH;
|
|
|
|
case SADB_SATYPE_ESP:
|
|
|
|
return IPPROTO_ESP;
|
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
|
|
|
return IPPROTO_IPCOMP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map IPPROTO_* to SADB_SATYPE_*
|
|
|
|
* OUT:
|
|
|
|
* 0: invalid protocol type.
|
|
|
|
*/
|
|
|
|
static u_int8_t
|
|
|
|
key_proto2satype(proto)
|
|
|
|
u_int16_t proto;
|
|
|
|
{
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_AH:
|
|
|
|
return SADB_SATYPE_AH;
|
|
|
|
case IPPROTO_ESP:
|
|
|
|
return SADB_SATYPE_ESP;
|
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
return SADB_X_SATYPE_IPCOMP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* %%% PF_KEY */
|
|
|
|
/*
|
|
|
|
* SADB_GETSPI processing is to receive
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, (SA2), src address, dst address, (SPI range)>
|
1999-06-28 10:36:47 +04:00
|
|
|
* from the IKMPd, to assign a unique spi value, to hang on the INBOUND
|
|
|
|
* tree with the status of LARVAL, and send
|
|
|
|
* <base, SA(*), address(SD)>
|
|
|
|
* to the IKMPd.
|
|
|
|
*
|
|
|
|
* IN: mhp: pointer to the pointer to each header.
|
|
|
|
* OUT: NULL if fail.
|
|
|
|
* other if success, return pointer to the message to send.
|
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_getspi(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct sadb_address *src0, *dst0;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *newsah;
|
|
|
|
struct secasvar *newsav;
|
|
|
|
u_int8_t proto;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int32_t spi;
|
2000-06-12 14:40:37 +04:00
|
|
|
u_int8_t mode;
|
|
|
|
u_int32_t reqid;
|
|
|
|
int error;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_getspi: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_getspi: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
|
|
|
|
mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
|
|
|
|
reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
|
|
|
|
} else {
|
|
|
|
mode = IPSEC_MODE_ANY;
|
|
|
|
reqid = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
|
|
|
|
dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* map satype to proto */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_getspi: invalid satype is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure if port number is zero. */
|
|
|
|
switch (((struct sockaddr *)(src0 + 1))->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (((struct sockaddr *)(src0 + 1))->sa_len !=
|
|
|
|
sizeof(struct sockaddr_in))
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (((struct sockaddr *)(src0 + 1))->sa_len !=
|
|
|
|
sizeof(struct sockaddr_in6))
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
; /*???*/
|
|
|
|
}
|
|
|
|
switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (((struct sockaddr *)(dst0 + 1))->sa_len !=
|
|
|
|
sizeof(struct sockaddr_in))
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (((struct sockaddr *)(dst0 + 1))->sa_len !=
|
|
|
|
sizeof(struct sockaddr_in6))
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
; /*???*/
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX boundary check against sa_len */
|
|
|
|
KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* SPI allocation */
|
2000-06-12 14:40:37 +04:00
|
|
|
spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
|
2000-01-31 17:18:52 +03:00
|
|
|
&saidx);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (spi == 0)
|
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* get a SA index */
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((newsah = key_getsah(&saidx)) == NULL) {
|
1999-07-31 22:41:15 +04:00
|
|
|
/* create a new SA index */
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((newsah = key_newsah(&saidx)) == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_getspi: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* get a new SA */
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX rewrite */
|
|
|
|
newsav = key_newsav(m, mhp, newsah, &error);
|
|
|
|
if (newsav == NULL) {
|
1999-06-28 10:36:47 +04:00
|
|
|
/* XXX don't free new SA index allocated in above. */
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set spi */
|
2003-09-07 19:59:36 +04:00
|
|
|
key_setspi(newsav, htonl(spi));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
|
|
|
/* delete the entry in acqtree */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->msg->sadb_msg_seq != 0) {
|
1999-06-28 10:36:47 +04:00
|
|
|
struct secacq *acq;
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
/* reset counter in order to deletion by timehandler. */
|
|
|
|
acq->created = time.tv_sec;
|
1999-06-28 10:36:47 +04:00
|
|
|
acq->count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n, *nn;
|
|
|
|
struct sadb_sa *m_sa;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sadb_msg *newmsg;
|
2000-06-12 14:40:37 +04:00
|
|
|
int off, len;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2000-06-12 14:40:37 +04:00
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
|
|
|
|
PFKEY_ALIGN8(sizeof(struct sadb_sa));
|
|
|
|
if (len > MCLBYTES)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
MGETHDR(n, M_DONTWAIT, MT_DATA);
|
|
|
|
if (len > MHLEN) {
|
|
|
|
MCLGET(n, M_DONTWAIT);
|
|
|
|
if ((n->m_flags & M_EXT) == 0) {
|
|
|
|
m_freem(n);
|
|
|
|
n = NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
n->m_len = len;
|
|
|
|
n->m_next = NULL;
|
|
|
|
off = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
|
|
|
|
off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
|
|
|
|
|
|
|
m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
|
1999-06-28 10:36:47 +04:00
|
|
|
m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
|
|
|
|
m_sa->sadb_sa_exttype = SADB_EXT_SA;
|
|
|
|
m_sa->sadb_sa_spi = htonl(spi);
|
2000-06-12 14:40:37 +04:00
|
|
|
off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (off != len)
|
|
|
|
panic("length inconsistency in key_getspi");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
|
|
|
|
SADB_EXT_ADDRESS_DST);
|
|
|
|
if (!n->m_next) {
|
|
|
|
m_freem(n);
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
n = m_pullup(n, sizeof(struct sadb_msg));
|
|
|
|
if (n == NULL)
|
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
n->m_pkthdr.len = 0;
|
|
|
|
for (nn = n; nn; nn = nn->m_next)
|
|
|
|
n->m_pkthdr.len += nn->m_len;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg = mtod(n, struct sadb_msg *);
|
|
|
|
newmsg->sadb_msg_seq = newsav->seq;
|
|
|
|
newmsg->sadb_msg_errno = 0;
|
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocating new SPI
|
|
|
|
* called by key_getspi().
|
|
|
|
* OUT:
|
|
|
|
* 0: failure.
|
|
|
|
* others: success.
|
|
|
|
*/
|
|
|
|
static u_int32_t
|
2000-01-31 17:18:52 +03:00
|
|
|
key_do_getnewspi(spirange, saidx)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sadb_spirange *spirange;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasindex *saidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
u_int32_t newspi;
|
|
|
|
u_int32_t min, max;
|
|
|
|
int count = key_spi_trycnt;
|
|
|
|
|
|
|
|
/* set spi range to allocate */
|
|
|
|
if (spirange != NULL) {
|
|
|
|
min = spirange->sadb_spirange_min;
|
|
|
|
max = spirange->sadb_spirange_max;
|
|
|
|
} else {
|
|
|
|
min = key_spi_minval;
|
|
|
|
max = key_spi_maxval;
|
|
|
|
}
|
|
|
|
/* IPCOMP needs 2-byte SPI */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (saidx->proto == IPPROTO_IPCOMP) {
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int32_t t;
|
|
|
|
if (min >= 0x10000)
|
|
|
|
min = 0xffff;
|
|
|
|
if (max >= 0x10000)
|
|
|
|
max = 0xffff;
|
|
|
|
if (min > max) {
|
|
|
|
t = min; min = max; max = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min == max) {
|
2000-01-31 17:18:52 +03:00
|
|
|
if (key_checkspidup(saidx, min) != NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_do_getnewspi: SPI %u exists already.\n", min));
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
count--; /* taking one cost. */
|
|
|
|
newspi = min;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* init SPI */
|
|
|
|
newspi = 0;
|
|
|
|
|
|
|
|
/* when requesting to allocate spi ranged */
|
|
|
|
while (count--) {
|
|
|
|
/* generate pseudo-random SPI value ranged. */
|
2000-06-12 14:40:37 +04:00
|
|
|
newspi = min + (key_random() % (max - min + 1));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (key_checkspidup(saidx, newspi) == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0 || newspi == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_do_getnewspi: to allocate spi is failed.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* statistics */
|
|
|
|
keystat.getspi_count =
|
|
|
|
(keystat.getspi_count + key_spi_trycnt - count) / 2;
|
|
|
|
|
|
|
|
return newspi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_UPDATE processing
|
|
|
|
* receive
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
|
1999-06-28 10:36:47 +04:00
|
|
|
* key(AE), (identity(SD),) (sensitivity)>
|
2000-01-31 17:18:52 +03:00
|
|
|
* from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
|
1999-06-28 10:36:47 +04:00
|
|
|
* and send
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
|
1999-06-28 10:36:47 +04:00
|
|
|
* (identity(SD),) (sensitivity)>
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_update(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct sadb_sa *sa0;
|
|
|
|
struct sadb_address *src0, *dst0;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
|
|
|
u_int16_t proto;
|
2000-06-12 14:40:37 +04:00
|
|
|
u_int8_t mode;
|
|
|
|
u_int32_t reqid;
|
|
|
|
int error;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_update: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* map satype to proto */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_update: invalid satype is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_SA] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
|
|
|
|
(mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
|
|
|
|
mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
|
|
|
|
(mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
|
|
|
|
mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
|
|
|
|
(mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
|
|
|
|
mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
|
|
|
|
(mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
|
|
|
|
mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_update: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
|
|
|
|
mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
|
|
|
|
reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
|
|
|
|
} else {
|
|
|
|
mode = IPSEC_MODE_ANY;
|
|
|
|
reqid = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX boundary checking for other extensions */
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
|
|
|
|
src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
|
|
|
|
dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX boundary check against sa_len */
|
|
|
|
KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* get a SA header */
|
|
|
|
if ((sah = key_getsah(&saidx)) == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_update: no SA index found.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOENT);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* set spidx if there */
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX rewrite */
|
|
|
|
error = key_setident(sah, m, mhp);
|
|
|
|
if (error)
|
|
|
|
return key_senderror(so, m, error);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* find a SA with sequence number. */
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifdef IPSEC_DOSEQCHECK
|
2003-07-22 15:12:15 +04:00
|
|
|
if (mhp->msg->sadb_msg_seq != 0 &&
|
|
|
|
(sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_update: no larval SA with sequence %u exists.\n",
|
|
|
|
mhp->msg->sadb_msg_seq));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOENT);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
#else
|
|
|
|
if ((sav = key_getsavbyspi(sah, sa0->sadb_sa_spi)) == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_update: no such a SA found (spi:%u)\n",
|
|
|
|
(u_int32_t)ntohl(sa0->sadb_sa_spi)));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* validity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->sah->saidx.proto != proto) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_update: protocol mismatched (DB=%u param=%u)\n",
|
|
|
|
sav->sah->saidx.proto, proto));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifdef IPSEC_DOSEQCHECK
|
|
|
|
if (sav->spi != sa0->sadb_sa_spi) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_update: SPI mismatched (DB:%u param:%u)\n",
|
|
|
|
(u_int32_t)ntohl(sav->spi),
|
|
|
|
(u_int32_t)ntohl(sa0->sadb_sa_spi)));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sav->pid != mhp->msg->sadb_msg_pid) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_update: pid mismatched (DB:%u param:%u)\n",
|
|
|
|
sav->pid, mhp->msg->sadb_msg_pid));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* copy sav values */
|
2000-06-12 14:40:37 +04:00
|
|
|
error = key_setsaval(sav, m, mhp);
|
|
|
|
if (error) {
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesav(sav);
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check SA values to be mature. */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesav(sav);
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, 0);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* set msg buf from mhp */
|
2000-06-12 14:40:37 +04:00
|
|
|
n = key_getmsgbuf_x1(m, mhp);
|
|
|
|
if (n == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
|
1999-06-28 10:36:47 +04:00
|
|
|
* only called by key_update().
|
|
|
|
* OUT:
|
|
|
|
* NULL : not found
|
|
|
|
* others : found, pointer to a SA.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifdef IPSEC_DOSEQCHECK
|
|
|
|
static struct secasvar *
|
|
|
|
key_getsavbyseq(sah, seq)
|
|
|
|
struct secashead *sah;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int32_t seq;
|
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int state;
|
|
|
|
|
|
|
|
state = SADB_SASTATE_LARVAL;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* search SAD with sequence number ? */
|
|
|
|
LIST_FOREACH(sav, &sah->savtree[state], chain) {
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
KEY_CHKSASTATE(state, sav->state, "key_getsabyseq");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->seq == seq) {
|
|
|
|
sav->refcnt++;
|
|
|
|
KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
|
|
|
|
printf("DP key_getsavbyseq cause "
|
|
|
|
"refcnt++:%d SA:%p\n",
|
|
|
|
sav->refcnt, sav));
|
|
|
|
return sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_ADD processing
|
2003-07-22 15:12:15 +04:00
|
|
|
* add an entry to SA database, when received
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
|
1999-06-28 10:36:47 +04:00
|
|
|
* key(AE), (identity(SD),) (sensitivity)>
|
|
|
|
* from the ikmpd,
|
|
|
|
* and send
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
|
1999-06-28 10:36:47 +04:00
|
|
|
* (identity(SD),) (sensitivity)>
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
|
|
|
* IGNORE identity and sensitivity messages.
|
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_add(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct sadb_sa *sa0;
|
|
|
|
struct sadb_address *src0, *dst0;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *newsah;
|
|
|
|
struct secasvar *newsav;
|
|
|
|
u_int16_t proto;
|
2000-06-12 14:40:37 +04:00
|
|
|
u_int8_t mode;
|
|
|
|
u_int32_t reqid;
|
|
|
|
int error;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_add: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* map satype to proto */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_add: invalid satype is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_SA] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
|
|
|
|
(mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
|
|
|
|
mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
|
|
|
|
(mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
|
|
|
|
mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
|
|
|
|
(mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
|
|
|
|
mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
|
|
|
|
(mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
|
|
|
|
mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2000-06-24 04:15:52 +04:00
|
|
|
if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
|
2000-06-12 14:40:37 +04:00
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
|
|
|
|
/* XXX need more */
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_add: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
|
|
|
|
mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
|
|
|
|
reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
|
|
|
|
} else {
|
|
|
|
mode = IPSEC_MODE_ANY;
|
|
|
|
reqid = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
|
|
|
|
src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
|
|
|
|
dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX boundary check against sa_len */
|
|
|
|
KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* get a SA header */
|
|
|
|
if ((newsah = key_getsah(&saidx)) == NULL) {
|
|
|
|
/* create a new SA header */
|
|
|
|
if ((newsah = key_newsah(&saidx)) == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_add: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set spidx if there */
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX rewrite */
|
|
|
|
error = key_setident(newsah, m, mhp);
|
|
|
|
if (error) {
|
|
|
|
return key_senderror(so, m, error);
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* create new SA entry. */
|
|
|
|
/* We can create new SA only if SPI is differenct. */
|
|
|
|
if (key_getsavbyspi(newsah, sa0->sadb_sa_spi)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_add: SA already exists.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EEXIST);
|
|
|
|
}
|
|
|
|
newsav = key_newsav(m, mhp, newsah, &error);
|
|
|
|
if (newsav == NULL) {
|
|
|
|
return key_senderror(so, m, error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* check SA values to be mature. */
|
2000-06-24 04:15:52 +04:00
|
|
|
if ((error = key_mature(newsav)) != 0) {
|
2000-01-31 17:18:52 +03:00
|
|
|
key_freesav(newsav);
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, error);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* don't call key_freesav() here, as we would like to keep the SA
|
|
|
|
* in the database on success.
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n;
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* set msg buf from mhp */
|
2000-06-12 14:40:37 +04:00
|
|
|
n = key_getmsgbuf_x1(m, mhp);
|
|
|
|
if (n == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_update: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* m is retained */
|
2000-01-31 17:18:52 +03:00
|
|
|
static int
|
2000-06-12 14:40:37 +04:00
|
|
|
key_setident(sah, m, mhp)
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secashead *sah;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
2000-01-31 17:18:52 +03:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
const struct sadb_ident *idsrc, *iddst;
|
2000-01-31 17:18:52 +03:00
|
|
|
int idsrclen, iddstlen;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (sah == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_setident: NULL pointer is passed.");
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* don't make buffer if not there */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
|
|
|
|
mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
sah->idents = NULL;
|
|
|
|
sah->identd = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setident: invalid identity.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
1999-07-31 22:41:15 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
|
|
|
|
iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
|
|
|
|
idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
|
|
|
|
iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* validity check */
|
|
|
|
if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setident: ident type mismatch.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return EINVAL;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (idsrc->sadb_ident_type) {
|
|
|
|
case SADB_IDENTTYPE_PREFIX:
|
|
|
|
case SADB_IDENTTYPE_FQDN:
|
|
|
|
case SADB_IDENTTYPE_USERFQDN:
|
|
|
|
default:
|
|
|
|
/* XXX do nothing */
|
|
|
|
sah->idents = NULL;
|
|
|
|
sah->identd = NULL;
|
|
|
|
return 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* make structure */
|
|
|
|
KMALLOC(sah->idents, struct sadb_ident *, idsrclen);
|
|
|
|
if (sah->idents == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return ENOBUFS;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
KMALLOC(sah->identd, struct sadb_ident *, iddstlen);
|
|
|
|
if (sah->identd == NULL) {
|
|
|
|
KFREE(sah->idents);
|
2000-09-20 04:08:42 +04:00
|
|
|
sah->idents = NULL;
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_setident: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return ENOBUFS;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
bcopy(idsrc, sah->idents, idsrclen);
|
|
|
|
bcopy(iddst, sah->identd, iddstlen);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
return 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/*
|
|
|
|
* m will not be freed on return.
|
|
|
|
* it is caller's responsibility to free the result.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_getmsgbuf_x1(m, mhp)
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_getmsgbuf_x1: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2000-06-12 14:40:37 +04:00
|
|
|
n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
|
|
|
|
SADB_EXT_SA, SADB_X_EXT_SA2,
|
|
|
|
SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
|
|
|
|
SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
|
|
|
|
SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
|
|
|
|
if (!n)
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (n->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
n = m_pullup(n, sizeof(struct sadb_msg));
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
|
|
|
|
mtod(n, struct sadb_msg *)->sadb_msg_len =
|
|
|
|
PFKEY_UNIT64(n->m_pkthdr.len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return n;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2001-02-17 02:53:59 +03:00
|
|
|
static int key_delete_all __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *, u_int16_t));
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* SADB_DELETE processing
|
|
|
|
* receive
|
|
|
|
* <base, SA(*), address(SD)>
|
|
|
|
* from the ikmpd, and set SADB_SASTATE_DEAD,
|
|
|
|
* and send,
|
|
|
|
* <base, SA(*), address(SD)>
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_delete(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct sadb_sa *sa0;
|
|
|
|
struct sadb_address *src0, *dst0;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *sah;
|
2000-07-01 05:01:34 +04:00
|
|
|
struct secasvar *sav = NULL;
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int16_t proto;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_delete: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* map satype to proto */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_delete: invalid satype is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
2001-02-17 02:53:59 +03:00
|
|
|
if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
|
2000-06-12 14:40:37 +04:00
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2001-02-17 02:53:59 +03:00
|
|
|
|
|
|
|
if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
|
2000-06-12 14:40:37 +04:00
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2001-02-17 02:53:59 +03:00
|
|
|
if (mhp->ext[SADB_EXT_SA] == NULL) {
|
|
|
|
/*
|
|
|
|
* Caller wants us to delete all non-LARVAL SAs
|
|
|
|
* that match the src/dst. This is used during
|
|
|
|
* IKE INITIAL-CONTACT.
|
|
|
|
*/
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_delete: doing delete all.\n"));
|
2001-02-17 02:53:59 +03:00
|
|
|
return key_delete_all(so, m, mhp, proto);
|
|
|
|
} else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_delete: invalid message is passed.\n"));
|
2001-02-17 02:53:59 +03:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
|
|
|
|
src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
|
|
|
|
dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
|
|
|
|
|
|
|
|
/* XXX boundary check against sa_len */
|
|
|
|
KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* get a SA header */
|
2000-06-12 14:40:37 +04:00
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
if (sah->state == SADB_SASTATE_DEAD)
|
|
|
|
continue;
|
2000-07-01 05:01:34 +04:00
|
|
|
if (key_cmpsaidx_withoutmode(&sah->saidx, &saidx) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* get a SA with SPI. */
|
|
|
|
sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
|
|
|
|
if (sav)
|
2000-06-12 14:40:37 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sah == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_delete: no SA found.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOENT);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_DEAD);
|
|
|
|
key_freesav(sav);
|
|
|
|
sav = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2000-06-12 14:40:37 +04:00
|
|
|
n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
|
|
|
|
SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
|
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (n->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
n = m_pullup(n, sizeof(struct sadb_msg));
|
2001-02-17 02:53:59 +03:00
|
|
|
if (n == NULL)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
newmsg = mtod(n, struct sadb_msg *);
|
|
|
|
newmsg->sadb_msg_errno = 0;
|
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* delete all SAs for src/dst. Called from key_delete().
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_delete_all(so, m, mhp, proto)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
|
|
|
u_int16_t proto;
|
|
|
|
{
|
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav, *nextsav;
|
|
|
|
u_int stateidx, state;
|
|
|
|
|
|
|
|
src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
|
|
|
|
dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
|
|
|
|
|
|
|
|
/* XXX boundary check against sa_len */
|
|
|
|
KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
|
|
|
|
|
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
if (sah->state == SADB_SASTATE_DEAD)
|
|
|
|
continue;
|
|
|
|
if (key_cmpsaidx_withoutmode(&sah->saidx, &saidx) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Delete all non-LARVAL SAs. */
|
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_alive);
|
|
|
|
stateidx++) {
|
|
|
|
state = saorder_state_alive[stateidx];
|
|
|
|
if (state == SADB_SASTATE_LARVAL)
|
|
|
|
continue;
|
|
|
|
for (sav = LIST_FIRST(&sah->savtree[state]);
|
|
|
|
sav != NULL; sav = nextsav) {
|
|
|
|
nextsav = LIST_NEXT(sav, chain);
|
|
|
|
/* sanity check */
|
|
|
|
if (sav->state != state) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_delete_all: "
|
2001-02-17 02:53:59 +03:00
|
|
|
"invalid sav->state "
|
2002-06-27 18:39:45 +04:00
|
|
|
"(queue: %u SA: %u)\n",
|
2002-01-31 09:35:25 +03:00
|
|
|
state, sav->state));
|
2001-02-17 02:53:59 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_DEAD);
|
|
|
|
key_freesav(sav);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
|
|
|
|
SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
|
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
|
|
|
|
if (n->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
n = m_pullup(n, sizeof(struct sadb_msg));
|
2000-06-12 14:40:37 +04:00
|
|
|
if (n == NULL)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg = mtod(n, struct sadb_msg *);
|
1999-06-28 10:36:47 +04:00
|
|
|
newmsg->sadb_msg_errno = 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_GET processing
|
|
|
|
* receive
|
|
|
|
* <base, SA(*), address(SD)>
|
|
|
|
* from the ikmpd, and get a SP and a SA to respond,
|
|
|
|
* and send,
|
|
|
|
* <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
|
|
|
|
* (identity(SD),) (sensitivity)>
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_get(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct sadb_sa *sa0;
|
|
|
|
struct sadb_address *src0, *dst0;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *sah;
|
2000-07-01 05:01:34 +04:00
|
|
|
struct secasvar *sav = NULL;
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int16_t proto;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_get: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* map satype to proto */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_get: invalid satype is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_SA] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_get: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
|
|
|
|
src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
|
|
|
|
dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
|
|
|
|
|
|
|
|
/* XXX boundary check against sa_len */
|
|
|
|
KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* get a SA header */
|
2000-06-12 14:40:37 +04:00
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
if (sah->state == SADB_SASTATE_DEAD)
|
|
|
|
continue;
|
2000-07-01 05:01:34 +04:00
|
|
|
if (key_cmpsaidx_withoutmode(&sah->saidx, &saidx) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* get a SA with SPI. */
|
|
|
|
sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
|
|
|
|
if (sav)
|
2000-06-12 14:40:37 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sah == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_get: no SA found.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOENT);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n;
|
2000-01-31 17:18:52 +03:00
|
|
|
u_int8_t satype;
|
|
|
|
|
|
|
|
/* map proto to satype */
|
|
|
|
if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_get: there was invalid proto in SAD.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2000-06-12 14:40:37 +04:00
|
|
|
n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
|
|
|
|
mhp->msg->sadb_msg_pid);
|
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-22 20:55:04 +04:00
|
|
|
/* XXX make it sysctl-configurable? */
|
|
|
|
static void
|
|
|
|
key_getcomb_setlifetime(comb)
|
|
|
|
struct sadb_comb *comb;
|
|
|
|
{
|
|
|
|
|
|
|
|
comb->sadb_comb_soft_allocations = 1;
|
|
|
|
comb->sadb_comb_hard_allocations = 1;
|
|
|
|
comb->sadb_comb_soft_bytes = 0;
|
|
|
|
comb->sadb_comb_hard_bytes = 0;
|
|
|
|
comb->sadb_comb_hard_addtime = 86400; /* 1 day */
|
2002-08-20 10:20:26 +04:00
|
|
|
comb->sadb_comb_soft_addtime = comb->sadb_comb_hard_addtime * 80 / 100;
|
2003-07-22 15:12:15 +04:00
|
|
|
comb->sadb_comb_hard_usetime = 28800; /* 8 hours */
|
2002-08-20 10:20:26 +04:00
|
|
|
comb->sadb_comb_soft_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
|
2000-09-22 20:55:04 +04:00
|
|
|
}
|
|
|
|
|
2000-06-15 17:44:22 +04:00
|
|
|
#ifdef IPSEC_ESP
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
2000-06-12 14:40:37 +04:00
|
|
|
* XXX reorder combinations by preference
|
|
|
|
* XXX no idea if the user wants ESP authentication or not
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct mbuf *
|
|
|
|
key_getcomb_esp()
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct sadb_comb *comb;
|
2000-07-18 18:56:42 +04:00
|
|
|
const struct esp_algorithm *algo;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *result = NULL, *m, *n;
|
|
|
|
int encmin;
|
|
|
|
int i, off, o;
|
|
|
|
int totlen;
|
|
|
|
const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
|
|
|
|
|
|
|
|
m = NULL;
|
2000-07-18 18:56:42 +04:00
|
|
|
for (i = 1; i <= SADB_EALG_MAX; i++) {
|
|
|
|
algo = esp_algorithm_lookup(i);
|
|
|
|
if (!algo)
|
|
|
|
continue;
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
if (algo->keymax < ipsec_esp_keymin)
|
|
|
|
continue;
|
|
|
|
if (algo->keymin < ipsec_esp_keymin)
|
|
|
|
encmin = ipsec_esp_keymin;
|
|
|
|
else
|
|
|
|
encmin = algo->keymin;
|
|
|
|
|
|
|
|
if (ipsec_esp_auth)
|
|
|
|
m = key_getcomb_ah();
|
|
|
|
else {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (l > MLEN)
|
|
|
|
panic("assumption failed in key_getcomb_esp");
|
|
|
|
#endif
|
|
|
|
MGET(m, M_DONTWAIT, MT_DATA);
|
|
|
|
if (m) {
|
|
|
|
M_ALIGN(m, l);
|
|
|
|
m->m_len = l;
|
|
|
|
m->m_next = NULL;
|
|
|
|
bzero(mtod(m, caddr_t), m->m_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
totlen = 0;
|
|
|
|
for (n = m; n; n = n->m_next)
|
|
|
|
totlen += n->m_len;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (totlen % l)
|
|
|
|
panic("assumption failed in key_getcomb_esp");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (off = 0; off < totlen; off += l) {
|
|
|
|
n = m_pulldown(m, off, l, &o);
|
|
|
|
if (!n) {
|
|
|
|
/* m is already freed */
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
|
2000-09-22 20:55:04 +04:00
|
|
|
bzero(comb, sizeof(*comb));
|
|
|
|
key_getcomb_setlifetime(comb);
|
2000-06-12 14:40:37 +04:00
|
|
|
comb->sadb_comb_encrypt = i;
|
|
|
|
comb->sadb_comb_encrypt_minbits = encmin;
|
|
|
|
comb->sadb_comb_encrypt_maxbits = algo->keymax;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
result = m;
|
|
|
|
else
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-15 17:44:22 +04:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX reorder combinations by preference
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_getcomb_ah()
|
|
|
|
{
|
|
|
|
struct sadb_comb *comb;
|
2000-07-18 18:56:42 +04:00
|
|
|
const struct ah_algorithm *algo;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
int min;
|
|
|
|
int i;
|
|
|
|
const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
|
|
|
|
|
|
|
|
m = NULL;
|
2000-07-18 18:56:42 +04:00
|
|
|
for (i = 1; i <= SADB_AALG_MAX; i++) {
|
2000-06-12 14:40:37 +04:00
|
|
|
#if 1
|
|
|
|
/* we prefer HMAC algorithms, not old algorithms */
|
|
|
|
if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
|
|
|
|
continue;
|
|
|
|
#endif
|
2000-07-18 18:56:42 +04:00
|
|
|
algo = ah_algorithm_lookup(i);
|
|
|
|
if (!algo)
|
|
|
|
continue;
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
if (algo->keymax < ipsec_ah_keymin)
|
|
|
|
continue;
|
|
|
|
if (algo->keymin < ipsec_ah_keymin)
|
|
|
|
min = ipsec_ah_keymin;
|
|
|
|
else
|
|
|
|
min = algo->keymin;
|
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (l > MLEN)
|
|
|
|
panic("assumption failed in key_getcomb_ah");
|
|
|
|
#endif
|
|
|
|
MGET(m, M_DONTWAIT, MT_DATA);
|
|
|
|
if (m) {
|
|
|
|
M_ALIGN(m, l);
|
|
|
|
m->m_len = l;
|
|
|
|
m->m_next = NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
M_PREPEND(m, l, M_DONTWAIT);
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
comb = mtod(m, struct sadb_comb *);
|
|
|
|
bzero(comb, sizeof(*comb));
|
2000-09-22 20:55:04 +04:00
|
|
|
key_getcomb_setlifetime(comb);
|
2000-06-12 14:40:37 +04:00
|
|
|
comb->sadb_comb_auth = i;
|
|
|
|
comb->sadb_comb_auth_minbits = min;
|
|
|
|
comb->sadb_comb_auth_maxbits = algo->keymax;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2000-09-20 04:08:42 +04:00
|
|
|
/*
|
2000-10-02 07:55:41 +04:00
|
|
|
* not really an official behavior. discussed in pf_key@inner.net in Sep2000.
|
2000-09-26 12:40:23 +04:00
|
|
|
* XXX reorder combinations by preference
|
2000-09-20 04:08:42 +04:00
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_getcomb_ipcomp()
|
|
|
|
{
|
2000-09-26 12:40:23 +04:00
|
|
|
struct sadb_comb *comb;
|
|
|
|
const struct ipcomp_algorithm *algo;
|
|
|
|
struct mbuf *m;
|
|
|
|
int i;
|
|
|
|
const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
|
2000-09-20 04:08:42 +04:00
|
|
|
|
2000-09-26 12:40:23 +04:00
|
|
|
m = NULL;
|
|
|
|
for (i = 1; i <= SADB_X_CALG_MAX; i++) {
|
|
|
|
algo = ipcomp_algorithm_lookup(i);
|
|
|
|
if (!algo)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (l > MLEN)
|
2002-01-31 09:35:25 +03:00
|
|
|
panic("assumption failed in key_getcomb_ipcomp");
|
2000-09-26 12:40:23 +04:00
|
|
|
#endif
|
|
|
|
MGET(m, M_DONTWAIT, MT_DATA);
|
|
|
|
if (m) {
|
|
|
|
M_ALIGN(m, l);
|
|
|
|
m->m_len = l;
|
|
|
|
m->m_next = NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
M_PREPEND(m, l, M_DONTWAIT);
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
comb = mtod(m, struct sadb_comb *);
|
|
|
|
bzero(comb, sizeof(*comb));
|
|
|
|
key_getcomb_setlifetime(comb);
|
|
|
|
comb->sadb_comb_encrypt = i;
|
|
|
|
/* what should we set into sadb_comb_*_{min,max}bits? */
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
2000-09-20 04:08:42 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/*
|
|
|
|
* XXX no way to pass mode (transport/tunnel) to userland
|
|
|
|
* XXX replay checking?
|
|
|
|
* XXX sysctl interface to ipsec_{ah,esp}_keymin
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_getprop(saidx)
|
|
|
|
const struct secasindex *saidx;
|
|
|
|
{
|
|
|
|
struct sadb_prop *prop;
|
|
|
|
struct mbuf *m, *n;
|
|
|
|
const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
|
|
|
|
int totlen;
|
|
|
|
|
|
|
|
switch (saidx->proto) {
|
2000-06-15 17:44:22 +04:00
|
|
|
#ifdef IPSEC_ESP
|
2000-06-12 14:40:37 +04:00
|
|
|
case IPPROTO_ESP:
|
|
|
|
m = key_getcomb_esp();
|
|
|
|
break;
|
2000-06-15 17:44:22 +04:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
case IPPROTO_AH:
|
|
|
|
m = key_getcomb_ah();
|
|
|
|
break;
|
2000-09-20 04:08:42 +04:00
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
m = key_getcomb_ipcomp();
|
|
|
|
break;
|
2000-06-12 14:40:37 +04:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
M_PREPEND(m, l, M_DONTWAIT);
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
totlen = 0;
|
|
|
|
for (n = m; n; n = n->m_next)
|
|
|
|
totlen += n->m_len;
|
|
|
|
|
|
|
|
prop = mtod(m, struct sadb_prop *);
|
|
|
|
bzero(prop, sizeof(*prop));
|
|
|
|
prop->sadb_prop_len = PFKEY_UNIT64(totlen);
|
|
|
|
prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
|
|
|
|
prop->sadb_prop_replay = 32; /* XXX */
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
|
|
|
|
* send
|
2000-10-02 07:55:41 +04:00
|
|
|
* <base, SA, address(SD), (address(P)), x_policy,
|
2000-06-12 14:40:37 +04:00
|
|
|
* (identity(SD),) (sensitivity,) proposal>
|
|
|
|
* to KMD, and expect to receive
|
2002-01-31 09:35:25 +03:00
|
|
|
* <base> with SADB_ACQUIRE if error occured,
|
2000-06-12 14:40:37 +04:00
|
|
|
* or
|
|
|
|
* <base, src address, dst address, (SPI range)> with SADB_GETSPI
|
|
|
|
* from KMD by PF_KEY.
|
|
|
|
*
|
2000-10-02 07:55:41 +04:00
|
|
|
* XXX x_policy is outside of RFC2367 (KAME extension).
|
2000-09-20 04:08:42 +04:00
|
|
|
* XXX sensitivity is not supported.
|
2000-10-02 07:55:41 +04:00
|
|
|
* XXX for ipcomp, RFC2367 does not define how to fill in proposal.
|
|
|
|
* see comment for key_getcomb_ipcomp().
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
|
|
|
* OUT:
|
|
|
|
* 0 : succeed
|
|
|
|
* others: error number
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_acquire(saidx, sp)
|
|
|
|
struct secasindex *saidx;
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
|
|
|
struct mbuf *result = NULL, *m;
|
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
|
|
|
struct secacq *newacq;
|
|
|
|
#endif
|
|
|
|
u_int8_t satype;
|
|
|
|
int error = -1;
|
|
|
|
u_int32_t seq;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2001-01-10 21:52:51 +03:00
|
|
|
if (saidx == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_acquire: NULL pointer is passed.");
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((satype = key_proto2satype(saidx->proto)) == 0)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_acquire: invalid proto is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* We never do anything about acquirng SA. There is anather
|
|
|
|
* solution that kernel blocks to send SADB_ACQUIRE message until
|
|
|
|
* getting something message from IKEd. In later case, to be
|
|
|
|
* managed with ACQUIRING list.
|
|
|
|
*/
|
2003-07-22 15:12:15 +04:00
|
|
|
/* get an entry to check whether sending message or not. */
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((newacq = key_getacq(saidx)) != NULL) {
|
1999-06-28 10:36:47 +04:00
|
|
|
if (key_blockacq_count < newacq->count) {
|
|
|
|
/* reset counter and do send message. */
|
|
|
|
newacq->count = 0;
|
|
|
|
} else {
|
|
|
|
/* increment counter and do nothing. */
|
|
|
|
newacq->count++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* make new entry for blocking to send SADB_ACQUIRE. */
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((newacq = key_newacq(saidx)) == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return ENOBUFS;
|
|
|
|
|
|
|
|
/* add to acqtree */
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_INSERT_HEAD(&acqtree, newacq, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
|
|
|
seq = newacq->seq;
|
|
|
|
#else
|
|
|
|
seq = (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
|
2000-01-31 17:18:52 +03:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
result = m;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* set sadb_address for saidx's. */
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
2001-10-19 05:57:20 +04:00
|
|
|
(struct sockaddr *)&saidx->src, FULLMASK, IPSEC_ULPROTO_ANY);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
2001-10-19 05:57:20 +04:00
|
|
|
(struct sockaddr *)&saidx->dst, FULLMASK, IPSEC_ULPROTO_ANY);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX proxy address (optional) */
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* set sadb_x_policy */
|
2001-01-10 21:52:51 +03:00
|
|
|
if (sp) {
|
2002-06-12 05:47:34 +04:00
|
|
|
m = key_setsadbxpolicy(sp->policy, sp->dir, sp->id);
|
2001-01-10 21:52:51 +03:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
/* set sadb_x_tag */
|
|
|
|
if (sp && sp->tag) {
|
|
|
|
m = key_setsadbxtag(sp->tag);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-07-01 05:01:34 +04:00
|
|
|
/* XXX identity (optional) */
|
2000-06-12 14:40:37 +04:00
|
|
|
#if 0
|
1999-06-28 10:36:47 +04:00
|
|
|
if (idexttype && fqdn) {
|
|
|
|
/* create identity extension (FQDN) */
|
|
|
|
struct sadb_ident *id;
|
|
|
|
int fqdnlen;
|
|
|
|
|
|
|
|
fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
|
|
|
|
id = (struct sadb_ident *)p;
|
|
|
|
bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
|
|
|
|
id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
|
|
|
|
id->sadb_ident_exttype = idexttype;
|
|
|
|
id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
|
|
|
|
bcopy(fqdn, id + 1, fqdnlen);
|
|
|
|
p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idexttype) {
|
|
|
|
/* create identity extension (USERFQDN) */
|
|
|
|
struct sadb_ident *id;
|
|
|
|
int userfqdnlen;
|
|
|
|
|
|
|
|
if (userfqdn) {
|
|
|
|
/* +1 for terminating-NUL */
|
|
|
|
userfqdnlen = strlen(userfqdn) + 1;
|
|
|
|
} else
|
|
|
|
userfqdnlen = 0;
|
|
|
|
id = (struct sadb_ident *)p;
|
|
|
|
bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
|
|
|
|
id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
|
|
|
|
id->sadb_ident_exttype = idexttype;
|
|
|
|
id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
|
|
|
|
/* XXX is it correct? */
|
|
|
|
if (curproc && curproc->p_cred)
|
|
|
|
id->sadb_ident_id = curproc->p_cred->p_ruid;
|
|
|
|
if (userfqdn && userfqdnlen)
|
|
|
|
bcopy(userfqdn, id + 1, userfqdnlen);
|
|
|
|
p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-07-01 05:01:34 +04:00
|
|
|
/* XXX sensitivity (optional) */
|
|
|
|
|
|
|
|
/* create proposal/combination extension */
|
|
|
|
m = key_getprop(saidx);
|
2000-09-20 04:08:42 +04:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* spec conformant: always attach proposal/combination extension,
|
|
|
|
* the problem is that we have no way to attach it for ipcomp,
|
|
|
|
* due to the way sadb_comb is declared in RFC2367.
|
|
|
|
*/
|
2000-07-01 05:01:34 +04:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
2000-09-20 04:08:42 +04:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* outside of spec; make proposal/combination extension optional.
|
|
|
|
*/
|
|
|
|
if (m)
|
|
|
|
m_cat(result, m);
|
|
|
|
#endif
|
2000-07-01 05:01:34 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((result->m_flags & M_PKTHDR) == 0) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (result->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
result = m_pullup(result, sizeof(struct sadb_msg));
|
|
|
|
if (result == NULL) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result->m_pkthdr.len = 0;
|
|
|
|
for (m = result; m; m = m->m_next)
|
|
|
|
result->m_pkthdr.len += m->m_len;
|
|
|
|
|
|
|
|
mtod(result, struct sadb_msg *)->sadb_msg_len =
|
|
|
|
PFKEY_UNIT64(result->m_pkthdr.len);
|
|
|
|
|
|
|
|
return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
|
|
|
return error;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
1999-06-28 10:36:47 +04:00
|
|
|
static struct secacq *
|
2000-01-31 17:18:52 +03:00
|
|
|
key_newacq(saidx)
|
|
|
|
struct secasindex *saidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct secacq *newacq;
|
|
|
|
|
|
|
|
/* get new entry */
|
|
|
|
KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
|
|
|
|
if (newacq == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bzero(newacq, sizeof(*newacq));
|
|
|
|
|
|
|
|
/* copy secindex */
|
2000-01-31 17:18:52 +03:00
|
|
|
bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
|
1999-06-28 10:36:47 +04:00
|
|
|
newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
|
2002-01-31 09:35:25 +03:00
|
|
|
newacq->created = time.tv_sec;
|
2003-08-22 10:22:21 +04:00
|
|
|
newacq->count = 1;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
return newacq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct secacq *
|
2000-01-31 17:18:52 +03:00
|
|
|
key_getacq(saidx)
|
|
|
|
struct secasindex *saidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct secacq *acq;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_FOREACH(acq, &acqtree, chain) {
|
|
|
|
if (key_cmpsaidx_exactly(saidx, &acq->saidx))
|
1999-06-28 10:36:47 +04:00
|
|
|
return acq;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct secacq *
|
|
|
|
key_getacqbyseq(seq)
|
|
|
|
u_int32_t seq;
|
|
|
|
{
|
|
|
|
struct secacq *acq;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_FOREACH(acq, &acqtree, chain) {
|
1999-06-28 10:36:47 +04:00
|
|
|
if (acq->seq == seq)
|
|
|
|
return acq;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
static struct secspacq *
|
|
|
|
key_newspacq(spidx)
|
|
|
|
struct secpolicyindex *spidx;
|
|
|
|
{
|
|
|
|
struct secspacq *acq;
|
|
|
|
|
2002-06-12 05:47:34 +04:00
|
|
|
if (!spidx)
|
|
|
|
return NULL;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* get new entry */
|
|
|
|
KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
|
|
|
|
if (acq == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bzero(acq, sizeof(*acq));
|
|
|
|
|
|
|
|
/* copy secindex */
|
|
|
|
bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
|
2002-01-31 09:35:25 +03:00
|
|
|
acq->created = time.tv_sec;
|
2000-06-12 14:40:37 +04:00
|
|
|
acq->count = 0;
|
|
|
|
|
|
|
|
return acq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct secspacq *
|
|
|
|
key_getspacq(spidx)
|
|
|
|
struct secpolicyindex *spidx;
|
|
|
|
{
|
|
|
|
struct secspacq *acq;
|
|
|
|
|
2002-06-12 05:47:34 +04:00
|
|
|
if (!spidx)
|
|
|
|
return NULL;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
LIST_FOREACH(acq, &spacqtree, chain) {
|
|
|
|
if (key_cmpspidx_exactly(spidx, &acq->spidx))
|
|
|
|
return acq;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* SADB_ACQUIRE processing,
|
|
|
|
* in first situation, is receiving
|
|
|
|
* <base>
|
2000-01-31 17:18:52 +03:00
|
|
|
* from the ikmpd, and clear sequence of its secasvar entry.
|
1999-06-28 10:36:47 +04:00
|
|
|
*
|
|
|
|
* In second situation, is receiving
|
|
|
|
* <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
|
|
|
|
* from a user land process, and return
|
|
|
|
* <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
|
|
|
|
* to the socket.
|
|
|
|
*
|
2000-07-18 18:56:42 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_acquire2(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
const struct sadb_address *src0, *dst0;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *sah;
|
|
|
|
u_int16_t proto;
|
2000-06-12 14:40:37 +04:00
|
|
|
int error;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_acquire2: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* Error message from KMd.
|
2002-01-31 09:35:25 +03:00
|
|
|
* We assume that if error was occured in IKEd, the length of PFKEY
|
2000-01-31 17:18:52 +03:00
|
|
|
* message is equal to the size of sadb_msg structure.
|
2002-01-31 09:35:25 +03:00
|
|
|
* We do not raise error even if error occured in this function.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
|
2000-01-31 17:18:52 +03:00
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
|
|
|
struct secacq *acq;
|
|
|
|
|
|
|
|
/* check sequence number */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->msg->sadb_msg_seq == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_acquire2: must specify sequence number.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return 0;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
|
2000-09-22 00:35:09 +04:00
|
|
|
/*
|
|
|
|
* the specified larval SA is already gone, or we got
|
|
|
|
* a bogus sequence number. we can silently ignore it.
|
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return 0;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* reset acq counter in order to deletion by timehander. */
|
2002-01-31 09:35:25 +03:00
|
|
|
acq->created = time.tv_sec;
|
2000-01-31 17:18:52 +03:00
|
|
|
acq->count = 0;
|
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
|
|
|
return 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* This message is from user land.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* map satype to proto */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_acquire2: invalid satype is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
|
|
|
|
mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
|
1999-06-28 10:36:47 +04:00
|
|
|
/* error */
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
|
|
|
|
mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
|
|
|
|
mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
|
|
|
|
/* error */
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_acquire2: invalid message is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
|
|
|
|
dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
|
|
|
|
|
|
|
|
/* XXX boundary check against sa_len */
|
|
|
|
KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* get a SA index */
|
2000-06-12 14:40:37 +04:00
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
if (sah->state == SADB_SASTATE_DEAD)
|
|
|
|
continue;
|
|
|
|
if (key_cmpsaidx_withmode(&sah->saidx, &saidx))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sah != NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_acquire2: a SA exists already.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EEXIST);
|
1999-07-31 22:41:15 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
error = key_acquire(&saidx, NULL);
|
|
|
|
if (error != 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_acquire2: error %d returned "
|
|
|
|
"from key_acquire.\n", mhp->msg->sadb_msg_errno));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* SADB_REGISTER processing.
|
|
|
|
* If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
|
1999-06-28 10:36:47 +04:00
|
|
|
* receive
|
|
|
|
* <base>
|
|
|
|
* from the ikmpd, and register a socket to send PF_KEY messages,
|
|
|
|
* and send
|
|
|
|
* <base, supported>
|
|
|
|
* to KMD by PF_KEY.
|
|
|
|
* If socket is detached, must free from regnode.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
2000-08-29 13:08:42 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_register(so, m, mhp)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct socket *so;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct secreg *reg, *newreg = 0;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_register: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* check for invalid register message */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
|
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* When SATYPE_UNSPEC is specified, only return sabd_supported. */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
|
1999-06-28 10:36:47 +04:00
|
|
|
goto setmsg;
|
|
|
|
|
|
|
|
/* check whether existing or not */
|
2000-06-12 14:40:37 +04:00
|
|
|
LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
|
1999-06-28 10:36:47 +04:00
|
|
|
if (reg->so == so) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_register: socket exists already.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EEXIST);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create regnode */
|
2000-06-12 14:40:37 +04:00
|
|
|
KMALLOC(newreg, struct secreg *, sizeof(*newreg));
|
1999-06-28 10:36:47 +04:00
|
|
|
if (newreg == NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
bzero((caddr_t)newreg, sizeof(*newreg));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
newreg->so = so;
|
|
|
|
((struct keycb *)sotorawcb(so))->kp_registered++;
|
|
|
|
|
|
|
|
/* add regnode to regtree. */
|
2000-06-12 14:40:37 +04:00
|
|
|
LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
setmsg:
|
2000-06-12 14:40:37 +04:00
|
|
|
{
|
|
|
|
struct mbuf *n;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
struct sadb_supported *sup;
|
|
|
|
u_int len, alen, elen;
|
2000-06-12 14:40:37 +04:00
|
|
|
int off;
|
|
|
|
int i;
|
|
|
|
struct sadb_alg *alg;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2000-07-18 18:56:42 +04:00
|
|
|
alen = 0;
|
|
|
|
for (i = 1; i <= SADB_AALG_MAX; i++) {
|
|
|
|
if (ah_algorithm_lookup(i))
|
|
|
|
alen += sizeof(struct sadb_alg);
|
|
|
|
}
|
|
|
|
if (alen)
|
|
|
|
alen += sizeof(struct sadb_supported);
|
1999-06-28 10:36:47 +04:00
|
|
|
elen = 0;
|
2000-07-18 18:56:42 +04:00
|
|
|
#ifdef IPSEC_ESP
|
|
|
|
for (i = 1; i <= SADB_EALG_MAX; i++) {
|
|
|
|
if (esp_algorithm_lookup(i))
|
|
|
|
elen += sizeof(struct sadb_alg);
|
|
|
|
}
|
|
|
|
if (elen)
|
|
|
|
elen += sizeof(struct sadb_supported);
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
len = sizeof(struct sadb_msg) + alen + elen;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (len > MCLBYTES)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
|
|
|
|
MGETHDR(n, M_DONTWAIT, MT_DATA);
|
|
|
|
if (len > MHLEN) {
|
|
|
|
MCLGET(n, M_DONTWAIT);
|
|
|
|
if ((n->m_flags & M_EXT) == 0) {
|
|
|
|
m_freem(n);
|
|
|
|
n = NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
n->m_pkthdr.len = n->m_len = len;
|
|
|
|
n->m_next = NULL;
|
|
|
|
off = 0;
|
|
|
|
|
|
|
|
m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
|
|
|
|
newmsg = mtod(n, struct sadb_msg *);
|
1999-06-28 10:36:47 +04:00
|
|
|
newmsg->sadb_msg_errno = 0;
|
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(len);
|
2000-06-12 14:40:37 +04:00
|
|
|
off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* for authentication algorithm */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (alen) {
|
|
|
|
sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
|
|
|
|
sup->sadb_supported_len = PFKEY_UNIT64(alen);
|
|
|
|
sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
|
|
|
|
off += PFKEY_ALIGN8(sizeof(*sup));
|
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
for (i = 1; i <= SADB_AALG_MAX; i++) {
|
|
|
|
const struct ah_algorithm *aalgo;
|
2000-06-12 14:40:37 +04:00
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
aalgo = ah_algorithm_lookup(i);
|
|
|
|
if (!aalgo)
|
|
|
|
continue;
|
2000-06-12 14:40:37 +04:00
|
|
|
alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
|
|
|
|
alg->sadb_alg_id = i;
|
|
|
|
alg->sadb_alg_ivlen = 0;
|
|
|
|
alg->sadb_alg_minbits = aalgo->keymin;
|
|
|
|
alg->sadb_alg_maxbits = aalgo->keymax;
|
|
|
|
off += PFKEY_ALIGN8(sizeof(*alg));
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef IPSEC_ESP
|
|
|
|
/* for encryption algorithm */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (elen) {
|
|
|
|
sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
|
|
|
|
sup->sadb_supported_len = PFKEY_UNIT64(elen);
|
|
|
|
sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
|
|
|
|
off += PFKEY_ALIGN8(sizeof(*sup));
|
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
for (i = 1; i <= SADB_EALG_MAX; i++) {
|
|
|
|
const struct esp_algorithm *ealgo;
|
2000-06-12 14:40:37 +04:00
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
ealgo = esp_algorithm_lookup(i);
|
|
|
|
if (!ealgo)
|
|
|
|
continue;
|
2000-06-12 14:40:37 +04:00
|
|
|
alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
|
|
|
|
alg->sadb_alg_id = i;
|
|
|
|
if (ealgo && ealgo->ivlen) {
|
|
|
|
/*
|
|
|
|
* give NULL to get the value preferred by
|
|
|
|
* algorithm XXX SADB_X_EXT_DERIV ?
|
|
|
|
*/
|
2000-08-29 13:08:42 +04:00
|
|
|
alg->sadb_alg_ivlen =
|
|
|
|
(*ealgo->ivlen)(ealgo, NULL);
|
2000-06-12 14:40:37 +04:00
|
|
|
} else
|
|
|
|
alg->sadb_alg_ivlen = 0;
|
|
|
|
alg->sadb_alg_minbits = ealgo->keymin;
|
|
|
|
alg->sadb_alg_maxbits = ealgo->keymax;
|
|
|
|
off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
#ifdef DIGAGNOSTIC
|
|
|
|
if (off != len)
|
|
|
|
panic("length assumption failed in key_register");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* free secreg entry registered.
|
|
|
|
* XXX: I want to do free a socket marked done SADB_RESIGER to socket.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
key_freereg(so)
|
|
|
|
struct socket *so;
|
|
|
|
{
|
|
|
|
struct secreg *reg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (so == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_freereg: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* check whether existing or not.
|
|
|
|
* check all type of SA, because there is a potential that
|
|
|
|
* one socket is registered to multiple type of SA.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
for (i = 0; i <= SADB_SATYPE_MAX; i++) {
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_FOREACH(reg, ®tree[i], chain) {
|
2003-07-22 15:12:15 +04:00
|
|
|
if (reg->so == so && __LIST_CHAINED(reg)) {
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_REMOVE(reg, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
KFREE(reg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_EXPIRE processing
|
|
|
|
* send
|
2000-06-12 14:40:37 +04:00
|
|
|
* <base, SA, SA2, lifetime(C and one of HS), address(SD)>
|
1999-06-28 10:36:47 +04:00
|
|
|
* to KMD by PF_KEY.
|
|
|
|
* NOTE: We send only soft lifetime extension.
|
|
|
|
*
|
|
|
|
* OUT: 0 : succeed
|
|
|
|
* others : error number
|
|
|
|
*/
|
|
|
|
static int
|
2000-01-31 17:18:52 +03:00
|
|
|
key_expire(sav)
|
|
|
|
struct secasvar *sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
int s;
|
2000-01-31 17:18:52 +03:00
|
|
|
int satype;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *result = NULL, *m;
|
|
|
|
int len;
|
|
|
|
int error = -1;
|
|
|
|
struct sadb_lifetime *lt;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* XXX: Why do we lock ? */
|
1999-07-04 06:01:15 +04:00
|
|
|
s = splsoftnet(); /*called from softclock()*/
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_expire: NULL pointer is passed.");
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->sah == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_expire: Why was SA index in SA NULL.");
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_expire: invalid proto is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* set msg header */
|
|
|
|
m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
result = m;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* create SA extension */
|
|
|
|
m = key_setsadbsa(sav);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* create SA extension */
|
2001-08-02 16:10:14 +04:00
|
|
|
m = key_setsadbxsa2(sav->sah->saidx.mode,
|
2003-09-07 19:59:36 +04:00
|
|
|
sav->replay ? (sav->replay->count & 0xffffffff) : 0,
|
|
|
|
sav->sah->saidx.reqid);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* create lifetime extension (current and soft) */
|
|
|
|
len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
|
|
|
|
m = key_alloc_mbuf(len);
|
|
|
|
if (!m || m->m_next) { /*XXX*/
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
bzero(mtod(m, caddr_t), len);
|
|
|
|
lt = mtod(m, struct sadb_lifetime *);
|
|
|
|
lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
|
|
|
|
lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
|
|
|
|
lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
|
|
|
|
lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
|
|
|
|
lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
|
|
|
|
lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
|
|
|
|
lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
|
|
|
|
bcopy(sav->lft_s, lt, sizeof(*lt));
|
|
|
|
m_cat(result, m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* set sadb_address for source */
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
|
|
|
(struct sockaddr *)&sav->sah->saidx.src,
|
2001-10-19 05:57:20 +04:00
|
|
|
FULLMASK, IPSEC_ULPROTO_ANY);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* set sadb_address for destination */
|
2000-06-12 14:40:37 +04:00
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
|
|
|
(struct sockaddr *)&sav->sah->saidx.dst,
|
2001-10-19 05:57:20 +04:00
|
|
|
FULLMASK, IPSEC_ULPROTO_ANY);
|
2000-06-12 14:40:37 +04:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
if ((result->m_flags & M_PKTHDR) == 0) {
|
|
|
|
error = EINVAL;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
2000-07-18 18:56:42 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
if (result->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
result = m_pullup(result, sizeof(struct sadb_msg));
|
2000-07-18 18:56:42 +04:00
|
|
|
if (result == NULL) {
|
|
|
|
error = ENOBUFS;
|
2000-06-12 14:40:37 +04:00
|
|
|
goto fail;
|
2000-07-18 18:56:42 +04:00
|
|
|
}
|
2000-06-12 14:40:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
result->m_pkthdr.len = 0;
|
|
|
|
for (m = result; m; m = m->m_next)
|
|
|
|
result->m_pkthdr.len += m->m_len;
|
|
|
|
|
|
|
|
mtod(result, struct sadb_msg *)->sadb_msg_len =
|
|
|
|
PFKEY_UNIT64(result->m_pkthdr.len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2002-03-01 07:19:42 +03:00
|
|
|
splx(s);
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
1999-06-28 10:36:47 +04:00
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_FLUSH processing
|
|
|
|
* receive
|
|
|
|
* <base>
|
|
|
|
* from the ikmpd, and free all entries in secastree.
|
|
|
|
* and send,
|
|
|
|
* <base>
|
|
|
|
* to the ikmpd.
|
|
|
|
* NOTE: to do is only marking SADB_SASTATE_DEAD.
|
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_flush(so, m, mhp)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct sadb_msg *newmsg;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secashead *sah, *nextsah;
|
|
|
|
struct secasvar *sav, *nextsav;
|
|
|
|
u_int16_t proto;
|
|
|
|
u_int8_t state;
|
|
|
|
u_int stateidx;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_flush: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* map satype to proto */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_flush: invalid satype is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* no SATYPE specified, i.e. flushing all SA. */
|
2003-09-07 19:59:36 +04:00
|
|
|
for (sah = LIST_FIRST(&sahtree); sah != NULL; sah = nextsah) {
|
2000-01-31 17:18:52 +03:00
|
|
|
nextsah = LIST_NEXT(sah, chain);
|
|
|
|
|
2003-07-22 15:12:15 +04:00
|
|
|
if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
|
|
|
|
proto != sah->saidx.proto)
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_alive);
|
|
|
|
stateidx++) {
|
|
|
|
state = saorder_state_any[stateidx];
|
|
|
|
for (sav = LIST_FIRST(&sah->savtree[state]);
|
|
|
|
sav != NULL;
|
|
|
|
sav = nextsav) {
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
nextsav = LIST_NEXT(sav, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_chgstate(sav, SADB_SASTATE_DEAD);
|
|
|
|
key_freesav(sav);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
sah->state = SADB_SASTATE_DEAD;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m->m_len < sizeof(struct sadb_msg) ||
|
|
|
|
sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_flush: No more memory.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m->m_next)
|
|
|
|
m_freem(m->m_next);
|
|
|
|
m->m_next = NULL;
|
|
|
|
m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
|
|
|
|
newmsg = mtod(m, struct sadb_msg *);
|
1999-06-28 10:36:47 +04:00
|
|
|
newmsg->sadb_msg_errno = 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_DUMP processing
|
2000-01-31 17:18:52 +03:00
|
|
|
* dump all entries including status of DEAD in SAD.
|
1999-06-28 10:36:47 +04:00
|
|
|
* receive
|
|
|
|
* <base>
|
2000-01-31 17:18:52 +03:00
|
|
|
* from the ikmpd, and dump all secasvar leaves
|
1999-06-28 10:36:47 +04:00
|
|
|
* and send,
|
|
|
|
* <base> .....
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
static int
|
2000-06-12 14:40:37 +04:00
|
|
|
key_dump(so, m, mhp)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct socket *so;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
|
|
|
u_int16_t proto;
|
|
|
|
u_int stateidx;
|
|
|
|
u_int8_t satype;
|
|
|
|
u_int8_t state;
|
2000-06-12 14:40:37 +04:00
|
|
|
int cnt;
|
|
|
|
struct mbuf *n;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_dump: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* map satype to proto */
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_dump: invalid satype is passed.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* count sav entries to be sent to the userland. */
|
1999-06-28 10:36:47 +04:00
|
|
|
cnt = 0;
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
2003-07-22 15:12:15 +04:00
|
|
|
if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
|
|
|
|
proto != sah->saidx.proto)
|
2000-01-31 17:18:52 +03:00
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_any);
|
|
|
|
stateidx++) {
|
|
|
|
state = saorder_state_any[stateidx];
|
|
|
|
LIST_FOREACH(sav, &sah->savtree[state], chain) {
|
|
|
|
cnt++;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
if (cnt == 0)
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, ENOENT);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* send this to the userland, one at a time. */
|
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
2003-07-22 15:12:15 +04:00
|
|
|
if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
|
|
|
|
proto != sah->saidx.proto)
|
2000-01-31 17:18:52 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* map proto to satype */
|
|
|
|
if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_dump: there was invalid proto in SAD.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_any);
|
|
|
|
stateidx++) {
|
|
|
|
state = saorder_state_any[stateidx];
|
|
|
|
LIST_FOREACH(sav, &sah->savtree[state], chain) {
|
2000-06-12 14:40:37 +04:00
|
|
|
n = key_setdumpsa(sav, SADB_DUMP, satype,
|
|
|
|
--cnt, mhp->msg->sadb_msg_pid);
|
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
static struct mbuf *
|
|
|
|
key_setdump(req_satype, errorp)
|
|
|
|
u_int8_t req_satype;
|
|
|
|
int *errorp;
|
|
|
|
{
|
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
|
|
|
u_int16_t proto;
|
|
|
|
u_int stateidx;
|
|
|
|
u_int8_t satype;
|
|
|
|
u_int8_t state;
|
|
|
|
int cnt;
|
|
|
|
struct mbuf *m, *n;
|
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(req_satype)) == 0) {
|
|
|
|
*errorp = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count sav entries to be sent to the userland. */
|
|
|
|
cnt = 0;
|
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
if (req_satype != SADB_SATYPE_UNSPEC &&
|
|
|
|
proto != sah->saidx.proto)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_any);
|
|
|
|
stateidx++) {
|
|
|
|
state = saorder_state_any[stateidx];
|
|
|
|
LIST_FOREACH(sav, &sah->savtree[state], chain) {
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt == 0) {
|
|
|
|
*errorp = ENOENT;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send this to the userland, one at a time. */
|
|
|
|
m = NULL;
|
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
if (req_satype != SADB_SATYPE_UNSPEC &&
|
|
|
|
proto != sah->saidx.proto)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* map proto to satype */
|
|
|
|
if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
|
|
|
|
m_freem(m);
|
|
|
|
*errorp = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (stateidx = 0;
|
|
|
|
stateidx < _ARRAYLEN(saorder_state_any);
|
|
|
|
stateidx++) {
|
|
|
|
state = saorder_state_any[stateidx];
|
|
|
|
LIST_FOREACH(sav, &sah->savtree[state], chain) {
|
|
|
|
n = key_setdumpsa(sav, SADB_DUMP, satype,
|
|
|
|
--cnt, 0);
|
|
|
|
if (!n) {
|
|
|
|
m_freem(m);
|
|
|
|
*errorp = ENOBUFS;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m)
|
|
|
|
m = n;
|
|
|
|
else
|
|
|
|
m_cat(m, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
*errorp = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((m->m_flags & M_PKTHDR) != 0) {
|
|
|
|
m->m_pkthdr.len = 0;
|
|
|
|
for (n = m; n; n = n->m_next)
|
|
|
|
m->m_pkthdr.len += n->m_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
*errorp = 0;
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
2003-09-08 10:51:53 +04:00
|
|
|
struct mbuf *
|
|
|
|
key_setdumpsa_spi(spi)
|
|
|
|
u_int32_t spi;
|
|
|
|
{
|
|
|
|
struct mbuf *m, *n;
|
|
|
|
struct secasvar *sav;
|
|
|
|
u_int8_t satype;
|
|
|
|
int cnt;
|
|
|
|
|
|
|
|
cnt = 0;
|
2003-09-14 11:30:32 +04:00
|
|
|
LIST_FOREACH(sav, &spihash[SPIHASH(spi)], spihash) {
|
2003-09-08 10:51:53 +04:00
|
|
|
if (sav->spi != spi)
|
|
|
|
continue;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt == 0)
|
2003-09-12 11:38:10 +04:00
|
|
|
return (NULL);
|
2003-09-08 10:51:53 +04:00
|
|
|
|
|
|
|
m = NULL;
|
2003-09-14 11:30:32 +04:00
|
|
|
LIST_FOREACH(sav, &spihash[SPIHASH(spi)], spihash) {
|
2003-09-08 10:51:53 +04:00
|
|
|
if (sav->spi != spi)
|
|
|
|
continue;
|
|
|
|
satype = key_proto2satype(sav->sah->saidx.proto);
|
|
|
|
n = key_setdumpsa(sav, SADB_DUMP, satype, --cnt, 0);
|
|
|
|
if (!m)
|
|
|
|
m = n;
|
|
|
|
else if (n)
|
|
|
|
m_cat(m, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m)
|
2003-09-12 11:38:10 +04:00
|
|
|
return (NULL);
|
2003-09-08 10:51:53 +04:00
|
|
|
|
|
|
|
if ((m->m_flags & M_PKTHDR) != 0) {
|
|
|
|
m->m_pkthdr.len = 0;
|
|
|
|
for (n = m; n; n = n->m_next)
|
|
|
|
m->m_pkthdr.len += n->m_len;
|
|
|
|
}
|
|
|
|
|
2003-09-12 11:38:10 +04:00
|
|
|
return (m);
|
2003-09-08 10:51:53 +04:00
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* SADB_X_PROMISC processing
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
|
|
|
* m will always be freed.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_promisc(so, m, mhp)
|
1999-06-28 10:36:47 +04:00
|
|
|
struct socket *so;
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
const struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
int olen;
|
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (so == NULL || m == NULL || mhp == NULL || mhp->msg == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_promisc: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
if (olen < sizeof(struct sadb_msg)) {
|
2000-06-12 14:40:37 +04:00
|
|
|
#if 1
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
#else
|
|
|
|
m_freem(m);
|
|
|
|
return 0;
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
} else if (olen == sizeof(struct sadb_msg)) {
|
|
|
|
/* enable/disable promisc mode */
|
|
|
|
struct keycb *kp;
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
mhp->msg->sadb_msg_errno = 0;
|
|
|
|
switch (mhp->msg->sadb_msg_satype) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
kp->kp_promisc = mhp->msg->sadb_msg_satype;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return key_senderror(so, m, EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* send the original message back to everyone */
|
2000-06-12 14:40:37 +04:00
|
|
|
mhp->msg->sadb_msg_errno = 0;
|
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
|
1999-06-28 10:36:47 +04:00
|
|
|
} else {
|
|
|
|
/* send packet as is */
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* TODO: if sadb_msg_seq is specified, send to specific pid */
|
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *)) = {
|
|
|
|
NULL, /* SADB_RESERVED */
|
|
|
|
key_getspi, /* SADB_GETSPI */
|
|
|
|
key_update, /* SADB_UPDATE */
|
|
|
|
key_add, /* SADB_ADD */
|
|
|
|
key_delete, /* SADB_DELETE */
|
|
|
|
key_get, /* SADB_GET */
|
|
|
|
key_acquire2, /* SADB_ACQUIRE */
|
|
|
|
key_register, /* SADB_REGISTER */
|
|
|
|
NULL, /* SADB_EXPIRE */
|
|
|
|
key_flush, /* SADB_FLUSH */
|
|
|
|
key_dump, /* SADB_DUMP */
|
|
|
|
key_promisc, /* SADB_X_PROMISC */
|
|
|
|
NULL, /* SADB_X_PCHANGE */
|
|
|
|
key_spdadd, /* SADB_X_SPDUPDATE */
|
|
|
|
key_spdadd, /* SADB_X_SPDADD */
|
|
|
|
key_spddelete, /* SADB_X_SPDDELETE */
|
|
|
|
key_spdget, /* SADB_X_SPDGET */
|
|
|
|
NULL, /* SADB_X_SPDACQUIRE */
|
|
|
|
key_spddump, /* SADB_X_SPDDUMP */
|
|
|
|
key_spdflush, /* SADB_X_SPDFLUSH */
|
|
|
|
key_spdadd, /* SADB_X_SPDSETIDX */
|
|
|
|
NULL, /* SADB_X_SPDEXPIRE */
|
|
|
|
key_spddelete2, /* SADB_X_SPDDELETE2 */
|
|
|
|
};
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* parse sadb_msg buffer to process PFKEYv2,
|
|
|
|
* and create a data to response if needed.
|
|
|
|
* I think to be dealed with mbuf directly.
|
|
|
|
* IN:
|
|
|
|
* msgp : pointer to pointer to a received buffer pulluped.
|
|
|
|
* This is rewrited to response.
|
|
|
|
* so : pointer to socket.
|
|
|
|
* OUT:
|
|
|
|
* length for buffer to send to user process.
|
|
|
|
*/
|
|
|
|
int
|
2000-06-12 14:40:37 +04:00
|
|
|
key_parse(m, so)
|
|
|
|
struct mbuf *m;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct socket *so;
|
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct sadb_msg *msg;
|
|
|
|
struct sadb_msghdr mh;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int orglen;
|
|
|
|
int error;
|
2000-06-12 14:40:37 +04:00
|
|
|
int target;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m == NULL || so == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_parse: NULL pointer is passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
#if 0 /*kdebug_sadb assumes msg in linear buffer*/
|
1999-06-28 10:36:47 +04:00
|
|
|
KEYDEBUG(KEYDEBUG_KEY_DUMP,
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: passed sadb_msg\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
kdebug_sadb(msg));
|
2000-06-12 14:40:37 +04:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
m = m_pullup(m, sizeof(struct sadb_msg));
|
|
|
|
if (!m)
|
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
msg = mtod(m, struct sadb_msg *);
|
1999-06-28 10:36:47 +04:00
|
|
|
orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
|
2000-06-12 14:40:37 +04:00
|
|
|
target = KEY_SENDUP_ONE;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if ((m->m_flags & M_PKTHDR) == 0 ||
|
|
|
|
m->m_pkthdr.len != m->m_pkthdr.len) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: invalid message length.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
pfkeystat.out_invlen++;
|
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (msg->sadb_msg_version != PF_KEY_V2) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_parse: PF_KEY version %u is mismatched.\n",
|
|
|
|
msg->sadb_msg_version));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invver++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->sadb_msg_type > SADB_MAX) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
|
|
|
|
msg->sadb_msg_type));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invmsgtype++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for old-fashioned code - should be nuked */
|
|
|
|
if (m->m_pkthdr.len > MCLBYTES) {
|
|
|
|
m_freem(m);
|
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
if (m->m_next) {
|
|
|
|
struct mbuf *n;
|
|
|
|
|
|
|
|
MGETHDR(n, M_DONTWAIT, MT_DATA);
|
|
|
|
if (n && m->m_pkthdr.len > MHLEN) {
|
|
|
|
MCLGET(n, M_DONTWAIT);
|
|
|
|
if ((n->m_flags & M_EXT) == 0) {
|
|
|
|
m_free(n);
|
|
|
|
n = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!n) {
|
|
|
|
m_freem(m);
|
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
|
|
|
|
n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
|
|
|
|
n->m_next = NULL;
|
|
|
|
m_freem(m);
|
|
|
|
m = n;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* align the mbuf chain so that extensions are in contiguous region. */
|
|
|
|
error = key_align(m, &mh);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
if (m->m_next) { /*XXX*/
|
|
|
|
m_freem(m);
|
|
|
|
return ENOBUFS;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
msg = mh.msg;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* check SA type */
|
|
|
|
switch (msg->sadb_msg_satype) {
|
|
|
|
case SADB_SATYPE_UNSPEC:
|
|
|
|
switch (msg->sadb_msg_type) {
|
|
|
|
case SADB_GETSPI:
|
|
|
|
case SADB_UPDATE:
|
|
|
|
case SADB_ADD:
|
|
|
|
case SADB_DELETE:
|
|
|
|
case SADB_GET:
|
|
|
|
case SADB_ACQUIRE:
|
|
|
|
case SADB_EXPIRE:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: must specify satype "
|
|
|
|
"when msg type=%u.\n", msg->sadb_msg_type));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invsatype++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SADB_SATYPE_AH:
|
|
|
|
case SADB_SATYPE_ESP:
|
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
|
|
|
switch (msg->sadb_msg_type) {
|
|
|
|
case SADB_X_SPDADD:
|
|
|
|
case SADB_X_SPDDELETE:
|
|
|
|
case SADB_X_SPDGET:
|
|
|
|
case SADB_X_SPDDUMP:
|
|
|
|
case SADB_X_SPDFLUSH:
|
2000-06-12 14:40:37 +04:00
|
|
|
case SADB_X_SPDSETIDX:
|
|
|
|
case SADB_X_SPDUPDATE:
|
|
|
|
case SADB_X_SPDDELETE2:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: illegal satype=%u\n",
|
|
|
|
msg->sadb_msg_type));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invsatype++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SADB_SATYPE_RSVP:
|
|
|
|
case SADB_SATYPE_OSPFV2:
|
|
|
|
case SADB_SATYPE_RIPV2:
|
|
|
|
case SADB_SATYPE_MIP:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: type %u isn't supported.\n",
|
|
|
|
msg->sadb_msg_satype));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invsatype++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EOPNOTSUPP;
|
|
|
|
goto senderror;
|
|
|
|
case 1: /* XXX: What does it do? */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (msg->sadb_msg_type == SADB_X_PROMISC)
|
|
|
|
break;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: invalid type %u is passed.\n",
|
|
|
|
msg->sadb_msg_satype));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invsatype++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check field of upper layer protocol and address family */
|
2003-07-22 15:12:15 +04:00
|
|
|
if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL &&
|
|
|
|
mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
struct sadb_address *src0, *dst0;
|
2000-06-12 14:40:37 +04:00
|
|
|
u_int plen;
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
|
|
|
|
dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
/* check upper layer protocol */
|
|
|
|
if (src0->sadb_address_proto != dst0->sadb_address_proto) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: upper layer protocol mismatched.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invaddr++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check family */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (PFKEY_ADDR_SADDR(src0)->sa_family !=
|
|
|
|
PFKEY_ADDR_SADDR(dst0)->sa_family) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG, "key_parse: address family mismatched.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invaddr++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
if (PFKEY_ADDR_SADDR(src0)->sa_len !=
|
|
|
|
PFKEY_ADDR_SADDR(dst0)->sa_len) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_parse: address struct size mismatched.\n"));
|
2000-06-12 14:40:37 +04:00
|
|
|
pfkeystat.out_invaddr++;
|
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (PFKEY_ADDR_SADDR(src0)->sa_len !=
|
|
|
|
sizeof(struct sockaddr_in)) {
|
|
|
|
pfkeystat.out_invaddr++;
|
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (PFKEY_ADDR_SADDR(src0)->sa_len !=
|
|
|
|
sizeof(struct sockaddr_in6)) {
|
|
|
|
pfkeystat.out_invaddr++;
|
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_parse: unsupported address family.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invaddr++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EAFNOSUPPORT;
|
|
|
|
goto senderror;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
|
|
|
|
case AF_INET:
|
2000-06-12 14:40:37 +04:00
|
|
|
plen = sizeof(struct in_addr) << 3;
|
|
|
|
break;
|
2000-01-31 17:18:52 +03:00
|
|
|
case AF_INET6:
|
2000-06-12 14:40:37 +04:00
|
|
|
plen = sizeof(struct in6_addr) << 3;
|
2000-01-31 17:18:52 +03:00
|
|
|
break;
|
|
|
|
default:
|
2000-06-12 14:40:37 +04:00
|
|
|
plen = 0; /*fool gcc*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check max prefix length */
|
|
|
|
if (src0->sadb_address_prefixlen > plen ||
|
|
|
|
dst0->sadb_address_prefixlen > plen) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_parse: illegal prefixlen.\n"));
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invaddr++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prefixlen == 0 is valid because there can be a case when
|
|
|
|
* all addresses are matched.
|
|
|
|
*/
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
|
|
|
|
key_typesw[msg->sadb_msg_type] == NULL) {
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invmsgtype++;
|
2000-06-12 14:40:37 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
senderror:
|
|
|
|
msg->sadb_msg_errno = error;
|
|
|
|
return key_sendup_mbuf(so, m, target);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
static int
|
|
|
|
key_senderror(so, m, code)
|
|
|
|
struct socket *so;
|
|
|
|
struct mbuf *m;
|
|
|
|
int code;
|
|
|
|
{
|
|
|
|
struct sadb_msg *msg;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m->m_len < sizeof(struct sadb_msg))
|
|
|
|
panic("invalid mbuf passed to key_senderror");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
msg = mtod(m, struct sadb_msg *);
|
|
|
|
msg->sadb_msg_errno = code;
|
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* set the pointer to each header into message buffer.
|
2000-06-12 14:40:37 +04:00
|
|
|
* m will be freed on error.
|
|
|
|
* XXX larger-than-MCLBYTES extension?
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
static int
|
2000-06-12 14:40:37 +04:00
|
|
|
key_align(m, mhp)
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_msghdr *mhp;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *n;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct sadb_ext *ext;
|
2000-06-12 14:40:37 +04:00
|
|
|
size_t off, end;
|
|
|
|
int extlen;
|
|
|
|
int toff;
|
2000-01-31 17:18:52 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* sanity check */
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m == NULL || mhp == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("key_align: NULL pointer is passed.");
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m->m_len < sizeof(struct sadb_msg))
|
|
|
|
panic("invalid mbuf passed to key_align");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* initialize */
|
2000-06-12 14:40:37 +04:00
|
|
|
bzero(mhp, sizeof(*mhp));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
mhp->msg = mtod(m, struct sadb_msg *);
|
|
|
|
mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
|
|
|
|
extlen = end; /*just in case extlen is not updated*/
|
|
|
|
for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
|
|
|
|
n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
|
|
|
|
if (!n) {
|
|
|
|
/* m is already freed */
|
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* set pointer */
|
|
|
|
switch (ext->sadb_ext_type) {
|
|
|
|
case SADB_EXT_SA:
|
|
|
|
case SADB_EXT_ADDRESS_SRC:
|
|
|
|
case SADB_EXT_ADDRESS_DST:
|
|
|
|
case SADB_EXT_ADDRESS_PROXY:
|
2000-06-12 14:40:37 +04:00
|
|
|
case SADB_EXT_LIFETIME_CURRENT:
|
|
|
|
case SADB_EXT_LIFETIME_HARD:
|
|
|
|
case SADB_EXT_LIFETIME_SOFT:
|
1999-06-28 10:36:47 +04:00
|
|
|
case SADB_EXT_KEY_AUTH:
|
|
|
|
case SADB_EXT_KEY_ENCRYPT:
|
|
|
|
case SADB_EXT_IDENTITY_SRC:
|
|
|
|
case SADB_EXT_IDENTITY_DST:
|
|
|
|
case SADB_EXT_SENSITIVITY:
|
|
|
|
case SADB_EXT_PROPOSAL:
|
|
|
|
case SADB_EXT_SUPPORTED_AUTH:
|
|
|
|
case SADB_EXT_SUPPORTED_ENCRYPT:
|
|
|
|
case SADB_EXT_SPIRANGE:
|
|
|
|
case SADB_X_EXT_POLICY:
|
2000-06-12 14:40:37 +04:00
|
|
|
case SADB_X_EXT_SA2:
|
2003-09-12 11:38:10 +04:00
|
|
|
#ifdef SADB_X_EXT_TAG
|
|
|
|
case SADB_X_EXT_TAG:
|
|
|
|
#endif
|
2000-01-31 17:18:52 +03:00
|
|
|
/* duplicate check */
|
|
|
|
/*
|
|
|
|
* XXX Are there duplication payloads of either
|
|
|
|
* KEY_AUTH or KEY_ENCRYPT ?
|
|
|
|
*/
|
2000-06-12 14:40:37 +04:00
|
|
|
if (mhp->ext[ext->sadb_ext_type] != NULL) {
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_align: duplicate ext_type %u "
|
|
|
|
"is passed.\n", ext->sadb_ext_type));
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_dupext++;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
default:
|
2002-01-31 09:35:25 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"key_align: invalid ext_type %u is passed.\n",
|
|
|
|
ext->sadb_ext_type));
|
2000-06-12 14:40:37 +04:00
|
|
|
m_freem(m);
|
2000-01-31 17:18:52 +03:00
|
|
|
pfkeystat.out_invexttype++;
|
1999-06-28 10:36:47 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
|
2000-06-12 14:40:37 +04:00
|
|
|
|
|
|
|
if (key_validate_ext(ext, extlen)) {
|
|
|
|
m_freem(m);
|
|
|
|
pfkeystat.out_invlen++;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = m_pulldown(m, off, extlen, &toff);
|
|
|
|
if (!n) {
|
|
|
|
/* m is already freed */
|
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
|
|
|
|
|
|
|
|
mhp->ext[ext->sadb_ext_type] = ext;
|
|
|
|
mhp->extoff[ext->sadb_ext_type] = off;
|
|
|
|
mhp->extlen[ext->sadb_ext_type] = extlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (off != end) {
|
|
|
|
m_freem(m);
|
|
|
|
pfkeystat.out_invlen++;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
key_validate_ext(ext, len)
|
|
|
|
const struct sadb_ext *ext;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
struct sockaddr *sa;
|
|
|
|
enum { NONE, ADDR } checktype = NONE;
|
2003-07-22 15:12:15 +04:00
|
|
|
int baselen = 0;
|
2000-06-12 14:40:37 +04:00
|
|
|
const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
|
|
|
|
|
|
|
|
if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* if it does not match minimum/maximum length, bail */
|
|
|
|
if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
|
|
|
|
ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
|
|
|
|
return EINVAL;
|
|
|
|
if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
|
|
|
|
return EINVAL;
|
|
|
|
if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* more checks based on sadb_ext_type XXX need more */
|
|
|
|
switch (ext->sadb_ext_type) {
|
|
|
|
case SADB_EXT_ADDRESS_SRC:
|
|
|
|
case SADB_EXT_ADDRESS_DST:
|
|
|
|
case SADB_EXT_ADDRESS_PROXY:
|
|
|
|
baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
|
|
|
|
checktype = ADDR;
|
|
|
|
break;
|
|
|
|
case SADB_EXT_IDENTITY_SRC:
|
|
|
|
case SADB_EXT_IDENTITY_DST:
|
|
|
|
if (((struct sadb_ident *)ext)->sadb_ident_type ==
|
|
|
|
SADB_X_IDENTTYPE_ADDR) {
|
|
|
|
baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
|
|
|
|
checktype = ADDR;
|
|
|
|
} else
|
|
|
|
checktype = NONE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
checktype = NONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (checktype) {
|
|
|
|
case NONE:
|
|
|
|
break;
|
|
|
|
case ADDR:
|
|
|
|
sa = (struct sockaddr *)((caddr_t)ext + baselen);
|
|
|
|
if (len < baselen + sal)
|
|
|
|
return EINVAL;
|
|
|
|
if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
|
|
|
|
return EINVAL;
|
|
|
|
break;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_init()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bzero((caddr_t)&key_cb, sizeof(key_cb));
|
|
|
|
|
2000-03-23 10:01:25 +03:00
|
|
|
callout_init(&key_timehandler_ch);
|
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
for (i = 0; i < IPSEC_DIR_MAX; i++)
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_INIT(&sptree[i]);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_INIT(&sahtree);
|
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
for (i = 0; i <= SADB_SATYPE_MAX; i++)
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_INIT(®tree[i]);
|
2003-09-07 19:59:36 +04:00
|
|
|
|
|
|
|
for (i = 0; i < SPIHASHSIZE; i++)
|
|
|
|
LIST_INIT(&spihash[i]);
|
2000-01-31 17:18:52 +03:00
|
|
|
|
|
|
|
#ifndef IPSEC_NONBLOCK_ACQUIRE
|
|
|
|
LIST_INIT(&acqtree);
|
1999-07-31 22:41:15 +04:00
|
|
|
#endif
|
2000-06-12 14:40:37 +04:00
|
|
|
LIST_INIT(&spacqtree);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-07 19:59:36 +04:00
|
|
|
TAILQ_INIT(&satailq);
|
2002-06-12 21:56:45 +04:00
|
|
|
TAILQ_INIT(&sptailq);
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* system default */
|
2000-10-02 07:55:41 +04:00
|
|
|
#ifdef INET
|
2003-08-22 10:22:21 +04:00
|
|
|
ip4_def_policy = key_newsp(0);
|
2002-06-12 21:56:45 +04:00
|
|
|
if (!ip4_def_policy)
|
|
|
|
panic("could not initialize IPv4 default security policy");
|
2002-06-14 18:47:24 +04:00
|
|
|
ip4_def_policy->state = IPSEC_SPSTATE_ALIVE;
|
2002-06-12 21:56:45 +04:00
|
|
|
ip4_def_policy->policy = IPSEC_POLICY_NONE;
|
2002-06-14 18:47:24 +04:00
|
|
|
ip4_def_policy->dir = IPSEC_DIR_ANY;
|
2002-06-12 21:56:45 +04:00
|
|
|
ip4_def_policy->readonly = 1;
|
2003-09-22 08:47:43 +04:00
|
|
|
ip4_def_policy->persist = 1;
|
2000-10-02 07:55:41 +04:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
#ifdef INET6
|
2003-08-22 10:22:21 +04:00
|
|
|
ip6_def_policy = key_newsp(0);
|
2002-06-12 21:56:45 +04:00
|
|
|
if (!ip6_def_policy)
|
|
|
|
panic("could not initialize IPv6 default security policy");
|
2002-06-14 18:47:24 +04:00
|
|
|
ip6_def_policy->state = IPSEC_SPSTATE_ALIVE;
|
2002-06-12 21:56:45 +04:00
|
|
|
ip6_def_policy->policy = IPSEC_POLICY_NONE;
|
2002-06-14 18:47:24 +04:00
|
|
|
ip6_def_policy->dir = IPSEC_DIR_ANY;
|
2002-06-12 21:56:45 +04:00
|
|
|
ip6_def_policy->readonly = 1;
|
2003-09-22 08:47:43 +04:00
|
|
|
ip6_def_policy->persist = 1;
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
|
|
|
|
2002-01-31 10:05:43 +03:00
|
|
|
callout_reset(&key_timehandler_ch, hz, key_timehandler, (void *)0);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* initialize key statistics */
|
|
|
|
keystat.getspi_count = 1;
|
|
|
|
|
1999-07-03 08:45:53 +04:00
|
|
|
printf("IPsec: Initialized Security Association Processing.\n");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-01-31 17:18:52 +03:00
|
|
|
* XXX: maybe This function is called after INBOUND IPsec processing.
|
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* Special check for tunnel-mode packets.
|
|
|
|
* We must make some checks for consistency between inner and outer IP header.
|
|
|
|
*
|
|
|
|
* xxx more checks to be provided
|
|
|
|
*/
|
|
|
|
int
|
2000-01-31 17:18:52 +03:00
|
|
|
key_checktunnelsanity(sav, family, src, dst)
|
|
|
|
struct secasvar *sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int family;
|
|
|
|
caddr_t src;
|
|
|
|
caddr_t dst;
|
|
|
|
{
|
|
|
|
/* sanity check */
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->sah == NULL)
|
|
|
|
panic("sav->sah == NULL at key_checktunnelsanity");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/* XXX: check inner IP header */
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
return 1;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* Get FQDN for the host.
|
|
|
|
* If the administrator configured hostname (by hostname(1)) without
|
|
|
|
* domain name, returns nothing.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
key_getfqdn()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int hasdot;
|
|
|
|
static char fqdn[MAXHOSTNAMELEN + 1];
|
|
|
|
|
|
|
|
if (!hostnamelen)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* check if it comes with domain name. */
|
|
|
|
hasdot = 0;
|
|
|
|
for (i = 0; i < hostnamelen; i++) {
|
|
|
|
if (hostname[i] == '.')
|
|
|
|
hasdot++;
|
|
|
|
}
|
|
|
|
if (!hasdot)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* NOTE: hostname may not be NUL-terminated. */
|
|
|
|
bzero(fqdn, sizeof(fqdn));
|
|
|
|
bcopy(hostname, fqdn, hostnamelen);
|
|
|
|
fqdn[hostnamelen] = '\0';
|
|
|
|
return fqdn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get username@FQDN for the host/user.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
key_getuserfqdn()
|
|
|
|
{
|
|
|
|
const char *host;
|
|
|
|
static char userfqdn[MAXHOSTNAMELEN + MAXLOGNAME + 2];
|
|
|
|
struct proc *p = curproc;
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
if (!p || !p->p_pgrp || !p->p_pgrp->pg_session)
|
|
|
|
return NULL;
|
|
|
|
if (!(host = key_getfqdn()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* NOTE: s_login may not be-NUL terminated. */
|
|
|
|
bzero(userfqdn, sizeof(userfqdn));
|
|
|
|
bcopy(p->p_pgrp->pg_session->s_login, userfqdn, MAXLOGNAME);
|
|
|
|
userfqdn[MAXLOGNAME] = '\0'; /* safeguard */
|
|
|
|
q = userfqdn + strlen(userfqdn);
|
|
|
|
*q++ = '@';
|
|
|
|
bcopy(host, q, strlen(host));
|
|
|
|
q += strlen(host);
|
|
|
|
*q++ = '\0';
|
|
|
|
|
|
|
|
return userfqdn;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* record data transfer on SA, and update timestamps */
|
|
|
|
void
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_recordxfer(sav, m)
|
|
|
|
struct secasvar *sav;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct mbuf *m;
|
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
if (!sav)
|
|
|
|
panic("key_sa_recordxfer called with sav == NULL");
|
1999-06-28 10:36:47 +04:00
|
|
|
if (!m)
|
|
|
|
panic("key_sa_recordxfer called with m == NULL");
|
2000-01-31 17:18:52 +03:00
|
|
|
if (!sav->lft_c)
|
1999-06-28 10:36:47 +04:00
|
|
|
return;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
/*
|
|
|
|
* XXX Currently, there is a difference of bytes size
|
|
|
|
* between inbound and outbound processing.
|
|
|
|
*/
|
|
|
|
sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
|
1999-06-28 10:36:47 +04:00
|
|
|
/* to check bytes lifetime is done in key_timehandler(). */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use the number of packets as the unit of
|
|
|
|
* sadb_lifetime_allocations. We increment the variable
|
|
|
|
* whenever {esp,ah}_{in,out}put is called.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->lft_c->sadb_lifetime_allocations++;
|
1999-06-28 10:36:47 +04:00
|
|
|
/* XXX check for expires? */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
|
|
|
|
* in seconds. HARD and SOFT lifetime are measured by the time
|
|
|
|
* difference (again in seconds) from sadb_lifetime_usetime.
|
|
|
|
*
|
|
|
|
* usetime
|
|
|
|
* v expire expire
|
|
|
|
* -----+-----+--------+---> t
|
|
|
|
* <--------------> HARD
|
|
|
|
* <-----> SOFT
|
|
|
|
*/
|
|
|
|
{
|
2002-01-31 09:35:25 +03:00
|
|
|
sav->lft_c->sadb_lifetime_usetime = time.tv_sec;
|
1999-06-28 10:36:47 +04:00
|
|
|
/* XXX check for expires? */
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dumb version */
|
|
|
|
void
|
|
|
|
key_sa_routechange(dst)
|
|
|
|
struct sockaddr *dst;
|
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secashead *sah;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct route *ro;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
LIST_FOREACH(sah, &sahtree, chain) {
|
|
|
|
ro = &sah->sa_route;
|
2003-07-22 15:12:15 +04:00
|
|
|
if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len &&
|
|
|
|
bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
|
1999-07-31 22:41:15 +04:00
|
|
|
RTFREE(ro->ro_rt);
|
|
|
|
ro->ro_rt = (struct rtentry *)NULL;
|
|
|
|
}
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_chgstate(sav, state)
|
|
|
|
struct secasvar *sav;
|
|
|
|
u_int8_t state;
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav == NULL)
|
|
|
|
panic("key_sa_chgstate called with sav == NULL");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->state == state)
|
1999-06-28 10:36:47 +04:00
|
|
|
return;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (__LIST_CHAINED(sav))
|
|
|
|
LIST_REMOVE(sav, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
sav->state = state;
|
|
|
|
LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-10-05 08:49:17 +04:00
|
|
|
void
|
|
|
|
key_sa_stir_iv(sav)
|
|
|
|
struct secasvar *sav;
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!sav->iv)
|
|
|
|
panic("key_sa_stir_iv called with sav == NULL");
|
|
|
|
key_randomfill(sav->iv, sav->ivlen);
|
|
|
|
}
|
|
|
|
|
2002-05-19 12:22:12 +04:00
|
|
|
static void
|
|
|
|
key_sp_dead(sp)
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
|
|
|
|
|
|
|
/* mark the SP dead */
|
|
|
|
sp->state = IPSEC_SPSTATE_DEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
key_sp_unlink(sp)
|
|
|
|
struct secpolicy *sp;
|
|
|
|
{
|
|
|
|
|
|
|
|
/* remove from SP index */
|
|
|
|
if (__LIST_CHAINED(sp))
|
|
|
|
LIST_REMOVE(sp, chain);
|
|
|
|
key_freesp(sp);
|
|
|
|
}
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
/* XXX too much? */
|
|
|
|
static struct mbuf *
|
|
|
|
key_alloc_mbuf(l)
|
|
|
|
int l;
|
2000-01-31 17:18:52 +03:00
|
|
|
{
|
2000-06-12 14:40:37 +04:00
|
|
|
struct mbuf *m = NULL, *n;
|
|
|
|
int len, t;
|
|
|
|
|
|
|
|
len = l;
|
|
|
|
while (len > 0) {
|
|
|
|
MGET(n, M_DONTWAIT, MT_DATA);
|
|
|
|
if (n && len > MLEN)
|
|
|
|
MCLGET(n, M_DONTWAIT);
|
|
|
|
if (!n) {
|
|
|
|
m_freem(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
n->m_next = NULL;
|
|
|
|
n->m_len = 0;
|
|
|
|
n->m_len = M_TRAILINGSPACE(n);
|
|
|
|
/* use the bottom of mbuf, hoping we can prepend afterwards */
|
|
|
|
if (n->m_len > len) {
|
|
|
|
t = (n->m_len - len) & ~(sizeof(long) - 1);
|
|
|
|
n->m_data += t;
|
|
|
|
n->m_len = len;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
len -= n->m_len;
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
if (m)
|
|
|
|
m_cat(m, n);
|
|
|
|
else
|
|
|
|
m = n;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
return m;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
static int
|
|
|
|
sysctl_net_key_dumpsa(SYSCTLFN_ARGS)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
struct mbuf *m, *n;
|
|
|
|
int err2 = 0;
|
|
|
|
char *p, *ep;
|
|
|
|
size_t len;
|
|
|
|
int s, error;
|
2003-09-12 11:38:10 +04:00
|
|
|
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
if (newp)
|
|
|
|
return (EPERM);
|
|
|
|
if (namelen != 1)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
s = splsoftnet();
|
|
|
|
m = key_setdump(name[0], &error);
|
|
|
|
splx(s);
|
|
|
|
if (!m)
|
2003-09-12 11:38:10 +04:00
|
|
|
return (error);
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
if (!oldp)
|
|
|
|
*oldlenp = m->m_pkthdr.len;
|
|
|
|
else {
|
|
|
|
p = oldp;
|
|
|
|
if (*oldlenp < m->m_pkthdr.len) {
|
|
|
|
err2 = ENOMEM;
|
|
|
|
ep = p + *oldlenp;
|
|
|
|
} else {
|
|
|
|
*oldlenp = m->m_pkthdr.len;
|
|
|
|
ep = p + m->m_pkthdr.len;
|
|
|
|
}
|
|
|
|
for (n = m; n; n = n->m_next) {
|
|
|
|
len = (ep - p < n->m_len) ?
|
|
|
|
ep - p : n->m_len;
|
|
|
|
error = copyout(mtod(n, const void *), p, len);
|
|
|
|
p += len;
|
|
|
|
if (error)
|
|
|
|
break;
|
2003-09-12 11:38:10 +04:00
|
|
|
}
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
if (error == 0)
|
|
|
|
error = err2;
|
|
|
|
}
|
|
|
|
m_freem(m);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_net_key_dumpsp(SYSCTLFN_ARGS)
|
|
|
|
{
|
|
|
|
struct mbuf *m, *n;
|
|
|
|
int err2 = 0;
|
|
|
|
char *p, *ep;
|
|
|
|
size_t len;
|
|
|
|
int s, error;
|
|
|
|
|
|
|
|
if (newp)
|
|
|
|
return (EPERM);
|
|
|
|
if (namelen != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
s = splsoftnet();
|
|
|
|
m = key_setspddump(&error);
|
|
|
|
splx(s);
|
|
|
|
if (!m)
|
2003-09-12 11:38:10 +04:00
|
|
|
return (error);
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
if (!oldp)
|
|
|
|
*oldlenp = m->m_pkthdr.len;
|
|
|
|
else {
|
|
|
|
p = oldp;
|
|
|
|
if (*oldlenp < m->m_pkthdr.len) {
|
|
|
|
err2 = ENOMEM;
|
|
|
|
ep = p + *oldlenp;
|
|
|
|
} else {
|
|
|
|
*oldlenp = m->m_pkthdr.len;
|
|
|
|
ep = p + m->m_pkthdr.len;
|
|
|
|
}
|
|
|
|
for (n = m; n; n = n->m_next) {
|
|
|
|
len = (ep - p < n->m_len) ?
|
|
|
|
ep - p : n->m_len;
|
|
|
|
error = copyout(mtod(n, const void *), p, len);
|
|
|
|
p += len;
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error == 0)
|
|
|
|
error = err2;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
m_freem(m);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_SETUP(sysctl_net_key_setup, "sysctl net.key subtree setup")
|
|
|
|
{
|
|
|
|
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT,
|
|
|
|
CTLTYPE_NODE, "net", NULL,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_NET, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT,
|
|
|
|
CTLTYPE_NODE, "key", NULL,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_NET, PF_KEY, CTL_EOL);
|
|
|
|
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "debug", NULL,
|
|
|
|
NULL, 0, &key_debug_level, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_DEBUG_LEVEL, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "spi_try", NULL,
|
|
|
|
NULL, 0, &key_spi_trycnt, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_SPI_TRY, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "spi_min_value", NULL,
|
|
|
|
NULL, 0, &key_spi_minval, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_SPI_MIN_VALUE, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "spi_max_value", NULL,
|
|
|
|
NULL, 0, &key_spi_maxval, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_SPI_MAX_VALUE, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "random_int", NULL,
|
|
|
|
NULL, 0, &key_int_random, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_RANDOM_INT, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "larval_lifetime", NULL,
|
|
|
|
NULL, 0, &key_larval_lifetime, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_LARVAL_LIFETIME, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "blockacq_count", NULL,
|
|
|
|
NULL, 0, &key_blockacq_count, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_BLOCKACQ_COUNT, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "blockacq_lifetime", NULL,
|
|
|
|
NULL, 0, &key_blockacq_lifetime, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_BLOCKACQ_LIFETIME, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "esp_keymin", NULL,
|
|
|
|
NULL, 0, &ipsec_esp_keymin, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_ESP_KEYMIN, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "esp_auth", NULL,
|
|
|
|
NULL, 0, &ipsec_esp_auth, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_ESP_AUTH, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
|
|
|
|
CTLTYPE_INT, "ah_keymin", NULL,
|
|
|
|
NULL, 0, &ipsec_ah_keymin, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_AH_KEYMIN, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT,
|
|
|
|
CTLTYPE_STRUCT, "dumpsa", NULL,
|
|
|
|
sysctl_net_key_dumpsa, 0, NULL, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_DUMPSA, CTL_EOL);
|
|
|
|
sysctl_createv(SYSCTL_PERMANENT,
|
|
|
|
CTLTYPE_STRUCT, "dumpsp", NULL,
|
|
|
|
sysctl_net_key_dumpsp, 0, NULL, 0,
|
|
|
|
CTL_NET, PF_KEY, KEYCTL_DUMPSP, CTL_EOL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|