nm-dhcp-utils.c 21.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2005 - 2010 Red Hat, Inc.
 *
 */

20
#include "nm-default.h"
21

22
#include <unistd.h>
23
#include <arpa/inet.h>
24

25 26
#include "nm-utils/nm-dedup-multi.h"

27 28 29
#include "nm-dhcp-utils.h"
#include "nm-utils.h"
#include "NetworkManagerUtils.h"
30
#include "platform/nm-platform.h"
31
#include "nm-dhcp-client-logging.h"
32
#include "nm-core-internal.h"
33

34
/*****************************************************************************/
35 36

static gboolean
37 38
ip4_process_dhcpcd_rfc3442_routes (const char *iface,
                                   const char *str,
39
                                   guint32 route_table,
40
                                   guint32 route_metric,
41 42 43 44 45 46 47 48 49 50 51
                                   NMIP4Config *ip4_config,
                                   guint32 *gwaddr)
{
	char **routes, **r;
	gboolean have_routes = FALSE;

	routes = g_strsplit (str, " ", 0);
	if (g_strv_length (routes) == 0)
		goto out;

	if ((g_strv_length (routes) % 2) != 0) {
52
		_LOG2W (LOGD_DHCP4, iface, "  classless static routes provided, but invalid");
53 54 55 56 57 58 59 60 61 62 63 64 65 66
		goto out;
	}

	for (r = routes; *r; r += 2) {
		char *slash;
		NMPlatformIP4Route route;
		int rt_cidr = 32;
		guint32 rt_addr, rt_route;

		slash = strchr(*r, '/');
		if (slash) {
			*slash = '\0';
			errno = 0;
			rt_cidr = strtol (slash + 1, NULL, 10);
67
			if (errno || rt_cidr > 32) {
68
				_LOG2W (LOGD_DHCP4, iface, "DHCP provided invalid classless static route cidr: '%s'", slash + 1);
69 70 71 72
				continue;
			}
		}
		if (inet_pton (AF_INET, *r, &rt_addr) <= 0) {
73
			_LOG2W (LOGD_DHCP4, iface, "DHCP provided invalid classless static route address: '%s'", *r);
74 75 76
			continue;
		}
		if (inet_pton (AF_INET, *(r + 1), &rt_route) <= 0) {
77
			_LOG2W (LOGD_DHCP4, iface, "DHCP provided invalid classless static route gateway: '%s'", *(r + 1));
78 79 80 81 82 83 84 85
			continue;
		}

		have_routes = TRUE;
		if (rt_cidr == 0 && rt_addr == 0) {
			/* FIXME: how to handle multiple routers? */
			*gwaddr = rt_route;
		} else {
86
			_LOG2I (LOGD_DHCP4, iface, "  classless static route %s/%d gw %s", *r, rt_cidr, *(r + 1));
87
			memset (&route, 0, sizeof (route));
88
			route.network = nm_utils_ip4_address_clear_host_address (rt_addr, rt_cidr);
89 90
			route.plen = rt_cidr;
			route.gateway = rt_route;
91
			route.rt_source = NM_IP_CONFIG_SOURCE_DHCP;
92
			route.metric = route_metric;
93
			route.table_coerced = nm_platform_route_table_coerce (route_table);
94
			nm_ip4_config_add_route (ip4_config, &route, NULL);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		}
	}

out:
	g_strfreev (routes);
	return have_routes;
}

static const char **
process_dhclient_rfc3442_route (const char **octets,
                                NMPlatformIP4Route *route,
                                gboolean *success)
{
	const char **o = octets;
	int addr_len = 0, i = 0;
	long int tmp;
	char *next_hop;
	guint32 tmp_addr;

	*success = FALSE;

	if (!*o)
		return o; /* no prefix */

	tmp = strtol (*o, NULL, 10);
	if (tmp < 0 || tmp > 32)  /* 32 == max IP4 prefix length */
		return o;

	memset (route, 0, sizeof (*route));
	route->plen = tmp;
	o++;

	if (tmp > 0)
		addr_len = ((tmp - 1) / 8) + 1;

	/* ensure there's at least the address + next hop left */
	if (g_strv_length ((char **) o) < addr_len + 4)
		goto error;

	if (tmp) {
		const char *addr[4] = { "0", "0", "0", "0" };
		char *str_addr;

		for (i = 0; i < addr_len; i++)
			addr[i] = *o++;

		str_addr = g_strjoin (".", addr[0], addr[1], addr[2], addr[3], NULL);
		if (inet_pton (AF_INET, str_addr, &tmp_addr) <= 0) {
			g_free (str_addr);
			goto error;
		}
146
		g_free (str_addr);
147
		route->network = nm_utils_ip4_address_clear_host_address (tmp_addr, tmp);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	}

	/* Handle next hop */
	next_hop = g_strjoin (".", o[0], o[1], o[2], o[3], NULL);
	if (inet_pton (AF_INET, next_hop, &tmp_addr) <= 0) {
		g_free (next_hop);
		goto error;
	}
	route->gateway = tmp_addr;
	g_free (next_hop);

	*success = TRUE;
	return o + 4; /* advance to past the next hop */

error:
	return o;
}

static gboolean
167 168
ip4_process_dhclient_rfc3442_routes (const char *iface,
                                     const char *str,
169
                                     guint32 route_table,
170
                                     guint32 route_metric,
171 172 173 174 175 176 177 178 179 180
                                     NMIP4Config *ip4_config,
                                     guint32 *gwaddr)
{
	char **octets, **o;
	gboolean have_routes = FALSE;
	NMPlatformIP4Route route;
	gboolean success;

	o = octets = g_strsplit_set (str, " .", 0);
	if (g_strv_length (octets) < 5) {
181
		_LOG2W (LOGD_DHCP4, iface, "ignoring invalid classless static routes '%s'", str);
182 183 184 185 186 187 188
		goto out;
	}

	while (*o) {
		memset (&route, 0, sizeof (route));
		o = (char **) process_dhclient_rfc3442_route ((const char **) o, &route, &success);
		if (!success) {
189
			_LOG2W (LOGD_DHCP4, iface, "ignoring invalid classless static routes");
190 191 192 193 194 195 196 197
			break;
		}

		have_routes = TRUE;
		if (!route.plen) {
			/* gateway passed as classless static route */
			*gwaddr = route.gateway;
		} else {
198 199
			char b1[INET_ADDRSTRLEN];
			char b2[INET_ADDRSTRLEN];
200 201

			/* normal route */
202
			route.rt_source = NM_IP_CONFIG_SOURCE_DHCP;
203
			route.metric = route_metric;
204
			route.table_coerced = nm_platform_route_table_coerce (route_table);
205
			nm_ip4_config_add_route (ip4_config, &route, NULL);
206

207
			_LOG2I (LOGD_DHCP4, iface, "  classless static route %s/%d gw %s",
208 209 210
			        nm_utils_inet4_ntop (route.network, b1),
			        route.plen,
			        nm_utils_inet4_ntop (route.gateway, b2));
211 212 213 214 215 216 217 218 219
		}
	}

out:
	g_strfreev (octets);
	return have_routes;
}

static gboolean
220 221
ip4_process_classless_routes (const char *iface,
                              GHashTable *options,
222
                              guint32 route_table,
223
                              guint32 route_metric,
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
                              NMIP4Config *ip4_config,
                              guint32 *gwaddr)
{
	const char *str, *p;

	g_return_val_if_fail (options != NULL, FALSE);
	g_return_val_if_fail (ip4_config != NULL, FALSE);

	*gwaddr = 0;

	/* dhcpd/dhclient in Fedora has support for rfc3442 implemented using a
	 * slightly different format:
	 *
	 * option classless-static-routes = array of (destination-descriptor ip-address);
	 *
	 * which results in:
	 *
	 * 0 192.168.0.113 25.129.210.177.132 192.168.0.113 7.2 10.34.255.6
	 *
	 * dhcpcd supports classless static routes natively and uses this same
	 * option identifier with the following format:
	 *
	 * 192.168.10.0/24 192.168.1.1 10.0.0.0/8 10.17.66.41
	 */
248
	str = g_hash_table_lookup (options, "classless_static_routes");
249 250 251 252 253 254 255 256 257 258

	/* dhclient doesn't have actual support for rfc3442 classless static routes
	 * upstream.  Thus, people resort to defining the option in dhclient.conf
	 * and using arbitrary formats like so:
	 *
	 * option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;
	 *
	 * See https://lists.isc.org/pipermail/dhcp-users/2008-December/007629.html
	 */
	if (!str)
259
		str = g_hash_table_lookup (options, "rfc3442_classless_static_routes");
260 261 262

	/* Microsoft version; same as rfc3442 but with a different option # (249) */
	if (!str)
263
		str = g_hash_table_lookup (options, "ms_classless_static_routes");
264 265 266 267 268 269 270

	if (!str || !strlen (str))
		return FALSE;

	p = str;
	while (*p) {
		if (!g_ascii_isdigit (*p) && (*p != ' ') && (*p != '.') && (*p != '/')) {
271
			_LOG2W (LOGD_DHCP4, iface, "ignoring invalid classless static routes '%s'", str);
272 273 274 275 276 277 278
			return FALSE;
		}
		p++;
	};

	if (strchr (str, '/')) {
		/* dhcpcd format */
279
		return ip4_process_dhcpcd_rfc3442_routes (iface, str, route_table, route_metric, ip4_config, gwaddr);
280 281
	}

282
	return ip4_process_dhclient_rfc3442_routes (iface, str, route_table, route_metric, ip4_config, gwaddr);
283 284 285
}

static void
286 287
process_classful_routes (const char *iface,
                         GHashTable *options,
288
                         guint32 route_table,
289
                         guint32 route_metric,
290
                         NMIP4Config *ip4_config)
291 292 293 294
{
	const char *str;
	char **searches, **s;

295
	str = g_hash_table_lookup (options, "static_routes");
296 297 298 299 300
	if (!str)
		return;

	searches = g_strsplit (str, " ", 0);
	if ((g_strv_length (searches) % 2)) {
301
		_LOG2I (LOGD_DHCP, iface, "  static routes provided, but invalid");
302 303 304 305 306 307 308 309
		goto out;
	}

	for (s = searches; *s; s += 2) {
		NMPlatformIP4Route route;
		guint32 rt_addr, rt_route;

		if (inet_pton (AF_INET, *s, &rt_addr) <= 0) {
310
			_LOG2W (LOGD_DHCP, iface, "DHCP provided invalid static route address: '%s'", *s);
311 312 313
			continue;
		}
		if (inet_pton (AF_INET, *(s + 1), &rt_route) <= 0) {
314
			_LOG2W (LOGD_DHCP, iface, "DHCP provided invalid static route gateway: '%s'", *(s + 1));
315 316 317 318 319 320 321 322 323 324 325
			continue;
		}

		// FIXME: ensure the IP address and route are sane

		memset (&route, 0, sizeof (route));
		route.network = rt_addr;
		/* RFC 2132, updated by RFC 3442:
		   The Static Routes option (option 33) does not provide a subnet mask
		   for each route - it is assumed that the subnet mask is implicit in
		   whatever network number is specified in each route entry */
326 327
		route.plen = _nm_utils_ip4_get_default_prefix (rt_addr);
		if (rt_addr & ~_nm_utils_ip4_prefix_to_netmask (route.plen)) {
328 329 330 331
			/* RFC 943: target not "this network"; using host routing */
			route.plen = 32;
		}
		route.gateway = rt_route;
332
		route.rt_source = NM_IP_CONFIG_SOURCE_DHCP;
333
		route.metric = route_metric;
334
		route.table_coerced = nm_platform_route_table_coerce (route_table);
335

336 337
		route.network = nm_utils_ip4_address_clear_host_address (route.network, route.plen);

338
		nm_ip4_config_add_route (ip4_config, &route, NULL);
339
		_LOG2I (LOGD_DHCP, iface, "  static route %s",
340
		             nm_platform_ip4_route_to_string (&route, NULL, 0));
341 342 343 344 345 346 347
	}

out:
	g_strfreev (searches);
}

static void
348 349 350 351
process_domain_search (const char *iface,
                       const char *str,
                       GFunc add_func,
                       gpointer user_data)
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
{
	char **searches, **s;
	char *unescaped, *p;
	int i;

	g_return_if_fail (str != NULL);
	g_return_if_fail (add_func != NULL);

	p = unescaped = g_strdup (str);
	do {
		p = strstr (p, "\\032");
		if (!p)
			break;

		/* Clear the escaped space with real spaces */
		for (i = 0; i < 4; i++)
			*p++ = ' ';
	} while (*p++);

	if (strchr (unescaped, '\\')) {
372
		_LOG2W (LOGD_DHCP, iface, "  invalid domain search: '%s'", unescaped);
373 374 375 376 377 378
		goto out;
	}

	searches = g_strsplit (unescaped, " ", 0);
	for (s = searches; *s; s++) {
		if (strlen (*s)) {
379
			_LOG2I (LOGD_DHCP, iface, "  domain search '%s'", *s);
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
			add_func (*s, user_data);
		}
	}
	g_strfreev (searches);

out:
	g_free (unescaped);
}

static void
ip4_add_domain_search (gpointer data, gpointer user_data)
{
	nm_ip4_config_add_search (NM_IP4_CONFIG (user_data), (const char *) data);
}

NMIP4Config *
396 397
nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx,
                                       int ifindex,
398
                                       const char *iface,
399
                                       GHashTable *options,
400
                                       guint32 route_table,
401
                                       guint32 route_metric)
402 403 404
{
	NMIP4Config *ip4_config = NULL;
	guint32 tmp_addr;
405
	in_addr_t addr;
406 407
	NMPlatformIP4Address address;
	char *str = NULL;
408 409
	gboolean gateway_has = FALSE;
	guint32 gateway = 0;
410
	guint8 plen = 0;
411
	char sbuf[NM_UTILS_INET_ADDRSTRLEN];
412 413 414

	g_return_val_if_fail (options != NULL, NULL);

415
	ip4_config = nm_ip4_config_new (multi_idx, ifindex);
416 417 418
	memset (&address, 0, sizeof (address));
	address.timestamp = nm_utils_get_monotonic_timestamp_s ();

419
	str = g_hash_table_lookup (options, "ip_address");
420
	if (str && (inet_pton (AF_INET, str, &addr) > 0))
421
		_LOG2I (LOGD_DHCP4, iface, "  address %s", str);
422
	else
423 424
		goto error;

425
	str = g_hash_table_lookup (options, "subnet_mask");
426 427
	if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
		plen = nm_utils_ip4_netmask_to_prefix (tmp_addr);
428
		_LOG2I (LOGD_DHCP4, iface, "  plen %d (%s)", plen, str);
429 430
	} else {
		/* Get default netmask for the IP according to appropriate class. */
431
		plen = _nm_utils_ip4_get_default_prefix (addr);
432
		_LOG2I (LOGD_DHCP4, iface, "  plen %d (default)", plen);
433
	}
434
	nm_platform_ip4_address_set_addr (&address, addr, plen);
435 436 437 438

	/* Routes: if the server returns classless static routes, we MUST ignore
	 * the 'static_routes' option.
	 */
439
	if (!ip4_process_classless_routes (iface, options, route_table, route_metric, ip4_config, &gateway))
440
		process_classful_routes (iface, options, route_table, route_metric, ip4_config);
441

442
	if (gateway) {
443
		_LOG2I (LOGD_DHCP4, iface, "  gateway %s", nm_utils_inet4_ntop (gateway, sbuf));
444
		gateway_has = TRUE;
445 446 447 448
	} else {
		/* If the gateway wasn't provided as a classless static route with a
		 * subnet length of 0, try to find it using the old-style 'routers' option.
		 */
449
		str = g_hash_table_lookup (options, "routers");
450 451 452 453 454 455
		if (str) {
			char **routers = g_strsplit (str, " ", 0);
			char **s;

			for (s = routers; *s; s++) {
				/* FIXME: how to handle multiple routers? */
456
				if (inet_pton (AF_INET, *s, &gateway) > 0) {
457
					_LOG2I (LOGD_DHCP4, iface, "  gateway %s", *s);
458
					gateway_has = TRUE;
459 460
					break;
				} else
461
					_LOG2W (LOGD_DHCP4, iface, "ignoring invalid gateway '%s'", *s);
462 463 464 465 466
			}
			g_strfreev (routers);
		}
	}

467 468 469 470 471 472 473 474 475 476 477
	if (gateway_has) {
		const NMPlatformIP4Route r = {
			.rt_source = NM_IP_CONFIG_SOURCE_DHCP,
			.gateway = gateway,
			.table_coerced = nm_platform_route_table_coerce (route_table),
			.metric = route_metric,
		};

		nm_ip4_config_add_route (ip4_config, &r, NULL);
	}

478
	str = g_hash_table_lookup (options, "dhcp_lease_time");
479 480
	if (str) {
		address.lifetime = address.preferred = strtoul (str, NULL, 10);
481
		_LOG2I (LOGD_DHCP4, iface, "  lease time %u", address.lifetime);
482 483
	}

484
	address.addr_source = NM_IP_CONFIG_SOURCE_DHCP;
485 486
	nm_ip4_config_add_address (ip4_config, &address);

487
	str = g_hash_table_lookup (options, "host_name");
488
	if (str)
489
		_LOG2I (LOGD_DHCP4, iface, "  hostname '%s'", str);
490

491
	str = g_hash_table_lookup (options, "domain_name_servers");
492
	if (str) {
493
		char **dns = g_strsplit (str, " ", 0);
494 495
		char **s;

496
		for (s = dns; *s; s++) {
497
			if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
498 499
				if (tmp_addr) {
					nm_ip4_config_add_nameserver (ip4_config, tmp_addr);
500
					_LOG2I (LOGD_DHCP4, iface, "  nameserver '%s'", *s);
501
				}
502
			} else
503
				_LOG2W (LOGD_DHCP4, iface, "ignoring invalid nameserver '%s'", *s);
504
		}
505
		g_strfreev (dns);
506 507
	}

508
	str = g_hash_table_lookup (options, "domain_name");
509 510 511 512 513
	if (str) {
		char **domains = g_strsplit (str, " ", 0);
		char **s;

		for (s = domains; *s; s++) {
514
			_LOG2I (LOGD_DHCP4, iface, "  domain name '%s'", *s);
515 516 517 518 519
			nm_ip4_config_add_domain (ip4_config, *s);
		}
		g_strfreev (domains);
	}

520
	str = g_hash_table_lookup (options, "domain_search");
521
	if (str)
522
		process_domain_search (iface, str, ip4_add_domain_search, ip4_config);
523

524
	str = g_hash_table_lookup (options, "netbios_name_servers");
525
	if (str) {
526
		char **nbns = g_strsplit (str, " ", 0);
527 528
		char **s;

529
		for (s = nbns; *s; s++) {
530
			if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
531 532
				if (tmp_addr) {
					nm_ip4_config_add_wins (ip4_config, tmp_addr);
533
					_LOG2I (LOGD_DHCP4, iface, "  wins '%s'", *s);
534
				}
535
			} else
536
				_LOG2W (LOGD_DHCP4, iface, "ignoring invalid WINS server '%s'", *s);
537
		}
538
		g_strfreev (nbns);
539 540
	}

541
	str = g_hash_table_lookup (options, "interface_mtu");
542 543 544 545 546
	if (str) {
		int int_mtu;

		errno = 0;
		int_mtu = strtol (str, NULL, 10);
547
		if (NM_IN_SET (errno, EINVAL, ERANGE))
548 549 550
			goto error;

		if (int_mtu > 576)
Lubomir Rintel's avatar
Lubomir Rintel committed
551
			nm_ip4_config_set_mtu (ip4_config, int_mtu, NM_IP_CONFIG_SOURCE_DHCP);
552 553
	}

554
	str = g_hash_table_lookup (options, "nis_domain");
555
	if (str) {
556
		_LOG2I (LOGD_DHCP4, iface, "  NIS domain '%s'", str);
557 558 559
		nm_ip4_config_set_nis_domain (ip4_config, str);
	}

560
	str = g_hash_table_lookup (options, "nis_servers");
561
	if (str) {
562
		char **nis = g_strsplit (str, " ", 0);
563 564
		char **s;

565
		for (s = nis; *s; s++) {
566
			if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
567 568
				if (tmp_addr) {
					nm_ip4_config_add_nis_server (ip4_config, tmp_addr);
569
					_LOG2I (LOGD_DHCP4, iface, "  nis '%s'", *s);
570
				}
571
			} else
572
				_LOG2W (LOGD_DHCP4, iface, "ignoring invalid NIS server '%s'", *s);
573
		}
574
		g_strfreev (nis);
575 576
	}

577 578 579
	str = g_hash_table_lookup (options, "vendor_encapsulated_options");
	nm_ip4_config_set_metered (ip4_config, str && strstr (str, "ANDROID_METERED"));

580 581 582 583 584 585 586
	return ip4_config;

error:
	g_object_unref (ip4_config);
	return NULL;
}

587
/*****************************************************************************/
588 589 590 591 592 593 594

static void
ip6_add_domain_search (gpointer data, gpointer user_data)
{
	nm_ip6_config_add_search (NM_IP6_CONFIG (user_data), (const char *) data);
}

595 596 597
NMPlatformIP6Address
nm_dhcp_utils_ip6_prefix_from_options (GHashTable *options)
{
598
	gs_strfreev char **split_addr = NULL;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	NMPlatformIP6Address address = { 0, };
	struct in6_addr tmp_addr;
	char *str = NULL;
	int prefix;

	g_return_val_if_fail (options != NULL, address);

	str = g_hash_table_lookup (options, "ip6_prefix");
	if (!str)
		return address;

	split_addr = g_strsplit (str, "/", 2);
	if (split_addr[0] == NULL && split_addr[1] == NULL) {
		nm_log_warn (LOGD_DHCP6, "DHCP returned prefix without length '%s'", str);
		return address;
	}

	if (!inet_pton (AF_INET6, split_addr[0], &tmp_addr)) {
		nm_log_warn (LOGD_DHCP6, "DHCP returned invalid prefix '%s'", str);
		return address;
	}

	prefix = _nm_utils_ascii_str_to_int64 (split_addr[1], 10, 0, 128, -1);
	if (prefix < 0) {
		nm_log_warn (LOGD_DHCP6, "DHCP returned prefix with invalid length '%s'", str);
		return address;
	}

	address.address = tmp_addr;
	address.addr_source = NM_IP_CONFIG_SOURCE_DHCP;
	address.plen = prefix;
	address.timestamp = nm_utils_get_monotonic_timestamp_s ();

	str = g_hash_table_lookup (options, "max_life");
	if (str)
		address.lifetime = strtoul (str, NULL, 10);

	str = g_hash_table_lookup (options, "preferred_life");
	if (str)
		address.preferred = strtoul (str, NULL, 10);

	return address;
}

643
NMIP6Config *
644 645
nm_dhcp_utils_ip6_config_from_options (NMDedupMultiIndex *multi_idx,
                                       int ifindex,
646
                                       const char *iface,
647 648 649 650 651 652 653 654 655 656 657 658 659 660
                                       GHashTable *options,
                                       gboolean info_only)
{
	NMIP6Config *ip6_config = NULL;
	struct in6_addr tmp_addr;
	NMPlatformIP6Address address;
	char *str = NULL;

	g_return_val_if_fail (options != NULL, NULL);

	memset (&address, 0, sizeof (address));
	address.plen = 128;
	address.timestamp = nm_utils_get_monotonic_timestamp_s ();

661
	ip6_config = nm_ip6_config_new (multi_idx, ifindex);
662

663
	str = g_hash_table_lookup (options, "max_life");
664 665
	if (str) {
		address.lifetime = strtoul (str, NULL, 10);
666
		_LOG2I (LOGD_DHCP6, iface, "  valid_lft %u", address.lifetime);
667 668
	}

669
	str = g_hash_table_lookup (options, "preferred_life");
670 671
	if (str) {
		address.preferred = strtoul (str, NULL, 10);
672
		_LOG2I (LOGD_DHCP6, iface, "  preferred_lft %u", address.preferred);
673 674
	}

675
	str = g_hash_table_lookup (options, "ip6_address");
676 677
	if (str) {
		if (!inet_pton (AF_INET6, str, &tmp_addr)) {
678 679
			_LOG2W (LOGD_DHCP6, iface, "(%s): DHCP returned invalid address '%s'",
			        iface, str);
680 681 682 683
			goto error;
		}

		address.address = tmp_addr;
684
		address.addr_source = NM_IP_CONFIG_SOURCE_DHCP;
685
		nm_ip6_config_add_address (ip6_config, &address);
686
		_LOG2I (LOGD_DHCP6, iface, "  address %s", str);
687 688 689 690 691
	} else if (info_only == FALSE) {
		/* No address in Managed mode is a hard error */
		goto error;
	}

692
	str = g_hash_table_lookup (options, "host_name");
693
	if (str)
694
		_LOG2I (LOGD_DHCP6, iface, "  hostname '%s'", str);
695

696
	str = g_hash_table_lookup (options, "dhcp6_name_servers");
697
	if (str) {
698
		char **dns = g_strsplit (str, " ", 0);
699 700
		char **s;

701
		for (s = dns; *s; s++) {
702
			if (inet_pton (AF_INET6, *s, &tmp_addr) > 0) {
703 704
				if (!IN6_IS_ADDR_UNSPECIFIED (&tmp_addr)) {
					nm_ip6_config_add_nameserver (ip6_config, &tmp_addr);
705
					_LOG2I (LOGD_DHCP6, iface, "  nameserver '%s'", *s);
706
				}
707
			} else
708
				_LOG2W (LOGD_DHCP6, iface, "ignoring invalid nameserver '%s'", *s);
709
		}
710
		g_strfreev (dns);
711 712
	}

713
	str = g_hash_table_lookup (options, "dhcp6_domain_search");
714
	if (str)
715
		process_domain_search (iface, str, ip6_add_domain_search, ip6_config);
716 717 718 719 720 721 722 723

	return ip6_config;

error:
	g_object_unref (ip6_config);
	return NULL;
}

724
char *
725
nm_dhcp_utils_duid_to_string (GBytes *duid)
726
{
727 728 729
	gconstpointer data;
	gsize len;

730
	g_return_val_if_fail (duid, NULL);
731

732
	data = g_bytes_get_data (duid, &len);
733
	return _nm_utils_bin2hexstr_full (data, len, ':', FALSE, NULL);
734 735
}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
/**
 * nm_dhcp_utils_client_id_string_to_bytes:
 * @client_id: the client ID string
 *
 * Accepts either a hex string ("aa:bb:cc") representing a binary client ID
 * (the first byte is assumed to be the 'type' field per RFC 2132 section 9.14),
 * or a string representing a non-hardware-address client ID, in which case
 * the 'type' field is set to 0.
 *
 * Returns: the binary client ID suitable for sending over the wire
 * to the DHCP server.
 */
GBytes *
nm_dhcp_utils_client_id_string_to_bytes (const char *client_id)
{
	GBytes *bytes = NULL;
	guint len;
	char *c;

	g_return_val_if_fail (client_id && client_id[0], NULL);

757
	/* Try as hex encoded */
758
	if (strchr (client_id, ':')) {
759
		bytes = nm_utils_hexstr2bin (client_id);
760 761 762 763 764 765 766

		/* the result must be at least two bytes long,
		 * because @client_id contains a delimiter
		 * but nm_utils_hexstr2bin() does not allow
		 * leading nor trailing delimiters. */
		nm_assert (!bytes || g_bytes_get_size (bytes) >= 2);
	}
767 768 769
	if (!bytes) {
		/* Fall back to string */
		len = strlen (client_id);
770 771 772 773 774 775 776 777 778
		c = g_malloc (len + 1);
		c[0] = 0;  /* type: non-hardware address per RFC 2132 section 9.14 */
		memcpy (c + 1, client_id, len);
		bytes = g_bytes_new_take (c, len + 1);
	}

	return bytes;
}