nm-setting-ip6-config.c 50.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
19
 * Copyright 2007 - 2014 Red Hat, Inc.
20
 */
21

22 23
#include "config.h"

24 25
#include <string.h>
#include <dbus/dbus-glib.h>
26
#include <glib/gi18n-lib.h>
27

28 29 30 31
#include "nm-setting-ip6-config.h"
#include "nm-param-spec-specialized.h"
#include "nm-utils.h"
#include "nm-dbus-glib-types.h"
32
#include "nm-glib-compat.h"
33
#include "nm-setting-private.h"
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/**
 * SECTION:nm-setting-ip6-config
 * @short_description: Describes IPv6 addressing, routing, and name service properties
 * @include: nm-setting-ip6-config.h
 *
 * The #NMSettingIP6Config object is a #NMSetting subclass that describes
 * properties related to IPv6 addressing, routing, and Domain Name Service
 **/

/**
 * nm_setting_ip6_config_error_quark:
 *
 * Registers an error quark for #NMSettingIP6Config if necessary.
 *
 * Returns: the error quark used for #NMSettingIP6Config errors.
 **/
51 52 53 54 55 56 57 58 59 60
GQuark
nm_setting_ip6_config_error_quark (void)
{
	static GQuark quark;

	if (G_UNLIKELY (!quark))
		quark = g_quark_from_static_string ("nm-setting-ip6-config-error-quark");
	return quark;
}

61 62
G_DEFINE_BOXED_TYPE (NMIP6Address, nm_ip6_address, nm_ip6_address_dup, nm_ip6_address_unref)
G_DEFINE_BOXED_TYPE (NMIP6Route, nm_ip6_route, nm_ip6_route_dup, nm_ip6_route_unref)
63

64 65 66 67 68 69
G_DEFINE_TYPE_WITH_CODE (NMSettingIP6Config, nm_setting_ip6_config, NM_TYPE_SETTING,
                         _nm_register_setting (NM_SETTING_IP6_CONFIG_SETTING_NAME,
                                               g_define_type_id,
                                               4,
                                               NM_SETTING_IP6_CONFIG_ERROR))
NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP6_CONFIG)
70

71 72 73 74
#define NM_SETTING_IP6_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_IP6_CONFIG, NMSettingIP6ConfigPrivate))

typedef struct {
	char *method;
75
	char *dhcp_hostname;
76 77 78 79
	GSList *dns;        /* array of struct in6_addr */
	GSList *dns_search; /* list of strings */
	GSList *addresses;  /* array of NMIP6Address */
	GSList *routes;     /* array of NMIP6Route */
80
	gint64  route_metric;
81
	gboolean ignore_auto_routes;
82
	gboolean ignore_auto_dns;
83
	gboolean never_default;
84
	gboolean may_fail;
85
	NMSettingIP6ConfigPrivacy ip6_privacy;
86 87 88
} NMSettingIP6ConfigPrivate;


89 90 91
enum {
	PROP_0,
	PROP_METHOD,
92
	PROP_DHCP_HOSTNAME,
93 94 95 96
	PROP_DNS,
	PROP_DNS_SEARCH,
	PROP_ADDRESSES,
	PROP_ROUTES,
97
	PROP_ROUTE_METRIC,
98
	PROP_IGNORE_AUTO_ROUTES,
99
	PROP_IGNORE_AUTO_DNS,
100
	PROP_NEVER_DEFAULT,
101
	PROP_MAY_FAIL,
102
	PROP_IP6_PRIVACY,
103 104 105 106

	LAST_PROP
};

107 108 109 110 111 112 113
/**
 * nm_setting_ip6_config_new:
 *
 * Creates a new #NMSettingIP6Config object with default values.
 *
 * Returns: (transfer full): the new empty #NMSettingIP6Config object
 **/
114 115 116 117 118 119
NMSetting *
nm_setting_ip6_config_new (void)
{
	return (NMSetting *) g_object_new (NM_TYPE_SETTING_IP6_CONFIG, NULL);
}

120 121 122 123 124 125
/**
 * nm_setting_ip6_config_get_method:
 * @setting: the #NMSettingIP6Config
 *
 * Returns: the #NMSettingIP6Config:method property of the setting
 **/
126 127 128 129 130 131 132 133
const char *
nm_setting_ip6_config_get_method (NMSettingIP6Config *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL);

	return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->method;
}

134 135 136 137 138 139 140 141
/**
 * nm_setting_ip6_config_get_dhcp_hostname:
 * @setting: the #NMSettingIP6Config
 *
 * Returns the value contained in the #NMSettingIP6Config:dhcp-hostname
 * property.
 *
 * Returns: the configured hostname to send to the DHCP server
142 143
 *
 * Since: 0.9.8
144 145 146 147 148 149 150 151 152
 **/
const char *
nm_setting_ip6_config_get_dhcp_hostname (NMSettingIP6Config *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL);

	return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dhcp_hostname;
}

153 154 155 156 157 158
/**
 * nm_setting_ip6_config_get_num_dns:
 * @setting: the #NMSettingIP6Config
 *
 * Returns: the number of configured DNS servers
 **/
159 160 161 162 163 164 165 166
guint32
nm_setting_ip6_config_get_num_dns (NMSettingIP6Config *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), 0);

	return g_slist_length (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns);
}

167 168 169 170 171 172 173
/**
 * nm_setting_ip6_config_get_dns:
 * @setting: the #NMSettingIP6Config
 * @i: index number of the DNS server to return
 *
 * Returns: (transfer none): the IPv6 address of the DNS server at index @i
 **/
174 175 176 177
const struct in6_addr *
nm_setting_ip6_config_get_dns (NMSettingIP6Config *setting, guint32 i)
{
	NMSettingIP6ConfigPrivate *priv;
178

179

180
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL);
181 182 183 184 185 186 187

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	g_return_val_if_fail (i <= g_slist_length (priv->dns), NULL);

	return (const struct in6_addr *) g_slist_nth_data (priv->dns, i);
}

188 189 190 191 192 193 194 195 196 197
/**
 * nm_setting_ip6_config_add_dns:
 * @setting: the #NMSettingIP6Config
 * @dns: the IPv6 address of the DNS server to add
 *
 * Adds a new DNS server to the setting.
 *
 * Returns: %TRUE if the DNS server was added; %FALSE if the server was already
 * known
 **/
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
gboolean
nm_setting_ip6_config_add_dns (NMSettingIP6Config *setting, const struct in6_addr *addr)
{
	NMSettingIP6ConfigPrivate *priv;
	struct in6_addr *copy;
	GSList *iter;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	for (iter = priv->dns; iter; iter = g_slist_next (iter)) {
		if (!memcmp (addr, (struct in6_addr *) iter->data, sizeof (struct in6_addr)))
			return FALSE;
	}

	copy = g_malloc0 (sizeof (struct in6_addr));
	memcpy (copy, addr, sizeof (struct in6_addr));
	priv->dns = g_slist_append (priv->dns, copy);
216
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS);
217 218 219 220

	return TRUE;
}

221 222 223 224 225 226 227
/**
 * nm_setting_ip6_config_remove_dns:
 * @setting: the #NMSettingIP6Config
 * @i: index number of the DNS server to remove
 *
 * Removes the DNS server at index @i.
 **/
228 229 230 231 232 233 234 235 236 237 238 239 240 241
void
nm_setting_ip6_config_remove_dns (NMSettingIP6Config *setting, guint32 i)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *elt;

	g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting));

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	elt = g_slist_nth (priv->dns, i);
	g_return_if_fail (elt != NULL);

	g_free (elt->data);
	priv->dns = g_slist_delete_link (priv->dns, elt);
242
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS);
243 244
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
/**
 * nm_setting_ip6_config_remove_dns_by_value:
 * @setting: the #NMSettingIP6Config
 * @dns: the IPv6 address of the DNS server to remove
 *
 * Removes the DNS server at index @i.
 *
 * Returns: %TRUE if the DNS server was found and removed; %FALSE if it was not.
 *
 * Since: 0.9.10
 **/
gboolean
nm_setting_ip6_config_remove_dns_by_value (NMSettingIP6Config *setting,
                                           const struct in6_addr *addr)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *iter;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	for (iter = priv->dns; iter; iter = g_slist_next (iter)) {
		if (!memcmp (addr, (struct in6_addr *) iter->data, sizeof (struct in6_addr))) {
			priv->dns = g_slist_delete_link (priv->dns, iter);
			g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS);
			return TRUE;
		}
	}
	return FALSE;
}

276 277 278 279 280 281
/**
 * nm_setting_ip6_config_clear_dns:
 * @setting: the #NMSettingIP6Config
 *
 * Removes all configured DNS servers.
 **/
282 283 284 285 286
void
nm_setting_ip6_config_clear_dns (NMSettingIP6Config *setting)
{
	g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting));

287
	g_slist_free_full (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns, g_free);
288
	NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns = NULL;
289
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS);
290 291
}

292 293 294 295 296 297
/**
 * nm_setting_ip6_config_get_num_dns_searches:
 * @setting: the #NMSettingIP6Config
 *
 * Returns: the number of configured DNS search domains
 **/
298 299 300 301 302 303 304 305
guint32
nm_setting_ip6_config_get_num_dns_searches (NMSettingIP6Config *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), 0);

	return g_slist_length (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns_search);
}

306 307 308 309 310 311 312
/**
 * nm_setting_ip6_config_get_dns_search:
 * @setting: the #NMSettingIP6Config
 * @i: index number of the DNS search domain to return
 *
 * Returns: the DNS search domain at index @i
 **/
313 314 315 316 317 318 319 320 321 322 323 324 325
const char *
nm_setting_ip6_config_get_dns_search (NMSettingIP6Config *setting, guint32 i)
{
	NMSettingIP6ConfigPrivate *priv;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	g_return_val_if_fail (i <= g_slist_length (priv->dns_search), NULL);

	return (const char *) g_slist_nth_data (priv->dns_search, i);
}

326 327 328 329 330 331 332 333 334 335
/**
 * nm_setting_ip6_config_add_dns_search:
 * @setting: the #NMSettingIP6Config
 * @dns_search: the search domain to add
 *
 * Adds a new DNS search domain to the setting.
 *
 * Returns: %TRUE if the DNS search domain was added; %FALSE if the search
 * domain was already known
 **/
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
gboolean
nm_setting_ip6_config_add_dns_search (NMSettingIP6Config *setting,
                                      const char *dns_search)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *iter;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);
	g_return_val_if_fail (dns_search != NULL, FALSE);
	g_return_val_if_fail (dns_search[0] != '\0', FALSE);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	for (iter = priv->dns_search; iter; iter = g_slist_next (iter)) {
		if (!strcmp (dns_search, (char *) iter->data))
			return FALSE;
	}

	priv->dns_search = g_slist_append (priv->dns_search, g_strdup (dns_search));
354
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS_SEARCH);
355 356 357
	return TRUE;
}

358 359 360 361 362 363 364
/**
 * nm_setting_ip6_config_remove_dns_search:
 * @setting: the #NMSettingIP6Config
 * @i: index number of the DNS search domain
 *
 * Removes the DNS search domain at index @i.
 **/
365 366 367 368 369 370 371 372 373 374 375 376 377 378
void
nm_setting_ip6_config_remove_dns_search (NMSettingIP6Config *setting, guint32 i)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *elt;

	g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting));

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	elt = g_slist_nth (priv->dns_search, i);
	g_return_if_fail (elt != NULL);

	g_free (elt->data);
	priv->dns_search = g_slist_delete_link (priv->dns_search, elt);
379
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS_SEARCH);
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/**
 * nm_setting_ip6_config_remove_dns_search_by_value:
 * @setting: the #NMSettingIP6Config
 * @dns_search: the search domain to remove
 *
 * Removes the DNS search domain @dns_search.
 *
 * Returns: %TRUE if the DNS search domain was found and removed; %FALSE if it was not.
 *
 * Since 0.9.10
 **/
gboolean
nm_setting_ip6_config_remove_dns_search_by_value (NMSettingIP6Config *setting,
                                                  const char *dns_search)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *iter;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);
	g_return_val_if_fail (dns_search != NULL, FALSE);
	g_return_val_if_fail (dns_search[0] != '\0', FALSE);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	for (iter = priv->dns_search; iter; iter = g_slist_next (iter)) {
		if (!strcmp (dns_search, (char *) iter->data)) {
			priv->dns_search = g_slist_delete_link (priv->dns_search, iter);
			g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS_SEARCH);
			return TRUE;
		}
	}
	return FALSE;
}

415 416 417 418 419 420
/**
 * nm_setting_ip6_config_clear_dns_searches:
 * @setting: the #NMSettingIP6Config
 *
 * Removes all configured DNS search domains.
 **/
421 422 423 424 425
void
nm_setting_ip6_config_clear_dns_searches (NMSettingIP6Config *setting)
{
	g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting));

426
	g_slist_free_full (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns_search, g_free);
427
	NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns_search = NULL;
428
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS_SEARCH);
429 430
}

431 432 433 434 435 436
/**
 * nm_setting_ip6_config_get_num_addresses:
 * @setting: the #NMSettingIP6Config
 *
 * Returns: the number of configured addresses
 **/
437 438 439 440 441 442 443 444
guint32
nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), 0);

	return g_slist_length (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->addresses);
}

445 446 447 448 449 450 451
/**
 * nm_setting_ip6_config_get_address:
 * @setting: the #NMSettingIP6Config
 * @i: index number of the address to return
 *
 * Returns: the address at index @i
 **/
452 453 454 455 456 457 458 459 460 461 462 463 464
NMIP6Address *
nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i)
{
	NMSettingIP6ConfigPrivate *priv;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	g_return_val_if_fail (i <= g_slist_length (priv->addresses), NULL);

	return (NMIP6Address *) g_slist_nth_data (priv->addresses, i);
}

465 466 467 468 469 470 471 472 473 474 475
/**
 * nm_setting_ip6_config_add_address:
 * @setting: the #NMSettingIP6Config
 * @address: the new address to add
 *
 * Adds a new IPv6 address and associated information to the setting.  The
 * given address is duplicated internally and is not changed by this function.
 *
 * Returns: %TRUE if the address was added; %FALSE if the address was already
 * known.
 **/
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
gboolean
nm_setting_ip6_config_add_address (NMSettingIP6Config *setting,
                                   NMIP6Address *address)
{
	NMSettingIP6ConfigPrivate *priv;
	NMIP6Address *copy;
	GSList *iter;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);
	g_return_val_if_fail (address != NULL, FALSE);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	for (iter = priv->addresses; iter; iter = g_slist_next (iter)) {
		if (nm_ip6_address_compare ((NMIP6Address *) iter->data, address))
			return FALSE;
	}

	copy = nm_ip6_address_dup (address);
	priv->addresses = g_slist_append (priv->addresses, copy);
495
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES);
496 497 498
	return TRUE;
}

499 500 501 502 503 504 505
/**
 * nm_setting_ip6_config_remove_address:
 * @setting: the #NMSettingIP6Config
 * @i: index number of the address to remove
 *
 * Removes the address at index @i.
 **/
506 507 508 509 510 511 512 513 514 515 516 517 518 519
void
nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *elt;

	g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting));

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	elt = g_slist_nth (priv->addresses, i);
	g_return_if_fail (elt != NULL);

	nm_ip6_address_unref ((NMIP6Address *) elt->data);
	priv->addresses = g_slist_delete_link (priv->addresses, elt);
520
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES);
521 522
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
/**
 * nm_setting_ip6_config_remove_address_by_value:
 * @setting: the #NMSettingIP6Config
 * @address: the address to remove
 *
 * Removes the address @address.
 *
 * Returns: %TRUE if the address was found and removed; %FALSE if it was not.
 *
 * Since: 0.9.10
 **/
gboolean
nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting,
                                               NMIP6Address *address)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *iter;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);
	g_return_val_if_fail (address != NULL, FALSE);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	for (iter = priv->addresses; iter; iter = g_slist_next (iter)) {
		if (nm_ip6_address_compare ((NMIP6Address *) iter->data, address)) {
			priv->addresses = g_slist_delete_link (priv->addresses, iter);
			g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES);
			return TRUE;
		}
	}
	return FALSE;
}

555 556 557 558 559 560
/**
 * nm_setting_ip6_config_clear_addresses:
 * @setting: the #NMSettingIP6Config
 *
 * Removes all configured addresses.
 **/
561 562 563 564 565 566 567
void
nm_setting_ip6_config_clear_addresses (NMSettingIP6Config *setting)
{
	NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);

	g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting));

568
	g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip6_address_unref);
569
	priv->addresses = NULL;
570
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES);
571 572
}

573 574 575 576 577 578
/**
 * nm_setting_ip6_config_get_num_routes:
 * @setting: the #NMSettingIP6Config
 *
 * Returns: the number of configured routes
 **/
579 580 581 582 583 584 585 586
guint32
nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), 0);

	return g_slist_length (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->routes);
}

587 588 589 590 591 592 593
/**
 * nm_setting_ip6_config_get_route:
 * @setting: the #NMSettingIP6Config
 * @i: index number of the route to return
 *
 * Returns: the route at index @i
 **/
594 595 596 597 598 599 600 601 602 603 604 605 606
NMIP6Route *
nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i)
{
	NMSettingIP6ConfigPrivate *priv;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	g_return_val_if_fail (i <= g_slist_length (priv->routes), NULL);

	return (NMIP6Route *) g_slist_nth_data (priv->routes, i);
}

607 608 609 610 611 612 613 614 615 616
/**
 * nm_setting_ip6_config_add_route:
 * @setting: the #NMSettingIP6Config
 * @route: the route to add
 *
 * Adds a new IPv6 route and associated information to the setting.  The
 * given route is duplicated internally and is not changed by this function.
 *
 * Returns: %TRUE if the route was added; %FALSE if the route was already known.
 **/
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
gboolean
nm_setting_ip6_config_add_route (NMSettingIP6Config *setting,
                                 NMIP6Route *route)
{
	NMSettingIP6ConfigPrivate *priv;
	NMIP6Route *copy;
	GSList *iter;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);
	g_return_val_if_fail (route != NULL, FALSE);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	for (iter = priv->routes; iter; iter = g_slist_next (iter)) {
		if (nm_ip6_route_compare ((NMIP6Route *) iter->data, route))
			return FALSE;
	}

	copy = nm_ip6_route_dup (route);
	priv->routes = g_slist_append (priv->routes, copy);
636
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES);
637 638 639
	return TRUE;
}

640 641 642 643 644 645 646
/**
 * nm_setting_ip6_config_remove_route:
 * @setting: the #NMSettingIP6Config
 * @i: index number of the route
 *
 * Removes the route at index @i.
 **/
647 648 649 650 651 652 653 654 655 656 657 658 659 660
void
nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *elt;

	g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting));

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	elt = g_slist_nth (priv->routes, i);
	g_return_if_fail (elt != NULL);

	nm_ip6_route_unref ((NMIP6Route *) elt->data);
	priv->routes = g_slist_delete_link (priv->routes, elt);
661
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES);
662 663
}

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
/**
 * nm_setting_ip6_config_remove_route_by_value:
 * @setting: the #NMSettingIP6Config
 * @route: the route to remove
 *
 * Removes the route @route.
 *
 * Returns: %TRUE if the route was found and removed; %FALSE if it was not.
 *
 * Since: 0.9.10
 **/
gboolean
nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting,
                                             NMIP6Route *route)
{
	NMSettingIP6ConfigPrivate *priv;
	GSList *iter;

	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);
	g_return_val_if_fail (route != NULL, FALSE);

	priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
	for (iter = priv->routes; iter; iter = g_slist_next (iter)) {
		if (nm_ip6_route_compare ((NMIP6Route *) iter->data, route)) {
			nm_ip6_route_unref ((NMIP6Route *) iter->data);
			priv->routes = g_slist_delete_link (priv->routes, iter);
			g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES);
			return TRUE;
		}
	}
	return FALSE;
}

697 698 699 700 701 702
/**
 * nm_setting_ip6_config_clear_routes:
 * @setting: the #NMSettingIP6Config
 *
 * Removes all configured routes.
 **/
703 704 705 706 707 708 709
void
nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting)
{
	NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);

	g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting));

710
	g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref);
711
	priv->routes = NULL;
712
	g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES);
713 714
}

715 716
/**
 * nm_setting_ip6_config_get_route_metric:
717
 * @setting: the #NMSettingIP6Config
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
 *
 * Returns the value contained in the #NMSettingIP6Config:route-metric
 * property.
 *
 * Returns: the route metric that is used for IPv6 routes that don't explicitly
 * specify a metric. See #NMSettingIP6Config:route-metric for more details.
 *
 * Since: 1.0
 **/
gint64
nm_setting_ip6_config_get_route_metric (NMSettingIP6Config *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), -1);

	return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->route_metric;
}

735 736 737 738 739 740 741 742 743 744
/**
 * nm_setting_ip6_config_get_ignore_auto_routes:
 * @setting: the #NMSettingIP6Config
 *
 * Returns the value contained in the #NMSettingIP6Config:ignore-auto-routes
 * property.
 *
 * Returns: %TRUE if automatically configured (ie via DHCP) routes should be
 * ignored.
 **/
745
gboolean
746
nm_setting_ip6_config_get_ignore_auto_routes (NMSettingIP6Config *setting)
747 748 749
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);

750
	return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->ignore_auto_routes;
751 752
}

753 754 755 756 757 758 759 760 761 762
/**
 * nm_setting_ip6_config_get_ignore_auto_dns:
 * @setting: the #NMSettingIP6Config
 *
 * Returns the value contained in the #NMSettingIP6Config:ignore-auto-dns
 * property.
 *
 * Returns: %TRUE if automatically configured (ie via DHCP or router
 * advertisements) DNS information should be ignored.
 **/
763
gboolean
764
nm_setting_ip6_config_get_ignore_auto_dns (NMSettingIP6Config *setting)
765 766 767
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);

768
	return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->ignore_auto_dns;
769 770
}

771 772 773 774 775 776 777 778 779 780
/**
 * nm_setting_ip6_config_get_never_default:
 * @setting: the #NMSettingIP6Config
 *
 * Returns the value contained in the #NMSettingIP6Config:never-default
 * property.
 *
 * Returns: %TRUE if this connection should never be the default connection
 * for IPv6 addressing
 **/
781 782
gboolean
nm_setting_ip6_config_get_never_default (NMSettingIP6Config *setting)
783 784 785
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);

786
	return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->never_default;
787 788
}

789 790 791 792 793 794 795 796 797 798
/**
 * nm_setting_ip6_config_get_may_fail:
 * @setting: the #NMSettingIP6Config
 *
 * Returns the value contained in the #NMSettingIP6Config:may-fail
 * property.
 *
 * Returns: %TRUE if this connection doesn't require IPv6 addressing to complete
 * for the connection to succeed.
 **/
799 800 801 802 803 804 805 806
gboolean
nm_setting_ip6_config_get_may_fail (NMSettingIP6Config *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE);

	return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->may_fail;
}

807 808 809 810 811 812 813 814 815 816 817 818
/**
 * nm_setting_ip6_config_get_ip6_privacy:
 * @setting: the #NMSettingIP6Config
 *
 * Returns the value contained in the #NMSettingIP6Config:ip6-privacy
 * property.
 *
 * Returns: IPv6 Privacy Extensions configuration value (#NMSettingIP6ConfigPrivacy).
 **/
NMSettingIP6ConfigPrivacy
nm_setting_ip6_config_get_ip6_privacy (NMSettingIP6Config *setting)
{
819
	g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN);
820 821 822 823

	return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->ip6_privacy;
}

824 825 826
static gboolean
verify (NMSetting *setting, GSList *all_settings, GError **error)
{
827
	NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
828

829
	if (!priv->method) {
830 831 832 833
		g_set_error_literal (error,
		                     NM_SETTING_IP6_CONFIG_ERROR,
		                     NM_SETTING_IP6_CONFIG_ERROR_MISSING_PROPERTY,
		                     _("property is missing"));
834
		g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_METHOD);
835 836 837
		return FALSE;
	}

838 839
	if (!strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) {
		if (!priv->addresses) {
840 841 842 843 844
			g_set_error (error,
			             NM_SETTING_IP6_CONFIG_ERROR,
			             NM_SETTING_IP6_CONFIG_ERROR_MISSING_PROPERTY,
			             _("this property cannot be empty for '%s=%s'"),
			             NM_SETTING_IP6_CONFIG_METHOD, priv->method);
845
			g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ADDRESSES);
846 847
			return FALSE;
		}
848
	} else if (   !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE)
849
	           || !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)
850
	           || !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_SHARED)) {
851 852 853 854
		if (g_slist_length (priv->dns)) {
			g_set_error (error,
			             NM_SETTING_IP6_CONFIG_ERROR,
			             NM_SETTING_IP6_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD,
855 856 857
			             _("'%s' not allowed for %s=%s"),
			             _("this property is not allowed for '%s=%s'"),
			             NM_SETTING_IP6_CONFIG_METHOD, priv->method);
858
			g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS);
859 860 861 862 863 864 865
			return FALSE;
		}

		if (g_slist_length (priv->dns_search)) {
			g_set_error (error,
			             NM_SETTING_IP6_CONFIG_ERROR,
			             NM_SETTING_IP6_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD,
866 867
			             _("this property is not allowed for '%s=%s'"),
			             NM_SETTING_IP6_CONFIG_METHOD, priv->method);
868
			g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS_SEARCH);
869
			return FALSE;
870 871
		}

872
		if (g_slist_length (priv->addresses)) {
873 874 875
			g_set_error (error,
			             NM_SETTING_IP6_CONFIG_ERROR,
			             NM_SETTING_IP6_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD,
876
			             _("this property is not allowed for '%s=%s'"),
877
			             NM_SETTING_IP6_CONFIG_METHOD, priv->method);
878
			g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ADDRESSES);
879 880
			return FALSE;
		}
881 882
	} else if (   !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)
	           || !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_DHCP)) {
883
		/* nothing to do */
884
	} else {
885 886 887 888
		g_set_error_literal (error,
		                     NM_SETTING_IP6_CONFIG_ERROR,
		                     NM_SETTING_IP6_CONFIG_ERROR_INVALID_PROPERTY,
		                     _("property is invalid"));
889
		g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_METHOD);
890 891 892
		return FALSE;
	}

893
	if (priv->dhcp_hostname && !strlen (priv->dhcp_hostname)) {
894 895 896 897
		g_set_error_literal (error,
		                     NM_SETTING_IP6_CONFIG_ERROR,
		                     NM_SETTING_IP6_CONFIG_ERROR_INVALID_PROPERTY,
		                     _("property is missing"));
898
		g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME);
899 900 901
		return FALSE;
	}

902 903 904 905 906 907 908 909 910 911 912 913
	return TRUE;
}


static void
nm_setting_ip6_config_init (NMSettingIP6Config *setting)
{
}

static void
finalize (GObject *object)
{
914
	NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (object);
915

916
	g_free (priv->method);
917 918
	g_free (priv->dhcp_hostname);

919
	g_slist_free_full (priv->dns, g_free);
920 921 922
	g_slist_free_full (priv->dns_search, g_free);
	g_slist_free_full (priv->addresses, g_free);
	g_slist_free_full (priv->routes, g_free);
923 924 925 926 927 928

	G_OBJECT_CLASS (nm_setting_ip6_config_parent_class)->finalize (object);
}

static void
set_property (GObject *object, guint prop_id,
929
              const GValue *value, GParamSpec *pspec)
930
{
931
	NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (object);
932 933 934

	switch (prop_id) {
	case PROP_METHOD:
935 936
		g_free (priv->method);
		priv->method = g_value_dup_string (value);
937 938
		break;
	case PROP_DNS:
939
		g_slist_free_full (priv->dns, g_free);
940
		priv->dns = nm_utils_ip6_dns_from_gvalue (value);
941 942
		break;
	case PROP_DNS_SEARCH:
943
		g_slist_free_full (priv->dns_search, g_free);
944
		priv->dns_search = g_value_dup_boxed (value);
945 946
		break;
	case PROP_ADDRESSES:
947
		g_slist_free_full (priv->addresses, g_free);
948
		priv->addresses = nm_utils_ip6_addresses_from_gvalue (value);
949
		break;
950
	case PROP_ROUTES:
951
		g_slist_free_full (priv->routes, g_free);
952 953
		priv->routes = nm_utils_ip6_routes_from_gvalue (value);
		break;
954 955 956
	case PROP_ROUTE_METRIC:
		priv->route_metric = g_value_get_int64 (value);
		break;
957 958
	case PROP_IGNORE_AUTO_ROUTES:
		priv->ignore_auto_routes = g_value_get_boolean (value);
959
		break;
960 961
	case PROP_IGNORE_AUTO_DNS:
		priv->ignore_auto_dns = g_value_get_boolean (value);
962
		break;
963 964 965 966
	case PROP_DHCP_HOSTNAME:
		g_free (priv->dhcp_hostname);
		priv->dhcp_hostname = g_value_dup_string (value);
		break;
967 968
	case PROP_NEVER_DEFAULT:
		priv->never_default = g_value_get_boolean (value);
969
		break;
970 971 972
	case PROP_MAY_FAIL:
		priv->may_fail = g_value_get_boolean (value);
		break;
973 974 975
	case PROP_IP6_PRIVACY:
		priv->ip6_privacy = g_value_get_int (value);
		break;
976 977 978 979 980 981 982 983
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
get_property (GObject *object, guint prop_id,
984
              GValue *value, GParamSpec *pspec)
985
{
986
	NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (object);
987 988 989

	switch (prop_id) {
	case PROP_METHOD:
990
		g_value_set_string (value, priv->method);
991 992
		break;
	case PROP_DNS:
993
		nm_utils_ip6_dns_to_gvalue (priv->dns, value);
994 995
		break;
	case PROP_DNS_SEARCH:
996
		g_value_set_boxed (value, priv->dns_search);
997 998
		break;
	case PROP_ADDRESSES:
999
		nm_utils_ip6_addresses_to_gvalue (priv->addresses, value);
1000 1001
		break;
	case PROP_ROUTES:
1002 1003
		nm_utils_ip6_routes_to_gvalue (priv->routes, value);
		break;
1004 1005 1006
	case PROP_ROUTE_METRIC:
		g_value_set_int64 (value, priv->route_metric);
		break;
1007 1008
	case PROP_IGNORE_AUTO_ROUTES:
		g_value_set_boolean (value, priv->ignore_auto_routes);
1009
		break;
1010 1011
	case PROP_IGNORE_AUTO_DNS:
		g_value_set_boolean (value, priv->ignore_auto_dns);
1012
		break;
1013 1014 1015
	case PROP_DHCP_HOSTNAME:
		g_value_set_string (value, priv->dhcp_hostname);
		break;
1016 1017
	case PROP_NEVER_DEFAULT:
		g_value_set_boolean (value, priv->never_default);
1018
		break;
1019 1020 1021
	case PROP_MAY_FAIL:
		g_value_set_boolean (value, priv->may_fail);
		break;
1022 1023 1024
	case PROP_IP6_PRIVACY:
		g_value_set_int (value, priv->ip6_privacy);
		break;
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
	NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);

1037 1038
	g_type_class_add_private (setting_class, sizeof (NMSettingIP6ConfigPrivate));

1039 1040 1041 1042 1043 1044 1045
	/* virtual methods */
	object_class->set_property = set_property;
	object_class->get_property = get_property;
	object_class->finalize     = finalize;
	parent_class->verify       = verify;

	/* Properties */
1046 1047 1048
	/**
	 * NMSettingIP6Config:method:
	 *
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	 * IPv6 configuration method.  If "auto" is specified then the appropriate
	 * automatic method (PPP, router advertisement, etc) is used for the device
	 * and most other properties can be left unset.  To force the use of DHCP
	 * only, specify "dhcp"; this method is only valid for Ethernet- based
	 * hardware.  If "link-local" is specified, then an IPv6 link-local address
	 * will be assigned to the interface.  If "manual" is specified, static IP
	 * addressing is used and at least one IP address must be given in the
	 * "addresses" property.  If "ignore" is specified, IPv6 configuration is
	 * not done. This property must be set.  Note: the "shared" method is not
	 * yet supported.
1059
	 **/
1060 1061
	g_object_class_install_property
		(object_class, PROP_METHOD,
1062 1063
		 g_param_spec_string (NM_SETTING_IP6_CONFIG_METHOD, "", "",
		                      NULL,
1064 1065
		                      G_PARAM_READWRITE |
		                      NM_SETTING_PARAM_INFERRABLE |
1066
		                      G_PARAM_STATIC_STRINGS));
1067

1068 1069 1070
	/**
	 * NMSettingIP6Config:dhcp-hostname:
	 *
1071 1072
	 * The specified name will be sent to the DHCP server when acquiring a
	 * lease.
1073 1074
	 *
	 * Since: 0.9.8
1075 1076 1077
	 **/
	g_object_class_install_property
		(object_class, PROP_DHCP_HOSTNAME,
1078
		 g_param_spec_string (NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME, "", "",
1079
		                      NULL,
1080 1081
		                      G_PARAM_READWRITE |
		                      G_PARAM_STATIC_STRINGS));
1082

1083 1084 1085 1086 1087
	/**
	 * NMSettingIP6Config:dns:
	 *
	 * Array of DNS servers, where each member of the array is a byte array
	 * containing the IPv6 address of the DNS server (in network byte order).
1088 1089 1090 1091
	 * For the "auto" method, these DNS servers are appended to those (if any)
	 * returned by automatic configuration.  DNS servers cannot be used with the
	 * "shared" or "link-local" methods as there is no usptream network. In all
	 * other methods, these DNS servers are used as the only DNS servers for
1092 1093
	 * this connection.
	 **/
1094 1095
	g_object_class_install_property
		(object_class, PROP_DNS,
1096 1097 1098 1099
		 _nm_param_spec_specialized (NM_SETTING_IP6_CONFIG_DNS, "", "",
		                             DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR,
		                             G_PARAM_READWRITE |
		                             G_PARAM_STATIC_STRINGS));
1100

1101 1102 1103
	/**
	 * NMSettingIP6Config:dns-search:
	 *
1104
	 * List of DNS search domains.  For the "auto" method, these search domains
1105
	 * are appended to those returned by automatic configuration. Search domains
1106 1107 1108
	 * cannot be used with the "shared" or "link-local" methods as there is no
	 * upstream network.  In all other methods, these search domains are used as
	 * the only search domains for this connection.
1109
	 **/
1110 1111
	g_object_class_install_property
		(object_class, PROP_DNS_SEARCH,
1112 1113 1114 1115
		 _nm_param_spec_specialized (NM_SETTING_IP6_CONFIG_DNS_SEARCH, "", "",
		                             DBUS_TYPE_G_LIST_OF_STRING,
		                             G_PARAM_READWRITE |
		                             G_PARAM_STATIC_STRINGS));
1116

1117 1118 1119 1120
	/**
	 * NMSettingIP6Config:addresses:
	 *
	 * Array of IPv6 address structures.  Each IPv6 address structure is
1121 1122 1123 1124
	 * composed of 3 members, the first being a byte array containing the IPv6
	 * address (network byte order), the second a 32-bit integer containing the
	 * IPv6 address prefix, and the third a byte array containing the IPv6
	 * address (network byte order) of the gateway associated with this address,
1125 1126 1127 1128
	 * if any.  If no gateway is given, the third element should be given as all
	 * zeros.  For the "auto" method, given IP addresses are appended to those
	 * returned by automatic configuration.  Addresses cannot be used with the
	 * "shared" or "link-local" methods as the interface is automatically
1129
	 * assigned an address with these methods.
1130
	 **/
1131 1132
	g_object_class_install_property
		(object_class, PROP_ADDRESSES,
1133 1134
		 _nm_param_spec_specialized (NM_SETTING_IP6_CONFIG_ADDRESSES, "", "",
		                             DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS,
1135 1136
		                             G_PARAM_READWRITE |
		                             NM_SETTING_PARAM_INFERRABLE |
1137
		                             G_PARAM_STATIC_STRINGS));
1138

1139 1140 1141
	/**
	 * NMSettingIP6Config:routes:
	 *
1142 1143 1144 1145
	 * Array of IPv6 route structures.  Each IPv6 route structure is composed of
	 * 4 members; the first being the destination IPv6 network or address
	 * (network byte order) as a byte array, the second the destination network
	 * or address IPv6 prefix, the third being the next-hop IPv6 address
1146
	 * (network byte order) if any, and the fourth being the route metric. For
1147 1148 1149
	 * the "auto" method, given IP routes are appended to those returned by
	 * automatic configuration.  Routes cannot be used with the "shared" or
	 * "link-local" methods because there is no upstream network.
1150
	 **/
1151 1152
	g_object_class_install_property
		(object_class, PROP_ROUTES,
1153 1154
		 _nm_param_spec_specialized (NM_SETTING_IP6_CONFIG_ROUTES, "", "",
		                             DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE,
1155 1156
		                             G_PARAM_READWRITE |
		                             NM_SETTING_PARAM_INFERRABLE |
1157
		                             G_PARAM_STATIC_STRINGS));
1158

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	/**
	 * NMSettingIP6Config:route-metric:
	 *
	 * The default metric for routes that don't explicitly specify a metric.
	 * The default value -1 means that the metric is choosen automatically
	 * based on the device type.
	 * The metric applies to dynamic routes, manual (static) routes that
	 * don't have an explicit metric setting, address prefix routes, and
	 * the default route.
	 * As the linux kernel replaces zero (0) by 1024 (user-default), setting
	 * this property to 0 means effectively setting it to 1024.
	 *
	 * Since: 1.0
	 **/
	g_object_class_install_property
	    (object_class, PROP_ROUTE_METRIC,
	     g_param_spec_int64 (NM_SETTING_IP6_CONFIG_ROUTE_METRIC, "", "",
	                         -1, G_MAXUINT32, -1,
	                         G_PARAM_READWRITE |
	                         G_PARAM_CONSTRUCT |
	                         G_PARAM_STATIC_STRINGS));

1181 1182 1183
	/**
	 * NMSettingIP6Config:ignore-auto-routes:
	 *
1184
	 * When the method is set to "auto" or "dhcp" and this property is set to
1185
	 * %TRUE, automatically configured routes are ignored and only routes
1186
	 * specified in the #NMSettingIP6Config:routes property, if any, are used.
1187
	 **/
1188 1189
	g_object_class_install_property
		(object_class, PROP_IGNORE_AUTO_ROUTES,
1190 1191
		 g_param_spec_boolean (NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, "", "",
		                       FALSE,
1192 1193
		                       G_PARAM_READWRITE |
		                       G_PARAM_CONSTRUCT |
1194
		                       G_PARAM_STATIC_STRINGS));
1195

1196 1197 1198
	/**
	 * NMSettingIP6Config:ignore-auto-dns:
	 *
1199 1200 1201 1202 1203
	 * When the method is set to "auto" or "dhcp" and this property is set to
	 * %TRUE, automatically configured nameservers and search domains are
	 * ignored and only nameservers and search domains specified in the
	 * #NMSettingIP6Config:dns and #NMSettingIP6Config:dns-search properties, if
	 * any, are used.
1204
	 **/
1205
	g_object_class_install_property
1206
		(object_class, PROP_IGNORE_AUTO_DNS,
1207 1208
		 g_param_spec_boolean (NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS, "", "",
		                       FALSE,
1209 1210
		                       G_PARAM_READWRITE |
		                       G_PARAM_CONSTRUCT |
1211
		                       G_PARAM_STATIC_STRINGS));
1212

1213 1214 1215
	/**
	 * NMSettingIP6Config:never-default:
	 *
1216
	 * If %TRUE, this connection will never be the default IPv6 connection,
1217 1218
	 * meaning it will never be assigned the default IPv6 route by
	 * NetworkManager.
1219
	 **/
1220
	g_object_class_install_property
1221
		(object_class, PROP_NEVER_DEFAULT,
1222 1223
		 g_param_spec_boolean (NM_SETTING_IP6_CONFIG_NEVER_DEFAULT, "", "",
		                       FALSE,
1224 1225
		                       G_PARAM_READWRITE |
		                       G_PARAM_CONSTRUCT |
1226
		                       G_PARAM_STATIC_STRINGS));
1227

1228 1229 1230
	/**
	 * NMSettingIP6Config:may-fail:
	 *
1231
	 * If %TRUE, allow overall network configuration to proceed even if IPv6
1232 1233 1234 1235 1236
	 * configuration times out.  Note that at least one IP configuration must
	 * succeed or overall network configuration will still fail.  For example,
	 * in IPv4-only networks, setting this property to %TRUE allows the overall
	 * network configuration to succeed if IPv6 configuration fails but IPv4
	 * configuration completes successfully.
1237 1238 1239
	 **/
	g_object_class_install_property
		(object_class, PROP_MAY_FAIL,
1240 1241
		 g_param_spec_boolean (NM_SETTING_IP6_CONFIG_MAY_FAIL, "", "",
		                       TRUE,
1242 1243
		                       G_PARAM_READWRITE |
		                       G_PARAM_CONSTRUCT |
1244
		                       G_PARAM_STATIC_STRINGS));
1245 1246 1247 1248

	/**
	 * NMSettingIP6Config:ip6-privacy:
	 *
1249 1250 1251 1252 1253 1254 1255
	 * Configure IPv6 Privacy Extensions for SLAAC, described in RFC4941.  If
	 * enabled, it makes the kernel generate a temporary IPv6 address in
	 * addition to the public one generated from MAC address via modified
	 * EUI-64.  This enhances privacy, but could cause problems in some
	 * applications, on the other hand.  The permitted values are: 0: disabled,
	 * 1: enabled (prefer public address), 2: enabled (prefer temporary
	 * addresses).
1256 1257 1258
	 **/
	g_object_class_install_property
		(object_class, PROP_IP6_PRIVACY,
1259
		 g_param_spec_int (NM_SETTING_IP6_CONFIG_IP6_PRIVACY, "", "",
1260 1261 1262
		                   NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN,
		                   NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR,
		                   NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN,
1263 1264
		                   G_PARAM_READWRITE |
		                   G_PARAM_CONSTRUCT |
1265
		                   G_PARAM_STATIC_STRINGS));
1266
}
1267

1268 1269
/********************************************************************/

1270 1271
struct NMIP6Address {
	guint32 refcount;
1272
	struct in6_addr address;
1273
	guint32 prefix;
1274
	struct in6_addr gateway;
1275 1276
};

1277 1278 1279 1280 1281 1282 1283
/**
 * nm_ip6_address_new:
 *
 * Creates and returns a new #NMIP6Address object.
 *
 * Returns: (transfer full): the new empty #NMIP6Address object
 **/
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
NMIP6Address *
nm_ip6_address_new (void)
{
	NMIP6Address *address;

	address = g_malloc0 (sizeof (NMIP6Address));
	address->refcount = 1;
	return address;
}

1294 1295 1296 1297 1298 1299 1300 1301
/**
 * nm_ip6_address_dup:
 * @source: the #NMIP6Address object to copy
 *
 * Copies a given #NMIP6Address object and returns the copy.
 *
 * Returns: (transfer full): the copy of the given #NMIP6Address copy
 **/
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
NMIP6Address *
nm_ip6_address_dup (NMIP6Address *source)
{
	NMIP6Address *address;

	g_return_val_if_fail (source != NULL, NULL);
	g_return_val_if_fail (source->refcount > 0, NULL);

	address = nm_ip6_address_new ();
	address->prefix = source->prefix;
1312
	memcpy (&address->address, &source->address, sizeof (struct in6_addr));
1313
	memcpy (&address->gateway, &source->gateway, sizeof (struct in6_addr));
1314 1315 1316 1317

	return address;
}

1318 1319 1320 1321 1322 1323
/**
 * nm_ip6_address_ref:
 * @address: the #NMIP6Address
 *
 * Increases the reference count of the object.
 **/
1324 1325 1326 1327 1328 1329 1330 1331 1332
void
nm_ip6_address_ref (NMIP6Address *address)
{
	g_return_if_fail (address != NULL);
	g_return_if_fail (address->refcount > 0);

	address->refcount++;
}

1333 1334 1335 1336 1337 1338 1339
/**
 * nm_ip6_address_unref:
 * @address: the #NMIP6Address
 *
 * Decreases the reference count of the object.  If the reference count
 * reaches zero, the object will be destroyed.
 **/
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
void
nm_ip6_address_unref (NMIP6Address *address)
{
	g_return_if_fail (address != NULL);
	g_return_if_fail (address->refcount > 0);

	address->refcount--;
	if (address->refcount == 0) {
		memset (address, 0, sizeof (NMIP6Address));
		g_free (address);
	}
}

1353 1354 1355 1356 1357 1358 1359 1360 1361
/**
 * nm_ip6_address_compare:
 * @address: the #NMIP6Address
 * @other: the #NMIP6Address to compare @address to.
 *
 * Determines if two #NMIP6Address objects contain the same values.
 *
 * Returns: %TRUE if the objects contain the same values, %FALSE if they do not.
 **/
1362 1363 1364 1365 1366 1367 1368 1369 1370
gboolean
nm_ip6_address_compare (NMIP6Address *address, NMIP6Address *other)
{
	g_return_val_if_fail (address != NULL, FALSE);
	g_return_val_if_fail (address->refcount > 0, FALSE);

	g_return_val_if_fail (other != NULL, FALSE);
	g_return_val_if_fail (other->refcount > 0, FALSE);

1371
	if (   memcmp (&address->address, &other->address, sizeof (struct in6_addr))
1372 1373
	    || address->prefix != other->prefix
	    || memcmp (&address->gateway, &other->gateway, sizeof (struct in6_addr)))
1374 1375 1376 1377
		return FALSE;
	return TRUE;
}

1378 1379 1380 1381 1382 1383
/**
 * nm_ip6_address_get_address:
 * @address: the #NMIP6Address
 *
 * Gets the IPv6 address property of this address object.
 *
1384 1385
 * Returns: (array fixed-size=16) (element-type guint8) (transfer none):
 *          the IPv6 address
1386
 **/
1387 1388 1389
const struct in6_addr *
nm_ip6_address_get_address (NMIP6Address *address)
{
1390 1391
	g_return_val_if_fail (address != NULL, NULL);
	g_return_val_if_fail (address->refcount > 0, NULL);
1392

1393
	return &address->address;
1394 1395
}

1396 1397 1398 1399 1400 1401 1402
/**
 * nm_ip6_address_set_address:
 * @address: the #NMIP6Address
 * @addr: the IPv6 address
 *
 * Sets the IPv6 address property of this object.
 **/
1403 1404 1405 1406 1407
void
nm_ip6_address_set_address (NMIP6Address *address, const struct in6_addr *addr)
{
	g_return_if_fail (address != NULL);
	g_return_if_fail (address->refcount > 0);
1408
	g_return_if_fail (addr != NULL);
1409

1410
	memcpy (&address->address, addr, sizeof (struct in6_addr));
1411 1412
}

1413 1414 1415 1416 1417 1418 1419 1420
/**
 * nm_ip6_address_get_prefix:
 * @address: the #NMIP6Address
 *
 * Gets the IPv6 address prefix property of this address object.
 *
 * Returns: the IPv6 address prefix
 **/
1421 1422 1423 1424 1425 1426 1427 1428 1429
guint32
nm_ip6_address_get_prefix (NMIP6Address *address)
{
	g_return_val_if_fail (address != NULL, 0);
	g_return_val_if_fail (address->refcount > 0, 0);

	return address->prefix;
}

1430 1431 1432 1433 1434 1435 1436
/**
 * nm_ip6_address_set_prefix:
 * @address: the #NMIP6Address
 * @prefix: the address prefix, a number between 0 and 128 inclusive
 *
 * Sets the IPv6 address prefix.
 **/
1437 1438 1439 1440 1441
void
nm_ip6_address_set_prefix (NMIP6Address *address, guint32 prefix)
{
	g_return_if_fail (address != NULL);
	g_return_if_fail (address->refcount > 0);
1442 1443
	g_return_if_fail (prefix <= 128);
	g_return_if_fail (prefix > 0);
1444 1445 1446 1447

	address->prefix = prefix;
}

1448 1449 1450 1451 1452 1453
/**
 * nm_ip6_address_get_gateway:
 * @address: the #NMIP6Address
 *
 * Gets the IPv6 default gateway property of this address object.
 *
1454 1455
 * Returns: (array fixed-size=16) (element-type guint8) (transfer none):
 *          the IPv6 gateway address
1456
 **/
1457 1458 1459
const struct in6_addr *
nm_ip6_address_get_gateway (NMIP6Address *address)
{
1460 1461
	g_return_val_if_fail (address != NULL, NULL);
	g_return_val_if_fail (address->refcount > 0, NULL);
1462 1463 1464 1465

	return &address->gateway;
}

1466 1467 1468 1469 1470 1471 1472
/**
 * nm_ip6_address_set_gateway:
 * @address: the #NMIP6Address
 * @gateway: the IPv6 default gateway
 *
 * Sets the IPv6 default gateway property of this address object.
 **/
Dan Williams's avatar