2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* xfrm_state.c
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
* Mitsuru KANDA @USAGI
|
|
|
|
* Kazunori MIYAZAWA @USAGI
|
|
|
|
* Kunihiro Ishiguro <kunihiro@ipinfusion.com>
|
|
|
|
* IPv6 support
|
|
|
|
* YOSHIFUJI Hideaki @USAGI
|
|
|
|
* Split up af-specific functions
|
|
|
|
* Derek Atkins <derek@ihtfp.com>
|
|
|
|
* Add UDP Encapsulation
|
[LSM-IPSec]: Security association restriction.
This patch series implements per packet access control via the
extension of the Linux Security Modules (LSM) interface by hooks in
the XFRM and pfkey subsystems that leverage IPSec security
associations to label packets. Extensions to the SELinux LSM are
included that leverage the patch for this purpose.
This patch implements the changes necessary to the XFRM subsystem,
pfkey interface, ipv4/ipv6, and xfrm_user interface to restrict a
socket to use only authorized security associations (or no security
association) to send/receive network packets.
Patch purpose:
The patch is designed to enable access control per packets based on
the strongly authenticated IPSec security association. Such access
controls augment the existing ones based on network interface and IP
address. The former are very coarse-grained, and the latter can be
spoofed. By using IPSec, the system can control access to remote
hosts based on cryptographic keys generated using the IPSec mechanism.
This enables access control on a per-machine basis or per-application
if the remote machine is running the same mechanism and trusted to
enforce the access control policy.
Patch design approach:
The overall approach is that policy (xfrm_policy) entries set by
user-level programs (e.g., setkey for ipsec-tools) are extended with a
security context that is used at policy selection time in the XFRM
subsystem to restrict the sockets that can send/receive packets via
security associations (xfrm_states) that are built from those
policies.
A presentation available at
www.selinux-symposium.org/2005/presentations/session2/2-3-jaeger.pdf
from the SELinux symposium describes the overall approach.
Patch implementation details:
On output, the policy retrieved (via xfrm_policy_lookup or
xfrm_sk_policy_lookup) must be authorized for the security context of
the socket and the same security context is required for resultant
security association (retrieved or negotiated via racoon in
ipsec-tools). This is enforced in xfrm_state_find.
On input, the policy retrieved must also be authorized for the socket
(at __xfrm_policy_check), and the security context of the policy must
also match the security association being used.
The patch has virtually no impact on packets that do not use IPSec.
The existing Netfilter (outgoing) and LSM rcv_skb hooks are used as
before.
Also, if IPSec is used without security contexts, the impact is
minimal. The LSM must allow such policies to be selected for the
combination of socket and remote machine, but subsequent IPSec
processing proceeds as in the original case.
Testing:
The pfkey interface is tested using the ipsec-tools. ipsec-tools have
been modified (a separate ipsec-tools patch is available for version
0.5) that supports assignment of xfrm_policy entries and security
associations with security contexts via setkey and the negotiation
using the security contexts via racoon.
The xfrm_user interface is tested via ad hoc programs that set
security contexts. These programs are also available from me, and
contain programs for setting, getting, and deleting policy for testing
this interface. Testing of sa functions was done by tracing kernel
behavior.
Signed-off-by: Trent Jaeger <tjaeger@cse.psu.edu>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2005-12-14 15:12:27 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <net/xfrm.h>
|
|
|
|
#include <linux/pfkeyv2.h>
|
|
|
|
#include <linux/ipsec.h>
|
|
|
|
#include <linux/module.h>
|
2006-08-24 18:08:07 +08:00
|
|
|
#include <linux/cache.h>
|
2007-12-21 12:49:33 +08:00
|
|
|
#include <linux/audit.h>
|
2007-08-11 06:20:21 +08:00
|
|
|
#include <asm/uaccess.h>
|
2009-11-09 12:58:41 +08:00
|
|
|
#include <linux/ktime.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2009-11-09 12:58:41 +08:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/kernel.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-08-24 19:50:50 +08:00
|
|
|
#include "xfrm_hash.h"
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Each xfrm_state may be linked to two tables:
|
|
|
|
|
|
|
|
1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
|
2006-08-24 18:24:33 +08:00
|
|
|
2. Hash table by (daddr,family,reqid) to find what SAs exist for given
|
2005-04-17 06:20:36 +08:00
|
|
|
destination/tunnel endpoint. (output)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(xfrm_state_lock);
|
|
|
|
|
2006-08-24 18:08:07 +08:00
|
|
|
static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
|
|
|
|
|
2007-10-18 12:33:12 +08:00
|
|
|
static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
|
|
|
|
static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
|
|
|
|
|
2007-12-22 06:58:11 +08:00
|
|
|
#ifdef CONFIG_AUDITSYSCALL
|
|
|
|
static void xfrm_audit_state_replay(struct xfrm_state *x,
|
|
|
|
struct sk_buff *skb, __be32 net_seq);
|
|
|
|
#else
|
|
|
|
#define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
|
|
|
|
#endif /* CONFIG_AUDITSYSCALL */
|
|
|
|
|
2008-11-26 09:29:21 +08:00
|
|
|
static inline unsigned int xfrm_dst_hash(struct net *net,
|
|
|
|
xfrm_address_t *daddr,
|
2006-08-24 19:00:03 +08:00
|
|
|
xfrm_address_t *saddr,
|
|
|
|
u32 reqid,
|
2006-08-24 18:24:33 +08:00
|
|
|
unsigned short family)
|
2006-08-24 18:08:07 +08:00
|
|
|
{
|
2008-11-26 09:29:21 +08:00
|
|
|
return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
|
2006-08-24 18:08:07 +08:00
|
|
|
}
|
|
|
|
|
2008-11-26 09:29:21 +08:00
|
|
|
static inline unsigned int xfrm_src_hash(struct net *net,
|
|
|
|
xfrm_address_t *daddr,
|
2006-10-04 06:56:09 +08:00
|
|
|
xfrm_address_t *saddr,
|
2006-08-24 19:50:50 +08:00
|
|
|
unsigned short family)
|
2006-08-24 18:08:07 +08:00
|
|
|
{
|
2008-11-26 09:29:21 +08:00
|
|
|
return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
|
2006-08-24 18:08:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int
|
2008-11-26 09:29:21 +08:00
|
|
|
xfrm_spi_hash(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
|
2006-08-24 18:08:07 +08:00
|
|
|
{
|
2008-11-26 09:29:21 +08:00
|
|
|
return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
|
2006-08-24 18:08:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xfrm_hash_transfer(struct hlist_head *list,
|
|
|
|
struct hlist_head *ndsttable,
|
|
|
|
struct hlist_head *nsrctable,
|
|
|
|
struct hlist_head *nspitable,
|
|
|
|
unsigned int nhashmask)
|
|
|
|
{
|
|
|
|
struct hlist_node *entry, *tmp;
|
|
|
|
struct xfrm_state *x;
|
|
|
|
|
|
|
|
hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
|
|
|
|
unsigned int h;
|
|
|
|
|
2006-08-24 19:00:03 +08:00
|
|
|
h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
|
|
|
|
x->props.reqid, x->props.family,
|
|
|
|
nhashmask);
|
2006-08-24 18:08:07 +08:00
|
|
|
hlist_add_head(&x->bydst, ndsttable+h);
|
|
|
|
|
2006-10-04 06:56:09 +08:00
|
|
|
h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
|
|
|
|
x->props.family,
|
2006-08-24 18:08:07 +08:00
|
|
|
nhashmask);
|
|
|
|
hlist_add_head(&x->bysrc, nsrctable+h);
|
|
|
|
|
2006-09-28 13:21:52 +08:00
|
|
|
if (x->id.spi) {
|
|
|
|
h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
|
|
|
|
x->id.proto, x->props.family,
|
|
|
|
nhashmask);
|
|
|
|
hlist_add_head(&x->byspi, nspitable+h);
|
|
|
|
}
|
2006-08-24 18:08:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-26 09:19:07 +08:00
|
|
|
static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
|
2006-08-24 18:08:07 +08:00
|
|
|
{
|
2008-11-26 09:19:07 +08:00
|
|
|
return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
|
2006-08-24 18:08:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static DEFINE_MUTEX(hash_resize_mutex);
|
|
|
|
|
2008-11-26 09:19:07 +08:00
|
|
|
static void xfrm_hash_resize(struct work_struct *work)
|
2006-08-24 18:08:07 +08:00
|
|
|
{
|
2008-11-26 09:19:07 +08:00
|
|
|
struct net *net = container_of(work, struct net, xfrm.state_hash_work);
|
2006-08-24 18:08:07 +08:00
|
|
|
struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
|
|
|
|
unsigned long nsize, osize;
|
|
|
|
unsigned int nhashmask, ohashmask;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mutex_lock(&hash_resize_mutex);
|
|
|
|
|
2008-11-26 09:19:07 +08:00
|
|
|
nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
|
2006-08-24 19:50:50 +08:00
|
|
|
ndst = xfrm_hash_alloc(nsize);
|
2006-08-24 18:08:07 +08:00
|
|
|
if (!ndst)
|
|
|
|
goto out_unlock;
|
2006-08-24 19:50:50 +08:00
|
|
|
nsrc = xfrm_hash_alloc(nsize);
|
2006-08-24 18:08:07 +08:00
|
|
|
if (!nsrc) {
|
2006-08-24 19:50:50 +08:00
|
|
|
xfrm_hash_free(ndst, nsize);
|
2006-08-24 18:08:07 +08:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
2006-08-24 19:50:50 +08:00
|
|
|
nspi = xfrm_hash_alloc(nsize);
|
2006-08-24 18:08:07 +08:00
|
|
|
if (!nspi) {
|
2006-08-24 19:50:50 +08:00
|
|
|
xfrm_hash_free(ndst, nsize);
|
|
|
|
xfrm_hash_free(nsrc, nsize);
|
2006-08-24 18:08:07 +08:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
|
|
|
|
|
|
|
nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
|
2008-11-26 09:19:07 +08:00
|
|
|
for (i = net->xfrm.state_hmask; i >= 0; i--)
|
|
|
|
xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
|
2006-08-24 18:08:07 +08:00
|
|
|
nhashmask);
|
|
|
|
|
2008-11-26 09:19:07 +08:00
|
|
|
odst = net->xfrm.state_bydst;
|
|
|
|
osrc = net->xfrm.state_bysrc;
|
|
|
|
ospi = net->xfrm.state_byspi;
|
|
|
|
ohashmask = net->xfrm.state_hmask;
|
2006-08-24 18:08:07 +08:00
|
|
|
|
2008-11-26 09:19:07 +08:00
|
|
|
net->xfrm.state_bydst = ndst;
|
|
|
|
net->xfrm.state_bysrc = nsrc;
|
|
|
|
net->xfrm.state_byspi = nspi;
|
|
|
|
net->xfrm.state_hmask = nhashmask;
|
2006-08-24 18:08:07 +08:00
|
|
|
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
|
|
|
|
osize = (ohashmask + 1) * sizeof(struct hlist_head);
|
2006-08-24 19:50:50 +08:00
|
|
|
xfrm_hash_free(odst, osize);
|
|
|
|
xfrm_hash_free(osrc, osize);
|
|
|
|
xfrm_hash_free(ospi, osize);
|
2006-08-24 18:08:07 +08:00
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&hash_resize_mutex);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
|
|
|
|
static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(xfrm_state_gc_lock);
|
|
|
|
|
2006-03-21 11:17:03 +08:00
|
|
|
int __xfrm_state_delete(struct xfrm_state *x);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
[IPSEC]: Sync series - acquire insert
This introduces a feature similar to the one described in RFC 2367:
"
... the application needing an SA sends a PF_KEY
SADB_ACQUIRE message down to the Key Engine, which then either
returns an error or sends a similar SADB_ACQUIRE message up to one or
more key management applications capable of creating such SAs.
...
...
The third is where an application-layer consumer of security
associations (e.g. an OSPFv2 or RIPv2 daemon) needs a security
association.
Send an SADB_ACQUIRE message from a user process to the kernel.
<base, address(SD), (address(P),) (identity(SD),) (sensitivity,)
proposal>
The kernel returns an SADB_ACQUIRE message to registered
sockets.
<base, address(SD), (address(P),) (identity(SD),) (sensitivity,)
proposal>
The user-level consumer waits for an SADB_UPDATE or SADB_ADD
message for its particular type, and then can use that
association by using SADB_GET messages.
"
An app such as OSPF could then use ipsec KM to get keys
Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 11:16:40 +08:00
|
|
|
int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
|
2006-03-21 11:17:03 +08:00
|
|
|
void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-18 12:31:12 +08:00
|
|
|
static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
|
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo;
|
|
|
|
if (unlikely(family >= NPROTO))
|
|
|
|
return NULL;
|
|
|
|
write_lock_bh(&xfrm_state_afinfo_lock);
|
|
|
|
afinfo = xfrm_state_afinfo[family];
|
|
|
|
if (unlikely(!afinfo))
|
|
|
|
write_unlock_bh(&xfrm_state_afinfo_lock);
|
|
|
|
return afinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
|
2008-01-02 13:58:02 +08:00
|
|
|
__releases(xfrm_state_afinfo_lock)
|
2007-10-18 12:31:12 +08:00
|
|
|
{
|
|
|
|
write_unlock_bh(&xfrm_state_afinfo_lock);
|
|
|
|
}
|
|
|
|
|
2008-01-31 11:11:50 +08:00
|
|
|
int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
|
2007-10-18 12:31:12 +08:00
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
|
2008-01-31 11:11:50 +08:00
|
|
|
const struct xfrm_type **typemap;
|
2007-10-18 12:31:12 +08:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (unlikely(afinfo == NULL))
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
typemap = afinfo->type_map;
|
|
|
|
|
|
|
|
if (likely(typemap[type->proto] == NULL))
|
|
|
|
typemap[type->proto] = type;
|
|
|
|
else
|
|
|
|
err = -EEXIST;
|
|
|
|
xfrm_state_unlock_afinfo(afinfo);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_register_type);
|
|
|
|
|
2008-01-31 11:11:50 +08:00
|
|
|
int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
|
2007-10-18 12:31:12 +08:00
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
|
2008-01-31 11:11:50 +08:00
|
|
|
const struct xfrm_type **typemap;
|
2007-10-18 12:31:12 +08:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (unlikely(afinfo == NULL))
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
typemap = afinfo->type_map;
|
|
|
|
|
|
|
|
if (unlikely(typemap[type->proto] != type))
|
|
|
|
err = -ENOENT;
|
|
|
|
else
|
|
|
|
typemap[type->proto] = NULL;
|
|
|
|
xfrm_state_unlock_afinfo(afinfo);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_unregister_type);
|
|
|
|
|
2008-01-31 11:11:50 +08:00
|
|
|
static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
|
2007-10-18 12:31:12 +08:00
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo;
|
2008-01-31 11:11:50 +08:00
|
|
|
const struct xfrm_type **typemap;
|
|
|
|
const struct xfrm_type *type;
|
2007-10-18 12:31:12 +08:00
|
|
|
int modload_attempted = 0;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
afinfo = xfrm_state_get_afinfo(family);
|
|
|
|
if (unlikely(afinfo == NULL))
|
|
|
|
return NULL;
|
|
|
|
typemap = afinfo->type_map;
|
|
|
|
|
|
|
|
type = typemap[proto];
|
|
|
|
if (unlikely(type && !try_module_get(type->owner)))
|
|
|
|
type = NULL;
|
|
|
|
if (!type && !modload_attempted) {
|
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
request_module("xfrm-type-%d-%d", family, proto);
|
|
|
|
modload_attempted = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2008-01-31 11:11:50 +08:00
|
|
|
static void xfrm_put_type(const struct xfrm_type *type)
|
2007-10-18 12:31:12 +08:00
|
|
|
{
|
|
|
|
module_put(type->owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xfrm_register_mode(struct xfrm_mode *mode, int family)
|
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo;
|
|
|
|
struct xfrm_mode **modemap;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (unlikely(mode->encap >= XFRM_MODE_MAX))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
afinfo = xfrm_state_lock_afinfo(family);
|
|
|
|
if (unlikely(afinfo == NULL))
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
|
|
|
|
err = -EEXIST;
|
|
|
|
modemap = afinfo->mode_map;
|
2007-10-18 12:33:12 +08:00
|
|
|
if (modemap[mode->encap])
|
|
|
|
goto out;
|
2007-10-18 12:31:12 +08:00
|
|
|
|
2007-10-18 12:33:12 +08:00
|
|
|
err = -ENOENT;
|
|
|
|
if (!try_module_get(afinfo->owner))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
mode->afinfo = afinfo;
|
|
|
|
modemap[mode->encap] = mode;
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
out:
|
2007-10-18 12:31:12 +08:00
|
|
|
xfrm_state_unlock_afinfo(afinfo);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_register_mode);
|
|
|
|
|
|
|
|
int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
|
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo;
|
|
|
|
struct xfrm_mode **modemap;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (unlikely(mode->encap >= XFRM_MODE_MAX))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
afinfo = xfrm_state_lock_afinfo(family);
|
|
|
|
if (unlikely(afinfo == NULL))
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
|
|
|
|
err = -ENOENT;
|
|
|
|
modemap = afinfo->mode_map;
|
|
|
|
if (likely(modemap[mode->encap] == mode)) {
|
|
|
|
modemap[mode->encap] = NULL;
|
2007-10-18 12:33:12 +08:00
|
|
|
module_put(mode->afinfo->owner);
|
2007-10-18 12:31:12 +08:00
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfrm_state_unlock_afinfo(afinfo);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_unregister_mode);
|
|
|
|
|
|
|
|
static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
|
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo;
|
|
|
|
struct xfrm_mode *mode;
|
|
|
|
int modload_attempted = 0;
|
|
|
|
|
|
|
|
if (unlikely(encap >= XFRM_MODE_MAX))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
afinfo = xfrm_state_get_afinfo(family);
|
|
|
|
if (unlikely(afinfo == NULL))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mode = afinfo->mode_map[encap];
|
|
|
|
if (unlikely(mode && !try_module_get(mode->owner)))
|
|
|
|
mode = NULL;
|
|
|
|
if (!mode && !modload_attempted) {
|
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
request_module("xfrm-mode-%d-%d", family, encap);
|
|
|
|
modload_attempted = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xfrm_put_mode(struct xfrm_mode *mode)
|
|
|
|
{
|
|
|
|
module_put(mode->owner);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static void xfrm_state_gc_destroy(struct xfrm_state *x)
|
|
|
|
{
|
2009-11-09 12:58:41 +08:00
|
|
|
tasklet_hrtimer_cancel(&x->mtimer);
|
2006-08-24 18:54:22 +08:00
|
|
|
del_timer_sync(&x->rtimer);
|
2005-11-09 01:41:34 +08:00
|
|
|
kfree(x->aalg);
|
|
|
|
kfree(x->ealg);
|
|
|
|
kfree(x->calg);
|
|
|
|
kfree(x->encap);
|
2006-08-24 09:18:55 +08:00
|
|
|
kfree(x->coaddr);
|
2007-10-18 12:35:51 +08:00
|
|
|
if (x->inner_mode)
|
|
|
|
xfrm_put_mode(x->inner_mode);
|
2008-03-25 05:51:51 +08:00
|
|
|
if (x->inner_mode_iaf)
|
|
|
|
xfrm_put_mode(x->inner_mode_iaf);
|
2007-10-18 12:35:51 +08:00
|
|
|
if (x->outer_mode)
|
|
|
|
xfrm_put_mode(x->outer_mode);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x->type) {
|
|
|
|
x->type->destructor(x);
|
|
|
|
xfrm_put_type(x->type);
|
|
|
|
}
|
[LSM-IPSec]: Security association restriction.
This patch series implements per packet access control via the
extension of the Linux Security Modules (LSM) interface by hooks in
the XFRM and pfkey subsystems that leverage IPSec security
associations to label packets. Extensions to the SELinux LSM are
included that leverage the patch for this purpose.
This patch implements the changes necessary to the XFRM subsystem,
pfkey interface, ipv4/ipv6, and xfrm_user interface to restrict a
socket to use only authorized security associations (or no security
association) to send/receive network packets.
Patch purpose:
The patch is designed to enable access control per packets based on
the strongly authenticated IPSec security association. Such access
controls augment the existing ones based on network interface and IP
address. The former are very coarse-grained, and the latter can be
spoofed. By using IPSec, the system can control access to remote
hosts based on cryptographic keys generated using the IPSec mechanism.
This enables access control on a per-machine basis or per-application
if the remote machine is running the same mechanism and trusted to
enforce the access control policy.
Patch design approach:
The overall approach is that policy (xfrm_policy) entries set by
user-level programs (e.g., setkey for ipsec-tools) are extended with a
security context that is used at policy selection time in the XFRM
subsystem to restrict the sockets that can send/receive packets via
security associations (xfrm_states) that are built from those
policies.
A presentation available at
www.selinux-symposium.org/2005/presentations/session2/2-3-jaeger.pdf
from the SELinux symposium describes the overall approach.
Patch implementation details:
On output, the policy retrieved (via xfrm_policy_lookup or
xfrm_sk_policy_lookup) must be authorized for the security context of
the socket and the same security context is required for resultant
security association (retrieved or negotiated via racoon in
ipsec-tools). This is enforced in xfrm_state_find.
On input, the policy retrieved must also be authorized for the socket
(at __xfrm_policy_check), and the security context of the policy must
also match the security association being used.
The patch has virtually no impact on packets that do not use IPSec.
The existing Netfilter (outgoing) and LSM rcv_skb hooks are used as
before.
Also, if IPSec is used without security contexts, the impact is
minimal. The LSM must allow such policies to be selected for the
combination of socket and remote machine, but subsequent IPSec
processing proceeds as in the original case.
Testing:
The pfkey interface is tested using the ipsec-tools. ipsec-tools have
been modified (a separate ipsec-tools patch is available for version
0.5) that supports assignment of xfrm_policy entries and security
associations with security contexts via setkey and the negotiation
using the security contexts via racoon.
The xfrm_user interface is tested via ad hoc programs that set
security contexts. These programs are also available from me, and
contain programs for setting, getting, and deleting policy for testing
this interface. Testing of sa functions was done by tracing kernel
behavior.
Signed-off-by: Trent Jaeger <tjaeger@cse.psu.edu>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2005-12-14 15:12:27 +08:00
|
|
|
security_xfrm_state_free(x);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(x);
|
|
|
|
}
|
|
|
|
|
2008-11-26 09:20:36 +08:00
|
|
|
static void xfrm_state_gc_task(struct work_struct *work)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-11-26 09:20:36 +08:00
|
|
|
struct net *net = container_of(work, struct net, xfrm.state_gc_work);
|
2008-10-01 22:03:24 +08:00
|
|
|
struct xfrm_state *x;
|
|
|
|
struct hlist_node *entry, *tmp;
|
|
|
|
struct hlist_head gc_list;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_gc_lock);
|
2008-11-26 09:20:36 +08:00
|
|
|
hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_gc_lock);
|
|
|
|
|
2008-10-01 22:03:24 +08:00
|
|
|
hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
|
2005-04-17 06:20:36 +08:00
|
|
|
xfrm_state_gc_destroy(x);
|
2006-08-24 17:45:07 +08:00
|
|
|
|
2008-11-26 09:21:01 +08:00
|
|
|
wake_up(&net->xfrm.km_waitq);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long make_jiffies(long secs)
|
|
|
|
{
|
|
|
|
if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
|
|
|
|
return MAX_SCHEDULE_TIMEOUT-1;
|
|
|
|
else
|
2007-02-09 22:25:29 +08:00
|
|
|
return secs*HZ;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-11-09 12:58:41 +08:00
|
|
|
static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2009-11-09 12:58:41 +08:00
|
|
|
struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
|
|
|
|
struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
|
2008-11-26 09:29:47 +08:00
|
|
|
struct net *net = xs_net(x);
|
2007-03-05 08:12:44 +08:00
|
|
|
unsigned long now = get_seconds();
|
2005-04-17 06:20:36 +08:00
|
|
|
long next = LONG_MAX;
|
|
|
|
int warn = 0;
|
2006-11-28 03:11:54 +08:00
|
|
|
int err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
spin_lock(&x->lock);
|
|
|
|
if (x->km.state == XFRM_STATE_DEAD)
|
|
|
|
goto out;
|
|
|
|
if (x->km.state == XFRM_STATE_EXPIRED)
|
|
|
|
goto expired;
|
|
|
|
if (x->lft.hard_add_expires_seconds) {
|
|
|
|
long tmo = x->lft.hard_add_expires_seconds +
|
|
|
|
x->curlft.add_time - now;
|
|
|
|
if (tmo <= 0)
|
|
|
|
goto expired;
|
|
|
|
if (tmo < next)
|
|
|
|
next = tmo;
|
|
|
|
}
|
|
|
|
if (x->lft.hard_use_expires_seconds) {
|
|
|
|
long tmo = x->lft.hard_use_expires_seconds +
|
|
|
|
(x->curlft.use_time ? : now) - now;
|
|
|
|
if (tmo <= 0)
|
|
|
|
goto expired;
|
|
|
|
if (tmo < next)
|
|
|
|
next = tmo;
|
|
|
|
}
|
|
|
|
if (x->km.dying)
|
|
|
|
goto resched;
|
|
|
|
if (x->lft.soft_add_expires_seconds) {
|
|
|
|
long tmo = x->lft.soft_add_expires_seconds +
|
|
|
|
x->curlft.add_time - now;
|
|
|
|
if (tmo <= 0)
|
|
|
|
warn = 1;
|
|
|
|
else if (tmo < next)
|
|
|
|
next = tmo;
|
|
|
|
}
|
|
|
|
if (x->lft.soft_use_expires_seconds) {
|
|
|
|
long tmo = x->lft.soft_use_expires_seconds +
|
|
|
|
(x->curlft.use_time ? : now) - now;
|
|
|
|
if (tmo <= 0)
|
|
|
|
warn = 1;
|
|
|
|
else if (tmo < next)
|
|
|
|
next = tmo;
|
|
|
|
}
|
|
|
|
|
2005-06-19 13:43:22 +08:00
|
|
|
x->km.dying = warn;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (warn)
|
2006-03-21 11:17:03 +08:00
|
|
|
km_state_expired(x, 0, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
resched:
|
2009-11-09 12:58:41 +08:00
|
|
|
if (next != LONG_MAX){
|
|
|
|
tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
|
|
|
|
}
|
2006-08-24 18:54:22 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
expired:
|
|
|
|
if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
|
|
|
|
x->km.state = XFRM_STATE_EXPIRED;
|
2008-11-26 09:29:47 +08:00
|
|
|
wake_up(&net->xfrm.km_waitq);
|
2005-04-17 06:20:36 +08:00
|
|
|
next = 2;
|
|
|
|
goto resched;
|
|
|
|
}
|
2006-11-28 03:11:54 +08:00
|
|
|
|
|
|
|
err = __xfrm_state_delete(x);
|
|
|
|
if (!err && x->id.spi)
|
2006-03-21 11:17:03 +08:00
|
|
|
km_state_expired(x, 1, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-09-18 02:51:22 +08:00
|
|
|
xfrm_audit_state_delete(x, err ? 0 : 1,
|
2008-04-18 22:09:25 +08:00
|
|
|
audit_get_loginuid(current),
|
|
|
|
audit_get_sessionid(current), 0);
|
2006-11-28 03:11:54 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
out:
|
|
|
|
spin_unlock(&x->lock);
|
2009-11-09 12:58:41 +08:00
|
|
|
return HRTIMER_NORESTART;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-03-21 11:18:23 +08:00
|
|
|
static void xfrm_replay_timer_handler(unsigned long data);
|
|
|
|
|
2008-11-26 09:15:16 +08:00
|
|
|
struct xfrm_state *xfrm_state_alloc(struct net *net)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct xfrm_state *x;
|
|
|
|
|
2006-07-22 05:51:30 +08:00
|
|
|
x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (x) {
|
2008-11-26 09:15:16 +08:00
|
|
|
write_pnet(&x->xs_net, net);
|
2005-04-17 06:20:36 +08:00
|
|
|
atomic_set(&x->refcnt, 1);
|
|
|
|
atomic_set(&x->tunnel_users, 0);
|
2008-10-01 22:03:24 +08:00
|
|
|
INIT_LIST_HEAD(&x->km.all);
|
2006-08-24 17:45:07 +08:00
|
|
|
INIT_HLIST_NODE(&x->bydst);
|
|
|
|
INIT_HLIST_NODE(&x->bysrc);
|
|
|
|
INIT_HLIST_NODE(&x->byspi);
|
2009-11-09 12:58:41 +08:00
|
|
|
tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
|
2008-01-24 13:20:07 +08:00
|
|
|
setup_timer(&x->rtimer, xfrm_replay_timer_handler,
|
|
|
|
(unsigned long)x);
|
2007-03-05 08:12:44 +08:00
|
|
|
x->curlft.add_time = get_seconds();
|
2005-04-17 06:20:36 +08:00
|
|
|
x->lft.soft_byte_limit = XFRM_INF;
|
|
|
|
x->lft.soft_packet_limit = XFRM_INF;
|
|
|
|
x->lft.hard_byte_limit = XFRM_INF;
|
|
|
|
x->lft.hard_packet_limit = XFRM_INF;
|
2006-03-21 11:15:11 +08:00
|
|
|
x->replay_maxage = 0;
|
|
|
|
x->replay_maxdiff = 0;
|
2008-03-25 05:51:51 +08:00
|
|
|
x->inner_mode = NULL;
|
|
|
|
x->inner_mode_iaf = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_init(&x->lock);
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_alloc);
|
|
|
|
|
|
|
|
void __xfrm_state_destroy(struct xfrm_state *x)
|
|
|
|
{
|
2008-11-26 09:29:47 +08:00
|
|
|
struct net *net = xs_net(x);
|
|
|
|
|
2008-07-26 12:43:18 +08:00
|
|
|
WARN_ON(x->km.state != XFRM_STATE_DEAD);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_gc_lock);
|
2008-11-26 09:29:47 +08:00
|
|
|
hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_gc_lock);
|
2008-11-26 09:29:47 +08:00
|
|
|
schedule_work(&net->xfrm.state_gc_work);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__xfrm_state_destroy);
|
|
|
|
|
2006-03-21 11:17:03 +08:00
|
|
|
int __xfrm_state_delete(struct xfrm_state *x)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-11-26 09:29:47 +08:00
|
|
|
struct net *net = xs_net(x);
|
2005-06-19 13:42:13 +08:00
|
|
|
int err = -ESRCH;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x->km.state != XFRM_STATE_DEAD) {
|
|
|
|
x->km.state = XFRM_STATE_DEAD;
|
|
|
|
spin_lock(&xfrm_state_lock);
|
2008-10-01 22:03:24 +08:00
|
|
|
list_del(&x->km.all);
|
2006-08-24 17:45:07 +08:00
|
|
|
hlist_del(&x->bydst);
|
|
|
|
hlist_del(&x->bysrc);
|
2006-08-24 18:54:22 +08:00
|
|
|
if (x->id.spi)
|
2006-08-24 17:45:07 +08:00
|
|
|
hlist_del(&x->byspi);
|
2008-11-26 09:29:47 +08:00
|
|
|
net->xfrm.state_num--;
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock(&xfrm_state_lock);
|
|
|
|
|
|
|
|
/* All xfrm_state objects are created by xfrm_state_alloc.
|
|
|
|
* The xfrm_state_alloc call gives a reference, and that
|
|
|
|
* is what we are dropping here.
|
|
|
|
*/
|
2007-11-27 11:10:07 +08:00
|
|
|
xfrm_state_put(x);
|
2005-06-19 13:42:13 +08:00
|
|
|
err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-06-19 13:42:13 +08:00
|
|
|
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-03-21 11:17:03 +08:00
|
|
|
EXPORT_SYMBOL(__xfrm_state_delete);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-06-19 13:42:13 +08:00
|
|
|
int xfrm_state_delete(struct xfrm_state *x)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-06-19 13:42:13 +08:00
|
|
|
int err;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_bh(&x->lock);
|
2005-06-19 13:42:13 +08:00
|
|
|
err = __xfrm_state_delete(x);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_bh(&x->lock);
|
2005-06-19 13:42:13 +08:00
|
|
|
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_delete);
|
|
|
|
|
2007-06-05 07:05:57 +08:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK_XFRM
|
|
|
|
static inline int
|
2008-11-26 09:30:18 +08:00
|
|
|
xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-06-05 07:05:57 +08:00
|
|
|
int i, err = 0;
|
|
|
|
|
2008-11-26 09:30:18 +08:00
|
|
|
for (i = 0; i <= net->xfrm.state_hmask; i++) {
|
2007-06-05 07:05:57 +08:00
|
|
|
struct hlist_node *entry;
|
|
|
|
struct xfrm_state *x;
|
|
|
|
|
2008-11-26 09:30:18 +08:00
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
|
2007-06-05 07:05:57 +08:00
|
|
|
if (xfrm_id_proto_match(x->id.proto, proto) &&
|
|
|
|
(err = security_xfrm_state_delete(x)) != 0) {
|
2007-09-18 02:51:22 +08:00
|
|
|
xfrm_audit_state_delete(x, 0,
|
|
|
|
audit_info->loginuid,
|
2008-04-18 22:09:25 +08:00
|
|
|
audit_info->sessionid,
|
2007-09-18 02:51:22 +08:00
|
|
|
audit_info->secid);
|
2007-06-05 07:05:57 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline int
|
2008-11-26 09:30:18 +08:00
|
|
|
xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
|
2007-06-05 07:05:57 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-26 09:30:18 +08:00
|
|
|
int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
|
2007-06-05 07:05:57 +08:00
|
|
|
{
|
2010-02-19 10:00:41 +08:00
|
|
|
int i, err = 0, cnt = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2008-11-26 09:30:18 +08:00
|
|
|
err = xfrm_state_flush_secctx_check(net, proto, audit_info);
|
2007-06-05 07:05:57 +08:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
2010-02-19 10:00:41 +08:00
|
|
|
err = -ESRCH;
|
2008-11-26 09:30:18 +08:00
|
|
|
for (i = 0; i <= net->xfrm.state_hmask; i++) {
|
2006-08-24 17:45:07 +08:00
|
|
|
struct hlist_node *entry;
|
|
|
|
struct xfrm_state *x;
|
2005-04-17 06:20:36 +08:00
|
|
|
restart:
|
2008-11-26 09:30:18 +08:00
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!xfrm_state_kern(x) &&
|
2006-09-23 06:06:24 +08:00
|
|
|
xfrm_id_proto_match(x->id.proto, proto)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
xfrm_state_hold(x);
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
|
2006-11-28 03:11:54 +08:00
|
|
|
err = xfrm_state_delete(x);
|
2007-09-18 02:51:22 +08:00
|
|
|
xfrm_audit_state_delete(x, err ? 0 : 1,
|
|
|
|
audit_info->loginuid,
|
2008-04-18 22:09:25 +08:00
|
|
|
audit_info->sessionid,
|
2007-09-18 02:51:22 +08:00
|
|
|
audit_info->secid);
|
2005-04-17 06:20:36 +08:00
|
|
|
xfrm_state_put(x);
|
2010-02-19 10:00:41 +08:00
|
|
|
if (!err)
|
|
|
|
cnt++;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-19 10:00:41 +08:00
|
|
|
if (cnt)
|
|
|
|
err = 0;
|
2007-06-05 07:05:57 +08:00
|
|
|
|
|
|
|
out:
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
2008-11-26 09:30:18 +08:00
|
|
|
wake_up(&net->xfrm.km_waitq);
|
2007-06-05 07:05:57 +08:00
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_flush);
|
|
|
|
|
2010-01-23 21:37:10 +08:00
|
|
|
void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
|
2007-04-26 15:10:29 +08:00
|
|
|
{
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2010-01-23 21:37:10 +08:00
|
|
|
si->sadcnt = net->xfrm.state_num;
|
|
|
|
si->sadhcnt = net->xfrm.state_hmask;
|
2007-04-26 15:10:29 +08:00
|
|
|
si->sadhmcnt = xfrm_state_hashmax;
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_sad_getinfo);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int
|
2011-02-23 10:22:34 +08:00
|
|
|
xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
|
2010-09-21 02:11:38 +08:00
|
|
|
struct xfrm_tmpl *tmpl,
|
|
|
|
xfrm_address_t *daddr, xfrm_address_t *saddr,
|
|
|
|
unsigned short family)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
|
|
|
|
if (!afinfo)
|
|
|
|
return -1;
|
2010-09-21 02:11:38 +08:00
|
|
|
afinfo->init_tempsel(&x->sel, fl);
|
|
|
|
|
|
|
|
if (family != tmpl->encap_family) {
|
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
|
|
|
|
if (!afinfo)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
afinfo->init_temprop(x, tmpl, daddr, saddr);
|
2005-04-17 06:20:36 +08:00
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-23 08:20:22 +08:00
|
|
|
static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
|
2006-08-24 15:42:45 +08:00
|
|
|
{
|
2008-11-26 09:30:50 +08:00
|
|
|
unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
|
2006-08-24 15:42:45 +08:00
|
|
|
struct xfrm_state *x;
|
2006-08-24 17:45:07 +08:00
|
|
|
struct hlist_node *entry;
|
2006-08-24 15:42:45 +08:00
|
|
|
|
2008-11-26 09:30:50 +08:00
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
|
2006-08-24 15:42:45 +08:00
|
|
|
if (x->props.family != family ||
|
|
|
|
x->id.spi != spi ||
|
2009-06-29 02:42:53 +08:00
|
|
|
x->id.proto != proto ||
|
|
|
|
xfrm_addr_cmp(&x->id.daddr, daddr, family))
|
2006-08-24 15:42:45 +08:00
|
|
|
continue;
|
|
|
|
|
2010-02-22 19:32:56 +08:00
|
|
|
if ((mark & x->mark.m) != x->mark.v)
|
|
|
|
continue;
|
2006-08-24 15:42:45 +08:00
|
|
|
xfrm_state_hold(x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-23 08:20:22 +08:00
|
|
|
static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark, xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
|
2006-08-24 15:42:45 +08:00
|
|
|
{
|
2008-11-26 09:30:50 +08:00
|
|
|
unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
|
2006-08-24 15:42:45 +08:00
|
|
|
struct xfrm_state *x;
|
2006-08-24 17:45:07 +08:00
|
|
|
struct hlist_node *entry;
|
2006-08-24 15:42:45 +08:00
|
|
|
|
2008-11-26 09:30:50 +08:00
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
|
2006-08-24 15:42:45 +08:00
|
|
|
if (x->props.family != family ||
|
2009-06-29 02:42:53 +08:00
|
|
|
x->id.proto != proto ||
|
|
|
|
xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
|
|
|
|
xfrm_addr_cmp(&x->props.saddr, saddr, family))
|
2006-08-24 15:42:45 +08:00
|
|
|
continue;
|
|
|
|
|
2010-02-22 19:32:56 +08:00
|
|
|
if ((mark & x->mark.m) != x->mark.v)
|
|
|
|
continue;
|
2006-08-24 15:42:45 +08:00
|
|
|
xfrm_state_hold(x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct xfrm_state *
|
|
|
|
__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
|
|
|
|
{
|
2008-11-26 09:30:50 +08:00
|
|
|
struct net *net = xs_net(x);
|
2010-02-23 08:20:22 +08:00
|
|
|
u32 mark = x->mark.v & x->mark.m;
|
2008-11-26 09:30:50 +08:00
|
|
|
|
2006-08-24 15:42:45 +08:00
|
|
|
if (use_spi)
|
2010-02-23 08:20:22 +08:00
|
|
|
return __xfrm_state_lookup(net, mark, &x->id.daddr,
|
|
|
|
x->id.spi, x->id.proto, family);
|
2006-08-24 15:42:45 +08:00
|
|
|
else
|
2010-02-23 08:20:22 +08:00
|
|
|
return __xfrm_state_lookup_byaddr(net, mark,
|
|
|
|
&x->id.daddr,
|
2006-08-24 15:42:45 +08:00
|
|
|
&x->props.saddr,
|
|
|
|
x->id.proto, family);
|
|
|
|
}
|
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
|
2006-10-25 06:34:00 +08:00
|
|
|
{
|
|
|
|
if (have_hash_collision &&
|
2008-11-26 09:29:47 +08:00
|
|
|
(net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
|
|
|
|
net->xfrm.state_num > net->xfrm.state_hmask)
|
|
|
|
schedule_work(&net->xfrm.state_hash_work);
|
2006-10-25 06:34:00 +08:00
|
|
|
}
|
|
|
|
|
2009-03-14 05:22:40 +08:00
|
|
|
static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
|
2011-02-23 10:21:31 +08:00
|
|
|
const struct flowi *fl, unsigned short family,
|
2009-03-14 05:22:40 +08:00
|
|
|
xfrm_address_t *daddr, xfrm_address_t *saddr,
|
|
|
|
struct xfrm_state **best, int *acq_in_progress,
|
|
|
|
int *error)
|
|
|
|
{
|
|
|
|
/* Resolution logic:
|
|
|
|
* 1. There is a valid state with matching selector. Done.
|
|
|
|
* 2. Valid state with inappropriate selector. Skip.
|
|
|
|
*
|
|
|
|
* Entering area of "sysdeps".
|
|
|
|
*
|
|
|
|
* 3. If state is not valid, selector is temporary, it selects
|
|
|
|
* only session which triggered previous resolution. Key
|
|
|
|
* manager will do something to install a state with proper
|
|
|
|
* selector.
|
|
|
|
*/
|
|
|
|
if (x->km.state == XFRM_STATE_VALID) {
|
|
|
|
if ((x->sel.family &&
|
|
|
|
!xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
|
|
|
|
!security_xfrm_state_pol_flow_match(x, pol, fl))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!*best ||
|
|
|
|
(*best)->km.dying > x->km.dying ||
|
|
|
|
((*best)->km.dying == x->km.dying &&
|
|
|
|
(*best)->curlft.add_time < x->curlft.add_time))
|
|
|
|
*best = x;
|
|
|
|
} else if (x->km.state == XFRM_STATE_ACQ) {
|
|
|
|
*acq_in_progress = 1;
|
|
|
|
} else if (x->km.state == XFRM_STATE_ERROR ||
|
|
|
|
x->km.state == XFRM_STATE_EXPIRED) {
|
|
|
|
if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
|
|
|
|
security_xfrm_state_pol_flow_match(x, pol, fl))
|
|
|
|
*error = -ESRCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
struct xfrm_state *
|
2007-02-09 22:25:29 +08:00
|
|
|
xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
|
2011-02-23 10:24:19 +08:00
|
|
|
const struct flowi *fl, struct xfrm_tmpl *tmpl,
|
2005-04-17 06:20:36 +08:00
|
|
|
struct xfrm_policy *pol, int *err,
|
|
|
|
unsigned short family)
|
|
|
|
{
|
2009-03-14 05:22:40 +08:00
|
|
|
static xfrm_address_t saddr_wildcard = { };
|
2008-11-26 09:31:51 +08:00
|
|
|
struct net *net = xp_net(pol);
|
2009-04-27 17:58:59 +08:00
|
|
|
unsigned int h, h_wildcard;
|
2006-08-24 17:45:07 +08:00
|
|
|
struct hlist_node *entry;
|
2008-09-03 11:14:15 +08:00
|
|
|
struct xfrm_state *x, *x0, *to_put;
|
2005-04-17 06:20:36 +08:00
|
|
|
int acquire_in_progress = 0;
|
|
|
|
int error = 0;
|
|
|
|
struct xfrm_state *best = NULL;
|
2010-02-23 08:20:22 +08:00
|
|
|
u32 mark = pol->mark.v & pol->mark.m;
|
2010-09-21 02:11:38 +08:00
|
|
|
unsigned short encap_family = tmpl->encap_family;
|
2007-02-09 22:25:29 +08:00
|
|
|
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = NULL;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2010-09-21 02:11:38 +08:00
|
|
|
h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
|
2008-11-26 09:31:51 +08:00
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
|
2010-09-21 02:11:38 +08:00
|
|
|
if (x->props.family == encap_family &&
|
2005-04-17 06:20:36 +08:00
|
|
|
x->props.reqid == tmpl->reqid &&
|
2010-02-22 19:32:56 +08:00
|
|
|
(mark & x->mark.m) == x->mark.v &&
|
2006-08-24 09:08:21 +08:00
|
|
|
!(x->props.flags & XFRM_STATE_WILDRECV) &&
|
2010-09-21 02:11:38 +08:00
|
|
|
xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
|
2005-04-17 06:20:36 +08:00
|
|
|
tmpl->mode == x->props.mode &&
|
|
|
|
tmpl->id.proto == x->id.proto &&
|
2009-03-14 05:22:40 +08:00
|
|
|
(tmpl->id.spi == x->id.spi || !tmpl->id.spi))
|
2010-09-21 02:11:38 +08:00
|
|
|
xfrm_state_look_at(pol, x, fl, encap_family, daddr, saddr,
|
2009-03-14 05:22:40 +08:00
|
|
|
&best, &acquire_in_progress, &error);
|
|
|
|
}
|
|
|
|
if (best)
|
|
|
|
goto found;
|
|
|
|
|
2010-09-21 02:11:38 +08:00
|
|
|
h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
|
2009-04-27 17:58:59 +08:00
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
|
2010-09-21 02:11:38 +08:00
|
|
|
if (x->props.family == encap_family &&
|
2009-03-14 05:22:40 +08:00
|
|
|
x->props.reqid == tmpl->reqid &&
|
2010-02-22 19:32:56 +08:00
|
|
|
(mark & x->mark.m) == x->mark.v &&
|
2009-03-14 05:22:40 +08:00
|
|
|
!(x->props.flags & XFRM_STATE_WILDRECV) &&
|
2010-09-21 02:11:38 +08:00
|
|
|
xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
|
2009-03-14 05:22:40 +08:00
|
|
|
tmpl->mode == x->props.mode &&
|
|
|
|
tmpl->id.proto == x->id.proto &&
|
|
|
|
(tmpl->id.spi == x->id.spi || !tmpl->id.spi))
|
2010-09-21 02:11:38 +08:00
|
|
|
xfrm_state_look_at(pol, x, fl, encap_family, daddr, saddr,
|
2009-03-14 05:22:40 +08:00
|
|
|
&best, &acquire_in_progress, &error);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-03-14 05:22:40 +08:00
|
|
|
found:
|
2005-04-17 06:20:36 +08:00
|
|
|
x = best;
|
|
|
|
if (!x && !error && !acquire_in_progress) {
|
2005-04-22 11:12:32 +08:00
|
|
|
if (tmpl->id.spi &&
|
2010-02-23 08:20:22 +08:00
|
|
|
(x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
|
2010-09-21 02:11:38 +08:00
|
|
|
tmpl->id.proto, encap_family)) != NULL) {
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = x0;
|
2005-04-17 06:20:36 +08:00
|
|
|
error = -EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-11-26 09:31:51 +08:00
|
|
|
x = xfrm_state_alloc(net);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x == NULL) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-09-21 02:11:38 +08:00
|
|
|
/* Initialize temporary state matching only
|
2005-04-17 06:20:36 +08:00
|
|
|
* to current session. */
|
2010-09-21 02:11:38 +08:00
|
|
|
xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
|
2010-02-23 08:20:22 +08:00
|
|
|
memcpy(&x->mark, &pol->mark, sizeof(x->mark));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-07-25 14:29:07 +08:00
|
|
|
error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
|
|
|
|
if (error) {
|
|
|
|
x->km.state = XFRM_STATE_DEAD;
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = x;
|
2006-07-25 14:29:07 +08:00
|
|
|
x = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (km_query(x, tmpl, pol) == 0) {
|
|
|
|
x->km.state = XFRM_STATE_ACQ;
|
2008-11-26 09:31:51 +08:00
|
|
|
list_add(&x->km.all, &net->xfrm.state_all);
|
|
|
|
hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
|
2010-09-21 02:11:38 +08:00
|
|
|
h = xfrm_src_hash(net, daddr, saddr, encap_family);
|
2008-11-26 09:31:51 +08:00
|
|
|
hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x->id.spi) {
|
2010-09-21 02:11:38 +08:00
|
|
|
h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
|
2008-11-26 09:31:51 +08:00
|
|
|
hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-11-26 10:00:48 +08:00
|
|
|
x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
|
2009-11-09 12:58:41 +08:00
|
|
|
tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
|
2008-11-26 09:31:51 +08:00
|
|
|
net->xfrm.state_num++;
|
|
|
|
xfrm_hash_grow_check(net, x->bydst.next != NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
|
|
|
x->km.state = XFRM_STATE_DEAD;
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = x;
|
2005-04-17 06:20:36 +08:00
|
|
|
x = NULL;
|
|
|
|
error = -ESRCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (x)
|
|
|
|
xfrm_state_hold(x);
|
|
|
|
else
|
|
|
|
*err = acquire_in_progress ? -EAGAIN : error;
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
2008-09-03 11:14:15 +08:00
|
|
|
if (to_put)
|
|
|
|
xfrm_state_put(to_put);
|
2005-04-17 06:20:36 +08:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2007-07-03 13:41:14 +08:00
|
|
|
struct xfrm_state *
|
2010-02-23 08:20:22 +08:00
|
|
|
xfrm_stateonly_find(struct net *net, u32 mark,
|
2008-11-26 09:31:51 +08:00
|
|
|
xfrm_address_t *daddr, xfrm_address_t *saddr,
|
2007-07-03 13:41:14 +08:00
|
|
|
unsigned short family, u8 mode, u8 proto, u32 reqid)
|
|
|
|
{
|
2007-12-15 03:38:04 +08:00
|
|
|
unsigned int h;
|
2007-07-03 13:41:14 +08:00
|
|
|
struct xfrm_state *rx = NULL, *x = NULL;
|
|
|
|
struct hlist_node *entry;
|
|
|
|
|
|
|
|
spin_lock(&xfrm_state_lock);
|
2008-11-26 09:31:51 +08:00
|
|
|
h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
|
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
|
2007-07-03 13:41:14 +08:00
|
|
|
if (x->props.family == family &&
|
|
|
|
x->props.reqid == reqid &&
|
2010-02-22 19:32:56 +08:00
|
|
|
(mark & x->mark.m) == x->mark.v &&
|
2007-07-03 13:41:14 +08:00
|
|
|
!(x->props.flags & XFRM_STATE_WILDRECV) &&
|
|
|
|
xfrm_state_addr_check(x, daddr, saddr, family) &&
|
|
|
|
mode == x->props.mode &&
|
|
|
|
proto == x->id.proto &&
|
|
|
|
x->km.state == XFRM_STATE_VALID) {
|
|
|
|
rx = x;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rx)
|
|
|
|
xfrm_state_hold(rx);
|
|
|
|
spin_unlock(&xfrm_state_lock);
|
|
|
|
|
|
|
|
|
|
|
|
return rx;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_stateonly_find);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static void __xfrm_state_insert(struct xfrm_state *x)
|
|
|
|
{
|
2008-11-26 09:29:47 +08:00
|
|
|
struct net *net = xs_net(x);
|
2006-08-24 18:24:33 +08:00
|
|
|
unsigned int h;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
list_add(&x->km.all, &net->xfrm.state_all);
|
2008-02-29 13:31:08 +08:00
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
|
2006-08-24 19:00:03 +08:00
|
|
|
x->props.reqid, x->props.family);
|
2008-11-26 09:29:47 +08:00
|
|
|
hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
|
|
|
|
hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-09-28 13:21:52 +08:00
|
|
|
if (x->id.spi) {
|
2008-11-26 09:29:47 +08:00
|
|
|
h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
|
2006-08-24 08:53:57 +08:00
|
|
|
x->props.family);
|
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
|
2006-08-24 08:53:57 +08:00
|
|
|
}
|
|
|
|
|
2009-11-09 12:58:41 +08:00
|
|
|
tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
|
2006-08-24 18:54:22 +08:00
|
|
|
if (x->replay_maxage)
|
|
|
|
mod_timer(&x->rtimer, jiffies + x->replay_maxage);
|
2006-03-21 11:15:11 +08:00
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
wake_up(&net->xfrm.km_waitq);
|
2006-08-24 18:08:07 +08:00
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
net->xfrm.state_num++;
|
2006-08-24 18:08:07 +08:00
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
xfrm_hash_grow_check(net, x->bydst.next != NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-08-24 18:29:04 +08:00
|
|
|
/* xfrm_state_lock is held */
|
|
|
|
static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
|
|
|
|
{
|
2008-11-26 09:29:47 +08:00
|
|
|
struct net *net = xs_net(xnew);
|
2006-08-24 18:29:04 +08:00
|
|
|
unsigned short family = xnew->props.family;
|
|
|
|
u32 reqid = xnew->props.reqid;
|
|
|
|
struct xfrm_state *x;
|
|
|
|
struct hlist_node *entry;
|
|
|
|
unsigned int h;
|
2010-02-22 19:32:56 +08:00
|
|
|
u32 mark = xnew->mark.v & xnew->mark.m;
|
2006-08-24 18:29:04 +08:00
|
|
|
|
2008-11-26 09:29:47 +08:00
|
|
|
h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
|
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
|
2006-08-24 18:29:04 +08:00
|
|
|
if (x->props.family == family &&
|
|
|
|
x->props.reqid == reqid &&
|
2010-02-22 19:32:56 +08:00
|
|
|
(mark & x->mark.m) == x->mark.v &&
|
2006-08-24 19:00:03 +08:00
|
|
|
!xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
|
|
|
|
!xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
|
2010-03-31 09:19:49 +08:00
|
|
|
x->genid++;
|
2006-08-24 18:29:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
void xfrm_state_insert(struct xfrm_state *x)
|
|
|
|
{
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2006-08-24 18:29:04 +08:00
|
|
|
__xfrm_state_bump_genids(x);
|
2005-04-17 06:20:36 +08:00
|
|
|
__xfrm_state_insert(x);
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_insert);
|
|
|
|
|
2006-08-24 15:13:10 +08:00
|
|
|
/* xfrm_state_lock is held */
|
2010-02-23 08:20:22 +08:00
|
|
|
static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m, unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
|
2006-08-24 15:13:10 +08:00
|
|
|
{
|
2008-11-26 09:31:51 +08:00
|
|
|
unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
|
2006-08-24 17:45:07 +08:00
|
|
|
struct hlist_node *entry;
|
2006-08-24 15:13:10 +08:00
|
|
|
struct xfrm_state *x;
|
2010-02-22 19:32:56 +08:00
|
|
|
u32 mark = m->v & m->m;
|
2006-08-24 15:13:10 +08:00
|
|
|
|
2008-11-26 09:31:51 +08:00
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
|
2006-08-24 15:13:10 +08:00
|
|
|
if (x->props.reqid != reqid ||
|
|
|
|
x->props.mode != mode ||
|
|
|
|
x->props.family != family ||
|
|
|
|
x->km.state != XFRM_STATE_ACQ ||
|
2007-03-13 08:14:07 +08:00
|
|
|
x->id.spi != 0 ||
|
2009-06-29 02:42:53 +08:00
|
|
|
x->id.proto != proto ||
|
2010-02-22 19:32:56 +08:00
|
|
|
(mark & x->mark.m) != x->mark.v ||
|
2009-06-29 02:42:53 +08:00
|
|
|
xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
|
|
|
|
xfrm_addr_cmp(&x->props.saddr, saddr, family))
|
2006-08-24 15:13:10 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
xfrm_state_hold(x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!create)
|
|
|
|
return NULL;
|
|
|
|
|
2008-11-26 09:31:51 +08:00
|
|
|
x = xfrm_state_alloc(net);
|
2006-08-24 15:13:10 +08:00
|
|
|
if (likely(x)) {
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
x->sel.daddr.a4 = daddr->a4;
|
|
|
|
x->sel.saddr.a4 = saddr->a4;
|
|
|
|
x->sel.prefixlen_d = 32;
|
|
|
|
x->sel.prefixlen_s = 32;
|
|
|
|
x->props.saddr.a4 = saddr->a4;
|
|
|
|
x->id.daddr.a4 = daddr->a4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
|
|
|
|
(struct in6_addr *)daddr);
|
|
|
|
ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
|
|
|
|
(struct in6_addr *)saddr);
|
|
|
|
x->sel.prefixlen_d = 128;
|
|
|
|
x->sel.prefixlen_s = 128;
|
|
|
|
ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
|
|
|
|
(struct in6_addr *)saddr);
|
|
|
|
ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
|
|
|
|
(struct in6_addr *)daddr);
|
|
|
|
break;
|
2007-04-21 08:09:22 +08:00
|
|
|
}
|
2006-08-24 15:13:10 +08:00
|
|
|
|
|
|
|
x->km.state = XFRM_STATE_ACQ;
|
|
|
|
x->id.proto = proto;
|
|
|
|
x->props.family = family;
|
|
|
|
x->props.mode = mode;
|
|
|
|
x->props.reqid = reqid;
|
2010-02-23 08:20:22 +08:00
|
|
|
x->mark.v = m->v;
|
|
|
|
x->mark.m = m->m;
|
2008-11-26 10:00:48 +08:00
|
|
|
x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
|
2006-08-24 15:13:10 +08:00
|
|
|
xfrm_state_hold(x);
|
2009-11-09 12:58:41 +08:00
|
|
|
tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
|
2008-11-26 09:31:51 +08:00
|
|
|
list_add(&x->km.all, &net->xfrm.state_all);
|
|
|
|
hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
|
|
|
|
h = xfrm_src_hash(net, daddr, saddr, family);
|
|
|
|
hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
|
2006-10-13 13:03:24 +08:00
|
|
|
|
2008-11-26 09:31:51 +08:00
|
|
|
net->xfrm.state_num++;
|
2006-10-13 13:03:24 +08:00
|
|
|
|
2008-11-26 09:31:51 +08:00
|
|
|
xfrm_hash_grow_check(net, x->bydst.next != NULL);
|
2006-08-24 15:13:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2010-02-23 08:20:22 +08:00
|
|
|
static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
int xfrm_state_add(struct xfrm_state *x)
|
|
|
|
{
|
2008-11-26 09:31:51 +08:00
|
|
|
struct net *net = xs_net(x);
|
2008-09-03 11:14:15 +08:00
|
|
|
struct xfrm_state *x1, *to_put;
|
2005-04-17 06:20:36 +08:00
|
|
|
int family;
|
|
|
|
int err;
|
2010-02-23 08:20:22 +08:00
|
|
|
u32 mark = x->mark.v & x->mark.m;
|
2006-08-24 08:56:04 +08:00
|
|
|
int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
family = x->props.family;
|
|
|
|
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = NULL;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
|
|
|
|
2006-08-24 15:42:45 +08:00
|
|
|
x1 = __xfrm_state_locate(x, use_spi, family);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x1) {
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = x1;
|
2005-04-17 06:20:36 +08:00
|
|
|
x1 = NULL;
|
|
|
|
err = -EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2006-08-24 08:56:04 +08:00
|
|
|
if (use_spi && x->km.seq) {
|
2010-02-23 08:20:22 +08:00
|
|
|
x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
|
2007-03-13 08:14:07 +08:00
|
|
|
if (x1 && ((x1->id.proto != x->id.proto) ||
|
|
|
|
xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = x1;
|
2005-04-17 06:20:36 +08:00
|
|
|
x1 = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-24 08:56:04 +08:00
|
|
|
if (use_spi && !x1)
|
2010-02-23 08:20:22 +08:00
|
|
|
x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
|
|
|
|
x->props.reqid, x->id.proto,
|
2006-08-24 15:13:10 +08:00
|
|
|
&x->id.daddr, &x->props.saddr, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-08-24 18:29:04 +08:00
|
|
|
__xfrm_state_bump_genids(x);
|
2005-04-17 06:20:36 +08:00
|
|
|
__xfrm_state_insert(x);
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
|
|
|
|
if (x1) {
|
|
|
|
xfrm_state_delete(x1);
|
|
|
|
xfrm_state_put(x1);
|
|
|
|
}
|
|
|
|
|
2008-09-03 11:14:15 +08:00
|
|
|
if (to_put)
|
|
|
|
xfrm_state_put(to_put);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_add);
|
|
|
|
|
2007-02-09 05:11:42 +08:00
|
|
|
#ifdef CONFIG_XFRM_MIGRATE
|
2008-01-08 17:35:52 +08:00
|
|
|
static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
|
2007-02-09 05:11:42 +08:00
|
|
|
{
|
2008-11-26 09:29:47 +08:00
|
|
|
struct net *net = xs_net(orig);
|
2007-02-09 05:11:42 +08:00
|
|
|
int err = -ENOMEM;
|
2008-11-26 09:29:47 +08:00
|
|
|
struct xfrm_state *x = xfrm_state_alloc(net);
|
2007-02-09 05:11:42 +08:00
|
|
|
if (!x)
|
2010-02-16 04:00:51 +08:00
|
|
|
goto out;
|
2007-02-09 05:11:42 +08:00
|
|
|
|
|
|
|
memcpy(&x->id, &orig->id, sizeof(x->id));
|
|
|
|
memcpy(&x->sel, &orig->sel, sizeof(x->sel));
|
|
|
|
memcpy(&x->lft, &orig->lft, sizeof(x->lft));
|
|
|
|
x->props.mode = orig->props.mode;
|
|
|
|
x->props.replay_window = orig->props.replay_window;
|
|
|
|
x->props.reqid = orig->props.reqid;
|
|
|
|
x->props.family = orig->props.family;
|
|
|
|
x->props.saddr = orig->props.saddr;
|
|
|
|
|
|
|
|
if (orig->aalg) {
|
2009-11-25 08:29:52 +08:00
|
|
|
x->aalg = xfrm_algo_auth_clone(orig->aalg);
|
2007-02-09 05:11:42 +08:00
|
|
|
if (!x->aalg)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
x->props.aalgo = orig->props.aalgo;
|
|
|
|
|
|
|
|
if (orig->ealg) {
|
|
|
|
x->ealg = xfrm_algo_clone(orig->ealg);
|
|
|
|
if (!x->ealg)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
x->props.ealgo = orig->props.ealgo;
|
|
|
|
|
|
|
|
if (orig->calg) {
|
|
|
|
x->calg = xfrm_algo_clone(orig->calg);
|
|
|
|
if (!x->calg)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
x->props.calgo = orig->props.calgo;
|
|
|
|
|
2007-02-09 22:25:29 +08:00
|
|
|
if (orig->encap) {
|
2007-02-09 05:11:42 +08:00
|
|
|
x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
|
|
|
|
if (!x->encap)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (orig->coaddr) {
|
|
|
|
x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!x->coaddr)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2010-02-23 08:20:22 +08:00
|
|
|
memcpy(&x->mark, &orig->mark, sizeof(x->mark));
|
|
|
|
|
2007-02-09 05:11:42 +08:00
|
|
|
err = xfrm_init_state(x);
|
|
|
|
if (err)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
x->props.flags = orig->props.flags;
|
|
|
|
|
|
|
|
x->curlft.add_time = orig->curlft.add_time;
|
|
|
|
x->km.state = orig->km.state;
|
|
|
|
x->km.seq = orig->km.seq;
|
|
|
|
|
|
|
|
return x;
|
|
|
|
|
|
|
|
error:
|
2010-02-16 04:00:51 +08:00
|
|
|
xfrm_state_put(x);
|
|
|
|
out:
|
2007-02-09 05:11:42 +08:00
|
|
|
if (errp)
|
|
|
|
*errp = err;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xfrm_state_lock is held */
|
|
|
|
struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
|
|
|
|
{
|
|
|
|
unsigned int h;
|
|
|
|
struct xfrm_state *x;
|
|
|
|
struct hlist_node *entry;
|
|
|
|
|
|
|
|
if (m->reqid) {
|
2008-11-26 09:29:21 +08:00
|
|
|
h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
|
2007-02-09 05:11:42 +08:00
|
|
|
m->reqid, m->old_family);
|
2008-11-26 09:16:58 +08:00
|
|
|
hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
|
2007-02-09 05:11:42 +08:00
|
|
|
if (x->props.mode != m->mode ||
|
|
|
|
x->id.proto != m->proto)
|
|
|
|
continue;
|
|
|
|
if (m->reqid && x->props.reqid != m->reqid)
|
|
|
|
continue;
|
|
|
|
if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
|
|
|
|
m->old_family) ||
|
|
|
|
xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
|
|
|
|
m->old_family))
|
|
|
|
continue;
|
|
|
|
xfrm_state_hold(x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
} else {
|
2008-11-26 09:29:21 +08:00
|
|
|
h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
|
2007-02-09 05:11:42 +08:00
|
|
|
m->old_family);
|
2008-11-26 09:17:24 +08:00
|
|
|
hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
|
2007-02-09 05:11:42 +08:00
|
|
|
if (x->props.mode != m->mode ||
|
|
|
|
x->id.proto != m->proto)
|
|
|
|
continue;
|
|
|
|
if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
|
|
|
|
m->old_family) ||
|
|
|
|
xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
|
|
|
|
m->old_family))
|
|
|
|
continue;
|
|
|
|
xfrm_state_hold(x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-09 22:25:29 +08:00
|
|
|
return NULL;
|
2007-02-09 05:11:42 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_migrate_state_find);
|
|
|
|
|
|
|
|
struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
|
|
|
|
struct xfrm_migrate *m)
|
|
|
|
{
|
|
|
|
struct xfrm_state *xc;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
xc = xfrm_state_clone(x, &err);
|
|
|
|
if (!xc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
|
|
|
|
memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
|
|
|
|
|
|
|
|
/* add state */
|
|
|
|
if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
|
|
|
|
/* a care is needed when the destination address of the
|
|
|
|
state is to be updated as it is a part of triplet */
|
|
|
|
xfrm_state_insert(xc);
|
|
|
|
} else {
|
|
|
|
if ((err = xfrm_state_add(xc)) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xc;
|
|
|
|
error:
|
2010-12-07 07:28:56 +08:00
|
|
|
xfrm_state_put(xc);
|
2007-02-09 05:11:42 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_migrate);
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int xfrm_state_update(struct xfrm_state *x)
|
|
|
|
{
|
2008-09-03 11:14:15 +08:00
|
|
|
struct xfrm_state *x1, *to_put;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err;
|
2006-08-24 08:56:04 +08:00
|
|
|
int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = NULL;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2006-08-24 15:42:45 +08:00
|
|
|
x1 = __xfrm_state_locate(x, use_spi, x->props.family);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
err = -ESRCH;
|
|
|
|
if (!x1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (xfrm_state_kern(x1)) {
|
2008-09-03 11:14:15 +08:00
|
|
|
to_put = x1;
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x1->km.state == XFRM_STATE_ACQ) {
|
|
|
|
__xfrm_state_insert(x);
|
|
|
|
x = NULL;
|
|
|
|
}
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
|
2008-09-03 11:14:15 +08:00
|
|
|
if (to_put)
|
|
|
|
xfrm_state_put(to_put);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!x) {
|
|
|
|
xfrm_state_delete(x1);
|
|
|
|
xfrm_state_put(x1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
spin_lock_bh(&x1->lock);
|
|
|
|
if (likely(x1->km.state == XFRM_STATE_VALID)) {
|
|
|
|
if (x->encap && x1->encap)
|
|
|
|
memcpy(x1->encap, x->encap, sizeof(*x1->encap));
|
2006-08-24 09:18:55 +08:00
|
|
|
if (x->coaddr && x1->coaddr) {
|
|
|
|
memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
|
|
|
|
}
|
|
|
|
if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
|
|
|
|
memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
|
2005-04-17 06:20:36 +08:00
|
|
|
memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
|
|
|
|
x1->km.dying = 0;
|
|
|
|
|
2009-11-09 12:58:41 +08:00
|
|
|
tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x1->curlft.use_time)
|
|
|
|
xfrm_state_check_expire(x1);
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
spin_unlock_bh(&x1->lock);
|
|
|
|
|
|
|
|
xfrm_state_put(x1);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_update);
|
|
|
|
|
|
|
|
int xfrm_state_check_expire(struct xfrm_state *x)
|
|
|
|
{
|
|
|
|
if (!x->curlft.use_time)
|
2007-03-05 08:12:44 +08:00
|
|
|
x->curlft.use_time = get_seconds();
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (x->km.state != XFRM_STATE_VALID)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (x->curlft.bytes >= x->lft.hard_byte_limit ||
|
|
|
|
x->curlft.packets >= x->lft.hard_packet_limit) {
|
2005-06-19 13:43:22 +08:00
|
|
|
x->km.state = XFRM_STATE_EXPIRED;
|
2009-11-09 12:58:41 +08:00
|
|
|
tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!x->km.dying &&
|
|
|
|
(x->curlft.bytes >= x->lft.soft_byte_limit ||
|
2005-06-19 13:43:22 +08:00
|
|
|
x->curlft.packets >= x->lft.soft_packet_limit)) {
|
|
|
|
x->km.dying = 1;
|
2006-03-21 11:17:03 +08:00
|
|
|
km_state_expired(x, 0, 0);
|
2005-06-19 13:43:22 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_check_expire);
|
|
|
|
|
|
|
|
struct xfrm_state *
|
2010-02-23 08:20:22 +08:00
|
|
|
xfrm_state_lookup(struct net *net, u32 mark, xfrm_address_t *daddr, __be32 spi,
|
|
|
|
u8 proto, unsigned short family)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct xfrm_state *x;
|
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2010-02-23 08:20:22 +08:00
|
|
|
x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_lookup);
|
|
|
|
|
|
|
|
struct xfrm_state *
|
2010-02-23 08:20:22 +08:00
|
|
|
xfrm_state_lookup_byaddr(struct net *net, u32 mark,
|
2008-11-26 09:30:50 +08:00
|
|
|
xfrm_address_t *daddr, xfrm_address_t *saddr,
|
2006-08-24 08:56:04 +08:00
|
|
|
u8 proto, unsigned short family)
|
|
|
|
{
|
|
|
|
struct xfrm_state *x;
|
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2010-02-23 08:20:22 +08:00
|
|
|
x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
|
2006-08-24 08:56:04 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
|
|
|
|
|
|
|
|
struct xfrm_state *
|
2010-02-23 08:20:22 +08:00
|
|
|
xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
|
2007-02-09 22:25:29 +08:00
|
|
|
xfrm_address_t *daddr, xfrm_address_t *saddr,
|
2005-04-17 06:20:36 +08:00
|
|
|
int create, unsigned short family)
|
|
|
|
{
|
|
|
|
struct xfrm_state *x;
|
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2010-02-23 08:20:22 +08:00
|
|
|
x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
2006-08-24 15:13:10 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_find_acq);
|
|
|
|
|
2006-08-24 13:48:31 +08:00
|
|
|
#ifdef CONFIG_XFRM_SUB_POLICY
|
|
|
|
int
|
|
|
|
xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
|
|
|
|
unsigned short family)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
|
|
|
|
if (!afinfo)
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
|
|
|
if (afinfo->tmpl_sort)
|
|
|
|
err = afinfo->tmpl_sort(dst, src, n);
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_tmpl_sort);
|
|
|
|
|
|
|
|
int
|
|
|
|
xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
|
|
|
|
unsigned short family)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
|
|
|
|
if (!afinfo)
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
|
|
|
if (afinfo->state_sort)
|
|
|
|
err = afinfo->state_sort(dst, src, n);
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_sort);
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Silly enough, but I'm lazy to build resolution list */
|
|
|
|
|
2010-02-23 08:20:22 +08:00
|
|
|
static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-11-26 09:31:51 +08:00
|
|
|
for (i = 0; i <= net->xfrm.state_hmask; i++) {
|
2006-08-24 17:45:07 +08:00
|
|
|
struct hlist_node *entry;
|
|
|
|
struct xfrm_state *x;
|
|
|
|
|
2008-11-26 09:31:51 +08:00
|
|
|
hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
|
2006-08-24 17:45:07 +08:00
|
|
|
if (x->km.seq == seq &&
|
2010-02-22 19:32:56 +08:00
|
|
|
(mark & x->mark.m) == x->mark.v &&
|
2006-08-24 17:45:07 +08:00
|
|
|
x->km.state == XFRM_STATE_ACQ) {
|
2005-04-17 06:20:36 +08:00
|
|
|
xfrm_state_hold(x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-23 08:20:22 +08:00
|
|
|
struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct xfrm_state *x;
|
|
|
|
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2010-02-23 08:20:22 +08:00
|
|
|
x = __xfrm_find_acq_byseq(net, mark, seq);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_find_acq_byseq);
|
|
|
|
|
|
|
|
u32 xfrm_get_acqseq(void)
|
|
|
|
{
|
|
|
|
u32 res;
|
2010-02-16 10:01:22 +08:00
|
|
|
static atomic_t acqseq;
|
|
|
|
|
|
|
|
do {
|
|
|
|
res = atomic_inc_return(&acqseq);
|
|
|
|
} while (!res);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_get_acqseq);
|
|
|
|
|
2007-10-10 04:29:52 +08:00
|
|
|
int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-11-26 09:30:50 +08:00
|
|
|
struct net *net = xs_net(x);
|
2006-08-24 18:08:07 +08:00
|
|
|
unsigned int h;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct xfrm_state *x0;
|
2007-10-10 04:29:52 +08:00
|
|
|
int err = -ENOENT;
|
|
|
|
__be32 minspi = htonl(low);
|
|
|
|
__be32 maxspi = htonl(high);
|
2010-02-23 08:20:22 +08:00
|
|
|
u32 mark = x->mark.v & x->mark.m;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-10 04:29:52 +08:00
|
|
|
spin_lock_bh(&x->lock);
|
|
|
|
if (x->km.state == XFRM_STATE_DEAD)
|
|
|
|
goto unlock;
|
|
|
|
|
|
|
|
err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x->id.spi)
|
2007-10-10 04:29:52 +08:00
|
|
|
goto unlock;
|
|
|
|
|
|
|
|
err = -ENOENT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (minspi == maxspi) {
|
2010-02-23 08:20:22 +08:00
|
|
|
x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x0) {
|
|
|
|
xfrm_state_put(x0);
|
2007-10-10 04:29:52 +08:00
|
|
|
goto unlock;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
x->id.spi = minspi;
|
|
|
|
} else {
|
|
|
|
u32 spi = 0;
|
2006-09-28 09:47:05 +08:00
|
|
|
for (h=0; h<high-low+1; h++) {
|
|
|
|
spi = low + net_random()%(high-low+1);
|
2010-02-23 08:20:22 +08:00
|
|
|
x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x0 == NULL) {
|
|
|
|
x->id.spi = htonl(spi);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xfrm_state_put(x0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (x->id.spi) {
|
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2008-11-26 09:31:18 +08:00
|
|
|
h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
|
|
|
|
hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
2007-10-10 04:29:52 +08:00
|
|
|
|
|
|
|
err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-10-10 04:29:52 +08:00
|
|
|
|
|
|
|
unlock:
|
|
|
|
spin_unlock_bh(&x->lock);
|
|
|
|
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_alloc_spi);
|
|
|
|
|
2008-11-26 09:32:14 +08:00
|
|
|
int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
|
2008-02-29 13:31:08 +08:00
|
|
|
int (*func)(struct xfrm_state *, int, void*),
|
2005-04-17 06:20:36 +08:00
|
|
|
void *data)
|
|
|
|
{
|
2008-10-01 22:03:24 +08:00
|
|
|
struct xfrm_state *state;
|
|
|
|
struct xfrm_state_walk *x;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err = 0;
|
|
|
|
|
2008-10-01 22:03:24 +08:00
|
|
|
if (walk->seq != 0 && list_empty(&walk->all))
|
2008-02-29 13:31:08 +08:00
|
|
|
return 0;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
2008-10-01 22:03:24 +08:00
|
|
|
if (list_empty(&walk->all))
|
2008-11-26 09:32:14 +08:00
|
|
|
x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
|
2008-10-01 22:03:24 +08:00
|
|
|
else
|
|
|
|
x = list_entry(&walk->all, struct xfrm_state_walk, all);
|
2008-11-26 09:32:14 +08:00
|
|
|
list_for_each_entry_from(x, &net->xfrm.state_all, all) {
|
2008-10-01 22:03:24 +08:00
|
|
|
if (x->state == XFRM_STATE_DEAD)
|
2008-02-29 13:31:08 +08:00
|
|
|
continue;
|
2008-10-01 22:03:24 +08:00
|
|
|
state = container_of(x, struct xfrm_state, km);
|
|
|
|
if (!xfrm_id_proto_match(state->id.proto, walk->proto))
|
2008-02-29 13:31:08 +08:00
|
|
|
continue;
|
2008-10-01 22:03:24 +08:00
|
|
|
err = func(state, walk->seq, data);
|
|
|
|
if (err) {
|
|
|
|
list_move_tail(&walk->all, &x->all);
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-10-01 22:03:24 +08:00
|
|
|
walk->seq++;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-10-01 22:03:24 +08:00
|
|
|
if (walk->seq == 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-10-01 22:03:24 +08:00
|
|
|
list_del_init(&walk->all);
|
2005-04-17 06:20:36 +08:00
|
|
|
out:
|
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_walk);
|
|
|
|
|
2008-09-23 10:48:19 +08:00
|
|
|
void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
|
|
|
|
{
|
2008-10-01 22:03:24 +08:00
|
|
|
INIT_LIST_HEAD(&walk->all);
|
2008-09-23 10:48:19 +08:00
|
|
|
walk->proto = proto;
|
2008-10-01 22:03:24 +08:00
|
|
|
walk->state = XFRM_STATE_DEAD;
|
|
|
|
walk->seq = 0;
|
2008-09-23 10:48:19 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_walk_init);
|
|
|
|
|
2008-09-10 10:58:29 +08:00
|
|
|
void xfrm_state_walk_done(struct xfrm_state_walk *walk)
|
|
|
|
{
|
2008-10-01 22:03:24 +08:00
|
|
|
if (list_empty(&walk->all))
|
2008-09-23 10:48:19 +08:00
|
|
|
return;
|
|
|
|
|
2008-10-01 22:03:24 +08:00
|
|
|
spin_lock_bh(&xfrm_state_lock);
|
|
|
|
list_del(&walk->all);
|
2009-03-27 15:22:01 +08:00
|
|
|
spin_unlock_bh(&xfrm_state_lock);
|
2008-09-10 10:58:29 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_walk_done);
|
|
|
|
|
2006-03-21 11:15:11 +08:00
|
|
|
|
|
|
|
void xfrm_replay_notify(struct xfrm_state *x, int event)
|
|
|
|
{
|
|
|
|
struct km_event c;
|
|
|
|
/* we send notify messages in case
|
|
|
|
* 1. we updated on of the sequence numbers, and the seqno difference
|
|
|
|
* is at least x->replay_maxdiff, in this case we also update the
|
|
|
|
* timeout of our timer function
|
|
|
|
* 2. if x->replay_maxage has elapsed since last update,
|
|
|
|
* and there were changes
|
|
|
|
*
|
|
|
|
* The state structure must be locked!
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case XFRM_REPLAY_UPDATE:
|
|
|
|
if (x->replay_maxdiff &&
|
|
|
|
(x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
|
2006-04-15 06:03:05 +08:00
|
|
|
(x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
|
|
|
|
if (x->xflags & XFRM_TIME_DEFER)
|
|
|
|
event = XFRM_REPLAY_TIMEOUT;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2006-03-21 11:15:11 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XFRM_REPLAY_TIMEOUT:
|
|
|
|
if ((x->replay.seq == x->preplay.seq) &&
|
|
|
|
(x->replay.bitmap == x->preplay.bitmap) &&
|
2006-04-15 06:03:05 +08:00
|
|
|
(x->replay.oseq == x->preplay.oseq)) {
|
|
|
|
x->xflags |= XFRM_TIME_DEFER;
|
2006-03-21 11:15:11 +08:00
|
|
|
return;
|
2006-04-15 06:03:05 +08:00
|
|
|
}
|
2006-03-21 11:15:11 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
|
|
|
|
c.event = XFRM_MSG_NEWAE;
|
|
|
|
c.data.aevent = event;
|
|
|
|
km_state_notify(x, &c);
|
|
|
|
|
|
|
|
if (x->replay_maxage &&
|
2006-08-24 18:54:22 +08:00
|
|
|
!mod_timer(&x->rtimer, jiffies + x->replay_maxage))
|
2006-04-15 06:03:05 +08:00
|
|
|
x->xflags &= ~XFRM_TIME_DEFER;
|
2006-03-21 11:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xfrm_replay_timer_handler(unsigned long data)
|
|
|
|
{
|
|
|
|
struct xfrm_state *x = (struct xfrm_state*)data;
|
|
|
|
|
|
|
|
spin_lock(&x->lock);
|
|
|
|
|
2006-04-15 06:03:05 +08:00
|
|
|
if (x->km.state == XFRM_STATE_VALID) {
|
2008-11-26 09:38:20 +08:00
|
|
|
if (xfrm_aevent_is_on(xs_net(x)))
|
2006-04-15 06:03:05 +08:00
|
|
|
xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
|
|
|
|
else
|
|
|
|
x->xflags |= XFRM_TIME_DEFER;
|
|
|
|
}
|
2006-03-21 11:15:11 +08:00
|
|
|
|
|
|
|
spin_unlock(&x->lock);
|
|
|
|
}
|
|
|
|
|
2007-12-22 06:58:11 +08:00
|
|
|
int xfrm_replay_check(struct xfrm_state *x,
|
|
|
|
struct sk_buff *skb, __be32 net_seq)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
u32 diff;
|
2006-09-28 09:48:18 +08:00
|
|
|
u32 seq = ntohl(net_seq);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (unlikely(seq == 0))
|
2007-12-22 06:58:11 +08:00
|
|
|
goto err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (likely(seq > x->replay.seq))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
diff = x->replay.seq - seq;
|
2007-04-05 15:07:39 +08:00
|
|
|
if (diff >= min_t(unsigned int, x->props.replay_window,
|
|
|
|
sizeof(x->replay.bitmap) * 8)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
x->stats.replay_window++;
|
2007-12-22 06:58:11 +08:00
|
|
|
goto err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (x->replay.bitmap & (1U << diff)) {
|
|
|
|
x->stats.replay++;
|
2007-12-22 06:58:11 +08:00
|
|
|
goto err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return 0;
|
2007-12-22 06:58:11 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
xfrm_audit_state_replay(x, skb, net_seq);
|
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-09-28 09:48:33 +08:00
|
|
|
void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
u32 diff;
|
2006-09-28 09:48:33 +08:00
|
|
|
u32 seq = ntohl(net_seq);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (seq > x->replay.seq) {
|
|
|
|
diff = seq - x->replay.seq;
|
|
|
|
if (diff < x->props.replay_window)
|
|
|
|
x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
|
|
|
|
else
|
|
|
|
x->replay.bitmap = 1;
|
|
|
|
x->replay.seq = seq;
|
|
|
|
} else {
|
|
|
|
diff = x->replay.seq - seq;
|
|
|
|
x->replay.bitmap |= (1U << diff);
|
|
|
|
}
|
2006-03-21 11:15:11 +08:00
|
|
|
|
2008-11-26 09:38:20 +08:00
|
|
|
if (xfrm_aevent_is_on(xs_net(x)))
|
2006-03-21 11:15:11 +08:00
|
|
|
xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-12-07 16:51:11 +08:00
|
|
|
static LIST_HEAD(xfrm_km_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
static DEFINE_RWLOCK(xfrm_km_lock);
|
|
|
|
|
2011-02-24 13:02:38 +08:00
|
|
|
void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct xfrm_mgr *km;
|
|
|
|
|
2005-06-19 13:42:13 +08:00
|
|
|
read_lock(&xfrm_km_lock);
|
|
|
|
list_for_each_entry(km, &xfrm_km_list, list)
|
|
|
|
if (km->notify_policy)
|
|
|
|
km->notify_policy(xp, dir, c);
|
|
|
|
read_unlock(&xfrm_km_lock);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-02-24 13:02:38 +08:00
|
|
|
void km_state_notify(struct xfrm_state *x, const struct km_event *c)
|
2005-06-19 13:42:13 +08:00
|
|
|
{
|
|
|
|
struct xfrm_mgr *km;
|
2005-04-17 06:20:36 +08:00
|
|
|
read_lock(&xfrm_km_lock);
|
|
|
|
list_for_each_entry(km, &xfrm_km_list, list)
|
2005-06-19 13:42:13 +08:00
|
|
|
if (km->notify)
|
|
|
|
km->notify(x, c);
|
2005-04-17 06:20:36 +08:00
|
|
|
read_unlock(&xfrm_km_lock);
|
2005-06-19 13:42:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(km_policy_notify);
|
|
|
|
EXPORT_SYMBOL(km_state_notify);
|
|
|
|
|
2006-03-21 11:17:03 +08:00
|
|
|
void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
|
2005-06-19 13:42:13 +08:00
|
|
|
{
|
2008-11-26 09:29:47 +08:00
|
|
|
struct net *net = xs_net(x);
|
2005-06-19 13:42:13 +08:00
|
|
|
struct km_event c;
|
|
|
|
|
2005-06-19 13:44:00 +08:00
|
|
|
c.data.hard = hard;
|
2006-03-21 11:17:03 +08:00
|
|
|
c.pid = pid;
|
2005-06-19 13:44:37 +08:00
|
|
|
c.event = XFRM_MSG_EXPIRE;
|
2005-06-19 13:42:13 +08:00
|
|
|
km_state_notify(x, &c);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (hard)
|
2008-11-26 09:29:47 +08:00
|
|
|
wake_up(&net->xfrm.km_waitq);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-03-21 11:17:03 +08:00
|
|
|
EXPORT_SYMBOL(km_state_expired);
|
2005-06-19 13:42:13 +08:00
|
|
|
/*
|
|
|
|
* We send to all registered managers regardless of failure
|
|
|
|
* We are happy with one success
|
|
|
|
*/
|
[IPSEC]: Sync series - acquire insert
This introduces a feature similar to the one described in RFC 2367:
"
... the application needing an SA sends a PF_KEY
SADB_ACQUIRE message down to the Key Engine, which then either
returns an error or sends a similar SADB_ACQUIRE message up to one or
more key management applications capable of creating such SAs.
...
...
The third is where an application-layer consumer of security
associations (e.g. an OSPFv2 or RIPv2 daemon) needs a security
association.
Send an SADB_ACQUIRE message from a user process to the kernel.
<base, address(SD), (address(P),) (identity(SD),) (sensitivity,)
proposal>
The kernel returns an SADB_ACQUIRE message to registered
sockets.
<base, address(SD), (address(P),) (identity(SD),) (sensitivity,)
proposal>
The user-level consumer waits for an SADB_UPDATE or SADB_ADD
message for its particular type, and then can use that
association by using SADB_GET messages.
"
An app such as OSPF could then use ipsec KM to get keys
Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 11:16:40 +08:00
|
|
|
int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-06-19 13:42:13 +08:00
|
|
|
int err = -EINVAL, acqret;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct xfrm_mgr *km;
|
|
|
|
|
|
|
|
read_lock(&xfrm_km_lock);
|
|
|
|
list_for_each_entry(km, &xfrm_km_list, list) {
|
2005-06-19 13:42:13 +08:00
|
|
|
acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
|
|
|
|
if (!acqret)
|
|
|
|
err = acqret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
read_unlock(&xfrm_km_lock);
|
|
|
|
return err;
|
|
|
|
}
|
[IPSEC]: Sync series - acquire insert
This introduces a feature similar to the one described in RFC 2367:
"
... the application needing an SA sends a PF_KEY
SADB_ACQUIRE message down to the Key Engine, which then either
returns an error or sends a similar SADB_ACQUIRE message up to one or
more key management applications capable of creating such SAs.
...
...
The third is where an application-layer consumer of security
associations (e.g. an OSPFv2 or RIPv2 daemon) needs a security
association.
Send an SADB_ACQUIRE message from a user process to the kernel.
<base, address(SD), (address(P),) (identity(SD),) (sensitivity,)
proposal>
The kernel returns an SADB_ACQUIRE message to registered
sockets.
<base, address(SD), (address(P),) (identity(SD),) (sensitivity,)
proposal>
The user-level consumer waits for an SADB_UPDATE or SADB_ADD
message for its particular type, and then can use that
association by using SADB_GET messages.
"
An app such as OSPF could then use ipsec KM to get keys
Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 11:16:40 +08:00
|
|
|
EXPORT_SYMBOL(km_query);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-11-08 16:24:06 +08:00
|
|
|
int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err = -EINVAL;
|
|
|
|
struct xfrm_mgr *km;
|
|
|
|
|
|
|
|
read_lock(&xfrm_km_lock);
|
|
|
|
list_for_each_entry(km, &xfrm_km_list, list) {
|
|
|
|
if (km->new_mapping)
|
|
|
|
err = km->new_mapping(x, ipaddr, sport);
|
|
|
|
if (!err)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
read_unlock(&xfrm_km_lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(km_new_mapping);
|
|
|
|
|
2006-03-21 11:17:25 +08:00
|
|
|
void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-11-26 09:29:47 +08:00
|
|
|
struct net *net = xp_net(pol);
|
2005-06-19 13:42:13 +08:00
|
|
|
struct km_event c;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-06-19 13:44:00 +08:00
|
|
|
c.data.hard = hard;
|
2006-03-21 11:17:25 +08:00
|
|
|
c.pid = pid;
|
2005-06-19 13:44:37 +08:00
|
|
|
c.event = XFRM_MSG_POLEXPIRE;
|
2005-06-19 13:42:13 +08:00
|
|
|
km_policy_notify(pol, dir, &c);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (hard)
|
2008-11-26 09:29:47 +08:00
|
|
|
wake_up(&net->xfrm.km_waitq);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-03-21 11:18:52 +08:00
|
|
|
EXPORT_SYMBOL(km_policy_expired);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-04 12:43:21 +08:00
|
|
|
#ifdef CONFIG_XFRM_MIGRATE
|
2007-02-09 05:11:42 +08:00
|
|
|
int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
|
2008-10-06 04:33:42 +08:00
|
|
|
struct xfrm_migrate *m, int num_migrate,
|
|
|
|
struct xfrm_kmaddress *k)
|
2007-02-09 05:11:42 +08:00
|
|
|
{
|
|
|
|
int err = -EINVAL;
|
|
|
|
int ret;
|
|
|
|
struct xfrm_mgr *km;
|
|
|
|
|
|
|
|
read_lock(&xfrm_km_lock);
|
|
|
|
list_for_each_entry(km, &xfrm_km_list, list) {
|
|
|
|
if (km->migrate) {
|
2008-10-06 04:33:42 +08:00
|
|
|
ret = km->migrate(sel, dir, type, m, num_migrate, k);
|
2007-02-09 05:11:42 +08:00
|
|
|
if (!ret)
|
|
|
|
err = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
read_unlock(&xfrm_km_lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(km_migrate);
|
2008-01-04 12:43:21 +08:00
|
|
|
#endif
|
2007-02-09 05:11:42 +08:00
|
|
|
|
2008-11-26 09:51:01 +08:00
|
|
|
int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
|
2006-08-24 11:44:06 +08:00
|
|
|
{
|
|
|
|
int err = -EINVAL;
|
|
|
|
int ret;
|
|
|
|
struct xfrm_mgr *km;
|
|
|
|
|
|
|
|
read_lock(&xfrm_km_lock);
|
|
|
|
list_for_each_entry(km, &xfrm_km_list, list) {
|
|
|
|
if (km->report) {
|
2008-11-26 09:51:01 +08:00
|
|
|
ret = km->report(net, proto, sel, addr);
|
2006-08-24 11:44:06 +08:00
|
|
|
if (!ret)
|
|
|
|
err = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
read_unlock(&xfrm_km_lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(km_report);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
u8 *data;
|
|
|
|
struct xfrm_mgr *km;
|
|
|
|
struct xfrm_policy *pol = NULL;
|
|
|
|
|
|
|
|
if (optlen <= 0 || optlen > PAGE_SIZE)
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
|
|
|
data = kmalloc(optlen, GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
err = -EFAULT;
|
|
|
|
if (copy_from_user(data, optval, optlen))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
read_lock(&xfrm_km_lock);
|
|
|
|
list_for_each_entry(km, &xfrm_km_list, list) {
|
2006-07-25 14:32:20 +08:00
|
|
|
pol = km->compile_policy(sk, optname, data,
|
2005-04-17 06:20:36 +08:00
|
|
|
optlen, &err);
|
|
|
|
if (err >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
read_unlock(&xfrm_km_lock);
|
|
|
|
|
|
|
|
if (err >= 0) {
|
|
|
|
xfrm_sk_policy_insert(sk, err, pol);
|
|
|
|
xfrm_pol_put(pol);
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
kfree(data);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_user_policy);
|
|
|
|
|
|
|
|
int xfrm_register_km(struct xfrm_mgr *km)
|
|
|
|
{
|
|
|
|
write_lock_bh(&xfrm_km_lock);
|
|
|
|
list_add_tail(&km->list, &xfrm_km_list);
|
|
|
|
write_unlock_bh(&xfrm_km_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_register_km);
|
|
|
|
|
|
|
|
int xfrm_unregister_km(struct xfrm_mgr *km)
|
|
|
|
{
|
|
|
|
write_lock_bh(&xfrm_km_lock);
|
|
|
|
list_del(&km->list);
|
|
|
|
write_unlock_bh(&xfrm_km_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_unregister_km);
|
|
|
|
|
|
|
|
int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
if (unlikely(afinfo == NULL))
|
|
|
|
return -EINVAL;
|
|
|
|
if (unlikely(afinfo->family >= NPROTO))
|
|
|
|
return -EAFNOSUPPORT;
|
2006-04-29 06:30:03 +08:00
|
|
|
write_lock_bh(&xfrm_state_afinfo_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
|
|
|
|
err = -ENOBUFS;
|
2006-08-24 15:42:45 +08:00
|
|
|
else
|
2005-04-17 06:20:36 +08:00
|
|
|
xfrm_state_afinfo[afinfo->family] = afinfo;
|
2006-04-29 06:30:03 +08:00
|
|
|
write_unlock_bh(&xfrm_state_afinfo_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_register_afinfo);
|
|
|
|
|
|
|
|
int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
if (unlikely(afinfo == NULL))
|
|
|
|
return -EINVAL;
|
|
|
|
if (unlikely(afinfo->family >= NPROTO))
|
|
|
|
return -EAFNOSUPPORT;
|
2006-04-29 06:30:03 +08:00
|
|
|
write_lock_bh(&xfrm_state_afinfo_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
|
|
|
|
if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
|
|
|
|
err = -EINVAL;
|
2006-08-24 15:42:45 +08:00
|
|
|
else
|
2005-04-17 06:20:36 +08:00
|
|
|
xfrm_state_afinfo[afinfo->family] = NULL;
|
|
|
|
}
|
2006-04-29 06:30:03 +08:00
|
|
|
write_unlock_bh(&xfrm_state_afinfo_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
|
|
|
|
|
2007-10-18 12:33:12 +08:00
|
|
|
static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct xfrm_state_afinfo *afinfo;
|
|
|
|
if (unlikely(family >= NPROTO))
|
|
|
|
return NULL;
|
|
|
|
read_lock(&xfrm_state_afinfo_lock);
|
|
|
|
afinfo = xfrm_state_afinfo[family];
|
2006-05-28 14:03:58 +08:00
|
|
|
if (unlikely(!afinfo))
|
|
|
|
read_unlock(&xfrm_state_afinfo_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return afinfo;
|
|
|
|
}
|
|
|
|
|
2007-10-18 12:33:12 +08:00
|
|
|
static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
|
2008-01-02 13:58:02 +08:00
|
|
|
__releases(xfrm_state_afinfo_lock)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-05-28 14:03:58 +08:00
|
|
|
read_unlock(&xfrm_state_afinfo_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
|
|
|
|
void xfrm_state_delete_tunnel(struct xfrm_state *x)
|
|
|
|
{
|
|
|
|
if (x->tunnel) {
|
|
|
|
struct xfrm_state *t = x->tunnel;
|
|
|
|
|
|
|
|
if (atomic_read(&t->tunnel_users) == 2)
|
|
|
|
xfrm_state_delete(t);
|
|
|
|
atomic_dec(&t->tunnel_users);
|
|
|
|
xfrm_state_put(t);
|
|
|
|
x->tunnel = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xfrm_state_delete_tunnel);
|
|
|
|
|
|
|
|
int xfrm_state_mtu(struct xfrm_state *x, int mtu)
|
|
|
|
{
|
2007-04-10 02:47:18 +08:00
|
|
|
int res;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-04-10 02:47:18 +08:00
|
|
|
spin_lock_bh(&x->lock);
|
|
|
|
if (x->km.state == XFRM_STATE_VALID &&
|
|
|
|
x->type && x->type->get_mtu)
|
|
|
|
res = x->type->get_mtu(x, mtu);
|
|
|
|
else
|
2007-06-19 13:30:15 +08:00
|
|
|
res = mtu - x->props.header_len;
|
2007-04-10 02:47:18 +08:00
|
|
|
spin_unlock_bh(&x->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-06-21 04:18:08 +08:00
|
|
|
int xfrm_init_state(struct xfrm_state *x)
|
|
|
|
{
|
2005-06-21 04:19:41 +08:00
|
|
|
struct xfrm_state_afinfo *afinfo;
|
2008-03-25 05:51:51 +08:00
|
|
|
struct xfrm_mode *inner_mode;
|
2005-06-21 04:19:41 +08:00
|
|
|
int family = x->props.family;
|
2005-06-21 04:18:08 +08:00
|
|
|
int err;
|
|
|
|
|
2005-06-21 04:19:41 +08:00
|
|
|
err = -EAFNOSUPPORT;
|
|
|
|
afinfo = xfrm_state_get_afinfo(family);
|
|
|
|
if (!afinfo)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
if (afinfo->init_flags)
|
|
|
|
err = afinfo->init_flags(x);
|
|
|
|
|
|
|
|
xfrm_state_put_afinfo(afinfo);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
err = -EPROTONOSUPPORT;
|
2007-10-18 12:35:51 +08:00
|
|
|
|
2008-03-25 05:51:51 +08:00
|
|
|
if (x->sel.family != AF_UNSPEC) {
|
|
|
|
inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
|
|
|
|
if (inner_mode == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
|
|
|
|
family != x->sel.family) {
|
|
|
|
xfrm_put_mode(inner_mode);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
x->inner_mode = inner_mode;
|
|
|
|
} else {
|
|
|
|
struct xfrm_mode *inner_mode_iaf;
|
2008-12-04 07:38:07 +08:00
|
|
|
int iafamily = AF_INET;
|
2008-03-25 05:51:51 +08:00
|
|
|
|
2008-12-04 07:38:07 +08:00
|
|
|
inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
|
2008-03-25 05:51:51 +08:00
|
|
|
if (inner_mode == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
|
|
|
|
xfrm_put_mode(inner_mode);
|
|
|
|
goto error;
|
|
|
|
}
|
2008-12-04 07:38:07 +08:00
|
|
|
x->inner_mode = inner_mode;
|
2008-03-25 05:51:51 +08:00
|
|
|
|
2008-12-04 07:38:07 +08:00
|
|
|
if (x->props.family == AF_INET)
|
|
|
|
iafamily = AF_INET6;
|
2008-03-25 05:51:51 +08:00
|
|
|
|
2008-12-04 07:38:07 +08:00
|
|
|
inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
|
|
|
|
if (inner_mode_iaf) {
|
|
|
|
if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
|
|
|
|
x->inner_mode_iaf = inner_mode_iaf;
|
|
|
|
else
|
|
|
|
xfrm_put_mode(inner_mode_iaf);
|
2008-03-25 05:51:51 +08:00
|
|
|
}
|
|
|
|
}
|
2007-10-18 12:35:51 +08:00
|
|
|
|
2005-06-21 04:19:41 +08:00
|
|
|
x->type = xfrm_get_type(x->id.proto, family);
|
2005-06-21 04:18:08 +08:00
|
|
|
if (x->type == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
err = x->type->init_state(x);
|
|
|
|
if (err)
|
|
|
|
goto error;
|
|
|
|
|
2007-10-18 12:35:51 +08:00
|
|
|
x->outer_mode = xfrm_get_mode(x->props.mode, family);
|
|
|
|
if (x->outer_mode == NULL)
|
2006-05-28 14:05:54 +08:00
|
|
|
goto error;
|
|
|
|
|
2005-06-21 04:18:08 +08:00
|
|
|
x->km.state = XFRM_STATE_VALID;
|
|
|
|
|
|
|
|
error:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(xfrm_init_state);
|
2007-02-09 22:25:29 +08:00
|
|
|
|
2008-11-26 09:14:31 +08:00
|
|
|
int __net_init xfrm_state_init(struct net *net)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-08-24 18:08:07 +08:00
|
|
|
unsigned int sz;
|
|
|
|
|
2008-11-26 09:16:11 +08:00
|
|
|
INIT_LIST_HEAD(&net->xfrm.state_all);
|
|
|
|
|
2006-08-24 18:08:07 +08:00
|
|
|
sz = sizeof(struct hlist_head) * 8;
|
|
|
|
|
2008-11-26 09:16:58 +08:00
|
|
|
net->xfrm.state_bydst = xfrm_hash_alloc(sz);
|
|
|
|
if (!net->xfrm.state_bydst)
|
|
|
|
goto out_bydst;
|
2008-11-26 09:17:24 +08:00
|
|
|
net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
|
|
|
|
if (!net->xfrm.state_bysrc)
|
|
|
|
goto out_bysrc;
|
2008-11-26 09:17:47 +08:00
|
|
|
net->xfrm.state_byspi = xfrm_hash_alloc(sz);
|
|
|
|
if (!net->xfrm.state_byspi)
|
|
|
|
goto out_byspi;
|
2008-11-26 09:18:12 +08:00
|
|
|
net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-11-26 09:18:39 +08:00
|
|
|
net->xfrm.state_num = 0;
|
2008-11-26 09:19:07 +08:00
|
|
|
INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
|
2008-11-26 09:20:11 +08:00
|
|
|
INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
|
2008-11-26 09:20:36 +08:00
|
|
|
INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
|
2008-11-26 09:21:01 +08:00
|
|
|
init_waitqueue_head(&net->xfrm.km_waitq);
|
2008-11-26 09:14:31 +08:00
|
|
|
return 0;
|
2008-11-26 09:16:58 +08:00
|
|
|
|
2008-11-26 09:17:47 +08:00
|
|
|
out_byspi:
|
|
|
|
xfrm_hash_free(net->xfrm.state_bysrc, sz);
|
2008-11-26 09:17:24 +08:00
|
|
|
out_bysrc:
|
|
|
|
xfrm_hash_free(net->xfrm.state_bydst, sz);
|
2008-11-26 09:16:58 +08:00
|
|
|
out_bydst:
|
|
|
|
return -ENOMEM;
|
2008-11-26 09:14:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void xfrm_state_fini(struct net *net)
|
|
|
|
{
|
2008-11-26 09:57:44 +08:00
|
|
|
struct xfrm_audit audit_info;
|
2008-11-26 09:16:58 +08:00
|
|
|
unsigned int sz;
|
|
|
|
|
2008-11-26 09:57:44 +08:00
|
|
|
flush_work(&net->xfrm.state_hash_work);
|
|
|
|
audit_info.loginuid = -1;
|
|
|
|
audit_info.sessionid = -1;
|
|
|
|
audit_info.secid = 0;
|
|
|
|
xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
|
|
|
|
flush_work(&net->xfrm.state_gc_work);
|
|
|
|
|
2008-11-26 09:16:11 +08:00
|
|
|
WARN_ON(!list_empty(&net->xfrm.state_all));
|
2008-11-26 09:16:58 +08:00
|
|
|
|
2008-11-26 09:18:12 +08:00
|
|
|
sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
|
2008-11-26 09:17:47 +08:00
|
|
|
WARN_ON(!hlist_empty(net->xfrm.state_byspi));
|
|
|
|
xfrm_hash_free(net->xfrm.state_byspi, sz);
|
2008-11-26 09:17:24 +08:00
|
|
|
WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
|
|
|
|
xfrm_hash_free(net->xfrm.state_bysrc, sz);
|
2008-11-26 09:16:58 +08:00
|
|
|
WARN_ON(!hlist_empty(net->xfrm.state_bydst));
|
|
|
|
xfrm_hash_free(net->xfrm.state_bydst, sz);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-09-18 02:51:22 +08:00
|
|
|
#ifdef CONFIG_AUDITSYSCALL
|
2008-01-06 15:13:20 +08:00
|
|
|
static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
|
|
|
|
struct audit_buffer *audit_buf)
|
2007-09-18 02:51:22 +08:00
|
|
|
{
|
2007-12-21 12:49:33 +08:00
|
|
|
struct xfrm_sec_ctx *ctx = x->security;
|
|
|
|
u32 spi = ntohl(x->id.spi);
|
|
|
|
|
|
|
|
if (ctx)
|
2007-09-18 02:51:22 +08:00
|
|
|
audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
|
2007-12-21 12:49:33 +08:00
|
|
|
ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
|
2007-09-18 02:51:22 +08:00
|
|
|
|
|
|
|
switch(x->props.family) {
|
|
|
|
case AF_INET:
|
2008-10-31 15:54:56 +08:00
|
|
|
audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
|
|
|
|
&x->props.saddr.a4, &x->id.daddr.a4);
|
2007-09-18 02:51:22 +08:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2008-10-30 03:52:50 +08:00
|
|
|
audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
|
2008-10-29 07:10:17 +08:00
|
|
|
x->props.saddr.a6, x->id.daddr.a6);
|
2007-09-18 02:51:22 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-12-21 12:49:33 +08:00
|
|
|
|
|
|
|
audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
|
2007-09-18 02:51:22 +08:00
|
|
|
}
|
|
|
|
|
2008-01-06 15:13:20 +08:00
|
|
|
static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
|
|
|
|
struct audit_buffer *audit_buf)
|
2007-12-22 06:58:11 +08:00
|
|
|
{
|
|
|
|
struct iphdr *iph4;
|
|
|
|
struct ipv6hdr *iph6;
|
|
|
|
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
iph4 = ip_hdr(skb);
|
2008-10-31 15:54:56 +08:00
|
|
|
audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
|
|
|
|
&iph4->saddr, &iph4->daddr);
|
2007-12-22 06:58:11 +08:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
iph6 = ipv6_hdr(skb);
|
|
|
|
audit_log_format(audit_buf,
|
2008-10-30 03:52:50 +08:00
|
|
|
" src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
|
2008-10-29 07:10:17 +08:00
|
|
|
&iph6->saddr,&iph6->daddr,
|
2007-12-22 06:58:11 +08:00
|
|
|
iph6->flow_lbl[0] & 0x0f,
|
|
|
|
iph6->flow_lbl[1],
|
|
|
|
iph6->flow_lbl[2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-21 12:49:33 +08:00
|
|
|
void xfrm_audit_state_add(struct xfrm_state *x, int result,
|
2008-04-18 22:09:25 +08:00
|
|
|
uid_t auid, u32 sessionid, u32 secid)
|
2007-09-18 02:51:22 +08:00
|
|
|
{
|
|
|
|
struct audit_buffer *audit_buf;
|
|
|
|
|
2007-12-22 06:58:11 +08:00
|
|
|
audit_buf = xfrm_audit_start("SAD-add");
|
2007-09-18 02:51:22 +08:00
|
|
|
if (audit_buf == NULL)
|
|
|
|
return;
|
2008-04-18 22:09:25 +08:00
|
|
|
xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
|
2007-12-22 06:58:11 +08:00
|
|
|
xfrm_audit_helper_sainfo(x, audit_buf);
|
|
|
|
audit_log_format(audit_buf, " res=%u", result);
|
2007-09-18 02:51:22 +08:00
|
|
|
audit_log_end(audit_buf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
|
|
|
|
|
2007-12-21 12:49:33 +08:00
|
|
|
void xfrm_audit_state_delete(struct xfrm_state *x, int result,
|
2008-04-18 22:09:25 +08:00
|
|
|
uid_t auid, u32 sessionid, u32 secid)
|
2007-09-18 02:51:22 +08:00
|
|
|
{
|
|
|
|
struct audit_buffer *audit_buf;
|
|
|
|
|
2007-12-22 06:58:11 +08:00
|
|
|
audit_buf = xfrm_audit_start("SAD-delete");
|
2007-09-18 02:51:22 +08:00
|
|
|
if (audit_buf == NULL)
|
|
|
|
return;
|
2008-04-18 22:09:25 +08:00
|
|
|
xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
|
2007-12-22 06:58:11 +08:00
|
|
|
xfrm_audit_helper_sainfo(x, audit_buf);
|
|
|
|
audit_log_format(audit_buf, " res=%u", result);
|
2007-09-18 02:51:22 +08:00
|
|
|
audit_log_end(audit_buf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
|
2007-12-22 06:58:11 +08:00
|
|
|
|
|
|
|
void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct audit_buffer *audit_buf;
|
|
|
|
u32 spi;
|
|
|
|
|
|
|
|
audit_buf = xfrm_audit_start("SA-replay-overflow");
|
|
|
|
if (audit_buf == NULL)
|
|
|
|
return;
|
|
|
|
xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
|
|
|
|
/* don't record the sequence number because it's inherent in this kind
|
|
|
|
* of audit message */
|
|
|
|
spi = ntohl(x->id.spi);
|
|
|
|
audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
|
|
|
|
audit_log_end(audit_buf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
|
|
|
|
|
|
|
|
static void xfrm_audit_state_replay(struct xfrm_state *x,
|
|
|
|
struct sk_buff *skb, __be32 net_seq)
|
|
|
|
{
|
|
|
|
struct audit_buffer *audit_buf;
|
|
|
|
u32 spi;
|
|
|
|
|
|
|
|
audit_buf = xfrm_audit_start("SA-replayed-pkt");
|
|
|
|
if (audit_buf == NULL)
|
|
|
|
return;
|
|
|
|
xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
|
|
|
|
spi = ntohl(x->id.spi);
|
|
|
|
audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
|
|
|
|
spi, spi, ntohl(net_seq));
|
|
|
|
audit_log_end(audit_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
|
|
|
|
{
|
|
|
|
struct audit_buffer *audit_buf;
|
|
|
|
|
|
|
|
audit_buf = xfrm_audit_start("SA-notfound");
|
|
|
|
if (audit_buf == NULL)
|
|
|
|
return;
|
|
|
|
xfrm_audit_helper_pktinfo(skb, family, audit_buf);
|
|
|
|
audit_log_end(audit_buf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
|
|
|
|
|
|
|
|
void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
|
|
|
|
__be32 net_spi, __be32 net_seq)
|
|
|
|
{
|
|
|
|
struct audit_buffer *audit_buf;
|
|
|
|
u32 spi;
|
|
|
|
|
|
|
|
audit_buf = xfrm_audit_start("SA-notfound");
|
|
|
|
if (audit_buf == NULL)
|
|
|
|
return;
|
|
|
|
xfrm_audit_helper_pktinfo(skb, family, audit_buf);
|
|
|
|
spi = ntohl(net_spi);
|
|
|
|
audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
|
|
|
|
spi, spi, ntohl(net_seq));
|
|
|
|
audit_log_end(audit_buf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
|
|
|
|
|
|
|
|
void xfrm_audit_state_icvfail(struct xfrm_state *x,
|
|
|
|
struct sk_buff *skb, u8 proto)
|
|
|
|
{
|
|
|
|
struct audit_buffer *audit_buf;
|
|
|
|
__be32 net_spi;
|
|
|
|
__be32 net_seq;
|
|
|
|
|
|
|
|
audit_buf = xfrm_audit_start("SA-icv-failure");
|
|
|
|
if (audit_buf == NULL)
|
|
|
|
return;
|
|
|
|
xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
|
|
|
|
if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
|
|
|
|
u32 spi = ntohl(net_spi);
|
|
|
|
audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
|
|
|
|
spi, spi, ntohl(net_seq));
|
|
|
|
}
|
|
|
|
audit_log_end(audit_buf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
|
2007-09-18 02:51:22 +08:00
|
|
|
#endif /* CONFIG_AUDITSYSCALL */
|