2002-10-16 02:10:08 +00:00
|
|
|
/* $FreeBSD$ */
|
|
|
|
/* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
|
|
|
|
|
2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
2017-11-20 19:43:44 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2002-10-16 02:10:08 +00:00
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This code is referd to RFC 2367
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opt_inet.h"
|
|
|
|
#include "opt_inet6.h"
|
|
|
|
#include "opt_ipsec.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
2017-02-06 08:49:57 +00:00
|
|
|
#include <sys/fnv_hash.h>
|
2003-09-01 05:35:55 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/malloc.h>
|
2014-12-24 18:34:56 +00:00
|
|
|
#include <sys/rmlock.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/queue.h>
|
2006-05-20 15:35:36 +00:00
|
|
|
#include <sys/refcount.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
#include <vm/uma.h>
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
#include <net/if.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/if_var.h>
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
#include <net/vnet.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/raw_cb.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_var.h>
|
2017-02-06 08:49:57 +00:00
|
|
|
#include <netinet/udp.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/in6_var.h>
|
|
|
|
#include <netinet6/ip6_var.h>
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
|
|
|
#include <net/pfkeyv2.h>
|
|
|
|
#include <netipsec/keydb.h>
|
|
|
|
#include <netipsec/key.h>
|
|
|
|
#include <netipsec/keysock.h>
|
|
|
|
#include <netipsec/key_debug.h>
|
|
|
|
|
|
|
|
#include <netipsec/ipsec.h>
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netipsec/ipsec6.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netipsec/xform.h>
|
2017-02-06 08:49:57 +00:00
|
|
|
#include <machine/in_cksum.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
|
|
|
/* randomness */
|
|
|
|
#include <sys/random.h>
|
|
|
|
|
|
|
|
#define FULLMASK 0xff
|
|
|
|
#define _BITS(bytes) ((bytes) << 3)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* SA header will be removed in key_delsav(), when the reference count
|
|
|
|
* field hits 0 (= no external reference other than from SA header.
|
|
|
|
*/
|
|
|
|
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
VNET_DEFINE(u_int32_t, key_debug_level) = 0;
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(u_int, key_spi_trycnt) = 1000;
|
|
|
|
static VNET_DEFINE(u_int32_t, key_spi_minval) = 0x100;
|
|
|
|
static VNET_DEFINE(u_int32_t, key_spi_maxval) = 0x0fffffff; /* XXX */
|
|
|
|
static VNET_DEFINE(u_int32_t, policy_id) = 0;
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
/*interval to initialize randseed,1(m)*/
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(u_int, key_int_random) = 60;
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
/* interval to expire acquiring, 30(s)*/
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(u_int, key_larval_lifetime) = 30;
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
/* counter for blocking SADB_ACQUIRE.*/
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(int, key_blockacq_count) = 10;
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
/* lifetime for blocking SADB_ACQUIRE.*/
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(int, key_blockacq_lifetime) = 20;
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
/* preferred old sa rather than new sa.*/
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(int, key_preferred_oldsa) = 1;
|
2010-04-29 11:52:42 +00:00
|
|
|
#define V_key_spi_trycnt VNET(key_spi_trycnt)
|
|
|
|
#define V_key_spi_minval VNET(key_spi_minval)
|
|
|
|
#define V_key_spi_maxval VNET(key_spi_maxval)
|
|
|
|
#define V_policy_id VNET(policy_id)
|
|
|
|
#define V_key_int_random VNET(key_int_random)
|
|
|
|
#define V_key_larval_lifetime VNET(key_larval_lifetime)
|
|
|
|
#define V_key_blockacq_count VNET(key_blockacq_count)
|
|
|
|
#define V_key_blockacq_lifetime VNET(key_blockacq_lifetime)
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_key_preferred_oldsa VNET(key_preferred_oldsa)
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(u_int32_t, acq_seq) = 0;
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_acq_seq VNET(acq_seq)
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static VNET_DEFINE(uint32_t, sp_genid) = 0;
|
|
|
|
#define V_sp_genid VNET(sp_genid)
|
|
|
|
|
|
|
|
/* SPD */
|
|
|
|
TAILQ_HEAD(secpolicy_queue, secpolicy);
|
|
|
|
LIST_HEAD(secpolicy_list, secpolicy);
|
|
|
|
static VNET_DEFINE(struct secpolicy_queue, sptree[IPSEC_DIR_MAX]);
|
|
|
|
static VNET_DEFINE(struct secpolicy_queue, sptree_ifnet[IPSEC_DIR_MAX]);
|
2014-12-24 18:34:56 +00:00
|
|
|
static struct rmlock sptree_lock;
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_sptree VNET(sptree)
|
2017-02-06 08:49:57 +00:00
|
|
|
#define V_sptree_ifnet VNET(sptree_ifnet)
|
2014-12-24 18:34:56 +00:00
|
|
|
#define SPTREE_LOCK_INIT() rm_init(&sptree_lock, "sptree")
|
|
|
|
#define SPTREE_LOCK_DESTROY() rm_destroy(&sptree_lock)
|
|
|
|
#define SPTREE_RLOCK_TRACKER struct rm_priotracker sptree_tracker
|
|
|
|
#define SPTREE_RLOCK() rm_rlock(&sptree_lock, &sptree_tracker)
|
|
|
|
#define SPTREE_RUNLOCK() rm_runlock(&sptree_lock, &sptree_tracker)
|
|
|
|
#define SPTREE_RLOCK_ASSERT() rm_assert(&sptree_lock, RA_RLOCKED)
|
|
|
|
#define SPTREE_WLOCK() rm_wlock(&sptree_lock)
|
|
|
|
#define SPTREE_WUNLOCK() rm_wunlock(&sptree_lock)
|
|
|
|
#define SPTREE_WLOCK_ASSERT() rm_assert(&sptree_lock, RA_WLOCKED)
|
|
|
|
#define SPTREE_UNLOCK_ASSERT() rm_assert(&sptree_lock, RA_UNLOCKED)
|
2003-09-29 22:57:43 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Hash table for lookup SP using unique id */
|
|
|
|
static VNET_DEFINE(struct secpolicy_list *, sphashtbl);
|
|
|
|
static VNET_DEFINE(u_long, sphash_mask);
|
|
|
|
#define V_sphashtbl VNET(sphashtbl)
|
|
|
|
#define V_sphash_mask VNET(sphash_mask)
|
|
|
|
|
|
|
|
#define SPHASH_NHASH_LOG2 7
|
|
|
|
#define SPHASH_NHASH (1 << SPHASH_NHASH_LOG2)
|
|
|
|
#define SPHASH_HASHVAL(id) (key_u32hash(id) & V_sphash_mask)
|
|
|
|
#define SPHASH_HASH(id) &V_sphashtbl[SPHASH_HASHVAL(id)]
|
|
|
|
|
|
|
|
/* SAD */
|
|
|
|
TAILQ_HEAD(secashead_queue, secashead);
|
|
|
|
LIST_HEAD(secashead_list, secashead);
|
|
|
|
static VNET_DEFINE(struct secashead_queue, sahtree);
|
|
|
|
static struct rmlock sahtree_lock;
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_sahtree VNET(sahtree)
|
2017-02-06 08:49:57 +00:00
|
|
|
#define SAHTREE_LOCK_INIT() rm_init(&sahtree_lock, "sahtree")
|
|
|
|
#define SAHTREE_LOCK_DESTROY() rm_destroy(&sahtree_lock)
|
|
|
|
#define SAHTREE_RLOCK_TRACKER struct rm_priotracker sahtree_tracker
|
|
|
|
#define SAHTREE_RLOCK() rm_rlock(&sahtree_lock, &sahtree_tracker)
|
|
|
|
#define SAHTREE_RUNLOCK() rm_runlock(&sahtree_lock, &sahtree_tracker)
|
|
|
|
#define SAHTREE_RLOCK_ASSERT() rm_assert(&sahtree_lock, RA_RLOCKED)
|
|
|
|
#define SAHTREE_WLOCK() rm_wlock(&sahtree_lock)
|
|
|
|
#define SAHTREE_WUNLOCK() rm_wunlock(&sahtree_lock)
|
|
|
|
#define SAHTREE_WLOCK_ASSERT() rm_assert(&sahtree_lock, RA_WLOCKED)
|
|
|
|
#define SAHTREE_UNLOCK_ASSERT() rm_assert(&sahtree_lock, RA_UNLOCKED)
|
|
|
|
|
|
|
|
/* Hash table for lookup in SAD using SA addresses */
|
|
|
|
static VNET_DEFINE(struct secashead_list *, sahaddrhashtbl);
|
|
|
|
static VNET_DEFINE(u_long, sahaddrhash_mask);
|
|
|
|
#define V_sahaddrhashtbl VNET(sahaddrhashtbl)
|
|
|
|
#define V_sahaddrhash_mask VNET(sahaddrhash_mask)
|
|
|
|
|
|
|
|
#define SAHHASH_NHASH_LOG2 7
|
|
|
|
#define SAHHASH_NHASH (1 << SAHHASH_NHASH_LOG2)
|
|
|
|
#define SAHADDRHASH_HASHVAL(saidx) \
|
|
|
|
(key_saidxhash(saidx) & V_sahaddrhash_mask)
|
|
|
|
#define SAHADDRHASH_HASH(saidx) \
|
|
|
|
&V_sahaddrhashtbl[SAHADDRHASH_HASHVAL(saidx)]
|
|
|
|
|
|
|
|
/* Hash table for lookup in SAD using SPI */
|
|
|
|
LIST_HEAD(secasvar_list, secasvar);
|
|
|
|
static VNET_DEFINE(struct secasvar_list *, savhashtbl);
|
|
|
|
static VNET_DEFINE(u_long, savhash_mask);
|
|
|
|
#define V_savhashtbl VNET(savhashtbl)
|
|
|
|
#define V_savhash_mask VNET(savhash_mask)
|
|
|
|
#define SAVHASH_NHASH_LOG2 7
|
|
|
|
#define SAVHASH_NHASH (1 << SAVHASH_NHASH_LOG2)
|
|
|
|
#define SAVHASH_HASHVAL(spi) (key_u32hash(spi) & V_savhash_mask)
|
|
|
|
#define SAVHASH_HASH(spi) &V_savhashtbl[SAVHASH_HASHVAL(spi)]
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
key_saidxhash(const struct secasindex *saidx)
|
|
|
|
{
|
|
|
|
uint32_t hval;
|
|
|
|
|
|
|
|
hval = fnv_32_buf(&saidx->proto, sizeof(saidx->proto),
|
|
|
|
FNV1_32_INIT);
|
|
|
|
switch (saidx->dst.sa.sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
hval = fnv_32_buf(&saidx->src.sin.sin_addr,
|
|
|
|
sizeof(in_addr_t), hval);
|
|
|
|
hval = fnv_32_buf(&saidx->dst.sin.sin_addr,
|
|
|
|
sizeof(in_addr_t), hval);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
hval = fnv_32_buf(&saidx->src.sin6.sin6_addr,
|
|
|
|
sizeof(struct in6_addr), hval);
|
|
|
|
hval = fnv_32_buf(&saidx->dst.sin6.sin6_addr,
|
|
|
|
sizeof(struct in6_addr), hval);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
hval = 0;
|
|
|
|
ipseclog((LOG_DEBUG, "%s: unknown address family %d",
|
|
|
|
__func__, saidx->dst.sa.sa_family));
|
|
|
|
}
|
|
|
|
return (hval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
key_u32hash(uint32_t val)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (fnv_32_buf(&val, sizeof(val), FNV1_32_INIT));
|
|
|
|
}
|
2003-09-29 22:57:43 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/* registed list */
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(LIST_HEAD(_regtree, secreg), regtree[SADB_SATYPE_MAX + 1]);
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_regtree VNET(regtree)
|
2003-09-01 05:35:55 +00:00
|
|
|
static struct mtx regtree_lock;
|
2003-09-29 22:57:43 +00:00
|
|
|
#define REGTREE_LOCK_INIT() \
|
|
|
|
mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
|
|
|
|
#define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock)
|
|
|
|
#define REGTREE_LOCK() mtx_lock(®tree_lock)
|
|
|
|
#define REGTREE_UNLOCK() mtx_unlock(®tree_lock)
|
|
|
|
#define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED)
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Acquiring list */
|
|
|
|
LIST_HEAD(secacq_list, secacq);
|
|
|
|
static VNET_DEFINE(struct secacq_list, acqtree);
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_acqtree VNET(acqtree)
|
2003-09-01 05:35:55 +00:00
|
|
|
static struct mtx acq_lock;
|
2003-09-29 22:57:43 +00:00
|
|
|
#define ACQ_LOCK_INIT() \
|
2017-02-06 08:49:57 +00:00
|
|
|
mtx_init(&acq_lock, "acqtree", "ipsec SA acquiring list", MTX_DEF)
|
2003-09-29 22:57:43 +00:00
|
|
|
#define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock)
|
|
|
|
#define ACQ_LOCK() mtx_lock(&acq_lock)
|
|
|
|
#define ACQ_UNLOCK() mtx_unlock(&acq_lock)
|
|
|
|
#define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED)
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Hash table for lookup in ACQ list using SA addresses */
|
|
|
|
static VNET_DEFINE(struct secacq_list *, acqaddrhashtbl);
|
|
|
|
static VNET_DEFINE(u_long, acqaddrhash_mask);
|
|
|
|
#define V_acqaddrhashtbl VNET(acqaddrhashtbl)
|
|
|
|
#define V_acqaddrhash_mask VNET(acqaddrhash_mask)
|
|
|
|
|
|
|
|
/* Hash table for lookup in ACQ list using SEQ number */
|
|
|
|
static VNET_DEFINE(struct secacq_list *, acqseqhashtbl);
|
|
|
|
static VNET_DEFINE(u_long, acqseqhash_mask);
|
|
|
|
#define V_acqseqhashtbl VNET(acqseqhashtbl)
|
|
|
|
#define V_acqseqhash_mask VNET(acqseqhash_mask)
|
|
|
|
|
|
|
|
#define ACQHASH_NHASH_LOG2 7
|
|
|
|
#define ACQHASH_NHASH (1 << ACQHASH_NHASH_LOG2)
|
|
|
|
#define ACQADDRHASH_HASHVAL(saidx) \
|
|
|
|
(key_saidxhash(saidx) & V_acqaddrhash_mask)
|
|
|
|
#define ACQSEQHASH_HASHVAL(seq) \
|
|
|
|
(key_u32hash(seq) & V_acqseqhash_mask)
|
|
|
|
#define ACQADDRHASH_HASH(saidx) \
|
|
|
|
&V_acqaddrhashtbl[ACQADDRHASH_HASHVAL(saidx)]
|
|
|
|
#define ACQSEQHASH_HASH(seq) \
|
|
|
|
&V_acqseqhashtbl[ACQSEQHASH_HASHVAL(seq)]
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
/* SP acquiring list */
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(LIST_HEAD(_spacqtree, secspacq), spacqtree);
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_spacqtree VNET(spacqtree)
|
2003-09-01 05:35:55 +00:00
|
|
|
static struct mtx spacq_lock;
|
2003-09-29 22:57:43 +00:00
|
|
|
#define SPACQ_LOCK_INIT() \
|
|
|
|
mtx_init(&spacq_lock, "spacqtree", \
|
|
|
|
"fast ipsec security policy acquire list", MTX_DEF)
|
|
|
|
#define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock)
|
|
|
|
#define SPACQ_LOCK() mtx_lock(&spacq_lock)
|
|
|
|
#define SPACQ_UNLOCK() mtx_unlock(&spacq_lock)
|
|
|
|
#define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED)
|
2002-10-16 02:10:08 +00: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 */
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
|
|
|
|
sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
|
|
|
|
sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
|
|
|
|
sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */
|
|
|
|
sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */
|
|
|
|
sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
|
2016-11-25 14:44:49 +00:00
|
|
|
sizeof(struct sadb_x_sa_replay), /* SADB_X_EXT_SA_REPLAY */
|
2017-02-06 08:49:57 +00:00
|
|
|
sizeof(struct sadb_address), /* SADB_X_EXT_NEW_ADDRESS_SRC */
|
|
|
|
sizeof(struct sadb_address), /* SADB_X_EXT_NEW_ADDRESS_DST */
|
2002-10-16 02:10:08 +00:00
|
|
|
};
|
2016-11-25 14:44:49 +00:00
|
|
|
_Static_assert(sizeof(minsize)/sizeof(int) == SADB_EXT_MAX + 1, "minsize size mismatch");
|
|
|
|
|
2002-10-16 02:10:08 +00: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 */
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
|
|
|
|
sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
|
|
|
|
sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
|
|
|
|
0, /* SADB_X_EXT_NAT_T_OAI */
|
|
|
|
0, /* SADB_X_EXT_NAT_T_OAR */
|
|
|
|
sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
|
2016-11-25 14:44:49 +00:00
|
|
|
sizeof(struct sadb_x_sa_replay), /* SADB_X_EXT_SA_REPLAY */
|
2017-02-06 08:49:57 +00:00
|
|
|
0, /* SADB_X_EXT_NEW_ADDRESS_SRC */
|
|
|
|
0, /* SADB_X_EXT_NEW_ADDRESS_DST */
|
2002-10-16 02:10:08 +00:00
|
|
|
};
|
2016-11-25 14:44:49 +00:00
|
|
|
_Static_assert(sizeof(maxsize)/sizeof(int) == SADB_EXT_MAX + 1, "minsize size mismatch");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* Internal values for SA flags:
|
|
|
|
* SADB_X_EXT_F_CLONED means that SA was cloned by key_updateaddresses,
|
|
|
|
* thus we will not free the most of SA content in key_delsav().
|
|
|
|
*/
|
|
|
|
#define SADB_X_EXT_F_CLONED 0x80000000
|
|
|
|
|
|
|
|
#define SADB_CHECKLEN(_mhp, _ext) \
|
|
|
|
((_mhp)->extlen[(_ext)] < minsize[(_ext)] || (maxsize[(_ext)] != 0 && \
|
|
|
|
((_mhp)->extlen[(_ext)] > maxsize[(_ext)])))
|
|
|
|
#define SADB_CHECKHDR(_mhp, _ext) ((_mhp)->ext[(_ext)] == NULL)
|
|
|
|
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(int, ipsec_esp_keymin) = 256;
|
|
|
|
static VNET_DEFINE(int, ipsec_esp_auth) = 0;
|
|
|
|
static VNET_DEFINE(int, ipsec_ah_keymin) = 128;
|
2010-04-29 11:52:42 +00:00
|
|
|
|
|
|
|
#define V_ipsec_esp_keymin VNET(ipsec_esp_keymin)
|
|
|
|
#define V_ipsec_esp_auth VNET(ipsec_esp_auth)
|
2009-07-16 21:13:04 +00:00
|
|
|
#define V_ipsec_ah_keymin VNET(ipsec_ah_keymin)
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
#ifdef IPSEC_DEBUG
|
|
|
|
VNET_DEFINE(int, ipsec_debug) = 1;
|
|
|
|
#else
|
|
|
|
VNET_DEFINE(int, ipsec_debug) = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INET
|
|
|
|
SYSCTL_DECL(_net_inet_ipsec);
|
|
|
|
SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG, debug,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
|
|
|
|
"Enable IPsec debugging output when set.");
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
SYSCTL_DECL(_net_inet6_ipsec6);
|
|
|
|
SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG, debug,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
|
|
|
|
"Enable IPsec debugging output when set.");
|
2002-10-16 02:10:08 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SYSCTL_DECL(_net_key);
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_debug_level), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* max count of trial for the decision of spi value */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_trycnt), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* minimum spi value to allocate automatically. */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_minval), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* maximun spi value to allocate automatically. */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_maxval), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* interval to initialize randseed */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_int_random), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* lifetime for larval SA */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_larval_lifetime), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* counter for blocking to send SADB_ACQUIRE to IKEd */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_blockacq_count), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* lifetime for blocking to send SADB_ACQUIRE to IKEd */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_blockacq_lifetime), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* ESP auth */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_esp_auth), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* minimum ESP key length */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_esp_keymin), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* minimum AH key length */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_ah_keymin), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* perfered old SA rather than new SA */
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, preferred_oldsa,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_preferred_oldsa), 0, "");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
#define __LIST_CHAINED(elm) \
|
|
|
|
(!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
|
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
|
|
|
|
MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
|
|
|
|
MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
|
|
|
|
MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
|
|
|
|
MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
|
|
|
|
MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
|
|
|
|
MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static VNET_DEFINE(uma_zone_t, key_lft_zone);
|
|
|
|
#define V_key_lft_zone VNET(key_lft_zone)
|
|
|
|
|
|
|
|
static LIST_HEAD(xforms_list, xformsw) xforms = LIST_HEAD_INITIALIZER();
|
|
|
|
static struct mtx xforms_lock;
|
|
|
|
#define XFORMS_LOCK_INIT() \
|
|
|
|
mtx_init(&xforms_lock, "xforms_list", "IPsec transforms list", MTX_DEF)
|
|
|
|
#define XFORMS_LOCK_DESTROY() mtx_destroy(&xforms_lock)
|
|
|
|
#define XFORMS_LOCK() mtx_lock(&xforms_lock)
|
|
|
|
#define XFORMS_UNLOCK() mtx_unlock(&xforms_lock)
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
|
|
|
* set parameters into secpolicyindex buffer.
|
|
|
|
* Must allocate secpolicyindex buffer passed to this function.
|
|
|
|
*/
|
|
|
|
#define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
|
|
|
|
do { \
|
|
|
|
bzero((idx), sizeof(struct secpolicyindex)); \
|
|
|
|
(idx)->dir = (_dir); \
|
|
|
|
(idx)->prefs = (ps); \
|
|
|
|
(idx)->prefd = (pd); \
|
|
|
|
(idx)->ul_proto = (ulp); \
|
|
|
|
bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
|
|
|
|
bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set parameters into secasindex buffer.
|
|
|
|
* Must allocate secasindex buffer before calling this function.
|
|
|
|
*/
|
|
|
|
#define KEY_SETSECASIDX(p, m, r, s, d, idx) \
|
|
|
|
do { \
|
|
|
|
bzero((idx), sizeof(struct secasindex)); \
|
|
|
|
(idx)->proto = (p); \
|
|
|
|
(idx)->mode = (m); \
|
|
|
|
(idx)->reqid = (r); \
|
|
|
|
bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
|
|
|
|
bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
|
2017-02-06 08:49:57 +00:00
|
|
|
key_porttosaddr(&(idx)->src.sa, 0); \
|
|
|
|
key_porttosaddr(&(idx)->dst.sa, 0); \
|
2002-10-16 02:10:08 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* key statistics */
|
|
|
|
struct _keystat {
|
|
|
|
u_long getspi_count; /* the avarage of count to try to get new SPI */
|
|
|
|
} keystat;
|
|
|
|
|
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static struct supported_ealgs {
|
|
|
|
int sadb_alg;
|
|
|
|
const struct enc_xform *xform;
|
|
|
|
} supported_ealgs[] = {
|
|
|
|
{ SADB_EALG_DESCBC, &enc_xform_des },
|
|
|
|
{ SADB_EALG_3DESCBC, &enc_xform_3des },
|
|
|
|
{ SADB_X_EALG_AES, &enc_xform_rijndael128 },
|
|
|
|
{ SADB_X_EALG_BLOWFISHCBC, &enc_xform_blf },
|
|
|
|
{ SADB_X_EALG_CAST128CBC, &enc_xform_cast5 },
|
|
|
|
{ SADB_EALG_NULL, &enc_xform_null },
|
|
|
|
{ SADB_X_EALG_CAMELLIACBC, &enc_xform_camellia },
|
|
|
|
{ SADB_X_EALG_AESCTR, &enc_xform_aes_icm },
|
|
|
|
{ SADB_X_EALG_AESGCM16, &enc_xform_aes_nist_gcm },
|
|
|
|
{ SADB_X_EALG_AESGMAC, &enc_xform_aes_nist_gmac },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct supported_aalgs {
|
|
|
|
int sadb_alg;
|
|
|
|
const struct auth_hash *xform;
|
|
|
|
} supported_aalgs[] = {
|
|
|
|
{ SADB_X_AALG_NULL, &auth_hash_null },
|
|
|
|
{ SADB_AALG_MD5HMAC, &auth_hash_hmac_md5 },
|
|
|
|
{ SADB_AALG_SHA1HMAC, &auth_hash_hmac_sha1 },
|
|
|
|
{ SADB_X_AALG_RIPEMD160HMAC, &auth_hash_hmac_ripemd_160 },
|
|
|
|
{ SADB_X_AALG_MD5, &auth_hash_key_md5 },
|
|
|
|
{ SADB_X_AALG_SHA, &auth_hash_key_sha1 },
|
|
|
|
{ SADB_X_AALG_SHA2_256, &auth_hash_hmac_sha2_256 },
|
|
|
|
{ SADB_X_AALG_SHA2_384, &auth_hash_hmac_sha2_384 },
|
|
|
|
{ SADB_X_AALG_SHA2_512, &auth_hash_hmac_sha2_512 },
|
|
|
|
{ SADB_X_AALG_AES128GMAC, &auth_hash_nist_gmac_aes_128 },
|
|
|
|
{ SADB_X_AALG_AES192GMAC, &auth_hash_nist_gmac_aes_192 },
|
|
|
|
{ SADB_X_AALG_AES256GMAC, &auth_hash_nist_gmac_aes_256 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct supported_calgs {
|
|
|
|
int sadb_alg;
|
|
|
|
const struct comp_algo *xform;
|
|
|
|
} supported_calgs[] = {
|
|
|
|
{ SADB_X_CALG_DEFLATE, &comp_algo_deflate },
|
|
|
|
};
|
|
|
|
|
2014-10-23 20:43:16 +00:00
|
|
|
#ifndef IPSEC_DEBUG2
|
|
|
|
static struct callout key_timer;
|
|
|
|
#endif
|
|
|
|
|
2014-12-24 18:34:56 +00:00
|
|
|
static void key_unlink(struct secpolicy *);
|
2014-12-03 04:08:41 +00:00
|
|
|
static struct secpolicy *key_getsp(struct secpolicyindex *);
|
|
|
|
static struct secpolicy *key_getspbyid(u_int32_t);
|
|
|
|
static struct mbuf *key_gather_mbuf(struct mbuf *,
|
|
|
|
const struct sadb_msghdr *, int, int, ...);
|
|
|
|
static int key_spdadd(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint32_t key_getnewspid(void);
|
2014-12-03 04:08:41 +00:00
|
|
|
static int key_spddelete(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_spddelete2(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_spdget(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_spdflush(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_spddump(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static struct mbuf *key_setdumpsp(struct secpolicy *,
|
|
|
|
u_int8_t, u_int32_t, u_int32_t);
|
2017-02-06 08:49:57 +00:00
|
|
|
static struct mbuf *key_sp2mbuf(struct secpolicy *);
|
|
|
|
static size_t key_getspreqmsglen(struct secpolicy *);
|
2014-12-03 04:08:41 +00:00
|
|
|
static int key_spdexpire(struct secpolicy *);
|
|
|
|
static struct secashead *key_newsah(struct secasindex *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static void key_freesah(struct secashead **);
|
2014-12-03 04:08:41 +00:00
|
|
|
static void key_delsah(struct secashead *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static struct secasvar *key_newsav(const struct sadb_msghdr *,
|
|
|
|
struct secasindex *, uint32_t, int *);
|
2014-12-03 04:08:41 +00:00
|
|
|
static void key_delsav(struct secasvar *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static void key_unlinksav(struct secasvar *);
|
2014-12-03 04:08:41 +00:00
|
|
|
static struct secashead *key_getsah(struct secasindex *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static int key_checkspidup(uint32_t);
|
|
|
|
static struct secasvar *key_getsavbyspi(uint32_t);
|
|
|
|
static int key_setnatt(struct secasvar *, const struct sadb_msghdr *);
|
|
|
|
static int key_setsaval(struct secasvar *, const struct sadb_msghdr *);
|
|
|
|
static int key_updatelifetimes(struct secasvar *, const struct sadb_msghdr *);
|
|
|
|
static int key_updateaddresses(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *, struct secasvar *, struct secasindex *);
|
|
|
|
|
2014-12-03 04:08:41 +00:00
|
|
|
static struct mbuf *key_setdumpsa(struct secasvar *, u_int8_t,
|
|
|
|
u_int8_t, u_int32_t, u_int32_t);
|
|
|
|
static struct mbuf *key_setsadbmsg(u_int8_t, u_int16_t, u_int8_t,
|
|
|
|
u_int32_t, pid_t, u_int16_t);
|
|
|
|
static struct mbuf *key_setsadbsa(struct secasvar *);
|
|
|
|
static struct mbuf *key_setsadbaddr(u_int16_t,
|
|
|
|
const struct sockaddr *, u_int8_t, u_int16_t);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
static struct mbuf *key_setsadbxport(u_int16_t, u_int16_t);
|
|
|
|
static struct mbuf *key_setsadbxtype(u_int16_t);
|
2014-12-03 04:08:41 +00:00
|
|
|
static struct mbuf *key_setsadbxsa2(u_int8_t, u_int32_t, u_int32_t);
|
2016-11-25 14:44:49 +00:00
|
|
|
static struct mbuf *key_setsadbxsareplay(u_int32_t);
|
2014-12-03 04:08:41 +00:00
|
|
|
static struct mbuf *key_setsadbxpolicy(u_int16_t, u_int8_t,
|
2015-11-17 14:39:33 +00:00
|
|
|
u_int32_t, u_int32_t);
|
2017-02-06 08:49:57 +00:00
|
|
|
static struct seckey *key_dup_keymsg(const struct sadb_key *, size_t,
|
|
|
|
struct malloc_type *);
|
2006-03-25 13:38:52 +00:00
|
|
|
static struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src,
|
2017-02-06 08:49:57 +00:00
|
|
|
struct malloc_type *);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* flags for key_cmpsaidx() */
|
|
|
|
#define CMP_HEAD 1 /* protocol, addresses. */
|
|
|
|
#define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
|
|
|
|
#define CMP_REQID 3 /* additionally HEAD, reaid. */
|
|
|
|
#define CMP_EXACTLY 4 /* all elements. */
|
2014-12-03 04:08:41 +00:00
|
|
|
static int key_cmpsaidx(const struct secasindex *,
|
|
|
|
const struct secasindex *, int);
|
|
|
|
static int key_cmpspidx_exactly(struct secpolicyindex *,
|
|
|
|
struct secpolicyindex *);
|
|
|
|
static int key_cmpspidx_withmask(struct secpolicyindex *,
|
|
|
|
struct secpolicyindex *);
|
|
|
|
static int key_bbcmp(const void *, const void *, u_int);
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint8_t key_satype2proto(uint8_t);
|
|
|
|
static uint8_t key_proto2satype(uint8_t);
|
2014-12-03 04:08:41 +00:00
|
|
|
|
|
|
|
static int key_getspi(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint32_t key_do_getnewspi(struct sadb_spirange *, struct secasindex *);
|
2014-12-03 04:08:41 +00:00
|
|
|
static int key_update(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_add(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static int key_setident(struct secashead *, const struct sadb_msghdr *);
|
2014-12-03 04:08:41 +00:00
|
|
|
static struct mbuf *key_getmsgbuf_x1(struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_delete(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_delete_all(struct socket *, struct mbuf *,
|
2017-02-06 08:49:57 +00:00
|
|
|
const struct sadb_msghdr *, struct secasindex *);
|
|
|
|
static void key_delete_xform(const struct xformsw *);
|
2014-12-03 04:08:41 +00:00
|
|
|
static int key_get(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
|
|
|
|
static void key_getcomb_setlifetime(struct sadb_comb *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static struct mbuf *key_getcomb_ealg(void);
|
2014-12-03 04:08:41 +00:00
|
|
|
static struct mbuf *key_getcomb_ah(void);
|
|
|
|
static struct mbuf *key_getcomb_ipcomp(void);
|
|
|
|
static struct mbuf *key_getprop(const struct secasindex *);
|
|
|
|
|
|
|
|
static int key_acquire(const struct secasindex *, struct secpolicy *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint32_t key_newacq(const struct secasindex *, int *);
|
|
|
|
static uint32_t key_getacq(const struct secasindex *, int *);
|
|
|
|
static int key_acqdone(const struct secasindex *, uint32_t);
|
|
|
|
static int key_acqreset(uint32_t);
|
2014-12-03 04:08:41 +00:00
|
|
|
static struct secspacq *key_newspacq(struct secpolicyindex *);
|
|
|
|
static struct secspacq *key_getspacq(struct secpolicyindex *);
|
|
|
|
static int key_acquire2(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_register(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
2015-05-19 08:30:04 +00:00
|
|
|
static int key_expire(struct secasvar *, int);
|
2014-12-03 04:08:41 +00:00
|
|
|
static int key_flush(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_dump(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_promisc(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *);
|
|
|
|
static int key_senderror(struct socket *, struct mbuf *, int);
|
|
|
|
static int key_validate_ext(const struct sadb_ext *, int);
|
|
|
|
static int key_align(struct mbuf *, struct sadb_msghdr *);
|
2017-02-06 08:49:57 +00:00
|
|
|
static struct mbuf *key_setlifetime(struct seclifetime *, uint16_t);
|
|
|
|
static struct mbuf *key_setkey(struct seckey *, uint16_t);
|
|
|
|
static int xform_init(struct secasvar *, u_short);
|
|
|
|
|
|
|
|
#define DBG_IPSEC_INITREF(t, p) do { \
|
|
|
|
refcount_init(&(p)->refcnt, 1); \
|
|
|
|
KEYDBG(KEY_STAMP, \
|
|
|
|
printf("%s: Initialize refcnt %s(%p) = %u\n", \
|
|
|
|
__func__, #t, (p), (p)->refcnt)); \
|
|
|
|
} while (0)
|
|
|
|
#define DBG_IPSEC_ADDREF(t, p) do { \
|
|
|
|
refcount_acquire(&(p)->refcnt); \
|
|
|
|
KEYDBG(KEY_STAMP, \
|
|
|
|
printf("%s: Acquire refcnt %s(%p) -> %u\n", \
|
|
|
|
__func__, #t, (p), (p)->refcnt)); \
|
|
|
|
} while (0)
|
|
|
|
#define DBG_IPSEC_DELREF(t, p) do { \
|
|
|
|
KEYDBG(KEY_STAMP, \
|
|
|
|
printf("%s: Release refcnt %s(%p) -> %u\n", \
|
|
|
|
__func__, #t, (p), (p)->refcnt - 1)); \
|
|
|
|
refcount_release(&(p)->refcnt); \
|
|
|
|
} while (0)
|
2006-05-20 15:35:36 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
#define IPSEC_INITREF(t, p) refcount_init(&(p)->refcnt, 1)
|
|
|
|
#define IPSEC_ADDREF(t, p) refcount_acquire(&(p)->refcnt)
|
|
|
|
#define IPSEC_DELREF(t, p) refcount_release(&(p)->refcnt)
|
2006-05-20 15:35:36 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
#define SP_INITREF(p) IPSEC_INITREF(SP, p)
|
|
|
|
#define SP_ADDREF(p) IPSEC_ADDREF(SP, p)
|
|
|
|
#define SP_DELREF(p) IPSEC_DELREF(SP, p)
|
2006-05-20 15:35:36 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
#define SAH_INITREF(p) IPSEC_INITREF(SAH, p)
|
|
|
|
#define SAH_ADDREF(p) IPSEC_ADDREF(SAH, p)
|
|
|
|
#define SAH_DELREF(p) IPSEC_DELREF(SAH, p)
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
#define SAV_INITREF(p) IPSEC_INITREF(SAV, p)
|
|
|
|
#define SAV_ADDREF(p) IPSEC_ADDREF(SAV, p)
|
|
|
|
#define SAV_DELREF(p) IPSEC_DELREF(SAV, p)
|
2004-09-30 01:08:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the refcnt while holding the SPTREE lock.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
key_addref(struct secpolicy *sp)
|
|
|
|
{
|
2014-12-24 18:34:56 +00:00
|
|
|
|
2004-09-30 01:08:02 +00:00
|
|
|
SP_ADDREF(sp);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 0 when there are known to be no SP's for the specified
|
|
|
|
* direction. Otherwise return 1. This is used by IPsec code
|
|
|
|
* to optimize performance.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
key_havesp(u_int dir)
|
|
|
|
{
|
Step 1.5 of importing the network stack virtualization infrastructure
from the vimage project, as per plan established at devsummit 08/08:
http://wiki.freebsd.org/Image/Notes200808DevSummit
Introduce INIT_VNET_*() initializer macros, VNET_FOREACH() iterator
macros, and CURVNET_SET() context setting macros, all currently
resolving to NOPs.
Prepare for virtualization of selected SYSCTL objects by introducing a
family of SYSCTL_V_*() macros, currently resolving to their global
counterparts, i.e. SYSCTL_V_INT() == SYSCTL_INT().
Move selected #defines from sys/sys/vimage.h to newly introduced header
files specific to virtualized subsystems (sys/net/vnet.h,
sys/netinet/vinet.h etc.).
All the changes are verified to have zero functional impact at this
point in time by doing MD5 comparision between pre- and post-change
object files(*).
(*) netipsec/keysock.c did not validate depending on compile time options.
Implemented by: julian, bz, brooks, zec
Reviewed by: julian, bz, brooks, kris, rwatson, ...
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
2008-10-02 15:37:58 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
|
2014-12-24 18:34:56 +00:00
|
|
|
TAILQ_FIRST(&V_sptree[dir]) != NULL : 1);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* %%% IPsec policy management */
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Return current SPDB generation.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t
|
|
|
|
key_getspgen(void)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
return (V_sp_genid);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
void
|
|
|
|
key_bumpspgen(void)
|
|
|
|
{
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
V_sp_genid++;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static int
|
|
|
|
key_checksockaddrs(struct sockaddr *src, struct sockaddr *dst)
|
|
|
|
{
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* family match */
|
|
|
|
if (src->sa_family != dst->sa_family)
|
|
|
|
return (EINVAL);
|
|
|
|
/* sa_len match */
|
|
|
|
if (src->sa_len != dst->sa_len)
|
|
|
|
return (EINVAL);
|
|
|
|
switch (src->sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
if (src->sa_len != sizeof(struct sockaddr_in))
|
|
|
|
return (EINVAL);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
if (src->sa_len != sizeof(struct sockaddr_in6))
|
|
|
|
return (EINVAL);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return (EAFNOSUPPORT);
|
|
|
|
}
|
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_allocsp(struct secpolicyindex *spidx, u_int dir)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secpolicy *sp;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(spidx != NULL, ("null spidx"));
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
|
|
|
|
("invalid direction %u", dir));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK();
|
|
|
|
TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
|
2017-02-06 08:49:57 +00:00
|
|
|
if (key_cmpspidx_withmask(&sp->spidx, spidx)) {
|
|
|
|
SP_ADDREF(sp);
|
|
|
|
break;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SPTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sp != NULL) { /* found a SPD entry */
|
2002-10-16 02:10:08 +00:00
|
|
|
sp->lastused = time_second;
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: return SP(%p)\n", __func__, sp));
|
|
|
|
KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
|
|
|
|
} else {
|
|
|
|
KEYDBG(IPSEC_DATA,
|
|
|
|
printf("%s: lookup failed for ", __func__);
|
|
|
|
kdebug_secpolicyindex(spidx, NULL));
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
return (sp);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Allocating an SA entry for an *INBOUND* or *OUTBOUND* TCP packet, signed
|
|
|
|
* or should be signed by MD5 signature.
|
|
|
|
* We don't use key_allocsa() for such lookups, because we don't know SPI.
|
|
|
|
* Unlike ESP and AH protocols, SPI isn't transmitted in the TCP header with
|
|
|
|
* signed packet. We use SADB only as storage for password.
|
|
|
|
* OUT: positive: corresponding SA for given saidx found.
|
|
|
|
* NULL: SA not found
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secasvar *
|
|
|
|
key_allocsa_tcpmd5(struct secasindex *saidx)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(saidx->proto == IPPROTO_TCP,
|
|
|
|
("unexpected security protocol %u", saidx->proto));
|
|
|
|
IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TCPMD5,
|
|
|
|
("unexpected mode %u", saidx->mode));
|
|
|
|
|
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
|
|
|
|
KEYDBG(IPSEC_DUMP,
|
|
|
|
printf("%s: checking SAH\n", __func__);
|
|
|
|
kdebug_secash(sah, " "));
|
|
|
|
if (sah->saidx.proto != IPPROTO_TCP)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
2017-04-04 13:41:50 +00:00
|
|
|
if (!key_sockaddrcmp(&saidx->dst.sa, &sah->saidx.dst.sa, 0) &&
|
|
|
|
!key_sockaddrcmp(&saidx->src.sa, &sah->saidx.src.sa, 0))
|
2017-02-06 08:49:57 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sah != NULL) {
|
|
|
|
if (V_key_preferred_oldsa)
|
|
|
|
sav = TAILQ_LAST(&sah->savtree_alive, secasvar_queue);
|
|
|
|
else
|
|
|
|
sav = TAILQ_FIRST(&sah->savtree_alive);
|
|
|
|
if (sav != NULL)
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
} else
|
|
|
|
sav = NULL;
|
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
|
|
|
|
if (sav != NULL) {
|
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: return SA(%p)\n", __func__, sav));
|
|
|
|
KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
|
|
|
|
} else {
|
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: SA not found\n", __func__));
|
|
|
|
KEYDBG(IPSEC_DATA, kdebug_secasindex(saidx, NULL));
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
return (sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Allocating an SA entry for an *OUTBOUND* packet.
|
|
|
|
* OUT: positive: corresponding SA for given saidx found.
|
|
|
|
* NULL: SA not found, but will be acquired, check *error
|
|
|
|
* for acquiring status.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secasvar *
|
|
|
|
key_allocsa_policy(struct secpolicy *sp, const struct secasindex *saidx,
|
|
|
|
int *error)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
|
|
|
struct secashead *sah;
|
2011-03-31 15:23:32 +00:00
|
|
|
struct secasvar *sav;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(saidx != NULL, ("null saidx"));
|
|
|
|
IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
|
2002-10-16 02:10:08 +00:00
|
|
|
saidx->mode == IPSEC_MODE_TUNNEL,
|
2003-09-29 22:57:43 +00:00
|
|
|
("unexpected policy %u", saidx->mode));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/*
|
2011-03-31 15:23:32 +00:00
|
|
|
* We check new SA in the IPsec request because a different
|
2002-10-16 02:10:08 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
|
|
|
|
KEYDBG(IPSEC_DUMP,
|
|
|
|
printf("%s: checking SAH\n", __func__);
|
|
|
|
kdebug_secash(sah, " "));
|
|
|
|
if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
|
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sah != NULL) {
|
|
|
|
/*
|
|
|
|
* Allocate the oldest SA available according to
|
|
|
|
* draft-jenkins-ipsec-rekeying-03.
|
|
|
|
*/
|
|
|
|
if (V_key_preferred_oldsa)
|
|
|
|
sav = TAILQ_LAST(&sah->savtree_alive, secasvar_queue);
|
|
|
|
else
|
|
|
|
sav = TAILQ_FIRST(&sah->savtree_alive);
|
|
|
|
if (sav != NULL)
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
} else
|
|
|
|
sav = NULL;
|
|
|
|
SAHTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav != NULL) {
|
|
|
|
*error = 0;
|
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: chosen SA(%p) for SP(%p)\n", __func__,
|
|
|
|
sav, sp));
|
|
|
|
KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
|
|
|
|
return (sav); /* return referenced SA */
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
/* there is no SA */
|
|
|
|
*error = key_acquire(saidx, sp);
|
|
|
|
if ((*error) != 0)
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: error %d returned from key_acquire()\n",
|
|
|
|
__func__, *error));
|
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: acquire SA for SP(%p), error %d\n",
|
|
|
|
__func__, sp, *error));
|
|
|
|
KEYDBG(IPSEC_DATA, kdebug_secasindex(saidx, NULL));
|
|
|
|
return (NULL);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* allocating a usable SA entry for a *INBOUND* packet.
|
|
|
|
* Must call key_freesav() later.
|
|
|
|
* OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
|
|
|
|
* NULL: not found, or error occurred.
|
|
|
|
*
|
|
|
|
* According to RFC 2401 SA is uniquely identified by a triple SPI,
|
|
|
|
* destination address, and security protocol. But according to RFC 4301,
|
|
|
|
* SPI by itself suffices to specify an SA.
|
|
|
|
*
|
|
|
|
* Note that, however, we do need to keep source address in IPsec SA.
|
|
|
|
* IKE specification and PF_KEY specification do assume that we
|
|
|
|
* keep source address in IPsec SA. We see a tricky situation here.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secasvar *
|
|
|
|
key_allocsa(union sockaddr_union *dst, uint8_t proto, uint32_t spi)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secasvar *sav;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(proto == IPPROTO_ESP || proto == IPPROTO_AH ||
|
|
|
|
proto == IPPROTO_IPCOMP, ("unexpected security protocol %u",
|
|
|
|
proto));
|
2009-09-06 18:09:25 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sav, SAVHASH_HASH(spi), spihash) {
|
|
|
|
if (sav->spi == spi)
|
2009-09-06 07:32:16 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* We use single SPI namespace for all protocols, so it is
|
|
|
|
* impossible to have SPI duplicates in the SAVHASH.
|
|
|
|
*/
|
|
|
|
if (sav != NULL) {
|
|
|
|
if (sav->state != SADB_SASTATE_LARVAL &&
|
|
|
|
sav->sah->saidx.proto == proto &&
|
|
|
|
key_sockaddrcmp(&dst->sa,
|
|
|
|
&sav->sah->saidx.dst.sa, 0) == 0)
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
else
|
|
|
|
sav = NULL;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav == NULL) {
|
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
char buf[IPSEC_ADDRSTRLEN];
|
|
|
|
printf("%s: SA not found for spi %u proto %u dst %s\n",
|
|
|
|
__func__, ntohl(spi), proto, ipsec_address(dst, buf,
|
|
|
|
sizeof(buf))));
|
|
|
|
} else {
|
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: return SA(%p)\n", __func__, sav));
|
|
|
|
KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
|
|
|
|
}
|
|
|
|
return (sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secasvar *
|
|
|
|
key_allocsa_tunnel(union sockaddr_union *src, union sockaddr_union *dst,
|
|
|
|
uint8_t proto)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(src != NULL, ("null src address"));
|
|
|
|
IPSEC_ASSERT(dst != NULL, ("null dst address"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEY_SETSECASIDX(proto, IPSEC_MODE_TUNNEL, 0, &src->sa,
|
|
|
|
&dst->sa, &saidx);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav = NULL;
|
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sah, SAHADDRHASH_HASH(&saidx), addrhash) {
|
|
|
|
if (IPSEC_MODE_TUNNEL != sah->saidx.mode)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
2017-02-06 08:49:57 +00:00
|
|
|
if (proto != sah->saidx.proto)
|
|
|
|
continue;
|
2017-03-29 10:24:48 +00:00
|
|
|
if (key_sockaddrcmp(&src->sa, &sah->saidx.src.sa, 0) != 0)
|
2017-02-06 08:49:57 +00:00
|
|
|
continue;
|
2017-03-29 10:24:48 +00:00
|
|
|
if (key_sockaddrcmp(&dst->sa, &sah->saidx.dst.sa, 0) != 0)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
2017-02-06 08:49:57 +00:00
|
|
|
/* XXXAE: is key_preferred_oldsa reasonably?*/
|
|
|
|
if (V_key_preferred_oldsa)
|
|
|
|
sav = TAILQ_LAST(&sah->savtree_alive, secasvar_queue);
|
|
|
|
else
|
|
|
|
sav = TAILQ_FIRST(&sah->savtree_alive);
|
|
|
|
if (sav != NULL) {
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
}
|
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: return SA(%p)\n", __func__, sav));
|
|
|
|
if (sav != NULL)
|
|
|
|
KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
|
|
|
|
return (sav);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* Must be called after calling key_allocsp().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
key_freesp(struct secpolicy **spp)
|
|
|
|
{
|
|
|
|
struct secpolicy *sp = *spp;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(sp != NULL, ("null sp"));
|
|
|
|
if (SP_DELREF(sp) == 0)
|
|
|
|
return;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: last reference to SP(%p)\n", __func__, sp));
|
|
|
|
KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
*spp = NULL;
|
|
|
|
while (sp->tcount > 0)
|
|
|
|
ipsec_delisr(sp->req[--sp->tcount]);
|
|
|
|
free(sp, M_IPSEC_SP);
|
|
|
|
}
|
2004-02-05 23:19:17 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static void
|
|
|
|
key_unlink(struct secpolicy *sp)
|
|
|
|
{
|
2004-02-05 23:19:17 +00:00
|
|
|
|
2014-12-24 18:34:56 +00:00
|
|
|
IPSEC_ASSERT(sp->spidx.dir == IPSEC_DIR_INBOUND ||
|
|
|
|
sp->spidx.dir == IPSEC_DIR_OUTBOUND,
|
|
|
|
("invalid direction %u", sp->spidx.dir));
|
|
|
|
SPTREE_UNLOCK_ASSERT();
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SP(%p)\n", __func__, sp));
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_WLOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sp->state != IPSEC_SPSTATE_ALIVE) {
|
|
|
|
/* SP is already unlinked */
|
2015-02-24 10:35:07 +00:00
|
|
|
SPTREE_WUNLOCK();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sp->state = IPSEC_SPSTATE_DEAD;
|
2014-12-24 18:34:56 +00:00
|
|
|
TAILQ_REMOVE(&V_sptree[sp->spidx.dir], sp, chain);
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_REMOVE(sp, idhash);
|
|
|
|
V_sp_genid++;
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_WUNLOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&sp);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 14:39:33 +00:00
|
|
|
/*
|
|
|
|
* insert a secpolicy into the SP database. Lower priorities first
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
key_insertsp(struct secpolicy *newsp)
|
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SPTREE_WLOCK_ASSERT();
|
2015-11-17 14:39:33 +00:00
|
|
|
TAILQ_FOREACH(sp, &V_sptree[newsp->spidx.dir], chain) {
|
|
|
|
if (newsp->priority < sp->priority) {
|
|
|
|
TAILQ_INSERT_BEFORE(sp, newsp, chain);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TAILQ_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, chain);
|
|
|
|
done:
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_INSERT_HEAD(SPHASH_HASH(newsp->id), newsp, idhash);
|
2015-11-17 14:39:33 +00:00
|
|
|
newsp->state = IPSEC_SPSTATE_ALIVE;
|
2017-02-06 08:49:57 +00:00
|
|
|
V_sp_genid++;
|
2015-11-17 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Insert a bunch of VTI secpolicies into the SPDB.
|
|
|
|
* We keep VTI policies in the separate list due to following reasons:
|
|
|
|
* 1) they should be immutable to user's or some deamon's attempts to
|
|
|
|
* delete. The only way delete such policies - destroy or unconfigure
|
|
|
|
* corresponding virtual inteface.
|
|
|
|
* 2) such policies have traffic selector that matches all traffic per
|
|
|
|
* address family.
|
|
|
|
* Since all VTI policies have the same priority, we don't care about
|
|
|
|
* policies order.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
int
|
|
|
|
key_register_ifnet(struct secpolicy **spp, u_int count)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct mbuf *m;
|
|
|
|
u_int i;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SPTREE_WLOCK();
|
|
|
|
/*
|
|
|
|
* First of try to acquire id for each SP.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
IPSEC_ASSERT(spp[i]->spidx.dir == IPSEC_DIR_INBOUND ||
|
|
|
|
spp[i]->spidx.dir == IPSEC_DIR_OUTBOUND,
|
|
|
|
("invalid direction %u", spp[i]->spidx.dir));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if ((spp[i]->id = key_getnewspid()) == 0) {
|
|
|
|
SPTREE_WUNLOCK();
|
|
|
|
return (EAGAIN);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
TAILQ_INSERT_TAIL(&V_sptree_ifnet[spp[i]->spidx.dir],
|
|
|
|
spp[i], chain);
|
|
|
|
/*
|
|
|
|
* NOTE: despite the fact that we keep VTI SP in the
|
|
|
|
* separate list, SPHASH contains policies from both
|
|
|
|
* sources. Thus SADB_X_SPDGET will correctly return
|
|
|
|
* SP by id, because it uses SPHASH for lookups.
|
|
|
|
*/
|
|
|
|
LIST_INSERT_HEAD(SPHASH_HASH(spp[i]->id), spp[i], idhash);
|
|
|
|
spp[i]->state = IPSEC_SPSTATE_IFNET;
|
|
|
|
}
|
|
|
|
SPTREE_WUNLOCK();
|
|
|
|
/*
|
|
|
|
* Notify user processes about new SP.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
m = key_setdumpsp(spp[i], SADB_X_SPDADD, 0, 0);
|
|
|
|
if (m != NULL)
|
|
|
|
key_sendup_mbuf(NULL, m, KEY_SENDUP_ALL);
|
|
|
|
}
|
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-31 15:23:32 +00:00
|
|
|
void
|
2017-02-06 08:49:57 +00:00
|
|
|
key_unregister_ifnet(struct secpolicy **spp, u_int count)
|
2011-03-31 15:23:32 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct mbuf *m;
|
|
|
|
u_int i;
|
2011-03-31 15:23:32 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SPTREE_WLOCK();
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
IPSEC_ASSERT(spp[i]->spidx.dir == IPSEC_DIR_INBOUND ||
|
|
|
|
spp[i]->spidx.dir == IPSEC_DIR_OUTBOUND,
|
|
|
|
("invalid direction %u", spp[i]->spidx.dir));
|
|
|
|
|
|
|
|
if (spp[i]->state != IPSEC_SPSTATE_IFNET)
|
|
|
|
continue;
|
|
|
|
spp[i]->state = IPSEC_SPSTATE_DEAD;
|
|
|
|
TAILQ_REMOVE(&V_sptree_ifnet[spp[i]->spidx.dir],
|
|
|
|
spp[i], chain);
|
|
|
|
LIST_REMOVE(spp[i], idhash);
|
|
|
|
}
|
|
|
|
SPTREE_WUNLOCK();
|
2011-03-31 15:23:32 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
m = key_setdumpsp(spp[i], SADB_X_SPDDELETE, 0, 0);
|
|
|
|
if (m != NULL)
|
|
|
|
key_sendup_mbuf(NULL, m, KEY_SENDUP_ALL);
|
|
|
|
}
|
2011-03-31 15:23:32 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
|
|
|
* Must be called after calling key_allocsa().
|
|
|
|
* This function is called by key_freesp() to free some SA allocated
|
|
|
|
* for a policy.
|
|
|
|
*/
|
|
|
|
void
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesav(struct secasvar **psav)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secasvar *sav = *psav;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sav != NULL, ("null sav"));
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SAV_DELREF(sav) == 0)
|
|
|
|
return;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(IPSEC_STAMP,
|
|
|
|
printf("%s: last reference to SA(%p)\n", __func__, sav));
|
|
|
|
|
|
|
|
*psav = NULL;
|
|
|
|
key_delsav(sav);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlink SA from SAH and SPI hash under SAHTREE_WLOCK.
|
|
|
|
* Expect that SA has extra reference due to lookup.
|
|
|
|
* Release this references, also release SAH reference after unlink.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
key_unlinksav(struct secasvar *sav)
|
|
|
|
{
|
|
|
|
struct secashead *sah;
|
|
|
|
|
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SA(%p)\n", __func__, sav));
|
|
|
|
|
|
|
|
SAHTREE_UNLOCK_ASSERT();
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
if (sav->state == SADB_SASTATE_DEAD) {
|
|
|
|
/* SA is already unlinked */
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
return;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Unlink from SAH */
|
|
|
|
if (sav->state == SADB_SASTATE_LARVAL)
|
|
|
|
TAILQ_REMOVE(&sav->sah->savtree_larval, sav, chain);
|
|
|
|
else
|
|
|
|
TAILQ_REMOVE(&sav->sah->savtree_alive, sav, chain);
|
|
|
|
/* Unlink from SPI hash */
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
sah = sav->sah;
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
key_freesav(&sav);
|
|
|
|
/* Since we are unlinked, release reference to SAH */
|
|
|
|
key_freesah(&sah);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* %%% SPD management */
|
|
|
|
/*
|
|
|
|
* search SPD
|
|
|
|
* OUT: NULL : not found
|
|
|
|
* others : found, pointer to a SP.
|
|
|
|
*/
|
|
|
|
static struct secpolicy *
|
|
|
|
key_getsp(struct secpolicyindex *spidx)
|
|
|
|
{
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secpolicy *sp;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(spidx != NULL, ("null spidx"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK();
|
|
|
|
TAILQ_FOREACH(sp, &V_sptree[spidx->dir], chain) {
|
2002-10-16 02:10:08 +00:00
|
|
|
if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
|
|
|
|
SP_ADDREF(sp);
|
2003-09-01 05:35:55 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
return sp;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get SP by index.
|
|
|
|
* OUT: NULL : not found
|
2017-02-06 08:49:57 +00:00
|
|
|
* others : found, pointer to referenced SP.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
static struct secpolicy *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_getspbyid(uint32_t id)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secpolicy *sp;
|
|
|
|
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_FOREACH(sp, SPHASH_HASH(id), idhash) {
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sp->id == id) {
|
|
|
|
SP_ADDREF(sp);
|
2017-02-06 08:49:57 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RUNLOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
return (sp);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct secpolicy *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_newsp(void)
|
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
|
|
|
|
|
|
|
sp = malloc(sizeof(*sp), M_IPSEC_SP, M_NOWAIT | M_ZERO);
|
|
|
|
if (sp != NULL)
|
|
|
|
SP_INITREF(sp);
|
|
|
|
return (sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ipsecrequest *
|
|
|
|
ipsec_newisr(void)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
return (malloc(sizeof(struct ipsecrequest), M_IPSEC_SR,
|
|
|
|
M_NOWAIT | M_ZERO));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ipsec_delisr(struct ipsecrequest *p)
|
|
|
|
{
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
free(p, M_IPSEC_SR);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create secpolicy structure from sadb_x_policy structure.
|
2017-02-06 08:49:57 +00:00
|
|
|
* NOTE: `state', `secpolicyindex' and 'id' in secpolicy structure
|
|
|
|
* are not set, so must be set properly later.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
struct secpolicy *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_msg2sp(struct sadb_x_policy *xpl0, size_t len, int *error)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secpolicy *newsp;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
|
2004-04-07 00:19:02 +00:00
|
|
|
IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
|
2003-09-29 22:57:43 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
if (len != PFKEY_EXTLEN(xpl0)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if ((newsp = key_newsp()) == NULL) {
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = ENOBUFS;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newsp->spidx.dir = xpl0->sadb_x_policy_dir;
|
|
|
|
newsp->policy = xpl0->sadb_x_policy_type;
|
2015-11-17 14:39:33 +00:00
|
|
|
newsp->priority = xpl0->sadb_x_policy_priority;
|
2017-02-06 08:49:57 +00:00
|
|
|
newsp->tcount = 0;
|
2002-10-16 02:10:08 +00: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:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPSEC_POLICY_IPSEC:
|
|
|
|
{
|
|
|
|
struct sadb_x_ipsecrequest *xisr;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct ipsecrequest *isr;
|
|
|
|
int tlen;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* validity check */
|
|
|
|
if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
|
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
|
|
|
|
xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
|
|
|
|
|
|
|
|
while (tlen > 0) {
|
|
|
|
/* length check */
|
2017-08-09 19:58:38 +00:00
|
|
|
if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr) ||
|
|
|
|
xisr->sadb_x_ipsecrequest_len > tlen) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
|
|
|
|
"length.\n", __func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (newsp->tcount >= IPSEC_MAXREQ) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: too many ipsecrequests.\n",
|
|
|
|
__func__));
|
|
|
|
key_freesp(&newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/* allocate request buffer */
|
2003-09-01 05:35:55 +00:00
|
|
|
/* NB: data structure is zero'd */
|
2017-02-06 08:49:57 +00:00
|
|
|
isr = ipsec_newisr();
|
|
|
|
if (isr == NULL) {
|
2002-10-16 02:10:08 +00:00
|
|
|
ipseclog((LOG_DEBUG,
|
2003-09-29 22:57:43 +00:00
|
|
|
"%s: No more memory.\n", __func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = ENOBUFS;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
newsp->req[newsp->tcount++] = isr;
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/* set values */
|
|
|
|
switch (xisr->sadb_x_ipsecrequest_proto) {
|
|
|
|
case IPPROTO_ESP:
|
|
|
|
case IPPROTO_AH:
|
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ipseclog((LOG_DEBUG,
|
2003-09-29 22:57:43 +00:00
|
|
|
"%s: invalid proto type=%u\n", __func__,
|
2002-10-16 02:10:08 +00:00
|
|
|
xisr->sadb_x_ipsecrequest_proto));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EPROTONOSUPPORT;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
isr->saidx.proto =
|
|
|
|
(uint8_t)xisr->sadb_x_ipsecrequest_proto;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
switch (xisr->sadb_x_ipsecrequest_mode) {
|
|
|
|
case IPSEC_MODE_TRANSPORT:
|
|
|
|
case IPSEC_MODE_TUNNEL:
|
|
|
|
break;
|
|
|
|
case IPSEC_MODE_ANY:
|
|
|
|
default:
|
|
|
|
ipseclog((LOG_DEBUG,
|
2003-09-29 22:57:43 +00:00
|
|
|
"%s: invalid mode=%u\n", __func__,
|
2002-10-16 02:10:08 +00:00
|
|
|
xisr->sadb_x_ipsecrequest_mode));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
isr->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
|
2002-10-16 02:10:08 +00: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) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
2003-09-29 22:57:43 +00:00
|
|
|
"%s: reqid=%d range "
|
2002-10-16 02:10:08 +00:00
|
|
|
"violation, updated by kernel.\n",
|
2003-09-29 22:57:43 +00:00
|
|
|
__func__,
|
2002-10-16 02:10:08 +00:00
|
|
|
xisr->sadb_x_ipsecrequest_reqid));
|
|
|
|
xisr->sadb_x_ipsecrequest_reqid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate new reqid id if reqid is zero. */
|
|
|
|
if (xisr->sadb_x_ipsecrequest_reqid == 0) {
|
|
|
|
u_int32_t reqid;
|
|
|
|
if ((reqid = key_newreqid()) == 0) {
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = ENOBUFS;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
isr->saidx.reqid = reqid;
|
2002-10-16 02:10:08 +00:00
|
|
|
xisr->sadb_x_ipsecrequest_reqid = reqid;
|
|
|
|
} else {
|
|
|
|
/* set it for manual keying. */
|
2017-02-06 08:49:57 +00:00
|
|
|
isr->saidx.reqid =
|
|
|
|
xisr->sadb_x_ipsecrequest_reqid;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
|
|
|
|
__func__,
|
2002-10-16 02:10:08 +00:00
|
|
|
xisr->sadb_x_ipsecrequest_level));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
isr->level = xisr->sadb_x_ipsecrequest_level;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* set IP addresses if there */
|
|
|
|
if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
|
|
|
|
struct sockaddr *paddr;
|
|
|
|
|
2017-08-09 19:58:38 +00:00
|
|
|
len = tlen - sizeof(*xisr);
|
2002-10-16 02:10:08 +00:00
|
|
|
paddr = (struct sockaddr *)(xisr + 1);
|
|
|
|
/* validity check */
|
2017-08-09 19:58:38 +00:00
|
|
|
if (len < sizeof(struct sockaddr) ||
|
|
|
|
len < 2 * paddr->sa_len ||
|
|
|
|
paddr->sa_len > sizeof(isr->saidx.src)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid "
|
|
|
|
"request address length.\n",
|
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-09 19:58:38 +00:00
|
|
|
/*
|
|
|
|
* Request length should be enough to keep
|
|
|
|
* source and destination addresses.
|
|
|
|
*/
|
|
|
|
if (xisr->sadb_x_ipsecrequest_len <
|
|
|
|
sizeof(*xisr) + 2 * paddr->sa_len) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid "
|
|
|
|
"ipsecrequest length.\n",
|
|
|
|
__func__));
|
|
|
|
key_freesp(&newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
bcopy(paddr, &isr->saidx.src, paddr->sa_len);
|
|
|
|
paddr = (struct sockaddr *)((caddr_t)paddr +
|
|
|
|
paddr->sa_len);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* validity check */
|
2017-08-09 19:58:38 +00:00
|
|
|
if (paddr->sa_len !=
|
|
|
|
isr->saidx.src.sa.sa_len) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid "
|
|
|
|
"request address length.\n",
|
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* AF family should match */
|
|
|
|
if (paddr->sa_family !=
|
|
|
|
isr->saidx.src.sa.sa_family) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: address "
|
|
|
|
"family doesn't match.\n",
|
|
|
|
__func__));
|
|
|
|
key_freesp(&newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
bcopy(paddr, &isr->saidx.dst, paddr->sa_len);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Addresses for TUNNEL mode requests are
|
|
|
|
* mandatory.
|
|
|
|
*/
|
|
|
|
if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: missing "
|
|
|
|
"request addresses.\n", __func__));
|
|
|
|
key_freesp(&newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
tlen -= xisr->sadb_x_ipsecrequest_len;
|
|
|
|
|
|
|
|
/* validity check */
|
|
|
|
if (tlen < 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
|
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
|
|
|
|
+ xisr->sadb_x_ipsecrequest_len);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* XXXAE: LARVAL SP */
|
|
|
|
if (newsp->tcount < 1) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: valid IPSEC transforms "
|
|
|
|
"not found.\n", __func__));
|
|
|
|
key_freesp(&newsp);
|
|
|
|
*error = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
*error = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*error = 0;
|
2017-02-06 08:49:57 +00:00
|
|
|
return (newsp);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t
|
|
|
|
key_newreqid(void)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (auto_reqid == ~0)
|
|
|
|
auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
|
|
|
|
else
|
|
|
|
auto_reqid++;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* XXX should be unique check */
|
2017-02-06 08:49:57 +00:00
|
|
|
return (auto_reqid);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy secpolicy struct to sadb_x_policy structure indicated.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
static struct mbuf *
|
|
|
|
key_sp2mbuf(struct secpolicy *sp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
2017-02-06 08:49:57 +00:00
|
|
|
size_t tlen;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
tlen = key_getspreqmsglen(sp);
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(tlen, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
m_align(m, tlen);
|
2002-10-16 02:10:08 +00:00
|
|
|
m->m_len = tlen;
|
2017-02-06 08:49:57 +00:00
|
|
|
if (key_sp2msg(sp, m->m_data, &tlen) != 0) {
|
|
|
|
m_freem(m);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
key_sp2msg(struct secpolicy *sp, void *request, size_t *len)
|
|
|
|
{
|
|
|
|
struct sadb_x_ipsecrequest *xisr;
|
|
|
|
struct sadb_x_policy *xpl;
|
|
|
|
struct ipsecrequest *isr;
|
|
|
|
size_t xlen, ilen;
|
|
|
|
caddr_t p;
|
|
|
|
int error, i;
|
|
|
|
|
|
|
|
IPSEC_ASSERT(sp != NULL, ("null policy"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
xlen = sizeof(*xpl);
|
|
|
|
if (*len < xlen)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
bzero(request, *len);
|
|
|
|
xpl = (struct sadb_x_policy *)request;
|
2002-10-16 02:10:08 +00:00
|
|
|
xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
|
|
|
|
xpl->sadb_x_policy_type = sp->policy;
|
|
|
|
xpl->sadb_x_policy_dir = sp->spidx.dir;
|
|
|
|
xpl->sadb_x_policy_id = sp->id;
|
2015-11-17 14:39:33 +00:00
|
|
|
xpl->sadb_x_policy_priority = sp->priority;
|
2017-03-07 00:13:53 +00:00
|
|
|
switch (sp->state) {
|
|
|
|
case IPSEC_SPSTATE_IFNET:
|
|
|
|
xpl->sadb_x_policy_scope = IPSEC_POLICYSCOPE_IFNET;
|
|
|
|
break;
|
|
|
|
case IPSEC_SPSTATE_PCB:
|
|
|
|
xpl->sadb_x_policy_scope = IPSEC_POLICYSCOPE_PCB;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xpl->sadb_x_policy_scope = IPSEC_POLICYSCOPE_GLOBAL;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* if is the policy for ipsec ? */
|
|
|
|
if (sp->policy == IPSEC_POLICY_IPSEC) {
|
2017-02-06 08:49:57 +00:00
|
|
|
p = (caddr_t)xpl + sizeof(*xpl);
|
|
|
|
for (i = 0; i < sp->tcount; i++) {
|
|
|
|
isr = sp->req[i];
|
|
|
|
ilen = PFKEY_ALIGN8(sizeof(*xisr) +
|
|
|
|
isr->saidx.src.sa.sa_len +
|
|
|
|
isr->saidx.dst.sa.sa_len);
|
|
|
|
xlen += ilen;
|
|
|
|
if (xlen > *len) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
/* Calculate needed size */
|
|
|
|
continue;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
xisr = (struct sadb_x_ipsecrequest *)p;
|
2017-02-06 08:49:57 +00:00
|
|
|
xisr->sadb_x_ipsecrequest_len = ilen;
|
2002-10-16 02:10:08 +00: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.sa.sa_len);
|
|
|
|
p += isr->saidx.src.sa.sa_len;
|
|
|
|
bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
|
2017-02-06 08:49:57 +00:00
|
|
|
p += isr->saidx.dst.sa.sa_len;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
xpl->sadb_x_policy_len = PFKEY_UNIT64(xlen);
|
|
|
|
if (error == 0)
|
|
|
|
*len = xlen;
|
|
|
|
else
|
|
|
|
*len = sizeof(*xpl);
|
|
|
|
return (error);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* m will not be freed nor modified */
|
|
|
|
static struct mbuf *
|
|
|
|
key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
|
2014-12-03 03:50:54 +00:00
|
|
|
int ndeep, int nitem, ...)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int idx;
|
|
|
|
int i;
|
|
|
|
struct mbuf *result = NULL, *n;
|
|
|
|
int len;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
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));
|
2003-09-29 22:57:43 +00:00
|
|
|
|
|
|
|
IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
|
|
|
|
|
2012-12-05 08:04:20 +00:00
|
|
|
MGETHDR(n, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
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];
|
2013-03-15 10:20:15 +00:00
|
|
|
n = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (n == NULL)
|
2002-10-16 02:10:08 +00:00
|
|
|
goto fail;
|
2013-03-15 10:20:15 +00:00
|
|
|
m_align(n, len);
|
|
|
|
n->m_len = len;
|
2002-10-16 02:10:08 +00:00
|
|
|
m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
|
|
|
|
mtod(n, caddr_t));
|
|
|
|
} else {
|
|
|
|
n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
|
2012-12-05 08:04:20 +00:00
|
|
|
M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
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:
|
|
|
|
m_freem(result);
|
2011-10-07 21:00:26 +00:00
|
|
|
va_end(ap);
|
2002-10-16 02:10:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
|
2003-01-01 18:49:04 +00:00
|
|
|
* add an entry to SP database, when received
|
2002-10-16 02:10:08 +00:00
|
|
|
* <base, address(SD), (lifetime(H),) policy>
|
|
|
|
* from the user(?).
|
|
|
|
* Adding to SP database,
|
|
|
|
* and send
|
|
|
|
* <base, address(SD), (lifetime(H),) policy>
|
|
|
|
* to the socket which was send.
|
|
|
|
*
|
|
|
|
* SPDADD set a unique policy entry.
|
|
|
|
* SPDSETIDX like SPDADD without a part of policy requests.
|
|
|
|
* SPDUPDATE replace a unique policy entry.
|
|
|
|
*
|
2017-02-06 08:49:57 +00:00
|
|
|
* XXXAE: serialize this in PF_KEY to avoid races.
|
2002-10-16 02:10:08 +00:00
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_spdadd(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secpolicyindex spidx;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
struct sadb_x_policy *xpl0, *xpl;
|
|
|
|
struct sadb_lifetime *lft = NULL;
|
|
|
|
struct secpolicy *newsp;
|
|
|
|
int error;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_X_EXT_POLICY)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_X_EXT_POLICY)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD)) {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_LIFETIME_HARD)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* check the direciton */
|
2002-10-16 02:10:08 +00:00
|
|
|
switch (xpl0->sadb_x_policy_dir) {
|
|
|
|
case IPSEC_DIR_INBOUND:
|
|
|
|
case IPSEC_DIR_OUTBOUND:
|
|
|
|
break;
|
|
|
|
default:
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid SP direction.\n", __func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
/* key_spdadd() accepts DISCARD, NONE and IPSEC. */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (xpl0->sadb_x_policy_type != IPSEC_POLICY_DISCARD &&
|
|
|
|
xpl0->sadb_x_policy_type != IPSEC_POLICY_NONE &&
|
|
|
|
xpl0->sadb_x_policy_type != IPSEC_POLICY_IPSEC) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* policy requests are mandatory when action is ipsec. */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
|
|
|
|
mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: policy requests required.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
|
|
|
|
(struct sockaddr *)(dst0 + 1));
|
|
|
|
if (error != 0 ||
|
|
|
|
src0->sadb_address_proto != dst0->sadb_address_proto) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, error);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* make secindex */
|
2002-10-16 02:10:08 +00:00
|
|
|
KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
|
|
|
|
src0 + 1,
|
|
|
|
dst0 + 1,
|
|
|
|
src0->sadb_address_prefixlen,
|
|
|
|
dst0->sadb_address_prefixlen,
|
|
|
|
src0->sadb_address_proto,
|
2017-02-06 08:49:57 +00:00
|
|
|
&spidx);
|
|
|
|
/* Checking there is SP already or not. */
|
|
|
|
newsp = key_getsp(&spidx);
|
|
|
|
if (newsp != NULL) {
|
|
|
|
if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
|
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: unlink SP(%p) for SPDUPDATE\n",
|
|
|
|
__func__, newsp));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secpolicy(newsp));
|
|
|
|
key_unlink(newsp);
|
|
|
|
key_freesp(&newsp);
|
|
|
|
} else {
|
|
|
|
key_freesp(&newsp);
|
|
|
|
ipseclog((LOG_DEBUG, "%s: a SP entry exists already.",
|
|
|
|
__func__));
|
|
|
|
return (key_senderror(so, m, EEXIST));
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* allocate new SP entry */
|
|
|
|
if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
|
|
|
|
return key_senderror(so, m, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
newsp->lastused = newsp->created = time_second;
|
2002-10-16 02:10:08 +00:00
|
|
|
newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
|
|
|
|
newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
|
2017-02-06 08:49:57 +00:00
|
|
|
bcopy(&spidx, &newsp->spidx, sizeof(spidx));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* XXXAE: there is race between key_getsp() and key_insertsp() */
|
|
|
|
SPTREE_WLOCK();
|
|
|
|
if ((newsp->id = key_getnewspid()) == 0) {
|
|
|
|
SPTREE_WUNLOCK();
|
|
|
|
key_freesp(&newsp);
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
2015-11-17 14:39:33 +00:00
|
|
|
key_insertsp(newsp);
|
2017-02-06 08:49:57 +00:00
|
|
|
SPTREE_WUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SP(%p)\n", __func__, newsp));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secpolicy(newsp));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
struct mbuf *n, *mpolicy;
|
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
int off;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
|
|
|
|
if (n->m_len < sizeof(*newmsg)) {
|
|
|
|
n = m_pullup(n, sizeof(*newmsg));
|
|
|
|
if (!n)
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get new policy id.
|
|
|
|
* OUT:
|
|
|
|
* 0: failure.
|
|
|
|
* others: success.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint32_t
|
|
|
|
key_getnewspid(void)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t newid = 0;
|
|
|
|
int count = V_key_spi_trycnt; /* XXX */
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SPTREE_WLOCK_ASSERT();
|
2002-10-16 02:10:08 +00:00
|
|
|
while (count--) {
|
2017-02-06 08:49:57 +00:00
|
|
|
if (V_policy_id == ~0) /* overflowed */
|
|
|
|
newid = V_policy_id = 1;
|
|
|
|
else
|
|
|
|
newid = ++V_policy_id;
|
|
|
|
LIST_FOREACH(sp, SPHASH_HASH(newid), idhash) {
|
|
|
|
if (sp->id == newid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sp == NULL)
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (count == 0 || newid == 0) {
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: failed to allocate policy id.\n",
|
|
|
|
__func__));
|
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
return (newid);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_SPDDELETE processing
|
|
|
|
* receive
|
|
|
|
* <base, address(SD), policy(*)>
|
|
|
|
* from the user(?), and set SADB_SASTATE_DEAD,
|
|
|
|
* and send,
|
|
|
|
* <base, address(SD), policy(*)>
|
|
|
|
* to the ikmpd.
|
|
|
|
* policy(*) including direction of policy.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_spddelete(struct socket *so, struct mbuf *m,
|
|
|
|
const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secpolicyindex spidx;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
struct sadb_x_policy *xpl0;
|
|
|
|
struct secpolicy *sp;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null so"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_X_EXT_POLICY)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_X_EXT_POLICY)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* check the direciton */
|
|
|
|
switch (xpl0->sadb_x_policy_dir) {
|
|
|
|
case IPSEC_DIR_INBOUND:
|
|
|
|
case IPSEC_DIR_OUTBOUND:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid SP direction.\n", __func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
/* Only DISCARD, NONE and IPSEC are allowed */
|
|
|
|
if (xpl0->sadb_x_policy_type != IPSEC_POLICY_DISCARD &&
|
|
|
|
xpl0->sadb_x_policy_type != IPSEC_POLICY_NONE &&
|
|
|
|
xpl0->sadb_x_policy_type != IPSEC_POLICY_IPSEC) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
if (key_checksockaddrs((struct sockaddr *)(src0 + 1),
|
|
|
|
(struct sockaddr *)(dst0 + 1)) != 0 ||
|
|
|
|
src0->sadb_address_proto != dst0->sadb_address_proto) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
/* make secindex */
|
|
|
|
KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
|
|
|
|
src0 + 1,
|
|
|
|
dst0 + 1,
|
|
|
|
src0->sadb_address_prefixlen,
|
|
|
|
dst0->sadb_address_prefixlen,
|
|
|
|
src0->sadb_address_proto,
|
|
|
|
&spidx);
|
|
|
|
|
|
|
|
/* Is there SP in SPD ? */
|
|
|
|
if ((sp = key_getsp(&spidx)) == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save policy id to buffer to be returned. */
|
|
|
|
xpl0->sadb_x_policy_id = sp->id;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SP(%p)\n", __func__, sp));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secpolicy(sp));
|
2014-12-24 18:34:56 +00:00
|
|
|
key_unlink(sp);
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&sp);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_SPDDELETE2 processing
|
|
|
|
* receive
|
|
|
|
* <base, policy(*)>
|
|
|
|
* from the user(?), and set SADB_SASTATE_DEAD,
|
|
|
|
* and send,
|
|
|
|
* <base, policy(*)>
|
|
|
|
* to the ikmpd.
|
|
|
|
* policy(*) including direction of policy.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_spddelete2(struct socket *so, struct mbuf *m,
|
|
|
|
const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t id;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_X_EXT_POLICY) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_X_EXT_POLICY)) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
|
|
|
|
__func__));
|
2007-06-15 23:45:39 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
id = ((struct sadb_x_policy *)
|
|
|
|
mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* Is there SP in SPD ? */
|
|
|
|
if ((sp = key_getspbyid(id)) == NULL) {
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: no SP found for id %u.\n",
|
|
|
|
__func__, id));
|
2007-06-15 23:45:39 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SP(%p)\n", __func__, sp));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secpolicy(sp));
|
2014-12-24 18:34:56 +00:00
|
|
|
key_unlink(sp);
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sp->state != IPSEC_SPSTATE_DEAD) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: failed to delete SP with id %u.\n",
|
|
|
|
__func__, id));
|
|
|
|
key_freesp(&sp);
|
|
|
|
return (key_senderror(so, m, EACCES));
|
|
|
|
}
|
|
|
|
key_freesp(&sp);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
struct mbuf *n, *nn;
|
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
int off, len;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
|
|
|
|
2012-12-05 08:04:20 +00:00
|
|
|
MGETHDR(n, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (n && len > MHLEN) {
|
2015-01-06 12:59:37 +00:00
|
|
|
if (!(MCLGET(n, M_NOWAIT))) {
|
2002-10-16 02:10:08 +00:00
|
|
|
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));
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
|
|
|
|
off, len));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
|
2012-12-05 08:04:20 +00:00
|
|
|
mhp->extlen[SADB_X_EXT_POLICY], M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!n->m_next) {
|
|
|
|
m_freem(n);
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 *);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-05-20 11:59:53 +00:00
|
|
|
* SADB_X_SPDGET processing
|
2002-10-16 02:10:08 +00:00
|
|
|
* receive
|
|
|
|
* <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
|
2014-12-03 03:50:54 +00:00
|
|
|
key_spdget(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secpolicy *sp;
|
|
|
|
struct mbuf *n;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t id;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_X_EXT_POLICY) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_X_EXT_POLICY)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
id = ((struct sadb_x_policy *)
|
|
|
|
mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* Is there SP in SPD ? */
|
|
|
|
if ((sp = key_getspbyid(id)) == NULL) {
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: no SP found for id %u.\n",
|
|
|
|
__func__, id));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, ENOENT);
|
|
|
|
}
|
|
|
|
|
2015-05-20 11:59:53 +00:00
|
|
|
n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq,
|
|
|
|
mhp->msg->sadb_msg_pid);
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&sp);
|
2002-10-16 02:10:08 +00:00
|
|
|
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
|
2016-05-03 18:05:43 +00:00
|
|
|
* <base> with SADB_X_SPDACQUIRE if error occurred,
|
2002-10-16 02:10:08 +00:00
|
|
|
* or
|
|
|
|
* <base, policy>
|
|
|
|
* with SADB_X_SPDUPDATE from KMD by PF_KEY.
|
|
|
|
* policy(*) is without policy requests.
|
|
|
|
*
|
|
|
|
* 0 : succeed
|
|
|
|
* others: error number
|
|
|
|
*/
|
|
|
|
int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_spdacquire(struct secpolicy *sp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *result = NULL, *m;
|
|
|
|
struct secspacq *newspacq;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
|
|
|
|
IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
|
|
|
|
IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
|
|
|
|
("policy not IPSEC %u", sp->policy));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-01-01 18:49:04 +00:00
|
|
|
/* Get an entry to check whether sent message or not. */
|
2003-09-01 05:35:55 +00:00
|
|
|
newspacq = key_getspacq(&sp->spidx);
|
|
|
|
if (newspacq != NULL) {
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (V_key_blockacq_count < newspacq->count) {
|
2002-10-16 02:10:08 +00:00
|
|
|
/* reset counter and do send message. */
|
|
|
|
newspacq->count = 0;
|
|
|
|
} else {
|
|
|
|
/* increment counter and do nothing. */
|
|
|
|
newspacq->count++;
|
2014-12-07 06:47:00 +00:00
|
|
|
SPACQ_UNLOCK();
|
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2003-09-29 22:57:43 +00:00
|
|
|
SPACQ_UNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
} else {
|
|
|
|
/* make new entry for blocking to send SADB_ACQUIRE. */
|
2003-09-01 05:35:55 +00:00
|
|
|
newspacq = key_newspacq(&sp->spidx);
|
|
|
|
if (newspacq == NULL)
|
2002-10-16 02:10:08 +00:00
|
|
|
return ENOBUFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
|
2007-06-16 00:15:14 +00:00
|
|
|
if (!m)
|
|
|
|
return ENOBUFS;
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
result = m;
|
|
|
|
|
|
|
|
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, m, KEY_SENDUP_REGISTERED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2014-12-03 03:50:54 +00:00
|
|
|
key_spdflush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secpolicy_queue drainq;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct sadb_msg *newmsg;
|
2014-12-24 18:34:56 +00:00
|
|
|
struct secpolicy *sp, *nextsp;
|
2002-10-16 02:10:08 +00:00
|
|
|
u_int dir;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
|
2014-12-24 18:34:56 +00:00
|
|
|
TAILQ_INIT(&drainq);
|
|
|
|
SPTREE_WLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
2014-12-24 18:34:56 +00:00
|
|
|
TAILQ_CONCAT(&drainq, &V_sptree[dir], chain);
|
|
|
|
}
|
2015-02-24 10:35:07 +00:00
|
|
|
/*
|
|
|
|
* We need to set state to DEAD for each policy to be sure,
|
|
|
|
* that another thread won't try to unlink it.
|
2017-02-06 08:49:57 +00:00
|
|
|
* Also remove SP from sphash.
|
2015-02-24 10:35:07 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_FOREACH(sp, &drainq, chain) {
|
2015-02-24 10:35:07 +00:00
|
|
|
sp->state = IPSEC_SPSTATE_DEAD;
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_REMOVE(sp, idhash);
|
|
|
|
}
|
|
|
|
V_sp_genid++;
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_WUNLOCK();
|
|
|
|
sp = TAILQ_FIRST(&drainq);
|
|
|
|
while (sp != NULL) {
|
|
|
|
nextsp = TAILQ_NEXT(sp, chain);
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&sp);
|
2014-12-24 18:34:56 +00:00
|
|
|
sp = nextsp;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2002-10-16 02:10:08 +00: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);
|
|
|
|
}
|
|
|
|
|
2017-03-07 00:13:53 +00:00
|
|
|
static uint8_t
|
|
|
|
key_satype2scopemask(uint8_t satype)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (satype == IPSEC_POLICYSCOPE_ANY)
|
|
|
|
return (0xff);
|
|
|
|
return (satype);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
|
|
|
* SADB_SPDDUMP processing
|
|
|
|
* receive
|
|
|
|
* <base>
|
2017-03-07 00:13:53 +00:00
|
|
|
* from the user, and dump all SP leaves and send,
|
2002-10-16 02:10:08 +00:00
|
|
|
* <base> .....
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
2017-03-07 00:13:53 +00:00
|
|
|
* NOTE:
|
|
|
|
* sadb_msg_satype is considered as mask of policy scopes.
|
|
|
|
* m will always be freed.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_spddump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secpolicy *sp;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct mbuf *n;
|
2002-10-16 02:10:08 +00:00
|
|
|
int cnt;
|
2017-03-07 00:13:53 +00:00
|
|
|
u_int dir, scope;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* search SPD entry and get buffer size. */
|
|
|
|
cnt = 0;
|
2017-03-07 00:13:53 +00:00
|
|
|
scope = key_satype2scopemask(mhp->msg->sadb_msg_satype);
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
2017-03-07 00:13:53 +00:00
|
|
|
if (scope & IPSEC_POLICYSCOPE_GLOBAL) {
|
|
|
|
TAILQ_FOREACH(sp, &V_sptree[dir], chain)
|
|
|
|
cnt++;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-03-07 00:13:53 +00:00
|
|
|
if (scope & IPSEC_POLICYSCOPE_IFNET) {
|
|
|
|
TAILQ_FOREACH(sp, &V_sptree_ifnet[dir], chain)
|
|
|
|
cnt++;
|
2017-02-06 08:49:57 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-27 09:44:14 +00:00
|
|
|
if (cnt == 0) {
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, ENOENT);
|
2009-05-27 09:44:14 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
2017-03-07 00:13:53 +00:00
|
|
|
if (scope & IPSEC_POLICYSCOPE_GLOBAL) {
|
|
|
|
TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
|
|
|
|
--cnt;
|
|
|
|
n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
|
|
|
|
mhp->msg->sadb_msg_pid);
|
|
|
|
|
|
|
|
if (n != NULL)
|
|
|
|
key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-03-07 00:13:53 +00:00
|
|
|
if (scope & IPSEC_POLICYSCOPE_IFNET) {
|
|
|
|
TAILQ_FOREACH(sp, &V_sptree_ifnet[dir], chain) {
|
|
|
|
--cnt;
|
|
|
|
n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
|
|
|
|
mhp->msg->sadb_msg_pid);
|
|
|
|
|
|
|
|
if (n != NULL)
|
|
|
|
key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
m_freem(m);
|
2017-02-06 08:49:57 +00:00
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct mbuf *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq,
|
|
|
|
u_int32_t pid)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *result = NULL, *m;
|
2008-08-05 15:36:50 +00:00
|
|
|
struct seclifetime lt;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
result = m;
|
|
|
|
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
|
|
|
&sp->spidx.src.sa, sp->spidx.prefs,
|
|
|
|
sp->spidx.ul_proto);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
|
|
|
&sp->spidx.dst.sa, sp->spidx.prefd,
|
|
|
|
sp->spidx.ul_proto);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
m = key_sp2mbuf(sp);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
|
2008-08-05 15:36:50 +00:00
|
|
|
if(sp->lifetime){
|
|
|
|
lt.addtime=sp->created;
|
|
|
|
lt.usetime= sp->lastused;
|
|
|
|
m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
lt.addtime=sp->lifetime;
|
|
|
|
lt.usetime= sp->validtime;
|
|
|
|
m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
if ((result->m_flags & M_PKTHDR) == 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
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);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* get PFKEY message length for security policy and request.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
static size_t
|
2014-12-03 03:50:54 +00:00
|
|
|
key_getspreqmsglen(struct secpolicy *sp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
size_t tlen, len;
|
|
|
|
int i;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
tlen = sizeof(struct sadb_x_policy);
|
|
|
|
/* if is the policy for ipsec ? */
|
|
|
|
if (sp->policy != IPSEC_POLICY_IPSEC)
|
2017-02-06 08:49:57 +00:00
|
|
|
return (tlen);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* get length of ipsec requests */
|
2017-02-06 08:49:57 +00:00
|
|
|
for (i = 0; i < sp->tcount; i++) {
|
2002-10-16 02:10:08 +00:00
|
|
|
len = sizeof(struct sadb_x_ipsecrequest)
|
2017-02-06 08:49:57 +00:00
|
|
|
+ sp->req[i]->saidx.src.sa.sa_len
|
|
|
|
+ sp->req[i]->saidx.dst.sa.sa_len;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
tlen += PFKEY_ALIGN8(len);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
return (tlen);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_SPDEXPIRE processing
|
|
|
|
* send
|
|
|
|
* <base, address(SD), lifetime(CH), policy>
|
|
|
|
* to KMD by PF_KEY.
|
|
|
|
*
|
|
|
|
* OUT: 0 : succeed
|
|
|
|
* others : error number
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_spdexpire(struct secpolicy *sp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct sadb_lifetime *lt;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct mbuf *result = NULL, *m;
|
|
|
|
int len, error = -1;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SP(%p)\n", __func__, sp));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secpolicy(sp));
|
|
|
|
|
2002-10-16 02:10:08 +00: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;
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL) {
|
2002-10-16 02:10:08 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-03-15 10:20:15 +00:00
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
2002-10-16 02:10:08 +00:00
|
|
|
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 */
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
|
|
|
&sp->spidx.src.sa,
|
|
|
|
sp->spidx.prefs, sp->spidx.ul_proto);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
/* set sadb_address for destination */
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
|
|
|
&sp->spidx.dst.sa,
|
|
|
|
sp->spidx.prefd, sp->spidx.ul_proto);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
/* set secpolicy */
|
2017-02-06 08:49:57 +00:00
|
|
|
m = key_sp2mbuf(sp);
|
2002-10-16 02:10:08 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* %%% SAD management */
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* allocating and initialize new SA head.
|
2002-10-16 02:10:08 +00:00
|
|
|
* OUT: NULL : failure due to the lack of memory.
|
|
|
|
* others : pointer to new SA head.
|
|
|
|
*/
|
|
|
|
static struct secashead *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_newsah(struct secasindex *saidx)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secashead *sah;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sah = malloc(sizeof(struct secashead), M_IPSEC_SAH,
|
|
|
|
M_NOWAIT | M_ZERO);
|
|
|
|
if (sah == NULL) {
|
|
|
|
PFKEYSTAT_INC(in_nomem);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
TAILQ_INIT(&sah->savtree_larval);
|
|
|
|
TAILQ_INIT(&sah->savtree_alive);
|
|
|
|
sah->saidx = *saidx;
|
|
|
|
sah->state = SADB_SASTATE_DEAD;
|
|
|
|
SAH_INITREF(sah);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SAH(%p)\n", __func__, sah));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secash(sah, NULL));
|
|
|
|
return (sah);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static void
|
|
|
|
key_freesah(struct secashead **psah)
|
|
|
|
{
|
|
|
|
struct secashead *sah = *psah;
|
2003-09-01 05:35:55 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SAH_DELREF(sah) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: last reference to SAH(%p)\n", __func__, sah));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secash(sah, NULL));
|
|
|
|
|
|
|
|
*psah = NULL;
|
|
|
|
key_delsah(sah);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-12-03 03:50:54 +00:00
|
|
|
key_delsah(struct secashead *sah)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sah != NULL, ("NULL sah"));
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(sah->state == SADB_SASTATE_DEAD,
|
|
|
|
("Attempt to free non DEAD SAH %p", sah));
|
|
|
|
IPSEC_ASSERT(TAILQ_EMPTY(&sah->savtree_larval),
|
|
|
|
("Attempt to free SAH %p with LARVAL SA", sah));
|
|
|
|
IPSEC_ASSERT(TAILQ_EMPTY(&sah->savtree_alive),
|
|
|
|
("Attempt to free SAH %p with ALIVE SA", sah));
|
|
|
|
|
|
|
|
free(sah, M_IPSEC_SAH);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* allocating a new SA for key_add() and key_getspi() call,
|
2002-10-16 02:10:08 +00:00
|
|
|
* and copy the values of mhp into new buffer.
|
2017-02-06 08:49:57 +00:00
|
|
|
* When SAD message type is SADB_GETSPI set SA state to LARVAL.
|
|
|
|
* For SADB_ADD create and initialize SA with MATURE state.
|
2002-10-16 02:10:08 +00:00
|
|
|
* OUT: NULL : fail
|
|
|
|
* others : pointer to new secasvar.
|
|
|
|
*/
|
|
|
|
static struct secasvar *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_newsav(const struct sadb_msghdr *mhp, struct secasindex *saidx,
|
|
|
|
uint32_t spi, int *errp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
|
|
|
int isnew;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(mhp->msg->sadb_msg_type == SADB_GETSPI ||
|
|
|
|
mhp->msg->sadb_msg_type == SADB_ADD, ("wrong message type"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav = NULL;
|
|
|
|
sah = NULL;
|
|
|
|
/* check SPI value */
|
|
|
|
switch (saidx->proto) {
|
|
|
|
case IPPROTO_ESP:
|
|
|
|
case IPPROTO_AH:
|
|
|
|
/*
|
|
|
|
* RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
|
|
|
|
* 1-255 reserved by IANA for future use,
|
|
|
|
* 0 for implementation specific, local use.
|
|
|
|
*/
|
|
|
|
if (ntohl(spi) <= 255) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
|
|
|
|
__func__, ntohl(spi)));
|
2002-10-16 02:10:08 +00:00
|
|
|
*errp = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT | M_ZERO);
|
|
|
|
if (sav == NULL) {
|
|
|
|
*errp = ENOBUFS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
sav->lock = malloc(sizeof(struct mtx), M_IPSEC_MISC,
|
|
|
|
M_NOWAIT | M_ZERO);
|
|
|
|
if (sav->lock == NULL) {
|
|
|
|
*errp = ENOBUFS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
mtx_init(sav->lock, "ipsec association", NULL, MTX_DEF);
|
|
|
|
sav->lft_c = uma_zalloc(V_key_lft_zone, M_NOWAIT);
|
|
|
|
if (sav->lft_c == NULL) {
|
|
|
|
*errp = ENOBUFS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
counter_u64_zero(sav->lft_c_allocations);
|
|
|
|
counter_u64_zero(sav->lft_c_bytes);
|
2003-09-01 05:35:55 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav->spi = spi;
|
|
|
|
sav->seq = mhp->msg->sadb_msg_seq;
|
|
|
|
sav->state = SADB_SASTATE_LARVAL;
|
|
|
|
sav->pid = (pid_t)mhp->msg->sadb_msg_pid;
|
|
|
|
SAV_INITREF(sav);
|
|
|
|
again:
|
|
|
|
sah = key_getsah(saidx);
|
|
|
|
if (sah == NULL) {
|
|
|
|
/* create a new SA index */
|
|
|
|
sah = key_newsah(saidx);
|
|
|
|
if (sah == NULL) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: No more memory.\n", __func__));
|
|
|
|
*errp = ENOBUFS;
|
2002-10-16 02:10:08 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
isnew = 1;
|
|
|
|
} else
|
|
|
|
isnew = 0;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav->sah = sah;
|
|
|
|
if (mhp->msg->sadb_msg_type == SADB_GETSPI) {
|
|
|
|
sav->created = time_second;
|
|
|
|
} else if (sav->state == SADB_SASTATE_LARVAL) {
|
|
|
|
/*
|
|
|
|
* Do not call key_setsaval() second time in case
|
|
|
|
* of `goto again`. We will have MATURE state.
|
|
|
|
*/
|
|
|
|
*errp = key_setsaval(sav, mhp);
|
|
|
|
if (*errp != 0)
|
|
|
|
goto done;
|
|
|
|
sav->state = SADB_SASTATE_MATURE;
|
|
|
|
}
|
2003-09-01 05:35:55 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_WLOCK();
|
|
|
|
/*
|
|
|
|
* Check that existing SAH wasn't unlinked.
|
|
|
|
* Since we didn't hold the SAHTREE lock, it is possible,
|
|
|
|
* that callout handler or key_flush() or key_delete() could
|
|
|
|
* unlink this SAH.
|
|
|
|
*/
|
|
|
|
if (isnew == 0 && sah->state == SADB_SASTATE_DEAD) {
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
key_freesah(&sah); /* reference from key_getsah() */
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
if (isnew != 0) {
|
|
|
|
/*
|
|
|
|
* Add new SAH into SADB.
|
|
|
|
*
|
|
|
|
* XXXAE: we can serialize key_add and key_getspi calls, so
|
|
|
|
* several threads will not fight in the race.
|
|
|
|
* Otherwise we should check under SAHTREE lock, that this
|
|
|
|
* SAH would not added twice.
|
|
|
|
*/
|
|
|
|
TAILQ_INSERT_HEAD(&V_sahtree, sah, chain);
|
|
|
|
/* Add new SAH into hash by addresses */
|
|
|
|
LIST_INSERT_HEAD(SAHADDRHASH_HASH(saidx), sah, addrhash);
|
|
|
|
/* Now we are linked in the chain */
|
|
|
|
sah->state = SADB_SASTATE_MATURE;
|
|
|
|
/*
|
|
|
|
* SAV references this new SAH.
|
|
|
|
* In case of existing SAH we reuse reference
|
|
|
|
* from key_getsah().
|
|
|
|
*/
|
|
|
|
SAH_ADDREF(sah);
|
|
|
|
}
|
|
|
|
/* Link SAV with SAH */
|
|
|
|
if (sav->state == SADB_SASTATE_MATURE)
|
|
|
|
TAILQ_INSERT_HEAD(&sah->savtree_alive, sav, chain);
|
|
|
|
else
|
|
|
|
TAILQ_INSERT_HEAD(&sah->savtree_larval, sav, chain);
|
|
|
|
/* Add SAV into SPI hash */
|
|
|
|
LIST_INSERT_HEAD(SAVHASH_HASH(sav->spi), sav, spihash);
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
*errp = 0; /* success */
|
2002-10-16 02:10:08 +00:00
|
|
|
done:
|
2017-02-06 08:49:57 +00:00
|
|
|
if (*errp != 0) {
|
|
|
|
if (sav != NULL) {
|
|
|
|
if (sav->lock != NULL) {
|
|
|
|
mtx_destroy(sav->lock);
|
|
|
|
free(sav->lock, M_IPSEC_MISC);
|
|
|
|
}
|
|
|
|
if (sav->lft_c != NULL)
|
|
|
|
uma_zfree(V_key_lft_zone, sav->lft_c);
|
|
|
|
free(sav, M_IPSEC_SA), sav = NULL;
|
|
|
|
}
|
|
|
|
if (sah != NULL)
|
|
|
|
key_freesah(&sah);
|
|
|
|
if (*errp == ENOBUFS) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n",
|
|
|
|
__func__));
|
|
|
|
PFKEYSTAT_INC(in_nomem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* free() SA variable entry.
|
|
|
|
*/
|
|
|
|
static void
|
2003-09-01 05:35:55 +00:00
|
|
|
key_cleansav(struct secasvar *sav)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
if (sav->natt != NULL) {
|
|
|
|
free(sav->natt, M_IPSEC_MISC);
|
|
|
|
sav->natt = NULL;
|
|
|
|
}
|
|
|
|
if (sav->flags & SADB_X_EXT_F_CLONED)
|
|
|
|
return;
|
2003-06-29 23:58:38 +00:00
|
|
|
/*
|
|
|
|
* Cleanup xform state. Note that zeroize'ing causes the
|
|
|
|
* keys to be cleared; otherwise we must do it ourself.
|
|
|
|
*/
|
|
|
|
if (sav->tdb_xform != NULL) {
|
|
|
|
sav->tdb_xform->xf_zeroize(sav);
|
|
|
|
sav->tdb_xform = NULL;
|
|
|
|
} else {
|
|
|
|
if (sav->key_auth != NULL)
|
2006-03-25 13:38:52 +00:00
|
|
|
bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
|
2003-06-29 23:58:38 +00:00
|
|
|
if (sav->key_enc != NULL)
|
2006-03-25 13:38:52 +00:00
|
|
|
bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
|
2003-06-29 23:58:38 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sav->key_auth != NULL) {
|
2006-03-25 13:38:52 +00:00
|
|
|
if (sav->key_auth->key_data != NULL)
|
|
|
|
free(sav->key_auth->key_data, M_IPSEC_MISC);
|
2003-09-01 05:35:55 +00:00
|
|
|
free(sav->key_auth, M_IPSEC_MISC);
|
2002-10-16 02:10:08 +00:00
|
|
|
sav->key_auth = NULL;
|
|
|
|
}
|
|
|
|
if (sav->key_enc != NULL) {
|
2006-03-25 13:38:52 +00:00
|
|
|
if (sav->key_enc->key_data != NULL)
|
|
|
|
free(sav->key_enc->key_data, M_IPSEC_MISC);
|
2003-09-01 05:35:55 +00:00
|
|
|
free(sav->key_enc, M_IPSEC_MISC);
|
2002-10-16 02:10:08 +00:00
|
|
|
sav->key_enc = NULL;
|
|
|
|
}
|
|
|
|
if (sav->replay != NULL) {
|
2016-11-25 14:44:49 +00:00
|
|
|
if (sav->replay->bitmap != NULL)
|
|
|
|
free(sav->replay->bitmap, M_IPSEC_MISC);
|
2003-09-01 05:35:55 +00:00
|
|
|
free(sav->replay, M_IPSEC_MISC);
|
2002-10-16 02:10:08 +00:00
|
|
|
sav->replay = NULL;
|
|
|
|
}
|
|
|
|
if (sav->lft_h != NULL) {
|
2003-09-01 05:35:55 +00:00
|
|
|
free(sav->lft_h, M_IPSEC_MISC);
|
2002-10-16 02:10:08 +00:00
|
|
|
sav->lft_h = NULL;
|
|
|
|
}
|
|
|
|
if (sav->lft_s != NULL) {
|
2003-09-01 05:35:55 +00:00
|
|
|
free(sav->lft_s, M_IPSEC_MISC);
|
2002-10-16 02:10:08 +00:00
|
|
|
sav->lft_s = NULL;
|
|
|
|
}
|
2003-09-01 05:35:55 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
/*
|
|
|
|
* free() SA variable entry.
|
|
|
|
*/
|
|
|
|
static void
|
2014-12-03 03:50:54 +00:00
|
|
|
key_delsav(struct secasvar *sav)
|
2003-09-01 05:35:55 +00:00
|
|
|
{
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sav != NULL, ("null sav"));
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(sav->state == SADB_SASTATE_DEAD,
|
|
|
|
("attempt to free non DEAD SA %p", sav));
|
|
|
|
IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0",
|
|
|
|
sav->refcnt));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* SA must be unlinked from the chain and hashtbl.
|
|
|
|
* If SA was cloned, we leave all fields untouched,
|
|
|
|
* except NAT-T config.
|
|
|
|
*/
|
2003-09-01 05:35:55 +00:00
|
|
|
key_cleansav(sav);
|
2017-02-06 08:49:57 +00:00
|
|
|
if ((sav->flags & SADB_X_EXT_F_CLONED) == 0) {
|
|
|
|
mtx_destroy(sav->lock);
|
|
|
|
free(sav->lock, M_IPSEC_MISC);
|
|
|
|
uma_zfree(V_key_lft_zone, sav->lft_c);
|
|
|
|
}
|
2003-09-01 05:35:55 +00:00
|
|
|
free(sav, M_IPSEC_SA);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* search SAH.
|
2002-10-16 02:10:08 +00:00
|
|
|
* OUT:
|
|
|
|
* NULL : not found
|
2017-02-06 08:49:57 +00:00
|
|
|
* others : found, referenced pointer to a SAH.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
static struct secashead *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_getsah(struct secasindex *saidx)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secashead *sah;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
|
|
|
|
if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID) != 0) {
|
|
|
|
SAH_ADDREF(sah);
|
|
|
|
break;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
return (sah);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Check not to be duplicated SPI.
|
2002-10-16 02:10:08 +00:00
|
|
|
* OUT:
|
2017-02-06 08:49:57 +00:00
|
|
|
* 0 : not found
|
|
|
|
* 1 : found SA with given SPI.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
static int
|
|
|
|
key_checkspidup(uint32_t spi)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secasvar *sav;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Assume SPI is in network byte order */
|
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sav, SAVHASH_HASH(spi), spihash) {
|
|
|
|
if (sav->spi == spi)
|
2003-09-01 05:35:55 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
return (sav != NULL);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Search SA by SPI.
|
2002-10-16 02:10:08 +00:00
|
|
|
* OUT:
|
|
|
|
* NULL : not found
|
2017-02-06 08:49:57 +00:00
|
|
|
* others : found, referenced pointer to a SA.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
static struct secasvar *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_getsavbyspi(uint32_t spi)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secasvar *sav;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Assume SPI is in network byte order */
|
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sav, SAVHASH_HASH(spi), spihash) {
|
|
|
|
if (sav->spi != spi)
|
|
|
|
continue;
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
return (sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-02-06 08:49:57 +00:00
|
|
|
key_updatelifetimes(struct secasvar *sav, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct seclifetime *lft_h, *lft_s, *tmp;
|
|
|
|
|
|
|
|
/* Lifetime extension is optional, check that it is present. */
|
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD) &&
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT)) {
|
|
|
|
/*
|
|
|
|
* In case of SADB_UPDATE we may need to change
|
|
|
|
* existing lifetimes.
|
|
|
|
*/
|
|
|
|
if (sav->state == SADB_SASTATE_MATURE) {
|
|
|
|
lft_h = lft_s = NULL;
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* Both HARD and SOFT extensions must present */
|
|
|
|
if ((SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD) &&
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT)) ||
|
|
|
|
(SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT) &&
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD))) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_LIFETIME_HARD) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_LIFETIME_SOFT)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
lft_h = key_dup_lifemsg((const struct sadb_lifetime *)
|
|
|
|
mhp->ext[SADB_EXT_LIFETIME_HARD], M_IPSEC_MISC);
|
|
|
|
if (lft_h == NULL) {
|
|
|
|
PFKEYSTAT_INC(in_nomem);
|
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
|
|
|
return (ENOBUFS);
|
|
|
|
}
|
|
|
|
lft_s = key_dup_lifemsg((const struct sadb_lifetime *)
|
|
|
|
mhp->ext[SADB_EXT_LIFETIME_SOFT], M_IPSEC_MISC);
|
|
|
|
if (lft_s == NULL) {
|
|
|
|
PFKEYSTAT_INC(in_nomem);
|
|
|
|
free(lft_h, M_IPSEC_MISC);
|
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
|
|
|
return (ENOBUFS);
|
|
|
|
}
|
|
|
|
reset:
|
|
|
|
if (sav->state != SADB_SASTATE_LARVAL) {
|
|
|
|
/*
|
|
|
|
* key_update() holds reference to this SA,
|
|
|
|
* so it won't be deleted in meanwhile.
|
|
|
|
*/
|
|
|
|
SECASVAR_LOCK(sav);
|
|
|
|
tmp = sav->lft_h;
|
|
|
|
sav->lft_h = lft_h;
|
|
|
|
lft_h = tmp;
|
|
|
|
|
|
|
|
tmp = sav->lft_s;
|
|
|
|
sav->lft_s = lft_s;
|
|
|
|
lft_s = tmp;
|
|
|
|
SECASVAR_UNLOCK(sav);
|
|
|
|
if (lft_h != NULL)
|
|
|
|
free(lft_h, M_IPSEC_MISC);
|
|
|
|
if (lft_s != NULL)
|
|
|
|
free(lft_s, M_IPSEC_MISC);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* We can update lifetime without holding a lock */
|
|
|
|
IPSEC_ASSERT(sav->lft_h == NULL, ("lft_h is already initialized\n"));
|
|
|
|
IPSEC_ASSERT(sav->lft_s == NULL, ("lft_s is already initialized\n"));
|
|
|
|
sav->lft_h = lft_h;
|
|
|
|
sav->lft_s = lft_s;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy SA values from PF_KEY message except *SPI, SEQ, PID and TYPE*.
|
|
|
|
* You must update these if need. Expects only LARVAL SAs.
|
|
|
|
* OUT: 0: success.
|
|
|
|
* !0: failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_setsaval(struct secasvar *sav, const struct sadb_msghdr *mhp)
|
|
|
|
{
|
|
|
|
const struct sadb_sa *sa0;
|
|
|
|
const struct sadb_key *key0;
|
|
|
|
uint32_t replay;
|
|
|
|
size_t len;
|
|
|
|
int error;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(sav->state == SADB_SASTATE_LARVAL,
|
|
|
|
("Attempt to update non LARVAL SA"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* XXX rewrite */
|
|
|
|
error = key_setident(sav->sah, mhp);
|
|
|
|
if (error != 0)
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* SA */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_EXT_SA)) {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_SA)) {
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
|
2002-10-16 02:10:08 +00:00
|
|
|
sav->alg_auth = sa0->sadb_sa_auth;
|
|
|
|
sav->alg_enc = sa0->sadb_sa_encrypt;
|
|
|
|
sav->flags = sa0->sadb_sa_flags;
|
2017-02-06 08:49:57 +00:00
|
|
|
if ((sav->flags & SADB_KEY_FLAGS_MAX) != sav->flags) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid sa_flags 0x%08x.\n", __func__,
|
|
|
|
sav->flags));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2016-11-25 14:44:49 +00:00
|
|
|
/* Optional replay window */
|
|
|
|
replay = 0;
|
|
|
|
if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0)
|
|
|
|
replay = sa0->sadb_sa_replay;
|
2017-02-06 08:49:57 +00:00
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_X_EXT_SA_REPLAY)) {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA_REPLAY)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-11-25 14:44:49 +00:00
|
|
|
replay = ((const struct sadb_x_sa_replay *)
|
2017-02-06 08:49:57 +00:00
|
|
|
mhp->ext[SADB_X_EXT_SA_REPLAY])->sadb_x_sa_replay_replay;
|
2016-11-25 14:44:49 +00:00
|
|
|
|
|
|
|
if (replay > UINT32_MAX - 32) {
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: replay window too big.\n", __func__));
|
2016-11-25 14:44:49 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
replay = (replay + 7) >> 3;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav->replay = malloc(sizeof(struct secreplay), M_IPSEC_MISC,
|
|
|
|
M_NOWAIT | M_ZERO);
|
2016-11-25 14:44:49 +00:00
|
|
|
if (sav->replay == NULL) {
|
2017-02-06 08:49:57 +00:00
|
|
|
PFKEYSTAT_INC(in_nomem);
|
2016-11-25 14:44:49 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
2016-11-25 14:44:49 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (replay != 0) {
|
|
|
|
/* number of 32b blocks to be allocated */
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t bitmap_size;
|
2016-11-25 14:44:49 +00:00
|
|
|
|
|
|
|
/* RFC 6479:
|
2017-02-06 08:49:57 +00:00
|
|
|
* - the allocated replay window size must be
|
|
|
|
* a power of two.
|
|
|
|
* - use an extra 32b block as a redundant window.
|
2016-11-25 14:44:49 +00:00
|
|
|
*/
|
|
|
|
bitmap_size = 1;
|
|
|
|
while (replay + 4 > bitmap_size)
|
|
|
|
bitmap_size <<= 1;
|
|
|
|
bitmap_size = bitmap_size / 4;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav->replay->bitmap = malloc(
|
|
|
|
bitmap_size * sizeof(uint32_t), M_IPSEC_MISC,
|
|
|
|
M_NOWAIT | M_ZERO);
|
2016-11-25 14:44:49 +00:00
|
|
|
if (sav->replay->bitmap == NULL) {
|
2017-02-06 08:49:57 +00:00
|
|
|
PFKEYSTAT_INC(in_nomem);
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-11-25 14:44:49 +00:00
|
|
|
sav->replay->bitmap_size = bitmap_size;
|
|
|
|
sav->replay->wsize = replay;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Authentication keys */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_EXT_KEY_AUTH)) {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_KEY_AUTH)) {
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
error = 0;
|
|
|
|
key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
|
|
|
|
len = mhp->extlen[SADB_EXT_KEY_AUTH];
|
2002-10-16 02:10:08 +00:00
|
|
|
switch (mhp->msg->sadb_msg_satype) {
|
|
|
|
case SADB_SATYPE_AH:
|
|
|
|
case SADB_SATYPE_ESP:
|
Initial import of RFC 2385 (TCP-MD5) digest support.
This is the first of two commits; bringing in the kernel support first.
This can be enabled by compiling a kernel with options TCP_SIGNATURE
and FAST_IPSEC.
For the uninitiated, this is a TCP option which provides for a means of
authenticating TCP sessions which came into being before IPSEC. It is
still relevant today, however, as it is used by many commercial router
vendors, particularly with BGP, and as such has become a requirement for
interconnect at many major Internet points of presence.
Several parts of the TCP and IP headers, including the segment payload,
are digested with MD5, including a shared secret. The PF_KEY interface
is used to manage the secrets using security associations in the SADB.
There is a limitation here in that as there is no way to map a TCP flow
per-port back to an SPI without polluting tcpcb or using the SPD; the
code to do the latter is unstable at this time. Therefore this code only
supports per-host keying granularity.
Whilst FAST_IPSEC is mutually exclusive with KAME IPSEC (and thus IPv6),
TCP_SIGNATURE applies only to IPv4. For the vast majority of prospective
users of this feature, this will not pose any problem.
This implementation is output-only; that is, the option is honoured when
responding to a host initiating a TCP session, but no effort is made
[yet] to authenticate inbound traffic. This is, however, sufficient to
interwork with Cisco equipment.
Tested with a Cisco 2501 running IOS 12.0(27), and Quagga 0.96.4 with
local patches. Patches for tcpdump to validate TCP-MD5 sessions are also
available from me upon request.
Sponsored by: sentex.net
2004-02-11 04:26:04 +00:00
|
|
|
case SADB_X_SATYPE_TCPSIGNATURE:
|
2002-10-16 02:10:08 +00:00
|
|
|
if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
|
|
|
|
sav->alg_auth != SADB_X_AALG_NULL)
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav->key_auth = key_dup_keymsg(key0, len, M_IPSEC_MISC);
|
2006-03-25 13:38:52 +00:00
|
|
|
if (sav->key_auth == NULL ) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n",
|
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
PFKEYSTAT_INC(in_nomem);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encryption key */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_EXT_KEY_ENCRYPT)) {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_KEY_ENCRYPT)) {
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
error = 0;
|
|
|
|
key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
|
|
|
|
len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
|
2002-10-16 02:10:08 +00:00
|
|
|
switch (mhp->msg->sadb_msg_satype) {
|
|
|
|
case SADB_SATYPE_ESP:
|
|
|
|
if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
|
|
|
|
sav->alg_enc != SADB_EALG_NULL) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
sav->key_enc = key_dup_keymsg(key0, len, M_IPSEC_MISC);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sav->key_enc == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n",
|
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
PFKEYSTAT_INC(in_nomem);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
|
|
|
if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
|
|
|
|
error = EINVAL;
|
|
|
|
sav->key_enc = NULL; /*just in case*/
|
|
|
|
break;
|
|
|
|
case SADB_SATYPE_AH:
|
Initial import of RFC 2385 (TCP-MD5) digest support.
This is the first of two commits; bringing in the kernel support first.
This can be enabled by compiling a kernel with options TCP_SIGNATURE
and FAST_IPSEC.
For the uninitiated, this is a TCP option which provides for a means of
authenticating TCP sessions which came into being before IPSEC. It is
still relevant today, however, as it is used by many commercial router
vendors, particularly with BGP, and as such has become a requirement for
interconnect at many major Internet points of presence.
Several parts of the TCP and IP headers, including the segment payload,
are digested with MD5, including a shared secret. The PF_KEY interface
is used to manage the secrets using security associations in the SADB.
There is a limitation here in that as there is no way to map a TCP flow
per-port back to an SPI without polluting tcpcb or using the SPD; the
code to do the latter is unstable at this time. Therefore this code only
supports per-host keying granularity.
Whilst FAST_IPSEC is mutually exclusive with KAME IPSEC (and thus IPv6),
TCP_SIGNATURE applies only to IPv4. For the vast majority of prospective
users of this feature, this will not pose any problem.
This implementation is output-only; that is, the option is honoured when
responding to a host initiating a TCP session, but no effort is made
[yet] to authenticate inbound traffic. This is, however, sufficient to
interwork with Cisco equipment.
Tested with a Cisco 2501 running IOS 12.0(27), and Quagga 0.96.4 with
local patches. Patches for tcpdump to validate TCP-MD5 sessions are also
available from me upon request.
Sponsored by: sentex.net
2004-02-11 04:26:04 +00:00
|
|
|
case SADB_X_SATYPE_TCPSIGNATURE:
|
2002-10-16 02:10:08 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set iv */
|
|
|
|
sav->ivlen = 0;
|
|
|
|
switch (mhp->msg->sadb_msg_satype) {
|
|
|
|
case SADB_SATYPE_AH:
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->flags & SADB_X_EXT_DERIV) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
|
|
|
|
"given to AH SA.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->alg_enc != SADB_EALG_NONE) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
|
|
|
|
"mismated.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
error = xform_init(sav, XF_AH);
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
2017-02-06 08:49:57 +00:00
|
|
|
case SADB_SATYPE_ESP:
|
|
|
|
if ((sav->flags & (SADB_X_EXT_OLD | SADB_X_EXT_DERIV)) ==
|
|
|
|
(SADB_X_EXT_OLD | SADB_X_EXT_DERIV)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
|
2017-02-06 08:49:57 +00:00
|
|
|
"given to old-esp.\n", __func__));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
error = xform_init(sav, XF_ESP);
|
|
|
|
break;
|
2017-02-06 08:49:57 +00:00
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sav->alg_auth != SADB_AALG_NONE) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
|
2017-02-06 08:49:57 +00:00
|
|
|
"mismated.\n", __func__));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if ((sav->flags & SADB_X_EXT_RAWCPI) == 0 &&
|
|
|
|
ntohl(sav->spi) >= 0x10000) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
error = xform_init(sav, XF_IPCOMP);
|
|
|
|
break;
|
2017-02-06 08:49:57 +00:00
|
|
|
case SADB_X_SATYPE_TCPSIGNATURE:
|
Initial import of RFC 2385 (TCP-MD5) digest support.
This is the first of two commits; bringing in the kernel support first.
This can be enabled by compiling a kernel with options TCP_SIGNATURE
and FAST_IPSEC.
For the uninitiated, this is a TCP option which provides for a means of
authenticating TCP sessions which came into being before IPSEC. It is
still relevant today, however, as it is used by many commercial router
vendors, particularly with BGP, and as such has become a requirement for
interconnect at many major Internet points of presence.
Several parts of the TCP and IP headers, including the segment payload,
are digested with MD5, including a shared secret. The PF_KEY interface
is used to manage the secrets using security associations in the SADB.
There is a limitation here in that as there is no way to map a TCP flow
per-port back to an SPI without polluting tcpcb or using the SPD; the
code to do the latter is unstable at this time. Therefore this code only
supports per-host keying granularity.
Whilst FAST_IPSEC is mutually exclusive with KAME IPSEC (and thus IPv6),
TCP_SIGNATURE applies only to IPv4. For the vast majority of prospective
users of this feature, this will not pose any problem.
This implementation is output-only; that is, the option is honoured when
responding to a host initiating a TCP session, but no effort is made
[yet] to authenticate inbound traffic. This is, however, sufficient to
interwork with Cisco equipment.
Tested with a Cisco 2501 running IOS 12.0(27), and Quagga 0.96.4 with
local patches. Patches for tcpdump to validate TCP-MD5 sessions are also
available from me upon request.
Sponsored by: sentex.net
2004-02-11 04:26:04 +00:00
|
|
|
if (sav->alg_enc != SADB_EALG_NONE) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
|
2017-02-06 08:49:57 +00:00
|
|
|
"mismated.\n", __func__));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
Initial import of RFC 2385 (TCP-MD5) digest support.
This is the first of two commits; bringing in the kernel support first.
This can be enabled by compiling a kernel with options TCP_SIGNATURE
and FAST_IPSEC.
For the uninitiated, this is a TCP option which provides for a means of
authenticating TCP sessions which came into being before IPSEC. It is
still relevant today, however, as it is used by many commercial router
vendors, particularly with BGP, and as such has become a requirement for
interconnect at many major Internet points of presence.
Several parts of the TCP and IP headers, including the segment payload,
are digested with MD5, including a shared secret. The PF_KEY interface
is used to manage the secrets using security associations in the SADB.
There is a limitation here in that as there is no way to map a TCP flow
per-port back to an SPI without polluting tcpcb or using the SPD; the
code to do the latter is unstable at this time. Therefore this code only
supports per-host keying granularity.
Whilst FAST_IPSEC is mutually exclusive with KAME IPSEC (and thus IPv6),
TCP_SIGNATURE applies only to IPv4. For the vast majority of prospective
users of this feature, this will not pose any problem.
This implementation is output-only; that is, the option is honoured when
responding to a host initiating a TCP session, but no effort is made
[yet] to authenticate inbound traffic. This is, however, sufficient to
interwork with Cisco equipment.
Tested with a Cisco 2501 running IOS 12.0(27), and Quagga 0.96.4 with
local patches. Patches for tcpdump to validate TCP-MD5 sessions are also
available from me upon request.
Sponsored by: sentex.net
2004-02-11 04:26:04 +00:00
|
|
|
}
|
|
|
|
error = xform_init(sav, XF_TCPSIGNATURE);
|
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
default:
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EPROTONOSUPPORT;
|
2017-02-06 08:49:57 +00:00
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (error) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
|
|
|
|
__func__, mhp->msg->sadb_msg_satype));
|
|
|
|
goto fail;
|
2003-09-01 05:35:55 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
/* Handle NAT-T headers */
|
|
|
|
error = key_setnatt(sav, mhp);
|
|
|
|
if (error != 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Initialize lifetime for CURRENT */
|
|
|
|
sav->firstused = 0;
|
|
|
|
sav->created = time_second;
|
|
|
|
|
|
|
|
/* lifetimes for HARD and SOFT */
|
|
|
|
error = key_updatelifetimes(sav, mhp);
|
|
|
|
if (error == 0)
|
|
|
|
return (0);
|
|
|
|
fail:
|
|
|
|
key_cleansav(sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subroutine for SADB_GET and SADB_DUMP.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_setdumpsa(struct secasvar *sav, uint8_t type, uint8_t satype,
|
|
|
|
uint32_t seq, uint32_t pid)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct seclifetime lft_c;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct mbuf *result = NULL, *tres = NULL, *m;
|
2017-02-06 08:49:57 +00:00
|
|
|
int i, dumporder[] = {
|
2016-11-25 14:44:49 +00:00
|
|
|
SADB_EXT_SA, SADB_X_EXT_SA2, SADB_X_EXT_SA_REPLAY,
|
2002-10-16 02:10:08 +00:00
|
|
|
SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
|
|
|
|
SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
|
2017-02-06 08:49:57 +00:00
|
|
|
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,
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
SADB_X_EXT_NAT_T_TYPE,
|
|
|
|
SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
|
|
|
|
SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
|
|
|
|
SADB_X_EXT_NAT_T_FRAG,
|
2002-10-16 02:10:08 +00:00
|
|
|
};
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t replay_count;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
|
|
|
|
if (m == NULL)
|
|
|
|
goto fail;
|
|
|
|
result = m;
|
|
|
|
|
2016-04-19 23:48:27 +00:00
|
|
|
for (i = nitems(dumporder) - 1; i >= 0; i--) {
|
2002-10-16 02:10:08 +00:00
|
|
|
m = NULL;
|
|
|
|
switch (dumporder[i]) {
|
|
|
|
case SADB_EXT_SA:
|
|
|
|
m = key_setsadbsa(sav);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_X_EXT_SA2:
|
2016-11-25 14:44:49 +00:00
|
|
|
SECASVAR_LOCK(sav);
|
|
|
|
replay_count = sav->replay ? sav->replay->count : 0;
|
|
|
|
SECASVAR_UNLOCK(sav);
|
|
|
|
m = key_setsadbxsa2(sav->sah->saidx.mode, replay_count,
|
2002-10-16 02:10:08 +00:00
|
|
|
sav->sah->saidx.reqid);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
|
2016-11-25 14:44:49 +00:00
|
|
|
case SADB_X_EXT_SA_REPLAY:
|
|
|
|
if (sav->replay == NULL ||
|
2017-02-06 08:49:57 +00:00
|
|
|
sav->replay->wsize <= UINT8_MAX)
|
2016-11-25 14:44:49 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
m = key_setsadbxsareplay(sav->replay->wsize);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
case SADB_EXT_ADDRESS_SRC:
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
|
|
|
&sav->sah->saidx.src.sa,
|
|
|
|
FULLMASK, IPSEC_ULPROTO_ANY);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_ADDRESS_DST:
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
|
|
|
&sav->sah->saidx.dst.sa,
|
|
|
|
FULLMASK, IPSEC_ULPROTO_ANY);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_KEY_AUTH:
|
|
|
|
if (!sav->key_auth)
|
|
|
|
continue;
|
2006-03-25 13:38:52 +00:00
|
|
|
m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_KEY_ENCRYPT:
|
|
|
|
if (!sav->key_enc)
|
|
|
|
continue;
|
2006-03-25 13:38:52 +00:00
|
|
|
m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_LIFETIME_CURRENT:
|
2017-02-06 08:49:57 +00:00
|
|
|
lft_c.addtime = sav->created;
|
|
|
|
lft_c.allocations = (uint32_t)counter_u64_fetch(
|
|
|
|
sav->lft_c_allocations);
|
|
|
|
lft_c.bytes = counter_u64_fetch(sav->lft_c_bytes);
|
|
|
|
lft_c.usetime = sav->firstused;
|
|
|
|
m = key_setlifetime(&lft_c, SADB_EXT_LIFETIME_CURRENT);
|
2006-03-25 13:38:52 +00:00
|
|
|
if (!m)
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_LIFETIME_HARD:
|
|
|
|
if (!sav->lft_h)
|
|
|
|
continue;
|
2006-03-25 13:38:52 +00:00
|
|
|
m = key_setlifetime(sav->lft_h,
|
|
|
|
SADB_EXT_LIFETIME_HARD);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_EXT_LIFETIME_SOFT:
|
|
|
|
if (!sav->lft_s)
|
|
|
|
continue;
|
2008-03-24 15:01:20 +00:00
|
|
|
m = key_setlifetime(sav->lft_s,
|
2006-03-25 13:38:52 +00:00
|
|
|
SADB_EXT_LIFETIME_SOFT);
|
|
|
|
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
case SADB_X_EXT_NAT_T_TYPE:
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->natt == NULL)
|
|
|
|
continue;
|
|
|
|
m = key_setsadbxtype(UDP_ENCAP_ESPINUDP);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
2017-02-06 08:49:57 +00:00
|
|
|
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
case SADB_X_EXT_NAT_T_DPORT:
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->natt == NULL)
|
|
|
|
continue;
|
|
|
|
m = key_setsadbxport(sav->natt->dport,
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
SADB_X_EXT_NAT_T_DPORT);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_X_EXT_NAT_T_SPORT:
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->natt == NULL)
|
|
|
|
continue;
|
|
|
|
m = key_setsadbxport(sav->natt->sport,
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
SADB_X_EXT_NAT_T_SPORT);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_X_EXT_NAT_T_OAI:
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->natt == NULL ||
|
|
|
|
(sav->natt->flags & IPSEC_NATT_F_OAI) == 0)
|
|
|
|
continue;
|
|
|
|
m = key_setsadbaddr(SADB_X_EXT_NAT_T_OAI,
|
|
|
|
&sav->natt->oai.sa, FULLMASK, IPSEC_ULPROTO_ANY);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
case SADB_X_EXT_NAT_T_OAR:
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->natt == NULL ||
|
|
|
|
(sav->natt->flags & IPSEC_NATT_F_OAR) == 0)
|
|
|
|
continue;
|
|
|
|
m = key_setsadbaddr(SADB_X_EXT_NAT_T_OAR,
|
|
|
|
&sav->natt->oar.sa, FULLMASK, IPSEC_ULPROTO_ANY);
|
|
|
|
if (!m)
|
|
|
|
goto fail;
|
|
|
|
break;
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
case SADB_X_EXT_NAT_T_FRAG:
|
|
|
|
/* We do not (yet) support those. */
|
|
|
|
continue;
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
case SADB_EXT_ADDRESS_PROXY:
|
|
|
|
case SADB_EXT_IDENTITY_SRC:
|
|
|
|
case SADB_EXT_IDENTITY_DST:
|
|
|
|
/* XXX: should we brought from SPD ? */
|
|
|
|
case SADB_EXT_SENSITIVITY:
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-03-25 13:38:52 +00:00
|
|
|
if (!m)
|
2002-10-16 02:10:08 +00:00
|
|
|
goto fail;
|
|
|
|
if (tres)
|
|
|
|
m_cat(m, tres);
|
|
|
|
tres = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cat(result, tres);
|
2016-03-02 05:04:04 +00:00
|
|
|
tres = NULL;
|
2002-10-16 02:10:08 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set data into sadb_msg.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2009-02-24 18:09:31 +00:00
|
|
|
key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
|
|
|
|
pid_t pid, u_int16_t reserved)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_msg *p;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
|
|
|
if (len > MCLBYTES)
|
|
|
|
return NULL;
|
2012-12-05 08:04:20 +00:00
|
|
|
MGETHDR(m, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m && len > MHLEN) {
|
2015-01-06 12:59:37 +00:00
|
|
|
if (!(MCLGET(m, M_NOWAIT))) {
|
2002-10-16 02:10:08 +00:00
|
|
|
m_freem(m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
m->m_pkthdr.len = m->m_len = len;
|
|
|
|
m->m_next = NULL;
|
|
|
|
|
|
|
|
p = mtod(m, struct sadb_msg *);
|
|
|
|
|
|
|
|
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);
|
|
|
|
p->sadb_msg_reserved = reserved;
|
|
|
|
p->sadb_msg_seq = seq;
|
|
|
|
p->sadb_msg_pid = (u_int32_t)pid;
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy secasvar data into sadb_address.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_setsadbsa(struct secasvar *sav)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_sa *p;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
2002-10-16 02:10:08 +00:00
|
|
|
p = mtod(m, struct sadb_sa *);
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_sa_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_sa_exttype = SADB_EXT_SA;
|
|
|
|
p->sadb_sa_spi = sav->spi;
|
2016-11-25 14:44:49 +00:00
|
|
|
p->sadb_sa_replay = sav->replay ?
|
2017-02-06 08:49:57 +00:00
|
|
|
(sav->replay->wsize > UINT8_MAX ? UINT8_MAX :
|
|
|
|
sav->replay->wsize): 0;
|
2002-10-16 02:10:08 +00:00
|
|
|
p->sadb_sa_state = sav->state;
|
|
|
|
p->sadb_sa_auth = sav->alg_auth;
|
|
|
|
p->sadb_sa_encrypt = sav->alg_enc;
|
2017-02-06 08:49:57 +00:00
|
|
|
p->sadb_sa_flags = sav->flags & SADB_KEY_FLAGS_MAX;
|
|
|
|
return (m);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set data into sadb_address.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr,
|
|
|
|
u_int8_t prefixlen, u_int16_t ul_proto)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_address *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
|
|
|
|
PFKEY_ALIGN8(saddr->sa_len);
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
2002-10-16 02:10:08 +00:00
|
|
|
p = mtod(m, struct sadb_address *);
|
|
|
|
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_address_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_address_exttype = exttype;
|
|
|
|
p->sadb_address_proto = ul_proto;
|
|
|
|
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*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p->sadb_address_prefixlen = prefixlen;
|
|
|
|
p->sadb_address_reserved = 0;
|
|
|
|
|
|
|
|
bcopy(saddr,
|
|
|
|
mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
|
|
|
|
saddr->sa_len);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set data into sadb_x_sa2.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2009-02-24 18:09:31 +00:00
|
|
|
key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_x_sa2 *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
2002-10-16 02:10:08 +00:00
|
|
|
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;
|
|
|
|
p->sadb_x_sa2_sequence = seq;
|
|
|
|
p->sadb_x_sa2_reqid = reqid;
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2016-11-25 14:44:49 +00:00
|
|
|
/*
|
|
|
|
* Set data into sadb_x_sa_replay.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbxsareplay(u_int32_t replay)
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_x_sa_replay *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa_replay));
|
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
|
|
|
p = mtod(m, struct sadb_x_sa_replay *);
|
|
|
|
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_x_sa_replay_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_x_sa_replay_exttype = SADB_X_EXT_SA_REPLAY;
|
|
|
|
p->sadb_x_sa_replay_replay = (replay << 3);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
/*
|
|
|
|
* Set a type in sadb_x_nat_t_type.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbxtype(u_int16_t type)
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
size_t len;
|
|
|
|
struct sadb_x_nat_t_type *p;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
|
|
|
|
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL)
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
return (NULL);
|
2013-03-15 10:20:15 +00:00
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
p = mtod(m, struct sadb_x_nat_t_type *);
|
|
|
|
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
|
|
|
|
p->sadb_x_nat_t_type_type = type;
|
|
|
|
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Set a port in sadb_x_nat_t_port.
|
|
|
|
* In contrast to default RFC 2367 behaviour, port is in network byte order.
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_setsadbxport(u_int16_t port, u_int16_t type)
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
size_t len;
|
|
|
|
struct sadb_x_nat_t_port *p;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
|
|
|
|
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL)
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
return (NULL);
|
2013-03-15 10:20:15 +00:00
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
p = mtod(m, struct sadb_x_nat_t_port *);
|
|
|
|
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_x_nat_t_port_exttype = type;
|
|
|
|
p->sadb_x_nat_t_port_port = port;
|
|
|
|
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
* Get port from sockaddr. Port is in network byte order.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
uint16_t
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
key_portfromsaddr(struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
return ((struct sockaddr_in *)sa)->sin_port;
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
return ((struct sockaddr_in6 *)sa)->sin6_port;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set port in struct sockaddr. Port is in network byte order.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
void
|
|
|
|
key_porttosaddr(struct sockaddr *sa, uint16_t port)
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
((struct sockaddr_in *)sa)->sin_port = port;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
((struct sockaddr_in6 *)sa)->sin6_port = port;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
|
|
|
|
__func__, sa->sa_family));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
|
|
|
* set data into sadb_x_policy
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2015-11-17 14:39:33 +00:00
|
|
|
key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id, u_int32_t priority)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_x_policy *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
2002-10-16 02:10:08 +00:00
|
|
|
p = mtod(m, struct sadb_x_policy *);
|
|
|
|
|
|
|
|
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;
|
2015-11-17 14:39:33 +00:00
|
|
|
p->sadb_x_policy_priority = priority;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* %%% utilities */
|
2006-03-25 13:38:52 +00:00
|
|
|
/* Take a key message (sadb_key) from the socket and turn it into one
|
|
|
|
* of the kernel's key structures (seckey).
|
|
|
|
*
|
|
|
|
* IN: pointer to the src
|
|
|
|
* OUT: NULL no more memory
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2006-03-25 13:38:52 +00:00
|
|
|
struct seckey *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_dup_keymsg(const struct sadb_key *src, size_t len,
|
2014-12-03 03:50:54 +00:00
|
|
|
struct malloc_type *type)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2006-03-25 13:38:52 +00:00
|
|
|
struct seckey *dst;
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
dst = malloc(sizeof(*dst), type, M_NOWAIT);
|
2006-03-25 13:38:52 +00:00
|
|
|
if (dst != NULL) {
|
|
|
|
dst->bits = src->sadb_key_bits;
|
2017-02-06 08:49:57 +00:00
|
|
|
dst->key_data = malloc(len, type, M_NOWAIT);
|
2006-03-25 13:38:52 +00:00
|
|
|
if (dst->key_data != NULL) {
|
2017-02-06 08:49:57 +00:00
|
|
|
bcopy((const char *)(src + 1), dst->key_data, len);
|
2006-03-25 13:38:52 +00:00
|
|
|
} else {
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n",
|
|
|
|
__func__));
|
2006-03-25 13:38:52 +00:00
|
|
|
free(dst, type);
|
|
|
|
dst = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2006-03-25 13:38:52 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
return (dst);
|
2006-03-25 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Take a lifetime message (sadb_lifetime) passed in on a socket and
|
|
|
|
* turn it into one of the kernel's lifetime structures (seclifetime).
|
|
|
|
*
|
|
|
|
* IN: pointer to the destination, source and malloc type
|
|
|
|
* OUT: NULL, no more memory
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct seclifetime *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_dup_lifemsg(const struct sadb_lifetime *src, struct malloc_type *type)
|
2006-03-25 13:38:52 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct seclifetime *dst;
|
2006-03-25 13:38:52 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
dst = malloc(sizeof(*dst), type, M_NOWAIT);
|
2006-03-25 13:38:52 +00:00
|
|
|
if (dst == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
return (NULL);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
dst->allocations = src->sadb_lifetime_allocations;
|
|
|
|
dst->bytes = src->sadb_lifetime_bytes;
|
|
|
|
dst->addtime = src->sadb_lifetime_addtime;
|
|
|
|
dst->usetime = src->sadb_lifetime_usetime;
|
|
|
|
return (dst);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compare two secasindex structure.
|
|
|
|
* flag can specify to compare 2 saidxes.
|
|
|
|
* compare two secasindex structure without both mode and reqid.
|
|
|
|
* don't compare port.
|
|
|
|
* IN:
|
|
|
|
* saidx0: source, it can be in SAD.
|
|
|
|
* saidx1: object.
|
|
|
|
* OUT:
|
|
|
|
* 1 : equal
|
|
|
|
* 0 : not equal
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_cmpsaidx(const struct secasindex *saidx0, const struct secasindex *saidx1,
|
|
|
|
int flag)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/* sanity */
|
|
|
|
if (saidx0 == NULL && saidx1 == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (saidx0 == NULL || saidx1 == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (saidx0->proto != saidx1->proto)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (flag == CMP_EXACTLY) {
|
|
|
|
if (saidx0->mode != saidx1->mode)
|
|
|
|
return 0;
|
|
|
|
if (saidx0->reqid != saidx1->reqid)
|
|
|
|
return 0;
|
2017-02-06 08:49:57 +00:00
|
|
|
if (bcmp(&saidx0->src, &saidx1->src,
|
|
|
|
saidx0->src.sa.sa_len) != 0 ||
|
|
|
|
bcmp(&saidx0->dst, &saidx1->dst,
|
|
|
|
saidx0->dst.sa.sa_len) != 0)
|
2002-10-16 02:10:08 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (flag == CMP_MODE_REQID || flag == CMP_REQID) {
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
|
|
|
* If reqid of SPD is non-zero, unique SA is required.
|
|
|
|
* The result must be of same reqid in this case.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
if (saidx1->reqid != 0 &&
|
|
|
|
saidx0->reqid != saidx1->reqid)
|
2002-10-16 02:10:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag == CMP_MODE_REQID) {
|
|
|
|
if (saidx0->mode != IPSEC_MODE_ANY
|
|
|
|
&& saidx0->mode != saidx1->mode)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0)
|
2002-10-16 02:10:08 +00:00
|
|
|
return 0;
|
2017-02-06 08:49:57 +00:00
|
|
|
if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0)
|
2002-10-16 02:10:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_cmpspidx_exactly(struct secpolicyindex *spidx0,
|
|
|
|
struct secpolicyindex *spidx1)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
/* sanity */
|
|
|
|
if (spidx0 == NULL && spidx1 == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (spidx0 == NULL || spidx1 == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (spidx0->prefs != spidx1->prefs
|
|
|
|
|| spidx0->prefd != spidx1->prefd
|
|
|
|
|| spidx0->ul_proto != spidx1->ul_proto)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
|
|
|
|
key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compare two secindex structure with mask.
|
|
|
|
* IN:
|
|
|
|
* spidx0: source, it is often in SPD.
|
|
|
|
* spidx1: object, it is often from IP header.
|
|
|
|
* OUT:
|
|
|
|
* 1 : equal
|
|
|
|
* 0 : not equal
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_cmpspidx_withmask(struct secpolicyindex *spidx0,
|
|
|
|
struct secpolicyindex *spidx1)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
/* sanity */
|
|
|
|
if (spidx0 == NULL && spidx1 == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (spidx0 == NULL || spidx1 == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
|
|
|
|
spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
|
|
|
|
spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
|
|
|
|
spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
|
|
|
|
if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
|
|
|
|
&& spidx0->ul_proto != spidx1->ul_proto)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (spidx0->src.sa.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
|
|
|
|
&& spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
|
|
|
|
return 0;
|
|
|
|
if (!key_bbcmp(&spidx0->src.sin.sin_addr,
|
|
|
|
&spidx1->src.sin.sin_addr, spidx0->prefs))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
|
|
|
|
&& spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* scope_id check. if sin6_scope_id is 0, we regard it
|
|
|
|
* as a wildcard scope, which matches any scope zone ID.
|
|
|
|
*/
|
|
|
|
if (spidx0->src.sin6.sin6_scope_id &&
|
|
|
|
spidx1->src.sin6.sin6_scope_id &&
|
|
|
|
spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
|
|
|
|
return 0;
|
|
|
|
if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
|
|
|
|
&spidx1->src.sin6.sin6_addr, spidx0->prefs))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* XXX */
|
|
|
|
if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (spidx0->dst.sa.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
|
|
|
|
&& spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
|
|
|
|
return 0;
|
|
|
|
if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
|
|
|
|
&spidx1->dst.sin.sin_addr, spidx0->prefd))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
|
|
|
|
&& spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* scope_id check. if sin6_scope_id is 0, we regard it
|
|
|
|
* as a wildcard scope, which matches any scope zone ID.
|
|
|
|
*/
|
2004-06-22 17:17:44 +00:00
|
|
|
if (spidx0->dst.sin6.sin6_scope_id &&
|
|
|
|
spidx1->dst.sin6.sin6_scope_id &&
|
2002-10-16 02:10:08 +00:00
|
|
|
spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
|
|
|
|
return 0;
|
|
|
|
if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
|
|
|
|
&spidx1->dst.sin6.sin6_addr, spidx0->prefd))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* XXX */
|
|
|
|
if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX Do we check other field ? e.g. flowinfo */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef satosin
|
|
|
|
#undef satosin
|
|
|
|
#endif
|
|
|
|
#define satosin(s) ((const struct sockaddr_in *)s)
|
|
|
|
#ifdef satosin6
|
|
|
|
#undef satosin6
|
|
|
|
#endif
|
|
|
|
#define satosin6(s) ((const struct sockaddr_in6 *)s)
|
2017-02-06 08:49:57 +00:00
|
|
|
/* returns 0 on match */
|
|
|
|
int
|
|
|
|
key_sockaddrcmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
|
|
|
|
int port)
|
|
|
|
{
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
switch (sa1->sa_family) {
|
2017-02-06 08:49:57 +00:00
|
|
|
#ifdef INET
|
2002-10-16 02:10:08 +00:00
|
|
|
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;
|
2017-02-06 08:49:57 +00:00
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
2002-10-16 02:10:08 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-05-29 22:18:44 +00:00
|
|
|
break;
|
2017-02-06 08:49:57 +00:00
|
|
|
#endif
|
2002-10-16 02:10:08 +00:00
|
|
|
default:
|
|
|
|
if (bcmp(sa1, sa2, sa1->sa_len) != 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* returns 0 on match */
|
|
|
|
int
|
|
|
|
key_sockaddrcmp_withmask(const struct sockaddr *sa1,
|
|
|
|
const struct sockaddr *sa2, size_t mask)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
switch (sa1->sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
return (!key_bbcmp(&satosin(sa1)->sin_addr,
|
|
|
|
&satosin(sa2)->sin_addr, mask));
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
if (satosin6(sa1)->sin6_scope_id !=
|
|
|
|
satosin6(sa2)->sin6_scope_id)
|
|
|
|
return (1);
|
|
|
|
return (!key_bbcmp(&satosin6(sa1)->sin6_addr,
|
|
|
|
&satosin6(sa2)->sin6_addr, mask));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#undef satosin
|
|
|
|
#undef satosin6
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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(const void *a1, const void *a2, u_int bits)
|
|
|
|
{
|
|
|
|
const unsigned char *p1 = a1;
|
2002-10-16 02:10:08 +00:00
|
|
|
const unsigned char *p2 = a2;
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
u_int8_t mask = ~((1<<(8-bits))-1);
|
|
|
|
if ((*p1 & mask) != (*p2 & mask))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1; /* Match! */
|
|
|
|
}
|
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
static void
|
|
|
|
key_flush_spd(time_t now)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2014-12-24 18:34:56 +00:00
|
|
|
SPTREE_RLOCK_TRACKER;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secpolicy_list drainq;
|
|
|
|
struct secpolicy *sp, *nextsp;
|
2002-10-16 02:10:08 +00:00
|
|
|
u_int dir;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_INIT(&drainq);
|
|
|
|
SPTREE_RLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
|
2014-12-24 18:34:56 +00:00
|
|
|
TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sp->lifetime == 0 && sp->validtime == 0)
|
|
|
|
continue;
|
2014-12-24 18:34:56 +00:00
|
|
|
if ((sp->lifetime &&
|
|
|
|
now - sp->created > sp->lifetime) ||
|
|
|
|
(sp->validtime &&
|
|
|
|
now - sp->lastused > sp->validtime)) {
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Hold extra reference to send SPDEXPIRE */
|
2015-02-24 10:35:07 +00:00
|
|
|
SP_ADDREF(sp);
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_INSERT_HEAD(&drainq, sp, drainq);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
}
|
|
|
|
SPTREE_RUNLOCK();
|
|
|
|
if (LIST_EMPTY(&drainq))
|
|
|
|
return;
|
|
|
|
|
|
|
|
SPTREE_WLOCK();
|
|
|
|
sp = LIST_FIRST(&drainq);
|
|
|
|
while (sp != NULL) {
|
|
|
|
nextsp = LIST_NEXT(sp, drainq);
|
|
|
|
/* Check that SP is still linked */
|
|
|
|
if (sp->state != IPSEC_SPSTATE_ALIVE) {
|
|
|
|
LIST_REMOVE(sp, drainq);
|
|
|
|
key_freesp(&sp); /* release extra reference */
|
|
|
|
sp = nextsp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&V_sptree[sp->spidx.dir], sp, chain);
|
|
|
|
LIST_REMOVE(sp, idhash);
|
|
|
|
sp->state = IPSEC_SPSTATE_DEAD;
|
|
|
|
sp = nextsp;
|
|
|
|
}
|
|
|
|
V_sp_genid++;
|
|
|
|
SPTREE_WUNLOCK();
|
|
|
|
|
|
|
|
sp = LIST_FIRST(&drainq);
|
|
|
|
while (sp != NULL) {
|
|
|
|
nextsp = LIST_NEXT(sp, drainq);
|
|
|
|
key_spdexpire(sp);
|
|
|
|
key_freesp(&sp); /* release extra reference */
|
|
|
|
key_freesp(&sp); /* release last reference */
|
|
|
|
sp = nextsp;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2003-09-01 05:35:55 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
static void
|
|
|
|
key_flush_sad(time_t now)
|
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
|
|
|
struct secashead_list emptyq;
|
|
|
|
struct secasvar_list drainq, hexpireq, sexpireq, freeq;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secashead *sah, *nextsah;
|
|
|
|
struct secasvar *sav, *nextsav;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_INIT(&drainq);
|
|
|
|
LIST_INIT(&hexpireq);
|
|
|
|
LIST_INIT(&sexpireq);
|
|
|
|
LIST_INIT(&emptyq);
|
|
|
|
|
|
|
|
SAHTREE_RLOCK();
|
|
|
|
TAILQ_FOREACH(sah, &V_sahtree, chain) {
|
|
|
|
/* Check for empty SAH */
|
|
|
|
if (TAILQ_EMPTY(&sah->savtree_larval) &&
|
|
|
|
TAILQ_EMPTY(&sah->savtree_alive)) {
|
|
|
|
SAH_ADDREF(sah);
|
|
|
|
LIST_INSERT_HEAD(&emptyq, sah, drainq);
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Add all stale LARVAL SAs into drainq */
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
|
|
|
|
if (now - sav->created < V_key_larval_lifetime)
|
|
|
|
continue;
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
LIST_INSERT_HEAD(&drainq, sav, drainq);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
|
|
|
|
/* lifetimes aren't specified */
|
|
|
|
if (sav->lft_h == NULL)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
2017-02-06 08:49:57 +00:00
|
|
|
SECASVAR_LOCK(sav);
|
|
|
|
/*
|
|
|
|
* Check again with lock held, because it may
|
|
|
|
* be updated by SADB_UPDATE.
|
|
|
|
*/
|
|
|
|
if (sav->lft_h == NULL) {
|
|
|
|
SECASVAR_UNLOCK(sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
2015-05-19 08:30:04 +00:00
|
|
|
* RFC 2367:
|
|
|
|
* HARD lifetimes MUST take precedence over SOFT
|
|
|
|
* lifetimes, meaning if the HARD and SOFT lifetimes
|
|
|
|
* are the same, the HARD lifetime will appear on the
|
|
|
|
* EXPIRE message.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2015-05-19 08:30:04 +00:00
|
|
|
/* check HARD lifetime */
|
|
|
|
if ((sav->lft_h->addtime != 0 &&
|
|
|
|
now - sav->created > sav->lft_h->addtime) ||
|
2017-02-06 08:49:57 +00:00
|
|
|
(sav->lft_h->usetime != 0 && sav->firstused &&
|
|
|
|
now - sav->firstused > sav->lft_h->usetime) ||
|
|
|
|
(sav->lft_h->bytes != 0 && counter_u64_fetch(
|
|
|
|
sav->lft_c_bytes) > sav->lft_h->bytes)) {
|
|
|
|
SECASVAR_UNLOCK(sav);
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
LIST_INSERT_HEAD(&hexpireq, sav, drainq);
|
|
|
|
continue;
|
2015-05-19 08:30:04 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* check SOFT lifetime (only for MATURE SAs) */
|
|
|
|
if (sav->state == SADB_SASTATE_MATURE && (
|
|
|
|
(sav->lft_s->addtime != 0 &&
|
2015-05-19 08:30:04 +00:00
|
|
|
now - sav->created > sav->lft_s->addtime) ||
|
2017-02-06 08:49:57 +00:00
|
|
|
(sav->lft_s->usetime != 0 && sav->firstused &&
|
|
|
|
now - sav->firstused > sav->lft_s->usetime) ||
|
|
|
|
(sav->lft_s->bytes != 0 && counter_u64_fetch(
|
|
|
|
sav->lft_c_bytes) > sav->lft_s->bytes))) {
|
|
|
|
SECASVAR_UNLOCK(sav);
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
LIST_INSERT_HEAD(&sexpireq, sav, drainq);
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SECASVAR_UNLOCK(sav);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SAHTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (LIST_EMPTY(&emptyq) && LIST_EMPTY(&drainq) &&
|
|
|
|
LIST_EMPTY(&hexpireq) && LIST_EMPTY(&sexpireq))
|
|
|
|
return;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_INIT(&freeq);
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
/* Unlink stale LARVAL SAs */
|
|
|
|
sav = LIST_FIRST(&drainq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = LIST_NEXT(sav, drainq);
|
|
|
|
/* Check that SA is still LARVAL */
|
|
|
|
if (sav->state != SADB_SASTATE_LARVAL) {
|
|
|
|
LIST_REMOVE(sav, drainq);
|
|
|
|
LIST_INSERT_HEAD(&freeq, sav, drainq);
|
|
|
|
sav = nextsav;
|
|
|
|
continue;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_REMOVE(&sav->sah->savtree_larval, sav, chain);
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
/* Unlink all SAs with expired HARD lifetime */
|
|
|
|
sav = LIST_FIRST(&hexpireq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = LIST_NEXT(sav, drainq);
|
|
|
|
/* Check that SA is not unlinked */
|
|
|
|
if (sav->state == SADB_SASTATE_DEAD) {
|
|
|
|
LIST_REMOVE(sav, drainq);
|
|
|
|
LIST_INSERT_HEAD(&freeq, sav, drainq);
|
|
|
|
sav = nextsav;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&sav->sah->savtree_alive, sav, chain);
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
/* Mark all SAs with expired SOFT lifetime as DYING */
|
|
|
|
sav = LIST_FIRST(&sexpireq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = LIST_NEXT(sav, drainq);
|
|
|
|
/* Check that SA is not unlinked */
|
|
|
|
if (sav->state == SADB_SASTATE_DEAD) {
|
|
|
|
LIST_REMOVE(sav, drainq);
|
|
|
|
LIST_INSERT_HEAD(&freeq, sav, drainq);
|
|
|
|
sav = nextsav;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* NOTE: this doesn't change SA order in the chain.
|
|
|
|
*/
|
|
|
|
sav->state = SADB_SASTATE_DYING;
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
/* Unlink empty SAHs */
|
|
|
|
sah = LIST_FIRST(&emptyq);
|
|
|
|
while (sah != NULL) {
|
|
|
|
nextsah = LIST_NEXT(sah, drainq);
|
|
|
|
/* Check that SAH is still empty and not unlinked */
|
|
|
|
if (sah->state == SADB_SASTATE_DEAD ||
|
|
|
|
!TAILQ_EMPTY(&sah->savtree_larval) ||
|
|
|
|
!TAILQ_EMPTY(&sah->savtree_alive)) {
|
|
|
|
LIST_REMOVE(sah, drainq);
|
|
|
|
key_freesah(&sah); /* release extra reference */
|
|
|
|
sah = nextsah;
|
|
|
|
continue;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_REMOVE(&V_sahtree, sah, chain);
|
|
|
|
LIST_REMOVE(sah, addrhash);
|
|
|
|
sah->state = SADB_SASTATE_DEAD;
|
|
|
|
sah = nextsah;
|
|
|
|
}
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
|
|
|
|
/* Send SPDEXPIRE messages */
|
|
|
|
sav = LIST_FIRST(&hexpireq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = LIST_NEXT(sav, drainq);
|
|
|
|
key_expire(sav, 1);
|
|
|
|
key_freesah(&sav->sah); /* release reference from SAV */
|
|
|
|
key_freesav(&sav); /* release extra reference */
|
|
|
|
key_freesav(&sav); /* release last reference */
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
sav = LIST_FIRST(&sexpireq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = LIST_NEXT(sav, drainq);
|
|
|
|
key_expire(sav, 0);
|
|
|
|
key_freesav(&sav); /* release extra reference */
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
/* Free stale LARVAL SAs */
|
|
|
|
sav = LIST_FIRST(&drainq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = LIST_NEXT(sav, drainq);
|
|
|
|
key_freesah(&sav->sah); /* release reference from SAV */
|
|
|
|
key_freesav(&sav); /* release extra reference */
|
|
|
|
key_freesav(&sav); /* release last reference */
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
/* Free SAs that were unlinked/changed by someone else */
|
|
|
|
sav = LIST_FIRST(&freeq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = LIST_NEXT(sav, drainq);
|
|
|
|
key_freesav(&sav); /* release extra reference */
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
/* Free empty SAH */
|
|
|
|
sah = LIST_FIRST(&emptyq);
|
|
|
|
while (sah != NULL) {
|
|
|
|
nextsah = LIST_NEXT(sah, drainq);
|
|
|
|
key_freesah(&sah); /* release extra reference */
|
|
|
|
key_freesah(&sah); /* release last reference */
|
|
|
|
sah = nextsah;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2003-09-01 05:35:55 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
static void
|
|
|
|
key_flush_acq(time_t now)
|
|
|
|
{
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secacq *acq, *nextacq;
|
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
/* ACQ tree */
|
2003-09-29 22:57:43 +00:00
|
|
|
ACQ_LOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
acq = LIST_FIRST(&V_acqtree);
|
|
|
|
while (acq != NULL) {
|
2002-10-16 02:10:08 +00:00
|
|
|
nextacq = LIST_NEXT(acq, chain);
|
2017-02-06 08:49:57 +00:00
|
|
|
if (now - acq->created > V_key_blockacq_lifetime) {
|
2002-10-16 02:10:08 +00:00
|
|
|
LIST_REMOVE(acq, chain);
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_REMOVE(acq, addrhash);
|
|
|
|
LIST_REMOVE(acq, seqhash);
|
2003-09-01 05:35:55 +00:00
|
|
|
free(acq, M_IPSEC_SAQ);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
acq = nextacq;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2003-09-29 22:57:43 +00:00
|
|
|
ACQ_UNLOCK();
|
2003-09-01 05:35:55 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
static void
|
|
|
|
key_flush_spacq(time_t now)
|
|
|
|
{
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secspacq *acq, *nextacq;
|
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
/* SP ACQ tree */
|
2003-09-29 22:57:43 +00:00
|
|
|
SPACQ_LOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
|
2002-10-16 02:10:08 +00:00
|
|
|
nextacq = LIST_NEXT(acq, chain);
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (now - acq->created > V_key_blockacq_lifetime
|
2002-10-16 02:10:08 +00:00
|
|
|
&& __LIST_CHAINED(acq)) {
|
|
|
|
LIST_REMOVE(acq, chain);
|
2003-09-01 05:35:55 +00:00
|
|
|
free(acq, M_IPSEC_SAQ);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2003-09-29 22:57:43 +00:00
|
|
|
SPACQ_UNLOCK();
|
2003-09-01 05:35:55 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
/*
|
|
|
|
* time handler.
|
|
|
|
* scanning SPD and SAD to check status for each entries,
|
|
|
|
* and do to remove or to expire.
|
|
|
|
* XXX: year 2038 problem may remain.
|
|
|
|
*/
|
2014-10-23 20:43:16 +00:00
|
|
|
static void
|
|
|
|
key_timehandler(void *arg)
|
2003-09-01 05:35:55 +00:00
|
|
|
{
|
Step 1.5 of importing the network stack virtualization infrastructure
from the vimage project, as per plan established at devsummit 08/08:
http://wiki.freebsd.org/Image/Notes200808DevSummit
Introduce INIT_VNET_*() initializer macros, VNET_FOREACH() iterator
macros, and CURVNET_SET() context setting macros, all currently
resolving to NOPs.
Prepare for virtualization of selected SYSCTL objects by introducing a
family of SYSCTL_V_*() macros, currently resolving to their global
counterparts, i.e. SYSCTL_V_INT() == SYSCTL_INT().
Move selected #defines from sys/sys/vimage.h to newly introduced header
files specific to virtualized subsystems (sys/net/vnet.h,
sys/netinet/vinet.h etc.).
All the changes are verified to have zero functional impact at this
point in time by doing MD5 comparision between pre- and post-change
object files(*).
(*) netipsec/keysock.c did not validate depending on compile time options.
Implemented by: julian, bz, brooks, zec
Reviewed by: julian, bz, brooks, kris, rwatson, ...
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
2008-10-02 15:37:58 +00:00
|
|
|
VNET_ITERATOR_DECL(vnet_iter);
|
2003-09-01 05:35:55 +00:00
|
|
|
time_t now = time_second;
|
|
|
|
|
2009-07-19 14:20:53 +00:00
|
|
|
VNET_LIST_RLOCK_NOSLEEP();
|
Step 1.5 of importing the network stack virtualization infrastructure
from the vimage project, as per plan established at devsummit 08/08:
http://wiki.freebsd.org/Image/Notes200808DevSummit
Introduce INIT_VNET_*() initializer macros, VNET_FOREACH() iterator
macros, and CURVNET_SET() context setting macros, all currently
resolving to NOPs.
Prepare for virtualization of selected SYSCTL objects by introducing a
family of SYSCTL_V_*() macros, currently resolving to their global
counterparts, i.e. SYSCTL_V_INT() == SYSCTL_INT().
Move selected #defines from sys/sys/vimage.h to newly introduced header
files specific to virtualized subsystems (sys/net/vnet.h,
sys/netinet/vinet.h etc.).
All the changes are verified to have zero functional impact at this
point in time by doing MD5 comparision between pre- and post-change
object files(*).
(*) netipsec/keysock.c did not validate depending on compile time options.
Implemented by: julian, bz, brooks, zec
Reviewed by: julian, bz, brooks, kris, rwatson, ...
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
2008-10-02 15:37:58 +00:00
|
|
|
VNET_FOREACH(vnet_iter) {
|
|
|
|
CURVNET_SET(vnet_iter);
|
|
|
|
key_flush_spd(now);
|
|
|
|
key_flush_sad(now);
|
|
|
|
key_flush_acq(now);
|
|
|
|
key_flush_spacq(now);
|
|
|
|
CURVNET_RESTORE();
|
|
|
|
}
|
2009-07-19 14:20:53 +00:00
|
|
|
VNET_LIST_RUNLOCK_NOSLEEP();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
#ifndef IPSEC_DEBUG2
|
|
|
|
/* do exchange to tick time !! */
|
2014-10-23 20:43:16 +00:00
|
|
|
callout_schedule(&key_timer, hz);
|
2002-10-16 02:10:08 +00:00
|
|
|
#endif /* IPSEC_DEBUG2 */
|
|
|
|
}
|
|
|
|
|
|
|
|
u_long
|
|
|
|
key_random()
|
|
|
|
{
|
|
|
|
u_long value;
|
|
|
|
|
|
|
|
key_randomfill(&value, sizeof(value));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-03 03:50:54 +00:00
|
|
|
key_randomfill(void *p, size_t l)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
u_long v;
|
|
|
|
static int warn = 1;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
n = (size_t)read_random(p, (u_int)l);
|
|
|
|
/* last resort */
|
|
|
|
while (n < l) {
|
|
|
|
v = random();
|
|
|
|
bcopy(&v, (u_int8_t *)p + n,
|
|
|
|
l - n < sizeof(v) ? l - n : sizeof(v));
|
|
|
|
n += sizeof(v);
|
|
|
|
|
|
|
|
if (warn) {
|
|
|
|
printf("WARNING: pseudo-random number generator "
|
|
|
|
"used for IPsec processing\n");
|
|
|
|
warn = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map SADB_SATYPE_* to IPPROTO_*.
|
|
|
|
* if satype == SADB_SATYPE then satype is mapped to ~0.
|
|
|
|
* OUT:
|
|
|
|
* 0: invalid satype.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint8_t
|
|
|
|
key_satype2proto(uint8_t satype)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
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;
|
Initial import of RFC 2385 (TCP-MD5) digest support.
This is the first of two commits; bringing in the kernel support first.
This can be enabled by compiling a kernel with options TCP_SIGNATURE
and FAST_IPSEC.
For the uninitiated, this is a TCP option which provides for a means of
authenticating TCP sessions which came into being before IPSEC. It is
still relevant today, however, as it is used by many commercial router
vendors, particularly with BGP, and as such has become a requirement for
interconnect at many major Internet points of presence.
Several parts of the TCP and IP headers, including the segment payload,
are digested with MD5, including a shared secret. The PF_KEY interface
is used to manage the secrets using security associations in the SADB.
There is a limitation here in that as there is no way to map a TCP flow
per-port back to an SPI without polluting tcpcb or using the SPD; the
code to do the latter is unstable at this time. Therefore this code only
supports per-host keying granularity.
Whilst FAST_IPSEC is mutually exclusive with KAME IPSEC (and thus IPv6),
TCP_SIGNATURE applies only to IPv4. For the vast majority of prospective
users of this feature, this will not pose any problem.
This implementation is output-only; that is, the option is honoured when
responding to a host initiating a TCP session, but no effort is made
[yet] to authenticate inbound traffic. This is, however, sufficient to
interwork with Cisco equipment.
Tested with a Cisco 2501 running IOS 12.0(27), and Quagga 0.96.4 with
local patches. Patches for tcpdump to validate TCP-MD5 sessions are also
available from me upon request.
Sponsored by: sentex.net
2004-02-11 04:26:04 +00:00
|
|
|
case SADB_X_SATYPE_TCPSIGNATURE:
|
|
|
|
return IPPROTO_TCP;
|
2002-10-16 02:10:08 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map IPPROTO_* to SADB_SATYPE_*
|
|
|
|
* OUT:
|
|
|
|
* 0: invalid protocol type.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint8_t
|
|
|
|
key_proto2satype(uint8_t proto)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_AH:
|
|
|
|
return SADB_SATYPE_AH;
|
|
|
|
case IPPROTO_ESP:
|
|
|
|
return SADB_SATYPE_ESP;
|
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
return SADB_X_SATYPE_IPCOMP;
|
Initial import of RFC 2385 (TCP-MD5) digest support.
This is the first of two commits; bringing in the kernel support first.
This can be enabled by compiling a kernel with options TCP_SIGNATURE
and FAST_IPSEC.
For the uninitiated, this is a TCP option which provides for a means of
authenticating TCP sessions which came into being before IPSEC. It is
still relevant today, however, as it is used by many commercial router
vendors, particularly with BGP, and as such has become a requirement for
interconnect at many major Internet points of presence.
Several parts of the TCP and IP headers, including the segment payload,
are digested with MD5, including a shared secret. The PF_KEY interface
is used to manage the secrets using security associations in the SADB.
There is a limitation here in that as there is no way to map a TCP flow
per-port back to an SPI without polluting tcpcb or using the SPD; the
code to do the latter is unstable at this time. Therefore this code only
supports per-host keying granularity.
Whilst FAST_IPSEC is mutually exclusive with KAME IPSEC (and thus IPv6),
TCP_SIGNATURE applies only to IPv4. For the vast majority of prospective
users of this feature, this will not pose any problem.
This implementation is output-only; that is, the option is honoured when
responding to a host initiating a TCP session, but no effort is made
[yet] to authenticate inbound traffic. This is, however, sufficient to
interwork with Cisco equipment.
Tested with a Cisco 2501 running IOS 12.0(27), and Quagga 0.96.4 with
local patches. Patches for tcpdump to validate TCP-MD5 sessions are also
available from me upon request.
Sponsored by: sentex.net
2004-02-11 04:26:04 +00:00
|
|
|
case IPPROTO_TCP:
|
|
|
|
return SADB_X_SATYPE_TCPSIGNATURE;
|
2002-10-16 02:10:08 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* %%% PF_KEY */
|
|
|
|
/*
|
|
|
|
* SADB_GETSPI processing is to receive
|
|
|
|
* <base, (SA2), src address, dst address, (SPI range)>
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_getspi(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secasindex saidx;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
struct secasvar *sav;
|
|
|
|
uint32_t reqid, spi;
|
2002-10-16 02:10:08 +00:00
|
|
|
int error;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint8_t mode, proto;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST)
|
|
|
|
#ifdef PFKEY_STRICT_CHECKS
|
|
|
|
|| SADB_CHECKHDR(mhp, SADB_EXT_SPIRANGE)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)
|
|
|
|
#ifdef PFKEY_STRICT_CHECKS
|
|
|
|
|| SADB_CHECKLEN(mhp, SADB_EXT_SPIRANGE)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_X_EXT_SA2)) {
|
2002-10-16 02:10:08 +00:00
|
|
|
mode = IPSEC_MODE_ANY;
|
|
|
|
reqid = 0;
|
2017-02-06 08:49:57 +00:00
|
|
|
} else {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA2)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n",
|
|
|
|
__func__));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
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;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
|
|
|
|
dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
|
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
|
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
|
|
|
|
(struct sockaddr *)(dst0 + 1));
|
|
|
|
if (error != 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
|
|
|
|
|
|
|
|
/* SPI allocation */
|
2017-02-06 08:49:57 +00:00
|
|
|
spi = key_do_getnewspi(
|
|
|
|
(struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE], &saidx);
|
|
|
|
if (spi == 0) {
|
|
|
|
/*
|
|
|
|
* Requested SPI or SPI range is not available or
|
|
|
|
* already used.
|
|
|
|
*/
|
|
|
|
error = EEXIST;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
sav = key_newsav(mhp, &saidx, spi, &error);
|
|
|
|
if (sav == NULL)
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->seq != 0) {
|
|
|
|
/*
|
|
|
|
* RFC2367:
|
|
|
|
* If the SADB_GETSPI message is in response to a
|
|
|
|
* kernel-generated SADB_ACQUIRE, the sadb_msg_seq
|
|
|
|
* MUST be the same as the SADB_ACQUIRE message.
|
|
|
|
*
|
|
|
|
* XXXAE: However it doesn't definethe behaviour how to
|
|
|
|
* check this and what to do if it doesn't match.
|
|
|
|
* Also what we should do if it matches?
|
|
|
|
*
|
|
|
|
* We can compare saidx used in SADB_ACQUIRE with saidx
|
|
|
|
* used in SADB_GETSPI, but this probably can break
|
|
|
|
* existing software. For now just warn if it doesn't match.
|
|
|
|
*
|
|
|
|
* XXXAE: anyway it looks useless.
|
|
|
|
*/
|
|
|
|
key_acqdone(&saidx, sav->seq);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SA(%p)\n", __func__, sav));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasv(sav));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
struct mbuf *n, *nn;
|
|
|
|
struct sadb_sa *m_sa;
|
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
int off, len;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
|
|
|
|
PFKEY_ALIGN8(sizeof(struct sadb_sa));
|
|
|
|
|
2012-12-05 08:04:20 +00:00
|
|
|
MGETHDR(n, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (len > MHLEN) {
|
2015-01-06 12:59:37 +00:00
|
|
|
if (!(MCLGET(n, M_NOWAIT))) {
|
2002-10-16 02:10:08 +00:00
|
|
|
m_freem(n);
|
|
|
|
n = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (!n) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
|
|
|
|
m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
|
|
|
|
m_sa->sadb_sa_exttype = SADB_EXT_SA;
|
2017-02-06 08:49:57 +00:00
|
|
|
m_sa->sadb_sa_spi = spi; /* SPI is already in network byte order */
|
2002-10-16 02:10:08 +00:00
|
|
|
off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(off == len,
|
|
|
|
("length inconsistency (off %u len %u)", off, len));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
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);
|
2017-02-06 08:49:57 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 *);
|
2017-02-06 08:49:57 +00:00
|
|
|
newmsg->sadb_msg_seq = sav->seq;
|
2002-10-16 02:10:08 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
return (key_senderror(so, m, error));
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocating new SPI
|
|
|
|
* called by key_getspi().
|
|
|
|
* OUT:
|
|
|
|
* 0: failure.
|
2017-02-06 08:49:57 +00:00
|
|
|
* others: success, SPI in network byte order.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint32_t
|
2014-12-03 03:50:54 +00:00
|
|
|
key_do_getnewspi(struct sadb_spirange *spirange, struct secasindex *saidx)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t min, max, newspi, t;
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
int count = V_key_spi_trycnt;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* set spi range to allocate */
|
|
|
|
if (spirange != NULL) {
|
|
|
|
min = spirange->sadb_spirange_min;
|
|
|
|
max = spirange->sadb_spirange_max;
|
|
|
|
} else {
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
min = V_key_spi_minval;
|
|
|
|
max = V_key_spi_maxval;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
/* IPCOMP needs 2-byte SPI */
|
|
|
|
if (saidx->proto == IPPROTO_IPCOMP) {
|
|
|
|
if (min >= 0x10000)
|
|
|
|
min = 0xffff;
|
|
|
|
if (max >= 0x10000)
|
|
|
|
max = 0xffff;
|
|
|
|
if (min > max) {
|
|
|
|
t = min; min = max; max = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min == max) {
|
2017-02-06 08:49:57 +00:00
|
|
|
if (!key_checkspidup(htonl(min))) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__, min));
|
2002-10-16 02:10:08 +00: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. */
|
|
|
|
newspi = min + (key_random() % (max - min + 1));
|
2017-02-06 08:49:57 +00:00
|
|
|
if (!key_checkspidup(htonl(newspi)))
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0 || newspi == 0) {
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: failed to allocate SPI.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* statistics */
|
|
|
|
keystat.getspi_count =
|
2017-02-06 08:49:57 +00:00
|
|
|
(keystat.getspi_count + V_key_spi_trycnt - count) / 2;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
return (htonl(newspi));
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Find TCP-MD5 SA with corresponding secasindex.
|
|
|
|
* If not found, return NULL and fill SPI with usable value if needed.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
static struct secasvar *
|
|
|
|
key_getsav_tcpmd5(struct secasindex *saidx, uint32_t *spi)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
IPSEC_ASSERT(saidx->proto == IPPROTO_TCP, ("wrong proto"));
|
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
|
|
|
|
if (sah->saidx.proto != IPPROTO_TCP)
|
|
|
|
continue;
|
2017-04-04 13:41:50 +00:00
|
|
|
if (!key_sockaddrcmp(&saidx->dst.sa, &sah->saidx.dst.sa, 0) &&
|
|
|
|
!key_sockaddrcmp(&saidx->src.sa, &sah->saidx.src.sa, 0))
|
2017-02-06 08:49:57 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sah != NULL) {
|
|
|
|
if (V_key_preferred_oldsa)
|
|
|
|
sav = TAILQ_LAST(&sah->savtree_alive, secasvar_queue);
|
|
|
|
else
|
|
|
|
sav = TAILQ_FIRST(&sah->savtree_alive);
|
|
|
|
if (sav != NULL) {
|
|
|
|
SAV_ADDREF(sav);
|
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
return (sav);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (spi == NULL) {
|
|
|
|
/* No SPI required */
|
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
return (NULL);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Check that SPI is unique */
|
|
|
|
LIST_FOREACH(sav, SAVHASH_HASH(*spi), spihash) {
|
|
|
|
if (sav->spi == *spi)
|
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav == NULL) {
|
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
/* SPI is already unique */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
/* XXX: not optimal */
|
|
|
|
*spi = key_do_getnewspi(NULL, saidx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static int
|
|
|
|
key_updateaddresses(struct socket *so, struct mbuf *m,
|
|
|
|
const struct sadb_msghdr *mhp, struct secasvar *sav,
|
|
|
|
struct secasindex *saidx)
|
|
|
|
{
|
|
|
|
struct sockaddr *newaddr;
|
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *newsav, *tmp;
|
|
|
|
struct mbuf *n;
|
|
|
|
int error, isnew;
|
|
|
|
|
|
|
|
/* Check that we need to change SAH */
|
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_SRC)) {
|
|
|
|
newaddr = (struct sockaddr *)(
|
|
|
|
((struct sadb_address *)
|
|
|
|
mhp->ext[SADB_X_EXT_NEW_ADDRESS_SRC]) + 1);
|
|
|
|
bcopy(newaddr, &saidx->src, newaddr->sa_len);
|
|
|
|
key_porttosaddr(&saidx->src.sa, 0);
|
|
|
|
}
|
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_DST)) {
|
|
|
|
newaddr = (struct sockaddr *)(
|
|
|
|
((struct sadb_address *)
|
|
|
|
mhp->ext[SADB_X_EXT_NEW_ADDRESS_DST]) + 1);
|
|
|
|
bcopy(newaddr, &saidx->dst, newaddr->sa_len);
|
|
|
|
key_porttosaddr(&saidx->dst.sa, 0);
|
|
|
|
}
|
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_SRC) ||
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_DST)) {
|
|
|
|
error = key_checksockaddrs(&saidx->src.sa, &saidx->dst.sa);
|
|
|
|
if (error != 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid new sockaddr.\n",
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
return (error);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sah = key_getsah(saidx);
|
|
|
|
if (sah == NULL) {
|
|
|
|
/* create a new SA index */
|
|
|
|
sah = key_newsah(saidx);
|
|
|
|
if (sah == NULL) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: No more memory.\n", __func__));
|
|
|
|
return (ENOBUFS);
|
|
|
|
}
|
|
|
|
isnew = 2; /* SAH is new */
|
|
|
|
} else
|
|
|
|
isnew = 1; /* existing SAH is referenced */
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
} else {
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* src and dst addresses are still the same.
|
|
|
|
* Do we want to change NAT-T config?
|
|
|
|
*/
|
|
|
|
if (sav->sah->saidx.proto != IPPROTO_ESP ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_TYPE) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_SPORT) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_DPORT)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
return (EINVAL);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* We hold reference to SA, thus SAH will be referenced too. */
|
|
|
|
sah = sav->sah;
|
|
|
|
isnew = 0;
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA,
|
|
|
|
M_NOWAIT | M_ZERO);
|
|
|
|
if (newsav == NULL) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clone SA's content into newsav */
|
|
|
|
SAV_INITREF(newsav);
|
|
|
|
bcopy(sav, newsav, offsetof(struct secasvar, chain));
|
|
|
|
/*
|
|
|
|
* We create new NAT-T config if it is needed.
|
|
|
|
* Old NAT-T config will be freed by key_cleansav() when
|
|
|
|
* last reference to SA will be released.
|
|
|
|
*/
|
|
|
|
newsav->natt = NULL;
|
|
|
|
newsav->sah = sah;
|
|
|
|
newsav->state = SADB_SASTATE_MATURE;
|
2017-11-03 11:33:13 +00:00
|
|
|
error = key_setnatt(newsav, mhp);
|
2017-02-06 08:49:57 +00:00
|
|
|
if (error != 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
/* Check that SA is still alive */
|
|
|
|
if (sav->state == SADB_SASTATE_DEAD) {
|
|
|
|
/* SA was unlinked */
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
error = ESRCH;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlink SA from SAH and SPI hash */
|
|
|
|
IPSEC_ASSERT((sav->flags & SADB_X_EXT_F_CLONED) == 0,
|
|
|
|
("SA is already cloned"));
|
|
|
|
IPSEC_ASSERT(sav->state == SADB_SASTATE_MATURE ||
|
|
|
|
sav->state == SADB_SASTATE_DYING,
|
|
|
|
("Wrong SA state %u\n", sav->state));
|
|
|
|
TAILQ_REMOVE(&sav->sah->savtree_alive, sav, chain);
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Link new SA with SAH. Keep SAs ordered by
|
|
|
|
* create time (newer are first).
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(tmp, &sah->savtree_alive, chain) {
|
|
|
|
if (newsav->created > tmp->created) {
|
|
|
|
TAILQ_INSERT_BEFORE(tmp, newsav, chain);
|
|
|
|
break;
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (tmp == NULL)
|
|
|
|
TAILQ_INSERT_TAIL(&sah->savtree_alive, newsav, chain);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Add new SA into SPI hash. */
|
|
|
|
LIST_INSERT_HEAD(SAVHASH_HASH(newsav->spi), newsav, spihash);
|
|
|
|
|
|
|
|
/* Add new SAH into SADB. */
|
|
|
|
if (isnew == 2) {
|
|
|
|
TAILQ_INSERT_HEAD(&V_sahtree, sah, chain);
|
|
|
|
LIST_INSERT_HEAD(SAHADDRHASH_HASH(saidx), sah, addrhash);
|
|
|
|
sah->state = SADB_SASTATE_MATURE;
|
|
|
|
SAH_ADDREF(sah); /* newsav references new SAH */
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* isnew == 1 -> @sah was referenced by key_getsah().
|
|
|
|
* isnew == 0 -> we use the same @sah, that was used by @sav,
|
|
|
|
* and we use its reference for @newsav.
|
|
|
|
*/
|
|
|
|
SECASVAR_LOCK(sav);
|
|
|
|
/* XXX: replace cntr with pointer? */
|
|
|
|
newsav->cntr = sav->cntr;
|
|
|
|
sav->flags |= SADB_X_EXT_F_CLONED;
|
|
|
|
SECASVAR_UNLOCK(sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_WUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SA(%p) cloned into SA(%p)\n",
|
|
|
|
__func__, sav, newsav));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasv(newsav));
|
|
|
|
|
|
|
|
key_freesav(&sav); /* release last reference */
|
|
|
|
|
|
|
|
/* set msg buf from mhp */
|
|
|
|
n = key_getmsgbuf_x1(m, mhp);
|
|
|
|
if (n == NULL) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
|
|
|
return (ENOBUFS);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
m_freem(m);
|
|
|
|
key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
|
|
|
return (0);
|
|
|
|
fail:
|
|
|
|
if (isnew != 0)
|
|
|
|
key_freesah(&sah);
|
|
|
|
if (newsav != NULL) {
|
|
|
|
if (newsav->natt != NULL)
|
|
|
|
free(newsav->natt, M_IPSEC_MISC);
|
|
|
|
free(newsav, M_IPSEC_SA);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* SADB_UPDATE processing
|
|
|
|
* receive
|
|
|
|
* <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
|
|
|
|
* key(AE), (identity(SD),) (sensitivity)>
|
|
|
|
* from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
|
|
|
|
* and send
|
|
|
|
* <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
|
|
|
|
* (identity(SD),) (sensitivity)>
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_update(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
|
|
|
{
|
|
|
|
struct secasindex saidx;
|
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
struct sadb_sa *sa0;
|
|
|
|
struct secasvar *sav;
|
|
|
|
uint32_t reqid;
|
|
|
|
int error;
|
|
|
|
uint8_t mode, proto;
|
|
|
|
|
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_SA) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
(SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD) &&
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT)) ||
|
|
|
|
(SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT) &&
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD))) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_SA) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_X_EXT_SA2)) {
|
|
|
|
mode = IPSEC_MODE_ANY;
|
|
|
|
reqid = 0;
|
|
|
|
} else {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA2)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n",
|
|
|
|
__func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
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;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00: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]);
|
2009-06-19 21:01:55 +00:00
|
|
|
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Only SADB_SASTATE_MATURE SAs may be submitted in an
|
|
|
|
* SADB_UPDATE message.
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sa0->sadb_sa_state != SADB_SASTATE_MATURE) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid state.\n", __func__));
|
|
|
|
#ifdef PFKEY_STRICT_CHECKS
|
|
|
|
return key_senderror(so, m, EINVAL);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
#endif
|
2010-05-05 08:55:26 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
|
|
|
|
(struct sockaddr *)(dst0 + 1));
|
|
|
|
if (error != 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
|
|
|
|
return key_senderror(so, m, error);
|
|
|
|
}
|
|
|
|
KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
|
|
|
|
sav = key_getsavbyspi(sa0->sadb_sa_spi);
|
|
|
|
if (sav == NULL) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: no SA found for SPI %u\n",
|
|
|
|
__func__, ntohl(sa0->sadb_sa_spi)));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check that SADB_UPDATE issued by the same process that did
|
|
|
|
* SADB_GETSPI or SADB_ADD.
|
|
|
|
*/
|
|
|
|
if (sav->pid != mhp->msg->sadb_msg_pid) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: pid mismatched (SPI %u, pid %u vs. %u)\n", __func__,
|
|
|
|
ntohl(sav->spi), sav->pid, mhp->msg->sadb_msg_pid));
|
|
|
|
key_freesav(&sav);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
/* saidx should match with SA. */
|
|
|
|
if (key_cmpsaidx(&sav->sah->saidx, &saidx, CMP_MODE_REQID) == 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: saidx mismatched for SPI %u",
|
|
|
|
__func__, ntohl(sav->spi)));
|
|
|
|
key_freesav(&sav);
|
|
|
|
return key_senderror(so, m, ESRCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sav->state == SADB_SASTATE_LARVAL) {
|
|
|
|
if ((mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_KEY_ENCRYPT)) ||
|
|
|
|
(mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_KEY_AUTH))) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
|
|
|
key_freesav(&sav);
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We can set any values except src, dst and SPI.
|
|
|
|
*/
|
|
|
|
error = key_setsaval(sav, mhp);
|
|
|
|
if (error != 0) {
|
|
|
|
key_freesav(&sav);
|
|
|
|
return (key_senderror(so, m, error));
|
|
|
|
}
|
|
|
|
/* Change SA state to MATURE */
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
if (sav->state != SADB_SASTATE_LARVAL) {
|
|
|
|
/* SA was deleted or another thread made it MATURE. */
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
key_freesav(&sav);
|
|
|
|
return (key_senderror(so, m, ESRCH));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* NOTE: we keep SAs in savtree_alive ordered by created
|
|
|
|
* time. When SA's state changed from LARVAL to MATURE,
|
|
|
|
* we update its created time in key_setsaval() and move
|
|
|
|
* it into head of savtree_alive.
|
|
|
|
*/
|
|
|
|
TAILQ_REMOVE(&sav->sah->savtree_larval, sav, chain);
|
|
|
|
TAILQ_INSERT_HEAD(&sav->sah->savtree_alive, sav, chain);
|
|
|
|
sav->state = SADB_SASTATE_MATURE;
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* For DYING and MATURE SA we can change only state
|
|
|
|
* and lifetimes. Report EINVAL if something else attempted
|
|
|
|
* to change.
|
|
|
|
*/
|
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_EXT_KEY_ENCRYPT) ||
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_EXT_KEY_AUTH)) {
|
|
|
|
key_freesav(&sav);
|
|
|
|
return (key_senderror(so, m, EINVAL));
|
|
|
|
}
|
|
|
|
error = key_updatelifetimes(sav, mhp);
|
|
|
|
if (error != 0) {
|
|
|
|
key_freesav(&sav);
|
|
|
|
return (key_senderror(so, m, error));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* This is FreeBSD extension to RFC2367.
|
|
|
|
* IKEd can specify SADB_X_EXT_NEW_ADDRESS_SRC and/or
|
|
|
|
* SADB_X_EXT_NEW_ADDRESS_DST when it wants to change
|
|
|
|
* SA addresses (for example to implement MOBIKE protocol
|
|
|
|
* as described in RFC4555). Also we allow to change
|
|
|
|
* NAT-T config.
|
|
|
|
*/
|
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_SRC) ||
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_DST) ||
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_TYPE) ||
|
|
|
|
sav->natt != NULL) {
|
|
|
|
error = key_updateaddresses(so, m, mhp, sav, &saidx);
|
|
|
|
key_freesav(&sav);
|
|
|
|
if (error != 0)
|
|
|
|
return (key_senderror(so, m, error));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* Check that SA is still alive */
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
if (sav->state == SADB_SASTATE_DEAD) {
|
|
|
|
/* SA was unlinked */
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
key_freesav(&sav);
|
|
|
|
return (key_senderror(so, m, ESRCH));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* NOTE: there is possible state moving from DYING to MATURE,
|
|
|
|
* but this doesn't change created time, so we won't reorder
|
|
|
|
* this SA.
|
|
|
|
*/
|
|
|
|
sav->state = SADB_SASTATE_MATURE;
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
}
|
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SA(%p)\n", __func__, sav));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasv(sav));
|
|
|
|
key_freesav(&sav);
|
2010-05-05 08:55:26 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
|
|
|
|
/* set msg buf from mhp */
|
|
|
|
n = key_getmsgbuf_x1(m, mhp);
|
|
|
|
if (n == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_ADD processing
|
2003-01-01 18:49:04 +00:00
|
|
|
* add an entry to SA database, when received
|
2002-10-16 02:10:08 +00:00
|
|
|
* <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
|
|
|
|
* key(AE), (identity(SD),) (sensitivity)>
|
|
|
|
* from the ikmpd,
|
|
|
|
* and send
|
|
|
|
* <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
|
|
|
|
* (identity(SD),) (sensitivity)>
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
|
|
|
* IGNORE identity and sensitivity messages.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_add(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secasindex saidx;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
struct sadb_sa *sa0;
|
|
|
|
struct secasvar *sav;
|
|
|
|
uint32_t reqid, spi;
|
|
|
|
uint8_t mode, proto;
|
2002-10-16 02:10:08 +00:00
|
|
|
int error;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_SA) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
(mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP && (
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_KEY_ENCRYPT) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_KEY_ENCRYPT))) ||
|
|
|
|
(mhp->msg->sadb_msg_satype == SADB_SATYPE_AH && (
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_KEY_AUTH) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_KEY_AUTH))) ||
|
|
|
|
(SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD) &&
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT)) ||
|
|
|
|
(SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT) &&
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD))) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_SA) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_X_EXT_SA2)) {
|
2002-10-16 02:10:08 +00:00
|
|
|
mode = IPSEC_MODE_ANY;
|
|
|
|
reqid = 0;
|
2017-02-06 08:49:57 +00:00
|
|
|
} else {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA2)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n",
|
|
|
|
__func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
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;
|
2002-10-16 02:10:08 +00: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];
|
|
|
|
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* Only SADB_SASTATE_MATURE SAs may be submitted in an
|
|
|
|
* SADB_ADD message.
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sa0->sadb_sa_state != SADB_SASTATE_MATURE) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid state.\n", __func__));
|
|
|
|
#ifdef PFKEY_STRICT_CHECKS
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
#endif
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
|
|
|
|
(struct sockaddr *)(dst0 + 1));
|
|
|
|
if (error != 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
|
|
|
|
return key_senderror(so, m, error);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
|
|
|
|
spi = sa0->sadb_sa_spi;
|
|
|
|
/*
|
|
|
|
* For TCP-MD5 SAs we don't use SPI. Check the uniqueness using
|
|
|
|
* secasindex.
|
|
|
|
* XXXAE: IPComp seems also doesn't use SPI.
|
|
|
|
*/
|
|
|
|
if (proto == IPPROTO_TCP) {
|
|
|
|
sav = key_getsav_tcpmd5(&saidx, &spi);
|
|
|
|
if (sav == NULL && spi == 0) {
|
|
|
|
/* Failed to allocate SPI */
|
|
|
|
ipseclog((LOG_DEBUG, "%s: SA already exists.\n",
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
__func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
return key_senderror(so, m, EEXIST);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
/* XXX: SPI that we report back can have another value */
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
} else {
|
2017-02-06 08:49:57 +00:00
|
|
|
/* We can create new SA only if SPI is different. */
|
|
|
|
sav = key_getsavbyspi(spi);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav != NULL) {
|
|
|
|
key_freesav(&sav);
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EEXIST);
|
|
|
|
}
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
sav = key_newsav(mhp, &saidx, spi, &error);
|
|
|
|
if (sav == NULL)
|
2010-05-05 08:58:58 +00:00
|
|
|
return key_senderror(so, m, error);
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: return SA(%p)\n", __func__, sav));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasv(sav));
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* If SADB_ADD was in response to SADB_ACQUIRE, we need to schedule
|
|
|
|
* ACQ for deletion.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->seq != 0)
|
|
|
|
key_acqdone(&saidx, sav->seq);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* Don't call key_freesav() on error here, as we would like to
|
|
|
|
* keep the SA in the database.
|
|
|
|
*/
|
2002-10-16 02:10:08 +00:00
|
|
|
struct mbuf *n;
|
|
|
|
|
|
|
|
/* set msg buf from mhp */
|
|
|
|
n = key_getmsgbuf_x1(m, mhp);
|
|
|
|
if (n == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* NAT-T support.
|
|
|
|
* IKEd may request the use ESP in UDP encapsulation when it detects the
|
|
|
|
* presence of NAT. It uses NAT-T extension headers for such SAs to specify
|
|
|
|
* parameters needed for encapsulation and decapsulation. These PF_KEY
|
|
|
|
* extension headers are not standardized, so this comment addresses our
|
|
|
|
* implementation.
|
|
|
|
* SADB_X_EXT_NAT_T_TYPE specifies type of encapsulation, we support only
|
|
|
|
* UDP_ENCAP_ESPINUDP as described in RFC3948.
|
|
|
|
* SADB_X_EXT_NAT_T_SPORT/DPORT specifies source and destination ports for
|
|
|
|
* UDP header. We use these ports in UDP encapsulation procedure, also we
|
|
|
|
* can check them in UDP decapsulation procedure.
|
|
|
|
* SADB_X_EXT_NAT_T_OA[IR] specifies original address of initiator or
|
|
|
|
* responder. These addresses can be used for transport mode to adjust
|
|
|
|
* checksum after decapsulation and decryption. Since original IP addresses
|
|
|
|
* used by peer usually different (we detected presence of NAT), TCP/UDP
|
|
|
|
* pseudo header checksum and IP header checksum was calculated using original
|
|
|
|
* addresses. After decapsulation and decryption we need to adjust checksum
|
|
|
|
* to have correct datagram.
|
|
|
|
*
|
|
|
|
* We expect presence of NAT-T extension headers only in SADB_ADD and
|
|
|
|
* SADB_UPDATE messages. We report NAT-T extension headers in replies
|
|
|
|
* to SADB_ADD, SADB_UPDATE, SADB_GET, and SADB_DUMP messages.
|
|
|
|
*/
|
2002-10-16 02:10:08 +00:00
|
|
|
static int
|
2017-02-06 08:49:57 +00:00
|
|
|
key_setnatt(struct secasvar *sav, const struct sadb_msghdr *mhp)
|
|
|
|
{
|
|
|
|
struct sadb_x_nat_t_port *port;
|
|
|
|
struct sadb_x_nat_t_type *type;
|
|
|
|
struct sadb_address *oai, *oar;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
uint32_t addr;
|
|
|
|
uint16_t cksum;
|
|
|
|
|
|
|
|
IPSEC_ASSERT(sav->natt == NULL, ("natt is already initialized"));
|
|
|
|
/*
|
|
|
|
* Ignore NAT-T headers if sproto isn't ESP.
|
|
|
|
*/
|
|
|
|
if (sav->sah->saidx.proto != IPPROTO_ESP)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_TYPE) &&
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_SPORT) &&
|
|
|
|
!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_DPORT)) {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_TYPE) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_SPORT) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_DPORT)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n",
|
|
|
|
__func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
type = (struct sadb_x_nat_t_type *)mhp->ext[SADB_X_EXT_NAT_T_TYPE];
|
|
|
|
if (type->sadb_x_nat_t_type_type != UDP_ENCAP_ESPINUDP) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: unsupported NAT-T type %u.\n",
|
|
|
|
__func__, type->sadb_x_nat_t_type_type));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Allocate storage for NAT-T config.
|
|
|
|
* On error it will be released by key_cleansav().
|
|
|
|
*/
|
|
|
|
sav->natt = malloc(sizeof(struct secnatt), M_IPSEC_MISC,
|
|
|
|
M_NOWAIT | M_ZERO);
|
|
|
|
if (sav->natt == NULL) {
|
|
|
|
PFKEYSTAT_INC(in_nomem);
|
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
|
|
|
return (ENOBUFS);
|
|
|
|
}
|
|
|
|
port = (struct sadb_x_nat_t_port *)mhp->ext[SADB_X_EXT_NAT_T_SPORT];
|
|
|
|
if (port->sadb_x_nat_t_port_port == 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid NAT-T sport specified.\n",
|
|
|
|
__func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
sav->natt->sport = port->sadb_x_nat_t_port_port;
|
|
|
|
port = (struct sadb_x_nat_t_port *)mhp->ext[SADB_X_EXT_NAT_T_DPORT];
|
|
|
|
if (port->sadb_x_nat_t_port_port == 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid NAT-T dport specified.\n",
|
|
|
|
__func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
sav->natt->dport = port->sadb_x_nat_t_port_port;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_X_EXT_NAT_T_OAI and SADB_X_EXT_NAT_T_OAR are optional
|
|
|
|
* and needed only for transport mode IPsec.
|
|
|
|
* Usually NAT translates only one address, but it is possible,
|
|
|
|
* that both addresses could be translated.
|
|
|
|
* NOTE: Value of SADB_X_EXT_NAT_T_OAI is equal to SADB_X_EXT_NAT_T_OA.
|
|
|
|
*/
|
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_OAI)) {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_OAI)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n",
|
|
|
|
__func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
oai = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
|
|
|
|
} else
|
|
|
|
oai = NULL;
|
|
|
|
if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_OAR)) {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_OAR)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n",
|
|
|
|
__func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
oar = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
|
|
|
|
} else
|
|
|
|
oar = NULL;
|
|
|
|
|
|
|
|
/* Initialize addresses only for transport mode */
|
|
|
|
if (sav->sah->saidx.mode != IPSEC_MODE_TUNNEL) {
|
|
|
|
cksum = 0;
|
|
|
|
if (oai != NULL) {
|
|
|
|
/* Currently we support only AF_INET */
|
|
|
|
sa = (struct sockaddr *)(oai + 1);
|
|
|
|
if (sa->sa_family != AF_INET ||
|
|
|
|
sa->sa_len != sizeof(struct sockaddr_in)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: wrong NAT-OAi header.\n",
|
|
|
|
__func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
/* Ignore address if it the same */
|
|
|
|
if (((struct sockaddr_in *)sa)->sin_addr.s_addr !=
|
|
|
|
sav->sah->saidx.src.sin.sin_addr.s_addr) {
|
|
|
|
bcopy(sa, &sav->natt->oai.sa, sa->sa_len);
|
|
|
|
sav->natt->flags |= IPSEC_NATT_F_OAI;
|
|
|
|
/* Calculate checksum delta */
|
|
|
|
addr = sav->sah->saidx.src.sin.sin_addr.s_addr;
|
|
|
|
cksum = in_addword(cksum, ~addr >> 16);
|
|
|
|
cksum = in_addword(cksum, ~addr & 0xffff);
|
|
|
|
addr = sav->natt->oai.sin.sin_addr.s_addr;
|
|
|
|
cksum = in_addword(cksum, addr >> 16);
|
|
|
|
cksum = in_addword(cksum, addr & 0xffff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (oar != NULL) {
|
|
|
|
/* Currently we support only AF_INET */
|
|
|
|
sa = (struct sockaddr *)(oar + 1);
|
|
|
|
if (sa->sa_family != AF_INET ||
|
|
|
|
sa->sa_len != sizeof(struct sockaddr_in)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: wrong NAT-OAr header.\n",
|
|
|
|
__func__));
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
/* Ignore address if it the same */
|
|
|
|
if (((struct sockaddr_in *)sa)->sin_addr.s_addr !=
|
|
|
|
sav->sah->saidx.dst.sin.sin_addr.s_addr) {
|
|
|
|
bcopy(sa, &sav->natt->oar.sa, sa->sa_len);
|
|
|
|
sav->natt->flags |= IPSEC_NATT_F_OAR;
|
|
|
|
/* Calculate checksum delta */
|
|
|
|
addr = sav->sah->saidx.dst.sin.sin_addr.s_addr;
|
|
|
|
cksum = in_addword(cksum, ~addr >> 16);
|
|
|
|
cksum = in_addword(cksum, ~addr & 0xffff);
|
|
|
|
addr = sav->natt->oar.sin.sin_addr.s_addr;
|
|
|
|
cksum = in_addword(cksum, addr >> 16);
|
|
|
|
cksum = in_addword(cksum, addr & 0xffff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sav->natt->cksum = cksum;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
key_setident(struct secashead *sah, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
const struct sadb_ident *idsrc, *iddst;
|
|
|
|
int idsrclen, iddstlen;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sah != NULL, ("null secashead"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* don't make buffer if not there */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_IDENTITY_SRC) &&
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_IDENTITY_DST)) {
|
2002-10-16 02:10:08 +00:00
|
|
|
sah->idents = NULL;
|
|
|
|
sah->identd = NULL;
|
2017-02-06 08:49:57 +00:00
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_IDENTITY_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_IDENTITY_DST)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
return (EINVAL);
|
2002-10-16 02:10:08 +00: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];
|
|
|
|
|
|
|
|
/* validity check */
|
|
|
|
if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make structure */
|
2006-03-25 13:38:52 +00:00
|
|
|
sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sah->idents == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return ENOBUFS;
|
|
|
|
}
|
2006-03-25 13:38:52 +00:00
|
|
|
sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sah->identd == NULL) {
|
2003-09-01 05:35:55 +00:00
|
|
|
free(sah->idents, M_IPSEC_MISC);
|
2002-10-16 02:10:08 +00:00
|
|
|
sah->idents = NULL;
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return ENOBUFS;
|
|
|
|
}
|
2006-03-25 13:38:52 +00:00
|
|
|
sah->idents->type = idsrc->sadb_ident_type;
|
|
|
|
sah->idents->id = idsrc->sadb_ident_id;
|
|
|
|
|
|
|
|
sah->identd->type = iddst->sadb_ident_type;
|
|
|
|
sah->identd->id = iddst->sadb_ident_id;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* m will not be freed on return.
|
2017-02-06 08:49:57 +00:00
|
|
|
* it is caller's responsibility to free the result.
|
|
|
|
*
|
|
|
|
* Called from SADB_ADD and SADB_UPDATE. Reply will contain headers
|
|
|
|
* from the request in defined order.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_getmsgbuf_x1(struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
2017-02-06 08:49:57 +00:00
|
|
|
n = key_gather_mbuf(m, mhp, 1, 16, SADB_EXT_RESERVED,
|
2002-10-16 02:10:08 +00:00
|
|
|
SADB_EXT_SA, SADB_X_EXT_SA2,
|
|
|
|
SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
|
|
|
|
SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
|
2017-02-06 08:49:57 +00:00
|
|
|
SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST,
|
|
|
|
SADB_X_EXT_NAT_T_TYPE, SADB_X_EXT_NAT_T_SPORT,
|
|
|
|
SADB_X_EXT_NAT_T_DPORT, SADB_X_EXT_NAT_T_OAI,
|
|
|
|
SADB_X_EXT_NAT_T_OAR, SADB_X_EXT_NEW_ADDRESS_SRC,
|
|
|
|
SADB_X_EXT_NEW_ADDRESS_DST);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!n)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_delete(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secasindex saidx;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
struct secasvar *sav;
|
|
|
|
struct sadb_sa *sa0;
|
|
|
|
uint8_t proto;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
|
|
|
|
dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (key_checksockaddrs((struct sockaddr *)(src0 + 1),
|
|
|
|
(struct sockaddr *)(dst0 + 1)) != 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
|
|
|
|
return (key_senderror(so, m, EINVAL));
|
|
|
|
}
|
|
|
|
KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
|
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_SA)) {
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
|
|
|
* Caller wants us to delete all non-LARVAL SAs
|
|
|
|
* that match the src/dst. This is used during
|
|
|
|
* IKE INITIAL-CONTACT.
|
2017-02-06 08:49:57 +00:00
|
|
|
* XXXAE: this looks like some extension to RFC2367.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
return (key_delete_all(so, m, mhp, &saidx));
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_SA)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
|
|
|
return (key_senderror(so, m, EINVAL));
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
|
|
|
|
if (proto == IPPROTO_TCP)
|
|
|
|
sav = key_getsav_tcpmd5(&saidx, NULL);
|
|
|
|
else
|
|
|
|
sav = key_getsavbyspi(sa0->sadb_sa_spi);
|
|
|
|
if (sav == NULL) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: no SA found for SPI %u.\n",
|
|
|
|
__func__, ntohl(sa0->sadb_sa_spi)));
|
|
|
|
return (key_senderror(so, m, ESRCH));
|
|
|
|
}
|
|
|
|
if (key_cmpsaidx(&sav->sah->saidx, &saidx, CMP_HEAD) == 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: saidx mismatched for SPI %u.\n",
|
|
|
|
__func__, ntohl(sav->spi)));
|
|
|
|
key_freesav(&sav);
|
|
|
|
return (key_senderror(so, m, ESRCH));
|
|
|
|
}
|
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SA(%p)\n", __func__, sav));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasv(sav));
|
|
|
|
key_unlinksav(sav);
|
|
|
|
key_freesav(&sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (n->m_len < sizeof(struct sadb_msg)) {
|
|
|
|
n = m_pullup(n, sizeof(struct sadb_msg));
|
|
|
|
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
|
2014-12-03 03:50:54 +00:00
|
|
|
key_delete_all(struct socket *so, struct mbuf *m,
|
2017-02-06 08:49:57 +00:00
|
|
|
const struct sadb_msghdr *mhp, struct secasindex *saidx)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secasvar_queue drainq;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav, *nextsav;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_INIT(&drainq);
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
|
|
|
|
if (key_cmpsaidx(&sah->saidx, saidx, CMP_HEAD) == 0)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Move all ALIVE SAs into drainq */
|
|
|
|
TAILQ_CONCAT(&drainq, &sah->savtree_alive, chain);
|
|
|
|
}
|
|
|
|
/* Unlink all queued SAs from SPI hash */
|
|
|
|
TAILQ_FOREACH(sav, &drainq, chain) {
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
}
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
/* Now we can release reference for all SAs in drainq */
|
|
|
|
sav = TAILQ_FIRST(&drainq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SA(%p)\n", __func__, sav));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasv(sav));
|
|
|
|
nextsav = TAILQ_NEXT(sav, chain);
|
|
|
|
key_freesah(&sav->sah); /* release reference from SAV */
|
|
|
|
key_freesav(&sav); /* release last reference */
|
|
|
|
sav = nextsav;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* Delete all alive SAs for corresponding xform.
|
|
|
|
* Larval SAs have not initialized tdb_xform, so it is safe to leave them
|
|
|
|
* here when xform disappears.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
key_delete_xform(const struct xformsw *xsp)
|
|
|
|
{
|
|
|
|
struct secasvar_queue drainq;
|
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav, *nextsav;
|
|
|
|
|
|
|
|
TAILQ_INIT(&drainq);
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
TAILQ_FOREACH(sah, &V_sahtree, chain) {
|
|
|
|
sav = TAILQ_FIRST(&sah->savtree_alive);
|
|
|
|
if (sav == NULL)
|
|
|
|
continue;
|
|
|
|
if (sav->tdb_xform != xsp)
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* It is supposed that all SAs in the chain are related to
|
|
|
|
* one xform.
|
|
|
|
*/
|
|
|
|
TAILQ_CONCAT(&drainq, &sah->savtree_alive, chain);
|
|
|
|
}
|
|
|
|
/* Unlink all queued SAs from SPI hash */
|
|
|
|
TAILQ_FOREACH(sav, &drainq, chain) {
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
}
|
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
|
|
|
|
/* Now we can release reference for all SAs in drainq */
|
|
|
|
sav = TAILQ_FIRST(&drainq);
|
|
|
|
while (sav != NULL) {
|
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SA(%p)\n", __func__, sav));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasv(sav));
|
|
|
|
nextsav = TAILQ_NEXT(sav, chain);
|
|
|
|
key_freesah(&sav->sah); /* release reference from SAV */
|
|
|
|
key_freesav(&sav); /* release last reference */
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00: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.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_get(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secasindex saidx;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
struct sadb_sa *sa0;
|
|
|
|
struct secasvar *sav;
|
|
|
|
uint8_t proto;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_SA) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_SA) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (key_checksockaddrs((struct sockaddr *)(src0 + 1),
|
|
|
|
(struct sockaddr *)(dst0 + 1)) != 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (proto == IPPROTO_TCP)
|
|
|
|
sav = key_getsav_tcpmd5(&saidx, NULL);
|
|
|
|
else
|
|
|
|
sav = key_getsavbyspi(sa0->sadb_sa_spi);
|
|
|
|
if (sav == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
return key_senderror(so, m, ESRCH);
|
|
|
|
}
|
|
|
|
if (key_cmpsaidx(&sav->sah->saidx, &saidx, CMP_HEAD) == 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: saidx mismatched for SPI %u.\n",
|
|
|
|
__func__, ntohl(sa0->sadb_sa_spi)));
|
|
|
|
key_freesav(&sav);
|
|
|
|
return (key_senderror(so, m, ESRCH));
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct mbuf *n;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint8_t satype;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* map proto to satype */
|
2017-02-06 08:49:57 +00:00
|
|
|
if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
|
|
|
key_freesav(&sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
|
|
|
|
mhp->msg->sadb_msg_pid);
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
key_freesav(&sav);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX make it sysctl-configurable? */
|
|
|
|
static void
|
2014-12-03 03:50:54 +00:00
|
|
|
key_getcomb_setlifetime(struct sadb_comb *comb)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
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 */
|
|
|
|
comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
|
|
|
|
comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
|
|
|
|
comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX reorder combinations by preference
|
|
|
|
* XXX no idea if the user wants ESP authentication or not
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_getcomb_ealg(void)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct sadb_comb *comb;
|
2017-02-06 08:49:57 +00:00
|
|
|
const struct enc_xform *algo;
|
2002-10-16 02:10:08 +00: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;
|
|
|
|
for (i = 1; i <= SADB_EALG_MAX; i++) {
|
2017-02-06 08:49:57 +00:00
|
|
|
algo = enc_algorithm_lookup(i);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (algo == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* discard algorithms with key size smaller than system min */
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
|
|
|
|
encmin = V_ipsec_esp_keymin;
|
2002-10-16 02:10:08 +00:00
|
|
|
else
|
|
|
|
encmin = _BITS(algo->minkey);
|
|
|
|
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (V_ipsec_esp_auth)
|
2002-10-16 02:10:08 +00:00
|
|
|
m = key_getcomb_ah();
|
|
|
|
else {
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(l <= MLEN,
|
|
|
|
("l=%u > MLEN=%lu", l, (u_long) MLEN));
|
2012-12-05 08:04:20 +00:00
|
|
|
MGET(m, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
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;
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
bzero(comb, sizeof(*comb));
|
|
|
|
key_getcomb_setlifetime(comb);
|
|
|
|
comb->sadb_comb_encrypt = i;
|
|
|
|
comb->sadb_comb_encrypt_minbits = encmin;
|
|
|
|
comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
result = m;
|
|
|
|
else
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-12-03 03:50:54 +00:00
|
|
|
key_getsizes_ah(const struct auth_hash *ah, int alg, u_int16_t* min,
|
|
|
|
u_int16_t* max)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
Step 1.5 of importing the network stack virtualization infrastructure
from the vimage project, as per plan established at devsummit 08/08:
http://wiki.freebsd.org/Image/Notes200808DevSummit
Introduce INIT_VNET_*() initializer macros, VNET_FOREACH() iterator
macros, and CURVNET_SET() context setting macros, all currently
resolving to NOPs.
Prepare for virtualization of selected SYSCTL objects by introducing a
family of SYSCTL_V_*() macros, currently resolving to their global
counterparts, i.e. SYSCTL_V_INT() == SYSCTL_INT().
Move selected #defines from sys/sys/vimage.h to newly introduced header
files specific to virtualized subsystems (sys/net/vnet.h,
sys/netinet/vinet.h etc.).
All the changes are verified to have zero functional impact at this
point in time by doing MD5 comparision between pre- and post-change
object files(*).
(*) netipsec/keysock.c did not validate depending on compile time options.
Implemented by: julian, bz, brooks, zec
Reviewed by: julian, bz, brooks, kris, rwatson, ...
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
2008-10-02 15:37:58 +00:00
|
|
|
|
2017-11-15 22:42:20 +00:00
|
|
|
*min = *max = ah->hashsize;
|
2002-10-16 02:10:08 +00:00
|
|
|
if (ah->keysize == 0) {
|
|
|
|
/*
|
|
|
|
* Transform takes arbitrary key size but algorithm
|
|
|
|
* key size is restricted. Enforce this here.
|
|
|
|
*/
|
|
|
|
switch (alg) {
|
|
|
|
case SADB_X_AALG_MD5: *min = *max = 16; break;
|
|
|
|
case SADB_X_AALG_SHA: *min = *max = 20; break;
|
|
|
|
case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
|
2011-02-18 09:40:13 +00:00
|
|
|
case SADB_X_AALG_SHA2_256: *min = *max = 32; break;
|
|
|
|
case SADB_X_AALG_SHA2_384: *min = *max = 48; break;
|
|
|
|
case SADB_X_AALG_SHA2_512: *min = *max = 64; break;
|
2002-10-16 02:10:08 +00:00
|
|
|
default:
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: unknown AH algorithm %u\n",
|
|
|
|
__func__, alg));
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX reorder combinations by preference
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_getcomb_ah()
|
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
const struct auth_hash *algo;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct sadb_comb *comb;
|
|
|
|
struct mbuf *m;
|
|
|
|
u_int16_t minkeysize, maxkeysize;
|
|
|
|
int i;
|
|
|
|
const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
|
|
|
|
|
|
|
|
m = NULL;
|
|
|
|
for (i = 1; i <= SADB_AALG_MAX; i++) {
|
|
|
|
#if 1
|
|
|
|
/* we prefer HMAC algorithms, not old algorithms */
|
2011-02-18 09:40:13 +00:00
|
|
|
if (i != SADB_AALG_SHA1HMAC &&
|
|
|
|
i != SADB_AALG_MD5HMAC &&
|
|
|
|
i != SADB_X_AALG_SHA2_256 &&
|
|
|
|
i != SADB_X_AALG_SHA2_384 &&
|
|
|
|
i != SADB_X_AALG_SHA2_512)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
|
|
|
#endif
|
2017-02-06 08:49:57 +00:00
|
|
|
algo = auth_algorithm_lookup(i);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!algo)
|
|
|
|
continue;
|
|
|
|
key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
|
|
|
|
/* discard algorithms with key size smaller than system min */
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (_BITS(minkeysize) < V_ipsec_ah_keymin)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!m) {
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(l <= MLEN,
|
|
|
|
("l=%u > MLEN=%lu", l, (u_long) MLEN));
|
2012-12-05 08:04:20 +00:00
|
|
|
MGET(m, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m) {
|
|
|
|
M_ALIGN(m, l);
|
|
|
|
m->m_len = l;
|
|
|
|
m->m_next = NULL;
|
|
|
|
}
|
|
|
|
} else
|
2012-12-05 08:04:20 +00:00
|
|
|
M_PREPEND(m, l, M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
comb = mtod(m, struct sadb_comb *);
|
|
|
|
bzero(comb, sizeof(*comb));
|
|
|
|
key_getcomb_setlifetime(comb);
|
|
|
|
comb->sadb_comb_auth = i;
|
|
|
|
comb->sadb_comb_auth_minbits = _BITS(minkeysize);
|
|
|
|
comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* not really an official behavior. discussed in pf_key@inner.net in Sep2000.
|
|
|
|
* XXX reorder combinations by preference
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
|
|
|
key_getcomb_ipcomp()
|
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
const struct comp_algo *algo;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct sadb_comb *comb;
|
|
|
|
struct mbuf *m;
|
|
|
|
int i;
|
|
|
|
const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
|
|
|
|
|
|
|
|
m = NULL;
|
|
|
|
for (i = 1; i <= SADB_X_CALG_MAX; i++) {
|
2017-02-06 08:49:57 +00:00
|
|
|
algo = comp_algorithm_lookup(i);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!algo)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!m) {
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(l <= MLEN,
|
|
|
|
("l=%u > MLEN=%lu", l, (u_long) MLEN));
|
2012-12-05 08:04:20 +00:00
|
|
|
MGET(m, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m) {
|
|
|
|
M_ALIGN(m, l);
|
|
|
|
m->m_len = l;
|
|
|
|
m->m_next = NULL;
|
|
|
|
}
|
|
|
|
} else
|
2012-12-05 08:04:20 +00:00
|
|
|
M_PREPEND(m, l, M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX no way to pass mode (transport/tunnel) to userland
|
|
|
|
* XXX replay checking?
|
|
|
|
* XXX sysctl interface to ipsec_{ah,esp}_keymin
|
|
|
|
*/
|
|
|
|
static struct mbuf *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_getprop(const struct secasindex *saidx)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct sadb_prop *prop;
|
|
|
|
struct mbuf *m, *n;
|
|
|
|
const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
|
|
|
|
int totlen;
|
|
|
|
|
|
|
|
switch (saidx->proto) {
|
|
|
|
case IPPROTO_ESP:
|
2017-02-06 08:49:57 +00:00
|
|
|
m = key_getcomb_ealg();
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_AH:
|
|
|
|
m = key_getcomb_ah();
|
|
|
|
break;
|
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
m = key_getcomb_ipcomp();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
2012-12-05 08:04:20 +00:00
|
|
|
M_PREPEND(m, l, M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
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
|
|
|
|
* <base, SA, address(SD), (address(P)), x_policy,
|
|
|
|
* (identity(SD),) (sensitivity,) proposal>
|
|
|
|
* to KMD, and expect to receive
|
2016-05-03 18:05:43 +00:00
|
|
|
* <base> with SADB_ACQUIRE if error occurred,
|
2002-10-16 02:10:08 +00:00
|
|
|
* or
|
|
|
|
* <base, src address, dst address, (SPI range)> with SADB_GETSPI
|
|
|
|
* from KMD by PF_KEY.
|
|
|
|
*
|
|
|
|
* XXX x_policy is outside of RFC2367 (KAME extension).
|
|
|
|
* XXX sensitivity is not supported.
|
|
|
|
* XXX for ipcomp, RFC2367 does not define how to fill in proposal.
|
|
|
|
* see comment for key_getcomb_ipcomp().
|
|
|
|
*
|
|
|
|
* OUT:
|
|
|
|
* 0 : succeed
|
|
|
|
* others: error number
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
|
|
|
|
{
|
2015-07-06 12:40:31 +00:00
|
|
|
union sockaddr_union addr;
|
|
|
|
struct mbuf *result, *m;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t seq;
|
2015-07-06 12:40:31 +00:00
|
|
|
int error;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint16_t ul_proto;
|
|
|
|
uint8_t mask, satype;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(saidx != NULL, ("null saidx"));
|
2002-10-16 02:10:08 +00:00
|
|
|
satype = key_proto2satype(saidx->proto);
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2015-07-06 12:40:31 +00:00
|
|
|
error = -1;
|
|
|
|
result = NULL;
|
|
|
|
ul_proto = IPSEC_ULPROTO_ANY;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/* Get seq number to check whether sending message or not. */
|
|
|
|
seq = key_getacq(saidx, &error);
|
|
|
|
if (seq == 0)
|
|
|
|
return (error);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
result = m;
|
|
|
|
|
2015-07-06 12:40:31 +00:00
|
|
|
/*
|
|
|
|
* set sadb_address for saidx's.
|
|
|
|
*
|
|
|
|
* Note that if sp is supplied, then we're being called from
|
2017-02-06 08:49:57 +00:00
|
|
|
* key_allocsa_policy() and should supply port and protocol
|
|
|
|
* information.
|
|
|
|
* XXXAE: why only TCP and UDP? ICMP and SCTP looks applicable too.
|
|
|
|
* XXXAE: probably we can handle this in the ipsec[46]_allocsa().
|
|
|
|
* XXXAE: it looks like we should save this info in the ACQ entry.
|
2015-07-06 12:40:31 +00:00
|
|
|
*/
|
|
|
|
if (sp != NULL && (sp->spidx.ul_proto == IPPROTO_TCP ||
|
|
|
|
sp->spidx.ul_proto == IPPROTO_UDP))
|
|
|
|
ul_proto = sp->spidx.ul_proto;
|
|
|
|
|
|
|
|
addr = saidx->src;
|
|
|
|
mask = FULLMASK;
|
|
|
|
if (ul_proto != IPSEC_ULPROTO_ANY) {
|
|
|
|
switch (sp->spidx.src.sa.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (sp->spidx.src.sin.sin_port != IPSEC_PORT_ANY) {
|
|
|
|
addr.sin.sin_port = sp->spidx.src.sin.sin_port;
|
|
|
|
mask = sp->spidx.prefs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (sp->spidx.src.sin6.sin6_port != IPSEC_PORT_ANY) {
|
2017-02-06 08:49:57 +00:00
|
|
|
addr.sin6.sin6_port =
|
|
|
|
sp->spidx.src.sin6.sin6_port;
|
2015-07-06 12:40:31 +00:00
|
|
|
mask = sp->spidx.prefs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &addr.sa, mask, ul_proto);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
2015-07-06 12:40:31 +00:00
|
|
|
addr = saidx->dst;
|
|
|
|
mask = FULLMASK;
|
|
|
|
if (ul_proto != IPSEC_ULPROTO_ANY) {
|
|
|
|
switch (sp->spidx.dst.sa.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (sp->spidx.dst.sin.sin_port != IPSEC_PORT_ANY) {
|
|
|
|
addr.sin.sin_port = sp->spidx.dst.sin.sin_port;
|
|
|
|
mask = sp->spidx.prefd;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (sp->spidx.dst.sin6.sin6_port != IPSEC_PORT_ANY) {
|
2017-02-06 08:49:57 +00:00
|
|
|
addr.sin6.sin6_port =
|
|
|
|
sp->spidx.dst.sin6.sin6_port;
|
2015-07-06 12:40:31 +00:00
|
|
|
mask = sp->spidx.prefd;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &addr.sa, mask, ul_proto);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
/* XXX proxy address (optional) */
|
|
|
|
|
|
|
|
/* set sadb_x_policy */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sp != NULL) {
|
|
|
|
m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id,
|
|
|
|
sp->priority);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX identity (optional) */
|
|
|
|
#if 0
|
|
|
|
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
|
|
|
|
|
|
|
|
/* XXX sensitivity (optional) */
|
|
|
|
|
|
|
|
/* create proposal/combination extension */
|
|
|
|
m = key_getprop(saidx);
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* outside of spec; make proposal/combination extension optional.
|
|
|
|
*/
|
|
|
|
if (m)
|
|
|
|
m_cat(result, m);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SP(%p)\n", __func__, sp));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasindex(saidx, NULL));
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint32_t
|
|
|
|
key_newacq(const struct secasindex *saidx, int *perror)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secacq *acq;
|
|
|
|
uint32_t seq;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
acq = malloc(sizeof(*acq), M_IPSEC_SAQ, M_NOWAIT | M_ZERO);
|
|
|
|
if (acq == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2017-02-06 08:49:57 +00:00
|
|
|
*perror = ENOBUFS;
|
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy secindex */
|
2017-02-06 08:49:57 +00:00
|
|
|
bcopy(saidx, &acq->saidx, sizeof(acq->saidx));
|
|
|
|
acq->created = time_second;
|
|
|
|
acq->count = 0;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
/* add to acqtree */
|
2003-09-29 22:57:43 +00:00
|
|
|
ACQ_LOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
seq = acq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
|
|
|
|
LIST_INSERT_HEAD(&V_acqtree, acq, chain);
|
|
|
|
LIST_INSERT_HEAD(ACQADDRHASH_HASH(saidx), acq, addrhash);
|
|
|
|
LIST_INSERT_HEAD(ACQSEQHASH_HASH(seq), acq, seqhash);
|
2003-09-29 22:57:43 +00:00
|
|
|
ACQ_UNLOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
*perror = 0;
|
|
|
|
return (seq);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static uint32_t
|
|
|
|
key_getacq(const struct secasindex *saidx, int *perror)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secacq *acq;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t seq;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
ACQ_LOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_FOREACH(acq, ACQADDRHASH_HASH(saidx), addrhash) {
|
|
|
|
if (key_cmpsaidx(&acq->saidx, saidx, CMP_EXACTLY)) {
|
|
|
|
if (acq->count > V_key_blockacq_count) {
|
|
|
|
/*
|
|
|
|
* Reset counter and send message.
|
|
|
|
* Also reset created time to keep ACQ for
|
|
|
|
* this saidx.
|
|
|
|
*/
|
|
|
|
acq->created = time_second;
|
|
|
|
acq->count = 0;
|
|
|
|
seq = acq->seq;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Increment counter and do nothing.
|
|
|
|
* We send SADB_ACQUIRE message only
|
|
|
|
* for each V_key_blockacq_count packet.
|
|
|
|
*/
|
|
|
|
acq->count++;
|
|
|
|
seq = 0;
|
|
|
|
}
|
2003-09-01 05:35:55 +00:00
|
|
|
break;
|
2017-02-06 08:49:57 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2003-09-29 22:57:43 +00:00
|
|
|
ACQ_UNLOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
if (acq != NULL) {
|
|
|
|
*perror = 0;
|
|
|
|
return (seq);
|
|
|
|
}
|
|
|
|
/* allocate new entry */
|
|
|
|
return (key_newacq(saidx, perror));
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static int
|
|
|
|
key_acqreset(uint32_t seq)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secacq *acq;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
ACQ_LOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_FOREACH(acq, ACQSEQHASH_HASH(seq), seqhash) {
|
|
|
|
if (acq->seq == seq) {
|
|
|
|
acq->count = 0;
|
|
|
|
acq->created = time_second;
|
2003-09-01 05:35:55 +00:00
|
|
|
break;
|
2017-02-06 08:49:57 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2003-09-29 22:57:43 +00:00
|
|
|
ACQ_UNLOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
if (acq == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Mark ACQ entry as stale to remove it in key_flush_acq().
|
|
|
|
* Called after successful SADB_GETSPI message.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
key_acqdone(const struct secasindex *saidx, uint32_t seq)
|
|
|
|
{
|
|
|
|
struct secacq *acq;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
ACQ_LOCK();
|
|
|
|
LIST_FOREACH(acq, ACQSEQHASH_HASH(seq), seqhash) {
|
|
|
|
if (acq->seq == seq)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (acq != NULL) {
|
|
|
|
if (key_cmpsaidx(&acq->saidx, saidx, CMP_EXACTLY) == 0) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: Mismatched saidx for ACQ %u", __func__, seq));
|
|
|
|
acq = NULL;
|
|
|
|
} else {
|
|
|
|
acq->created = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: ACQ %u is not found.", __func__, seq));
|
|
|
|
}
|
|
|
|
ACQ_UNLOCK();
|
|
|
|
if (acq == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct secspacq *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_newspacq(struct secpolicyindex *spidx)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secspacq *acq;
|
|
|
|
|
|
|
|
/* get new entry */
|
2003-09-01 05:35:55 +00:00
|
|
|
acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (acq == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy secindex */
|
|
|
|
bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
|
|
|
|
acq->created = time_second;
|
|
|
|
acq->count = 0;
|
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
/* add to spacqtree */
|
2003-09-29 22:57:43 +00:00
|
|
|
SPACQ_LOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
|
2003-09-29 22:57:43 +00:00
|
|
|
SPACQ_UNLOCK();
|
2003-09-01 05:35:55 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
return acq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct secspacq *
|
2014-12-03 03:50:54 +00:00
|
|
|
key_getspacq(struct secpolicyindex *spidx)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secspacq *acq;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
SPACQ_LOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
LIST_FOREACH(acq, &V_spacqtree, chain) {
|
2003-09-01 05:35:55 +00:00
|
|
|
if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
|
|
|
|
/* NB: return holding spacq_lock */
|
2002-10-16 02:10:08 +00:00
|
|
|
return acq;
|
2003-09-01 05:35:55 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2003-09-29 22:57:43 +00:00
|
|
|
SPACQ_UNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_ACQUIRE processing,
|
|
|
|
* in first situation, is receiving
|
|
|
|
* <base>
|
|
|
|
* from the ikmpd, and clear sequence of its secasvar entry.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_acquire2(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
|
|
|
struct sadb_address *src0, *dst0;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secasindex saidx;
|
|
|
|
struct secashead *sah;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t reqid;
|
2002-10-16 02:10:08 +00:00
|
|
|
int error;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint8_t mode, proto;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Error message from KMd.
|
2016-05-03 18:05:43 +00:00
|
|
|
* We assume that if error was occurred in IKEd, the length of PFKEY
|
2002-10-16 02:10:08 +00:00
|
|
|
* message is equal to the size of sadb_msg structure.
|
2016-05-03 18:05:43 +00:00
|
|
|
* We do not raise error even if error occurred in this function.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
|
|
|
if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
|
|
|
|
/* check sequence number */
|
2017-02-06 08:49:57 +00:00
|
|
|
if (mhp->msg->sadb_msg_seq == 0 ||
|
|
|
|
mhp->msg->sadb_msg_errno == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: must specify sequence "
|
2017-02-06 08:49:57 +00:00
|
|
|
"number and errno.\n", __func__));
|
|
|
|
} else {
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
2017-02-06 08:49:57 +00:00
|
|
|
* IKEd reported that error occurred.
|
|
|
|
* XXXAE: what it expects from the kernel?
|
|
|
|
* Probably we should send SADB_ACQUIRE again?
|
|
|
|
* If so, reset ACQ's state.
|
|
|
|
* XXXAE: it looks useless.
|
2002-10-16 02:10:08 +00:00
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
key_acqreset(mhp->msg->sadb_msg_seq);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
m_freem(m);
|
2017-02-06 08:49:57 +00:00
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This message is from user land.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
SADB_CHECKHDR(mhp, SADB_EXT_PROPOSAL)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: missing required header.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST) ||
|
|
|
|
SADB_CHECKLEN(mhp, SADB_EXT_PROPOSAL)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
if (SADB_CHECKHDR(mhp, SADB_X_EXT_SA2)) {
|
|
|
|
mode = IPSEC_MODE_ANY;
|
|
|
|
reqid = 0;
|
|
|
|
} else {
|
|
|
|
if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA2)) {
|
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: invalid message: wrong header size.\n",
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
__func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
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;
|
|
|
|
}
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
|
|
|
|
dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
|
|
|
|
(struct sockaddr *)(dst0 + 1));
|
|
|
|
if (error != 0) {
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
|
|
|
|
return key_senderror(so, m, EINVAL);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/* get a SA index */
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK();
|
|
|
|
LIST_FOREACH(sah, SAHADDRHASH_HASH(&saidx), addrhash) {
|
2002-10-16 02:10:08 +00:00
|
|
|
if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
|
|
|
|
break;
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
if (sah != NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EEXIST);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = key_acquire(&saidx, NULL);
|
|
|
|
if (error != 0) {
|
2017-02-06 08:49:57 +00:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"%s: error %d returned from key_acquire()\n",
|
|
|
|
__func__, error));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, error);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
m_freem(m);
|
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_REGISTER processing.
|
|
|
|
* If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_register(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2016-04-15 17:30:33 +00:00
|
|
|
struct secreg *reg, *newreg = NULL;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* check for invalid register message */
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
|
|
|
|
/* When SATYPE_UNSPEC is specified, only return sabd_supported. */
|
|
|
|
if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
|
|
|
|
goto setmsg;
|
|
|
|
|
|
|
|
/* check whether existing or not */
|
2003-09-29 22:57:43 +00:00
|
|
|
REGTREE_LOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
|
2002-10-16 02:10:08 +00:00
|
|
|
if (reg->so == so) {
|
2003-09-29 22:57:43 +00:00
|
|
|
REGTREE_UNLOCK();
|
|
|
|
ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EEXIST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create regnode */
|
2003-09-01 05:35:55 +00:00
|
|
|
newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (newreg == NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
REGTREE_UNLOCK();
|
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
newreg->so = so;
|
|
|
|
((struct keycb *)sotorawcb(so))->kp_registered++;
|
|
|
|
|
|
|
|
/* add regnode to regtree. */
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
|
2003-09-29 22:57:43 +00:00
|
|
|
REGTREE_UNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
setmsg:
|
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
struct sadb_supported *sup;
|
|
|
|
u_int len, alen, elen;
|
|
|
|
int off;
|
|
|
|
int i;
|
|
|
|
struct sadb_alg *alg;
|
|
|
|
|
|
|
|
/* create new sadb_msg to reply. */
|
|
|
|
alen = 0;
|
|
|
|
for (i = 1; i <= SADB_AALG_MAX; i++) {
|
2017-02-06 08:49:57 +00:00
|
|
|
if (auth_algorithm_lookup(i))
|
2002-10-16 02:10:08 +00:00
|
|
|
alen += sizeof(struct sadb_alg);
|
|
|
|
}
|
|
|
|
if (alen)
|
|
|
|
alen += sizeof(struct sadb_supported);
|
|
|
|
elen = 0;
|
|
|
|
for (i = 1; i <= SADB_EALG_MAX; i++) {
|
2017-02-06 08:49:57 +00:00
|
|
|
if (enc_algorithm_lookup(i))
|
2002-10-16 02:10:08 +00:00
|
|
|
elen += sizeof(struct sadb_alg);
|
|
|
|
}
|
|
|
|
if (elen)
|
|
|
|
elen += sizeof(struct sadb_supported);
|
|
|
|
|
|
|
|
len = sizeof(struct sadb_msg) + alen + elen;
|
|
|
|
|
|
|
|
if (len > MCLBYTES)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
|
2012-12-05 08:04:20 +00:00
|
|
|
MGETHDR(n, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (len > MHLEN) {
|
2015-01-06 12:59:37 +00:00
|
|
|
if (!(MCLGET(n, M_NOWAIT))) {
|
2002-10-16 02:10:08 +00:00
|
|
|
m_freem(n);
|
|
|
|
n = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!n)
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
|
|
|
|
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 *);
|
|
|
|
newmsg->sadb_msg_errno = 0;
|
|
|
|
newmsg->sadb_msg_len = PFKEY_UNIT64(len);
|
|
|
|
off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
|
|
|
|
|
|
|
|
/* for authentication algorithm */
|
|
|
|
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));
|
|
|
|
|
|
|
|
for (i = 1; i <= SADB_AALG_MAX; i++) {
|
2017-02-06 08:49:57 +00:00
|
|
|
const struct auth_hash *aalgo;
|
2002-10-16 02:10:08 +00:00
|
|
|
u_int16_t minkeysize, maxkeysize;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
aalgo = auth_algorithm_lookup(i);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!aalgo)
|
|
|
|
continue;
|
|
|
|
alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
|
|
|
|
alg->sadb_alg_id = i;
|
|
|
|
alg->sadb_alg_ivlen = 0;
|
|
|
|
key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
|
|
|
|
alg->sadb_alg_minbits = _BITS(minkeysize);
|
|
|
|
alg->sadb_alg_maxbits = _BITS(maxkeysize);
|
|
|
|
off += PFKEY_ALIGN8(sizeof(*alg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for encryption algorithm */
|
|
|
|
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));
|
|
|
|
|
|
|
|
for (i = 1; i <= SADB_EALG_MAX; i++) {
|
2017-02-06 08:49:57 +00:00
|
|
|
const struct enc_xform *ealgo;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
ealgo = enc_algorithm_lookup(i);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (!ealgo)
|
|
|
|
continue;
|
|
|
|
alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
|
|
|
|
alg->sadb_alg_id = i;
|
2015-11-16 07:10:42 +00:00
|
|
|
alg->sadb_alg_ivlen = ealgo->ivsize;
|
2002-10-16 02:10:08 +00:00
|
|
|
alg->sadb_alg_minbits = _BITS(ealgo->minkey);
|
|
|
|
alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
|
|
|
|
off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(off == len,
|
|
|
|
("length assumption failed (off %u len %u)", off, len));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* free secreg entry registered.
|
|
|
|
* XXX: I want to do free a socket marked done SADB_RESIGER to socket.
|
|
|
|
*/
|
|
|
|
void
|
2003-09-01 05:35:55 +00:00
|
|
|
key_freereg(struct socket *so)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secreg *reg;
|
|
|
|
int i;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("NULL so"));
|
2002-10-16 02:10:08 +00: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.
|
|
|
|
*/
|
2003-09-29 22:57:43 +00:00
|
|
|
REGTREE_LOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
for (i = 0; i <= SADB_SATYPE_MAX; i++) {
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
LIST_FOREACH(reg, &V_regtree[i], chain) {
|
2003-09-01 05:35:55 +00:00
|
|
|
if (reg->so == so && __LIST_CHAINED(reg)) {
|
2002-10-16 02:10:08 +00:00
|
|
|
LIST_REMOVE(reg, chain);
|
2003-09-01 05:35:55 +00:00
|
|
|
free(reg, M_IPSEC_SAR);
|
2002-10-16 02:10:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-29 22:57:43 +00:00
|
|
|
REGTREE_UNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SADB_EXPIRE processing
|
|
|
|
* send
|
|
|
|
* <base, SA, SA2, lifetime(C and one of HS), address(SD)>
|
|
|
|
* to KMD by PF_KEY.
|
|
|
|
* NOTE: We send only soft lifetime extension.
|
|
|
|
*
|
|
|
|
* OUT: 0 : succeed
|
|
|
|
* others : error number
|
|
|
|
*/
|
|
|
|
static int
|
2015-05-19 08:30:04 +00:00
|
|
|
key_expire(struct secasvar *sav, int hard)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *result = NULL, *m;
|
|
|
|
struct sadb_lifetime *lt;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t replay_count;
|
|
|
|
int error, len;
|
|
|
|
uint8_t satype;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT (sav != NULL, ("null sav"));
|
|
|
|
IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: SA(%p) expired %s lifetime\n", __func__,
|
|
|
|
sav, hard ? "hard": "soft"));
|
|
|
|
KEYDBG(KEY_DATA, kdebug_secasv(sav));
|
2002-10-16 02:10:08 +00:00
|
|
|
/* set msg header */
|
2003-09-29 22:57:43 +00:00
|
|
|
satype = key_proto2satype(sav->sah->saidx.proto);
|
|
|
|
IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
|
2002-10-16 02:10:08 +00:00
|
|
|
m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
result = m;
|
|
|
|
|
|
|
|
/* create SA extension */
|
|
|
|
m = key_setsadbsa(sav);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
/* create SA extension */
|
2016-11-25 14:44:49 +00:00
|
|
|
SECASVAR_LOCK(sav);
|
|
|
|
replay_count = sav->replay ? sav->replay->count : 0;
|
|
|
|
SECASVAR_UNLOCK(sav);
|
|
|
|
|
|
|
|
m = key_setsadbxsa2(sav->sah->saidx.mode, replay_count,
|
2002-10-16 02:10:08 +00:00
|
|
|
sav->sah->saidx.reqid);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
2016-11-25 14:44:49 +00:00
|
|
|
if (sav->replay && sav->replay->wsize > UINT8_MAX) {
|
|
|
|
m = key_setsadbxsareplay(sav->replay->wsize);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/* create lifetime extension (current and soft) */
|
|
|
|
len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
|
|
|
if (m == NULL) {
|
2002-10-16 02:10:08 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-03-15 10:20:15 +00:00
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
2002-10-16 02:10:08 +00:00
|
|
|
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;
|
2017-02-06 08:49:57 +00:00
|
|
|
lt->sadb_lifetime_allocations =
|
|
|
|
(uint32_t)counter_u64_fetch(sav->lft_c_allocations);
|
|
|
|
lt->sadb_lifetime_bytes =
|
|
|
|
counter_u64_fetch(sav->lft_c_bytes);
|
|
|
|
lt->sadb_lifetime_addtime = sav->created;
|
|
|
|
lt->sadb_lifetime_usetime = sav->firstused;
|
2002-10-16 02:10:08 +00:00
|
|
|
lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
|
2008-03-02 17:12:28 +00:00
|
|
|
lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
|
2015-05-19 08:30:04 +00:00
|
|
|
if (hard) {
|
|
|
|
lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
|
|
|
|
lt->sadb_lifetime_allocations = sav->lft_h->allocations;
|
|
|
|
lt->sadb_lifetime_bytes = sav->lft_h->bytes;
|
|
|
|
lt->sadb_lifetime_addtime = sav->lft_h->addtime;
|
|
|
|
lt->sadb_lifetime_usetime = sav->lft_h->usetime;
|
|
|
|
} else {
|
|
|
|
lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
|
|
|
|
lt->sadb_lifetime_allocations = sav->lft_s->allocations;
|
|
|
|
lt->sadb_lifetime_bytes = sav->lft_s->bytes;
|
|
|
|
lt->sadb_lifetime_addtime = sav->lft_s->addtime;
|
|
|
|
lt->sadb_lifetime_usetime = sav->lft_s->usetime;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
/* set sadb_address for source */
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
|
|
|
|
&sav->sah->saidx.src.sa,
|
|
|
|
FULLMASK, IPSEC_ULPROTO_ANY);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
|
|
|
/* set sadb_address for destination */
|
|
|
|
m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
|
|
|
|
&sav->sah->saidx.dst.sa,
|
|
|
|
FULLMASK, IPSEC_ULPROTO_ANY);
|
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
m_cat(result, m);
|
|
|
|
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
/*
|
|
|
|
* XXX-BZ Handle NAT-T extensions here.
|
2017-02-06 08:49:57 +00:00
|
|
|
* XXXAE: it doesn't seem quite useful. IKEs should not depend on
|
|
|
|
* this information, we report only significant SA fields.
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
*/
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (result)
|
|
|
|
m_freem(result);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
static void
|
|
|
|
key_freesah_flushed(struct secashead_queue *flushq)
|
|
|
|
{
|
|
|
|
struct secashead *sah, *nextsah;
|
|
|
|
struct secasvar *sav, *nextsav;
|
|
|
|
|
|
|
|
sah = TAILQ_FIRST(flushq);
|
|
|
|
while (sah != NULL) {
|
|
|
|
sav = TAILQ_FIRST(&sah->savtree_larval);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = TAILQ_NEXT(sav, chain);
|
|
|
|
TAILQ_REMOVE(&sah->savtree_larval, sav, chain);
|
|
|
|
key_freesav(&sav); /* release last reference */
|
|
|
|
key_freesah(&sah); /* release reference from SAV */
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
sav = TAILQ_FIRST(&sah->savtree_alive);
|
|
|
|
while (sav != NULL) {
|
|
|
|
nextsav = TAILQ_NEXT(sav, chain);
|
|
|
|
TAILQ_REMOVE(&sah->savtree_alive, sav, chain);
|
|
|
|
key_freesav(&sav); /* release last reference */
|
|
|
|
key_freesah(&sah); /* release reference from SAV */
|
|
|
|
sav = nextsav;
|
|
|
|
}
|
|
|
|
nextsah = TAILQ_NEXT(sah, chain);
|
|
|
|
key_freesah(&sah); /* release last reference */
|
|
|
|
sah = nextsah;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_flush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secashead_queue flushq;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
struct secashead *sah, *nextsah;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secasvar *sav;
|
|
|
|
uint8_t proto;
|
|
|
|
int i;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
|
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
KEYDBG(KEY_STAMP,
|
|
|
|
printf("%s: proto %u\n", __func__, proto));
|
|
|
|
|
|
|
|
TAILQ_INIT(&flushq);
|
|
|
|
if (proto == IPSEC_PROTO_ANY) {
|
|
|
|
/* no SATYPE specified, i.e. flushing all SA. */
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
/* Move all SAHs into flushq */
|
|
|
|
TAILQ_CONCAT(&flushq, &V_sahtree, chain);
|
|
|
|
/* Flush all buckets in SPI hash */
|
|
|
|
for (i = 0; i < V_savhash_mask + 1; i++)
|
|
|
|
LIST_INIT(&V_savhashtbl[i]);
|
|
|
|
/* Flush all buckets in SAHADDRHASH */
|
|
|
|
for (i = 0; i < V_sahaddrhash_mask + 1; i++)
|
|
|
|
LIST_INIT(&V_sahaddrhashtbl[i]);
|
|
|
|
/* Mark all SAHs as unlinked */
|
|
|
|
TAILQ_FOREACH(sah, &flushq, chain) {
|
|
|
|
sah->state = SADB_SASTATE_DEAD;
|
|
|
|
/*
|
|
|
|
* Callout handler makes its job using
|
|
|
|
* RLOCK and drain queues. In case, when this
|
|
|
|
* function will be called just before it
|
|
|
|
* acquires WLOCK, we need to mark SAs as
|
|
|
|
* unlinked to prevent second unlink.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
}
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
} else {
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
sah = TAILQ_FIRST(&V_sahtree);
|
|
|
|
while (sah != NULL) {
|
|
|
|
IPSEC_ASSERT(sah->state != SADB_SASTATE_DEAD,
|
|
|
|
("DEAD SAH %p in SADB_FLUSH", sah));
|
|
|
|
nextsah = TAILQ_NEXT(sah, chain);
|
|
|
|
if (sah->saidx.proto != proto) {
|
|
|
|
sah = nextsah;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sah->state = SADB_SASTATE_DEAD;
|
|
|
|
TAILQ_REMOVE(&V_sahtree, sah, chain);
|
|
|
|
LIST_REMOVE(sah, addrhash);
|
|
|
|
/* Unlink all SAs from SPI hash */
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
}
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
|
|
|
|
LIST_REMOVE(sav, spihash);
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
}
|
|
|
|
/* Add SAH into flushq */
|
|
|
|
TAILQ_INSERT_HEAD(&flushq, sah, chain);
|
|
|
|
sah = nextsah;
|
|
|
|
}
|
|
|
|
SAHTREE_WUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesah_flushed(&flushq);
|
|
|
|
/* Free all queued SAs and SAHs */
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m->m_len < sizeof(struct sadb_msg) ||
|
|
|
|
sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
|
2002-10-16 02:10:08 +00: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 = 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_DUMP processing
|
|
|
|
* dump all entries including status of DEAD in SAD.
|
|
|
|
* receive
|
|
|
|
* <base>
|
|
|
|
* from the ikmpd, and dump all secasvar leaves
|
|
|
|
* and send,
|
|
|
|
* <base> .....
|
|
|
|
* to the ikmpd.
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_dump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK_TRACKER;
|
2002-10-16 02:10:08 +00:00
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
|
|
|
struct sadb_msg *newmsg;
|
|
|
|
struct mbuf *n;
|
2017-02-06 08:49:57 +00:00
|
|
|
uint32_t cnt;
|
|
|
|
uint8_t proto, satype;
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* map satype to proto */
|
|
|
|
if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
|
2017-02-06 08:49:57 +00:00
|
|
|
__func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count sav entries to be sent to the userland. */
|
|
|
|
cnt = 0;
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RLOCK();
|
|
|
|
TAILQ_FOREACH(sah, &V_sahtree, chain) {
|
|
|
|
if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
|
|
|
|
proto != sah->saidx.proto)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_larval, chain)
|
|
|
|
cnt++;
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_alive, chain)
|
|
|
|
cnt++;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
if (cnt == 0) {
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, ENOENT);
|
2003-09-01 05:35:55 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* send this to the userland, one at a time. */
|
|
|
|
newmsg = NULL;
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_FOREACH(sah, &V_sahtree, chain) {
|
|
|
|
if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
|
|
|
|
proto != sah->saidx.proto)
|
2002-10-16 02:10:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* map proto to satype */
|
|
|
|
if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RUNLOCK();
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
|
2017-02-06 08:49:57 +00:00
|
|
|
"SAD.\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
|
|
|
|
n = key_setdumpsa(sav, SADB_DUMP, satype,
|
|
|
|
--cnt, mhp->msg->sadb_msg_pid);
|
|
|
|
if (n == NULL) {
|
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
|
|
|
}
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
|
|
|
|
n = key_setdumpsa(sav, SADB_DUMP, satype,
|
|
|
|
--cnt, mhp->msg->sadb_msg_pid);
|
|
|
|
if (n == NULL) {
|
|
|
|
SAHTREE_RUNLOCK();
|
|
|
|
return key_senderror(so, m, ENOBUFS);
|
|
|
|
}
|
|
|
|
key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_RUNLOCK();
|
2002-10-16 02:10:08 +00:00
|
|
|
m_freem(m);
|
2017-02-06 08:49:57 +00:00
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* SADB_X_PROMISC processing
|
|
|
|
*
|
|
|
|
* m will always be freed.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_promisc(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
int olen;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
|
|
|
|
|
|
|
|
if (olen < sizeof(struct sadb_msg)) {
|
|
|
|
#if 1
|
|
|
|
return key_senderror(so, m, EINVAL);
|
|
|
|
#else
|
|
|
|
m_freem(m);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
} else if (olen == sizeof(struct sadb_msg)) {
|
|
|
|
/* enable/disable promisc mode */
|
|
|
|
struct keycb *kp;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the original message back to everyone */
|
|
|
|
mhp->msg->sadb_msg_errno = 0;
|
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
|
|
|
|
} else {
|
|
|
|
/* send packet as is */
|
|
|
|
|
|
|
|
m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
|
|
|
|
|
|
|
|
/* TODO: if sadb_msg_seq is specified, send to specific pid */
|
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 04:08:41 +00:00
|
|
|
static int (*key_typesw[])(struct socket *, struct mbuf *,
|
|
|
|
const struct sadb_msghdr *) = {
|
2002-10-16 02:10:08 +00:00
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2014-12-03 03:50:54 +00:00
|
|
|
key_parse(struct mbuf *m, struct socket *so)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
struct sadb_msghdr mh;
|
|
|
|
u_int orglen;
|
|
|
|
int error;
|
|
|
|
int target;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(so != NULL, ("null socket"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
2002-10-16 02:10:08 +00: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 *);
|
|
|
|
orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
|
|
|
|
target = KEY_SENDUP_ONE;
|
|
|
|
|
2016-02-24 12:28:49 +00:00
|
|
|
if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len != orglen) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invlen);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->sadb_msg_version != PF_KEY_V2) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
|
|
|
|
__func__, msg->sadb_msg_version));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invver);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->sadb_msg_type > SADB_MAX) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
|
|
|
|
__func__, msg->sadb_msg_type));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invmsgtype);
|
2002-10-16 02:10:08 +00: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;
|
|
|
|
|
2012-12-05 08:04:20 +00:00
|
|
|
MGETHDR(n, M_NOWAIT, MT_DATA);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (n && m->m_pkthdr.len > MHLEN) {
|
2015-01-06 12:59:37 +00:00
|
|
|
if (!(MCLGET(n, M_NOWAIT))) {
|
2002-10-16 02:10:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* align the mbuf chain so that extensions are in contiguous region. */
|
|
|
|
error = key_align(m, &mh);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
msg = mh.msg;
|
|
|
|
|
2017-03-07 00:13:53 +00:00
|
|
|
/* We use satype as scope mask for spddump */
|
|
|
|
if (msg->sadb_msg_type == SADB_X_SPDDUMP) {
|
|
|
|
switch (msg->sadb_msg_satype) {
|
|
|
|
case IPSEC_POLICYSCOPE_ANY:
|
|
|
|
case IPSEC_POLICYSCOPE_GLOBAL:
|
|
|
|
case IPSEC_POLICYSCOPE_IFNET:
|
|
|
|
case IPSEC_POLICYSCOPE_PCB:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
|
|
|
|
__func__, msg->sadb_msg_type));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invsatype);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
2017-03-07 00:13:53 +00:00
|
|
|
} else {
|
|
|
|
switch (msg->sadb_msg_satype) { /* check SA type */
|
|
|
|
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:
|
|
|
|
ipseclog((LOG_DEBUG, "%s: must specify satype "
|
|
|
|
"when msg type=%u.\n", __func__,
|
|
|
|
msg->sadb_msg_type));
|
|
|
|
PFKEYSTAT_INC(out_invsatype);
|
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SADB_SATYPE_AH:
|
|
|
|
case SADB_SATYPE_ESP:
|
|
|
|
case SADB_X_SATYPE_IPCOMP:
|
|
|
|
case SADB_X_SATYPE_TCPSIGNATURE:
|
|
|
|
switch (msg->sadb_msg_type) {
|
|
|
|
case SADB_X_SPDADD:
|
|
|
|
case SADB_X_SPDDELETE:
|
|
|
|
case SADB_X_SPDGET:
|
|
|
|
case SADB_X_SPDFLUSH:
|
|
|
|
case SADB_X_SPDSETIDX:
|
|
|
|
case SADB_X_SPDUPDATE:
|
|
|
|
case SADB_X_SPDDELETE2:
|
|
|
|
ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
|
|
|
|
__func__, msg->sadb_msg_type));
|
|
|
|
PFKEYSTAT_INC(out_invsatype);
|
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SADB_SATYPE_RSVP:
|
|
|
|
case SADB_SATYPE_OSPFV2:
|
|
|
|
case SADB_SATYPE_RIPV2:
|
|
|
|
case SADB_SATYPE_MIP:
|
|
|
|
ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
|
|
|
|
__func__, msg->sadb_msg_satype));
|
|
|
|
PFKEYSTAT_INC(out_invsatype);
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
goto senderror;
|
|
|
|
case 1: /* XXX: What does it do? */
|
|
|
|
if (msg->sadb_msg_type == SADB_X_PROMISC)
|
|
|
|
break;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
default:
|
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
|
|
|
|
__func__, msg->sadb_msg_satype));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invsatype);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check field of upper layer protocol and address family */
|
|
|
|
if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
|
|
|
|
&& mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
|
|
|
|
struct sadb_address *src0, *dst0;
|
|
|
|
u_int plen;
|
|
|
|
|
|
|
|
src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
|
|
|
|
dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
|
|
|
|
|
|
|
|
/* check upper layer protocol */
|
|
|
|
if (src0->sadb_address_proto != dst0->sadb_address_proto) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: upper layer protocol "
|
|
|
|
"mismatched.\n", __func__));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invaddr);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check family */
|
|
|
|
if (PFKEY_ADDR_SADDR(src0)->sa_family !=
|
|
|
|
PFKEY_ADDR_SADDR(dst0)->sa_family) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
|
|
|
|
__func__));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invaddr);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
if (PFKEY_ADDR_SADDR(src0)->sa_len !=
|
|
|
|
PFKEY_ADDR_SADDR(dst0)->sa_len) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: address struct size "
|
|
|
|
"mismatched.\n", __func__));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invaddr);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (PFKEY_ADDR_SADDR(src0)->sa_len !=
|
|
|
|
sizeof(struct sockaddr_in)) {
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invaddr);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (PFKEY_ADDR_SADDR(src0)->sa_len !=
|
|
|
|
sizeof(struct sockaddr_in6)) {
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invaddr);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
|
|
|
|
__func__));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invaddr);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EAFNOSUPPORT;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
plen = sizeof(struct in_addr) << 3;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
plen = sizeof(struct in6_addr) << 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
plen = 0; /*fool gcc*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check max prefix length */
|
|
|
|
if (src0->sadb_address_prefixlen > plen ||
|
|
|
|
dst0->sadb_address_prefixlen > plen) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
|
|
|
|
__func__));
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invaddr);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prefixlen == 0 is valid because there can be a case when
|
|
|
|
* all addresses are matched.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2016-04-19 23:48:27 +00:00
|
|
|
if (msg->sadb_msg_type >= nitems(key_typesw) ||
|
2002-10-16 02:10:08 +00:00
|
|
|
key_typesw[msg->sadb_msg_type] == NULL) {
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invmsgtype);
|
2002-10-16 02:10:08 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto senderror;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
|
|
|
|
|
|
|
|
senderror:
|
|
|
|
msg->sadb_msg_errno = error;
|
|
|
|
return key_sendup_mbuf(so, m, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_senderror(struct socket *so, struct mbuf *m, int code)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
|
|
|
|
("mbuf too small, len %u", m->m_len));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
msg = mtod(m, struct sadb_msg *);
|
|
|
|
msg->sadb_msg_errno = code;
|
|
|
|
return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set the pointer to each header into message buffer.
|
|
|
|
* m will be freed on error.
|
|
|
|
* XXX larger-than-MCLBYTES extension?
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_align(struct mbuf *m, struct sadb_msghdr *mhp)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
struct sadb_ext *ext;
|
|
|
|
size_t off, end;
|
|
|
|
int extlen;
|
|
|
|
int toff;
|
|
|
|
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(m != NULL, ("null mbuf"));
|
|
|
|
IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
|
|
|
|
IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
|
|
|
|
("mbuf too small, len %u", m->m_len));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* initialize */
|
|
|
|
bzero(mhp, sizeof(*mhp));
|
|
|
|
|
|
|
|
mhp->msg = mtod(m, struct sadb_msg *);
|
|
|
|
mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* 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:
|
|
|
|
case SADB_EXT_LIFETIME_CURRENT:
|
|
|
|
case SADB_EXT_LIFETIME_HARD:
|
|
|
|
case SADB_EXT_LIFETIME_SOFT:
|
|
|
|
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:
|
|
|
|
case SADB_X_EXT_SA2:
|
Added support for NAT-Traversal (RFC 3948) in IPsec stack.
Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...
X-MFC: never
Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
2009-06-12 15:44:35 +00:00
|
|
|
case SADB_X_EXT_NAT_T_TYPE:
|
|
|
|
case SADB_X_EXT_NAT_T_SPORT:
|
|
|
|
case SADB_X_EXT_NAT_T_DPORT:
|
|
|
|
case SADB_X_EXT_NAT_T_OAI:
|
|
|
|
case SADB_X_EXT_NAT_T_OAR:
|
|
|
|
case SADB_X_EXT_NAT_T_FRAG:
|
2016-11-25 14:44:49 +00:00
|
|
|
case SADB_X_EXT_SA_REPLAY:
|
2017-02-06 08:49:57 +00:00
|
|
|
case SADB_X_EXT_NEW_ADDRESS_SRC:
|
|
|
|
case SADB_X_EXT_NEW_ADDRESS_DST:
|
2002-10-16 02:10:08 +00:00
|
|
|
/* duplicate check */
|
|
|
|
/*
|
|
|
|
* XXX Are there duplication payloads of either
|
|
|
|
* KEY_AUTH or KEY_ENCRYPT ?
|
|
|
|
*/
|
|
|
|
if (mhp->ext[ext->sadb_ext_type] != NULL) {
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
|
|
|
|
"%u\n", __func__, ext->sadb_ext_type));
|
2002-10-16 02:10:08 +00:00
|
|
|
m_freem(m);
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_dupext);
|
2002-10-16 02:10:08 +00:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2003-09-29 22:57:43 +00:00
|
|
|
ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
|
|
|
|
__func__, ext->sadb_ext_type));
|
2002-10-16 02:10:08 +00:00
|
|
|
m_freem(m);
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invexttype);
|
2002-10-16 02:10:08 +00:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
|
|
|
|
|
|
|
|
if (key_validate_ext(ext, extlen)) {
|
|
|
|
m_freem(m);
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invlen);
|
2002-10-16 02:10:08 +00:00
|
|
|
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);
|
2013-06-20 11:44:16 +00:00
|
|
|
PFKEYSTAT_INC(out_invlen);
|
2002-10-16 02:10:08 +00:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-12-03 03:50:54 +00:00
|
|
|
key_validate_ext(const struct sadb_ext *ext, int len)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
const struct sockaddr *sa;
|
|
|
|
enum { NONE, ADDR } checktype = NONE;
|
|
|
|
int baselen = 0;
|
|
|
|
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 */
|
2016-04-19 23:48:27 +00:00
|
|
|
if (ext->sadb_ext_type >= nitems(minsize) ||
|
|
|
|
ext->sadb_ext_type >= nitems(maxsize))
|
2002-10-16 02:10:08 +00:00
|
|
|
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:
|
2017-02-06 08:49:57 +00:00
|
|
|
case SADB_X_EXT_NAT_T_OAI:
|
|
|
|
case SADB_X_EXT_NAT_T_OAR:
|
|
|
|
case SADB_X_EXT_NEW_ADDRESS_SRC:
|
|
|
|
case SADB_X_EXT_NEW_ADDRESS_DST:
|
2002-10-16 02:10:08 +00:00
|
|
|
baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
|
|
|
|
checktype = ADDR;
|
|
|
|
break;
|
|
|
|
case SADB_EXT_IDENTITY_SRC:
|
|
|
|
case SADB_EXT_IDENTITY_DST:
|
|
|
|
if (((const 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 = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
|
|
|
|
if (len < baselen + sal)
|
|
|
|
return EINVAL;
|
|
|
|
if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
|
|
|
|
return EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-06-29 00:49:50 +00:00
|
|
|
key_init(void)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
for (i = 0; i < IPSEC_DIR_MAX; i++) {
|
2014-12-24 18:34:56 +00:00
|
|
|
TAILQ_INIT(&V_sptree[i]);
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_INIT(&V_sptree_ifnet[i]);
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
V_key_lft_zone = uma_zcreate("IPsec SA lft_c",
|
|
|
|
sizeof(uint64_t) * 2, NULL, NULL, NULL, NULL,
|
|
|
|
UMA_ALIGN_PTR, UMA_ZONE_PCPU);
|
|
|
|
|
|
|
|
TAILQ_INIT(&V_sahtree);
|
|
|
|
V_sphashtbl = hashinit(SPHASH_NHASH, M_IPSEC_SP, &V_sphash_mask);
|
|
|
|
V_savhashtbl = hashinit(SAVHASH_NHASH, M_IPSEC_SA, &V_savhash_mask);
|
|
|
|
V_sahaddrhashtbl = hashinit(SAHHASH_NHASH, M_IPSEC_SAH,
|
|
|
|
&V_sahaddrhash_mask);
|
|
|
|
V_acqaddrhashtbl = hashinit(ACQHASH_NHASH, M_IPSEC_SAQ,
|
|
|
|
&V_acqaddrhash_mask);
|
|
|
|
V_acqseqhashtbl = hashinit(ACQHASH_NHASH, M_IPSEC_SAQ,
|
|
|
|
&V_acqseqhash_mask);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2003-09-01 05:35:55 +00:00
|
|
|
for (i = 0; i <= SADB_SATYPE_MAX; i++)
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
LIST_INIT(&V_regtree[i]);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
LIST_INIT(&V_acqtree);
|
|
|
|
LIST_INIT(&V_spacqtree);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
First pass at separating per-vnet initializer functions
from existing functions for initializing global state.
At this stage, the new per-vnet initializer functions are
directly called from the existing global initialization code,
which should in most cases result in compiler inlining those
new functions, hence yielding a near-zero functional change.
Modify the existing initializer functions which are invoked via
protosw, like ip_init() et. al., to allow them to be invoked
multiple times, i.e. per each vnet. Global state, if any,
is initialized only if such functions are called within the
context of vnet0, which will be determined via the
IS_DEFAULT_VNET(curvnet) check (currently always true).
While here, V_irtualize a few remaining global UMA zones
used by net/netinet/netipsec networking code. While it is
not yet clear to me or anybody else whether this is the right
thing to do, at this stage this makes the code more readable,
and makes it easier to track uncollected UMA-zone-backed
objects on vnet removal. In the long run, it's quite possible
that some form of shared use of UMA zone pools among multiple
vnets should be considered.
Bump __FreeBSD_version due to changes in layout of structs
vnet_ipfw, vnet_inet and vnet_net.
Approved by: julian (mentor)
2009-04-06 22:29:41 +00:00
|
|
|
if (!IS_DEFAULT_VNET(curvnet))
|
|
|
|
return;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
XFORMS_LOCK_INIT();
|
First pass at separating per-vnet initializer functions
from existing functions for initializing global state.
At this stage, the new per-vnet initializer functions are
directly called from the existing global initialization code,
which should in most cases result in compiler inlining those
new functions, hence yielding a near-zero functional change.
Modify the existing initializer functions which are invoked via
protosw, like ip_init() et. al., to allow them to be invoked
multiple times, i.e. per each vnet. Global state, if any,
is initialized only if such functions are called within the
context of vnet0, which will be determined via the
IS_DEFAULT_VNET(curvnet) check (currently always true).
While here, V_irtualize a few remaining global UMA zones
used by net/netinet/netipsec networking code. While it is
not yet clear to me or anybody else whether this is the right
thing to do, at this stage this makes the code more readable,
and makes it easier to track uncollected UMA-zone-backed
objects on vnet removal. In the long run, it's quite possible
that some form of shared use of UMA zone pools among multiple
vnets should be considered.
Bump __FreeBSD_version due to changes in layout of structs
vnet_ipfw, vnet_inet and vnet_net.
Approved by: julian (mentor)
2009-04-06 22:29:41 +00:00
|
|
|
SPTREE_LOCK_INIT();
|
|
|
|
REGTREE_LOCK_INIT();
|
|
|
|
SAHTREE_LOCK_INIT();
|
|
|
|
ACQ_LOCK_INIT();
|
|
|
|
SPACQ_LOCK_INIT();
|
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
#ifndef IPSEC_DEBUG2
|
2015-05-22 17:05:21 +00:00
|
|
|
callout_init(&key_timer, 1);
|
2014-10-23 20:43:16 +00:00
|
|
|
callout_reset(&key_timer, hz, key_timehandler, NULL);
|
2002-10-16 02:10:08 +00:00
|
|
|
#endif /*IPSEC_DEBUG2*/
|
|
|
|
|
|
|
|
/* initialize key statistics */
|
|
|
|
keystat.getspi_count = 1;
|
|
|
|
|
2016-03-13 19:27:46 +00:00
|
|
|
if (bootverbose)
|
|
|
|
printf("IPsec: Initialized Security Association Processing.\n");
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef VIMAGE
|
|
|
|
void
|
|
|
|
key_destroy(void)
|
|
|
|
{
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secashead_queue sahdrainq;
|
|
|
|
struct secpolicy_queue drainq;
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
struct secpolicy *sp, *nextsp;
|
2010-03-28 06:51:50 +00:00
|
|
|
struct secacq *acq, *nextacq;
|
|
|
|
struct secspacq *spacq, *nextspacq;
|
2017-02-06 08:49:57 +00:00
|
|
|
struct secashead *sah;
|
|
|
|
struct secasvar *sav;
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
struct secreg *reg;
|
|
|
|
int i;
|
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
/*
|
|
|
|
* XXX: can we just call free() for each object without
|
|
|
|
* walking through safe way with releasing references?
|
|
|
|
*/
|
2014-12-24 18:34:56 +00:00
|
|
|
TAILQ_INIT(&drainq);
|
|
|
|
SPTREE_WLOCK();
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
for (i = 0; i < IPSEC_DIR_MAX; i++) {
|
2014-12-25 13:38:51 +00:00
|
|
|
TAILQ_CONCAT(&drainq, &V_sptree[i], chain);
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_CONCAT(&drainq, &V_sptree_ifnet[i], chain);
|
2014-12-24 18:34:56 +00:00
|
|
|
}
|
|
|
|
SPTREE_WUNLOCK();
|
|
|
|
sp = TAILQ_FIRST(&drainq);
|
|
|
|
while (sp != NULL) {
|
|
|
|
nextsp = TAILQ_NEXT(sp, chain);
|
2017-02-06 08:49:57 +00:00
|
|
|
key_freesp(&sp);
|
2014-12-24 18:34:56 +00:00
|
|
|
sp = nextsp;
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
2017-02-06 08:49:57 +00:00
|
|
|
TAILQ_INIT(&sahdrainq);
|
|
|
|
SAHTREE_WLOCK();
|
|
|
|
TAILQ_CONCAT(&sahdrainq, &V_sahtree, chain);
|
|
|
|
for (i = 0; i < V_savhash_mask + 1; i++)
|
|
|
|
LIST_INIT(&V_savhashtbl[i]);
|
|
|
|
for (i = 0; i < V_sahaddrhash_mask + 1; i++)
|
|
|
|
LIST_INIT(&V_sahaddrhashtbl[i]);
|
|
|
|
TAILQ_FOREACH(sah, &sahdrainq, chain) {
|
|
|
|
sah->state = SADB_SASTATE_DEAD;
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
|
|
|
}
|
|
|
|
TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
|
|
|
|
sav->state = SADB_SASTATE_DEAD;
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
SAHTREE_WUNLOCK();
|
|
|
|
|
|
|
|
key_freesah_flushed(&sahdrainq);
|
|
|
|
hashdestroy(V_sphashtbl, M_IPSEC_SP, V_sphash_mask);
|
|
|
|
hashdestroy(V_savhashtbl, M_IPSEC_SA, V_savhash_mask);
|
|
|
|
hashdestroy(V_sahaddrhashtbl, M_IPSEC_SAH, V_sahaddrhash_mask);
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
|
|
|
|
REGTREE_LOCK();
|
|
|
|
for (i = 0; i <= SADB_SATYPE_MAX; i++) {
|
|
|
|
LIST_FOREACH(reg, &V_regtree[i], chain) {
|
|
|
|
if (__LIST_CHAINED(reg)) {
|
|
|
|
LIST_REMOVE(reg, chain);
|
|
|
|
free(reg, M_IPSEC_SAR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
REGTREE_UNLOCK();
|
|
|
|
|
|
|
|
ACQ_LOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
acq = LIST_FIRST(&V_acqtree);
|
|
|
|
while (acq != NULL) {
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
nextacq = LIST_NEXT(acq, chain);
|
2017-02-06 08:49:57 +00:00
|
|
|
LIST_REMOVE(acq, chain);
|
|
|
|
free(acq, M_IPSEC_SAQ);
|
|
|
|
acq = nextacq;
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
}
|
|
|
|
ACQ_UNLOCK();
|
|
|
|
|
|
|
|
SPACQ_LOCK();
|
2010-03-28 06:51:50 +00:00
|
|
|
for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
|
|
|
|
spacq = nextspacq) {
|
|
|
|
nextspacq = LIST_NEXT(spacq, chain);
|
|
|
|
if (__LIST_CHAINED(spacq)) {
|
|
|
|
LIST_REMOVE(spacq, chain);
|
|
|
|
free(spacq, M_IPSEC_SAQ);
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SPACQ_UNLOCK();
|
2017-02-06 08:49:57 +00:00
|
|
|
hashdestroy(V_acqaddrhashtbl, M_IPSEC_SAQ, V_acqaddrhash_mask);
|
|
|
|
hashdestroy(V_acqseqhashtbl, M_IPSEC_SAQ, V_acqseqhash_mask);
|
|
|
|
uma_zdestroy(V_key_lft_zone);
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
Introduce an infrastructure for dismantling vnet instances.
Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.
While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.
Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.
Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
2009-06-08 17:15:40 +00:00
|
|
|
#endif
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* record data transfer on SA, and update timestamps */
|
|
|
|
void
|
2014-12-03 03:50:54 +00:00
|
|
|
key_sa_recordxfer(struct secasvar *sav, struct mbuf *m)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
|
|
|
|
IPSEC_ASSERT(m != NULL, ("Null mbuf"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX Currently, there is a difference of bytes size
|
|
|
|
* between inbound and outbound processing.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
counter_u64_add(sav->lft_c_bytes, m->m_pkthdr.len);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use the number of packets as the unit of
|
2006-03-25 13:38:52 +00:00
|
|
|
* allocations. We increment the variable
|
2002-10-16 02:10:08 +00:00
|
|
|
* whenever {esp,ah}_{in,out}put is called.
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
counter_u64_add(sav->lft_c_allocations, 1);
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/*
|
2006-03-25 13:38:52 +00:00
|
|
|
* NOTE: We record CURRENT usetime by using wall clock,
|
2002-10-16 02:10:08 +00:00
|
|
|
* in seconds. HARD and SOFT lifetime are measured by the time
|
2006-03-25 13:38:52 +00:00
|
|
|
* difference (again in seconds) from usetime.
|
2002-10-16 02:10:08 +00:00
|
|
|
*
|
|
|
|
* usetime
|
|
|
|
* v expire expire
|
|
|
|
* -----+-----+--------+---> t
|
|
|
|
* <--------------> HARD
|
|
|
|
* <-----> SOFT
|
|
|
|
*/
|
2017-02-06 08:49:57 +00:00
|
|
|
if (sav->firstused == 0)
|
|
|
|
sav->firstused = time_second;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
|
2006-03-25 13:38:52 +00:00
|
|
|
/*
|
|
|
|
* Take one of the kernel's security keys and convert it into a PF_KEY
|
|
|
|
* structure within an mbuf, suitable for sending up to a waiting
|
|
|
|
* application in user land.
|
|
|
|
*
|
|
|
|
* IN:
|
|
|
|
* src: A pointer to a kernel security key.
|
|
|
|
* exttype: Which type of key this is. Refer to the PF_KEY data structures.
|
|
|
|
* OUT:
|
|
|
|
* a valid mbuf or NULL indicating an error
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct mbuf *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_setkey(struct seckey *src, uint16_t exttype)
|
2006-03-25 13:38:52 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct sadb_key *p;
|
2007-06-15 22:35:59 +00:00
|
|
|
int len;
|
2006-03-25 13:38:52 +00:00
|
|
|
|
|
|
|
if (src == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2007-06-15 22:35:59 +00:00
|
|
|
len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
2006-03-25 13:38:52 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
2013-03-15 10:20:15 +00:00
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
2006-03-25 13:38:52 +00:00
|
|
|
p = mtod(m, struct sadb_key *);
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_key_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_key_exttype = exttype;
|
|
|
|
p->sadb_key_bits = src->bits;
|
|
|
|
bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take one of the kernel's lifetime data structures and convert it
|
|
|
|
* into a PF_KEY structure within an mbuf, suitable for sending up to
|
|
|
|
* a waiting application in user land.
|
|
|
|
*
|
|
|
|
* IN:
|
|
|
|
* src: A pointer to a kernel lifetime structure.
|
|
|
|
* exttype: Which type of lifetime this is. Refer to the PF_KEY
|
|
|
|
* data structures for more information.
|
|
|
|
* OUT:
|
|
|
|
* a valid mbuf or NULL indicating an error
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct mbuf *
|
2017-02-06 08:49:57 +00:00
|
|
|
key_setlifetime(struct seclifetime *src, uint16_t exttype)
|
2006-03-25 13:38:52 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m = NULL;
|
|
|
|
struct sadb_lifetime *p;
|
|
|
|
int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
|
|
|
|
|
|
|
|
if (src == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2013-03-15 10:20:15 +00:00
|
|
|
m = m_get2(len, M_NOWAIT, MT_DATA, 0);
|
2006-03-25 13:38:52 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return m;
|
2013-03-15 10:20:15 +00:00
|
|
|
m_align(m, len);
|
|
|
|
m->m_len = len;
|
2006-03-25 13:38:52 +00:00
|
|
|
p = mtod(m, struct sadb_lifetime *);
|
|
|
|
|
|
|
|
bzero(p, len);
|
|
|
|
p->sadb_lifetime_len = PFKEY_UNIT64(len);
|
|
|
|
p->sadb_lifetime_exttype = exttype;
|
|
|
|
p->sadb_lifetime_allocations = src->allocations;
|
|
|
|
p->sadb_lifetime_bytes = src->bytes;
|
|
|
|
p->sadb_lifetime_addtime = src->addtime;
|
|
|
|
p->sadb_lifetime_usetime = src->usetime;
|
|
|
|
|
|
|
|
return m;
|
|
|
|
|
|
|
|
}
|
2017-02-06 08:49:57 +00:00
|
|
|
|
|
|
|
const struct enc_xform *
|
|
|
|
enc_algorithm_lookup(int alg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems(supported_ealgs); i++)
|
|
|
|
if (alg == supported_ealgs[i].sadb_alg)
|
|
|
|
return (supported_ealgs[i].xform);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct auth_hash *
|
|
|
|
auth_algorithm_lookup(int alg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems(supported_aalgs); i++)
|
|
|
|
if (alg == supported_aalgs[i].sadb_alg)
|
|
|
|
return (supported_aalgs[i].xform);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct comp_algo *
|
|
|
|
comp_algorithm_lookup(int alg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems(supported_calgs); i++)
|
|
|
|
if (alg == supported_calgs[i].sadb_alg)
|
|
|
|
return (supported_calgs[i].xform);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a transform.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
xform_register(struct xformsw* xsp)
|
|
|
|
{
|
|
|
|
struct xformsw *entry;
|
|
|
|
|
|
|
|
XFORMS_LOCK();
|
|
|
|
LIST_FOREACH(entry, &xforms, chain) {
|
|
|
|
if (entry->xf_type == xsp->xf_type) {
|
|
|
|
XFORMS_UNLOCK();
|
|
|
|
return (EEXIST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LIST_INSERT_HEAD(&xforms, xsp, chain);
|
|
|
|
XFORMS_UNLOCK();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xform_attach(void *data)
|
|
|
|
{
|
|
|
|
struct xformsw *xsp = (struct xformsw *)data;
|
|
|
|
|
|
|
|
if (xform_register(xsp) != 0)
|
|
|
|
printf("%s: failed to register %s xform\n", __func__,
|
|
|
|
xsp->xf_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xform_detach(void *data)
|
|
|
|
{
|
|
|
|
struct xformsw *xsp = (struct xformsw *)data;
|
|
|
|
|
|
|
|
XFORMS_LOCK();
|
|
|
|
LIST_REMOVE(xsp, chain);
|
|
|
|
XFORMS_UNLOCK();
|
|
|
|
|
|
|
|
/* Delete all SAs related to this xform. */
|
|
|
|
key_delete_xform(xsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize transform support in an sav.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
xform_init(struct secasvar *sav, u_short xftype)
|
|
|
|
{
|
|
|
|
struct xformsw *entry;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
IPSEC_ASSERT(sav->tdb_xform == NULL,
|
|
|
|
("tdb_xform is already initialized"));
|
|
|
|
|
|
|
|
ret = EINVAL;
|
|
|
|
XFORMS_LOCK();
|
|
|
|
LIST_FOREACH(entry, &xforms, chain) {
|
|
|
|
if (entry->xf_type == xftype) {
|
|
|
|
ret = (*entry->xf_init)(sav, entry);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XFORMS_UNLOCK();
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|