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

7
#include "src/core/nm-default-daemon.h"
8

9
10
#include "nm-device.h"

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

26
#include "libnm-std-aux/unaligned.h"
27
#include "libnm-glib-aux/nm-uuid.h"
28
29
#include "libnm-glib-aux/nm-dedup-multi.h"
#include "libnm-glib-aux/nm-random-utils.h"
30
#include "libnm-systemd-shared/nm-sd-utils-shared.h"
31

32
#include "libnm-base/nm-ethtool-base.h"
33
#include "libnm-core-aux-intern/nm-common-macros.h"
34
#include "nm-device-private.h"
35
36
#include "nm-l3cfg.h"
#include "nm-l3-config-data.h"
37
38
#include "nm-l3-ipv4ll.h"
#include "nm-l3-ipv6ll.h"
39
#include "NetworkManagerUtils.h"
40
#include "nm-manager.h"
41
#include "libnm-platform/nm-platform.h"
42
#include "libnm-platform/nm-platform-utils.h"
43
44
#include "libnm-platform/nmp-object.h"
#include "libnm-platform/nmp-rules-manager.h"
45
46
#include "ndisc/nm-ndisc.h"
#include "ndisc/nm-lndp-ndisc.h"
47

48
#include "dhcp/nm-dhcp-manager.h"
49
#include "dhcp/nm-dhcp-utils.h"
50
#include "nm-act-request.h"
Atul Anand's avatar
Atul Anand committed
51
#include "nm-pacrunner-manager.h"
52
#include "dnsmasq/nm-dnsmasq-manager.h"
53
#include "nm-ip-config.h"
54
#include "nm-dhcp-config.h"
55
#include "nm-rfkill-manager.h"
56
#include "nm-firewall-utils.h"
57
#include "nm-firewalld-manager.h"
58
59
#include "settings/nm-settings-connection.h"
#include "settings/nm-settings.h"
60
#include "nm-setting-ethtool.h"
61
#include "nm-setting-ovs-external-ids.h"
62
#include "nm-setting-user.h"
63
#include "nm-auth-utils.h"
64
#include "nm-keep-alive.h"
65
#include "nm-netns.h"
66
#include "nm-dispatcher.h"
67
#include "nm-config.h"
68
#include "c-list/src/c-list.h"
69
#include "dns/nm-dns-manager.h"
70
#include "libnm-core-intern/nm-core-internal.h"
71
#include "libnm-systemd-core/nm-sd.h"
72
#include "nm-lldp-listener.h"
Beniamino Galvani's avatar
Beniamino Galvani committed
73
#include "nm-audit-manager.h"
74
75
#include "nm-connectivity.h"
#include "nm-dbus-interface.h"
76
#include "nm-hostname-manager.h"
77
78

#include "nm-device-generic.h"
79
#include "nm-device-vlan.h"
80
#include "nm-device-vrf.h"
81
#include "nm-device-wireguard.h"
82

83
84
#include "nm-device-logging.h"

85
/*****************************************************************************/
86

87
#define DEFAULT_AUTOCONNECT TRUE
88

89
#define GRACE_PERIOD_MULTIPLIER 2U
90

91
#define CARRIER_WAIT_TIME_MS           6000
92
93
#define CARRIER_WAIT_TIME_AFTER_MTU_MS 10000

94
95
96
97
#define NM_DEVICE_AUTH_RETRIES_UNSET    -1
#define NM_DEVICE_AUTH_RETRIES_INFINITY -2
#define NM_DEVICE_AUTH_RETRIES_DEFAULT  3

98
99
/*****************************************************************************/

100
typedef void (*ActivationHandleFunc)(NMDevice *self);
101
102

typedef enum {
103
104
105
    CLEANUP_TYPE_KEEP,
    CLEANUP_TYPE_REMOVED,
    CLEANUP_TYPE_DECONFIGURE,
106
107
} CleanupType;

108
109
110
111
112
113
114
typedef enum _nm_packed {
    ADDR_METHOD_STATE_DISABLED,
    ADDR_METHOD_STATE_PENDING,
    ADDR_METHOD_STATE_GOOD,
    ADDR_METHOD_STATE_FAILED,
} AddrMethodState;

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

typedef struct {
124
125
    NMDevice *device;
    guint     idle_add_id;
126
127
} DeleteOnDeactivateData;

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

137
typedef enum {
138
    /* The various NML3ConfigData types that we track explicitly. Note that
139
140
     * their relative order matters: higher numbers in this enum means more
     * important (and during merge overwrites other settings). */
141

142
143
    L3_CONFIG_DATA_TYPE_MANUALIP,

144
    L3_CONFIG_DATA_TYPE_LL_4,
145
146
147
148
149
    L3_CONFIG_DATA_TYPE_LL_6,

#define L3_CONFIG_DATA_TYPE_LL_X(IS_IPv4) \
    ((IS_IPv4) ? L3_CONFIG_DATA_TYPE_LL_4 : L3_CONFIG_DATA_TYPE_LL_6)

150
    L3_CONFIG_DATA_TYPE_AC_6,
151
152
    L3_CONFIG_DATA_TYPE_PD_6,

153
154
    L3_CONFIG_DATA_TYPE_DHCP_4,
    L3_CONFIG_DATA_TYPE_DHCP_6,
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

#define L3_CONFIG_DATA_TYPE_DHCP_X(IS_IPv4) \
    ((IS_IPv4) ? L3_CONFIG_DATA_TYPE_DHCP_4 : L3_CONFIG_DATA_TYPE_DHCP_6)

    L3_CONFIG_DATA_TYPE_SHARED_4,
    L3_CONFIG_DATA_TYPE_DEVIP_UNSPEC,
    L3_CONFIG_DATA_TYPE_DEVIP_4,
    L3_CONFIG_DATA_TYPE_DEVIP_6,

#define L3_CONFIG_DATA_TYPE_DEVIP(addr_family)     \
    ({                                             \
        L3ConfigDataType _t;                       \
                                                   \
        switch (addr_family) {                     \
        case AF_INET:                              \
            _t = L3_CONFIG_DATA_TYPE_DEVIP_4;      \
            break;                                 \
        case AF_INET6:                             \
            _t = L3_CONFIG_DATA_TYPE_DEVIP_6;      \
            break;                                 \
        default:                                   \
            nm_assert_not_reached();               \
            /* fall-through */                     \
        case AF_UNSPEC:                            \
            _t = L3_CONFIG_DATA_TYPE_DEVIP_UNSPEC; \
            break;                                 \
        }                                          \
                                                   \
        _t;                                        \
    })

186
187
    _L3_CONFIG_DATA_TYPE_NUM,
    _L3_CONFIG_DATA_TYPE_NONE,
188
    _L3_CONFIG_DATA_TYPE_ACD_ONLY,
189
190
} L3ConfigDataType;

191
G_STATIC_ASSERT(NM_L3CFG_CONFIG_PRIORITY_IPV4LL == L3_CONFIG_DATA_TYPE_LL_4);
192
193

typedef enum {
194
195
196
197
    HW_ADDR_TYPE_UNSET = 0,
    HW_ADDR_TYPE_PERMANENT,
    HW_ADDR_TYPE_EXPLICIT,
    HW_ADDR_TYPE_GENERATED,
198
199
} HwAddrType;

200
typedef enum {
201
202
203
204
    FIREWALL_STATE_UNMANAGED = 0,
    FIREWALL_STATE_INITIALIZED,
    FIREWALL_STATE_WAIT_STAGE_3,
    FIREWALL_STATE_WAIT_IP_CONFIG,
205
206
} FirewallState;

207
typedef struct {
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    NMIPConfig *ip_config;
} L3IPData;

typedef struct {
    GSource *check_async_source;
    GSource *req_timeout_source;
    union {
        const NMDeviceIPState state;
        NMDeviceIPState       state_;
    };
    bool wait_for_carrier : 1;
    bool wait_for_ports : 1;
    bool is_disabled : 1;
    bool is_ignore : 1;
    bool do_reapply : 1;
} IPStateData;

typedef struct {
226
227
    NMDhcpClient   *client;
    NMDhcpConfig   *config;
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    gulong          notify_sigid;
    NMDeviceIPState state;
    union {
        struct {
        } v4;
        struct {
            guint            needed_prefixes;
            NMNDiscDHCPLevel mode;
        } v6;
    };
} IPDhcpStateData;

typedef struct {
    NMDeviceIPState     state;
    NMDeviceStateReason failed_reason;
} IPDevStateData;
244

245
typedef struct {
246
247
248
    NMDeviceIPState state;
    union {
        struct {
249
            NMDnsMasqManager      *dnsmasq_manager;
250
            NMNetnsSharedIPHandle *shared_ip_handle;
251
            NMFirewallConfig      *firewall_config;
252
            gulong                 dnsmasq_state_id;
253
            const NML3ConfigData  *l3cd;
254
255
256
257
258
259
260
261
262
263
        } v4;
        struct {
        } v6;
    };
} IPSharedStateData;

typedef struct {
    NMDeviceIPState state;
    union {
        struct {
264
            NML3IPv4LL             *ipv4ll;
265
            NML3IPv4LLRegistration *ipv4ll_registation;
266
            GSource                *timeout_source;
267
268
        } v4;
        struct {
269
270
            NML3IPv6LL     *ipv6ll;
            GSource        *retry_source;
271
272
273
274
275
            NML3IPv6LLState llstate;
            struct in6_addr lladdr;
        } v6;
    };
} IPLLStateData;
276

277
struct _NMDeviceConnectivityHandle {
278
    CList                        concheck_lst;
279
    NMDevice                    *self;
280
281
    NMDeviceConnectivityCallback callback;
    gpointer                     user_data;
282
    NMConnectivityCheckHandle   *c_handle;
283
284
285
286
287
    guint64                      seq;
    bool                         is_periodic : 1;
    bool                         is_periodic_bump : 1;
    bool                         is_periodic_bump_on_complete : 1;
    int                          addr_family;
288
289
};

290
typedef struct {
291
292
    int                     ifindex;
    NMEthtoolFeatureStates *features;
293
    NMOptionBool            requested[_NM_ETHTOOL_ID_FEATURE_NUM];
294
    NMEthtoolCoalesceState *coalesce;
295
296
    NMEthtoolRingState     *ring;
    NMEthtoolPauseState    *pause;
297
298
} EthtoolState;

299
300
301
302
303
304
305
306
307
308
typedef enum {
    RESOLVER_WAIT_ADDRESS = 0,
    RESOLVER_IN_PROGRESS,
    RESOLVER_DONE,
} ResolverState;

typedef struct {
    ResolverState state;
    GInetAddress *address;
    GCancellable *cancellable;
309
310
    char         *hostname;
    NMDevice     *device;
311
312
313
314
    guint         timeout_id; /* Used when waiting for the address */
    int           addr_family;
} HostnameResolver;

315
/*****************************************************************************/
316

317
enum {
318
319
    STATE_CHANGED,
    AUTOCONNECT_ALLOWED,
320
    L3CD_CHANGED,
321
322
323
324
325
    IP6_PREFIX_DELEGATED,
    IP6_SUBNET_NEEDED,
    REMOVED,
    RECHECK_AUTO_ACTIVATE,
    RECHECK_ASSUME,
326
    DNS_LOOKUP_DONE,
327
    PLATFORM_ADDRESS_CHANGED,
328
    LAST_SIGNAL,
329
};
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
static guint signals[LAST_SIGNAL] = {0};

NM_GOBJECT_PROPERTIES_DEFINE(NMDevice,
                             PROP_UDI,
                             PROP_PATH,
                             PROP_IFACE,
                             PROP_IP_IFACE,
                             PROP_DRIVER,
                             PROP_DRIVER_VERSION,
                             PROP_FIRMWARE_VERSION,
                             PROP_CAPABILITIES,
                             PROP_CARRIER,
                             PROP_MTU,
                             PROP_IP4_ADDRESS,
                             PROP_IP4_CONFIG,
                             PROP_DHCP4_CONFIG,
346
347
348

#define PROP_DHCPX_CONFIG(IS_IPv4) ((IS_IPv4) ? PROP_DHCP4_CONFIG : PROP_DHCP6_CONFIG)

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
                             PROP_IP6_CONFIG,
                             PROP_DHCP6_CONFIG,
                             PROP_STATE,
                             PROP_STATE_REASON,
                             PROP_ACTIVE_CONNECTION,
                             PROP_DEVICE_TYPE,
                             PROP_LINK_TYPE,
                             PROP_MANAGED,
                             PROP_AUTOCONNECT,
                             PROP_FIRMWARE_MISSING,
                             PROP_NM_PLUGIN_MISSING,
                             PROP_TYPE_DESC,
                             PROP_RFKILL_TYPE,
                             PROP_IFINDEX,
                             PROP_AVAILABLE_CONNECTIONS,
                             PROP_PHYSICAL_PORT_ID,
                             PROP_MASTER,
                             PROP_PARENT,
                             PROP_HW_ADDRESS,
                             PROP_PERM_HW_ADDRESS,
                             PROP_HAS_PENDING_ACTION,
                             PROP_METERED,
                             PROP_LLDP_NEIGHBORS,
                             PROP_REAL,
                             PROP_SLAVES,
                             PROP_STATISTICS_REFRESH_RATE_MS,
                             PROP_STATISTICS_TX_BYTES,
                             PROP_STATISTICS_RX_BYTES,
                             PROP_IP4_CONNECTIVITY,
                             PROP_IP6_CONNECTIVITY,
379
380
                             PROP_INTERFACE_FLAGS,
                             PROP_PORTS, );
381

382
typedef struct _NMDevicePrivate {
383
384
385
386
387
388
389
390
391
392
393
394
395
    guint device_link_changed_id;
    guint device_ip_link_changed_id;

    NMDeviceState       state;
    NMDeviceStateReason state_reason;
    struct {
        guint id;

        /* The @state/@reason is only valid, when @id is set. */
        NMDeviceState       state;
        NMDeviceStateReason reason;
    } queued_state;

396
397
398
399
400
401
    struct {
        const char **arr;
        guint        len;
        guint        alloc;
    } pending_actions;

402
403
404
405
406
407
408
    NMDBusTrackObjPath parent_device;

    char *udi;
    char *path;

    union {
        const char *const iface;
409
        char             *iface_;
410
411
412
    };
    union {
        const char *const ip_iface;
413
        char             *ip_iface_;
414
415
    };

416
417
    union {
        NML3Cfg *const l3cfg;
418
        NML3Cfg       *l3cfg_;
419
420
421
422
423
424
425
426
427
428
429
430
    };

    union {
        struct {
            L3IPData l3ipdata_6;
            L3IPData l3ipdata_4;
        };
        L3IPData l3ipdata_x[2];
    };

    NML3CfgCommitTypeHandle *l3cfg_commit_type;

431
432
433
434
    union {
        const int ifindex;
        int       ifindex_;
    };
435

436
437
438
439
440
    union {
        const int ip_ifindex;
        int       ip_ifindex_;
    };

441
442
443
    union {
        const NML3ConfigData *d;
    } l3cds[_L3_CONFIG_DATA_TYPE_NUM];
444
445
446
447
448

    int parent_ifindex;

    int auth_retries;

449
450
451
452
453
454
455
456
    union {
        struct {
            HostnameResolver *hostname_resolver_6;
            HostnameResolver *hostname_resolver_4;
        };
        HostnameResolver *hostname_resolver_x[2];
    };

457
458
459
460
461
462
463
464
465
466
    union {
        const guint8 hw_addr_len; /* read-only */
        guint8       hw_addr_len_;
    };

    HwAddrType hw_addr_type : 5;

    bool real : 1;

    NMDeviceType         type;
467
    char                *type_desc;
468
469
    NMLinkType           link_type;
    NMDeviceCapabilities capabilities;
470
471
472
    char                *driver;
    char                *driver_version;
    char                *firmware_version;
473
474
475
476
477
478
    RfKillType           rfkill_type;
    bool                 firmware_missing : 1;
    bool                 nm_plugin_missing : 1;
    bool
        hw_addr_perm_fake : 1; /* whether the permanent HW address could not be read and is a fake */

479
480
    guint8 in_state_changed : 4;

481
482
483
484
485
486
487
    NMUtilsStableType current_stable_id_type : 3;

    bool nm_owned : 1; /* whether the device is a device owned and created by NM */

    bool  assume_state_guess_assume : 1;
    char *assume_state_connection_uuid;

488
489
    guint64 udi_id;

490
    GHashTable *available_connections;
491
492
493
494
    char       *hw_addr;
    char       *hw_addr_perm;
    char       *hw_addr_initial;
    char       *physical_port_id;
495
496
497
498
499
500
501
502
503
    guint       dev_id;

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

    GCancellable *deactivating_cancellable;

504
    NMActRequest      *queued_act_request;
505
506
507
    bool               queued_act_request_is_waiting_for_carrier : 1;
    NMDBusTrackObjPath act_request;

508
    GSource             *activation_idle_source;
509
    ActivationHandleFunc activation_func;
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530

    guint recheck_assume_id;

    struct {
        guint               call_id;
        NMDeviceStateReason available_reason;
        NMDeviceStateReason unavailable_reason;
    } recheck_available;

    struct {
        NMDispatcherCallId *call_id;
        NMDeviceState       post_state;
        NMDeviceStateReason post_state_reason;
    } dispatcher;

    /* Link stuff */
    guint             link_connected_id;
    guint             link_disconnected_id;
    guint             carrier_defer_id;
    guint             carrier_wait_id;
    gulong            config_changed_id;
531
    gulong            ifindex_changed_id;
532
    guint32           mtu;
533
    guint32           ip6_mtu; /* FIXME(l3cfg) */
534
535
536
537
538
539
540
541
    guint32           mtu_initial;
    guint32           ip6_mtu_initial;
    NMDeviceMtuSource mtu_source;

    guint32 v4_route_table;
    guint32 v6_route_table;

    /* when carrier goes away, we give a grace period of _get_carrier_wait_ms()
542
543
544
545
546
     * until taking action.
     *
     * When changing MTU, the device might take longer then that. So, whenever
     * NM changes the MTU it sets @carrier_wait_until_ms to CARRIER_WAIT_TIME_AFTER_MTU_MS
     * in the future. This is used to extend the grace period in this particular case. */
547
    gint64 carrier_wait_until_ms;
548

549
550
551
552
553
554
555
556
    union {
        struct {
            NML3ConfigMergeFlags l3config_merge_flags_6;
            NML3ConfigMergeFlags l3config_merge_flags_4;
        };
        NML3ConfigMergeFlags l3config_merge_flags_x[2];
    };

557
558
559
560
    union {
        const NMDeviceSysIfaceState sys_iface_state;
        NMDeviceSysIfaceState       sys_iface_state_;
    };
561

562
563
    bool carrier : 1;
    bool ignore_carrier : 1;
Thomas Haller's avatar
Thomas Haller committed
564

565
    bool up : 1; /* IFF_UP */
566

567
568
    bool v4_route_table_initialized : 1;
    bool v6_route_table_initialized : 1;
569

570
    bool l3config_merge_flags_has : 1;
571

572
573
    bool v4_route_table_all_sync_before : 1;
    bool v6_route_table_all_sync_before : 1;
574

575
    NMDeviceAutoconnectBlockedFlags autoconnect_blocked_flags : 5;
576

577
    bool is_enslaved : 1;
578

579
    bool device_link_changed_down : 1;
580

581
    bool concheck_rp_filter_checked : 1;
582

583
    bool tc_committed : 1;
584

585
    NMDeviceStageState stage1_sriov_state : 3;
586

587
    char *current_stable_id;
588

589
    NMPacrunnerConfId *pacrunner_conf_id;
Atul Anand's avatar
Atul Anand committed
590

591
592
593
594
595
596
597
    struct {
        union {
            const NMDeviceIPState state;
            NMDeviceIPState       state_;
        };
    } ip_data;

598
599
    union {
        struct {
600
601
            IPStateData ip_data_6;
            IPStateData ip_data_4;
602
        };
603
        IPStateData ip_data_x[2];
604
    };
605

606
607
608
609
610
611
612
613
614
615
616
    struct {
        GSource *carrier_timeout;
        union {
            struct {
                NMDeviceIPState state_6;
                NMDeviceIPState state_4;
            };
            NMDeviceIPState state_x[2];
        };
        bool carrier_timeout_expired;
    } ipmanual_data;
617

618
619
    union {
        struct {
620
621
            IPDhcpStateData ipdhcp_data_6;
            IPDhcpStateData ipdhcp_data_4;
622
        };
623
        IPDhcpStateData ipdhcp_data_x[2];
624
    };
625

626
    struct {
627
628
        NMNDisc              *ndisc;
        GSource              *ndisc_grace_source;
629
630
631
632
        gulong                ndisc_changed_id;
        gulong                ndisc_timeout_id;
        NMDeviceIPState       state;
        const NML3ConfigData *l3cd;
633
634
    } ipac6_data;

635
636
    union {
        struct {
637
638
            IPLLStateData ipll_data_6;
            IPLLStateData ipll_data_4;
639
        };
640
        IPLLStateData ipll_data_x[2];
641
    };
642

643
644
    union {
        struct {
645
646
            IPSharedStateData ipshared_data_6;
            IPSharedStateData ipshared_data_4;
647
        };
648
        IPSharedStateData ipshared_data_x[2];
649
    };
650

651
652
    union {
        struct {
653
654
            IPDevStateData ipdev_data_6;
            IPDevStateData ipdev_data_4;
655
        };
656
        IPDevStateData ipdev_data_x[2];
657
658
    };

659
    IPDevStateData ipdev_data_unspec;
660
661

    struct {
662
663
        /* If we set the addrgenmode6, this records the previously set value. */
        guint8 previous_mode_val;
664

665
666
667
        /* whether @previous_mode_val is set. */
        bool previous_mode_has : 1;
    } addrgenmode6_data;
668
669
670
671
672
673

    struct {
        NMLogDomain log_domain;
        guint       timeout;
        guint       watch;
        GPid        pid;
674
675
        char       *binary;
        char       *address;
676
677
678
679
        guint       deadline;
    } gw_ping;

    /* Firewall */
680
    FirewallState             fw_state : 4;
681
    NMFirewalldManager       *fw_mgr;
682
    NMFirewalldManagerCallId *fw_call;
683
684
685
686
687
688
689
690

    GHashTable *ip6_saved_properties;

    EthtoolState *ethtool_state;

    /* master interface for bridge/bond/team slave */
    NMDevice *master;
    gulong    master_ready_id;
691
    int       master_ifindex;
692
693
694
695
696
697
698

    /* slave management */
    CList slaves; /* list of SlaveInfo */

    NMMetered metered;

    NMSettings *settings;
699
    NMManager  *manager;
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714

    NMNetns *netns;

    NMLldpListener *lldp_listener;

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

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

        /* the current interval. If we are probing, the interval might be lower
715
         * then the configured max interval. */
716
        guint p_cur_interval;
717

718
        /* the timestamp, when we last scheduled the timer p_cur_id with current interval
719
         * p_cur_interval. */
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
        gint64 p_cur_basetime_ns;

        NMConnectivityState state;
    } concheck_x[2];

    guint   check_delete_unrealized_id;
    guint32 interface_flags;

    struct {
        SriovOp *pending; /* SR-IOV operation currently running */
        SriovOp *next;    /* next SR-IOV operation scheduled */
    } sriov;
    guint sriov_reset_pending;

    struct {
735
736
737
738
        GSource *timeout_source;
        guint    refresh_rate_ms;
        guint64  tx_bytes;
        guint64  rx_bytes;
739
    } stats;
740
741

    bool mtu_force_set_done : 1;
742

743
744
    bool needs_ip6_subnet : 1;

745
    NMOptionBool promisc_reset;
746
747

    GVariant *ports_variant; /* Array of port devices D-Bus path */
748
    char     *prop_ip_iface; /* IP interface D-Bus property */
749
} NMDevicePrivate;
750

751
G_DEFINE_ABSTRACT_TYPE(NMDevice, nm_device, NM_TYPE_DBUS_OBJECT)
752
753
754
755
756

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

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

757
static const NMDBusInterfaceInfoExtended interface_info_device;
758
759
static const GDBusSignalInfo             signal_info_state_changed;

760
static void _dev_l3_cfg_commit(NMDevice *self, gboolean do_sync);
761

762
static void _dev_l3_cfg_commit_type_reset(NMDevice *self);
763
764
765
766
767

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

768
static void _dev_ipll6_start(NMDevice *self);
769

770
771
static void _dev_ipac6_start_continue(NMDevice *self);
static void _dev_ipac6_ndisc_set_router_config(NMDevice *self);
772

773
static guint32 _dev_default_route_metric_penalty_get(NMDevice *self, int addr_family);
774

775
static guint32 _prop_get_ipv4_dad_timeout(NMDevice *self);
776

777
778
779
static void   _carrier_wait_check_queued_act_request(NMDevice *self);
static gint64 _get_carrier_wait_ms(NMDevice *self);

780
static GBytes *_prop_get_ipv6_dhcp_duid(NMDevice     *self,
781
                                        NMConnection *connection,
782
783
                                        GBytes       *hwaddr,
                                        gboolean     *out_enforce);
784

785
786
787
788
static const char *_activation_func_to_string(ActivationHandleFunc func);

static void
_set_state_full(NMDevice *self, NMDeviceState state, NMDeviceStateReason reason, gboolean quitting);
789
790
791
static void queued_state_clear(NMDevice *device);
static void ip_check_ping_watch_cb(GPid pid, int status, gpointer user_data);
static void nm_device_start_ip_check(NMDevice *self);
792
static void realize_start_setup(NMDevice             *self,
793
794
                                const NMPlatformLink *plink,
                                gboolean              assume_state_guess_assume,
795
                                const char           *assume_state_connection_uuid,
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
                                gboolean              set_nm_owned,
                                NMUnmanFlagOp         unmanaged_user_explicit,
                                gboolean              force_platform_init);
static void _set_mtu(NMDevice *self, guint32 mtu);
static void _commit_mtu(NMDevice *self);
static void _cancel_activation(NMDevice *self);

static void _dev_ipll4_notify_event(NMDevice *self);

static void _dev_ip_state_check(NMDevice *self, int addr_family);

static void _dev_ipmanual_check_ready(NMDevice *self);

static void
_dev_ipdhcpx_cleanup(NMDevice *self, int addr_family, gboolean reset_dhcp_config, gboolean release);

static void _dev_ip_state_check_async(NMDevice *self, int addr_family);

static void _dev_ipdhcpx_set_state(NMDevice *self, int addr_family, NMDeviceIPState state);

static void _dev_ipdhcpx_restart(NMDevice *self, int addr_family, gboolean release);

static void _dev_ipdhcpx_handle_accept(NMDevice *self, int addr_family, const NML3ConfigData *l3cd);

static gboolean
_dev_ipac6_grace_period_start(NMDevice *self, guint32 timeout_sec, gboolean force_restart);

static void _dev_ipac6_start(NMDevice *self);

825
826
static void _dev_ipac6_set_state(NMDevice *self, NMDeviceIPState state);

827
828
829
830
831
832
833
static void _dev_unamanged_check_external_down(NMDevice *self, gboolean only_if_unmanaged);

static void _dev_ipshared4_start(NMDevice *self);
static void _dev_ipshared4_spawn_dnsmasq(NMDevice *self);

static void _dev_ipshared6_start(NMDevice *self);

834
835
static void
_cleanup_ip_pre(NMDevice *self, int addr_family, CleanupType cleanup_type, gboolean from_reapply);
836

837
static void concheck_update_state(NMDevice           *self,
838
839
840
841
842
                                  int                 addr_family,
                                  NMConnectivityState state,
                                  gboolean            is_periodic);

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

844
static void device_ifindex_changed_cb(NMManager *manager, NMDevice *device_changed, NMDevice *self);
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
static gboolean device_link_changed(gpointer user_data);
static gboolean _get_maybe_ipv6_disabled(NMDevice *self);

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

#define _NMLOG_addr_family(level, prefix, addr_family, fmt, ...)                             \
    G_STMT_START                                                                             \
    {                                                                                        \
        const int _addr_family2 = (addr_family);                                             \
                                                                                             \
        _NMLOG(level,                                                                        \
               (_addr_family2 == AF_UNSPEC ? LOGD_IP : LOGD_IPX(NM_IS_IPv4(_addr_family2))), \
               "" prefix "%s: " fmt,                                                         \
               nm_utils_addr_family_to_str(_addr_family2),                                   \
               ##__VA_ARGS__);                                                               \
    }                                                                                        \
    G_STMT_END

#define _NMLOG_ip(level, ...) _NMLOG_addr_family(level, "ip", __VA_ARGS__)
#define _LOGT_ip(...)         _NMLOG_ip(LOGL_TRACE, __VA_ARGS__)
#define _LOGD_ip(...)         _NMLOG_ip(LOGL_DEBUG, __VA_ARGS__)
#define _LOGI_ip(...)         _NMLOG_ip(LOGL_INFO, __VA_ARGS__)
#define _LOGW_ip(...)         _NMLOG_ip(LOGL_WARN, __VA_ARGS__)

#define _NMLOG_ipll(level, ...) _NMLOG_addr_family(level, "ip:ll", __VA_ARGS__)
#define _LOGT_ipll(...)         _NMLOG_ipll(LOGL_TRACE, __VA_ARGS__)
#define _LOGD_ipll(...)         _NMLOG_ipll(LOGL_DEBUG, __VA_ARGS__)
#define _LOGI_ipll(...)         _NMLOG_ipll(LOGL_INFO, __VA_ARGS__)
#define _LOGW_ipll(...)         _NMLOG_ipll(LOGL_WARN, __VA_ARGS__)

#define _NMLOG_ipdev(level, ...) _NMLOG_addr_family(level, "ip:dev", __VA_ARGS__)
#define _LOGT_ipdev(...)         _NMLOG_ipdev(LOGL_TRACE, __VA_ARGS__)
#define _LOGD_ipdev(...)         _NMLOG_ipdev(LOGL_DEBUG, __VA_ARGS__)
#define _LOGI_ipdev(...)         _NMLOG_ipdev(LOGL_INFO, __VA_ARGS__)
#define _LOGW_ipdev(...)         _NMLOG_ipdev(LOGL_WARN, __VA_ARGS__)

#define _NMLOG_ipdhcp(level, ...) _NMLOG_addr_family(level, "ip:dhcp", __VA_ARGS__)
#define _LOGT_ipdhcp(...)         _NMLOG_ipdhcp(LOGL_TRACE, __VA_ARGS__)
#define _LOGD_ipdhcp(...)         _NMLOG_ipdhcp(LOGL_DEBUG, __VA_ARGS__)
#define _LOGI_ipdhcp(...)         _NMLOG_ipdhcp(LOGL_INFO, __VA_ARGS__)
#define _LOGW_ipdhcp(...)         _NMLOG_ipdhcp(LOGL_WARN, __VA_ARGS__)

#define _NMLOG_ipshared(level, ...) _NMLOG_addr_family(level, "ip:shared", __VA_ARGS__)
#define _LOGT_ipshared(...)         _NMLOG_ipshared(LOGL_TRACE, __VA_ARGS__)
#define _LOGD_ipshared(...)         _NMLOG_ipshared(LOGL_DEBUG, __VA_ARGS__)
#define _LOGI_ipshared(...)         _NMLOG_ipshared(LOGL_INFO, __VA_ARGS__)
#define _LOGW_ipshared(...)         _NMLOG_ipshared(LOGL_WARN, __VA_ARGS__)

#define _NMLOG_ipac6(level, ...) _NMLOG_addr_family(level, "ip:ac6", AF_UNSPEC, __VA_ARGS__)
#define _LOGT_ipac6(...)         _NMLOG_ipac6(LOGL_TRACE, __VA_ARGS__)
#define _LOGD_ipac6(...)         _NMLOG_ipac6(LOGL_DEBUG, __VA_ARGS__)
#define _LOGI_ipac6(...)         _NMLOG_ipac6(LOGL_INFO, __VA_ARGS__)
#define _LOGW_ipac6(...)         _NMLOG_ipac6(LOGL_WARN, __VA_ARGS__)

#define _NMLOG_ipmanual(level, ...) _NMLOG_addr_family(level, "ip:manual", __VA_ARGS__)
#define _LOGT_ipmanual(...)         _NMLOG_ipmanual(LOGL_TRACE, __VA_ARGS__)
#define _LOGD_ipmanual(...)         _NMLOG_ipmanual(LOGL_DEBUG, __VA_ARGS__)
#define _LOGI_ipmanual(...)         _NMLOG_ipmanual(LOGL_INFO, __VA_ARGS__)
#define _LOGW_ipmanual(...)         _NMLOG_ipmanual(LOGL_WARN, __VA_ARGS__)

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

#define _CACHED_BOOL(cached_value, cmd)                  \
    ({                                                   \
        NMTernary *const _cached_value = (cached_value); \
                                                         \
        nm_assert(_cached_value);                        \
        nm_assert_is_ternary(*_cached_value);            \
                                                         \
        if (*_cached_value == NM_TERNARY_DEFAULT)        \
            *_cached_value = !!(cmd);                    \
                                                         \
        !!(*_cached_value);                              \
    })
919

920
/*****************************************************************************/
921

922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
static void
_hostname_resolver_free(HostnameResolver *resolver)
{
    if (!resolver)
        return;

    nm_clear_g_source(&resolver->timeout_id);
    nm_clear_g_cancellable(&resolver->cancellable);
    nm_g_object_unref(resolver->address);
    g_free(resolver->hostname);
    nm_g_slice_free(resolver);
}

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

937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/**
 * Update the "ip_iface" property when something changes (device
 * state, ifindex) and emit a notify signal if needed. Note that
 * the property must be NULL for devices without an ifindex and
 * when the device is not activated. This behavior is part of the
 * API and should not be changed.
 */
static void
update_prop_ip_iface(NMDevice *self)
{
    NMDevicePrivate *priv     = NM_DEVICE_GET_PRIVATE(self);
    const char      *ip_iface = NULL;
    gs_free char    *to_free  = NULL;

    if (nm_device_get_ip_ifindex(self) > 0
        && (priv->state == NM_DEVICE_STATE_UNMANAGED
            || (priv->state >= NM_DEVICE_STATE_IP_CHECK
                && priv->state <= NM_DEVICE_STATE_DEACTIVATING))) {
        ip_iface = nm_utils_str_utf8safe_escape(nm_device_get_ip_iface(self),
                                                NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL,
                                                &to_free);
    }

    if (!nm_streq0(priv->prop_ip_iface, ip_iface)) {
        g_free(priv->prop_ip_iface);
        priv->prop_ip_iface = to_free ? g_steal_pointer(&to_free) : g_strdup(ip_iface);
        _notify(self, PROP_IP_IFACE);
    }
}

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

969
static NMSettingIP6ConfigPrivacy
970
_ip6_privacy_clamp(NMSettingIP6ConfigPrivacy use_tempaddr)
971
{
972
973
974
975
976
977
978
979
    switch (use_tempaddr) {
    case NM_SETTING_IP6_CONFIG_PRIVACY_DISABLED:
    case NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR:
    case NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR:
        return use_tempaddr;
    default:
        return NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN;
    }
980
981
}

982
/*****************************************************************************/
983

984
static const char *
985
986
_prop_get_connection_stable_id(NMDevice          *self,
                               NMConnection      *connection,
987
                               NMUtilsStableType *out_stable_type)
988
{
989
    NMDevicePrivate *priv;
990

991
992
993
    nm_assert(NM_IS_DEVICE(self));
    nm_assert(NM_IS_CONNECTION(connection));
    nm_assert(out_stable_type);
994

995
    priv = NM_DEVICE_GET_PRIVATE(self);
996

997
    /* we cache the generated stable ID for the time of an activation.
998
999
1000
     *
     * The reason is, that we don't want the stable-id to change as long
     * as the device is active.
For faster browsing, not all history is shown. View entire blame