icmp6: Allow multiple event handlers and clean up
Andrew Zaborowski
PATCH 1/5 icmp6: Allow multiple event handlers and clean up
Allow registering multiple event handlers and rename l_icmp6_client_set_event_handler to l_icmp6_client_add_event_handler accordingly. The use case is for a client like IWD to be able to track routing information.
This also fixes a bug where the destroy callback passed to l_icmp6_client_set_event_handler wasn't actually used except if l_icmp6_client_set_event_handler was called again.
While there, add an event_data parameter to the event callback, rename the "userdata" parameters to "user_data" for consistency and clean up an extra empty line.
--- ell/dhcp6.c | 4 ++-- ell/ell.sym | 2 +- ell/icmp6.c | 56 +++++++++++++++++++++++++++++++++++++++-------------- ell/icmp6.h | 8 ++++---- 4 files changed, 48 insertions(+), 22 deletions(-)
ell/dhcp6.c +2 -2
@@ -1467,7 +1467,7 @@ static void dhcp6_client_send_initial(struct l_dhcp6_client *client)static void dhcp6_client_icmp6_event (struct l_icmp6_client *icmp6,enum l_icmp6_client_event event, -void *user_data) +void *event_data,void *user_data) {struct l_dhcp6_client *client = user_data;
@@ -1521,7 +1521,7 @@ LIB_EXPORT struct l_dhcp6_client *l_dhcp6_client_new(uint32_t ifindex) client->request_na = true; client->icmp6 =l_icmp6_client_new (ifindex); -l_icmp6_client_set_event_handler (client->icmp6, +l_icmp6_client_add_event_handler (client->icmp6, dhcp6_client_icmp6_event, client,NULL );
ell/ell.sym +1 -1
@@ -679,7 +679,7 @@ global: l_icmp6_client_start; l_icmp6_client_stop; l_icmp6_client_get_router; - l_icmp6_client_set_event_handler; + l_icmp6_client_add_event_handler; l_icmp6_client_set_debug; l_icmp6_client_set_address; l_icmp6_client_set_nodelay;
ell/icmp6.c +41 -15
112 lines not on this page · raw message
ell/icmp6.h +4 -4
@@ -38,10 +38,10 @@ enum l_icmp6_client_event { };typedef void (*l_icmp6_debug_cb_t )(const char *str,void *user_data); -typedef void (*l_icmp6_destroy_cb_t )(void *userdata); +typedef void (*l_icmp6_destroy_cb_t )(void *user_data);typedef void (*l_icmp6_client_event_cb_t )(struct l_icmp6_client *client,enum l_icmp6_client_event event, -void *userdata); +void *event_data,void *user_data);struct l_icmp6_client *l_icmp6_client_new (uint32_t ifindex);void l_icmp6_client_free (struct l_icmp6_client *client);
@@ -51,9 +51,9 @@ bool l_icmp6_client_stop(struct l_icmp6_client *client);const struct l_icmp6_router *l_icmp6_client_get_router (struct l_icmp6_client *client); -bool l_icmp6_client_set_event_handler (struct l_icmp6_client *client, +bool l_icmp6_client_add_event_handler (struct l_icmp6_client *client,l_icmp6_client_event_cb_t handler, -void *userdata, +void *user_data,l_icmp6_destroy_cb_t destroy); bool l_icmp6_client_set_debug(struct l_icmp6_client *client, l_icmp6_debug_cb_t function,
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 2/5 icmp6: Free ra in icmp6_client_read_handler on ECANCELED
Make sure we free @ra in in icmp6_client_read_handler even when icmp6_client_handle_message returns -ECANCELED.
Fixes this:
==1088== 1,176 bytes in 9 blocks are definitely lost in loss record 28 of 29
==1088== at 0x4843839: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1088== by 0x4808A1: l_malloc (util.c:62) ==1088== by 0x4C0966: icmp6_client_read_handler (icmp6.c:659) ==1088== by 0x4877AF: io_callback (io.c:120) ==1088== by 0x48693C: l_main_iterate (main.c:478) ==1088== by 0x486A0B: l_main_run (main.c:525) ==1088== by 0x486A0B: l_main_run (main.c:507) ==1088== by 0x486C3F: l_main_run_with_signal (main.c:647) ==1088== by 0x404755: main (main.c:532) --- ell/icmp6.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
ell/icmp6.c +1 -3
@@ -401,9 +401,7 @@ static bool icmp6_client_read_handler(struct l_io *io, void *userdata) } r =icmp6_client_handle_message (client, ra, l, &src); -if (r == -ECANCELED ) -return true; -else if (r <0 ) +if (r <0 ) goto done;/* Stop solicitations */
Signature · 1 line
2.32.0
Denis Kenzior
AppliedRe: [PATCH 2/5] icmp6: Free ra in icmp6_client_read_handler on ECANCELED
Hi Andrew,
Quoted 5 lines · Andrew Zaborowski
Make sure we free @ra in in
icmp6_client_read_handlereven whenicmp6_client_handle_messagereturns-ECANCELED.Fixes this:
==1088== 1,176 bytes in 9 blocks are definitely lost in loss record 28 of 29
Quoted 13 lines
==1088== at 0x4843839: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1088== by 0x4808A1: l_malloc (util.c:62) ==1088== by 0x4C0966: icmp6_client_read_handler (icmp6.c:659) ==1088== by 0x4877AF: io_callback (io.c:120) ==1088== by 0x48693C: l_main_iterate (main.c:478) ==1088== by 0x486A0B: l_main_run (main.c:525) ==1088== by 0x486A0B: l_main_run (main.c:507) ==1088== by 0x486C3F: l_main_run_with_signal (main.c:647) ==1088== by 0x404755: main (main.c:532) --- ell/icmp6.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
I added an additional Fixes tag:
8cef9c50c9f4 ("icmp6: Don't crash if client was stopped")
Applied, thanks.
Regards,
-Denis
Andrew Zaborowski
PATCH 3/5 icmp6: Call debug user data destroy handler
In reply to Andrew Zaborowski--- ell/icmp6.c | 1 + 1 file changed, 1 insertion(+)
ell/icmp6.c +1 -0
@@ -473,6 +473,7 @@ LIB_EXPORT void l_icmp6_client_free(struct l_icmp6_client *client)l_icmp6_client_stop (client);l_queue_destroy (client->routes,NULL ); +l_icmp6_client_set_debug (client,NULL ,NULL ,NULL );l_queue_destroy (client->event_handlers, icmp6_event_handler_destroy);l_free (client); }
Signature · 1 line
2.32.0
Denis Kenzior
AppliedRe: [PATCH 3/5] icmp6: Call debug user data destroy handler
Hi Andrew,
Quoted 4 lines · Andrew Zaborowski
--- ell/icmp6.c | 1 + 1 file changed, 1 insertion(+)
Applied, thanks.
Regards,
-Denis
Andrew Zaborowski
PATCH 4/5 icmp6: Track routers and prefix routes from multiple RAs
In reply to Andrew ZaborowskiAdd tracking of default/non-default routers and prefix routes in separate lists that are updated with new information from new RAs. This is gets us closer to what RFC4861 mandates.
There's no single client->routes list now, there are two lists, one for the routers and one for prefix routes. According to the RFC there's no relation between the prefix routes and which router's RA they were received in (same prefixes can be received from multiple routers).
The DHCP6 behaviour should be unchanged for now, it'll still only use the first router that sent an announcement. The public API is also unchnaged for now except for new events.
---
There are some rought corners in this code but the commit is already a little big. For example the route_info structure we use for prefix routes is actually sent to clients in event_data but the structure is private so it's useless to the clients as is.
Also icmp6 may end up using a lot of timeouts since we have to track router timeouts and prefix route timeouts. In this case it would be pretty easy to convert all those timeouts into one that only tracks the soonest expiry event so I'd be happy to do that. We can't really avoid tracking the lifetimes if we want to correctly implement the RFC.
--- ell/dhcp6.c | 15 +- ell/icmp6-private.h | 13 ++ ell/icmp6.c | 339 ++++++++++++++++++++++++++++++++++++-------- ell/icmp6.h | 6 + 4 files changed, 313 insertions(+), 60 deletions(-)
ell/dhcp6.c +13 -2
@@ -1471,8 +1471,9 @@ static void dhcp6_client_icmp6_event(struct l_icmp6_client *icmp6, {struct l_dhcp6_client *client = user_data; -l_timeout_remove (client->timeout_send); - client->timeout_send =NULL ; +/* We only process the first RA received for now */ +if (!client->timeout_send) +return ;switch (event) {case L_ICMP6_CLIENT_EVENT_ROUTER_FOUND :
@@ -1482,6 +1483,9 @@ static void dhcp6_client_icmp6_event(struct l_icmp6_client *icmp6,bool managed =l_icmp6_router_get_managed (r);bool other =l_icmp6_router_get_other (r); +l_timeout_remove (client->timeout_send); + client->timeout_send =NULL ; +CLIENT_DEBUG ("Received RA, managed: %s, other: %s" , managed ?"yes" :"no" , other ?"yes" :"no" );
@@ -1498,6 +1502,13 @@ static void dhcp6_client_icmp6_event(struct l_icmp6_client *icmp6,break ; } +case L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_ADDED : +case L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_REMOVED : +case L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_CHANGED : +case L_ICMP6_CLIENT_EVENT_ONLINK_PREFIX_ADDED : +case L_ICMP6_CLIENT_EVENT_ONLINK_PREFIX_REMOVED : +case L_ICMP6_CLIENT_EVENT_ONLINK_PREFIX_CHANGED : +break ; } }
ell/icmp6-private.h +13 -0
@@ -20,12 +20,20 @@* */ +struct l_rtnl_route ; +struct l_timeout ; +struct l_icmp6_client ; +struct route_info {uint8_t address[16 ];uint8_t prefix_len;uint8_t preference;uint32_t preferred_lifetime;uint32_t valid_lifetime; +struct l_rtnl_route *route; +uint64_t expiry; +struct l_timeout *timeout; +struct l_icmp6_client *client; };struct l_icmp6_router {
@@ -37,6 +45,11 @@ struct l_icmp6_router {uint32_t mtu;uint32_t n_prefixes;struct route_info *prefixes; +struct l_rtnl_route *default_route; +uint64_t default_expiry; +struct l_timeout *default_timeout; +struct l_icmp6_client *client; +uint64_t ra_timestamp; };struct l_icmp6_router *_icmp6_router_new ();
ell/icmp6.c +281 -58
428 lines not on this page · raw message
ell/icmp6.h +6 -0
@@ -35,6 +35,12 @@ struct l_netlink;enum l_icmp6_client_event {L_ICMP6_CLIENT_EVENT_ROUTER_FOUND =0 , +L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_ADDED , +L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_REMOVED , +L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_CHANGED , +L_ICMP6_CLIENT_EVENT_ONLINK_PREFIX_ADDED , +L_ICMP6_CLIENT_EVENT_ONLINK_PREFIX_REMOVED , +L_ICMP6_CLIENT_EVENT_ONLINK_PREFIX_CHANGED , };typedef void (*l_icmp6_debug_cb_t )(const char *str,void *user_data);
Signature · 1 line
2.32.0
Denis Kenzior
Re: [PATCH 4/5] icmp6: Track routers and prefix routes from multiple RAs
Hi Andrew,
Quoted 4 lines · Andrew Zaborowski
Add tracking of default/non-default routers and prefix routes in separate lists that are updated with new information from new RAs. This is gets us closer to what RFC4861 mandates.
So I'm still reviewing this, but let me share some preliminary thoughts and see what you think.
Quoted 4 lines
There's no single
client->routeslist now, there are two lists, one for the routers and one for prefix routes. According to the RFC there's no relation between the prefix routes and which router's RA they were received in (same prefixes can be received from multiple routers).
While this makes sense on some level, I'm actually concerned that this can lead to real weirdness. For example, you might have one RA broadcasting a prefix with a non-zero lifetime and another RA broadcasting the same prefix with a lifetime of 0. They might also have differing priority, mtu or valid lifetime information. It may be easier to just simply set the info into the kernel and let it deal with it.
I actually wonder how common this actually is in the real world?
Quoted 4 lines
The DHCP6 behaviour should be unchanged for now, it'll still only use the first router that sent an announcement. The public API is also unchnaged for now except for new events.
As you point out, this patch is getting rather long. Perhaps you want to break it up and add the ROUTER and PREFIX events in separate commits.
Also, some of these events are somewhat useless if icmp6 is responsible for setting the routes into the kernel. Maybe they shouldn't even be fired in this case.
Quoted 5 lines
---
There are some rought corners in this code but the commit is already a little big. For example theroute_infostructure we use for prefix routes is actually sent to clients inevent_databut the structure is private so it's useless to the clients as is.
This might need to be figured out first.
Quoted 6 lines
Also icmp6 may end up using a lot of timeouts since we have to track router timeouts and prefix route timeouts. In this case it would be pretty easy to convert all those timeouts into one that only tracks the soonest expiry event so I'd be happy to do that. We can't really avoid tracking the lifetimes if we want to correctly implement the RFC.
This is another part that seriously concerns me right now. I actually wonder why we would need to use timeouts at all? If we set the lifetime into the kernel, then it can take care of removing these routes (assuming it doesn't just leave them in there with lifetime 0).
Quoted 7 lines
--- ell/dhcp6.c | 15 +- ell/icmp6-private.h | 13 ++ ell/icmp6.c | 339 ++++++++++++++++++++++++++++++++++++-------- ell/icmp6.h | 6 + 4 files changed, 313 insertions(+), 60 deletions(-)
<snip>
Quoted 20 lines
ell/icmp6.c +3
-11@@ -292,53 +292,242 @@ static bool icmp6_client_remove_route(void *data, void *user_data)return true; } -static void icmp6_client_setup_routes (struct l_icmp6_client *client) +static void icmp6_router_timeout (struct l_timeout *timeout,void *user_data) { -struct l_icmp6_router *ra = client->ra; -struct l_rtnl_route *rt; -char buf[INET6_ADDRSTRLEN ]; -unsigned int i; +struct l_icmp6_router *r = user_data; - rt =l_rtnl_route_new_gateway (inet_ntop (AF_INET6 , ra->address, - buf,sizeof (buf))); -if (!rt) { -CLIENT_DEBUG ("Unable to parse RA 'from' address" ); -return ; - } +icmp6_client_remove_route (l_steal_ptr (r->default_route), r->client);
ell/icmp6.c static bool icmp6_client_remove_route(void *data, void *user_data) · line 304
Quoted 10 lines
ell/icmp6.c +4
-5+l_timeout_remove (l_steal_ptr (r->default_timeout)); + r->default_expiry =0 ; -l_rtnl_route_set_preference (rt, ra->pref); -l_rtnl_route_set_protocol (rt,RTPROT_RA ); -l_rtnl_route_set_mtu (rt, ra->mtu); -l_rtnl_route_set_priority (rt, client->route_priority); -l_queue_push_tail (client->routes, rt); +icmp6_client_event_notify (r->client, +L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_REMOVED , r);
What does the kernel do when a default route reaches 0 lifetime? Can we listen to DELROUTE events and infer that the lifetime expired based on the timestamp of the event and our own records?
Quoted 17 lines
ell/icmp6.c +10
-5+} -if (client->rtnl) -l_rtnl_route_add (client->rtnl, client->ifindex, rt, -NULL ,NULL ,NULL ); +static bool icmp6_route_info_free (void *data,void *user_data) { +struct route_info *info = data; -for (i =0 ; i < ra->n_prefixes; i++) { -struct route_info *info = &ra->prefixes[i]; +icmp6_client_remove_route (info->route, info->client); +l_timeout_remove (info->timeout); +l_free (info); +return true; +} + +static void icmp6_prefix_timeout (struct l_timeout *timeout,void *user_data)
ell/icmp6.c static bool icmp6_client_remove_route(void *data, void *user_data) · line 324
Quoted 10 lines
ell/icmp6.c +7
-2+{ +struct route_info *info = user_data; -if (info->valid_lifetime ==0 ) -continue ; +l_queue_remove (info->client->prefix_routes, info); +icmp6_client_event_notify (info->client, +L_ICMP6_CLIENT_EVENT_ONLINK_PREFIX_REMOVED , + info); +icmp6_route_info_free (info,NULL );
Same as above here?
Quoted 31 lines
ell/icmp6.c +17
-10+} -if (!inet_ntop (AF_INET6 , info->address, buf,sizeof (buf))) -continue ; +/* Mostly RFC4861 Section 6.3.4 */ +static void icmp6_router_update (struct l_icmp6_client *client, +struct l_icmp6_router *r, +struct l_icmp6_router *new_ra) +{ +bool changed = false; +enum l_icmp6_client_event event =-1 ; - rt =l_rtnl_route_new_prefix (buf, info->prefix_len); -if (!rt) -continue ; +if (r) { +uint64_t new_expiry =0 ; -l_rtnl_route_set_preference (rt, info->preference); -l_rtnl_route_set_protocol (rt,RTPROT_RA ); -l_rtnl_route_set_mtu (rt, ra->mtu); -l_rtnl_route_set_priority (rt, client->route_priority); -l_queue_push_tail (client->routes, rt); + r->lifetime = new_ra->lifetime; +if (r->lifetime) + new_expiry = new_ra->ra_timestamp + + r->lifetime *L_USEC_PER_SEC ; + +if (r->default_expiry != new_expiry) { + r->default_expiry = new_expiry;
ell/icmp6.c static bool icmp6_client_remove_route(void *data, void *user_data) · line 355
Quoted 10 lines
ell/icmp6.c +10
-0+ changed = true; + } + +if (new_ra->mtu) { + r->mtu = new_ra->mtu; + changed = true; + } + }else + r = new_ra; +
One thing to keep in mind is that for us, the 99% use case is a single router that keeps sending the same RA message every several seconds. The net effect is that the same route info keeps getting re-uploaded to the kernel every few seconds. I think this is pointless and we need to optimize that. Maybe something along these lines:
- If RA is binary the same, extract the new lifetime.
- If the lifetime of the gateway route (timestamped at the time when we sent the NEWROUTE message) is still at least N% of the original lifetime, then do nothing.
- Otherwise, re-upload the route to the kernel
I guess for external client purposes (NM) you may want to make the N above configurable and emit the event on every RA. Seems wasteful though?
Also, are you sure that we need to fully remove the route just to change the lifetime?
Quoted 35 lines
ell/icmp6.c +15
-5@@ -347,31 +536,53 @@ static int icmp6_client_handle_message(struct l_icmp6_client *client, const struct in6_addr *src) {struct l_icmp6_router *r =_icmp6_router_parse (ra, len, src->s6_addr); +struct l_icmp6_router *old_ra; +unsigned int i;if (!r)return -EBADMSG ; -if (!client->ra) { - client->ra = r; + old_ra =l_queue_remove_if (client->routers, icmp6_router_match_address, + r->address); +if (old_ra) { +/* Move to the head of the queue */ +l_queue_push_head (client->routers, old_ra); + }else { + r->ra_timestamp =l_time_now (); + r->client = client; +l_queue_push_head (client->routers, r); +icmp6_client_event_notify (client,L_ICMP6_CLIENT_EVENT_ROUTER_FOUND ,NULL );/* DHCP6 client may have stopped us */ -if (!client->ra) +if (!client->io)return -ECANCELED ; + } -icmp6_client_setup_routes (client); -return 0 ; +icmp6_router_update (client, old_ra, r);
ell/icmp6.c static int icmp6_client_handle_message(struct l_icmp6_client *client, · line 574
Quoted 10 lines
ell/icmp6.c +10
-0+ +if (!client->io) + goto done; + +for (i =0 ; i < r->n_prefixes; i++) { +struct route_info *info = &r->prefixes[i]; +struct route_info *old_info = +l_queue_remove_if (client->prefix_routes, + icmp6_route_match_prefix, info); +
As mentioned above, I wonder if this is the right approach.
ell/icmp6.c static int icmp6_client_handle_message(struct l_icmp6_client *client, · line 580
Quoted 6 lines
ell/icmp6.c +4
-0+icmp6_onlink_prefix_update (client, r, old_info, info); + +if (!client->io) + goto done; }
Regards,
-Denis
Andrew Zaborowski
Re: [PATCH 4/5] icmp6: Track routers and prefix routes from multiple RAs
Hi Denis,
Quoted 14 lines · Denis Kenzior
Quoted 4 lines · Andrew Zaborowski
Add tracking of default/non-default routers and prefix routes in separate lists that are updated with new information from new RAs. This is gets us closer to what RFC4861 mandates.
So I'm still reviewing this, but let me share some preliminary thoughts and see what you think.
Quoted 4 lines
There's no single
client->routeslist now, there are two lists, one for the routers and one for prefix routes. According to the RFC there's no relation between the prefix routes and which router's RA they were received in (same prefixes can be received from multiple routers).
Quoted 5 lines
While this makes sense on some level, I'm actually concerned that this can lead to real weirdness. For example, you might have one RA broadcasting a prefix with a non-zero lifetime and another RA broadcasting the same prefix with a lifetime of 0. They might also have differing priority, mtu or valid lifetime information.
Right, so the RFC specifically talks about how cases like these are dealt with, in essence every parameter is optional (has a possible "not present" value) and we should keep the old value if the new value is not present. Now it could happen that two routers are adevrtising different actual values and I think it would be a configuration error, I don't think we should be ignoring new values just because in theory it's possible that they're wrong.
I found that some network stacks document exactly how they process router advertisements (also which options they support etc.), e.g. Cisco and z/OS document this. They seem to follow the RFC exactly, but it being explicitly explained in their docs should help admins be clear in how those things should be configured.
Quoted 4 lines
It may be easier to just simply set the info into the kernel and let it deal with it.
I actually wonder how common this actually is in the real world?
I have no idea.
Quoted 6 lines
Quoted 5 lines
The DHCP6 behaviour should be unchanged for now, it'll still only use the first router that sent an announcement. The public API is also unchnaged for now except for new events.
Quoted 6 lines
As you point out, this patch is getting rather long. Perhaps you want to break it up and add the ROUTER and PREFIX events in separate commits.
Also, some of these events are somewhat useless if icmp6 is responsible for setting the routes into the kernel. Maybe they shouldn't even be fired in this case.
Right, we could do this but we wouldn't be saving a lot of cycles and it sounds like a hack.
Quoted 8 lines
Quoted 6 lines
---
There are some rought corners in this code but the commit is already a little big. For example theroute_infostructure we use for prefix routes is actually sent to clients inevent_databut the structure is private so it's useless to the clients as is.This might need to be figured out first.
Maybe I should pass the l_rtnl_route objects.
Quoted 8 lines
Quoted 7 lines
Also icmp6 may end up using a lot of timeouts since we have to track router timeouts and prefix route timeouts. In this case it would be pretty easy to convert all those timeouts into one that only tracks the soonest expiry event so I'd be happy to do that. We can't really avoid tracking the lifetimes if we want to correctly implement the RFC.
Quoted 4 lines
This is another part that seriously concerns me right now. I actually wonder why we would need to use timeouts at all? If we set the lifetime into the kernel, then it can take care of removing these routes (assuming it doesn't just leave them in there with lifetime 0).
So when we receive a new RA we need to know whether we already have this router/prefix to know how to process it. For a lot of things we can instead save an expiration timestamp and next time we look at the data we ignore it if it's expired. But would couldn't emit events.
The docs I mentioned also explain how different RA or prefix parameters influcence how two routes can be shadowed by other routes already in the system and how they're supposed to be re-instated when the higher priority route times out, or how a lower priority route is supposed to be re-instated when this route is removed, etc.
Quoted 34 lines
Quoted 8 lines
--- ell/dhcp6.c | 15 +- ell/icmp6-private.h | 13 ++ ell/icmp6.c | 339 ++++++++++++++++++++++++++++++++++++-------- ell/icmp6.h | 6 + 4 files changed, 313 insertions(+), 60 deletions(-)<snip>
Quoted 23 lines
ell/icmp6.c +5
-11@@ -292,53 +292,242 @@ static bool icmp6_client_remove_route(void *data, void *user_data)return true; } -static void icmp6_client_setup_routes (struct l_icmp6_client *client) +static void icmp6_router_timeout (struct l_timeout *timeout,void *user_data) { -struct l_icmp6_router *ra = client->ra; -struct l_rtnl_route *rt; -char buf[INET6_ADDRSTRLEN ]; -unsigned int i; +struct l_icmp6_router *r = user_data; - rt =l_rtnl_route_new_gateway (inet_ntop (AF_INET6 , ra->address, - buf,sizeof (buf))); -if (!rt) { -CLIENT_DEBUG ("Unable to parse RA 'from' address" ); -return ; - } +icmp6_client_remove_route (l_steal_ptr (r->default_route), r->client); +l_timeout_remove (l_steal_ptr (r->default_timeout)); + r->default_expiry =0 ;
Quoted 11 lines
ell/icmp6.c static bool icmp6_client_remove_route(void *data, void *user_data) · line 304
Quoted 7 lines
ell/icmp6.c +2
-5-l_rtnl_route_set_preference (rt, ra->pref); -l_rtnl_route_set_protocol (rt,RTPROT_RA ); -l_rtnl_route_set_mtu (rt, ra->mtu); -l_rtnl_route_set_priority (rt, client->route_priority); -l_queue_push_tail (client->routes, rt); +icmp6_client_event_notify (r->client, +L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_REMOVED , r);What does the kernel do when a default route reaches 0 lifetime? Can we listen to DELROUTE events and infer that the lifetime expired based on the timestamp of the event and our own records?
We could do this if ell is managing the RTNL routes. I considered it but I thought it's easiest to just have one variant of the code (at least initially) and one place where it's all handled whether we're managing the RTNL routes or delegating it.
Quoted 69 lines
Quoted 18 lines
ell/icmp6.c +10
-5+} -if (client->rtnl) -l_rtnl_route_add (client->rtnl, client->ifindex, rt, -NULL ,NULL ,NULL ); +static bool icmp6_route_info_free (void *data,void *user_data) { +struct route_info *info = data; -for (i =0 ; i < ra->n_prefixes; i++) { -struct route_info *info = &ra->prefixes[i]; +icmp6_client_remove_route (info->route, info->client); +l_timeout_remove (info->timeout); +l_free (info); +return true; +} + +static void icmp6_prefix_timeout (struct l_timeout *timeout,void *user_data)ell/icmp6.c static bool icmp6_client_remove_route(void *data, void *user_data) · line 324
Quoted 10 lines
ell/icmp6.c +7
-2+{ +struct route_info *info = user_data; -if (info->valid_lifetime ==0 ) -continue ; +l_queue_remove (info->client->prefix_routes, info); +icmp6_client_event_notify (info->client, +L_ICMP6_CLIENT_EVENT_ONLINK_PREFIX_REMOVED , + info); +icmp6_route_info_free (info,NULL );Same as above here?
Quoted 38 lines
ell/icmp6.c +24
-10+} -if (!inet_ntop (AF_INET6 , info->address, buf,sizeof (buf))) -continue ; +/* Mostly RFC4861 Section 6.3.4 */ +static void icmp6_router_update (struct l_icmp6_client *client, +struct l_icmp6_router *r, +struct l_icmp6_router *new_ra) +{ +bool changed = false; +enum l_icmp6_client_event event =-1 ; - rt =l_rtnl_route_new_prefix (buf, info->prefix_len); -if (!rt) -continue ; +if (r) { +uint64_t new_expiry =0 ; -l_rtnl_route_set_preference (rt, info->preference); -l_rtnl_route_set_protocol (rt,RTPROT_RA ); -l_rtnl_route_set_mtu (rt, ra->mtu); -l_rtnl_route_set_priority (rt, client->route_priority); -l_queue_push_tail (client->routes, rt); + r->lifetime = new_ra->lifetime; +if (r->lifetime) + new_expiry = new_ra->ra_timestamp + + r->lifetime *L_USEC_PER_SEC ; + +if (r->default_expiry != new_expiry) { + r->default_expiry = new_expiry; + changed = true; + } + +if (new_ra->mtu) { + r->mtu = new_ra->mtu; + changed = true; + }
Quoted 8 lines
ell/icmp6.c static bool icmp6_client_remove_route(void *data, void *user_data) · line 355
Quoted 3 lines
ell/icmp6.c +3
-0+ }else + r = new_ra; +One thing to keep in mind is that for us, the 99% use case is a single router that keeps sending the same RA message every several seconds. The net effect is that the same route info keeps getting re-uploaded to the kernel every few seconds. I think this is pointless and we need to optimize that.
What we could do is not set lifetimes on the RTNL routes and delete them ourselves, then we wouldn't have to update their lifetimes.
Quoted 7 lines
Maybe
something along these lines:- If RA is binary the same, extract the new lifetime.
- If the lifetime of the gateway route (timestamped at the time when we sent the NEWROUTE message) is still at least N% of the original lifetime, then do nothing.
- Otherwise, re-upload the route to the kernel
That sounds very complicated but maybe it's also an option, ideally though I'd do this when we have the basic version working.
Quoted 6 lines
I guess for external client purposes (NM) you may want to make the N above configurable and emit the event on every RA. Seems wasteful though?
Also, are you sure that we need to fully remove the route just to change the lifetime?
So with a quick search right now I can't see the place where it checks whether a route already exists, it might be that we can create duplicates which would also be bad... I'll try to look deeper in the kernel, this doesn't seem to be documented.
Best regards
Denis Kenzior
Re: [PATCH 4/5] icmp6: Track routers and prefix routes from multiple RAs
Hi Andrew,
Quoted 5 lines
I found that some network stacks document exactly how they process router advertisements (also which options they support etc.), e.g. Cisco and z/OS document this. They seem to follow the RFC exactly, but it being explicitly explained in their docs should help admins be clear in how those things should be configured.
Do you have some links handy?
Quoted 6 lines
Quoted 3 lines
Also, some of these events are somewhat useless if icmp6 is responsible for setting the routes into the kernel. Maybe they shouldn't even be fired in this case.
Right, we could do this but we wouldn't be saving a lot of cycles and it sounds like a hack.
How so? We in theory have two use cases:
1. iwd managing an ipv6 interface.
2. info is sent out via IP configuration events over D-Bus.
In case of 1, none of these events are really useful since iwd would just let icmp6/dhcp6_client manage this completely.
In case of 2, the client just wants a snapshot of what the settings should be, not get notified that each individual route has changed.
Or do you have something else in mind?
Quoted 9 lines
Quoted 4 lines
This is another part that seriously concerns me right now. I actually wonder why we would need to use timeouts at all? If we set the lifetime into the kernel, then it can take care of removing these routes (assuming it doesn't just leave them in there with lifetime 0).
So when we receive a new RA we need to know whether we already have this router/prefix to know how to process it. For a lot of things we can instead save an expiration timestamp and next time we look at the data we ignore it if it's expired. But would couldn't emit events.
I suppose we do need at least a single timer for case 2 above. But having a timer for each route seems excessive.
Quoted 8 lines
Quoted 5 lines
One thing to keep in mind is that for us, the 99% use case is a single router that keeps sending the same RA message every several seconds. The net effect is that the same route info keeps getting re-uploaded to the kernel every few seconds. I think this is pointless and we need to optimize that.
What we could do is not set lifetimes on the RTNL routes and delete them ourselves, then we wouldn't have to update their lifetimes.
Yes, we could do that since you're not passing lifetimes in the proposed iwd API. But is that intended or an oversight?
Regards,
-Denis
Andrew Zaborowski
Re: [PATCH 4/5] icmp6: Track routers and prefix routes from multiple RAs
Quoted 7 lines · Denis Kenzior
Quoted 5 lines
I found that some network stacks document exactly how they process router advertisements (also which options they support etc.), e.g. Cisco and z/OS document this. They seem to follow the RFC exactly, but it being explicitly explained in their docs should help admins be clear in how those things should be configured.
Do you have some links handy?
https://www.ibm.com/docs/en/zos/2.4.0?topic=discovery-router-advertisements
and the prefix information subpage.
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Also, some of these events are somewhat useless if icmp6 is responsible for setting the routes into the kernel. Maybe they shouldn't even be fired in this case.
Right, we could do this but we wouldn't be saving a lot of cycles and it sounds like a hack.
Quoted 7 lines
How so? We in theory have two use cases:
1. iwd managing an ipv6 interface.
2. info is sent out via IP configuration events over D-Bus.In case of 1, none of these events are really useful since iwd would just let icmp6/dhcp6_client manage this completely.
Currently dhcp6.c is the only listener of those events, so by not emitting one event we merely save one function call and a switch(){}. Now if IWD isn't interested in them then ideally it won't register a listener.
(But, didn't we talk on the IWD list about how we need to pass those events to a "netconfig-committer" entity and let it apply the settings?)
Quoted 3 lines
In case of 2, the client just wants a snapshot of what the settings should be, not get notified that each individual route has changed.
Right, but we do need most of the events in this case. If we're not sending original lifetimes over D-Bus then naturally we wouldn't send lifetime changes over D-Bus either but that's for netconfig-committer to determine. We should maybe consider skipping the CHANGED events when it's only the lifetime changing. For other changes like MTU and preference we probably do need to handle the event and send a new snapshot of the routing information.
Quoted 8 lines
Or do you have something else in mind?
Quoted 5 lines
Quoted 4 lines
This is another part that seriously concerns me right now. I actually wonder why we would need to use timeouts at all? If we set the lifetime into the kernel, then it can take care of removing these routes (assuming it doesn't just leave them in there with lifetime 0).
Quoted 7 lines
Quoted 4 lines
So when we receive a new RA we need to know whether we already have this router/prefix to know how to process it. For a lot of things we can instead save an expiration timestamp and next time we look at the data we ignore it if it's expired. But would couldn't emit events.
I suppose we do need at least a single timer for case 2 above. But having a timer for each route seems excessive.
Are you ok we me converting this new code to use a single timer as a separate optimisation later, like I mentioned?
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
One thing to keep in mind is that for us, the 99% use case is a single router that keeps sending the same RA message every several seconds. The net effect is that the same route info keeps getting re-uploaded to the kernel every few seconds. I think this is pointless and we need to optimize that.
Quoted 5 lines
Quoted 2 lines
What we could do is not set lifetimes on the RTNL routes and delete them ourselves, then we wouldn't have to update their lifetimes.
Yes, we could do that since you're not passing lifetimes in the proposed iwd API. But is that intended or an oversight?
To be honest I didn't realise that finite lifetimes are actually popular (judging from the docs I read so far...), in fact only the prefix options support infinite lifetimes, default router info doesn't. So wrt the D-Bus API it seems that not passing the lifetimes and lifetime updates will be much more efficient. As for applying them through RTNL we can also take care of cleaning up the routes ourselves and not set the lifetimes (either we don't call l_rtnl_route_set_lifetime at all, or we just don't pass the value to the kernel in rtnl.c)
Best regards
Denis Kenzior
Re: [PATCH 4/5] icmp6: Track routers and prefix routes from multiple RAs
Hi Andrew,
Quoted 4 lines
(But, didn't we talk on the IWD list about how we need to pass those events to a "netconfig-committer" entity and let it apply the settings?)
That's why I'm asking. It seems there is an implicit assumption that we want to pass all the information from l_icmp6_client up to iwd and have it commit the
routes. But given the complexity of doing this, I'm actually not entirely sure
it is worth the effort.
Quoted 5 lines
Quoted 3 lines
In case of 2, the client just wants a snapshot of what the settings should be, not get notified that each individual route has changed.
Right, but we do need most of the events in this case. If we're not
I'm still not following. Wouldn't l_icmp6_client_get_full_info() be what you want? And have the client call it after every L_ICMP6_CLIENT_EVENT_ROUTER_FOUND?
Quoted 6 lines
Quoted 2 lines
I suppose we do need at least a single timer for case 2 above. But having a timer for each route seems excessive.
Are you ok we me converting this new code to use a single timer as a separate optimisation later, like I mentioned?
Sure.
Quoted 8 lines
Quoted 2 lines
Yes, we could do that since you're not passing lifetimes in the proposed iwd API. But is that intended or an oversight?
To be honest I didn't realise that finite lifetimes are actually popular (judging from the docs I read so far...), in fact only the prefix options support infinite lifetimes, default router info doesn't. So wrt the D-Bus API it seems that not passing the lifetimes and lifetime updates will be much more efficient. As for applying
Question is, are you sure that this is what NM guys would be ok with?
Quoted 5 lines
them through RTNL we can also take care of cleaning up the routes ourselves and not set the lifetimes (either we don't call
l_rtnl_route_set_lifetimeat all, or we just don't pass the value to the kernel inrtnl.c)
I think we need to find out if we can update just the lifetime first. But in general I would tend to lean in the direction that we should be setting the lifetime. The reason we don't do this now is just for expediency, not because it is the 'correct' thing to do.
Regards,
-Denis
Andrew Zaborowski
Re: [PATCH 4/5] icmp6: Track routers and prefix routes from multiple RAs
Quoted 9 lines · Denis Kenzior
Quoted 4 lines
(But, didn't we talk on the IWD list about how we need to pass those events to a "netconfig-committer" entity and let it apply the settings?)
That's why I'm asking. It seems there is an implicit assumption that we want to pass all the information from
l_icmp6_clientup to iwd and have it commit the
routes. But given the complexity of doing this, I'm actually not entirely sure
it is worth the effort.
There's also going to be complexity in receiving this information on the NM side and passing it further on, but I'm not sure what is the alternative.
Once we have a D-Bus backend for this, the cost of doing the other (non-DBus) backend in netconfig-committer.c is small.
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
In case of 2, the client just wants a snapshot of what the settings should be, not get notified that each individual route has changed.
Right, but we do need most of the events in this case. If we're not
I'm still not following. Wouldn't
l_icmp6_client_get_full_info()be what you want? And have the client call it after everyL_ICMP6_CLIENT_EVENT_ROUTER_FOUND?
We will need something like l_icmp6_client_get_full_info(), but we still need to react to all the other events: a new prefix being added or removed, a router MTU changing, a router declaring that it no longer wants to be a default router, etc.
Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
Yes, we could do that since you're not passing lifetimes in the proposed iwd API. But is that intended or an oversight?
To be honest I didn't realise that finite lifetimes are actually popular (judging from the docs I read so far...), in fact only the prefix options support infinite lifetimes, default router info doesn't. So wrt the D-Bus API it seems that not passing the lifetimes and lifetime updates will be much more efficient. As for applying
Question is, are you sure that this is what NM guys would be ok with?
I still need an answer on this, but it shouldn't affect ell as long as it has a complete-ish icmp6 API.
Best regards
Andrew Zaborowski
PATCH 5/5 icmp6: Set limits on route list sizes
In reply to Andrew ZaborowskiIt seems to be easy for someone to DOS the ICMP6 stacks on a local network by sending a lot of RAs.
--- ell/icmp6.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
ell/icmp6.c +30 -0
@@ -410,6 +410,8 @@ static void icmp6_router_update(struct l_icmp6_client *client,icmp6_client_event_notify (client, event, r); } +#define ICMP6_MAX_ROUTES 50 +/* Mostly RFC4861 Section 6.3.4 */ static void icmp6_onlink_prefix_update(struct l_icmp6_client *client, struct l_icmp6_router *ra,
@@ -494,6 +496,14 @@ static void icmp6_onlink_prefix_update(struct l_icmp6_client *client, info->client = client;l_queue_push_head (client->prefix_routes, info); +/* Prevent the queue from growing too long (possible DOS) */ +if (l_queue_length (client->prefix_routes) >ICMP6_MAX_ROUTES ) { +CLIENT_DEBUG ("Too many prefix routes, removing oldest" ); + +icmp6_prefix_timeout (NULL , +l_queue_peek_tail (client->prefix_routes)); + } +if (!changed)return ;
@@ -552,6 +562,26 @@ static int icmp6_client_handle_message(struct l_icmp6_client *client, r->client = client;l_queue_push_head (client->routers, r); +/* Prevent the queue from growing too long (possible DOS) */ +if (l_queue_length (client->routers) >ICMP6_MAX_ROUTES ) { +struct l_icmp6_router *r2 = +l_queue_peek_tail (client->routers); +enum l_icmp6_client_event event = +L_ICMP6_CLIENT_EVENT_DEFAULT_ROUTER_REMOVED ; + +CLIENT_DEBUG ("Too many routers, removing oldest" ); +l_queue_remove (client->routers, r2); + +if (r2->default_route) +icmp6_client_event_notify (client, event, r2); + +_icmp6_router_free (r2); + +/* Client may have stopped us */ +if (!client->io) +return -ECANCELED ; + } +icmp6_client_event_notify (client,L_ICMP6_CLIENT_EVENT_ROUTER_FOUND ,NULL );
Signature · 1 line
2.32.0
Denis Kenzior
AppliedHi Andrew,
Quoted 12 lines · Andrew Zaborowski
Allow registering multiple event handlers and rename
l_icmp6_client_set_event_handlertol_icmp6_client_add_event_handleraccordingly. The use case is for a client like IWD to be able to track routing information.This also fixes a bug where the destroy callback passed to
l_icmp6_client_set_event_handlerwasn't actually used except ifl_icmp6_client_set_event_handlerwas called again.While there, add an
event_dataparameter to the event callback, rename the "userdata" parameters to "user_data" for consistency and clean up an extra empty line.
Quoted 7 lines
--- ell/dhcp6.c | 4 ++-- ell/ell.sym | 2 +- ell/icmp6.c | 56 +++++++++++++++++++++++++++++++++++++++-------------- ell/icmp6.h | 8 ++++---- 4 files changed, 48 insertions(+), 22 deletions(-)
Applied, thanks.
Regards,
-Denis