Commit 288f06a0 authored by Nicolas Dichtel's avatar Nicolas Dichtel Committed by David S. Miller

netns: enable to dump full nsid translation table

Like the previous patch, the goal is to ease to convert nsids from one
netns to another netns.
A new attribute (NETNSA_CURRENT_NSID) is added to the kernel answer when
NETNSA_TARGET_NSID is provided, thus the user can easily convert nsids.
Signed-off-by: default avatarNicolas Dichtel <nicolas.dichtel@6wind.com>
Reviewed-by: default avatarDavid Ahern <dsahern@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 3a4f68bf
...@@ -17,6 +17,7 @@ enum { ...@@ -17,6 +17,7 @@ enum {
NETNSA_PID, NETNSA_PID,
NETNSA_FD, NETNSA_FD,
NETNSA_TARGET_NSID, NETNSA_TARGET_NSID,
NETNSA_CURRENT_NSID,
__NETNSA_MAX, __NETNSA_MAX,
}; };
......
...@@ -736,6 +736,7 @@ static int rtnl_net_get_size(void) ...@@ -736,6 +736,7 @@ static int rtnl_net_get_size(void)
{ {
return NLMSG_ALIGN(sizeof(struct rtgenmsg)) return NLMSG_ALIGN(sizeof(struct rtgenmsg))
+ nla_total_size(sizeof(s32)) /* NETNSA_NSID */ + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
+ nla_total_size(sizeof(s32)) /* NETNSA_CURRENT_NSID */
; ;
} }
...@@ -745,6 +746,8 @@ struct net_fill_args { ...@@ -745,6 +746,8 @@ struct net_fill_args {
int flags; int flags;
int cmd; int cmd;
int nsid; int nsid;
bool add_ref;
int ref_nsid;
}; };
static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args) static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args)
...@@ -763,6 +766,10 @@ static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args) ...@@ -763,6 +766,10 @@ static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args)
if (nla_put_s32(skb, NETNSA_NSID, args->nsid)) if (nla_put_s32(skb, NETNSA_NSID, args->nsid))
goto nla_put_failure; goto nla_put_failure;
if (args->add_ref &&
nla_put_s32(skb, NETNSA_CURRENT_NSID, args->ref_nsid))
goto nla_put_failure;
nlmsg_end(skb, nlh); nlmsg_end(skb, nlh);
return 0; return 0;
...@@ -782,7 +789,6 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -782,7 +789,6 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
.cmd = RTM_NEWNSID, .cmd = RTM_NEWNSID,
}; };
struct net *peer, *target = net; struct net *peer, *target = net;
bool put_target = false;
struct nlattr *nla; struct nlattr *nla;
struct sk_buff *msg; struct sk_buff *msg;
int err; int err;
...@@ -824,7 +830,8 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -824,7 +830,8 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
err = PTR_ERR(target); err = PTR_ERR(target);
goto out; goto out;
} }
put_target = true; fillargs.add_ref = true;
fillargs.ref_nsid = peernet2id(net, peer);
} }
msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL); msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
...@@ -844,7 +851,7 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -844,7 +851,7 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
err_out: err_out:
nlmsg_free(msg); nlmsg_free(msg);
out: out:
if (put_target) if (fillargs.add_ref)
put_net(target); put_net(target);
put_net(peer); put_net(peer);
return err; return err;
...@@ -852,11 +859,11 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -852,11 +859,11 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
struct rtnl_net_dump_cb { struct rtnl_net_dump_cb {
struct net *tgt_net; struct net *tgt_net;
struct net *ref_net;
struct sk_buff *skb; struct sk_buff *skb;
struct net_fill_args fillargs; struct net_fill_args fillargs;
int idx; int idx;
int s_idx; int s_idx;
bool put_tgt_net;
}; };
static int rtnl_net_dumpid_one(int id, void *peer, void *data) static int rtnl_net_dumpid_one(int id, void *peer, void *data)
...@@ -868,6 +875,8 @@ static int rtnl_net_dumpid_one(int id, void *peer, void *data) ...@@ -868,6 +875,8 @@ static int rtnl_net_dumpid_one(int id, void *peer, void *data)
goto cont; goto cont;
net_cb->fillargs.nsid = id; net_cb->fillargs.nsid = id;
if (net_cb->fillargs.add_ref)
net_cb->fillargs.ref_nsid = __peernet2id(net_cb->ref_net, peer);
ret = rtnl_net_fill(net_cb->skb, &net_cb->fillargs); ret = rtnl_net_fill(net_cb->skb, &net_cb->fillargs);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -904,8 +913,9 @@ static int rtnl_valid_dump_net_req(const struct nlmsghdr *nlh, struct sock *sk, ...@@ -904,8 +913,9 @@ static int rtnl_valid_dump_net_req(const struct nlmsghdr *nlh, struct sock *sk,
"Invalid target network namespace id"); "Invalid target network namespace id");
return PTR_ERR(net); return PTR_ERR(net);
} }
net_cb->fillargs.add_ref = true;
net_cb->ref_net = net_cb->tgt_net;
net_cb->tgt_net = net; net_cb->tgt_net = net;
net_cb->put_tgt_net = true;
} else { } else {
NL_SET_BAD_ATTR(extack, tb[i]); NL_SET_BAD_ATTR(extack, tb[i]);
NL_SET_ERR_MSG(extack, NL_SET_ERR_MSG(extack,
...@@ -940,12 +950,22 @@ static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb) ...@@ -940,12 +950,22 @@ static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
} }
spin_lock_bh(&net_cb.tgt_net->nsid_lock); spin_lock_bh(&net_cb.tgt_net->nsid_lock);
if (net_cb.fillargs.add_ref &&
!net_eq(net_cb.ref_net, net_cb.tgt_net) &&
!spin_trylock_bh(&net_cb.ref_net->nsid_lock)) {
spin_unlock_bh(&net_cb.tgt_net->nsid_lock);
err = -EAGAIN;
goto end;
}
idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb); idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb);
if (net_cb.fillargs.add_ref &&
!net_eq(net_cb.ref_net, net_cb.tgt_net))
spin_unlock_bh(&net_cb.ref_net->nsid_lock);
spin_unlock_bh(&net_cb.tgt_net->nsid_lock); spin_unlock_bh(&net_cb.tgt_net->nsid_lock);
cb->args[0] = net_cb.idx; cb->args[0] = net_cb.idx;
end: end:
if (net_cb.put_tgt_net) if (net_cb.fillargs.add_ref)
put_net(net_cb.tgt_net); put_net(net_cb.tgt_net);
return err < 0 ? err : skb->len; return err < 0 ? err : skb->len;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment