eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

netconfig.c (21126B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <errno.h>
     29 #include <arpa/inet.h>
     30 #include <net/if_arp.h>
     31 #include <netinet/in.h>
     32 #include <limits.h>
     33 #include <string.h>
     34 #include <fcntl.h>
     35 #include <sys/param.h>
     36 #include <sys/types.h>
     37 #include <sys/stat.h>
     38 #include <unistd.h>
     39 
     40 #include <ell/ell.h>
     41 
     42 #include "ell/useful.h"
     43 #include "src/iwd.h"
     44 #include "src/module.h"
     45 #include "src/netdev.h"
     46 #include "src/station.h"
     47 #include "src/common.h"
     48 #include "src/network.h"
     49 #include "src/resolve.h"
     50 #include "src/util.h"
     51 #include "src/ie.h"
     52 #include "src/netconfig.h"
     53 #include "src/sysfs.h"
     54 
     55 #define DHCP_ATTEMPTS 4
     56 
     57 /*
     58  * Routing priority offset, configurable in main.conf. The route with lower
     59  * priority offset is preferred.
     60  */
     61 static uint32_t ROUTE_PRIORITY_OFFSET;
     62 static bool ipv6_enabled;
     63 static char *mdns_global;
     64 
     65 static void do_debug(const char *str, void *user_data)
     66 {
     67 	const char *prefix = user_data;
     68 
     69 	l_info("%s%s", prefix, str);
     70 }
     71 
     72 static void netconfig_free_settings(struct netconfig *netconfig)
     73 {
     74 	netconfig->enabled[0] = true;
     75 	netconfig->enabled[1] = false;
     76 	netconfig->static_config[0] = false;
     77 	netconfig->static_config[1] = false;
     78 	netconfig->gateway_overridden[0] = false;
     79 	netconfig->gateway_overridden[1] = false;
     80 	netconfig->dns_overridden[0] = false;
     81 	netconfig->dns_overridden[1] = false;
     82 	l_netconfig_reset_config(netconfig->nc);
     83 
     84 	l_free(netconfig->mdns);
     85 	netconfig->mdns = NULL;
     86 
     87 	l_free(l_steal_ptr(netconfig->fils_override));
     88 }
     89 
     90 static void netconfig_free(void *data)
     91 {
     92 	struct netconfig *netconfig = data;
     93 
     94 	l_netconfig_destroy(netconfig->nc);
     95 	l_free(netconfig);
     96 }
     97 
     98 static bool netconfig_addr_to_str(uint8_t af, const void *v4_addr,
     99 					const void *v6_addr, char *out_str,
    100 					bool *out_is_zero)
    101 {
    102 	const void *addr = (af == AF_INET ? v4_addr : v6_addr);
    103 	uint8_t bytes = (af == AF_INET ? 4 : 16);
    104 
    105 	if (l_memeqzero(addr, bytes)) {
    106 		*out_is_zero = true;
    107 		return true;
    108 	}
    109 
    110 	*out_is_zero = false;
    111 
    112 	if (L_WARN_ON(!inet_ntop(af, addr, out_str, INET6_ADDRSTRLEN)))
    113 		return false;
    114 
    115 	return true;
    116 }
    117 
    118 bool netconfig_use_fils_addr(struct netconfig *netconfig, int af)
    119 {
    120 	if (!netconfig->enabled[INDEX_FOR_AF(af)])
    121 		return false;
    122 
    123 	if (netconfig->static_config[INDEX_FOR_AF(af)])
    124 		return false;
    125 
    126 	if (!netconfig->fils_override)
    127 		return false;
    128 
    129 	if (af == AF_INET)
    130 		return !!netconfig->fils_override->ipv4_addr;
    131 
    132 	return !l_memeqzero(netconfig->fils_override->ipv6_addr, 16);
    133 }
    134 
    135 static struct l_rtnl_address *netconfig_get_static4_address(
    136 				const struct l_settings *active_settings)
    137 {
    138 	_auto_(l_rtnl_address_free) struct l_rtnl_address *ifaddr = NULL;
    139 	L_AUTO_FREE_VAR(char *, ip) = NULL;
    140 	L_AUTO_FREE_VAR(char *, netmask) = NULL;
    141 	struct in_addr in_addr;
    142 	L_AUTO_FREE_VAR(char *, broadcast) = NULL;
    143 	uint32_t prefix_len;
    144 
    145 	ip = l_settings_get_string(active_settings, "IPv4", "Address");
    146 	if (unlikely(!ip)) {
    147 		l_error("netconfig: Can't load IPv4.Address");
    148 		return NULL;
    149 	}
    150 
    151 	if (l_settings_has_key(active_settings, "IPv4", "Netmask") &&
    152 			!(netmask = l_settings_get_string(active_settings,
    153 								"IPv4",
    154 								"Netmask"))) {
    155 		l_error("netconfig: Can't load IPv4.Netmask");
    156 		return NULL;
    157 	}
    158 
    159 	if (netmask) {
    160 		if (inet_pton(AF_INET, netmask, &in_addr) != 1) {
    161 			l_error("netconfig: Can't parse IPv4.Netmask");
    162 			return NULL;
    163 		}
    164 
    165 		prefix_len = __builtin_popcountl(in_addr.s_addr);
    166 
    167 		if (ntohl(in_addr.s_addr) !=
    168 				util_netmask_from_prefix(prefix_len)) {
    169 			l_error("netconfig: Invalid IPv4.Netmask");
    170 			return NULL;
    171 		}
    172 	} else
    173 		prefix_len = 24;
    174 
    175 	ifaddr = l_rtnl_address_new(ip, prefix_len);
    176 	if (!ifaddr || l_rtnl_address_get_family(ifaddr) != AF_INET) {
    177 		l_error("netconfig: Unable to parse IPv4.Address");
    178 		return NULL;
    179 	}
    180 
    181 	broadcast = l_settings_get_string(active_settings, "IPv4", "Broadcast");
    182 	if (broadcast && !l_rtnl_address_set_broadcast(ifaddr, broadcast)) {
    183 		l_error("netconfig: Unable to parse IPv4.Broadcast");
    184 		return NULL;
    185 	}
    186 
    187 	return l_steal_ptr(ifaddr);
    188 }
    189 
    190 static struct l_rtnl_address *netconfig_get_static6_address(
    191 				const struct l_settings *active_settings)
    192 {
    193 	L_AUTO_FREE_VAR(char *, ip);
    194 	char *p;
    195 	char *endp;
    196 	_auto_(l_rtnl_address_free) struct l_rtnl_address *ret = NULL;
    197 	uint32_t prefix_len = 64;
    198 
    199 	ip = l_settings_get_string(active_settings, "IPv6", "Address");
    200 	if (unlikely(!ip)) {
    201 		l_error("netconfig: Can't load IPv6.Address");
    202 		return NULL;
    203 	}
    204 
    205 	p = strrchr(ip, '/');
    206 	if (!p)
    207 		goto no_prefix_len;
    208 
    209 	*p = '\0';
    210 	if (*++p == '\0')
    211 		goto no_prefix_len;
    212 
    213 	errno = 0;
    214 	prefix_len = strtoul(p, &endp, 10);
    215 	if (unlikely(*endp != '\0' || errno ||
    216 			!prefix_len || prefix_len > 128)) {
    217 		l_error("netconfig: Invalid prefix '%s' provided in network"
    218 				" configuration file", p);
    219 		return NULL;
    220 	}
    221 
    222 no_prefix_len:
    223 	ret = l_rtnl_address_new(ip, prefix_len);
    224 	if (!ret || l_rtnl_address_get_family(ret) != AF_INET6) {
    225 		l_error("netconfig: Invalid IPv6 address %s provided in "
    226 			"network configuration file.", ip);
    227 		return NULL;
    228 	}
    229 
    230 	return l_steal_ptr(ret);
    231 }
    232 
    233 static bool netconfig_load_dns(struct netconfig *netconfig,
    234 				const struct l_settings *active_settings,
    235 				const char *group_name, uint8_t family)
    236 {
    237 	_auto_(l_strv_free) char **dns_str_list = NULL;
    238 
    239 	if (!l_settings_has_key(active_settings, group_name, "DNS"))
    240 		return true;
    241 
    242 	dns_str_list = l_settings_get_string_list(active_settings,
    243 							group_name, "DNS", ' ');
    244 	if (unlikely(!dns_str_list)) {
    245 		l_error("netconfig: Can't load [%s].DNS", group_name);
    246 		return false;
    247 	}
    248 
    249 	if (unlikely(!l_netconfig_set_dns_override(netconfig->nc, family,
    250 							dns_str_list))) {
    251 		l_error("netconfig: l_netconfig_set_dns_override(%s) failed",
    252 			family == AF_INET ? "AF_INET" : "AF_INET6");
    253 		return false;
    254 	}
    255 
    256 	netconfig->dns_overridden[INDEX_FOR_AF(family)] = true;
    257 	return true;
    258 }
    259 
    260 static bool netconfig_load_gateway(struct netconfig *netconfig,
    261 				const struct l_settings *active_settings,
    262 				const char *group_name, uint8_t family)
    263 {
    264 	_auto_(l_free) char *gateway_str = NULL;
    265 
    266 	if (!l_settings_has_key(active_settings, group_name, "Gateway"))
    267 		return true;
    268 
    269 	gateway_str = l_settings_get_string(active_settings, group_name,
    270 						"Gateway");
    271 	if (unlikely(!gateway_str)) {
    272 		l_error("netconfig: Can't load [%s].Gateway", group_name);
    273 		return false;
    274 	}
    275 
    276 	if (unlikely(!l_netconfig_set_gateway_override(netconfig->nc, family,
    277 							gateway_str))) {
    278 		l_error("netconfig: l_netconfig_set_gateway_override(%s) "
    279 			"failed", family == AF_INET ? "AF_INET" : "AF_INET6");
    280 		return false;
    281 	}
    282 
    283 	netconfig->gateway_overridden[INDEX_FOR_AF(family)] = true;
    284 	return true;
    285 }
    286 
    287 bool netconfig_load_settings(struct netconfig *netconfig,
    288 				const struct l_settings *active_settings)
    289 {
    290 	bool send_hostname = false;
    291 	char hostname[HOST_NAME_MAX + 1];
    292 	_auto_(l_free) char *mdns = NULL;
    293 	bool success = true;
    294 	bool static_ipv4 = false;
    295 	bool static_ipv6 = false;
    296 	bool enable_ipv4 = true;
    297 	bool enable_ipv6 = ipv6_enabled;
    298 
    299 	netconfig_free_settings(netconfig);
    300 
    301 	/*
    302 	 * Note we try to print errors and continue validating the
    303 	 * configuration until we've gone through all the settings so
    304 	 * as to make fixing the settings more efficient for the user.
    305 	 */
    306 
    307 	if (l_settings_has_key(active_settings, "IPv4", "Address")) {
    308 		_auto_(l_rtnl_address_free) struct l_rtnl_address *addr =
    309 			netconfig_get_static4_address(active_settings);
    310 
    311 		if (unlikely(!addr)) {
    312 			success = false;
    313 			goto ipv6_addr;
    314 		}
    315 
    316 		if (!l_netconfig_set_static_addr(netconfig->nc, AF_INET,
    317 							addr)) {
    318 			l_error("netconfig: l_netconfig_set_static_addr("
    319 				"AF_INET) failed");
    320 			success = false;
    321 			goto ipv6_addr;
    322 		}
    323 
    324 		static_ipv4 = true;
    325 	}
    326 
    327 ipv6_addr:
    328 	if (l_settings_has_key(active_settings, "IPv6", "Address")) {
    329 		_auto_(l_rtnl_address_free) struct l_rtnl_address *addr =
    330 			netconfig_get_static6_address(active_settings);
    331 
    332 		if (unlikely(!addr)) {
    333 			success = false;
    334 			goto gateway;
    335 		}
    336 
    337 		if (!l_netconfig_set_static_addr(netconfig->nc, AF_INET6,
    338 							addr)) {
    339 			l_error("netconfig: l_netconfig_set_static_addr("
    340 				"AF_INET6) failed");
    341 			success = false;
    342 			goto gateway;
    343 		}
    344 
    345 		static_ipv6 = true;
    346 	}
    347 
    348 gateway:
    349 	if (!netconfig_load_gateway(netconfig, active_settings,
    350 					"IPv4", AF_INET))
    351 		success = false;
    352 
    353 	if (!netconfig_load_gateway(netconfig, active_settings,
    354 					"IPv6", AF_INET6))
    355 		success = false;
    356 
    357 	if (!netconfig_load_dns(netconfig, active_settings, "IPv4", AF_INET))
    358 		success = false;
    359 
    360 	if (!netconfig_load_dns(netconfig, active_settings, "IPv6", AF_INET6))
    361 		success = false;
    362 
    363 	if (l_settings_has_key(active_settings, "IPv6", "Enabled") &&
    364 			!l_settings_get_bool(active_settings, "IPv6", "Enabled",
    365 						&enable_ipv6)) {
    366 		l_error("netconfig: Can't load IPv6.Enabled");
    367 		success = false;
    368 		goto send_hostname;
    369 	}
    370 
    371 	if (!l_netconfig_set_family_enabled(netconfig->nc, AF_INET,
    372 						enable_ipv4) ||
    373 			!l_netconfig_set_family_enabled(netconfig->nc, AF_INET6,
    374 							enable_ipv6)) {
    375 		l_error("netconfig: l_netconfig_set_family_enabled() failed");
    376 		success = false;
    377 	}
    378 
    379 send_hostname:
    380 	if (l_settings_has_key(active_settings, "IPv4", "SendHostname") &&
    381 			!l_settings_get_bool(active_settings, "IPv4",
    382 						"SendHostname",
    383 						&send_hostname)) {
    384 		l_error("netconfig: Can't load [IPv4].SendHostname");
    385 		success = false;
    386 		goto mdns;
    387 	}
    388 
    389 	if (send_hostname && gethostname(hostname, sizeof(hostname)) != 0) {
    390 		/* Warning only */
    391 		l_warn("netconfig: Unable to get hostname. "
    392 			"Error %d: %s", errno, strerror(errno));
    393 		goto mdns;
    394 	}
    395 
    396 	if (send_hostname &&
    397 			!l_netconfig_set_hostname(netconfig->nc, hostname)) {
    398 		l_error("netconfig: l_netconfig_set_hostname() failed");
    399 		success = false;
    400 		goto mdns;
    401 	}
    402 
    403 mdns:
    404 	/* If the networks has this set take that over the global */
    405 	if (l_settings_has_key(active_settings, "Network", "MulticastDNS")) {
    406 		mdns = l_settings_get_string(active_settings, "Network",
    407 							"MulticastDNS");
    408 		if (!mdns) {
    409 			l_error("netconfig: Can't load Network.MulticastDNS");
    410 			success = false;
    411 		}
    412 
    413 		if (mdns && !L_IN_STRSET(mdns, "true", "false", "resolve")) {
    414 			l_error("netconfig: Bad profile Network.MulticastDNS "
    415 				"value '%s'", mdns);
    416 			success = false;
    417 		}
    418 
    419 		if (!success)
    420 			goto route_priority;
    421 	}
    422 
    423 	if (!mdns && mdns_global) {
    424 		mdns = l_strdup(mdns_global);
    425 
    426 		if (!L_IN_STRSET(mdns, "true", "false", "resolve")) {
    427 			l_error("netconfig: Bad global Network.MulticastDNS "
    428 				"value '%s'", mdns);
    429 			success = false;
    430 		}
    431 	}
    432 
    433 route_priority:
    434 	l_netconfig_set_route_priority(netconfig->nc, ROUTE_PRIORITY_OFFSET);
    435 	l_netconfig_set_optimistic_dad_enabled(netconfig->nc, true);
    436 
    437 	if (!l_netconfig_check_config(netconfig->nc)) {
    438 		l_error("netconfig: Invalid configuration");
    439 		success = false;
    440 	}
    441 
    442 	if (success) {
    443 		netconfig->active_settings = active_settings;
    444 		netconfig->static_config[INDEX_FOR_AF(AF_INET)] = static_ipv4;
    445 		netconfig->static_config[INDEX_FOR_AF(AF_INET6)] = static_ipv6;
    446 		netconfig->enabled[INDEX_FOR_AF(AF_INET)] = enable_ipv4;
    447 		netconfig->enabled[INDEX_FOR_AF(AF_INET6)] = enable_ipv6;
    448 		netconfig->mdns = l_steal_ptr(mdns);
    449 		return true;
    450 	}
    451 
    452 	l_netconfig_reset_config(netconfig->nc);
    453 	return false;
    454 }
    455 
    456 static bool netconfig_load_fils_settings(struct netconfig *netconfig,
    457 						uint8_t af)
    458 {
    459 	struct ie_fils_ip_addr_response_info *fils = netconfig->fils_override;
    460 	char addr_str[INET6_ADDRSTRLEN];
    461 	char gw_addr_str[INET6_ADDRSTRLEN];
    462 	char dns_addr_str[INET6_ADDRSTRLEN];
    463 	_auto_(l_rtnl_address_free) struct l_rtnl_address *rtnl_addr = NULL;
    464 	bool is_zero = false;
    465 	uint8_t prefix_len;
    466 
    467 	if (!netconfig_addr_to_str(af, &fils->ipv4_addr, &fils->ipv6_addr,
    468 					addr_str, &is_zero) || is_zero)
    469 		return is_zero;
    470 
    471 	prefix_len = (af == AF_INET ? fils->ipv4_prefix_len :
    472 			fils->ipv6_prefix_len);
    473 
    474 	if (L_WARN_ON(!(rtnl_addr = l_rtnl_address_new(addr_str, prefix_len))))
    475 		return false;
    476 
    477 	if (L_WARN_ON(!l_netconfig_set_static_addr(netconfig->nc, af,
    478 							rtnl_addr)))
    479 		return false;
    480 
    481 	if (af == AF_INET &&
    482 			L_WARN_ON(!l_netconfig_set_acd_enabled(netconfig->nc,
    483 								false)))
    484 		return false;
    485 
    486 	/*
    487 	 * Done with local address, move on to gateway and DNS.
    488 	 *
    489 	 * Since load_settings is called early, generally before the actual
    490 	 * connection setup starts, and load_fils_settings is called after
    491 	 * 802.11 Authentication & Association, we need to check if either
    492 	 * the gateway or DNS settings were overridden in load_settings so
    493 	 * as not to overwrite the user-provided values.  Values received
    494 	 * with FILS are expected to have the same weight as those from
    495 	 * DHCP/SLAAC.
    496 	 *
    497 	 * TODO: If netconfig->fils_override->ipv{4,6}_lifetime is set,
    498 	 * start a timeout to renew the address using FILS IP Address
    499 	 * Assignment or perhaps just start the DHCP client after that
    500 	 * time.
    501 	 *
    502 	 * TODO: validate gateway and/or DNS on local subnet, link-local,
    503 	 * etc.?
    504 	 */
    505 
    506 	if (!netconfig_addr_to_str(af, &fils->ipv4_gateway, &fils->ipv6_gateway,
    507 					gw_addr_str, &is_zero))
    508 		return false;
    509 
    510 	if (!netconfig->gateway_overridden[INDEX_FOR_AF(af)] && !is_zero &&
    511 			L_WARN_ON(!l_netconfig_set_gateway_override(
    512 								netconfig->nc,
    513 								af,
    514 								gw_addr_str)))
    515 		return false;
    516 
    517 	if (!netconfig_addr_to_str(af, &fils->ipv4_dns, &fils->ipv6_dns,
    518 					dns_addr_str, &is_zero))
    519 		return is_zero;
    520 
    521 	if (!netconfig->dns_overridden[INDEX_FOR_AF(af)] && !is_zero) {
    522 		char *dns_list[2] = { dns_addr_str, NULL };
    523 
    524 		if (L_WARN_ON(!l_netconfig_set_dns_override(netconfig->nc,
    525 								af, dns_list)))
    526 			return false;
    527 	}
    528 
    529 	return true;
    530 }
    531 
    532 bool netconfig_configure(struct netconfig *netconfig,
    533 				netconfig_notify_func_t notify, void *user_data)
    534 {
    535 	if (netconfig->started)
    536 		return false;
    537 
    538 	netconfig->notify = notify;
    539 	netconfig->user_data = user_data;
    540 
    541 	if (netconfig_use_fils_addr(netconfig, AF_INET) &&
    542 			!netconfig_load_fils_settings(netconfig, AF_INET))
    543 		return false;
    544 
    545 	if (netconfig_use_fils_addr(netconfig, AF_INET6) &&
    546 			!netconfig_load_fils_settings(netconfig, AF_INET6))
    547 		return false;
    548 
    549 	if (unlikely(!l_netconfig_start(netconfig->nc)))
    550 		return false;
    551 
    552 	netconfig->started = true;
    553 	return true;
    554 }
    555 
    556 bool netconfig_reconfigure(struct netconfig *netconfig, bool set_arp_gw)
    557 {
    558 	if (!netconfig->started)
    559 		return false;
    560 
    561 	/*
    562 	 * Starting with kernel 4.20, ARP cache is flushed when the netdev
    563 	 * detects NO CARRIER.  This can result in unnecessarily long delays
    564 	 * (about 1 second on some networks) due to ARP query response being
    565 	 * lost or delayed.  Try to force the gateway into the ARP cache
    566 	 * to alleviate this
    567 	 */
    568 	if (set_arp_gw) {
    569 		netconfig_dhcp_gateway_to_arp(netconfig);
    570 
    571 		if (netconfig->connected[INDEX_FOR_AF(AF_INET)] &&
    572 				netconfig_use_fils_addr(netconfig, AF_INET))
    573 			netconfig_commit_fils_macs(netconfig, AF_INET);
    574 
    575 		if (netconfig->connected[INDEX_FOR_AF(AF_INET6)] &&
    576 				netconfig_use_fils_addr(netconfig, AF_INET6))
    577 			netconfig_commit_fils_macs(netconfig, AF_INET6);
    578 	}
    579 
    580 	if (!netconfig->static_config[INDEX_FOR_AF(AF_INET)]) {
    581 		/* TODO l_dhcp_client sending a DHCP inform request */
    582 	}
    583 
    584 	if (!netconfig->static_config[INDEX_FOR_AF(AF_INET6)]) {
    585 		/* TODO l_dhcp_v6_client sending a DHCP inform request */
    586 	}
    587 
    588 	return true;
    589 }
    590 
    591 bool netconfig_reset(struct netconfig *netconfig)
    592 {
    593 	if (!netconfig->started)
    594 		return false;
    595 
    596 	netconfig->started = false;
    597 	l_netconfig_unconfigure(netconfig->nc);
    598 	l_netconfig_stop(netconfig->nc);
    599 
    600 	netconfig->connected[0] = false;
    601 	netconfig->connected[1] = false;
    602 
    603 	netconfig_free_settings(netconfig);
    604 	return true;
    605 }
    606 
    607 char *netconfig_get_dhcp_server_ipv4(struct netconfig *netconfig)
    608 {
    609 	struct l_dhcp_client *client =
    610 		l_netconfig_get_dhcp_client(netconfig->nc);
    611 	const struct l_dhcp_lease *lease;
    612 
    613 	if (!client)
    614 		return NULL;
    615 
    616 	lease = l_dhcp_client_get_lease(client);
    617 	if (!lease)
    618 		return NULL;
    619 
    620 	return l_dhcp_lease_get_server_id(lease);
    621 }
    622 
    623 bool netconfig_get_fils_ip_req(struct netconfig *netconfig,
    624 				struct ie_fils_ip_addr_request_info *info)
    625 {
    626 	/*
    627 	 * Fill in the fields used for building the FILS IP Address Assignment
    628 	 * IE during connection if we're configured to do automatic network
    629 	 * configuration (usually DHCP).  If we're configured with static
    630 	 * values return false to mean the IE should not be sent.
    631 	 */
    632 	if (netconfig->static_config[0] && netconfig->static_config[1])
    633 		return false;
    634 
    635 	memset(info, 0, sizeof(*info));
    636 	info->ipv4 = !netconfig->static_config[INDEX_FOR_AF(AF_INET)];
    637 	info->ipv6 = !netconfig->static_config[INDEX_FOR_AF(AF_INET6)];
    638 	info->dns = (info->ipv4 &&
    639 			!netconfig->dns_overridden[INDEX_FOR_AF(AF_INET)]) ||
    640 		(info->ipv6 &&
    641 			!netconfig->dns_overridden[INDEX_FOR_AF(AF_INET)]);
    642 
    643 	return true;
    644 }
    645 
    646 void netconfig_handle_fils_ip_resp(struct netconfig *netconfig,
    647 			const struct ie_fils_ip_addr_response_info *info)
    648 {
    649 	l_free(netconfig->fils_override);
    650 	netconfig->fils_override = l_memdup(info, sizeof(*info));
    651 }
    652 
    653 static void netconfig_event_handler(struct l_netconfig *nc, uint8_t family,
    654 					enum l_netconfig_event event,
    655 					void *user_data)
    656 {
    657 	struct netconfig *netconfig = user_data;
    658 
    659 	/* Once stopped, only commit a final L_NETCONFIG_EVENT_UNCONFIGURE */
    660 	if (!netconfig->started && event != L_NETCONFIG_EVENT_UNCONFIGURE)
    661 		return;
    662 
    663 	l_debug("l_netconfig event %d", event);
    664 
    665 	netconfig_commit(netconfig, family, event);
    666 
    667 	switch (event) {
    668 	case L_NETCONFIG_EVENT_CONFIGURE:
    669 	case L_NETCONFIG_EVENT_UPDATE:
    670 		break;
    671 
    672 	case L_NETCONFIG_EVENT_UNCONFIGURE:
    673 		break;
    674 
    675 	case L_NETCONFIG_EVENT_FAILED:
    676 		netconfig->connected[INDEX_FOR_AF(family)] = false;
    677 
    678 		/*
    679 		 * l_netconfig might have emitted an UNCONFIGURE before this
    680 		 * but now it tells us it's given up on (re)establishing the
    681 		 * IP setup.
    682 		 */
    683 		if (family == AF_INET && netconfig->notify)
    684 			netconfig->notify(NETCONFIG_EVENT_FAILED,
    685 						netconfig->user_data);
    686 
    687 		break;
    688 
    689 	default:
    690 		l_error("netconfig: Received unsupported l_netconfig event: %d",
    691 			event);
    692 	}
    693 }
    694 
    695 struct netconfig *netconfig_new(uint32_t ifindex)
    696 {
    697 	struct netdev *netdev = netdev_find(ifindex);
    698 	struct netconfig *netconfig;
    699 	const char *debug_level = NULL;
    700 	int dhcp_priority = L_LOG_INFO;
    701 	struct l_dhcp6_client *dhcp6;
    702 	struct l_icmp6_client *icmp6;
    703 	struct l_dhcp_client *dhcp;
    704 
    705 	l_debug("Creating netconfig for interface: %d", ifindex);
    706 
    707 	netconfig = l_new(struct netconfig, 1);
    708 	netconfig->nc = l_netconfig_new(ifindex);
    709 	netconfig->netdev = netdev;
    710 	netconfig->resolve = resolve_new(ifindex);
    711 
    712 	netconfig_commit_init(netconfig);
    713 
    714 	debug_level = getenv("IWD_DHCP_DEBUG");
    715 	if (debug_level) {
    716 		if (!strcmp("debug", debug_level))
    717 			dhcp_priority = L_LOG_DEBUG;
    718 		else if (!strcmp("info", debug_level))
    719 			dhcp_priority = L_LOG_INFO;
    720 		else if (!strcmp("warn", debug_level))
    721 			dhcp_priority = L_LOG_WARNING;
    722 		else if (!strcmp("error", debug_level))
    723 			dhcp_priority = L_LOG_ERR;
    724 		else	/* Default for backwards compatibility */
    725 			dhcp_priority = L_LOG_DEBUG;
    726 	}
    727 
    728 	l_netconfig_set_event_handler(netconfig->nc, netconfig_event_handler,
    729 					netconfig, NULL);
    730 
    731 	dhcp = l_netconfig_get_dhcp_client(netconfig->nc);
    732 	l_dhcp_client_set_max_attempts(dhcp, DHCP_ATTEMPTS);
    733 
    734 	dhcp6 = l_netconfig_get_dhcp6_client(netconfig->nc);
    735 	l_dhcp6_client_set_lla_randomized(dhcp6, true);
    736 
    737 	icmp6 = l_netconfig_get_icmp6_client(netconfig->nc);
    738 	l_icmp6_client_set_nodelay(icmp6, true);
    739 
    740 	if (debug_level) {
    741 		l_dhcp6_client_set_debug(dhcp6, do_debug, "[DHCPv6] ", NULL);
    742 		l_icmp6_client_set_debug(icmp6, do_debug, "[ICMPv6] ", NULL);
    743 		l_dhcp_client_set_debug(dhcp, do_debug, "[DHCPv4] ", NULL,
    744 					dhcp_priority);
    745 	}
    746 
    747 	return netconfig;
    748 }
    749 
    750 void netconfig_destroy(struct netconfig *netconfig)
    751 {
    752 	l_debug("");
    753 
    754 	netconfig_reset(netconfig);
    755 	netconfig_commit_free(netconfig, "aborted");
    756 	resolve_free(netconfig->resolve);
    757 	netconfig_free(netconfig);
    758 }
    759 
    760 bool netconfig_enabled(void)
    761 {
    762 	bool enabled;
    763 
    764 	return l_settings_get_bool(iwd_get_config(), "General",
    765 					"EnableNetworkConfiguration",
    766 					&enabled) && enabled;
    767 }
    768 
    769 static int netconfig_init(void)
    770 {
    771 	if (!l_settings_get_uint(iwd_get_config(), "Network",
    772 							"RoutePriorityOffset",
    773 							&ROUTE_PRIORITY_OFFSET))
    774 		ROUTE_PRIORITY_OFFSET = 300;
    775 
    776 	if (!l_settings_get_bool(iwd_get_config(), "Network",
    777 					"EnableIPv6",
    778 					&ipv6_enabled))
    779 		ipv6_enabled = true;
    780 
    781 	mdns_global = l_settings_get_string(iwd_get_config(), "Network",
    782 						"MulticastDNS");
    783 
    784 	return 0;
    785 }
    786 
    787 static void netconfig_exit(void)
    788 {
    789 	l_free(mdns_global);
    790 }
    791 
    792 IWD_MODULE(netconfig, netconfig_init, netconfig_exit)