Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Reviewed-by: David Ahern <dsahern@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Nicolas Dichtel and committed by
David S. Miller
288f06a0 3a4f68bf

+27 -6
+1
include/uapi/linux/net_namespace.h
··· 17 17 NETNSA_PID, 18 18 NETNSA_FD, 19 19 NETNSA_TARGET_NSID, 20 + NETNSA_CURRENT_NSID, 20 21 __NETNSA_MAX, 21 22 }; 22 23
+26 -6
net/core/net_namespace.c
··· 736 736 { 737 737 return NLMSG_ALIGN(sizeof(struct rtgenmsg)) 738 738 + nla_total_size(sizeof(s32)) /* NETNSA_NSID */ 739 + + nla_total_size(sizeof(s32)) /* NETNSA_CURRENT_NSID */ 739 740 ; 740 741 } 741 742 ··· 746 745 int flags; 747 746 int cmd; 748 747 int nsid; 748 + bool add_ref; 749 + int ref_nsid; 749 750 }; 750 751 751 752 static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args) ··· 764 761 rth->rtgen_family = AF_UNSPEC; 765 762 766 763 if (nla_put_s32(skb, NETNSA_NSID, args->nsid)) 764 + goto nla_put_failure; 765 + 766 + if (args->add_ref && 767 + nla_put_s32(skb, NETNSA_CURRENT_NSID, args->ref_nsid)) 767 768 goto nla_put_failure; 768 769 769 770 nlmsg_end(skb, nlh); ··· 789 782 .cmd = RTM_NEWNSID, 790 783 }; 791 784 struct net *peer, *target = net; 792 - bool put_target = false; 793 785 struct nlattr *nla; 794 786 struct sk_buff *msg; 795 787 int err; ··· 830 824 err = PTR_ERR(target); 831 825 goto out; 832 826 } 833 - put_target = true; 827 + fillargs.add_ref = true; 828 + fillargs.ref_nsid = peernet2id(net, peer); 834 829 } 835 830 836 831 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL); ··· 851 844 err_out: 852 845 nlmsg_free(msg); 853 846 out: 854 - if (put_target) 847 + if (fillargs.add_ref) 855 848 put_net(target); 856 849 put_net(peer); 857 850 return err; ··· 859 852 860 853 struct rtnl_net_dump_cb { 861 854 struct net *tgt_net; 855 + struct net *ref_net; 862 856 struct sk_buff *skb; 863 857 struct net_fill_args fillargs; 864 858 int idx; 865 859 int s_idx; 866 - bool put_tgt_net; 867 860 }; 868 861 869 862 static int rtnl_net_dumpid_one(int id, void *peer, void *data) ··· 875 868 goto cont; 876 869 877 870 net_cb->fillargs.nsid = id; 871 + if (net_cb->fillargs.add_ref) 872 + net_cb->fillargs.ref_nsid = __peernet2id(net_cb->ref_net, peer); 878 873 ret = rtnl_net_fill(net_cb->skb, &net_cb->fillargs); 879 874 if (ret < 0) 880 875 return ret; ··· 913 904 "Invalid target network namespace id"); 914 905 return PTR_ERR(net); 915 906 } 907 + net_cb->fillargs.add_ref = true; 908 + net_cb->ref_net = net_cb->tgt_net; 916 909 net_cb->tgt_net = net; 917 - net_cb->put_tgt_net = true; 918 910 } else { 919 911 NL_SET_BAD_ATTR(extack, tb[i]); 920 912 NL_SET_ERR_MSG(extack, ··· 950 940 } 951 941 952 942 spin_lock_bh(&net_cb.tgt_net->nsid_lock); 943 + if (net_cb.fillargs.add_ref && 944 + !net_eq(net_cb.ref_net, net_cb.tgt_net) && 945 + !spin_trylock_bh(&net_cb.ref_net->nsid_lock)) { 946 + spin_unlock_bh(&net_cb.tgt_net->nsid_lock); 947 + err = -EAGAIN; 948 + goto end; 949 + } 953 950 idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb); 951 + if (net_cb.fillargs.add_ref && 952 + !net_eq(net_cb.ref_net, net_cb.tgt_net)) 953 + spin_unlock_bh(&net_cb.ref_net->nsid_lock); 954 954 spin_unlock_bh(&net_cb.tgt_net->nsid_lock); 955 955 956 956 cb->args[0] = net_cb.idx; 957 957 end: 958 - if (net_cb.put_tgt_net) 958 + if (net_cb.fillargs.add_ref) 959 959 put_net(net_cb.tgt_net); 960 960 return err < 0 ? err : skb->len; 961 961 }