nm-device.c 613 KB
Newer Older
Lubomir Rintel's avatar
Lubomir Rintel committed
1
// SPDX-License-Identifier: GPL-2.0+
2
/*
Lubomir Rintel's avatar
Lubomir Rintel committed
3
 * Copyright (C) 2005 - 2018 Red Hat, Inc.
4
 * Copyright (C) 2006 - 2008 Novell, Inc.
5 6
 */

7
#include "nm-default.h"
8

9 10
#include "nm-device.h"

11
#include <netinet/in.h>
Dan Williams's avatar
Dan Williams committed
12 13
#include <unistd.h>
#include <sys/ioctl.h>
14 15 16 17
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <arpa/inet.h>
18
#include <fcntl.h>
19
#include <linux/if_addr.h>
20
#include <linux/if_arp.h>
21
#include <linux/rtnetlink.h>
22

23
#include "nm-std-aux/unaligned.h"
24 25
#include "nm-glib-aux/nm-dedup-multi.h"
#include "nm-glib-aux/nm-random-utils.h"
26
#include "systemd/nm-sd-utils-shared.h"
27

28 29
#include "nm-libnm-core-intern/nm-ethtool-utils.h"
#include "nm-libnm-core-intern/nm-common-macros.h"
30 31
#include "nm-device-private.h"
#include "NetworkManagerUtils.h"
32
#include "nm-manager.h"
33
#include "platform/nm-platform.h"
34
#include "platform/nm-platform-utils.h"
35
#include "platform/nmp-object.h"
36
#include "platform/nmp-rules-manager.h"
37 38 39
#include "ndisc/nm-ndisc.h"
#include "ndisc/nm-lndp-ndisc.h"
#include "dhcp/nm-dhcp-manager.h"
40
#include "dhcp/nm-dhcp-utils.h"
41
#include "nm-act-request.h"
Atul Anand's avatar
Atul Anand committed
42
#include "nm-proxy-config.h"
43 44
#include "nm-ip4-config.h"
#include "nm-ip6-config.h"
Atul Anand's avatar
Atul Anand committed
45
#include "nm-pacrunner-manager.h"
46
#include "dnsmasq/nm-dnsmasq-manager.h"
47
#include "nm-dhcp-config.h"
48
#include "nm-rfkill-manager.h"
Jiri Popelka's avatar
Jiri Popelka committed
49
#include "nm-firewall-manager.h"
50 51
#include "settings/nm-settings-connection.h"
#include "settings/nm-settings.h"
52
#include "nm-setting-ethtool.h"
53
#include "nm-setting-user.h"
54
#include "nm-auth-utils.h"
55
#include "nm-keep-alive.h"
56
#include "nm-netns.h"
57
#include "nm-dispatcher.h"
58
#include "nm-config.h"
59
#include "c-list/src/c-list.h"
60
#include "dns/nm-dns-manager.h"
61
#include "nm-acd-manager.h"
62
#include "nm-core-internal.h"
63
#include "systemd/nm-sd.h"
64
#include "nm-lldp-listener.h"
Beniamino Galvani's avatar
Beniamino Galvani committed
65
#include "nm-audit-manager.h"
66 67
#include "nm-connectivity.h"
#include "nm-dbus-interface.h"
68 69

#include "nm-device-generic.h"
70
#include "nm-device-vlan.h"
71
#include "nm-device-vrf.h"
72
#include "nm-device-wireguard.h"
73

74 75 76
#include "nm-device-logging.h"
_LOG_DECLARE_SELF (NMDevice);

77
/*****************************************************************************/
78

79
#define DEFAULT_AUTOCONNECT    TRUE
80 81 82 83

static guint32
dhcp_grace_period_from_timeout (guint32 timeout)
{
84
#define DHCP_GRACE_PERIOD_MULTIPLIER 2U
85

86 87 88 89 90 91 92 93 94
	nm_assert (timeout > 0);
	nm_assert (timeout < G_MAXINT32);

	if (timeout < G_MAXUINT32 / DHCP_GRACE_PERIOD_MULTIPLIER)
		return timeout * DHCP_GRACE_PERIOD_MULTIPLIER;

	return G_MAXUINT32;
}

95
#define CARRIER_WAIT_TIME_MS 6000
96 97
#define CARRIER_WAIT_TIME_AFTER_MTU_MS 10000

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

102 103 104 105 106 107 108 109 110 111 112
/*****************************************************************************/

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

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

typedef struct {
113
	CList lst_slave;
114 115 116 117 118 119 120 121 122 123 124 125
	NMDevice *slave;
	gulong watch_id;
	bool slave_is_enslaved;
	bool configure;
} SlaveInfo;

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

126 127 128 129 130 131 132 133 134
typedef struct {
	NMDevice *device;
	GCancellable *cancellable;
	NMPlatformAsyncCallback callback;
	gpointer callback_data;
	guint num_vfs;
	NMTernary autoprobe;
} SriovOp;

135
typedef void (*AcdCallback) (NMDevice *, NMIP4Config **, gboolean);
136 137

typedef struct {
138
	AcdCallback callback;
139 140
	NMDevice *device;
	NMIP4Config **configs;
141
} AcdData;
142 143 144 145 146 147 148 149

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

150 151 152 153 154 155 156
typedef enum {
	FIREWALL_STATE_UNMANAGED = 0,
	FIREWALL_STATE_INITIALIZED,
	FIREWALL_STATE_WAIT_STAGE_3,
	FIREWALL_STATE_WAIT_IP_CONFIG,
} FirewallState;

157 158 159 160 161 162
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;

163
typedef struct {
164
	NMDhcpClient *client;
165
	NMDhcpConfig *config;
166
	gulong state_sigid;
167 168
	guint grace_id;
	bool grace_pending:1;
169
	bool was_active:1;
170 171
} DhcpData;

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

185 186 187 188
typedef struct {
	int ifindex;
	NMEthtoolFeatureStates *features;
	NMTernary requested[_NM_ETHTOOL_ID_FEATURE_NUM];
189
	NMEthtoolCoalesceState *coalesce;
190
	NMEthtoolRingState *ring;
191 192
} EthtoolState;

193
/*****************************************************************************/
194

195
enum {
196
	STATE_CHANGED,
197
	AUTOCONNECT_ALLOWED,
198 199
	IP4_CONFIG_CHANGED,
	IP6_CONFIG_CHANGED,
200 201
	IP6_PREFIX_DELEGATED,
	IP6_SUBNET_NEEDED,
202
	REMOVED,
203
	RECHECK_AUTO_ACTIVATE,
204
	RECHECK_ASSUME,
205 206 207 208
	LAST_SIGNAL,
};
static guint signals[LAST_SIGNAL] = { 0 };

209
NM_GOBJECT_PROPERTIES_DEFINE (NMDevice,
210
	PROP_UDI,
211
	PROP_PATH,
212 213 214
	PROP_IFACE,
	PROP_IP_IFACE,
	PROP_DRIVER,
215 216
	PROP_DRIVER_VERSION,
	PROP_FIRMWARE_VERSION,
217
	PROP_CAPABILITIES,
218
	PROP_CARRIER,
Jiří Klimeš's avatar
Jiří Klimeš committed
219
	PROP_MTU,
220 221 222 223 224 225
	PROP_IP4_ADDRESS,
	PROP_IP4_CONFIG,
	PROP_DHCP4_CONFIG,
	PROP_IP6_CONFIG,
	PROP_DHCP6_CONFIG,
	PROP_STATE,
226
	PROP_STATE_REASON,
227 228
	PROP_ACTIVE_CONNECTION,
	PROP_DEVICE_TYPE,
229
	PROP_LINK_TYPE,
230
	PROP_MANAGED,
231
	PROP_AUTOCONNECT,
232
	PROP_FIRMWARE_MISSING,
233
	PROP_NM_PLUGIN_MISSING,
234 235 236
	PROP_TYPE_DESC,
	PROP_RFKILL_TYPE,
	PROP_IFINDEX,
237
	PROP_AVAILABLE_CONNECTIONS,
238
	PROP_PHYSICAL_PORT_ID,
Dan Winship's avatar
Dan Winship committed
239
	PROP_MASTER,
240
	PROP_PARENT,
241
	PROP_HW_ADDRESS,
242
	PROP_PERM_HW_ADDRESS,
243
	PROP_HAS_PENDING_ACTION,
244
	PROP_METERED,
245
	PROP_LLDP_NEIGHBORS,
246
	PROP_REAL,
247
	PROP_SLAVES,
248 249 250
	PROP_STATISTICS_REFRESH_RATE_MS,
	PROP_STATISTICS_TX_BYTES,
	PROP_STATISTICS_RX_BYTES,
251 252
	PROP_IP4_CONNECTIVITY,
	PROP_IP6_CONNECTIVITY,
253
	PROP_INTERFACE_FLAGS,
254
);
255

256
typedef struct _NMDevicePrivate {
257
	bool in_state_changed;
258

259 260 261
	guint device_link_changed_id;
	guint device_ip_link_changed_id;

262
	NMDeviceState state;
263
	NMDeviceStateReason state_reason;
264
	struct {
265 266 267
		guint id;

		/* The @state/@reason is only valid, when @id is set. */
268 269 270
		NMDeviceState state;
		NMDeviceStateReason reason;
	} queued_state;
Thomas Haller's avatar
Thomas Haller committed
271

272 273
	union {
		struct {
274 275
			guint queued_ip_config_id_6;
			guint queued_ip_config_id_4;
276
		};
277
		guint queued_ip_config_id_x[2];
278 279
	};

280
	GSList *pending_actions;
281
	GSList *dad6_failed_addrs;
282

283
	NMDBusTrackObjPath parent_device;
284

285
	char *        udi;
286
	char *        path;
287
	char *        iface;   /* may change, could be renamed by user */
288
	int           ifindex;
289

290 291
	int parent_ifindex;

292 293
	int auth_retries;

294 295 296 297
	union {
		const guint8 hw_addr_len; /* read-only */
		guint8 hw_addr_len_;
	};
298 299

	HwAddrType hw_addr_type:5;
300

301 302
	bool          real:1;

303 304 305
	bool update_ip_config_completed_v4:1;
	bool update_ip_config_completed_v6:1;

306
	char *        ip_iface;
307
	int           ip_ifindex;
308 309
	NMDeviceType  type;
	char *        type_desc;
310
	NMLinkType    link_type;
311
	NMDeviceCapabilities capabilities;
312
	char *        driver;
313 314
	char *        driver_version;
	char *        firmware_version;
315
	RfKillType    rfkill_type;
316 317 318
	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 */
319 320 321

	NMUtilsStableType current_stable_id_type:3;

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

324 325 326
	bool          assume_state_guess_assume:1;
	char *        assume_state_connection_uuid;

327
	GHashTable *  available_connections;
328
	char *        hw_addr;
329 330
	char *        hw_addr_perm;
	char *        hw_addr_initial;
331
	char *        physical_port_id;
332
	guint         dev_id;
333

334
	NMUnmanagedFlags        unmanaged_mask;
335
	NMUnmanagedFlags        unmanaged_flags;
336
	DeleteOnDeactivateData *delete_on_deactivate_data; /* data for scheduled cleanup when deleting link (g_idle_add) */
337

338 339
	GCancellable *deactivating_cancellable;

340
	guint32         ip4_address;
341

342
	NMActRequest *  queued_act_request;
343
	bool            queued_act_request_is_waiting_for_carrier:1;
344
	NMDBusTrackObjPath act_request;
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

	union {
		struct {
			guint activation_source_id_6;
			guint activation_source_id_4; /* for layer2 and IPv4. */
		};
		guint activation_source_id_x[2];
	};

	union {
		struct {
			ActivationHandleFunc activation_source_func_6;
			ActivationHandleFunc activation_source_func_4; /* for layer2 and IPv4. */
		};
		ActivationHandleFunc activation_source_func_x[2];
	};

362
	guint           recheck_assume_id;
363

364
	struct {
365
		guint               call_id;
366 367
		NMDeviceStateReason available_reason;
		NMDeviceStateReason unavailable_reason;
368 369
	} recheck_available;

370
	struct {
371
		NMDispatcherCallId *call_id;
372 373
		NMDeviceState       post_state;
		NMDeviceStateReason post_state_reason;
374
	} dispatcher;
375

376 377 378 379
	/* Link stuff */
	guint           link_connected_id;
	guint           link_disconnected_id;
	guint           carrier_defer_id;
380
	guint           carrier_wait_id;
381
	gulong          config_changed_id;
Jiří Klimeš's avatar
Jiří Klimeš committed
382
	guint32         mtu;
383
	guint32         ip6_mtu;
384 385 386
	guint32         mtu_initial;
	guint32         ip6_mtu_initial;
	NMDeviceMtuSource mtu_source;
387

388 389 390
	guint32         v4_route_table;
	guint32         v6_route_table;

391
	/* when carrier goes away, we give a grace period of _get_carrier_wait_ms()
392 393 394 395 396 397 398
	 * 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
399 400 401
	bool            carrier:1;
	bool            ignore_carrier:1;

402
	bool            up:1;   /* IFF_UP */
403

404 405 406
	bool            v4_commit_first_time:1;
	bool            v6_commit_first_time:1;

407 408 409
	bool            default_route_metric_penalty_ip4_has:1;
	bool            default_route_metric_penalty_ip6_has:1;

410 411
	NMDeviceSysIfaceState sys_iface_state:2;

412 413
	bool            v4_route_table_initialized:1;
	bool            v6_route_table_initialized:1;
414

415 416
	bool            v4_route_table_all_sync_before:1;
	bool            v6_route_table_all_sync_before:1;
417

418
	NMDeviceAutoconnectBlockedFlags autoconnect_blocked_flags:5;
419

420 421 422
	bool            is_enslaved:1;

	bool            ipv6ll_handle:1; /* TRUE if NM handles the device's IPv6LL address */
423
	bool            ipv6ll_has:1;
424
	bool            ndisc_started:1;
425
	bool            device_link_changed_down:1;
426

427 428
	bool            concheck_rp_filter_checked:1;

429 430
	NMDeviceStageState stage1_sriov_state:3;

431
	/* Generic DHCP stuff */
432
	char *          dhcp_anycast_address;
433

434 435
	char *          current_stable_id;

Atul Anand's avatar
Atul Anand committed
436
	/* Proxy Configuration */
437
	NMProxyConfig *proxy_config;
438
	NMPacrunnerConfId *pacrunner_conf_id;
Atul Anand's avatar
Atul Anand committed
439

440 441 442 443 444 445 446 447 448
	/* 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];
	};

449 450
	/* Config from DHCP, PPP, LLv4, etc */
	AppliedConfig  dev_ip_config_4;
451 452 453 454

	/* config from the setting */
	union {
		struct {
455 456
			NMIP6Config *con_ip_config_6;
			NMIP4Config *con_ip_config_4;
457
		};
458
		NMIPConfig *con_ip_config_x[2];
459 460 461 462 463
	};

	/* Stuff added outside NM */
	union {
		struct {
464 465
			NMIP6Config *ext_ip_config_6;
			NMIP4Config *ext_ip_config_4;
466
		};
467
		NMIPConfig *ext_ip_config_x[2];
468 469 470 471 472
	};

	/* VPNs which use this device */
	union {
		struct {
473 474
			GSList *vpn_configs_6;
			GSList *vpn_configs_4;
475
		};
476
		GSList *vpn_configs_x[2];
477 478
	};

479 480
	/* Extra device configuration, injected by the subclass of NMDevice.
	 * This is used for example by NMDeviceModem for WWAN configuration. */
481 482
	union {
		struct {
483 484
			AppliedConfig dev2_ip_config_6;
			AppliedConfig dev2_ip_config_4;
485
		};
486
		AppliedConfig dev2_ip_config_x[2];
487
	};
488

489
	/* DHCPv4 tracking */
490
	struct {
491
		char *          pac_url;
492
		char *          root_path;
493
	} dhcp4;
494

495 496 497 498 499 500 501 502 503 504
	struct {
		/* IP6 config from DHCP */
		AppliedConfig    ip6_config;
		/* Event ID of the current IP6 config from DHCP */
		char *           event_id;
		gulong           prefix_sigid;
		NMNDiscDHCPLevel mode;
		guint            needed_prefixes;
	} dhcp6;

505 506 507 508 509 510 511 512
	union {
		struct {
			DhcpData dhcp_data_6;
			DhcpData dhcp_data_4;
		};
		DhcpData dhcp_data_x[2];
	};

513 514 515 516 517
	struct {
		NMLogDomain log_domain;
		guint timeout;
		guint watch;
		GPid pid;
518 519
		char *binary;
		char *address;
520 521
		guint deadline;
	} gw_ping;
522

523
	/* dnsmasq stuff for shared connections */
524 525
	NMDnsMasqManager *dnsmasq_manager;
	gulong            dnsmasq_state_id;
526

527
	/* Firewall */
528 529
	FirewallState fw_state:4;
	NMFirewallManager *fw_mgr;
530
	NMFirewallManagerCallId *fw_call;
Jiri Popelka's avatar
Jiri Popelka committed
531

532
	/* IPv4LL stuff */
533 534
	sd_ipv4ll *    ipv4ll;
	guint          ipv4ll_timeout;
535
	guint          rt6_temporary_not_available_id;
536

537 538 539
	/* IPv4 DAD stuff */
	struct {
		GSList *          dad_list;
540 541
		NMAcdManager * announcing;
	} acd;
542

543
	union {
544 545 546 547 548 549 550 551
		struct {
			const NMDeviceIPState   ip_state_6;
			const NMDeviceIPState   ip_state_4;
		};
		union {
			const NMDeviceIPState   ip_state_x[2];
			NMDeviceIPState         ip_state_x_[2];
		};
552
	};
553

554
	AppliedConfig  ac_ip6_config;  /* config from IPv6 autoconfiguration */
555
	NMIP6Config *  ext_ip6_config_captured; /* Configuration captured from platform. */
556
	NMIP6Config *  dad6_ip6_config;
557
	struct in6_addr ipv6ll_addr;
558

559 560
	GHashTable *   rt6_temporary_not_available;

Lubomir Rintel's avatar
Lubomir Rintel committed
561 562 563 564
	NMNDisc *      ndisc;
	gulong         ndisc_changed_id;
	gulong         ndisc_timeout_id;
	NMSettingIP6ConfigPrivacy ndisc_use_tempaddr;
565

566
	guint          linklocal6_timeout_id;
567
	guint8         linklocal6_dad_counter;
568

569
	GHashTable *   ip6_saved_properties;
570

571 572
	EthtoolState  *ethtool_state;

573 574
	gboolean needs_ip6_subnet;

Jiri Pirko's avatar
Jiri Pirko committed
575
	/* master interface for bridge/bond/team slave */
576
	NMDevice *      master;
577
	gulong          master_ready_id;
578

579
	/* slave management */
580
	CList           slaves;    /* list of SlaveInfo */
581

582 583
	NMMetered       metered;

584
	NMSettings *settings;
585
	NMManager *manager;
586

587 588
	NMNetns *netns;

589
	NMLldpListener *lldp_listener;
590 591

	NMConnectivity *concheck_mgr;
592 593 594 595
	CList concheck_lst_head;
	struct {
		/* if periodic checks are enabled, this is the source id for the next check. */
		guint p_cur_id;
596

597 598
		/* the currently configured max periodic interval. */
		guint p_max_interval;
599

600 601 602
		/* the current interval. If we are probing, the interval might be lower
		 * then the configured max interval. */
		guint p_cur_interval;
603

604 605 606
		/* the timestamp, when we last scheduled the timer p_cur_id with current interval
		 * p_cur_interval. */
		gint64 p_cur_basetime_ns;
607

608 609
		NMConnectivityState state;
	} concheck_x[2];
610 611

	guint check_delete_unrealized_id;
612
	guint32 interface_flags;
613

614 615 616 617
	struct {
		SriovOp *pending;    /* SR-IOV operation currently running */
		SriovOp *next;       /* next SR-IOV operation scheduled */
	} sriov;
618
	guint sriov_reset_pending;
619

620
	struct {
621
		guint timeout_id;
622 623 624 625
		guint refresh_rate_ms;
		guint64 tx_bytes;
		guint64 rx_bytes;
	} stats;
626
} NMDevicePrivate;
627

628
G_DEFINE_ABSTRACT_TYPE (NMDevice, nm_device, NM_TYPE_DBUS_OBJECT)
629 630 631 632 633

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

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

634 635 636
static const NMDBusInterfaceInfoExtended interface_info_device;
static const GDBusSignalInfo signal_info_state_changed;

637
static void nm_device_set_proxy_config (NMDevice *self, const char *pac_url);
Atul Anand's avatar
Atul Anand committed
638

639
static gboolean update_ext_ip_config (NMDevice *self, int addr_family, gboolean intersect_configs);
640

641 642 643 644 645 646
static gboolean nm_device_set_ip_config (NMDevice *self,
                                         int addr_family,
                                         NMIPConfig *config,
                                         gboolean commit,
                                         GPtrArray *ip4_dev_route_blacklist);

647 648 649
static gboolean ip_config_merge_and_apply (NMDevice *self,
                                           int addr_family,
                                           gboolean commit);
650

651
static gboolean nm_device_master_add_slave (NMDevice *self, NMDevice *slave, gboolean configure);
652 653
static void nm_device_slave_notify_enslave (NMDevice *self, gboolean success);
static void nm_device_slave_notify_release (NMDevice *self, NMDeviceStateReason reason);
654

655 656
static void addrconf6_start_with_link_ready (NMDevice *self);
static gboolean linklocal6_start (NMDevice *self);
657

658
static void _carrier_wait_check_queued_act_request (NMDevice *self);
659
static gint64 _get_carrier_wait_ms (NMDevice *self);
660

661 662
static const char *_activation_func_to_string (ActivationHandleFunc func);

663
static void _set_state_full (NMDevice *self,
664 665 666
                             NMDeviceState state,
                             NMDeviceStateReason reason,
                             gboolean quitting);
667
static void queued_state_clear (NMDevice *device);
668 669
static gboolean queued_ip4_config_change (gpointer user_data);
static gboolean queued_ip6_config_change (gpointer user_data);
670
static void ip_check_ping_watch_cb (GPid pid, int status, gpointer user_data);
671
static gboolean ip_config_valid (NMDeviceState state);
672
static NMActStageReturn dhcp4_start (NMDevice *self);
673
static gboolean dhcp6_start (NMDevice *self, gboolean wait_for_ll);
674
static void nm_device_start_ip_check (NMDevice *self);
675 676
static void realize_start_setup (NMDevice *self,
                                 const NMPlatformLink *plink,
677 678
                                 gboolean assume_state_guess_assume,
                                 const char *assume_state_connection_uuid,
679
                                 gboolean set_nm_owned,
680 681
                                 NMUnmanFlagOp unmanaged_user_explicit,
                                 gboolean force_platform_init);
682
static void _set_mtu (NMDevice *self, guint32 mtu);
683
static void _commit_mtu (NMDevice *self, const NMIP4Config *config);
684
static void _cancel_activation (NMDevice *self);
685

686 687 688 689
static void concheck_update_state (NMDevice *self,
                                   int addr_family,
                                   NMConnectivityState state,
                                   gboolean is_periodic);
690

691 692 693 694 695 696 697 698
static void activate_stage4_ip_config_timeout_4 (NMDevice *self);
static void activate_stage4_ip_config_timeout_6 (NMDevice *self);

static void (*const activate_stage4_ip_config_timeout_x[2]) (NMDevice *self) = {
	activate_stage4_ip_config_timeout_6,
	activate_stage4_ip_config_timeout_4,
};

699 700
static void sriov_op_cb (GError *error, gpointer user_data);

701 702 703 704 705 706 707 708
static void activate_stage5_ip_config_result_4 (NMDevice *self);
static void activate_stage5_ip_config_result_6 (NMDevice *self);

static void (*const activate_stage5_ip_config_result_x[2]) (NMDevice *self) = {
	activate_stage5_ip_config_result_6,
	activate_stage5_ip_config_result_4,
};

709
/*****************************************************************************/
710

711 712
static
NM_UTILS_LOOKUP_STR_DEFINE (queued_state_to_string, NMDeviceState,
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	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"),
);
728

729 730
const char *
nm_device_state_to_str (NMDeviceState state)
731
{
732
	return queued_state_to_string (state) + NM_STRLEN (NM_PENDING_ACTIONPREFIX_QUEUED_STATE_CHANGE);
733
}
734

735
NM_UTILS_LOOKUP_STR_DEFINE (nm_device_state_reason_to_str, NMDeviceStateReason,
736
	NM_UTILS_LOOKUP_DEFAULT (NULL),
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 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
	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"),
800
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_OVSDB_FAILED,                   "ovsdb-failed"),
801
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_IP_ADDRESS_DUPLICATE,           "ip-address-duplicate"),
802
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_IP_METHOD_UNSUPPORTED,          "ip-method-unsupported"),
Beniamino Galvani's avatar
Beniamino Galvani committed
803
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SRIOV_CONFIGURATION_FAILED,     "sriov-configuration-failed"),
804
	NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PEER_NOT_FOUND,                 "peer-not-found"),
805
);
806

807
#define reason_to_string_a(reason) NM_UTILS_LOOKUP_STR_A (nm_device_state_reason_to_str, reason)
808

809 810
static
NM_UTILS_LOOKUP_STR_DEFINE (mtu_source_to_str, NMDeviceMtuSource,
811 812 813 814 815 816 817
	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"),
);

818
/*****************************************************************************/
819

820
static void
821 822 823
_ethtool_features_reset (NMDevice *self,
                         NMPlatform *platform,
                         EthtoolState *ethtool_state)
824
{
825
	gs_free NMEthtoolFeatureStates *features;
826

827 828 829 830 831 832 833 834 835 836
	features = g_steal_pointer (&ethtool_state->features);

	if (!nm_platform_ethtool_set_features (platform,
	                                       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");
837 838 839
}

static void
840 841 842 843
_ethtool_features_set (NMDevice *self,
                       NMPlatform *platform,
                       EthtoolState *ethtool_state,
                       NMSettingEthtool *s_ethtool)
844 845 846
{
	gs_free NMEthtoolFeatureStates *features = NULL;

847 848
	if (ethtool_state->features)
		_ethtool_features_reset (self, platform, ethtool_state);
849 850 851 852

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

853 854
	features = nm_platform_ethtool_get_link_features (platform,
	                                                  ethtool_state->ifindex);
855 856 857 858 859 860
	if (!features) {
		_LOGW (LOGD_DEVICE, "ethtool: failure setting offload features (cannot read features)");
		return;
	}

	if (!nm_platform_ethtool_set_features (platform,
861
	                                       ethtool_state->ifindex,
862 863 864 865 866 867 868 869
	                                       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->features = g_steal_pointer (&features);
870 871 872 873 874 875 876
}

static void
_ethtool_coalesce_reset (NMDevice *self,
                         NMPlatform *platform,
                         EthtoolState *ethtool_state)
{