dhcp6-lease: Make getters argument const, cleanup
Andrew Zaborowski
PATCH 1/8 dhcp6-lease: Make getters argument const, cleanup
Add "const" to the lifetime getters argument, drop dhcp6-private.h declarations I probably added/renamed by mistake.
--- ell/dhcp6-lease.c | 13 +++++++------ ell/dhcp6-private.h | 7 ++----- ell/dhcp6.h | 7 ++++--- 3 files changed, 13 insertions(+), 14 deletions(-)
ell/dhcp6-lease.c +7 -6
@@ -373,7 +373,7 @@ LIB_EXPORT uint8_t l_dhcp6_lease_get_prefix_length( }#define PICK_IA () \ - struct dhcp6_ia *ia; \ + const struct dhcp6_ia *ia; \ \ if (lease->have_na) \ ia = &lease->ia_na; \
@@ -382,7 +382,7 @@ LIB_EXPORT uint8_t l_dhcp6_lease_get_prefix_length( else \ return 0 \ -uint32_t _dhcp6_lease_get_t1 (struct l_dhcp6_lease *lease) +uint32_t _dhcp6_lease_get_t1 (const struct l_dhcp6_lease *lease) {PICK_IA ();
@@ -395,7 +395,7 @@ uint32_t _dhcp6_lease_get_t1(struct l_dhcp6_lease *lease)return ia->info.valid_lifetime /2 ; } -uint32_t _dhcp6_lease_get_t2 (struct l_dhcp6_lease *lease) +uint32_t _dhcp6_lease_get_t2 (const struct l_dhcp6_lease *lease) {PICK_IA ();
@@ -409,7 +409,7 @@ uint32_t _dhcp6_lease_get_t2(struct l_dhcp6_lease *lease) } LIB_EXPORT uint32_t l_dhcp6_lease_get_valid_lifetime( -struct l_dhcp6_lease *lease) +const struct l_dhcp6_lease *lease) {if (unlikely (!lease))return 0 ;
@@ -421,7 +421,7 @@ LIB_EXPORT uint32_t l_dhcp6_lease_get_valid_lifetime( } LIB_EXPORT uint32_t l_dhcp6_lease_get_preferred_lifetime( -struct l_dhcp6_lease *lease) +const struct l_dhcp6_lease *lease) {if (unlikely (!lease))return 0 ;
@@ -433,7 +433,8 @@ LIB_EXPORT uint32_t l_dhcp6_lease_get_preferred_lifetime( }/* Get the reception timestamp, i.e. when lifetimes are counted from */ -LIB_EXPORT uint64_t l_dhcp6_lease_get_start_time(struct l_dhcp6_lease *lease) +LIB_EXPORT uint64_t l_dhcp6_lease_get_start_time( +const struct l_dhcp6_lease *lease) {if (unlikely (!lease))return 0 ;
ell/dhcp6-private.h +2 -5
@@ -148,8 +148,5 @@ void _dhcp6_lease_free(struct l_dhcp6_lease *lease);struct l_dhcp6_lease *_dhcp6_lease_parse_options (struct dhcp6_option_iter *iter,const uint8_t expected_iaid[static 4 ]); -uint32_t _dhcp6_lease_get_t1 (struct l_dhcp6_lease *lease); -uint32_t _dhcp6_lease_get_t2 (struct l_dhcp6_lease *lease); -uint32_t dhcp6_lease_get_valid_lifetime (struct l_dhcp6_lease *lease); -uint32_t dhcp6_lease_get_preferred_lifetime (struct l_dhcp6_lease *lease); -uint32_t dhcp6_lease_get_start_time (struct l_dhcp6_lease *lease); +uint32_t _dhcp6_lease_get_t1 (const struct l_dhcp6_lease *lease); +uint32_t _dhcp6_lease_get_t2 (const struct l_dhcp6_lease *lease);
ell/dhcp6.h +4 -3
@@ -94,9 +94,10 @@ char *l_dhcp6_lease_get_address(const struct l_dhcp6_lease *lease);char **l_dhcp6_lease_get_dns (const struct l_dhcp6_lease *lease);char **l_dhcp6_lease_get_domains (const struct l_dhcp6_lease *lease);uint8_t l_dhcp6_lease_get_prefix_length (const struct l_dhcp6_lease *lease); -uint32_t l_dhcp6_lease_get_valid_lifetime (struct l_dhcp6_lease *lease); -uint32_t l_dhcp6_lease_get_preferred_lifetime (struct l_dhcp6_lease *lease); -uint64_t l_dhcp6_lease_get_start_time (struct l_dhcp6_lease *lease); +uint32_t l_dhcp6_lease_get_valid_lifetime (const struct l_dhcp6_lease *lease); +uint32_t l_dhcp6_lease_get_preferred_lifetime ( +const struct l_dhcp6_lease *lease); +uint64_t l_dhcp6_lease_get_start_time (const struct l_dhcp6_lease *lease); #ifdef __cplusplus }
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 2/8 util: Use unlikely() for L_WARN_ON() condition
Any condition used in L_WARN_ON() should be only true in an error or
trouble situation and thus be unlikely true. Add unlikely() directly
in the macro to avoid repeating use of L_WARN_ON(unlikely(condition))
--- ell/util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
ell/util.h +1 -1
@@ -49,7 +49,7 @@ _Pragma("GCC diagnostic pop") \#define L_WARN_ON (condition) __extension__ ({ \ bool r = !!(condition); \ - if (r) \ + if (unlikely(r)) \ l_warn("WARNING: %s:%s() condition %s failed", \ __FILE__, __func__, \ #condition); \
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 3/8 netconfig: Avoid returning internal l_queues
In reply to Andrew ZaborowskiReturn const struct l_queue_entry pointers instead.
--- ell/netconfig.c | 35 ++++++++++++++++++----------------- ell/netconfig.h | 20 +++++++++++--------- 2 files changed, 29 insertions(+), 26 deletions(-)
ell/netconfig.c +18 -17
@@ -1134,37 +1134,38 @@ LIB_EXPORT void l_netconfig_apply_rtnl(struct l_netconfig *netconfig, NULL, NULL, NULL); } -LIB_EXPORT struct l_queue *l_netconfig_get_addresses( -struct l_netconfig *netconfig, -struct l_queue **out_added, -struct l_queue **out_updated, -struct l_queue **out_removed) +LIB_EXPORT const struct l_queue_entry *l_netconfig_get_addresses( +struct l_netconfig *netconfig, +const struct l_queue_entry **out_added, +const struct l_queue_entry **out_updated, +const struct l_queue_entry **out_removed) {if (out_added) - *out_added = netconfig->addresses.added; + *out_added =l_queue_get_entries (netconfig->addresses.added);if (out_updated) - *out_updated = netconfig->addresses.updated; + *out_updated =l_queue_get_entries (netconfig->addresses.updated);if (out_removed) - *out_removed = netconfig->addresses.removed; + *out_removed =l_queue_get_entries (netconfig->addresses.removed); -return netconfig->addresses.current; +return l_queue_get_entries (netconfig->addresses.current); } -LIB_EXPORT struct l_queue *l_netconfig_get_routes(struct l_netconfig *netconfig, -struct l_queue **out_added, -struct l_queue **out_updated, -struct l_queue **out_removed) +LIB_EXPORT const struct l_queue_entry *l_netconfig_get_routes( +struct l_netconfig *netconfig, +const struct l_queue_entry **out_added, +const struct l_queue_entry **out_updated, +const struct l_queue_entry **out_removed) {if (out_added) - *out_added = netconfig->routes.added; + *out_added =l_queue_get_entries (netconfig->routes.added);if (out_updated) - *out_updated = netconfig->routes.updated; + *out_updated =l_queue_get_entries (netconfig->routes.updated);if (out_removed) - *out_removed = netconfig->routes.removed; + *out_removed =l_queue_get_entries (netconfig->routes.removed); -return netconfig->routes.current; +return l_queue_get_entries (netconfig->routes.current); }
ell/netconfig.h +11 -9
@@ -33,7 +33,7 @@ struct l_dhcp_client;struct l_dhcp6_client ;struct l_icmp6_client ;struct l_netlink ; -struct l_queue ; +struct l_queue_entry ;struct l_netconfig ;struct l_rtnl_address ;struct l_rtnl_route ;
@@ -87,14 +87,16 @@ void l_netconfig_set_event_handler(struct l_netconfig *netconfig,void l_netconfig_apply_rtnl (struct l_netconfig *netconfig,struct l_netlink *rtnl); -struct l_queue *l_netconfig_get_addresses (struct l_netconfig *netconfig, -struct l_queue **out_added, -struct l_queue **out_updated, -struct l_queue **out_removed); -struct l_queue *l_netconfig_get_routes (struct l_netconfig *netconfig, -struct l_queue **out_added, -struct l_queue **out_updated, -struct l_queue **out_removed); +const struct l_queue_entry *l_netconfig_get_addresses ( +struct l_netconfig *netconfig, +const struct l_queue_entry **out_added, +const struct l_queue_entry **out_updated, +const struct l_queue_entry **out_removed); +const struct l_queue_entry *l_netconfig_get_routes ( +struct l_netconfig *netconfig, +const struct l_queue_entry **out_added, +const struct l_queue_entry **out_updated, +const struct l_queue_entry **out_removed); #ifdef __cplusplus }
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 4/8 netconfig: In netconfig_find_icmp6_route check protocol is RTPROT_RA
In reply to Andrew ZaborowskiSince we're interested only in routes from icmp6 (Router Advertisements) we can check that the route's protocol is RTPROT_RA before following with other checks.
---
I'll merge this into the original ICMP6 handling patch if/when I'm resending that one.
ell/netconfig.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
ell/netconfig.c +2 -1
@@ -371,7 +371,8 @@ static struct l_rtnl_route *netconfig_find_icmp6_route(const uint8_t *route_dst;uint8_t route_prefix_len =0 ; -if (l_rtnl_route_get_family (route) !=AF_INET6 ) +if (l_rtnl_route_get_family (route) !=AF_INET6 || +l_rtnl_route_get_protocol (route) !=RTPROT_RA )continue ; route_gateway =l_rtnl_route_get_gateway_in_addr (route);
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 5/8 netconfig: Handle DHCPv6 events
In reply to Andrew ZaborowskiUse l_dhcp6_client to support IPv6 address assignment through DHCPv6.
--- ell/netconfig.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+)
ell/netconfig.c +133 -0
@@ -357,6 +357,136 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client, } } +static void netconfig_add_dhcp6_address (struct l_netconfig *nc) +{ +const struct l_dhcp6_lease *lease = +l_dhcp6_client_get_lease (nc->dhcp6_client); + _auto_(l_free) char *ip = NULL; +uint32_t prefix_len; + +if (L_WARN_ON (!lease)) +return ; + + ip =l_dhcp6_lease_get_address (lease); + prefix_len =l_dhcp6_lease_get_prefix_length (lease); + nc->v6_address =l_rtnl_address_new (ip, prefix_len); + +if (L_WARN_ON (!nc->v6_address)) +return ; + +/* +* TODO: check if we have a prefix route covering the IP, +* l_dhcp6_client doesn't guarantee that the received IP is in +* on of the subnets marked as on-link in the RA. If it isn't, +* maybe warn and set the below to false. Or create a route using +* the prefix length from lease, set RTPROT_DHCP to distinguish it +* from other routes. +*/ +l_rtnl_address_set_noprefixroute (nc->v6_address, true); + +l_queue_push_tail (nc->addresses.current, nc->v6_address); +l_queue_push_tail (nc->addresses.added, nc->v6_address); +} + +static void netconfig_set_dhcp6_address_lifetimes (struct l_netconfig *nc, +bool updated) +{ +const struct l_dhcp6_lease *lease = +l_dhcp6_client_get_lease (nc->dhcp6_client); +uint32_t p, v; +uint64_t start_time; + +if (L_WARN_ON (!lease)) +return ; + + p =l_dhcp6_lease_get_preferred_lifetime (lease); + v =l_dhcp6_lease_get_valid_lifetime (lease); + start_time =l_dhcp6_lease_get_start_time (lease); + +l_rtnl_address_set_lifetimes (nc->v6_address, p, v); +l_rtnl_address_set_expiry (nc->v6_address, + start_time + p *L_USEC_PER_SEC , + start_time + v *L_USEC_PER_SEC ); + +if (updated) +l_queue_push_tail (nc->addresses.updated, nc->v6_address); +} + +static void netconfig_remove_dhcp6_address (struct l_netconfig *nc) +{ +l_queue_remove (nc->addresses.current, nc->v6_address); +l_queue_push_tail (nc->addresses.removed, nc->v6_address); + nc->v6_address =NULL ; +} + +static void netconfig_dhcp6_event_handler (struct l_dhcp6_client *client, +enum l_dhcp6_client_event event, +void *user_data) +{ +struct l_netconfig *nc = user_data; + +switch (event) { +case L_DHCP6_CLIENT_EVENT_LEASE_OBTAINED : +if (L_WARN_ON (nc->v6_configured)) +break ; + +netconfig_add_dhcp6_address (nc); +netconfig_set_dhcp6_address_lifetimes (nc, false); + nc->v6_configured = true; +netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_CONFIGURE ); +break ; +case L_DHCP6_CLIENT_EVENT_IP_CHANGED : +if (L_WARN_ON (!nc->v6_configured)) +break ; + +netconfig_remove_dhcp6_address (nc); +netconfig_add_dhcp6_address (nc); +netconfig_set_dhcp6_address_lifetimes (nc, false); +netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_UPDATE ); +break ; +case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED : +if (L_WARN_ON (!nc->v6_configured)) +break ; + +netconfig_remove_dhcp6_address (nc); + nc->v6_configured = false; + +if (l_dhcp6_client_start (nc->dhcp6_client)) +/* TODO: also start a new timeout */ +netconfig_emit_event (nc,AF_INET6 , +L_NETCONFIG_EVENT_UNCONFIGURE ); +else +netconfig_emit_event (nc,AF_INET6 , +L_NETCONFIG_EVENT_FAILED ); + +break ; +case L_DHCP6_CLIENT_EVENT_LEASE_RENEWED : +if (L_WARN_ON (!nc->v6_configured)) +break ; + +netconfig_set_dhcp6_address_lifetimes (nc, true); +netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_UPDATE ); +break ; +case L_DHCP6_CLIENT_EVENT_NO_LEASE : +if (L_WARN_ON (nc->v6_configured)) +break ; + +/* +* The requested address is no longer available, try to restart +* the client. +* +* TODO: this may need to be delayed so we don't flood the +* network with SOLICITs and DECLINEs. Also add a retry limit +* or better yet a configurable timeout. +*/ +if (!l_dhcp6_client_start (nc->dhcp6_client)) +netconfig_emit_event (nc,AF_INET6 , +L_NETCONFIG_EVENT_FAILED ); + +break ; + } +} + static struct l_rtnl_route *netconfig_find_icmp6_route( struct l_netconfig *nc, const uint8_t *gateway,
@@ -563,6 +693,9 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) nc, NULL); nc->dhcp6_client =l_dhcp6_client_new (ifindex); +l_dhcp6_client_set_event_handler (nc->dhcp6_client, + netconfig_dhcp6_event_handler, + nc,NULL ); nc->icmp6_client =l_dhcp6_client_get_icmp6 (nc->dhcp6_client); l_icmp6_client_add_event_handler(nc->icmp6_client,
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 6/8 netconfig: Add getters for DNS addresses and domain names
In reply to Andrew Zaborowski--- ell/ell.sym | 2 ++ ell/netconfig.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ ell/netconfig.h | 2 ++ 3 files changed, 68 insertions(+)
ell/ell.sym +2 -0
@@ -755,6 +755,8 @@ global: l_netconfig_apply_rtnl; l_netconfig_get_addresses; l_netconfig_get_routes; + l_netconfig_get_dns_list; + l_netconfig_get_domain_names; local: *; };
ell/netconfig.c +64 -0
67 lines not on this page · raw message
ell/netconfig.h +2 -0
@@ -97,6 +97,8 @@ const struct l_queue_entry *l_netconfig_get_routes(const struct l_queue_entry **out_added, const struct l_queue_entry **out_updated, const struct l_queue_entry **out_removed); +char **l_netconfig_get_dns_list (struct l_netconfig *netconfig); +char **l_netconfig_get_domain_names (struct l_netconfig *netconfig); #ifdef __cplusplus }
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 7/8 netconfig: Expire addresses and routes
In reply to Andrew ZaborowskiStart differentiating between routes/addresses being removed and those expiring. Generally if we're committing the routes through RTNL, the lifetimes are submitted to the kernel and the kernel should expire them for us on the kernel side. In that case there's no need to send an RTM_DELROUTE/RTM_DELADDR. That should apply to routes received in Router Advertisements as well as addresses and routes from expiring DHCP leases and DHCPv6 leases.
For routes/addresses being removed because a router is going offline and signals that by setting the lifetime to 0 in an RA, or as a result of L_DHCP_CLIENT_EVENT_IP_CHANGED and similar, us or the user do need to send RTM_DELROUTE/RTM_DELADDR. To differentiate between the two cases (removal and expiry) pass two separate lists to the user.
Add checks for expired IPv6 routes in the ICMPv6 event handler to avoid using timers for that. We have no bound on the number of routes (yet) so we avoid keeping potentially many timers or keeping one timer tracking multiple expiry times which would add a little complexity (but might be acceptable.)
--- ell/netconfig.c | 160 ++++++++++++++++++++++++++++++++++++++++++------ ell/netconfig.h | 6 +- 2 files changed, 145 insertions(+), 21 deletions(-)
ell/netconfig.c +141 -19
@@ -69,6 +69,7 @@ struct l_netconfig {bool v6_configured;struct l_icmp6_client *icmp6_client;struct l_dhcp6_client *dhcp6_client; +struct l_idle *signal_expired_work;/* These objects, if not NULL, are owned by @addresses and @routes */ struct l_rtnl_address *v4_address;
@@ -89,10 +90,14 @@ struct l_netconfig {* for example those that only have their lifetime updated. * * Any entries in @added and @updated are owned by @current. +* Entries in @removed need to be removed with an +* RTM_DELADD/RTM_DELROUTE while those in @expired are only +* informative as the kernel will have removed them already. */ struct l_queue *added;struct l_queue *updated;struct l_queue *removed; +struct l_queue *expired; } addresses, routes;struct {
@@ -108,10 +113,14 @@ static void netconfig_update_cleanup(struct l_netconfig *nc)l_queue_clear (nc->addresses.updated,NULL );l_queue_clear (nc->addresses.removed, (l_queue_destroy_func_t ) l_rtnl_address_free); +l_queue_clear (nc->addresses.expired, + (l_queue_destroy_func_t ) l_rtnl_address_free);l_queue_clear (nc->routes.added,NULL );l_queue_clear (nc->routes.updated,NULL );l_queue_clear (nc->routes.removed, (l_queue_destroy_func_t ) l_rtnl_route_free); +l_queue_clear (nc->routes.expired, + (l_queue_destroy_func_t ) l_rtnl_route_free); } static void netconfig_emit_event(struct l_netconfig *nc, uint8_t family,
@@ -128,6 +137,24 @@ static void netconfig_emit_event(struct l_netconfig *nc, uint8_t family,netconfig_update_cleanup (nc); } +static void netconfig_signal_expired (struct l_idle *idle,void *user_data) +{ +struct l_netconfig *nc = user_data; + +l_idle_remove (l_steal_ptr (nc->signal_expired_work)); + +/* +* If the idle work was scheduled from within l_netconfig_get_routes +* or netconfig_icmp6_event_handler, the user is likely to have +* already received an event and had a chance to process the expired +* routes list. In that case there's no need to emit a new event, +* and the list will have been emptied in netconfig_update_cleanup() +* anyway. +*/ +if (!l_queue_isempty (nc->routes.expired)) +netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_UPDATE ); +} +static void netconfig_add_v4_routes (struct l_netconfig *nc,const char *ip,uint8_t prefix_len,const char *gateway,uint8_t rtm_protocol)
@@ -211,6 +238,11 @@ static void netconfig_add_v6_static_routes(struct l_netconfig *nc,l_queue_push_tail (nc->routes.added, v6_default_route); } +static bool netconfig_match_ptr (const void *a,const void *b) +{ +return a == b; +} +static void netconfig_add_dhcp_address_routes (struct l_netconfig *nc) { const struct l_dhcp_lease *lease =
@@ -254,13 +286,15 @@ static void netconfig_set_dhcp_lifetimes(struct l_netconfig *nc, bool updated)l_rtnl_address_set_lifetimes (nc->v4_address,0 , lifetime);l_rtnl_address_set_expiry (nc->v4_address,0 , expiry); -if (updated) +if (updated && !l_queue_find (nc->addresses.added, netconfig_match_ptr, + nc->v4_address))l_queue_push_tail (nc->addresses.updated, nc->v4_address);l_rtnl_route_set_lifetime (nc->v4_subnet_route, lifetime);l_rtnl_route_set_expiry (nc->v4_subnet_route, expiry); -if (updated) +if (updated && !l_queue_find (nc->routes.added, netconfig_match_ptr, + nc->v4_subnet_route))l_queue_push_tail (nc->routes.updated, nc->v4_subnet_route);if (!nc->v4_default_route)
@@ -269,23 +303,42 @@ static void netconfig_set_dhcp_lifetimes(struct l_netconfig *nc, bool updated)l_rtnl_route_set_lifetime (nc->v4_default_route, lifetime);l_rtnl_route_set_expiry (nc->v4_default_route, expiry); -if (updated) +if (updated && !l_queue_find (nc->routes.added, netconfig_match_ptr, + nc->v4_default_route))l_queue_push_tail (nc->routes.updated, nc->v4_default_route); } -static void netconfig_remove_dhcp_address_routes (struct l_netconfig *nc) +static void netconfig_remove_dhcp_address_routes (struct l_netconfig *nc, +bool expired) { +struct l_queue *routes = + expired ? nc->routes.expired : nc->routes.removed; +l_queue_remove (nc->addresses.current, nc->v4_address); -l_queue_push_tail (nc->addresses.removed, nc->v4_address); +l_queue_remove (nc->addresses.updated, nc->v4_address); + +if (!l_queue_remove (nc->addresses.added, nc->v4_address)) +l_queue_push_tail ( + expired ? nc->addresses.expired : nc->addresses.removed, + nc->v4_address); + nc->v4_address =NULL ;l_queue_remove (nc->routes.current, nc->v4_subnet_route); -l_queue_push_tail (nc->routes.removed, nc->v4_subnet_route); +l_queue_remove (nc->routes.updated, nc->v4_subnet_route); + +if (!l_queue_remove (nc->routes.added, nc->v4_subnet_route)) +l_queue_push_tail (routes, nc->v4_subnet_route); + nc->v4_subnet_route =NULL ;if (nc->v4_default_route) {l_queue_remove (nc->routes.current, nc->v4_default_route); -l_queue_push_tail (nc->routes.removed, nc->v4_default_route); +l_queue_remove (nc->routes.updated, nc->v4_default_route); + +if (!l_queue_remove (nc->routes.added, nc->v4_default_route)) +l_queue_push_tail (routes, nc->v4_default_route); + nc->v4_default_route =NULL ; } }
@@ -301,7 +354,7 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,if (L_WARN_ON (!nc->v4_configured))break ; -netconfig_remove_dhcp_address_routes (nc); +netconfig_remove_dhcp_address_routes (nc, false);netconfig_add_dhcp_address_routes (nc);netconfig_set_dhcp_lifetimes (nc, false);netconfig_emit_event (nc,AF_INET ,L_NETCONFIG_EVENT_UPDATE );
@@ -326,7 +379,7 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,if (L_WARN_ON (!nc->v4_configured))break ; -netconfig_remove_dhcp_address_routes (nc); +netconfig_remove_dhcp_address_routes (nc, true); nc->v4_configured = false;if (l_dhcp_client_start (nc->dhcp_client))
@@ -408,14 +461,21 @@ static void netconfig_set_dhcp6_address_lifetimes(struct l_netconfig *nc, start_time + p *L_USEC_PER_SEC , start_time + v * L_USEC_PER_SEC); -if (updated) +if (updated && !l_queue_find (nc->addresses.added, netconfig_match_ptr, + nc->v6_address))l_queue_push_tail (nc->addresses.updated, nc->v6_address); } -static void netconfig_remove_dhcp6_address (struct l_netconfig *nc) +static void netconfig_remove_dhcp6_address (struct l_netconfig *nc,bool expired) {l_queue_remove (nc->addresses.current, nc->v6_address); -l_queue_push_tail (nc->addresses.removed, nc->v6_address); +l_queue_remove (nc->addresses.updated, nc->v6_address); + +if (!l_queue_remove (nc->addresses.added, nc->v6_address)) +l_queue_push_tail ( + expired ? nc->addresses.expired : nc->addresses.removed, + nc->v6_address); + nc->v6_address =NULL ; }
@@ -439,7 +499,7 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,if (L_WARN_ON (!nc->v6_configured))break ; -netconfig_remove_dhcp6_address (nc); +netconfig_remove_dhcp6_address (nc, false);netconfig_add_dhcp6_address (nc);netconfig_set_dhcp6_address_lifetimes (nc, false);netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_UPDATE );
@@ -448,7 +508,7 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,if (L_WARN_ON (!nc->v6_configured))break ; -netconfig_remove_dhcp6_address (nc); +netconfig_remove_dhcp6_address (nc, true); nc->v6_configured = false;if (l_dhcp6_client_start (nc->dhcp6_client))
@@ -487,6 +547,49 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client, } } +static uint64_t now; + +static bool netconfig_check_route_expired (void *data,void *user_data) +{ +struct l_netconfig *nc = user_data; +struct l_rtnl_route *route = data; +uint64_t expiry_time =l_rtnl_route_get_expiry (route); + +if (!expiry_time || now < expiry_time) +return false; + +/* +* Since we set lifetimes on the routes we submit to the kernel with +* RTM_NEWROUTE, we count on them being deleted automatically so no +* need to send an RTM_DELROUTE. We signal the fact that the route +* expired to the user by having it on the expired list but there's +* nothing that the user needs to do with the routes on that list +* like they do with the added, updated and removed lists. +* +* If for some reason the route is still on the added list, drop it +* from there and there's nothing to notify the user of. +*/ +if (!l_queue_remove (nc->routes.added, route)) +l_queue_push_tail (nc->routes.expired, route); + +l_queue_remove (nc->routes.updated, route); +l_queue_remove (nc->routes.removed, route); +return true; +} + +static void netconfig_expire_routes (struct l_netconfig *nc) +{ + now =l_time_now (); + +if (l_queue_foreach_remove (nc->routes.current, + netconfig_check_route_expired, nc) && + !l_queue_isempty (nc->routes.expired) && + !nc->signal_expired_work) + nc->signal_expired_work =l_idle_create ( + netconfig_signal_expired, + nc,NULL ); +} + static struct l_rtnl_route *netconfig_find_icmp6_route( struct l_netconfig *nc, const uint8_t *gateway,
@@ -574,7 +677,7 @@ static void netconfig_set_icmp6_route_data(struct l_netconfig *nc,l_rtnl_route_set_lifetime (rt, valid_lifetime);l_rtnl_route_set_expiry (rt, expiry); -if (updated) +if (updated && !l_queue_find (nc->routes.added, netconfig_match_ptr, rt))l_queue_push_tail (nc->routes.updated, rt); }
@@ -582,7 +685,10 @@ static void netconfig_remove_icmp6_route(struct l_netconfig *nc, struct l_rtnl_route *route) {l_queue_remove (nc->routes.current, route); -l_queue_push_tail (nc->routes.removed, route); +l_queue_remove (nc->routes.updated, route); + +if (!l_queue_remove (nc->routes.added, route)) +l_queue_push_tail (nc->routes.removed, route); } static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
@@ -609,6 +715,8 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,if (nc->v4_gateway_override)return ; +netconfig_expire_routes (nc); +/* Process the default gateway information */ default_route =netconfig_find_icmp6_route (nc, r->address,NULL );
@@ -666,7 +774,8 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,*/ if (!l_queue_isempty (nc->routes.added) || !l_queue_isempty (nc->routes.updated) || - !l_queue_isempty (nc->routes.removed)) + !l_queue_isempty (nc->routes.removed) || + !l_queue_isempty (nc->routes.expired))netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_UPDATE ); }
@@ -1173,6 +1282,9 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig)if (netconfig->do_static_work)l_idle_remove (l_steal_ptr (netconfig->do_static_work)); +if (netconfig->signal_expired_work) +l_idle_remove (l_steal_ptr (netconfig->signal_expired_work)); +netconfig_update_cleanup (netconfig);l_queue_clear (netconfig->routes.current, (l_queue_destroy_func_t ) l_rtnl_route_free);
@@ -1272,7 +1384,8 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_addresses(struct l_netconfig *netconfig, const struct l_queue_entry **out_added, const struct l_queue_entry **out_updated, - const struct l_queue_entry **out_removed) + const struct l_queue_entry **out_removed, + const struct l_queue_entry **out_expired) { if (out_added) *out_added =l_queue_get_entries (netconfig->addresses.added);
@@ -1283,6 +1396,9 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_addresses(if (out_removed) *out_removed =l_queue_get_entries (netconfig->addresses.removed); +if (out_expired) + *out_expired =l_queue_get_entries (netconfig->addresses.expired); +return l_queue_get_entries (netconfig->addresses.current); }
@@ -1290,8 +1406,11 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_routes(struct l_netconfig *netconfig, const struct l_queue_entry **out_added, const struct l_queue_entry **out_updated, - const struct l_queue_entry **out_removed) + const struct l_queue_entry **out_removed, + const struct l_queue_entry **out_expired) { +netconfig_expire_routes (netconfig ); +if (out_added) *out_added =l_queue_get_entries (netconfig->routes.added);
@@ -1301,6 +1420,9 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_routes(if (out_removed) *out_removed =l_queue_get_entries (netconfig->routes.removed); +if (out_expired) + *out_expired =l_queue_get_entries (netconfig->routes.expired); +return l_queue_get_entries (netconfig->routes.current); }
ell/netconfig.h +4 -2
@@ -91,12 +91,14 @@ const struct l_queue_entry *l_netconfig_get_addresses(struct l_netconfig *netconfig, const struct l_queue_entry **out_added, const struct l_queue_entry **out_updated, - const struct l_queue_entry **out_removed); + const struct l_queue_entry **out_removed, + const struct l_queue_entry **out_expired);const struct l_queue_entry *l_netconfig_get_routes (struct l_netconfig *netconfig,const struct l_queue_entry **out_added,const struct l_queue_entry **out_updated, -const struct l_queue_entry **out_removed); +const struct l_queue_entry **out_removed, +const struct l_queue_entry **out_expired);char **l_netconfig_get_dns_list (struct l_netconfig *netconfig);char **l_netconfig_get_domain_names (struct l_netconfig *netconfig);
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 8/8 examples: Update l_netconfig_get_{addresses, routes} calls
In reply to Andrew Zaborowski--- examples/netconfig-test.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-)
examples/netconfig-test.c +19 -17
@@ -61,16 +61,14 @@ static void signal_handler(uint32_t signo, void *user_data) }static void log_addresses (const char *af_str,const char *action, -struct l_queue *list) +const struct l_queue_entry *entry) { -const struct l_queue_entry *entry; - -if (l_queue_isempty (list)) +if (!entry)return ;l_info ("[netconfig%s] Addresses %s:" , af_str, action); -for (entry =l_queue_get_entries (list); entry; entry = entry->next) { +for (; entry; entry = entry->next) {struct l_rtnl_address *addr = entry->data;char ip_str[INET6_ADDRSTRLEN ];
@@ -83,25 +81,26 @@ static void log_addresses(const char *af_str, const char *action, }static void log_routes (const char *af_str,const char *action, -struct l_queue *list) +const struct l_queue_entry *entry) { -const struct l_queue_entry *entry; - -if (l_queue_isempty (list)) +if (!entry)return ;l_info ("[netconfig%s] Routes %s:" , af_str, action); -for (entry =l_queue_get_entries (list); entry; entry = entry->next) { +for (; entry; entry = entry->next) {struct l_rtnl_route *rt = entry->data; -char subnet_str[INET6_ADDRSTRLEN ]; +char subnet_str[INET6_ADDRSTRLEN ] ="unknown" ;char gateway_str[INET6_ADDRSTRLEN ];uint8_t prefix_len; +bool onlink;l_rtnl_route_get_dst (rt, subnet_str, &prefix_len); -l_rtnl_route_get_gateway (rt, gateway_str); -l_info ("[netconfig%s] \t%s/%i, gateway %s, orig lifetime %i s" , - af_str, subnet_str, prefix_len, gateway_str, + onlink = !l_rtnl_route_get_gateway (rt, gateway_str); +l_info ("[netconfig%s] \t%s/%i, %s%s, orig lifetime %i s" , + af_str, subnet_str, prefix_len, + onlink ?"onlink" :"next-hop " , + onlink ?"" : gateway_str,l_rtnl_route_get_lifetime (rt)); } }
@@ -110,7 +109,7 @@ static void event_handler(struct l_netconfig *netconfig, uint8_t family, enum l_netconfig_event event, void *user_data) { const char *af_str = family == AF_INET ? "v4" : "v6"; -struct l_queue *added, *updated, *removed; +const struct l_queue_entry *added, *updated, *removed, *expired;switch (event) {case L_NETCONFIG_EVENT_CONFIGURE :
@@ -128,15 +127,18 @@ static void event_handler(struct l_netconfig *netconfig, uint8_t family,return ; } -l_netconfig_get_addresses (netconfig, &added, &updated, &removed); +l_netconfig_get_addresses (netconfig, &added, &updated, + &removed, &expired);log_addresses (af_str,"added" , added);log_addresses (af_str,"updated" , updated);log_addresses (af_str,"removed" , removed); +log_addresses (af_str,"expired" , expired); -l_netconfig_get_routes (netconfig, &added, &updated, &removed); +l_netconfig_get_routes (netconfig, &added, &updated, &removed, &expired);log_routes (af_str,"added" , added);log_routes (af_str,"updated" , updated);log_routes (af_str,"removed" , removed); +log_routes (af_str,"expired" , expired);if (apply)l_netconfig_apply_rtnl (netconfig, rtnl);
Signature · 1 line
2.32.0
Denis Kenzior
Hi Andrew,
Quoted 8 lines · Andrew Zaborowski
Add "const" to the lifetime getters argument, drop
dhcp6-private.hdeclarations I probably added/renamed by mistake.--- ell/dhcp6-lease.c | 13 +++++++------ ell/dhcp6-private.h | 7 ++----- ell/dhcp6.h | 7 ++++--- 3 files changed, 13 insertions(+), 14 deletions(-)
Patch 1,2 applied, thanks.
Regards,
-Denis