Skip to content

Commit 9a96345

Browse files
NicolasDichteldavem330
authored andcommitted
netns: notify netns id events
With this patch, netns ids that are created and deleted are advertised into the group RTNLGRP_NSID. Because callers of rtnl_net_notifyid() already know the id of the peer, there is no need to call __peernet2id() in rtnl_net_fill(). Signed-off-by: Nicolas Dichtel <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b111e4e commit 9a96345

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

include/uapi/linux/rtnetlink.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ enum {
134134

135135
RTM_NEWNSID = 88,
136136
#define RTM_NEWNSID RTM_NEWNSID
137+
RTM_DELNSID = 89,
138+
#define RTM_DELNSID RTM_DELNSID
137139
RTM_GETNSID = 90,
138140
#define RTM_GETNSID RTM_GETNSID
139141

@@ -635,6 +637,8 @@ enum rtnetlink_groups {
635637
#define RTNLGRP_MDB RTNLGRP_MDB
636638
RTNLGRP_MPLS_ROUTE,
637639
#define RTNLGRP_MPLS_ROUTE RTNLGRP_MPLS_ROUTE
640+
RTNLGRP_NSID,
641+
#define RTNLGRP_NSID RTNLGRP_NSID
638642
__RTNLGRP_MAX
639643
};
640644
#define RTNLGRP_MAX (__RTNLGRP_MAX - 1)

net/core/net_namespace.c

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ static void ops_free_list(const struct pernet_operations *ops,
148148
}
149149
}
150150

151+
static void rtnl_net_notifyid(struct net *net, struct net *peer, int cmd,
152+
int id);
151153
static int alloc_netid(struct net *net, struct net *peer, int reqid)
152154
{
153-
int min = 0, max = 0;
155+
int min = 0, max = 0, id;
154156

155157
ASSERT_RTNL();
156158

@@ -159,7 +161,11 @@ static int alloc_netid(struct net *net, struct net *peer, int reqid)
159161
max = reqid + 1;
160162
}
161163

162-
return idr_alloc(&net->netns_ids, peer, min, max, GFP_KERNEL);
164+
id = idr_alloc(&net->netns_ids, peer, min, max, GFP_KERNEL);
165+
if (id >= 0)
166+
rtnl_net_notifyid(net, peer, RTM_NEWNSID, id);
167+
168+
return id;
163169
}
164170

165171
/* This function is used by idr_for_each(). If net is equal to peer, the
@@ -359,8 +365,10 @@ static void cleanup_net(struct work_struct *work)
359365
for_each_net(tmp) {
360366
int id = __peernet2id(tmp, net, false);
361367

362-
if (id >= 0)
368+
if (id >= 0) {
369+
rtnl_net_notifyid(tmp, net, RTM_DELNSID, id);
363370
idr_remove(&tmp->netns_ids, id);
371+
}
364372
}
365373
idr_destroy(&net->netns_ids);
366374

@@ -531,7 +539,8 @@ static int rtnl_net_get_size(void)
531539
}
532540

533541
static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
534-
int cmd, struct net *net, struct net *peer)
542+
int cmd, struct net *net, struct net *peer,
543+
int nsid)
535544
{
536545
struct nlmsghdr *nlh;
537546
struct rtgenmsg *rth;
@@ -546,9 +555,13 @@ static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
546555
rth = nlmsg_data(nlh);
547556
rth->rtgen_family = AF_UNSPEC;
548557

549-
id = __peernet2id(net, peer, false);
550-
if (id < 0)
551-
id = NETNSA_NSID_NOT_ASSIGNED;
558+
if (nsid >= 0) {
559+
id = nsid;
560+
} else {
561+
id = __peernet2id(net, peer, false);
562+
if (id < 0)
563+
id = NETNSA_NSID_NOT_ASSIGNED;
564+
}
552565
if (nla_put_s32(skb, NETNSA_NSID, id))
553566
goto nla_put_failure;
554567

@@ -589,7 +602,7 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh)
589602
}
590603

591604
err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
592-
RTM_GETNSID, net, peer);
605+
RTM_GETNSID, net, peer, -1);
593606
if (err < 0)
594607
goto err_out;
595608

@@ -603,6 +616,29 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh)
603616
return err;
604617
}
605618

619+
static void rtnl_net_notifyid(struct net *net, struct net *peer, int cmd,
620+
int id)
621+
{
622+
struct sk_buff *msg;
623+
int err = -ENOMEM;
624+
625+
msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
626+
if (!msg)
627+
goto out;
628+
629+
err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, peer, id);
630+
if (err < 0)
631+
goto err_out;
632+
633+
rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
634+
return;
635+
636+
err_out:
637+
nlmsg_free(msg);
638+
out:
639+
rtnl_set_sk_err(net, RTNLGRP_NSID, err);
640+
}
641+
606642
static int __init net_ns_init(void)
607643
{
608644
struct net_generic *ng;

0 commit comments

Comments
 (0)