Skip to content

Commit c9e7c76

Browse files
0x7f454c46klassert
authored andcommitted
xfrm: Provide API to register translator module
Add a skeleton for xfrm_compat module and provide API to register it in xfrm_state.ko. struct xfrm_translator will have function pointers to translate messages received from 32-bit userspace or to be sent to it from 64-bit kernel. module_get()/module_put() are used instead of rcu_read_lock() as the module will vmalloc() memory for translation. The new API is registered with xfrm_state module, not with xfrm_user as the former needs translator for user_policy set by setsockopt() and xfrm_user already uses functions from xfrm_state. Signed-off-by: Dmitry Safonov <dima@arista.com> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent 02a20d4 commit c9e7c76

5 files changed

Lines changed: 119 additions & 0 deletions

File tree

‎include/net/xfrm.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,25 @@ static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
20002000
return 0;
20012001
}
20022002

2003+
struct xfrm_translator {
2004+
struct module *owner;
2005+
};
2006+
2007+
#if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2008+
extern int xfrm_register_translator(struct xfrm_translator *xtr);
2009+
extern int xfrm_unregister_translator(struct xfrm_translator *xtr);
2010+
extern struct xfrm_translator *xfrm_get_translator(void);
2011+
extern void xfrm_put_translator(struct xfrm_translator *xtr);
2012+
#else
2013+
static inline struct xfrm_translator *xfrm_get_translator(void)
2014+
{
2015+
return NULL;
2016+
}
2017+
static inline void xfrm_put_translator(struct xfrm_translator *xtr)
2018+
{
2019+
}
2020+
#endif
2021+
20032022
#if IS_ENABLED(CONFIG_IPV6)
20042023
static inline bool xfrm6_local_dontfrag(const struct sock *sk)
20052024
{

‎net/xfrm/Kconfig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ config XFRM_USER
2828

2929
If unsure, say Y.
3030

31+
config XFRM_USER_COMPAT
32+
tristate "Compatible ABI support"
33+
depends on XFRM_USER && COMPAT_FOR_U64_ALIGNMENT
34+
select WANT_COMPAT_NETLINK_MESSAGES
35+
help
36+
Transformation(XFRM) user configuration interface like IPsec
37+
used by compatible Linux applications.
38+
39+
If unsure, say N.
40+
3141
config XFRM_INTERFACE
3242
tristate "Transformation virtual interface"
3343
depends on XFRM && IPV6

‎net/xfrm/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ obj-$(CONFIG_XFRM) := xfrm_policy.o xfrm_state.o xfrm_hash.o \
99
obj-$(CONFIG_XFRM_STATISTICS) += xfrm_proc.o
1010
obj-$(CONFIG_XFRM_ALGO) += xfrm_algo.o
1111
obj-$(CONFIG_XFRM_USER) += xfrm_user.o
12+
obj-$(CONFIG_XFRM_USER_COMPAT) += xfrm_compat.o
1213
obj-$(CONFIG_XFRM_IPCOMP) += xfrm_ipcomp.o
1314
obj-$(CONFIG_XFRM_INTERFACE) += xfrm_interface.o
1415
obj-$(CONFIG_XFRM_ESPINTCP) += espintcp.o

‎net/xfrm/xfrm_compat.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* XFRM compat layer
4+
* Author: Dmitry Safonov <dima@arista.com>
5+
* Based on code and translator idea by: Florian Westphal <fw@strlen.de>
6+
*/
7+
#include <linux/compat.h>
8+
#include <linux/xfrm.h>
9+
#include <net/xfrm.h>
10+
11+
static struct xfrm_translator xfrm_translator = {
12+
.owner = THIS_MODULE,
13+
};
14+
15+
static int __init xfrm_compat_init(void)
16+
{
17+
return xfrm_register_translator(&xfrm_translator);
18+
}
19+
20+
static void __exit xfrm_compat_exit(void)
21+
{
22+
xfrm_unregister_translator(&xfrm_translator);
23+
}
24+
25+
module_init(xfrm_compat_init);
26+
module_exit(xfrm_compat_exit);
27+
MODULE_LICENSE("GPL");
28+
MODULE_AUTHOR("Dmitry Safonov");
29+
MODULE_DESCRIPTION("XFRM 32-bit compatibility layer");

‎net/xfrm/xfrm_state.c‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,66 @@ static bool km_is_alive(const struct km_event *c)
22642264
return is_alive;
22652265
}
22662266

2267+
#if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2268+
static DEFINE_SPINLOCK(xfrm_translator_lock);
2269+
static struct xfrm_translator __rcu *xfrm_translator;
2270+
2271+
struct xfrm_translator *xfrm_get_translator(void)
2272+
{
2273+
struct xfrm_translator *xtr;
2274+
2275+
rcu_read_lock();
2276+
xtr = rcu_dereference(xfrm_translator);
2277+
if (unlikely(!xtr))
2278+
goto out;
2279+
if (!try_module_get(xtr->owner))
2280+
xtr = NULL;
2281+
out:
2282+
rcu_read_unlock();
2283+
return xtr;
2284+
}
2285+
EXPORT_SYMBOL_GPL(xfrm_get_translator);
2286+
2287+
void xfrm_put_translator(struct xfrm_translator *xtr)
2288+
{
2289+
module_put(xtr->owner);
2290+
}
2291+
EXPORT_SYMBOL_GPL(xfrm_put_translator);
2292+
2293+
int xfrm_register_translator(struct xfrm_translator *xtr)
2294+
{
2295+
int err = 0;
2296+
2297+
spin_lock_bh(&xfrm_translator_lock);
2298+
if (unlikely(xfrm_translator != NULL))
2299+
err = -EEXIST;
2300+
else
2301+
rcu_assign_pointer(xfrm_translator, xtr);
2302+
spin_unlock_bh(&xfrm_translator_lock);
2303+
2304+
return err;
2305+
}
2306+
EXPORT_SYMBOL_GPL(xfrm_register_translator);
2307+
2308+
int xfrm_unregister_translator(struct xfrm_translator *xtr)
2309+
{
2310+
int err = 0;
2311+
2312+
spin_lock_bh(&xfrm_translator_lock);
2313+
if (likely(xfrm_translator != NULL)) {
2314+
if (rcu_access_pointer(xfrm_translator) != xtr)
2315+
err = -EINVAL;
2316+
else
2317+
RCU_INIT_POINTER(xfrm_translator, NULL);
2318+
}
2319+
spin_unlock_bh(&xfrm_translator_lock);
2320+
synchronize_rcu();
2321+
2322+
return err;
2323+
}
2324+
EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2325+
#endif
2326+
22672327
int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
22682328
{
22692329
int err;

0 commit comments

Comments
 (0)