From 95c3df3ccf216f9ed5cfcb54f1bc31fe9b7db74c Mon Sep 17 00:00:00 2001 From: Tim Kourt Date: Tue, 1 Oct 2019 16:44:24 -0700 Subject: [PATCH] netconfig: Install IPv6 default route --- src/netconfig.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/netconfig.c b/src/netconfig.c index e1ea33e4..b2389fed 100644 --- a/src/netconfig.c +++ b/src/netconfig.c @@ -286,6 +286,37 @@ proceed: return NULL; } +static char *netconfig_ipv6_get_gateway(struct netconfig *netconfig) +{ + struct in6_addr in6_addr; + char *gateway; + + switch (netconfig->rtm_v6_protocol) { + case RTPROT_STATIC: + gateway = l_settings_get_string(netconfig->active_settings, + "IPv6", "gateway"); + + if (inet_pton(AF_INET6, gateway, &in6_addr) < 1) { + l_error("netconfig: Invalid IPv6 gateway address %s is " + "provided in network configuration file.", + gateway); + + l_free(gateway); + + return NULL; + } + + return gateway; + + case RTPROT_DHCP: + /* TODO */ + + return NULL; + } + + return NULL; +} + static bool netconfig_ifaddr_match(const void *a, const void *b) { const struct netconfig_ifaddr *entry = a; @@ -603,17 +634,52 @@ done: netconfig_ifaddr_destroy(ifaddr); } +static bool netconfig_ipv6_routes_install(struct netconfig *netconfig) +{ + L_AUTO_FREE_VAR(char *, gateway) = NULL; + + gateway = netconfig_ipv6_get_gateway(netconfig); + if (!gateway) { + l_error("netconfig: Failed to obtain gateway from %s.", + netconfig->rtm_v6_protocol == RTPROT_STATIC ? + "settings file" : "DHCPv6 lease"); + + return false; + } + + if (!rtnl_route_ipv6_add_gateway(rtnl, netconfig->ifindex, gateway, + ROUTE_PRIORITY_OFFSET, + netconfig->rtm_v6_protocol, + netconfig_route_cmd_cb, + NULL, NULL)) { + l_error("netconfig: Failed to add route for: %s gateway.", + gateway); + + return false; + } + + return true; +} + static void netconfig_ipv6_ifaddr_add_cmd_cb(int error, uint16_t type, const void *data, uint32_t len, void *user_data) { + struct netconfig *netconfig = user_data; + if (error && error != -EEXIST) { l_error("netconfig: Failed to add IPv6 address. " "Error %d: %s", error, strerror(-error)); return; } - /* TODO Install routes and DNS */ + if (!netconfig_ipv6_routes_install(netconfig)) { + l_error("netconfig: Failed to install IPv6 routes."); + + return; + } + + /* TODO Install DNS */ } static void netconfig_install_address(struct netconfig *netconfig,