Skip to contentSkip to conversation

rtnl: Add neighbor discovery utilities

PATCH In review Andrew Zaborowski 2 participants 7 messages Aug 25, 2021 – Aug 26, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtnl: Add neighbor discovery utilities

Add a few variants of RTNL utilities to look up addresses in the kernel's neighbour table entries and to add new entries. This is basically the ARP (for IPv4) and NDP (for IPv6) tables, that map IP addresses to hardware addresses.
---
There are three variants of each method, I currently only have a use case for the ipv4 and ipv6 variants so I'm happy dropping the struct l_rtnl_address variant.

 ell/ell.sym  |   6 ++
 ell/rtnl.c   | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/rtnl.h   |  36 ++++++++++
 ell/useful.h |   4 ++
 4 files changed, 240 insertions(+)
ell/ell.sym +6 -0
index b29d98d..8760f2b 100644
@@ -663,6 +663,12 @@ global:
 	l_rtnl_ifaddr_extract;
 	l_rtnl_ifaddr_add;
 	l_rtnl_ifaddr_delete;
+	l_rtnl_neighbour_get_ipv4_hwaddr;
+	l_rtnl_neighbour_get_ipv6_hwaddr;
+	l_rtnl_neighbour_get_addr_hwaddr;
+	l_rtnl_neighbour_set_ipv4_hwaddr;
+	l_rtnl_neighbour_set_ipv6_hwaddr;
+	l_rtnl_neighbour_set_addr_hwaddr;
 /* icmp6 */
 	l_icmp6_client_new;
 	l_icmp6_client_free;
ell/rtnl.c +194 -0

209 lines not on this page · raw message

ell/rtnl.h +36 -0
index 9ea24f5..834cd14 100644
@@ -33,6 +33,9 @@ extern "C" {
 struct l_rtnl_address;
 struct l_rtnl_route;
 
+typedef void (*l_rtnl_neighbour_get_cb_t) (int error, const uint8_t *hwaddr,
+						void *user_data);
+
 struct l_rtnl_address *l_rtnl_address_new(const char *ip, uint8_t prefix_len);
 struct l_rtnl_address *l_rtnl_address_clone(const struct l_rtnl_address *orig);
 void l_rtnl_address_free(struct l_rtnl_address *addr);
@@ -191,6 +194,39 @@ uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex,
 					void *user_data,
 					l_netlink_destroy_func_t destroy);
 
+uint32_t l_rtnl_neighbour_get_ipv4_hwaddr(struct l_netlink *rtnl, int ifindex,
+					uint32_t addr,
+					l_rtnl_neighbour_get_cb_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_get_ipv6_hwaddr(struct l_netlink *rtnl, int ifindex,
+					const uint8_t *addr,
+					l_rtnl_neighbour_get_cb_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_get_addr_hwaddr(struct l_netlink *rtnl, int ifindex,
+					const struct l_rtnl_address *addr,
+					l_rtnl_neighbour_get_cb_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_ipv4_hwaddr(struct l_netlink *rtnl, int ifindex,
+					uint32_t addr, const uint8_t *hwaddr,
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_ipv6_hwaddr(struct l_netlink *rtnl, int ifindex,
+					const uint8_t *addr,
+					const uint8_t *hwaddr,
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_addr_hwaddr(struct l_netlink *rtnl, int ifindex,
+					const struct l_rtnl_address *addr,
+					const uint8_t *hwaddr,
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+
 #ifdef __cplusplus
 }
 #endif
ell/useful.h +4 -0
index d696bc3..b4783ce 100644
@@ -83,3 +83,7 @@ static inline int secure_select(int select_left, int l, int r)
 
 	return r ^ ((l ^ r) & mask);
 }
+
+#define struct_alloc(structname, ...) \
+	(struct structname *) l_memdup(&(struct structname) { __VA_ARGS__ }, \
+					sizeof(struct structname))
Signature · 1 line
2.30.2
#1 lore raw

Hi Andrew,

Quoted 15 lines · Andrew Zaborowski

Add a few variants of RTNL utilities to look up addresses in the kernel's neighbour table entries and to add new entries. This is basically the ARP (for IPv4) and NDP (for IPv6) tables, that map IP addresses to hardware addresses.
---
There are three variants of each method, I currently only have a use case for the ipv4 and ipv6 variants so I'm happy dropping the struct l_rtnl_address variant.

  ell/ell.sym  |   6 ++
  ell/rtnl.c   | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++
  ell/rtnl.h   |  36 ++++++++++
  ell/useful.h |   4 ++
  4 files changed, 240 insertions(+)

<snip>

ell/rtnl.h extern "C" { · line 36

Quoted 9 lines
ell/rtnl.h +1 -0
index 9ea24f5..834cd14 100644
@@ -33,6 +33,9 @@ extern "C" {
  struct l_rtnl_address;
  struct l_rtnl_route;
 
+typedef void (*l_rtnl_neighbour_get_cb_t) (int error, const uint8_t *hwaddr,

You probably want a length as well, no?

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 198

Quoted 11 lines
ell/rtnl.h +4 -0
+						void *user_data);
+
  struct l_rtnl_address *l_rtnl_address_new(const char *ip, uint8_t prefix_len);
  struct l_rtnl_address *l_rtnl_address_clone(const struct l_rtnl_address *orig);
  void l_rtnl_address_free(struct l_rtnl_address *addr);
@@ -191,6 +194,39 @@ uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex,
  					void *user_data,
  					l_netlink_destroy_func_t destroy);
 
+uint32_t l_rtnl_neighbour_get_ipv4_hwaddr(struct l_netlink *rtnl, int ifindex,
+					uint32_t addr,

We do not use uint32_t to represent an ipv4 anywhere in the ell API.

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 203

Quoted 5 lines
ell/rtnl.h +5 -0
+					l_rtnl_neighbour_get_cb_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_get_ipv6_hwaddr(struct l_netlink *rtnl, int ifindex,
+					const uint8_t *addr,

similarly to above, ipv6 is never represented like this.

I was hoping to avoid using in_addr / in6_addr in the public API, but maybe that would be the lesser evil option than 'const uint8_t *'. Or just use l_rtnl_address for everything.

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 213

Quoted 10 lines
ell/rtnl.h +10 -0
+					l_rtnl_neighbour_get_cb_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_get_addr_hwaddr(struct l_netlink *rtnl, int ifindex,
+					const struct l_rtnl_address *addr,
+					l_rtnl_neighbour_get_cb_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_ipv4_hwaddr(struct l_netlink *rtnl, int ifindex,
+					uint32_t addr, const uint8_t *hwaddr,

You're making an assumption that hwaddr is always 6? That will not really be true long term. Might as well take care of this now.

Quoted 6 lines
ell/rtnl.h +6 -0
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_ipv6_hwaddr(struct l_netlink *rtnl, int ifindex,
+					const uint8_t *addr,
+					const uint8_t *hwaddr,

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 229

Quoted 10 lines
ell/rtnl.h +10 -0
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_addr_hwaddr(struct l_netlink *rtnl, int ifindex,
+					const struct l_rtnl_address *addr,
+					const uint8_t *hwaddr,
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
+

Regards,
-Denis

#2 lore raw

Hi Denis,

Quoted 51 lines · Denis Kenzior
Quoted 15 lines · Andrew Zaborowski

Add a few variants of RTNL utilities to look up addresses in the kernel's neighbour table entries and to add new entries. This is basically the ARP (for IPv4) and NDP (for IPv6) tables, that map IP addresses to hardware addresses.
---
There are three variants of each method, I currently only have a use case for the ipv4 and ipv6 variants so I'm happy dropping the struct l_rtnl_address variant.

  ell/ell.sym  |   6 ++
  ell/rtnl.c   | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++
  ell/rtnl.h   |  36 ++++++++++
  ell/useful.h |   4 ++
  4 files changed, 240 insertions(+)

<snip>

ell/rtnl.h extern "C" { · line 36

Quoted 9 lines
ell/rtnl.h +1 -0
index 9ea24f5..834cd14 100644
@@ -33,6 +33,9 @@ extern "C" {
  struct l_rtnl_address;
  struct l_rtnl_route;
 
+typedef void (*l_rtnl_neighbour_get_cb_t) (int error, const uint8_t *hwaddr,

You probably want a length as well, no?

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 198

Quoted 11 lines
ell/rtnl.h +4 -0
+                                             void *user_data);
+
  struct l_rtnl_address *l_rtnl_address_new(const char *ip, uint8_t prefix_len);
  struct l_rtnl_address *l_rtnl_address_clone(const struct l_rtnl_address *orig);
  void l_rtnl_address_free(struct l_rtnl_address *addr);
@@ -191,6 +194,39 @@ uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex,
                                      void *user_data,
                                      l_netlink_destroy_func_t destroy);
 
+uint32_t l_rtnl_neighbour_get_ipv4_hwaddr(struct l_netlink *rtnl, int ifindex,
+                                     uint32_t addr,

We do not use uint32_t to represent an ipv4 anywhere in the ell API.

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 203

Quoted 5 lines
ell/rtnl.h +5 -0
+                                     l_rtnl_neighbour_get_cb_t cb,
+                                     void *user_data,
+                                     l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_get_ipv6_hwaddr(struct l_netlink *rtnl, int ifindex,
+                                     const uint8_t *addr,
Quoted 5 lines

similarly to above, ipv6 is never represented like this.

I was hoping to avoid using in_addr / in6_addr in the public API, but maybe that would be the lesser evil option than 'const uint8_t *'. Or just use l_rtnl_address for everything.

My preference would be for in_addr / in6_addr, l_rtnl_address seems to be overkill for a wrapper for a 32-bit value.

The only evil aspect of these that I can see is uint32_t's endianness ambiguity (something similar to kernel's __be32 would have been better) but the simplicity is a big advantage.

Quoted 14 lines

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 213

Quoted 11 lines
ell/rtnl.h +10 -0
+                                     l_rtnl_neighbour_get_cb_t cb,
+                                     void *user_data,
+                                     l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_get_addr_hwaddr(struct l_netlink *rtnl, int ifindex,
+                                     const struct l_rtnl_address *addr,
+                                     l_rtnl_neighbour_get_cb_t cb,
+                                     void *user_data,
+                                     l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_ipv4_hwaddr(struct l_netlink *rtnl, int ifindex,
+                                     uint32_t addr, const uint8_t *hwaddr,

You're making an assumption that hwaddr is always 6? That will not really be true long term. Might as well take care of this now.

I am, but if the address is not 6 bytes the user will know it based on the ifindex too. We can be more explicit and include a length though.

Best regards

#3 lore raw

Hi Andrew,

Quoted 6 lines
Quoted 3 lines

I was hoping to avoid using in_addr / in6_addr in the public API, but maybe that would be the lesser evil option than 'const uint8_t *'. Or just use l_rtnl_address for everything.

My preference would be for in_addr / in6_addr, l_rtnl_address seems to be overkill for a wrapper for a 32-bit value.

I wouldn't actually mind seeing more context on how you're using these. In theory you would need the rtnl address anyway, unless you're using this within dhcp server classes. In which case you can provide a private API as well.

Quoted 5 lines

The only evil aspect of these that I can see is uint32_t's endianness ambiguity (something similar to kernel's __be32 would have been better) but the simplicity is a big advantage.

Use of struct in_addr would imply network order is being used. Same as inet_ntoa/inet_ntop, etc.

Quoted 15 lines
Quoted 14 lines

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 213

Quoted 11 lines
ell/rtnl.h +10 -0
+                                     l_rtnl_neighbour_get_cb_t cb,
+                                     void *user_data,
+                                     l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_get_addr_hwaddr(struct l_netlink *rtnl, int ifindex,
+                                     const struct l_rtnl_address *addr,
+                                     l_rtnl_neighbour_get_cb_t cb,
+                                     void *user_data,
+                                     l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_ipv4_hwaddr(struct l_netlink *rtnl, int ifindex,
+                                     uint32_t addr, const uint8_t *hwaddr,

You're making an assumption that hwaddr is always 6? That will not really be true long term. Might as well take care of this now.

Quoted 2 lines

I am, but if the address is not 6 bytes the user will know it based on the ifindex too. We can be more explicit and include a length though.

User might, but API implementation doesn't. How does the implementation of this function know that ifindex is for a device that uses 8 byte hardware address? ifindex is just an int as far as it is concerned. So unless you're going to introduce a netdev abstraction, hardware length would be needed here at some point.

Regards,
-Denis

#4 lore raw
Quoted 10 lines · Denis Kenzior
Quoted 6 lines
Quoted 3 lines

I was hoping to avoid using in_addr / in6_addr in the public API, but maybe that would be the lesser evil option than 'const uint8_t *'. Or just use l_rtnl_address for everything.

My preference would be for in_addr / in6_addr, l_rtnl_address seems to be overkill for a wrapper for a 32-bit value.

I wouldn't actually mind seeing more context on how you're using these. In theory you would need the rtnl address anyway, unless you're using this within dhcp server classes. In which case you can provide a private API as well.

This would be used in IWD like in
https://github.com/balrog-kun/iwd/commit/2da9ad4bb20a85f53fc459b5ec00dd90adbfe01f#diff-38b3dd2a52d30079b126664f14e4c72daab4887eb5d84aa07c684076db1f5e0cR278-R292

We only build an l_rtnl_address for our own IPs in netconfig.c.

Quoted 9 lines
Quoted 6 lines

The only evil aspect of these that I can see is uint32_t's endianness ambiguity (something similar to kernel's __be32 would have been better) but the simplicity is a big advantage.

Use of struct in_addr would imply network order is being used. Same as inet_ntoa/inet_ntop, etc.

Right, and same as __be32. __be32 has the advantage that it's not a struct so, 1. you know its size and contents and can pass its pointer directly to, say, an attribute builder without looking up internal structures, 2. don't need to convert to/from it by assigning cryptic field names, 3. can pass it as parameter.

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

ell/rtnl.h uint32_t l_rtnl_route_delete(struct l_netlink *rtnl, int ifindex, · line 213

Quoted 12 lines
ell/rtnl.h +10 -0
+                                     l_rtnl_neighbour_get_cb_t cb,
+                                     void *user_data,
+                                     l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_get_addr_hwaddr(struct l_netlink *rtnl, int ifindex,
+                                     const struct l_rtnl_address *addr,
+                                     l_rtnl_neighbour_get_cb_t cb,
+                                     void *user_data,
+                                     l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_neighbour_set_ipv4_hwaddr(struct l_netlink *rtnl, int ifindex,
+                                     uint32_t addr, const uint8_t *hwaddr,

You're making an assumption that hwaddr is always 6? That will not really be true long term. Might as well take care of this now.

Quoted 7 lines
Quoted 2 lines

I am, but if the address is not 6 bytes the user will know it based on the ifindex too. We can be more explicit and include a length though.

User might, but API implementation doesn't. How does the implementation of this function know that ifindex is for a device that uses 8 byte hardware address? ifindex is just an int as far as it is concerned. So unless you're going to introduce a netdev abstraction, hardware length would be needed here at some point.

True, we don't have netdev data directly in ell and would need to either get it from the user or query it from the kernel if that is ever a concern.

Best regards

#5 lore raw

Hi Andrew,

Quoted 8 lines
Quoted 2 lines

Use of struct in_addr would imply network order is being used. Same as inet_ntoa/inet_ntop, etc.

Right, and same as __be32. __be32 has the advantage that it's not a struct so, 1. you know its size and contents and can pass its pointer directly to, say, an attribute builder without looking up internal structures, 2. don't need to convert to/from it by assigning cryptic field names, 3. can pass it as parameter.

Some of what you say isn't really correct. Look at the signature of inet_ntoa for example.

You're arguing for use of __be32 because you store ipv4 addresses as uints. I get that. But that doesn't help you with ipv6 and there is already a well defined structure to represent an ipv4 address.

Anyhow, given how you're planning to use this, I'd just adopt a signature similar to inet_ntop. Something like:

l_rtnl_neighbor_set_hwaddr(rtnl, ifindex, int family, const void *, const uint8_t *hwaddr, size_t hwaddr_len, cb, user_data, destroy)

Also, US spelling of 'neighbor' would be preferred.

Regards,
-Denis

#6 lore raw

Hi Denis,

Quoted 9 lines · Denis Kenzior
Quoted 8 lines
Quoted 2 lines

Use of struct in_addr would imply network order is being used. Same as inet_ntoa/inet_ntop, etc.

Right, and same as __be32. __be32 has the advantage that it's not a struct so, 1. you know its size and contents and can pass its pointer directly to, say, an attribute builder without looking up internal structures, 2. don't need to convert to/from it by assigning cryptic field names, 3. can pass it as parameter.

Quoted 6 lines

Some of what you say isn't really correct. Look at the signature of inet_ntoa for example.

You're arguing for use of __be32 because you store ipv4 addresses as uints. I get that. But that doesn't help you with ipv6 and there is already a well defined structure to represent an ipv4 address.

I'd argue that an array for ipv6 is still more practical than the same array wrapped in a struct (basically same thing as with IPv4 in_addr) but ok.

Quoted 6 lines

Anyhow, given how you're planning to use this, I'd just adopt a signature similar to inet_ntop. Something like:

l_rtnl_neighbor_set_hwaddr(rtnl, ifindex, int family, const void *, const uint8_t *hwaddr, size_t hwaddr_len, cb, user_data, destroy)

Ok.

Quoted 2 lines

Also, US spelling of 'neighbor' would be preferred.

The reason I chose neighbour is because that is the name of the kernel API but ok.

Best regards