nm-platform.c 59.4 KB
Newer Older
Pavel Šimerda's avatar
Pavel Šimerda committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* nm-platform.c - Handle runtime kernel networking configuration
 *
 * 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) 2012 Red Hat, Inc.
 */

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
25
#include <arpa/inet.h>
26
#include <string.h>
27
#include <netlink/route/addr.h>
Pavel Šimerda's avatar
Pavel Šimerda committed
28

29
#include "NetworkManagerUtils.h"
30
#include "nm-utils.h"
Pavel Šimerda's avatar
Pavel Šimerda committed
31
#include "nm-platform.h"
32
#include "NetworkManagerUtils.h"
Pavel Šimerda's avatar
Pavel Šimerda committed
33
#include "nm-logging.h"
34
#include "nm-enum-types.h"
Pavel Šimerda's avatar
Pavel Šimerda committed
35 36 37 38 39 40 41 42 43 44 45 46

#define debug(...) nm_log_dbg (LOGD_PLATFORM, __VA_ARGS__)

#define NM_PLATFORM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_PLATFORM, NMPlatformPrivate))

G_DEFINE_TYPE (NMPlatform, nm_platform, G_TYPE_OBJECT)

/* NMPlatform signals */
enum {
	LINK_ADDED,
	LINK_CHANGED,
	LINK_REMOVED,
47 48 49 50 51 52
	IP4_ADDRESS_ADDED,
	IP4_ADDRESS_CHANGED,
	IP4_ADDRESS_REMOVED,
	IP6_ADDRESS_ADDED,
	IP6_ADDRESS_CHANGED,
	IP6_ADDRESS_REMOVED,
Pavel Šimerda's avatar
Pavel Šimerda committed
53 54 55 56 57 58
	IP4_ROUTE_ADDED,
	IP4_ROUTE_CHANGED,
	IP4_ROUTE_REMOVED,
	IP6_ROUTE_ADDED,
	IP6_ROUTE_CHANGED,
	IP6_ROUTE_REMOVED,
Pavel Šimerda's avatar
Pavel Šimerda committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

/******************************************************************/

/* Singleton NMPlatform subclass instance and cached class object */
static NMPlatform *platform = NULL;
static NMPlatformClass *klass = NULL;

/**
 * nm_platform_setup:
 * @type: The #GType for a subclass of #NMPlatform
 *
 * Do not use this function directly, it is intended to be called by
 * NMPlatform subclasses. For the linux platform initialization use
 * nm_linux_platform_setup() instead.
 *
 * Failing to set up #NMPlatform singleton results in a fatal error,
 * as well as trying to initialize it multiple times without freeing
 * it.
 *
 * NetworkManager will typically use only one platform object during
 * its run. Test programs might want to switch platform implementations,
 * though. This is done with a combination of nm_platform_free() and
 * nm_*_platform_setup().
 */
void
nm_platform_setup (GType type)
{
	gboolean status;

	g_assert (platform == NULL);

	platform = g_object_new (type, NULL);
	g_assert (NM_IS_PLATFORM (platform));

	klass = NM_PLATFORM_GET_CLASS (platform);
	g_assert (klass->setup);

	status = klass->setup (platform);
	g_assert (status);
}

/**
 * nm_platform_free:
 *
 * Free #NMPlatform singleton created by nm_*_platform_setup().
 */
void
nm_platform_free (void)
{
	g_assert (platform);

	g_object_unref (platform);
	platform = NULL;
}

/**
 * nm_platform_get:
 *
 * Retrieve #NMPlatform singleton. Use this whenever you want to connect to
 * #NMPlatform signals. It is an error to call it before nm_*_platform_setup()
 * or after nm_platform_free().
 *
 * Returns: (transfer none): The #NMPlatform singleton reference.
 */
NMPlatform *
nm_platform_get (void)
{
	g_assert (platform);

	return platform;
}

/******************************************************************/

137 138 139 140 141 142 143 144 145 146 147 148 149
/**
 * nm_platform_set_error:
 * @error: The error code
 *
 * Convenience function to falsify platform->error. It can be used for example
 * by functions that want to save the error, execute some operations and
 * restore it.
 */
void nm_platform_set_error (NMPlatformError error)
{
	platform->error = error;
}

Pavel Šimerda's avatar
Pavel Šimerda committed
150 151 152 153 154 155 156 157
/**
 * nm_platform_get_error:
 *
 * Convenience function to quickly retrieve the error code of the last
 * operation.
 *
 * Returns: Integer error code.
 */
158
NMPlatformError
Pavel Šimerda's avatar
Pavel Šimerda committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
nm_platform_get_error (void)
{
	g_assert (platform);

	return platform->error;
}

/**
 * nm_platform_get_error_message:
 *
 * Returns: Static human-readable string for the error. Don't free.
 */
const char *
nm_platform_get_error_msg (void)
{
	g_assert (platform);

	switch (platform->error) {
	case NM_PLATFORM_ERROR_NONE:
		return "unknown error";
	case NM_PLATFORM_ERROR_NOT_FOUND:
		return "object not found";
	case NM_PLATFORM_ERROR_EXISTS:
		return "object already exists";
183 184
	case NM_PLATFORM_ERROR_WRONG_TYPE:
		return "object is wrong type";
185 186
	case NM_PLATFORM_ERROR_NOT_SLAVE:
		return "link not a slave";
187 188
	case NM_PLATFORM_ERROR_NO_FIRMWARE:
		return "firmware not found";
Pavel Šimerda's avatar
Pavel Šimerda committed
189 190 191 192 193 194 195 196 197 198 199 200
	default:
		return "invalid error number";
	}
}

static void
reset_error (void)
{
	g_assert (platform);
	platform->error = NM_PLATFORM_ERROR_NONE;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
#define IFA_F_MANAGETEMPADDR_STR "mngtmpaddr"
#define IFA_F_NOPREFIXROUTE_STR "noprefixroute"
gboolean
nm_platform_check_support_libnl_extended_ifa_flags ()
{
	static int supported = -1;

	/* support for extended ifa-flags was added together
	 * with the IFA_F_MANAGETEMPADDR flag. So, check if libnl
	 * is able to parse this flag. */
	if (supported == -1)
		supported = rtnl_addr_str2flags (IFA_F_MANAGETEMPADDR_STR) == IFA_F_MANAGETEMPADDR;

	return supported;
}

217 218 219 220 221 222 223 224 225 226 227
gboolean
nm_platform_check_support_kernel_extended_ifa_flags ()
{
	g_return_val_if_fail (NM_IS_PLATFORM (platform), FALSE);

	if (!klass->check_support_kernel_extended_ifa_flags)
		return FALSE;

	return klass->check_support_kernel_extended_ifa_flags (platform);
}

Pavel Šimerda's avatar
Pavel Šimerda committed
228 229
/******************************************************************/

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/**
 * nm_platform_sysctl_set:
 * @path: Absolute option path
 * @value: Value to write
 *
 * This function is intended to be used for writing values to sysctl-style
 * virtual runtime configuration files. This includes not only /proc/sys
 * but also for example /sys/class.
 *
 * Returns: %TRUE on success.
 */
gboolean
nm_platform_sysctl_set (const char *path, const char *value)
{
	reset_error ();

	g_return_val_if_fail (path, FALSE);
	g_return_val_if_fail (value, FALSE);
	g_return_val_if_fail (klass->sysctl_set, FALSE);

	return klass->sysctl_set (platform, path, value);
}

/**
 * nm_platform_sysctl_get:
 * @path: Absolute path to sysctl
 *
 * Returns: (transfer full): Contents of the virtual sysctl file.
 */
char *
260
nm_platform_sysctl_get (const char *path)
261 262 263 264 265 266
{
	reset_error ();

	g_return_val_if_fail (path, NULL);
	g_return_val_if_fail (klass->sysctl_get, NULL);

267
	return klass->sysctl_get (platform, path);
268 269
}

270
/**
271
 * nm_platform_sysctl_get_int32:
272
 * @path: Absolute path to sysctl
273 274
 * @fallback: default value, if the content of path could not be read
 * as decimal integer.
275
 *
276 277 278
 * Returns: contents of the sysctl file parsed as s32 integer, or
 * @fallback on error. Also, on error, @errno will be set to a non-zero
 * value.
279
 */
280 281
gint32
nm_platform_sysctl_get_int32 (const char *path, gint32 fallback)
282
{
283 284
	char *value = NULL;
	gint32 ret;
285

286
	g_return_val_if_fail (path, fallback);
287

288
	if (path)
289
		value = nm_platform_sysctl_get (path);
290 291 292 293 294

	if (!value) {
		errno = EINVAL;
		return fallback;
	}
295

296 297
	ret = nm_utils_ascii_str_to_int64 (value, 10, G_MININT32, G_MAXINT32, fallback);
	g_free (value);
298 299 300
	return ret;
}

301 302
/******************************************************************/

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/**
 * nm_platform_query_devices:
 *
 * Emit #NMPlatform:link-added signals for all currently-known links.
 * Should only be called at startup.
 */
void
nm_platform_query_devices (void)
{
	GArray *links_array;
	NMPlatformLink *links;
	int i;

	links_array = nm_platform_link_get_all ();
	links = (NMPlatformLink *) links_array->data;
318 319 320 321
	for (i = 0; i < links_array->len; i++) {
		g_signal_emit (platform, signals[LINK_ADDED], 0,
		               links[i].ifindex, &links[i], NM_PLATFORM_REASON_INTERNAL);
	}
322 323 324
	g_array_unref (links_array);
}

Pavel Šimerda's avatar
Pavel Šimerda committed
325 326 327 328 329 330 331
/**
 * nm_platform_link_get_all:
 *
 * Retrieve a snapshot of configuration for all links at once. The result is
 * owned by the caller and should be freed with g_array_unref().
 */
GArray *
332
nm_platform_link_get_all (void)
Pavel Šimerda's avatar
Pavel Šimerda committed
333
{
334 335 336 337
	GArray *links, *result;
	guint i, j, nresult;
	GHashTable *unseen;
	NMPlatformLink *item;
338

Pavel Šimerda's avatar
Pavel Šimerda committed
339 340 341 342
	reset_error ();

	g_return_val_if_fail (klass->link_get_all, NULL);

343
	links = klass->link_get_all (platform);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 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 415 416 417 418 419 420 421 422 423 424 425 426 427

	if (!links || links->len == 0)
		return links;

	unseen = g_hash_table_new (g_direct_hash, g_direct_equal);
	for (i = 0; i < links->len; i++) {
		item = &g_array_index (links, NMPlatformLink, i);

		if (item->ifindex <= 0 || g_hash_table_contains (unseen, GINT_TO_POINTER (item->ifindex))) {
			g_warn_if_reached ();
			item->ifindex = 0;
			continue;
		}

		g_hash_table_insert (unseen, GINT_TO_POINTER (item->ifindex), NULL);
	}

#ifndef G_DISABLE_ASSERT
	/* Ensure that link_get_all returns a consistent and valid result. */
	for (i = 0; i < links->len; i++) {
		item = &g_array_index (links, NMPlatformLink, i);

		if (!item->ifindex)
			continue;
		if (item->master != 0) {
			g_warn_if_fail (item->master > 0);
			g_warn_if_fail (item->master != item->ifindex);
			g_warn_if_fail (g_hash_table_contains (unseen, GINT_TO_POINTER (item->master)));
		}
		if (item->parent != 0) {
			g_warn_if_fail (item->parent > 0);
			g_warn_if_fail (item->parent != item->ifindex);
			g_warn_if_fail (g_hash_table_contains (unseen, GINT_TO_POINTER (item->parent)));
		}
	}
#endif

	/* Re-order the links list such that children/slaves come after all ancestors */
	nresult = g_hash_table_size (unseen);
	result = g_array_sized_new (TRUE, TRUE, sizeof (NMPlatformLink), nresult);
	g_array_set_size (result, nresult);

	j = 0;
	do {
		gboolean found_something = FALSE;
		guint first_idx = G_MAXUINT;

		for (i = 0; i < links->len; i++) {
			item = &g_array_index (links, NMPlatformLink, i);

			if (!item->ifindex)
				continue;

			if (first_idx == G_MAXUINT)
				first_idx = i;

			g_assert (g_hash_table_contains (unseen, GINT_TO_POINTER (item->ifindex)));

			if (item->master > 0 && g_hash_table_contains (unseen, GINT_TO_POINTER (item->master)))
				continue;
			if (item->parent > 0 && g_hash_table_contains (unseen, GINT_TO_POINTER (item->parent)))
				continue;

			g_hash_table_remove (unseen, GINT_TO_POINTER (item->ifindex));
			g_array_index (result, NMPlatformLink, j++) = *item;
			item->ifindex = 0;
			found_something = TRUE;
		}

		if (!found_something) {
			/* there is a circle, pop the first (remaining) element from the list */
			g_warn_if_reached ();
			item = &g_array_index (links, NMPlatformLink, first_idx);

			g_hash_table_remove (unseen, GINT_TO_POINTER (item->ifindex));
			g_array_index (result, NMPlatformLink, j++) = *item;
			item->ifindex = 0;
		}
	} while (j < nresult);

	g_hash_table_destroy (unseen);
	g_array_free (links, TRUE);

	return result;
Pavel Šimerda's avatar
Pavel Šimerda committed
428 429 430 431 432 433 434 435
}

/**
 * nm_platform_link_add:
 * @name: Interface name
 * @type: Interface type
 *
 * Add a software interface. Sets platform->error to NM_PLATFORM_ERROR_EXISTS
436 437
 * if interface is already already exists.  Any link-added signal will be
 * emitted from an idle handler and not within this function.
Pavel Šimerda's avatar
Pavel Šimerda committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
 */
static gboolean
nm_platform_link_add (const char *name, NMLinkType type)
{
	reset_error ();

	g_return_val_if_fail (name, FALSE);
	g_return_val_if_fail (klass->link_add, FALSE);

	if (nm_platform_link_exists (name)) {
		debug ("link: already exists");
		platform->error = NM_PLATFORM_ERROR_EXISTS;
		return FALSE;
	}

	return klass->link_add (platform, name, type);
}

/**
 * nm_platform_dummy_add:
 * @name: New interface name
 *
 * Create a software ethernet-like interface
 */
gboolean
nm_platform_dummy_add (const char *name)
{
	g_return_val_if_fail (name, FALSE);

	debug ("link: adding dummy '%s'", name);
	return nm_platform_link_add (name, NM_LINK_TYPE_DUMMY);
}

/**
 * nm_platform_link_exists:
 * @name: Interface name
 *
 * Returns: %TRUE if an interface of this name exists, %FALSE otherwise.
 */
gboolean
nm_platform_link_exists (const char *name)
{
	int ifindex = nm_platform_link_get_ifindex (name);

	reset_error();
	return ifindex > 0;
}

/**
 * nm_platform_link_delete:
 * @ifindex: Interface index
 *
 * Delete a software interface. Sets platform->error to
 * NM_PLATFORM_ERROR_NOT_FOUND if ifindex not available.
 */
gboolean
nm_platform_link_delete (int ifindex)
{
	const char *name;

	reset_error ();

	g_return_val_if_fail (klass->link_delete, FALSE);

	name = nm_platform_link_get_name (ifindex);

	if (!name)
		return FALSE;

	debug ("link: deleting '%s' (%d)", name, ifindex);
	return klass->link_delete (platform, ifindex);
}

/**
 * nm_platform_link_get_index:
 * @name: Interface name
 *
 * Returns: The interface index corresponding to the given interface name
 * or 0. Inteface name is owned by #NMPlatform, don't free it.
 */
int
nm_platform_link_get_ifindex (const char *name)
{
	int ifindex;

	reset_error ();

	g_return_val_if_fail (name, 0);
	g_return_val_if_fail (klass->link_get_ifindex, 0);

	ifindex = klass->link_get_ifindex (platform, name);

	if (!ifindex) {
		debug ("link not found: %s", name);
		platform->error = NM_PLATFORM_ERROR_NOT_FOUND;
	}

	return ifindex;
}

/**
 * nm_platform_link_get_name:
 * @name: Interface name
 *
 * Returns: The interface name corresponding to the given interface index
Martin Pitt's avatar
Martin Pitt committed
543
 * or %NULL.
Pavel Šimerda's avatar
Pavel Šimerda committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
 */
const char *
nm_platform_link_get_name (int ifindex)
{
	const char *name;

	reset_error ();

	g_return_val_if_fail (klass->link_get_name, NULL);

	name = klass->link_get_name (platform, ifindex);

	if (!name) {
		debug ("link not found: %d", ifindex);
		platform->error = NM_PLATFORM_ERROR_NOT_FOUND;
		return FALSE;
	}

	return name;
}

/**
 * nm_platform_link_get_type:
 * @ifindex: Interface index.
 *
 * Returns: Link type constant as defined in nm-platform.h. On error,
 * NM_LINK_TYPE_NONE is returned.
 */
NMLinkType
nm_platform_link_get_type (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (klass->link_get_type, NM_LINK_TYPE_NONE);

	return klass->link_get_type (platform, ifindex);
}

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
/**
 * nm_platform_link_get_type_name:
 * @ifindex: Interface index.
 *
 * Returns: A string describing the type of link. In some cases this
 * may be more specific than nm_platform_link_get_type(), but in
 * other cases it may not. On error, %NULL is returned.
 */
const char *
nm_platform_link_get_type_name (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (klass->link_get_type_name, NULL);

	return klass->link_get_type_name (platform, ifindex);
}

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
/**
 * nm_platform_link_is_software:
 * @ifindex: Interface index.
 *
 * Returns: %TRUE if ifindex belongs to a software interface, not backed by
 * a physical device.
 */
gboolean
nm_platform_link_is_software (int ifindex)
{
	return (nm_platform_link_get_type (ifindex) & 0x10000);
}

/**
 * nm_platform_link_supports_slaves:
 * @ifindex: Interface index.
 *
 * Returns: %TRUE if ifindex belongs to an interface capable of enslaving
 * other interfaces.
 */
gboolean
nm_platform_link_supports_slaves (int ifindex)
{
	return (nm_platform_link_get_type (ifindex) & 0x20000);
}
625

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
/**
 * nm_platform_link_refresh:
 * @ifindex: Interface index
 *
 * Reload the cache for ifindex synchronously.
 */
gboolean
nm_platform_link_refresh (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);

	if (klass->link_refresh)
		return klass->link_refresh (platform, ifindex);

	return TRUE;
}

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 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
/**
 * nm_platform_link_is_up:
 * @ifindex: Interface index
 *
 * Check if the interface is up.
 */
gboolean
nm_platform_link_is_up (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex >= 0, FALSE);
	g_return_val_if_fail (klass->link_is_up, FALSE);

	return klass->link_is_up (platform, ifindex);
}

/**
 * nm_platform_link_is_connected:
 * @ifindex: Interface index
 *
 * Check if the interface is connected.
 */
gboolean
nm_platform_link_is_connected (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex >= 0, FALSE);
	g_return_val_if_fail (klass->link_is_connected, FALSE);

	return klass->link_is_connected (platform, ifindex);
}

/**
 * nm_platform_link_uses_arp:
 * @ifindex: Interface index
 *
 * Check if the interface is configured to use ARP.
 */
gboolean
nm_platform_link_uses_arp (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex >= 0, FALSE);
	g_return_val_if_fail (klass->link_uses_arp, FALSE);

	return klass->link_uses_arp (platform, ifindex);
}

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
/**
 * nm_platform_link_set_address:
 * @ifindex: Interface index
 * @address: The new MAC address
 *
 * Set interface MAC address.
 */
gboolean
nm_platform_link_set_address (int ifindex, gconstpointer address, size_t length)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (address, FALSE);
	g_return_val_if_fail (length > 0, FALSE);
	g_return_val_if_fail (klass->link_set_address, FALSE);

	debug ("link: setting '%s' (%d) hardware address", nm_platform_link_get_name (ifindex), ifindex);
	return klass->link_set_address (platform, ifindex, address, length);
}

/**
 * nm_platform_link_get_address:
 * @ifindex: Interface index
 * @length: Pointer to a variable to store address length
 *
 * Saves interface hardware address to @address.
 */
gconstpointer
nm_platform_link_get_address (int ifindex, size_t *length)
{
	reset_error ();

	if (length)
		*length = 0;

	g_return_val_if_fail (ifindex > 0, NULL);
	g_return_val_if_fail (klass->link_get_address, NULL);

	return klass->link_get_address (platform, ifindex, length);
}

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
gboolean
nm_platform_link_supports_carrier_detect (int ifindex)
{
	g_return_val_if_fail (ifindex >= 0, FALSE);
	g_return_val_if_fail (klass->link_supports_carrier_detect, FALSE);

	return klass->link_supports_carrier_detect (platform, ifindex);
}

gboolean
nm_platform_link_supports_vlans (int ifindex)
{
	g_return_val_if_fail (ifindex >= 0, FALSE);
	g_return_val_if_fail (klass->link_supports_vlans, FALSE);

	return klass->link_supports_vlans (platform, ifindex);
}

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
/**
 * nm_platform_link_set_up:
 * @ifindex: Interface index
 *
 * Bring the interface up.
 */
gboolean
nm_platform_link_set_up (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (klass->link_set_up, FALSE);

	debug ("link: setting up '%s' (%d)", nm_platform_link_get_name (ifindex), ifindex);
	return klass->link_set_up (platform, ifindex);
}

/**
 * nm_platform_link_set_down:
 * @ifindex: Interface index
 *
 * Take the interface down.
 */
gboolean
nm_platform_link_set_down (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (klass->link_set_down, FALSE);

	debug ("link: setting down '%s' (%d)", nm_platform_link_get_name (ifindex), ifindex);
	return klass->link_set_down (platform, ifindex);
}

/**
 * nm_platform_link_set_arp:
 * @ifindex: Interface index
 *
 * Enable ARP on the interface.
 */
gboolean
nm_platform_link_set_arp (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex >= 0, FALSE);
	g_return_val_if_fail (klass->link_set_arp, FALSE);

	debug ("link: setting arp '%s' (%d)", nm_platform_link_get_name (ifindex), ifindex);
	return klass->link_set_arp (platform, ifindex);
}

/**
 * nm_platform_link_set_noarp:
 * @ifindex: Interface index
 *
 * Disable ARP on the interface.
 */
gboolean
nm_platform_link_set_noarp (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex >= 0, FALSE);
	g_return_val_if_fail (klass->link_set_noarp, FALSE);

	debug ("link: setting noarp '%s' (%d)", nm_platform_link_get_name (ifindex), ifindex);
	return klass->link_set_noarp (platform, ifindex);
}

Pavel Šimerda's avatar
Pavel Šimerda committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
/**
 * nm_platform_link_set_mtu:
 * @ifindex: Interface index
 * @mtu: The new MTU value
 *
 * Set interface MTU.
 */
gboolean
nm_platform_link_set_mtu (int ifindex, guint32 mtu)
{
	reset_error ();

	g_return_val_if_fail (ifindex >= 0, FALSE);
	g_return_val_if_fail (mtu > 0, FALSE);
	g_return_val_if_fail (klass->link_set_mtu, FALSE);

	debug ("link: setting '%s' (%d) mtu %d", nm_platform_link_get_name (ifindex), ifindex, mtu);
	return klass->link_set_mtu (platform, ifindex, mtu);
}

/**
 * nm_platform_link_get_mtu:
 * @ifindex: Interface index
 *
 * Returns: MTU value for the interface or 0 on error.
 */
guint32
nm_platform_link_get_mtu (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex >= 0, 0);
	g_return_val_if_fail (klass->link_get_mtu, 0);

	return klass->link_get_mtu (platform, ifindex);
}

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
/**
 * nm_platform_link_get_mtu:
 * @ifindex: Interface index
 *
 * Returns: physical port ID for the interface, or %NULL on error
 * or if the interface has no physical port ID.
 */
char *
nm_platform_link_get_physical_port_id (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex >= 0, NULL);
	g_return_val_if_fail (klass->link_get_physical_port_id, NULL);

	return klass->link_get_physical_port_id (platform, ifindex);
}

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
/**
 * nm_platform_link_enslave:
 * @master: Interface index of the master
 * @slave: Interface index of the slave
 *
 * Enslave @slave to @master.
 */
gboolean
nm_platform_link_enslave (int master, int slave)
{
	reset_error ();

	g_assert (platform);
	g_return_val_if_fail (master > 0, FALSE);
	g_return_val_if_fail (slave> 0, FALSE);
	g_return_val_if_fail (klass->link_enslave, FALSE);

	debug ("link: enslaving '%s' (%d) to master '%s' (%d)",
		nm_platform_link_get_name (slave), slave,
		nm_platform_link_get_name (master), master);
	return klass->link_enslave (platform, master, slave);
}

/**
 * nm_platform_link_release:
 * @master: Interface index of the master
 * @slave: Interface index of the slave
 *
 * Release @slave from @master.
 */
gboolean
nm_platform_link_release (int master, int slave)
{
	reset_error ();

	g_assert (platform);
	g_return_val_if_fail (master > 0, FALSE);
	g_return_val_if_fail (slave > 0, FALSE);
	g_return_val_if_fail (klass->link_release, FALSE);

	if (nm_platform_link_get_master (slave) != master) {
		platform->error = NM_PLATFORM_ERROR_NOT_SLAVE;
		return FALSE;
	}

	debug ("link: releasing '%s' (%d) from master '%s' (%d)",
		nm_platform_link_get_name (slave), slave,
		nm_platform_link_get_name (master), master);
	return klass->link_release (platform, master, slave);
}

/**
 * nm_platform_link_get_master:
 * @slave: Interface index of the slave.
 *
 * Returns: Interfase index of the slave's master.
 */
int
nm_platform_link_get_master (int slave)
{
	reset_error ();

	g_assert (platform);
	g_return_val_if_fail (slave >= 0, FALSE);
	g_return_val_if_fail (klass->link_get_master, FALSE);

	if (!nm_platform_link_get_name (slave)) {
		platform->error = NM_PLATFORM_ERROR_NOT_FOUND;
		return 0;
	}
	return klass->link_get_master (platform, slave);
}

/**
 * nm_platform_bridge_add:
 * @name: New interface name
 *
960
 * Create a software bridge.
961 962 963 964 965 966 967 968 969 970 971 972
 */
gboolean
nm_platform_bridge_add (const char *name)
{
	debug ("link: adding bridge '%s'", name);
	return nm_platform_link_add (name, NM_LINK_TYPE_BRIDGE);
}

/**
 * nm_platform_bond_add:
 * @name: New interface name
 *
973
 * Create a software bonding device.
974 975 976 977 978 979 980 981 982 983 984 985
 */
gboolean
nm_platform_bond_add (const char *name)
{
	debug ("link: adding bond '%s'", name);
	return nm_platform_link_add (name, NM_LINK_TYPE_BOND);
}

/**
 * nm_platform_team_add:
 * @name: New interface name
 *
986
 * Create a software teaming device.
987 988 989 990 991 992 993 994
 */
gboolean
nm_platform_team_add (const char *name)
{
	debug ("link: adding team '%s'", name);
	return nm_platform_link_add (name, NM_LINK_TYPE_TEAM);
}

Pavel Šimerda's avatar
Pavel Šimerda committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
/**
 * nm_platform_vlan_add:
 * @name: New interface name
 * @vlanid: VLAN identifier
 * @vlanflags: VLAN flags from libnm-util
 *
 * Create a software VLAN device.
 */
gboolean
nm_platform_vlan_add (const char *name, int parent, int vlanid, guint32 vlanflags)
{
	reset_error ();

	g_assert (platform);
	g_return_val_if_fail (parent >= 0, FALSE);
	g_return_val_if_fail (vlanid >= 0, FALSE);
	g_return_val_if_fail (name, FALSE);
	g_return_val_if_fail (klass->vlan_add, FALSE);

	if (nm_platform_link_exists (name)) {
		debug ("link already exists: %s", name);
		platform->error = NM_PLATFORM_ERROR_EXISTS;
		return FALSE;
	}

	debug ("link: adding vlan '%s' parent %d vlanid %d vlanflags %x",
		name, parent, vlanid, vlanflags);
	return klass->vlan_add (platform, name, parent, vlanid, vlanflags);
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
gboolean
nm_platform_master_set_option (int ifindex, const char *option, const char *value)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (option, FALSE);
	g_return_val_if_fail (value, FALSE);
	g_return_val_if_fail (klass->master_set_option, FALSE);

	return klass->master_set_option (platform, ifindex, option, value);
}

char *
nm_platform_master_get_option (int ifindex, const char *option)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (option, FALSE);
	g_return_val_if_fail (klass->master_set_option, FALSE);

	return klass->master_get_option (platform, ifindex, option);
}

gboolean
nm_platform_slave_set_option (int ifindex, const char *option, const char *value)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (option, FALSE);
	g_return_val_if_fail (value, FALSE);
	g_return_val_if_fail (klass->slave_set_option, FALSE);

	return klass->slave_set_option (platform, ifindex, option, value);
}

char *
nm_platform_slave_get_option (int ifindex, const char *option)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (option, FALSE);
	g_return_val_if_fail (klass->slave_set_option, FALSE);

	return klass->slave_get_option (platform, ifindex, option);
}

Pavel Šimerda's avatar
Pavel Šimerda committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
gboolean
nm_platform_vlan_get_info (int ifindex, int *parent, int *vlanid)
{
	reset_error ();

	g_assert (platform);
	g_return_val_if_fail (klass->vlan_get_info, FALSE);

	if (parent)
		*parent = 0;
	if (vlanid)
		*vlanid = 0;

	if (nm_platform_link_get_type (ifindex) != NM_LINK_TYPE_VLAN)
		return FALSE;

	return klass->vlan_get_info (platform, ifindex, parent, vlanid);
}

gboolean
nm_platform_vlan_set_ingress_map (int ifindex, int from, int to)
{
	reset_error ();

	g_assert (platform);
	g_return_val_if_fail (klass->vlan_set_ingress_map, FALSE);

	debug ("link: setting vlan ingress map for %d from %d to %d", ifindex, from, to);
	return klass->vlan_set_ingress_map (platform, ifindex, from, to);
}

gboolean
nm_platform_vlan_set_egress_map (int ifindex, int from, int to)
{
	reset_error ();

	g_assert (platform);
	g_return_val_if_fail (klass->vlan_set_egress_map, FALSE);

	debug ("link: setting vlan egress map for %d from %d to %d", ifindex, from, to);
	return klass->vlan_set_egress_map (platform, ifindex, from, to);
}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
gboolean
nm_platform_infiniband_partition_add (int parent, int p_key)
{
	const char *parent_name;
	char *name;

	reset_error ();

	g_return_val_if_fail (parent >= 0, FALSE);
	g_return_val_if_fail (p_key >= 0, FALSE);
	g_return_val_if_fail (klass->infiniband_partition_add, FALSE);

	if (nm_platform_link_get_type (parent) != NM_LINK_TYPE_INFINIBAND) {
		platform->error = NM_PLATFORM_ERROR_WRONG_TYPE;
		return FALSE;
	}

	parent_name = nm_platform_link_get_name (parent);
	name = g_strdup_printf ("%s.%04x", parent_name, p_key);
	if (nm_platform_link_exists (name)) {
		debug ("infiniband: already exists");
		platform->error = NM_PLATFORM_ERROR_EXISTS;
		g_free (name);
		return FALSE;
	}
	g_free (name);

	return klass->infiniband_partition_add (platform, parent, p_key);
}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
gboolean
nm_platform_veth_get_properties (int ifindex, NMPlatformVethProperties *props)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (props != NULL, FALSE);

	return klass->veth_get_properties (platform, ifindex, props);
}

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
gboolean
nm_platform_tun_get_properties (int ifindex, NMPlatformTunProperties *props)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (props != NULL, FALSE);

	return klass->tun_get_properties (platform, ifindex, props);
}

1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
gboolean
nm_platform_macvlan_get_properties (int ifindex, NMPlatformMacvlanProperties *props)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (props != NULL, FALSE);

	return klass->macvlan_get_properties (platform, ifindex, props);
}

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
gboolean
nm_platform_vxlan_get_properties (int ifindex, NMPlatformVxlanProperties *props)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (props != NULL, FALSE);

	return klass->vxlan_get_properties (platform, ifindex, props);
}

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
gboolean
nm_platform_gre_get_properties (int ifindex, NMPlatformGreProperties *props)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (props != NULL, FALSE);

	return klass->gre_get_properties (platform, ifindex, props);
}

Pavel Šimerda's avatar
Pavel Šimerda committed
1203 1204
/******************************************************************/

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
GArray *
nm_platform_ip4_address_get_all (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, NULL);
	g_return_val_if_fail (klass->ip4_address_get_all, NULL);

	return klass->ip4_address_get_all (platform, ifindex);
}

GArray *
nm_platform_ip6_address_get_all (int ifindex)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, NULL);
	g_return_val_if_fail (klass->ip6_address_get_all, NULL);

	return klass->ip6_address_get_all (platform, ifindex);
}

gboolean
1228 1229 1230 1231 1232 1233
nm_platform_ip4_address_add (int ifindex,
                             in_addr_t address,
                             in_addr_t peer_address,
                             int plen,
                             guint32 lifetime,
                             guint32 preferred)
1234 1235 1236 1237 1238
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (plen > 0, FALSE);
1239
	g_return_val_if_fail (lifetime > 0, FALSE);
1240 1241
	g_return_val_if_fail (klass->ip4_address_add, FALSE);

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	if (nm_logging_enabled (LOGL_DEBUG, LOGD_PLATFORM)) {
		NMPlatformIP4Address addr = { 0 };

		addr.ifindex = ifindex;
		addr.address = address;
		addr.peer_address = peer_address;
		addr.plen = plen;
		addr.lifetime = lifetime;
		addr.preferred = preferred;

		debug ("address: adding or updating IPv4 address: %s", nm_platform_ip4_address_to_string (&addr));
	}
1254
	return klass->ip4_address_add (platform, ifindex, address, peer_address, plen, lifetime, preferred);
1255 1256 1257
}

gboolean
1258 1259 1260 1261 1262 1263 1264
nm_platform_ip6_address_add (int ifindex,
                             struct in6_addr address,
                             struct in6_addr peer_address,
                             int plen,
                             guint32 lifetime,
                             guint32 preferred,
                             guint flags)
1265 1266 1267 1268 1269
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (plen > 0, FALSE);
1270
	g_return_val_if_fail (lifetime > 0, FALSE);
1271 1272
	g_return_val_if_fail (klass->ip6_address_add, FALSE);

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	if (nm_logging_enabled (LOGL_DEBUG, LOGD_PLATFORM)) {
		NMPlatformIP6Address addr = { 0 };

		addr.ifindex = ifindex;
		addr.address = address;
		addr.peer_address = peer_address;
		addr.plen = plen;
		addr.lifetime = lifetime;
		addr.preferred = preferred;
		addr.flags = flags;

		debug ("address: adding or updating IPv6 address: %s", nm_platform_ip6_address_to_string (&addr));
	}
1286
	return klass->ip6_address_add (platform, ifindex, address, peer_address, plen, lifetime, preferred, flags);
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
}

gboolean
nm_platform_ip4_address_delete (int ifindex, in_addr_t address, int plen)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (plen > 0, FALSE);
	g_return_val_if_fail (klass->ip4_address_delete, FALSE);

1298
	debug ("address: deleting IPv4 address %s/%d", nm_utils_inet4_ntop (address, NULL), plen);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
	return klass->ip4_address_delete (platform, ifindex, address, plen);
}

gboolean
nm_platform_ip6_address_delete (int ifindex, struct in6_addr address, int plen)
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, FALSE);
	g_return_val_if_fail (plen > 0, FALSE);
	g_return_val_if_fail (klass->ip6_address_delete, FALSE);

1311
	debug ("address: deleting IPv6 address %s/%d", nm_utils_inet6_ntop (&address, NULL), plen);
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	return klass->ip6_address_delete (platform, ifindex, address, plen);
}

gboolean
nm_platform_ip4_address_exists (int ifindex, in_addr_t address, int plen)
{
	reset_error ();

	g_return_val_if_fail (plen > 0, FALSE);
	g_return_val_if_fail (klass->ip4_address_exists, FALSE);

	return klass->ip4_address_exists (platform, ifindex, address, plen);
}

gboolean
nm_platform_ip6_address_exists (int ifindex, struct in6_addr address, int plen)
{
	reset_error ();

	g_return_val_if_fail (plen > 0, FALSE);
	g_return_val_if_fail (klass->ip6_address_exists, FALSE);

	return klass->ip6_address_exists (platform, ifindex, address, plen);
}

1337 1338 1339
static gboolean
array_contains_ip4_address (const GArray *addresses, const NMPlatformIP4Address *address)
{
1340 1341
	guint len = addresses ? addresses->len : 0;
	guint i;
1342

1343
	for (i = 0; i < len; i++) {
1344 1345
		NMPlatformIP4Address *candidate = &g_array_index (addresses, NMPlatformIP4Address, i);

1346
		if (candidate->address == address->address && candidate->plen == address->plen)
1347 1348 1349 1350 1351 1352 1353 1354 1355
			return TRUE;
	}

	return FALSE;
}

static gboolean
array_contains_ip6_address (const GArray *addresses, const NMPlatformIP6Address *address)
{
1356 1357
	guint len = addresses ? addresses->len : 0;
	guint i;
1358

1359
	for (i = 0; i < len; i++) {
1360 1361
		NMPlatformIP6Address *candidate = &g_array_index (addresses, NMPlatformIP6Address, i);

1362
		if (IN6_ARE_ADDR_EQUAL (&candidate->address, &address->address) && candidate->plen == address->plen)
1363 1364 1365 1366 1367 1368
			return TRUE;
	}

	return FALSE;
}

1369
/* Compute (a - b) in an overflow-safe manner. */
1370
static guint32
1371
subtract_guint32 (guint32 a, guint32 b)
1372
{
1373 1374
	if (a == G_MAXUINT32)
		return G_MAXUINT32;
1375

1376
	return a > b ? a - b : 0;
1377 1378
}

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
/**
 * nm_platform_ip4_address_sync:
 * @ifindex: Interface index
 * @known_addresses: List of addresses
 *
 * A convenience function to synchronize addresses for a specific interface
 * with the least possible disturbance. It simply removes addresses that are
 * not listed and adds addresses that are.
 *
 * Returns: %TRUE on success.
 */
gboolean
nm_platform_ip4_address_sync (int ifindex, const GArray *known_addresses)
{
	GArray *addresses;
	NMPlatformIP4Address *address;
1395
	guint32 now = nm_utils_get_monotonic_timestamp_s ();
1396 1397 1398 1399 1400 1401 1402
	int i;

	/* Delete unknown addresses */
	addresses = nm_platform_ip4_address_get_all (ifindex);
	for (i = 0; i < addresses->len; i++) {
		address = &g_array_index (addresses, NMPlatformIP4Address, i);

1403
		if (!array_contains_ip4_address (known_addresses, address))
1404 1405 1406 1407 1408 1409 1410 1411 1412
			nm_platform_ip4_address_delete (ifindex, address->address, address->plen);
	}
	g_array_free (addresses, TRUE);

	if (!known_addresses)
		return TRUE;

	/* Add missing addresses */
	for (i = 0; i < known_addresses->len; i++) {
1413 1414
		const NMPlatformIP4Address *known_address = &g_array_index (known_addresses, NMPlatformIP4Address, i);
		guint32 lifetime, preferred;
1415

1416
		if (known_address->lifetime) {
1417 1418
			/* Pad the timestamp by 5 seconds to avoid potential races. */
			guint32 shift = subtract_guint32 (now, known_address->timestamp + 5);
1419

1420 1421
			lifetime = subtract_guint32 (known_address->lifetime, shift);
			preferred = subtract_guint32 (known_address->lifetime, shift);
1422 1423
		} else
			lifetime = preferred = NM_PLATFORM_LIFETIME_PERMANENT;
1424

1425
		if (!nm_platform_ip4_address_add (ifindex, known_address->address, known_address->peer_address, known_address->plen, lifetime, preferred))
1426
			return FALSE;
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
	}

	return TRUE;
}

/**
 * nm_platform_ip6_address_sync:
 * @ifindex: Interface index
 * @known_addresses: List of addresses
 *
 * A convenience function to synchronize addresses for a specific interface
 * with the least possible disturbance. It simply removes addresses that are
 * not listed and adds addresses that are.
 *
 * Returns: %TRUE on success.
 */
gboolean
nm_platform_ip6_address_sync (int ifindex, const GArray *known_addresses)
{
	GArray *addresses;
	NMPlatformIP6Address *address;
1448
	guint32 now = nm_utils_get_monotonic_timestamp_s ();
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
	int i;

	/* Delete unknown addresses */
	addresses = nm_platform_ip6_address_get_all (ifindex);
	for (i = 0; i < addresses->len; i++) {
		address = &g_array_index (addresses, NMPlatformIP6Address, i);

		/* Leave link local address management to the kernel */
		if (IN6_IS_ADDR_LINKLOCAL (&address->address))
			continue;

1460
		if (!array_contains_ip6_address (known_addresses, address))
1461 1462 1463 1464 1465 1466 1467 1468 1469
			nm_platform_ip6_address_delete (ifindex, address->address, address->plen);
	}
	g_array_free (addresses, TRUE);

	if (!known_addresses)
		return TRUE;

	/* Add missing addresses */
	for (i = 0; i < known_addresses->len; i++) {
1470 1471
		const NMPlatformIP6Address *known_address = &g_array_index (known_addresses, NMPlatformIP6Address, i);
		guint32 lifetime, preferred;
1472

1473
		if (known_address->lifetime) {
1474 1475
			/* Pad the timestamp by 5 seconds to avoid potential races. */
			guint32 shift = subtract_guint32 (now, known_address->timestamp + 5);
1476

1477 1478
			lifetime = subtract_guint32 (known_address->lifetime, shift);
			preferred = subtract_guint32 (known_address->lifetime, shift);
1479 1480
		} else
			lifetime = preferred = NM_PLATFORM_LIFETIME_PERMANENT;
1481

1482 1483
		if (!nm_platform_ip6_address_add (ifindex, known_address->address,
		                                  known_address->peer_address, known_address->plen,
1484
		                                  lifetime, preferred, known_address->flags))
1485
			return FALSE;
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	}

	return TRUE;
}

gboolean
nm_platform_address_flush (int ifindex)
{
	return nm_platform_ip4_address_sync (ifindex, NULL)
			&& nm_platform_ip6_address_sync (ifindex, NULL);
}

/******************************************************************/

Pavel Šimerda's avatar
Pavel Šimerda committed
1500
GArray *
1501
nm_platform_ip4_route_get_all (int ifindex, gboolean include_default)
Pavel Šimerda's avatar
Pavel Šimerda committed
1502 1503 1504 1505 1506 1507
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, NULL);
	g_return_val_if_fail (klass->ip4_route_get_all, NULL);

1508
	return klass->ip4_route_get_all (platform, ifindex, include_default);
Pavel Šimerda's avatar
Pavel Šimerda committed
1509 1510 1511
}

GArray *
1512
nm_platform_ip6_route_get_all (int ifindex, gboolean include_default)
Pavel Šimerda's avatar
Pavel Šimerda committed
1513 1514 1515 1516 1517 1518
{
	reset_error ();

	g_return_val_if_fail (ifindex > 0, NULL);
	g_return_val_if_fail (klass->ip6_route_get_all, NULL);

1519
	return klass->ip6_route_get_all (platform, ifindex, include_default);
Pavel Šimerda's avatar
Pavel Šimerda committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
}

gboolean
nm_platform_ip4_route_add (int ifindex,
		in_addr_t network, int plen,
		in_addr_t gateway, int metric, int mss)
{
	reset_error ();

	g_return_val_if_fail (platform, FALSE);
	g_return_val_if_fail (0 <= plen && plen <= 32, FALSE);
	g_return_val_if_fail (metric >= 0, FALSE);
	g_return_val_if_fail (mss >= 0, FALSE);
	g_return_val_if_fail (klass->ip4_route_add, FALSE);

1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	if (nm_logging_enabled (LOGL_DEBUG, LOGD_PLATFORM)) {
		NMPlatformIP4Route route = { 0 };

		route.ifindex = ifindex;
		route.network = network;
		route.plen = plen;
		route.gateway = gateway;
		route.metric = metric;
		route.mss = mss;

		debug ("route: adding or updating IPv4 route: %s", nm_platform_ip4_route_to_string (&route));
	}
Pavel Šimerda's avatar
Pavel Šimerda committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	return klass->ip4_route_add (platform, ifindex, network, plen, gateway, metric, mss);
}

gboolean
nm_platform_ip6_route_add (int ifindex,
		struct in6_addr network, int plen, struct in6_addr gateway, int metric, int mss)
{
	g_return_val_if_fail (platform, FALSE);
	g_return_val_if_fail (0 <= plen && plen <= 128, FALSE);
	g_return_val_if_fail (metric >= 0, FALSE);
	g_return_val_if_fail (mss >= 0, FALSE);
	g_return_val_if_fail (klass->ip6_route_add, FALSE);

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
	if (nm_logging_enabled (LOGL_DEBUG, LOGD_PLATFORM)) {
		NMPlatformIP6Route route = { 0 };

		route.ifindex = ifindex;
		route.network = network;
		route.plen = plen;
		route.gateway = gateway;
		route.metric = metric;
		route.mss = mss;

		debug ("route: adding or updating IPv6 route: %s", nm_platform_ip6_route_to_string (&route));
	}
Pavel Šimerda's avatar
Pavel Šimerda committed
1572 1573 1574 1575
	return klass->ip6_route_add (platform, ifindex, network, plen, gateway, metric, mss);
}

gboolean
1576
nm_platform_ip4_route_delete (int ifindex, in_addr_t network, int plen, int metric)
Pavel Šimerda's avatar
Pavel Šimerda committed
1577 1578 1579 1580 1581 1582
{
	reset_error ();

	g_return_val_if_fail (platform, FALSE);
	g_return_val_if_fail (klass->ip4_route_delete, FALSE);

1583
	debug ("route: deleting IPv4 route %s/%d, metric=%d", nm_utils_inet4_ntop (network, NULL), plen, metric);
1584
	return klass->ip4_route_delete (platform, ifindex, network, plen, metric);
Pavel Šimerda's avatar
Pavel Šimerda committed
1585 1586 1587 1588
}

gboolean
nm_platform_ip6_route_delete (int ifindex,
1589
		struct in6_addr network, int plen, int metric)
Pavel Šimerda's avatar
Pavel Šimerda committed
1590 1591 1592 1593 1594 1595
{
	reset_error ();

	g_return_val_if_fail (platform, FALSE);
	g_return_val_if_fail (klass->ip6_route_delete, FALSE);

1596
	debug ("route: deleting IPv6 route %s/%d, metric=%d", nm_utils_inet6_ntop (&network, NULL), plen, metric);
1597
	return klass->ip6_route_delete (platform, ifindex, network, plen, metric);
Pavel Šimerda's avatar
Pavel Šimerda committed
1598 1599 1600
}

gboolean
1601
nm_platform_ip4_route_exists (int ifindex, in_addr_t network, int plen, int metric)
Pavel Šimerda's avatar
Pavel Šimerda committed
1602 1603 1604 1605 1606 1607
{
	reset_error ();

	g_return_val_if_fail (platform, FALSE);
	g_return_val_if_fail (klass->ip4_route_exists, FALSE);

1608
	return klass->ip4_route_exists (platform,ifindex, network, plen, metric);
Pavel Šimerda's avatar
Pavel Šimerda committed
1609 1610 1611
}

gboolean
1612
nm_platform_ip6_route_exists (int ifindex, struct in6_addr network, int plen, int metric)
Pavel Šimerda's avatar
Pavel Šimerda committed
1613 1614 1615 1616 1617 1618
{
	reset_error ();

	g_return_val_if_fail (platform, FALSE);
	g_return_val_if_fail (klass->ip6_route_exists, FALSE);

1619
	return klass->ip6_route_exists (platform, ifindex, network, plen, metric);
Pavel Šimerda's avatar
Pavel Šimerda committed
1620 1621
}

1622 1623 1624
static gboolean
array_contains_ip4_route (const GArray *routes, const NMPlatformIP4Route *route)
{
1625 1626
	guint len = routes ? routes->len : 0;
	guint i;
1627

1628 1629 1630 1631 1632 1633 1634
	for (i = 0; i < len; i++) {
		NMPlatformIP4Route *c = &g_array_index (routes, NMPlatformIP4Route, i);

		if (route->network == c->network &&
		    route->plen == c->plen &&
		    route->gateway == c->gateway &&
		    route->metric == c->metric)
1635
			return TRUE;
1636
	}
1637 1638 1639 1640 1641 1642 1643

	return FALSE;
}

static gboolean
array_contains_ip6_route (const GArray *routes, const NMPlatformIP6Route *route)
{
1644 1645
	guint len = routes ? routes->len : 0;
	guint i;
1646

1647 1648 1649 1650 1651 1652 1653
	for (i = 0; i < len; i++) {
		NMPlatformIP6Route *c = &g_array_index (routes, NMPlatformIP6Route, i);

		if (IN6_ARE_ADDR_EQUAL (&route->network, &c->network) &&
		    route->plen == c->plen &&
		    IN6_ARE_ADDR_EQUAL (&route->gateway, &c->gateway) &&
		    route->metric == c->metric)
1654
			return TRUE;
1655
	}
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676

	return FALSE;
}

/**
 * nm_platform_ip4_route_sync:
 * @ifindex: Interface index
 * @known_routes: List of routes
 *
 * A convenience function to synchronize routes for a specific interface
 * with the least possible disturbance. It simply removes routes that are
 * not listed and adds routes that are.
 *
 * Returns: %TRUE on success.
 */
gboolean
nm_platform_ip4_route_sync (int ifindex, const GArray *known_routes)
{
	GArray *routes;
	NMPlatformIP4Route *route;
	const NMPlatformIP4Route *known_route;
1677
	gboolean success;
1678
	int i, i_type;
1679 1680

	/* Delete unknown routes */
1681
	routes = nm_platform_ip4_route_get_all (ifindex, FALSE);
1682 1683 1684
	for (i = 0; i < routes->len; i++) {
		route = &g_array_index (routes, NMPlatformIP4Route, i);

1685
		if (!array_contains_ip4_route (known_routes, route))
1686 1687 1688
			nm_platform_ip4_route_delete (ifindex, route->network, route->plen, route->metric);
	}

1689 1690
	if (!known_routes) {
		g_array_free (routes, TRUE);
1691
		return TRUE;
1692
	}
1693 1694

	/* Add missing routes */
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
	for (i_type = 0, success = TRUE; i_type < 2 && success; i_type++) {
		for (i = 0; i < known_routes->len && success; i++) {
			known_route = &g_array_index (known_routes, NMPlatformIP4Route, i);

			if ((known_route->gateway == 0) ^ (i_type != 0)) {
				/* Make two runs over the list of routes. On the first, only add
				 * device routes, on the second the others (gateway routes). */
				continue;
			}

			/* Ignore routes that already exist */
			if (!array_contains_ip4_route (routes, known_route)) {
				success = nm_platform_ip4_route_add (ifindex,
				                                     known_route->network,
				                                     known_route->plen,
				                                     known_route->gateway,
				                                     known_route->metric,
				                                     known_route->mss);
				if (!success && known_route->source < NM_PLATFORM_SOURCE_USER) {
					nm_log_dbg (LOGD_PLATFORM, "ignore error adding IPv4 route to kernel: %s",
					                           nm_platform_ip4_route_to_string (known_route));
					success = TRUE;
				}
1718
			}
1719
		}
1720 1721
	}

1722 1723
	g_array_free (routes, TRUE);
	return success;
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
}

/**
 * nm_platform_ip6_route_sync:
 * @ifindex: Interface index
 * @known_routes: List of routes
 *
 * A convenience function to synchronize routes for a specific interface
 * with the least possible disturbance. It simply removes routes that are
 * not listed and adds routes that are.
 *
 * Returns: %TRUE on success.
 */
gboolean
nm_platform_ip6_route_sync (int ifindex, const GArray *known_routes)
{
	GArray *routes;
	NMPlatformIP6Route *route;
	const NMPlatformIP6Route *known_route;
1743
	gboolean success;
1744
	int i, i_type;
1745 1746

	/* Delete unknown routes */
1747
	routes = nm_platform_ip6_route_get_all (ifindex, FALSE);
1748 1749 1750 1751
	for (i = 0; i < routes->len; i++) {
		route = &g_array_index (routes, NMPlatformIP6Route, i);
		route->ifindex = 0;

1752
		if (!array_contains_ip6_route (known_routes, route))
1753 1754 1755
			nm_platform_ip6_route_delete (ifindex, route->network, route->plen, route->metric);
	}

1756 1757
	if (!known_routes) {
		g_array_free (routes, TRUE);
1758
		return TRUE;
1759
	}
1760 1761

	/* Add missing routes */
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
	for (i_type = 0, success = TRUE; i_type < 2 && success; i_type++) {
		for (i = 0; i < known_routes->len && success; i++) {
			known_route = &g_array_index (known_routes, NMPlatformIP6Route, i);

			if (IN6_IS_ADDR_UNSPECIFIED (&known_route->gateway) ^ (i_type != 0)) {
				/* Make two runs over the list of routes. On the first, only add
				 * device routes, on the second the others (gateway routes). */
				continue;
			}

			/* Ignore routes that already exist */
			if (!array_contains_ip6_route (routes, known_route)) {
				success = nm_platform_ip6_route_add (ifindex,
				                                     known_route->network,
				                                     known_route->plen,
				                                     known_route->gateway,
				                                     known_route->metric,
				                                     known_route->mss);
				if (!success && known_route->source < NM_PLATFORM_SOURCE_USER) {
					nm_log_dbg (LOGD_PLATFORM, "ignore error adding IPv6 route to kernel: %s",
					                           nm_platform_ip6_route_to_string (known_route));
					success = TRUE;
				}
1785
			}
1786
		}
1787 1788
	}

1789 1790
	g_array_free (routes, TRUE);
	return success;
1791 1792 1793 1794 1795 1796 1797 1798 1799
}

gboolean
nm_platform_route_flush (int ifindex)
{
	return nm_platform_ip4_route_sync (ifindex, NULL)
			&& nm_platform_ip6_route_sync (ifindex, NULL);
}

1800 1801
/******************************************************************/

1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
static const char *
source_to_string (NMPlatformSource source)
{
	switch (source) {
	case NM_PLATFORM_SOURCE_KERNEL:
		return "kernel";
	case NM_PLATFORM_SOURCE_SHARED:
		return "shared";
	case NM_PLATFORM_SOURCE_IP4LL:
		return "ipv4ll";
	case NM_PLATFORM_SOURCE_PPP:
		return "ppp";
	case NM_PLATFORM_SOURCE_WWAN:
		return "wwan";
	case NM_PLATFORM_SOURCE_VPN:
		return "vpn";
	case NM_PLATFORM_SOURCE_DHCP:
		return "dhcp";
	case NM_PLATFORM_SOURCE_RDISC:
		return "rdisc";
	case NM_PLATFORM_SOURCE_USER:
		return "user";
	default:
		break;
	}
	return "unknown";
}

1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
#define TO_STRING_DEV_BUF_SIZE (5+15+1)
static void
_to_string_dev (int ifindex, char *buf, size_t size)
{
	g_assert (buf && size >= TO_STRING_DEV_BUF_SIZE);

	if (ifindex){
		const char *name = ifindex > 0 ? nm_platform_link_get_name (ifindex) : NULL;

		strcpy (buf, " dev ");
		buf += 5;
		size -= 5;

		if (name)
			g_strlcpy (buf, name, size);
		else
			g_snprintf (buf, size, "%d", ifindex);
	} else
		buf[0] = 0;
}

1851 1852
static char to_string_buffer[256];

1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
/**
 * nm_platform_ip4_address_to_string:
 * @route: pointer to NMPlatformIP4Address address structure
 *
 * A method for converting an address struct into a string representation.
 *
 * Example output: ""
 *
 * Returns: a string representation of the address. The returned string
 * is an internal buffer, so do not keep or free the returned string.
 * Also, this function is not thread safe.
 */
const char *
nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address)
{
	char s_address[INET_ADDRSTRLEN];
1869
	char s_peer[INET_ADDRSTRLEN];
1870
	char str_dev[TO_STRING_DEV_BUF_SIZE];
1871
	char *str_peer = NULL;
1872 1873 1874 1875

	g_return_val_if_fail (address, "(unknown)");

	inet_ntop (AF_INET, &address->address, s_address, sizeof (s_address));
1876

1877 1878 1879 1880 1881
	if (address->peer_address) {
		inet_ntop (AF_INET, &address->peer_address, s_peer, sizeof (s_peer));
		str_peer = g_strconcat (" ptp ", s_peer, NULL);
	}

1882
	_to_string_dev (address->ifindex, str_dev, sizeof (str_dev));
1883

1884
	g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d lft %u pref %u time %u%s%s src %s",
1885
	            s_address, address->plen, (guint)address->lifetime, (guint)address->preferred,
1886
	            (guint)address->timestamp,
1887
	            str_peer ? str_peer : "",
1888
	            str_dev,
1889
	            source_to_string (address->source));
1890
	g_free (str_peer);
1891
	return to_string_buffer;
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
}

/**
 * nm_platform_ip6_address_to_string:
 * @route: pointer to NMPlatformIP6Address address structure
 *
 * A method for converting an address struct into a string representation.
 *
 * Example output: "2001:db8:0:f101::1/64 lft 4294967295 pref 4294967295 time 16922666 on dev em1"
 *
 * Returns: a string representation of the address. The returned string
 * is an internal buffer, so do not keep or free the returned string.
 * Also, this function is not thread safe.
 */
const char *
nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address)
{
1909
	char s_flags[256];
1910
	char s_address[INET6_ADDRSTRLEN];
1911
	char s_peer[INET6_ADDRSTRLEN];
1912
	char *str_flags;
1913
	char str_dev[TO_STRING_DEV_BUF_SIZE];
1914
	char *str_peer = NULL;
1915 1916 1917 1918

	g_return_val_if_fail (address, "(unknown)");

	inet_ntop (AF_INET6, &address->address, s_address, sizeof (s_address));
1919

1920 1921 1922 1923 1924
	if (!IN6_IS_ADDR_UNSPECIFIED (&address->peer_address)) {
		inet_ntop (AF_INET6, &address->peer_address, s_peer, sizeof (s_peer));
		str_peer = g_strconcat (" ptp ", s_peer, NULL);
	}

1925
	_to_string_dev (address->ifindex, str_dev, sizeof (str_dev));
1926

1927 1928 1929 1930 1931 1932 1933
	rtnl_addr_flags2str(address->flags, s_flags, sizeof (s_flags));

	/* There are two recent flags IFA_F_MANAGETEMPADDR and IFA_F_NOPREFIXROUTE.
	 * If libnl does not yet support them, add them by hand.
	 * These two flags were introduced together with the extended ifa_flags,
	 * so, check for that.
	 **/
1934
	if ((address->flags & IFA_F_MANAGETEMPADDR) && !nm_platform_check_support_libnl_extended_ifa_flags ()) {
1935 1936 1937
		strncat (s_flags, s_flags[0] ? "," IFA_F_MANAGETEMPADDR_STR : IFA_F_MANAGETEMPADDR_STR,
		         sizeof (s_flags) - strlen (s_flags) - 1);
	}
1938
	if ((address->flags & IFA_F_NOPREFIXROUTE) && !nm_platform_check_support_libnl_extended_ifa_flags ()) {
1939 1940 1941 1942
		strncat (s_flags, s_flags[0] ? "," IFA_F_NOPREFIXROUTE_STR : IFA_F_NOPREFIXROUTE_STR,
		         sizeof (s_flags) - strlen (s_flags) - 1);
	}

1943 1944
	str_flags = s_flags[0] ? g_strconcat (" flags ", s_flags, NULL) : NULL;

1945
	g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d lft %u pref %u time %u%s%s%s src %s",
1946
	            s_address, address->plen, (guint)address->lifetime, (guint)address->preferred,
1947
	            (guint)address->timestamp,
1948
	            str_peer ? str_peer : "",