summaryrefslogtreecommitdiff
path: root/src/core/dhcp/nm-dhcp-manager.c
blob: 44b8ede2c08dd6d4958a7dd7cdb2bd13148b6965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2005 - 2013 Red Hat, Inc.
 * Copyright (C) 2006 - 2008 Novell, Inc.
 */

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

#include "nm-dhcp-manager.h"

#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#include "libnm-glib-aux/nm-dedup-multi.h"
#include "libnm-systemd-shared/nm-sd-utils-shared.h"

#include "nm-config.h"
#include "NetworkManagerUtils.h"

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

typedef struct {
    const NMDhcpClientFactory *client_factory;
    char *                     default_hostname;
    CList                      dhcp_client_lst_head;
} NMDhcpManagerPrivate;

struct _NMDhcpManager {
    GObject              parent;
    NMDhcpManagerPrivate _priv;
};

struct _NMDhcpManagerClass {
    GObjectClass parent;
};

G_DEFINE_TYPE(NMDhcpManager, nm_dhcp_manager, G_TYPE_OBJECT)

#define NM_DHCP_MANAGER_GET_PRIVATE(self) _NM_GET_PRIVATE(self, NMDhcpManager, NM_IS_DHCP_MANAGER)

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

static void client_state_changed(NMDhcpClient * client,
                                 NMDhcpState    state,
                                 GObject *      ip_config,
                                 GVariant *     options,
                                 NMDhcpManager *self);

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

/* default to installed helper, but can be modified for testing */
const char *nm_dhcp_helper_path = LIBEXECDIR "/nm-dhcp-helper";

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

static const NMDhcpClientFactory *
_client_factory_find_by_name(const char *name)
{
    int i;

    g_return_val_if_fail(name, NULL);

    for (i = 0; i < G_N_ELEMENTS(_nm_dhcp_manager_factories); i++) {
        const NMDhcpClientFactory *f = _nm_dhcp_manager_factories[i];

        if (f && nm_streq(f->name, name))
            return f;
    }
    return NULL;
}

static const NMDhcpClientFactory *
_client_factory_available(const NMDhcpClientFactory *client_factory)
{
    if (client_factory && (!client_factory->get_path || client_factory->get_path()))
        return client_factory;
    return NULL;
}

static GType
_client_factory_get_gtype(const NMDhcpClientFactory *client_factory, int addr_family)
{
    GType                    gtype;
    nm_auto_unref_gtypeclass NMDhcpClientClass *klass = NULL;

    nm_assert(client_factory);
    nm_assert_addr_family(addr_family);

    /* currently, the chosen DHCP plugin for IPv4 and IPv6 is configured in NetworkManager.conf
     * and cannot be reloaded. It would be nice to configure the plugin per address family
     * or to be able to reload it.
     *
     * Note that certain options in NetworkManager.conf depend on the chosen DHCP plugin.
     * See "dhcp-plugin:" in "Device List Format" (`man NetworkManager.conf`).
     * Supporting reloading the plugin would also require to re-evalate the decisions from
     * the "Device List Format". Likewise, having per-address family plugins would make the
     * "main.dhcp" setting and "dhcp-plugin:" match non-sensical because these configurations
     * currently are address family independent.
     *
     * So actually, we don't want that complexity. We want to phase out all plugins in favor
     * of the internal plugin.
     * However, certain existing plugins are well known to not support an address family.
     * In those cases, we should just silently fallback to the internal plugin.
     *
     * This could be a problem with forward compatibility if we ever intended to add IPv6 support
     * to those plugins. But we don't intend to do so. The internal plugin is the way forward and
     * not extending other plugins. */

    if (client_factory->get_type_per_addr_family)
        gtype = client_factory->get_type_per_addr_family(addr_family);
    else
        gtype = client_factory->get_type();

    if (client_factory == &_nm_dhcp_client_factory_internal) {
        /* we are already using the internal plugin. Nothing to do. */
        goto out;
    }

    klass = g_type_class_ref(gtype);

    nm_assert(NM_IS_DHCP_CLIENT_CLASS(klass));

    if (addr_family == AF_INET6) {
        if (!klass->ip6_start)
            gtype = _client_factory_get_gtype(&_nm_dhcp_client_factory_internal, addr_family);
    } else {
        if (!klass->ip4_start)
            gtype = _client_factory_get_gtype(&_nm_dhcp_client_factory_internal, addr_family);
    }

out:
    nm_assert(g_type_is_a(gtype, NM_TYPE_DHCP_CLIENT));
    nm_assert(({
        nm_auto_unref_gtypeclass NMDhcpClientClass *k = g_type_class_ref(gtype);

        (addr_family == AF_INET6 && k->ip6_start) || (addr_family == AF_INET && k->ip4_start);
    }));

    return gtype;
}

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

static NMDhcpClient *
get_client_for_ifindex(NMDhcpManager *manager, int addr_family, int ifindex)
{
    NMDhcpManagerPrivate *priv;
    NMDhcpClient *        client;

    g_return_val_if_fail(NM_IS_DHCP_MANAGER(manager), NULL);
    g_return_val_if_fail(ifindex > 0, NULL);

    priv = NM_DHCP_MANAGER_GET_PRIVATE(manager);

    c_list_for_each_entry (client, &priv->dhcp_client_lst_head, dhcp_client_lst) {
        if (nm_dhcp_client_get_ifindex(client) == ifindex
            && nm_dhcp_client_get_addr_family(client) == addr_family)
            return client;
    }

    return NULL;
}

static void
remove_client(NMDhcpManager *self, NMDhcpClient *client)
{
    g_signal_handlers_disconnect_by_func(client, client_state_changed, self);
    c_list_unlink(&client->dhcp_client_lst);

    /* Stopping the client is left up to the controlling device
     * explicitly since we may want to quit NetworkManager but not terminate
     * the DHCP client.
     */
}

static void
remove_client_unref(NMDhcpManager *self, NMDhcpClient *client)
{
    remove_client(self, client);
    g_object_unref(client);
}

static void
client_state_changed(NMDhcpClient * client,
                     NMDhcpState    state,
                     GObject *      ip_config,
                     GVariant *     options,
                     NMDhcpManager *self)
{
    if (state >= NM_DHCP_STATE_TIMEOUT)
        remove_client_unref(self, client);
}

static NMDhcpClient *
client_start(NMDhcpManager *           self,
             int                       addr_family,
             NMDedupMultiIndex *       multi_idx,
             const char *              iface,
             int                       ifindex,
             GBytes *                  hwaddr,
             GBytes *                  bcast_hwaddr,
             const char *              uuid,
             guint32                   route_table,
             guint32                   route_metric,
             const struct in6_addr *   ipv6_ll_addr,
             GBytes *                  dhcp_client_id,
             gboolean                  enforce_duid,
             guint32                   iaid,
             gboolean                  iaid_explicit,
             guint32                   timeout,
             NMDhcpClientFlags         client_flags,
             const char *              anycast_address,
             const char *              hostname,
             NMDhcpHostnameFlags       hostname_flags,
             const char *              mud_url,
             NMSettingIP6ConfigPrivacy privacy,
             const char *              last_ip4_address,
             guint                     needed_prefixes,
             GBytes *                  vendor_class_identifier,
             const char *const *       reject_servers,
             GError **                 error)
{
    NMDhcpManagerPrivate *priv;
    NMDhcpClient *        client;
    gboolean              success = FALSE;
    gsize                 hwaddr_len;
    GType                 gtype;

    g_return_val_if_fail(NM_IS_DHCP_MANAGER(self), NULL);
    g_return_val_if_fail(iface, NULL);
    g_return_val_if_fail(ifindex > 0, NULL);
    g_return_val_if_fail(uuid != NULL, NULL);
    g_return_val_if_fail(!dhcp_client_id || g_bytes_get_size(dhcp_client_id) >= 2, NULL);
    g_return_val_if_fail(!vendor_class_identifier
                             || g_bytes_get_size(vendor_class_identifier) <= 255,
                         NULL);
    g_return_val_if_fail(!error || !*error, NULL);
    nm_assert(!NM_FLAGS_ANY(client_flags, ~NM_DHCP_CLIENT_FLAGS_ALL));

    if (addr_family == AF_INET) {
        if (!hwaddr || !bcast_hwaddr) {
            nm_utils_error_set(error,
                               NM_UTILS_ERROR_UNKNOWN,
                               "missing %s address",
                               hwaddr ? "broadcast" : "MAC");
            return NULL;
        }

        hwaddr_len = g_bytes_get_size(hwaddr);
        if (hwaddr_len == 0 || hwaddr_len > _NM_UTILS_HWADDR_LEN_MAX) {
            nm_utils_error_set(error, NM_UTILS_ERROR_UNKNOWN, "invalid MAC address");
            g_return_val_if_reached(NULL);
        }
        nm_assert(g_bytes_get_size(hwaddr) == g_bytes_get_size(bcast_hwaddr));
    } else {
        hwaddr       = NULL;
        bcast_hwaddr = NULL;
    }

    if (hostname) {
        gboolean use_fqdn = NM_FLAGS_HAS(client_flags, NM_DHCP_CLIENT_FLAGS_USE_FQDN);

        if ((use_fqdn && !nm_sd_dns_name_is_valid(hostname))
            || (!use_fqdn && !nm_sd_hostname_is_valid(hostname, FALSE))) {
            nm_log_warn(LOGD_DHCP,
                        "dhcp%c: %s '%s' is invalid, will be ignored",
                        nm_utils_addr_family_to_char(addr_family),
                        use_fqdn ? "FQDN" : "hostname",
                        hostname);
            hostname = NULL;
        }
    }

    priv = NM_DHCP_MANAGER_GET_PRIVATE(self);

    /* Kill any old client instance */
    client = get_client_for_ifindex(self, addr_family, ifindex);
    if (client) {
        /* FIXME: we cannot just call synchronously "stop()" and forget about the client.
         * We need to wait for the client to be fully stopped because most/all clients
         * cannot quit right away.
         *
         * FIXME(shutdown): also fix this during shutdown, to wait for all DHCP clients
         * to be fully stopped. */
        remove_client(self, client);
        nm_dhcp_client_stop(client, FALSE);
        g_object_unref(client);
    }

    gtype = _client_factory_get_gtype(priv->client_factory, addr_family);

    nm_log_trace(LOGD_DHCP,
                 "dhcp%c: creating IPv%c DHCP client of type %s",
                 nm_utils_addr_family_to_char(addr_family),
                 nm_utils_addr_family_to_char(addr_family),
                 g_type_name(gtype));

    client = g_object_new(gtype,
                          NM_DHCP_CLIENT_MULTI_IDX,
                          multi_idx,
                          NM_DHCP_CLIENT_ADDR_FAMILY,
                          addr_family,
                          NM_DHCP_CLIENT_INTERFACE,
                          iface,
                          NM_DHCP_CLIENT_IFINDEX,
                          ifindex,
                          NM_DHCP_CLIENT_HWADDR,
                          hwaddr,
                          NM_DHCP_CLIENT_BROADCAST_HWADDR,
                          bcast_hwaddr,
                          NM_DHCP_CLIENT_UUID,
                          uuid,
                          NM_DHCP_CLIENT_IAID,
                          (guint) iaid,
                          NM_DHCP_CLIENT_IAID_EXPLICIT,
                          iaid_explicit,
                          NM_DHCP_CLIENT_HOSTNAME,
                          hostname,
                          NM_DHCP_CLIENT_MUD_URL,
                          mud_url,
                          NM_DHCP_CLIENT_ROUTE_TABLE,
                          (guint) route_table,
                          NM_DHCP_CLIENT_ROUTE_METRIC,
                          (guint) route_metric,
                          NM_DHCP_CLIENT_TIMEOUT,
                          (guint) timeout,
                          NM_DHCP_CLIENT_HOSTNAME_FLAGS,
                          (guint) hostname_flags,
                          NM_DHCP_CLIENT_VENDOR_CLASS_IDENTIFIER,
                          vendor_class_identifier,
                          NM_DHCP_CLIENT_REJECT_SERVERS,
                          reject_servers,
                          NM_DHCP_CLIENT_FLAGS,
                          (guint) client_flags,
                          NM_DHCP_CLIENT_ANYCAST_ADDRESS,
                          anycast_address,
                          NULL);
    nm_assert(client && c_list_is_empty(&client->dhcp_client_lst));
    c_list_link_tail(&priv->dhcp_client_lst_head, &client->dhcp_client_lst);
    g_signal_connect(client,
                     NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED,
                     G_CALLBACK(client_state_changed),
                     self);

    /* unfortunately, our implementations work differently per address-family regarding client-id/DUID.
     *
     * - for IPv4, the calling code may determine a client-id (from NM's connection profile).
     *   If present, it is taken. If not present, the DHCP plugin uses a plugin specific default.
     *     - for "internal" plugin, the default is just "mac".
     *     - for "dhclient", we try to get the configuration from dhclient's /etc/dhcp or fallback
     *       to whatever dhclient uses by default.
     *   We do it this way, because for dhclient the user may configure a default
     *   outside of NM, and we want to honor that. Worse, dhclient could be a wapper
     *   script where the wrapper script overwrites the client-id. We need to distinguish
     *   between: force a particular client-id and leave it unspecified to whatever dhclient
     *   wants.
     *
     * - for IPv6, the calling code always determines a client-id. It also specifies @enforce_duid,
     *   to determine whether the given client-id must be used.
     *     - for "internal" plugin @enforce_duid doesn't matter and the given client-id is
     *       always used.
     *     - for "dhclient", @enforce_duid FALSE means to first try to load the DUID from the
     *       lease file, and only otherwise fallback to the given client-id.
     *     - other plugins don't support DHCPv6.
     *   It's done this way, so that existing dhclient setups don't change behavior on upgrade.
     *
     * This difference is cumbersome and only exists because of "dhclient" which supports hacking the
     * default outside of NetworkManager API.
     */

    if (addr_family == AF_INET) {
        success = nm_dhcp_client_start_ip4(client, dhcp_client_id, last_ip4_address, error);
    } else {
        success = nm_dhcp_client_start_ip6(client,
                                           dhcp_client_id,
                                           enforce_duid,
                                           ipv6_ll_addr,
                                           privacy,
                                           needed_prefixes,
                                           error);
    }

    if (!success) {
        remove_client_unref(self, client);
        return NULL;
    }

    return g_object_ref(client);
}

/* Caller owns a reference to the NMDhcpClient on return */
NMDhcpClient *
nm_dhcp_manager_start_ip4(NMDhcpManager *     self,
                          NMDedupMultiIndex * multi_idx,
                          const char *        iface,
                          int                 ifindex,
                          GBytes *            hwaddr,
                          GBytes *            bcast_hwaddr,
                          const char *        uuid,
                          guint32             route_table,
                          guint32             route_metric,
                          NMDhcpClientFlags   client_flags,
                          gboolean            send_hostname,
                          const char *        dhcp_hostname,
                          const char *        dhcp_fqdn,
                          NMDhcpHostnameFlags hostname_flags,
                          const char *        mud_url,
                          GBytes *            dhcp_client_id,
                          guint32             timeout,
                          const char *        anycast_address,
                          const char *        last_ip_address,
                          GBytes *            vendor_class_identifier,
                          const char *const * reject_servers,
                          GError **           error)
{
    NMDhcpManagerPrivate *priv;
    const char *          hostname     = NULL;
    gs_free char *        hostname_tmp = NULL;
    gboolean              use_fqdn     = FALSE;
    char *                dot;

    /* these flags are set automatically/prohibited, and not free to set to the caller.  */
    nm_assert(!NM_FLAGS_ANY(client_flags,
                            NM_DHCP_CLIENT_FLAGS_USE_FQDN | NM_DHCP_CLIENT_FLAGS_INFO_ONLY));

    g_return_val_if_fail(NM_IS_DHCP_MANAGER(self), NULL);
    priv = NM_DHCP_MANAGER_GET_PRIVATE(self);

    if (send_hostname) {
        /* Use, in order of preference:
         *  1. FQDN from configuration
         *  2. hostname from configuration
         *  3. system hostname (only host part)
         */
        if (dhcp_fqdn) {
            hostname = dhcp_fqdn;
            use_fqdn = TRUE;
        } else if (dhcp_hostname)
            hostname = dhcp_hostname;
        else {
            hostname = priv->default_hostname;
            if (hostname) {
                hostname_tmp = g_strdup(hostname);
                dot          = strchr(hostname_tmp, '.');
                if (dot)
                    *dot = '\0';
                hostname = hostname_tmp;
            }
        }
    }

    return client_start(
        self,
        AF_INET,
        multi_idx,
        iface,
        ifindex,
        hwaddr,
        bcast_hwaddr,
        uuid,
        route_table,
        route_metric,
        NULL,
        dhcp_client_id,
        FALSE,
        0,
        FALSE,
        timeout,
        client_flags | (use_fqdn ? NM_DHCP_CLIENT_FLAGS_USE_FQDN : NM_DHCP_CLIENT_FLAGS_NONE),
        anycast_address,
        hostname,
        hostname_flags,
        mud_url,
        0,
        last_ip_address,
        0,
        vendor_class_identifier,
        reject_servers,
        error);
}

/* Caller owns a reference to the NMDhcpClient on return */
NMDhcpClient *
nm_dhcp_manager_start_ip6(NMDhcpManager *           self,
                          NMDedupMultiIndex *       multi_idx,
                          const char *              iface,
                          int                       ifindex,
                          const struct in6_addr *   ll_addr,
                          const char *              uuid,
                          guint32                   route_table,
                          guint32                   route_metric,
                          NMDhcpClientFlags         client_flags,
                          gboolean                  send_hostname,
                          const char *              dhcp_hostname,
                          NMDhcpHostnameFlags       hostname_flags,
                          const char *              mud_url,
                          GBytes *                  duid,
                          gboolean                  enforce_duid,
                          guint32                   iaid,
                          gboolean                  iaid_explicit,
                          guint32                   timeout,
                          const char *              anycast_address,
                          NMSettingIP6ConfigPrivacy privacy,
                          guint                     needed_prefixes,
                          GError **                 error)
{
    NMDhcpManagerPrivate *priv;
    const char *          hostname = NULL;

    /* this flag is set automatically, and not free to set to the caller.  */
    nm_assert(!NM_FLAGS_ANY(client_flags, NM_DHCP_CLIENT_FLAGS_USE_FQDN));

    g_return_val_if_fail(NM_IS_DHCP_MANAGER(self), NULL);
    priv = NM_DHCP_MANAGER_GET_PRIVATE(self);

    if (send_hostname) {
        /* Always prefer the explicit dhcp-hostname if given */
        hostname = dhcp_hostname ?: priv->default_hostname;
    }
    return client_start(self,
                        AF_INET6,
                        multi_idx,
                        iface,
                        ifindex,
                        NULL,
                        NULL,
                        uuid,
                        route_table,
                        route_metric,
                        ll_addr,
                        duid,
                        enforce_duid,
                        iaid,
                        iaid_explicit,
                        timeout,
                        client_flags | NM_DHCP_CLIENT_FLAGS_USE_FQDN,
                        anycast_address,
                        hostname,
                        hostname_flags,
                        mud_url,
                        privacy,
                        NULL,
                        needed_prefixes,
                        NULL,
                        NULL,
                        error);
}

void
nm_dhcp_manager_set_default_hostname(NMDhcpManager *manager, const char *hostname)
{
    NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE(manager);

    nm_clear_g_free(&priv->default_hostname);

    /* Never send 'localhost'-type names to the DHCP server */
    if (!nm_utils_is_specific_hostname(hostname))
        return;

    priv->default_hostname = g_strdup(hostname);
}

const char *
nm_dhcp_manager_get_config(NMDhcpManager *self)
{
    const NMDhcpClientFactory *factory;

    g_return_val_if_fail(NM_IS_DHCP_MANAGER(self), NULL);

    factory = NM_DHCP_MANAGER_GET_PRIVATE(self)->client_factory;
    return factory ? factory->name : NULL;
}

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

NM_DEFINE_SINGLETON_GETTER(NMDhcpManager, nm_dhcp_manager_get, NM_TYPE_DHCP_MANAGER);

void
nmtst_dhcp_manager_unget(gpointer self)
{
    _nmtst_nm_dhcp_manager_get_reset(self);
}

static void
nm_dhcp_manager_init(NMDhcpManager *self)
{
    NMDhcpManagerPrivate *     priv        = NM_DHCP_MANAGER_GET_PRIVATE(self);
    NMConfig *                 config      = nm_config_get();
    gs_free char *             client_free = NULL;
    const char *               client;
    int                        i;
    const NMDhcpClientFactory *client_factory = NULL;

    c_list_init(&priv->dhcp_client_lst_head);

    for (i = 0; i < G_N_ELEMENTS(_nm_dhcp_manager_factories); i++) {
        const NMDhcpClientFactory *f = _nm_dhcp_manager_factories[i];

        if (!f)
            continue;

        nm_log_dbg(LOGD_DHCP,
                   "dhcp-init: enabled DHCP client '%s'%s%s",
                   f->name,
                   _client_factory_available(f) ? "" : " (not available)",
                   f->experimental ? " (undocumented internal plugin)" : "");
    }

    /* Client-specific setup */
    client_free =
        nm_config_data_get_value(nm_config_get_data_orig(config),
                                 NM_CONFIG_KEYFILE_GROUP_MAIN,
                                 NM_CONFIG_KEYFILE_KEY_MAIN_DHCP,
                                 NM_CONFIG_GET_VALUE_STRIP | NM_CONFIG_GET_VALUE_NO_EMPTY);
    client = client_free;
    if (nm_config_get_configure_and_quit(config) == NM_CONFIG_CONFIGURE_AND_QUIT_ENABLED) {
        client_factory = &_nm_dhcp_client_factory_internal;
        if (client && !nm_streq(client, client_factory->name))
            nm_log_info(LOGD_DHCP,
                        "dhcp-init: Using internal DHCP client since configure-and-quit is set.");
    } else {
        if (client) {
            client_factory = _client_factory_available(_client_factory_find_by_name(client));
            if (!client_factory)
                nm_log_warn(LOGD_DHCP, "dhcp-init: DHCP client '%s' not available", client);
        }
        if (!client_factory) {
            client_factory = _client_factory_find_by_name("" NM_CONFIG_DEFAULT_MAIN_DHCP);
            if (!client_factory)
                nm_log_err(LOGD_DHCP,
                           "dhcp-init: default DHCP client '%s' is not installed",
                           NM_CONFIG_DEFAULT_MAIN_DHCP);
            else {
                client_factory = _client_factory_available(client_factory);
                if (!client_factory)
                    nm_log_info(LOGD_DHCP,
                                "dhcp-init: default DHCP client '%s' is not available",
                                NM_CONFIG_DEFAULT_MAIN_DHCP);
            }
        }
        if (!client_factory) {
            for (i = 0; i < G_N_ELEMENTS(_nm_dhcp_manager_factories); i++) {
                client_factory = _client_factory_available(_nm_dhcp_manager_factories[i]);
                if (client_factory)
                    break;
            }
        }
    }

    g_return_if_fail(client_factory);

    nm_log_info(LOGD_DHCP, "dhcp-init: Using DHCP client '%s'", client_factory->name);

    /* NOTE: currently the DHCP plugin is chosen once at start. It's not
     * possible to reload that configuration. If that ever becomes possible,
     * beware that the "dhcp-plugin" device spec made decisions based on
     * the previous plugin and may need reevaluation. */
    priv->client_factory = client_factory;
}

static void
dispose(GObject *object)
{
    NMDhcpManager *       self = NM_DHCP_MANAGER(object);
    NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE(self);
    NMDhcpClient *        client, *client_safe;

    c_list_for_each_entry_safe (client, client_safe, &priv->dhcp_client_lst_head, dhcp_client_lst)
        remove_client_unref(self, client);

    G_OBJECT_CLASS(nm_dhcp_manager_parent_class)->dispose(object);

    nm_clear_g_free(&priv->default_hostname);
}

static void
nm_dhcp_manager_class_init(NMDhcpManagerClass *manager_class)
{
    GObjectClass *object_class = G_OBJECT_CLASS(manager_class);

    object_class->dispose = dispose;
}