1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager -- Network link manager
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Copyright (C) 2005 - 2013 Red Hat, Inc.
19 * Copyright (C) 2006 - 2008 Novell, Inc.
22 #include "nm-default.h"
24 #include <netinet/in.h>
28 #include <sys/ioctl.h>
30 #include <sys/types.h>
32 #include <arpa/inet.h>
34 #include <netlink/route/addr.h>
35 #include <linux/if_addr.h>
37 #include "nm-device.h"
38 #include "nm-device-private.h"
39 #include "NetworkManagerUtils.h"
40 #include "nm-manager.h"
41 #include "nm-platform.h"
43 #include "nm-lndp-rdisc.h"
44 #include "nm-dhcp-manager.h"
45 #include "nm-activation-request.h"
46 #include "nm-ip4-config.h"
47 #include "nm-ip6-config.h"
48 #include "nm-dnsmasq-manager.h"
49 #include "nm-dhcp4-config.h"
50 #include "nm-dhcp6-config.h"
51 #include "nm-rfkill-manager.h"
52 #include "nm-firewall-manager.h"
53 #include "nm-enum-types.h"
54 #include "nm-settings-connection.h"
55 #include "nm-connection-provider.h"
56 #include "nm-auth-utils.h"
57 #include "nm-dispatcher.h"
58 #include "nm-config.h"
59 #include "nm-dns-manager.h"
60 #include "nm-core-internal.h"
61 #include "nm-default-route-manager.h"
62 #include "nm-route-manager.h"
63 #include "nm-lldp-listener.h"
64 #include "sd-ipv4ll.h"
65 #include "nm-audit-manager.h"
66 #include "nm-arping-manager.h"
68 #include "nm-device-logging.h"
69 _LOG_DECLARE_SELF (NMDevice);
71 #include "nmdbus-device.h"
73 G_DEFINE_ABSTRACT_TYPE (NMDevice, nm_device, NM_TYPE_EXPORTED_OBJECT)
75 #define NM_DEVICE_GET_PRIVATE(o) ((o)->priv)
84 RECHECK_AUTO_ACTIVATE,
88 static guint signals[LAST_SIGNAL] = { 0 };
90 NM_GOBJECT_PROPERTIES_DEFINE (NMDevice,
96 PROP_FIRMWARE_VERSION,
107 PROP_ACTIVE_CONNECTION,
112 PROP_FIRMWARE_MISSING,
113 PROP_NM_PLUGIN_MISSING,
117 PROP_AVAILABLE_CONNECTIONS,
118 PROP_PHYSICAL_PORT_ID,
122 PROP_HAS_PENDING_ACTION,
129 #define DEFAULT_AUTOCONNECT TRUE
131 /***********************************************************/
133 #define PENDING_ACTION_DHCP4 "dhcp4"
134 #define PENDING_ACTION_DHCP6 "dhcp6"
135 #define PENDING_ACTION_AUTOCONF6 "autoconf6"
137 typedef void (*ActivationHandleFunc) (NMDevice *self);
140 ActivationHandleFunc func;
142 } ActivationHandleData;
145 CLEANUP_TYPE_DECONFIGURE,
147 CLEANUP_TYPE_REMOVED,
160 NMDeviceStateReason reason;
166 gboolean slave_is_enslaved;
172 NMLogDomain log_domain;
185 } DeleteOnDeactivateData;
187 typedef void (*ArpingCallback) (NMDevice *, NMIP4Config **, gboolean);
190 ArpingCallback callback;
192 NMIP4Config **configs;
195 typedef struct _NMDevicePrivate {
196 gboolean in_state_changed;
198 guint device_link_changed_id;
199 guint device_ip_link_changed_id;
202 NMDeviceStateReason state_reason;
203 QueuedState queued_state;
204 guint queued_ip4_config_id;
205 guint queued_ip6_config_id;
206 GSList *pending_actions;
207 GSList *dad6_failed_addrs;
210 char * iface; /* may change, could be renamed by user */
217 char * type_description;
218 NMLinkType link_type;
219 NMDeviceCapabilities capabilities;
221 char * driver_version;
222 char * firmware_version;
223 RfKillType rfkill_type;
224 gboolean firmware_missing;
225 gboolean nm_plugin_missing;
226 GHashTable * available_connections;
230 char * initial_hw_addr;
231 char * physical_port_id;
234 NMUnmanagedFlags unmanaged_mask;
235 NMUnmanagedFlags unmanaged_flags;
236 gboolean is_nm_owned; /* whether the device is a device owned and created by NM */
237 DeleteOnDeactivateData *delete_on_deactivate_data; /* data for scheduled cleanup when deleting link (g_idle_add) */
239 GCancellable *deactivating_cancellable;
243 NMActRequest * queued_act_request;
244 gboolean queued_act_request_is_waiting_for_carrier;
245 NMActRequest * act_request;
246 ActivationHandleData act_handle4; /* for layer2 and IPv4. */
247 ActivationHandleData act_handle6;
248 guint recheck_assume_id;
251 NMDeviceStateReason available_reason;
252 NMDeviceStateReason unavailable_reason;
256 NMDeviceState post_state;
257 NMDeviceStateReason post_state_reason;
261 guint link_connected_id;
262 guint link_disconnected_id;
263 guint carrier_defer_id;
265 guint carrier_wait_id;
266 gboolean ignore_carrier;
268 gboolean up; /* IFF_UP */
270 /* Generic DHCP stuff */
271 guint32 dhcp_timeout;
272 char * dhcp_anycast_address;
274 /* IP4 configuration info */
275 NMIP4Config * ip4_config; /* Combined config from VPN, settings, and device */
277 NMIP4Config * con_ip4_config; /* config from the setting */
278 NMIP4Config * dev_ip4_config; /* Config from DHCP, PPP, LLv4, etc */
279 NMIP4Config * ext_ip4_config; /* Stuff added outside NM */
280 NMIP4Config * wwan_ip4_config; /* WWAN configuration */
281 GSList * vpn4_configs; /* VPNs which use this device */
284 gboolean v4_is_assumed;
285 NMPlatformIP4Route v4;
287 gboolean v6_is_assumed;
288 NMPlatformIP6Route v6;
291 gboolean v4_commit_first_time;
292 gboolean v6_commit_first_time;
294 /* DHCPv4 tracking */
295 NMDhcpClient * dhcp4_client;
296 gulong dhcp4_state_sigid;
297 NMDhcp4Config * dhcp4_config;
298 guint dhcp4_restart_id;
302 /* dnsmasq stuff for shared connections */
303 NMDnsMasqManager *dnsmasq_manager;
304 gulong dnsmasq_state_id;
308 NMFirewallManagerCallId fw_call;
312 guint ipv4ll_timeout;
317 NMArpingManager * announcing;
320 /* IP6 configuration info */
321 NMIP6Config * ip6_config;
323 NMIP6Config * con_ip6_config; /* config from the setting */
324 NMIP6Config * wwan_ip6_config;
325 NMIP6Config * ext_ip6_config; /* Stuff added outside NM */
326 NMIP6Config * ext_ip6_config_captured; /* Configuration captured from platform. */
327 GSList * vpn6_configs; /* VPNs which use this device */
328 gboolean nm_ipv6ll; /* TRUE if NM handles the device's IPv6LL address */
332 gulong rdisc_changed_id;
333 gulong rdisc_timeout_id;
334 NMSettingIP6ConfigPrivacy rdisc_use_tempaddr;
335 /* IP6 config from autoconf */
336 NMIP6Config * ac_ip6_config;
338 guint linklocal6_timeout_id;
339 guint8 linklocal6_dad_counter;
341 GHashTable * ip6_saved_properties;
343 NMDhcpClient * dhcp6_client;
344 NMRDiscDHCPLevel dhcp6_mode;
345 gulong dhcp6_state_sigid;
346 NMDhcp6Config * dhcp6_config;
347 /* IP6 config from DHCP */
348 NMIP6Config * dhcp6_ip6_config;
349 /* Event ID of the current IP6 config from DHCP */
350 char * dhcp6_event_id;
351 guint dhcp6_restart_id;
353 /* allow autoconnect feature */
354 gboolean autoconnect;
356 /* master interface for bridge/bond/team slave */
358 gboolean is_enslaved;
359 gboolean master_ready_handled;
360 gulong master_ready_id;
362 /* slave management */
364 GSList * slaves; /* list of SlaveInfo */
368 NMConnectionProvider *con_provider;
369 NMLldpListener *lldp_listener;
371 guint check_delete_unrealized_id;
374 static gboolean nm_device_set_ip4_config (NMDevice *self,
376 guint32 default_route_metric,
378 gboolean routes_full_sync,
379 NMDeviceStateReason *reason);
380 static gboolean ip4_config_merge_and_apply (NMDevice *self,
383 NMDeviceStateReason *out_reason);
385 static gboolean nm_device_set_ip6_config (NMDevice *self,
388 gboolean routes_full_sync,
389 NMDeviceStateReason *reason);
391 static void nm_device_master_add_slave (NMDevice *self, NMDevice *slave, gboolean configure);
392 static void nm_device_slave_notify_enslave (NMDevice *self, gboolean success);
393 static void nm_device_slave_notify_release (NMDevice *self, NMDeviceStateReason reason);
395 static gboolean addrconf6_start_with_link_ready (NMDevice *self);
396 static NMActStageReturn linklocal6_start (NMDevice *self);
398 static void _carrier_wait_check_queued_act_request (NMDevice *self);
400 static const char *_activation_func_to_string (ActivationHandleFunc func);
401 static void activation_source_handle_cb (NMDevice *self, int family);
403 static void _set_state_full (NMDevice *self,
405 NMDeviceStateReason reason,
408 static gboolean queued_ip4_config_change (gpointer user_data);
409 static gboolean queued_ip6_config_change (gpointer user_data);
410 static void ip_check_ping_watch_cb (GPid pid, gint status, gpointer user_data);
411 static gboolean ip_config_valid (NMDeviceState state);
412 static NMActStageReturn dhcp4_start (NMDevice *self, NMConnection *connection, NMDeviceStateReason *reason);
413 static gboolean dhcp6_start (NMDevice *self, gboolean wait_for_ll, NMDeviceStateReason *reason);
414 static void nm_device_start_ip_check (NMDevice *self);
415 static void realize_start_setup (NMDevice *self, const NMPlatformLink *plink);
417 /***********************************************************/
419 #define QUEUED_PREFIX "queued state change to "
421 static const char *state_table[] = {
422 [NM_DEVICE_STATE_UNKNOWN] = QUEUED_PREFIX "unknown",
423 [NM_DEVICE_STATE_UNMANAGED] = QUEUED_PREFIX "unmanaged",
424 [NM_DEVICE_STATE_UNAVAILABLE] = QUEUED_PREFIX "unavailable",
425 [NM_DEVICE_STATE_DISCONNECTED] = QUEUED_PREFIX "disconnected",
426 [NM_DEVICE_STATE_PREPARE] = QUEUED_PREFIX "prepare",
427 [NM_DEVICE_STATE_CONFIG] = QUEUED_PREFIX "config",
428 [NM_DEVICE_STATE_NEED_AUTH] = QUEUED_PREFIX "need-auth",
429 [NM_DEVICE_STATE_IP_CONFIG] = QUEUED_PREFIX "ip-config",
430 [NM_DEVICE_STATE_IP_CHECK] = QUEUED_PREFIX "ip-check",
431 [NM_DEVICE_STATE_SECONDARIES] = QUEUED_PREFIX "secondaries",
432 [NM_DEVICE_STATE_ACTIVATED] = QUEUED_PREFIX "activated",
433 [NM_DEVICE_STATE_DEACTIVATING] = QUEUED_PREFIX "deactivating",
434 [NM_DEVICE_STATE_FAILED] = QUEUED_PREFIX "failed",
438 queued_state_to_string (NMDeviceState state)
440 if ((gsize) state < G_N_ELEMENTS (state_table))
441 return state_table[state];
442 return state_table[NM_DEVICE_STATE_UNKNOWN];
446 state_to_string (NMDeviceState state)
448 return queued_state_to_string (state) + strlen (QUEUED_PREFIX);
451 NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_reason_to_string, NMDeviceStateReason,
452 NM_UTILS_LOOKUP_DEFAULT (NULL),
453 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_UNKNOWN, "unknown"),
454 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NONE, "none"),
455 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NOW_MANAGED, "managed"),
456 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NOW_UNMANAGED, "unmanaged"),
457 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_CONFIG_FAILED, "config-failed"),
458 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE, "ip-config-unavailable"),
459 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED, "ip-config-expired"),
460 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NO_SECRETS, "no-secrets"),
461 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT, "supplicant-disconnect"),
462 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_CONFIG_FAILED, "supplicant-config-failed"),
463 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED, "supplicant-failed"),
464 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_TIMEOUT, "supplicant-timeout"),
465 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PPP_START_FAILED, "ppp-start-failed"),
466 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PPP_DISCONNECT, "ppp-disconnect"),
467 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PPP_FAILED, "ppp-failed"),
468 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DHCP_START_FAILED, "dhcp-start-failed"),
469 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DHCP_ERROR, "dhcp-error"),
470 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DHCP_FAILED, "dhcp-failed"),
471 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SHARED_START_FAILED, "sharing-start-failed"),
472 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SHARED_FAILED, "sharing-failed"),
473 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_AUTOIP_START_FAILED, "autoip-start-failed"),
474 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_AUTOIP_ERROR, "autoip-error"),
475 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_AUTOIP_FAILED, "autoip-failed"),
476 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_BUSY, "modem-busy"),
477 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_NO_DIAL_TONE, "modem-no-dialtone"),
478 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_NO_CARRIER, "modem-no-carrier"),
479 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_DIAL_TIMEOUT, "modem-dial-timeout"),
480 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_DIAL_FAILED, "modem-dial-failed"),
481 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_INIT_FAILED, "modem-init-failed"),
482 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_APN_FAILED, "gsm-apn-failed"),
483 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_REGISTRATION_NOT_SEARCHING, "gsm-registration-idle"),
484 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_REGISTRATION_DENIED, "gsm-registration-denied"),
485 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_REGISTRATION_TIMEOUT, "gsm-registration-timeout"),
486 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_REGISTRATION_FAILED, "gsm-registration-failed"),
487 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_PIN_CHECK_FAILED, "gsm-pin-check-failed"),
488 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_FIRMWARE_MISSING, "firmware-missing"),
489 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_REMOVED, "removed"),
490 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SLEEPING, "sleeping"),
491 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_CONNECTION_REMOVED, "connection-removed"),
492 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_USER_REQUESTED, "user-requested"),
493 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_CARRIER, "carrier-changed"),
494 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED, "connection-assumed"),
495 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SUPPLICANT_AVAILABLE, "supplicant-available"),
496 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_NOT_FOUND, "modem-not-found"),
497 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_BT_FAILED, "bluetooth-failed"),
498 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_SIM_NOT_INSERTED, "gsm-sim-not-inserted"),
499 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_SIM_PIN_REQUIRED, "gsm-sim-pin-required"),
500 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_SIM_PUK_REQUIRED, "gsm-sim-puk-required"),
501 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_GSM_SIM_WRONG, "gsm-sim-wrong"),
502 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_INFINIBAND_MODE, "infiniband-mode"),
503 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DEPENDENCY_FAILED, "dependency-failed"),
504 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_BR2684_FAILED, "br2684-bridge-failed"),
505 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_MANAGER_UNAVAILABLE, "modem-manager-unavailable"),
506 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SSID_NOT_FOUND, "ssid-not-found"),
507 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SECONDARY_CONNECTION_FAILED, "secondary-connection-failed"),
508 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_DCB_FCOE_FAILED, "dcb-fcoe-failed"),
509 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_TEAMD_CONTROL_FAILED, "teamd-control-failed"),
510 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_FAILED, "modem-failed"),
511 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_MODEM_AVAILABLE, "modem-available"),
512 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_SIM_PIN_INCORRECT, "sim-pin-incorrect"),
513 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_NEW_ACTIVATION, "new-activation"),
514 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PARENT_CHANGED, "parent-changed"),
515 NM_UTILS_LOOKUP_STR_ITEM (NM_DEVICE_STATE_REASON_PARENT_MANAGED_CHANGED, "parent-managed-changed"),
518 #define reason_to_string(reason) \
519 NM_UTILS_LOOKUP_STR (_reason_to_string, reason)
521 /***********************************************************/
524 nm_device_ipv6_sysctl_set (NMDevice *self, const char *property, const char *value)
526 return nm_platform_sysctl_set (NM_PLATFORM_GET, nm_utils_ip6_property_path (nm_device_get_ip_iface (self), property), value);
530 nm_device_ipv6_sysctl_get_int32 (NMDevice *self, const char *property, gint32 fallback)
532 return nm_platform_sysctl_get_int32 (NM_PLATFORM_GET, nm_utils_ip6_property_path (nm_device_get_ip_iface (self), property), fallback);
536 nm_device_has_capability (NMDevice *self, NMDeviceCapabilities caps)
538 return NM_FLAGS_ANY (NM_DEVICE_GET_PRIVATE (self)->capabilities, caps);
541 /***********************************************************/
544 nm_device_get_udi (NMDevice *self)
546 g_return_val_if_fail (self != NULL, NULL);
548 return NM_DEVICE_GET_PRIVATE (self)->udi;
552 nm_device_get_iface (NMDevice *self)
554 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
556 return NM_DEVICE_GET_PRIVATE (self)->iface;
560 nm_device_get_ifindex (NMDevice *self)
562 g_return_val_if_fail (NM_IS_DEVICE (self), 0);
564 return NM_DEVICE_GET_PRIVATE (self)->ifindex;
568 * nm_device_is_software:
569 * @self: the #NMDevice
571 * Indicates if the device is a software-based virtual device without
572 * backing hardware, which can be added and removed programmatically.
574 * Returns: %TRUE if the device is a software-based device
577 nm_device_is_software (NMDevice *self)
579 return NM_FLAGS_HAS (NM_DEVICE_GET_PRIVATE (self)->capabilities, NM_DEVICE_CAP_IS_SOFTWARE);
584 * @self: the #NMDevice
586 * Returns: %TRUE if the device exists, %FALSE if the device is a placeholder
589 nm_device_is_real (NMDevice *self)
591 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
593 return NM_DEVICE_GET_PRIVATE (self)->real;
597 nm_device_get_ip_iface (NMDevice *self)
599 NMDevicePrivate *priv;
601 g_return_val_if_fail (self != NULL, NULL);
603 priv = NM_DEVICE_GET_PRIVATE (self);
604 /* If it's not set, default to iface */
605 return priv->ip_iface ? priv->ip_iface : priv->iface;
609 nm_device_get_ip_ifindex (NMDevice *self)
611 NMDevicePrivate *priv;
613 g_return_val_if_fail (self != NULL, 0);
615 priv = NM_DEVICE_GET_PRIVATE (self);
616 /* If it's not set, default to ifindex */
617 return priv->ip_iface ? priv->ip_ifindex : priv->ifindex;
621 nm_device_set_ip_iface (NMDevice *self, const char *iface)
623 NMDevicePrivate *priv;
626 g_return_if_fail (NM_IS_DEVICE (self));
628 priv = NM_DEVICE_GET_PRIVATE (self);
629 if (!g_strcmp0 (iface, priv->ip_iface))
632 old_ip_iface = priv->ip_iface;
633 priv->ip_ifindex = 0;
635 priv->ip_iface = g_strdup (iface);
636 if (priv->ip_iface) {
637 priv->ip_ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, priv->ip_iface);
638 if (priv->ip_ifindex > 0) {
639 if (nm_platform_check_support_user_ipv6ll (NM_PLATFORM_GET))
640 nm_platform_link_set_user_ipv6ll_enabled (NM_PLATFORM_GET, priv->ip_ifindex, TRUE);
642 if (!nm_platform_link_is_up (NM_PLATFORM_GET, priv->ip_ifindex))
643 nm_platform_link_set_up (NM_PLATFORM_GET, priv->ip_ifindex, NULL);
645 /* Device IP interface must always be a kernel network interface */
646 _LOGW (LOGD_HW, "failed to look up interface index");
650 /* We don't care about any saved values from the old iface */
651 g_hash_table_remove_all (priv->ip6_saved_properties);
653 /* Emit change notification */
654 if (g_strcmp0 (old_ip_iface, priv->ip_iface))
655 _notify (self, PROP_IP_IFACE);
656 g_free (old_ip_iface);
660 get_ip_iface_identifier (NMDevice *self, NMUtilsIPv6IfaceId *out_iid)
662 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
663 NMLinkType link_type;
664 const guint8 *hwaddr = NULL;
665 size_t hwaddr_len = 0;
669 /* If we get here, we *must* have a kernel netdev, which implies an ifindex */
670 ifindex = nm_device_get_ip_ifindex (self);
673 link_type = nm_platform_link_get_type (NM_PLATFORM_GET, ifindex);
674 g_return_val_if_fail (link_type > NM_LINK_TYPE_UNKNOWN, 0);
676 hwaddr = nm_platform_link_get_address (NM_PLATFORM_GET, ifindex, &hwaddr_len);
680 success = nm_utils_get_ipv6_interface_identifier (link_type,
686 _LOGW (LOGD_HW, "failed to generate interface identifier "
687 "for link type %u hwaddr_len %zu", link_type, hwaddr_len);
693 nm_device_get_ip_iface_identifier (NMDevice *self, NMUtilsIPv6IfaceId *iid)
695 return NM_DEVICE_GET_CLASS (self)->get_ip_iface_identifier (self, iid);
699 nm_device_get_driver (NMDevice *self)
701 g_return_val_if_fail (self != NULL, NULL);
703 return NM_DEVICE_GET_PRIVATE (self)->driver;
707 nm_device_get_driver_version (NMDevice *self)
709 g_return_val_if_fail (self != NULL, NULL);
711 return NM_DEVICE_GET_PRIVATE (self)->driver_version;
715 nm_device_get_device_type (NMDevice *self)
717 g_return_val_if_fail (NM_IS_DEVICE (self), NM_DEVICE_TYPE_UNKNOWN);
719 return NM_DEVICE_GET_PRIVATE (self)->type;
723 nm_device_get_link_type (NMDevice *self)
725 g_return_val_if_fail (NM_IS_DEVICE (self), NM_LINK_TYPE_UNKNOWN);
727 return NM_DEVICE_GET_PRIVATE (self)->link_type;
731 * nm_device_get_metered:
732 * @setting: the #NMDevice
734 * Returns: the #NMDevice:metered property of the device.
739 nm_device_get_metered (NMDevice *self)
741 g_return_val_if_fail (NM_IS_DEVICE (self), NM_METERED_UNKNOWN);
743 return NM_DEVICE_GET_PRIVATE (self)->metered;
747 * nm_device_get_priority():
748 * @self: the #NMDevice
750 * Returns: the device's routing priority. Lower numbers means a "better"
751 * device, eg higher priority.
754 nm_device_get_priority (NMDevice *self)
756 g_return_val_if_fail (NM_IS_DEVICE (self), 1000);
758 /* Device 'priority' is used for the default route-metric and is based on
759 * the device type. The settings ipv4.route-metric and ipv6.route-metric
760 * can overwrite this default.
762 * Currently for both IPv4 and IPv6 we use the same default values.
764 * The route-metric is used for the metric of the routes of device.
765 * This also applies to the default route. Therefore it affects also
766 * which device is the "best".
768 * For comparison, note that iproute2 by default adds IPv4 routes with
769 * metric 0, and IPv6 routes with metric 1024. The latter is the IPv6
770 * "user default" in the kernel (NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6).
771 * In kernel, the full uint32_t range is available for route
772 * metrics (except for IPv6, where 0 means 1024).
775 switch (nm_device_get_device_type (self)) {
776 /* 50 is reserved for VPN (NM_VPN_ROUTE_METRIC_DEFAULT) */
777 case NM_DEVICE_TYPE_ETHERNET:
778 case NM_DEVICE_TYPE_VETH:
780 case NM_DEVICE_TYPE_INFINIBAND:
782 case NM_DEVICE_TYPE_ADSL:
784 case NM_DEVICE_TYPE_WIMAX:
786 case NM_DEVICE_TYPE_BOND:
788 case NM_DEVICE_TYPE_TEAM:
790 case NM_DEVICE_TYPE_VLAN:
792 case NM_DEVICE_TYPE_MACVLAN:
794 case NM_DEVICE_TYPE_BRIDGE:
796 case NM_DEVICE_TYPE_TUN:
798 case NM_DEVICE_TYPE_VXLAN:
800 case NM_DEVICE_TYPE_WIFI:
802 case NM_DEVICE_TYPE_OLPC_MESH:
804 case NM_DEVICE_TYPE_IP_TUNNEL:
806 case NM_DEVICE_TYPE_MODEM:
808 case NM_DEVICE_TYPE_BT:
810 case NM_DEVICE_TYPE_GENERIC:
812 case NM_DEVICE_TYPE_UNKNOWN:
814 case NM_DEVICE_TYPE_UNUSED1:
815 case NM_DEVICE_TYPE_UNUSED2:
816 /* omit default: to get compiler warning about missing switch cases */
823 _get_ipx_route_metric (NMDevice *self,
828 NMSettingIPConfig *s_ip;
829 NMConnection *connection;
831 g_return_val_if_fail (NM_IS_DEVICE (self), G_MAXUINT32);
833 connection = nm_device_get_applied_connection (self);
836 ? nm_connection_get_setting_ip4_config (connection)
837 : nm_connection_get_setting_ip6_config (connection);
839 /* Slave interfaces don't have IP settings, but we may get here when
840 * external changes are made or when noticing IP changes when starting
841 * the slave connection.
844 route_metric = nm_setting_ip_config_get_route_metric (s_ip);
845 if (route_metric >= 0)
850 /* use the current NMConfigData, which makes this configuration reloadable.
851 * Note that that means that the route-metric might change between SIGHUP.
852 * You must cache the returned value if that is a problem. */
853 value = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA,
854 is_v4 ? "ipv4.route-metric" : "ipv6.route-metric", self);
856 route_metric = _nm_utils_ascii_str_to_int64 (value, 10, 0, G_MAXUINT32, -1);
859 if (route_metric >= 0)
862 route_metric = nm_device_get_priority (self);
865 route_metric = nm_utils_ip6_route_metric_normalize (route_metric);
870 nm_device_get_ip4_route_metric (NMDevice *self)
872 return _get_ipx_route_metric (self, TRUE);
876 nm_device_get_ip6_route_metric (NMDevice *self)
878 return _get_ipx_route_metric (self, FALSE);
881 const NMPlatformIP4Route *
882 nm_device_get_ip4_default_route (NMDevice *self, gboolean *out_is_assumed)
884 NMDevicePrivate *priv;
886 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
888 priv = NM_DEVICE_GET_PRIVATE (self);
891 *out_is_assumed = priv->default_route.v4_is_assumed;
893 return priv->default_route.v4_has ? &priv->default_route.v4 : NULL;
896 const NMPlatformIP6Route *
897 nm_device_get_ip6_default_route (NMDevice *self, gboolean *out_is_assumed)
899 NMDevicePrivate *priv;
901 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
903 priv = NM_DEVICE_GET_PRIVATE (self);
906 *out_is_assumed = priv->default_route.v6_is_assumed;
908 return priv->default_route.v6_has ? &priv->default_route.v6 : NULL;
912 nm_device_get_type_desc (NMDevice *self)
914 g_return_val_if_fail (self != NULL, NULL);
916 return NM_DEVICE_GET_PRIVATE (self)->type_desc;
920 nm_device_get_type_description (NMDevice *self)
922 g_return_val_if_fail (self != NULL, NULL);
924 /* Beware: this function should return the same
925 * value as nm_device_get_type_description() in libnm. */
927 return NM_DEVICE_GET_CLASS (self)->get_type_description (self);
931 get_type_description (NMDevice *self)
933 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
935 if (!priv->type_description) {
936 const char *typename;
938 typename = G_OBJECT_TYPE_NAME (self);
939 if (g_str_has_prefix (typename, "NMDevice"))
941 priv->type_description = g_ascii_strdown (typename, -1);
944 return priv->type_description;
948 nm_device_has_carrier (NMDevice *self)
950 return NM_DEVICE_GET_PRIVATE (self)->carrier;
954 nm_device_get_act_request (NMDevice *self)
956 g_return_val_if_fail (self != NULL, NULL);
958 return NM_DEVICE_GET_PRIVATE (self)->act_request;
961 NMSettingsConnection *
962 nm_device_get_settings_connection (NMDevice *self)
964 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
966 return priv->act_request ? nm_act_request_get_settings_connection (priv->act_request) : NULL;
970 nm_device_get_applied_connection (NMDevice *self)
972 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
974 return priv->act_request ? nm_act_request_get_applied_connection (priv->act_request) : NULL;
978 nm_device_has_unmodified_applied_connection (NMDevice *self, NMSettingCompareFlags compare_flags)
980 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
982 if (!priv->act_request)
985 return nm_active_connection_has_unmodified_applied_connection ((NMActiveConnection *) priv->act_request, compare_flags);
989 nm_device_get_applied_setting (NMDevice *device, GType setting_type)
992 NMSetting *setting = NULL;
994 g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
996 req = nm_device_get_act_request (device);
998 NMConnection *connection;
1000 connection = nm_act_request_get_applied_connection (req);
1002 setting = nm_connection_get_setting (connection, setting_type);
1009 nm_device_get_rfkill_type (NMDevice *self)
1011 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
1013 return NM_DEVICE_GET_PRIVATE (self)->rfkill_type;
1017 nm_device_get_physical_port_id (NMDevice *self)
1019 return NM_DEVICE_GET_PRIVATE (self)->physical_port_id;
1022 /***********************************************************/
1025 nm_device_uses_generated_assumed_connection (NMDevice *self)
1027 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1028 NMSettingsConnection *connection;
1030 if ( priv->act_request
1031 && nm_active_connection_get_assumed (NM_ACTIVE_CONNECTION (priv->act_request))) {
1032 connection = nm_act_request_get_settings_connection (priv->act_request);
1034 && nm_settings_connection_get_nm_generated_assumed (connection))
1041 nm_device_uses_assumed_connection (NMDevice *self)
1043 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1045 if ( priv->act_request
1046 && nm_active_connection_get_assumed (NM_ACTIVE_CONNECTION (priv->act_request)))
1052 find_slave_info (NMDevice *self, NMDevice *slave)
1054 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1058 for (iter = priv->slaves; iter; iter = g_slist_next (iter)) {
1060 if (info->slave == slave)
1067 * nm_device_master_enslave_slave:
1068 * @self: the master device
1069 * @slave: the slave device to enslave
1070 * @connection: (allow-none): the slave device's connection
1072 * If @self is capable of enslaving other devices (ie it's a bridge, bond, team,
1073 * etc) then this function enslaves @slave.
1075 * Returns: %TRUE on success, %FALSE on failure or if this device cannot enslave
1079 nm_device_master_enslave_slave (NMDevice *self, NMDevice *slave, NMConnection *connection)
1082 gboolean success = FALSE;
1085 g_return_val_if_fail (self != NULL, FALSE);
1086 g_return_val_if_fail (slave != NULL, FALSE);
1087 g_return_val_if_fail (NM_DEVICE_GET_CLASS (self)->enslave_slave != NULL, FALSE);
1089 info = find_slave_info (self, slave);
1093 if (info->slave_is_enslaved)
1096 configure = (info->configure && connection != NULL);
1098 g_return_val_if_fail (nm_device_get_state (slave) >= NM_DEVICE_STATE_DISCONNECTED, FALSE);
1100 success = NM_DEVICE_GET_CLASS (self)->enslave_slave (self, slave, connection, configure);
1101 info->slave_is_enslaved = success;
1104 nm_device_slave_notify_enslave (info->slave, success);
1106 /* Ensure the device's hardware address is up-to-date; it often changes
1107 * when slaves change.
1109 nm_device_update_hw_address (self);
1111 /* Restart IP configuration if we're waiting for slaves. Do this
1112 * after updating the hardware address as IP config may need the
1116 if (NM_DEVICE_GET_PRIVATE (self)->ip4_state == IP_WAIT)
1117 nm_device_activate_stage3_ip4_start (self);
1119 if (NM_DEVICE_GET_PRIVATE (self)->ip6_state == IP_WAIT)
1120 nm_device_activate_stage3_ip6_start (self);
1127 * nm_device_master_release_one_slave:
1128 * @self: the master device
1129 * @slave: the slave device to release
1130 * @configure: whether @self needs to actually release @slave
1131 * @reason: the state change reason for the @slave
1133 * If @self is capable of enslaving other devices (ie it's a bridge, bond, team,
1134 * etc) then this function releases the previously enslaved @slave and/or
1135 * updates the state of @self and @slave to reflect its release.
1138 nm_device_master_release_one_slave (NMDevice *self, NMDevice *slave, gboolean configure, NMDeviceStateReason reason)
1140 NMDevicePrivate *priv;
1141 NMDevicePrivate *slave_priv;
1143 gs_unref_object NMDevice *self_free = NULL;
1145 g_return_if_fail (NM_DEVICE (self));
1146 g_return_if_fail (NM_DEVICE (slave));
1147 g_return_if_fail (NM_DEVICE_GET_CLASS (self)->release_slave != NULL);
1149 info = find_slave_info (self, slave);
1151 _LOGt (LOGD_CORE, "master: release one slave %p/%s%s", slave, nm_device_get_iface (slave),
1152 !info ? " (not registered)" : "");
1155 g_return_if_reached ();
1157 priv = NM_DEVICE_GET_PRIVATE (self);
1158 slave_priv = NM_DEVICE_GET_PRIVATE (slave);
1160 g_return_if_fail (self == slave_priv->master);
1161 nm_assert (slave == info->slave);
1163 /* first, let subclasses handle the release ... */
1164 if (info->slave_is_enslaved)
1165 NM_DEVICE_GET_CLASS (self)->release_slave (self, slave, configure);
1167 /* raise notifications about the release, including clearing is_enslaved. */
1168 nm_device_slave_notify_release (slave, reason);
1170 /* keep both alive until the end of the function.
1171 * Transfers ownership from slave_priv->master. */
1174 priv->slaves = g_slist_remove (priv->slaves, info);
1175 slave_priv->master = NULL;
1177 g_signal_handler_disconnect (slave, info->watch_id);
1178 g_object_unref (slave);
1179 g_slice_free (SlaveInfo, info);
1181 /* Ensure the device's hardware address is up-to-date; it often changes
1182 * when slaves change.
1184 nm_device_update_hw_address (self);
1185 nm_device_set_unmanaged_by_flags (slave, NM_UNMANAGED_IS_SLAVE, NM_UNMAN_FLAG_OP_FORGET, NM_DEVICE_STATE_REASON_REMOVED);
1189 * can_unmanaged_external_down:
1192 * Check whether the device should stay NM_UNMANAGED_EXTERNAL_DOWN unless
1193 * IFF_UP-ed externally.
1196 can_unmanaged_external_down (NMDevice *self)
1198 return nm_device_is_software (self) && !NM_DEVICE_GET_PRIVATE (self)->is_nm_owned;
1202 update_dynamic_ip_setup (NMDevice *self)
1204 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1209 g_hash_table_remove_all (priv->ip6_saved_properties);
1211 if (priv->dhcp4_client) {
1212 if (!nm_device_dhcp4_renew (self, FALSE)) {
1213 nm_device_state_changed (self,
1214 NM_DEVICE_STATE_FAILED,
1215 NM_DEVICE_STATE_REASON_DHCP_FAILED);
1219 if (priv->dhcp6_client) {
1220 if (!nm_device_dhcp6_renew (self, FALSE)) {
1221 nm_device_state_changed (self,
1222 NM_DEVICE_STATE_FAILED,
1223 NM_DEVICE_STATE_REASON_DHCP_FAILED);
1230 if (priv->dnsmasq_manager) {
1234 if (priv->lldp_listener && nm_lldp_listener_is_running (priv->lldp_listener)) {
1235 nm_lldp_listener_stop (priv->lldp_listener);
1236 addr = nm_platform_link_get_address (NM_PLATFORM_GET, priv->ifindex, &addr_length);
1238 if (!nm_lldp_listener_start (priv->lldp_listener, nm_device_get_ifindex (self), &error)) {
1239 _LOGD (LOGD_DEVICE, "LLDP listener %p could not be restarted: %s",
1240 priv->lldp_listener, error->message);
1241 g_clear_error (&error);
1247 carrier_changed (NMDevice *self, gboolean carrier)
1249 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1251 if (priv->state <= NM_DEVICE_STATE_UNMANAGED)
1254 nm_device_recheck_available_connections (self);
1256 /* ignore-carrier devices ignore all carrier-down events */
1257 if (priv->ignore_carrier && !carrier)
1260 if (priv->is_master) {
1261 /* Bridge/bond/team carrier does not affect its own activation,
1262 * but when carrier comes on, if there are slaves waiting,
1263 * it will restart them.
1268 if (nm_device_activate_ip4_state_in_wait (self))
1269 nm_device_activate_stage3_ip4_start (self);
1270 if (nm_device_activate_ip6_state_in_wait (self))
1271 nm_device_activate_stage3_ip6_start (self);
1274 } else if (nm_device_get_enslaved (self) && !carrier) {
1275 /* Slaves don't deactivate when they lose carrier; for
1276 * bonds/teams in particular that would be actively
1277 * counterproductive.
1283 if (priv->state == NM_DEVICE_STATE_UNAVAILABLE) {
1284 nm_device_queue_state (self, NM_DEVICE_STATE_DISCONNECTED,
1285 NM_DEVICE_STATE_REASON_CARRIER);
1286 } else if (priv->state == NM_DEVICE_STATE_DISCONNECTED) {
1287 /* If the device is already in DISCONNECTED state without a carrier
1288 * (probably because it is tagged for carrier ignore) ensure that
1289 * when the carrier appears, auto connections are rechecked for
1292 nm_device_emit_recheck_auto_activate (self);
1293 } else if (priv->state == NM_DEVICE_STATE_ACTIVATED) {
1294 /* If the device is active without a carrier (probably because it is
1295 * tagged for carrier ignore) ensure that when the carrier appears we
1296 * renew DHCP leases and such.
1298 update_dynamic_ip_setup (self);
1301 if (priv->state == NM_DEVICE_STATE_UNAVAILABLE) {
1302 if (nm_device_queued_state_peek (self) >= NM_DEVICE_STATE_DISCONNECTED)
1303 nm_device_queued_state_clear (self);
1305 nm_device_queue_state (self, NM_DEVICE_STATE_UNAVAILABLE,
1306 NM_DEVICE_STATE_REASON_CARRIER);
1311 #define LINK_DISCONNECT_DELAY 4
1314 link_disconnect_action_cb (gpointer user_data)
1316 NMDevice *self = NM_DEVICE (user_data);
1317 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1319 _LOGD (LOGD_DEVICE, "link disconnected (calling deferred action) (id=%u)", priv->carrier_defer_id);
1321 priv->carrier_defer_id = 0;
1323 _LOGI (LOGD_DEVICE, "link disconnected (calling deferred action)");
1325 NM_DEVICE_GET_CLASS (self)->carrier_changed (self, FALSE);
1331 link_disconnect_action_cancel (NMDevice *self)
1333 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1335 if (priv->carrier_defer_id) {
1336 g_source_remove (priv->carrier_defer_id);
1337 _LOGD (LOGD_DEVICE, "link disconnected (canceling deferred action) (id=%u)", priv->carrier_defer_id);
1338 priv->carrier_defer_id = 0;
1343 nm_device_set_carrier (NMDevice *self, gboolean carrier)
1345 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1346 NMDeviceClass *klass = NM_DEVICE_GET_CLASS (self);
1347 NMDeviceState state = nm_device_get_state (self);
1349 if (priv->carrier == carrier)
1352 priv->carrier = carrier;
1353 _notify (self, PROP_CARRIER);
1355 if (priv->carrier) {
1356 _LOGI (LOGD_DEVICE, "link connected");
1357 link_disconnect_action_cancel (self);
1358 klass->carrier_changed (self, TRUE);
1360 if (nm_clear_g_source (&priv->carrier_wait_id)) {
1361 nm_device_remove_pending_action (self, "carrier wait", TRUE);
1362 _carrier_wait_check_queued_act_request (self);
1364 } else if (state <= NM_DEVICE_STATE_DISCONNECTED) {
1365 _LOGI (LOGD_DEVICE, "link disconnected");
1366 klass->carrier_changed (self, FALSE);
1368 _LOGI (LOGD_DEVICE, "link disconnected (deferring action for %d seconds)", LINK_DISCONNECT_DELAY);
1369 priv->carrier_defer_id = g_timeout_add_seconds (LINK_DISCONNECT_DELAY,
1370 link_disconnect_action_cb, self);
1371 _LOGD (LOGD_DEVICE, "link disconnected (deferring action for %d seconds) (id=%u)",
1372 LINK_DISCONNECT_DELAY, priv->carrier_defer_id);
1377 device_recheck_slave_status (NMDevice *self, const NMPlatformLink *plink)
1379 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1381 g_return_if_fail (plink);
1383 if (plink->master <= 0)
1387 if ( plink->master > 0
1388 && plink->master == nm_device_get_ifindex (priv->master)) {
1389 /* call add-slave again. We expect @self already to be added to
1390 * the master, but this also triggers a recheck-assume. */
1391 nm_device_master_add_slave (priv->master, self, FALSE);
1395 nm_device_master_release_one_slave (priv->master, self, FALSE, NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
1397 if (plink->master > 0) {
1400 master = nm_manager_get_device_by_ifindex (nm_manager_get (), plink->master);
1401 if (master && NM_DEVICE_GET_CLASS (master)->enslave_slave)
1402 nm_device_master_add_slave (master, self, FALSE);
1404 _LOGI (LOGD_DEVICE, "enslaved to non-master-type device %s; ignoring",
1405 nm_device_get_iface (master));
1407 _LOGW (LOGD_DEVICE, "enslaved to unknown device %d %s",
1409 nm_platform_link_get_name (NM_PLATFORM_GET, plink->master));
1415 device_link_changed (NMDevice *self)
1417 NMDeviceClass *klass = NM_DEVICE_GET_CLASS (self);
1418 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1419 NMUtilsIPv6IfaceId token_iid;
1420 gboolean ip_ifname_changed = FALSE;
1422 NMPlatformLink info;
1423 const NMPlatformLink *pllink;
1427 priv->device_link_changed_id = 0;
1429 ifindex = nm_device_get_ifindex (self);
1430 pllink = nm_platform_link_get (NM_PLATFORM_GET, ifindex);
1432 return G_SOURCE_REMOVE;
1436 udi = nm_platform_link_get_udi (NM_PLATFORM_GET, info.ifindex);
1437 if (udi && g_strcmp0 (udi, priv->udi)) {
1438 /* Update UDI to what udev gives us */
1440 priv->udi = g_strdup (udi);
1441 _notify (self, PROP_UDI);
1444 if (g_strcmp0 (info.driver, priv->driver)) {
1445 /* Update driver to what udev gives us */
1446 g_free (priv->driver);
1447 priv->driver = g_strdup (info.driver);
1448 _notify (self, PROP_DRIVER);
1451 /* Update MTU if it has changed. */
1452 if (priv->mtu != info.mtu) {
1453 priv->mtu = info.mtu;
1454 _notify (self, PROP_MTU);
1457 if (info.driver && g_strcmp0 (priv->driver, info.driver) != 0) {
1458 g_free (priv->driver);
1459 priv->driver = g_strdup (info.driver);
1460 _notify (self, PROP_DRIVER);
1463 if (info.name[0] && strcmp (priv->iface, info.name) != 0) {
1464 _LOGI (LOGD_DEVICE, "interface index %d renamed iface from '%s' to '%s'",
1465 priv->ifindex, priv->iface, info.name);
1466 g_free (priv->iface);
1467 priv->iface = g_strdup (info.name);
1469 /* If the device has no explicit ip_iface, then changing iface changes ip_iface too. */
1470 ip_ifname_changed = !priv->ip_iface;
1472 _notify (self, PROP_IFACE);
1473 if (ip_ifname_changed)
1474 _notify (self, PROP_IP_IFACE);
1476 /* Re-match available connections against the new interface name */
1477 nm_device_recheck_available_connections (self);
1479 /* Let any connections that use the new interface name have a chance
1480 * to auto-activate on the device.
1482 nm_device_emit_recheck_auto_activate (self);
1485 if (priv->rdisc && nm_platform_link_get_ipv6_token (NM_PLATFORM_GET, priv->ifindex, &token_iid)) {
1486 _LOGD (LOGD_DEVICE, "IPv6 tokenized identifier present on device %s", priv->iface);
1487 if (nm_rdisc_set_iid (priv->rdisc, token_iid))
1488 nm_rdisc_start (priv->rdisc);
1491 if (klass->link_changed)
1492 klass->link_changed (self, &info);
1494 /* Update DHCP, etc, if needed */
1495 if (ip_ifname_changed)
1496 update_dynamic_ip_setup (self);
1499 priv->up = NM_FLAGS_HAS (info.n_ifi_flags, IFF_UP);
1501 if ( priv->ifindex > 0
1503 && nm_device_get_unmanaged_flags (self, NM_UNMANAGED_PLATFORM_INIT)) {
1504 NMDeviceStateReason reason;
1506 nm_device_set_unmanaged_by_user_udev (self);
1508 /* If the devices that need an external IFF_UP go managed below,
1509 * it means they're already up. In that case we should use an "assumed"
1510 * reason to prevent the cleanup sequence from being run on transition
1511 * from "unmanaged" to "unavailable". */
1513 && !nm_device_get_unmanaged_flags (self, NM_UNMANAGED_EXTERNAL_DOWN)
1514 && NM_DEVICE_GET_CLASS (self)->can_unmanaged_external_down (self)) {
1515 /* Ensure the assume check is queued before any queued state changes
1516 * from the transition to UNAVAILABLE.
1518 nm_device_queue_recheck_assume (self);
1519 reason = NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED;
1521 reason = NM_DEVICE_STATE_REASON_NOW_MANAGED;
1523 nm_device_set_unmanaged_by_flags (self, NM_UNMANAGED_PLATFORM_INIT, FALSE, reason);
1526 if ( priv->ifindex > 0
1527 && priv->up != was_up
1528 && NM_DEVICE_GET_CLASS (self)->can_unmanaged_external_down (self)) {
1529 /* Manage externally-created software interfaces only when they are IFF_UP */
1531 && nm_device_get_unmanaged_flags (self, NM_UNMANAGED_EXTERNAL_DOWN)) {
1532 /* Ensure the assume check is queued before any queued state changes
1533 * from the transition to UNAVAILABLE.
1535 nm_device_queue_recheck_assume (self);
1538 /* In case of @priv->up, resetting the EXTERNAL_DOWN flag may change the device's
1539 * state to UNAVAILABLE. To ensure that the state change doesn't touch
1540 * the device before assumption occurs, pass NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED
1543 * In case of !@priv->up, and the device is already unmanaged for other reasons, the
1544 * state-change-reason has no effect.
1545 * If the device is managed for an explict user-request, the state-change-reason
1546 * also has no effect, because the device stays managed.
1548 * The state-change-reason only has effect if the device was assumed
1549 * and is now to be unmanaged. */
1550 nm_device_set_unmanaged_by_flags (self,
1551 NM_UNMANAGED_EXTERNAL_DOWN,
1553 NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
1556 device_recheck_slave_status (self, &info);
1557 return G_SOURCE_REMOVE;
1561 device_ip_link_changed (NMDevice *self)
1563 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1564 const NMPlatformLink *pllink;
1566 priv->device_ip_link_changed_id = 0;
1568 if (!priv->ip_ifindex)
1569 return G_SOURCE_REMOVE;
1571 pllink = nm_platform_link_get (NM_PLATFORM_GET, priv->ip_ifindex);
1573 return G_SOURCE_REMOVE;
1575 if (pllink->name[0] && g_strcmp0 (priv->ip_iface, pllink->name)) {
1576 _LOGI (LOGD_DEVICE, "interface index %d renamed ip_iface (%d) from '%s' to '%s'",
1577 priv->ifindex, nm_device_get_ip_ifindex (self),
1578 priv->ip_iface, pllink->name);
1579 g_free (priv->ip_iface);
1580 priv->ip_iface = g_strdup (pllink->name);
1582 _notify (self, PROP_IP_IFACE);
1583 update_dynamic_ip_setup (self);
1585 return G_SOURCE_REMOVE;
1589 link_changed_cb (NMPlatform *platform,
1590 NMPObjectType obj_type,
1592 NMPlatformLink *info,
1593 NMPlatformSignalChangeType change_type,
1596 NMDevicePrivate *priv;
1598 if (change_type != NM_PLATFORM_SIGNAL_CHANGED)
1601 priv = NM_DEVICE_GET_PRIVATE (self);
1603 if (ifindex == nm_device_get_ifindex (self)) {
1604 if (!priv->device_link_changed_id) {
1605 priv->device_link_changed_id = g_idle_add ((GSourceFunc) device_link_changed, self);
1606 _LOGD (LOGD_DEVICE, "queued link change for ifindex %d", ifindex);
1608 } else if (ifindex == nm_device_get_ip_ifindex (self)) {
1609 if (!priv->device_ip_link_changed_id) {
1610 priv->device_ip_link_changed_id = g_idle_add ((GSourceFunc) device_ip_link_changed, self);
1611 _LOGD (LOGD_DEVICE, "queued link change for ip-ifindex %d", ifindex);
1617 link_changed (NMDevice *self, NMPlatformLink *info)
1619 /* Update carrier from link event if applicable. */
1620 if ( nm_device_has_capability (self, NM_DEVICE_CAP_CARRIER_DETECT)
1621 && !nm_device_has_capability (self, NM_DEVICE_CAP_NONSTANDARD_CARRIER))
1622 nm_device_set_carrier (self, info->connected);
1626 link_type_compatible (NMDevice *self,
1627 NMLinkType link_type,
1628 gboolean *out_compatible,
1631 NMDeviceClass *klass;
1632 NMLinkType device_type;
1635 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
1637 klass = NM_DEVICE_GET_CLASS (self);
1639 if (!klass->link_types) {
1640 NM_SET_OUT (out_compatible, FALSE);
1641 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
1642 "Device does not support platform links");
1646 device_type = self->priv->link_type;
1647 if (device_type > NM_LINK_TYPE_UNKNOWN && device_type != link_type) {
1648 g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
1649 "Needed link type 0x%x does not match the platform link type 0x%X",
1650 device_type, link_type);
1654 for (i = 0; klass->link_types[i] > NM_LINK_TYPE_UNKNOWN; i++) {
1655 if (klass->link_types[i] == link_type)
1657 if (klass->link_types[i] == NM_LINK_TYPE_ANY)
1661 NM_SET_OUT (out_compatible, FALSE);
1662 g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
1663 "Device does not support platform link type 0x%X",
1669 * nm_device_realize_start():
1670 * @self: the #NMDevice
1671 * @plink: an existing platform link or %NULL
1672 * @out_compatible: %TRUE on return if @self is compatible with @plink
1673 * @error: location to store error, or %NULL
1675 * Initializes and sets up the device using existing backing resources. Before
1676 * the device is ready for use nm_device_realize_finish() must be called.
1677 * @out_compatible will only be set if @plink is not %NULL, and
1679 * Important: if nm_device_realize_start() returns %TRUE, the caller MUST
1680 * also call nm_device_realize_finish() to balance g_object_freeze_notify().
1682 * Returns: %TRUE on success, %FALSE on error
1685 nm_device_realize_start (NMDevice *self,
1686 const NMPlatformLink *plink,
1687 gboolean *out_compatible,
1690 NM_SET_OUT (out_compatible, TRUE);
1693 if (g_strcmp0 (nm_device_get_iface (self), plink->name) != 0) {
1694 NM_SET_OUT (out_compatible, FALSE);
1695 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
1696 "Device interface name does not match platform link");
1700 if (!link_type_compatible (self, plink->type, out_compatible, error))
1704 realize_start_setup (self, plink);
1710 * nm_device_create_and_realize():
1711 * @self: the #NMDevice
1712 * @connection: the #NMConnection being activated
1713 * @parent: the parent #NMDevice if any
1714 * @error: location to store error, or %NULL
1716 * Creates any backing resources needed to realize the device to proceed
1717 * with activating @connection.
1719 * Returns: %TRUE on success, %FALSE on error
1722 nm_device_create_and_realize (NMDevice *self,
1723 NMConnection *connection,
1727 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1728 NMPlatformLink plink_copy;
1729 const NMPlatformLink *plink = NULL;
1731 /* Must be set before device is realized */
1732 priv->is_nm_owned = !nm_platform_link_get_by_ifname (NM_PLATFORM_GET, priv->iface);
1734 _LOGD (LOGD_DEVICE, "create (is %snm-owned)", priv->is_nm_owned ? "" : "not ");
1736 /* Create any resources the device needs */
1737 if (NM_DEVICE_GET_CLASS (self)->create_and_realize) {
1738 if (!NM_DEVICE_GET_CLASS (self)->create_and_realize (self, connection, parent, &plink, error))
1740 plink_copy = *plink;
1741 plink = &plink_copy;
1744 realize_start_setup (self, plink);
1745 nm_device_realize_finish (self, plink);
1747 if (nm_device_get_managed (self, FALSE)) {
1748 nm_device_state_changed (self,
1749 NM_DEVICE_STATE_UNAVAILABLE,
1750 NM_DEVICE_STATE_REASON_NOW_MANAGED);
1756 update_device_from_platform_link (NMDevice *self, const NMPlatformLink *plink)
1758 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1761 g_return_if_fail (plink != NULL);
1763 udi = nm_platform_link_get_udi (NM_PLATFORM_GET, plink->ifindex);
1764 if (udi && !g_strcmp0 (udi, priv->udi)) {
1766 priv->udi = g_strdup (udi);
1767 _notify (self, PROP_UDI);
1770 if (!g_strcmp0 (plink->name, priv->iface)) {
1771 g_free (priv->iface);
1772 priv->iface = g_strdup (plink->name);
1773 _notify (self, PROP_IFACE);
1776 priv->ifindex = plink->ifindex;
1777 _notify (self, PROP_IFINDEX);
1779 priv->up = NM_FLAGS_HAS (plink->n_ifi_flags, IFF_UP);
1780 if (plink->driver && g_strcmp0 (plink->driver, priv->driver) != 0) {
1781 g_free (priv->driver);
1782 priv->driver = g_strdup (plink->driver);
1783 _notify (self, PROP_DRIVER);
1788 config_changed_update_ignore_carrier (NMConfig *config,
1789 NMConfigData *config_data,
1790 NMConfigChangeFlags changes,
1791 NMConfigData *old_data,
1794 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
1796 if ( priv->state <= NM_DEVICE_STATE_DISCONNECTED
1797 || priv->state > NM_DEVICE_STATE_ACTIVATED)
1798 priv->ignore_carrier = nm_config_data_get_ignore_carrier (config_data, self);
1802 check_carrier (NMDevice *self)
1804 int ifindex = nm_device_get_ip_ifindex (self);
1806 if (!nm_device_has_capability (self, NM_DEVICE_CAP_NONSTANDARD_CARRIER))
1807 nm_device_set_carrier (self, nm_platform_link_is_connected (NM_PLATFORM_GET, ifindex));
1811 realize_start_notify (NMDevice *self, const NMPlatformLink *plink)
1813 /* Stub implementation for realize_start_notify(). It does nothing,
1814 * but allows derived classes to uniformly invoke the parent
1815 * implementation. */
1819 * realize_start_setup():
1820 * @self: the #NMDevice
1821 * @plink: the #NMPlatformLink if backed by a kernel netdevice
1823 * Update the device from backing resource properties (like hardware
1824 * addresses, carrier states, driver/firmware info, etc). This function
1825 * should only change properties for this device, and should not perform
1826 * any tasks that affect other interfaces (like master/slave or parent/child
1830 realize_start_setup (NMDevice *self, const NMPlatformLink *plink)
1832 NMDevicePrivate *priv;
1833 NMDeviceClass *klass;
1834 static guint32 id = 0;
1836 g_return_if_fail (NM_IS_DEVICE (self));
1838 priv = NM_DEVICE_GET_PRIVATE (self);
1840 /* The device should not be realized */
1841 g_return_if_fail (!priv->real);
1842 g_return_if_fail (nm_device_get_unmanaged_flags (self, NM_UNMANAGED_PLATFORM_INIT));
1843 g_return_if_fail (priv->ip_ifindex <= 0);
1844 g_return_if_fail (priv->ip_iface == NULL);
1846 _LOGD (LOGD_DEVICE, "start setup of %s, kernel ifindex %d", G_OBJECT_TYPE_NAME (self), plink ? plink->ifindex : 0);
1848 klass = NM_DEVICE_GET_CLASS (self);
1850 /* Balanced by a thaw in nm_device_realize_finish() */
1851 g_object_freeze_notify (G_OBJECT (self));
1854 g_return_if_fail (link_type_compatible (self, plink->type, NULL, NULL));
1855 update_device_from_platform_link (self, plink);
1858 if (priv->ifindex > 0) {
1859 priv->physical_port_id = nm_platform_link_get_physical_port_id (NM_PLATFORM_GET, priv->ifindex);
1860 _notify (self, PROP_PHYSICAL_PORT_ID);
1862 priv->dev_id = nm_platform_link_get_dev_id (NM_PLATFORM_GET, priv->ifindex);
1864 if (nm_platform_link_is_software (NM_PLATFORM_GET, priv->ifindex))
1865 priv->capabilities |= NM_DEVICE_CAP_IS_SOFTWARE;
1867 priv->mtu = nm_platform_link_get_mtu (NM_PLATFORM_GET, priv->ifindex);
1868 _notify (self, PROP_MTU);
1870 nm_platform_link_get_driver_info (NM_PLATFORM_GET,
1873 &priv->driver_version,
1874 &priv->firmware_version);
1875 if (priv->driver_version)
1876 _notify (self, PROP_DRIVER_VERSION);
1877 if (priv->firmware_version)
1878 _notify (self, PROP_FIRMWARE_VERSION);
1880 if (nm_platform_check_support_user_ipv6ll (NM_PLATFORM_GET))
1881 priv->nm_ipv6ll = nm_platform_link_get_user_ipv6ll_enabled (NM_PLATFORM_GET, priv->ifindex);
1884 if (klass->get_generic_capabilities)
1885 priv->capabilities |= klass->get_generic_capabilities (self);
1888 /* Use a placeholder UDI until we get a real one */
1889 priv->udi = g_strdup_printf ("/virtual/device/placeholder/%d", id++);
1890 _notify (self, PROP_UDI);
1893 /* trigger initial ip config change to initialize ip-config */
1894 priv->queued_ip4_config_id = g_idle_add (queued_ip4_config_change, self);
1895 priv->queued_ip6_config_id = g_idle_add (queued_ip6_config_change, self);
1897 nm_device_update_hw_address (self);
1898 nm_device_update_initial_hw_address (self);
1900 /* Note: initial hardware address must be read before calling get_ignore_carrier() */
1901 if (nm_device_has_capability (self, NM_DEVICE_CAP_CARRIER_DETECT)) {
1902 NMConfig *config = nm_config_get ();
1904 priv->ignore_carrier = nm_config_data_get_ignore_carrier (nm_config_get_data (config), self);
1905 g_signal_connect (G_OBJECT (config),
1906 NM_CONFIG_SIGNAL_CONFIG_CHANGED,
1907 G_CALLBACK (config_changed_update_ignore_carrier),
1910 check_carrier (self);
1913 priv->carrier ? "ON" : "OFF",
1914 priv->ignore_carrier ? " (but ignored)" : "");
1916 /* Fake online link when carrier detection is not available. */
1917 priv->carrier = TRUE;
1920 _notify (self, PROP_CAPABILITIES);
1922 klass->realize_start_notify (self, plink);
1924 /* Do not manage externally created software devices until they are IFF_UP */
1925 if ( priv->ifindex > 0
1928 && NM_DEVICE_GET_CLASS (self)->can_unmanaged_external_down (self))
1929 nm_device_set_unmanaged_flags (self, NM_UNMANAGED_EXTERNAL_DOWN, TRUE);
1931 /* Unmanaged the loopback device with an explicit NM_UNMANAGED_LOOPBACK flag.
1932 * Later we might want to manage 'lo' too. Currently that doesn't work because
1933 * NetworkManager might down the interface or remove the 127.0.0.1 address. */
1934 nm_device_set_unmanaged_flags (self, NM_UNMANAGED_LOOPBACK, priv->ifindex == 1);
1936 nm_device_set_unmanaged_by_user_udev (self);
1938 nm_device_set_unmanaged_flags (self, NM_UNMANAGED_PLATFORM_INIT,
1939 plink && !plink->initialized);
1943 * nm_device_realize_finish():
1944 * @self: the #NMDevice
1945 * @plink: the #NMPlatformLink if backed by a kernel netdevice
1947 * Update the device's master/slave or parent/child relationships from
1948 * backing resource properties. After this function finishes, the device
1949 * is ready for network connectivity.
1952 nm_device_realize_finish (NMDevice *self, const NMPlatformLink *plink)
1954 NMDevicePrivate *priv;
1956 g_return_if_fail (NM_IS_DEVICE (self));
1957 g_return_if_fail (!plink || link_type_compatible (self, plink->type, NULL, NULL));
1959 priv = NM_DEVICE_GET_PRIVATE (self);
1961 g_return_if_fail (!priv->real);
1964 device_recheck_slave_status (self, plink);
1967 _notify (self, PROP_REAL);
1969 nm_device_recheck_available_connections (self);
1971 /* Balanced by a freeze in realize_start_setup(). */
1972 g_object_thaw_notify (G_OBJECT (self));
1976 unrealize_notify (NMDevice *self)
1978 /* Stub implementation for unrealize_notify(). It does nothing,
1979 * but allows derived classes to uniformly invoke the parent
1980 * implementation. */
1984 available_connections_check_delete_unrealized_on_idle (gpointer user_data)
1986 NMDevice *self = user_data;
1987 NMDevicePrivate *priv;
1989 g_return_val_if_fail (NM_IS_DEVICE (self), G_SOURCE_REMOVE);
1991 priv = NM_DEVICE_GET_PRIVATE (self);
1993 priv->check_delete_unrealized_id = 0;
1995 if ( g_hash_table_size (priv->available_connections) == 0
1996 && !nm_device_is_real (self))
1997 g_signal_emit (self, signals[REMOVED], 0);
1999 return G_SOURCE_REMOVE;
2003 available_connections_check_delete_unrealized (NMDevice *self)
2005 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2007 /* always rescheadule the remove signal. */
2008 nm_clear_g_source (&priv->check_delete_unrealized_id);
2010 if ( g_hash_table_size (priv->available_connections) == 0
2011 && !nm_device_is_real (self))
2012 priv->check_delete_unrealized_id = g_idle_add (available_connections_check_delete_unrealized_on_idle, self);
2016 * nm_device_unrealize():
2017 * @self: the #NMDevice
2018 * @remove_resources: if %TRUE, remove backing resources
2019 * @error: location to store error, or %NULL
2021 * Clears any properties that depend on backing resources (kernel devices,
2022 * etc) and removes those resources if @remove_resources is %TRUE.
2024 * Returns: %TRUE on success, %FALSE on error
2027 nm_device_unrealize (NMDevice *self, gboolean remove_resources, GError **error)
2029 NMDevicePrivate *priv;
2032 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
2034 if (!nm_device_is_software (self) || !nm_device_is_real (self)) {
2035 g_set_error_literal (error,
2037 NM_DEVICE_ERROR_NOT_SOFTWARE,
2038 "This device is not a software device or is not realized");
2042 priv = NM_DEVICE_GET_PRIVATE (self);
2044 g_return_val_if_fail (priv->iface != NULL, FALSE);
2045 g_return_val_if_fail (priv->real, FALSE);
2047 g_object_freeze_notify (G_OBJECT (self));
2049 ifindex = nm_device_get_ifindex (self);
2051 _LOGD (LOGD_DEVICE, "unrealize (ifindex %d)", ifindex > 0 ? ifindex : 0);
2053 if (remove_resources) {
2055 nm_platform_link_delete (NM_PLATFORM_GET, ifindex);
2058 NM_DEVICE_GET_CLASS (self)->unrealize_notify (self);
2060 if (priv->ifindex > 0) {
2062 _notify (self, PROP_IFINDEX);
2064 priv->ip_ifindex = 0;
2065 if (priv->ip_iface) {
2066 g_clear_pointer (&priv->ip_iface, g_free);
2067 _notify (self, PROP_IP_IFACE);
2069 if (priv->driver_version) {
2070 g_clear_pointer (&priv->driver_version, g_free);
2071 _notify (self, PROP_DRIVER_VERSION);
2073 if (priv->firmware_version) {
2074 g_clear_pointer (&priv->firmware_version, g_free);
2075 _notify (self, PROP_FIRMWARE_VERSION);
2078 g_clear_pointer (&priv->udi, g_free);
2079 _notify (self, PROP_UDI);
2081 if (priv->hw_addr) {
2082 g_clear_pointer (&priv->hw_addr, g_free);
2083 _notify (self, PROP_HW_ADDRESS);
2085 if (priv->physical_port_id) {
2086 g_clear_pointer (&priv->physical_port_id, g_free);
2087 _notify (self, PROP_PHYSICAL_PORT_ID);
2090 g_clear_pointer (&priv->perm_hw_addr, g_free);
2091 g_clear_pointer (&priv->initial_hw_addr, g_free);
2093 priv->capabilities = NM_DEVICE_CAP_NM_SUPPORTED;
2094 if (NM_DEVICE_GET_CLASS (self)->get_generic_capabilities)
2095 priv->capabilities |= NM_DEVICE_GET_CLASS (self)->get_generic_capabilities (self);
2096 _notify (self, PROP_CAPABILITIES);
2099 _notify (self, PROP_REAL);
2101 nm_device_set_autoconnect (self, DEFAULT_AUTOCONNECT);
2103 g_object_thaw_notify (G_OBJECT (self));
2105 nm_device_set_unmanaged_flags (self,
2106 NM_UNMANAGED_PLATFORM_INIT,
2109 nm_device_set_unmanaged_flags (self,
2110 NM_UNMANAGED_PARENT |
2111 NM_UNMANAGED_LOOPBACK |
2112 NM_UNMANAGED_USER_UDEV |
2113 NM_UNMANAGED_EXTERNAL_DOWN |
2114 NM_UNMANAGED_IS_SLAVE,
2115 NM_UNMAN_FLAG_OP_FORGET);
2117 nm_device_state_changed (self,
2118 NM_DEVICE_STATE_UNMANAGED,
2120 NM_DEVICE_STATE_REASON_USER_REQUESTED : NM_DEVICE_STATE_REASON_NOW_UNMANAGED);
2122 /* Garbage-collect unneeded unrealized devices. */
2123 nm_device_recheck_available_connections (self);
2129 * nm_device_notify_new_device_added():
2130 * @self: the #NMDevice
2131 * @device: the newly added device
2133 * Called by the manager to notify the device that a new device has
2134 * been found and added.
2137 nm_device_notify_new_device_added (NMDevice *self, NMDevice *device)
2139 NMDeviceClass *klass;
2141 g_return_if_fail (NM_IS_DEVICE (self));
2142 g_return_if_fail (NM_IS_DEVICE (device));
2144 klass = NM_DEVICE_GET_CLASS (self);
2145 if (klass->notify_new_device_added)
2146 klass->notify_new_device_added (self, device);
2150 * nm_device_notify_component_added():
2151 * @self: the #NMDevice
2152 * @component: the component being added by a plugin
2154 * Called by the manager to notify the device that a new component has
2155 * been found. The device implementation should return %TRUE if it
2156 * wishes to claim the component, or %FALSE if it cannot.
2158 * Returns: %TRUE to claim the component, %FALSE if the component cannot be
2162 nm_device_notify_component_added (NMDevice *self, GObject *component)
2164 NMDeviceClass *klass;
2166 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
2167 g_return_val_if_fail (G_IS_OBJECT (component), FALSE);
2169 klass = NM_DEVICE_GET_CLASS (self);
2170 if (klass->component_added)
2171 return klass->component_added (self, component);
2176 * nm_device_owns_iface():
2177 * @self: the #NMDevice
2178 * @iface: an interface name
2180 * Called by the manager to ask if the device or any of its components owns
2181 * @iface. For example, a WWAN implementation would return %TRUE for an
2182 * ethernet interface name that was owned by the WWAN device's modem component,
2183 * because that ethernet interface is controlled by the WWAN device and cannot
2184 * be used independently of the WWAN device.
2186 * Returns: %TRUE if @self or it's components owns the interface name,
2190 nm_device_owns_iface (NMDevice *self, const char *iface)
2192 if (NM_DEVICE_GET_CLASS (self)->owns_iface)
2193 return NM_DEVICE_GET_CLASS (self)->owns_iface (self, iface);
2198 nm_device_new_default_connection (NMDevice *self)
2200 if (NM_DEVICE_GET_CLASS (self)->new_default_connection)
2201 return NM_DEVICE_GET_CLASS (self)->new_default_connection (self);
2206 slave_state_changed (NMDevice *slave,
2207 NMDeviceState slave_new_state,
2208 NMDeviceState slave_old_state,
2209 NMDeviceStateReason reason,
2212 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2213 gboolean release = FALSE;
2215 _LOGD (LOGD_DEVICE, "slave %s state change %d (%s) -> %d (%s)",
2216 nm_device_get_iface (slave),
2218 state_to_string (slave_old_state),
2220 state_to_string (slave_new_state));
2222 /* Don't try to enslave slaves until the master is ready */
2223 if (priv->state < NM_DEVICE_STATE_CONFIG)
2226 if (slave_new_state == NM_DEVICE_STATE_IP_CONFIG)
2227 nm_device_master_enslave_slave (self, slave, nm_device_get_applied_connection (slave));
2228 else if (slave_new_state > NM_DEVICE_STATE_ACTIVATED)
2230 else if ( slave_new_state <= NM_DEVICE_STATE_DISCONNECTED
2231 && slave_old_state > NM_DEVICE_STATE_DISCONNECTED) {
2232 /* Catch failures due to unavailable or unmanaged */
2237 nm_device_master_release_one_slave (self, slave, TRUE, reason);
2238 /* Bridge/bond/team interfaces are left up until manually deactivated */
2239 if (priv->slaves == NULL && priv->state == NM_DEVICE_STATE_ACTIVATED)
2240 _LOGD (LOGD_DEVICE, "last slave removed; remaining activated");
2245 * nm_device_master_add_slave:
2246 * @self: the master device
2247 * @slave: the slave device to enslave
2248 * @configure: pass %TRUE if the slave should be configured by the master, or
2249 * %FALSE if it is already configured outside NetworkManager
2251 * If @self is capable of enslaving other devices (ie it's a bridge, bond, team,
2252 * etc) then this function adds @slave to the slave list for later enslavement.
2255 nm_device_master_add_slave (NMDevice *self, NMDevice *slave, gboolean configure)
2257 NMDevicePrivate *priv;
2258 NMDevicePrivate *slave_priv;
2261 g_return_if_fail (NM_IS_DEVICE (self));
2262 g_return_if_fail (NM_IS_DEVICE (slave));
2263 g_return_if_fail (NM_DEVICE_GET_CLASS (self)->enslave_slave != NULL);
2265 priv = NM_DEVICE_GET_PRIVATE (self);
2266 slave_priv = NM_DEVICE_GET_PRIVATE (slave);
2268 info = find_slave_info (self, slave);
2270 _LOGt (LOGD_CORE, "master: add one slave %p/%s%s", slave, nm_device_get_iface (slave),
2271 info ? " (already registered)" : "");
2274 g_return_if_fail (nm_device_get_state (slave) >= NM_DEVICE_STATE_DISCONNECTED);
2277 g_return_if_fail (!slave_priv->master);
2278 g_return_if_fail (!slave_priv->is_enslaved);
2280 info = g_slice_new0 (SlaveInfo);
2281 info->slave = g_object_ref (slave);
2282 info->configure = configure;
2283 info->watch_id = g_signal_connect (slave,
2284 NM_DEVICE_STATE_CHANGED,
2285 G_CALLBACK (slave_state_changed), self);
2286 priv->slaves = g_slist_append (priv->slaves, info);
2287 slave_priv->master = g_object_ref (self);
2291 * _notify (slave, PROP_MASTER);
2293 * because slave_priv->is_enslaved is not true, thus the value
2294 * didn't change yet. */
2296 g_warn_if_fail (!NM_FLAGS_HAS (slave_priv->unmanaged_mask, NM_UNMANAGED_IS_SLAVE));
2297 nm_device_set_unmanaged_by_flags (slave, NM_UNMANAGED_IS_SLAVE, FALSE, NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
2299 g_return_if_fail (slave_priv->master == self);
2301 nm_device_queue_recheck_assume (self);
2302 nm_device_queue_recheck_assume (slave);
2307 * nm_device_master_get_slaves:
2308 * @self: the master device
2310 * Returns: any slaves of which @self is the master. Caller owns returned list.
2313 nm_device_master_get_slaves (NMDevice *self)
2315 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2316 GSList *slaves = NULL, *iter;
2318 for (iter = priv->slaves; iter; iter = g_slist_next (iter))
2319 slaves = g_slist_prepend (slaves, ((SlaveInfo *) iter->data)->slave);
2325 * nm_device_master_get_slave_by_ifindex:
2326 * @self: the master device
2327 * @ifindex: the slave's interface index
2329 * Returns: the slave with the given @ifindex of which @self is the master,
2330 * or %NULL if no device with @ifindex is a slave of @self.
2333 nm_device_master_get_slave_by_ifindex (NMDevice *self, int ifindex)
2337 for (iter = NM_DEVICE_GET_PRIVATE (self)->slaves; iter; iter = g_slist_next (iter)) {
2338 SlaveInfo *info = iter->data;
2340 if (nm_device_get_ip_ifindex (info->slave) == ifindex)
2347 * nm_device_master_check_slave_physical_port:
2348 * @self: the master device
2349 * @slave: a slave device
2350 * @log_domain: domain to log a warning in
2352 * Checks if @self already has a slave with the same #NMDevice:physical-port-id
2353 * as @slave, and logs a warning if so.
2356 nm_device_master_check_slave_physical_port (NMDevice *self, NMDevice *slave,
2357 NMLogDomain log_domain)
2359 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2360 const char *slave_physical_port_id, *existing_physical_port_id;
2364 slave_physical_port_id = nm_device_get_physical_port_id (slave);
2365 if (!slave_physical_port_id)
2368 for (iter = priv->slaves; iter; iter = iter->next) {
2370 if (info->slave == slave)
2373 existing_physical_port_id = nm_device_get_physical_port_id (info->slave);
2374 if (!g_strcmp0 (slave_physical_port_id, existing_physical_port_id)) {
2375 _LOGW (log_domain, "slave %s shares a physical port with existing slave %s",
2376 nm_device_get_ip_iface (slave),
2377 nm_device_get_ip_iface (info->slave));
2378 /* Since this function will get called for every slave, we only have
2379 * to warn about the first match we find; if there are other matches
2380 * later in the list, we will have already warned about them matching
2381 * @existing earlier.
2388 /* release all slaves */
2390 nm_device_master_release_slaves (NMDevice *self)
2392 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2393 NMDeviceStateReason reason;
2394 gboolean configure = TRUE;
2396 /* Don't release the slaves if this connection doesn't belong to NM. */
2397 if (nm_device_uses_generated_assumed_connection (self))
2400 reason = priv->state_reason;
2401 if (priv->state == NM_DEVICE_STATE_FAILED)
2402 reason = NM_DEVICE_STATE_REASON_DEPENDENCY_FAILED;
2404 if (!nm_platform_link_get (NM_PLATFORM_GET, priv->ifindex))
2407 while (priv->slaves) {
2408 SlaveInfo *info = priv->slaves->data;
2410 nm_device_master_release_one_slave (self, info->slave, configure, reason);
2415 * nm_device_is_master:
2418 * Returns: %TRUE if the device can have slaves
2421 nm_device_is_master (NMDevice *self)
2423 return NM_DEVICE_GET_PRIVATE (self)->is_master;
2427 * nm_device_get_master:
2430 * If @self has been enslaved by another device, this returns that
2431 * device. Otherwise it returns %NULL. (In particular, note that if
2432 * @self is in the process of activating as a slave, but has not yet
2433 * been enslaved by its master, this will return %NULL.)
2435 * Returns: (transfer none): @self's master, or %NULL
2438 nm_device_get_master (NMDevice *self)
2440 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2442 if (priv->is_enslaved) {
2443 g_return_val_if_fail (priv->master, NULL);
2444 return priv->master;
2450 * nm_device_slave_notify_enslave:
2451 * @self: the slave device
2452 * @success: whether the enslaving operation succeeded
2454 * Notifies a slave that either it has been enslaved, or else its master tried
2455 * to enslave it and failed.
2458 nm_device_slave_notify_enslave (NMDevice *self, gboolean success)
2460 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2461 NMConnection *connection = nm_device_get_applied_connection (self);
2462 gboolean activating = (priv->state == NM_DEVICE_STATE_IP_CONFIG);
2464 g_return_if_fail (priv->master);
2466 if (!priv->is_enslaved) {
2469 _LOGI (LOGD_DEVICE, "Activation: connection '%s' enslaved, continuing activation",
2470 nm_connection_get_id (connection));
2472 _LOGI (LOGD_DEVICE, "enslaved to %s", nm_device_get_iface (priv->master));
2474 priv->is_enslaved = TRUE;
2475 _notify (self, PROP_MASTER);
2476 _notify (priv->master, PROP_SLAVES);
2477 } else if (activating) {
2478 _LOGW (LOGD_DEVICE, "Activation: connection '%s' could not be enslaved",
2479 nm_connection_get_id (connection));
2484 priv->ip4_state = IP_DONE;
2485 priv->ip6_state = IP_DONE;
2487 nm_device_queue_state (self, NM_DEVICE_STATE_SECONDARIES, NM_DEVICE_STATE_REASON_NONE);
2489 nm_device_queue_state (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_UNKNOWN);
2491 nm_device_queue_recheck_assume (self);
2495 * nm_device_slave_notify_release:
2496 * @self: the slave device
2497 * @reason: the reason associated with the state change
2499 * Notifies a slave that it has been released, and why.
2502 nm_device_slave_notify_release (NMDevice *self, NMDeviceStateReason reason)
2504 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2505 NMConnection *connection = nm_device_get_applied_connection (self);
2506 NMDeviceState new_state;
2507 const char *master_status;
2509 g_return_if_fail (priv->master);
2511 if ( priv->state > NM_DEVICE_STATE_DISCONNECTED
2512 && priv->state <= NM_DEVICE_STATE_ACTIVATED) {
2513 if (reason == NM_DEVICE_STATE_REASON_DEPENDENCY_FAILED) {
2514 new_state = NM_DEVICE_STATE_FAILED;
2515 master_status = "failed";
2516 } else if (reason == NM_DEVICE_STATE_REASON_USER_REQUESTED) {
2517 new_state = NM_DEVICE_STATE_DEACTIVATING;
2518 master_status = "deactivated by user request";
2520 new_state = NM_DEVICE_STATE_DISCONNECTED;
2521 master_status = "deactivated";
2524 _LOGD (LOGD_DEVICE, "Activation: connection '%s' master %s",
2525 nm_connection_get_id (connection),
2528 nm_device_queue_state (self, new_state, reason);
2530 _LOGI (LOGD_DEVICE, "released from master device %s", nm_device_get_iface (priv->master));
2532 if (priv->is_enslaved) {
2533 priv->is_enslaved = FALSE;
2534 _notify (self, PROP_MASTER);
2535 _notify (priv->master, PROP_SLAVES);
2540 * nm_device_get_enslaved:
2541 * @self: the #NMDevice
2543 * Returns: %TRUE if the device is enslaved to a master device (eg bridge or
2544 * bond or team), %FALSE if not
2547 nm_device_get_enslaved (NMDevice *self)
2549 return NM_DEVICE_GET_PRIVATE (self)->is_enslaved;
2553 * nm_device_removed:
2554 * @self: the #NMDevice
2556 * Called by the manager when the device was removed. Releases the device from
2557 * the master in case it's enslaved.
2560 nm_device_removed (NMDevice *self)
2562 NMDevicePrivate *priv;
2564 g_return_if_fail (NM_IS_DEVICE (self));
2566 priv = NM_DEVICE_GET_PRIVATE (self);
2568 /* this is called when something externally messes with the slave or during shut-down.
2569 * Release the slave from master, but don't touch the device. */
2570 nm_device_master_release_one_slave (priv->master, self, FALSE, NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
2575 is_available (NMDevice *self, NMDeviceCheckDevAvailableFlags flags)
2577 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2579 if (priv->carrier || priv->ignore_carrier)
2582 if (NM_FLAGS_HAS (flags, _NM_DEVICE_CHECK_DEV_AVAILABLE_IGNORE_CARRIER))
2589 * nm_device_is_available:
2590 * @self: the #NMDevice
2591 * @flags: additional flags to influence the check. Flags have the
2592 * meaning to increase the availability of a device.
2594 * Checks if @self would currently be capable of activating a
2595 * connection. In particular, it checks that the device is ready (eg,
2596 * is not missing firmware), that it has carrier (if necessary), and
2597 * that any necessary external software (eg, ModemManager,
2598 * wpa_supplicant) is available.
2600 * @self can only be in a state higher than
2601 * %NM_DEVICE_STATE_UNAVAILABLE when nm_device_is_available() returns
2602 * %TRUE. (But note that it can still be %NM_DEVICE_STATE_UNMANAGED
2603 * when it is available.)
2605 * Returns: %TRUE or %FALSE
2608 nm_device_is_available (NMDevice *self, NMDeviceCheckDevAvailableFlags flags)
2610 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2612 if (priv->firmware_missing)
2615 return NM_DEVICE_GET_CLASS (self)->is_available (self, flags);
2619 nm_device_get_enabled (NMDevice *self)
2621 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
2623 if (NM_DEVICE_GET_CLASS (self)->get_enabled)
2624 return NM_DEVICE_GET_CLASS (self)->get_enabled (self);
2629 nm_device_set_enabled (NMDevice *self, gboolean enabled)
2631 g_return_if_fail (NM_IS_DEVICE (self));
2633 if (NM_DEVICE_GET_CLASS (self)->set_enabled)
2634 NM_DEVICE_GET_CLASS (self)->set_enabled (self, enabled);
2638 * nm_device_get_autoconnect:
2639 * @self: the #NMDevice
2641 * Returns: %TRUE if the device allows autoconnect connections, or %FALSE if the
2642 * device is explicitly blocking all autoconnect connections. Does not take
2643 * into account transient conditions like companion devices that may wish to
2647 nm_device_get_autoconnect (NMDevice *self)
2649 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
2651 return NM_DEVICE_GET_PRIVATE (self)->autoconnect;
2655 nm_device_set_autoconnect (NMDevice *self, gboolean autoconnect)
2657 NMDevicePrivate *priv;
2659 g_return_if_fail (NM_IS_DEVICE (self));
2661 autoconnect = !!autoconnect;
2663 priv = NM_DEVICE_GET_PRIVATE (self);
2664 if (priv->autoconnect != autoconnect) {
2665 priv->autoconnect = autoconnect;
2666 _notify (self, PROP_AUTOCONNECT);
2671 autoconnect_allowed_accumulator (GSignalInvocationHint *ihint,
2672 GValue *return_accu,
2673 const GValue *handler_return, gpointer data)
2675 if (!g_value_get_boolean (handler_return))
2676 g_value_set_boolean (return_accu, FALSE);
2681 * nm_device_autoconnect_allowed:
2682 * @self: the #NMDevice
2684 * Returns: %TRUE if the device can be auto-connected immediately, taking
2685 * transient conditions into account (like companion devices that may wish to
2686 * block autoconnect for a time).
2689 nm_device_autoconnect_allowed (NMDevice *self)
2691 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2692 GValue instance = G_VALUE_INIT;
2693 GValue retval = G_VALUE_INIT;
2695 if (!priv->autoconnect)
2698 /* Unrealized devices can always autoconnect. */
2699 if (nm_device_is_real (self) && priv->state < NM_DEVICE_STATE_DISCONNECTED)
2702 /* The 'autoconnect-allowed' signal is emitted on a device to allow
2703 * other listeners to block autoconnect on the device if they wish.
2704 * This is mainly used by the OLPC Mesh devices to block autoconnect
2705 * on their companion WiFi device as they share radio resources and
2706 * cannot be connected at the same time.
2709 g_value_init (&instance, G_TYPE_OBJECT);
2710 g_value_set_object (&instance, self);
2712 g_value_init (&retval, G_TYPE_BOOLEAN);
2713 g_value_set_boolean (&retval, TRUE);
2715 /* Use g_signal_emitv() rather than g_signal_emit() to avoid the return
2716 * value being changed if no handlers are connected */
2717 g_signal_emitv (&instance, signals[AUTOCONNECT_ALLOWED], 0, &retval);
2718 g_value_unset (&instance);
2720 return g_value_get_boolean (&retval);
2724 can_auto_connect (NMDevice *self,
2725 NMConnection *connection,
2726 char **specific_object)
2728 NMSettingConnection *s_con;
2730 s_con = nm_connection_get_setting_connection (connection);
2731 if (!nm_setting_connection_get_autoconnect (s_con))
2734 return nm_device_check_connection_available (self, connection, NM_DEVICE_CHECK_CON_AVAILABLE_NONE, NULL);
2738 * nm_device_can_auto_connect:
2739 * @self: an #NMDevice
2740 * @connection: a #NMConnection
2741 * @specific_object: (out) (transfer full): on output, the path of an
2742 * object associated with the returned connection, to be passed to
2743 * nm_manager_activate_connection(), or %NULL.
2745 * Checks if @connection can be auto-activated on @self right now.
2746 * This requires, at a minimum, that the connection be compatible with
2747 * @self, and that it have the #NMSettingConnection:autoconnect property
2748 * set, and that the device allow auto connections. Some devices impose
2749 * additional requirements. (Eg, a Wi-Fi connection can only be activated
2750 * if its SSID was seen in the last scan.)
2752 * Returns: %TRUE, if the @connection can be auto-activated.
2755 nm_device_can_auto_connect (NMDevice *self,
2756 NMConnection *connection,
2757 char **specific_object)
2759 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
2760 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
2761 g_return_val_if_fail (specific_object && !*specific_object, FALSE);
2763 if (nm_device_autoconnect_allowed (self))
2764 return NM_DEVICE_GET_CLASS (self)->can_auto_connect (self, connection, specific_object);
2769 device_has_config (NMDevice *self)
2771 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2773 /* Check for IP configuration. */
2774 if (priv->ip4_config && nm_ip4_config_get_num_addresses (priv->ip4_config))
2776 if (priv->ip6_config && nm_ip6_config_get_num_addresses (priv->ip6_config))
2779 /* The existence of a software device is good enough. */
2780 if (nm_device_is_software (self) && nm_device_is_real (self))
2783 /* Slaves are also configured by definition */
2784 if (nm_platform_link_get_master (NM_PLATFORM_GET, priv->ifindex) > 0)
2791 * nm_device_master_update_slave_connection:
2792 * @self: the master #NMDevice
2793 * @slave: the slave #NMDevice
2794 * @connection: the #NMConnection to update with the slave settings
2795 * @GError: (out): error description
2797 * Reads the slave configuration for @slave and updates @connection with those
2798 * properties. This invokes a virtual function on the master device @self.
2800 * Returns: %TRUE if the configuration was read and @connection updated,
2801 * %FALSE on failure.
2804 nm_device_master_update_slave_connection (NMDevice *self,
2806 NMConnection *connection,
2809 NMDeviceClass *klass;
2812 g_return_val_if_fail (self, FALSE);
2813 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
2814 g_return_val_if_fail (slave, FALSE);
2815 g_return_val_if_fail (connection, FALSE);
2816 g_return_val_if_fail (!error || !*error, FALSE);
2817 g_return_val_if_fail (nm_connection_get_setting_connection (connection), FALSE);
2819 g_return_val_if_fail (nm_device_get_iface (self), FALSE);
2821 klass = NM_DEVICE_GET_CLASS (self);
2822 if (klass->master_update_slave_connection) {
2823 success = klass->master_update_slave_connection (self, slave, connection, error);
2825 g_return_val_if_fail (!error || (success && !*error) || *error, success);
2831 NM_DEVICE_ERROR_FAILED,
2832 "master device '%s' cannot update a slave connection for slave device '%s' (master type not supported?)",
2833 nm_device_get_iface (self), nm_device_get_iface (slave));
2838 nm_device_generate_connection (NMDevice *self, NMDevice *master)
2840 NMDeviceClass *klass = NM_DEVICE_GET_CLASS (self);
2841 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
2842 const char *ifname = nm_device_get_iface (self);
2843 NMConnection *connection;
2847 gs_free char *uuid = NULL;
2848 const char *ip4_method, *ip6_method;
2849 GError *error = NULL;
2851 /* If update_connection() is not implemented, just fail. */
2852 if (!klass->update_connection)
2855 /* Return NULL if device is unconfigured. */
2856 if (!device_has_config (self)) {
2857 _LOGD (LOGD_DEVICE, "device has no existing configuration");
2861 connection = nm_simple_connection_new ();
2862 s_con = nm_setting_connection_new ();
2863 uuid = nm_utils_uuid_generate ();
2865 g_object_set (s_con,
2866 NM_SETTING_CONNECTION_UUID, uuid,
2867 NM_SETTING_CONNECTION_ID, ifname,
2868 NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
2869 NM_SETTING_CONNECTION_INTERFACE_NAME, ifname,
2870 NM_SETTING_CONNECTION_TIMESTAMP, (guint64) time (NULL),
2872 if (klass->connection_type)
2873 g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, klass->connection_type, NULL);
2874 nm_connection_add_setting (connection, s_con);
2876 /* If the device is a slave, update various slave settings */
2878 if (!nm_device_master_update_slave_connection (master,
2883 _LOGE (LOGD_DEVICE, "master device '%s' failed to update slave connection: %s",
2884 nm_device_get_iface (master), error->message);
2885 g_error_free (error);
2886 g_object_unref (connection);
2890 /* Only regular and master devices get IP configuration; slaves do not */
2891 s_ip4 = nm_ip4_config_create_setting (priv->ip4_config);
2892 nm_connection_add_setting (connection, s_ip4);
2894 s_ip6 = nm_ip6_config_create_setting (priv->ip6_config);
2895 nm_connection_add_setting (connection, s_ip6);
2898 klass->update_connection (self, connection);
2900 /* Check the connection in case of update_connection() bug. */
2901 if (!nm_connection_verify (connection, &error)) {
2902 _LOGE (LOGD_DEVICE, "Generated connection does not verify: %s", error->message);
2903 g_clear_error (&error);
2904 g_object_unref (connection);
2908 /* Ignore the connection if it has no IP configuration,
2909 * no slave configuration, and is not a master interface.
2911 ip4_method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG);
2912 ip6_method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG);
2913 if ( g_strcmp0 (ip4_method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0
2914 && g_strcmp0 (ip6_method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0
2915 && !nm_setting_connection_get_master (NM_SETTING_CONNECTION (s_con))
2917 _LOGD (LOGD_DEVICE, "ignoring generated connection (no IP and not in master-slave relationship)");
2918 g_object_unref (connection);
2922 /* Ignore any IPv6LL-only, not master connections without slaves,
2923 * unless they are in the assume-ipv6ll-only list.
2926 && g_strcmp0 (ip4_method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0
2927 && g_strcmp0 (ip6_method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0
2928 && !nm_setting_connection_get_master (NM_SETTING_CONNECTION (s_con))
2930 && !nm_config_data_get_assume_ipv6ll_only (NM_CONFIG_GET_DATA, self)) {
2931 _LOGD (LOGD_DEVICE, "ignoring generated connection (IPv6LL-only and not in master-slave relationship)");
2932 g_object_unref (connection);
2940 nm_device_complete_connection (NMDevice *self,
2941 NMConnection *connection,
2942 const char *specific_object,
2943 const GSList *existing_connections,
2946 gboolean success = FALSE;
2948 g_return_val_if_fail (self != NULL, FALSE);
2949 g_return_val_if_fail (connection != NULL, FALSE);
2951 if (!NM_DEVICE_GET_CLASS (self)->complete_connection) {
2952 g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INVALID_CONNECTION,
2953 "Device class %s had no complete_connection method",
2954 G_OBJECT_TYPE_NAME (self));
2958 success = NM_DEVICE_GET_CLASS (self)->complete_connection (self,
2961 existing_connections,
2964 success = nm_connection_verify (connection, error);
2970 check_connection_compatible (NMDevice *self, NMConnection *connection)
2972 const char *device_iface = nm_device_get_iface (self);
2973 gs_free char *conn_iface = nm_manager_get_connection_iface (nm_manager_get (),
2977 /* We always need a interface name for virtual devices, but for
2978 * physical ones a connection without interface name is fine for
2981 return !nm_connection_is_virtual (connection);
2983 if (strcmp (conn_iface, device_iface) != 0)
2990 * nm_device_check_connection_compatible:
2991 * @self: an #NMDevice
2992 * @connection: an #NMConnection
2994 * Checks if @connection could potentially be activated on @self.
2995 * This means only that @self has the proper capabilities, and that
2996 * @connection is not locked to some other device. It does not
2997 * necessarily mean that @connection could be activated on @self
2998 * right now. (Eg, it might refer to a Wi-Fi network that is not
2999 * currently available.)
3001 * Returns: #TRUE if @connection could potentially be activated on
3005 nm_device_check_connection_compatible (NMDevice *self, NMConnection *connection)
3007 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
3008 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
3010 return NM_DEVICE_GET_CLASS (self)->check_connection_compatible (self, connection);
3014 nm_device_check_slave_connection_compatible (NMDevice *self, NMConnection *slave)
3016 NMDevicePrivate *priv;
3017 NMSettingConnection *s_con;
3018 const char *connection_type, *slave_type;
3020 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
3021 g_return_val_if_fail (NM_IS_CONNECTION (slave), FALSE);
3023 priv = NM_DEVICE_GET_PRIVATE (self);
3025 if (!priv->is_master)
3028 /* All masters should have connection type set */
3029 connection_type = NM_DEVICE_GET_CLASS (self)->connection_type;
3030 g_return_val_if_fail (connection_type, FALSE);
3032 s_con = nm_connection_get_setting_connection (slave);
3034 slave_type = nm_setting_connection_get_slave_type (s_con);
3038 return strcmp (connection_type, slave_type) == 0;
3042 * nm_device_can_assume_connections:
3043 * @self: #NMDevice instance
3045 * This is a convenience function to determine whether connection assumption
3046 * is available for this device.
3048 * Returns: %TRUE if the device is capable of assuming connections, %FALSE if not
3051 nm_device_can_assume_connections (NMDevice *self)
3053 return !!NM_DEVICE_GET_CLASS (self)->update_connection
3054 && !NM_DEVICE_GET_PRIVATE (self)->is_nm_owned;
3058 * nm_device_can_assume_active_connection:
3059 * @self: #NMDevice instance
3061 * This is a convenience function to determine whether the device's active
3062 * connection can be assumed if NetworkManager restarts. This method returns
3063 * %TRUE if and only if the device can assume connections, and the device has
3064 * an active connection, and that active connection can be assumed.
3066 * Returns: %TRUE if the device's active connection can be assumed, or %FALSE
3067 * if there is no active connection or the active connection cannot be
3071 nm_device_can_assume_active_connection (NMDevice *self)
3073 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3074 NMConnection *connection;
3076 const char *assumable_ip6_methods[] = {
3077 NM_SETTING_IP6_CONFIG_METHOD_IGNORE,
3078 NM_SETTING_IP6_CONFIG_METHOD_AUTO,
3079 NM_SETTING_IP6_CONFIG_METHOD_DHCP,
3080 NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL,
3081 NM_SETTING_IP6_CONFIG_METHOD_MANUAL,
3084 const char *assumable_ip4_methods[] = {
3085 NM_SETTING_IP4_CONFIG_METHOD_DISABLED,
3086 NM_SETTING_IP6_CONFIG_METHOD_AUTO,
3087 NM_SETTING_IP6_CONFIG_METHOD_MANUAL,
3091 if (!nm_device_can_assume_connections (self))
3094 connection = nm_device_get_applied_connection (self);
3098 /* Can't assume connections that aren't yet configured
3099 * FIXME: what about bridges/bonds waiting for slaves?
3101 if (priv->state < NM_DEVICE_STATE_IP_CONFIG)
3103 if (priv->ip4_state != IP_DONE && priv->ip6_state != IP_DONE)
3106 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG);
3107 if (!_nm_utils_string_in_list (method, assumable_ip6_methods))
3110 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG);
3111 if (!_nm_utils_string_in_list (method, assumable_ip4_methods))
3118 nm_device_emit_recheck_assume (gpointer user_data)
3120 NMDevice *self = user_data;
3121 NMDevicePrivate *priv;
3123 g_return_val_if_fail (NM_IS_DEVICE (self), G_SOURCE_REMOVE);
3125 priv = NM_DEVICE_GET_PRIVATE (self);
3127 priv->recheck_assume_id = 0;
3128 if (!nm_device_get_act_request (self)) {
3129 _LOGD (LOGD_DEVICE, "emit RECHECK_ASSUME signal");
3130 g_signal_emit (self, signals[RECHECK_ASSUME], 0);
3132 return G_SOURCE_REMOVE;
3136 nm_device_queue_recheck_assume (NMDevice *self)
3138 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3140 if ( !priv->recheck_assume_id
3141 && nm_device_can_assume_connections (self))
3142 priv->recheck_assume_id = g_idle_add (nm_device_emit_recheck_assume, self);
3146 recheck_available (gpointer user_data)
3148 NMDevice *self = NM_DEVICE (user_data);
3149 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3150 gboolean now_available = nm_device_is_available (self, NM_DEVICE_CHECK_DEV_AVAILABLE_NONE);
3151 NMDeviceState state = nm_device_get_state (self);
3152 NMDeviceState new_state = NM_DEVICE_STATE_UNKNOWN;
3154 priv->recheck_available.call_id = 0;
3156 if (state == NM_DEVICE_STATE_UNAVAILABLE && now_available) {
3157 new_state = NM_DEVICE_STATE_DISCONNECTED;
3158 nm_device_queue_state (self, new_state, priv->recheck_available.available_reason);
3159 } else if (state >= NM_DEVICE_STATE_DISCONNECTED && !now_available) {
3160 new_state = NM_DEVICE_STATE_UNAVAILABLE;
3161 nm_device_queue_state (self, new_state, priv->recheck_available.unavailable_reason);
3164 if (new_state > NM_DEVICE_STATE_UNKNOWN) {
3165 _LOGD (LOGD_DEVICE, "is %savailable, %s %s",
3166 now_available ? "" : "not ",
3167 new_state == NM_DEVICE_STATE_UNAVAILABLE ? "no change required for" : "will transition to",
3168 state_to_string (new_state == NM_DEVICE_STATE_UNAVAILABLE ? state : new_state));
3170 priv->recheck_available.available_reason = NM_DEVICE_STATE_REASON_NONE;
3171 priv->recheck_available.unavailable_reason = NM_DEVICE_STATE_REASON_NONE;
3174 return G_SOURCE_REMOVE;
3178 nm_device_queue_recheck_available (NMDevice *self,
3179 NMDeviceStateReason available_reason,
3180 NMDeviceStateReason unavailable_reason)
3182 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3184 priv->recheck_available.available_reason = available_reason;
3185 priv->recheck_available.unavailable_reason = unavailable_reason;
3186 if (!priv->recheck_available.call_id)
3187 priv->recheck_available.call_id = g_idle_add (recheck_available, self);
3191 nm_device_emit_recheck_auto_activate (NMDevice *self)
3193 g_signal_emit (self, signals[RECHECK_AUTO_ACTIVATE], 0);
3197 dnsmasq_state_changed_cb (NMDnsMasqManager *manager, guint32 status, gpointer user_data)
3199 NMDevice *self = NM_DEVICE (user_data);
3202 case NM_DNSMASQ_STATUS_DEAD:
3203 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_SHARED_START_FAILED);
3210 /*****************************************************************************/
3213 activation_source_handle_cb4 (gpointer user_data)
3215 activation_source_handle_cb (user_data, AF_INET);
3216 return G_SOURCE_REMOVE;
3220 activation_source_handle_cb6 (gpointer user_data)
3222 activation_source_handle_cb (user_data, AF_INET6);
3223 return G_SOURCE_REMOVE;
3226 static ActivationHandleData *
3227 activation_source_get_by_family (NMDevice *self,
3229 GSourceFunc *out_idle_func)
3231 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3233 if (family == AF_INET6) {
3234 NM_SET_OUT (out_idle_func, activation_source_handle_cb6);
3235 return &priv->act_handle6;
3237 NM_SET_OUT (out_idle_func, activation_source_handle_cb4);
3238 g_return_val_if_fail (family == AF_INET, &priv->act_handle4);
3239 return &priv->act_handle4;
3244 activation_source_clear (NMDevice *self, int family)
3246 ActivationHandleData *act_data;
3248 act_data = activation_source_get_by_family (self, family, NULL);
3251 _LOGD (LOGD_DEVICE, "activation-stage: clear %s,%d (id %u)",
3252 _activation_func_to_string (act_data->func), family, act_data->id);
3253 nm_clear_g_source (&act_data->id);
3254 act_data->func = NULL;
3259 activation_source_handle_cb (NMDevice *self, int family)
3261 ActivationHandleData *act_data, a;
3263 g_return_if_fail (NM_IS_DEVICE (self));
3265 act_data = activation_source_get_by_family (self, family, NULL);
3267 g_return_if_fail (act_data->id);
3268 g_return_if_fail (act_data->func);
3272 act_data->func = NULL;
3275 _LOGD (LOGD_DEVICE, "activation-stage: invoke %s,%d (id %u)",
3276 _activation_func_to_string (a.func), family, a.id);
3280 _LOGD (LOGD_DEVICE, "activation-stage: complete %s,%d (id %u)",
3281 _activation_func_to_string (a.func), family, a.id);
3285 activation_source_schedule (NMDevice *self, ActivationHandleFunc func, int family)
3287 ActivationHandleData *act_data;
3288 GSourceFunc source_func;
3291 act_data = activation_source_get_by_family (self, family, &source_func);
3293 if (act_data->id && act_data->func != func) {
3294 /* Don't bother rescheduling the same function that's about to
3295 * run anyway. Fixes issues with crappy wireless drivers sending
3296 * streams of associate events before NM has had a chance to process
3299 _LOGD (LOGD_DEVICE, "activation-stage: already scheduled %s,%d (id %u)",
3300 _activation_func_to_string (func), family, act_data->id);
3304 new_id = g_idle_add (source_func, self);
3307 _LOGW (LOGD_DEVICE, "activation-stage: schedule %s,%d which replaces %s,%d (id %u -> %u)",
3308 _activation_func_to_string (func), family,
3309 _activation_func_to_string (act_data->func), family,
3310 act_data->id, new_id);
3311 nm_clear_g_source (&act_data->id);
3313 _LOGD (LOGD_DEVICE, "activation-stage: schedule %s,%d (id %u)",
3314 _activation_func_to_string (func), family, new_id);
3317 act_data->func = func;
3318 act_data->id = new_id;
3321 /*****************************************************************************/
3324 get_ip_config_may_fail (NMDevice *self, int family)
3326 NMConnection *connection;
3327 NMSettingIPConfig *s_ip = NULL;
3329 g_return_val_if_fail (self != NULL, TRUE);
3331 connection = nm_device_get_applied_connection (self);
3332 g_assert (connection);
3334 /* Fail the connection if the failed IP method is required to complete */
3337 s_ip = nm_connection_get_setting_ip4_config (connection);
3340 s_ip = nm_connection_get_setting_ip6_config (connection);
3343 g_assert_not_reached ();
3346 return nm_setting_ip_config_get_may_fail (s_ip);
3350 master_ready (NMDevice *self,
3351 NMActiveConnection *active)
3353 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3354 NMActiveConnection *master_connection;
3357 g_return_if_fail (priv->state == NM_DEVICE_STATE_PREPARE);
3358 g_return_if_fail (!priv->master_ready_handled);
3360 /* Notify a master device that it has a new slave */
3361 g_return_if_fail (nm_active_connection_get_master_ready (active));
3362 master_connection = nm_active_connection_get_master (active);
3364 priv->master_ready_handled = TRUE;
3365 nm_clear_g_signal_handler (active, &priv->master_ready_id);
3367 master = nm_active_connection_get_device (master_connection);
3369 _LOGD (LOGD_DEVICE, "master connection ready; master device %s",
3370 nm_device_get_iface (master));
3372 if (priv->master && priv->master != master)
3373 nm_device_master_release_one_slave (priv->master, self, FALSE, NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
3375 /* If the master didn't change, add-slave only rechecks whether to assume a connection. */
3376 nm_device_master_add_slave (master,
3378 nm_active_connection_get_assumed (active) ? FALSE : TRUE);
3382 master_ready_cb (NMActiveConnection *active,
3386 master_ready (self, active);
3387 nm_device_activate_schedule_stage2_device_config (self);
3391 lldp_neighbors_changed (NMLldpListener *lldp_listener, GParamSpec *pspec,
3394 NMDevice *self = NM_DEVICE (user_data);
3396 _notify (self, PROP_LLDP_NEIGHBORS);
3400 lldp_rx_enabled (NMDevice *self)
3402 NMConnection *connection;
3403 NMSettingConnection *s_con;
3404 NMSettingConnectionLldp lldp = NM_SETTING_CONNECTION_LLDP_DEFAULT;
3406 connection = nm_device_get_applied_connection (self);
3407 g_return_val_if_fail (connection, FALSE);
3409 s_con = nm_connection_get_setting_connection (connection);
3410 g_return_val_if_fail (s_con, FALSE);
3412 lldp = nm_setting_connection_get_lldp (s_con);
3413 if (lldp == NM_SETTING_CONNECTION_LLDP_DEFAULT) {
3414 gs_free char *value = NULL;
3416 value = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA,
3419 lldp = _nm_utils_ascii_str_to_int64 (value, 10,
3420 NM_SETTING_CONNECTION_LLDP_DEFAULT,
3421 NM_SETTING_CONNECTION_LLDP_ENABLE_RX,
3422 NM_SETTING_CONNECTION_LLDP_DEFAULT);
3423 if (lldp == NM_SETTING_CONNECTION_LLDP_DEFAULT)
3424 lldp = NM_SETTING_CONNECTION_LLDP_DISABLE;
3426 return lldp == NM_SETTING_CONNECTION_LLDP_ENABLE_RX;
3429 static NMActStageReturn
3430 act_stage1_prepare (NMDevice *self, NMDeviceStateReason *reason)
3432 return NM_ACT_STAGE_RETURN_SUCCESS;
3436 * activate_stage1_device_prepare
3438 * Prepare for device activation
3442 activate_stage1_device_prepare (NMDevice *self)
3444 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3445 NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS;
3446 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
3447 NMActiveConnection *active = NM_ACTIVE_CONNECTION (priv->act_request);
3449 priv->ip4_state = priv->ip6_state = IP_NONE;
3451 /* Notify the new ActiveConnection along with the state change */
3452 _notify (self, PROP_ACTIVE_CONNECTION);
3454 nm_device_state_changed (self, NM_DEVICE_STATE_PREPARE, NM_DEVICE_STATE_REASON_NONE);
3456 /* Assumed connections were already set up outside NetworkManager */
3457 if (!nm_active_connection_get_assumed (active)) {
3458 ret = NM_DEVICE_GET_CLASS (self)->act_stage1_prepare (self, &reason);
3459 if (ret == NM_ACT_STAGE_RETURN_POSTPONE) {
3461 } else if (ret == NM_ACT_STAGE_RETURN_FAILURE) {
3462 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
3465 g_assert (ret == NM_ACT_STAGE_RETURN_SUCCESS);
3468 nm_device_activate_schedule_stage2_device_config (self);
3473 * nm_device_activate_schedule_stage1_device_prepare
3475 * Prepare a device for activation
3479 nm_device_activate_schedule_stage1_device_prepare (NMDevice *self)
3481 NMDevicePrivate *priv;
3483 g_return_if_fail (NM_IS_DEVICE (self));
3485 priv = NM_DEVICE_GET_PRIVATE (self);
3486 g_return_if_fail (priv->act_request);
3488 activation_source_schedule (self, activate_stage1_device_prepare, AF_INET);
3491 static NMActStageReturn
3492 act_stage2_config (NMDevice *self, NMDeviceStateReason *reason)
3495 return NM_ACT_STAGE_RETURN_SUCCESS;
3499 * activate_stage2_device_config
3501 * Determine device parameters and set those on the device, ie
3502 * for wireless devices, set SSID, keys, etc.
3506 activate_stage2_device_config (NMDevice *self)
3508 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3509 NMActStageReturn ret;
3510 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
3511 gboolean no_firmware = FALSE;
3512 NMActiveConnection *active = NM_ACTIVE_CONNECTION (priv->act_request);
3515 nm_device_state_changed (self, NM_DEVICE_STATE_CONFIG, NM_DEVICE_STATE_REASON_NONE);
3517 /* Assumed connections were already set up outside NetworkManager */
3518 if (!nm_active_connection_get_assumed (active)) {
3519 if (!nm_device_bring_up (self, FALSE, &no_firmware)) {
3521 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_FIRMWARE_MISSING);
3523 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_CONFIG_FAILED);
3527 ret = NM_DEVICE_GET_CLASS (self)->act_stage2_config (self, &reason);
3528 if (ret == NM_ACT_STAGE_RETURN_POSTPONE)
3530 else if (ret == NM_ACT_STAGE_RETURN_FAILURE) {
3531 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
3534 g_assert (ret == NM_ACT_STAGE_RETURN_SUCCESS);
3537 /* If we have slaves that aren't yet enslaved, do that now */
3538 for (iter = priv->slaves; iter; iter = g_slist_next (iter)) {
3539 SlaveInfo *info = iter->data;
3540 NMDeviceState slave_state = nm_device_get_state (info->slave);
3542 if (slave_state == NM_DEVICE_STATE_IP_CONFIG)
3543 nm_device_master_enslave_slave (self, info->slave, nm_device_get_applied_connection (info->slave));
3544 else if ( nm_device_uses_generated_assumed_connection (self)
3545 && slave_state <= NM_DEVICE_STATE_DISCONNECTED)
3546 nm_device_queue_recheck_assume (info->slave);
3549 if (lldp_rx_enabled (self)) {
3550 gs_free_error GError *error = NULL;
3554 if (priv->lldp_listener)
3555 nm_lldp_listener_stop (priv->lldp_listener);
3557 priv->lldp_listener = nm_lldp_listener_new ();
3558 g_signal_connect (priv->lldp_listener,
3559 "notify::" NM_LLDP_LISTENER_NEIGHBORS,
3560 G_CALLBACK (lldp_neighbors_changed),
3564 addr = nm_platform_link_get_address (NM_PLATFORM_GET, priv->ifindex, &addr_length);
3566 if (nm_lldp_listener_start (priv->lldp_listener, nm_device_get_ifindex (self), &error))
3567 _LOGD (LOGD_DEVICE, "LLDP listener %p started", priv->lldp_listener);
3569 _LOGD (LOGD_DEVICE, "LLDP listener %p could not be started: %s",
3570 priv->lldp_listener, error->message);
3574 nm_device_activate_schedule_stage3_ip_config_start (self);
3579 * nm_device_activate_schedule_stage2_device_config
3581 * Schedule setup of the hardware device
3585 nm_device_activate_schedule_stage2_device_config (NMDevice *self)
3587 NMDevicePrivate *priv;
3589 g_return_if_fail (NM_IS_DEVICE (self));
3591 priv = NM_DEVICE_GET_PRIVATE (self);
3592 g_return_if_fail (priv->act_request);
3594 if (!priv->master_ready_handled) {
3595 NMActiveConnection *active = NM_ACTIVE_CONNECTION (priv->act_request);
3597 if (!nm_active_connection_get_master (active)) {
3598 g_warn_if_fail (!priv->master_ready_id);
3599 priv->master_ready_handled = TRUE;
3601 /* If the master connection is ready for slaves, attach ourselves */
3602 if (nm_active_connection_get_master_ready (active))
3603 master_ready (self, active);
3605 _LOGD (LOGD_DEVICE, "waiting for master connection to become ready");
3607 if (priv->master_ready_id == 0) {
3608 priv->master_ready_id = g_signal_connect (active,
3609 "notify::" NM_ACTIVE_CONNECTION_INT_MASTER_READY,
3610 (GCallback) master_ready_cb,
3619 activation_source_schedule (self, activate_stage2_device_config, AF_INET);
3625 * Progress the device to appropriate state if both IPv4 and IPv6 failed
3628 check_ip_failed (NMDevice *self, gboolean may_fail)
3630 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3631 NMDeviceState state;
3633 if ( priv->ip4_state != IP_FAIL
3634 || priv->ip6_state != IP_FAIL)
3637 if (nm_device_uses_assumed_connection (self)) {
3638 /* We have assumed configuration, but couldn't
3639 * redo it. No problem, move to check state. */
3640 priv->ip4_state = priv->ip6_state = IP_DONE;
3641 state = NM_DEVICE_STATE_IP_CHECK;
3642 } else if ( may_fail
3643 && get_ip_config_may_fail (self, AF_INET)
3644 && get_ip_config_may_fail (self, AF_INET6)) {
3645 /* Couldn't start either IPv6 and IPv4 autoconfiguration,
3646 * but both are allowed to fail. */
3647 state = NM_DEVICE_STATE_SECONDARIES;
3649 /* Autoconfiguration attempted without success. */
3650 state = NM_DEVICE_STATE_FAILED;
3653 nm_device_state_changed (self,
3655 NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE);
3661 * Progress the device to ip connectivity check state if IPv4 or IPv6 succeeded
3664 check_ip_done (NMDevice *self)
3666 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3668 if (nm_device_get_state (self) != NM_DEVICE_STATE_IP_CONFIG)
3671 if (priv->ip4_state != IP_DONE && !get_ip_config_may_fail (self, AF_INET))
3674 if (priv->ip6_state != IP_DONE && !get_ip_config_may_fail (self, AF_INET6))
3677 nm_device_state_changed (self, NM_DEVICE_STATE_IP_CHECK, NM_DEVICE_STATE_REASON_NONE);
3680 /*********************************************/
3681 /* IPv4 DAD stuff */
3684 get_ipv4_dad_timeout (NMDevice *self)
3686 NMConnection *connection;
3687 NMSettingIPConfig *s_ip4 = NULL;
3688 gs_free char *value = NULL;
3691 connection = nm_device_get_applied_connection (self);
3693 s_ip4 = nm_connection_get_setting_ip4_config (connection);
3696 ret = nm_setting_ip_config_get_dad_timeout (s_ip4);
3699 value = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA,
3700 "ipv4.dad-timeout", self);
3701 ret = _nm_utils_ascii_str_to_int64 (value, 10, -1,
3702 NM_SETTING_IP_CONFIG_DAD_TIMEOUT_MAX,
3704 ret = ret < 0 ? 0 : ret;
3712 arping_data_destroy (gpointer ptr, GClosure *closure)
3714 ArpingData *data = ptr;
3718 for (i = 0; data->configs && data->configs[i]; i++)
3719 g_object_unref (data->configs[i]);
3720 g_free (data->configs);
3721 g_slice_free (ArpingData, data);
3726 ipv4_manual_method_apply (NMDevice *self, NMIP4Config **configs, gboolean success)
3731 empty = nm_ip4_config_new (nm_device_get_ip_ifindex (self));
3732 nm_device_activate_schedule_ip4_config_result (self, empty);
3733 g_object_unref (empty);
3735 nm_device_queue_state (self, NM_DEVICE_STATE_FAILED,
3736 NM_DEVICE_STATE_REASON_CONFIG_FAILED);
3741 arping_manager_probe_terminated (NMArpingManager *arping_manager, ArpingData *data)
3744 NMDevicePrivate *priv;
3745 const NMPlatformIP4Address *address;
3746 gboolean result, success = TRUE;
3750 self = data->device;
3751 priv = NM_DEVICE_GET_PRIVATE (self);
3753 for (i = 0; data->configs && data->configs[i]; i++) {
3754 for (j = 0; j < nm_ip4_config_get_num_addresses (data->configs[i]); j++) {
3755 address = nm_ip4_config_get_address (data->configs[i], j);
3756 result = nm_arping_manager_check_address (arping_manager, address->address);
3759 _NMLOG (result ? LOGL_DEBUG : LOGL_WARN,
3761 "IPv4 DAD result: address %s is %s",
3762 nm_utils_inet4_ntop (address->address, NULL),
3763 result ? "unique" : "duplicate");
3767 data->callback (self, data->configs, success);
3769 priv->arping.dad_list = g_slist_remove (priv->arping.dad_list, arping_manager);
3770 nm_arping_manager_destroy (arping_manager);
3775 * @self: device instance
3776 * @configs: NULL-terminated array of IPv4 configurations
3777 * @cb: callback function
3779 * Start IPv4 DAD on device @self, check addresses in @configs and call @cb
3780 * when the procedure ends. @cb will be called in any case, even if DAD can't
3781 * be started. @configs will be unreferenced after @cb has been called.
3784 ipv4_dad_start (NMDevice *self, NMIP4Config **configs, ArpingCallback cb)
3786 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3787 NMArpingManager *arping_manager;
3788 const NMPlatformIP4Address *address;
3791 gboolean ret, addr_found;
3792 const guint8 *hw_addr;
3793 size_t hw_addr_len = 0;
3794 GError *error = NULL;
3797 g_return_if_fail (NM_IS_DEVICE (self));
3798 g_return_if_fail (configs);
3799 g_return_if_fail (cb);
3801 for (i = 0, addr_found = FALSE; configs[i]; i++) {
3802 if (nm_ip4_config_get_num_addresses (configs[i]) > 0) {
3808 timeout = get_ipv4_dad_timeout (self);
3809 hw_addr = nm_platform_link_get_address (NM_PLATFORM_GET,
3810 nm_device_get_ip_ifindex (self),
3817 || nm_device_uses_assumed_connection (self)) {
3819 /* DAD not needed, signal success */
3820 cb (self, configs, TRUE);
3822 for (i = 0; configs[i]; i++)
3823 g_object_unref (configs[i]);
3829 /* don't take additional references of @arping_manager that outlive @self.
3830 * Otherwise, the callback can be invoked on a dangling pointer as we don't
3831 * disconnect the handler. */
3832 arping_manager = nm_arping_manager_new (nm_device_get_ip_ifindex (self));
3833 priv->arping.dad_list = g_slist_append (priv->arping.dad_list, arping_manager);
3835 data = g_slice_new0 (ArpingData);
3836 data->configs = configs;
3837 data->callback = cb;
3838 data->device = self;
3840 for (i = 0; configs[i]; i++) {
3841 for (j = 0; j < nm_ip4_config_get_num_addresses (configs[i]); j++) {
3842 address = nm_ip4_config_get_address (configs[i], j);
3843 nm_arping_manager_add_address (arping_manager, address->address);
3847 g_signal_connect_data (arping_manager, NM_ARPING_MANAGER_PROBE_TERMINATED,
3848 G_CALLBACK (arping_manager_probe_terminated), data,
3849 arping_data_destroy, 0);
3851 ret = nm_arping_manager_start_probe (arping_manager, timeout, &error);
3854 _LOGW (LOGD_DEVICE, "arping probe failed: %s", error->message);
3856 /* DAD could not be started, signal success */
3857 cb (self, configs, TRUE);
3859 priv->arping.dad_list = g_slist_remove (priv->arping.dad_list, arping_manager);
3860 nm_arping_manager_destroy (arping_manager);
3864 /*********************************************/
3868 ipv4ll_cleanup (NMDevice *self)
3870 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3873 sd_ipv4ll_set_callback (priv->ipv4ll, NULL, NULL);
3874 sd_ipv4ll_stop (priv->ipv4ll);
3875 priv->ipv4ll = sd_ipv4ll_unref (priv->ipv4ll);
3878 nm_clear_g_source (&priv->ipv4ll_timeout);
3881 static NMIP4Config *
3882 ipv4ll_get_ip4_config (NMDevice *self, guint32 lla)
3884 NMIP4Config *config = NULL;
3885 NMPlatformIP4Address address;
3886 NMPlatformIP4Route route;
3888 config = nm_ip4_config_new (nm_device_get_ip_ifindex (self));
3891 memset (&address, 0, sizeof (address));
3892 nm_platform_ip4_address_set_addr (&address, lla, 16);
3893 address.source = NM_IP_CONFIG_SOURCE_IP4LL;
3894 nm_ip4_config_add_address (config, &address);
3896 /* Add a multicast route for link-local connections: destination= 224.0.0.0, netmask=240.0.0.0 */
3897 memset (&route, 0, sizeof (route));
3898 route.network = htonl (0xE0000000L);
3900 route.source = NM_IP_CONFIG_SOURCE_IP4LL;
3901 route.metric = nm_device_get_ip4_route_metric (self);
3902 nm_ip4_config_add_route (config, &route);
3907 #define IPV4LL_NETWORK (htonl (0xA9FE0000L))
3908 #define IPV4LL_NETMASK (htonl (0xFFFF0000L))
3911 nm_device_handle_ipv4ll_event (sd_ipv4ll *ll, int event, void *data)
3913 NMDevice *self = data;
3914 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3915 NMConnection *connection = NULL;
3917 struct in_addr address;
3918 NMIP4Config *config;
3921 if (priv->act_request == NULL)
3924 connection = nm_act_request_get_applied_connection (priv->act_request);
3925 g_assert (connection);
3927 /* Ignore if the connection isn't an AutoIP connection */
3928 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG);
3929 if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL) != 0)
3933 case SD_IPV4LL_EVENT_BIND:
3934 r = sd_ipv4ll_get_address (ll, &address);
3936 _LOGE (LOGD_AUTOIP4, "invalid IPv4 link-local address received, error %d.", r);
3937 priv->ip4_state = IP_FAIL;
3938 check_ip_failed (self, FALSE);
3942 if ((address.s_addr & IPV4LL_NETMASK) != IPV4LL_NETWORK) {
3943 _LOGE (LOGD_AUTOIP4, "invalid address %08x received (not link-local).", address.s_addr);
3944 priv->ip4_state = IP_FAIL;
3945 check_ip_failed (self, FALSE);
3949 config = ipv4ll_get_ip4_config (self, address.s_addr);
3950 if (config == NULL) {
3951 _LOGE (LOGD_AUTOIP4, "failed to get IPv4LL config");
3952 priv->ip4_state = IP_FAIL;
3953 check_ip_failed (self, FALSE);
3957 if (priv->ip4_state == IP_CONF) {
3958 nm_clear_g_source (&priv->ipv4ll_timeout);
3959 nm_device_activate_schedule_ip4_config_result (self, config);
3960 } else if (priv->ip4_state == IP_DONE) {
3961 if (!ip4_config_merge_and_apply (self, config, TRUE, NULL)) {
3962 _LOGE (LOGD_AUTOIP4, "failed to update IP4 config for autoip change.");
3963 priv->ip4_state = IP_FAIL;
3964 check_ip_failed (self, FALSE);
3967 g_assert_not_reached ();
3969 g_object_unref (config);
3972 _LOGW (LOGD_AUTOIP4, "IPv4LL address no longer valid after event %d.", event);
3973 priv->ip4_state = IP_FAIL;
3974 check_ip_failed (self, FALSE);
3979 ipv4ll_timeout_cb (gpointer user_data)
3981 NMDevice *self = NM_DEVICE (user_data);
3982 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
3984 if (priv->ipv4ll_timeout) {
3985 _LOGI (LOGD_AUTOIP4, "IPv4LL configuration timed out.");
3986 priv->ipv4ll_timeout = 0;
3987 ipv4ll_cleanup (self);
3989 if (priv->ip4_state == IP_CONF)
3990 nm_device_activate_schedule_ip4_config_timeout (self);
3996 static NMActStageReturn
3997 ipv4ll_start (NMDevice *self, NMDeviceStateReason *reason)
3999 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4000 const struct ether_addr *addr;
4004 ipv4ll_cleanup (self);
4006 r = sd_ipv4ll_new (&priv->ipv4ll);
4008 _LOGE (LOGD_AUTOIP4, "IPv4LL: new() failed with error %d", r);
4012 r = sd_ipv4ll_attach_event (priv->ipv4ll, NULL, 0);
4014 _LOGE (LOGD_AUTOIP4, "IPv4LL: attach_event() failed with error %d", r);
4018 ifindex = nm_device_get_ip_ifindex (self);
4019 addr = nm_platform_link_get_address (NM_PLATFORM_GET, ifindex, &addr_len);
4020 if (!addr || addr_len != ETH_ALEN) {
4021 _LOGE (LOGD_AUTOIP4, "IPv4LL: can't retrieve hardware address");
4025 r = sd_ipv4ll_set_mac (priv->ipv4ll, addr);
4027 _LOGE (LOGD_AUTOIP4, "IPv4LL: set_mac() failed with error %d", r);
4031 r = sd_ipv4ll_set_index (priv->ipv4ll, ifindex);
4033 _LOGE (LOGD_AUTOIP4, "IPv4LL: set_index() failed with error %d", r);
4037 r = sd_ipv4ll_set_callback (priv->ipv4ll, nm_device_handle_ipv4ll_event, self);
4039 _LOGE (LOGD_AUTOIP4, "IPv4LL: set_callback() failed with error %d", r);
4043 r = sd_ipv4ll_start (priv->ipv4ll);
4045 _LOGE (LOGD_AUTOIP4, "IPv4LL: start() failed with error %d", r);
4049 _LOGI (LOGD_DEVICE | LOGD_AUTOIP4, "IPv4LL: started");
4051 /* Start a timeout to bound the address attempt */
4052 priv->ipv4ll_timeout = g_timeout_add_seconds (20, ipv4ll_timeout_cb, self);
4054 return NM_ACT_STAGE_RETURN_POSTPONE;
4056 *reason = NM_DEVICE_STATE_REASON_AUTOIP_START_FAILED;
4057 return NM_ACT_STAGE_RETURN_FAILURE;
4060 /*********************************************/
4063 _device_get_default_route_from_platform (NMDevice *self, int addr_family, NMPlatformIPRoute *out_route)
4065 gboolean success = FALSE;
4066 int ifindex = nm_device_get_ip_ifindex (self);
4069 if (addr_family == AF_INET)
4070 routes = nm_platform_ip4_route_get_all (NM_PLATFORM_GET, ifindex, NM_PLATFORM_GET_ROUTE_FLAGS_WITH_DEFAULT);
4072 routes = nm_platform_ip6_route_get_all (NM_PLATFORM_GET, ifindex, NM_PLATFORM_GET_ROUTE_FLAGS_WITH_DEFAULT);
4075 guint route_metric = G_MAXUINT32, m;
4076 const NMPlatformIPRoute *route = NULL, *r;
4079 /* if there are several default routes, find the one with the best metric */
4080 for (i = 0; i < routes->len; i++) {
4081 if (addr_family == AF_INET) {
4082 r = (const NMPlatformIPRoute *) &g_array_index (routes, NMPlatformIP4Route, i);
4085 r = (const NMPlatformIPRoute *) &g_array_index (routes, NMPlatformIP6Route, i);
4086 m = nm_utils_ip6_route_metric_normalize (r->metric);
4088 if (!route || m < route_metric) {
4095 if (addr_family == AF_INET)
4096 *((NMPlatformIP4Route *) out_route) = *((NMPlatformIP4Route *) route);
4098 *((NMPlatformIP6Route *) out_route) = *((NMPlatformIP6Route *) route);
4101 g_array_free (routes, TRUE);
4106 /*********************************************/
4109 ensure_con_ip4_config (NMDevice *self)
4111 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4112 int ip_ifindex = nm_device_get_ip_ifindex (self);
4113 NMConnection *connection;
4115 if (priv->con_ip4_config)
4118 connection = nm_device_get_applied_connection (self);
4122 priv->con_ip4_config = nm_ip4_config_new (ip_ifindex);
4123 nm_ip4_config_merge_setting (priv->con_ip4_config,
4124 nm_connection_get_setting_ip4_config (connection),
4125 nm_device_get_ip4_route_metric (self));
4127 if (nm_device_uses_assumed_connection (self)) {
4128 /* For assumed connections ignore all addresses and routes. */
4129 nm_ip4_config_reset_addresses (priv->con_ip4_config);
4130 nm_ip4_config_reset_routes (priv->con_ip4_config);
4135 ensure_con_ip6_config (NMDevice *self)
4137 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4138 int ip_ifindex = nm_device_get_ip_ifindex (self);
4139 NMConnection *connection;
4141 if (priv->con_ip6_config)
4144 connection = nm_device_get_applied_connection (self);
4148 priv->con_ip6_config = nm_ip6_config_new (ip_ifindex);
4149 nm_ip6_config_merge_setting (priv->con_ip6_config,
4150 nm_connection_get_setting_ip6_config (connection),
4151 nm_device_get_ip6_route_metric (self));
4153 if (nm_device_uses_assumed_connection (self)) {
4154 /* For assumed connections ignore all addresses and routes. */
4155 nm_ip6_config_reset_addresses (priv->con_ip6_config);
4156 nm_ip6_config_reset_routes (priv->con_ip6_config);
4160 /*********************************************/
4164 dhcp4_cleanup (NMDevice *self, CleanupType cleanup_type, gboolean release)
4166 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4168 nm_clear_g_source (&priv->dhcp4_restart_id);
4170 if (priv->dhcp4_client) {
4171 /* Stop any ongoing DHCP transaction on this device */
4172 nm_clear_g_signal_handler (priv->dhcp4_client, &priv->dhcp4_state_sigid);
4174 nm_device_remove_pending_action (self, PENDING_ACTION_DHCP4, FALSE);
4176 if ( cleanup_type == CLEANUP_TYPE_DECONFIGURE
4177 || cleanup_type == CLEANUP_TYPE_REMOVED)
4178 nm_dhcp_client_stop (priv->dhcp4_client, release);
4180 g_clear_object (&priv->dhcp4_client);
4183 if (priv->dhcp4_config) {
4184 nm_exported_object_clear_and_unexport (&priv->dhcp4_config);
4185 _notify (self, PROP_DHCP4_CONFIG);
4190 _ip4_config_merge_default (gpointer value, gpointer user_data)
4192 NMIP4Config *src = (NMIP4Config *) value;
4193 NMIP4Config *dst = (NMIP4Config *) user_data;
4195 nm_ip4_config_merge (dst, src, NM_IP_CONFIG_MERGE_DEFAULT);
4199 ip4_config_merge_and_apply (NMDevice *self,
4200 NMIP4Config *config,
4202 NMDeviceStateReason *out_reason)
4204 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4205 NMConnection *connection;
4207 NMIP4Config *composite;
4208 gboolean has_direct_route;
4209 const guint32 default_route_metric = nm_device_get_ip4_route_metric (self);
4211 gboolean connection_has_default_route, connection_is_never_default;
4212 gboolean routes_full_sync;
4213 gboolean ignore_auto_routes = FALSE;
4214 gboolean ignore_auto_dns = FALSE;
4216 /* Merge all the configs into the composite config */
4218 g_clear_object (&priv->dev_ip4_config);
4219 priv->dev_ip4_config = g_object_ref (config);
4222 /* Apply ignore-auto-routes and ignore-auto-dns settings */
4223 connection = nm_device_get_applied_connection (self);
4225 NMSettingIPConfig *s_ip4 = nm_connection_get_setting_ip4_config (connection);
4228 ignore_auto_routes = nm_setting_ip_config_get_ignore_auto_routes (s_ip4);
4229 ignore_auto_dns = nm_setting_ip_config_get_ignore_auto_dns (s_ip4);
4233 composite = nm_ip4_config_new (nm_device_get_ip_ifindex (self));
4236 ensure_con_ip4_config (self);
4238 if (priv->dev_ip4_config) {
4239 nm_ip4_config_merge (composite, priv->dev_ip4_config,
4240 (ignore_auto_routes ? NM_IP_CONFIG_MERGE_NO_ROUTES : 0)
4241 | (ignore_auto_dns ? NM_IP_CONFIG_MERGE_NO_DNS : 0));
4244 g_slist_foreach (priv->vpn4_configs, _ip4_config_merge_default, composite);
4246 if (priv->ext_ip4_config)
4247 nm_ip4_config_merge (composite, priv->ext_ip4_config, NM_IP_CONFIG_MERGE_DEFAULT);
4249 /* Merge WWAN config *last* to ensure modem-given settings overwrite
4250 * any external stuff set by pppd or other scripts.
4252 if (priv->wwan_ip4_config) {
4253 nm_ip4_config_merge (composite, priv->wwan_ip4_config,
4254 (ignore_auto_routes ? NM_IP_CONFIG_MERGE_NO_ROUTES : 0)
4255 | (ignore_auto_dns ? NM_IP_CONFIG_MERGE_NO_DNS : 0));
4258 /* Merge user overrides into the composite config. For assumed connections,
4259 * con_ip4_config is empty. */
4260 if (priv->con_ip4_config)
4261 nm_ip4_config_merge (composite, priv->con_ip4_config, NM_IP_CONFIG_MERGE_DEFAULT);
4263 /* Add the default route.
4265 * We keep track of the default route of a device in a private field.
4266 * NMDevice needs to know the default route at this point, because the gateway
4267 * might require a direct route (see below).
4269 * But also, we don't want to add the default route to priv->ip4_config,
4270 * because the default route from the setting might not be the same that
4271 * NMDefaultRouteManager eventually configures (because the it might
4272 * tweak the effective metric).
4275 /* unless we come to a different conclusion below, we have no default route and
4276 * the route is assumed. */
4277 priv->default_route.v4_has = FALSE;
4278 priv->default_route.v4_is_assumed = TRUE;
4281 /* during a non-commit event, we always pickup whatever is configured. */
4282 goto END_ADD_DEFAULT_ROUTE;
4285 if (nm_device_uses_generated_assumed_connection (self)) {
4286 /* a generate-assumed-connection always detects the default route from platform */
4287 goto END_ADD_DEFAULT_ROUTE;
4290 /* At this point, we treat assumed and non-assumed connections alike.
4291 * For assumed connections we do that because we still manage RA and DHCP
4292 * leases for them, so we must extend/update the default route on commits.
4295 connection_has_default_route
4296 = nm_default_route_manager_ip4_connection_has_default_route (nm_default_route_manager_get (),
4297 connection, &connection_is_never_default);
4299 if ( !priv->v4_commit_first_time
4300 && connection_is_never_default) {
4301 /* If the connection is explicitly configured as never-default, we enforce the (absence of the)
4302 * default-route only once. That allows the user to configure a connection as never-default,
4303 * but he can add default routes externally (via a dispatcher script) and NM will not interfere. */
4304 goto END_ADD_DEFAULT_ROUTE;
4307 /* we are about to commit (for a non-assumed connection). Enforce whatever we have
4309 priv->default_route.v4_is_assumed = FALSE;
4311 if (!connection_has_default_route)
4312 goto END_ADD_DEFAULT_ROUTE;
4314 if (!nm_ip4_config_get_num_addresses (composite)) {
4315 /* without addresses we can have no default route. */
4316 goto END_ADD_DEFAULT_ROUTE;
4319 gateway = nm_ip4_config_get_gateway (composite);
4320 if ( !nm_ip4_config_has_gateway (composite)
4321 && nm_device_get_device_type (self) != NM_DEVICE_TYPE_MODEM)
4322 goto END_ADD_DEFAULT_ROUTE;
4324 has_direct_route = ( gateway == 0
4325 || nm_ip4_config_destination_is_direct (composite, gateway, 32)
4326 || nm_ip4_config_get_direct_route_for_host (composite, gateway));
4328 priv->default_route.v4_has = TRUE;
4329 memset (&priv->default_route.v4, 0, sizeof (priv->default_route.v4));
4330 priv->default_route.v4.source = NM_IP_CONFIG_SOURCE_USER;
4331 priv->default_route.v4.gateway = gateway;
4332 priv->default_route.v4.metric = default_route_metric;
4333 priv->default_route.v4.mss = nm_ip4_config_get_mss (composite);
4335 if (!has_direct_route) {
4336 NMPlatformIP4Route r = priv->default_route.v4;
4338 /* add a direct route to the gateway */
4339 r.network = gateway;
4342 nm_ip4_config_add_route (composite, &r);
4345 END_ADD_DEFAULT_ROUTE:
4347 if (priv->default_route.v4_is_assumed) {
4348 /* If above does not explicitly assign a default route, we always pick up the
4349 * default route based on what is currently configured.
4350 * That means that even managed connections with never-default, can
4351 * get a default route (if configured externally).
4353 priv->default_route.v4_has = _device_get_default_route_from_platform (self, AF_INET, (NMPlatformIPRoute *) &priv->default_route.v4);
4356 nm_ip4_config_addresses_sort (composite);
4358 /* Allow setting MTU etc */
4360 if (NM_DEVICE_GET_CLASS (self)->ip4_config_pre_commit)
4361 NM_DEVICE_GET_CLASS (self)->ip4_config_pre_commit (self, composite);
4364 routes_full_sync = commit
4365 && priv->v4_commit_first_time
4366 && !nm_device_uses_assumed_connection (self);
4368 success = nm_device_set_ip4_config (self, composite, default_route_metric, commit, routes_full_sync, out_reason);
4369 g_object_unref (composite);
4372 priv->v4_commit_first_time = FALSE;
4377 dhcp4_lease_change (NMDevice *self, NMIP4Config *config)
4379 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
4381 g_return_if_fail (config != NULL);
4383 if (!ip4_config_merge_and_apply (self, config, TRUE, &reason)) {
4384 _LOGW (LOGD_DHCP4, "failed to update IPv4 config for DHCP change.");
4385 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
4387 /* Notify dispatcher scripts of new DHCP4 config */
4388 nm_dispatcher_call (DISPATCHER_ACTION_DHCP4_CHANGE,
4389 nm_device_get_settings_connection (self),
4390 nm_device_get_applied_connection (self),
4399 dhcp4_restart_cb (gpointer user_data)
4401 NMDevice *self = user_data;
4402 NMDevicePrivate *priv;
4403 NMDeviceStateReason reason;
4404 NMConnection *connection;
4406 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
4408 priv = NM_DEVICE_GET_PRIVATE (self);
4409 priv->dhcp4_restart_id = 0;
4410 connection = nm_device_get_applied_connection (self);
4412 if (dhcp4_start (self, connection, &reason) == NM_ACT_STAGE_RETURN_FAILURE)
4413 priv->dhcp4_restart_id = g_timeout_add_seconds (120, dhcp4_restart_cb, self);
4419 dhcp4_fail (NMDevice *self, gboolean timeout)
4421 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4423 dhcp4_cleanup (self, CLEANUP_TYPE_DECONFIGURE, FALSE);
4425 /* Don't fail if there are static addresses configured on
4426 * the device, instead retry after some time.
4428 if ( priv->ip4_state == IP_DONE
4429 && priv->con_ip4_config
4430 && nm_ip4_config_get_num_addresses (priv->con_ip4_config) > 0) {
4431 _LOGI (LOGD_DHCP4, "Scheduling DHCPv4 restart because device has IP addresses");
4432 priv->dhcp4_restart_id = g_timeout_add_seconds (120, dhcp4_restart_cb, self);
4436 /* Instead of letting an assumed connection fail (which means that the
4437 * device will transition to the ACTIVATED state without IP configuration),
4440 if (nm_device_uses_assumed_connection (self)) {
4441 _LOGI (LOGD_DHCP4, "Scheduling DHCPv4 restart because the connection is assumed");
4442 priv->dhcp4_restart_id = g_timeout_add_seconds (120, dhcp4_restart_cb, self);
4446 if (timeout || (priv->ip4_state == IP_CONF))
4447 nm_device_activate_schedule_ip4_config_timeout (self);
4448 else if (priv->ip4_state == IP_DONE)
4449 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED);
4451 g_warn_if_reached ();
4455 dhcp4_dad_cb (NMDevice *self, NMIP4Config **configs, gboolean success)
4458 nm_device_activate_schedule_ip4_config_result (self, configs[1]);
4460 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED,
4461 NM_DEVICE_STATE_REASON_CONFIG_FAILED);
4466 dhcp4_state_changed (NMDhcpClient *client,
4468 NMIP4Config *ip4_config,
4469 GHashTable *options,
4470 const char *event_id,
4473 NMDevice *self = NM_DEVICE (user_data);
4474 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4475 NMIP4Config *manual, **configs;
4476 NMConnection *connection;
4478 g_return_if_fail (nm_dhcp_client_get_ipv6 (client) == FALSE);
4479 g_return_if_fail (!ip4_config || NM_IS_IP4_CONFIG (ip4_config));
4481 _LOGD (LOGD_DHCP4, "new DHCPv4 client state %d", state);
4484 case NM_DHCP_STATE_BOUND:
4486 _LOGW (LOGD_DHCP4, "failed to get IPv4 config in response to DHCP event.");
4487 nm_device_state_changed (self,
4488 NM_DEVICE_STATE_FAILED,
4489 NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE);
4493 nm_dhcp4_config_set_options (priv->dhcp4_config, options);
4494 _notify (self, PROP_DHCP4_CONFIG);
4496 if (priv->ip4_state == IP_CONF) {
4497 connection = nm_device_get_applied_connection (self);
4498 g_assert (connection);
4500 manual = nm_ip4_config_new (nm_device_get_ip_ifindex (self));
4501 nm_ip4_config_merge_setting (manual,
4502 nm_connection_get_setting_ip4_config (connection),
4503 nm_device_get_ip4_route_metric (self));
4505 configs = g_new0 (NMIP4Config *, 3);
4506 configs[0] = manual;
4507 configs[1] = g_object_ref (ip4_config);
4509 ipv4_dad_start (self, configs, dhcp4_dad_cb);
4510 } else if (priv->ip4_state == IP_DONE) {
4511 dhcp4_lease_change (self, ip4_config);
4512 nm_device_update_metered (self);
4515 case NM_DHCP_STATE_TIMEOUT:
4516 dhcp4_fail (self, TRUE);
4518 case NM_DHCP_STATE_EXPIRE:
4519 /* Ignore expiry before we even have a lease (NAK, old lease, etc) */
4520 if (priv->ip4_state == IP_CONF)
4523 case NM_DHCP_STATE_DONE:
4524 case NM_DHCP_STATE_FAIL:
4525 dhcp4_fail (self, FALSE);
4533 dhcp4_get_timeout (NMDevice *self, NMSettingIP4Config *s_ip4)
4535 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4536 gs_free char *value = NULL;
4539 timeout = nm_setting_ip_config_get_dhcp_timeout (NM_SETTING_IP_CONFIG (s_ip4));
4543 value = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA,
4544 "ipv4.dhcp-timeout",
4546 timeout = _nm_utils_ascii_str_to_int64 (value, 10,
4551 return priv->dhcp_timeout;
4554 static NMActStageReturn
4555 dhcp4_start (NMDevice *self,
4556 NMConnection *connection,
4557 NMDeviceStateReason *reason)
4559 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4560 NMSettingIPConfig *s_ip4;
4561 const guint8 *hw_addr;
4562 size_t hw_addr_len = 0;
4563 GByteArray *tmp = NULL;
4565 s_ip4 = nm_connection_get_setting_ip4_config (connection);
4567 /* Clear old exported DHCP options */
4568 nm_exported_object_clear_and_unexport (&priv->dhcp4_config);
4569 priv->dhcp4_config = nm_dhcp4_config_new ();
4571 hw_addr = nm_platform_link_get_address (NM_PLATFORM_GET, nm_device_get_ip_ifindex (self), &hw_addr_len);
4573 tmp = g_byte_array_sized_new (hw_addr_len);
4574 g_byte_array_append (tmp, hw_addr, hw_addr_len);
4577 /* Begin DHCP on the interface */
4578 g_warn_if_fail (priv->dhcp4_client == NULL);
4579 priv->dhcp4_client = nm_dhcp_manager_start_ip4 (nm_dhcp_manager_get (),
4580 nm_device_get_ip_iface (self),
4581 nm_device_get_ip_ifindex (self),
4583 nm_connection_get_uuid (connection),
4584 nm_device_get_ip4_route_metric (self),
4585 nm_setting_ip_config_get_dhcp_send_hostname (s_ip4),
4586 nm_setting_ip_config_get_dhcp_hostname (s_ip4),
4587 nm_setting_ip4_config_get_dhcp_fqdn (NM_SETTING_IP4_CONFIG (s_ip4)),
4588 nm_setting_ip4_config_get_dhcp_client_id (NM_SETTING_IP4_CONFIG (s_ip4)),
4589 dhcp4_get_timeout (self, NM_SETTING_IP4_CONFIG (s_ip4)),
4590 priv->dhcp_anycast_address,
4594 g_byte_array_free (tmp, TRUE);
4596 if (!priv->dhcp4_client) {
4597 *reason = NM_DEVICE_STATE_REASON_DHCP_START_FAILED;
4598 return NM_ACT_STAGE_RETURN_FAILURE;
4601 priv->dhcp4_state_sigid = g_signal_connect (priv->dhcp4_client,
4602 NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED,
4603 G_CALLBACK (dhcp4_state_changed),
4606 nm_device_add_pending_action (self, PENDING_ACTION_DHCP4, TRUE);
4608 /* DHCP devices will be notified by the DHCP manager when stuff happens */
4609 return NM_ACT_STAGE_RETURN_POSTPONE;
4613 nm_device_dhcp4_renew (NMDevice *self, gboolean release)
4615 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4616 NMActStageReturn ret;
4617 NMDeviceStateReason reason;
4618 NMConnection *connection;
4620 g_return_val_if_fail (priv->dhcp4_client != NULL, FALSE);
4622 _LOGI (LOGD_DHCP4, "DHCPv4 lease renewal requested");
4624 /* Terminate old DHCP instance and release the old lease */
4625 dhcp4_cleanup (self, CLEANUP_TYPE_DECONFIGURE, release);
4627 connection = nm_device_get_applied_connection (self);
4628 g_assert (connection);
4630 /* Start DHCP again on the interface */
4631 ret = dhcp4_start (self, connection, &reason);
4633 return (ret != NM_ACT_STAGE_RETURN_FAILURE);
4636 /*********************************************/
4638 static GHashTable *shared_ips = NULL;
4641 release_shared_ip (gpointer data)
4643 g_hash_table_remove (shared_ips, data);
4647 reserve_shared_ip (NMDevice *self, NMSettingIPConfig *s_ip4, NMPlatformIP4Address *address)
4649 if (G_UNLIKELY (shared_ips == NULL))
4650 shared_ips = g_hash_table_new (g_direct_hash, g_direct_equal);
4652 memset (address, 0, sizeof (*address));
4654 if (s_ip4 && nm_setting_ip_config_get_num_addresses (s_ip4)) {
4655 /* Use the first user-supplied address */
4656 NMIPAddress *user = nm_setting_ip_config_get_address (s_ip4, 0);
4660 nm_ip_address_get_address_binary (user, &a);
4661 nm_platform_ip4_address_set_addr (address, a, nm_ip_address_get_prefix (user));
4663 /* Find an unused address in the 10.42.x.x range */
4664 guint32 start = (guint32) ntohl (0x0a2a0001); /* 10.42.0.1 */
4667 while (g_hash_table_lookup (shared_ips, GUINT_TO_POINTER (start + count))) {
4668 count += ntohl (0x100);
4669 if (count > ntohl (0xFE00)) {
4670 _LOGE (LOGD_SHARING, "ran out of shared IP addresses!");
4674 nm_platform_ip4_address_set_addr (address, start + count, 24);
4675 g_hash_table_add (shared_ips, GUINT_TO_POINTER (address->address));
4681 static NMIP4Config *
4682 shared4_new_config (NMDevice *self, NMConnection *connection, NMDeviceStateReason *reason)
4684 NMIP4Config *config = NULL;
4685 NMPlatformIP4Address address;
4687 g_return_val_if_fail (self != NULL, NULL);
4689 if (!reserve_shared_ip (self, nm_connection_get_setting_ip4_config (connection), &address)) {
4690 *reason = NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE;
4694 config = nm_ip4_config_new (nm_device_get_ip_ifindex (self));
4695 address.source = NM_IP_CONFIG_SOURCE_SHARED;
4696 nm_ip4_config_add_address (config, &address);
4698 /* Remove the address lock when the object gets disposed */
4699 g_object_set_data_full (G_OBJECT (config), "shared-ip",
4700 GUINT_TO_POINTER (address.address),
4706 /*********************************************/
4709 connection_ip4_method_requires_carrier (NMConnection *connection,
4710 gboolean *out_ip4_enabled)
4712 const char *method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG);
4713 static const char *ip4_carrier_methods[] = {
4714 NM_SETTING_IP4_CONFIG_METHOD_AUTO,
4715 NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL,
4719 if (out_ip4_enabled)
4720 *out_ip4_enabled = !!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED);
4721 return _nm_utils_string_in_list (method, ip4_carrier_methods);
4725 connection_ip6_method_requires_carrier (NMConnection *connection,
4726 gboolean *out_ip6_enabled)
4728 const char *method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG);
4729 static const char *ip6_carrier_methods[] = {
4730 NM_SETTING_IP6_CONFIG_METHOD_AUTO,
4731 NM_SETTING_IP6_CONFIG_METHOD_DHCP,
4732 NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL,
4736 if (out_ip6_enabled)
4737 *out_ip6_enabled = !!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
4738 return _nm_utils_string_in_list (method, ip6_carrier_methods);
4742 connection_requires_carrier (NMConnection *connection)
4744 NMSettingIPConfig *s_ip4, *s_ip6;
4745 gboolean ip4_carrier_wanted, ip6_carrier_wanted;
4746 gboolean ip4_used = FALSE, ip6_used = FALSE;
4748 ip4_carrier_wanted = connection_ip4_method_requires_carrier (connection, &ip4_used);
4749 if (ip4_carrier_wanted) {
4750 /* If IPv4 wants a carrier and cannot fail, the whole connection
4751 * requires a carrier regardless of the IPv6 method.
4753 s_ip4 = nm_connection_get_setting_ip4_config (connection);
4754 if (s_ip4 && !nm_setting_ip_config_get_may_fail (s_ip4))
4758 ip6_carrier_wanted = connection_ip6_method_requires_carrier (connection, &ip6_used);
4759 if (ip6_carrier_wanted) {
4760 /* If IPv6 wants a carrier and cannot fail, the whole connection
4761 * requires a carrier regardless of the IPv4 method.
4763 s_ip6 = nm_connection_get_setting_ip6_config (connection);
4764 if (s_ip6 && !nm_setting_ip_config_get_may_fail (s_ip6))
4768 /* If an IP version wants a carrier and and the other IP version isn't
4769 * used, the connection requires carrier since it will just fail without one.
4771 if (ip4_carrier_wanted && !ip6_used)
4773 if (ip6_carrier_wanted && !ip4_used)
4776 /* If both want a carrier, the whole connection wants a carrier */
4777 return ip4_carrier_wanted && ip6_carrier_wanted;
4781 have_any_ready_slaves (NMDevice *self, const GSList *slaves)
4785 /* Any enslaved slave is "ready" in the generic case as it's
4786 * at least >= NM_DEVCIE_STATE_IP_CONFIG and has had Layer 2
4787 * properties set up.
4789 for (iter = slaves; iter; iter = g_slist_next (iter)) {
4790 if (nm_device_get_enslaved (iter->data))
4797 ip4_requires_slaves (NMConnection *connection)
4801 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG);
4802 return strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0;
4805 static NMActStageReturn
4806 act_stage3_ip4_config_start (NMDevice *self,
4807 NMIP4Config **out_config,
4808 NMDeviceStateReason *reason)
4810 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4811 NMConnection *connection;
4812 NMActStageReturn ret = NM_ACT_STAGE_RETURN_FAILURE;
4815 gboolean ready_slaves;
4817 g_return_val_if_fail (reason != NULL, NM_ACT_STAGE_RETURN_FAILURE);
4819 connection = nm_device_get_applied_connection (self);
4820 g_assert (connection);
4822 if ( connection_ip4_method_requires_carrier (connection, NULL)
4824 && !priv->carrier) {
4825 _LOGI (LOGD_IP4 | LOGD_DEVICE,
4826 "IPv4 config waiting until carrier is on");
4827 return NM_ACT_STAGE_RETURN_WAIT;
4830 if (priv->is_master && ip4_requires_slaves (connection)) {
4831 /* If the master has no ready slaves, and depends on slaves for
4832 * a successful IPv4 attempt, then postpone IPv4 addressing.
4834 slaves = nm_device_master_get_slaves (self);
4835 ready_slaves = NM_DEVICE_GET_CLASS (self)->have_any_ready_slaves (self, slaves);
4836 g_slist_free (slaves);
4838 if (ready_slaves == FALSE) {
4839 _LOGI (LOGD_DEVICE | LOGD_IP4,
4840 "IPv4 config waiting until slaves are ready");
4841 return NM_ACT_STAGE_RETURN_WAIT;
4845 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG);
4847 /* Start IPv4 addressing based on the method requested */
4848 if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0)
4849 ret = dhcp4_start (self, connection, reason);
4850 else if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL) == 0)
4851 ret = ipv4ll_start (self, reason);
4852 else if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0) {
4853 NMIP4Config **configs, *config;
4855 config = nm_ip4_config_new (nm_device_get_ip_ifindex (self));
4856 nm_ip4_config_merge_setting (config,
4857 nm_connection_get_setting_ip4_config (connection),
4858 nm_device_get_ip4_route_metric (self));
4860 configs = g_new0 (NMIP4Config *, 2);
4861 configs[0] = config;
4862 ipv4_dad_start (self, configs, ipv4_manual_method_apply);
4863 ret = NM_ACT_STAGE_RETURN_POSTPONE;
4864 } else if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0) {
4865 *out_config = shared4_new_config (self, connection, reason);
4867 priv->dnsmasq_manager = nm_dnsmasq_manager_new (nm_device_get_ip_iface (self));
4868 ret = NM_ACT_STAGE_RETURN_SUCCESS;
4870 ret = NM_ACT_STAGE_RETURN_FAILURE;
4871 } else if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0) {
4872 /* Nothing to do... */
4873 ret = NM_ACT_STAGE_RETURN_STOP;
4875 _LOGW (LOGD_IP4, "unhandled IPv4 config method '%s'; will fail", method);
4880 /*********************************************/
4884 dhcp6_cleanup (NMDevice *self, CleanupType cleanup_type, gboolean release)
4886 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4888 priv->dhcp6_mode = NM_RDISC_DHCP_LEVEL_NONE;
4889 g_clear_object (&priv->dhcp6_ip6_config);
4890 g_clear_pointer (&priv->dhcp6_event_id, g_free);
4891 nm_clear_g_source (&priv->dhcp6_restart_id);
4893 if (priv->dhcp6_client) {
4894 nm_clear_g_signal_handler (priv->dhcp6_client, &priv->dhcp6_state_sigid);
4896 if ( cleanup_type == CLEANUP_TYPE_DECONFIGURE
4897 || cleanup_type == CLEANUP_TYPE_REMOVED)
4898 nm_dhcp_client_stop (priv->dhcp6_client, release);
4900 g_clear_object (&priv->dhcp6_client);
4903 nm_device_remove_pending_action (self, PENDING_ACTION_DHCP6, FALSE);
4905 if (priv->dhcp6_config) {
4906 nm_exported_object_clear_and_unexport (&priv->dhcp6_config);
4907 _notify (self, PROP_DHCP6_CONFIG);
4912 _ip6_config_merge_default (gpointer value, gpointer user_data)
4914 NMIP6Config *src = (NMIP6Config *) value;
4915 NMIP6Config *dst = (NMIP6Config *) user_data;
4917 nm_ip6_config_merge (dst, src, NM_IP_CONFIG_MERGE_DEFAULT);
4921 ip6_config_merge_and_apply (NMDevice *self,
4923 NMDeviceStateReason *out_reason)
4925 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
4926 NMConnection *connection;
4928 NMIP6Config *composite;
4929 gboolean has_direct_route;
4930 const struct in6_addr *gateway;
4931 gboolean connection_has_default_route, connection_is_never_default;
4932 gboolean routes_full_sync;
4933 gboolean ignore_auto_routes = FALSE;
4934 gboolean ignore_auto_dns = FALSE;
4936 /* Apply ignore-auto-routes and ignore-auto-dns settings */
4937 connection = nm_device_get_applied_connection (self);
4939 NMSettingIPConfig *s_ip6 = nm_connection_get_setting_ip6_config (connection);
4942 ignore_auto_routes = nm_setting_ip_config_get_ignore_auto_routes (s_ip6);
4943 ignore_auto_dns = nm_setting_ip_config_get_ignore_auto_dns (s_ip6);
4947 /* If no config was passed in, create a new one */
4948 composite = nm_ip6_config_new (nm_device_get_ip_ifindex (self));
4951 ensure_con_ip6_config (self);
4952 g_assert (composite);
4954 /* Merge all the IP configs into the composite config */
4955 if (priv->ac_ip6_config) {
4956 nm_ip6_config_merge (composite, priv->ac_ip6_config,
4957 (ignore_auto_routes ? NM_IP_CONFIG_MERGE_NO_ROUTES : 0)
4958 | (ignore_auto_dns ? NM_IP_CONFIG_MERGE_NO_DNS : 0));
4960 if (priv->dhcp6_ip6_config) {
4961 nm_ip6_config_merge (composite, priv->dhcp6_ip6_config,
4962 (ignore_auto_routes ? NM_IP_CONFIG_MERGE_NO_ROUTES : 0)
4963 | (ignore_auto_dns ? NM_IP_CONFIG_MERGE_NO_DNS : 0));
4966 g_slist_foreach (priv->vpn6_configs, _ip6_config_merge_default, composite);
4968 if (priv->ext_ip6_config)
4969 nm_ip6_config_merge (composite, priv->ext_ip6_config, NM_IP_CONFIG_MERGE_DEFAULT);
4971 /* Merge WWAN config *last* to ensure modem-given settings overwrite
4972 * any external stuff set by pppd or other scripts.
4974 if (priv->wwan_ip6_config) {
4975 nm_ip6_config_merge (composite, priv->wwan_ip6_config,
4976 (ignore_auto_routes ? NM_IP_CONFIG_MERGE_NO_ROUTES : 0)
4977 | (ignore_auto_dns ? NM_IP_CONFIG_MERGE_NO_DNS : 0));
4980 /* Merge user overrides into the composite config. For assumed connections,
4981 * con_ip6_config is empty. */
4982 if (priv->con_ip6_config)
4983 nm_ip6_config_merge (composite, priv->con_ip6_config, NM_IP_CONFIG_MERGE_DEFAULT);
4985 /* Add the default route.
4987 * We keep track of the default route of a device in a private field.
4988 * NMDevice needs to know the default route at this point, because the gateway
4989 * might require a direct route (see below).
4991 * But also, we don't want to add the default route to priv->ip6_config,
4992 * because the default route from the setting might not be the same that
4993 * NMDefaultRouteManager eventually configures (because the it might
4994 * tweak the effective metric).
4997 /* unless we come to a different conclusion below, we have no default route and
4998 * the route is assumed. */
4999 priv->default_route.v6_has = FALSE;
5000 priv->default_route.v6_is_assumed = TRUE;
5003 /* during a non-commit event, we always pickup whatever is configured. */
5004 goto END_ADD_DEFAULT_ROUTE;
5007 if (nm_device_uses_generated_assumed_connection (self)) {
5008 /* a generate-assumed-connection always detects the default route from platform */
5009 goto END_ADD_DEFAULT_ROUTE;
5012 /* At this point, we treat assumed and non-assumed connections alike.
5013 * For assumed connections we do that because we still manage RA and DHCP
5014 * leases for them, so we must extend/update the default route on commits.
5017 connection_has_default_route
5018 = nm_default_route_manager_ip6_connection_has_default_route (nm_default_route_manager_get (),
5019 connection, &connection_is_never_default);
5021 if ( !priv->v6_commit_first_time
5022 && connection_is_never_default) {
5023 /* If the connection is explicitly configured as never-default, we enforce the (absence of the)
5024 * default-route only once. That allows the user to configure a connection as never-default,
5025 * but he can add default routes externally (via a dispatcher script) and NM will not interfere. */
5026 goto END_ADD_DEFAULT_ROUTE;
5029 /* we are about to commit (for a non-assumed connection). Enforce whatever we have
5031 priv->default_route.v6_is_assumed = FALSE;
5033 if (!connection_has_default_route)
5034 goto END_ADD_DEFAULT_ROUTE;
5036 if (!nm_ip6_config_get_num_addresses (composite)) {
5037 /* without addresses we can have no default route. */
5038 goto END_ADD_DEFAULT_ROUTE;
5041 gateway = nm_ip6_config_get_gateway (composite);
5043 goto END_ADD_DEFAULT_ROUTE;
5046 has_direct_route = nm_ip6_config_get_direct_route_for_host (composite, gateway) != NULL;
5050 priv->default_route.v6_has = TRUE;
5051 memset (&priv->default_route.v6, 0, sizeof (priv->default_route.v6));
5052 priv->default_route.v6.source = NM_IP_CONFIG_SOURCE_USER;
5053 priv->default_route.v6.gateway = *gateway;
5054 priv->default_route.v6.metric = nm_device_get_ip6_route_metric (self);
5055 priv->default_route.v6.mss = nm_ip6_config_get_mss (composite);
5057 if (!has_direct_route) {
5058 NMPlatformIP6Route r = priv->default_route.v6;
5060 /* add a direct route to the gateway */
5061 r.network = *gateway;
5063 r.gateway = in6addr_any;
5064 nm_ip6_config_add_route (composite, &r);
5067 END_ADD_DEFAULT_ROUTE:
5069 if (priv->default_route.v6_is_assumed) {
5070 /* If above does not explicitly assign a default route, we always pick up the
5071 * default route based on what is currently configured.
5072 * That means that even managed connections with never-default, can
5073 * get a default route (if configured externally).
5075 priv->default_route.v6_has = _device_get_default_route_from_platform (self, AF_INET6, (NMPlatformIPRoute *) &priv->default_route.v6);
5078 nm_ip6_config_addresses_sort (composite,
5079 priv->rdisc ? priv->rdisc_use_tempaddr : NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN);
5081 /* Allow setting MTU etc */
5083 if (NM_DEVICE_GET_CLASS (self)->ip6_config_pre_commit)
5084 NM_DEVICE_GET_CLASS (self)->ip6_config_pre_commit (self, composite);
5087 routes_full_sync = commit
5088 && priv->v6_commit_first_time
5089 && !nm_device_uses_assumed_connection (self);
5091 success = nm_device_set_ip6_config (self, composite, commit, routes_full_sync, out_reason);
5092 g_object_unref (composite);
5094 priv->v6_commit_first_time = FALSE;
5099 dhcp6_lease_change (NMDevice *self)
5101 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5102 NMSettingsConnection *settings_connection;
5103 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
5105 if (priv->dhcp6_ip6_config == NULL) {
5106 _LOGW (LOGD_DHCP6, "failed to get DHCPv6 config for rebind");
5107 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED);
5111 g_assert (priv->dhcp6_client); /* sanity check */
5113 settings_connection = nm_device_get_settings_connection (self);
5114 g_assert (settings_connection);
5116 /* Apply the updated config */
5117 if (ip6_config_merge_and_apply (self, TRUE, &reason) == FALSE) {
5118 _LOGW (LOGD_DHCP6, "failed to update IPv6 config in response to DHCP event.");
5119 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
5121 /* Notify dispatcher scripts of new DHCPv6 config */
5122 nm_dispatcher_call (DISPATCHER_ACTION_DHCP6_CHANGE,
5123 settings_connection,
5124 nm_device_get_applied_connection (self),
5125 self, NULL, NULL, NULL);
5130 dhcp6_restart_cb (gpointer user_data)
5132 NMDevice *self = user_data;
5133 NMDevicePrivate *priv;
5134 NMDeviceStateReason reason;
5136 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
5138 priv = NM_DEVICE_GET_PRIVATE (self);
5139 priv->dhcp6_restart_id = 0;
5141 if (!dhcp6_start (self, FALSE, &reason))
5142 priv->dhcp6_restart_id = g_timeout_add_seconds (120, dhcp6_restart_cb, self);
5148 dhcp6_fail (NMDevice *self, gboolean timeout)
5150 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5152 dhcp6_cleanup (self, CLEANUP_TYPE_DECONFIGURE, FALSE);
5154 if (priv->dhcp6_mode == NM_RDISC_DHCP_LEVEL_MANAGED) {
5155 /* Don't fail if there are static addresses configured on
5156 * the device, instead retry after some time.
5158 if ( priv->ip6_state == IP_DONE
5159 && priv->con_ip6_config
5160 && nm_ip6_config_get_num_addresses (priv->con_ip6_config)) {
5161 _LOGI (LOGD_DHCP6, "Scheduling DHCPv6 restart because device has IP addresses");
5162 priv->dhcp6_restart_id = g_timeout_add_seconds (120, dhcp6_restart_cb, self);
5166 /* Instead of letting an assumed connection fail (which means that the
5167 * device will transition to the ACTIVATED state without IP configuration),
5170 if (nm_device_uses_assumed_connection (self)) {
5171 _LOGI (LOGD_DHCP6, "Scheduling DHCPv6 restart because the connection is assumed");
5172 priv->dhcp6_restart_id = g_timeout_add_seconds (120, dhcp6_restart_cb, self);
5176 if (timeout || (priv->ip6_state == IP_CONF))
5177 nm_device_activate_schedule_ip6_config_timeout (self);
5178 else if (priv->ip6_state == IP_DONE)
5179 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED);
5181 g_warn_if_reached ();
5183 /* not a hard failure; just live with the RA info */
5184 if (priv->ip6_state == IP_CONF)
5185 nm_device_activate_schedule_ip6_config_result (self);
5190 dhcp6_timeout (NMDevice *self, NMDhcpClient *client)
5192 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5194 if (priv->dhcp6_mode == NM_RDISC_DHCP_LEVEL_MANAGED)
5195 dhcp6_fail (self, TRUE);
5197 /* not a hard failure; just live with the RA info */
5198 dhcp6_cleanup (self, CLEANUP_TYPE_DECONFIGURE, FALSE);
5199 if (priv->ip6_state == IP_CONF)
5200 nm_device_activate_schedule_ip6_config_result (self);
5205 dhcp6_state_changed (NMDhcpClient *client,
5207 NMIP6Config *ip6_config,
5208 GHashTable *options,
5209 const char *event_id,
5212 NMDevice *self = NM_DEVICE (user_data);
5213 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5216 g_return_if_fail (nm_dhcp_client_get_ipv6 (client) == TRUE);
5217 g_return_if_fail (!ip6_config || NM_IS_IP6_CONFIG (ip6_config));
5219 _LOGD (LOGD_DHCP6, "new DHCPv6 client state %d", state);
5222 case NM_DHCP_STATE_BOUND:
5223 /* If the server sends multiple IPv6 addresses, we receive a state
5224 * changed event for each of them. Use the event ID to merge IPv6
5225 * addresses from the same transaction into a single configuration.
5229 && priv->dhcp6_event_id
5230 && !strcmp (event_id, priv->dhcp6_event_id)) {
5231 for (i = 0; i < nm_ip6_config_get_num_addresses (ip6_config); i++) {
5232 nm_ip6_config_add_address (priv->dhcp6_ip6_config,
5233 nm_ip6_config_get_address (ip6_config, i));
5236 g_clear_object (&priv->dhcp6_ip6_config);
5237 g_clear_pointer (&priv->dhcp6_event_id, g_free);
5239 priv->dhcp6_ip6_config = g_object_ref (ip6_config);
5240 priv->dhcp6_event_id = g_strdup (event_id);
5241 nm_dhcp6_config_set_options (priv->dhcp6_config, options);
5242 _notify (self, PROP_DHCP6_CONFIG);
5246 if (priv->ip6_state == IP_CONF) {
5247 if (priv->dhcp6_ip6_config == NULL) {
5248 /* FIXME: Initial DHCP failed; should we fail IPv6 entirely then? */
5249 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_DHCP_FAILED);
5252 nm_device_activate_schedule_ip6_config_result (self);
5253 } else if (priv->ip6_state == IP_DONE)
5254 dhcp6_lease_change (self);
5256 case NM_DHCP_STATE_TIMEOUT:
5257 dhcp6_timeout (self, client);
5259 case NM_DHCP_STATE_EXPIRE:
5260 /* Ignore expiry before we even have a lease (NAK, old lease, etc) */
5261 if (priv->ip6_state != IP_CONF)
5262 dhcp6_fail (self, FALSE);
5264 case NM_DHCP_STATE_DONE:
5265 /* In IPv6 info-only mode, the client doesn't handle leases so it
5266 * may exit right after getting a response from the server. That's
5267 * normal. In that case we just ignore the exit.
5269 if (priv->dhcp6_mode == NM_RDISC_DHCP_LEVEL_OTHERCONF)
5271 /* Otherwise, fall through */
5272 case NM_DHCP_STATE_FAIL:
5273 dhcp6_fail (self, FALSE);
5281 dhcp6_start_with_link_ready (NMDevice *self, NMConnection *connection)
5283 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5284 NMSettingIPConfig *s_ip6;
5285 GByteArray *tmp = NULL;
5286 const guint8 *hw_addr;
5287 size_t hw_addr_len = 0;
5288 const NMPlatformIP6Address *ll_addr = NULL;
5290 g_assert (connection);
5291 s_ip6 = nm_connection_get_setting_ip6_config (connection);
5294 hw_addr = nm_platform_link_get_address (NM_PLATFORM_GET, nm_device_get_ip_ifindex (self), &hw_addr_len);
5296 tmp = g_byte_array_sized_new (hw_addr_len);
5297 g_byte_array_append (tmp, hw_addr, hw_addr_len);
5300 if (priv->ext_ip6_config_captured)
5301 ll_addr = nm_ip6_config_get_address_first_nontentative (priv->ext_ip6_config_captured, TRUE);
5303 g_return_val_if_fail (ll_addr, FALSE);
5305 priv->dhcp6_client = nm_dhcp_manager_start_ip6 (nm_dhcp_manager_get (),
5306 nm_device_get_ip_iface (self),
5307 nm_device_get_ip_ifindex (self),
5310 nm_connection_get_uuid (connection),
5311 nm_device_get_ip6_route_metric (self),
5312 nm_setting_ip_config_get_dhcp_send_hostname (s_ip6),
5313 nm_setting_ip_config_get_dhcp_hostname (s_ip6),
5315 priv->dhcp_anycast_address,
5316 (priv->dhcp6_mode == NM_RDISC_DHCP_LEVEL_OTHERCONF) ? TRUE : FALSE,
5317 nm_setting_ip6_config_get_ip6_privacy (NM_SETTING_IP6_CONFIG (s_ip6)));
5319 g_byte_array_free (tmp, TRUE);
5321 if (priv->dhcp6_client) {
5322 priv->dhcp6_state_sigid = g_signal_connect (priv->dhcp6_client,
5323 NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED,
5324 G_CALLBACK (dhcp6_state_changed),
5328 return !!priv->dhcp6_client;
5332 dhcp6_start (NMDevice *self, gboolean wait_for_ll, NMDeviceStateReason *reason)
5334 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5335 NMConnection *connection;
5336 NMSettingIPConfig *s_ip6;
5338 nm_exported_object_clear_and_unexport (&priv->dhcp6_config);
5339 priv->dhcp6_config = nm_dhcp6_config_new ();
5341 g_warn_if_fail (priv->dhcp6_ip6_config == NULL);
5342 g_clear_object (&priv->dhcp6_ip6_config);
5343 g_clear_pointer (&priv->dhcp6_event_id, g_free);
5345 connection = nm_device_get_applied_connection (self);
5346 g_assert (connection);
5347 s_ip6 = nm_connection_get_setting_ip6_config (connection);
5348 if (!nm_setting_ip_config_get_may_fail (s_ip6) ||
5349 !strcmp (nm_setting_ip_config_get_method (s_ip6), NM_SETTING_IP6_CONFIG_METHOD_DHCP))
5350 nm_device_add_pending_action (self, PENDING_ACTION_DHCP6, TRUE);
5353 NMActStageReturn ret;
5355 /* ensure link local is ready... */
5356 ret = linklocal6_start (self);
5357 if (ret == NM_ACT_STAGE_RETURN_POSTPONE) {
5358 /* success; wait for the LL address to show up */
5362 /* success; already have the LL address; kick off DHCP */
5363 g_assert (ret == NM_ACT_STAGE_RETURN_SUCCESS || ret == NM_ACT_STAGE_RETURN_FINISH);
5366 if (!dhcp6_start_with_link_ready (self, connection)) {
5367 *reason = NM_DEVICE_STATE_REASON_DHCP_START_FAILED;
5375 nm_device_dhcp6_renew (NMDevice *self, gboolean release)
5377 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5379 g_return_val_if_fail (priv->dhcp6_client != NULL, FALSE);
5381 _LOGI (LOGD_DHCP6, "DHCPv6 lease renewal requested");
5383 /* Terminate old DHCP instance and release the old lease */
5384 dhcp6_cleanup (self, CLEANUP_TYPE_DECONFIGURE, release);
5386 /* Start DHCP again on the interface */
5387 return dhcp6_start (self, FALSE, NULL);
5390 /******************************************/
5393 linklocal6_cleanup (NMDevice *self)
5395 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5397 nm_clear_g_source (&priv->linklocal6_timeout_id);
5401 linklocal6_failed (NMDevice *self)
5403 linklocal6_cleanup (self);
5404 nm_device_activate_schedule_ip6_config_timeout (self);
5408 linklocal6_timeout_cb (gpointer user_data)
5410 NMDevice *self = user_data;
5412 _LOGD (LOGD_DEVICE, "linklocal6: waiting for link-local addresses failed due to timeout");
5413 linklocal6_failed (self);
5414 return G_SOURCE_REMOVE;
5418 linklocal6_complete (NMDevice *self)
5420 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5421 NMConnection *connection;
5424 g_assert (priv->linklocal6_timeout_id);
5425 g_assert (nm_ip6_config_get_address_first_nontentative (priv->ip6_config, TRUE));
5427 linklocal6_cleanup (self);
5429 connection = nm_device_get_applied_connection (self);
5430 g_assert (connection);
5432 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG);
5434 _LOGD (LOGD_DEVICE, "linklocal6: waiting for link-local addresses successful, continue with method %s", method);
5436 if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0) {
5437 if (!addrconf6_start_with_link_ready (self)) {
5438 /* Time out IPv6 instead of failing the entire activation */
5439 nm_device_activate_schedule_ip6_config_timeout (self);
5441 } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0) {
5442 if (!dhcp6_start_with_link_ready (self, connection)) {
5443 /* Time out IPv6 instead of failing the entire activation */
5444 nm_device_activate_schedule_ip6_config_timeout (self);
5446 } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0)
5447 nm_device_activate_schedule_ip6_config_result (self);
5449 g_return_if_fail (FALSE);
5453 check_and_add_ipv6ll_addr (NMDevice *self)
5455 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5456 int ip_ifindex = nm_device_get_ip_ifindex (self);
5457 struct in6_addr lladdr;
5459 NMConnection *connection;
5460 NMSettingIP6Config *s_ip6 = NULL;
5461 GError *error = NULL;
5463 if (priv->nm_ipv6ll == FALSE)
5466 if (priv->ip6_config) {
5467 n = nm_ip6_config_get_num_addresses (priv->ip6_config);
5468 for (i = 0; i < n; i++) {
5469 const NMPlatformIP6Address *addr;
5471 addr = nm_ip6_config_get_address (priv->ip6_config, i);
5472 if ( IN6_IS_ADDR_LINKLOCAL (&addr->address)
5473 && !(addr->n_ifa_flags & IFA_F_DADFAILED)) {
5474 /* Already have an LL address, nothing to do */
5480 memset (&lladdr, 0, sizeof (lladdr));
5481 lladdr.s6_addr16[0] = htons (0xfe80);
5483 connection = nm_device_get_applied_connection (self);
5485 s_ip6 = NM_SETTING_IP6_CONFIG (nm_connection_get_setting_ip6_config (connection));
5487 if (s_ip6 && nm_setting_ip6_config_get_addr_gen_mode (s_ip6) == NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_STABLE_PRIVACY) {
5488 if (!nm_utils_ipv6_addr_set_stable_privacy (&lladdr,
5489 nm_device_get_iface (self),
5490 nm_connection_get_uuid (connection),
5491 priv->linklocal6_dad_counter++,
5493 _LOGW (LOGD_IP6, "linklocal6: failed to generate an address: %s", error->message);
5494 g_clear_error (&error);
5495 linklocal6_failed (self);
5498 _LOGD (LOGD_IP6, "linklocal6: using IPv6 stable-privacy addressing");
5500 NMUtilsIPv6IfaceId iid;
5502 if (priv->linklocal6_timeout_id) {
5503 /* We already started and attempt to add a LL address. For the EUI-64
5504 * mode we can't pick a new one, we'll just fail. */
5505 _LOGW (LOGD_IP6, "linklocal6: DAD failed for an EUI-64 address");
5506 linklocal6_failed (self);
5510 if (!nm_device_get_ip_iface_identifier (self, &iid)) {
5511 _LOGW (LOGD_IP6, "linklocal6: failed to get interface identifier; IPv6 cannot continue");
5514 _LOGD (LOGD_IP6, "linklocal6: using EUI-64 identifier to generate IPv6LL address");
5516 nm_utils_ipv6_addr_set_interface_identfier (&lladdr, iid);
5519 _LOGD (LOGD_IP6, "linklocal6: adding IPv6LL address %s", nm_utils_inet6_ntop (&lladdr, NULL));
5520 if (!nm_platform_ip6_address_add (NM_PLATFORM_GET,
5525 NM_PLATFORM_LIFETIME_PERMANENT,
5526 NM_PLATFORM_LIFETIME_PERMANENT,
5528 _LOGW (LOGD_IP6, "failed to add IPv6 link-local address %s",
5529 nm_utils_inet6_ntop (&lladdr, NULL));
5533 static NMActStageReturn
5534 linklocal6_start (NMDevice *self)
5536 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5537 NMConnection *connection;
5540 linklocal6_cleanup (self);
5542 if ( priv->ip6_config
5543 && nm_ip6_config_get_address_first_nontentative (priv->ip6_config, TRUE))
5544 return NM_ACT_STAGE_RETURN_FINISH;
5546 connection = nm_device_get_applied_connection (self);
5547 g_assert (connection);
5549 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG);
5550 _LOGD (LOGD_DEVICE, "linklocal6: starting IPv6 with method '%s', but the device has no link-local addresses configured. Wait.", method);
5552 check_and_add_ipv6ll_addr (self);
5554 /* Depending on the network and what the 'dad_transmits' and 'retrans_time_ms'
5555 * sysctl values are, DAD for the IPv6LL address may take quite a while.
5556 * FIXME: use dad/retrans sysctl values if they are higher than a minimum time.
5559 priv->linklocal6_timeout_id = g_timeout_add_seconds (15, linklocal6_timeout_cb, self);
5561 return NM_ACT_STAGE_RETURN_POSTPONE;
5564 /******************************************/
5566 static void nm_device_ipv6_set_mtu (NMDevice *self, guint32 mtu);
5569 nm_device_set_mtu (NMDevice *self, guint32 mtu)
5571 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5572 int ifindex = nm_device_get_ip_ifindex (self);
5577 /* Ensure the IPv6 MTU is still alright. */
5579 nm_device_ipv6_set_mtu (self, priv->ip6_mtu);
5581 if (priv->mtu && priv->mtu != nm_platform_link_get_mtu (NM_PLATFORM_GET, ifindex))
5582 nm_platform_link_set_mtu (NM_PLATFORM_GET, ifindex, priv->mtu);
5586 nm_device_ipv6_set_mtu (NMDevice *self, guint32 mtu)
5588 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5589 guint32 plat_mtu = nm_device_ipv6_sysctl_get_int32 (self, "mtu", priv->mtu);
5592 priv->ip6_mtu = mtu ?: plat_mtu;
5594 if (priv->ip6_mtu && priv->mtu && priv->mtu < priv->ip6_mtu) {
5595 _LOGI (LOGD_DEVICE | LOGD_IP6, "Lowering IPv6 MTU (%d) to match device MTU (%d)",
5596 priv->ip6_mtu, priv->mtu);
5597 priv->ip6_mtu = priv->mtu;
5600 if (priv->ip6_mtu && priv->ip6_mtu < 1280) {
5601 _LOGI (LOGD_DEVICE | LOGD_IP6, "IPv6 MTU (%d) smaller than 1280, adjusting",
5603 priv->ip6_mtu = 1280;
5606 if (priv->ip6_mtu && priv->mtu && priv->mtu < priv->ip6_mtu) {
5607 _LOGI (LOGD_DEVICE | LOGD_IP6, "Raising device MTU (%d) to match IPv6 MTU (%d)",
5608 priv->mtu, priv->ip6_mtu);
5609 nm_device_set_mtu (self, priv->ip6_mtu);
5612 if (priv->ip6_mtu != plat_mtu) {
5613 g_snprintf (val, sizeof (val), "%d", mtu);
5614 nm_device_ipv6_sysctl_set (self, "mtu", val);
5619 rdisc_config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, NMDevice *self)
5621 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5624 guint32 ifa_flags = 0x00;
5627 * Check, whether kernel is recent enough to help user space handling RA.
5628 * If it's not supported, we have no ipv6-privacy and must add autoconf
5629 * addresses as /128. The reason for the /128 is to prevent the kernel
5630 * from adding a prefix route for this address.
5632 system_support = nm_platform_check_support_kernel_extended_ifa_flags (NM_PLATFORM_GET);
5635 ifa_flags = IFA_F_NOPREFIXROUTE;
5636 if ( priv->rdisc_use_tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR
5637 || priv->rdisc_use_tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR)
5639 /* without system_support, this flag will be ignored. Still set it, doesn't seem to do any harm. */
5640 ifa_flags |= IFA_F_MANAGETEMPADDR;
5643 g_return_if_fail (priv->act_request);
5645 if (!priv->ac_ip6_config)
5646 priv->ac_ip6_config = nm_ip6_config_new (nm_device_get_ip_ifindex (self));
5648 if (changed & NM_RDISC_CONFIG_GATEWAYS) {
5649 /* Use the first gateway as ordered in router discovery cache. */
5650 if (rdisc->gateways->len) {
5651 NMRDiscGateway *gateway = &g_array_index (rdisc->gateways, NMRDiscGateway, 0);
5653 nm_ip6_config_set_gateway (priv->ac_ip6_config, &gateway->address);
5655 nm_ip6_config_set_gateway (priv->ac_ip6_config, NULL);
5658 if (changed & NM_RDISC_CONFIG_ADDRESSES) {
5659 /* Rebuild address list from router discovery cache. */
5660 nm_ip6_config_reset_addresses (priv->ac_ip6_config);
5662 /* rdisc->addresses contains at most max_addresses entries.
5663 * This is different from what the kernel does, which
5664 * also counts static and temporary addresses when checking
5667 for (i = 0; i < rdisc->addresses->len; i++) {
5668 NMRDiscAddress *discovered_address = &g_array_index (rdisc->addresses, NMRDiscAddress, i);
5669 NMPlatformIP6Address address;
5671 memset (&address, 0, sizeof (address));
5672 address.address = discovered_address->address;
5673 address.plen = system_support ? 64 : 128;
5674 address.timestamp = discovered_address->timestamp;
5675 address.lifetime = discovered_address->lifetime;
5676 address.preferred = discovered_address->preferred;
5677 if (address.preferred > address.lifetime)
5678 address.preferred = address.lifetime;
5679 address.source = NM_IP_CONFIG_SOURCE_RDISC;
5680 address.n_ifa_flags = ifa_flags;
5682 nm_ip6_config_add_address (priv->ac_ip6_config, &address);
5686 if (changed & NM_RDISC_CONFIG_ROUTES) {
5687 /* Rebuild route list from router discovery cache. */
5688 nm_ip6_config_reset_routes (priv->ac_ip6_config);
5690 for (i = 0; i < rdisc->routes->len; i++) {
5691 NMRDiscRoute *discovered_route = &g_array_index (rdisc->routes, NMRDiscRoute, i);
5692 NMPlatformIP6Route route;
5694 /* Only accept non-default routes. The router has no idea what the
5695 * local configuration or user preferences are, so sending routes
5696 * with a prefix length of 0 is quite rude and thus ignored.
5698 if (discovered_route->plen > 0) {
5699 memset (&route, 0, sizeof (route));
5700 route.network = discovered_route->network;
5701 route.plen = discovered_route->plen;
5702 route.gateway = discovered_route->gateway;
5703 route.source = NM_IP_CONFIG_SOURCE_RDISC;
5704 route.metric = nm_device_get_ip6_route_metric (self);
5706 nm_ip6_config_add_route (priv->ac_ip6_config, &route);
5711 if (changed & NM_RDISC_CONFIG_DNS_SERVERS) {
5712 /* Rebuild DNS server list from router discovery cache. */
5713 nm_ip6_config_reset_nameservers (priv->ac_ip6_config);
5715 for (i = 0; i < rdisc->dns_servers->len; i++) {
5716 NMRDiscDNSServer *discovered_server = &g_array_index (rdisc->dns_servers, NMRDiscDNSServer, i);
5718 nm_ip6_config_add_nameserver (priv->ac_ip6_config, &discovered_server->address);
5722 if (changed & NM_RDISC_CONFIG_DNS_DOMAINS) {
5723 /* Rebuild domain list from router discovery cache. */
5724 nm_ip6_config_reset_domains (priv->ac_ip6_config);
5726 for (i = 0; i < rdisc->dns_domains->len; i++) {
5727 NMRDiscDNSDomain *discovered_domain = &g_array_index (rdisc->dns_domains, NMRDiscDNSDomain, i);
5729 nm_ip6_config_add_domain (priv->ac_ip6_config, discovered_domain->domain);
5733 if (changed & NM_RDISC_CONFIG_DHCP_LEVEL) {
5734 dhcp6_cleanup (self, CLEANUP_TYPE_DECONFIGURE, TRUE);
5736 priv->dhcp6_mode = rdisc->dhcp_level;
5737 if (priv->dhcp6_mode != NM_RDISC_DHCP_LEVEL_NONE) {
5738 NMDeviceStateReason reason;
5740 _LOGD (LOGD_DEVICE | LOGD_DHCP6,
5741 "Activation: Stage 3 of 5 (IP Configure Start) starting DHCPv6"
5742 " as requested by IPv6 router...");
5743 if (!dhcp6_start (self, FALSE, &reason)) {
5744 if (priv->dhcp6_mode == NM_RDISC_DHCP_LEVEL_MANAGED) {
5745 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
5752 if (changed & NM_RDISC_CONFIG_HOP_LIMIT)
5753 nm_platform_sysctl_set_ip6_hop_limit_safe (NM_PLATFORM_GET, nm_device_get_ip_iface (self), rdisc->hop_limit);
5755 if (changed & NM_RDISC_CONFIG_MTU)
5756 priv->ip6_mtu = rdisc->mtu;
5758 nm_device_activate_schedule_ip6_config_result (self);
5762 rdisc_ra_timeout (NMRDisc *rdisc, NMDevice *self)
5764 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5766 /* We don't want to stop listening for router advertisements completely,
5767 * but instead let device activation continue activating. If an RA
5768 * shows up later, we'll use it as long as the device is not disconnected.
5771 _LOGD (LOGD_IP6, "timed out waiting for IPv6 router advertisement");
5772 if (priv->ip6_state == IP_CONF) {
5773 /* If RA is our only source of addressing information and we don't
5774 * ever receive one, then time out IPv6. But if there is other
5775 * IPv6 configuration, like manual IPv6 addresses or external IPv6
5776 * config, consider that sufficient for IPv6 success.
5778 if ( priv->ip6_config
5779 && nm_ip6_config_get_address_first_nontentative (priv->ip6_config, FALSE))
5780 nm_device_activate_schedule_ip6_config_result (self);
5782 nm_device_activate_schedule_ip6_config_timeout (self);
5787 addrconf6_start_with_link_ready (NMDevice *self)
5789 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5790 NMUtilsIPv6IfaceId iid;
5792 g_assert (priv->rdisc);
5794 if (nm_platform_link_get_ipv6_token (NM_PLATFORM_GET, priv->ifindex, &iid)) {
5795 _LOGD (LOGD_IP6, "addrconf6: IPv6 tokenized identifier present");
5796 nm_rdisc_set_iid (priv->rdisc, iid);
5797 } else if (nm_device_get_ip_iface_identifier (self, &iid)) {
5798 _LOGD (LOGD_IP6, "addrconf6: using the device EUI-64 identifier");
5799 nm_rdisc_set_iid (priv->rdisc, iid);
5801 /* Don't abort the addrconf at this point -- if rdisc needs the iid
5802 * it will notice this itself. */
5803 _LOGI (LOGD_IP6, "addrconf6: no interface identifier; IPv6 adddress creation may fail");
5806 /* Apply any manual configuration before starting RA */
5807 if (!ip6_config_merge_and_apply (self, TRUE, NULL))
5808 _LOGW (LOGD_IP6, "failed to apply manual IPv6 configuration");
5810 nm_device_ipv6_sysctl_set (self, "accept_ra", "1");
5811 nm_device_ipv6_sysctl_set (self, "accept_ra_defrtr", "0");
5812 nm_device_ipv6_sysctl_set (self, "accept_ra_pinfo", "0");
5813 nm_device_ipv6_sysctl_set (self, "accept_ra_rtr_pref", "0");
5815 priv->rdisc_changed_id = g_signal_connect (priv->rdisc,
5816 NM_RDISC_CONFIG_CHANGED,
5817 G_CALLBACK (rdisc_config_changed),
5819 priv->rdisc_timeout_id = g_signal_connect (priv->rdisc,
5820 NM_RDISC_RA_TIMEOUT,
5821 G_CALLBACK (rdisc_ra_timeout),
5824 nm_rdisc_start (priv->rdisc);
5829 addrconf6_start (NMDevice *self, NMSettingIP6ConfigPrivacy use_tempaddr)
5831 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5832 NMConnection *connection;
5833 NMActStageReturn ret;
5834 NMSettingIP6Config *s_ip6 = NULL;
5835 GError *error = NULL;
5837 connection = nm_device_get_applied_connection (self);
5838 g_assert (connection);
5840 g_warn_if_fail (priv->ac_ip6_config == NULL);
5841 if (priv->ac_ip6_config) {
5842 g_object_unref (priv->ac_ip6_config);
5843 priv->ac_ip6_config = NULL;
5846 s_ip6 = NM_SETTING_IP6_CONFIG (nm_connection_get_setting_ip6_config (connection));
5849 priv->rdisc = nm_lndp_rdisc_new (NM_PLATFORM_GET,
5850 nm_device_get_ip_ifindex (self),
5851 nm_device_get_ip_iface (self),
5852 nm_connection_get_uuid (connection),
5853 nm_setting_ip6_config_get_addr_gen_mode (s_ip6),
5856 _LOGE (LOGD_IP6, "addrconf6: failed to start router discovery: %s", error->message);
5857 g_error_free (error);
5861 priv->rdisc_use_tempaddr = use_tempaddr;
5863 if ( NM_IN_SET (use_tempaddr, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR)
5864 && !nm_platform_check_support_kernel_extended_ifa_flags (NM_PLATFORM_GET)) {
5865 _LOGW (LOGD_IP6, "The kernel does not support extended IFA_FLAGS needed by NM for "
5866 "IPv6 private addresses. This feature is not available");
5869 if (!nm_setting_ip_config_get_may_fail (nm_connection_get_setting_ip6_config (connection)))
5870 nm_device_add_pending_action (self, PENDING_ACTION_AUTOCONF6, TRUE);
5872 /* ensure link local is ready... */
5873 ret = linklocal6_start (self);
5874 if (ret == NM_ACT_STAGE_RETURN_POSTPONE) {
5875 /* success; wait for the LL address to show up */
5879 /* success; already have the LL address; kick off router discovery */
5880 g_assert (ret == NM_ACT_STAGE_RETURN_SUCCESS || ret == NM_ACT_STAGE_RETURN_FINISH);
5881 return addrconf6_start_with_link_ready (self);
5885 addrconf6_cleanup (NMDevice *self)
5887 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5889 nm_clear_g_signal_handler (priv->rdisc, &priv->rdisc_changed_id);
5890 nm_clear_g_signal_handler (priv->rdisc, &priv->rdisc_timeout_id);
5892 nm_device_remove_pending_action (self, PENDING_ACTION_AUTOCONF6, FALSE);
5894 g_clear_object (&priv->ac_ip6_config);
5895 g_clear_object (&priv->rdisc);
5898 /******************************************/
5900 static const char *ip6_properties_to_save[] = {
5904 "accept_ra_rtr_pref",
5911 save_ip6_properties (NMDevice *self)
5913 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5914 const char *ifname = nm_device_get_ip_iface (self);
5918 g_hash_table_remove_all (priv->ip6_saved_properties);
5920 for (i = 0; i < G_N_ELEMENTS (ip6_properties_to_save); i++) {
5921 value = nm_platform_sysctl_get (NM_PLATFORM_GET, nm_utils_ip6_property_path (ifname, ip6_properties_to_save[i]));
5923 g_hash_table_insert (priv->ip6_saved_properties,
5924 (char *) ip6_properties_to_save[i],
5931 restore_ip6_properties (NMDevice *self)
5933 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5934 GHashTableIter iter;
5935 gpointer key, value;
5937 g_hash_table_iter_init (&iter, priv->ip6_saved_properties);
5938 while (g_hash_table_iter_next (&iter, &key, &value)) {
5939 /* Don't touch "disable_ipv6" if we're doing userland IPv6LL */
5940 if (priv->nm_ipv6ll && strcmp (key, "disable_ipv6") == 0)
5942 nm_device_ipv6_sysctl_set (self, key, value);
5947 set_disable_ipv6 (NMDevice *self, const char *value)
5949 /* We only touch disable_ipv6 when NM is not managing the IPv6LL address */
5950 if (NM_DEVICE_GET_PRIVATE (self)->nm_ipv6ll == FALSE)
5951 nm_device_ipv6_sysctl_set (self, "disable_ipv6", value);
5955 set_nm_ipv6ll (NMDevice *self, gboolean enable)
5957 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
5958 int ifindex = nm_device_get_ip_ifindex (self);
5961 if (!nm_platform_check_support_user_ipv6ll (NM_PLATFORM_GET))
5964 priv->nm_ipv6ll = enable;
5966 const char *detail = enable ? "enable" : "disable";
5968 _LOGD (LOGD_IP6, "will %s userland IPv6LL", detail);
5969 if (!nm_platform_link_set_user_ipv6ll_enabled (NM_PLATFORM_GET, ifindex, enable))
5970 _LOGW (LOGD_IP6, "failed to %s userspace IPv6LL address handling", detail);
5973 /* Bounce IPv6 to ensure the kernel stops IPv6LL address generation */
5974 value = nm_platform_sysctl_get (NM_PLATFORM_GET,
5975 nm_utils_ip6_property_path (nm_device_get_ip_iface (self), "disable_ipv6"));
5976 if (g_strcmp0 (value, "0") == 0)
5977 nm_device_ipv6_sysctl_set (self, "disable_ipv6", "1");
5980 /* Ensure IPv6 is enabled */
5981 nm_device_ipv6_sysctl_set (self, "disable_ipv6", "0");
5987 /************************************************************************/
5989 static NMSettingIP6ConfigPrivacy
5990 _ip6_privacy_clamp (NMSettingIP6ConfigPrivacy use_tempaddr)
5992 switch (use_tempaddr) {
5993 case NM_SETTING_IP6_CONFIG_PRIVACY_DISABLED:
5994 case NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR:
5995 case NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR:
5996 return use_tempaddr;
5998 return NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN;
6002 static NMSettingIP6ConfigPrivacy
6003 _ip6_privacy_get (NMDevice *self)
6005 NMSettingIP6ConfigPrivacy ip6_privacy;
6006 gs_free char *value = NULL;
6007 NMConnection *connection;
6009 g_return_val_if_fail (self, NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN);
6011 /* 1.) First look at the per-connection setting. If it is not -1 (unknown),
6013 connection = nm_device_get_applied_connection (self);
6015 NMSettingIPConfig *s_ip6 = nm_connection_get_setting_ip6_config (connection);
6018 ip6_privacy = nm_setting_ip6_config_get_ip6_privacy (NM_SETTING_IP6_CONFIG (s_ip6));
6019 ip6_privacy = _ip6_privacy_clamp (ip6_privacy);
6020 if (ip6_privacy != NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN)
6025 value = nm_config_data_get_connection_default (NM_CONFIG_GET_DATA,
6026 "ipv6.ip6-privacy", self);
6028 /* 2.) use the default value from the configuration. */
6029 ip6_privacy = _nm_utils_ascii_str_to_int64 (value, 10,
6030 NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN,
6031 NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR,
6032 NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN);
6033 if (ip6_privacy != NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN)
6036 /* 3.) No valid default-value configured. Fallback to reading sysctl.
6038 * Instead of reading static config files in /etc, just read the current sysctl value.
6039 * This works as NM only writes to "/proc/sys/net/ipv6/conf/IFNAME/use_tempaddr", but leaves
6040 * the "default" entry untouched. */
6041 ip6_privacy = nm_platform_sysctl_get_int32 (NM_PLATFORM_GET, "/proc/sys/net/ipv6/conf/default/use_tempaddr", NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN);
6042 return _ip6_privacy_clamp (ip6_privacy);
6045 /****************************************************************/
6048 ip6_requires_slaves (NMConnection *connection)
6052 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG);
6054 /* SLAAC, DHCP, and Link-Local depend on connectivity (and thus slaves)
6055 * to complete addressing. SLAAC and DHCP obviously need a peer to
6056 * provide a prefix, while Link-Local must perform DAD on the local link.
6058 return strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0
6059 || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0
6060 || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0;
6063 static NMActStageReturn
6064 act_stage3_ip6_config_start (NMDevice *self,
6065 NMIP6Config **out_config,
6066 NMDeviceStateReason *reason)
6068 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6069 NMActStageReturn ret = NM_ACT_STAGE_RETURN_FAILURE;
6070 NMConnection *connection;
6072 NMSettingIP6ConfigPrivacy ip6_privacy = NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN;
6073 const char *ip6_privacy_str = "0\n";
6075 gboolean ready_slaves;
6077 g_return_val_if_fail (reason != NULL, NM_ACT_STAGE_RETURN_FAILURE);
6079 connection = nm_device_get_applied_connection (self);
6080 g_assert (connection);
6082 if ( connection_ip6_method_requires_carrier (connection, NULL)
6084 && !priv->carrier) {
6085 _LOGI (LOGD_IP6 | LOGD_DEVICE,
6086 "IPv6 config waiting until carrier is on");
6087 return NM_ACT_STAGE_RETURN_WAIT;
6090 if (priv->is_master && ip6_requires_slaves (connection)) {
6091 /* If the master has no ready slaves, and depends on slaves for
6092 * a successful IPv6 attempt, then postpone IPv6 addressing.
6094 slaves = nm_device_master_get_slaves (self);
6095 ready_slaves = NM_DEVICE_GET_CLASS (self)->have_any_ready_slaves (self, slaves);
6096 g_slist_free (slaves);
6098 if (ready_slaves == FALSE) {
6099 _LOGI (LOGD_DEVICE | LOGD_IP6,
6100 "IPv6 config waiting until slaves are ready");
6101 return NM_ACT_STAGE_RETURN_WAIT;
6105 priv->dhcp6_mode = NM_RDISC_DHCP_LEVEL_NONE;
6107 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG);
6109 if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0) {
6110 if (!priv->master) {
6111 gboolean old_nm_ipv6ll = priv->nm_ipv6ll;
6113 /* When activating an IPv6 'ignore' connection we need to revert back
6114 * to kernel IPv6LL, but the kernel won't actually assign an address
6115 * to the interface until disable_ipv6 is bounced.
6117 set_nm_ipv6ll (self, FALSE);
6118 if (old_nm_ipv6ll == TRUE)
6119 nm_device_ipv6_sysctl_set (self, "disable_ipv6", "1");
6120 restore_ip6_properties (self);
6122 return NM_ACT_STAGE_RETURN_STOP;
6125 /* Ensure the MTU makes sense. If it was below 1280 the kernel would not
6126 * expose any ipv6 sysctls or allow presence of any addresses on the interface,
6127 * including LL, which * would make it impossible to autoconfigure MTU to a
6129 if (!nm_device_uses_assumed_connection (self))
6130 nm_device_ipv6_set_mtu (self, priv->ip6_mtu);
6132 /* Any method past this point requires an IPv6LL address. Use NM-controlled
6133 * IPv6LL if this is not an assumed connection, since assumed connections
6134 * will already have IPv6 set up.
6136 if (!nm_device_uses_assumed_connection (self))
6137 set_nm_ipv6ll (self, TRUE);
6139 /* Re-enable IPv6 on the interface */
6140 set_disable_ipv6 (self, "0");
6142 ip6_privacy = _ip6_privacy_get (self);
6144 if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0) {
6145 if (!addrconf6_start (self, ip6_privacy)) {
6146 /* IPv6 might be disabled; allow IPv4 to proceed */
6147 ret = NM_ACT_STAGE_RETURN_STOP;
6149 ret = NM_ACT_STAGE_RETURN_POSTPONE;
6150 } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0) {
6151 ret = linklocal6_start (self);
6152 } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0) {
6153 priv->dhcp6_mode = NM_RDISC_DHCP_LEVEL_MANAGED;
6154 if (!dhcp6_start (self, TRUE, reason)) {
6155 /* IPv6 might be disabled; allow IPv4 to proceed */
6156 ret = NM_ACT_STAGE_RETURN_STOP;
6158 ret = NM_ACT_STAGE_RETURN_POSTPONE;
6159 } else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0) {
6160 /* New blank config */
6161 *out_config = nm_ip6_config_new (nm_device_get_ip_ifindex (self));
6162 g_assert (*out_config);
6164 ret = NM_ACT_STAGE_RETURN_SUCCESS;
6166 _LOGW (LOGD_IP6, "unhandled IPv6 config method '%s'; will fail", method);
6168 /* Other methods (shared) aren't implemented yet */
6170 switch (ip6_privacy) {
6171 case NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN:
6172 case NM_SETTING_IP6_CONFIG_PRIVACY_DISABLED:
6173 ip6_privacy_str = "0";
6175 case NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR:
6176 ip6_privacy_str = "1";
6178 case NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR:
6179 ip6_privacy_str = "2";
6182 nm_device_ipv6_sysctl_set (self, "use_tempaddr", ip6_privacy_str);
6188 * nm_device_activate_stage3_ip4_start:
6191 * Try starting IPv4 configuration.
6194 nm_device_activate_stage3_ip4_start (NMDevice *self)
6196 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6197 NMActStageReturn ret;
6198 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
6199 NMIP4Config *ip4_config = NULL;
6201 g_assert (priv->ip4_state == IP_WAIT);
6203 priv->ip4_state = IP_CONF;
6204 ret = NM_DEVICE_GET_CLASS (self)->act_stage3_ip4_config_start (self, &ip4_config, &reason);
6205 if (ret == NM_ACT_STAGE_RETURN_SUCCESS) {
6206 g_assert (ip4_config);
6207 nm_device_activate_schedule_ip4_config_result (self, ip4_config);
6208 g_object_unref (ip4_config);
6209 } else if (ret == NM_ACT_STAGE_RETURN_FAILURE) {
6210 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
6212 } else if (ret == NM_ACT_STAGE_RETURN_STOP) {
6214 priv->ip4_state = IP_FAIL;
6215 } else if (ret == NM_ACT_STAGE_RETURN_WAIT) {
6216 /* Wait for something to try IP config again */
6217 priv->ip4_state = IP_WAIT;
6219 g_assert (ret == NM_ACT_STAGE_RETURN_POSTPONE);
6225 * nm_device_activate_stage3_ip6_start:
6228 * Try starting IPv6 configuration.
6231 nm_device_activate_stage3_ip6_start (NMDevice *self)
6233 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6234 NMActStageReturn ret;
6235 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
6236 NMIP6Config *ip6_config = NULL;
6238 g_assert (priv->ip6_state == IP_WAIT);
6240 priv->ip6_state = IP_CONF;
6241 ret = NM_DEVICE_GET_CLASS (self)->act_stage3_ip6_config_start (self, &ip6_config, &reason);
6242 if (ret == NM_ACT_STAGE_RETURN_SUCCESS) {
6243 g_assert (ip6_config);
6244 /* Here we get a static IPv6 config, like for Shared where it's
6245 * autogenerated or from modems where it comes from ModemManager.
6247 g_warn_if_fail (priv->ac_ip6_config == NULL);
6248 priv->ac_ip6_config = ip6_config;
6249 nm_device_activate_schedule_ip6_config_result (self);
6250 } else if (ret == NM_ACT_STAGE_RETURN_FAILURE) {
6251 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
6253 } else if (ret == NM_ACT_STAGE_RETURN_STOP) {
6254 /* Activation not wanted */
6255 priv->ip6_state = IP_FAIL;
6256 } else if (ret == NM_ACT_STAGE_RETURN_FINISH) {
6257 /* Early finish, nothing more to do */
6258 priv->ip6_state = IP_DONE;
6259 check_ip_done (self);
6260 } else if (ret == NM_ACT_STAGE_RETURN_WAIT) {
6261 /* Wait for something to try IP config again */
6262 priv->ip6_state = IP_WAIT;
6264 g_assert (ret == NM_ACT_STAGE_RETURN_POSTPONE);
6270 * activate_stage3_ip_config_start
6272 * Begin automatic/manual IP configuration
6276 activate_stage3_ip_config_start (NMDevice *self)
6278 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6279 NMActiveConnection *master;
6280 NMDevice *master_device;
6282 priv->ip4_state = priv->ip6_state = IP_WAIT;
6284 nm_device_state_changed (self, NM_DEVICE_STATE_IP_CONFIG, NM_DEVICE_STATE_REASON_NONE);
6286 /* Device should be up before we can do anything with it */
6287 if (!nm_platform_link_is_up (NM_PLATFORM_GET, nm_device_get_ip_ifindex (self)))
6288 _LOGW (LOGD_DEVICE, "interface %s not up for IP configuration", nm_device_get_ip_iface (self));
6290 /* If the device is a slave, then we don't do any IP configuration but we
6291 * use the IP config stage to indicate to the master we're ready for
6292 * enslavement. If the master is already activating, it will have tried to
6293 * enslave us when we changed state to IP_CONFIG, causing us to queue a
6294 * transition to SECONDARIES (or FAILED if the enslavement failed), with
6295 * our IP states set to IP_DONE either way. If the master isn't yet
6296 * activating, then they'll still be in IP_WAIT. Either way, we bail out
6297 * of IP config here.
6299 master = nm_active_connection_get_master (NM_ACTIVE_CONNECTION (priv->act_request));
6301 master_device = nm_active_connection_get_device (master);
6302 if (priv->ip4_state == IP_WAIT && priv->ip6_state == IP_WAIT) {
6303 _LOGI (LOGD_DEVICE, "Activation: connection '%s' waiting on master '%s'",
6304 nm_connection_get_id (nm_device_get_applied_connection (self)),
6305 master_device ? nm_device_get_iface (master_device) : "(unknown)");
6311 if ( nm_device_activate_ip4_state_in_wait (self)
6312 && !nm_device_activate_stage3_ip4_start (self))
6316 if ( nm_device_activate_ip6_state_in_wait (self)
6317 && !nm_device_activate_stage3_ip6_start (self))
6320 check_ip_failed (self, TRUE);
6324 fw_change_zone_handle (NMDevice *self,
6325 NMFirewallManagerCallId call_id,
6328 NMDevicePrivate *priv;
6330 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
6332 priv = NM_DEVICE_GET_PRIVATE (self);
6334 g_return_val_if_fail (priv->fw_call == call_id, FALSE);
6335 priv->fw_call = NULL;
6337 return !nm_utils_error_is_cancelled (error, FALSE);
6341 fw_change_zone_cb_stage2 (NMFirewallManager *firewall_manager,
6342 NMFirewallManagerCallId call_id,
6346 NMDevice *self = user_data;
6347 NMDevicePrivate *priv;
6349 if (!fw_change_zone_handle (self, call_id, error))
6352 /* FIXME: fail the device on error? */
6354 priv = NM_DEVICE_GET_PRIVATE (self);
6355 priv->fw_ready = TRUE;
6357 nm_device_activate_schedule_stage3_ip_config_start (self);
6361 fw_change_zone_cb_ip_check (NMFirewallManager *firewall_manager,
6362 NMFirewallManagerCallId call_id,
6366 NMDevice *self = user_data;
6368 if (!fw_change_zone_handle (self, call_id, error))
6371 /* FIXME: fail the device on error? */
6372 nm_device_start_ip_check (self);
6376 * nm_device_activate_schedule_stage3_ip_config_start
6378 * Schedule IP configuration start
6381 nm_device_activate_schedule_stage3_ip_config_start (NMDevice *self)
6383 NMDevicePrivate *priv;
6384 NMConnection *connection;
6385 NMSettingConnection *s_con = NULL;
6388 g_return_if_fail (NM_IS_DEVICE (self));
6390 priv = NM_DEVICE_GET_PRIVATE (self);
6391 g_return_if_fail (priv->act_request);
6393 /* Add the interface to the specified firewall zone */
6394 connection = nm_device_get_applied_connection (self);
6395 g_assert (connection);
6396 s_con = nm_connection_get_setting_connection (connection);
6398 if (!priv->fw_ready) {
6399 if (nm_device_uses_assumed_connection (self))
6400 priv->fw_ready = TRUE;
6402 if (!priv->fw_call) {
6403 zone = nm_setting_connection_get_zone (s_con);
6405 _LOGD (LOGD_DEVICE, "Activation: setting firewall zone '%s'", zone ? zone : "default");
6406 priv->fw_call = nm_firewall_manager_add_or_change_zone (nm_firewall_manager_get (),
6407 nm_device_get_ip_iface (self),
6410 fw_change_zone_cb_stage2,
6417 activation_source_schedule (self, activate_stage3_ip_config_start, AF_INET);
6420 static NMActStageReturn
6421 act_stage4_ip4_config_timeout (NMDevice *self, NMDeviceStateReason *reason)
6423 if (!get_ip_config_may_fail (self, AF_INET)) {
6424 *reason = NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE;
6425 return NM_ACT_STAGE_RETURN_FAILURE;
6427 return NM_ACT_STAGE_RETURN_SUCCESS;
6432 * nm_device_activate_stage4_ip4_config_timeout
6434 * Time out on retrieving the IPv4 config.
6438 activate_stage4_ip4_config_timeout (NMDevice *self)
6440 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6441 NMActStageReturn ret = NM_ACT_STAGE_RETURN_FAILURE;
6442 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
6444 ret = NM_DEVICE_GET_CLASS (self)->act_stage4_ip4_config_timeout (self, &reason);
6445 if (ret == NM_ACT_STAGE_RETURN_POSTPONE)
6447 else if (ret == NM_ACT_STAGE_RETURN_FAILURE) {
6448 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
6451 g_assert (ret == NM_ACT_STAGE_RETURN_SUCCESS);
6453 priv->ip4_state = IP_FAIL;
6455 check_ip_failed (self, FALSE);
6460 * nm_device_activate_schedule_ip4_config_timeout
6462 * Deal with a timeout of the IPv4 configuration
6466 nm_device_activate_schedule_ip4_config_timeout (NMDevice *self)
6468 NMDevicePrivate *priv;
6470 g_return_if_fail (NM_IS_DEVICE (self));
6472 priv = NM_DEVICE_GET_PRIVATE (self);
6473 g_return_if_fail (priv->act_request);
6475 activation_source_schedule (self, activate_stage4_ip4_config_timeout, AF_INET);
6479 static NMActStageReturn
6480 act_stage4_ip6_config_timeout (NMDevice *self, NMDeviceStateReason *reason)
6482 if (!get_ip_config_may_fail (self, AF_INET6)) {
6483 *reason = NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE;
6484 return NM_ACT_STAGE_RETURN_FAILURE;
6487 return NM_ACT_STAGE_RETURN_SUCCESS;
6492 * activate_stage4_ip6_config_timeout
6494 * Time out on retrieving the IPv6 config.
6498 activate_stage4_ip6_config_timeout (NMDevice *self)
6500 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6501 NMActStageReturn ret = NM_ACT_STAGE_RETURN_FAILURE;
6502 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
6504 ret = NM_DEVICE_GET_CLASS (self)->act_stage4_ip6_config_timeout (self, &reason);
6505 if (ret == NM_ACT_STAGE_RETURN_POSTPONE)
6507 if (ret == NM_ACT_STAGE_RETURN_FAILURE) {
6508 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
6511 g_assert (ret == NM_ACT_STAGE_RETURN_SUCCESS);
6513 priv->ip6_state = IP_FAIL;
6515 check_ip_failed (self, FALSE);
6520 * nm_device_activate_schedule_ip6_config_timeout
6522 * Deal with a timeout of the IPv6 configuration
6526 nm_device_activate_schedule_ip6_config_timeout (NMDevice *self)
6528 NMDevicePrivate *priv;
6530 g_return_if_fail (NM_IS_DEVICE (self));
6532 priv = NM_DEVICE_GET_PRIVATE (self);
6533 g_return_if_fail (priv->act_request);
6535 activation_source_schedule (self, activate_stage4_ip6_config_timeout, AF_INET6);
6541 char *modules[] = { "ip_tables", "iptable_nat", "nf_nat_ftp", "nf_nat_irc",
6542 "nf_nat_sip", "nf_nat_tftp", "nf_nat_pptp", "nf_nat_h323",
6547 if (!nm_platform_sysctl_set (NM_PLATFORM_GET, "/proc/sys/net/ipv4/ip_forward", "1")) {
6549 nm_log_err (LOGD_SHARING, "share: error starting IP forwarding: (%d) %s",
6550 errsv, strerror (errsv));
6554 if (!nm_platform_sysctl_set (NM_PLATFORM_GET, "/proc/sys/net/ipv4/ip_dynaddr", "1")) {
6556 nm_log_err (LOGD_SHARING, "share: error starting IP forwarding: (%d) %s",
6557 errsv, strerror (errsv));
6560 for (iter = modules; *iter; iter++)
6561 nm_utils_modprobe (NULL, FALSE, *iter, NULL);
6567 add_share_rule (NMActRequest *req, const char *table, const char *fmt, ...)
6572 va_start (args, fmt);
6573 cmd = g_strdup_vprintf (fmt, args);
6576 nm_act_request_add_share_rule (req, table, cmd);
6581 start_sharing (NMDevice *self, NMIP4Config *config)
6583 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6585 GError *error = NULL;
6586 char str_addr[INET_ADDRSTRLEN + 1];
6587 char str_mask[INET_ADDRSTRLEN + 1];
6588 guint32 netmask, network;
6589 const NMPlatformIP4Address *ip4_addr;
6590 const char *ip_iface;
6592 g_return_val_if_fail (config != NULL, FALSE);
6594 ip_iface = nm_device_get_ip_iface (self);
6596 ip4_addr = nm_ip4_config_get_address (config, 0);
6597 if (!ip4_addr || !ip4_addr->address)
6600 netmask = nm_utils_ip4_prefix_to_netmask (ip4_addr->plen);
6601 if (!inet_ntop (AF_INET, &netmask, str_mask, sizeof (str_mask)))
6604 network = ip4_addr->address & netmask;
6605 if (!inet_ntop (AF_INET, &network, str_addr, sizeof (str_addr)))
6611 req = nm_device_get_act_request (self);
6614 add_share_rule (req, "nat", "POSTROUTING --source %s/%s ! --destination %s/%s --jump MASQUERADE", str_addr, str_mask, str_addr, str_mask);
6615 add_share_rule (req, "filter", "FORWARD --destination %s/%s --out-interface %s --match state --state ESTABLISHED,RELATED --jump ACCEPT", str_addr, str_mask, ip_iface);
6616 add_share_rule (req, "filter", "FORWARD --source %s/%s --in-interface %s --jump ACCEPT", str_addr, str_mask, ip_iface);
6617 add_share_rule (req, "filter", "FORWARD --in-interface %s --out-interface %s --jump ACCEPT", ip_iface, ip_iface);
6618 add_share_rule (req, "filter", "FORWARD --out-interface %s --jump REJECT", ip_iface);
6619 add_share_rule (req, "filter", "FORWARD --in-interface %s --jump REJECT", ip_iface);
6620 add_share_rule (req, "filter", "INPUT --in-interface %s --protocol udp --destination-port 67 --jump ACCEPT", ip_iface);
6621 add_share_rule (req, "filter", "INPUT --in-interface %s --protocol tcp --destination-port 67 --jump ACCEPT", ip_iface);
6622 add_share_rule (req, "filter", "INPUT --in-interface %s --protocol udp --destination-port 53 --jump ACCEPT", ip_iface);
6623 add_share_rule (req, "filter", "INPUT --in-interface %s --protocol tcp --destination-port 53 --jump ACCEPT", ip_iface);
6625 nm_act_request_set_shared (req, TRUE);
6627 if (!nm_dnsmasq_manager_start (priv->dnsmasq_manager, config, &error)) {
6628 _LOGE (LOGD_SHARING, "share: (%s) failed to start dnsmasq: %s",
6629 ip_iface, error->message);
6630 g_error_free (error);
6631 nm_act_request_set_shared (req, FALSE);
6635 priv->dnsmasq_state_id = g_signal_connect (priv->dnsmasq_manager, NM_DNS_MASQ_MANAGER_STATE_CHANGED,
6636 G_CALLBACK (dnsmasq_state_changed_cb),
6642 arp_cleanup (NMDevice *self)
6644 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6646 if (priv->arping.announcing) {
6647 nm_arping_manager_destroy (priv->arping.announcing);
6648 priv->arping.announcing = NULL;
6653 arp_announce (NMDevice *self)
6655 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6656 NMConnection *connection;
6657 NMSettingIPConfig *s_ip4;
6659 const guint8 *hw_addr;
6660 size_t hw_addr_len = 0;
6664 hw_addr = nm_platform_link_get_address (NM_PLATFORM_GET,
6665 nm_device_get_ip_ifindex (self),
6668 if (!hw_addr_len || !hw_addr)
6671 /* We only care about manually-configured addresses; DHCP- and autoip-configured
6672 * ones should already have been seen on the network at this point.
6674 connection = nm_device_get_applied_connection (self);
6677 s_ip4 = nm_connection_get_setting_ip4_config (connection);
6680 num = nm_setting_ip_config_get_num_addresses (s_ip4);
6684 priv->arping.announcing = nm_arping_manager_new (nm_device_get_ip_ifindex (self));
6686 for (i = 0; i < num; i++) {
6687 NMIPAddress *ip = nm_setting_ip_config_get_address (s_ip4, i);
6690 if (inet_pton (AF_INET, nm_ip_address_get_address (ip), &addr) == 1)
6691 nm_arping_manager_add_address (priv->arping.announcing, addr);
6693 g_warn_if_reached ();
6696 nm_arping_manager_announce_addresses (priv->arping.announcing);
6700 activate_stage5_ip4_config_commit (NMDevice *self)
6702 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6705 NMConnection *connection;
6706 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
6709 req = nm_device_get_act_request (self);
6711 connection = nm_act_request_get_applied_connection (req);
6712 g_assert (connection);
6714 /* Interface must be IFF_UP before IP config can be applied */
6715 ip_ifindex = nm_device_get_ip_ifindex (self);
6716 if (!nm_platform_link_is_up (NM_PLATFORM_GET, ip_ifindex) && !nm_device_uses_assumed_connection (self)) {
6717 nm_platform_link_set_up (NM_PLATFORM_GET, ip_ifindex, NULL);
6718 if (!nm_platform_link_is_up (NM_PLATFORM_GET, ip_ifindex))
6719 _LOGW (LOGD_DEVICE, "interface %s not up for IP configuration", nm_device_get_ip_iface (self));
6722 /* NULL to use the existing priv->dev_ip4_config */
6723 if (!ip4_config_merge_and_apply (self, NULL, TRUE, &reason)) {
6724 _LOGD (LOGD_DEVICE | LOGD_IP4, "Activation: Stage 5 of 5 (IPv4 Commit) failed");
6725 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
6729 /* Start IPv4 sharing if we need it */
6730 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG);
6732 if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0) {
6733 if (!start_sharing (self, priv->ip4_config)) {
6734 _LOGW (LOGD_SHARING, "Activation: Stage 5 of 5 (IPv4 Commit) start sharing failed.");
6735 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_SHARED_START_FAILED);
6740 /* If IPv4 wasn't the first to complete, and DHCP was used, then ensure
6741 * dispatcher scripts get the DHCP lease information.
6743 if ( priv->dhcp4_client
6744 && nm_device_activate_ip4_state_in_conf (self)
6745 && (nm_device_get_state (self) > NM_DEVICE_STATE_IP_CONFIG)) {
6746 /* Notify dispatcher scripts of new DHCP4 config */
6747 nm_dispatcher_call (DISPATCHER_ACTION_DHCP4_CHANGE,
6748 nm_device_get_settings_connection (self),
6749 nm_device_get_applied_connection (self),
6756 arp_announce (self);
6758 nm_device_remove_pending_action (self, PENDING_ACTION_DHCP4, FALSE);
6760 /* Enter the IP_CHECK state if this is the first method to complete */
6761 priv->ip4_state = IP_DONE;
6762 check_ip_done (self);
6766 queued_ip4_config_change_clear (NMDevice *self)
6768 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6770 if (priv->queued_ip4_config_id) {
6771 _LOGD (LOGD_DEVICE, "clearing queued IP4 config change");
6772 g_source_remove (priv->queued_ip4_config_id);
6773 priv->queued_ip4_config_id = 0;
6778 queued_ip6_config_change_clear (NMDevice *self)
6780 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6782 if (priv->queued_ip6_config_id) {
6783 _LOGD (LOGD_DEVICE, "clearing queued IP6 config change");
6784 g_source_remove (priv->queued_ip6_config_id);
6785 priv->queued_ip6_config_id = 0;
6790 nm_device_activate_schedule_ip4_config_result (NMDevice *self, NMIP4Config *config)
6792 NMDevicePrivate *priv;
6794 g_return_if_fail (NM_IS_DEVICE (self));
6795 priv = NM_DEVICE_GET_PRIVATE (self);
6797 g_clear_object (&priv->dev_ip4_config);
6799 priv->dev_ip4_config = g_object_ref (config);
6801 queued_ip4_config_change_clear (self);
6802 activation_source_schedule (self, activate_stage5_ip4_config_commit, AF_INET);
6806 nm_device_activate_ip4_state_in_conf (NMDevice *self)
6808 g_return_val_if_fail (self != NULL, FALSE);
6809 return NM_DEVICE_GET_PRIVATE (self)->ip4_state == IP_CONF;
6813 nm_device_activate_ip4_state_in_wait (NMDevice *self)
6815 g_return_val_if_fail (self != NULL, FALSE);
6816 return NM_DEVICE_GET_PRIVATE (self)->ip4_state == IP_WAIT;
6820 activate_stage5_ip6_config_commit (NMDevice *self)
6822 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6824 NMConnection *connection;
6825 NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
6828 req = nm_device_get_act_request (self);
6830 connection = nm_act_request_get_applied_connection (req);
6831 g_assert (connection);
6833 /* Interface must be IFF_UP before IP config can be applied */
6834 ip_ifindex = nm_device_get_ip_ifindex (self);
6835 if (!nm_platform_link_is_up (NM_PLATFORM_GET, ip_ifindex) && !nm_device_uses_assumed_connection (self)) {
6836 nm_platform_link_set_up (NM_PLATFORM_GET, ip_ifindex, NULL);
6837 if (!nm_platform_link_is_up (NM_PLATFORM_GET, ip_ifindex))
6838 _LOGW (LOGD_DEVICE, "interface %s not up for IP configuration", nm_device_get_ip_iface (self));
6841 if (ip6_config_merge_and_apply (self, TRUE, &reason)) {
6842 if ( priv->dhcp6_mode != NM_RDISC_DHCP_LEVEL_NONE
6843 && priv->ip6_state == IP_CONF) {
6844 if (priv->dhcp6_ip6_config) {
6845 /* If IPv6 wasn't the first IP to complete, and DHCP was used,
6846 * then ensure dispatcher scripts get the DHCP lease information.
6848 nm_dispatcher_call (DISPATCHER_ACTION_DHCP6_CHANGE,
6849 nm_device_get_settings_connection (self),
6850 nm_device_get_applied_connection (self),
6856 /* still waiting for first dhcp6 lease. */
6861 nm_device_remove_pending_action (self, PENDING_ACTION_DHCP6, FALSE);
6862 nm_device_remove_pending_action (self, PENDING_ACTION_AUTOCONF6, FALSE);
6864 /* Enter the IP_CHECK state if this is the first method to complete */
6865 priv->ip6_state = IP_DONE;
6866 check_ip_done (self);
6868 _LOGW (LOGD_DEVICE | LOGD_IP6, "Activation: Stage 5 of 5 (IPv6 Commit) failed");
6869 nm_device_state_changed (self, NM_DEVICE_STATE_FAILED, reason);
6874 nm_device_activate_schedule_ip6_config_result (NMDevice *self)
6876 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6878 g_return_if_fail (NM_IS_DEVICE (self));
6880 /* If IP had previously failed, move it back to IP_CONF since we
6881 * clearly now have configuration.
6883 if (priv->ip6_state == IP_FAIL)
6884 priv->ip6_state = IP_CONF;
6886 activation_source_schedule (self, activate_stage5_ip6_config_commit, AF_INET6);
6890 nm_device_activate_ip6_state_in_conf (NMDevice *self)
6892 g_return_val_if_fail (self != NULL, FALSE);
6893 return NM_DEVICE_GET_PRIVATE (self)->ip6_state == IP_CONF;
6897 nm_device_activate_ip6_state_in_wait (NMDevice *self)
6899 g_return_val_if_fail (self != NULL, FALSE);
6900 return NM_DEVICE_GET_PRIVATE (self)->ip6_state == IP_WAIT;
6904 clear_act_request (NMDevice *self)
6906 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6908 if (!priv->act_request)
6911 nm_active_connection_set_default (NM_ACTIVE_CONNECTION (priv->act_request), FALSE);
6913 priv->master_ready_handled = FALSE;
6914 nm_clear_g_signal_handler (priv->act_request, &priv->master_ready_id);
6916 g_clear_object (&priv->act_request);
6917 _notify (self, PROP_ACTIVE_CONNECTION);
6921 dnsmasq_cleanup (NMDevice *self)
6923 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6925 if (!priv->dnsmasq_manager)
6928 nm_clear_g_signal_handler (priv->dnsmasq_manager, &priv->dnsmasq_state_id);
6930 nm_dnsmasq_manager_stop (priv->dnsmasq_manager);
6931 g_object_unref (priv->dnsmasq_manager);
6932 priv->dnsmasq_manager = NULL;
6936 _update_ip4_address (NMDevice *self)
6938 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6941 g_return_if_fail (NM_IS_DEVICE (self));
6943 if ( priv->ip4_config
6944 && ip_config_valid (priv->state)
6945 && nm_ip4_config_get_num_addresses (priv->ip4_config)) {
6946 addr = nm_ip4_config_get_address (priv->ip4_config, 0)->address;
6947 if (addr != priv->ip4_address) {
6948 priv->ip4_address = addr;
6949 _notify (self, PROP_IP4_ADDRESS);
6955 nm_device_get_is_nm_owned (NMDevice *self)
6957 return NM_DEVICE_GET_PRIVATE (self)->is_nm_owned;
6961 * delete_on_deactivate_link_delete
6963 * Function will be queued with g_idle_add to call
6964 * nm_platform_link_delete for the underlying resources
6968 delete_on_deactivate_link_delete (gpointer user_data)
6970 DeleteOnDeactivateData *data = user_data;
6971 NMDevice *self = data->device;
6973 _LOGD (LOGD_DEVICE, "delete_on_deactivate: cleanup and delete virtual link #%d (id=%u)",
6974 data->ifindex, data->idle_add_id);
6977 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (data->device);
6978 gs_free_error GError *error = NULL;
6980 g_object_remove_weak_pointer (G_OBJECT (data->device), (void **) &data->device);
6981 priv->delete_on_deactivate_data = NULL;
6983 if (!nm_device_unrealize (data->device, TRUE, &error))
6984 _LOGD (LOGD_DEVICE, "delete_on_deactivate: unrealizing %d failed (%s)", data->ifindex, error->message);
6986 nm_platform_link_delete (NM_PLATFORM_GET, data->ifindex);
6993 delete_on_deactivate_unschedule (NMDevice *self)
6995 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
6997 if (priv->delete_on_deactivate_data) {
6998 DeleteOnDeactivateData *data = priv->delete_on_deactivate_data;
7000 priv->delete_on_deactivate_data = NULL;
7002 g_source_remove (data->idle_add_id);
7003 g_object_remove_weak_pointer (G_OBJECT (self), (void **) &data->device);
7004 _LOGD (LOGD_DEVICE, "delete_on_deactivate: cancel cleanup and delete virtual link #%d (id=%u)",
7005 data->ifindex, data->idle_add_id);
7011 delete_on_deactivate_check_and_schedule (NMDevice *self, int ifindex)
7013 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7014 DeleteOnDeactivateData *data;
7018 if (!priv->is_nm_owned)
7020 if (priv->queued_act_request)
7022 if (!nm_device_is_software (self) || !nm_device_is_real (self))
7024 if (nm_device_get_state (self) == NM_DEVICE_STATE_UNMANAGED)
7026 if (nm_device_get_state (self) == NM_DEVICE_STATE_UNAVAILABLE)
7028 delete_on_deactivate_unschedule (self); /* always cancel and reschedule */
7030 data = g_new (DeleteOnDeactivateData, 1);
7031 g_object_add_weak_pointer (G_OBJECT (self), (void **) &data->device);
7032 data->device = self;
7033 data->ifindex = ifindex;
7034 data->idle_add_id = g_idle_add (delete_on_deactivate_link_delete, data);
7035 priv->delete_on_deactivate_data = data;
7037 _LOGD (LOGD_DEVICE, "delete_on_deactivate: schedule cleanup and delete virtual link #%d (id=%u)",
7038 ifindex, data->idle_add_id);
7042 _cleanup_ip4_pre (NMDevice *self, CleanupType cleanup_type)
7044 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7046 priv->ip4_state = IP_NONE;
7047 queued_ip4_config_change_clear (self);
7049 dhcp4_cleanup (self, cleanup_type, FALSE);
7051 dnsmasq_cleanup (self);
7052 ipv4ll_cleanup (self);
7056 _cleanup_ip6_pre (NMDevice *self, CleanupType cleanup_type)
7058 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7060 priv->ip6_state = IP_NONE;
7061 queued_ip6_config_change_clear (self);
7063 dhcp6_cleanup (self, cleanup_type, FALSE);
7064 linklocal6_cleanup (self);
7065 addrconf6_cleanup (self);
7069 _hash_check_invalid_keys_impl (GHashTable *hash, const char *setting_name, GError **error, const char **argv)
7071 guint found_keys = 0;
7074 nm_assert (argv && argv[0]);
7076 #if NM_MORE_ASSERTS > 10
7077 /* Assert that the keys are unique. */
7079 gs_unref_hashtable GHashTable *check_dups = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
7081 for (i = 0; argv[i]; i++) {
7082 if (!g_hash_table_add (check_dups, (char *) argv[i]))
7085 nm_assert (g_hash_table_size (check_dups) > 0);
7089 if (!hash || g_hash_table_size (hash) == 0)
7092 for (i = 0; argv[i]; i++) {
7093 if (g_hash_table_contains (hash, argv[i]))
7097 if (found_keys != g_hash_table_size (hash)) {
7098 GHashTableIter iter;
7099 const char *k = NULL;
7100 const char *first_invalid_key = NULL;
7105 g_hash_table_iter_init (&iter, hash);
7106 while (g_hash_table_iter_next (&iter, (gpointer *) &k, NULL)) {
7107 for (i = 0; argv[i]; i++) {
7108 if (!strcmp (argv[i], k)) {
7109 first_invalid_key = k;
7113 if (first_invalid_key)
7118 NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
7119 "Can't reapply changes to '%s%s%s' setting",
7120 setting_name ? : "",
7121 setting_name ? "." : "",
7122 first_invalid_key ? : "<UNKNOWN>");
7123 g_return_val_if_fail (first_invalid_key, FALSE);
7129 #define _hash_check_invalid_keys(hash, setting_name, error, ...) _hash_check_invalid_keys_impl (hash, setting_name, error, ((const char *[]) { __VA_ARGS__, NULL }))
7132 nm_device_reactivate_ip4_config (NMDevice *self,
7133 NMSettingIPConfig *s_ip4_old,
7134 NMSettingIPConfig *s_ip4_new)
7136 NMDevicePrivate *priv;
7138 g_return_if_fail (NM_IS_DEVICE (self));
7139 priv = NM_DEVICE_GET_PRIVATE (self);
7141 if (priv->ip4_state != IP_NONE) {
7142 g_clear_object (&priv->con_ip4_config);
7143 priv->con_ip4_config = nm_ip4_config_new (nm_device_get_ip_ifindex (self));
7144 nm_ip4_config_merge_setting (priv->con_ip4_config,
7146 nm_device_get_ip4_route_metric (self));
7148 if (strcmp (nm_setting_ip_config_get_method (s_ip4_new),
7149 nm_setting_ip_config_get_method (s_ip4_old))) {
7150 _cleanup_ip4_pre (self, CLEANUP_TYPE_DECONFIGURE);
7151 priv->ip4_state = IP_WAIT;
7152 if (!nm_device_activate_stage3_ip4_start (self))
7153 _LOGW (LOGD_IP4, "Failed to apply IPv4 configuration");
7155 if (!ip4_config_merge_and_apply (self, NULL, TRUE, NULL))
7156 _LOGW (LOGD_IP4, "Failed to reapply IPv4 configuration");
7162 nm_device_reactivate_ip6_config (NMDevice *self,
7163 NMSettingIPConfig *s_ip6_old,
7164 NMSettingIPConfig *s_ip6_new)
7166 NMDevicePrivate *priv;
7168 g_return_if_fail (NM_IS_DEVICE (self));
7169 priv = NM_DEVICE_GET_PRIVATE (self);
7171 if (priv->ip6_state != IP_NONE) {
7172 g_clear_object (&priv->con_ip6_config);
7173 priv->con_ip6_config = nm_ip6_config_new (nm_device_get_ip_ifindex (self));
7174 nm_ip6_config_merge_setting (priv->con_ip6_config,
7176 nm_device_get_ip6_route_metric (self));
7178 if (strcmp (nm_setting_ip_config_get_method (s_ip6_new),
7179 nm_setting_ip_config_get_method (s_ip6_old))) {
7180 _cleanup_ip6_pre (self, CLEANUP_TYPE_DECONFIGURE);
7181 priv->ip6_state = IP_WAIT;
7182 if (!nm_device_activate_stage3_ip6_start (self))
7183 _LOGW (LOGD_IP6, "Failed to apply IPv6 configuration");
7185 if (!ip6_config_merge_and_apply (self, TRUE, NULL))
7186 _LOGW (LOGD_IP4, "Failed to reapply IPv6 configuration");
7192 /* reapply_connection:
7193 * @connection: the new connection settings to be applied or %NULL to reapply
7194 * the current settings connection
7195 * @version_id: either zero, or the current version id for the applied
7197 * @error: the error if %FALSE is returned
7199 * Change configuration of an already configured device if possible.
7200 * Updates the device's applied connection upon success.
7202 * Return: %FALSE if the new configuration can not be reapplied.
7205 reapply_connection (NMDevice *self,
7206 NMConnection *connection,
7210 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7211 NMConnection *applied = nm_device_get_applied_connection (self);
7212 gs_unref_object NMConnection *applied_clone = NULL;
7213 gs_unref_hashtable GHashTable *diffs = NULL;
7214 NMConnection *con_old, *con_new;
7215 NMSettingIPConfig *s_ip4_old, *s_ip4_new;
7216 NMSettingIPConfig *s_ip6_old, *s_ip6_new;
7218 if (priv->state != NM_DEVICE_STATE_ACTIVATED) {
7219 g_set_error_literal (error,
7221 NM_DEVICE_ERROR_NOT_ACTIVE,
7222 "Device is not activated");
7226 nm_connection_diff (connection,
7228 NM_SETTING_COMPARE_FLAG_IGNORE_TIMESTAMP |
7229 NM_SETTING_COMPARE_FLAG_IGNORE_SECRETS,
7232 /**************************************************************************
7233 * check for unsupported changes and reject to reapply
7234 *************************************************************************/
7235 if (!_hash_check_invalid_keys (diffs, NULL, error,
7236 NM_SETTING_IP4_CONFIG_SETTING_NAME,
7237 NM_SETTING_IP6_CONFIG_SETTING_NAME,
7238 NM_SETTING_CONNECTION_SETTING_NAME))
7241 if (!_hash_check_invalid_keys (diffs ? g_hash_table_lookup (diffs, NM_SETTING_CONNECTION_SETTING_NAME) : NULL,
7242 NM_SETTING_CONNECTION_SETTING_NAME,
7244 NM_SETTING_CONNECTION_ZONE,
7245 NM_SETTING_CONNECTION_METERED))
7248 if ( version_id != 0
7249 && version_id != nm_active_connection_version_id_get ((NMActiveConnection *) priv->act_request)) {
7250 g_set_error_literal (error,
7252 NM_DEVICE_ERROR_VERSION_ID_MISMATCH,
7253 "Reapply failed because device changed in the meantime and the version-id mismatches");
7257 /**************************************************************************
7258 * Update applied connection
7259 *************************************************************************/
7262 nm_active_connection_version_id_bump ((NMActiveConnection *) priv->act_request);
7264 _LOGD (LOGD_DEVICE, "reapply (version-id %llu%s)",
7265 (long long unsigned) nm_active_connection_version_id_get (((NMActiveConnection *) priv->act_request)),
7266 diffs ? "" : " (unmodified)");
7269 con_old = applied_clone = nm_simple_connection_new_clone (applied);
7271 nm_connection_replace_settings_from_connection (applied, connection);
7273 con_old = con_new = applied;
7275 s_ip4_new = nm_connection_get_setting_ip4_config (con_new);
7276 s_ip4_old = nm_connection_get_setting_ip4_config (con_old);
7277 s_ip6_new = nm_connection_get_setting_ip6_config (con_new);
7278 s_ip6_old = nm_connection_get_setting_ip6_config (con_old);
7280 /**************************************************************************
7282 *************************************************************************/
7284 nm_device_update_firewall_zone (self);
7285 nm_device_update_metered (self);
7287 nm_device_reactivate_ip4_config (self, s_ip4_old, s_ip4_new);
7288 nm_device_reactivate_ip6_config (self, s_ip6_old, s_ip6_new);
7294 NMConnection *connection;
7299 reapply_cb (NMDevice *self,
7300 GDBusMethodInvocation *context,
7301 NMAuthSubject *subject,
7305 ReapplyData *reapply_data = user_data;
7306 guint64 version_id = 0;
7307 gs_unref_object NMConnection *connection = NULL;
7308 GError *local = NULL;
7311 connection = reapply_data->connection;
7312 version_id = reapply_data->version_id;
7313 g_slice_free (ReapplyData, reapply_data);
7317 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_REAPPLY, self, FALSE, subject, error->message);
7318 g_dbus_method_invocation_return_gerror (context, error);
7322 if (!reapply_connection (self,
7323 connection ? : (NMConnection *) nm_device_get_settings_connection (self),
7326 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_REAPPLY, self, FALSE, subject, local->message);
7327 g_dbus_method_invocation_take_error (context, local);
7330 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_REAPPLY, self, TRUE, subject, NULL);
7331 g_dbus_method_invocation_return_value (context, NULL);
7336 impl_device_reapply (NMDevice *self,
7337 GDBusMethodInvocation *context,
7342 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7343 NMSettingsConnection *settings_connection;
7344 NMConnection *connection = NULL;
7345 GError *error = NULL;
7346 ReapplyData *reapply_data;
7348 /* No flags supported as of now. */
7350 error = g_error_new_literal (NM_DEVICE_ERROR,
7351 NM_DEVICE_ERROR_FAILED,
7352 "Invalid flags specified");
7353 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_REAPPLY, self, FALSE, context, error->message);
7354 g_dbus_method_invocation_take_error (context, error);
7358 if (priv->state != NM_DEVICE_STATE_ACTIVATED) {
7359 error = g_error_new_literal (NM_DEVICE_ERROR,
7360 NM_DEVICE_ERROR_NOT_ACTIVE,
7361 "Device is not activated");
7362 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_REAPPLY, self, FALSE, context, error->message);
7363 g_dbus_method_invocation_take_error (context, error);
7367 settings_connection = nm_device_get_settings_connection (self);
7368 g_return_if_fail (settings_connection);
7370 if (settings && g_variant_n_children (settings)) {
7371 /* New settings specified inline. */
7372 connection = nm_simple_connection_new_from_dbus (settings, &error);
7374 g_prefix_error (&error, "The settings specified are invalid: ");
7375 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_REAPPLY, self, FALSE, context, error->message);
7376 g_dbus_method_invocation_take_error (context, error);
7379 nm_connection_clear_secrets (connection);
7382 if (connection || version_id) {
7383 reapply_data = g_slice_new (ReapplyData);
7384 reapply_data->connection = connection;
7385 reapply_data->version_id = version_id;
7387 reapply_data = NULL;
7389 /* Ask the manager to authenticate this request for us */
7390 g_signal_emit (self, signals[AUTH_REQUEST], 0,
7392 nm_device_get_applied_connection (self),
7393 NM_AUTH_PERMISSION_NETWORK_CONTROL,
7399 /*****************************************************************************/
7402 get_applied_connection_cb (NMDevice *self,
7403 GDBusMethodInvocation *context,
7404 NMAuthSubject *subject,
7406 gpointer user_data /* possibly dangling pointer */)
7408 NMDevicePrivate *priv;
7409 NMConnection *applied_connection;
7412 g_return_if_fail (NM_IS_DEVICE (self));
7415 g_dbus_method_invocation_return_gerror (context, error);
7419 priv = NM_DEVICE_GET_PRIVATE (self);
7421 applied_connection = nm_device_get_applied_connection (self);
7423 if (!applied_connection) {
7424 error = g_error_new_literal (NM_DEVICE_ERROR,
7425 NM_DEVICE_ERROR_NOT_ACTIVE,
7426 "Device is not activated");
7427 g_dbus_method_invocation_take_error (context, error);
7431 if (applied_connection != user_data) {
7432 /* The applied connection changed due to a race. Reauthenticate. */
7433 g_signal_emit (self, signals[AUTH_REQUEST], 0,
7436 NM_AUTH_PERMISSION_NETWORK_CONTROL,
7438 get_applied_connection_cb,
7439 applied_connection /* no need take a ref. We will not dereference this pointer. */);
7443 settings = nm_connection_to_dbus (applied_connection, NM_CONNECTION_SERIALIZE_NO_SECRETS);
7445 settings = g_variant_new_array (G_VARIANT_TYPE ("{sa{sv}}"), NULL, 0);
7447 g_dbus_method_invocation_return_value (context,
7448 g_variant_new ("(@a{sa{sv}}t)",
7450 nm_active_connection_version_id_get ((NMActiveConnection *) priv->act_request)));
7454 impl_device_get_applied_connection (NMDevice *self,
7455 GDBusMethodInvocation *context,
7458 NMConnection *applied_connection;
7459 GError *error = NULL;
7461 g_return_if_fail (NM_IS_DEVICE (self));
7463 /* No flags supported as of now. */
7465 error = g_error_new_literal (NM_DEVICE_ERROR,
7466 NM_DEVICE_ERROR_FAILED,
7467 "Invalid flags specified");
7468 g_dbus_method_invocation_take_error (context, error);
7472 applied_connection = nm_device_get_applied_connection (self);
7473 if (!applied_connection) {
7474 error = g_error_new_literal (NM_DEVICE_ERROR,
7475 NM_DEVICE_ERROR_NOT_ACTIVE,
7476 "Device is not activated");
7477 g_dbus_method_invocation_take_error (context, error);
7481 /* Ask the manager to authenticate this request for us */
7482 g_signal_emit (self, signals[AUTH_REQUEST], 0,
7485 NM_AUTH_PERMISSION_NETWORK_CONTROL,
7487 get_applied_connection_cb,
7488 applied_connection /* no need take a ref. We will not dereference this pointer. */);
7491 /*****************************************************************************/
7494 disconnect_cb (NMDevice *self,
7495 GDBusMethodInvocation *context,
7496 NMAuthSubject *subject,
7500 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7501 GError *local = NULL;
7504 g_dbus_method_invocation_return_gerror (context, error);
7505 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_DISCONNECT, self, FALSE, subject, error->message);
7510 if (priv->state <= NM_DEVICE_STATE_DISCONNECTED) {
7511 local = g_error_new_literal (NM_DEVICE_ERROR,
7512 NM_DEVICE_ERROR_NOT_ACTIVE,
7513 "Device is not active");
7514 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_DISCONNECT, self, FALSE, subject, local->message);
7515 g_dbus_method_invocation_take_error (context, local);
7517 nm_device_set_autoconnect (self, FALSE);
7519 nm_device_state_changed (self,
7520 NM_DEVICE_STATE_DEACTIVATING,
7521 NM_DEVICE_STATE_REASON_USER_REQUESTED);
7522 g_dbus_method_invocation_return_value (context, NULL);
7523 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_DISCONNECT, self, TRUE, subject, NULL);
7528 _clear_queued_act_request (NMDevicePrivate *priv)
7530 if (priv->queued_act_request) {
7531 nm_active_connection_set_state ((NMActiveConnection *) priv->queued_act_request, NM_ACTIVE_CONNECTION_STATE_DEACTIVATED);
7532 g_clear_object (&priv->queued_act_request);
7537 impl_device_disconnect (NMDevice *self, GDBusMethodInvocation *context)
7539 NMConnection *connection;
7540 GError *error = NULL;
7542 if (NM_DEVICE_GET_PRIVATE (self)->act_request == NULL) {
7543 error = g_error_new_literal (NM_DEVICE_ERROR,
7544 NM_DEVICE_ERROR_NOT_ACTIVE,
7545 "This device is not active");
7546 g_dbus_method_invocation_take_error (context, error);
7550 connection = nm_device_get_applied_connection (self);
7551 g_assert (connection);
7553 /* Ask the manager to authenticate this request for us */
7554 g_signal_emit (self, signals[AUTH_REQUEST], 0,
7557 NM_AUTH_PERMISSION_NETWORK_CONTROL,
7564 delete_cb (NMDevice *self,
7565 GDBusMethodInvocation *context,
7566 NMAuthSubject *subject,
7570 GError *local = NULL;
7573 g_dbus_method_invocation_return_gerror (context, error);
7574 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_DELETE, self, FALSE, subject, error->message);
7579 nm_audit_log_device_op (NM_AUDIT_OP_DEVICE_DELETE, self, TRUE, subject, NULL);
7580 if (nm_device_unrealize (self, TRUE, &local))
7581 g_dbus_method_invocation_return_value (context, NULL);
7583 g_dbus_method_invocation_take_error (context, local);
7587 impl_device_delete (NMDevice *self, GDBusMethodInvocation *context)
7589 GError *error = NULL;
7591 if (!nm_device_is_software (self) || !nm_device_is_real (self)) {
7592 error = g_error_new_literal (NM_DEVICE_ERROR,
7593 NM_DEVICE_ERROR_NOT_SOFTWARE,
7594 "This device is not a software device or is not realized");
7595 g_dbus_method_invocation_take_error (context, error);
7599 /* Ask the manager to authenticate this request for us */
7600 g_signal_emit (self, signals[AUTH_REQUEST], 0,
7603 NM_AUTH_PERMISSION_NETWORK_CONTROL,
7610 _device_activate (NMDevice *self, NMActRequest *req)
7612 NMDevicePrivate *priv;
7613 NMConnection *connection;
7615 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
7616 g_return_val_if_fail (NM_IS_ACT_REQUEST (req), FALSE);
7617 g_return_val_if_fail (nm_device_get_managed (self, FALSE), FALSE);
7619 /* Ensure the activation request is still valid; the master may have
7620 * already failed in which case activation of this device should not proceed.
7622 if (nm_active_connection_get_state (NM_ACTIVE_CONNECTION (req)) >= NM_ACTIVE_CONNECTION_STATE_DEACTIVATING)
7625 priv = NM_DEVICE_GET_PRIVATE (self);
7627 connection = nm_act_request_get_applied_connection (req);
7628 g_assert (connection);
7630 _LOGI (LOGD_DEVICE, "Activation: starting connection '%s' (%s)",
7631 nm_connection_get_id (connection),
7632 nm_connection_get_uuid (connection));
7634 delete_on_deactivate_unschedule (self);
7636 /* note: don't notify D-Bus of the new AC here, but do it later when
7637 * changing state to PREPARE so that the two properties change together.
7639 priv->act_request = g_object_ref (req);
7641 nm_device_activate_schedule_stage1_device_prepare (self);
7646 _carrier_wait_check_queued_act_request (NMDevice *self)
7648 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7649 NMActRequest *queued_req;
7651 if ( !priv->queued_act_request
7652 || !priv->queued_act_request_is_waiting_for_carrier)
7655 priv->queued_act_request_is_waiting_for_carrier = FALSE;
7656 if (!priv->carrier) {
7657 _LOGD (LOGD_DEVICE, "Cancel queued activation request as we have no carrier after timeout");
7658 _clear_queued_act_request (priv);
7660 _LOGD (LOGD_DEVICE, "Activate queued activation request as we now have carrier");
7661 queued_req = priv->queued_act_request;
7662 priv->queued_act_request = NULL;
7663 _device_activate (self, queued_req);
7664 g_object_unref (queued_req);
7669 _carrier_wait_check_act_request_must_queue (NMDevice *self, NMActRequest *req)
7671 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7672 NMConnection *connection;
7674 /* If we have carrier or if we are not waiting for it, the activation
7675 * request is not blocked waiting for carrier. */
7678 if (priv->carrier_wait_id == 0)
7681 connection = nm_act_request_get_applied_connection (req);
7682 if (!connection_requires_carrier (connection))
7685 if (!nm_device_check_connection_available (self, connection, NM_DEVICE_CHECK_CON_AVAILABLE_ALL, NULL)) {
7686 /* We passed all @flags we have, and no @specific_object.
7687 * This equals maximal availability, if a connection is not available
7688 * in this case, it is not waiting for carrier.
7690 * Actually, why are we even trying to activate it? Strange, but whatever
7691 * the reason, don't wait for carrier.
7696 if (nm_device_check_connection_available (self, connection, NM_DEVICE_CHECK_CON_AVAILABLE_ALL & ~_NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST_WAITING_CARRIER, NULL)) {
7697 /* The connection was available with flags ALL, and it is still available
7698 * if we pretend not to wait for carrier. That means that the
7699 * connection is available now, and does not wait for carrier.
7701 * Since the flags increase the availability of a connection, when checking
7702 * ALL&~WAITING_CARRIER, it means that we certainly would wait for carrier. */
7706 /* The activation request must wait for carrier. */
7711 nm_device_steal_connection (NMDevice *self, NMSettingsConnection *connection)
7713 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7715 _LOGW (LOGD_DEVICE, "disconnecting connection '%s' for new activation request.",
7716 nm_settings_connection_get_id (connection));
7718 if ( priv->queued_act_request
7719 && connection == nm_active_connection_get_settings_connection (NM_ACTIVE_CONNECTION (priv->queued_act_request)))
7720 _clear_queued_act_request (priv);
7722 if ( priv->act_request
7723 && connection == nm_active_connection_get_settings_connection (NM_ACTIVE_CONNECTION (priv->act_request))
7724 && priv->state < NM_DEVICE_STATE_DEACTIVATING)
7725 nm_device_state_changed (self,
7726 NM_DEVICE_STATE_DEACTIVATING,
7727 NM_DEVICE_STATE_REASON_NEW_ACTIVATION);
7731 nm_device_queue_activation (NMDevice *self, NMActRequest *req)
7733 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7734 gboolean must_queue;
7736 must_queue = _carrier_wait_check_act_request_must_queue (self, req);
7738 if (!priv->act_request && !must_queue) {
7739 /* Just activate immediately */
7740 if (!_device_activate (self, req))
7741 g_assert_not_reached ();
7745 /* supercede any already-queued request */
7746 _clear_queued_act_request (priv);
7747 priv->queued_act_request = g_object_ref (req);
7748 priv->queued_act_request_is_waiting_for_carrier = must_queue;
7750 _LOGD (LOGD_DEVICE, "queue activation request waiting for %s", must_queue ? "carrier" : "currently active connection to disconnect");
7752 /* Deactivate existing activation request first */
7753 if (priv->act_request) {
7754 _LOGI (LOGD_DEVICE, "disconnecting for new activation request.");
7755 nm_device_state_changed (self,
7756 NM_DEVICE_STATE_DEACTIVATING,
7757 NM_DEVICE_STATE_REASON_NEW_ACTIVATION);
7762 * nm_device_is_activating
7764 * Return whether or not the device is currently activating itself.
7768 nm_device_is_activating (NMDevice *self)
7770 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7771 NMDeviceState state;
7773 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
7775 state = nm_device_get_state (self);
7776 if (state >= NM_DEVICE_STATE_PREPARE && state <= NM_DEVICE_STATE_SECONDARIES)
7779 /* There's a small race between the time when stage 1 is scheduled
7780 * and when the device actually sets STATE_PREPARE when the activation
7781 * handler is actually run. If there's an activation handler scheduled
7782 * we're activating anyway.
7784 return priv->act_handle4.id ? TRUE : FALSE;
7787 /* IP Configuration stuff */
7790 nm_device_get_dhcp4_config (NMDevice *self)
7792 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
7794 return NM_DEVICE_GET_PRIVATE (self)->dhcp4_config;
7798 nm_device_get_ip4_config (NMDevice *self)
7800 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
7802 return NM_DEVICE_GET_PRIVATE (self)->ip4_config;
7807 nm_device_set_ip4_config (NMDevice *self,
7808 NMIP4Config *new_config,
7809 guint32 default_route_metric,
7811 gboolean routes_full_sync,
7812 NMDeviceStateReason *reason)
7814 NMDevicePrivate *priv;
7815 NMIP4Config *old_config = NULL;
7816 gboolean has_changes = FALSE;
7817 gboolean success = TRUE;
7818 NMDeviceStateReason reason_local = NM_DEVICE_STATE_REASON_NONE;
7819 int ip_ifindex, config_ifindex;
7821 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
7823 priv = NM_DEVICE_GET_PRIVATE (self);
7824 ip_ifindex = nm_device_get_ip_ifindex (self);
7827 config_ifindex = nm_ip4_config_get_ifindex (new_config);
7828 if (config_ifindex > 0)
7829 g_return_val_if_fail (ip_ifindex == config_ifindex, FALSE);
7832 old_config = priv->ip4_config;
7834 /* Always commit to nm-platform to update lifetimes */
7835 if (commit && new_config) {
7836 gboolean assumed = nm_device_uses_assumed_connection (self);
7838 nm_device_set_mtu (self, nm_ip4_config_get_mtu (new_config));
7840 /* For assumed devices we must not touch the kernel-routes, such as the device-route.
7841 * FIXME: this is wrong in case where "assumed" means "take-over-seamlessly". In this
7842 * case, we should manage the device route, for example on new DHCP lease. */
7843 success = nm_ip4_config_commit (new_config, ip_ifindex,
7845 assumed ? (gint64) -1 : (gint64) default_route_metric);
7847 reason_local = NM_DEVICE_STATE_REASON_CONFIG_FAILED;
7852 /* has_changes is set only on relevant changes, because when the configuration changes,
7853 * this causes a re-read and reset. This should only happen for relevant changes */
7854 nm_ip4_config_replace (old_config, new_config, &has_changes);
7856 _LOGD (LOGD_IP4, "update IP4Config instance (%s)",
7857 nm_exported_object_get_path (NM_EXPORTED_OBJECT (old_config)));
7861 priv->ip4_config = g_object_ref (new_config);
7863 if (success && !nm_exported_object_is_exported (NM_EXPORTED_OBJECT (new_config)))
7864 nm_exported_object_export (NM_EXPORTED_OBJECT (new_config));
7866 _LOGD (LOGD_IP4, "set IP4Config instance (%s)",
7867 nm_exported_object_get_path (NM_EXPORTED_OBJECT (new_config)));
7869 } else if (old_config) {
7871 priv->ip4_config = NULL;
7872 _LOGD (LOGD_IP4, "clear IP4Config instance (%s)",
7873 nm_exported_object_get_path (NM_EXPORTED_OBJECT (old_config)));
7874 /* Device config is invalid if combined config is invalid */
7875 g_clear_object (&priv->dev_ip4_config);
7878 nm_default_route_manager_ip4_update_default_route (nm_default_route_manager_get (), self);
7881 _update_ip4_address (self);
7883 if (old_config != priv->ip4_config)
7884 _notify (self, PROP_IP4_CONFIG);
7885 g_signal_emit (self, signals[IP4_CONFIG_CHANGED], 0, priv->ip4_config, old_config);
7887 if (old_config != priv->ip4_config)
7888 nm_exported_object_clear_and_unexport (&old_config);
7890 if (nm_device_uses_generated_assumed_connection (self)) {
7891 NMConnection *connection = nm_device_get_applied_connection (self);
7892 NMConnection *settings_connection = NM_CONNECTION (nm_device_get_settings_connection (self));
7895 g_object_freeze_notify (G_OBJECT (connection));
7896 g_object_freeze_notify (G_OBJECT (settings_connection));
7898 nm_connection_remove_setting (settings_connection, NM_TYPE_SETTING_IP4_CONFIG);
7899 s_ip4 = nm_ip4_config_create_setting (priv->ip4_config);
7900 nm_connection_add_setting (settings_connection, s_ip4);
7902 nm_connection_remove_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
7903 s_ip4 = nm_ip4_config_create_setting (priv->ip4_config);
7904 nm_connection_add_setting (connection, s_ip4);
7906 g_object_thaw_notify (G_OBJECT (settings_connection));
7907 g_object_thaw_notify (G_OBJECT (connection));
7910 nm_device_queue_recheck_assume (self);
7914 *reason = reason_local;
7920 _replace_vpn_config_in_list (GSList **plist, GObject *old, GObject *new)
7924 /* Below, assert that @new is not yet tracked, but still behave
7925 * correctly in any case. Don't complain for missing @old since
7926 * it could have been removed when the parent device became
7930 && (old_link = g_slist_find (*plist, old))) {
7933 old_link->data = g_object_ref (new);
7935 *plist = g_slist_delete_link (*plist, old_link);
7936 g_object_unref (old);
7942 if (!g_slist_find (*plist, new))
7943 *plist = g_slist_append (*plist, g_object_ref (new));
7945 g_return_val_if_reached (TRUE);
7953 nm_device_replace_vpn4_config (NMDevice *self, NMIP4Config *old, NMIP4Config *config)
7955 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7957 if (!_replace_vpn_config_in_list (&priv->vpn4_configs, (GObject *) old, (GObject *) config))
7960 /* NULL to use existing configs */
7961 if (!ip4_config_merge_and_apply (self, NULL, TRUE, NULL))
7962 _LOGW (LOGD_IP4, "failed to set VPN routes for device");
7966 nm_device_set_wwan_ip4_config (NMDevice *self, NMIP4Config *config)
7968 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
7970 if (priv->wwan_ip4_config == config)
7973 g_clear_object (&priv->wwan_ip4_config);
7975 priv->wwan_ip4_config = g_object_ref (config);
7977 /* NULL to use existing configs */
7978 if (!ip4_config_merge_and_apply (self, NULL, TRUE, NULL))
7979 _LOGW (LOGD_IP4, "failed to set WWAN IPv4 configuration");
7983 nm_device_set_ip6_config (NMDevice *self,
7984 NMIP6Config *new_config,
7986 gboolean routes_full_sync,
7987 NMDeviceStateReason *reason)
7989 NMDevicePrivate *priv;
7990 NMIP6Config *old_config = NULL;
7991 gboolean has_changes = FALSE;
7992 gboolean success = TRUE;
7993 NMDeviceStateReason reason_local = NM_DEVICE_STATE_REASON_NONE;
7994 int ip_ifindex, config_ifindex;
7996 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
7998 priv = NM_DEVICE_GET_PRIVATE (self);
7999 ip_ifindex = nm_device_get_ip_ifindex (self);
8002 config_ifindex = nm_ip6_config_get_ifindex (new_config);
8003 if (config_ifindex > 0)
8004 g_return_val_if_fail (ip_ifindex == config_ifindex, FALSE);
8007 old_config = priv->ip6_config;
8009 /* Always commit to nm-platform to update lifetimes */
8010 if (commit && new_config) {
8011 nm_device_ipv6_set_mtu (self, priv->ip6_mtu);
8012 success = nm_ip6_config_commit (new_config,
8016 reason_local = NM_DEVICE_STATE_REASON_CONFIG_FAILED;
8021 /* has_changes is set only on relevant changes, because when the configuration changes,
8022 * this causes a re-read and reset. This should only happen for relevant changes */
8023 nm_ip6_config_replace (old_config, new_config, &has_changes);
8025 _LOGD (LOGD_IP6, "update IP6Config instance (%s)",
8026 nm_exported_object_get_path (NM_EXPORTED_OBJECT (old_config)));
8030 priv->ip6_config = g_object_ref (new_config);
8032 if (success && !nm_exported_object_is_exported (NM_EXPORTED_OBJECT (new_config)))
8033 nm_exported_object_export (NM_EXPORTED_OBJECT (new_config));
8035 _LOGD (LOGD_IP6, "set IP6Config instance (%s)",
8036 nm_exported_object_get_path (NM_EXPORTED_OBJECT (new_config)));
8038 } else if (old_config) {
8040 priv->ip6_config = NULL;
8041 _LOGD (LOGD_IP6, "clear IP6Config instance (%s)",
8042 nm_exported_object_get_path (NM_EXPORTED_OBJECT (old_config)));
8045 nm_default_route_manager_ip6_update_default_route (nm_default_route_manager_get (), self);
8048 if (old_config != priv->ip6_config)
8049 _notify (self, PROP_IP6_CONFIG);
8050 g_signal_emit (self, signals[IP6_CONFIG_CHANGED], 0, priv->ip6_config, old_config);
8052 if (old_config != priv->ip6_config)
8053 nm_exported_object_clear_and_unexport (&old_config);
8055 if (nm_device_uses_generated_assumed_connection (self)) {
8056 NMConnection *connection = nm_device_get_applied_connection (self);
8057 NMConnection *settings_connection = NM_CONNECTION (nm_device_get_settings_connection (self));
8060 g_object_freeze_notify (G_OBJECT (connection));
8061 g_object_freeze_notify (G_OBJECT (settings_connection));
8063 nm_connection_remove_setting (settings_connection, NM_TYPE_SETTING_IP6_CONFIG);
8064 s_ip6 = nm_ip6_config_create_setting (priv->ip6_config);
8065 nm_connection_add_setting (settings_connection, s_ip6);
8067 nm_connection_remove_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
8068 s_ip6 = nm_ip6_config_create_setting (priv->ip6_config);
8069 nm_connection_add_setting (connection, s_ip6);
8071 g_object_thaw_notify (G_OBJECT (settings_connection));
8072 g_object_thaw_notify (G_OBJECT (connection));
8075 nm_device_queue_recheck_assume (self);
8079 *reason = reason_local;
8085 nm_device_replace_vpn6_config (NMDevice *self, NMIP6Config *old, NMIP6Config *config)
8087 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8089 if (!_replace_vpn_config_in_list (&priv->vpn6_configs, (GObject *) old, (GObject *) config))
8092 /* NULL to use existing configs */
8093 if (!ip6_config_merge_and_apply (self, TRUE, NULL))
8094 _LOGW (LOGD_IP6, "failed to set VPN routes for device");
8098 nm_device_set_wwan_ip6_config (NMDevice *self, NMIP6Config *config)
8100 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8102 if (priv->wwan_ip6_config == config)
8105 g_clear_object (&priv->wwan_ip6_config);
8107 priv->wwan_ip6_config = g_object_ref (config);
8109 /* NULL to use existing configs */
8110 if (!ip6_config_merge_and_apply (self, TRUE, NULL))
8111 _LOGW (LOGD_IP6, "failed to set WWAN IPv6 configuration");
8115 nm_device_get_dhcp6_config (NMDevice *self)
8117 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
8119 return NM_DEVICE_GET_PRIVATE (self)->dhcp6_config;
8123 nm_device_get_ip6_config (NMDevice *self)
8125 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
8127 return NM_DEVICE_GET_PRIVATE (self)->ip6_config;
8130 /****************************************************************/
8133 dispatcher_cleanup (NMDevice *self)
8135 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8137 if (priv->dispatcher.call_id) {
8138 nm_dispatcher_call_cancel (priv->dispatcher.call_id);
8139 priv->dispatcher.call_id = 0;
8140 priv->dispatcher.post_state = NM_DEVICE_STATE_UNKNOWN;
8141 priv->dispatcher.post_state_reason = NM_DEVICE_STATE_REASON_NONE;
8146 dispatcher_complete_proceed_state (guint call_id, gpointer user_data)
8148 NMDevice *self = NM_DEVICE (user_data);
8149 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8151 g_return_if_fail (call_id == priv->dispatcher.call_id);
8153 priv->dispatcher.call_id = 0;
8154 nm_device_queue_state (self, priv->dispatcher.post_state,
8155 priv->dispatcher.post_state_reason);
8156 priv->dispatcher.post_state = NM_DEVICE_STATE_UNKNOWN;
8157 priv->dispatcher.post_state_reason = NM_DEVICE_STATE_REASON_NONE;
8160 /****************************************************************/
8163 ip_check_pre_up (NMDevice *self)
8165 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8167 if (priv->dispatcher.call_id != 0) {
8168 g_warn_if_reached ();
8169 dispatcher_cleanup (self);
8172 priv->dispatcher.post_state = NM_DEVICE_STATE_SECONDARIES;
8173 priv->dispatcher.post_state_reason = NM_DEVICE_STATE_REASON_NONE;
8174 if (!nm_dispatcher_call (DISPATCHER_ACTION_PRE_UP,
8175 nm_device_get_settings_connection (self),
8176 nm_device_get_applied_connection (self),
8178 dispatcher_complete_proceed_state,
8180 &priv->dispatcher.call_id)) {
8181 /* Just proceed on errors */
8182 dispatcher_complete_proceed_state (0, self);
8187 ip_check_gw_ping_cleanup (NMDevice *self)
8189 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8191 nm_clear_g_source (&priv->gw_ping.watch);
8192 nm_clear_g_source (&priv->gw_ping.timeout);
8194 if (priv->gw_ping.pid) {
8195 nm_utils_kill_child_async (priv->gw_ping.pid, SIGTERM, priv->gw_ping.log_domain, "ping", 1000, NULL, NULL);
8196 priv->gw_ping.pid = 0;
8199 g_clear_pointer (&priv->gw_ping.binary, g_free);
8200 g_clear_pointer (&priv->gw_ping.address, g_free);
8204 spawn_ping (NMDevice *self)
8206 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8207 gs_free char *str_timeout = NULL;
8208 gs_free char *tmp_str = NULL;
8209 const char *args[] = { priv->gw_ping.binary, "-I", nm_device_get_ip_iface (self),
8210 "-c", "1", "-w", NULL, priv->gw_ping.address, NULL };
8211 gs_free_error GError *error = NULL;
8214 args[6] = str_timeout = g_strdup_printf ("%u", priv->gw_ping.deadline);
8215 tmp_str = g_strjoinv (" ", (gchar **) args);
8216 _LOGD (priv->gw_ping.log_domain, "ping: running '%s'", tmp_str);
8218 ret = g_spawn_async ("/",
8221 G_SPAWN_DO_NOT_REAP_CHILD,
8228 _LOGW (priv->gw_ping.log_domain, "ping: could not spawn %s: %s",
8229 priv->gw_ping.binary, error->message);
8236 respawn_ping_cb (gpointer user_data)
8238 NMDevice *self = NM_DEVICE (user_data);
8239 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8241 priv->gw_ping.watch = 0;
8243 if (spawn_ping (self)) {
8244 priv->gw_ping.watch = g_child_watch_add (priv->gw_ping.pid,
8245 ip_check_ping_watch_cb, self);
8247 ip_check_gw_ping_cleanup (self);
8248 ip_check_pre_up (self);
8255 ip_check_ping_watch_cb (GPid pid, gint status, gpointer user_data)
8257 NMDevice *self = NM_DEVICE (user_data);
8258 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8259 NMLogDomain log_domain = priv->gw_ping.log_domain;
8260 gboolean success = FALSE;
8262 if (!priv->gw_ping.watch)
8264 priv->gw_ping.watch = 0;
8265 priv->gw_ping.pid = 0;
8267 if (WIFEXITED (status)) {
8268 if (WEXITSTATUS (status) == 0) {
8269 _LOGD (log_domain, "ping: gateway ping succeeded");
8272 _LOGW (log_domain, "ping: gateway ping failed with error code %d",
8273 WEXITSTATUS (status));
8276 _LOGW (log_domain, "ping: stopped unexpectedly with status %d", status);
8279 /* We've got connectivity, proceed to pre_up */
8280 ip_check_gw_ping_cleanup (self);
8281 ip_check_pre_up (self);
8283 /* If ping exited with an error it may have returned early,
8284 * wait 1 second and restart it */
8285 priv->gw_ping.watch = g_timeout_add_seconds (1, respawn_ping_cb, self);
8290 ip_check_ping_timeout_cb (gpointer user_data)
8292 NMDevice *self = NM_DEVICE (user_data);
8293 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8295 priv->gw_ping.timeout = 0;
8297 _LOGW (priv->gw_ping.log_domain, "ping: gateway ping timed out");
8299 ip_check_gw_ping_cleanup (self);
8300 ip_check_pre_up (self);
8305 start_ping (NMDevice *self,
8306 NMLogDomain log_domain,
8308 const char *address,
8311 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8313 g_return_val_if_fail (priv->gw_ping.watch == 0, FALSE);
8314 g_return_val_if_fail (priv->gw_ping.timeout == 0, FALSE);
8316 priv->gw_ping.log_domain = log_domain;
8317 priv->gw_ping.address = g_strdup (address);
8318 priv->gw_ping.binary = g_strdup (binary);
8319 priv->gw_ping.deadline = timeout + 10; /* the proper termination is enforced by a timer */
8321 if (spawn_ping (self)) {
8322 priv->gw_ping.watch = g_child_watch_add (priv->gw_ping.pid, ip_check_ping_watch_cb, self);
8323 priv->gw_ping.timeout = g_timeout_add_seconds (timeout, ip_check_ping_timeout_cb, self);
8327 ip_check_gw_ping_cleanup (self);
8332 nm_device_start_ip_check (NMDevice *self)
8334 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8335 NMConnection *connection;
8336 NMSettingConnection *s_con;
8338 const char *ping_binary = NULL;
8339 char buf[INET6_ADDRSTRLEN] = { 0 };
8340 NMLogDomain log_domain = LOGD_IP4;
8342 /* Shouldn't be any active ping here, since IP_CHECK happens after the
8343 * first IP method completes. Any subsequently completing IP method doesn't
8346 g_assert (!priv->gw_ping.watch);
8347 g_assert (!priv->gw_ping.timeout);
8348 g_assert (!priv->gw_ping.pid);
8349 g_assert (priv->ip4_state == IP_DONE || priv->ip6_state == IP_DONE);
8351 connection = nm_device_get_applied_connection (self);
8352 g_assert (connection);
8354 s_con = nm_connection_get_setting_connection (connection);
8356 timeout = nm_setting_connection_get_gateway_ping_timeout (s_con);
8359 if (priv->ip4_config && priv->ip4_state == IP_DONE) {
8362 ping_binary = nm_utils_find_helper ("ping", "/usr/bin/ping", NULL);
8363 log_domain = LOGD_IP4;
8365 gw = nm_ip4_config_get_gateway (priv->ip4_config);
8366 if (gw && !inet_ntop (AF_INET, &gw, buf, sizeof (buf)))
8368 } else if (priv->ip6_config && priv->ip6_state == IP_DONE) {
8369 const struct in6_addr *gw = NULL;
8371 ping_binary = nm_utils_find_helper ("ping6", "/usr/bin/ping6", NULL);
8372 log_domain = LOGD_IP6;
8374 gw = nm_ip6_config_get_gateway (priv->ip6_config);
8375 if (gw && !inet_ntop (AF_INET6, gw, buf, sizeof (buf)))
8381 start_ping (self, log_domain, ping_binary, buf, timeout);
8383 /* If no ping was started, just advance to pre_up */
8384 if (!priv->gw_ping.pid)
8385 ip_check_pre_up (self);
8388 /****************************************************************/
8391 carrier_wait_timeout (gpointer user_data)
8393 NMDevice *self = NM_DEVICE (user_data);
8395 NM_DEVICE_GET_PRIVATE (self)->carrier_wait_id = 0;
8396 nm_device_remove_pending_action (self, "carrier wait", TRUE);
8398 _carrier_wait_check_queued_act_request (self);
8400 return G_SOURCE_REMOVE;
8404 nm_device_is_up (NMDevice *self)
8406 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
8408 if (NM_DEVICE_GET_CLASS (self)->is_up)
8409 return NM_DEVICE_GET_CLASS (self)->is_up (self);
8415 is_up (NMDevice *self)
8417 int ifindex = nm_device_get_ip_ifindex (self);
8419 return ifindex > 0 ? nm_platform_link_is_up (NM_PLATFORM_GET, ifindex) : TRUE;
8423 nm_device_bring_up (NMDevice *self, gboolean block, gboolean *no_firmware)
8425 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8426 gboolean device_is_up = FALSE;
8428 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
8430 _LOGD (LOGD_HW, "bringing up device");
8432 if (NM_DEVICE_GET_CLASS (self)->bring_up) {
8433 if (!NM_DEVICE_GET_CLASS (self)->bring_up (self, no_firmware))
8437 device_is_up = nm_device_is_up (self);
8438 if (block && !device_is_up) {
8439 int ifindex = nm_device_get_ip_ifindex (self);
8440 gint64 wait_until = nm_utils_get_monotonic_timestamp_us () + 10000 /* microseconds */;
8444 if (!nm_platform_link_refresh (NM_PLATFORM_GET, ifindex))
8446 device_is_up = nm_device_is_up (self);
8447 } while (!device_is_up && nm_utils_get_monotonic_timestamp_us () < wait_until);
8450 if (!device_is_up) {
8452 _LOGW (LOGD_HW, "device not up after timeout!");
8454 _LOGD (LOGD_HW, "device not up immediately");
8458 /* Devices that support carrier detect must be IFF_UP to report carrier
8459 * changes; so after setting the device IFF_UP we must suppress startup
8460 * complete (via a pending action) until either the carrier turns on, or
8461 * a timeout is reached.
8463 if (nm_device_has_capability (self, NM_DEVICE_CAP_CARRIER_DETECT)) {
8464 if (!nm_clear_g_source (&priv->carrier_wait_id))
8465 nm_device_add_pending_action (self, "carrier wait", TRUE);
8466 priv->carrier_wait_id = g_timeout_add_seconds (5, carrier_wait_timeout, self);
8469 /* Can only get HW address of some devices when they are up */
8470 nm_device_update_hw_address (self);
8472 _update_ip4_address (self);
8477 bring_up (NMDevice *self, gboolean *no_firmware)
8479 int ifindex = nm_device_get_ip_ifindex (self);
8484 *no_firmware = FALSE;
8488 result = nm_platform_link_set_up (NM_PLATFORM_GET, ifindex, no_firmware);
8490 /* Store carrier immediately. */
8491 if (result && nm_device_has_capability (self, NM_DEVICE_CAP_CARRIER_DETECT))
8492 check_carrier (self);
8498 nm_device_take_down (NMDevice *self, gboolean block)
8500 gboolean device_is_up;
8502 g_return_if_fail (NM_IS_DEVICE (self));
8504 _LOGD (LOGD_HW, "taking down device");
8506 if (NM_DEVICE_GET_CLASS (self)->take_down) {
8507 if (!NM_DEVICE_GET_CLASS (self)->take_down (self))
8511 device_is_up = nm_device_is_up (self);
8512 if (block && device_is_up) {
8513 int ifindex = nm_device_get_ip_ifindex (self);
8514 gint64 wait_until = nm_utils_get_monotonic_timestamp_us () + 10000 /* microseconds */;
8518 if (!nm_platform_link_refresh (NM_PLATFORM_GET, ifindex))
8520 device_is_up = nm_device_is_up (self);
8521 } while (device_is_up && nm_utils_get_monotonic_timestamp_us () < wait_until);
8526 _LOGW (LOGD_HW, "device not down after timeout!");
8528 _LOGD (LOGD_HW, "device not down immediately");
8533 take_down (NMDevice *self)
8535 int ifindex = nm_device_get_ip_ifindex (self);
8538 return nm_platform_link_set_down (NM_PLATFORM_GET, ifindex);
8540 /* devices without ifindex are always up. */
8541 _LOGD (LOGD_HW, "cannot take down device without ifindex");
8546 nm_device_set_firmware_missing (NMDevice *self, gboolean new_missing)
8548 NMDevicePrivate *priv;
8550 g_return_if_fail (NM_IS_DEVICE (self));
8552 priv = NM_DEVICE_GET_PRIVATE (self);
8553 if (priv->firmware_missing != new_missing) {
8554 priv->firmware_missing = new_missing;
8555 _notify (self, PROP_FIRMWARE_MISSING);
8560 nm_device_get_firmware_missing (NMDevice *self)
8562 return NM_DEVICE_GET_PRIVATE (self)->firmware_missing;
8566 nm_device_set_nm_plugin_missing (NMDevice *self, gboolean new_missing)
8568 NMDevicePrivate *priv;
8570 g_return_if_fail (NM_IS_DEVICE (self));
8572 priv = NM_DEVICE_GET_PRIVATE (self);
8573 if (priv->nm_plugin_missing != new_missing) {
8574 priv->nm_plugin_missing = new_missing;
8575 _notify (self, PROP_NM_PLUGIN_MISSING);
8580 nm_device_get_nm_plugin_missing (NMDevice *self)
8582 return NM_DEVICE_GET_PRIVATE (self)->nm_plugin_missing;
8585 static NMIP4Config *
8586 find_ip4_lease_config (NMDevice *self,
8587 NMConnection *connection,
8588 NMIP4Config *ext_ip4_config)
8590 const char *ip_iface = nm_device_get_ip_iface (self);
8591 int ip_ifindex = nm_device_get_ip_ifindex (self);
8592 GSList *leases, *liter;
8593 NMIP4Config *found = NULL;
8595 g_return_val_if_fail (NM_IS_IP4_CONFIG (ext_ip4_config), NULL);
8596 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
8598 leases = nm_dhcp_manager_get_lease_ip_configs (nm_dhcp_manager_get (),
8601 nm_connection_get_uuid (connection),
8603 nm_device_get_ip4_route_metric (self));
8604 for (liter = leases; liter && !found; liter = liter->next) {
8605 NMIP4Config *lease_config = liter->data;
8606 const NMPlatformIP4Address *address = nm_ip4_config_get_address (lease_config, 0);
8607 guint32 gateway = nm_ip4_config_get_gateway (lease_config);
8610 if (!nm_ip4_config_address_exists (ext_ip4_config, address))
8612 if (gateway != nm_ip4_config_get_gateway (ext_ip4_config))
8614 found = g_object_ref (lease_config);
8617 g_slist_free_full (leases, g_object_unref);
8622 capture_lease_config (NMDevice *self,
8623 NMIP4Config *ext_ip4_config,
8624 NMIP4Config **out_ip4_config,
8625 NMIP6Config *ext_ip6_config,
8626 NMIP6Config **out_ip6_config)
8628 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8629 const GSList *connections, *citer;
8631 gboolean dhcp_used = FALSE;
8633 /* Ensure at least one address on the device has a non-infinite lifetime,
8634 * otherwise DHCP cannot possibly be active on the device right now.
8636 if (ext_ip4_config && out_ip4_config) {
8637 for (i = 0; i < nm_ip4_config_get_num_addresses (ext_ip4_config); i++) {
8638 const NMPlatformIP4Address *addr = nm_ip4_config_get_address (ext_ip4_config, i);
8640 if (addr->lifetime != NM_PLATFORM_LIFETIME_PERMANENT) {
8645 } else if (ext_ip6_config && out_ip6_config) {
8646 for (i = 0; i < nm_ip6_config_get_num_addresses (ext_ip6_config); i++) {
8647 const NMPlatformIP6Address *addr = nm_ip6_config_get_address (ext_ip6_config, i);
8649 if (addr->lifetime != NM_PLATFORM_LIFETIME_PERMANENT) {
8655 g_return_if_fail ( (ext_ip6_config && out_ip6_config)
8656 || (ext_ip4_config && out_ip4_config));
8662 connections = nm_connection_provider_get_connections (priv->con_provider);
8663 for (citer = connections; citer; citer = citer->next) {
8664 NMConnection *candidate = citer->data;
8667 if (!nm_device_check_connection_compatible (self, candidate))
8671 method = nm_utils_get_ip_config_method (candidate, NM_TYPE_SETTING_IP4_CONFIG);
8672 if (out_ip4_config && strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0) {
8673 *out_ip4_config = find_ip4_lease_config (self, candidate, ext_ip4_config);
8674 if (*out_ip4_config)
8679 method = nm_utils_get_ip_config_method (candidate, NM_TYPE_SETTING_IP6_CONFIG);
8680 if (out_ip6_config && strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0) {
8681 /* FIXME: implement find_ip6_lease_config() */
8687 _ip4_config_intersect (gpointer value, gpointer user_data)
8689 NMIP4Config *dst = (NMIP4Config *) value;
8690 NMIP4Config *src = (NMIP4Config *) user_data;
8692 nm_ip4_config_intersect (dst, src);
8696 _ip4_config_subtract (gpointer value, gpointer user_data)
8698 NMIP4Config *dst = (NMIP4Config *) user_data;
8699 NMIP4Config *src = (NMIP4Config *) value;
8701 nm_ip4_config_subtract (dst, src);
8705 update_ip4_config (NMDevice *self, gboolean initial)
8707 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8709 gboolean capture_resolv_conf;
8710 NMDnsManagerResolvConfMode resolv_conf_mode;
8712 ifindex = nm_device_get_ip_ifindex (self);
8716 resolv_conf_mode = nm_dns_manager_get_resolv_conf_mode (nm_dns_manager_get ());
8717 capture_resolv_conf = initial && (resolv_conf_mode == NM_DNS_MANAGER_RESOLV_CONF_EXPLICIT);
8720 g_clear_object (&priv->ext_ip4_config);
8721 priv->ext_ip4_config = nm_ip4_config_capture (ifindex, capture_resolv_conf);
8722 if (priv->ext_ip4_config) {
8724 g_clear_object (&priv->dev_ip4_config);
8725 capture_lease_config (self, priv->ext_ip4_config, &priv->dev_ip4_config, NULL, NULL);
8728 /* FIXME: ext_ip4_config does not contain routes with source==RTPROT_KERNEL.
8729 * Hence, we will wrongly remove device-routes with metric=0 if they were added by
8730 * the user on purpose. This should be fixed by also tracking and exposing
8733 /* This function was called upon external changes. Remove the configuration
8734 * (addresses,routes) that is no longer present externally from the internal
8735 * config. This way, we don't re-add addresses that were manually removed
8737 if (priv->con_ip4_config)
8738 nm_ip4_config_intersect (priv->con_ip4_config, priv->ext_ip4_config);
8739 if (priv->dev_ip4_config)
8740 nm_ip4_config_intersect (priv->dev_ip4_config, priv->ext_ip4_config);
8742 g_slist_foreach (priv->vpn4_configs, _ip4_config_intersect, priv->ext_ip4_config);
8744 if (priv->wwan_ip4_config)
8745 nm_ip4_config_intersect (priv->wwan_ip4_config, priv->ext_ip4_config);
8747 /* Remove parts from ext_ip4_config to only contain the information that
8748 * was configured externally -- we already have the same configuration from
8749 * internal origins. */
8750 if (priv->con_ip4_config)
8751 nm_ip4_config_subtract (priv->ext_ip4_config, priv->con_ip4_config);
8752 if (priv->dev_ip4_config)
8753 nm_ip4_config_subtract (priv->ext_ip4_config, priv->dev_ip4_config);
8755 g_slist_foreach (priv->vpn4_configs, _ip4_config_subtract, priv->ext_ip4_config);
8757 if (priv->wwan_ip4_config)
8758 nm_ip4_config_subtract (priv->ext_ip4_config, priv->wwan_ip4_config);
8760 ip4_config_merge_and_apply (self, NULL, FALSE, NULL);
8765 _ip6_config_intersect (gpointer value, gpointer user_data)
8767 NMIP6Config *dst = (NMIP6Config *) value;
8768 NMIP6Config *src = (NMIP6Config *) user_data;
8770 nm_ip6_config_intersect (dst, src);
8774 _ip6_config_subtract (gpointer value, gpointer user_data)
8776 NMIP6Config *dst = (NMIP6Config *) user_data;
8777 NMIP6Config *src = (NMIP6Config *) value;
8779 nm_ip6_config_subtract (dst, src);
8783 update_ip6_config (NMDevice *self, gboolean initial)
8785 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8787 gboolean capture_resolv_conf;
8788 NMDnsManagerResolvConfMode resolv_conf_mode;
8790 ifindex = nm_device_get_ip_ifindex (self);
8794 resolv_conf_mode = nm_dns_manager_get_resolv_conf_mode (nm_dns_manager_get ());
8795 capture_resolv_conf = initial && (resolv_conf_mode == NM_DNS_MANAGER_RESOLV_CONF_EXPLICIT);
8798 g_clear_object (&priv->ext_ip6_config);
8799 g_clear_object (&priv->ext_ip6_config_captured);
8800 priv->ext_ip6_config_captured = nm_ip6_config_capture (ifindex, capture_resolv_conf, NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN);
8801 if (priv->ext_ip6_config_captured) {
8803 priv->ext_ip6_config = nm_ip6_config_new_cloned (priv->ext_ip6_config_captured);
8805 /* This function was called upon external changes. Remove the configuration
8806 * (addresses,routes) that is no longer present externally from the internal
8807 * config. This way, we don't re-add addresses that were manually removed
8809 if (priv->con_ip6_config)
8810 nm_ip6_config_intersect (priv->con_ip6_config, priv->ext_ip6_config);
8811 if (priv->ac_ip6_config)
8812 nm_ip6_config_intersect (priv->ac_ip6_config, priv->ext_ip6_config);
8813 if (priv->dhcp6_ip6_config)
8814 nm_ip6_config_intersect (priv->dhcp6_ip6_config, priv->ext_ip6_config);
8815 if (priv->wwan_ip6_config)
8816 nm_ip6_config_intersect (priv->wwan_ip6_config, priv->ext_ip6_config);
8817 g_slist_foreach (priv->vpn6_configs, _ip6_config_intersect, priv->ext_ip6_config);
8819 /* Remove parts from ext_ip6_config to only contain the information that
8820 * was configured externally -- we already have the same configuration from
8821 * internal origins. */
8822 if (priv->con_ip6_config)
8823 nm_ip6_config_subtract (priv->ext_ip6_config, priv->con_ip6_config);
8824 if (priv->ac_ip6_config)
8825 nm_ip6_config_subtract (priv->ext_ip6_config, priv->ac_ip6_config);
8826 if (priv->dhcp6_ip6_config)
8827 nm_ip6_config_subtract (priv->ext_ip6_config, priv->dhcp6_ip6_config);
8828 if (priv->wwan_ip6_config)
8829 nm_ip6_config_subtract (priv->ext_ip6_config, priv->wwan_ip6_config);
8830 g_slist_foreach (priv->vpn6_configs, _ip6_config_subtract, priv->ext_ip6_config);
8832 ip6_config_merge_and_apply (self, FALSE, NULL);
8835 if ( priv->linklocal6_timeout_id
8836 && priv->ext_ip6_config_captured
8837 && nm_ip6_config_get_address_first_nontentative (priv->ext_ip6_config_captured, TRUE)) {
8838 /* linklocal6 is ready now, do the state transition... we are also
8839 * invoked as g_idle_add, so no problems with reentrance doing it now.
8841 linklocal6_complete (self);
8846 nm_device_capture_initial_config (NMDevice *self)
8848 update_ip4_config (self, TRUE);
8849 update_ip6_config (self, TRUE);
8853 queued_ip4_config_change (gpointer user_data)
8855 NMDevice *self = NM_DEVICE (user_data);
8856 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8858 /* Wait for any queued state changes */
8859 if (priv->queued_state.id)
8862 priv->queued_ip4_config_id = 0;
8863 g_object_ref (self);
8864 update_ip4_config (self, FALSE);
8865 g_object_unref (self);
8871 queued_ip6_config_change (gpointer user_data)
8873 NMDevice *self = NM_DEVICE (user_data);
8874 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
8876 gboolean need_ipv6ll = FALSE;
8878 /* Wait for any queued state changes */
8879 if (priv->queued_state.id)
8882 priv->queued_ip6_config_id = 0;
8883 g_object_ref (self);
8884 update_ip6_config (self, FALSE);
8886 if ( nm_platform_link_get (NM_PLATFORM_GET, priv->ifindex)
8887 && priv->state < NM_DEVICE_STATE_DEACTIVATING) {
8888 /* Handle DAD falures */
8889 for (iter = priv->dad6_failed_addrs; iter; iter = g_slist_next (iter)) {
8890 NMPlatformIP6Address *addr = iter->data;
8892 if (addr->source >= NM_IP_CONFIG_SOURCE_USER)
8895 _LOGI (LOGD_IP6, "ipv6: duplicate address check failed for the %s address",
8896 nm_platform_ip6_address_to_string (addr, NULL, 0));
8898 if (IN6_IS_ADDR_LINKLOCAL (&addr->address))
8900 else if (priv->rdisc)
8901 nm_rdisc_dad_failed (priv->rdisc, &addr->address);
8904 /* If no IPv6 link-local address exists but other addresses do then we
8905 * must add the LL address to remain conformant with RFC 3513 chapter 2.1
8906 * ("Addressing Model"): "All interfaces are required to have at least
8907 * one link-local unicast address".
8909 if (priv->ip6_config && nm_ip6_config_get_num_addresses (priv->ip6_config))
8913 check_and_add_ipv6ll_addr (self);
8916 g_slist_free_full (priv->dad6_failed_addrs, g_free);
8917 priv->dad6_failed_addrs = NULL;
8919 g_object_unref (self);
8925 device_ipx_changed (NMPlatform *platform,
8926 NMPObjectType obj_type,
8928 gpointer platform_object,
8929 NMPlatformSignalChangeType change_type,
8932 NMDevicePrivate *priv;
8933 NMPlatformIP6Address *addr;
8935 if (nm_device_get_ip_ifindex (self) != ifindex)
8938 priv = NM_DEVICE_GET_PRIVATE (self);
8941 case NMP_OBJECT_TYPE_IP4_ADDRESS:
8942 case NMP_OBJECT_TYPE_IP4_ROUTE:
8943 if (!priv->queued_ip4_config_id) {
8944 priv->queued_ip4_config_id = g_idle_add (queued_ip4_config_change, self);
8945 _LOGD (LOGD_DEVICE, "queued IP4 config change");
8948 case NMP_OBJECT_TYPE_IP6_ADDRESS:
8949 addr = platform_object;
8951 if ( priv->state > NM_DEVICE_STATE_DISCONNECTED
8952 && priv->state < NM_DEVICE_STATE_DEACTIVATING
8953 && ( (change_type == NM_PLATFORM_SIGNAL_CHANGED && addr->n_ifa_flags & IFA_F_DADFAILED)
8954 || (change_type == NM_PLATFORM_SIGNAL_REMOVED && addr->n_ifa_flags & IFA_F_TENTATIVE))) {
8955 priv->dad6_failed_addrs = g_slist_append (priv->dad6_failed_addrs,
8956 g_memdup (addr, sizeof (NMPlatformIP6Address)));
8959 case NMP_OBJECT_TYPE_IP6_ROUTE:
8960 if (!priv->queued_ip6_config_id) {
8961 priv->queued_ip6_config_id = g_idle_add (queued_ip6_config_change, self);
8962 _LOGD (LOGD_DEVICE, "queued IP6 config change");
8966 g_return_if_reached ();
8970 /*****************************************************************************/
8972 NM_UTILS_FLAGS2STR_DEFINE (nm_unmanaged_flags2str, NMUnmanagedFlags,
8973 NM_UTILS_FLAGS2STR (NM_UNMANAGED_SLEEPING, "sleeping"),
8974 NM_UTILS_FLAGS2STR (NM_UNMANAGED_QUITTING, "quitting"),
8975 NM_UTILS_FLAGS2STR (NM_UNMANAGED_PARENT, "parent"),
8976 NM_UTILS_FLAGS2STR (NM_UNMANAGED_LOOPBACK, "loopback"),
8977 NM_UTILS_FLAGS2STR (NM_UNMANAGED_PLATFORM_INIT, "platform-init"),
8978 NM_UTILS_FLAGS2STR (NM_UNMANAGED_USER_EXPLICIT, "user-explicit"),
8979 NM_UTILS_FLAGS2STR (NM_UNMANAGED_BY_DEFAULT, "by-default"),
8980 NM_UTILS_FLAGS2STR (NM_UNMANAGED_USER_SETTINGS, "user-settings"),
8981 NM_UTILS_FLAGS2STR (NM_UNMANAGED_USER_UDEV, "user-udev"),
8982 NM_UTILS_FLAGS2STR (NM_UNMANAGED_EXTERNAL_DOWN, "external-down"),
8983 NM_UTILS_FLAGS2STR (NM_UNMANAGED_IS_SLAVE, "is-slave"),
8987 _unmanaged_flags2str (NMUnmanagedFlags flags, NMUnmanagedFlags mask, char *buf, gsize len)
8994 nm_utils_to_string_buffer_init (&buf, &len);
9002 nm_unmanaged_flags2str (flags, b, len);
9007 nm_unmanaged_flags2str (mask & ~flags, buf2, sizeof (buf2));
9009 gboolean add_separator = l > 0;
9014 nm_utils_strbuf_append_c (&b, &len, ',');
9015 add_separator = TRUE;
9017 tmp2 = strchr (tmp, ',');
9021 nm_utils_strbuf_append_c (&b, &len, '!');
9022 nm_utils_strbuf_append_str (&b, &len, tmp);
9034 _get_managed_by_flags(NMUnmanagedFlags flags, NMUnmanagedFlags mask, gboolean for_user_request)
9036 /* Evaluate the managed state based on the unmanaged flags.
9038 * Some flags are authoritative, meaning they always cause
9039 * the device to be unmanaged (e.g. @NM_UNMANAGED_PLATFORM_INIT).
9041 * OTOH, some flags can be overwritten. For example NM_UNMANAGED_USER_SETTINGS
9042 * is ignored once NM_UNMANAGED_USER_EXPLICIT is set. The idea is that
9043 * the flag from the configuration has no effect once the user explicitly
9044 * touches the unmanaged flags. */
9046 if (for_user_request) {
9048 /* @for_user_request can make the result only ~more~ managed.
9049 * If the flags already indicate a managed state for a non-user-request,
9050 * then it is also managed for an explict user-request.
9052 * Effectively, this check is redundant, as the code below already
9053 * already ensures that. Still, express this invariant explictly here. */
9054 if (_get_managed_by_flags (flags, mask, FALSE))
9057 /* A for-user-request, is effectively the same as pretending
9058 * that user-dbus flag is cleared. */
9059 mask |= NM_UNMANAGED_USER_EXPLICIT;
9060 flags &= ~NM_UNMANAGED_USER_EXPLICIT;
9063 if ( NM_FLAGS_ANY (mask, NM_UNMANAGED_USER_SETTINGS)
9064 && !NM_FLAGS_ANY (flags, NM_UNMANAGED_USER_SETTINGS)) {
9065 /* NM_UNMANAGED_USER_SETTINGS can only explicitly unmanage a device. It cannot
9066 * *manage* it. Having NM_UNMANAGED_USER_SETTINGS explicitly not set, is the
9067 * same as having it not set at all. */
9068 mask &= ~NM_UNMANAGED_USER_SETTINGS;
9071 if (NM_FLAGS_ANY (mask, NM_UNMANAGED_USER_UDEV)) {
9072 /* configuration from udev or nm-config overwrites the by-default flag
9073 * which is based on the device type. */
9074 flags &= ~NM_UNMANAGED_BY_DEFAULT;
9077 if ( NM_FLAGS_HAS (mask, NM_UNMANAGED_IS_SLAVE)
9078 && !NM_FLAGS_HAS (flags, NM_UNMANAGED_IS_SLAVE)) {
9079 /* for an enslaved device, by-default doesn't matter */
9080 flags &= ~NM_UNMANAGED_BY_DEFAULT;
9083 if (NM_FLAGS_HAS (mask, NM_UNMANAGED_USER_EXPLICIT)) {
9084 /* if the device is managed by user-decision, certain other flags
9087 flags &= ~( NM_UNMANAGED_BY_DEFAULT
9088 | NM_UNMANAGED_USER_UDEV
9089 | NM_UNMANAGED_EXTERNAL_DOWN);
9092 return flags == NM_UNMANAGED_NONE;
9096 * nm_device_get_managed:
9097 * @self: the #NMDevice
9098 * @for_user_request: whether to check the flags for an explict user-request
9100 * Whether the device is unmanaged according to the unmanaged flags.
9102 * Returns: %TRUE if the device is unmanaged because of the flags.
9105 nm_device_get_managed (NMDevice *self, gboolean for_user_request)
9107 NMDevicePrivate *priv;
9109 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
9111 if (!nm_device_is_real (self)) {
9112 /* a unrealized device is always considered unmanaged. */
9116 priv = NM_DEVICE_GET_PRIVATE (self);
9118 return _get_managed_by_flags (priv->unmanaged_flags, priv->unmanaged_mask, for_user_request);
9122 * nm_device_get_unmanaged_flags:
9123 * @self: the #NMDevice
9124 * @flag: the unmanaged flags to check.
9126 * Return the unmanaged flags of the device.
9128 * Returns: the flags of the device ( & @flag)
9131 nm_device_get_unmanaged_flags (NMDevice *self, NMUnmanagedFlags flag)
9133 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
9134 g_return_val_if_fail (flag != NM_UNMANAGED_NONE, FALSE);
9136 return NM_DEVICE_GET_PRIVATE (self)->unmanaged_flags & flag;
9140 * _set_unmanaged_flags:
9141 * @self: the #NMDevice instance
9142 * @flags: which #NMUnmanagedFlags to set.
9143 * @set_op: whether to set/clear/forget the flags. You can also pass
9144 * boolean values %TRUE and %FALSE, which mean %NM_UNMAN_FLAG_OP_SET_UNMANAGED
9145 * and %NM_UNMAN_FLAG_OP_SET_MANAGED, respectively.
9146 * @allow_state_transition: if %FALSE, setting flags never triggers a device
9147 * state change. If %TRUE, the device can change state, if it is real and
9148 * switches from managed to unmanaged (or vice versa).
9149 * @reason: the device state reason passed to nm_device_state_changed() if
9150 * the device becomes managed/unmanaged. This is only relevant if the
9151 * device switches state and if @allow_state_transition is %TRUE.
9153 * Set the unmanaged flags of the device.
9156 _set_unmanaged_flags (NMDevice *self,
9157 NMUnmanagedFlags flags,
9158 NMUnmanFlagOp set_op,
9159 gboolean allow_state_transition,
9160 NMDeviceStateReason reason)
9162 NMDevicePrivate *priv;
9163 gboolean was_managed, transition_state;
9164 NMUnmanagedFlags old_flags, old_mask;
9165 const char *operation = NULL;
9169 g_return_if_fail (NM_IS_DEVICE (self));
9170 g_return_if_fail (flags);
9172 priv = NM_DEVICE_GET_PRIVATE (self);
9175 allow_state_transition = FALSE;
9176 was_managed = allow_state_transition && nm_device_get_managed (self, FALSE);
9178 old_flags = priv->unmanaged_flags;
9179 old_mask = priv->unmanaged_mask;
9182 case NM_UNMAN_FLAG_OP_FORGET:
9183 priv->unmanaged_mask &= ~flags;
9184 priv->unmanaged_flags &= ~flags;
9185 operation = "forget";
9187 case NM_UNMAN_FLAG_OP_SET_UNMANAGED:
9188 priv->unmanaged_mask |= flags;
9189 priv->unmanaged_flags |= flags;
9190 operation = "set-unmanaged";
9192 case NM_UNMAN_FLAG_OP_SET_MANAGED:
9193 priv->unmanaged_mask |= flags;
9194 priv->unmanaged_flags &= ~flags;
9195 operation = "set-managed";
9198 g_return_if_reached ();
9201 if ( old_flags == priv->unmanaged_flags
9202 && old_mask == priv->unmanaged_mask)
9205 transition_state = allow_state_transition
9206 && was_managed != nm_device_get_managed (self, FALSE)
9209 && nm_device_get_state (self) == NM_DEVICE_STATE_UNMANAGED));
9211 #define _FMTX "[%s%s0x%0x/0x%x/%s"
9212 #define _FMT(flags, mask, str) \
9213 _unmanaged_flags2str ((flags), (mask), str, sizeof (str)), \
9214 ((flags) | (mask)) ? "=" : "", \
9217 (_get_managed_by_flags (flags, mask, FALSE) \
9219 : (_get_managed_by_flags (flags, mask, TRUE) \
9222 _LOGD (LOGD_DEVICE, "unmanaged: flags set to "_FMTX"%s, %s [%s=0x%0x]%s%s%s)",
9223 _FMT (priv->unmanaged_flags, priv->unmanaged_mask, str1),
9224 priv->real ? "" : "/unrealized",
9226 nm_unmanaged_flags2str (flags, str2, sizeof (str2)),
9228 NM_PRINT_FMT_QUOTED (allow_state_transition,
9230 reason_to_string (reason),
9231 transition_state ? ", transition-state" : "",
9235 if (transition_state) {
9237 nm_device_state_changed (self, NM_DEVICE_STATE_UNMANAGED, reason);
9239 nm_device_state_changed (self, NM_DEVICE_STATE_UNAVAILABLE, reason);
9244 * @self: the #NMDevice instance
9245 * @flags: which #NMUnmanagedFlags to set.
9246 * @set_op: whether to set/clear/forget the flags. You can also pass
9247 * boolean values %TRUE and %FALSE, which mean %NM_UNMAN_FLAG_OP_SET_UNMANAGED
9248 * and %NM_UNMAN_FLAG_OP_SET_MANAGED, respectively.
9250 * Set the unmanaged flags of the device (does not trigger a state change).
9253 nm_device_set_unmanaged_flags (NMDevice *self,
9254 NMUnmanagedFlags flags,
9255 NMUnmanFlagOp set_op)
9257 _set_unmanaged_flags (self, flags, set_op, FALSE, NM_DEVICE_STATE_REASON_NONE);
9261 * nm_device_set_unmanaged_by_flags:
9262 * @self: the #NMDevice instance
9263 * @flags: which #NMUnmanagedFlags to set.
9264 * @set_op: whether to set/clear/forget the flags. You can also pass
9265 * boolean values %TRUE and %FALSE, which mean %NM_UNMAN_FLAG_OP_SET_UNMANAGED
9266 * and %NM_UNMAN_FLAG_OP_SET_MANAGED, respectively.
9267 * @reason: the device state reason passed to nm_device_state_changed() if
9268 * the device becomes managed/unmanaged.
9270 * Set the unmanaged flags of the device and possibly trigger a state change.
9273 nm_device_set_unmanaged_by_flags (NMDevice *self,
9274 NMUnmanagedFlags flags,
9275 NMUnmanFlagOp set_op,
9276 NMDeviceStateReason reason)
9278 _set_unmanaged_flags (self, flags, set_op, TRUE, reason);
9282 nm_device_set_unmanaged_by_user_config (NMDevice *self, const GSList *unmanaged_specs)
9284 NMDevicePrivate *priv;
9287 g_return_if_fail (NM_IS_DEVICE (self));
9289 priv = NM_DEVICE_GET_PRIVATE (self);
9291 unmanaged = nm_device_spec_match_list (self, unmanaged_specs);
9293 nm_device_set_unmanaged_by_flags (self,
9294 NM_UNMANAGED_USER_SETTINGS,
9297 ? NM_DEVICE_STATE_REASON_NOW_UNMANAGED
9298 : NM_DEVICE_STATE_REASON_NOW_MANAGED);
9302 nm_device_set_unmanaged_by_user_udev (NMDevice *self)
9305 gboolean platform_unmanaged = FALSE;
9307 ifindex = self->priv->ifindex;
9310 || !nm_platform_link_get_unmanaged (NM_PLATFORM_GET, ifindex, &platform_unmanaged))
9313 nm_device_set_unmanaged_by_flags (self,
9314 NM_UNMANAGED_USER_UDEV,
9316 NM_DEVICE_STATE_REASON_USER_REQUESTED);
9320 nm_device_set_unmanaged_by_quitting (NMDevice *self)
9322 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
9323 gboolean need_deactivate = nm_device_is_activating (self) ||
9324 priv->state == NM_DEVICE_STATE_ACTIVATED;
9326 /* It's OK to block here because we're quitting */
9327 if (need_deactivate)
9328 _set_state_full (self, NM_DEVICE_STATE_DEACTIVATING, NM_DEVICE_STATE_REASON_NOW_UNMANAGED, TRUE);
9330 nm_device_set_unmanaged_by_flags (self,
9331 NM_UNMANAGED_QUITTING,
9333 need_deactivate ? NM_DEVICE_STATE_REASON_REMOVED
9334 : NM_DEVICE_STATE_REASON_NOW_UNMANAGED);
9337 /*****************************************************************************/
9340 nm_device_set_dhcp_timeout (NMDevice *self, guint32 timeout)
9342 g_return_if_fail (NM_IS_DEVICE (self));
9344 NM_DEVICE_GET_PRIVATE (self)->dhcp_timeout = timeout;
9348 nm_device_set_dhcp_anycast_address (NMDevice *self, const char *addr)
9350 NMDevicePrivate *priv;
9352 g_return_if_fail (NM_IS_DEVICE (self));
9353 g_return_if_fail (!addr || nm_utils_hwaddr_valid (addr, ETH_ALEN));
9355 priv = NM_DEVICE_GET_PRIVATE (self);
9357 g_free (priv->dhcp_anycast_address);
9358 priv->dhcp_anycast_address = g_strdup (addr);
9362 nm_device_reapply_settings_immediately (NMDevice *self)
9364 NMConnection *applied_connection;
9365 NMSettingsConnection *settings_connection;
9366 NMDeviceState state;
9367 NMSettingConnection *s_con_settings;
9368 NMSettingConnection *s_con_applied;
9373 g_return_if_fail (NM_IS_DEVICE (self));
9375 state = nm_device_get_state (self);
9376 if ( state <= NM_DEVICE_STATE_DISCONNECTED
9377 || state > NM_DEVICE_STATE_ACTIVATED)
9380 applied_connection = nm_device_get_applied_connection (self);
9381 settings_connection = nm_device_get_settings_connection (self);
9383 if (!nm_settings_connection_has_unmodified_applied_connection (settings_connection,
9385 NM_SETTING_COMPARE_FLAG_IGNORE_REAPPLY_IMMEDIATELY))
9388 s_con_settings = nm_connection_get_setting_connection ((NMConnection *) settings_connection);
9389 s_con_applied = nm_connection_get_setting_connection (applied_connection);
9391 if (g_strcmp0 ((zone = nm_setting_connection_get_zone (s_con_settings)),
9392 nm_setting_connection_get_zone (s_con_applied)) != 0) {
9394 version_id = nm_active_connection_version_id_bump ((NMActiveConnection *) self->priv->act_request);
9395 _LOGD (LOGD_DEVICE, "reapply setting: zone = %s%s%s (version-id %llu)", NM_PRINT_FMT_QUOTE_STRING (zone), (long long unsigned) version_id);
9397 g_object_set (G_OBJECT (s_con_applied),
9398 NM_SETTING_CONNECTION_ZONE, zone,
9401 nm_device_update_firewall_zone (self);
9404 if ((metered = nm_setting_connection_get_metered (s_con_settings)) != nm_setting_connection_get_metered (s_con_applied)) {
9406 version_id = nm_active_connection_version_id_bump ((NMActiveConnection *) self->priv->act_request);
9407 _LOGD (LOGD_DEVICE, "reapply setting: metered = %d (version-id %llu)", (int) metered, (long long unsigned) version_id);
9409 g_object_set (G_OBJECT (s_con_applied),
9410 NM_SETTING_CONNECTION_METERED, metered,
9413 nm_device_update_metered (self);
9418 nm_device_update_firewall_zone (NMDevice *self)
9420 NMConnection *applied_connection;
9421 NMSettingConnection *s_con;
9423 g_return_if_fail (NM_IS_DEVICE (self));
9425 applied_connection = nm_device_get_applied_connection (self);
9426 if (!applied_connection)
9429 s_con = nm_connection_get_setting_connection (applied_connection);
9430 if ( nm_device_get_state (self) == NM_DEVICE_STATE_ACTIVATED
9431 && !nm_device_uses_assumed_connection (self)) {
9432 nm_firewall_manager_add_or_change_zone (nm_firewall_manager_get (),
9433 nm_device_get_ip_iface (self),
9434 nm_setting_connection_get_zone (s_con),
9435 FALSE, /* change zone */
9442 nm_device_update_metered (NMDevice *self)
9444 #define NM_METERED_INVALID ((NMMetered) -1)
9445 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
9446 NMSettingConnection *setting;
9447 NMMetered conn_value, value = NM_METERED_INVALID;
9448 NMConnection *connection = NULL;
9449 NMDeviceState state;
9451 g_return_if_fail (NM_IS_DEVICE (self));
9453 state = nm_device_get_state (self);
9454 if ( state <= NM_DEVICE_STATE_DISCONNECTED
9455 || state > NM_DEVICE_STATE_ACTIVATED)
9456 value = NM_METERED_UNKNOWN;
9458 if (value == NM_METERED_INVALID) {
9459 connection = nm_device_get_applied_connection (self);
9461 setting = nm_connection_get_setting_connection (connection);
9463 conn_value = nm_setting_connection_get_metered (setting);
9464 if (conn_value != NM_METERED_UNKNOWN)
9470 /* Try to guess a value using the metered flag in IP configuration */
9471 if (value == NM_METERED_INVALID) {
9472 if ( priv->ip4_config
9473 && priv->ip4_state == IP_DONE
9474 && nm_ip4_config_get_metered (priv->ip4_config))
9475 value = NM_METERED_GUESS_YES;
9478 /* Otherwise look at connection type */
9479 if (value == NM_METERED_INVALID) {
9480 if ( nm_connection_is_type (connection, NM_SETTING_GSM_SETTING_NAME)
9481 || nm_connection_is_type (connection, NM_SETTING_CDMA_SETTING_NAME))
9482 value = NM_METERED_GUESS_YES;
9484 value = NM_METERED_GUESS_NO;
9487 if (value != priv->metered) {
9488 _LOGD (LOGD_DEVICE, "set metered value %d", value);
9489 priv->metered = value;
9490 _notify (self, PROP_METERED);
9495 _nm_device_check_connection_available (NMDevice *self,
9496 NMConnection *connection,
9497 NMDeviceCheckConAvailableFlags flags,
9498 const char *specific_object)
9500 NMDeviceState state;
9502 /* an unrealized software device is always available, hardware devices never. */
9503 if (!nm_device_is_real (self)) {
9504 if (nm_device_is_software (self))
9505 return nm_device_check_connection_compatible (self, connection);
9509 state = nm_device_get_state (self);
9510 if (state < NM_DEVICE_STATE_UNMANAGED)
9512 if ( state < NM_DEVICE_STATE_UNAVAILABLE
9513 && ( ( !NM_FLAGS_ANY (flags, NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST)
9514 && !nm_device_get_managed (self, FALSE))
9515 || ( NM_FLAGS_ANY (flags, NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST)
9516 && !nm_device_get_managed (self, TRUE))))
9518 if ( state < NM_DEVICE_STATE_DISCONNECTED
9519 && !nm_device_is_software (self)
9520 && ( ( !NM_FLAGS_ANY (flags, NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST)
9521 && !nm_device_is_available (self, NM_DEVICE_CHECK_DEV_AVAILABLE_NONE))
9522 || ( NM_FLAGS_ANY (flags, NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST)
9523 && !nm_device_is_available (self, NM_DEVICE_CHECK_DEV_AVAILABLE_FOR_USER_REQUEST))))
9526 if (!nm_device_check_connection_compatible (self, connection))
9529 return NM_DEVICE_GET_CLASS (self)->check_connection_available (self, connection, flags, specific_object);
9533 * nm_device_check_connection_available():
9534 * @self: the #NMDevice
9535 * @connection: the #NMConnection to check for availability
9536 * @flags: flags to affect the decision making of whether a connection
9537 * is available. Adding a flag can only make a connection more available,
9539 * @specific_object: a device type dependent argument to further
9540 * filter the result. Passing a non %NULL specific object can only reduce
9541 * the availability of a connection.
9543 * Check if @connection is available to be activated on @self.
9545 * Returns: %TRUE if @connection can be activated on @self
9548 nm_device_check_connection_available (NMDevice *self,
9549 NMConnection *connection,
9550 NMDeviceCheckConAvailableFlags flags,
9551 const char *specific_object)
9555 available = _nm_device_check_connection_available (self, connection, flags, specific_object);
9557 #if NM_MORE_ASSERTS >= 2
9559 /* The meaning of the flags is so that *adding* a flag relaxes a condition, thus making
9560 * the device *more* available. Assert against that requirement by testing all the flags. */
9561 NMDeviceCheckConAvailableFlags i, j, k;
9562 gboolean available_all[NM_DEVICE_CHECK_CON_AVAILABLE_ALL + 1] = { FALSE };
9564 for (i = 0; i <= NM_DEVICE_CHECK_CON_AVAILABLE_ALL; i++)
9565 available_all[i] = _nm_device_check_connection_available (self, connection, i, specific_object);
9567 for (i = 0; i <= NM_DEVICE_CHECK_CON_AVAILABLE_ALL; i++) {
9568 for (j = 1; j <= NM_DEVICE_CHECK_CON_AVAILABLE_ALL; j <<= 1) {
9569 if (NM_FLAGS_HAS (i, j)) {
9571 nm_assert ( available_all[i] == available_all[k]
9572 || available_all[i]);
9583 available_connections_notify (NMDevice *self)
9585 _notify (self, PROP_AVAILABLE_CONNECTIONS);
9589 available_connections_del_all (NMDevice *self)
9591 if (g_hash_table_size (self->priv->available_connections) == 0)
9593 g_hash_table_remove_all (self->priv->available_connections);
9598 available_connections_add (NMDevice *self, NMConnection *connection)
9600 return nm_g_hash_table_add (self->priv->available_connections, g_object_ref (connection));
9604 available_connections_del (NMDevice *self, NMConnection *connection)
9606 return g_hash_table_remove (self->priv->available_connections, connection);
9610 check_connection_available (NMDevice *self,
9611 NMConnection *connection,
9612 NMDeviceCheckConAvailableFlags flags,
9613 const char *specific_object)
9615 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE(self);
9617 /* Connections which require a network connection are not available when
9618 * the device has no carrier, even with ignore-carrer=TRUE.
9621 || !connection_requires_carrier (connection))
9624 if ( NM_FLAGS_HAS (flags, _NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST_WAITING_CARRIER)
9625 && priv->carrier_wait_id != 0) {
9626 /* The device has no carrier though the connection requires it.
9628 * If we are still waiting for carrier, the connection is available
9629 * for an explicit user-request. */
9637 nm_device_recheck_available_connections (NMDevice *self)
9639 NMDevicePrivate *priv;
9640 const GSList *connections, *iter;
9641 gboolean changed = FALSE;
9642 GHashTableIter h_iter;
9643 NMConnection *connection;
9645 g_return_if_fail (NM_IS_DEVICE (self));
9647 priv = NM_DEVICE_GET_PRIVATE(self);
9649 if (priv->con_provider) {
9650 gs_unref_hashtable GHashTable *prune_list = NULL;
9652 if (g_hash_table_size (priv->available_connections) > 0) {
9653 prune_list = g_hash_table_new (g_direct_hash, g_direct_equal);
9654 g_hash_table_iter_init (&h_iter, priv->available_connections);
9655 while (g_hash_table_iter_next (&h_iter, (gpointer *) &connection, NULL))
9656 g_hash_table_add (prune_list, connection);
9659 connections = nm_connection_provider_get_connections (priv->con_provider);
9660 for (iter = connections; iter; iter = g_slist_next (iter)) {
9661 connection = NM_CONNECTION (iter->data);
9663 if (nm_device_check_connection_available (self,
9665 NM_DEVICE_CHECK_CON_AVAILABLE_NONE,
9667 if (available_connections_add (self, connection))
9670 g_hash_table_remove (prune_list, connection);
9675 g_hash_table_iter_init (&h_iter, prune_list);
9676 while (g_hash_table_iter_next (&h_iter, (gpointer *) &connection, NULL)) {
9677 if (available_connections_del (self, connection))
9682 if (available_connections_del_all (self))
9687 available_connections_notify (self);
9688 available_connections_check_delete_unrealized (self);
9692 * nm_device_get_available_connections:
9693 * @self: the #NMDevice
9694 * @specific_object: a specific object path if any
9696 * Returns a list of connections available to activate on the device, taking
9697 * into account any device-specific details given by @specific_object (like
9698 * WiFi access point path).
9700 * Returns: caller-owned #GPtrArray of #NMConnections
9703 nm_device_get_available_connections (NMDevice *self, const char *specific_object)
9705 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
9706 GHashTableIter iter;
9707 guint num_available;
9708 NMConnection *connection = NULL;
9709 GPtrArray *array = NULL;
9711 num_available = g_hash_table_size (priv->available_connections);
9712 if (num_available > 0) {
9713 array = g_ptr_array_sized_new (num_available);
9714 g_hash_table_iter_init (&iter, priv->available_connections);
9715 while (g_hash_table_iter_next (&iter, (gpointer) &connection, NULL)) {
9716 /* If a specific object is given, only include connections that are
9717 * compatible with it.
9719 if ( !specific_object /* << Optimization: we know that the connection is available without @specific_object. */
9720 || nm_device_check_connection_available (self, connection, _NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST, specific_object))
9721 g_ptr_array_add (array, connection);
9728 cp_connection_added_or_updated (NMConnectionProvider *cp, NMConnection *connection, gpointer user_data)
9731 NMDevice *self = user_data;
9733 g_return_if_fail (NM_IS_DEVICE (self));
9734 g_return_if_fail (NM_IS_SETTINGS_CONNECTION (connection));
9736 if (nm_device_check_connection_available (self,
9738 _NM_DEVICE_CHECK_CON_AVAILABLE_FOR_USER_REQUEST,
9740 changed = available_connections_add (self, connection);
9742 changed = available_connections_del (self, connection);
9745 available_connections_notify (self);
9746 available_connections_check_delete_unrealized (self);
9751 cp_connection_removed (NMConnectionProvider *cp, NMConnection *connection, gpointer user_data)
9753 NMDevice *self = user_data;
9755 g_return_if_fail (NM_IS_DEVICE (self));
9757 if (available_connections_del (self, connection)) {
9758 available_connections_notify (self);
9759 available_connections_check_delete_unrealized (self);
9764 nm_device_supports_vlans (NMDevice *self)
9766 return nm_platform_link_supports_vlans (NM_PLATFORM_GET, nm_device_get_ifindex (self));
9770 * nm_device_add_pending_action():
9771 * @self: the #NMDevice to add the pending action to
9772 * @action: a static string that identifies the action
9773 * @assert_not_yet_pending: if %TRUE, assert that the @action is currently not yet pending.
9774 * Otherwise, ignore duplicate scheduling of the same action silently.
9776 * Adds a pending action to the device.
9778 * Returns: %TRUE if the action was added (and not already added before). %FALSE
9779 * if the same action is already scheduled. In the latter case, the action was not scheduled
9783 nm_device_add_pending_action (NMDevice *self, const char *action, gboolean assert_not_yet_pending)
9785 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
9789 g_return_val_if_fail (action, FALSE);
9791 /* Check if the action is already pending. Cannot add duplicate actions */
9792 for (iter = priv->pending_actions; iter; iter = iter->next) {
9793 if (!strcmp (action, iter->data)) {
9794 if (assert_not_yet_pending) {
9795 _LOGW (LOGD_DEVICE, "add_pending_action (%d): '%s' already pending",
9796 count + g_slist_length (iter), action);
9797 g_return_val_if_reached (FALSE);
9799 _LOGD (LOGD_DEVICE, "add_pending_action (%d): '%s' already pending (expected)",
9800 count + g_slist_length (iter), action);
9807 priv->pending_actions = g_slist_append (priv->pending_actions, g_strdup (action));
9810 _LOGD (LOGD_DEVICE, "add_pending_action (%d): '%s'", count, action);
9813 _notify (self, PROP_HAS_PENDING_ACTION);
9819 * nm_device_remove_pending_action():
9820 * @self: the #NMDevice to remove the pending action from
9821 * @action: a static string that identifies the action
9822 * @assert_is_pending: if %TRUE, assert that the @action is pending.
9823 * If %FALSE, don't do anything if the current action is not pending and
9826 * Removes a pending action previously added by nm_device_add_pending_action().
9828 * Returns: whether the @action was pending and is now removed.
9831 nm_device_remove_pending_action (NMDevice *self, const char *action, gboolean assert_is_pending)
9833 NMDevicePrivate *priv;
9834 GSList *iter, *next;
9837 g_return_val_if_fail (self, FALSE);
9838 g_return_val_if_fail (action, FALSE);
9840 priv = NM_DEVICE_GET_PRIVATE (self);
9842 for (iter = priv->pending_actions; iter; iter = next) {
9844 if (!strcmp (action, iter->data)) {
9845 _LOGD (LOGD_DEVICE, "remove_pending_action (%d): '%s'",
9846 count + g_slist_length (iter->next), /* length excluding 'iter' */
9848 g_free (iter->data);
9849 priv->pending_actions = g_slist_delete_link (priv->pending_actions, iter);
9850 if (priv->pending_actions == NULL)
9851 _notify (self, PROP_HAS_PENDING_ACTION);
9857 if (assert_is_pending) {
9858 _LOGW (LOGD_DEVICE, "remove_pending_action (%d): '%s' not pending", count, action);
9859 g_return_val_if_reached (FALSE);
9861 _LOGD (LOGD_DEVICE, "remove_pending_action (%d): '%s' not pending (expected)", count, action);
9867 nm_device_has_pending_action (NMDevice *self)
9869 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
9871 return !!priv->pending_actions;
9874 /***********************************************************/
9877 _cancel_activation (NMDevice *self)
9879 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
9881 /* Clean up when device was deactivated during call to firewall */
9882 if (priv->fw_call) {
9883 nm_firewall_manager_cancel_call (priv->fw_call);
9884 g_warn_if_fail (!priv->fw_call);
9885 priv->fw_call = NULL;
9887 priv->fw_ready = FALSE;
9889 ip_check_gw_ping_cleanup (self);
9891 /* Break the activation chain */
9892 activation_source_clear (self, AF_INET);
9893 activation_source_clear (self, AF_INET6);
9897 _cleanup_generic_pre (NMDevice *self, CleanupType cleanup_type)
9899 NMConnection *connection;
9901 _cancel_activation (self);
9903 connection = nm_device_get_applied_connection (self);
9904 if ( cleanup_type == CLEANUP_TYPE_DECONFIGURE
9906 && !nm_device_uses_assumed_connection (self)) {
9907 nm_firewall_manager_remove_from_zone (nm_firewall_manager_get (),
9908 nm_device_get_ip_iface (self),
9914 /* Clear any queued transitions */
9915 nm_device_queued_state_clear (self);
9917 _cleanup_ip4_pre (self, cleanup_type);
9918 _cleanup_ip6_pre (self, cleanup_type);
9922 _cleanup_generic_post (NMDevice *self, CleanupType cleanup_type)
9924 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
9925 NMDeviceStateReason ignored = NM_DEVICE_STATE_REASON_NONE;
9927 priv->default_route.v4_has = FALSE;
9928 priv->default_route.v6_has = FALSE;
9930 if (cleanup_type == CLEANUP_TYPE_DECONFIGURE) {
9931 priv->default_route.v4_is_assumed = FALSE;
9932 priv->default_route.v6_is_assumed = FALSE;
9933 nm_default_route_manager_ip4_update_default_route (nm_default_route_manager_get (), self);
9934 nm_default_route_manager_ip6_update_default_route (nm_default_route_manager_get (), self);
9937 priv->default_route.v4_is_assumed = TRUE;
9938 priv->default_route.v6_is_assumed = TRUE;
9939 nm_default_route_manager_ip4_update_default_route (nm_default_route_manager_get (), self);
9940 nm_default_route_manager_ip6_update_default_route (nm_default_route_manager_get (), self);
9942 priv->v4_commit_first_time = TRUE;
9943 priv->v6_commit_first_time = TRUE;
9945 priv->linklocal6_dad_counter = 0;
9947 /* Clean up IP configs; this does not actually deconfigure the
9948 * interface; the caller must flush routes and addresses explicitly.
9950 nm_device_set_ip4_config (self, NULL, 0, TRUE, TRUE, &ignored);
9951 nm_device_set_ip6_config (self, NULL, TRUE, TRUE, &ignored);
9952 g_clear_object (&priv->con_ip4_config);
9953 g_clear_object (&priv->dev_ip4_config);
9954 g_clear_object (&priv->ext_ip4_config);
9955 g_clear_object (&priv->wwan_ip4_config);
9956 g_clear_object (&priv->ip4_config);
9957 g_clear_object (&priv->con_ip6_config);
9958 g_clear_object (&priv->ac_ip6_config);
9959 g_clear_object (&priv->ext_ip6_config);
9960 g_clear_object (&priv->ext_ip6_config_captured);
9961 g_clear_object (&priv->wwan_ip6_config);
9962 g_clear_object (&priv->ip6_config);
9964 g_slist_free_full (priv->vpn4_configs, g_object_unref);
9965 priv->vpn4_configs = NULL;
9966 g_slist_free_full (priv->vpn6_configs, g_object_unref);
9967 priv->vpn6_configs = NULL;
9969 clear_act_request (self);
9971 /* Clear legacy IPv4 address property */
9972 if (priv->ip4_address) {
9973 priv->ip4_address = 0;
9974 _notify (self, PROP_IP4_ADDRESS);
9977 if (cleanup_type == CLEANUP_TYPE_DECONFIGURE) {
9978 /* Check if the device was deactivated, and if so, delete_link.
9979 * Don't call delete_link synchronously because we are currently
9980 * handling a state change -- which is not reentrant. */
9981 delete_on_deactivate_check_and_schedule (self, nm_device_get_ip_ifindex (self));
9984 /* ip_iface should be cleared after flushing all routes and addreses, since
9985 * those are identified by ip_iface, not by iface (which might be a tty
9988 nm_device_set_ip_iface (self, NULL);
9994 * Remove a device's routing table entries and IP addresses.
9998 nm_device_cleanup (NMDevice *self, NMDeviceStateReason reason, CleanupType cleanup_type)
10000 NMDevicePrivate *priv;
10003 g_return_if_fail (NM_IS_DEVICE (self));
10005 if (reason == NM_DEVICE_STATE_REASON_NOW_MANAGED)
10006 _LOGD (LOGD_DEVICE, "preparing device");
10008 _LOGD (LOGD_DEVICE, "deactivating device (reason '%s') [%d]", reason_to_string (reason), reason);
10010 /* Save whether or not we tried IPv6 for later */
10011 priv = NM_DEVICE_GET_PRIVATE (self);
10013 _cleanup_generic_pre (self, cleanup_type);
10015 /* Turn off kernel IPv6 */
10016 if (cleanup_type == CLEANUP_TYPE_DECONFIGURE) {
10017 set_disable_ipv6 (self, "1");
10018 nm_device_ipv6_sysctl_set (self, "accept_ra", "0");
10019 nm_device_ipv6_sysctl_set (self, "use_tempaddr", "0");
10022 /* Call device type-specific deactivation */
10023 if (NM_DEVICE_GET_CLASS (self)->deactivate)
10024 NM_DEVICE_GET_CLASS (self)->deactivate (self);
10026 /* master: release slaves */
10027 nm_device_master_release_slaves (self);
10029 /* slave: mark no longer enslaved */
10031 && nm_platform_link_get_master (NM_PLATFORM_GET, priv->ifindex) <= 0)
10032 nm_device_master_release_one_slave (priv->master, self, FALSE, NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
10034 /* Take out any entries in the routing table and any IP address the device had. */
10035 ifindex = nm_device_get_ip_ifindex (self);
10037 nm_route_manager_route_flush (nm_route_manager_get (), ifindex);
10038 nm_platform_address_flush (NM_PLATFORM_GET, ifindex);
10041 if (priv->lldp_listener)
10042 nm_lldp_listener_stop (priv->lldp_listener);
10044 nm_device_update_metered (self);
10045 _cleanup_generic_post (self, cleanup_type);
10049 bin2hexstr (const char *bytes, gsize len)
10054 g_return_val_if_fail (bytes != NULL, NULL);
10055 g_return_val_if_fail (len > 0, NULL);
10057 str = g_string_sized_new (len * 2 + 1);
10058 for (i = 0; i < len; i++) {
10060 g_string_append_c (str, ':');
10061 g_string_append_printf (str, "%02x", (guint8) bytes[i]);
10063 return g_string_free (str, FALSE);
10067 find_dhcp4_address (NMDevice *self)
10069 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10072 if (!priv->ip4_config)
10075 n = nm_ip4_config_get_num_addresses (priv->ip4_config);
10076 for (i = 0; i < n; i++) {
10077 const NMPlatformIP4Address *a = nm_ip4_config_get_address (priv->ip4_config, i);
10079 if (a->source == NM_IP_CONFIG_SOURCE_DHCP)
10080 return g_strdup (nm_utils_inet4_ntop (a->address, NULL));
10086 nm_device_spawn_iface_helper (NMDevice *self)
10088 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10089 gboolean configured = FALSE;
10090 NMConnection *connection;
10091 GError *error = NULL;
10092 const char *method;
10094 gs_free char *dhcp4_address = NULL;
10095 char *logging_backend;
10097 if (priv->state != NM_DEVICE_STATE_ACTIVATED)
10099 if (!nm_device_can_assume_connections (self))
10102 connection = nm_device_get_applied_connection (self);
10103 g_assert (connection);
10105 argv = g_ptr_array_sized_new (10);
10106 g_ptr_array_set_free_func (argv, g_free);
10108 g_ptr_array_add (argv, g_strdup (LIBEXECDIR "/nm-iface-helper"));
10109 g_ptr_array_add (argv, g_strdup ("--ifname"));
10110 g_ptr_array_add (argv, g_strdup (nm_device_get_ip_iface (self)));
10111 g_ptr_array_add (argv, g_strdup ("--uuid"));
10112 g_ptr_array_add (argv, g_strdup (nm_connection_get_uuid (connection)));
10114 logging_backend = nm_config_get_is_debug (nm_config_get ())
10115 ? g_strdup ("debug")
10116 : nm_config_data_get_value (NM_CONFIG_GET_DATA_ORIG,
10117 NM_CONFIG_KEYFILE_GROUP_LOGGING,
10118 NM_CONFIG_KEYFILE_KEY_LOGGING_BACKEND,
10119 NM_CONFIG_GET_VALUE_STRIP | NM_CONFIG_GET_VALUE_NO_EMPTY);
10120 if (logging_backend) {
10121 g_ptr_array_add (argv, g_strdup ("--logging-backend"));
10122 g_ptr_array_add (argv, logging_backend);
10125 dhcp4_address = find_dhcp4_address (self);
10127 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG);
10128 if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0) {
10129 NMSettingIPConfig *s_ip4;
10130 char *hex_client_id;
10132 s_ip4 = nm_connection_get_setting_ip4_config (connection);
10135 g_ptr_array_add (argv, g_strdup ("--priority4"));
10136 g_ptr_array_add (argv, g_strdup_printf ("%u", nm_device_get_ip4_route_metric (self)));
10138 g_ptr_array_add (argv, g_strdup ("--dhcp4"));
10139 g_ptr_array_add (argv, g_strdup (dhcp4_address));
10140 if (nm_setting_ip_config_get_may_fail (s_ip4) == FALSE)
10141 g_ptr_array_add (argv, g_strdup ("--dhcp4-required"));
10143 if (priv->dhcp4_client) {
10144 const char *hostname, *fqdn;
10147 client_id = nm_dhcp_client_get_client_id (priv->dhcp4_client);
10149 g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid"));
10150 hex_client_id = bin2hexstr (g_bytes_get_data (client_id, NULL),
10151 g_bytes_get_size (client_id));
10152 g_ptr_array_add (argv, hex_client_id);
10155 hostname = nm_dhcp_client_get_hostname (priv->dhcp4_client);
10157 g_ptr_array_add (argv, g_strdup ("--dhcp4-hostname"));
10158 g_ptr_array_add (argv, g_strdup (hostname));
10161 fqdn = nm_dhcp_client_get_fqdn (priv->dhcp4_client);
10163 g_ptr_array_add (argv, g_strdup ("--dhcp4-fqdn"));
10164 g_ptr_array_add (argv, g_strdup (fqdn));
10171 method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG);
10172 if (g_strcmp0 (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0) {
10173 NMSettingIPConfig *s_ip6;
10175 NMUtilsIPv6IfaceId iid = NM_UTILS_IPV6_IFACE_ID_INIT;
10177 s_ip6 = nm_connection_get_setting_ip6_config (connection);
10180 g_ptr_array_add (argv, g_strdup ("--priority6"));
10181 g_ptr_array_add (argv, g_strdup_printf ("%u", nm_device_get_ip6_route_metric (self)));
10183 g_ptr_array_add (argv, g_strdup ("--slaac"));
10185 if (nm_setting_ip_config_get_may_fail (s_ip6) == FALSE)
10186 g_ptr_array_add (argv, g_strdup ("--slaac-required"));
10188 g_ptr_array_add (argv, g_strdup ("--slaac-tempaddr"));
10189 g_ptr_array_add (argv, g_strdup_printf ("%d", priv->rdisc_use_tempaddr));
10191 if (nm_device_get_ip_iface_identifier (self, &iid)) {
10192 g_ptr_array_add (argv, g_strdup ("--iid"));
10193 hex_iid = bin2hexstr ((const char *) iid.id_u8, sizeof (NMUtilsIPv6IfaceId));
10194 g_ptr_array_add (argv, hex_iid);
10197 g_ptr_array_add (argv, g_strdup ("--addr-gen-mode"));
10198 g_ptr_array_add (argv, g_strdup_printf ("%d", nm_setting_ip6_config_get_addr_gen_mode (NM_SETTING_IP6_CONFIG (s_ip6))));
10206 g_ptr_array_add (argv, NULL);
10208 if (nm_logging_enabled (LOGL_DEBUG, LOGD_DEVICE)) {
10211 tmp = g_strjoinv (" ", (char **) argv->pdata);
10212 _LOGD (LOGD_DEVICE, "running '%s'", tmp);
10216 if (g_spawn_async (NULL, (char **) argv->pdata, NULL,
10217 G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error)) {
10218 _LOGI (LOGD_DEVICE, "spawned helper PID %u", (guint) pid);
10220 _LOGW (LOGD_DEVICE, "failed to spawn helper: %s", error->message);
10221 g_error_free (error);
10225 g_ptr_array_unref (argv);
10228 /***********************************************************/
10231 ip_config_valid (NMDeviceState state)
10233 return (state == NM_DEVICE_STATE_UNMANAGED) ||
10234 (state >= NM_DEVICE_STATE_IP_CHECK &&
10235 state <= NM_DEVICE_STATE_DEACTIVATING);
10239 notify_ip_properties (NMDevice *self)
10241 _notify (self, PROP_IP_IFACE);
10242 _notify (self, PROP_IP4_CONFIG);
10243 _notify (self, PROP_DHCP4_CONFIG);
10244 _notify (self, PROP_IP6_CONFIG);
10245 _notify (self, PROP_DHCP6_CONFIG);
10249 ip6_managed_setup (NMDevice *self)
10251 set_nm_ipv6ll (self, TRUE);
10252 set_disable_ipv6 (self, "1");
10253 nm_device_ipv6_sysctl_set (self, "accept_ra_defrtr", "0");
10254 nm_device_ipv6_sysctl_set (self, "accept_ra_pinfo", "0");
10255 nm_device_ipv6_sysctl_set (self, "accept_ra_rtr_pref", "0");
10256 nm_device_ipv6_sysctl_set (self, "use_tempaddr", "0");
10260 deactivate_async_ready (NMDevice *self,
10262 gpointer user_data)
10264 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10265 NMDeviceStateReason reason = GPOINTER_TO_UINT (user_data);
10266 GError *error = NULL;
10268 NM_DEVICE_GET_CLASS (self)->deactivate_async_finish (self, res, &error);
10270 /* If operation cancelled, just return */
10271 if ( g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)
10272 || (priv->deactivating_cancellable && g_cancellable_is_cancelled (priv->deactivating_cancellable))) {
10273 _LOGW (LOGD_DEVICE, "Deactivation cancelled");
10275 /* In every other case, transition to the DISCONNECTED state */
10278 _LOGW (LOGD_DEVICE, "Deactivation failed: %s",
10281 nm_device_queue_state (self, NM_DEVICE_STATE_DISCONNECTED, reason);
10284 g_clear_object (&priv->deactivating_cancellable);
10285 g_clear_error (&error);
10289 deactivate_dispatcher_complete (guint call_id, gpointer user_data)
10291 NMDevice *self = NM_DEVICE (user_data);
10292 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10293 NMDeviceStateReason reason;
10295 g_return_if_fail (call_id == priv->dispatcher.call_id);
10296 g_return_if_fail (priv->dispatcher.post_state == NM_DEVICE_STATE_DISCONNECTED);
10298 reason = priv->dispatcher.post_state_reason;
10300 priv->dispatcher.call_id = 0;
10301 priv->dispatcher.post_state = NM_DEVICE_STATE_UNKNOWN;
10302 priv->dispatcher.post_state_reason = NM_DEVICE_STATE_REASON_NONE;
10304 if (priv->deactivating_cancellable) {
10305 g_warn_if_reached ();
10306 g_cancellable_cancel (priv->deactivating_cancellable);
10307 g_clear_object (&priv->deactivating_cancellable);
10310 if ( NM_DEVICE_GET_CLASS (self)->deactivate_async
10311 && NM_DEVICE_GET_CLASS (self)->deactivate_async_finish) {
10312 priv->deactivating_cancellable = g_cancellable_new ();
10313 NM_DEVICE_GET_CLASS (self)->deactivate_async (self,
10314 priv->deactivating_cancellable,
10315 (GAsyncReadyCallback) deactivate_async_ready,
10316 GUINT_TO_POINTER (reason));
10318 nm_device_queue_state (self, NM_DEVICE_STATE_DISCONNECTED, reason);
10322 _set_state_full (NMDevice *self,
10323 NMDeviceState state,
10324 NMDeviceStateReason reason,
10327 NMDevicePrivate *priv;
10328 NMDeviceState old_state;
10330 gboolean no_firmware = FALSE;
10331 NMSettingsConnection *connection;
10332 NMConnection *applied_connection;
10334 g_return_if_fail (NM_IS_DEVICE (self));
10336 priv = NM_DEVICE_GET_PRIVATE (self);
10338 /* Track re-entry */
10339 g_warn_if_fail (priv->in_state_changed == FALSE);
10341 old_state = priv->state;
10343 /* Do nothing if state isn't changing, but as a special case allow
10344 * re-setting UNAVAILABLE if the device is missing firmware so that we
10345 * can retry device initialization.
10347 if ( (priv->state == state)
10348 && ( state != NM_DEVICE_STATE_UNAVAILABLE
10349 || !priv->firmware_missing)) {
10350 _LOGD (LOGD_DEVICE, "state change: %s -> %s (reason '%s') [%d %d %d]%s",
10351 state_to_string (old_state),
10352 state_to_string (state),
10353 reason_to_string (reason),
10357 priv->firmware_missing ? " (missing firmware)" : "");
10361 _LOGI (LOGD_DEVICE, "state change: %s -> %s (reason '%s') [%d %d %d]",
10362 state_to_string (old_state),
10363 state_to_string (state),
10364 reason_to_string (reason),
10369 priv->in_state_changed = TRUE;
10371 priv->state = state;
10372 priv->state_reason = reason;
10374 /* Clear any queued transitions */
10375 nm_device_queued_state_clear (self);
10377 dispatcher_cleanup (self);
10378 if (priv->deactivating_cancellable)
10379 g_cancellable_cancel (priv->deactivating_cancellable);
10381 /* Cache the activation request for the dispatcher */
10382 req = priv->act_request ? g_object_ref (priv->act_request) : NULL;
10384 if (state <= NM_DEVICE_STATE_UNAVAILABLE) {
10385 if (available_connections_del_all (self))
10386 available_connections_notify (self);
10387 _clear_queued_act_request (priv);
10390 /* Update the available connections list when a device first becomes available */
10391 if (state >= NM_DEVICE_STATE_DISCONNECTED && old_state < NM_DEVICE_STATE_DISCONNECTED)
10392 nm_device_recheck_available_connections (self);
10394 /* Handle the new state here; but anything that could trigger
10395 * another state change should be done below.
10398 case NM_DEVICE_STATE_UNMANAGED:
10399 nm_device_set_firmware_missing (self, FALSE);
10400 if (old_state > NM_DEVICE_STATE_UNMANAGED) {
10401 if (reason == NM_DEVICE_STATE_REASON_REMOVED) {
10402 nm_device_cleanup (self, reason, CLEANUP_TYPE_REMOVED);
10404 /* Clean up if the device is now unmanaged but was activated */
10405 if (nm_device_get_act_request (self))
10406 nm_device_cleanup (self, reason, CLEANUP_TYPE_DECONFIGURE);
10407 nm_device_take_down (self, TRUE);
10408 set_nm_ipv6ll (self, FALSE);
10409 restore_ip6_properties (self);
10413 case NM_DEVICE_STATE_UNAVAILABLE:
10414 if (old_state == NM_DEVICE_STATE_UNMANAGED) {
10415 save_ip6_properties (self);
10416 if (reason != NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED)
10417 ip6_managed_setup (self);
10420 if (reason != NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED) {
10421 if (old_state == NM_DEVICE_STATE_UNMANAGED || priv->firmware_missing) {
10422 if (!nm_device_bring_up (self, TRUE, &no_firmware) && no_firmware)
10423 _LOGW (LOGD_HW, "firmware may be missing.");
10424 nm_device_set_firmware_missing (self, no_firmware ? TRUE : FALSE);
10427 /* Ensure the device gets deactivated in response to stuff like
10428 * carrier changes or rfkill. But don't deactivate devices that are
10429 * about to assume a connection since that defeats the purpose of
10430 * assuming the device's existing connection.
10432 * Note that we "deactivate" the device even when coming from
10433 * UNMANAGED, to ensure that it's in a clean state.
10435 nm_device_cleanup (self, reason, CLEANUP_TYPE_DECONFIGURE);
10438 case NM_DEVICE_STATE_DISCONNECTED:
10439 if (old_state > NM_DEVICE_STATE_DISCONNECTED) {
10440 /* Ensure devices that previously assumed a connection now have
10441 * userspace IPv6LL enabled.
10443 set_nm_ipv6ll (self, TRUE);
10445 nm_device_cleanup (self, reason, CLEANUP_TYPE_DECONFIGURE);
10446 } else if (old_state < NM_DEVICE_STATE_DISCONNECTED) {
10447 if (reason != NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED) {
10448 /* Ensure IPv6 is set up as it may not have been done when
10449 * entering the UNAVAILABLE state depending on the reason.
10451 ip6_managed_setup (self);
10455 case NM_DEVICE_STATE_NEED_AUTH:
10456 if (old_state > NM_DEVICE_STATE_NEED_AUTH) {
10457 /* Clean up any half-done IP operations if the device's layer2
10458 * finds out it needs authentication during IP config.
10460 _cleanup_ip4_pre (self, CLEANUP_TYPE_DECONFIGURE);
10461 _cleanup_ip6_pre (self, CLEANUP_TYPE_DECONFIGURE);
10468 /* Reset autoconnect flag when the device is activating or connected. */
10469 if ( state >= NM_DEVICE_STATE_PREPARE
10470 && state <= NM_DEVICE_STATE_ACTIVATED)
10471 nm_device_set_autoconnect (self, TRUE);
10473 _notify (self, PROP_STATE);
10474 _notify (self, PROP_STATE_REASON);
10475 g_signal_emit_by_name (self, NM_DEVICE_STATE_CHANGED, state, old_state, reason);
10477 /* Post-process the event after internal notification */
10480 case NM_DEVICE_STATE_UNAVAILABLE:
10481 /* If the device can activate now (ie, it's got a carrier, the supplicant
10482 * is active, or whatever) schedule a delayed transition to DISCONNECTED
10483 * to get things rolling. The device can't transition immediately because
10484 * we can't change states again from the state handler for a variety of
10487 if (nm_device_is_available (self, NM_DEVICE_CHECK_DEV_AVAILABLE_NONE)) {
10488 nm_device_queue_recheck_available (self,
10489 NM_DEVICE_STATE_REASON_NONE,
10490 NM_DEVICE_STATE_REASON_NONE);
10492 _LOGD (LOGD_DEVICE, "device not yet available for transition to DISCONNECTED");
10495 case NM_DEVICE_STATE_DEACTIVATING:
10496 _cancel_activation (self);
10498 if (nm_device_has_capability (self, NM_DEVICE_CAP_CARRIER_DETECT)) {
10499 /* We cache the ignore_carrier state to not react on config-reloads while the connection
10500 * is active. But on deactivating, reset the ignore-carrier flag to the current state. */
10501 priv->ignore_carrier = nm_config_data_get_ignore_carrier (NM_CONFIG_GET_DATA, self);
10505 nm_dispatcher_call_sync (DISPATCHER_ACTION_PRE_DOWN,
10506 nm_act_request_get_settings_connection (req),
10507 nm_act_request_get_applied_connection (req),
10510 priv->dispatcher.post_state = NM_DEVICE_STATE_DISCONNECTED;
10511 priv->dispatcher.post_state_reason = reason;
10512 if (!nm_dispatcher_call (DISPATCHER_ACTION_PRE_DOWN,
10513 nm_act_request_get_settings_connection (req),
10514 nm_act_request_get_applied_connection (req),
10516 deactivate_dispatcher_complete,
10518 &priv->dispatcher.call_id)) {
10519 /* Just proceed on errors */
10520 deactivate_dispatcher_complete (0, self);
10524 case NM_DEVICE_STATE_DISCONNECTED:
10525 if ( priv->queued_act_request
10526 && !priv->queued_act_request_is_waiting_for_carrier) {
10527 NMActRequest *queued_req;
10530 queued_req = priv->queued_act_request;
10531 priv->queued_act_request = NULL;
10532 success = _device_activate (self, queued_req);
10533 g_object_unref (queued_req);
10539 case NM_DEVICE_STATE_ACTIVATED:
10540 _LOGI (LOGD_DEVICE, "Activation: successful, device activated.");
10541 nm_device_update_metered (self);
10542 nm_dispatcher_call (DISPATCHER_ACTION_UP,
10543 nm_act_request_get_settings_connection (req),
10544 nm_act_request_get_applied_connection (req),
10545 self, NULL, NULL, NULL);
10547 case NM_DEVICE_STATE_FAILED:
10548 /* Usually upon failure the activation chain is interrupted in
10549 * one of the stages; but in some cases the device fails for
10550 * external events (as a failure of master connection) while
10551 * the activation sequence is running and so we need to ensure
10552 * that the chain is terminated here.
10554 _cancel_activation (self);
10556 if (nm_device_uses_assumed_connection (self)) {
10557 /* Avoid tearing down assumed connection, assume it's connected */
10558 nm_device_queue_state (self,
10559 NM_DEVICE_STATE_ACTIVATED,
10560 NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED);
10564 connection = nm_device_get_settings_connection (self);
10565 _LOGW (LOGD_DEVICE | LOGD_WIFI,
10566 "Activation: failed for connection '%s'",
10567 connection ? nm_settings_connection_get_id (connection) : "<unknown>");
10569 /* Notify any slaves of the unexpected failure */
10570 nm_device_master_release_slaves (self);
10572 /* If the connection doesn't yet have a timestamp, set it to zero so that
10573 * we can distinguish between connections we've tried to activate and have
10574 * failed (zero timestamp), connections that succeeded (non-zero timestamp),
10575 * and those we haven't tried yet (no timestamp).
10577 if (connection && !nm_settings_connection_get_timestamp (connection, NULL))
10578 nm_settings_connection_update_timestamp (connection, (guint64) 0, TRUE);
10580 /* Schedule the transition to DISCONNECTED. The device can't transition
10581 * immediately because we can't change states again from the state
10582 * handler for a variety of reasons.
10584 nm_device_queue_state (self, NM_DEVICE_STATE_DISCONNECTED, NM_DEVICE_STATE_REASON_NONE);
10586 case NM_DEVICE_STATE_IP_CHECK:
10587 /* Now that IP config has completed, check if the firewall
10588 * zone must be set again for the IP interface.
10590 applied_connection = nm_device_get_applied_connection (self);
10592 if ( applied_connection
10593 && priv->ifindex != priv->ip_ifindex
10594 && !nm_device_uses_assumed_connection (self)) {
10595 NMSettingConnection *s_con;
10598 s_con = nm_connection_get_setting_connection (applied_connection);
10599 zone = nm_setting_connection_get_zone (s_con);
10600 g_assert (!priv->fw_call);
10601 priv->fw_call = nm_firewall_manager_add_or_change_zone (nm_firewall_manager_get (),
10602 nm_device_get_ip_iface (self),
10605 fw_change_zone_cb_ip_check,
10608 nm_device_start_ip_check (self);
10610 /* IP-related properties are only valid when the device has IP configuration;
10611 * now that it does, ensure their change notifications are emitted.
10613 notify_ip_properties (self);
10615 case NM_DEVICE_STATE_SECONDARIES:
10616 ip_check_gw_ping_cleanup (self);
10617 _LOGD (LOGD_DEVICE, "device entered SECONDARIES state");
10623 if (state > NM_DEVICE_STATE_DISCONNECTED)
10624 delete_on_deactivate_unschedule (self);
10626 if ( (old_state == NM_DEVICE_STATE_ACTIVATED || old_state == NM_DEVICE_STATE_DEACTIVATING)
10627 && (state != NM_DEVICE_STATE_DEACTIVATING)) {
10629 nm_dispatcher_call_sync (DISPATCHER_ACTION_DOWN,
10630 nm_act_request_get_settings_connection (req),
10631 nm_act_request_get_applied_connection (req),
10634 nm_dispatcher_call (DISPATCHER_ACTION_DOWN,
10635 nm_act_request_get_settings_connection (req),
10636 nm_act_request_get_applied_connection (req),
10637 self, NULL, NULL, NULL);
10641 /* IP-related properties are only valid when the device has IP configuration.
10642 * If it no longer does, ensure their change notifications are emitted.
10644 if (ip_config_valid (old_state) && !ip_config_valid (state))
10645 notify_ip_properties (self);
10647 /* Dispose of the cached activation request */
10649 g_object_unref (req);
10651 priv->in_state_changed = FALSE;
10653 if ((old_state > NM_DEVICE_STATE_UNMANAGED) != (state > NM_DEVICE_STATE_UNMANAGED))
10654 _notify (self, PROP_MANAGED);
10658 nm_device_state_changed (NMDevice *self,
10659 NMDeviceState state,
10660 NMDeviceStateReason reason)
10662 _set_state_full (self, state, reason, FALSE);
10666 queued_set_state (gpointer user_data)
10668 NMDevice *self = NM_DEVICE (user_data);
10669 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10670 NMDeviceState new_state;
10671 NMDeviceStateReason new_reason;
10673 if (priv->queued_state.id) {
10674 _LOGD (LOGD_DEVICE, "running queued state change to %s (id %d)",
10675 state_to_string (priv->queued_state.state),
10676 priv->queued_state.id);
10678 /* Clear queued state struct before triggering state change, since
10679 * the state change may queue another state.
10681 priv->queued_state.id = 0;
10682 new_state = priv->queued_state.state;
10683 new_reason = priv->queued_state.reason;
10684 nm_device_queued_state_clear (self);
10686 nm_device_state_changed (self, new_state, new_reason);
10687 nm_device_remove_pending_action (self, queued_state_to_string (new_state), TRUE);
10689 g_warn_if_fail (priv->queued_state.state == NM_DEVICE_STATE_UNKNOWN);
10690 g_warn_if_fail (priv->queued_state.reason == NM_DEVICE_STATE_REASON_NONE);
10696 nm_device_queue_state (NMDevice *self,
10697 NMDeviceState state,
10698 NMDeviceStateReason reason)
10700 NMDevicePrivate *priv;
10702 g_return_if_fail (NM_IS_DEVICE (self));
10704 priv = NM_DEVICE_GET_PRIVATE (self);
10706 if (priv->queued_state.id && priv->queued_state.state == state)
10709 /* Add pending action for the new state before clearing the queued states, so
10710 * that we don't accidently pop all pending states and reach 'startup complete' */
10711 nm_device_add_pending_action (self, queued_state_to_string (state), TRUE);
10713 /* We should only ever have one delayed state transition at a time */
10714 if (priv->queued_state.id) {
10715 _LOGW (LOGD_DEVICE, "overwriting previously queued state change to %s (%s)",
10716 state_to_string (priv->queued_state.state),
10717 reason_to_string (priv->queued_state.reason));
10718 nm_device_queued_state_clear (self);
10721 priv->queued_state.state = state;
10722 priv->queued_state.reason = reason;
10723 priv->queued_state.id = g_idle_add (queued_set_state, self);
10725 _LOGD (LOGD_DEVICE, "queued state change to %s due to %s (id %d)",
10726 state_to_string (state), reason_to_string (reason),
10727 priv->queued_state.id);
10731 nm_device_queued_state_peek (NMDevice *self)
10733 NMDevicePrivate *priv;
10735 g_return_val_if_fail (NM_IS_DEVICE (self), NM_DEVICE_STATE_UNKNOWN);
10737 priv = NM_DEVICE_GET_PRIVATE (self);
10739 return priv->queued_state.id ? priv->queued_state.state : NM_DEVICE_STATE_UNKNOWN;
10743 nm_device_queued_state_clear (NMDevice *self)
10745 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10747 if (priv->queued_state.id) {
10748 _LOGD (LOGD_DEVICE, "clearing queued state transition (id %d)",
10749 priv->queued_state.id);
10750 nm_clear_g_source (&priv->queued_state.id);
10751 nm_device_remove_pending_action (self, queued_state_to_string (priv->queued_state.state), TRUE);
10753 memset (&priv->queued_state, 0, sizeof (priv->queued_state));
10757 nm_device_get_state (NMDevice *self)
10759 g_return_val_if_fail (NM_IS_DEVICE (self), NM_DEVICE_STATE_UNKNOWN);
10761 return NM_DEVICE_GET_PRIVATE (self)->state;
10764 /***********************************************************/
10765 /* NMConfigDevice interface related stuff */
10768 nm_device_get_hw_address (NMDevice *self)
10770 NMDevicePrivate *priv;
10772 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
10773 priv = NM_DEVICE_GET_PRIVATE (self);
10775 return priv->hw_addr_len ? priv->hw_addr : NULL;
10779 nm_device_update_hw_address (NMDevice *self)
10781 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10782 int ifindex = nm_device_get_ifindex (self);
10783 const guint8 *hwaddr;
10784 gsize hwaddrlen = 0;
10785 static const guint8 zero_hwaddr[ETH_ALEN];
10790 hwaddr = nm_platform_link_get_address (NM_PLATFORM_GET, ifindex, &hwaddrlen);
10792 if ( priv->type == NM_DEVICE_TYPE_ETHERNET
10793 && nm_utils_hwaddr_matches (hwaddr, hwaddrlen, zero_hwaddr, sizeof (zero_hwaddr)))
10797 priv->hw_addr_len = hwaddrlen;
10798 if (!priv->hw_addr || !nm_utils_hwaddr_matches (priv->hw_addr, -1, hwaddr, hwaddrlen)) {
10799 g_free (priv->hw_addr);
10800 priv->hw_addr = nm_utils_hwaddr_ntoa (hwaddr, hwaddrlen);
10802 _LOGD (LOGD_HW | LOGD_DEVICE, "hardware address now %s", priv->hw_addr);
10803 _notify (self, PROP_HW_ADDRESS);
10806 /* Invalid or no hardware address */
10807 if (priv->hw_addr_len != 0) {
10808 g_clear_pointer (&priv->hw_addr, g_free);
10809 priv->hw_addr_len = 0;
10810 _LOGD (LOGD_HW | LOGD_DEVICE,
10811 "previous hardware address is no longer valid");
10812 _notify (self, PROP_HW_ADDRESS);
10818 nm_device_update_initial_hw_address (NMDevice *self)
10820 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10822 if (priv->hw_addr_len) {
10823 priv->initial_hw_addr = g_strdup (priv->hw_addr);
10824 _LOGD (LOGD_DEVICE | LOGD_HW, "read initial MAC address %s", priv->initial_hw_addr);
10826 if (priv->ifindex > 0) {
10827 guint8 buf[NM_UTILS_HWADDR_LEN_MAX];
10830 if (nm_platform_link_get_permanent_address (NM_PLATFORM_GET, priv->ifindex, buf, &len)) {
10831 g_warn_if_fail (len == priv->hw_addr_len);
10832 priv->perm_hw_addr = nm_utils_hwaddr_ntoa (buf, priv->hw_addr_len);
10833 _LOGD (LOGD_DEVICE | LOGD_HW, "read permanent MAC address %s",
10834 priv->perm_hw_addr);
10836 /* Fall back to current address */
10837 _LOGD (LOGD_HW | LOGD_ETHER, "unable to read permanent MAC address");
10838 priv->perm_hw_addr = g_strdup (priv->hw_addr);
10845 nm_device_set_hw_addr (NMDevice *self, const char *addr,
10846 const char *detail, guint64 hw_log_domain)
10848 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10849 gboolean success = FALSE;
10850 const char *cur_addr = nm_device_get_hw_address (self);
10851 guint8 addr_bytes[NM_UTILS_HWADDR_LEN_MAX];
10853 /* Fall back to the permanent address */
10855 addr = priv->perm_hw_addr;
10859 /* Do nothing if current MAC is same */
10860 if (cur_addr && nm_utils_hwaddr_matches (cur_addr, -1, addr, -1)) {
10861 _LOGD (LOGD_DEVICE | hw_log_domain, "no MAC address change needed");
10864 if (!nm_utils_hwaddr_aton (addr, addr_bytes, priv->hw_addr_len)) {
10865 _LOGW (LOGD_DEVICE | hw_log_domain, "invalid MAC address %s", addr);
10869 /* Can't change MAC address while device is up */
10870 nm_device_take_down (self, FALSE);
10872 success = nm_platform_link_set_address (NM_PLATFORM_GET, nm_device_get_ip_ifindex (self), addr_bytes, priv->hw_addr_len);
10874 /* MAC address succesfully changed; update the current MAC to match */
10875 nm_device_update_hw_address (self);
10876 cur_addr = nm_device_get_hw_address (self);
10877 if (cur_addr && nm_utils_hwaddr_matches (cur_addr, -1, addr, -1)) {
10878 _LOGI (LOGD_DEVICE | hw_log_domain, "%s MAC address to %s",
10881 _LOGW (LOGD_DEVICE | hw_log_domain,
10882 "new MAC address %s not successfully set", addr);
10886 _LOGW (LOGD_DEVICE | hw_log_domain, "failed to %s MAC address to %s",
10889 nm_device_bring_up (self, TRUE, NULL);
10895 nm_device_get_permanent_hw_address (NMDevice *self)
10897 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
10899 return NM_DEVICE_GET_PRIVATE (self)->perm_hw_addr;
10903 nm_device_get_initial_hw_address (NMDevice *self)
10905 g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
10907 return NM_DEVICE_GET_PRIVATE (self)->initial_hw_addr;
10911 * nm_device_spec_match_list:
10912 * @self: an #NMDevice
10913 * @specs: (element-type utf8): a list of device specs
10915 * Checks if @self matches any of the specifications in @specs. The
10916 * currently-supported spec types are:
10918 * "mac:00:11:22:33:44:55" - matches a device with the given
10921 * "interface-name:foo0" - matches a device with the given
10924 * "s390-subchannels:00.11.22" - matches a device with the given
10925 * z/VM / s390 subchannels.
10927 * "*" - matches any device
10929 * Returns: #TRUE if @self matches one of the specs in @specs
10932 nm_device_spec_match_list (NMDevice *self, const GSList *specs)
10934 g_return_val_if_fail (NM_IS_DEVICE (self), FALSE);
10939 return NM_DEVICE_GET_CLASS (self)->spec_match_list (self, specs) == NM_MATCH_SPEC_MATCH;
10942 static NMMatchSpecMatchType
10943 spec_match_list (NMDevice *self, const GSList *specs)
10945 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
10946 NMMatchSpecMatchType matched = NM_MATCH_SPEC_NO_MATCH, m;
10947 const GSList *iter;
10949 for (iter = specs; iter; iter = g_slist_next (iter)) {
10950 if (!strcmp ((const char *) iter->data, "*")) {
10951 matched = NM_MATCH_SPEC_MATCH;
10955 if (priv->hw_addr_len && priv->hw_addr) {
10956 m = nm_match_spec_hwaddr (specs, priv->hw_addr);
10957 matched = MAX (matched, m);
10959 if (matched != NM_MATCH_SPEC_NEG_MATCH) {
10960 m = nm_match_spec_interface_name (specs, nm_device_get_iface (self));
10961 matched = MAX (matched, m);
10963 if (matched != NM_MATCH_SPEC_NEG_MATCH) {
10964 m = nm_match_spec_device_type (specs, nm_device_get_type_description (self));
10965 matched = MAX (matched, m);
10970 /***********************************************************/
10972 static const char *
10973 _activation_func_to_string (ActivationHandleFunc func)
10975 #define FUNC_TO_STRING_CHECK_AND_RETURN(func, f) \
10977 if ((func) == (f)) \
10980 FUNC_TO_STRING_CHECK_AND_RETURN (func, activate_stage1_device_prepare);
10981 FUNC_TO_STRING_CHECK_AND_RETURN (func, activate_stage2_device_config);
10982 FUNC_TO_STRING_CHECK_AND_RETURN (func, activate_stage3_ip_config_start);
10983 FUNC_TO_STRING_CHECK_AND_RETURN (func, activate_stage4_ip4_config_timeout);
10984 FUNC_TO_STRING_CHECK_AND_RETURN (func, activate_stage4_ip6_config_timeout);
10985 FUNC_TO_STRING_CHECK_AND_RETURN (func, activate_stage5_ip4_config_commit);
10986 FUNC_TO_STRING_CHECK_AND_RETURN (func, activate_stage5_ip6_config_commit);
10987 g_return_val_if_reached ("unknown");
10990 /***********************************************************/
10993 nm_device_init (NMDevice *self)
10995 NMDevicePrivate *priv;
10997 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, NM_TYPE_DEVICE, NMDevicePrivate);
11001 priv->type = NM_DEVICE_TYPE_UNKNOWN;
11002 priv->capabilities = NM_DEVICE_CAP_NM_SUPPORTED;
11003 priv->state = NM_DEVICE_STATE_UNMANAGED;
11004 priv->state_reason = NM_DEVICE_STATE_REASON_NONE;
11005 priv->dhcp_timeout = 0;
11006 priv->rfkill_type = RFKILL_TYPE_UNKNOWN;
11007 priv->autoconnect = DEFAULT_AUTOCONNECT;
11008 priv->unmanaged_flags = NM_UNMANAGED_PLATFORM_INIT;
11009 priv->unmanaged_mask = priv->unmanaged_flags;
11010 priv->available_connections = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, NULL);
11011 priv->ip6_saved_properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
11013 priv->default_route.v4_is_assumed = TRUE;
11014 priv->default_route.v6_is_assumed = TRUE;
11016 priv->v4_commit_first_time = TRUE;
11017 priv->v6_commit_first_time = TRUE;
11021 constructor (GType type,
11022 guint n_construct_params,
11023 GObjectConstructParam *construct_params)
11026 GObjectClass *klass;
11028 NMDevicePrivate *priv;
11029 const NMPlatformLink *pllink;
11031 klass = G_OBJECT_CLASS (nm_device_parent_class);
11032 object = klass->constructor (type, n_construct_params, construct_params);
11036 self = NM_DEVICE (object);
11037 priv = NM_DEVICE_GET_PRIVATE (self);
11040 pllink = nm_platform_link_get_by_ifname (NM_PLATFORM_GET, priv->iface);
11042 if (pllink && link_type_compatible (self, pllink->type, NULL, NULL)) {
11043 priv->ifindex = pllink->ifindex;
11044 priv->up = NM_FLAGS_HAS (pllink->n_ifi_flags, IFF_UP);
11052 constructed (GObject *object)
11054 NMDevice *self = NM_DEVICE (object);
11055 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
11056 NMPlatform *platform;
11058 platform = nm_platform_get ();
11060 if (NM_DEVICE_GET_CLASS (self)->get_generic_capabilities)
11061 priv->capabilities |= NM_DEVICE_GET_CLASS (self)->get_generic_capabilities (self);
11063 /* Watch for external IP config changes */
11064 g_signal_connect (platform, NM_PLATFORM_SIGNAL_IP4_ADDRESS_CHANGED, G_CALLBACK (device_ipx_changed), self);
11065 g_signal_connect (platform, NM_PLATFORM_SIGNAL_IP6_ADDRESS_CHANGED, G_CALLBACK (device_ipx_changed), self);
11066 g_signal_connect (platform, NM_PLATFORM_SIGNAL_IP4_ROUTE_CHANGED, G_CALLBACK (device_ipx_changed), self);
11067 g_signal_connect (platform, NM_PLATFORM_SIGNAL_IP6_ROUTE_CHANGED, G_CALLBACK (device_ipx_changed), self);
11068 g_signal_connect (platform, NM_PLATFORM_SIGNAL_LINK_CHANGED, G_CALLBACK (link_changed_cb), self);
11070 priv->con_provider = nm_connection_provider_get ();
11071 g_assert (priv->con_provider);
11072 g_signal_connect (priv->con_provider,
11073 NM_CP_SIGNAL_CONNECTION_ADDED,
11074 G_CALLBACK (cp_connection_added_or_updated),
11077 g_signal_connect (priv->con_provider,
11078 NM_CP_SIGNAL_CONNECTION_REMOVED,
11079 G_CALLBACK (cp_connection_removed),
11082 g_signal_connect (priv->con_provider,
11083 NM_CP_SIGNAL_CONNECTION_UPDATED,
11084 G_CALLBACK (cp_connection_added_or_updated),
11087 G_OBJECT_CLASS (nm_device_parent_class)->constructed (object);
11089 _LOGD (LOGD_DEVICE, "constructed (%s)", G_OBJECT_TYPE_NAME (self));
11093 dispose (GObject *object)
11095 NMDevice *self = NM_DEVICE (object);
11096 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
11097 NMPlatform *platform;
11099 _LOGD (LOGD_DEVICE, "disposing");
11101 g_slist_free_full (priv->arping.dad_list, (GDestroyNotify) nm_arping_manager_destroy);
11102 priv->arping.dad_list = NULL;
11104 arp_cleanup (self);
11106 g_signal_handlers_disconnect_by_func (nm_config_get (), config_changed_update_ignore_carrier, self);
11108 dispatcher_cleanup (self);
11110 _cleanup_generic_pre (self, CLEANUP_TYPE_KEEP);
11112 g_warn_if_fail (priv->slaves == NULL);
11113 g_assert (priv->master_ready_id == 0);
11115 /* Let the kernel manage IPv6LL again */
11116 set_nm_ipv6ll (self, FALSE);
11118 _cleanup_generic_post (self, CLEANUP_TYPE_KEEP);
11120 g_hash_table_remove_all (priv->ip6_saved_properties);
11122 nm_clear_g_source (&priv->recheck_assume_id);
11123 nm_clear_g_source (&priv->recheck_available.call_id);
11125 nm_clear_g_source (&priv->check_delete_unrealized_id);
11127 link_disconnect_action_cancel (self);
11129 if (priv->con_provider) {
11130 g_signal_handlers_disconnect_by_func (priv->con_provider, cp_connection_added_or_updated, self);
11131 g_signal_handlers_disconnect_by_func (priv->con_provider, cp_connection_removed, self);
11132 priv->con_provider = NULL;
11135 available_connections_del_all (self);
11137 nm_clear_g_source (&priv->carrier_wait_id);
11139 _clear_queued_act_request (priv);
11141 platform = nm_platform_get ();
11142 g_signal_handlers_disconnect_by_func (platform, G_CALLBACK (device_ipx_changed), self);
11143 g_signal_handlers_disconnect_by_func (platform, G_CALLBACK (link_changed_cb), self);
11145 nm_clear_g_source (&priv->device_link_changed_id);
11146 nm_clear_g_source (&priv->device_ip_link_changed_id);
11148 if (priv->lldp_listener) {
11149 g_signal_handlers_disconnect_by_func (priv->lldp_listener,
11150 G_CALLBACK (lldp_neighbors_changed),
11152 nm_lldp_listener_stop (priv->lldp_listener);
11153 g_clear_object (&priv->lldp_listener);
11156 G_OBJECT_CLASS (nm_device_parent_class)->dispose (object);
11158 if (nm_clear_g_source (&priv->queued_state.id)) {
11159 /* FIXME: we'd expect the queud_state to be alredy cleared and this statement
11160 * not being necessary. Add this check here to hopefully investigate crash
11162 g_return_if_reached ();
11167 finalize (GObject *object)
11169 NMDevice *self = NM_DEVICE (object);
11170 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
11172 _LOGD (LOGD_DEVICE, "finalize(): %s", G_OBJECT_TYPE_NAME (self));
11174 g_free (priv->hw_addr);
11175 g_free (priv->perm_hw_addr);
11176 g_free (priv->initial_hw_addr);
11177 g_slist_free_full (priv->pending_actions, g_free);
11178 g_slist_free_full (priv->dad6_failed_addrs, g_free);
11179 g_clear_pointer (&priv->physical_port_id, g_free);
11180 g_free (priv->udi);
11181 g_free (priv->iface);
11182 g_free (priv->ip_iface);
11183 g_free (priv->driver);
11184 g_free (priv->driver_version);
11185 g_free (priv->firmware_version);
11186 g_free (priv->type_desc);
11187 g_free (priv->type_description);
11188 g_free (priv->dhcp_anycast_address);
11190 g_hash_table_unref (priv->ip6_saved_properties);
11191 g_hash_table_unref (priv->available_connections);
11193 G_OBJECT_CLASS (nm_device_parent_class)->finalize (object);
11197 set_property (GObject *object, guint prop_id,
11198 const GValue *value, GParamSpec *pspec)
11200 NMDevice *self = NM_DEVICE (object);
11201 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
11202 const char *hw_addr, *p;
11207 if (g_value_get_string (value)) {
11208 g_free (priv->udi);
11209 priv->udi = g_value_dup_string (value);
11213 /* construct only */
11214 g_return_if_fail (!priv->iface);
11215 priv->iface = g_value_dup_string (value);
11218 if (g_value_get_string (value)) {
11219 g_free (priv->driver);
11220 priv->driver = g_value_dup_string (value);
11223 case PROP_DRIVER_VERSION:
11224 g_free (priv->driver_version);
11225 priv->driver_version = g_strdup (g_value_get_string (value));
11227 case PROP_FIRMWARE_VERSION:
11228 g_free (priv->firmware_version);
11229 priv->firmware_version = g_strdup (g_value_get_string (value));
11232 priv->mtu = g_value_get_uint (value);
11234 case PROP_IP4_ADDRESS:
11235 priv->ip4_address = g_value_get_uint (value);
11237 case PROP_MANAGED: {
11239 NMDeviceStateReason reason;
11241 managed = g_value_get_boolean (value);
11243 reason = NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED;
11245 reason = NM_DEVICE_STATE_REASON_REMOVED;
11246 nm_device_set_unmanaged_by_flags (self,
11247 NM_UNMANAGED_USER_EXPLICIT,
11252 case PROP_AUTOCONNECT:
11253 nm_device_set_autoconnect (self, g_value_get_boolean (value));
11255 case PROP_FIRMWARE_MISSING:
11256 /* construct only */
11257 priv->firmware_missing = g_value_get_boolean (value);
11259 case PROP_NM_PLUGIN_MISSING:
11260 priv->nm_plugin_missing = g_value_get_boolean (value);
11262 case PROP_DEVICE_TYPE:
11263 g_return_if_fail (priv->type == NM_DEVICE_TYPE_UNKNOWN);
11264 priv->type = g_value_get_uint (value);
11266 case PROP_LINK_TYPE:
11267 /* construct only */
11268 g_return_if_fail (priv->link_type == NM_LINK_TYPE_NONE);
11269 priv->link_type = g_value_get_uint (value);
11271 case PROP_TYPE_DESC:
11272 g_free (priv->type_desc);
11273 priv->type_desc = g_value_dup_string (value);
11275 case PROP_RFKILL_TYPE:
11276 priv->rfkill_type = g_value_get_uint (value);
11278 case PROP_IS_MASTER:
11279 priv->is_master = g_value_get_boolean (value);
11281 case PROP_HW_ADDRESS:
11282 /* construct only */
11283 p = hw_addr = g_value_get_string (value);
11285 /* Hardware address length is the number of ':' plus 1 */
11291 if (count < ETH_ALEN || count > NM_UTILS_HWADDR_LEN_MAX) {
11292 if (hw_addr && *hw_addr) {
11293 _LOGW (LOGD_DEVICE, "ignoring hardware address '%s' with unexpected length %d",
11299 priv->hw_addr_len = count;
11300 g_free (priv->hw_addr);
11301 if (nm_utils_hwaddr_valid (hw_addr, priv->hw_addr_len))
11302 priv->hw_addr = g_strdup (hw_addr);
11304 _LOGW (LOGD_DEVICE, "could not parse hw-address '%s'", hw_addr);
11305 priv->hw_addr = NULL;
11309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
11315 get_property (GObject *object, guint prop_id,
11316 GValue *value, GParamSpec *pspec)
11318 NMDevice *self = NM_DEVICE (object);
11319 NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
11321 GHashTableIter iter;
11322 NMConnection *connection;
11323 GVariantBuilder array_builder;
11327 g_value_set_string (value, priv->udi);
11330 g_value_set_string (value, priv->iface);
11332 case PROP_IP_IFACE:
11333 if (ip_config_valid (priv->state))
11334 g_value_set_string (value, nm_device_get_ip_iface (self));
11336 g_value_set_string (value, NULL);
11339 g_value_set_int (value, priv->ifindex);
11342 g_value_set_string (value, priv->driver);
11344 case PROP_DRIVER_VERSION:
11345 g_value_set_string (value, priv->driver_version);
11347 case PROP_FIRMWARE_VERSION:
11348 g_value_set_string (value, priv->firmware_version);
11350 case PROP_CAPABILITIES:
11351 g_value_set_uint (value, (priv->capabilities & ~NM_DEVICE_CAP_INTERNAL_MASK));
11353 case PROP_IP4_ADDRESS:
11354 g_value_set_uint (value, priv->ip4_address);
11357 g_value_set_boolean (value, priv->carrier);
11360 g_value_set_uint (value, priv->mtu);
11362 case PROP_IP4_CONFIG:
11363 nm_utils_g_value_set_object_path (value, ip_config_valid (priv->state) ? priv->ip4_config : NULL);
11365 case PROP_DHCP4_CONFIG:
11366 nm_utils_g_value_set_object_path (value, ip_config_valid (priv->state) ? priv->dhcp4_config : NULL);
11368 case PROP_IP6_CONFIG:
11369 nm_utils_g_value_set_object_path (value, ip_config_valid (priv->state) ? priv->ip6_config : NULL);
11371 case PROP_DHCP6_CONFIG:
11372 nm_utils_g_value_set_object_path (value, ip_config_valid (priv->state) ? priv->dhcp6_config : NULL);
11375 g_value_set_uint (value, priv->state);
11377 case PROP_STATE_REASON:
11378 g_value_take_variant (value,
11379 g_variant_new ("(uu)", priv->state, priv->state_reason));
11381 case PROP_ACTIVE_CONNECTION:
11382 nm_utils_g_value_set_object_path (value, priv->act_request);
11384 case PROP_DEVICE_TYPE:
11385 g_value_set_uint (value, priv->type);
11387 case PROP_LINK_TYPE:
11388 g_value_set_uint (value, priv->link_type);
11391 /* The managed state exposed on D-Bus only depends on the current device state alone. */
11392 g_value_set_boolean (value, nm_device_get_state (self) > NM_DEVICE_STATE_UNMANAGED);
11394 case PROP_AUTOCONNECT:
11395 g_value_set_boolean (value, priv->autoconnect);
11397 case PROP_FIRMWARE_MISSING:
11398 g_value_set_boolean (value, priv->firmware_missing);
11400 case PROP_NM_PLUGIN_MISSING:
11401 g_value_set_boolean (value, priv->nm_plugin_missing);
11403 case PROP_TYPE_DESC:
11404 g_value_set_string (value, priv->type_desc);
11406 case PROP_RFKILL_TYPE:
11407 g_value_set_uint (value, priv->rfkill_type);
11409 case PROP_AVAILABLE_CONNECTIONS:
11410 array = g_ptr_array_sized_new (g_hash_table_size (priv->available_connections));
11411 g_hash_table_iter_init (&iter, priv->available_connections);
11412 while (g_hash_table_iter_next (&iter, (gpointer) &connection, NULL))
11413 g_ptr_array_add (array, g_strdup (nm_connection_get_path (connection)));
11414 g_ptr_array_add (array, NULL);
11415 g_value_take_boxed (value, (char **) g_ptr_array_free (array, FALSE));
11417 case PROP_PHYSICAL_PORT_ID:
11418 g_value_set_string (value, priv->physical_port_id);
11420 case PROP_IS_MASTER:
11421 g_value_set_boolean (value, priv->is_master);
11424 g_value_set_object (value, nm_device_get_master (self));
11426 case PROP_HW_ADDRESS:
11427 g_value_set_string (value, priv->hw_addr);
11429 case PROP_HAS_PENDING_ACTION:
11430 g_value_set_boolean (value, nm_device_has_pending_action (self));
11433 g_value_set_uint (value, priv->metered);
11435 case PROP_LLDP_NEIGHBORS:
11436 if (priv->lldp_listener)
11437 g_value_set_variant (value, nm_lldp_listener_get_neighbors (priv->lldp_listener));
11439 g_variant_builder_init (&array_builder, G_VARIANT_TYPE ("aa{sv}"));
11440 g_value_take_variant (value, g_variant_builder_end (&array_builder));
11444 g_value_set_boolean (value, nm_device_is_real (self));
11446 case PROP_SLAVES: {
11447 GSList *slave_iter;
11451 slave_list = g_new (char *, g_slist_length (priv->slaves) + 1);
11452 for (slave_iter = priv->slaves, i = 0; slave_iter; slave_iter = slave_iter->next) {
11453 SlaveInfo *info = slave_iter->data;
11456 if (!NM_DEVICE_GET_PRIVATE (info->slave)->is_enslaved)
11458 path = nm_exported_object_get_path ((NMExportedObject *) info->slave);
11460 slave_list[i++] = g_strdup (path);
11462 slave_list[i] = NULL;
11463 g_value_take_boxed (value, slave_list);
11467 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
11473 nm_device_class_init (NMDeviceClass *klass)
11475 GObjectClass *object_class = G_OBJECT_CLASS (klass);
11476 NMExportedObjectClass *exported_object_class = NM_EXPORTED_OBJECT_CLASS (klass);
11478 g_type_class_add_private (object_class, sizeof (NMDevicePrivate));
11480 exported_object_class->export_path = NM_DBUS_PATH "/Devices/%u";
11482 /* Virtual methods */
11483 object_class->dispose = dispose;
11484 object_class->finalize = finalize;
11485 object_class->set_property = set_property;
11486 object_class->get_property = get_property;
11487 object_class->constructor = constructor;
11488 object_class->constructed = constructed;
11490 klass->link_changed = link_changed;
11492 klass->is_available = is_available;
11493 klass->act_stage1_prepare = act_stage1_prepare;
11494 klass->act_stage2_config = act_stage2_config;
11495 klass->act_stage3_ip4_config_start = act_stage3_ip4_config_start;
11496 klass->act_stage3_ip6_config_start = act_stage3_ip6_config_start;
11497 klass->act_stage4_ip4_config_timeout = act_stage4_ip4_config_timeout;
11498 klass->act_stage4_ip6_config_timeout = act_stage4_ip6_config_timeout;
11499 klass->have_any_ready_slaves = have_any_ready_slaves;
11501 klass->get_type_description = get_type_description;
11502 klass->spec_match_list = spec_match_list;
11503 klass->can_auto_connect = can_auto_connect;
11504 klass->check_connection_compatible = check_connection_compatible;
11505 klass->check_connection_available = check_connection_available;
11506 klass->can_unmanaged_external_down = can_unmanaged_external_down;
11507 klass->realize_start_notify = realize_start_notify;
11508 klass->unrealize_notify = unrealize_notify;
11509 klass->is_up = is_up;
11510 klass->bring_up = bring_up;
11511 klass->take_down = take_down;
11512 klass->carrier_changed = carrier_changed;
11513 klass->get_ip_iface_identifier = get_ip_iface_identifier;
11516 obj_properties[PROP_UDI] =
11517 g_param_spec_string (NM_DEVICE_UDI, "", "",
11519 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
11520 G_PARAM_STATIC_STRINGS);
11521 obj_properties[PROP_IFACE] =
11522 g_param_spec_string (NM_DEVICE_IFACE, "", "",
11524 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11525 G_PARAM_STATIC_STRINGS);
11526 obj_properties[PROP_IP_IFACE] =
11527 g_param_spec_string (NM_DEVICE_IP_IFACE, "", "",
11530 G_PARAM_STATIC_STRINGS);
11531 obj_properties[PROP_DRIVER] =
11532 g_param_spec_string (NM_DEVICE_DRIVER, "", "",
11534 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11535 G_PARAM_STATIC_STRINGS);
11536 obj_properties[PROP_DRIVER_VERSION] =
11537 g_param_spec_string (NM_DEVICE_DRIVER_VERSION, "", "",
11539 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11540 G_PARAM_STATIC_STRINGS);
11541 obj_properties[PROP_FIRMWARE_VERSION] =
11542 g_param_spec_string (NM_DEVICE_FIRMWARE_VERSION, "", "",
11544 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11545 G_PARAM_STATIC_STRINGS);
11546 obj_properties[PROP_CAPABILITIES] =
11547 g_param_spec_uint (NM_DEVICE_CAPABILITIES, "", "",
11548 0, G_MAXUINT32, NM_DEVICE_CAP_NONE,
11550 G_PARAM_STATIC_STRINGS);
11551 obj_properties[PROP_CARRIER] =
11552 g_param_spec_boolean (NM_DEVICE_CARRIER, "", "",
11555 G_PARAM_STATIC_STRINGS);
11556 obj_properties[PROP_MTU] =
11557 g_param_spec_uint (NM_DEVICE_MTU, "", "",
11558 0, G_MAXUINT32, 1500,
11560 G_PARAM_STATIC_STRINGS);
11561 obj_properties[PROP_IP4_ADDRESS] =
11562 g_param_spec_uint (NM_DEVICE_IP4_ADDRESS, "", "",
11563 0, G_MAXUINT32, 0, /* FIXME */
11564 G_PARAM_READWRITE |
11565 G_PARAM_STATIC_STRINGS);
11566 obj_properties[PROP_IP4_CONFIG] =
11567 g_param_spec_string (NM_DEVICE_IP4_CONFIG, "", "",
11569 G_PARAM_READWRITE |
11570 G_PARAM_STATIC_STRINGS);
11571 obj_properties[PROP_DHCP4_CONFIG] =
11572 g_param_spec_string (NM_DEVICE_DHCP4_CONFIG, "", "",
11574 G_PARAM_READWRITE |
11575 G_PARAM_STATIC_STRINGS);
11576 obj_properties[PROP_IP6_CONFIG] =
11577 g_param_spec_string (NM_DEVICE_IP6_CONFIG, "", "",
11579 G_PARAM_READWRITE |
11580 G_PARAM_STATIC_STRINGS);
11581 obj_properties[PROP_DHCP6_CONFIG] =
11582 g_param_spec_string (NM_DEVICE_DHCP6_CONFIG, "", "",
11584 G_PARAM_READWRITE |
11585 G_PARAM_STATIC_STRINGS);
11586 obj_properties[PROP_STATE] =
11587 g_param_spec_uint (NM_DEVICE_STATE, "", "",
11588 0, G_MAXUINT32, NM_DEVICE_STATE_UNKNOWN,
11590 G_PARAM_STATIC_STRINGS);
11591 obj_properties[PROP_STATE_REASON] =
11592 g_param_spec_variant (NM_DEVICE_STATE_REASON, "", "",
11593 G_VARIANT_TYPE ("(uu)"),
11596 G_PARAM_STATIC_STRINGS);
11597 obj_properties[PROP_ACTIVE_CONNECTION] =
11598 g_param_spec_string (NM_DEVICE_ACTIVE_CONNECTION, "", "",
11601 G_PARAM_STATIC_STRINGS);
11602 obj_properties[PROP_DEVICE_TYPE] =
11603 g_param_spec_uint (NM_DEVICE_DEVICE_TYPE, "", "",
11604 0, G_MAXUINT32, NM_DEVICE_TYPE_UNKNOWN,
11605 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11606 G_PARAM_STATIC_STRINGS);
11607 obj_properties[PROP_LINK_TYPE] =
11608 g_param_spec_uint (NM_DEVICE_LINK_TYPE, "", "",
11609 0, G_MAXUINT32, NM_LINK_TYPE_NONE,
11610 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11611 G_PARAM_STATIC_STRINGS);
11612 obj_properties[PROP_MANAGED] =
11613 g_param_spec_boolean (NM_DEVICE_MANAGED, "", "",
11615 G_PARAM_READWRITE |
11616 G_PARAM_STATIC_STRINGS);
11617 obj_properties[PROP_AUTOCONNECT] =
11618 g_param_spec_boolean (NM_DEVICE_AUTOCONNECT, "", "",
11619 DEFAULT_AUTOCONNECT,
11620 G_PARAM_READWRITE |
11621 G_PARAM_STATIC_STRINGS);
11622 obj_properties[PROP_FIRMWARE_MISSING] =
11623 g_param_spec_boolean (NM_DEVICE_FIRMWARE_MISSING, "", "",
11625 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11626 G_PARAM_STATIC_STRINGS);
11627 obj_properties[PROP_NM_PLUGIN_MISSING] =
11628 g_param_spec_boolean (NM_DEVICE_NM_PLUGIN_MISSING, "", "",
11630 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11631 G_PARAM_STATIC_STRINGS);
11632 obj_properties[PROP_TYPE_DESC] =
11633 g_param_spec_string (NM_DEVICE_TYPE_DESC, "", "",
11635 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11636 G_PARAM_STATIC_STRINGS);
11637 obj_properties[PROP_RFKILL_TYPE] =
11638 g_param_spec_uint (NM_DEVICE_RFKILL_TYPE, "", "",
11641 RFKILL_TYPE_UNKNOWN,
11642 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11643 G_PARAM_STATIC_STRINGS);
11644 obj_properties[PROP_IFINDEX] =
11645 g_param_spec_int (NM_DEVICE_IFINDEX, "", "",
11648 G_PARAM_STATIC_STRINGS);
11649 obj_properties[PROP_AVAILABLE_CONNECTIONS] =
11650 g_param_spec_boxed (NM_DEVICE_AVAILABLE_CONNECTIONS, "", "",
11653 G_PARAM_STATIC_STRINGS);
11654 obj_properties[PROP_PHYSICAL_PORT_ID] =
11655 g_param_spec_string (NM_DEVICE_PHYSICAL_PORT_ID, "", "",
11658 G_PARAM_STATIC_STRINGS);
11659 obj_properties[PROP_IS_MASTER] =
11660 g_param_spec_boolean (NM_DEVICE_IS_MASTER, "", "",
11662 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11663 G_PARAM_STATIC_STRINGS);
11664 obj_properties[PROP_MASTER] =
11665 g_param_spec_object (NM_DEVICE_MASTER, "", "",
11668 G_PARAM_STATIC_STRINGS);
11669 obj_properties[PROP_HW_ADDRESS] =
11670 g_param_spec_string (NM_DEVICE_HW_ADDRESS, "", "",
11672 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
11673 G_PARAM_STATIC_STRINGS);
11674 obj_properties[PROP_HAS_PENDING_ACTION] =
11675 g_param_spec_boolean (NM_DEVICE_HAS_PENDING_ACTION, "", "",
11678 G_PARAM_STATIC_STRINGS);
11681 * NMDevice:metered:
11683 * Whether the connection is metered.
11687 obj_properties[PROP_METERED] =
11688 g_param_spec_uint (NM_DEVICE_METERED, "", "",
11689 0, G_MAXUINT32, NM_METERED_UNKNOWN,
11691 G_PARAM_STATIC_STRINGS);
11692 obj_properties[PROP_LLDP_NEIGHBORS] =
11693 g_param_spec_variant (NM_DEVICE_LLDP_NEIGHBORS, "", "",
11694 G_VARIANT_TYPE ("aa{sv}"),
11697 G_PARAM_STATIC_STRINGS);
11698 obj_properties[PROP_REAL] =
11699 g_param_spec_boolean (NM_DEVICE_REAL, "", "",
11702 G_PARAM_STATIC_STRINGS);
11703 obj_properties[PROP_SLAVES] =
11704 g_param_spec_boxed (NM_DEVICE_SLAVES, "", "",
11707 G_PARAM_STATIC_STRINGS);
11709 g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);
11712 signals[STATE_CHANGED] =
11713 g_signal_new (NM_DEVICE_STATE_CHANGED,
11714 G_OBJECT_CLASS_TYPE (object_class),
11716 G_STRUCT_OFFSET (NMDeviceClass, state_changed),
11719 G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
11721 signals[AUTOCONNECT_ALLOWED] =
11722 g_signal_new ("autoconnect-allowed",
11723 G_OBJECT_CLASS_TYPE (object_class),
11726 autoconnect_allowed_accumulator, NULL, NULL,
11727 G_TYPE_BOOLEAN, 0);
11729 signals[AUTH_REQUEST] =
11730 g_signal_new (NM_DEVICE_AUTH_REQUEST,
11731 G_OBJECT_CLASS_TYPE (object_class),
11732 G_SIGNAL_RUN_FIRST,
11733 0, NULL, NULL, NULL,
11734 /* context, connection, permission, allow_interaction, callback, user_data */
11735 G_TYPE_NONE, 6, G_TYPE_DBUS_METHOD_INVOCATION, NM_TYPE_CONNECTION, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER, G_TYPE_POINTER);
11737 signals[IP4_CONFIG_CHANGED] =
11738 g_signal_new (NM_DEVICE_IP4_CONFIG_CHANGED,
11739 G_OBJECT_CLASS_TYPE (object_class),
11740 G_SIGNAL_RUN_FIRST,
11741 0, NULL, NULL, NULL,
11742 G_TYPE_NONE, 2, G_TYPE_OBJECT, G_TYPE_OBJECT);
11744 signals[IP6_CONFIG_CHANGED] =
11745 g_signal_new (NM_DEVICE_IP6_CONFIG_CHANGED,
11746 G_OBJECT_CLASS_TYPE (object_class),
11747 G_SIGNAL_RUN_FIRST,
11748 0, NULL, NULL, NULL,
11749 G_TYPE_NONE, 2, G_TYPE_OBJECT, G_TYPE_OBJECT);
11752 g_signal_new (NM_DEVICE_REMOVED,
11753 G_OBJECT_CLASS_TYPE (object_class),
11754 G_SIGNAL_RUN_FIRST,
11755 0, NULL, NULL, NULL,
11758 signals[RECHECK_AUTO_ACTIVATE] =
11759 g_signal_new (NM_DEVICE_RECHECK_AUTO_ACTIVATE,
11760 G_OBJECT_CLASS_TYPE (object_class),
11761 G_SIGNAL_RUN_FIRST,
11762 0, NULL, NULL, NULL,
11765 signals[RECHECK_ASSUME] =
11766 g_signal_new (NM_DEVICE_RECHECK_ASSUME,
11767 G_OBJECT_CLASS_TYPE (object_class),
11768 G_SIGNAL_RUN_FIRST,
11769 0, NULL, NULL, NULL,
11772 nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (klass),
11773 NMDBUS_TYPE_DEVICE_SKELETON,
11774 "Reapply", impl_device_reapply,
11775 "GetAppliedConnection", impl_device_get_applied_connection,
11776 "Disconnect", impl_device_disconnect,
11777 "Delete", impl_device_delete,