Skip to content

Commit 673c09b

Browse files
Alexey Dobriyandavem330
authored andcommitted
netns xfrm: add struct xfrm_state::xs_net
To avoid unnecessary complications with passing netns around. * set once, very early after allocating * once set, never changes For a while create every xfrm_state in init_net. Signed-off-by: Alexey Dobriyan <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d62ddc2 commit 673c09b

File tree

6 files changed

+19
-10
lines changed

6 files changed

+19
-10
lines changed

include/net/xfrm.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ struct xfrm_state_walk {
130130
/* Full description of state of transformer. */
131131
struct xfrm_state
132132
{
133+
#ifdef CONFIG_NET_NS
134+
struct net *xs_net;
135+
#endif
133136
union {
134137
struct hlist_node gclist;
135138
struct hlist_node bydst;
@@ -223,6 +226,11 @@ struct xfrm_state
223226
void *data;
224227
};
225228

229+
static inline struct net *xs_net(struct xfrm_state *x)
230+
{
231+
return read_pnet(&x->xs_net);
232+
}
233+
226234
/* xflags - make enum if more show up */
227235
#define XFRM_TIME_DEFER 1
228236

@@ -1296,7 +1304,7 @@ extern void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto);
12961304
extern int xfrm_state_walk(struct xfrm_state_walk *walk,
12971305
int (*func)(struct xfrm_state *, int, void*), void *);
12981306
extern void xfrm_state_walk_done(struct xfrm_state_walk *walk);
1299-
extern struct xfrm_state *xfrm_state_alloc(void);
1307+
extern struct xfrm_state *xfrm_state_alloc(struct net *net);
13001308
extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
13011309
struct flowi *fl, struct xfrm_tmpl *tmpl,
13021310
struct xfrm_policy *pol, int *err,

net/ipv4/ipcomp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
4949
{
5050
struct xfrm_state *t;
5151

52-
t = xfrm_state_alloc();
52+
t = xfrm_state_alloc(&init_net);
5353
if (t == NULL)
5454
goto out;
5555

net/ipv6/ipcomp6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
7676
{
7777
struct xfrm_state *t = NULL;
7878

79-
t = xfrm_state_alloc();
79+
t = xfrm_state_alloc(&init_net);
8080
if (!t)
8181
goto out;
8282

net/key/af_key.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
11221122
(key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
11231123
return ERR_PTR(-EINVAL);
11241124

1125-
x = xfrm_state_alloc();
1125+
x = xfrm_state_alloc(&init_net);
11261126
if (x == NULL)
11271127
return ERR_PTR(-ENOBUFS);
11281128

net/xfrm/xfrm_state.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,14 @@ static void xfrm_timer_handler(unsigned long data)
504504

505505
static void xfrm_replay_timer_handler(unsigned long data);
506506

507-
struct xfrm_state *xfrm_state_alloc(void)
507+
struct xfrm_state *xfrm_state_alloc(struct net *net)
508508
{
509509
struct xfrm_state *x;
510510

511511
x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
512512

513513
if (x) {
514+
write_pnet(&x->xs_net, net);
514515
atomic_set(&x->refcnt, 1);
515516
atomic_set(&x->tunnel_users, 0);
516517
INIT_LIST_HEAD(&x->km.all);
@@ -835,7 +836,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
835836
error = -EEXIST;
836837
goto out;
837838
}
838-
x = xfrm_state_alloc();
839+
x = xfrm_state_alloc(&init_net);
839840
if (x == NULL) {
840841
error = -ENOMEM;
841842
goto out;
@@ -1017,7 +1018,7 @@ static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 re
10171018
if (!create)
10181019
return NULL;
10191020

1020-
x = xfrm_state_alloc();
1021+
x = xfrm_state_alloc(&init_net);
10211022
if (likely(x)) {
10221023
switch (family) {
10231024
case AF_INET:
@@ -1125,7 +1126,7 @@ EXPORT_SYMBOL(xfrm_state_add);
11251126
static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
11261127
{
11271128
int err = -ENOMEM;
1128-
struct xfrm_state *x = xfrm_state_alloc();
1129+
struct xfrm_state *x = xfrm_state_alloc(&init_net);
11291130
if (!x)
11301131
goto error;
11311132

net/xfrm/xfrm_user.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
320320
struct nlattr **attrs,
321321
int *errp)
322322
{
323-
struct xfrm_state *x = xfrm_state_alloc();
323+
struct xfrm_state *x = xfrm_state_alloc(&init_net);
324324
int err = -ENOMEM;
325325

326326
if (!x)
@@ -1663,7 +1663,7 @@ static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
16631663
struct nlattr *rt = attrs[XFRMA_TMPL];
16641664

16651665
struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1666-
struct xfrm_state *x = xfrm_state_alloc();
1666+
struct xfrm_state *x = xfrm_state_alloc(&init_net);
16671667
int err = -ENOMEM;
16681668

16691669
if (!x)

0 commit comments

Comments
 (0)