Skip to contentSkip to conversation

rtnl: add missing APIs

PATCH In review Daniel Wagner 2 participants 5 messages Feb 14, 2020

lore
Share & tools
lore
#0 lore raw

cover rtnl: add missing APIs

The first two patches make the API for both protocol version complete. The last patch adds the table, priority and preference to the extract functions, which is a very uselful information.

This series is based on the cleanup series send before.

Daniel Wagner (3):
rtnl: Add l_rtnl_route6_dump() and l_rtnl_route6_extract()
unit: Add unit test for l_rtnl_route6_{dump|extract}
rtnl: Extract table, priority and pref

 ell/rtnl.c       | 130 ++++++++++++++++++++++++++++++++---------------
 ell/rtnl.h       |  10 +++-
 unit/test-rtnl.c |  42 +++++++++++++--
 3 files changed, 137 insertions(+), 45 deletions(-)
Signature · 1 line
2.25.0
#1 lore raw

PATCH 1/3 rtnl: Add l_rtnl_route6_dump() and l_rtnl_route6_extract()

Add missing function for handling IPv6. Now we for both protocol versions the same functionality.

While at it, factor out the l_rtnl_route4_extract to use inet_ntop which can handle both address families.

---
 ell/rtnl.c | 104 +++++++++++++++++++++++++++++++++--------------------
 ell/rtnl.h |   6 ++++
 2 files changed, 72 insertions(+), 38 deletions(-)
ell/rtnl.c +66 -38
index ced9baab66b9..c94531144eb4 100644
@@ -68,6 +68,50 @@ static size_t rta_add_data(void *rta_buf, unsigned short type, void *data,
 	return RTA_SPACE(data_len);
 }
 
+static void l_rtnl_route_extract(const struct rtmsg *rtmsg, uint32_t len,
+				int family, uint32_t *ifindex, char **dst,
+				char **gateway,	char **src)
+{
+	struct rtattr *attr;
+	char buf[INET6_ADDRSTRLEN];
+
+	for (attr = RTM_RTA(rtmsg); RTA_OK(attr, len);
+						attr = RTA_NEXT(attr, len)) {
+		switch (attr->rta_type) {
+		case RTA_DST:
+			if (!dst)
+				break;
+
+			inet_ntop(family, RTA_DATA(attr), buf, sizeof(buf));
+			*dst = l_strdup(buf);
+
+			break;
+		case RTA_GATEWAY:
+			if (!gateway)
+				break;
+
+			inet_ntop(family, RTA_DATA(attr), buf, sizeof(buf));
+			*gateway = l_strdup(buf);
+
+			break;
+		case RTA_PREFSRC:
+			if (!src)
+				break;
+
+			inet_ntop(family, RTA_DATA(attr), buf, sizeof(buf));
+			*src = l_strdup(buf);
+
+			break;
+		case RTA_OIF:
+			if (!ifindex)
+				break;
+
+			*ifindex = *((uint32_t *) RTA_DATA(attr));
+			break;
+		}
+	}
+}
+
 uint32_t l_rtnl_set_linkmode_and_operstate(struct l_netlink *rtnl, int ifindex,
 					uint8_t linkmode, uint8_t operstate,
 					l_netlink_command_func_t cb,
@@ -294,44 +338,7 @@ void l_rtnl_route4_extract(const struct rtmsg *rtmsg, uint32_t len,
 				uint32_t *ifindex, char **dst, char **gateway,
 				char **src)
 {
-	struct in_addr in_addr;
-	struct rtattr *attr;
-
-	for (attr = RTM_RTA(rtmsg); RTA_OK(attr, len);
-						attr = RTA_NEXT(attr, len)) {
-		switch (attr->rta_type) {
-		case RTA_DST:
-			if (!dst)
-				break;
-
-			in_addr = *((struct in_addr *) RTA_DATA(attr));
-			*dst = l_strdup(inet_ntoa(in_addr));
-
-			break;
-		case RTA_GATEWAY:
-			if (!gateway)
-				break;
-
-			in_addr = *((struct in_addr *) RTA_DATA(attr));
-			*gateway = l_strdup(inet_ntoa(in_addr));
-
-			break;
-		case RTA_PREFSRC:
-			if (!src)
-				break;
-
-			in_addr = *((struct in_addr *) RTA_DATA(attr));
-			*src = l_strdup(inet_ntoa(in_addr));
-
-			break;
-		case RTA_OIF:
-			if (!ifindex)
-				break;
-
-			*ifindex = *((uint32_t *) RTA_DATA(attr));
-			break;
-		}
-	}
+	l_rtnl_route_extract(rtmsg, len, AF_INET, ifindex, dst, gateway, src);
 }
 
 uint32_t l_rtnl_route4_dump(struct l_netlink *rtnl,
@@ -560,6 +567,27 @@ uint32_t l_rtnl_ifaddr6_delete(struct l_netlink *rtnl, int ifindex,
 						ip, cb, user_data, destroy);
 }
 
+void l_rtnl_route6_extract(const struct rtmsg *rtmsg, uint32_t len,
+				uint32_t *ifindex, char **dst, char **gateway,
+				char **src)
+{
+	l_rtnl_route_extract(rtmsg, len, AF_INET6, ifindex, dst, gateway, src);
+}
+
+uint32_t l_rtnl_route6_dump(struct l_netlink *rtnl,
+				l_netlink_command_func_t cb, void *user_data,
+				l_netlink_destroy_func_t destroy)
+{
+	struct rtmsg rtmsg;
+
+	memset(&rtmsg, 0, sizeof(struct rtmsg));
+	rtmsg.rtm_family = AF_INET6;
+
+	return l_netlink_send(rtnl, RTM_GETROUTE, NLM_F_DUMP, &rtmsg,
+					sizeof(struct rtmsg), cb, user_data,
+					destroy);
+}
+
 static uint32_t l_rtnl_route6_change(struct l_netlink *rtnl,
 					uint16_t nlmsg_type, int ifindex,
 					const char *gateway,
ell/rtnl.h +6 -0
index a6e6d1323015..2be2eaee99e2 100644
@@ -97,6 +97,12 @@ uint32_t l_rtnl_ifaddr6_delete(struct l_netlink *rtnl, int ifindex,
 					l_netlink_command_func_t cb,
 					void *user_data,
 					l_netlink_destroy_func_t destroy);
+void l_rtnl_route6_extract(const struct rtmsg *rtmsg, uint32_t len,
+				uint32_t *ifindex, char **dst, char **gateway,
+				char **src);
+uint32_t l_rtnl_route6_dump(struct l_netlink *rtnl,
+				l_netlink_command_func_t cb, void *user_data,
+				l_netlink_destroy_func_t destroy);
 uint32_t l_rtnl_route6_add_gateway(struct l_netlink *rtnl, int ifindex,
 					const char *gateway,
 					uint32_t priority_offset,
Signature · 1 line
2.25.0
#2 lore raw

PATCH 2/3 unit: Add unit test for l_rtnl_route6_{dump|extract}

In reply to Daniel Wagner
---
 unit/test-rtnl.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
unit/test-rtnl.c +32 -0
index c0659c4378da..37db73dc6f2f 100644
@@ -139,6 +139,37 @@ static void test_route4_dump(struct l_netlink *rtnl, void *user_data)
 					NULL, route4_dump_destroy_cb));
 }
 
+static void route6_dump_cb(int error,
+			uint16_t type, const void *data,
+			uint32_t len, void *user_data)
+{
+	const struct rtmsg *rtmsg = data;
+	char *dst = NULL, *gateway = NULL, *src = NULL;
+	uint32_t idx;
+
+	test_assert(!error);
+	test_assert(type == RTM_NEWROUTE);
+
+	l_rtnl_route6_extract(rtmsg, len, &idx, &dst, &gateway, &src);
+
+	l_info("idx %d dst %s gateway %s src %s", idx, dst, gateway, src);
+
+	l_free(dst);
+	l_free(gateway);
+	l_free(src);
+}
+
+static void route6_dump_destroy_cb(void *user_data)
+{
+	test_next();
+}
+
+static void test_route6_dump(struct l_netlink *rtnl, void *user_data)
+{
+	test_assert(l_rtnl_route6_dump(rtnl, route6_dump_cb,
+					NULL, route6_dump_destroy_cb));
+}
+
 static void ifaddr4_dump_cb(int error,
 				uint16_t type, const void *data,
 				uint32_t len, void *user_data)
@@ -211,6 +242,7 @@ int main(int argc, char *argv[])
 		return -1;
 
 	test_add("Dump IPv4 routing table", test_route4_dump, NULL);
+	test_add("Dump IPv6 routing table", test_route6_dump, NULL);
 	test_add("Dump IPv4 addresses", test_ifaddr4_dump, NULL);
 	test_add("Dump IPv6 addresses", test_ifaddr6_dump, NULL);
 
Signature · 1 line
2.25.0
#3 lore raw

PATCH 3/3 rtnl: Extract table, priority and pref

In reply to Daniel Wagner
---
 ell/rtnl.c       | 38 ++++++++++++++++++++++++++++++--------
 ell/rtnl.h       |  8 ++++----
 unit/test-rtnl.c | 16 ++++++++++------
 3 files changed, 44 insertions(+), 18 deletions(-)
ell/rtnl.c +30 -8
index c94531144eb4..4600a7e42001 100644
@@ -69,12 +69,14 @@ static size_t rta_add_data(void *rta_buf, unsigned short type, void *data,
 }
 
 static void l_rtnl_route_extract(const struct rtmsg *rtmsg, uint32_t len,
-				int family, uint32_t *ifindex, char **dst,
-				char **gateway,	char **src)
+				int family, uint32_t *table, uint32_t *ifindex,
+				uint32_t *priority, uint8_t *pref,
+				char **dst, char **gateway, char **src)
 {
 	struct rtattr *attr;
 	char buf[INET6_ADDRSTRLEN];
 
+	/* Not extracted at the moment: RTA_CACHEINFO for IPv6 */
 	for (attr = RTM_RTA(rtmsg); RTA_OK(attr, len);
 						attr = RTA_NEXT(attr, len)) {
 		switch (attr->rta_type) {
@@ -101,6 +103,24 @@ static void l_rtnl_route_extract(const struct rtmsg *rtmsg, uint32_t len,
 			inet_ntop(family, RTA_DATA(attr), buf, sizeof(buf));
 			*src = l_strdup(buf);
 
+			break;
+		case RTA_TABLE:
+			if (!table)
+				break;
+
+			*table = *((uint32_t *) RTA_DATA(attr));
+			break;
+		case RTA_PRIORITY:
+			if (!priority)
+				break;
+
+			*priority = *((uint32_t *) RTA_DATA(attr));
+			break;
+		case RTA_PREF:
+			if (!pref)
+				break;
+
+			*pref = *((uint8_t *) RTA_DATA(attr));
 			break;
 		case RTA_OIF:
 			if (!ifindex)
@@ -335,10 +355,11 @@ uint32_t l_rtnl_ifaddr4_delete(struct l_netlink *rtnl, int ifindex,
 }
 
 void l_rtnl_route4_extract(const struct rtmsg *rtmsg, uint32_t len,
-				uint32_t *ifindex, char **dst, char **gateway,
-				char **src)
+				uint32_t *table, uint32_t *ifindex,
+				char **dst, char **gateway, char **src)
 {
-	l_rtnl_route_extract(rtmsg, len, AF_INET, ifindex, dst, gateway, src);
+	l_rtnl_route_extract(rtmsg, len, AF_INET, table, ifindex,
+				NULL, NULL, dst, gateway, src);
 }
 
 uint32_t l_rtnl_route4_dump(struct l_netlink *rtnl,
@@ -568,10 +589,11 @@ uint32_t l_rtnl_ifaddr6_delete(struct l_netlink *rtnl, int ifindex,
 }
 
 void l_rtnl_route6_extract(const struct rtmsg *rtmsg, uint32_t len,
-				uint32_t *ifindex, char **dst, char **gateway,
-				char **src)
+				uint32_t *table, uint32_t *ifindex,
+				char **dst, char **gateway, char **src)
 {
-	l_rtnl_route_extract(rtmsg, len, AF_INET6, ifindex, dst, gateway, src);
+	l_rtnl_route_extract(rtmsg, len, AF_INET6, table, ifindex,
+				NULL, NULL, dst, gateway, src);
 }
 
 uint32_t l_rtnl_route6_dump(struct l_netlink *rtnl,
ell/rtnl.h +4 -4
index 2be2eaee99e2..ea283c1c5a6e 100644
@@ -63,8 +63,8 @@ uint32_t l_rtnl_ifaddr4_delete(struct l_netlink *rtnl, int ifindex,
 				l_netlink_destroy_func_t destroy);
 
 void l_rtnl_route4_extract(const struct rtmsg *rtmsg, uint32_t len,
-				uint32_t *ifindex, char **dst, char **gateway,
-				char **src);
+				uint32_t *table, uint32_t *ifindex,
+				char **dst, char **gateway, char **src);
 uint32_t l_rtnl_route4_dump(struct l_netlink *rtnl,
 				l_netlink_command_func_t cb, void *user_data,
 				l_netlink_destroy_func_t destroy);
@@ -98,8 +98,8 @@ uint32_t l_rtnl_ifaddr6_delete(struct l_netlink *rtnl, int ifindex,
 					void *user_data,
 					l_netlink_destroy_func_t destroy);
 void l_rtnl_route6_extract(const struct rtmsg *rtmsg, uint32_t len,
-				uint32_t *ifindex, char **dst, char **gateway,
-				char **src);
+				uint32_t *table, uint32_t *ifindex,
+				char **dst, char **gateway, char **src);
 uint32_t l_rtnl_route6_dump(struct l_netlink *rtnl,
 				l_netlink_command_func_t cb, void *user_data,
 				l_netlink_destroy_func_t destroy);
unit/test-rtnl.c +10 -6
index 37db73dc6f2f..107f69a76da1 100644
@@ -114,14 +114,16 @@ static void route4_dump_cb(int error,
 {
 	const struct rtmsg *rtmsg = data;
 	char *dst = NULL, *gateway = NULL, *src = NULL;
-	uint32_t idx;
+	uint32_t table, ifindex;
 
 	test_assert(!error);
 	test_assert(type == RTM_NEWROUTE);
 
-	l_rtnl_route4_extract(rtmsg, len, &idx, &dst, &gateway, &src);
+	l_rtnl_route4_extract(rtmsg, len, &table, &ifindex,
+				&dst, &gateway, &src);
 
-	l_info("idx %d dst %s gateway %s src %s", idx, dst, gateway, src);
+	l_info("table %d ifindex %d dst %s gateway %s src %s",
+		table, ifindex, dst, gateway, src);
 
 	l_free(dst);
 	l_free(gateway);
@@ -145,14 +147,16 @@ static void route6_dump_cb(int error,
 {
 	const struct rtmsg *rtmsg = data;
 	char *dst = NULL, *gateway = NULL, *src = NULL;
-	uint32_t idx;
+	uint32_t table, ifindex;
 
 	test_assert(!error);
 	test_assert(type == RTM_NEWROUTE);
 
-	l_rtnl_route6_extract(rtmsg, len, &idx, &dst, &gateway, &src);
+	l_rtnl_route6_extract(rtmsg, len, &table, &ifindex,
+				&dst, &gateway, &src);
 
-	l_info("idx %d dst %s gateway %s src %s", idx, dst, gateway, src);
+	l_info("table %d ifindex %d dst %s gateway %s src %s",
+		table, ifindex, dst, gateway, src);
 
 	l_free(dst);
 	l_free(gateway);
Signature · 1 line
2.25.0
#4 lore raw
In reply to Daniel Wagner

Hi Daniel,

Quoted 11 lines · Daniel Wagner

The first two patches make the API for both protocol version complete. The last patch adds the table, priority and preference to the extract functions, which is a very uselful information.

This series is based on the cleanup series send before.

Daniel Wagner (3):
rtnl: Add l_rtnl_route6_dump() and l_rtnl_route6_extract()
unit: Add unit test for l_rtnl_route6_{dump|extract}
rtnl: Extract table, priority and pref

Quoted 5 lines
  ell/rtnl.c       | 130 ++++++++++++++++++++++++++++++++---------------
  ell/rtnl.h       |  10 +++-
  unit/test-rtnl.c |  42 +++++++++++++--
  3 files changed, 137 insertions(+), 45 deletions(-)

All applied. I did split the last patch into two.

Regards,
-Denis