2019-05-19 15:08:55 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2005-04-17 02:20:36 +04: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 10:12:27 +03:00
*
2005-04-17 02:20:36 +04:00
*/
2021-12-29 03:49:13 +03:00
# include <linux/compat.h>
2005-04-17 02:20:36 +04:00
# include <linux/workqueue.h>
# include <net/xfrm.h>
# include <linux/pfkeyv2.h>
# include <linux/ipsec.h>
# include <linux/module.h>
2006-08-24 14:08:07 +04:00
# include <linux/cache.h>
2007-12-21 07:49:33 +03:00
# include <linux/audit.h>
2016-12-24 22:46:01 +03:00
# include <linux/uaccess.h>
2009-11-09 07:58:41 +03: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 11:04:11 +03:00
# include <linux/slab.h>
2009-11-09 07:58:41 +03:00
# include <linux/interrupt.h>
# include <linux/kernel.h>
2005-04-17 02:20:36 +04:00
2019-06-24 23:04:48 +03:00
# include <crypto/aead.h>
2006-08-24 15:50:50 +04:00
# include "xfrm_hash.h"
2016-08-09 13:16:08 +03:00
# define xfrm_state_deref_prot(table, net) \
rcu_dereference_protected ( ( table ) , lockdep_is_held ( & ( net ) - > xfrm . xfrm_state_lock ) )
2016-08-23 17:00:12 +03:00
static void xfrm_state_gc_task ( struct work_struct * work ) ;
2005-04-17 02:20:36 +04: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 14:24:33 +04:00
2. Hash table by ( daddr , family , reqid ) to find what SAs exist for given
2005-04-17 02:20:36 +04:00
destination / tunnel endpoint . ( output )
*/
2006-08-24 14:08:07 +04:00
static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024 ;
2018-05-03 11:55:07 +03:00
static struct kmem_cache * xfrm_state_cache __ro_after_init ;
2006-08-24 14:08:07 +04:00
2016-08-23 17:00:12 +03:00
static DECLARE_WORK ( xfrm_state_gc_work , xfrm_state_gc_task ) ;
static HLIST_HEAD ( xfrm_state_gc_list ) ;
2016-08-09 13:16:05 +03:00
static inline bool xfrm_state_hold_rcu ( struct xfrm_state __rcu * x )
{
2017-07-04 15:53:21 +03:00
return refcount_inc_not_zero ( & x - > refcnt ) ;
2016-08-09 13:16:05 +03:00
}
2008-11-26 04:29:21 +03:00
static inline unsigned int xfrm_dst_hash ( struct net * net ,
2011-02-24 09:47:16 +03:00
const xfrm_address_t * daddr ,
const xfrm_address_t * saddr ,
2006-08-24 15:00:03 +04:00
u32 reqid ,
2006-08-24 14:24:33 +04:00
unsigned short family )
2006-08-24 14:08:07 +04:00
{
2008-11-26 04:29:21 +03:00
return __xfrm_dst_hash ( daddr , saddr , reqid , family , net - > xfrm . state_hmask ) ;
2006-08-24 14:08:07 +04:00
}
2008-11-26 04:29:21 +03:00
static inline unsigned int xfrm_src_hash ( struct net * net ,
2011-02-24 09:47:16 +03:00
const xfrm_address_t * daddr ,
const xfrm_address_t * saddr ,
2006-08-24 15:50:50 +04:00
unsigned short family )
2006-08-24 14:08:07 +04:00
{
2008-11-26 04:29:21 +03:00
return __xfrm_src_hash ( daddr , saddr , family , net - > xfrm . state_hmask ) ;
2006-08-24 14:08:07 +04:00
}
static inline unsigned int
2011-02-24 09:47:16 +03:00
xfrm_spi_hash ( struct net * net , const xfrm_address_t * daddr ,
__be32 spi , u8 proto , unsigned short family )
2006-08-24 14:08:07 +04:00
{
2008-11-26 04:29:21 +03:00
return __xfrm_spi_hash ( daddr , spi , proto , family , net - > xfrm . state_hmask ) ;
2006-08-24 14:08:07 +04:00
}
2021-04-25 22:47:12 +03:00
static unsigned int xfrm_seq_hash ( struct net * net , u32 seq )
{
return __xfrm_seq_hash ( seq , net - > xfrm . state_hmask ) ;
}
2022-12-02 21:41:32 +03:00
# define XFRM_STATE_INSERT(by, _n, _h, _type) \
{ \
struct xfrm_state * _x = NULL ; \
\
if ( _type ! = XFRM_DEV_OFFLOAD_PACKET ) { \
hlist_for_each_entry_rcu ( _x , _h , by ) { \
if ( _x - > xso . type = = XFRM_DEV_OFFLOAD_PACKET ) \
continue ; \
break ; \
} \
} \
\
if ( ! _x | | _x - > xso . type = = XFRM_DEV_OFFLOAD_PACKET ) \
/* SAD is empty or consist from HW SAs only */ \
hlist_add_head_rcu ( _n , _h ) ; \
else \
hlist_add_before_rcu ( _n , & _x - > by ) ; \
}
2006-08-24 14:08:07 +04:00
static void xfrm_hash_transfer ( struct hlist_head * list ,
struct hlist_head * ndsttable ,
struct hlist_head * nsrctable ,
struct hlist_head * nspitable ,
2021-04-25 22:47:12 +03:00
struct hlist_head * nseqtable ,
2006-08-24 14:08:07 +04:00
unsigned int nhashmask )
{
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
struct hlist_node * tmp ;
2006-08-24 14:08:07 +04:00
struct xfrm_state * x ;
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
hlist_for_each_entry_safe ( x , tmp , list , bydst ) {
2006-08-24 14:08:07 +04:00
unsigned int h ;
2006-08-24 15:00:03 +04:00
h = __xfrm_dst_hash ( & x - > id . daddr , & x - > props . saddr ,
x - > props . reqid , x - > props . family ,
nhashmask ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( bydst , & x - > bydst , ndsttable + h , x - > xso . type ) ;
2006-08-24 14:08:07 +04:00
2006-10-04 02:56:09 +04:00
h = __xfrm_src_hash ( & x - > id . daddr , & x - > props . saddr ,
x - > props . family ,
2006-08-24 14:08:07 +04:00
nhashmask ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( bysrc , & x - > bysrc , nsrctable + h , x - > xso . type ) ;
2006-08-24 14:08:07 +04:00
2006-09-28 09:21:52 +04:00
if ( x - > id . spi ) {
h = __xfrm_spi_hash ( & x - > id . daddr , x - > id . spi ,
x - > id . proto , x - > props . family ,
nhashmask ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( byspi , & x - > byspi , nspitable + h ,
x - > xso . type ) ;
2006-09-28 09:21:52 +04:00
}
2021-04-25 22:47:12 +03:00
if ( x - > km . seq ) {
h = __xfrm_seq_hash ( x - > km . seq , nhashmask ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( byseq , & x - > byseq , nseqtable + h ,
x - > xso . type ) ;
2021-04-25 22:47:12 +03:00
}
2006-08-24 14:08:07 +04:00
}
}
2008-11-26 04:19:07 +03:00
static unsigned long xfrm_hash_new_size ( unsigned int state_hmask )
2006-08-24 14:08:07 +04:00
{
2008-11-26 04:19:07 +03:00
return ( ( state_hmask + 1 ) < < 1 ) * sizeof ( struct hlist_head ) ;
2006-08-24 14:08:07 +04:00
}
2008-11-26 04:19:07 +03:00
static void xfrm_hash_resize ( struct work_struct * work )
2006-08-24 14:08:07 +04:00
{
2008-11-26 04:19:07 +03:00
struct net * net = container_of ( work , struct net , xfrm . state_hash_work ) ;
2021-04-25 22:47:12 +03:00
struct hlist_head * ndst , * nsrc , * nspi , * nseq , * odst , * osrc , * ospi , * oseq ;
2006-08-24 14:08:07 +04:00
unsigned long nsize , osize ;
unsigned int nhashmask , ohashmask ;
int i ;
2008-11-26 04:19:07 +03:00
nsize = xfrm_hash_new_size ( net - > xfrm . state_hmask ) ;
2006-08-24 15:50:50 +04:00
ndst = xfrm_hash_alloc ( nsize ) ;
2006-08-24 14:08:07 +04:00
if ( ! ndst )
2014-08-29 13:09:07 +04:00
return ;
2006-08-24 15:50:50 +04:00
nsrc = xfrm_hash_alloc ( nsize ) ;
2006-08-24 14:08:07 +04:00
if ( ! nsrc ) {
2006-08-24 15:50:50 +04:00
xfrm_hash_free ( ndst , nsize ) ;
2014-08-29 13:09:07 +04:00
return ;
2006-08-24 14:08:07 +04:00
}
2006-08-24 15:50:50 +04:00
nspi = xfrm_hash_alloc ( nsize ) ;
2006-08-24 14:08:07 +04:00
if ( ! nspi ) {
2006-08-24 15:50:50 +04:00
xfrm_hash_free ( ndst , nsize ) ;
xfrm_hash_free ( nsrc , nsize ) ;
2014-08-29 13:09:07 +04:00
return ;
2006-08-24 14:08:07 +04:00
}
2021-04-25 22:47:12 +03:00
nseq = xfrm_hash_alloc ( nsize ) ;
if ( ! nseq ) {
xfrm_hash_free ( ndst , nsize ) ;
xfrm_hash_free ( nsrc , nsize ) ;
xfrm_hash_free ( nspi , nsize ) ;
return ;
}
2006-08-24 14:08:07 +04:00
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2021-03-16 13:56:29 +03:00
write_seqcount_begin ( & net - > xfrm . xfrm_state_hash_generation ) ;
2006-08-24 14:08:07 +04:00
nhashmask = ( nsize / sizeof ( struct hlist_head ) ) - 1U ;
2016-08-09 13:16:08 +03:00
odst = xfrm_state_deref_prot ( net - > xfrm . state_bydst , net ) ;
2008-11-26 04:19:07 +03:00
for ( i = net - > xfrm . state_hmask ; i > = 0 ; i - - )
2021-04-25 22:47:12 +03:00
xfrm_hash_transfer ( odst + i , ndst , nsrc , nspi , nseq , nhashmask ) ;
2006-08-24 14:08:07 +04:00
2016-08-09 13:16:08 +03:00
osrc = xfrm_state_deref_prot ( net - > xfrm . state_bysrc , net ) ;
ospi = xfrm_state_deref_prot ( net - > xfrm . state_byspi , net ) ;
2021-04-25 22:47:12 +03:00
oseq = xfrm_state_deref_prot ( net - > xfrm . state_byseq , net ) ;
2008-11-26 04:19:07 +03:00
ohashmask = net - > xfrm . state_hmask ;
2006-08-24 14:08:07 +04:00
2016-08-09 13:16:08 +03:00
rcu_assign_pointer ( net - > xfrm . state_bydst , ndst ) ;
rcu_assign_pointer ( net - > xfrm . state_bysrc , nsrc ) ;
rcu_assign_pointer ( net - > xfrm . state_byspi , nspi ) ;
2021-04-25 22:47:12 +03:00
rcu_assign_pointer ( net - > xfrm . state_byseq , nseq ) ;
2008-11-26 04:19:07 +03:00
net - > xfrm . state_hmask = nhashmask ;
2006-08-24 14:08:07 +04:00
2021-03-16 13:56:29 +03:00
write_seqcount_end ( & net - > xfrm . xfrm_state_hash_generation ) ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2006-08-24 14:08:07 +04:00
osize = ( ohashmask + 1 ) * sizeof ( struct hlist_head ) ;
2016-08-09 13:16:06 +03:00
synchronize_rcu ( ) ;
2006-08-24 15:50:50 +04:00
xfrm_hash_free ( odst , osize ) ;
xfrm_hash_free ( osrc , osize ) ;
xfrm_hash_free ( ospi , osize ) ;
2021-04-25 22:47:12 +03:00
xfrm_hash_free ( oseq , osize ) ;
2006-08-24 14:08:07 +04:00
}
2013-01-16 12:05:05 +04:00
static DEFINE_SPINLOCK ( xfrm_state_afinfo_lock ) ;
static struct xfrm_state_afinfo __rcu * xfrm_state_afinfo [ NPROTO ] ;
2005-04-17 02:20:36 +04:00
static DEFINE_SPINLOCK ( xfrm_state_gc_lock ) ;
2006-03-21 06:17:03 +03:00
int __xfrm_state_delete ( struct xfrm_state * x ) ;
2005-04-17 02:20:36 +04: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 06:16:40 +03:00
int km_query ( struct xfrm_state * x , struct xfrm_tmpl * t , struct xfrm_policy * pol ) ;
2019-04-17 12:45:13 +03:00
static bool km_is_alive ( const struct km_event * c ) ;
2012-09-08 00:12:54 +04:00
void km_state_expired ( struct xfrm_state * x , int hard , u32 portid ) ;
2005-04-17 02:20:36 +04:00
2008-01-31 06:11:50 +03:00
int xfrm_register_type ( const struct xfrm_type * type , unsigned short family )
2007-10-18 08:31:12 +04:00
{
2013-01-17 12:34:11 +04:00
struct xfrm_state_afinfo * afinfo = xfrm_state_get_afinfo ( family ) ;
2007-10-18 08:31:12 +04:00
int err = 0 ;
2019-05-03 18:46:19 +03:00
if ( ! afinfo )
2007-10-18 08:31:12 +04:00
return - EAFNOSUPPORT ;
2019-05-03 18:46:19 +03:00
# define X(afi, T, name) do { \
WARN_ON ( ( afi ) - > type_ # # name ) ; \
( afi ) - > type_ # # name = ( T ) ; \
} while ( 0 )
switch ( type - > proto ) {
case IPPROTO_COMP :
X ( afinfo , type , comp ) ;
break ;
case IPPROTO_AH :
X ( afinfo , type , ah ) ;
break ;
case IPPROTO_ESP :
X ( afinfo , type , esp ) ;
break ;
case IPPROTO_IPIP :
X ( afinfo , type , ipip ) ;
break ;
case IPPROTO_DSTOPTS :
X ( afinfo , type , dstopts ) ;
break ;
case IPPROTO_ROUTING :
X ( afinfo , type , routing ) ;
break ;
case IPPROTO_IPV6 :
X ( afinfo , type , ipip6 ) ;
break ;
default :
WARN_ON ( 1 ) ;
err = - EPROTONOSUPPORT ;
break ;
}
# undef X
2017-01-09 16:20:47 +03:00
rcu_read_unlock ( ) ;
2007-10-18 08:31:12 +04:00
return err ;
}
EXPORT_SYMBOL ( xfrm_register_type ) ;
2019-05-03 18:46:19 +03:00
void xfrm_unregister_type ( const struct xfrm_type * type , unsigned short family )
2007-10-18 08:31:12 +04:00
{
2013-01-17 12:34:11 +04:00
struct xfrm_state_afinfo * afinfo = xfrm_state_get_afinfo ( family ) ;
2007-10-18 08:31:12 +04:00
if ( unlikely ( afinfo = = NULL ) )
2019-05-03 18:46:19 +03:00
return ;
# define X(afi, T, name) do { \
WARN_ON ( ( afi ) - > type_ # # name ! = ( T ) ) ; \
( afi ) - > type_ # # name = NULL ; \
} while ( 0 )
switch ( type - > proto ) {
case IPPROTO_COMP :
X ( afinfo , type , comp ) ;
break ;
case IPPROTO_AH :
X ( afinfo , type , ah ) ;
break ;
case IPPROTO_ESP :
X ( afinfo , type , esp ) ;
break ;
case IPPROTO_IPIP :
X ( afinfo , type , ipip ) ;
break ;
case IPPROTO_DSTOPTS :
X ( afinfo , type , dstopts ) ;
break ;
case IPPROTO_ROUTING :
X ( afinfo , type , routing ) ;
break ;
case IPPROTO_IPV6 :
X ( afinfo , type , ipip6 ) ;
break ;
default :
WARN_ON ( 1 ) ;
break ;
}
# undef X
2017-01-09 16:20:47 +03:00
rcu_read_unlock ( ) ;
2007-10-18 08:31:12 +04:00
}
EXPORT_SYMBOL ( xfrm_unregister_type ) ;
2008-01-31 06:11:50 +03:00
static const struct xfrm_type * xfrm_get_type ( u8 proto , unsigned short family )
2007-10-18 08:31:12 +04:00
{
2019-05-03 18:46:19 +03:00
const struct xfrm_type * type = NULL ;
2007-10-18 08:31:12 +04:00
struct xfrm_state_afinfo * afinfo ;
int modload_attempted = 0 ;
retry :
afinfo = xfrm_state_get_afinfo ( family ) ;
if ( unlikely ( afinfo = = NULL ) )
return NULL ;
2019-05-03 18:46:19 +03:00
switch ( proto ) {
case IPPROTO_COMP :
type = afinfo - > type_comp ;
break ;
case IPPROTO_AH :
type = afinfo - > type_ah ;
break ;
case IPPROTO_ESP :
type = afinfo - > type_esp ;
break ;
case IPPROTO_IPIP :
type = afinfo - > type_ipip ;
break ;
case IPPROTO_DSTOPTS :
type = afinfo - > type_dstopts ;
break ;
case IPPROTO_ROUTING :
type = afinfo - > type_routing ;
break ;
case IPPROTO_IPV6 :
type = afinfo - > type_ipip6 ;
break ;
default :
break ;
}
2007-10-18 08:31:12 +04:00
if ( unlikely ( type & & ! try_module_get ( type - > owner ) ) )
type = NULL ;
2017-01-09 16:20:49 +03:00
rcu_read_unlock ( ) ;
2007-10-18 08:31:12 +04:00
if ( ! type & & ! modload_attempted ) {
request_module ( " xfrm-type-%d-%d " , family , proto ) ;
modload_attempted = 1 ;
goto retry ;
}
return type ;
}
2008-01-31 06:11:50 +03:00
static void xfrm_put_type ( const struct xfrm_type * type )
2007-10-18 08:31:12 +04:00
{
module_put ( type - > owner ) ;
}
2017-04-14 11:05:44 +03:00
int xfrm_register_type_offload ( const struct xfrm_type_offload * type ,
unsigned short family )
{
struct xfrm_state_afinfo * afinfo = xfrm_state_get_afinfo ( family ) ;
int err = 0 ;
if ( unlikely ( afinfo = = NULL ) )
return - EAFNOSUPPORT ;
2019-05-03 18:46:19 +03:00
switch ( type - > proto ) {
case IPPROTO_ESP :
WARN_ON ( afinfo - > type_offload_esp ) ;
afinfo - > type_offload_esp = type ;
break ;
default :
WARN_ON ( 1 ) ;
err = - EPROTONOSUPPORT ;
break ;
}
2017-04-14 11:05:44 +03:00
rcu_read_unlock ( ) ;
return err ;
}
EXPORT_SYMBOL ( xfrm_register_type_offload ) ;
2019-05-03 18:46:19 +03:00
void xfrm_unregister_type_offload ( const struct xfrm_type_offload * type ,
unsigned short family )
2017-04-14 11:05:44 +03:00
{
struct xfrm_state_afinfo * afinfo = xfrm_state_get_afinfo ( family ) ;
if ( unlikely ( afinfo = = NULL ) )
2019-05-03 18:46:19 +03:00
return ;
switch ( type - > proto ) {
case IPPROTO_ESP :
WARN_ON ( afinfo - > type_offload_esp ! = type ) ;
afinfo - > type_offload_esp = NULL ;
break ;
default :
WARN_ON ( 1 ) ;
break ;
}
2017-04-14 11:05:44 +03:00
rcu_read_unlock ( ) ;
}
EXPORT_SYMBOL ( xfrm_unregister_type_offload ) ;
2017-08-01 12:49:08 +03:00
static const struct xfrm_type_offload *
xfrm_get_type_offload ( u8 proto , unsigned short family , bool try_load )
2017-04-14 11:05:44 +03:00
{
2019-05-03 18:46:19 +03:00
const struct xfrm_type_offload * type = NULL ;
2017-04-14 11:05:44 +03:00
struct xfrm_state_afinfo * afinfo ;
2017-08-01 12:49:08 +03:00
retry :
2017-04-14 11:05:44 +03:00
afinfo = xfrm_state_get_afinfo ( family ) ;
if ( unlikely ( afinfo = = NULL ) )
return NULL ;
2019-05-03 18:46:19 +03:00
switch ( proto ) {
case IPPROTO_ESP :
type = afinfo - > type_offload_esp ;
break ;
default :
break ;
}
2017-04-14 11:05:44 +03:00
if ( ( type & & ! try_module_get ( type - > owner ) ) )
type = NULL ;
2017-12-31 18:18:56 +03:00
rcu_read_unlock ( ) ;
2017-08-01 12:49:08 +03:00
if ( ! type & & try_load ) {
request_module ( " xfrm-offload-%d-%d " , family , proto ) ;
2018-01-23 01:34:09 +03:00
try_load = false ;
2017-08-01 12:49:08 +03:00
goto retry ;
}
2017-04-14 11:05:44 +03:00
return type ;
}
static void xfrm_put_type_offload ( const struct xfrm_type_offload * type )
{
module_put ( type - > owner ) ;
}
2019-03-29 23:16:31 +03:00
static const struct xfrm_mode xfrm4_mode_map [ XFRM_MODE_MAX ] = {
[ XFRM_MODE_BEET ] = {
. encap = XFRM_MODE_BEET ,
. flags = XFRM_MODE_FLAG_TUNNEL ,
. family = AF_INET ,
} ,
[ XFRM_MODE_TRANSPORT ] = {
. encap = XFRM_MODE_TRANSPORT ,
. family = AF_INET ,
} ,
[ XFRM_MODE_TUNNEL ] = {
. encap = XFRM_MODE_TUNNEL ,
. flags = XFRM_MODE_FLAG_TUNNEL ,
. family = AF_INET ,
} ,
} ;
static const struct xfrm_mode xfrm6_mode_map [ XFRM_MODE_MAX ] = {
[ XFRM_MODE_BEET ] = {
. encap = XFRM_MODE_BEET ,
. flags = XFRM_MODE_FLAG_TUNNEL ,
. family = AF_INET6 ,
} ,
[ XFRM_MODE_ROUTEOPTIMIZATION ] = {
. encap = XFRM_MODE_ROUTEOPTIMIZATION ,
. family = AF_INET6 ,
} ,
[ XFRM_MODE_TRANSPORT ] = {
. encap = XFRM_MODE_TRANSPORT ,
. family = AF_INET6 ,
} ,
[ XFRM_MODE_TUNNEL ] = {
. encap = XFRM_MODE_TUNNEL ,
. flags = XFRM_MODE_FLAG_TUNNEL ,
. family = AF_INET6 ,
} ,
} ;
static const struct xfrm_mode * xfrm_get_mode ( unsigned int encap , int family )
{
const struct xfrm_mode * mode ;
2007-10-18 08:31:12 +04:00
if ( unlikely ( encap > = XFRM_MODE_MAX ) )
return NULL ;
2019-03-29 23:16:31 +03:00
switch ( family ) {
case AF_INET :
mode = & xfrm4_mode_map [ encap ] ;
if ( mode - > family = = family )
return mode ;
break ;
case AF_INET6 :
mode = & xfrm6_mode_map [ encap ] ;
if ( mode - > family = = family )
return mode ;
break ;
default :
break ;
2007-10-18 08:31:12 +04:00
}
2019-03-29 23:16:31 +03:00
return NULL ;
2007-10-18 08:31:12 +04:00
}
2018-11-21 23:09:23 +03:00
void xfrm_state_free ( struct xfrm_state * x )
{
kmem_cache_free ( xfrm_state_cache , x ) ;
}
EXPORT_SYMBOL ( xfrm_state_free ) ;
xfrm: destroy xfrm_state synchronously on net exit path
xfrm_state_put() moves struct xfrm_state to the GC list
and schedules the GC work to clean it up. On net exit call
path, xfrm_state_flush() is called to clean up and
xfrm_flush_gc() is called to wait for the GC work to complete
before exit.
However, this doesn't work because one of the ->destructor(),
ipcomp_destroy(), schedules the same GC work again inside
the GC work. It is hard to wait for such a nested async
callback. This is also why syzbot still reports the following
warning:
WARNING: CPU: 1 PID: 33 at net/ipv6/xfrm6_tunnel.c:351 xfrm6_tunnel_net_exit+0x2cb/0x500 net/ipv6/xfrm6_tunnel.c:351
...
ops_exit_list.isra.0+0xb0/0x160 net/core/net_namespace.c:153
cleanup_net+0x51d/0xb10 net/core/net_namespace.c:551
process_one_work+0xd0c/0x1ce0 kernel/workqueue.c:2153
worker_thread+0x143/0x14a0 kernel/workqueue.c:2296
kthread+0x357/0x430 kernel/kthread.c:246
ret_from_fork+0x3a/0x50 arch/x86/entry/entry_64.S:352
In fact, it is perfectly fine to bypass GC and destroy xfrm_state
synchronously on net exit call path, because it is in process context
and doesn't need a work struct to do any blocking work.
This patch introduces xfrm_state_put_sync() which simply bypasses
GC, and lets its callers to decide whether to use this synchronous
version. On net exit path, xfrm_state_fini() and
xfrm6_tunnel_net_exit() use it. And, as ipcomp_destroy() itself is
blocking, it can use xfrm_state_put_sync() directly too.
Also rename xfrm_state_gc_destroy() to ___xfrm_state_destroy() to
reflect this change.
Fixes: b48c05ab5d32 ("xfrm: Fix warning in xfrm6_tunnel_net_exit.")
Reported-and-tested-by: syzbot+e9aebef558e3ed673934@syzkaller.appspotmail.com
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2019-02-01 00:05:49 +03:00
static void ___xfrm_state_destroy ( struct xfrm_state * x )
2005-04-17 02:20:36 +04:00
{
2019-03-02 01:48:20 +03:00
hrtimer_cancel ( & x - > mtimer ) ;
2006-08-24 14:54:22 +04:00
del_timer_sync ( & x - > rtimer ) ;
2016-09-18 10:42:53 +03:00
kfree ( x - > aead ) ;
2005-11-08 20:41:34 +03:00
kfree ( x - > aalg ) ;
kfree ( x - > ealg ) ;
kfree ( x - > calg ) ;
kfree ( x - > encap ) ;
2006-08-24 05:18:55 +04:00
kfree ( x - > coaddr ) ;
2011-03-08 03:10:27 +03:00
kfree ( x - > replay_esn ) ;
kfree ( x - > preplay_esn ) ;
2017-04-14 11:05:44 +03:00
if ( x - > type_offload )
xfrm_put_type_offload ( x - > type_offload ) ;
2005-04-17 02:20:36 +04:00
if ( x - > type ) {
x - > type - > destructor ( x ) ;
xfrm_put_type ( x - > type ) ;
}
2019-11-06 10:13:49 +03:00
if ( x - > xfrag . page )
put_page ( x - > xfrag . page ) ;
2017-04-14 11:06:10 +03:00
xfrm_dev_state_free ( x ) ;
[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 10:12:27 +03:00
security_xfrm_state_free ( x ) ;
2018-11-21 23:09:23 +03:00
xfrm_state_free ( x ) ;
2005-04-17 02:20:36 +04:00
}
2008-11-26 04:20:36 +03:00
static void xfrm_state_gc_task ( struct work_struct * work )
2005-04-17 02:20:36 +04:00
{
2008-10-01 18:03:24 +04:00
struct xfrm_state * x ;
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
struct hlist_node * tmp ;
2008-10-01 18:03:24 +04:00
struct hlist_head gc_list ;
2005-04-17 02:20:36 +04:00
spin_lock_bh ( & xfrm_state_gc_lock ) ;
2016-08-23 17:00:12 +03:00
hlist_move_list ( & xfrm_state_gc_list , & gc_list ) ;
2005-04-17 02:20:36 +04:00
spin_unlock_bh ( & xfrm_state_gc_lock ) ;
2016-08-09 13:16:06 +03:00
synchronize_rcu ( ) ;
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
hlist_for_each_entry_safe ( x , tmp , & gc_list , gclist )
xfrm: destroy xfrm_state synchronously on net exit path
xfrm_state_put() moves struct xfrm_state to the GC list
and schedules the GC work to clean it up. On net exit call
path, xfrm_state_flush() is called to clean up and
xfrm_flush_gc() is called to wait for the GC work to complete
before exit.
However, this doesn't work because one of the ->destructor(),
ipcomp_destroy(), schedules the same GC work again inside
the GC work. It is hard to wait for such a nested async
callback. This is also why syzbot still reports the following
warning:
WARNING: CPU: 1 PID: 33 at net/ipv6/xfrm6_tunnel.c:351 xfrm6_tunnel_net_exit+0x2cb/0x500 net/ipv6/xfrm6_tunnel.c:351
...
ops_exit_list.isra.0+0xb0/0x160 net/core/net_namespace.c:153
cleanup_net+0x51d/0xb10 net/core/net_namespace.c:551
process_one_work+0xd0c/0x1ce0 kernel/workqueue.c:2153
worker_thread+0x143/0x14a0 kernel/workqueue.c:2296
kthread+0x357/0x430 kernel/kthread.c:246
ret_from_fork+0x3a/0x50 arch/x86/entry/entry_64.S:352
In fact, it is perfectly fine to bypass GC and destroy xfrm_state
synchronously on net exit call path, because it is in process context
and doesn't need a work struct to do any blocking work.
This patch introduces xfrm_state_put_sync() which simply bypasses
GC, and lets its callers to decide whether to use this synchronous
version. On net exit path, xfrm_state_fini() and
xfrm6_tunnel_net_exit() use it. And, as ipcomp_destroy() itself is
blocking, it can use xfrm_state_put_sync() directly too.
Also rename xfrm_state_gc_destroy() to ___xfrm_state_destroy() to
reflect this change.
Fixes: b48c05ab5d32 ("xfrm: Fix warning in xfrm6_tunnel_net_exit.")
Reported-and-tested-by: syzbot+e9aebef558e3ed673934@syzkaller.appspotmail.com
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2019-02-01 00:05:49 +03:00
___xfrm_state_destroy ( x ) ;
2005-04-17 02:20:36 +04:00
}
2013-12-24 05:43:47 +04:00
static enum hrtimer_restart xfrm_timer_handler ( struct hrtimer * me )
2005-04-17 02:20:36 +04:00
{
2019-03-02 01:48:20 +03:00
struct xfrm_state * x = container_of ( me , struct xfrm_state , mtimer ) ;
enum hrtimer_restart ret = HRTIMER_NORESTART ;
2018-07-11 13:19:13 +03:00
time64_t now = ktime_get_real_seconds ( ) ;
time64_t next = TIME64_MAX ;
2005-04-17 02:20:36 +04:00
int warn = 0 ;
2006-11-27 22:11:54 +03:00
int err = 0 ;
2005-04-17 02:20:36 +04:00
spin_lock ( & x - > lock ) ;
2023-10-03 20:57:20 +03:00
xfrm_dev_state_update_stats ( x ) ;
2022-12-02 21:41:33 +03:00
2005-04-17 02:20:36 +04:00
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 ) {
2023-01-26 14:21:29 +03:00
time64_t tmo = x - > lft . hard_add_expires_seconds +
2005-04-17 02:20:36 +04:00
x - > curlft . add_time - now ;
2012-07-31 01:43:54 +04:00
if ( tmo < = 0 ) {
if ( x - > xflags & XFRM_SOFT_EXPIRE ) {
/* enter hard expire without soft expire first?!
* setting a new date could trigger this .
2017-01-03 19:13:20 +03:00
* workaround : fix x - > curflt . add_time by below :
2012-07-31 01:43:54 +04:00
*/
x - > curlft . add_time = now - x - > saved_tmo - 1 ;
tmo = x - > lft . hard_add_expires_seconds - x - > saved_tmo ;
} else
goto expired ;
}
2005-04-17 02:20:36 +04:00
if ( tmo < next )
next = tmo ;
}
if ( x - > lft . hard_use_expires_seconds ) {
2023-01-26 14:21:29 +03:00
time64_t tmo = x - > lft . hard_use_expires_seconds +
2023-01-26 14:21:30 +03:00
( READ_ONCE ( x - > curlft . use_time ) ? : now ) - now ;
2005-04-17 02:20:36 +04:00
if ( tmo < = 0 )
goto expired ;
if ( tmo < next )
next = tmo ;
}
if ( x - > km . dying )
goto resched ;
if ( x - > lft . soft_add_expires_seconds ) {
2023-01-26 14:21:29 +03:00
time64_t tmo = x - > lft . soft_add_expires_seconds +
2005-04-17 02:20:36 +04:00
x - > curlft . add_time - now ;
2012-07-31 01:43:54 +04:00
if ( tmo < = 0 ) {
2005-04-17 02:20:36 +04:00
warn = 1 ;
2012-07-31 01:43:54 +04:00
x - > xflags & = ~ XFRM_SOFT_EXPIRE ;
} else if ( tmo < next ) {
2005-04-17 02:20:36 +04:00
next = tmo ;
2012-07-31 01:43:54 +04:00
x - > xflags | = XFRM_SOFT_EXPIRE ;
x - > saved_tmo = tmo ;
}
2005-04-17 02:20:36 +04:00
}
if ( x - > lft . soft_use_expires_seconds ) {
2023-01-26 14:21:29 +03:00
time64_t tmo = x - > lft . soft_use_expires_seconds +
2023-01-26 14:21:30 +03:00
( READ_ONCE ( x - > curlft . use_time ) ? : now ) - now ;
2005-04-17 02:20:36 +04:00
if ( tmo < = 0 )
warn = 1 ;
else if ( tmo < next )
next = tmo ;
}
2005-06-19 09:43:22 +04:00
x - > km . dying = warn ;
2005-04-17 02:20:36 +04:00
if ( warn )
2006-03-21 06:17:03 +03:00
km_state_expired ( x , 0 , 0 ) ;
2005-04-17 02:20:36 +04:00
resched :
2018-07-11 13:19:13 +03:00
if ( next ! = TIME64_MAX ) {
2019-03-02 01:48:20 +03:00
hrtimer_forward_now ( & x - > mtimer , ktime_set ( next , 0 ) ) ;
ret = HRTIMER_RESTART ;
2009-11-09 07:58:41 +03:00
}
2006-08-24 14:54:22 +04:00
2005-04-17 02:20:36 +04:00
goto out ;
expired :
2013-08-27 15:43:30 +04:00
if ( x - > km . state = = XFRM_STATE_ACQ & & x - > id . spi = = 0 )
2005-04-17 02:20:36 +04:00
x - > km . state = XFRM_STATE_EXPIRED ;
2006-11-27 22:11:54 +03:00
err = __xfrm_state_delete ( x ) ;
2013-08-23 17:46:08 +04:00
if ( ! err )
2006-03-21 06:17:03 +03:00
km_state_expired ( x , 1 , 0 ) ;
2005-04-17 02:20:36 +04:00
2014-04-22 16:48:30 +04:00
xfrm_audit_state_delete ( x , err ? 0 : 1 , true ) ;
2006-11-27 22:11:54 +03:00
2005-04-17 02:20:36 +04:00
out :
spin_unlock ( & x - > lock ) ;
2019-03-02 01:48:20 +03:00
return ret ;
2005-04-17 02:20:36 +04:00
}
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 00:43:17 +03:00
static void xfrm_replay_timer_handler ( struct timer_list * t ) ;
2006-03-21 06:18:23 +03:00
2008-11-26 04:15:16 +03:00
struct xfrm_state * xfrm_state_alloc ( struct net * net )
2005-04-17 02:20:36 +04:00
{
struct xfrm_state * x ;
2020-02-12 12:54:36 +03:00
x = kmem_cache_zalloc ( xfrm_state_cache , GFP_ATOMIC ) ;
2005-04-17 02:20:36 +04:00
if ( x ) {
2008-11-26 04:15:16 +03:00
write_pnet ( & x - > xs_net , net ) ;
2017-07-04 15:53:21 +03:00
refcount_set ( & x - > refcnt , 1 ) ;
2005-04-17 02:20:36 +04:00
atomic_set ( & x - > tunnel_users , 0 ) ;
2008-10-01 18:03:24 +04:00
INIT_LIST_HEAD ( & x - > km . all ) ;
2006-08-24 13:45:07 +04:00
INIT_HLIST_NODE ( & x - > bydst ) ;
INIT_HLIST_NODE ( & x - > bysrc ) ;
INIT_HLIST_NODE ( & x - > byspi ) ;
2021-04-25 22:47:12 +03:00
INIT_HLIST_NODE ( & x - > byseq ) ;
2019-03-02 01:48:20 +03:00
hrtimer_init ( & x - > mtimer , CLOCK_BOOTTIME , HRTIMER_MODE_ABS_SOFT ) ;
x - > mtimer . function = xfrm_timer_handler ;
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 00:43:17 +03:00
timer_setup ( & x - > rtimer , xfrm_replay_timer_handler , 0 ) ;
2018-07-11 13:19:13 +03:00
x - > curlft . add_time = ktime_get_real_seconds ( ) ;
2005-04-17 02:20:36 +04: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 06:15:11 +03:00
x - > replay_maxage = 0 ;
x - > replay_maxdiff = 0 ;
2005-04-17 02:20:36 +04:00
spin_lock_init ( & x - > lock ) ;
}
return x ;
}
EXPORT_SYMBOL ( xfrm_state_alloc ) ;
xfrm: destroy xfrm_state synchronously on net exit path
xfrm_state_put() moves struct xfrm_state to the GC list
and schedules the GC work to clean it up. On net exit call
path, xfrm_state_flush() is called to clean up and
xfrm_flush_gc() is called to wait for the GC work to complete
before exit.
However, this doesn't work because one of the ->destructor(),
ipcomp_destroy(), schedules the same GC work again inside
the GC work. It is hard to wait for such a nested async
callback. This is also why syzbot still reports the following
warning:
WARNING: CPU: 1 PID: 33 at net/ipv6/xfrm6_tunnel.c:351 xfrm6_tunnel_net_exit+0x2cb/0x500 net/ipv6/xfrm6_tunnel.c:351
...
ops_exit_list.isra.0+0xb0/0x160 net/core/net_namespace.c:153
cleanup_net+0x51d/0xb10 net/core/net_namespace.c:551
process_one_work+0xd0c/0x1ce0 kernel/workqueue.c:2153
worker_thread+0x143/0x14a0 kernel/workqueue.c:2296
kthread+0x357/0x430 kernel/kthread.c:246
ret_from_fork+0x3a/0x50 arch/x86/entry/entry_64.S:352
In fact, it is perfectly fine to bypass GC and destroy xfrm_state
synchronously on net exit call path, because it is in process context
and doesn't need a work struct to do any blocking work.
This patch introduces xfrm_state_put_sync() which simply bypasses
GC, and lets its callers to decide whether to use this synchronous
version. On net exit path, xfrm_state_fini() and
xfrm6_tunnel_net_exit() use it. And, as ipcomp_destroy() itself is
blocking, it can use xfrm_state_put_sync() directly too.
Also rename xfrm_state_gc_destroy() to ___xfrm_state_destroy() to
reflect this change.
Fixes: b48c05ab5d32 ("xfrm: Fix warning in xfrm6_tunnel_net_exit.")
Reported-and-tested-by: syzbot+e9aebef558e3ed673934@syzkaller.appspotmail.com
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2019-02-01 00:05:49 +03:00
void __xfrm_state_destroy ( struct xfrm_state * x , bool sync )
2005-04-17 02:20:36 +04:00
{
2008-07-26 08:43:18 +04:00
WARN_ON ( x - > km . state ! = XFRM_STATE_DEAD ) ;
2005-04-17 02:20:36 +04:00
xfrm: destroy xfrm_state synchronously on net exit path
xfrm_state_put() moves struct xfrm_state to the GC list
and schedules the GC work to clean it up. On net exit call
path, xfrm_state_flush() is called to clean up and
xfrm_flush_gc() is called to wait for the GC work to complete
before exit.
However, this doesn't work because one of the ->destructor(),
ipcomp_destroy(), schedules the same GC work again inside
the GC work. It is hard to wait for such a nested async
callback. This is also why syzbot still reports the following
warning:
WARNING: CPU: 1 PID: 33 at net/ipv6/xfrm6_tunnel.c:351 xfrm6_tunnel_net_exit+0x2cb/0x500 net/ipv6/xfrm6_tunnel.c:351
...
ops_exit_list.isra.0+0xb0/0x160 net/core/net_namespace.c:153
cleanup_net+0x51d/0xb10 net/core/net_namespace.c:551
process_one_work+0xd0c/0x1ce0 kernel/workqueue.c:2153
worker_thread+0x143/0x14a0 kernel/workqueue.c:2296
kthread+0x357/0x430 kernel/kthread.c:246
ret_from_fork+0x3a/0x50 arch/x86/entry/entry_64.S:352
In fact, it is perfectly fine to bypass GC and destroy xfrm_state
synchronously on net exit call path, because it is in process context
and doesn't need a work struct to do any blocking work.
This patch introduces xfrm_state_put_sync() which simply bypasses
GC, and lets its callers to decide whether to use this synchronous
version. On net exit path, xfrm_state_fini() and
xfrm6_tunnel_net_exit() use it. And, as ipcomp_destroy() itself is
blocking, it can use xfrm_state_put_sync() directly too.
Also rename xfrm_state_gc_destroy() to ___xfrm_state_destroy() to
reflect this change.
Fixes: b48c05ab5d32 ("xfrm: Fix warning in xfrm6_tunnel_net_exit.")
Reported-and-tested-by: syzbot+e9aebef558e3ed673934@syzkaller.appspotmail.com
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2019-02-01 00:05:49 +03:00
if ( sync ) {
synchronize_rcu ( ) ;
___xfrm_state_destroy ( x ) ;
} else {
spin_lock_bh ( & xfrm_state_gc_lock ) ;
hlist_add_head ( & x - > gclist , & xfrm_state_gc_list ) ;
spin_unlock_bh ( & xfrm_state_gc_lock ) ;
schedule_work ( & xfrm_state_gc_work ) ;
}
2005-04-17 02:20:36 +04:00
}
EXPORT_SYMBOL ( __xfrm_state_destroy ) ;
2006-03-21 06:17:03 +03:00
int __xfrm_state_delete ( struct xfrm_state * x )
2005-04-17 02:20:36 +04:00
{
2008-11-26 04:29:47 +03:00
struct net * net = xs_net ( x ) ;
2005-06-19 09:42:13 +04:00
int err = - ESRCH ;
2005-04-17 02:20:36 +04:00
if ( x - > km . state ! = XFRM_STATE_DEAD ) {
x - > km . state = XFRM_STATE_DEAD ;
2013-11-07 13:47:50 +04:00
spin_lock ( & net - > xfrm . xfrm_state_lock ) ;
2008-10-01 18:03:24 +04:00
list_del ( & x - > km . all ) ;
2016-08-09 13:16:04 +03:00
hlist_del_rcu ( & x - > bydst ) ;
hlist_del_rcu ( & x - > bysrc ) ;
2021-04-25 22:47:12 +03:00
if ( x - > km . seq )
hlist_del_rcu ( & x - > byseq ) ;
2006-08-24 14:54:22 +04:00
if ( x - > id . spi )
2016-08-09 13:16:04 +03:00
hlist_del_rcu ( & x - > byspi ) ;
2008-11-26 04:29:47 +03:00
net - > xfrm . state_num - - ;
2013-11-07 13:47:50 +04:00
spin_unlock ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
2019-11-25 16:49:02 +03:00
if ( x - > encap_sk )
sock_put ( rcu_dereference_raw ( x - > encap_sk ) ) ;
2017-04-14 11:06:10 +03:00
xfrm_dev_state_delete ( x ) ;
2005-04-17 02:20:36 +04:00
/* 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 06:10:07 +03:00
xfrm_state_put ( x ) ;
2005-06-19 09:42:13 +04:00
err = 0 ;
2005-04-17 02:20:36 +04:00
}
2005-06-19 09:42:13 +04:00
return err ;
2005-04-17 02:20:36 +04:00
}
2006-03-21 06:17:03 +03:00
EXPORT_SYMBOL ( __xfrm_state_delete ) ;
2005-04-17 02:20:36 +04:00
2005-06-19 09:42:13 +04:00
int xfrm_state_delete ( struct xfrm_state * x )
2005-04-17 02:20:36 +04:00
{
2005-06-19 09:42:13 +04:00
int err ;
2005-04-17 02:20:36 +04:00
spin_lock_bh ( & x - > lock ) ;
2005-06-19 09:42:13 +04:00
err = __xfrm_state_delete ( x ) ;
2005-04-17 02:20:36 +04:00
spin_unlock_bh ( & x - > lock ) ;
2005-06-19 09:42:13 +04:00
return err ;
2005-04-17 02:20:36 +04:00
}
EXPORT_SYMBOL ( xfrm_state_delete ) ;
2007-06-05 03:05:57 +04:00
# ifdef CONFIG_SECURITY_NETWORK_XFRM
static inline int
2014-04-22 16:48:30 +04:00
xfrm_state_flush_secctx_check ( struct net * net , u8 proto , bool task_valid )
2005-04-17 02:20:36 +04:00
{
2007-06-05 03:05:57 +04:00
int i , err = 0 ;
2008-11-26 04:30:18 +03:00
for ( i = 0 ; i < = net - > xfrm . state_hmask ; i + + ) {
2007-06-05 03:05:57 +04:00
struct xfrm_state * x ;
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
hlist_for_each_entry ( x , net - > xfrm . state_bydst + i , bydst ) {
2007-06-05 03:05:57 +04:00
if ( xfrm_id_proto_match ( x - > id . proto , proto ) & &
( err = security_xfrm_state_delete ( x ) ) ! = 0 ) {
2014-04-22 16:48:30 +04:00
xfrm_audit_state_delete ( x , 0 , task_valid ) ;
2007-06-05 03:05:57 +04:00
return err ;
}
}
}
return err ;
}
2017-04-14 11:06:10 +03:00
static inline int
xfrm_dev_state_flush_secctx_check ( struct net * net , struct net_device * dev , bool task_valid )
{
int i , err = 0 ;
for ( i = 0 ; i < = net - > xfrm . state_hmask ; i + + ) {
struct xfrm_state * x ;
2022-05-05 13:06:40 +03:00
struct xfrm_dev_offload * xso ;
2017-04-14 11:06:10 +03:00
hlist_for_each_entry ( x , net - > xfrm . state_bydst + i , bydst ) {
xso = & x - > xso ;
if ( xso - > dev = = dev & &
( err = security_xfrm_state_delete ( x ) ) ! = 0 ) {
xfrm_audit_state_delete ( x , 0 , task_valid ) ;
return err ;
}
}
}
return err ;
}
2007-06-05 03:05:57 +04:00
# else
static inline int
2014-04-22 16:48:30 +04:00
xfrm_state_flush_secctx_check ( struct net * net , u8 proto , bool task_valid )
2007-06-05 03:05:57 +04:00
{
return 0 ;
}
2017-04-14 11:06:10 +03:00
static inline int
xfrm_dev_state_flush_secctx_check ( struct net * net , struct net_device * dev , bool task_valid )
{
return 0 ;
}
2007-06-05 03:05:57 +04:00
# endif
xfrm: destroy xfrm_state synchronously on net exit path
xfrm_state_put() moves struct xfrm_state to the GC list
and schedules the GC work to clean it up. On net exit call
path, xfrm_state_flush() is called to clean up and
xfrm_flush_gc() is called to wait for the GC work to complete
before exit.
However, this doesn't work because one of the ->destructor(),
ipcomp_destroy(), schedules the same GC work again inside
the GC work. It is hard to wait for such a nested async
callback. This is also why syzbot still reports the following
warning:
WARNING: CPU: 1 PID: 33 at net/ipv6/xfrm6_tunnel.c:351 xfrm6_tunnel_net_exit+0x2cb/0x500 net/ipv6/xfrm6_tunnel.c:351
...
ops_exit_list.isra.0+0xb0/0x160 net/core/net_namespace.c:153
cleanup_net+0x51d/0xb10 net/core/net_namespace.c:551
process_one_work+0xd0c/0x1ce0 kernel/workqueue.c:2153
worker_thread+0x143/0x14a0 kernel/workqueue.c:2296
kthread+0x357/0x430 kernel/kthread.c:246
ret_from_fork+0x3a/0x50 arch/x86/entry/entry_64.S:352
In fact, it is perfectly fine to bypass GC and destroy xfrm_state
synchronously on net exit call path, because it is in process context
and doesn't need a work struct to do any blocking work.
This patch introduces xfrm_state_put_sync() which simply bypasses
GC, and lets its callers to decide whether to use this synchronous
version. On net exit path, xfrm_state_fini() and
xfrm6_tunnel_net_exit() use it. And, as ipcomp_destroy() itself is
blocking, it can use xfrm_state_put_sync() directly too.
Also rename xfrm_state_gc_destroy() to ___xfrm_state_destroy() to
reflect this change.
Fixes: b48c05ab5d32 ("xfrm: Fix warning in xfrm6_tunnel_net_exit.")
Reported-and-tested-by: syzbot+e9aebef558e3ed673934@syzkaller.appspotmail.com
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2019-02-01 00:05:49 +03:00
int xfrm_state_flush ( struct net * net , u8 proto , bool task_valid , bool sync )
2007-06-05 03:05:57 +04:00
{
2010-02-19 05:00:41 +03:00
int i , err = 0 , cnt = 0 ;
2005-04-17 02:20:36 +04:00
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2014-04-22 16:48:30 +04:00
err = xfrm_state_flush_secctx_check ( net , proto , task_valid ) ;
2007-06-05 03:05:57 +04:00
if ( err )
goto out ;
2010-02-19 05:00:41 +03:00
err = - ESRCH ;
2008-11-26 04:30:18 +03:00
for ( i = 0 ; i < = net - > xfrm . state_hmask ; i + + ) {
2006-08-24 13:45:07 +04:00
struct xfrm_state * x ;
2005-04-17 02:20:36 +04:00
restart :
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
hlist_for_each_entry ( x , net - > xfrm . state_bydst + i , bydst ) {
2005-04-17 02:20:36 +04:00
if ( ! xfrm_state_kern ( x ) & &
2006-09-23 02:06:24 +04:00
xfrm_id_proto_match ( x - > id . proto , proto ) ) {
2005-04-17 02:20:36 +04:00
xfrm_state_hold ( x ) ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
2006-11-27 22:11:54 +03:00
err = xfrm_state_delete ( x ) ;
2007-09-17 22:51:22 +04:00
xfrm_audit_state_delete ( x , err ? 0 : 1 ,
2014-04-22 16:48:30 +04:00
task_valid ) ;
xfrm: destroy xfrm_state synchronously on net exit path
xfrm_state_put() moves struct xfrm_state to the GC list
and schedules the GC work to clean it up. On net exit call
path, xfrm_state_flush() is called to clean up and
xfrm_flush_gc() is called to wait for the GC work to complete
before exit.
However, this doesn't work because one of the ->destructor(),
ipcomp_destroy(), schedules the same GC work again inside
the GC work. It is hard to wait for such a nested async
callback. This is also why syzbot still reports the following
warning:
WARNING: CPU: 1 PID: 33 at net/ipv6/xfrm6_tunnel.c:351 xfrm6_tunnel_net_exit+0x2cb/0x500 net/ipv6/xfrm6_tunnel.c:351
...
ops_exit_list.isra.0+0xb0/0x160 net/core/net_namespace.c:153
cleanup_net+0x51d/0xb10 net/core/net_namespace.c:551
process_one_work+0xd0c/0x1ce0 kernel/workqueue.c:2153
worker_thread+0x143/0x14a0 kernel/workqueue.c:2296
kthread+0x357/0x430 kernel/kthread.c:246
ret_from_fork+0x3a/0x50 arch/x86/entry/entry_64.S:352
In fact, it is perfectly fine to bypass GC and destroy xfrm_state
synchronously on net exit call path, because it is in process context
and doesn't need a work struct to do any blocking work.
This patch introduces xfrm_state_put_sync() which simply bypasses
GC, and lets its callers to decide whether to use this synchronous
version. On net exit path, xfrm_state_fini() and
xfrm6_tunnel_net_exit() use it. And, as ipcomp_destroy() itself is
blocking, it can use xfrm_state_put_sync() directly too.
Also rename xfrm_state_gc_destroy() to ___xfrm_state_destroy() to
reflect this change.
Fixes: b48c05ab5d32 ("xfrm: Fix warning in xfrm6_tunnel_net_exit.")
Reported-and-tested-by: syzbot+e9aebef558e3ed673934@syzkaller.appspotmail.com
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2019-02-01 00:05:49 +03:00
if ( sync )
xfrm_state_put_sync ( x ) ;
else
xfrm_state_put ( x ) ;
2010-02-19 05:00:41 +03:00
if ( ! err )
cnt + + ;
2005-04-17 02:20:36 +04:00
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
goto restart ;
}
}
}
2017-09-27 15:25:37 +03:00
out :
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
xfrm: policy: remove pcpu policy cache
Kristian Evensen says:
In a project I am involved in, we are running ipsec (Strongswan) on
different mt7621-based routers. Each router is configured as an
initiator and has around ~30 tunnels to different responders (running
on misc. devices). Before the flow cache was removed (kernel 4.9), we
got a combined throughput of around 70Mbit/s for all tunnels on one
router. However, we recently switched to kernel 4.14 (4.14.48), and
the total throughput is somewhere around 57Mbit/s (best-case). I.e., a
drop of around 20%. Reverting the flow cache removal restores, as
expected, performance levels to that of kernel 4.9.
When pcpu xdst exists, it has to be validated first before it can be
used.
A negative hit thus increases cost vs. no-cache.
As number of tunnels increases, hit rate decreases so this pcpu caching
isn't a viable strategy.
Furthermore, the xdst cache also needs to run with BH off, so when
removing this the bh disable/enable pairs can be removed too.
Kristian tested a 4.14.y backport of this change and reported
increased performance:
In our tests, the throughput reduction has been reduced from around -20%
to -5%. We also see that the overall throughput is independent of the
number of tunnels, while before the throughput was reduced as the number
of tunnels increased.
Reported-by: Kristian Evensen <kristian.evensen@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2018-06-25 18:26:02 +03:00
if ( cnt )
2010-02-19 05:00:41 +03:00
err = 0 ;
xfrm: policy: remove pcpu policy cache
Kristian Evensen says:
In a project I am involved in, we are running ipsec (Strongswan) on
different mt7621-based routers. Each router is configured as an
initiator and has around ~30 tunnels to different responders (running
on misc. devices). Before the flow cache was removed (kernel 4.9), we
got a combined throughput of around 70Mbit/s for all tunnels on one
router. However, we recently switched to kernel 4.14 (4.14.48), and
the total throughput is somewhere around 57Mbit/s (best-case). I.e., a
drop of around 20%. Reverting the flow cache removal restores, as
expected, performance levels to that of kernel 4.9.
When pcpu xdst exists, it has to be validated first before it can be
used.
A negative hit thus increases cost vs. no-cache.
As number of tunnels increases, hit rate decreases so this pcpu caching
isn't a viable strategy.
Furthermore, the xdst cache also needs to run with BH off, so when
removing this the bh disable/enable pairs can be removed too.
Kristian tested a 4.14.y backport of this change and reported
increased performance:
In our tests, the throughput reduction has been reduced from around -20%
to -5%. We also see that the overall throughput is independent of the
number of tunnels, while before the throughput was reduced as the number
of tunnels increased.
Reported-by: Kristian Evensen <kristian.evensen@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2018-06-25 18:26:02 +03:00
2007-06-05 03:05:57 +04:00
return err ;
2005-04-17 02:20:36 +04:00
}
EXPORT_SYMBOL ( xfrm_state_flush ) ;
2017-04-14 11:06:10 +03:00
int xfrm_dev_state_flush ( struct net * net , struct net_device * dev , bool task_valid )
{
int i , err = 0 , cnt = 0 ;
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
err = xfrm_dev_state_flush_secctx_check ( net , dev , task_valid ) ;
if ( err )
goto out ;
err = - ESRCH ;
for ( i = 0 ; i < = net - > xfrm . state_hmask ; i + + ) {
struct xfrm_state * x ;
2022-05-05 13:06:40 +03:00
struct xfrm_dev_offload * xso ;
2017-04-14 11:06:10 +03:00
restart :
hlist_for_each_entry ( x , net - > xfrm . state_bydst + i , bydst ) {
xso = & x - > xso ;
if ( ! xfrm_state_kern ( x ) & & xso - > dev = = dev ) {
xfrm_state_hold ( x ) ;
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
err = xfrm_state_delete ( x ) ;
xfrm_audit_state_delete ( x , err ? 0 : 1 ,
task_valid ) ;
xfrm_state_put ( x ) ;
if ( ! err )
cnt + + ;
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
goto restart ;
}
}
}
if ( cnt )
err = 0 ;
out :
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
return err ;
}
EXPORT_SYMBOL ( xfrm_dev_state_flush ) ;
2010-01-23 16:37:10 +03:00
void xfrm_sad_getinfo ( struct net * net , struct xfrmk_sadinfo * si )
2007-04-26 11:10:29 +04:00
{
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2010-01-23 16:37:10 +03:00
si - > sadcnt = net - > xfrm . state_num ;
2018-11-05 11:00:53 +03:00
si - > sadhcnt = net - > xfrm . state_hmask + 1 ;
2007-04-26 11:10:29 +04:00
si - > sadhmcnt = xfrm_state_hashmax ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2007-04-26 11:10:29 +04:00
}
EXPORT_SYMBOL ( xfrm_sad_getinfo ) ;
2019-05-03 18:46:14 +03:00
static void
__xfrm4_init_tempsel ( struct xfrm_selector * sel , const struct flowi * fl )
{
const struct flowi4 * fl4 = & fl - > u . ip4 ;
sel - > daddr . a4 = fl4 - > daddr ;
sel - > saddr . a4 = fl4 - > saddr ;
sel - > dport = xfrm_flowi_dport ( fl , & fl4 - > uli ) ;
sel - > dport_mask = htons ( 0xffff ) ;
sel - > sport = xfrm_flowi_sport ( fl , & fl4 - > uli ) ;
sel - > sport_mask = htons ( 0xffff ) ;
sel - > family = AF_INET ;
sel - > prefixlen_d = 32 ;
sel - > prefixlen_s = 32 ;
sel - > proto = fl4 - > flowi4_proto ;
sel - > ifindex = fl4 - > flowi4_oif ;
}
static void
__xfrm6_init_tempsel ( struct xfrm_selector * sel , const struct flowi * fl )
{
const struct flowi6 * fl6 = & fl - > u . ip6 ;
/* Initialize temporary selector matching only to current session. */
* ( struct in6_addr * ) & sel - > daddr = fl6 - > daddr ;
* ( struct in6_addr * ) & sel - > saddr = fl6 - > saddr ;
sel - > dport = xfrm_flowi_dport ( fl , & fl6 - > uli ) ;
sel - > dport_mask = htons ( 0xffff ) ;
sel - > sport = xfrm_flowi_sport ( fl , & fl6 - > uli ) ;
sel - > sport_mask = htons ( 0xffff ) ;
sel - > family = AF_INET6 ;
sel - > prefixlen_d = 128 ;
sel - > prefixlen_s = 128 ;
sel - > proto = fl6 - > flowi6_proto ;
sel - > ifindex = fl6 - > flowi6_oif ;
}
2017-01-09 16:20:48 +03:00
static void
2011-02-23 05:22:34 +03:00
xfrm_init_tempstate ( struct xfrm_state * x , const struct flowi * fl ,
2011-02-24 09:50:12 +03:00
const struct xfrm_tmpl * tmpl ,
2011-02-24 09:55:45 +03:00
const xfrm_address_t * daddr , const xfrm_address_t * saddr ,
2010-09-20 22:11:38 +04:00
unsigned short family )
2005-04-17 02:20:36 +04:00
{
2019-05-03 18:46:14 +03:00
switch ( family ) {
case AF_INET :
__xfrm4_init_tempsel ( & x - > sel , fl ) ;
break ;
case AF_INET6 :
__xfrm6_init_tempsel ( & x - > sel , fl ) ;
break ;
}
2019-05-03 18:46:15 +03:00
x - > id = tmpl - > id ;
2019-05-03 18:46:14 +03:00
2019-05-03 18:46:15 +03:00
switch ( tmpl - > encap_family ) {
case AF_INET :
if ( x - > id . daddr . a4 = = 0 )
x - > id . daddr . a4 = daddr - > a4 ;
x - > props . saddr = tmpl - > saddr ;
if ( x - > props . saddr . a4 = = 0 )
x - > props . saddr . a4 = saddr - > a4 ;
break ;
case AF_INET6 :
if ( ipv6_addr_any ( ( struct in6_addr * ) & x - > id . daddr ) )
memcpy ( & x - > id . daddr , daddr , sizeof ( x - > sel . daddr ) ) ;
memcpy ( & x - > props . saddr , & tmpl - > saddr , sizeof ( x - > props . saddr ) ) ;
if ( ipv6_addr_any ( ( struct in6_addr * ) & x - > props . saddr ) )
memcpy ( & x - > props . saddr , saddr , sizeof ( x - > props . saddr ) ) ;
break ;
}
2017-01-13 16:55:14 +03:00
2019-05-03 18:46:15 +03:00
x - > props . mode = tmpl - > mode ;
x - > props . reqid = tmpl - > reqid ;
x - > props . family = tmpl - > encap_family ;
2005-04-17 02:20:36 +04:00
}
2022-12-02 21:41:30 +03:00
static struct xfrm_state * __xfrm_state_lookup_all ( struct net * net , u32 mark ,
const xfrm_address_t * daddr ,
__be32 spi , u8 proto ,
unsigned short family ,
struct xfrm_dev_offload * xdo )
{
unsigned int h = xfrm_spi_hash ( net , daddr , spi , proto , family ) ;
struct xfrm_state * x ;
hlist_for_each_entry_rcu ( x , net - > xfrm . state_byspi + h , byspi ) {
# ifdef CONFIG_XFRM_OFFLOAD
if ( xdo - > type = = XFRM_DEV_OFFLOAD_PACKET ) {
if ( x - > xso . type ! = XFRM_DEV_OFFLOAD_PACKET )
/* HW states are in the head of list, there is
* no need to iterate further .
*/
break ;
/* Packet offload: both policy and SA should
* have same device .
*/
if ( xdo - > dev ! = x - > xso . dev )
continue ;
} else if ( x - > xso . type = = XFRM_DEV_OFFLOAD_PACKET )
/* Skip HW policy for SW lookups */
continue ;
# endif
if ( x - > props . family ! = family | |
x - > id . spi ! = spi | |
x - > id . proto ! = proto | |
! xfrm_addr_equal ( & x - > id . daddr , daddr , family ) )
continue ;
if ( ( mark & x - > mark . m ) ! = x - > mark . v )
continue ;
if ( ! xfrm_state_hold_rcu ( x ) )
continue ;
return x ;
}
return NULL ;
}
2011-02-24 09:51:36 +03:00
static struct xfrm_state * __xfrm_state_lookup ( struct net * net , u32 mark ,
const xfrm_address_t * daddr ,
__be32 spi , u8 proto ,
unsigned short family )
2006-08-24 11:42:45 +04:00
{
2008-11-26 04:30:50 +03:00
unsigned int h = xfrm_spi_hash ( net , daddr , spi , proto , family ) ;
2006-08-24 11:42:45 +04:00
struct xfrm_state * x ;
2016-08-09 13:16:04 +03:00
hlist_for_each_entry_rcu ( x , net - > xfrm . state_byspi + h , byspi ) {
2006-08-24 11:42:45 +04:00
if ( x - > props . family ! = family | |
x - > id . spi ! = spi | |
2009-06-28 22:42:53 +04:00
x - > id . proto ! = proto | |
2013-01-29 16:48:50 +04:00
! xfrm_addr_equal ( & x - > id . daddr , daddr , family ) )
2006-08-24 11:42:45 +04:00
continue ;
2010-02-22 14:32:56 +03:00
if ( ( mark & x - > mark . m ) ! = x - > mark . v )
continue ;
2016-08-09 13:16:05 +03:00
if ( ! xfrm_state_hold_rcu ( x ) )
continue ;
2006-08-24 11:42:45 +04:00
return x ;
}
return NULL ;
}
2011-02-24 09:51:36 +03:00
static struct xfrm_state * __xfrm_state_lookup_byaddr ( struct net * net , u32 mark ,
const xfrm_address_t * daddr ,
const xfrm_address_t * saddr ,
u8 proto , unsigned short family )
2006-08-24 11:42:45 +04:00
{
2008-11-26 04:30:50 +03:00
unsigned int h = xfrm_src_hash ( net , daddr , saddr , family ) ;
2006-08-24 11:42:45 +04:00
struct xfrm_state * x ;
2016-08-09 13:16:04 +03:00
hlist_for_each_entry_rcu ( x , net - > xfrm . state_bysrc + h , bysrc ) {
2006-08-24 11:42:45 +04:00
if ( x - > props . family ! = family | |
2009-06-28 22:42:53 +04:00
x - > id . proto ! = proto | |
2013-01-29 16:48:50 +04:00
! xfrm_addr_equal ( & x - > id . daddr , daddr , family ) | |
! xfrm_addr_equal ( & x - > props . saddr , saddr , family ) )
2006-08-24 11:42:45 +04:00
continue ;
2010-02-22 14:32:56 +03:00
if ( ( mark & x - > mark . m ) ! = x - > mark . v )
continue ;
2016-08-09 13:16:05 +03:00
if ( ! xfrm_state_hold_rcu ( x ) )
continue ;
2006-08-24 11:42:45 +04:00
return x ;
}
return NULL ;
}
static inline struct xfrm_state *
__xfrm_state_locate ( struct xfrm_state * x , int use_spi , int family )
{
2008-11-26 04:30:50 +03:00
struct net * net = xs_net ( x ) ;
2010-02-23 03:20:22 +03:00
u32 mark = x - > mark . v & x - > mark . m ;
2008-11-26 04:30:50 +03:00
2006-08-24 11:42:45 +04:00
if ( use_spi )
2010-02-23 03:20:22 +03:00
return __xfrm_state_lookup ( net , mark , & x - > id . daddr ,
x - > id . spi , x - > id . proto , family ) ;
2006-08-24 11:42:45 +04:00
else
2010-02-23 03:20:22 +03:00
return __xfrm_state_lookup_byaddr ( net , mark ,
& x - > id . daddr ,
2006-08-24 11:42:45 +04:00
& x - > props . saddr ,
x - > id . proto , family ) ;
}
2008-11-26 04:29:47 +03:00
static void xfrm_hash_grow_check ( struct net * net , int have_hash_collision )
2006-10-25 02:34:00 +04:00
{
if ( have_hash_collision & &
2008-11-26 04:29:47 +03: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 02:34:00 +04:00
}
2009-03-14 00:22:40 +03:00
static void xfrm_state_look_at ( struct xfrm_policy * pol , struct xfrm_state * x ,
2011-02-23 05:21:31 +03:00
const struct flowi * fl , unsigned short family ,
2009-03-14 00:22:40 +03:00
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 & &
2020-09-25 07:42:56 +03:00
( x - > sel . family ! = family | |
! xfrm_selector_match ( & x - > sel , fl , family ) ) ) | |
2020-09-28 05:38:26 +03:00
! security_xfrm_state_pol_flow_match ( x , pol ,
& fl - > u . __fl_common ) )
2009-03-14 00:22:40 +03:00
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 ) {
2020-09-25 07:42:56 +03:00
if ( ( ! x - > sel . family | |
( x - > sel . family = = family & &
xfrm_selector_match ( & x - > sel , fl , family ) ) ) & &
2020-09-28 05:38:26 +03:00
security_xfrm_state_pol_flow_match ( x , pol ,
& fl - > u . __fl_common ) )
2009-03-14 00:22:40 +03:00
* error = - ESRCH ;
}
}
2005-04-17 02:20:36 +04:00
struct xfrm_state *
2011-02-24 09:55:45 +03:00
xfrm_state_find ( const xfrm_address_t * daddr , const xfrm_address_t * saddr ,
2011-02-23 05:24:19 +03:00
const struct flowi * fl , struct xfrm_tmpl * tmpl ,
2005-04-17 02:20:36 +04:00
struct xfrm_policy * pol , int * err ,
2018-07-19 20:50:44 +03:00
unsigned short family , u32 if_id )
2005-04-17 02:20:36 +04:00
{
2009-03-14 00:22:40 +03:00
static xfrm_address_t saddr_wildcard = { } ;
2008-11-26 04:31:51 +03:00
struct net * net = xp_net ( pol ) ;
2009-04-27 13:58:59 +04:00
unsigned int h , h_wildcard ;
2008-09-03 07:14:15 +04:00
struct xfrm_state * x , * x0 , * to_put ;
2005-04-17 02:20:36 +04:00
int acquire_in_progress = 0 ;
int error = 0 ;
struct xfrm_state * best = NULL ;
2010-02-23 03:20:22 +03:00
u32 mark = pol - > mark . v & pol - > mark . m ;
2010-09-20 22:11:38 +04:00
unsigned short encap_family = tmpl - > encap_family ;
2016-08-09 13:16:07 +03:00
unsigned int sequence ;
2014-02-12 18:20:06 +04:00
struct km_event c ;
2007-02-09 17:25:29 +03:00
2008-09-03 07:14:15 +04:00
to_put = NULL ;
2021-03-16 13:56:29 +03:00
sequence = read_seqcount_begin ( & net - > xfrm . xfrm_state_hash_generation ) ;
2016-08-09 13:16:07 +03:00
2016-08-09 13:16:09 +03:00
rcu_read_lock ( ) ;
2010-09-20 22:11:38 +04:00
h = xfrm_dst_hash ( net , daddr , saddr , tmpl - > reqid , encap_family ) ;
2016-08-09 13:16:04 +03:00
hlist_for_each_entry_rcu ( x , net - > xfrm . state_bydst + h , bydst ) {
2022-12-02 21:41:30 +03:00
# ifdef CONFIG_XFRM_OFFLOAD
if ( pol - > xdo . type = = XFRM_DEV_OFFLOAD_PACKET ) {
if ( x - > xso . type ! = XFRM_DEV_OFFLOAD_PACKET )
/* HW states are in the head of list, there is
* no need to iterate further .
*/
break ;
/* Packet offload: both policy and SA should
* have same device .
*/
if ( pol - > xdo . dev ! = x - > xso . dev )
continue ;
} else if ( x - > xso . type = = XFRM_DEV_OFFLOAD_PACKET )
/* Skip HW policy for SW lookups */
continue ;
# endif
2010-09-20 22:11:38 +04:00
if ( x - > props . family = = encap_family & &
2005-04-17 02:20:36 +04:00
x - > props . reqid = = tmpl - > reqid & &
2010-02-22 14:32:56 +03:00
( mark & x - > mark . m ) = = x - > mark . v & &
2018-06-12 15:07:07 +03:00
x - > if_id = = if_id & &
2006-08-24 05:08:21 +04:00
! ( x - > props . flags & XFRM_STATE_WILDRECV ) & &
2010-09-20 22:11:38 +04:00
xfrm_state_addr_check ( x , daddr , saddr , encap_family ) & &
2005-04-17 02:20:36 +04:00
tmpl - > mode = = x - > props . mode & &
tmpl - > id . proto = = x - > id . proto & &
2009-03-14 00:22:40 +03:00
( tmpl - > id . spi = = x - > id . spi | | ! tmpl - > id . spi ) )
2020-09-25 07:42:56 +03:00
xfrm_state_look_at ( pol , x , fl , family ,
2009-03-14 00:22:40 +03:00
& best , & acquire_in_progress , & error ) ;
}
2013-09-23 13:18:25 +04:00
if ( best | | acquire_in_progress )
2009-03-14 00:22:40 +03:00
goto found ;
2010-09-20 22:11:38 +04:00
h_wildcard = xfrm_dst_hash ( net , daddr , & saddr_wildcard , tmpl - > reqid , encap_family ) ;
2016-08-09 13:16:04 +03:00
hlist_for_each_entry_rcu ( x , net - > xfrm . state_bydst + h_wildcard , bydst ) {
2022-12-02 21:41:30 +03:00
# ifdef CONFIG_XFRM_OFFLOAD
if ( pol - > xdo . type = = XFRM_DEV_OFFLOAD_PACKET ) {
if ( x - > xso . type ! = XFRM_DEV_OFFLOAD_PACKET )
/* HW states are in the head of list, there is
* no need to iterate further .
*/
break ;
/* Packet offload: both policy and SA should
* have same device .
*/
if ( pol - > xdo . dev ! = x - > xso . dev )
continue ;
} else if ( x - > xso . type = = XFRM_DEV_OFFLOAD_PACKET )
/* Skip HW policy for SW lookups */
continue ;
# endif
2010-09-20 22:11:38 +04:00
if ( x - > props . family = = encap_family & &
2009-03-14 00:22:40 +03:00
x - > props . reqid = = tmpl - > reqid & &
2010-02-22 14:32:56 +03:00
( mark & x - > mark . m ) = = x - > mark . v & &
2018-06-12 15:07:07 +03:00
x - > if_id = = if_id & &
2009-03-14 00:22:40 +03:00
! ( x - > props . flags & XFRM_STATE_WILDRECV ) & &
2013-09-27 12:32:50 +04:00
xfrm_addr_equal ( & x - > id . daddr , daddr , encap_family ) & &
2009-03-14 00:22:40 +03:00
tmpl - > mode = = x - > props . mode & &
tmpl - > id . proto = = x - > id . proto & &
( tmpl - > id . spi = = x - > id . spi | | ! tmpl - > id . spi ) )
2020-09-25 07:42:56 +03:00
xfrm_state_look_at ( pol , x , fl , family ,
2009-03-14 00:22:40 +03:00
& best , & acquire_in_progress , & error ) ;
2005-04-17 02:20:36 +04:00
}
2009-03-14 00:22:40 +03:00
found :
2005-04-17 02:20:36 +04:00
x = best ;
if ( ! x & & ! error & & ! acquire_in_progress ) {
2005-04-22 07:12:32 +04:00
if ( tmpl - > id . spi & &
2022-12-02 21:41:30 +03:00
( x0 = __xfrm_state_lookup_all ( net , mark , daddr ,
tmpl - > id . spi , tmpl - > id . proto ,
encap_family ,
& pol - > xdo ) ) ! = NULL ) {
2008-09-03 07:14:15 +04:00
to_put = x0 ;
2005-04-17 02:20:36 +04:00
error = - EEXIST ;
goto out ;
}
2014-02-12 18:20:06 +04:00
c . net = net ;
/* If the KMs have no listeners (yet...), avoid allocating an SA
* for each and every packet - garbage collection might not
* handle the flood .
*/
if ( ! km_is_alive ( & c ) ) {
error = - ESRCH ;
goto out ;
}
2008-11-26 04:31:51 +03:00
x = xfrm_state_alloc ( net ) ;
2005-04-17 02:20:36 +04:00
if ( x = = NULL ) {
error = - ENOMEM ;
goto out ;
}
2010-09-20 22:11:38 +04:00
/* Initialize temporary state matching only
2005-04-17 02:20:36 +04:00
* to current session . */
2010-09-20 22:11:38 +04:00
xfrm_init_tempstate ( x , fl , tmpl , daddr , saddr , family ) ;
2010-02-23 03:20:22 +03:00
memcpy ( & x - > mark , & pol - > mark , sizeof ( x - > mark ) ) ;
2018-06-12 15:07:07 +03:00
x - > if_id = if_id ;
2005-04-17 02:20:36 +04:00
2011-03-12 08:29:39 +03:00
error = security_xfrm_state_alloc_acquire ( x , pol - > security , fl - > flowi_secid ) ;
2006-07-25 10:29:07 +04:00
if ( error ) {
x - > km . state = XFRM_STATE_DEAD ;
2008-09-03 07:14:15 +04:00
to_put = x ;
2006-07-25 10:29:07 +04:00
x = NULL ;
goto out ;
}
2022-12-02 21:41:30 +03:00
# ifdef CONFIG_XFRM_OFFLOAD
if ( pol - > xdo . type = = XFRM_DEV_OFFLOAD_PACKET ) {
struct xfrm_dev_offload * xdo = & pol - > xdo ;
struct xfrm_dev_offload * xso = & x - > xso ;
xso - > type = XFRM_DEV_OFFLOAD_PACKET ;
xso - > dir = xdo - > dir ;
xso - > dev = xdo - > dev ;
xso - > real_dev = xdo - > real_dev ;
2023-03-14 11:58:39 +03:00
xso - > flags = XFRM_DEV_OFFLOAD_FLAG_ACQ ;
2022-12-02 21:41:30 +03:00
netdev_tracker_alloc ( xso - > dev , & xso - > dev_tracker ,
GFP_ATOMIC ) ;
2023-01-24 14:54:59 +03:00
error = xso - > dev - > xfrmdev_ops - > xdo_dev_state_add ( x , NULL ) ;
2022-12-02 21:41:30 +03:00
if ( error ) {
xso - > dir = 0 ;
netdev_put ( xso - > dev , & xso - > dev_tracker ) ;
xso - > dev = NULL ;
xso - > real_dev = NULL ;
xso - > type = XFRM_DEV_OFFLOAD_UNSPECIFIED ;
x - > km . state = XFRM_STATE_DEAD ;
to_put = x ;
x = NULL ;
goto out ;
}
}
# endif
2005-04-17 02:20:36 +04:00
if ( km_query ( x , tmpl , pol ) = = 0 ) {
2016-08-09 13:16:09 +03:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
x - > km . state = XFRM_STATE_ACQ ;
2008-11-26 04:31:51 +03:00
list_add ( & x - > km . all , & net - > xfrm . state_all ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( bydst , & x - > bydst ,
net - > xfrm . state_bydst + h ,
x - > xso . type ) ;
2010-09-20 22:11:38 +04:00
h = xfrm_src_hash ( net , daddr , saddr , encap_family ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( bysrc , & x - > bysrc ,
net - > xfrm . state_bysrc + h ,
x - > xso . type ) ;
2005-04-17 02:20:36 +04:00
if ( x - > id . spi ) {
2010-09-20 22:11:38 +04:00
h = xfrm_spi_hash ( net , & x - > id . daddr , x - > id . spi , x - > id . proto , encap_family ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( byspi , & x - > byspi ,
net - > xfrm . state_byspi + h ,
x - > xso . type ) ;
2005-04-17 02:20:36 +04:00
}
2021-04-25 22:47:12 +03:00
if ( x - > km . seq ) {
h = xfrm_seq_hash ( net , x - > km . seq ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( byseq , & x - > byseq ,
net - > xfrm . state_byseq + h ,
x - > xso . type ) ;
2021-04-25 22:47:12 +03:00
}
2008-11-26 05:00:48 +03:00
x - > lft . hard_add_expires_seconds = net - > xfrm . sysctl_acq_expires ;
2019-03-02 01:48:20 +03:00
hrtimer_start ( & x - > mtimer ,
ktime_set ( net - > xfrm . sysctl_acq_expires , 0 ) ,
HRTIMER_MODE_REL_SOFT ) ;
2008-11-26 04:31:51 +03:00
net - > xfrm . state_num + + ;
xfrm_hash_grow_check ( net , x - > bydst . next ! = NULL ) ;
2016-08-09 13:16:09 +03:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
} else {
2022-12-02 21:41:30 +03:00
# ifdef CONFIG_XFRM_OFFLOAD
struct xfrm_dev_offload * xso = & x - > xso ;
if ( xso - > type = = XFRM_DEV_OFFLOAD_PACKET ) {
2023-07-31 14:38:27 +03:00
xfrm_dev_state_delete ( x ) ;
xfrm_dev_state_free ( x ) ;
2022-12-02 21:41:30 +03:00
}
# endif
2005-04-17 02:20:36 +04:00
x - > km . state = XFRM_STATE_DEAD ;
2008-09-03 07:14:15 +04:00
to_put = x ;
2005-04-17 02:20:36 +04:00
x = NULL ;
error = - ESRCH ;
}
}
out :
2016-08-09 13:16:05 +03:00
if ( x ) {
if ( ! xfrm_state_hold_rcu ( x ) ) {
* err = - EAGAIN ;
x = NULL ;
}
} else {
2005-04-17 02:20:36 +04:00
* err = acquire_in_progress ? - EAGAIN : error ;
2016-08-09 13:16:05 +03:00
}
2016-08-09 13:16:09 +03:00
rcu_read_unlock ( ) ;
2008-09-03 07:14:15 +04:00
if ( to_put )
xfrm_state_put ( to_put ) ;
2016-08-09 13:16:07 +03:00
2021-03-16 13:56:29 +03:00
if ( read_seqcount_retry ( & net - > xfrm . xfrm_state_hash_generation , sequence ) ) {
2016-08-09 13:16:07 +03:00
* err = - EAGAIN ;
if ( x ) {
xfrm_state_put ( x ) ;
x = NULL ;
}
}
2005-04-17 02:20:36 +04:00
return x ;
}
2007-07-03 09:41:14 +04:00
struct xfrm_state *
2018-06-12 15:07:07 +03:00
xfrm_stateonly_find ( struct net * net , u32 mark , u32 if_id ,
2008-11-26 04:31:51 +03:00
xfrm_address_t * daddr , xfrm_address_t * saddr ,
2007-07-03 09:41:14 +04:00
unsigned short family , u8 mode , u8 proto , u32 reqid )
{
2007-12-14 22:38:04 +03:00
unsigned int h ;
2007-07-03 09:41:14 +04:00
struct xfrm_state * rx = NULL , * x = NULL ;
2014-01-03 07:18:29 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2008-11-26 04:31:51 +03:00
h = xfrm_dst_hash ( net , daddr , saddr , reqid , family ) ;
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
hlist_for_each_entry ( x , net - > xfrm . state_bydst + h , bydst ) {
2007-07-03 09:41:14 +04:00
if ( x - > props . family = = family & &
x - > props . reqid = = reqid & &
2010-02-22 14:32:56 +03:00
( mark & x - > mark . m ) = = x - > mark . v & &
2018-06-12 15:07:07 +03:00
x - > if_id = = if_id & &
2007-07-03 09:41:14 +04: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 ) ;
2014-01-03 07:18:29 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2007-07-03 09:41:14 +04:00
return rx ;
}
EXPORT_SYMBOL ( xfrm_stateonly_find ) ;
2014-01-03 07:18:32 +04:00
struct xfrm_state * xfrm_state_lookup_byspi ( struct net * net , __be32 spi ,
unsigned short family )
{
struct xfrm_state * x ;
struct xfrm_state_walk * w ;
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
list_for_each_entry ( w , & net - > xfrm . state_all , all ) {
x = container_of ( w , struct xfrm_state , km ) ;
if ( x - > props . family ! = family | |
x - > id . spi ! = spi )
continue ;
xfrm_state_hold ( x ) ;
2015-04-29 03:42:44 +03:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2014-01-03 07:18:32 +04:00
return x ;
}
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
return NULL ;
}
EXPORT_SYMBOL ( xfrm_state_lookup_byspi ) ;
2005-04-17 02:20:36 +04:00
static void __xfrm_state_insert ( struct xfrm_state * x )
{
2008-11-26 04:29:47 +03:00
struct net * net = xs_net ( x ) ;
2006-08-24 14:24:33 +04:00
unsigned int h ;
2005-04-17 02:20:36 +04:00
2008-11-26 04:29:47 +03:00
list_add ( & x - > km . all , & net - > xfrm . state_all ) ;
2008-02-29 08:31:08 +03:00
2008-11-26 04:29:47 +03:00
h = xfrm_dst_hash ( net , & x - > id . daddr , & x - > props . saddr ,
2006-08-24 15:00:03 +04:00
x - > props . reqid , x - > props . family ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( bydst , & x - > bydst , net - > xfrm . state_bydst + h ,
x - > xso . type ) ;
2005-04-17 02:20:36 +04:00
2008-11-26 04:29:47 +03:00
h = xfrm_src_hash ( net , & x - > id . daddr , & x - > props . saddr , x - > props . family ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( bysrc , & x - > bysrc , net - > xfrm . state_bysrc + h ,
x - > xso . type ) ;
2005-04-17 02:20:36 +04:00
2006-09-28 09:21:52 +04:00
if ( x - > id . spi ) {
2008-11-26 04:29:47 +03:00
h = xfrm_spi_hash ( net , & x - > id . daddr , x - > id . spi , x - > id . proto ,
2006-08-24 04:53:57 +04:00
x - > props . family ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( byspi , & x - > byspi , net - > xfrm . state_byspi + h ,
x - > xso . type ) ;
2006-08-24 04:53:57 +04:00
}
2021-04-25 22:47:12 +03:00
if ( x - > km . seq ) {
h = xfrm_seq_hash ( net , x - > km . seq ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( byseq , & x - > byseq , net - > xfrm . state_byseq + h ,
x - > xso . type ) ;
2021-04-25 22:47:12 +03:00
}
2019-03-02 01:48:20 +03:00
hrtimer_start ( & x - > mtimer , ktime_set ( 1 , 0 ) , HRTIMER_MODE_REL_SOFT ) ;
2006-08-24 14:54:22 +04:00
if ( x - > replay_maxage )
mod_timer ( & x - > rtimer , jiffies + x - > replay_maxage ) ;
2006-03-21 06:15:11 +03:00
2008-11-26 04:29:47 +03:00
net - > xfrm . state_num + + ;
2006-08-24 14:08:07 +04:00
2008-11-26 04:29:47 +03:00
xfrm_hash_grow_check ( net , x - > bydst . next ! = NULL ) ;
2005-04-17 02:20:36 +04:00
}
2013-11-07 13:47:50 +04:00
/* net->xfrm.xfrm_state_lock is held */
2006-08-24 14:29:04 +04:00
static void __xfrm_state_bump_genids ( struct xfrm_state * xnew )
{
2008-11-26 04:29:47 +03:00
struct net * net = xs_net ( xnew ) ;
2006-08-24 14:29:04 +04:00
unsigned short family = xnew - > props . family ;
u32 reqid = xnew - > props . reqid ;
struct xfrm_state * x ;
unsigned int h ;
2010-02-22 14:32:56 +03:00
u32 mark = xnew - > mark . v & xnew - > mark . m ;
2018-06-12 15:07:07 +03:00
u32 if_id = xnew - > if_id ;
2006-08-24 14:29:04 +04:00
2008-11-26 04:29:47 +03:00
h = xfrm_dst_hash ( net , & xnew - > id . daddr , & xnew - > props . saddr , reqid , family ) ;
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
hlist_for_each_entry ( x , net - > xfrm . state_bydst + h , bydst ) {
2006-08-24 14:29:04 +04:00
if ( x - > props . family = = family & &
x - > props . reqid = = reqid & &
2018-06-12 15:07:07 +03:00
x - > if_id = = if_id & &
2010-02-22 14:32:56 +03:00
( mark & x - > mark . m ) = = x - > mark . v & &
2013-01-29 16:48:50 +04:00
xfrm_addr_equal ( & x - > id . daddr , & xnew - > id . daddr , family ) & &
xfrm_addr_equal ( & x - > props . saddr , & xnew - > props . saddr , family ) )
2010-03-31 05:19:49 +04:00
x - > genid + + ;
2006-08-24 14:29:04 +04:00
}
}
2005-04-17 02:20:36 +04:00
void xfrm_state_insert ( struct xfrm_state * x )
{
2013-11-07 13:47:50 +04:00
struct net * net = xs_net ( x ) ;
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2006-08-24 14:29:04 +04:00
__xfrm_state_bump_genids ( x ) ;
2005-04-17 02:20:36 +04:00
__xfrm_state_insert ( x ) ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
}
EXPORT_SYMBOL ( xfrm_state_insert ) ;
2013-11-07 13:47:50 +04:00
/* net->xfrm.xfrm_state_lock is held */
2013-06-27 01:56:58 +04:00
static struct xfrm_state * __find_acq_core ( struct net * net ,
const struct xfrm_mark * m ,
2011-02-28 10:17:24 +03:00
unsigned short family , u8 mode ,
2018-06-12 15:07:07 +03:00
u32 reqid , u32 if_id , u8 proto ,
2011-02-28 10:17:24 +03:00
const xfrm_address_t * daddr ,
2013-06-27 01:56:58 +04:00
const xfrm_address_t * saddr ,
int create )
2006-08-24 11:13:10 +04:00
{
2008-11-26 04:31:51 +03:00
unsigned int h = xfrm_dst_hash ( net , daddr , saddr , reqid , family ) ;
2006-08-24 11:13:10 +04:00
struct xfrm_state * x ;
2010-02-22 14:32:56 +03:00
u32 mark = m - > v & m - > m ;
2006-08-24 11:13:10 +04:00
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 05:06:00 +04:00
hlist_for_each_entry ( x , net - > xfrm . state_bydst + h , bydst ) {
2006-08-24 11:13:10 +04:00
if ( x - > props . reqid ! = reqid | |
x - > props . mode ! = mode | |
x - > props . family ! = family | |
x - > km . state ! = XFRM_STATE_ACQ | |
2007-03-13 03:14:07 +03:00
x - > id . spi ! = 0 | |
2009-06-28 22:42:53 +04:00
x - > id . proto ! = proto | |
2010-02-22 14:32:56 +03:00
( mark & x - > mark . m ) ! = x - > mark . v | |
2013-01-29 16:48:50 +04:00
! xfrm_addr_equal ( & x - > id . daddr , daddr , family ) | |
! xfrm_addr_equal ( & x - > props . saddr , saddr , family ) )
2006-08-24 11:13:10 +04:00
continue ;
xfrm_state_hold ( x ) ;
return x ;
}
if ( ! create )
return NULL ;
2008-11-26 04:31:51 +03:00
x = xfrm_state_alloc ( net ) ;
2006-08-24 11:13:10 +04: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 :
2015-03-29 17:59:24 +03:00
x - > sel . daddr . in6 = daddr - > in6 ;
x - > sel . saddr . in6 = saddr - > in6 ;
2006-08-24 11:13:10 +04:00
x - > sel . prefixlen_d = 128 ;
x - > sel . prefixlen_s = 128 ;
2015-03-29 17:59:24 +03:00
x - > props . saddr . in6 = saddr - > in6 ;
x - > id . daddr . in6 = daddr - > in6 ;
2006-08-24 11:13:10 +04:00
break ;
2007-04-21 04:09:22 +04:00
}
2006-08-24 11:13:10 +04:00
x - > km . state = XFRM_STATE_ACQ ;
x - > id . proto = proto ;
x - > props . family = family ;
x - > props . mode = mode ;
x - > props . reqid = reqid ;
2018-06-12 15:07:07 +03:00
x - > if_id = if_id ;
2010-02-23 03:20:22 +03:00
x - > mark . v = m - > v ;
x - > mark . m = m - > m ;
2008-11-26 05:00:48 +03:00
x - > lft . hard_add_expires_seconds = net - > xfrm . sysctl_acq_expires ;
2006-08-24 11:13:10 +04:00
xfrm_state_hold ( x ) ;
2019-03-02 01:48:20 +03:00
hrtimer_start ( & x - > mtimer ,
ktime_set ( net - > xfrm . sysctl_acq_expires , 0 ) ,
HRTIMER_MODE_REL_SOFT ) ;
2008-11-26 04:31:51 +03:00
list_add ( & x - > km . all , & net - > xfrm . state_all ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( bydst , & x - > bydst , net - > xfrm . state_bydst + h ,
x - > xso . type ) ;
2008-11-26 04:31:51 +03:00
h = xfrm_src_hash ( net , daddr , saddr , family ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( bysrc , & x - > bysrc , net - > xfrm . state_bysrc + h ,
x - > xso . type ) ;
2006-10-13 09:03:24 +04:00
2008-11-26 04:31:51 +03:00
net - > xfrm . state_num + + ;
2006-10-13 09:03:24 +04:00
2008-11-26 04:31:51 +03:00
xfrm_hash_grow_check ( net , x - > bydst . next ! = NULL ) ;
2006-08-24 11:13:10 +04:00
}
return x ;
}
2010-02-23 03:20:22 +03:00
static struct xfrm_state * __xfrm_find_acq_byseq ( struct net * net , u32 mark , u32 seq ) ;
2005-04-17 02:20:36 +04:00
int xfrm_state_add ( struct xfrm_state * x )
{
2008-11-26 04:31:51 +03:00
struct net * net = xs_net ( x ) ;
2008-09-03 07:14:15 +04:00
struct xfrm_state * x1 , * to_put ;
2005-04-17 02:20:36 +04:00
int family ;
int err ;
2010-02-23 03:20:22 +03:00
u32 mark = x - > mark . v & x - > mark . m ;
2006-08-24 04:56:04 +04:00
int use_spi = xfrm_id_proto_match ( x - > id . proto , IPSEC_PROTO_ANY ) ;
2005-04-17 02:20:36 +04:00
family = x - > props . family ;
2008-09-03 07:14:15 +04:00
to_put = NULL ;
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
2006-08-24 11:42:45 +04:00
x1 = __xfrm_state_locate ( x , use_spi , family ) ;
2005-04-17 02:20:36 +04:00
if ( x1 ) {
2008-09-03 07:14:15 +04:00
to_put = x1 ;
2005-04-17 02:20:36 +04:00
x1 = NULL ;
err = - EEXIST ;
goto out ;
}
2006-08-24 04:56:04 +04:00
if ( use_spi & & x - > km . seq ) {
2010-02-23 03:20:22 +03:00
x1 = __xfrm_find_acq_byseq ( net , mark , x - > km . seq ) ;
2007-03-13 03:14:07 +03:00
if ( x1 & & ( ( x1 - > id . proto ! = x - > id . proto ) | |
2013-01-29 16:48:50 +04:00
! xfrm_addr_equal ( & x1 - > id . daddr , & x - > id . daddr , family ) ) ) {
2008-09-03 07:14:15 +04:00
to_put = x1 ;
2005-04-17 02:20:36 +04:00
x1 = NULL ;
}
}
2006-08-24 04:56:04 +04:00
if ( use_spi & & ! x1 )
2010-02-23 03:20:22 +03:00
x1 = __find_acq_core ( net , & x - > mark , family , x - > props . mode ,
2018-06-12 15:07:07 +03:00
x - > props . reqid , x - > if_id , x - > id . proto ,
2006-08-24 11:13:10 +04:00
& x - > id . daddr , & x - > props . saddr , 0 ) ;
2005-04-17 02:20:36 +04:00
2006-08-24 14:29:04 +04:00
__xfrm_state_bump_genids ( x ) ;
2005-04-17 02:20:36 +04:00
__xfrm_state_insert ( x ) ;
err = 0 ;
out :
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
if ( x1 ) {
xfrm_state_delete ( x1 ) ;
xfrm_state_put ( x1 ) ;
}
2008-09-03 07:14:15 +04:00
if ( to_put )
xfrm_state_put ( to_put ) ;
2005-04-17 02:20:36 +04:00
return err ;
}
EXPORT_SYMBOL ( xfrm_state_add ) ;
2007-02-09 00:11:42 +03:00
# ifdef CONFIG_XFRM_MIGRATE
2020-09-04 09:50:11 +03:00
static inline int clone_security ( struct xfrm_state * x , struct xfrm_sec_ctx * security )
{
struct xfrm_user_sec_ctx * uctx ;
int size = sizeof ( * uctx ) + security - > ctx_len ;
int err ;
uctx = kmalloc ( size , GFP_KERNEL ) ;
if ( ! uctx )
return - ENOMEM ;
uctx - > exttype = XFRMA_SEC_CTX ;
uctx - > len = size ;
uctx - > ctx_doi = security - > ctx_doi ;
uctx - > ctx_alg = security - > ctx_alg ;
uctx - > ctx_len = security - > ctx_len ;
memcpy ( uctx + 1 , security - > ctx_str , security - > ctx_len ) ;
err = security_xfrm_state_alloc ( x , uctx ) ;
kfree ( uctx ) ;
if ( err )
return err ;
return 0 ;
}
2017-06-06 13:12:13 +03:00
static struct xfrm_state * xfrm_state_clone ( struct xfrm_state * orig ,
struct xfrm_encap_tmpl * encap )
2007-02-09 00:11:42 +03:00
{
2008-11-26 04:29:47 +03:00
struct net * net = xs_net ( orig ) ;
struct xfrm_state * x = xfrm_state_alloc ( net ) ;
2007-02-09 00:11:42 +03:00
if ( ! x )
2010-02-15 23:00:51 +03:00
goto out ;
2007-02-09 00:11:42 +03: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 03:29:52 +03:00
x - > aalg = xfrm_algo_auth_clone ( orig - > aalg ) ;
2007-02-09 00:11:42 +03:00
if ( ! x - > aalg )
goto error ;
}
x - > props . aalgo = orig - > props . aalgo ;
2014-02-19 16:33:24 +04:00
if ( orig - > aead ) {
x - > aead = xfrm_algo_aead_clone ( orig - > aead ) ;
2017-12-07 23:54:27 +03:00
x - > geniv = orig - > geniv ;
2014-02-19 16:33:24 +04:00
if ( ! x - > aead )
goto error ;
}
2007-02-09 00:11:42 +03:00
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 ;
2017-06-06 13:12:13 +03:00
if ( encap | | orig - > encap ) {
if ( encap )
x - > encap = kmemdup ( encap , sizeof ( * x - > encap ) ,
GFP_KERNEL ) ;
else
x - > encap = kmemdup ( orig - > encap , sizeof ( * x - > encap ) ,
GFP_KERNEL ) ;
2007-02-09 00:11:42 +03:00
if ( ! x - > encap )
goto error ;
}
2020-09-04 09:50:11 +03:00
if ( orig - > security )
if ( clone_security ( x , orig - > security ) )
goto error ;
2007-02-09 00:11:42 +03:00
if ( orig - > coaddr ) {
x - > coaddr = kmemdup ( orig - > coaddr , sizeof ( * x - > coaddr ) ,
GFP_KERNEL ) ;
if ( ! x - > coaddr )
goto error ;
}
2011-03-28 23:46:39 +04:00
if ( orig - > replay_esn ) {
2014-02-19 16:33:24 +04:00
if ( xfrm_replay_clone ( x , orig ) )
2011-03-28 23:46:39 +04:00
goto error ;
}
2010-02-23 03:20:22 +03:00
memcpy ( & x - > mark , & orig - > mark , sizeof ( x - > mark ) ) ;
2020-09-04 09:49:38 +03:00
memcpy ( & x - > props . smark , & orig - > props . smark , sizeof ( x - > props . smark ) ) ;
2010-02-23 03:20:22 +03:00
2007-02-09 00:11:42 +03:00
x - > props . flags = orig - > props . flags ;
2013-02-22 13:54:54 +04:00
x - > props . extra_flags = orig - > props . extra_flags ;
2007-02-09 00:11:42 +03:00
2018-06-12 15:07:07 +03:00
x - > if_id = orig - > if_id ;
2014-02-19 16:33:24 +04:00
x - > tfcpad = orig - > tfcpad ;
x - > replay_maxdiff = orig - > replay_maxdiff ;
x - > replay_maxage = orig - > replay_maxage ;
2020-09-04 09:50:29 +03:00
memcpy ( & x - > curlft , & orig - > curlft , sizeof ( x - > curlft ) ) ;
2007-02-09 00:11:42 +03:00
x - > km . state = orig - > km . state ;
x - > km . seq = orig - > km . seq ;
xfrm: fix state migration copy replay sequence numbers
During xfrm migration copy replay and preplay sequence numbers
from the previous state.
Here is a tcpdump output showing the problem.
10.0.10.46 is running vanilla kernel, is the IKE/IPsec responder.
After the migration it sent wrong sequence number, reset to 1.
The migration is from 10.0.0.52 to 10.0.0.53.
IP 10.0.0.52.4500 > 10.0.10.46.4500: UDP-encap: ESP(spi=0x43ef462d,seq=0x7cf), length 136
IP 10.0.10.46.4500 > 10.0.0.52.4500: UDP-encap: ESP(spi=0xca1c282d,seq=0x7cf), length 136
IP 10.0.0.52.4500 > 10.0.10.46.4500: UDP-encap: ESP(spi=0x43ef462d,seq=0x7d0), length 136
IP 10.0.10.46.4500 > 10.0.0.52.4500: UDP-encap: ESP(spi=0xca1c282d,seq=0x7d0), length 136
IP 10.0.0.53.4500 > 10.0.10.46.4500: NONESP-encap: isakmp: child_sa inf2[I]
IP 10.0.10.46.4500 > 10.0.0.53.4500: NONESP-encap: isakmp: child_sa inf2[R]
IP 10.0.0.53.4500 > 10.0.10.46.4500: NONESP-encap: isakmp: child_sa inf2[I]
IP 10.0.10.46.4500 > 10.0.0.53.4500: NONESP-encap: isakmp: child_sa inf2[R]
IP 10.0.0.53.4500 > 10.0.10.46.4500: UDP-encap: ESP(spi=0x43ef462d,seq=0x7d1), length 136
NOTE: next sequence is wrong 0x1
IP 10.0.10.46.4500 > 10.0.0.53.4500: UDP-encap: ESP(spi=0xca1c282d,seq=0x1), length 136
IP 10.0.0.53.4500 > 10.0.10.46.4500: UDP-encap: ESP(spi=0x43ef462d,seq=0x7d2), length 136
IP 10.0.10.46.4500 > 10.0.0.53.4500: UDP-encap: ESP(spi=0xca1c282d,seq=0x2), length 136
Signed-off-by: Antony Antony <antony@phenome.org>
Reviewed-by: Richard Guy Briggs <rgb@tricolour.ca>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2017-05-19 13:47:00 +03:00
x - > replay = orig - > replay ;
x - > preplay = orig - > preplay ;
2021-12-22 16:11:18 +03:00
x - > mapping_maxage = orig - > mapping_maxage ;
2022-07-27 18:41:22 +03:00
x - > lastused = orig - > lastused ;
2021-12-22 16:11:18 +03:00
x - > new_mapping = 0 ;
x - > new_mapping_sport = 0 ;
2007-02-09 00:11:42 +03:00
return x ;
error :
2010-02-15 23:00:51 +03:00
xfrm_state_put ( x ) ;
out :
2007-02-09 00:11:42 +03:00
return NULL ;
}
xfrm: Check if_id in xfrm_migrate
This patch enables distinguishing SAs and SPs based on if_id during
the xfrm_migrate flow. This ensures support for xfrm interfaces
throughout the SA/SP lifecycle.
When there are multiple existing SPs with the same direction,
the same xfrm_selector and different endpoint addresses,
xfrm_migrate might fail with ENODATA.
Specifically, the code path for performing xfrm_migrate is:
Stage 1: find policy to migrate with
xfrm_migrate_policy_find(sel, dir, type, net)
Stage 2: find and update state(s) with
xfrm_migrate_state_find(mp, net)
Stage 3: update endpoint address(es) of template(s) with
xfrm_policy_migrate(pol, m, num_migrate)
Currently "Stage 1" always returns the first xfrm_policy that
matches, and "Stage 3" looks for the xfrm_tmpl that matches the
old endpoint address. Thus if there are multiple xfrm_policy
with same selector, direction, type and net, "Stage 1" might
rertun a wrong xfrm_policy and "Stage 3" will fail with ENODATA
because it cannot find a xfrm_tmpl with the matching endpoint
address.
The fix is to allow userspace to pass an if_id and add if_id
to the matching rule in Stage 1 and Stage 2 since if_id is a
unique ID for xfrm_policy and xfrm_state. For compatibility,
if_id will only be checked if the attribute is set.
Tested with additions to Android's kernel unit test suite:
https://android-review.googlesource.com/c/kernel/tests/+/1668886
Signed-off-by: Yan Yan <evitayan@google.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2022-01-19 03:00:13 +03:00
struct xfrm_state * xfrm_migrate_state_find ( struct xfrm_migrate * m , struct net * net ,
u32 if_id )
2007-02-09 00:11:42 +03:00
{
unsigned int h ;
2014-02-19 16:33:24 +04:00
struct xfrm_state * x = NULL ;
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2007-02-09 00:11:42 +03:00
if ( m - > reqid ) {
2013-11-07 13:47:50 +04:00
h = xfrm_dst_hash ( net , & m - > old_daddr , & m - > old_saddr ,
2007-02-09 00:11:42 +03:00
m - > reqid , m - > old_family ) ;
2013-11-07 13:47:50 +04:00
hlist_for_each_entry ( x , net - > xfrm . state_bydst + h , bydst ) {
2007-02-09 00:11:42 +03:00
if ( x - > props . mode ! = m - > mode | |
x - > id . proto ! = m - > proto )
continue ;
if ( m - > reqid & & x - > props . reqid ! = m - > reqid )
continue ;
xfrm: Check if_id in xfrm_migrate
This patch enables distinguishing SAs and SPs based on if_id during
the xfrm_migrate flow. This ensures support for xfrm interfaces
throughout the SA/SP lifecycle.
When there are multiple existing SPs with the same direction,
the same xfrm_selector and different endpoint addresses,
xfrm_migrate might fail with ENODATA.
Specifically, the code path for performing xfrm_migrate is:
Stage 1: find policy to migrate with
xfrm_migrate_policy_find(sel, dir, type, net)
Stage 2: find and update state(s) with
xfrm_migrate_state_find(mp, net)
Stage 3: update endpoint address(es) of template(s) with
xfrm_policy_migrate(pol, m, num_migrate)
Currently "Stage 1" always returns the first xfrm_policy that
matches, and "Stage 3" looks for the xfrm_tmpl that matches the
old endpoint address. Thus if there are multiple xfrm_policy
with same selector, direction, type and net, "Stage 1" might
rertun a wrong xfrm_policy and "Stage 3" will fail with ENODATA
because it cannot find a xfrm_tmpl with the matching endpoint
address.
The fix is to allow userspace to pass an if_id and add if_id
to the matching rule in Stage 1 and Stage 2 since if_id is a
unique ID for xfrm_policy and xfrm_state. For compatibility,
if_id will only be checked if the attribute is set.
Tested with additions to Android's kernel unit test suite:
https://android-review.googlesource.com/c/kernel/tests/+/1668886
Signed-off-by: Yan Yan <evitayan@google.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2022-01-19 03:00:13 +03:00
if ( if_id ! = 0 & & x - > if_id ! = if_id )
continue ;
2013-01-29 16:48:50 +04:00
if ( ! xfrm_addr_equal ( & x - > id . daddr , & m - > old_daddr ,
m - > old_family ) | |
! xfrm_addr_equal ( & x - > props . saddr , & m - > old_saddr ,
m - > old_family ) )
2007-02-09 00:11:42 +03:00
continue ;
xfrm_state_hold ( x ) ;
2014-02-19 16:33:24 +04:00
break ;
2007-02-09 00:11:42 +03:00
}
} else {
2013-11-07 13:47:50 +04:00
h = xfrm_src_hash ( net , & m - > old_daddr , & m - > old_saddr ,
2007-02-09 00:11:42 +03:00
m - > old_family ) ;
2013-11-07 13:47:50 +04:00
hlist_for_each_entry ( x , net - > xfrm . state_bysrc + h , bysrc ) {
2007-02-09 00:11:42 +03:00
if ( x - > props . mode ! = m - > mode | |
x - > id . proto ! = m - > proto )
continue ;
xfrm: Check if_id in xfrm_migrate
This patch enables distinguishing SAs and SPs based on if_id during
the xfrm_migrate flow. This ensures support for xfrm interfaces
throughout the SA/SP lifecycle.
When there are multiple existing SPs with the same direction,
the same xfrm_selector and different endpoint addresses,
xfrm_migrate might fail with ENODATA.
Specifically, the code path for performing xfrm_migrate is:
Stage 1: find policy to migrate with
xfrm_migrate_policy_find(sel, dir, type, net)
Stage 2: find and update state(s) with
xfrm_migrate_state_find(mp, net)
Stage 3: update endpoint address(es) of template(s) with
xfrm_policy_migrate(pol, m, num_migrate)
Currently "Stage 1" always returns the first xfrm_policy that
matches, and "Stage 3" looks for the xfrm_tmpl that matches the
old endpoint address. Thus if there are multiple xfrm_policy
with same selector, direction, type and net, "Stage 1" might
rertun a wrong xfrm_policy and "Stage 3" will fail with ENODATA
because it cannot find a xfrm_tmpl with the matching endpoint
address.
The fix is to allow userspace to pass an if_id and add if_id
to the matching rule in Stage 1 and Stage 2 since if_id is a
unique ID for xfrm_policy and xfrm_state. For compatibility,
if_id will only be checked if the attribute is set.
Tested with additions to Android's kernel unit test suite:
https://android-review.googlesource.com/c/kernel/tests/+/1668886
Signed-off-by: Yan Yan <evitayan@google.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2022-01-19 03:00:13 +03:00
if ( if_id ! = 0 & & x - > if_id ! = if_id )
continue ;
2013-01-29 16:48:50 +04:00
if ( ! xfrm_addr_equal ( & x - > id . daddr , & m - > old_daddr ,
m - > old_family ) | |
! xfrm_addr_equal ( & x - > props . saddr , & m - > old_saddr ,
m - > old_family ) )
2007-02-09 00:11:42 +03:00
continue ;
xfrm_state_hold ( x ) ;
2014-02-19 16:33:24 +04:00
break ;
2007-02-09 00:11:42 +03:00
}
}
2014-02-19 16:33:24 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
return x ;
2007-02-09 00:11:42 +03:00
}
EXPORT_SYMBOL ( xfrm_migrate_state_find ) ;
2013-12-24 05:43:47 +04:00
struct xfrm_state * xfrm_state_migrate ( struct xfrm_state * x ,
2017-06-06 13:12:13 +03:00
struct xfrm_migrate * m ,
struct xfrm_encap_tmpl * encap )
2007-02-09 00:11:42 +03:00
{
struct xfrm_state * xc ;
2017-06-06 13:12:13 +03:00
xc = xfrm_state_clone ( x , encap ) ;
2007-02-09 00:11:42 +03:00
if ( ! xc )
return NULL ;
2022-01-19 03:00:14 +03:00
xc - > props . family = m - > new_family ;
if ( xfrm_init_state ( xc ) < 0 )
goto error ;
2007-02-09 00:11:42 +03:00
memcpy ( & xc - > id . daddr , & m - > new_daddr , sizeof ( xc - > id . daddr ) ) ;
memcpy ( & xc - > props . saddr , & m - > new_saddr , sizeof ( xc - > props . saddr ) ) ;
/* add state */
2013-01-29 16:48:50 +04:00
if ( xfrm_addr_equal ( & x - > id . daddr , & m - > new_daddr , m - > new_family ) ) {
2007-02-09 00:11:42 +03:00
/* 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 {
2014-02-19 16:33:24 +04:00
if ( xfrm_state_add ( xc ) < 0 )
2007-02-09 00:11:42 +03:00
goto error ;
}
return xc ;
error :
2010-12-07 02:28:56 +03:00
xfrm_state_put ( xc ) ;
2007-02-09 00:11:42 +03:00
return NULL ;
}
EXPORT_SYMBOL ( xfrm_state_migrate ) ;
# endif
2005-04-17 02:20:36 +04:00
int xfrm_state_update ( struct xfrm_state * x )
{
2008-09-03 07:14:15 +04:00
struct xfrm_state * x1 , * to_put ;
2005-04-17 02:20:36 +04:00
int err ;
2006-08-24 04:56:04 +04:00
int use_spi = xfrm_id_proto_match ( x - > id . proto , IPSEC_PROTO_ANY ) ;
2013-11-07 13:47:50 +04:00
struct net * net = xs_net ( x ) ;
2005-04-17 02:20:36 +04:00
2008-09-03 07:14:15 +04:00
to_put = NULL ;
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2006-08-24 11:42:45 +04:00
x1 = __xfrm_state_locate ( x , use_spi , x - > props . family ) ;
2005-04-17 02:20:36 +04:00
err = - ESRCH ;
if ( ! x1 )
goto out ;
if ( xfrm_state_kern ( x1 ) ) {
2008-09-03 07:14:15 +04:00
to_put = x1 ;
2005-04-17 02:20:36 +04:00
err = - EEXIST ;
goto out ;
}
if ( x1 - > km . state = = XFRM_STATE_ACQ ) {
__xfrm_state_insert ( x ) ;
x = NULL ;
}
err = 0 ;
out :
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
2008-09-03 07:14:15 +04:00
if ( to_put )
xfrm_state_put ( to_put ) ;
2005-04-17 02:20:36 +04: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 ) ) {
2017-12-26 09:34:44 +03:00
if ( x - > encap & & x1 - > encap & &
x - > encap - > encap_type = = x1 - > encap - > encap_type )
2005-04-17 02:20:36 +04:00
memcpy ( x1 - > encap , x - > encap , sizeof ( * x1 - > encap ) ) ;
2017-12-26 09:34:44 +03:00
else if ( x - > encap | | x1 - > encap )
goto fail ;
2006-08-24 05:18:55 +04: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 02:20:36 +04:00
memcpy ( & x1 - > lft , & x - > lft , sizeof ( x1 - > lft ) ) ;
x1 - > km . dying = 0 ;
2019-03-02 01:48:20 +03:00
hrtimer_start ( & x1 - > mtimer , ktime_set ( 1 , 0 ) ,
HRTIMER_MODE_REL_SOFT ) ;
2023-01-26 14:21:30 +03:00
if ( READ_ONCE ( x1 - > curlft . use_time ) )
2005-04-17 02:20:36 +04:00
xfrm_state_check_expire ( x1 ) ;
2018-07-20 05:07:47 +03:00
if ( x - > props . smark . m | | x - > props . smark . v | | x - > if_id ) {
2018-06-30 01:07:10 +03:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2018-07-20 05:07:47 +03:00
if ( x - > props . smark . m | | x - > props . smark . v )
x1 - > props . smark = x - > props . smark ;
if ( x - > if_id )
x1 - > if_id = x - > if_id ;
2018-06-30 01:07:10 +03:00
__xfrm_state_bump_genids ( x1 ) ;
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
}
2005-04-17 02:20:36 +04:00
err = 0 ;
2011-07-07 19:38:52 +04:00
x - > km . state = XFRM_STATE_DEAD ;
__xfrm_state_put ( x ) ;
2005-04-17 02:20:36 +04:00
}
2017-12-26 09:34:44 +03:00
fail :
2005-04-17 02:20:36 +04:00
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 )
{
2023-10-03 20:57:20 +03:00
xfrm_dev_state_update_stats ( x ) ;
2022-12-02 21:41:33 +03:00
2023-01-26 14:21:30 +03:00
if ( ! READ_ONCE ( x - > curlft . use_time ) )
WRITE_ONCE ( x - > curlft . use_time , ktime_get_real_seconds ( ) ) ;
2005-04-17 02:20:36 +04:00
if ( x - > curlft . bytes > = x - > lft . hard_byte_limit | |
x - > curlft . packets > = x - > lft . hard_packet_limit ) {
2005-06-19 09:43:22 +04:00
x - > km . state = XFRM_STATE_EXPIRED ;
2019-03-02 01:48:20 +03:00
hrtimer_start ( & x - > mtimer , 0 , HRTIMER_MODE_REL_SOFT ) ;
2005-04-17 02:20:36 +04:00
return - EINVAL ;
}
if ( ! x - > km . dying & &
( x - > curlft . bytes > = x - > lft . soft_byte_limit | |
2005-06-19 09:43:22 +04:00
x - > curlft . packets > = x - > lft . soft_packet_limit ) ) {
x - > km . dying = 1 ;
2006-03-21 06:17:03 +03:00
km_state_expired ( x , 0 , 0 ) ;
2005-06-19 09:43:22 +04:00
}
2005-04-17 02:20:36 +04:00
return 0 ;
}
EXPORT_SYMBOL ( xfrm_state_check_expire ) ;
2023-10-04 14:11:48 +03:00
void xfrm_state_update_stats ( struct net * net )
{
struct xfrm_state * x ;
int i ;
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
for ( i = 0 ; i < = net - > xfrm . state_hmask ; i + + ) {
hlist_for_each_entry ( x , net - > xfrm . state_bydst + i , bydst )
xfrm_dev_state_update_stats ( x ) ;
}
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
}
2005-04-17 02:20:36 +04:00
struct xfrm_state *
2011-02-28 10:17:24 +03:00
xfrm_state_lookup ( struct net * net , u32 mark , const xfrm_address_t * daddr , __be32 spi ,
2010-02-23 03:20:22 +03:00
u8 proto , unsigned short family )
2005-04-17 02:20:36 +04:00
{
struct xfrm_state * x ;
2016-09-20 16:45:26 +03:00
rcu_read_lock ( ) ;
2010-02-23 03:20:22 +03:00
x = __xfrm_state_lookup ( net , mark , daddr , spi , proto , family ) ;
2016-09-20 16:45:26 +03:00
rcu_read_unlock ( ) ;
2005-04-17 02:20:36 +04:00
return x ;
}
EXPORT_SYMBOL ( xfrm_state_lookup ) ;
struct xfrm_state *
2010-02-23 03:20:22 +03:00
xfrm_state_lookup_byaddr ( struct net * net , u32 mark ,
2011-02-28 10:17:24 +03:00
const xfrm_address_t * daddr , const xfrm_address_t * saddr ,
2006-08-24 04:56:04 +04:00
u8 proto , unsigned short family )
{
struct xfrm_state * x ;
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2010-02-23 03:20:22 +03:00
x = __xfrm_state_lookup_byaddr ( net , mark , daddr , saddr , proto , family ) ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2006-08-24 04:56:04 +04:00
return x ;
}
EXPORT_SYMBOL ( xfrm_state_lookup_byaddr ) ;
struct xfrm_state *
2013-06-27 01:56:58 +04:00
xfrm_find_acq ( struct net * net , const struct xfrm_mark * mark , u8 mode , u32 reqid ,
2018-06-12 15:07:07 +03:00
u32 if_id , u8 proto , const xfrm_address_t * daddr ,
2013-06-27 01:56:58 +04:00
const xfrm_address_t * saddr , int create , unsigned short family )
2005-04-17 02:20:36 +04:00
{
struct xfrm_state * x ;
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2018-06-12 15:07:07 +03:00
x = __find_acq_core ( net , mark , family , mode , reqid , if_id , proto , daddr , saddr , create ) ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2006-08-24 11:13:10 +04:00
2005-04-17 02:20:36 +04:00
return x ;
}
EXPORT_SYMBOL ( xfrm_find_acq ) ;
2006-08-24 09:48:31 +04:00
# ifdef CONFIG_XFRM_SUB_POLICY
2019-05-03 18:46:17 +03:00
# if IS_ENABLED(CONFIG_IPV6)
/* distribution counting sort function for xfrm_state and xfrm_tmpl */
static void
__xfrm6_sort ( void * * dst , void * * src , int n ,
int ( * cmp ) ( const void * p ) , int maxclass )
{
int count [ XFRM_MAX_DEPTH ] = { } ;
int class [ XFRM_MAX_DEPTH ] ;
int i ;
for ( i = 0 ; i < n ; i + + ) {
int c = cmp ( src [ i ] ) ;
class [ i ] = c ;
count [ c ] + + ;
}
for ( i = 2 ; i < maxclass ; i + + )
count [ i ] + = count [ i - 1 ] ;
for ( i = 0 ; i < n ; i + + ) {
dst [ count [ class [ i ] - 1 ] + + ] = src [ i ] ;
src [ i ] = NULL ;
}
}
/* Rule for xfrm_state:
*
* rule 1 : select IPsec transport except AH
* rule 2 : select MIPv6 RO or inbound trigger
* rule 3 : select IPsec transport AH
* rule 4 : select IPsec tunnel
* rule 5 : others
*/
static int __xfrm6_state_sort_cmp ( const void * p )
{
const struct xfrm_state * v = p ;
switch ( v - > props . mode ) {
case XFRM_MODE_TRANSPORT :
if ( v - > id . proto ! = IPPROTO_AH )
return 1 ;
else
return 3 ;
# if IS_ENABLED(CONFIG_IPV6_MIP6)
case XFRM_MODE_ROUTEOPTIMIZATION :
case XFRM_MODE_IN_TRIGGER :
return 2 ;
# endif
case XFRM_MODE_TUNNEL :
case XFRM_MODE_BEET :
return 4 ;
}
return 5 ;
}
/* Rule for xfrm_tmpl:
*
* rule 1 : select IPsec transport
* rule 2 : select MIPv6 RO or inbound trigger
* rule 3 : select IPsec tunnel
* rule 4 : others
*/
static int __xfrm6_tmpl_sort_cmp ( const void * p )
{
const struct xfrm_tmpl * v = p ;
switch ( v - > mode ) {
case XFRM_MODE_TRANSPORT :
return 1 ;
# if IS_ENABLED(CONFIG_IPV6_MIP6)
case XFRM_MODE_ROUTEOPTIMIZATION :
case XFRM_MODE_IN_TRIGGER :
return 2 ;
# endif
case XFRM_MODE_TUNNEL :
case XFRM_MODE_BEET :
return 3 ;
}
return 4 ;
}
# else
static inline int __xfrm6_state_sort_cmp ( const void * p ) { return 5 ; }
static inline int __xfrm6_tmpl_sort_cmp ( const void * p ) { return 4 ; }
static inline void
__xfrm6_sort ( void * * dst , void * * src , int n ,
int ( * cmp ) ( const void * p ) , int maxclass )
{
int i ;
for ( i = 0 ; i < n ; i + + )
dst [ i ] = src [ i ] ;
}
# endif /* CONFIG_IPV6 */
void
2006-08-24 09:48:31 +04:00
xfrm_tmpl_sort ( struct xfrm_tmpl * * dst , struct xfrm_tmpl * * src , int n ,
2019-05-03 18:46:17 +03:00
unsigned short family )
2006-08-24 09:48:31 +04:00
{
2017-08-01 17:21:46 +03:00
int i ;
2006-08-24 09:48:31 +04:00
2019-05-03 18:46:17 +03:00
if ( family = = AF_INET6 )
__xfrm6_sort ( ( void * * ) dst , ( void * * ) src , n ,
__xfrm6_tmpl_sort_cmp , 5 ) ;
2017-08-01 17:21:46 +03:00
else
for ( i = 0 ; i < n ; i + + )
dst [ i ] = src [ i ] ;
2006-08-24 09:48:31 +04:00
}
2019-05-03 18:46:17 +03:00
void
2006-08-24 09:48:31 +04:00
xfrm_state_sort ( struct xfrm_state * * dst , struct xfrm_state * * src , int n ,
unsigned short family )
{
2017-08-01 17:21:46 +03:00
int i ;
2013-11-07 13:47:50 +04:00
2019-05-03 18:46:17 +03:00
if ( family = = AF_INET6 )
__xfrm6_sort ( ( void * * ) dst , ( void * * ) src , n ,
__xfrm6_state_sort_cmp , 6 ) ;
2017-08-01 17:21:46 +03:00
else
for ( i = 0 ; i < n ; i + + )
dst [ i ] = src [ i ] ;
2006-08-24 09:48:31 +04:00
}
# endif
2005-04-17 02:20:36 +04:00
/* Silly enough, but I'm lazy to build resolution list */
2010-02-23 03:20:22 +03:00
static struct xfrm_state * __xfrm_find_acq_byseq ( struct net * net , u32 mark , u32 seq )
2005-04-17 02:20:36 +04:00
{
2021-04-25 22:47:12 +03:00
unsigned int h = xfrm_seq_hash ( net , seq ) ;
struct xfrm_state * x ;
2006-08-24 13:45:07 +04:00
2021-04-25 22:47:12 +03:00
hlist_for_each_entry_rcu ( x , net - > xfrm . state_byseq + h , byseq ) {
if ( x - > km . seq = = seq & &
( mark & x - > mark . m ) = = x - > mark . v & &
x - > km . state = = XFRM_STATE_ACQ ) {
xfrm_state_hold ( x ) ;
return x ;
2005-04-17 02:20:36 +04:00
}
}
2021-04-25 22:47:12 +03:00
2005-04-17 02:20:36 +04:00
return NULL ;
}
2010-02-23 03:20:22 +03:00
struct xfrm_state * xfrm_find_acq_byseq ( struct net * net , u32 mark , u32 seq )
2005-04-17 02:20:36 +04:00
{
struct xfrm_state * x ;
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2010-02-23 03:20:22 +03:00
x = __xfrm_find_acq_byseq ( net , mark , seq ) ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
return x ;
}
EXPORT_SYMBOL ( xfrm_find_acq_byseq ) ;
u32 xfrm_get_acqseq ( void )
{
u32 res ;
2010-02-16 05:01:22 +03:00
static atomic_t acqseq ;
do {
res = atomic_inc_return ( & acqseq ) ;
} while ( ! res ) ;
2005-04-17 02:20:36 +04:00
return res ;
}
EXPORT_SYMBOL ( xfrm_get_acqseq ) ;
2022-11-24 17:43:43 +03:00
int verify_spi_info ( u8 proto , u32 min , u32 max , struct netlink_ext_ack * extack )
2013-12-16 14:47:49 +04:00
{
switch ( proto ) {
case IPPROTO_AH :
case IPPROTO_ESP :
break ;
case IPPROTO_COMP :
/* IPCOMP spi is 16-bits. */
2022-11-24 17:43:43 +03:00
if ( max > = 0x10000 ) {
NL_SET_ERR_MSG ( extack , " IPCOMP SPI must be <= 65535 " ) ;
2013-12-16 14:47:49 +04:00
return - EINVAL ;
2022-11-24 17:43:43 +03:00
}
2013-12-16 14:47:49 +04:00
break ;
default :
2022-11-24 17:43:43 +03:00
NL_SET_ERR_MSG ( extack , " Invalid protocol, must be one of AH, ESP, IPCOMP " ) ;
2013-12-16 14:47:49 +04:00
return - EINVAL ;
}
2022-11-24 17:43:43 +03:00
if ( min > max ) {
NL_SET_ERR_MSG ( extack , " Invalid SPI range: min > max " ) ;
2013-12-16 14:47:49 +04:00
return - EINVAL ;
2022-11-24 17:43:43 +03:00
}
2013-12-16 14:47:49 +04:00
return 0 ;
}
EXPORT_SYMBOL ( verify_spi_info ) ;
2022-11-24 17:43:43 +03:00
int xfrm_alloc_spi ( struct xfrm_state * x , u32 low , u32 high ,
struct netlink_ext_ack * extack )
2005-04-17 02:20:36 +04:00
{
2008-11-26 04:30:50 +03:00
struct net * net = xs_net ( x ) ;
2006-08-24 14:08:07 +04:00
unsigned int h ;
2005-04-17 02:20:36 +04:00
struct xfrm_state * x0 ;
2007-10-10 00:29:52 +04:00
int err = - ENOENT ;
__be32 minspi = htonl ( low ) ;
__be32 maxspi = htonl ( high ) ;
2020-10-23 10:05:35 +03:00
__be32 newspi = 0 ;
2010-02-23 03:20:22 +03:00
u32 mark = x - > mark . v & x - > mark . m ;
2005-04-17 02:20:36 +04:00
2007-10-10 00:29:52 +04:00
spin_lock_bh ( & x - > lock ) ;
2022-11-24 17:43:43 +03:00
if ( x - > km . state = = XFRM_STATE_DEAD ) {
NL_SET_ERR_MSG ( extack , " Target ACQUIRE is in DEAD state " ) ;
2007-10-10 00:29:52 +04:00
goto unlock ;
2022-11-24 17:43:43 +03:00
}
2007-10-10 00:29:52 +04:00
err = 0 ;
2005-04-17 02:20:36 +04:00
if ( x - > id . spi )
2007-10-10 00:29:52 +04:00
goto unlock ;
err = - ENOENT ;
2005-04-17 02:20:36 +04:00
if ( minspi = = maxspi ) {
2010-02-23 03:20:22 +03:00
x0 = xfrm_state_lookup ( net , mark , & x - > id . daddr , minspi , x - > id . proto , x - > props . family ) ;
2005-04-17 02:20:36 +04:00
if ( x0 ) {
2022-11-24 17:43:43 +03:00
NL_SET_ERR_MSG ( extack , " Requested SPI is already in use " ) ;
2005-04-17 02:20:36 +04:00
xfrm_state_put ( x0 ) ;
2007-10-10 00:29:52 +04:00
goto unlock ;
2005-04-17 02:20:36 +04:00
}
2020-10-23 10:05:35 +03:00
newspi = minspi ;
2005-04-17 02:20:36 +04:00
} else {
u32 spi = 0 ;
2013-12-24 05:43:46 +04:00
for ( h = 0 ; h < high - low + 1 ; h + + ) {
2022-10-10 05:44:02 +03:00
spi = get_random_u32_inclusive ( low , high ) ;
2010-02-23 03:20:22 +03:00
x0 = xfrm_state_lookup ( net , mark , & x - > id . daddr , htonl ( spi ) , x - > id . proto , x - > props . family ) ;
2005-04-17 02:20:36 +04:00
if ( x0 = = NULL ) {
2020-10-23 10:05:35 +03:00
newspi = htonl ( spi ) ;
2005-04-17 02:20:36 +04:00
break ;
}
xfrm_state_put ( x0 ) ;
}
}
2020-10-23 10:05:35 +03:00
if ( newspi ) {
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2020-10-23 10:05:35 +03:00
x - > id . spi = newspi ;
2008-11-26 04:31:18 +03:00
h = xfrm_spi_hash ( net , & x - > id . daddr , x - > id . spi , x - > id . proto , x - > props . family ) ;
2022-12-02 21:41:32 +03:00
XFRM_STATE_INSERT ( byspi , & x - > byspi , net - > xfrm . state_byspi + h ,
x - > xso . type ) ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2007-10-10 00:29:52 +04:00
err = 0 ;
2022-11-24 17:43:43 +03:00
} else {
NL_SET_ERR_MSG ( extack , " No SPI available in the requested range " ) ;
2005-04-17 02:20:36 +04:00
}
2007-10-10 00:29:52 +04:00
unlock :
spin_unlock_bh ( & x - > lock ) ;
return err ;
2005-04-17 02:20:36 +04:00
}
EXPORT_SYMBOL ( xfrm_alloc_spi ) ;
2014-02-14 18:30:36 +04:00
static bool __xfrm_state_filter_match ( struct xfrm_state * x ,
2014-03-06 21:24:29 +04:00
struct xfrm_address_filter * filter )
2014-02-14 18:30:36 +04:00
{
if ( filter ) {
if ( ( filter - > family = = AF_INET | |
filter - > family = = AF_INET6 ) & &
x - > props . family ! = filter - > family )
return false ;
return addr_match ( & x - > props . saddr , & filter - > saddr ,
filter - > splen ) & &
addr_match ( & x - > id . daddr , & filter - > daddr ,
filter - > dplen ) ;
}
return true ;
}
2008-11-26 04:32:14 +03:00
int xfrm_state_walk ( struct net * net , struct xfrm_state_walk * walk ,
2008-02-29 08:31:08 +03:00
int ( * func ) ( struct xfrm_state * , int , void * ) ,
2005-04-17 02:20:36 +04:00
void * data )
{
2008-10-01 18:03:24 +04:00
struct xfrm_state * state ;
struct xfrm_state_walk * x ;
2005-04-17 02:20:36 +04:00
int err = 0 ;
2008-10-01 18:03:24 +04:00
if ( walk - > seq ! = 0 & & list_empty ( & walk - > all ) )
2008-02-29 08:31:08 +03:00
return 0 ;
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2008-10-01 18:03:24 +04:00
if ( list_empty ( & walk - > all ) )
2008-11-26 04:32:14 +03:00
x = list_first_entry ( & net - > xfrm . state_all , struct xfrm_state_walk , all ) ;
2008-10-01 18:03:24 +04:00
else
2015-04-22 12:09:54 +03:00
x = list_first_entry ( & walk - > all , struct xfrm_state_walk , all ) ;
2008-11-26 04:32:14 +03:00
list_for_each_entry_from ( x , & net - > xfrm . state_all , all ) {
2008-10-01 18:03:24 +04:00
if ( x - > state = = XFRM_STATE_DEAD )
2008-02-29 08:31:08 +03:00
continue ;
2008-10-01 18:03:24 +04:00
state = container_of ( x , struct xfrm_state , km ) ;
if ( ! xfrm_id_proto_match ( state - > id . proto , walk - > proto ) )
2008-02-29 08:31:08 +03:00
continue ;
2014-02-14 18:30:36 +04:00
if ( ! __xfrm_state_filter_match ( state , walk - > filter ) )
continue ;
2008-10-01 18:03:24 +04:00
err = func ( state , walk - > seq , data ) ;
if ( err ) {
list_move_tail ( & walk - > all , & x - > all ) ;
goto out ;
2005-04-17 02:20:36 +04:00
}
2008-10-01 18:03:24 +04:00
walk - > seq + + ;
2005-04-17 02:20:36 +04:00
}
2008-10-01 18:03:24 +04:00
if ( walk - > seq = = 0 ) {
2005-04-17 02:20:36 +04:00
err = - ENOENT ;
goto out ;
}
2008-10-01 18:03:24 +04:00
list_del_init ( & walk - > all ) ;
2005-04-17 02:20:36 +04:00
out :
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2005-04-17 02:20:36 +04:00
return err ;
}
EXPORT_SYMBOL ( xfrm_state_walk ) ;
2014-02-14 18:30:36 +04:00
void xfrm_state_walk_init ( struct xfrm_state_walk * walk , u8 proto ,
2014-03-06 21:24:29 +04:00
struct xfrm_address_filter * filter )
2008-09-23 06:48:19 +04:00
{
2008-10-01 18:03:24 +04:00
INIT_LIST_HEAD ( & walk - > all ) ;
2008-09-23 06:48:19 +04:00
walk - > proto = proto ;
2008-10-01 18:03:24 +04:00
walk - > state = XFRM_STATE_DEAD ;
walk - > seq = 0 ;
2014-02-14 18:30:36 +04:00
walk - > filter = filter ;
2008-09-23 06:48:19 +04:00
}
EXPORT_SYMBOL ( xfrm_state_walk_init ) ;
2013-11-07 13:47:50 +04:00
void xfrm_state_walk_done ( struct xfrm_state_walk * walk , struct net * net )
2008-09-10 06:58:29 +04:00
{
2014-02-14 18:30:36 +04:00
kfree ( walk - > filter ) ;
2008-10-01 18:03:24 +04:00
if ( list_empty ( & walk - > all ) )
2008-09-23 06:48:19 +04:00
return ;
2013-11-07 13:47:50 +04:00
spin_lock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2008-10-01 18:03:24 +04:00
list_del ( & walk - > all ) ;
2013-11-07 13:47:50 +04:00
spin_unlock_bh ( & net - > xfrm . xfrm_state_lock ) ;
2008-09-10 06:58:29 +04:00
}
EXPORT_SYMBOL ( xfrm_state_walk_done ) ;
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 00:43:17 +03:00
static void xfrm_replay_timer_handler ( struct timer_list * t )
2006-03-21 06:15:11 +03:00
{
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 00:43:17 +03:00
struct xfrm_state * x = from_timer ( x , t , rtimer ) ;
2006-03-21 06:15:11 +03:00
spin_lock ( & x - > lock ) ;
2006-04-15 02:03:05 +04:00
if ( x - > km . state = = XFRM_STATE_VALID ) {
2008-11-26 04:38:20 +03:00
if ( xfrm_aevent_is_on ( xs_net ( x ) ) )
2021-06-18 16:51:56 +03:00
xfrm_replay_notify ( x , XFRM_REPLAY_TIMEOUT ) ;
2006-04-15 02:03:05 +04:00
else
x - > xflags | = XFRM_TIME_DEFER ;
}
2006-03-21 06:15:11 +03:00
spin_unlock ( & x - > lock ) ;
}
2007-12-07 11:51:11 +03:00
static LIST_HEAD ( xfrm_km_list ) ;
2005-04-17 02:20:36 +04:00
2011-02-24 08:02:38 +03:00
void km_policy_notify ( struct xfrm_policy * xp , int dir , const struct km_event * c )
2005-04-17 02:20:36 +04:00
{
struct xfrm_mgr * km ;
2013-01-16 12:05:06 +04:00
rcu_read_lock ( ) ;
list_for_each_entry_rcu ( km , & xfrm_km_list , list )
2005-06-19 09:42:13 +04:00
if ( km - > notify_policy )
km - > notify_policy ( xp , dir , c ) ;
2013-01-16 12:05:06 +04:00
rcu_read_unlock ( ) ;
2005-06-19 09:42:13 +04:00
}
2005-04-17 02:20:36 +04:00
2011-02-24 08:02:38 +03:00
void km_state_notify ( struct xfrm_state * x , const struct km_event * c )
2005-06-19 09:42:13 +04:00
{
struct xfrm_mgr * km ;
2013-01-16 12:05:06 +04:00
rcu_read_lock ( ) ;
list_for_each_entry_rcu ( km , & xfrm_km_list , list )
2005-06-19 09:42:13 +04:00
if ( km - > notify )
km - > notify ( x , c ) ;
2013-01-16 12:05:06 +04:00
rcu_read_unlock ( ) ;
2005-06-19 09:42:13 +04:00
}
EXPORT_SYMBOL ( km_policy_notify ) ;
EXPORT_SYMBOL ( km_state_notify ) ;
2012-09-08 00:12:54 +04:00
void km_state_expired ( struct xfrm_state * x , int hard , u32 portid )
2005-06-19 09:42:13 +04:00
{
struct km_event c ;
2005-06-19 09:44:00 +04:00
c . data . hard = hard ;
2012-09-08 00:12:54 +04:00
c . portid = portid ;
2005-06-19 09:44:37 +04:00
c . event = XFRM_MSG_EXPIRE ;
2005-06-19 09:42:13 +04:00
km_state_notify ( x , & c ) ;
2005-04-17 02:20:36 +04:00
}
2006-03-21 06:17:03 +03:00
EXPORT_SYMBOL ( km_state_expired ) ;
2005-06-19 09:42:13 +04: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 06:16:40 +03:00
int km_query ( struct xfrm_state * x , struct xfrm_tmpl * t , struct xfrm_policy * pol )
2005-04-17 02:20:36 +04:00
{
2005-06-19 09:42:13 +04:00
int err = - EINVAL , acqret ;
2005-04-17 02:20:36 +04:00
struct xfrm_mgr * km ;
2013-01-16 12:05:06 +04:00
rcu_read_lock ( ) ;
list_for_each_entry_rcu ( km , & xfrm_km_list , list ) {
2012-08-15 06:13:47 +04:00
acqret = km - > acquire ( x , t , pol ) ;
2005-06-19 09:42:13 +04:00
if ( ! acqret )
err = acqret ;
2005-04-17 02:20:36 +04:00
}
2013-01-16 12:05:06 +04:00
rcu_read_unlock ( ) ;
2005-04-17 02:20:36 +04:00
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 06:16:40 +03:00
EXPORT_SYMBOL ( km_query ) ;
2005-04-17 02:20:36 +04:00
2021-12-22 16:11:18 +03:00
static int __km_new_mapping ( struct xfrm_state * x , xfrm_address_t * ipaddr , __be16 sport )
2005-04-17 02:20:36 +04:00
{
int err = - EINVAL ;
struct xfrm_mgr * km ;
2013-01-16 12:05:06 +04:00
rcu_read_lock ( ) ;
list_for_each_entry_rcu ( km , & xfrm_km_list , list ) {
2005-04-17 02:20:36 +04:00
if ( km - > new_mapping )
err = km - > new_mapping ( x , ipaddr , sport ) ;
if ( ! err )
break ;
}
2013-01-16 12:05:06 +04:00
rcu_read_unlock ( ) ;
2005-04-17 02:20:36 +04:00
return err ;
}
2021-12-22 16:11:18 +03:00
int km_new_mapping ( struct xfrm_state * x , xfrm_address_t * ipaddr , __be16 sport )
{
int ret = 0 ;
if ( x - > mapping_maxage ) {
if ( ( jiffies / HZ - x - > new_mapping ) > x - > mapping_maxage | |
x - > new_mapping_sport ! = sport ) {
x - > new_mapping_sport = sport ;
x - > new_mapping = jiffies / HZ ;
ret = __km_new_mapping ( x , ipaddr , sport ) ;
}
} else {
ret = __km_new_mapping ( x , ipaddr , sport ) ;
}
return ret ;
}
2005-04-17 02:20:36 +04:00
EXPORT_SYMBOL ( km_new_mapping ) ;
2012-09-08 00:12:54 +04:00
void km_policy_expired ( struct xfrm_policy * pol , int dir , int hard , u32 portid )
2005-04-17 02:20:36 +04:00
{
2005-06-19 09:42:13 +04:00
struct km_event c ;
2005-04-17 02:20:36 +04:00
2005-06-19 09:44:00 +04:00
c . data . hard = hard ;
2012-09-08 00:12:54 +04:00
c . portid = portid ;
2005-06-19 09:44:37 +04:00
c . event = XFRM_MSG_POLEXPIRE ;
2005-06-19 09:42:13 +04:00
km_policy_notify ( pol , dir , & c ) ;
2005-04-17 02:20:36 +04:00
}
2006-03-21 06:18:52 +03:00
EXPORT_SYMBOL ( km_policy_expired ) ;
2005-04-17 02:20:36 +04:00
2008-01-04 07:43:21 +03:00
# ifdef CONFIG_XFRM_MIGRATE
2011-02-24 08:28:01 +03:00
int km_migrate ( const struct xfrm_selector * sel , u8 dir , u8 type ,
const struct xfrm_migrate * m , int num_migrate ,
2017-06-06 13:12:14 +03:00
const struct xfrm_kmaddress * k ,
const struct xfrm_encap_tmpl * encap )
2007-02-09 00:11:42 +03:00
{
int err = - EINVAL ;
int ret ;
struct xfrm_mgr * km ;
2013-01-16 12:05:06 +04:00
rcu_read_lock ( ) ;
list_for_each_entry_rcu ( km , & xfrm_km_list , list ) {
2007-02-09 00:11:42 +03:00
if ( km - > migrate ) {
2017-06-06 13:12:14 +03:00
ret = km - > migrate ( sel , dir , type , m , num_migrate , k ,
encap ) ;
2007-02-09 00:11:42 +03:00
if ( ! ret )
err = ret ;
}
}
2013-01-16 12:05:06 +04:00
rcu_read_unlock ( ) ;
2007-02-09 00:11:42 +03:00
return err ;
}
EXPORT_SYMBOL ( km_migrate ) ;
2008-01-04 07:43:21 +03:00
# endif
2007-02-09 00:11:42 +03:00
2008-11-26 04:51:01 +03:00
int km_report ( struct net * net , u8 proto , struct xfrm_selector * sel , xfrm_address_t * addr )
2006-08-24 07:44:06 +04:00
{
int err = - EINVAL ;
int ret ;
struct xfrm_mgr * km ;
2013-01-16 12:05:06 +04:00
rcu_read_lock ( ) ;
list_for_each_entry_rcu ( km , & xfrm_km_list , list ) {
2006-08-24 07:44:06 +04:00
if ( km - > report ) {
2008-11-26 04:51:01 +03:00
ret = km - > report ( net , proto , sel , addr ) ;
2006-08-24 07:44:06 +04:00
if ( ! ret )
err = ret ;
}
}
2013-01-16 12:05:06 +04:00
rcu_read_unlock ( ) ;
2006-08-24 07:44:06 +04:00
return err ;
}
EXPORT_SYMBOL ( km_report ) ;
2019-04-17 12:45:13 +03:00
static bool km_is_alive ( const struct km_event * c )
2014-02-12 18:20:06 +04:00
{
struct xfrm_mgr * km ;
bool is_alive = false ;
rcu_read_lock ( ) ;
list_for_each_entry_rcu ( km , & xfrm_km_list , list ) {
if ( km - > is_alive & & km - > is_alive ( c ) ) {
is_alive = true ;
break ;
}
}
rcu_read_unlock ( ) ;
return is_alive ;
}
2020-09-21 17:36:51 +03:00
# if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
static DEFINE_SPINLOCK ( xfrm_translator_lock ) ;
static struct xfrm_translator __rcu * xfrm_translator ;
struct xfrm_translator * xfrm_get_translator ( void )
{
struct xfrm_translator * xtr ;
rcu_read_lock ( ) ;
xtr = rcu_dereference ( xfrm_translator ) ;
if ( unlikely ( ! xtr ) )
goto out ;
if ( ! try_module_get ( xtr - > owner ) )
xtr = NULL ;
out :
rcu_read_unlock ( ) ;
return xtr ;
}
EXPORT_SYMBOL_GPL ( xfrm_get_translator ) ;
void xfrm_put_translator ( struct xfrm_translator * xtr )
{
module_put ( xtr - > owner ) ;
}
EXPORT_SYMBOL_GPL ( xfrm_put_translator ) ;
int xfrm_register_translator ( struct xfrm_translator * xtr )
{
int err = 0 ;
spin_lock_bh ( & xfrm_translator_lock ) ;
if ( unlikely ( xfrm_translator ! = NULL ) )
err = - EEXIST ;
else
rcu_assign_pointer ( xfrm_translator , xtr ) ;
spin_unlock_bh ( & xfrm_translator_lock ) ;
return err ;
}
EXPORT_SYMBOL_GPL ( xfrm_register_translator ) ;
int xfrm_unregister_translator ( struct xfrm_translator * xtr )
{
int err = 0 ;
spin_lock_bh ( & xfrm_translator_lock ) ;
if ( likely ( xfrm_translator ! = NULL ) ) {
if ( rcu_access_pointer ( xfrm_translator ) ! = xtr )
err = - EINVAL ;
else
RCU_INIT_POINTER ( xfrm_translator , NULL ) ;
}
spin_unlock_bh ( & xfrm_translator_lock ) ;
synchronize_rcu ( ) ;
return err ;
}
EXPORT_SYMBOL_GPL ( xfrm_unregister_translator ) ;
# endif
2020-07-23 09:08:51 +03:00
int xfrm_user_policy ( struct sock * sk , int optname , sockptr_t optval , int optlen )
2005-04-17 02:20:36 +04:00
{
int err ;
u8 * data ;
struct xfrm_mgr * km ;
struct xfrm_policy * pol = NULL ;
2020-07-23 09:08:51 +03:00
if ( sockptr_is_null ( optval ) & & ! optlen ) {
2017-11-20 13:26:02 +03:00
xfrm_sk_policy_insert ( sk , XFRM_POLICY_IN , NULL ) ;
xfrm_sk_policy_insert ( sk , XFRM_POLICY_OUT , NULL ) ;
__sk_dst_reset ( sk ) ;
return 0 ;
}
2005-04-17 02:20:36 +04:00
if ( optlen < = 0 | | optlen > PAGE_SIZE )
return - EMSGSIZE ;
2020-07-23 09:08:51 +03:00
data = memdup_sockptr ( optval , optlen ) ;
2017-05-06 18:42:21 +03:00
if ( IS_ERR ( data ) )
return PTR_ERR ( data ) ;
2005-04-17 02:20:36 +04:00
2020-09-21 17:36:56 +03:00
if ( in_compat_syscall ( ) ) {
struct xfrm_translator * xtr = xfrm_get_translator ( ) ;
2020-11-10 04:14:43 +03:00
if ( ! xtr ) {
kfree ( data ) ;
2020-09-21 17:36:56 +03:00
return - EOPNOTSUPP ;
2020-11-10 04:14:43 +03:00
}
2020-09-21 17:36:56 +03:00
err = xtr - > xlate_user_policy_sockptr ( & data , optlen ) ;
xfrm_put_translator ( xtr ) ;
if ( err ) {
kfree ( data ) ;
return err ;
}
}
2005-04-17 02:20:36 +04:00
err = - EINVAL ;
2013-01-16 12:05:06 +04:00
rcu_read_lock ( ) ;
list_for_each_entry_rcu ( km , & xfrm_km_list , list ) {
2006-07-25 10:32:20 +04:00
pol = km - > compile_policy ( sk , optname , data ,
2005-04-17 02:20:36 +04:00
optlen , & err ) ;
if ( err > = 0 )
break ;
}
2013-01-16 12:05:06 +04:00
rcu_read_unlock ( ) ;
2005-04-17 02:20:36 +04:00
if ( err > = 0 ) {
xfrm_sk_policy_insert ( sk , err , pol ) ;
xfrm_pol_put ( pol ) ;
2017-10-25 19:52:27 +03:00
__sk_dst_reset ( sk ) ;
2005-04-17 02:20:36 +04:00
err = 0 ;
}
kfree ( data ) ;
return err ;
}
EXPORT_SYMBOL ( xfrm_user_policy ) ;
2013-01-16 12:05:06 +04:00
static DEFINE_SPINLOCK ( xfrm_km_lock ) ;
2022-06-15 04:55:19 +03:00
void xfrm_register_km ( struct xfrm_mgr * km )
2005-04-17 02:20:36 +04:00
{
2013-01-16 12:05:06 +04:00
spin_lock_bh ( & xfrm_km_lock ) ;
list_add_tail_rcu ( & km - > list , & xfrm_km_list ) ;
spin_unlock_bh ( & xfrm_km_lock ) ;
2005-04-17 02:20:36 +04:00
}
EXPORT_SYMBOL ( xfrm_register_km ) ;
2022-06-15 04:55:19 +03:00
void xfrm_unregister_km ( struct xfrm_mgr * km )
2005-04-17 02:20:36 +04:00
{
2013-01-16 12:05:06 +04:00
spin_lock_bh ( & xfrm_km_lock ) ;
list_del_rcu ( & km - > list ) ;
spin_unlock_bh ( & xfrm_km_lock ) ;
synchronize_rcu ( ) ;
2005-04-17 02:20:36 +04:00
}
EXPORT_SYMBOL ( xfrm_unregister_km ) ;
int xfrm_state_register_afinfo ( struct xfrm_state_afinfo * afinfo )
{
int err = 0 ;
2017-01-09 16:20:46 +03:00
if ( WARN_ON ( afinfo - > family > = NPROTO ) )
2005-04-17 02:20:36 +04:00
return - EAFNOSUPPORT ;
2017-01-09 16:20:46 +03:00
2013-01-16 12:05:05 +04:00
spin_lock_bh ( & xfrm_state_afinfo_lock ) ;
2005-04-17 02:20:36 +04:00
if ( unlikely ( xfrm_state_afinfo [ afinfo - > family ] ! = NULL ) )
2015-04-23 06:06:53 +03:00
err = - EEXIST ;
2006-08-24 11:42:45 +04:00
else
2013-01-16 12:05:05 +04:00
rcu_assign_pointer ( xfrm_state_afinfo [ afinfo - > family ] , afinfo ) ;
spin_unlock_bh ( & xfrm_state_afinfo_lock ) ;
2005-04-17 02:20:36 +04:00
return err ;
}
EXPORT_SYMBOL ( xfrm_state_register_afinfo ) ;
int xfrm_state_unregister_afinfo ( struct xfrm_state_afinfo * afinfo )
{
2017-01-09 16:20:46 +03:00
int err = 0 , family = afinfo - > family ;
if ( WARN_ON ( family > = NPROTO ) )
2005-04-17 02:20:36 +04:00
return - EAFNOSUPPORT ;
2017-01-09 16:20:46 +03:00
2013-01-16 12:05:05 +04:00
spin_lock_bh ( & xfrm_state_afinfo_lock ) ;
2005-04-17 02:20:36 +04:00
if ( likely ( xfrm_state_afinfo [ afinfo - > family ] ! = NULL ) ) {
2017-01-09 16:20:46 +03:00
if ( rcu_access_pointer ( xfrm_state_afinfo [ family ] ) ! = afinfo )
2005-04-17 02:20:36 +04:00
err = - EINVAL ;
2006-08-24 11:42:45 +04:00
else
2013-01-16 12:05:05 +04:00
RCU_INIT_POINTER ( xfrm_state_afinfo [ afinfo - > family ] , NULL ) ;
2005-04-17 02:20:36 +04:00
}
2013-01-16 12:05:05 +04:00
spin_unlock_bh ( & xfrm_state_afinfo_lock ) ;
synchronize_rcu ( ) ;
2005-04-17 02:20:36 +04:00
return err ;
}
EXPORT_SYMBOL ( xfrm_state_unregister_afinfo ) ;
2017-01-09 16:20:48 +03:00
struct xfrm_state_afinfo * xfrm_state_afinfo_get_rcu ( unsigned int family )
{
if ( unlikely ( family > = NPROTO ) )
return NULL ;
return rcu_dereference ( xfrm_state_afinfo [ family ] ) ;
}
2019-03-29 23:16:30 +03:00
EXPORT_SYMBOL_GPL ( xfrm_state_afinfo_get_rcu ) ;
2017-01-09 16:20:48 +03:00
2013-08-14 15:05:23 +04:00
struct xfrm_state_afinfo * xfrm_state_get_afinfo ( unsigned int family )
2005-04-17 02:20:36 +04:00
{
struct xfrm_state_afinfo * afinfo ;
if ( unlikely ( family > = NPROTO ) )
return NULL ;
2013-01-16 12:05:05 +04:00
rcu_read_lock ( ) ;
afinfo = rcu_dereference ( xfrm_state_afinfo [ family ] ) ;
2006-05-28 10:03:58 +04:00
if ( unlikely ( ! afinfo ) )
2013-01-16 12:05:05 +04:00
rcu_read_unlock ( ) ;
2005-04-17 02:20:36 +04:00
return afinfo ;
}
2018-04-16 08:50:09 +03:00
void xfrm_flush_gc ( void )
{
flush_work ( & xfrm_state_gc_work ) ;
}
EXPORT_SYMBOL ( xfrm_flush_gc ) ;
2005-04-17 02:20:36 +04: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: destroy xfrm_state synchronously on net exit path
xfrm_state_put() moves struct xfrm_state to the GC list
and schedules the GC work to clean it up. On net exit call
path, xfrm_state_flush() is called to clean up and
xfrm_flush_gc() is called to wait for the GC work to complete
before exit.
However, this doesn't work because one of the ->destructor(),
ipcomp_destroy(), schedules the same GC work again inside
the GC work. It is hard to wait for such a nested async
callback. This is also why syzbot still reports the following
warning:
WARNING: CPU: 1 PID: 33 at net/ipv6/xfrm6_tunnel.c:351 xfrm6_tunnel_net_exit+0x2cb/0x500 net/ipv6/xfrm6_tunnel.c:351
...
ops_exit_list.isra.0+0xb0/0x160 net/core/net_namespace.c:153
cleanup_net+0x51d/0xb10 net/core/net_namespace.c:551
process_one_work+0xd0c/0x1ce0 kernel/workqueue.c:2153
worker_thread+0x143/0x14a0 kernel/workqueue.c:2296
kthread+0x357/0x430 kernel/kthread.c:246
ret_from_fork+0x3a/0x50 arch/x86/entry/entry_64.S:352
In fact, it is perfectly fine to bypass GC and destroy xfrm_state
synchronously on net exit call path, because it is in process context
and doesn't need a work struct to do any blocking work.
This patch introduces xfrm_state_put_sync() which simply bypasses
GC, and lets its callers to decide whether to use this synchronous
version. On net exit path, xfrm_state_fini() and
xfrm6_tunnel_net_exit() use it. And, as ipcomp_destroy() itself is
blocking, it can use xfrm_state_put_sync() directly too.
Also rename xfrm_state_gc_destroy() to ___xfrm_state_destroy() to
reflect this change.
Fixes: b48c05ab5d32 ("xfrm: Fix warning in xfrm6_tunnel_net_exit.")
Reported-and-tested-by: syzbot+e9aebef558e3ed673934@syzkaller.appspotmail.com
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2019-02-01 00:05:49 +03:00
xfrm_state_put_sync ( t ) ;
2005-04-17 02:20:36 +04:00
x - > tunnel = NULL ;
}
}
EXPORT_SYMBOL ( xfrm_state_delete_tunnel ) ;
2022-01-26 18:00:18 +03:00
u32 xfrm_state_mtu ( struct xfrm_state * x , int mtu )
2005-04-17 02:20:36 +04:00
{
2017-01-05 15:23:58 +03:00
const struct xfrm_type * type = READ_ONCE ( x - > type ) ;
2019-06-24 23:04:48 +03:00
struct crypto_aead * aead ;
u32 blksize , net_adj = 0 ;
if ( x - > km . state ! = XFRM_STATE_VALID | |
! type | | type - > proto ! = IPPROTO_ESP )
return mtu - x - > props . header_len ;
aead = x - > data ;
blksize = ALIGN ( crypto_aead_blocksize ( aead ) , 4 ) ;
2005-04-17 02:20:36 +04:00
2019-06-24 23:04:48 +03:00
switch ( x - > props . mode ) {
case XFRM_MODE_TRANSPORT :
case XFRM_MODE_BEET :
if ( x - > props . family = = AF_INET )
net_adj = sizeof ( struct iphdr ) ;
else if ( x - > props . family = = AF_INET6 )
net_adj = sizeof ( struct ipv6hdr ) ;
break ;
case XFRM_MODE_TUNNEL :
break ;
default :
WARN_ON_ONCE ( 1 ) ;
break ;
}
2017-01-05 15:23:58 +03:00
2019-06-24 23:04:48 +03:00
return ( ( mtu - x - > props . header_len - crypto_aead_authsize ( aead ) -
net_adj ) & ~ ( blksize - 1 ) ) + net_adj - 2 ;
2005-04-17 02:20:36 +04:00
}
2022-01-26 18:00:18 +03:00
EXPORT_SYMBOL_GPL ( xfrm_state_mtu ) ;
2005-04-17 02:20:36 +04:00
2022-09-14 20:04:05 +03:00
int __xfrm_init_state ( struct xfrm_state * x , bool init_replay , bool offload ,
struct netlink_ext_ack * extack )
2005-06-21 00:18:08 +04:00
{
2019-03-29 23:16:32 +03:00
const struct xfrm_mode * inner_mode ;
const struct xfrm_mode * outer_mode ;
2005-06-21 00:19:41 +04:00
int family = x - > props . family ;
2005-06-21 00:18:08 +04:00
int err ;
2019-05-03 18:46:16 +03:00
if ( family = = AF_INET & &
2022-07-13 23:51:52 +03:00
READ_ONCE ( xs_net ( x ) - > ipv4 . sysctl_ip_no_pmtu_disc ) )
2019-05-03 18:46:16 +03:00
x - > props . flags | = XFRM_STATE_NOPMTUDISC ;
2005-06-21 00:19:41 +04:00
err = - EPROTONOSUPPORT ;
2007-10-18 08:35:51 +04:00
2008-03-25 00:51:51 +03:00
if ( x - > sel . family ! = AF_UNSPEC ) {
inner_mode = xfrm_get_mode ( x - > props . mode , x - > sel . family ) ;
2022-09-14 20:04:05 +03:00
if ( inner_mode = = NULL ) {
NL_SET_ERR_MSG ( extack , " Requested mode not found " ) ;
2008-03-25 00:51:51 +03:00
goto error ;
2022-09-14 20:04:05 +03:00
}
2008-03-25 00:51:51 +03:00
if ( ! ( inner_mode - > flags & XFRM_MODE_FLAG_TUNNEL ) & &
2022-09-14 20:04:05 +03:00
family ! = x - > sel . family ) {
NL_SET_ERR_MSG ( extack , " Only tunnel modes can accommodate a change of family " ) ;
2008-03-25 00:51:51 +03:00
goto error ;
2022-09-14 20:04:05 +03:00
}
2008-03-25 00:51:51 +03:00
2019-03-29 23:16:32 +03:00
x - > inner_mode = * inner_mode ;
2008-03-25 00:51:51 +03:00
} else {
2019-03-29 23:16:31 +03:00
const struct xfrm_mode * inner_mode_iaf ;
2008-12-04 02:38:07 +03:00
int iafamily = AF_INET ;
2008-03-25 00:51:51 +03:00
2008-12-04 02:38:07 +03:00
inner_mode = xfrm_get_mode ( x - > props . mode , x - > props . family ) ;
2022-09-14 20:04:05 +03:00
if ( inner_mode = = NULL ) {
NL_SET_ERR_MSG ( extack , " Requested mode not found " ) ;
2008-03-25 00:51:51 +03:00
goto error ;
2022-09-14 20:04:05 +03:00
}
2008-03-25 00:51:51 +03:00
2019-03-29 23:16:32 +03:00
x - > inner_mode = * inner_mode ;
2008-03-25 00:51:51 +03:00
2008-12-04 02:38:07 +03:00
if ( x - > props . family = = AF_INET )
iafamily = AF_INET6 ;
2008-03-25 00:51:51 +03:00
2008-12-04 02:38:07 +03:00
inner_mode_iaf = xfrm_get_mode ( x - > props . mode , iafamily ) ;
if ( inner_mode_iaf ) {
if ( inner_mode_iaf - > flags & XFRM_MODE_FLAG_TUNNEL )
2019-03-29 23:16:32 +03:00
x - > inner_mode_iaf = * inner_mode_iaf ;
2008-03-25 00:51:51 +03:00
}
}
2007-10-18 08:35:51 +04:00
2005-06-21 00:19:41 +04:00
x - > type = xfrm_get_type ( x - > id . proto , family ) ;
2022-09-14 20:04:05 +03:00
if ( x - > type = = NULL ) {
NL_SET_ERR_MSG ( extack , " Requested type not found " ) ;
2005-06-21 00:18:08 +04:00
goto error ;
2022-09-14 20:04:05 +03:00
}
2005-06-21 00:18:08 +04:00
2017-08-01 12:49:08 +03:00
x - > type_offload = xfrm_get_type_offload ( x - > id . proto , family , offload ) ;
2017-04-14 11:05:44 +03:00
2022-09-27 18:45:29 +03:00
err = x - > type - > init_state ( x , extack ) ;
2005-06-21 00:18:08 +04:00
if ( err )
goto error ;
2019-03-29 23:16:32 +03:00
outer_mode = xfrm_get_mode ( x - > props . mode , family ) ;
if ( ! outer_mode ) {
2022-09-14 20:04:05 +03:00
NL_SET_ERR_MSG ( extack , " Requested mode not found " ) ;
2012-08-29 10:49:15 +04:00
err = - EPROTONOSUPPORT ;
2006-05-28 10:05:54 +04:00
goto error ;
2012-08-29 10:49:15 +04:00
}
2006-05-28 10:05:54 +04:00
2019-03-29 23:16:32 +03:00
x - > outer_mode = * outer_mode ;
2011-03-22 04:08:28 +03:00
if ( init_replay ) {
2022-09-14 20:04:06 +03:00
err = xfrm_init_replay ( x , extack ) ;
2011-03-22 04:08:28 +03:00
if ( err )
goto error ;
}
2005-06-21 00:18:08 +04:00
error :
return err ;
}
2011-03-22 04:08:28 +03:00
EXPORT_SYMBOL ( __xfrm_init_state ) ;
int xfrm_init_state ( struct xfrm_state * x )
{
2018-01-17 16:52:41 +03:00
int err ;
2022-09-14 20:04:05 +03:00
err = __xfrm_init_state ( x , true , false , NULL ) ;
2018-01-17 16:52:41 +03:00
if ( ! err )
x - > km . state = XFRM_STATE_VALID ;
return err ;
2011-03-22 04:08:28 +03:00
}
2005-06-21 00:18:08 +04:00
EXPORT_SYMBOL ( xfrm_init_state ) ;
2007-02-09 17:25:29 +03:00
2008-11-26 04:14:31 +03:00
int __net_init xfrm_state_init ( struct net * net )
2005-04-17 02:20:36 +04:00
{
2006-08-24 14:08:07 +04:00
unsigned int sz ;
2018-05-03 11:55:07 +03:00
if ( net_eq ( net , & init_net ) )
xfrm_state_cache = KMEM_CACHE ( xfrm_state ,
SLAB_HWCACHE_ALIGN | SLAB_PANIC ) ;
2008-11-26 04:16:11 +03:00
INIT_LIST_HEAD ( & net - > xfrm . state_all ) ;
2006-08-24 14:08:07 +04:00
sz = sizeof ( struct hlist_head ) * 8 ;
2008-11-26 04:16:58 +03:00
net - > xfrm . state_bydst = xfrm_hash_alloc ( sz ) ;
if ( ! net - > xfrm . state_bydst )
goto out_bydst ;
2008-11-26 04:17:24 +03:00
net - > xfrm . state_bysrc = xfrm_hash_alloc ( sz ) ;
if ( ! net - > xfrm . state_bysrc )
goto out_bysrc ;
2008-11-26 04:17:47 +03:00
net - > xfrm . state_byspi = xfrm_hash_alloc ( sz ) ;
if ( ! net - > xfrm . state_byspi )
goto out_byspi ;
2021-04-25 22:47:12 +03:00
net - > xfrm . state_byseq = xfrm_hash_alloc ( sz ) ;
if ( ! net - > xfrm . state_byseq )
goto out_byseq ;
2008-11-26 04:18:12 +03:00
net - > xfrm . state_hmask = ( ( sz / sizeof ( struct hlist_head ) ) - 1 ) ;
2005-04-17 02:20:36 +04:00
2008-11-26 04:18:39 +03:00
net - > xfrm . state_num = 0 ;
2008-11-26 04:19:07 +03:00
INIT_WORK ( & net - > xfrm . state_hash_work , xfrm_hash_resize ) ;
2013-11-07 13:47:50 +04:00
spin_lock_init ( & net - > xfrm . xfrm_state_lock ) ;
2021-03-16 13:56:30 +03:00
seqcount_spinlock_init ( & net - > xfrm . xfrm_state_hash_generation ,
& net - > xfrm . xfrm_state_lock ) ;
2008-11-26 04:14:31 +03:00
return 0 ;
2008-11-26 04:16:58 +03:00
2021-04-25 22:47:12 +03:00
out_byseq :
xfrm_hash_free ( net - > xfrm . state_byspi , sz ) ;
2008-11-26 04:17:47 +03:00
out_byspi :
xfrm_hash_free ( net - > xfrm . state_bysrc , sz ) ;
2008-11-26 04:17:24 +03:00
out_bysrc :
xfrm_hash_free ( net - > xfrm . state_bydst , sz ) ;
2008-11-26 04:16:58 +03:00
out_bydst :
return - ENOMEM ;
2008-11-26 04:14:31 +03:00
}
void xfrm_state_fini ( struct net * net )
{
2008-11-26 04:16:58 +03:00
unsigned int sz ;
2008-11-26 04:57:44 +03:00
flush_work ( & net - > xfrm . state_hash_work ) ;
2016-08-23 17:00:12 +03:00
flush_work ( & xfrm_state_gc_work ) ;
2019-03-23 02:26:19 +03:00
xfrm_state_flush ( net , 0 , false , true ) ;
2008-11-26 04:57:44 +03:00
2008-11-26 04:16:11 +03:00
WARN_ON ( ! list_empty ( & net - > xfrm . state_all ) ) ;
2008-11-26 04:16:58 +03:00
2008-11-26 04:18:12 +03:00
sz = ( net - > xfrm . state_hmask + 1 ) * sizeof ( struct hlist_head ) ;
2021-04-25 22:47:12 +03:00
WARN_ON ( ! hlist_empty ( net - > xfrm . state_byseq ) ) ;
xfrm_hash_free ( net - > xfrm . state_byseq , sz ) ;
2008-11-26 04:17:47 +03:00
WARN_ON ( ! hlist_empty ( net - > xfrm . state_byspi ) ) ;
xfrm_hash_free ( net - > xfrm . state_byspi , sz ) ;
2008-11-26 04:17:24 +03:00
WARN_ON ( ! hlist_empty ( net - > xfrm . state_bysrc ) ) ;
xfrm_hash_free ( net - > xfrm . state_bysrc , sz ) ;
2008-11-26 04:16:58 +03:00
WARN_ON ( ! hlist_empty ( net - > xfrm . state_bydst ) ) ;
xfrm_hash_free ( net - > xfrm . state_bydst , sz ) ;
2005-04-17 02:20:36 +04:00
}
2007-09-17 22:51:22 +04:00
# ifdef CONFIG_AUDITSYSCALL
2008-01-06 10:13:20 +03:00
static void xfrm_audit_helper_sainfo ( struct xfrm_state * x ,
struct audit_buffer * audit_buf )
2007-09-17 22:51:22 +04:00
{
2007-12-21 07:49:33 +03:00
struct xfrm_sec_ctx * ctx = x - > security ;
u32 spi = ntohl ( x - > id . spi ) ;
if ( ctx )
2007-09-17 22:51:22 +04:00
audit_log_format ( audit_buf , " sec_alg=%u sec_doi=%u sec_obj=%s " ,
2007-12-21 07:49:33 +03:00
ctx - > ctx_alg , ctx - > ctx_doi , ctx - > ctx_str ) ;
2007-09-17 22:51:22 +04:00
2013-12-24 05:43:46 +04:00
switch ( x - > props . family ) {
2007-09-17 22:51:22 +04:00
case AF_INET :
2008-10-31 10:54:56 +03:00
audit_log_format ( audit_buf , " src=%pI4 dst=%pI4 " ,
& x - > props . saddr . a4 , & x - > id . daddr . a4 ) ;
2007-09-17 22:51:22 +04:00
break ;
case AF_INET6 :
2008-10-29 22:52:50 +03:00
audit_log_format ( audit_buf , " src=%pI6 dst=%pI6 " ,
2008-10-29 02:10:17 +03:00
x - > props . saddr . a6 , x - > id . daddr . a6 ) ;
2007-09-17 22:51:22 +04:00
break ;
}
2007-12-21 07:49:33 +03:00
audit_log_format ( audit_buf , " spi=%u(0x%x) " , spi , spi ) ;
2007-09-17 22:51:22 +04:00
}
2008-01-06 10:13:20 +03:00
static void xfrm_audit_helper_pktinfo ( struct sk_buff * skb , u16 family ,
struct audit_buffer * audit_buf )
2007-12-22 01:58:11 +03:00
{
2011-04-22 08:53:02 +04:00
const struct iphdr * iph4 ;
const struct ipv6hdr * iph6 ;
2007-12-22 01:58:11 +03:00
switch ( family ) {
case AF_INET :
iph4 = ip_hdr ( skb ) ;
2008-10-31 10:54:56 +03:00
audit_log_format ( audit_buf , " src=%pI4 dst=%pI4 " ,
& iph4 - > saddr , & iph4 - > daddr ) ;
2007-12-22 01:58:11 +03:00
break ;
case AF_INET6 :
iph6 = ipv6_hdr ( skb ) ;
audit_log_format ( audit_buf ,
2008-10-29 22:52:50 +03:00
" src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x " ,
2013-12-24 05:43:46 +04:00
& iph6 - > saddr , & iph6 - > daddr ,
2007-12-22 01:58:11 +03:00
iph6 - > flow_lbl [ 0 ] & 0x0f ,
iph6 - > flow_lbl [ 1 ] ,
iph6 - > flow_lbl [ 2 ] ) ;
break ;
}
}
2014-04-22 16:48:30 +04:00
void xfrm_audit_state_add ( struct xfrm_state * x , int result , bool task_valid )
2007-09-17 22:51:22 +04:00
{
struct audit_buffer * audit_buf ;
2007-12-22 01:58:11 +03:00
audit_buf = xfrm_audit_start ( " SAD-add " ) ;
2007-09-17 22:51:22 +04:00
if ( audit_buf = = NULL )
return ;
2014-04-22 16:48:30 +04:00
xfrm_audit_helper_usrinfo ( task_valid , audit_buf ) ;
2007-12-22 01:58:11 +03:00
xfrm_audit_helper_sainfo ( x , audit_buf ) ;
audit_log_format ( audit_buf , " res=%u " , result ) ;
2007-09-17 22:51:22 +04:00
audit_log_end ( audit_buf ) ;
}
EXPORT_SYMBOL_GPL ( xfrm_audit_state_add ) ;
2014-04-22 16:48:30 +04:00
void xfrm_audit_state_delete ( struct xfrm_state * x , int result , bool task_valid )
2007-09-17 22:51:22 +04:00
{
struct audit_buffer * audit_buf ;
2007-12-22 01:58:11 +03:00
audit_buf = xfrm_audit_start ( " SAD-delete " ) ;
2007-09-17 22:51:22 +04:00
if ( audit_buf = = NULL )
return ;
2014-04-22 16:48:30 +04:00
xfrm_audit_helper_usrinfo ( task_valid , audit_buf ) ;
2007-12-22 01:58:11 +03:00
xfrm_audit_helper_sainfo ( x , audit_buf ) ;
audit_log_format ( audit_buf , " res=%u " , result ) ;
2007-09-17 22:51:22 +04:00
audit_log_end ( audit_buf ) ;
}
EXPORT_SYMBOL_GPL ( xfrm_audit_state_delete ) ;
2007-12-22 01:58:11 +03: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 ) ;
2011-03-08 03:08:32 +03:00
void xfrm_audit_state_replay ( struct xfrm_state * x ,
2007-12-22 01:58:11 +03:00
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 ) ;
}
2011-03-08 03:08:32 +03:00
EXPORT_SYMBOL_GPL ( xfrm_audit_state_replay ) ;
2007-12-22 01:58:11 +03:00
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-17 22:51:22 +04:00
# endif /* CONFIG_AUDITSYSCALL */