[PATCH iproute2 v2] libgenl: make genl_add_mcast_grp set errno on error

Subsystems: library code, the rest

STALE1906d

2 messages, 2 authors, 2021-05-17 · open the first message on its own page

[PATCH iproute2 v2] libgenl: make genl_add_mcast_grp set errno on error

From: Florian Westphal <fw@strlen.de>
Date: 2021-05-10 12:41:14

genl_add_mcast_grp doesn't set errno in all cases.

On kernels that support mptcp but lack event support (all kernels <= 5.11)
MPTCP_PM_EV_GRP_NAME won't be found and ip will exit with

    "can't subscribe to mptcp events: Success"

Set errno to a meaningful value (ENOENT) when the group name isn't found
and also cover other spots where it returns nonzero with errno unset.

Fixes: ff619e4fd370 ("mptcp: add support for event monitoring")
Signed-off-by: Florian Westphal <fw@strlen.de>
---
 Change since v1: fix libgenl instead of setting errno in the caller.

 lib/libgenl.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/lib/libgenl.c b/lib/libgenl.c
index 4c51d47af46b..8b22c06e7941 100644
--- a/lib/libgenl.c
+++ b/lib/libgenl.c
@@ -84,6 +84,7 @@ static int genl_parse_grps(struct rtattr *attr, const char *name, unsigned int *
 		}
 	}
 
+	errno = ENOENT;
 	return -1;
 }
 
@@ -108,17 +109,22 @@ int genl_add_mcast_grp(struct rtnl_handle *grth, __u16 fnum, const char *group)
 	ghdr = NLMSG_DATA(answer);
 	len = answer->nlmsg_len;
 
-	if (answer->nlmsg_type != GENL_ID_CTRL)
+	if (answer->nlmsg_type != GENL_ID_CTRL) {
+		errno = EINVAL;
 		goto err_free;
+	}
 
 	len -= NLMSG_LENGTH(GENL_HDRLEN);
-	if (len < 0)
+	if (len < 0) {
+		errno = EINVAL;
 		goto err_free;
+	}
 
 	attrs = (struct rtattr *) ((char *) ghdr + GENL_HDRLEN);
 	parse_rtattr(tb, CTRL_ATTR_MAX, attrs, len);
 
 	if (tb[CTRL_ATTR_MCAST_GROUPS] == NULL) {
+		errno = ENOENT;
 		fprintf(stderr, "Missing mcast groups TLV\n");
 		goto err_free;
 	}
-- 
2.26.3

Re: [PATCH iproute2 v2] libgenl: make genl_add_mcast_grp set errno on error

From: David Ahern <hidden>
Date: 2021-05-17 02:38:20

On 5/10/21 6:06 AM, Florian Westphal wrote:
quoted hunk
diff --git a/lib/libgenl.c b/lib/libgenl.c
index 4c51d47af46b..8b22c06e7941 100644
--- a/lib/libgenl.c
+++ b/lib/libgenl.c
@@ -84,6 +84,7 @@ static int genl_parse_grps(struct rtattr *attr, const char *name, unsigned int *
 		}
 	}
 
+	errno = ENOENT;
 	return -1;
 }
 
@@ -108,17 +109,22 @@ int genl_add_mcast_grp(struct rtnl_handle *grth, __u16 fnum, const char *group)
 	ghdr = NLMSG_DATA(answer);
 	len = answer->nlmsg_len;
 
-	if (answer->nlmsg_type != GENL_ID_CTRL)
+	if (answer->nlmsg_type != GENL_ID_CTRL) {
+		errno = EINVAL;
 		goto err_free;
+	}
 
 	len -= NLMSG_LENGTH(GENL_HDRLEN);
-	if (len < 0)
+	if (len < 0) {
+		errno = EINVAL;
 		goto err_free;
+	}
 
 	attrs = (struct rtattr *) ((char *) ghdr + GENL_HDRLEN);
 	parse_rtattr(tb, CTRL_ATTR_MAX, attrs, len);
 
 	if (tb[CTRL_ATTR_MCAST_GROUPS] == NULL) {
+		errno = ENOENT;
 		fprintf(stderr, "Missing mcast groups TLV\n");
 		goto err_free;
 	}
I get compile errors on Ubuntu 20.04:


lib
    CC       libgenl.o
libgenl.c: In function ‘genl_parse_grps’:
libgenl.c:87:2: error: ‘errno’ undeclared (first use in this function)
   87 |  errno = ENOENT;
      |  ^~~~~
libgenl.c:12:1: note: ‘errno’ is defined in header ‘<errno.h>’; did you
forget to ‘#include <errno.h>’?
   11 | #include "libgenl.h"
  +++ |+#include <errno.h>
   12 |
libgenl.c:87:2: note: each undeclared identifier is reported only once
for each function it appears in
   87 |  errno = ENOENT;
      |  ^~~~~
libgenl.c:87:10: error: ‘ENOENT’ undeclared (first use in this function)
   87 |  errno = ENOENT;
      |          ^~~~~~
libgenl.c: In function ‘genl_add_mcast_grp’:
libgenl.c:113:3: error: ‘errno’ undeclared (first use in this function)
  113 |   errno = EINVAL;
      |   ^~~~~
libgenl.c:113:3: note: ‘errno’ is defined in header ‘<errno.h>’; did you
forget to ‘#include <errno.h>’?
libgenl.c:113:11: error: ‘EINVAL’ undeclared (first use in this function)
  113 |   errno = EINVAL;
      |           ^~~~~~
libgenl.c:127:11: error: ‘ENOENT’ undeclared (first use in this function)
  127 |   errno = ENOENT;
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help