[PATCH 1/1] fixup! veth: grab a reference for rtnl_link_veth_get_peer()

Thomas Haller thaller at redhat.com
Mon Apr 28 07:35:40 PDT 2014


---
Hi Cong,

I am pretty sure that you need to change rtnl_link_veth_add()
and veth_free() not to leak the peer (or am I wrong??).

(and a minor change in utils.h, that I got wrong in my previous
patch).

Thomas

 include/netlink/utils.h |  2 +-
 lib/route/link/veth.c   | 12 ++++++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/include/netlink/utils.h b/include/netlink/utils.h
index 6c4cb52..4c2aa19 100644
--- a/include/netlink/utils.h
+++ b/include/netlink/utils.h
@@ -90,11 +90,11 @@ enum {
 	NL_CAPABILITY_ROUTE_BUILD_MSG_SET_SCOPE         = 1,
 #define NL_CAPABILITY_ROUTE_BUILD_MSG_SET_SCOPE NL_CAPABILITY_ROUTE_BUILD_MSG_SET_SCOPE
 
-	NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE = 2,
 	/**
 	 * rtnl_link_veth_get_peer() now returns a reference that is owned by the
 	 * caller and must be released by the caller with rtnl_link_put().
 	 */
+	NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE = 2,
 #define NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE
 
 	__NL_CAPABILITY_MAX
diff --git a/lib/route/link/veth.c b/lib/route/link/veth.c
index 3b6a89c..117e1b9 100644
--- a/lib/route/link/veth.c
+++ b/lib/route/link/veth.c
@@ -37,6 +37,8 @@ static struct nla_policy veth_policy[VETH_INFO_MAX+1] = {
 	[VETH_INFO_PEER]	= { .minlen = sizeof(struct ifinfomsg) },
 };
 
+static struct rtnl_link *_veth_get_peer(struct rtnl_link *link);
+
 static int veth_parse(struct rtnl_link *link, struct nlattr *data,
 		      struct nlattr *xstats)
 {
@@ -167,7 +169,7 @@ static int veth_alloc(struct rtnl_link *link)
 
 static void veth_free(struct rtnl_link *link)
 {
-	struct rtnl_link *peer = rtnl_link_veth_get_peer(link);
+	struct rtnl_link *peer = _veth_get_peer(link);
 	if (peer) {
 		link->l_info = NULL;
 		/* avoid calling this recursively */
@@ -224,6 +226,12 @@ struct rtnl_link *rtnl_link_veth_alloc(void)
 	return link;
 }
 
+static struct rtnl_link *_veth_get_peer(struct rtnl_link *link)
+{
+	IS_VETH_LINK_ASSERT(link);
+	return link->l_info;
+}
+
 /**
  * Get the peer link of a veth link
  *
@@ -279,7 +287,7 @@ int rtnl_link_veth_add(struct nl_sock *sock, const char *name,
 
 	if (!(link = rtnl_link_veth_alloc()))
 		return -NLE_NOMEM;
-	peer = rtnl_link_veth_get_peer(link);
+	peer = _veth_get_peer(link);
 
 	if (name && peer_name) {
 		rtnl_link_set_name(link, name);
-- 
1.9.0




More information about the libnl mailing list