net: Add net_prefix_from_ipv6 utility
Andrew Zaborowski
PATCH 1/17 net: Add net_prefix_from_ipv6 utility
Add function that zeroes bits after prefix_len'th bit in an address. Put it in net-private.h so it can be used by both netconfig.c and icmp6.c, potentially others.
--- ell/net-private.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
ell/net-private.h +18 -0
@@ -22,3 +22,21 @@char *net_domain_name_parse (const uint8_t *raw,size_t raw_len);char **net_domain_list_parse (const uint8_t *raw,size_t raw_len); + +static inline const void *net_prefix_from_ipv6 (const uint8_t *address, +uint8_t prefix_len) +{ +uint8_t last_byte = prefix_len /8 ; +uint8_t bits = prefix_len &7 ; +static uint8_t prefix[16 ]; + +memcpy (prefix, address, last_byte); + +if (prefix_len &7 ) { + prefix[last_byte] = address[last_byte] & (0xff00 >> bits); + last_byte++; + } + +memset (prefix + last_byte,0 ,16 - last_byte); +return prefix; +}
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 2/17 netconfig: Support IPv6 static configurations
--- ell/netconfig.c | 157 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 128 insertions(+), 29 deletions(-)
ell/netconfig.c +128 -29
@@ -40,6 +40,7 @@#include "idle.h" #include "strv.h" #include "net.h" +#include "net-private.h" #include "netconfig.h" struct l_netconfig {
@@ -62,11 +63,13 @@ struct l_netconfig {struct l_idle *do_static_work;bool v4_configured;struct l_dhcp_client *dhcp_client; +bool v6_configured;/* These objects, if not NULL, are owned by @addresses and @routes */ struct l_rtnl_address *v4_address;struct l_rtnl_route *v4_subnet_route;struct l_rtnl_route *v4_default_route; +struct l_rtnl_address *v6_address;struct {struct l_queue *current;
@@ -94,6 +97,11 @@ struct l_netconfig { } handler; }; +union netconfig_addr { +struct in_addr v4; +struct in6_addr v6; +}; +static void netconfig_update_cleanup (struct l_netconfig *nc) {l_queue_clear (nc->addresses.added,NULL );
@@ -159,6 +167,53 @@ static void netconfig_add_v4_routes(struct l_netconfig *nc, const char *ip,l_queue_push_tail (nc->routes.added, nc->v4_default_route); } +static void netconfig_add_v6_static_routes (struct l_netconfig *nc, +const char *ip, +uint8_t prefix_len) +{ +struct in6_addr in6_addr; +const void *prefix; +char network[INET6_ADDRSTRLEN ]; +struct l_rtnl_route *v6_subnet_route; +struct l_rtnl_route *v6_default_route; + +/* Subnet route */ + +if (L_WARN_ON (inet_pton (AF_INET6 , ip, &in6_addr) !=1 )) +return ; + +/* +* Zero out host address bits, aka. interface ID, to produce +* the network address or prefix. +*/ + prefix =net_prefix_from_ipv6 (in6_addr.s6_addr, prefix_len); + +if (L_WARN_ON (!inet_ntop (AF_INET6 , prefix, network,INET6_ADDRSTRLEN ))) +return ; + +/* +* One reason we add a subnet route instead of letting the kernel +* do it, by not specifying IFA_F_NOPREFIXROUTE for the address, +* is that that would force a 0 metric for the route. +*/ + v6_subnet_route =l_rtnl_route_new_prefix (network, prefix_len); +l_rtnl_route_set_protocol (v6_subnet_route,RTPROT_STATIC ); +l_rtnl_route_set_priority (v6_subnet_route, nc->route_priority); +l_queue_push_tail (nc->routes.current, v6_subnet_route); +l_queue_push_tail (nc->routes.added, v6_subnet_route); + +/* Gateway route */ + +if (!nc->v6_gateway_override) +return ; + + v6_default_route =l_rtnl_route_new_gateway (nc->v6_gateway_override); +l_rtnl_route_set_protocol (v6_default_route,RTPROT_STATIC ); +L_WARN_ON (!l_rtnl_route_set_prefsrc (v6_default_route, ip)); +l_queue_push_tail (nc->routes.current, v6_default_route); +l_queue_push_tail (nc->routes.added, v6_default_route); +} +static void netconfig_add_dhcp_address_routes (struct l_netconfig *nc) { const struct l_dhcp_lease *lease =
@@ -388,6 +443,9 @@ LIB_EXPORT bool l_netconfig_set_family_enabled(struct l_netconfig *netconfig,case AF_INET : netconfig->v4_enabled = enabled;return true; +case AF_INET6 : + netconfig->v6_enabled = enabled; +return true; }return false;
@@ -536,43 +594,39 @@ LIB_EXPORT bool l_netconfig_set_domain_names_override(return true; } -static bool netconfig_check_v4_config (struct l_netconfig *netconfig) +static bool netconfig_check_family_config (struct l_netconfig *nc, +uint8_t family) { -struct in_addr local; -struct in_addr gateway; -uint8_t prefix_len =0 ; +struct l_rtnl_address *static_addr = (family ==AF_INET ) ? + nc->v4_static_addr : nc->v6_static_addr; +char *gateway_override = (family ==AF_INET ) ? + nc->v4_gateway_override : nc->v6_gateway_override; +char **dns_override = (family ==AF_INET ) ? + nc->v4_dns_override : nc->v6_dns_override;unsigned int dns_num =0 ; - _auto_(l_free) struct in_addr *dns_list = NULL; - -if (!netconfig->v4_enabled) -return true; -if (netconfig->v4_static_addr) { -char str[INET_ADDRSTRLEN ]; +if (static_addr && family ==AF_INET ) { +uint8_t prefix_len = +l_rtnl_address_get_prefix_length (static_addr); - prefix_len =l_rtnl_address_get_prefix_length ( - netconfig->v4_static_addr);if (prefix_len >30 )return false; - -l_rtnl_address_get_address (netconfig->v4_static_addr, str); -inet_pton (AF_INET , str, &local); } -if (netconfig->v4_gateway_override) { -if (inet_pton (AF_INET , netconfig->v4_gateway_override, - &gateway) !=1 ) +if (gateway_override) { +union netconfig_addr gateway; + +if (inet_pton (family, gateway_override, &gateway) !=1 )return false; } -if (netconfig->v4_dns_override && - (dns_num =l_strv_length (netconfig->v4_dns_override))) { +if (dns_override && (dns_num =l_strv_length (dns_override))) {unsigned int i; - - dns_list = l_new(struct in_addr, dns_num); + _auto_(l_free) union netconfig_addr *dns_list = + l_new(union netconfig_addr, dns_num);for (i =0 ; i < dns_num; i++) -if (inet_pton (AF_INET , netconfig->v4_dns_override[i], +if (inet_pton (family, dns_override[i], &dns_list[i]) !=1 )return false; }
@@ -580,11 +634,12 @@ static bool netconfig_check_v4_config(struct l_netconfig *netconfig)return true; } -static bool netconfig_check_config (struct l_netconfig *netconfig) +static bool netconfig_check_config (struct l_netconfig *nc) {/* TODO: error reporting through a debug log handler or otherwise */ -return netconfig_check_v4_config (netconfig); +return netconfig_check_family_config (nc,AF_INET ) && +netconfig_check_family_config (nc,AF_INET6 ); } LIB_EXPORT bool l_netconfig_check_config(struct l_netconfig *netconfig)
@@ -595,7 +650,7 @@ LIB_EXPORT bool l_netconfig_check_config(struct l_netconfig *netconfig)return netconfig_check_config (netconfig); } -static void netconfig_add_static_address_routes (struct l_netconfig *nc) +static void netconfig_add_v4_static_address_routes (struct l_netconfig *nc) {char ip[INET_ADDRSTRLEN ];uint32_t prefix_len;
@@ -609,6 +664,29 @@ static void netconfig_add_static_address_routes(struct l_netconfig *nc)netconfig_add_v4_routes (nc, ip, prefix_len,NULL ,RTPROT_STATIC ); } +/* +* Just mirror the IPv4 behaviour with static IPv6 configuration. It would +* be more logical to let the user choose between static IPv6 address and +* DHCPv6, and, completely independently, choose between static routes +* (if a static prefix length and/or gateway address is set) and ICMPv6. +* Yet a mechanism identical with IPv4 is easier to understand for a typical +* user so providing a static address just disables all automatic +* configuration. +*/ +static void netconfig_add_v6_static_address_routes (struct l_netconfig *nc) +{ +char ip[INET6_ADDRSTRLEN ]; +uint32_t prefix_len; + + nc->v6_address =l_rtnl_address_clone (nc->v6_static_addr); +l_queue_push_tail (nc->addresses.current, nc->v6_address); +l_queue_push_tail (nc->addresses.added, nc->v6_address); + +l_rtnl_address_get_address (nc->v6_static_addr, ip); + prefix_len =l_rtnl_address_get_prefix_length (nc->v6_static_addr); +netconfig_add_v6_static_routes (nc, ip, prefix_len); +} +static void netconfig_do_static_config (struct l_idle *idle,void *user_data) {struct l_netconfig *nc = user_data;
@@ -616,10 +694,16 @@ static void netconfig_do_static_config(struct l_idle *idle, void *user_data)l_idle_remove (l_steal_ptr (nc->do_static_work));if (nc->v4_static_addr && !nc->v4_configured) { -netconfig_add_static_address_routes (nc); +netconfig_add_v4_static_address_routes (nc); nc->v4_configured = true;netconfig_emit_event (nc,AF_INET ,L_NETCONFIG_EVENT_CONFIGURE ); } + +if (nc->v6_static_addr && !nc->v6_configured) { +netconfig_add_v6_static_address_routes (nc); + nc->v6_configured = true; +netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_CONFIGURE ); + } } LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
@@ -631,7 +715,7 @@ LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)return false;if (!netconfig->v4_enabled) - goto done; + goto configure_ipv6;if (netconfig->v4_static_addr) {/*
@@ -641,12 +725,26 @@ LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig) netconfig->do_static_work =l_idle_create ( netconfig_do_static_config, netconfig,NULL ); - goto done; + goto configure_ipv6; }if (!l_dhcp_client_start (netconfig->dhcp_client))return false; +configure_ipv6: +if (!netconfig->v6_enabled) + goto done; + +if (netconfig->v6_static_addr && !netconfig->do_static_work) { +/* +* We're basically ready to configure the interface +* but do this in an idle callback. +*/ + netconfig->do_static_work =l_idle_create ( + netconfig_do_static_config, + netconfig,NULL ); + } + done: netconfig->started = true;return true;
@@ -670,6 +768,7 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig) netconfig->v4_address =NULL ; netconfig->v4_subnet_route =NULL ; netconfig->v4_default_route =NULL ; + netconfig->v6_address =NULL ;l_dhcp_client_stop (netconfig->dhcp_client); }
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 3/17 rtnl: Move l_rtnl_route declaration to private header
In reply to Andrew ZaborowskiMove struct l_rtnl_route declaration to new file ell/rtnl-private.h so the members can be accessed directly from ell files to save address to string conversions.
--- Makefile.am | 1 + ell/rtnl-private.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ ell/rtnl.c | 25 +------------------------ 3 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 ell/rtnl-private.h
Makefile.am +1 -0
@@ -90,6 +90,7 @@ ell_libell_la_SOURCES = $(linux_headers) \ ell/netlink-private.h \ ell/netlink.c \ ell/genl.c \ + ell/rtnl-private.h \ ell/rtnl.c \ ell/dbus-private.h \ ell/dbus.c \
ell/rtnl-private.h new file +45 -0
45 lines not on this page · raw message
ell/rtnl.c +1 -24
@@ -39,6 +39,7 @@#include "log.h" #include "util.h" #include "time.h" +#include "rtnl-private.h" #include "rtnl.h" #include "private.h"
@@ -333,30 +334,6 @@ LIB_EXPORT bool l_rtnl_address_set_scope(struct l_rtnl_address *addr,return true; } -struct l_rtnl_route { -uint8_t family; -uint8_t scope; -uint8_t protocol; -union { -struct in6_addr in6_addr; -struct in_addr in_addr; - } gw; -union { -struct in6_addr in6_addr; -struct in_addr in_addr; - } dst; -uint8_t dst_prefix_len; -union { -struct in6_addr in6_addr; -struct in_addr in_addr; - } prefsrc; -uint32_t lifetime; -uint64_t expiry_time; -uint32_t mtu; -uint32_t priority; -uint8_t preference; -}; - LIB_EXPORT struct l_rtnl_route *l_rtnl_route_new_gateway(const char *gw) {struct in_addr in_addr;
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 4/17 netconfig: Create routes from Router Advertisements
In reply to Andrew ZaborowskiIf IPv6 is enabled, create the ICMP6 client and handle Router Advertisements received by creating, updating and removing routes per the data in the Router Advertisements. Timeouts aren't handled yet.
--- ell/netconfig.c | 293 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 278 insertions(+), 15 deletions(-)
ell/netconfig.c +278 -15
@@ -27,14 +27,19 @@#include <linux/types.h> #include <netinet/ip.h> #include <arpa/inet.h> +#include <netinet/icmp6.h> #include "private.h" #include "useful.h" #include "log.h" #include "dhcp.h" #include "dhcp-private.h" +#include "icmp6.h" +#include "icmp6-private.h" +#include "dhcp6.h" #include "netlink.h" #include "rtnl.h" +#include "rtnl-private.h" #include "queue.h" #include "time.h" #include "idle.h"
@@ -64,6 +69,8 @@ struct l_netconfig {bool v4_configured;struct l_dhcp_client *dhcp_client;bool v6_configured; +struct l_icmp6_client *icmp6_client; +struct l_dhcp6_client *dhcp6_client;/* These objects, if not NULL, are owned by @addresses and @routes */ struct l_rtnl_address *v4_address;
@@ -128,6 +135,33 @@ static void netconfig_emit_event(struct l_netconfig *nc, uint8_t family,netconfig_update_cleanup (nc); } +static struct l_rtnl_route *netconfig_route_new (struct l_netconfig *nc, +uint8_t family, +const void *dst, +uint8_t prefix_len, +const void *gw, +uint8_t protocol) +{ + struct l_rtnl_route *rt = l_new(struct l_rtnl_route, 1); + + rt->family = family; + rt->scope = (family ==AF_INET && dst) ? +RT_SCOPE_LINK :RT_SCOPE_UNIVERSE ; + rt->protocol = protocol; + rt->lifetime =0xffffffff ; + rt->priority = nc->route_priority; + +if (dst) { +memcpy (&rt->dst, dst, family ==AF_INET ?4 :16 ); + rt->dst_prefix_len = prefix_len; + } + +if (gw) +memcpy (&rt->gw, gw, family ==AF_INET ?4 :16 ); + +return rt; +} +static void netconfig_add_v4_routes (struct l_netconfig *nc,const char *ip,uint8_t prefix_len,const char *gateway,uint8_t rtm_protocol)
@@ -141,12 +175,9 @@ static void netconfig_add_v4_routes(struct l_netconfig *nc, const char *ip,return ; in_addr.s_addr &=htonl (0xfffffffflu << (32 - prefix_len)); -if (L_WARN_ON (!inet_ntop (AF_INET , &in_addr, network,INET_ADDRSTRLEN ))) -return ; - - nc->v4_subnet_route =l_rtnl_route_new_prefix (network, prefix_len); -l_rtnl_route_set_protocol (nc->v4_subnet_route, rtm_protocol); -l_rtnl_route_set_priority (nc->v4_subnet_route, nc->route_priority); + nc->v4_subnet_route =netconfig_route_new (nc,AF_INET , network, + prefix_len,NULL , + rtm_protocol);l_queue_push_tail (nc->routes.current, nc->v4_subnet_route);l_queue_push_tail (nc->routes.added, nc->v4_subnet_route);
@@ -163,6 +194,7 @@ static void netconfig_add_v4_routes(struct l_netconfig *nc, const char *ip, nc->v4_default_route =l_rtnl_route_new_gateway (gateway);l_rtnl_route_set_protocol (nc->v4_default_route, rtm_protocol);L_WARN_ON (!l_rtnl_route_set_prefsrc (nc->v4_default_route, ip)); +l_rtnl_route_set_priority (nc->v4_default_route, nc->route_priority);l_queue_push_tail (nc->routes.current, nc->v4_default_route);l_queue_push_tail (nc->routes.added, nc->v4_default_route); }
@@ -173,7 +205,6 @@ static void netconfig_add_v6_static_routes(struct l_netconfig *nc, {struct in6_addr in6_addr;const void *prefix; -char network[INET6_ADDRSTRLEN ];struct l_rtnl_route *v6_subnet_route;struct l_rtnl_route *v6_default_route;
@@ -188,17 +219,13 @@ static void netconfig_add_v6_static_routes(struct l_netconfig *nc,*/ prefix =net_prefix_from_ipv6 (in6_addr.s6_addr, prefix_len); -if (L_WARN_ON (!inet_ntop (AF_INET6 , prefix, network,INET6_ADDRSTRLEN ))) -return ; -/* * One reason we add a subnet route instead of letting the kernel * do it, by not specifying IFA_F_NOPREFIXROUTE for the address, * is that that would force a 0 metric for the route. */ - v6_subnet_route =l_rtnl_route_new_prefix (network, prefix_len); -l_rtnl_route_set_protocol (v6_subnet_route,RTPROT_STATIC ); -l_rtnl_route_set_priority (v6_subnet_route, nc->route_priority); + v6_subnet_route =netconfig_route_new (nc,AF_INET6 , prefix, prefix_len, +NULL ,RTPROT_STATIC );l_queue_push_tail (nc->routes.current, v6_subnet_route);l_queue_push_tail (nc->routes.added, v6_subnet_route);
@@ -214,6 +241,19 @@ static void netconfig_add_v6_static_routes(struct l_netconfig *nc,l_queue_push_tail (nc->routes.added, v6_default_route); } +static bool netconfig_route_exists (struct l_queue *list, +const struct l_rtnl_route *route) +{ +const struct l_queue_entry *entry; + +for (entry =l_queue_get_entries (list); entry; + entry = entry->next) +if ((const struct l_rtnl_route *) entry->data == route) +return true; + +return false; +} +static void netconfig_add_dhcp_address_routes (struct l_netconfig *nc) { const struct l_dhcp_lease *lease =
@@ -360,6 +400,204 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client, } } +static struct l_rtnl_route *netconfig_find_icmp6_route ( +struct l_netconfig *nc, +const uint8_t *gateway, +const struct route_info *dst) +{ +const struct l_queue_entry *entry; + +for (entry =l_queue_get_entries (nc->routes.current); entry; + entry = entry->next) { +struct l_rtnl_route *route = entry->data; +const uint8_t *route_gateway; +const uint8_t *route_dst; +uint8_t route_prefix_len =0 ; + +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); +if ((gateway || route_gateway) && + (!gateway || !route_gateway || +memcmp (gateway, route_gateway,16 ))) +continue ; + + route_dst =l_rtnl_route_get_dst_in_addr (route, + &route_prefix_len); +if ((dst || route_prefix_len) && + (!dst || !route_prefix_len || + dst->prefix_len != route_prefix_len || +memcmp (dst->address, route_dst,16 ))) +continue ; + +return route; + } + +return NULL ; +} + +static struct l_rtnl_route *netconfig_add_icmp6_route (struct l_netconfig *nc, +const uint8_t *gateway, +const struct route_info *dst, +uint8_t preference) +{ +struct l_rtnl_route *rt; + + rt =netconfig_route_new (nc,AF_INET6 , dst->address, dst->prefix_len, + gateway,RTPROT_RA ); +if (L_WARN_ON (!rt)) +return NULL ; + +l_rtnl_route_set_preference (rt, preference); +l_queue_push_tail (nc->routes.current, rt); +l_queue_push_tail (nc->routes.added, rt); +return rt; +} + +static void netconfig_set_icmp6_route_data (struct l_netconfig *nc, +struct l_rtnl_route *rt, +uint64_t start_time, +uint32_t preferred_lifetime, +uint32_t valid_lifetime, +uint32_t mtu,bool updated) +{ +uint64_t expiry = start_time + valid_lifetime *L_USEC_PER_SEC ; +uint64_t old_expiry =l_rtnl_route_get_expiry (rt); +bool differs = false; + +if (mtu !=l_rtnl_route_get_mtu (rt)) { +l_rtnl_route_set_mtu (rt, mtu); + differs = true; + } + +/* +* valid_lifetime of 0 from a route_info means the route is being +* removed so we wouldn't be here. valid_lifetime of 0xffffffff +* means no timeout. Check if the lifetime is changing between +* finite and infinite, or two finite values that result in expiry +* time difference of more than a second -- to avoid emitting +* updates for changes resulting only from the valid_lifetime one +* second resolution and RA transmission jitter. As RFC4861 +* Section 6.2.7 puts it: "Due to link propagation delays and +* potentially poorly synchronized clocks between the routers such +* comparison SHOULD allow some time skew." The RFC talks about +* routers processing one another's RAs but the same logic applies +* here. +*/ +if (valid_lifetime ==0xffffffff ) + expiry =0 ; + +if ((expiry || old_expiry) && + (!expiry || !old_expiry || +l_time_diff (expiry, old_expiry) >L_USEC_PER_SEC )) { +l_rtnl_route_set_lifetime (rt, valid_lifetime); +l_rtnl_route_set_expiry (rt, expiry); + differs = true; + } + +if (updated && differs && !netconfig_route_exists (nc->routes.added, rt)) +l_queue_push_tail (nc->routes.updated, rt); +} + +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); +} + +static void netconfig_icmp6_event_handler (struct l_icmp6_client *client, +enum l_icmp6_client_event event, +void *event_data, +void *user_data) +{ +struct l_netconfig *nc = user_data; +const struct l_icmp6_router *r; +struct l_rtnl_route *default_route; +unsigned int i; + +if (event !=L_ICMP6_CLIENT_EVENT_ROUTER_FOUND ) +return ; + + r = event_data; + +/* +* Note: If this is the first RA received, the l_dhcp6_client +* will have received the event before us and will be acting +* on it by now. +*/ + +if (nc->v4_gateway_override) +return ; + +/* Process the default gateway information */ + default_route =netconfig_find_icmp6_route (nc, r->address,NULL ); + +if (!default_route && r->lifetime) { + default_route =netconfig_add_icmp6_route (nc, r->address,NULL , + r->pref); +if (unlikely (!default_route)) +return ; + +/* +* r->lifetime is 16-bit only so there's no risk it gets +* confused for the special 0xffffffff value in +* netconfig_set_icmp6_route_data. +*/ +netconfig_set_icmp6_route_data (nc, default_route, r->start_time, + r->lifetime, r->lifetime, + r->mtu, false); + }else if (default_route && r->lifetime) +netconfig_set_icmp6_route_data (nc, default_route, r->start_time, + r->lifetime, r->lifetime, + r->mtu, true); +else if (default_route && !r->lifetime) +netconfig_remove_icmp6_route (nc, default_route); + +/* +* Process the onlink and offlink routes, from the Router +* Advertisement's Prefix Information options and Route +* Information options respectively. +*/ +for (i =0 ; i < r->n_routes; i++) { +const struct route_info *info = &r->routes[i]; +const uint8_t *gateway = info->onlink ?NULL : r->address; +struct l_rtnl_route *route = +netconfig_find_icmp6_route (nc, gateway, info); + +if (!route && info->valid_lifetime) { + route =netconfig_add_icmp6_route (nc, gateway, info, + info->preference); +if (unlikely (!route)) +continue ; + +netconfig_set_icmp6_route_data (nc, route, r->start_time, + info->preferred_lifetime, + info->valid_lifetime, + gateway ? r->mtu :0 , false); + }else if (route && info->valid_lifetime) +netconfig_set_icmp6_route_data (nc, route, r->start_time, + info->preferred_lifetime, + info->valid_lifetime, + gateway ? r->mtu :0 , true); +else if (route && !info->valid_lifetime) +netconfig_remove_icmp6_route (nc, route); + } + +/* +* Note: we may be emitting this before L_NETCONFIG_EVENT_CONFIGURE. +* We should probably instead save the affected routes in separate +* lists and add them to the _CONFIGURE event, suppressing any _UPDATE +* events while nc->v6_configured is false. +*/ +if (!l_queue_isempty (nc->routes.added) || + !l_queue_isempty (nc->routes.updated) || + !l_queue_isempty (nc->routes.removed)) +netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_UPDATE ); +} + LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) {struct l_netconfig *nc;
@@ -382,6 +620,13 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) netconfig_dhcp_event_handler, nc, NULL); + nc->dhcp6_client =l_dhcp6_client_new (ifindex); + + nc->icmp6_client =l_dhcp6_client_get_icmp6 (nc->dhcp6_client); +l_icmp6_client_add_event_handler (nc->icmp6_client, + netconfig_icmp6_event_handler, + nc,NULL ); +return nc; }
@@ -402,6 +647,7 @@ LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig)l_netconfig_set_domain_names_override (netconfig,AF_INET6 ,NULL );l_dhcp_client_destroy (netconfig->dhcp_client); +l_dhcp6_client_destroy (netconfig->dhcp6_client);l_netconfig_set_event_handler (netconfig,NULL ,NULL ,NULL );l_queue_destroy (netconfig->addresses.current,NULL );l_queue_destroy (netconfig->addresses.added,NULL );
@@ -735,19 +981,35 @@ configure_ipv6:if (!netconfig->v6_enabled) goto done; -if (netconfig->v6_static_addr && !netconfig->do_static_work) { +if (netconfig->v6_static_addr) {/* * We're basically ready to configure the interface * but do this in an idle callback. */ - netconfig->do_static_work =l_idle_create ( +if (!netconfig->do_static_work) + netconfig->do_static_work =l_idle_create ( netconfig_do_static_config, netconfig,NULL ); + + goto done; } +if (!l_dhcp6_client_start (netconfig->dhcp6_client)) + goto stop_ipv4; + done: netconfig->started = true;return true; + +stop_ipv4: +if (netconfig->v4_enabled) { +if (netconfig->v4_static_addr) +l_idle_remove (l_steal_ptr (netconfig->do_static_work)); +else +l_dhcp_client_stop (netconfig->dhcp_client); + } + +return false; } LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig)
@@ -771,6 +1033,7 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig) netconfig->v6_address =NULL ;l_dhcp_client_stop (netconfig->dhcp_client); +l_dhcp6_client_stop (netconfig->dhcp6_client); } LIB_EXPORT struct l_dhcp_client *l_netconfig_get_dhcp_client(
Signature · 1 line
2.32.0
Denis Kenzior
Re: [PATCH 04/17] netconfig: Create routes from Router Advertisements
Hi Andrew,
Quoted 7 lines · Andrew Zaborowski
If IPv6 is enabled, create the ICMP6 client and handle Router Advertisements received by creating, updating and removing routes per the data in the Router Advertisements. Timeouts aren't handled yet.
--- ell/netconfig.c | 293 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 278 insertions(+), 15 deletions(-)
<snip>
Quoted 14 lines
ell/netconfig.c +14
-0+static void netconfig_icmp6_event_handler (struct l_icmp6_client *client, +enum l_icmp6_client_event event, +void *event_data, +void *user_data) +{ +struct l_netconfig *nc = user_data; +const struct l_icmp6_router *r; +struct l_rtnl_route *default_route; +unsigned int i; + +if (event !=L_ICMP6_CLIENT_EVENT_ROUTER_FOUND ) +return ; + + r = event_data;
ell/netconfig.c static void netconfig_dhcp_event_handler(struct l_dhcp_client *client, · line 534
Quoted 10 lines
ell/netconfig.c +10
-0+ +/* +* Note: If this is the first RA received, the l_dhcp6_client +* will have received the event before us and will be acting +* on it by now. +*/ + +if (nc->v4_gateway_override) +return ; +
So I think you meant v6_gateway_override here and I fixed this up accordingly. Send a fix if this wasn't correct.
Patches 1-4 applied, thanks.
Regards,
-Denis
Andrew Zaborowski
PATCH 5/17 netconfig: Handle DHCPv6 events
In reply to Andrew ZaborowskiUse l_dhcp6_client to support IPv6 address assignment through DHCPv6.
--- ell/netconfig.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+)
ell/netconfig.c +132 -0
@@ -400,6 +400,135 @@ 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 ; + +/* +* Assume we already have a route from a Router Advertisement +* covering the address from DHCPv6 + prefix length from DHCPv6. +* We might want to emit a warning of some sort or +* L_NETCONFIG_EVENT_FAILED if we don't since this would +* basically be fatal for IPv6 connectivity. +*/ +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,
@@ -621,6 +750,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
Denis Kenzior
AppliedRe: [PATCH 05/17] netconfig: Handle DHCPv6 events
Hi Andrew,
Quoted 5 lines · Andrew Zaborowski
Use
l_dhcp6_clientto support IPv6 address assignment through DHCPv6.--- ell/netconfig.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+)
Applied, thanks.
Regards,
-Denis
Andrew Zaborowski
PATCH 6/17 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
@@ -1231,37 +1231,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 ;
@@ -83,14 +83,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
Denis Kenzior
Re: [PATCH 06/17] netconfig: Avoid returning internal l_queues
Hi Andrew,
Quoted 6 lines · Andrew Zaborowski
Return const struct
l_queue_entrypointers instead.--- ell/netconfig.c | 35 ++++++++++++++++++----------------- ell/netconfig.h | 20 +++++++++++--------- 2 files changed, 29 insertions(+), 26 deletions(-)
No objections to this, but I wish examples/ changes were right after this patch :)
Regards,
-Denis
Andrew Zaborowski
PATCH 7/17 netconfig: Use an internal rtnl socket for l_netconfig_apply_rtnl
In reply to Andrew ZaborowskiSince we'll need to keep an internal RTNL socket for DHCPv6 functionality anyway, change l_netconfig_apply_rtnl's signature to not require an rtnl parameter.
--- ell/netconfig.c | 34 ++++++++++++++++++++-------------- ell/netconfig.h | 3 +-- 2 files changed, 21 insertions(+), 16 deletions(-)
ell/netconfig.c +20 -14
@@ -64,6 +64,7 @@ struct l_netconfig {char **v6_dns_override;char **v6_domain_names_override; +struct l_netlink *rtnl;bool started;struct l_idle *do_static_work;bool v4_configured;
@@ -730,8 +731,13 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client, LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) {struct l_netconfig *nc; +struct l_netlink *rtnl =l_netlink_new (NETLINK_ROUTE ); + +if (!rtnl) +return NULL ; nc = l_new(struct l_netconfig, 1); + nc->rtnl = rtnl; nc->ifindex = ifindex; nc->v4_enabled = true;
@@ -789,6 +795,7 @@ LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig)l_queue_destroy (netconfig->routes.added,NULL );l_queue_destroy (netconfig->routes.updated,NULL );l_queue_destroy (netconfig->routes.removed,NULL ); +l_netlink_destroy (netconfig->rtnl);l_free (netconfig); }
@@ -1193,42 +1200,41 @@ LIB_EXPORT void l_netconfig_set_event_handler(struct l_netconfig *netconfig, netconfig->handler.destroy = destroy; } -LIB_EXPORT void l_netconfig_apply_rtnl(struct l_netconfig *netconfig, -struct l_netlink *rtnl) +LIB_EXPORT void l_netconfig_apply_rtnl(struct l_netconfig *netconfig) {const struct l_queue_entry *entry;for (entry =l_queue_get_entries (netconfig->addresses.removed); entry; entry = entry->next) -l_rtnl_ifaddr_delete (rtnl, netconfig->ifindex, entry->data, -NULL ,NULL ,NULL ); +l_rtnl_ifaddr_delete (netconfig->rtnl, netconfig->ifindex, + entry->data,NULL ,NULL ,NULL );for (entry =l_queue_get_entries (netconfig->addresses.added); entry; entry = entry->next) -l_rtnl_ifaddr_add (rtnl, netconfig->ifindex, entry->data, -NULL ,NULL ,NULL ); +l_rtnl_ifaddr_add (netconfig->rtnl, netconfig->ifindex, + entry->data,NULL ,NULL ,NULL );/* We can use l_rtnl_ifaddr_add here since that uses NLM_F_REPLACE */ for (entry =l_queue_get_entries (netconfig->addresses.updated); entry; entry = entry->next) -l_rtnl_ifaddr_add (rtnl, netconfig->ifindex, entry->data, -NULL ,NULL ,NULL ); +l_rtnl_ifaddr_add (netconfig->rtnl, netconfig->ifindex, + entry->data,NULL ,NULL ,NULL );for (entry =l_queue_get_entries (netconfig->routes.removed); entry; entry = entry->next) -l_rtnl_route_delete (rtnl, netconfig->ifindex, entry->data, -NULL ,NULL ,NULL ); +l_rtnl_route_delete (netconfig->rtnl, netconfig->ifindex, + entry->data,NULL ,NULL ,NULL );for (entry =l_queue_get_entries (netconfig->routes.added); entry; entry = entry->next) -l_rtnl_route_add (rtnl, netconfig->ifindex, entry->data, -NULL ,NULL ,NULL ); +l_rtnl_route_add (netconfig->rtnl, netconfig->ifindex, + entry->data,NULL ,NULL ,NULL );/* We can use l_rtnl_route_add here since that uses NLM_F_REPLACE */ for (entry =l_queue_get_entries (netconfig->routes.updated); entry; entry = entry->next) -l_rtnl_route_add (rtnl, netconfig->ifindex, entry->data, -NULL ,NULL ,NULL ); +l_rtnl_route_add (netconfig->rtnl, netconfig->ifindex, + entry->data,NULL ,NULL ,NULL ); } LIB_EXPORT const struct l_queue_entry *l_netconfig_get_addresses(
ell/netconfig.h +1 -2
@@ -81,8 +81,7 @@ void l_netconfig_set_event_handler(struct l_netconfig *netconfig,void *user_data, l_netconfig_destroy_cb_t destroy); -void l_netconfig_apply_rtnl (struct l_netconfig *netconfig, -struct l_netlink *rtnl); +void l_netconfig_apply_rtnl (struct l_netconfig *netconfig); const struct l_queue_entry *l_netconfig_get_addresses( struct l_netconfig *netconfig, const struct l_queue_entry **out_added,
Signature · 1 line
2.32.0
Denis Kenzior
Re: [PATCH 07/17] netconfig: Use an internal rtnl socket for l_netconfig_apply_rtnl
Hi Andrew,
Quoted 8 lines · Andrew Zaborowski
Since we'll need to keep an internal RTNL socket for DHCPv6 functionality anyway, change l_netconfig_apply_rtnl's signature to not require an rtnl parameter.
--- ell/netconfig.c | 34 ++++++++++++++++++++-------------- ell/netconfig.h | 3 +-- 2 files changed, 21 insertions(+), 16 deletions(-)
<snip>
ell/netconfig.c static void netconfig_icmp6_event_handler(struct l_icmp6_client *client, · line 737
Quoted 8 lines
ell/netconfig.c +4
-0@@ -730,8 +731,13 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client, LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) {struct l_netconfig *nc; +struct l_netlink *rtnl =l_netlink_new (NETLINK_ROUTE ); + +if (!rtnl) +return NULL ;
I'm against this. We will have multiple netconfig objects in play and each one would be utilizing an fd. We should be keeping our fd use to a minimum, particularly since we're limited to ~128 right now. Passing in the rtnl object from outside is preferred.
Regards,
-Denis
Andrew Zaborowski
Re: [PATCH 07/17] netconfig: Use an internal rtnl socket for l_netconfig_apply_rtnl
Hi Denis,
Quoted 9 lines · Denis Kenzior
ell/netconfig.c static void netconfig_icmp6_event_handler(struct l_icmp6_client *client, · line 737
Quoted 7 lines · Andrew Zaborowski
LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) { struct l_netconfig *nc;ell/netconfig.c +4
-0+struct l_netlink *rtnl =l_netlink_new (NETLINK_ROUTE ); + +if (!rtnl) +return NULL ;
Quoted 4 lines
I'm against this. We will have multiple netconfig objects in play and each one would be utilizing an fd. We should be keeping our fd use to a minimum, particularly since we're limited to ~128 right now. Passing in the rtnl object from outside is preferred.
Makes sense. How about using a global rtnl object for all of the l_netconfig objects? Do you still prefer that it be passed from outside?
I did this later in patch 11 and maybe should have merged that change directly here. We could also create it only after l_netconfig_start().
Best regards
Denis Kenzior
Re: [PATCH 07/17] netconfig: Use an internal rtnl socket for l_netconfig_apply_rtnl
Hi Andrew,
Quoted 3 lines
Makes sense. How about using a global rtnl object for all of the
l_netconfigobjects? Do you still prefer that it be passed from outside?
I have no real preference on whether it is passed in from the outside vs not. But, there's another thing to consider here. Today the call to l_foo_set_rtnl is used determine whether the dhcp/icmp/dhcp6 object should set everything up directly vs letting the outside layer do this. With l_netconfig I don't think this is a problem per se, but you should consider the other classes when you design this.
We can start by trying to come up with a nice ell API for obtaining the singleton rtnl object and go from there.
Regards,
-Denis
Andrew Zaborowski
Re: [PATCH 07/17] netconfig: Use an internal rtnl socket for l_netconfig_apply_rtnl
Quoted 10 lines · Denis Kenzior
Quoted 3 lines
Makes sense. How about using a global rtnl object for all of the
l_netconfigobjects? Do you still prefer that it be passed from outside?I have no real preference on whether it is passed in from the outside vs not. But, there's another thing to consider here. Today the call to
l_foo_set_rtnlis used determine whether the dhcp/icmp/dhcp6 object should set everything up directly vs letting the outside layer do this. Withl_netconfigI don't think this is a problem per se, but you should consider the other classes when you design this.
Right but with l_netconfig you make this choice by either calling l_netconfig_apply_rtnl or not, and you shouldn't call l_foo_set_rtnl on the underlying dhcp/icmp objects.
Quoted 3 lines
We can start by trying to come up with a nice ell API for obtaining the singleton rtnl object and go from there.
The global rtnl object I meant is to be used only by l_netconfig instances so there's no need for a getter, or I'm misunderstanding what you're saying.
Best regards
Denis Kenzior
Re: [PATCH 07/17] netconfig: Use an internal rtnl socket for l_netconfig_apply_rtnl
Hi Andrew,
Quoted 12 lines · Andrew Zaborowski
Quoted 10 lines · Denis Kenzior
Quoted 3 lines
Makes sense. How about using a global rtnl object for all of the
l_netconfigobjects? Do you still prefer that it be passed from outside?I have no real preference on whether it is passed in from the outside vs not. But, there's another thing to consider here. Today the call to
l_foo_set_rtnlis used determine whether the dhcp/icmp/dhcp6 object should set everything up directly vs letting the outside layer do this. Withl_netconfigI don't think this is a problem per se, but you should consider the other classes when you design this.
Quoted 4 lines
Right but with
l_netconfigyou make this choice by either callingl_netconfig_apply_rtnlor not, and you shouldn't calll_foo_set_rtnlon the underlying dhcp/icmp objects.
Sure, but are you assuming we will not have any further classes using rtnl besides netconfig?
Quoted 8 lines
Quoted 3 lines
We can start by trying to come up with a nice ell API for obtaining the singleton rtnl object and go from there.
The global rtnl object I meant is to be used only by
l_netconfiginstances so there's no need for a getter, or I'm misunderstanding what you're saying.
We use rtnl all over the place in iwd, like netdev, p2p, ap, etc. All these + netconfig should share the same one.
Regards,
-Denis
Andrew Zaborowski
Re: [PATCH 07/17] netconfig: Use an internal rtnl socket for l_netconfig_apply_rtnl
Quoted 25 lines · Denis Kenzior
Quoted 12 lines · Andrew Zaborowski
Quoted 10 lines · Denis Kenzior
Quoted 3 lines
Makes sense. How about using a global rtnl object for all of the
l_netconfigobjects? Do you still prefer that it be passed from outside?I have no real preference on whether it is passed in from the outside vs not. But, there's another thing to consider here. Today the call to
l_foo_set_rtnlis used determine whether the dhcp/icmp/dhcp6 object should set everything up directly vs letting the outside layer do this. Withl_netconfigI don't think this is a problem per se, but you should consider the other classes when you design this.Quoted 4 lines
Right but with
l_netconfigyou make this choice by either callingl_netconfig_apply_rtnlor not, and you shouldn't calll_foo_set_rtnlon the underlying dhcp/icmp objects.Sure, but are you assuming we will not have any further classes using rtnl besides netconfig?
Quoted 4 lines
Quoted 3 lines
We can start by trying to come up with a nice ell API for obtaining the singleton rtnl object and go from there.
Quoted 7 lines
Quoted 4 lines
The global rtnl object I meant is to be used only by
l_netconfiginstances so there's no need for a getter, or I'm misunderstanding what you're saying.We use rtnl all over the place in iwd, like netdev, p2p, ap, etc. All these + netconfig should share the same one.
That's different than what I did in patch 11/17 but it would work. I'll put it in rtnl.c then, with a _get() and a _put() since we have no generic refcounting for l_netlink.
Best refards
Andrew Zaborowski
PATCH 8/17 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 | 166 ++++++++++++++++++++++++++++++++++++++++++------ ell/netconfig.h | 6 +- 2 files changed, 152 insertions(+), 20 deletions(-)
ell/netconfig.c +148 -18
@@ -72,6 +72,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;
@@ -92,10 +93,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 {
@@ -116,10 +121,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,
@@ -163,6 +172,24 @@ static struct l_rtnl_route *netconfig_route_new(struct l_netconfig *nc,return rt; } +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)
@@ -242,6 +269,19 @@ static void netconfig_add_v6_static_routes(struct l_netconfig *nc,l_queue_push_tail (nc->routes.added, v6_default_route); } +static bool netconfig_address_exists (struct l_queue *list, +const struct l_rtnl_address *address) +{ +const struct l_queue_entry *entry; + +for (entry =l_queue_get_entries (list); entry; + entry = entry->next) +if ((const struct l_rtnl_address *) entry->data == address) +return true; + +return false; +} +static bool netconfig_route_exists (struct l_queue *list,const struct l_rtnl_route *route) {
@@ -298,13 +338,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 && !netconfig_address_exists (nc->addresses.added, + 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 && !netconfig_route_exists (nc->routes.added, + nc->v4_subnet_route))l_queue_push_tail (nc->routes.updated, nc->v4_subnet_route);if (!nc->v4_default_route)
@@ -313,23 +355,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 && !netconfig_route_exists (nc->routes.added, + 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 ; } }
@@ -345,7 +406,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 );
@@ -370,7 +431,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))
@@ -451,14 +512,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 && !netconfig_address_exists (nc->addresses.added, + 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 ; }
@@ -482,7 +550,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 );
@@ -491,7 +559,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))
@@ -530,6 +598,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,
@@ -635,7 +746,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,
@@ -662,6 +776,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 );
@@ -724,7 +840,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 ); }
@@ -1161,6 +1278,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);
@@ -1241,7 +1361,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);
@@ -1252,6 +1373,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); }
@@ -1259,8 +1383,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);
@@ -1270,5 +1397,8 @@ 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
@@ -86,12 +86,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); #ifdef __cplusplus }
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 9/17 netconfig: Add l_netconfig_get_{dhcp6,icmp6}_client
In reply to Andrew Zaborowski--- ell/ell.sym | 2 ++ ell/netconfig.c | 18 ++++++++++++++++++ ell/netconfig.h | 4 ++++ 3 files changed, 24 insertions(+)
ell/ell.sym +2 -0
@@ -749,6 +749,8 @@ global: l_netconfig_start; l_netconfig_stop; l_netconfig_get_dhcp_client; + l_netconfig_get_dhcp6_client; + l_netconfig_get_icmp6_client; l_netconfig_set_event_handler; l_netconfig_apply_rtnl; l_netconfig_get_addresses;
ell/netconfig.c +18 -0
@@ -1304,6 +1304,24 @@ LIB_EXPORT struct l_dhcp_client *l_netconfig_get_dhcp_client(return netconfig->dhcp_client; } +LIB_EXPORT struct l_dhcp6_client *l_netconfig_get_dhcp6_client( +struct l_netconfig *netconfig) +{ +if (unlikely (!netconfig)) +return NULL ; + +return netconfig->dhcp6_client; +} + +LIB_EXPORT struct l_icmp6_client *l_netconfig_get_icmp6_client( +struct l_netconfig *netconfig) +{ +if (unlikely (!netconfig)) +return NULL ; + +return netconfig->icmp6_client; +} + LIB_EXPORT void l_netconfig_set_event_handler(struct l_netconfig *netconfig, l_netconfig_event_cb_t handler, void *user_data,
ell/netconfig.h +4 -0
@@ -75,6 +75,10 @@ void l_netconfig_stop(struct l_netconfig *netconfig);struct l_dhcp_client *l_netconfig_get_dhcp_client (struct l_netconfig *netconfig); +struct l_dhcp6_client *l_netconfig_get_dhcp6_client ( +struct l_netconfig *netconfig); +struct l_icmp6_client *l_netconfig_get_icmp6_client ( +struct l_netconfig *netconfig); void l_netconfig_set_event_handler(struct l_netconfig *netconfig, l_netconfig_event_cb_t handler,
Signature · 1 line
2.32.0
Denis Kenzior
AppliedRe: [PATCH 09/17] netconfig: Add l_netconfig_get_{dhcp6, icmp6}_client
Hi Andrew,
Quoted 6 lines · Andrew Zaborowski
--- ell/ell.sym | 2 ++ ell/netconfig.c | 18 ++++++++++++++++++ ell/netconfig.h | 4 ++++ 3 files changed, 24 insertions(+)
Applied, thanks.
Regards,
-Denis
Andrew Zaborowski
PATCH 10/17 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
@@ -98,6 +98,8 @@ const struct l_queue_entry *l_netconfig_get_routes(const struct l_queue_entry **out_updated, 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); #ifdef __cplusplus }
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 11/17 netconfig: Wait for link-local address before DHCPv6
In reply to Andrew ZaborowskiKeep one global rtnl instance instead of one per interface so that we can have just one RTM_NEWADDR listener. On l_netconfig_start dump existing IPv6 addresses and wait until a link-local address is added so it can be used as the source address for our DHCPv6 frames.
--- ell/netconfig.c | 178 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 163 insertions(+), 15 deletions(-)
ell/netconfig.c +163 -15
@@ -64,7 +64,7 @@ struct l_netconfig {char **v6_dns_override;char **v6_domain_names_override; -struct l_netlink *rtnl; +unsigned int ifaddr6_dump_cmd_id;bool started;struct l_idle *do_static_work;bool v4_configured;
@@ -115,6 +115,11 @@ union netconfig_addr {struct in6_addr v6; }; +static struct l_netlink *rtnl; +static unsigned int rtnl_use_count; +static struct l_queue *addr_wait_list; +static unsigned int rtnl_id; +static void netconfig_update_cleanup (struct l_netconfig *nc) {l_queue_clear (nc->addresses.added,NULL );
@@ -848,13 +853,17 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client, LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) {struct l_netconfig *nc; -struct l_netlink *rtnl =l_netlink_new (NETLINK_ROUTE ); -if (!rtnl) -return NULL ; +if (!rtnl) { + rtnl =l_netlink_new (NETLINK_ROUTE ); + +if (!rtnl) +return NULL ; + } + + rtnl_use_count++; nc = l_new(struct l_netconfig, 1); - nc->rtnl = rtnl; nc->ifindex = ifindex; nc->v4_enabled = true;
@@ -912,8 +921,12 @@ LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig)l_queue_destroy (netconfig->routes.added,NULL );l_queue_destroy (netconfig->routes.updated,NULL );l_queue_destroy (netconfig->routes.removed,NULL ); -l_netlink_destroy (netconfig->rtnl);l_free (netconfig); + + rtnl_use_count--; + +if (!rtnl_use_count) +l_netlink_destroy (l_steal_ptr (rtnl)); }/*
@@ -1208,6 +1221,115 @@ static void netconfig_do_static_config(struct l_idle *idle, void *user_data) } } +static void netconfig_rtnl_unregister (void *user_data) +{ +if (!addr_wait_list || !l_queue_isempty (addr_wait_list)) +return ; + +l_queue_destroy (l_steal_ptr (addr_wait_list),NULL ); +l_netlink_unregister (rtnl, rtnl_id); + rtnl_id =0 ; +} + +static void netconfig_addr_wait_unregister (struct l_netconfig *nc, +bool in_notify) +{ +if (nc->ifaddr6_dump_cmd_id) { +unsigned int cmd_id = nc->ifaddr6_dump_cmd_id; + + nc->ifaddr6_dump_cmd_id =0 ; +l_netlink_cancel (rtnl, cmd_id); + } + +if (!l_queue_remove (addr_wait_list, nc)) +return ; + +if (!l_queue_isempty (addr_wait_list)) +return ; + +/* We can't do l_netlink_unregister() inside a notification */ +if (in_notify) +l_idle_oneshot (netconfig_rtnl_unregister,NULL ,NULL ); +else +netconfig_rtnl_unregister (NULL ); +} + +static void netconfig_ifaddr_ipv6_added (struct l_netconfig *nc, +const struct ifaddrmsg *ifa, +uint32_t len) +{ +struct in6_addr in6; + _auto_(l_free) char *ip = NULL; + +if (ifa->ifa_flags &IFA_F_TENTATIVE ) +return ; + +if (!nc->started) +return ; + +l_rtnl_ifaddr6_extract (ifa, len, &ip); +inet_pton (AF_INET6 , ip, &in6); + +if (!IN6_IS_ADDR_LINKLOCAL (&in6)) +return ; + +netconfig_addr_wait_unregister (nc, true); + +l_dhcp6_client_set_link_local_address (nc->dhcp6_client, ip); + +/* +* Only now that we have a link-local address start actual DHCPv6 +* setup. +*/ +if (l_dhcp6_client_start (nc->dhcp6_client)) +return ; + +netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_FAILED ); +} + +static void netconfig_ifaddr_ipv6_notify (uint16_t type,const void *data, +uint32_t len,void *user_data) +{ +const struct ifaddrmsg *ifa = data; +uint32_t bytes = len -NLMSG_ALIGN (sizeof (struct ifaddrmsg )); +const struct l_queue_entry *entry, *next; + +switch (type) { +case RTM_NEWADDR : +/* Iterate safely since elements may be removed */ +for (entry =l_queue_get_entries (addr_wait_list); entry; + entry = next) { +struct l_netconfig *nc = entry->data; + + next = entry->next; +netconfig_ifaddr_ipv6_added (nc, ifa, bytes); + } + +break ; + } +} + +static void netconfig_ifaddr_ipv6_dump_cb (int error,uint16_t type, +const void *data,uint32_t len, +void *user_data) +{ +struct l_netconfig *nc = user_data; + +if (!nc->ifaddr6_dump_cmd_id || !nc->started) +return ; + +if (error) { +netconfig_addr_wait_unregister (nc, false); +netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_FAILED ); +return ; + } + +if (type !=RTM_NEWADDR ) +return ; + +netconfig_ifaddr_ipv6_notify (type, data, len, user_data); +} + LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig) {if (unlikely (!netconfig || netconfig->started))
@@ -1250,14 +1372,38 @@ configure_ipv6: goto done; } -if (!l_dhcp6_client_start (netconfig->dhcp6_client)) - goto stop_ipv4; +/* +* We only care about being on addr_wait_list if we're waiting for +* the link-local address for DHCP6. Add ourself to the list here +* before we start the dump, instead of after it ends, to eliminate +* the possibility of missing an RTM_NEWADDR between the end of +* the dump command and registering for the events. +*/ +if (!addr_wait_list) { + addr_wait_list =l_queue_new (); + + rtnl_id =l_netlink_register (rtnl,RTNLGRP_IPV6_IFADDR , + netconfig_ifaddr_ipv6_notify, + netconfig,NULL ); +if (!rtnl_id) + goto unregister; + } + + netconfig->ifaddr6_dump_cmd_id =l_rtnl_ifaddr6_dump (rtnl, + netconfig_ifaddr_ipv6_dump_cb, + netconfig,NULL ); +if (!netconfig->ifaddr6_dump_cmd_id) + goto unregister; + +l_queue_push_tail (addr_wait_list, netconfig); done: netconfig->started = true;return true; -stop_ipv4: +unregister: +netconfig_addr_wait_unregister (netconfig, false); +if (netconfig->v4_enabled) {if (netconfig->v4_static_addr)l_idle_remove (l_steal_ptr (netconfig->do_static_work));
@@ -1281,6 +1427,8 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig)if (netconfig->signal_expired_work)l_idle_remove (l_steal_ptr (netconfig->signal_expired_work)); +netconfig_addr_wait_unregister (netconfig, false); +netconfig_update_cleanup (netconfig);l_queue_clear (netconfig->routes.current, (l_queue_destroy_func_t ) l_rtnl_route_free);
@@ -1344,34 +1492,34 @@ LIB_EXPORT void l_netconfig_apply_rtnl(struct l_netconfig *netconfig)for (entry =l_queue_get_entries (netconfig->addresses.removed); entry; entry = entry->next) -l_rtnl_ifaddr_delete (netconfig->rtnl, netconfig->ifindex, +l_rtnl_ifaddr_delete (rtnl, netconfig->ifindex, entry->data,NULL ,NULL ,NULL );for (entry =l_queue_get_entries (netconfig->addresses.added); entry; entry = entry->next) -l_rtnl_ifaddr_add (netconfig->rtnl, netconfig->ifindex, +l_rtnl_ifaddr_add (rtnl, netconfig->ifindex, entry->data,NULL ,NULL ,NULL );/* We can use l_rtnl_ifaddr_add here since that uses NLM_F_REPLACE */ for (entry =l_queue_get_entries (netconfig->addresses.updated); entry; entry = entry->next) -l_rtnl_ifaddr_add (netconfig->rtnl, netconfig->ifindex, +l_rtnl_ifaddr_add (rtnl, netconfig->ifindex, entry->data,NULL ,NULL ,NULL );for (entry =l_queue_get_entries (netconfig->routes.removed); entry; entry = entry->next) -l_rtnl_route_delete (netconfig->rtnl, netconfig->ifindex, +l_rtnl_route_delete (rtnl, netconfig->ifindex, entry->data,NULL ,NULL ,NULL );for (entry =l_queue_get_entries (netconfig->routes.added); entry; entry = entry->next) -l_rtnl_route_add (netconfig->rtnl, netconfig->ifindex, +l_rtnl_route_add (rtnl, netconfig->ifindex, entry->data,NULL ,NULL ,NULL );/* We can use l_rtnl_route_add here since that uses NLM_F_REPLACE */ for (entry =l_queue_get_entries (netconfig->routes.updated); entry; entry = entry->next) -l_rtnl_route_add (netconfig->rtnl, netconfig->ifindex, +l_rtnl_route_add (rtnl, netconfig->ifindex, entry->data,NULL ,NULL ,NULL ); }
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 12/17 netconfig: Write /proc/sys/net files to enable IPv6
In reply to Andrew ZaborowskiWrite the accept_ra setting to 0. On l_netconfig_start if the existing address dump command finds no link-local address then also try resetting disable_ipv6 and toggling addr_gen_mode to trigger the kernel to generate a link-local address.
--- ell/netconfig.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-)
ell/netconfig.c +62 -2
@@ -28,6 +28,12 @@#include <netinet/ip.h> #include <arpa/inet.h> #include <netinet/icmp6.h> +#include <net/if.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> #include "private.h" #include "useful.h"
@@ -850,6 +856,30 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,netconfig_emit_event (nc,AF_INET6 ,L_NETCONFIG_EVENT_UPDATE ); } +static int netconfig_proc_write_ipv6_setting (struct l_netconfig *nc, +const char *setting, +const char *value) +{ +char ifname[IF_NAMESIZE ]; + _auto_(l_free) char *filename = NULL; +int fd; +int r; + +if (unlikely (!if_indextoname (nc->ifindex, ifname))) +return -errno; + + filename =l_strdup_printf ("/proc/sys/net/ipv6/conf/%s/%s" , + ifname, setting); + + fd =L_TFR (open (filename,O_WRONLY )); +if (unlikely (fd <0 )) +return -errno; + + r =L_TFR (write (fd, value,strlen (value))); +L_TFR (close (fd)); +return r; +} + LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) {struct l_netconfig *nc;
@@ -891,6 +921,9 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) netconfig_icmp6_event_handler, nc, NULL); +/* Disable in-kernel autoconfiguration for the interface */ +netconfig_proc_write_ipv6_setting (nc,"accept_ra" ,"0" ); +return nc; }
@@ -1330,6 +1363,32 @@ static void netconfig_ifaddr_ipv6_dump_cb(int error, uint16_t type,netconfig_ifaddr_ipv6_notify (type, data, len, user_data); } +static void netconfig_ifaddr_ipv6_dump_done_cb (void *user_data) +{ +struct l_netconfig *nc = user_data; + +/* +* Handle the case of no link-local address having been found during +* the dump. If nc->ifaddr6_dump_cmd_id is 0, we have found one or +* the dump is being cancelled. Otherwise try disabing the +* "disable_ipv6" setting for the interface since it may have been +* enabled. Also write "addr_gen_mode" which triggers regerating +* the link-local addresss on the interface in the kernel if it +* was previously removed. +*/ +if (!nc->ifaddr6_dump_cmd_id || !nc->started) +return ; + + nc->ifaddr6_dump_cmd_id =0 ; + +/* "do no generate a link-local address" */ +netconfig_proc_write_ipv6_setting (nc,"addr_gen_mode" ,"1" ); +/* "generate address based on EUI64 (default)" */ +netconfig_proc_write_ipv6_setting (nc,"addr_gen_mode" ,"0" ); +/* "enable IPv6 operation" */ +netconfig_proc_write_ipv6_setting (nc,"disable_ipv6" ,"0" ); +} + LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig) {if (unlikely (!netconfig || netconfig->started))
@@ -1390,8 +1449,9 @@ configure_ipv6: } netconfig->ifaddr6_dump_cmd_id =l_rtnl_ifaddr6_dump (rtnl, - netconfig_ifaddr_ipv6_dump_cb, - netconfig,NULL ); + netconfig_ifaddr_ipv6_dump_cb, + netconfig, + netconfig_ifaddr_ipv6_dump_done_cb);if (!netconfig->ifaddr6_dump_cmd_id) goto unregister;
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 13/17 netconfig: Write gateway MAC from DHCP to ARP cache
In reply to Andrew ZaborowskiWrite the MAC directly to the ARP table to save an ARP query for the gateway's MAC when it is known from the freshly obtained lease. For practical reasons do this outside of l_netconfig_apply_rtnl().
--- ell/netconfig.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
ell/netconfig.c +33 -0
@@ -25,6 +25,7 @@#endif #include <linux/types.h> +#include <linux/if_ether.h> #include <netinet/ip.h> #include <arpa/inet.h> #include <netinet/icmp6.h>
@@ -406,6 +407,37 @@ static void netconfig_remove_dhcp_address_routes(struct l_netconfig *nc, } } +static void netconfig_set_neighbor_entry_cb (int error, +uint16_t type,const void *data, +uint32_t len,void *user_data) +{ +/* Not critical. TODO: log warning */ +} + +static int netconfig_dhcp_gateway_to_arp (struct l_netconfig *nc) +{ +const struct l_dhcp_lease *lease = +l_dhcp_client_get_lease (nc->dhcp_client); + _auto_(l_free) char *server_id = l_dhcp_lease_get_server_id(lease); + _auto_(l_free) char *gw = l_dhcp_lease_get_gateway(lease); +const uint8_t *server_mac =l_dhcp_lease_get_server_mac (lease); +struct in_addr in_gw; + +if (!gw ||strcmp (server_id, gw) || !server_mac) +return -ENOENT ; + +/* Gateway MAC is known, write it into ARP cache to save ARP traffic */ + in_gw.s_addr =l_dhcp_lease_get_gateway_u32 (lease); + +if (!l_rtnl_neighbor_set_hwaddr (rtnl, nc->ifindex,AF_INET , &in_gw, + server_mac,ETH_ALEN , + netconfig_set_neighbor_entry_cb, nc, +NULL )) +return -EIO ; + +return 0 ; +} +static void netconfig_dhcp_event_handler (struct l_dhcp_client *client,enum l_dhcp_client_event event,void *user_data)
@@ -430,6 +462,7 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,netconfig_set_dhcp_lifetimes (nc, false); nc->v4_configured = true;netconfig_emit_event (nc,AF_INET ,L_NETCONFIG_EVENT_CONFIGURE ); +netconfig_dhcp_gateway_to_arp (nc);break ;case L_DHCP_CLIENT_EVENT_LEASE_RENEWED :if (L_WARN_ON (!nc->v4_configured))
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 14/17 netconfig: Ratelimit route lifetime updates
In reply to Andrew ZaborowskiOptimize away prefix route updates when the expiry time is far enough in the future that we expect to receive at least 10 Router Advertisements before then. For that purpose start keeping an extra record for each route where we save the last time this route was included in an RA and the maximum interval between two RAs with this route seen so far. By working with the maximum intervals we avoid worrying about different RA content policies where some routes may be skipped in some RAs and there even might be multiple routers sending RAs with subsets of the same prefixes.
This new mechanism can be made configurable if desired but it's always enabled for now.
---
There's some renaming in this patch that makes it much bigger.
ell/netconfig.c | 183 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 122 insertions(+), 61 deletions(-)
ell/netconfig.c +122 -61
@@ -80,6 +80,7 @@ struct l_netconfig {struct l_icmp6_client *icmp6_client;struct l_dhcp6_client *dhcp6_client;struct l_idle *signal_expired_work; +struct l_queue *icmp_route_data;/* These objects, if not NULL, are owned by @addresses and @routes */ struct l_rtnl_address *v4_address;
@@ -117,6 +118,13 @@ struct l_netconfig { } handler; }; +struct netconfig_route_data { +struct l_rtnl_route *route; +uint64_t last_ra_time; +uint64_t kernel_expiry; +uint64_t max_ra_interval; +}; +union netconfig_addr {struct in_addr v4;struct in6_addr v6;
@@ -647,10 +655,9 @@ 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); +struct netconfig_route_data *rd = data; -if (!expiry_time || now < expiry_time) +if (!rd->kernel_expiry || now < rd->kernel_expiry)return false;/*
@@ -664,11 +671,12 @@ static bool netconfig_check_route_expired(void *data, void *user_data)* 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); +if (!l_queue_remove (nc->routes.added, rd->route)) +l_queue_push_tail (nc->routes.expired, rd->route); -l_queue_remove (nc->routes.updated, route); -l_queue_remove (nc->routes.removed, route); +l_queue_remove (nc->routes.current, rd->route); +l_queue_remove (nc->routes.updated, rd->route); +l_queue_remove (nc->routes.removed, rd->route);return true; }
@@ -676,7 +684,7 @@ static void netconfig_expire_routes(struct l_netconfig *nc) { now =l_time_now (); -if (l_queue_foreach_remove (nc->routes.current, +if (l_queue_foreach_remove (nc->icmp_route_data, netconfig_check_route_expired, nc) && !l_queue_isempty (nc->routes.expired) && !nc->signal_expired_work)
@@ -685,31 +693,27 @@ static void netconfig_expire_routes(struct l_netconfig *nc) nc, NULL); } -static struct l_rtnl_route *netconfig_find_icmp6_route ( +static struct netconfig_route_data *netconfig_find_icmp6_route (struct l_netconfig *nc,const uint8_t *gateway,const struct route_info *dst) {const struct l_queue_entry *entry; -for (entry =l_queue_get_entries (nc->routes.current); entry; +for (entry =l_queue_get_entries (nc->icmp_route_data); entry; entry = entry->next) { -struct l_rtnl_route *route = entry->data; +struct netconfig_route_data *rd = entry->data;const uint8_t *route_gateway;const uint8_t *route_dst;uint8_t route_prefix_len =0 ; -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); + route_gateway =l_rtnl_route_get_gateway_in_addr (rd->route);if ((gateway || route_gateway) && (!gateway || !route_gateway ||memcmp (gateway, route_gateway,16 )))continue ; - route_dst =l_rtnl_route_get_dst_in_addr (route, + route_dst =l_rtnl_route_get_dst_in_addr (rd->route, &route_prefix_len); if ((dst || route_prefix_len) && (!dst || !route_prefix_len ||
@@ -717,17 +721,19 @@ static struct l_rtnl_route *netconfig_find_icmp6_route( memcmp(dst->address, route_dst, 16)))continue ; -return route; +return rd; }return NULL ; } -static struct l_rtnl_route *netconfig_add_icmp6_route (struct l_netconfig *nc, +static struct netconfig_route_data *netconfig_add_icmp6_route ( +struct l_netconfig *nc,const uint8_t *gateway,const struct route_info *dst,uint8_t preference) { +struct netconfig_route_data *rd;struct l_rtnl_route *rt; rt = netconfig_route_new(nc, AF_INET6, dst->address, dst->prefix_len,
@@ -738,25 +744,73 @@ static struct l_rtnl_route *netconfig_add_icmp6_route(struct l_netconfig *nc,l_rtnl_route_set_preference (rt, preference);l_queue_push_tail (nc->routes.current, rt);l_queue_push_tail (nc->routes.added, rt); -return rt; + + rd = l_new(struct netconfig_route_data, 1); + rd->route = rt; +l_queue_push_tail (nc->icmp_route_data, rd); +return rd; +} + +static bool netconfig_check_route_need_update ( +const struct netconfig_route_data *rd, +const struct l_icmp6_router *ra, +uint64_t new_expiry, +uint64_t old_expiry) +{ +/* +* Decide whether the route is close enough to its expiry time that, +* based on the expected Router Advertisement frequency, we should +* notify the user and have them update the route's lifetime in the +* kernel. This is an optimization to avoid triggering a syscall and +* potentially multiple context-switches in case we expect to have +* many more opportunities to update the lifetime before we even get +* close to the last expiry time we passed to the kernel. Without +* this we might be wasting a lot of cycles over time if the RAs are +* frequent. +* +* Always update if we have no RA interval information or if the +* expiry is moved forward. +*/ +if (!rd->max_ra_interval || new_expiry < rd->kernel_expiry) +return true; + +return rd->kernel_expiry < ra->start_time + rd->max_ra_interval *10 ; }static void netconfig_set_icmp6_route_data (struct l_netconfig *nc, -struct l_rtnl_route *rt, -uint64_t start_time, +struct netconfig_route_data *rd, +const struct l_icmp6_router *ra,uint32_t preferred_lifetime,uint32_t valid_lifetime,uint32_t mtu,bool updated) { -uint64_t expiry = start_time + valid_lifetime *L_USEC_PER_SEC ; -uint64_t old_expiry =l_rtnl_route_get_expiry (rt); +uint64_t expiry = ra->start_time + valid_lifetime *L_USEC_PER_SEC ; +uint64_t old_expiry =l_rtnl_route_get_expiry (rd->route);bool differs = false; -if (mtu !=l_rtnl_route_get_mtu (rt)) { -l_rtnl_route_set_mtu (rt, mtu); +if (mtu !=l_rtnl_route_get_mtu (rd->route)) { +l_rtnl_route_set_mtu (rd->route, mtu); differs = true; } +/* +* The route's lifetime is pretty useless on its own but keep it +* updated with the value from the last RA. Routers can send the same +* lifetime in every RA, keep decreasing the lifetimes linearly or +* implement any other policy, regardless of whether the resulting +* expiry time varies or not. +*/ +l_rtnl_route_set_lifetime (rd->route, valid_lifetime); + +if (rd->last_ra_time) { +uint64_t interval = ra->start_time - rd->last_ra_time; + +if (interval > rd->max_ra_interval) + rd->max_ra_interval = interval; + } + + rd->last_ra_time = ra->start_time; +/* * valid_lifetime of 0 from a route_info means the route is being * removed so we wouldn't be here. valid_lifetime of 0xffffffff
@@ -777,23 +831,29 @@ static void netconfig_set_icmp6_route_data(struct l_netconfig *nc,if ((expiry || old_expiry) && (!expiry || !old_expiry ||l_time_diff (expiry, old_expiry) >L_USEC_PER_SEC )) { -l_rtnl_route_set_lifetime (rt, valid_lifetime); -l_rtnl_route_set_expiry (rt, expiry); - differs = true; +l_rtnl_route_set_expiry (rd->route, expiry); + + differs = differs || !expiry || !old_expiry || +netconfig_check_route_need_update (rd, ra, + expiry, old_expiry); } -if (updated && differs && !netconfig_route_exists (nc->routes.added, rt)) -l_queue_push_tail (nc->routes.updated, rt); +if (updated && differs && !netconfig_route_exists (nc->routes.added, + rd->route)) { +l_queue_push_tail (nc->routes.updated, rd->route); + rd->kernel_expiry = expiry; + } }static void netconfig_remove_icmp6_route (struct l_netconfig *nc, -struct l_rtnl_route *route) +struct netconfig_route_data *rd) { -l_queue_remove (nc->routes.current, route); -l_queue_remove (nc->routes.updated, route); +l_queue_remove (nc->icmp_route_data, rd); +l_queue_remove (nc->routes.current, rd->route); +l_queue_remove (nc->routes.updated, rd->route); -if (!l_queue_remove (nc->routes.added, route)) -l_queue_push_tail (nc->routes.removed, route); +if (!l_queue_remove (nc->routes.added, rd->route)) +l_queue_push_tail (nc->routes.removed, rd->route); } static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
@@ -803,7 +863,7 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client, {struct l_netconfig *nc = user_data;const struct l_icmp6_router *r; -struct l_rtnl_route *default_route; +struct netconfig_route_data *default_rd;unsigned int i;if (event !=L_ICMP6_CLIENT_EVENT_ROUTER_FOUND )
@@ -823,12 +883,12 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,netconfig_expire_routes (nc);/* Process the default gateway information */ - default_route =netconfig_find_icmp6_route (nc, r->address,NULL ); + default_rd =netconfig_find_icmp6_route (nc, r->address,NULL ); -if (!default_route && r->lifetime) { - default_route =netconfig_add_icmp6_route (nc, r->address,NULL , +if (!default_rd && r->lifetime) { + default_rd =netconfig_add_icmp6_route (nc, r->address,NULL , r->pref); -if (unlikely (!default_route)) +if (unlikely (!default_rd))return ;/*
@@ -836,15 +896,13 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,* confused for the special 0xffffffff value in * netconfig_set_icmp6_route_data. */ -netconfig_set_icmp6_route_data (nc, default_route, r->start_time, - r->lifetime, r->lifetime, - r->mtu, false); - }else if (default_route && r->lifetime) -netconfig_set_icmp6_route_data (nc, default_route, r->start_time, - r->lifetime, r->lifetime, - r->mtu, true); -else if (default_route && !r->lifetime) -netconfig_remove_icmp6_route (nc, default_route); +netconfig_set_icmp6_route_data (nc, default_rd, r, r->lifetime, + r->lifetime, r->mtu, false); + }else if (default_rd && r->lifetime) +netconfig_set_icmp6_route_data (nc, default_rd, r, r->lifetime, + r->lifetime, r->mtu, true); +else if (default_rd && !r->lifetime) +netconfig_remove_icmp6_route (nc, default_rd);/* * Process the onlink and offlink routes, from the Router
@@ -854,26 +912,26 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,for (i =0 ; i < r->n_routes; i++) {const struct route_info *info = &r->routes[i];const uint8_t *gateway = info->onlink ?NULL : r->address; -struct l_rtnl_route *route = +struct netconfig_route_data *rd =netconfig_find_icmp6_route (nc, gateway, info); -if (!route && info->valid_lifetime) { - route =netconfig_add_icmp6_route (nc, gateway, info, +if (!rd && info->valid_lifetime) { + rd =netconfig_add_icmp6_route (nc, gateway, info, info->preference); -if (unlikely (!route)) +if (unlikely (!rd))continue ; -netconfig_set_icmp6_route_data (nc, route, r->start_time, +netconfig_set_icmp6_route_data (nc, rd, r, info->preferred_lifetime, info->valid_lifetime, gateway ? r->mtu :0 , false); - }else if (route && info->valid_lifetime) -netconfig_set_icmp6_route_data (nc, route, r->start_time, + }else if (rd && info->valid_lifetime) +netconfig_set_icmp6_route_data (nc, rd, r, info->preferred_lifetime, info->valid_lifetime, gateway ? r->mtu :0 , true); -else if (route && !info->valid_lifetime) -netconfig_remove_icmp6_route (nc, route); +else if (rd && !info->valid_lifetime) +netconfig_remove_icmp6_route (nc, rd); }/*
@@ -938,6 +996,7 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) nc->routes.added =l_queue_new (); nc->routes.updated =l_queue_new (); nc->routes.removed =l_queue_new (); + nc->icmp_route_data =l_queue_new (); nc->dhcp_client =l_dhcp_client_new (ifindex); l_dhcp_client_set_event_handler(nc->dhcp_client,
@@ -987,6 +1046,7 @@ LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig)l_queue_destroy (netconfig->routes.added,NULL );l_queue_destroy (netconfig->routes.updated,NULL );l_queue_destroy (netconfig->routes.removed,NULL ); +l_queue_destroy (netconfig->icmp_route_data,NULL );l_free (netconfig); rtnl_use_count--;
@@ -1523,10 +1583,11 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig)netconfig_addr_wait_unregister (netconfig, false);netconfig_update_cleanup (netconfig); -l_queue_clear (netconfig->routes.current, - (l_queue_destroy_func_t ) l_rtnl_route_free);l_queue_clear (netconfig->addresses.current, (l_queue_destroy_func_t ) l_rtnl_address_free); +l_queue_clear (netconfig->routes.current, + (l_queue_destroy_func_t ) l_rtnl_route_free); +l_queue_clear (netconfig->icmp_route_data, l_free); netconfig->v4_address =NULL ; netconfig->v4_subnet_route =NULL ; netconfig->v4_default_route =NULL ;
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 15/17 netconfig: Set a limit on number of routes from ICMPv6
In reply to Andrew ZaborowskiWhile some networks can be configured in a way that prevents rogue Router Advertisements from reaching clients, add a very basic mechanism to avoid a denial-of-service in such a case by limiting the number of routes we track from RAs. This won't prevent the loss of routing but might prevent OOM crashes or slowdowns.
--- ell/netconfig.c | 5 +++++ 1 file changed, 5 insertions(+)
ell/netconfig.c +5 -0
@@ -135,6 +135,8 @@ static unsigned int rtnl_use_count;static struct l_queue *addr_wait_list;static unsigned int rtnl_id; +static const unsigned int max_icmp6_routes =100 ; +static void netconfig_update_cleanup (struct l_netconfig *nc) {l_queue_clear (nc->addresses.added,NULL );
@@ -736,6 +738,9 @@ static struct netconfig_route_data *netconfig_add_icmp6_route(struct netconfig_route_data *rd;struct l_rtnl_route *rt; +if (l_queue_length (nc->icmp_route_data) >= max_icmp6_routes) +return NULL ;/* TODO: log a warning the first time */ + rt =netconfig_route_new (nc,AF_INET6 , dst->address, dst->prefix_len, gateway,RTPROT_RA );if (L_WARN_ON (!rt))
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 16/17 examples: Update netconfig method calls
In reply to Andrew Zaborowski--- examples/netconfig-test.c | 49 ++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 29 deletions(-)
examples/netconfig-test.c +20 -29
@@ -40,7 +40,6 @@#include <ell/ell.h> static bool apply; -static struct l_netlink *rtnl;static void do_debug (const char *str,void *user_data) {
@@ -61,16 +60,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 +80,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 +108,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,18 +126,21 @@ 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); +l_netconfig_apply_rtnl (netconfig); }static const struct option main_options[] = {
@@ -181,14 +182,6 @@ int main(int argc, char *argv[])if (!l_main_init ())return EXIT_FAILURE ; -if (apply) { - rtnl =l_netlink_new (NETLINK_ROUTE ); -if (!rtnl) { -fprintf (stderr,"l_netlink_new(NETLINK_ROUTE) error\n" ); -return EXIT_FAILURE ; - } - } -l_log_set_stderr ();l_debug_enable ("*" );
@@ -203,7 +196,5 @@ int main(int argc, char *argv[])l_netconfig_destroy (netconfig);l_main_exit (); -l_netlink_destroy (rtnl); -return EXIT_SUCCESS ; }
Signature · 1 line
2.32.0
Andrew Zaborowski
PATCH 17/17 icmp6: Ignore bad prefixes per RFC4862
In reply to Andrew Zaborowski"Silently ignore" Prefix Information options with a preferred time longer than valid time and those matching the link-local prefix as mandated by RFC4862 Section 5.5.3. These prefixes are also invalid according to RFC4861.
--- ell/icmp6.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
ell/icmp6.c +30 -2
@@ -45,6 +45,7 @@#include "time-private.h" #include "queue.h" #include "net.h" +#include "net-private.h" #include "netlink.h" #include "rtnl.h" #include "missing.h"
@@ -817,7 +818,27 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra, i->onlink = true; i->valid_lifetime =l_get_be32 (opts +4 ); i->preferred_lifetime =l_get_be32 (opts +8 ); -memcpy (i->address, opts +16 ,16 ); + +/* +* Only the initial Prefix Length bits of the prefix +* are valid. The remaining bits "MUST" be ignored +* by the receiver. +*/ +memcpy (i->address,net_prefix_from_ipv6 (opts +16 , + i->prefix_len),16 ); + +/* +* For SLAAC (RFC4862) we need to "silently ignore" +* routes with a preferred lifetime longer than valid +* lifetime, and those with the link-local prefix. +* Since it makes sense, do it regardless of SLAAC. +*/ +if (i->preferred_lifetime > i->valid_lifetime) +break ; + +if (i->prefix_len >=10 && +IN6_IS_ADDR_LINKLOCAL (i->address)) +break ; n_routes +=1 ;break ;
@@ -869,7 +890,14 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra, i->onlink = false; i->preference = preference; i->valid_lifetime =l_get_be32 (opts +4 ); -memcpy (i->address, opts +8 , (i->prefix_len +7 ) /8 ); + +/* +* Only the initial Prefix Length bits of the prefix +* are valid. The remaining bits "MUST" be ignored +* by the receiver. +*/ +memcpy (i->address,net_prefix_from_ipv6 (opts +8 , + i->prefix_len),16 ); n_routes +=1 ;break ;
Signature · 1 line
2.32.0
Denis Kenzior
AppliedRe: [PATCH 17/17] icmp6: Ignore bad prefixes per RFC4862
Hi Andrew,
Quoted 8 lines · Andrew Zaborowski
"Silently ignore" Prefix Information options with a preferred time longer than valid time and those matching the link-local prefix as mandated by RFC4862 Section 5.5.3. These prefixes are also invalid according to RFC4861.
--- ell/icmp6.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
Applied, thanks.
Regards,
-Denis