Skip to contentSkip to conversation

netns: simple cleanups

PATCH In review Guillaume Nault 2 participants 5 messages Jan 15, 2020 – Jan 16, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover netns: simple cleanups

A few things to cleanup, found while working on net_namespace.c:
* align netlink attribute parsing policy with their real usage,
* some constification.

v2: also update IFLA_NET_NS_* in rtnetlink.c.

Guillaume Nault (2):
netns: Parse *_PID and *_FD netlink attributes as signed integers
netns: constify exported functions

 include/net/net_namespace.h | 10 +++++-----
 net/core/net_namespace.c    | 18 +++++++++---------
 net/core/rtnetlink.c        |  8 ++++----
 3 files changed, 18 insertions(+), 18 deletions(-)
Signature · 1 line
2.21.1
#1 lore raw

PATCH v2 1/2 netns: Parse *_PID and *_FD netlink attributes as signed integers

These attributes represent signed values (file descriptors and PIDs). Make that clear in nla_policy.

---
 net/core/net_namespace.c | 12 ++++++------
 net/core/rtnetlink.c     |  8 ++++----
 2 files changed, 10 insertions(+), 10 deletions(-)
net/core/net_namespace.c +6 -6
index 6412c1fbfcb5..85c565571c1c 100644
@@ -706,8 +706,8 @@ static struct pernet_operations __net_initdata net_ns_ops = {
 static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
 	[NETNSA_NONE]		= { .type = NLA_UNSPEC },
 	[NETNSA_NSID]		= { .type = NLA_S32 },
-	[NETNSA_PID]		= { .type = NLA_U32 },
-	[NETNSA_FD]		= { .type = NLA_U32 },
+	[NETNSA_PID]		= { .type = NLA_S32 },
+	[NETNSA_FD]		= { .type = NLA_S32 },
 	[NETNSA_TARGET_NSID]	= { .type = NLA_S32 },
 };
 
@@ -731,10 +731,10 @@ static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
 	nsid = nla_get_s32(tb[NETNSA_NSID]);
 
 	if (tb[NETNSA_PID]) {
-		peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
+		peer = get_net_ns_by_pid(nla_get_s32(tb[NETNSA_PID]));
 		nla = tb[NETNSA_PID];
 	} else if (tb[NETNSA_FD]) {
-		peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
+		peer = get_net_ns_by_fd(nla_get_s32(tb[NETNSA_FD]));
 		nla = tb[NETNSA_FD];
 	} else {
 		NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
@@ -874,10 +874,10 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err < 0)
 		return err;
 	if (tb[NETNSA_PID]) {
-		peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
+		peer = get_net_ns_by_pid(nla_get_s32(tb[NETNSA_PID]));
 		nla = tb[NETNSA_PID];
 	} else if (tb[NETNSA_FD]) {
-		peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
+		peer = get_net_ns_by_fd(nla_get_s32(tb[NETNSA_FD]));
 		nla = tb[NETNSA_FD];
 	} else if (tb[NETNSA_NSID]) {
 		peer = get_net_ns_by_id(net, nla_get_s32(tb[NETNSA_NSID]));
net/core/rtnetlink.c +4 -4
index 20bc406f3871..9b5419a7bd74 100644
@@ -1794,8 +1794,8 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
-	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
-	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
+	[IFLA_NET_NS_PID]	= { .type = NLA_S32 },
+	[IFLA_NET_NS_FD]	= { .type = NLA_S32 },
 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
 	 * allow 0-length string (needed to remove an alias).
 	 */
@@ -2118,9 +2118,9 @@ struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
 	 * network namespace we are talking about.
 	 */
 	if (tb[IFLA_NET_NS_PID])
-		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
+		net = get_net_ns_by_pid(nla_get_s32(tb[IFLA_NET_NS_PID]));
 	else if (tb[IFLA_NET_NS_FD])
-		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
+		net = get_net_ns_by_fd(nla_get_s32(tb[IFLA_NET_NS_FD]));
 	else
 		net = get_net(src_net);
 	return net;
Signature · 1 line
2.21.1
#2 lore raw

Re: [PATCH net-next v2 1/2] netns: Parse *_PID and *_FD netlink attributes as signed integers

Le 15/01/2020 à 16:36, Guillaume Nault a écrit :

Quoted 2 lines

These attributes represent signed values (file descriptors and PIDs). Make that clear in nla_policy.

After more check, I also find these one:

$ git grep "NET.*_PID\|NET.*_FD" include/uapi/linux/
include/uapi/linux/devlink.h:   DEVLINK_ATTR_NETNS_FD,                  /* u32 */
include/uapi/linux/devlink.h:   DEVLINK_ATTR_NETNS_PID,                 /* u32 */
include/uapi/linux/gtp.h:       GTPA_NET_NS_FD,
include/uapi/linux/if_link.h:   IFLA_NET_NS_PID,
include/uapi/linux/if_link.h:   IFLA_NET_NS_FD,
include/uapi/linux/net_namespace.h:     NETNSA_PID,
include/uapi/linux/net_namespace.h:     NETNSA_FD,
include/uapi/linux/nl80211.h:   NL80211_ATTR_NETNS_FD,

Regards,
Nicolas

#3 lore raw

Re: [PATCH net-next v2 1/2] netns: Parse *_PID and *_FD netlink attributes as signed integers

Quoted 4 lines · Nicolas Dichtel

Le 15/01/2020 à 16:36, Guillaume Nault a écrit :

Quoted 2 lines

These attributes represent signed values (file descriptors and PIDs). Make that clear in nla_policy.

After more check, I also find these one:

Quoted 10 lines
$ git grep "NET.*_PID\|NET.*_FD" include/uapi/linux/
include/uapi/linux/devlink.h:   DEVLINK_ATTR_NETNS_FD,                  /* u32 */
include/uapi/linux/devlink.h:   DEVLINK_ATTR_NETNS_PID,                 /* u32 */
include/uapi/linux/gtp.h:       GTPA_NET_NS_FD,
include/uapi/linux/if_link.h:   IFLA_NET_NS_PID,
include/uapi/linux/if_link.h:   IFLA_NET_NS_FD,
include/uapi/linux/net_namespace.h:     NETNSA_PID,
include/uapi/linux/net_namespace.h:     NETNSA_FD,
include/uapi/linux/nl80211.h:   NL80211_ATTR_NETNS_FD,

Well, this is really going beyond the scope of the original patch set. But ok, let's drop this series. I'll repost the constification as stand alone patch and will audit the netns attribute parsing when I'll get more time.

#4 lore raw

PATCH v2 2/2 netns: constify exported functions

In reply to Guillaume Nault

Mark function parameters as 'const' where possible.

---
 include/net/net_namespace.h | 10 +++++-----
 net/core/net_namespace.c    |  6 +++---
 2 files changed, 8 insertions(+), 8 deletions(-)
include/net/net_namespace.h +5 -5
index b8ceaf0cd997..854d39ef1ca3 100644
@@ -347,9 +347,9 @@ static inline struct net *read_pnet(const possible_net_t *pnet)
 #endif
 
 int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp);
-int peernet2id(struct net *net, struct net *peer);
-bool peernet_has_id(struct net *net, struct net *peer);
-struct net *get_net_ns_by_id(struct net *net, int id);
+int peernet2id(const struct net *net, struct net *peer);
+bool peernet_has_id(const struct net *net, struct net *peer);
+struct net *get_net_ns_by_id(const struct net *net, int id);
 
 struct pernet_operations {
 	struct list_head list;
@@ -427,7 +427,7 @@ static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
 }
 #endif
 
-static inline int rt_genid_ipv4(struct net *net)
+static inline int rt_genid_ipv4(const struct net *net)
 {
 	return atomic_read(&net->ipv4.rt_genid);
 }
@@ -459,7 +459,7 @@ static inline void rt_genid_bump_all(struct net *net)
 	rt_genid_bump_ipv6(net);
 }
 
-static inline int fnhe_genid(struct net *net)
+static inline int fnhe_genid(const struct net *net)
 {
 	return atomic_read(&net->fnhe_genid);
 }
net/core/net_namespace.c +3 -3
index 85c565571c1c..fd0727670f34 100644
@@ -268,7 +268,7 @@ int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp)
 EXPORT_SYMBOL_GPL(peernet2id_alloc);
 
 /* This function returns, if assigned, the id of a peer netns. */
-int peernet2id(struct net *net, struct net *peer)
+int peernet2id(const struct net *net, struct net *peer)
 {
 	int id;
 
@@ -283,12 +283,12 @@ EXPORT_SYMBOL(peernet2id);
 /* This function returns true is the peer netns has an id assigned into the
  * current netns.
  */
-bool peernet_has_id(struct net *net, struct net *peer)
+bool peernet_has_id(const struct net *net, struct net *peer)
 {
 	return peernet2id(net, peer) >= 0;
 }
 
-struct net *get_net_ns_by_id(struct net *net, int id)
+struct net *get_net_ns_by_id(const struct net *net, int id)
 {
 	struct net *peer;
 
Signature · 1 line
2.21.1