nm-device.c 562 KB
Newer Older
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 3 4 5 6 7 8 9 10 11 12 13
/* NetworkManager -- Network link manager
 *
 * 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 of the License, 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.
 *
14 15 16
 * 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.
17
 *
18
 * Copyright (C) 2005 - 2018 Red Hat, Inc.
19
 * Copyright (C) 2006 - 2008 Novell, Inc.
20 21
 */

22
#include "nm-default.h"
23

24 25
#include "nm-device.h"

26 27
#include <netinet/in.h>
#include <string.h>
Dan Williams's avatar
Dan Williams committed
28 29 30
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
31 32 33 34
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <arpa/inet.h>
35
#include <fcntl.h>
36
#include <linux/if_addr.h>
37
#include <linux/if_arp.h>
38
#include <linux/rtnetlink.h>
39
#include <linux/pkt_sched.h>
40

41
#include "nm-utils/nm-dedup-multi.h"
42 43
#include "nm-utils/nm-random-utils.h"
#include "nm-utils/unaligned.h"
44

45
#include "nm-ethtool-utils.h"
46
#include "nm-common-macros.h"
47 48
#include "nm-device-private.h"
#include "NetworkManagerUtils.h"
49
#include "nm-manager.h"
50
#include "platform/nm-platform.h"
51
#include "platform/nmp-object.h"
52 53 54
#include "ndisc/nm-ndisc.h"
#include "ndisc/nm-lndp-ndisc.h"
#include "dhcp/nm-dhcp-manager.h"
55
#include "dhcp/nm-dhcp-utils.h"
56
#include "nm-act-request.h"
57
#include "nm-proxy-config.h"
58 59
#include "nm-ip4-config.h"
#include "nm-ip6-config.h"
60
#include "nm-pacrunner-manager.h"
61
#include "dnsmasq/nm-dnsmasq-manager.h"
62
#include "nm-dhcp4-config.h"
63
#include "nm-dhcp6-config.h"
64
#include "nm-rfkill-manager.h"
65
#include "nm-firewall-manager.h"
66 67
#include "settings/nm-settings-connection.h"
#include "settings/nm-settings.h"
68
#include "nm-setting-ethtool.h"
69
#include "nm-auth-utils.h"
70
#include "nm-keep-alive.h"
71
#include "nm-netns.h"
72
#include "nm-dispatcher.h"
73
#include "nm-config.h"
74
#include "c-list/src/c-list.h"
75
#include "dns/nm-dns-manager.h"
76
#include "nm-acd-manager.h"
77
#include "nm-core-internal.h"
78
#include "systemd/nm-sd.h"
79
#include "nm-lldp-listener.h"
80
#include "nm-audit-manager.h"
81 82
#include "nm-connectivity.h"
#include "nm-dbus-interface.h"
83 84

#include "nm-device-generic.h"
85
#include "nm-device-vlan.h"
86
#include "nm-device-wireguard.h"
87

88 89 90
#include "nm-device-logging.h"
_LOG_DECLARE_SELF (NMDevice);

91
/*****************************************************************************/
92

93
#define DEFAULT_AUTOCONNECT    TRUE
94
#define DHCP_GRACE_PERIOD_SEC  480
95

96
#define CARRIER_WAIT_TIME_MS 6000
97 98
#define CARRIER_WAIT_TIME_AFTER_MTU_MS 10000

99 100 101 102
#define NM_DEVICE_AUTH_RETRIES_UNSET    -1
#define NM_DEVICE_AUTH_RETRIES_INFINITY -2
#define NM_DEVICE_AUTH_RETRIES_DEFAULT  3

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/*****************************************************************************/

typedef void (*ActivationHandleFunc) (NMDevice *self);

typedef struct {
	ActivationHandleFunc func;
	guint id;
} ActivationHandleData;

typedef enum {
	CLEANUP_TYPE_KEEP,
	CLEANUP_TYPE_REMOVED,
	CLEANUP_TYPE_DECONFIGURE,
} CleanupType;

typedef enum {
	IP_NONE = 0,
	IP_WAIT,
	IP_CONF,
	IP_DONE,
	IP_FAIL
} IpState;

typedef struct {
127
	CList lst_slave;
128 129 130 131 132 133 134 135 136 137 138 139
	NMDevice *slave;
	gulong watch_id;
	bool slave_is_enslaved;
	bool configure;
} SlaveInfo;

typedef struct {
	NMDevice *device;
	guint idle_add_id;
	int ifindex;
} DeleteOnDeactivateData;

140
typedef void (*AcdCallback) (NMDevice *, NMIP4Config **, gboolean);
141 142

typedef struct {
143
	AcdCallback callback;
144 145
	NMDevice *device;
	NMIP4Config **configs;
146
} AcdData;
147 148 149 150 151 152 153 154

typedef enum {
	HW_ADDR_TYPE_UNSET = 0,
	HW_ADDR_TYPE_PERMANENT,
	HW_ADDR_TYPE_EXPLICIT,
	HW_ADDR_TYPE_GENERATED,
} HwAddrType;

155 156 157 158 159 160 161
typedef enum {
	FIREWALL_STATE_UNMANAGED = 0,
	FIREWALL_STATE_INITIALIZED,
	FIREWALL_STATE_WAIT_STAGE_3,
	FIREWALL_STATE_WAIT_IP_CONFIG,
} FirewallState;

162 163 164 165 166 167
typedef struct {
	NMIPConfig *orig;      /* the original configuration applied to the device */
	NMIPConfig *current;   /* configuration after external changes.  NULL means
	                          that the original configuration didn't change. */
} AppliedConfig;

168 169 170 171 172 173 174
struct _NMDeviceConnectivityHandle {
	CList concheck_lst;
	NMDevice *self;
	NMDeviceConnectivityCallback callback;
	gpointer user_data;
	NMConnectivityCheckHandle *c_handle;
	guint64 seq;
175
	bool is_periodic:1;
176 177
	bool is_periodic_bump:1;
	bool is_periodic_bump_on_complete:1;
178
	int addr_family;
179 180
};

181 182 183 184 185 186
typedef struct {
	int ifindex;
	NMEthtoolFeatureStates *features;
	NMTernary requested[_NM_ETHTOOL_ID_FEATURE_NUM];
} EthtoolState;

187
/*****************************************************************************/
188

189
enum {
190
	STATE_CHANGED,
191
	AUTOCONNECT_ALLOWED,
192
	AUTH_REQUEST,
193 194
	IP4_CONFIG_CHANGED,
	IP6_CONFIG_CHANGED,
195 196
	IP6_PREFIX_DELEGATED,
	IP6_SUBNET_NEEDED,
197
	REMOVED,
198
	RECHECK_AUTO_ACTIVATE,
199
	RECHECK_ASSUME,
200 201 202 203
	LAST_SIGNAL,
};
static guint signals[LAST_SIGNAL] = { 0 };

204
NM_GOBJECT_PROPERTIES_DEFINE (NMDevice,
205 206 207 208
	PROP_UDI,
	PROP_IFACE,
	PROP_IP_IFACE,
	PROP_DRIVER,
209 210
	PROP_DRIVER_VERSION,
	PROP_FIRMWARE_VERSION,
211
	PROP_CAPABILITIES,
212
	PROP_CARRIER,
213
	PROP_MTU,
214 215 216 217 218 219
	PROP_IP4_ADDRESS,
	PROP_IP4_CONFIG,
	PROP_DHCP4_CONFIG,
	PROP_IP6_CONFIG,
	PROP_DHCP6_CONFIG,
	PROP_STATE,
220
	PROP_STATE_REASON,
221 222
	PROP_ACTIVE_CONNECTION,
	PROP_DEVICE_TYPE,
223
	PROP_LINK_TYPE,
224
	PROP_MANAGED,
225
	PROP_AUTOCONNECT,
226
	PROP_FIRMWARE_MISSING,
227
	PROP_NM_PLUGIN_MISSING,
228 229 230
	PROP_TYPE_DESC,
	PROP_RFKILL_TYPE,
	PROP_IFINDEX,
231
	PROP_AVAILABLE_CONNECTIONS,
232
	PROP_PHYSICAL_PORT_ID,
233
	PROP_MASTER,
234
	PROP_PARENT,
235
	PROP_HW_ADDRESS,
236
	PROP_PERM_HW_ADDRESS,
237
	PROP_HAS_PENDING_ACTION,
238
	PROP_METERED,
239
	PROP_LLDP_NEIGHBORS,
240
	PROP_REAL,
241
	PROP_SLAVES,
242 243 244
	PROP_REFRESH_RATE_MS,
	PROP_TX_BYTES,
	PROP_RX_BYTES,
245 246
	PROP_IP4_CONNECTIVITY,
	PROP_IP6_CONNECTIVITY,
247
);
248

249
typedef struct _NMDevicePrivate {
250
	bool in_state_changed;
251

252 253 254
	guint device_link_changed_id;
	guint device_ip_link_changed_id;

255
	NMDeviceState state;
256
	NMDeviceStateReason state_reason;
257
	struct {
258 259 260
		guint id;

		/* The @state/@reason is only valid, when @id is set. */
261 262 263
		NMDeviceState state;
		NMDeviceStateReason reason;
	} queued_state;
Thomas Haller's avatar
Thomas Haller committed
264

265 266
	union {
		struct {
267 268
			guint queued_ip_config_id_6;
			guint queued_ip_config_id_4;
269
		};
270
		guint queued_ip_config_id_x[2];
271 272
	};

273
	GSList *pending_actions;
274
	GSList *dad6_failed_addrs;
275

276
	NMDBusTrackObjPath parent_device;
277

278 279
	char *        udi;
	char *        iface;   /* may change, could be renamed by user */
280
	int           ifindex;
281

282 283
	int parent_ifindex;

284 285
	int auth_retries;

286 287 288 289
	union {
		const guint8 hw_addr_len; /* read-only */
		guint8 hw_addr_len_;
	};
290 291

	HwAddrType hw_addr_type:5;
292

293 294
	bool          real:1;

295 296 297
	bool update_ip_config_completed_v4:1;
	bool update_ip_config_completed_v6:1;

298
	char *        ip_iface;
299
	int           ip_ifindex;
300 301
	NMDeviceType  type;
	char *        type_desc;
302
	NMLinkType    link_type;
303
	NMDeviceCapabilities capabilities;
304
	char *        driver;
305 306
	char *        driver_version;
	char *        firmware_version;
307
	RfKillType    rfkill_type;
308 309 310
	bool          firmware_missing:1;
	bool          nm_plugin_missing:1;
	bool          hw_addr_perm_fake:1; /* whether the permanent HW address could not be read and is a fake */
311 312 313

	NMUtilsStableType current_stable_id_type:3;

314
	bool          nm_owned:1; /* whether the device is a device owned and created by NM */
Thomas Haller's avatar
Thomas Haller committed
315

316 317 318
	bool          assume_state_guess_assume:1;
	char *        assume_state_connection_uuid;

319
	GHashTable *  available_connections;
320
	char *        hw_addr;
321 322
	char *        hw_addr_perm;
	char *        hw_addr_initial;
323
	char *        physical_port_id;
324
	guint         dev_id;
325

326
	NMUnmanagedFlags        unmanaged_mask;
327
	NMUnmanagedFlags        unmanaged_flags;
328
	DeleteOnDeactivateData *delete_on_deactivate_data; /* data for scheduled cleanup when deleting link (g_idle_add) */
329

330 331
	GCancellable *deactivating_cancellable;

332
	guint32         ip4_address;
333

334
	NMActRequest *  queued_act_request;
335
	bool            queued_act_request_is_waiting_for_carrier:1;
336
	NMDBusTrackObjPath act_request;
337 338
	ActivationHandleData act_handle4; /* for layer2 and IPv4. */
	ActivationHandleData act_handle6;
339
	guint           recheck_assume_id;
340
	struct {
341
		guint               call_id;
342 343 344
		NMDeviceStateReason available_reason;
		NMDeviceStateReason unavailable_reason;
	}               recheck_available;
345 346 347 348 349
	struct {
		guint               call_id;
		NMDeviceState       post_state;
		NMDeviceStateReason post_state_reason;
	}               dispatcher;
350

351 352 353 354
	/* Link stuff */
	guint           link_connected_id;
	guint           link_disconnected_id;
	guint           carrier_defer_id;
355
	guint           carrier_wait_id;
356
	gulong          config_changed_id;
357
	guint32         mtu;
358
	guint32         ip6_mtu;
359 360 361
	guint32         mtu_initial;
	guint32         ip6_mtu_initial;
	NMDeviceMtuSource mtu_source;
362

363 364 365
	guint32         v4_route_table;
	guint32         v6_route_table;

366
	/* when carrier goes away, we give a grace period of _get_carrier_wait_ms()
367 368 369 370 371 372 373
	 * until taking action.
	 *
	 * When changing MTU, the device might take longer then that. So, whenever
	 * NM changes the MTU it sets @carrier_wait_until_ms to CARRIER_WAIT_TIME_AFTER_MTU_MS
	 * in the future. This is used to extend the grace period in this particular case. */
	gint64          carrier_wait_until_ms;

Thomas Haller's avatar
Thomas Haller committed
374 375 376
	bool            carrier:1;
	bool            ignore_carrier:1;

377
	bool            up:1;   /* IFF_UP */
378

379 380 381
	bool            v4_commit_first_time:1;
	bool            v6_commit_first_time:1;

382 383 384
	bool            default_route_metric_penalty_ip4_has:1;
	bool            default_route_metric_penalty_ip6_has:1;

385 386
	NMDeviceSysIfaceState sys_iface_state:2;

387 388
	bool            v4_route_table_initialized:1;
	bool            v6_route_table_initialized:1;
389

390
	NMDeviceAutoconnectBlockedFlags autoconnect_blocked_flags:5;
391

392 393 394 395
	bool            is_enslaved:1;
	bool            master_ready_handled:1;

	bool            ipv6ll_handle:1; /* TRUE if NM handles the device's IPv6LL address */
396
	bool            ipv6ll_has:1;
397
	bool            ndisc_started:1;
398

399
	/* Generic DHCP stuff */
400
	char *          dhcp_anycast_address;
401

402 403
	char *          current_stable_id;

404
	/* Proxy Configuration */
405
	NMProxyConfig *proxy_config;
406
	NMPacrunnerManager *pacrunner_manager;
407
	NMPacrunnerCallId *pacrunner_call_id;
408

409 410 411 412 413 414 415 416 417
	/* IP configuration info. Combined config from VPN, settings, and device */
	union {
		struct {
			NMIP6Config *ip_config_6;
			NMIP4Config *ip_config_4;
		};
		NMIPConfig *ip_config_x[2];
	};

418 419 420 421
	union {
		const IpState   ip4_state;
		IpState         ip4_state_;
	};
422
	AppliedConfig   dev_ip4_config; /* Config from DHCP, PPP, LLv4, etc */
423 424 425 426

	/* config from the setting */
	union {
		struct {
427 428
			NMIP6Config *con_ip_config_6;
			NMIP4Config *con_ip_config_4;
429
		};
430
		NMIPConfig *con_ip_config_x[2];
431 432 433 434 435
	};

	/* Stuff added outside NM */
	union {
		struct {
436 437
			NMIP6Config *ext_ip_config_6;
			NMIP4Config *ext_ip_config_4;
438
		};
439
		NMIPConfig *ext_ip_config_x[2];
440 441 442 443 444
	};

	/* VPNs which use this device */
	union {
		struct {
445 446
			GSList *vpn_configs_6;
			GSList *vpn_configs_4;
447
		};
448
		GSList *vpn_configs_x[2];
449 450 451 452 453
	};

	/* WWAN configuration */
	union {
		struct {
454 455
			AppliedConfig  wwan_ip_config_6;
			AppliedConfig  wwan_ip_config_4;
456
		};
457
		AppliedConfig wwan_ip_config_x[2];
458
	};
459

460
	/* DHCPv4 tracking */
461 462 463 464
	struct {
		NMDhcpClient *  client;
		gulong          state_sigid;
		NMDhcp4Config * config;
465
		char *          pac_url;
466
		char *          root_path;
467
		bool            was_active;
468
		guint           grace_id;
469
	} dhcp4;
470

471 472 473 474 475
	struct {
		NMLogDomain log_domain;
		guint timeout;
		guint watch;
		GPid pid;
476 477
		char *binary;
		char *address;
478 479
		guint deadline;
	} gw_ping;
480

481
	/* dnsmasq stuff for shared connections */
482 483
	NMDnsMasqManager *dnsmasq_manager;
	gulong            dnsmasq_state_id;
484

485
	/* Firewall */
486 487
	FirewallState fw_state:4;
	NMFirewallManager *fw_mgr;
488
	NMFirewallManagerCallId fw_call;
489

490
	/* IPv4LL stuff */
491 492
	sd_ipv4ll *    ipv4ll;
	guint          ipv4ll_timeout;
493
	guint          rt6_temporary_not_available_id;
494

495 496 497
	/* IPv4 DAD stuff */
	struct {
		GSList *          dad_list;
498 499
		NMAcdManager * announcing;
	} acd;
500

501 502 503 504
	union {
		const IpState   ip6_state;
		IpState         ip6_state_;
	};
505
	AppliedConfig  ac_ip6_config;  /* config from IPv6 autoconfiguration */
506
	NMIP6Config *  ext_ip6_config_captured; /* Configuration captured from platform. */
507
	NMIP6Config *  dad6_ip6_config;
508
	struct in6_addr ipv6ll_addr;
509

510 511
	GHashTable *   rt6_temporary_not_available;

Lubomir Rintel's avatar
Lubomir Rintel committed
512 513 514 515
	NMNDisc *      ndisc;
	gulong         ndisc_changed_id;
	gulong         ndisc_timeout_id;
	NMSettingIP6ConfigPrivacy ndisc_use_tempaddr;
516

517
	guint          linklocal6_timeout_id;
518
	guint8         linklocal6_dad_counter;
519

520
	GHashTable *   ip6_saved_properties;
521

522 523
	EthtoolState  *ethtool_state;

524 525
	struct {
		NMDhcpClient *   client;
Lubomir Rintel's avatar
Lubomir Rintel committed
526
		NMNDiscDHCPLevel mode;
527
		gulong           state_sigid;
528
		gulong           prefix_sigid;
529 530
		NMDhcp6Config *  config;
		/* IP6 config from DHCP */
531
		AppliedConfig    ip6_config;
532 533
		/* Event ID of the current IP6 config from DHCP */
		char *           event_id;
534
		guint            needed_prefixes;
535
		bool             was_active;
536
		guint            grace_id;
537
	} dhcp6;
538

539 540
	gboolean needs_ip6_subnet;

541
	/* master interface for bridge/bond/team slave */
542
	NMDevice *      master;
543
	gulong          master_ready_id;
544

545
	/* slave management */
546
	CList           slaves;    /* list of SlaveInfo */
547

548 549
	NMMetered       metered;

550 551
	NMSettings *settings;

552 553
	NMNetns *netns;

554
	NMLldpListener *lldp_listener;
555 556

	NMConnectivity *concheck_mgr;
557 558 559 560
	CList concheck_lst_head;
	struct {
		/* if periodic checks are enabled, this is the source id for the next check. */
		guint p_cur_id;
561

562 563
		/* the currently configured max periodic interval. */
		guint p_max_interval;
564

565 566 567
		/* the current interval. If we are probing, the interval might be lower
		 * then the configured max interval. */
		guint p_cur_interval;
568

569 570 571
		/* the timestamp, when we last scheduled the timer p_cur_id with current interval
		 * p_cur_interval. */
		gint64 p_cur_basetime_ns;
572

573 574
		NMConnectivityState state;
	} concheck_x[2];
575 576

	guint check_delete_unrealized_id;
577

578
	struct {
579
		guint timeout_id;
580 581 582 583
		guint refresh_rate_ms;
		guint64 tx_bytes;
		guint64 rx_bytes;
	} stats;
584

585
} NMDevicePrivate;
586

587
G_DEFINE_ABSTRACT_TYPE (NMDevice, nm_device, NM_TYPE_DBUS_OBJECT)
588 589 590 591 592

#define NM_DEVICE_GET_PRIVATE(self) _NM_GET_PRIVATE_PTR(self, NMDevice, NM_IS_DEVICE)

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

593 594 595
static const NMDBusInterfaceInfoExtended interface_info_device;
static const GDBusSignalInfo signal_info_state_changed;

596
static void nm_device_set_proxy_config (NMDevice *self, const char *pac_url);
597

598
static gboolean update_ext_ip_config (NMDevice *self, int addr_family, gboolean intersect_configs);
599

600 601 602 603 604 605
static gboolean nm_device_set_ip_config (NMDevice *self,
                                         int addr_family,
                                         NMIPConfig *config,
                                         gboolean commit,
                                         GPtrArray *ip4_dev_route_blacklist);

606 607 608
static gboolean ip_config_merge_and_apply (NMDevice *self,
                                           int addr_family,
                                           gboolean commit);
609

610
static gboolean nm_device_master_add_slave (NMDevice *self, NMDevice *slave, gboolean configure);
611 612
static void nm_device_slave_notify_enslave (NMDevice *self, gboolean success);
static void nm_device_slave_notify_release (NMDevice *self, NMDeviceStateReason reason);
613

614 615
static void addrconf6_start_with_link_ready (NMDevice *self);
static gboolean linklocal6_start (NMDevice *self);
616

617
static void _carrier_wait_check_queued_act_request (NMDevice *self);
618
static gint64 _get_carrier_wait_ms (NMDevice *self);
619

620
static const char *_activation_func_to_string (ActivationHandleFunc func);
621
static void activation_source_handle_cb (NMDevice *self, int addr_family);
622

623
static void _set_state_full (NMDevice *self,
624 625 626
                             NMDeviceState state,
                             NMDeviceStateReason reason,
                             gboolean quitting);
627
static void queued_state_clear (NMDevice *device);
628 629
static gboolean queued_ip4_config_change (gpointer user_data);
static gboolean queued_ip6_config_change (gpointer user_data);
630
static void ip_check_ping_watch_cb (GPid pid, int status, gpointer user_data);
631
static gboolean ip_config_valid (NMDeviceState state);
632
static NMActStageReturn dhcp4_start (NMDevice *self);
633
static gboolean dhcp6_start (NMDevice *self, gboolean wait_for_ll);
634
static void nm_device_start_ip_check (NMDevice *self);
635 636
static void realize_start_setup (NMDevice *self,
                                 const NMPlatformLink *plink,
637 638
                                 gboolean assume_state_guess_assume,
                                 const char *assume_state_connection_uuid,
639
                                 gboolean set_nm_owned,
640
                                 NMUnmanFlagOp unmanaged_user_explicit);
641
static void _set_mtu (NMDevice *self, guint32 mtu);
642
static void _commit_mtu (NMDevice *self, const NMIP4Config *config);
643
static void _cancel_activation (NMDevice *self);
644

645 646 647 648
static void concheck_update_state (NMDevice *self,
                                   int addr_family,
                                   NMConnectivityState state,
                                   gboolean is_periodic);
649

650
/*****************************************************************************/
651

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
NM_UTILS_LOOKUP_STR_DEFINE_STATIC (queued_state_to_string, NMDeviceState,
	NM_UTILS_LOOKUP_DEFAULT  (                              NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "???"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_UNKNOWN,      NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "unknown"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_UNMANAGED,    NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "unmanaged"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_UNAVAILABLE,  NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "unavailable"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_DISCONNECTED, NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "disconnected"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_PREPARE,      NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "prepare"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_CONFIG,       NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "config"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_NEED_AUTH,    NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "need-auth"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_IP_CONFIG,    NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "ip-config"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_IP_CHECK,     NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "ip-check"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_SECONDARIES,  NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "secondaries"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_ACTIVATED,    NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "activated"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_DEACTIVATING, NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "deactivating"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_FAILED,       NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE "failed"),
);
668

669 670
const char *
nm_device_state_to_str (NMDeviceState state)
671
{
672
	return queued_state_to_string (state) + NM_STRLEN (NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE);
673
}
674

675
NM_UTILS_LOOKUP_STR_DEFINE (nm_device_state_reason_to_str, NMDeviceStateReason,
676
	NM_UTILS_LOOKUP_DEFAULT (NULL),
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 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 738 739
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_UNKNOWN,                        "unknown"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NONE,                           "none"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NOW_MANAGED,                    "managed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NOW_UNMANAGED,                  "unmanaged"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_CONFIG_FAILED,                  "config-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE,          "ip-config-unavailable"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED,              "ip-config-expired"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NO_SECRETS,                     "no-secrets"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT,          "supplicant-disconnect"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_CONFIG_FAILED,       "supplicant-config-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED,              "supplicant-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_TIMEOUT,             "supplicant-timeout"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PPP_START_FAILED,               "ppp-start-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PPP_DISCONNECT,                 "ppp-disconnect"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PPP_FAILED,                     "ppp-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DHCP_START_FAILED,              "dhcp-start-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DHCP_ERROR,                     "dhcp-error"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DHCP_FAILED,                    "dhcp-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SHARED_START_FAILED,            "sharing-start-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SHARED_FAILED,                  "sharing-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_AUTOIP_START_FAILED,            "autoip-start-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_AUTOIP_ERROR,                   "autoip-error"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_AUTOIP_FAILED,                  "autoip-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_BUSY,                     "modem-busy"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_NO_DIAL_TONE,             "modem-no-dialtone"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_NO_CARRIER,               "modem-no-carrier"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_DIAL_TIMEOUT,             "modem-dial-timeout"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_DIAL_FAILED,              "modem-dial-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_INIT_FAILED,              "modem-init-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_APN_FAILED,                 "gsm-apn-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_REGISTRATION_NOT_SEARCHING, "gsm-registration-idle"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_REGISTRATION_DENIED,        "gsm-registration-denied"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_REGISTRATION_TIMEOUT,       "gsm-registration-timeout"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_REGISTRATION_FAILED,        "gsm-registration-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_PIN_CHECK_FAILED,           "gsm-pin-check-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_FIRMWARE_MISSING,               "firmware-missing"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_REMOVED,                        "removed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SLEEPING,                       "sleeping"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_CONNECTION_REMOVED,             "connection-removed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_USER_REQUESTED,                 "user-requested"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_CARRIER,                        "carrier-changed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED,             "connection-assumed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_AVAILABLE,           "supplicant-available"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_NOT_FOUND,                "modem-not-found"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_BT_FAILED,                      "bluetooth-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_SIM_NOT_INSERTED,           "gsm-sim-not-inserted"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_SIM_PIN_REQUIRED,           "gsm-sim-pin-required"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_SIM_PUK_REQUIRED,           "gsm-sim-puk-required"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_SIM_WRONG,                  "gsm-sim-wrong"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_INFINIBAND_MODE,                "infiniband-mode"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DEPENDENCY_FAILED,              "dependency-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_BR2684_FAILED,                  "br2684-bridge-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_MANAGER_UNAVAILABLE,      "modem-manager-unavailable"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SSID_NOT_FOUND,                 "ssid-not-found"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SECONDARY_CONNECTION_FAILED,    "secondary-connection-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DCB_FCOE_FAILED,                "dcb-fcoe-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_TEAMD_CONTROL_FAILED,           "teamd-control-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_FAILED,                   "modem-failed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_AVAILABLE,                "modem-available"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SIM_PIN_INCORRECT,              "sim-pin-incorrect"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NEW_ACTIVATION,                 "new-activation"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PARENT_CHANGED,                 "parent-changed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PARENT_MANAGED_CHANGED,         "parent-managed-changed"),
740
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_OVSDB_FAILED,                   "ovsdb-failed"),
741
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_IP_ADDRESS_DUPLICATE,           "ip-address-duplicate"),
742
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_IP_METHOD_UNSUPPORTED,          "ip-method-unsupported"),
743
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SRIOV_CONFIGURATION_FAILED,     "sriov-configuration-failed"),
744
);
745

746
#define reason_to_string(reason) \
747
	NM_UTILS_LOOKUP_STR (nm_device_state_reason_to_str, reason)
748

749 750 751 752 753 754 755 756
NM_UTILS_LOOKUP_STR_DEFINE_STATIC (mtu_source_to_str, NMDeviceMtuSource,
	NM_UTILS_LOOKUP_DEFAULT_NM_ASSERT ("unknown"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_MTU_SOURCE_NONE,       "none"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_MTU_SOURCE_PARENT,     "parent"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_MTU_SOURCE_IP_CONFIG,  "ip-config"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_MTU_SOURCE_CONNECTION, "connection"),
);

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 828 829 830 831
static void
_ethtool_state_reset (NMDevice *self)
{
	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);

	if (priv->ethtool_state) {
		gs_free NMEthtoolFeatureStates *features = priv->ethtool_state->features;
		gs_free EthtoolState *ethtool_state = g_steal_pointer (&priv->ethtool_state);

		if (!nm_platform_ethtool_set_features (nm_device_get_platform (self),
		                                       ethtool_state->ifindex,
		                                       features,
		                                       ethtool_state->requested,
		                                       FALSE))
			_LOGW (LOGD_DEVICE, "ethtool: failure resetting one or more offload features");
		else
			_LOGD (LOGD_DEVICE, "ethtool: offload features successfully reset");
	}
}

static void
_ethtool_state_set (NMDevice *self)
{
	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
	int ifindex;
	NMConnection *connection;
	NMSettingEthtool *s_ethtool;
	NMPlatform *platform;
	gs_free EthtoolState *ethtool_state = NULL;
	gs_free NMEthtoolFeatureStates *features = NULL;

	_ethtool_state_reset (self);

	connection = nm_device_get_applied_connection (self);
	if (!connection)
		return;

	ifindex = nm_device_get_ip_ifindex (self);
	if (ifindex <= 0)
		return;

	s_ethtool = NM_SETTING_ETHTOOL (nm_connection_get_setting (connection, NM_TYPE_SETTING_ETHTOOL));
	if (!s_ethtool)
		return;

	ethtool_state = g_new (EthtoolState, 1);
	if (nm_setting_ethtool_init_features (s_ethtool, ethtool_state->requested) == 0)
		return;

	platform = nm_device_get_platform (self);

	features = nm_platform_ethtool_get_link_features (platform, ifindex);
	if (!features) {
		_LOGW (LOGD_DEVICE, "ethtool: failure setting offload features (cannot read features)");
		return;
	}

	if (!nm_platform_ethtool_set_features (platform,
	                                       ifindex,
	                                       features,
	                                       ethtool_state->requested,
	                                       TRUE))
		_LOGW (LOGD_DEVICE, "ethtool: failure setting one or more offload features");
	else
		_LOGD (LOGD_DEVICE, "ethtool: offload features successfully set");

	ethtool_state->ifindex = ifindex;
	ethtool_state->features = g_steal_pointer (&features);
	priv->ethtool_state = g_steal_pointer (&ethtool_state);
}

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

832 833 834 835 836 837 838
static gboolean
is_loopback (NMDevice *self)
{
	return    NM_IS_DEVICE_GENERIC (self)
	       && NM_DEVICE_GET_PRIVATE (self)->ifindex == 1;
}

839 840 841 842 843 844
NMSettings *
nm_device_get_settings (NMDevice *self)
{
	return NM_DEVICE_GET_PRIVATE (self)->settings;
}

845 846 847 848 849 850
NMNetns *
nm_device_get_netns (NMDevice *self)
{
	return NM_DEVICE_GET_PRIVATE (self)->netns;
}

851 852 853 854 855 856
NMDedupMultiIndex *
nm_device_get_multi_index (NMDevice *self)
{
	return nm_netns_get_multi_idx (nm_device_get_netns (self));
}

857 858 859 860 861 862
NMPlatform *
nm_device_get_platform (NMDevice *self)
{
	return nm_netns_get_platform (nm_device_get_netns (self));
}

863 864 865 866 867 868 869 870 871 872
static NMConnectivity *
concheck_get_mgr (NMDevice *self)
{
	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);

	if (G_UNLIKELY (!priv->concheck_mgr))
		priv->concheck_mgr = g_object_ref (nm_connectivity_get ());
	return priv->concheck_mgr;
}

873 874 875 876 877 878 879 880 881 882 883 884 885 886
static NMIP4Config *
_ip4_config_new (NMDevice *self)
{
	return nm_ip4_config_new (nm_device_get_multi_index (self),
	                          nm_device_get_ip_ifindex (self));
}

static NMIP6Config *
_ip6_config_new (NMDevice *self)
{
	return nm_ip6_config_new (nm_device_get_multi_index (self),
	                          nm_device_get_ip_ifindex (self));
}

887 888 889 890 891 892 893 894 895 896
static NMIPConfig *
_ip_config_new (NMDevice *self, int addr_family)
{
	nm_assert_addr_family (addr_family);

	return addr_family == AF_INET
	       ? (gpointer) _ip4_config_new (self)
	       : (gpointer) _ip6_config_new (self);
}

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
static void
applied_config_clear (AppliedConfig *config)
{
	g_clear_object (&config->current);
	g_clear_object (&config->orig);
}

static void
applied_config_init (AppliedConfig *config, gpointer ip_config)
{
	nm_g_object_ref (ip_config);
	applied_config_clear (config);
	config->orig = ip_config;
}

912 913 914 915 916 917 918 919
static void
applied_config_init_new (AppliedConfig *config, NMDevice *self, int addr_family)
{
	gs_unref_object NMIPConfig *c = _ip_config_new (self, addr_family);

	applied_config_init (config, c);
}

920 921 922 923 924 925
static NMIPConfig *
applied_config_get_current (AppliedConfig *config)
{
	return config->current ?: config->orig;
}

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 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
static void
applied_config_add_address (AppliedConfig *config, const NMPlatformIPAddress *address)
{
	if (config->orig)
		nm_ip_config_add_address (config->orig, address);
	else
		nm_assert (!config->current);

	if (config->current)
		nm_ip_config_add_address (config->current, address);
}

static void
applied_config_add_nameserver (AppliedConfig *config, const NMIPAddr *ns)
{
	if (config->orig)
		nm_ip_config_add_nameserver (config->orig, ns);
	else
		nm_assert (!config->current);

	if (config->current)
		nm_ip_config_add_nameserver (config->current, ns);
}

static void
applied_config_add_search (AppliedConfig *config, const char *new)
{
	if (config->orig)
		nm_ip_config_add_search (config->orig, new);
	else
		nm_assert (!config->current);

	if (config->current)
		nm_ip_config_add_search (config->current, new);
}

static void
applied_config_reset_searches (AppliedConfig *config)
{
	if (config->orig)
		nm_ip_config_reset_searches (config->orig);
	else
		nm_assert (!config->current);

	if (config->current)
		nm_ip_config_reset_searches (config->current);
}

static void
applied_config_reset_nameservers (AppliedConfig *config)
{
	if (config->orig)
		nm_ip_config_reset_nameservers (config->orig);
	else
		nm_assert (!config->current);

	if (config->current)
		nm_ip_config_reset_nameservers (config->current);
}

986 987 988 989 990 991 992 993 994 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 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
/*****************************************************************************/

NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_sys_iface_state_to_str, NMDeviceSysIfaceState,
	NM_UTILS_LOOKUP_DEFAULT_NM_ASSERT ("unknown"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_SYS_IFACE_STATE_EXTERNAL, "external"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_SYS_IFACE_STATE_ASSUME,   "assume"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_SYS_IFACE_STATE_MANAGED,  "managed"),
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_SYS_IFACE_STATE_REMOVED,  "removed"),
);

NMDeviceSysIfaceState
nm_device_sys_iface_state_get (NMDevice *self)
{
	g_return_val_if_fail (NM_IS_DEVICE (self), NM_DEVICE_SYS_IFACE_STATE_EXTERNAL);

	return NM_DEVICE_GET_PRIVATE (self)->sys_iface_state;
}

gboolean
nm_device_sys_iface_state_is_external (NMDevice *self)
{
	return NM_IN_SET (nm_device_sys_iface_state_get (self),
	                  NM_DEVICE_SYS_IFACE_STATE_EXTERNAL);
}

gboolean
nm_device_sys_iface_state_is_external_or_assume (NMDevice *self)
{
	return NM_IN_SET (nm_device_sys_iface_state_get (self),
	                  NM_DEVICE_SYS_IFACE_STATE_EXTERNAL,
	                  NM_DEVICE_SYS_IFACE_STATE_ASSUME);
}

void
nm_device_sys_iface_state_set (NMDevice *self,
                               NMDeviceSysIfaceState sys_iface_state)
{
	NMDevicePrivate *priv;

	g_return_if_fail (NM_IS_DEVICE (self));
	g_return_if_fail (NM_IN_SET (sys_iface_state,
	                             NM_DEVICE_SYS_IFACE_STATE_EXTERNAL,
	                             NM_DEVICE_SYS_IFACE_STATE_ASSUME,
	                             NM_DEVICE_SYS_IFACE_STATE_MANAGED,
	                             NM_DEVICE_SYS_IFACE_STATE_REMOVED));

	priv = NM_DEVICE_GET_PRIVATE (self);
	if (priv->sys_iface_state != sys_iface_state) {
		_LOGT (LOGD_DEVICE, "sys-iface-state: %s -> %s",
		       _sys_iface_state_to_str (priv->sys_iface_state),
		       _sys_iface_state_to_str (sys_iface_state));
		priv->sys_iface_state = sys_iface_state;
	}

	/* this function only sets a flag, no immediate actions are initiated.
	 *
	 * If you change this, make sure that all callers are fine with such actions. */

	nm_assert (priv->sys_iface_state == sys_iface_state);
}

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
static void
_active_connection_set_state_flags_full (NMDevice *self,
                                         NMActivationStateFlags flags,
                                         NMActivationStateFlags mask)
{
	NMActiveConnection *ac;

	ac = NM_ACTIVE_CONNECTION (nm_device_get_act_request (self));
	if (ac)
		nm_active_connection_set_state_flags_full (ac, flags, mask);
}

static void
_active_connection_set_state_flags (NMDevice *self,
                                    NMActivationStateFlags flags)
{
	_active_connection_set_state_flags_full (self, flags, flags);
}

1066 1067
/*****************************************************************************/

1068 1069 1070 1071 1072 1073 1074 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
void
nm_device_assume_state_get (NMDevice *self,
                            gboolean *out_assume_state_guess_assume,
                            const char **out_assume_state_connection_uuid)
{
	NMDevicePrivate *priv;

	g_return_if_fail (NM_IS_DEVICE (self));

	priv = NM_DEVICE_GET_PRIVATE (self);
	NM_SET_OUT (out_assume_state_guess_assume, priv->assume_state_guess_assume);
	NM_SET_OUT (out_assume_state_connection_uuid, priv->assume_state_connection_uuid);
}

static void
_assume_state_set (NMDevice *self,
                   gboolean assume_state_guess_assume,
                   const char *assume_state_connection_uuid)
{
	NMDevicePrivate *priv;

	nm_assert (NM_IS_DEVICE (self));

	priv = NM_DEVICE_GET_PRIVATE (self);
	if (   priv->assume_state_guess_assume == !!assume_state_guess_assume
	    && nm_streq0 (priv->assume_state_connection_uuid, assume_state_connection_uuid))
		return;

	_LOGD (LOGD_DEVICE, "assume-state: set guess-assume=%c, connection=%s%s%s",
	       assume_state_guess_assume ? '1' : '0',
	       NM_PRINT_FMT_QUOTE_STRING (assume_state_connection_uuid));
	priv->assume_state_guess_assume = assume_state_guess_assume;
	g_free (priv->assume_state_connection_uuid);
	priv->assume_state_connection_uuid = g_strdup (assume_state_connection_uuid);
}

void
nm_device_assume_state_reset (NMDevice *self)
{
	g_return_if_fail (NM_IS_DEVICE (self));

	_assume_state_set (self, FALSE, NULL);
}

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

1114
static void
1115
init_ip_config_dns_priority (NMDevice *self, NMIPConfig *config)
1116
{
1117
	const char *property;
1118
	int priority;
1119

1120
	property = (nm_ip_config_get_addr_family (config) == AF_INET)
1121 1122
	             ? NM_CON_DEFAULT ("ipv4.dns-priority")
	             : NM_CON_DEFAULT ("ipv6.dns-priority");
1123 1124 1125 1126 1127 1128 1129

	priority = nm_config_data_get_connection_default_int64 (NM_CONFIG_GET_DATA,
	                                                        property,
	                                                        self,
	                                                        G_MININT,
	                                                        G_MAXINT,
	                                                        0);
1130
	nm_ip_config_set_dns_priority (config, priority ?: NM_DNS_PRIORITY_DEFAULT_NORMAL);
1131 1132
}

1133
/*****************************************************************************/
1134

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
static char *
nm_device_sysctl_ip_conf_get (NMDevice *self,
                              int addr_family,
                              const char *property)
{
	const char *ifname;

	nm_assert_addr_family (addr_family);

	ifname = nm_device_get_ip_iface_from_platform (self);
	if (!ifname)
		return NULL;
	return nm_platform_sysctl_ip_conf_get (nm_device_get_platform (self), addr_family, ifname, property);
}

static gint64
nm_device_sysctl_ip_conf_get_int_checked (NMDevice *self,
                                          int addr_family,
                                          const char *property,
                                          guint base,
                                          gint64 min,
                                          gint64 max,
                                          gint64 fallback)
{
	const char *ifname;

	nm_assert_addr_family (addr_family);

	ifname = nm_device_get_ip_iface_from_platform (self);
	if (!ifname) {
		errno = EINVAL;
		return fallback;
	}
	return nm_platform_sysctl_ip_conf_get_int_checked (nm_device_get_platform (self),
	                                                   addr_family,
	                                                   ifname,
	                                                   property,
	                                                   base,
	                                                   min,
	                                                   max,
	                                                   fallback);
}

1178
gboolean
1179 1180 1181 1182
nm_device_sysctl_ip_conf_set (NMDevice *self,
                              int addr_family,
                              const char *property,
                              const char *value)
1183
{
1184
	NMPlatform *platform = nm_device_get_platform (self);
1185
	gs_free char *value_to_free = NULL;
1186
	const char *ifname;
1187

1188 1189
	nm_assert_addr_family (addr_family);

1190
	ifname = nm_device_get_ip_iface_from_platform (self);
1191 1192 1193 1194
	if (!ifname)
		return FALSE;

	if (!value) {
1195
		/* Set to a default value when we've got a NULL @value. */
1196 1197 1198 1199
		value_to_free = nm_platform_sysctl_ip_conf_get (platform,
		                                                addr_family,
		                                                "default",
		                                                property);
1200 1201 1202
		value = value_to_free;
		if (!value)
			return FALSE;
1203 1204
	}

1205 1206 1207 1208 1209
	return nm_platform_sysctl_ip_conf_set (platform,
	                                       addr_family,
	                                       ifname,
	                                       property,
	                                       value);
1210 1211
}

1212 1213
/*****************************************************************************/

1214 1215
gboolean
nm_device_has_capability (NMDevice *self, NMDeviceCapabilities caps)
1216
{
1217
	return NM_FLAGS_ANY (NM_DEVICE_GET_PRIVATE (self)->capabilities, caps);
1218
}
1219

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
static void
_add_capabilities (NMDevice *self, NMDeviceCapabilities capabilities)
{
	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);

	if (!NM_FLAGS_ALL (priv->capabilities, capabilities)) {
		priv->capabilities |= capabilities;
		_notify (self, PROP_CAPABILITIES);
	}
}

1231
/*****************************************************************************/
1232

1233
static const char *
1234 1235 1236
_get_stable_id (NMDevice *self,
                NMConnection *connection,
                NMUtilsStableType *out_stable_type)
1237
{
1238
	NMDevicePrivate *priv;
1239

1240
	nm_assert (NM_IS_DEVICE (self));
1241 1242 1243
	nm_assert (NM_IS_CONNECTION (connection));
	nm_assert (out_stable_type);

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
	priv = NM_DEVICE_GET_PRIVATE (self);

	/* we cache the generated stable ID for the time of an activation.
	 *
	 * The reason is, that we don't want the stable-id to change as long
	 * as the device is active.
	 *
	 * Especially with ${RANDOM} stable-id we want to generate *one* configuration
	 * for each activation. */
	if (G_UNLIKELY (!priv->current_stable_id)) {
		gs_free char *default_id = NULL;
		gs_free char *generated = NULL;
		NMUtilsStableType stable_type;
		NMSettingConnection *s_con;
1258 1259
		gboolean hwaddr_is_fake;
		const char *hwaddr;
1260 1261 1262 1263 1264 1265 1266 1267 1268
		const char *stable_id;
		const char *uuid;

		s_con = nm_connection_get_setting_connection (connection);

		stable_id = nm_setting_connection_get_stable_id (s_con);

		if (!stable_id) {
			default_id = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA,
1269
			                                                    NM_CON_DEFAULT ("connection.stable-id"),
1270 1271 1272
			                                                    self);
			stable_id = default_id;
		}
1273

1274 1275
		uuid = nm_connection_get_uuid (connection);

1276 1277 1278 1279 1280
		/* the cloned-mac-address may be generated based on the stable-id.
		 * Thus, at this point, we can only use the permanant MAC address
		 * as seed. */
		hwaddr = nm_device_get_permanent_hw_address_full (self, TRUE, &hwaddr_is_fake);

1281
		stable_type = nm_utils_stable_id_parse (stable_id,
1282
		                                        nm_device_get_ip_iface (self),
1283
		                                        !hwaddr_is_fake ? hwaddr : NULL,
1284
		                                        nm_utils_boot_id_str (),
1285
		                                        uuid,
1286 1287 1288 1289
		                                        &generated);

		/* current_stable_id_type is a bitfield! */
		priv->current_stable_id_type = stable_type;
1290 1291 1292
		nm_assert (stable_type <= (NMUtilsStableType) 0x3);
		nm_assert (stable_type + (NMUtilsStableType) 1 > (NMUtilsStableType) 0);
		nm_assert (priv->current_stable_id_type == stable_type);
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309

		if (stable_type == NM_UTILS_STABLE_TYPE_UUID)
			priv->current_stable_id = g_strdup (uuid);
		else if (stable_type == NM_UTILS_STABLE_TYPE_STABLE_ID)
			priv->current_stable_id = g_strdup (stable_id);
		else if (stable_type == NM_UTILS_STABLE_TYPE_GENERATED)
			priv->current_stable_id = nm_str_realloc (nm_utils_stable_id_generated_complete (generated));
		else {
			nm_assert (stable_type == NM_UTILS_STABLE_TYPE_RANDOM);
			priv->current_stable_id = nm_str_realloc (nm_utils_stable_id_random ());
		}
		_LOGT (LOGD_DEVICE,
		       "stable-id: type=%d, \"%s\""
		       "%s%s%s",
		       (int) priv->current_stable_id_type,
		       priv->current_stable_id,
		       NM_PRINT_FMT_QUOTED (stable_type == NM_UTILS_STABLE_TYPE_GENERATED, " from \"", generated, "\"", ""));
1310 1311
	}

1312 1313
	*out_stable_type = priv->current_stable_id_type;
	return priv->current_stable_id;
1314 1315
}

1316
/*****************************************************************************/
1317

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_ip_state_to_string, IpState,
	NM_UTILS_LOOKUP_DEFAULT_WARN ("unknown"),
	NM_UTILS_LOOKUP_STR_ITEM (IP_NONE, "none"),
	NM_UTILS_LOOKUP_STR_ITEM (IP_WAIT, "wait"),
	NM_UTILS_LOOKUP_STR_ITEM (IP_CONF, "conf"),
	NM_UTILS_LOOKUP_STR_ITEM (IP_DONE, "done"),
	NM_UTILS_LOOKUP_STR_ITEM (IP_FAIL, "fail"),
);

static void
_set_ip_state (NMDevice *self, int addr_family, IpState new_state)
{
	IpState *p;
	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);

1333
	nm_assert_addr_family (addr_family);
1334

1335 1336
	p =   (addr_family == AF_INET)
	    ? &priv->ip4_state_
1337
	    : &priv->ip6_state_;
1338 1339

	if (*p != new_state) {
1340 1341 1342 1343
		_LOGT (LOGD_DEVICE, "ip%c-state: set to %d (%s)",
		       nm_utils_addr_family_to_char (addr_family),
		       (int) new_state,
		       _ip_state_to_string (new_state));
1344
		*p = new_state;
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355

		if (new_state == IP_DONE) {
			/* we only set the IPx_READY flag once we reach IP_DONE state. We don't
			 * ever clear it, even if we later enter IP_FAIL state.
			 *
			 * This is not documented/guaranteed behavior, but seems to make sense for now. */
			_active_connection_set_state_flags (self,
			                                    addr_family == AF_INET
			                                      ? NM_ACTIVATION_STATE_FLAG_IP4_READY
			                                      : NM_ACTIVATION_STATE_FLAG_IP6_READY);
		}
1356 1357 1358 1359 1360
	}
}

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

1361 1362 1363 1364 1365
const char *
nm_device_get_udi (NMDevice *self)
{
	g_return_val_if_fail (self != NULL, NULL);

1366
	return NM_DEVICE_GET_PRIVATE (self)->udi;
1367 1368 1369 1370 1371
}

const char *
nm_device_get_iface (NMDevice *self)
{
1372
	g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
1373

1374
	return NM_DEVICE_GET_PRIVATE (self)->iface;
1375 1376
}

1377
gboolean
1378
nm_device_take_over_link (NMDevice *self, int ifindex, char **old_name)
1379 1380 1381 1382
{
	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
	const NMPlatformLink *plink;
	NMPlatform *platform;
1383
	gboolean up, success = TRUE;
1384
	gs_free char *name = NULL;
1385 1386

	g_return_val_if_fail (priv->ifindex <= 0, FALSE);
1387

1388
	NM_SET_OUT (old_name, NULL);
1389 1390

	platform = nm_device_get_platform (self);
1391
	plink = nm_platform_link_get (platform, ifindex);
1392 1393 1394
	if (!plink)
		return FALSE;

1395
	if (!nm_streq (plink->name, nm_device_get_iface (self))) {
1396
		up = NM_FLAGS_HAS (plink->n_ifi_flags, IFF_UP);
1397
		name = g_strdup (plink->name);